From d51caef48e9534f6510e3e7119abc641f60421f5 Mon Sep 17 00:00:00 2001 From: Tokuhiro Matsuno Date: Mon, 14 Sep 2009 09:36:16 +0900 Subject: [PATCH] post support --- server.cc | 15 +++++++++++++++ t/01_simple.t | 1 + t/02_input.t | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 62 insertions(+) create mode 100644 t/02_input.t diff --git a/server.cc b/server.cc index 28eb646..6c8bdb0 100644 --- a/server.cc +++ b/server.cc @@ -34,6 +34,9 @@ static SV* handler; } \ } while (0) +// 500KB is good enough +#define BUFSIZ 500000 + extern "C" { void run(int port, int _nchildren, SV *_handler); } @@ -130,6 +133,7 @@ void send_status_line(int connfd, const char *protocol, int status) { static void http_error(int fd, const char *protocol, int status, const char *message) { send_status_line(fd, protocol, status); + // TODO: send message body } static void http_error_500(int fd, const char * protocol, const char *internal_reason) { @@ -263,6 +267,17 @@ void do_handle(int connfd) hv_store(env, key.c_str(), key.size(), newSVpv(val.c_str(), val.size()), 0); } + // set input file handle + { + PerlIO *input = PerlIO_fdopen(connfd, "r"); + GV *gv = newGVgen("HTTP::Server::Fast::_sock"); // so bad, we don't need to use glob + if (input && do_open(gv, "+<&", 3, FALSE, 0, 0, input)) { + SV * input_sv = sv_2mortal(newSViv(0)); + sv_setsv(input_sv, sv_bless(newRV((SV*)gv), gv_stashpv("HTTP::Server::Fast::_sock",1))); + (void) hv_store(env, "psgi.input", strlen("psgi.input"), input_sv, 0); + } + } + debug("READY TO CALLBACK\n"); // see perlcall.pod diff --git a/t/01_simple.t b/t/01_simple.t index c97b086..930fea8 100644 --- a/t/01_simple.t +++ b/t/01_simple.t @@ -36,6 +36,7 @@ test_tcp( my $port = shift; HTTP::Server::Fast::run($port, 1, sub { my $env = shift; + delete $env->{'psgi.input'}; is_deeply($env, { 'REQUEST_METHOD' => 'GET', 'SERVER_PROTOCOL' => '1.0', diff --git a/t/02_input.t b/t/02_input.t new file mode 100644 index 0000000..fb98afd --- /dev/null +++ b/t/02_input.t @@ -0,0 +1,46 @@ +use strict; +use warnings; +use Test::TCP; +use HTTP::Server::Fast; +use Data::Dumper; +use Test::More; + +test_tcp( + client => sub { + my $port = shift; + my $sock = IO::Socket::INET->new( + PeerHost => '127.0.0.1', + PeerPort => $port, + ) or die; + print $sock "POST /foo?bar=baz HTTP/1.0\r\n"; + print $sock "Content-Type: text/plain\r\n"; + print $sock "X-Foo: bar\r\n"; + print $sock "\r\n"; + print $sock "YATTA!!\n"; + { + my $buf = <$sock>; + is $buf, "HTTP/1.0 200 200\r\n"; + $buf = <$sock>; + is $buf, "Content-Length:3\r\n"; + $buf = <$sock>; + is $buf, "Content-Type:text/html\r\n"; + $buf = <$sock>; + is $buf, "\r\n"; + $buf = <$sock>; + is $buf, "OK!"; + } + done_testing; + }, + server => sub { + my $port = shift; + HTTP::Server::Fast::run($port, 1, sub { + my $env = shift; + my $fh = $env->{'psgi.input'}; + ok $fh; + my $line = <$fh>; + is $line, "YATTA!!\n"; + return [200, ['Content-Length' => 3, 'Content-Type' => 'text/html'], ['OK!']]; + }); + }, +); +