http_parser.c 70 KB

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