Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

what server? What version cgi.pm? You can create cgi objects without it reading fom stdin.

  1. I'm experimenting with an Erlang inets httpd server. If you install Erlang, I can give you the code to start a server.
  2. ~$ perl -MCGI -e 'print "$CGI::VERSION\n";' => 4.38

You can create cgi objects without it reading fom stdin.

When I create a cgi object after reading from STDIN, my curl request to my Erlang server for the cgi script hangs, then times out (but it does succeed on my apache server):

#!/usr/bin/env perl use strict; use warnings; use 5.020; use autodie; use Data::Dumper; use JSON; use CGI; use CGI::Carp qw(fatalsToBrowser); if (my $content_len = $ENV{CONTENT_LENGTH}) { read(STDIN, my $json, $content_len); my $href = decode_json($json); my $a = $href->{a}; my $b = $href->{b}; my $q = CGI->new; #Doesn't work with inets httpd server print $q->header, $q->start_html("Test Page"), $q->div("json=$json"), $q->div("a=$a"), $q->div("b=$b"), $q->end_html; #print 'Content-type: text/html'; #print "\r\n\r\n"; #print "<div>a=$a</div>"; #print "<div>b=$b</div>"; } else { my $error = "Could not read json: No Content-Length header in requ +est."; #print 'Content-type: text/html'; #print "\r\n\r\n"; #print "<div>$error</div>"; my $q = CGI->new; print $q->header, $q->start_html("Test Page"), $q->h1($error), $q->end_html; }

If I comment out the $q lines and uncomment the other print statements, then my curl request succeeds.

Failed curl request:

~$ curl -v -H 'Content-Type: application/json' --data '{"a": 1, "b": 2 +}' http://localhost:65451/cgi-bin/1.pl * Trying ::1... * TCP_NODELAY set * Connection failed * connect to ::1 port 65451 failed: Connection refused * Trying 127.0.0.1... * TCP_NODELAY set * Connected to localhost (127.0.0.1) port 65451 (#0) > POST /cgi-bin/1.pl HTTP/1.1 > Host: localhost:65451 > User-Agent: curl/7.58.0 > Accept: */* > Content-Type: application/json > Content-Length: 16 > * upload completely sent off: 16 out of 16 bytes ==== Hangs here for about 5 sseconds ======= < HTTP/1.1 504 Gateway Time-out < Date: Fri, 09 Mar 2018 14:40:55 GMT < Content-Type: text/html < Server: inets/6.4.5 * no chunk, no close, no size. Assume close to signal end < * Closing connection 0 ~$

Succesful curl request:

~$ curl -v -H 'Content-Type: application/json' --data '{"a": 1, "b": 2 +}' http://localhost:65451/cgi-bin/1.pl * Trying ::1... * TCP_NODELAY set * Connection failed * connect to ::1 port 65451 failed: Connection refused * Trying 127.0.0.1... * TCP_NODELAY set * Connected to localhost (127.0.0.1) port 65451 (#0) > POST /cgi-bin/1.pl HTTP/1.1 > Host: localhost:65451 > User-Agent: curl/7.58.0 > Accept: */* > Content-Type: application/json > Content-Length: 16 > * upload completely sent off: 16 out of 16 bytes < HTTP/1.1 200 OK < Date: Fri, 09 Mar 2018 14:52:03 GMT < Server: inets/6.4.5 < Transfer-Encoding: chunked < Content-Type: text/html < * Connection #0 to host localhost left intact <div>a=1</div><div>b=2</div>~$ </div>

In reply to Re: CGI: newlines, write exactly "\r\n" to end the headers, then turn off binmode by 7stud
in thread CGI: newlines, write exactly "\r\n" to end the headers, then turn off binmode by 7stud

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Domain Nodelet?
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this?Last hourOther CB clients
    Other Users?
    Others chanting in the Monastery: (3)
    As of 2024-07-12 20:53 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      No recent polls found

      Notices?
      erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.