anyauthput.c
Go to the documentation of this file.
00001 /***************************************************************************
00002  *                                  _   _ ____  _
00003  *  Project                     ___| | | |  _ \| |
00004  *                             / __| | | | |_) | |
00005  *                            | (__| |_| |  _ <| |___
00006  *                             \___|\___/|_| \_\_____|
00007  *
00008  * Copyright (C) 1998 - 2016, Daniel Stenberg, <daniel@haxx.se>, et al.
00009  *
00010  * This software is licensed as described in the file COPYING, which
00011  * you should have received as part of this distribution. The terms
00012  * are also available at https://curl.haxx.se/docs/copyright.html.
00013  *
00014  * You may opt to use, copy, modify, merge, publish, distribute and/or sell
00015  * copies of the Software, and permit persons to whom the Software is
00016  * furnished to do so, under the terms of the COPYING file.
00017  *
00018  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
00019  * KIND, either express or implied.
00020  *
00021  ***************************************************************************/
00022 /* <DESC>
00023  * HTTP PUT upload with authentiction using "any" method. libcurl picks the
00024  * one the server supports/wants.
00025  * </DESC>
00026  */
00027 #include <stdio.h>
00028 #include <fcntl.h>
00029 #ifdef WIN32
00030 #  include <io.h>
00031 #else
00032 #  ifdef __VMS
00033      typedef int intptr_t;
00034 #  endif
00035 #  if !defined(_AIX) && !defined(__sgi) && !defined(__osf__)
00036 #    include <stdint.h>
00037 #  endif
00038 #  include <unistd.h>
00039 #endif
00040 #include <sys/types.h>
00041 #include <sys/stat.h>
00042 
00043 #ifdef _MSC_VER
00044 #  ifdef _WIN64
00045      typedef __int64 intptr_t;
00046 #  else
00047      typedef int intptr_t;
00048 #  endif
00049 #endif
00050 
00051 #include <curl/curl.h>
00052 
00053 #if LIBCURL_VERSION_NUM < 0x070c03
00054 #error "upgrade your libcurl to no less than 7.12.3"
00055 #endif
00056 
00057 #ifndef TRUE
00058 #define TRUE 1
00059 #endif
00060 
00061 #if defined(_AIX) || defined(__sgi) || defined(__osf__)
00062 #ifndef intptr_t
00063 #define intptr_t long
00064 #endif
00065 #endif
00066 
00067 /*
00068  * This example shows a HTTP PUT operation with authentiction using "any"
00069  * type. It PUTs a file given as a command line argument to the URL also given
00070  * on the command line.
00071  *
00072  * Since libcurl 7.12.3, using "any" auth and POST/PUT requires a set ioctl
00073  * function.
00074  *
00075  * This example also uses its own read callback.
00076  */
00077 
00078 /* ioctl callback function */
00079 static curlioerr my_ioctl(CURL *handle, curliocmd cmd, void *userp)
00080 {
00081   int *fdp = (int *)userp;
00082   int fd = *fdp;
00083 
00084   (void)handle; /* not used in here */
00085 
00086   switch(cmd) {
00087   case CURLIOCMD_RESTARTREAD:
00088     /* mr libcurl kindly asks as to rewind the read data stream to start */
00089     if(-1 == lseek(fd, 0, SEEK_SET))
00090       /* couldn't rewind */
00091       return CURLIOE_FAILRESTART;
00092 
00093     break;
00094 
00095   default: /* ignore unknown commands */
00096     return CURLIOE_UNKNOWNCMD;
00097   }
00098   return CURLIOE_OK; /* success! */
00099 }
00100 
00101 /* read callback function, fread() look alike */
00102 static size_t read_callback(void *ptr, size_t size, size_t nmemb, void *stream)
00103 {
00104   ssize_t retcode;
00105   curl_off_t nread;
00106 
00107   int *fdp = (int *)stream;
00108   int fd = *fdp;
00109 
00110   retcode = read(fd, ptr, size * nmemb);
00111 
00112   nread = (curl_off_t)retcode;
00113 
00114   fprintf(stderr, "*** We read %" CURL_FORMAT_CURL_OFF_T
00115           " bytes from file\n", nread);
00116 
00117   return retcode;
00118 }
00119 
00120 int main(int argc, char **argv)
00121 {
00122   CURL *curl;
00123   CURLcode res;
00124   int hd;
00125   struct stat file_info;
00126 
00127   char *file;
00128   char *url;
00129 
00130   if(argc < 3)
00131     return 1;
00132 
00133   file= argv[1];
00134   url = argv[2];
00135 
00136   /* get the file size of the local file */
00137   hd = open(file, O_RDONLY);
00138   fstat(hd, &file_info);
00139 
00140   /* In windows, this will init the winsock stuff */
00141   curl_global_init(CURL_GLOBAL_ALL);
00142 
00143   /* get a curl handle */
00144   curl = curl_easy_init();
00145   if(curl) {
00146     /* we want to use our own read function */
00147     curl_easy_setopt(curl, CURLOPT_READFUNCTION, read_callback);
00148 
00149     /* which file to upload */
00150     curl_easy_setopt(curl, CURLOPT_READDATA, (void *)&hd);
00151 
00152     /* set the ioctl function */
00153     curl_easy_setopt(curl, CURLOPT_IOCTLFUNCTION, my_ioctl);
00154 
00155     /* pass the file descriptor to the ioctl callback as well */
00156     curl_easy_setopt(curl, CURLOPT_IOCTLDATA, (void *)&hd);
00157 
00158     /* enable "uploading" (which means PUT when doing HTTP) */
00159     curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
00160 
00161     /* specify target URL, and note that this URL should also include a file
00162        name, not only a directory (as you can do with GTP uploads) */
00163     curl_easy_setopt(curl, CURLOPT_URL, url);
00164 
00165     /* and give the size of the upload, this supports large file sizes
00166        on systems that have general support for it */
00167     curl_easy_setopt(curl, CURLOPT_INFILESIZE_LARGE,
00168                      (curl_off_t)file_info.st_size);
00169 
00170     /* tell libcurl we can use "any" auth, which lets the lib pick one, but it
00171        also costs one extra round-trip and possibly sending of all the PUT
00172        data twice!!! */
00173     curl_easy_setopt(curl, CURLOPT_HTTPAUTH, (long)CURLAUTH_ANY);
00174 
00175     /* set user name and password for the authentication */
00176     curl_easy_setopt(curl, CURLOPT_USERPWD, "user:password");
00177 
00178     /* Now run off and do what you've been told! */
00179     res = curl_easy_perform(curl);
00180     /* Check for errors */
00181     if(res != CURLE_OK)
00182       fprintf(stderr, "curl_easy_perform() failed: %s\n",
00183               curl_easy_strerror(res));
00184 
00185     /* always cleanup */
00186     curl_easy_cleanup(curl);
00187   }
00188   close(hd); /* close the local file */
00189 
00190   curl_global_cleanup();
00191   return 0;
00192 }


rc_visard_driver
Author(s): Heiko Hirschmueller , Christian Emmerich , Felix Ruess
autogenerated on Thu Jun 6 2019 20:43:01