1 /*****************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
15 #include <curl/curl.h>
17 #if LIBCURL_VERSION_NUM < 0x070c03
18 #error "upgrade your libcurl to no less than 7.12.3"
22 * This example shows a HTTP PUT operation with authentiction using "any"
23 * type. It PUTs a file given as a command line argument to the URL also given
24 * on the command line.
26 * Since libcurl 7.12.3, using "any" auth and POST/PUT requires a set ioctl
29 * This example also uses its own read callback.
32 /* ioctl callback function */
33 static curlioerr my_ioctl(CURL *handle, curliocmd cmd, void *userp)
37 (void)handle; /* not used in here */
40 case CURLIOCMD_RESTARTREAD:
41 /* mr libcurl kindly asks as to rewind the read data stream to start */
42 if(-1 == lseek(fd, 0, SEEK_SET))
44 return CURLIOE_FAILRESTART;
48 default: /* ignore unknown commands */
49 return CURLIOE_UNKNOWNCMD;
51 return CURLIOE_OK; /* success! */
54 /* read callback function, fread() look alike */
55 size_t read_callback(void *ptr, size_t size, size_t nmemb, void *stream)
61 retcode = read(fd, ptr, size * nmemb);
63 fprintf(stderr, "*** We read %d bytes from file\n", retcode);
68 int main(int argc, char **argv)
73 struct stat file_info;
84 /* get the file size of the local file */
85 hd = open(file, O_RDONLY) ;
86 fstat(hd, &file_info);
88 /* In windows, this will init the winsock stuff */
89 curl_global_init(CURL_GLOBAL_ALL);
91 /* get a curl handle */
92 curl = curl_easy_init();
94 /* we want to use our own read function */
95 curl_easy_setopt(curl, CURLOPT_READFUNCTION, read_callback);
97 /* which file to upload */
98 curl_easy_setopt(curl, CURLOPT_READDATA, hd);
100 /* set the ioctl function */
101 curl_easy_setopt(curl, CURLOPT_IOCTLFUNCTION, my_ioctl);
103 /* pass the file descriptor to the ioctl callback as well */
104 curl_easy_setopt(curl, CURLOPT_IOCTLDATA, hd);
106 /* enable "uploading" (which means PUT when doing HTTP) */
107 curl_easy_setopt(curl, CURLOPT_UPLOAD, TRUE) ;
109 /* specify target URL, and note that this URL should also include a file
110 name, not only a directory (as you can do with GTP uploads) */
111 curl_easy_setopt(curl,CURLOPT_URL, url);
113 /* and give the size of the upload, this supports large file sizes
114 on systems that have general support for it */
115 curl_easy_setopt(curl, CURLOPT_INFILESIZE_LARGE, file_info.st_size);
117 /* tell libcurl we can use "any" auth, which lets the lib pick one, but it
118 also costs one extra round-trip and possibly sending of all the PUT
120 curl_easy_setopt(curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
122 /* set user name and password for the authentication */
123 curl_easy_setopt(curl, CURLOPT_USERPWD, "user:password");
125 /* Now run off and do what you've been told! */
126 res = curl_easy_perform(curl);
129 curl_easy_cleanup(curl);
131 close(hd); /* close the local file */
133 curl_global_cleanup();