diff --git a/lib/httpserver.js b/lib/httpserver.js index 0e1d364..8aa1e64 100644 --- a/lib/httpserver.js +++ b/lib/httpserver.js @@ -2,39 +2,73 @@ // HTTPServer API /////////////////////////////////////////////////////////////////////// -var SHELL = require("lib/shell"); +var HTTPServer = { + _this: this, // Avoid conflicts between HTTPServer and Winsock variables -var ResponseCodes = { - 100: "Continue", - 200: "OK", - 206: "Partial Content", - 301: "Moved Permanently", - 302: "Found", - 304: "Not Modified", - 400: "Bad Request", - 401: "Unauthorized", - 403: "Forbidden", - 404: "Not Found", - 500: "Internal Server Error", - 503: "Service Unavailable" -}; + StatusCodes: { + 100: "Continue", + 200: "OK", + 206: "Partial Content", + 301: "Moved Permanently", + 302: "Found", + 304: "Not Modified", + 400: "Bad Request", + 401: "Unauthorized", + 403: "Forbidden", + 404: "Not Found", + 500: "Internal Server Error", + 503: "Service Unavailable" + }, -var listener, http = {}; + Listener: null, -var listen = function(port) { - try { - listener = CreateObject("MSWinsock.Winsock.1", "listener_"); - listener.localPort = port; - listener.bind(); - listener.listen(); - console.info("Listening port: " + port); - } catch(e) { - console.error("port " + port " could not bind: " + e.message); + Connections: {}, + + Bind: function(port) { + try { + _this.Listener = CreateObject(["MSWinsock.Winsock.1", "MSWinsock.Winsock"], "listener_"); + _this.Listener.localPort = port; + _this.Listener.bind(); + _this.Listener.listen(); + console.info("Listening port: " + port); + } catch(e) { + console.error("port " + port " could not bind: " + e.message); + } + }, + + OnRequest : function(request, response) { + console.log("HTTPServer.OnRequest() dose not implemented"); + }, + + CreateServer: function(OnRequest) { + if(typeof OnRequest !== "function") { + throw new TypeError("OnRequest() must be a function."); + } + _this.OnRequest = OnRequest; + + return HTTPServer; + }, + + ConnectionRequest: function(requestID) { + console.info("Connection request " + requestID); + + _this.Connections[requestID] = { + Listener: CreateObject(["MSWinsock.Winsock.1", "MSWinsock.Winsock"], "listener_"); + }; + _this.Connections[requestID].Listener.accept(requestID); + }, + + DataArrival: function(length) { + // TODO: DataArrival + }, + + SendComplete: function() { + // TODO: SendComplete } }; -var arrival = function() { - // todo -}; +global.listener_ConnectionRequest = HTTPServer.ConnectionRequest; +global.listener_DataArrival = HTTPServer.DataArrival; +global.listener_SendComplete = HTTPServer.SendComplete; -// todo +exports = HTTPServer;