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