test.c 108 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132
  1. /* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
  2. *
  3. * Permission is hereby granted, free of charge, to any person obtaining a copy
  4. * of this software and associated documentation files (the "Software"), to
  5. * deal in the Software without restriction, including without limitation the
  6. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  7. * sell copies of the Software, and to permit persons to whom the Software is
  8. * furnished to do so, subject to the following conditions:
  9. *
  10. * The above copyright notice and this permission notice shall be included in
  11. * all copies or substantial portions of the Software.
  12. *
  13. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  18. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  19. * IN THE SOFTWARE.
  20. */
  21. #include "http_parser.h"
  22. #include <stdlib.h>
  23. #include <assert.h>
  24. #include <stdio.h>
  25. #include <stdlib.h> /* rand */
  26. #include <string.h>
  27. #include <stdarg.h>
  28. #if defined(__APPLE__)
  29. # undef strlcat
  30. # undef strlncpy
  31. # undef strlcpy
  32. #endif /* defined(__APPLE__) */
  33. #undef TRUE
  34. #define TRUE 1
  35. #undef FALSE
  36. #define FALSE 0
  37. #define MAX_HEADERS 13
  38. #define MAX_ELEMENT_SIZE 2048
  39. #define MAX_CHUNKS 16
  40. #define MIN(a,b) ((a) < (b) ? (a) : (b))
  41. static http_parser *parser;
  42. struct message {
  43. const char *name; // for debugging purposes
  44. const char *raw;
  45. enum http_parser_type type;
  46. enum http_method method;
  47. int status_code;
  48. char response_status[MAX_ELEMENT_SIZE];
  49. char request_path[MAX_ELEMENT_SIZE];
  50. char request_url[MAX_ELEMENT_SIZE];
  51. char fragment[MAX_ELEMENT_SIZE];
  52. char query_string[MAX_ELEMENT_SIZE];
  53. char body[MAX_ELEMENT_SIZE];
  54. size_t body_size;
  55. const char *host;
  56. const char *userinfo;
  57. uint16_t port;
  58. int num_headers;
  59. enum { NONE=0, FIELD, VALUE } last_header_element;
  60. char headers [MAX_HEADERS][2][MAX_ELEMENT_SIZE];
  61. int should_keep_alive;
  62. int num_chunks;
  63. int num_chunks_complete;
  64. int chunk_lengths[MAX_CHUNKS];
  65. const char *upgrade; // upgraded body
  66. unsigned short http_major;
  67. unsigned short http_minor;
  68. int message_begin_cb_called;
  69. int headers_complete_cb_called;
  70. int message_complete_cb_called;
  71. int message_complete_on_eof;
  72. int body_is_final;
  73. };
  74. static int currently_parsing_eof;
  75. static struct message messages[5];
  76. static int num_messages;
  77. static http_parser_settings *current_pause_parser;
  78. /* * R E Q U E S T S * */
  79. const struct message requests[] =
  80. #define CURL_GET 0
  81. { {.name= "curl get"
  82. ,.type= HTTP_REQUEST
  83. ,.raw= "GET /test HTTP/1.1\r\n"
  84. "User-Agent: curl/7.18.0 (i486-pc-linux-gnu) libcurl/7.18.0 OpenSSL/0.9.8g zlib/1.2.3.3 libidn/1.1\r\n"
  85. "Host: 0.0.0.0=5000\r\n"
  86. "Accept: */*\r\n"
  87. "\r\n"
  88. ,.should_keep_alive= TRUE
  89. ,.message_complete_on_eof= FALSE
  90. ,.http_major= 1
  91. ,.http_minor= 1
  92. ,.method= HTTP_GET
  93. ,.query_string= ""
  94. ,.fragment= ""
  95. ,.request_path= "/test"
  96. ,.request_url= "/test"
  97. ,.num_headers= 3
  98. ,.headers=
  99. { { "User-Agent", "curl/7.18.0 (i486-pc-linux-gnu) libcurl/7.18.0 OpenSSL/0.9.8g zlib/1.2.3.3 libidn/1.1" }
  100. , { "Host", "0.0.0.0=5000" }
  101. , { "Accept", "*/*" }
  102. }
  103. ,.body= ""
  104. }
  105. #define FIREFOX_GET 1
  106. , {.name= "firefox get"
  107. ,.type= HTTP_REQUEST
  108. ,.raw= "GET /favicon.ico HTTP/1.1\r\n"
  109. "Host: 0.0.0.0=5000\r\n"
  110. "User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9) Gecko/2008061015 Firefox/3.0\r\n"
  111. "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\n"
  112. "Accept-Language: en-us,en;q=0.5\r\n"
  113. "Accept-Encoding: gzip,deflate\r\n"
  114. "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7\r\n"
  115. "Keep-Alive: 300\r\n"
  116. "Connection: keep-alive\r\n"
  117. "\r\n"
  118. ,.should_keep_alive= TRUE
  119. ,.message_complete_on_eof= FALSE
  120. ,.http_major= 1
  121. ,.http_minor= 1
  122. ,.method= HTTP_GET
  123. ,.query_string= ""
  124. ,.fragment= ""
  125. ,.request_path= "/favicon.ico"
  126. ,.request_url= "/favicon.ico"
  127. ,.num_headers= 8
  128. ,.headers=
  129. { { "Host", "0.0.0.0=5000" }
  130. , { "User-Agent", "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9) Gecko/2008061015 Firefox/3.0" }
  131. , { "Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8" }
  132. , { "Accept-Language", "en-us,en;q=0.5" }
  133. , { "Accept-Encoding", "gzip,deflate" }
  134. , { "Accept-Charset", "ISO-8859-1,utf-8;q=0.7,*;q=0.7" }
  135. , { "Keep-Alive", "300" }
  136. , { "Connection", "keep-alive" }
  137. }
  138. ,.body= ""
  139. }
  140. #define DUMBFUCK 2
  141. , {.name= "dumbfuck"
  142. ,.type= HTTP_REQUEST
  143. ,.raw= "GET /dumbfuck HTTP/1.1\r\n"
  144. "aaaaaaaaaaaaa:++++++++++\r\n"
  145. "\r\n"
  146. ,.should_keep_alive= TRUE
  147. ,.message_complete_on_eof= FALSE
  148. ,.http_major= 1
  149. ,.http_minor= 1
  150. ,.method= HTTP_GET
  151. ,.query_string= ""
  152. ,.fragment= ""
  153. ,.request_path= "/dumbfuck"
  154. ,.request_url= "/dumbfuck"
  155. ,.num_headers= 1
  156. ,.headers=
  157. { { "aaaaaaaaaaaaa", "++++++++++" }
  158. }
  159. ,.body= ""
  160. }
  161. #define FRAGMENT_IN_URI 3
  162. , {.name= "fragment in url"
  163. ,.type= HTTP_REQUEST
  164. ,.raw= "GET /forums/1/topics/2375?page=1#posts-17408 HTTP/1.1\r\n"
  165. "\r\n"
  166. ,.should_keep_alive= TRUE
  167. ,.message_complete_on_eof= FALSE
  168. ,.http_major= 1
  169. ,.http_minor= 1
  170. ,.method= HTTP_GET
  171. ,.query_string= "page=1"
  172. ,.fragment= "posts-17408"
  173. ,.request_path= "/forums/1/topics/2375"
  174. /* XXX request url does include fragment? */
  175. ,.request_url= "/forums/1/topics/2375?page=1#posts-17408"
  176. ,.num_headers= 0
  177. ,.body= ""
  178. }
  179. #define GET_NO_HEADERS_NO_BODY 4
  180. , {.name= "get no headers no body"
  181. ,.type= HTTP_REQUEST
  182. ,.raw= "GET /get_no_headers_no_body/world HTTP/1.1\r\n"
  183. "\r\n"
  184. ,.should_keep_alive= TRUE
  185. ,.message_complete_on_eof= FALSE /* would need Connection: close */
  186. ,.http_major= 1
  187. ,.http_minor= 1
  188. ,.method= HTTP_GET
  189. ,.query_string= ""
  190. ,.fragment= ""
  191. ,.request_path= "/get_no_headers_no_body/world"
  192. ,.request_url= "/get_no_headers_no_body/world"
  193. ,.num_headers= 0
  194. ,.body= ""
  195. }
  196. #define GET_ONE_HEADER_NO_BODY 5
  197. , {.name= "get one header no body"
  198. ,.type= HTTP_REQUEST
  199. ,.raw= "GET /get_one_header_no_body HTTP/1.1\r\n"
  200. "Accept: */*\r\n"
  201. "\r\n"
  202. ,.should_keep_alive= TRUE
  203. ,.message_complete_on_eof= FALSE /* would need Connection: close */
  204. ,.http_major= 1
  205. ,.http_minor= 1
  206. ,.method= HTTP_GET
  207. ,.query_string= ""
  208. ,.fragment= ""
  209. ,.request_path= "/get_one_header_no_body"
  210. ,.request_url= "/get_one_header_no_body"
  211. ,.num_headers= 1
  212. ,.headers=
  213. { { "Accept" , "*/*" }
  214. }
  215. ,.body= ""
  216. }
  217. #define GET_FUNKY_CONTENT_LENGTH 6
  218. , {.name= "get funky content length body hello"
  219. ,.type= HTTP_REQUEST
  220. ,.raw= "GET /get_funky_content_length_body_hello HTTP/1.0\r\n"
  221. "conTENT-Length: 5\r\n"
  222. "\r\n"
  223. "HELLO"
  224. ,.should_keep_alive= FALSE
  225. ,.message_complete_on_eof= FALSE
  226. ,.http_major= 1
  227. ,.http_minor= 0
  228. ,.method= HTTP_GET
  229. ,.query_string= ""
  230. ,.fragment= ""
  231. ,.request_path= "/get_funky_content_length_body_hello"
  232. ,.request_url= "/get_funky_content_length_body_hello"
  233. ,.num_headers= 1
  234. ,.headers=
  235. { { "conTENT-Length" , "5" }
  236. }
  237. ,.body= "HELLO"
  238. }
  239. #define POST_IDENTITY_BODY_WORLD 7
  240. , {.name= "post identity body world"
  241. ,.type= HTTP_REQUEST
  242. ,.raw= "POST /post_identity_body_world?q=search#hey HTTP/1.1\r\n"
  243. "Accept: */*\r\n"
  244. "Transfer-Encoding: identity\r\n"
  245. "Content-Length: 5\r\n"
  246. "\r\n"
  247. "World"
  248. ,.should_keep_alive= TRUE
  249. ,.message_complete_on_eof= FALSE
  250. ,.http_major= 1
  251. ,.http_minor= 1
  252. ,.method= HTTP_POST
  253. ,.query_string= "q=search"
  254. ,.fragment= "hey"
  255. ,.request_path= "/post_identity_body_world"
  256. ,.request_url= "/post_identity_body_world?q=search#hey"
  257. ,.num_headers= 3
  258. ,.headers=
  259. { { "Accept", "*/*" }
  260. , { "Transfer-Encoding", "identity" }
  261. , { "Content-Length", "5" }
  262. }
  263. ,.body= "World"
  264. }
  265. #define POST_CHUNKED_ALL_YOUR_BASE 8
  266. , {.name= "post - chunked body: all your base are belong to us"
  267. ,.type= HTTP_REQUEST
  268. ,.raw= "POST /post_chunked_all_your_base HTTP/1.1\r\n"
  269. "Transfer-Encoding: chunked\r\n"
  270. "\r\n"
  271. "1e\r\nall your base are belong to us\r\n"
  272. "0\r\n"
  273. "\r\n"
  274. ,.should_keep_alive= TRUE
  275. ,.message_complete_on_eof= FALSE
  276. ,.http_major= 1
  277. ,.http_minor= 1
  278. ,.method= HTTP_POST
  279. ,.query_string= ""
  280. ,.fragment= ""
  281. ,.request_path= "/post_chunked_all_your_base"
  282. ,.request_url= "/post_chunked_all_your_base"
  283. ,.num_headers= 1
  284. ,.headers=
  285. { { "Transfer-Encoding" , "chunked" }
  286. }
  287. ,.body= "all your base are belong to us"
  288. ,.num_chunks_complete= 2
  289. ,.chunk_lengths= { 0x1e }
  290. }
  291. #define TWO_CHUNKS_MULT_ZERO_END 9
  292. , {.name= "two chunks ; triple zero ending"
  293. ,.type= HTTP_REQUEST
  294. ,.raw= "POST /two_chunks_mult_zero_end HTTP/1.1\r\n"
  295. "Transfer-Encoding: chunked\r\n"
  296. "\r\n"
  297. "5\r\nhello\r\n"
  298. "6\r\n world\r\n"
  299. "000\r\n"
  300. "\r\n"
  301. ,.should_keep_alive= TRUE
  302. ,.message_complete_on_eof= FALSE
  303. ,.http_major= 1
  304. ,.http_minor= 1
  305. ,.method= HTTP_POST
  306. ,.query_string= ""
  307. ,.fragment= ""
  308. ,.request_path= "/two_chunks_mult_zero_end"
  309. ,.request_url= "/two_chunks_mult_zero_end"
  310. ,.num_headers= 1
  311. ,.headers=
  312. { { "Transfer-Encoding", "chunked" }
  313. }
  314. ,.body= "hello world"
  315. ,.num_chunks_complete= 3
  316. ,.chunk_lengths= { 5, 6 }
  317. }
  318. #define CHUNKED_W_TRAILING_HEADERS 10
  319. , {.name= "chunked with trailing headers. blech."
  320. ,.type= HTTP_REQUEST
  321. ,.raw= "POST /chunked_w_trailing_headers HTTP/1.1\r\n"
  322. "Transfer-Encoding: chunked\r\n"
  323. "\r\n"
  324. "5\r\nhello\r\n"
  325. "6\r\n world\r\n"
  326. "0\r\n"
  327. "Vary: *\r\n"
  328. "Content-Type: text/plain\r\n"
  329. "\r\n"
  330. ,.should_keep_alive= TRUE
  331. ,.message_complete_on_eof= FALSE
  332. ,.http_major= 1
  333. ,.http_minor= 1
  334. ,.method= HTTP_POST
  335. ,.query_string= ""
  336. ,.fragment= ""
  337. ,.request_path= "/chunked_w_trailing_headers"
  338. ,.request_url= "/chunked_w_trailing_headers"
  339. ,.num_headers= 3
  340. ,.headers=
  341. { { "Transfer-Encoding", "chunked" }
  342. , { "Vary", "*" }
  343. , { "Content-Type", "text/plain" }
  344. }
  345. ,.body= "hello world"
  346. ,.num_chunks_complete= 3
  347. ,.chunk_lengths= { 5, 6 }
  348. }
  349. #define CHUNKED_W_BULLSHIT_AFTER_LENGTH 11
  350. , {.name= "with bullshit after the length"
  351. ,.type= HTTP_REQUEST
  352. ,.raw= "POST /chunked_w_bullshit_after_length HTTP/1.1\r\n"
  353. "Transfer-Encoding: chunked\r\n"
  354. "\r\n"
  355. "5; ihatew3;whatthefuck=aretheseparametersfor\r\nhello\r\n"
  356. "6; blahblah; blah\r\n world\r\n"
  357. "0\r\n"
  358. "\r\n"
  359. ,.should_keep_alive= TRUE
  360. ,.message_complete_on_eof= FALSE
  361. ,.http_major= 1
  362. ,.http_minor= 1
  363. ,.method= HTTP_POST
  364. ,.query_string= ""
  365. ,.fragment= ""
  366. ,.request_path= "/chunked_w_bullshit_after_length"
  367. ,.request_url= "/chunked_w_bullshit_after_length"
  368. ,.num_headers= 1
  369. ,.headers=
  370. { { "Transfer-Encoding", "chunked" }
  371. }
  372. ,.body= "hello world"
  373. ,.num_chunks_complete= 3
  374. ,.chunk_lengths= { 5, 6 }
  375. }
  376. #define WITH_QUOTES 12
  377. , {.name= "with quotes"
  378. ,.type= HTTP_REQUEST
  379. ,.raw= "GET /with_\"stupid\"_quotes?foo=\"bar\" HTTP/1.1\r\n\r\n"
  380. ,.should_keep_alive= TRUE
  381. ,.message_complete_on_eof= FALSE
  382. ,.http_major= 1
  383. ,.http_minor= 1
  384. ,.method= HTTP_GET
  385. ,.query_string= "foo=\"bar\""
  386. ,.fragment= ""
  387. ,.request_path= "/with_\"stupid\"_quotes"
  388. ,.request_url= "/with_\"stupid\"_quotes?foo=\"bar\""
  389. ,.num_headers= 0
  390. ,.headers= { }
  391. ,.body= ""
  392. }
  393. #define APACHEBENCH_GET 13
  394. /* The server receiving this request SHOULD NOT wait for EOF
  395. * to know that content-length == 0.
  396. * How to represent this in a unit test? message_complete_on_eof
  397. * Compare with NO_CONTENT_LENGTH_RESPONSE.
  398. */
  399. , {.name = "apachebench get"
  400. ,.type= HTTP_REQUEST
  401. ,.raw= "GET /test HTTP/1.0\r\n"
  402. "Host: 0.0.0.0:5000\r\n"
  403. "User-Agent: ApacheBench/2.3\r\n"
  404. "Accept: */*\r\n\r\n"
  405. ,.should_keep_alive= FALSE
  406. ,.message_complete_on_eof= FALSE
  407. ,.http_major= 1
  408. ,.http_minor= 0
  409. ,.method= HTTP_GET
  410. ,.query_string= ""
  411. ,.fragment= ""
  412. ,.request_path= "/test"
  413. ,.request_url= "/test"
  414. ,.num_headers= 3
  415. ,.headers= { { "Host", "0.0.0.0:5000" }
  416. , { "User-Agent", "ApacheBench/2.3" }
  417. , { "Accept", "*/*" }
  418. }
  419. ,.body= ""
  420. }
  421. #define QUERY_URL_WITH_QUESTION_MARK_GET 14
  422. /* Some clients include '?' characters in query strings.
  423. */
  424. , {.name = "query url with question mark"
  425. ,.type= HTTP_REQUEST
  426. ,.raw= "GET /test.cgi?foo=bar?baz HTTP/1.1\r\n\r\n"
  427. ,.should_keep_alive= TRUE
  428. ,.message_complete_on_eof= FALSE
  429. ,.http_major= 1
  430. ,.http_minor= 1
  431. ,.method= HTTP_GET
  432. ,.query_string= "foo=bar?baz"
  433. ,.fragment= ""
  434. ,.request_path= "/test.cgi"
  435. ,.request_url= "/test.cgi?foo=bar?baz"
  436. ,.num_headers= 0
  437. ,.headers= {}
  438. ,.body= ""
  439. }
  440. #define PREFIX_NEWLINE_GET 15
  441. /* Some clients, especially after a POST in a keep-alive connection,
  442. * will send an extra CRLF before the next request
  443. */
  444. , {.name = "newline prefix get"
  445. ,.type= HTTP_REQUEST
  446. ,.raw= "\r\nGET /test HTTP/1.1\r\n\r\n"
  447. ,.should_keep_alive= TRUE
  448. ,.message_complete_on_eof= FALSE
  449. ,.http_major= 1
  450. ,.http_minor= 1
  451. ,.method= HTTP_GET
  452. ,.query_string= ""
  453. ,.fragment= ""
  454. ,.request_path= "/test"
  455. ,.request_url= "/test"
  456. ,.num_headers= 0
  457. ,.headers= { }
  458. ,.body= ""
  459. }
  460. #define UPGRADE_REQUEST 16
  461. , {.name = "upgrade request"
  462. ,.type= HTTP_REQUEST
  463. ,.raw= "GET /demo HTTP/1.1\r\n"
  464. "Host: example.com\r\n"
  465. "Connection: Upgrade\r\n"
  466. "Sec-WebSocket-Key2: 12998 5 Y3 1 .P00\r\n"
  467. "Sec-WebSocket-Protocol: sample\r\n"
  468. "Upgrade: WebSocket\r\n"
  469. "Sec-WebSocket-Key1: 4 @1 46546xW%0l 1 5\r\n"
  470. "Origin: http://example.com\r\n"
  471. "\r\n"
  472. "Hot diggity dogg"
  473. ,.should_keep_alive= TRUE
  474. ,.message_complete_on_eof= FALSE
  475. ,.http_major= 1
  476. ,.http_minor= 1
  477. ,.method= HTTP_GET
  478. ,.query_string= ""
  479. ,.fragment= ""
  480. ,.request_path= "/demo"
  481. ,.request_url= "/demo"
  482. ,.num_headers= 7
  483. ,.upgrade="Hot diggity dogg"
  484. ,.headers= { { "Host", "example.com" }
  485. , { "Connection", "Upgrade" }
  486. , { "Sec-WebSocket-Key2", "12998 5 Y3 1 .P00" }
  487. , { "Sec-WebSocket-Protocol", "sample" }
  488. , { "Upgrade", "WebSocket" }
  489. , { "Sec-WebSocket-Key1", "4 @1 46546xW%0l 1 5" }
  490. , { "Origin", "http://example.com" }
  491. }
  492. ,.body= ""
  493. }
  494. #define CONNECT_REQUEST 17
  495. , {.name = "connect request"
  496. ,.type= HTTP_REQUEST
  497. ,.raw= "CONNECT 0-home0.netscape.com:443 HTTP/1.0\r\n"
  498. "User-agent: Mozilla/1.1N\r\n"
  499. "Proxy-authorization: basic aGVsbG86d29ybGQ=\r\n"
  500. "\r\n"
  501. "some data\r\n"
  502. "and yet even more data"
  503. ,.should_keep_alive= FALSE
  504. ,.message_complete_on_eof= FALSE
  505. ,.http_major= 1
  506. ,.http_minor= 0
  507. ,.method= HTTP_CONNECT
  508. ,.query_string= ""
  509. ,.fragment= ""
  510. ,.request_path= ""
  511. ,.request_url= "0-home0.netscape.com:443"
  512. ,.num_headers= 2
  513. ,.upgrade="some data\r\nand yet even more data"
  514. ,.headers= { { "User-agent", "Mozilla/1.1N" }
  515. , { "Proxy-authorization", "basic aGVsbG86d29ybGQ=" }
  516. }
  517. ,.body= ""
  518. }
  519. #define REPORT_REQ 18
  520. , {.name= "report request"
  521. ,.type= HTTP_REQUEST
  522. ,.raw= "REPORT /test HTTP/1.1\r\n"
  523. "\r\n"
  524. ,.should_keep_alive= TRUE
  525. ,.message_complete_on_eof= FALSE
  526. ,.http_major= 1
  527. ,.http_minor= 1
  528. ,.method= HTTP_REPORT
  529. ,.query_string= ""
  530. ,.fragment= ""
  531. ,.request_path= "/test"
  532. ,.request_url= "/test"
  533. ,.num_headers= 0
  534. ,.headers= {}
  535. ,.body= ""
  536. }
  537. #define NO_HTTP_VERSION 19
  538. , {.name= "request with no http version"
  539. ,.type= HTTP_REQUEST
  540. ,.raw= "GET /\r\n"
  541. "\r\n"
  542. ,.should_keep_alive= FALSE
  543. ,.message_complete_on_eof= FALSE
  544. ,.http_major= 0
  545. ,.http_minor= 9
  546. ,.method= HTTP_GET
  547. ,.query_string= ""
  548. ,.fragment= ""
  549. ,.request_path= "/"
  550. ,.request_url= "/"
  551. ,.num_headers= 0
  552. ,.headers= {}
  553. ,.body= ""
  554. }
  555. #define MSEARCH_REQ 20
  556. , {.name= "m-search request"
  557. ,.type= HTTP_REQUEST
  558. ,.raw= "M-SEARCH * HTTP/1.1\r\n"
  559. "HOST: 239.255.255.250:1900\r\n"
  560. "MAN: \"ssdp:discover\"\r\n"
  561. "ST: \"ssdp:all\"\r\n"
  562. "\r\n"
  563. ,.should_keep_alive= TRUE
  564. ,.message_complete_on_eof= FALSE
  565. ,.http_major= 1
  566. ,.http_minor= 1
  567. ,.method= HTTP_MSEARCH
  568. ,.query_string= ""
  569. ,.fragment= ""
  570. ,.request_path= "*"
  571. ,.request_url= "*"
  572. ,.num_headers= 3
  573. ,.headers= { { "HOST", "239.255.255.250:1900" }
  574. , { "MAN", "\"ssdp:discover\"" }
  575. , { "ST", "\"ssdp:all\"" }
  576. }
  577. ,.body= ""
  578. }
  579. #define LINE_FOLDING_IN_HEADER 21
  580. , {.name= "line folding in header value"
  581. ,.type= HTTP_REQUEST
  582. ,.raw= "GET / HTTP/1.1\r\n"
  583. "Line1: abc\r\n"
  584. "\tdef\r\n"
  585. " ghi\r\n"
  586. "\t\tjkl\r\n"
  587. " mno \r\n"
  588. "\t \tqrs\r\n"
  589. "Line2: \t line2\t\r\n"
  590. "Line3:\r\n"
  591. " line3\r\n"
  592. "Line4: \r\n"
  593. " \r\n"
  594. "Connection:\r\n"
  595. " close\r\n"
  596. "\r\n"
  597. ,.should_keep_alive= FALSE
  598. ,.message_complete_on_eof= FALSE
  599. ,.http_major= 1
  600. ,.http_minor= 1
  601. ,.method= HTTP_GET
  602. ,.query_string= ""
  603. ,.fragment= ""
  604. ,.request_path= "/"
  605. ,.request_url= "/"
  606. ,.num_headers= 5
  607. ,.headers= { { "Line1", "abc\tdef ghi\t\tjkl mno \t \tqrs" }
  608. , { "Line2", "line2\t" }
  609. , { "Line3", "line3" }
  610. , { "Line4", "" }
  611. , { "Connection", "close" },
  612. }
  613. ,.body= ""
  614. }
  615. #define QUERY_TERMINATED_HOST 22
  616. , {.name= "host terminated by a query string"
  617. ,.type= HTTP_REQUEST
  618. ,.raw= "GET http://hypnotoad.org?hail=all HTTP/1.1\r\n"
  619. "\r\n"
  620. ,.should_keep_alive= TRUE
  621. ,.message_complete_on_eof= FALSE
  622. ,.http_major= 1
  623. ,.http_minor= 1
  624. ,.method= HTTP_GET
  625. ,.query_string= "hail=all"
  626. ,.fragment= ""
  627. ,.request_path= ""
  628. ,.request_url= "http://hypnotoad.org?hail=all"
  629. ,.host= "hypnotoad.org"
  630. ,.num_headers= 0
  631. ,.headers= { }
  632. ,.body= ""
  633. }
  634. #define QUERY_TERMINATED_HOSTPORT 23
  635. , {.name= "host:port terminated by a query string"
  636. ,.type= HTTP_REQUEST
  637. ,.raw= "GET http://hypnotoad.org:1234?hail=all HTTP/1.1\r\n"
  638. "\r\n"
  639. ,.should_keep_alive= TRUE
  640. ,.message_complete_on_eof= FALSE
  641. ,.http_major= 1
  642. ,.http_minor= 1
  643. ,.method= HTTP_GET
  644. ,.query_string= "hail=all"
  645. ,.fragment= ""
  646. ,.request_path= ""
  647. ,.request_url= "http://hypnotoad.org:1234?hail=all"
  648. ,.host= "hypnotoad.org"
  649. ,.port= 1234
  650. ,.num_headers= 0
  651. ,.headers= { }
  652. ,.body= ""
  653. }
  654. #define SPACE_TERMINATED_HOSTPORT 24
  655. , {.name= "host:port terminated by a space"
  656. ,.type= HTTP_REQUEST
  657. ,.raw= "GET http://hypnotoad.org:1234 HTTP/1.1\r\n"
  658. "\r\n"
  659. ,.should_keep_alive= TRUE
  660. ,.message_complete_on_eof= FALSE
  661. ,.http_major= 1
  662. ,.http_minor= 1
  663. ,.method= HTTP_GET
  664. ,.query_string= ""
  665. ,.fragment= ""
  666. ,.request_path= ""
  667. ,.request_url= "http://hypnotoad.org:1234"
  668. ,.host= "hypnotoad.org"
  669. ,.port= 1234
  670. ,.num_headers= 0
  671. ,.headers= { }
  672. ,.body= ""
  673. }
  674. #define PATCH_REQ 25
  675. , {.name = "PATCH request"
  676. ,.type= HTTP_REQUEST
  677. ,.raw= "PATCH /file.txt HTTP/1.1\r\n"
  678. "Host: www.example.com\r\n"
  679. "Content-Type: application/example\r\n"
  680. "If-Match: \"e0023aa4e\"\r\n"
  681. "Content-Length: 10\r\n"
  682. "\r\n"
  683. "cccccccccc"
  684. ,.should_keep_alive= TRUE
  685. ,.message_complete_on_eof= FALSE
  686. ,.http_major= 1
  687. ,.http_minor= 1
  688. ,.method= HTTP_PATCH
  689. ,.query_string= ""
  690. ,.fragment= ""
  691. ,.request_path= "/file.txt"
  692. ,.request_url= "/file.txt"
  693. ,.num_headers= 4
  694. ,.headers= { { "Host", "www.example.com" }
  695. , { "Content-Type", "application/example" }
  696. , { "If-Match", "\"e0023aa4e\"" }
  697. , { "Content-Length", "10" }
  698. }
  699. ,.body= "cccccccccc"
  700. }
  701. #define CONNECT_CAPS_REQUEST 26
  702. , {.name = "connect caps request"
  703. ,.type= HTTP_REQUEST
  704. ,.raw= "CONNECT HOME0.NETSCAPE.COM:443 HTTP/1.0\r\n"
  705. "User-agent: Mozilla/1.1N\r\n"
  706. "Proxy-authorization: basic aGVsbG86d29ybGQ=\r\n"
  707. "\r\n"
  708. ,.should_keep_alive= FALSE
  709. ,.message_complete_on_eof= FALSE
  710. ,.http_major= 1
  711. ,.http_minor= 0
  712. ,.method= HTTP_CONNECT
  713. ,.query_string= ""
  714. ,.fragment= ""
  715. ,.request_path= ""
  716. ,.request_url= "HOME0.NETSCAPE.COM:443"
  717. ,.num_headers= 2
  718. ,.upgrade=""
  719. ,.headers= { { "User-agent", "Mozilla/1.1N" }
  720. , { "Proxy-authorization", "basic aGVsbG86d29ybGQ=" }
  721. }
  722. ,.body= ""
  723. }
  724. #if !HTTP_PARSER_STRICT
  725. #define UTF8_PATH_REQ 27
  726. , {.name= "utf-8 path request"
  727. ,.type= HTTP_REQUEST
  728. ,.raw= "GET /δ¶/δt/pope?q=1#narf HTTP/1.1\r\n"
  729. "Host: github.com\r\n"
  730. "\r\n"
  731. ,.should_keep_alive= TRUE
  732. ,.message_complete_on_eof= FALSE
  733. ,.http_major= 1
  734. ,.http_minor= 1
  735. ,.method= HTTP_GET
  736. ,.query_string= "q=1"
  737. ,.fragment= "narf"
  738. ,.request_path= "/δ¶/δt/pope"
  739. ,.request_url= "/δ¶/δt/pope?q=1#narf"
  740. ,.num_headers= 1
  741. ,.headers= { {"Host", "github.com" }
  742. }
  743. ,.body= ""
  744. }
  745. #define HOSTNAME_UNDERSCORE 28
  746. , {.name = "hostname underscore"
  747. ,.type= HTTP_REQUEST
  748. ,.raw= "CONNECT home_0.netscape.com:443 HTTP/1.0\r\n"
  749. "User-agent: Mozilla/1.1N\r\n"
  750. "Proxy-authorization: basic aGVsbG86d29ybGQ=\r\n"
  751. "\r\n"
  752. ,.should_keep_alive= FALSE
  753. ,.message_complete_on_eof= FALSE
  754. ,.http_major= 1
  755. ,.http_minor= 0
  756. ,.method= HTTP_CONNECT
  757. ,.query_string= ""
  758. ,.fragment= ""
  759. ,.request_path= ""
  760. ,.request_url= "home_0.netscape.com:443"
  761. ,.num_headers= 2
  762. ,.upgrade=""
  763. ,.headers= { { "User-agent", "Mozilla/1.1N" }
  764. , { "Proxy-authorization", "basic aGVsbG86d29ybGQ=" }
  765. }
  766. ,.body= ""
  767. }
  768. #endif /* !HTTP_PARSER_STRICT */
  769. /* see https://github.com/ry/http-parser/issues/47 */
  770. #define EAT_TRAILING_CRLF_NO_CONNECTION_CLOSE 29
  771. , {.name = "eat CRLF between requests, no \"Connection: close\" header"
  772. ,.raw= "POST / HTTP/1.1\r\n"
  773. "Host: www.example.com\r\n"
  774. "Content-Type: application/x-www-form-urlencoded\r\n"
  775. "Content-Length: 4\r\n"
  776. "\r\n"
  777. "q=42\r\n" /* note the trailing CRLF */
  778. ,.should_keep_alive= TRUE
  779. ,.message_complete_on_eof= FALSE
  780. ,.http_major= 1
  781. ,.http_minor= 1
  782. ,.method= HTTP_POST
  783. ,.query_string= ""
  784. ,.fragment= ""
  785. ,.request_path= "/"
  786. ,.request_url= "/"
  787. ,.num_headers= 3
  788. ,.upgrade= 0
  789. ,.headers= { { "Host", "www.example.com" }
  790. , { "Content-Type", "application/x-www-form-urlencoded" }
  791. , { "Content-Length", "4" }
  792. }
  793. ,.body= "q=42"
  794. }
  795. /* see https://github.com/ry/http-parser/issues/47 */
  796. #define EAT_TRAILING_CRLF_WITH_CONNECTION_CLOSE 30
  797. , {.name = "eat CRLF between requests even if \"Connection: close\" is set"
  798. ,.raw= "POST / HTTP/1.1\r\n"
  799. "Host: www.example.com\r\n"
  800. "Content-Type: application/x-www-form-urlencoded\r\n"
  801. "Content-Length: 4\r\n"
  802. "Connection: close\r\n"
  803. "\r\n"
  804. "q=42\r\n" /* note the trailing CRLF */
  805. ,.should_keep_alive= FALSE
  806. ,.message_complete_on_eof= FALSE /* input buffer isn't empty when on_message_complete is called */
  807. ,.http_major= 1
  808. ,.http_minor= 1
  809. ,.method= HTTP_POST
  810. ,.query_string= ""
  811. ,.fragment= ""
  812. ,.request_path= "/"
  813. ,.request_url= "/"
  814. ,.num_headers= 4
  815. ,.upgrade= 0
  816. ,.headers= { { "Host", "www.example.com" }
  817. , { "Content-Type", "application/x-www-form-urlencoded" }
  818. , { "Content-Length", "4" }
  819. , { "Connection", "close" }
  820. }
  821. ,.body= "q=42"
  822. }
  823. #define PURGE_REQ 31
  824. , {.name = "PURGE request"
  825. ,.type= HTTP_REQUEST
  826. ,.raw= "PURGE /file.txt HTTP/1.1\r\n"
  827. "Host: www.example.com\r\n"
  828. "\r\n"
  829. ,.should_keep_alive= TRUE
  830. ,.message_complete_on_eof= FALSE
  831. ,.http_major= 1
  832. ,.http_minor= 1
  833. ,.method= HTTP_PURGE
  834. ,.query_string= ""
  835. ,.fragment= ""
  836. ,.request_path= "/file.txt"
  837. ,.request_url= "/file.txt"
  838. ,.num_headers= 1
  839. ,.headers= { { "Host", "www.example.com" } }
  840. ,.body= ""
  841. }
  842. #define SEARCH_REQ 32
  843. , {.name = "SEARCH request"
  844. ,.type= HTTP_REQUEST
  845. ,.raw= "SEARCH / HTTP/1.1\r\n"
  846. "Host: www.example.com\r\n"
  847. "\r\n"
  848. ,.should_keep_alive= TRUE
  849. ,.message_complete_on_eof= FALSE
  850. ,.http_major= 1
  851. ,.http_minor= 1
  852. ,.method= HTTP_SEARCH
  853. ,.query_string= ""
  854. ,.fragment= ""
  855. ,.request_path= "/"
  856. ,.request_url= "/"
  857. ,.num_headers= 1
  858. ,.headers= { { "Host", "www.example.com" } }
  859. ,.body= ""
  860. }
  861. #define PROXY_WITH_BASIC_AUTH 33
  862. , {.name= "host:port and basic_auth"
  863. ,.type= HTTP_REQUEST
  864. ,.raw= "GET http://a%12:b!&*$@hypnotoad.org:1234/toto HTTP/1.1\r\n"
  865. "\r\n"
  866. ,.should_keep_alive= TRUE
  867. ,.message_complete_on_eof= FALSE
  868. ,.http_major= 1
  869. ,.http_minor= 1
  870. ,.method= HTTP_GET
  871. ,.fragment= ""
  872. ,.request_path= "/toto"
  873. ,.request_url= "http://a%12:b!&*$@hypnotoad.org:1234/toto"
  874. ,.host= "hypnotoad.org"
  875. ,.userinfo= "a%12:b!&*$"
  876. ,.port= 1234
  877. ,.num_headers= 0
  878. ,.headers= { }
  879. ,.body= ""
  880. }
  881. #define LINE_FOLDING_IN_HEADER_WITH_LF 34
  882. , {.name= "line folding in header value"
  883. ,.type= HTTP_REQUEST
  884. ,.raw= "GET / HTTP/1.1\n"
  885. "Line1: abc\n"
  886. "\tdef\n"
  887. " ghi\n"
  888. "\t\tjkl\n"
  889. " mno \n"
  890. "\t \tqrs\n"
  891. "Line2: \t line2\t\n"
  892. "Line3:\n"
  893. " line3\n"
  894. "Line4: \n"
  895. " \n"
  896. "Connection:\n"
  897. " close\n"
  898. "\n"
  899. ,.should_keep_alive= FALSE
  900. ,.message_complete_on_eof= FALSE
  901. ,.http_major= 1
  902. ,.http_minor= 1
  903. ,.method= HTTP_GET
  904. ,.query_string= ""
  905. ,.fragment= ""
  906. ,.request_path= "/"
  907. ,.request_url= "/"
  908. ,.num_headers= 5
  909. ,.headers= { { "Line1", "abc\tdef ghi\t\tjkl mno \t \tqrs" }
  910. , { "Line2", "line2\t" }
  911. , { "Line3", "line3" }
  912. , { "Line4", "" }
  913. , { "Connection", "close" },
  914. }
  915. ,.body= ""
  916. }
  917. #define CONNECTION_MULTI 35
  918. , {.name = "multiple connection header values with folding"
  919. ,.type= HTTP_REQUEST
  920. ,.raw= "GET /demo HTTP/1.1\r\n"
  921. "Host: example.com\r\n"
  922. "Connection: Something,\r\n"
  923. " Upgrade, ,Keep-Alive\r\n"
  924. "Sec-WebSocket-Key2: 12998 5 Y3 1 .P00\r\n"
  925. "Sec-WebSocket-Protocol: sample\r\n"
  926. "Upgrade: WebSocket\r\n"
  927. "Sec-WebSocket-Key1: 4 @1 46546xW%0l 1 5\r\n"
  928. "Origin: http://example.com\r\n"
  929. "\r\n"
  930. "Hot diggity dogg"
  931. ,.should_keep_alive= TRUE
  932. ,.message_complete_on_eof= FALSE
  933. ,.http_major= 1
  934. ,.http_minor= 1
  935. ,.method= HTTP_GET
  936. ,.query_string= ""
  937. ,.fragment= ""
  938. ,.request_path= "/demo"
  939. ,.request_url= "/demo"
  940. ,.num_headers= 7
  941. ,.upgrade="Hot diggity dogg"
  942. ,.headers= { { "Host", "example.com" }
  943. , { "Connection", "Something, Upgrade, ,Keep-Alive" }
  944. , { "Sec-WebSocket-Key2", "12998 5 Y3 1 .P00" }
  945. , { "Sec-WebSocket-Protocol", "sample" }
  946. , { "Upgrade", "WebSocket" }
  947. , { "Sec-WebSocket-Key1", "4 @1 46546xW%0l 1 5" }
  948. , { "Origin", "http://example.com" }
  949. }
  950. ,.body= ""
  951. }
  952. #define CONNECTION_MULTI_LWS 36
  953. , {.name = "multiple connection header values with folding and lws"
  954. ,.type= HTTP_REQUEST
  955. ,.raw= "GET /demo HTTP/1.1\r\n"
  956. "Connection: keep-alive, upgrade\r\n"
  957. "Upgrade: WebSocket\r\n"
  958. "\r\n"
  959. "Hot diggity dogg"
  960. ,.should_keep_alive= TRUE
  961. ,.message_complete_on_eof= FALSE
  962. ,.http_major= 1
  963. ,.http_minor= 1
  964. ,.method= HTTP_GET
  965. ,.query_string= ""
  966. ,.fragment= ""
  967. ,.request_path= "/demo"
  968. ,.request_url= "/demo"
  969. ,.num_headers= 2
  970. ,.upgrade="Hot diggity dogg"
  971. ,.headers= { { "Connection", "keep-alive, upgrade" }
  972. , { "Upgrade", "WebSocket" }
  973. }
  974. ,.body= ""
  975. }
  976. #define CONNECTION_MULTI_LWS_CRLF 37
  977. , {.name = "multiple connection header values with folding and lws"
  978. ,.type= HTTP_REQUEST
  979. ,.raw= "GET /demo HTTP/1.1\r\n"
  980. "Connection: keep-alive, \r\n upgrade\r\n"
  981. "Upgrade: WebSocket\r\n"
  982. "\r\n"
  983. "Hot diggity dogg"
  984. ,.should_keep_alive= TRUE
  985. ,.message_complete_on_eof= FALSE
  986. ,.http_major= 1
  987. ,.http_minor= 1
  988. ,.method= HTTP_GET
  989. ,.query_string= ""
  990. ,.fragment= ""
  991. ,.request_path= "/demo"
  992. ,.request_url= "/demo"
  993. ,.num_headers= 2
  994. ,.upgrade="Hot diggity dogg"
  995. ,.headers= { { "Connection", "keep-alive, upgrade" }
  996. , { "Upgrade", "WebSocket" }
  997. }
  998. ,.body= ""
  999. }
  1000. #define UPGRADE_POST_REQUEST 38
  1001. , {.name = "upgrade post request"
  1002. ,.type= HTTP_REQUEST
  1003. ,.raw= "POST /demo HTTP/1.1\r\n"
  1004. "Host: example.com\r\n"
  1005. "Connection: Upgrade\r\n"
  1006. "Upgrade: HTTP/2.0\r\n"
  1007. "Content-Length: 15\r\n"
  1008. "\r\n"
  1009. "sweet post body"
  1010. "Hot diggity dogg"
  1011. ,.should_keep_alive= TRUE
  1012. ,.message_complete_on_eof= FALSE
  1013. ,.http_major= 1
  1014. ,.http_minor= 1
  1015. ,.method= HTTP_POST
  1016. ,.request_path= "/demo"
  1017. ,.request_url= "/demo"
  1018. ,.num_headers= 4
  1019. ,.upgrade="Hot diggity dogg"
  1020. ,.headers= { { "Host", "example.com" }
  1021. , { "Connection", "Upgrade" }
  1022. , { "Upgrade", "HTTP/2.0" }
  1023. , { "Content-Length", "15" }
  1024. }
  1025. ,.body= "sweet post body"
  1026. }
  1027. #define CONNECT_WITH_BODY_REQUEST 39
  1028. , {.name = "connect with body request"
  1029. ,.type= HTTP_REQUEST
  1030. ,.raw= "CONNECT foo.bar.com:443 HTTP/1.0\r\n"
  1031. "User-agent: Mozilla/1.1N\r\n"
  1032. "Proxy-authorization: basic aGVsbG86d29ybGQ=\r\n"
  1033. "Content-Length: 10\r\n"
  1034. "\r\n"
  1035. "blarfcicle"
  1036. ,.should_keep_alive= FALSE
  1037. ,.message_complete_on_eof= FALSE
  1038. ,.http_major= 1
  1039. ,.http_minor= 0
  1040. ,.method= HTTP_CONNECT
  1041. ,.request_url= "foo.bar.com:443"
  1042. ,.num_headers= 3
  1043. ,.upgrade="blarfcicle"
  1044. ,.headers= { { "User-agent", "Mozilla/1.1N" }
  1045. , { "Proxy-authorization", "basic aGVsbG86d29ybGQ=" }
  1046. , { "Content-Length", "10" }
  1047. }
  1048. ,.body= ""
  1049. }
  1050. /* Examples from the Internet draft for LINK/UNLINK methods:
  1051. * https://tools.ietf.org/id/draft-snell-link-method-01.html#rfc.section.5
  1052. */
  1053. #define LINK_REQUEST 40
  1054. , {.name = "link request"
  1055. ,.type= HTTP_REQUEST
  1056. ,.raw= "LINK /images/my_dog.jpg HTTP/1.1\r\n"
  1057. "Host: example.com\r\n"
  1058. "Link: <http://example.com/profiles/joe>; rel=\"tag\"\r\n"
  1059. "Link: <http://example.com/profiles/sally>; rel=\"tag\"\r\n"
  1060. "\r\n"
  1061. ,.should_keep_alive= TRUE
  1062. ,.message_complete_on_eof= FALSE
  1063. ,.http_major= 1
  1064. ,.http_minor= 1
  1065. ,.method= HTTP_LINK
  1066. ,.request_path= "/images/my_dog.jpg"
  1067. ,.request_url= "/images/my_dog.jpg"
  1068. ,.query_string= ""
  1069. ,.fragment= ""
  1070. ,.num_headers= 3
  1071. ,.headers= { { "Host", "example.com" }
  1072. , { "Link", "<http://example.com/profiles/joe>; rel=\"tag\"" }
  1073. , { "Link", "<http://example.com/profiles/sally>; rel=\"tag\"" }
  1074. }
  1075. ,.body= ""
  1076. }
  1077. #define UNLINK_REQUEST 41
  1078. , {.name = "link request"
  1079. ,.type= HTTP_REQUEST
  1080. ,.raw= "UNLINK /images/my_dog.jpg HTTP/1.1\r\n"
  1081. "Host: example.com\r\n"
  1082. "Link: <http://example.com/profiles/sally>; rel=\"tag\"\r\n"
  1083. "\r\n"
  1084. ,.should_keep_alive= TRUE
  1085. ,.message_complete_on_eof= FALSE
  1086. ,.http_major= 1
  1087. ,.http_minor= 1
  1088. ,.method= HTTP_UNLINK
  1089. ,.request_path= "/images/my_dog.jpg"
  1090. ,.request_url= "/images/my_dog.jpg"
  1091. ,.query_string= ""
  1092. ,.fragment= ""
  1093. ,.num_headers= 2
  1094. ,.headers= { { "Host", "example.com" }
  1095. , { "Link", "<http://example.com/profiles/sally>; rel=\"tag\"" }
  1096. }
  1097. ,.body= ""
  1098. }
  1099. , {.name= NULL } /* sentinel */
  1100. };
  1101. /* * R E S P O N S E S * */
  1102. const struct message responses[] =
  1103. #define GOOGLE_301 0
  1104. { {.name= "google 301"
  1105. ,.type= HTTP_RESPONSE
  1106. ,.raw= "HTTP/1.1 301 Moved Permanently\r\n"
  1107. "Location: http://www.google.com/\r\n"
  1108. "Content-Type: text/html; charset=UTF-8\r\n"
  1109. "Date: Sun, 26 Apr 2009 11:11:49 GMT\r\n"
  1110. "Expires: Tue, 26 May 2009 11:11:49 GMT\r\n"
  1111. "X-$PrototypeBI-Version: 1.6.0.3\r\n" /* $ char in header field */
  1112. "Cache-Control: public, max-age=2592000\r\n"
  1113. "Server: gws\r\n"
  1114. "Content-Length: 219 \r\n"
  1115. "\r\n"
  1116. "<HTML><HEAD><meta http-equiv=\"content-type\" content=\"text/html;charset=utf-8\">\n"
  1117. "<TITLE>301 Moved</TITLE></HEAD><BODY>\n"
  1118. "<H1>301 Moved</H1>\n"
  1119. "The document has moved\n"
  1120. "<A HREF=\"http://www.google.com/\">here</A>.\r\n"
  1121. "</BODY></HTML>\r\n"
  1122. ,.should_keep_alive= TRUE
  1123. ,.message_complete_on_eof= FALSE
  1124. ,.http_major= 1
  1125. ,.http_minor= 1
  1126. ,.status_code= 301
  1127. ,.response_status= "Moved Permanently"
  1128. ,.num_headers= 8
  1129. ,.headers=
  1130. { { "Location", "http://www.google.com/" }
  1131. , { "Content-Type", "text/html; charset=UTF-8" }
  1132. , { "Date", "Sun, 26 Apr 2009 11:11:49 GMT" }
  1133. , { "Expires", "Tue, 26 May 2009 11:11:49 GMT" }
  1134. , { "X-$PrototypeBI-Version", "1.6.0.3" }
  1135. , { "Cache-Control", "public, max-age=2592000" }
  1136. , { "Server", "gws" }
  1137. , { "Content-Length", "219 " }
  1138. }
  1139. ,.body= "<HTML><HEAD><meta http-equiv=\"content-type\" content=\"text/html;charset=utf-8\">\n"
  1140. "<TITLE>301 Moved</TITLE></HEAD><BODY>\n"
  1141. "<H1>301 Moved</H1>\n"
  1142. "The document has moved\n"
  1143. "<A HREF=\"http://www.google.com/\">here</A>.\r\n"
  1144. "</BODY></HTML>\r\n"
  1145. }
  1146. #define NO_CONTENT_LENGTH_RESPONSE 1
  1147. /* The client should wait for the server's EOF. That is, when content-length
  1148. * is not specified, and "Connection: close", the end of body is specified
  1149. * by the EOF.
  1150. * Compare with APACHEBENCH_GET
  1151. */
  1152. , {.name= "no content-length response"
  1153. ,.type= HTTP_RESPONSE
  1154. ,.raw= "HTTP/1.1 200 OK\r\n"
  1155. "Date: Tue, 04 Aug 2009 07:59:32 GMT\r\n"
  1156. "Server: Apache\r\n"
  1157. "X-Powered-By: Servlet/2.5 JSP/2.1\r\n"
  1158. "Content-Type: text/xml; charset=utf-8\r\n"
  1159. "Connection: close\r\n"
  1160. "\r\n"
  1161. "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
  1162. "<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\">\n"
  1163. " <SOAP-ENV:Body>\n"
  1164. " <SOAP-ENV:Fault>\n"
  1165. " <faultcode>SOAP-ENV:Client</faultcode>\n"
  1166. " <faultstring>Client Error</faultstring>\n"
  1167. " </SOAP-ENV:Fault>\n"
  1168. " </SOAP-ENV:Body>\n"
  1169. "</SOAP-ENV:Envelope>"
  1170. ,.should_keep_alive= FALSE
  1171. ,.message_complete_on_eof= TRUE
  1172. ,.http_major= 1
  1173. ,.http_minor= 1
  1174. ,.status_code= 200
  1175. ,.response_status= "OK"
  1176. ,.num_headers= 5
  1177. ,.headers=
  1178. { { "Date", "Tue, 04 Aug 2009 07:59:32 GMT" }
  1179. , { "Server", "Apache" }
  1180. , { "X-Powered-By", "Servlet/2.5 JSP/2.1" }
  1181. , { "Content-Type", "text/xml; charset=utf-8" }
  1182. , { "Connection", "close" }
  1183. }
  1184. ,.body= "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
  1185. "<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\">\n"
  1186. " <SOAP-ENV:Body>\n"
  1187. " <SOAP-ENV:Fault>\n"
  1188. " <faultcode>SOAP-ENV:Client</faultcode>\n"
  1189. " <faultstring>Client Error</faultstring>\n"
  1190. " </SOAP-ENV:Fault>\n"
  1191. " </SOAP-ENV:Body>\n"
  1192. "</SOAP-ENV:Envelope>"
  1193. }
  1194. #define NO_HEADERS_NO_BODY_404 2
  1195. , {.name= "404 no headers no body"
  1196. ,.type= HTTP_RESPONSE
  1197. ,.raw= "HTTP/1.1 404 Not Found\r\n\r\n"
  1198. ,.should_keep_alive= FALSE
  1199. ,.message_complete_on_eof= TRUE
  1200. ,.http_major= 1
  1201. ,.http_minor= 1
  1202. ,.status_code= 404
  1203. ,.response_status= "Not Found"
  1204. ,.num_headers= 0
  1205. ,.headers= {}
  1206. ,.body_size= 0
  1207. ,.body= ""
  1208. }
  1209. #define NO_REASON_PHRASE 3
  1210. , {.name= "301 no response phrase"
  1211. ,.type= HTTP_RESPONSE
  1212. ,.raw= "HTTP/1.1 301\r\n\r\n"
  1213. ,.should_keep_alive = FALSE
  1214. ,.message_complete_on_eof= TRUE
  1215. ,.http_major= 1
  1216. ,.http_minor= 1
  1217. ,.status_code= 301
  1218. ,.response_status= ""
  1219. ,.num_headers= 0
  1220. ,.headers= {}
  1221. ,.body= ""
  1222. }
  1223. #define TRAILING_SPACE_ON_CHUNKED_BODY 4
  1224. , {.name="200 trailing space on chunked body"
  1225. ,.type= HTTP_RESPONSE
  1226. ,.raw= "HTTP/1.1 200 OK\r\n"
  1227. "Content-Type: text/plain\r\n"
  1228. "Transfer-Encoding: chunked\r\n"
  1229. "\r\n"
  1230. "25 \r\n"
  1231. "This is the data in the first chunk\r\n"
  1232. "\r\n"
  1233. "1C\r\n"
  1234. "and this is the second one\r\n"
  1235. "\r\n"
  1236. "0 \r\n"
  1237. "\r\n"
  1238. ,.should_keep_alive= TRUE
  1239. ,.message_complete_on_eof= FALSE
  1240. ,.http_major= 1
  1241. ,.http_minor= 1
  1242. ,.status_code= 200
  1243. ,.response_status= "OK"
  1244. ,.num_headers= 2
  1245. ,.headers=
  1246. { {"Content-Type", "text/plain" }
  1247. , {"Transfer-Encoding", "chunked" }
  1248. }
  1249. ,.body_size = 37+28
  1250. ,.body =
  1251. "This is the data in the first chunk\r\n"
  1252. "and this is the second one\r\n"
  1253. ,.num_chunks_complete= 3
  1254. ,.chunk_lengths= { 0x25, 0x1c }
  1255. }
  1256. #define NO_CARRIAGE_RET 5
  1257. , {.name="no carriage ret"
  1258. ,.type= HTTP_RESPONSE
  1259. ,.raw= "HTTP/1.1 200 OK\n"
  1260. "Content-Type: text/html; charset=utf-8\n"
  1261. "Connection: close\n"
  1262. "\n"
  1263. "these headers are from http://news.ycombinator.com/"
  1264. ,.should_keep_alive= FALSE
  1265. ,.message_complete_on_eof= TRUE
  1266. ,.http_major= 1
  1267. ,.http_minor= 1
  1268. ,.status_code= 200
  1269. ,.response_status= "OK"
  1270. ,.num_headers= 2
  1271. ,.headers=
  1272. { {"Content-Type", "text/html; charset=utf-8" }
  1273. , {"Connection", "close" }
  1274. }
  1275. ,.body= "these headers are from http://news.ycombinator.com/"
  1276. }
  1277. #define PROXY_CONNECTION 6
  1278. , {.name="proxy connection"
  1279. ,.type= HTTP_RESPONSE
  1280. ,.raw= "HTTP/1.1 200 OK\r\n"
  1281. "Content-Type: text/html; charset=UTF-8\r\n"
  1282. "Content-Length: 11\r\n"
  1283. "Proxy-Connection: close\r\n"
  1284. "Date: Thu, 31 Dec 2009 20:55:48 +0000\r\n"
  1285. "\r\n"
  1286. "hello world"
  1287. ,.should_keep_alive= FALSE
  1288. ,.message_complete_on_eof= FALSE
  1289. ,.http_major= 1
  1290. ,.http_minor= 1
  1291. ,.status_code= 200
  1292. ,.response_status= "OK"
  1293. ,.num_headers= 4
  1294. ,.headers=
  1295. { {"Content-Type", "text/html; charset=UTF-8" }
  1296. , {"Content-Length", "11" }
  1297. , {"Proxy-Connection", "close" }
  1298. , {"Date", "Thu, 31 Dec 2009 20:55:48 +0000"}
  1299. }
  1300. ,.body= "hello world"
  1301. }
  1302. #define UNDERSTORE_HEADER_KEY 7
  1303. // shown by
  1304. // curl -o /dev/null -v "http://ad.doubleclick.net/pfadx/DARTSHELLCONFIGXML;dcmt=text/xml;"
  1305. , {.name="underscore header key"
  1306. ,.type= HTTP_RESPONSE
  1307. ,.raw= "HTTP/1.1 200 OK\r\n"
  1308. "Server: DCLK-AdSvr\r\n"
  1309. "Content-Type: text/xml\r\n"
  1310. "Content-Length: 0\r\n"
  1311. "DCLK_imp: v7;x;114750856;0-0;0;17820020;0/0;21603567/21621457/1;;~okv=;dcmt=text/xml;;~cs=o\r\n\r\n"
  1312. ,.should_keep_alive= TRUE
  1313. ,.message_complete_on_eof= FALSE
  1314. ,.http_major= 1
  1315. ,.http_minor= 1
  1316. ,.status_code= 200
  1317. ,.response_status= "OK"
  1318. ,.num_headers= 4
  1319. ,.headers=
  1320. { {"Server", "DCLK-AdSvr" }
  1321. , {"Content-Type", "text/xml" }
  1322. , {"Content-Length", "0" }
  1323. , {"DCLK_imp", "v7;x;114750856;0-0;0;17820020;0/0;21603567/21621457/1;;~okv=;dcmt=text/xml;;~cs=o" }
  1324. }
  1325. ,.body= ""
  1326. }
  1327. #define BONJOUR_MADAME_FR 8
  1328. /* The client should not merge two headers fields when the first one doesn't
  1329. * have a value.
  1330. */
  1331. , {.name= "bonjourmadame.fr"
  1332. ,.type= HTTP_RESPONSE
  1333. ,.raw= "HTTP/1.0 301 Moved Permanently\r\n"
  1334. "Date: Thu, 03 Jun 2010 09:56:32 GMT\r\n"
  1335. "Server: Apache/2.2.3 (Red Hat)\r\n"
  1336. "Cache-Control: public\r\n"
  1337. "Pragma: \r\n"
  1338. "Location: http://www.bonjourmadame.fr/\r\n"
  1339. "Vary: Accept-Encoding\r\n"
  1340. "Content-Length: 0\r\n"
  1341. "Content-Type: text/html; charset=UTF-8\r\n"
  1342. "Connection: keep-alive\r\n"
  1343. "\r\n"
  1344. ,.should_keep_alive= TRUE
  1345. ,.message_complete_on_eof= FALSE
  1346. ,.http_major= 1
  1347. ,.http_minor= 0
  1348. ,.status_code= 301
  1349. ,.response_status= "Moved Permanently"
  1350. ,.num_headers= 9
  1351. ,.headers=
  1352. { { "Date", "Thu, 03 Jun 2010 09:56:32 GMT" }
  1353. , { "Server", "Apache/2.2.3 (Red Hat)" }
  1354. , { "Cache-Control", "public" }
  1355. , { "Pragma", "" }
  1356. , { "Location", "http://www.bonjourmadame.fr/" }
  1357. , { "Vary", "Accept-Encoding" }
  1358. , { "Content-Length", "0" }
  1359. , { "Content-Type", "text/html; charset=UTF-8" }
  1360. , { "Connection", "keep-alive" }
  1361. }
  1362. ,.body= ""
  1363. }
  1364. #define RES_FIELD_UNDERSCORE 9
  1365. /* Should handle spaces in header fields */
  1366. , {.name= "field underscore"
  1367. ,.type= HTTP_RESPONSE
  1368. ,.raw= "HTTP/1.1 200 OK\r\n"
  1369. "Date: Tue, 28 Sep 2010 01:14:13 GMT\r\n"
  1370. "Server: Apache\r\n"
  1371. "Cache-Control: no-cache, must-revalidate\r\n"
  1372. "Expires: Mon, 26 Jul 1997 05:00:00 GMT\r\n"
  1373. ".et-Cookie: PlaxoCS=1274804622353690521; path=/; domain=.plaxo.com\r\n"
  1374. "Vary: Accept-Encoding\r\n"
  1375. "_eep-Alive: timeout=45\r\n" /* semantic value ignored */
  1376. "_onnection: Keep-Alive\r\n" /* semantic value ignored */
  1377. "Transfer-Encoding: chunked\r\n"
  1378. "Content-Type: text/html\r\n"
  1379. "Connection: close\r\n"
  1380. "\r\n"
  1381. "0\r\n\r\n"
  1382. ,.should_keep_alive= FALSE
  1383. ,.message_complete_on_eof= FALSE
  1384. ,.http_major= 1
  1385. ,.http_minor= 1
  1386. ,.status_code= 200
  1387. ,.response_status= "OK"
  1388. ,.num_headers= 11
  1389. ,.headers=
  1390. { { "Date", "Tue, 28 Sep 2010 01:14:13 GMT" }
  1391. , { "Server", "Apache" }
  1392. , { "Cache-Control", "no-cache, must-revalidate" }
  1393. , { "Expires", "Mon, 26 Jul 1997 05:00:00 GMT" }
  1394. , { ".et-Cookie", "PlaxoCS=1274804622353690521; path=/; domain=.plaxo.com" }
  1395. , { "Vary", "Accept-Encoding" }
  1396. , { "_eep-Alive", "timeout=45" }
  1397. , { "_onnection", "Keep-Alive" }
  1398. , { "Transfer-Encoding", "chunked" }
  1399. , { "Content-Type", "text/html" }
  1400. , { "Connection", "close" }
  1401. }
  1402. ,.body= ""
  1403. ,.num_chunks_complete= 1
  1404. ,.chunk_lengths= {}
  1405. }
  1406. #define NON_ASCII_IN_STATUS_LINE 10
  1407. /* Should handle non-ASCII in status line */
  1408. , {.name= "non-ASCII in status line"
  1409. ,.type= HTTP_RESPONSE
  1410. ,.raw= "HTTP/1.1 500 Oriëntatieprobleem\r\n"
  1411. "Date: Fri, 5 Nov 2010 23:07:12 GMT+2\r\n"
  1412. "Content-Length: 0\r\n"
  1413. "Connection: close\r\n"
  1414. "\r\n"
  1415. ,.should_keep_alive= FALSE
  1416. ,.message_complete_on_eof= FALSE
  1417. ,.http_major= 1
  1418. ,.http_minor= 1
  1419. ,.status_code= 500
  1420. ,.response_status= "Oriëntatieprobleem"
  1421. ,.num_headers= 3
  1422. ,.headers=
  1423. { { "Date", "Fri, 5 Nov 2010 23:07:12 GMT+2" }
  1424. , { "Content-Length", "0" }
  1425. , { "Connection", "close" }
  1426. }
  1427. ,.body= ""
  1428. }
  1429. #define HTTP_VERSION_0_9 11
  1430. /* Should handle HTTP/0.9 */
  1431. , {.name= "http version 0.9"
  1432. ,.type= HTTP_RESPONSE
  1433. ,.raw= "HTTP/0.9 200 OK\r\n"
  1434. "\r\n"
  1435. ,.should_keep_alive= FALSE
  1436. ,.message_complete_on_eof= TRUE
  1437. ,.http_major= 0
  1438. ,.http_minor= 9
  1439. ,.status_code= 200
  1440. ,.response_status= "OK"
  1441. ,.num_headers= 0
  1442. ,.headers=
  1443. {}
  1444. ,.body= ""
  1445. }
  1446. #define NO_CONTENT_LENGTH_NO_TRANSFER_ENCODING_RESPONSE 12
  1447. /* The client should wait for the server's EOF. That is, when neither
  1448. * content-length nor transfer-encoding is specified, the end of body
  1449. * is specified by the EOF.
  1450. */
  1451. , {.name= "neither content-length nor transfer-encoding response"
  1452. ,.type= HTTP_RESPONSE
  1453. ,.raw= "HTTP/1.1 200 OK\r\n"
  1454. "Content-Type: text/plain\r\n"
  1455. "\r\n"
  1456. "hello world"
  1457. ,.should_keep_alive= FALSE
  1458. ,.message_complete_on_eof= TRUE
  1459. ,.http_major= 1
  1460. ,.http_minor= 1
  1461. ,.status_code= 200
  1462. ,.response_status= "OK"
  1463. ,.num_headers= 1
  1464. ,.headers=
  1465. { { "Content-Type", "text/plain" }
  1466. }
  1467. ,.body= "hello world"
  1468. }
  1469. #define NO_BODY_HTTP10_KA_200 13
  1470. , {.name= "HTTP/1.0 with keep-alive and EOF-terminated 200 status"
  1471. ,.type= HTTP_RESPONSE
  1472. ,.raw= "HTTP/1.0 200 OK\r\n"
  1473. "Connection: keep-alive\r\n"
  1474. "\r\n"
  1475. ,.should_keep_alive= FALSE
  1476. ,.message_complete_on_eof= TRUE
  1477. ,.http_major= 1
  1478. ,.http_minor= 0
  1479. ,.status_code= 200
  1480. ,.response_status= "OK"
  1481. ,.num_headers= 1
  1482. ,.headers=
  1483. { { "Connection", "keep-alive" }
  1484. }
  1485. ,.body_size= 0
  1486. ,.body= ""
  1487. }
  1488. #define NO_BODY_HTTP10_KA_204 14
  1489. , {.name= "HTTP/1.0 with keep-alive and a 204 status"
  1490. ,.type= HTTP_RESPONSE
  1491. ,.raw= "HTTP/1.0 204 No content\r\n"
  1492. "Connection: keep-alive\r\n"
  1493. "\r\n"
  1494. ,.should_keep_alive= TRUE
  1495. ,.message_complete_on_eof= FALSE
  1496. ,.http_major= 1
  1497. ,.http_minor= 0
  1498. ,.status_code= 204
  1499. ,.response_status= "No content"
  1500. ,.num_headers= 1
  1501. ,.headers=
  1502. { { "Connection", "keep-alive" }
  1503. }
  1504. ,.body_size= 0
  1505. ,.body= ""
  1506. }
  1507. #define NO_BODY_HTTP11_KA_200 15
  1508. , {.name= "HTTP/1.1 with an EOF-terminated 200 status"
  1509. ,.type= HTTP_RESPONSE
  1510. ,.raw= "HTTP/1.1 200 OK\r\n"
  1511. "\r\n"
  1512. ,.should_keep_alive= FALSE
  1513. ,.message_complete_on_eof= TRUE
  1514. ,.http_major= 1
  1515. ,.http_minor= 1
  1516. ,.status_code= 200
  1517. ,.response_status= "OK"
  1518. ,.num_headers= 0
  1519. ,.headers={}
  1520. ,.body_size= 0
  1521. ,.body= ""
  1522. }
  1523. #define NO_BODY_HTTP11_KA_204 16
  1524. , {.name= "HTTP/1.1 with a 204 status"
  1525. ,.type= HTTP_RESPONSE
  1526. ,.raw= "HTTP/1.1 204 No content\r\n"
  1527. "\r\n"
  1528. ,.should_keep_alive= TRUE
  1529. ,.message_complete_on_eof= FALSE
  1530. ,.http_major= 1
  1531. ,.http_minor= 1
  1532. ,.status_code= 204
  1533. ,.response_status= "No content"
  1534. ,.num_headers= 0
  1535. ,.headers={}
  1536. ,.body_size= 0
  1537. ,.body= ""
  1538. }
  1539. #define NO_BODY_HTTP11_NOKA_204 17
  1540. , {.name= "HTTP/1.1 with a 204 status and keep-alive disabled"
  1541. ,.type= HTTP_RESPONSE
  1542. ,.raw= "HTTP/1.1 204 No content\r\n"
  1543. "Connection: close\r\n"
  1544. "\r\n"
  1545. ,.should_keep_alive= FALSE
  1546. ,.message_complete_on_eof= FALSE
  1547. ,.http_major= 1
  1548. ,.http_minor= 1
  1549. ,.status_code= 204
  1550. ,.response_status= "No content"
  1551. ,.num_headers= 1
  1552. ,.headers=
  1553. { { "Connection", "close" }
  1554. }
  1555. ,.body_size= 0
  1556. ,.body= ""
  1557. }
  1558. #define NO_BODY_HTTP11_KA_CHUNKED_200 18
  1559. , {.name= "HTTP/1.1 with chunked endocing and a 200 response"
  1560. ,.type= HTTP_RESPONSE
  1561. ,.raw= "HTTP/1.1 200 OK\r\n"
  1562. "Transfer-Encoding: chunked\r\n"
  1563. "\r\n"
  1564. "0\r\n"
  1565. "\r\n"
  1566. ,.should_keep_alive= TRUE
  1567. ,.message_complete_on_eof= FALSE
  1568. ,.http_major= 1
  1569. ,.http_minor= 1
  1570. ,.status_code= 200
  1571. ,.response_status= "OK"
  1572. ,.num_headers= 1
  1573. ,.headers=
  1574. { { "Transfer-Encoding", "chunked" }
  1575. }
  1576. ,.body_size= 0
  1577. ,.body= ""
  1578. ,.num_chunks_complete= 1
  1579. }
  1580. #if !HTTP_PARSER_STRICT
  1581. #define SPACE_IN_FIELD_RES 19
  1582. /* Should handle spaces in header fields */
  1583. , {.name= "field space"
  1584. ,.type= HTTP_RESPONSE
  1585. ,.raw= "HTTP/1.1 200 OK\r\n"
  1586. "Server: Microsoft-IIS/6.0\r\n"
  1587. "X-Powered-By: ASP.NET\r\n"
  1588. "en-US Content-Type: text/xml\r\n" /* this is the problem */
  1589. "Content-Type: text/xml\r\n"
  1590. "Content-Length: 16\r\n"
  1591. "Date: Fri, 23 Jul 2010 18:45:38 GMT\r\n"
  1592. "Connection: keep-alive\r\n"
  1593. "\r\n"
  1594. "<xml>hello</xml>" /* fake body */
  1595. ,.should_keep_alive= TRUE
  1596. ,.message_complete_on_eof= FALSE
  1597. ,.http_major= 1
  1598. ,.http_minor= 1
  1599. ,.status_code= 200
  1600. ,.response_status= "OK"
  1601. ,.num_headers= 7
  1602. ,.headers=
  1603. { { "Server", "Microsoft-IIS/6.0" }
  1604. , { "X-Powered-By", "ASP.NET" }
  1605. , { "en-US Content-Type", "text/xml" }
  1606. , { "Content-Type", "text/xml" }
  1607. , { "Content-Length", "16" }
  1608. , { "Date", "Fri, 23 Jul 2010 18:45:38 GMT" }
  1609. , { "Connection", "keep-alive" }
  1610. }
  1611. ,.body= "<xml>hello</xml>"
  1612. }
  1613. #endif /* !HTTP_PARSER_STRICT */
  1614. #define AMAZON_COM 20
  1615. , {.name= "amazon.com"
  1616. ,.type= HTTP_RESPONSE
  1617. ,.raw= "HTTP/1.1 301 MovedPermanently\r\n"
  1618. "Date: Wed, 15 May 2013 17:06:33 GMT\r\n"
  1619. "Server: Server\r\n"
  1620. "x-amz-id-1: 0GPHKXSJQ826RK7GZEB2\r\n"
  1621. "p3p: policyref=\"http://www.amazon.com/w3c/p3p.xml\",CP=\"CAO DSP LAW CUR ADM IVAo IVDo CONo OTPo OUR DELi PUBi OTRi BUS PHY ONL UNI PUR FIN COM NAV INT DEM CNT STA HEA PRE LOC GOV OTC \"\r\n"
  1622. "x-amz-id-2: STN69VZxIFSz9YJLbz1GDbxpbjG6Qjmmq5E3DxRhOUw+Et0p4hr7c/Q8qNcx4oAD\r\n"
  1623. "Location: http://www.amazon.com/Dan-Brown/e/B000AP9DSU/ref=s9_pop_gw_al1?_encoding=UTF8&refinementId=618073011&pf_rd_m=ATVPDKIKX0DER&pf_rd_s=center-2&pf_rd_r=0SHYY5BZXN3KR20BNFAY&pf_rd_t=101&pf_rd_p=1263340922&pf_rd_i=507846\r\n"
  1624. "Vary: Accept-Encoding,User-Agent\r\n"
  1625. "Content-Type: text/html; charset=ISO-8859-1\r\n"
  1626. "Transfer-Encoding: chunked\r\n"
  1627. "\r\n"
  1628. "1\r\n"
  1629. "\n\r\n"
  1630. "0\r\n"
  1631. "\r\n"
  1632. ,.should_keep_alive= TRUE
  1633. ,.message_complete_on_eof= FALSE
  1634. ,.http_major= 1
  1635. ,.http_minor= 1
  1636. ,.status_code= 301
  1637. ,.response_status= "MovedPermanently"
  1638. ,.num_headers= 9
  1639. ,.headers= { { "Date", "Wed, 15 May 2013 17:06:33 GMT" }
  1640. , { "Server", "Server" }
  1641. , { "x-amz-id-1", "0GPHKXSJQ826RK7GZEB2" }
  1642. , { "p3p", "policyref=\"http://www.amazon.com/w3c/p3p.xml\",CP=\"CAO DSP LAW CUR ADM IVAo IVDo CONo OTPo OUR DELi PUBi OTRi BUS PHY ONL UNI PUR FIN COM NAV INT DEM CNT STA HEA PRE LOC GOV OTC \"" }
  1643. , { "x-amz-id-2", "STN69VZxIFSz9YJLbz1GDbxpbjG6Qjmmq5E3DxRhOUw+Et0p4hr7c/Q8qNcx4oAD" }
  1644. , { "Location", "http://www.amazon.com/Dan-Brown/e/B000AP9DSU/ref=s9_pop_gw_al1?_encoding=UTF8&refinementId=618073011&pf_rd_m=ATVPDKIKX0DER&pf_rd_s=center-2&pf_rd_r=0SHYY5BZXN3KR20BNFAY&pf_rd_t=101&pf_rd_p=1263340922&pf_rd_i=507846" }
  1645. , { "Vary", "Accept-Encoding,User-Agent" }
  1646. , { "Content-Type", "text/html; charset=ISO-8859-1" }
  1647. , { "Transfer-Encoding", "chunked" }
  1648. }
  1649. ,.body= "\n"
  1650. ,.num_chunks_complete= 2
  1651. ,.chunk_lengths= { 1 }
  1652. }
  1653. #define EMPTY_REASON_PHRASE_AFTER_SPACE 20
  1654. , {.name= "empty reason phrase after space"
  1655. ,.type= HTTP_RESPONSE
  1656. ,.raw= "HTTP/1.1 200 \r\n"
  1657. "\r\n"
  1658. ,.should_keep_alive= FALSE
  1659. ,.message_complete_on_eof= TRUE
  1660. ,.http_major= 1
  1661. ,.http_minor= 1
  1662. ,.status_code= 200
  1663. ,.response_status= ""
  1664. ,.num_headers= 0
  1665. ,.headers= {}
  1666. ,.body= ""
  1667. }
  1668. , {.name= NULL } /* sentinel */
  1669. };
  1670. /* strnlen() is a POSIX.2008 addition. Can't rely on it being available so
  1671. * define it ourselves.
  1672. */
  1673. size_t
  1674. strnlen(const char *s, size_t maxlen)
  1675. {
  1676. const char *p;
  1677. p = memchr(s, '\0', maxlen);
  1678. if (p == NULL)
  1679. return maxlen;
  1680. return p - s;
  1681. }
  1682. size_t
  1683. strlncat(char *dst, size_t len, const char *src, size_t n)
  1684. {
  1685. size_t slen;
  1686. size_t dlen;
  1687. size_t rlen;
  1688. size_t ncpy;
  1689. slen = strnlen(src, n);
  1690. dlen = strnlen(dst, len);
  1691. if (dlen < len) {
  1692. rlen = len - dlen;
  1693. ncpy = slen < rlen ? slen : (rlen - 1);
  1694. memcpy(dst + dlen, src, ncpy);
  1695. dst[dlen + ncpy] = '\0';
  1696. }
  1697. assert(len > slen + dlen);
  1698. return slen + dlen;
  1699. }
  1700. size_t
  1701. strlcat(char *dst, const char *src, size_t len)
  1702. {
  1703. return strlncat(dst, len, src, (size_t) -1);
  1704. }
  1705. size_t
  1706. strlncpy(char *dst, size_t len, const char *src, size_t n)
  1707. {
  1708. size_t slen;
  1709. size_t ncpy;
  1710. slen = strnlen(src, n);
  1711. if (len > 0) {
  1712. ncpy = slen < len ? slen : (len - 1);
  1713. memcpy(dst, src, ncpy);
  1714. dst[ncpy] = '\0';
  1715. }
  1716. assert(len > slen);
  1717. return slen;
  1718. }
  1719. size_t
  1720. strlcpy(char *dst, const char *src, size_t len)
  1721. {
  1722. return strlncpy(dst, len, src, (size_t) -1);
  1723. }
  1724. int
  1725. request_url_cb (http_parser *p, const char *buf, size_t len)
  1726. {
  1727. assert(p == parser);
  1728. strlncat(messages[num_messages].request_url,
  1729. sizeof(messages[num_messages].request_url),
  1730. buf,
  1731. len);
  1732. return 0;
  1733. }
  1734. int
  1735. header_field_cb (http_parser *p, const char *buf, size_t len)
  1736. {
  1737. assert(p == parser);
  1738. struct message *m = &messages[num_messages];
  1739. if (m->last_header_element != FIELD)
  1740. m->num_headers++;
  1741. strlncat(m->headers[m->num_headers-1][0],
  1742. sizeof(m->headers[m->num_headers-1][0]),
  1743. buf,
  1744. len);
  1745. m->last_header_element = FIELD;
  1746. return 0;
  1747. }
  1748. int
  1749. header_value_cb (http_parser *p, const char *buf, size_t len)
  1750. {
  1751. assert(p == parser);
  1752. struct message *m = &messages[num_messages];
  1753. strlncat(m->headers[m->num_headers-1][1],
  1754. sizeof(m->headers[m->num_headers-1][1]),
  1755. buf,
  1756. len);
  1757. m->last_header_element = VALUE;
  1758. return 0;
  1759. }
  1760. void
  1761. check_body_is_final (const http_parser *p)
  1762. {
  1763. if (messages[num_messages].body_is_final) {
  1764. fprintf(stderr, "\n\n *** Error http_body_is_final() should return 1 "
  1765. "on last on_body callback call "
  1766. "but it doesn't! ***\n\n");
  1767. assert(0);
  1768. abort();
  1769. }
  1770. messages[num_messages].body_is_final = http_body_is_final(p);
  1771. }
  1772. int
  1773. body_cb (http_parser *p, const char *buf, size_t len)
  1774. {
  1775. assert(p == parser);
  1776. strlncat(messages[num_messages].body,
  1777. sizeof(messages[num_messages].body),
  1778. buf,
  1779. len);
  1780. messages[num_messages].body_size += len;
  1781. check_body_is_final(p);
  1782. // printf("body_cb: '%s'\n", requests[num_messages].body);
  1783. return 0;
  1784. }
  1785. int
  1786. count_body_cb (http_parser *p, const char *buf, size_t len)
  1787. {
  1788. assert(p == parser);
  1789. assert(buf);
  1790. messages[num_messages].body_size += len;
  1791. check_body_is_final(p);
  1792. return 0;
  1793. }
  1794. int
  1795. message_begin_cb (http_parser *p)
  1796. {
  1797. assert(p == parser);
  1798. messages[num_messages].message_begin_cb_called = TRUE;
  1799. return 0;
  1800. }
  1801. int
  1802. headers_complete_cb (http_parser *p)
  1803. {
  1804. assert(p == parser);
  1805. messages[num_messages].method = parser->method;
  1806. messages[num_messages].status_code = parser->status_code;
  1807. messages[num_messages].http_major = parser->http_major;
  1808. messages[num_messages].http_minor = parser->http_minor;
  1809. messages[num_messages].headers_complete_cb_called = TRUE;
  1810. messages[num_messages].should_keep_alive = http_should_keep_alive(parser);
  1811. return 0;
  1812. }
  1813. int
  1814. message_complete_cb (http_parser *p)
  1815. {
  1816. assert(p == parser);
  1817. if (messages[num_messages].should_keep_alive != http_should_keep_alive(parser))
  1818. {
  1819. fprintf(stderr, "\n\n *** Error http_should_keep_alive() should have same "
  1820. "value in both on_message_complete and on_headers_complete "
  1821. "but it doesn't! ***\n\n");
  1822. assert(0);
  1823. abort();
  1824. }
  1825. if (messages[num_messages].body_size &&
  1826. http_body_is_final(p) &&
  1827. !messages[num_messages].body_is_final)
  1828. {
  1829. fprintf(stderr, "\n\n *** Error http_body_is_final() should return 1 "
  1830. "on last on_body callback call "
  1831. "but it doesn't! ***\n\n");
  1832. assert(0);
  1833. abort();
  1834. }
  1835. messages[num_messages].message_complete_cb_called = TRUE;
  1836. messages[num_messages].message_complete_on_eof = currently_parsing_eof;
  1837. num_messages++;
  1838. return 0;
  1839. }
  1840. int
  1841. response_status_cb (http_parser *p, const char *buf, size_t len)
  1842. {
  1843. assert(p == parser);
  1844. strlncat(messages[num_messages].response_status,
  1845. sizeof(messages[num_messages].response_status),
  1846. buf,
  1847. len);
  1848. return 0;
  1849. }
  1850. int
  1851. chunk_header_cb (http_parser *p)
  1852. {
  1853. assert(p == parser);
  1854. int chunk_idx = messages[num_messages].num_chunks;
  1855. messages[num_messages].num_chunks++;
  1856. if (chunk_idx < MAX_CHUNKS) {
  1857. messages[num_messages].chunk_lengths[chunk_idx] = p->content_length;
  1858. }
  1859. return 0;
  1860. }
  1861. int
  1862. chunk_complete_cb (http_parser *p)
  1863. {
  1864. assert(p == parser);
  1865. /* Here we want to verify that each chunk_header_cb is matched by a
  1866. * chunk_complete_cb, so not only should the total number of calls to
  1867. * both callbacks be the same, but they also should be interleaved
  1868. * properly */
  1869. assert(messages[num_messages].num_chunks ==
  1870. messages[num_messages].num_chunks_complete + 1);
  1871. messages[num_messages].num_chunks_complete++;
  1872. return 0;
  1873. }
  1874. /* These dontcall_* callbacks exist so that we can verify that when we're
  1875. * paused, no additional callbacks are invoked */
  1876. int
  1877. dontcall_message_begin_cb (http_parser *p)
  1878. {
  1879. if (p) { } // gcc
  1880. fprintf(stderr, "\n\n*** on_message_begin() called on paused parser ***\n\n");
  1881. abort();
  1882. }
  1883. int
  1884. dontcall_header_field_cb (http_parser *p, const char *buf, size_t len)
  1885. {
  1886. if (p || buf || len) { } // gcc
  1887. fprintf(stderr, "\n\n*** on_header_field() called on paused parser ***\n\n");
  1888. abort();
  1889. }
  1890. int
  1891. dontcall_header_value_cb (http_parser *p, const char *buf, size_t len)
  1892. {
  1893. if (p || buf || len) { } // gcc
  1894. fprintf(stderr, "\n\n*** on_header_value() called on paused parser ***\n\n");
  1895. abort();
  1896. }
  1897. int
  1898. dontcall_request_url_cb (http_parser *p, const char *buf, size_t len)
  1899. {
  1900. if (p || buf || len) { } // gcc
  1901. fprintf(stderr, "\n\n*** on_request_url() called on paused parser ***\n\n");
  1902. abort();
  1903. }
  1904. int
  1905. dontcall_body_cb (http_parser *p, const char *buf, size_t len)
  1906. {
  1907. if (p || buf || len) { } // gcc
  1908. fprintf(stderr, "\n\n*** on_body_cb() called on paused parser ***\n\n");
  1909. abort();
  1910. }
  1911. int
  1912. dontcall_headers_complete_cb (http_parser *p)
  1913. {
  1914. if (p) { } // gcc
  1915. fprintf(stderr, "\n\n*** on_headers_complete() called on paused "
  1916. "parser ***\n\n");
  1917. abort();
  1918. }
  1919. int
  1920. dontcall_message_complete_cb (http_parser *p)
  1921. {
  1922. if (p) { } // gcc
  1923. fprintf(stderr, "\n\n*** on_message_complete() called on paused "
  1924. "parser ***\n\n");
  1925. abort();
  1926. }
  1927. int
  1928. dontcall_response_status_cb (http_parser *p, const char *buf, size_t len)
  1929. {
  1930. if (p || buf || len) { } // gcc
  1931. fprintf(stderr, "\n\n*** on_status() called on paused parser ***\n\n");
  1932. abort();
  1933. }
  1934. int
  1935. dontcall_chunk_header_cb (http_parser *p)
  1936. {
  1937. if (p) { } // gcc
  1938. fprintf(stderr, "\n\n*** on_chunk_header() called on paused parser ***\n\n");
  1939. exit(1);
  1940. }
  1941. int
  1942. dontcall_chunk_complete_cb (http_parser *p)
  1943. {
  1944. if (p) { } // gcc
  1945. fprintf(stderr, "\n\n*** on_chunk_complete() "
  1946. "called on paused parser ***\n\n");
  1947. exit(1);
  1948. }
  1949. static http_parser_settings settings_dontcall =
  1950. {.on_message_begin = dontcall_message_begin_cb
  1951. ,.on_header_field = dontcall_header_field_cb
  1952. ,.on_header_value = dontcall_header_value_cb
  1953. ,.on_url = dontcall_request_url_cb
  1954. ,.on_status = dontcall_response_status_cb
  1955. ,.on_body = dontcall_body_cb
  1956. ,.on_headers_complete = dontcall_headers_complete_cb
  1957. ,.on_message_complete = dontcall_message_complete_cb
  1958. ,.on_chunk_header = dontcall_chunk_header_cb
  1959. ,.on_chunk_complete = dontcall_chunk_complete_cb
  1960. };
  1961. /* These pause_* callbacks always pause the parser and just invoke the regular
  1962. * callback that tracks content. Before returning, we overwrite the parser
  1963. * settings to point to the _dontcall variety so that we can verify that
  1964. * the pause actually did, you know, pause. */
  1965. int
  1966. pause_message_begin_cb (http_parser *p)
  1967. {
  1968. http_parser_pause(p, 1);
  1969. *current_pause_parser = settings_dontcall;
  1970. return message_begin_cb(p);
  1971. }
  1972. int
  1973. pause_header_field_cb (http_parser *p, const char *buf, size_t len)
  1974. {
  1975. http_parser_pause(p, 1);
  1976. *current_pause_parser = settings_dontcall;
  1977. return header_field_cb(p, buf, len);
  1978. }
  1979. int
  1980. pause_header_value_cb (http_parser *p, const char *buf, size_t len)
  1981. {
  1982. http_parser_pause(p, 1);
  1983. *current_pause_parser = settings_dontcall;
  1984. return header_value_cb(p, buf, len);
  1985. }
  1986. int
  1987. pause_request_url_cb (http_parser *p, const char *buf, size_t len)
  1988. {
  1989. http_parser_pause(p, 1);
  1990. *current_pause_parser = settings_dontcall;
  1991. return request_url_cb(p, buf, len);
  1992. }
  1993. int
  1994. pause_body_cb (http_parser *p, const char *buf, size_t len)
  1995. {
  1996. http_parser_pause(p, 1);
  1997. *current_pause_parser = settings_dontcall;
  1998. return body_cb(p, buf, len);
  1999. }
  2000. int
  2001. pause_headers_complete_cb (http_parser *p)
  2002. {
  2003. http_parser_pause(p, 1);
  2004. *current_pause_parser = settings_dontcall;
  2005. return headers_complete_cb(p);
  2006. }
  2007. int
  2008. pause_message_complete_cb (http_parser *p)
  2009. {
  2010. http_parser_pause(p, 1);
  2011. *current_pause_parser = settings_dontcall;
  2012. return message_complete_cb(p);
  2013. }
  2014. int
  2015. pause_response_status_cb (http_parser *p, const char *buf, size_t len)
  2016. {
  2017. http_parser_pause(p, 1);
  2018. *current_pause_parser = settings_dontcall;
  2019. return response_status_cb(p, buf, len);
  2020. }
  2021. int
  2022. pause_chunk_header_cb (http_parser *p)
  2023. {
  2024. http_parser_pause(p, 1);
  2025. *current_pause_parser = settings_dontcall;
  2026. return chunk_header_cb(p);
  2027. }
  2028. int
  2029. pause_chunk_complete_cb (http_parser *p)
  2030. {
  2031. http_parser_pause(p, 1);
  2032. *current_pause_parser = settings_dontcall;
  2033. return chunk_complete_cb(p);
  2034. }
  2035. static http_parser_settings settings_pause =
  2036. {.on_message_begin = pause_message_begin_cb
  2037. ,.on_header_field = pause_header_field_cb
  2038. ,.on_header_value = pause_header_value_cb
  2039. ,.on_url = pause_request_url_cb
  2040. ,.on_status = pause_response_status_cb
  2041. ,.on_body = pause_body_cb
  2042. ,.on_headers_complete = pause_headers_complete_cb
  2043. ,.on_message_complete = pause_message_complete_cb
  2044. ,.on_chunk_header = pause_chunk_header_cb
  2045. ,.on_chunk_complete = pause_chunk_complete_cb
  2046. };
  2047. static http_parser_settings settings =
  2048. {.on_message_begin = message_begin_cb
  2049. ,.on_header_field = header_field_cb
  2050. ,.on_header_value = header_value_cb
  2051. ,.on_url = request_url_cb
  2052. ,.on_status = response_status_cb
  2053. ,.on_body = body_cb
  2054. ,.on_headers_complete = headers_complete_cb
  2055. ,.on_message_complete = message_complete_cb
  2056. ,.on_chunk_header = chunk_header_cb
  2057. ,.on_chunk_complete = chunk_complete_cb
  2058. };
  2059. static http_parser_settings settings_count_body =
  2060. {.on_message_begin = message_begin_cb
  2061. ,.on_header_field = header_field_cb
  2062. ,.on_header_value = header_value_cb
  2063. ,.on_url = request_url_cb
  2064. ,.on_status = response_status_cb
  2065. ,.on_body = count_body_cb
  2066. ,.on_headers_complete = headers_complete_cb
  2067. ,.on_message_complete = message_complete_cb
  2068. ,.on_chunk_header = chunk_header_cb
  2069. ,.on_chunk_complete = chunk_complete_cb
  2070. };
  2071. static http_parser_settings settings_null =
  2072. {.on_message_begin = 0
  2073. ,.on_header_field = 0
  2074. ,.on_header_value = 0
  2075. ,.on_url = 0
  2076. ,.on_status = 0
  2077. ,.on_body = 0
  2078. ,.on_headers_complete = 0
  2079. ,.on_message_complete = 0
  2080. ,.on_chunk_header = 0
  2081. ,.on_chunk_complete = 0
  2082. };
  2083. void
  2084. parser_init (enum http_parser_type type)
  2085. {
  2086. num_messages = 0;
  2087. assert(parser == NULL);
  2088. parser = malloc(sizeof(http_parser));
  2089. http_parser_init(parser, type);
  2090. memset(&messages, 0, sizeof messages);
  2091. }
  2092. void
  2093. parser_free ()
  2094. {
  2095. assert(parser);
  2096. free(parser);
  2097. parser = NULL;
  2098. }
  2099. size_t parse (const char *buf, size_t len)
  2100. {
  2101. size_t nparsed;
  2102. currently_parsing_eof = (len == 0);
  2103. nparsed = http_parser_execute(parser, &settings, buf, len);
  2104. return nparsed;
  2105. }
  2106. size_t parse_count_body (const char *buf, size_t len)
  2107. {
  2108. size_t nparsed;
  2109. currently_parsing_eof = (len == 0);
  2110. nparsed = http_parser_execute(parser, &settings_count_body, buf, len);
  2111. return nparsed;
  2112. }
  2113. size_t parse_pause (const char *buf, size_t len)
  2114. {
  2115. size_t nparsed;
  2116. http_parser_settings s = settings_pause;
  2117. currently_parsing_eof = (len == 0);
  2118. current_pause_parser = &s;
  2119. nparsed = http_parser_execute(parser, current_pause_parser, buf, len);
  2120. return nparsed;
  2121. }
  2122. static inline int
  2123. check_str_eq (const struct message *m,
  2124. const char *prop,
  2125. const char *expected,
  2126. const char *found) {
  2127. if ((expected == NULL) != (found == NULL)) {
  2128. printf("\n*** Error: %s in '%s' ***\n\n", prop, m->name);
  2129. printf("expected %s\n", (expected == NULL) ? "NULL" : expected);
  2130. printf(" found %s\n", (found == NULL) ? "NULL" : found);
  2131. return 0;
  2132. }
  2133. if (expected != NULL && 0 != strcmp(expected, found)) {
  2134. printf("\n*** Error: %s in '%s' ***\n\n", prop, m->name);
  2135. printf("expected '%s'\n", expected);
  2136. printf(" found '%s'\n", found);
  2137. return 0;
  2138. }
  2139. return 1;
  2140. }
  2141. static inline int
  2142. check_num_eq (const struct message *m,
  2143. const char *prop,
  2144. int expected,
  2145. int found) {
  2146. if (expected != found) {
  2147. printf("\n*** Error: %s in '%s' ***\n\n", prop, m->name);
  2148. printf("expected %d\n", expected);
  2149. printf(" found %d\n", found);
  2150. return 0;
  2151. }
  2152. return 1;
  2153. }
  2154. #define MESSAGE_CHECK_STR_EQ(expected, found, prop) \
  2155. if (!check_str_eq(expected, #prop, expected->prop, found->prop)) return 0
  2156. #define MESSAGE_CHECK_NUM_EQ(expected, found, prop) \
  2157. if (!check_num_eq(expected, #prop, expected->prop, found->prop)) return 0
  2158. #define MESSAGE_CHECK_URL_EQ(u, expected, found, prop, fn) \
  2159. do { \
  2160. char ubuf[256]; \
  2161. \
  2162. if ((u)->field_set & (1 << (fn))) { \
  2163. memcpy(ubuf, (found)->request_url + (u)->field_data[(fn)].off, \
  2164. (u)->field_data[(fn)].len); \
  2165. ubuf[(u)->field_data[(fn)].len] = '\0'; \
  2166. } else { \
  2167. ubuf[0] = '\0'; \
  2168. } \
  2169. \
  2170. check_str_eq(expected, #prop, expected->prop, ubuf); \
  2171. } while(0)
  2172. int
  2173. message_eq (int index, const struct message *expected)
  2174. {
  2175. int i;
  2176. struct message *m = &messages[index];
  2177. MESSAGE_CHECK_NUM_EQ(expected, m, http_major);
  2178. MESSAGE_CHECK_NUM_EQ(expected, m, http_minor);
  2179. if (expected->type == HTTP_REQUEST) {
  2180. MESSAGE_CHECK_NUM_EQ(expected, m, method);
  2181. } else {
  2182. MESSAGE_CHECK_NUM_EQ(expected, m, status_code);
  2183. MESSAGE_CHECK_STR_EQ(expected, m, response_status);
  2184. }
  2185. MESSAGE_CHECK_NUM_EQ(expected, m, should_keep_alive);
  2186. MESSAGE_CHECK_NUM_EQ(expected, m, message_complete_on_eof);
  2187. assert(m->message_begin_cb_called);
  2188. assert(m->headers_complete_cb_called);
  2189. assert(m->message_complete_cb_called);
  2190. MESSAGE_CHECK_STR_EQ(expected, m, request_url);
  2191. /* Check URL components; we can't do this w/ CONNECT since it doesn't
  2192. * send us a well-formed URL.
  2193. */
  2194. if (*m->request_url && m->method != HTTP_CONNECT) {
  2195. struct http_parser_url u;
  2196. if (http_parser_parse_url(m->request_url, strlen(m->request_url), 0, &u)) {
  2197. fprintf(stderr, "\n\n*** failed to parse URL %s ***\n\n",
  2198. m->request_url);
  2199. abort();
  2200. }
  2201. if (expected->host) {
  2202. MESSAGE_CHECK_URL_EQ(&u, expected, m, host, UF_HOST);
  2203. }
  2204. if (expected->userinfo) {
  2205. MESSAGE_CHECK_URL_EQ(&u, expected, m, userinfo, UF_USERINFO);
  2206. }
  2207. m->port = (u.field_set & (1 << UF_PORT)) ?
  2208. u.port : 0;
  2209. MESSAGE_CHECK_URL_EQ(&u, expected, m, query_string, UF_QUERY);
  2210. MESSAGE_CHECK_URL_EQ(&u, expected, m, fragment, UF_FRAGMENT);
  2211. MESSAGE_CHECK_URL_EQ(&u, expected, m, request_path, UF_PATH);
  2212. MESSAGE_CHECK_NUM_EQ(expected, m, port);
  2213. }
  2214. if (expected->body_size) {
  2215. MESSAGE_CHECK_NUM_EQ(expected, m, body_size);
  2216. } else {
  2217. MESSAGE_CHECK_STR_EQ(expected, m, body);
  2218. }
  2219. assert(m->num_chunks == m->num_chunks_complete);
  2220. MESSAGE_CHECK_NUM_EQ(expected, m, num_chunks_complete);
  2221. for (i = 0; i < m->num_chunks && i < MAX_CHUNKS; i++) {
  2222. MESSAGE_CHECK_NUM_EQ(expected, m, chunk_lengths[i]);
  2223. }
  2224. MESSAGE_CHECK_NUM_EQ(expected, m, num_headers);
  2225. int r;
  2226. for (i = 0; i < m->num_headers; i++) {
  2227. r = check_str_eq(expected, "header field", expected->headers[i][0], m->headers[i][0]);
  2228. if (!r) return 0;
  2229. r = check_str_eq(expected, "header value", expected->headers[i][1], m->headers[i][1]);
  2230. if (!r) return 0;
  2231. }
  2232. MESSAGE_CHECK_STR_EQ(expected, m, upgrade);
  2233. return 1;
  2234. }
  2235. /* Given a sequence of varargs messages, return the number of them that the
  2236. * parser should successfully parse, taking into account that upgraded
  2237. * messages prevent all subsequent messages from being parsed.
  2238. */
  2239. size_t
  2240. count_parsed_messages(const size_t nmsgs, ...) {
  2241. size_t i;
  2242. va_list ap;
  2243. va_start(ap, nmsgs);
  2244. for (i = 0; i < nmsgs; i++) {
  2245. struct message *m = va_arg(ap, struct message *);
  2246. if (m->upgrade) {
  2247. va_end(ap);
  2248. return i + 1;
  2249. }
  2250. }
  2251. va_end(ap);
  2252. return nmsgs;
  2253. }
  2254. /* Given a sequence of bytes and the number of these that we were able to
  2255. * parse, verify that upgrade bodies are correct.
  2256. */
  2257. void
  2258. upgrade_message_fix(char *body, const size_t nread, const size_t nmsgs, ...) {
  2259. va_list ap;
  2260. size_t i;
  2261. size_t off = 0;
  2262. va_start(ap, nmsgs);
  2263. for (i = 0; i < nmsgs; i++) {
  2264. struct message *m = va_arg(ap, struct message *);
  2265. off += strlen(m->raw);
  2266. if (m->upgrade) {
  2267. off -= strlen(m->upgrade);
  2268. /* Check the portion of the response after its specified upgrade */
  2269. if (!check_str_eq(m, "upgrade", body + off, body + nread)) {
  2270. abort();
  2271. }
  2272. /* Fix up the response so that message_eq() will verify the beginning
  2273. * of the upgrade */
  2274. *(body + nread + strlen(m->upgrade)) = '\0';
  2275. messages[num_messages -1 ].upgrade = body + nread;
  2276. va_end(ap);
  2277. return;
  2278. }
  2279. }
  2280. va_end(ap);
  2281. printf("\n\n*** Error: expected a message with upgrade ***\n");
  2282. abort();
  2283. }
  2284. static void
  2285. print_error (const char *raw, size_t error_location)
  2286. {
  2287. fprintf(stderr, "\n*** %s ***\n\n",
  2288. http_errno_description(HTTP_PARSER_ERRNO(parser)));
  2289. int this_line = 0, char_len = 0;
  2290. size_t i, j, len = strlen(raw), error_location_line = 0;
  2291. for (i = 0; i < len; i++) {
  2292. if (i == error_location) this_line = 1;
  2293. switch (raw[i]) {
  2294. case '\r':
  2295. char_len = 2;
  2296. fprintf(stderr, "\\r");
  2297. break;
  2298. case '\n':
  2299. fprintf(stderr, "\\n\n");
  2300. if (this_line) goto print;
  2301. error_location_line = 0;
  2302. continue;
  2303. default:
  2304. char_len = 1;
  2305. fputc(raw[i], stderr);
  2306. break;
  2307. }
  2308. if (!this_line) error_location_line += char_len;
  2309. }
  2310. fprintf(stderr, "[eof]\n");
  2311. print:
  2312. for (j = 0; j < error_location_line; j++) {
  2313. fputc(' ', stderr);
  2314. }
  2315. fprintf(stderr, "^\n\nerror location: %u\n", (unsigned int)error_location);
  2316. }
  2317. void
  2318. test_preserve_data (void)
  2319. {
  2320. char my_data[] = "application-specific data";
  2321. http_parser parser;
  2322. parser.data = my_data;
  2323. http_parser_init(&parser, HTTP_REQUEST);
  2324. if (parser.data != my_data) {
  2325. printf("\n*** parser.data not preserved accross http_parser_init ***\n\n");
  2326. abort();
  2327. }
  2328. }
  2329. struct url_test {
  2330. const char *name;
  2331. const char *url;
  2332. int is_connect;
  2333. struct http_parser_url u;
  2334. int rv;
  2335. };
  2336. const struct url_test url_tests[] =
  2337. { {.name="proxy request"
  2338. ,.url="http://hostname/"
  2339. ,.is_connect=0
  2340. ,.u=
  2341. {.field_set=(1 << UF_SCHEMA) | (1 << UF_HOST) | (1 << UF_PATH)
  2342. ,.port=0
  2343. ,.field_data=
  2344. {{ 0, 4 } /* UF_SCHEMA */
  2345. ,{ 7, 8 } /* UF_HOST */
  2346. ,{ 0, 0 } /* UF_PORT */
  2347. ,{ 15, 1 } /* UF_PATH */
  2348. ,{ 0, 0 } /* UF_QUERY */
  2349. ,{ 0, 0 } /* UF_FRAGMENT */
  2350. ,{ 0, 0 } /* UF_USERINFO */
  2351. }
  2352. }
  2353. ,.rv=0
  2354. }
  2355. , {.name="proxy request with port"
  2356. ,.url="http://hostname:444/"
  2357. ,.is_connect=0
  2358. ,.u=
  2359. {.field_set=(1 << UF_SCHEMA) | (1 << UF_HOST) | (1 << UF_PORT) | (1 << UF_PATH)
  2360. ,.port=444
  2361. ,.field_data=
  2362. {{ 0, 4 } /* UF_SCHEMA */
  2363. ,{ 7, 8 } /* UF_HOST */
  2364. ,{ 16, 3 } /* UF_PORT */
  2365. ,{ 19, 1 } /* UF_PATH */
  2366. ,{ 0, 0 } /* UF_QUERY */
  2367. ,{ 0, 0 } /* UF_FRAGMENT */
  2368. ,{ 0, 0 } /* UF_USERINFO */
  2369. }
  2370. }
  2371. ,.rv=0
  2372. }
  2373. , {.name="CONNECT request"
  2374. ,.url="hostname:443"
  2375. ,.is_connect=1
  2376. ,.u=
  2377. {.field_set=(1 << UF_HOST) | (1 << UF_PORT)
  2378. ,.port=443
  2379. ,.field_data=
  2380. {{ 0, 0 } /* UF_SCHEMA */
  2381. ,{ 0, 8 } /* UF_HOST */
  2382. ,{ 9, 3 } /* UF_PORT */
  2383. ,{ 0, 0 } /* UF_PATH */
  2384. ,{ 0, 0 } /* UF_QUERY */
  2385. ,{ 0, 0 } /* UF_FRAGMENT */
  2386. ,{ 0, 0 } /* UF_USERINFO */
  2387. }
  2388. }
  2389. ,.rv=0
  2390. }
  2391. , {.name="CONNECT request but not connect"
  2392. ,.url="hostname:443"
  2393. ,.is_connect=0
  2394. ,.rv=1
  2395. }
  2396. , {.name="proxy ipv6 request"
  2397. ,.url="http://[1:2::3:4]/"
  2398. ,.is_connect=0
  2399. ,.u=
  2400. {.field_set=(1 << UF_SCHEMA) | (1 << UF_HOST) | (1 << UF_PATH)
  2401. ,.port=0
  2402. ,.field_data=
  2403. {{ 0, 4 } /* UF_SCHEMA */
  2404. ,{ 8, 8 } /* UF_HOST */
  2405. ,{ 0, 0 } /* UF_PORT */
  2406. ,{ 17, 1 } /* UF_PATH */
  2407. ,{ 0, 0 } /* UF_QUERY */
  2408. ,{ 0, 0 } /* UF_FRAGMENT */
  2409. ,{ 0, 0 } /* UF_USERINFO */
  2410. }
  2411. }
  2412. ,.rv=0
  2413. }
  2414. , {.name="proxy ipv6 request with port"
  2415. ,.url="http://[1:2::3:4]:67/"
  2416. ,.is_connect=0
  2417. ,.u=
  2418. {.field_set=(1 << UF_SCHEMA) | (1 << UF_HOST) | (1 << UF_PORT) | (1 << UF_PATH)
  2419. ,.port=67
  2420. ,.field_data=
  2421. {{ 0, 4 } /* UF_SCHEMA */
  2422. ,{ 8, 8 } /* UF_HOST */
  2423. ,{ 18, 2 } /* UF_PORT */
  2424. ,{ 20, 1 } /* UF_PATH */
  2425. ,{ 0, 0 } /* UF_QUERY */
  2426. ,{ 0, 0 } /* UF_FRAGMENT */
  2427. ,{ 0, 0 } /* UF_USERINFO */
  2428. }
  2429. }
  2430. ,.rv=0
  2431. }
  2432. , {.name="CONNECT ipv6 address"
  2433. ,.url="[1:2::3:4]:443"
  2434. ,.is_connect=1
  2435. ,.u=
  2436. {.field_set=(1 << UF_HOST) | (1 << UF_PORT)
  2437. ,.port=443
  2438. ,.field_data=
  2439. {{ 0, 0 } /* UF_SCHEMA */
  2440. ,{ 1, 8 } /* UF_HOST */
  2441. ,{ 11, 3 } /* UF_PORT */
  2442. ,{ 0, 0 } /* UF_PATH */
  2443. ,{ 0, 0 } /* UF_QUERY */
  2444. ,{ 0, 0 } /* UF_FRAGMENT */
  2445. ,{ 0, 0 } /* UF_USERINFO */
  2446. }
  2447. }
  2448. ,.rv=0
  2449. }
  2450. , {.name="ipv4 in ipv6 address"
  2451. ,.url="http://[2001:0000:0000:0000:0000:0000:1.9.1.1]/"
  2452. ,.is_connect=0
  2453. ,.u=
  2454. {.field_set=(1 << UF_SCHEMA) | (1 << UF_HOST) | (1 << UF_PATH)
  2455. ,.port=0
  2456. ,.field_data=
  2457. {{ 0, 4 } /* UF_SCHEMA */
  2458. ,{ 8, 37 } /* UF_HOST */
  2459. ,{ 0, 0 } /* UF_PORT */
  2460. ,{ 46, 1 } /* UF_PATH */
  2461. ,{ 0, 0 } /* UF_QUERY */
  2462. ,{ 0, 0 } /* UF_FRAGMENT */
  2463. ,{ 0, 0 } /* UF_USERINFO */
  2464. }
  2465. }
  2466. ,.rv=0
  2467. }
  2468. , {.name="extra ? in query string"
  2469. ,.url="http://a.tbcdn.cn/p/fp/2010c/??fp-header-min.css,fp-base-min.css,"
  2470. "fp-channel-min.css,fp-product-min.css,fp-mall-min.css,fp-category-min.css,"
  2471. "fp-sub-min.css,fp-gdp4p-min.css,fp-css3-min.css,fp-misc-min.css?t=20101022.css"
  2472. ,.is_connect=0
  2473. ,.u=
  2474. {.field_set=(1<<UF_SCHEMA) | (1<<UF_HOST) | (1<<UF_PATH) | (1<<UF_QUERY)
  2475. ,.port=0
  2476. ,.field_data=
  2477. {{ 0, 4 } /* UF_SCHEMA */
  2478. ,{ 7, 10 } /* UF_HOST */
  2479. ,{ 0, 0 } /* UF_PORT */
  2480. ,{ 17, 12 } /* UF_PATH */
  2481. ,{ 30,187 } /* UF_QUERY */
  2482. ,{ 0, 0 } /* UF_FRAGMENT */
  2483. ,{ 0, 0 } /* UF_USERINFO */
  2484. }
  2485. }
  2486. ,.rv=0
  2487. }
  2488. , {.name="space URL encoded"
  2489. ,.url="/toto.html?toto=a%20b"
  2490. ,.is_connect=0
  2491. ,.u=
  2492. {.field_set= (1<<UF_PATH) | (1<<UF_QUERY)
  2493. ,.port=0
  2494. ,.field_data=
  2495. {{ 0, 0 } /* UF_SCHEMA */
  2496. ,{ 0, 0 } /* UF_HOST */
  2497. ,{ 0, 0 } /* UF_PORT */
  2498. ,{ 0, 10 } /* UF_PATH */
  2499. ,{ 11, 10 } /* UF_QUERY */
  2500. ,{ 0, 0 } /* UF_FRAGMENT */
  2501. ,{ 0, 0 } /* UF_USERINFO */
  2502. }
  2503. }
  2504. ,.rv=0
  2505. }
  2506. , {.name="URL fragment"
  2507. ,.url="/toto.html#titi"
  2508. ,.is_connect=0
  2509. ,.u=
  2510. {.field_set= (1<<UF_PATH) | (1<<UF_FRAGMENT)
  2511. ,.port=0
  2512. ,.field_data=
  2513. {{ 0, 0 } /* UF_SCHEMA */
  2514. ,{ 0, 0 } /* UF_HOST */
  2515. ,{ 0, 0 } /* UF_PORT */
  2516. ,{ 0, 10 } /* UF_PATH */
  2517. ,{ 0, 0 } /* UF_QUERY */
  2518. ,{ 11, 4 } /* UF_FRAGMENT */
  2519. ,{ 0, 0 } /* UF_USERINFO */
  2520. }
  2521. }
  2522. ,.rv=0
  2523. }
  2524. , {.name="complex URL fragment"
  2525. ,.url="http://www.webmasterworld.com/r.cgi?f=21&d=8405&url="
  2526. "http://www.example.com/index.html?foo=bar&hello=world#midpage"
  2527. ,.is_connect=0
  2528. ,.u=
  2529. {.field_set= (1<<UF_SCHEMA) | (1<<UF_HOST) | (1<<UF_PATH) | (1<<UF_QUERY) |\
  2530. (1<<UF_FRAGMENT)
  2531. ,.port=0
  2532. ,.field_data=
  2533. {{ 0, 4 } /* UF_SCHEMA */
  2534. ,{ 7, 22 } /* UF_HOST */
  2535. ,{ 0, 0 } /* UF_PORT */
  2536. ,{ 29, 6 } /* UF_PATH */
  2537. ,{ 36, 69 } /* UF_QUERY */
  2538. ,{106, 7 } /* UF_FRAGMENT */
  2539. ,{ 0, 0 } /* UF_USERINFO */
  2540. }
  2541. }
  2542. ,.rv=0
  2543. }
  2544. , {.name="complex URL from node js url parser doc"
  2545. ,.url="http://host.com:8080/p/a/t/h?query=string#hash"
  2546. ,.is_connect=0
  2547. ,.u=
  2548. {.field_set= (1<<UF_SCHEMA) | (1<<UF_HOST) | (1<<UF_PORT) | (1<<UF_PATH) |\
  2549. (1<<UF_QUERY) | (1<<UF_FRAGMENT)
  2550. ,.port=8080
  2551. ,.field_data=
  2552. {{ 0, 4 } /* UF_SCHEMA */
  2553. ,{ 7, 8 } /* UF_HOST */
  2554. ,{ 16, 4 } /* UF_PORT */
  2555. ,{ 20, 8 } /* UF_PATH */
  2556. ,{ 29, 12 } /* UF_QUERY */
  2557. ,{ 42, 4 } /* UF_FRAGMENT */
  2558. ,{ 0, 0 } /* UF_USERINFO */
  2559. }
  2560. }
  2561. ,.rv=0
  2562. }
  2563. , {.name="complex URL with basic auth from node js url parser doc"
  2564. ,.url="http://a:b@host.com:8080/p/a/t/h?query=string#hash"
  2565. ,.is_connect=0
  2566. ,.u=
  2567. {.field_set= (1<<UF_SCHEMA) | (1<<UF_HOST) | (1<<UF_PORT) | (1<<UF_PATH) |\
  2568. (1<<UF_QUERY) | (1<<UF_FRAGMENT) | (1<<UF_USERINFO)
  2569. ,.port=8080
  2570. ,.field_data=
  2571. {{ 0, 4 } /* UF_SCHEMA */
  2572. ,{ 11, 8 } /* UF_HOST */
  2573. ,{ 20, 4 } /* UF_PORT */
  2574. ,{ 24, 8 } /* UF_PATH */
  2575. ,{ 33, 12 } /* UF_QUERY */
  2576. ,{ 46, 4 } /* UF_FRAGMENT */
  2577. ,{ 7, 3 } /* UF_USERINFO */
  2578. }
  2579. }
  2580. ,.rv=0
  2581. }
  2582. , {.name="double @"
  2583. ,.url="http://a:b@@hostname:443/"
  2584. ,.is_connect=0
  2585. ,.rv=1
  2586. }
  2587. , {.name="proxy empty host"
  2588. ,.url="http://:443/"
  2589. ,.is_connect=0
  2590. ,.rv=1
  2591. }
  2592. , {.name="proxy empty port"
  2593. ,.url="http://hostname:/"
  2594. ,.is_connect=0
  2595. ,.rv=1
  2596. }
  2597. , {.name="CONNECT with basic auth"
  2598. ,.url="a:b@hostname:443"
  2599. ,.is_connect=1
  2600. ,.rv=1
  2601. }
  2602. , {.name="CONNECT empty host"
  2603. ,.url=":443"
  2604. ,.is_connect=1
  2605. ,.rv=1
  2606. }
  2607. , {.name="CONNECT empty port"
  2608. ,.url="hostname:"
  2609. ,.is_connect=1
  2610. ,.rv=1
  2611. }
  2612. , {.name="CONNECT with extra bits"
  2613. ,.url="hostname:443/"
  2614. ,.is_connect=1
  2615. ,.rv=1
  2616. }
  2617. , {.name="space in URL"
  2618. ,.url="/foo bar/"
  2619. ,.rv=1 /* s_dead */
  2620. }
  2621. , {.name="proxy basic auth with space url encoded"
  2622. ,.url="http://a%20:b@host.com/"
  2623. ,.is_connect=0
  2624. ,.u=
  2625. {.field_set= (1<<UF_SCHEMA) | (1<<UF_HOST) | (1<<UF_PATH) | (1<<UF_USERINFO)
  2626. ,.port=0
  2627. ,.field_data=
  2628. {{ 0, 4 } /* UF_SCHEMA */
  2629. ,{ 14, 8 } /* UF_HOST */
  2630. ,{ 0, 0 } /* UF_PORT */
  2631. ,{ 22, 1 } /* UF_PATH */
  2632. ,{ 0, 0 } /* UF_QUERY */
  2633. ,{ 0, 0 } /* UF_FRAGMENT */
  2634. ,{ 7, 6 } /* UF_USERINFO */
  2635. }
  2636. }
  2637. ,.rv=0
  2638. }
  2639. , {.name="carriage return in URL"
  2640. ,.url="/foo\rbar/"
  2641. ,.rv=1 /* s_dead */
  2642. }
  2643. , {.name="proxy double : in URL"
  2644. ,.url="http://hostname::443/"
  2645. ,.rv=1 /* s_dead */
  2646. }
  2647. , {.name="proxy basic auth with double :"
  2648. ,.url="http://a::b@host.com/"
  2649. ,.is_connect=0
  2650. ,.u=
  2651. {.field_set= (1<<UF_SCHEMA) | (1<<UF_HOST) | (1<<UF_PATH) | (1<<UF_USERINFO)
  2652. ,.port=0
  2653. ,.field_data=
  2654. {{ 0, 4 } /* UF_SCHEMA */
  2655. ,{ 12, 8 } /* UF_HOST */
  2656. ,{ 0, 0 } /* UF_PORT */
  2657. ,{ 20, 1 } /* UF_PATH */
  2658. ,{ 0, 0 } /* UF_QUERY */
  2659. ,{ 0, 0 } /* UF_FRAGMENT */
  2660. ,{ 7, 4 } /* UF_USERINFO */
  2661. }
  2662. }
  2663. ,.rv=0
  2664. }
  2665. , {.name="line feed in URL"
  2666. ,.url="/foo\nbar/"
  2667. ,.rv=1 /* s_dead */
  2668. }
  2669. , {.name="proxy empty basic auth"
  2670. ,.url="http://@hostname/fo"
  2671. ,.u=
  2672. {.field_set= (1<<UF_SCHEMA) | (1<<UF_HOST) | (1<<UF_PATH)
  2673. ,.port=0
  2674. ,.field_data=
  2675. {{ 0, 4 } /* UF_SCHEMA */
  2676. ,{ 8, 8 } /* UF_HOST */
  2677. ,{ 0, 0 } /* UF_PORT */
  2678. ,{ 16, 3 } /* UF_PATH */
  2679. ,{ 0, 0 } /* UF_QUERY */
  2680. ,{ 0, 0 } /* UF_FRAGMENT */
  2681. ,{ 0, 0 } /* UF_USERINFO */
  2682. }
  2683. }
  2684. ,.rv=0
  2685. }
  2686. , {.name="proxy line feed in hostname"
  2687. ,.url="http://host\name/fo"
  2688. ,.rv=1 /* s_dead */
  2689. }
  2690. , {.name="proxy % in hostname"
  2691. ,.url="http://host%name/fo"
  2692. ,.rv=1 /* s_dead */
  2693. }
  2694. , {.name="proxy ; in hostname"
  2695. ,.url="http://host;ame/fo"
  2696. ,.rv=1 /* s_dead */
  2697. }
  2698. , {.name="proxy basic auth with unreservedchars"
  2699. ,.url="http://a!;-_!=+$@host.com/"
  2700. ,.is_connect=0
  2701. ,.u=
  2702. {.field_set= (1<<UF_SCHEMA) | (1<<UF_HOST) | (1<<UF_PATH) | (1<<UF_USERINFO)
  2703. ,.port=0
  2704. ,.field_data=
  2705. {{ 0, 4 } /* UF_SCHEMA */
  2706. ,{ 17, 8 } /* UF_HOST */
  2707. ,{ 0, 0 } /* UF_PORT */
  2708. ,{ 25, 1 } /* UF_PATH */
  2709. ,{ 0, 0 } /* UF_QUERY */
  2710. ,{ 0, 0 } /* UF_FRAGMENT */
  2711. ,{ 7, 9 } /* UF_USERINFO */
  2712. }
  2713. }
  2714. ,.rv=0
  2715. }
  2716. , {.name="proxy only empty basic auth"
  2717. ,.url="http://@/fo"
  2718. ,.rv=1 /* s_dead */
  2719. }
  2720. , {.name="proxy only basic auth"
  2721. ,.url="http://toto@/fo"
  2722. ,.rv=1 /* s_dead */
  2723. }
  2724. , {.name="proxy emtpy hostname"
  2725. ,.url="http:///fo"
  2726. ,.rv=1 /* s_dead */
  2727. }
  2728. , {.name="proxy = in URL"
  2729. ,.url="http://host=ame/fo"
  2730. ,.rv=1 /* s_dead */
  2731. }
  2732. , {.name="ipv6 address with Zone ID"
  2733. ,.url="http://[fe80::a%25eth0]/"
  2734. ,.is_connect=0
  2735. ,.u=
  2736. {.field_set= (1<<UF_SCHEMA) | (1<<UF_HOST) | (1<<UF_PATH)
  2737. ,.port=0
  2738. ,.field_data=
  2739. {{ 0, 4 } /* UF_SCHEMA */
  2740. ,{ 8, 14 } /* UF_HOST */
  2741. ,{ 0, 0 } /* UF_PORT */
  2742. ,{ 23, 1 } /* UF_PATH */
  2743. ,{ 0, 0 } /* UF_QUERY */
  2744. ,{ 0, 0 } /* UF_FRAGMENT */
  2745. ,{ 0, 0 } /* UF_USERINFO */
  2746. }
  2747. }
  2748. ,.rv=0
  2749. }
  2750. , {.name="ipv6 address with Zone ID, but '%' is not percent-encoded"
  2751. ,.url="http://[fe80::a%eth0]/"
  2752. ,.is_connect=0
  2753. ,.u=
  2754. {.field_set= (1<<UF_SCHEMA) | (1<<UF_HOST) | (1<<UF_PATH)
  2755. ,.port=0
  2756. ,.field_data=
  2757. {{ 0, 4 } /* UF_SCHEMA */
  2758. ,{ 8, 12 } /* UF_HOST */
  2759. ,{ 0, 0 } /* UF_PORT */
  2760. ,{ 21, 1 } /* UF_PATH */
  2761. ,{ 0, 0 } /* UF_QUERY */
  2762. ,{ 0, 0 } /* UF_FRAGMENT */
  2763. ,{ 0, 0 } /* UF_USERINFO */
  2764. }
  2765. }
  2766. ,.rv=0
  2767. }
  2768. , {.name="ipv6 address ending with '%'"
  2769. ,.url="http://[fe80::a%]/"
  2770. ,.rv=1 /* s_dead */
  2771. }
  2772. , {.name="ipv6 address with Zone ID including bad character"
  2773. ,.url="http://[fe80::a%$HOME]/"
  2774. ,.rv=1 /* s_dead */
  2775. }
  2776. , {.name="just ipv6 Zone ID"
  2777. ,.url="http://[%eth0]/"
  2778. ,.rv=1 /* s_dead */
  2779. }
  2780. #if HTTP_PARSER_STRICT
  2781. , {.name="tab in URL"
  2782. ,.url="/foo\tbar/"
  2783. ,.rv=1 /* s_dead */
  2784. }
  2785. , {.name="form feed in URL"
  2786. ,.url="/foo\fbar/"
  2787. ,.rv=1 /* s_dead */
  2788. }
  2789. #else /* !HTTP_PARSER_STRICT */
  2790. , {.name="tab in URL"
  2791. ,.url="/foo\tbar/"
  2792. ,.u=
  2793. {.field_set=(1 << UF_PATH)
  2794. ,.field_data=
  2795. {{ 0, 0 } /* UF_SCHEMA */
  2796. ,{ 0, 0 } /* UF_HOST */
  2797. ,{ 0, 0 } /* UF_PORT */
  2798. ,{ 0, 9 } /* UF_PATH */
  2799. ,{ 0, 0 } /* UF_QUERY */
  2800. ,{ 0, 0 } /* UF_FRAGMENT */
  2801. ,{ 0, 0 } /* UF_USERINFO */
  2802. }
  2803. }
  2804. ,.rv=0
  2805. }
  2806. , {.name="form feed in URL"
  2807. ,.url="/foo\fbar/"
  2808. ,.u=
  2809. {.field_set=(1 << UF_PATH)
  2810. ,.field_data=
  2811. {{ 0, 0 } /* UF_SCHEMA */
  2812. ,{ 0, 0 } /* UF_HOST */
  2813. ,{ 0, 0 } /* UF_PORT */
  2814. ,{ 0, 9 } /* UF_PATH */
  2815. ,{ 0, 0 } /* UF_QUERY */
  2816. ,{ 0, 0 } /* UF_FRAGMENT */
  2817. ,{ 0, 0 } /* UF_USERINFO */
  2818. }
  2819. }
  2820. ,.rv=0
  2821. }
  2822. #endif
  2823. };
  2824. void
  2825. dump_url (const char *url, const struct http_parser_url *u)
  2826. {
  2827. unsigned int i;
  2828. printf("\tfield_set: 0x%x, port: %u\n", u->field_set, u->port);
  2829. for (i = 0; i < UF_MAX; i++) {
  2830. if ((u->field_set & (1 << i)) == 0) {
  2831. printf("\tfield_data[%u]: unset\n", i);
  2832. continue;
  2833. }
  2834. printf("\tfield_data[%u]: off: %u len: %u part: \"%.*s\n\"",
  2835. i,
  2836. u->field_data[i].off,
  2837. u->field_data[i].len,
  2838. u->field_data[i].len,
  2839. url + u->field_data[i].off);
  2840. }
  2841. }
  2842. void
  2843. test_parse_url (void)
  2844. {
  2845. struct http_parser_url u;
  2846. const struct url_test *test;
  2847. unsigned int i;
  2848. int rv;
  2849. for (i = 0; i < (sizeof(url_tests) / sizeof(url_tests[0])); i++) {
  2850. test = &url_tests[i];
  2851. memset(&u, 0, sizeof(u));
  2852. rv = http_parser_parse_url(test->url,
  2853. strlen(test->url),
  2854. test->is_connect,
  2855. &u);
  2856. if (test->rv == 0) {
  2857. if (rv != 0) {
  2858. printf("\n*** http_parser_parse_url(\"%s\") \"%s\" test failed, "
  2859. "unexpected rv %d ***\n\n", test->url, test->name, rv);
  2860. abort();
  2861. }
  2862. if (memcmp(&u, &test->u, sizeof(u)) != 0) {
  2863. printf("\n*** http_parser_parse_url(\"%s\") \"%s\" failed ***\n",
  2864. test->url, test->name);
  2865. printf("target http_parser_url:\n");
  2866. dump_url(test->url, &test->u);
  2867. printf("result http_parser_url:\n");
  2868. dump_url(test->url, &u);
  2869. abort();
  2870. }
  2871. } else {
  2872. /* test->rv != 0 */
  2873. if (rv == 0) {
  2874. printf("\n*** http_parser_parse_url(\"%s\") \"%s\" test failed, "
  2875. "unexpected rv %d ***\n\n", test->url, test->name, rv);
  2876. abort();
  2877. }
  2878. }
  2879. }
  2880. }
  2881. void
  2882. test_method_str (void)
  2883. {
  2884. assert(0 == strcmp("GET", http_method_str(HTTP_GET)));
  2885. assert(0 == strcmp("<unknown>", http_method_str(1337)));
  2886. }
  2887. void
  2888. test_message (const struct message *message)
  2889. {
  2890. size_t raw_len = strlen(message->raw);
  2891. size_t msg1len;
  2892. for (msg1len = 0; msg1len < raw_len; msg1len++) {
  2893. parser_init(message->type);
  2894. size_t read;
  2895. const char *msg1 = message->raw;
  2896. const char *msg2 = msg1 + msg1len;
  2897. size_t msg2len = raw_len - msg1len;
  2898. if (msg1len) {
  2899. read = parse(msg1, msg1len);
  2900. if (message->upgrade && parser->upgrade && num_messages > 0) {
  2901. messages[num_messages - 1].upgrade = msg1 + read;
  2902. goto test;
  2903. }
  2904. if (read != msg1len) {
  2905. print_error(msg1, read);
  2906. abort();
  2907. }
  2908. }
  2909. read = parse(msg2, msg2len);
  2910. if (message->upgrade && parser->upgrade) {
  2911. messages[num_messages - 1].upgrade = msg2 + read;
  2912. goto test;
  2913. }
  2914. if (read != msg2len) {
  2915. print_error(msg2, read);
  2916. abort();
  2917. }
  2918. read = parse(NULL, 0);
  2919. if (read != 0) {
  2920. print_error(message->raw, read);
  2921. abort();
  2922. }
  2923. test:
  2924. if (num_messages != 1) {
  2925. printf("\n*** num_messages != 1 after testing '%s' ***\n\n", message->name);
  2926. abort();
  2927. }
  2928. if(!message_eq(0, message)) abort();
  2929. parser_free();
  2930. }
  2931. }
  2932. void
  2933. test_message_count_body (const struct message *message)
  2934. {
  2935. parser_init(message->type);
  2936. size_t read;
  2937. size_t l = strlen(message->raw);
  2938. size_t i, toread;
  2939. size_t chunk = 4024;
  2940. for (i = 0; i < l; i+= chunk) {
  2941. toread = MIN(l-i, chunk);
  2942. read = parse_count_body(message->raw + i, toread);
  2943. if (read != toread) {
  2944. print_error(message->raw, read);
  2945. abort();
  2946. }
  2947. }
  2948. read = parse_count_body(NULL, 0);
  2949. if (read != 0) {
  2950. print_error(message->raw, read);
  2951. abort();
  2952. }
  2953. if (num_messages != 1) {
  2954. printf("\n*** num_messages != 1 after testing '%s' ***\n\n", message->name);
  2955. abort();
  2956. }
  2957. if(!message_eq(0, message)) abort();
  2958. parser_free();
  2959. }
  2960. void
  2961. test_simple (const char *buf, enum http_errno err_expected)
  2962. {
  2963. parser_init(HTTP_REQUEST);
  2964. enum http_errno err;
  2965. parse(buf, strlen(buf));
  2966. err = HTTP_PARSER_ERRNO(parser);
  2967. parse(NULL, 0);
  2968. parser_free();
  2969. /* In strict mode, allow us to pass with an unexpected HPE_STRICT as
  2970. * long as the caller isn't expecting success.
  2971. */
  2972. #if HTTP_PARSER_STRICT
  2973. if (err_expected != err && err_expected != HPE_OK && err != HPE_STRICT) {
  2974. #else
  2975. if (err_expected != err) {
  2976. #endif
  2977. fprintf(stderr, "\n*** test_simple expected %s, but saw %s ***\n\n%s\n",
  2978. http_errno_name(err_expected), http_errno_name(err), buf);
  2979. abort();
  2980. }
  2981. }
  2982. void
  2983. test_invalid_header_content (int req, const char* str)
  2984. {
  2985. http_parser parser;
  2986. http_parser_init(&parser, req ? HTTP_REQUEST : HTTP_RESPONSE);
  2987. size_t parsed;
  2988. const char *buf;
  2989. buf = req ?
  2990. "GET / HTTP/1.1\r\n" :
  2991. "HTTP/1.1 200 OK\r\n";
  2992. parsed = http_parser_execute(&parser, &settings_null, buf, strlen(buf));
  2993. assert(parsed == strlen(buf));
  2994. buf = str;
  2995. size_t buflen = strlen(buf);
  2996. parsed = http_parser_execute(&parser, &settings_null, buf, buflen);
  2997. if (parsed != buflen) {
  2998. assert(HTTP_PARSER_ERRNO(&parser) == HPE_INVALID_HEADER_TOKEN);
  2999. return;
  3000. }
  3001. fprintf(stderr,
  3002. "\n*** Error expected but none in invalid header content test ***\n");
  3003. abort();
  3004. }
  3005. void
  3006. test_invalid_header_field_content_error (int req)
  3007. {
  3008. test_invalid_header_content(req, "Foo: F\01ailure");
  3009. test_invalid_header_content(req, "Foo: B\02ar");
  3010. }
  3011. void
  3012. test_invalid_header_field (int req, const char* str)
  3013. {
  3014. http_parser parser;
  3015. http_parser_init(&parser, req ? HTTP_REQUEST : HTTP_RESPONSE);
  3016. size_t parsed;
  3017. const char *buf;
  3018. buf = req ?
  3019. "GET / HTTP/1.1\r\n" :
  3020. "HTTP/1.1 200 OK\r\n";
  3021. parsed = http_parser_execute(&parser, &settings_null, buf, strlen(buf));
  3022. assert(parsed == strlen(buf));
  3023. buf = str;
  3024. size_t buflen = strlen(buf);
  3025. parsed = http_parser_execute(&parser, &settings_null, buf, buflen);
  3026. if (parsed != buflen) {
  3027. assert(HTTP_PARSER_ERRNO(&parser) == HPE_INVALID_HEADER_TOKEN);
  3028. return;
  3029. }
  3030. fprintf(stderr,
  3031. "\n*** Error expected but none in invalid header token test ***\n");
  3032. abort();
  3033. }
  3034. void
  3035. test_invalid_header_field_token_error (int req)
  3036. {
  3037. test_invalid_header_field(req, "Fo@: Failure");
  3038. test_invalid_header_field(req, "Foo\01\test: Bar");
  3039. }
  3040. void
  3041. test_double_content_length_error (int req)
  3042. {
  3043. http_parser parser;
  3044. http_parser_init(&parser, req ? HTTP_REQUEST : HTTP_RESPONSE);
  3045. size_t parsed;
  3046. const char *buf;
  3047. buf = req ?
  3048. "GET / HTTP/1.1\r\n" :
  3049. "HTTP/1.1 200 OK\r\n";
  3050. parsed = http_parser_execute(&parser, &settings_null, buf, strlen(buf));
  3051. assert(parsed == strlen(buf));
  3052. buf = "Content-Length: 0\r\nContent-Length: 1\r\n\r\n";
  3053. size_t buflen = strlen(buf);
  3054. parsed = http_parser_execute(&parser, &settings_null, buf, buflen);
  3055. if (parsed != buflen) {
  3056. assert(HTTP_PARSER_ERRNO(&parser) == HPE_UNEXPECTED_CONTENT_LENGTH);
  3057. return;
  3058. }
  3059. fprintf(stderr,
  3060. "\n*** Error expected but none in double content-length test ***\n");
  3061. abort();
  3062. }
  3063. void
  3064. test_chunked_content_length_error (int req)
  3065. {
  3066. http_parser parser;
  3067. http_parser_init(&parser, req ? HTTP_REQUEST : HTTP_RESPONSE);
  3068. size_t parsed;
  3069. const char *buf;
  3070. buf = req ?
  3071. "GET / HTTP/1.1\r\n" :
  3072. "HTTP/1.1 200 OK\r\n";
  3073. parsed = http_parser_execute(&parser, &settings_null, buf, strlen(buf));
  3074. assert(parsed == strlen(buf));
  3075. buf = "Transfer-Encoding: chunked\r\nContent-Length: 1\r\n\r\n";
  3076. size_t buflen = strlen(buf);
  3077. parsed = http_parser_execute(&parser, &settings_null, buf, buflen);
  3078. if (parsed != buflen) {
  3079. assert(HTTP_PARSER_ERRNO(&parser) == HPE_UNEXPECTED_CONTENT_LENGTH);
  3080. return;
  3081. }
  3082. fprintf(stderr,
  3083. "\n*** Error expected but none in chunked content-length test ***\n");
  3084. abort();
  3085. }
  3086. void
  3087. test_header_cr_no_lf_error (int req)
  3088. {
  3089. http_parser parser;
  3090. http_parser_init(&parser, req ? HTTP_REQUEST : HTTP_RESPONSE);
  3091. size_t parsed;
  3092. const char *buf;
  3093. buf = req ?
  3094. "GET / HTTP/1.1\r\n" :
  3095. "HTTP/1.1 200 OK\r\n";
  3096. parsed = http_parser_execute(&parser, &settings_null, buf, strlen(buf));
  3097. assert(parsed == strlen(buf));
  3098. buf = "Foo: 1\rBar: 1\r\n\r\n";
  3099. size_t buflen = strlen(buf);
  3100. parsed = http_parser_execute(&parser, &settings_null, buf, buflen);
  3101. if (parsed != buflen) {
  3102. assert(HTTP_PARSER_ERRNO(&parser) == HPE_LF_EXPECTED);
  3103. return;
  3104. }
  3105. fprintf(stderr,
  3106. "\n*** Error expected but none in header whitespace test ***\n");
  3107. abort();
  3108. }
  3109. void
  3110. test_header_overflow_error (int req)
  3111. {
  3112. http_parser parser;
  3113. http_parser_init(&parser, req ? HTTP_REQUEST : HTTP_RESPONSE);
  3114. size_t parsed;
  3115. const char *buf;
  3116. buf = req ? "GET / HTTP/1.1\r\n" : "HTTP/1.0 200 OK\r\n";
  3117. parsed = http_parser_execute(&parser, &settings_null, buf, strlen(buf));
  3118. assert(parsed == strlen(buf));
  3119. buf = "header-key: header-value\r\n";
  3120. size_t buflen = strlen(buf);
  3121. int i;
  3122. for (i = 0; i < 10000; i++) {
  3123. parsed = http_parser_execute(&parser, &settings_null, buf, buflen);
  3124. if (parsed != buflen) {
  3125. //fprintf(stderr, "error found on iter %d\n", i);
  3126. assert(HTTP_PARSER_ERRNO(&parser) == HPE_HEADER_OVERFLOW);
  3127. return;
  3128. }
  3129. }
  3130. fprintf(stderr, "\n*** Error expected but none in header overflow test ***\n");
  3131. abort();
  3132. }
  3133. void
  3134. test_header_nread_value ()
  3135. {
  3136. http_parser parser;
  3137. http_parser_init(&parser, HTTP_REQUEST);
  3138. size_t parsed;
  3139. const char *buf;
  3140. buf = "GET / HTTP/1.1\r\nheader: value\nhdr: value\r\n";
  3141. parsed = http_parser_execute(&parser, &settings_null, buf, strlen(buf));
  3142. assert(parsed == strlen(buf));
  3143. assert(parser.nread == strlen(buf));
  3144. }
  3145. static void
  3146. test_content_length_overflow (const char *buf, size_t buflen, int expect_ok)
  3147. {
  3148. http_parser parser;
  3149. http_parser_init(&parser, HTTP_RESPONSE);
  3150. http_parser_execute(&parser, &settings_null, buf, buflen);
  3151. if (expect_ok)
  3152. assert(HTTP_PARSER_ERRNO(&parser) == HPE_OK);
  3153. else
  3154. assert(HTTP_PARSER_ERRNO(&parser) == HPE_INVALID_CONTENT_LENGTH);
  3155. }
  3156. void
  3157. test_header_content_length_overflow_error (void)
  3158. {
  3159. #define X(size) \
  3160. "HTTP/1.1 200 OK\r\n" \
  3161. "Content-Length: " #size "\r\n" \
  3162. "\r\n"
  3163. const char a[] = X(1844674407370955160); /* 2^64 / 10 - 1 */
  3164. const char b[] = X(18446744073709551615); /* 2^64-1 */
  3165. const char c[] = X(18446744073709551616); /* 2^64 */
  3166. #undef X
  3167. test_content_length_overflow(a, sizeof(a) - 1, 1); /* expect ok */
  3168. test_content_length_overflow(b, sizeof(b) - 1, 0); /* expect failure */
  3169. test_content_length_overflow(c, sizeof(c) - 1, 0); /* expect failure */
  3170. }
  3171. void
  3172. test_chunk_content_length_overflow_error (void)
  3173. {
  3174. #define X(size) \
  3175. "HTTP/1.1 200 OK\r\n" \
  3176. "Transfer-Encoding: chunked\r\n" \
  3177. "\r\n" \
  3178. #size "\r\n" \
  3179. "..."
  3180. const char a[] = X(FFFFFFFFFFFFFFE); /* 2^64 / 16 - 1 */
  3181. const char b[] = X(FFFFFFFFFFFFFFFF); /* 2^64-1 */
  3182. const char c[] = X(10000000000000000); /* 2^64 */
  3183. #undef X
  3184. test_content_length_overflow(a, sizeof(a) - 1, 1); /* expect ok */
  3185. test_content_length_overflow(b, sizeof(b) - 1, 0); /* expect failure */
  3186. test_content_length_overflow(c, sizeof(c) - 1, 0); /* expect failure */
  3187. }
  3188. void
  3189. test_no_overflow_long_body (int req, size_t length)
  3190. {
  3191. http_parser parser;
  3192. http_parser_init(&parser, req ? HTTP_REQUEST : HTTP_RESPONSE);
  3193. size_t parsed;
  3194. size_t i;
  3195. char buf1[3000];
  3196. size_t buf1len = sprintf(buf1, "%s\r\nConnection: Keep-Alive\r\nContent-Length: %lu\r\n\r\n",
  3197. req ? "POST / HTTP/1.0" : "HTTP/1.0 200 OK", (unsigned long)length);
  3198. parsed = http_parser_execute(&parser, &settings_null, buf1, buf1len);
  3199. if (parsed != buf1len)
  3200. goto err;
  3201. for (i = 0; i < length; i++) {
  3202. char foo = 'a';
  3203. parsed = http_parser_execute(&parser, &settings_null, &foo, 1);
  3204. if (parsed != 1)
  3205. goto err;
  3206. }
  3207. parsed = http_parser_execute(&parser, &settings_null, buf1, buf1len);
  3208. if (parsed != buf1len) goto err;
  3209. return;
  3210. err:
  3211. fprintf(stderr,
  3212. "\n*** error in test_no_overflow_long_body %s of length %lu ***\n",
  3213. req ? "REQUEST" : "RESPONSE",
  3214. (unsigned long)length);
  3215. abort();
  3216. }
  3217. void
  3218. test_multiple3 (const struct message *r1, const struct message *r2, const struct message *r3)
  3219. {
  3220. int message_count = count_parsed_messages(3, r1, r2, r3);
  3221. char total[ strlen(r1->raw)
  3222. + strlen(r2->raw)
  3223. + strlen(r3->raw)
  3224. + 1
  3225. ];
  3226. total[0] = '\0';
  3227. strcat(total, r1->raw);
  3228. strcat(total, r2->raw);
  3229. strcat(total, r3->raw);
  3230. parser_init(r1->type);
  3231. size_t read;
  3232. read = parse(total, strlen(total));
  3233. if (parser->upgrade) {
  3234. upgrade_message_fix(total, read, 3, r1, r2, r3);
  3235. goto test;
  3236. }
  3237. if (read != strlen(total)) {
  3238. print_error(total, read);
  3239. abort();
  3240. }
  3241. read = parse(NULL, 0);
  3242. if (read != 0) {
  3243. print_error(total, read);
  3244. abort();
  3245. }
  3246. test:
  3247. if (message_count != num_messages) {
  3248. fprintf(stderr, "\n\n*** Parser didn't see 3 messages only %d *** \n", num_messages);
  3249. abort();
  3250. }
  3251. if (!message_eq(0, r1)) abort();
  3252. if (message_count > 1 && !message_eq(1, r2)) abort();
  3253. if (message_count > 2 && !message_eq(2, r3)) abort();
  3254. parser_free();
  3255. }
  3256. /* SCAN through every possible breaking to make sure the
  3257. * parser can handle getting the content in any chunks that
  3258. * might come from the socket
  3259. */
  3260. void
  3261. test_scan (const struct message *r1, const struct message *r2, const struct message *r3)
  3262. {
  3263. char total[80*1024] = "\0";
  3264. char buf1[80*1024] = "\0";
  3265. char buf2[80*1024] = "\0";
  3266. char buf3[80*1024] = "\0";
  3267. strcat(total, r1->raw);
  3268. strcat(total, r2->raw);
  3269. strcat(total, r3->raw);
  3270. size_t read;
  3271. int total_len = strlen(total);
  3272. int total_ops = 2 * (total_len - 1) * (total_len - 2) / 2;
  3273. int ops = 0 ;
  3274. size_t buf1_len, buf2_len, buf3_len;
  3275. int message_count = count_parsed_messages(3, r1, r2, r3);
  3276. int i,j,type_both;
  3277. for (type_both = 0; type_both < 2; type_both ++ ) {
  3278. for (j = 2; j < total_len; j ++ ) {
  3279. for (i = 1; i < j; i ++ ) {
  3280. if (ops % 1000 == 0) {
  3281. printf("\b\b\b\b%3.0f%%", 100 * (float)ops /(float)total_ops);
  3282. fflush(stdout);
  3283. }
  3284. ops += 1;
  3285. parser_init(type_both ? HTTP_BOTH : r1->type);
  3286. buf1_len = i;
  3287. strlncpy(buf1, sizeof(buf1), total, buf1_len);
  3288. buf1[buf1_len] = 0;
  3289. buf2_len = j - i;
  3290. strlncpy(buf2, sizeof(buf1), total+i, buf2_len);
  3291. buf2[buf2_len] = 0;
  3292. buf3_len = total_len - j;
  3293. strlncpy(buf3, sizeof(buf1), total+j, buf3_len);
  3294. buf3[buf3_len] = 0;
  3295. read = parse(buf1, buf1_len);
  3296. if (parser->upgrade) goto test;
  3297. if (read != buf1_len) {
  3298. print_error(buf1, read);
  3299. goto error;
  3300. }
  3301. read += parse(buf2, buf2_len);
  3302. if (parser->upgrade) goto test;
  3303. if (read != buf1_len + buf2_len) {
  3304. print_error(buf2, read);
  3305. goto error;
  3306. }
  3307. read += parse(buf3, buf3_len);
  3308. if (parser->upgrade) goto test;
  3309. if (read != buf1_len + buf2_len + buf3_len) {
  3310. print_error(buf3, read);
  3311. goto error;
  3312. }
  3313. parse(NULL, 0);
  3314. test:
  3315. if (parser->upgrade) {
  3316. upgrade_message_fix(total, read, 3, r1, r2, r3);
  3317. }
  3318. if (message_count != num_messages) {
  3319. fprintf(stderr, "\n\nParser didn't see %d messages only %d\n",
  3320. message_count, num_messages);
  3321. goto error;
  3322. }
  3323. if (!message_eq(0, r1)) {
  3324. fprintf(stderr, "\n\nError matching messages[0] in test_scan.\n");
  3325. goto error;
  3326. }
  3327. if (message_count > 1 && !message_eq(1, r2)) {
  3328. fprintf(stderr, "\n\nError matching messages[1] in test_scan.\n");
  3329. goto error;
  3330. }
  3331. if (message_count > 2 && !message_eq(2, r3)) {
  3332. fprintf(stderr, "\n\nError matching messages[2] in test_scan.\n");
  3333. goto error;
  3334. }
  3335. parser_free();
  3336. }
  3337. }
  3338. }
  3339. puts("\b\b\b\b100%");
  3340. return;
  3341. error:
  3342. fprintf(stderr, "i=%d j=%d\n", i, j);
  3343. fprintf(stderr, "buf1 (%u) %s\n\n", (unsigned int)buf1_len, buf1);
  3344. fprintf(stderr, "buf2 (%u) %s\n\n", (unsigned int)buf2_len , buf2);
  3345. fprintf(stderr, "buf3 (%u) %s\n", (unsigned int)buf3_len, buf3);
  3346. abort();
  3347. }
  3348. // user required to free the result
  3349. // string terminated by \0
  3350. char *
  3351. create_large_chunked_message (int body_size_in_kb, const char* headers)
  3352. {
  3353. int i;
  3354. size_t wrote = 0;
  3355. size_t headers_len = strlen(headers);
  3356. size_t bufsize = headers_len + (5+1024+2)*body_size_in_kb + 6;
  3357. char * buf = malloc(bufsize);
  3358. memcpy(buf, headers, headers_len);
  3359. wrote += headers_len;
  3360. for (i = 0; i < body_size_in_kb; i++) {
  3361. // write 1kb chunk into the body.
  3362. memcpy(buf + wrote, "400\r\n", 5);
  3363. wrote += 5;
  3364. memset(buf + wrote, 'C', 1024);
  3365. wrote += 1024;
  3366. strcpy(buf + wrote, "\r\n");
  3367. wrote += 2;
  3368. }
  3369. memcpy(buf + wrote, "0\r\n\r\n", 6);
  3370. wrote += 6;
  3371. assert(wrote == bufsize);
  3372. return buf;
  3373. }
  3374. /* Verify that we can pause parsing at any of the bytes in the
  3375. * message and still get the result that we're expecting. */
  3376. void
  3377. test_message_pause (const struct message *msg)
  3378. {
  3379. char *buf = (char*) msg->raw;
  3380. size_t buflen = strlen(msg->raw);
  3381. size_t nread;
  3382. parser_init(msg->type);
  3383. do {
  3384. nread = parse_pause(buf, buflen);
  3385. // We can only set the upgrade buffer once we've gotten our message
  3386. // completion callback.
  3387. if (messages[0].message_complete_cb_called &&
  3388. msg->upgrade &&
  3389. parser->upgrade) {
  3390. messages[0].upgrade = buf + nread;
  3391. goto test;
  3392. }
  3393. if (nread < buflen) {
  3394. // Not much do to if we failed a strict-mode check
  3395. if (HTTP_PARSER_ERRNO(parser) == HPE_STRICT) {
  3396. parser_free();
  3397. return;
  3398. }
  3399. assert (HTTP_PARSER_ERRNO(parser) == HPE_PAUSED);
  3400. }
  3401. buf += nread;
  3402. buflen -= nread;
  3403. http_parser_pause(parser, 0);
  3404. } while (buflen > 0);
  3405. nread = parse_pause(NULL, 0);
  3406. assert (nread == 0);
  3407. test:
  3408. if (num_messages != 1) {
  3409. printf("\n*** num_messages != 1 after testing '%s' ***\n\n", msg->name);
  3410. abort();
  3411. }
  3412. if(!message_eq(0, msg)) abort();
  3413. parser_free();
  3414. }
  3415. int
  3416. main (void)
  3417. {
  3418. parser = NULL;
  3419. int i, j, k;
  3420. int request_count;
  3421. int response_count;
  3422. unsigned long version;
  3423. unsigned major;
  3424. unsigned minor;
  3425. unsigned patch;
  3426. version = http_parser_version();
  3427. major = (version >> 16) & 255;
  3428. minor = (version >> 8) & 255;
  3429. patch = version & 255;
  3430. printf("http_parser v%u.%u.%u (0x%06lx)\n", major, minor, patch, version);
  3431. printf("sizeof(http_parser) = %u\n", (unsigned int)sizeof(http_parser));
  3432. for (request_count = 0; requests[request_count].name; request_count++);
  3433. for (response_count = 0; responses[response_count].name; response_count++);
  3434. //// API
  3435. test_preserve_data();
  3436. test_parse_url();
  3437. test_method_str();
  3438. //// NREAD
  3439. test_header_nread_value();
  3440. //// OVERFLOW CONDITIONS
  3441. test_header_overflow_error(HTTP_REQUEST);
  3442. test_no_overflow_long_body(HTTP_REQUEST, 1000);
  3443. test_no_overflow_long_body(HTTP_REQUEST, 100000);
  3444. test_header_overflow_error(HTTP_RESPONSE);
  3445. test_no_overflow_long_body(HTTP_RESPONSE, 1000);
  3446. test_no_overflow_long_body(HTTP_RESPONSE, 100000);
  3447. test_header_content_length_overflow_error();
  3448. test_chunk_content_length_overflow_error();
  3449. //// HEADER FIELD CONDITIONS
  3450. test_double_content_length_error(HTTP_REQUEST);
  3451. test_chunked_content_length_error(HTTP_REQUEST);
  3452. test_header_cr_no_lf_error(HTTP_REQUEST);
  3453. test_invalid_header_field_token_error(HTTP_REQUEST);
  3454. test_invalid_header_field_content_error(HTTP_REQUEST);
  3455. test_double_content_length_error(HTTP_RESPONSE);
  3456. test_chunked_content_length_error(HTTP_RESPONSE);
  3457. test_header_cr_no_lf_error(HTTP_RESPONSE);
  3458. test_invalid_header_field_token_error(HTTP_RESPONSE);
  3459. test_invalid_header_field_content_error(HTTP_RESPONSE);
  3460. //// RESPONSES
  3461. for (i = 0; i < response_count; i++) {
  3462. test_message(&responses[i]);
  3463. }
  3464. for (i = 0; i < response_count; i++) {
  3465. test_message_pause(&responses[i]);
  3466. }
  3467. for (i = 0; i < response_count; i++) {
  3468. if (!responses[i].should_keep_alive) continue;
  3469. for (j = 0; j < response_count; j++) {
  3470. if (!responses[j].should_keep_alive) continue;
  3471. for (k = 0; k < response_count; k++) {
  3472. test_multiple3(&responses[i], &responses[j], &responses[k]);
  3473. }
  3474. }
  3475. }
  3476. test_message_count_body(&responses[NO_HEADERS_NO_BODY_404]);
  3477. test_message_count_body(&responses[TRAILING_SPACE_ON_CHUNKED_BODY]);
  3478. // test very large chunked response
  3479. {
  3480. char * msg = create_large_chunked_message(31337,
  3481. "HTTP/1.0 200 OK\r\n"
  3482. "Transfer-Encoding: chunked\r\n"
  3483. "Content-Type: text/plain\r\n"
  3484. "\r\n");
  3485. struct message large_chunked =
  3486. {.name= "large chunked"
  3487. ,.type= HTTP_RESPONSE
  3488. ,.raw= msg
  3489. ,.should_keep_alive= FALSE
  3490. ,.message_complete_on_eof= FALSE
  3491. ,.http_major= 1
  3492. ,.http_minor= 0
  3493. ,.status_code= 200
  3494. ,.response_status= "OK"
  3495. ,.num_headers= 2
  3496. ,.headers=
  3497. { { "Transfer-Encoding", "chunked" }
  3498. , { "Content-Type", "text/plain" }
  3499. }
  3500. ,.body_size= 31337*1024
  3501. ,.num_chunks_complete= 31338
  3502. };
  3503. for (i = 0; i < MAX_CHUNKS; i++) {
  3504. large_chunked.chunk_lengths[i] = 1024;
  3505. }
  3506. test_message_count_body(&large_chunked);
  3507. free(msg);
  3508. }
  3509. printf("response scan 1/2 ");
  3510. test_scan( &responses[TRAILING_SPACE_ON_CHUNKED_BODY]
  3511. , &responses[NO_BODY_HTTP10_KA_204]
  3512. , &responses[NO_REASON_PHRASE]
  3513. );
  3514. printf("response scan 2/2 ");
  3515. test_scan( &responses[BONJOUR_MADAME_FR]
  3516. , &responses[UNDERSTORE_HEADER_KEY]
  3517. , &responses[NO_CARRIAGE_RET]
  3518. );
  3519. puts("responses okay");
  3520. /// REQUESTS
  3521. test_simple("GET / HTP/1.1\r\n\r\n", HPE_INVALID_VERSION);
  3522. // Extended characters - see nodejs/test/parallel/test-http-headers-obstext.js
  3523. test_simple("GET / HTTP/1.1\r\n"
  3524. "Test: Düsseldorf\r\n",
  3525. HPE_OK);
  3526. // Well-formed but incomplete
  3527. test_simple("GET / HTTP/1.1\r\n"
  3528. "Content-Type: text/plain\r\n"
  3529. "Content-Length: 6\r\n"
  3530. "\r\n"
  3531. "fooba",
  3532. HPE_OK);
  3533. static const char *all_methods[] = {
  3534. "DELETE",
  3535. "GET",
  3536. "HEAD",
  3537. "POST",
  3538. "PUT",
  3539. //"CONNECT", //CONNECT can't be tested like other methods, it's a tunnel
  3540. "OPTIONS",
  3541. "TRACE",
  3542. "COPY",
  3543. "LOCK",
  3544. "MKCOL",
  3545. "MOVE",
  3546. "PROPFIND",
  3547. "PROPPATCH",
  3548. "SEARCH",
  3549. "UNLOCK",
  3550. "BIND",
  3551. "REBIND",
  3552. "UNBIND",
  3553. "ACL",
  3554. "REPORT",
  3555. "MKACTIVITY",
  3556. "CHECKOUT",
  3557. "MERGE",
  3558. "M-SEARCH",
  3559. "NOTIFY",
  3560. "SUBSCRIBE",
  3561. "UNSUBSCRIBE",
  3562. "PATCH",
  3563. "PURGE",
  3564. "MKCALENDAR",
  3565. "LINK",
  3566. "UNLINK",
  3567. 0 };
  3568. const char **this_method;
  3569. for (this_method = all_methods; *this_method; this_method++) {
  3570. char buf[200];
  3571. sprintf(buf, "%s / HTTP/1.1\r\n\r\n", *this_method);
  3572. test_simple(buf, HPE_OK);
  3573. }
  3574. static const char *bad_methods[] = {
  3575. "ASDF",
  3576. "C******",
  3577. "COLA",
  3578. "GEM",
  3579. "GETA",
  3580. "M****",
  3581. "MKCOLA",
  3582. "PROPPATCHA",
  3583. "PUN",
  3584. "PX",
  3585. "SA",
  3586. "hello world",
  3587. 0 };
  3588. for (this_method = bad_methods; *this_method; this_method++) {
  3589. char buf[200];
  3590. sprintf(buf, "%s / HTTP/1.1\r\n\r\n", *this_method);
  3591. test_simple(buf, HPE_INVALID_METHOD);
  3592. }
  3593. // illegal header field name line folding
  3594. test_simple("GET / HTTP/1.1\r\n"
  3595. "name\r\n"
  3596. " : value\r\n"
  3597. "\r\n",
  3598. HPE_INVALID_HEADER_TOKEN);
  3599. const char *dumbfuck2 =
  3600. "GET / HTTP/1.1\r\n"
  3601. "X-SSL-Bullshit: -----BEGIN CERTIFICATE-----\r\n"
  3602. "\tMIIFbTCCBFWgAwIBAgICH4cwDQYJKoZIhvcNAQEFBQAwcDELMAkGA1UEBhMCVUsx\r\n"
  3603. "\tETAPBgNVBAoTCGVTY2llbmNlMRIwEAYDVQQLEwlBdXRob3JpdHkxCzAJBgNVBAMT\r\n"
  3604. "\tAkNBMS0wKwYJKoZIhvcNAQkBFh5jYS1vcGVyYXRvckBncmlkLXN1cHBvcnQuYWMu\r\n"
  3605. "\tdWswHhcNMDYwNzI3MTQxMzI4WhcNMDcwNzI3MTQxMzI4WjBbMQswCQYDVQQGEwJV\r\n"
  3606. "\tSzERMA8GA1UEChMIZVNjaWVuY2UxEzARBgNVBAsTCk1hbmNoZXN0ZXIxCzAJBgNV\r\n"
  3607. "\tBAcTmrsogriqMWLAk1DMRcwFQYDVQQDEw5taWNoYWVsIHBhcmQYJKoZIhvcNAQEB\r\n"
  3608. "\tBQADggEPADCCAQoCggEBANPEQBgl1IaKdSS1TbhF3hEXSl72G9J+WC/1R64fAcEF\r\n"
  3609. "\tW51rEyFYiIeZGx/BVzwXbeBoNUK41OK65sxGuflMo5gLflbwJtHBRIEKAfVVp3YR\r\n"
  3610. "\tgW7cMA/s/XKgL1GEC7rQw8lIZT8RApukCGqOVHSi/F1SiFlPDxuDfmdiNzL31+sL\r\n"
  3611. "\t0iwHDdNkGjy5pyBSB8Y79dsSJtCW/iaLB0/n8Sj7HgvvZJ7x0fr+RQjYOUUfrePP\r\n"
  3612. "\tu2MSpFyf+9BbC/aXgaZuiCvSR+8Snv3xApQY+fULK/xY8h8Ua51iXoQ5jrgu2SqR\r\n"
  3613. "\twgA7BUi3G8LFzMBl8FRCDYGUDy7M6QaHXx1ZWIPWNKsCAwEAAaOCAiQwggIgMAwG\r\n"
  3614. "\tA1UdEwEB/wQCMAAwEQYJYIZIAYb4QgHTTPAQDAgWgMA4GA1UdDwEB/wQEAwID6DAs\r\n"
  3615. "\tBglghkgBhvhCAQ0EHxYdVUsgZS1TY2llbmNlIFVzZXIgQ2VydGlmaWNhdGUwHQYD\r\n"
  3616. "\tVR0OBBYEFDTt/sf9PeMaZDHkUIldrDYMNTBZMIGaBgNVHSMEgZIwgY+AFAI4qxGj\r\n"
  3617. "\tloCLDdMVKwiljjDastqooXSkcjBwMQswCQYDVQQGEwJVSzERMA8GA1UEChMIZVNj\r\n"
  3618. "\taWVuY2UxEjAQBgNVBAsTCUF1dGhvcml0eTELMAkGA1UEAxMCQ0ExLTArBgkqhkiG\r\n"
  3619. "\t9w0BCQEWHmNhLW9wZXJhdG9yQGdyaWQtc3VwcG9ydC5hYy51a4IBADApBgNVHRIE\r\n"
  3620. "\tIjAggR5jYS1vcGVyYXRvckBncmlkLXN1cHBvcnQuYWMudWswGQYDVR0gBBIwEDAO\r\n"
  3621. "\tBgwrBgEEAdkvAQEBAQYwPQYJYIZIAYb4QgEEBDAWLmh0dHA6Ly9jYS5ncmlkLXN1\r\n"
  3622. "\tcHBvcnQuYWMudmT4sopwqlBWsvcHViL2NybC9jYWNybC5jcmwwPQYJYIZIAYb4QgEDBDAWLmh0\r\n"
  3623. "\tdHA6Ly9jYS5ncmlkLXN1cHBvcnQuYWMudWsvcHViL2NybC9jYWNybC5jcmwwPwYD\r\n"
  3624. "\tVR0fBDgwNjA0oDKgMIYuaHR0cDovL2NhLmdyaWQt5hYy51ay9wdWIv\r\n"
  3625. "\tY3JsL2NhY3JsLmNybDANBgkqhkiG9w0BAQUFAAOCAQEAS/U4iiooBENGW/Hwmmd3\r\n"
  3626. "\tXCy6Zrt08YjKCzGNjorT98g8uGsqYjSxv/hmi0qlnlHs+k/3Iobc3LjS5AMYr5L8\r\n"
  3627. "\tUO7OSkgFFlLHQyC9JzPfmLCAugvzEbyv4Olnsr8hbxF1MbKZoQxUZtMVu29wjfXk\r\n"
  3628. "\thTeApBv7eaKCWpSp7MCbvgzm74izKhu3vlDk9w6qVrxePfGgpKPqfHiOoGhFnbTK\r\n"
  3629. "\twTC6o2xq5y0qZ03JonF7OJspEd3I5zKY3E+ov7/ZhW6DqT8UFvsAdjvQbXyhV8Eu\r\n"
  3630. "\tYhixw1aKEPzNjNowuIseVogKOLXxWI5vAi5HgXdS0/ES5gDGsABo4fqovUKlgop3\r\n"
  3631. "\tRA==\r\n"
  3632. "\t-----END CERTIFICATE-----\r\n"
  3633. "\r\n";
  3634. test_simple(dumbfuck2, HPE_OK);
  3635. const char *corrupted_connection =
  3636. "GET / HTTP/1.1\r\n"
  3637. "Host: www.example.com\r\n"
  3638. "Connection\r\033\065\325eep-Alive\r\n"
  3639. "Accept-Encoding: gzip\r\n"
  3640. "\r\n";
  3641. test_simple(corrupted_connection, HPE_INVALID_HEADER_TOKEN);
  3642. const char *corrupted_header_name =
  3643. "GET / HTTP/1.1\r\n"
  3644. "Host: www.example.com\r\n"
  3645. "X-Some-Header\r\033\065\325eep-Alive\r\n"
  3646. "Accept-Encoding: gzip\r\n"
  3647. "\r\n";
  3648. test_simple(corrupted_header_name, HPE_INVALID_HEADER_TOKEN);
  3649. #if 0
  3650. // NOTE(Wed Nov 18 11:57:27 CET 2009) this seems okay. we just read body
  3651. // until EOF.
  3652. //
  3653. // no content-length
  3654. // error if there is a body without content length
  3655. const char *bad_get_no_headers_no_body = "GET /bad_get_no_headers_no_body/world HTTP/1.1\r\n"
  3656. "Accept: */*\r\n"
  3657. "\r\n"
  3658. "HELLO";
  3659. test_simple(bad_get_no_headers_no_body, 0);
  3660. #endif
  3661. /* TODO sending junk and large headers gets rejected */
  3662. /* check to make sure our predefined requests are okay */
  3663. for (i = 0; requests[i].name; i++) {
  3664. test_message(&requests[i]);
  3665. }
  3666. for (i = 0; i < request_count; i++) {
  3667. test_message_pause(&requests[i]);
  3668. }
  3669. for (i = 0; i < request_count; i++) {
  3670. if (!requests[i].should_keep_alive) continue;
  3671. for (j = 0; j < request_count; j++) {
  3672. if (!requests[j].should_keep_alive) continue;
  3673. for (k = 0; k < request_count; k++) {
  3674. test_multiple3(&requests[i], &requests[j], &requests[k]);
  3675. }
  3676. }
  3677. }
  3678. printf("request scan 1/4 ");
  3679. test_scan( &requests[GET_NO_HEADERS_NO_BODY]
  3680. , &requests[GET_ONE_HEADER_NO_BODY]
  3681. , &requests[GET_NO_HEADERS_NO_BODY]
  3682. );
  3683. printf("request scan 2/4 ");
  3684. test_scan( &requests[POST_CHUNKED_ALL_YOUR_BASE]
  3685. , &requests[POST_IDENTITY_BODY_WORLD]
  3686. , &requests[GET_FUNKY_CONTENT_LENGTH]
  3687. );
  3688. printf("request scan 3/4 ");
  3689. test_scan( &requests[TWO_CHUNKS_MULT_ZERO_END]
  3690. , &requests[CHUNKED_W_TRAILING_HEADERS]
  3691. , &requests[CHUNKED_W_BULLSHIT_AFTER_LENGTH]
  3692. );
  3693. printf("request scan 4/4 ");
  3694. test_scan( &requests[QUERY_URL_WITH_QUESTION_MARK_GET]
  3695. , &requests[PREFIX_NEWLINE_GET ]
  3696. , &requests[CONNECT_REQUEST]
  3697. );
  3698. puts("requests okay");
  3699. return 0;
  3700. }