1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2017, 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.
34 #include <sys/types.h>
37 #include <curl/curl.h>
39 #if LIBCURL_VERSION_NUM < 0x070c03
40 #error "upgrade your libcurl to no less than 7.12.3"
44 * This example shows a HTTP PUT operation with authentiction using "any"
45 * type. It PUTs a file given as a command line argument to the URL also given
46 * on the command line.
48 * Since libcurl 7.12.3, using "any" auth and POST/PUT requires a set ioctl
51 * This example also uses its own read callback.
54 /* ioctl callback function */
55 static curlioerr my_ioctl(CURL *handle, curliocmd cmd, void *userp)
57 int *fdp = (int *)userp;
60 (void)handle; /* not used in here */
63 case CURLIOCMD_RESTARTREAD:
64 /* mr libcurl kindly asks as to rewind the read data stream to start */
65 if(-1 == lseek(fd, 0, SEEK_SET))
67 return CURLIOE_FAILRESTART;
71 default: /* ignore unknown commands */
72 return CURLIOE_UNKNOWNCMD;
74 return CURLIOE_OK; /* success! */
77 /* read callback function, fread() look alike */
78 static size_t read_callback(void *ptr, size_t size, size_t nmemb, void *stream)
83 int *fdp = (int *)stream;
86 retcode = read(fd, ptr, size * nmemb);
88 nread = (curl_off_t)retcode;
90 fprintf(stderr, "*** We read %" CURL_FORMAT_CURL_OFF_T
91 " bytes from file\n", nread);
96 int main(int argc, char **argv)
101 struct stat file_info;
112 /* get the file size of the local file */
113 hd = open(file, O_RDONLY);
114 fstat(hd, &file_info);
116 /* In windows, this will init the winsock stuff */
117 curl_global_init(CURL_GLOBAL_ALL);
119 /* get a curl handle */
120 curl = curl_easy_init();
122 /* we want to use our own read function */
123 curl_easy_setopt(curl, CURLOPT_READFUNCTION, read_callback);
125 /* which file to upload */
126 curl_easy_setopt(curl, CURLOPT_READDATA, (void *)&hd);
128 /* set the ioctl function */
129 curl_easy_setopt(curl, CURLOPT_IOCTLFUNCTION, my_ioctl);
131 /* pass the file descriptor to the ioctl callback as well */
132 curl_easy_setopt(curl, CURLOPT_IOCTLDATA, (void *)&hd);
134 /* enable "uploading" (which means PUT when doing HTTP) */
135 curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
137 /* specify target URL, and note that this URL should also include a file
138 name, not only a directory (as you can do with GTP uploads) */
139 curl_easy_setopt(curl, CURLOPT_URL, url);
141 /* and give the size of the upload, this supports large file sizes
142 on systems that have general support for it */
143 curl_easy_setopt(curl, CURLOPT_INFILESIZE_LARGE,
144 (curl_off_t)file_info.st_size);
146 /* tell libcurl we can use "any" auth, which lets the lib pick one, but it
147 also costs one extra round-trip and possibly sending of all the PUT
149 curl_easy_setopt(curl, CURLOPT_HTTPAUTH, (long)CURLAUTH_ANY);
151 /* set user name and password for the authentication */
152 curl_easy_setopt(curl, CURLOPT_USERPWD, "user:password");
154 /* Now run off and do what you've been told! */
155 res = curl_easy_perform(curl);
156 /* Check for errors */
158 fprintf(stderr, "curl_easy_perform() failed: %s\n",
159 curl_easy_strerror(res));
162 curl_easy_cleanup(curl);
164 close(hd); /* close the local file */
166 curl_global_cleanup();