HTTP::Request format.

Discussion in 'Perl Misc' started by Danny Woods, Jan 29, 2004.

  1. Danny Woods

    Danny Woods Guest

    Hi all,

    I have a script that listens on a socket, reads some XML from an
    external entity, slaps a POST header on it, and POSTs it off to a Web
    server for processing. So, using HTTP::Request::Common, it's a simple
    matter of:

    $request = POST("http://www.no-server-here.com/cgi-bin/script.cgi");
    $request->header("Content-Length", length($xml));
    $request->content($xml);

    When sent, this looks something like:

    POST http://www.no-server-here.com/cgi-bin/script.cgi
    Content-Length: [some-number]

    <some-xml..../>

    But I have a client who (for some reason) wants:

    POST /cgi-bin/script.cgi HTTP/1.0
    Host: www.no-server-here.com
    Content-Length: [some-number]

    <some-xml..../>

    ?

    The RFC states (at
    http://www.w3.org/Protocols/rfc2616/rfc2616-sec5.html#sec5) that
    requests start with a request line, which has a format of:

    Method SP Request-URI SP HTTP-Version CRLF

    The format for a Request-URI seems to be leaning towards the absolute
    format produced by HTTP::Request, but the other format with the Host
    header is still valid. In any case, the protocol and version don't
    appear to be optional, and yet are missed out by HTTP::Request.

    Is there any way that I'm missing to hammer the request string into
    shape, or so I have to resort to sockets and hand made requests?


    Many thanks,

    Danny.
     
    Danny Woods, Jan 29, 2004
    #1
    1. Advertising

  2. Danny Woods

    Ben Morrow Guest

    Danny Woods <> wrote:
    > $request = POST("http://www.no-server-here.com/cgi-bin/script.cgi");
    > $request->header("Content-Length", length($xml));
    > $request->content($xml);
    >
    > When sent, this looks something like:
    >
    > POST http://www.no-server-here.com/cgi-bin/script.cgi
    > Content-Length: [some-number]
    >
    > <some-xml..../>
    >
    > But I have a client who (for some reason) wants:
    >
    > POST /cgi-bin/script.cgi HTTP/1.0
    > Host: www.no-server-here.com
    > Content-Length: [some-number]
    >
    > <some-xml..../>
    >
    > ?
    >
    > The RFC states (at
    > http://www.w3.org/Protocols/rfc2616/rfc2616-sec5.html#sec5) that
    > requests start with a request line, which has a format of:
    >
    > Method SP Request-URI SP HTTP-Version CRLF
    >
    > The format for a Request-URI seems to be leaning towards the absolute
    > format produced by HTTP::Request, but the other format with the Host
    > header is still valid. In any case, the protocol and version don't
    > appear to be optional, and yet are missed out by HTTP::Request.


    This is Not Good... :)

    The protocol is absolutely not optional, and can be put in with

    $request->protocol("HTTP/1.0");

    The Host: header is required for HTTP/1.1 and recommended for
    HTTP/1.0, even if the request-URI is absolute. You can put this in
    with

    $request->header(host => $request->uri->host);

    The server ought to accept a request in this format, i.e.

    POST http://www.no-server-here.com/cgi-bin/script.cgi HTTP/1.0
    Host: www.no-server-here.com
    Content-Length: ...

    , but if it really won't then you can make the request-URI relative
    with

    $request->uri($request->uri->path);

    (obviously you must do this *after* you set the Host: header from the
    URI :).

    Note that if you use LWP::UserAgent to send the request it does all
    this for you.

    Ben

    --
    $.=1;*g=sub{print@_};sub r($$\$){my($w,$x,$y)=@_;for(keys%$x){/main/&&next;*p=$
    $x{$_};/(\w)::$/&&(r($w.$1,$x.$_,$y),next);$y eq\$p&&&g("$w$_")}};sub t{for(@_)
    {$f&&($_||&g(" "));$f=1;r"","::",$_;$_&&&g(chr(0012))}};t #
    $J::u::s::t, $a::n::eek:::t::h::e::r, $P::e::r::l, $h::a::c::k::e::r, $.
     
    Ben Morrow, Jan 29, 2004
    #2
    1. Advertising

  3. Danny Woods

    Danny Woods Guest

    Ben Morrow wrote:

    > Note that if you use LWP::UserAgent to send the request it does all
    > this for you.


    Thanks, Ben.

    I've been using LWP::UserAgent to actually send the request, but was
    confusing things by setting up the request object and then using
    $request->as_string to examine the request before sending. This shows
    something rather different from what the agent actually fires out.

    So the problem wasn't really a problem at all...

    Thanks again,

    Danny.
     
    Danny Woods, Jan 30, 2004
    #3
  4. Danny Woods

    kz Guest

    > Ben
    >
    > --
    > $.=1;*g=sub{print@_};sub

    r($$\$){my($w,$x,$y)=@_;for(keys%$x){/main/&&next;*p=$
    > $x{$_};/(\w)::$/&&(r($w.$1,$x.$_,$y),next);$y eq\$p&&&g("$w$_")}};sub

    t{for(@_)
    > {$f&&($_||&g(" "));$f=1;r"","::",$_;$_&&&g(chr(0012))}};t #


    > $J::u::s::t, $a::n::eek:::t::h::e::r, $P::e::r::l, $h::a::c::k::e::r, $.


    This is OFPOS.
    One Fine Piece Of Signature.
    Wow!

    Zoltan
     
    kz, Jan 30, 2004
    #4
  5. Danny Woods

    kz Guest

    > Ben
    >
    > --
    > $.=1;*g=sub{print@_};sub

    r($$\$){my($w,$x,$y)=@_;for(keys%$x){/main/&&next;*p=$
    > $x{$_};/(\w)::$/&&(r($w.$1,$x.$_,$y),next);$y eq\$p&&&g("$w$_")}};sub

    t{for(@_)
    > {$f&&($_||&g(" "));$f=1;r"","::",$_;$_&&&g(chr(0012))}};t #


    > $J::u::s::t, $a::n::eek:::t::h::e::r, $P::e::r::l, $h::a::c::k::e::r, $.


    This is OFPOS.
    One Fine Piece Of Signature.
    Wow!

    Zoltan
     
    kz, Jan 30, 2004
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Brian Birtle
    Replies:
    2
    Views:
    2,102
    John Saunders
    Oct 16, 2003
  2. nRk
    Replies:
    1
    Views:
    1,020
    Steven D'Aprano
    Feb 12, 2009
  3. Slie
    Replies:
    3
    Views:
    971
    Tim Harig
    Jan 6, 2011
  4. Chris Rebert
    Replies:
    0
    Views:
    570
    Chris Rebert
    Jan 6, 2011
  5. tesis
    Replies:
    2
    Views:
    339
    Saverio Tedeschi
    Nov 2, 2005
Loading...

Share This Page