test.c 88 KB

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