welsonjs/lib/httpserver.js

81 lines
2.2 KiB
JavaScript
Raw Normal View History

2020-10-20 02:41:26 +00:00
////////////////////////////////////////////////////////////////////////
// HTTPServer API
///////////////////////////////////////////////////////////////////////
2020-10-20 05:17:00 +00:00
var HTTPServer = {
_this: this, // Avoid conflicts between HTTPServer and Winsock variables
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"
},
Listener: null,
Connections: {},
2020-10-20 08:58:58 +00:00
CreateWinsockObject: function() {
return CreateObject([
"MSWinsock.Winsock.1",
"MSWinsock.Winsock"
], "listener_");
},
2020-10-20 05:17:00 +00:00
Bind: function(port) {
try {
2020-10-20 08:58:58 +00:00
_this.Listener = _this.CreateWinsockObject();
2020-10-20 05:17:00 +00:00
_this.Listener.localPort = port;
_this.Listener.bind();
_this.Listener.listen();
console.info("Listening port: " + port);
2020-10-20 08:58:58 +00:00
} catch (e) {
2020-12-07 03:48:37 +00:00
console.error("port ", port, " could not bind: ", e.message);
2020-10-20 05:17:00 +00:00
}
},
2020-10-20 08:58:58 +00:00
OnRequest: function(request, response) {
2020-10-20 05:17:00 +00:00
console.log("HTTPServer.OnRequest() dose not implemented");
},
2020-10-20 02:41:26 +00:00
2020-10-20 05:17:00 +00:00
CreateServer: function(OnRequest) {
2020-10-20 08:58:58 +00:00
if (typeof OnRequest !== "function") {
2020-10-20 05:17:00 +00:00
throw new TypeError("OnRequest() must be a function.");
}
_this.OnRequest = OnRequest;
return HTTPServer;
},
ConnectionRequest: function(requestID) {
console.info("Connection request " + requestID);
_this.Connections[requestID] = {
2020-10-20 08:58:58 +00:00
Listener: _this.CreateWinsockObject()
2020-10-20 05:17:00 +00:00
};
_this.Connections[requestID].Listener.accept(requestID);
},
2020-10-20 08:58:58 +00:00
2020-10-20 05:17:00 +00:00
DataArrival: function(length) {
// TODO: DataArrival
},
SendComplete: function() {
// TODO: SendComplete
2020-10-20 02:41:26 +00:00
}
};
2020-10-20 05:17:00 +00:00
global.listener_ConnectionRequest = HTTPServer.ConnectionRequest;
global.listener_DataArrival = HTTPServer.DataArrival;
global.listener_SendComplete = HTTPServer.SendComplete;
2020-10-20 02:41:26 +00:00
2020-10-20 05:17:00 +00:00
exports = HTTPServer;