request.c 68 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544
  1. /*
  2. HTTP request handling tests
  3. Copyright (C) 2001-2010, Joe Orton <[email protected]>
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. */
  16. #include "config.h"
  17. #include <sys/types.h>
  18. #include <time.h> /* for time() */
  19. #ifdef HAVE_STDLIB_H
  20. #include <stdlib.h>
  21. #endif
  22. #ifdef HAVE_UNISTD_H
  23. #include <unistd.h>
  24. #endif
  25. #include <fcntl.h>
  26. #include <errno.h>
  27. #include "ne_request.h"
  28. #include "ne_socket.h"
  29. #include "tests.h"
  30. #include "child.h"
  31. #include "utils.h"
  32. static char buffer[BUFSIZ];
  33. static ne_session *def_sess;
  34. static ne_request *def_req;
  35. /* Last (real) port used by fail_request_with_error(). */
  36. static unsigned int fail_request_last_port;
  37. static int prepare_request(server_fn fn, void *ud)
  38. {
  39. static char uri[100];
  40. unsigned int port;
  41. CALL(new_spawn_server(1, fn, ud, &port));
  42. def_sess = ne_session_create("http", "localhost", port);
  43. sprintf(uri, "/test%d", test_num);
  44. def_req = ne_request_create(def_sess, "GET", uri);
  45. return OK;
  46. }
  47. static int finish_request(void)
  48. {
  49. ne_request_destroy(def_req);
  50. return destroy_and_wait(def_sess);
  51. }
  52. #define RESP200 "HTTP/1.1 200 OK\r\n" "Server: neon-test-server\r\n"
  53. #define TE_CHUNKED "Transfer-Encoding: chunked\r\n"
  54. /* takes response body chunks and appends them to a buffer. */
  55. static int collector(void *ud, const char *data, size_t len)
  56. {
  57. ne_buffer *buf = ud;
  58. ne_buffer_append(buf, data, len);
  59. return 0;
  60. }
  61. typedef ne_request *(*construct_request)(ne_session *sess, void *userdata);
  62. /* construct a get request, callback for run_request. */
  63. static ne_request *construct_get(ne_session *sess, void *userdata)
  64. {
  65. ne_request *r = ne_request_create(sess, "GET", "/");
  66. ne_buffer *buf = userdata;
  67. if (buf) ne_add_response_body_reader(r, ne_accept_2xx, collector, buf);
  68. return r;
  69. }
  70. /* run a request created by callback 'cb' in session 'sess'. */
  71. static int run_request(ne_session *sess, int status,
  72. construct_request cb, void *userdata)
  73. {
  74. ne_request *req = cb(sess, userdata);
  75. ON(req == NULL);
  76. ONREQ(ne_request_dispatch(req));
  77. ONV(ne_get_status(req)->code != status,
  78. ("response status-code was %d not %d",
  79. ne_get_status(req)->code, status));
  80. ne_request_destroy(req);
  81. return OK;
  82. }
  83. /* Runs a server function 'fn', expecting to get a header 'name' with
  84. * value 'value' in the response. If 'value' is NULL, expects that
  85. * *no* header of that name is present. */
  86. static int expect_header_value(const char *name, const char *value,
  87. server_fn fn, void *userdata)
  88. {
  89. ne_session *sess;
  90. ne_request *req;
  91. const char *gotval;
  92. CALL(make_session(&sess, fn, userdata));
  93. req = ne_request_create(sess, "FOO", "/bar");
  94. ONREQ(ne_request_dispatch(req));
  95. ne_close_connection(sess);
  96. CALL(await_server());
  97. gotval = ne_get_response_header(req, name);
  98. ONV(value && !gotval, ("header '%s: %s' not sent", name, value));
  99. ONV(!value && gotval, ("header '%s: %s' not expected", name, gotval));
  100. ONV(value && gotval && strcmp(gotval, value),
  101. ("header '%s' mis-match: got '%s' not '%s'",
  102. name, gotval, value));
  103. ne_request_destroy(req);
  104. ne_session_destroy(sess);
  105. return OK;
  106. }
  107. /* runs a server function 'fn', expecting response body to be equal to
  108. * 'expect' */
  109. static int expect_response(const char *expect, server_fn fn, void *userdata)
  110. {
  111. ne_session *sess;
  112. ne_buffer *buf = ne_buffer_create();
  113. CALL(session_server(&sess, fn, userdata));
  114. CALL(run_request(sess, 200, construct_get, buf));
  115. CALL(destroy_and_wait(sess));
  116. ONN("response body match", strcmp(buf->data, expect));
  117. ne_buffer_destroy(buf);
  118. return OK;
  119. }
  120. #define EMPTY_RESP RESP200 "Content-Length: 0\r\n\r\n"
  121. /* Process a request with given method and response, expecting to get
  122. * a zero-length response body. A second request is sent down the
  123. * connection (to ensure that the response isn't silently eaten), so
  124. * 'resp' must be an HTTP/1.1 response with no 'Connection: close'
  125. * header. */
  126. static int expect_no_body(const char *method, const char *resp)
  127. {
  128. ne_session *sess;
  129. ne_request *req;
  130. ssize_t ret;
  131. char *r = ne_malloc(strlen(resp) + sizeof(EMPTY_RESP));
  132. strcpy(r, resp);
  133. strcat(r, EMPTY_RESP);
  134. CALL(session_server(&sess, single_serve_string, r));
  135. ne_free(r);
  136. req = ne_request_create(sess, method, "/first");
  137. ONN("failed to begin request", ne_begin_request(req));
  138. ret = ne_read_response_block(req, buffer, BUFSIZ);
  139. ONV(ret != 0, ("got response block of size %" NE_FMT_SSIZE_T, ret));
  140. ONN("failed to end request", ne_end_request(req));
  141. /* process following request; makes sure that nothing extra has
  142. * been eaten by the first request. */
  143. ONV(any_request(sess, "/second"),
  144. ("second request on connection failed: %s",ne_get_error(sess)));
  145. ne_request_destroy(req);
  146. return destroy_and_wait(sess);
  147. }
  148. static int reason_phrase(void)
  149. {
  150. ne_session *sess;
  151. CALL(make_session(&sess, single_serve_string, RESP200
  152. "Connection: close\r\n\r\n"));
  153. ONREQ(any_request(sess, "/foo"));
  154. ne_close_connection(sess);
  155. CALL(await_server());
  156. ONV(strcmp(ne_get_error(sess), "200 OK"),
  157. ("reason phrase mismatch: got `%s' not `200 OK'",
  158. ne_get_error(sess)));
  159. ne_session_destroy(sess);
  160. return OK;
  161. }
  162. static int single_get_eof(void)
  163. {
  164. return expect_response("a", single_serve_string,
  165. RESP200
  166. "Connection: close\r\n"
  167. "\r\n"
  168. "a");
  169. }
  170. static int single_get_clength(void)
  171. {
  172. return expect_response("a", single_serve_string,
  173. RESP200
  174. "Content-Length: \t\t 1 \t\t\r\n"
  175. "\r\n"
  176. "a"
  177. "bbbbbbbbasdasd");
  178. }
  179. static int single_get_chunked(void)
  180. {
  181. return expect_response("a", single_serve_string,
  182. RESP200 TE_CHUNKED
  183. "\r\n"
  184. "1\r\n"
  185. "a\r\n"
  186. "0\r\n" "\r\n"
  187. "g;lkjalskdjalksjd");
  188. }
  189. static int no_body_304(void)
  190. {
  191. return expect_no_body("GET", "HTTP/1.1 304 Not Mfodified\r\n"
  192. "Content-Length: 5\r\n\r\n");
  193. }
  194. static int no_body_204(void)
  195. {
  196. return expect_no_body("GET", "HTTP/1.1 204 Not Modified\r\n"
  197. "Content-Length: 5\r\n\r\n");
  198. }
  199. static int no_body_HEAD(void)
  200. {
  201. return expect_no_body("HEAD", "HTTP/1.1 200 OK\r\n"
  202. "Content-Length: 5\r\n\r\n");
  203. }
  204. static int no_headers(void)
  205. {
  206. return expect_response("abcde", single_serve_string,
  207. "HTTP/1.1 200 OK\r\n\r\n"
  208. "abcde");
  209. }
  210. #define CHUNK(len, data) #len "\r\n" data "\r\n"
  211. #define ABCDE_CHUNKS CHUNK(1, "a") CHUNK(1, "b") \
  212. CHUNK(1, "c") CHUNK(1, "d") \
  213. CHUNK(1, "e") CHUNK(0, "")
  214. static int chunks(void)
  215. {
  216. /* lots of little chunks. */
  217. return expect_response("abcde", single_serve_string,
  218. RESP200 TE_CHUNKED
  219. "\r\n"
  220. ABCDE_CHUNKS);
  221. }
  222. static int te_header(void)
  223. {
  224. return expect_response("abcde", single_serve_string,
  225. RESP200 "Transfer-Encoding: CHUNKED\r\n"
  226. "\r\n" ABCDE_CHUNKS);
  227. }
  228. static int te_identity(void)
  229. {
  230. /* http://bugzilla.gnome.org/show_bug.cgi?id=310636 says privoxy
  231. * uses the "identity" transfer-coding. */
  232. return expect_response("abcde", single_serve_string,
  233. RESP200 "Transfer-Encoding: identity\r\n"
  234. "Content-Length: 5\r\n"
  235. "\r\n"
  236. "abcde");
  237. }
  238. static int chunk_numeric(void)
  239. {
  240. /* leading zero's */
  241. return expect_response("0123456789abcdef", single_serve_string,
  242. RESP200 TE_CHUNKED
  243. "\r\n"
  244. "000000010\r\n" "0123456789abcdef\r\n"
  245. "000000000\r\n" "\r\n");
  246. }
  247. static int chunk_extensions(void)
  248. {
  249. /* chunk-extensions. */
  250. return expect_response("0123456789abcdef", single_serve_string,
  251. RESP200 TE_CHUNKED
  252. "\r\n"
  253. "000000010; foo=bar; norm=fish\r\n"
  254. "0123456789abcdef\r\n"
  255. "000000000\r\n" "\r\n");
  256. }
  257. static int chunk_trailers(void)
  258. {
  259. /* trailers. */
  260. return expect_response("abcde", single_serve_string,
  261. RESP200 TE_CHUNKED
  262. "\r\n"
  263. "00000005; foo=bar; norm=fish\r\n"
  264. "abcde\r\n"
  265. "000000000\r\n"
  266. "X-Hello: world\r\n"
  267. "X-Another: header\r\n"
  268. "\r\n");
  269. }
  270. static int chunk_oversize(void)
  271. {
  272. #define BIG (20000)
  273. char *body = ne_malloc(BIG + 1);
  274. static const char rnd[] = "abcdefghijklm";
  275. int n;
  276. ne_buffer *buf = ne_buffer_create();
  277. for (n = 0; n < BIG; n++) {
  278. body[n] = rnd[n % (sizeof(rnd) - 1)];
  279. }
  280. body[n] = '\0';
  281. #undef BIG
  282. ne_buffer_concat(buf, RESP200 TE_CHUNKED "\r\n"
  283. "4E20\r\n", body, "\r\n",
  284. "0\r\n\r\n", NULL);
  285. CALL(expect_response(body, single_serve_string, buf->data));
  286. ne_buffer_destroy(buf);
  287. ne_free(body);
  288. return OK;
  289. }
  290. static int te_over_clength(void)
  291. {
  292. /* T-E dominates over C-L. */
  293. return expect_response("abcde", single_serve_string,
  294. RESP200 TE_CHUNKED
  295. "Content-Length: 300\r\n"
  296. "\r\n"
  297. ABCDE_CHUNKS);
  298. }
  299. /* te_over_clength with the headers the other way round; check for
  300. * ordering problems. */
  301. static int te_over_clength2(void)
  302. {
  303. return expect_response("abcde", single_serve_string,
  304. RESP200 "Content-Length: 300\r\n"
  305. TE_CHUNKED
  306. "\r\n"
  307. ABCDE_CHUNKS);
  308. }
  309. /* obscure case which is possibly a valid request by 2616, but should
  310. * be handled correctly in any case. neon <0.22.0 tries to
  311. * eat the response body, which is probably incorrect. */
  312. static int no_body_chunks(void)
  313. {
  314. return expect_no_body("HEAD", "HTTP/1.1 204 Not Modified\r\n"
  315. TE_CHUNKED "\r\n");
  316. }
  317. static int serve_twice(ne_socket *sock, void *userdata)
  318. {
  319. const char *resp = userdata;
  320. CALL(discard_request(sock));
  321. SEND_STRING(sock, resp);
  322. CALL(discard_request(sock));
  323. SEND_STRING(sock, resp);
  324. return OK;
  325. }
  326. /* Test persistent connection handling: serve 'response' twice on a
  327. * single TCP connection, expecting to get a response body equal to
  328. * 'body' both times. */
  329. static int test_persist_p(const char *response, const char *body, int proxy)
  330. {
  331. ne_session *sess;
  332. ne_buffer *buf = ne_buffer_create();
  333. if (proxy) {
  334. CALL(proxied_session_server(&sess, "http", "localhost", 1234,
  335. serve_twice, (void *)response));
  336. ne_set_session_flag(sess, NE_SESSFLAG_CONNAUTH, 1);
  337. }
  338. else {
  339. CALL(session_server(&sess, serve_twice, (void *)response));
  340. }
  341. CALL(run_request(sess, 200, construct_get, buf));
  342. ONV(strcmp(buf->data, body),
  343. ("response #1 mismatch: [%s] not [%s]", buf->data, body));
  344. /* Run it again. */
  345. ne_buffer_clear(buf);
  346. CALL(run_request(sess, 200, construct_get, buf));
  347. CALL(destroy_and_wait(sess));
  348. ONV(strcmp(buf->data, body),
  349. ("response #2 mismatch: [%s] not [%s]", buf->data, body));
  350. ne_buffer_destroy(buf);
  351. return OK;
  352. }
  353. static int test_persist(const char *response, const char *body)
  354. {
  355. return test_persist_p(response, body, 0);
  356. }
  357. static int persist_http11(void)
  358. {
  359. return test_persist(RESP200 "Content-Length: 5\r\n\r\n" "abcde",
  360. "abcde");
  361. }
  362. static int persist_chunked(void)
  363. {
  364. return test_persist(RESP200 TE_CHUNKED "\r\n" ABCDE_CHUNKS,
  365. "abcde");
  366. }
  367. static int persist_http10(void)
  368. {
  369. return test_persist("HTTP/1.0 200 OK\r\n"
  370. "Connection: keep-alive\r\n"
  371. "Content-Length: 5\r\n\r\n" "abcde",
  372. "abcde");
  373. }
  374. static int persist_proxy_http10(void)
  375. {
  376. return test_persist_p("HTTP/1.0 200 OK\r\n"
  377. "Proxy-Connection: keep-alive\r\n"
  378. "Content-Length: 5\r\n\r\n" "abcde",
  379. "abcde", 1);
  380. }
  381. /* Server function for fail_early_eof */
  382. static int serve_eof(ne_socket *sock, void *ud)
  383. {
  384. const char *resp = ud;
  385. /* dummy request/response. */
  386. CALL(discard_request(sock));
  387. CALL(SEND_STRING(sock, RESP200 "Content-Length: 0\r\n\r\n"));
  388. /* real request/response. */
  389. CALL(discard_request(sock));
  390. CALL(SEND_STRING(sock, resp));
  391. return OK;
  392. }
  393. /* Utility function: 'resp' is a truncated response; such that an EOF
  394. * arrives early during response processing; but NOT as a valid
  395. * premature EOF due to a persistent connection timeout. It is an
  396. * error if the request is then retried, and the test fails. */
  397. static int fail_early_eof(const char *resp)
  398. {
  399. ne_session *sess;
  400. unsigned int port;
  401. CALL(new_spawn_server(3, serve_eof, (char *)resp, &port));
  402. sess = ne_session_create("http", "localhost", port);
  403. ONREQ(any_request(sess, "/foo"));
  404. ONN("request retried after early EOF",
  405. any_request(sess, "/foobar") == NE_OK);
  406. CALL(reap_server());
  407. ne_session_destroy(sess);
  408. return OK;
  409. }
  410. /* This failed with neon <0.22. */
  411. static int fail_eof_continued(void)
  412. {
  413. return fail_early_eof("HTTP/1.1 100 OK\r\n\r\n");
  414. }
  415. static int fail_eof_headers(void)
  416. {
  417. return fail_early_eof("HTTP/1.1 200 OK\r\nJimbob\r\n");
  418. }
  419. static int fail_eof_chunk(void)
  420. {
  421. return fail_early_eof(RESP200 TE_CHUNKED "\r\n" "1\r\n" "a");
  422. }
  423. static int fail_eof_badclen(void)
  424. {
  425. return fail_early_eof(RESP200 "Content-Length: 10\r\n\r\n" "abcde");
  426. }
  427. /* Persistent connection timeout where a FIN is sent to terminate the
  428. * connection, which is caught by a 0 return from the read() when the
  429. * second request reads the status-line. */
  430. static int ptimeout_eof(void)
  431. {
  432. ne_session *sess;
  433. unsigned int port;
  434. CALL(new_spawn_server(4, single_serve_string,
  435. RESP200 "Content-Length: 0\r\n" "\r\n",
  436. &port));
  437. sess = ne_session_create("http", "localhost", port);
  438. CALL(any_2xx_request(sess, "/first"));
  439. CALL(any_2xx_request(sess, "/second"));
  440. ONN("server died prematurely?", dead_server());
  441. reap_server();
  442. ne_session_destroy(sess);
  443. return OK;
  444. }
  445. /* Persistent connection timeout where a FIN is sent to terminate the
  446. * connection, but the request fails in the write() call which sends
  447. * the body. */
  448. static int ptimeout_eof2(void)
  449. {
  450. ne_session *sess;
  451. unsigned int port;
  452. CALL(new_spawn_server(4, single_serve_string,
  453. RESP200 "Content-Length: 0\r\n" "\r\n",
  454. &port));
  455. sess = ne_session_create("http", "localhost", port);
  456. CALL(any_2xx_request(sess, "/first"));
  457. minisleep();
  458. CALL(any_2xx_request_body(sess, "/second"));
  459. ONN("server died prematurely?", dead_server());
  460. reap_server();
  461. ne_session_destroy(sess);
  462. return OK;
  463. }
  464. /* TODO: add a ptimeout_reset too, if an RST can be reliably generated
  465. * mid-connection. */
  466. static int incr_server(ne_socket *sock, void *arg)
  467. {
  468. struct many_serve_args *msa = arg;
  469. CALL(many_serve_string(sock, msa));
  470. msa->count++;
  471. return OK;
  472. }
  473. /* Emulates a persistent connection timeout on the server. This tests
  474. * the timeout occurring after between 1 and 10 requests down the
  475. * connection. */
  476. static int persist_timeout(void)
  477. {
  478. ne_session *sess;
  479. ne_buffer *buf = ne_buffer_create();
  480. struct many_serve_args args;
  481. unsigned int port;
  482. int n;
  483. args.str = RESP200 "Content-Length: 5\r\n\r\n" "abcde";
  484. args.count = 1;
  485. CALL(new_spawn_server(9, incr_server, &args, &port));
  486. sess = ne_session_create("http", "localhost", port);
  487. for (args.count = 1; args.count < 10; args.count++) {
  488. for (n = 0; n < args.count; n++) {
  489. ONV(run_request(sess, 200, construct_get, buf),
  490. ("%d of %d, request failed: %s", n, args.count,
  491. ne_get_error(sess)));
  492. ONV(strcmp(buf->data, "abcde"),
  493. ("%d of %d, response body mismatch", n, args.count));
  494. /* Ready for next time. */
  495. ne_buffer_clear(buf);
  496. }
  497. }
  498. ne_buffer_destroy(buf);
  499. return destroy_and_wait(sess);
  500. }
  501. /* Test that an HTTP/1.0 server is not presumed to support persistent
  502. * connections by default. */
  503. static int no_persist_http10(void)
  504. {
  505. ne_session *sess;
  506. unsigned int port;
  507. CALL(new_spawn_server(4, single_serve_string,
  508. "HTTP/1.0 200 OK\r\n"
  509. "Content-Length: 5\r\n\r\n"
  510. "abcde"
  511. "Hello, world - what a nice day!\r\n",
  512. &port));
  513. sess = ne_session_create("http", "localhost", port);
  514. /* if the connection is treated as persistent, the status-line for
  515. * the second request will be "Hello, world...", which will
  516. * fail. */
  517. ONREQ(any_request(sess, "/foobar"));
  518. ONREQ(any_request(sess, "/foobar"));
  519. ONN("server died prematurely?", dead_server());
  520. CALL(reap_server());
  521. ne_session_destroy(sess);
  522. return OK;
  523. }
  524. #define NO_BODY "Content-Length: 0\r\n\r\n"
  525. static int response_bodies(void)
  526. {
  527. struct {
  528. const char *expect;
  529. server_fn fn;
  530. const char *response;
  531. } ts[] = {
  532. { "abcde", single_serve_string, RESP200 "Stupid Header\r\n" "ReallyStupidHeader\r\n"
  533. "Content-Length: 5\r\n" "\r\n" "abcde" },
  534. { "abcde", single_serve_string, RESP200 "Content-Length: \r\n 5\r\n" "\r\n" "abcde" },
  535. { "abcde", single_serve_string, RESP200 "Content-Length: \r\n \r\n \r\n \r\n 5\r\n" "\r\n" "abcde" },
  536. { NULL, NULL, NULL }
  537. };
  538. unsigned n;
  539. for (n = 0; ts[n].expect != NULL; n++ ){
  540. CALL(expect_response(ts[n].expect, ts[n].fn, (void *)ts[n].response));
  541. }
  542. return OK;
  543. }
  544. static int response_headers(void)
  545. {
  546. struct {
  547. const char *name, *value;
  548. server_fn fn;
  549. const char *response;
  550. } ts[] = {
  551. { "ranDom-HEader", "", single_serve_string, RESP200 "RANDom-HeADEr:\r\n" NO_BODY },
  552. { "ranDom-HEader", "noddy", single_serve_string, RESP200 "RANDom-HeADEr: noddy\r\n" NO_BODY },
  553. { "ranDom-HEader", "fishy", single_serve_string, RESP200 "RANDom-HeADEr: fishy\r\n" NO_BODY },
  554. { "ranDom-HEader", "fishy", single_serve_string, RESP200 "RANDom-HeADEr \t : fishy\r\n" NO_BODY},
  555. { "ranDom-HEader", "fishy", single_serve_string, RESP200 "RANDom-HeADEr: fishy \r\n" NO_BODY },
  556. { "ranDom-HEader", "geezer", single_serve_string, RESP200 "RANDom-HeADEr: \t \tgeezer\r\n" NO_BODY },
  557. { "gONe", "fishing", single_serve_string,
  558. RESP200 TE_CHUNKED "\r\n0\r\n" "Hello: world\r\n" "GONE: fishing\r\n" "\r\n" },
  559. { "hello", "w o r l d", single_serve_string,
  560. RESP200 "Hello: \n\tw\r\n\to r l\r\n\td \r\n" NO_BODY },
  561. { "X-Header", "jim, jab, jar", single_serve_string,
  562. RESP200 "X-Header: jim\r\n" "x-header: jab\r\n" "x-Header: jar\r\n" NO_BODY },
  563. { "X-Header", "jim, jab, jar", single_serve_string,
  564. RESP200 "X-Header: jim \r\n" "x-header: jab \r\n" "x-Header: jar \r\n" NO_BODY },
  565. /* RFC 2616 14.10: headers listed in Connection must be stripped on
  566. * receiving an HTTP/1.0 message in case there was a pre-1.1 proxy
  567. * somewhere. */
  568. { "X-Widget", NULL, single_serve_string,
  569. "HTTP/1.0 200 OK\r\n" "Connection: x-widget\r\n" "x-widget: blah\r\n" NO_BODY },
  570. { "X-Widget", NULL, single_serve_string,
  571. "HTTP/1.0 200 OK\r\n" "Connection: connection, x-fish, x-widget\r\n" "x-widget: blah\r\n" NO_BODY },
  572. /* Test trailer header handling. */
  573. { "X-Trailer", "foo", single_serve_string,
  574. RESP200 TE_CHUNKED "X-Header: bar\r\n\r\n"
  575. CHUNK(6, "foobar") "0\r\nX-Trailer: foo\r\n\r\n" },
  576. { "X-Header", "foo", single_serve_string,
  577. RESP200 TE_CHUNKED "X-Header: foo\r\n\r\n"
  578. CHUNK(6, "foobar") "0\r\nX-Trailer: barish\r\n\r\n" },
  579. /* Test that merging between trailers and main response headers works. */
  580. { "X-Trailer", "fooish, barish", single_serve_string,
  581. RESP200 TE_CHUNKED "X-Trailer: fooish\r\n\r\n"
  582. CHUNK(6, "foobar") "0\r\nX-Trailer: barish\r\n\r\n" },
  583. { NULL, NULL, NULL, NULL }
  584. };
  585. unsigned n;
  586. for (n = 0; ts[n].name != NULL; n++ ){
  587. CALL(expect_header_value(ts[n].name, ts[n].value, ts[n].fn, (void *)ts[n].response));
  588. }
  589. return OK;
  590. }
  591. static int post_send_retry(ne_request *req, void *userdata,
  592. const ne_status *status)
  593. {
  594. return status->code == 400 ? NE_RETRY : NE_OK;
  595. }
  596. /* Test that the stored response headers are forgotten if the request
  597. * is retried. */
  598. static int reset_headers(void)
  599. {
  600. ne_session *sess;
  601. ne_request *req;
  602. const char *value;
  603. CALL(make_session(&sess, single_serve_string,
  604. "HTTP/1.1 400 Hit me again\r\n"
  605. "Content-Length: 0\r\n"
  606. "X-Foo: bar\r\n" "\r\n"
  607. "HTTP/1.1 200 Thank you kindly\r\n"
  608. "Content-Length: 0\r\n"
  609. "X-Foo: hello fair world\r\n" "\r\n"));
  610. ne_hook_post_send(sess, post_send_retry, NULL);
  611. req = ne_request_create(sess, "GET", "/foo");
  612. ONREQ(ne_request_dispatch(req));
  613. value = ne_get_response_header(req, "X-Foo");
  614. ONCMP("hello fair world", value, "response header", "X-Foo");
  615. ne_request_destroy(req);
  616. return destroy_and_wait(sess);
  617. }
  618. static int iterate_none(void)
  619. {
  620. ne_session *sess;
  621. ne_request *req;
  622. CALL(make_session(&sess, single_serve_string,
  623. "HTTP/1.0 200 OK\r\n\r\n"));
  624. req = ne_request_create(sess, "GET", "/");
  625. ONREQ(ne_request_dispatch(req));
  626. ONN("iterator was not NULL for no headers",
  627. ne_response_header_iterate(req, NULL, NULL, NULL) != NULL);
  628. ne_request_destroy(req);
  629. return destroy_and_wait(sess);
  630. }
  631. #define MANY_HEADERS (90)
  632. static int iterate_many(void)
  633. {
  634. ne_request *req;
  635. ne_buffer *buf = ne_buffer_create();
  636. ne_session *sess;
  637. int n;
  638. struct header {
  639. char name[10], value[10];
  640. int seen;
  641. } hdrs[MANY_HEADERS];
  642. void *cursor = NULL;
  643. const char *name, *value;
  644. ne_buffer_czappend(buf, "HTTP/1.0 200 OK\r\n");
  645. for (n = 0; n < MANY_HEADERS; n++) {
  646. sprintf(hdrs[n].name, "x-%d", n);
  647. sprintf(hdrs[n].value, "Y-%d", n);
  648. hdrs[n].seen = 0;
  649. ne_buffer_concat(buf, hdrs[n].name, ": ", hdrs[n].value, "\r\n", NULL);
  650. }
  651. ne_buffer_czappend(buf, "\r\n");
  652. CALL(make_session(&sess, single_serve_string, buf->data));
  653. req = ne_request_create(sess, "GET", "/foo");
  654. ONREQ(ne_request_dispatch(req));
  655. while ((cursor = ne_response_header_iterate(req, cursor, &name, &value))) {
  656. ONV(strncmp(name, "x-", 2) || strncmp(value, "Y-", 2)
  657. || strcmp(name + 2, value + 2)
  658. || (n = atoi(name + 2)) >= MANY_HEADERS
  659. || n < 0,
  660. ("bad name/value pair: %s = %s", name, value));
  661. NE_DEBUG(NE_DBG_HTTP, "iterate: got pair (%d): %s = %s\n",
  662. n, name, value);
  663. ONV(hdrs[n].seen == 1, ("duplicate pair %d", n));
  664. hdrs[n].seen = 1;
  665. }
  666. for (n = 0; n < MANY_HEADERS; n++) {
  667. ONV(hdrs[n].seen == 0, ("unseen pair %d", n));
  668. }
  669. ne_buffer_destroy(buf);
  670. ne_request_destroy(req);
  671. return destroy_and_wait(sess);
  672. }
  673. struct s1xx_args {
  674. int count;
  675. int hdrs;
  676. };
  677. static int serve_1xx(ne_socket *sock, void *ud)
  678. {
  679. struct s1xx_args *args = ud;
  680. CALL(discard_request(sock));
  681. do {
  682. if (args->hdrs) {
  683. SEND_STRING(sock, "HTTP/1.1 100 Continue\r\n"
  684. "Random: header\r\n"
  685. "Another: header\r\n\r\n");
  686. } else {
  687. SEND_STRING(sock, "HTTP/1.1 100 Continue\r\n\r\n");
  688. }
  689. } while (--args->count > 0);
  690. SEND_STRING(sock, RESP200 "Content-Length: 0\r\n\r\n");
  691. return OK;
  692. }
  693. #define sess def_sess
  694. static int skip_interim_1xx(void)
  695. {
  696. struct s1xx_args args = {0, 0};
  697. ON(prepare_request(serve_1xx, &args));
  698. ONREQ(ne_request_dispatch(def_req));
  699. return finish_request();
  700. }
  701. static int skip_many_1xx(void)
  702. {
  703. struct s1xx_args args = {5, 0};
  704. ON(prepare_request(serve_1xx, &args));
  705. ONREQ(ne_request_dispatch(def_req));
  706. return finish_request();
  707. }
  708. static int skip_1xx_hdrs(void)
  709. {
  710. struct s1xx_args args = {5, 5};
  711. ON(prepare_request(serve_1xx, &args));
  712. ONREQ(ne_request_dispatch(def_req));
  713. return finish_request();
  714. }
  715. #undef sess
  716. /* server for expect_100_once: serves a 100-continue request, and
  717. * fails if the request body is sent twice. */
  718. static int serve_100_once(ne_socket *sock, void *ud)
  719. {
  720. struct s1xx_args args = {2, 0};
  721. char ch;
  722. CALL(serve_1xx(sock, &args));
  723. CALL(discard_body(sock));
  724. ONN("body was served twice", ne_sock_read(sock, &ch, 1) == 1);
  725. return OK;
  726. }
  727. /* regression test; fails with neon <0.22, where the request body was
  728. * served *every* time a 1xx response was received, rather than just
  729. * once. */
  730. static int expect_100_once(void)
  731. {
  732. ne_session *sess;
  733. ne_request *req;
  734. char body[BUFSIZ];
  735. CALL(make_session(&sess, serve_100_once, NULL));
  736. req = ne_request_create(sess, "GET", "/foo");
  737. ne_set_request_flag(req, NE_REQFLAG_EXPECT100, 1);
  738. ONN("expect100 flag ignored",
  739. ne_get_request_flag(req, NE_REQFLAG_EXPECT100) != 1);
  740. memset(body, 'A', sizeof(body));
  741. ne_set_request_body_buffer(req, body, sizeof(body));
  742. ONREQ(ne_request_dispatch(req));
  743. ne_request_destroy(req);
  744. return destroy_and_wait(sess);
  745. }
  746. /* regression test for enabling 100-continue without sending a body. */
  747. static int expect_100_nobody(void)
  748. {
  749. ne_session *sess;
  750. ne_request *req;
  751. CALL(make_session(&sess, serve_100_once, NULL));
  752. req = ne_request_create(sess, "GET", "/foo");
  753. ne_set_request_flag(req, NE_REQFLAG_EXPECT100, 1);
  754. ONREQ(ne_request_dispatch(req));
  755. ne_request_destroy(req);
  756. return destroy_and_wait(sess);
  757. }
  758. struct body {
  759. char *body;
  760. size_t size;
  761. };
  762. static int want_body(ne_socket *sock, void *userdata)
  763. {
  764. struct body *b = userdata;
  765. char *buf = ne_malloc(b->size);
  766. clength = 0;
  767. CALL(discard_request(sock));
  768. ONN("request has c-l header", clength == 0);
  769. ONN("request length", clength != (int)b->size);
  770. NE_DEBUG(NE_DBG_HTTP,
  771. "reading body of %" NE_FMT_SIZE_T " bytes...\n", b->size);
  772. ON(ne_sock_fullread(sock, buf, b->size));
  773. ON(SEND_STRING(sock, RESP200 "Content-Length: 0\r\n\r\n"));
  774. ON(memcmp(buf, b->body, b->size));
  775. ne_free(buf);
  776. return OK;
  777. }
  778. static ssize_t provide_body(void *userdata, char *buf, size_t buflen)
  779. {
  780. static const char *pnt;
  781. static size_t left;
  782. struct body *b = userdata;
  783. if (buflen == 0) {
  784. pnt = b->body;
  785. left = b->size;
  786. } else {
  787. if (left < buflen) buflen = left;
  788. memcpy(buf, pnt, buflen);
  789. left -= buflen;
  790. }
  791. return buflen;
  792. }
  793. static int send_bodies(void)
  794. {
  795. unsigned int n, m;
  796. struct body bodies[] = {
  797. { "abcde", 5 },
  798. { "\0\0\0\0\0\0", 6 },
  799. { NULL, 50000 },
  800. { NULL }
  801. };
  802. #define BIG 2
  803. /* make the body with some cruft. */
  804. bodies[BIG].body = ne_malloc(bodies[BIG].size);
  805. for (n = 0; n < bodies[BIG].size; n++) {
  806. bodies[BIG].body[n] = (char)n%80;
  807. }
  808. for (m = 0; m < 2; m++) {
  809. for (n = 0; bodies[n].body != NULL; n++) {
  810. ne_session *sess;
  811. ne_request *req;
  812. CALL(session_server(&sess, want_body, &(bodies[n])));
  813. req = ne_request_create(sess, "PUT", "/");
  814. ON(req == NULL);
  815. if (m == 0) {
  816. ne_set_request_body_buffer(req, bodies[n].body, bodies[n].size);
  817. } else {
  818. ne_set_request_body_provider(req, bodies[n].size,
  819. provide_body, &bodies[n]);
  820. }
  821. ONREQ(ne_request_dispatch(req));
  822. ne_request_destroy(req);
  823. CALL(destroy_and_wait(sess));
  824. }
  825. }
  826. ne_free(bodies[BIG].body);
  827. return OK;
  828. }
  829. /* Utility function: run a request using the given server fn, and the
  830. * request should fail. If 'error' is non-NULL, it must be a substring
  831. * of the error string. */
  832. static int fail_request_with_error(int with_body, server_fn fn, void *ud,
  833. int forever, const char *error)
  834. {
  835. ne_session *sess;
  836. ne_request *req;
  837. unsigned int port;
  838. int ret;
  839. CALL(new_spawn_server(forever ? 100 : 1, fn, ud, &port));
  840. sess = ne_session_create("http", "localhost", port);
  841. /* Set default timeout, required by e.g. fail_excess_1xx. */
  842. ne_set_read_timeout(sess, 2);
  843. req = ne_request_create(sess, "GET", "/");
  844. if (with_body) {
  845. static const char *body = "random stuff";
  846. ne_set_request_body_buffer(req, body, strlen(body));
  847. }
  848. /* request should fail. */
  849. ret = ne_request_dispatch(req);
  850. ONN("request succeeded", ret == NE_OK);
  851. if (!forever) {
  852. /* reap the server, don't care what it's doing. */
  853. reap_server();
  854. }
  855. NE_DEBUG(NE_DBG_HTTP, "Response gave error `%s'\n", ne_get_error(sess));
  856. ONV(error && strstr(ne_get_error(sess), error) == NULL,
  857. ("failed with error `%s', no `%s'", ne_get_error(sess), error));
  858. if (!forever)
  859. ONV(any_request(sess, "/fail/to/connect") != NE_CONNECT,
  860. ("subsequent request re-used connection?"));
  861. ne_request_destroy(req);
  862. ne_session_destroy(sess);
  863. fail_request_last_port = port;
  864. return OK;
  865. }
  866. /* Run a random GET request which is given 'body' as the response; the
  867. * request must fail, and 'error' must be found in the error
  868. * string. */
  869. static int invalid_response_gives_error(const char *resp, const char *error)
  870. {
  871. return fail_request_with_error(0, single_serve_string, (void *)resp, 0, error);
  872. }
  873. /* Utility function: run a request using the given server fn, and the
  874. * request must fail. */
  875. static int fail_request(int with_body, server_fn fn, void *ud, int forever)
  876. {
  877. return fail_request_with_error(with_body, fn, ud, forever, NULL);
  878. }
  879. static int unbounded_headers(void)
  880. {
  881. struct infinite i = { RESP200, "x-foo: bar\r\n" };
  882. return fail_request(0, serve_infinite, &i, 0);
  883. }
  884. static int blank_response(void)
  885. {
  886. return fail_request(0, single_serve_string, "\r\n", 0);
  887. }
  888. static int serve_non_http(ne_socket *sock, void *ud)
  889. {
  890. SEND_STRING(sock, "Hello Mum.\n");
  891. ne_sock_readline(sock, buffer, BUFSIZ);
  892. return OK;
  893. }
  894. /* Test behaviour when not speaking to an HTTP server. Regression test
  895. * for infinite loop. */
  896. static int not_http(void)
  897. {
  898. return fail_request(0, serve_non_http, NULL, 0);
  899. }
  900. static int unbounded_folding(void)
  901. {
  902. struct infinite i = { "HTTP/1.0 200 OK\r\nFoo: bar\r\n",
  903. " hello there.\r\n" };
  904. return fail_request(0, serve_infinite, &i, 0);
  905. }
  906. static int serve_close(ne_socket *sock, void *ud)
  907. {
  908. /* do nothing; the socket will be closed. */
  909. return 0;
  910. }
  911. /* Returns non-zero if port is alive. */
  912. static int is_alive(int port)
  913. {
  914. ne_sock_addr *addr;
  915. ne_socket *sock = ne_sock_create();
  916. const ne_inet_addr *ia;
  917. int connected = 0;
  918. addr = ne_addr_resolve("localhost", 0);
  919. for (ia = ne_addr_first(addr); ia && !connected; ia = ne_addr_next(addr))
  920. connected = ne_sock_connect(sock, ia, port) == 0;
  921. ne_addr_destroy(addr);
  922. if (sock == NULL)
  923. return 0;
  924. else {
  925. ne_sock_close(sock);
  926. return 1;
  927. }
  928. }
  929. /* This is a regression test for neon 0.17.0 and earlier, which goes
  930. * into an infinite loop if a request with a body is sent to a server
  931. * which simply closes the connection. */
  932. static int closed_connection(void)
  933. {
  934. int ret;
  935. /* This spawns a server process which will run the 'serve_close'
  936. * response function 200 times, then die. This guarantees that the
  937. * request eventually fails... */
  938. CALL(fail_request(1, serve_close, NULL, 1));
  939. /* if server died -> infinite loop was detected. */
  940. ret = !is_alive(fail_request_last_port);
  941. reap_server();
  942. ONN("server aborted, infinite loop?", ret);
  943. return OK;
  944. }
  945. static int serve_close2(ne_socket *sock, void *userdata)
  946. {
  947. int *count = userdata;
  948. *count += 1;
  949. if (*count == 1)
  950. return 0;
  951. NE_DEBUG(NE_DBG_HTTP, "Re-entered! Buggy client.\n");
  952. CALL(discard_request(sock));
  953. CALL(SEND_STRING(sock, RESP200 "Content-Length: 0\r\n\r\n"));
  954. return 0;
  955. }
  956. /* As closed_connection(); but check that the client doesn't retry
  957. * after receiving the EOF on the first request down a new
  958. * connection. */
  959. static int close_not_retried(void)
  960. {
  961. int count = 0;
  962. ne_session *sess;
  963. unsigned int port;
  964. CALL(new_spawn_server(3, serve_close2, &count, &port));
  965. sess = ne_session_create("http", "localhost", port);
  966. ONN("request was retried after EOF", any_request(sess, "/foo") == NE_OK);
  967. reap_server();
  968. ne_session_destroy(sess);
  969. return OK;
  970. }
  971. static enum {
  972. prog_error, /* error */
  973. prog_transfer, /* doing a transfer */
  974. prog_done /* finished. */
  975. } prog_state = prog_transfer;
  976. static ne_off_t prog_last = -1, prog_total;
  977. #define FOFF "%" NE_FMT_NE_OFF_T
  978. /* callback for send_progress. */
  979. static void s_progress(void *userdata, ne_off_t prog, ne_off_t total)
  980. {
  981. NE_DEBUG(NE_DBG_HTTP,
  982. "progress callback: " FOFF "/" FOFF ".\n",
  983. prog, total);
  984. switch (prog_state) {
  985. case prog_error:
  986. case prog_done:
  987. return;
  988. case prog_transfer:
  989. if (total != prog_total) {
  990. t_context("total unexpected: " FOFF " not " FOFF "", total, prog_total);
  991. prog_state = prog_error;
  992. }
  993. else if (prog > total) {
  994. t_context("first progress was invalid (" FOFF "/" FOFF ")", prog, total);
  995. prog_state = prog_error;
  996. }
  997. else if (prog_last != -1 && prog_last > prog) {
  998. t_context("progress went backwards: " FOFF " to " FOFF, prog_last, prog);
  999. prog_state = prog_error;
  1000. }
  1001. else if (prog_last == prog) {
  1002. t_context("no progress made! " FOFF " to " FOFF, prog_last, prog);
  1003. prog_state = prog_error;
  1004. }
  1005. else if (prog == total) {
  1006. prog_state = prog_done;
  1007. }
  1008. break;
  1009. }
  1010. prog_last = prog;
  1011. }
  1012. #undef FOFF
  1013. static ssize_t provide_progress(void *userdata, char *buf, size_t bufsiz)
  1014. {
  1015. int *count = userdata;
  1016. if (*count >= 0 && buf != NULL) {
  1017. buf[0] = 'a';
  1018. *count -= 1;
  1019. return 1;
  1020. } else {
  1021. return 0;
  1022. }
  1023. }
  1024. static int send_progress(void)
  1025. {
  1026. static int count = 200;
  1027. ON(prepare_request(single_serve_string,
  1028. RESP200 "Connection: close\r\n\r\n"));
  1029. prog_total = 200;
  1030. ne_set_progress(def_sess, s_progress, NULL);
  1031. ne_set_request_body_provider(def_req, count,
  1032. provide_progress, &count);
  1033. #define sess def_sess
  1034. ONREQ(ne_request_dispatch(def_req));
  1035. #undef sess
  1036. ON(finish_request());
  1037. CALL(prog_state == prog_error);
  1038. return OK;
  1039. }
  1040. static int read_timeout(void)
  1041. {
  1042. ne_session *sess;
  1043. ne_request *req;
  1044. time_t start, finish;
  1045. int ret;
  1046. CALL(make_session(&sess, sleepy_server, NULL));
  1047. /* timeout after one second. */
  1048. ne_set_read_timeout(sess, 1);
  1049. req = ne_request_create(sess, "GET", "/timeout");
  1050. time(&start);
  1051. ret = ne_request_dispatch(req);
  1052. time(&finish);
  1053. reap_server();
  1054. ONN("request succeeded, should have timed out", ret == NE_OK);
  1055. ONV(ret != NE_TIMEOUT,
  1056. ("request failed non-timeout error: %s", ne_get_error(sess)));
  1057. ONN("timeout ignored, or very slow machine", finish - start > 3);
  1058. ne_request_destroy(req);
  1059. ne_session_destroy(sess);
  1060. return OK;
  1061. }
  1062. /* expect failure code 'code', for request to given hostname and port,
  1063. * without running a server. */
  1064. static int fail_noserver(const char *hostname, unsigned int port, int code)
  1065. {
  1066. ne_session *sess = ne_session_create("http", hostname, port);
  1067. int ret = any_request(sess, "/foo");
  1068. ne_session_destroy(sess);
  1069. ONV(ret == NE_OK,
  1070. ("request to server at %s:%u succeeded?!", hostname, port));
  1071. ONV(ret != code, ("request failed with %d not %d", ret, code));
  1072. return OK;
  1073. }
  1074. static int fail_lookup(void)
  1075. {
  1076. return fail_noserver("no.such.domain", 4242, NE_LOOKUP);
  1077. }
  1078. /* neon 0.23.0 to 0.23.3: if a nameserver lookup failed, subsequent
  1079. * requests on the session would crash. */
  1080. static int fail_double_lookup(void)
  1081. {
  1082. ne_session *sess = ne_session_create("http", "nonesuch.invalid", 80);
  1083. ONN("request did not give lookup failure",
  1084. any_request(sess, "/foo") != NE_LOOKUP);
  1085. ONN("second request did not give lookup failure",
  1086. any_request(sess, "/bar") != NE_LOOKUP);
  1087. ne_session_destroy(sess);
  1088. return OK;
  1089. }
  1090. static int fail_connect(void)
  1091. {
  1092. return fail_noserver("localhost", 32767, NE_CONNECT);
  1093. }
  1094. /* Test that the origin server hostname is NOT resolved for a proxied
  1095. * request. */
  1096. static int proxy_no_resolve(void)
  1097. {
  1098. ne_session *sess;
  1099. int ret;
  1100. CALL(proxied_session_server(&sess, "http", "no.such.server.invalid", 80,
  1101. single_serve_string,
  1102. RESP200 "Content-Length: 0\r\n\r\n"));
  1103. ret = any_request(sess, "/foo");
  1104. ONN("origin server name resolved when proxy used", ret == NE_LOOKUP);
  1105. return destroy_and_wait(sess);
  1106. }
  1107. /* If the chunk size is entirely invalid, the request should be
  1108. * aborted. Fails with neon <0.22; invalid chunk sizes would be
  1109. * silently treated as 'zero'. */
  1110. static int fail_chunksize(void)
  1111. {
  1112. return fail_request(0, single_serve_string,
  1113. RESP200 TE_CHUNKED "\r\n" "ZZZZZ\r\n\r\n", 0);
  1114. }
  1115. /* in neon <0.22, if an error occcurred whilst reading the response
  1116. * body, the connection would not be closed (though this test will
  1117. * succeed in neon <0.22 since it the previous test fails). */
  1118. static int abort_respbody(void)
  1119. {
  1120. ne_session *sess;
  1121. CALL(make_session(&sess, single_serve_string,
  1122. RESP200 TE_CHUNKED "\r\n"
  1123. "zzz\r\n"
  1124. RESP200 "Content-Length: 0\r\n\r\n"));
  1125. /* connection must be aborted on the first request, since it
  1126. * contains an invalid chunk size. */
  1127. ONN("invalid chunk size was accepted?",
  1128. any_request(sess, "/foo") != NE_ERROR);
  1129. ne_close_connection(sess);
  1130. CALL(await_server());
  1131. /* second request should fail since server has gone away. */
  1132. ONN("connection was not aborted", any_request(sess, "/foo") == NE_OK);
  1133. ne_session_destroy(sess);
  1134. return OK;
  1135. }
  1136. static int serve_then_abort(ne_socket *sock, void *ud)
  1137. {
  1138. int *flag = ud;
  1139. if (*flag == 1) {
  1140. CALL(single_serve_string(sock,
  1141. RESP200 "Content-Length: 0\r\n\r\n"
  1142. RESP200 TE_CHUNKED "\r\n"
  1143. "zzzzz\r\n"));
  1144. *flag = 0;
  1145. }
  1146. exit(0);
  1147. }
  1148. /* Test that after an aborted request on a persistent connection, a
  1149. * failure of the *subsequent* request is not treated as a persistent
  1150. * connection timeout and retried. */
  1151. static int retry_after_abort(void)
  1152. {
  1153. ne_session *sess;
  1154. int flag = 1;
  1155. /* Serve two responses down a single persistent connection, the
  1156. * second of which is invalid and will cause the request to be
  1157. * aborted. */
  1158. CALL(make_session(&sess, serve_then_abort, &flag));
  1159. ONREQ(any_request(sess, "/first"));
  1160. ONN("second request should fail", any_request(sess, "/second") == NE_OK);
  1161. ne_close_connection(sess);
  1162. CALL(await_server());
  1163. /* A third attempt to connect to the server should fail to
  1164. * connect, though this is racy since someone else might come
  1165. * along and steal the port... oh well. */
  1166. ONN("third request was retried",
  1167. any_request(sess, "/third") != NE_CONNECT);
  1168. ne_session_destroy(sess);
  1169. return OK;
  1170. }
  1171. /* Fail to parse the response status line: check the error message is
  1172. * sane. Failed during 0.23-dev briefly, and possibly with 0.22.0
  1173. * too. */
  1174. static int fail_statusline(void)
  1175. {
  1176. ne_session *sess;
  1177. int ret;
  1178. CALL(make_session(&sess, single_serve_string, "Fish.\r\n"));
  1179. ret = any_request(sess, "/fail");
  1180. ONV(ret != NE_ERROR, ("request failed with %d not NE_ERROR", ret));
  1181. ne_close_connection(sess);
  1182. ONV(strstr(ne_get_error(sess),
  1183. "Could not parse response status line") == NULL,
  1184. ("session error was `%s'", ne_get_error(sess)));
  1185. ne_session_destroy(sess);
  1186. return OK;
  1187. }
  1188. #define LEN (9000)
  1189. static int fail_long_header(void)
  1190. {
  1191. char resp[LEN + 500] = "HTTP/1.1 200 OK\r\n"
  1192. "Server: fish\r\n";
  1193. size_t len = strlen(resp);
  1194. /* add a long header */
  1195. memset(resp + len, 'a', LEN);
  1196. resp[len + LEN] = '\0';
  1197. strcat(resp, "\r\n\r\n");
  1198. return invalid_response_gives_error(resp, "Line too long");
  1199. }
  1200. static int fail_on_invalid(void)
  1201. {
  1202. static const struct {
  1203. const char *resp, *error;
  1204. } ts[] = {
  1205. /* non-chunked TE. */
  1206. { RESP200 "transfer-encoding: punked\r\n" "\r\n" ABCDE_CHUNKS ,
  1207. "Unknown transfer-coding" },
  1208. /* chunk without trailing CRLF */
  1209. { RESP200 TE_CHUNKED "\r\n" "5\r\n" "abcdeFISH",
  1210. "delimiter was invalid" },
  1211. /* chunk with CR then EOF */
  1212. { RESP200 TE_CHUNKED "\r\n" "5\r\n" "abcde\n",
  1213. "not read chunk delimiter" },
  1214. /* chunk with CR then notLF */
  1215. { RESP200 TE_CHUNKED "\r\n" "5\r\n" "abcde\rZZZ",
  1216. "delimiter was invalid" },
  1217. /* chunk size overflow */
  1218. { RESP200 TE_CHUNKED "\r\n" "800000000\r\n" "abcde\r\n",
  1219. "Could not parse chunk size" },
  1220. /* EOF at chunk size */
  1221. { RESP200 TE_CHUNKED "\r\n", "Could not read chunk size" },
  1222. /* negative C-L */
  1223. { RESP200 "Content-Length: -1\r\n" "\r\n" "abcde",
  1224. "Invalid Content-Length" },
  1225. /* invalid C-Ls */
  1226. { RESP200 "Content-Length: 5, 3\r\n" "\r\n" "abcde",
  1227. "Invalid Content-Length" },
  1228. { RESP200 "Content-Length: 5z\r\n" "\r\n" "abcde",
  1229. "Invalid Content-Length" },
  1230. { RESP200 "Content-Length: z5\r\n" "\r\n" "abcde",
  1231. "Invalid Content-Length" },
  1232. /* stupidly-large C-L */
  1233. { RESP200 "Content-Length: 99999999999999999999999999\r\n"
  1234. "\r\n" "abcde",
  1235. "Invalid Content-Length" },
  1236. { NULL, NULL }
  1237. };
  1238. int n;
  1239. for (n = 0; ts[n].resp; n++)
  1240. CALL(invalid_response_gives_error(ts[n].resp, ts[n].error));
  1241. return OK;
  1242. }
  1243. static int versions(void)
  1244. {
  1245. ne_session *sess;
  1246. CALL(make_session(&sess, single_serve_string,
  1247. "HTTP/1.1 200 OK\r\n"
  1248. "Content-Length: 0\r\n\r\n"
  1249. "HTTP/1.0 200 OK\r\n"
  1250. "Content-Length: 0\r\n\r\n"));
  1251. ONREQ(any_request(sess, "/http11"));
  1252. ONN("did not detect HTTP/1.1 compliance",
  1253. ne_version_pre_http11(sess) != 0);
  1254. ONREQ(any_request(sess, "/http10"));
  1255. ONN("did not detect lack of HTTP/1.1 compliance",
  1256. ne_version_pre_http11(sess) == 0);
  1257. return destroy_and_wait(sess);
  1258. }
  1259. struct cr_args {
  1260. const char *method, *uri;
  1261. int result;
  1262. };
  1263. static void hk_createreq(ne_request *req, void *userdata,
  1264. const char *method, const char *requri)
  1265. {
  1266. struct cr_args *args = userdata;
  1267. args->result = 1; /* presume failure */
  1268. if (strcmp(args->method, method))
  1269. t_context("Hook got method %s not %s", method, args->method);
  1270. else if (strcmp(args->uri, requri))
  1271. t_context("Hook got Req-URI %s not %s", requri, args->uri);
  1272. else
  1273. args->result = 0;
  1274. }
  1275. static int hook_create_req(void)
  1276. {
  1277. ne_session *sess;
  1278. struct cr_args args;
  1279. ne_uri uri;
  1280. char *u;
  1281. CALL(make_session(&sess, single_serve_string, EMPTY_RESP EMPTY_RESP));
  1282. ne_hook_create_request(sess, hk_createreq, &args);
  1283. args.method = "GET";
  1284. args.uri = "/foo";
  1285. args.result = -1;
  1286. ONREQ(any_request(sess, "/foo"));
  1287. ONN("first hook never called", args.result == -1);
  1288. if (args.result) return FAIL;
  1289. memset(&uri, 0, sizeof uri);
  1290. ne_fill_server_uri(sess, &uri);
  1291. uri.path = "/bar";
  1292. args.uri = u = ne_uri_unparse(&uri);
  1293. args.result = -1;
  1294. ne_free(uri.host);
  1295. ne_free(uri.scheme);
  1296. /* force use of absoluteURI in request-uri */
  1297. ne_session_proxy(sess, "localhost", 7777);
  1298. ONREQ(any_request(sess, "/bar"));
  1299. ONN("second hook never called", args.result == -1);
  1300. if (args.result) return FAIL;
  1301. ne_free(u);
  1302. return destroy_and_wait(sess);
  1303. }
  1304. static int serve_check_method(ne_socket *sock, void *ud)
  1305. {
  1306. char *method = ud;
  1307. char buf[20];
  1308. size_t methlen = strlen(method);
  1309. if (ne_sock_read(sock, buf, methlen) != (ssize_t)methlen)
  1310. return -1;
  1311. ONN("method corrupted", memcmp(buf, method, methlen));
  1312. return single_serve_string(sock, "HTTP/1.1 204 OK\r\n\r\n");
  1313. }
  1314. /* Test that the method string passed to ne_request_create is
  1315. * strdup'ed. */
  1316. static int dup_method(void)
  1317. {
  1318. char method[] = "FOO";
  1319. ne_session *sess;
  1320. ne_request *req;
  1321. CALL(make_session(&sess, serve_check_method, method));
  1322. req = ne_request_create(sess, method, "/bar");
  1323. strcpy(method, "ZZZ");
  1324. ONREQ(ne_request_dispatch(req));
  1325. ne_request_destroy(req);
  1326. return destroy_and_wait(sess);
  1327. }
  1328. static int abortive_reader(void *userdata, const char *buf, size_t len)
  1329. {
  1330. ne_session *sess = userdata;
  1331. if (len == 5 && strncmp(buf, "abcde", 5) == 0) {
  1332. ne_set_error(sess, "Reader callback failed");
  1333. } else {
  1334. ne_set_error(sess, "Reader callback called with length %" NE_FMT_SIZE_T,
  1335. len);
  1336. }
  1337. return NE_ERROR;
  1338. }
  1339. static int abort_reader(void)
  1340. {
  1341. ne_session *sess;
  1342. ne_request *req;
  1343. int ret;
  1344. CALL(make_session(&sess, single_serve_string,
  1345. RESP200 "Content-Length: 5\r\n\r\n"
  1346. "abcde"
  1347. "HTTP/1.1 200 OK\r\n"
  1348. "Content-Length: 0\r\n\r\n"));
  1349. req = ne_request_create(sess, "GET", "/foo");
  1350. ne_add_response_body_reader(req, ne_accept_2xx, abortive_reader, sess);
  1351. ret = ne_request_dispatch(req);
  1352. ONV(ret != NE_ERROR, ("request did not fail with NE_ERROR: %d", ret));
  1353. ONV(strcmp(ne_get_error(sess), "Reader callback failed") != 0,
  1354. ("unexpected session error string: %s", ne_get_error(sess)));
  1355. ne_request_destroy(req);
  1356. /* test that the connection was closed. */
  1357. ONN("connection not closed after aborted response",
  1358. any_2xx_request(sess, "/failmeplease") == OK);
  1359. return destroy_and_wait(sess);
  1360. }
  1361. /* attempt and fail to send request from offset 500 of /dev/null. */
  1362. static int send_bad_offset(void)
  1363. {
  1364. ne_session *sess;
  1365. ne_request *req;
  1366. int ret, fds[2];
  1367. CALL(make_session(&sess, single_serve_string,
  1368. RESP200 "Content-Length: 0\r\n" "\r\n"));
  1369. /* create a pipe, on which seek is guaranteed to fail. */
  1370. ONN("could not create pipe", pipe(fds) != 0);
  1371. req = ne_request_create(sess, "PUT", "/null");
  1372. ne_set_request_body_fd(req, fds[0], 500, 5);
  1373. ret = ne_request_dispatch(req);
  1374. close(fds[0]);
  1375. close(fds[1]);
  1376. ONN("request dispatched with bad offset!", ret == NE_OK);
  1377. ONV(ret != NE_ERROR,
  1378. ("request failed with unexpected error code %d: %s",
  1379. ret, ne_get_error(sess)));
  1380. ONV(strstr(ne_get_error(sess), "Could not seek") == NULL,
  1381. ("bad error message from seek failure: %s", ne_get_error(sess)));
  1382. reap_server();
  1383. ne_request_destroy(req);
  1384. ne_session_destroy(sess);
  1385. return OK;
  1386. }
  1387. static void thook_create_req(ne_request *req, void *userdata,
  1388. const char *method, const char *requri)
  1389. {
  1390. ne_buffer *buf = userdata;
  1391. ne_buffer_concat(buf, "(create,", method, ",", requri, ")\n", NULL);
  1392. }
  1393. static void hook_pre_send(ne_request *req, void *userdata,
  1394. ne_buffer *header)
  1395. {
  1396. ne_buffer *buf = userdata;
  1397. ne_buffer_czappend(buf, "(pre-send)\n");
  1398. }
  1399. /* Returns a static string giving a comma-separated representation of
  1400. * the status structure passed in. */
  1401. static char *status_to_string(const ne_status *status)
  1402. {
  1403. static char sbuf[128];
  1404. ne_snprintf(sbuf, sizeof sbuf, "HTTP/%d.%d,%d,%s",
  1405. status->major_version, status->minor_version,
  1406. status->code, status->reason_phrase);
  1407. return sbuf;
  1408. }
  1409. static void hook_post_headers(ne_request *req, void *userdata,
  1410. const ne_status *status)
  1411. {
  1412. ne_buffer *buf = userdata;
  1413. ne_buffer_concat(buf, "(post-headers,", status_to_string(status), ")\n",
  1414. NULL);
  1415. }
  1416. static int hook_post_send(ne_request *req, void *userdata,
  1417. const ne_status *status)
  1418. {
  1419. ne_buffer *buf = userdata;
  1420. ne_buffer_concat(buf, "(post-send,", status_to_string(status), ")\n",
  1421. NULL);
  1422. return NE_OK;
  1423. }
  1424. static void hook_destroy_req(ne_request *req, void *userdata)
  1425. {
  1426. ne_buffer *buf = userdata;
  1427. ne_buffer_czappend(buf, "(destroy-req)\n");
  1428. }
  1429. static void hook_destroy_sess(void *userdata)
  1430. {
  1431. ne_buffer *buf = userdata;
  1432. ne_buffer_czappend(buf, "(destroy-sess)\n");
  1433. }
  1434. static void hook_close_conn(void *userdata)
  1435. {
  1436. ne_buffer *buf = userdata;
  1437. ne_buffer_czappend(buf, "(close-conn)\n");
  1438. }
  1439. static int hooks(void)
  1440. {
  1441. ne_buffer *buf = ne_buffer_create();
  1442. ne_session *sess;
  1443. struct many_serve_args args;
  1444. args.str = RESP200 "Content-Length: 0\r\n" "\r\n";
  1445. args.count = 3;
  1446. CALL(make_session(&sess, many_serve_string, &args));
  1447. ne_hook_create_request(sess, thook_create_req, buf);
  1448. ne_hook_pre_send(sess, hook_pre_send, buf);
  1449. ne_hook_post_headers(sess, hook_post_headers, buf);
  1450. ne_hook_post_send(sess, hook_post_send, buf);
  1451. ne_hook_destroy_request(sess, hook_destroy_req, buf);
  1452. ne_hook_destroy_session(sess, hook_destroy_sess, buf);
  1453. ne_hook_close_conn(sess, hook_close_conn, buf);
  1454. CALL(any_2xx_request(sess, "/first"));
  1455. ONCMP("(create,GET,/first)\n"
  1456. "(pre-send)\n"
  1457. "(post-headers,HTTP/1.1,200,OK)\n"
  1458. "(post-send,HTTP/1.1,200,OK)\n"
  1459. "(destroy-req)\n", buf->data, "hook ordering", "first result");
  1460. ne_buffer_clear(buf);
  1461. /* Unhook for mismatched fn/ud pointers: */
  1462. ne_unhook_create_request(sess, hk_createreq, buf);
  1463. ne_unhook_create_request(sess, thook_create_req, sess);
  1464. /* Unhook real functions. */
  1465. ne_unhook_pre_send(sess, hook_pre_send, buf);
  1466. ne_unhook_destroy_request(sess, hook_destroy_req, buf);
  1467. ne_unhook_post_headers(sess, hook_post_headers, buf);
  1468. CALL(any_2xx_request(sess, "/second"));
  1469. ONCMP("(create,GET,/second)\n"
  1470. "(post-send,HTTP/1.1,200,OK)\n",
  1471. buf->data, "hook ordering", "second result");
  1472. ne_buffer_clear(buf);
  1473. /* Double hook create, double hook then double unhook post. */
  1474. ne_hook_create_request(sess, thook_create_req, buf);
  1475. ne_hook_post_send(sess, hook_post_send, buf);
  1476. ne_unhook_post_send(sess, hook_post_send, buf);
  1477. ne_unhook_post_send(sess, hook_post_send, buf);
  1478. CALL(any_2xx_request(sess, "/third"));
  1479. ONCMP("(create,GET,/third)\n"
  1480. "(create,GET,/third)\n",
  1481. buf->data, "hook ordering", "third result");
  1482. ne_buffer_clear(buf);
  1483. ne_session_destroy(sess);
  1484. CALL(await_server());
  1485. ONCMP("(destroy-sess)\n"
  1486. "(close-conn)\n", buf->data, "hook ordering", "first destroyed session");
  1487. ne_buffer_clear(buf);
  1488. sess = ne_session_create("http", "www.example.com", 80);
  1489. ne_hook_destroy_session(sess, hook_destroy_sess, buf);
  1490. ne_unhook_destroy_session(sess, hook_destroy_sess, buf);
  1491. ne_session_destroy(sess);
  1492. ONCMP("", buf->data, "hook ordering", "second destroyed session");
  1493. ne_buffer_destroy(buf);
  1494. return OK;
  1495. }
  1496. static void hook_self_destroy_req(ne_request *req, void *userdata)
  1497. {
  1498. ne_unhook_destroy_request(ne_get_session(req),
  1499. hook_self_destroy_req, userdata);
  1500. }
  1501. /* Test that it's safe to call ne_unhook_destroy_request from a
  1502. * destroy_request hook. */
  1503. static int hook_self_destroy(void)
  1504. {
  1505. ne_session *sess = ne_session_create("http", "localhost", 1234);
  1506. ne_hook_destroy_request(sess, hook_self_destroy_req, NULL);
  1507. ne_request_destroy(ne_request_create(sess, "GET", "/"));
  1508. ne_session_destroy(sess);
  1509. return OK;
  1510. }
  1511. static int icy_protocol(void)
  1512. {
  1513. ne_session *sess;
  1514. CALL(make_session(&sess, single_serve_string,
  1515. "ICY 200 OK\r\n"
  1516. "Content-Length: 0\r\n\r\n"));
  1517. ne_set_session_flag(sess, NE_SESSFLAG_ICYPROTO, 1);
  1518. ONREQ(any_request(sess, "/foo"));
  1519. return destroy_and_wait(sess);
  1520. }
  1521. static void status_cb(void *userdata, ne_session_status status,
  1522. const ne_session_status_info *info)
  1523. {
  1524. ne_buffer *buf = userdata;
  1525. char scratch[512];
  1526. switch (status) {
  1527. case ne_status_lookup:
  1528. ne_buffer_concat(buf, "lookup(", info->lu.hostname, ")-", NULL);
  1529. break;
  1530. case ne_status_connecting:
  1531. ne_iaddr_print(info->ci.address, scratch, sizeof scratch);
  1532. ne_buffer_concat(buf, "connecting(", info->lu.hostname,
  1533. ",", scratch, ")-", NULL);
  1534. break;
  1535. case ne_status_disconnected:
  1536. ne_buffer_czappend(buf, "dis");
  1537. /* fallthrough */
  1538. case ne_status_connected:
  1539. ne_buffer_concat(buf, "connected(", info->cd.hostname,
  1540. ")-", NULL);
  1541. break;
  1542. case ne_status_sending:
  1543. case ne_status_recving:
  1544. ne_snprintf(scratch, sizeof scratch,
  1545. "%" NE_FMT_NE_OFF_T ",%" NE_FMT_NE_OFF_T,
  1546. info->sr.progress, info->sr.total);
  1547. ne_buffer_concat(buf,
  1548. status == ne_status_sending ? "send" : "recv",
  1549. "(", scratch, ")-", NULL);
  1550. break;
  1551. default:
  1552. ne_buffer_czappend(buf, "bork!");
  1553. break;
  1554. }
  1555. }
  1556. static int status(void)
  1557. {
  1558. ne_session *sess;
  1559. ne_buffer *buf = ne_buffer_create();
  1560. const char *host, *addr = get_lh_addr();
  1561. char expect[1024];
  1562. CALL(make_session(&sess, single_serve_string, RESP200
  1563. "Content-Length: 5\r\n\r\n" "abcde"));
  1564. host = get_session_host();
  1565. ne_snprintf(expect, sizeof expect,
  1566. "connecting(%s,%s)-"
  1567. "connected(%s)-"
  1568. "send(0,5000)-"
  1569. "send(5000,5000)-"
  1570. "recv(0,5)-"
  1571. "recv(5,5)-"
  1572. "disconnected(%s)-",
  1573. host, addr, host, host);
  1574. ne_set_notifier(sess, status_cb, buf);
  1575. CALL(any_2xx_request_body(sess, "/status"));
  1576. CALL(destroy_and_wait(sess));
  1577. ONV(strcmp(expect, buf->data),
  1578. ("status event sequence mismatch: got [%s] not [%s]",
  1579. buf->data, expect));
  1580. ne_buffer_destroy(buf);
  1581. return OK;
  1582. }
  1583. static int status_chunked(void)
  1584. {
  1585. ne_session *sess;
  1586. ne_buffer *buf = ne_buffer_create();
  1587. const char *host, *addr = get_lh_addr();
  1588. char expect[1024];
  1589. CALL(make_session(&sess, single_serve_string,
  1590. RESP200 TE_CHUNKED "\r\n" ABCDE_CHUNKS));
  1591. host = get_session_host();
  1592. /* This sequence is not exactly guaranteed by the API, but it's
  1593. * what the current implementation should do. */
  1594. ne_snprintf(expect, sizeof expect,
  1595. "connecting(%s,%s)-"
  1596. "connected(%s)-"
  1597. "send(0,5000)-"
  1598. "send(5000,5000)-"
  1599. "recv(0,-1)-"
  1600. "recv(1,-1)-"
  1601. "recv(2,-1)-"
  1602. "recv(3,-1)-"
  1603. "recv(4,-1)-"
  1604. "recv(5,-1)-"
  1605. "disconnected(%s)-",
  1606. host, addr, host, host);
  1607. ne_set_notifier(sess, status_cb, buf);
  1608. CALL(any_2xx_request_body(sess, "/status"));
  1609. CALL(destroy_and_wait(sess));
  1610. ONV(strcmp(expect, buf->data),
  1611. ("status event sequence mismatch: got [%s] not [%s]",
  1612. buf->data, expect));
  1613. ne_buffer_destroy(buf);
  1614. return OK;
  1615. }
  1616. static int local_addr(void)
  1617. {
  1618. ne_session *sess;
  1619. ne_inet_addr *ia = get_lh_inet_addr();
  1620. CALL(make_session(&sess, single_serve_string, RESP200
  1621. "Connection: close\r\n\r\n"));
  1622. ne_set_localaddr(sess, ia);
  1623. ONREQ(any_request(sess, "/foo"));
  1624. ne_session_destroy(sess);
  1625. ne_iaddr_free(ia);
  1626. return reap_server();
  1627. }
  1628. /* Regression in 0.27.0, ne_set_progress(sess, NULL, NULL) should
  1629. * register the progress callback. */
  1630. static int dereg_progress(void)
  1631. {
  1632. ne_session *sess;
  1633. CALL(make_session(&sess, single_serve_string,
  1634. RESP200 TE_CHUNKED "\r\n" ABCDE_CHUNKS));
  1635. ne_set_progress(sess, NULL, NULL);
  1636. ONREQ(any_request(sess, "/foo"));
  1637. return destroy_and_wait(sess);
  1638. }
  1639. static int addrlist(void)
  1640. {
  1641. ne_session *sess;
  1642. ne_inet_addr *ia = get_lh_inet_addr();
  1643. const ne_inet_addr *ial[1];
  1644. unsigned int port;
  1645. CALL(new_spawn_server(1, single_serve_string, EMPTY_RESP, &port));
  1646. sess = ne_session_create("http", "www.example.com", port);
  1647. ial[0] = ia;
  1648. ne_set_addrlist(sess, ial, 1);
  1649. CALL(any_2xx_request(sess, "/blah"));
  1650. ne_iaddr_free(ia);
  1651. return destroy_and_wait(sess);
  1652. }
  1653. static int socks_session(ne_session **sess, struct socks_server *srv,
  1654. const char *hostname, unsigned int port,
  1655. server_fn server, void *userdata)
  1656. {
  1657. unsigned int realport;
  1658. srv->server = server;
  1659. srv->userdata = userdata;
  1660. CALL(new_spawn_server(1, socks_server, srv, &realport));
  1661. *sess = ne_session_create("http", hostname, port);
  1662. ne_session_socks_proxy(*sess, srv->version, "localhost", realport,
  1663. srv->username, srv->password);
  1664. return OK;
  1665. }
  1666. static int socks_proxy(void)
  1667. {
  1668. ne_session *sess;
  1669. struct socks_server srv = {0};
  1670. srv.version = NE_SOCK_SOCKSV5;
  1671. srv.failure = fail_none;
  1672. srv.expect_port = 4242;
  1673. srv.expect_addr = NULL;
  1674. srv.expect_fqdn = "socks.example.com";
  1675. srv.username = "bloggs";
  1676. srv.password = "guessme";
  1677. CALL(socks_session(&sess, &srv, srv.expect_fqdn, srv.expect_port,
  1678. single_serve_string, EMPTY_RESP));
  1679. CALL(any_2xx_request(sess, "/blee"));
  1680. return destroy_and_wait(sess);
  1681. }
  1682. static int socks_v4_proxy(void)
  1683. {
  1684. ne_session *sess;
  1685. struct socks_server srv = {0};
  1686. srv.version = NE_SOCK_SOCKSV4;
  1687. srv.failure = fail_none;
  1688. srv.expect_port = 4242;
  1689. srv.expect_addr = ne_iaddr_parse("127.0.0.1", ne_iaddr_ipv4);
  1690. srv.expect_fqdn = "localhost";
  1691. srv.username = "bloggs";
  1692. srv.password = "guessme";
  1693. CALL(socks_session(&sess, &srv, srv.expect_fqdn, srv.expect_port,
  1694. single_serve_string, EMPTY_RESP));
  1695. CALL(any_2xx_request(sess, "/blee"));
  1696. ne_iaddr_free(srv.expect_addr);
  1697. return destroy_and_wait(sess);
  1698. }
  1699. /* Server function which serves the request body back as the response
  1700. * body. */
  1701. static int serve_mirror(ne_socket *sock, void *userdata)
  1702. {
  1703. char response[1024];
  1704. CALL(discard_request(sock));
  1705. ONV(clength == 0 || (size_t)clength > sizeof buffer,
  1706. ("C-L out of bounds: %d", clength));
  1707. ONV(ne_sock_fullread(sock, buffer, clength),
  1708. ("read failed: %s", ne_sock_error(sock)));
  1709. ne_snprintf(response, sizeof response,
  1710. "HTTP/1.0 200 OK\r\n"
  1711. "Content-Length: %d\r\n"
  1712. "\r\n", clength);
  1713. ONN("send response header failed",
  1714. server_send(sock, response, strlen(response)));
  1715. ONN("send response body failed",
  1716. server_send(sock, buffer, clength));
  1717. ONV(ne_sock_read(sock, buffer, 1) != NE_SOCK_CLOSED,
  1718. ("client sent data after request: %c", buffer[0]));
  1719. return OK;
  1720. }
  1721. /* Test for ne_set_request_body_fd() bug in <= 0.29.3. */
  1722. static int send_length(void)
  1723. {
  1724. ne_session *sess;
  1725. ne_request *req;
  1726. int fd;
  1727. ne_buffer *buf = ne_buffer_create();
  1728. fd = open("foobar.txt", O_RDONLY);
  1729. ONV(fd < 0, ("open random.txt failed: %s", strerror(errno)));
  1730. CALL(make_session(&sess, serve_mirror, NULL));
  1731. req = ne_request_create(sess, "GET", "/foo");
  1732. ne_set_request_body_fd(req, fd, 0, 3);
  1733. ne_add_response_body_reader(req, ne_accept_2xx, collector, buf);
  1734. ONREQ(ne_request_dispatch(req));
  1735. ONCMP("foo", buf->data, "response body", "match");
  1736. ne_request_destroy(req);
  1737. ne_buffer_destroy(buf);
  1738. close(fd);
  1739. return destroy_and_wait(sess);
  1740. }
  1741. /* Test for error code for a SOCKS proxy failure, bug in <= 0.29.3. */
  1742. static int socks_fail(void)
  1743. {
  1744. ne_session *sess;
  1745. struct socks_server srv = {0};
  1746. int ret;
  1747. srv.version = NE_SOCK_SOCKSV5;
  1748. srv.failure = fail_init_vers;
  1749. srv.expect_port = 4242;
  1750. srv.expect_addr = ne_iaddr_parse("127.0.0.1", ne_iaddr_ipv4);
  1751. srv.expect_fqdn = "localhost";
  1752. srv.username = "bloggs";
  1753. srv.password = "guessme";
  1754. CALL(socks_session(&sess, &srv, srv.expect_fqdn, srv.expect_port,
  1755. single_serve_string, EMPTY_RESP));
  1756. ret = any_request(sess, "/blee");
  1757. ONV(ret != NE_ERROR,
  1758. ("request failed with %d not NE_ERROR", ret));
  1759. ONV(strstr(ne_get_error(sess),
  1760. "Could not establish connection from SOCKS proxy") == NULL
  1761. || strstr(ne_get_error(sess),
  1762. "Invalid version in proxy response") == NULL,
  1763. ("unexpected error string: %s", ne_get_error(sess)));
  1764. ne_iaddr_free(srv.expect_addr);
  1765. return destroy_and_wait(sess);
  1766. }
  1767. static int safe_flags(void)
  1768. {
  1769. ne_session *sess = ne_session_create("http", "localhost", 80);
  1770. ne_request *req = ne_request_create(sess, "GET", "/");
  1771. ne_set_request_flag(req, NE_REQFLAG_LAST, 0xAAAAAAAA);
  1772. ONN("flags array bound check failed", ne_get_session(req) != sess);
  1773. ne_request_destroy(req);
  1774. ne_session_destroy(sess);
  1775. return OK;
  1776. }
  1777. /* Hit the timeout (2 seconds) for reading interim responses. */
  1778. static int fail_excess_1xx(void)
  1779. {
  1780. struct s1xx_args args = {20000000, 0};
  1781. return fail_request_with_error(0, serve_1xx, &args, 0,
  1782. "Timed out reading interim responses");
  1783. }
  1784. static int serve_check_reqline(ne_socket *sock, void *userdata)
  1785. {
  1786. const char *expect = userdata;
  1787. ONN("failed to read request-line",
  1788. ne_sock_readline(sock, buffer, BUFSIZ) <= 0);
  1789. NE_DEBUG(NE_DBG_HTTP, "child: got request-line %s\n", buffer);
  1790. ONCMPN(expect, buffer, "request-line", "check for absolute URI");
  1791. return single_serve_string(sock, RESP200 "Connection: close\r\n\r\n");
  1792. }
  1793. /* Test that various request target forms are allowed. */
  1794. static int target_forms(void)
  1795. {
  1796. static const struct {
  1797. const char *uri, *reqline;
  1798. } ts[] = {
  1799. { "/foo", "GET /foo HTTP/1.1\r\n" },
  1800. { "/foo?bar", "GET /foo?bar HTTP/1.1\r\n" },
  1801. { "ftp://www.example.com/foo", "GET ftp://www.example.com/foo HTTP/1.1\r\n" },
  1802. { "*", "GET * HTTP/1.1\r\n" },
  1803. { NULL, NULL }
  1804. };
  1805. unsigned n;
  1806. for (n = 0; ts[n].uri; n++) {
  1807. ne_session *sess;
  1808. CALL(make_session(&sess, serve_check_reqline, (void *)ts[n].reqline));
  1809. ONREQ(any_request(sess, ts[n].uri));
  1810. CALL(destroy_and_wait(sess));
  1811. }
  1812. return OK;
  1813. }
  1814. /* Interim response callback. */
  1815. static void test_interim(void *userdata, ne_request *req, const ne_status *st)
  1816. {
  1817. ne_buffer *buf = userdata;
  1818. const char *val;
  1819. ne_buffer_snprintf(buf, 15, "[code=%d]", st->code);
  1820. val = ne_get_response_header(req, "X-Interim");
  1821. ne_buffer_concat(buf, "[hdr=", val ? val : "null", "]", NULL);
  1822. }
  1823. /* Test for interim response headers. */
  1824. static int interims(void)
  1825. {
  1826. ne_session *sess;
  1827. ne_request *req;
  1828. ne_buffer *buf = ne_buffer_create();
  1829. CALL(make_session(&sess, single_serve_string,
  1830. "HTTP/1.1 103 Early Stuff\r\n"
  1831. "X-Interim: foobar\r\n" "\r\n"
  1832. "HTTP/1.1 102 Stuff Is Happening\r\n"
  1833. "X-Interim: bar, foo\r\n" "\r\n"
  1834. "HTTP/1.1 200 Thank you kindly\r\n"
  1835. "Content-Length: 0\r\n"
  1836. "X-Not-Interim: finally\r\n" "\r\n"));
  1837. req = ne_request_create(sess, "GET", "/");
  1838. ne_add_interim_handler(req, test_interim, buf);
  1839. ONREQ(ne_request_dispatch(req));
  1840. ONN("interim header leaked to final response",
  1841. ne_get_response_header(req, "X-Interim") != NULL);
  1842. ne_request_destroy(req);
  1843. CALL(destroy_and_wait(sess));
  1844. ONCMPN("[code=103][hdr=foobar][code=102][hdr=bar, foo]", buf->data,
  1845. "interim responses", "trace");
  1846. ne_buffer_destroy(buf);
  1847. return OK;
  1848. }
  1849. static int retry_408(void)
  1850. {
  1851. ne_session *sess;
  1852. /* Serve two responses down a single persistent connection, the
  1853. * second of which is invalid and will cause the request to be
  1854. * aborted. */
  1855. CALL(multi_session_server(&sess, "http", "localhost",
  1856. 2, single_serve_string,
  1857. EMPTY_RESP
  1858. "HTTP/1.1 408 Request Timeout\r\n"
  1859. "Server: neon-test-server\r\n"
  1860. "Content-Length: 0\r\n\r\n"));
  1861. CALL(any_2xx_request(sess, "/first408"));
  1862. CALL(any_2xx_request(sess, "/second408"));
  1863. return destroy_and_wait(sess);
  1864. }
  1865. /* Ensure that only a 408 on a persisted connection is
  1866. * retried. Otherwise it should just be handled as a final
  1867. * response. */
  1868. static int dont_retry_408(void)
  1869. {
  1870. ne_session *sess;
  1871. CALL(make_session(&sess, single_serve_string,
  1872. "HTTP/1.1 408 Request Timeout\r\n"
  1873. "Server: neon-test-server\r\n"
  1874. "Content-Length: 0\r\n\r\n"));
  1875. /* Run any request, ensure it gets a 408 response. */
  1876. CALL(run_request(sess, 408, construct_get, NULL));
  1877. return destroy_and_wait(sess);
  1878. }
  1879. static const char *ipv6_check_result = "500 Something Went Wrong";
  1880. #define V6_EXAMPLE_HOST "[2001:db8::cafe]"
  1881. static void ipv6_host_checker(char *value)
  1882. {
  1883. NE_DEBUG(NE_DBG_HTTP, "child: header value [%s]\n", value);
  1884. if (strcmp(value, V6_EXAMPLE_HOST) == 0)
  1885. ipv6_check_result = "200 Good Header";
  1886. else
  1887. ipv6_check_result = "400 Bad Header";
  1888. }
  1889. /* Server which acts as a proxy accepting a CONNECT request. */
  1890. static int serve_v6_check(ne_socket *sock, void *ud)
  1891. {
  1892. char resp[512];
  1893. got_header = ipv6_host_checker;
  1894. want_header = "Host";
  1895. CALL(discard_request(sock));
  1896. ne_snprintf(resp, sizeof resp, "HTTP/1.1 %s\r\n"
  1897. "Content-Length: 0\r\n"
  1898. "Server: serve_v6_check\r\n\r\n",
  1899. ipv6_check_result);
  1900. SEND_STRING(sock, resp);
  1901. return 0;
  1902. }
  1903. static int ipv6_literal(void)
  1904. {
  1905. ne_session *sess;
  1906. CALL(fakeproxied_session_server(&sess, "http", V6_EXAMPLE_HOST,
  1907. 80, serve_v6_check,
  1908. RESP200 "Content-Length: 2\r\n"
  1909. "ok"));
  1910. CALL(any_2xx_request(sess, "/ipv6ish"));
  1911. return destroy_and_wait(sess);
  1912. }
  1913. /* TODO: test that ne_set_notifier(, NULL, NULL) DTRT too. */
  1914. ne_test tests[] = {
  1915. T(lookup_localhost),
  1916. T(single_get_clength),
  1917. T(single_get_eof),
  1918. T(single_get_chunked),
  1919. T(no_body_204),
  1920. T(no_body_304),
  1921. T(no_body_HEAD),
  1922. T(no_headers),
  1923. T(chunks),
  1924. T(te_header),
  1925. T(te_identity),
  1926. T(reason_phrase),
  1927. T(chunk_numeric),
  1928. T(chunk_extensions),
  1929. T(chunk_trailers),
  1930. T(chunk_oversize),
  1931. T(te_over_clength),
  1932. T(te_over_clength2),
  1933. T(no_body_chunks),
  1934. T(persist_http11),
  1935. T(persist_chunked),
  1936. T(persist_http10),
  1937. T(persist_proxy_http10),
  1938. T(persist_timeout),
  1939. T(no_persist_http10),
  1940. T(ptimeout_eof),
  1941. T(ptimeout_eof2),
  1942. T(closed_connection),
  1943. T(close_not_retried),
  1944. T(send_progress),
  1945. T(response_bodies),
  1946. T(response_headers),
  1947. T(reset_headers),
  1948. T(iterate_none),
  1949. T(iterate_many),
  1950. T(skip_interim_1xx),
  1951. T(skip_many_1xx),
  1952. T(skip_1xx_hdrs),
  1953. T(send_bodies),
  1954. T(expect_100_once),
  1955. T(expect_100_nobody),
  1956. T(unbounded_headers),
  1957. T(unbounded_folding),
  1958. T(blank_response),
  1959. T(not_http),
  1960. T(fail_eof_continued),
  1961. T(fail_eof_headers),
  1962. T(fail_eof_chunk),
  1963. T(fail_eof_badclen),
  1964. T(fail_long_header),
  1965. T(fail_on_invalid),
  1966. T(read_timeout),
  1967. T(fail_connect),
  1968. T(proxy_no_resolve),
  1969. T(fail_chunksize),
  1970. T(abort_respbody),
  1971. T(retry_after_abort),
  1972. T(fail_statusline),
  1973. T(dup_method),
  1974. T(versions),
  1975. T(hook_create_req),
  1976. T(abort_reader),
  1977. T(send_bad_offset),
  1978. T(hooks),
  1979. T(hook_self_destroy),
  1980. T(icy_protocol),
  1981. T(status),
  1982. T(status_chunked),
  1983. T(local_addr),
  1984. T(dereg_progress),
  1985. T(addrlist),
  1986. T(socks_proxy),
  1987. T(socks_v4_proxy),
  1988. T(send_length),
  1989. T(socks_fail),
  1990. T(fail_lookup),
  1991. T(fail_double_lookup),
  1992. T(safe_flags),
  1993. T(fail_excess_1xx),
  1994. T(target_forms),
  1995. T(interims),
  1996. T(retry_408),
  1997. T(dont_retry_408),
  1998. T(ipv6_literal),
  1999. T(NULL)
  2000. };