http1.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) Daniel Stenberg, <[email protected]>, 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 "curl_setup.h"
  25. #ifndef CURL_DISABLE_HTTP
  26. #include "urldata.h"
  27. #include "http.h"
  28. #include "http1.h"
  29. #include "urlapi-int.h"
  30. #define H1_MAX_URL_LEN (8 * 1024)
  31. void Curl_h1_req_parse_init(struct h1_req_parser *parser, size_t max_line_len)
  32. {
  33. memset(parser, 0, sizeof(*parser));
  34. parser->max_line_len = max_line_len;
  35. curlx_dyn_init(&parser->scratch, max_line_len);
  36. }
  37. void Curl_h1_req_parse_free(struct h1_req_parser *parser)
  38. {
  39. if(parser) {
  40. Curl_http_req_free(parser->req);
  41. curlx_dyn_free(&parser->scratch);
  42. parser->req = NULL;
  43. parser->done = FALSE;
  44. }
  45. }
  46. static CURLcode trim_line(struct h1_req_parser *parser, int options)
  47. {
  48. DEBUGASSERT(parser->line);
  49. if(parser->line_len) {
  50. if(parser->line[parser->line_len - 1] == '\n')
  51. --parser->line_len;
  52. if(parser->line_len) {
  53. if(parser->line[parser->line_len - 1] == '\r')
  54. --parser->line_len;
  55. else if(options & H1_PARSE_OPT_STRICT)
  56. return CURLE_URL_MALFORMAT;
  57. }
  58. else if(options & H1_PARSE_OPT_STRICT)
  59. return CURLE_URL_MALFORMAT;
  60. }
  61. else if(options & H1_PARSE_OPT_STRICT)
  62. return CURLE_URL_MALFORMAT;
  63. if(parser->line_len > parser->max_line_len) {
  64. return CURLE_URL_MALFORMAT;
  65. }
  66. return CURLE_OK;
  67. }
  68. static CURLcode detect_line(struct h1_req_parser *parser,
  69. const uint8_t *buf, const size_t buflen,
  70. size_t *pnread)
  71. {
  72. const char *line_end;
  73. DEBUGASSERT(!parser->line);
  74. *pnread = 0;
  75. line_end = memchr(buf, '\n', buflen);
  76. if(!line_end)
  77. return CURLE_AGAIN;
  78. parser->line = (const char *)buf;
  79. parser->line_len = line_end - parser->line + 1;
  80. *pnread = parser->line_len;
  81. return CURLE_OK;
  82. }
  83. static CURLcode next_line(struct h1_req_parser *parser,
  84. const uint8_t *buf, const size_t buflen, int options,
  85. size_t *pnread)
  86. {
  87. CURLcode result;
  88. *pnread = 0;
  89. if(parser->line) {
  90. parser->line = NULL;
  91. parser->line_len = 0;
  92. curlx_dyn_reset(&parser->scratch);
  93. }
  94. result = detect_line(parser, buf, buflen, pnread);
  95. if(!result) {
  96. if(curlx_dyn_len(&parser->scratch)) {
  97. /* append detected line to scratch to have the complete line */
  98. result = curlx_dyn_addn(&parser->scratch, parser->line,
  99. parser->line_len);
  100. if(result)
  101. return result;
  102. parser->line = curlx_dyn_ptr(&parser->scratch);
  103. parser->line_len = curlx_dyn_len(&parser->scratch);
  104. }
  105. result = trim_line(parser, options);
  106. if(result)
  107. return result;
  108. }
  109. else if(result == CURLE_AGAIN) {
  110. /* no line end in `buf`, add it to our scratch */
  111. result = curlx_dyn_addn(&parser->scratch, (const unsigned char *)buf,
  112. buflen);
  113. *pnread = buflen;
  114. }
  115. return result;
  116. }
  117. static CURLcode start_req(struct h1_req_parser *parser,
  118. const char *scheme_default,
  119. const char *custom_method,
  120. int options)
  121. {
  122. const char *p, *m, *target, *hv, *scheme, *authority, *path;
  123. size_t m_len, target_len, hv_len, scheme_len, authority_len, path_len;
  124. size_t i;
  125. CURLU *url = NULL;
  126. CURLcode result = CURLE_URL_MALFORMAT; /* Use this as default fail */
  127. DEBUGASSERT(!parser->req);
  128. /* line must match: "METHOD TARGET HTTP_VERSION" */
  129. if(custom_method && custom_method[0] &&
  130. !strncmp(custom_method, parser->line, strlen(custom_method))) {
  131. p = parser->line + strlen(custom_method);
  132. }
  133. else {
  134. p = memchr(parser->line, ' ', parser->line_len);
  135. if(!p || p == parser->line)
  136. goto out;
  137. }
  138. m = parser->line;
  139. m_len = p - parser->line;
  140. target = p + 1;
  141. target_len = hv_len = 0;
  142. hv = NULL;
  143. /* URL may contain spaces so scan backwards */
  144. for(i = parser->line_len; i > m_len; --i) {
  145. if(parser->line[i] == ' ') {
  146. hv = &parser->line[i + 1];
  147. hv_len = parser->line_len - i;
  148. target_len = (hv - target) - 1;
  149. break;
  150. }
  151. }
  152. /* no SPACE found or empty TARGET or empty HTTP_VERSION */
  153. if(!target_len || !hv_len)
  154. goto out;
  155. (void)hv;
  156. /* The TARGET can be (rfc 9112, ch. 3.2):
  157. * origin-form: path + optional query
  158. * absolute-form: absolute URI
  159. * authority-form: host+port for CONNECT
  160. * asterisk-form: '*' for OPTIONS
  161. *
  162. * from TARGET, we derive `scheme` `authority` `path`
  163. * origin-form -- -- TARGET
  164. * absolute-form URL* URL* URL*
  165. * authority-form -- TARGET --
  166. * asterisk-form -- -- TARGET
  167. */
  168. scheme = authority = path = NULL;
  169. scheme_len = authority_len = path_len = 0;
  170. if(target_len == 1 && target[0] == '*') {
  171. /* asterisk-form */
  172. path = target;
  173. path_len = target_len;
  174. }
  175. else if(!strncmp("CONNECT", m, m_len)) {
  176. /* authority-form */
  177. authority = target;
  178. authority_len = target_len;
  179. }
  180. else if(target[0] == '/') {
  181. /* origin-form */
  182. path = target;
  183. path_len = target_len;
  184. }
  185. else {
  186. /* origin-form OR absolute-form */
  187. CURLUcode uc;
  188. char tmp[H1_MAX_URL_LEN];
  189. /* default, unless we see an absolute URL */
  190. path = target;
  191. path_len = target_len;
  192. /* URL parser wants null-termination */
  193. if(target_len >= sizeof(tmp))
  194. goto out;
  195. memcpy(tmp, target, target_len);
  196. tmp[target_len] = '\0';
  197. /* See if treating TARGET as an absolute URL makes sense */
  198. if(Curl_is_absolute_url(tmp, NULL, 0, FALSE)) {
  199. unsigned int url_options;
  200. url = curl_url();
  201. if(!url) {
  202. result = CURLE_OUT_OF_MEMORY;
  203. goto out;
  204. }
  205. url_options = (CURLU_NON_SUPPORT_SCHEME |
  206. CURLU_PATH_AS_IS |
  207. CURLU_NO_DEFAULT_PORT);
  208. if(!(options & H1_PARSE_OPT_STRICT))
  209. url_options |= CURLU_ALLOW_SPACE;
  210. uc = curl_url_set(url, CURLUPART_URL, tmp, url_options);
  211. if(uc) {
  212. goto out;
  213. }
  214. }
  215. if(!url && (options & H1_PARSE_OPT_STRICT)) {
  216. /* we should have an absolute URL or have seen `/` earlier */
  217. goto out;
  218. }
  219. }
  220. if(url) {
  221. result = Curl_http_req_make2(&parser->req, m, m_len, url, scheme_default);
  222. }
  223. else {
  224. if(!scheme && scheme_default) {
  225. scheme = scheme_default;
  226. scheme_len = strlen(scheme_default);
  227. }
  228. result = Curl_http_req_make(&parser->req, m, m_len, scheme, scheme_len,
  229. authority, authority_len, path, path_len);
  230. }
  231. out:
  232. curl_url_cleanup(url);
  233. return result;
  234. }
  235. CURLcode Curl_h1_req_parse_read(struct h1_req_parser *parser,
  236. const uint8_t *buf, size_t buflen,
  237. const char *scheme_default,
  238. const char *custom_method,
  239. int options, size_t *pnread)
  240. {
  241. CURLcode result = CURLE_OK;
  242. size_t nread;
  243. *pnread = 0;
  244. while(!parser->done) {
  245. result = next_line(parser, buf, buflen, options, &nread);
  246. if(result) {
  247. if(result == CURLE_AGAIN)
  248. result = CURLE_OK;
  249. goto out;
  250. }
  251. /* Consume this line */
  252. *pnread += nread;
  253. buf += nread;
  254. buflen -= nread;
  255. if(!parser->line) {
  256. /* consumed bytes, but line not complete */
  257. if(!buflen)
  258. goto out;
  259. }
  260. else if(!parser->req) {
  261. result = start_req(parser, scheme_default, custom_method, options);
  262. if(result)
  263. goto out;
  264. }
  265. else if(parser->line_len == 0) {
  266. /* last, empty line, we are finished */
  267. if(!parser->req) {
  268. result = CURLE_URL_MALFORMAT;
  269. goto out;
  270. }
  271. parser->done = TRUE;
  272. curlx_dyn_reset(&parser->scratch);
  273. /* last chance adjustments */
  274. }
  275. else {
  276. result = Curl_dynhds_h1_add_line(&parser->req->headers,
  277. parser->line, parser->line_len);
  278. if(result)
  279. goto out;
  280. }
  281. }
  282. out:
  283. return result;
  284. }
  285. CURLcode Curl_h1_req_write_head(struct httpreq *req, int http_minor,
  286. struct dynbuf *dbuf)
  287. {
  288. CURLcode result;
  289. result = curlx_dyn_addf(dbuf, "%s %s%s%s%s HTTP/1.%d\r\n",
  290. req->method,
  291. req->scheme ? req->scheme : "",
  292. req->scheme ? "://" : "",
  293. req->authority ? req->authority : "",
  294. req->path ? req->path : "",
  295. http_minor);
  296. if(result)
  297. goto out;
  298. result = Curl_dynhds_h1_dprint(&req->headers, dbuf);
  299. if(result)
  300. goto out;
  301. result = curlx_dyn_addn(dbuf, STRCONST("\r\n"));
  302. out:
  303. return result;
  304. }
  305. #endif /* !CURL_DISABLE_HTTP */