1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2016, 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 https://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 int *fdp = (int *)userp;
84 (void)handle; /* not used in here */
87 case CURLIOCMD_RESTARTREAD:
88 /* mr libcurl kindly asks as to rewind the read data stream to start */
89 if(-1 == lseek(fd, 0, SEEK_SET))
91 return CURLIOE_FAILRESTART;
95 default: /* ignore unknown commands */
96 return CURLIOE_UNKNOWNCMD;
98 return CURLIOE_OK; /* success! */
101 /* read callback function, fread() look alike */
102 static size_t read_callback(void *ptr, size_t size, size_t nmemb, void *stream)
107 int *fdp = (int *)stream;
110 retcode = read(fd, ptr, size * nmemb);
112 nread = (curl_off_t)retcode;
114 fprintf(stderr, "*** We read %" CURL_FORMAT_CURL_OFF_T
115 " bytes from file\n", nread);
120 int main(int argc, char **argv)
125 struct stat file_info;
136 /* get the file size of the local file */
137 hd = open(file, O_RDONLY);
138 fstat(hd, &file_info);
140 /* In windows, this will init the winsock stuff */
141 curl_global_init(CURL_GLOBAL_ALL);
143 /* get a curl handle */
144 curl = curl_easy_init();
146 /* we want to use our own read function */
147 curl_easy_setopt(curl, CURLOPT_READFUNCTION, read_callback);
149 /* which file to upload */
150 curl_easy_setopt(curl, CURLOPT_READDATA, (void*)&hd);
152 /* set the ioctl function */
153 curl_easy_setopt(curl, CURLOPT_IOCTLFUNCTION, my_ioctl);
155 /* pass the file descriptor to the ioctl callback as well */
156 curl_easy_setopt(curl, CURLOPT_IOCTLDATA, (void*)&hd);
158 /* enable "uploading" (which means PUT when doing HTTP) */
159 curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
161 /* specify target URL, and note that this URL should also include a file
162 name, not only a directory (as you can do with GTP uploads) */
163 curl_easy_setopt(curl, CURLOPT_URL, url);
165 /* and give the size of the upload, this supports large file sizes
166 on systems that have general support for it */
167 curl_easy_setopt(curl, CURLOPT_INFILESIZE_LARGE,
168 (curl_off_t)file_info.st_size);
170 /* tell libcurl we can use "any" auth, which lets the lib pick one, but it
171 also costs one extra round-trip and possibly sending of all the PUT
173 curl_easy_setopt(curl, CURLOPT_HTTPAUTH, (long)CURLAUTH_ANY);
175 /* set user name and password for the authentication */
176 curl_easy_setopt(curl, CURLOPT_USERPWD, "user:password");
178 /* Now run off and do what you've been told! */
179 res = curl_easy_perform(curl);
180 /* Check for errors */
182 fprintf(stderr, "curl_easy_perform() failed: %s\n",
183 curl_easy_strerror(res));
186 curl_easy_cleanup(curl);
188 close(hd); /* close the local file */
190 curl_global_cleanup();