http_parser.h 107 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139
  1. /*
  2. * this file has modifyed by 37792738@qq.com,so that it can be used in C++
  3. * programs,and add a param "void * user_data" into the http callback,and
  4. * add a param "void * user_data" into the http_parser_execute function.
  5. */
  6. /* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to
  10. * deal in the Software without restriction, including without limitation the
  11. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  12. * sell copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  23. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  24. * IN THE SOFTWARE.
  25. */
  26. #ifndef __ASIO2_HTTP_PARSER_H__
  27. #define __ASIO2_HTTP_PARSER_H__
  28. #include <asio2/base/detail/push_options.hpp>
  29. #ifdef ASIO2_HEADER_ONLY
  30. namespace bho::beast::http::parses
  31. #else
  32. namespace boost::beast::http::parses
  33. #endif
  34. {
  35. //#ifdef __cplusplus
  36. //extern "C" {
  37. //#endif
  38. /* Also update SONAME in the Makefile whenever you change these. */
  39. #define HTTP_PARSER_VERSION_MAJOR 2
  40. #define HTTP_PARSER_VERSION_MINOR 9
  41. #define HTTP_PARSER_VERSION_PATCH 4
  42. #include <stddef.h>
  43. #if defined(_WIN32) && !defined(__MINGW32__) && \
  44. (!defined(_MSC_VER) || _MSC_VER<1600) && !defined(__WINE__)
  45. #include <BaseTsd.h>
  46. typedef __int8 int8_t;
  47. typedef unsigned __int8 uint8_t;
  48. typedef __int16 int16_t;
  49. typedef unsigned __int16 uint16_t;
  50. typedef __int32 int32_t;
  51. typedef unsigned __int32 uint32_t;
  52. typedef __int64 int64_t;
  53. typedef unsigned __int64 uint64_t;
  54. #elif (defined(__sun) || defined(__sun__)) && defined(__SunOS_5_9)
  55. #include <sys/inttypes.h>
  56. #else
  57. #include <stdint.h>
  58. #endif
  59. /* Compile with -DHTTP_PARSER_STRICT=0 to make less checks, but run
  60. * faster
  61. */
  62. #ifndef HTTP_PARSER_STRICT
  63. # define HTTP_PARSER_STRICT 1
  64. #endif
  65. /* Maximium header size allowed. If the macro is not defined
  66. * before including this header then the default is used. To
  67. * change the maximum header size, define the macro in the build
  68. * environment (e.g. -DHTTP_MAX_HEADER_SIZE=<value>). To remove
  69. * the effective limit on the size of the header, define the macro
  70. * to a very large number (e.g. -DHTTP_MAX_HEADER_SIZE=0x7fffffff)
  71. */
  72. #ifndef HTTP_MAX_HEADER_SIZE
  73. # define HTTP_MAX_HEADER_SIZE (80*1024)
  74. #endif
  75. typedef struct http_parser http_parser;
  76. typedef struct http_parser_settings http_parser_settings;
  77. /* Callbacks should return non-zero to indicate an error. The parser will
  78. * then halt execution.
  79. *
  80. * The one exception is on_headers_complete. In a HTTP_RESPONSE parser
  81. * returning '1' from on_headers_complete will tell the parser that it
  82. * should not expect a body. This is used when receiving a response to a
  83. * HEAD request which may contain 'Content-Length' or 'Transfer-Encoding:
  84. * chunked' headers that indicate the presence of a body.
  85. *
  86. * Returning `2` from on_headers_complete will tell parser that it should not
  87. * expect neither a body nor any futher responses on this connection. This is
  88. * useful for handling responses to a CONNECT request which may not contain
  89. * `Upgrade` or `Connection: upgrade` headers.
  90. *
  91. * http_data_cb does not return data chunks. It will be called arbitrarily
  92. * many times for each string. E.G. you might get 10 callbacks for "on_url"
  93. * each providing just a few characters more data.
  94. */
  95. typedef int (*http_data_cb) (http_parser*, const char *at, size_t length, void * user_data);
  96. typedef int (*http_cb) (http_parser*, void * user_data);
  97. /* Status Codes */
  98. #define HTTP_STATUS_MAP(HTTP_XX) \
  99. HTTP_XX(100, continuation, Continue) \
  100. HTTP_XX(101, switching_protocols, Switching Protocols) \
  101. HTTP_XX(102, processing, Processing) \
  102. HTTP_XX(200, ok, OK) \
  103. HTTP_XX(201, created, Created) \
  104. HTTP_XX(202, accepted, Accepted) \
  105. HTTP_XX(203, non_authoritative_information, Non-Authoritative Information) \
  106. HTTP_XX(204, no_content, No Content) \
  107. HTTP_XX(205, reset_content, Reset Content) \
  108. HTTP_XX(206, partial_content, Partial Content) \
  109. HTTP_XX(207, multi_status, Multi-Status) \
  110. HTTP_XX(208, already_reported, Already Reported) \
  111. HTTP_XX(226, im_used, IM Used) \
  112. HTTP_XX(300, multiple_choices, Multiple Choices) \
  113. HTTP_XX(301, moved_permanently, Moved Permanently) \
  114. HTTP_XX(302, found, Found) \
  115. HTTP_XX(303, see_other, See Other) \
  116. HTTP_XX(304, not_modified, Not Modified) \
  117. HTTP_XX(305, use_proxy, Use Proxy) \
  118. HTTP_XX(307, temporary_redirect, Temporary Redirect) \
  119. HTTP_XX(308, permanent_redirect, Permanent Redirect) \
  120. HTTP_XX(400, bad_request, Bad Request) \
  121. HTTP_XX(401, unauthorized, Unauthorized) \
  122. HTTP_XX(402, payment_required, Payment Required) \
  123. HTTP_XX(403, forbidden, Forbidden) \
  124. HTTP_XX(404, not_found, Not Found) \
  125. HTTP_XX(405, method_not_allowed, Method Not Allowed) \
  126. HTTP_XX(406, not_acceptable, Not Acceptable) \
  127. HTTP_XX(407, proxy_authentication_required, Proxy Authentication Required) \
  128. HTTP_XX(408, request_timeout, Request Timeout) \
  129. HTTP_XX(409, conflict, Conflict) \
  130. HTTP_XX(410, gone, Gone) \
  131. HTTP_XX(411, length_required, Length Required) \
  132. HTTP_XX(412, precondition_failed, Precondition Failed) \
  133. HTTP_XX(413, payload_too_large, Payload Too Large) \
  134. HTTP_XX(414, uri_too_long, URI Too Long) \
  135. HTTP_XX(415, unsupported_media_type, Unsupported Media Type) \
  136. HTTP_XX(416, range_not_satisfiable, Range Not Satisfiable) \
  137. HTTP_XX(417, expectation_failed, Expectation Failed) \
  138. HTTP_XX(421, misdirected_request, Misdirected Request) \
  139. HTTP_XX(422, unprocessable_entity, Unprocessable Entity) \
  140. HTTP_XX(423, locked, Locked) \
  141. HTTP_XX(424, failed_dependency, Failed Dependency) \
  142. HTTP_XX(426, upgrade_required, Upgrade Required) \
  143. HTTP_XX(428, precondition_required, Precondition Required) \
  144. HTTP_XX(429, too_many_requests, Too Many Requests) \
  145. HTTP_XX(431, request_header_fields_too_large, Request Header Fields Too Large) \
  146. HTTP_XX(451, unavailable_for_legal_reasons, Unavailable For Legal Reasons) \
  147. HTTP_XX(500, internal_server_error, Internal Server Error) \
  148. HTTP_XX(501, not_implemented, Not Implemented) \
  149. HTTP_XX(502, bad_gateway, Bad Gateway) \
  150. HTTP_XX(503, service_unavailable, Service Unavailable) \
  151. HTTP_XX(504, gateway_timeout, Gateway Timeout) \
  152. HTTP_XX(505, http_version_not_supported, HTTP Version Not Supported) \
  153. HTTP_XX(506, variant_also_negotiates, Variant Also Negotiates) \
  154. HTTP_XX(507, insufficient_storage, Insufficient Storage) \
  155. HTTP_XX(508, loop_detected, Loop Detected) \
  156. HTTP_XX(510, not_extended, Not Extended) \
  157. HTTP_XX(511, network_authentication_required, Network Authentication Required) \
  158. enum class http_status
  159. {
  160. // 20220214 Fix the conflict with the macro definition in <wininet.h>
  161. // HTTP_STATUS_CONTINUE is also defined in <wininet.h>
  162. #define HTTP_XX(num, name, string) name = num,
  163. HTTP_STATUS_MAP(HTTP_XX)
  164. #undef HTTP_XX
  165. };
  166. /* Request Methods */
  167. #define HTTP_METHOD_MAP(HTTP_XX) \
  168. HTTP_XX(0, DELETE, DELETE) \
  169. HTTP_XX(1, GET, GET) \
  170. HTTP_XX(2, HEAD, HEAD) \
  171. HTTP_XX(3, POST, POST) \
  172. HTTP_XX(4, PUT, PUT) \
  173. /* pathological */ \
  174. HTTP_XX(5, CONNECT, CONNECT) \
  175. HTTP_XX(6, OPTIONS, OPTIONS) \
  176. HTTP_XX(7, TRACE, TRACE) \
  177. /* WebDAV */ \
  178. HTTP_XX(8, COPY, COPY) \
  179. HTTP_XX(9, LOCK, LOCK) \
  180. HTTP_XX(10, MKCOL, MKCOL) \
  181. HTTP_XX(11, MOVE, MOVE) \
  182. HTTP_XX(12, PROPFIND, PROPFIND) \
  183. HTTP_XX(13, PROPPATCH, PROPPATCH) \
  184. HTTP_XX(14, SEARCH, SEARCH) \
  185. HTTP_XX(15, UNLOCK, UNLOCK) \
  186. HTTP_XX(16, BIND, BIND) \
  187. HTTP_XX(17, REBIND, REBIND) \
  188. HTTP_XX(18, UNBIND, UNBIND) \
  189. HTTP_XX(19, ACL, ACL) \
  190. /* subversion */ \
  191. HTTP_XX(20, REPORT, REPORT) \
  192. HTTP_XX(21, MKACTIVITY, MKACTIVITY) \
  193. HTTP_XX(22, CHECKOUT, CHECKOUT) \
  194. HTTP_XX(23, MERGE, MERGE) \
  195. /* upnp */ \
  196. HTTP_XX(24, MSEARCH, M-SEARCH) \
  197. HTTP_XX(25, NOTIFY, NOTIFY) \
  198. HTTP_XX(26, SUBSCRIBE, SUBSCRIBE) \
  199. HTTP_XX(27, UNSUBSCRIBE, UNSUBSCRIBE) \
  200. /* RFC-5789 */ \
  201. HTTP_XX(28, PATCH, PATCH) \
  202. HTTP_XX(29, PURGE, PURGE) \
  203. /* CalDAV */ \
  204. HTTP_XX(30, MKCALENDAR, MKCALENDAR) \
  205. /* RFC-2068, section 19.6.1.2 */ \
  206. HTTP_XX(31, LINK, LINK) \
  207. HTTP_XX(32, UNLINK, UNLINK) \
  208. /* icecast */ \
  209. HTTP_XX(33, SOURCE, SOURCE) \
  210. enum class http_method
  211. {
  212. #define HTTP_XX(num, name, string) HTTP_##name = num,
  213. HTTP_METHOD_MAP(HTTP_XX)
  214. #undef HTTP_XX
  215. };
  216. enum class http_parser_type { HTTP_REQUEST, HTTP_RESPONSE, HTTP_BOTH };
  217. /* Flag values for http_parser.flags field */
  218. enum class flags
  219. { F_CHUNKED = 1 << 0
  220. , F_CONNECTION_KEEP_ALIVE = 1 << 1
  221. , F_CONNECTION_CLOSE = 1 << 2
  222. , F_CONNECTION_UPGRADE = 1 << 3
  223. , F_TRAILING = 1 << 4
  224. , F_UPGRADE = 1 << 5
  225. , F_SKIPBODY = 1 << 6
  226. , F_CONTENTLENGTH = 1 << 7
  227. };
  228. /* Map for errno-related constants
  229. *
  230. * The provided argument should be a macro that takes 2 arguments.
  231. */
  232. #define HTTP_ERRNO_MAP(HTTP_XX) \
  233. /* No error */ \
  234. HTTP_XX(OK, "success") \
  235. \
  236. /* Callback-related errors */ \
  237. HTTP_XX(CB_message_begin, "the on_message_begin callback failed") \
  238. HTTP_XX(CB_url, "the on_url callback failed") \
  239. HTTP_XX(CB_header_field, "the on_header_field callback failed") \
  240. HTTP_XX(CB_header_value, "the on_header_value callback failed") \
  241. HTTP_XX(CB_headers_complete, "the on_headers_complete callback failed") \
  242. HTTP_XX(CB_body, "the on_body callback failed") \
  243. HTTP_XX(CB_message_complete, "the on_message_complete callback failed") \
  244. HTTP_XX(CB_status, "the on_status callback failed") \
  245. HTTP_XX(CB_chunk_header, "the on_chunk_header callback failed") \
  246. HTTP_XX(CB_chunk_complete, "the on_chunk_complete callback failed") \
  247. \
  248. /* Parsing-related errors */ \
  249. HTTP_XX(INVALID_EOF_STATE, "stream ended at an unexpected time") \
  250. HTTP_XX(HEADER_OVERFLOW, \
  251. "too many header bytes seen; overflow detected") \
  252. HTTP_XX(CLOSED_CONNECTION, \
  253. "data received after completed connection: close message") \
  254. HTTP_XX(INVALID_VERSION, "invalid HTTP version") \
  255. HTTP_XX(INVALID_STATUS, "invalid HTTP status code") \
  256. HTTP_XX(INVALID_METHOD, "invalid HTTP method") \
  257. HTTP_XX(INVALID_URL, "invalid URL") \
  258. HTTP_XX(INVALID_HOST, "invalid host") \
  259. HTTP_XX(INVALID_PORT, "invalid port") \
  260. HTTP_XX(INVALID_PATH, "invalid path") \
  261. HTTP_XX(INVALID_QUERY_STRING, "invalid query string") \
  262. HTTP_XX(INVALID_FRAGMENT, "invalid fragment") \
  263. HTTP_XX(LF_EXPECTED, "LF character expected") \
  264. HTTP_XX(INVALID_HEADER_TOKEN, "invalid character in header") \
  265. HTTP_XX(INVALID_CONTENT_LENGTH, \
  266. "invalid character in content-length header") \
  267. HTTP_XX(UNEXPECTED_CONTENT_LENGTH, \
  268. "unexpected content-length header") \
  269. HTTP_XX(INVALID_CHUNK_SIZE, \
  270. "invalid character in chunk size header") \
  271. HTTP_XX(INVALID_CONSTANT, "invalid constant string") \
  272. HTTP_XX(INVALID_INTERNAL_STATE, "encountered unexpected internal state")\
  273. HTTP_XX(STRICT, "strict mode assertion failed") \
  274. HTTP_XX(PAUSED, "parser is paused") \
  275. HTTP_XX(UNKNOWN, "an unknown error occurred") \
  276. HTTP_XX(INVALID_TRANSFER_ENCODING, \
  277. "request has invalid transfer-encoding") \
  278. /* Define HPE_* values for each errno value above */
  279. #define HTTP_ERRNO_GEN(n, s) HPE_##n,
  280. enum class http_errno {
  281. HTTP_ERRNO_MAP(HTTP_ERRNO_GEN)
  282. };
  283. #undef HTTP_ERRNO_GEN
  284. /* Get an http_errno value from an http_parser */
  285. #define HTTP_PARSER_ERRNO(p) ((http_errno) (p)->http_errno)
  286. struct http_parser {
  287. /** PRIVATE **/
  288. unsigned int type : 2; /* enum http_parser_type */
  289. unsigned int flags : 8; /* F_* values from 'flags' enum; semi-public */
  290. unsigned int state : 7; /* enum state from http_parser.c */
  291. unsigned int header_state : 7; /* enum header_state from http_parser.c */
  292. unsigned int index : 5; /* index into current matcher */
  293. unsigned int uses_transfer_encoding : 1; /* Transfer-Encoding header is present */
  294. unsigned int allow_chunked_length : 1; /* Allow headers with both
  295. * `Content-Length` and
  296. * `Transfer-Encoding: chunked` set */
  297. unsigned int lenient_http_headers : 1;
  298. uint32_t nread; /* # bytes read in various scenarios */
  299. uint64_t content_length; /* # bytes in body. `(uint64_t) -1` (all bits one)
  300. * if no Content-Length header.
  301. */
  302. /** READ-ONLY **/
  303. unsigned short http_major;
  304. unsigned short http_minor;
  305. unsigned int status_code : 16; /* responses only */
  306. unsigned int method : 8; /* requests only */
  307. unsigned int http_errno : 7;
  308. /* 1 = Upgrade header was present and the parser has exited because of that.
  309. * 0 = No upgrade header present.
  310. * Should be checked when http_parser_execute() returns in addition to
  311. * error checking.
  312. */
  313. unsigned int upgrade : 1;
  314. /** PUBLIC **/
  315. void *data; /* A pointer to get hook to the "connection" or "socket" object */
  316. };
  317. struct http_parser_settings {
  318. http_cb on_message_begin;
  319. http_data_cb on_url;
  320. http_data_cb on_status;
  321. http_data_cb on_header_field;
  322. http_data_cb on_header_value;
  323. http_cb on_headers_complete;
  324. http_data_cb on_body;
  325. http_cb on_message_complete;
  326. /* When on_chunk_header is called, the current chunk length is stored
  327. * in parser->content_length.
  328. */
  329. http_cb on_chunk_header;
  330. http_cb on_chunk_complete;
  331. };
  332. enum class url_fields
  333. { UF_SCHEMA = 0
  334. , UF_HOST = 1
  335. , UF_PORT = 2
  336. , UF_PATH = 3
  337. , UF_QUERY = 4
  338. , UF_FRAGMENT = 5
  339. , UF_USERINFO = 6
  340. , UF_MAX = 7
  341. };
  342. /* Result structure for http_parser_parse_url().
  343. *
  344. * Callers should index into field_data[] with UF_* values iff field_set
  345. * has the relevant (1 << UF_*) bit set. As a courtesy to clients (and
  346. * because we probably have padding left over), we convert any port to
  347. * a uint16_t.
  348. */
  349. struct http_parser_url {
  350. uint16_t field_set; /* Bitmask of (1 << UF_*) values */
  351. uint16_t port; /* Converted UF_PORT string */
  352. struct {
  353. uint16_t off; /* Offset into buffer in which field starts */
  354. uint16_t len; /* Length of run in buffer */
  355. } field_data[(int)(url_fields::UF_MAX)];
  356. };
  357. /* Returns the library version. Bits 16-23 contain the major version number,
  358. * bits 8-15 the minor version number and bits 0-7 the patch level.
  359. * Usage example:
  360. *
  361. * unsigned long version = http_parser_version();
  362. * unsigned major = (version >> 16) & 255;
  363. * unsigned minor = (version >> 8) & 255;
  364. * unsigned patch = version & 255;
  365. * printf("http_parser v%u.%u.%u\n", major, minor, patch);
  366. */
  367. //unsigned long http_parser_version(void);
  368. //void http_parser_init(http_parser *parser, http_parser_type type);
  369. /* Initialize http_parser_settings members to 0
  370. */
  371. //void http_parser_settings_init(http_parser_settings *settings);
  372. /* Executes the parser. Returns number of parsed bytes. Sets
  373. * `parser->http_errno` on error. */
  374. //size_t http_parser_execute(http_parser *parser,
  375. // const http_parser_settings *settings,
  376. // const char *data,
  377. // size_t len);
  378. /* If http_should_keep_alive() in the on_headers_complete or
  379. * on_message_complete callback returns 0, then this should be
  380. * the last message on the connection.
  381. * If you are the server, respond with the "Connection: close" header.
  382. * If you are the client, close the connection.
  383. */
  384. //int http_should_keep_alive(const http_parser *parser);
  385. /* Returns a string version of the HTTP method. */
  386. //const char *http_method_str(http_method m);
  387. /* Return a string name of the given error */
  388. //const char *http_errno_name(http_errno err);
  389. /* Return a string description of the given error */
  390. //const char *http_errno_description(http_errno err);
  391. /* Initialize all http_parser_url members to 0 */
  392. //void http_parser_url_init(struct http_parser_url *u);
  393. /* Parse a URL; return nonzero on failure */
  394. //int http_parser_parse_url(const char *buf, size_t buflen,
  395. // int is_connect,
  396. // struct http_parser_url *u);
  397. /* Pause or un-pause the parser; a nonzero value pauses */
  398. //void http_parser_pause(http_parser *parser, int paused);
  399. /* Checks if this is the final chunk of the body. */
  400. //int http_body_is_final(const http_parser *parser);
  401. // ---------------------------------------------------------------------------- http_parser.c ---------------------------------------------------------------------------- //
  402. /* Based on src/http/ngx_http_parse.c from NGINX copyright Igor Sysoev
  403. *
  404. * Additional changes are licensed under the same terms as NGINX and
  405. * copyright Joyent, Inc. and other Node contributors. All rights reserved.
  406. *
  407. * Permission is hereby granted, free of charge, to any person obtaining a copy
  408. * of this software and associated documentation files (the "Software"), to
  409. * deal in the Software without restriction, including without limitation the
  410. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  411. * sell copies of the Software, and to permit persons to whom the Software is
  412. * furnished to do so, subject to the following conditions:
  413. *
  414. * The above copyright notice and this permission notice shall be included in
  415. * all copies or substantial portions of the Software.
  416. *
  417. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  418. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  419. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  420. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  421. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  422. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  423. * IN THE SOFTWARE.
  424. */
  425. //#include "http_parser.h"
  426. #include <assert.h>
  427. #include <stddef.h>
  428. #include <ctype.h>
  429. #include <stdlib.h>
  430. #include <string.h>
  431. #include <limits.h>
  432. static uint32_t max_header_size = HTTP_MAX_HEADER_SIZE;
  433. #ifndef HTTP_ULLONG_MAX
  434. # define HTTP_ULLONG_MAX ((uint64_t) -1) /* 2^64-1 */
  435. #endif
  436. #ifndef HTTP_MIN
  437. # define HTTP_MIN(a,b) ((a) < (b) ? (a) : (b))
  438. #endif
  439. #ifndef HTTP_ARRAY_SIZE
  440. # define HTTP_ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
  441. #endif
  442. #ifndef HTTP_BIT_AT
  443. # define HTTP_BIT_AT(a, i) \
  444. (!!((unsigned int) (a)[(unsigned int) (i) >> 3] & \
  445. (1 << ((unsigned int) (i) & 7))))
  446. #endif
  447. #ifndef HTTP_ELEM_AT
  448. # define HTTP_ELEM_AT(a, i, v) ((unsigned int) (i) < HTTP_ARRAY_SIZE(a) ? (a)[(unsigned int)(i)] : (v))
  449. #endif
  450. #define HTTP_SET_ERRNO(e) \
  451. do { \
  452. parser->nread = nread; \
  453. parser->http_errno = (unsigned int)(e); \
  454. } while(0)
  455. #define HTTP_CURRENT_STATE() p_state
  456. #define HTTP_UPDATE_STATE(V) p_state = (state) (V);
  457. #define HTTP_RETURN(V) \
  458. do { \
  459. parser->nread = nread; \
  460. parser->state = (unsigned int)(HTTP_CURRENT_STATE()); \
  461. return (V); \
  462. } while (0);
  463. #define HTTP_REEXECUTE() \
  464. goto reexecute; \
  465. #ifdef __GNUC__
  466. # define HTTP_LIKELY(X) __builtin_expect(!!(X), 1)
  467. # define HTTP_UNLIKELY(X) __builtin_expect(!!(X), 0)
  468. #else
  469. # define HTTP_LIKELY(X) (X)
  470. # define HTTP_UNLIKELY(X) (X)
  471. #endif
  472. /* Run the notify callback FOR, returning ER if it fails */
  473. #define HTTP_CALLBACK_NOTIFY_(FOR, ER) \
  474. do { \
  475. assert(HTTP_PARSER_ERRNO(parser) == http_errno::HPE_OK); \
  476. \
  477. if (HTTP_LIKELY(settings->on_##FOR)) { \
  478. parser->state = (unsigned int)(HTTP_CURRENT_STATE()); \
  479. if (HTTP_UNLIKELY(0 != settings->on_##FOR(parser, user_data))) { \
  480. HTTP_SET_ERRNO(http_errno::HPE_CB_##FOR); \
  481. } \
  482. HTTP_UPDATE_STATE(parser->state); \
  483. \
  484. /* We either errored above or got paused; get out */ \
  485. if (HTTP_UNLIKELY(HTTP_PARSER_ERRNO(parser) != http_errno::HPE_OK)) { \
  486. return (ER); \
  487. } \
  488. } \
  489. } while (0)
  490. /* Run the notify callback FOR and consume the current byte */
  491. #define HTTP_CALLBACK_NOTIFY(FOR) HTTP_CALLBACK_NOTIFY_(FOR, p - data + 1)
  492. /* Run the notify callback FOR and don't consume the current byte */
  493. #define HTTP_CALLBACK_NOTIFY_NOADVANCE(FOR) HTTP_CALLBACK_NOTIFY_(FOR, p - data)
  494. /* Run data callback FOR with LEN bytes, returning ER if it fails */
  495. #define HTTP_CALLBACK_DATA_(FOR, LEN, ER) \
  496. do { \
  497. assert(HTTP_PARSER_ERRNO(parser) == http_errno::HPE_OK); \
  498. \
  499. if (FOR##_mark) { \
  500. if (HTTP_LIKELY(settings->on_##FOR)) { \
  501. parser->state = (unsigned int)(HTTP_CURRENT_STATE()); \
  502. if (HTTP_UNLIKELY(0 != \
  503. settings->on_##FOR(parser, FOR##_mark, (LEN), user_data))) { \
  504. HTTP_SET_ERRNO(http_errno::HPE_CB_##FOR); \
  505. } \
  506. HTTP_UPDATE_STATE(parser->state); \
  507. \
  508. /* We either errored above or got paused; get out */ \
  509. if (HTTP_UNLIKELY(HTTP_PARSER_ERRNO(parser) != http_errno::HPE_OK)) { \
  510. return (ER); \
  511. } \
  512. } \
  513. FOR##_mark = NULL; \
  514. } \
  515. } while (0)
  516. /* Run the data callback FOR and consume the current byte */
  517. #define HTTP_CALLBACK_DATA(FOR) \
  518. HTTP_CALLBACK_DATA_(FOR, p - FOR##_mark, p - data + 1)
  519. /* Run the data callback FOR and don't consume the current byte */
  520. #define HTTP_CALLBACK_DATA_NOADVANCE(FOR) \
  521. HTTP_CALLBACK_DATA_(FOR, p - FOR##_mark, p - data)
  522. /* Set the mark FOR; non-destructive if mark is already set */
  523. #define HTTP_MARK(FOR) \
  524. do { \
  525. if (!FOR##_mark) { \
  526. FOR##_mark = p; \
  527. } \
  528. } while (0)
  529. /* Don't allow the total size of the HTTP headers (including the status
  530. * line) to exceed max_header_size. This check is here to protect
  531. * embedders against denial-of-service attacks where the attacker feeds
  532. * us a never-ending header that the embedder keeps buffering.
  533. *
  534. * This check is arguably the responsibility of embedders but we're doing
  535. * it on the embedder's behalf because most won't bother and this way we
  536. * make the web a little safer. max_header_size is still far bigger
  537. * than any reasonable request or response so this should never affect
  538. * day-to-day operation.
  539. */
  540. #define HTTP_COUNT_HEADER_SIZE(V) \
  541. do { \
  542. nread += (uint32_t)(V); \
  543. if (HTTP_UNLIKELY(nread > max_header_size)) { \
  544. HTTP_SET_ERRNO(http_errno::HPE_HEADER_OVERFLOW); \
  545. goto error; \
  546. } \
  547. } while (0)
  548. #define HTTP_PROXY_CONNECTION "proxy-connection"
  549. #define HTTP_CONNECTION "connection"
  550. #define HTTP_CONTENT_LENGTH "content-length"
  551. #define HTTP_TRANSFER_ENCODING "transfer-encoding"
  552. #define HTTP_UPGRADE "upgrade"
  553. #define HTTP_CHUNKED "chunked"
  554. #define HTTP_KEEP_ALIVE "keep-alive"
  555. #define HTTP_CLOSE "close"
  556. static const char *method_strings[] =
  557. {
  558. #define HTTP_XX(num, name, string) #string,
  559. HTTP_METHOD_MAP(HTTP_XX)
  560. #undef HTTP_XX
  561. };
  562. /* Tokens as defined by rfc 2616. Also lowercases them.
  563. * token = 1*<any CHAR except CTLs or separators>
  564. * separators = "(" | ")" | "<" | ">" | "@"
  565. * | "," | ";" | ":" | "\" | <">
  566. * | "/" | "[" | "]" | "?" | "="
  567. * | "{" | "}" | SP | HT
  568. */
  569. static const char tokens[256] = {
  570. /* 0 nul 1 soh 2 stx 3 etx 4 eot 5 enq 6 ack 7 bel */
  571. 0, 0, 0, 0, 0, 0, 0, 0,
  572. /* 8 bs 9 ht 10 nl 11 vt 12 np 13 cr 14 so 15 si */
  573. 0, 0, 0, 0, 0, 0, 0, 0,
  574. /* 16 dle 17 dc1 18 dc2 19 dc3 20 dc4 21 nak 22 syn 23 etb */
  575. 0, 0, 0, 0, 0, 0, 0, 0,
  576. /* 24 can 25 em 26 sub 27 esc 28 fs 29 gs 30 rs 31 us */
  577. 0, 0, 0, 0, 0, 0, 0, 0,
  578. /* 32 sp 33 ! 34 " 35 # 36 $ 37 % 38 & 39 ' */
  579. ' ', '!', 0, '#', '$', '%', '&', '\'',
  580. /* 40 ( 41 ) 42 * 43 + 44 , 45 - 46 . 47 / */
  581. 0, 0, '*', '+', 0, '-', '.', 0,
  582. /* 48 0 49 1 50 2 51 3 52 4 53 5 54 6 55 7 */
  583. '0', '1', '2', '3', '4', '5', '6', '7',
  584. /* 56 8 57 9 58 : 59 ; 60 < 61 = 62 > 63 ? */
  585. '8', '9', 0, 0, 0, 0, 0, 0,
  586. /* 64 @ 65 A 66 B 67 C 68 D 69 E 70 F 71 G */
  587. 0, 'a', 'b', 'c', 'd', 'e', 'f', 'g',
  588. /* 72 H 73 I 74 J 75 K 76 L 77 M 78 N 79 O */
  589. 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
  590. /* 80 P 81 Q 82 R 83 S 84 T 85 U 86 V 87 W */
  591. 'p', 'q', 'r', 's', 't', 'u', 'v', 'w',
  592. /* 88 X 89 Y 90 Z 91 [ 92 \ 93 ] 94 ^ 95 _ */
  593. 'x', 'y', 'z', 0, 0, 0, '^', '_',
  594. /* 96 ` 97 a 98 b 99 c 100 d 101 e 102 f 103 g */
  595. '`', 'a', 'b', 'c', 'd', 'e', 'f', 'g',
  596. /* 104 h 105 i 106 j 107 k 108 l 109 m 110 n 111 o */
  597. 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
  598. /* 112 p 113 q 114 r 115 s 116 t 117 u 118 v 119 w */
  599. 'p', 'q', 'r', 's', 't', 'u', 'v', 'w',
  600. /* 120 x 121 y 122 z 123 { 124 | 125 } 126 ~ 127 del */
  601. 'x', 'y', 'z', 0, '|', 0, '~', 0 };
  602. static const int8_t unhex[256] =
  603. {-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1
  604. ,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1
  605. ,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1
  606. , 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,-1,-1,-1,-1,-1,-1
  607. ,-1,10,11,12,13,14,15,-1,-1,-1,-1,-1,-1,-1,-1,-1
  608. ,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1
  609. ,-1,10,11,12,13,14,15,-1,-1,-1,-1,-1,-1,-1,-1,-1
  610. ,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1
  611. };
  612. #if HTTP_PARSER_STRICT
  613. # define T(v) 0
  614. #else
  615. # define T(v) v
  616. #endif
  617. static const uint8_t normal_url_char[32] = {
  618. /* 0 nul 1 soh 2 stx 3 etx 4 eot 5 enq 6 ack 7 bel */
  619. 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0,
  620. /* 8 bs 9 ht 10 nl 11 vt 12 np 13 cr 14 so 15 si */
  621. 0 | T(2) | 0 | 0 | T(16) | 0 | 0 | 0,
  622. /* 16 dle 17 dc1 18 dc2 19 dc3 20 dc4 21 nak 22 syn 23 etb */
  623. 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0,
  624. /* 24 can 25 em 26 sub 27 esc 28 fs 29 gs 30 rs 31 us */
  625. 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0,
  626. /* 32 sp 33 ! 34 " 35 # 36 $ 37 % 38 & 39 ' */
  627. 0 | 2 | 4 | 0 | 16 | 32 | 64 | 128,
  628. /* 40 ( 41 ) 42 * 43 + 44 , 45 - 46 . 47 / */
  629. 1 | 2 | 4 | 8 | 16 | 32 | 64 | 128,
  630. /* 48 0 49 1 50 2 51 3 52 4 53 5 54 6 55 7 */
  631. 1 | 2 | 4 | 8 | 16 | 32 | 64 | 128,
  632. /* 56 8 57 9 58 : 59 ; 60 < 61 = 62 > 63 ? */
  633. 1 | 2 | 4 | 8 | 16 | 32 | 64 | 0,
  634. /* 64 @ 65 A 66 B 67 C 68 D 69 E 70 F 71 G */
  635. 1 | 2 | 4 | 8 | 16 | 32 | 64 | 128,
  636. /* 72 H 73 I 74 J 75 K 76 L 77 M 78 N 79 O */
  637. 1 | 2 | 4 | 8 | 16 | 32 | 64 | 128,
  638. /* 80 P 81 Q 82 R 83 S 84 T 85 U 86 V 87 W */
  639. 1 | 2 | 4 | 8 | 16 | 32 | 64 | 128,
  640. /* 88 X 89 Y 90 Z 91 [ 92 \ 93 ] 94 ^ 95 _ */
  641. 1 | 2 | 4 | 8 | 16 | 32 | 64 | 128,
  642. /* 96 ` 97 a 98 b 99 c 100 d 101 e 102 f 103 g */
  643. 1 | 2 | 4 | 8 | 16 | 32 | 64 | 128,
  644. /* 104 h 105 i 106 j 107 k 108 l 109 m 110 n 111 o */
  645. 1 | 2 | 4 | 8 | 16 | 32 | 64 | 128,
  646. /* 112 p 113 q 114 r 115 s 116 t 117 u 118 v 119 w */
  647. 1 | 2 | 4 | 8 | 16 | 32 | 64 | 128,
  648. /* 120 x 121 y 122 z 123 { 124 | 125 } 126 ~ 127 del */
  649. 1 | 2 | 4 | 8 | 16 | 32 | 64 | 0, };
  650. #undef T
  651. enum class state
  652. { s_dead = 1 /* important that this is > 0 */
  653. , s_start_req_or_res
  654. , s_res_or_resp_H
  655. , s_start_res
  656. , s_res_H
  657. , s_res_HT
  658. , s_res_HTT
  659. , s_res_HTTP
  660. , s_res_http_major
  661. , s_res_http_dot
  662. , s_res_http_minor
  663. , s_res_http_end
  664. , s_res_first_status_code
  665. , s_res_status_code
  666. , s_res_status_start
  667. , s_res_status
  668. , s_res_line_almost_done
  669. , s_start_req
  670. , s_req_method
  671. , s_req_spaces_before_url
  672. , s_req_schema
  673. , s_req_schema_slash
  674. , s_req_schema_slash_slash
  675. , s_req_server_start
  676. , s_req_server
  677. , s_req_server_with_at
  678. , s_req_path
  679. , s_req_query_string_start
  680. , s_req_query_string
  681. , s_req_fragment_start
  682. , s_req_fragment
  683. , s_req_http_start
  684. , s_req_http_H
  685. , s_req_http_HT
  686. , s_req_http_HTT
  687. , s_req_http_HTTP
  688. , s_req_http_I
  689. , s_req_http_IC
  690. , s_req_http_major
  691. , s_req_http_dot
  692. , s_req_http_minor
  693. , s_req_http_end
  694. , s_req_line_almost_done
  695. , s_header_field_start
  696. , s_header_field
  697. , s_header_value_discard_ws
  698. , s_header_value_discard_ws_almost_done
  699. , s_header_value_discard_lws
  700. , s_header_value_start
  701. , s_header_value
  702. , s_header_value_lws
  703. , s_header_almost_done
  704. , s_chunk_size_start
  705. , s_chunk_size
  706. , s_chunk_parameters
  707. , s_chunk_size_almost_done
  708. , s_headers_almost_done
  709. , s_headers_done
  710. /* Important: 's_headers_done' must be the last 'header' state. All
  711. * states beyond this must be 'body' states. It is used for overflow
  712. * checking. See the HTTP_PARSING_HEADER() macro.
  713. */
  714. , s_chunk_data
  715. , s_chunk_data_almost_done
  716. , s_chunk_data_done
  717. , s_body_identity
  718. , s_body_identity_eof
  719. , s_message_done
  720. };
  721. #define HTTP_PARSING_HEADER(_state) (_state <= state::s_headers_done)
  722. enum class header_states
  723. { h_general = 0
  724. , h_C
  725. , h_CO
  726. , h_CON
  727. , h_matching_connection
  728. , h_matching_proxy_connection
  729. , h_matching_content_length
  730. , h_matching_transfer_encoding
  731. , h_matching_upgrade
  732. , h_connection
  733. , h_content_length
  734. , h_content_length_num
  735. , h_content_length_ws
  736. , h_transfer_encoding
  737. , h_upgrade
  738. , h_matching_transfer_encoding_token_start
  739. , h_matching_transfer_encoding_chunked
  740. , h_matching_transfer_encoding_token
  741. , h_matching_connection_token_start
  742. , h_matching_connection_keep_alive
  743. , h_matching_connection_close
  744. , h_matching_connection_upgrade
  745. , h_matching_connection_token
  746. , h_transfer_encoding_chunked
  747. , h_connection_keep_alive
  748. , h_connection_close
  749. , h_connection_upgrade
  750. };
  751. enum class http_host_state
  752. {
  753. s_http_host_dead = 1
  754. , s_http_userinfo_start
  755. , s_http_userinfo
  756. , s_http_host_start
  757. , s_http_host_v6_start
  758. , s_http_host
  759. , s_http_host_v6
  760. , s_http_host_v6_end
  761. , s_http_host_v6_zone_start
  762. , s_http_host_v6_zone
  763. , s_http_host_port_start
  764. , s_http_host_port
  765. };
  766. /* Macros for character classes; depends on strict-mode */
  767. #define HTTP_CR '\r'
  768. #define HTTP_LF '\n'
  769. #define HTTP_LOWER(c) (unsigned char)(c | 0x20)
  770. #define HTTP_IS_ALPHA(c) (HTTP_LOWER(c) >= 'a' && HTTP_LOWER(c) <= 'z')
  771. #define HTTP_IS_NUM(c) ((c) >= '0' && (c) <= '9')
  772. #define HTTP_IS_ALPHANUM(c) (HTTP_IS_ALPHA(c) || HTTP_IS_NUM(c))
  773. #define HTTP_IS_HEX(c) (HTTP_IS_NUM(c) || (HTTP_LOWER(c) >= 'a' && HTTP_LOWER(c) <= 'f'))
  774. #define HTTP_IS_MARK(c) ((c) == '-' || (c) == '_' || (c) == '.' || \
  775. (c) == '!' || (c) == '~' || (c) == '*' || (c) == '\'' || (c) == '(' || \
  776. (c) == ')')
  777. #define HTTP_IS_USERINFO_CHAR(c) (HTTP_IS_ALPHANUM(c) || HTTP_IS_MARK(c) || (c) == '%' || \
  778. (c) == ';' || (c) == ':' || (c) == '&' || (c) == '=' || (c) == '+' || \
  779. (c) == '$' || (c) == ',')
  780. #define HTTP_STRICT_TOKEN(c) ((c == ' ') ? 0 : tokens[(unsigned char)c])
  781. #if HTTP_PARSER_STRICT
  782. #define HTTP_TOKEN(c) HTTP_STRICT_TOKEN(c)
  783. #define HTTP_IS_URL_CHAR(c) (HTTP_BIT_AT(normal_url_char, (unsigned char)c))
  784. #define HTTP_IS_HOST_CHAR(c) (HTTP_IS_ALPHANUM(c) || (c) == '.' || (c) == '-')
  785. #else
  786. #define HTTP_TOKEN(c) tokens[(unsigned char)c]
  787. #define HTTP_IS_URL_CHAR(c) \
  788. (HTTP_BIT_AT(normal_url_char, (unsigned char)c) || ((c) & 0x80))
  789. #define HTTP_IS_HOST_CHAR(c) \
  790. (HTTP_IS_ALPHANUM(c) || (c) == '.' || (c) == '-' || (c) == '_')
  791. #endif
  792. /**
  793. * Verify that a char is a valid visible (printable) US-ASCII
  794. * character or %x80-FF
  795. **/
  796. #define HTTP_IS_HEADER_CHAR(ch) \
  797. (ch == HTTP_CR || ch == HTTP_LF || ch == 9 || ((unsigned char)ch > 31 && ch != 127))
  798. #define start_state (parser->type == (unsigned int)(http_parser_type::HTTP_REQUEST) ? state::s_start_req : state::s_start_res)
  799. #if HTTP_PARSER_STRICT
  800. # define HTTP_STRICT_CHECK(cond) \
  801. do { \
  802. if (cond) { \
  803. HTTP_SET_ERRNO(http_errno::HPE_STRICT); \
  804. goto error; \
  805. } \
  806. } while (0)
  807. # define HTTP_NEW_MESSAGE() (http_should_keep_alive(parser) ? start_state : state::s_dead)
  808. #else
  809. # define HTTP_STRICT_CHECK(cond)
  810. # define HTTP_NEW_MESSAGE() start_state
  811. #endif
  812. /* Map errno values to strings for human-readable output */
  813. #define HTTP_STRERROR_GEN(n, s) { "HPE_" #n, s },
  814. static struct {
  815. const char *name;
  816. const char *description;
  817. } http_strerror_tab[] = {
  818. HTTP_ERRNO_MAP(HTTP_STRERROR_GEN)
  819. };
  820. #undef HTTP_STRERROR_GEN
  821. //int http_message_needs_eof(const http_parser *parser);
  822. /* Does the parser need to see an EOF to find the end of the message? */
  823. template<typename = void>
  824. int
  825. http_message_needs_eof (const http_parser *parser)
  826. {
  827. if (parser->type == (unsigned int)(http_parser_type::HTTP_REQUEST)) {
  828. return 0;
  829. }
  830. /* See RFC 2616 section 4.4 */
  831. if (parser->status_code / 100 == 1 || /* 1xx e.g. Continue */
  832. parser->status_code == 204 || /* No Content */
  833. parser->status_code == 304 || /* Not Modified */
  834. parser->flags & (unsigned int)(flags::F_SKIPBODY)) { /* response to a HEAD request */
  835. return 0;
  836. }
  837. /* RFC 7230 3.3.3, see `s_headers_almost_done` */
  838. if ((parser->uses_transfer_encoding == 1) &&
  839. (parser->flags & (unsigned int)(flags::F_CHUNKED)) == 0) {
  840. return 1;
  841. }
  842. if ((parser->flags & (unsigned int)(flags::F_CHUNKED)) || parser->content_length != HTTP_ULLONG_MAX) {
  843. return 0;
  844. }
  845. return 1;
  846. }
  847. template<typename = void>
  848. int
  849. http_should_keep_alive (const http_parser *parser)
  850. {
  851. if (parser->http_major > 0 && parser->http_minor > 0) {
  852. /* HTTP/1.1 */
  853. if (parser->flags & (unsigned int)(flags::F_CONNECTION_CLOSE)) {
  854. return 0;
  855. }
  856. } else {
  857. /* HTTP/1.0 or earlier */
  858. if (!(parser->flags & (unsigned int)(flags::F_CONNECTION_KEEP_ALIVE))) {
  859. return 0;
  860. }
  861. }
  862. return !http_message_needs_eof(parser);
  863. }
  864. /* Our URL parser.
  865. *
  866. * This is designed to be shared by http_parser_execute() for URL validation,
  867. * hence it has a state transition + byte-for-byte interface. In addition, it
  868. * is meant to be embedded in http_parser_parse_url(), which does the dirty
  869. * work of turning state transitions URL components for its API.
  870. *
  871. * This function should only be invoked with non-space characters. It is
  872. * assumed that the caller cares about (and can detect) the transition between
  873. * URL and non-URL states by looking for these.
  874. */
  875. template<typename = void>
  876. static state
  877. parse_url_char(state s, const char ch)
  878. {
  879. // if (ch == ' ' || ch == '\r' || ch == '\n') {
  880. // return state::s_dead;
  881. // }
  882. //
  883. //#if HTTP_PARSER_STRICT
  884. // if (ch == '\t' || ch == '\f') {
  885. // return state::s_dead;
  886. // }
  887. //#endif
  888. switch (s) {
  889. case state::s_req_spaces_before_url:
  890. /* Proxied requests are followed by scheme of an absolute URI (alpha).
  891. * All methods except CONNECT are followed by '/' or '*'.
  892. */
  893. if (ch == '/' || ch == '*') {
  894. return state::s_req_path;
  895. }
  896. if (HTTP_IS_ALPHA(ch)) {
  897. return state::s_req_schema;
  898. }
  899. break;
  900. case state::s_req_schema:
  901. if (HTTP_IS_ALPHA(ch)) {
  902. return s;
  903. }
  904. if (ch == ':') {
  905. return state::s_req_schema_slash;
  906. }
  907. break;
  908. case state::s_req_schema_slash:
  909. if (ch == '/') {
  910. return state::s_req_schema_slash_slash;
  911. }
  912. break;
  913. case state::s_req_schema_slash_slash:
  914. if (ch == '/') {
  915. return state::s_req_server_start;
  916. }
  917. break;
  918. case state::s_req_server_with_at:
  919. if (ch == '@') {
  920. return state::s_dead;
  921. }
  922. /* fall through */
  923. case state::s_req_server_start:
  924. case state::s_req_server:
  925. if (ch == '/') {
  926. return state::s_req_path;
  927. }
  928. if (ch == '?') {
  929. return state::s_req_query_string_start;
  930. }
  931. if (ch == '@') {
  932. return state::s_req_server_with_at;
  933. }
  934. if (HTTP_IS_USERINFO_CHAR(ch) || ch == '[' || ch == ']') {
  935. return state::s_req_server;
  936. }
  937. break;
  938. case state::s_req_path:
  939. if (HTTP_IS_URL_CHAR(ch)) {
  940. return s;
  941. }
  942. switch (ch) {
  943. case '?':
  944. return state::s_req_query_string_start;
  945. case '#':
  946. return state::s_req_fragment_start;
  947. default:
  948. return s;
  949. }
  950. break;
  951. case state::s_req_query_string_start:
  952. case state::s_req_query_string:
  953. if (HTTP_IS_URL_CHAR(ch)) {
  954. return state::s_req_query_string;
  955. }
  956. switch (ch) {
  957. case '?':
  958. /* allow extra '?' in query string */
  959. return state::s_req_query_string;
  960. case '#':
  961. return state::s_req_fragment_start;
  962. default:
  963. return state::s_req_query_string;
  964. }
  965. break;
  966. case state::s_req_fragment_start:
  967. if (HTTP_IS_URL_CHAR(ch)) {
  968. return state::s_req_fragment;
  969. }
  970. switch (ch) {
  971. case '?':
  972. return state::s_req_fragment;
  973. case '#':
  974. return s;
  975. default:
  976. return state::s_req_fragment;
  977. }
  978. break;
  979. case state::s_req_fragment:
  980. if (HTTP_IS_URL_CHAR(ch)) {
  981. return s;
  982. }
  983. switch (ch) {
  984. case '?':
  985. case '#':
  986. return s;
  987. default:
  988. return s;
  989. }
  990. break;
  991. default:
  992. break;
  993. }
  994. /* We should never fall out of the switch above unless there's an error */
  995. return state::s_dead;
  996. }
  997. template<typename = void>
  998. size_t http_parser_execute (http_parser *parser,
  999. const http_parser_settings *settings,
  1000. const char *data,
  1001. size_t len,
  1002. void * user_data = nullptr
  1003. )
  1004. {
  1005. char c, ch;
  1006. int8_t unhex_val;
  1007. const char *p = data;
  1008. const char *header_field_mark = 0;
  1009. const char *header_value_mark = 0;
  1010. const char *url_mark = 0;
  1011. const char *body_mark = 0;
  1012. const char *status_mark = 0;
  1013. state p_state = (state) parser->state;
  1014. const unsigned int lenient = parser->lenient_http_headers;
  1015. const unsigned int allow_chunked_length = parser->allow_chunked_length;
  1016. uint32_t nread = parser->nread;
  1017. /* We're in an error state. Don't bother doing anything. */
  1018. if (HTTP_PARSER_ERRNO(parser) != http_errno::HPE_OK) {
  1019. return 0;
  1020. }
  1021. if (len == 0) {
  1022. switch (HTTP_CURRENT_STATE()) {
  1023. case state::s_body_identity_eof:
  1024. /* Use of HTTP_CALLBACK_NOTIFY() here would erroneously return 1 byte read if
  1025. * we got paused.
  1026. */
  1027. HTTP_CALLBACK_NOTIFY_NOADVANCE(message_complete);
  1028. return 0;
  1029. case state::s_dead:
  1030. case state::s_start_req_or_res:
  1031. case state::s_start_res:
  1032. case state::s_start_req:
  1033. return 0;
  1034. default:
  1035. HTTP_SET_ERRNO(http_errno::HPE_INVALID_EOF_STATE);
  1036. return 1;
  1037. }
  1038. }
  1039. if (HTTP_CURRENT_STATE() == state::s_header_field)
  1040. header_field_mark = data;
  1041. if (HTTP_CURRENT_STATE() == state::s_header_value)
  1042. header_value_mark = data;
  1043. switch (HTTP_CURRENT_STATE()) {
  1044. case state::s_req_path:
  1045. case state::s_req_schema:
  1046. case state::s_req_schema_slash:
  1047. case state::s_req_schema_slash_slash:
  1048. case state::s_req_server_start:
  1049. case state::s_req_server:
  1050. case state::s_req_server_with_at:
  1051. case state::s_req_query_string_start:
  1052. case state::s_req_query_string:
  1053. case state::s_req_fragment_start:
  1054. case state::s_req_fragment:
  1055. url_mark = data;
  1056. break;
  1057. case state::s_res_status:
  1058. status_mark = data;
  1059. break;
  1060. default:
  1061. break;
  1062. }
  1063. for (p=data; p != data + len; ++p) {
  1064. ch = *p;
  1065. if (HTTP_PARSING_HEADER(HTTP_CURRENT_STATE()))
  1066. HTTP_COUNT_HEADER_SIZE(1);
  1067. reexecute:
  1068. switch (HTTP_CURRENT_STATE()) {
  1069. case state::s_dead:
  1070. /* this state is used after a 'Connection: close' message
  1071. * the parser will error out if it reads another message
  1072. */
  1073. if (HTTP_LIKELY(ch == HTTP_CR || ch == HTTP_LF))
  1074. break;
  1075. HTTP_SET_ERRNO(http_errno::HPE_CLOSED_CONNECTION);
  1076. goto error;
  1077. case state::s_start_req_or_res:
  1078. {
  1079. if (ch == HTTP_CR || ch == HTTP_LF)
  1080. break;
  1081. parser->flags = (unsigned int)(0);
  1082. parser->uses_transfer_encoding = 0;
  1083. parser->content_length = HTTP_ULLONG_MAX;
  1084. if (ch == 'H') {
  1085. HTTP_UPDATE_STATE(state::s_res_or_resp_H);
  1086. HTTP_CALLBACK_NOTIFY(message_begin);
  1087. } else {
  1088. parser->type = (unsigned int)(http_parser_type::HTTP_REQUEST);
  1089. HTTP_UPDATE_STATE(state::s_start_req);
  1090. HTTP_REEXECUTE();
  1091. }
  1092. break;
  1093. }
  1094. case state::s_res_or_resp_H:
  1095. if (ch == 'T') {
  1096. parser->type = (unsigned int)(http_parser_type::HTTP_RESPONSE);
  1097. HTTP_UPDATE_STATE(state::s_res_HT);
  1098. } else {
  1099. if (HTTP_UNLIKELY(ch != 'E')) {
  1100. HTTP_SET_ERRNO(http_errno::HPE_INVALID_CONSTANT);
  1101. goto error;
  1102. }
  1103. parser->type = (unsigned int)(http_parser_type::HTTP_REQUEST);
  1104. parser->method = (unsigned int)(http_method::HTTP_HEAD);
  1105. parser->index = 2;
  1106. HTTP_UPDATE_STATE(state::s_req_method);
  1107. }
  1108. break;
  1109. case state::s_start_res:
  1110. {
  1111. if (ch == HTTP_CR || ch == HTTP_LF)
  1112. break;
  1113. parser->flags = (unsigned int)(0);
  1114. parser->uses_transfer_encoding = 0;
  1115. parser->content_length = HTTP_ULLONG_MAX;
  1116. if (ch == 'H') {
  1117. HTTP_UPDATE_STATE(state::s_res_H);
  1118. } else {
  1119. HTTP_SET_ERRNO(http_errno::HPE_INVALID_CONSTANT);
  1120. goto error;
  1121. }
  1122. HTTP_CALLBACK_NOTIFY(message_begin);
  1123. break;
  1124. }
  1125. case state::s_res_H:
  1126. HTTP_STRICT_CHECK(ch != 'T');
  1127. HTTP_UPDATE_STATE(state::s_res_HT);
  1128. break;
  1129. case state::s_res_HT:
  1130. HTTP_STRICT_CHECK(ch != 'T');
  1131. HTTP_UPDATE_STATE(state::s_res_HTT);
  1132. break;
  1133. case state::s_res_HTT:
  1134. HTTP_STRICT_CHECK(ch != 'P');
  1135. HTTP_UPDATE_STATE(state::s_res_HTTP);
  1136. break;
  1137. case state::s_res_HTTP:
  1138. HTTP_STRICT_CHECK(ch != '/');
  1139. HTTP_UPDATE_STATE(state::s_res_http_major);
  1140. break;
  1141. case state::s_res_http_major:
  1142. if (HTTP_UNLIKELY(!HTTP_IS_NUM(ch))) {
  1143. HTTP_SET_ERRNO(http_errno::HPE_INVALID_VERSION);
  1144. goto error;
  1145. }
  1146. parser->http_major = ch - '0';
  1147. HTTP_UPDATE_STATE(state::s_res_http_dot);
  1148. break;
  1149. case state::s_res_http_dot:
  1150. {
  1151. if (HTTP_UNLIKELY(ch != '.')) {
  1152. HTTP_SET_ERRNO(http_errno::HPE_INVALID_VERSION);
  1153. goto error;
  1154. }
  1155. HTTP_UPDATE_STATE(state::s_res_http_minor);
  1156. break;
  1157. }
  1158. case state::s_res_http_minor:
  1159. if (HTTP_UNLIKELY(!HTTP_IS_NUM(ch))) {
  1160. HTTP_SET_ERRNO(http_errno::HPE_INVALID_VERSION);
  1161. goto error;
  1162. }
  1163. parser->http_minor = ch - '0';
  1164. HTTP_UPDATE_STATE(state::s_res_http_end);
  1165. break;
  1166. case state::s_res_http_end:
  1167. {
  1168. if (HTTP_UNLIKELY(ch != ' ')) {
  1169. HTTP_SET_ERRNO(http_errno::HPE_INVALID_VERSION);
  1170. goto error;
  1171. }
  1172. HTTP_UPDATE_STATE(state::s_res_first_status_code);
  1173. break;
  1174. }
  1175. case state::s_res_first_status_code:
  1176. {
  1177. if (!HTTP_IS_NUM(ch)) {
  1178. if (ch == ' ') {
  1179. break;
  1180. }
  1181. HTTP_SET_ERRNO(http_errno::HPE_INVALID_STATUS);
  1182. goto error;
  1183. }
  1184. parser->status_code = ch - '0';
  1185. HTTP_UPDATE_STATE(state::s_res_status_code);
  1186. break;
  1187. }
  1188. case state::s_res_status_code:
  1189. {
  1190. if (!HTTP_IS_NUM(ch)) {
  1191. switch (ch) {
  1192. case ' ':
  1193. HTTP_UPDATE_STATE(state::s_res_status_start);
  1194. break;
  1195. case HTTP_CR:
  1196. case HTTP_LF:
  1197. HTTP_UPDATE_STATE(state::s_res_status_start);
  1198. HTTP_REEXECUTE();
  1199. break;
  1200. default:
  1201. HTTP_SET_ERRNO(http_errno::HPE_INVALID_STATUS);
  1202. goto error;
  1203. }
  1204. break;
  1205. }
  1206. parser->status_code *= 10;
  1207. parser->status_code += ch - '0';
  1208. if (HTTP_UNLIKELY(parser->status_code > 999)) {
  1209. HTTP_SET_ERRNO(http_errno::HPE_INVALID_STATUS);
  1210. goto error;
  1211. }
  1212. break;
  1213. }
  1214. case state::s_res_status_start:
  1215. {
  1216. HTTP_MARK(status);
  1217. HTTP_UPDATE_STATE(state::s_res_status);
  1218. parser->index = 0;
  1219. if (ch == HTTP_CR || ch == HTTP_LF)
  1220. HTTP_REEXECUTE();
  1221. break;
  1222. }
  1223. case state::s_res_status:
  1224. if (ch == HTTP_CR) {
  1225. HTTP_UPDATE_STATE(state::s_res_line_almost_done);
  1226. HTTP_CALLBACK_DATA(status);
  1227. break;
  1228. }
  1229. if (ch == HTTP_LF) {
  1230. HTTP_UPDATE_STATE(state::s_header_field_start);
  1231. HTTP_CALLBACK_DATA(status);
  1232. break;
  1233. }
  1234. break;
  1235. case state::s_res_line_almost_done:
  1236. HTTP_STRICT_CHECK(ch != HTTP_LF);
  1237. HTTP_UPDATE_STATE(state::s_header_field_start);
  1238. break;
  1239. case state::s_start_req:
  1240. {
  1241. if (ch == HTTP_CR || ch == HTTP_LF)
  1242. break;
  1243. parser->flags = (unsigned int)(0);
  1244. parser->uses_transfer_encoding = 0;
  1245. parser->content_length = HTTP_ULLONG_MAX;
  1246. if (HTTP_UNLIKELY(!HTTP_IS_ALPHA(ch))) {
  1247. HTTP_SET_ERRNO(http_errno::HPE_INVALID_METHOD);
  1248. goto error;
  1249. }
  1250. parser->method = (unsigned int)(0);
  1251. parser->index = 1;
  1252. switch (ch) {
  1253. case 'A': parser->method = (unsigned int)(http_method::HTTP_ACL ); break;
  1254. case 'B': parser->method = (unsigned int)(http_method::HTTP_BIND ); break;
  1255. case 'C': parser->method = (unsigned int)(http_method::HTTP_CONNECT); /* or COPY, CHECKOUT */ break;
  1256. case 'D': parser->method = (unsigned int)(http_method::HTTP_DELETE ); break;
  1257. case 'G': parser->method = (unsigned int)(http_method::HTTP_GET ); break;
  1258. case 'H': parser->method = (unsigned int)(http_method::HTTP_HEAD ); break;
  1259. case 'L': parser->method = (unsigned int)(http_method::HTTP_LOCK ); /* or LINK */ break;
  1260. case 'M': parser->method = (unsigned int)(http_method::HTTP_MKCOL ); /* or MOVE, MKACTIVITY, MERGE, M-SEARCH, MKCALENDAR */ break;
  1261. case 'N': parser->method = (unsigned int)(http_method::HTTP_NOTIFY ); break;
  1262. case 'O': parser->method = (unsigned int)(http_method::HTTP_OPTIONS); break;
  1263. case 'P': parser->method = (unsigned int)(http_method::HTTP_POST );
  1264. /* or PROPFIND|PROPPATCH|PUT|PATCH|PURGE */
  1265. break;
  1266. case 'R': parser->method = (unsigned int)(http_method::HTTP_REPORT ); /* or REBIND */ break;
  1267. case 'S': parser->method = (unsigned int)(http_method::HTTP_SUBSCRIBE); /* or SEARCH, SOURCE */ break;
  1268. case 'T': parser->method = (unsigned int)(http_method::HTTP_TRACE ); break;
  1269. case 'U': parser->method = (unsigned int)(http_method::HTTP_UNLOCK ); /* or UNSUBSCRIBE, UNBIND, UNLINK */ break;
  1270. default:
  1271. HTTP_SET_ERRNO(http_errno::HPE_INVALID_METHOD);
  1272. goto error;
  1273. }
  1274. HTTP_UPDATE_STATE(state::s_req_method);
  1275. HTTP_CALLBACK_NOTIFY(message_begin);
  1276. break;
  1277. }
  1278. case state::s_req_method:
  1279. {
  1280. const char *matcher;
  1281. if (HTTP_UNLIKELY(ch == '\0')) {
  1282. HTTP_SET_ERRNO(http_errno::HPE_INVALID_METHOD);
  1283. goto error;
  1284. }
  1285. matcher = method_strings[parser->method];
  1286. if (ch == ' ' && matcher[parser->index] == '\0') {
  1287. HTTP_UPDATE_STATE(state::s_req_spaces_before_url);
  1288. } else if (ch == matcher[parser->index]) {
  1289. ; /* nada */
  1290. } else if ((ch >= 'A' && ch <= 'Z') || ch == '-') {
  1291. switch (parser->method << 16 | parser->index << 8 | ch) {
  1292. #define HTTP_XX(meth, pos, ch, new_meth) \
  1293. case ((size_t)http_method::HTTP_##meth << 16 | pos << 8 | ch): \
  1294. parser->method = (unsigned int)(http_method::HTTP_##new_meth); break;
  1295. HTTP_XX(POST, 1, 'U', PUT)
  1296. HTTP_XX(POST, 1, 'A', PATCH)
  1297. HTTP_XX(POST, 1, 'R', PROPFIND)
  1298. HTTP_XX(PUT, 2, 'R', PURGE)
  1299. HTTP_XX(CONNECT, 1, 'H', CHECKOUT)
  1300. HTTP_XX(CONNECT, 2, 'P', COPY)
  1301. HTTP_XX(MKCOL, 1, 'O', MOVE)
  1302. HTTP_XX(MKCOL, 1, 'E', MERGE)
  1303. HTTP_XX(MKCOL, 1, '-', MSEARCH)
  1304. HTTP_XX(MKCOL, 2, 'A', MKACTIVITY)
  1305. HTTP_XX(MKCOL, 3, 'A', MKCALENDAR)
  1306. HTTP_XX(SUBSCRIBE, 1, 'E', SEARCH)
  1307. HTTP_XX(SUBSCRIBE, 1, 'O', SOURCE)
  1308. HTTP_XX(REPORT, 2, 'B', REBIND)
  1309. HTTP_XX(PROPFIND, 4, 'P', PROPPATCH)
  1310. HTTP_XX(LOCK, 1, 'I', LINK)
  1311. HTTP_XX(UNLOCK, 2, 'S', UNSUBSCRIBE)
  1312. HTTP_XX(UNLOCK, 2, 'B', UNBIND)
  1313. HTTP_XX(UNLOCK, 3, 'I', UNLINK)
  1314. #undef HTTP_XX
  1315. default:
  1316. HTTP_SET_ERRNO(http_errno::HPE_INVALID_METHOD);
  1317. goto error;
  1318. }
  1319. } else {
  1320. HTTP_SET_ERRNO(http_errno::HPE_INVALID_METHOD);
  1321. goto error;
  1322. }
  1323. ++parser->index;
  1324. break;
  1325. }
  1326. case state::s_req_spaces_before_url:
  1327. {
  1328. if (ch == ' ') break;
  1329. HTTP_MARK(url);
  1330. if (parser->method == (unsigned int)(http_method::HTTP_CONNECT)) {
  1331. HTTP_UPDATE_STATE(state::s_req_server_start);
  1332. }
  1333. HTTP_UPDATE_STATE(parse_url_char(HTTP_CURRENT_STATE(), ch));
  1334. if (HTTP_UNLIKELY(HTTP_CURRENT_STATE() == state::s_dead)) {
  1335. HTTP_SET_ERRNO(http_errno::HPE_INVALID_URL);
  1336. goto error;
  1337. }
  1338. break;
  1339. }
  1340. case state::s_req_schema:
  1341. case state::s_req_schema_slash:
  1342. case state::s_req_schema_slash_slash:
  1343. case state::s_req_server_start:
  1344. {
  1345. switch (ch) {
  1346. /* No whitespace allowed here */
  1347. case ' ':
  1348. case HTTP_CR:
  1349. case HTTP_LF:
  1350. HTTP_SET_ERRNO(http_errno::HPE_INVALID_URL);
  1351. goto error;
  1352. default:
  1353. HTTP_UPDATE_STATE(parse_url_char(HTTP_CURRENT_STATE(), ch));
  1354. if (HTTP_UNLIKELY(HTTP_CURRENT_STATE() == state::s_dead)) {
  1355. HTTP_SET_ERRNO(http_errno::HPE_INVALID_URL);
  1356. goto error;
  1357. }
  1358. }
  1359. break;
  1360. }
  1361. case state::s_req_server:
  1362. case state::s_req_server_with_at:
  1363. case state::s_req_path:
  1364. case state::s_req_query_string_start:
  1365. case state::s_req_query_string:
  1366. case state::s_req_fragment_start:
  1367. case state::s_req_fragment:
  1368. {
  1369. switch (ch) {
  1370. case ' ':
  1371. HTTP_UPDATE_STATE(state::s_req_http_start);
  1372. HTTP_CALLBACK_DATA(url);
  1373. break;
  1374. case HTTP_CR:
  1375. case HTTP_LF:
  1376. parser->http_major = 0;
  1377. parser->http_minor = 9;
  1378. HTTP_UPDATE_STATE((ch == HTTP_CR) ?
  1379. state::s_req_line_almost_done :
  1380. state::s_header_field_start);
  1381. HTTP_CALLBACK_DATA(url);
  1382. break;
  1383. default:
  1384. HTTP_UPDATE_STATE(parse_url_char(HTTP_CURRENT_STATE(), ch));
  1385. if (HTTP_UNLIKELY(HTTP_CURRENT_STATE() == state::s_dead)) {
  1386. HTTP_SET_ERRNO(http_errno::HPE_INVALID_URL);
  1387. goto error;
  1388. }
  1389. }
  1390. break;
  1391. }
  1392. case state::s_req_http_start:
  1393. switch (ch) {
  1394. case ' ':
  1395. break;
  1396. case 'H':
  1397. HTTP_UPDATE_STATE(state::s_req_http_H);
  1398. break;
  1399. case 'I':
  1400. if (parser->method == (unsigned int)(http_method::HTTP_SOURCE)) {
  1401. HTTP_UPDATE_STATE(state::s_req_http_I);
  1402. break;
  1403. }
  1404. /* fall through */
  1405. default:
  1406. HTTP_SET_ERRNO(http_errno::HPE_INVALID_CONSTANT);
  1407. goto error;
  1408. }
  1409. break;
  1410. case state::s_req_http_H:
  1411. HTTP_STRICT_CHECK(ch != 'T');
  1412. HTTP_UPDATE_STATE(state::s_req_http_HT);
  1413. break;
  1414. case state::s_req_http_HT:
  1415. HTTP_STRICT_CHECK(ch != 'T');
  1416. HTTP_UPDATE_STATE(state::s_req_http_HTT);
  1417. break;
  1418. case state::s_req_http_HTT:
  1419. HTTP_STRICT_CHECK(ch != 'P');
  1420. HTTP_UPDATE_STATE(state::s_req_http_HTTP);
  1421. break;
  1422. case state::s_req_http_I:
  1423. HTTP_STRICT_CHECK(ch != 'C');
  1424. HTTP_UPDATE_STATE(state::s_req_http_IC);
  1425. break;
  1426. case state::s_req_http_IC:
  1427. HTTP_STRICT_CHECK(ch != 'E');
  1428. HTTP_UPDATE_STATE(state::s_req_http_HTTP); /* Treat "ICE" as "HTTP". */
  1429. break;
  1430. case state::s_req_http_HTTP:
  1431. HTTP_STRICT_CHECK(ch != '/');
  1432. HTTP_UPDATE_STATE(state::s_req_http_major);
  1433. break;
  1434. case state::s_req_http_major:
  1435. if (HTTP_UNLIKELY(!HTTP_IS_NUM(ch))) {
  1436. HTTP_SET_ERRNO(http_errno::HPE_INVALID_VERSION);
  1437. goto error;
  1438. }
  1439. parser->http_major = ch - '0';
  1440. HTTP_UPDATE_STATE(state::s_req_http_dot);
  1441. break;
  1442. case state::s_req_http_dot:
  1443. {
  1444. if (HTTP_UNLIKELY(ch != '.')) {
  1445. HTTP_SET_ERRNO(http_errno::HPE_INVALID_VERSION);
  1446. goto error;
  1447. }
  1448. HTTP_UPDATE_STATE(state::s_req_http_minor);
  1449. break;
  1450. }
  1451. case state::s_req_http_minor:
  1452. if (HTTP_UNLIKELY(!HTTP_IS_NUM(ch))) {
  1453. HTTP_SET_ERRNO(http_errno::HPE_INVALID_VERSION);
  1454. goto error;
  1455. }
  1456. parser->http_minor = ch - '0';
  1457. HTTP_UPDATE_STATE(state::s_req_http_end);
  1458. break;
  1459. case state::s_req_http_end:
  1460. {
  1461. if (ch == HTTP_CR) {
  1462. HTTP_UPDATE_STATE(state::s_req_line_almost_done);
  1463. break;
  1464. }
  1465. if (ch == HTTP_LF) {
  1466. HTTP_UPDATE_STATE(state::s_header_field_start);
  1467. break;
  1468. }
  1469. HTTP_SET_ERRNO(http_errno::HPE_INVALID_VERSION);
  1470. goto error;
  1471. break;
  1472. }
  1473. /* end of request line */
  1474. case state::s_req_line_almost_done:
  1475. {
  1476. if (HTTP_UNLIKELY(ch != HTTP_LF)) {
  1477. HTTP_SET_ERRNO(http_errno::HPE_LF_EXPECTED);
  1478. goto error;
  1479. }
  1480. HTTP_UPDATE_STATE(state::s_header_field_start);
  1481. break;
  1482. }
  1483. case state::s_header_field_start:
  1484. {
  1485. if (ch == HTTP_CR) {
  1486. HTTP_UPDATE_STATE(state::s_headers_almost_done);
  1487. break;
  1488. }
  1489. if (ch == HTTP_LF) {
  1490. /* they might be just sending \n instead of \r\n so this would be
  1491. * the second \n to denote the end of headers*/
  1492. HTTP_UPDATE_STATE(state::s_headers_almost_done);
  1493. HTTP_REEXECUTE();
  1494. }
  1495. c = HTTP_TOKEN(ch);
  1496. if (HTTP_UNLIKELY(!c)) {
  1497. HTTP_SET_ERRNO(http_errno::HPE_INVALID_HEADER_TOKEN);
  1498. goto error;
  1499. }
  1500. HTTP_MARK(header_field);
  1501. parser->index = 0;
  1502. HTTP_UPDATE_STATE(state::s_header_field);
  1503. switch (c) {
  1504. case 'c':
  1505. parser->header_state = (unsigned int)(header_states::h_C);
  1506. break;
  1507. case 'p':
  1508. parser->header_state = (unsigned int)(header_states::h_matching_proxy_connection);
  1509. break;
  1510. case 't':
  1511. parser->header_state = (unsigned int)(header_states::h_matching_transfer_encoding);
  1512. break;
  1513. case 'u':
  1514. parser->header_state = (unsigned int)(header_states::h_matching_upgrade);
  1515. break;
  1516. default:
  1517. parser->header_state = (unsigned int)(header_states::h_general);
  1518. break;
  1519. }
  1520. break;
  1521. }
  1522. case state::s_header_field:
  1523. {
  1524. const char* start = p;
  1525. for (; p != data + len; ++p) {
  1526. ch = *p;
  1527. c = HTTP_TOKEN(ch);
  1528. if (!c)
  1529. break;
  1530. switch (header_states(parser->header_state)) {
  1531. case header_states::h_general: {
  1532. size_t left = size_t(data + len - p);
  1533. const char* pe = p + HTTP_MIN(left, max_header_size);
  1534. while (p+1 < pe && HTTP_TOKEN(p[1])) {
  1535. p++;
  1536. }
  1537. break;
  1538. }
  1539. case header_states::h_C:
  1540. parser->index++;
  1541. parser->header_state = (unsigned int)(c == 'o' ? header_states::h_CO : header_states::h_general);
  1542. break;
  1543. case header_states::h_CO:
  1544. parser->index++;
  1545. parser->header_state = (unsigned int)(c == 'n' ? header_states::h_CON : header_states::h_general);
  1546. break;
  1547. case header_states::h_CON:
  1548. parser->index++;
  1549. switch (c) {
  1550. case 'n':
  1551. parser->header_state = (unsigned int)(header_states::h_matching_connection);
  1552. break;
  1553. case 't':
  1554. parser->header_state = (unsigned int)(header_states::h_matching_content_length);
  1555. break;
  1556. default:
  1557. parser->header_state = (unsigned int)(header_states::h_general);
  1558. break;
  1559. }
  1560. break;
  1561. /* connection */
  1562. case header_states::h_matching_connection:
  1563. parser->index++;
  1564. if (parser->index > sizeof(HTTP_CONNECTION)-1
  1565. || c != HTTP_CONNECTION[parser->index]) {
  1566. parser->header_state = (unsigned int)(header_states::h_general);
  1567. } else if (parser->index == sizeof(HTTP_CONNECTION)-2) {
  1568. parser->header_state = (unsigned int)(header_states::h_connection);
  1569. }
  1570. break;
  1571. /* proxy-connection */
  1572. case header_states::h_matching_proxy_connection:
  1573. parser->index++;
  1574. if (parser->index > sizeof(HTTP_PROXY_CONNECTION)-1
  1575. || c != HTTP_PROXY_CONNECTION[parser->index]) {
  1576. parser->header_state = (unsigned int)(header_states::h_general);
  1577. } else if (parser->index == sizeof(HTTP_PROXY_CONNECTION)-2) {
  1578. parser->header_state = (unsigned int)(header_states::h_connection);
  1579. }
  1580. break;
  1581. /* content-length */
  1582. case header_states::h_matching_content_length:
  1583. parser->index++;
  1584. if (parser->index > sizeof(HTTP_CONTENT_LENGTH)-1
  1585. || c != HTTP_CONTENT_LENGTH[parser->index]) {
  1586. parser->header_state = (unsigned int)(header_states::h_general);
  1587. } else if (parser->index == sizeof(HTTP_CONTENT_LENGTH)-2) {
  1588. parser->header_state = (unsigned int)(header_states::h_content_length);
  1589. }
  1590. break;
  1591. /* transfer-encoding */
  1592. case header_states::h_matching_transfer_encoding:
  1593. parser->index++;
  1594. if (parser->index > sizeof(HTTP_TRANSFER_ENCODING)-1
  1595. || c != HTTP_TRANSFER_ENCODING[parser->index]) {
  1596. parser->header_state = (unsigned int)(header_states::h_general);
  1597. } else if (parser->index == sizeof(HTTP_TRANSFER_ENCODING)-2) {
  1598. parser->header_state = (unsigned int)(header_states::h_transfer_encoding);
  1599. parser->uses_transfer_encoding = 1;
  1600. }
  1601. break;
  1602. /* upgrade */
  1603. case header_states::h_matching_upgrade:
  1604. parser->index++;
  1605. if (parser->index > sizeof(HTTP_UPGRADE)-1
  1606. || c != HTTP_UPGRADE[parser->index]) {
  1607. parser->header_state = (unsigned int)(header_states::h_general);
  1608. } else if (parser->index == sizeof(HTTP_UPGRADE)-2) {
  1609. parser->header_state = (unsigned int)(header_states::h_upgrade);
  1610. }
  1611. break;
  1612. case header_states::h_connection:
  1613. case header_states::h_content_length:
  1614. case header_states::h_transfer_encoding:
  1615. case header_states::h_upgrade:
  1616. if (ch != ' ') parser->header_state = (unsigned int)(header_states::h_general);
  1617. break;
  1618. default:
  1619. assert(0 && "Unknown header_state");
  1620. break;
  1621. }
  1622. }
  1623. if (p == data + len) {
  1624. --p;
  1625. HTTP_COUNT_HEADER_SIZE(p - start);
  1626. break;
  1627. }
  1628. HTTP_COUNT_HEADER_SIZE(p - start);
  1629. if (ch == ':') {
  1630. HTTP_UPDATE_STATE(state::s_header_value_discard_ws);
  1631. HTTP_CALLBACK_DATA(header_field);
  1632. break;
  1633. }
  1634. HTTP_SET_ERRNO(http_errno::HPE_INVALID_HEADER_TOKEN);
  1635. goto error;
  1636. }
  1637. case state::s_header_value_discard_ws:
  1638. if (ch == ' ' || ch == '\t') break;
  1639. if (ch == HTTP_CR) {
  1640. HTTP_UPDATE_STATE(state::s_header_value_discard_ws_almost_done);
  1641. break;
  1642. }
  1643. if (ch == HTTP_LF) {
  1644. HTTP_UPDATE_STATE(state::s_header_value_discard_lws);
  1645. break;
  1646. }
  1647. /* fall through */
  1648. case state::s_header_value_start:
  1649. {
  1650. HTTP_MARK(header_value);
  1651. HTTP_UPDATE_STATE(state::s_header_value);
  1652. parser->index = 0;
  1653. c = HTTP_LOWER(ch);
  1654. switch (header_states(parser->header_state)) {
  1655. case header_states::h_upgrade:
  1656. parser->flags |= (unsigned int)(flags::F_UPGRADE);
  1657. parser->header_state = (unsigned int)(header_states::h_general);
  1658. break;
  1659. case header_states::h_transfer_encoding:
  1660. /* looking for 'Transfer-Encoding: chunked' */
  1661. if ('c' == c) {
  1662. parser->header_state = (unsigned int)(header_states::h_matching_transfer_encoding_chunked);
  1663. } else {
  1664. parser->header_state = (unsigned int)(header_states::h_matching_transfer_encoding_token);
  1665. }
  1666. break;
  1667. /* Multi-value `Transfer-Encoding` header */
  1668. case header_states::h_matching_transfer_encoding_token_start:
  1669. break;
  1670. case header_states::h_content_length:
  1671. if (HTTP_UNLIKELY(!HTTP_IS_NUM(ch))) {
  1672. HTTP_SET_ERRNO(http_errno::HPE_INVALID_CONTENT_LENGTH);
  1673. goto error;
  1674. }
  1675. if (parser->flags & (unsigned int)(flags::F_CONTENTLENGTH)) {
  1676. HTTP_SET_ERRNO(http_errno::HPE_UNEXPECTED_CONTENT_LENGTH);
  1677. goto error;
  1678. }
  1679. parser->flags |= (unsigned int)(flags::F_CONTENTLENGTH);
  1680. parser->content_length = ch - '0';
  1681. parser->header_state = (unsigned int)(header_states::h_content_length_num);
  1682. break;
  1683. /* when obsolete line folding is encountered for content length
  1684. * continue to the s_header_value state */
  1685. case header_states::h_content_length_ws:
  1686. break;
  1687. case header_states::h_connection:
  1688. /* looking for 'Connection: keep-alive' */
  1689. if (c == 'k') {
  1690. parser->header_state = (unsigned int)(header_states::h_matching_connection_keep_alive);
  1691. /* looking for 'Connection: close' */
  1692. } else if (c == 'c') {
  1693. parser->header_state = (unsigned int)(header_states::h_matching_connection_close);
  1694. } else if (c == 'u') {
  1695. parser->header_state = (unsigned int)(header_states::h_matching_connection_upgrade);
  1696. } else {
  1697. parser->header_state = (unsigned int)(header_states::h_matching_connection_token);
  1698. }
  1699. break;
  1700. /* Multi-value `Connection` header */
  1701. case header_states::h_matching_connection_token_start:
  1702. break;
  1703. default:
  1704. parser->header_state = (unsigned int)(header_states::h_general);
  1705. break;
  1706. }
  1707. break;
  1708. }
  1709. case state::s_header_value:
  1710. {
  1711. const char* start = p;
  1712. header_states h_state = (header_states) parser->header_state;
  1713. for (; p != data + len; ++p) {
  1714. ch = *p;
  1715. if (ch == HTTP_CR) {
  1716. HTTP_UPDATE_STATE(state::s_header_almost_done);
  1717. parser->header_state = (unsigned int)(h_state);
  1718. HTTP_CALLBACK_DATA(header_value);
  1719. break;
  1720. }
  1721. if (ch == HTTP_LF) {
  1722. HTTP_UPDATE_STATE(state::s_header_almost_done);
  1723. HTTP_COUNT_HEADER_SIZE(p - start);
  1724. parser->header_state = (unsigned int)(h_state);
  1725. HTTP_CALLBACK_DATA_NOADVANCE(header_value);
  1726. HTTP_REEXECUTE();
  1727. }
  1728. if (!lenient && !HTTP_IS_HEADER_CHAR(ch)) {
  1729. HTTP_SET_ERRNO(http_errno::HPE_INVALID_HEADER_TOKEN);
  1730. goto error;
  1731. }
  1732. c = HTTP_LOWER(ch);
  1733. switch (h_state) {
  1734. case header_states::h_general:
  1735. {
  1736. size_t left = size_t(data + len - p);
  1737. const char* pe = p + HTTP_MIN(left, max_header_size);
  1738. for (; p != pe; p++) {
  1739. ch = *p;
  1740. if (ch == HTTP_CR || ch == HTTP_LF) {
  1741. --p;
  1742. break;
  1743. }
  1744. if (!lenient && !HTTP_IS_HEADER_CHAR(ch)) {
  1745. HTTP_SET_ERRNO(http_errno::HPE_INVALID_HEADER_TOKEN);
  1746. goto error;
  1747. }
  1748. }
  1749. if (p == data + len)
  1750. --p;
  1751. break;
  1752. }
  1753. case header_states::h_connection:
  1754. case header_states::h_transfer_encoding:
  1755. assert(0 && "Shouldn't get here.");
  1756. break;
  1757. case header_states::h_content_length:
  1758. if (ch == ' ') break;
  1759. h_state = header_states::h_content_length_num;
  1760. /* fall through */
  1761. case header_states::h_content_length_num:
  1762. {
  1763. uint64_t t;
  1764. if (ch == ' ') {
  1765. h_state = header_states::h_content_length_ws;
  1766. break;
  1767. }
  1768. if (HTTP_UNLIKELY(!HTTP_IS_NUM(ch))) {
  1769. HTTP_SET_ERRNO(http_errno::HPE_INVALID_CONTENT_LENGTH);
  1770. parser->header_state = (unsigned int)(h_state);
  1771. goto error;
  1772. }
  1773. t = parser->content_length;
  1774. t *= 10;
  1775. t += ch - '0';
  1776. /* Overflow? Test against a conservative limit for simplicity. */
  1777. if (HTTP_UNLIKELY((HTTP_ULLONG_MAX - 10) / 10 < parser->content_length)) {
  1778. HTTP_SET_ERRNO(http_errno::HPE_INVALID_CONTENT_LENGTH);
  1779. parser->header_state = (unsigned int)(h_state);
  1780. goto error;
  1781. }
  1782. parser->content_length = t;
  1783. break;
  1784. }
  1785. case header_states::h_content_length_ws:
  1786. if (ch == ' ') break;
  1787. HTTP_SET_ERRNO(http_errno::HPE_INVALID_CONTENT_LENGTH);
  1788. parser->header_state = (unsigned int)(h_state);
  1789. goto error;
  1790. /* Transfer-Encoding: chunked */
  1791. case header_states::h_matching_transfer_encoding_token_start:
  1792. /* looking for 'Transfer-Encoding: chunked' */
  1793. if ('c' == c) {
  1794. h_state = header_states::h_matching_transfer_encoding_chunked;
  1795. } else if (HTTP_STRICT_TOKEN(c)) {
  1796. /* TODO(indutny): similar code below does this, but why?
  1797. * At the very least it seems to be inconsistent given that
  1798. * h_matching_transfer_encoding_token does not check for
  1799. * `STRICT_TOKEN`
  1800. */
  1801. h_state = header_states::h_matching_transfer_encoding_token;
  1802. } else if (c == ' ' || c == '\t') {
  1803. /* Skip lws */
  1804. } else {
  1805. h_state = header_states::h_general;
  1806. }
  1807. break;
  1808. case header_states::h_matching_transfer_encoding_chunked:
  1809. parser->index++;
  1810. if (parser->index > sizeof(HTTP_CHUNKED)-1
  1811. || c != HTTP_CHUNKED[parser->index]) {
  1812. h_state = header_states::h_matching_transfer_encoding_token;
  1813. } else if (parser->index == sizeof(HTTP_CHUNKED)-2) {
  1814. h_state = header_states::h_transfer_encoding_chunked;
  1815. }
  1816. break;
  1817. case header_states::h_matching_transfer_encoding_token:
  1818. if (ch == ',') {
  1819. h_state = header_states::h_matching_transfer_encoding_token_start;
  1820. parser->index = 0;
  1821. }
  1822. break;
  1823. case header_states::h_matching_connection_token_start:
  1824. /* looking for 'Connection: keep-alive' */
  1825. if (c == 'k') {
  1826. h_state = header_states::h_matching_connection_keep_alive;
  1827. /* looking for 'Connection: close' */
  1828. } else if (c == 'c') {
  1829. h_state = header_states::h_matching_connection_close;
  1830. } else if (c == 'u') {
  1831. h_state = header_states::h_matching_connection_upgrade;
  1832. } else if (HTTP_STRICT_TOKEN(c)) {
  1833. h_state = header_states::h_matching_connection_token;
  1834. } else if (c == ' ' || c == '\t') {
  1835. /* Skip lws */
  1836. } else {
  1837. h_state = header_states::h_general;
  1838. }
  1839. break;
  1840. /* looking for 'Connection: keep-alive' */
  1841. case header_states::h_matching_connection_keep_alive:
  1842. parser->index++;
  1843. if (parser->index > sizeof(HTTP_KEEP_ALIVE)-1
  1844. || c != HTTP_KEEP_ALIVE[parser->index]) {
  1845. h_state = header_states::h_matching_connection_token;
  1846. } else if (parser->index == sizeof(HTTP_KEEP_ALIVE)-2) {
  1847. h_state = header_states::h_connection_keep_alive;
  1848. }
  1849. break;
  1850. /* looking for 'Connection: close' */
  1851. case header_states::h_matching_connection_close:
  1852. parser->index++;
  1853. if (parser->index > sizeof(HTTP_CLOSE)-1 || c != HTTP_CLOSE[parser->index]) {
  1854. h_state = header_states::h_matching_connection_token;
  1855. } else if (parser->index == sizeof(HTTP_CLOSE)-2) {
  1856. h_state = header_states::h_connection_close;
  1857. }
  1858. break;
  1859. /* looking for 'Connection: upgrade' */
  1860. case header_states::h_matching_connection_upgrade:
  1861. parser->index++;
  1862. if (parser->index > sizeof(HTTP_UPGRADE) - 1 ||
  1863. c != HTTP_UPGRADE[parser->index]) {
  1864. h_state = header_states::h_matching_connection_token;
  1865. } else if (parser->index == sizeof(HTTP_UPGRADE)-2) {
  1866. h_state = header_states::h_connection_upgrade;
  1867. }
  1868. break;
  1869. case header_states::h_matching_connection_token:
  1870. if (ch == ',') {
  1871. h_state = header_states::h_matching_connection_token_start;
  1872. parser->index = 0;
  1873. }
  1874. break;
  1875. case header_states::h_transfer_encoding_chunked:
  1876. if (ch != ' ') h_state = header_states::h_matching_transfer_encoding_token;
  1877. break;
  1878. case header_states::h_connection_keep_alive:
  1879. case header_states::h_connection_close:
  1880. case header_states::h_connection_upgrade:
  1881. if (ch == ',') {
  1882. if (h_state == header_states::h_connection_keep_alive) {
  1883. parser->flags |= (unsigned int)(flags::F_CONNECTION_KEEP_ALIVE);
  1884. } else if (h_state == header_states::h_connection_close) {
  1885. parser->flags |= (unsigned int)(flags::F_CONNECTION_CLOSE);
  1886. } else if (h_state == header_states::h_connection_upgrade) {
  1887. parser->flags |= (unsigned int)(flags::F_CONNECTION_UPGRADE);
  1888. }
  1889. h_state = header_states::h_matching_connection_token_start;
  1890. parser->index = 0;
  1891. } else if (ch != ' ') {
  1892. h_state = header_states::h_matching_connection_token;
  1893. }
  1894. break;
  1895. default:
  1896. HTTP_UPDATE_STATE(state::s_header_value);
  1897. h_state = header_states::h_general;
  1898. break;
  1899. }
  1900. }
  1901. parser->header_state = (unsigned int)(h_state);
  1902. if (p == data + len)
  1903. --p;
  1904. HTTP_COUNT_HEADER_SIZE(p - start);
  1905. break;
  1906. }
  1907. case state::s_header_almost_done:
  1908. {
  1909. if (HTTP_UNLIKELY(ch != HTTP_LF)) {
  1910. HTTP_SET_ERRNO(http_errno::HPE_LF_EXPECTED);
  1911. goto error;
  1912. }
  1913. HTTP_UPDATE_STATE(state::s_header_value_lws);
  1914. break;
  1915. }
  1916. case state::s_header_value_lws:
  1917. {
  1918. if (ch == ' ' || ch == '\t') {
  1919. if (parser->header_state == (unsigned int)(header_states::h_content_length_num)) {
  1920. /* treat obsolete line folding as space */
  1921. parser->header_state = (unsigned int)(header_states::h_content_length_ws);
  1922. }
  1923. HTTP_UPDATE_STATE(state::s_header_value_start);
  1924. HTTP_REEXECUTE();
  1925. }
  1926. /* finished the header */
  1927. switch (header_states(parser->header_state)) {
  1928. case header_states::h_connection_keep_alive:
  1929. parser->flags |= (unsigned int)(flags::F_CONNECTION_KEEP_ALIVE);
  1930. break;
  1931. case header_states::h_connection_close:
  1932. parser->flags |= (unsigned int)(flags::F_CONNECTION_CLOSE);
  1933. break;
  1934. case header_states::h_transfer_encoding_chunked:
  1935. parser->flags |= (unsigned int)(flags::F_CHUNKED);
  1936. break;
  1937. case header_states::h_connection_upgrade:
  1938. parser->flags |= (unsigned int)(flags::F_CONNECTION_UPGRADE);
  1939. break;
  1940. default:
  1941. break;
  1942. }
  1943. HTTP_UPDATE_STATE(state::s_header_field_start);
  1944. HTTP_REEXECUTE();
  1945. }
  1946. case state::s_header_value_discard_ws_almost_done:
  1947. {
  1948. HTTP_STRICT_CHECK(ch != HTTP_LF);
  1949. HTTP_UPDATE_STATE(state::s_header_value_discard_lws);
  1950. break;
  1951. }
  1952. case state::s_header_value_discard_lws:
  1953. {
  1954. if (ch == ' ' || ch == '\t') {
  1955. HTTP_UPDATE_STATE(state::s_header_value_discard_ws);
  1956. break;
  1957. } else {
  1958. switch (header_states(parser->header_state)) {
  1959. case header_states::h_connection_keep_alive:
  1960. parser->flags |= (unsigned int)(flags::F_CONNECTION_KEEP_ALIVE);
  1961. break;
  1962. case header_states::h_connection_close:
  1963. parser->flags |= (unsigned int)(flags::F_CONNECTION_CLOSE);
  1964. break;
  1965. case header_states::h_connection_upgrade:
  1966. parser->flags |= (unsigned int)(flags::F_CONNECTION_UPGRADE);
  1967. break;
  1968. case header_states::h_transfer_encoding_chunked:
  1969. parser->flags |= (unsigned int)(flags::F_CHUNKED);
  1970. break;
  1971. case header_states::h_content_length:
  1972. /* do not allow empty content length */
  1973. HTTP_SET_ERRNO(http_errno::HPE_INVALID_CONTENT_LENGTH);
  1974. goto error;
  1975. break;
  1976. default:
  1977. break;
  1978. }
  1979. /* header value was empty */
  1980. HTTP_MARK(header_value);
  1981. HTTP_UPDATE_STATE(state::s_header_field_start);
  1982. HTTP_CALLBACK_DATA_NOADVANCE(header_value);
  1983. HTTP_REEXECUTE();
  1984. }
  1985. }
  1986. case state::s_headers_almost_done:
  1987. {
  1988. HTTP_STRICT_CHECK(ch != HTTP_LF);
  1989. if (parser->flags & (unsigned int)(flags::F_TRAILING)) {
  1990. /* End of a chunked request */
  1991. HTTP_UPDATE_STATE(state::s_message_done);
  1992. HTTP_CALLBACK_NOTIFY_NOADVANCE(chunk_complete);
  1993. HTTP_REEXECUTE();
  1994. }
  1995. /* Cannot use transfer-encoding and a content-length header together
  1996. per the HTTP specification. (RFC 7230 Section 3.3.3) */
  1997. if ((parser->uses_transfer_encoding == 1) &&
  1998. (parser->flags & (unsigned int)(flags::F_CONTENTLENGTH))) {
  1999. /* Allow it for lenient parsing as long as `Transfer-Encoding` is
  2000. * not `chunked` or allow_length_with_encoding is set
  2001. */
  2002. if (parser->flags & (unsigned int)(flags::F_CHUNKED)) {
  2003. if (!allow_chunked_length) {
  2004. HTTP_SET_ERRNO(http_errno::HPE_UNEXPECTED_CONTENT_LENGTH);
  2005. goto error;
  2006. }
  2007. } else if (!lenient) {
  2008. HTTP_SET_ERRNO(http_errno::HPE_UNEXPECTED_CONTENT_LENGTH);
  2009. goto error;
  2010. }
  2011. }
  2012. HTTP_UPDATE_STATE(state::s_headers_done);
  2013. /* Set this here so that on_headers_complete() callbacks can see it */
  2014. if ((parser->flags & (unsigned int)(flags::F_UPGRADE)) &&
  2015. (parser->flags & (unsigned int)(flags::F_CONNECTION_UPGRADE))) {
  2016. /* For responses, "Upgrade: foo" and "Connection: upgrade" are
  2017. * mandatory only when it is a 101 Switching Protocols response,
  2018. * otherwise it is purely informational, to announce support.
  2019. */
  2020. parser->upgrade =
  2021. (parser->type == (unsigned int)(http_parser_type::HTTP_REQUEST) || parser->status_code == 101);
  2022. } else {
  2023. parser->upgrade = (parser->method == (unsigned int)(http_method::HTTP_CONNECT));
  2024. }
  2025. /* Here we call the headers_complete callback. This is somewhat
  2026. * different than other callbacks because if the user returns 1, we
  2027. * will interpret that as saying that this message has no body. This
  2028. * is needed for the annoying case of recieving a response to a HEAD
  2029. * request.
  2030. *
  2031. * We'd like to use HTTP_CALLBACK_NOTIFY_NOADVANCE() here but we cannot, so
  2032. * we have to simulate it by handling a change in errno below.
  2033. */
  2034. if (settings->on_headers_complete) {
  2035. switch (settings->on_headers_complete(parser, user_data)) {
  2036. case 0:
  2037. break;
  2038. case 2:
  2039. parser->upgrade = 1;
  2040. /* fall through */
  2041. case 1:
  2042. parser->flags |= (unsigned int)(flags::F_SKIPBODY);
  2043. break;
  2044. default:
  2045. HTTP_SET_ERRNO(http_errno::HPE_CB_headers_complete);
  2046. HTTP_RETURN(p - data); /* Error */
  2047. }
  2048. }
  2049. if (HTTP_PARSER_ERRNO(parser) != http_errno::HPE_OK) {
  2050. HTTP_RETURN(p - data);
  2051. }
  2052. HTTP_REEXECUTE();
  2053. }
  2054. case state::s_headers_done:
  2055. {
  2056. int hasBody;
  2057. HTTP_STRICT_CHECK(ch != HTTP_LF);
  2058. parser->nread = 0;
  2059. nread = 0;
  2060. hasBody = parser->flags & (unsigned int)(flags::F_CHUNKED) ||
  2061. (parser->content_length > 0 && parser->content_length != HTTP_ULLONG_MAX);
  2062. if (parser->upgrade && (parser->method == (unsigned int)(http_method::HTTP_CONNECT) ||
  2063. (parser->flags & (unsigned int)(flags::F_SKIPBODY)) || !hasBody)) {
  2064. /* Exit, the rest of the message is in a different protocol. */
  2065. HTTP_UPDATE_STATE(HTTP_NEW_MESSAGE());
  2066. HTTP_CALLBACK_NOTIFY(message_complete);
  2067. HTTP_RETURN((p - data) + 1);
  2068. }
  2069. if (parser->flags & (unsigned int)(flags::F_SKIPBODY)) {
  2070. HTTP_UPDATE_STATE(HTTP_NEW_MESSAGE());
  2071. HTTP_CALLBACK_NOTIFY(message_complete);
  2072. } else if (parser->flags & (unsigned int)(flags::F_CHUNKED)) {
  2073. /* chunked encoding - ignore Content-Length header,
  2074. * prepare for a chunk */
  2075. HTTP_UPDATE_STATE(state::s_chunk_size_start);
  2076. } else if (parser->uses_transfer_encoding == 1) {
  2077. if (parser->type == (unsigned int)(http_parser_type::HTTP_REQUEST) && !lenient) {
  2078. /* RFC 7230 3.3.3 */
  2079. /* If a Transfer-Encoding header field
  2080. * is present in a request and the chunked transfer coding is not
  2081. * the final encoding, the message body length cannot be determined
  2082. * reliably; the server MUST respond with the 400 (Bad Request)
  2083. * status code and then close the connection.
  2084. */
  2085. HTTP_SET_ERRNO(http_errno::HPE_INVALID_TRANSFER_ENCODING);
  2086. HTTP_RETURN(p - data); /* Error */
  2087. } else {
  2088. /* RFC 7230 3.3.3 */
  2089. /* If a Transfer-Encoding header field is present in a response and
  2090. * the chunked transfer coding is not the final encoding, the
  2091. * message body length is determined by reading the connection until
  2092. * it is closed by the server.
  2093. */
  2094. HTTP_UPDATE_STATE(state::s_body_identity_eof);
  2095. }
  2096. } else {
  2097. if (parser->content_length == 0) {
  2098. /* Content-Length header given but zero: Content-Length: 0\r\n */
  2099. HTTP_UPDATE_STATE(HTTP_NEW_MESSAGE());
  2100. HTTP_CALLBACK_NOTIFY(message_complete);
  2101. } else if (parser->content_length != HTTP_ULLONG_MAX) {
  2102. /* Content-Length header given and non-zero */
  2103. HTTP_UPDATE_STATE(state::s_body_identity);
  2104. } else {
  2105. if (!http_message_needs_eof(parser)) {
  2106. /* Assume content-length 0 - read the next */
  2107. HTTP_UPDATE_STATE(HTTP_NEW_MESSAGE());
  2108. HTTP_CALLBACK_NOTIFY(message_complete);
  2109. } else {
  2110. /* Read body until EOF */
  2111. HTTP_UPDATE_STATE(state::s_body_identity_eof);
  2112. }
  2113. }
  2114. }
  2115. break;
  2116. }
  2117. case state::s_body_identity:
  2118. {
  2119. uint64_t to_read = HTTP_MIN(parser->content_length,
  2120. (uint64_t) ((data + len) - p));
  2121. assert(parser->content_length != 0
  2122. && parser->content_length != HTTP_ULLONG_MAX);
  2123. /* The difference between advancing content_length and p is because
  2124. * the latter will automaticaly advance on the next loop iteration.
  2125. * Further, if content_length ends up at 0, we want to see the last
  2126. * byte again for our message complete callback.
  2127. */
  2128. HTTP_MARK(body);
  2129. parser->content_length -= to_read;
  2130. p += to_read - 1;
  2131. if (parser->content_length == 0) {
  2132. HTTP_UPDATE_STATE(state::s_message_done);
  2133. /* Mimic HTTP_CALLBACK_DATA_NOADVANCE() but with one extra byte.
  2134. *
  2135. * The alternative to doing this is to wait for the next byte to
  2136. * trigger the data callback, just as in every other case. The
  2137. * problem with this is that this makes it difficult for the test
  2138. * harness to distinguish between complete-on-EOF and
  2139. * complete-on-length. It's not clear that this distinction is
  2140. * important for applications, but let's keep it for now.
  2141. */
  2142. HTTP_CALLBACK_DATA_(body, p - body_mark + 1, p - data);
  2143. HTTP_REEXECUTE();
  2144. }
  2145. break;
  2146. }
  2147. /* read until EOF */
  2148. case state::s_body_identity_eof:
  2149. HTTP_MARK(body);
  2150. p = data + len - 1;
  2151. break;
  2152. case state::s_message_done:
  2153. HTTP_UPDATE_STATE(HTTP_NEW_MESSAGE());
  2154. HTTP_CALLBACK_NOTIFY(message_complete);
  2155. if (parser->upgrade) {
  2156. /* Exit, the rest of the message is in a different protocol. */
  2157. HTTP_RETURN((p - data) + 1);
  2158. }
  2159. break;
  2160. case state::s_chunk_size_start:
  2161. {
  2162. assert(nread == 1);
  2163. assert(parser->flags & (unsigned int)(flags::F_CHUNKED));
  2164. unhex_val = unhex[(unsigned char)ch];
  2165. if (HTTP_UNLIKELY(unhex_val == -1)) {
  2166. HTTP_SET_ERRNO(http_errno::HPE_INVALID_CHUNK_SIZE);
  2167. goto error;
  2168. }
  2169. parser->content_length = unhex_val;
  2170. HTTP_UPDATE_STATE(state::s_chunk_size);
  2171. break;
  2172. }
  2173. case state::s_chunk_size:
  2174. {
  2175. uint64_t t;
  2176. assert(parser->flags & (unsigned int)(flags::F_CHUNKED));
  2177. if (ch == HTTP_CR) {
  2178. HTTP_UPDATE_STATE(state::s_chunk_size_almost_done);
  2179. break;
  2180. }
  2181. unhex_val = unhex[(unsigned char)ch];
  2182. if (unhex_val == -1) {
  2183. if (ch == ';' || ch == ' ') {
  2184. HTTP_UPDATE_STATE(state::s_chunk_parameters);
  2185. break;
  2186. }
  2187. HTTP_SET_ERRNO(http_errno::HPE_INVALID_CHUNK_SIZE);
  2188. goto error;
  2189. }
  2190. t = parser->content_length;
  2191. t *= 16;
  2192. t += unhex_val;
  2193. /* Overflow? Test against a conservative limit for simplicity. */
  2194. if (HTTP_UNLIKELY((HTTP_ULLONG_MAX - 16) / 16 < parser->content_length)) {
  2195. HTTP_SET_ERRNO(http_errno::HPE_INVALID_CONTENT_LENGTH);
  2196. goto error;
  2197. }
  2198. parser->content_length = t;
  2199. break;
  2200. }
  2201. case state::s_chunk_parameters:
  2202. {
  2203. assert(parser->flags & (unsigned int)(flags::F_CHUNKED));
  2204. /* just ignore this shit. TODO check for overflow */
  2205. if (ch == HTTP_CR) {
  2206. HTTP_UPDATE_STATE(state::s_chunk_size_almost_done);
  2207. break;
  2208. }
  2209. break;
  2210. }
  2211. case state::s_chunk_size_almost_done:
  2212. {
  2213. assert(parser->flags & (unsigned int)(flags::F_CHUNKED));
  2214. HTTP_STRICT_CHECK(ch != HTTP_LF);
  2215. parser->nread = 0;
  2216. nread = 0;
  2217. if (parser->content_length == 0) {
  2218. parser->flags |= (unsigned int)(flags::F_TRAILING);
  2219. HTTP_UPDATE_STATE(state::s_header_field_start);
  2220. } else {
  2221. HTTP_UPDATE_STATE(state::s_chunk_data);
  2222. }
  2223. HTTP_CALLBACK_NOTIFY(chunk_header);
  2224. break;
  2225. }
  2226. case state::s_chunk_data:
  2227. {
  2228. uint64_t to_read = HTTP_MIN(parser->content_length,
  2229. (uint64_t) ((data + len) - p));
  2230. assert(parser->flags & (unsigned int)(flags::F_CHUNKED));
  2231. assert(parser->content_length != 0
  2232. && parser->content_length != HTTP_ULLONG_MAX);
  2233. /* See the explanation in state::s_body_identity for why the content
  2234. * length and data pointers are managed this way.
  2235. */
  2236. HTTP_MARK(body);
  2237. parser->content_length -= to_read;
  2238. p += to_read - 1;
  2239. if (parser->content_length == 0) {
  2240. HTTP_UPDATE_STATE(state::s_chunk_data_almost_done);
  2241. }
  2242. break;
  2243. }
  2244. case state::s_chunk_data_almost_done:
  2245. assert(parser->flags & (unsigned int)(flags::F_CHUNKED));
  2246. assert(parser->content_length == 0);
  2247. HTTP_STRICT_CHECK(ch != HTTP_CR);
  2248. HTTP_UPDATE_STATE(state::s_chunk_data_done);
  2249. HTTP_CALLBACK_DATA(body);
  2250. break;
  2251. case state::s_chunk_data_done:
  2252. assert(parser->flags & (unsigned int)(flags::F_CHUNKED));
  2253. HTTP_STRICT_CHECK(ch != HTTP_LF);
  2254. parser->nread = 0;
  2255. nread = 0;
  2256. HTTP_UPDATE_STATE(state::s_chunk_size_start);
  2257. HTTP_CALLBACK_NOTIFY(chunk_complete);
  2258. break;
  2259. default:
  2260. assert(0 && "unhandled state");
  2261. HTTP_SET_ERRNO(http_errno::HPE_INVALID_INTERNAL_STATE);
  2262. goto error;
  2263. }
  2264. }
  2265. /* Run callbacks for any marks that we have leftover after we ran out of
  2266. * bytes. There should be at most one of these set, so it's OK to invoke
  2267. * them in series (unset marks will not result in callbacks).
  2268. *
  2269. * We use the NOADVANCE() variety of callbacks here because 'p' has already
  2270. * overflowed 'data' and this allows us to correct for the off-by-one that
  2271. * we'd otherwise have (since HTTP_CALLBACK_DATA() is meant to be run with a 'p'
  2272. * value that's in-bounds).
  2273. */
  2274. assert(((header_field_mark ? 1 : 0) +
  2275. (header_value_mark ? 1 : 0) +
  2276. (url_mark ? 1 : 0) +
  2277. (body_mark ? 1 : 0) +
  2278. (status_mark ? 1 : 0)) <= 1);
  2279. HTTP_CALLBACK_DATA_NOADVANCE(header_field);
  2280. HTTP_CALLBACK_DATA_NOADVANCE(header_value);
  2281. HTTP_CALLBACK_DATA_NOADVANCE(url);
  2282. HTTP_CALLBACK_DATA_NOADVANCE(body);
  2283. HTTP_CALLBACK_DATA_NOADVANCE(status);
  2284. HTTP_RETURN(len);
  2285. error:
  2286. if (HTTP_PARSER_ERRNO(parser) == http_errno::HPE_OK) {
  2287. HTTP_SET_ERRNO(http_errno::HPE_UNKNOWN);
  2288. }
  2289. HTTP_RETURN(p - data);
  2290. }
  2291. /* Returns a string version of the HTTP method. */
  2292. template<typename = void>
  2293. const char *http_method_str (http_method m)
  2294. {
  2295. return HTTP_ELEM_AT(method_strings, m, "<unknown>");
  2296. }
  2297. /* Returns a string version of the HTTP status code. */
  2298. template<typename = void>
  2299. const char *http_status_str(http_status s)
  2300. {
  2301. switch (s) {
  2302. #define HTTP_XX(num, name, string) case http_status::name: return #string;
  2303. HTTP_STATUS_MAP(HTTP_XX)
  2304. #undef HTTP_XX
  2305. default: return "<unknown>";
  2306. }
  2307. }
  2308. // add by zhllxt
  2309. template<typename = void>
  2310. const char * http_status_str(unsigned int v)
  2311. {
  2312. return http_status_str(static_cast<http_status>(v));
  2313. }
  2314. template<typename = void>
  2315. void
  2316. http_parser_init (http_parser *parser, http_parser_type t)
  2317. {
  2318. void *data = parser->data; /* preserve application data */
  2319. memset(parser, 0, sizeof(*parser));
  2320. parser->data = data;
  2321. parser->type = (unsigned int)(t);
  2322. parser->state = (unsigned int)(t == http_parser_type::HTTP_REQUEST ? state::s_start_req : (t == http_parser_type::HTTP_RESPONSE ? state::s_start_res : state::s_start_req_or_res));
  2323. parser->http_errno = (unsigned int)(http_errno::HPE_OK);
  2324. }
  2325. template<typename = void>
  2326. void
  2327. http_parser_settings_init(http_parser_settings *settings)
  2328. {
  2329. memset(settings, 0, sizeof(*settings));
  2330. }
  2331. template<typename = void>
  2332. const char *
  2333. http_errno_name(http_errno err) {
  2334. assert(((size_t) err) < HTTP_ARRAY_SIZE(http_strerror_tab));
  2335. return http_strerror_tab[(size_t)err].name;
  2336. }
  2337. template<typename = void>
  2338. const char *
  2339. http_errno_description(http_errno err) {
  2340. assert(((size_t) err) < HTTP_ARRAY_SIZE(http_strerror_tab));
  2341. return http_strerror_tab[(size_t)err].description;
  2342. }
  2343. template<typename = void>
  2344. static http_host_state
  2345. http_parse_host_char(http_host_state s, const char ch) {
  2346. switch(s) {
  2347. case http_host_state::s_http_userinfo:
  2348. case http_host_state::s_http_userinfo_start:
  2349. if (ch == '@') {
  2350. return http_host_state::s_http_host_start;
  2351. }
  2352. if (HTTP_IS_USERINFO_CHAR(ch)) {
  2353. return http_host_state::s_http_userinfo;
  2354. }
  2355. break;
  2356. case http_host_state::s_http_host_start:
  2357. if (ch == '[') {
  2358. return http_host_state::s_http_host_v6_start;
  2359. }
  2360. if (HTTP_IS_HOST_CHAR(ch)) {
  2361. return http_host_state::s_http_host;
  2362. }
  2363. break;
  2364. case http_host_state::s_http_host:
  2365. if (HTTP_IS_HOST_CHAR(ch)) {
  2366. return http_host_state::s_http_host;
  2367. }
  2368. /* fall through */
  2369. case http_host_state::s_http_host_v6_end:
  2370. if (ch == ':') {
  2371. return http_host_state::s_http_host_port_start;
  2372. }
  2373. break;
  2374. case http_host_state::s_http_host_v6:
  2375. if (ch == ']') {
  2376. return http_host_state::s_http_host_v6_end;
  2377. }
  2378. /* fall through */
  2379. case http_host_state::s_http_host_v6_start:
  2380. if (HTTP_IS_HEX(ch) || ch == ':' || ch == '.') {
  2381. return http_host_state::s_http_host_v6;
  2382. }
  2383. if (s == http_host_state::s_http_host_v6 && ch == '%') {
  2384. return http_host_state::s_http_host_v6_zone_start;
  2385. }
  2386. break;
  2387. case http_host_state::s_http_host_v6_zone:
  2388. if (ch == ']') {
  2389. return http_host_state::s_http_host_v6_end;
  2390. }
  2391. /* fall through */
  2392. case http_host_state::s_http_host_v6_zone_start:
  2393. /* RFC 6874 Zone ID consists of 1*( unreserved / pct-encoded) */
  2394. if (HTTP_IS_ALPHANUM(ch) || ch == '%' || ch == '.' || ch == '-' || ch == '_' ||
  2395. ch == '~') {
  2396. return http_host_state::s_http_host_v6_zone;
  2397. }
  2398. break;
  2399. case http_host_state::s_http_host_port:
  2400. case http_host_state::s_http_host_port_start:
  2401. if (HTTP_IS_NUM(ch)) {
  2402. return http_host_state::s_http_host_port;
  2403. }
  2404. break;
  2405. default:
  2406. break;
  2407. }
  2408. return http_host_state::s_http_host_dead;
  2409. }
  2410. template<typename = void>
  2411. static int
  2412. http_parse_host(const char * buf, struct http_parser_url *u, int found_at) {
  2413. http_host_state s;
  2414. const char *p;
  2415. size_t buflen = u->field_data[(int)url_fields::UF_HOST].off + u->field_data[(int)url_fields::UF_HOST].len;
  2416. assert(u->field_set & (1 << (int)url_fields::UF_HOST));
  2417. u->field_data[(int)url_fields::UF_HOST].len = 0;
  2418. s = found_at ? http_host_state::s_http_userinfo_start : http_host_state::s_http_host_start;
  2419. for (p = buf + u->field_data[(int)url_fields::UF_HOST].off; p < buf + buflen; ++p) {
  2420. http_host_state new_s = http_parse_host_char(s, *p);
  2421. if (new_s == http_host_state::s_http_host_dead) {
  2422. return 1;
  2423. }
  2424. switch(new_s) {
  2425. case http_host_state::s_http_host:
  2426. if (s != http_host_state::s_http_host) {
  2427. u->field_data[(int)url_fields::UF_HOST].off = static_cast<uint16_t>(p - buf);
  2428. }
  2429. u->field_data[(int)url_fields::UF_HOST].len++;
  2430. break;
  2431. case http_host_state::s_http_host_v6:
  2432. if (s != http_host_state::s_http_host_v6) {
  2433. u->field_data[(int)url_fields::UF_HOST].off = static_cast<uint16_t>(p - buf);
  2434. }
  2435. u->field_data[(int)url_fields::UF_HOST].len++;
  2436. break;
  2437. case http_host_state::s_http_host_v6_zone_start:
  2438. case http_host_state::s_http_host_v6_zone:
  2439. u->field_data[(int)url_fields::UF_HOST].len++;
  2440. break;
  2441. case http_host_state::s_http_host_port:
  2442. if (s != http_host_state::s_http_host_port) {
  2443. u->field_data[(int)url_fields::UF_PORT].off = static_cast<uint16_t>(p - buf);
  2444. u->field_data[(int)url_fields::UF_PORT].len = 0;
  2445. u->field_set |= (1 << (int)url_fields::UF_PORT);
  2446. }
  2447. u->field_data[(int)url_fields::UF_PORT].len++;
  2448. break;
  2449. case http_host_state::s_http_userinfo:
  2450. if (s != http_host_state::s_http_userinfo) {
  2451. u->field_data[(int)url_fields::UF_USERINFO].off = static_cast<uint16_t>(p - buf);
  2452. u->field_data[(int)url_fields::UF_USERINFO].len = 0;
  2453. u->field_set |= (1 << (int)url_fields::UF_USERINFO);
  2454. }
  2455. u->field_data[(int)url_fields::UF_USERINFO].len++;
  2456. break;
  2457. default:
  2458. break;
  2459. }
  2460. s = new_s;
  2461. }
  2462. /* Make sure we don't end somewhere unexpected */
  2463. switch (s) {
  2464. case http_host_state::s_http_host_start:
  2465. case http_host_state::s_http_host_v6_start:
  2466. case http_host_state::s_http_host_v6:
  2467. case http_host_state::s_http_host_v6_zone_start:
  2468. case http_host_state::s_http_host_v6_zone:
  2469. case http_host_state::s_http_host_port_start:
  2470. case http_host_state::s_http_userinfo:
  2471. case http_host_state::s_http_userinfo_start:
  2472. return 1;
  2473. default:
  2474. break;
  2475. }
  2476. return 0;
  2477. }
  2478. template<typename = void>
  2479. void
  2480. http_parser_url_init(struct http_parser_url *u) {
  2481. memset(u, 0, sizeof(*u));
  2482. }
  2483. template<typename = void>
  2484. int
  2485. http_parser_parse_url(const char *buf, size_t buflen, int is_connect,
  2486. struct http_parser_url *u)
  2487. {
  2488. state s;
  2489. const char *p;
  2490. url_fields uf, old_uf;
  2491. int found_at = 0;
  2492. if (buflen == 0) {
  2493. return 1;
  2494. }
  2495. u->port = u->field_set = 0;
  2496. s = is_connect ? state::s_req_server_start : state::s_req_spaces_before_url;
  2497. old_uf = url_fields::UF_MAX;
  2498. for (p = buf; p < buf + buflen; ++p) {
  2499. s = parse_url_char(s, *p);
  2500. /* Figure out the next field that we're operating on */
  2501. switch (s) {
  2502. case state::s_dead:
  2503. return 1;
  2504. /* Skip delimeters */
  2505. case state::s_req_schema_slash:
  2506. case state::s_req_schema_slash_slash:
  2507. case state::s_req_server_start:
  2508. case state::s_req_query_string_start:
  2509. case state::s_req_fragment_start:
  2510. continue;
  2511. case state::s_req_schema:
  2512. uf = url_fields::UF_SCHEMA;
  2513. break;
  2514. case state::s_req_server_with_at:
  2515. found_at = 1;
  2516. /* fall through */
  2517. case state::s_req_server:
  2518. uf = url_fields::UF_HOST;
  2519. break;
  2520. case state::s_req_path:
  2521. uf = url_fields::UF_PATH;
  2522. break;
  2523. case state::s_req_query_string:
  2524. uf = url_fields::UF_QUERY;
  2525. break;
  2526. case state::s_req_fragment:
  2527. uf = url_fields::UF_FRAGMENT;
  2528. break;
  2529. default:
  2530. assert(!"Unexpected state");
  2531. return 1;
  2532. }
  2533. /* Nothing's changed; soldier on */
  2534. if (uf == old_uf) {
  2535. u->field_data[(int)uf].len++;
  2536. continue;
  2537. }
  2538. u->field_data[(int)uf].off = static_cast<uint16_t>(p - buf);
  2539. u->field_data[(int)uf].len = 1;
  2540. u->field_set |= uint16_t(1 << (int)uf);
  2541. old_uf = uf;
  2542. }
  2543. /* host must be present if there is a schema */
  2544. /* parsing http:///toto will fail */
  2545. if ((u->field_set & (1 << (int)url_fields::UF_SCHEMA)) &&
  2546. (u->field_set & (1 << (int)url_fields::UF_HOST)) == 0) {
  2547. return 1;
  2548. }
  2549. if (u->field_set & (1 << (int)url_fields::UF_HOST)) {
  2550. if (http_parse_host(buf, u, found_at) != 0) {
  2551. return 1;
  2552. }
  2553. }
  2554. /* CONNECT requests can only contain "hostname:port" */
  2555. if (is_connect && u->field_set != ((1 << (int)url_fields::UF_HOST)|(1 << (int)url_fields::UF_PORT))) {
  2556. return 1;
  2557. }
  2558. if (u->field_set & (1 << (int)url_fields::UF_PORT)) {
  2559. uint16_t off;
  2560. uint16_t len;
  2561. const char* pn;
  2562. const char* end;
  2563. unsigned long v;
  2564. off = u->field_data[(int)url_fields::UF_PORT].off;
  2565. len = u->field_data[(int)url_fields::UF_PORT].len;
  2566. end = buf + off + len;
  2567. /* NOTE: The characters are already validated and are in the [0-9] range */
  2568. assert(size_t(off + len) <= buflen && "Port number overflow");
  2569. v = 0;
  2570. for (pn = buf + off; pn < end; ++pn) {
  2571. v *= 10;
  2572. v += *pn - '0';
  2573. /* Ports have a max value of 2^16 */
  2574. if (v > 0xffff) {
  2575. return 1;
  2576. }
  2577. }
  2578. u->port = (uint16_t) v;
  2579. }
  2580. return 0;
  2581. }
  2582. template<typename = void>
  2583. void
  2584. http_parser_pause(http_parser *parser, int paused) {
  2585. /* Users should only be pausing/unpausing a parser that is not in an error
  2586. * state. In non-debug builds, there's not much that we can do about this
  2587. * other than ignore it.
  2588. */
  2589. if (HTTP_PARSER_ERRNO(parser) == http_errno::HPE_OK ||
  2590. HTTP_PARSER_ERRNO(parser) == http_errno::HPE_PAUSED) {
  2591. uint32_t nread = parser->nread; /* used by the HTTP_SET_ERRNO macro */
  2592. HTTP_SET_ERRNO((paused) ? http_errno::HPE_PAUSED : http_errno::HPE_OK);
  2593. } else {
  2594. assert(0 && "Attempting to pause parser in error state");
  2595. }
  2596. }
  2597. /* Checks if this is the final chunk of the body. */
  2598. template<typename = void>
  2599. int http_body_is_final(const struct http_parser *parser) {
  2600. return parser->state == (unsigned int)(state::s_message_done);
  2601. }
  2602. /* Change the maximum header size provided at compile time. */
  2603. template<typename = void>
  2604. void http_parser_set_max_header_size(uint32_t size)
  2605. {
  2606. max_header_size = size;
  2607. }
  2608. template<typename = void>
  2609. unsigned long
  2610. http_parser_version(void) {
  2611. return HTTP_PARSER_VERSION_MAJOR * 0x10000 |
  2612. HTTP_PARSER_VERSION_MINOR * 0x00100 |
  2613. HTTP_PARSER_VERSION_PATCH * 0x00001;
  2614. }
  2615. #undef HTTP_CR
  2616. #undef HTTP_LF
  2617. #undef HTTP_LOWER
  2618. #undef HTTP_IS_ALPHA
  2619. #undef HTTP_IS_NUM
  2620. #undef HTTP_IS_ALPHANUM
  2621. #undef HTTP_IS_HEX
  2622. #undef HTTP_IS_MARK
  2623. #undef HTTP_IS_USERINFO_CHAR
  2624. #undef HTTP_STRICT_TOKEN
  2625. #undef HTTP_TOKEN
  2626. #undef HTTP_IS_URL_CHAR
  2627. #undef HTTP_IS_HOST_CHAR
  2628. #undef HTTP_IS_HEADER_CHAR
  2629. #undef start_state
  2630. #undef HTTP_STRICT_CHECK
  2631. #undef HTTP_NEW_MESSAGE
  2632. #undef HTTP_STRERROR_GEN
  2633. #undef HTTP_PARSING_HEADER
  2634. #undef T
  2635. #undef HTTP_PROXY_CONNECTION
  2636. #undef HTTP_CONNECTION
  2637. #undef HTTP_CONTENT_LENGTH
  2638. #undef HTTP_TRANSFER_ENCODING
  2639. #undef HTTP_UPGRADE
  2640. #undef HTTP_CHUNKED
  2641. #undef HTTP_KEEP_ALIVE
  2642. #undef HTTP_CLOSE
  2643. #undef HTTP_COUNT_HEADER_SIZE
  2644. #undef HTTP_MARK
  2645. #undef HTTP_CALLBACK_DATA_NOADVANCE
  2646. #undef HTTP_CALLBACK_DATA
  2647. #undef HTTP_CALLBACK_DATA_
  2648. #undef HTTP_CALLBACK_NOTIFY_NOADVANCE
  2649. #undef HTTP_CALLBACK_NOTIFY
  2650. #undef HTTP_CALLBACK_NOTIFY_
  2651. #undef HTTP_UNLIKELY
  2652. #undef HTTP_LIKELY
  2653. #undef HTTP_REEXECUTE
  2654. #undef HTTP_RETURN
  2655. #undef HTTP_UPDATE_STATE
  2656. #undef HTTP_CURRENT_STATE
  2657. #undef HTTP_SET_ERRNO
  2658. #undef HTTP_ELEM_AT
  2659. #undef HTTP_BIT_AT
  2660. #undef HTTP_ARRAY_SIZE
  2661. #undef HTTP_MIN
  2662. #undef HTTP_ULLONG_MAX
  2663. #undef HTTP_PARSER_ERRNO
  2664. #undef HTTP_ERRNO_GEN
  2665. #undef HTTP_ERRNO_MAP
  2666. #undef HTTP_XX
  2667. #undef HTTP_STATUS_MAP
  2668. #undef HTTP_MAX_HEADER_SIZE
  2669. #undef HTTP_PARSER_STRICT
  2670. //#ifdef __cplusplus
  2671. //}
  2672. //#endif
  2673. }
  2674. #include <asio2/base/detail/pop_options.hpp>
  2675. #endif