You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

http_parser.c 62KB

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