Linux-Event-HTTP
view release on metacpan or search on metacpan
my $loop = Linux::Event::Loop->new;
my $server = Linux::Event::HTTP::Server->new(
loop => $loop,
host => '127.0.0.1',
port => 8080,
on_request => sub ($tx, $req) {
$tx->respond(
status => 200,
headers => [['content-type', 'text/plain']],
body => "Hello from Linux::Event!\n",
);
},
);
say "Listening on http://127.0.0.1:8080/";
$loop->run;
```
Start it with `perl server.pl`. In another terminal, run:
docs/GETTING-STARTED.md view on Meta::CPAN
This guide assumes Linux, Perl 5.36+, and `Linux::Event::HTTP` installed from CPAN. Start with the two complete examples in the [README](../README.md). The examples below build on those same `$loop`, `$client`, or `$server` objects.
## Make an HTTP request
For a GET, call `get($url, %options)`. For a POST, use `post`. The Client also has `head`, `put`, and `delete` methods.
```perl
$client->post(
'https://example.com/messages',
headers => [['content-type', 'text/plain']],
body => "Hello\n",
timeout => 15,
on_response => sub ($tx, $res) {
say "Status: ", $res->status;
say "Content type: ", ($res->header('content-type') // 'none');
say "Body: ", $res->body;
},
on_error => sub ($object, $error, @details) {
warn "Request failed: $error\n";
},
on_finish => sub { $loop->stop },
);
```
Use `request(method => 'PATCH', url => $url, ...)` when you need another HTTP method.
docs/GETTING-STARTED.md view on Meta::CPAN
This callback can go into the server constructor from the README:
```perl
on_request => sub ($tx, $req) {
if ($req->method eq 'GET' && $req->target eq '/') {
$tx->respond(status => 200, body => "Welcome\n");
}
elsif ($req->method eq 'POST' && $req->target eq '/echo') {
$tx->respond(
status => 200,
headers => [['content-type', 'text/plain']],
body => $req->body,
);
}
else {
$tx->respond(status => 404, body => "Not found\n");
}
},
```
`target` contains the request path and, if present, its query string. For example, `/items?page=2` is different from `/items`. Routing and query parsing are up to the application; this module is not a web framework.
lib/Linux/Event/HTTP/Client.pm view on Meta::CPAN
=head1 MAKING REQUESTS
Call C<get>, C<post>, C<put>, C<head>, or C<delete> with a URL
followed by options. Each call returns immediately; the request is
handled while the loop runs.
To send a POST with a body and headers:
$client->post(
'https://example.com/messages',
headers => [['content-type', 'text/plain']],
body => "Hello\n",
on_response => sub ($tx, $res) {
say $res->status;
say $res->body;
},
);
Headers are an array reference of name/value pairs. For other HTTP
methods, use C<request>:
lib/Linux/Event/HTTP/Client.pm view on Meta::CPAN
},
);
=head1 RESPONSES AND CALLBACKS
C<on_response> receives a transaction C<$tx> and a response C<$res>.
The response is a L<Uniform::HTTP::Response> object. Common methods are:
$res->status; # HTTP status code
$res->body; # Response body
$res->header('content-type'); # A header value
HTTP 404 and 500 are valid responses, not connection errors. Check
C<$res-E<gt>status> to decide how to handle them.
Response bodies are not automatically decompressed. If you want to
request plain content, pass C<headers =E<gt> [['accept-encoding',
'identity']]> in the request. Servers can still send compressed
content, so check the C<Content-Encoding> response header and
decode the body yourself when necessary.
lib/Linux/Event/HTTP/Server.pm view on Meta::CPAN
my $loop = Linux::Event::Loop->new;
my $server = Linux::Event::HTTP::Server->new(
loop => $loop,
host => '127.0.0.1',
port => 8080,
on_request => sub ($tx, $req) {
$tx->respond(
status => 200,
headers => [['content-type', 'text/plain']],
body => "Hello from Linux::Event!\n",
);
},
);
say "Listening on http://127.0.0.1:8080/";
$loop->run;
Save this as C<server.pl> and run C<perl server.pl>. In another
terminal, try:
lib/Linux/Event/HTTP/Server.pm view on Meta::CPAN
=item * C<$req> - the request, with its method, URL target, headers, and body.
=item * C<$tx> - the transaction you use to send a response.
=back
Read the request with:
$req->method; # GET, POST, ...
$req->target; # /items?color=blue
$req->header('content-type'); # A request header
$req->body; # Complete request body
C<target> includes the query string. This module does not provide
a router; you can inspect the method and target yourself.
For example, replace C<on_request> in the first example with:
on_request => sub ($tx, $req) {
if ($req->method eq 'GET' && $req->target eq '/') {
$tx->respond(status => 200, body => "Welcome\n");
lib/Linux/Event/HTTP/Server.pm view on Meta::CPAN
else {
$tx->respond(status => 404, body => "Not found\n");
}
},
Use C<$tx-E<gt>respond> to send the status, headers, and body.
Headers are an array reference of name/value pairs:
$tx->respond(
status => 200,
headers => [['content-type', 'text/html; charset=utf-8']],
body => '<h1>Hello</h1>',
);
=head1 RECEIVING LARGE REQUESTS
By default, the server collects each request body before calling
C<on_request>. The body has a 1 MiB limit by default. A larger
non-streamed body fails rather than using unlimited memory.
If an upload might be large, receive it a chunk at a time. Supply
tools/http2-h2spec-server.pl view on Meta::CPAN
tls => {
cert_file => $cert,
key_file => $key,
},
# Compare early response against response after the full request.
# Both modes retain identical status/body; no engine behavior changes.
on_headers => sub ($tx, $request) {
$tx->on_data(sub { });
if ($response_mode eq 'headers') {
$tx->respond(status => 200,
headers => [['content-type', 'text/plain']], body => "unblock-h2spec-ok\n");
}
},
on_request => sub ($tx, $request) {
if ($response_mode eq 'request') {
$tx->respond(status => 200,
headers => [['content-type', 'text/plain']], body => "ok\n");
}
},
);
$| = 1;
say "READY " . $server->host . ":" . $server->port;
$loop->run;
( run in 1.654 second using v1.01-cache-2.11-cpan-99fa8243a42 )