• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /***************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) 1998 - 2022, 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  * SPDX-License-Identifier: curl
22  *
23  ***************************************************************************/
24 
25 #include "test.h"
26 
27 typedef struct
28 {
29   char *buf;
30   size_t len;
31 } put_buffer;
32 
put_callback(char * ptr,size_t size,size_t nmemb,void * stream)33 static size_t put_callback(char *ptr, size_t size, size_t nmemb, void *stream)
34 {
35   put_buffer *putdata = (put_buffer *)stream;
36   size_t totalsize = size * nmemb;
37   size_t tocopy = (putdata->len < totalsize) ? putdata->len : totalsize;
38   memcpy(ptr, putdata->buf, tocopy);
39   putdata->len -= tocopy;
40   putdata->buf += tocopy;
41   return tocopy;
42 }
43 
test(char * URL)44 int test(char *URL)
45 {
46   CURL *curl;
47   CURLcode res = CURLE_OUT_OF_MEMORY;
48 
49   curl_global_init(CURL_GLOBAL_DEFAULT);
50 
51   curl = curl_easy_init();
52   if(curl) {
53     const char *testput = "This is test PUT data\n";
54     put_buffer pbuf;
55 
56     /* PUT */
57     curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
58     curl_easy_setopt(curl, CURLOPT_HEADER, 1L);
59     curl_easy_setopt(curl, CURLOPT_READFUNCTION, put_callback);
60     pbuf.buf = (char *)testput;
61     pbuf.len = strlen(testput);
62     curl_easy_setopt(curl, CURLOPT_READDATA, &pbuf);
63     curl_easy_setopt(curl, CURLOPT_INFILESIZE, (long)strlen(testput));
64     res = curl_easy_setopt(curl, CURLOPT_URL, URL);
65     if(!res)
66       res = curl_easy_perform(curl);
67     if(!res) {
68       /* POST */
69       curl_easy_setopt(curl, CURLOPT_POST, 1L);
70       curl_easy_setopt(curl, CURLOPT_POSTFIELDS, testput);
71       curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, (long)strlen(testput));
72       res = curl_easy_perform(curl);
73     }
74     curl_easy_cleanup(curl);
75   }
76 
77   curl_global_cleanup();
78   return (int)res;
79 }
80