Compare commits
42 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 8eab4b3e5f | |||
| 46923f37d9 | |||
| 2771d95bde | |||
| 7c2e61fac0 | |||
| 1dc9ea32f5 | |||
| e8ed108767 | |||
| 4cf6771177 | |||
| e54de15542 | |||
| 943f5bc89d | |||
| 1c25d6f3a3 | |||
| b63537f11d | |||
| 11d14fc642 | |||
| 8e1f534a8a | |||
| c1417c5dfa | |||
| 8df10e0d65 | |||
| 5ee30ad5cc | |||
| 9321e56abd | |||
| 881e7c8f3d | |||
| 43761f2799 | |||
| 70f8c7d0b7 | |||
| f304ef13a2 | |||
| e8ef98b286 | |||
| 3af33f862f | |||
| 138d6c1d29 | |||
| f97dd885ea | |||
| f9fb1c724c | |||
| 1b077bf249 | |||
| e342cdbb10 | |||
| 0a04a09f68 | |||
| 8c07b21b82 | |||
| 662b9626be | |||
| abdc5573da | |||
| 525957277a | |||
| 4e7103a5a9 | |||
| 34aa684789 | |||
| 11ac890744 | |||
| 33587da68d | |||
| 5cf0329683 | |||
| 0cc4549d4e | |||
| 64e2d89062 | |||
| 57d0ab245f | |||
| 4b9bbe04b2 |
@@ -15,7 +15,7 @@ browsers:
|
||||
- name: iphone
|
||||
version: '8.1..latest'
|
||||
- name: android
|
||||
version: '4.0..latest'
|
||||
version: '4.4..latest'
|
||||
server: ./test/server/index.js
|
||||
scripts:
|
||||
- "/ie8-polyfill.js"
|
||||
|
||||
@@ -2,11 +2,11 @@
|
||||
|
||||
[](https://saucelabs.com/u/stream-http)
|
||||
|
||||
This module is an implementation of node's native `http` module for the browser.
|
||||
It tries to match node's api and behavior as closely as possible, but some features
|
||||
This module is an implementation of Node's native `http` module for the browser.
|
||||
It tries to match Node's API and behavior as closely as possible, but some features
|
||||
aren't available, since browsers don't give nearly as much control over requests.
|
||||
|
||||
This is heavily inspired by, and intended to replace, [http-browserify](https://github.com/substack/http-browserify)
|
||||
This is heavily inspired by, and intended to replace, [http-browserify](https://github.com/substack/http-browserify).
|
||||
|
||||
## What does it do?
|
||||
|
||||
@@ -15,7 +15,7 @@ the request has completed whenever possible.
|
||||
|
||||
The following browsers support true streaming, where only a small amount of the request
|
||||
has to be held in memory at once:
|
||||
* Chrome >= 43 (using the `fetch` api)
|
||||
* Chrome >= 43 (using the `fetch` API)
|
||||
* Firefox >= 9 (using `moz-chunked-arraybuffer` responseType with xhr)
|
||||
|
||||
The following browsers support pseudo-streaming, where the data is available before the
|
||||
@@ -38,18 +38,18 @@ All browsers with full ES5 support shouldn't require any polyfills.
|
||||
|
||||
## How do you use it?
|
||||
|
||||
The intent is to have the same api as the client part of the
|
||||
[node HTTP module](https://nodejs.org/api/http.html). The interfaces are the same wherever
|
||||
practical, although limitations in browsers make an exact clone of the node api impossible.
|
||||
The intent is to have the same API as the client part of the
|
||||
[Node HTTP module](https://nodejs.org/api/http.html). The interfaces are the same wherever
|
||||
practical, although limitations in browsers make an exact clone of the Node API impossible.
|
||||
|
||||
This module implements `http.request`, `http.get`, and most of `http.ClientRequest`
|
||||
and `http.IncomingMessage` in addition to `http.METHODS` and `http.STATUS_CODES`. See the
|
||||
node docs for how these work.
|
||||
Node docs for how these work.
|
||||
|
||||
### Extra features compared to node
|
||||
### Extra features compared to Node
|
||||
|
||||
* The `message.url` property provides access to the final url after all redirects. This
|
||||
is useful since the browser follows all redirects silently, unlike node. It is available
|
||||
* The `message.url` property provides access to the final URL after all redirects. This
|
||||
is useful since the browser follows all redirects silently, unlike Node. It is available
|
||||
in Chrome 37 and newer, Firefox 32 and newer, and Safari 9 and newer.
|
||||
|
||||
* The `options.withCredentials` boolean flag, used to indicate if the browser should send
|
||||
@@ -57,22 +57,22 @@ cookies or authentication information with a CORS request. Default false.
|
||||
|
||||
This module has to make some tradeoffs to support binary data and/or streaming. Generally,
|
||||
the module can make a fairly good decision about which underlying browser features to use,
|
||||
but sometimes it helps to get a little input from the user.
|
||||
but sometimes it helps to get a little input from the developer.
|
||||
|
||||
* The `options.mode` field passed into `http.request` or `http.get` can take on one of the
|
||||
following values:
|
||||
* 'default' (or any falsy value, including undefined): Try to provide partial data before
|
||||
* 'default' (or any falsy value, including `undefined`): Try to provide partial data before
|
||||
the request completes, but not at the cost of correctness for binary data or correctness of
|
||||
the 'content-type' response header. This mode will also avoid slower code paths whenever
|
||||
possible, which is particularly useful when making large requests in a browser like Safari
|
||||
that has a weaker javascript engine.
|
||||
that has a weaker JavaScript engine.
|
||||
* 'allow-wrong-content-type': Provides partial data in more cases than 'default', but
|
||||
at the expense of causing the 'content-type' response header to be incorrectly reported
|
||||
(as 'text/plain; charset=x-user-defined') in some browsers, notably Safari and Chrome 42
|
||||
and older. Preserves binary data whenever possible. In some cases the implementation may
|
||||
also be a bit slow. This was the default in versions of this module before 1.5.
|
||||
* 'prefer-stream': Provide data before the request completes even if binary data (anything
|
||||
that isn't a single-byte ASCII or utf8 character) will be corrupted. Of course, this option
|
||||
that isn't a single-byte ASCII or UTF8 character) will be corrupted. Of course, this option
|
||||
is only safe for text data. May also cause the 'content-type' response header to be
|
||||
incorrectly reported (as 'text/plain; charset=x-user-defined').
|
||||
* 'disable-fetch': Force the use of plain XHR regardless of the browser declaring a fetch
|
||||
@@ -80,7 +80,7 @@ capability. Preserves the correctness of binary data and the 'content-type' resp
|
||||
* 'prefer-fast': Deprecated; now a synonym for 'default', which has the same performance
|
||||
characteristics as this mode did in versions before 1.5.
|
||||
|
||||
### Features missing compared to node
|
||||
### Features missing compared to Node
|
||||
|
||||
* `http.Agent` is only a stub
|
||||
* The 'socket', 'connect', 'upgrade', and 'continue' events on `http.ClientRequest`.
|
||||
@@ -94,6 +94,8 @@ the server.
|
||||
* `message.trailers` and `message.rawTrailers` will remain empty.
|
||||
* Redirects are followed silently by the browser, so it isn't possible to access the 301/302
|
||||
redirect pages.
|
||||
* The `timeout` options in the `request` method is non-operational in Safari <= 5 and
|
||||
Opera <= 12.
|
||||
|
||||
## Example
|
||||
|
||||
@@ -101,11 +103,11 @@ redirect pages.
|
||||
http.get('/bundle.js', function (res) {
|
||||
var div = document.getElementById('result');
|
||||
div.innerHTML += 'GET /beep<br>';
|
||||
|
||||
|
||||
res.on('data', function (buf) {
|
||||
div.innerHTML += buf;
|
||||
});
|
||||
|
||||
|
||||
res.on('end', function () {
|
||||
div.innerHTML += '<br>__END__';
|
||||
});
|
||||
@@ -114,7 +116,7 @@ http.get('/bundle.js', function (res) {
|
||||
|
||||
## Running tests
|
||||
|
||||
There are two sets of tests: the tests that run in node (found in `test/node`) and the tests
|
||||
There are two sets of tests: the tests that run in Node (found in `test/node`) and the tests
|
||||
that run in the browser (found in `test/browser`). Normally the browser tests run on
|
||||
[Sauce Labs](http://saucelabs.com/).
|
||||
|
||||
@@ -122,7 +124,7 @@ Running `npm test` will run both sets of tests, but in order for the Sauce Labs
|
||||
you will need to sign up for an account (free for open source projects) and put the
|
||||
credentials in a [`.zuulrc` file](https://github.com/defunctzombie/zuul/wiki/zuulrc).
|
||||
|
||||
To run just the node tests, run `npm run test-node`.
|
||||
To run just the Node tests, run `npm run test-node`.
|
||||
|
||||
To run the browser tests locally, run `npm run test-browser-local` and point your browser to
|
||||
`http://localhost:8080/__zuul`
|
||||
|
||||
+36
-7
@@ -6,12 +6,34 @@ try {
|
||||
exports.blobConstructor = true
|
||||
} catch (e) {}
|
||||
|
||||
var xhr = new global.XMLHttpRequest()
|
||||
// If location.host is empty, e.g. if this page/worker was loaded
|
||||
// from a Blob, then use example.com to avoid an error
|
||||
xhr.open('GET', global.location.host ? '/' : 'https://example.com')
|
||||
// The xhr request to example.com may violate some restrictive CSP configurations,
|
||||
// so if we're running in a browser that supports `fetch`, avoid calling getXHR()
|
||||
// and assume support for certain features below.
|
||||
var xhr
|
||||
function getXHR () {
|
||||
// Cache the xhr value
|
||||
if (xhr !== undefined) return xhr
|
||||
|
||||
if (global.XMLHttpRequest) {
|
||||
xhr = new global.XMLHttpRequest()
|
||||
// If XDomainRequest is available (ie only, where xhr might not work
|
||||
// cross domain), use the page location. Otherwise use example.com
|
||||
// Note: this doesn't actually make an http request.
|
||||
try {
|
||||
xhr.open('GET', global.XDomainRequest ? '/' : 'https://example.com')
|
||||
} catch(e) {
|
||||
xhr = null
|
||||
}
|
||||
} else {
|
||||
// Service workers don't have XHR
|
||||
xhr = null
|
||||
}
|
||||
return xhr
|
||||
}
|
||||
|
||||
function checkTypeSupport (type) {
|
||||
var xhr = getXHR()
|
||||
if (!xhr) return false
|
||||
try {
|
||||
xhr.responseType = type
|
||||
return xhr.responseType === type
|
||||
@@ -24,17 +46,24 @@ function checkTypeSupport (type) {
|
||||
var haveArrayBuffer = typeof global.ArrayBuffer !== 'undefined'
|
||||
var haveSlice = haveArrayBuffer && isFunction(global.ArrayBuffer.prototype.slice)
|
||||
|
||||
exports.arraybuffer = haveArrayBuffer && checkTypeSupport('arraybuffer')
|
||||
// If fetch is supported, then arraybuffer will be supported too. Skip calling
|
||||
// checkTypeSupport(), since that calls getXHR().
|
||||
exports.arraybuffer = exports.fetch || (haveArrayBuffer && checkTypeSupport('arraybuffer'))
|
||||
|
||||
// These next two tests unavoidably show warnings in Chrome. Since fetch will always
|
||||
// be used if it's available, just return false for these to avoid the warnings.
|
||||
exports.msstream = !exports.fetch && haveSlice && checkTypeSupport('ms-stream')
|
||||
exports.mozchunkedarraybuffer = !exports.fetch && haveArrayBuffer &&
|
||||
checkTypeSupport('moz-chunked-arraybuffer')
|
||||
exports.overrideMimeType = isFunction(xhr.overrideMimeType)
|
||||
|
||||
// If fetch is supported, then overrideMimeType will be supported too. Skip calling
|
||||
// getXHR().
|
||||
exports.overrideMimeType = exports.fetch || (getXHR() ? isFunction(getXHR().overrideMimeType) : false)
|
||||
|
||||
exports.vbArray = isFunction(global.VBArray)
|
||||
|
||||
function isFunction (value) {
|
||||
return typeof value === 'function'
|
||||
return typeof value === 'function'
|
||||
}
|
||||
|
||||
xhr = null // Help gc
|
||||
|
||||
+38
-14
@@ -38,8 +38,9 @@ var ClientRequest = module.exports = function (opts) {
|
||||
|
||||
var preferBinary
|
||||
var useFetch = true
|
||||
if (opts.mode === 'disable-fetch') {
|
||||
// If the use of XHR should be preferred and includes preserving the 'content-type' header
|
||||
if (opts.mode === 'disable-fetch' || 'timeout' in opts) {
|
||||
// If the use of XHR should be preferred and includes preserving the 'content-type' header.
|
||||
// Force XHR to be used since the Fetch API does not yet support timeouts.
|
||||
useFetch = false
|
||||
preferBinary = true
|
||||
} else if (opts.mode === 'prefer-streaming') {
|
||||
@@ -80,8 +81,10 @@ ClientRequest.prototype.setHeader = function (name, value) {
|
||||
}
|
||||
|
||||
ClientRequest.prototype.getHeader = function (name) {
|
||||
var self = this
|
||||
return self._headers[name.toLowerCase()].value
|
||||
var header = this._headers[name.toLowerCase()]
|
||||
if (header)
|
||||
return header.value
|
||||
return null
|
||||
}
|
||||
|
||||
ClientRequest.prototype.removeHeader = function (name) {
|
||||
@@ -97,8 +100,8 @@ ClientRequest.prototype._onFinish = function () {
|
||||
var opts = self._opts
|
||||
|
||||
var headersObj = self._headers
|
||||
var body
|
||||
if (opts.method === 'POST' || opts.method === 'PUT' || opts.method === 'PATCH') {
|
||||
var body = null
|
||||
if (opts.method !== 'GET' && opts.method !== 'HEAD') {
|
||||
if (capability.blobConstructor) {
|
||||
body = new global.Blob(self._body.map(function (buffer) {
|
||||
return toArrayBuffer(buffer)
|
||||
@@ -111,15 +114,25 @@ ClientRequest.prototype._onFinish = function () {
|
||||
}
|
||||
}
|
||||
|
||||
if (self._mode === 'fetch') {
|
||||
var headers = Object.keys(headersObj).map(function (name) {
|
||||
return [headersObj[name].name, headersObj[name].value]
|
||||
})
|
||||
// create flattened list of headers
|
||||
var headersList = []
|
||||
Object.keys(headersObj).forEach(function (keyName) {
|
||||
var name = headersObj[keyName].name
|
||||
var value = headersObj[keyName].value
|
||||
if (Array.isArray(value)) {
|
||||
value.forEach(function (v) {
|
||||
headersList.push([name, v])
|
||||
})
|
||||
} else {
|
||||
headersList.push([name, value])
|
||||
}
|
||||
})
|
||||
|
||||
if (self._mode === 'fetch') {
|
||||
global.fetch(self._opts.url, {
|
||||
method: self._opts.method,
|
||||
headers: headers,
|
||||
body: body,
|
||||
headers: headersList,
|
||||
body: body || undefined,
|
||||
mode: 'cors',
|
||||
credentials: opts.withCredentials ? 'include' : 'same-origin'
|
||||
}).then(function (response) {
|
||||
@@ -149,8 +162,15 @@ ClientRequest.prototype._onFinish = function () {
|
||||
if (self._mode === 'text' && 'overrideMimeType' in xhr)
|
||||
xhr.overrideMimeType('text/plain; charset=x-user-defined')
|
||||
|
||||
Object.keys(headersObj).forEach(function (name) {
|
||||
xhr.setRequestHeader(headersObj[name].name, headersObj[name].value)
|
||||
if ('timeout' in opts) {
|
||||
xhr.timeout = opts.timeout
|
||||
xhr.ontimeout = function () {
|
||||
self.emit('timeout')
|
||||
}
|
||||
}
|
||||
|
||||
headersList.forEach(function (header) {
|
||||
xhr.setRequestHeader(header[0], header[1])
|
||||
})
|
||||
|
||||
self._response = null
|
||||
@@ -220,6 +240,10 @@ ClientRequest.prototype._connect = function () {
|
||||
return
|
||||
|
||||
self._response = new IncomingMessage(self._xhr, self._fetchResponse, self._mode)
|
||||
self._response.on('error', function(err) {
|
||||
self.emit('error', err)
|
||||
})
|
||||
|
||||
self.emit('response', self._response)
|
||||
}
|
||||
|
||||
|
||||
+8
-6
@@ -34,12 +34,12 @@ var IncomingMessage = exports.IncomingMessage = function (xhr, response, mode) {
|
||||
self.url = response.url
|
||||
self.statusCode = response.status
|
||||
self.statusMessage = response.statusText
|
||||
// backwards compatible version of for (<item> of <iterable>):
|
||||
// for (var <item>,_i,_it = <iterable>[Symbol.iterator](); <item> = (_i = _it.next()).value,!_i.done;)
|
||||
for (var header, _i, _it = response.headers[Symbol.iterator](); header = (_i = _it.next()).value, !_i.done;) {
|
||||
self.headers[header[0].toLowerCase()] = header[1]
|
||||
self.rawHeaders.push(header[0], header[1])
|
||||
}
|
||||
|
||||
response.headers.forEach(function(header, key){
|
||||
self.headers[key.toLowerCase()] = header
|
||||
self.rawHeaders.push(key, header)
|
||||
})
|
||||
|
||||
|
||||
// TODO: this doesn't respect backpressure. Once WritableStream is available, this can be fixed
|
||||
var reader = response.body.getReader()
|
||||
@@ -53,6 +53,8 @@ var IncomingMessage = exports.IncomingMessage = function (xhr, response, mode) {
|
||||
}
|
||||
self.push(new Buffer(result.value))
|
||||
read()
|
||||
}).catch(function(err) {
|
||||
self.emit('error', err)
|
||||
})
|
||||
}
|
||||
read()
|
||||
|
||||
+4
-4
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "stream-http",
|
||||
"version": "2.4.0",
|
||||
"version": "2.7.1",
|
||||
"description": "Streaming http in the browser",
|
||||
"main": "index.js",
|
||||
"repository": {
|
||||
@@ -27,9 +27,9 @@
|
||||
"http-browserify"
|
||||
],
|
||||
"dependencies": {
|
||||
"builtin-status-codes": "^2.0.0",
|
||||
"builtin-status-codes": "^3.0.0",
|
||||
"inherits": "^2.0.1",
|
||||
"readable-stream": "^2.1.0",
|
||||
"readable-stream": "^2.2.6",
|
||||
"to-arraybuffer": "^1.0.0",
|
||||
"xtend": "^4.0.0"
|
||||
},
|
||||
@@ -37,7 +37,7 @@
|
||||
"basic-auth": "^1.0.3",
|
||||
"brfs": "^1.4.0",
|
||||
"cookie-parser": "^1.3.5",
|
||||
"express": "^4.13.0",
|
||||
"express": "^4.15.2",
|
||||
"tape": "^4.0.0",
|
||||
"ua-parser-js": "^0.7.7",
|
||||
"webworkify": "^1.0.2",
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
var Buffer = require('buffer').Buffer
|
||||
var fs = require('fs')
|
||||
var test = require('tape')
|
||||
|
||||
var http = require('../..')
|
||||
|
||||
var reference = fs.readFileSync(__dirname + '/../server/static/basic.txt')
|
||||
|
||||
test('get body empty', function (t) {
|
||||
var req = http.request({
|
||||
path: '/verifyEmpty',
|
||||
method: 'GET'
|
||||
}, function (res) {
|
||||
var buffers = []
|
||||
|
||||
res.on('end', function () {
|
||||
console.log(Buffer.concat(buffers).toString('utf8'))
|
||||
t.ok(Buffer.from('empty').equals(Buffer.concat(buffers)), 'response body indicates request body was empty')
|
||||
t.end()
|
||||
})
|
||||
|
||||
res.on('data', function (data) {
|
||||
buffers.push(data)
|
||||
})
|
||||
})
|
||||
|
||||
req.write(reference)
|
||||
req.end()
|
||||
})
|
||||
+38
-2
@@ -46,6 +46,40 @@ test('headers', function (t) {
|
||||
})
|
||||
})
|
||||
|
||||
test('arrays of headers', function (t) {
|
||||
http.get({
|
||||
path: '/testHeaders?Response-Header=bar&Response-Header=BAR2',
|
||||
headers: {
|
||||
'Test-Request-Header': ['foo', 'FOO2']
|
||||
}
|
||||
}, function (res) {
|
||||
var rawHeaders = []
|
||||
for (var i = 0; i < res.rawHeaders.length; i += 2) {
|
||||
var lowerKey = res.rawHeaders[i].toLowerCase()
|
||||
if (lowerKey.indexOf('test-') === 0)
|
||||
rawHeaders.push(lowerKey, res.rawHeaders[i + 1])
|
||||
}
|
||||
t.equal(rawHeaders[0], 'test-response-header', 'raw response header present')
|
||||
t.equal(rawHeaders[1], 'bar, BAR2', 'raw response header value')
|
||||
t.equal(rawHeaders.length, 2, 'correct number of raw headers')
|
||||
|
||||
t.equal(res.headers['test-response-header'], 'bar, BAR2', 'response header')
|
||||
|
||||
var buffers = []
|
||||
|
||||
res.on('end', function () {
|
||||
var body = JSON.parse(Buffer.concat(buffers).toString())
|
||||
t.equal(body['test-request-header'], 'foo,FOO2', 'request headers')
|
||||
t.equal(Object.keys(body).length, 1, 'correct number of request headers')
|
||||
t.end()
|
||||
})
|
||||
|
||||
res.on('data', function (data) {
|
||||
buffers.push(data)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
test('content-type response header', function (t) {
|
||||
http.get('/testHeaders', function (res) {
|
||||
t.equal(res.headers['content-type'], 'application/json', 'content-type preserved')
|
||||
@@ -56,10 +90,12 @@ test('content-type response header', function (t) {
|
||||
var browser = (new UAParser()).setUA(navigator.userAgent).getBrowser()
|
||||
var browserName = browser.name
|
||||
var browserVersion = browser.major
|
||||
var browserMinorVersion = browser.minor || 0
|
||||
// The content-type header is broken when 'prefer-streaming' or 'allow-wrong-content-type'
|
||||
// is passed in browsers that rely on xhr.overrideMimeType(), namely older chrome and newer safari
|
||||
// is passed in browsers that rely on xhr.overrideMimeType(), namely older chrome, safari 6-10.0, and the stock Android browser
|
||||
var wrongMimeType = ((browserName === 'Chrome' && browserVersion <= 42) ||
|
||||
((browserName === 'Safari' || browserName === 'Mobile Safari') && browserVersion >= 6))
|
||||
((browserName === 'Safari' || browserName === 'Mobile Safari') && browserVersion >= 6 && (browserVersion < 10 || (browserVersion == 10 && browserMinorVersion == 0)))
|
||||
|| (browserName === 'Android Browser'))
|
||||
|
||||
test('content-type response header with forced streaming', function (t) {
|
||||
http.get({
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
var Buffer = require('buffer').Buffer
|
||||
var fs = require('fs')
|
||||
var test = require('tape')
|
||||
var UAParser = require('ua-parser-js')
|
||||
var url = require('url')
|
||||
|
||||
var http = require('../..')
|
||||
|
||||
var browser = (new UAParser()).setUA(navigator.userAgent).getBrowser()
|
||||
var browserName = browser.name
|
||||
var browserVersion = browser.major
|
||||
|
||||
var skipTimeout = ((browserName === 'Opera' && browserVersion <= 12) ||
|
||||
(browserName === 'Safari' && browserVersion <= 5))
|
||||
|
||||
|
||||
test('emits timeout events', function (t) {
|
||||
if (skipTimeout) {
|
||||
return t.skip('Browser does not support setting timeouts')
|
||||
}
|
||||
|
||||
var req = http.request({
|
||||
path: '/basic.txt',
|
||||
timeout: 1
|
||||
})
|
||||
|
||||
req.on('timeout', function () {
|
||||
t.pass('timeout caught')
|
||||
t.end() // the test will timeout if this does not happen
|
||||
})
|
||||
|
||||
req.end()
|
||||
})
|
||||
+24
-2
@@ -32,13 +32,17 @@ app.get('/testHeaders', function (req, res) {
|
||||
})
|
||||
|
||||
res.setHeader('Content-Type', 'application/json')
|
||||
res.setHeader('Cache-Control', 'no-cache')
|
||||
|
||||
// Request headers are sent in the body as json
|
||||
var reqHeaders = {}
|
||||
Object.keys(req.headers).forEach(function (key) {
|
||||
key = key.toLowerCase()
|
||||
if (key.indexOf('test-') === 0)
|
||||
reqHeaders[key] = req.headers[key]
|
||||
if (key.indexOf('test-') === 0) {
|
||||
// different browsers format request headers with multiple values
|
||||
// slightly differently, so normalize
|
||||
reqHeaders[key] = req.headers[key].replace(', ', ',')
|
||||
}
|
||||
})
|
||||
|
||||
var body = JSON.stringify(reqHeaders)
|
||||
@@ -71,6 +75,24 @@ app.post('/echo', function (req, res) {
|
||||
req.pipe(res)
|
||||
})
|
||||
|
||||
app.use('/verifyEmpty', function (req, res) {
|
||||
var empty = true
|
||||
req.on('data', function (buf) {
|
||||
if (buf.length > 0) {
|
||||
empty = false
|
||||
}
|
||||
})
|
||||
req.on('end', function () {
|
||||
res.setHeader('Content-Type', 'text/plain')
|
||||
|
||||
if (empty) {
|
||||
res.end('empty')
|
||||
} else {
|
||||
res.end('not empty')
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
app.use(function (req, res, next) {
|
||||
var parsed = url.parse(req.url, true)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user