vwf.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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. persistence = require( './persistence' ),
  7. servehandler = require( './serve-handler' ),
  8. helpers = require( './helpers' ),
  9. application = require( './application' ),
  10. url = require( 'url' );
  11. // HandleParsableRequest takes the incoming request, and uses the helper library functions to parse the
  12. // URL into its 'public_path, application, instance and private_path' components, and then attempts to redirect
  13. // or serve that request as appropriate. If it succesfully completes the request, it returns true, otherwise it
  14. // returns false.
  15. function HandleParsableRequest( request, response ) {
  16. var parsedRequest = parseurl.Process( url.parse(request.url).pathname );
  17. // if (parsedRequest.application == undefined) {
  18. // return false
  19. // }
  20. // Used to check if the URL referer was an application instance. Components added by the "includes" keyword
  21. // in yaml are loaded using jQuery which appends a query parameter to handle the callback. Checking the referer
  22. // allows those URLs to be handled correctly, instead of treating them as a new application that needs an instance ID.
  23. var parsedReferer = request.headers.referer ? parseurl.Process( url.parse(request.headers.referer).pathname ) : undefined;
  24. if ( ( request.url[ request.url.length - 1 ] != "/" ) && ( parsedRequest[ 'private_path' ] == undefined ) && ( url.parse( request.url ).search == undefined ||
  25. ( parsedReferer && parsedReferer[ 'instance' ] != undefined ) ) ) { // If the referer was an application, allow it even if it has query parameters
  26. var browserIsIE8 = ( request.headers['user-agent'] ) && ( request.headers['user-agent'].indexOf("MSIE 8.0" ) > -1 );
  27. var urlIsUnsupportedPage = ( request.url.indexOf("/web/unsupported.html") !== -1 );
  28. var refererIsUnsupportedPage = ( request.headers.referer && ( request.headers.referer.indexOf( "/web/unsupported.html" ) !== -1 ) );
  29. if ( browserIsIE8 && !( urlIsUnsupportedPage || refererIsUnsupportedPage ) ) {
  30. serve.Redirect( "/web/unsupported.html", response ); // Redirect unsupported browsers to web/docs/unsupported.html
  31. return true;
  32. }
  33. else if ( ( parsedRequest[ 'instance' ] == undefined ) && ( request.headers['accept'].indexOf( "text/html" ) == -1 ) ) {
  34. return servehandler.Component( request, response, helpers.JoinPath( global.applicationRoot, parsedRequest[ 'public_path' ], parsedRequest[ 'application' ] ) );
  35. }
  36. else if ( parsedRequest[ 'instance' ] == undefined && request.headers['accept'].indexOf( "text/html" ) != -1 && helpers.IsFile( helpers.JoinPath( global.applicationRoot, request.url ) ) ) {
  37. return servehandler.File( request, response, helpers.JoinPath( global.applicationRoot, request.url ) );
  38. }
  39. else {
  40. serve.Redirect( request.url + "/", response );
  41. return true;
  42. }
  43. }
  44. else if ( ( parsedRequest[ 'instance' ] == undefined ) && ( parsedRequest[ 'private_path' ] == undefined ) ) {
  45. if ( request.url != "/" ) {
  46. // Redirect if the requested url is either a specified directory or application
  47. if ( helpers.IsDirectory( helpers.JoinPath( global.applicationRoot + request.url )) || parsedRequest['application'] != undefined ) {
  48. // Get the driver specific url parameters if applicable
  49. var queryString = url.parse( request.url ).search;
  50. if ( queryString == undefined ) {
  51. serve.Redirect( request.url + helpers.GenerateInstanceID( ), response );
  52. return true;
  53. }
  54. else {
  55. // Tack on the driver specific configuration parameters
  56. serve.Redirect( helpers.JoinPath( url.parse( request.url ).pathname, helpers.GenerateInstanceID( ), queryString.replace( /\/$/, '' ) ), response );
  57. return true;
  58. }
  59. }
  60. }
  61. else if ( isDefaultApp( request.url ) ) {
  62. // Redirect if the url request does not include an application/file && a default 'index.vwf.yaml' exists
  63. serve.Redirect( request.url + helpers.GenerateInstanceID( ), response );
  64. return true;
  65. } else {
  66. return false;
  67. }
  68. }
  69. else {
  70. return application.Serve( request, response, parsedRequest );
  71. }
  72. }
  73. // Assuming no application or file was specified in the url request, check for the existance of
  74. // the default 'index.vwf.yaml' in either applicationRoot or cwd.
  75. function isDefaultApp ( requestURL ) {
  76. if ( helpers.IsFile( helpers.JoinPath( global.applicationRoot, "/index.vwf.yaml" ) )
  77. || helpers.IsFile( helpers.JoinPath( process.cwd(), "/index.vwf.yaml" ) ) ) {
  78. return true;
  79. }
  80. return false;
  81. }
  82. // HandleProxyRequest attempts to identify any of the 'proxy' based URL paths and serves then attempts to
  83. // serve them out of the the support/proxy subdirectories.
  84. // If the request is identified as being a proxy request and succesfully served, this returns true,
  85. // if it is not a proxy request (or it is a proxy request, but fails due to the file not being present),
  86. // then this will return false.
  87. function HandleProxyRequest( request, response ) {
  88. var updatedURL = url.parse( request.url ).pathname;
  89. var segments = helpers.GenerateSegments( updatedURL );
  90. if ( ( segments.length > 0 ) && ( segments[ 0 ] == "proxy" ) ) {
  91. if ( servehandler.File( request, response, helpers.JoinPath( global.vwfRoot + "/support/", updatedURL ) ) ) {
  92. return true;
  93. }
  94. if ( servehandler.Component( request, response, helpers.JoinPath( global.vwfRoot + "/support/", updatedURL ) ) ) {
  95. return true;
  96. }
  97. }
  98. return false;
  99. }
  100. // HandleFileRequest simply attempts to handle the incoming URL as if it is a direct request for a file within the public directory
  101. // structure.
  102. // The function returns true if a file is succesfully served, false if it is not.
  103. function HandleFileRequest( request, response ) {
  104. var updatedURL = url.parse( request.url ).pathname;
  105. var segments = helpers.GenerateSegments( updatedURL );
  106. if ( segments.length == 0 ) {
  107. updatedURL = "/index.html";
  108. }
  109. return servehandler.File( request, response, helpers.JoinPath( global.applicationRoot, updatedURL ) );
  110. }
  111. function HandleWebAppRequest(request, response) {
  112. var updatedURL = url.parse(request.url).pathname;
  113. var address = request.headers.host;
  114. var res = false;
  115. switch (updatedURL) {
  116. case '/app':
  117. response.writeHead(200, { 'content-type': 'text/html' });
  118. console.log(global.instances);
  119. for (var prop in global.instances) {
  120. response.write("<a href=http://" + address + prop + ">" + prop + "</a>" + "<br>");
  121. }
  122. response.end();
  123. res = true;
  124. break;
  125. case '/allinstances.json':
  126. response.writeHead(200, {"Content-Type": "application/json"});
  127. var obj = {};
  128. console.log(global.instances);
  129. for (var prop in global.instances) {
  130. //var name = prop.split('/');
  131. obj[prop] = {
  132. "instance":address + prop,
  133. "clients": Object.keys(global.instances[prop].clients).length
  134. };
  135. //response.write("<a href=http://" + address + prop + ">" + prop + "</a>" + "<br>");
  136. }
  137. var json = JSON.stringify(obj);
  138. response.end(json);
  139. res = true;
  140. break;
  141. default:
  142. break;
  143. }
  144. return res;
  145. }
  146. function parseUrlForReflector( pathName ) {
  147. try
  148. {
  149. var namespace = pathName;
  150. if(!namespace) return null;
  151. if (namespace[namespace.length - 1] != "/")
  152. namespace += "/";
  153. return parseurl.Process( namespace );
  154. }
  155. catch (e)
  156. {
  157. return null;
  158. }
  159. }
  160. function GetLoadForSocket( processedURL ) {
  161. if ( processedURL[ 'private_path' ] ) {
  162. return persistence.GetLoadInformation( processedURL );
  163. }
  164. return { 'save_name': undefined, 'save_revision': undefined, 'explicit_revision': undefined, 'application_path': undefined };
  165. }
  166. function HandleReflectorRequest(request, response) {
  167. //var updatedURL = url.parse(request.url).pathname;
  168. // var address = request.headers.host;
  169. var res = false;
  170. if (request.method === 'POST' && request.url === '/parseurl') {
  171. let body = [];
  172. request.on('data', (chunk) => {
  173. body.push(chunk);
  174. }).on('end', () => {
  175. body = Buffer.concat(body).toString();
  176. let pathUrl = JSON.parse(body);
  177. let refPath = parseUrlForReflector( pathUrl.url );
  178. //prepare for persistence request in case that's what this is
  179. let loadInfo = GetLoadForSocket( refPath );
  180. let saveObject = persistence.LoadSaveObject( loadInfo );
  181. let resObj = {
  182. path: refPath,
  183. loadInfo: loadInfo,
  184. saveObject: saveObject
  185. }
  186. //console.log(resObj);
  187. let json = JSON.stringify(resObj);
  188. response.writeHead(200, {"Content-Type": "application/json"});
  189. response.end(json);
  190. });
  191. } else {
  192. //response.statusCode = 404;
  193. //response.end();
  194. }
  195. return res;
  196. }
  197. // Serve is the top level function for serving requests. It first attempts to
  198. // serve the request based on parsing the incoming URL.
  199. // If that fails, it continues to attempt to serve the request as a 'proxy' request,
  200. // if that also does not serve anything to the request, then an attempt is made
  201. // to handle the request as a simple direct request for a file within the public
  202. // directory structure.
  203. // If all that fails, serve up a 404 response since the request was not handled.
  204. function Serve( request, response ) {
  205. var handledRequest = HandleReflectorRequest( request, response );
  206. if ( ! ( handledRequest ) ) {
  207. handledRequest = HandleParsableRequest( request, response ); }
  208. if ( ! ( handledRequest ) ) {
  209. handledRequest = HandleProxyRequest( request, response );
  210. }
  211. if ( ! ( handledRequest ) ) {
  212. handledRequest = HandleFileRequest( request, response );
  213. }
  214. if ( ! ( handledRequest ) ) {
  215. handledRequest = HandleWebAppRequest( request, response );
  216. }
  217. if ( ! ( handledRequest ) ) {
  218. global.log("404 : " + url.parse( request.url ).pathname )
  219. serve._404( response, "404.html" );
  220. }
  221. }
  222. exports.Serve = Serve;