You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

http_parser.c 62KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268
  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 "config.h"
  25. #include "http_parser.h"
  26. #include <assert.h>
  27. #ifndef ULLONG_MAX
  28. # define ULLONG_MAX ((uint64_t) -1) /* 2^64-1 */
  29. #endif
  30. #ifndef MIN
  31. # define MIN(a,b) ((a) < (b) ? (a) : (b))
  32. #endif
  33. #ifndef ARRAY_SIZE
  34. # define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
  35. #endif
  36. #ifndef BIT_AT
  37. # define BIT_AT(a, i) \
  38. (!!((unsigned int) (a)[(unsigned int) (i) >> 3] & \
  39. (1 << ((unsigned int) (i) & 7))))
  40. #endif
  41. #ifndef ELEM_AT
  42. # define ELEM_AT(a, i, v) ((unsigned int) (i) < ARRAY_SIZE(a) ? (a)[(i)] : (v))
  43. #endif
  44. #define SET_ERRNO(e) \
  45. do { \
  46. parser->http_errno = (e); \
  47. } while(0)
  48. /* Run the notify callback FOR, returning ER if it fails */
  49. #define CALLBACK_NOTIFY_(FOR, ER) \
  50. do { \
  51. assert(HTTP_PARSER_ERRNO(parser) == HPE_OK); \
  52. \
  53. if (settings->on_##FOR) { \
  54. if (0 != settings->on_##FOR(parser)) { \
  55. SET_ERRNO(HPE_CB_##FOR); \
  56. } \
  57. \
  58. /* We either errored above or got paused; get out */ \
  59. if (HTTP_PARSER_ERRNO(parser) != HPE_OK) { \
  60. return (ER); \
  61. } \
  62. } \
  63. } while (0)
  64. /* Run the notify callback FOR and consume the current byte */
  65. #define CALLBACK_NOTIFY(FOR) CALLBACK_NOTIFY_(FOR, p - data + 1)
  66. /* Run the notify callback FOR and don't consume the current byte */
  67. #define CALLBACK_NOTIFY_NOADVANCE(FOR) CALLBACK_NOTIFY_(FOR, p - data)
  68. /* Run data callback FOR with LEN bytes, returning ER if it fails */
  69. #define CALLBACK_DATA_(FOR, LEN, ER) \
  70. do { \
  71. assert(HTTP_PARSER_ERRNO(parser) == HPE_OK); \
  72. \
  73. if (FOR##_mark) { \
  74. if (settings->on_##FOR) { \
  75. if (0 != settings->on_##FOR(parser, FOR##_mark, (LEN))) { \
  76. SET_ERRNO(HPE_CB_##FOR); \
  77. } \
  78. \
  79. /* We either errored above or got paused; get out */ \
  80. if (HTTP_PARSER_ERRNO(parser) != HPE_OK) { \
  81. return (ER); \
  82. } \
  83. } \
  84. FOR##_mark = NULL; \
  85. } \
  86. } while (0)
  87. /* Run the data callback FOR and consume the current byte */
  88. #define CALLBACK_DATA(FOR) \
  89. CALLBACK_DATA_(FOR, p - FOR##_mark, p - data + 1)
  90. /* Run the data callback FOR and don't consume the current byte */
  91. #define CALLBACK_DATA_NOADVANCE(FOR) \
  92. CALLBACK_DATA_(FOR, p - FOR##_mark, p - data)
  93. /* Set the mark FOR; non-destructive if mark is already set */
  94. #define MARK(FOR) \
  95. do { \
  96. if (!FOR##_mark) { \
  97. FOR##_mark = p; \
  98. } \
  99. } while (0)
  100. #define PROXY_CONNECTION "proxy-connection"
  101. #define CONNECTION "connection"
  102. #define CONTENT_LENGTH "content-length"
  103. #define TRANSFER_ENCODING "transfer-encoding"
  104. #define UPGRADE "upgrade"
  105. #define CHUNKED "chunked"
  106. #define KEEP_ALIVE "keep-alive"
  107. #define CLOSE "close"
  108. enum rspamd_http_message_type { HTTP_REQUEST, HTTP_RESPONSE, HTTP_BOTH };
  109. static const char *method_strings[] =
  110. {
  111. #define XX(num, name, string) #string,
  112. HTTP_METHOD_MAP(XX)
  113. #undef XX
  114. };
  115. /* Tokens as defined by rfc 2616. Also lowercases them.
  116. * token = 1*<any CHAR except CTLs or separators>
  117. * separators = "(" | ")" | "<" | ">" | "@"
  118. * | "," | ";" | ":" | "\" | <">
  119. * | "/" | "[" | "]" | "?" | "="
  120. * | "{" | "}" | SP | HT
  121. */
  122. static const char tokens[256] = {
  123. /* 0 nul 1 soh 2 stx 3 etx 4 eot 5 enq 6 ack 7 bel */
  124. 0, 0, 0, 0, 0, 0, 0, 0,
  125. /* 8 bs 9 ht 10 nl 11 vt 12 np 13 cr 14 so 15 si */
  126. 0, 0, 0, 0, 0, 0, 0, 0,
  127. /* 16 dle 17 dc1 18 dc2 19 dc3 20 dc4 21 nak 22 syn 23 etb */
  128. 0, 0, 0, 0, 0, 0, 0, 0,
  129. /* 24 can 25 em 26 sub 27 esc 28 fs 29 gs 30 rs 31 us */
  130. 0, 0, 0, 0, 0, 0, 0, 0,
  131. /* 32 sp 33 ! 34 " 35 # 36 $ 37 % 38 & 39 ' */
  132. 0, '!', 0, '#', '$', '%', '&', '\'',
  133. /* 40 ( 41 ) 42 * 43 + 44 , 45 - 46 . 47 / */
  134. 0, 0, '*', '+', 0, '-', '.', 0,
  135. /* 48 0 49 1 50 2 51 3 52 4 53 5 54 6 55 7 */
  136. '0', '1', '2', '3', '4', '5', '6', '7',
  137. /* 56 8 57 9 58 : 59 ; 60 < 61 = 62 > 63 ? */
  138. '8', '9', 0, 0, 0, 0, 0, 0,
  139. /* 64 @ 65 A 66 B 67 C 68 D 69 E 70 F 71 G */
  140. 0, 'a', 'b', 'c', 'd', 'e', 'f', 'g',
  141. /* 72 H 73 I 74 J 75 K 76 L 77 M 78 N 79 O */
  142. 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
  143. /* 80 P 81 Q 82 R 83 S 84 T 85 U 86 V 87 W */
  144. 'p', 'q', 'r', 's', 't', 'u', 'v', 'w',
  145. /* 88 X 89 Y 90 Z 91 [ 92 \ 93 ] 94 ^ 95 _ */
  146. 'x', 'y', 'z', 0, 0, 0, '^', '_',
  147. /* 96 ` 97 a 98 b 99 c 100 d 101 e 102 f 103 g */
  148. '`', 'a', 'b', 'c', 'd', 'e', 'f', 'g',
  149. /* 104 h 105 i 106 j 107 k 108 l 109 m 110 n 111 o */
  150. 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
  151. /* 112 p 113 q 114 r 115 s 116 t 117 u 118 v 119 w */
  152. 'p', 'q', 'r', 's', 't', 'u', 'v', 'w',
  153. /* 120 x 121 y 122 z 123 { 124 | 125 } 126 ~ 127 del */
  154. 'x', 'y', 'z', 0, '|', 0, '~', 0 };
  155. static const int8_t unhex[256] =
  156. {-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1
  157. ,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1
  158. ,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1
  159. , 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,-1,-1,-1,-1,-1,-1
  160. ,-1,10,11,12,13,14,15,-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. ,-1,10,11,12,13,14,15,-1,-1,-1,-1,-1,-1,-1,-1,-1
  163. ,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1
  164. };
  165. #if HTTP_PARSER_STRICT
  166. # define T(v) 0
  167. #else
  168. # define T(v) v
  169. #endif
  170. static const uint8_t normal_url_char[32] = {
  171. /* 0 nul 1 soh 2 stx 3 etx 4 eot 5 enq 6 ack 7 bel */
  172. 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0,
  173. /* 8 bs 9 ht 10 nl 11 vt 12 np 13 cr 14 so 15 si */
  174. 0 | T(2) | 0 | 0 | T(16) | 0 | 0 | 0,
  175. /* 16 dle 17 dc1 18 dc2 19 dc3 20 dc4 21 nak 22 syn 23 etb */
  176. 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0,
  177. /* 24 can 25 em 26 sub 27 esc 28 fs 29 gs 30 rs 31 us */
  178. 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0,
  179. /* 32 sp 33 ! 34 " 35 # 36 $ 37 % 38 & 39 ' */
  180. 0 | 2 | 4 | 0 | 16 | 32 | 64 | 128,
  181. /* 40 ( 41 ) 42 * 43 + 44 , 45 - 46 . 47 / */
  182. 1 | 2 | 4 | 8 | 16 | 32 | 64 | 128,
  183. /* 48 0 49 1 50 2 51 3 52 4 53 5 54 6 55 7 */
  184. 1 | 2 | 4 | 8 | 16 | 32 | 64 | 128,
  185. /* 56 8 57 9 58 : 59 ; 60 < 61 = 62 > 63 ? */
  186. 1 | 2 | 4 | 8 | 16 | 32 | 64 | 0,
  187. /* 64 @ 65 A 66 B 67 C 68 D 69 E 70 F 71 G */
  188. 1 | 2 | 4 | 8 | 16 | 32 | 64 | 128,
  189. /* 72 H 73 I 74 J 75 K 76 L 77 M 78 N 79 O */
  190. 1 | 2 | 4 | 8 | 16 | 32 | 64 | 128,
  191. /* 80 P 81 Q 82 R 83 S 84 T 85 U 86 V 87 W */
  192. 1 | 2 | 4 | 8 | 16 | 32 | 64 | 128,
  193. /* 88 X 89 Y 90 Z 91 [ 92 \ 93 ] 94 ^ 95 _ */
  194. 1 | 2 | 4 | 8 | 16 | 32 | 64 | 128,
  195. /* 96 ` 97 a 98 b 99 c 100 d 101 e 102 f 103 g */
  196. 1 | 2 | 4 | 8 | 16 | 32 | 64 | 128,
  197. /* 104 h 105 i 106 j 107 k 108 l 109 m 110 n 111 o */
  198. 1 | 2 | 4 | 8 | 16 | 32 | 64 | 128,
  199. /* 112 p 113 q 114 r 115 s 116 t 117 u 118 v 119 w */
  200. 1 | 2 | 4 | 8 | 16 | 32 | 64 | 128,
  201. /* 120 x 121 y 122 z 123 { 124 | 125 } 126 ~ 127 del */
  202. 1 | 2 | 4 | 8 | 16 | 32 | 64 | 0, };
  203. #undef T
  204. enum state
  205. { s_dead = 1 /* important that this is > 0 */
  206. , s_start_req_or_res
  207. , s_res_or_resp_H
  208. , s_start_res
  209. , s_res_H
  210. , s_res_HT
  211. , s_res_HTT
  212. , s_res_HTTP
  213. , s_res_first_http_major
  214. , s_res_http_major
  215. , s_res_first_http_minor
  216. , s_res_http_minor
  217. , s_res_first_status_code
  218. , s_res_status_code
  219. , s_res_status_start
  220. , s_res_status
  221. , s_res_line_almost_done
  222. , s_start_req
  223. , s_req_method
  224. , s_req_spaces_before_url
  225. , s_req_schema
  226. , s_req_schema_slash
  227. , s_req_schema_slash_slash
  228. , s_req_server_start
  229. , s_req_server
  230. , s_req_server_with_at
  231. , s_req_path
  232. , s_req_query_string_start
  233. , s_req_query_string
  234. , s_req_fragment_start
  235. , s_req_fragment
  236. , s_req_http_start
  237. , s_req_http_H
  238. , s_req_http_HT
  239. , s_req_http_HTT
  240. , s_req_http_HTTP
  241. , s_req_first_http_major
  242. , s_req_http_major
  243. , s_req_first_http_minor
  244. , s_req_http_minor
  245. , s_req_spamc_start
  246. , s_req_spamc
  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. const char *status_mark = 0;
  491. /* We're in an error state. Don't bother doing anything. */
  492. if (HTTP_PARSER_ERRNO(parser) != HPE_OK) {
  493. return 0;
  494. }
  495. if (len == 0) {
  496. switch (parser->state) {
  497. case s_body_identity_eof:
  498. /* Use of CALLBACK_NOTIFY() here would erroneously return 1 byte read if
  499. * we got paused.
  500. */
  501. CALLBACK_NOTIFY_NOADVANCE(message_complete);
  502. return 0;
  503. case s_dead:
  504. case s_start_req_or_res:
  505. case s_start_res:
  506. case s_start_req:
  507. return 0;
  508. default:
  509. SET_ERRNO(HPE_INVALID_EOF_STATE);
  510. return 1;
  511. }
  512. }
  513. if (parser->state == s_header_field)
  514. header_field_mark = data;
  515. if (parser->state == s_header_value)
  516. header_value_mark = data;
  517. switch (parser->state) {
  518. case s_req_path:
  519. case s_req_schema:
  520. case s_req_schema_slash:
  521. case s_req_schema_slash_slash:
  522. case s_req_server_start:
  523. case s_req_server:
  524. case s_req_server_with_at:
  525. case s_req_query_string_start:
  526. case s_req_query_string:
  527. case s_req_fragment_start:
  528. case s_req_fragment:
  529. url_mark = data;
  530. break;
  531. case s_res_status:
  532. status_mark = data;
  533. break;
  534. }
  535. for (p=data; p != data + len; p++) {
  536. ch = *p;
  537. if (PARSING_HEADER(parser->state)) {
  538. ++parser->nread;
  539. /* Don't allow the total size of the HTTP headers (including the status
  540. * line) to exceed HTTP_MAX_HEADER_SIZE. This check is here to protect
  541. * embedders against denial-of-service attacks where the attacker feeds
  542. * us a never-ending header that the embedder keeps buffering.
  543. *
  544. * This check is arguably the responsibility of embedders but we're doing
  545. * it on the embedder's behalf because most won't bother and this way we
  546. * make the web a little safer. HTTP_MAX_HEADER_SIZE is still far bigger
  547. * than any reasonable request or response so this should never affect
  548. * day-to-day operation.
  549. */
  550. if (parser->nread > HTTP_MAX_HEADER_SIZE) {
  551. SET_ERRNO(HPE_HEADER_OVERFLOW);
  552. goto error;
  553. }
  554. }
  555. reexecute_byte:
  556. switch (parser->state) {
  557. case s_dead:
  558. /* this state is used after a 'Connection: close' message
  559. * the parser will error out if it reads another message
  560. */
  561. if (ch == CR || ch == LF)
  562. break;
  563. SET_ERRNO(HPE_CLOSED_CONNECTION);
  564. goto error;
  565. case s_start_req_or_res:
  566. {
  567. if (ch == CR || ch == LF)
  568. break;
  569. parser->flags = 0;
  570. parser->content_length = ULLONG_MAX;
  571. if (ch == 'H') {
  572. parser->state = s_res_or_resp_H;
  573. CALLBACK_NOTIFY(message_begin);
  574. } else {
  575. parser->type = HTTP_REQUEST;
  576. parser->state = s_start_req;
  577. goto reexecute_byte;
  578. }
  579. break;
  580. }
  581. case s_res_or_resp_H:
  582. if (ch == 'T') {
  583. parser->type = HTTP_RESPONSE;
  584. parser->state = s_res_HT;
  585. } else {
  586. if (ch != 'E') {
  587. SET_ERRNO(HPE_INVALID_CONSTANT);
  588. goto error;
  589. }
  590. parser->type = HTTP_REQUEST;
  591. parser->method = HTTP_HEAD;
  592. parser->index = 2;
  593. parser->state = s_req_method;
  594. }
  595. break;
  596. case s_start_res:
  597. {
  598. parser->flags = 0;
  599. parser->content_length = ULLONG_MAX;
  600. switch (ch) {
  601. case 'H':
  602. parser->state = s_res_H;
  603. break;
  604. case CR:
  605. case LF:
  606. break;
  607. default:
  608. SET_ERRNO(HPE_INVALID_CONSTANT);
  609. goto error;
  610. }
  611. CALLBACK_NOTIFY(message_begin);
  612. break;
  613. }
  614. case s_res_H:
  615. STRICT_CHECK(ch != 'T');
  616. parser->state = s_res_HT;
  617. break;
  618. case s_res_HT:
  619. STRICT_CHECK(ch != 'T');
  620. parser->state = s_res_HTT;
  621. break;
  622. case s_res_HTT:
  623. STRICT_CHECK(ch != 'P');
  624. parser->state = s_res_HTTP;
  625. break;
  626. case s_res_HTTP:
  627. STRICT_CHECK(ch != '/');
  628. parser->state = s_res_first_http_major;
  629. break;
  630. case s_res_first_http_major:
  631. if (ch < '0' || ch > '9') {
  632. SET_ERRNO(HPE_INVALID_VERSION);
  633. goto error;
  634. }
  635. parser->http_major = ch - '0';
  636. parser->state = s_res_http_major;
  637. break;
  638. /* major HTTP version or dot */
  639. case s_res_http_major:
  640. {
  641. if (ch == '.') {
  642. parser->state = s_res_first_http_minor;
  643. break;
  644. }
  645. if (!IS_NUM(ch)) {
  646. SET_ERRNO(HPE_INVALID_VERSION);
  647. goto error;
  648. }
  649. parser->http_major *= 10;
  650. parser->http_major += ch - '0';
  651. if (parser->http_major > 999) {
  652. SET_ERRNO(HPE_INVALID_VERSION);
  653. goto error;
  654. }
  655. break;
  656. }
  657. /* first digit of minor HTTP version */
  658. case s_res_first_http_minor:
  659. if (!IS_NUM(ch)) {
  660. SET_ERRNO(HPE_INVALID_VERSION);
  661. goto error;
  662. }
  663. parser->http_minor = ch - '0';
  664. parser->state = s_res_http_minor;
  665. break;
  666. /* minor HTTP version or end of request line */
  667. case s_res_http_minor:
  668. {
  669. if (ch == ' ') {
  670. parser->state = s_res_first_status_code;
  671. break;
  672. }
  673. if (!IS_NUM(ch)) {
  674. SET_ERRNO(HPE_INVALID_VERSION);
  675. goto error;
  676. }
  677. parser->http_minor *= 10;
  678. parser->http_minor += ch - '0';
  679. if (parser->http_minor > 999) {
  680. SET_ERRNO(HPE_INVALID_VERSION);
  681. goto error;
  682. }
  683. break;
  684. }
  685. case s_res_first_status_code:
  686. {
  687. if (!IS_NUM(ch)) {
  688. if (ch == ' ') {
  689. break;
  690. }
  691. SET_ERRNO(HPE_INVALID_STATUS);
  692. goto error;
  693. }
  694. parser->status_code = ch - '0';
  695. parser->state = s_res_status_code;
  696. break;
  697. }
  698. case s_res_status_code:
  699. {
  700. if (!IS_NUM(ch)) {
  701. switch (ch) {
  702. case ' ':
  703. parser->state = s_res_status_start;
  704. break;
  705. case CR:
  706. parser->state = s_res_line_almost_done;
  707. break;
  708. case LF:
  709. parser->state = s_header_field_start;
  710. break;
  711. default:
  712. SET_ERRNO(HPE_INVALID_STATUS);
  713. goto error;
  714. }
  715. break;
  716. }
  717. parser->status_code *= 10;
  718. parser->status_code += ch - '0';
  719. if (parser->status_code > 999) {
  720. SET_ERRNO(HPE_INVALID_STATUS);
  721. goto error;
  722. }
  723. break;
  724. }
  725. case s_res_status_start:
  726. {
  727. if (ch == CR) {
  728. parser->state = s_res_line_almost_done;
  729. break;
  730. }
  731. if (ch == LF) {
  732. parser->state = s_header_field_start;
  733. break;
  734. }
  735. MARK(status);
  736. parser->state = s_res_status;
  737. parser->index = 0;
  738. break;
  739. }
  740. case s_res_status:
  741. if (ch == CR) {
  742. parser->state = s_res_line_almost_done;
  743. CALLBACK_DATA(status);
  744. break;
  745. }
  746. if (ch == LF) {
  747. parser->state = s_header_field_start;
  748. CALLBACK_DATA(status);
  749. break;
  750. }
  751. break;
  752. case s_res_line_almost_done:
  753. STRICT_CHECK(ch != LF);
  754. parser->state = s_header_field_start;
  755. break;
  756. case s_start_req:
  757. {
  758. if (ch == CR || ch == LF)
  759. break;
  760. parser->flags = 0;
  761. parser->content_length = ULLONG_MAX;
  762. if (!IS_ALPHA(ch)) {
  763. SET_ERRNO(HPE_INVALID_METHOD);
  764. goto error;
  765. }
  766. parser->method = (enum http_method) 0;
  767. parser->index = 1;
  768. switch (ch) {
  769. case 'C': parser->method = HTTP_CONNECT; /* or COPY, CHECKOUT */ break;
  770. case 'D': parser->method = HTTP_DELETE; break;
  771. case 'G': parser->method = HTTP_GET; break;
  772. case 'H': parser->method = HTTP_HEAD; break;
  773. case 'L': parser->method = HTTP_LOCK; break;
  774. case 'M': parser->method = HTTP_MKCOL; /* or MOVE, MKACTIVITY, MERGE, M-SEARCH */ break;
  775. case 'N': parser->method = HTTP_NOTIFY; break;
  776. case 'O': parser->method = HTTP_OPTIONS; break;
  777. case 'P': parser->method = HTTP_POST;
  778. /* or PROPFIND|PROPPATCH|PUT|PATCH|PURGE */
  779. break;
  780. case 'R': parser->method = HTTP_REPORT; break;
  781. case 'S': parser->method = HTTP_SUBSCRIBE; /* or SEARCH or SYMBOLS */ break;
  782. case 'T': parser->method = HTTP_TRACE; break;
  783. case 'U': parser->method = HTTP_UNLOCK; /* or UNSUBSCRIBE */ break;
  784. default:
  785. SET_ERRNO(HPE_INVALID_METHOD);
  786. goto error;
  787. }
  788. parser->state = s_req_method;
  789. CALLBACK_NOTIFY(message_begin);
  790. break;
  791. }
  792. case s_req_method:
  793. {
  794. const char *matcher;
  795. if (ch == '\0') {
  796. SET_ERRNO(HPE_INVALID_METHOD);
  797. goto error;
  798. }
  799. matcher = method_strings[parser->method];
  800. if (ch == ' ' && matcher[parser->index] == '\0') {
  801. if (parser->method != HTTP_SYMBOLS && parser->method != HTTP_CHECK) {
  802. parser->state = s_req_spaces_before_url;
  803. }
  804. else {
  805. parser->state = s_req_spamc_start;
  806. }
  807. } else if (ch == matcher[parser->index]) {
  808. ; /* nada */
  809. } else if (parser->method == HTTP_CONNECT) {
  810. if (parser->index == 1 && ch == 'H') {
  811. /* XXX: CHECKOUT has been removed */
  812. parser->method = HTTP_CHECK;
  813. } else if (parser->index == 2 && ch == 'P') {
  814. parser->method = HTTP_COPY;
  815. } else {
  816. SET_ERRNO(HPE_INVALID_METHOD);
  817. goto error;
  818. }
  819. } else if (parser->method == HTTP_MKCOL) {
  820. if (parser->index == 1 && ch == 'O') {
  821. parser->method = HTTP_MOVE;
  822. } else if (parser->index == 1 && ch == 'E') {
  823. parser->method = HTTP_MERGE;
  824. } else if (parser->index == 1 && ch == '-') {
  825. parser->method = HTTP_MSEARCH;
  826. } else if (parser->index == 2 && ch == 'A') {
  827. parser->method = HTTP_MKACTIVITY;
  828. } else {
  829. SET_ERRNO(HPE_INVALID_METHOD);
  830. goto error;
  831. }
  832. } else if (parser->method == HTTP_SUBSCRIBE) {
  833. if (parser->index == 1 && ch == 'E') {
  834. parser->method = HTTP_SEARCH;
  835. } else if (ch == 'Y') {
  836. parser->method = HTTP_SYMBOLS;
  837. }
  838. else {
  839. SET_ERRNO(HPE_INVALID_METHOD);
  840. goto error;
  841. }
  842. } else if (parser->index == 1 && parser->method == HTTP_POST) {
  843. if (ch == 'R') {
  844. parser->method = HTTP_PROPFIND; /* or HTTP_PROPPATCH */
  845. } else if (ch == 'U') {
  846. parser->method = HTTP_PUT; /* or HTTP_PURGE */
  847. } else if (ch == 'A') {
  848. parser->method = HTTP_PATCH;
  849. } else {
  850. SET_ERRNO(HPE_INVALID_METHOD);
  851. goto error;
  852. }
  853. } else if (parser->index == 2) {
  854. if (parser->method == HTTP_PUT) {
  855. if (ch == 'R') {
  856. parser->method = HTTP_PURGE;
  857. } else {
  858. SET_ERRNO(HPE_INVALID_METHOD);
  859. goto error;
  860. }
  861. } else if (parser->method == HTTP_UNLOCK) {
  862. if (ch == 'S') {
  863. parser->method = HTTP_UNSUBSCRIBE;
  864. } else {
  865. SET_ERRNO(HPE_INVALID_METHOD);
  866. goto error;
  867. }
  868. } else {
  869. SET_ERRNO(HPE_INVALID_METHOD);
  870. goto error;
  871. }
  872. } else if (parser->index == 4 && parser->method == HTTP_PROPFIND && ch == 'P') {
  873. parser->method = HTTP_PROPPATCH;
  874. } else {
  875. SET_ERRNO(HPE_INVALID_METHOD);
  876. goto error;
  877. }
  878. ++parser->index;
  879. break;
  880. }
  881. case s_req_spaces_before_url:
  882. {
  883. if (ch == ' ') break;
  884. MARK(url);
  885. if (parser->method == HTTP_CONNECT) {
  886. parser->state = s_req_server_start;
  887. }
  888. parser->state = parse_url_char((enum state)parser->state, ch);
  889. if (parser->state == s_dead) {
  890. SET_ERRNO(HPE_INVALID_URL);
  891. goto error;
  892. }
  893. break;
  894. }
  895. case s_req_schema:
  896. case s_req_schema_slash:
  897. case s_req_schema_slash_slash:
  898. case s_req_server_start:
  899. {
  900. switch (ch) {
  901. /* No whitespace allowed here */
  902. case ' ':
  903. case CR:
  904. case LF:
  905. SET_ERRNO(HPE_INVALID_URL);
  906. goto error;
  907. default:
  908. parser->state = parse_url_char((enum state)parser->state, ch);
  909. if (parser->state == s_dead) {
  910. SET_ERRNO(HPE_INVALID_URL);
  911. goto error;
  912. }
  913. }
  914. break;
  915. }
  916. case s_req_server:
  917. case s_req_server_with_at:
  918. case s_req_path:
  919. case s_req_query_string_start:
  920. case s_req_query_string:
  921. case s_req_fragment_start:
  922. case s_req_fragment:
  923. {
  924. switch (ch) {
  925. case ' ':
  926. parser->state = s_req_http_start;
  927. CALLBACK_DATA(url);
  928. break;
  929. case CR:
  930. case LF:
  931. parser->http_major = 0;
  932. parser->http_minor = 9;
  933. parser->state = (ch == CR) ?
  934. s_req_line_almost_done :
  935. s_header_field_start;
  936. CALLBACK_DATA(url);
  937. break;
  938. default:
  939. parser->state = parse_url_char((enum state)parser->state, ch);
  940. if (parser->state == s_dead) {
  941. SET_ERRNO(HPE_INVALID_URL);
  942. goto error;
  943. }
  944. }
  945. break;
  946. }
  947. case s_req_http_start:
  948. switch (ch) {
  949. case 'H':
  950. parser->state = s_req_http_H;
  951. break;
  952. case ' ':
  953. break;
  954. default:
  955. SET_ERRNO(HPE_INVALID_CONSTANT);
  956. goto error;
  957. }
  958. break;
  959. case s_req_http_H:
  960. STRICT_CHECK(ch != 'T');
  961. parser->state = s_req_http_HT;
  962. break;
  963. case s_req_http_HT:
  964. STRICT_CHECK(ch != 'T');
  965. parser->state = s_req_http_HTT;
  966. break;
  967. case s_req_http_HTT:
  968. STRICT_CHECK(ch != 'P');
  969. parser->state = s_req_http_HTTP;
  970. break;
  971. case s_req_http_HTTP:
  972. STRICT_CHECK(ch != '/');
  973. parser->state = s_req_first_http_major;
  974. break;
  975. /* first digit of major HTTP version */
  976. case s_req_first_http_major:
  977. if (ch < '1' || ch > '9') {
  978. SET_ERRNO(HPE_INVALID_VERSION);
  979. goto error;
  980. }
  981. parser->http_major = ch - '0';
  982. parser->state = s_req_http_major;
  983. break;
  984. /* major HTTP version or dot */
  985. case s_req_http_major:
  986. {
  987. if (ch == '.') {
  988. parser->state = s_req_first_http_minor;
  989. break;
  990. }
  991. if (!IS_NUM(ch)) {
  992. SET_ERRNO(HPE_INVALID_VERSION);
  993. goto error;
  994. }
  995. parser->http_major *= 10;
  996. parser->http_major += ch - '0';
  997. if (parser->http_major > 999) {
  998. SET_ERRNO(HPE_INVALID_VERSION);
  999. goto error;
  1000. }
  1001. break;
  1002. }
  1003. /* first digit of minor HTTP version */
  1004. case s_req_first_http_minor:
  1005. if (!IS_NUM(ch)) {
  1006. SET_ERRNO(HPE_INVALID_VERSION);
  1007. goto error;
  1008. }
  1009. parser->http_minor = ch - '0';
  1010. parser->state = s_req_http_minor;
  1011. break;
  1012. /* minor HTTP version or end of request line */
  1013. case s_req_http_minor:
  1014. {
  1015. if (ch == CR) {
  1016. parser->state = s_req_line_almost_done;
  1017. break;
  1018. }
  1019. if (ch == LF) {
  1020. parser->state = s_header_field_start;
  1021. break;
  1022. }
  1023. /* XXX allow spaces after digit? */
  1024. if (!IS_NUM(ch)) {
  1025. SET_ERRNO(HPE_INVALID_VERSION);
  1026. goto error;
  1027. }
  1028. parser->http_minor *= 10;
  1029. parser->http_minor += ch - '0';
  1030. if (parser->http_minor > 999) {
  1031. SET_ERRNO(HPE_INVALID_VERSION);
  1032. goto error;
  1033. }
  1034. break;
  1035. }
  1036. case s_req_spamc_start: {
  1037. if (ch == 'S') {
  1038. parser->flags |= F_SPAMC;
  1039. parser->state = s_req_spamc;
  1040. }
  1041. else if (ch == 'R') {
  1042. parser->state = s_req_spamc;
  1043. }
  1044. else if (ch != ' ') {
  1045. SET_ERRNO(HPE_INVALID_CONSTANT);
  1046. goto error;
  1047. }
  1048. break;
  1049. }
  1050. case s_req_spamc:
  1051. {
  1052. if (ch == CR) {
  1053. parser->state = s_req_line_almost_done;
  1054. }
  1055. else if (ch == LF) {
  1056. parser->state = s_header_field_start;
  1057. }
  1058. break;
  1059. }
  1060. /* end of request line */
  1061. case s_req_line_almost_done:
  1062. {
  1063. if (ch != LF) {
  1064. SET_ERRNO(HPE_LF_EXPECTED);
  1065. goto error;
  1066. }
  1067. parser->state = s_header_field_start;
  1068. break;
  1069. }
  1070. case s_header_field_start:
  1071. {
  1072. if (ch == CR) {
  1073. parser->state = s_headers_almost_done;
  1074. break;
  1075. }
  1076. if (ch == LF) {
  1077. /* they might be just sending \n instead of \r\n so this would be
  1078. * the second \n to denote the end of headers*/
  1079. parser->state = s_headers_almost_done;
  1080. goto reexecute_byte;
  1081. }
  1082. c = TOKEN(ch);
  1083. if (!c) {
  1084. SET_ERRNO(HPE_INVALID_HEADER_TOKEN);
  1085. goto error;
  1086. }
  1087. MARK(header_field);
  1088. parser->index = 0;
  1089. parser->state = s_header_field;
  1090. switch (c) {
  1091. case 'c':
  1092. parser->header_state = h_C;
  1093. break;
  1094. case 'p':
  1095. parser->header_state = h_matching_proxy_connection;
  1096. break;
  1097. case 't':
  1098. parser->header_state = h_matching_transfer_encoding;
  1099. break;
  1100. case 'u':
  1101. parser->header_state = h_matching_upgrade;
  1102. break;
  1103. default:
  1104. parser->header_state = h_general;
  1105. break;
  1106. }
  1107. break;
  1108. }
  1109. case s_header_field:
  1110. {
  1111. c = TOKEN(ch);
  1112. if (c) {
  1113. switch (parser->header_state) {
  1114. case h_general:
  1115. break;
  1116. case h_C:
  1117. parser->index++;
  1118. parser->header_state = (c == 'o' ? h_CO : h_general);
  1119. break;
  1120. case h_CO:
  1121. parser->index++;
  1122. parser->header_state = (c == 'n' ? h_CON : h_general);
  1123. break;
  1124. case h_CON:
  1125. parser->index++;
  1126. switch (c) {
  1127. case 'n':
  1128. parser->header_state = h_matching_connection;
  1129. break;
  1130. case 't':
  1131. parser->header_state = h_matching_content_length;
  1132. break;
  1133. default:
  1134. parser->header_state = h_general;
  1135. break;
  1136. }
  1137. break;
  1138. /* connection */
  1139. case h_matching_connection:
  1140. parser->index++;
  1141. if (parser->index > sizeof(CONNECTION)-1
  1142. || c != CONNECTION[parser->index]) {
  1143. parser->header_state = h_general;
  1144. } else if (parser->index == sizeof(CONNECTION)-2) {
  1145. parser->header_state = h_connection;
  1146. }
  1147. break;
  1148. /* proxy-connection */
  1149. case h_matching_proxy_connection:
  1150. parser->index++;
  1151. if (parser->index > sizeof(PROXY_CONNECTION)-1
  1152. || c != PROXY_CONNECTION[parser->index]) {
  1153. parser->header_state = h_general;
  1154. } else if (parser->index == sizeof(PROXY_CONNECTION)-2) {
  1155. parser->header_state = h_connection;
  1156. }
  1157. break;
  1158. /* content-length */
  1159. case h_matching_content_length:
  1160. parser->index++;
  1161. if (parser->index > sizeof(CONTENT_LENGTH)-1
  1162. || c != CONTENT_LENGTH[parser->index]) {
  1163. parser->header_state = h_general;
  1164. } else if (parser->index == sizeof(CONTENT_LENGTH)-2) {
  1165. parser->header_state = h_content_length;
  1166. }
  1167. break;
  1168. /* transfer-encoding */
  1169. case h_matching_transfer_encoding:
  1170. parser->index++;
  1171. if (parser->index > sizeof(TRANSFER_ENCODING)-1
  1172. || c != TRANSFER_ENCODING[parser->index]) {
  1173. parser->header_state = h_general;
  1174. } else if (parser->index == sizeof(TRANSFER_ENCODING)-2) {
  1175. parser->header_state = h_transfer_encoding;
  1176. }
  1177. break;
  1178. /* upgrade */
  1179. case h_matching_upgrade:
  1180. parser->index++;
  1181. if (parser->index > sizeof(UPGRADE)-1
  1182. || c != UPGRADE[parser->index]) {
  1183. parser->header_state = h_general;
  1184. } else if (parser->index == sizeof(UPGRADE)-2) {
  1185. parser->header_state = h_upgrade;
  1186. }
  1187. break;
  1188. case h_connection:
  1189. case h_content_length:
  1190. case h_transfer_encoding:
  1191. case h_upgrade:
  1192. if (ch != ' ') parser->header_state = h_general;
  1193. break;
  1194. default:
  1195. assert(0 && "Unknown header_state");
  1196. break;
  1197. }
  1198. break;
  1199. }
  1200. if (ch == ':') {
  1201. parser->state = s_header_value_start;
  1202. CALLBACK_DATA(header_field);
  1203. break;
  1204. }
  1205. if (ch == CR) {
  1206. parser->state = s_header_almost_done;
  1207. CALLBACK_DATA(header_field);
  1208. break;
  1209. }
  1210. if (ch == LF) {
  1211. parser->state = s_header_field_start;
  1212. CALLBACK_DATA(header_field);
  1213. break;
  1214. }
  1215. SET_ERRNO(HPE_INVALID_HEADER_TOKEN);
  1216. goto error;
  1217. }
  1218. case s_header_value_start:
  1219. {
  1220. if (ch == ' ' || ch == '\t') break;
  1221. MARK(header_value);
  1222. parser->state = s_header_value;
  1223. parser->index = 0;
  1224. if (ch == CR) {
  1225. parser->header_state = h_general;
  1226. parser->state = s_header_almost_done;
  1227. CALLBACK_DATA(header_value);
  1228. break;
  1229. }
  1230. if (ch == LF) {
  1231. parser->state = s_header_field_start;
  1232. CALLBACK_DATA(header_value);
  1233. break;
  1234. }
  1235. c = LOWER(ch);
  1236. switch (parser->header_state) {
  1237. case h_upgrade:
  1238. parser->flags |= F_UPGRADE;
  1239. parser->header_state = h_general;
  1240. break;
  1241. case h_transfer_encoding:
  1242. /* looking for 'Transfer-Encoding: chunked' */
  1243. if ('c' == c) {
  1244. parser->header_state = h_matching_transfer_encoding_chunked;
  1245. } else {
  1246. parser->header_state = h_general;
  1247. }
  1248. break;
  1249. case h_content_length:
  1250. if (!IS_NUM(ch)) {
  1251. SET_ERRNO(HPE_INVALID_CONTENT_LENGTH);
  1252. goto error;
  1253. }
  1254. parser->content_length = ch - '0';
  1255. break;
  1256. case h_connection:
  1257. /* looking for 'Connection: keep-alive' */
  1258. if (c == 'k') {
  1259. parser->header_state = h_matching_connection_keep_alive;
  1260. /* looking for 'Connection: close' */
  1261. } else if (c == 'c') {
  1262. parser->header_state = h_matching_connection_close;
  1263. } else {
  1264. parser->header_state = h_general;
  1265. }
  1266. break;
  1267. default:
  1268. parser->header_state = h_general;
  1269. break;
  1270. }
  1271. break;
  1272. }
  1273. case s_header_value:
  1274. {
  1275. if (ch == CR) {
  1276. parser->state = s_header_almost_done;
  1277. CALLBACK_DATA(header_value);
  1278. break;
  1279. }
  1280. if (ch == LF) {
  1281. parser->state = s_header_almost_done;
  1282. CALLBACK_DATA_NOADVANCE(header_value);
  1283. goto reexecute_byte;
  1284. }
  1285. c = LOWER(ch);
  1286. switch (parser->header_state) {
  1287. case h_general:
  1288. break;
  1289. case h_connection:
  1290. case h_transfer_encoding:
  1291. assert(0 && "Shouldn't get here.");
  1292. break;
  1293. case h_content_length:
  1294. {
  1295. uint64_t t;
  1296. if (ch == ' ') break;
  1297. if (!IS_NUM(ch)) {
  1298. SET_ERRNO(HPE_INVALID_CONTENT_LENGTH);
  1299. goto error;
  1300. }
  1301. t = parser->content_length;
  1302. t *= 10;
  1303. t += ch - '0';
  1304. /* Overflow? */
  1305. if (t < parser->content_length || t == ULLONG_MAX) {
  1306. SET_ERRNO(HPE_INVALID_CONTENT_LENGTH);
  1307. goto error;
  1308. }
  1309. parser->content_length = t;
  1310. break;
  1311. }
  1312. /* Transfer-Encoding: chunked */
  1313. case h_matching_transfer_encoding_chunked:
  1314. parser->index++;
  1315. if (parser->index > sizeof(CHUNKED)-1
  1316. || c != CHUNKED[parser->index]) {
  1317. parser->header_state = h_general;
  1318. } else if (parser->index == sizeof(CHUNKED)-2) {
  1319. parser->header_state = h_transfer_encoding_chunked;
  1320. }
  1321. break;
  1322. /* looking for 'Connection: keep-alive' */
  1323. case h_matching_connection_keep_alive:
  1324. parser->index++;
  1325. if (parser->index > sizeof(KEEP_ALIVE)-1
  1326. || c != KEEP_ALIVE[parser->index]) {
  1327. parser->header_state = h_general;
  1328. } else if (parser->index == sizeof(KEEP_ALIVE)-2) {
  1329. parser->header_state = h_connection_keep_alive;
  1330. }
  1331. break;
  1332. /* looking for 'Connection: close' */
  1333. case h_matching_connection_close:
  1334. parser->index++;
  1335. if (parser->index > sizeof(CLOSE)-1 || c != CLOSE[parser->index]) {
  1336. parser->header_state = h_general;
  1337. } else if (parser->index == sizeof(CLOSE)-2) {
  1338. parser->header_state = h_connection_close;
  1339. }
  1340. break;
  1341. case h_transfer_encoding_chunked:
  1342. case h_connection_keep_alive:
  1343. case h_connection_close:
  1344. if (ch != ' ') parser->header_state = h_general;
  1345. break;
  1346. default:
  1347. parser->state = s_header_value;
  1348. parser->header_state = h_general;
  1349. break;
  1350. }
  1351. break;
  1352. }
  1353. case s_header_almost_done:
  1354. {
  1355. STRICT_CHECK(ch != LF);
  1356. parser->state = s_header_value_lws;
  1357. switch (parser->header_state) {
  1358. case h_connection_keep_alive:
  1359. parser->flags |= F_CONNECTION_KEEP_ALIVE;
  1360. break;
  1361. case h_connection_close:
  1362. /* XXX: not needed for rspamd parser->flags |= F_CONNECTION_CLOSE; */
  1363. break;
  1364. case h_transfer_encoding_chunked:
  1365. parser->flags |= F_CHUNKED;
  1366. break;
  1367. default:
  1368. break;
  1369. }
  1370. break;
  1371. }
  1372. case s_header_value_lws:
  1373. {
  1374. if (ch == ' ' || ch == '\t')
  1375. parser->state = s_header_value_start;
  1376. else
  1377. {
  1378. parser->state = s_header_field_start;
  1379. goto reexecute_byte;
  1380. }
  1381. break;
  1382. }
  1383. case s_headers_almost_done:
  1384. {
  1385. STRICT_CHECK(ch != LF);
  1386. if (parser->flags & F_TRAILING) {
  1387. /* End of a chunked request */
  1388. parser->state = NEW_MESSAGE();
  1389. CALLBACK_NOTIFY(message_complete);
  1390. break;
  1391. }
  1392. parser->state = s_headers_done;
  1393. /* Set this here so that on_headers_complete() callbacks can see it */
  1394. parser->upgrade =
  1395. (parser->flags & F_UPGRADE || parser->method == HTTP_CONNECT);
  1396. /* Here we call the headers_complete callback. This is somewhat
  1397. * different than other callbacks because if the user returns 1, we
  1398. * will interpret that as saying that this message has no body. This
  1399. * is needed for the annoying case of receiving a response to a HEAD
  1400. * request.
  1401. *
  1402. * We'd like to use CALLBACK_NOTIFY_NOADVANCE() here but we cannot, so
  1403. * we have to simulate it by handling a change in errno below.
  1404. */
  1405. if (settings->on_headers_complete) {
  1406. switch (settings->on_headers_complete(parser)) {
  1407. case 0:
  1408. break;
  1409. case 1:
  1410. parser->flags |= F_SKIPBODY;
  1411. break;
  1412. default:
  1413. SET_ERRNO(HPE_CB_headers_complete);
  1414. return p - data; /* Error */
  1415. }
  1416. }
  1417. if (HTTP_PARSER_ERRNO(parser) != HPE_OK) {
  1418. return p - data;
  1419. }
  1420. goto reexecute_byte;
  1421. }
  1422. case s_headers_done:
  1423. {
  1424. STRICT_CHECK(ch != LF);
  1425. parser->nread = 0;
  1426. /* Exit, the rest of the connect is in a different protocol. */
  1427. if (parser->upgrade) {
  1428. parser->state = NEW_MESSAGE();
  1429. CALLBACK_NOTIFY(message_complete);
  1430. return (p - data) + 1;
  1431. }
  1432. if (parser->flags & F_SKIPBODY) {
  1433. parser->state = NEW_MESSAGE();
  1434. CALLBACK_NOTIFY(message_complete);
  1435. } else if (parser->flags & F_CHUNKED) {
  1436. /* chunked encoding - ignore Content-Length header */
  1437. parser->state = s_chunk_size_start;
  1438. } else {
  1439. if (parser->content_length == 0) {
  1440. /* Content-Length header given but zero: Content-Length: 0\r\n */
  1441. parser->state = NEW_MESSAGE();
  1442. CALLBACK_NOTIFY(message_complete);
  1443. } else if (parser->content_length != ULLONG_MAX) {
  1444. /* Content-Length header given and non-zero */
  1445. parser->state = s_body_identity;
  1446. } else {
  1447. if (parser->type == HTTP_REQUEST ||
  1448. !http_message_needs_eof(parser)) {
  1449. /* Assume content-length 0 - read the next */
  1450. parser->state = NEW_MESSAGE();
  1451. CALLBACK_NOTIFY(message_complete);
  1452. } else {
  1453. /* Read body until EOF */
  1454. parser->state = s_body_identity_eof;
  1455. }
  1456. }
  1457. }
  1458. break;
  1459. }
  1460. case s_body_identity:
  1461. {
  1462. uint64_t to_read = MIN(parser->content_length,
  1463. (uint64_t) ((data + len) - p));
  1464. assert(parser->content_length != 0
  1465. && parser->content_length != ULLONG_MAX);
  1466. /* The difference between advancing content_length and p is because
  1467. * the latter will automatically advance on the next loop iteration.
  1468. * Further, if content_length ends up at 0, we want to see the last
  1469. * byte again for our message complete callback.
  1470. */
  1471. MARK(body);
  1472. parser->content_length -= to_read;
  1473. p += to_read - 1;
  1474. if (parser->content_length == 0) {
  1475. parser->state = s_message_done;
  1476. /* Mimic CALLBACK_DATA_NOADVANCE() but with one extra byte.
  1477. *
  1478. * The alternative to doing this is to wait for the next byte to
  1479. * trigger the data callback, just as in every other case. The
  1480. * problem with this is that this makes it difficult for the test
  1481. * harness to distinguish between complete-on-EOF and
  1482. * complete-on-length. It's not clear that this distinction is
  1483. * important for applications, but let's keep it for now.
  1484. */
  1485. CALLBACK_DATA_(body, p - body_mark + 1, p - data);
  1486. goto reexecute_byte;
  1487. }
  1488. break;
  1489. }
  1490. /* read until EOF */
  1491. case s_body_identity_eof:
  1492. MARK(body);
  1493. p = data + len - 1;
  1494. break;
  1495. case s_message_done:
  1496. parser->state = NEW_MESSAGE();
  1497. CALLBACK_NOTIFY(message_complete);
  1498. break;
  1499. case s_chunk_size_start:
  1500. {
  1501. assert(parser->nread == 1);
  1502. assert(parser->flags & F_CHUNKED);
  1503. unhex_val = unhex[(unsigned char)ch];
  1504. if (unhex_val == -1) {
  1505. SET_ERRNO(HPE_INVALID_CHUNK_SIZE);
  1506. goto error;
  1507. }
  1508. parser->content_length = unhex_val;
  1509. parser->state = s_chunk_size;
  1510. break;
  1511. }
  1512. case s_chunk_size:
  1513. {
  1514. uint64_t t;
  1515. assert(parser->flags & F_CHUNKED);
  1516. if (ch == CR) {
  1517. parser->state = s_chunk_size_almost_done;
  1518. break;
  1519. }
  1520. unhex_val = unhex[(unsigned char)ch];
  1521. if (unhex_val == -1) {
  1522. if (ch == ';' || ch == ' ') {
  1523. parser->state = s_chunk_parameters;
  1524. break;
  1525. }
  1526. SET_ERRNO(HPE_INVALID_CHUNK_SIZE);
  1527. goto error;
  1528. }
  1529. t = parser->content_length;
  1530. t *= 16;
  1531. t += unhex_val;
  1532. /* Overflow? */
  1533. if (t < parser->content_length || t == ULLONG_MAX) {
  1534. SET_ERRNO(HPE_INVALID_CONTENT_LENGTH);
  1535. goto error;
  1536. }
  1537. parser->content_length = t;
  1538. break;
  1539. }
  1540. case s_chunk_parameters:
  1541. {
  1542. assert(parser->flags & F_CHUNKED);
  1543. /* just ignore this shit. TODO check for overflow */
  1544. if (ch == CR) {
  1545. parser->state = s_chunk_size_almost_done;
  1546. break;
  1547. }
  1548. break;
  1549. }
  1550. case s_chunk_size_almost_done:
  1551. {
  1552. assert(parser->flags & F_CHUNKED);
  1553. STRICT_CHECK(ch != LF);
  1554. parser->nread = 0;
  1555. if (parser->content_length == 0) {
  1556. parser->flags |= F_TRAILING;
  1557. parser->state = s_header_field_start;
  1558. } else {
  1559. parser->state = s_chunk_data;
  1560. }
  1561. break;
  1562. }
  1563. case s_chunk_data:
  1564. {
  1565. uint64_t to_read = MIN(parser->content_length,
  1566. (uint64_t) ((data + len) - p));
  1567. assert(parser->flags & F_CHUNKED);
  1568. assert(parser->content_length != 0
  1569. && parser->content_length != ULLONG_MAX);
  1570. /* See the explanation in s_body_identity for why the content
  1571. * length and data pointers are managed this way.
  1572. */
  1573. MARK(body);
  1574. parser->content_length -= to_read;
  1575. p += to_read - 1;
  1576. if (parser->content_length == 0) {
  1577. parser->state = s_chunk_data_almost_done;
  1578. }
  1579. break;
  1580. }
  1581. case s_chunk_data_almost_done:
  1582. assert(parser->flags & F_CHUNKED);
  1583. assert(parser->content_length == 0);
  1584. STRICT_CHECK(ch != CR);
  1585. parser->state = s_chunk_data_done;
  1586. CALLBACK_DATA(body);
  1587. break;
  1588. case s_chunk_data_done:
  1589. assert(parser->flags & F_CHUNKED);
  1590. STRICT_CHECK(ch != LF);
  1591. parser->nread = 0;
  1592. parser->state = s_chunk_size_start;
  1593. break;
  1594. default:
  1595. assert(0 && "unhandled state");
  1596. SET_ERRNO(HPE_INVALID_INTERNAL_STATE);
  1597. goto error;
  1598. }
  1599. }
  1600. /* Run callbacks for any marks that we have leftover after we ran our of
  1601. * bytes. There should be at most one of these set, so it's OK to invoke
  1602. * them in series (unset marks will not result in callbacks).
  1603. *
  1604. * We use the NOADVANCE() variety of callbacks here because 'p' has already
  1605. * overflowed 'data' and this allows us to correct for the off-by-one that
  1606. * we'd otherwise have (since CALLBACK_DATA() is meant to be run with a 'p'
  1607. * value that's in-bounds).
  1608. */
  1609. assert(((header_field_mark ? 1 : 0) +
  1610. (header_value_mark ? 1 : 0) +
  1611. (url_mark ? 1 : 0) +
  1612. (body_mark ? 1 : 0) +
  1613. (status_mark ? 1 : 0)) <= 1);
  1614. CALLBACK_DATA_NOADVANCE(header_field);
  1615. CALLBACK_DATA_NOADVANCE(header_value);
  1616. CALLBACK_DATA_NOADVANCE(url);
  1617. CALLBACK_DATA_NOADVANCE(body);
  1618. CALLBACK_DATA_NOADVANCE(status);
  1619. return len;
  1620. error:
  1621. if (HTTP_PARSER_ERRNO(parser) == HPE_OK) {
  1622. SET_ERRNO(HPE_UNKNOWN);
  1623. }
  1624. return (p - data);
  1625. }
  1626. /* Does the parser need to see an EOF to find the end of the message? */
  1627. int
  1628. http_message_needs_eof (const http_parser *parser)
  1629. {
  1630. if (parser->type == HTTP_REQUEST) {
  1631. return 0;
  1632. }
  1633. /* See RFC 2616 section 4.4 */
  1634. if (parser->status_code / 100 == 1 || /* 1xx e.g. Continue */
  1635. parser->status_code == 204 || /* No Content */
  1636. parser->status_code == 304 || /* Not Modified */
  1637. parser->flags & F_SKIPBODY) { /* response to a HEAD request */
  1638. return 0;
  1639. }
  1640. if ((parser->flags & F_CHUNKED) || parser->content_length != ULLONG_MAX) {
  1641. return 0;
  1642. }
  1643. return 1;
  1644. }
  1645. int
  1646. http_should_keep_alive (const http_parser *parser)
  1647. {
  1648. if (parser->http_major > 0 && parser->http_minor > 0) {
  1649. /* HTTP/1.1 */
  1650. if (!(parser->flags & F_CONNECTION_KEEP_ALIVE)) {
  1651. return 0;
  1652. }
  1653. } else {
  1654. /* HTTP/1.0 or earlier */
  1655. if (!(parser->flags & F_CONNECTION_KEEP_ALIVE)) {
  1656. return 0;
  1657. }
  1658. }
  1659. return !http_message_needs_eof(parser);
  1660. }
  1661. const char *
  1662. http_method_str (enum http_method m)
  1663. {
  1664. return ELEM_AT(method_strings, m, "<unknown>");
  1665. }
  1666. void
  1667. http_parser_init (http_parser *parser, int t)
  1668. {
  1669. void *data = parser->data; /* preserve application data */
  1670. memset(parser, 0, sizeof(*parser));
  1671. parser->data = data;
  1672. parser->type = t;
  1673. parser->state = (t == HTTP_REQUEST ? s_start_req : (t == HTTP_RESPONSE ? s_start_res : s_start_req_or_res));
  1674. parser->http_errno = HPE_OK;
  1675. }
  1676. const char *
  1677. http_errno_name(enum http_errno err) {
  1678. assert(err < (sizeof(http_strerror_tab)/sizeof(http_strerror_tab[0])));
  1679. return http_strerror_tab[err].name;
  1680. }
  1681. const char *
  1682. http_errno_description(enum http_errno err) {
  1683. assert(err < (sizeof(http_strerror_tab)/sizeof(http_strerror_tab[0])));
  1684. return http_strerror_tab[err].description;
  1685. }
  1686. static enum http_host_state
  1687. http_parse_host_char(enum http_host_state s, const char ch) {
  1688. switch(s) {
  1689. case s_http_userinfo:
  1690. case s_http_userinfo_start:
  1691. if (ch == '@') {
  1692. return s_http_host_start;
  1693. }
  1694. if (IS_USERINFO_CHAR(ch)) {
  1695. return s_http_userinfo;
  1696. }
  1697. break;
  1698. case s_http_host_start:
  1699. if (ch == '[') {
  1700. return s_http_host_v6_start;
  1701. }
  1702. if (IS_HOST_CHAR(ch)) {
  1703. return s_http_host;
  1704. }
  1705. break;
  1706. case s_http_host:
  1707. if (IS_HOST_CHAR(ch)) {
  1708. return s_http_host;
  1709. }
  1710. /* FALLTHROUGH */
  1711. case s_http_host_v6_end:
  1712. if (ch == ':') {
  1713. return s_http_host_port_start;
  1714. }
  1715. break;
  1716. case s_http_host_v6:
  1717. if (ch == ']') {
  1718. return s_http_host_v6_end;
  1719. }
  1720. /* FALLTHROUGH */
  1721. case s_http_host_v6_start:
  1722. if (IS_HEX(ch) || ch == ':' || ch == '.') {
  1723. return s_http_host_v6;
  1724. }
  1725. break;
  1726. case s_http_host_port:
  1727. case s_http_host_port_start:
  1728. if (IS_NUM(ch)) {
  1729. return s_http_host_port;
  1730. }
  1731. break;
  1732. default:
  1733. break;
  1734. }
  1735. return s_http_host_dead;
  1736. }
  1737. static int
  1738. http_parse_host(const char * buf, struct http_parser_url *u, int found_at) {
  1739. enum http_host_state s;
  1740. const char *p;
  1741. size_t buflen = u->field_data[UF_HOST].off + u->field_data[UF_HOST].len;
  1742. u->field_data[UF_HOST].len = 0;
  1743. s = found_at ? s_http_userinfo_start : s_http_host_start;
  1744. for (p = buf + u->field_data[UF_HOST].off; p < buf + buflen; p++) {
  1745. enum http_host_state new_s = http_parse_host_char(s, *p);
  1746. if (new_s == s_http_host_dead) {
  1747. return 1;
  1748. }
  1749. switch(new_s) {
  1750. case s_http_host:
  1751. if (s != s_http_host) {
  1752. u->field_data[UF_HOST].off = p - buf;
  1753. }
  1754. u->field_data[UF_HOST].len++;
  1755. break;
  1756. case s_http_host_v6:
  1757. if (s != s_http_host_v6) {
  1758. u->field_data[UF_HOST].off = p - buf;
  1759. }
  1760. u->field_data[UF_HOST].len++;
  1761. break;
  1762. case s_http_host_port:
  1763. if (s != s_http_host_port) {
  1764. u->field_data[UF_PORT].off = p - buf;
  1765. u->field_data[UF_PORT].len = 0;
  1766. u->field_set |= (1 << UF_PORT);
  1767. }
  1768. u->field_data[UF_PORT].len++;
  1769. break;
  1770. case s_http_userinfo:
  1771. if (s != s_http_userinfo) {
  1772. u->field_data[UF_USERINFO].off = p - buf ;
  1773. u->field_data[UF_USERINFO].len = 0;
  1774. u->field_set |= (1 << UF_USERINFO);
  1775. }
  1776. u->field_data[UF_USERINFO].len++;
  1777. break;
  1778. default:
  1779. break;
  1780. }
  1781. s = new_s;
  1782. }
  1783. /* Make sure we don't end somewhere unexpected */
  1784. switch (s) {
  1785. case s_http_host_start:
  1786. case s_http_host_v6_start:
  1787. case s_http_host_v6:
  1788. case s_http_host_port_start:
  1789. case s_http_userinfo:
  1790. case s_http_userinfo_start:
  1791. return 1;
  1792. default:
  1793. break;
  1794. }
  1795. return 0;
  1796. }
  1797. int
  1798. http_parser_parse_url(const char *buf, size_t buflen, int is_connect,
  1799. struct http_parser_url *u)
  1800. {
  1801. enum state s;
  1802. const char *p;
  1803. enum http_parser_url_fields uf, old_uf;
  1804. int found_at = 0;
  1805. u->port = u->field_set = 0;
  1806. s = is_connect ? s_req_server_start : s_req_spaces_before_url;
  1807. uf = old_uf = UF_MAX;
  1808. for (p = buf; p < buf + buflen; p++) {
  1809. s = parse_url_char(s, *p);
  1810. /* Figure out the next field that we're operating on */
  1811. switch (s) {
  1812. case s_dead:
  1813. return 1;
  1814. /* Skip delimiters */
  1815. case s_req_schema_slash:
  1816. case s_req_schema_slash_slash:
  1817. case s_req_server_start:
  1818. case s_req_query_string_start:
  1819. case s_req_fragment_start:
  1820. continue;
  1821. case s_req_schema:
  1822. uf = UF_SCHEMA;
  1823. break;
  1824. case s_req_server_with_at:
  1825. found_at = 1;
  1826. /* FALLTROUGH */
  1827. case s_req_server:
  1828. uf = UF_HOST;
  1829. break;
  1830. case s_req_path:
  1831. uf = UF_PATH;
  1832. break;
  1833. case s_req_query_string:
  1834. uf = UF_QUERY;
  1835. break;
  1836. case s_req_fragment:
  1837. uf = UF_FRAGMENT;
  1838. break;
  1839. default:
  1840. assert(!"Unexpected state");
  1841. return 1;
  1842. }
  1843. /* Nothing's changed; soldier on */
  1844. if (uf == old_uf) {
  1845. u->field_data[uf].len++;
  1846. continue;
  1847. }
  1848. u->field_data[uf].off = p - buf;
  1849. u->field_data[uf].len = 1;
  1850. u->field_set |= (1 << uf);
  1851. old_uf = uf;
  1852. }
  1853. /* host must be present if there is a schema */
  1854. /* parsing http:///toto will fail */
  1855. if ((u->field_set & ((1 << UF_SCHEMA) | (1 << UF_HOST))) != 0) {
  1856. if (http_parse_host(buf, u, found_at) != 0) {
  1857. return 1;
  1858. }
  1859. }
  1860. /* CONNECT requests can only contain "hostname:port" */
  1861. if (is_connect && u->field_set != ((1 << UF_HOST)|(1 << UF_PORT))) {
  1862. return 1;
  1863. }
  1864. if (u->field_set & (1 << UF_PORT)) {
  1865. /* Don't bother with endp; we've already validated the string */
  1866. unsigned long v = strtoul(buf + u->field_data[UF_PORT].off, NULL, 10);
  1867. /* Ports have a max value of 2^16 */
  1868. if (v > 0xffff) {
  1869. return 1;
  1870. }
  1871. u->port = (uint16_t) v;
  1872. }
  1873. return 0;
  1874. }
  1875. void
  1876. http_parser_pause(http_parser *parser, int paused) {
  1877. /* Users should only be pausing/unpausing a parser that is not in an error
  1878. * state. In non-debug builds, there's not much that we can do about this
  1879. * other than ignore it.
  1880. */
  1881. if (HTTP_PARSER_ERRNO(parser) == HPE_OK ||
  1882. HTTP_PARSER_ERRNO(parser) == HPE_PAUSED) {
  1883. SET_ERRNO((paused) ? HPE_PAUSED : HPE_OK);
  1884. } else {
  1885. assert(0 && "Attempting to pause parser in error state");
  1886. }
  1887. }
  1888. int
  1889. http_body_is_final(const struct http_parser *parser) {
  1890. return parser->state == s_message_done;
  1891. }
  1892. unsigned long
  1893. http_parser_version(void) {
  1894. return HTTP_PARSER_VERSION_MAJOR * 0x10000 |
  1895. HTTP_PARSER_VERSION_MINOR * 0x00100 |
  1896. HTTP_PARSER_VERSION_PATCH * 0x00001;
  1897. }