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 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
test(char * URL)56 CURLcode test(char *URL)
57 {
58 CURL *curl;
59 CURLcode res = CURLE_OK;
60
61 struct WriteThis pooh;
62
63 if(!strcmp(URL, "check")) {
64 #if (defined(_WIN32) || defined(__CYGWIN__))
65 printf("Windows TCP does not deliver response data but reports "
66 "CONNABORTED\n");
67 return (CURLcode)1; /* skip since it fails on Windows without workaround */
68 #else
69 return CURLE_OK; /* sure, run this! */
70 #endif
71 }
72
73 pooh.readptr = testdata;
74 pooh.sizeleft = strlen(testdata);
75
76 if(curl_global_init(CURL_GLOBAL_ALL)) {
77 fprintf(stderr, "curl_global_init() failed\n");
78 return TEST_ERR_MAJOR_BAD;
79 }
80
81 curl = curl_easy_init();
82 if(!curl) {
83 fprintf(stderr, "curl_easy_init() failed\n");
84 curl_global_cleanup();
85 return TEST_ERR_MAJOR_BAD;
86 }
87
88 /* First set the URL that is about to receive our POST. */
89 test_setopt(curl, CURLOPT_URL, URL);
90
91 /* Now specify we want to POST data */
92 test_setopt(curl, CURLOPT_POST, 1L);
93
94 /* Set the expected POST size */
95 test_setopt(curl, CURLOPT_POSTFIELDSIZE, (long)pooh.sizeleft);
96
97 /* we want to use our own read function */
98 test_setopt(curl, CURLOPT_READFUNCTION, read_callback);
99
100 /* pointer to pass to our read function */
101 test_setopt(curl, CURLOPT_READDATA, &pooh);
102
103 /* get verbose debug output please */
104 test_setopt(curl, CURLOPT_VERBOSE, 1L);
105
106 /* include headers in the output */
107 test_setopt(curl, CURLOPT_HEADER, 1L);
108
109 /* detect HTTP error codes >= 400 */
110 /* test_setopt(curl, CURLOPT_FAILONERROR, 1L); */
111
112
113 /* Perform the request, res will get the return code */
114 res = curl_easy_perform(curl);
115
116 test_cleanup:
117
118 /* always cleanup */
119 curl_easy_cleanup(curl);
120 curl_global_cleanup();
121
122 return res;
123 }
124