vwf.js 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. // vwf.js
  2. // This file contains the functions that handle the top level parsing and responses to
  3. // requests for the VWF nodeJS server.
  4. var parseurl = require( './parse-url' ),
  5. serve = require( './serve' ),
  6. servehandler = require( './serve-handler' ),
  7. helpers = require( './helpers' ),
  8. application = require( './application' ),
  9. url = require( 'url' );
  10. // HandleParsableRequest takes the incoming request, and uses the helper library functions to parse the
  11. // URL into its 'public_path, application, instance and private_path' components, and then attempts to redirect
  12. // or serve that request as appropriate. If it succesfully completes the request, it returns true, otherwise it
  13. // returns false.
  14. function HandleParsableRequest( request, response ) {
  15. var parsedRequest = parseurl.Process( url.parse(request.url).pathname );
  16. // if (parsedRequest.application == undefined) {
  17. // return false
  18. // }
  19. // Used to check if the URL referer was an application instance. Components added by the "includes" keyword
  20. // in yaml are loaded using jQuery which appends a query parameter to handle the callback. Checking the referer
  21. // allows those URLs to be handled correctly, instead of treating them as a new application that needs an instance ID.
  22. var parsedReferer = request.headers.referer ? parseurl.Process( url.parse(request.headers.referer).pathname ) : undefined;
  23. if ( ( request.url[ request.url.length - 1 ] != "/" ) && ( parsedRequest[ 'private_path' ] == undefined ) && ( url.parse( request.url ).search == undefined ||
  24. ( parsedReferer && parsedReferer[ 'instance' ] != undefined ) ) ) { // If the referer was an application, allow it even if it has query parameters
  25. var browserIsIE8 = ( request.headers['user-agent'] ) && ( request.headers['user-agent'].indexOf("MSIE 8.0" ) > -1 );
  26. var urlIsUnsupportedPage = ( request.url.indexOf("/web/unsupported.html") !== -1 );
  27. var refererIsUnsupportedPage = ( request.headers.referer && ( request.headers.referer.indexOf( "/web/unsupported.html" ) !== -1 ) );
  28. if ( browserIsIE8 && !( urlIsUnsupportedPage || refererIsUnsupportedPage ) ) {
  29. serve.Redirect( "/web/unsupported.html", response ); // Redirect unsupported browsers to web/docs/unsupported.html
  30. return true;
  31. }
  32. else if ( ( parsedRequest[ 'instance' ] == undefined ) && ( request.headers['accept'].indexOf( "text/html" ) == -1 ) ) {
  33. return servehandler.Component( request, response, helpers.JoinPath( global.applicationRoot, parsedRequest[ 'public_path' ], parsedRequest[ 'application' ] ) );
  34. }
  35. else if ( parsedRequest[ 'instance' ] == undefined && request.headers['accept'].indexOf( "text/html" ) != -1 && helpers.IsFile( helpers.JoinPath( global.applicationRoot, request.url ) ) ) {
  36. return servehandler.File( request, response, helpers.JoinPath( global.applicationRoot, request.url ) );
  37. }
  38. else {
  39. serve.Redirect( request.url + "/", response );
  40. return true;
  41. }
  42. }
  43. else if ( ( parsedRequest[ 'instance' ] == undefined ) && ( parsedRequest[ 'private_path' ] == undefined ) ) {
  44. if ( request.url != "/" ) {
  45. // Redirect if the requested url is either a specified directory or application
  46. if ( helpers.IsDirectory( helpers.JoinPath( global.applicationRoot + request.url )) || parsedRequest['application'] != undefined ) {
  47. // Get the driver specific url parameters if applicable
  48. var queryString = url.parse( request.url ).search;
  49. if ( queryString == undefined ) {
  50. serve.Redirect( request.url + helpers.GenerateInstanceID( ), response );
  51. return true;
  52. }
  53. else {
  54. // Tack on the driver specific configuration parameters
  55. serve.Redirect( helpers.JoinPath( url.parse( request.url ).pathname, helpers.GenerateInstanceID( ), queryString.replace( /\/$/, '' ) ), response );
  56. return true;
  57. }
  58. }
  59. }
  60. else if ( isDefaultApp( request.url ) ) {
  61. // Redirect if the url request does not include an application/file && a default 'index.vwf.yaml' exists
  62. serve.Redirect( request.url + helpers.GenerateInstanceID( ), response );
  63. return true;
  64. } else {
  65. return false;
  66. }
  67. }
  68. else {
  69. return application.Serve( request, response, parsedRequest );
  70. }
  71. }
  72. // Assuming no application or file was specified in the url request, check for the existance of
  73. // the default 'index.vwf.yaml' in either applicationRoot or cwd.
  74. function isDefaultApp ( requestURL ) {
  75. if ( helpers.IsFile( helpers.JoinPath( global.applicationRoot, "/index.vwf.yaml" ) )
  76. || helpers.IsFile( helpers.JoinPath( process.cwd(), "/index.vwf.yaml" ) ) ) {
  77. return true;
  78. }
  79. return false;
  80. }
  81. // HandleProxyRequest attempts to identify any of the 'proxy' based URL paths and serves then attempts to
  82. // serve them out of the the support/proxy subdirectories.
  83. // If the request is identified as being a proxy request and succesfully served, this returns true,
  84. // if it is not a proxy request (or it is a proxy request, but fails due to the file not being present),
  85. // then this will return false.
  86. function HandleProxyRequest( request, response ) {
  87. var updatedURL = url.parse( request.url ).pathname;
  88. var segments = helpers.GenerateSegments( updatedURL );
  89. if ( ( segments.length > 0 ) && ( segments[ 0 ] == "proxy" ) ) {
  90. if ( servehandler.File( request, response, helpers.JoinPath( global.vwfRoot + "/support/", updatedURL ) ) ) {
  91. return true;
  92. }
  93. if ( servehandler.Component( request, response, helpers.JoinPath( global.vwfRoot + "/support/", updatedURL ) ) ) {
  94. return true;
  95. }
  96. }
  97. return false;
  98. }
  99. // HandleFileRequest simply attempts to handle the incoming URL as if it is a direct request for a file within the public directory
  100. // structure.
  101. // The function returns true if a file is succesfully served, false if it is not.
  102. function HandleFileRequest( request, response ) {
  103. var updatedURL = url.parse( request.url ).pathname;
  104. var segments = helpers.GenerateSegments( updatedURL );
  105. if ( segments.length == 0 ) {
  106. updatedURL = "/index.html";
  107. }
  108. return servehandler.File( request, response, helpers.JoinPath( global.applicationRoot, updatedURL ) );
  109. }
  110. function HandleWebAppRequest(request, response) {
  111. var updatedURL = url.parse(request.url).pathname;
  112. var address = request.headers.host;
  113. var res = false;
  114. switch (updatedURL) {
  115. case '/app':
  116. response.writeHead(200, { 'content-type': 'text/html' });
  117. console.log(global.instances);
  118. for (var prop in global.instances) {
  119. response.write("<a href=http://" + address + prop + ">" + prop + "</a>" + "<br>");
  120. }
  121. response.end();
  122. res = true;
  123. break;
  124. case '/allinstances.json':
  125. response.writeHead(200, {"Content-Type": "application/json"});
  126. var obj = {};
  127. console.log(global.instances);
  128. for (var prop in global.instances) {
  129. //var name = prop.split('/');
  130. obj[prop] = {
  131. "instance":address + prop,
  132. "clients": Object.keys(global.instances[prop].clients).length
  133. };
  134. //response.write("<a href=http://" + address + prop + ">" + prop + "</a>" + "<br>");
  135. }
  136. var json = JSON.stringify(obj);
  137. response.end(json);
  138. res = true;
  139. break;
  140. default:
  141. break;
  142. }
  143. return res;
  144. }
  145. // Serve is the top level function for serving requests. It first attempts to
  146. // serve the request based on parsing the incoming URL.
  147. // If that fails, it continues to attempt to serve the request as a 'proxy' request,
  148. // if that also does not serve anything to the request, then an attempt is made
  149. // to handle the request as a simple direct request for a file within the public
  150. // directory structure.
  151. // If all that fails, serve up a 404 response since the request was not handled.
  152. function Serve( request, response ) {
  153. var handledRequest = HandleParsableRequest( request, response );
  154. if ( ! ( handledRequest ) ) {
  155. handledRequest = HandleProxyRequest( request, response );
  156. }
  157. if ( ! ( handledRequest ) ) {
  158. handledRequest = HandleFileRequest( request, response );
  159. }
  160. if ( ! ( handledRequest ) ) {
  161. handledRequest = HandleWebAppRequest( request, response );
  162. }
  163. if ( ! ( handledRequest ) ) {
  164. global.log("404 : " + url.parse( request.url ).pathname )
  165. serve._404( response, "404.html" );
  166. }
  167. }
  168. exports.Serve = Serve;