Skip to content

Instantly share code, notes, and snippets.

@zwetan
Created June 19, 2015 11:37

Revisions

  1. zwetan revised this gist Jun 19, 2015. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion nodeas.as
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,3 @@

    include "Socket.as";
    include "HttpRequest.as";
    include "HttpResponse.as";
  2. zwetan created this gist Jun 19, 2015.
    25 changes: 25 additions & 0 deletions HttpRequest.as
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,25 @@
    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 );
    }
    }

    }
    33 changes: 33 additions & 0 deletions HttpResponse.as
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,33 @@
    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 );
    }

    }

    }
    40 changes: 40 additions & 0 deletions HttpServer.as
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,40 @@
    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 );
    }

    }

    }
    80 changes: 80 additions & 0 deletions Socket.as
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,80 @@
    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 );
    }

    }

    }
    3 changes: 3 additions & 0 deletions build.as3
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,3 @@
    import redbean.*;

    compile( "nodeas.as" );
    37 changes: 37 additions & 0 deletions httpserver_sample1
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,37 @@
    #!/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());
    }
    7 changes: 7 additions & 0 deletions nodeas.as
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,7 @@

    include "Socket.as";
    include "HttpRequest.as";
    include "HttpResponse.as";
    include "HttpServer.as";

    "nodeas 0.1"