unit2603.c 5.1 KB

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