PageRenderTime 47ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/js/lib/Socket.IO-node/support/expresso/deps/jscoverage/tests/server.pl

http://github.com/onedayitwillmake/RealtimeMultiplayerNodeJs
Perl | 61 lines | 56 code | 4 blank | 1 comment | 10 complexity | a714389ef86397ea0d175856cb522141 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, MPL-2.0-no-copyleft-exception, BSD-3-Clause
  1. use strict;
  2. use warnings;
  3. use HTTP::Daemon;
  4. use HTTP::Status;
  5. $|++;
  6. my $d = HTTP::Daemon->new(LocalPort => 8000, ReuseAddr => 1) || die;
  7. print "Please contact me at: <URL:", $d->url, ">\n";
  8. my $done = 0;
  9. while (not $done and my $c = $d->accept) {
  10. my $r = $c->get_request;
  11. if (not defined($r)) {
  12. print "Error: ", $c->reason, "\n";
  13. $c->close;
  14. undef($c);
  15. next;
  16. }
  17. print STDERR $r->method, ' ', $r->url, "\n";
  18. $c->force_last_request;
  19. if ($r->method eq 'GET') {
  20. my $file = substr($r->url->path, 1);
  21. if (open FILE, $file) {
  22. undef $/;
  23. binmode FILE;
  24. my $content = <FILE>;
  25. close FILE;
  26. my @headers = ('Connection' => 'close');
  27. if ($file =~ /\.js$/) {
  28. push @headers, 'Content-Type' => 'text/javascript';
  29. }
  30. elsif ($file =~ /\.[^\/]+$/) {
  31. push @headers, 'Content-Type' => 'application/octet-stream';
  32. }
  33. else {
  34. # do nothing - no Content-Type
  35. }
  36. my $response = HTTP::Response->new(200, 'OK', \@headers, $content);
  37. $c->send_response($response);
  38. }
  39. else {
  40. my $response = HTTP::Response->new(404, 'Not found', ['Connection' => 'close'], 'Not found');
  41. $c->send_response($response);
  42. }
  43. }
  44. elsif ($r->method eq 'POST') {
  45. if ($r->url->path eq '/perl-shutdown') {
  46. $done = 1;
  47. }
  48. my $content = $r->content;
  49. my $response = HTTP::Response->new(200, 'OK', ['Connection' => 'close'], $content);
  50. $c->send_response($response);
  51. }
  52. else {
  53. $c->send_error(RC_FORBIDDEN);
  54. }
  55. $c->close;
  56. undef($c);
  57. }