Resync
[CMakeLuaTailorHgBridge.git] / CMakeLua / Utilities / cmcurl-7.19.0 / docs / examples / anyauthput.c
blob288c0863a93e88acf825320e32dab0efc43989bd
1 /*****************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * $Id: anyauthput.c,v 1.1.1.1 2008-09-23 16:32:05 hoffman Exp $
9 */
11 #include <stdio.h>
12 #include <fcntl.h>
13 #ifdef WIN32
14 # include <io.h>
15 #else
16 # include <stdint.h>
17 # include <unistd.h>
18 #endif
19 #include <sys/types.h>
20 #include <sys/stat.h>
22 #ifdef _MSC_VER
23 # ifdef _WIN64
24 typedef __int64 intptr_t;
25 # else
26 typedef int intptr_t;
27 # endif
28 #endif
30 #include <curl/curl.h>
32 #if LIBCURL_VERSION_NUM < 0x070c03
33 #error "upgrade your libcurl to no less than 7.12.3"
34 #endif
36 #ifndef TRUE
37 #define TRUE 1
38 #endif
41 * This example shows a HTTP PUT operation with authentiction using "any"
42 * type. It PUTs a file given as a command line argument to the URL also given
43 * on the command line.
45 * Since libcurl 7.12.3, using "any" auth and POST/PUT requires a set ioctl
46 * function.
48 * This example also uses its own read callback.
51 /* ioctl callback function */
52 static curlioerr my_ioctl(CURL *handle, curliocmd cmd, void *userp)
54 intptr_t fd = (intptr_t)userp;
56 (void)handle; /* not used in here */
58 switch(cmd) {
59 case CURLIOCMD_RESTARTREAD:
60 /* mr libcurl kindly asks as to rewind the read data stream to start */
61 if(-1 == lseek(fd, 0, SEEK_SET))
62 /* couldn't rewind */
63 return CURLIOE_FAILRESTART;
65 break;
67 default: /* ignore unknown commands */
68 return CURLIOE_UNKNOWNCMD;
70 return CURLIOE_OK; /* success! */
73 /* read callback function, fread() look alike */
74 static size_t read_callback(void *ptr, size_t size, size_t nmemb, void *stream)
76 size_t retcode;
78 intptr_t fd = (intptr_t)stream;
80 retcode = read(fd, ptr, size * nmemb);
82 fprintf(stderr, "*** We read %d bytes from file\n", retcode);
84 return retcode;
87 int main(int argc, char **argv)
89 CURL *curl;
90 CURLcode res;
91 intptr_t hd ;
92 struct stat file_info;
94 char *file;
95 char *url;
97 if(argc < 3)
98 return 1;
100 file= argv[1];
101 url = argv[2];
103 /* get the file size of the local file */
104 hd = open(file, O_RDONLY) ;
105 fstat(hd, &file_info);
107 /* In windows, this will init the winsock stuff */
108 curl_global_init(CURL_GLOBAL_ALL);
110 /* get a curl handle */
111 curl = curl_easy_init();
112 if(curl) {
113 /* we want to use our own read function */
114 curl_easy_setopt(curl, CURLOPT_READFUNCTION, read_callback);
116 /* which file to upload */
117 curl_easy_setopt(curl, CURLOPT_READDATA, (void*)hd);
119 /* set the ioctl function */
120 curl_easy_setopt(curl, CURLOPT_IOCTLFUNCTION, my_ioctl);
122 /* pass the file descriptor to the ioctl callback as well */
123 curl_easy_setopt(curl, CURLOPT_IOCTLDATA, (void*)hd);
125 /* enable "uploading" (which means PUT when doing HTTP) */
126 curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L) ;
128 /* specify target URL, and note that this URL should also include a file
129 name, not only a directory (as you can do with GTP uploads) */
130 curl_easy_setopt(curl,CURLOPT_URL, url);
132 /* and give the size of the upload, this supports large file sizes
133 on systems that have general support for it */
134 curl_easy_setopt(curl, CURLOPT_INFILESIZE_LARGE,
135 (curl_off_t)file_info.st_size);
137 /* tell libcurl we can use "any" auth, which lets the lib pick one, but it
138 also costs one extra round-trip and possibly sending of all the PUT
139 data twice!!! */
140 curl_easy_setopt(curl, CURLOPT_HTTPAUTH, (long)CURLAUTH_ANY);
142 /* set user name and password for the authentication */
143 curl_easy_setopt(curl, CURLOPT_USERPWD, "user:password");
145 /* Now run off and do what you've been told! */
146 res = curl_easy_perform(curl);
148 /* always cleanup */
149 curl_easy_cleanup(curl);
151 close(hd); /* close the local file */
153 curl_global_cleanup();
154 return 0;