quickjs-tart

quickjs-based runtime for wallet-core logic
Log | Files | Refs | README | LICENSE

anyauthput.c (4650B)


      1 /***************************************************************************
      2  *                                  _   _ ____  _
      3  *  Project                     ___| | | |  _ \| |
      4  *                             / __| | | | |_) | |
      5  *                            | (__| |_| |  _ <| |___
      6  *                             \___|\___/|_| \_\_____|
      7  *
      8  * Copyright (C) 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.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  * SPDX-License-Identifier: curl
     22  *
     23  ***************************************************************************/
     24 /* <DESC>
     25  * HTTP PUT upload with authentication using "any" method. libcurl picks the
     26  * one the server supports/wants.
     27  * </DESC>
     28  */
     29 #include <stdio.h>
     30 #include <fcntl.h>
     31 #include <sys/types.h>
     32 #include <sys/stat.h>
     33 
     34 #include <curl/curl.h>
     35 
     36 #ifdef _WIN32
     37 #undef stat
     38 #define stat _stat
     39 #undef fstat
     40 #define fstat _fstat
     41 #define fileno _fileno
     42 #endif
     43 
     44 #if LIBCURL_VERSION_NUM < 0x070c03
     45 #error "upgrade your libcurl to no less than 7.12.3"
     46 #endif
     47 
     48 /*
     49  * This example shows an HTTP PUT operation with authentication using "any"
     50  * type. It PUTs a file given as a command line argument to the URL also given
     51  * on the command line.
     52  *
     53  * Since libcurl 7.12.3, using "any" auth and POST/PUT requires a set seek
     54  * function.
     55  *
     56  * This example also uses its own read callback.
     57  */
     58 
     59 /* seek callback function */
     60 static int my_seek(void *userp, curl_off_t offset, int origin)
     61 {
     62   FILE *fp = (FILE *) userp;
     63 
     64   if(-1 == fseek(fp, (long) offset, origin))
     65     /* could not seek */
     66     return CURL_SEEKFUNC_CANTSEEK;
     67 
     68   return CURL_SEEKFUNC_OK; /* success! */
     69 }
     70 
     71 /* read callback function, fread() look alike */
     72 static size_t read_callback(char *ptr, size_t size, size_t nmemb, void *stream)
     73 {
     74   size_t nread;
     75 
     76   nread = fread(ptr, size, nmemb, stream);
     77 
     78   if(nread > 0) {
     79     fprintf(stderr, "*** We read %lu bytes from file\n", (unsigned long)nread);
     80   }
     81 
     82   return nread;
     83 }
     84 
     85 int main(int argc, char **argv)
     86 {
     87   CURL *curl;
     88   CURLcode res;
     89   FILE *fp;
     90   struct stat file_info;
     91 
     92   char *file;
     93   char *url;
     94 
     95   if(argc < 3)
     96     return 1;
     97 
     98   file = argv[1];
     99   url = argv[2];
    100 
    101   /* get the file size of the local file */
    102   fp = fopen(file, "rb");
    103   if(!fp)
    104     return 2;
    105 
    106 #ifdef UNDER_CE
    107   stat(file, &file_info);
    108 #else
    109   fstat(fileno(fp), &file_info);
    110 #endif
    111 
    112   /* In Windows, this inits the Winsock stuff */
    113   curl_global_init(CURL_GLOBAL_ALL);
    114 
    115   /* get a curl handle */
    116   curl = curl_easy_init();
    117   if(curl) {
    118     /* we want to use our own read function */
    119     curl_easy_setopt(curl, CURLOPT_READFUNCTION, read_callback);
    120 
    121     /* which file to upload */
    122     curl_easy_setopt(curl, CURLOPT_READDATA, (void *) fp);
    123 
    124     /* set the seek function */
    125     curl_easy_setopt(curl, CURLOPT_SEEKFUNCTION, my_seek);
    126 
    127     /* pass the file descriptor to the seek callback as well */
    128     curl_easy_setopt(curl, CURLOPT_SEEKDATA, (void *) fp);
    129 
    130     /* enable "uploading" (which means PUT when doing HTTP) */
    131     curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
    132 
    133     /* specify target URL, and note that this URL should also include a file
    134        name, not only a directory (as you can do with GTP uploads) */
    135     curl_easy_setopt(curl, CURLOPT_URL, url);
    136 
    137     /* and give the size of the upload, this supports large file sizes
    138        on systems that have general support for it */
    139     curl_easy_setopt(curl, CURLOPT_INFILESIZE_LARGE,
    140                      (curl_off_t)file_info.st_size);
    141 
    142     /* tell libcurl we can use "any" auth, which lets the lib pick one, but it
    143        also costs one extra round-trip and possibly sending of all the PUT
    144        data twice!!! */
    145     curl_easy_setopt(curl, CURLOPT_HTTPAUTH, (long)CURLAUTH_ANY);
    146 
    147     /* set user name and password for the authentication */
    148     curl_easy_setopt(curl, CURLOPT_USERPWD, "user:password");
    149 
    150     /* Now run off and do what you have been told! */
    151     res = curl_easy_perform(curl);
    152     /* Check for errors */
    153     if(res != CURLE_OK)
    154       fprintf(stderr, "curl_easy_perform() failed: %s\n",
    155               curl_easy_strerror(res));
    156 
    157     /* always cleanup */
    158     curl_easy_cleanup(curl);
    159   }
    160   fclose(fp); /* close the local file */
    161 
    162   curl_global_cleanup();
    163   return 0;
    164 }