1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2011, 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 ***************************************************************************/
32 #include <sys/types.h>
37 typedef __int64 intptr_t;
43 #include <curl/curl.h>
44 #include "printf_macro.h"
46 #if LIBCURL_VERSION_NUM < 0x070c03
47 #error "upgrade your libcurl to no less than 7.12.3"
55 * This example shows a HTTP PUT operation with authentiction using "any"
56 * type. It PUTs a file given as a command line argument to the URL also given
57 * on the command line.
59 * Since libcurl 7.12.3, using "any" auth and POST/PUT requires a set ioctl
62 * This example also uses its own read callback.
65 /* ioctl callback function */
66 static curlioerr my_ioctl(CURL *handle, curliocmd cmd, void *userp)
68 intptr_t fd = (intptr_t)userp;
70 (void)handle; /* not used in here */
73 case CURLIOCMD_RESTARTREAD:
74 /* mr libcurl kindly asks as to rewind the read data stream to start */
75 if(-1 == lseek(fd, 0, SEEK_SET))
77 return CURLIOE_FAILRESTART;
81 default: /* ignore unknown commands */
82 return CURLIOE_UNKNOWNCMD;
84 return CURLIOE_OK; /* success! */
87 /* read callback function, fread() look alike */
88 static size_t read_callback(void *ptr, size_t size, size_t nmemb, void *stream)
92 intptr_t fd = (intptr_t)stream;
94 retcode = read(fd, ptr, size * nmemb);
96 fprintf(stderr, "*** We read %" _FMT_SIZE_T " bytes from file\n", retcode);
101 int main(int argc, char **argv)
106 struct stat file_info;
117 /* get the file size of the local file */
118 hd = open(file, O_RDONLY) ;
119 fstat(hd, &file_info);
121 /* In windows, this will init the winsock stuff */
122 curl_global_init(CURL_GLOBAL_ALL);
124 /* get a curl handle */
125 curl = curl_easy_init();
127 /* we want to use our own read function */
128 curl_easy_setopt(curl, CURLOPT_READFUNCTION, read_callback);
130 /* which file to upload */
131 curl_easy_setopt(curl, CURLOPT_READDATA, (void*)hd);
133 /* set the ioctl function */
134 curl_easy_setopt(curl, CURLOPT_IOCTLFUNCTION, my_ioctl);
136 /* pass the file descriptor to the ioctl callback as well */
137 curl_easy_setopt(curl, CURLOPT_IOCTLDATA, (void*)hd);
139 /* enable "uploading" (which means PUT when doing HTTP) */
140 curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L) ;
142 /* specify target URL, and note that this URL should also include a file
143 name, not only a directory (as you can do with GTP uploads) */
144 curl_easy_setopt(curl,CURLOPT_URL, url);
146 /* and give the size of the upload, this supports large file sizes
147 on systems that have general support for it */
148 curl_easy_setopt(curl, CURLOPT_INFILESIZE_LARGE,
149 (curl_off_t)file_info.st_size);
151 /* tell libcurl we can use "any" auth, which lets the lib pick one, but it
152 also costs one extra round-trip and possibly sending of all the PUT
154 curl_easy_setopt(curl, CURLOPT_HTTPAUTH, (long)CURLAUTH_ANY);
156 /* set user name and password for the authentication */
157 curl_easy_setopt(curl, CURLOPT_USERPWD, "user:password");
159 /* Now run off and do what you've been told! */
160 res = curl_easy_perform(curl);
163 curl_easy_cleanup(curl);
165 close(hd); /* close the local file */
167 curl_global_cleanup();