1 /*****************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
23 #include <sys/types.h>
28 typedef __int64 intptr_t;
34 #include <curl/curl.h>
36 #if LIBCURL_VERSION_NUM < 0x070c03
37 #error "upgrade your libcurl to no less than 7.12.3"
45 * This example shows a HTTP PUT operation with authentiction using "any"
46 * type. It PUTs a file given as a command line argument to the URL also given
47 * on the command line.
49 * Since libcurl 7.12.3, using "any" auth and POST/PUT requires a set ioctl
52 * This example also uses its own read callback.
55 /* ioctl callback function */
56 static curlioerr my_ioctl(CURL *handle, curliocmd cmd, void *userp)
58 intptr_t fd = (intptr_t)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)
82 intptr_t fd = (intptr_t)stream;
84 retcode = read(fd, ptr, size * nmemb);
86 fprintf(stderr, "*** We read %d bytes from file\n", retcode);
91 int main(int argc, char **argv)
96 struct stat file_info;
107 /* get the file size of the local file */
108 hd = open(file, O_RDONLY) ;
109 fstat(hd, &file_info);
111 /* In windows, this will init the winsock stuff */
112 curl_global_init(CURL_GLOBAL_ALL);
114 /* get a curl handle */
115 curl = curl_easy_init();
117 /* we want to use our own read function */
118 curl_easy_setopt(curl, CURLOPT_READFUNCTION, read_callback);
120 /* which file to upload */
121 curl_easy_setopt(curl, CURLOPT_READDATA, (void*)hd);
123 /* set the ioctl function */
124 curl_easy_setopt(curl, CURLOPT_IOCTLFUNCTION, my_ioctl);
126 /* pass the file descriptor to the ioctl callback as well */
127 curl_easy_setopt(curl, CURLOPT_IOCTLDATA, (void*)hd);
129 /* enable "uploading" (which means PUT when doing HTTP) */
130 curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L) ;
132 /* specify target URL, and note that this URL should also include a file
133 name, not only a directory (as you can do with GTP uploads) */
134 curl_easy_setopt(curl,CURLOPT_URL, url);
136 /* and give the size of the upload, this supports large file sizes
137 on systems that have general support for it */
138 curl_easy_setopt(curl, CURLOPT_INFILESIZE_LARGE,
139 (curl_off_t)file_info.st_size);
141 /* tell libcurl we can use "any" auth, which lets the lib pick one, but it
142 also costs one extra round-trip and possibly sending of all the PUT
144 curl_easy_setopt(curl, CURLOPT_HTTPAUTH, (long)CURLAUTH_ANY);
146 /* set user name and password for the authentication */
147 curl_easy_setopt(curl, CURLOPT_USERPWD, "user:password");
149 /* Now run off and do what you've been told! */
150 res = curl_easy_perform(curl);
153 curl_easy_cleanup(curl);
155 close(hd); /* close the local file */
157 curl_global_cleanup();