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