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 #include "test.h"
25
26 #include "memdebug.h"
27
28 static char testdata[]="this is what we post to the silly web server\n";
29
30 struct WriteThis {
31 char *readptr;
32 size_t sizeleft;
33 };
34
read_callback(char * ptr,size_t size,size_t nmemb,void * userp)35 static size_t read_callback(char *ptr, size_t size, size_t nmemb, void *userp)
36 {
37 struct WriteThis *pooh = (struct WriteThis *)userp;
38
39 if(size*nmemb < 1)
40 return 0;
41
42 if(pooh->sizeleft) {
43 *ptr = pooh->readptr[0]; /* copy one single byte */
44 pooh->readptr++; /* advance pointer */
45 pooh->sizeleft--; /* less data left */
46 return 1; /* we return 1 byte at a time! */
47 }
48
49 return 0; /* no more data left to deliver */
50 }
51
test(char * URL)52 CURLcode test(char *URL)
53 {
54 CURL *curl;
55 CURLcode res = CURLE_OK;
56
57 struct WriteThis pooh;
58
59 pooh.readptr = testdata;
60 pooh.sizeleft = strlen(testdata);
61
62 if(curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK) {
63 fprintf(stderr, "curl_global_init() failed\n");
64 return TEST_ERR_MAJOR_BAD;
65 }
66
67 curl = curl_easy_init();
68 if(!curl) {
69 fprintf(stderr, "curl_easy_init() failed\n");
70 curl_global_cleanup();
71 return TEST_ERR_MAJOR_BAD;
72 }
73
74 /* First set the URL that is about to receive our POST. */
75 test_setopt(curl, CURLOPT_URL, URL);
76
77 /* Now specify we want to POST data */
78 test_setopt(curl, CURLOPT_POST, 1L);
79
80 /* Set the expected POST size */
81 test_setopt(curl, CURLOPT_POSTFIELDSIZE, (long)pooh.sizeleft);
82
83 /* we want to use our own read function */
84 test_setopt(curl, CURLOPT_READFUNCTION, read_callback);
85
86 /* pointer to pass to our read function */
87 test_setopt(curl, CURLOPT_READDATA, &pooh);
88
89 /* get verbose debug output please */
90 test_setopt(curl, CURLOPT_VERBOSE, 1L);
91
92 /* include headers in the output */
93 test_setopt(curl, CURLOPT_HEADER, 1L);
94
95 /* Perform the request, res will get the return code */
96 res = curl_easy_perform(curl);
97
98 test_cleanup:
99
100 /* always cleanup */
101 curl_easy_cleanup(curl);
102 curl_global_cleanup();
103
104 return res;
105 }
106