Skip to content

Commit

Permalink
post support
Browse files Browse the repository at this point in the history
  • Loading branch information
tokuhirom committed Sep 14, 2009
1 parent c37d93e commit d51caef
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 0 deletions.
15 changes: 15 additions & 0 deletions server.cc
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions t/01_simple.t
Expand Up @@ -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',
Expand Down
46 changes: 46 additions & 0 deletions 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!']];
});
},
);

0 comments on commit d51caef

Please sign in to comment.