blob: 85e027a93cf8cb69aeda1d91d083350baecdfcee [file] [log] [blame]
xf.li6c8fc1e2023-08-12 00:11:09 -07001/***************************************************************************
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.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
28static char data[]="this is what we post to the silly web server\n";
29
30struct WriteThis {
31 char *readptr;
32 size_t sizeleft;
33};
34
35static size_t read_callback(char *ptr, size_t size, size_t nmemb, void *userp)
36{
37 struct WriteThis *pooh = (struct WriteThis *)userp;
38 size_t tocopy = size * nmemb;
39
40 /* Wait one second before return POST data *
41 * so libcurl will wait before sending request body */
42 wait_ms(1000);
43
44 if(tocopy < 1 || !pooh->sizeleft)
45 return 0;
46
47 if(pooh->sizeleft < tocopy)
48 tocopy = pooh->sizeleft;
49
50 memcpy(ptr, pooh->readptr, tocopy);/* copy requested data */
51 pooh->readptr += tocopy; /* advance pointer */
52 pooh->sizeleft -= tocopy; /* less data left */
53 return tocopy;
54}
55
56int test(char *URL)
57{
58 CURL *curl;
59 CURLcode res = CURLE_OK;
60
61 struct WriteThis pooh;
62
63 pooh.readptr = data;
64 pooh.sizeleft = strlen(data);
65
66 if(curl_global_init(CURL_GLOBAL_ALL)) {
67 fprintf(stderr, "curl_global_init() failed\n");
68 return TEST_ERR_MAJOR_BAD;
69 }
70
71 curl = curl_easy_init();
72 if(!curl) {
73 fprintf(stderr, "curl_easy_init() failed\n");
74 curl_global_cleanup();
75 return TEST_ERR_MAJOR_BAD;
76 }
77
78 /* First set the URL that is about to receive our POST. */
79 test_setopt(curl, CURLOPT_URL, URL);
80
81 /* Now specify we want to POST data */
82 test_setopt(curl, CURLOPT_POST, 1L);
83
84 /* Set the expected POST size */
85 test_setopt(curl, CURLOPT_POSTFIELDSIZE, (long)pooh.sizeleft);
86
87 /* we want to use our own read function */
88 test_setopt(curl, CURLOPT_READFUNCTION, read_callback);
89
90 /* pointer to pass to our read function */
91 test_setopt(curl, CURLOPT_READDATA, &pooh);
92
93 /* get verbose debug output please */
94 test_setopt(curl, CURLOPT_VERBOSE, 1L);
95
96 /* include headers in the output */
97 test_setopt(curl, CURLOPT_HEADER, 1L);
98
99 /* detect HTTP error codes >= 400 */
100 /* test_setopt(curl, CURLOPT_FAILONERROR, 1L); */
101
102
103 /* Perform the request, res will get the return code */
104 res = curl_easy_perform(curl);
105
106test_cleanup:
107
108 /* always cleanup */
109 curl_easy_cleanup(curl);
110 curl_global_cleanup();
111
112 return res;
113}