2
0

http1.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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 "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 3 #include files should be in this order */
  32. #include "curl_printf.h"
  33. #include "curl_memory.h"
  34. #include "memdebug.h"
  35. #define H1_MAX_URL_LEN (8*1024)
  36. void Curl_h1_req_parse_init(struct h1_req_parser *parser, size_t max_line_len)
  37. {
  38. memset(parser, 0, sizeof(*parser));
  39. parser->max_line_len = max_line_len;
  40. Curl_dyn_init(&parser->scratch, max_line_len);
  41. }
  42. void Curl_h1_req_parse_free(struct h1_req_parser *parser)
  43. {
  44. if(parser) {
  45. Curl_http_req_free(parser->req);
  46. Curl_dyn_free(&parser->scratch);
  47. parser->req = NULL;
  48. parser->done = FALSE;
  49. }
  50. }
  51. static CURLcode trim_line(struct h1_req_parser *parser, int options)
  52. {
  53. DEBUGASSERT(parser->line);
  54. if(parser->line_len) {
  55. if(parser->line[parser->line_len - 1] == '\n')
  56. --parser->line_len;
  57. if(parser->line_len) {
  58. if(parser->line[parser->line_len - 1] == '\r')
  59. --parser->line_len;
  60. else if(options & H1_PARSE_OPT_STRICT)
  61. return CURLE_URL_MALFORMAT;
  62. }
  63. else if(options & H1_PARSE_OPT_STRICT)
  64. return CURLE_URL_MALFORMAT;
  65. }
  66. else if(options & H1_PARSE_OPT_STRICT)
  67. return CURLE_URL_MALFORMAT;
  68. if(parser->line_len > parser->max_line_len) {
  69. return CURLE_URL_MALFORMAT;
  70. }
  71. return CURLE_OK;
  72. }
  73. static ssize_t detect_line(struct h1_req_parser *parser,
  74. const char *buf, const size_t buflen,
  75. CURLcode *err)
  76. {
  77. const char *line_end;
  78. DEBUGASSERT(!parser->line);
  79. line_end = memchr(buf, '\n', buflen);
  80. if(!line_end) {
  81. *err = CURLE_AGAIN;
  82. return -1;
  83. }
  84. parser->line = buf;
  85. parser->line_len = line_end - buf + 1;
  86. *err = CURLE_OK;
  87. return (ssize_t)parser->line_len;
  88. }
  89. static ssize_t next_line(struct h1_req_parser *parser,
  90. const char *buf, const size_t buflen, int options,
  91. CURLcode *err)
  92. {
  93. ssize_t nread = 0;
  94. if(parser->line) {
  95. parser->line = NULL;
  96. parser->line_len = 0;
  97. Curl_dyn_reset(&parser->scratch);
  98. }
  99. nread = detect_line(parser, buf, buflen, err);
  100. if(nread >= 0) {
  101. if(Curl_dyn_len(&parser->scratch)) {
  102. /* append detected line to scratch to have the complete line */
  103. *err = Curl_dyn_addn(&parser->scratch, parser->line, parser->line_len);
  104. if(*err)
  105. return -1;
  106. parser->line = Curl_dyn_ptr(&parser->scratch);
  107. parser->line_len = Curl_dyn_len(&parser->scratch);
  108. }
  109. *err = trim_line(parser, options);
  110. if(*err)
  111. return -1;
  112. }
  113. else if(*err == CURLE_AGAIN) {
  114. /* no line end in `buf`, add it to our scratch */
  115. *err = Curl_dyn_addn(&parser->scratch, (const unsigned char *)buf, 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. /* TODO: we do not check HTTP_VERSION for conformity, should
  151. + do that when STRICT option is supplied. */
  152. (void)hv;
  153. /* The TARGET can be (rfc 9112, ch. 3.2):
  154. * origin-form: path + optional query
  155. * absolute-form: absolute URI
  156. * authority-form: host+port for CONNECT
  157. * asterisk-form: '*' for OPTIONS
  158. *
  159. * from TARGET, we derive `scheme` `authority` `path`
  160. * origin-form -- -- TARGET
  161. * absolute-form URL* URL* URL*
  162. * authority-form -- TARGET --
  163. * asterisk-form -- -- TARGET
  164. */
  165. scheme = authority = path = NULL;
  166. scheme_len = authority_len = path_len = 0;
  167. if(target_len == 1 && target[0] == '*') {
  168. /* asterisk-form */
  169. path = target;
  170. path_len = target_len;
  171. }
  172. else if(!strncmp("CONNECT", m, m_len)) {
  173. /* authority-form */
  174. authority = target;
  175. authority_len = target_len;
  176. }
  177. else if(target[0] == '/') {
  178. /* origin-form */
  179. path = target;
  180. path_len = target_len;
  181. }
  182. else {
  183. /* origin-form OR absolute-form */
  184. CURLUcode uc;
  185. char tmp[H1_MAX_URL_LEN];
  186. /* default, unless we see an absolute URL */
  187. path = target;
  188. path_len = target_len;
  189. /* URL parser wants 0-termination */
  190. if(target_len >= sizeof(tmp))
  191. goto out;
  192. memcpy(tmp, target, target_len);
  193. tmp[target_len] = '\0';
  194. /* See if treating TARGET as an absolute URL makes sense */
  195. if(Curl_is_absolute_url(tmp, NULL, 0, FALSE)) {
  196. unsigned int url_options;
  197. url = curl_url();
  198. if(!url) {
  199. result = CURLE_OUT_OF_MEMORY;
  200. goto out;
  201. }
  202. url_options = (CURLU_NON_SUPPORT_SCHEME|
  203. CURLU_PATH_AS_IS|
  204. CURLU_NO_DEFAULT_PORT);
  205. if(!(options & H1_PARSE_OPT_STRICT))
  206. url_options |= CURLU_ALLOW_SPACE;
  207. uc = curl_url_set(url, CURLUPART_URL, tmp, url_options);
  208. if(uc) {
  209. goto out;
  210. }
  211. }
  212. if(!url && (options & H1_PARSE_OPT_STRICT)) {
  213. /* we should have an absolute URL or have seen `/` earlier */
  214. goto out;
  215. }
  216. }
  217. if(url) {
  218. result = Curl_http_req_make2(&parser->req, m, m_len, url, scheme_default);
  219. }
  220. else {
  221. if(!scheme && scheme_default) {
  222. scheme = scheme_default;
  223. scheme_len = strlen(scheme_default);
  224. }
  225. result = Curl_http_req_make(&parser->req, m, m_len, scheme, scheme_len,
  226. authority, authority_len, path, path_len);
  227. }
  228. out:
  229. curl_url_cleanup(url);
  230. return result;
  231. }
  232. ssize_t Curl_h1_req_parse_read(struct h1_req_parser *parser,
  233. const char *buf, size_t buflen,
  234. const char *scheme_default, int options,
  235. CURLcode *err)
  236. {
  237. ssize_t nread = 0, n;
  238. *err = CURLE_OK;
  239. while(!parser->done) {
  240. n = next_line(parser, buf, buflen, options, err);
  241. if(n < 0) {
  242. if(*err != CURLE_AGAIN) {
  243. nread = -1;
  244. }
  245. *err = CURLE_OK;
  246. goto out;
  247. }
  248. /* Consume this line */
  249. nread += (size_t)n;
  250. buf += (size_t)n;
  251. buflen -= (size_t)n;
  252. if(!parser->line) {
  253. /* consumed bytes, but line not complete */
  254. if(!buflen)
  255. goto out;
  256. }
  257. else if(!parser->req) {
  258. *err = start_req(parser, scheme_default, options);
  259. if(*err) {
  260. nread = -1;
  261. goto out;
  262. }
  263. }
  264. else if(parser->line_len == 0) {
  265. /* last, empty line, we are finished */
  266. if(!parser->req) {
  267. *err = CURLE_URL_MALFORMAT;
  268. nread = -1;
  269. goto out;
  270. }
  271. parser->done = TRUE;
  272. Curl_dyn_reset(&parser->scratch);
  273. /* last chance adjustments */
  274. }
  275. else {
  276. *err = Curl_dynhds_h1_add_line(&parser->req->headers,
  277. parser->line, parser->line_len);
  278. if(*err) {
  279. nread = -1;
  280. goto out;
  281. }
  282. }
  283. }
  284. out:
  285. return nread;
  286. }
  287. CURLcode Curl_h1_req_write_head(struct httpreq *req, int http_minor,
  288. struct dynbuf *dbuf)
  289. {
  290. CURLcode result;
  291. result = Curl_dyn_addf(dbuf, "%s %s%s%s%s HTTP/1.%d\r\n",
  292. req->method,
  293. req->scheme? req->scheme : "",
  294. req->scheme? "://" : "",
  295. req->authority? req->authority : "",
  296. req->path? req->path : "",
  297. http_minor);
  298. if(result)
  299. goto out;
  300. result = Curl_dynhds_h1_dprint(&req->headers, dbuf);
  301. if(result)
  302. goto out;
  303. result = Curl_dyn_addn(dbuf, STRCONST("\r\n"));
  304. out:
  305. return result;
  306. }
  307. #endif /* !CURL_DISABLE_HTTP */