http_parser.c 70 KB

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