unit2603.c 5.1 KB

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