-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(web-server): strip scheme, host and port
- Loading branch information
1 parent
abdcb45
commit 06a0da0
Showing
6 changed files
with
106 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
/** | ||
* Strip host middleware is responsible for stripping hostname from request path | ||
* This to handle requests that uses (normally over proxies) an absolutURI as request path | ||
*/ | ||
|
||
var createStripHostMiddleware = function() { | ||
return function(request, response, next) { | ||
function stripHostFromUrl(url) { | ||
return url.replace(/^http[s]?:\/\/([a-z\-\.\:\d]+)\//, '/'); | ||
} | ||
|
||
request.normalizedUrl = stripHostFromUrl(request.url) || request.url; | ||
next(); | ||
}; | ||
}; | ||
|
||
// PUBLIC API | ||
exports.create = createStripHostMiddleware; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
describe 'middleware.strip_host', -> | ||
q = require 'q' | ||
|
||
mocks = require 'mocks' | ||
HttpResponseMock = mocks.http.ServerResponse | ||
HttpRequestMock = mocks.http.ServerRequest | ||
|
||
File = require('../../../lib/file_list').File | ||
Url = require('../../../lib/file_list').Url | ||
|
||
fsMock = mocks.fs.create | ||
base: | ||
path: | ||
'a.js': mocks.fs.file(0, 'js-src-a') | ||
'index.html': mocks.fs.file(0, '<html>') | ||
src: | ||
'some.js': mocks.fs.file(0, 'js-source') | ||
'utf8ášč': | ||
'some.js': mocks.fs.file(0, 'utf8-file') | ||
|
||
|
||
serveFile = require('../../../lib/middleware/common').createServeFile fsMock, null | ||
createStripHostMiddleware = require('../../../lib/middleware/strip_host').create | ||
|
||
handler = filesDeferred = nextSpy = response = null | ||
|
||
beforeEach -> | ||
nextSpy = sinon.spy() | ||
request = null | ||
handler = createStripHostMiddleware null, null, '/base/path' | ||
|
||
it 'should strip request with IP number', (done) -> | ||
request = new HttpRequestMock('http://192.12.31.100/base/a.js?123345') | ||
handler request, null, nextSpy | ||
|
||
expect(request.normalizedUrl).to.equal '/base/a.js?123345' | ||
expect(nextSpy).to.have.been.called | ||
done() | ||
|
||
it 'should strip request with absoluteURI', (done) -> | ||
request = new HttpRequestMock('http://localhost/base/a.js?123345') | ||
handler request, null, nextSpy | ||
|
||
expect(request.normalizedUrl).to.equal '/base/a.js?123345' | ||
expect(nextSpy).to.have.been.called | ||
done() | ||
|
||
it 'should strip request with absoluteURI and port', (done) -> | ||
request = new HttpRequestMock('http://localhost:9876/base/a.js?123345') | ||
handler request, null, nextSpy | ||
|
||
expect(request.normalizedUrl).to.equal '/base/a.js?123345' | ||
expect(nextSpy).to.have.been.called | ||
done() | ||
|
||
it 'should strip request with absoluteURI over HTTPS', (done) -> | ||
request = new HttpRequestMock('https://karma-runner.github.io/base/a.js?123345') | ||
handler request, null, nextSpy | ||
|
||
expect(request.normalizedUrl).to.equal '/base/a.js?123345' | ||
expect(nextSpy).to.have.been.called | ||
done() | ||
|
||
it 'should return same url as passed one', (done) -> | ||
request = new HttpRequestMock('/base/b.js?123345') | ||
handler request, null, nextSpy | ||
|
||
expect(request.normalizedUrl).to.equal '/base/b.js?123345' | ||
expect(nextSpy).to.have.been.called | ||
done() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters