in reply to A simple web server with HTTP::Daemon and threads

I don't know if it is because the root isn't set for the http server, but I needed to strip the leading slash off of filenames to get this to work, when the server is serving files out of it's working directory. Otherwise I would get "file not found". Example: /index.html needed to be index,html.
if ($r->method eq "GET") { my $path = $r->url->path(); $path = substr($path,1); #strip leading slash print "$path\n"; $c->send_file_response($path); #or do whatever you want here }

Replies are listed 'Best First'.
Re: Re: A simple web server with HTTP::Daemon and threads
by pg (Canon) on Oct 31, 2003 at 22:57 UTC

    Leading slash might be a problem, might not. Three things could affect this (actually two and three ould be considered as one point):

    • How you organize and store your web content on your local driver, and how you map those URL’s to the actual storage location.
    • In your html files, how you specify links, full path or relative path.
    • How users specify the link from their browser, full or relative?

    In general, a path with a leading slash, when you use it directely without any mapping, the system understands it as absolute path starting from the root directory, which might not be what you want.

    A good design practice is to have a mapping function, to map URL/URI to the actual storage location. This could largely ease your maintenance effort. In the future, if you move your stuffs around, this could be the single point that requires change. Or at least have a constant to specify the base path.

Re^2: A simple web server with HTTP::Daemon and threads
by lixus (Initiate) on Jun 23, 2009 at 10:23 UTC
    Hello Fellows, I am really not a thread nor an OO expert but I am still wondering what is wrong with the code below.

    My intention is to write a http proxy in perl. Once this is working I am going to use it to cache/serve png map tiles from e.g. www.openstreetmap.org

    Why is it so slow?
    Why does it stop running after a a few request?

    #!/usr/bin/perl use HTTP::Daemon; use LWP::UserAgent; use threads; my $proxy = HTTP::Daemon->new( LocalPort => 3128, Listen => 20, Reuse= +>1) || die; while (my $conn = $proxy->accept) { threads->create(\&process_one_req, $conn)->detach(); } sub process_one_req { my $conn = shift; my $request = $conn->get_request; my $ua = LWP::UserAgent->new; print $request->uri,"\n"; my $response = $ua->simple_request($request); $conn->send_response($response); $conn->close; undef($conn); undef($ua); } ## end sub process_one_req
      To get the code to work reliably on Windows7 (ActiveState Perl v5.12.4) I had to add a sleep of at least 15ms after the thread creation. I use 50ms for good measure:
      use Time::HiRes qw( usleep ); ... while (my $c = $d->accept) { threads->create(\&process_one_req, $c)->detach(); usleep(50_000); }
      Update: after attempting AJAX POSTs with some jQuery/JavaScript, the Perl web server code was not reliable. I seemed to loose AJAX messages. (As a workaround, I installed an AJAX error handler in the JavaScript code, which performs the same AJAX POST again (well, using setTimeout)... that worked, since I can tolerate multi-second delays -- the app updates graphs that are generated only every 4 seconds.)

      Update: after playing around with this some more, I was able to get reliable operation, and handle multiple browsers and multiple requests per connection/browser if I closed the client connection/socket in the server, and the daemon/server socket in the client:

      use HTTP::Daemon; use threads; my $d = HTTP::Daemon->new(LocalAddr => $ARGV[0], LocalPort => 80, Reuse => 1, Listen => 20) || die; print "Web Server started, server address: ", $d->sockhost(), ", serve +r port: ", $d->sockport(), "\n"; while (my $c = $d->accept) { threads->create(\&process_client_requests, $c)->detach; $c->close; # close client socket in server } sub process_client_requests { my $c = shift; $c->daemon->close; # close server socket in client while(my $r = $c->get_request) { if ($r->method eq "GET") { my $path = $r->url->path(); $c->send_file_response($path) or die $!; #or do whatever you want here } else { print "unknown method ".$r->method."\n" } } $c->close; }