1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2015, Daniel Stenberg, <daniel@haxx.se>, et al.
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 http://curl.haxx.se/docs/copyright.html.
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.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 ***************************************************************************/
23 * HTTP PUT upload with authentiction using "any" method. libcurl picks the
24 * one the server supports/wants.
35 # if !defined(_AIX) && !defined(__sgi) && !defined(__osf__)
40 #include <sys/types.h>
45 typedef __int64 intptr_t;
51 #include <curl/curl.h>
53 #if LIBCURL_VERSION_NUM < 0x070c03
54 #error "upgrade your libcurl to no less than 7.12.3"
61 #if defined(_AIX) || defined(__sgi) || defined(__osf__)
68 * This example shows a HTTP PUT operation with authentiction using "any"
69 * type. It PUTs a file given as a command line argument to the URL also given
70 * on the command line.
72 * Since libcurl 7.12.3, using "any" auth and POST/PUT requires a set ioctl
75 * This example also uses its own read callback.
78 /* ioctl callback function */
79 static curlioerr my_ioctl(CURL *handle, curliocmd cmd, void *userp)
81 intptr_t fd = (intptr_t)userp;
83 (void)handle; /* not used in here */
86 case CURLIOCMD_RESTARTREAD:
87 /* mr libcurl kindly asks as to rewind the read data stream to start */
88 if(-1 == lseek(fd, 0, SEEK_SET))
90 return CURLIOE_FAILRESTART;
94 default: /* ignore unknown commands */
95 return CURLIOE_UNKNOWNCMD;
97 return CURLIOE_OK; /* success! */
100 /* read callback function, fread() look alike */
101 static size_t read_callback(void *ptr, size_t size, size_t nmemb, void *stream)
106 intptr_t fd = (intptr_t)stream;
108 retcode = read(fd, ptr, size * nmemb);
110 nread = (curl_off_t)retcode;
112 fprintf(stderr, "*** We read %" CURL_FORMAT_CURL_OFF_T
113 " bytes from file\n", nread);
118 int main(int argc, char **argv)
123 struct stat file_info;
134 /* get the file size of the local file */
135 hd = open(file, O_RDONLY) ;
136 fstat(hd, &file_info);
138 /* In windows, this will init the winsock stuff */
139 curl_global_init(CURL_GLOBAL_ALL);
141 /* get a curl handle */
142 curl = curl_easy_init();
144 /* we want to use our own read function */
145 curl_easy_setopt(curl, CURLOPT_READFUNCTION, read_callback);
147 /* which file to upload */
148 curl_easy_setopt(curl, CURLOPT_READDATA, (void*)hd);
150 /* set the ioctl function */
151 curl_easy_setopt(curl, CURLOPT_IOCTLFUNCTION, my_ioctl);
153 /* pass the file descriptor to the ioctl callback as well */
154 curl_easy_setopt(curl, CURLOPT_IOCTLDATA, (void*)hd);
156 /* enable "uploading" (which means PUT when doing HTTP) */
157 curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L) ;
159 /* specify target URL, and note that this URL should also include a file
160 name, not only a directory (as you can do with GTP uploads) */
161 curl_easy_setopt(curl,CURLOPT_URL, url);
163 /* and give the size of the upload, this supports large file sizes
164 on systems that have general support for it */
165 curl_easy_setopt(curl, CURLOPT_INFILESIZE_LARGE,
166 (curl_off_t)file_info.st_size);
168 /* tell libcurl we can use "any" auth, which lets the lib pick one, but it
169 also costs one extra round-trip and possibly sending of all the PUT
171 curl_easy_setopt(curl, CURLOPT_HTTPAUTH, (long)CURLAUTH_ANY);
173 /* set user name and password for the authentication */
174 curl_easy_setopt(curl, CURLOPT_USERPWD, "user:password");
176 /* Now run off and do what you've been told! */
177 res = curl_easy_perform(curl);
178 /* Check for errors */
180 fprintf(stderr, "curl_easy_perform() failed: %s\n",
181 curl_easy_strerror(res));
184 curl_easy_cleanup(curl);
186 close(hd); /* close the local file */
188 curl_global_cleanup();