| xf.li | 6c8fc1e | 2023-08-12 00:11:09 -0700 | [diff] [blame] | 1 | /*************************************************************************** | 
|  | 2 | *                                  _   _ ____  _ | 
|  | 3 | *  Project                     ___| | | |  _ \| | | 
|  | 4 | *                             / __| | | | |_) | | | 
|  | 5 | *                            | (__| |_| |  _ <| |___ | 
|  | 6 | *                             \___|\___/|_| \_\_____| | 
|  | 7 | * | 
|  | 8 | * Copyright (C) 1998 - 2022, 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.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 | * SPDX-License-Identifier: curl | 
|  | 22 | * | 
|  | 23 | ***************************************************************************/ | 
|  | 24 | /* <DESC> | 
|  | 25 | * HTTP PUT upload with authentication using "any" method. libcurl picks the | 
|  | 26 | * one the server supports/wants. | 
|  | 27 | * </DESC> | 
|  | 28 | */ | 
|  | 29 | #include <stdio.h> | 
|  | 30 | #include <fcntl.h> | 
|  | 31 | #include <sys/types.h> | 
|  | 32 | #include <sys/stat.h> | 
|  | 33 |  | 
|  | 34 | #include <curl/curl.h> | 
|  | 35 |  | 
|  | 36 | #ifdef WIN32 | 
|  | 37 | #  define FILENO(fp) _fileno(fp) | 
|  | 38 | #else | 
|  | 39 | #  define FILENO(fp) fileno(fp) | 
|  | 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 authentication 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 seek | 
|  | 52 | * function. | 
|  | 53 | * | 
|  | 54 | * This example also uses its own read callback. | 
|  | 55 | */ | 
|  | 56 |  | 
|  | 57 | /* seek callback function */ | 
|  | 58 | static int my_seek(void *userp, curl_off_t offset, int origin) | 
|  | 59 | { | 
|  | 60 | FILE *fp = (FILE *) userp; | 
|  | 61 |  | 
|  | 62 | if(-1 == fseek(fp, (long) offset, origin)) | 
|  | 63 | /* couldn't seek */ | 
|  | 64 | return CURL_SEEKFUNC_CANTSEEK; | 
|  | 65 |  | 
|  | 66 | return CURL_SEEKFUNC_OK; /* success! */ | 
|  | 67 | } | 
|  | 68 |  | 
|  | 69 | /* read callback function, fread() look alike */ | 
|  | 70 | static size_t read_callback(char *ptr, size_t size, size_t nmemb, void *stream) | 
|  | 71 | { | 
|  | 72 | ssize_t retcode; | 
|  | 73 | unsigned long nread; | 
|  | 74 |  | 
|  | 75 | retcode = fread(ptr, size, nmemb, stream); | 
|  | 76 |  | 
|  | 77 | if(retcode > 0) { | 
|  | 78 | nread = (unsigned long)retcode; | 
|  | 79 | fprintf(stderr, "*** We read %lu bytes from file\n", nread); | 
|  | 80 | } | 
|  | 81 |  | 
|  | 82 | return retcode; | 
|  | 83 | } | 
|  | 84 |  | 
|  | 85 | int main(int argc, char **argv) | 
|  | 86 | { | 
|  | 87 | CURL *curl; | 
|  | 88 | CURLcode res; | 
|  | 89 | FILE *fp; | 
|  | 90 | struct stat file_info; | 
|  | 91 |  | 
|  | 92 | char *file; | 
|  | 93 | char *url; | 
|  | 94 |  | 
|  | 95 | if(argc < 3) | 
|  | 96 | return 1; | 
|  | 97 |  | 
|  | 98 | file = argv[1]; | 
|  | 99 | url = argv[2]; | 
|  | 100 |  | 
|  | 101 | /* get the file size of the local file */ | 
|  | 102 | fp = fopen(file, "rb"); | 
|  | 103 | fstat(FILENO(fp), &file_info); | 
|  | 104 |  | 
|  | 105 | /* In windows, this will init the winsock stuff */ | 
|  | 106 | curl_global_init(CURL_GLOBAL_ALL); | 
|  | 107 |  | 
|  | 108 | /* get a curl handle */ | 
|  | 109 | curl = curl_easy_init(); | 
|  | 110 | if(curl) { | 
|  | 111 | /* we want to use our own read function */ | 
|  | 112 | curl_easy_setopt(curl, CURLOPT_READFUNCTION, read_callback); | 
|  | 113 |  | 
|  | 114 | /* which file to upload */ | 
|  | 115 | curl_easy_setopt(curl, CURLOPT_READDATA, (void *) fp); | 
|  | 116 |  | 
|  | 117 | /* set the seek function */ | 
|  | 118 | curl_easy_setopt(curl, CURLOPT_SEEKFUNCTION, my_seek); | 
|  | 119 |  | 
|  | 120 | /* pass the file descriptor to the seek callback as well */ | 
|  | 121 | curl_easy_setopt(curl, CURLOPT_SEEKDATA, (void *) fp); | 
|  | 122 |  | 
|  | 123 | /* enable "uploading" (which means PUT when doing HTTP) */ | 
|  | 124 | curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L); | 
|  | 125 |  | 
|  | 126 | /* specify target URL, and note that this URL should also include a file | 
|  | 127 | name, not only a directory (as you can do with GTP uploads) */ | 
|  | 128 | curl_easy_setopt(curl, CURLOPT_URL, url); | 
|  | 129 |  | 
|  | 130 | /* and give the size of the upload, this supports large file sizes | 
|  | 131 | on systems that have general support for it */ | 
|  | 132 | curl_easy_setopt(curl, CURLOPT_INFILESIZE_LARGE, | 
|  | 133 | (curl_off_t)file_info.st_size); | 
|  | 134 |  | 
|  | 135 | /* tell libcurl we can use "any" auth, which lets the lib pick one, but it | 
|  | 136 | also costs one extra round-trip and possibly sending of all the PUT | 
|  | 137 | data twice!!! */ | 
|  | 138 | curl_easy_setopt(curl, CURLOPT_HTTPAUTH, (long)CURLAUTH_ANY); | 
|  | 139 |  | 
|  | 140 | /* set user name and password for the authentication */ | 
|  | 141 | curl_easy_setopt(curl, CURLOPT_USERPWD, "user:password"); | 
|  | 142 |  | 
|  | 143 | /* Now run off and do what you have been told! */ | 
|  | 144 | res = curl_easy_perform(curl); | 
|  | 145 | /* Check for errors */ | 
|  | 146 | if(res != CURLE_OK) | 
|  | 147 | fprintf(stderr, "curl_easy_perform() failed: %s\n", | 
|  | 148 | curl_easy_strerror(res)); | 
|  | 149 |  | 
|  | 150 | /* always cleanup */ | 
|  | 151 | curl_easy_cleanup(curl); | 
|  | 152 | } | 
|  | 153 | fclose(fp); /* close the local file */ | 
|  | 154 |  | 
|  | 155 | curl_global_cleanup(); | 
|  | 156 | return 0; | 
|  | 157 | } |