Skip to content

Instantly share code, notes, and snippets.

@olegwtf
Created October 13, 2015 14:17
Show Gist options
  • Save olegwtf/c4dc83711c03a66ba07a to your computer and use it in GitHub Desktop.
Save olegwtf/c4dc83711c03a66ba07a to your computer and use it in GitHub Desktop.
use strict;
use IO::Socket;
my $serv = IO::Socket::INET->new(Listen => 10, LocalPort => 8080)
or die $@;
my $header = join(
"\r\n",
"HTTP/1.1 200 OK",
"Server: nginx/1.0.4",
"Date: Thu, 06 Oct 2011 16:14:01 GMT",
"Content-Type: text/html",
"Transfer-Encoding: chunked",
"Connection: keep-alive",
"Vary: Accept-Encoding",
"X-Powered-By: PHP/5.3.6",
"\r\n"
);
my @chunks = (
'This is the data in the first chunk ',
'and this is the second one ',
'con',
'sequence'
);
while (my $client = $serv->accept()) {
my $req;
while ($req !~ /\r\n\r\n$/) {
$client->sysread($req, 1024, length $req) or die $!;
}
$client->syswrite($header);
for my $c (@chunks) {
$client->syswrite(sprintf("%x\r\n", length $c));
sleep 1;
$client->syswrite($c."\r\n");
}
$client->syswrite("0\r\n\r\n");
}
use strict;
use Net::HTTP::NB;
use Data::Dumper;
my $s = Net::HTTP::NB->new(Host => "localhost:8080") || die $@;
$s->write_request(GET => "/", 'User-Agent' => "Mozilla/5.0");
use IO::Select;
my $sel = IO::Select->new($s);
READ_HEADER: {
die "Header timeout" unless $sel->can_read(10);
my($code, $mess, %h) = $s->read_response_headers;
redo READ_HEADER unless $code;
print Dumper $code, $mess, \%h;
}
while (1) {
die "Body timeout" unless $sel->can_read(10);
my $buf;
my $n = $s->read_entity_body($buf, 1024);
last unless $n;
print $buf;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment