Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re^5: Adding simple HTTP controls to existing code

by huck (Prior)
on Jul 05, 2021 at 18:33 UTC ( [id://11134677]=note: print w/replies, xml ) Need Help??


in reply to Re^4: Adding simple HTTP controls to existing code
in thread Adding simple HTTP controls to existing code

Mine is a little different

use strict; use warnings; select (STDOUT); $| = 1; select(STDOUT); # http://127.0.0.1/exit # http://127.0.0.1/hi # http://127.0.0.1/null # http://127.0.0.1/unknown use IO::Handle; use IO::Socket; use IO::Select; my $server = IO::Socket::INET->new( Proto => 'tcp', PeerAddr => "127.0.0.1", # only + us LocalPort => 80, Listen => SOMAXCONN, Reuse => 1); die "can't setup server on 80" unless $server; my $sel_read = IO::Select->new(); my $sel_write = IO::Select->new(); my $sel_error = IO::Select->new(); $sel_read-> add($server); my $cmdrun={}; $cmdrun->{'/exit'} =sub{exit;} ; $cmdrun->{'/hi'} =sub{my $fh=shift; print $fh "\nhi\n";} ; $cmdrun->{'/null'} =sub{} ; while(1) { checkforrequests(); } # superloop exit; sub checkforrequests { my $timeout=1; my ($rd_ptr,$wr_ptr,$er_ptr)=IO::Select->select($sel_read,undef,$s +el_error,$timeout); for my $fh (@$rd_ptr) { if ($fh == $server) { my $client = $server->accept; $client->autoflush(1); $sel_read->add($client); } # server else { my $line=<$fh>; print "line:$line"; my ($junk,$post)=split(' ',$line,3); my $cmd=$cmdrun->{$post}; if ($cmd) {&$cmd($fh);} else { print $fh "\n\nNO\n"; } $sel_read->remove($fh); close $fh; } # client } # for fh } # checkforrequests

Replies are listed 'Best First'.
Re^6: Adding simple HTTP controls to existing code
by brachism (Novice) on Jul 05, 2021 at 19:25 UTC

    Thanks Huck.

    I was able to quickly adapt Tybalt89's suggestion in my code. I will give your suggestion a try as well.

    Comparing and contrasting different solutions while learning is always helpful. Thanks again

      They have a similar base

      Mine does a two step between accept and read, a client CAN create the connection and wait a bit before writing, so mine wont block there. But in general http: clients shouldnt matter.

      His has a better read, the way i commonly use it is a lot like his, but i just collect data, then have another loop that looks for completed lines so i dont block on the \n. IN both of these examples we block on the \n read, mine till \n, his to blank line.

      I added a cmd lookup table for what to execute

      I only look at the first line sent.

      I added the /null command to show you what might happen if you dont send anything back

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (3)
As of 2024-04-18 03:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found