parser_test.cc 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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. #include "src/core/lib/http/parser.h"
  19. #include <stdarg.h>
  20. #include <string.h>
  21. #include <string>
  22. #include "absl/strings/str_format.h"
  23. #include <grpc/grpc.h>
  24. #include <grpc/support/alloc.h>
  25. #include <grpc/support/log.h>
  26. #include "src/core/lib/gpr/useful.h"
  27. #include "test/core/util/slice_splitter.h"
  28. #include "test/core/util/test_config.h"
  29. static void test_request_succeeds(grpc_slice_split_mode split_mode,
  30. const char* request_text,
  31. const char* expect_method,
  32. grpc_http_version expect_version,
  33. const char* expect_path,
  34. const char* expect_body, ...) {
  35. grpc_http_parser parser;
  36. grpc_slice input_slice = grpc_slice_from_copied_string(request_text);
  37. size_t num_slices;
  38. size_t i;
  39. grpc_slice* slices;
  40. va_list args;
  41. grpc_http_request request;
  42. memset(&request, 0, sizeof(request));
  43. grpc_split_slices(split_mode, &input_slice, 1, &slices, &num_slices);
  44. grpc_slice_unref(input_slice);
  45. grpc_http_parser_init(&parser, GRPC_HTTP_REQUEST, &request);
  46. for (i = 0; i < num_slices; i++) {
  47. GPR_ASSERT(grpc_http_parser_parse(&parser, slices[i], nullptr) ==
  48. GRPC_ERROR_NONE);
  49. grpc_slice_unref(slices[i]);
  50. }
  51. GPR_ASSERT(grpc_http_parser_eof(&parser) == GRPC_ERROR_NONE);
  52. GPR_ASSERT(GRPC_HTTP_REQUEST == parser.type);
  53. GPR_ASSERT(0 == strcmp(expect_method, request.method));
  54. GPR_ASSERT(0 == strcmp(expect_path, request.path));
  55. GPR_ASSERT(expect_version == request.version);
  56. if (expect_body != nullptr) {
  57. GPR_ASSERT(strlen(expect_body) == request.body_length);
  58. GPR_ASSERT(0 == memcmp(expect_body, request.body, request.body_length));
  59. } else {
  60. GPR_ASSERT(request.body_length == 0);
  61. }
  62. va_start(args, expect_body);
  63. i = 0;
  64. for (;;) {
  65. char* expect_key;
  66. char* expect_value;
  67. expect_key = va_arg(args, char*);
  68. if (!expect_key) break;
  69. GPR_ASSERT(i < request.hdr_count);
  70. expect_value = va_arg(args, char*);
  71. GPR_ASSERT(expect_value);
  72. GPR_ASSERT(0 == strcmp(expect_key, request.hdrs[i].key));
  73. GPR_ASSERT(0 == strcmp(expect_value, request.hdrs[i].value));
  74. i++;
  75. }
  76. va_end(args);
  77. GPR_ASSERT(i == request.hdr_count);
  78. grpc_http_request_destroy(&request);
  79. grpc_http_parser_destroy(&parser);
  80. gpr_free(slices);
  81. }
  82. static void test_succeeds(grpc_slice_split_mode split_mode,
  83. const char* response_text, int expect_status,
  84. const char* expect_body, ...) {
  85. grpc_http_parser parser;
  86. grpc_slice input_slice = grpc_slice_from_copied_string(response_text);
  87. size_t num_slices;
  88. size_t i;
  89. grpc_slice* slices;
  90. va_list args;
  91. grpc_http_response response;
  92. response = {};
  93. grpc_split_slices(split_mode, &input_slice, 1, &slices, &num_slices);
  94. grpc_slice_unref(input_slice);
  95. grpc_http_parser_init(&parser, GRPC_HTTP_RESPONSE, &response);
  96. for (i = 0; i < num_slices; i++) {
  97. GPR_ASSERT(grpc_http_parser_parse(&parser, slices[i], nullptr) ==
  98. GRPC_ERROR_NONE);
  99. grpc_slice_unref(slices[i]);
  100. }
  101. GPR_ASSERT(grpc_http_parser_eof(&parser) == GRPC_ERROR_NONE);
  102. GPR_ASSERT(GRPC_HTTP_RESPONSE == parser.type);
  103. GPR_ASSERT(expect_status == response.status);
  104. if (expect_body != nullptr) {
  105. GPR_ASSERT(strlen(expect_body) == response.body_length);
  106. GPR_ASSERT(0 == memcmp(expect_body, response.body, response.body_length));
  107. } else {
  108. GPR_ASSERT(response.body_length == 0);
  109. }
  110. va_start(args, expect_body);
  111. i = 0;
  112. for (;;) {
  113. char* expect_key;
  114. char* expect_value;
  115. expect_key = va_arg(args, char*);
  116. if (!expect_key) break;
  117. GPR_ASSERT(i < response.hdr_count);
  118. expect_value = va_arg(args, char*);
  119. GPR_ASSERT(expect_value);
  120. GPR_ASSERT(0 == strcmp(expect_key, response.hdrs[i].key));
  121. GPR_ASSERT(0 == strcmp(expect_value, response.hdrs[i].value));
  122. i++;
  123. }
  124. va_end(args);
  125. GPR_ASSERT(i == response.hdr_count);
  126. grpc_http_response_destroy(&response);
  127. grpc_http_parser_destroy(&parser);
  128. gpr_free(slices);
  129. }
  130. static void test_fails(grpc_slice_split_mode split_mode,
  131. const char* response_text) {
  132. grpc_http_parser parser;
  133. grpc_slice input_slice = grpc_slice_from_copied_string(response_text);
  134. size_t num_slices;
  135. size_t i;
  136. grpc_slice* slices;
  137. grpc_error_handle error = GRPC_ERROR_NONE;
  138. grpc_http_response response;
  139. response = {};
  140. grpc_split_slices(split_mode, &input_slice, 1, &slices, &num_slices);
  141. grpc_slice_unref(input_slice);
  142. grpc_http_parser_init(&parser, GRPC_HTTP_RESPONSE, &response);
  143. for (i = 0; i < num_slices; i++) {
  144. if (GRPC_ERROR_NONE == error) {
  145. error = grpc_http_parser_parse(&parser, slices[i], nullptr);
  146. }
  147. grpc_slice_unref(slices[i]);
  148. }
  149. if (GRPC_ERROR_NONE == error) {
  150. error = grpc_http_parser_eof(&parser);
  151. }
  152. GPR_ASSERT(error != GRPC_ERROR_NONE);
  153. GRPC_ERROR_UNREF(error);
  154. grpc_http_response_destroy(&response);
  155. grpc_http_parser_destroy(&parser);
  156. gpr_free(slices);
  157. }
  158. static void test_request_fails(grpc_slice_split_mode split_mode,
  159. const char* request_text) {
  160. grpc_http_parser parser;
  161. grpc_slice input_slice = grpc_slice_from_copied_string(request_text);
  162. size_t num_slices;
  163. size_t i;
  164. grpc_slice* slices;
  165. grpc_error_handle error = GRPC_ERROR_NONE;
  166. grpc_http_request request;
  167. memset(&request, 0, sizeof(request));
  168. grpc_split_slices(split_mode, &input_slice, 1, &slices, &num_slices);
  169. grpc_slice_unref(input_slice);
  170. grpc_http_parser_init(&parser, GRPC_HTTP_REQUEST, &request);
  171. for (i = 0; i < num_slices; i++) {
  172. if (error == GRPC_ERROR_NONE) {
  173. error = grpc_http_parser_parse(&parser, slices[i], nullptr);
  174. }
  175. grpc_slice_unref(slices[i]);
  176. }
  177. if (error == GRPC_ERROR_NONE) {
  178. error = grpc_http_parser_eof(&parser);
  179. }
  180. GPR_ASSERT(error != GRPC_ERROR_NONE);
  181. GRPC_ERROR_UNREF(error);
  182. grpc_http_request_destroy(&request);
  183. grpc_http_parser_destroy(&parser);
  184. gpr_free(slices);
  185. }
  186. int main(int argc, char** argv) {
  187. size_t i;
  188. const grpc_slice_split_mode split_modes[] = {GRPC_SLICE_SPLIT_IDENTITY,
  189. GRPC_SLICE_SPLIT_ONE_BYTE};
  190. grpc::testing::TestEnvironment env(argc, argv);
  191. grpc_init();
  192. for (i = 0; i < GPR_ARRAY_SIZE(split_modes); i++) {
  193. test_succeeds(split_modes[i],
  194. "HTTP/1.0 200 OK\r\n"
  195. "xyz: abc\r\n"
  196. "\r\n"
  197. "hello world!",
  198. 200, "hello world!", "xyz", "abc", NULL);
  199. test_succeeds(split_modes[i],
  200. "HTTP/1.0 404 Not Found\r\n"
  201. "\r\n",
  202. 404, nullptr, NULL);
  203. test_succeeds(split_modes[i],
  204. "HTTP/1.1 200 OK\r\n"
  205. "xyz: abc\r\n"
  206. "\r\n"
  207. "hello world!",
  208. 200, "hello world!", "xyz", "abc", NULL);
  209. test_succeeds(split_modes[i],
  210. "HTTP/1.1 200 OK\n"
  211. "\n"
  212. "abc",
  213. 200, "abc", NULL);
  214. test_request_succeeds(split_modes[i],
  215. "GET / HTTP/1.0\r\n"
  216. "\r\n",
  217. "GET", GRPC_HTTP_HTTP10, "/", nullptr, NULL);
  218. test_request_succeeds(split_modes[i],
  219. "GET / HTTP/1.0\r\n"
  220. "\r\n"
  221. "xyz",
  222. "GET", GRPC_HTTP_HTTP10, "/", "xyz", NULL);
  223. test_request_succeeds(split_modes[i],
  224. "GET / HTTP/1.1\r\n"
  225. "\r\n"
  226. "xyz",
  227. "GET", GRPC_HTTP_HTTP11, "/", "xyz", NULL);
  228. test_request_succeeds(split_modes[i],
  229. "GET / HTTP/2.0\r\n"
  230. "\r\n"
  231. "xyz",
  232. "GET", GRPC_HTTP_HTTP20, "/", "xyz", NULL);
  233. test_request_succeeds(split_modes[i],
  234. "GET / HTTP/1.0\r\n"
  235. "xyz: abc\r\n"
  236. "\r\n"
  237. "xyz",
  238. "GET", GRPC_HTTP_HTTP10, "/", "xyz", "xyz", "abc",
  239. NULL);
  240. test_request_succeeds(split_modes[i],
  241. "GET / HTTP/1.0\n"
  242. "\n"
  243. "xyz",
  244. "GET", GRPC_HTTP_HTTP10, "/", "xyz", NULL);
  245. test_fails(split_modes[i], "HTTP/1.0\r\n");
  246. test_fails(split_modes[i], "HTTP/1.2\r\n");
  247. test_fails(split_modes[i], "HTTP/1.0 000 XYX\r\n");
  248. test_fails(split_modes[i], "HTTP/1.0 200 OK\n");
  249. test_fails(split_modes[i], "HTTP/1.0 200 OK\r\n");
  250. test_fails(split_modes[i], "HTTP/1.0 200 OK\r\nFoo x\r\n");
  251. test_fails(split_modes[i],
  252. "HTTP/1.0 200 OK\r\n"
  253. "xyz: abc\r\n"
  254. " def\r\n"
  255. "\r\n"
  256. "hello world!");
  257. test_request_fails(split_modes[i], "GET\r\n");
  258. test_request_fails(split_modes[i], "GET /\r\n");
  259. test_request_fails(split_modes[i], "GET / HTTP/0.0\r\n");
  260. test_request_fails(split_modes[i], "GET / ____/1.0\r\n");
  261. test_request_fails(split_modes[i], "GET / HTTP/1.2\r\n");
  262. test_request_fails(split_modes[i], "GET / HTTP/1.0\n");
  263. char* tmp1 =
  264. static_cast<char*>(gpr_malloc(2 * GRPC_HTTP_PARSER_MAX_HEADER_LENGTH));
  265. memset(tmp1, 'a', 2 * GRPC_HTTP_PARSER_MAX_HEADER_LENGTH - 1);
  266. tmp1[2 * GRPC_HTTP_PARSER_MAX_HEADER_LENGTH - 1] = 0;
  267. std::string tmp2 =
  268. absl::StrFormat("HTTP/1.0 200 OK\r\nxyz: %s\r\n\r\n", tmp1);
  269. gpr_free(tmp1);
  270. test_fails(split_modes[i], tmp2.c_str());
  271. }
  272. grpc_shutdown();
  273. return 0;
  274. }