Skip to content

Commit c5ad22e

Browse files
committed
breaking: req.origin should display the origin header if it exists, not the current hostname closes #1008
1 parent 40d5d33 commit c5ad22e

File tree

2 files changed

+9
-7
lines changed

2 files changed

+9
-7
lines changed

__tests__/request/origin.test.js

+5-3
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,17 @@ describe('ctx.origin', () => {
1111
const req = {
1212
url: '/users/1?next=/dashboard',
1313
headers: {
14-
host: 'localhost'
14+
host: 'localhost',
15+
origin: 'http://example.com'
1516
},
1617
socket,
1718
__proto__: Stream.Readable.prototype
1819
}
1920
const ctx = context(req)
20-
assert.strictEqual(ctx.origin, 'http://localhost')
21+
assert.strictEqual(ctx.origin, 'http://example.com')
22+
2123
// change it also work
2224
ctx.url = '/foo/users/1?next=/dashboard'
23-
assert.strictEqual(ctx.origin, 'http://localhost')
25+
assert.strictEqual(ctx.origin, 'http://example.com')
2426
})
2527
})

lib/request.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -89,14 +89,14 @@ module.exports = {
8989
},
9090

9191
/**
92-
* Get origin of URL.
92+
* Get the origin header.
9393
*
9494
* @return {String}
9595
* @api public
9696
*/
9797

9898
get origin () {
99-
return `${this.protocol}://${this.host}`
99+
return this.req.headers.origin || null
100100
},
101101

102102
/**
@@ -109,7 +109,7 @@ module.exports = {
109109
get href () {
110110
// support: `GET http://example.com/foo`
111111
if (/^https?:\/\//i.test(this.originalUrl)) return this.originalUrl
112-
return this.origin + this.originalUrl
112+
return this.protocol + '://' + this.host + this.originalUrl
113113
},
114114

115115
/**
@@ -288,7 +288,7 @@ module.exports = {
288288
if (!this.memoizedURL) {
289289
const originalUrl = this.originalUrl || '' // avoid undefined in template string
290290
try {
291-
this.memoizedURL = new URL(`${this.origin}${originalUrl}`)
291+
this.memoizedURL = new URL(`${this.protocol}://${this.host}${originalUrl}`)
292292
} catch (err) {
293293
this.memoizedURL = Object.create(null)
294294
}

0 commit comments

Comments
 (0)