Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/pkg
Path: blob/main/external/curl/tests/libtest/lib1517.c
2651 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 "first.h"
25
26
#include "memdebug.h"
27
28
struct t1517_WriteThis {
29
const char *readptr;
30
size_t sizeleft;
31
};
32
33
static size_t t1517_read_cb(char *ptr, size_t size, size_t nmemb, void *userp)
34
{
35
struct t1517_WriteThis *pooh = (struct t1517_WriteThis *)userp;
36
size_t tocopy = size * nmemb;
37
38
/* Wait one second before return POST data *
39
* so libcurl will wait before sending request body */
40
curlx_wait_ms(1000);
41
42
if(tocopy < 1 || !pooh->sizeleft)
43
return 0;
44
45
if(pooh->sizeleft < tocopy)
46
tocopy = pooh->sizeleft;
47
48
memcpy(ptr, pooh->readptr, tocopy);/* copy requested data */
49
pooh->readptr += tocopy; /* advance pointer */
50
pooh->sizeleft -= tocopy; /* less data left */
51
return tocopy;
52
}
53
54
static CURLcode test_lib1517(const char *URL)
55
{
56
static const char testdata[] =
57
"this is what we post to the silly web server\n";
58
59
CURL *curl;
60
CURLcode res = CURLE_OK;
61
62
struct t1517_WriteThis pooh;
63
64
pooh.readptr = testdata;
65
pooh.sizeleft = strlen(testdata);
66
67
if(curl_global_init(CURL_GLOBAL_ALL)) {
68
curl_mfprintf(stderr, "curl_global_init() failed\n");
69
return TEST_ERR_MAJOR_BAD;
70
}
71
72
curl = curl_easy_init();
73
if(!curl) {
74
curl_mfprintf(stderr, "curl_easy_init() failed\n");
75
curl_global_cleanup();
76
return TEST_ERR_MAJOR_BAD;
77
}
78
79
/* First set the URL that is about to receive our POST. */
80
test_setopt(curl, CURLOPT_URL, URL);
81
82
/* Now specify we want to POST data */
83
test_setopt(curl, CURLOPT_POST, 1L);
84
85
/* Set the expected POST size */
86
test_setopt(curl, CURLOPT_POSTFIELDSIZE, (long)pooh.sizeleft);
87
88
/* we want to use our own read function */
89
test_setopt(curl, CURLOPT_READFUNCTION, t1517_read_cb);
90
91
/* pointer to pass to our read function */
92
test_setopt(curl, CURLOPT_READDATA, &pooh);
93
94
/* get verbose debug output please */
95
test_setopt(curl, CURLOPT_VERBOSE, 1L);
96
97
/* include headers in the output */
98
test_setopt(curl, CURLOPT_HEADER, 1L);
99
100
/* detect HTTP error codes >= 400 */
101
/* test_setopt(curl, CURLOPT_FAILONERROR, 1L); */
102
103
104
/* Perform the request, res will get the return code */
105
res = curl_easy_perform(curl);
106
107
test_cleanup:
108
109
/* always cleanup */
110
curl_easy_cleanup(curl);
111
curl_global_cleanup();
112
113
return res;
114
}
115
116