Skip to content
This repository has been archived by the owner on Sep 6, 2021. It is now read-only.

Commit

Permalink
keep url filtering and mapping separate
Browse files Browse the repository at this point in the history
  • Loading branch information
redmunds committed Feb 26, 2013
1 parent bddb4a6 commit 7e37d28
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 84 deletions.
79 changes: 8 additions & 71 deletions src/LiveDevelopment/HttpServerManager.js
Expand Up @@ -29,25 +29,15 @@
* order of descending priority by way their canServe methods.
*
*
* # HttpServerManager.canServe(url)
* # HttpServerManager.getProvider(url)
*
* Determines whether we can serve url.
* Returns highest priority provider that can serve the url.
*
* @param {String} url
* A url to file being served.
*
* @return {Boolean}
* true for yes, otherwise false.
*
* # HttpServerManager.getBaseUrl(url)
*
* Determines base url.
*
* @param {String} url
* A url to file being served.
*
* @return {String}
* Base url for current project.
* @return {HttpServerProvider}
* Provider or null.
*
*
* HttpServerProvider Overview:
Expand All @@ -61,9 +51,8 @@
*
* # HttpServerProvider.canServe(url)
*
* The method by which a provider indicates intent to serbver a url.
* The manager calls this method when...
*
* The method by which a provider indicates intent to serve a url.
* The manager calls this method when querying providers
*
* param {String} url
* A url for the page to be served.
Expand Down Expand Up @@ -109,7 +98,7 @@ define(function (require, exports, module) {
* @return {HttpServerProvider}
* true for yes, otherwise false.
*/
function _providerCanServe(url) {
function getProvider(url) {

var provider, i;

Expand All @@ -123,57 +112,6 @@ define(function (require, exports, module) {
return null;
}


/**
* Determines whether we can serve url.
*
* @param {String} url
* A url to file being served.
*
* @return {Boolean}
* true for yes, otherwise false.
*/
function canServe(url) {

if (ProjectManager.getBaseUrl() && FileUtils.isServerHtmlFileExt(url)) {
return true;
}

if (_providerCanServe(url)) {
return true;
}

return false;
}

/**
* Determines base url.
*
* @param {String} url
* A url to file being served.
*
* @return {String}
* Base url for current project.
*/
function getBaseUrl(url) {

// First, use ProjectManager base url specified by user
var userBaseUrl = ProjectManager.getBaseUrl();
if (userBaseUrl) {
return userBaseUrl;
}

// Next, query all registered providers and getting base url
// from highest priority provider that can serve file type.
var provider = _providerCanServe(url);
if (provider) {
return provider.getBaseUrl();
}

// Otherwise, return empty string
return "";
}

/**
* The method by which a HttpServerProvider registers itself.
*
Expand All @@ -195,7 +133,6 @@ define(function (require, exports, module) {
}

// Define public API
exports.canServe = canServe;
exports.getBaseUrl = getBaseUrl;
exports.getProvider = getProvider;
exports.registerProvider = registerProvider;
});
70 changes: 61 additions & 9 deletions src/LiveDevelopment/LiveDevelopment.js
Expand Up @@ -115,8 +115,9 @@ define(function LiveDevelopment(require, exports, module) {
// store the names (matching property names in the 'agent' object) of agents that we've loaded
var _loadedAgentNames = [];

var _liveDocument; // the document open for live editing.
var _relatedDocuments; // CSS and JS documents that are used by the live HTML document
var _liveDocument; // the document open for live editing.
var _relatedDocuments; // CSS and JS documents that are used by the live HTML document
var _httpServerProvider; // http server provider

function _isHtmlFileExt(ext) {
return (FileUtils.isStaticHtmlFileExt(ext) ||
Expand All @@ -126,7 +127,11 @@ define(function LiveDevelopment(require, exports, module) {
/** Convert a URL to a local full file path */
function _urlToPath(url) {
var path,
baseUrl = HttpServerManager.getBaseUrl(url);
baseUrl = "";

if (_httpServerProvider) {
baseUrl = _httpServerProvider.getBaseUrl(url);
}

if (baseUrl !== "" && url.indexOf(baseUrl) === 0) {
// Use base url to translate to local file path.
Expand All @@ -146,7 +151,11 @@ define(function LiveDevelopment(require, exports, module) {
/** Convert a local full file path to a URL */
function _pathToUrl(path) {
var url,
baseUrl = HttpServerManager.getBaseUrl(path);
baseUrl = "";

if (_httpServerProvider) {
baseUrl = _httpServerProvider.getBaseUrl(url);
}

// See if base url has been specified and path is within project
if (baseUrl !== "" && ProjectManager.isWithinProject(path)) {
Expand Down Expand Up @@ -452,6 +461,7 @@ define(function LiveDevelopment(require, exports, module) {
unloadAgents();
_closeDocument();
_setStatus(STATUS_INACTIVE);
_httpServerProvider = null;
}

function _onReconnect() {
Expand Down Expand Up @@ -498,12 +508,11 @@ define(function LiveDevelopment(require, exports, module) {
showWrongDocError();

} else {
if (!exports.config.experimental) {
_httpServerProvider = HttpServerManager.getProvider(doc.file.fullPath);
if (!exports.config.experimental && !_httpServerProvider) {
if (FileUtils.isServerHtmlFileExt(doc.extension)) {
if (!HttpServerManager.getBaseUrl(doc.url)) {
showNeedBaseUrlError();
return promise;
}
showNeedBaseUrlError();
return promise;
} else if (!FileUtils.isStaticHtmlFileExt(doc.extension)) {
showWrongDocError();
return promise;
Expand Down Expand Up @@ -608,6 +617,7 @@ define(function LiveDevelopment(require, exports, module) {
}
Inspector.disconnect();
_setStatus(STATUS_INACTIVE);
_httpServerProvider = null;
}

/** Enable highlighting */
Expand Down Expand Up @@ -682,6 +692,44 @@ define(function LiveDevelopment(require, exports, module) {
}
}

/**
* @constructor
*/
function UserServerProvider() {}

/**
* Determines whether we can serve file type.
*
* @param {String} url
* A url to file being served.
*
* @return {Boolean}
* true for yes, otherwise false.
*/
UserServerProvider.prototype.canServe = function (url) {

var baseUrl = ProjectManager.getBaseUrl();
if (!baseUrl) {
return false;
}

if (!ProjectManager.isWithinProject(url)) {
return false;
}

return true;
};

/**
* Returns a base url for current project.
*
* @return {String}
* Base url for current project.
*/
UserServerProvider.prototype.getBaseUrl = function () {
return ProjectManager.getBaseUrl();
};

/** Initialize the LiveDevelopment Session */
function init(theConfig) {
exports.config = theConfig;
Expand All @@ -691,6 +739,10 @@ define(function LiveDevelopment(require, exports, module) {
$(DocumentManager).on("currentDocumentChange", _onDocumentChange)
.on("documentSaved", _onDocumentSaved)
.on("dirtyFlagChange", _onDirtyFlagChange);

// Register user defined server provider
var userServerProvider = new UserServerProvider();
HttpServerManager.registerProvider(userServerProvider, 99);
}

// For unit testing
Expand Down
13 changes: 9 additions & 4 deletions src/extensions/default/StaticServer/main.js
Expand Up @@ -61,12 +61,12 @@ define(function (require, exports, module) {
}
}


/**
* @constructor
*/
function StaticHttpServerProvider() {}

/**
* Determines whether we can serve file type.
*
Expand All @@ -77,16 +77,21 @@ define(function (require, exports, module) {
* true for yes, otherwise false.
*/
StaticHttpServerProvider.prototype.canServe = function (url) {

if (!ProjectManager.isWithinProject(url)) {
return false;
}

// url ending in / implies default file, which is usually
// index.html, so HttpServerManager we can server it
if (url.match(/\/$/)) {
return true;
}

// TODO: do a MIME Type lookup on file extension
return FileUtils.isStaticHtmlFileExt(url);
};

/**
* Returns a base url for current project.
*
Expand Down

0 comments on commit 7e37d28

Please sign in to comment.