quickjs-tart

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

httpput.c (3772B)


      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 with easy interface and read callback
     26  * </DESC>
     27  */
     28 #include <stdio.h>
     29 #include <fcntl.h>
     30 #include <sys/stat.h>
     31 #include <curl/curl.h>
     32 
     33 #ifdef _WIN32
     34 #undef stat
     35 #define stat _stat
     36 #endif
     37 
     38 /*
     39  * This example shows an HTTP PUT operation. PUTs a file given as a command
     40  * line argument to the URL also given on the command line.
     41  *
     42  * This example also uses its own read callback.
     43  *
     44  * Here's an article on how to setup a PUT handler for Apache:
     45  * http://www.apacheweek.com/features/put
     46  */
     47 
     48 static size_t read_callback(char *ptr, size_t size, size_t nmemb, void *stream)
     49 {
     50   size_t retcode;
     51   unsigned long nread;
     52 
     53   /* in real-world cases, this would probably get this data differently
     54      as this fread() stuff is exactly what the library already would do
     55      by default internally */
     56   retcode = fread(ptr, size, nmemb, stream);
     57 
     58   if(retcode > 0) {
     59     nread = (unsigned long)retcode;
     60     fprintf(stderr, "*** We read %lu bytes from file\n", nread);
     61   }
     62 
     63   return retcode;
     64 }
     65 
     66 int main(int argc, char **argv)
     67 {
     68   CURL *curl;
     69   CURLcode res;
     70   FILE * hd_src;
     71   struct stat file_info;
     72 
     73   char *file;
     74   char *url;
     75 
     76   if(argc < 3)
     77     return 1;
     78 
     79   file = argv[1];
     80   url = argv[2];
     81 
     82   /* get the file size of the local file */
     83   stat(file, &file_info);
     84 
     85   /* get a FILE * of the same file, could also be made with
     86      fdopen() from the previous descriptor, but hey this is just
     87      an example! */
     88   hd_src = fopen(file, "rb");
     89   if(!hd_src)
     90     return 2;
     91 
     92   /* In Windows, this inits the Winsock stuff */
     93   curl_global_init(CURL_GLOBAL_ALL);
     94 
     95   /* get a curl handle */
     96   curl = curl_easy_init();
     97   if(curl) {
     98     /* we want to use our own read function */
     99     curl_easy_setopt(curl, CURLOPT_READFUNCTION, read_callback);
    100 
    101     /* enable uploading (implies PUT over HTTP) */
    102     curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
    103 
    104     /* specify target URL, and note that this URL should include a file
    105        name, not only a directory */
    106     curl_easy_setopt(curl, CURLOPT_URL, url);
    107 
    108     /* now specify which file to upload */
    109     curl_easy_setopt(curl, CURLOPT_READDATA, hd_src);
    110 
    111     /* provide the size of the upload, we typecast the value to curl_off_t
    112        since we must be sure to use the correct data size */
    113     curl_easy_setopt(curl, CURLOPT_INFILESIZE_LARGE,
    114                      (curl_off_t)file_info.st_size);
    115 
    116     /* Now run off and do what you have been told! */
    117     res = curl_easy_perform(curl);
    118     /* Check for errors */
    119     if(res != CURLE_OK)
    120       fprintf(stderr, "curl_easy_perform() failed: %s\n",
    121               curl_easy_strerror(res));
    122 
    123     /* always cleanup */
    124     curl_easy_cleanup(curl);
    125   }
    126   fclose(hd_src); /* close the local file */
    127 
    128   curl_global_cleanup();
    129   return 0;
    130 }