http_parser.c 60 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175
  1. /* Based on src/http/ngx_http_parse.c from NGINX copyright Igor Sysoev
  2. *
  3. * Additional changes are licensed under the same terms as NGINX and
  4. * copyright Joyent, Inc. and other Node contributors. All rights reserved.
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to
  8. * deal in the Software without restriction, including without limitation the
  9. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  10. * sell copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  21. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  22. * IN THE SOFTWARE.
  23. */
  24. #include "http_parser.h"
  25. #include <assert.h>
  26. #include <stddef.h>
  27. #include <ctype.h>
  28. #include <stdlib.h>
  29. #include <string.h>
  30. #include <limits.h>
  31. #ifndef ULLONG_MAX
  32. # define ULLONG_MAX ((uint64_t) -1) /* 2^64-1 */
  33. #endif
  34. #ifndef MIN
  35. # define MIN(a,b) ((a) < (b) ? (a) : (b))
  36. #endif
  37. #ifndef ARRAY_SIZE
  38. # define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
  39. #endif
  40. #ifndef BIT_AT
  41. # define BIT_AT(a, i) \
  42. (!!((unsigned int) (a)[(unsigned int) (i) >> 3] & \
  43. (1 << ((unsigned int) (i) & 7))))
  44. #endif
  45. #ifndef ELEM_AT
  46. # define ELEM_AT(a, i, v) ((unsigned int) (i) < ARRAY_SIZE(a) ? (a)[(i)] : (v))
  47. #endif
  48. #define SET_ERRNO(e) \
  49. do { \
  50. parser->http_errno = (e); \
  51. } while(0)
  52. /* Run the notify callback FOR, returning ER if it fails */
  53. #define CALLBACK_NOTIFY_(FOR, ER) \
  54. do { \
  55. assert(HTTP_PARSER_ERRNO(parser) == HPE_OK); \
  56. \
  57. if (settings->on_##FOR) { \
  58. if (0 != settings->on_##FOR(parser)) { \
  59. SET_ERRNO(HPE_CB_##FOR); \
  60. } \
  61. \
  62. /* We either errored above or got paused; get out */ \
  63. if (HTTP_PARSER_ERRNO(parser) != HPE_OK) { \
  64. return (ER); \
  65. } \
  66. } \
  67. } while (0)
  68. /* Run the notify callback FOR and consume the current byte */
  69. #define CALLBACK_NOTIFY(FOR) CALLBACK_NOTIFY_(FOR, p - data + 1)
  70. /* Run the notify callback FOR and don't consume the current byte */
  71. #define CALLBACK_NOTIFY_NOADVANCE(FOR) CALLBACK_NOTIFY_(FOR, p - data)
  72. /* Run data callback FOR with LEN bytes, returning ER if it fails */
  73. #define CALLBACK_DATA_(FOR, LEN, ER) \
  74. do { \
  75. assert(HTTP_PARSER_ERRNO(parser) == HPE_OK); \
  76. \
  77. if (FOR##_mark) { \
  78. if (settings->on_##FOR) { \
  79. if (0 != settings->on_##FOR(parser, FOR##_mark, (LEN))) { \
  80. SET_ERRNO(HPE_CB_##FOR); \
  81. } \
  82. \
  83. /* We either errored above or got paused; get out */ \
  84. if (HTTP_PARSER_ERRNO(parser) != HPE_OK) { \
  85. return (ER); \
  86. } \
  87. } \
  88. FOR##_mark = NULL; \
  89. } \
  90. } while (0)
  91. /* Run the data callback FOR and consume the current byte */
  92. #define CALLBACK_DATA(FOR) \
  93. CALLBACK_DATA_(FOR, p - FOR##_mark, p - data + 1)
  94. /* Run the data callback FOR and don't consume the current byte */
  95. #define CALLBACK_DATA_NOADVANCE(FOR) \
  96. CALLBACK_DATA_(FOR, p - FOR##_mark, p - data)
  97. /* Set the mark FOR; non-destructive if mark is already set */
  98. #define MARK(FOR) \
  99. do { \
  100. if (!FOR##_mark) { \
  101. FOR##_mark = p; \
  102. } \
  103. } while (0)
  104. #define PROXY_CONNECTION "proxy-connection"
  105. #define CONNECTION "connection"
  106. #define CONTENT_LENGTH "content-length"
  107. #define TRANSFER_ENCODING "transfer-encoding"
  108. #define UPGRADE "upgrade"
  109. #define CHUNKED "chunked"
  110. #define KEEP_ALIVE "keep-alive"
  111. #define CLOSE "close"
  112. static const char *method_strings[] =
  113. {
  114. #define XX(num, name, string) #string,
  115. HTTP_METHOD_MAP(XX)
  116. #undef XX
  117. };
  118. /* Tokens as defined by rfc 2616. Also lowercases them.
  119. * token = 1*<any CHAR except CTLs or separators>
  120. * separators = "(" | ")" | "<" | ">" | "@"
  121. * | "," | ";" | ":" | "\" | <">
  122. * | "/" | "[" | "]" | "?" | "="
  123. * | "{" | "}" | SP | HT
  124. */
  125. static const char tokens[256] = {
  126. /* 0 nul 1 soh 2 stx 3 etx 4 eot 5 enq 6 ack 7 bel */
  127. 0, 0, 0, 0, 0, 0, 0, 0,
  128. /* 8 bs 9 ht 10 nl 11 vt 12 np 13 cr 14 so 15 si */
  129. 0, 0, 0, 0, 0, 0, 0, 0,
  130. /* 16 dle 17 dc1 18 dc2 19 dc3 20 dc4 21 nak 22 syn 23 etb */
  131. 0, 0, 0, 0, 0, 0, 0, 0,
  132. /* 24 can 25 em 26 sub 27 esc 28 fs 29 gs 30 rs 31 us */
  133. 0, 0, 0, 0, 0, 0, 0, 0,
  134. /* 32 sp 33 ! 34 " 35 # 36 $ 37 % 38 & 39 ' */
  135. 0, '!', 0, '#', '$', '%', '&', '\'',
  136. /* 40 ( 41 ) 42 * 43 + 44 , 45 - 46 . 47 / */
  137. 0, 0, '*', '+', 0, '-', '.', 0,
  138. /* 48 0 49 1 50 2 51 3 52 4 53 5 54 6 55 7 */
  139. '0', '1', '2', '3', '4', '5', '6', '7',
  140. /* 56 8 57 9 58 : 59 ; 60 < 61 = 62 > 63 ? */
  141. '8', '9', 0, 0, 0, 0, 0, 0,
  142. /* 64 @ 65 A 66 B 67 C 68 D 69 E 70 F 71 G */
  143. 0, 'a', 'b', 'c', 'd', 'e', 'f', 'g',
  144. /* 72 H 73 I 74 J 75 K 76 L 77 M 78 N 79 O */
  145. 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
  146. /* 80 P 81 Q 82 R 83 S 84 T 85 U 86 V 87 W */
  147. 'p', 'q', 'r', 's', 't', 'u', 'v', 'w',
  148. /* 88 X 89 Y 90 Z 91 [ 92 \ 93 ] 94 ^ 95 _ */
  149. 'x', 'y', 'z', 0, 0, 0, '^', '_',
  150. /* 96 ` 97 a 98 b 99 c 100 d 101 e 102 f 103 g */
  151. '`', 'a', 'b', 'c', 'd', 'e', 'f', 'g',
  152. /* 104 h 105 i 106 j 107 k 108 l 109 m 110 n 111 o */
  153. 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
  154. /* 112 p 113 q 114 r 115 s 116 t 117 u 118 v 119 w */
  155. 'p', 'q', 'r', 's', 't', 'u', 'v', 'w',
  156. /* 120 x 121 y 122 z 123 { 124 | 125 } 126 ~ 127 del */
  157. 'x', 'y', 'z', 0, '|', 0, '~', 0 };
  158. static const int8_t unhex[256] =
  159. {-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1
  160. ,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1
  161. ,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1
  162. , 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,-1,-1,-1,-1,-1,-1
  163. ,-1,10,11,12,13,14,15,-1,-1,-1,-1,-1,-1,-1,-1,-1
  164. ,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1
  165. ,-1,10,11,12,13,14,15,-1,-1,-1,-1,-1,-1,-1,-1,-1
  166. ,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1
  167. };
  168. #if HTTP_PARSER_STRICT
  169. # define T(v) 0
  170. #else
  171. # define T(v) v
  172. #endif
  173. static const uint8_t normal_url_char[32] = {
  174. /* 0 nul 1 soh 2 stx 3 etx 4 eot 5 enq 6 ack 7 bel */
  175. 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0,
  176. /* 8 bs 9 ht 10 nl 11 vt 12 np 13 cr 14 so 15 si */
  177. 0 | T(2) | 0 | 0 | T(16) | 0 | 0 | 0,
  178. /* 16 dle 17 dc1 18 dc2 19 dc3 20 dc4 21 nak 22 syn 23 etb */
  179. 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0,
  180. /* 24 can 25 em 26 sub 27 esc 28 fs 29 gs 30 rs 31 us */
  181. 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0,
  182. /* 32 sp 33 ! 34 " 35 # 36 $ 37 % 38 & 39 ' */
  183. 0 | 2 | 4 | 0 | 16 | 32 | 64 | 128,
  184. /* 40 ( 41 ) 42 * 43 + 44 , 45 - 46 . 47 / */
  185. 1 | 2 | 4 | 8 | 16 | 32 | 64 | 128,
  186. /* 48 0 49 1 50 2 51 3 52 4 53 5 54 6 55 7 */
  187. 1 | 2 | 4 | 8 | 16 | 32 | 64 | 128,
  188. /* 56 8 57 9 58 : 59 ; 60 < 61 = 62 > 63 ? */
  189. 1 | 2 | 4 | 8 | 16 | 32 | 64 | 0,
  190. /* 64 @ 65 A 66 B 67 C 68 D 69 E 70 F 71 G */
  191. 1 | 2 | 4 | 8 | 16 | 32 | 64 | 128,
  192. /* 72 H 73 I 74 J 75 K 76 L 77 M 78 N 79 O */
  193. 1 | 2 | 4 | 8 | 16 | 32 | 64 | 128,
  194. /* 80 P 81 Q 82 R 83 S 84 T 85 U 86 V 87 W */
  195. 1 | 2 | 4 | 8 | 16 | 32 | 64 | 128,
  196. /* 88 X 89 Y 90 Z 91 [ 92 \ 93 ] 94 ^ 95 _ */
  197. 1 | 2 | 4 | 8 | 16 | 32 | 64 | 128,
  198. /* 96 ` 97 a 98 b 99 c 100 d 101 e 102 f 103 g */
  199. 1 | 2 | 4 | 8 | 16 | 32 | 64 | 128,
  200. /* 104 h 105 i 106 j 107 k 108 l 109 m 110 n 111 o */
  201. 1 | 2 | 4 | 8 | 16 | 32 | 64 | 128,
  202. /* 112 p 113 q 114 r 115 s 116 t 117 u 118 v 119 w */
  203. 1 | 2 | 4 | 8 | 16 | 32 | 64 | 128,
  204. /* 120 x 121 y 122 z 123 { 124 | 125 } 126 ~ 127 del */
  205. 1 | 2 | 4 | 8 | 16 | 32 | 64 | 0, };
  206. #undef T
  207. enum state
  208. { s_dead = 1 /* important that this is > 0 */
  209. , s_start_req_or_res
  210. , s_res_or_resp_H
  211. , s_start_res
  212. , s_res_H
  213. , s_res_HT
  214. , s_res_HTT
  215. , s_res_HTTP
  216. , s_res_first_http_major
  217. , s_res_http_major
  218. , s_res_first_http_minor
  219. , s_res_http_minor
  220. , s_res_first_status_code
  221. , s_res_status_code
  222. , s_res_status
  223. , s_res_line_almost_done
  224. , s_start_req
  225. , s_req_method
  226. , s_req_spaces_before_url
  227. , s_req_schema
  228. , s_req_schema_slash
  229. , s_req_schema_slash_slash
  230. , s_req_server_start
  231. , s_req_server
  232. , s_req_server_with_at
  233. , s_req_path
  234. , s_req_query_string_start
  235. , s_req_query_string
  236. , s_req_fragment_start
  237. , s_req_fragment
  238. , s_req_http_start
  239. , s_req_http_H
  240. , s_req_http_HT
  241. , s_req_http_HTT
  242. , s_req_http_HTTP
  243. , s_req_first_http_major
  244. , s_req_http_major
  245. , s_req_first_http_minor
  246. , s_req_http_minor
  247. , s_req_line_almost_done
  248. , s_header_field_start
  249. , s_header_field
  250. , s_header_value_start
  251. , s_header_value
  252. , s_header_value_lws
  253. , s_header_almost_done
  254. , s_chunk_size_start
  255. , s_chunk_size
  256. , s_chunk_parameters
  257. , s_chunk_size_almost_done
  258. , s_headers_almost_done
  259. , s_headers_done
  260. /* Important: 's_headers_done' must be the last 'header' state. All
  261. * states beyond this must be 'body' states. It is used for overflow
  262. * checking. See the PARSING_HEADER() macro.
  263. */
  264. , s_chunk_data
  265. , s_chunk_data_almost_done
  266. , s_chunk_data_done
  267. , s_body_identity
  268. , s_body_identity_eof
  269. , s_message_done
  270. };
  271. #define PARSING_HEADER(state) (state <= s_headers_done)
  272. enum header_states
  273. { h_general = 0
  274. , h_C
  275. , h_CO
  276. , h_CON
  277. , h_matching_connection
  278. , h_matching_proxy_connection
  279. , h_matching_content_length
  280. , h_matching_transfer_encoding
  281. , h_matching_upgrade
  282. , h_connection
  283. , h_content_length
  284. , h_transfer_encoding
  285. , h_upgrade
  286. , h_matching_transfer_encoding_chunked
  287. , h_matching_connection_keep_alive
  288. , h_matching_connection_close
  289. , h_transfer_encoding_chunked
  290. , h_connection_keep_alive
  291. , h_connection_close
  292. };
  293. enum http_host_state
  294. {
  295. s_http_host_dead = 1
  296. , s_http_userinfo_start
  297. , s_http_userinfo
  298. , s_http_host_start
  299. , s_http_host_v6_start
  300. , s_http_host
  301. , s_http_host_v6
  302. , s_http_host_v6_end
  303. , s_http_host_port_start
  304. , s_http_host_port
  305. };
  306. /* Macros for character classes; depends on strict-mode */
  307. #define CR '\r'
  308. #define LF '\n'
  309. #define LOWER(c) (unsigned char)(c | 0x20)
  310. #define IS_ALPHA(c) (LOWER(c) >= 'a' && LOWER(c) <= 'z')
  311. #define IS_NUM(c) ((c) >= '0' && (c) <= '9')
  312. #define IS_ALPHANUM(c) (IS_ALPHA(c) || IS_NUM(c))
  313. #define IS_HEX(c) (IS_NUM(c) || (LOWER(c) >= 'a' && LOWER(c) <= 'f'))
  314. #define IS_MARK(c) ((c) == '-' || (c) == '_' || (c) == '.' || \
  315. (c) == '!' || (c) == '~' || (c) == '*' || (c) == '\'' || (c) == '(' || \
  316. (c) == ')')
  317. #define IS_USERINFO_CHAR(c) (IS_ALPHANUM(c) || IS_MARK(c) || (c) == '%' || \
  318. (c) == ';' || (c) == ':' || (c) == '&' || (c) == '=' || (c) == '+' || \
  319. (c) == '$' || (c) == ',')
  320. #if HTTP_PARSER_STRICT
  321. #define TOKEN(c) (tokens[(unsigned char)c])
  322. #define IS_URL_CHAR(c) (BIT_AT(normal_url_char, (unsigned char)c))
  323. #define IS_HOST_CHAR(c) (IS_ALPHANUM(c) || (c) == '.' || (c) == '-')
  324. #else
  325. #define TOKEN(c) ((c == ' ') ? ' ' : tokens[(unsigned char)c])
  326. #define IS_URL_CHAR(c) \
  327. (BIT_AT(normal_url_char, (unsigned char)c) || ((c) & 0x80))
  328. #define IS_HOST_CHAR(c) \
  329. (IS_ALPHANUM(c) || (c) == '.' || (c) == '-' || (c) == '_')
  330. #endif
  331. #define start_state (parser->type == HTTP_REQUEST ? s_start_req : s_start_res)
  332. #if HTTP_PARSER_STRICT
  333. # define STRICT_CHECK(cond) \
  334. do { \
  335. if (cond) { \
  336. SET_ERRNO(HPE_STRICT); \
  337. goto error; \
  338. } \
  339. } while (0)
  340. # define NEW_MESSAGE() (http_should_keep_alive(parser) ? start_state : s_dead)
  341. #else
  342. # define STRICT_CHECK(cond)
  343. # define NEW_MESSAGE() start_state
  344. #endif
  345. /* Map errno values to strings for human-readable output */
  346. #define HTTP_STRERROR_GEN(n, s) { "HPE_" #n, s },
  347. static struct {
  348. const char *name;
  349. const char *description;
  350. } http_strerror_tab[] = {
  351. HTTP_ERRNO_MAP(HTTP_STRERROR_GEN)
  352. };
  353. #undef HTTP_STRERROR_GEN
  354. int http_message_needs_eof(const http_parser *parser);
  355. /* Our URL parser.
  356. *
  357. * This is designed to be shared by http_parser_execute() for URL validation,
  358. * hence it has a state transition + byte-for-byte interface. In addition, it
  359. * is meant to be embedded in http_parser_parse_url(), which does the dirty
  360. * work of turning state transitions URL components for its API.
  361. *
  362. * This function should only be invoked with non-space characters. It is
  363. * assumed that the caller cares about (and can detect) the transition between
  364. * URL and non-URL states by looking for these.
  365. */
  366. static enum state
  367. parse_url_char(enum state s, const char ch)
  368. {
  369. if (ch == ' ' || ch == '\r' || ch == '\n') {
  370. return s_dead;
  371. }
  372. #if HTTP_PARSER_STRICT
  373. if (ch == '\t' || ch == '\f') {
  374. return s_dead;
  375. }
  376. #endif
  377. switch (s) {
  378. case s_req_spaces_before_url:
  379. /* Proxied requests are followed by scheme of an absolute URI (alpha).
  380. * All methods except CONNECT are followed by '/' or '*'.
  381. */
  382. if (ch == '/' || ch == '*') {
  383. return s_req_path;
  384. }
  385. if (IS_ALPHA(ch)) {
  386. return s_req_schema;
  387. }
  388. break;
  389. case s_req_schema:
  390. if (IS_ALPHA(ch)) {
  391. return s;
  392. }
  393. if (ch == ':') {
  394. return s_req_schema_slash;
  395. }
  396. break;
  397. case s_req_schema_slash:
  398. if (ch == '/') {
  399. return s_req_schema_slash_slash;
  400. }
  401. break;
  402. case s_req_schema_slash_slash:
  403. if (ch == '/') {
  404. return s_req_server_start;
  405. }
  406. break;
  407. case s_req_server_with_at:
  408. if (ch == '@') {
  409. return s_dead;
  410. }
  411. /* FALLTHROUGH */
  412. case s_req_server_start:
  413. case s_req_server:
  414. if (ch == '/') {
  415. return s_req_path;
  416. }
  417. if (ch == '?') {
  418. return s_req_query_string_start;
  419. }
  420. if (ch == '@') {
  421. return s_req_server_with_at;
  422. }
  423. if (IS_USERINFO_CHAR(ch) || ch == '[' || ch == ']') {
  424. return s_req_server;
  425. }
  426. break;
  427. case s_req_path:
  428. if (IS_URL_CHAR(ch)) {
  429. return s;
  430. }
  431. switch (ch) {
  432. case '?':
  433. return s_req_query_string_start;
  434. case '#':
  435. return s_req_fragment_start;
  436. }
  437. break;
  438. case s_req_query_string_start:
  439. case s_req_query_string:
  440. if (IS_URL_CHAR(ch)) {
  441. return s_req_query_string;
  442. }
  443. switch (ch) {
  444. case '?':
  445. /* allow extra '?' in query string */
  446. return s_req_query_string;
  447. case '#':
  448. return s_req_fragment_start;
  449. }
  450. break;
  451. case s_req_fragment_start:
  452. if (IS_URL_CHAR(ch)) {
  453. return s_req_fragment;
  454. }
  455. switch (ch) {
  456. case '?':
  457. return s_req_fragment;
  458. case '#':
  459. return s;
  460. }
  461. break;
  462. case s_req_fragment:
  463. if (IS_URL_CHAR(ch)) {
  464. return s;
  465. }
  466. switch (ch) {
  467. case '?':
  468. case '#':
  469. return s;
  470. }
  471. break;
  472. default:
  473. break;
  474. }
  475. /* We should never fall out of the switch above unless there's an error */
  476. return s_dead;
  477. }
  478. size_t http_parser_execute (http_parser *parser,
  479. const http_parser_settings *settings,
  480. const char *data,
  481. size_t len)
  482. {
  483. char c, ch;
  484. int8_t unhex_val;
  485. const char *p = data;
  486. const char *header_field_mark = 0;
  487. const char *header_value_mark = 0;
  488. const char *url_mark = 0;
  489. const char *body_mark = 0;
  490. /* We're in an error state. Don't bother doing anything. */
  491. if (HTTP_PARSER_ERRNO(parser) != HPE_OK) {
  492. return 0;
  493. }
  494. if (len == 0) {
  495. switch (parser->state) {
  496. case s_body_identity_eof:
  497. /* Use of CALLBACK_NOTIFY() here would erroneously return 1 byte read if
  498. * we got paused.
  499. */
  500. CALLBACK_NOTIFY_NOADVANCE(message_complete);
  501. return 0;
  502. case s_dead:
  503. case s_start_req_or_res:
  504. case s_start_res:
  505. case s_start_req:
  506. return 0;
  507. default:
  508. SET_ERRNO(HPE_INVALID_EOF_STATE);
  509. return 1;
  510. }
  511. }
  512. if (parser->state == s_header_field)
  513. header_field_mark = data;
  514. if (parser->state == s_header_value)
  515. header_value_mark = data;
  516. switch (parser->state) {
  517. case s_req_path:
  518. case s_req_schema:
  519. case s_req_schema_slash:
  520. case s_req_schema_slash_slash:
  521. case s_req_server_start:
  522. case s_req_server:
  523. case s_req_server_with_at:
  524. case s_req_query_string_start:
  525. case s_req_query_string:
  526. case s_req_fragment_start:
  527. case s_req_fragment:
  528. url_mark = data;
  529. break;
  530. }
  531. for (p=data; p != data + len; p++) {
  532. ch = *p;
  533. if (PARSING_HEADER(parser->state)) {
  534. ++parser->nread;
  535. /* Buffer overflow attack */
  536. if (parser->nread > HTTP_MAX_HEADER_SIZE) {
  537. SET_ERRNO(HPE_HEADER_OVERFLOW);
  538. goto error;
  539. }
  540. }
  541. reexecute_byte:
  542. switch (parser->state) {
  543. case s_dead:
  544. /* this state is used after a 'Connection: close' message
  545. * the parser will error out if it reads another message
  546. */
  547. if (ch == CR || ch == LF)
  548. break;
  549. SET_ERRNO(HPE_CLOSED_CONNECTION);
  550. goto error;
  551. case s_start_req_or_res:
  552. {
  553. if (ch == CR || ch == LF)
  554. break;
  555. parser->flags = 0;
  556. parser->content_length = ULLONG_MAX;
  557. if (ch == 'H') {
  558. parser->state = s_res_or_resp_H;
  559. CALLBACK_NOTIFY(message_begin);
  560. } else {
  561. parser->type = HTTP_REQUEST;
  562. parser->state = s_start_req;
  563. goto reexecute_byte;
  564. }
  565. break;
  566. }
  567. case s_res_or_resp_H:
  568. if (ch == 'T') {
  569. parser->type = HTTP_RESPONSE;
  570. parser->state = s_res_HT;
  571. } else {
  572. if (ch != 'E') {
  573. SET_ERRNO(HPE_INVALID_CONSTANT);
  574. goto error;
  575. }
  576. parser->type = HTTP_REQUEST;
  577. parser->method = HTTP_HEAD;
  578. parser->index = 2;
  579. parser->state = s_req_method;
  580. }
  581. break;
  582. case s_start_res:
  583. {
  584. parser->flags = 0;
  585. parser->content_length = ULLONG_MAX;
  586. switch (ch) {
  587. case 'H':
  588. parser->state = s_res_H;
  589. break;
  590. case CR:
  591. case LF:
  592. break;
  593. default:
  594. SET_ERRNO(HPE_INVALID_CONSTANT);
  595. goto error;
  596. }
  597. CALLBACK_NOTIFY(message_begin);
  598. break;
  599. }
  600. case s_res_H:
  601. STRICT_CHECK(ch != 'T');
  602. parser->state = s_res_HT;
  603. break;
  604. case s_res_HT:
  605. STRICT_CHECK(ch != 'T');
  606. parser->state = s_res_HTT;
  607. break;
  608. case s_res_HTT:
  609. STRICT_CHECK(ch != 'P');
  610. parser->state = s_res_HTTP;
  611. break;
  612. case s_res_HTTP:
  613. STRICT_CHECK(ch != '/');
  614. parser->state = s_res_first_http_major;
  615. break;
  616. case s_res_first_http_major:
  617. if (ch < '0' || ch > '9') {
  618. SET_ERRNO(HPE_INVALID_VERSION);
  619. goto error;
  620. }
  621. parser->http_major = ch - '0';
  622. parser->state = s_res_http_major;
  623. break;
  624. /* major HTTP version or dot */
  625. case s_res_http_major:
  626. {
  627. if (ch == '.') {
  628. parser->state = s_res_first_http_minor;
  629. break;
  630. }
  631. if (!IS_NUM(ch)) {
  632. SET_ERRNO(HPE_INVALID_VERSION);
  633. goto error;
  634. }
  635. parser->http_major *= 10;
  636. parser->http_major += ch - '0';
  637. if (parser->http_major > 999) {
  638. SET_ERRNO(HPE_INVALID_VERSION);
  639. goto error;
  640. }
  641. break;
  642. }
  643. /* first digit of minor HTTP version */
  644. case s_res_first_http_minor:
  645. if (!IS_NUM(ch)) {
  646. SET_ERRNO(HPE_INVALID_VERSION);
  647. goto error;
  648. }
  649. parser->http_minor = ch - '0';
  650. parser->state = s_res_http_minor;
  651. break;
  652. /* minor HTTP version or end of request line */
  653. case s_res_http_minor:
  654. {
  655. if (ch == ' ') {
  656. parser->state = s_res_first_status_code;
  657. break;
  658. }
  659. if (!IS_NUM(ch)) {
  660. SET_ERRNO(HPE_INVALID_VERSION);
  661. goto error;
  662. }
  663. parser->http_minor *= 10;
  664. parser->http_minor += ch - '0';
  665. if (parser->http_minor > 999) {
  666. SET_ERRNO(HPE_INVALID_VERSION);
  667. goto error;
  668. }
  669. break;
  670. }
  671. case s_res_first_status_code:
  672. {
  673. if (!IS_NUM(ch)) {
  674. if (ch == ' ') {
  675. break;
  676. }
  677. SET_ERRNO(HPE_INVALID_STATUS);
  678. goto error;
  679. }
  680. parser->status_code = ch - '0';
  681. parser->state = s_res_status_code;
  682. break;
  683. }
  684. case s_res_status_code:
  685. {
  686. if (!IS_NUM(ch)) {
  687. switch (ch) {
  688. case ' ':
  689. parser->state = s_res_status;
  690. break;
  691. case CR:
  692. parser->state = s_res_line_almost_done;
  693. break;
  694. case LF:
  695. parser->state = s_header_field_start;
  696. break;
  697. default:
  698. SET_ERRNO(HPE_INVALID_STATUS);
  699. goto error;
  700. }
  701. break;
  702. }
  703. parser->status_code *= 10;
  704. parser->status_code += ch - '0';
  705. if (parser->status_code > 999) {
  706. SET_ERRNO(HPE_INVALID_STATUS);
  707. goto error;
  708. }
  709. break;
  710. }
  711. case s_res_status:
  712. /* the human readable status. e.g. "NOT FOUND"
  713. * we are not humans so just ignore this */
  714. if (ch == CR) {
  715. parser->state = s_res_line_almost_done;
  716. break;
  717. }
  718. if (ch == LF) {
  719. parser->state = s_header_field_start;
  720. break;
  721. }
  722. break;
  723. case s_res_line_almost_done:
  724. STRICT_CHECK(ch != LF);
  725. parser->state = s_header_field_start;
  726. CALLBACK_NOTIFY(status_complete);
  727. break;
  728. case s_start_req:
  729. {
  730. if (ch == CR || ch == LF)
  731. break;
  732. parser->flags = 0;
  733. parser->content_length = ULLONG_MAX;
  734. if (!IS_ALPHA(ch)) {
  735. SET_ERRNO(HPE_INVALID_METHOD);
  736. goto error;
  737. }
  738. parser->method = (enum http_method) 0;
  739. parser->index = 1;
  740. switch (ch) {
  741. case 'C': parser->method = HTTP_CONNECT; /* or COPY, CHECKOUT */ break;
  742. case 'D': parser->method = HTTP_DELETE; break;
  743. case 'G': parser->method = HTTP_GET; break;
  744. case 'H': parser->method = HTTP_HEAD; break;
  745. case 'L': parser->method = HTTP_LOCK; break;
  746. case 'M': parser->method = HTTP_MKCOL; /* or MOVE, MKACTIVITY, MERGE, M-SEARCH */ break;
  747. case 'N': parser->method = HTTP_NOTIFY; break;
  748. case 'O': parser->method = HTTP_OPTIONS; break;
  749. case 'P': parser->method = HTTP_POST;
  750. /* or PROPFIND|PROPPATCH|PUT|PATCH|PURGE */
  751. break;
  752. case 'R': parser->method = HTTP_REPORT; break;
  753. case 'S': parser->method = HTTP_SUBSCRIBE; /* or SEARCH */ break;
  754. case 'T': parser->method = HTTP_TRACE; break;
  755. case 'U': parser->method = HTTP_UNLOCK; /* or UNSUBSCRIBE */ break;
  756. default:
  757. SET_ERRNO(HPE_INVALID_METHOD);
  758. goto error;
  759. }
  760. parser->state = s_req_method;
  761. CALLBACK_NOTIFY(message_begin);
  762. break;
  763. }
  764. case s_req_method:
  765. {
  766. const char *matcher;
  767. if (ch == '\0') {
  768. SET_ERRNO(HPE_INVALID_METHOD);
  769. goto error;
  770. }
  771. matcher = method_strings[parser->method];
  772. if (ch == ' ' && matcher[parser->index] == '\0') {
  773. parser->state = s_req_spaces_before_url;
  774. } else if (ch == matcher[parser->index]) {
  775. ; /* nada */
  776. } else if (parser->method == HTTP_CONNECT) {
  777. if (parser->index == 1 && ch == 'H') {
  778. parser->method = HTTP_CHECKOUT;
  779. } else if (parser->index == 2 && ch == 'P') {
  780. parser->method = HTTP_COPY;
  781. } else {
  782. goto error;
  783. }
  784. } else if (parser->method == HTTP_MKCOL) {
  785. if (parser->index == 1 && ch == 'O') {
  786. parser->method = HTTP_MOVE;
  787. } else if (parser->index == 1 && ch == 'E') {
  788. parser->method = HTTP_MERGE;
  789. } else if (parser->index == 1 && ch == '-') {
  790. parser->method = HTTP_MSEARCH;
  791. } else if (parser->index == 2 && ch == 'A') {
  792. parser->method = HTTP_MKACTIVITY;
  793. } else {
  794. goto error;
  795. }
  796. } else if (parser->method == HTTP_SUBSCRIBE) {
  797. if (parser->index == 1 && ch == 'E') {
  798. parser->method = HTTP_SEARCH;
  799. } else {
  800. goto error;
  801. }
  802. } else if (parser->index == 1 && parser->method == HTTP_POST) {
  803. if (ch == 'R') {
  804. parser->method = HTTP_PROPFIND; /* or HTTP_PROPPATCH */
  805. } else if (ch == 'U') {
  806. parser->method = HTTP_PUT; /* or HTTP_PURGE */
  807. } else if (ch == 'A') {
  808. parser->method = HTTP_PATCH;
  809. } else {
  810. goto error;
  811. }
  812. } else if (parser->index == 2) {
  813. if (parser->method == HTTP_PUT) {
  814. if (ch == 'R') parser->method = HTTP_PURGE;
  815. } else if (parser->method == HTTP_UNLOCK) {
  816. if (ch == 'S') parser->method = HTTP_UNSUBSCRIBE;
  817. }
  818. } else if (parser->index == 4 && parser->method == HTTP_PROPFIND && ch == 'P') {
  819. parser->method = HTTP_PROPPATCH;
  820. } else {
  821. SET_ERRNO(HPE_INVALID_METHOD);
  822. goto error;
  823. }
  824. ++parser->index;
  825. break;
  826. }
  827. case s_req_spaces_before_url:
  828. {
  829. if (ch == ' ') break;
  830. MARK(url);
  831. if (parser->method == HTTP_CONNECT) {
  832. parser->state = s_req_server_start;
  833. }
  834. parser->state = parse_url_char((enum state)parser->state, ch);
  835. if (parser->state == s_dead) {
  836. SET_ERRNO(HPE_INVALID_URL);
  837. goto error;
  838. }
  839. break;
  840. }
  841. case s_req_schema:
  842. case s_req_schema_slash:
  843. case s_req_schema_slash_slash:
  844. case s_req_server_start:
  845. {
  846. switch (ch) {
  847. /* No whitespace allowed here */
  848. case ' ':
  849. case CR:
  850. case LF:
  851. SET_ERRNO(HPE_INVALID_URL);
  852. goto error;
  853. default:
  854. parser->state = parse_url_char((enum state)parser->state, ch);
  855. if (parser->state == s_dead) {
  856. SET_ERRNO(HPE_INVALID_URL);
  857. goto error;
  858. }
  859. }
  860. break;
  861. }
  862. case s_req_server:
  863. case s_req_server_with_at:
  864. case s_req_path:
  865. case s_req_query_string_start:
  866. case s_req_query_string:
  867. case s_req_fragment_start:
  868. case s_req_fragment:
  869. {
  870. switch (ch) {
  871. case ' ':
  872. parser->state = s_req_http_start;
  873. CALLBACK_DATA(url);
  874. break;
  875. case CR:
  876. case LF:
  877. parser->http_major = 0;
  878. parser->http_minor = 9;
  879. parser->state = (ch == CR) ?
  880. s_req_line_almost_done :
  881. s_header_field_start;
  882. CALLBACK_DATA(url);
  883. break;
  884. default:
  885. parser->state = parse_url_char((enum state)parser->state, ch);
  886. if (parser->state == s_dead) {
  887. SET_ERRNO(HPE_INVALID_URL);
  888. goto error;
  889. }
  890. }
  891. break;
  892. }
  893. case s_req_http_start:
  894. switch (ch) {
  895. case 'H':
  896. parser->state = s_req_http_H;
  897. break;
  898. case ' ':
  899. break;
  900. default:
  901. SET_ERRNO(HPE_INVALID_CONSTANT);
  902. goto error;
  903. }
  904. break;
  905. case s_req_http_H:
  906. STRICT_CHECK(ch != 'T');
  907. parser->state = s_req_http_HT;
  908. break;
  909. case s_req_http_HT:
  910. STRICT_CHECK(ch != 'T');
  911. parser->state = s_req_http_HTT;
  912. break;
  913. case s_req_http_HTT:
  914. STRICT_CHECK(ch != 'P');
  915. parser->state = s_req_http_HTTP;
  916. break;
  917. case s_req_http_HTTP:
  918. STRICT_CHECK(ch != '/');
  919. parser->state = s_req_first_http_major;
  920. break;
  921. /* first digit of major HTTP version */
  922. case s_req_first_http_major:
  923. if (ch < '1' || ch > '9') {
  924. SET_ERRNO(HPE_INVALID_VERSION);
  925. goto error;
  926. }
  927. parser->http_major = ch - '0';
  928. parser->state = s_req_http_major;
  929. break;
  930. /* major HTTP version or dot */
  931. case s_req_http_major:
  932. {
  933. if (ch == '.') {
  934. parser->state = s_req_first_http_minor;
  935. break;
  936. }
  937. if (!IS_NUM(ch)) {
  938. SET_ERRNO(HPE_INVALID_VERSION);
  939. goto error;
  940. }
  941. parser->http_major *= 10;
  942. parser->http_major += ch - '0';
  943. if (parser->http_major > 999) {
  944. SET_ERRNO(HPE_INVALID_VERSION);
  945. goto error;
  946. }
  947. break;
  948. }
  949. /* first digit of minor HTTP version */
  950. case s_req_first_http_minor:
  951. if (!IS_NUM(ch)) {
  952. SET_ERRNO(HPE_INVALID_VERSION);
  953. goto error;
  954. }
  955. parser->http_minor = ch - '0';
  956. parser->state = s_req_http_minor;
  957. break;
  958. /* minor HTTP version or end of request line */
  959. case s_req_http_minor:
  960. {
  961. if (ch == CR) {
  962. parser->state = s_req_line_almost_done;
  963. break;
  964. }
  965. if (ch == LF) {
  966. parser->state = s_header_field_start;
  967. break;
  968. }
  969. /* XXX allow spaces after digit? */
  970. if (!IS_NUM(ch)) {
  971. SET_ERRNO(HPE_INVALID_VERSION);
  972. goto error;
  973. }
  974. parser->http_minor *= 10;
  975. parser->http_minor += ch - '0';
  976. if (parser->http_minor > 999) {
  977. SET_ERRNO(HPE_INVALID_VERSION);
  978. goto error;
  979. }
  980. break;
  981. }
  982. /* end of request line */
  983. case s_req_line_almost_done:
  984. {
  985. if (ch != LF) {
  986. SET_ERRNO(HPE_LF_EXPECTED);
  987. goto error;
  988. }
  989. parser->state = s_header_field_start;
  990. break;
  991. }
  992. case s_header_field_start:
  993. {
  994. if (ch == CR) {
  995. parser->state = s_headers_almost_done;
  996. break;
  997. }
  998. if (ch == LF) {
  999. /* they might be just sending \n instead of \r\n so this would be
  1000. * the second \n to denote the end of headers*/
  1001. parser->state = s_headers_almost_done;
  1002. goto reexecute_byte;
  1003. }
  1004. c = TOKEN(ch);
  1005. if (!c) {
  1006. SET_ERRNO(HPE_INVALID_HEADER_TOKEN);
  1007. goto error;
  1008. }
  1009. MARK(header_field);
  1010. parser->index = 0;
  1011. parser->state = s_header_field;
  1012. switch (c) {
  1013. case 'c':
  1014. parser->header_state = h_C;
  1015. break;
  1016. case 'p':
  1017. parser->header_state = h_matching_proxy_connection;
  1018. break;
  1019. case 't':
  1020. parser->header_state = h_matching_transfer_encoding;
  1021. break;
  1022. case 'u':
  1023. parser->header_state = h_matching_upgrade;
  1024. break;
  1025. default:
  1026. parser->header_state = h_general;
  1027. break;
  1028. }
  1029. break;
  1030. }
  1031. case s_header_field:
  1032. {
  1033. c = TOKEN(ch);
  1034. if (c) {
  1035. switch (parser->header_state) {
  1036. case h_general:
  1037. break;
  1038. case h_C:
  1039. parser->index++;
  1040. parser->header_state = (c == 'o' ? h_CO : h_general);
  1041. break;
  1042. case h_CO:
  1043. parser->index++;
  1044. parser->header_state = (c == 'n' ? h_CON : h_general);
  1045. break;
  1046. case h_CON:
  1047. parser->index++;
  1048. switch (c) {
  1049. case 'n':
  1050. parser->header_state = h_matching_connection;
  1051. break;
  1052. case 't':
  1053. parser->header_state = h_matching_content_length;
  1054. break;
  1055. default:
  1056. parser->header_state = h_general;
  1057. break;
  1058. }
  1059. break;
  1060. /* connection */
  1061. case h_matching_connection:
  1062. parser->index++;
  1063. if (parser->index > sizeof(CONNECTION)-1
  1064. || c != CONNECTION[parser->index]) {
  1065. parser->header_state = h_general;
  1066. } else if (parser->index == sizeof(CONNECTION)-2) {
  1067. parser->header_state = h_connection;
  1068. }
  1069. break;
  1070. /* proxy-connection */
  1071. case h_matching_proxy_connection:
  1072. parser->index++;
  1073. if (parser->index > sizeof(PROXY_CONNECTION)-1
  1074. || c != PROXY_CONNECTION[parser->index]) {
  1075. parser->header_state = h_general;
  1076. } else if (parser->index == sizeof(PROXY_CONNECTION)-2) {
  1077. parser->header_state = h_connection;
  1078. }
  1079. break;
  1080. /* content-length */
  1081. case h_matching_content_length:
  1082. parser->index++;
  1083. if (parser->index > sizeof(CONTENT_LENGTH)-1
  1084. || c != CONTENT_LENGTH[parser->index]) {
  1085. parser->header_state = h_general;
  1086. } else if (parser->index == sizeof(CONTENT_LENGTH)-2) {
  1087. parser->header_state = h_content_length;
  1088. }
  1089. break;
  1090. /* transfer-encoding */
  1091. case h_matching_transfer_encoding:
  1092. parser->index++;
  1093. if (parser->index > sizeof(TRANSFER_ENCODING)-1
  1094. || c != TRANSFER_ENCODING[parser->index]) {
  1095. parser->header_state = h_general;
  1096. } else if (parser->index == sizeof(TRANSFER_ENCODING)-2) {
  1097. parser->header_state = h_transfer_encoding;
  1098. }
  1099. break;
  1100. /* upgrade */
  1101. case h_matching_upgrade:
  1102. parser->index++;
  1103. if (parser->index > sizeof(UPGRADE)-1
  1104. || c != UPGRADE[parser->index]) {
  1105. parser->header_state = h_general;
  1106. } else if (parser->index == sizeof(UPGRADE)-2) {
  1107. parser->header_state = h_upgrade;
  1108. }
  1109. break;
  1110. case h_connection:
  1111. case h_content_length:
  1112. case h_transfer_encoding:
  1113. case h_upgrade:
  1114. if (ch != ' ') parser->header_state = h_general;
  1115. break;
  1116. default:
  1117. assert(0 && "Unknown header_state");
  1118. break;
  1119. }
  1120. break;
  1121. }
  1122. if (ch == ':') {
  1123. parser->state = s_header_value_start;
  1124. CALLBACK_DATA(header_field);
  1125. break;
  1126. }
  1127. if (ch == CR) {
  1128. parser->state = s_header_almost_done;
  1129. CALLBACK_DATA(header_field);
  1130. break;
  1131. }
  1132. if (ch == LF) {
  1133. parser->state = s_header_field_start;
  1134. CALLBACK_DATA(header_field);
  1135. break;
  1136. }
  1137. SET_ERRNO(HPE_INVALID_HEADER_TOKEN);
  1138. goto error;
  1139. }
  1140. case s_header_value_start:
  1141. {
  1142. if (ch == ' ' || ch == '\t') break;
  1143. MARK(header_value);
  1144. parser->state = s_header_value;
  1145. parser->index = 0;
  1146. if (ch == CR) {
  1147. parser->header_state = h_general;
  1148. parser->state = s_header_almost_done;
  1149. CALLBACK_DATA(header_value);
  1150. break;
  1151. }
  1152. if (ch == LF) {
  1153. parser->state = s_header_field_start;
  1154. CALLBACK_DATA(header_value);
  1155. break;
  1156. }
  1157. c = LOWER(ch);
  1158. switch (parser->header_state) {
  1159. case h_upgrade:
  1160. parser->flags |= F_UPGRADE;
  1161. parser->header_state = h_general;
  1162. break;
  1163. case h_transfer_encoding:
  1164. /* looking for 'Transfer-Encoding: chunked' */
  1165. if ('c' == c) {
  1166. parser->header_state = h_matching_transfer_encoding_chunked;
  1167. } else {
  1168. parser->header_state = h_general;
  1169. }
  1170. break;
  1171. case h_content_length:
  1172. if (!IS_NUM(ch)) {
  1173. SET_ERRNO(HPE_INVALID_CONTENT_LENGTH);
  1174. goto error;
  1175. }
  1176. parser->content_length = ch - '0';
  1177. break;
  1178. case h_connection:
  1179. /* looking for 'Connection: keep-alive' */
  1180. if (c == 'k') {
  1181. parser->header_state = h_matching_connection_keep_alive;
  1182. /* looking for 'Connection: close' */
  1183. } else if (c == 'c') {
  1184. parser->header_state = h_matching_connection_close;
  1185. } else {
  1186. parser->header_state = h_general;
  1187. }
  1188. break;
  1189. default:
  1190. parser->header_state = h_general;
  1191. break;
  1192. }
  1193. break;
  1194. }
  1195. case s_header_value:
  1196. {
  1197. if (ch == CR) {
  1198. parser->state = s_header_almost_done;
  1199. CALLBACK_DATA(header_value);
  1200. break;
  1201. }
  1202. if (ch == LF) {
  1203. parser->state = s_header_almost_done;
  1204. CALLBACK_DATA_NOADVANCE(header_value);
  1205. goto reexecute_byte;
  1206. }
  1207. c = LOWER(ch);
  1208. switch (parser->header_state) {
  1209. case h_general:
  1210. break;
  1211. case h_connection:
  1212. case h_transfer_encoding:
  1213. assert(0 && "Shouldn't get here.");
  1214. break;
  1215. case h_content_length:
  1216. {
  1217. uint64_t t;
  1218. if (ch == ' ') break;
  1219. if (!IS_NUM(ch)) {
  1220. SET_ERRNO(HPE_INVALID_CONTENT_LENGTH);
  1221. goto error;
  1222. }
  1223. t = parser->content_length;
  1224. t *= 10;
  1225. t += ch - '0';
  1226. /* Overflow? */
  1227. if (t < parser->content_length || t == ULLONG_MAX) {
  1228. SET_ERRNO(HPE_INVALID_CONTENT_LENGTH);
  1229. goto error;
  1230. }
  1231. parser->content_length = t;
  1232. break;
  1233. }
  1234. /* Transfer-Encoding: chunked */
  1235. case h_matching_transfer_encoding_chunked:
  1236. parser->index++;
  1237. if (parser->index > sizeof(CHUNKED)-1
  1238. || c != CHUNKED[parser->index]) {
  1239. parser->header_state = h_general;
  1240. } else if (parser->index == sizeof(CHUNKED)-2) {
  1241. parser->header_state = h_transfer_encoding_chunked;
  1242. }
  1243. break;
  1244. /* looking for 'Connection: keep-alive' */
  1245. case h_matching_connection_keep_alive:
  1246. parser->index++;
  1247. if (parser->index > sizeof(KEEP_ALIVE)-1
  1248. || c != KEEP_ALIVE[parser->index]) {
  1249. parser->header_state = h_general;
  1250. } else if (parser->index == sizeof(KEEP_ALIVE)-2) {
  1251. parser->header_state = h_connection_keep_alive;
  1252. }
  1253. break;
  1254. /* looking for 'Connection: close' */
  1255. case h_matching_connection_close:
  1256. parser->index++;
  1257. if (parser->index > sizeof(CLOSE)-1 || c != CLOSE[parser->index]) {
  1258. parser->header_state = h_general;
  1259. } else if (parser->index == sizeof(CLOSE)-2) {
  1260. parser->header_state = h_connection_close;
  1261. }
  1262. break;
  1263. case h_transfer_encoding_chunked:
  1264. case h_connection_keep_alive:
  1265. case h_connection_close:
  1266. if (ch != ' ') parser->header_state = h_general;
  1267. break;
  1268. default:
  1269. parser->state = s_header_value;
  1270. parser->header_state = h_general;
  1271. break;
  1272. }
  1273. break;
  1274. }
  1275. case s_header_almost_done:
  1276. {
  1277. STRICT_CHECK(ch != LF);
  1278. parser->state = s_header_value_lws;
  1279. switch (parser->header_state) {
  1280. case h_connection_keep_alive:
  1281. parser->flags |= F_CONNECTION_KEEP_ALIVE;
  1282. break;
  1283. case h_connection_close:
  1284. parser->flags |= F_CONNECTION_CLOSE;
  1285. break;
  1286. case h_transfer_encoding_chunked:
  1287. parser->flags |= F_CHUNKED;
  1288. break;
  1289. default:
  1290. break;
  1291. }
  1292. break;
  1293. }
  1294. case s_header_value_lws:
  1295. {
  1296. if (ch == ' ' || ch == '\t')
  1297. parser->state = s_header_value_start;
  1298. else
  1299. {
  1300. parser->state = s_header_field_start;
  1301. goto reexecute_byte;
  1302. }
  1303. break;
  1304. }
  1305. case s_headers_almost_done:
  1306. {
  1307. STRICT_CHECK(ch != LF);
  1308. if (parser->flags & F_TRAILING) {
  1309. /* End of a chunked request */
  1310. parser->state = NEW_MESSAGE();
  1311. CALLBACK_NOTIFY(message_complete);
  1312. break;
  1313. }
  1314. parser->state = s_headers_done;
  1315. /* Set this here so that on_headers_complete() callbacks can see it */
  1316. parser->upgrade =
  1317. (parser->flags & F_UPGRADE || parser->method == HTTP_CONNECT);
  1318. /* Here we call the headers_complete callback. This is somewhat
  1319. * different than other callbacks because if the user returns 1, we
  1320. * will interpret that as saying that this message has no body. This
  1321. * is needed for the annoying case of recieving a response to a HEAD
  1322. * request.
  1323. *
  1324. * We'd like to use CALLBACK_NOTIFY_NOADVANCE() here but we cannot, so
  1325. * we have to simulate it by handling a change in errno below.
  1326. */
  1327. if (settings->on_headers_complete) {
  1328. switch (settings->on_headers_complete(parser)) {
  1329. case 0:
  1330. break;
  1331. case 1:
  1332. parser->flags |= F_SKIPBODY;
  1333. break;
  1334. default:
  1335. SET_ERRNO(HPE_CB_headers_complete);
  1336. return p - data; /* Error */
  1337. }
  1338. }
  1339. if (HTTP_PARSER_ERRNO(parser) != HPE_OK) {
  1340. return p - data;
  1341. }
  1342. goto reexecute_byte;
  1343. }
  1344. case s_headers_done:
  1345. {
  1346. STRICT_CHECK(ch != LF);
  1347. parser->nread = 0;
  1348. /* Exit, the rest of the connect is in a different protocol. */
  1349. if (parser->upgrade) {
  1350. parser->state = NEW_MESSAGE();
  1351. CALLBACK_NOTIFY(message_complete);
  1352. return (p - data) + 1;
  1353. }
  1354. if (parser->flags & F_SKIPBODY) {
  1355. parser->state = NEW_MESSAGE();
  1356. CALLBACK_NOTIFY(message_complete);
  1357. } else if (parser->flags & F_CHUNKED) {
  1358. /* chunked encoding - ignore Content-Length header */
  1359. parser->state = s_chunk_size_start;
  1360. } else {
  1361. if (parser->content_length == 0) {
  1362. /* Content-Length header given but zero: Content-Length: 0\r\n */
  1363. parser->state = NEW_MESSAGE();
  1364. CALLBACK_NOTIFY(message_complete);
  1365. } else if (parser->content_length != ULLONG_MAX) {
  1366. /* Content-Length header given and non-zero */
  1367. parser->state = s_body_identity;
  1368. } else {
  1369. if (parser->type == HTTP_REQUEST ||
  1370. !http_message_needs_eof(parser)) {
  1371. /* Assume content-length 0 - read the next */
  1372. parser->state = NEW_MESSAGE();
  1373. CALLBACK_NOTIFY(message_complete);
  1374. } else {
  1375. /* Read body until EOF */
  1376. parser->state = s_body_identity_eof;
  1377. }
  1378. }
  1379. }
  1380. break;
  1381. }
  1382. case s_body_identity:
  1383. {
  1384. uint64_t to_read = MIN(parser->content_length,
  1385. (uint64_t) ((data + len) - p));
  1386. assert(parser->content_length != 0
  1387. && parser->content_length != ULLONG_MAX);
  1388. /* The difference between advancing content_length and p is because
  1389. * the latter will automaticaly advance on the next loop iteration.
  1390. * Further, if content_length ends up at 0, we want to see the last
  1391. * byte again for our message complete callback.
  1392. */
  1393. MARK(body);
  1394. parser->content_length -= to_read;
  1395. p += to_read - 1;
  1396. if (parser->content_length == 0) {
  1397. parser->state = s_message_done;
  1398. /* Mimic CALLBACK_DATA_NOADVANCE() but with one extra byte.
  1399. *
  1400. * The alternative to doing this is to wait for the next byte to
  1401. * trigger the data callback, just as in every other case. The
  1402. * problem with this is that this makes it difficult for the test
  1403. * harness to distinguish between complete-on-EOF and
  1404. * complete-on-length. It's not clear that this distinction is
  1405. * important for applications, but let's keep it for now.
  1406. */
  1407. CALLBACK_DATA_(body, p - body_mark + 1, p - data);
  1408. goto reexecute_byte;
  1409. }
  1410. break;
  1411. }
  1412. /* read until EOF */
  1413. case s_body_identity_eof:
  1414. MARK(body);
  1415. p = data + len - 1;
  1416. break;
  1417. case s_message_done:
  1418. parser->state = NEW_MESSAGE();
  1419. CALLBACK_NOTIFY(message_complete);
  1420. break;
  1421. case s_chunk_size_start:
  1422. {
  1423. assert(parser->nread == 1);
  1424. assert(parser->flags & F_CHUNKED);
  1425. unhex_val = unhex[(unsigned char)ch];
  1426. if (unhex_val == -1) {
  1427. SET_ERRNO(HPE_INVALID_CHUNK_SIZE);
  1428. goto error;
  1429. }
  1430. parser->content_length = unhex_val;
  1431. parser->state = s_chunk_size;
  1432. break;
  1433. }
  1434. case s_chunk_size:
  1435. {
  1436. uint64_t t;
  1437. assert(parser->flags & F_CHUNKED);
  1438. if (ch == CR) {
  1439. parser->state = s_chunk_size_almost_done;
  1440. break;
  1441. }
  1442. unhex_val = unhex[(unsigned char)ch];
  1443. if (unhex_val == -1) {
  1444. if (ch == ';' || ch == ' ') {
  1445. parser->state = s_chunk_parameters;
  1446. break;
  1447. }
  1448. SET_ERRNO(HPE_INVALID_CHUNK_SIZE);
  1449. goto error;
  1450. }
  1451. t = parser->content_length;
  1452. t *= 16;
  1453. t += unhex_val;
  1454. /* Overflow? */
  1455. if (t < parser->content_length || t == ULLONG_MAX) {
  1456. SET_ERRNO(HPE_INVALID_CONTENT_LENGTH);
  1457. goto error;
  1458. }
  1459. parser->content_length = t;
  1460. break;
  1461. }
  1462. case s_chunk_parameters:
  1463. {
  1464. assert(parser->flags & F_CHUNKED);
  1465. /* just ignore this shit. TODO check for overflow */
  1466. if (ch == CR) {
  1467. parser->state = s_chunk_size_almost_done;
  1468. break;
  1469. }
  1470. break;
  1471. }
  1472. case s_chunk_size_almost_done:
  1473. {
  1474. assert(parser->flags & F_CHUNKED);
  1475. STRICT_CHECK(ch != LF);
  1476. parser->nread = 0;
  1477. if (parser->content_length == 0) {
  1478. parser->flags |= F_TRAILING;
  1479. parser->state = s_header_field_start;
  1480. } else {
  1481. parser->state = s_chunk_data;
  1482. }
  1483. break;
  1484. }
  1485. case s_chunk_data:
  1486. {
  1487. uint64_t to_read = MIN(parser->content_length,
  1488. (uint64_t) ((data + len) - p));
  1489. assert(parser->flags & F_CHUNKED);
  1490. assert(parser->content_length != 0
  1491. && parser->content_length != ULLONG_MAX);
  1492. /* See the explanation in s_body_identity for why the content
  1493. * length and data pointers are managed this way.
  1494. */
  1495. MARK(body);
  1496. parser->content_length -= to_read;
  1497. p += to_read - 1;
  1498. if (parser->content_length == 0) {
  1499. parser->state = s_chunk_data_almost_done;
  1500. }
  1501. break;
  1502. }
  1503. case s_chunk_data_almost_done:
  1504. assert(parser->flags & F_CHUNKED);
  1505. assert(parser->content_length == 0);
  1506. STRICT_CHECK(ch != CR);
  1507. parser->state = s_chunk_data_done;
  1508. CALLBACK_DATA(body);
  1509. break;
  1510. case s_chunk_data_done:
  1511. assert(parser->flags & F_CHUNKED);
  1512. STRICT_CHECK(ch != LF);
  1513. parser->nread = 0;
  1514. parser->state = s_chunk_size_start;
  1515. break;
  1516. default:
  1517. assert(0 && "unhandled state");
  1518. SET_ERRNO(HPE_INVALID_INTERNAL_STATE);
  1519. goto error;
  1520. }
  1521. }
  1522. /* Run callbacks for any marks that we have leftover after we ran our of
  1523. * bytes. There should be at most one of these set, so it's OK to invoke
  1524. * them in series (unset marks will not result in callbacks).
  1525. *
  1526. * We use the NOADVANCE() variety of callbacks here because 'p' has already
  1527. * overflowed 'data' and this allows us to correct for the off-by-one that
  1528. * we'd otherwise have (since CALLBACK_DATA() is meant to be run with a 'p'
  1529. * value that's in-bounds).
  1530. */
  1531. assert(((header_field_mark ? 1 : 0) +
  1532. (header_value_mark ? 1 : 0) +
  1533. (url_mark ? 1 : 0) +
  1534. (body_mark ? 1 : 0)) <= 1);
  1535. CALLBACK_DATA_NOADVANCE(header_field);
  1536. CALLBACK_DATA_NOADVANCE(header_value);
  1537. CALLBACK_DATA_NOADVANCE(url);
  1538. CALLBACK_DATA_NOADVANCE(body);
  1539. return len;
  1540. error:
  1541. if (HTTP_PARSER_ERRNO(parser) == HPE_OK) {
  1542. SET_ERRNO(HPE_UNKNOWN);
  1543. }
  1544. return (p - data);
  1545. }
  1546. /* Does the parser need to see an EOF to find the end of the message? */
  1547. int
  1548. http_message_needs_eof (const http_parser *parser)
  1549. {
  1550. if (parser->type == HTTP_REQUEST) {
  1551. return 0;
  1552. }
  1553. /* See RFC 2616 section 4.4 */
  1554. if (parser->status_code / 100 == 1 || /* 1xx e.g. Continue */
  1555. parser->status_code == 204 || /* No Content */
  1556. parser->status_code == 304 || /* Not Modified */
  1557. parser->flags & F_SKIPBODY) { /* response to a HEAD request */
  1558. return 0;
  1559. }
  1560. if ((parser->flags & F_CHUNKED) || parser->content_length != ULLONG_MAX) {
  1561. return 0;
  1562. }
  1563. return 1;
  1564. }
  1565. int
  1566. http_should_keep_alive (const http_parser *parser)
  1567. {
  1568. if (parser->http_major > 0 && parser->http_minor > 0) {
  1569. /* HTTP/1.1 */
  1570. if (parser->flags & F_CONNECTION_CLOSE) {
  1571. return 0;
  1572. }
  1573. } else {
  1574. /* HTTP/1.0 or earlier */
  1575. if (!(parser->flags & F_CONNECTION_KEEP_ALIVE)) {
  1576. return 0;
  1577. }
  1578. }
  1579. return !http_message_needs_eof(parser);
  1580. }
  1581. const char *
  1582. http_method_str (enum http_method m)
  1583. {
  1584. return ELEM_AT(method_strings, m, "<unknown>");
  1585. }
  1586. void
  1587. http_parser_init (http_parser *parser, enum http_parser_type t)
  1588. {
  1589. void *data = parser->data; /* preserve application data */
  1590. memset(parser, 0, sizeof(*parser));
  1591. parser->data = data;
  1592. parser->type = t;
  1593. parser->state = (t == HTTP_REQUEST ? s_start_req : (t == HTTP_RESPONSE ? s_start_res : s_start_req_or_res));
  1594. parser->http_errno = HPE_OK;
  1595. }
  1596. const char *
  1597. http_errno_name(enum http_errno err) {
  1598. assert(err < (sizeof(http_strerror_tab)/sizeof(http_strerror_tab[0])));
  1599. return http_strerror_tab[err].name;
  1600. }
  1601. const char *
  1602. http_errno_description(enum http_errno err) {
  1603. assert(err < (sizeof(http_strerror_tab)/sizeof(http_strerror_tab[0])));
  1604. return http_strerror_tab[err].description;
  1605. }
  1606. static enum http_host_state
  1607. http_parse_host_char(enum http_host_state s, const char ch) {
  1608. switch(s) {
  1609. case s_http_userinfo:
  1610. case s_http_userinfo_start:
  1611. if (ch == '@') {
  1612. return s_http_host_start;
  1613. }
  1614. if (IS_USERINFO_CHAR(ch)) {
  1615. return s_http_userinfo;
  1616. }
  1617. break;
  1618. case s_http_host_start:
  1619. if (ch == '[') {
  1620. return s_http_host_v6_start;
  1621. }
  1622. if (IS_HOST_CHAR(ch)) {
  1623. return s_http_host;
  1624. }
  1625. break;
  1626. case s_http_host:
  1627. if (IS_HOST_CHAR(ch)) {
  1628. return s_http_host;
  1629. }
  1630. /* FALLTHROUGH */
  1631. case s_http_host_v6_end:
  1632. if (ch == ':') {
  1633. return s_http_host_port_start;
  1634. }
  1635. break;
  1636. case s_http_host_v6:
  1637. if (ch == ']') {
  1638. return s_http_host_v6_end;
  1639. }
  1640. /* FALLTHROUGH */
  1641. case s_http_host_v6_start:
  1642. if (IS_HEX(ch) || ch == ':' || ch == '.') {
  1643. return s_http_host_v6;
  1644. }
  1645. break;
  1646. case s_http_host_port:
  1647. case s_http_host_port_start:
  1648. if (IS_NUM(ch)) {
  1649. return s_http_host_port;
  1650. }
  1651. break;
  1652. default:
  1653. break;
  1654. }
  1655. return s_http_host_dead;
  1656. }
  1657. static int
  1658. http_parse_host(const char * buf, struct http_parser_url *u, int found_at) {
  1659. enum http_host_state s;
  1660. const char *p;
  1661. size_t buflen = u->field_data[UF_HOST].off + u->field_data[UF_HOST].len;
  1662. u->field_data[UF_HOST].len = 0;
  1663. s = found_at ? s_http_userinfo_start : s_http_host_start;
  1664. for (p = buf + u->field_data[UF_HOST].off; p < buf + buflen; p++) {
  1665. enum http_host_state new_s = http_parse_host_char(s, *p);
  1666. if (new_s == s_http_host_dead) {
  1667. return 1;
  1668. }
  1669. switch(new_s) {
  1670. case s_http_host:
  1671. if (s != s_http_host) {
  1672. u->field_data[UF_HOST].off = p - buf;
  1673. }
  1674. u->field_data[UF_HOST].len++;
  1675. break;
  1676. case s_http_host_v6:
  1677. if (s != s_http_host_v6) {
  1678. u->field_data[UF_HOST].off = p - buf;
  1679. }
  1680. u->field_data[UF_HOST].len++;
  1681. break;
  1682. case s_http_host_port:
  1683. if (s != s_http_host_port) {
  1684. u->field_data[UF_PORT].off = p - buf;
  1685. u->field_data[UF_PORT].len = 0;
  1686. u->field_set |= (1 << UF_PORT);
  1687. }
  1688. u->field_data[UF_PORT].len++;
  1689. break;
  1690. case s_http_userinfo:
  1691. if (s != s_http_userinfo) {
  1692. u->field_data[UF_USERINFO].off = p - buf ;
  1693. u->field_data[UF_USERINFO].len = 0;
  1694. u->field_set |= (1 << UF_USERINFO);
  1695. }
  1696. u->field_data[UF_USERINFO].len++;
  1697. break;
  1698. default:
  1699. break;
  1700. }
  1701. s = new_s;
  1702. }
  1703. /* Make sure we don't end somewhere unexpected */
  1704. switch (s) {
  1705. case s_http_host_start:
  1706. case s_http_host_v6_start:
  1707. case s_http_host_v6:
  1708. case s_http_host_port_start:
  1709. case s_http_userinfo:
  1710. case s_http_userinfo_start:
  1711. return 1;
  1712. default:
  1713. break;
  1714. }
  1715. return 0;
  1716. }
  1717. int
  1718. http_parser_parse_url(const char *buf, size_t buflen, int is_connect,
  1719. struct http_parser_url *u)
  1720. {
  1721. enum state s;
  1722. const char *p;
  1723. enum http_parser_url_fields uf, old_uf;
  1724. int found_at = 0;
  1725. u->port = u->field_set = 0;
  1726. s = is_connect ? s_req_server_start : s_req_spaces_before_url;
  1727. uf = old_uf = UF_MAX;
  1728. for (p = buf; p < buf + buflen; p++) {
  1729. s = parse_url_char(s, *p);
  1730. /* Figure out the next field that we're operating on */
  1731. switch (s) {
  1732. case s_dead:
  1733. return 1;
  1734. /* Skip delimeters */
  1735. case s_req_schema_slash:
  1736. case s_req_schema_slash_slash:
  1737. case s_req_server_start:
  1738. case s_req_query_string_start:
  1739. case s_req_fragment_start:
  1740. continue;
  1741. case s_req_schema:
  1742. uf = UF_SCHEMA;
  1743. break;
  1744. case s_req_server_with_at:
  1745. found_at = 1;
  1746. /* FALLTROUGH */
  1747. case s_req_server:
  1748. uf = UF_HOST;
  1749. break;
  1750. case s_req_path:
  1751. uf = UF_PATH;
  1752. break;
  1753. case s_req_query_string:
  1754. uf = UF_QUERY;
  1755. break;
  1756. case s_req_fragment:
  1757. uf = UF_FRAGMENT;
  1758. break;
  1759. default:
  1760. assert(!"Unexpected state");
  1761. return 1;
  1762. }
  1763. /* Nothing's changed; soldier on */
  1764. if (uf == old_uf) {
  1765. u->field_data[uf].len++;
  1766. continue;
  1767. }
  1768. u->field_data[uf].off = p - buf;
  1769. u->field_data[uf].len = 1;
  1770. u->field_set |= (1 << uf);
  1771. old_uf = uf;
  1772. }
  1773. /* host must be present if there is a schema */
  1774. /* parsing http:///toto will fail */
  1775. if ((u->field_set & ((1 << UF_SCHEMA) | (1 << UF_HOST))) != 0) {
  1776. if (http_parse_host(buf, u, found_at) != 0) {
  1777. return 1;
  1778. }
  1779. }
  1780. /* CONNECT requests can only contain "hostname:port" */
  1781. if (is_connect && u->field_set != ((1 << UF_HOST)|(1 << UF_PORT))) {
  1782. return 1;
  1783. }
  1784. if (u->field_set & (1 << UF_PORT)) {
  1785. /* Don't bother with endp; we've already validated the string */
  1786. unsigned long v = strtoul(buf + u->field_data[UF_PORT].off, NULL, 10);
  1787. /* Ports have a max value of 2^16 */
  1788. if (v > 0xffff) {
  1789. return 1;
  1790. }
  1791. u->port = (uint16_t) v;
  1792. }
  1793. return 0;
  1794. }
  1795. void
  1796. http_parser_pause(http_parser *parser, int paused) {
  1797. /* Users should only be pausing/unpausing a parser that is not in an error
  1798. * state. In non-debug builds, there's not much that we can do about this
  1799. * other than ignore it.
  1800. */
  1801. if (HTTP_PARSER_ERRNO(parser) == HPE_OK ||
  1802. HTTP_PARSER_ERRNO(parser) == HPE_PAUSED) {
  1803. SET_ERRNO((paused) ? HPE_PAUSED : HPE_OK);
  1804. } else {
  1805. assert(0 && "Attempting to pause parser in error state");
  1806. }
  1807. }
  1808. int
  1809. http_body_is_final(const struct http_parser *parser) {
  1810. return parser->state == s_message_done;
  1811. }