1 /*****************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
22 #include <sys/types.h>
27 typedef __int64 intptr_t;
33 #include <curl/curl.h>
35 #if LIBCURL_VERSION_NUM < 0x070c03
36 #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 intptr_t fd = (intptr_t)userp;
59 (void)handle; /* not used in here */
62 case CURLIOCMD_RESTARTREAD:
63 /* mr libcurl kindly asks as to rewind the read data stream to start */
64 if(-1 == lseek(fd, 0, SEEK_SET))
66 return CURLIOE_FAILRESTART;
70 default: /* ignore unknown commands */
71 return CURLIOE_UNKNOWNCMD;
73 return CURLIOE_OK; /* success! */
76 /* read callback function, fread() look alike */
77 static size_t read_callback(void *ptr, size_t size, size_t nmemb, void *stream)
81 intptr_t fd = (intptr_t)stream;
83 retcode = read(fd, ptr, size * nmemb);
85 fprintf(stderr, "*** We read %d bytes from file\n", retcode);
90 int main(int argc, char **argv)
95 struct stat file_info;
106 /* get the file size of the local file */
107 hd = open(file, O_RDONLY) ;
108 fstat(hd, &file_info);
110 /* In windows, this will init the winsock stuff */
111 curl_global_init(CURL_GLOBAL_ALL);
113 /* get a curl handle */
114 curl = curl_easy_init();
116 /* we want to use our own read function */
117 curl_easy_setopt(curl, CURLOPT_READFUNCTION, read_callback);
119 /* which file to upload */
120 curl_easy_setopt(curl, CURLOPT_READDATA, (void*)hd);
122 /* set the ioctl function */
123 curl_easy_setopt(curl, CURLOPT_IOCTLFUNCTION, my_ioctl);
125 /* pass the file descriptor to the ioctl callback as well */
126 curl_easy_setopt(curl, CURLOPT_IOCTLDATA, (void*)hd);
128 /* enable "uploading" (which means PUT when doing HTTP) */
129 curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L) ;
131 /* specify target URL, and note that this URL should also include a file
132 name, not only a directory (as you can do with GTP uploads) */
133 curl_easy_setopt(curl,CURLOPT_URL, url);
135 /* and give the size of the upload, this supports large file sizes
136 on systems that have general support for it */
137 curl_easy_setopt(curl, CURLOPT_INFILESIZE_LARGE,
138 (curl_off_t)file_info.st_size);
140 /* tell libcurl we can use "any" auth, which lets the lib pick one, but it
141 also costs one extra round-trip and possibly sending of all the PUT
143 curl_easy_setopt(curl, CURLOPT_HTTPAUTH, (long)CURLAUTH_ANY);
145 /* set user name and password for the authentication */
146 curl_easy_setopt(curl, CURLOPT_USERPWD, "user:password");
148 /* Now run off and do what you've been told! */
149 res = curl_easy_perform(curl);
152 curl_easy_cleanup(curl);
154 close(hd); /* close the local file */
156 curl_global_cleanup();