bkiahg has asked for the wisdom of the Perl Monks concerning the following question:

Hello Wise Monks,

I'm having problems with HTTP::Daemon when serving multiple images. I found this post http://use.perl.org/~jjohn/journal/9030 describing the problem I was getting and added $SIG{'PIPE'} = 'IGNORE'; to the top of the script. That helped, but it still randomly doesn't display one or more of the images.

Ok, here is the long version of how I came to this problem. I originally couldn't get it to serve more than one image. If there were 2 images on the page the webserver wouldn't show any of them after the first image. It would serve the page and the first image and then sit there.

So I searched the web and found this post http://use.perl.org/~jjohn/journal/9030 describing exactly the problem I was having. It stated that it was a IO::Socket::INET issue. I followed his advice and added $SIG{'PIPE'} = 'IGNORE'; to the top of the script.

That worked for the original crashing of the pipe and it allowed it to serve multiple images after the initial image. The problem I have now is that it randomly does or doesn't display all of the images. Some times it shows all of them, and sometimes it doesn't show one or more. All very random.

The only constant I can see is that if the print inside of the while loop doesn't fire, then no image is served. But I have no idea why sometimes it isn't getting inside of that loop and I'm not sure how to trouble shoot this..


So I'm looking for some advice or a point in a direction on how to troubleshoot this. Really at a loss on the solution here..
use strict; $SIG{'PIPE'} = 'IGNORE'; use HTTP::Daemon; use HTTP::Status; require 'C:\\rrships\\default.cgi'; my $d = HTTP::Daemon->new(Listen => 5, LocalAddr => 'localhost', LocalPort => 80, Proto => 'tcp') or die "Can't bin +d : $@\n"; # print "Please contact me at: <URL:", $d->url, ">\n"; while (my $c = $d->accept) { while (my $r = $c->get_request) { # if the below print goes, then the image is served correctl +y print $r->method . ' ' . $r->url->path . "\n"; if (($r->method eq 'GET') && ($r->url->path =~ m/\.(html|cgi +|htm|pl)/)) { my $response = HTTP::Response->new(200); $response->content(&Hello_World($r->url->path, $r->url-> +query)); $response->header("Content-Type" => "text/html"); $c->send_response($response); } elsif (($r->method eq 'GET') && ($r->url->path =~ m/\.(gif|j +pg|bmp|png)/)) { open (IMAGE, 'C:\\rrships' . $r->url->path) or warn 'Cou +ldn\'t open image: ' . $r->url->path; my $response = HTTP::Response->new(200); $response->push_header('Content-Type','image/' . $1); my $content = ''; while (<IMAGE>) { $content .= $_; } close IMAGE; $response->content($content); $c->send_response($response); } elsif (($r->method eq 'GET') && ($r->url->path eq '/')) { my $response = HTTP::Response->new(200); $response->content(&Hello_World($r->url->path, $r->url-> +query)); $response->header("Content-Type" => "text/html"); $c->send_response($response); } else { $c->send_error(RC_FORBIDDEN); } } $c->close; undef($c); }

Replies are listed 'Best First'.
Re: HTTP::Daemon Pipe Breaking
by Illuminatus (Curate) on Jan 13, 2009 at 22:42 UTC
    1. You do realize that your program is single-threaded, right? It does each connection in order. You have the listen set to 5, so if you get more than 5 requests while you are servicing one, the extra ones will get dropped.
    2. Your read of the image files is not optimal. Reading based on newlines is not efficient for a binary file. It will probably go faster if you get the file size and do a single read on it. Also, if you are on Windows, I am pretty sure you have to set binmode.
      So:

      1. Read up on making this multi-threaded. Would it be better if I fork them? Never been down this path.
      2. Figure out how to set binmode and do a lookup on the file size and do a single read.

      Check.