httpput.c
Go to the documentation of this file.
00001 /***************************************************************************
00002  *                                  _   _ ____  _
00003  *  Project                     ___| | | |  _ \| |
00004  *                             / __| | | | |_) | |
00005  *                            | (__| |_| |  _ <| |___
00006  *                             \___|\___/|_| \_\_____|
00007  *
00008  * Copyright (C) 1998 - 2016, Daniel Stenberg, <daniel@haxx.se>, et al.
00009  *
00010  * This software is licensed as described in the file COPYING, which
00011  * you should have received as part of this distribution. The terms
00012  * are also available at https://curl.haxx.se/docs/copyright.html.
00013  *
00014  * You may opt to use, copy, modify, merge, publish, distribute and/or sell
00015  * copies of the Software, and permit persons to whom the Software is
00016  * furnished to do so, under the terms of the COPYING file.
00017  *
00018  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
00019  * KIND, either express or implied.
00020  *
00021  ***************************************************************************/
00022 /* <DESC>
00023  * HTTP PUT with easy interface and read callback
00024  * </DESC>
00025  */
00026 #include <stdio.h>
00027 #include <fcntl.h>
00028 #include <sys/stat.h>
00029 #include <curl/curl.h>
00030 
00031 /*
00032  * This example shows a HTTP PUT operation. PUTs a file given as a command
00033  * line argument to the URL also given on the command line.
00034  *
00035  * This example also uses its own read callback.
00036  *
00037  * Here's an article on how to setup a PUT handler for Apache:
00038  * http://www.apacheweek.com/features/put
00039  */
00040 
00041 static size_t read_callback(void *ptr, size_t size, size_t nmemb, void *stream)
00042 {
00043   size_t retcode;
00044   curl_off_t nread;
00045 
00046   /* in real-world cases, this would probably get this data differently
00047      as this fread() stuff is exactly what the library already would do
00048      by default internally */
00049   retcode = fread(ptr, size, nmemb, stream);
00050 
00051   nread = (curl_off_t)retcode;
00052 
00053   fprintf(stderr, "*** We read %" CURL_FORMAT_CURL_OFF_T
00054           " bytes from file\n", nread);
00055 
00056   return retcode;
00057 }
00058 
00059 int main(int argc, char **argv)
00060 {
00061   CURL *curl;
00062   CURLcode res;
00063   FILE * hd_src;
00064   struct stat file_info;
00065 
00066   char *file;
00067   char *url;
00068 
00069   if(argc < 3)
00070     return 1;
00071 
00072   file= argv[1];
00073   url = argv[2];
00074 
00075   /* get the file size of the local file */
00076   stat(file, &file_info);
00077 
00078   /* get a FILE * of the same file, could also be made with
00079      fdopen() from the previous descriptor, but hey this is just
00080      an example! */
00081   hd_src = fopen(file, "rb");
00082 
00083   /* In windows, this will init the winsock stuff */
00084   curl_global_init(CURL_GLOBAL_ALL);
00085 
00086   /* get a curl handle */
00087   curl = curl_easy_init();
00088   if(curl) {
00089     /* we want to use our own read function */
00090     curl_easy_setopt(curl, CURLOPT_READFUNCTION, read_callback);
00091 
00092     /* enable uploading */
00093     curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
00094 
00095     /* HTTP PUT please */
00096     curl_easy_setopt(curl, CURLOPT_PUT, 1L);
00097 
00098     /* specify target URL, and note that this URL should include a file
00099        name, not only a directory */
00100     curl_easy_setopt(curl, CURLOPT_URL, url);
00101 
00102     /* now specify which file to upload */
00103     curl_easy_setopt(curl, CURLOPT_READDATA, hd_src);
00104 
00105     /* provide the size of the upload, we specicially typecast the value
00106        to curl_off_t since we must be sure to use the correct data size */
00107     curl_easy_setopt(curl, CURLOPT_INFILESIZE_LARGE,
00108                      (curl_off_t)file_info.st_size);
00109 
00110     /* Now run off and do what you've been told! */
00111     res = curl_easy_perform(curl);
00112     /* Check for errors */
00113     if(res != CURLE_OK)
00114       fprintf(stderr, "curl_easy_perform() failed: %s\n",
00115               curl_easy_strerror(res));
00116 
00117     /* always cleanup */
00118     curl_easy_cleanup(curl);
00119   }
00120   fclose(hd_src); /* close the local file */
00121 
00122   curl_global_cleanup();
00123   return 0;
00124 }


rc_visard_driver
Author(s): Heiko Hirschmueller , Christian Emmerich , Felix Ruess
autogenerated on Thu Jun 6 2019 20:43:04