Resync
[CMakeLuaTailorHgBridge.git] / CMakeLua / Utilities / cmcurl-7.19.0 / docs / examples / httpput.c
blobac9255f0565b7f46e1b09f6ca4692fda9ec45aa6
1 /*****************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * $Id: httpput.c,v 1.1.1.1 2008-09-23 16:32:05 hoffman Exp $
9 */
11 #include <stdio.h>
12 #include <fcntl.h>
13 #include <sys/stat.h>
14 #include <unistd.h>
16 #include <curl/curl.h>
19 * This example shows a HTTP PUT operation. PUTs a file given as a command
20 * line argument to the URL also given on the command line.
22 * This example also uses its own read callback.
24 * Here's an article on how to setup a PUT handler for Apache:
25 * http://www.apacheweek.com/features/put
28 static size_t read_callback(void *ptr, size_t size, size_t nmemb, void *stream)
30 size_t retcode;
32 /* in real-world cases, this would probably get this data differently
33 as this fread() stuff is exactly what the library already would do
34 by default internally */
35 retcode = fread(ptr, size, nmemb, stream);
37 fprintf(stderr, "*** We read %d bytes from file\n", retcode);
39 return retcode;
42 int main(int argc, char **argv)
44 CURL *curl;
45 CURLcode res;
46 FILE * hd_src ;
47 int hd ;
48 struct stat file_info;
50 char *file;
51 char *url;
53 if(argc < 3)
54 return 1;
56 file= argv[1];
57 url = argv[2];
59 /* get the file size of the local file */
60 hd = open(file, O_RDONLY) ;
61 fstat(hd, &file_info);
62 close(hd) ;
64 /* get a FILE * of the same file, could also be made with
65 fdopen() from the previous descriptor, but hey this is just
66 an example! */
67 hd_src = fopen(file, "rb");
69 /* In windows, this will init the winsock stuff */
70 curl_global_init(CURL_GLOBAL_ALL);
72 /* get a curl handle */
73 curl = curl_easy_init();
74 if(curl) {
75 /* we want to use our own read function */
76 curl_easy_setopt(curl, CURLOPT_READFUNCTION, read_callback);
78 /* enable uploading */
79 curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
81 /* HTTP PUT please */
82 curl_easy_setopt(curl, CURLOPT_PUT, 1L);
84 /* specify target URL, and note that this URL should include a file
85 name, not only a directory */
86 curl_easy_setopt(curl, CURLOPT_URL, url);
88 /* now specify which file to upload */
89 curl_easy_setopt(curl, CURLOPT_READDATA, hd_src);
91 /* provide the size of the upload, we specicially typecast the value
92 to curl_off_t since we must be sure to use the correct data size */
93 curl_easy_setopt(curl, CURLOPT_INFILESIZE_LARGE,
94 (curl_off_t)file_info.st_size);
96 /* Now run off and do what you've been told! */
97 res = curl_easy_perform(curl);
99 /* always cleanup */
100 curl_easy_cleanup(curl);
102 fclose(hd_src); /* close the local file */
104 curl_global_cleanup();
105 return 0;