| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425 | /* Copyright Joyent, Inc. and other Node contributors. All rights reserved. * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to * deal in the Software without restriction, including without limitation the * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or * sell copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS * IN THE SOFTWARE. */#include "http_parser.h"#include <stdlib.h>#include <assert.h>#include <stdio.h>#include <stdlib.h> /* rand */#include <string.h>#include <stdarg.h>#undef TRUE#define TRUE 1#undef FALSE#define FALSE 0#define MAX_HEADERS 13#define MAX_ELEMENT_SIZE 2048#define MIN(a,b) ((a) < (b) ? (a) : (b))static http_parser *parser;struct message {  const char *name; // for debugging purposes  const char *raw;  enum http_parser_type type;  enum http_method method;  int status_code;  char request_path[MAX_ELEMENT_SIZE];  char request_url[MAX_ELEMENT_SIZE];  char fragment[MAX_ELEMENT_SIZE];  char query_string[MAX_ELEMENT_SIZE];  char body[MAX_ELEMENT_SIZE];  size_t body_size;  const char *host;  const char *userinfo;  uint16_t port;  int num_headers;  enum { NONE=0, FIELD, VALUE } last_header_element;  char headers [MAX_HEADERS][2][MAX_ELEMENT_SIZE];  int should_keep_alive;  const char *upgrade; // upgraded body  unsigned short http_major;  unsigned short http_minor;  int message_begin_cb_called;  int headers_complete_cb_called;  int message_complete_cb_called;  int message_complete_on_eof;  int body_is_final;};static int currently_parsing_eof;static struct message messages[5];static int num_messages;static http_parser_settings *current_pause_parser;/* * R E Q U E S T S * */const struct message requests[] =#define CURL_GET 0{ {.name= "curl get"  ,.type= HTTP_REQUEST  ,.raw= "GET /test HTTP/1.1\r\n"         "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"         "Host: 0.0.0.0=5000\r\n"         "Accept: */*\r\n"         "\r\n"  ,.should_keep_alive= TRUE  ,.message_complete_on_eof= FALSE  ,.http_major= 1  ,.http_minor= 1  ,.method= HTTP_GET  ,.query_string= ""  ,.fragment= ""  ,.request_path= "/test"  ,.request_url= "/test"  ,.num_headers= 3  ,.headers=    { { "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" }    , { "Host", "0.0.0.0=5000" }    , { "Accept", "*/*" }    }  ,.body= ""  }#define FIREFOX_GET 1, {.name= "firefox get"  ,.type= HTTP_REQUEST  ,.raw= "GET /favicon.ico HTTP/1.1\r\n"         "Host: 0.0.0.0=5000\r\n"         "User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9) Gecko/2008061015 Firefox/3.0\r\n"         "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\n"         "Accept-Language: en-us,en;q=0.5\r\n"         "Accept-Encoding: gzip,deflate\r\n"         "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7\r\n"         "Keep-Alive: 300\r\n"         "Connection: keep-alive\r\n"         "\r\n"  ,.should_keep_alive= TRUE  ,.message_complete_on_eof= FALSE  ,.http_major= 1  ,.http_minor= 1  ,.method= HTTP_GET  ,.query_string= ""  ,.fragment= ""  ,.request_path= "/favicon.ico"  ,.request_url= "/favicon.ico"  ,.num_headers= 8  ,.headers=    { { "Host", "0.0.0.0=5000" }    , { "User-Agent", "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9) Gecko/2008061015 Firefox/3.0" }    , { "Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8" }    , { "Accept-Language", "en-us,en;q=0.5" }    , { "Accept-Encoding", "gzip,deflate" }    , { "Accept-Charset", "ISO-8859-1,utf-8;q=0.7,*;q=0.7" }    , { "Keep-Alive", "300" }    , { "Connection", "keep-alive" }    }  ,.body= ""  }#define DUMBFUCK 2, {.name= "dumbfuck"  ,.type= HTTP_REQUEST  ,.raw= "GET /dumbfuck HTTP/1.1\r\n"         "aaaaaaaaaaaaa:++++++++++\r\n"         "\r\n"  ,.should_keep_alive= TRUE  ,.message_complete_on_eof= FALSE  ,.http_major= 1  ,.http_minor= 1  ,.method= HTTP_GET  ,.query_string= ""  ,.fragment= ""  ,.request_path= "/dumbfuck"  ,.request_url= "/dumbfuck"  ,.num_headers= 1  ,.headers=    { { "aaaaaaaaaaaaa",  "++++++++++" }    }  ,.body= ""  }#define FRAGMENT_IN_URI 3, {.name= "fragment in url"  ,.type= HTTP_REQUEST  ,.raw= "GET /forums/1/topics/2375?page=1#posts-17408 HTTP/1.1\r\n"         "\r\n"  ,.should_keep_alive= TRUE  ,.message_complete_on_eof= FALSE  ,.http_major= 1  ,.http_minor= 1  ,.method= HTTP_GET  ,.query_string= "page=1"  ,.fragment= "posts-17408"  ,.request_path= "/forums/1/topics/2375"  /* XXX request url does include fragment? */  ,.request_url= "/forums/1/topics/2375?page=1#posts-17408"  ,.num_headers= 0  ,.body= ""  }#define GET_NO_HEADERS_NO_BODY 4, {.name= "get no headers no body"  ,.type= HTTP_REQUEST  ,.raw= "GET /get_no_headers_no_body/world HTTP/1.1\r\n"         "\r\n"  ,.should_keep_alive= TRUE  ,.message_complete_on_eof= FALSE /* would need Connection: close */  ,.http_major= 1  ,.http_minor= 1  ,.method= HTTP_GET  ,.query_string= ""  ,.fragment= ""  ,.request_path= "/get_no_headers_no_body/world"  ,.request_url= "/get_no_headers_no_body/world"  ,.num_headers= 0  ,.body= ""  }#define GET_ONE_HEADER_NO_BODY 5, {.name= "get one header no body"  ,.type= HTTP_REQUEST  ,.raw= "GET /get_one_header_no_body HTTP/1.1\r\n"         "Accept: */*\r\n"         "\r\n"  ,.should_keep_alive= TRUE  ,.message_complete_on_eof= FALSE /* would need Connection: close */  ,.http_major= 1  ,.http_minor= 1  ,.method= HTTP_GET  ,.query_string= ""  ,.fragment= ""  ,.request_path= "/get_one_header_no_body"  ,.request_url= "/get_one_header_no_body"  ,.num_headers= 1  ,.headers=    { { "Accept" , "*/*" }    }  ,.body= ""  }#define GET_FUNKY_CONTENT_LENGTH 6, {.name= "get funky content length body hello"  ,.type= HTTP_REQUEST  ,.raw= "GET /get_funky_content_length_body_hello HTTP/1.0\r\n"         "conTENT-Length: 5\r\n"         "\r\n"         "HELLO"  ,.should_keep_alive= FALSE  ,.message_complete_on_eof= FALSE  ,.http_major= 1  ,.http_minor= 0  ,.method= HTTP_GET  ,.query_string= ""  ,.fragment= ""  ,.request_path= "/get_funky_content_length_body_hello"  ,.request_url= "/get_funky_content_length_body_hello"  ,.num_headers= 1  ,.headers=    { { "conTENT-Length" , "5" }    }  ,.body= "HELLO"  }#define POST_IDENTITY_BODY_WORLD 7, {.name= "post identity body world"  ,.type= HTTP_REQUEST  ,.raw= "POST /post_identity_body_world?q=search#hey HTTP/1.1\r\n"         "Accept: */*\r\n"         "Transfer-Encoding: identity\r\n"         "Content-Length: 5\r\n"         "\r\n"         "World"  ,.should_keep_alive= TRUE  ,.message_complete_on_eof= FALSE  ,.http_major= 1  ,.http_minor= 1  ,.method= HTTP_POST  ,.query_string= "q=search"  ,.fragment= "hey"  ,.request_path= "/post_identity_body_world"  ,.request_url= "/post_identity_body_world?q=search#hey"  ,.num_headers= 3  ,.headers=    { { "Accept", "*/*" }    , { "Transfer-Encoding", "identity" }    , { "Content-Length", "5" }    }  ,.body= "World"  }#define POST_CHUNKED_ALL_YOUR_BASE 8, {.name= "post - chunked body: all your base are belong to us"  ,.type= HTTP_REQUEST  ,.raw= "POST /post_chunked_all_your_base HTTP/1.1\r\n"         "Transfer-Encoding: chunked\r\n"         "\r\n"         "1e\r\nall your base are belong to us\r\n"         "0\r\n"         "\r\n"  ,.should_keep_alive= TRUE  ,.message_complete_on_eof= FALSE  ,.http_major= 1  ,.http_minor= 1  ,.method= HTTP_POST  ,.query_string= ""  ,.fragment= ""  ,.request_path= "/post_chunked_all_your_base"  ,.request_url= "/post_chunked_all_your_base"  ,.num_headers= 1  ,.headers=    { { "Transfer-Encoding" , "chunked" }    }  ,.body= "all your base are belong to us"  }#define TWO_CHUNKS_MULT_ZERO_END 9, {.name= "two chunks ; triple zero ending"  ,.type= HTTP_REQUEST  ,.raw= "POST /two_chunks_mult_zero_end HTTP/1.1\r\n"         "Transfer-Encoding: chunked\r\n"         "\r\n"         "5\r\nhello\r\n"         "6\r\n world\r\n"         "000\r\n"         "\r\n"  ,.should_keep_alive= TRUE  ,.message_complete_on_eof= FALSE  ,.http_major= 1  ,.http_minor= 1  ,.method= HTTP_POST  ,.query_string= ""  ,.fragment= ""  ,.request_path= "/two_chunks_mult_zero_end"  ,.request_url= "/two_chunks_mult_zero_end"  ,.num_headers= 1  ,.headers=    { { "Transfer-Encoding", "chunked" }    }  ,.body= "hello world"  }#define CHUNKED_W_TRAILING_HEADERS 10, {.name= "chunked with trailing headers. blech."  ,.type= HTTP_REQUEST  ,.raw= "POST /chunked_w_trailing_headers HTTP/1.1\r\n"         "Transfer-Encoding: chunked\r\n"         "\r\n"         "5\r\nhello\r\n"         "6\r\n world\r\n"         "0\r\n"         "Vary: *\r\n"         "Content-Type: text/plain\r\n"         "\r\n"  ,.should_keep_alive= TRUE  ,.message_complete_on_eof= FALSE  ,.http_major= 1  ,.http_minor= 1  ,.method= HTTP_POST  ,.query_string= ""  ,.fragment= ""  ,.request_path= "/chunked_w_trailing_headers"  ,.request_url= "/chunked_w_trailing_headers"  ,.num_headers= 3  ,.headers=    { { "Transfer-Encoding",  "chunked" }    , { "Vary", "*" }    , { "Content-Type", "text/plain" }    }  ,.body= "hello world"  }#define CHUNKED_W_BULLSHIT_AFTER_LENGTH 11, {.name= "with bullshit after the length"  ,.type= HTTP_REQUEST  ,.raw= "POST /chunked_w_bullshit_after_length HTTP/1.1\r\n"         "Transfer-Encoding: chunked\r\n"         "\r\n"         "5; ihatew3;whatthefuck=aretheseparametersfor\r\nhello\r\n"         "6; blahblah; blah\r\n world\r\n"         "0\r\n"         "\r\n"  ,.should_keep_alive= TRUE  ,.message_complete_on_eof= FALSE  ,.http_major= 1  ,.http_minor= 1  ,.method= HTTP_POST  ,.query_string= ""  ,.fragment= ""  ,.request_path= "/chunked_w_bullshit_after_length"  ,.request_url= "/chunked_w_bullshit_after_length"  ,.num_headers= 1  ,.headers=    { { "Transfer-Encoding", "chunked" }    }  ,.body= "hello world"  }#define WITH_QUOTES 12, {.name= "with quotes"  ,.type= HTTP_REQUEST  ,.raw= "GET /with_\"stupid\"_quotes?foo=\"bar\" HTTP/1.1\r\n\r\n"  ,.should_keep_alive= TRUE  ,.message_complete_on_eof= FALSE  ,.http_major= 1  ,.http_minor= 1  ,.method= HTTP_GET  ,.query_string= "foo=\"bar\""  ,.fragment= ""  ,.request_path= "/with_\"stupid\"_quotes"  ,.request_url= "/with_\"stupid\"_quotes?foo=\"bar\""  ,.num_headers= 0  ,.headers= { }  ,.body= ""  }#define APACHEBENCH_GET 13/* The server receiving this request SHOULD NOT wait for EOF * to know that content-length == 0. * How to represent this in a unit test? message_complete_on_eof * Compare with NO_CONTENT_LENGTH_RESPONSE. */, {.name = "apachebench get"  ,.type= HTTP_REQUEST  ,.raw= "GET /test HTTP/1.0\r\n"         "Host: 0.0.0.0:5000\r\n"         "User-Agent: ApacheBench/2.3\r\n"         "Accept: */*\r\n\r\n"  ,.should_keep_alive= FALSE  ,.message_complete_on_eof= FALSE  ,.http_major= 1  ,.http_minor= 0  ,.method= HTTP_GET  ,.query_string= ""  ,.fragment= ""  ,.request_path= "/test"  ,.request_url= "/test"  ,.num_headers= 3  ,.headers= { { "Host", "0.0.0.0:5000" }             , { "User-Agent", "ApacheBench/2.3" }             , { "Accept", "*/*" }             }  ,.body= ""  }#define QUERY_URL_WITH_QUESTION_MARK_GET 14/* Some clients include '?' characters in query strings. */, {.name = "query url with question mark"  ,.type= HTTP_REQUEST  ,.raw= "GET /test.cgi?foo=bar?baz HTTP/1.1\r\n\r\n"  ,.should_keep_alive= TRUE  ,.message_complete_on_eof= FALSE  ,.http_major= 1  ,.http_minor= 1  ,.method= HTTP_GET  ,.query_string= "foo=bar?baz"  ,.fragment= ""  ,.request_path= "/test.cgi"  ,.request_url= "/test.cgi?foo=bar?baz"  ,.num_headers= 0  ,.headers= {}  ,.body= ""  }#define PREFIX_NEWLINE_GET 15/* Some clients, especially after a POST in a keep-alive connection, * will send an extra CRLF before the next request */, {.name = "newline prefix get"  ,.type= HTTP_REQUEST  ,.raw= "\r\nGET /test HTTP/1.1\r\n\r\n"  ,.should_keep_alive= TRUE  ,.message_complete_on_eof= FALSE  ,.http_major= 1  ,.http_minor= 1  ,.method= HTTP_GET  ,.query_string= ""  ,.fragment= ""  ,.request_path= "/test"  ,.request_url= "/test"  ,.num_headers= 0  ,.headers= { }  ,.body= ""  }#define UPGRADE_REQUEST 16, {.name = "upgrade request"  ,.type= HTTP_REQUEST  ,.raw= "GET /demo HTTP/1.1\r\n"         "Host: example.com\r\n"         "Connection: Upgrade\r\n"         "Sec-WebSocket-Key2: 12998 5 Y3 1  .P00\r\n"         "Sec-WebSocket-Protocol: sample\r\n"         "Upgrade: WebSocket\r\n"         "Sec-WebSocket-Key1: 4 @1  46546xW%0l 1 5\r\n"         "Origin: http://example.com\r\n"         "\r\n"         "Hot diggity dogg"  ,.should_keep_alive= TRUE  ,.message_complete_on_eof= FALSE  ,.http_major= 1  ,.http_minor= 1  ,.method= HTTP_GET  ,.query_string= ""  ,.fragment= ""  ,.request_path= "/demo"  ,.request_url= "/demo"  ,.num_headers= 7  ,.upgrade="Hot diggity dogg"  ,.headers= { { "Host", "example.com" }             , { "Connection", "Upgrade" }             , { "Sec-WebSocket-Key2", "12998 5 Y3 1  .P00" }             , { "Sec-WebSocket-Protocol", "sample" }             , { "Upgrade", "WebSocket" }             , { "Sec-WebSocket-Key1", "4 @1  46546xW%0l 1 5" }             , { "Origin", "http://example.com" }             }  ,.body= ""  }#define CONNECT_REQUEST 17, {.name = "connect request"  ,.type= HTTP_REQUEST  ,.raw= "CONNECT 0-home0.netscape.com:443 HTTP/1.0\r\n"         "User-agent: Mozilla/1.1N\r\n"         "Proxy-authorization: basic aGVsbG86d29ybGQ=\r\n"         "\r\n"         "some data\r\n"         "and yet even more data"  ,.should_keep_alive= FALSE  ,.message_complete_on_eof= FALSE  ,.http_major= 1  ,.http_minor= 0  ,.method= HTTP_CONNECT  ,.query_string= ""  ,.fragment= ""  ,.request_path= ""  ,.request_url= "0-home0.netscape.com:443"  ,.num_headers= 2  ,.upgrade="some data\r\nand yet even more data"  ,.headers= { { "User-agent", "Mozilla/1.1N" }             , { "Proxy-authorization", "basic aGVsbG86d29ybGQ=" }             }  ,.body= ""  }#define REPORT_REQ 18, {.name= "report request"  ,.type= HTTP_REQUEST  ,.raw= "REPORT /test HTTP/1.1\r\n"         "\r\n"  ,.should_keep_alive= TRUE  ,.message_complete_on_eof= FALSE  ,.http_major= 1  ,.http_minor= 1  ,.method= HTTP_REPORT  ,.query_string= ""  ,.fragment= ""  ,.request_path= "/test"  ,.request_url= "/test"  ,.num_headers= 0  ,.headers= {}  ,.body= ""  }#define NO_HTTP_VERSION 19, {.name= "request with no http version"  ,.type= HTTP_REQUEST  ,.raw= "GET /\r\n"         "\r\n"  ,.should_keep_alive= FALSE  ,.message_complete_on_eof= FALSE  ,.http_major= 0  ,.http_minor= 9  ,.method= HTTP_GET  ,.query_string= ""  ,.fragment= ""  ,.request_path= "/"  ,.request_url= "/"  ,.num_headers= 0  ,.headers= {}  ,.body= ""  }#define MSEARCH_REQ 20, {.name= "m-search request"  ,.type= HTTP_REQUEST  ,.raw= "M-SEARCH * HTTP/1.1\r\n"         "HOST: 239.255.255.250:1900\r\n"         "MAN: \"ssdp:discover\"\r\n"         "ST: \"ssdp:all\"\r\n"         "\r\n"  ,.should_keep_alive= TRUE  ,.message_complete_on_eof= FALSE  ,.http_major= 1  ,.http_minor= 1  ,.method= HTTP_MSEARCH  ,.query_string= ""  ,.fragment= ""  ,.request_path= "*"  ,.request_url= "*"  ,.num_headers= 3  ,.headers= { { "HOST", "239.255.255.250:1900" }             , { "MAN", "\"ssdp:discover\"" }             , { "ST", "\"ssdp:all\"" }             }  ,.body= ""  }#define LINE_FOLDING_IN_HEADER 21, {.name= "line folding in header value"  ,.type= HTTP_REQUEST  ,.raw= "GET / HTTP/1.1\r\n"         "Line1:   abc\r\n"         "\tdef\r\n"         " ghi\r\n"         "\t\tjkl\r\n"         "  mno \r\n"         "\t \tqrs\r\n"         "Line2: \t line2\t\r\n"         "\r\n"  ,.should_keep_alive= TRUE  ,.message_complete_on_eof= FALSE  ,.http_major= 1  ,.http_minor= 1  ,.method= HTTP_GET  ,.query_string= ""  ,.fragment= ""  ,.request_path= "/"  ,.request_url= "/"  ,.num_headers= 2  ,.headers= { { "Line1", "abcdefghijklmno qrs" }             , { "Line2", "line2\t" }             }  ,.body= ""  }#define QUERY_TERMINATED_HOST 22, {.name= "host terminated by a query string"  ,.type= HTTP_REQUEST  ,.raw= "GET http://hypnotoad.org?hail=all HTTP/1.1\r\n"         "\r\n"  ,.should_keep_alive= TRUE  ,.message_complete_on_eof= FALSE  ,.http_major= 1  ,.http_minor= 1  ,.method= HTTP_GET  ,.query_string= "hail=all"  ,.fragment= ""  ,.request_path= ""  ,.request_url= "http://hypnotoad.org?hail=all"  ,.host= "hypnotoad.org"  ,.num_headers= 0  ,.headers= { }  ,.body= ""  }#define QUERY_TERMINATED_HOSTPORT 23, {.name= "host:port terminated by a query string"  ,.type= HTTP_REQUEST  ,.raw= "GET http://hypnotoad.org:1234?hail=all HTTP/1.1\r\n"         "\r\n"  ,.should_keep_alive= TRUE  ,.message_complete_on_eof= FALSE  ,.http_major= 1  ,.http_minor= 1  ,.method= HTTP_GET  ,.query_string= "hail=all"  ,.fragment= ""  ,.request_path= ""  ,.request_url= "http://hypnotoad.org:1234?hail=all"  ,.host= "hypnotoad.org"  ,.port= 1234  ,.num_headers= 0  ,.headers= { }  ,.body= ""  }#define SPACE_TERMINATED_HOSTPORT 24, {.name= "host:port terminated by a space"  ,.type= HTTP_REQUEST  ,.raw= "GET http://hypnotoad.org:1234 HTTP/1.1\r\n"         "\r\n"  ,.should_keep_alive= TRUE  ,.message_complete_on_eof= FALSE  ,.http_major= 1  ,.http_minor= 1  ,.method= HTTP_GET  ,.query_string= ""  ,.fragment= ""  ,.request_path= ""  ,.request_url= "http://hypnotoad.org:1234"  ,.host= "hypnotoad.org"  ,.port= 1234  ,.num_headers= 0  ,.headers= { }  ,.body= ""  }#define PATCH_REQ 25, {.name = "PATCH request"  ,.type= HTTP_REQUEST  ,.raw= "PATCH /file.txt HTTP/1.1\r\n"         "Host: www.example.com\r\n"         "Content-Type: application/example\r\n"         "If-Match: \"e0023aa4e\"\r\n"         "Content-Length: 10\r\n"         "\r\n"         "cccccccccc"  ,.should_keep_alive= TRUE  ,.message_complete_on_eof= FALSE  ,.http_major= 1  ,.http_minor= 1  ,.method= HTTP_PATCH  ,.query_string= ""  ,.fragment= ""  ,.request_path= "/file.txt"  ,.request_url= "/file.txt"  ,.num_headers= 4  ,.headers= { { "Host", "www.example.com" }             , { "Content-Type", "application/example" }             , { "If-Match", "\"e0023aa4e\"" }             , { "Content-Length", "10" }             }  ,.body= "cccccccccc"  }#define CONNECT_CAPS_REQUEST 26, {.name = "connect caps request"  ,.type= HTTP_REQUEST  ,.raw= "CONNECT HOME0.NETSCAPE.COM:443 HTTP/1.0\r\n"         "User-agent: Mozilla/1.1N\r\n"         "Proxy-authorization: basic aGVsbG86d29ybGQ=\r\n"         "\r\n"  ,.should_keep_alive= FALSE  ,.message_complete_on_eof= FALSE  ,.http_major= 1  ,.http_minor= 0  ,.method= HTTP_CONNECT  ,.query_string= ""  ,.fragment= ""  ,.request_path= ""  ,.request_url= "HOME0.NETSCAPE.COM:443"  ,.num_headers= 2  ,.upgrade=""  ,.headers= { { "User-agent", "Mozilla/1.1N" }             , { "Proxy-authorization", "basic aGVsbG86d29ybGQ=" }             }  ,.body= ""  }#if !HTTP_PARSER_STRICT#define UTF8_PATH_REQ 27, {.name= "utf-8 path request"  ,.type= HTTP_REQUEST  ,.raw= "GET /δ¶/δt/pope?q=1#narf HTTP/1.1\r\n"         "Host: github.com\r\n"         "\r\n"  ,.should_keep_alive= TRUE  ,.message_complete_on_eof= FALSE  ,.http_major= 1  ,.http_minor= 1  ,.method= HTTP_GET  ,.query_string= "q=1"  ,.fragment= "narf"  ,.request_path= "/δ¶/δt/pope"  ,.request_url= "/δ¶/δt/pope?q=1#narf"  ,.num_headers= 1  ,.headers= { {"Host", "github.com" }             }  ,.body= ""  }#define HOSTNAME_UNDERSCORE 28, {.name = "hostname underscore"  ,.type= HTTP_REQUEST  ,.raw= "CONNECT home_0.netscape.com:443 HTTP/1.0\r\n"         "User-agent: Mozilla/1.1N\r\n"         "Proxy-authorization: basic aGVsbG86d29ybGQ=\r\n"         "\r\n"  ,.should_keep_alive= FALSE  ,.message_complete_on_eof= FALSE  ,.http_major= 1  ,.http_minor= 0  ,.method= HTTP_CONNECT  ,.query_string= ""  ,.fragment= ""  ,.request_path= ""  ,.request_url= "home_0.netscape.com:443"  ,.num_headers= 2  ,.upgrade=""  ,.headers= { { "User-agent", "Mozilla/1.1N" }             , { "Proxy-authorization", "basic aGVsbG86d29ybGQ=" }             }  ,.body= ""  }#endif  /* !HTTP_PARSER_STRICT *//* see https://github.com/ry/http-parser/issues/47 */#define EAT_TRAILING_CRLF_NO_CONNECTION_CLOSE 29, {.name = "eat CRLF between requests, no \"Connection: close\" header"  ,.raw= "POST / HTTP/1.1\r\n"         "Host: www.example.com\r\n"         "Content-Type: application/x-www-form-urlencoded\r\n"         "Content-Length: 4\r\n"         "\r\n"         "q=42\r\n" /* note the trailing CRLF */  ,.should_keep_alive= TRUE  ,.message_complete_on_eof= FALSE  ,.http_major= 1  ,.http_minor= 1  ,.method= HTTP_POST  ,.query_string= ""  ,.fragment= ""  ,.request_path= "/"  ,.request_url= "/"  ,.num_headers= 3  ,.upgrade= 0  ,.headers= { { "Host", "www.example.com" }             , { "Content-Type", "application/x-www-form-urlencoded" }             , { "Content-Length", "4" }             }  ,.body= "q=42"  }/* see https://github.com/ry/http-parser/issues/47 */#define EAT_TRAILING_CRLF_WITH_CONNECTION_CLOSE 30, {.name = "eat CRLF between requests even if \"Connection: close\" is set"  ,.raw= "POST / HTTP/1.1\r\n"         "Host: www.example.com\r\n"         "Content-Type: application/x-www-form-urlencoded\r\n"         "Content-Length: 4\r\n"         "Connection: close\r\n"         "\r\n"         "q=42\r\n" /* note the trailing CRLF */  ,.should_keep_alive= FALSE  ,.message_complete_on_eof= FALSE /* input buffer isn't empty when on_message_complete is called */  ,.http_major= 1  ,.http_minor= 1  ,.method= HTTP_POST  ,.query_string= ""  ,.fragment= ""  ,.request_path= "/"  ,.request_url= "/"  ,.num_headers= 4  ,.upgrade= 0  ,.headers= { { "Host", "www.example.com" }             , { "Content-Type", "application/x-www-form-urlencoded" }             , { "Content-Length", "4" }             , { "Connection", "close" }             }  ,.body= "q=42"  }#define PURGE_REQ 31, {.name = "PURGE request"  ,.type= HTTP_REQUEST  ,.raw= "PURGE /file.txt HTTP/1.1\r\n"         "Host: www.example.com\r\n"         "\r\n"  ,.should_keep_alive= TRUE  ,.message_complete_on_eof= FALSE  ,.http_major= 1  ,.http_minor= 1  ,.method= HTTP_PURGE  ,.query_string= ""  ,.fragment= ""  ,.request_path= "/file.txt"  ,.request_url= "/file.txt"  ,.num_headers= 1  ,.headers= { { "Host", "www.example.com" } }  ,.body= ""  }#define SEARCH_REQ 32, {.name = "SEARCH request"  ,.type= HTTP_REQUEST  ,.raw= "SEARCH / HTTP/1.1\r\n"         "Host: www.example.com\r\n"         "\r\n"  ,.should_keep_alive= TRUE  ,.message_complete_on_eof= FALSE  ,.http_major= 1  ,.http_minor= 1  ,.method= HTTP_SEARCH  ,.query_string= ""  ,.fragment= ""  ,.request_path= "/"  ,.request_url= "/"  ,.num_headers= 1  ,.headers= { { "Host", "www.example.com" } }  ,.body= ""  }#define PROXY_WITH_BASIC_AUTH 33, {.name= "host:port and basic_auth"  ,.type= HTTP_REQUEST  ,.raw= "GET http://a%12:b!&*$@hypnotoad.org:1234/toto HTTP/1.1\r\n"         "\r\n"  ,.should_keep_alive= TRUE  ,.message_complete_on_eof= FALSE  ,.http_major= 1  ,.http_minor= 1  ,.method= HTTP_GET  ,.fragment= ""  ,.request_path= "/toto"  ,.request_url= "http://a%12:b!&*$@hypnotoad.org:1234/toto"  ,.host= "hypnotoad.org"  ,.userinfo= "a%12:b!&*$"  ,.port= 1234  ,.num_headers= 0  ,.headers= { }  ,.body= ""  }, {.name= NULL } /* sentinel */};/* * R E S P O N S E S * */const struct message responses[] =#define GOOGLE_301 0{ {.name= "google 301"  ,.type= HTTP_RESPONSE  ,.raw= "HTTP/1.1 301 Moved Permanently\r\n"         "Location: http://www.google.com/\r\n"         "Content-Type: text/html; charset=UTF-8\r\n"         "Date: Sun, 26 Apr 2009 11:11:49 GMT\r\n"         "Expires: Tue, 26 May 2009 11:11:49 GMT\r\n"         "X-$PrototypeBI-Version: 1.6.0.3\r\n" /* $ char in header field */         "Cache-Control: public, max-age=2592000\r\n"         "Server: gws\r\n"         "Content-Length:  219  \r\n"         "\r\n"         "<HTML><HEAD><meta http-equiv=\"content-type\" content=\"text/html;charset=utf-8\">\n"         "<TITLE>301 Moved</TITLE></HEAD><BODY>\n"         "<H1>301 Moved</H1>\n"         "The document has moved\n"         "<A HREF=\"http://www.google.com/\">here</A>.\r\n"         "</BODY></HTML>\r\n"  ,.should_keep_alive= TRUE  ,.message_complete_on_eof= FALSE  ,.http_major= 1  ,.http_minor= 1  ,.status_code= 301  ,.num_headers= 8  ,.headers=    { { "Location", "http://www.google.com/" }    , { "Content-Type", "text/html; charset=UTF-8" }    , { "Date", "Sun, 26 Apr 2009 11:11:49 GMT" }    , { "Expires", "Tue, 26 May 2009 11:11:49 GMT" }    , { "X-$PrototypeBI-Version", "1.6.0.3" }    , { "Cache-Control", "public, max-age=2592000" }    , { "Server", "gws" }    , { "Content-Length", "219  " }    }  ,.body= "<HTML><HEAD><meta http-equiv=\"content-type\" content=\"text/html;charset=utf-8\">\n"          "<TITLE>301 Moved</TITLE></HEAD><BODY>\n"          "<H1>301 Moved</H1>\n"          "The document has moved\n"          "<A HREF=\"http://www.google.com/\">here</A>.\r\n"          "</BODY></HTML>\r\n"  }#define NO_CONTENT_LENGTH_RESPONSE 1/* The client should wait for the server's EOF. That is, when content-length * is not specified, and "Connection: close", the end of body is specified * by the EOF. * Compare with APACHEBENCH_GET */, {.name= "no content-length response"  ,.type= HTTP_RESPONSE  ,.raw= "HTTP/1.1 200 OK\r\n"         "Date: Tue, 04 Aug 2009 07:59:32 GMT\r\n"         "Server: Apache\r\n"         "X-Powered-By: Servlet/2.5 JSP/2.1\r\n"         "Content-Type: text/xml; charset=utf-8\r\n"         "Connection: close\r\n"         "\r\n"         "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"         "<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\">\n"         "  <SOAP-ENV:Body>\n"         "    <SOAP-ENV:Fault>\n"         "       <faultcode>SOAP-ENV:Client</faultcode>\n"         "       <faultstring>Client Error</faultstring>\n"         "    </SOAP-ENV:Fault>\n"         "  </SOAP-ENV:Body>\n"         "</SOAP-ENV:Envelope>"  ,.should_keep_alive= FALSE  ,.message_complete_on_eof= TRUE  ,.http_major= 1  ,.http_minor= 1  ,.status_code= 200  ,.num_headers= 5  ,.headers=    { { "Date", "Tue, 04 Aug 2009 07:59:32 GMT" }    , { "Server", "Apache" }    , { "X-Powered-By", "Servlet/2.5 JSP/2.1" }    , { "Content-Type", "text/xml; charset=utf-8" }    , { "Connection", "close" }    }  ,.body= "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"          "<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\">\n"          "  <SOAP-ENV:Body>\n"          "    <SOAP-ENV:Fault>\n"          "       <faultcode>SOAP-ENV:Client</faultcode>\n"          "       <faultstring>Client Error</faultstring>\n"          "    </SOAP-ENV:Fault>\n"          "  </SOAP-ENV:Body>\n"          "</SOAP-ENV:Envelope>"  }#define NO_HEADERS_NO_BODY_404 2, {.name= "404 no headers no body"  ,.type= HTTP_RESPONSE  ,.raw= "HTTP/1.1 404 Not Found\r\n\r\n"  ,.should_keep_alive= FALSE  ,.message_complete_on_eof= TRUE  ,.http_major= 1  ,.http_minor= 1  ,.status_code= 404  ,.num_headers= 0  ,.headers= {}  ,.body_size= 0  ,.body= ""  }#define NO_REASON_PHRASE 3, {.name= "301 no response phrase"  ,.type= HTTP_RESPONSE  ,.raw= "HTTP/1.1 301\r\n\r\n"  ,.should_keep_alive = FALSE  ,.message_complete_on_eof= TRUE  ,.http_major= 1  ,.http_minor= 1  ,.status_code= 301  ,.num_headers= 0  ,.headers= {}  ,.body= ""  }#define TRAILING_SPACE_ON_CHUNKED_BODY 4, {.name="200 trailing space on chunked body"  ,.type= HTTP_RESPONSE  ,.raw= "HTTP/1.1 200 OK\r\n"         "Content-Type: text/plain\r\n"         "Transfer-Encoding: chunked\r\n"         "\r\n"         "25  \r\n"         "This is the data in the first chunk\r\n"         "\r\n"         "1C\r\n"         "and this is the second one\r\n"         "\r\n"         "0  \r\n"         "\r\n"  ,.should_keep_alive= TRUE  ,.message_complete_on_eof= FALSE  ,.http_major= 1  ,.http_minor= 1  ,.status_code= 200  ,.num_headers= 2  ,.headers=    { {"Content-Type", "text/plain" }    , {"Transfer-Encoding", "chunked" }    }  ,.body_size = 37+28  ,.body =         "This is the data in the first chunk\r\n"         "and this is the second one\r\n"  }#define NO_CARRIAGE_RET 5, {.name="no carriage ret"  ,.type= HTTP_RESPONSE  ,.raw= "HTTP/1.1 200 OK\n"         "Content-Type: text/html; charset=utf-8\n"         "Connection: close\n"         "\n"         "these headers are from http://news.ycombinator.com/"  ,.should_keep_alive= FALSE  ,.message_complete_on_eof= TRUE  ,.http_major= 1  ,.http_minor= 1  ,.status_code= 200  ,.num_headers= 2  ,.headers=    { {"Content-Type", "text/html; charset=utf-8" }    , {"Connection", "close" }    }  ,.body= "these headers are from http://news.ycombinator.com/"  }#define PROXY_CONNECTION 6, {.name="proxy connection"  ,.type= HTTP_RESPONSE  ,.raw= "HTTP/1.1 200 OK\r\n"         "Content-Type: text/html; charset=UTF-8\r\n"         "Content-Length: 11\r\n"         "Proxy-Connection: close\r\n"         "Date: Thu, 31 Dec 2009 20:55:48 +0000\r\n"         "\r\n"         "hello world"  ,.should_keep_alive= FALSE  ,.message_complete_on_eof= FALSE  ,.http_major= 1  ,.http_minor= 1  ,.status_code= 200  ,.num_headers= 4  ,.headers=    { {"Content-Type", "text/html; charset=UTF-8" }    , {"Content-Length", "11" }    , {"Proxy-Connection", "close" }    , {"Date", "Thu, 31 Dec 2009 20:55:48 +0000"}    }  ,.body= "hello world"  }#define UNDERSTORE_HEADER_KEY 7  // shown by  // curl -o /dev/null -v "http://ad.doubleclick.net/pfadx/DARTSHELLCONFIGXML;dcmt=text/xml;", {.name="underscore header key"  ,.type= HTTP_RESPONSE  ,.raw= "HTTP/1.1 200 OK\r\n"         "Server: DCLK-AdSvr\r\n"         "Content-Type: text/xml\r\n"         "Content-Length: 0\r\n"         "DCLK_imp: v7;x;114750856;0-0;0;17820020;0/0;21603567/21621457/1;;~okv=;dcmt=text/xml;;~cs=o\r\n\r\n"  ,.should_keep_alive= TRUE  ,.message_complete_on_eof= FALSE  ,.http_major= 1  ,.http_minor= 1  ,.status_code= 200  ,.num_headers= 4  ,.headers=    { {"Server", "DCLK-AdSvr" }    , {"Content-Type", "text/xml" }    , {"Content-Length", "0" }    , {"DCLK_imp", "v7;x;114750856;0-0;0;17820020;0/0;21603567/21621457/1;;~okv=;dcmt=text/xml;;~cs=o" }    }  ,.body= ""  }#define BONJOUR_MADAME_FR 8/* The client should not merge two headers fields when the first one doesn't * have a value. */, {.name= "bonjourmadame.fr"  ,.type= HTTP_RESPONSE  ,.raw= "HTTP/1.0 301 Moved Permanently\r\n"         "Date: Thu, 03 Jun 2010 09:56:32 GMT\r\n"         "Server: Apache/2.2.3 (Red Hat)\r\n"         "Cache-Control: public\r\n"         "Pragma: \r\n"         "Location: http://www.bonjourmadame.fr/\r\n"         "Vary: Accept-Encoding\r\n"         "Content-Length: 0\r\n"         "Content-Type: text/html; charset=UTF-8\r\n"         "Connection: keep-alive\r\n"         "\r\n"  ,.should_keep_alive= TRUE  ,.message_complete_on_eof= FALSE  ,.http_major= 1  ,.http_minor= 0  ,.status_code= 301  ,.num_headers= 9  ,.headers=    { { "Date", "Thu, 03 Jun 2010 09:56:32 GMT" }    , { "Server", "Apache/2.2.3 (Red Hat)" }    , { "Cache-Control", "public" }    , { "Pragma", "" }    , { "Location", "http://www.bonjourmadame.fr/" }    , { "Vary",  "Accept-Encoding" }    , { "Content-Length", "0" }    , { "Content-Type", "text/html; charset=UTF-8" }    , { "Connection", "keep-alive" }    }  ,.body= ""  }#define RES_FIELD_UNDERSCORE 9/* Should handle spaces in header fields */, {.name= "field underscore"  ,.type= HTTP_RESPONSE  ,.raw= "HTTP/1.1 200 OK\r\n"         "Date: Tue, 28 Sep 2010 01:14:13 GMT\r\n"         "Server: Apache\r\n"         "Cache-Control: no-cache, must-revalidate\r\n"         "Expires: Mon, 26 Jul 1997 05:00:00 GMT\r\n"         ".et-Cookie: PlaxoCS=1274804622353690521; path=/; domain=.plaxo.com\r\n"         "Vary: Accept-Encoding\r\n"         "_eep-Alive: timeout=45\r\n" /* semantic value ignored */         "_onnection: Keep-Alive\r\n" /* semantic value ignored */         "Transfer-Encoding: chunked\r\n"         "Content-Type: text/html\r\n"         "Connection: close\r\n"         "\r\n"         "0\r\n\r\n"  ,.should_keep_alive= FALSE  ,.message_complete_on_eof= FALSE  ,.http_major= 1  ,.http_minor= 1  ,.status_code= 200  ,.num_headers= 11  ,.headers=    { { "Date", "Tue, 28 Sep 2010 01:14:13 GMT" }    , { "Server", "Apache" }    , { "Cache-Control", "no-cache, must-revalidate" }    , { "Expires", "Mon, 26 Jul 1997 05:00:00 GMT" }    , { ".et-Cookie", "PlaxoCS=1274804622353690521; path=/; domain=.plaxo.com" }    , { "Vary", "Accept-Encoding" }    , { "_eep-Alive", "timeout=45" }    , { "_onnection", "Keep-Alive" }    , { "Transfer-Encoding", "chunked" }    , { "Content-Type", "text/html" }    , { "Connection", "close" }    }  ,.body= ""  }#define NON_ASCII_IN_STATUS_LINE 10/* Should handle non-ASCII in status line */, {.name= "non-ASCII in status line"  ,.type= HTTP_RESPONSE  ,.raw= "HTTP/1.1 500 Oriëntatieprobleem\r\n"         "Date: Fri, 5 Nov 2010 23:07:12 GMT+2\r\n"         "Content-Length: 0\r\n"         "Connection: close\r\n"         "\r\n"  ,.should_keep_alive= FALSE  ,.message_complete_on_eof= FALSE  ,.http_major= 1  ,.http_minor= 1  ,.status_code= 500  ,.num_headers= 3  ,.headers=    { { "Date", "Fri, 5 Nov 2010 23:07:12 GMT+2" }    , { "Content-Length", "0" }    , { "Connection", "close" }    }  ,.body= ""  }#define HTTP_VERSION_0_9 11/* Should handle HTTP/0.9 */, {.name= "http version 0.9"  ,.type= HTTP_RESPONSE  ,.raw= "HTTP/0.9 200 OK\r\n"         "\r\n"  ,.should_keep_alive= FALSE  ,.message_complete_on_eof= TRUE  ,.http_major= 0  ,.http_minor= 9  ,.status_code= 200  ,.num_headers= 0  ,.headers=    {}  ,.body= ""  }#define NO_CONTENT_LENGTH_NO_TRANSFER_ENCODING_RESPONSE 12/* The client should wait for the server's EOF. That is, when neither * content-length nor transfer-encoding is specified, the end of body * is specified by the EOF. */, {.name= "neither content-length nor transfer-encoding response"  ,.type= HTTP_RESPONSE  ,.raw= "HTTP/1.1 200 OK\r\n"         "Content-Type: text/plain\r\n"         "\r\n"         "hello world"  ,.should_keep_alive= FALSE  ,.message_complete_on_eof= TRUE  ,.http_major= 1  ,.http_minor= 1  ,.status_code= 200  ,.num_headers= 1  ,.headers=    { { "Content-Type", "text/plain" }    }  ,.body= "hello world"  }#define NO_BODY_HTTP10_KA_200 13, {.name= "HTTP/1.0 with keep-alive and EOF-terminated 200 status"  ,.type= HTTP_RESPONSE  ,.raw= "HTTP/1.0 200 OK\r\n"         "Connection: keep-alive\r\n"         "\r\n"  ,.should_keep_alive= FALSE  ,.message_complete_on_eof= TRUE  ,.http_major= 1  ,.http_minor= 0  ,.status_code= 200  ,.num_headers= 1  ,.headers=    { { "Connection", "keep-alive" }    }  ,.body_size= 0  ,.body= ""  }#define NO_BODY_HTTP10_KA_204 14, {.name= "HTTP/1.0 with keep-alive and a 204 status"  ,.type= HTTP_RESPONSE  ,.raw= "HTTP/1.0 204 No content\r\n"         "Connection: keep-alive\r\n"         "\r\n"  ,.should_keep_alive= TRUE  ,.message_complete_on_eof= FALSE  ,.http_major= 1  ,.http_minor= 0  ,.status_code= 204  ,.num_headers= 1  ,.headers=    { { "Connection", "keep-alive" }    }  ,.body_size= 0  ,.body= ""  }#define NO_BODY_HTTP11_KA_200 15, {.name= "HTTP/1.1 with an EOF-terminated 200 status"  ,.type= HTTP_RESPONSE  ,.raw= "HTTP/1.1 200 OK\r\n"         "\r\n"  ,.should_keep_alive= FALSE  ,.message_complete_on_eof= TRUE  ,.http_major= 1  ,.http_minor= 1  ,.status_code= 200  ,.num_headers= 0  ,.headers={}  ,.body_size= 0  ,.body= ""  }#define NO_BODY_HTTP11_KA_204 16, {.name= "HTTP/1.1 with a 204 status"  ,.type= HTTP_RESPONSE  ,.raw= "HTTP/1.1 204 No content\r\n"         "\r\n"  ,.should_keep_alive= TRUE  ,.message_complete_on_eof= FALSE  ,.http_major= 1  ,.http_minor= 1  ,.status_code= 204  ,.num_headers= 0  ,.headers={}  ,.body_size= 0  ,.body= ""  }#define NO_BODY_HTTP11_NOKA_204 17, {.name= "HTTP/1.1 with a 204 status and keep-alive disabled"  ,.type= HTTP_RESPONSE  ,.raw= "HTTP/1.1 204 No content\r\n"         "Connection: close\r\n"         "\r\n"  ,.should_keep_alive= FALSE  ,.message_complete_on_eof= FALSE  ,.http_major= 1  ,.http_minor= 1  ,.status_code= 204  ,.num_headers= 1  ,.headers=    { { "Connection", "close" }    }  ,.body_size= 0  ,.body= ""  }#define NO_BODY_HTTP11_KA_CHUNKED_200 18, {.name= "HTTP/1.1 with chunked endocing and a 200 response"  ,.type= HTTP_RESPONSE  ,.raw= "HTTP/1.1 200 OK\r\n"         "Transfer-Encoding: chunked\r\n"         "\r\n"         "0\r\n"         "\r\n"  ,.should_keep_alive= TRUE  ,.message_complete_on_eof= FALSE  ,.http_major= 1  ,.http_minor= 1  ,.status_code= 200  ,.num_headers= 1  ,.headers=    { { "Transfer-Encoding", "chunked" }    }  ,.body_size= 0  ,.body= ""  }#if !HTTP_PARSER_STRICT#define SPACE_IN_FIELD_RES 19/* Should handle spaces in header fields */, {.name= "field space"  ,.type= HTTP_RESPONSE  ,.raw= "HTTP/1.1 200 OK\r\n"         "Server: Microsoft-IIS/6.0\r\n"         "X-Powered-By: ASP.NET\r\n"         "en-US Content-Type: text/xml\r\n" /* this is the problem */         "Content-Type: text/xml\r\n"         "Content-Length: 16\r\n"         "Date: Fri, 23 Jul 2010 18:45:38 GMT\r\n"         "Connection: keep-alive\r\n"         "\r\n"         "<xml>hello</xml>" /* fake body */  ,.should_keep_alive= TRUE  ,.message_complete_on_eof= FALSE  ,.http_major= 1  ,.http_minor= 1  ,.status_code= 200  ,.num_headers= 7  ,.headers=    { { "Server",  "Microsoft-IIS/6.0" }    , { "X-Powered-By", "ASP.NET" }    , { "en-US Content-Type", "text/xml" }    , { "Content-Type", "text/xml" }    , { "Content-Length", "16" }    , { "Date", "Fri, 23 Jul 2010 18:45:38 GMT" }    , { "Connection", "keep-alive" }    }  ,.body= "<xml>hello</xml>"  }#endif /* !HTTP_PARSER_STRICT */, {.name= NULL } /* sentinel */};/* strnlen() is a POSIX.2008 addition. Can't rely on it being available so * define it ourselves. */size_tstrnlen(const char *s, size_t maxlen){  const char *p;  p = memchr(s, '\0', maxlen);  if (p == NULL)    return maxlen;  return p - s;}size_tstrlncat(char *dst, size_t len, const char *src, size_t n){  size_t slen;  size_t dlen;  size_t rlen;  size_t ncpy;  slen = strnlen(src, n);  dlen = strnlen(dst, len);  if (dlen < len) {    rlen = len - dlen;    ncpy = slen < rlen ? slen : (rlen - 1);    memcpy(dst + dlen, src, ncpy);    dst[dlen + ncpy] = '\0';  }  assert(len > slen + dlen);  return slen + dlen;}size_tstrlcat(char *dst, const char *src, size_t len){  return strlncat(dst, len, src, (size_t) -1);}size_tstrlncpy(char *dst, size_t len, const char *src, size_t n){  size_t slen;  size_t ncpy;  slen = strnlen(src, n);  if (len > 0) {    ncpy = slen < len ? slen : (len - 1);    memcpy(dst, src, ncpy);    dst[ncpy] = '\0';  }  assert(len > slen);  return slen;}size_tstrlcpy(char *dst, const char *src, size_t len){  return strlncpy(dst, len, src, (size_t) -1);}intrequest_url_cb (http_parser *p, const char *buf, size_t len){  assert(p == parser);  strlncat(messages[num_messages].request_url,           sizeof(messages[num_messages].request_url),           buf,           len);  return 0;}intstatus_complete_cb (http_parser *p) {  assert(p == parser);  p->data++;  return 0;}intheader_field_cb (http_parser *p, const char *buf, size_t len){  assert(p == parser);  struct message *m = &messages[num_messages];  if (m->last_header_element != FIELD)    m->num_headers++;  strlncat(m->headers[m->num_headers-1][0],           sizeof(m->headers[m->num_headers-1][0]),           buf,           len);  m->last_header_element = FIELD;  return 0;}intheader_value_cb (http_parser *p, const char *buf, size_t len){  assert(p == parser);  struct message *m = &messages[num_messages];  strlncat(m->headers[m->num_headers-1][1],           sizeof(m->headers[m->num_headers-1][1]),           buf,           len);  m->last_header_element = VALUE;  return 0;}voidcheck_body_is_final (const http_parser *p){  if (messages[num_messages].body_is_final) {    fprintf(stderr, "\n\n *** Error http_body_is_final() should return 1 "                    "on last on_body callback call "                    "but it doesn't! ***\n\n");    assert(0);    abort();  }  messages[num_messages].body_is_final = http_body_is_final(p);}intbody_cb (http_parser *p, const char *buf, size_t len){  assert(p == parser);  strlncat(messages[num_messages].body,           sizeof(messages[num_messages].body),           buf,           len);  messages[num_messages].body_size += len;  check_body_is_final(p); // printf("body_cb: '%s'\n", requests[num_messages].body);  return 0;}intcount_body_cb (http_parser *p, const char *buf, size_t len){  assert(p == parser);  assert(buf);  messages[num_messages].body_size += len;  check_body_is_final(p);  return 0;}intmessage_begin_cb (http_parser *p){  assert(p == parser);  messages[num_messages].message_begin_cb_called = TRUE;  return 0;}intheaders_complete_cb (http_parser *p){  assert(p == parser);  messages[num_messages].method = parser->method;  messages[num_messages].status_code = parser->status_code;  messages[num_messages].http_major = parser->http_major;  messages[num_messages].http_minor = parser->http_minor;  messages[num_messages].headers_complete_cb_called = TRUE;  messages[num_messages].should_keep_alive = http_should_keep_alive(parser);  return 0;}intmessage_complete_cb (http_parser *p){  assert(p == parser);  if (messages[num_messages].should_keep_alive != http_should_keep_alive(parser))  {    fprintf(stderr, "\n\n *** Error http_should_keep_alive() should have same "                    "value in both on_message_complete and on_headers_complete "                    "but it doesn't! ***\n\n");    assert(0);    abort();  }  if (messages[num_messages].body_size &&      http_body_is_final(p) &&      !messages[num_messages].body_is_final)  {    fprintf(stderr, "\n\n *** Error http_body_is_final() should return 1 "                    "on last on_body callback call "                    "but it doesn't! ***\n\n");    assert(0);    abort();  }  messages[num_messages].message_complete_cb_called = TRUE;  messages[num_messages].message_complete_on_eof = currently_parsing_eof;  num_messages++;  return 0;}/* These dontcall_* callbacks exist so that we can verify that when we're * paused, no additional callbacks are invoked */intdontcall_message_begin_cb (http_parser *p){  if (p) { } // gcc  fprintf(stderr, "\n\n*** on_message_begin() called on paused parser ***\n\n");  abort();}intdontcall_header_field_cb (http_parser *p, const char *buf, size_t len){  if (p || buf || len) { } // gcc  fprintf(stderr, "\n\n*** on_header_field() called on paused parser ***\n\n");  abort();}intdontcall_header_value_cb (http_parser *p, const char *buf, size_t len){  if (p || buf || len) { } // gcc  fprintf(stderr, "\n\n*** on_header_value() called on paused parser ***\n\n");  abort();}intdontcall_request_url_cb (http_parser *p, const char *buf, size_t len){  if (p || buf || len) { } // gcc  fprintf(stderr, "\n\n*** on_request_url() called on paused parser ***\n\n");  abort();}intdontcall_body_cb (http_parser *p, const char *buf, size_t len){  if (p || buf || len) { } // gcc  fprintf(stderr, "\n\n*** on_body_cb() called on paused parser ***\n\n");  abort();}intdontcall_headers_complete_cb (http_parser *p){  if (p) { } // gcc  fprintf(stderr, "\n\n*** on_headers_complete() called on paused "                  "parser ***\n\n");  abort();}intdontcall_message_complete_cb (http_parser *p){  if (p) { } // gcc  fprintf(stderr, "\n\n*** on_message_complete() called on paused "                  "parser ***\n\n");  abort();}static http_parser_settings settings_dontcall =  {.on_message_begin = dontcall_message_begin_cb  ,.on_header_field = dontcall_header_field_cb  ,.on_header_value = dontcall_header_value_cb  ,.on_url = dontcall_request_url_cb  ,.on_body = dontcall_body_cb  ,.on_headers_complete = dontcall_headers_complete_cb  ,.on_message_complete = dontcall_message_complete_cb  };/* These pause_* callbacks always pause the parser and just invoke the regular * callback that tracks content. Before returning, we overwrite the parser * settings to point to the _dontcall variety so that we can verify that * the pause actually did, you know, pause. */intpause_message_begin_cb (http_parser *p){  http_parser_pause(p, 1);  *current_pause_parser = settings_dontcall;  return message_begin_cb(p);}intpause_header_field_cb (http_parser *p, const char *buf, size_t len){  http_parser_pause(p, 1);  *current_pause_parser = settings_dontcall;  return header_field_cb(p, buf, len);}intpause_header_value_cb (http_parser *p, const char *buf, size_t len){  http_parser_pause(p, 1);  *current_pause_parser = settings_dontcall;  return header_value_cb(p, buf, len);}intpause_request_url_cb (http_parser *p, const char *buf, size_t len){  http_parser_pause(p, 1);  *current_pause_parser = settings_dontcall;  return request_url_cb(p, buf, len);}intpause_body_cb (http_parser *p, const char *buf, size_t len){  http_parser_pause(p, 1);  *current_pause_parser = settings_dontcall;  return body_cb(p, buf, len);}intpause_headers_complete_cb (http_parser *p){  http_parser_pause(p, 1);  *current_pause_parser = settings_dontcall;  return headers_complete_cb(p);}intpause_message_complete_cb (http_parser *p){  http_parser_pause(p, 1);  *current_pause_parser = settings_dontcall;  return message_complete_cb(p);}static http_parser_settings settings_pause =  {.on_message_begin = pause_message_begin_cb  ,.on_header_field = pause_header_field_cb  ,.on_header_value = pause_header_value_cb  ,.on_url = pause_request_url_cb  ,.on_body = pause_body_cb  ,.on_headers_complete = pause_headers_complete_cb  ,.on_message_complete = pause_message_complete_cb  };static http_parser_settings settings =  {.on_message_begin = message_begin_cb  ,.on_header_field = header_field_cb  ,.on_header_value = header_value_cb  ,.on_url = request_url_cb  ,.on_body = body_cb  ,.on_headers_complete = headers_complete_cb  ,.on_message_complete = message_complete_cb  };static http_parser_settings settings_count_body =  {.on_message_begin = message_begin_cb  ,.on_header_field = header_field_cb  ,.on_header_value = header_value_cb  ,.on_url = request_url_cb  ,.on_body = count_body_cb  ,.on_headers_complete = headers_complete_cb  ,.on_message_complete = message_complete_cb  };static http_parser_settings settings_null =  {.on_message_begin = 0  ,.on_header_field = 0  ,.on_header_value = 0  ,.on_url = 0  ,.on_body = 0  ,.on_headers_complete = 0  ,.on_message_complete = 0  };voidparser_init (enum http_parser_type type){  num_messages = 0;  assert(parser == NULL);  parser = malloc(sizeof(http_parser));  http_parser_init(parser, type);  memset(&messages, 0, sizeof messages);}voidparser_free (){  assert(parser);  free(parser);  parser = NULL;}size_t parse (const char *buf, size_t len){  size_t nparsed;  currently_parsing_eof = (len == 0);  nparsed = http_parser_execute(parser, &settings, buf, len);  return nparsed;}size_t parse_count_body (const char *buf, size_t len){  size_t nparsed;  currently_parsing_eof = (len == 0);  nparsed = http_parser_execute(parser, &settings_count_body, buf, len);  return nparsed;}size_t parse_pause (const char *buf, size_t len){  size_t nparsed;  http_parser_settings s = settings_pause;  currently_parsing_eof = (len == 0);  current_pause_parser = &s;  nparsed = http_parser_execute(parser, current_pause_parser, buf, len);  return nparsed;}static inline intcheck_str_eq (const struct message *m,              const char *prop,              const char *expected,              const char *found) {  if ((expected == NULL) != (found == NULL)) {    printf("\n*** Error: %s in '%s' ***\n\n", prop, m->name);    printf("expected %s\n", (expected == NULL) ? "NULL" : expected);    printf("   found %s\n", (found == NULL) ? "NULL" : found);    return 0;  }  if (expected != NULL && 0 != strcmp(expected, found)) {    printf("\n*** Error: %s in '%s' ***\n\n", prop, m->name);    printf("expected '%s'\n", expected);    printf("   found '%s'\n", found);    return 0;  }  return 1;}static inline intcheck_num_eq (const struct message *m,              const char *prop,              int expected,              int found) {  if (expected != found) {    printf("\n*** Error: %s in '%s' ***\n\n", prop, m->name);    printf("expected %d\n", expected);    printf("   found %d\n", found);    return 0;  }  return 1;}#define MESSAGE_CHECK_STR_EQ(expected, found, prop) \  if (!check_str_eq(expected, #prop, expected->prop, found->prop)) return 0#define MESSAGE_CHECK_NUM_EQ(expected, found, prop) \  if (!check_num_eq(expected, #prop, expected->prop, found->prop)) return 0#define MESSAGE_CHECK_URL_EQ(u, expected, found, prop, fn)           \do {                                                                 \  char ubuf[256];                                                    \                                                                     \  if ((u)->field_set & (1 << (fn))) {                                \    memcpy(ubuf, (found)->request_url + (u)->field_data[(fn)].off,   \      (u)->field_data[(fn)].len);                                    \    ubuf[(u)->field_data[(fn)].len] = '\0';                          \  } else {                                                           \    ubuf[0] = '\0';                                                  \  }                                                                  \                                                                     \  check_str_eq(expected, #prop, expected->prop, ubuf);               \} while(0)intmessage_eq (int index, const struct message *expected){  int i;  struct message *m = &messages[index];  MESSAGE_CHECK_NUM_EQ(expected, m, http_major);  MESSAGE_CHECK_NUM_EQ(expected, m, http_minor);  if (expected->type == HTTP_REQUEST) {    MESSAGE_CHECK_NUM_EQ(expected, m, method);  } else {    MESSAGE_CHECK_NUM_EQ(expected, m, status_code);  }  MESSAGE_CHECK_NUM_EQ(expected, m, should_keep_alive);  MESSAGE_CHECK_NUM_EQ(expected, m, message_complete_on_eof);  assert(m->message_begin_cb_called);  assert(m->headers_complete_cb_called);  assert(m->message_complete_cb_called);  MESSAGE_CHECK_STR_EQ(expected, m, request_url);  /* Check URL components; we can't do this w/ CONNECT since it doesn't   * send us a well-formed URL.   */  if (*m->request_url && m->method != HTTP_CONNECT) {    struct http_parser_url u;    if (http_parser_parse_url(m->request_url, strlen(m->request_url), 0, &u)) {      fprintf(stderr, "\n\n*** failed to parse URL %s ***\n\n",        m->request_url);      abort();    }    if (expected->host) {      MESSAGE_CHECK_URL_EQ(&u, expected, m, host, UF_HOST);    }    if (expected->userinfo) {      MESSAGE_CHECK_URL_EQ(&u, expected, m, userinfo, UF_USERINFO);    }    m->port = (u.field_set & (1 << UF_PORT)) ?      u.port : 0;    MESSAGE_CHECK_URL_EQ(&u, expected, m, query_string, UF_QUERY);    MESSAGE_CHECK_URL_EQ(&u, expected, m, fragment, UF_FRAGMENT);    MESSAGE_CHECK_URL_EQ(&u, expected, m, request_path, UF_PATH);    MESSAGE_CHECK_NUM_EQ(expected, m, port);  }  if (expected->body_size) {    MESSAGE_CHECK_NUM_EQ(expected, m, body_size);  } else {    MESSAGE_CHECK_STR_EQ(expected, m, body);  }  MESSAGE_CHECK_NUM_EQ(expected, m, num_headers);  int r;  for (i = 0; i < m->num_headers; i++) {    r = check_str_eq(expected, "header field", expected->headers[i][0], m->headers[i][0]);    if (!r) return 0;    r = check_str_eq(expected, "header value", expected->headers[i][1], m->headers[i][1]);    if (!r) return 0;  }  MESSAGE_CHECK_STR_EQ(expected, m, upgrade);  return 1;}/* Given a sequence of varargs messages, return the number of them that the * parser should successfully parse, taking into account that upgraded * messages prevent all subsequent messages from being parsed. */size_tcount_parsed_messages(const size_t nmsgs, ...) {  size_t i;  va_list ap;  va_start(ap, nmsgs);  for (i = 0; i < nmsgs; i++) {    struct message *m = va_arg(ap, struct message *);    if (m->upgrade) {      va_end(ap);      return i + 1;    }  }  va_end(ap);  return nmsgs;}/* Given a sequence of bytes and the number of these that we were able to * parse, verify that upgrade bodies are correct. */voidupgrade_message_fix(char *body, const size_t nread, const size_t nmsgs, ...) {  va_list ap;  size_t i;  size_t off = 0;   va_start(ap, nmsgs);  for (i = 0; i < nmsgs; i++) {    struct message *m = va_arg(ap, struct message *);    off += strlen(m->raw);    if (m->upgrade) {      off -= strlen(m->upgrade);      /* Check the portion of the response after its specified upgrade */      if (!check_str_eq(m, "upgrade", body + off, body + nread)) {        abort();      }      /* Fix up the response so that message_eq() will verify the beginning       * of the upgrade */      *(body + nread + strlen(m->upgrade)) = '\0';      messages[num_messages -1 ].upgrade = body + nread;      va_end(ap);      return;    }  }  va_end(ap);  printf("\n\n*** Error: expected a message with upgrade ***\n");  abort();}static voidprint_error (const char *raw, size_t error_location){  fprintf(stderr, "\n*** %s ***\n\n",          http_errno_description(HTTP_PARSER_ERRNO(parser)));  int this_line = 0, char_len = 0;  size_t i, j, len = strlen(raw), error_location_line = 0;  for (i = 0; i < len; i++) {    if (i == error_location) this_line = 1;    switch (raw[i]) {      case '\r':        char_len = 2;        fprintf(stderr, "\\r");        break;      case '\n':        char_len = 2;        fprintf(stderr, "\\n\n");        if (this_line) goto print;        error_location_line = 0;        continue;      default:        char_len = 1;        fputc(raw[i], stderr);        break;    }    if (!this_line) error_location_line += char_len;  }  fprintf(stderr, "[eof]\n"); print:  for (j = 0; j < error_location_line; j++) {    fputc(' ', stderr);  }  fprintf(stderr, "^\n\nerror location: %u\n", (unsigned int)error_location);}voidtest_preserve_data (void){  char my_data[] = "application-specific data";  http_parser parser;  parser.data = my_data;  http_parser_init(&parser, HTTP_REQUEST);  if (parser.data != my_data) {    printf("\n*** parser.data not preserved accross http_parser_init ***\n\n");    abort();  }}struct url_test {  const char *name;  const char *url;  int is_connect;  struct http_parser_url u;  int rv;};const struct url_test url_tests[] ={ {.name="proxy request"  ,.url="http://hostname/"  ,.is_connect=0  ,.u=    {.field_set=(1 << UF_SCHEMA) | (1 << UF_HOST) | (1 << UF_PATH)    ,.port=0    ,.field_data=      {{  0,  4 } /* UF_SCHEMA */      ,{  7,  8 } /* UF_HOST */      ,{  0,  0 } /* UF_PORT */      ,{ 15,  1 } /* UF_PATH */      ,{  0,  0 } /* UF_QUERY */      ,{  0,  0 } /* UF_FRAGMENT */      ,{  0,  0 } /* UF_USERINFO */      }    }  ,.rv=0  }, {.name="proxy request with port"  ,.url="http://hostname:444/"  ,.is_connect=0  ,.u=    {.field_set=(1 << UF_SCHEMA) | (1 << UF_HOST) | (1 << UF_PORT) | (1 << UF_PATH)    ,.port=444    ,.field_data=      {{  0,  4 } /* UF_SCHEMA */      ,{  7,  8 } /* UF_HOST */      ,{ 16,  3 } /* UF_PORT */      ,{ 19,  1 } /* UF_PATH */      ,{  0,  0 } /* UF_QUERY */      ,{  0,  0 } /* UF_FRAGMENT */      ,{  0,  0 } /* UF_USERINFO */      }    }  ,.rv=0  }, {.name="CONNECT request"  ,.url="hostname:443"  ,.is_connect=1  ,.u=    {.field_set=(1 << UF_HOST) | (1 << UF_PORT)    ,.port=443    ,.field_data=      {{  0,  0 } /* UF_SCHEMA */      ,{  0,  8 } /* UF_HOST */      ,{  9,  3 } /* UF_PORT */      ,{  0,  0 } /* UF_PATH */      ,{  0,  0 } /* UF_QUERY */      ,{  0,  0 } /* UF_FRAGMENT */      ,{  0,  0 } /* UF_USERINFO */      }    }  ,.rv=0  }, {.name="CONNECT request but not connect"  ,.url="hostname:443"  ,.is_connect=0  ,.rv=1  }, {.name="proxy ipv6 request"  ,.url="http://[1:2::3:4]/"  ,.is_connect=0  ,.u=    {.field_set=(1 << UF_SCHEMA) | (1 << UF_HOST) | (1 << UF_PATH)    ,.port=0    ,.field_data=      {{  0,  4 } /* UF_SCHEMA */      ,{  8,  8 } /* UF_HOST */      ,{  0,  0 } /* UF_PORT */      ,{ 17,  1 } /* UF_PATH */      ,{  0,  0 } /* UF_QUERY */      ,{  0,  0 } /* UF_FRAGMENT */      ,{  0,  0 } /* UF_USERINFO */      }    }  ,.rv=0  }, {.name="proxy ipv6 request with port"  ,.url="http://[1:2::3:4]:67/"  ,.is_connect=0  ,.u=    {.field_set=(1 << UF_SCHEMA) | (1 << UF_HOST) | (1 << UF_PORT) | (1 << UF_PATH)    ,.port=67    ,.field_data=      {{  0,  4 } /* UF_SCHEMA */      ,{  8,  8 } /* UF_HOST */      ,{ 18,  2 } /* UF_PORT */      ,{ 20,  1 } /* UF_PATH */      ,{  0,  0 } /* UF_QUERY */      ,{  0,  0 } /* UF_FRAGMENT */      ,{  0,  0 } /* UF_USERINFO */      }    }  ,.rv=0  }, {.name="CONNECT ipv6 address"  ,.url="[1:2::3:4]:443"  ,.is_connect=1  ,.u=    {.field_set=(1 << UF_HOST) | (1 << UF_PORT)    ,.port=443    ,.field_data=      {{  0,  0 } /* UF_SCHEMA */      ,{  1,  8 } /* UF_HOST */      ,{ 11,  3 } /* UF_PORT */      ,{  0,  0 } /* UF_PATH */      ,{  0,  0 } /* UF_QUERY */      ,{  0,  0 } /* UF_FRAGMENT */      ,{  0,  0 } /* UF_USERINFO */      }    }  ,.rv=0  }, {.name="ipv4 in ipv6 address"  ,.url="http://[2001:0000:0000:0000:0000:0000:1.9.1.1]/"  ,.is_connect=0  ,.u=    {.field_set=(1 << UF_SCHEMA) | (1 << UF_HOST) | (1 << UF_PATH)    ,.port=0    ,.field_data=      {{  0,  4 } /* UF_SCHEMA */      ,{  8, 37 } /* UF_HOST */      ,{  0,  0 } /* UF_PORT */      ,{ 46,  1 } /* UF_PATH */      ,{  0,  0 } /* UF_QUERY */      ,{  0,  0 } /* UF_FRAGMENT */      ,{  0,  0 } /* UF_USERINFO */      }    }  ,.rv=0  }, {.name="extra ? in query string"  ,.url="http://a.tbcdn.cn/p/fp/2010c/??fp-header-min.css,fp-base-min.css,"  "fp-channel-min.css,fp-product-min.css,fp-mall-min.css,fp-category-min.css,"  "fp-sub-min.css,fp-gdp4p-min.css,fp-css3-min.css,fp-misc-min.css?t=20101022.css"  ,.is_connect=0  ,.u=    {.field_set=(1<<UF_SCHEMA) | (1<<UF_HOST) | (1<<UF_PATH) | (1<<UF_QUERY)    ,.port=0    ,.field_data=      {{  0,  4 } /* UF_SCHEMA */      ,{  7, 10 } /* UF_HOST */      ,{  0,  0 } /* UF_PORT */      ,{ 17, 12 } /* UF_PATH */      ,{ 30,187 } /* UF_QUERY */      ,{  0,  0 } /* UF_FRAGMENT */      ,{  0,  0 } /* UF_USERINFO */      }    }  ,.rv=0  }, {.name="space URL encoded"  ,.url="/toto.html?toto=a%20b"  ,.is_connect=0  ,.u=    {.field_set= (1<<UF_PATH) | (1<<UF_QUERY)    ,.port=0    ,.field_data=      {{  0,  0 } /* UF_SCHEMA */      ,{  0,  0 } /* UF_HOST */      ,{  0,  0 } /* UF_PORT */      ,{  0, 10 } /* UF_PATH */      ,{ 11, 10 } /* UF_QUERY */      ,{  0,  0 } /* UF_FRAGMENT */      ,{  0,  0 } /* UF_USERINFO */      }    }  ,.rv=0  }, {.name="URL fragment"  ,.url="/toto.html#titi"  ,.is_connect=0  ,.u=    {.field_set= (1<<UF_PATH) | (1<<UF_FRAGMENT)    ,.port=0    ,.field_data=      {{  0,  0 } /* UF_SCHEMA */      ,{  0,  0 } /* UF_HOST */      ,{  0,  0 } /* UF_PORT */      ,{  0, 10 } /* UF_PATH */      ,{  0,  0 } /* UF_QUERY */      ,{ 11,  4 } /* UF_FRAGMENT */      ,{  0,  0 } /* UF_USERINFO */      }    }  ,.rv=0  }, {.name="complex URL fragment"  ,.url="http://www.webmasterworld.com/r.cgi?f=21&d=8405&url="    "http://www.example.com/index.html?foo=bar&hello=world#midpage"  ,.is_connect=0  ,.u=    {.field_set= (1<<UF_SCHEMA) | (1<<UF_HOST) | (1<<UF_PATH) | (1<<UF_QUERY) |\      (1<<UF_FRAGMENT)    ,.port=0    ,.field_data=      {{  0,  4 } /* UF_SCHEMA */      ,{  7, 22 } /* UF_HOST */      ,{  0,  0 } /* UF_PORT */      ,{ 29,  6 } /* UF_PATH */      ,{ 36, 69 } /* UF_QUERY */      ,{106,  7 } /* UF_FRAGMENT */      ,{  0,  0 } /* UF_USERINFO */      }    }  ,.rv=0  }, {.name="complex URL from node js url parser doc"  ,.url="http://host.com:8080/p/a/t/h?query=string#hash"  ,.is_connect=0  ,.u=    {.field_set= (1<<UF_SCHEMA) | (1<<UF_HOST) | (1<<UF_PORT) | (1<<UF_PATH) |\      (1<<UF_QUERY) | (1<<UF_FRAGMENT)    ,.port=8080    ,.field_data=      {{  0,  4 } /* UF_SCHEMA */      ,{  7,  8 } /* UF_HOST */      ,{ 16,  4 } /* UF_PORT */      ,{ 20,  8 } /* UF_PATH */      ,{ 29, 12 } /* UF_QUERY */      ,{ 42,  4 } /* UF_FRAGMENT */      ,{  0,  0 } /* UF_USERINFO */      }    }  ,.rv=0  }, {.name="complex URL with basic auth from node js url parser doc"  ,.url="http://a:b@host.com:8080/p/a/t/h?query=string#hash"  ,.is_connect=0  ,.u=    {.field_set= (1<<UF_SCHEMA) | (1<<UF_HOST) | (1<<UF_PORT) | (1<<UF_PATH) |\      (1<<UF_QUERY) | (1<<UF_FRAGMENT) | (1<<UF_USERINFO)    ,.port=8080    ,.field_data=      {{  0,  4 } /* UF_SCHEMA */      ,{ 11,  8 } /* UF_HOST */      ,{ 20,  4 } /* UF_PORT */      ,{ 24,  8 } /* UF_PATH */      ,{ 33, 12 } /* UF_QUERY */      ,{ 46,  4 } /* UF_FRAGMENT */      ,{  7,  3 } /* UF_USERINFO */      }    }  ,.rv=0  }, {.name="double @"  ,.url="http://a:b@@hostname:443/"  ,.is_connect=0  ,.rv=1  }, {.name="proxy empty host"  ,.url="http://:443/"  ,.is_connect=0  ,.rv=1  }, {.name="proxy empty port"  ,.url="http://hostname:/"  ,.is_connect=0  ,.rv=1  }, {.name="CONNECT with basic auth"  ,.url="a:b@hostname:443"  ,.is_connect=1  ,.rv=1  }, {.name="CONNECT empty host"  ,.url=":443"  ,.is_connect=1  ,.rv=1  }, {.name="CONNECT empty port"  ,.url="hostname:"  ,.is_connect=1  ,.rv=1  }, {.name="CONNECT with extra bits"  ,.url="hostname:443/"  ,.is_connect=1  ,.rv=1  }, {.name="space in URL"  ,.url="/foo bar/"  ,.rv=1 /* s_dead */  }, {.name="proxy basic auth with space url encoded"  ,.url="http://a%20:b@host.com/"  ,.is_connect=0  ,.u=    {.field_set= (1<<UF_SCHEMA) | (1<<UF_HOST) | (1<<UF_PATH) | (1<<UF_USERINFO)    ,.port=0    ,.field_data=      {{  0,  4 } /* UF_SCHEMA */      ,{ 14,  8 } /* UF_HOST */      ,{  0,  0 } /* UF_PORT */      ,{ 22,  1 } /* UF_PATH */      ,{  0,  0 } /* UF_QUERY */      ,{  0,  0 } /* UF_FRAGMENT */      ,{  7,  6 } /* UF_USERINFO */      }    }  ,.rv=0  }, {.name="carriage return in URL"  ,.url="/foo\rbar/"  ,.rv=1 /* s_dead */  }, {.name="proxy double : in URL"  ,.url="http://hostname::443/"  ,.rv=1 /* s_dead */  }, {.name="proxy basic auth with double :"  ,.url="http://a::b@host.com/"  ,.is_connect=0  ,.u=    {.field_set= (1<<UF_SCHEMA) | (1<<UF_HOST) | (1<<UF_PATH) | (1<<UF_USERINFO)    ,.port=0    ,.field_data=      {{  0,  4 } /* UF_SCHEMA */      ,{ 12,  8 } /* UF_HOST */      ,{  0,  0 } /* UF_PORT */      ,{ 20,  1 } /* UF_PATH */      ,{  0,  0 } /* UF_QUERY */      ,{  0,  0 } /* UF_FRAGMENT */      ,{  7,  4 } /* UF_USERINFO */      }    }  ,.rv=0  }, {.name="line feed in URL"  ,.url="/foo\nbar/"  ,.rv=1 /* s_dead */  }, {.name="proxy empty basic auth"  ,.url="http://@hostname/fo"  ,.u=    {.field_set= (1<<UF_SCHEMA) | (1<<UF_HOST) | (1<<UF_PATH)    ,.port=0    ,.field_data=      {{  0,  4 } /* UF_SCHEMA */      ,{  8,  8 } /* UF_HOST */      ,{  0,  0 } /* UF_PORT */      ,{ 16,  3 } /* UF_PATH */      ,{  0,  0 } /* UF_QUERY */      ,{  0,  0 } /* UF_FRAGMENT */      ,{  0,  0 } /* UF_USERINFO */      }    }  ,.rv=0  }, {.name="proxy line feed in hostname"  ,.url="http://host\name/fo"  ,.rv=1 /* s_dead */  }, {.name="proxy % in hostname"  ,.url="http://host%name/fo"  ,.rv=1 /* s_dead */  }, {.name="proxy ; in hostname"  ,.url="http://host;ame/fo"  ,.rv=1 /* s_dead */  }, {.name="proxy basic auth with unreservedchars"  ,.url="http://a!;-_!=+$@host.com/"  ,.is_connect=0  ,.u=    {.field_set= (1<<UF_SCHEMA) | (1<<UF_HOST) | (1<<UF_PATH) | (1<<UF_USERINFO)    ,.port=0    ,.field_data=      {{  0,  4 } /* UF_SCHEMA */      ,{ 17,  8 } /* UF_HOST */      ,{  0,  0 } /* UF_PORT */      ,{ 25,  1 } /* UF_PATH */      ,{  0,  0 } /* UF_QUERY */      ,{  0,  0 } /* UF_FRAGMENT */      ,{  7,  9 } /* UF_USERINFO */      }    }  ,.rv=0  }, {.name="proxy only empty basic auth"  ,.url="http://@/fo"  ,.rv=1 /* s_dead */  }, {.name="proxy only basic auth"  ,.url="http://toto@/fo"  ,.rv=1 /* s_dead */  }, {.name="proxy emtpy hostname"  ,.url="http:///fo"  ,.rv=1 /* s_dead */  }, {.name="proxy = in URL"  ,.url="http://host=ame/fo"  ,.rv=1 /* s_dead */  }#if HTTP_PARSER_STRICT, {.name="tab in URL"  ,.url="/foo\tbar/"  ,.rv=1 /* s_dead */  }, {.name="form feed in URL"  ,.url="/foo\fbar/"  ,.rv=1 /* s_dead */  }#else /* !HTTP_PARSER_STRICT */, {.name="tab in URL"  ,.url="/foo\tbar/"  ,.u=    {.field_set=(1 << UF_PATH)    ,.field_data=      {{  0,  0 } /* UF_SCHEMA */      ,{  0,  0 } /* UF_HOST */      ,{  0,  0 } /* UF_PORT */      ,{  0,  9 } /* UF_PATH */      ,{  0,  0 } /* UF_QUERY */      ,{  0,  0 } /* UF_FRAGMENT */      ,{  0,  0 } /* UF_USERINFO */      }    }  ,.rv=0  }, {.name="form feed in URL"  ,.url="/foo\fbar/"  ,.u=    {.field_set=(1 << UF_PATH)    ,.field_data=      {{  0,  0 } /* UF_SCHEMA */      ,{  0,  0 } /* UF_HOST */      ,{  0,  0 } /* UF_PORT */      ,{  0,  9 } /* UF_PATH */      ,{  0,  0 } /* UF_QUERY */      ,{  0,  0 } /* UF_FRAGMENT */      ,{  0,  0 } /* UF_USERINFO */      }    }  ,.rv=0  }#endif};voiddump_url (const char *url, const struct http_parser_url *u){  unsigned int i;  printf("\tfield_set: 0x%x, port: %u\n", u->field_set, u->port);  for (i = 0; i < UF_MAX; i++) {    if ((u->field_set & (1 << i)) == 0) {      printf("\tfield_data[%u]: unset\n", i);      continue;    }    printf("\tfield_data[%u]: off: %u len: %u part: \"%.*s\n\"",           i,           u->field_data[i].off,           u->field_data[i].len,           u->field_data[i].len,           url + u->field_data[i].off);  }}voidtest_parse_url (void){  struct http_parser_url u;  const struct url_test *test;  unsigned int i;  int rv;  for (i = 0; i < (sizeof(url_tests) / sizeof(url_tests[0])); i++) {    test = &url_tests[i];    memset(&u, 0, sizeof(u));    rv = http_parser_parse_url(test->url,                               strlen(test->url),                               test->is_connect,                               &u);    if (test->rv == 0) {      if (rv != 0) {        printf("\n*** http_parser_parse_url(\"%s\") \"%s\" test failed, "               "unexpected rv %d ***\n\n", test->url, test->name, rv);        abort();      }      if (memcmp(&u, &test->u, sizeof(u)) != 0) {        printf("\n*** http_parser_parse_url(\"%s\") \"%s\" failed ***\n",               test->url, test->name);        printf("target http_parser_url:\n");        dump_url(test->url, &test->u);        printf("result http_parser_url:\n");        dump_url(test->url, &u);        abort();      }    } else {      /* test->rv != 0 */      if (rv == 0) {        printf("\n*** http_parser_parse_url(\"%s\") \"%s\" test failed, "               "unexpected rv %d ***\n\n", test->url, test->name, rv);        abort();      }    }  }}voidtest_method_str (void){  assert(0 == strcmp("GET", http_method_str(HTTP_GET)));  assert(0 == strcmp("<unknown>", http_method_str(1337)));}voidtest_message (const struct message *message){  size_t raw_len = strlen(message->raw);  size_t msg1len;  for (msg1len = 0; msg1len < raw_len; msg1len++) {    parser_init(message->type);    size_t read;    const char *msg1 = message->raw;    const char *msg2 = msg1 + msg1len;    size_t msg2len = raw_len - msg1len;    if (msg1len) {      read = parse(msg1, msg1len);      if (message->upgrade && parser->upgrade) {        messages[num_messages - 1].upgrade = msg1 + read;        goto test;      }      if (read != msg1len) {        print_error(msg1, read);        abort();      }    }    read = parse(msg2, msg2len);    if (message->upgrade && parser->upgrade) {      messages[num_messages - 1].upgrade = msg2 + read;      goto test;    }    if (read != msg2len) {      print_error(msg2, read);      abort();    }    read = parse(NULL, 0);    if (read != 0) {      print_error(message->raw, read);      abort();    }  test:    if (num_messages != 1) {      printf("\n*** num_messages != 1 after testing '%s' ***\n\n", message->name);      abort();    }    if(!message_eq(0, message)) abort();    parser_free();  }}voidtest_message_count_body (const struct message *message){  parser_init(message->type);  size_t read;  size_t l = strlen(message->raw);  size_t i, toread;  size_t chunk = 4024;  for (i = 0; i < l; i+= chunk) {    toread = MIN(l-i, chunk);    read = parse_count_body(message->raw + i, toread);    if (read != toread) {      print_error(message->raw, read);      abort();    }  }  read = parse_count_body(NULL, 0);  if (read != 0) {    print_error(message->raw, read);    abort();  }  if (num_messages != 1) {    printf("\n*** num_messages != 1 after testing '%s' ***\n\n", message->name);    abort();  }  if(!message_eq(0, message)) abort();  parser_free();}voidtest_simple (const char *buf, enum http_errno err_expected){  parser_init(HTTP_REQUEST);  size_t parsed;  int pass;  enum http_errno err;  parsed = parse(buf, strlen(buf));  pass = (parsed == strlen(buf));  err = HTTP_PARSER_ERRNO(parser);  parsed = parse(NULL, 0);  pass &= (parsed == 0);  parser_free();  /* In strict mode, allow us to pass with an unexpected HPE_STRICT as   * long as the caller isn't expecting success.   */#if HTTP_PARSER_STRICT  if (err_expected != err && err_expected != HPE_OK && err != HPE_STRICT) {#else  if (err_expected != err) {#endif    fprintf(stderr, "\n*** test_simple expected %s, but saw %s ***\n\n%s\n",        http_errno_name(err_expected), http_errno_name(err), buf);    abort();  }}voidtest_header_overflow_error (int req){  http_parser parser;  http_parser_init(&parser, req ? HTTP_REQUEST : HTTP_RESPONSE);  size_t parsed;  const char *buf;  buf = req ? "GET / HTTP/1.1\r\n" : "HTTP/1.0 200 OK\r\n";  parsed = http_parser_execute(&parser, &settings_null, buf, strlen(buf));  assert(parsed == strlen(buf));  buf = "header-key: header-value\r\n";  size_t buflen = strlen(buf);  int i;  for (i = 0; i < 10000; i++) {    parsed = http_parser_execute(&parser, &settings_null, buf, buflen);    if (parsed != buflen) {      //fprintf(stderr, "error found on iter %d\n", i);      assert(HTTP_PARSER_ERRNO(&parser) == HPE_HEADER_OVERFLOW);      return;    }  }  fprintf(stderr, "\n*** Error expected but none in header overflow test ***\n");  abort();}static voidtest_content_length_overflow (const char *buf, size_t buflen, int expect_ok){  http_parser parser;  http_parser_init(&parser, HTTP_RESPONSE);  http_parser_execute(&parser, &settings_null, buf, buflen);  if (expect_ok)    assert(HTTP_PARSER_ERRNO(&parser) == HPE_OK);  else    assert(HTTP_PARSER_ERRNO(&parser) == HPE_INVALID_CONTENT_LENGTH);}voidtest_header_content_length_overflow_error (void){#define X(size)                                                               \  "HTTP/1.1 200 OK\r\n"                                                       \  "Content-Length: " #size "\r\n"                                             \  "\r\n"  const char a[] = X(18446744073709551614); /* 2^64-2 */  const char b[] = X(18446744073709551615); /* 2^64-1 */  const char c[] = X(18446744073709551616); /* 2^64   */#undef X  test_content_length_overflow(a, sizeof(a) - 1, 1); /* expect ok      */  test_content_length_overflow(b, sizeof(b) - 1, 0); /* expect failure */  test_content_length_overflow(c, sizeof(c) - 1, 0); /* expect failure */}voidtest_chunk_content_length_overflow_error (void){#define X(size)                                                               \    "HTTP/1.1 200 OK\r\n"                                                     \    "Transfer-Encoding: chunked\r\n"                                          \    "\r\n"                                                                    \    #size "\r\n"                                                              \    "..."  const char a[] = X(FFFFFFFFFFFFFFFE);  /* 2^64-2 */  const char b[] = X(FFFFFFFFFFFFFFFF);  /* 2^64-1 */  const char c[] = X(10000000000000000); /* 2^64   */#undef X  test_content_length_overflow(a, sizeof(a) - 1, 1); /* expect ok      */  test_content_length_overflow(b, sizeof(b) - 1, 0); /* expect failure */  test_content_length_overflow(c, sizeof(c) - 1, 0); /* expect failure */}voidtest_no_overflow_long_body (int req, size_t length){  http_parser parser;  http_parser_init(&parser, req ? HTTP_REQUEST : HTTP_RESPONSE);  size_t parsed;  size_t i;  char buf1[3000];  size_t buf1len = sprintf(buf1, "%s\r\nConnection: Keep-Alive\r\nContent-Length: %lu\r\n\r\n",      req ? "POST / HTTP/1.0" : "HTTP/1.0 200 OK", (unsigned long)length);  parsed = http_parser_execute(&parser, &settings_null, buf1, buf1len);  if (parsed != buf1len)    goto err;  for (i = 0; i < length; i++) {    char foo = 'a';    parsed = http_parser_execute(&parser, &settings_null, &foo, 1);    if (parsed != 1)      goto err;  }  parsed = http_parser_execute(&parser, &settings_null, buf1, buf1len);  if (parsed != buf1len) goto err;  return; err:  fprintf(stderr,          "\n*** error in test_no_overflow_long_body %s of length %lu ***\n",          req ? "REQUEST" : "RESPONSE",          (unsigned long)length);  abort();}voidtest_multiple3 (const struct message *r1, const struct message *r2, const struct message *r3){  int message_count = count_parsed_messages(3, r1, r2, r3);  char total[ strlen(r1->raw)            + strlen(r2->raw)            + strlen(r3->raw)            + 1            ];  total[0] = '\0';  strcat(total, r1->raw);  strcat(total, r2->raw);  strcat(total, r3->raw);  parser_init(r1->type);  size_t read;  read = parse(total, strlen(total));  if (parser->upgrade) {    upgrade_message_fix(total, read, 3, r1, r2, r3);    goto test;  }  if (read != strlen(total)) {    print_error(total, read);    abort();  }  read = parse(NULL, 0);  if (read != 0) {    print_error(total, read);    abort();  }test:  if (message_count != num_messages) {    fprintf(stderr, "\n\n*** Parser didn't see 3 messages only %d *** \n", num_messages);    abort();  }  if (!message_eq(0, r1)) abort();  if (message_count > 1 && !message_eq(1, r2)) abort();  if (message_count > 2 && !message_eq(2, r3)) abort();  parser_free();}/* SCAN through every possible breaking to make sure the * parser can handle getting the content in any chunks that * might come from the socket */voidtest_scan (const struct message *r1, const struct message *r2, const struct message *r3){  char total[80*1024] = "\0";  char buf1[80*1024] = "\0";  char buf2[80*1024] = "\0";  char buf3[80*1024] = "\0";  strcat(total, r1->raw);  strcat(total, r2->raw);  strcat(total, r3->raw);  size_t read;  int total_len = strlen(total);  int total_ops = 2 * (total_len - 1) * (total_len - 2) / 2;  int ops = 0 ;  size_t buf1_len, buf2_len, buf3_len;  int message_count = count_parsed_messages(3, r1, r2, r3);  int i,j,type_both;  for (type_both = 0; type_both < 2; type_both ++ ) {    for (j = 2; j < total_len; j ++ ) {      for (i = 1; i < j; i ++ ) {        if (ops % 1000 == 0)  {          printf("\b\b\b\b%3.0f%%", 100 * (float)ops /(float)total_ops);          fflush(stdout);        }        ops += 1;        parser_init(type_both ? HTTP_BOTH : r1->type);        buf1_len = i;        strlncpy(buf1, sizeof(buf1), total, buf1_len);        buf1[buf1_len] = 0;        buf2_len = j - i;        strlncpy(buf2, sizeof(buf1), total+i, buf2_len);        buf2[buf2_len] = 0;        buf3_len = total_len - j;        strlncpy(buf3, sizeof(buf1), total+j, buf3_len);        buf3[buf3_len] = 0;        read = parse(buf1, buf1_len);        if (parser->upgrade) goto test;        if (read != buf1_len) {          print_error(buf1, read);          goto error;        }        read += parse(buf2, buf2_len);        if (parser->upgrade) goto test;        if (read != buf1_len + buf2_len) {          print_error(buf2, read);          goto error;        }        read += parse(buf3, buf3_len);        if (parser->upgrade) goto test;        if (read != buf1_len + buf2_len + buf3_len) {          print_error(buf3, read);          goto error;        }        parse(NULL, 0);test:        if (parser->upgrade) {          upgrade_message_fix(total, read, 3, r1, r2, r3);        }        if (message_count != num_messages) {          fprintf(stderr, "\n\nParser didn't see %d messages only %d\n",            message_count, num_messages);          goto error;        }        if (!message_eq(0, r1)) {          fprintf(stderr, "\n\nError matching messages[0] in test_scan.\n");          goto error;        }        if (message_count > 1 && !message_eq(1, r2)) {          fprintf(stderr, "\n\nError matching messages[1] in test_scan.\n");          goto error;        }        if (message_count > 2 && !message_eq(2, r3)) {          fprintf(stderr, "\n\nError matching messages[2] in test_scan.\n");          goto error;        }        parser_free();      }    }  }  puts("\b\b\b\b100%");  return; error:  fprintf(stderr, "i=%d  j=%d\n", i, j);  fprintf(stderr, "buf1 (%u) %s\n\n", (unsigned int)buf1_len, buf1);  fprintf(stderr, "buf2 (%u) %s\n\n", (unsigned int)buf2_len , buf2);  fprintf(stderr, "buf3 (%u) %s\n", (unsigned int)buf3_len, buf3);  abort();}// user required to free the result// string terminated by \0char *create_large_chunked_message (int body_size_in_kb, const char* headers){  int i;  size_t wrote = 0;  size_t headers_len = strlen(headers);  size_t bufsize = headers_len + (5+1024+2)*body_size_in_kb + 6;  char * buf = malloc(bufsize);  memcpy(buf, headers, headers_len);  wrote += headers_len;  for (i = 0; i < body_size_in_kb; i++) {    // write 1kb chunk into the body.    memcpy(buf + wrote, "400\r\n", 5);    wrote += 5;    memset(buf + wrote, 'C', 1024);    wrote += 1024;    strcpy(buf + wrote, "\r\n");    wrote += 2;  }  memcpy(buf + wrote, "0\r\n\r\n", 6);  wrote += 6;  assert(wrote == bufsize);  return buf;}voidtest_status_complete (void){  parser_init(HTTP_RESPONSE);  parser->data = 0;  http_parser_settings settings = settings_null;  settings.on_status_complete = status_complete_cb;  char *response = "don't mind me, just a simple response";  http_parser_execute(parser, &settings, response, strlen(response));  assert(parser->data == (void*)0); // the status_complete callback was never called  assert(parser->http_errno == HPE_INVALID_CONSTANT); // the errno for an invalid status line}/* Verify that we can pause parsing at any of the bytes in the * message and still get the result that we're expecting. */voidtest_message_pause (const struct message *msg){  char *buf = (char*) msg->raw;  size_t buflen = strlen(msg->raw);  size_t nread;  parser_init(msg->type);  do {    nread = parse_pause(buf, buflen);    // We can only set the upgrade buffer once we've gotten our message    // completion callback.    if (messages[0].message_complete_cb_called &&        msg->upgrade &&        parser->upgrade) {      messages[0].upgrade = buf + nread;      goto test;    }    if (nread < buflen) {      // Not much do to if we failed a strict-mode check      if (HTTP_PARSER_ERRNO(parser) == HPE_STRICT) {        parser_free();        return;      }      assert (HTTP_PARSER_ERRNO(parser) == HPE_PAUSED);    }    buf += nread;    buflen -= nread;    http_parser_pause(parser, 0);  } while (buflen > 0);  nread = parse_pause(NULL, 0);  assert (nread == 0);test:  if (num_messages != 1) {    printf("\n*** num_messages != 1 after testing '%s' ***\n\n", msg->name);    abort();  }  if(!message_eq(0, msg)) abort();  parser_free();}intmain (void){  parser = NULL;  int i, j, k;  int request_count;  int response_count;  printf("sizeof(http_parser) = %u\n", (unsigned int)sizeof(http_parser));  for (request_count = 0; requests[request_count].name; request_count++);  for (response_count = 0; responses[response_count].name; response_count++);  //// API  test_preserve_data();  test_parse_url();  test_method_str();  //// OVERFLOW CONDITIONS  test_header_overflow_error(HTTP_REQUEST);  test_no_overflow_long_body(HTTP_REQUEST, 1000);  test_no_overflow_long_body(HTTP_REQUEST, 100000);  test_header_overflow_error(HTTP_RESPONSE);  test_no_overflow_long_body(HTTP_RESPONSE, 1000);  test_no_overflow_long_body(HTTP_RESPONSE, 100000);  test_header_content_length_overflow_error();  test_chunk_content_length_overflow_error();  //// RESPONSES  for (i = 0; i < response_count; i++) {    test_message(&responses[i]);  }  for (i = 0; i < response_count; i++) {    test_message_pause(&responses[i]);  }  for (i = 0; i < response_count; i++) {    if (!responses[i].should_keep_alive) continue;    for (j = 0; j < response_count; j++) {      if (!responses[j].should_keep_alive) continue;      for (k = 0; k < response_count; k++) {        test_multiple3(&responses[i], &responses[j], &responses[k]);      }    }  }  test_message_count_body(&responses[NO_HEADERS_NO_BODY_404]);  test_message_count_body(&responses[TRAILING_SPACE_ON_CHUNKED_BODY]);  // test very large chunked response  {    char * msg = create_large_chunked_message(31337,      "HTTP/1.0 200 OK\r\n"      "Transfer-Encoding: chunked\r\n"      "Content-Type: text/plain\r\n"      "\r\n");    struct message large_chunked =      {.name= "large chunked"      ,.type= HTTP_RESPONSE      ,.raw= msg      ,.should_keep_alive= FALSE      ,.message_complete_on_eof= FALSE      ,.http_major= 1      ,.http_minor= 0      ,.status_code= 200      ,.num_headers= 2      ,.headers=        { { "Transfer-Encoding", "chunked" }        , { "Content-Type", "text/plain" }        }      ,.body_size= 31337*1024      };    test_message_count_body(&large_chunked);    free(msg);  }  printf("response scan 1/2      ");  test_scan( &responses[TRAILING_SPACE_ON_CHUNKED_BODY]           , &responses[NO_BODY_HTTP10_KA_204]           , &responses[NO_REASON_PHRASE]           );  printf("response scan 2/2      ");  test_scan( &responses[BONJOUR_MADAME_FR]           , &responses[UNDERSTORE_HEADER_KEY]           , &responses[NO_CARRIAGE_RET]           );  puts("responses okay");  /// REQUESTS  test_simple("hello world", HPE_INVALID_METHOD);  test_simple("GET / HTP/1.1\r\n\r\n", HPE_INVALID_VERSION);  test_simple("ASDF / HTTP/1.1\r\n\r\n", HPE_INVALID_METHOD);  test_simple("PROPPATCHA / HTTP/1.1\r\n\r\n", HPE_INVALID_METHOD);  test_simple("GETA / HTTP/1.1\r\n\r\n", HPE_INVALID_METHOD);  // Well-formed but incomplete  test_simple("GET / HTTP/1.1\r\n"              "Content-Type: text/plain\r\n"              "Content-Length: 6\r\n"              "\r\n"              "fooba",              HPE_OK);  static const char *all_methods[] = {    "DELETE",    "GET",    "HEAD",    "POST",    "PUT",    //"CONNECT", //CONNECT can't be tested like other methods, it's a tunnel    "OPTIONS",    "TRACE",    "COPY",    "LOCK",    "MKCOL",    "MOVE",    "PROPFIND",    "PROPPATCH",    "UNLOCK",    "REPORT",    "MKACTIVITY",    "CHECKOUT",    "MERGE",    "M-SEARCH",    "NOTIFY",    "SUBSCRIBE",    "UNSUBSCRIBE",    "PATCH",    0 };  const char **this_method;  for (this_method = all_methods; *this_method; this_method++) {    char buf[200];    sprintf(buf, "%s / HTTP/1.1\r\n\r\n", *this_method);    test_simple(buf, HPE_OK);  }  static const char *bad_methods[] = {      "C******",      "M****",      0 };  for (this_method = bad_methods; *this_method; this_method++) {    char buf[200];    sprintf(buf, "%s / HTTP/1.1\r\n\r\n", *this_method);    test_simple(buf, HPE_UNKNOWN);  }  const char *dumbfuck2 =    "GET / HTTP/1.1\r\n"    "X-SSL-Bullshit:   -----BEGIN CERTIFICATE-----\r\n"    "\tMIIFbTCCBFWgAwIBAgICH4cwDQYJKoZIhvcNAQEFBQAwcDELMAkGA1UEBhMCVUsx\r\n"    "\tETAPBgNVBAoTCGVTY2llbmNlMRIwEAYDVQQLEwlBdXRob3JpdHkxCzAJBgNVBAMT\r\n"    "\tAkNBMS0wKwYJKoZIhvcNAQkBFh5jYS1vcGVyYXRvckBncmlkLXN1cHBvcnQuYWMu\r\n"    "\tdWswHhcNMDYwNzI3MTQxMzI4WhcNMDcwNzI3MTQxMzI4WjBbMQswCQYDVQQGEwJV\r\n"    "\tSzERMA8GA1UEChMIZVNjaWVuY2UxEzARBgNVBAsTCk1hbmNoZXN0ZXIxCzAJBgNV\r\n"    "\tBAcTmrsogriqMWLAk1DMRcwFQYDVQQDEw5taWNoYWVsIHBhcmQYJKoZIhvcNAQEB\r\n"    "\tBQADggEPADCCAQoCggEBANPEQBgl1IaKdSS1TbhF3hEXSl72G9J+WC/1R64fAcEF\r\n"    "\tW51rEyFYiIeZGx/BVzwXbeBoNUK41OK65sxGuflMo5gLflbwJtHBRIEKAfVVp3YR\r\n"    "\tgW7cMA/s/XKgL1GEC7rQw8lIZT8RApukCGqOVHSi/F1SiFlPDxuDfmdiNzL31+sL\r\n"    "\t0iwHDdNkGjy5pyBSB8Y79dsSJtCW/iaLB0/n8Sj7HgvvZJ7x0fr+RQjYOUUfrePP\r\n"    "\tu2MSpFyf+9BbC/aXgaZuiCvSR+8Snv3xApQY+fULK/xY8h8Ua51iXoQ5jrgu2SqR\r\n"    "\twgA7BUi3G8LFzMBl8FRCDYGUDy7M6QaHXx1ZWIPWNKsCAwEAAaOCAiQwggIgMAwG\r\n"    "\tA1UdEwEB/wQCMAAwEQYJYIZIAYb4QgHTTPAQDAgWgMA4GA1UdDwEB/wQEAwID6DAs\r\n"    "\tBglghkgBhvhCAQ0EHxYdVUsgZS1TY2llbmNlIFVzZXIgQ2VydGlmaWNhdGUwHQYD\r\n"    "\tVR0OBBYEFDTt/sf9PeMaZDHkUIldrDYMNTBZMIGaBgNVHSMEgZIwgY+AFAI4qxGj\r\n"    "\tloCLDdMVKwiljjDastqooXSkcjBwMQswCQYDVQQGEwJVSzERMA8GA1UEChMIZVNj\r\n"    "\taWVuY2UxEjAQBgNVBAsTCUF1dGhvcml0eTELMAkGA1UEAxMCQ0ExLTArBgkqhkiG\r\n"    "\t9w0BCQEWHmNhLW9wZXJhdG9yQGdyaWQtc3VwcG9ydC5hYy51a4IBADApBgNVHRIE\r\n"    "\tIjAggR5jYS1vcGVyYXRvckBncmlkLXN1cHBvcnQuYWMudWswGQYDVR0gBBIwEDAO\r\n"    "\tBgwrBgEEAdkvAQEBAQYwPQYJYIZIAYb4QgEEBDAWLmh0dHA6Ly9jYS5ncmlkLXN1\r\n"    "\tcHBvcnQuYWMudmT4sopwqlBWsvcHViL2NybC9jYWNybC5jcmwwPQYJYIZIAYb4QgEDBDAWLmh0\r\n"    "\tdHA6Ly9jYS5ncmlkLXN1cHBvcnQuYWMudWsvcHViL2NybC9jYWNybC5jcmwwPwYD\r\n"    "\tVR0fBDgwNjA0oDKgMIYuaHR0cDovL2NhLmdyaWQt5hYy51ay9wdWIv\r\n"    "\tY3JsL2NhY3JsLmNybDANBgkqhkiG9w0BAQUFAAOCAQEAS/U4iiooBENGW/Hwmmd3\r\n"    "\tXCy6Zrt08YjKCzGNjorT98g8uGsqYjSxv/hmi0qlnlHs+k/3Iobc3LjS5AMYr5L8\r\n"    "\tUO7OSkgFFlLHQyC9JzPfmLCAugvzEbyv4Olnsr8hbxF1MbKZoQxUZtMVu29wjfXk\r\n"    "\thTeApBv7eaKCWpSp7MCbvgzm74izKhu3vlDk9w6qVrxePfGgpKPqfHiOoGhFnbTK\r\n"    "\twTC6o2xq5y0qZ03JonF7OJspEd3I5zKY3E+ov7/ZhW6DqT8UFvsAdjvQbXyhV8Eu\r\n"    "\tYhixw1aKEPzNjNowuIseVogKOLXxWI5vAi5HgXdS0/ES5gDGsABo4fqovUKlgop3\r\n"    "\tRA==\r\n"    "\t-----END CERTIFICATE-----\r\n"    "\r\n";  test_simple(dumbfuck2, HPE_OK);#if 0  // NOTE(Wed Nov 18 11:57:27 CET 2009) this seems okay. we just read body  // until EOF.  //  // no content-length  // error if there is a body without content length  const char *bad_get_no_headers_no_body = "GET /bad_get_no_headers_no_body/world HTTP/1.1\r\n"                                           "Accept: */*\r\n"                                           "\r\n"                                           "HELLO";  test_simple(bad_get_no_headers_no_body, 0);#endif  /* TODO sending junk and large headers gets rejected */  /* check to make sure our predefined requests are okay */  for (i = 0; requests[i].name; i++) {    test_message(&requests[i]);  }  for (i = 0; i < request_count; i++) {    test_message_pause(&requests[i]);  }  for (i = 0; i < request_count; i++) {    if (!requests[i].should_keep_alive) continue;    for (j = 0; j < request_count; j++) {      if (!requests[j].should_keep_alive) continue;      for (k = 0; k < request_count; k++) {        test_multiple3(&requests[i], &requests[j], &requests[k]);      }    }  }  printf("request scan 1/4      ");  test_scan( &requests[GET_NO_HEADERS_NO_BODY]           , &requests[GET_ONE_HEADER_NO_BODY]           , &requests[GET_NO_HEADERS_NO_BODY]           );  printf("request scan 2/4      ");  test_scan( &requests[POST_CHUNKED_ALL_YOUR_BASE]           , &requests[POST_IDENTITY_BODY_WORLD]           , &requests[GET_FUNKY_CONTENT_LENGTH]           );  printf("request scan 3/4      ");  test_scan( &requests[TWO_CHUNKS_MULT_ZERO_END]           , &requests[CHUNKED_W_TRAILING_HEADERS]           , &requests[CHUNKED_W_BULLSHIT_AFTER_LENGTH]           );  printf("request scan 4/4      ");  test_scan( &requests[QUERY_URL_WITH_QUESTION_MARK_GET]           , &requests[PREFIX_NEWLINE_GET ]           , &requests[CONNECT_REQUEST]           );  test_status_complete();  puts("requests okay");  return 0;}
 |