mod-fastcgi.t 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. #!/usr/bin/env perl
  2. BEGIN {
  3. # add current source dir to the include-path
  4. # we need this for make distcheck
  5. (my $srcdir = $0) =~ s,/[^/]+$,/,;
  6. unshift @INC, $srcdir;
  7. }
  8. use strict;
  9. use Test::More tests => 58;
  10. use LightyTest;
  11. my $tf = LightyTest->new();
  12. my $t;
  13. my $php_child = -1;
  14. my $phpbin = (defined $ENV{'PHP'} ? $ENV{'PHP'} : '/usr/bin/php-cgi');
  15. $ENV{'PHP'} = $phpbin;
  16. SKIP: {
  17. skip "PHP already running on port 1026", 1 if $tf->listening_on(1026);
  18. skip "no php binary found", 1 unless -x $phpbin;
  19. ok(-1 != ($php_child = $tf->spawnfcgi($phpbin, 1026)), "Spawning php");
  20. }
  21. SKIP: {
  22. skip "no PHP running on port 1026", 35 unless $tf->listening_on(1026);
  23. ok($tf->start_proc == 0, "Starting lighttpd") or goto cleanup;
  24. $t->{REQUEST} = ( <<EOF
  25. GET /phpinfo.php HTTP/1.0
  26. Host: www.example.org
  27. EOF
  28. );
  29. $t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200 } ];
  30. ok($tf->handle_http($t) == 0, 'valid request');
  31. $t->{REQUEST} = ( <<EOF
  32. GET /phpinfofoobar.php HTTP/1.0
  33. Host: www.example.org
  34. EOF
  35. );
  36. $t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 404 } ];
  37. ok($tf->handle_http($t) == 0, 'file not found');
  38. $t->{REQUEST} = ( <<EOF
  39. GET /go/ HTTP/1.0
  40. Host: www.example.org
  41. EOF
  42. );
  43. $t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200 } ];
  44. ok($tf->handle_http($t) == 0, 'index-file handling');
  45. $t->{REQUEST} = ( <<EOF
  46. GET /redirect.php HTTP/1.0
  47. Host: www.example.org
  48. EOF
  49. );
  50. $t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 302, 'Location' => 'http://www.example.org:2048/' } ];
  51. ok($tf->handle_http($t) == 0, 'Status + Location via FastCGI');
  52. $t->{REQUEST} = ( <<EOF
  53. GET /redirect.php/ HTTP/1.0
  54. Host: www.example.org
  55. EOF
  56. );
  57. $t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 302, 'Location' => 'http://www.example.org:2048/' } ];
  58. ok($tf->handle_http($t) == 0, 'Trailing slash as path-info (#1989: workaround broken operating systems)');
  59. $t->{REQUEST} = ( <<EOF
  60. GET /get-server-env.php?env=PHP_SELF HTTP/1.0
  61. Host: www.example.org
  62. EOF
  63. );
  64. $t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200 } ];
  65. ok($tf->handle_http($t) == 0, '$_SERVER["PHP_SELF"]');
  66. $t->{REQUEST} = ( <<EOF
  67. GET /get-server-env.php/foo?env=SCRIPT_NAME HTTP/1.0
  68. Host: www.example.org
  69. EOF
  70. );
  71. $t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200, 'HTTP-Content' => '/get-server-env.php' } ];
  72. ok($tf->handle_http($t) == 0, '$_SERVER["SCRIPT_NAME"]');
  73. $t->{REQUEST} = ( <<EOF
  74. GET /get-server-env.php/foo?env=PATH_INFO HTTP/1.0
  75. Host: www.example.org
  76. EOF
  77. );
  78. $t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200, 'HTTP-Content' => '/foo' } ];
  79. ok($tf->handle_http($t) == 0, '$_SERVER["PATH_INFO"]');
  80. $t->{REQUEST} = ( <<EOF
  81. GET /get-server-env.php?env=SERVER_NAME HTTP/1.0
  82. Host: www.example.org
  83. EOF
  84. );
  85. $t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200, 'HTTP-Content' => 'www.example.org' } ];
  86. ok($tf->handle_http($t) == 0, 'SERVER_NAME');
  87. $t->{REQUEST} = ( <<EOF
  88. GET /get-server-env.php?env=SERVER_NAME HTTP/1.0
  89. Host: foo.example.org
  90. EOF
  91. );
  92. $t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200, 'HTTP-Content' => 'www.example.org' } ];
  93. ok($tf->handle_http($t) == 0, 'SERVER_NAME');
  94. $t->{REQUEST} = ( <<EOF
  95. GET /get-server-env.php?env=SERVER_NAME HTTP/1.0
  96. Host: vvv.example.org
  97. EOF
  98. );
  99. $t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200, 'HTTP-Content' => 'www.example.org' } ];
  100. ok($tf->handle_http($t) == 0, 'SERVER_NAME');
  101. $t->{REQUEST} = ( <<EOF
  102. GET /get-server-env.php?env=SERVER_NAME HTTP/1.0
  103. Host: zzz.example.org
  104. EOF
  105. );
  106. $t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200, 'HTTP-Content' => 'www.example.org' } ];
  107. ok($tf->handle_http($t) == 0, 'SERVER_NAME');
  108. $t->{REQUEST} = ( <<EOF
  109. GET /cgi.php/abc HTTP/1.0
  110. EOF
  111. );
  112. $t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200 } ];
  113. ok($tf->handle_http($t) == 0, 'PATHINFO');
  114. $t->{REQUEST} = ( <<EOF
  115. GET /cgi.php%20%20%20 HTTP/1.0
  116. EOF
  117. );
  118. $t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 404 } ];
  119. ok($tf->handle_http($t) == 0, 'No source retrieval');
  120. $t->{REQUEST} = ( <<EOF
  121. GET /www/abc/def HTTP/1.0
  122. EOF
  123. );
  124. $t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 404 } ];
  125. ok($tf->handle_http($t) == 0, 'PATHINFO on a directory');
  126. $t->{REQUEST} = ( <<EOF
  127. GET /indexfile/ HTTP/1.0
  128. EOF
  129. );
  130. $t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200, 'HTTP-Content' => '/indexfile/index.php' } ];
  131. ok($tf->handle_http($t) == 0, 'PHP_SELF + Indexfile, Bug #3');
  132. $t->{REQUEST} = ( <<EOF
  133. GET /prefix.fcgi?var=SCRIPT_NAME HTTP/1.0
  134. EOF
  135. );
  136. $t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200, 'HTTP-Content' => '/prefix.fcgi' } ];
  137. ok($tf->handle_http($t) == 0, 'PATH_INFO, check-local off');
  138. $t->{REQUEST} = ( <<EOF
  139. GET /prefix.fcgi/foo/bar?var=SCRIPT_NAME HTTP/1.0
  140. EOF
  141. );
  142. $t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200, 'HTTP-Content' => '/prefix.fcgi' } ];
  143. ok($tf->handle_http($t) == 0, 'PATH_INFO, check-local off');
  144. $t->{REQUEST} = ( <<EOF
  145. GET /prefix.fcgi/foo/bar?var=PATH_INFO HTTP/1.0
  146. EOF
  147. );
  148. $t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200, 'HTTP-Content' => '/foo/bar' } ];
  149. ok($tf->handle_http($t) == 0, 'PATH_INFO, check-local off');
  150. $t->{REQUEST} = ( <<EOF
  151. GET /sendfile.php?range=0- HTTP/1.0
  152. EOF
  153. );
  154. $t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200, 'Content-Length' => 4348 } ];
  155. ok($tf->handle_http($t) == 0, 'X-Sendfile2');
  156. $t->{REQUEST} = ( <<EOF
  157. GET /sendfile.php?range=0-4&range2=5- HTTP/1.0
  158. EOF
  159. );
  160. $t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200, 'Content-Length' => 4348 } ];
  161. ok($tf->handle_http($t) == 0, 'X-Sendfile2');
  162. $t->{REQUEST} = ( <<EOF
  163. GET /get-server-env.php?env=REMOTE_USER HTTP/1.0
  164. Host: auth.example.org
  165. Authorization: Basic ZGVzOmRlcw==
  166. EOF
  167. );
  168. $t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200, 'HTTP-Content' => 'des' } ];
  169. ok($tf->handle_http($t) == 0, '$_SERVER["REMOTE_USER"]');
  170. $t->{REQUEST} = ( <<EOF
  171. GET /get-server-env.php?env=AUTH_TYPE HTTP/1.0
  172. Host: auth.example.org
  173. Authorization: Basic ZGVzOmRlcw==
  174. EOF
  175. );
  176. $t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200, 'HTTP-Content' => 'Basic' } ];
  177. ok($tf->handle_http($t) == 0, '$_SERVER["AUTH_TYPE"]');
  178. ok($tf->stop_proc == 0, "Stopping lighttpd");
  179. $tf->{CONFIGFILE} = 'fastcgi-10.conf';
  180. ok($tf->start_proc == 0, "Starting lighttpd with $tf->{CONFIGFILE}") or goto cleanup;
  181. $t->{REQUEST} = ( <<EOF
  182. GET /get-server-env.php?env=SERVER_NAME HTTP/1.0
  183. Host: zzz.example.org
  184. EOF
  185. );
  186. $t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200, 'HTTP-Content' => 'zzz.example.org' } ];
  187. ok($tf->handle_http($t) == 0, 'FastCGI + Host');
  188. $t->{REQUEST} = ( <<EOF
  189. GET http://zzz.example.org/get-server-env.php?env=SERVER_NAME HTTP/1.0
  190. Host: aaa.example.org
  191. EOF
  192. );
  193. $t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200, 'HTTP-Content' => 'zzz.example.org' } ];
  194. ok($tf->handle_http($t) == 0, 'SERVER_NAME (absolute url in request line)');
  195. ok($tf->stop_proc == 0, "Stopping lighttpd");
  196. $tf->{CONFIGFILE} = 'bug-06.conf';
  197. ok($tf->start_proc == 0, "Starting lighttpd with $tf->{CONFIGFILE}") or goto cleanup;
  198. $t->{REQUEST} = ( <<EOF
  199. GET /indexfile/ HTTP/1.0
  200. Host: www.example.org
  201. EOF
  202. );
  203. $t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200, 'HTTP-Content' => '/indexfile/index.php' } ];
  204. ok($tf->handle_http($t) == 0, 'Bug #6');
  205. ok($tf->stop_proc == 0, "Stopping lighttpd");
  206. $tf->{CONFIGFILE} = 'bug-12.conf';
  207. ok($tf->start_proc == 0, "Starting lighttpd with bug-12.conf") or goto cleanup;
  208. $t->{REQUEST} = ( <<EOF
  209. POST /indexfile/abc HTTP/1.0
  210. Host: www.example.org
  211. Content-Length: 0
  212. EOF
  213. );
  214. $t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 404, 'HTTP-Content' => '/indexfile/return-404.php' } ];
  215. ok($tf->handle_http($t) == 0, 'Bug #12');
  216. ok($tf->stop_proc == 0, "Stopping lighttpd");
  217. }
  218. SKIP: {
  219. skip "PHP not started, cannot stop it", 1 unless $php_child != -1;
  220. ok(0 == $tf->endspawnfcgi($php_child), "Stopping php");
  221. $php_child = -1;
  222. }
  223. SKIP: {
  224. skip "no fcgi-auth found", 5 unless -x $tf->{BASEDIR}."/tests/fcgi-auth" || -x $tf->{BASEDIR}."/tests/fcgi-auth.exe";
  225. $tf->{CONFIGFILE} = 'fastcgi-auth.conf';
  226. ok($tf->start_proc == 0, "Starting lighttpd with $tf->{CONFIGFILE}") or die();
  227. $t->{REQUEST} = ( <<EOF
  228. GET /index.html?ok HTTP/1.0
  229. Host: www.example.org
  230. EOF
  231. );
  232. $t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200 } ];
  233. ok($tf->handle_http($t) == 0, 'FastCGI - Auth');
  234. $t->{REQUEST} = ( <<EOF
  235. GET /index.html?fail HTTP/1.0
  236. Host: www.example.org
  237. EOF
  238. );
  239. $t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 403 } ];
  240. ok($tf->handle_http($t) == 0, 'FastCGI - Auth');
  241. $t->{REQUEST} = ( <<EOF
  242. GET /expire/access.txt?ok HTTP/1.0
  243. Host: www.example.org
  244. EOF
  245. );
  246. $t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200 } ];
  247. ok($tf->handle_http($t) == 0, 'FastCGI - Auth in subdirectory');
  248. ok($tf->stop_proc == 0, "Stopping lighttpd");
  249. }
  250. SKIP: {
  251. skip "no php found", 5 unless -x $phpbin;
  252. $tf->{CONFIGFILE} = 'fastcgi-13.conf';
  253. ok($tf->start_proc == 0, "Starting lighttpd with $tf->{CONFIGFILE}") or die();
  254. $t->{REQUEST} = ( <<EOF
  255. GET /indexfile/index.php HTTP/1.0
  256. Host: www.example.org
  257. EOF
  258. );
  259. $t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200 } ];
  260. ok($tf->handle_http($t) == 0, 'FastCGI + local spawning');
  261. $t->{REQUEST} = ( <<EOF
  262. HEAD /indexfile/index.php HTTP/1.0
  263. Host: www.example.org
  264. EOF
  265. );
  266. $t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200, '-Content-Length' => '0' } ];
  267. # Of course a valid content-length != 0 would be ok, but we assume for now that such one is not generated.
  268. ok($tf->handle_http($t) == 0, 'Check for buggy content length with HEAD');
  269. $t->{REQUEST} = ( <<EOF
  270. GET /get-env.php?env=MAIL HTTP/1.0
  271. Host: www.example.org
  272. EOF
  273. );
  274. $t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200 , 'HTTP-Content' => '' } ];
  275. ok($tf->handle_http($t) == 0, 'FastCGI + bin-copy-environment');
  276. ok($tf->stop_proc == 0, "Stopping lighttpd");
  277. }
  278. SKIP: {
  279. skip "no fcgi-responder found", 11 unless -x $tf->{BASEDIR}."/tests/fcgi-responder" || -x $tf->{BASEDIR}."/tests/fcgi-responder.exe";
  280. $tf->{CONFIGFILE} = 'fastcgi-responder.conf';
  281. ok($tf->start_proc == 0, "Starting lighttpd with $tf->{CONFIGFILE}") or die();
  282. $t->{REQUEST} = ( <<EOF
  283. GET /index.fcgi?lf HTTP/1.0
  284. Host: www.example.org
  285. EOF
  286. );
  287. $t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200, 'HTTP-Content' => 'test123' } ];
  288. ok($tf->handle_http($t) == 0, 'line-ending \n\n');
  289. $t->{REQUEST} = ( <<EOF
  290. GET /index.fcgi?crlf HTTP/1.0
  291. Host: www.example.org
  292. EOF
  293. );
  294. $t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200, 'HTTP-Content' => 'test123' } ];
  295. ok($tf->handle_http($t) == 0, 'line-ending \r\n\r\n');
  296. $t->{REQUEST} = ( <<EOF
  297. GET /index.fcgi?slow-lf HTTP/1.0
  298. Host: www.example.org
  299. EOF
  300. );
  301. $t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200, 'HTTP-Content' => 'test123' } ];
  302. ok($tf->handle_http($t) == 0, 'line-ending \n + \n');
  303. $t->{REQUEST} = ( <<EOF
  304. GET /index.fcgi?slow-crlf HTTP/1.0
  305. Host: www.example.org
  306. EOF
  307. );
  308. $t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200, 'HTTP-Content' => 'test123' } ];
  309. ok($tf->handle_http($t) == 0, 'line-ending \r\n + \r\n');
  310. $t->{REQUEST} = ( <<EOF
  311. GET /abc/def/ghi?path_info HTTP/1.0
  312. Host: wsgi.example.org
  313. EOF
  314. );
  315. $t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200, 'HTTP-Content' => '/abc/def/ghi' } ];
  316. ok($tf->handle_http($t) == 0, 'PATH_INFO (wsgi)');
  317. $t->{REQUEST} = ( <<EOF
  318. GET /abc/def/ghi?script_name HTTP/1.0
  319. Host: wsgi.example.org
  320. EOF
  321. );
  322. $t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200, 'HTTP-Content' => '' } ];
  323. ok($tf->handle_http($t) == 0, 'SCRIPT_NAME (wsgi)');
  324. $t->{REQUEST} = ( <<EOF
  325. GET /index.fcgi?die-at-end HTTP/1.0
  326. Host: www.example.org
  327. EOF
  328. );
  329. $t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200, 'HTTP-Content' => 'test123' } ];
  330. ok($tf->handle_http($t) == 0, 'killing fastcgi and wait for restart');
  331. select(undef, undef, undef, .2);
  332. $t->{REQUEST} = ( <<EOF
  333. GET /index.fcgi?die-at-end HTTP/1.0
  334. Host: www.example.org
  335. EOF
  336. );
  337. $t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200, 'HTTP-Content' => 'test123' } ];
  338. ok($tf->handle_http($t) == 0, 'killing fastcgi and wait for restart');
  339. select(undef, undef, undef, .2);
  340. $t->{REQUEST} = ( <<EOF
  341. GET /index.fcgi?crlf HTTP/1.0
  342. Host: www.example.org
  343. EOF
  344. );
  345. $t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200, 'HTTP-Content' => 'test123' } ];
  346. ok($tf->handle_http($t) == 0, 'regular response of after restart');
  347. ok($tf->stop_proc == 0, "Stopping lighttpd");
  348. }
  349. exit 0;
  350. cleanup: ;
  351. $tf->endspawnfcgi($php_child) if $php_child != -1;
  352. die();