113498266Sopenharmony_ci/***************************************************************************
213498266Sopenharmony_ci *                                  _   _ ____  _
313498266Sopenharmony_ci *  Project                     ___| | | |  _ \| |
413498266Sopenharmony_ci *                             / __| | | | |_) | |
513498266Sopenharmony_ci *                            | (__| |_| |  _ <| |___
613498266Sopenharmony_ci *                             \___|\___/|_| \_\_____|
713498266Sopenharmony_ci *
813498266Sopenharmony_ci * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
913498266Sopenharmony_ci *
1013498266Sopenharmony_ci * This software is licensed as described in the file COPYING, which
1113498266Sopenharmony_ci * you should have received as part of this distribution. The terms
1213498266Sopenharmony_ci * are also available at https://curl.se/docs/copyright.html.
1313498266Sopenharmony_ci *
1413498266Sopenharmony_ci * You may opt to use, copy, modify, merge, publish, distribute and/or sell
1513498266Sopenharmony_ci * copies of the Software, and permit persons to whom the Software is
1613498266Sopenharmony_ci * furnished to do so, under the terms of the COPYING file.
1713498266Sopenharmony_ci *
1813498266Sopenharmony_ci * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
1913498266Sopenharmony_ci * KIND, either express or implied.
2013498266Sopenharmony_ci *
2113498266Sopenharmony_ci * SPDX-License-Identifier: curl
2213498266Sopenharmony_ci *
2313498266Sopenharmony_ci ***************************************************************************/
2413498266Sopenharmony_ci/* <DESC>
2513498266Sopenharmony_ci * HTTP PUT upload with authentication using "any" method. libcurl picks the
2613498266Sopenharmony_ci * one the server supports/wants.
2713498266Sopenharmony_ci * </DESC>
2813498266Sopenharmony_ci */
2913498266Sopenharmony_ci#include <stdio.h>
3013498266Sopenharmony_ci#include <fcntl.h>
3113498266Sopenharmony_ci#include <sys/types.h>
3213498266Sopenharmony_ci#include <sys/stat.h>
3313498266Sopenharmony_ci
3413498266Sopenharmony_ci#include <curl/curl.h>
3513498266Sopenharmony_ci
3613498266Sopenharmony_ci#ifdef _WIN32
3713498266Sopenharmony_ci#  define FILENO(fp) _fileno(fp)
3813498266Sopenharmony_ci#else
3913498266Sopenharmony_ci#  define FILENO(fp) fileno(fp)
4013498266Sopenharmony_ci#endif
4113498266Sopenharmony_ci
4213498266Sopenharmony_ci#if LIBCURL_VERSION_NUM < 0x070c03
4313498266Sopenharmony_ci#error "upgrade your libcurl to no less than 7.12.3"
4413498266Sopenharmony_ci#endif
4513498266Sopenharmony_ci
4613498266Sopenharmony_ci/*
4713498266Sopenharmony_ci * This example shows an HTTP PUT operation with authentication using "any"
4813498266Sopenharmony_ci * type. It PUTs a file given as a command line argument to the URL also given
4913498266Sopenharmony_ci * on the command line.
5013498266Sopenharmony_ci *
5113498266Sopenharmony_ci * Since libcurl 7.12.3, using "any" auth and POST/PUT requires a set seek
5213498266Sopenharmony_ci * function.
5313498266Sopenharmony_ci *
5413498266Sopenharmony_ci * This example also uses its own read callback.
5513498266Sopenharmony_ci */
5613498266Sopenharmony_ci
5713498266Sopenharmony_ci/* seek callback function */
5813498266Sopenharmony_cistatic int my_seek(void *userp, curl_off_t offset, int origin)
5913498266Sopenharmony_ci{
6013498266Sopenharmony_ci  FILE *fp = (FILE *) userp;
6113498266Sopenharmony_ci
6213498266Sopenharmony_ci  if(-1 == fseek(fp, (long) offset, origin))
6313498266Sopenharmony_ci    /* could not seek */
6413498266Sopenharmony_ci    return CURL_SEEKFUNC_CANTSEEK;
6513498266Sopenharmony_ci
6613498266Sopenharmony_ci  return CURL_SEEKFUNC_OK; /* success! */
6713498266Sopenharmony_ci}
6813498266Sopenharmony_ci
6913498266Sopenharmony_ci/* read callback function, fread() look alike */
7013498266Sopenharmony_cistatic size_t read_callback(char *ptr, size_t size, size_t nmemb, void *stream)
7113498266Sopenharmony_ci{
7213498266Sopenharmony_ci  size_t nread;
7313498266Sopenharmony_ci
7413498266Sopenharmony_ci  nread = fread(ptr, size, nmemb, stream);
7513498266Sopenharmony_ci
7613498266Sopenharmony_ci  if(nread > 0) {
7713498266Sopenharmony_ci    fprintf(stderr, "*** We read %lu bytes from file\n", (unsigned long)nread);
7813498266Sopenharmony_ci  }
7913498266Sopenharmony_ci
8013498266Sopenharmony_ci  return nread;
8113498266Sopenharmony_ci}
8213498266Sopenharmony_ci
8313498266Sopenharmony_ciint main(int argc, char **argv)
8413498266Sopenharmony_ci{
8513498266Sopenharmony_ci  CURL *curl;
8613498266Sopenharmony_ci  CURLcode res;
8713498266Sopenharmony_ci  FILE *fp;
8813498266Sopenharmony_ci  struct stat file_info;
8913498266Sopenharmony_ci
9013498266Sopenharmony_ci  char *file;
9113498266Sopenharmony_ci  char *url;
9213498266Sopenharmony_ci
9313498266Sopenharmony_ci  if(argc < 3)
9413498266Sopenharmony_ci    return 1;
9513498266Sopenharmony_ci
9613498266Sopenharmony_ci  file = argv[1];
9713498266Sopenharmony_ci  url = argv[2];
9813498266Sopenharmony_ci
9913498266Sopenharmony_ci  /* get the file size of the local file */
10013498266Sopenharmony_ci  fp = fopen(file, "rb");
10113498266Sopenharmony_ci  fstat(FILENO(fp), &file_info);
10213498266Sopenharmony_ci
10313498266Sopenharmony_ci  /* In windows, this will init the winsock stuff */
10413498266Sopenharmony_ci  curl_global_init(CURL_GLOBAL_ALL);
10513498266Sopenharmony_ci
10613498266Sopenharmony_ci  /* get a curl handle */
10713498266Sopenharmony_ci  curl = curl_easy_init();
10813498266Sopenharmony_ci  if(curl) {
10913498266Sopenharmony_ci    /* we want to use our own read function */
11013498266Sopenharmony_ci    curl_easy_setopt(curl, CURLOPT_READFUNCTION, read_callback);
11113498266Sopenharmony_ci
11213498266Sopenharmony_ci    /* which file to upload */
11313498266Sopenharmony_ci    curl_easy_setopt(curl, CURLOPT_READDATA, (void *) fp);
11413498266Sopenharmony_ci
11513498266Sopenharmony_ci    /* set the seek function */
11613498266Sopenharmony_ci    curl_easy_setopt(curl, CURLOPT_SEEKFUNCTION, my_seek);
11713498266Sopenharmony_ci
11813498266Sopenharmony_ci    /* pass the file descriptor to the seek callback as well */
11913498266Sopenharmony_ci    curl_easy_setopt(curl, CURLOPT_SEEKDATA, (void *) fp);
12013498266Sopenharmony_ci
12113498266Sopenharmony_ci    /* enable "uploading" (which means PUT when doing HTTP) */
12213498266Sopenharmony_ci    curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
12313498266Sopenharmony_ci
12413498266Sopenharmony_ci    /* specify target URL, and note that this URL should also include a file
12513498266Sopenharmony_ci       name, not only a directory (as you can do with GTP uploads) */
12613498266Sopenharmony_ci    curl_easy_setopt(curl, CURLOPT_URL, url);
12713498266Sopenharmony_ci
12813498266Sopenharmony_ci    /* and give the size of the upload, this supports large file sizes
12913498266Sopenharmony_ci       on systems that have general support for it */
13013498266Sopenharmony_ci    curl_easy_setopt(curl, CURLOPT_INFILESIZE_LARGE,
13113498266Sopenharmony_ci                     (curl_off_t)file_info.st_size);
13213498266Sopenharmony_ci
13313498266Sopenharmony_ci    /* tell libcurl we can use "any" auth, which lets the lib pick one, but it
13413498266Sopenharmony_ci       also costs one extra round-trip and possibly sending of all the PUT
13513498266Sopenharmony_ci       data twice!!! */
13613498266Sopenharmony_ci    curl_easy_setopt(curl, CURLOPT_HTTPAUTH, (long)CURLAUTH_ANY);
13713498266Sopenharmony_ci
13813498266Sopenharmony_ci    /* set user name and password for the authentication */
13913498266Sopenharmony_ci    curl_easy_setopt(curl, CURLOPT_USERPWD, "user:password");
14013498266Sopenharmony_ci
14113498266Sopenharmony_ci    /* Now run off and do what you have been told! */
14213498266Sopenharmony_ci    res = curl_easy_perform(curl);
14313498266Sopenharmony_ci    /* Check for errors */
14413498266Sopenharmony_ci    if(res != CURLE_OK)
14513498266Sopenharmony_ci      fprintf(stderr, "curl_easy_perform() failed: %s\n",
14613498266Sopenharmony_ci              curl_easy_strerror(res));
14713498266Sopenharmony_ci
14813498266Sopenharmony_ci    /* always cleanup */
14913498266Sopenharmony_ci    curl_easy_cleanup(curl);
15013498266Sopenharmony_ci  }
15113498266Sopenharmony_ci  fclose(fp); /* close the local file */
15213498266Sopenharmony_ci
15313498266Sopenharmony_ci  curl_global_cleanup();
15413498266Sopenharmony_ci  return 0;
15513498266Sopenharmony_ci}
156