http_parser.c 73 KB

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