http_parser.c 74 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568
  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->extra_flags = 0;
  622. parser->content_length = ULLONG_MAX;
  623. if (ch == 'H') {
  624. UPDATE_STATE(s_res_or_resp_H);
  625. CALLBACK_NOTIFY(message_begin);
  626. } else {
  627. parser->type = HTTP_REQUEST;
  628. UPDATE_STATE(s_start_req);
  629. REEXECUTE();
  630. }
  631. break;
  632. }
  633. case s_res_or_resp_H:
  634. if (ch == 'T') {
  635. parser->type = HTTP_RESPONSE;
  636. UPDATE_STATE(s_res_HT);
  637. } else {
  638. if (UNLIKELY(ch != 'E')) {
  639. SET_ERRNO(HPE_INVALID_CONSTANT);
  640. goto error;
  641. }
  642. parser->type = HTTP_REQUEST;
  643. parser->method = HTTP_HEAD;
  644. parser->index = 2;
  645. UPDATE_STATE(s_req_method);
  646. }
  647. break;
  648. case s_start_res:
  649. {
  650. if (ch == CR || ch == LF)
  651. break;
  652. parser->flags = 0;
  653. parser->extra_flags = 0;
  654. parser->content_length = ULLONG_MAX;
  655. if (ch == 'H') {
  656. UPDATE_STATE(s_res_H);
  657. } else {
  658. SET_ERRNO(HPE_INVALID_CONSTANT);
  659. goto error;
  660. }
  661. CALLBACK_NOTIFY(message_begin);
  662. break;
  663. }
  664. case s_res_H:
  665. STRICT_CHECK(ch != 'T');
  666. UPDATE_STATE(s_res_HT);
  667. break;
  668. case s_res_HT:
  669. STRICT_CHECK(ch != 'T');
  670. UPDATE_STATE(s_res_HTT);
  671. break;
  672. case s_res_HTT:
  673. STRICT_CHECK(ch != 'P');
  674. UPDATE_STATE(s_res_HTTP);
  675. break;
  676. case s_res_HTTP:
  677. STRICT_CHECK(ch != '/');
  678. UPDATE_STATE(s_res_http_major);
  679. break;
  680. case s_res_http_major:
  681. if (UNLIKELY(!IS_NUM(ch))) {
  682. SET_ERRNO(HPE_INVALID_VERSION);
  683. goto error;
  684. }
  685. parser->http_major = ch - '0';
  686. UPDATE_STATE(s_res_http_dot);
  687. break;
  688. case s_res_http_dot:
  689. {
  690. if (UNLIKELY(ch != '.')) {
  691. SET_ERRNO(HPE_INVALID_VERSION);
  692. goto error;
  693. }
  694. UPDATE_STATE(s_res_http_minor);
  695. break;
  696. }
  697. case s_res_http_minor:
  698. if (UNLIKELY(!IS_NUM(ch))) {
  699. SET_ERRNO(HPE_INVALID_VERSION);
  700. goto error;
  701. }
  702. parser->http_minor = ch - '0';
  703. UPDATE_STATE(s_res_http_end);
  704. break;
  705. case s_res_http_end:
  706. {
  707. if (UNLIKELY(ch != ' ')) {
  708. SET_ERRNO(HPE_INVALID_VERSION);
  709. goto error;
  710. }
  711. UPDATE_STATE(s_res_first_status_code);
  712. break;
  713. }
  714. case s_res_first_status_code:
  715. {
  716. if (!IS_NUM(ch)) {
  717. if (ch == ' ') {
  718. break;
  719. }
  720. SET_ERRNO(HPE_INVALID_STATUS);
  721. goto error;
  722. }
  723. parser->status_code = ch - '0';
  724. UPDATE_STATE(s_res_status_code);
  725. break;
  726. }
  727. case s_res_status_code:
  728. {
  729. if (!IS_NUM(ch)) {
  730. switch (ch) {
  731. case ' ':
  732. UPDATE_STATE(s_res_status_start);
  733. break;
  734. case CR:
  735. case LF:
  736. UPDATE_STATE(s_res_status_start);
  737. REEXECUTE();
  738. break;
  739. default:
  740. SET_ERRNO(HPE_INVALID_STATUS);
  741. goto error;
  742. }
  743. break;
  744. }
  745. parser->status_code *= 10;
  746. parser->status_code += ch - '0';
  747. if (UNLIKELY(parser->status_code > 999)) {
  748. SET_ERRNO(HPE_INVALID_STATUS);
  749. goto error;
  750. }
  751. break;
  752. }
  753. case s_res_status_start:
  754. {
  755. MARK(status);
  756. UPDATE_STATE(s_res_status);
  757. parser->index = 0;
  758. if (ch == CR || ch == LF)
  759. REEXECUTE();
  760. break;
  761. }
  762. case s_res_status:
  763. if (ch == CR) {
  764. UPDATE_STATE(s_res_line_almost_done);
  765. CALLBACK_DATA(status);
  766. break;
  767. }
  768. if (ch == LF) {
  769. UPDATE_STATE(s_header_field_start);
  770. CALLBACK_DATA(status);
  771. break;
  772. }
  773. break;
  774. case s_res_line_almost_done:
  775. STRICT_CHECK(ch != LF);
  776. UPDATE_STATE(s_header_field_start);
  777. break;
  778. case s_start_req:
  779. {
  780. if (ch == CR || ch == LF)
  781. break;
  782. parser->flags = 0;
  783. parser->extra_flags = 0;
  784. parser->content_length = ULLONG_MAX;
  785. if (UNLIKELY(!IS_ALPHA(ch))) {
  786. SET_ERRNO(HPE_INVALID_METHOD);
  787. goto error;
  788. }
  789. parser->method = (enum http_method) 0;
  790. parser->index = 1;
  791. switch (ch) {
  792. case 'A': parser->method = HTTP_ACL; break;
  793. case 'B': parser->method = HTTP_BIND; break;
  794. case 'C': parser->method = HTTP_CONNECT; /* or COPY, CHECKOUT */ break;
  795. case 'D': parser->method = HTTP_DELETE; break;
  796. case 'G': parser->method = HTTP_GET; break;
  797. case 'H': parser->method = HTTP_HEAD; break;
  798. case 'L': parser->method = HTTP_LOCK; /* or LINK */ break;
  799. case 'M': parser->method = HTTP_MKCOL; /* or MOVE, MKACTIVITY, MERGE, M-SEARCH, MKCALENDAR */ break;
  800. case 'N': parser->method = HTTP_NOTIFY; break;
  801. case 'O': parser->method = HTTP_OPTIONS; break;
  802. case 'P': parser->method = HTTP_POST;
  803. /* or PROPFIND|PROPPATCH|PUT|PATCH|PURGE */
  804. break;
  805. case 'R': parser->method = HTTP_REPORT; /* or REBIND */ break;
  806. case 'S': parser->method = HTTP_SUBSCRIBE; /* or SEARCH, SOURCE */ break;
  807. case 'T': parser->method = HTTP_TRACE; break;
  808. case 'U': parser->method = HTTP_UNLOCK; /* or UNSUBSCRIBE, UNBIND, UNLINK */ break;
  809. default:
  810. SET_ERRNO(HPE_INVALID_METHOD);
  811. goto error;
  812. }
  813. UPDATE_STATE(s_req_method);
  814. CALLBACK_NOTIFY(message_begin);
  815. break;
  816. }
  817. case s_req_method:
  818. {
  819. const char *matcher;
  820. if (UNLIKELY(ch == '\0')) {
  821. SET_ERRNO(HPE_INVALID_METHOD);
  822. goto error;
  823. }
  824. matcher = method_strings[parser->method];
  825. if (ch == ' ' && matcher[parser->index] == '\0') {
  826. UPDATE_STATE(s_req_spaces_before_url);
  827. } else if (ch == matcher[parser->index]) {
  828. ; /* nada */
  829. } else if ((ch >= 'A' && ch <= 'Z') || ch == '-') {
  830. switch (parser->method << 16 | parser->index << 8 | ch) {
  831. #define XX(meth, pos, ch, new_meth) \
  832. case (HTTP_##meth << 16 | pos << 8 | ch): \
  833. parser->method = HTTP_##new_meth; break;
  834. XX(POST, 1, 'U', PUT)
  835. XX(POST, 1, 'A', PATCH)
  836. XX(POST, 1, 'R', PROPFIND)
  837. XX(PUT, 2, 'R', PURGE)
  838. XX(CONNECT, 1, 'H', CHECKOUT)
  839. XX(CONNECT, 2, 'P', COPY)
  840. XX(MKCOL, 1, 'O', MOVE)
  841. XX(MKCOL, 1, 'E', MERGE)
  842. XX(MKCOL, 1, '-', MSEARCH)
  843. XX(MKCOL, 2, 'A', MKACTIVITY)
  844. XX(MKCOL, 3, 'A', MKCALENDAR)
  845. XX(SUBSCRIBE, 1, 'E', SEARCH)
  846. XX(SUBSCRIBE, 1, 'O', SOURCE)
  847. XX(REPORT, 2, 'B', REBIND)
  848. XX(PROPFIND, 4, 'P', PROPPATCH)
  849. XX(LOCK, 1, 'I', LINK)
  850. XX(UNLOCK, 2, 'S', UNSUBSCRIBE)
  851. XX(UNLOCK, 2, 'B', UNBIND)
  852. XX(UNLOCK, 3, 'I', UNLINK)
  853. #undef XX
  854. default:
  855. SET_ERRNO(HPE_INVALID_METHOD);
  856. goto error;
  857. }
  858. } else {
  859. SET_ERRNO(HPE_INVALID_METHOD);
  860. goto error;
  861. }
  862. ++parser->index;
  863. break;
  864. }
  865. case s_req_spaces_before_url:
  866. {
  867. if (ch == ' ') break;
  868. MARK(url);
  869. if (parser->method == HTTP_CONNECT) {
  870. UPDATE_STATE(s_req_server_start);
  871. }
  872. UPDATE_STATE(parse_url_char(CURRENT_STATE(), ch));
  873. if (UNLIKELY(CURRENT_STATE() == s_dead)) {
  874. SET_ERRNO(HPE_INVALID_URL);
  875. goto error;
  876. }
  877. break;
  878. }
  879. case s_req_schema:
  880. case s_req_schema_slash:
  881. case s_req_schema_slash_slash:
  882. case s_req_server_start:
  883. {
  884. switch (ch) {
  885. /* No whitespace allowed here */
  886. case ' ':
  887. case CR:
  888. case LF:
  889. SET_ERRNO(HPE_INVALID_URL);
  890. goto error;
  891. default:
  892. UPDATE_STATE(parse_url_char(CURRENT_STATE(), ch));
  893. if (UNLIKELY(CURRENT_STATE() == s_dead)) {
  894. SET_ERRNO(HPE_INVALID_URL);
  895. goto error;
  896. }
  897. }
  898. break;
  899. }
  900. case s_req_server:
  901. case s_req_server_with_at:
  902. case s_req_path:
  903. case s_req_query_string_start:
  904. case s_req_query_string:
  905. case s_req_fragment_start:
  906. case s_req_fragment:
  907. {
  908. switch (ch) {
  909. case ' ':
  910. UPDATE_STATE(s_req_http_start);
  911. CALLBACK_DATA(url);
  912. break;
  913. case CR:
  914. case LF:
  915. parser->http_major = 0;
  916. parser->http_minor = 9;
  917. UPDATE_STATE((ch == CR) ?
  918. s_req_line_almost_done :
  919. s_header_field_start);
  920. CALLBACK_DATA(url);
  921. break;
  922. default:
  923. UPDATE_STATE(parse_url_char(CURRENT_STATE(), ch));
  924. if (UNLIKELY(CURRENT_STATE() == s_dead)) {
  925. SET_ERRNO(HPE_INVALID_URL);
  926. goto error;
  927. }
  928. }
  929. break;
  930. }
  931. case s_req_http_start:
  932. switch (ch) {
  933. case ' ':
  934. break;
  935. case 'H':
  936. UPDATE_STATE(s_req_http_H);
  937. break;
  938. case 'I':
  939. if (parser->method == HTTP_SOURCE) {
  940. UPDATE_STATE(s_req_http_I);
  941. break;
  942. }
  943. /* fall through */
  944. default:
  945. SET_ERRNO(HPE_INVALID_CONSTANT);
  946. goto error;
  947. }
  948. break;
  949. case s_req_http_H:
  950. STRICT_CHECK(ch != 'T');
  951. UPDATE_STATE(s_req_http_HT);
  952. break;
  953. case s_req_http_HT:
  954. STRICT_CHECK(ch != 'T');
  955. UPDATE_STATE(s_req_http_HTT);
  956. break;
  957. case s_req_http_HTT:
  958. STRICT_CHECK(ch != 'P');
  959. UPDATE_STATE(s_req_http_HTTP);
  960. break;
  961. case s_req_http_I:
  962. STRICT_CHECK(ch != 'C');
  963. UPDATE_STATE(s_req_http_IC);
  964. break;
  965. case s_req_http_IC:
  966. STRICT_CHECK(ch != 'E');
  967. UPDATE_STATE(s_req_http_HTTP); /* Treat "ICE" as "HTTP". */
  968. break;
  969. case s_req_http_HTTP:
  970. STRICT_CHECK(ch != '/');
  971. UPDATE_STATE(s_req_http_major);
  972. break;
  973. case s_req_http_major:
  974. if (UNLIKELY(!IS_NUM(ch))) {
  975. SET_ERRNO(HPE_INVALID_VERSION);
  976. goto error;
  977. }
  978. parser->http_major = ch - '0';
  979. UPDATE_STATE(s_req_http_dot);
  980. break;
  981. case s_req_http_dot:
  982. {
  983. if (UNLIKELY(ch != '.')) {
  984. SET_ERRNO(HPE_INVALID_VERSION);
  985. goto error;
  986. }
  987. UPDATE_STATE(s_req_http_minor);
  988. break;
  989. }
  990. case s_req_http_minor:
  991. if (UNLIKELY(!IS_NUM(ch))) {
  992. SET_ERRNO(HPE_INVALID_VERSION);
  993. goto error;
  994. }
  995. parser->http_minor = ch - '0';
  996. UPDATE_STATE(s_req_http_end);
  997. break;
  998. case s_req_http_end:
  999. {
  1000. if (ch == CR) {
  1001. UPDATE_STATE(s_req_line_almost_done);
  1002. break;
  1003. }
  1004. if (ch == LF) {
  1005. UPDATE_STATE(s_header_field_start);
  1006. break;
  1007. }
  1008. SET_ERRNO(HPE_INVALID_VERSION);
  1009. goto error;
  1010. break;
  1011. }
  1012. /* end of request line */
  1013. case s_req_line_almost_done:
  1014. {
  1015. if (UNLIKELY(ch != LF)) {
  1016. SET_ERRNO(HPE_LF_EXPECTED);
  1017. goto error;
  1018. }
  1019. UPDATE_STATE(s_header_field_start);
  1020. break;
  1021. }
  1022. case s_header_field_start:
  1023. {
  1024. if (ch == CR) {
  1025. UPDATE_STATE(s_headers_almost_done);
  1026. break;
  1027. }
  1028. if (ch == LF) {
  1029. /* they might be just sending \n instead of \r\n so this would be
  1030. * the second \n to denote the end of headers*/
  1031. UPDATE_STATE(s_headers_almost_done);
  1032. REEXECUTE();
  1033. }
  1034. c = TOKEN(ch);
  1035. if (UNLIKELY(!c)) {
  1036. SET_ERRNO(HPE_INVALID_HEADER_TOKEN);
  1037. goto error;
  1038. }
  1039. MARK(header_field);
  1040. parser->index = 0;
  1041. UPDATE_STATE(s_header_field);
  1042. switch (c) {
  1043. case 'c':
  1044. parser->header_state = h_C;
  1045. break;
  1046. case 'p':
  1047. parser->header_state = h_matching_proxy_connection;
  1048. break;
  1049. case 't':
  1050. parser->header_state = h_matching_transfer_encoding;
  1051. break;
  1052. case 'u':
  1053. parser->header_state = h_matching_upgrade;
  1054. break;
  1055. default:
  1056. parser->header_state = h_general;
  1057. break;
  1058. }
  1059. break;
  1060. }
  1061. case s_header_field:
  1062. {
  1063. const char* start = p;
  1064. for (; p != data + len; p++) {
  1065. ch = *p;
  1066. c = TOKEN(ch);
  1067. if (!c)
  1068. break;
  1069. switch (parser->header_state) {
  1070. case h_general: {
  1071. size_t left = data + len - p;
  1072. const char* pe = p + MIN(left, max_header_size);
  1073. while (p+1 < pe && TOKEN(p[1])) {
  1074. p++;
  1075. }
  1076. break;
  1077. }
  1078. case h_C:
  1079. parser->index++;
  1080. parser->header_state = (c == 'o' ? h_CO : h_general);
  1081. break;
  1082. case h_CO:
  1083. parser->index++;
  1084. parser->header_state = (c == 'n' ? h_CON : h_general);
  1085. break;
  1086. case h_CON:
  1087. parser->index++;
  1088. switch (c) {
  1089. case 'n':
  1090. parser->header_state = h_matching_connection;
  1091. break;
  1092. case 't':
  1093. parser->header_state = h_matching_content_length;
  1094. break;
  1095. default:
  1096. parser->header_state = h_general;
  1097. break;
  1098. }
  1099. break;
  1100. /* connection */
  1101. case h_matching_connection:
  1102. parser->index++;
  1103. if (parser->index > sizeof(CONNECTION)-1
  1104. || c != CONNECTION[parser->index]) {
  1105. parser->header_state = h_general;
  1106. } else if (parser->index == sizeof(CONNECTION)-2) {
  1107. parser->header_state = h_connection;
  1108. }
  1109. break;
  1110. /* proxy-connection */
  1111. case h_matching_proxy_connection:
  1112. parser->index++;
  1113. if (parser->index > sizeof(PROXY_CONNECTION)-1
  1114. || c != PROXY_CONNECTION[parser->index]) {
  1115. parser->header_state = h_general;
  1116. } else if (parser->index == sizeof(PROXY_CONNECTION)-2) {
  1117. parser->header_state = h_connection;
  1118. }
  1119. break;
  1120. /* content-length */
  1121. case h_matching_content_length:
  1122. parser->index++;
  1123. if (parser->index > sizeof(CONTENT_LENGTH)-1
  1124. || c != CONTENT_LENGTH[parser->index]) {
  1125. parser->header_state = h_general;
  1126. } else if (parser->index == sizeof(CONTENT_LENGTH)-2) {
  1127. parser->header_state = h_content_length;
  1128. }
  1129. break;
  1130. /* transfer-encoding */
  1131. case h_matching_transfer_encoding:
  1132. parser->index++;
  1133. if (parser->index > sizeof(TRANSFER_ENCODING)-1
  1134. || c != TRANSFER_ENCODING[parser->index]) {
  1135. parser->header_state = h_general;
  1136. } else if (parser->index == sizeof(TRANSFER_ENCODING)-2) {
  1137. parser->header_state = h_transfer_encoding;
  1138. parser->extra_flags |= F_TRANSFER_ENCODING >> 8;
  1139. }
  1140. break;
  1141. /* upgrade */
  1142. case h_matching_upgrade:
  1143. parser->index++;
  1144. if (parser->index > sizeof(UPGRADE)-1
  1145. || c != UPGRADE[parser->index]) {
  1146. parser->header_state = h_general;
  1147. } else if (parser->index == sizeof(UPGRADE)-2) {
  1148. parser->header_state = h_upgrade;
  1149. }
  1150. break;
  1151. case h_connection:
  1152. case h_content_length:
  1153. case h_transfer_encoding:
  1154. case h_upgrade:
  1155. if (ch != ' ') parser->header_state = h_general;
  1156. break;
  1157. default:
  1158. assert(0 && "Unknown header_state");
  1159. break;
  1160. }
  1161. }
  1162. if (p == data + len) {
  1163. --p;
  1164. COUNT_HEADER_SIZE(p - start);
  1165. break;
  1166. }
  1167. COUNT_HEADER_SIZE(p - start);
  1168. if (ch == ':') {
  1169. UPDATE_STATE(s_header_value_discard_ws);
  1170. CALLBACK_DATA(header_field);
  1171. break;
  1172. }
  1173. SET_ERRNO(HPE_INVALID_HEADER_TOKEN);
  1174. goto error;
  1175. }
  1176. case s_header_value_discard_ws:
  1177. if (ch == ' ' || ch == '\t') break;
  1178. if (ch == CR) {
  1179. UPDATE_STATE(s_header_value_discard_ws_almost_done);
  1180. break;
  1181. }
  1182. if (ch == LF) {
  1183. UPDATE_STATE(s_header_value_discard_lws);
  1184. break;
  1185. }
  1186. /* fall through */
  1187. case s_header_value_start:
  1188. {
  1189. MARK(header_value);
  1190. UPDATE_STATE(s_header_value);
  1191. parser->index = 0;
  1192. c = LOWER(ch);
  1193. switch (parser->header_state) {
  1194. case h_upgrade:
  1195. parser->flags |= F_UPGRADE;
  1196. parser->header_state = h_general;
  1197. break;
  1198. case h_transfer_encoding:
  1199. /* looking for 'Transfer-Encoding: chunked' */
  1200. if ('c' == c) {
  1201. parser->header_state = h_matching_transfer_encoding_chunked;
  1202. } else {
  1203. parser->header_state = h_matching_transfer_encoding_token;
  1204. }
  1205. break;
  1206. /* Multi-value `Transfer-Encoding` header */
  1207. case h_matching_transfer_encoding_token_start:
  1208. break;
  1209. case h_content_length:
  1210. if (UNLIKELY(!IS_NUM(ch))) {
  1211. SET_ERRNO(HPE_INVALID_CONTENT_LENGTH);
  1212. goto error;
  1213. }
  1214. if (parser->flags & F_CONTENTLENGTH) {
  1215. SET_ERRNO(HPE_UNEXPECTED_CONTENT_LENGTH);
  1216. goto error;
  1217. }
  1218. parser->flags |= F_CONTENTLENGTH;
  1219. parser->content_length = ch - '0';
  1220. parser->header_state = h_content_length_num;
  1221. break;
  1222. /* when obsolete line folding is encountered for content length
  1223. * continue to the s_header_value state */
  1224. case h_content_length_ws:
  1225. break;
  1226. case h_connection:
  1227. /* looking for 'Connection: keep-alive' */
  1228. if (c == 'k') {
  1229. parser->header_state = h_matching_connection_keep_alive;
  1230. /* looking for 'Connection: close' */
  1231. } else if (c == 'c') {
  1232. parser->header_state = h_matching_connection_close;
  1233. } else if (c == 'u') {
  1234. parser->header_state = h_matching_connection_upgrade;
  1235. } else {
  1236. parser->header_state = h_matching_connection_token;
  1237. }
  1238. break;
  1239. /* Multi-value `Connection` header */
  1240. case h_matching_connection_token_start:
  1241. break;
  1242. default:
  1243. parser->header_state = h_general;
  1244. break;
  1245. }
  1246. break;
  1247. }
  1248. case s_header_value:
  1249. {
  1250. const char* start = p;
  1251. enum header_states h_state = (enum header_states) parser->header_state;
  1252. for (; p != data + len; p++) {
  1253. ch = *p;
  1254. if (ch == CR) {
  1255. UPDATE_STATE(s_header_almost_done);
  1256. parser->header_state = h_state;
  1257. CALLBACK_DATA(header_value);
  1258. break;
  1259. }
  1260. if (ch == LF) {
  1261. UPDATE_STATE(s_header_almost_done);
  1262. COUNT_HEADER_SIZE(p - start);
  1263. parser->header_state = h_state;
  1264. CALLBACK_DATA_NOADVANCE(header_value);
  1265. REEXECUTE();
  1266. }
  1267. if (!lenient && !IS_HEADER_CHAR(ch)) {
  1268. SET_ERRNO(HPE_INVALID_HEADER_TOKEN);
  1269. goto error;
  1270. }
  1271. c = LOWER(ch);
  1272. switch (h_state) {
  1273. case h_general:
  1274. {
  1275. size_t left = data + len - p;
  1276. const char* pe = p + MIN(left, max_header_size);
  1277. for (; p != pe; p++) {
  1278. ch = *p;
  1279. if (ch == CR || ch == LF) {
  1280. --p;
  1281. break;
  1282. }
  1283. if (!lenient && !IS_HEADER_CHAR(ch)) {
  1284. SET_ERRNO(HPE_INVALID_HEADER_TOKEN);
  1285. goto error;
  1286. }
  1287. }
  1288. if (p == data + len)
  1289. --p;
  1290. break;
  1291. }
  1292. case h_connection:
  1293. case h_transfer_encoding:
  1294. assert(0 && "Shouldn't get here.");
  1295. break;
  1296. case h_content_length:
  1297. if (ch == ' ') break;
  1298. h_state = h_content_length_num;
  1299. /* fall through */
  1300. case h_content_length_num:
  1301. {
  1302. uint64_t t;
  1303. if (ch == ' ') {
  1304. h_state = h_content_length_ws;
  1305. break;
  1306. }
  1307. if (UNLIKELY(!IS_NUM(ch))) {
  1308. SET_ERRNO(HPE_INVALID_CONTENT_LENGTH);
  1309. parser->header_state = h_state;
  1310. goto error;
  1311. }
  1312. t = parser->content_length;
  1313. t *= 10;
  1314. t += ch - '0';
  1315. /* Overflow? Test against a conservative limit for simplicity. */
  1316. if (UNLIKELY((ULLONG_MAX - 10) / 10 < parser->content_length)) {
  1317. SET_ERRNO(HPE_INVALID_CONTENT_LENGTH);
  1318. parser->header_state = h_state;
  1319. goto error;
  1320. }
  1321. parser->content_length = t;
  1322. break;
  1323. }
  1324. case h_content_length_ws:
  1325. if (ch == ' ') break;
  1326. SET_ERRNO(HPE_INVALID_CONTENT_LENGTH);
  1327. parser->header_state = h_state;
  1328. goto error;
  1329. /* Transfer-Encoding: chunked */
  1330. case h_matching_transfer_encoding_token_start:
  1331. /* looking for 'Transfer-Encoding: chunked' */
  1332. if ('c' == c) {
  1333. h_state = h_matching_transfer_encoding_chunked;
  1334. } else if (STRICT_TOKEN(c)) {
  1335. /* TODO(indutny): similar code below does this, but why?
  1336. * At the very least it seems to be inconsistent given that
  1337. * h_matching_transfer_encoding_token does not check for
  1338. * `STRICT_TOKEN`
  1339. */
  1340. h_state = h_matching_transfer_encoding_token;
  1341. } else if (c == ' ' || c == '\t') {
  1342. /* Skip lws */
  1343. } else {
  1344. h_state = h_general;
  1345. }
  1346. break;
  1347. case h_matching_transfer_encoding_chunked:
  1348. parser->index++;
  1349. if (parser->index > sizeof(CHUNKED)-1
  1350. || c != CHUNKED[parser->index]) {
  1351. h_state = h_matching_transfer_encoding_token;
  1352. } else if (parser->index == sizeof(CHUNKED)-2) {
  1353. h_state = h_transfer_encoding_chunked;
  1354. }
  1355. break;
  1356. case h_matching_transfer_encoding_token:
  1357. if (ch == ',') {
  1358. h_state = h_matching_transfer_encoding_token_start;
  1359. parser->index = 0;
  1360. }
  1361. break;
  1362. case h_matching_connection_token_start:
  1363. /* looking for 'Connection: keep-alive' */
  1364. if (c == 'k') {
  1365. h_state = h_matching_connection_keep_alive;
  1366. /* looking for 'Connection: close' */
  1367. } else if (c == 'c') {
  1368. h_state = h_matching_connection_close;
  1369. } else if (c == 'u') {
  1370. h_state = h_matching_connection_upgrade;
  1371. } else if (STRICT_TOKEN(c)) {
  1372. h_state = h_matching_connection_token;
  1373. } else if (c == ' ' || c == '\t') {
  1374. /* Skip lws */
  1375. } else {
  1376. h_state = h_general;
  1377. }
  1378. break;
  1379. /* looking for 'Connection: keep-alive' */
  1380. case h_matching_connection_keep_alive:
  1381. parser->index++;
  1382. if (parser->index > sizeof(KEEP_ALIVE)-1
  1383. || c != KEEP_ALIVE[parser->index]) {
  1384. h_state = h_matching_connection_token;
  1385. } else if (parser->index == sizeof(KEEP_ALIVE)-2) {
  1386. h_state = h_connection_keep_alive;
  1387. }
  1388. break;
  1389. /* looking for 'Connection: close' */
  1390. case h_matching_connection_close:
  1391. parser->index++;
  1392. if (parser->index > sizeof(CLOSE)-1 || c != CLOSE[parser->index]) {
  1393. h_state = h_matching_connection_token;
  1394. } else if (parser->index == sizeof(CLOSE)-2) {
  1395. h_state = h_connection_close;
  1396. }
  1397. break;
  1398. /* looking for 'Connection: upgrade' */
  1399. case h_matching_connection_upgrade:
  1400. parser->index++;
  1401. if (parser->index > sizeof(UPGRADE) - 1 ||
  1402. c != UPGRADE[parser->index]) {
  1403. h_state = h_matching_connection_token;
  1404. } else if (parser->index == sizeof(UPGRADE)-2) {
  1405. h_state = h_connection_upgrade;
  1406. }
  1407. break;
  1408. case h_matching_connection_token:
  1409. if (ch == ',') {
  1410. h_state = h_matching_connection_token_start;
  1411. parser->index = 0;
  1412. }
  1413. break;
  1414. case h_transfer_encoding_chunked:
  1415. if (ch != ' ') h_state = h_matching_transfer_encoding_token;
  1416. break;
  1417. case h_connection_keep_alive:
  1418. case h_connection_close:
  1419. case h_connection_upgrade:
  1420. if (ch == ',') {
  1421. if (h_state == h_connection_keep_alive) {
  1422. parser->flags |= F_CONNECTION_KEEP_ALIVE;
  1423. } else if (h_state == h_connection_close) {
  1424. parser->flags |= F_CONNECTION_CLOSE;
  1425. } else if (h_state == h_connection_upgrade) {
  1426. parser->flags |= F_CONNECTION_UPGRADE;
  1427. }
  1428. h_state = h_matching_connection_token_start;
  1429. parser->index = 0;
  1430. } else if (ch != ' ') {
  1431. h_state = h_matching_connection_token;
  1432. }
  1433. break;
  1434. default:
  1435. UPDATE_STATE(s_header_value);
  1436. h_state = h_general;
  1437. break;
  1438. }
  1439. }
  1440. parser->header_state = h_state;
  1441. if (p == data + len)
  1442. --p;
  1443. COUNT_HEADER_SIZE(p - start);
  1444. break;
  1445. }
  1446. case s_header_almost_done:
  1447. {
  1448. if (UNLIKELY(ch != LF)) {
  1449. SET_ERRNO(HPE_LF_EXPECTED);
  1450. goto error;
  1451. }
  1452. UPDATE_STATE(s_header_value_lws);
  1453. break;
  1454. }
  1455. case s_header_value_lws:
  1456. {
  1457. if (ch == ' ' || ch == '\t') {
  1458. if (parser->header_state == h_content_length_num) {
  1459. /* treat obsolete line folding as space */
  1460. parser->header_state = h_content_length_ws;
  1461. }
  1462. UPDATE_STATE(s_header_value_start);
  1463. REEXECUTE();
  1464. }
  1465. /* finished the header */
  1466. switch (parser->header_state) {
  1467. case h_connection_keep_alive:
  1468. parser->flags |= F_CONNECTION_KEEP_ALIVE;
  1469. break;
  1470. case h_connection_close:
  1471. parser->flags |= F_CONNECTION_CLOSE;
  1472. break;
  1473. case h_transfer_encoding_chunked:
  1474. parser->flags |= F_CHUNKED;
  1475. break;
  1476. case h_connection_upgrade:
  1477. parser->flags |= F_CONNECTION_UPGRADE;
  1478. break;
  1479. default:
  1480. break;
  1481. }
  1482. UPDATE_STATE(s_header_field_start);
  1483. REEXECUTE();
  1484. }
  1485. case s_header_value_discard_ws_almost_done:
  1486. {
  1487. STRICT_CHECK(ch != LF);
  1488. UPDATE_STATE(s_header_value_discard_lws);
  1489. break;
  1490. }
  1491. case s_header_value_discard_lws:
  1492. {
  1493. if (ch == ' ' || ch == '\t') {
  1494. UPDATE_STATE(s_header_value_discard_ws);
  1495. break;
  1496. } else {
  1497. switch (parser->header_state) {
  1498. case h_connection_keep_alive:
  1499. parser->flags |= F_CONNECTION_KEEP_ALIVE;
  1500. break;
  1501. case h_connection_close:
  1502. parser->flags |= F_CONNECTION_CLOSE;
  1503. break;
  1504. case h_connection_upgrade:
  1505. parser->flags |= F_CONNECTION_UPGRADE;
  1506. break;
  1507. case h_transfer_encoding_chunked:
  1508. parser->flags |= F_CHUNKED;
  1509. break;
  1510. case h_content_length:
  1511. /* do not allow empty content length */
  1512. SET_ERRNO(HPE_INVALID_CONTENT_LENGTH);
  1513. goto error;
  1514. break;
  1515. default:
  1516. break;
  1517. }
  1518. /* header value was empty */
  1519. MARK(header_value);
  1520. UPDATE_STATE(s_header_field_start);
  1521. CALLBACK_DATA_NOADVANCE(header_value);
  1522. REEXECUTE();
  1523. }
  1524. }
  1525. case s_headers_almost_done:
  1526. {
  1527. STRICT_CHECK(ch != LF);
  1528. if (parser->flags & F_TRAILING) {
  1529. /* End of a chunked request */
  1530. UPDATE_STATE(s_message_done);
  1531. CALLBACK_NOTIFY_NOADVANCE(chunk_complete);
  1532. REEXECUTE();
  1533. }
  1534. /* Cannot us transfer-encoding and a content-length header together
  1535. per the HTTP specification. (RFC 7230 Section 3.3.3) */
  1536. if ((parser->extra_flags & (F_TRANSFER_ENCODING >> 8)) &&
  1537. (parser->flags & F_CONTENTLENGTH)) {
  1538. /* Allow it for lenient parsing as long as `Transfer-Encoding` is
  1539. * not `chunked`
  1540. */
  1541. if (!lenient || (parser->flags & F_CHUNKED)) {
  1542. SET_ERRNO(HPE_UNEXPECTED_CONTENT_LENGTH);
  1543. goto error;
  1544. }
  1545. }
  1546. UPDATE_STATE(s_headers_done);
  1547. /* Set this here so that on_headers_complete() callbacks can see it */
  1548. if ((parser->flags & F_UPGRADE) &&
  1549. (parser->flags & F_CONNECTION_UPGRADE)) {
  1550. /* For responses, "Upgrade: foo" and "Connection: upgrade" are
  1551. * mandatory only when it is a 101 Switching Protocols response,
  1552. * otherwise it is purely informational, to announce support.
  1553. */
  1554. parser->upgrade =
  1555. (parser->type == HTTP_REQUEST || parser->status_code == 101);
  1556. } else {
  1557. parser->upgrade = (parser->method == HTTP_CONNECT);
  1558. }
  1559. /* Here we call the headers_complete callback. This is somewhat
  1560. * different than other callbacks because if the user returns 1, we
  1561. * will interpret that as saying that this message has no body. This
  1562. * is needed for the annoying case of recieving a response to a HEAD
  1563. * request.
  1564. *
  1565. * We'd like to use CALLBACK_NOTIFY_NOADVANCE() here but we cannot, so
  1566. * we have to simulate it by handling a change in errno below.
  1567. */
  1568. if (settings->on_headers_complete) {
  1569. switch (settings->on_headers_complete(parser)) {
  1570. case 0:
  1571. break;
  1572. case 2:
  1573. parser->upgrade = 1;
  1574. /* fall through */
  1575. case 1:
  1576. parser->flags |= F_SKIPBODY;
  1577. break;
  1578. default:
  1579. SET_ERRNO(HPE_CB_headers_complete);
  1580. RETURN(p - data); /* Error */
  1581. }
  1582. }
  1583. if (HTTP_PARSER_ERRNO(parser) != HPE_OK) {
  1584. RETURN(p - data);
  1585. }
  1586. REEXECUTE();
  1587. }
  1588. case s_headers_done:
  1589. {
  1590. int hasBody;
  1591. STRICT_CHECK(ch != LF);
  1592. parser->nread = 0;
  1593. nread = 0;
  1594. hasBody = parser->flags & F_CHUNKED ||
  1595. (parser->content_length > 0 && parser->content_length != ULLONG_MAX);
  1596. if (parser->upgrade && (parser->method == HTTP_CONNECT ||
  1597. (parser->flags & F_SKIPBODY) || !hasBody)) {
  1598. /* Exit, the rest of the message is in a different protocol. */
  1599. UPDATE_STATE(NEW_MESSAGE());
  1600. CALLBACK_NOTIFY(message_complete);
  1601. RETURN((p - data) + 1);
  1602. }
  1603. if (parser->flags & F_SKIPBODY) {
  1604. UPDATE_STATE(NEW_MESSAGE());
  1605. CALLBACK_NOTIFY(message_complete);
  1606. } else if (parser->flags & F_CHUNKED) {
  1607. /* chunked encoding - ignore Content-Length header,
  1608. * prepare for a chunk */
  1609. UPDATE_STATE(s_chunk_size_start);
  1610. } else if (parser->extra_flags & (F_TRANSFER_ENCODING >> 8)) {
  1611. if (parser->type == HTTP_REQUEST && !lenient) {
  1612. /* RFC 7230 3.3.3 */
  1613. /* If a Transfer-Encoding header field
  1614. * is present in a request and the chunked transfer coding is not
  1615. * the final encoding, the message body length cannot be determined
  1616. * reliably; the server MUST respond with the 400 (Bad Request)
  1617. * status code and then close the connection.
  1618. */
  1619. SET_ERRNO(HPE_INVALID_TRANSFER_ENCODING);
  1620. RETURN(p - data); /* Error */
  1621. } else {
  1622. /* RFC 7230 3.3.3 */
  1623. /* If a Transfer-Encoding header field is present in a response and
  1624. * the chunked transfer coding is not the final encoding, the
  1625. * message body length is determined by reading the connection until
  1626. * it is closed by the server.
  1627. */
  1628. UPDATE_STATE(s_body_identity_eof);
  1629. }
  1630. } else {
  1631. if (parser->content_length == 0) {
  1632. /* Content-Length header given but zero: Content-Length: 0\r\n */
  1633. UPDATE_STATE(NEW_MESSAGE());
  1634. CALLBACK_NOTIFY(message_complete);
  1635. } else if (parser->content_length != ULLONG_MAX) {
  1636. /* Content-Length header given and non-zero */
  1637. UPDATE_STATE(s_body_identity);
  1638. } else {
  1639. if (!http_message_needs_eof(parser)) {
  1640. /* Assume content-length 0 - read the next */
  1641. UPDATE_STATE(NEW_MESSAGE());
  1642. CALLBACK_NOTIFY(message_complete);
  1643. } else {
  1644. /* Read body until EOF */
  1645. UPDATE_STATE(s_body_identity_eof);
  1646. }
  1647. }
  1648. }
  1649. break;
  1650. }
  1651. case s_body_identity:
  1652. {
  1653. uint64_t to_read = MIN(parser->content_length,
  1654. (uint64_t) ((data + len) - p));
  1655. assert(parser->content_length != 0
  1656. && parser->content_length != ULLONG_MAX);
  1657. /* The difference between advancing content_length and p is because
  1658. * the latter will automaticaly advance on the next loop iteration.
  1659. * Further, if content_length ends up at 0, we want to see the last
  1660. * byte again for our message complete callback.
  1661. */
  1662. MARK(body);
  1663. parser->content_length -= to_read;
  1664. p += to_read - 1;
  1665. if (parser->content_length == 0) {
  1666. UPDATE_STATE(s_message_done);
  1667. /* Mimic CALLBACK_DATA_NOADVANCE() but with one extra byte.
  1668. *
  1669. * The alternative to doing this is to wait for the next byte to
  1670. * trigger the data callback, just as in every other case. The
  1671. * problem with this is that this makes it difficult for the test
  1672. * harness to distinguish between complete-on-EOF and
  1673. * complete-on-length. It's not clear that this distinction is
  1674. * important for applications, but let's keep it for now.
  1675. */
  1676. CALLBACK_DATA_(body, p - body_mark + 1, p - data);
  1677. REEXECUTE();
  1678. }
  1679. break;
  1680. }
  1681. /* read until EOF */
  1682. case s_body_identity_eof:
  1683. MARK(body);
  1684. p = data + len - 1;
  1685. break;
  1686. case s_message_done:
  1687. UPDATE_STATE(NEW_MESSAGE());
  1688. CALLBACK_NOTIFY(message_complete);
  1689. if (parser->upgrade) {
  1690. /* Exit, the rest of the message is in a different protocol. */
  1691. RETURN((p - data) + 1);
  1692. }
  1693. break;
  1694. case s_chunk_size_start:
  1695. {
  1696. assert(nread == 1);
  1697. assert(parser->flags & F_CHUNKED);
  1698. unhex_val = unhex[(unsigned char)ch];
  1699. if (UNLIKELY(unhex_val == -1)) {
  1700. SET_ERRNO(HPE_INVALID_CHUNK_SIZE);
  1701. goto error;
  1702. }
  1703. parser->content_length = unhex_val;
  1704. UPDATE_STATE(s_chunk_size);
  1705. break;
  1706. }
  1707. case s_chunk_size:
  1708. {
  1709. uint64_t t;
  1710. assert(parser->flags & F_CHUNKED);
  1711. if (ch == CR) {
  1712. UPDATE_STATE(s_chunk_size_almost_done);
  1713. break;
  1714. }
  1715. unhex_val = unhex[(unsigned char)ch];
  1716. if (unhex_val == -1) {
  1717. if (ch == ';' || ch == ' ') {
  1718. UPDATE_STATE(s_chunk_parameters);
  1719. break;
  1720. }
  1721. SET_ERRNO(HPE_INVALID_CHUNK_SIZE);
  1722. goto error;
  1723. }
  1724. t = parser->content_length;
  1725. t *= 16;
  1726. t += unhex_val;
  1727. /* Overflow? Test against a conservative limit for simplicity. */
  1728. if (UNLIKELY((ULLONG_MAX - 16) / 16 < parser->content_length)) {
  1729. SET_ERRNO(HPE_INVALID_CONTENT_LENGTH);
  1730. goto error;
  1731. }
  1732. parser->content_length = t;
  1733. break;
  1734. }
  1735. case s_chunk_parameters:
  1736. {
  1737. assert(parser->flags & F_CHUNKED);
  1738. /* just ignore this shit. TODO check for overflow */
  1739. if (ch == CR) {
  1740. UPDATE_STATE(s_chunk_size_almost_done);
  1741. break;
  1742. }
  1743. break;
  1744. }
  1745. case s_chunk_size_almost_done:
  1746. {
  1747. assert(parser->flags & F_CHUNKED);
  1748. STRICT_CHECK(ch != LF);
  1749. parser->nread = 0;
  1750. nread = 0;
  1751. if (parser->content_length == 0) {
  1752. parser->flags |= F_TRAILING;
  1753. UPDATE_STATE(s_header_field_start);
  1754. } else {
  1755. UPDATE_STATE(s_chunk_data);
  1756. }
  1757. CALLBACK_NOTIFY(chunk_header);
  1758. break;
  1759. }
  1760. case s_chunk_data:
  1761. {
  1762. uint64_t to_read = MIN(parser->content_length,
  1763. (uint64_t) ((data + len) - p));
  1764. assert(parser->flags & F_CHUNKED);
  1765. assert(parser->content_length != 0
  1766. && parser->content_length != ULLONG_MAX);
  1767. /* See the explanation in s_body_identity for why the content
  1768. * length and data pointers are managed this way.
  1769. */
  1770. MARK(body);
  1771. parser->content_length -= to_read;
  1772. p += to_read - 1;
  1773. if (parser->content_length == 0) {
  1774. UPDATE_STATE(s_chunk_data_almost_done);
  1775. }
  1776. break;
  1777. }
  1778. case s_chunk_data_almost_done:
  1779. assert(parser->flags & F_CHUNKED);
  1780. assert(parser->content_length == 0);
  1781. STRICT_CHECK(ch != CR);
  1782. UPDATE_STATE(s_chunk_data_done);
  1783. CALLBACK_DATA(body);
  1784. break;
  1785. case s_chunk_data_done:
  1786. assert(parser->flags & F_CHUNKED);
  1787. STRICT_CHECK(ch != LF);
  1788. parser->nread = 0;
  1789. nread = 0;
  1790. UPDATE_STATE(s_chunk_size_start);
  1791. CALLBACK_NOTIFY(chunk_complete);
  1792. break;
  1793. default:
  1794. assert(0 && "unhandled state");
  1795. SET_ERRNO(HPE_INVALID_INTERNAL_STATE);
  1796. goto error;
  1797. }
  1798. }
  1799. /* Run callbacks for any marks that we have leftover after we ran out of
  1800. * bytes. There should be at most one of these set, so it's OK to invoke
  1801. * them in series (unset marks will not result in callbacks).
  1802. *
  1803. * We use the NOADVANCE() variety of callbacks here because 'p' has already
  1804. * overflowed 'data' and this allows us to correct for the off-by-one that
  1805. * we'd otherwise have (since CALLBACK_DATA() is meant to be run with a 'p'
  1806. * value that's in-bounds).
  1807. */
  1808. assert(((header_field_mark ? 1 : 0) +
  1809. (header_value_mark ? 1 : 0) +
  1810. (url_mark ? 1 : 0) +
  1811. (body_mark ? 1 : 0) +
  1812. (status_mark ? 1 : 0)) <= 1);
  1813. CALLBACK_DATA_NOADVANCE(header_field);
  1814. CALLBACK_DATA_NOADVANCE(header_value);
  1815. CALLBACK_DATA_NOADVANCE(url);
  1816. CALLBACK_DATA_NOADVANCE(body);
  1817. CALLBACK_DATA_NOADVANCE(status);
  1818. RETURN(len);
  1819. error:
  1820. if (HTTP_PARSER_ERRNO(parser) == HPE_OK) {
  1821. SET_ERRNO(HPE_UNKNOWN);
  1822. }
  1823. RETURN(p - data);
  1824. }
  1825. /* Does the parser need to see an EOF to find the end of the message? */
  1826. int
  1827. http_message_needs_eof (const http_parser *parser)
  1828. {
  1829. if (parser->type == HTTP_REQUEST) {
  1830. return 0;
  1831. }
  1832. /* See RFC 2616 section 4.4 */
  1833. if (parser->status_code / 100 == 1 || /* 1xx e.g. Continue */
  1834. parser->status_code == 204 || /* No Content */
  1835. parser->status_code == 304 || /* Not Modified */
  1836. parser->flags & F_SKIPBODY) { /* response to a HEAD request */
  1837. return 0;
  1838. }
  1839. /* RFC 7230 3.3.3, see `s_headers_almost_done` */
  1840. if ((parser->extra_flags & (F_TRANSFER_ENCODING >> 8)) &&
  1841. (parser->flags & F_CHUNKED) == 0) {
  1842. return 1;
  1843. }
  1844. if ((parser->flags & F_CHUNKED) || parser->content_length != ULLONG_MAX) {
  1845. return 0;
  1846. }
  1847. return 1;
  1848. }
  1849. int
  1850. http_should_keep_alive (const http_parser *parser)
  1851. {
  1852. if (parser->http_major > 0 && parser->http_minor > 0) {
  1853. /* HTTP/1.1 */
  1854. if (parser->flags & F_CONNECTION_CLOSE) {
  1855. return 0;
  1856. }
  1857. } else {
  1858. /* HTTP/1.0 or earlier */
  1859. if (!(parser->flags & F_CONNECTION_KEEP_ALIVE)) {
  1860. return 0;
  1861. }
  1862. }
  1863. return !http_message_needs_eof(parser);
  1864. }
  1865. const char *
  1866. http_method_str (enum http_method m)
  1867. {
  1868. return ELEM_AT(method_strings, m, "<unknown>");
  1869. }
  1870. const char *
  1871. http_status_str (enum http_status s)
  1872. {
  1873. switch (s) {
  1874. #define XX(num, name, string) case HTTP_STATUS_##name: return #string;
  1875. HTTP_STATUS_MAP(XX)
  1876. #undef XX
  1877. default: return "<unknown>";
  1878. }
  1879. }
  1880. void
  1881. http_parser_init (http_parser *parser, enum http_parser_type t)
  1882. {
  1883. void *data = parser->data; /* preserve application data */
  1884. memset(parser, 0, sizeof(*parser));
  1885. parser->data = data;
  1886. parser->type = t;
  1887. parser->state = (t == HTTP_REQUEST ? s_start_req : (t == HTTP_RESPONSE ? s_start_res : s_start_req_or_res));
  1888. parser->http_errno = HPE_OK;
  1889. }
  1890. void
  1891. http_parser_settings_init(http_parser_settings *settings)
  1892. {
  1893. memset(settings, 0, sizeof(*settings));
  1894. }
  1895. const char *
  1896. http_errno_name(enum http_errno err) {
  1897. assert(((size_t) err) < ARRAY_SIZE(http_strerror_tab));
  1898. return http_strerror_tab[err].name;
  1899. }
  1900. const char *
  1901. http_errno_description(enum http_errno err) {
  1902. assert(((size_t) err) < ARRAY_SIZE(http_strerror_tab));
  1903. return http_strerror_tab[err].description;
  1904. }
  1905. static enum http_host_state
  1906. http_parse_host_char(enum http_host_state s, const char ch) {
  1907. switch(s) {
  1908. case s_http_userinfo:
  1909. case s_http_userinfo_start:
  1910. if (ch == '@') {
  1911. return s_http_host_start;
  1912. }
  1913. if (IS_USERINFO_CHAR(ch)) {
  1914. return s_http_userinfo;
  1915. }
  1916. break;
  1917. case s_http_host_start:
  1918. if (ch == '[') {
  1919. return s_http_host_v6_start;
  1920. }
  1921. if (IS_HOST_CHAR(ch)) {
  1922. return s_http_host;
  1923. }
  1924. break;
  1925. case s_http_host:
  1926. if (IS_HOST_CHAR(ch)) {
  1927. return s_http_host;
  1928. }
  1929. /* fall through */
  1930. case s_http_host_v6_end:
  1931. if (ch == ':') {
  1932. return s_http_host_port_start;
  1933. }
  1934. break;
  1935. case s_http_host_v6:
  1936. if (ch == ']') {
  1937. return s_http_host_v6_end;
  1938. }
  1939. /* fall through */
  1940. case s_http_host_v6_start:
  1941. if (IS_HEX(ch) || ch == ':' || ch == '.') {
  1942. return s_http_host_v6;
  1943. }
  1944. if (s == s_http_host_v6 && ch == '%') {
  1945. return s_http_host_v6_zone_start;
  1946. }
  1947. break;
  1948. case s_http_host_v6_zone:
  1949. if (ch == ']') {
  1950. return s_http_host_v6_end;
  1951. }
  1952. /* fall through */
  1953. case s_http_host_v6_zone_start:
  1954. /* RFC 6874 Zone ID consists of 1*( unreserved / pct-encoded) */
  1955. if (IS_ALPHANUM(ch) || ch == '%' || ch == '.' || ch == '-' || ch == '_' ||
  1956. ch == '~') {
  1957. return s_http_host_v6_zone;
  1958. }
  1959. break;
  1960. case s_http_host_port:
  1961. case s_http_host_port_start:
  1962. if (IS_NUM(ch)) {
  1963. return s_http_host_port;
  1964. }
  1965. break;
  1966. default:
  1967. break;
  1968. }
  1969. return s_http_host_dead;
  1970. }
  1971. static int
  1972. http_parse_host(const char * buf, struct http_parser_url *u, int found_at) {
  1973. enum http_host_state s;
  1974. const char *p;
  1975. size_t buflen = u->field_data[UF_HOST].off + u->field_data[UF_HOST].len;
  1976. assert(u->field_set & (1 << UF_HOST));
  1977. u->field_data[UF_HOST].len = 0;
  1978. s = found_at ? s_http_userinfo_start : s_http_host_start;
  1979. for (p = buf + u->field_data[UF_HOST].off; p < buf + buflen; p++) {
  1980. enum http_host_state new_s = http_parse_host_char(s, *p);
  1981. if (new_s == s_http_host_dead) {
  1982. return 1;
  1983. }
  1984. switch(new_s) {
  1985. case s_http_host:
  1986. if (s != s_http_host) {
  1987. u->field_data[UF_HOST].off = (uint16_t)(p - buf);
  1988. }
  1989. u->field_data[UF_HOST].len++;
  1990. break;
  1991. case s_http_host_v6:
  1992. if (s != s_http_host_v6) {
  1993. u->field_data[UF_HOST].off = (uint16_t)(p - buf);
  1994. }
  1995. u->field_data[UF_HOST].len++;
  1996. break;
  1997. case s_http_host_v6_zone_start:
  1998. case s_http_host_v6_zone:
  1999. u->field_data[UF_HOST].len++;
  2000. break;
  2001. case s_http_host_port:
  2002. if (s != s_http_host_port) {
  2003. u->field_data[UF_PORT].off = (uint16_t)(p - buf);
  2004. u->field_data[UF_PORT].len = 0;
  2005. u->field_set |= (1 << UF_PORT);
  2006. }
  2007. u->field_data[UF_PORT].len++;
  2008. break;
  2009. case s_http_userinfo:
  2010. if (s != s_http_userinfo) {
  2011. u->field_data[UF_USERINFO].off = (uint16_t)(p - buf);
  2012. u->field_data[UF_USERINFO].len = 0;
  2013. u->field_set |= (1 << UF_USERINFO);
  2014. }
  2015. u->field_data[UF_USERINFO].len++;
  2016. break;
  2017. default:
  2018. break;
  2019. }
  2020. s = new_s;
  2021. }
  2022. /* Make sure we don't end somewhere unexpected */
  2023. switch (s) {
  2024. case s_http_host_start:
  2025. case s_http_host_v6_start:
  2026. case s_http_host_v6:
  2027. case s_http_host_v6_zone_start:
  2028. case s_http_host_v6_zone:
  2029. case s_http_host_port_start:
  2030. case s_http_userinfo:
  2031. case s_http_userinfo_start:
  2032. return 1;
  2033. default:
  2034. break;
  2035. }
  2036. return 0;
  2037. }
  2038. void
  2039. http_parser_url_init(struct http_parser_url *u) {
  2040. memset(u, 0, sizeof(*u));
  2041. }
  2042. int
  2043. http_parser_parse_url(const char *buf, size_t buflen, int is_connect,
  2044. struct http_parser_url *u)
  2045. {
  2046. enum state s;
  2047. const char *p;
  2048. enum http_parser_url_fields uf, old_uf;
  2049. int found_at = 0;
  2050. if (buflen == 0) {
  2051. return 1;
  2052. }
  2053. u->port = u->field_set = 0;
  2054. s = is_connect ? s_req_server_start : s_req_spaces_before_url;
  2055. old_uf = UF_MAX;
  2056. for (p = buf; p < buf + buflen; p++) {
  2057. s = parse_url_char(s, *p);
  2058. /* Figure out the next field that we're operating on */
  2059. switch (s) {
  2060. case s_dead:
  2061. return 1;
  2062. /* Skip delimeters */
  2063. case s_req_schema_slash:
  2064. case s_req_schema_slash_slash:
  2065. case s_req_server_start:
  2066. case s_req_query_string_start:
  2067. case s_req_fragment_start:
  2068. continue;
  2069. case s_req_schema:
  2070. uf = UF_SCHEMA;
  2071. break;
  2072. case s_req_server_with_at:
  2073. found_at = 1;
  2074. /* fall through */
  2075. case s_req_server:
  2076. uf = UF_HOST;
  2077. break;
  2078. case s_req_path:
  2079. uf = UF_PATH;
  2080. break;
  2081. case s_req_query_string:
  2082. uf = UF_QUERY;
  2083. break;
  2084. case s_req_fragment:
  2085. uf = UF_FRAGMENT;
  2086. break;
  2087. default:
  2088. assert(!"Unexpected state");
  2089. return 1;
  2090. }
  2091. /* Nothing's changed; soldier on */
  2092. if (uf == old_uf) {
  2093. u->field_data[uf].len++;
  2094. continue;
  2095. }
  2096. u->field_data[uf].off = (uint16_t)(p - buf);
  2097. u->field_data[uf].len = 1;
  2098. u->field_set |= (1 << uf);
  2099. old_uf = uf;
  2100. }
  2101. /* host must be present if there is a schema */
  2102. /* parsing http:///toto will fail */
  2103. if ((u->field_set & (1 << UF_SCHEMA)) &&
  2104. (u->field_set & (1 << UF_HOST)) == 0) {
  2105. return 1;
  2106. }
  2107. if (u->field_set & (1 << UF_HOST)) {
  2108. if (http_parse_host(buf, u, found_at) != 0) {
  2109. return 1;
  2110. }
  2111. }
  2112. /* CONNECT requests can only contain "hostname:port" */
  2113. if (is_connect && u->field_set != ((1 << UF_HOST)|(1 << UF_PORT))) {
  2114. return 1;
  2115. }
  2116. if (u->field_set & (1 << UF_PORT)) {
  2117. uint16_t off;
  2118. uint16_t len;
  2119. const char* p;
  2120. const char* end;
  2121. unsigned long v;
  2122. off = u->field_data[UF_PORT].off;
  2123. len = u->field_data[UF_PORT].len;
  2124. end = buf + off + len;
  2125. /* NOTE: The characters are already validated and are in the [0-9] range */
  2126. assert(off + len <= buflen && "Port number overflow");
  2127. v = 0;
  2128. for (p = buf + off; p < end; p++) {
  2129. v *= 10;
  2130. v += *p - '0';
  2131. /* Ports have a max value of 2^16 */
  2132. if (v > 0xffff) {
  2133. return 1;
  2134. }
  2135. }
  2136. u->port = (uint16_t) v;
  2137. }
  2138. return 0;
  2139. }
  2140. void
  2141. http_parser_pause(http_parser *parser, int paused) {
  2142. /* Users should only be pausing/unpausing a parser that is not in an error
  2143. * state. In non-debug builds, there's not much that we can do about this
  2144. * other than ignore it.
  2145. */
  2146. if (HTTP_PARSER_ERRNO(parser) == HPE_OK ||
  2147. HTTP_PARSER_ERRNO(parser) == HPE_PAUSED) {
  2148. uint32_t nread = parser->nread; /* used by the SET_ERRNO macro */
  2149. SET_ERRNO((paused) ? HPE_PAUSED : HPE_OK);
  2150. } else {
  2151. assert(0 && "Attempting to pause parser in error state");
  2152. }
  2153. }
  2154. int
  2155. http_body_is_final(const struct http_parser *parser) {
  2156. return parser->state == s_message_done;
  2157. }
  2158. unsigned long
  2159. http_parser_version(void) {
  2160. return HTTP_PARSER_VERSION_MAJOR * 0x10000 |
  2161. HTTP_PARSER_VERSION_MINOR * 0x00100 |
  2162. HTTP_PARSER_VERSION_PATCH * 0x00001;
  2163. }
  2164. void
  2165. http_parser_set_max_header_size(uint32_t size) {
  2166. max_header_size = size;
  2167. }