1 //
2 //
3 // Copyright 2015 gRPC authors.
4 //
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17 //
18
19 #include "src/core/util/http_client/parser.h"
20
21 #include <grpc/support/alloc.h>
22 #include <stdarg.h>
23 #include <string.h>
24
25 #include <string>
26
27 #include "absl/status/status.h"
28 #include "absl/strings/str_format.h"
29 #include "gtest/gtest.h"
30 #include "src/core/util/useful.h"
31 #include "test/core/test_util/slice_splitter.h"
32 #include "test/core/test_util/test_config.h"
33
test_request_succeeds(grpc_slice_split_mode split_mode,const char * request_text,const char * expect_method,grpc_http_version expect_version,const char * expect_path,const char * expect_body,...)34 static void test_request_succeeds(grpc_slice_split_mode split_mode,
35 const char* request_text,
36 const char* expect_method,
37 grpc_http_version expect_version,
38 const char* expect_path,
39 const char* expect_body, ...) {
40 grpc_http_parser parser;
41 grpc_slice input_slice = grpc_slice_from_copied_string(request_text);
42 size_t num_slices;
43 size_t i;
44 grpc_slice* slices;
45 va_list args;
46 grpc_http_request request;
47 memset(&request, 0, sizeof(request));
48
49 grpc_split_slices(split_mode, &input_slice, 1, &slices, &num_slices);
50 grpc_slice_unref(input_slice);
51
52 grpc_http_parser_init(&parser, GRPC_HTTP_REQUEST, &request);
53
54 for (i = 0; i < num_slices; i++) {
55 ASSERT_EQ(grpc_http_parser_parse(&parser, slices[i], nullptr),
56 absl::OkStatus());
57 grpc_slice_unref(slices[i]);
58 }
59 ASSERT_EQ(grpc_http_parser_eof(&parser), absl::OkStatus());
60
61 ASSERT_EQ(GRPC_HTTP_REQUEST, parser.type);
62 ASSERT_STREQ(expect_method, request.method);
63 ASSERT_STREQ(expect_path, request.path);
64 ASSERT_EQ(expect_version, request.version);
65
66 if (expect_body != nullptr) {
67 ASSERT_EQ(strlen(expect_body), request.body_length);
68 ASSERT_EQ(0, memcmp(expect_body, request.body, request.body_length));
69 } else {
70 ASSERT_EQ(request.body_length, 0);
71 }
72
73 va_start(args, expect_body);
74 i = 0;
75 for (;;) {
76 char* expect_key;
77 char* expect_value;
78 expect_key = va_arg(args, char*);
79 if (!expect_key) break;
80 ASSERT_LT(i, request.hdr_count);
81 expect_value = va_arg(args, char*);
82 ASSERT_TRUE(expect_value);
83 ASSERT_STREQ(expect_key, request.hdrs[i].key);
84 ASSERT_STREQ(expect_value, request.hdrs[i].value);
85 i++;
86 }
87 va_end(args);
88 ASSERT_EQ(i, request.hdr_count);
89
90 grpc_http_request_destroy(&request);
91 grpc_http_parser_destroy(&parser);
92 gpr_free(slices);
93 }
94
test_succeeds(grpc_slice_split_mode split_mode,const char * response_text,int expect_status,const char * expect_body,...)95 static void test_succeeds(grpc_slice_split_mode split_mode,
96 const char* response_text, int expect_status,
97 const char* expect_body, ...) {
98 grpc_http_parser parser;
99 grpc_slice input_slice = grpc_slice_from_copied_string(response_text);
100 size_t num_slices;
101 size_t i;
102 grpc_slice* slices;
103 va_list args;
104 grpc_http_response response;
105 response = {};
106
107 grpc_split_slices(split_mode, &input_slice, 1, &slices, &num_slices);
108 grpc_slice_unref(input_slice);
109
110 grpc_http_parser_init(&parser, GRPC_HTTP_RESPONSE, &response);
111
112 for (i = 0; i < num_slices; i++) {
113 ASSERT_EQ(grpc_http_parser_parse(&parser, slices[i], nullptr),
114 absl::OkStatus());
115 grpc_slice_unref(slices[i]);
116 }
117 ASSERT_EQ(grpc_http_parser_eof(&parser), absl::OkStatus());
118
119 ASSERT_EQ(GRPC_HTTP_RESPONSE, parser.type);
120 ASSERT_EQ(expect_status, response.status);
121 if (expect_body != nullptr) {
122 ASSERT_EQ(strlen(expect_body), response.body_length);
123 ASSERT_EQ(0, memcmp(expect_body, response.body, response.body_length));
124 } else {
125 ASSERT_EQ(response.body_length, 0);
126 }
127
128 va_start(args, expect_body);
129 i = 0;
130 for (;;) {
131 char* expect_key;
132 char* expect_value;
133 expect_key = va_arg(args, char*);
134 if (!expect_key) break;
135 ASSERT_LT(i, response.hdr_count);
136 expect_value = va_arg(args, char*);
137 ASSERT_TRUE(expect_value);
138 ASSERT_STREQ(expect_key, response.hdrs[i].key);
139 ASSERT_STREQ(expect_value, response.hdrs[i].value);
140 i++;
141 }
142 va_end(args);
143 ASSERT_EQ(i, response.hdr_count);
144
145 grpc_http_response_destroy(&response);
146 grpc_http_parser_destroy(&parser);
147 gpr_free(slices);
148 }
149
test_fails(grpc_slice_split_mode split_mode,const char * response_text)150 static void test_fails(grpc_slice_split_mode split_mode,
151 const char* response_text) {
152 grpc_http_parser parser;
153 grpc_slice input_slice = grpc_slice_from_copied_string(response_text);
154 size_t num_slices;
155 size_t i;
156 grpc_slice* slices;
157 grpc_error_handle error;
158 grpc_http_response response;
159 response = {};
160
161 grpc_split_slices(split_mode, &input_slice, 1, &slices, &num_slices);
162 grpc_slice_unref(input_slice);
163
164 grpc_http_parser_init(&parser, GRPC_HTTP_RESPONSE, &response);
165
166 for (i = 0; i < num_slices; i++) {
167 if (absl::OkStatus() == error) {
168 error = grpc_http_parser_parse(&parser, slices[i], nullptr);
169 }
170 grpc_slice_unref(slices[i]);
171 }
172 if (absl::OkStatus() == error) {
173 error = grpc_http_parser_eof(&parser);
174 }
175 ASSERT_FALSE(error.ok());
176
177 grpc_http_response_destroy(&response);
178 grpc_http_parser_destroy(&parser);
179 gpr_free(slices);
180 }
181
test_request_fails(grpc_slice_split_mode split_mode,const char * request_text)182 static void test_request_fails(grpc_slice_split_mode split_mode,
183 const char* request_text) {
184 grpc_http_parser parser;
185 grpc_slice input_slice = grpc_slice_from_copied_string(request_text);
186 size_t num_slices;
187 size_t i;
188 grpc_slice* slices;
189 grpc_error_handle error;
190 grpc_http_request request;
191 memset(&request, 0, sizeof(request));
192
193 grpc_split_slices(split_mode, &input_slice, 1, &slices, &num_slices);
194 grpc_slice_unref(input_slice);
195
196 grpc_http_parser_init(&parser, GRPC_HTTP_REQUEST, &request);
197
198 for (i = 0; i < num_slices; i++) {
199 if (error.ok()) {
200 error = grpc_http_parser_parse(&parser, slices[i], nullptr);
201 }
202 grpc_slice_unref(slices[i]);
203 }
204 if (error.ok()) {
205 error = grpc_http_parser_eof(&parser);
206 }
207 ASSERT_FALSE(error.ok());
208
209 grpc_http_request_destroy(&request);
210 grpc_http_parser_destroy(&parser);
211 gpr_free(slices);
212 }
213
TEST(ParserTest,MainTest)214 TEST(ParserTest, MainTest) {
215 size_t i;
216 const grpc_slice_split_mode split_modes[] = {GRPC_SLICE_SPLIT_IDENTITY,
217 GRPC_SLICE_SPLIT_ONE_BYTE};
218 for (i = 0; i < GPR_ARRAY_SIZE(split_modes); i++) {
219 test_succeeds(split_modes[i],
220 "HTTP/1.0 200 OK\r\n"
221 "xyz: abc\r\n"
222 "\r\n"
223 "hello world!",
224 200, "hello world!", "xyz", "abc", NULL);
225 test_succeeds(split_modes[i],
226 "HTTP/1.0 404 Not Found\r\n"
227 "\r\n",
228 404, nullptr, NULL);
229 test_succeeds(split_modes[i],
230 "HTTP/1.1 200 OK\r\n"
231 "xyz: abc\r\n"
232 "\r\n"
233 "hello world!",
234 200, "hello world!", "xyz", "abc", NULL);
235 test_succeeds(split_modes[i],
236 "HTTP/1.1 200 OK\n"
237 "\n"
238 "abc",
239 200, "abc", NULL);
240 test_succeeds(split_modes[i],
241 "HTTP/1.1 200 OK\r\n"
242 "Transfer-Encoding: chunked\r\n"
243 "\r\n"
244 "4\r\n"
245 "This\r\n"
246 "16;param1;param2\r\n"
247 " is a chunked encoding\r\n"
248 "1D\r\n"
249 " example.\r\nNo params handled.\r\n"
250 "0\r\n"
251 "\r\n",
252 200,
253 "This is a chunked encoding example.\r\nNo params handled.",
254 "Transfer-Encoding", "chunked", NULL);
255 test_succeeds(split_modes[i],
256 "HTTP/1.1 200 OK\r\n"
257 "Transfer-Encoding: chunked\r\n"
258 "\r\n"
259 "e\r\n"
260 "HTTP Trailers \r\n"
261 "13\r\n"
262 "are also supported.\r\n"
263 "0\r\n"
264 "abc: xyz\r\n"
265 "\r\n",
266 200, "HTTP Trailers are also supported.", "Transfer-Encoding",
267 "chunked", "abc", "xyz", NULL);
268 test_request_succeeds(split_modes[i],
269 "GET / HTTP/1.0\r\n"
270 "\r\n",
271 "GET", GRPC_HTTP_HTTP10, "/", nullptr, NULL);
272 test_request_succeeds(split_modes[i],
273 "GET / HTTP/1.0\r\n"
274 "\r\n"
275 "xyz",
276 "GET", GRPC_HTTP_HTTP10, "/", "xyz", NULL);
277 test_request_succeeds(split_modes[i],
278 "GET / HTTP/1.1\r\n"
279 "\r\n"
280 "xyz",
281 "GET", GRPC_HTTP_HTTP11, "/", "xyz", NULL);
282 test_request_succeeds(split_modes[i],
283 "GET / HTTP/2.0\r\n"
284 "\r\n"
285 "xyz",
286 "GET", GRPC_HTTP_HTTP20, "/", "xyz", NULL);
287 test_request_succeeds(split_modes[i],
288 "GET / HTTP/1.0\r\n"
289 "xyz: abc\r\n"
290 "\r\n"
291 "xyz",
292 "GET", GRPC_HTTP_HTTP10, "/", "xyz", "xyz", "abc",
293 NULL);
294 test_request_succeeds(split_modes[i],
295 "GET / HTTP/1.0\n"
296 "\n"
297 "xyz",
298 "GET", GRPC_HTTP_HTTP10, "/", "xyz", NULL);
299 test_fails(split_modes[i], "HTTP/1.0\r\n");
300 test_fails(split_modes[i], "HTTP/1.2\r\n");
301 test_fails(split_modes[i], "HTTP/1.0 000 XYX\r\n");
302 test_fails(split_modes[i], "HTTP/1.0 200 OK\n");
303 test_fails(split_modes[i], "HTTP/1.0 200 OK\r\n");
304 test_fails(split_modes[i], "HTTP/1.0 200 OK\r\nFoo x\r\n");
305 test_fails(split_modes[i],
306 "HTTP/1.0 200 OK\r\n"
307 "xyz: abc\r\n"
308 " def\r\n"
309 "\r\n"
310 "hello world!");
311 test_request_fails(split_modes[i], "GET\r\n");
312 test_request_fails(split_modes[i], "GET /\r\n");
313 test_request_fails(split_modes[i], "GET / HTTP/0.0\r\n");
314 test_request_fails(split_modes[i], "GET / ____/1.0\r\n");
315 test_request_fails(split_modes[i], "GET / HTTP/1.2\r\n");
316 test_request_fails(split_modes[i], "GET / HTTP/1.0\n");
317
318 char* tmp1 =
319 static_cast<char*>(gpr_malloc(2 * GRPC_HTTP_PARSER_MAX_HEADER_LENGTH));
320 memset(tmp1, 'a', (2 * GRPC_HTTP_PARSER_MAX_HEADER_LENGTH) - 1);
321 tmp1[(2 * GRPC_HTTP_PARSER_MAX_HEADER_LENGTH) - 1] = 0;
322 std::string tmp2 =
323 absl::StrFormat("HTTP/1.0 200 OK\r\nxyz: %s\r\n\r\n", tmp1);
324 gpr_free(tmp1);
325 test_fails(split_modes[i], tmp2.c_str());
326 }
327 }
328
main(int argc,char ** argv)329 int main(int argc, char** argv) {
330 grpc::testing::TestEnvironment env(&argc, argv);
331 ::testing::InitGoogleTest(&argc, argv);
332 grpc::testing::TestGrpcScope grpc_scope;
333 return RUN_ALL_TESTS();
334 }
335