a7ad0869b481f6081fdbfd51ffb2abfb64363f41
[nodejsws.git] / lib / wsrequest.js
1 var url = require("url");
2 var path = require("path");
3 var fs = require("fs");
4 var myparse = require("./myparse.js");
5 var webmain = require("./webmain.js");
6
7 //var staticExtensions = ["html", "gif", "jpg", "css", "js", "ico"];
8
9 function wsRequest(request, response) {
10         var isStatic = 0;
11         var purl = url.parse(request.url);
12         
13         /*staticExtensions.forEach(function testExtn(setest) {
14                 console.log("testing url: ", request.url);
15                 console.log("against: ", purl.pathname);
16                 var chk = purl.pathname.split(".");
17                 console.log("chk: ", chk);
18                 var chkid = staticExtensions.indexOf(chk[chk.length-1].toLowerCase());
19                 console.log("chkid is ", chkid);
20                 if(chkid != -1) {
21                         isStatic = 1;
22                 }
23         });*/
24         
25         // if the end of the pathname is something.something, we assume static
26         var lpath = purl.pathname.split("/").pop();
27         var idx = lpath.indexOf(".");
28         console.log("testing url: ", request.url);
29         console.log("against: ", purl.pathname);
30         console.log("lpath is: '%s'", lpath);
31         console.log("type is: '%s'", typeof lpath);
32         console.log("idx is: ", idx);
33         
34         if(idx > 0) isStatic = 1;
35         
36         if(isStatic == 1) {
37                 // do the static
38                 console.log("Service as static");
39                 serveStatic(lpath, response);
40                 return;
41         }
42         
43         //console.log("request: ", request);
44         console.log("purl: ", purl);
45
46         // now we need to find the extension used
47         // to serve the request based no the first purl
48         if(purl.pathname == "/") {
49                 console.log("Serv main");
50                 webmain.serveMain(request, response, function (request, response) {
51                         response.end();
52                 });
53                 return;
54         }
55
56         var thispurl = purl.pathname.split("/")[1];
57         fs.stat("./purls/web_"+thispurl+".js", function (err, stats) {
58                 console.log("get purl is "+thispurl+" and err "+err+" and "+stats);
59                 if(err) {
60                         response.writeHead(404, {"Content-Type": "text/plain"});
61                         response.write("404 Not Found\n");
62                         response.end();
63                         return;
64                 }
65                 var thiserv = require("../purls/web_"+thispurl+".js");
66                 if(thiserv.requireBody()) {
67                         console.log("yubber is true");
68                         webmain.serveBody(request, response, thiserv.process);
69                 } else {
70                         console.log("yubber is false");
71                         thiserv.process(request, response, function(request, response) {
72                                 response.end();
73                         });
74                 }
75                 return;
76         });
77         
78         
79         return;
80 }
81
82 function serveStatic(staticname, response) {
83         var pathName = "./res/"+staticname;
84         console.log("Pathname for check is ", pathName);
85         fs.exists(pathName, function(exists) {
86                 if(!exists) {
87                         response.writeHead(404, {"Content-Type": "text/plain"});
88                         response.write("404 Not Found\n");
89                         response.end();
90                         return;
91                 }
92                 
93                 fs.readFile(pathName, "binary", function(err, file) {
94                         if(err) {        
95                                 response.writeHead(500, {"Content-Type": "text/plain"});
96                                 response.write(err + "\n");
97                                 response.end();
98                                 return;
99                         }
100
101                         response.writeHead(200);
102                         response.write(file, "binary");
103                         response.end();                 
104                 });
105         });
106 }
107
108 exports.wsRequest = wsRequest;