- #! / usr / bin / perl
- use LWP :: Socket ;
- $ headers = "HTTP / 1.1 200 OK \ r \ n Content-Type: text / html \ r \ n \ r \ n " ;
- $ sock = new LWP :: Socket ( ) ;
- $ sock -> bind ( '127.0.0.1' , '8080' ) ;
- $ sock -> listen ( 10 ) ;
- while ( $ socket = $ sock -> accept ( 10 ) ) {
- $ content = "Hello from Habr" ;
- $ file_name ; $ socket -> read ( \ $ file_name ) ;
- $ file_name = ~ s / GET \ / ([^] *) HTTP. + / $ 1 / s ;
- if ( open FILE , '<' , $ file_name ) {
- $ content = join "" , <FILE> ; close FILE ;
- }
- $ socket -> write ( $ headers . $ content ) ;
- $ socket -> shutdown ( ) ;
- }
- #! / usr / bin / perl
- use strict ;
- use warnings ;
- use LWP :: Socket ;
- my $ headers = "HTTP / 1.1 200 OK \ r \ n Content-Type: text / html \ r \ n \ r \ n " ;
- my $ sock = new LWP :: Socket ( ) ;
- die "Can't bind a socket" unless $ sock -> bind ( '127.0.0.1' , '8080' ) ;
- $ sock -> listen ( 10 ) ;
- while ( my $ socket = $ sock -> accept ( 10 ) ) {
- my $ content = "Hello from Habr" ;
- my $ file_name ;
- $ socket -> read ( \ $ file_name ) ;
- $ file_name = ~ s / GET \ / ([^] *) HTTP. + / $ 1 / s ;
- if ( - f $ file_name and open FILE , '<' , $ file_name ) {
- $ content = join "" , <FILE> ;
- close FILE ;
- }
- $ socket -> write ( $ headers . $ content ) ;
- $ socket -> shutdown ( ) ;
- }
- $ sock -> shutdown ( ) ;
use LWP::Socket;
my $sock = new LWP::Socket();
$sock->bind('127.0.0.1', '8080');
$sock->listen(10);
while ( my $socket = $sock->accept(10) ) {
$socket->read( \$file_name );
$file_name =~ s/GET \/([^ ]*) HTTP.+/$1/s;
$socket->write( $headers . $content );
$socket->shutdown();
- # ...
- use LWP :: Socket ;
- use FCGI :: ProcManager qw / pm_manage pm_pre_dispatch pm_post_dispatch / ;
- my $ headers = "HTTP / 1.1 200 OK \ r \ n Content-Type: text / html \ r \ n \ r \ n " ;
- my $ sock = new LWP :: Socket ( ) ;
- die "Can't bind a socket" unless $ sock -> bind ( '127.0.0.1' , '8080' ) ;
- $ sock -> listen ( 10 ) ;
- pm_manage ( n_processes => 5 ) ;
- while ( my $ socket = $ sock -> accept ( 10 ) ) {
- pm_pre_dispatch ( ) ;
- my $ content = "Hello from Habr" ;
- # ...
- $ socket -> shutdown ( ) ;
- pm_post_dispatch ( ) ;
- }
- $ sock -> shutdown ( ) ;
- #! / usr / bin / perl
- use strict ;
- use warnings ;
- use LWP :: Socket ;
- use FCGI :: ProcManager qw / pm_manage pm_pre_dispatch pm_post_dispatch / ;
- # Prepare headers
- my $ headers = "HTTP / 1.1% d OK \ r \ n "
- . "Server: FakeServer / 2009-09-12 \ r \ n "
- . "Content-Type: text / html \ r \ n "
- . "Content-Length:% d \ r \ n "
- . "Connection: close \ r \ n \ r \ n " ;
- # Prepare and open socket
- my $ sock = new LWP :: Socket ( ) ;
- die "Can't bind a socket" unless $ sock -> bind ( '127.0.0.1' , '8080' ) ;
- $ sock -> listen ( 10 ) ;
- # Create 5 childs
- pm_manage ( n_processes => 5 ) ;
- # Accepts a new connection
- while ( my $ socket = $ sock -> accept ( 10 ) ) {
- # Passing direction to child
- pm_pre_dispatch ( ) ;
- # Default content
- my $ content = "<html> <body> <h1> Hello from Habr </ h1> </ body> </ html>" ;
- my $ stat = 200 ;
- my $ file_name ;
- # Read from socket
- $ socket -> read ( \ $ file_name ) ;
- # Get wanted file name
- $ file_name = ~ s / GET \ / ([^] *) HTTP. + / $ 1 / s ;
- if ( $ file_name ) {
- if ( - f $ file_name and open FILE , '<' , $ file_name ) {
- # Read from file
- $ content = join "" , <FILE> ;
- close FILE ;
- }
- else {
- $ content = "File not found" ;
- $ stat = 404 ;
- }
- }
- # Puts headers and content into the socket
- $ socket -> write ( sprintf ( $ headers , $ stat , length $ content ) ) ;
- $ socket -> write ( $ content ) ;
- $ socket -> shutdown ( ) ;
- # Child's work complete
- pm_post_dispatch ( ) ;
- }
- # Close socket
- $ sock -> shutdown ( ) ;
Source: https://habr.com/ru/post/69411/
All Articles