Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/pkg
Path: blob/main/external/curl/tests/libtest/lib1485.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 "testutil.h"
27
#include "warnless.h"
28
#include "memdebug.h"
29
30
struct transfer_status {
31
CURL *easy;
32
curl_off_t out_len;
33
size_t hd_line;
34
CURLcode result;
35
int http_status;
36
};
37
38
static size_t header_callback(char *ptr, size_t size, size_t nmemb,
39
void *userp)
40
{
41
struct transfer_status *st = (struct transfer_status *)userp;
42
const char *hd = ptr;
43
size_t len = size * nmemb;
44
CURLcode result;
45
46
(void)fwrite(ptr, size, nmemb, stdout);
47
++st->hd_line;
48
if(len == 2 && hd[0] == '\r' && hd[1] == '\n') {
49
curl_off_t clen;
50
long httpcode = 0;
51
/* end of a response */
52
result = curl_easy_getinfo(st->easy, CURLINFO_RESPONSE_CODE, &httpcode);
53
curl_mfprintf(stderr, "header_callback, get status: %ld, %d\n",
54
httpcode, result);
55
if(httpcode < 100 || httpcode >= 1000) {
56
curl_mfprintf(stderr, "header_callback, invalid status: %ld, %d\n",
57
httpcode, result);
58
return CURLE_WRITE_ERROR;
59
}
60
st->http_status = (int)httpcode;
61
if(st->http_status >= 200 && st->http_status < 300) {
62
result = curl_easy_getinfo(st->easy, CURLINFO_CONTENT_LENGTH_DOWNLOAD_T,
63
&clen);
64
curl_mfprintf(stderr, "header_callback, info Content-Length: %ld, %d\n",
65
(long)clen, result);
66
if(result) {
67
st->result = result;
68
return CURLE_WRITE_ERROR;
69
}
70
if(clen < 0) {
71
curl_mfprintf(stderr,
72
"header_callback, expected known Content-Length, "
73
"got: %ld\n", (long)clen);
74
return CURLE_WRITE_ERROR;
75
}
76
}
77
}
78
return len;
79
}
80
81
static size_t write_callback(char *ptr, size_t size, size_t nmemb, void *userp)
82
{
83
struct transfer_status *st = (struct transfer_status *)userp;
84
size_t len = size * nmemb;
85
fwrite(ptr, size, nmemb, stdout);
86
st->out_len += (curl_off_t)len;
87
return len;
88
}
89
90
CURLcode test(char *URL)
91
{
92
CURL *curls = NULL;
93
CURLcode res = CURLE_OK;
94
struct transfer_status st;
95
96
start_test_timing();
97
98
memset(&st, 0, sizeof(st));
99
100
global_init(CURL_GLOBAL_ALL);
101
102
easy_init(curls);
103
st.easy = curls; /* to allow callbacks access */
104
105
easy_setopt(curls, CURLOPT_URL, URL);
106
easy_setopt(curls, CURLOPT_WRITEFUNCTION, write_callback);
107
easy_setopt(curls, CURLOPT_WRITEDATA, &st);
108
easy_setopt(curls, CURLOPT_HEADERFUNCTION, header_callback);
109
easy_setopt(curls, CURLOPT_HEADERDATA, &st);
110
111
easy_setopt(curls, CURLOPT_NOPROGRESS, 1L);
112
113
res = curl_easy_perform(curls);
114
115
test_cleanup:
116
117
curl_easy_cleanup(curls);
118
curl_global_cleanup();
119
120
return res; /* return the final return code */
121
}
122
123