Permalink
Browse files

initial https support, have a look at HttpTest.hx

  • Loading branch information...
1 parent fe6c905 commit 32c267519f1d88982051fad0dff65ec3ebce4e3d @cloudshift committed Jun 4, 2012
View
@@ -36,6 +36,7 @@ interface HttpServer implements Part<HostPort,String,HttpServer,HttpEvents> {
function notFound(cb:NodeHttpServerReq->NodeHttpServerResp->Void):HttpServer;
function index(indexFile:String):HttpServer;
function serverName(serverName:String):HttpServer;
+ function credentials(key:String,cert:String,?ca:Array<String>):HttpServer;
function root(rootDir:String):HttpServer;
}
@@ -24,11 +24,12 @@ class HttpImpl implements HttpServer,implements Part<HostPort,String,HttpServer,
var _getHandler:String->NodeHttpServerReq->NodeHttpServerResp->Int->Void;
var _routes:Array<{re:EReg,handler:THandler}>;
var _notFound:NodeHttpServerReq->NodeHttpServerResp->Void;
-
+ var _creds:NodeCredDetails;
+
var _index:String;
var _root:String;
var _serverName:String;
-
+
static var readStreamOpt:ReadStreamOpt = cast {
flags: 'r',
mode: 0666
@@ -44,7 +45,7 @@ class HttpImpl implements HttpServer,implements Part<HostPort,String,HttpServer,
new() {
_routes = [];
- _index = "index.html";
+ _index = "/index.html";
_root = null;
_serverName = "Cloudshift 0.2.3";
@@ -59,45 +60,14 @@ class HttpImpl implements HttpServer,implements Part<HostPort,String,HttpServer,
start_(d:HostPort,?oc:Outcome<String,HttpServer>) {
if (oc == null)
oc = Core.outcome();
-
- var server = Node.http.createServer(function(req,resp) {
- var
- url = req.url,
- match = false;
-
- if (_routes != null) {
- for (r in _routes) {
- if (r.re.match(url)) {
- match = true;
- try {
- r.handler(r.re,req,resp);
- } catch(ex:Dynamic) {
- Core.log(E("handler exp:"+ex));
- }
-
- break;
- }
- }
- }
-
- if (!match && _root != null) {
- if (req.method == "GET") {
- if (url == "/") url = _index;
- _getHandler(url,req,resp,200);
- }
- }
- });
- Core.log(I("Starting "+_serverName+" on "+d.host+":"+d.port));
+ var server =
+ if (_creds != null)
+ Node.https.createServer(_creds,requestHandler);
+ else
+ Node.http.createServer(requestHandler);
- /*
- _server.addListener(NodeC.EVENT_HTTPSERVER_REQUEST,function(request,response) {
- notify(Request(request,response));
- });
- */
-
server.listen(d.port,d.host,function() {
-
stop_(function(d) {
var p = Core.outcome();
server.close();
@@ -107,13 +77,48 @@ class HttpImpl implements HttpServer,implements Part<HostPort,String,HttpServer,
return p;
});
+
+ if (_creds != null)
+ Core.log(I("Listening on Https "+_serverName+" on "+d.host+":"+d.port));
+ else
+ Core.log(I("Listening on Http "+_serverName+" on "+d.host+":"+d.port));
oc.resolve(Right(cast(this,HttpServer)));
});
return oc;
}
+ function
+ requestHandler(req:NodeHttpServerReq,resp:NodeHttpServerResp) {
+ var
+ url = req.url,
+ match = false;
+
+ if (_routes != null) {
+ for (r in _routes) {
+ if (r.re.match(url)) {
+ match = true;
+ try {
+ r.handler(r.re,req,resp);
+ } catch(ex:Dynamic) {
+ Core.log(E("handler exp:"+ex));
+ }
+
+ break;
+ }
+ }
+ }
+
+ if (!match && _root != null) {
+ if (req.method == "GET") {
+ if (url == "/") url = _index;
+ _getHandler(url,req,resp,200);
+ }
+ }
+ }
+
+
public function
handler(r:EReg,handler:THandler):HttpServer {
_routes.push({re:r,handler:handler});
@@ -140,11 +145,19 @@ class HttpImpl implements HttpServer,implements Part<HostPort,String,HttpServer,
public function
root(rootDir:String):HttpServer {
- _root = if (!rootDir.endsWith("/")) rootDir + "/" ;
+ _root = if (!rootDir.endsWith("/")) rootDir else rootDir.substr(0,-1);
_getHandler = serve;
return this;
}
+ public function
+ credentials(key:String,cert:String,?ca:Array<String>):HttpServer {
+ var k = Node.fs.readFileSync(key);
+ var c = Node.fs.readFileSync(cert);
+ _creds = {key:k, cert:c,ca:ca};
+ return this;
+ }
+
function
defaultGetHandler(path:String,req:NodeHttpServerReq,resp:NodeHttpServerResp,statusCode:Int) {
do404(req,resp);
@@ -180,6 +193,8 @@ class HttpImpl implements HttpServer,implements Part<HostPort,String,HttpServer,
public function
serve(path:String,req:NodeHttpServerReq,resp:NodeHttpServerResp,statusCode=200) {
var fileToServe = if (_root != null ) _root+path else path;
+ trace("serving :"+path);
+
Node.fs.stat(fileToServe,function (e, stat:NodeStat) {
if (e != null) {
do404(req,resp);
View
@@ -0,0 +1,35 @@
+
+
+
+/*
+openssl genrsa -out privatekey.pem 1024
+openssl req -new -key privatekey.pem -out certrequest.csr
+openssl x509 -req -in certrequest.csr -signkey privatekey.pem -out certificate.pem
+*/
+
+
+import cloudshift.Core;
+import cloudshift.Http;
+using cloudshift.Mixin;
+
+class HttpTest {
+
+ public static function
+ main() {
+ Core.init();
+
+ /*
+ Http.get("http://bingocoin.net").deliver(function(html) {
+ trace(html);
+ });
+ */
+
+ Http.server()
+ .root("www")
+ .credentials("privatekey.pem","certificate.pem")
+ .start({host:"localhost",port:8000}).outcome(function(http) {
+
+ });
+ }
+
+}
View
@@ -0,0 +1,3 @@
+
+all:
+ haxe build.hxml
View
@@ -0,0 +1,8 @@
+--times
+-js test.js
+-cp .
+-cp ../../
+-cp ../../../hx-node
+-D nodejs
+--macro cloudshift.core.JSGenerator.use()
+-main HttpTest
View
@@ -0,0 +1,17 @@
+-----BEGIN CERTIFICATE-----
+MIICnTCCAgYCCQCcJmO+18nwdjANBgkqhkiG9w0BAQUFADCBkjELMAkGA1UEBhMC
+VUsxFjAUBgNVBAgMDU1ldHJvcG9saXRhbmExEDAOBgNVBAcMB0xhUmVpbmExEzAR
+BgNVBAoMCmNsb3Vkc2hpZnQxCzAJBgNVBAsMAml0MRAwDgYDVQQDDAdSaXRjaGll
+MSUwIwYJKoZIhvcNAQkBFhZibGFja2RvZ0BjbG91ZHNoaWZ0LmNsMB4XDTEyMDYw
+NDEzMTQyOFoXDTEyMDcwNDEzMTQyOFowgZIxCzAJBgNVBAYTAlVLMRYwFAYDVQQI
+DA1NZXRyb3BvbGl0YW5hMRAwDgYDVQQHDAdMYVJlaW5hMRMwEQYDVQQKDApjbG91
+ZHNoaWZ0MQswCQYDVQQLDAJpdDEQMA4GA1UEAwwHUml0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PXmKFrV
+O8/DXQQTInr69MMu6a/WN+xAJHUc4xtN2XHbh6WcDD/PK0pPlzRcbm27+tXMEaM4
+6WM/UGt7g89BIu81NQb4uRC42iDgybZvpUO6s+c6Q6IE1PsNHBcJ
+-----END CERTIFICATE REQUEST-----
View
@@ -0,0 +1,15 @@
+-----BEGIN RSA PRIVATE KEY-----
+MIICXAIBAAKBgQDHtDk2FeZuMRHMO2RUDSEPXjYXLkCNWkf2BlzmmBhJ6fMmWEDV
+9RtfPO4t70d0xUQ9whuQeyRFHjS0OBBUIikqmmUlv7Tc7ysAayIXaZtwbiagIpjp
+cHDy450Pa0PE8oZxNVMGV/YVSKfjzD5VQTm4E8VqN6fPiXhJPSpRD/SLYQIDAQAB
+AoGASiBFVCQQ4jnoh0xMUz6EVPX9UeFjKJSoPVPjS9yMt3LVRBhiCIjwXgt5xScZ
++Of4CoxwOtLahaVOns5okTIU4QmLalwlxxf/7w1Lp+0rWS5aSZ8cleCIdnfnXAuO
+G47qUG/VWPnLVib1iXNRjpF18cnDmWuuRdF9OL2SId7kCBkCQQDlJe/xatWWM+bh
+7UAvDtY40IE+UB4gGm3Zfh5KsMZdFhnT/dt6KAfDF5qCJMZRC4+fYft/VmwWca8k
+roVmbX33AkEA3xsCdGTkqecifT53tO06PDQWVO2V0gy85UTDwqiuwtbpjZkEEGDs
+IyuMhuqU56v3aswWxTPCSqVW3J2PwBzLZwJAcCOIsNRfggoUSywX5O+V1mfzV9NN
+atiEOZOGr3ajHJrcOOpapgVBBzBd5KKqiq7lslH5Ce/5JWKtG35suhuLMQJATh4j
+7uNA2Wivv9C2k2gY4umOBDCoK8MABPrd3HO5tUkQv96a+K0nIW1nJ/pNJw0kdYV+
+V5A28HISBu84XRj38wJBANJz98wgtEck6578+/9kIgZW+RDQdcVr8xUid1+1KnS+
+H0+wTs/n1IhmyrqivGum7POethAPuANNS94EqSUdTD0=
+-----END RSA PRIVATE KEY-----
Oops, something went wrong.

0 comments on commit 32c2675

Please sign in to comment.