Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

TCP server with IO::Socket

by AlexP (Pilgrim)
on Jul 20, 2021 at 18:29 UTC ( [id://11135224]=perlquestion: print w/replies, xml ) Need Help??

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

    Use

    my ($method, $uri, $protocol) = $request_line =~ /(\V+) \040 (\V+) \04 +0 (\V+)/x;

    instead of

    my ($method, $uri, $protocol) = $request_line =~ /(.+) \040 (.+) \040 +(.+)/x;

    You were matching a \r with . which caused the output to overwrite itself.

      Huh! So weird. Thank you so mush =) It's a good lesson.

      I guess chomp might also help or just split first line by space.

Re: TCP server with IO::Socket
by Fletch (Bishop) on Jul 21, 2021 at 00:19 UTC

    Whenever you get weird terminal output and carriage returns may be in the mix that's one of the first things to look for; piping things through od -xa or the like may be elucidating.

    Also while you're doing this as a learning exercise be aware there's lots of well tested HTTP servers on CPAN and you'd probably be better served by one of them for anything beyond pedagogical experimenting. Things like Mojolicoious or Dancer2 give you a complete stack, whereas HTTP::Daemon is lower level; or look at POE::Filter::HTTPD for another approach.

    The cake is a lie.
    The cake is a lie.
    The cake is a lie.

      od -xa looks very helpful. Thanks.

      Yes, mojo and dancer are my following goals!

Re: TCP server with IO::Socket
by eyepopslikeamosquito (Archbishop) on Jul 21, 2021 at 01:37 UTC

      Thanks for the support, I'am enjoying perl more and more with every iteration!

      Your link looks very interesting and promising great time to study it.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11135224]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (5)
As of 2024-04-25 19:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found