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 "curlcheck.h"
25
26 #include "urldata.h"
27 #include "http.h"
28 #include "http1.h"
29 #include "curl_trc.h"
30
unit_setup(void)31 static CURLcode unit_setup(void)
32 {
33 return CURLE_OK;
34 }
35
unit_stop(void)36 static void unit_stop(void)
37 {
38 }
39
40 #ifndef CURL_DISABLE_HTTP
41 struct tcase {
42 const char **input;
43 const char *default_scheme;
44 const char *method;
45 const char *scheme;
46 const char *authority;
47 const char *path;
48 size_t header_count;
49 size_t input_remain;
50 };
51
check_eq(const char * s,const char * exp_s,const char * name)52 static void check_eq(const char *s, const char *exp_s, const char *name)
53 {
54 if(s && exp_s) {
55 if(strcmp(s, exp_s)) {
56 fprintf(stderr, "expected %s: '%s' but got '%s'\n", name, exp_s, s);
57 fail("unexpected req component");
58 }
59 }
60 else if(!s && exp_s) {
61 fprintf(stderr, "expected %s: '%s' but got NULL\n", name, exp_s);
62 fail("unexpected req component");
63 }
64 else if(s && !exp_s) {
65 fprintf(stderr, "expected %s: NULL but got '%s'\n", name, s);
66 fail("unexpected req component");
67 }
68 }
69
parse_success(struct tcase * t)70 static void parse_success(struct tcase *t)
71 {
72 struct h1_req_parser p;
73 const char *buf;
74 size_t buflen, i, in_len, in_consumed;
75 CURLcode err;
76 ssize_t nread;
77
78 Curl_h1_req_parse_init(&p, 1024);
79 in_len = in_consumed = 0;
80 for(i = 0; t->input[i]; ++i) {
81 buf = t->input[i];
82 buflen = strlen(buf);
83 in_len += buflen;
84 nread = Curl_h1_req_parse_read(&p, buf, buflen, t->default_scheme,
85 0, &err);
86 if(nread < 0) {
87 fprintf(stderr, "got err %d parsing: '%s'\n", err, buf);
88 fail("error consuming");
89 }
90 in_consumed += (size_t)nread;
91 if((size_t)nread != buflen) {
92 if(!p.done) {
93 fprintf(stderr, "only %zd/%zu consumed for: '%s'\n",
94 nread, buflen, buf);
95 fail("not all consumed");
96 }
97 }
98 }
99
100 fail_if(!p.done, "end not detected");
101 fail_if(!p.req, "not request created");
102 if(t->input_remain != (in_len - in_consumed)) {
103 fprintf(stderr, "expected %zu input bytes to remain, but got %zu\n",
104 t->input_remain, in_len - in_consumed);
105 fail("unexpected input consumption");
106 }
107 if(p.req) {
108 check_eq(p.req->method, t->method, "method");
109 check_eq(p.req->scheme, t->scheme, "scheme");
110 check_eq(p.req->authority, t->authority, "authority");
111 check_eq(p.req->path, t->path, "path");
112 if(Curl_dynhds_count(&p.req->headers) != t->header_count) {
113 fprintf(stderr, "expected %zu headers but got %zu\n", t->header_count,
114 Curl_dynhds_count(&p.req->headers));
115 fail("unexpected req header count");
116 }
117 }
118
119 Curl_h1_req_parse_free(&p);
120 }
121
122 static const char *T1_INPUT[] = {
123 "GET /path HTTP/1.1\r\nHost: test.curl.se\r\n\r\n",
124 NULL,
125 };
126 static struct tcase TEST1a = {
127 T1_INPUT, NULL, "GET", NULL, NULL, "/path", 1, 0
128 };
129 static struct tcase TEST1b = {
130 T1_INPUT, "https", "GET", "https", NULL, "/path", 1, 0
131 };
132
133 static const char *T2_INPUT[] = {
134 "GET /path HTT",
135 "P/1.1\r\nHost: te",
136 "st.curl.se\r\n\r",
137 "\n12345678",
138 NULL,
139 };
140 static struct tcase TEST2 = {
141 T2_INPUT, NULL, "GET", NULL, NULL, "/path", 1, 8
142 };
143
144 static const char *T3_INPUT[] = {
145 "GET ftp://ftp.curl.se/xxx?a=2 HTTP/1.1\r\nContent-Length: 0\r",
146 "\nUser-Agent: xxx\r\n\r\n",
147 NULL,
148 };
149 static struct tcase TEST3a = {
150 T3_INPUT, NULL, "GET", "ftp", "ftp.curl.se", "/xxx?a=2", 2, 0
151 };
152
153 static const char *T4_INPUT[] = {
154 "CONNECT ftp.curl.se:123 HTTP/1.1\r\nContent-Length: 0\r\n",
155 "User-Agent: xxx\r\n",
156 "nothing: \r\n\r\n\n\n",
157 NULL,
158 };
159 static struct tcase TEST4a = {
160 T4_INPUT, NULL, "CONNECT", NULL, "ftp.curl.se:123", NULL, 3, 2
161 };
162
163 static const char *T5_INPUT[] = {
164 "OPTIONS * HTTP/1.1\r\nContent-Length: 0\r\nBlabla: xxx.yyy\r",
165 "\n\tzzzzzz\r\n\r\n",
166 "123",
167 NULL,
168 };
169 static struct tcase TEST5a = {
170 T5_INPUT, NULL, "OPTIONS", NULL, NULL, "*", 2, 3
171 };
172
173 static const char *T6_INPUT[] = {
174 "PUT /path HTTP/1.1\nHost: test.curl.se\n\n123",
175 NULL,
176 };
177 static struct tcase TEST6a = {
178 T6_INPUT, NULL, "PUT", NULL, NULL, "/path", 1, 3
179 };
180 #endif
181
182 UNITTEST_START
183
184 #ifndef CURL_DISABLE_HTTP
185 parse_success(&TEST1a);
186 parse_success(&TEST1b);
187 parse_success(&TEST2);
188 parse_success(&TEST3a);
189 parse_success(&TEST4a);
190 parse_success(&TEST5a);
191 parse_success(&TEST6a);
192 #endif
193
194 UNITTEST_STOP
195