Advertisement
Guest User

http

a guest
May 25th, 2018
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 5.98 KB | None | 0 0
  1. #! /usr/bin/perl
  2. use 5.016;
  3. use Socket ':all';
  4.  
  5. use File::Spec;
  6. use File::Basename;
  7. use File::Copy;
  8. use Cwd;
  9. use FindBin;
  10. use Getopt::Long;
  11. use lib "$FindBin::Bin/lib",glob("$FindBin::Bin/../libs/*/lib"),;
  12. use LocalProcessingInput;
  13. use JSON;
  14. use AnyEvent::Socket;
  15. use AnyEvent::Handle;
  16. binmode(STDOUT, ':utf8');
  17.  
  18. my $dir = @ARGV[0];
  19. my $root  = "$dir";
  20. my $address = '127.0.0.1';
  21. my %headers;
  22. my $port = @ARGV[1];
  23.  
  24. sub isin {
  25.     my $path = shift;
  26.     my $cur = 0;
  27.     foreach(grep{!(($_ eq "")||($_ eq "."))}(split /\//,$path)){
  28.         if($_ eq ".."){
  29.             $cur--;
  30.             last if $cur < 0;
  31.         }else{
  32.             $cur++;
  33.         };
  34.     };
  35.     if($cur < 0){
  36.         return 0;
  37.     }else{
  38.         return 1;
  39.     };
  40. }
  41.  
  42. our %state;
  43.  
  44. tcp_server $address, $port, sub {
  45.     my $cln = shift;
  46.     say "Connected to client";
  47.     my $h; $h = AnyEvent::Handle->new(
  48.         fh => $cln,
  49.         on_error => sub {
  50.             $h->destroy;
  51.             warn "Client disconnected: $!";
  52.         },
  53.     );
  54.     my $reader; $reader = sub {
  55.         $h->push_read(line => sub {
  56.             if($_[1] =~ m/^([A-Z]+)\s+\/(.*)\s+HTTP/){
  57.                 ($headers{method}, $headers{path}) = ($1, $2);
  58.                 $reader->();
  59.  
  60.             }elsif($_[1] =~ m/^([\w-]+):\s+(.+?)$/){
  61.                 $headers{lc($1)} = $2;
  62.                 $reader->();
  63.  
  64.             }else{
  65.                 say "Client's method:\t"."'$headers{method}'";
  66.                 say "Client's path:\t".$root."/".$headers{path};
  67.                 if (($headers{method} eq "POST") && ($headers{"content-type"} eq "application/json")){
  68.                     use DDP;
  69.  
  70.                     p %headers;
  71.                     my $left = $headers{"content-length"};
  72.                     my $json;
  73.                     $h->push_write("HTTP/1.1 100 Continue\nContent-Length: 0\n\n");
  74.                     my $read_data; $read_data = sub {
  75.                         $h->unshift_read(chunk => $left>=$h->{read_size} ? $h->{read_size} : $left, sub {
  76.                             $json .= $_[1];
  77.                             $left -= length $_[1];
  78.                             if($left){
  79.                                 $read_data->();
  80.                             }else{
  81.  
  82.                                 my $uncode_json_ref =  decode_json($json);
  83.                                 if (!(exists $uncode_json_ref->{v})){
  84.                                     $uncode_json_ref->{v} = 0;
  85.                                 }
  86.                                 say "command\t $uncode_json_ref->{cmd}";
  87.                                 my $output_to_clnt = LocalProcessingInput->new($uncode_json_ref->{cmd}." ".$uncode_json_ref->{path})->processing($uncode_json_ref->{cmd}." ".$uncode_json_ref->{path},$uncode_json_ref->{v},$root);
  88.                                 say $output_to_clnt;
  89.                                 $h->push_write("HTTP/1.1 200 OK\nContent-Length: ".length($output_to_clnt)."\n\n$output_to_clnt\n");
  90.                                 say "done";
  91.                                 undef $read_data;
  92.  
  93.                             };
  94.                         });
  95.                     };$read_data->();
  96.                 }elsif ($headers{method} eq 'GET'){
  97.                     if (-f $root."/".$headers{path}){
  98.  
  99.                         my ($size,$data);
  100.                         my $left = -s $root.'/'.$headers{path};
  101.                         open ( my $fh , '<:raw' , $root ."/". $headers{path} ) or do  {
  102.                             say "ERROR: $!";
  103.                             my $err  = "Could not open file ' $root"."/"."$headers{path} ' $! ";
  104.                             $h->push_write("HTTP/1.1 404 Not Found\nContent-Length: " . length ( $err ). "\n\n $err \n");
  105.                             last;
  106.                         };
  107.  
  108.                         $h->push_write("HTTP/1.1 200 OK\nContent-Length: ".$left."\n\n");
  109.                         my $fhrite_data; $fhrite_data = sub {
  110.                             $size = $left >= $h->{read_size} ? $h->{read_size} : $left;
  111.                             sysread($fh, $data, $size) or warn "Sysread: $!";
  112.                             $h->push_write($data);
  113.                             $left -= $size;
  114.                             if($left>0){
  115.                                 if($h->{wbuf}){
  116.                                     say "Buffer is not empty. Left $left bytes";
  117.                                     $h->on_drain(sub {
  118.                                         $h->on_drain(undef);
  119.                                         $fhrite_data->();
  120.                                     });
  121.                                 }else{
  122.                                     $fhrite_data->();
  123.                                 };
  124.                             }else{
  125.                                 undef $fhrite_data;
  126.                                 close($fh) or warn "Close file: $!";
  127.                             };
  128.                         };$fhrite_data->();
  129.  
  130.                     }elsif(-d $root."/".$headers{path}){
  131.  
  132.                         opendir DIR ,  $root."/".$headers{path}  or do  {
  133.                             say "ERROR: $!";
  134.                             my $err  = "Could not open directory ' $root"."/"."$headers{path} ' $! ";
  135.                             $h->push_write("HTTP/1.1 404 Not Found\nContent-Length: " . length ( $err ). "\n\n $err \n");
  136.                             return;
  137.                         };
  138.  
  139.                         my $data;
  140.                         while(my $fname = readdir DIR) {
  141.                             next if ($fname eq ".") or($fname eq "..");
  142.                             $data  .= "$fname\n";
  143.                         }
  144.                         closedir DIR or warn "$!";
  145.                         $h->push_write("HTTP/1.1 200 OK\nContent-Length: ".length($data)."\n\n$data \n");
  146.                     }
  147.                 }elsif ($headers{method} eq 'PUT') {
  148.                     if (-f $root."/".$headers{path}){
  149.                         my $err = "file '$root".'/'."$headers{path}' already exsists!\n";
  150.                         $h->push_write("HTTP/1.1 412 Precondition Failed\nContent-Length: ".length($err). "\n\n$err\n");
  151.                         return;
  152.                     }
  153.                     my $left = $headers{"content-length"};
  154.                     open(my $fh, '>:raw', $root."/".$headers{path}) or do {
  155.                         my $err = "Could not create file '$root".'/'."$headers{path}' $!";
  156.                         $h->push_write("HTTP/1.1 404 Not Found\nContent-Length: ".length($err). "\n\n$err\n");
  157.                         return;
  158.                     };
  159.                     $h->push_write("HTTP/1.1 100 Continue\nContent-Length: 0\n\n");
  160.                     my $read_data; $read_data = sub {
  161.                         $h->unshift_read(chunk => $left>=$h->{read_size} ? $h->{read_size} : $left, sub {
  162.                             my (undef, $data) = @_;
  163.                             syswrite($fh, $data) or warn "Syswrite: $!";
  164.                             $left -= length $data;
  165.                             if($left){
  166.                                 $read_data->();
  167.                             }else{
  168.                                 undef $read_data;
  169.                                 close($fh) or warn "Close: $!";
  170.                                 $h->push_write("HTTP/1.1 200 OK\nContent-Length: 0\n\n");
  171.                             };
  172.                         });
  173.                     };$read_data->();
  174.  
  175.                 }elsif ($headers{method} =~ /DELETE/) {
  176.                     my $file_to_del = $root."/".$headers{path};
  177.                     unlink ($file_to_del) or do {
  178.                         my $err  = "Could not delete $file_to_del: $!";
  179.                         $h->push_write("HTTP/1.1 412 Precondition Failed\nContent-Length: ".length($err)."\n\n$err\n");
  180.                         return;
  181.                     };
  182.                     $h->push_write("HTTP/1.1 200 OK\n"."Content-Length: 0\n\n");
  183.                 }else{
  184.                     $h->push_write("HTTP/1.1 400 Bad Request\nContent-Length: 0\n\n");
  185.                 };
  186.                 $reader->();
  187.             }
  188.         });
  189.     };$reader->();
  190. };
  191.  
  192. AE::cv->recv;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement