in reply to Re^2: CGI: newlines, write exactly "\r\n" to end the headers, then turn off binmode
in thread CGI: newlines, write exactly "\r\n" to end the headers, then turn off binmode
what server? What version cgi.pm? You can create cgi objects without it reading fom stdin.
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>
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: CGI: newlines, write exactly "\r\n" to end the headers, then turn off binmode
by Anonymous Monk on Mar 10, 2018 at 08:01 UTC | |
by 7stud (Deacon) on Mar 10, 2018 at 11:41 UTC |