Annotation of embedaddon/curl/docs/examples/anyauthput.c, revision 1.1.1.1

1.1       misho       1: /***************************************************************************
                      2:  *                                  _   _ ____  _
                      3:  *  Project                     ___| | | |  _ \| |
                      4:  *                             / __| | | | |_) | |
                      5:  *                            | (__| |_| |  _ <| |___
                      6:  *                             \___|\___/|_| \_\_____|
                      7:  *
                      8:  * Copyright (C) 1998 - 2019, Daniel Stenberg, <daniel@haxx.se>, et al.
                      9:  *
                     10:  * This software is licensed as described in the file COPYING, which
                     11:  * you should have received as part of this distribution. The terms
                     12:  * are also available at https://curl.haxx.se/docs/copyright.html.
                     13:  *
                     14:  * You may opt to use, copy, modify, merge, publish, distribute and/or sell
                     15:  * copies of the Software, and permit persons to whom the Software is
                     16:  * furnished to do so, under the terms of the COPYING file.
                     17:  *
                     18:  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
                     19:  * KIND, either express or implied.
                     20:  *
                     21:  ***************************************************************************/
                     22: /* <DESC>
                     23:  * HTTP PUT upload with authentiction using "any" method. libcurl picks the
                     24:  * one the server supports/wants.
                     25:  * </DESC>
                     26:  */
                     27: #include <stdio.h>
                     28: #include <fcntl.h>
                     29: #include <sys/types.h>
                     30: #include <sys/stat.h>
                     31: 
                     32: #include <curl/curl.h>
                     33: 
                     34: #ifdef WIN32
                     35: #  include <io.h>
                     36: #  define READ_3RD_ARG unsigned int
                     37: #else
                     38: #  include <unistd.h>
                     39: #  define READ_3RD_ARG size_t
                     40: #endif
                     41: 
                     42: #if LIBCURL_VERSION_NUM < 0x070c03
                     43: #error "upgrade your libcurl to no less than 7.12.3"
                     44: #endif
                     45: 
                     46: /*
                     47:  * This example shows a HTTP PUT operation with authentiction using "any"
                     48:  * type. It PUTs a file given as a command line argument to the URL also given
                     49:  * on the command line.
                     50:  *
                     51:  * Since libcurl 7.12.3, using "any" auth and POST/PUT requires a set ioctl
                     52:  * function.
                     53:  *
                     54:  * This example also uses its own read callback.
                     55:  */
                     56: 
                     57: /* ioctl callback function */
                     58: static curlioerr my_ioctl(CURL *handle, curliocmd cmd, void *userp)
                     59: {
                     60:   int *fdp = (int *)userp;
                     61:   int fd = *fdp;
                     62: 
                     63:   (void)handle; /* not used in here */
                     64: 
                     65:   switch(cmd) {
                     66:   case CURLIOCMD_RESTARTREAD:
                     67:     /* mr libcurl kindly asks as to rewind the read data stream to start */
                     68:     if(-1 == lseek(fd, 0, SEEK_SET))
                     69:       /* couldn't rewind */
                     70:       return CURLIOE_FAILRESTART;
                     71: 
                     72:     break;
                     73: 
                     74:   default: /* ignore unknown commands */
                     75:     return CURLIOE_UNKNOWNCMD;
                     76:   }
                     77:   return CURLIOE_OK; /* success! */
                     78: }
                     79: 
                     80: /* read callback function, fread() look alike */
                     81: static size_t read_callback(void *ptr, size_t size, size_t nmemb, void *stream)
                     82: {
                     83:   ssize_t retcode;
                     84:   curl_off_t nread;
                     85: 
                     86:   int *fdp = (int *)stream;
                     87:   int fd = *fdp;
                     88: 
                     89:   retcode = read(fd, ptr, (READ_3RD_ARG)(size * nmemb));
                     90: 
                     91:   nread = (curl_off_t)retcode;
                     92: 
                     93:   fprintf(stderr, "*** We read %" CURL_FORMAT_CURL_OFF_T
                     94:           " bytes from file\n", nread);
                     95: 
                     96:   return retcode;
                     97: }
                     98: 
                     99: int main(int argc, char **argv)
                    100: {
                    101:   CURL *curl;
                    102:   CURLcode res;
                    103:   int hd;
                    104:   struct stat file_info;
                    105: 
                    106:   char *file;
                    107:   char *url;
                    108: 
                    109:   if(argc < 3)
                    110:     return 1;
                    111: 
                    112:   file = argv[1];
                    113:   url = argv[2];
                    114: 
                    115:   /* get the file size of the local file */
                    116:   hd = open(file, O_RDONLY);
                    117:   fstat(hd, &file_info);
                    118: 
                    119:   /* In windows, this will init the winsock stuff */
                    120:   curl_global_init(CURL_GLOBAL_ALL);
                    121: 
                    122:   /* get a curl handle */
                    123:   curl = curl_easy_init();
                    124:   if(curl) {
                    125:     /* we want to use our own read function */
                    126:     curl_easy_setopt(curl, CURLOPT_READFUNCTION, read_callback);
                    127: 
                    128:     /* which file to upload */
                    129:     curl_easy_setopt(curl, CURLOPT_READDATA, (void *)&hd);
                    130: 
                    131:     /* set the ioctl function */
                    132:     curl_easy_setopt(curl, CURLOPT_IOCTLFUNCTION, my_ioctl);
                    133: 
                    134:     /* pass the file descriptor to the ioctl callback as well */
                    135:     curl_easy_setopt(curl, CURLOPT_IOCTLDATA, (void *)&hd);
                    136: 
                    137:     /* enable "uploading" (which means PUT when doing HTTP) */
                    138:     curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
                    139: 
                    140:     /* specify target URL, and note that this URL should also include a file
                    141:        name, not only a directory (as you can do with GTP uploads) */
                    142:     curl_easy_setopt(curl, CURLOPT_URL, url);
                    143: 
                    144:     /* and give the size of the upload, this supports large file sizes
                    145:        on systems that have general support for it */
                    146:     curl_easy_setopt(curl, CURLOPT_INFILESIZE_LARGE,
                    147:                      (curl_off_t)file_info.st_size);
                    148: 
                    149:     /* tell libcurl we can use "any" auth, which lets the lib pick one, but it
                    150:        also costs one extra round-trip and possibly sending of all the PUT
                    151:        data twice!!! */
                    152:     curl_easy_setopt(curl, CURLOPT_HTTPAUTH, (long)CURLAUTH_ANY);
                    153: 
                    154:     /* set user name and password for the authentication */
                    155:     curl_easy_setopt(curl, CURLOPT_USERPWD, "user:password");
                    156: 
                    157:     /* Now run off and do what you've been told! */
                    158:     res = curl_easy_perform(curl);
                    159:     /* Check for errors */
                    160:     if(res != CURLE_OK)
                    161:       fprintf(stderr, "curl_easy_perform() failed: %s\n",
                    162:               curl_easy_strerror(res));
                    163: 
                    164:     /* always cleanup */
                    165:     curl_easy_cleanup(curl);
                    166:   }
                    167:   close(hd); /* close the local file */
                    168: 
                    169:   curl_global_cleanup();
                    170:   return 0;
                    171: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>