commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Kyle R . Burton" <>
Subject Re: bug: HTTPClient hangs when the server doesn't close the connection
Date Sun, 06 Jan 2002 17:19:04 GMT
> I wanted a simple way to test HTTPClient with a server whose response I can easily manipulate,
so I got this little perl server to do that for me.

For a project I was working on I needed something very similar.  I was
writing code that used HTTPClient and am using JUnit to run the unit tests
for the code.  To properly test my code, I wanted to have a web server
that I could start from the unit test (from JUnit's setUp), have the 
unit tests use it, and then shut it down programmaitlcy (from JUnit's tearDown).

I couldn't find anything suitable on the net, so I ended up writing a 
simple one.  It's farily easy to sub class for each of the unit tests to
provide either canned or simple dynamic responses for the unit tests.  Since
it's all run from the unit test, I don't have to maintain any kind of external
server just to run the unit tests.

If anyone is interested, it's available here:

It is alot more code than your Perl code, but I can control it from
other Java code.


> A simple perl server that listens to a port, prints something, and sleeps.
> It shows that:
> 1. HTTPClient does not time out if the server sends something back often enough that
there is never a pause greater than the specified timeout.
> 2. HTTPClient does time out if the server does not send any data back for a period longer
than the specified timeout.
> 3. If the server responds without any headers (Status line in particular), but sends
something (e.g. body) back periodically (to satisfy 1.) HTTPClient will keep reading the data
forever, hoping to find the Status line.
> This needs to be changed, in my opinion, so that HTTPClient gives up either after some
amount of time, or after it reads a certain number of lines, or after it reads a certain amount
of data and does not find the Status line, or ...
> Here is the perl server:
> #!/usr/bin/perl -w
> use strict;
> use IO::Socket;
> sub Slayer {
>   1 until ( -1 == waitpid(-1,0));
>   $SIG{CHLD} = \&Slayer;
> }
> $SIG{CHLD} = \&Slayer;
> my $server = IO::Socket::INET->new(LocalPort => 8080,
>                                    Type      => SOCK_STREAM,
>                                    Reuse     => 1,
>                                    Listen    => 10) or die "Oink $@\n";
> my $client;
> while ( $client = $server->accept())
> {
>   next if my $pid = fork;
>   die "fork - $!\n" unless defined $pid;
>   select $client;
>   $| = 1;
>   while (1)
>   {
>     print "Slow server response line\n";
>     sleep 20;
>   }
> }
> continue
> {
>   close($client);
> }
> Just change/remove the sleep line depending on what and how you want the server to respond.
> Ok, now I really need to go to bed or I'll miss dim-sum tomorrow morning.
> Otis


Wisdom and Compassion are inseparable.
        -- Christmas Humphreys                  

To unsubscribe, e-mail:   <>
For additional commands, e-mail: <>

View raw message