Skip to content

Commit

Permalink
update refrence implentation with net device
Browse files Browse the repository at this point in the history
  • Loading branch information
psema4 committed Nov 19, 2011
1 parent fece286 commit bb7ce0c
Show file tree
Hide file tree
Showing 17 changed files with 2,439 additions and 144 deletions.
5 changes: 3 additions & 2 deletions index.html

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions net-example/hosted/test-file
@@ -0,0 +1 @@
Hello, World
100 changes: 100 additions & 0 deletions net-example/netdevice.cgi
@@ -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];
87 changes: 87 additions & 0 deletions net-example/netdevice.php
@@ -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
?>

0 comments on commit bb7ce0c

Please sign in to comment.