http_parser.c 68 KB

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