Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
update refrence implentation with net device
- Loading branch information
Showing
17 changed files
with
2,439 additions
and
144 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Hello, World |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
#!/usr/bin/env perl | ||
|
||
# Example only, NOT intended for production use | ||
# | ||
# Be sure to create a folder (called "hosted" or whatever you set $file_root to) and give the webserver write access to it! | ||
|
||
use strict; | ||
use CGI; | ||
use JSON; | ||
use Switch; | ||
|
||
# define file storage area | ||
my $file_root = './hosted'; | ||
|
||
# setup CGI, get CGI paramenters and setup JSON | ||
my $q = new CGI; | ||
my @params = $q->param; | ||
my $json = JSON->new->allow_nonref; | ||
|
||
# determine command | ||
my $cmd = $q->param('cmd') || 'ping'; | ||
my $subcmd = $q->param('subcmd') || 'NOP'; | ||
|
||
my $result_text = ''; | ||
switch ($cmd) { | ||
case "ping" { $result_text = 'pong'; } | ||
|
||
case "time" { $result_text = localtime; } | ||
|
||
case "file" { | ||
my $path = $q->param('path') || 'NOP'; | ||
$path =~ s/\.+//g; # strip relative paths | ||
$path =~ s/`//g; # strip for `cmd` | ||
$path =~ s/;//g; # strip for ; cmd | ||
$path =~ s/^\///g; # strip leading / | ||
|
||
if ($path ne 'NOP') { | ||
switch ($subcmd) { | ||
case "read" { | ||
if ( -f "$file_root/$path" ) { | ||
open(FILE, "< $file_root/$path") || die "Content-type: text/plain\n\nCAN'T READ $file_root/$path: $!"; | ||
my @file = <FILE>; | ||
close FILE; | ||
$result_text = join('', @file); | ||
} | ||
} | ||
|
||
case "write" { | ||
my $buf = $q->param('buffer'); | ||
$result_text = "fail"; | ||
|
||
if ( -f "$file_root/$path" ) { | ||
open(FILE, "> $file_root/$path") || die "Content-type: text/plain\n\nCAN'T WRITE $file_root/$path: $!"; | ||
print FILE $buf; | ||
close FILE; | ||
$result_text = "ok"; | ||
} | ||
} | ||
|
||
case "append" { | ||
my $buf = $q->param('buffer'); | ||
$result_text = "fail"; | ||
|
||
if ( -f "$file_root/$path" ) { | ||
open(FILE, ">> $file_root/$path") || die "Content-type: text/plain\n\nCAN'T APPEND $file_root/$path: $!"; | ||
print FILE $buf; | ||
close FILE; | ||
$result_text = "ok"; | ||
} | ||
} | ||
|
||
case "create" { | ||
`touch $file_root/$path`; | ||
$result_text = ( -f "$file_root/$path" ) ? 'ok' : 'fail'; | ||
} | ||
|
||
case "delete" { | ||
`rm $file_root/$path`; | ||
$result_text = ( -f "$file_root/$path" ) ? 'fail' : 'ok'; | ||
} | ||
|
||
else { | ||
$result_text = "unknown file command"; | ||
} | ||
} | ||
} | ||
} | ||
|
||
else { $result_text = 'not supported' } | ||
} | ||
|
||
my %result = ( | ||
cmd => $cmd, | ||
subcmd => $subcmd, | ||
data => $result_text, | ||
); | ||
|
||
# encode response as JSON and send it | ||
my $response = $json->encode(\%result); | ||
print qq[Content-type: application/json\n\n$response]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
<?php | ||
|
||
# Example only, NOT intended for production use | ||
# | ||
# Be sure to create a folder (called "hosted" or whatever you set $file_root to) and give the webserver write access to it! | ||
|
||
$self_file = __FILE__; | ||
$file_root = str_replace(basename($self_file), '' , $self_file) . 'hosted'; | ||
|
||
$cmd = $_REQUEST['cmd']; | ||
$subcmd = $_REQUEST['subcmd']; | ||
|
||
$result_text = ''; | ||
switch ($cmd) { | ||
case "ping": | ||
$result_text = 'pong'; | ||
break; | ||
|
||
case "time": | ||
$result_text = date(DATE_RFC822); | ||
break; | ||
|
||
case "file": | ||
$path = $_REQUEST['path']; | ||
$path = preg_replace('/\.+/', '', $path); | ||
$path = preg_replace('/`/', '', $path); | ||
$path = preg_replace('/;/', '', $path); | ||
$path = preg_replace('/^\//', '', $path); | ||
|
||
$hosted_path = "$file_root/$path"; | ||
$hosted_size = filesize($hosted_path); | ||
|
||
switch($subcmd) { | ||
case "read": | ||
$fh = fopen("$hosted_path", "r"); | ||
$result_text = fread($fh, $hosted_size); | ||
fclose($fh); | ||
break; | ||
|
||
case "write": | ||
$buf = $_REQUEST['buffer']; | ||
$result_text = 'fail'; | ||
$fh = fopen($hosted_path, "w") or die("CAN'T WRITE $hosted_path"); | ||
fwrite($fh, $buf); | ||
fclose($fh); | ||
$result_text = 'ok'; | ||
break; | ||
|
||
case "append": | ||
$buf = $_REQUEST['buffer']; | ||
$result_text = 'fail'; | ||
$fh = fopen($hosted_path, "a") or die("CAN'T APPEND $hosted_path"); | ||
fwrite($fh, $buf); | ||
fclose($fh); | ||
$result_text = 'ok'; | ||
break; | ||
|
||
case "create": | ||
$result_text = 'fail'; | ||
$fh = fopen($hosted_path, "x"); | ||
if ($fh) { | ||
fclose($fh); | ||
$result_text = 'ok'; | ||
} | ||
break; | ||
|
||
case "delete": | ||
$result_text = 'fail'; | ||
if (unlink($hosted_path)) { | ||
$result_text = 'ok'; | ||
} | ||
break; | ||
} | ||
|
||
break; | ||
} | ||
|
||
$result = array( | ||
'cmd' => $cmd, | ||
'subcmd' => $subcmd, | ||
'data' => $result_text, | ||
'hosted_path' => $hosted_path | ||
); | ||
|
||
$response = json_encode($result); | ||
echo $response | ||
?> |
Oops, something went wrong.