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

Hi all, I am running Perl 5.8.8 scripts on an Apache HTTP web server which is running on a linux machine.

I need to receive one HTTP Request, and respond with multiple HTTP Responses (for the purpose of testing error conditions in a piece of software.) This is not the kind of thing that would generally make any sense, as there is generally one HTTP Request followed by one matching HTTP Response.

I suppose my question is: "Is there any Perl module that I can use to create and send an HTTP Response, without having first received an HTTP Request?"

Any help, or interesting ideas would be very much appreciated. This has me stumped, and I think there is a chance that it is just not possible.

Cheers,
Shug

Replies are listed 'Best First'.
Re: Sending unmatched HTTP Responses
by Corion (Patriarch) on Oct 29, 2009 at 10:29 UTC
      The problem with HTTP:Response is that it allows you to construct an HTTP:Response, or receive one from LWP::UserAgent, but it doesn't allow you to SEND an HTTP::Response.

      I suppose my problem is that LWP::UserAgent doesn't seem to want to let me send an HTTP::Response. Which is generally perfectly reasonable, but would be useful right now.

        Without knowing your further framework, it's hard to advise how to actually send the response. In the simplest way, you would get the client socket from your framework and do

        print {$client_socket} $my_faked_response->as_string;

        which would send the response to your client. Alternatively, look at HTTP::Proxy, which you can use to sit in between your client and the actual server to inject your own responses.

        Maybe you could take Apache out of the picture entirely (for testing purposes) and run a simple Perl webserver, such as HTTP::Server::Simple. Then you could essentially send anything you want, without having to mess with Apache's standard request cycle...

        But, as Corion said, it's a little hard to be more specific, without knowing what exactly you need to do.  In other words, if Apache is a constituent to be included in the testing, this won't work of course.

Re: Sending unmatched HTTP Responses
by moritz (Cardinal) on Oct 29, 2009 at 10:30 UTC

    I know that the mojo project went great lengths to test their HTTP code for compliance and graceful error handling, you could look into their test suites or talk to their developers. I'm sure there's something to be found that you can reuse.

    Perl 6 - links to (nearly) everything that is Perl 6.