httpput.c
Go to the documentation of this file.
1 /***************************************************************************
2  * _ _ ____ _
3  * Project ___| | | | _ \| |
4  * / __| | | | |_) | |
5  * | (__| |_| | _ <| |___
6  * \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) 1998 - 2017, 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.haxx.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  ***************************************************************************/
22 /* <DESC>
23  * HTTP PUT with easy interface and read callback
24  * </DESC>
25  */
26 #include <stdio.h>
27 #include <fcntl.h>
28 #include <sys/stat.h>
29 #include <curl/curl.h>
30 
31 /*
32  * This example shows a HTTP PUT operation. PUTs a file given as a command
33  * line argument to the URL also given on the command line.
34  *
35  * This example also uses its own read callback.
36  *
37  * Here's an article on how to setup a PUT handler for Apache:
38  * http://www.apacheweek.com/features/put
39  */
40 
41 static size_t read_callback(void *ptr, size_t size, size_t nmemb, void *stream)
42 {
43  size_t retcode;
44  curl_off_t nread;
45 
46  /* in real-world cases, this would probably get this data differently
47  as this fread() stuff is exactly what the library already would do
48  by default internally */
49  retcode = fread(ptr, size, nmemb, stream);
50 
51  nread = (curl_off_t)retcode;
52 
53  fprintf(stderr, "*** We read %" CURL_FORMAT_CURL_OFF_T
54  " bytes from file\n", nread);
55 
56  return retcode;
57 }
58 
59 int main(int argc, char **argv)
60 {
61  CURL *curl;
62  CURLcode res;
63  FILE * hd_src;
64  struct stat file_info;
65 
66  char *file;
67  char *url;
68 
69  if(argc < 3)
70  return 1;
71 
72  file = argv[1];
73  url = argv[2];
74 
75  /* get the file size of the local file */
76  stat(file, &file_info);
77 
78  /* get a FILE * of the same file, could also be made with
79  fdopen() from the previous descriptor, but hey this is just
80  an example! */
81  hd_src = fopen(file, "rb");
82 
83  /* In windows, this will init the winsock stuff */
85 
86  /* get a curl handle */
87  curl = curl_easy_init();
88  if(curl) {
89  /* we want to use our own read function */
90  curl_easy_setopt(curl, CURLOPT_READFUNCTION, read_callback);
91 
92  /* enable uploading */
93  curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
94 
95  /* HTTP PUT please */
96  curl_easy_setopt(curl, CURLOPT_PUT, 1L);
97 
98  /* specify target URL, and note that this URL should include a file
99  name, not only a directory */
100  curl_easy_setopt(curl, CURLOPT_URL, url);
101 
102  /* now specify which file to upload */
103  curl_easy_setopt(curl, CURLOPT_READDATA, hd_src);
104 
105  /* provide the size of the upload, we specicially typecast the value
106  to curl_off_t since we must be sure to use the correct data size */
107  curl_easy_setopt(curl, CURLOPT_INFILESIZE_LARGE,
108  (curl_off_t)file_info.st_size);
109 
110  /* Now run off and do what you've been told! */
111  res = curl_easy_perform(curl);
112  /* Check for errors */
113  if(res != CURLE_OK)
114  fprintf(stderr, "curl_easy_perform() failed: %s\n",
115  curl_easy_strerror(res));
116 
117  /* always cleanup */
118  curl_easy_cleanup(curl);
119  }
120  fclose(hd_src); /* close the local file */
121 
123  return 0;
124 }
int stat(const char *path, struct stat *buffer)
UNITTEST_START char * ptr
Definition: unit1330.c:38
CURLcode
Definition: curl.h:454
static int res
static size_t read_callback(void *ptr, size_t size, size_t nmemb, void *stream)
Definition: httpput.c:41
#define curl_easy_setopt(handle, option, value)
Definition: typecheck-gcc.h:41
CURL_EXTERN CURL * curl_easy_init(void)
Definition: easy.c:343
int main(int argc, char **argv)
Definition: httpput.c:59
CURL_EXTERN void curl_easy_cleanup(CURL *curl)
CURL_TYPEOF_CURL_OFF_T curl_off_t
Definition: system.h:420
Definition: curl.h:455
size_t fread(void *, size_t, size_t, FILE *)
CURL_EXTERN CURLcode curl_global_init(long flags)
curl_global_init() globally initializes curl given a bitwise set of the different features of what to...
Definition: easy.c:271
void CURL
Definition: curl.h:102
size_t size
Definition: unit1302.c:52
#define fprintf
Definition: curl_printf.h:41
CURL_EXTERN void curl_global_cleanup(void)
curl_global_cleanup() globally cleanups curl, uses the value of "init_flags" to determine what needs ...
Definition: easy.c:312
#define CURL_GLOBAL_ALL
Definition: curl.h:2519
static CURL * curl
Definition: sessioninfo.c:35
CURL_EXTERN const char * curl_easy_strerror(CURLcode)
Definition: strerror.c:57
CURL_EXTERN CURLcode curl_easy_perform(CURL *curl)
#define CURL_FORMAT_CURL_OFF_T
Definition: system.h:373


rc_tagdetect_client
Author(s): Monika Florek-Jasinska , Raphael Schaller
autogenerated on Sat Feb 13 2021 03:42:15