use strict; use warnings; use IO::Socket::INET; use Date; $| = 1; my $server = IO::Socket::INET->new( LocalPort => 8080, Type => SOCK_STREAM, ReuseAddr => 1, Listen => 5, ); unless($server) { die "Can't start server on port 8080: $! \n"; } while (my $client = $server->accept() ) { $client->autoflush(1); my $request_line = <$client>; my ($method, $uri, $protocol) = $request_line =~ /(.+) \040 (.+) \040 (.+)/x; my $content = "Method: $method
Uri: $uri
Protocol: $protocol
"; $content .= 'Hi there'; my $response = "HTTP/1.1 200 OK \r\n"; my $date = Date->new(time() ); $date = $date->strftime("%a, %d %b %Y %X GMT"); $response .= "Date: $date \r\n"; $response .= "Content-Type: text/html; charset=UTF-8 \r\n"; $response .= "Content-Length: " . length($content) . "\r\n"; $response .= "\r\n"; $response .= $content; print $client $response; shutdown($client, 1); close($client); } close($server); #### < HTTP/1.1 200 OK < Date: Tue, 20 Jul 2021 21:20:38 GMT < Content-Type: text/html; charset=UTF-8 < Content-Length: 61 < * Connection #0 to host localhost left intact
Hi there* Closing connection 0ol: HTTP/1.1