Skip to content

Instantly share code, notes, and snippets.

@Mons
Created May 23, 2018 18:39
Show Gist options
  • Select an option

  • Save Mons/b25e30e580e4df5762ecce4758e44ffa to your computer and use it in GitHub Desktop.

Select an option

Save Mons/b25e30e580e4df5762ecce4758e44ffa to your computer and use it in GitHub Desktop.
sub listener {
push_read(line => sub {
my $line = shift; # GET / HTTP/1.1
# PUT /file HTTP/1.1
my ($method, $path, $http) = $line =~ m{^(\S+)\s+(\S+)};
# Key: value
# Conetent-Length: 10
#
# body...
# 0x0D 0x0A
# \015 \012
# \r \n
unshift_read( line => qr/\015?\012\015?\012/, sub {
# Key: value
# Content-Length: 10
# ConTent-LeNgth: 10
my $line = shift;
my @headers = split /\015?\012/, $line;
my %headers;
for (@headers) {
my ($key,$value) = split /\s*:\s*/, $_, 2;
$headers{lc $key} = $value;
}
if ($headers{'content-length'} > 0) {
unshift_read(chunk => $headers{'content-length'}, sub {
my $body = shift;
listener()
})
}
} );
})
}
listener()
listener()
listener()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment