test.c 110 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226
  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. #define CONTENT_LENGTH_X 21
  1669. , {.name= "Content-Length-X"
  1670. ,.type= HTTP_RESPONSE
  1671. ,.raw= "HTTP/1.1 200 OK\r\n"
  1672. "Content-Length-X: 0\r\n"
  1673. "Transfer-Encoding: chunked\r\n"
  1674. "\r\n"
  1675. "2\r\n"
  1676. "OK\r\n"
  1677. "0\r\n"
  1678. "\r\n"
  1679. ,.should_keep_alive= TRUE
  1680. ,.message_complete_on_eof= FALSE
  1681. ,.http_major= 1
  1682. ,.http_minor= 1
  1683. ,.status_code= 200
  1684. ,.response_status= "OK"
  1685. ,.num_headers= 2
  1686. ,.headers= { { "Content-Length-X", "0" }
  1687. , { "Transfer-Encoding", "chunked" }
  1688. }
  1689. ,.body= "OK"
  1690. ,.num_chunks_complete= 2
  1691. ,.chunk_lengths= { 2 }
  1692. }
  1693. , {.name= NULL } /* sentinel */
  1694. };
  1695. /* strnlen() is a POSIX.2008 addition. Can't rely on it being available so
  1696. * define it ourselves.
  1697. */
  1698. size_t
  1699. strnlen(const char *s, size_t maxlen)
  1700. {
  1701. const char *p;
  1702. p = memchr(s, '\0', maxlen);
  1703. if (p == NULL)
  1704. return maxlen;
  1705. return p - s;
  1706. }
  1707. size_t
  1708. strlncat(char *dst, size_t len, const char *src, size_t n)
  1709. {
  1710. size_t slen;
  1711. size_t dlen;
  1712. size_t rlen;
  1713. size_t ncpy;
  1714. slen = strnlen(src, n);
  1715. dlen = strnlen(dst, len);
  1716. if (dlen < len) {
  1717. rlen = len - dlen;
  1718. ncpy = slen < rlen ? slen : (rlen - 1);
  1719. memcpy(dst + dlen, src, ncpy);
  1720. dst[dlen + ncpy] = '\0';
  1721. }
  1722. assert(len > slen + dlen);
  1723. return slen + dlen;
  1724. }
  1725. size_t
  1726. strlcat(char *dst, const char *src, size_t len)
  1727. {
  1728. return strlncat(dst, len, src, (size_t) -1);
  1729. }
  1730. size_t
  1731. strlncpy(char *dst, size_t len, const char *src, size_t n)
  1732. {
  1733. size_t slen;
  1734. size_t ncpy;
  1735. slen = strnlen(src, n);
  1736. if (len > 0) {
  1737. ncpy = slen < len ? slen : (len - 1);
  1738. memcpy(dst, src, ncpy);
  1739. dst[ncpy] = '\0';
  1740. }
  1741. assert(len > slen);
  1742. return slen;
  1743. }
  1744. size_t
  1745. strlcpy(char *dst, const char *src, size_t len)
  1746. {
  1747. return strlncpy(dst, len, src, (size_t) -1);
  1748. }
  1749. int
  1750. request_url_cb (http_parser *p, const char *buf, size_t len)
  1751. {
  1752. assert(p == parser);
  1753. strlncat(messages[num_messages].request_url,
  1754. sizeof(messages[num_messages].request_url),
  1755. buf,
  1756. len);
  1757. return 0;
  1758. }
  1759. int
  1760. header_field_cb (http_parser *p, const char *buf, size_t len)
  1761. {
  1762. assert(p == parser);
  1763. struct message *m = &messages[num_messages];
  1764. if (m->last_header_element != FIELD)
  1765. m->num_headers++;
  1766. strlncat(m->headers[m->num_headers-1][0],
  1767. sizeof(m->headers[m->num_headers-1][0]),
  1768. buf,
  1769. len);
  1770. m->last_header_element = FIELD;
  1771. return 0;
  1772. }
  1773. int
  1774. header_value_cb (http_parser *p, const char *buf, size_t len)
  1775. {
  1776. assert(p == parser);
  1777. struct message *m = &messages[num_messages];
  1778. strlncat(m->headers[m->num_headers-1][1],
  1779. sizeof(m->headers[m->num_headers-1][1]),
  1780. buf,
  1781. len);
  1782. m->last_header_element = VALUE;
  1783. return 0;
  1784. }
  1785. void
  1786. check_body_is_final (const http_parser *p)
  1787. {
  1788. if (messages[num_messages].body_is_final) {
  1789. fprintf(stderr, "\n\n *** Error http_body_is_final() should return 1 "
  1790. "on last on_body callback call "
  1791. "but it doesn't! ***\n\n");
  1792. assert(0);
  1793. abort();
  1794. }
  1795. messages[num_messages].body_is_final = http_body_is_final(p);
  1796. }
  1797. int
  1798. body_cb (http_parser *p, const char *buf, size_t len)
  1799. {
  1800. assert(p == parser);
  1801. strlncat(messages[num_messages].body,
  1802. sizeof(messages[num_messages].body),
  1803. buf,
  1804. len);
  1805. messages[num_messages].body_size += len;
  1806. check_body_is_final(p);
  1807. // printf("body_cb: '%s'\n", requests[num_messages].body);
  1808. return 0;
  1809. }
  1810. int
  1811. count_body_cb (http_parser *p, const char *buf, size_t len)
  1812. {
  1813. assert(p == parser);
  1814. assert(buf);
  1815. messages[num_messages].body_size += len;
  1816. check_body_is_final(p);
  1817. return 0;
  1818. }
  1819. int
  1820. message_begin_cb (http_parser *p)
  1821. {
  1822. assert(p == parser);
  1823. messages[num_messages].message_begin_cb_called = TRUE;
  1824. return 0;
  1825. }
  1826. int
  1827. headers_complete_cb (http_parser *p)
  1828. {
  1829. assert(p == parser);
  1830. messages[num_messages].method = parser->method;
  1831. messages[num_messages].status_code = parser->status_code;
  1832. messages[num_messages].http_major = parser->http_major;
  1833. messages[num_messages].http_minor = parser->http_minor;
  1834. messages[num_messages].headers_complete_cb_called = TRUE;
  1835. messages[num_messages].should_keep_alive = http_should_keep_alive(parser);
  1836. return 0;
  1837. }
  1838. int
  1839. message_complete_cb (http_parser *p)
  1840. {
  1841. assert(p == parser);
  1842. if (messages[num_messages].should_keep_alive != http_should_keep_alive(parser))
  1843. {
  1844. fprintf(stderr, "\n\n *** Error http_should_keep_alive() should have same "
  1845. "value in both on_message_complete and on_headers_complete "
  1846. "but it doesn't! ***\n\n");
  1847. assert(0);
  1848. abort();
  1849. }
  1850. if (messages[num_messages].body_size &&
  1851. http_body_is_final(p) &&
  1852. !messages[num_messages].body_is_final)
  1853. {
  1854. fprintf(stderr, "\n\n *** Error http_body_is_final() should return 1 "
  1855. "on last on_body callback call "
  1856. "but it doesn't! ***\n\n");
  1857. assert(0);
  1858. abort();
  1859. }
  1860. messages[num_messages].message_complete_cb_called = TRUE;
  1861. messages[num_messages].message_complete_on_eof = currently_parsing_eof;
  1862. num_messages++;
  1863. return 0;
  1864. }
  1865. int
  1866. response_status_cb (http_parser *p, const char *buf, size_t len)
  1867. {
  1868. assert(p == parser);
  1869. strlncat(messages[num_messages].response_status,
  1870. sizeof(messages[num_messages].response_status),
  1871. buf,
  1872. len);
  1873. return 0;
  1874. }
  1875. int
  1876. chunk_header_cb (http_parser *p)
  1877. {
  1878. assert(p == parser);
  1879. int chunk_idx = messages[num_messages].num_chunks;
  1880. messages[num_messages].num_chunks++;
  1881. if (chunk_idx < MAX_CHUNKS) {
  1882. messages[num_messages].chunk_lengths[chunk_idx] = p->content_length;
  1883. }
  1884. return 0;
  1885. }
  1886. int
  1887. chunk_complete_cb (http_parser *p)
  1888. {
  1889. assert(p == parser);
  1890. /* Here we want to verify that each chunk_header_cb is matched by a
  1891. * chunk_complete_cb, so not only should the total number of calls to
  1892. * both callbacks be the same, but they also should be interleaved
  1893. * properly */
  1894. assert(messages[num_messages].num_chunks ==
  1895. messages[num_messages].num_chunks_complete + 1);
  1896. messages[num_messages].num_chunks_complete++;
  1897. return 0;
  1898. }
  1899. /* These dontcall_* callbacks exist so that we can verify that when we're
  1900. * paused, no additional callbacks are invoked */
  1901. int
  1902. dontcall_message_begin_cb (http_parser *p)
  1903. {
  1904. if (p) { } // gcc
  1905. fprintf(stderr, "\n\n*** on_message_begin() called on paused parser ***\n\n");
  1906. abort();
  1907. }
  1908. int
  1909. dontcall_header_field_cb (http_parser *p, const char *buf, size_t len)
  1910. {
  1911. if (p || buf || len) { } // gcc
  1912. fprintf(stderr, "\n\n*** on_header_field() called on paused parser ***\n\n");
  1913. abort();
  1914. }
  1915. int
  1916. dontcall_header_value_cb (http_parser *p, const char *buf, size_t len)
  1917. {
  1918. if (p || buf || len) { } // gcc
  1919. fprintf(stderr, "\n\n*** on_header_value() called on paused parser ***\n\n");
  1920. abort();
  1921. }
  1922. int
  1923. dontcall_request_url_cb (http_parser *p, const char *buf, size_t len)
  1924. {
  1925. if (p || buf || len) { } // gcc
  1926. fprintf(stderr, "\n\n*** on_request_url() called on paused parser ***\n\n");
  1927. abort();
  1928. }
  1929. int
  1930. dontcall_body_cb (http_parser *p, const char *buf, size_t len)
  1931. {
  1932. if (p || buf || len) { } // gcc
  1933. fprintf(stderr, "\n\n*** on_body_cb() called on paused parser ***\n\n");
  1934. abort();
  1935. }
  1936. int
  1937. dontcall_headers_complete_cb (http_parser *p)
  1938. {
  1939. if (p) { } // gcc
  1940. fprintf(stderr, "\n\n*** on_headers_complete() called on paused "
  1941. "parser ***\n\n");
  1942. abort();
  1943. }
  1944. int
  1945. dontcall_message_complete_cb (http_parser *p)
  1946. {
  1947. if (p) { } // gcc
  1948. fprintf(stderr, "\n\n*** on_message_complete() called on paused "
  1949. "parser ***\n\n");
  1950. abort();
  1951. }
  1952. int
  1953. dontcall_response_status_cb (http_parser *p, const char *buf, size_t len)
  1954. {
  1955. if (p || buf || len) { } // gcc
  1956. fprintf(stderr, "\n\n*** on_status() called on paused parser ***\n\n");
  1957. abort();
  1958. }
  1959. int
  1960. dontcall_chunk_header_cb (http_parser *p)
  1961. {
  1962. if (p) { } // gcc
  1963. fprintf(stderr, "\n\n*** on_chunk_header() called on paused parser ***\n\n");
  1964. exit(1);
  1965. }
  1966. int
  1967. dontcall_chunk_complete_cb (http_parser *p)
  1968. {
  1969. if (p) { } // gcc
  1970. fprintf(stderr, "\n\n*** on_chunk_complete() "
  1971. "called on paused parser ***\n\n");
  1972. exit(1);
  1973. }
  1974. static http_parser_settings settings_dontcall =
  1975. {.on_message_begin = dontcall_message_begin_cb
  1976. ,.on_header_field = dontcall_header_field_cb
  1977. ,.on_header_value = dontcall_header_value_cb
  1978. ,.on_url = dontcall_request_url_cb
  1979. ,.on_status = dontcall_response_status_cb
  1980. ,.on_body = dontcall_body_cb
  1981. ,.on_headers_complete = dontcall_headers_complete_cb
  1982. ,.on_message_complete = dontcall_message_complete_cb
  1983. ,.on_chunk_header = dontcall_chunk_header_cb
  1984. ,.on_chunk_complete = dontcall_chunk_complete_cb
  1985. };
  1986. /* These pause_* callbacks always pause the parser and just invoke the regular
  1987. * callback that tracks content. Before returning, we overwrite the parser
  1988. * settings to point to the _dontcall variety so that we can verify that
  1989. * the pause actually did, you know, pause. */
  1990. int
  1991. pause_message_begin_cb (http_parser *p)
  1992. {
  1993. http_parser_pause(p, 1);
  1994. *current_pause_parser = settings_dontcall;
  1995. return message_begin_cb(p);
  1996. }
  1997. int
  1998. pause_header_field_cb (http_parser *p, const char *buf, size_t len)
  1999. {
  2000. http_parser_pause(p, 1);
  2001. *current_pause_parser = settings_dontcall;
  2002. return header_field_cb(p, buf, len);
  2003. }
  2004. int
  2005. pause_header_value_cb (http_parser *p, const char *buf, size_t len)
  2006. {
  2007. http_parser_pause(p, 1);
  2008. *current_pause_parser = settings_dontcall;
  2009. return header_value_cb(p, buf, len);
  2010. }
  2011. int
  2012. pause_request_url_cb (http_parser *p, const char *buf, size_t len)
  2013. {
  2014. http_parser_pause(p, 1);
  2015. *current_pause_parser = settings_dontcall;
  2016. return request_url_cb(p, buf, len);
  2017. }
  2018. int
  2019. pause_body_cb (http_parser *p, const char *buf, size_t len)
  2020. {
  2021. http_parser_pause(p, 1);
  2022. *current_pause_parser = settings_dontcall;
  2023. return body_cb(p, buf, len);
  2024. }
  2025. int
  2026. pause_headers_complete_cb (http_parser *p)
  2027. {
  2028. http_parser_pause(p, 1);
  2029. *current_pause_parser = settings_dontcall;
  2030. return headers_complete_cb(p);
  2031. }
  2032. int
  2033. pause_message_complete_cb (http_parser *p)
  2034. {
  2035. http_parser_pause(p, 1);
  2036. *current_pause_parser = settings_dontcall;
  2037. return message_complete_cb(p);
  2038. }
  2039. int
  2040. pause_response_status_cb (http_parser *p, const char *buf, size_t len)
  2041. {
  2042. http_parser_pause(p, 1);
  2043. *current_pause_parser = settings_dontcall;
  2044. return response_status_cb(p, buf, len);
  2045. }
  2046. int
  2047. pause_chunk_header_cb (http_parser *p)
  2048. {
  2049. http_parser_pause(p, 1);
  2050. *current_pause_parser = settings_dontcall;
  2051. return chunk_header_cb(p);
  2052. }
  2053. int
  2054. pause_chunk_complete_cb (http_parser *p)
  2055. {
  2056. http_parser_pause(p, 1);
  2057. *current_pause_parser = settings_dontcall;
  2058. return chunk_complete_cb(p);
  2059. }
  2060. int
  2061. connect_headers_complete_cb (http_parser *p)
  2062. {
  2063. headers_complete_cb(p);
  2064. return 1;
  2065. }
  2066. int
  2067. connect_message_complete_cb (http_parser *p)
  2068. {
  2069. messages[num_messages].should_keep_alive = http_should_keep_alive(parser);
  2070. return message_complete_cb(p);
  2071. }
  2072. static http_parser_settings settings_pause =
  2073. {.on_message_begin = pause_message_begin_cb
  2074. ,.on_header_field = pause_header_field_cb
  2075. ,.on_header_value = pause_header_value_cb
  2076. ,.on_url = pause_request_url_cb
  2077. ,.on_status = pause_response_status_cb
  2078. ,.on_body = pause_body_cb
  2079. ,.on_headers_complete = pause_headers_complete_cb
  2080. ,.on_message_complete = pause_message_complete_cb
  2081. ,.on_chunk_header = pause_chunk_header_cb
  2082. ,.on_chunk_complete = pause_chunk_complete_cb
  2083. };
  2084. static http_parser_settings settings =
  2085. {.on_message_begin = message_begin_cb
  2086. ,.on_header_field = header_field_cb
  2087. ,.on_header_value = header_value_cb
  2088. ,.on_url = request_url_cb
  2089. ,.on_status = response_status_cb
  2090. ,.on_body = body_cb
  2091. ,.on_headers_complete = headers_complete_cb
  2092. ,.on_message_complete = message_complete_cb
  2093. ,.on_chunk_header = chunk_header_cb
  2094. ,.on_chunk_complete = chunk_complete_cb
  2095. };
  2096. static http_parser_settings settings_count_body =
  2097. {.on_message_begin = message_begin_cb
  2098. ,.on_header_field = header_field_cb
  2099. ,.on_header_value = header_value_cb
  2100. ,.on_url = request_url_cb
  2101. ,.on_status = response_status_cb
  2102. ,.on_body = count_body_cb
  2103. ,.on_headers_complete = headers_complete_cb
  2104. ,.on_message_complete = message_complete_cb
  2105. ,.on_chunk_header = chunk_header_cb
  2106. ,.on_chunk_complete = chunk_complete_cb
  2107. };
  2108. static http_parser_settings settings_connect =
  2109. {.on_message_begin = message_begin_cb
  2110. ,.on_header_field = header_field_cb
  2111. ,.on_header_value = header_value_cb
  2112. ,.on_url = request_url_cb
  2113. ,.on_status = response_status_cb
  2114. ,.on_body = dontcall_body_cb
  2115. ,.on_headers_complete = connect_headers_complete_cb
  2116. ,.on_message_complete = connect_message_complete_cb
  2117. ,.on_chunk_header = chunk_header_cb
  2118. ,.on_chunk_complete = chunk_complete_cb
  2119. };
  2120. static http_parser_settings settings_null =
  2121. {.on_message_begin = 0
  2122. ,.on_header_field = 0
  2123. ,.on_header_value = 0
  2124. ,.on_url = 0
  2125. ,.on_status = 0
  2126. ,.on_body = 0
  2127. ,.on_headers_complete = 0
  2128. ,.on_message_complete = 0
  2129. ,.on_chunk_header = 0
  2130. ,.on_chunk_complete = 0
  2131. };
  2132. void
  2133. parser_init (enum http_parser_type type)
  2134. {
  2135. num_messages = 0;
  2136. assert(parser == NULL);
  2137. parser = malloc(sizeof(http_parser));
  2138. http_parser_init(parser, type);
  2139. memset(&messages, 0, sizeof messages);
  2140. }
  2141. void
  2142. parser_free ()
  2143. {
  2144. assert(parser);
  2145. free(parser);
  2146. parser = NULL;
  2147. }
  2148. size_t parse (const char *buf, size_t len)
  2149. {
  2150. size_t nparsed;
  2151. currently_parsing_eof = (len == 0);
  2152. nparsed = http_parser_execute(parser, &settings, buf, len);
  2153. return nparsed;
  2154. }
  2155. size_t parse_count_body (const char *buf, size_t len)
  2156. {
  2157. size_t nparsed;
  2158. currently_parsing_eof = (len == 0);
  2159. nparsed = http_parser_execute(parser, &settings_count_body, buf, len);
  2160. return nparsed;
  2161. }
  2162. size_t parse_pause (const char *buf, size_t len)
  2163. {
  2164. size_t nparsed;
  2165. http_parser_settings s = settings_pause;
  2166. currently_parsing_eof = (len == 0);
  2167. current_pause_parser = &s;
  2168. nparsed = http_parser_execute(parser, current_pause_parser, buf, len);
  2169. return nparsed;
  2170. }
  2171. size_t parse_connect (const char *buf, size_t len)
  2172. {
  2173. size_t nparsed;
  2174. currently_parsing_eof = (len == 0);
  2175. nparsed = http_parser_execute(parser, &settings_connect, buf, len);
  2176. return nparsed;
  2177. }
  2178. static inline int
  2179. check_str_eq (const struct message *m,
  2180. const char *prop,
  2181. const char *expected,
  2182. const char *found) {
  2183. if ((expected == NULL) != (found == NULL)) {
  2184. printf("\n*** Error: %s in '%s' ***\n\n", prop, m->name);
  2185. printf("expected %s\n", (expected == NULL) ? "NULL" : expected);
  2186. printf(" found %s\n", (found == NULL) ? "NULL" : found);
  2187. return 0;
  2188. }
  2189. if (expected != NULL && 0 != strcmp(expected, found)) {
  2190. printf("\n*** Error: %s in '%s' ***\n\n", prop, m->name);
  2191. printf("expected '%s'\n", expected);
  2192. printf(" found '%s'\n", found);
  2193. return 0;
  2194. }
  2195. return 1;
  2196. }
  2197. static inline int
  2198. check_num_eq (const struct message *m,
  2199. const char *prop,
  2200. int expected,
  2201. int found) {
  2202. if (expected != found) {
  2203. printf("\n*** Error: %s in '%s' ***\n\n", prop, m->name);
  2204. printf("expected %d\n", expected);
  2205. printf(" found %d\n", found);
  2206. return 0;
  2207. }
  2208. return 1;
  2209. }
  2210. #define MESSAGE_CHECK_STR_EQ(expected, found, prop) \
  2211. if (!check_str_eq(expected, #prop, expected->prop, found->prop)) return 0
  2212. #define MESSAGE_CHECK_NUM_EQ(expected, found, prop) \
  2213. if (!check_num_eq(expected, #prop, expected->prop, found->prop)) return 0
  2214. #define MESSAGE_CHECK_URL_EQ(u, expected, found, prop, fn) \
  2215. do { \
  2216. char ubuf[256]; \
  2217. \
  2218. if ((u)->field_set & (1 << (fn))) { \
  2219. memcpy(ubuf, (found)->request_url + (u)->field_data[(fn)].off, \
  2220. (u)->field_data[(fn)].len); \
  2221. ubuf[(u)->field_data[(fn)].len] = '\0'; \
  2222. } else { \
  2223. ubuf[0] = '\0'; \
  2224. } \
  2225. \
  2226. check_str_eq(expected, #prop, expected->prop, ubuf); \
  2227. } while(0)
  2228. int
  2229. message_eq (int index, int connect, const struct message *expected)
  2230. {
  2231. int i;
  2232. struct message *m = &messages[index];
  2233. MESSAGE_CHECK_NUM_EQ(expected, m, http_major);
  2234. MESSAGE_CHECK_NUM_EQ(expected, m, http_minor);
  2235. if (expected->type == HTTP_REQUEST) {
  2236. MESSAGE_CHECK_NUM_EQ(expected, m, method);
  2237. } else {
  2238. MESSAGE_CHECK_NUM_EQ(expected, m, status_code);
  2239. MESSAGE_CHECK_STR_EQ(expected, m, response_status);
  2240. }
  2241. if (!connect) {
  2242. MESSAGE_CHECK_NUM_EQ(expected, m, should_keep_alive);
  2243. MESSAGE_CHECK_NUM_EQ(expected, m, message_complete_on_eof);
  2244. }
  2245. assert(m->message_begin_cb_called);
  2246. assert(m->headers_complete_cb_called);
  2247. assert(m->message_complete_cb_called);
  2248. MESSAGE_CHECK_STR_EQ(expected, m, request_url);
  2249. /* Check URL components; we can't do this w/ CONNECT since it doesn't
  2250. * send us a well-formed URL.
  2251. */
  2252. if (*m->request_url && m->method != HTTP_CONNECT) {
  2253. struct http_parser_url u;
  2254. if (http_parser_parse_url(m->request_url, strlen(m->request_url), 0, &u)) {
  2255. fprintf(stderr, "\n\n*** failed to parse URL %s ***\n\n",
  2256. m->request_url);
  2257. abort();
  2258. }
  2259. if (expected->host) {
  2260. MESSAGE_CHECK_URL_EQ(&u, expected, m, host, UF_HOST);
  2261. }
  2262. if (expected->userinfo) {
  2263. MESSAGE_CHECK_URL_EQ(&u, expected, m, userinfo, UF_USERINFO);
  2264. }
  2265. m->port = (u.field_set & (1 << UF_PORT)) ?
  2266. u.port : 0;
  2267. MESSAGE_CHECK_URL_EQ(&u, expected, m, query_string, UF_QUERY);
  2268. MESSAGE_CHECK_URL_EQ(&u, expected, m, fragment, UF_FRAGMENT);
  2269. MESSAGE_CHECK_URL_EQ(&u, expected, m, request_path, UF_PATH);
  2270. MESSAGE_CHECK_NUM_EQ(expected, m, port);
  2271. }
  2272. if (connect) {
  2273. check_num_eq(m, "body_size", 0, m->body_size);
  2274. } else if (expected->body_size) {
  2275. MESSAGE_CHECK_NUM_EQ(expected, m, body_size);
  2276. } else {
  2277. MESSAGE_CHECK_STR_EQ(expected, m, body);
  2278. }
  2279. if (connect) {
  2280. check_num_eq(m, "num_chunks_complete", 0, m->num_chunks_complete);
  2281. } else {
  2282. assert(m->num_chunks == m->num_chunks_complete);
  2283. MESSAGE_CHECK_NUM_EQ(expected, m, num_chunks_complete);
  2284. for (i = 0; i < m->num_chunks && i < MAX_CHUNKS; i++) {
  2285. MESSAGE_CHECK_NUM_EQ(expected, m, chunk_lengths[i]);
  2286. }
  2287. }
  2288. MESSAGE_CHECK_NUM_EQ(expected, m, num_headers);
  2289. int r;
  2290. for (i = 0; i < m->num_headers; i++) {
  2291. r = check_str_eq(expected, "header field", expected->headers[i][0], m->headers[i][0]);
  2292. if (!r) return 0;
  2293. r = check_str_eq(expected, "header value", expected->headers[i][1], m->headers[i][1]);
  2294. if (!r) return 0;
  2295. }
  2296. MESSAGE_CHECK_STR_EQ(expected, m, upgrade);
  2297. return 1;
  2298. }
  2299. /* Given a sequence of varargs messages, return the number of them that the
  2300. * parser should successfully parse, taking into account that upgraded
  2301. * messages prevent all subsequent messages from being parsed.
  2302. */
  2303. size_t
  2304. count_parsed_messages(const size_t nmsgs, ...) {
  2305. size_t i;
  2306. va_list ap;
  2307. va_start(ap, nmsgs);
  2308. for (i = 0; i < nmsgs; i++) {
  2309. struct message *m = va_arg(ap, struct message *);
  2310. if (m->upgrade) {
  2311. va_end(ap);
  2312. return i + 1;
  2313. }
  2314. }
  2315. va_end(ap);
  2316. return nmsgs;
  2317. }
  2318. /* Given a sequence of bytes and the number of these that we were able to
  2319. * parse, verify that upgrade bodies are correct.
  2320. */
  2321. void
  2322. upgrade_message_fix(char *body, const size_t nread, const size_t nmsgs, ...) {
  2323. va_list ap;
  2324. size_t i;
  2325. size_t off = 0;
  2326. va_start(ap, nmsgs);
  2327. for (i = 0; i < nmsgs; i++) {
  2328. struct message *m = va_arg(ap, struct message *);
  2329. off += strlen(m->raw);
  2330. if (m->upgrade) {
  2331. off -= strlen(m->upgrade);
  2332. /* Check the portion of the response after its specified upgrade */
  2333. if (!check_str_eq(m, "upgrade", body + off, body + nread)) {
  2334. abort();
  2335. }
  2336. /* Fix up the response so that message_eq() will verify the beginning
  2337. * of the upgrade */
  2338. *(body + nread + strlen(m->upgrade)) = '\0';
  2339. messages[num_messages -1 ].upgrade = body + nread;
  2340. va_end(ap);
  2341. return;
  2342. }
  2343. }
  2344. va_end(ap);
  2345. printf("\n\n*** Error: expected a message with upgrade ***\n");
  2346. abort();
  2347. }
  2348. static void
  2349. print_error (const char *raw, size_t error_location)
  2350. {
  2351. fprintf(stderr, "\n*** %s ***\n\n",
  2352. http_errno_description(HTTP_PARSER_ERRNO(parser)));
  2353. int this_line = 0, char_len = 0;
  2354. size_t i, j, len = strlen(raw), error_location_line = 0;
  2355. for (i = 0; i < len; i++) {
  2356. if (i == error_location) this_line = 1;
  2357. switch (raw[i]) {
  2358. case '\r':
  2359. char_len = 2;
  2360. fprintf(stderr, "\\r");
  2361. break;
  2362. case '\n':
  2363. fprintf(stderr, "\\n\n");
  2364. if (this_line) goto print;
  2365. error_location_line = 0;
  2366. continue;
  2367. default:
  2368. char_len = 1;
  2369. fputc(raw[i], stderr);
  2370. break;
  2371. }
  2372. if (!this_line) error_location_line += char_len;
  2373. }
  2374. fprintf(stderr, "[eof]\n");
  2375. print:
  2376. for (j = 0; j < error_location_line; j++) {
  2377. fputc(' ', stderr);
  2378. }
  2379. fprintf(stderr, "^\n\nerror location: %u\n", (unsigned int)error_location);
  2380. }
  2381. void
  2382. test_preserve_data (void)
  2383. {
  2384. char my_data[] = "application-specific data";
  2385. http_parser parser;
  2386. parser.data = my_data;
  2387. http_parser_init(&parser, HTTP_REQUEST);
  2388. if (parser.data != my_data) {
  2389. printf("\n*** parser.data not preserved accross http_parser_init ***\n\n");
  2390. abort();
  2391. }
  2392. }
  2393. struct url_test {
  2394. const char *name;
  2395. const char *url;
  2396. int is_connect;
  2397. struct http_parser_url u;
  2398. int rv;
  2399. };
  2400. const struct url_test url_tests[] =
  2401. { {.name="proxy request"
  2402. ,.url="http://hostname/"
  2403. ,.is_connect=0
  2404. ,.u=
  2405. {.field_set=(1 << UF_SCHEMA) | (1 << UF_HOST) | (1 << UF_PATH)
  2406. ,.port=0
  2407. ,.field_data=
  2408. {{ 0, 4 } /* UF_SCHEMA */
  2409. ,{ 7, 8 } /* UF_HOST */
  2410. ,{ 0, 0 } /* UF_PORT */
  2411. ,{ 15, 1 } /* UF_PATH */
  2412. ,{ 0, 0 } /* UF_QUERY */
  2413. ,{ 0, 0 } /* UF_FRAGMENT */
  2414. ,{ 0, 0 } /* UF_USERINFO */
  2415. }
  2416. }
  2417. ,.rv=0
  2418. }
  2419. , {.name="proxy request with port"
  2420. ,.url="http://hostname:444/"
  2421. ,.is_connect=0
  2422. ,.u=
  2423. {.field_set=(1 << UF_SCHEMA) | (1 << UF_HOST) | (1 << UF_PORT) | (1 << UF_PATH)
  2424. ,.port=444
  2425. ,.field_data=
  2426. {{ 0, 4 } /* UF_SCHEMA */
  2427. ,{ 7, 8 } /* UF_HOST */
  2428. ,{ 16, 3 } /* UF_PORT */
  2429. ,{ 19, 1 } /* UF_PATH */
  2430. ,{ 0, 0 } /* UF_QUERY */
  2431. ,{ 0, 0 } /* UF_FRAGMENT */
  2432. ,{ 0, 0 } /* UF_USERINFO */
  2433. }
  2434. }
  2435. ,.rv=0
  2436. }
  2437. , {.name="CONNECT request"
  2438. ,.url="hostname:443"
  2439. ,.is_connect=1
  2440. ,.u=
  2441. {.field_set=(1 << UF_HOST) | (1 << UF_PORT)
  2442. ,.port=443
  2443. ,.field_data=
  2444. {{ 0, 0 } /* UF_SCHEMA */
  2445. ,{ 0, 8 } /* UF_HOST */
  2446. ,{ 9, 3 } /* UF_PORT */
  2447. ,{ 0, 0 } /* UF_PATH */
  2448. ,{ 0, 0 } /* UF_QUERY */
  2449. ,{ 0, 0 } /* UF_FRAGMENT */
  2450. ,{ 0, 0 } /* UF_USERINFO */
  2451. }
  2452. }
  2453. ,.rv=0
  2454. }
  2455. , {.name="CONNECT request but not connect"
  2456. ,.url="hostname:443"
  2457. ,.is_connect=0
  2458. ,.rv=1
  2459. }
  2460. , {.name="proxy ipv6 request"
  2461. ,.url="http://[1:2::3:4]/"
  2462. ,.is_connect=0
  2463. ,.u=
  2464. {.field_set=(1 << UF_SCHEMA) | (1 << UF_HOST) | (1 << UF_PATH)
  2465. ,.port=0
  2466. ,.field_data=
  2467. {{ 0, 4 } /* UF_SCHEMA */
  2468. ,{ 8, 8 } /* UF_HOST */
  2469. ,{ 0, 0 } /* UF_PORT */
  2470. ,{ 17, 1 } /* UF_PATH */
  2471. ,{ 0, 0 } /* UF_QUERY */
  2472. ,{ 0, 0 } /* UF_FRAGMENT */
  2473. ,{ 0, 0 } /* UF_USERINFO */
  2474. }
  2475. }
  2476. ,.rv=0
  2477. }
  2478. , {.name="proxy ipv6 request with port"
  2479. ,.url="http://[1:2::3:4]:67/"
  2480. ,.is_connect=0
  2481. ,.u=
  2482. {.field_set=(1 << UF_SCHEMA) | (1 << UF_HOST) | (1 << UF_PORT) | (1 << UF_PATH)
  2483. ,.port=67
  2484. ,.field_data=
  2485. {{ 0, 4 } /* UF_SCHEMA */
  2486. ,{ 8, 8 } /* UF_HOST */
  2487. ,{ 18, 2 } /* UF_PORT */
  2488. ,{ 20, 1 } /* UF_PATH */
  2489. ,{ 0, 0 } /* UF_QUERY */
  2490. ,{ 0, 0 } /* UF_FRAGMENT */
  2491. ,{ 0, 0 } /* UF_USERINFO */
  2492. }
  2493. }
  2494. ,.rv=0
  2495. }
  2496. , {.name="CONNECT ipv6 address"
  2497. ,.url="[1:2::3:4]:443"
  2498. ,.is_connect=1
  2499. ,.u=
  2500. {.field_set=(1 << UF_HOST) | (1 << UF_PORT)
  2501. ,.port=443
  2502. ,.field_data=
  2503. {{ 0, 0 } /* UF_SCHEMA */
  2504. ,{ 1, 8 } /* UF_HOST */
  2505. ,{ 11, 3 } /* UF_PORT */
  2506. ,{ 0, 0 } /* UF_PATH */
  2507. ,{ 0, 0 } /* UF_QUERY */
  2508. ,{ 0, 0 } /* UF_FRAGMENT */
  2509. ,{ 0, 0 } /* UF_USERINFO */
  2510. }
  2511. }
  2512. ,.rv=0
  2513. }
  2514. , {.name="ipv4 in ipv6 address"
  2515. ,.url="http://[2001:0000:0000:0000:0000:0000:1.9.1.1]/"
  2516. ,.is_connect=0
  2517. ,.u=
  2518. {.field_set=(1 << UF_SCHEMA) | (1 << UF_HOST) | (1 << UF_PATH)
  2519. ,.port=0
  2520. ,.field_data=
  2521. {{ 0, 4 } /* UF_SCHEMA */
  2522. ,{ 8, 37 } /* UF_HOST */
  2523. ,{ 0, 0 } /* UF_PORT */
  2524. ,{ 46, 1 } /* UF_PATH */
  2525. ,{ 0, 0 } /* UF_QUERY */
  2526. ,{ 0, 0 } /* UF_FRAGMENT */
  2527. ,{ 0, 0 } /* UF_USERINFO */
  2528. }
  2529. }
  2530. ,.rv=0
  2531. }
  2532. , {.name="extra ? in query string"
  2533. ,.url="http://a.tbcdn.cn/p/fp/2010c/??fp-header-min.css,fp-base-min.css,"
  2534. "fp-channel-min.css,fp-product-min.css,fp-mall-min.css,fp-category-min.css,"
  2535. "fp-sub-min.css,fp-gdp4p-min.css,fp-css3-min.css,fp-misc-min.css?t=20101022.css"
  2536. ,.is_connect=0
  2537. ,.u=
  2538. {.field_set=(1<<UF_SCHEMA) | (1<<UF_HOST) | (1<<UF_PATH) | (1<<UF_QUERY)
  2539. ,.port=0
  2540. ,.field_data=
  2541. {{ 0, 4 } /* UF_SCHEMA */
  2542. ,{ 7, 10 } /* UF_HOST */
  2543. ,{ 0, 0 } /* UF_PORT */
  2544. ,{ 17, 12 } /* UF_PATH */
  2545. ,{ 30,187 } /* UF_QUERY */
  2546. ,{ 0, 0 } /* UF_FRAGMENT */
  2547. ,{ 0, 0 } /* UF_USERINFO */
  2548. }
  2549. }
  2550. ,.rv=0
  2551. }
  2552. , {.name="space URL encoded"
  2553. ,.url="/toto.html?toto=a%20b"
  2554. ,.is_connect=0
  2555. ,.u=
  2556. {.field_set= (1<<UF_PATH) | (1<<UF_QUERY)
  2557. ,.port=0
  2558. ,.field_data=
  2559. {{ 0, 0 } /* UF_SCHEMA */
  2560. ,{ 0, 0 } /* UF_HOST */
  2561. ,{ 0, 0 } /* UF_PORT */
  2562. ,{ 0, 10 } /* UF_PATH */
  2563. ,{ 11, 10 } /* UF_QUERY */
  2564. ,{ 0, 0 } /* UF_FRAGMENT */
  2565. ,{ 0, 0 } /* UF_USERINFO */
  2566. }
  2567. }
  2568. ,.rv=0
  2569. }
  2570. , {.name="URL fragment"
  2571. ,.url="/toto.html#titi"
  2572. ,.is_connect=0
  2573. ,.u=
  2574. {.field_set= (1<<UF_PATH) | (1<<UF_FRAGMENT)
  2575. ,.port=0
  2576. ,.field_data=
  2577. {{ 0, 0 } /* UF_SCHEMA */
  2578. ,{ 0, 0 } /* UF_HOST */
  2579. ,{ 0, 0 } /* UF_PORT */
  2580. ,{ 0, 10 } /* UF_PATH */
  2581. ,{ 0, 0 } /* UF_QUERY */
  2582. ,{ 11, 4 } /* UF_FRAGMENT */
  2583. ,{ 0, 0 } /* UF_USERINFO */
  2584. }
  2585. }
  2586. ,.rv=0
  2587. }
  2588. , {.name="complex URL fragment"
  2589. ,.url="http://www.webmasterworld.com/r.cgi?f=21&d=8405&url="
  2590. "http://www.example.com/index.html?foo=bar&hello=world#midpage"
  2591. ,.is_connect=0
  2592. ,.u=
  2593. {.field_set= (1<<UF_SCHEMA) | (1<<UF_HOST) | (1<<UF_PATH) | (1<<UF_QUERY) |\
  2594. (1<<UF_FRAGMENT)
  2595. ,.port=0
  2596. ,.field_data=
  2597. {{ 0, 4 } /* UF_SCHEMA */
  2598. ,{ 7, 22 } /* UF_HOST */
  2599. ,{ 0, 0 } /* UF_PORT */
  2600. ,{ 29, 6 } /* UF_PATH */
  2601. ,{ 36, 69 } /* UF_QUERY */
  2602. ,{106, 7 } /* UF_FRAGMENT */
  2603. ,{ 0, 0 } /* UF_USERINFO */
  2604. }
  2605. }
  2606. ,.rv=0
  2607. }
  2608. , {.name="complex URL from node js url parser doc"
  2609. ,.url="http://host.com:8080/p/a/t/h?query=string#hash"
  2610. ,.is_connect=0
  2611. ,.u=
  2612. {.field_set= (1<<UF_SCHEMA) | (1<<UF_HOST) | (1<<UF_PORT) | (1<<UF_PATH) |\
  2613. (1<<UF_QUERY) | (1<<UF_FRAGMENT)
  2614. ,.port=8080
  2615. ,.field_data=
  2616. {{ 0, 4 } /* UF_SCHEMA */
  2617. ,{ 7, 8 } /* UF_HOST */
  2618. ,{ 16, 4 } /* UF_PORT */
  2619. ,{ 20, 8 } /* UF_PATH */
  2620. ,{ 29, 12 } /* UF_QUERY */
  2621. ,{ 42, 4 } /* UF_FRAGMENT */
  2622. ,{ 0, 0 } /* UF_USERINFO */
  2623. }
  2624. }
  2625. ,.rv=0
  2626. }
  2627. , {.name="complex URL with basic auth from node js url parser doc"
  2628. ,.url="http://a:b@host.com:8080/p/a/t/h?query=string#hash"
  2629. ,.is_connect=0
  2630. ,.u=
  2631. {.field_set= (1<<UF_SCHEMA) | (1<<UF_HOST) | (1<<UF_PORT) | (1<<UF_PATH) |\
  2632. (1<<UF_QUERY) | (1<<UF_FRAGMENT) | (1<<UF_USERINFO)
  2633. ,.port=8080
  2634. ,.field_data=
  2635. {{ 0, 4 } /* UF_SCHEMA */
  2636. ,{ 11, 8 } /* UF_HOST */
  2637. ,{ 20, 4 } /* UF_PORT */
  2638. ,{ 24, 8 } /* UF_PATH */
  2639. ,{ 33, 12 } /* UF_QUERY */
  2640. ,{ 46, 4 } /* UF_FRAGMENT */
  2641. ,{ 7, 3 } /* UF_USERINFO */
  2642. }
  2643. }
  2644. ,.rv=0
  2645. }
  2646. , {.name="double @"
  2647. ,.url="http://a:b@@hostname:443/"
  2648. ,.is_connect=0
  2649. ,.rv=1
  2650. }
  2651. , {.name="proxy empty host"
  2652. ,.url="http://:443/"
  2653. ,.is_connect=0
  2654. ,.rv=1
  2655. }
  2656. , {.name="proxy empty port"
  2657. ,.url="http://hostname:/"
  2658. ,.is_connect=0
  2659. ,.rv=1
  2660. }
  2661. , {.name="CONNECT with basic auth"
  2662. ,.url="a:b@hostname:443"
  2663. ,.is_connect=1
  2664. ,.rv=1
  2665. }
  2666. , {.name="CONNECT empty host"
  2667. ,.url=":443"
  2668. ,.is_connect=1
  2669. ,.rv=1
  2670. }
  2671. , {.name="CONNECT empty port"
  2672. ,.url="hostname:"
  2673. ,.is_connect=1
  2674. ,.rv=1
  2675. }
  2676. , {.name="CONNECT with extra bits"
  2677. ,.url="hostname:443/"
  2678. ,.is_connect=1
  2679. ,.rv=1
  2680. }
  2681. , {.name="space in URL"
  2682. ,.url="/foo bar/"
  2683. ,.rv=1 /* s_dead */
  2684. }
  2685. , {.name="proxy basic auth with space url encoded"
  2686. ,.url="http://a%20:b@host.com/"
  2687. ,.is_connect=0
  2688. ,.u=
  2689. {.field_set= (1<<UF_SCHEMA) | (1<<UF_HOST) | (1<<UF_PATH) | (1<<UF_USERINFO)
  2690. ,.port=0
  2691. ,.field_data=
  2692. {{ 0, 4 } /* UF_SCHEMA */
  2693. ,{ 14, 8 } /* UF_HOST */
  2694. ,{ 0, 0 } /* UF_PORT */
  2695. ,{ 22, 1 } /* UF_PATH */
  2696. ,{ 0, 0 } /* UF_QUERY */
  2697. ,{ 0, 0 } /* UF_FRAGMENT */
  2698. ,{ 7, 6 } /* UF_USERINFO */
  2699. }
  2700. }
  2701. ,.rv=0
  2702. }
  2703. , {.name="carriage return in URL"
  2704. ,.url="/foo\rbar/"
  2705. ,.rv=1 /* s_dead */
  2706. }
  2707. , {.name="proxy double : in URL"
  2708. ,.url="http://hostname::443/"
  2709. ,.rv=1 /* s_dead */
  2710. }
  2711. , {.name="proxy basic auth with double :"
  2712. ,.url="http://a::b@host.com/"
  2713. ,.is_connect=0
  2714. ,.u=
  2715. {.field_set= (1<<UF_SCHEMA) | (1<<UF_HOST) | (1<<UF_PATH) | (1<<UF_USERINFO)
  2716. ,.port=0
  2717. ,.field_data=
  2718. {{ 0, 4 } /* UF_SCHEMA */
  2719. ,{ 12, 8 } /* UF_HOST */
  2720. ,{ 0, 0 } /* UF_PORT */
  2721. ,{ 20, 1 } /* UF_PATH */
  2722. ,{ 0, 0 } /* UF_QUERY */
  2723. ,{ 0, 0 } /* UF_FRAGMENT */
  2724. ,{ 7, 4 } /* UF_USERINFO */
  2725. }
  2726. }
  2727. ,.rv=0
  2728. }
  2729. , {.name="line feed in URL"
  2730. ,.url="/foo\nbar/"
  2731. ,.rv=1 /* s_dead */
  2732. }
  2733. , {.name="proxy empty basic auth"
  2734. ,.url="http://@hostname/fo"
  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, 8 } /* UF_HOST */
  2741. ,{ 0, 0 } /* UF_PORT */
  2742. ,{ 16, 3 } /* 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="proxy line feed in hostname"
  2751. ,.url="http://host\name/fo"
  2752. ,.rv=1 /* s_dead */
  2753. }
  2754. , {.name="proxy % in hostname"
  2755. ,.url="http://host%name/fo"
  2756. ,.rv=1 /* s_dead */
  2757. }
  2758. , {.name="proxy ; in hostname"
  2759. ,.url="http://host;ame/fo"
  2760. ,.rv=1 /* s_dead */
  2761. }
  2762. , {.name="proxy basic auth with unreservedchars"
  2763. ,.url="http://a!;-_!=+$@host.com/"
  2764. ,.is_connect=0
  2765. ,.u=
  2766. {.field_set= (1<<UF_SCHEMA) | (1<<UF_HOST) | (1<<UF_PATH) | (1<<UF_USERINFO)
  2767. ,.port=0
  2768. ,.field_data=
  2769. {{ 0, 4 } /* UF_SCHEMA */
  2770. ,{ 17, 8 } /* UF_HOST */
  2771. ,{ 0, 0 } /* UF_PORT */
  2772. ,{ 25, 1 } /* UF_PATH */
  2773. ,{ 0, 0 } /* UF_QUERY */
  2774. ,{ 0, 0 } /* UF_FRAGMENT */
  2775. ,{ 7, 9 } /* UF_USERINFO */
  2776. }
  2777. }
  2778. ,.rv=0
  2779. }
  2780. , {.name="proxy only empty basic auth"
  2781. ,.url="http://@/fo"
  2782. ,.rv=1 /* s_dead */
  2783. }
  2784. , {.name="proxy only basic auth"
  2785. ,.url="http://toto@/fo"
  2786. ,.rv=1 /* s_dead */
  2787. }
  2788. , {.name="proxy emtpy hostname"
  2789. ,.url="http:///fo"
  2790. ,.rv=1 /* s_dead */
  2791. }
  2792. , {.name="proxy = in URL"
  2793. ,.url="http://host=ame/fo"
  2794. ,.rv=1 /* s_dead */
  2795. }
  2796. , {.name="ipv6 address with Zone ID"
  2797. ,.url="http://[fe80::a%25eth0]/"
  2798. ,.is_connect=0
  2799. ,.u=
  2800. {.field_set= (1<<UF_SCHEMA) | (1<<UF_HOST) | (1<<UF_PATH)
  2801. ,.port=0
  2802. ,.field_data=
  2803. {{ 0, 4 } /* UF_SCHEMA */
  2804. ,{ 8, 14 } /* UF_HOST */
  2805. ,{ 0, 0 } /* UF_PORT */
  2806. ,{ 23, 1 } /* UF_PATH */
  2807. ,{ 0, 0 } /* UF_QUERY */
  2808. ,{ 0, 0 } /* UF_FRAGMENT */
  2809. ,{ 0, 0 } /* UF_USERINFO */
  2810. }
  2811. }
  2812. ,.rv=0
  2813. }
  2814. , {.name="ipv6 address with Zone ID, but '%' is not percent-encoded"
  2815. ,.url="http://[fe80::a%eth0]/"
  2816. ,.is_connect=0
  2817. ,.u=
  2818. {.field_set= (1<<UF_SCHEMA) | (1<<UF_HOST) | (1<<UF_PATH)
  2819. ,.port=0
  2820. ,.field_data=
  2821. {{ 0, 4 } /* UF_SCHEMA */
  2822. ,{ 8, 12 } /* UF_HOST */
  2823. ,{ 0, 0 } /* UF_PORT */
  2824. ,{ 21, 1 } /* UF_PATH */
  2825. ,{ 0, 0 } /* UF_QUERY */
  2826. ,{ 0, 0 } /* UF_FRAGMENT */
  2827. ,{ 0, 0 } /* UF_USERINFO */
  2828. }
  2829. }
  2830. ,.rv=0
  2831. }
  2832. , {.name="ipv6 address ending with '%'"
  2833. ,.url="http://[fe80::a%]/"
  2834. ,.rv=1 /* s_dead */
  2835. }
  2836. , {.name="ipv6 address with Zone ID including bad character"
  2837. ,.url="http://[fe80::a%$HOME]/"
  2838. ,.rv=1 /* s_dead */
  2839. }
  2840. , {.name="just ipv6 Zone ID"
  2841. ,.url="http://[%eth0]/"
  2842. ,.rv=1 /* s_dead */
  2843. }
  2844. #if HTTP_PARSER_STRICT
  2845. , {.name="tab in URL"
  2846. ,.url="/foo\tbar/"
  2847. ,.rv=1 /* s_dead */
  2848. }
  2849. , {.name="form feed in URL"
  2850. ,.url="/foo\fbar/"
  2851. ,.rv=1 /* s_dead */
  2852. }
  2853. #else /* !HTTP_PARSER_STRICT */
  2854. , {.name="tab in URL"
  2855. ,.url="/foo\tbar/"
  2856. ,.u=
  2857. {.field_set=(1 << UF_PATH)
  2858. ,.field_data=
  2859. {{ 0, 0 } /* UF_SCHEMA */
  2860. ,{ 0, 0 } /* UF_HOST */
  2861. ,{ 0, 0 } /* UF_PORT */
  2862. ,{ 0, 9 } /* UF_PATH */
  2863. ,{ 0, 0 } /* UF_QUERY */
  2864. ,{ 0, 0 } /* UF_FRAGMENT */
  2865. ,{ 0, 0 } /* UF_USERINFO */
  2866. }
  2867. }
  2868. ,.rv=0
  2869. }
  2870. , {.name="form feed in URL"
  2871. ,.url="/foo\fbar/"
  2872. ,.u=
  2873. {.field_set=(1 << UF_PATH)
  2874. ,.field_data=
  2875. {{ 0, 0 } /* UF_SCHEMA */
  2876. ,{ 0, 0 } /* UF_HOST */
  2877. ,{ 0, 0 } /* UF_PORT */
  2878. ,{ 0, 9 } /* UF_PATH */
  2879. ,{ 0, 0 } /* UF_QUERY */
  2880. ,{ 0, 0 } /* UF_FRAGMENT */
  2881. ,{ 0, 0 } /* UF_USERINFO */
  2882. }
  2883. }
  2884. ,.rv=0
  2885. }
  2886. #endif
  2887. };
  2888. void
  2889. dump_url (const char *url, const struct http_parser_url *u)
  2890. {
  2891. unsigned int i;
  2892. printf("\tfield_set: 0x%x, port: %u\n", u->field_set, u->port);
  2893. for (i = 0; i < UF_MAX; i++) {
  2894. if ((u->field_set & (1 << i)) == 0) {
  2895. printf("\tfield_data[%u]: unset\n", i);
  2896. continue;
  2897. }
  2898. printf("\tfield_data[%u]: off: %u len: %u part: \"%.*s\n\"",
  2899. i,
  2900. u->field_data[i].off,
  2901. u->field_data[i].len,
  2902. u->field_data[i].len,
  2903. url + u->field_data[i].off);
  2904. }
  2905. }
  2906. void
  2907. test_parse_url (void)
  2908. {
  2909. struct http_parser_url u;
  2910. const struct url_test *test;
  2911. unsigned int i;
  2912. int rv;
  2913. for (i = 0; i < (sizeof(url_tests) / sizeof(url_tests[0])); i++) {
  2914. test = &url_tests[i];
  2915. memset(&u, 0, sizeof(u));
  2916. rv = http_parser_parse_url(test->url,
  2917. strlen(test->url),
  2918. test->is_connect,
  2919. &u);
  2920. if (test->rv == 0) {
  2921. if (rv != 0) {
  2922. printf("\n*** http_parser_parse_url(\"%s\") \"%s\" test failed, "
  2923. "unexpected rv %d ***\n\n", test->url, test->name, rv);
  2924. abort();
  2925. }
  2926. if (memcmp(&u, &test->u, sizeof(u)) != 0) {
  2927. printf("\n*** http_parser_parse_url(\"%s\") \"%s\" failed ***\n",
  2928. test->url, test->name);
  2929. printf("target http_parser_url:\n");
  2930. dump_url(test->url, &test->u);
  2931. printf("result http_parser_url:\n");
  2932. dump_url(test->url, &u);
  2933. abort();
  2934. }
  2935. } else {
  2936. /* test->rv != 0 */
  2937. if (rv == 0) {
  2938. printf("\n*** http_parser_parse_url(\"%s\") \"%s\" test failed, "
  2939. "unexpected rv %d ***\n\n", test->url, test->name, rv);
  2940. abort();
  2941. }
  2942. }
  2943. }
  2944. }
  2945. void
  2946. test_method_str (void)
  2947. {
  2948. assert(0 == strcmp("GET", http_method_str(HTTP_GET)));
  2949. assert(0 == strcmp("<unknown>", http_method_str(1337)));
  2950. }
  2951. void
  2952. test_message (const struct message *message)
  2953. {
  2954. size_t raw_len = strlen(message->raw);
  2955. size_t msg1len;
  2956. for (msg1len = 0; msg1len < raw_len; msg1len++) {
  2957. parser_init(message->type);
  2958. size_t read;
  2959. const char *msg1 = message->raw;
  2960. const char *msg2 = msg1 + msg1len;
  2961. size_t msg2len = raw_len - msg1len;
  2962. if (msg1len) {
  2963. read = parse(msg1, msg1len);
  2964. if (message->upgrade && parser->upgrade && num_messages > 0) {
  2965. messages[num_messages - 1].upgrade = msg1 + read;
  2966. goto test;
  2967. }
  2968. if (read != msg1len) {
  2969. print_error(msg1, read);
  2970. abort();
  2971. }
  2972. }
  2973. read = parse(msg2, msg2len);
  2974. if (message->upgrade && parser->upgrade) {
  2975. messages[num_messages - 1].upgrade = msg2 + read;
  2976. goto test;
  2977. }
  2978. if (read != msg2len) {
  2979. print_error(msg2, read);
  2980. abort();
  2981. }
  2982. read = parse(NULL, 0);
  2983. if (read != 0) {
  2984. print_error(message->raw, read);
  2985. abort();
  2986. }
  2987. test:
  2988. if (num_messages != 1) {
  2989. printf("\n*** num_messages != 1 after testing '%s' ***\n\n", message->name);
  2990. abort();
  2991. }
  2992. if(!message_eq(0, 0, message)) abort();
  2993. parser_free();
  2994. }
  2995. }
  2996. void
  2997. test_message_count_body (const struct message *message)
  2998. {
  2999. parser_init(message->type);
  3000. size_t read;
  3001. size_t l = strlen(message->raw);
  3002. size_t i, toread;
  3003. size_t chunk = 4024;
  3004. for (i = 0; i < l; i+= chunk) {
  3005. toread = MIN(l-i, chunk);
  3006. read = parse_count_body(message->raw + i, toread);
  3007. if (read != toread) {
  3008. print_error(message->raw, read);
  3009. abort();
  3010. }
  3011. }
  3012. read = parse_count_body(NULL, 0);
  3013. if (read != 0) {
  3014. print_error(message->raw, read);
  3015. abort();
  3016. }
  3017. if (num_messages != 1) {
  3018. printf("\n*** num_messages != 1 after testing '%s' ***\n\n", message->name);
  3019. abort();
  3020. }
  3021. if(!message_eq(0, 0, message)) abort();
  3022. parser_free();
  3023. }
  3024. void
  3025. test_simple (const char *buf, enum http_errno err_expected)
  3026. {
  3027. parser_init(HTTP_REQUEST);
  3028. enum http_errno err;
  3029. parse(buf, strlen(buf));
  3030. err = HTTP_PARSER_ERRNO(parser);
  3031. parse(NULL, 0);
  3032. parser_free();
  3033. /* In strict mode, allow us to pass with an unexpected HPE_STRICT as
  3034. * long as the caller isn't expecting success.
  3035. */
  3036. #if HTTP_PARSER_STRICT
  3037. if (err_expected != err && err_expected != HPE_OK && err != HPE_STRICT) {
  3038. #else
  3039. if (err_expected != err) {
  3040. #endif
  3041. fprintf(stderr, "\n*** test_simple expected %s, but saw %s ***\n\n%s\n",
  3042. http_errno_name(err_expected), http_errno_name(err), buf);
  3043. abort();
  3044. }
  3045. }
  3046. void
  3047. test_invalid_header_content (int req, const char* str)
  3048. {
  3049. http_parser parser;
  3050. http_parser_init(&parser, req ? HTTP_REQUEST : HTTP_RESPONSE);
  3051. size_t parsed;
  3052. const char *buf;
  3053. buf = req ?
  3054. "GET / HTTP/1.1\r\n" :
  3055. "HTTP/1.1 200 OK\r\n";
  3056. parsed = http_parser_execute(&parser, &settings_null, buf, strlen(buf));
  3057. assert(parsed == strlen(buf));
  3058. buf = str;
  3059. size_t buflen = strlen(buf);
  3060. parsed = http_parser_execute(&parser, &settings_null, buf, buflen);
  3061. if (parsed != buflen) {
  3062. assert(HTTP_PARSER_ERRNO(&parser) == HPE_INVALID_HEADER_TOKEN);
  3063. return;
  3064. }
  3065. fprintf(stderr,
  3066. "\n*** Error expected but none in invalid header content test ***\n");
  3067. abort();
  3068. }
  3069. void
  3070. test_invalid_header_field_content_error (int req)
  3071. {
  3072. test_invalid_header_content(req, "Foo: F\01ailure");
  3073. test_invalid_header_content(req, "Foo: B\02ar");
  3074. }
  3075. void
  3076. test_invalid_header_field (int req, const char* str)
  3077. {
  3078. http_parser parser;
  3079. http_parser_init(&parser, req ? HTTP_REQUEST : HTTP_RESPONSE);
  3080. size_t parsed;
  3081. const char *buf;
  3082. buf = req ?
  3083. "GET / HTTP/1.1\r\n" :
  3084. "HTTP/1.1 200 OK\r\n";
  3085. parsed = http_parser_execute(&parser, &settings_null, buf, strlen(buf));
  3086. assert(parsed == strlen(buf));
  3087. buf = str;
  3088. size_t buflen = strlen(buf);
  3089. parsed = http_parser_execute(&parser, &settings_null, buf, buflen);
  3090. if (parsed != buflen) {
  3091. assert(HTTP_PARSER_ERRNO(&parser) == HPE_INVALID_HEADER_TOKEN);
  3092. return;
  3093. }
  3094. fprintf(stderr,
  3095. "\n*** Error expected but none in invalid header token test ***\n");
  3096. abort();
  3097. }
  3098. void
  3099. test_invalid_header_field_token_error (int req)
  3100. {
  3101. test_invalid_header_field(req, "Fo@: Failure");
  3102. test_invalid_header_field(req, "Foo\01\test: Bar");
  3103. }
  3104. void
  3105. test_double_content_length_error (int req)
  3106. {
  3107. http_parser parser;
  3108. http_parser_init(&parser, req ? HTTP_REQUEST : HTTP_RESPONSE);
  3109. size_t parsed;
  3110. const char *buf;
  3111. buf = req ?
  3112. "GET / HTTP/1.1\r\n" :
  3113. "HTTP/1.1 200 OK\r\n";
  3114. parsed = http_parser_execute(&parser, &settings_null, buf, strlen(buf));
  3115. assert(parsed == strlen(buf));
  3116. buf = "Content-Length: 0\r\nContent-Length: 1\r\n\r\n";
  3117. size_t buflen = strlen(buf);
  3118. parsed = http_parser_execute(&parser, &settings_null, buf, buflen);
  3119. if (parsed != buflen) {
  3120. assert(HTTP_PARSER_ERRNO(&parser) == HPE_UNEXPECTED_CONTENT_LENGTH);
  3121. return;
  3122. }
  3123. fprintf(stderr,
  3124. "\n*** Error expected but none in double content-length test ***\n");
  3125. abort();
  3126. }
  3127. void
  3128. test_chunked_content_length_error (int req)
  3129. {
  3130. http_parser parser;
  3131. http_parser_init(&parser, req ? HTTP_REQUEST : HTTP_RESPONSE);
  3132. size_t parsed;
  3133. const char *buf;
  3134. buf = req ?
  3135. "GET / HTTP/1.1\r\n" :
  3136. "HTTP/1.1 200 OK\r\n";
  3137. parsed = http_parser_execute(&parser, &settings_null, buf, strlen(buf));
  3138. assert(parsed == strlen(buf));
  3139. buf = "Transfer-Encoding: chunked\r\nContent-Length: 1\r\n\r\n";
  3140. size_t buflen = strlen(buf);
  3141. parsed = http_parser_execute(&parser, &settings_null, buf, buflen);
  3142. if (parsed != buflen) {
  3143. assert(HTTP_PARSER_ERRNO(&parser) == HPE_UNEXPECTED_CONTENT_LENGTH);
  3144. return;
  3145. }
  3146. fprintf(stderr,
  3147. "\n*** Error expected but none in chunked content-length test ***\n");
  3148. abort();
  3149. }
  3150. void
  3151. test_header_cr_no_lf_error (int req)
  3152. {
  3153. http_parser parser;
  3154. http_parser_init(&parser, req ? HTTP_REQUEST : HTTP_RESPONSE);
  3155. size_t parsed;
  3156. const char *buf;
  3157. buf = req ?
  3158. "GET / HTTP/1.1\r\n" :
  3159. "HTTP/1.1 200 OK\r\n";
  3160. parsed = http_parser_execute(&parser, &settings_null, buf, strlen(buf));
  3161. assert(parsed == strlen(buf));
  3162. buf = "Foo: 1\rBar: 1\r\n\r\n";
  3163. size_t buflen = strlen(buf);
  3164. parsed = http_parser_execute(&parser, &settings_null, buf, buflen);
  3165. if (parsed != buflen) {
  3166. assert(HTTP_PARSER_ERRNO(&parser) == HPE_LF_EXPECTED);
  3167. return;
  3168. }
  3169. fprintf(stderr,
  3170. "\n*** Error expected but none in header whitespace test ***\n");
  3171. abort();
  3172. }
  3173. void
  3174. test_header_overflow_error (int req)
  3175. {
  3176. http_parser parser;
  3177. http_parser_init(&parser, req ? HTTP_REQUEST : HTTP_RESPONSE);
  3178. size_t parsed;
  3179. const char *buf;
  3180. buf = req ? "GET / HTTP/1.1\r\n" : "HTTP/1.0 200 OK\r\n";
  3181. parsed = http_parser_execute(&parser, &settings_null, buf, strlen(buf));
  3182. assert(parsed == strlen(buf));
  3183. buf = "header-key: header-value\r\n";
  3184. size_t buflen = strlen(buf);
  3185. int i;
  3186. for (i = 0; i < 10000; i++) {
  3187. parsed = http_parser_execute(&parser, &settings_null, buf, buflen);
  3188. if (parsed != buflen) {
  3189. //fprintf(stderr, "error found on iter %d\n", i);
  3190. assert(HTTP_PARSER_ERRNO(&parser) == HPE_HEADER_OVERFLOW);
  3191. return;
  3192. }
  3193. }
  3194. fprintf(stderr, "\n*** Error expected but none in header overflow test ***\n");
  3195. abort();
  3196. }
  3197. void
  3198. test_header_nread_value ()
  3199. {
  3200. http_parser parser;
  3201. http_parser_init(&parser, HTTP_REQUEST);
  3202. size_t parsed;
  3203. const char *buf;
  3204. buf = "GET / HTTP/1.1\r\nheader: value\nhdr: value\r\n";
  3205. parsed = http_parser_execute(&parser, &settings_null, buf, strlen(buf));
  3206. assert(parsed == strlen(buf));
  3207. assert(parser.nread == strlen(buf));
  3208. }
  3209. static void
  3210. test_content_length_overflow (const char *buf, size_t buflen, int expect_ok)
  3211. {
  3212. http_parser parser;
  3213. http_parser_init(&parser, HTTP_RESPONSE);
  3214. http_parser_execute(&parser, &settings_null, buf, buflen);
  3215. if (expect_ok)
  3216. assert(HTTP_PARSER_ERRNO(&parser) == HPE_OK);
  3217. else
  3218. assert(HTTP_PARSER_ERRNO(&parser) == HPE_INVALID_CONTENT_LENGTH);
  3219. }
  3220. void
  3221. test_header_content_length_overflow_error (void)
  3222. {
  3223. #define X(size) \
  3224. "HTTP/1.1 200 OK\r\n" \
  3225. "Content-Length: " #size "\r\n" \
  3226. "\r\n"
  3227. const char a[] = X(1844674407370955160); /* 2^64 / 10 - 1 */
  3228. const char b[] = X(18446744073709551615); /* 2^64-1 */
  3229. const char c[] = X(18446744073709551616); /* 2^64 */
  3230. #undef X
  3231. test_content_length_overflow(a, sizeof(a) - 1, 1); /* expect ok */
  3232. test_content_length_overflow(b, sizeof(b) - 1, 0); /* expect failure */
  3233. test_content_length_overflow(c, sizeof(c) - 1, 0); /* expect failure */
  3234. }
  3235. void
  3236. test_chunk_content_length_overflow_error (void)
  3237. {
  3238. #define X(size) \
  3239. "HTTP/1.1 200 OK\r\n" \
  3240. "Transfer-Encoding: chunked\r\n" \
  3241. "\r\n" \
  3242. #size "\r\n" \
  3243. "..."
  3244. const char a[] = X(FFFFFFFFFFFFFFE); /* 2^64 / 16 - 1 */
  3245. const char b[] = X(FFFFFFFFFFFFFFFF); /* 2^64-1 */
  3246. const char c[] = X(10000000000000000); /* 2^64 */
  3247. #undef X
  3248. test_content_length_overflow(a, sizeof(a) - 1, 1); /* expect ok */
  3249. test_content_length_overflow(b, sizeof(b) - 1, 0); /* expect failure */
  3250. test_content_length_overflow(c, sizeof(c) - 1, 0); /* expect failure */
  3251. }
  3252. void
  3253. test_no_overflow_long_body (int req, size_t length)
  3254. {
  3255. http_parser parser;
  3256. http_parser_init(&parser, req ? HTTP_REQUEST : HTTP_RESPONSE);
  3257. size_t parsed;
  3258. size_t i;
  3259. char buf1[3000];
  3260. size_t buf1len = sprintf(buf1, "%s\r\nConnection: Keep-Alive\r\nContent-Length: %lu\r\n\r\n",
  3261. req ? "POST / HTTP/1.0" : "HTTP/1.0 200 OK", (unsigned long)length);
  3262. parsed = http_parser_execute(&parser, &settings_null, buf1, buf1len);
  3263. if (parsed != buf1len)
  3264. goto err;
  3265. for (i = 0; i < length; i++) {
  3266. char foo = 'a';
  3267. parsed = http_parser_execute(&parser, &settings_null, &foo, 1);
  3268. if (parsed != 1)
  3269. goto err;
  3270. }
  3271. parsed = http_parser_execute(&parser, &settings_null, buf1, buf1len);
  3272. if (parsed != buf1len) goto err;
  3273. return;
  3274. err:
  3275. fprintf(stderr,
  3276. "\n*** error in test_no_overflow_long_body %s of length %lu ***\n",
  3277. req ? "REQUEST" : "RESPONSE",
  3278. (unsigned long)length);
  3279. abort();
  3280. }
  3281. void
  3282. test_multiple3 (const struct message *r1, const struct message *r2, const struct message *r3)
  3283. {
  3284. int message_count = count_parsed_messages(3, r1, r2, r3);
  3285. char total[ strlen(r1->raw)
  3286. + strlen(r2->raw)
  3287. + strlen(r3->raw)
  3288. + 1
  3289. ];
  3290. total[0] = '\0';
  3291. strcat(total, r1->raw);
  3292. strcat(total, r2->raw);
  3293. strcat(total, r3->raw);
  3294. parser_init(r1->type);
  3295. size_t read;
  3296. read = parse(total, strlen(total));
  3297. if (parser->upgrade) {
  3298. upgrade_message_fix(total, read, 3, r1, r2, r3);
  3299. goto test;
  3300. }
  3301. if (read != strlen(total)) {
  3302. print_error(total, read);
  3303. abort();
  3304. }
  3305. read = parse(NULL, 0);
  3306. if (read != 0) {
  3307. print_error(total, read);
  3308. abort();
  3309. }
  3310. test:
  3311. if (message_count != num_messages) {
  3312. fprintf(stderr, "\n\n*** Parser didn't see 3 messages only %d *** \n", num_messages);
  3313. abort();
  3314. }
  3315. if (!message_eq(0, 0, r1)) abort();
  3316. if (message_count > 1 && !message_eq(1, 0, r2)) abort();
  3317. if (message_count > 2 && !message_eq(2, 0, r3)) abort();
  3318. parser_free();
  3319. }
  3320. /* SCAN through every possible breaking to make sure the
  3321. * parser can handle getting the content in any chunks that
  3322. * might come from the socket
  3323. */
  3324. void
  3325. test_scan (const struct message *r1, const struct message *r2, const struct message *r3)
  3326. {
  3327. char total[80*1024] = "\0";
  3328. char buf1[80*1024] = "\0";
  3329. char buf2[80*1024] = "\0";
  3330. char buf3[80*1024] = "\0";
  3331. strcat(total, r1->raw);
  3332. strcat(total, r2->raw);
  3333. strcat(total, r3->raw);
  3334. size_t read;
  3335. int total_len = strlen(total);
  3336. int total_ops = 2 * (total_len - 1) * (total_len - 2) / 2;
  3337. int ops = 0 ;
  3338. size_t buf1_len, buf2_len, buf3_len;
  3339. int message_count = count_parsed_messages(3, r1, r2, r3);
  3340. int i,j,type_both;
  3341. for (type_both = 0; type_both < 2; type_both ++ ) {
  3342. for (j = 2; j < total_len; j ++ ) {
  3343. for (i = 1; i < j; i ++ ) {
  3344. if (ops % 1000 == 0) {
  3345. printf("\b\b\b\b%3.0f%%", 100 * (float)ops /(float)total_ops);
  3346. fflush(stdout);
  3347. }
  3348. ops += 1;
  3349. parser_init(type_both ? HTTP_BOTH : r1->type);
  3350. buf1_len = i;
  3351. strlncpy(buf1, sizeof(buf1), total, buf1_len);
  3352. buf1[buf1_len] = 0;
  3353. buf2_len = j - i;
  3354. strlncpy(buf2, sizeof(buf1), total+i, buf2_len);
  3355. buf2[buf2_len] = 0;
  3356. buf3_len = total_len - j;
  3357. strlncpy(buf3, sizeof(buf1), total+j, buf3_len);
  3358. buf3[buf3_len] = 0;
  3359. read = parse(buf1, buf1_len);
  3360. if (parser->upgrade) goto test;
  3361. if (read != buf1_len) {
  3362. print_error(buf1, read);
  3363. goto error;
  3364. }
  3365. read += parse(buf2, buf2_len);
  3366. if (parser->upgrade) goto test;
  3367. if (read != buf1_len + buf2_len) {
  3368. print_error(buf2, read);
  3369. goto error;
  3370. }
  3371. read += parse(buf3, buf3_len);
  3372. if (parser->upgrade) goto test;
  3373. if (read != buf1_len + buf2_len + buf3_len) {
  3374. print_error(buf3, read);
  3375. goto error;
  3376. }
  3377. parse(NULL, 0);
  3378. test:
  3379. if (parser->upgrade) {
  3380. upgrade_message_fix(total, read, 3, r1, r2, r3);
  3381. }
  3382. if (message_count != num_messages) {
  3383. fprintf(stderr, "\n\nParser didn't see %d messages only %d\n",
  3384. message_count, num_messages);
  3385. goto error;
  3386. }
  3387. if (!message_eq(0, 0, r1)) {
  3388. fprintf(stderr, "\n\nError matching messages[0] in test_scan.\n");
  3389. goto error;
  3390. }
  3391. if (message_count > 1 && !message_eq(1, 0, r2)) {
  3392. fprintf(stderr, "\n\nError matching messages[1] in test_scan.\n");
  3393. goto error;
  3394. }
  3395. if (message_count > 2 && !message_eq(2, 0, r3)) {
  3396. fprintf(stderr, "\n\nError matching messages[2] in test_scan.\n");
  3397. goto error;
  3398. }
  3399. parser_free();
  3400. }
  3401. }
  3402. }
  3403. puts("\b\b\b\b100%");
  3404. return;
  3405. error:
  3406. fprintf(stderr, "i=%d j=%d\n", i, j);
  3407. fprintf(stderr, "buf1 (%u) %s\n\n", (unsigned int)buf1_len, buf1);
  3408. fprintf(stderr, "buf2 (%u) %s\n\n", (unsigned int)buf2_len , buf2);
  3409. fprintf(stderr, "buf3 (%u) %s\n", (unsigned int)buf3_len, buf3);
  3410. abort();
  3411. }
  3412. // user required to free the result
  3413. // string terminated by \0
  3414. char *
  3415. create_large_chunked_message (int body_size_in_kb, const char* headers)
  3416. {
  3417. int i;
  3418. size_t wrote = 0;
  3419. size_t headers_len = strlen(headers);
  3420. size_t bufsize = headers_len + (5+1024+2)*body_size_in_kb + 6;
  3421. char * buf = malloc(bufsize);
  3422. memcpy(buf, headers, headers_len);
  3423. wrote += headers_len;
  3424. for (i = 0; i < body_size_in_kb; i++) {
  3425. // write 1kb chunk into the body.
  3426. memcpy(buf + wrote, "400\r\n", 5);
  3427. wrote += 5;
  3428. memset(buf + wrote, 'C', 1024);
  3429. wrote += 1024;
  3430. strcpy(buf + wrote, "\r\n");
  3431. wrote += 2;
  3432. }
  3433. memcpy(buf + wrote, "0\r\n\r\n", 6);
  3434. wrote += 6;
  3435. assert(wrote == bufsize);
  3436. return buf;
  3437. }
  3438. /* Verify that we can pause parsing at any of the bytes in the
  3439. * message and still get the result that we're expecting. */
  3440. void
  3441. test_message_pause (const struct message *msg)
  3442. {
  3443. char *buf = (char*) msg->raw;
  3444. size_t buflen = strlen(msg->raw);
  3445. size_t nread;
  3446. parser_init(msg->type);
  3447. do {
  3448. nread = parse_pause(buf, buflen);
  3449. // We can only set the upgrade buffer once we've gotten our message
  3450. // completion callback.
  3451. if (messages[0].message_complete_cb_called &&
  3452. msg->upgrade &&
  3453. parser->upgrade) {
  3454. messages[0].upgrade = buf + nread;
  3455. goto test;
  3456. }
  3457. if (nread < buflen) {
  3458. // Not much do to if we failed a strict-mode check
  3459. if (HTTP_PARSER_ERRNO(parser) == HPE_STRICT) {
  3460. parser_free();
  3461. return;
  3462. }
  3463. assert (HTTP_PARSER_ERRNO(parser) == HPE_PAUSED);
  3464. }
  3465. buf += nread;
  3466. buflen -= nread;
  3467. http_parser_pause(parser, 0);
  3468. } while (buflen > 0);
  3469. nread = parse_pause(NULL, 0);
  3470. assert (nread == 0);
  3471. test:
  3472. if (num_messages != 1) {
  3473. printf("\n*** num_messages != 1 after testing '%s' ***\n\n", msg->name);
  3474. abort();
  3475. }
  3476. if(!message_eq(0, 0, msg)) abort();
  3477. parser_free();
  3478. }
  3479. /* Verify that body and next message won't be parsed in responses to CONNECT */
  3480. void
  3481. test_message_connect (const struct message *msg)
  3482. {
  3483. char *buf = (char*) msg->raw;
  3484. size_t buflen = strlen(msg->raw);
  3485. parser_init(msg->type);
  3486. parse_connect(buf, buflen);
  3487. if (num_messages != 1) {
  3488. printf("\n*** num_messages != 1 after testing '%s' ***\n\n", msg->name);
  3489. abort();
  3490. }
  3491. if(!message_eq(0, 1, msg)) abort();
  3492. parser_free();
  3493. }
  3494. int
  3495. main (void)
  3496. {
  3497. parser = NULL;
  3498. int i, j, k;
  3499. int request_count;
  3500. int response_count;
  3501. unsigned long version;
  3502. unsigned major;
  3503. unsigned minor;
  3504. unsigned patch;
  3505. version = http_parser_version();
  3506. major = (version >> 16) & 255;
  3507. minor = (version >> 8) & 255;
  3508. patch = version & 255;
  3509. printf("http_parser v%u.%u.%u (0x%06lx)\n", major, minor, patch, version);
  3510. printf("sizeof(http_parser) = %u\n", (unsigned int)sizeof(http_parser));
  3511. for (request_count = 0; requests[request_count].name; request_count++);
  3512. for (response_count = 0; responses[response_count].name; response_count++);
  3513. //// API
  3514. test_preserve_data();
  3515. test_parse_url();
  3516. test_method_str();
  3517. //// NREAD
  3518. test_header_nread_value();
  3519. //// OVERFLOW CONDITIONS
  3520. test_header_overflow_error(HTTP_REQUEST);
  3521. test_no_overflow_long_body(HTTP_REQUEST, 1000);
  3522. test_no_overflow_long_body(HTTP_REQUEST, 100000);
  3523. test_header_overflow_error(HTTP_RESPONSE);
  3524. test_no_overflow_long_body(HTTP_RESPONSE, 1000);
  3525. test_no_overflow_long_body(HTTP_RESPONSE, 100000);
  3526. test_header_content_length_overflow_error();
  3527. test_chunk_content_length_overflow_error();
  3528. //// HEADER FIELD CONDITIONS
  3529. test_double_content_length_error(HTTP_REQUEST);
  3530. test_chunked_content_length_error(HTTP_REQUEST);
  3531. test_header_cr_no_lf_error(HTTP_REQUEST);
  3532. test_invalid_header_field_token_error(HTTP_REQUEST);
  3533. test_invalid_header_field_content_error(HTTP_REQUEST);
  3534. test_double_content_length_error(HTTP_RESPONSE);
  3535. test_chunked_content_length_error(HTTP_RESPONSE);
  3536. test_header_cr_no_lf_error(HTTP_RESPONSE);
  3537. test_invalid_header_field_token_error(HTTP_RESPONSE);
  3538. test_invalid_header_field_content_error(HTTP_RESPONSE);
  3539. //// RESPONSES
  3540. for (i = 0; i < response_count; i++) {
  3541. test_message(&responses[i]);
  3542. }
  3543. for (i = 0; i < response_count; i++) {
  3544. test_message_pause(&responses[i]);
  3545. }
  3546. for (i = 0; i < response_count; i++) {
  3547. test_message_connect(&responses[i]);
  3548. }
  3549. for (i = 0; i < response_count; i++) {
  3550. if (!responses[i].should_keep_alive) continue;
  3551. for (j = 0; j < response_count; j++) {
  3552. if (!responses[j].should_keep_alive) continue;
  3553. for (k = 0; k < response_count; k++) {
  3554. test_multiple3(&responses[i], &responses[j], &responses[k]);
  3555. }
  3556. }
  3557. }
  3558. test_message_count_body(&responses[NO_HEADERS_NO_BODY_404]);
  3559. test_message_count_body(&responses[TRAILING_SPACE_ON_CHUNKED_BODY]);
  3560. // test very large chunked response
  3561. {
  3562. char * msg = create_large_chunked_message(31337,
  3563. "HTTP/1.0 200 OK\r\n"
  3564. "Transfer-Encoding: chunked\r\n"
  3565. "Content-Type: text/plain\r\n"
  3566. "\r\n");
  3567. struct message large_chunked =
  3568. {.name= "large chunked"
  3569. ,.type= HTTP_RESPONSE
  3570. ,.raw= msg
  3571. ,.should_keep_alive= FALSE
  3572. ,.message_complete_on_eof= FALSE
  3573. ,.http_major= 1
  3574. ,.http_minor= 0
  3575. ,.status_code= 200
  3576. ,.response_status= "OK"
  3577. ,.num_headers= 2
  3578. ,.headers=
  3579. { { "Transfer-Encoding", "chunked" }
  3580. , { "Content-Type", "text/plain" }
  3581. }
  3582. ,.body_size= 31337*1024
  3583. ,.num_chunks_complete= 31338
  3584. };
  3585. for (i = 0; i < MAX_CHUNKS; i++) {
  3586. large_chunked.chunk_lengths[i] = 1024;
  3587. }
  3588. test_message_count_body(&large_chunked);
  3589. free(msg);
  3590. }
  3591. printf("response scan 1/2 ");
  3592. test_scan( &responses[TRAILING_SPACE_ON_CHUNKED_BODY]
  3593. , &responses[NO_BODY_HTTP10_KA_204]
  3594. , &responses[NO_REASON_PHRASE]
  3595. );
  3596. printf("response scan 2/2 ");
  3597. test_scan( &responses[BONJOUR_MADAME_FR]
  3598. , &responses[UNDERSTORE_HEADER_KEY]
  3599. , &responses[NO_CARRIAGE_RET]
  3600. );
  3601. puts("responses okay");
  3602. /// REQUESTS
  3603. test_simple("GET / HTP/1.1\r\n\r\n", HPE_INVALID_VERSION);
  3604. // Extended characters - see nodejs/test/parallel/test-http-headers-obstext.js
  3605. test_simple("GET / HTTP/1.1\r\n"
  3606. "Test: Düsseldorf\r\n",
  3607. HPE_OK);
  3608. // Well-formed but incomplete
  3609. test_simple("GET / HTTP/1.1\r\n"
  3610. "Content-Type: text/plain\r\n"
  3611. "Content-Length: 6\r\n"
  3612. "\r\n"
  3613. "fooba",
  3614. HPE_OK);
  3615. static const char *all_methods[] = {
  3616. "DELETE",
  3617. "GET",
  3618. "HEAD",
  3619. "POST",
  3620. "PUT",
  3621. //"CONNECT", //CONNECT can't be tested like other methods, it's a tunnel
  3622. "OPTIONS",
  3623. "TRACE",
  3624. "COPY",
  3625. "LOCK",
  3626. "MKCOL",
  3627. "MOVE",
  3628. "PROPFIND",
  3629. "PROPPATCH",
  3630. "SEARCH",
  3631. "UNLOCK",
  3632. "BIND",
  3633. "REBIND",
  3634. "UNBIND",
  3635. "ACL",
  3636. "REPORT",
  3637. "MKACTIVITY",
  3638. "CHECKOUT",
  3639. "MERGE",
  3640. "M-SEARCH",
  3641. "NOTIFY",
  3642. "SUBSCRIBE",
  3643. "UNSUBSCRIBE",
  3644. "PATCH",
  3645. "PURGE",
  3646. "MKCALENDAR",
  3647. "LINK",
  3648. "UNLINK",
  3649. 0 };
  3650. const char **this_method;
  3651. for (this_method = all_methods; *this_method; this_method++) {
  3652. char buf[200];
  3653. sprintf(buf, "%s / HTTP/1.1\r\n\r\n", *this_method);
  3654. test_simple(buf, HPE_OK);
  3655. }
  3656. static const char *bad_methods[] = {
  3657. "ASDF",
  3658. "C******",
  3659. "COLA",
  3660. "GEM",
  3661. "GETA",
  3662. "M****",
  3663. "MKCOLA",
  3664. "PROPPATCHA",
  3665. "PUN",
  3666. "PX",
  3667. "SA",
  3668. "hello world",
  3669. 0 };
  3670. for (this_method = bad_methods; *this_method; this_method++) {
  3671. char buf[200];
  3672. sprintf(buf, "%s / HTTP/1.1\r\n\r\n", *this_method);
  3673. test_simple(buf, HPE_INVALID_METHOD);
  3674. }
  3675. // illegal header field name line folding
  3676. test_simple("GET / HTTP/1.1\r\n"
  3677. "name\r\n"
  3678. " : value\r\n"
  3679. "\r\n",
  3680. HPE_INVALID_HEADER_TOKEN);
  3681. const char *dumbfuck2 =
  3682. "GET / HTTP/1.1\r\n"
  3683. "X-SSL-Bullshit: -----BEGIN CERTIFICATE-----\r\n"
  3684. "\tMIIFbTCCBFWgAwIBAgICH4cwDQYJKoZIhvcNAQEFBQAwcDELMAkGA1UEBhMCVUsx\r\n"
  3685. "\tETAPBgNVBAoTCGVTY2llbmNlMRIwEAYDVQQLEwlBdXRob3JpdHkxCzAJBgNVBAMT\r\n"
  3686. "\tAkNBMS0wKwYJKoZIhvcNAQkBFh5jYS1vcGVyYXRvckBncmlkLXN1cHBvcnQuYWMu\r\n"
  3687. "\tdWswHhcNMDYwNzI3MTQxMzI4WhcNMDcwNzI3MTQxMzI4WjBbMQswCQYDVQQGEwJV\r\n"
  3688. "\tSzERMA8GA1UEChMIZVNjaWVuY2UxEzARBgNVBAsTCk1hbmNoZXN0ZXIxCzAJBgNV\r\n"
  3689. "\tBAcTmrsogriqMWLAk1DMRcwFQYDVQQDEw5taWNoYWVsIHBhcmQYJKoZIhvcNAQEB\r\n"
  3690. "\tBQADggEPADCCAQoCggEBANPEQBgl1IaKdSS1TbhF3hEXSl72G9J+WC/1R64fAcEF\r\n"
  3691. "\tW51rEyFYiIeZGx/BVzwXbeBoNUK41OK65sxGuflMo5gLflbwJtHBRIEKAfVVp3YR\r\n"
  3692. "\tgW7cMA/s/XKgL1GEC7rQw8lIZT8RApukCGqOVHSi/F1SiFlPDxuDfmdiNzL31+sL\r\n"
  3693. "\t0iwHDdNkGjy5pyBSB8Y79dsSJtCW/iaLB0/n8Sj7HgvvZJ7x0fr+RQjYOUUfrePP\r\n"
  3694. "\tu2MSpFyf+9BbC/aXgaZuiCvSR+8Snv3xApQY+fULK/xY8h8Ua51iXoQ5jrgu2SqR\r\n"
  3695. "\twgA7BUi3G8LFzMBl8FRCDYGUDy7M6QaHXx1ZWIPWNKsCAwEAAaOCAiQwggIgMAwG\r\n"
  3696. "\tA1UdEwEB/wQCMAAwEQYJYIZIAYb4QgHTTPAQDAgWgMA4GA1UdDwEB/wQEAwID6DAs\r\n"
  3697. "\tBglghkgBhvhCAQ0EHxYdVUsgZS1TY2llbmNlIFVzZXIgQ2VydGlmaWNhdGUwHQYD\r\n"
  3698. "\tVR0OBBYEFDTt/sf9PeMaZDHkUIldrDYMNTBZMIGaBgNVHSMEgZIwgY+AFAI4qxGj\r\n"
  3699. "\tloCLDdMVKwiljjDastqooXSkcjBwMQswCQYDVQQGEwJVSzERMA8GA1UEChMIZVNj\r\n"
  3700. "\taWVuY2UxEjAQBgNVBAsTCUF1dGhvcml0eTELMAkGA1UEAxMCQ0ExLTArBgkqhkiG\r\n"
  3701. "\t9w0BCQEWHmNhLW9wZXJhdG9yQGdyaWQtc3VwcG9ydC5hYy51a4IBADApBgNVHRIE\r\n"
  3702. "\tIjAggR5jYS1vcGVyYXRvckBncmlkLXN1cHBvcnQuYWMudWswGQYDVR0gBBIwEDAO\r\n"
  3703. "\tBgwrBgEEAdkvAQEBAQYwPQYJYIZIAYb4QgEEBDAWLmh0dHA6Ly9jYS5ncmlkLXN1\r\n"
  3704. "\tcHBvcnQuYWMudmT4sopwqlBWsvcHViL2NybC9jYWNybC5jcmwwPQYJYIZIAYb4QgEDBDAWLmh0\r\n"
  3705. "\tdHA6Ly9jYS5ncmlkLXN1cHBvcnQuYWMudWsvcHViL2NybC9jYWNybC5jcmwwPwYD\r\n"
  3706. "\tVR0fBDgwNjA0oDKgMIYuaHR0cDovL2NhLmdyaWQt5hYy51ay9wdWIv\r\n"
  3707. "\tY3JsL2NhY3JsLmNybDANBgkqhkiG9w0BAQUFAAOCAQEAS/U4iiooBENGW/Hwmmd3\r\n"
  3708. "\tXCy6Zrt08YjKCzGNjorT98g8uGsqYjSxv/hmi0qlnlHs+k/3Iobc3LjS5AMYr5L8\r\n"
  3709. "\tUO7OSkgFFlLHQyC9JzPfmLCAugvzEbyv4Olnsr8hbxF1MbKZoQxUZtMVu29wjfXk\r\n"
  3710. "\thTeApBv7eaKCWpSp7MCbvgzm74izKhu3vlDk9w6qVrxePfGgpKPqfHiOoGhFnbTK\r\n"
  3711. "\twTC6o2xq5y0qZ03JonF7OJspEd3I5zKY3E+ov7/ZhW6DqT8UFvsAdjvQbXyhV8Eu\r\n"
  3712. "\tYhixw1aKEPzNjNowuIseVogKOLXxWI5vAi5HgXdS0/ES5gDGsABo4fqovUKlgop3\r\n"
  3713. "\tRA==\r\n"
  3714. "\t-----END CERTIFICATE-----\r\n"
  3715. "\r\n";
  3716. test_simple(dumbfuck2, HPE_OK);
  3717. const char *corrupted_connection =
  3718. "GET / HTTP/1.1\r\n"
  3719. "Host: www.example.com\r\n"
  3720. "Connection\r\033\065\325eep-Alive\r\n"
  3721. "Accept-Encoding: gzip\r\n"
  3722. "\r\n";
  3723. test_simple(corrupted_connection, HPE_INVALID_HEADER_TOKEN);
  3724. const char *corrupted_header_name =
  3725. "GET / HTTP/1.1\r\n"
  3726. "Host: www.example.com\r\n"
  3727. "X-Some-Header\r\033\065\325eep-Alive\r\n"
  3728. "Accept-Encoding: gzip\r\n"
  3729. "\r\n";
  3730. test_simple(corrupted_header_name, HPE_INVALID_HEADER_TOKEN);
  3731. #if 0
  3732. // NOTE(Wed Nov 18 11:57:27 CET 2009) this seems okay. we just read body
  3733. // until EOF.
  3734. //
  3735. // no content-length
  3736. // error if there is a body without content length
  3737. const char *bad_get_no_headers_no_body = "GET /bad_get_no_headers_no_body/world HTTP/1.1\r\n"
  3738. "Accept: */*\r\n"
  3739. "\r\n"
  3740. "HELLO";
  3741. test_simple(bad_get_no_headers_no_body, 0);
  3742. #endif
  3743. /* TODO sending junk and large headers gets rejected */
  3744. /* check to make sure our predefined requests are okay */
  3745. for (i = 0; requests[i].name; i++) {
  3746. test_message(&requests[i]);
  3747. }
  3748. for (i = 0; i < request_count; i++) {
  3749. test_message_pause(&requests[i]);
  3750. }
  3751. for (i = 0; i < request_count; i++) {
  3752. if (!requests[i].should_keep_alive) continue;
  3753. for (j = 0; j < request_count; j++) {
  3754. if (!requests[j].should_keep_alive) continue;
  3755. for (k = 0; k < request_count; k++) {
  3756. test_multiple3(&requests[i], &requests[j], &requests[k]);
  3757. }
  3758. }
  3759. }
  3760. printf("request scan 1/4 ");
  3761. test_scan( &requests[GET_NO_HEADERS_NO_BODY]
  3762. , &requests[GET_ONE_HEADER_NO_BODY]
  3763. , &requests[GET_NO_HEADERS_NO_BODY]
  3764. );
  3765. printf("request scan 2/4 ");
  3766. test_scan( &requests[POST_CHUNKED_ALL_YOUR_BASE]
  3767. , &requests[POST_IDENTITY_BODY_WORLD]
  3768. , &requests[GET_FUNKY_CONTENT_LENGTH]
  3769. );
  3770. printf("request scan 3/4 ");
  3771. test_scan( &requests[TWO_CHUNKS_MULT_ZERO_END]
  3772. , &requests[CHUNKED_W_TRAILING_HEADERS]
  3773. , &requests[CHUNKED_W_BULLSHIT_AFTER_LENGTH]
  3774. );
  3775. printf("request scan 4/4 ");
  3776. test_scan( &requests[QUERY_URL_WITH_QUESTION_MARK_GET]
  3777. , &requests[PREFIX_NEWLINE_GET ]
  3778. , &requests[CONNECT_REQUEST]
  3779. );
  3780. puts("requests okay");
  3781. return 0;
  3782. }