test.c 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952
  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 500
  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. int num_headers;
  49. enum { NONE=0, FIELD, VALUE } last_header_element;
  50. char headers [MAX_HEADERS][2][MAX_ELEMENT_SIZE];
  51. int should_keep_alive;
  52. int upgrade;
  53. unsigned short http_major;
  54. unsigned short http_minor;
  55. int message_begin_cb_called;
  56. int headers_complete_cb_called;
  57. int message_complete_cb_called;
  58. int message_complete_on_eof;
  59. };
  60. static int currently_parsing_eof;
  61. static struct message messages[5];
  62. static int num_messages;
  63. /* * R E Q U E S T S * */
  64. const struct message requests[] =
  65. #define CURL_GET 0
  66. { {.name= "curl get"
  67. ,.type= HTTP_REQUEST
  68. ,.raw= "GET /test HTTP/1.1\r\n"
  69. "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"
  70. "Host: 0.0.0.0=5000\r\n"
  71. "Accept: */*\r\n"
  72. "\r\n"
  73. ,.should_keep_alive= TRUE
  74. ,.message_complete_on_eof= FALSE
  75. ,.http_major= 1
  76. ,.http_minor= 1
  77. ,.method= HTTP_GET
  78. ,.query_string= ""
  79. ,.fragment= ""
  80. ,.request_path= "/test"
  81. ,.request_url= "/test"
  82. ,.num_headers= 3
  83. ,.headers=
  84. { { "User-Agent", "curl/7.18.0 (i486-pc-linux-gnu) libcurl/7.18.0 OpenSSL/0.9.8g zlib/1.2.3.3 libidn/1.1" }
  85. , { "Host", "0.0.0.0=5000" }
  86. , { "Accept", "*/*" }
  87. }
  88. ,.body= ""
  89. }
  90. #define FIREFOX_GET 1
  91. , {.name= "firefox get"
  92. ,.type= HTTP_REQUEST
  93. ,.raw= "GET /favicon.ico HTTP/1.1\r\n"
  94. "Host: 0.0.0.0=5000\r\n"
  95. "User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9) Gecko/2008061015 Firefox/3.0\r\n"
  96. "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\n"
  97. "Accept-Language: en-us,en;q=0.5\r\n"
  98. "Accept-Encoding: gzip,deflate\r\n"
  99. "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7\r\n"
  100. "Keep-Alive: 300\r\n"
  101. "Connection: keep-alive\r\n"
  102. "\r\n"
  103. ,.should_keep_alive= TRUE
  104. ,.message_complete_on_eof= FALSE
  105. ,.http_major= 1
  106. ,.http_minor= 1
  107. ,.method= HTTP_GET
  108. ,.query_string= ""
  109. ,.fragment= ""
  110. ,.request_path= "/favicon.ico"
  111. ,.request_url= "/favicon.ico"
  112. ,.num_headers= 8
  113. ,.headers=
  114. { { "Host", "0.0.0.0=5000" }
  115. , { "User-Agent", "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9) Gecko/2008061015 Firefox/3.0" }
  116. , { "Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8" }
  117. , { "Accept-Language", "en-us,en;q=0.5" }
  118. , { "Accept-Encoding", "gzip,deflate" }
  119. , { "Accept-Charset", "ISO-8859-1,utf-8;q=0.7,*;q=0.7" }
  120. , { "Keep-Alive", "300" }
  121. , { "Connection", "keep-alive" }
  122. }
  123. ,.body= ""
  124. }
  125. #define DUMBFUCK 2
  126. , {.name= "dumbfuck"
  127. ,.type= HTTP_REQUEST
  128. ,.raw= "GET /dumbfuck HTTP/1.1\r\n"
  129. "aaaaaaaaaaaaa:++++++++++\r\n"
  130. "\r\n"
  131. ,.should_keep_alive= TRUE
  132. ,.message_complete_on_eof= FALSE
  133. ,.http_major= 1
  134. ,.http_minor= 1
  135. ,.method= HTTP_GET
  136. ,.query_string= ""
  137. ,.fragment= ""
  138. ,.request_path= "/dumbfuck"
  139. ,.request_url= "/dumbfuck"
  140. ,.num_headers= 1
  141. ,.headers=
  142. { { "aaaaaaaaaaaaa", "++++++++++" }
  143. }
  144. ,.body= ""
  145. }
  146. #define FRAGMENT_IN_URI 3
  147. , {.name= "fragment in url"
  148. ,.type= HTTP_REQUEST
  149. ,.raw= "GET /forums/1/topics/2375?page=1#posts-17408 HTTP/1.1\r\n"
  150. "\r\n"
  151. ,.should_keep_alive= TRUE
  152. ,.message_complete_on_eof= FALSE
  153. ,.http_major= 1
  154. ,.http_minor= 1
  155. ,.method= HTTP_GET
  156. ,.query_string= "page=1"
  157. ,.fragment= "posts-17408"
  158. ,.request_path= "/forums/1/topics/2375"
  159. /* XXX request url does include fragment? */
  160. ,.request_url= "/forums/1/topics/2375?page=1#posts-17408"
  161. ,.num_headers= 0
  162. ,.body= ""
  163. }
  164. #define GET_NO_HEADERS_NO_BODY 4
  165. , {.name= "get no headers no body"
  166. ,.type= HTTP_REQUEST
  167. ,.raw= "GET /get_no_headers_no_body/world HTTP/1.1\r\n"
  168. "\r\n"
  169. ,.should_keep_alive= TRUE
  170. ,.message_complete_on_eof= FALSE /* would need Connection: close */
  171. ,.http_major= 1
  172. ,.http_minor= 1
  173. ,.method= HTTP_GET
  174. ,.query_string= ""
  175. ,.fragment= ""
  176. ,.request_path= "/get_no_headers_no_body/world"
  177. ,.request_url= "/get_no_headers_no_body/world"
  178. ,.num_headers= 0
  179. ,.body= ""
  180. }
  181. #define GET_ONE_HEADER_NO_BODY 5
  182. , {.name= "get one header no body"
  183. ,.type= HTTP_REQUEST
  184. ,.raw= "GET /get_one_header_no_body HTTP/1.1\r\n"
  185. "Accept: */*\r\n"
  186. "\r\n"
  187. ,.should_keep_alive= TRUE
  188. ,.message_complete_on_eof= FALSE /* would need Connection: close */
  189. ,.http_major= 1
  190. ,.http_minor= 1
  191. ,.method= HTTP_GET
  192. ,.query_string= ""
  193. ,.fragment= ""
  194. ,.request_path= "/get_one_header_no_body"
  195. ,.request_url= "/get_one_header_no_body"
  196. ,.num_headers= 1
  197. ,.headers=
  198. { { "Accept" , "*/*" }
  199. }
  200. ,.body= ""
  201. }
  202. #define GET_FUNKY_CONTENT_LENGTH 6
  203. , {.name= "get funky content length body hello"
  204. ,.type= HTTP_REQUEST
  205. ,.raw= "GET /get_funky_content_length_body_hello HTTP/1.0\r\n"
  206. "conTENT-Length: 5\r\n"
  207. "\r\n"
  208. "HELLO"
  209. ,.should_keep_alive= FALSE
  210. ,.message_complete_on_eof= FALSE
  211. ,.http_major= 1
  212. ,.http_minor= 0
  213. ,.method= HTTP_GET
  214. ,.query_string= ""
  215. ,.fragment= ""
  216. ,.request_path= "/get_funky_content_length_body_hello"
  217. ,.request_url= "/get_funky_content_length_body_hello"
  218. ,.num_headers= 1
  219. ,.headers=
  220. { { "conTENT-Length" , "5" }
  221. }
  222. ,.body= "HELLO"
  223. }
  224. #define POST_IDENTITY_BODY_WORLD 7
  225. , {.name= "post identity body world"
  226. ,.type= HTTP_REQUEST
  227. ,.raw= "POST /post_identity_body_world?q=search#hey HTTP/1.1\r\n"
  228. "Accept: */*\r\n"
  229. "Transfer-Encoding: identity\r\n"
  230. "Content-Length: 5\r\n"
  231. "\r\n"
  232. "World"
  233. ,.should_keep_alive= TRUE
  234. ,.message_complete_on_eof= FALSE
  235. ,.http_major= 1
  236. ,.http_minor= 1
  237. ,.method= HTTP_POST
  238. ,.query_string= "q=search"
  239. ,.fragment= "hey"
  240. ,.request_path= "/post_identity_body_world"
  241. ,.request_url= "/post_identity_body_world?q=search#hey"
  242. ,.num_headers= 3
  243. ,.headers=
  244. { { "Accept", "*/*" }
  245. , { "Transfer-Encoding", "identity" }
  246. , { "Content-Length", "5" }
  247. }
  248. ,.body= "World"
  249. }
  250. #define POST_CHUNKED_ALL_YOUR_BASE 8
  251. , {.name= "post - chunked body: all your base are belong to us"
  252. ,.type= HTTP_REQUEST
  253. ,.raw= "POST /post_chunked_all_your_base HTTP/1.1\r\n"
  254. "Transfer-Encoding: chunked\r\n"
  255. "\r\n"
  256. "1e\r\nall your base are belong to us\r\n"
  257. "0\r\n"
  258. "\r\n"
  259. ,.should_keep_alive= TRUE
  260. ,.message_complete_on_eof= FALSE
  261. ,.http_major= 1
  262. ,.http_minor= 1
  263. ,.method= HTTP_POST
  264. ,.query_string= ""
  265. ,.fragment= ""
  266. ,.request_path= "/post_chunked_all_your_base"
  267. ,.request_url= "/post_chunked_all_your_base"
  268. ,.num_headers= 1
  269. ,.headers=
  270. { { "Transfer-Encoding" , "chunked" }
  271. }
  272. ,.body= "all your base are belong to us"
  273. }
  274. #define TWO_CHUNKS_MULT_ZERO_END 9
  275. , {.name= "two chunks ; triple zero ending"
  276. ,.type= HTTP_REQUEST
  277. ,.raw= "POST /two_chunks_mult_zero_end HTTP/1.1\r\n"
  278. "Transfer-Encoding: chunked\r\n"
  279. "\r\n"
  280. "5\r\nhello\r\n"
  281. "6\r\n world\r\n"
  282. "000\r\n"
  283. "\r\n"
  284. ,.should_keep_alive= TRUE
  285. ,.message_complete_on_eof= FALSE
  286. ,.http_major= 1
  287. ,.http_minor= 1
  288. ,.method= HTTP_POST
  289. ,.query_string= ""
  290. ,.fragment= ""
  291. ,.request_path= "/two_chunks_mult_zero_end"
  292. ,.request_url= "/two_chunks_mult_zero_end"
  293. ,.num_headers= 1
  294. ,.headers=
  295. { { "Transfer-Encoding", "chunked" }
  296. }
  297. ,.body= "hello world"
  298. }
  299. #define CHUNKED_W_TRAILING_HEADERS 10
  300. , {.name= "chunked with trailing headers. blech."
  301. ,.type= HTTP_REQUEST
  302. ,.raw= "POST /chunked_w_trailing_headers HTTP/1.1\r\n"
  303. "Transfer-Encoding: chunked\r\n"
  304. "\r\n"
  305. "5\r\nhello\r\n"
  306. "6\r\n world\r\n"
  307. "0\r\n"
  308. "Vary: *\r\n"
  309. "Content-Type: text/plain\r\n"
  310. "\r\n"
  311. ,.should_keep_alive= TRUE
  312. ,.message_complete_on_eof= FALSE
  313. ,.http_major= 1
  314. ,.http_minor= 1
  315. ,.method= HTTP_POST
  316. ,.query_string= ""
  317. ,.fragment= ""
  318. ,.request_path= "/chunked_w_trailing_headers"
  319. ,.request_url= "/chunked_w_trailing_headers"
  320. ,.num_headers= 3
  321. ,.headers=
  322. { { "Transfer-Encoding", "chunked" }
  323. , { "Vary", "*" }
  324. , { "Content-Type", "text/plain" }
  325. }
  326. ,.body= "hello world"
  327. }
  328. #define CHUNKED_W_BULLSHIT_AFTER_LENGTH 11
  329. , {.name= "with bullshit after the length"
  330. ,.type= HTTP_REQUEST
  331. ,.raw= "POST /chunked_w_bullshit_after_length HTTP/1.1\r\n"
  332. "Transfer-Encoding: chunked\r\n"
  333. "\r\n"
  334. "5; ihatew3;whatthefuck=aretheseparametersfor\r\nhello\r\n"
  335. "6; blahblah; blah\r\n world\r\n"
  336. "0\r\n"
  337. "\r\n"
  338. ,.should_keep_alive= TRUE
  339. ,.message_complete_on_eof= FALSE
  340. ,.http_major= 1
  341. ,.http_minor= 1
  342. ,.method= HTTP_POST
  343. ,.query_string= ""
  344. ,.fragment= ""
  345. ,.request_path= "/chunked_w_bullshit_after_length"
  346. ,.request_url= "/chunked_w_bullshit_after_length"
  347. ,.num_headers= 1
  348. ,.headers=
  349. { { "Transfer-Encoding", "chunked" }
  350. }
  351. ,.body= "hello world"
  352. }
  353. #define WITH_QUOTES 12
  354. , {.name= "with quotes"
  355. ,.type= HTTP_REQUEST
  356. ,.raw= "GET /with_\"stupid\"_quotes?foo=\"bar\" HTTP/1.1\r\n\r\n"
  357. ,.should_keep_alive= TRUE
  358. ,.message_complete_on_eof= FALSE
  359. ,.http_major= 1
  360. ,.http_minor= 1
  361. ,.method= HTTP_GET
  362. ,.query_string= "foo=\"bar\""
  363. ,.fragment= ""
  364. ,.request_path= "/with_\"stupid\"_quotes"
  365. ,.request_url= "/with_\"stupid\"_quotes?foo=\"bar\""
  366. ,.num_headers= 0
  367. ,.headers= { }
  368. ,.body= ""
  369. }
  370. #define APACHEBENCH_GET 13
  371. /* The server receiving this request SHOULD NOT wait for EOF
  372. * to know that content-length == 0.
  373. * How to represent this in a unit test? message_complete_on_eof
  374. * Compare with NO_CONTENT_LENGTH_RESPONSE.
  375. */
  376. , {.name = "apachebench get"
  377. ,.type= HTTP_REQUEST
  378. ,.raw= "GET /test HTTP/1.0\r\n"
  379. "Host: 0.0.0.0:5000\r\n"
  380. "User-Agent: ApacheBench/2.3\r\n"
  381. "Accept: */*\r\n\r\n"
  382. ,.should_keep_alive= FALSE
  383. ,.message_complete_on_eof= FALSE
  384. ,.http_major= 1
  385. ,.http_minor= 0
  386. ,.method= HTTP_GET
  387. ,.query_string= ""
  388. ,.fragment= ""
  389. ,.request_path= "/test"
  390. ,.request_url= "/test"
  391. ,.num_headers= 3
  392. ,.headers= { { "Host", "0.0.0.0:5000" }
  393. , { "User-Agent", "ApacheBench/2.3" }
  394. , { "Accept", "*/*" }
  395. }
  396. ,.body= ""
  397. }
  398. #define QUERY_URL_WITH_QUESTION_MARK_GET 14
  399. /* Some clients include '?' characters in query strings.
  400. */
  401. , {.name = "query url with question mark"
  402. ,.type= HTTP_REQUEST
  403. ,.raw= "GET /test.cgi?foo=bar?baz HTTP/1.1\r\n\r\n"
  404. ,.should_keep_alive= TRUE
  405. ,.message_complete_on_eof= FALSE
  406. ,.http_major= 1
  407. ,.http_minor= 1
  408. ,.method= HTTP_GET
  409. ,.query_string= "foo=bar?baz"
  410. ,.fragment= ""
  411. ,.request_path= "/test.cgi"
  412. ,.request_url= "/test.cgi?foo=bar?baz"
  413. ,.num_headers= 0
  414. ,.headers= {}
  415. ,.body= ""
  416. }
  417. #define PREFIX_NEWLINE_GET 15
  418. /* Some clients, especially after a POST in a keep-alive connection,
  419. * will send an extra CRLF before the next request
  420. */
  421. , {.name = "newline prefix get"
  422. ,.type= HTTP_REQUEST
  423. ,.raw= "\r\nGET /test HTTP/1.1\r\n\r\n"
  424. ,.should_keep_alive= TRUE
  425. ,.message_complete_on_eof= FALSE
  426. ,.http_major= 1
  427. ,.http_minor= 1
  428. ,.method= HTTP_GET
  429. ,.query_string= ""
  430. ,.fragment= ""
  431. ,.request_path= "/test"
  432. ,.request_url= "/test"
  433. ,.num_headers= 0
  434. ,.headers= { }
  435. ,.body= ""
  436. }
  437. #define UPGRADE_REQUEST 16
  438. , {.name = "upgrade request"
  439. ,.type= HTTP_REQUEST
  440. ,.raw= "GET /demo HTTP/1.1\r\n"
  441. "Host: example.com\r\n"
  442. "Connection: Upgrade\r\n"
  443. "Sec-WebSocket-Key2: 12998 5 Y3 1 .P00\r\n"
  444. "Sec-WebSocket-Protocol: sample\r\n"
  445. "Upgrade: WebSocket\r\n"
  446. "Sec-WebSocket-Key1: 4 @1 46546xW%0l 1 5\r\n"
  447. "Origin: http://example.com\r\n"
  448. "\r\n"
  449. ,.should_keep_alive= TRUE
  450. ,.message_complete_on_eof= FALSE
  451. ,.http_major= 1
  452. ,.http_minor= 1
  453. ,.method= HTTP_GET
  454. ,.query_string= ""
  455. ,.fragment= ""
  456. ,.request_path= "/demo"
  457. ,.request_url= "/demo"
  458. ,.num_headers= 7
  459. ,.upgrade=1
  460. ,.headers= { { "Host", "example.com" }
  461. , { "Connection", "Upgrade" }
  462. , { "Sec-WebSocket-Key2", "12998 5 Y3 1 .P00" }
  463. , { "Sec-WebSocket-Protocol", "sample" }
  464. , { "Upgrade", "WebSocket" }
  465. , { "Sec-WebSocket-Key1", "4 @1 46546xW%0l 1 5" }
  466. , { "Origin", "http://example.com" }
  467. }
  468. ,.body= ""
  469. }
  470. #define CONNECT_REQUEST 17
  471. , {.name = "connect request"
  472. ,.type= HTTP_REQUEST
  473. ,.raw= "CONNECT home0.netscape.com:443 HTTP/1.0\r\n"
  474. "User-agent: Mozilla/1.1N\r\n"
  475. "Proxy-authorization: basic aGVsbG86d29ybGQ=\r\n"
  476. "\r\n"
  477. ,.should_keep_alive= FALSE
  478. ,.message_complete_on_eof= FALSE
  479. ,.http_major= 1
  480. ,.http_minor= 0
  481. ,.method= HTTP_CONNECT
  482. ,.query_string= ""
  483. ,.fragment= ""
  484. ,.request_path= ""
  485. ,.request_url= "home0.netscape.com:443"
  486. ,.num_headers= 2
  487. ,.upgrade=1
  488. ,.headers= { { "User-agent", "Mozilla/1.1N" }
  489. , { "Proxy-authorization", "basic aGVsbG86d29ybGQ=" }
  490. }
  491. ,.body= ""
  492. }
  493. #define REPORT_REQ 18
  494. , {.name= "report request"
  495. ,.type= HTTP_REQUEST
  496. ,.raw= "REPORT /test HTTP/1.1\r\n"
  497. "\r\n"
  498. ,.should_keep_alive= TRUE
  499. ,.message_complete_on_eof= FALSE
  500. ,.http_major= 1
  501. ,.http_minor= 1
  502. ,.method= HTTP_REPORT
  503. ,.query_string= ""
  504. ,.fragment= ""
  505. ,.request_path= "/test"
  506. ,.request_url= "/test"
  507. ,.num_headers= 0
  508. ,.headers= {}
  509. ,.body= ""
  510. }
  511. #define NO_HTTP_VERSION 19
  512. , {.name= "request with no http version"
  513. ,.type= HTTP_REQUEST
  514. ,.raw= "GET /\r\n"
  515. "\r\n"
  516. ,.should_keep_alive= FALSE
  517. ,.message_complete_on_eof= FALSE
  518. ,.http_major= 0
  519. ,.http_minor= 9
  520. ,.method= HTTP_GET
  521. ,.query_string= ""
  522. ,.fragment= ""
  523. ,.request_path= "/"
  524. ,.request_url= "/"
  525. ,.num_headers= 0
  526. ,.headers= {}
  527. ,.body= ""
  528. }
  529. #define MSEARCH_REQ 20
  530. , {.name= "m-search request"
  531. ,.type= HTTP_REQUEST
  532. ,.raw= "M-SEARCH * HTTP/1.1\r\n"
  533. "HOST: 239.255.255.250:1900\r\n"
  534. "MAN: \"ssdp:discover\"\r\n"
  535. "ST: \"ssdp:all\"\r\n"
  536. "\r\n"
  537. ,.should_keep_alive= TRUE
  538. ,.message_complete_on_eof= FALSE
  539. ,.http_major= 1
  540. ,.http_minor= 1
  541. ,.method= HTTP_MSEARCH
  542. ,.query_string= ""
  543. ,.fragment= ""
  544. ,.request_path= "*"
  545. ,.request_url= "*"
  546. ,.num_headers= 3
  547. ,.headers= { { "HOST", "239.255.255.250:1900" }
  548. , { "MAN", "\"ssdp:discover\"" }
  549. , { "ST", "\"ssdp:all\"" }
  550. }
  551. ,.body= ""
  552. }
  553. #define UTF8_PATH_REQ 21
  554. , {.name= "utf-8 path request"
  555. ,.type= HTTP_REQUEST
  556. ,.raw= "GET /δ¶/δt/pope?q=1#narf HTTP/1.1\r\n"
  557. "Host: github.com\r\n"
  558. "\r\n"
  559. ,.should_keep_alive= TRUE
  560. ,.message_complete_on_eof= FALSE
  561. ,.http_major= 1
  562. ,.http_minor= 1
  563. ,.method= HTTP_GET
  564. ,.query_string= "q=1"
  565. ,.fragment= "narf"
  566. ,.request_path= "/δ¶/δt/pope"
  567. ,.request_url= "/δ¶/δt/pope?q=1#narf"
  568. ,.num_headers= 1
  569. ,.headers= { {"Host", "github.com" }
  570. }
  571. ,.body= ""
  572. }
  573. #define QUERY_TERMINATED_HOST 22
  574. , {.name= "host terminated by a query string"
  575. ,.type= HTTP_REQUEST
  576. ,.raw= "GET http://hypnotoad.org?hail=all HTTP/1.1\r\n"
  577. "\r\n"
  578. ,.should_keep_alive= TRUE
  579. ,.message_complete_on_eof= FALSE
  580. ,.http_major= 1
  581. ,.http_minor= 1
  582. ,.method= HTTP_GET
  583. ,.query_string= "hail=all"
  584. ,.fragment= ""
  585. ,.request_path= ""
  586. ,.request_url= "http://hypnotoad.org?hail=all"
  587. ,.num_headers= 0
  588. ,.headers= { }
  589. ,.body= ""
  590. }
  591. #define QUERY_TERMINATED_HOSTPORT 23
  592. , {.name= "host:port terminated by a query string"
  593. ,.type= HTTP_REQUEST
  594. ,.raw= "GET http://hypnotoad.org:1234?hail=all HTTP/1.1\r\n"
  595. "\r\n"
  596. ,.should_keep_alive= TRUE
  597. ,.message_complete_on_eof= FALSE
  598. ,.http_major= 1
  599. ,.http_minor= 1
  600. ,.method= HTTP_GET
  601. ,.query_string= "hail=all"
  602. ,.fragment= ""
  603. ,.request_path= ""
  604. ,.request_url= "http://hypnotoad.org:1234?hail=all"
  605. ,.num_headers= 0
  606. ,.headers= { }
  607. ,.body= ""
  608. }
  609. #define SPACE_TERMINATED_HOSTPORT 24
  610. , {.name= "host:port terminated by a space"
  611. ,.type= HTTP_REQUEST
  612. ,.raw= "GET http://hypnotoad.org:1234 HTTP/1.1\r\n"
  613. "\r\n"
  614. ,.should_keep_alive= TRUE
  615. ,.message_complete_on_eof= FALSE
  616. ,.http_major= 1
  617. ,.http_minor= 1
  618. ,.method= HTTP_GET
  619. ,.query_string= ""
  620. ,.fragment= ""
  621. ,.request_path= ""
  622. ,.request_url= "http://hypnotoad.org:1234"
  623. ,.num_headers= 0
  624. ,.headers= { }
  625. ,.body= ""
  626. }
  627. , {.name= NULL } /* sentinel */
  628. };
  629. /* * R E S P O N S E S * */
  630. const struct message responses[] =
  631. #define GOOGLE_301 0
  632. { {.name= "google 301"
  633. ,.type= HTTP_RESPONSE
  634. ,.raw= "HTTP/1.1 301 Moved Permanently\r\n"
  635. "Location: http://www.google.com/\r\n"
  636. "Content-Type: text/html; charset=UTF-8\r\n"
  637. "Date: Sun, 26 Apr 2009 11:11:49 GMT\r\n"
  638. "Expires: Tue, 26 May 2009 11:11:49 GMT\r\n"
  639. "X-$PrototypeBI-Version: 1.6.0.3\r\n" /* $ char in header field */
  640. "Cache-Control: public, max-age=2592000\r\n"
  641. "Server: gws\r\n"
  642. "Content-Length: 219 \r\n"
  643. "\r\n"
  644. "<HTML><HEAD><meta http-equiv=\"content-type\" content=\"text/html;charset=utf-8\">\n"
  645. "<TITLE>301 Moved</TITLE></HEAD><BODY>\n"
  646. "<H1>301 Moved</H1>\n"
  647. "The document has moved\n"
  648. "<A HREF=\"http://www.google.com/\">here</A>.\r\n"
  649. "</BODY></HTML>\r\n"
  650. ,.should_keep_alive= TRUE
  651. ,.message_complete_on_eof= FALSE
  652. ,.http_major= 1
  653. ,.http_minor= 1
  654. ,.status_code= 301
  655. ,.num_headers= 8
  656. ,.headers=
  657. { { "Location", "http://www.google.com/" }
  658. , { "Content-Type", "text/html; charset=UTF-8" }
  659. , { "Date", "Sun, 26 Apr 2009 11:11:49 GMT" }
  660. , { "Expires", "Tue, 26 May 2009 11:11:49 GMT" }
  661. , { "X-$PrototypeBI-Version", "1.6.0.3" }
  662. , { "Cache-Control", "public, max-age=2592000" }
  663. , { "Server", "gws" }
  664. , { "Content-Length", "219 " }
  665. }
  666. ,.body= "<HTML><HEAD><meta http-equiv=\"content-type\" content=\"text/html;charset=utf-8\">\n"
  667. "<TITLE>301 Moved</TITLE></HEAD><BODY>\n"
  668. "<H1>301 Moved</H1>\n"
  669. "The document has moved\n"
  670. "<A HREF=\"http://www.google.com/\">here</A>.\r\n"
  671. "</BODY></HTML>\r\n"
  672. }
  673. #define NO_CONTENT_LENGTH_RESPONSE 1
  674. /* The client should wait for the server's EOF. That is, when content-length
  675. * is not specified, and "Connection: close", the end of body is specified
  676. * by the EOF.
  677. * Compare with APACHEBENCH_GET
  678. */
  679. , {.name= "no content-length response"
  680. ,.type= HTTP_RESPONSE
  681. ,.raw= "HTTP/1.1 200 OK\r\n"
  682. "Date: Tue, 04 Aug 2009 07:59:32 GMT\r\n"
  683. "Server: Apache\r\n"
  684. "X-Powered-By: Servlet/2.5 JSP/2.1\r\n"
  685. "Content-Type: text/xml; charset=utf-8\r\n"
  686. "Connection: close\r\n"
  687. "\r\n"
  688. "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
  689. "<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\">\n"
  690. " <SOAP-ENV:Body>\n"
  691. " <SOAP-ENV:Fault>\n"
  692. " <faultcode>SOAP-ENV:Client</faultcode>\n"
  693. " <faultstring>Client Error</faultstring>\n"
  694. " </SOAP-ENV:Fault>\n"
  695. " </SOAP-ENV:Body>\n"
  696. "</SOAP-ENV:Envelope>"
  697. ,.should_keep_alive= FALSE
  698. ,.message_complete_on_eof= TRUE
  699. ,.http_major= 1
  700. ,.http_minor= 1
  701. ,.status_code= 200
  702. ,.num_headers= 5
  703. ,.headers=
  704. { { "Date", "Tue, 04 Aug 2009 07:59:32 GMT" }
  705. , { "Server", "Apache" }
  706. , { "X-Powered-By", "Servlet/2.5 JSP/2.1" }
  707. , { "Content-Type", "text/xml; charset=utf-8" }
  708. , { "Connection", "close" }
  709. }
  710. ,.body= "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
  711. "<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\">\n"
  712. " <SOAP-ENV:Body>\n"
  713. " <SOAP-ENV:Fault>\n"
  714. " <faultcode>SOAP-ENV:Client</faultcode>\n"
  715. " <faultstring>Client Error</faultstring>\n"
  716. " </SOAP-ENV:Fault>\n"
  717. " </SOAP-ENV:Body>\n"
  718. "</SOAP-ENV:Envelope>"
  719. }
  720. #define NO_HEADERS_NO_BODY_404 2
  721. , {.name= "404 no headers no body"
  722. ,.type= HTTP_RESPONSE
  723. ,.raw= "HTTP/1.1 404 Not Found\r\n\r\n"
  724. ,.should_keep_alive= TRUE
  725. ,.message_complete_on_eof= FALSE
  726. ,.http_major= 1
  727. ,.http_minor= 1
  728. ,.status_code= 404
  729. ,.num_headers= 0
  730. ,.headers= {}
  731. ,.body_size= 0
  732. ,.body= ""
  733. }
  734. #define NO_REASON_PHRASE 3
  735. , {.name= "301 no response phrase"
  736. ,.type= HTTP_RESPONSE
  737. ,.raw= "HTTP/1.1 301\r\n\r\n"
  738. ,.should_keep_alive = TRUE
  739. ,.message_complete_on_eof= FALSE
  740. ,.http_major= 1
  741. ,.http_minor= 1
  742. ,.status_code= 301
  743. ,.num_headers= 0
  744. ,.headers= {}
  745. ,.body= ""
  746. }
  747. #define TRAILING_SPACE_ON_CHUNKED_BODY 4
  748. , {.name="200 trailing space on chunked body"
  749. ,.type= HTTP_RESPONSE
  750. ,.raw= "HTTP/1.1 200 OK\r\n"
  751. "Content-Type: text/plain\r\n"
  752. "Transfer-Encoding: chunked\r\n"
  753. "\r\n"
  754. "25 \r\n"
  755. "This is the data in the first chunk\r\n"
  756. "\r\n"
  757. "1C\r\n"
  758. "and this is the second one\r\n"
  759. "\r\n"
  760. "0 \r\n"
  761. "\r\n"
  762. ,.should_keep_alive= TRUE
  763. ,.message_complete_on_eof= FALSE
  764. ,.http_major= 1
  765. ,.http_minor= 1
  766. ,.status_code= 200
  767. ,.num_headers= 2
  768. ,.headers=
  769. { {"Content-Type", "text/plain" }
  770. , {"Transfer-Encoding", "chunked" }
  771. }
  772. ,.body_size = 37+28
  773. ,.body =
  774. "This is the data in the first chunk\r\n"
  775. "and this is the second one\r\n"
  776. }
  777. #define NO_CARRIAGE_RET 5
  778. , {.name="no carriage ret"
  779. ,.type= HTTP_RESPONSE
  780. ,.raw= "HTTP/1.1 200 OK\n"
  781. "Content-Type: text/html; charset=utf-8\n"
  782. "Connection: close\n"
  783. "\n"
  784. "these headers are from http://news.ycombinator.com/"
  785. ,.should_keep_alive= FALSE
  786. ,.message_complete_on_eof= TRUE
  787. ,.http_major= 1
  788. ,.http_minor= 1
  789. ,.status_code= 200
  790. ,.num_headers= 2
  791. ,.headers=
  792. { {"Content-Type", "text/html; charset=utf-8" }
  793. , {"Connection", "close" }
  794. }
  795. ,.body= "these headers are from http://news.ycombinator.com/"
  796. }
  797. #define PROXY_CONNECTION 6
  798. , {.name="proxy connection"
  799. ,.type= HTTP_RESPONSE
  800. ,.raw= "HTTP/1.1 200 OK\r\n"
  801. "Content-Type: text/html; charset=UTF-8\r\n"
  802. "Content-Length: 11\r\n"
  803. "Proxy-Connection: close\r\n"
  804. "Date: Thu, 31 Dec 2009 20:55:48 +0000\r\n"
  805. "\r\n"
  806. "hello world"
  807. ,.should_keep_alive= FALSE
  808. ,.message_complete_on_eof= FALSE
  809. ,.http_major= 1
  810. ,.http_minor= 1
  811. ,.status_code= 200
  812. ,.num_headers= 4
  813. ,.headers=
  814. { {"Content-Type", "text/html; charset=UTF-8" }
  815. , {"Content-Length", "11" }
  816. , {"Proxy-Connection", "close" }
  817. , {"Date", "Thu, 31 Dec 2009 20:55:48 +0000"}
  818. }
  819. ,.body= "hello world"
  820. }
  821. #define UNDERSTORE_HEADER_KEY 7
  822. // shown by
  823. // curl -o /dev/null -v "http://ad.doubleclick.net/pfadx/DARTSHELLCONFIGXML;dcmt=text/xml;"
  824. , {.name="underscore header key"
  825. ,.type= HTTP_RESPONSE
  826. ,.raw= "HTTP/1.1 200 OK\r\n"
  827. "Server: DCLK-AdSvr\r\n"
  828. "Content-Type: text/xml\r\n"
  829. "Content-Length: 0\r\n"
  830. "DCLK_imp: v7;x;114750856;0-0;0;17820020;0/0;21603567/21621457/1;;~okv=;dcmt=text/xml;;~cs=o\r\n\r\n"
  831. ,.should_keep_alive= TRUE
  832. ,.message_complete_on_eof= FALSE
  833. ,.http_major= 1
  834. ,.http_minor= 1
  835. ,.status_code= 200
  836. ,.num_headers= 4
  837. ,.headers=
  838. { {"Server", "DCLK-AdSvr" }
  839. , {"Content-Type", "text/xml" }
  840. , {"Content-Length", "0" }
  841. , {"DCLK_imp", "v7;x;114750856;0-0;0;17820020;0/0;21603567/21621457/1;;~okv=;dcmt=text/xml;;~cs=o" }
  842. }
  843. ,.body= ""
  844. }
  845. #define BONJOUR_MADAME_FR 8
  846. /* The client should not merge two headers fields when the first one doesn't
  847. * have a value.
  848. */
  849. , {.name= "bonjourmadame.fr"
  850. ,.type= HTTP_RESPONSE
  851. ,.raw= "HTTP/1.0 301 Moved Permanently\r\n"
  852. "Date: Thu, 03 Jun 2010 09:56:32 GMT\r\n"
  853. "Server: Apache/2.2.3 (Red Hat)\r\n"
  854. "Cache-Control: public\r\n"
  855. "Pragma: \r\n"
  856. "Location: http://www.bonjourmadame.fr/\r\n"
  857. "Vary: Accept-Encoding\r\n"
  858. "Content-Length: 0\r\n"
  859. "Content-Type: text/html; charset=UTF-8\r\n"
  860. "Connection: keep-alive\r\n"
  861. "\r\n"
  862. ,.should_keep_alive= TRUE
  863. ,.message_complete_on_eof= FALSE
  864. ,.http_major= 1
  865. ,.http_minor= 0
  866. ,.status_code= 301
  867. ,.num_headers= 9
  868. ,.headers=
  869. { { "Date", "Thu, 03 Jun 2010 09:56:32 GMT" }
  870. , { "Server", "Apache/2.2.3 (Red Hat)" }
  871. , { "Cache-Control", "public" }
  872. , { "Pragma", "" }
  873. , { "Location", "http://www.bonjourmadame.fr/" }
  874. , { "Vary", "Accept-Encoding" }
  875. , { "Content-Length", "0" }
  876. , { "Content-Type", "text/html; charset=UTF-8" }
  877. , { "Connection", "keep-alive" }
  878. }
  879. ,.body= ""
  880. }
  881. #define SPACE_IN_FIELD_RES 9
  882. /* Should handle spaces in header fields */
  883. , {.name= "field space"
  884. ,.type= HTTP_RESPONSE
  885. ,.raw= "HTTP/1.1 200 OK\r\n"
  886. "Server: Microsoft-IIS/6.0\r\n"
  887. "X-Powered-By: ASP.NET\r\n"
  888. "en-US Content-Type: text/xml\r\n" /* this is the problem */
  889. "Content-Type: text/xml\r\n"
  890. "Content-Length: 16\r\n"
  891. "Date: Fri, 23 Jul 2010 18:45:38 GMT\r\n"
  892. "Connection: keep-alive\r\n"
  893. "\r\n"
  894. "<xml>hello</xml>" /* fake body */
  895. ,.should_keep_alive= TRUE
  896. ,.message_complete_on_eof= FALSE
  897. ,.http_major= 1
  898. ,.http_minor= 1
  899. ,.status_code= 200
  900. ,.num_headers= 7
  901. ,.headers=
  902. { { "Server", "Microsoft-IIS/6.0" }
  903. , { "X-Powered-By", "ASP.NET" }
  904. , { "en-US Content-Type", "text/xml" }
  905. , { "Content-Type", "text/xml" }
  906. , { "Content-Length", "16" }
  907. , { "Date", "Fri, 23 Jul 2010 18:45:38 GMT" }
  908. , { "Connection", "keep-alive" }
  909. }
  910. ,.body= "<xml>hello</xml>"
  911. }
  912. #define RES_FIELD_UNDERSCORE 10
  913. /* Should handle spaces in header fields */
  914. , {.name= "field underscore"
  915. ,.type= HTTP_RESPONSE
  916. ,.raw= "HTTP/1.1 200 OK\r\n"
  917. "Date: Tue, 28 Sep 2010 01:14:13 GMT\r\n"
  918. "Server: Apache\r\n"
  919. "Cache-Control: no-cache, must-revalidate\r\n"
  920. "Expires: Mon, 26 Jul 1997 05:00:00 GMT\r\n"
  921. ".et-Cookie: PlaxoCS=1274804622353690521; path=/; domain=.plaxo.com\r\n"
  922. "Vary: Accept-Encoding\r\n"
  923. "_eep-Alive: timeout=45\r\n" /* semantic value ignored */
  924. "_onnection: Keep-Alive\r\n" /* semantic value ignored */
  925. "Transfer-Encoding: chunked\r\n"
  926. "Content-Type: text/html\r\n"
  927. "Connection: close\r\n"
  928. "\r\n"
  929. "0\r\n\r\n"
  930. ,.should_keep_alive= FALSE
  931. ,.message_complete_on_eof= FALSE
  932. ,.http_major= 1
  933. ,.http_minor= 1
  934. ,.status_code= 200
  935. ,.num_headers= 11
  936. ,.headers=
  937. { { "Date", "Tue, 28 Sep 2010 01:14:13 GMT" }
  938. , { "Server", "Apache" }
  939. , { "Cache-Control", "no-cache, must-revalidate" }
  940. , { "Expires", "Mon, 26 Jul 1997 05:00:00 GMT" }
  941. , { ".et-Cookie", "PlaxoCS=1274804622353690521; path=/; domain=.plaxo.com" }
  942. , { "Vary", "Accept-Encoding" }
  943. , { "_eep-Alive", "timeout=45" }
  944. , { "_onnection", "Keep-Alive" }
  945. , { "Transfer-Encoding", "chunked" }
  946. , { "Content-Type", "text/html" }
  947. , { "Connection", "close" }
  948. }
  949. ,.body= ""
  950. }
  951. #define NON_ASCII_IN_STATUS_LINE 11
  952. /* Should handle non-ASCII in status line */
  953. , {.name= "non-ASCII in status line"
  954. ,.type= HTTP_RESPONSE
  955. ,.raw= "HTTP/1.1 500 Oriëntatieprobleem\r\n"
  956. "Date: Fri, 5 Nov 2010 23:07:12 GMT+2\r\n"
  957. "Content-Length: 0\r\n"
  958. "Connection: close\r\n"
  959. "\r\n"
  960. ,.should_keep_alive= FALSE
  961. ,.message_complete_on_eof= FALSE
  962. ,.http_major= 1
  963. ,.http_minor= 1
  964. ,.status_code= 500
  965. ,.num_headers= 3
  966. ,.headers=
  967. { { "Date", "Fri, 5 Nov 2010 23:07:12 GMT+2" }
  968. , { "Content-Length", "0" }
  969. , { "Connection", "close" }
  970. }
  971. ,.body= ""
  972. }
  973. , {.name= NULL } /* sentinel */
  974. };
  975. int
  976. request_path_cb (http_parser *p, const char *buf, size_t len)
  977. {
  978. assert(p == parser);
  979. strncat(messages[num_messages].request_path, buf, len);
  980. return 0;
  981. }
  982. int
  983. request_url_cb (http_parser *p, const char *buf, size_t len)
  984. {
  985. assert(p == parser);
  986. strncat(messages[num_messages].request_url, buf, len);
  987. return 0;
  988. }
  989. int
  990. query_string_cb (http_parser *p, const char *buf, size_t len)
  991. {
  992. assert(p == parser);
  993. strncat(messages[num_messages].query_string, buf, len);
  994. return 0;
  995. }
  996. int
  997. fragment_cb (http_parser *p, const char *buf, size_t len)
  998. {
  999. assert(p == parser);
  1000. strncat(messages[num_messages].fragment, buf, len);
  1001. return 0;
  1002. }
  1003. int
  1004. header_field_cb (http_parser *p, const char *buf, size_t len)
  1005. {
  1006. assert(p == parser);
  1007. struct message *m = &messages[num_messages];
  1008. if (m->last_header_element != FIELD)
  1009. m->num_headers++;
  1010. strncat(m->headers[m->num_headers-1][0], buf, len);
  1011. m->last_header_element = FIELD;
  1012. return 0;
  1013. }
  1014. int
  1015. header_value_cb (http_parser *p, const char *buf, size_t len)
  1016. {
  1017. assert(p == parser);
  1018. struct message *m = &messages[num_messages];
  1019. strncat(m->headers[m->num_headers-1][1], buf, len);
  1020. m->last_header_element = VALUE;
  1021. return 0;
  1022. }
  1023. int
  1024. body_cb (http_parser *p, const char *buf, size_t len)
  1025. {
  1026. assert(p == parser);
  1027. strncat(messages[num_messages].body, buf, len);
  1028. messages[num_messages].body_size += len;
  1029. // printf("body_cb: '%s'\n", requests[num_messages].body);
  1030. return 0;
  1031. }
  1032. int
  1033. count_body_cb (http_parser *p, const char *buf, size_t len)
  1034. {
  1035. assert(p == parser);
  1036. assert(buf);
  1037. messages[num_messages].body_size += len;
  1038. return 0;
  1039. }
  1040. int
  1041. message_begin_cb (http_parser *p)
  1042. {
  1043. assert(p == parser);
  1044. messages[num_messages].message_begin_cb_called = TRUE;
  1045. return 0;
  1046. }
  1047. int
  1048. headers_complete_cb (http_parser *p)
  1049. {
  1050. assert(p == parser);
  1051. messages[num_messages].method = parser->method;
  1052. messages[num_messages].status_code = parser->status_code;
  1053. messages[num_messages].http_major = parser->http_major;
  1054. messages[num_messages].http_minor = parser->http_minor;
  1055. messages[num_messages].headers_complete_cb_called = TRUE;
  1056. messages[num_messages].should_keep_alive = http_should_keep_alive(parser);
  1057. return 0;
  1058. }
  1059. int
  1060. message_complete_cb (http_parser *p)
  1061. {
  1062. assert(p == parser);
  1063. if (messages[num_messages].should_keep_alive != http_should_keep_alive(parser))
  1064. {
  1065. fprintf(stderr, "\n\n *** Error http_should_keep_alive() should have same "
  1066. "value in both on_message_complete and on_headers_complete "
  1067. "but it doesn't! ***\n\n");
  1068. assert(0);
  1069. exit(1);
  1070. }
  1071. messages[num_messages].message_complete_cb_called = TRUE;
  1072. messages[num_messages].message_complete_on_eof = currently_parsing_eof;
  1073. num_messages++;
  1074. return 0;
  1075. }
  1076. static http_parser_settings settings =
  1077. {.on_message_begin = message_begin_cb
  1078. ,.on_header_field = header_field_cb
  1079. ,.on_header_value = header_value_cb
  1080. ,.on_path = request_path_cb
  1081. ,.on_url = request_url_cb
  1082. ,.on_fragment = fragment_cb
  1083. ,.on_query_string = query_string_cb
  1084. ,.on_body = body_cb
  1085. ,.on_headers_complete = headers_complete_cb
  1086. ,.on_message_complete = message_complete_cb
  1087. };
  1088. static http_parser_settings settings_count_body =
  1089. {.on_message_begin = message_begin_cb
  1090. ,.on_header_field = header_field_cb
  1091. ,.on_header_value = header_value_cb
  1092. ,.on_path = request_path_cb
  1093. ,.on_url = request_url_cb
  1094. ,.on_fragment = fragment_cb
  1095. ,.on_query_string = query_string_cb
  1096. ,.on_body = count_body_cb
  1097. ,.on_headers_complete = headers_complete_cb
  1098. ,.on_message_complete = message_complete_cb
  1099. };
  1100. static http_parser_settings settings_null =
  1101. {.on_message_begin = 0
  1102. ,.on_header_field = 0
  1103. ,.on_header_value = 0
  1104. ,.on_path = 0
  1105. ,.on_url = 0
  1106. ,.on_fragment = 0
  1107. ,.on_query_string = 0
  1108. ,.on_body = 0
  1109. ,.on_headers_complete = 0
  1110. ,.on_message_complete = 0
  1111. };
  1112. void
  1113. parser_init (enum http_parser_type type)
  1114. {
  1115. num_messages = 0;
  1116. assert(parser == NULL);
  1117. parser = malloc(sizeof(http_parser));
  1118. http_parser_init(parser, type);
  1119. memset(&messages, 0, sizeof messages);
  1120. }
  1121. void
  1122. parser_free ()
  1123. {
  1124. assert(parser);
  1125. free(parser);
  1126. parser = NULL;
  1127. }
  1128. size_t parse (const char *buf, size_t len)
  1129. {
  1130. size_t nparsed;
  1131. currently_parsing_eof = (len == 0);
  1132. nparsed = http_parser_execute(parser, &settings, buf, len);
  1133. return nparsed;
  1134. }
  1135. size_t parse_count_body (const char *buf, size_t len)
  1136. {
  1137. size_t nparsed;
  1138. currently_parsing_eof = (len == 0);
  1139. nparsed = http_parser_execute(parser, &settings_count_body, buf, len);
  1140. return nparsed;
  1141. }
  1142. static inline int
  1143. check_str_eq (const struct message *m,
  1144. const char *prop,
  1145. const char *expected,
  1146. const char *found) {
  1147. if (0 != strcmp(expected, found)) {
  1148. printf("\n*** Error: %s in '%s' ***\n\n", prop, m->name);
  1149. printf("expected '%s'\n", expected);
  1150. printf(" found '%s'\n", found);
  1151. return 0;
  1152. }
  1153. return 1;
  1154. }
  1155. static inline int
  1156. check_num_eq (const struct message *m,
  1157. const char *prop,
  1158. int expected,
  1159. int found) {
  1160. if (expected != found) {
  1161. printf("\n*** Error: %s in '%s' ***\n\n", prop, m->name);
  1162. printf("expected %d\n", expected);
  1163. printf(" found %d\n", found);
  1164. return 0;
  1165. }
  1166. return 1;
  1167. }
  1168. #define MESSAGE_CHECK_STR_EQ(expected, found, prop) \
  1169. if (!check_str_eq(expected, #prop, expected->prop, found->prop)) return 0
  1170. #define MESSAGE_CHECK_NUM_EQ(expected, found, prop) \
  1171. if (!check_num_eq(expected, #prop, expected->prop, found->prop)) return 0
  1172. int
  1173. message_eq (int index, const struct message *expected)
  1174. {
  1175. int i;
  1176. struct message *m = &messages[index];
  1177. MESSAGE_CHECK_NUM_EQ(expected, m, http_major);
  1178. MESSAGE_CHECK_NUM_EQ(expected, m, http_minor);
  1179. if (expected->type == HTTP_REQUEST) {
  1180. MESSAGE_CHECK_NUM_EQ(expected, m, method);
  1181. } else {
  1182. MESSAGE_CHECK_NUM_EQ(expected, m, status_code);
  1183. }
  1184. MESSAGE_CHECK_NUM_EQ(expected, m, should_keep_alive);
  1185. MESSAGE_CHECK_NUM_EQ(expected, m, message_complete_on_eof);
  1186. assert(m->message_begin_cb_called);
  1187. assert(m->headers_complete_cb_called);
  1188. assert(m->message_complete_cb_called);
  1189. MESSAGE_CHECK_STR_EQ(expected, m, request_path);
  1190. MESSAGE_CHECK_STR_EQ(expected, m, query_string);
  1191. MESSAGE_CHECK_STR_EQ(expected, m, fragment);
  1192. MESSAGE_CHECK_STR_EQ(expected, m, request_url);
  1193. if (expected->body_size) {
  1194. MESSAGE_CHECK_NUM_EQ(expected, m, body_size);
  1195. } else {
  1196. MESSAGE_CHECK_STR_EQ(expected, m, body);
  1197. }
  1198. MESSAGE_CHECK_NUM_EQ(expected, m, num_headers);
  1199. int r;
  1200. for (i = 0; i < m->num_headers; i++) {
  1201. r = check_str_eq(expected, "header field", expected->headers[i][0], m->headers[i][0]);
  1202. if (!r) return 0;
  1203. r = check_str_eq(expected, "header value", expected->headers[i][1], m->headers[i][1]);
  1204. if (!r) return 0;
  1205. }
  1206. return 1;
  1207. }
  1208. static void
  1209. print_error (const char *raw, size_t error_location)
  1210. {
  1211. fprintf(stderr, "\n*** parse error ***\n\n");
  1212. int this_line = 0, char_len = 0;
  1213. size_t i, j, len = strlen(raw), error_location_line = 0;
  1214. for (i = 0; i < len; i++) {
  1215. if (i == error_location) this_line = 1;
  1216. switch (raw[i]) {
  1217. case '\r':
  1218. char_len = 2;
  1219. fprintf(stderr, "\\r");
  1220. break;
  1221. case '\n':
  1222. char_len = 2;
  1223. fprintf(stderr, "\\n\n");
  1224. if (this_line) goto print;
  1225. error_location_line = 0;
  1226. continue;
  1227. default:
  1228. char_len = 1;
  1229. fputc(raw[i], stderr);
  1230. break;
  1231. }
  1232. if (!this_line) error_location_line += char_len;
  1233. }
  1234. fprintf(stderr, "[eof]\n");
  1235. print:
  1236. for (j = 0; j < error_location_line; j++) {
  1237. fputc(' ', stderr);
  1238. }
  1239. fprintf(stderr, "^\n\nerror location: %u\n", (unsigned int)error_location);
  1240. }
  1241. void
  1242. test_message (const struct message *message)
  1243. {
  1244. size_t raw_len = strlen(message->raw);
  1245. size_t msg1len;
  1246. for (msg1len = 0; msg1len < raw_len; msg1len++) {
  1247. parser_init(message->type);
  1248. size_t read;
  1249. const char *msg1 = message->raw;
  1250. const char *msg2 = msg1 + msg1len;
  1251. size_t msg2len = raw_len - msg1len;
  1252. if (msg1len) {
  1253. read = parse(msg1, msg1len);
  1254. if (message->upgrade && parser->upgrade) goto test;
  1255. if (read != msg1len) {
  1256. print_error(msg1, read);
  1257. exit(1);
  1258. }
  1259. }
  1260. read = parse(msg2, msg2len);
  1261. if (message->upgrade && parser->upgrade) goto test;
  1262. if (read != msg2len) {
  1263. print_error(msg2, read);
  1264. exit(1);
  1265. }
  1266. read = parse(NULL, 0);
  1267. if (message->upgrade && parser->upgrade) goto test;
  1268. if (read != 0) {
  1269. print_error(message->raw, read);
  1270. exit(1);
  1271. }
  1272. test:
  1273. if (num_messages != 1) {
  1274. printf("\n*** num_messages != 1 after testing '%s' ***\n\n", message->name);
  1275. exit(1);
  1276. }
  1277. if(!message_eq(0, message)) exit(1);
  1278. parser_free();
  1279. }
  1280. }
  1281. void
  1282. test_message_count_body (const struct message *message)
  1283. {
  1284. parser_init(message->type);
  1285. size_t read;
  1286. size_t l = strlen(message->raw);
  1287. size_t i, toread;
  1288. size_t chunk = 4024;
  1289. for (i = 0; i < l; i+= chunk) {
  1290. toread = MIN(l-i, chunk);
  1291. read = parse_count_body(message->raw + i, toread);
  1292. if (read != toread) {
  1293. print_error(message->raw, read);
  1294. exit(1);
  1295. }
  1296. }
  1297. read = parse_count_body(NULL, 0);
  1298. if (read != 0) {
  1299. print_error(message->raw, read);
  1300. exit(1);
  1301. }
  1302. if (num_messages != 1) {
  1303. printf("\n*** num_messages != 1 after testing '%s' ***\n\n", message->name);
  1304. exit(1);
  1305. }
  1306. if(!message_eq(0, message)) exit(1);
  1307. parser_free();
  1308. }
  1309. void
  1310. test_simple (const char *buf, int should_pass)
  1311. {
  1312. parser_init(HTTP_REQUEST);
  1313. size_t parsed;
  1314. int pass;
  1315. parsed = parse(buf, strlen(buf));
  1316. pass = (parsed == strlen(buf));
  1317. parsed = parse(NULL, 0);
  1318. pass &= (parsed == 0);
  1319. parser_free();
  1320. if (pass != should_pass) {
  1321. fprintf(stderr, "\n*** test_simple expected %s ***\n\n%s", should_pass ? "success" : "error", buf);
  1322. exit(1);
  1323. }
  1324. }
  1325. void
  1326. test_header_overflow_error (int req)
  1327. {
  1328. http_parser parser;
  1329. http_parser_init(&parser, req ? HTTP_REQUEST : HTTP_RESPONSE);
  1330. size_t parsed;
  1331. const char *buf;
  1332. buf = req ? "GET / HTTP/1.1\r\n" : "HTTP/1.0 200 OK\r\n";
  1333. parsed = http_parser_execute(&parser, &settings_null, buf, strlen(buf));
  1334. assert(parsed == strlen(buf));
  1335. buf = "header-key: header-value\r\n";
  1336. int i;
  1337. for (i = 0; i < 10000; i++) {
  1338. if (http_parser_execute(&parser, &settings_null, buf, strlen(buf)) != strlen(buf)) {
  1339. //fprintf(stderr, "error found on iter %d\n", i);
  1340. return;
  1341. }
  1342. }
  1343. fprintf(stderr, "\n*** Error expected but none in header overflow test ***\n");
  1344. exit(1);
  1345. }
  1346. void
  1347. test_no_overflow_long_body (int req, size_t length)
  1348. {
  1349. http_parser parser;
  1350. http_parser_init(&parser, req ? HTTP_REQUEST : HTTP_RESPONSE);
  1351. size_t parsed;
  1352. size_t i;
  1353. char buf1[3000];
  1354. size_t buf1len = sprintf(buf1, "%s\r\nConnection: Keep-Alive\r\nContent-Length: %zu\r\n\r\n",
  1355. req ? "POST / HTTP/1.0" : "HTTP/1.0 200 OK", length);
  1356. parsed = http_parser_execute(&parser, &settings_null, buf1, buf1len);
  1357. if (parsed != buf1len)
  1358. goto err;
  1359. for (i = 0; i < length; i++) {
  1360. char foo = 'a';
  1361. parsed = http_parser_execute(&parser, &settings_null, &foo, 1);
  1362. if (parsed != 1)
  1363. goto err;
  1364. }
  1365. parsed = http_parser_execute(&parser, &settings_null, buf1, buf1len);
  1366. if (parsed != buf1len) goto err;
  1367. return;
  1368. err:
  1369. fprintf(stderr,
  1370. "\n*** error in test_no_overflow_long_body %s of length %zu ***\n",
  1371. req ? "REQUEST" : "RESPONSE",
  1372. length);
  1373. exit(1);
  1374. }
  1375. void
  1376. test_multiple3 (const struct message *r1, const struct message *r2, const struct message *r3)
  1377. {
  1378. int message_count = 1;
  1379. if (!r1->upgrade) {
  1380. message_count++;
  1381. if (!r2->upgrade) message_count++;
  1382. }
  1383. int has_upgrade = (message_count < 3 || r3->upgrade);
  1384. char total[ strlen(r1->raw)
  1385. + strlen(r2->raw)
  1386. + strlen(r3->raw)
  1387. + 1
  1388. ];
  1389. total[0] = '\0';
  1390. strcat(total, r1->raw);
  1391. strcat(total, r2->raw);
  1392. strcat(total, r3->raw);
  1393. parser_init(r1->type);
  1394. size_t read;
  1395. read = parse(total, strlen(total));
  1396. if (has_upgrade && parser->upgrade) goto test;
  1397. if (read != strlen(total)) {
  1398. print_error(total, read);
  1399. exit(1);
  1400. }
  1401. read = parse(NULL, 0);
  1402. if (has_upgrade && parser->upgrade) goto test;
  1403. if (read != 0) {
  1404. print_error(total, read);
  1405. exit(1);
  1406. }
  1407. test:
  1408. if (message_count != num_messages) {
  1409. fprintf(stderr, "\n\n*** Parser didn't see 3 messages only %d *** \n", num_messages);
  1410. exit(1);
  1411. }
  1412. if (!message_eq(0, r1)) exit(1);
  1413. if (message_count > 1) {
  1414. if (!message_eq(1, r2)) exit(1);
  1415. if (message_count > 2) {
  1416. if (!message_eq(2, r3)) exit(1);
  1417. }
  1418. }
  1419. parser_free();
  1420. }
  1421. /* SCAN through every possible breaking to make sure the
  1422. * parser can handle getting the content in any chunks that
  1423. * might come from the socket
  1424. */
  1425. void
  1426. test_scan (const struct message *r1, const struct message *r2, const struct message *r3)
  1427. {
  1428. char total[80*1024] = "\0";
  1429. char buf1[80*1024] = "\0";
  1430. char buf2[80*1024] = "\0";
  1431. char buf3[80*1024] = "\0";
  1432. strcat(total, r1->raw);
  1433. strcat(total, r2->raw);
  1434. strcat(total, r3->raw);
  1435. size_t read;
  1436. int total_len = strlen(total);
  1437. int total_ops = 2 * (total_len - 1) * (total_len - 2) / 2;
  1438. int ops = 0 ;
  1439. size_t buf1_len, buf2_len, buf3_len;
  1440. int i,j,type_both;
  1441. for (type_both = 0; type_both < 2; type_both ++ ) {
  1442. for (j = 2; j < total_len; j ++ ) {
  1443. for (i = 1; i < j; i ++ ) {
  1444. if (ops % 1000 == 0) {
  1445. printf("\b\b\b\b%3.0f%%", 100 * (float)ops /(float)total_ops);
  1446. fflush(stdout);
  1447. }
  1448. ops += 1;
  1449. parser_init(type_both ? HTTP_BOTH : r1->type);
  1450. buf1_len = i;
  1451. strncpy(buf1, total, buf1_len);
  1452. buf1[buf1_len] = 0;
  1453. buf2_len = j - i;
  1454. strncpy(buf2, total+i, buf2_len);
  1455. buf2[buf2_len] = 0;
  1456. buf3_len = total_len - j;
  1457. strncpy(buf3, total+j, buf3_len);
  1458. buf3[buf3_len] = 0;
  1459. read = parse(buf1, buf1_len);
  1460. if (r3->upgrade && parser->upgrade) goto test;
  1461. if (read != buf1_len) {
  1462. print_error(buf1, read);
  1463. goto error;
  1464. }
  1465. read = parse(buf2, buf2_len);
  1466. if (r3->upgrade && parser->upgrade) goto test;
  1467. if (read != buf2_len) {
  1468. print_error(buf2, read);
  1469. goto error;
  1470. }
  1471. read = parse(buf3, buf3_len);
  1472. if (r3->upgrade && parser->upgrade) goto test;
  1473. if (read != buf3_len) {
  1474. print_error(buf3, read);
  1475. goto error;
  1476. }
  1477. parse(NULL, 0);
  1478. test:
  1479. if (3 != num_messages) {
  1480. fprintf(stderr, "\n\nParser didn't see 3 messages only %d\n", num_messages);
  1481. goto error;
  1482. }
  1483. if (!message_eq(0, r1)) {
  1484. fprintf(stderr, "\n\nError matching messages[0] in test_scan.\n");
  1485. goto error;
  1486. }
  1487. if (!message_eq(1, r2)) {
  1488. fprintf(stderr, "\n\nError matching messages[1] in test_scan.\n");
  1489. goto error;
  1490. }
  1491. if (!message_eq(2, r3)) {
  1492. fprintf(stderr, "\n\nError matching messages[2] in test_scan.\n");
  1493. goto error;
  1494. }
  1495. parser_free();
  1496. }
  1497. }
  1498. }
  1499. puts("\b\b\b\b100%");
  1500. return;
  1501. error:
  1502. fprintf(stderr, "i=%d j=%d\n", i, j);
  1503. fprintf(stderr, "buf1 (%u) %s\n\n", (unsigned int)buf1_len, buf1);
  1504. fprintf(stderr, "buf2 (%u) %s\n\n", (unsigned int)buf2_len , buf2);
  1505. fprintf(stderr, "buf3 (%u) %s\n", (unsigned int)buf3_len, buf3);
  1506. exit(1);
  1507. }
  1508. // user required to free the result
  1509. // string terminated by \0
  1510. char *
  1511. create_large_chunked_message (int body_size_in_kb, const char* headers)
  1512. {
  1513. int i;
  1514. size_t wrote = 0;
  1515. size_t headers_len = strlen(headers);
  1516. size_t bufsize = headers_len + (5+1024+2)*body_size_in_kb + 6;
  1517. char * buf = malloc(bufsize);
  1518. memcpy(buf, headers, headers_len);
  1519. wrote += headers_len;
  1520. for (i = 0; i < body_size_in_kb; i++) {
  1521. // write 1kb chunk into the body.
  1522. memcpy(buf + wrote, "400\r\n", 5);
  1523. wrote += 5;
  1524. memset(buf + wrote, 'C', 1024);
  1525. wrote += 1024;
  1526. strcpy(buf + wrote, "\r\n");
  1527. wrote += 2;
  1528. }
  1529. memcpy(buf + wrote, "0\r\n\r\n", 6);
  1530. wrote += 6;
  1531. assert(wrote == bufsize);
  1532. return buf;
  1533. }
  1534. int
  1535. main (void)
  1536. {
  1537. parser = NULL;
  1538. int i, j, k;
  1539. int request_count;
  1540. int response_count;
  1541. printf("sizeof(http_parser) = %u\n", (unsigned int)sizeof(http_parser));
  1542. for (request_count = 0; requests[request_count].name; request_count++);
  1543. for (response_count = 0; responses[response_count].name; response_count++);
  1544. //// OVERFLOW CONDITIONS
  1545. test_header_overflow_error(HTTP_REQUEST);
  1546. test_no_overflow_long_body(HTTP_REQUEST, 1000);
  1547. test_no_overflow_long_body(HTTP_REQUEST, 100000);
  1548. test_header_overflow_error(HTTP_RESPONSE);
  1549. test_no_overflow_long_body(HTTP_RESPONSE, 1000);
  1550. test_no_overflow_long_body(HTTP_RESPONSE, 100000);
  1551. //// RESPONSES
  1552. for (i = 0; i < response_count; i++) {
  1553. test_message(&responses[i]);
  1554. }
  1555. for (i = 0; i < response_count; i++) {
  1556. if (!responses[i].should_keep_alive) continue;
  1557. for (j = 0; j < response_count; j++) {
  1558. if (!responses[j].should_keep_alive) continue;
  1559. for (k = 0; k < response_count; k++) {
  1560. test_multiple3(&responses[i], &responses[j], &responses[k]);
  1561. }
  1562. }
  1563. }
  1564. test_message_count_body(&responses[NO_HEADERS_NO_BODY_404]);
  1565. test_message_count_body(&responses[TRAILING_SPACE_ON_CHUNKED_BODY]);
  1566. // test very large chunked response
  1567. {
  1568. char * msg = create_large_chunked_message(31337,
  1569. "HTTP/1.0 200 OK\r\n"
  1570. "Transfer-Encoding: chunked\r\n"
  1571. "Content-Type: text/plain\r\n"
  1572. "\r\n");
  1573. struct message large_chunked =
  1574. {.name= "large chunked"
  1575. ,.type= HTTP_RESPONSE
  1576. ,.raw= msg
  1577. ,.should_keep_alive= FALSE
  1578. ,.message_complete_on_eof= FALSE
  1579. ,.http_major= 1
  1580. ,.http_minor= 0
  1581. ,.status_code= 200
  1582. ,.num_headers= 2
  1583. ,.headers=
  1584. { { "Transfer-Encoding", "chunked" }
  1585. , { "Content-Type", "text/plain" }
  1586. }
  1587. ,.body_size= 31337*1024
  1588. };
  1589. test_message_count_body(&large_chunked);
  1590. free(msg);
  1591. }
  1592. printf("response scan 1/2 ");
  1593. test_scan( &responses[TRAILING_SPACE_ON_CHUNKED_BODY]
  1594. , &responses[NO_HEADERS_NO_BODY_404]
  1595. , &responses[NO_REASON_PHRASE]
  1596. );
  1597. printf("response scan 2/2 ");
  1598. test_scan( &responses[BONJOUR_MADAME_FR]
  1599. , &responses[UNDERSTORE_HEADER_KEY]
  1600. , &responses[NO_CARRIAGE_RET]
  1601. );
  1602. puts("responses okay");
  1603. /// REQUESTS
  1604. test_simple("hello world", 0);
  1605. test_simple("GET / HTP/1.1\r\n\r\n", 0);
  1606. test_simple("ASDF / HTTP/1.1\r\n\r\n", 0);
  1607. test_simple("PROPPATCHA / HTTP/1.1\r\n\r\n", 0);
  1608. test_simple("GETA / HTTP/1.1\r\n\r\n", 0);
  1609. // Well-formed but incomplete
  1610. test_simple("GET / HTTP/1.1\r\n"
  1611. "Content-Type: text/plain\r\n"
  1612. "Content-Length: 6\r\n"
  1613. "\r\n"
  1614. "fooba",
  1615. 0);
  1616. static const char *all_methods[] = {
  1617. "DELETE",
  1618. "GET",
  1619. "HEAD",
  1620. "POST",
  1621. "PUT",
  1622. //"CONNECT", //CONNECT can't be tested like other methods, it's a tunnel
  1623. "OPTIONS",
  1624. "TRACE",
  1625. "COPY",
  1626. "LOCK",
  1627. "MKCOL",
  1628. "MOVE",
  1629. "PROPFIND",
  1630. "PROPPATCH",
  1631. "UNLOCK",
  1632. 0 };
  1633. const char **this_method;
  1634. for (this_method = all_methods; *this_method; this_method++) {
  1635. char buf[200];
  1636. sprintf(buf, "%s / HTTP/1.1\r\n\r\n", *this_method);
  1637. test_simple(buf, 1);
  1638. }
  1639. const char *dumbfuck2 =
  1640. "GET / HTTP/1.1\r\n"
  1641. "X-SSL-Bullshit: -----BEGIN CERTIFICATE-----\r\n"
  1642. "\tMIIFbTCCBFWgAwIBAgICH4cwDQYJKoZIhvcNAQEFBQAwcDELMAkGA1UEBhMCVUsx\r\n"
  1643. "\tETAPBgNVBAoTCGVTY2llbmNlMRIwEAYDVQQLEwlBdXRob3JpdHkxCzAJBgNVBAMT\r\n"
  1644. "\tAkNBMS0wKwYJKoZIhvcNAQkBFh5jYS1vcGVyYXRvckBncmlkLXN1cHBvcnQuYWMu\r\n"
  1645. "\tdWswHhcNMDYwNzI3MTQxMzI4WhcNMDcwNzI3MTQxMzI4WjBbMQswCQYDVQQGEwJV\r\n"
  1646. "\tSzERMA8GA1UEChMIZVNjaWVuY2UxEzARBgNVBAsTCk1hbmNoZXN0ZXIxCzAJBgNV\r\n"
  1647. "\tBAcTmrsogriqMWLAk1DMRcwFQYDVQQDEw5taWNoYWVsIHBhcmQYJKoZIhvcNAQEB\r\n"
  1648. "\tBQADggEPADCCAQoCggEBANPEQBgl1IaKdSS1TbhF3hEXSl72G9J+WC/1R64fAcEF\r\n"
  1649. "\tW51rEyFYiIeZGx/BVzwXbeBoNUK41OK65sxGuflMo5gLflbwJtHBRIEKAfVVp3YR\r\n"
  1650. "\tgW7cMA/s/XKgL1GEC7rQw8lIZT8RApukCGqOVHSi/F1SiFlPDxuDfmdiNzL31+sL\r\n"
  1651. "\t0iwHDdNkGjy5pyBSB8Y79dsSJtCW/iaLB0/n8Sj7HgvvZJ7x0fr+RQjYOUUfrePP\r\n"
  1652. "\tu2MSpFyf+9BbC/aXgaZuiCvSR+8Snv3xApQY+fULK/xY8h8Ua51iXoQ5jrgu2SqR\r\n"
  1653. "\twgA7BUi3G8LFzMBl8FRCDYGUDy7M6QaHXx1ZWIPWNKsCAwEAAaOCAiQwggIgMAwG\r\n"
  1654. "\tA1UdEwEB/wQCMAAwEQYJYIZIAYb4QgHTTPAQDAgWgMA4GA1UdDwEB/wQEAwID6DAs\r\n"
  1655. "\tBglghkgBhvhCAQ0EHxYdVUsgZS1TY2llbmNlIFVzZXIgQ2VydGlmaWNhdGUwHQYD\r\n"
  1656. "\tVR0OBBYEFDTt/sf9PeMaZDHkUIldrDYMNTBZMIGaBgNVHSMEgZIwgY+AFAI4qxGj\r\n"
  1657. "\tloCLDdMVKwiljjDastqooXSkcjBwMQswCQYDVQQGEwJVSzERMA8GA1UEChMIZVNj\r\n"
  1658. "\taWVuY2UxEjAQBgNVBAsTCUF1dGhvcml0eTELMAkGA1UEAxMCQ0ExLTArBgkqhkiG\r\n"
  1659. "\t9w0BCQEWHmNhLW9wZXJhdG9yQGdyaWQtc3VwcG9ydC5hYy51a4IBADApBgNVHRIE\r\n"
  1660. "\tIjAggR5jYS1vcGVyYXRvckBncmlkLXN1cHBvcnQuYWMudWswGQYDVR0gBBIwEDAO\r\n"
  1661. "\tBgwrBgEEAdkvAQEBAQYwPQYJYIZIAYb4QgEEBDAWLmh0dHA6Ly9jYS5ncmlkLXN1\r\n"
  1662. "\tcHBvcnQuYWMudmT4sopwqlBWsvcHViL2NybC9jYWNybC5jcmwwPQYJYIZIAYb4QgEDBDAWLmh0\r\n"
  1663. "\tdHA6Ly9jYS5ncmlkLXN1cHBvcnQuYWMudWsvcHViL2NybC9jYWNybC5jcmwwPwYD\r\n"
  1664. "\tVR0fBDgwNjA0oDKgMIYuaHR0cDovL2NhLmdyaWQt5hYy51ay9wdWIv\r\n"
  1665. "\tY3JsL2NhY3JsLmNybDANBgkqhkiG9w0BAQUFAAOCAQEAS/U4iiooBENGW/Hwmmd3\r\n"
  1666. "\tXCy6Zrt08YjKCzGNjorT98g8uGsqYjSxv/hmi0qlnlHs+k/3Iobc3LjS5AMYr5L8\r\n"
  1667. "\tUO7OSkgFFlLHQyC9JzPfmLCAugvzEbyv4Olnsr8hbxF1MbKZoQxUZtMVu29wjfXk\r\n"
  1668. "\thTeApBv7eaKCWpSp7MCbvgzm74izKhu3vlDk9w6qVrxePfGgpKPqfHiOoGhFnbTK\r\n"
  1669. "\twTC6o2xq5y0qZ03JonF7OJspEd3I5zKY3E+ov7/ZhW6DqT8UFvsAdjvQbXyhV8Eu\r\n"
  1670. "\tYhixw1aKEPzNjNowuIseVogKOLXxWI5vAi5HgXdS0/ES5gDGsABo4fqovUKlgop3\r\n"
  1671. "\tRA==\r\n"
  1672. "\t-----END CERTIFICATE-----\r\n"
  1673. "\r\n";
  1674. test_simple(dumbfuck2, 0);
  1675. #if 0
  1676. // NOTE(Wed Nov 18 11:57:27 CET 2009) this seems okay. we just read body
  1677. // until EOF.
  1678. //
  1679. // no content-length
  1680. // error if there is a body without content length
  1681. const char *bad_get_no_headers_no_body = "GET /bad_get_no_headers_no_body/world HTTP/1.1\r\n"
  1682. "Accept: */*\r\n"
  1683. "\r\n"
  1684. "HELLO";
  1685. test_simple(bad_get_no_headers_no_body, 0);
  1686. #endif
  1687. /* TODO sending junk and large headers gets rejected */
  1688. /* check to make sure our predefined requests are okay */
  1689. for (i = 0; requests[i].name; i++) {
  1690. test_message(&requests[i]);
  1691. }
  1692. for (i = 0; i < request_count; i++) {
  1693. if (!requests[i].should_keep_alive) continue;
  1694. for (j = 0; j < request_count; j++) {
  1695. if (!requests[j].should_keep_alive) continue;
  1696. for (k = 0; k < request_count; k++) {
  1697. test_multiple3(&requests[i], &requests[j], &requests[k]);
  1698. }
  1699. }
  1700. }
  1701. printf("request scan 1/4 ");
  1702. test_scan( &requests[GET_NO_HEADERS_NO_BODY]
  1703. , &requests[GET_ONE_HEADER_NO_BODY]
  1704. , &requests[GET_NO_HEADERS_NO_BODY]
  1705. );
  1706. printf("request scan 2/4 ");
  1707. test_scan( &requests[POST_CHUNKED_ALL_YOUR_BASE]
  1708. , &requests[POST_IDENTITY_BODY_WORLD]
  1709. , &requests[GET_FUNKY_CONTENT_LENGTH]
  1710. );
  1711. printf("request scan 3/4 ");
  1712. test_scan( &requests[TWO_CHUNKS_MULT_ZERO_END]
  1713. , &requests[CHUNKED_W_TRAILING_HEADERS]
  1714. , &requests[CHUNKED_W_BULLSHIT_AFTER_LENGTH]
  1715. );
  1716. printf("request scan 4/4 ");
  1717. test_scan( &requests[QUERY_URL_WITH_QUESTION_MARK_GET]
  1718. , &requests[PREFIX_NEWLINE_GET ]
  1719. , &requests[CONNECT_REQUEST]
  1720. );
  1721. puts("requests okay");
  1722. return 0;
  1723. }