Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/pkg
Path: blob/main/external/curl/tests/libtest/lib1517.c
2066 views
1
/***************************************************************************
2
* _ _ ____ _
3
* Project ___| | | | _ \| |
4
* / __| | | | |_) | |
5
* | (__| |_| | _ <| |___
6
* \___|\___/|_| \_\_____|
7
*
8
* Copyright (C) Daniel Stenberg, <[email protected]>, 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
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
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
curl_mprintf("Windows TCP does not deliver response data but reports "
66
"CONNABORTED\n");
67
return TEST_ERR_FAILURE; /* skip since it fails on Windows without
68
workaround */
69
#else
70
return CURLE_OK; /* sure, run this! */
71
#endif
72
}
73
74
pooh.readptr = testdata;
75
pooh.sizeleft = strlen(testdata);
76
77
if(curl_global_init(CURL_GLOBAL_ALL)) {
78
curl_mfprintf(stderr, "curl_global_init() failed\n");
79
return TEST_ERR_MAJOR_BAD;
80
}
81
82
curl = curl_easy_init();
83
if(!curl) {
84
curl_mfprintf(stderr, "curl_easy_init() failed\n");
85
curl_global_cleanup();
86
return TEST_ERR_MAJOR_BAD;
87
}
88
89
/* First set the URL that is about to receive our POST. */
90
test_setopt(curl, CURLOPT_URL, URL);
91
92
/* Now specify we want to POST data */
93
test_setopt(curl, CURLOPT_POST, 1L);
94
95
/* Set the expected POST size */
96
test_setopt(curl, CURLOPT_POSTFIELDSIZE, (long)pooh.sizeleft);
97
98
/* we want to use our own read function */
99
test_setopt(curl, CURLOPT_READFUNCTION, read_callback);
100
101
/* pointer to pass to our read function */
102
test_setopt(curl, CURLOPT_READDATA, &pooh);
103
104
/* get verbose debug output please */
105
test_setopt(curl, CURLOPT_VERBOSE, 1L);
106
107
/* include headers in the output */
108
test_setopt(curl, CURLOPT_HEADER, 1L);
109
110
/* detect HTTP error codes >= 400 */
111
/* test_setopt(curl, CURLOPT_FAILONERROR, 1L); */
112
113
114
/* Perform the request, res will get the return code */
115
res = curl_easy_perform(curl);
116
117
test_cleanup:
118
119
/* always cleanup */
120
curl_easy_cleanup(curl);
121
curl_global_cleanup();
122
123
return res;
124
}
125
126