http_parser.c 69 KB

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