AlexP has asked for the wisdom of the Perl Monks concerning the following question:
Hi monks!Today I have some free time and immediately took up the study of perl.
And stuck with creating simple http(tcp) server.
Problem with response.
So, this is my code:
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 <br> Uri: $uri <br> Protocol: $prot +ocol <br>"; $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);
When using curl -v http://localhost:8080, I get the response:
< 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 <br>Hi there* Closing connection 0ol: HTTP/1.1
As you can see, there is truncated body.
Can you please tell, what I'am doing wrong?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: TCP server with IO::Socket
by tybalt89 (Monsignor) on Jul 20, 2021 at 21:21 UTC | |
by AlexP (Pilgrim) on Jul 21, 2021 at 09:09 UTC | |
|
Re: TCP server with IO::Socket
by eyepopslikeamosquito (Archbishop) on Jul 21, 2021 at 01:37 UTC | |
by AlexP (Pilgrim) on Jul 21, 2021 at 09:15 UTC | |
|
Re: TCP server with IO::Socket
by Fletch (Bishop) on Jul 21, 2021 at 00:19 UTC | |
by AlexP (Pilgrim) on Jul 21, 2021 at 09:12 UTC |