Git init
[external/curl.git] / docs / examples / anyauthput.c
1 /*****************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  */
9
10 #include <stdio.h>
11 #include <fcntl.h>
12 #ifdef WIN32
13 #  include <io.h>
14 #else
15 #  ifdef __VMS
16      typedef int intptr_t;
17 #  else
18 #    include <stdint.h>
19 #  endif
20 #  include <unistd.h>
21 #endif
22 #include <sys/types.h>
23 #include <sys/stat.h>
24
25 #ifdef _MSC_VER
26 #  ifdef _WIN64
27      typedef __int64 intptr_t;
28 #  else
29      typedef int intptr_t;
30 #  endif
31 #endif
32
33 #include <curl/curl.h>
34
35 #if LIBCURL_VERSION_NUM < 0x070c03
36 #error "upgrade your libcurl to no less than 7.12.3"
37 #endif
38
39 #ifndef TRUE
40 #define TRUE 1
41 #endif
42
43 /*
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.
47  *
48  * Since libcurl 7.12.3, using "any" auth and POST/PUT requires a set ioctl
49  * function.
50  *
51  * This example also uses its own read callback.
52  */
53
54 /* ioctl callback function */
55 static curlioerr my_ioctl(CURL *handle, curliocmd cmd, void *userp)
56 {
57   intptr_t fd = (intptr_t)userp;
58
59   (void)handle; /* not used in here */
60
61   switch(cmd) {
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))
65       /* couldn't rewind */
66       return CURLIOE_FAILRESTART;
67
68     break;
69
70   default: /* ignore unknown commands */
71     return CURLIOE_UNKNOWNCMD;
72   }
73   return CURLIOE_OK; /* success! */
74 }
75
76 /* read callback function, fread() look alike */
77 static size_t read_callback(void *ptr, size_t size, size_t nmemb, void *stream)
78 {
79   size_t retcode;
80
81   intptr_t fd = (intptr_t)stream;
82
83   retcode = read(fd, ptr, size * nmemb);
84
85   fprintf(stderr, "*** We read %d bytes from file\n", retcode);
86
87   return retcode;
88 }
89
90 int main(int argc, char **argv)
91 {
92   CURL *curl;
93   CURLcode res;
94   intptr_t hd ;
95   struct stat file_info;
96
97   char *file;
98   char *url;
99
100   if(argc < 3)
101     return 1;
102
103   file= argv[1];
104   url = argv[2];
105
106   /* get the file size of the local file */
107   hd = open(file, O_RDONLY) ;
108   fstat(hd, &file_info);
109
110   /* In windows, this will init the winsock stuff */
111   curl_global_init(CURL_GLOBAL_ALL);
112
113   /* get a curl handle */
114   curl = curl_easy_init();
115   if(curl) {
116     /* we want to use our own read function */
117     curl_easy_setopt(curl, CURLOPT_READFUNCTION, read_callback);
118
119     /* which file to upload */
120     curl_easy_setopt(curl, CURLOPT_READDATA, (void*)hd);
121
122     /* set the ioctl function */
123     curl_easy_setopt(curl, CURLOPT_IOCTLFUNCTION, my_ioctl);
124
125     /* pass the file descriptor to the ioctl callback as well */
126     curl_easy_setopt(curl, CURLOPT_IOCTLDATA, (void*)hd);
127
128     /* enable "uploading" (which means PUT when doing HTTP) */
129     curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L) ;
130
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);
134
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);
139
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
142        data twice!!! */
143     curl_easy_setopt(curl, CURLOPT_HTTPAUTH, (long)CURLAUTH_ANY);
144
145     /* set user name and password for the authentication */
146     curl_easy_setopt(curl, CURLOPT_USERPWD, "user:password");
147
148     /* Now run off and do what you've been told! */
149     res = curl_easy_perform(curl);
150
151     /* always cleanup */
152     curl_easy_cleanup(curl);
153   }
154   close(hd); /* close the local file */
155
156   curl_global_cleanup();
157   return 0;
158 }