1 /***************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
7 *
8 * Copyright (C) 1998 - 2017, Daniel Stenberg, <daniel@haxx.se>, et al.
9 *
10 * This software is licensed as described in the file COPYING, which
11 * you should have received as part of this distribution. The terms
12 * are also available at https://curl.haxx.se/docs/copyright.html.
13 *
14 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15 * copies of the Software, and permit persons to whom the Software is
16 * furnished to do so, under the terms of the COPYING file.
17 *
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
20 *
21 ***************************************************************************/
22 /* <DESC>
23 * HTTP PUT upload with authentiction using "any" method. libcurl picks the
24 * one the server supports/wants.
25 * </DESC>
26 */
27 #include <stdio.h>
28 #include <fcntl.h>
29 #ifdef WIN32
30 # include <io.h>
31 #else
32 # include <unistd.h>
33 #endif
34 #include <sys/types.h>
35 #include <sys/stat.h>
36
37 #include <curl/curl.h>
38
39 #if LIBCURL_VERSION_NUM < 0x070c03
40 #error "upgrade your libcurl to no less than 7.12.3"
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 */
my_ioctl(CURL * handle,curliocmd cmd,void * userp)55 static curlioerr my_ioctl(CURL *handle, curliocmd cmd, void *userp)
56 {
57 int *fdp = (int *)userp;
58 int fd = *fdp;
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 */
read_callback(void * ptr,size_t size,size_t nmemb,void * stream)78 static size_t read_callback(void *ptr, size_t size, size_t nmemb, void *stream)
79 {
80 ssize_t retcode;
81 curl_off_t nread;
82
83 int *fdp = (int *)stream;
84 int fd = *fdp;
85
86 retcode = read(fd, ptr, size * nmemb);
87
88 nread = (curl_off_t)retcode;
89
90 fprintf(stderr, "*** We read %" CURL_FORMAT_CURL_OFF_T
91 " bytes from file\n", nread);
92
93 return retcode;
94 }
95
main(int argc,char ** argv)96 int main(int argc, char **argv)
97 {
98 CURL *curl;
99 CURLcode res;
100 int hd;
101 struct stat file_info;
102
103 char *file;
104 char *url;
105
106 if(argc < 3)
107 return 1;
108
109 file = argv[1];
110 url = argv[2];
111
112 /* get the file size of the local file */
113 hd = open(file, O_RDONLY);
114 fstat(hd, &file_info);
115
116 /* In windows, this will init the winsock stuff */
117 curl_global_init(CURL_GLOBAL_ALL);
118
119 /* get a curl handle */
120 curl = curl_easy_init();
121 if(curl) {
122 /* we want to use our own read function */
123 curl_easy_setopt(curl, CURLOPT_READFUNCTION, read_callback);
124
125 /* which file to upload */
126 curl_easy_setopt(curl, CURLOPT_READDATA, (void *)&hd);
127
128 /* set the ioctl function */
129 curl_easy_setopt(curl, CURLOPT_IOCTLFUNCTION, my_ioctl);
130
131 /* pass the file descriptor to the ioctl callback as well */
132 curl_easy_setopt(curl, CURLOPT_IOCTLDATA, (void *)&hd);
133
134 /* enable "uploading" (which means PUT when doing HTTP) */
135 curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
136
137 /* specify target URL, and note that this URL should also include a file
138 name, not only a directory (as you can do with GTP uploads) */
139 curl_easy_setopt(curl, CURLOPT_URL, url);
140
141 /* and give the size of the upload, this supports large file sizes
142 on systems that have general support for it */
143 curl_easy_setopt(curl, CURLOPT_INFILESIZE_LARGE,
144 (curl_off_t)file_info.st_size);
145
146 /* tell libcurl we can use "any" auth, which lets the lib pick one, but it
147 also costs one extra round-trip and possibly sending of all the PUT
148 data twice!!! */
149 curl_easy_setopt(curl, CURLOPT_HTTPAUTH, (long)CURLAUTH_ANY);
150
151 /* set user name and password for the authentication */
152 curl_easy_setopt(curl, CURLOPT_USERPWD, "user:password");
153
154 /* Now run off and do what you've been told! */
155 res = curl_easy_perform(curl);
156 /* Check for errors */
157 if(res != CURLE_OK)
158 fprintf(stderr, "curl_easy_perform() failed: %s\n",
159 curl_easy_strerror(res));
160
161 /* always cleanup */
162 curl_easy_cleanup(curl);
163 }
164 close(hd); /* close the local file */
165
166 curl_global_cleanup();
167 return 0;
168 }
169