Skip to content

Instantly share code, notes, and snippets.

@zwetan
Created June 19, 2015 11:37
Show Gist options
  • Save zwetan/26c15af3dcb96c15322a to your computer and use it in GitHub Desktop.
Save zwetan/26c15af3dcb96c15322a to your computer and use it in GitHub Desktop.
nodeas replica
import redbean.*;
compile( "nodeas.as" );
package nodeas.http
{
public class HttpRequest
{
private var _request:String;
public function HttpRequest():void
{
super();
}
public function setRequest( in_str:String ):void
{
trace( in_str );
_request = in_str;
}
public function getPath():String
{
return _request.substring( 4, _request.indexOf("HTTP/") - 1 );
}
}
}
package nodeas.http
{
import nodeas.socket.Socket;
public class HttpResponse
{
private var _handle:uint;
public function HttpResponse():void
{
super();
}
private function createHeader():String
{
//return "HTTP/1.0 200 OK\r\nContent-Length: 20\r\nContent-Type: text/html\r\n\r\n";
return "HTTP/1.0 200 OK\r\nContent-Length: 200\r\nContent-Type: text/html\r\n\r\n";
}
public function setConnectHandle( in_handle:uint ):void
{
_handle = in_handle;
}
public function send( in_html:String, in_status:uint = 200 ):void
{
var header:String = createHeader();
Socket.send( _handle, header + in_html );
}
}
}
package nodeas.http
{
import nodeas.socket.Socket;
public class HttpServer
{
private var _socket:uint;
private var _connfd:uint;
private var _response:HttpResponse;
private var _request:HttpRequest;
public function HttpServer( in_port:uint ):void
{
super();
trace( "HttpServer is starting at port:" + in_port );
_socket = Socket.startlisten( in_port );
_response = new HttpResponse();
_request = new HttpRequest();
}
public function start( in_fn:Function ):void
{
for (;;)
{
_connfd = Socket.accept( _socket );
_response.setConnectHandle( _connfd );
_request.setRequest( Socket.receive( _connfd ) );
in_fn.call( this, _request, _response );
}
}
public function stop():void
{
Socket.close( _socket );
}
}
}
#!/usr/bin/as3shebang --
import shell.*;
var nodeas:* = Domain.currentDomain.load( "nodeas.abc" );
trace( nodeas + " loaded" );
import nodeas.socket.*;
import nodeas.http.*;
trace("Start!");
var test_html1:String = "<div style=\"color:#f00;\">This is in html1</div>\r\n";
var test_html2:String = "<div style=\"color:#f00;\">This is in html2</div>\r\n";
var html_404:String = "<div style=\"color:#f00;\">Not found</div>\r\n";
function send_socket(in_request:* /*HttpRequest*/, in_response:* /*HttpResponse*/)
{
var path = in_request.getPath();
trace( "path = [" + path + "]" );
if(path == "/") {
in_response.send(test_html1);
} else if (path == "/test.html") {
in_response.send(test_html2);
} else {
in_response.send(html_404, 404);
}
};
try {
var server:* /*HttpServer*/ = new HttpServer(5689);
server.start(send_socket);
server.stop();
} catch (error: Error) {
trace("catch");
trace(error.toString());
}
include "Socket.as";
include "HttpRequest.as";
include "HttpResponse.as";
include "HttpServer.as";
"nodeas 0.1"
package nodeas.socket
{
import C.sys.socket.*; // socket(), bind(), listen(), accept()
import C.netinet.*; // sockaddr_in
import C.arpa.inet.*; // htonl(), htons()
import C.unistd.*; // close()
import flash.utils.ByteArray;
public class Socket
{
public function Socket()
{
super();
}
public static function startlisten( port:uint ):uint
{
var listenfd:int;
var local:sockaddr_in = new sockaddr_in();
listenfd = socket( AF_INET, SOCK_STREAM, 0 );
local.sin_family = AF_INET;
local.sin_addr.s_addr = htonl(INADDR_ANY);
//local.sin_addr = htonl( INADDR_ANY );
local.sin_port = htons( port );
bind( listenfd, local );
listen( listenfd, 5 );
trace( "started listening port " + port );
return listenfd;
}
public static function accept( socketid: uint ):uint
{
var connfd:int;
var client:sockaddr_in = new sockaddr_in();
connfd = C.sys.socket.accept( socketid, client );
trace( "accepted connection from " + inet_ntoa( client.sin_addr ) );
return connfd;
}
public static function send( connfd:uint, data:String ):uint
{
var bytes:ByteArray = new ByteArray();
bytes.writeUTFBytes( data );
bytes.position = 0;
//var sent:int = C.sys.socket.send( connfd, bytes );
//return sent;
// handle partial sends
var sent:int = C.sys.socket.sendall( connfd, bytes );
return sent;
}
public static function receive( connfd:uint ):String
{
var bytes:ByteArray = new ByteArray();
var received:int = recv( connfd, bytes );
trace( "received: " + received + " bytes" );
trace( "" );
bytes.position = 0;
var str:String = bytes.readUTFBytes( bytes.length );
return str;
}
public static function close( connfd:uint ):void
{
C.unistd.close( connfd );
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment