2011-03-10 13:48:02 +03:00
|
|
|
/***************************************************************************
|
2004-11-24 19:11:35 +03:00
|
|
|
* _ _ ____ _
|
|
|
|
* Project ___| | | | _ \| |
|
|
|
|
* / __| | | | |_) | |
|
|
|
|
* | (__| |_| | _ <| |___
|
|
|
|
* \___|\___/|_| \_\_____|
|
|
|
|
*
|
2023-01-02 15:51:48 +03:00
|
|
|
* Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
|
2011-03-10 13:48:02 +03:00
|
|
|
*
|
|
|
|
* 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 16:02:01 +03:00
|
|
|
* are also available at https://curl.se/docs/copyright.html.
|
2011-03-10 13:48:02 +03:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
2004-11-24 19:11:35 +03:00
|
|
|
* SPDX-License-Identifier: curl
|
2022-05-17 12:16:50 +03:00
|
|
|
*
|
2011-03-10 13:48:02 +03:00
|
|
|
***************************************************************************/
|
2015-06-18 11:17:02 +03:00
|
|
|
/* <DESC>
|
2020-05-25 22:44:04 +03:00
|
|
|
* HTTP PUT upload with authentication using "any" method. libcurl picks the
|
2015-06-18 11:17:02 +03:00
|
|
|
* one the server supports/wants.
|
|
|
|
* </DESC>
|
|
|
|
*/
|
2004-11-24 19:11:35 +03:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <fcntl.h>
|
2018-09-10 22:10:38 +03:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
|
|
|
|
#include <curl/curl.h>
|
|
|
|
|
2008-08-31 16:12:35 +04:00
|
|
|
#ifdef WIN32
|
2022-10-07 02:43:21 +03:00
|
|
|
# define FILENO(fp) _fileno(fp)
|
2008-08-31 16:12:35 +04:00
|
|
|
#else
|
2022-10-07 02:43:21 +03:00
|
|
|
# define FILENO(fp) fileno(fp)
|
2008-08-31 16:12:35 +04:00
|
|
|
#endif
|
2004-11-24 19:11:35 +03:00
|
|
|
|
|
|
|
#if LIBCURL_VERSION_NUM < 0x070c03
|
|
|
|
#error "upgrade your libcurl to no less than 7.12.3"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/*
|
2022-10-26 09:59:35 +03:00
|
|
|
* This example shows an HTTP PUT operation with authentication using "any"
|
2004-11-24 19:11:35 +03:00
|
|
|
* type. It PUTs a file given as a command line argument to the URL also given
|
|
|
|
* on the command line.
|
|
|
|
*
|
2022-10-07 02:43:21 +03:00
|
|
|
* Since libcurl 7.12.3, using "any" auth and POST/PUT requires a set seek
|
2004-11-24 19:11:35 +03:00
|
|
|
* function.
|
|
|
|
*
|
|
|
|
* This example also uses its own read callback.
|
|
|
|
*/
|
|
|
|
|
2022-10-07 02:43:21 +03:00
|
|
|
/* seek callback function */
|
|
|
|
static int my_seek(void *userp, curl_off_t offset, int origin)
|
2004-11-24 19:11:35 +03:00
|
|
|
{
|
2022-10-07 02:43:21 +03:00
|
|
|
FILE *fp = (FILE *) userp;
|
2004-11-24 19:11:35 +03:00
|
|
|
|
2022-10-07 02:43:21 +03:00
|
|
|
if(-1 == fseek(fp, (long) offset, origin))
|
|
|
|
/* couldn't seek */
|
|
|
|
return CURL_SEEKFUNC_CANTSEEK;
|
2004-11-24 19:11:35 +03:00
|
|
|
|
2022-10-07 02:43:21 +03:00
|
|
|
return CURL_SEEKFUNC_OK; /* success! */
|
2004-11-24 19:11:35 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* read callback function, fread() look alike */
|
2020-12-30 19:01:47 +03:00
|
|
|
static size_t read_callback(char *ptr, size_t size, size_t nmemb, void *stream)
|
2004-11-24 19:11:35 +03:00
|
|
|
{
|
2016-02-11 12:21:09 +03:00
|
|
|
ssize_t retcode;
|
2021-12-14 09:52:26 +03:00
|
|
|
unsigned long nread;
|
2004-11-24 19:11:35 +03:00
|
|
|
|
2022-10-07 02:43:21 +03:00
|
|
|
retcode = fread(ptr, size, nmemb, stream);
|
2004-11-24 19:11:35 +03:00
|
|
|
|
2021-12-14 09:52:26 +03:00
|
|
|
if(retcode > 0) {
|
|
|
|
nread = (unsigned long)retcode;
|
|
|
|
fprintf(stderr, "*** We read %lu bytes from file\n", nread);
|
|
|
|
}
|
2004-11-24 19:11:35 +03:00
|
|
|
|
|
|
|
return retcode;
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
CURL *curl;
|
|
|
|
CURLcode res;
|
2022-10-07 02:43:21 +03:00
|
|
|
FILE *fp;
|
2004-11-24 19:11:35 +03:00
|
|
|
struct stat file_info;
|
|
|
|
|
|
|
|
char *file;
|
|
|
|
char *url;
|
|
|
|
|
|
|
|
if(argc < 3)
|
|
|
|
return 1;
|
|
|
|
|
2017-09-10 00:09:06 +03:00
|
|
|
file = argv[1];
|
2004-11-24 19:11:35 +03:00
|
|
|
url = argv[2];
|
|
|
|
|
|
|
|
/* get the file size of the local file */
|
2022-10-07 02:43:21 +03:00
|
|
|
fp = fopen(file, "rb");
|
|
|
|
fstat(FILENO(fp), &file_info);
|
2004-11-24 19:11:35 +03:00
|
|
|
|
|
|
|
/* 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);
|
|
|
|
|
|
|
|
/* which file to upload */
|
2022-10-07 02:43:21 +03:00
|
|
|
curl_easy_setopt(curl, CURLOPT_READDATA, (void *) fp);
|
2004-11-24 19:11:35 +03:00
|
|
|
|
2022-10-07 02:43:21 +03:00
|
|
|
/* set the seek function */
|
|
|
|
curl_easy_setopt(curl, CURLOPT_SEEKFUNCTION, my_seek);
|
2004-11-24 19:11:35 +03:00
|
|
|
|
2022-10-07 02:43:21 +03:00
|
|
|
/* pass the file descriptor to the seek callback as well */
|
|
|
|
curl_easy_setopt(curl, CURLOPT_SEEKDATA, (void *) fp);
|
2004-11-24 19:11:35 +03:00
|
|
|
|
|
|
|
/* enable "uploading" (which means PUT when doing HTTP) */
|
2016-02-11 11:42:38 +03:00
|
|
|
curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
|
2004-11-24 19:11:35 +03:00
|
|
|
|
|
|
|
/* specify target URL, and note that this URL should also include a file
|
|
|
|
name, not only a directory (as you can do with GTP uploads) */
|
2016-02-11 11:42:38 +03:00
|
|
|
curl_easy_setopt(curl, CURLOPT_URL, url);
|
2004-11-24 19:11:35 +03:00
|
|
|
|
|
|
|
/* and give the size of the upload, this supports large file sizes
|
|
|
|
on systems that have general support for it */
|
2008-05-23 01:20:07 +04:00
|
|
|
curl_easy_setopt(curl, CURLOPT_INFILESIZE_LARGE,
|
2010-02-16 16:32:45 +03:00
|
|
|
(curl_off_t)file_info.st_size);
|
2004-11-24 19:11:35 +03:00
|
|
|
|
|
|
|
/* tell libcurl we can use "any" auth, which lets the lib pick one, but it
|
|
|
|
also costs one extra round-trip and possibly sending of all the PUT
|
|
|
|
data twice!!! */
|
2008-05-23 01:20:07 +04:00
|
|
|
curl_easy_setopt(curl, CURLOPT_HTTPAUTH, (long)CURLAUTH_ANY);
|
2004-11-24 19:11:35 +03:00
|
|
|
|
|
|
|
/* set user name and password for the authentication */
|
|
|
|
curl_easy_setopt(curl, CURLOPT_USERPWD, "user:password");
|
|
|
|
|
2021-10-31 18:34:44 +03:00
|
|
|
/* Now run off and do what you have been told! */
|
2004-11-24 19:11:35 +03:00
|
|
|
res = curl_easy_perform(curl);
|
2012-07-04 19:03:52 +04:00
|
|
|
/* Check for errors */
|
|
|
|
if(res != CURLE_OK)
|
|
|
|
fprintf(stderr, "curl_easy_perform() failed: %s\n",
|
|
|
|
curl_easy_strerror(res));
|
2004-11-24 19:11:35 +03:00
|
|
|
|
|
|
|
/* always cleanup */
|
|
|
|
curl_easy_cleanup(curl);
|
|
|
|
}
|
2022-10-07 02:43:21 +03:00
|
|
|
fclose(fp); /* close the local file */
|
2004-11-24 19:11:35 +03:00
|
|
|
|
|
|
|
curl_global_cleanup();
|
|
|
|
return 0;
|
|
|
|
}
|