Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #! /usr/bin/perl
- use 5.016;
- use Socket ':all';
- use File::Spec;
- use File::Basename;
- use File::Copy;
- use Cwd;
- use FindBin;
- use Getopt::Long;
- use lib "$FindBin::Bin/lib",glob("$FindBin::Bin/../libs/*/lib"),;
- use LocalProcessingInput;
- use JSON;
- use AnyEvent::Socket;
- use AnyEvent::Handle;
- binmode(STDOUT, ':utf8');
- my $dir = @ARGV[0];
- my $root = "$dir";
- my $address = '127.0.0.1';
- my %headers;
- my $port = @ARGV[1];
- sub isin {
- my $path = shift;
- my $cur = 0;
- foreach(grep{!(($_ eq "")||($_ eq "."))}(split /\//,$path)){
- if($_ eq ".."){
- $cur--;
- last if $cur < 0;
- }else{
- $cur++;
- };
- };
- if($cur < 0){
- return 0;
- }else{
- return 1;
- };
- }
- our %state;
- tcp_server $address, $port, sub {
- my $cln = shift;
- say "Connected to client";
- my $h; $h = AnyEvent::Handle->new(
- fh => $cln,
- on_error => sub {
- $h->destroy;
- warn "Client disconnected: $!";
- },
- );
- my $reader; $reader = sub {
- $h->push_read(line => sub {
- if($_[1] =~ m/^([A-Z]+)\s+\/(.*)\s+HTTP/){
- ($headers{method}, $headers{path}) = ($1, $2);
- $reader->();
- }elsif($_[1] =~ m/^([\w-]+):\s+(.+?)$/){
- $headers{lc($1)} = $2;
- $reader->();
- }else{
- say "Client's method:\t"."'$headers{method}'";
- say "Client's path:\t".$root."/".$headers{path};
- if (($headers{method} eq "POST") && ($headers{"content-type"} eq "application/json")){
- use DDP;
- p %headers;
- my $left = $headers{"content-length"};
- my $json;
- $h->push_write("HTTP/1.1 100 Continue\nContent-Length: 0\n\n");
- my $read_data; $read_data = sub {
- $h->unshift_read(chunk => $left>=$h->{read_size} ? $h->{read_size} : $left, sub {
- $json .= $_[1];
- $left -= length $_[1];
- if($left){
- $read_data->();
- }else{
- my $uncode_json_ref = decode_json($json);
- if (!(exists $uncode_json_ref->{v})){
- $uncode_json_ref->{v} = 0;
- }
- say "command\t $uncode_json_ref->{cmd}";
- 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);
- say $output_to_clnt;
- $h->push_write("HTTP/1.1 200 OK\nContent-Length: ".length($output_to_clnt)."\n\n$output_to_clnt\n");
- say "done";
- undef $read_data;
- };
- });
- };$read_data->();
- }elsif ($headers{method} eq 'GET'){
- if (-f $root."/".$headers{path}){
- my ($size,$data);
- my $left = -s $root.'/'.$headers{path};
- open ( my $fh , '<:raw' , $root ."/". $headers{path} ) or do {
- say "ERROR: $!";
- my $err = "Could not open file ' $root"."/"."$headers{path} ' $! ";
- $h->push_write("HTTP/1.1 404 Not Found\nContent-Length: " . length ( $err ). "\n\n $err \n");
- last;
- };
- $h->push_write("HTTP/1.1 200 OK\nContent-Length: ".$left."\n\n");
- my $fhrite_data; $fhrite_data = sub {
- $size = $left >= $h->{read_size} ? $h->{read_size} : $left;
- sysread($fh, $data, $size) or warn "Sysread: $!";
- $h->push_write($data);
- $left -= $size;
- if($left>0){
- if($h->{wbuf}){
- say "Buffer is not empty. Left $left bytes";
- $h->on_drain(sub {
- $h->on_drain(undef);
- $fhrite_data->();
- });
- }else{
- $fhrite_data->();
- };
- }else{
- undef $fhrite_data;
- close($fh) or warn "Close file: $!";
- };
- };$fhrite_data->();
- }elsif(-d $root."/".$headers{path}){
- opendir DIR , $root."/".$headers{path} or do {
- say "ERROR: $!";
- my $err = "Could not open directory ' $root"."/"."$headers{path} ' $! ";
- $h->push_write("HTTP/1.1 404 Not Found\nContent-Length: " . length ( $err ). "\n\n $err \n");
- return;
- };
- my $data;
- while(my $fname = readdir DIR) {
- next if ($fname eq ".") or($fname eq "..");
- $data .= "$fname\n";
- }
- closedir DIR or warn "$!";
- $h->push_write("HTTP/1.1 200 OK\nContent-Length: ".length($data)."\n\n$data \n");
- }
- }elsif ($headers{method} eq 'PUT') {
- if (-f $root."/".$headers{path}){
- my $err = "file '$root".'/'."$headers{path}' already exsists!\n";
- $h->push_write("HTTP/1.1 412 Precondition Failed\nContent-Length: ".length($err). "\n\n$err\n");
- return;
- }
- my $left = $headers{"content-length"};
- open(my $fh, '>:raw', $root."/".$headers{path}) or do {
- my $err = "Could not create file '$root".'/'."$headers{path}' $!";
- $h->push_write("HTTP/1.1 404 Not Found\nContent-Length: ".length($err). "\n\n$err\n");
- return;
- };
- $h->push_write("HTTP/1.1 100 Continue\nContent-Length: 0\n\n");
- my $read_data; $read_data = sub {
- $h->unshift_read(chunk => $left>=$h->{read_size} ? $h->{read_size} : $left, sub {
- my (undef, $data) = @_;
- syswrite($fh, $data) or warn "Syswrite: $!";
- $left -= length $data;
- if($left){
- $read_data->();
- }else{
- undef $read_data;
- close($fh) or warn "Close: $!";
- $h->push_write("HTTP/1.1 200 OK\nContent-Length: 0\n\n");
- };
- });
- };$read_data->();
- }elsif ($headers{method} =~ /DELETE/) {
- my $file_to_del = $root."/".$headers{path};
- unlink ($file_to_del) or do {
- my $err = "Could not delete $file_to_del: $!";
- $h->push_write("HTTP/1.1 412 Precondition Failed\nContent-Length: ".length($err)."\n\n$err\n");
- return;
- };
- $h->push_write("HTTP/1.1 200 OK\n"."Content-Length: 0\n\n");
- }else{
- $h->push_write("HTTP/1.1 400 Bad Request\nContent-Length: 0\n\n");
- };
- $reader->();
- }
- });
- };$reader->();
- };
- AE::cv->recv;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement