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