http1.c 9.3 KB

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