HTTP "auth done right". See lib/README.httpauth
[platform/upstream/curl.git] / docs / examples / anyauthput.c
1 /*****************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  * $Id$
9  */
10
11 #include <stdio.h>
12 #include <fcntl.h>
13 #include <sys/stat.h>
14
15 #include <curl/curl.h>
16
17 #if LIBCURL_VERSION_NUM < 0x070c03
18 #error "upgrade your libcurl to no less than 7.12.3"
19 #endif
20
21 /*
22  * This example shows a HTTP PUT operation with authentiction using "any"
23  * type. It PUTs a file given as a command line argument to the URL also given
24  * on the command line.
25  *
26  * Since libcurl 7.12.3, using "any" auth and POST/PUT requires a set ioctl
27  * function.
28  *
29  * This example also uses its own read callback.
30  */
31
32 /* ioctl callback function */
33 static curlioerr my_ioctl(CURL *handle, curliocmd cmd, void *userp)
34 {
35   int fd = (int)userp;
36
37   (void)handle; /* not used in here */
38
39   switch(cmd) {
40   case CURLIOCMD_RESTARTREAD:
41     /* mr libcurl kindly asks as to rewind the read data stream to start */
42     if(-1 == lseek(fd, 0, SEEK_SET))
43       /* couldn't rewind */
44       return CURLIOE_FAILRESTART;
45
46     break;
47
48   default: /* ignore unknown commands */
49     return CURLIOE_UNKNOWNCMD;
50   }
51   return CURLIOE_OK; /* success! */
52 }
53
54 /* read callback function, fread() look alike */
55 size_t read_callback(void *ptr, size_t size, size_t nmemb, void *stream)
56 {
57   size_t retcode;
58
59   int fd = (int)stream;
60
61   retcode = read(fd, ptr, size * nmemb);
62
63   fprintf(stderr, "*** We read %d bytes from file\n", retcode);
64
65   return retcode;
66 }
67
68 int main(int argc, char **argv)
69 {
70   CURL *curl;
71   CURLcode res;
72   int hd ;
73   struct stat file_info;
74
75   char *file;
76   char *url;
77
78   if(argc < 3)
79     return 1;
80
81   file= argv[1];
82   url = argv[2];
83
84   /* get the file size of the local file */
85   hd = open(file, O_RDONLY) ;
86   fstat(hd, &file_info);
87
88   /* In windows, this will init the winsock stuff */
89   curl_global_init(CURL_GLOBAL_ALL);
90
91   /* get a curl handle */
92   curl = curl_easy_init();
93   if(curl) {
94     /* we want to use our own read function */
95     curl_easy_setopt(curl, CURLOPT_READFUNCTION, read_callback);
96
97     /* which file to upload */
98     curl_easy_setopt(curl, CURLOPT_READDATA, hd);
99
100     /* set the ioctl function */
101     curl_easy_setopt(curl, CURLOPT_IOCTLFUNCTION, my_ioctl);
102
103     /* pass the file descriptor to the ioctl callback as well */
104     curl_easy_setopt(curl, CURLOPT_IOCTLDATA, hd);
105
106     /* enable "uploading" (which means PUT when doing HTTP) */
107     curl_easy_setopt(curl, CURLOPT_UPLOAD, TRUE) ;
108
109     /* specify target URL, and note that this URL should also include a file
110        name, not only a directory (as you can do with GTP uploads) */
111     curl_easy_setopt(curl,CURLOPT_URL, url);
112
113     /* and give the size of the upload, this supports large file sizes
114        on systems that have general support for it */
115     curl_easy_setopt(curl, CURLOPT_INFILESIZE_LARGE, file_info.st_size);
116
117     /* tell libcurl we can use "any" auth, which lets the lib pick one, but it
118        also costs one extra round-trip and possibly sending of all the PUT
119        data twice!!! */
120     curl_easy_setopt(curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
121
122     /* set user name and password for the authentication */
123     curl_easy_setopt(curl, CURLOPT_USERPWD, "user:password");
124
125     /* Now run off and do what you've been told! */
126     res = curl_easy_perform(curl);
127
128     /* always cleanup */
129     curl_easy_cleanup(curl);
130   }
131   close(hd); /* close the local file */
132
133   curl_global_cleanup();
134   return 0;
135 }