curl/docs/examples/httpput.c

122 lines
3.6 KiB
C
Raw Normal View History

/***************************************************************************
* _ _ ____ _
* Project ___| | | | _ \| |
* / __| | | | |_) | |
* | (__| |_| | _ <| |___
2001-09-25 13:55:56 +08:00
* \___|\___/|_| \_\_____|
*
* Copyright (C) 1998 - 2021, Daniel Stenberg, <daniel@haxx.se>, et al.
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
2020-11-04 21:02:01 +08:00
* are also available at https://curl.se/docs/copyright.html.
*
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
* copies of the Software, and permit persons to whom the Software is
* furnished to do so, under the terms of the COPYING file.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
***************************************************************************/
2015-07-01 17:43:12 +08:00
/* <DESC>
* HTTP PUT with easy interface and read callback
* </DESC>
*/
2001-09-25 13:55:56 +08:00
#include <stdio.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <curl/curl.h>
/*
* This example shows a HTTP PUT operation. PUTs a file given as a command
* line argument to the URL also given on the command line.
*
* This example also uses its own read callback.
*
* Here's an article on how to setup a PUT handler for Apache:
* http://www.apacheweek.com/features/put
2001-09-25 13:55:56 +08:00
*/
static size_t read_callback(char *ptr, size_t size, size_t nmemb, void *stream)
2001-09-25 13:55:56 +08:00
{
size_t retcode;
2012-04-13 23:58:41 +08:00
curl_off_t nread;
2001-09-25 13:55:56 +08:00
/* in real-world cases, this would probably get this data differently
as this fread() stuff is exactly what the library already would do
by default internally */
retcode = fread(ptr, size, nmemb, stream);
2012-04-13 23:58:41 +08:00
nread = (curl_off_t)retcode;
fprintf(stderr, "*** We read %" CURL_FORMAT_CURL_OFF_T
" bytes from file\n", nread);
2001-09-25 13:55:56 +08:00
return retcode;
}
int main(int argc, char **argv)
{
CURL *curl;
CURLcode res;
FILE * hd_src;
2001-09-25 13:55:56 +08:00
struct stat file_info;
char *file;
char *url;
if(argc < 3)
return 1;
file = argv[1];
2001-09-25 13:55:56 +08:00
url = argv[2];
2001-09-25 13:55:56 +08:00
/* get the file size of the local file */
stat(file, &file_info);
2001-09-25 13:55:56 +08:00
/* get a FILE * of the same file, could also be made with
fdopen() from the previous descriptor, but hey this is just
2001-09-25 13:55:56 +08:00
an example! */
hd_src = fopen(file, "rb");
/* In windows, this will init the winsock stuff */
curl_global_init(CURL_GLOBAL_ALL);
/* get a curl handle */
curl = curl_easy_init();
if(curl) {
/* we want to use our own read function */
curl_easy_setopt(curl, CURLOPT_READFUNCTION, read_callback);
/* enable uploading (implies PUT over HTTP) */
curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
2001-09-25 13:55:56 +08:00
/* specify target URL, and note that this URL should include a file
name, not only a directory */
curl_easy_setopt(curl, CURLOPT_URL, url);
2001-09-25 13:55:56 +08:00
/* now specify which file to upload */
2003-12-08 22:13:19 +08:00
curl_easy_setopt(curl, CURLOPT_READDATA, hd_src);
2001-09-25 13:55:56 +08:00
/* provide the size of the upload, we specicially typecast the value
to curl_off_t since we must be sure to use the correct data size */
curl_easy_setopt(curl, CURLOPT_INFILESIZE_LARGE,
(curl_off_t)file_info.st_size);
2001-09-25 13:55:56 +08:00
/* Now run off and do what you have been told! */
2001-09-25 13:55:56 +08:00
res = curl_easy_perform(curl);
2012-07-04 23:03:52 +08:00
/* Check for errors */
if(res != CURLE_OK)
fprintf(stderr, "curl_easy_perform() failed: %s\n",
curl_easy_strerror(res));
2001-09-25 13:55:56 +08:00
/* always cleanup */
curl_easy_cleanup(curl);
}
fclose(hd_src); /* close the local file */
curl_global_cleanup();
return 0;
}