1 /*****************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * $Id: anyauthput.c,v 1.1.1.1 2008-09-23 16:32:05 hoffman Exp $
19 #include <sys/types.h>
24 typedef __int64
intptr_t;
30 #include <curl/curl.h>
32 #if LIBCURL_VERSION_NUM < 0x070c03
33 #error "upgrade your libcurl to no less than 7.12.3"
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
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 */
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
))
63 return CURLIOE_FAILRESTART
;
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
)
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
);
87 int main(int argc
, char **argv
)
92 struct stat file_info
;
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();
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
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
);
149 curl_easy_cleanup(curl
);
151 close(hd
); /* close the local file */
153 curl_global_cleanup();