Skip to content

Commit

Permalink
Rework parsing of raw response HTTP headers
Browse files Browse the repository at this point in the history
Now splits by `\r\n` because that's the header line delimiter.
No tests because I can't figure out how to return a manually formatted
headers payload from Node.js HTTP server.

Fixes #422
  • Loading branch information
mislav committed Nov 10, 2016
1 parent b99a4f2 commit 57565a1
Showing 1 changed file with 12 additions and 24 deletions.
36 changes: 12 additions & 24 deletions fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -311,16 +311,17 @@
return form
}

function headers(xhr) {
var head = new Headers()
var pairs = (xhr.getAllResponseHeaders() || '').trim().split('\n')
pairs.forEach(function(header) {
var split = header.trim().split(':')
var key = split.shift().trim()
var value = split.join(':').trim()
head.append(key, value)
function parseHeaders(rawHeaders) {
var headers = new Headers()
rawHeaders.split('\r\n').forEach(function(line) {
var parts = line.split(':')
var key = parts.shift().trim()
if (key) {
var value = parts.join(':').trim()
headers.append(key, value)
}
})
return head
return headers
}

Body.call(Request.prototype)
Expand Down Expand Up @@ -375,26 +376,13 @@
var request = new Request(input, init)
var xhr = new XMLHttpRequest()

function responseURL() {
if ('responseURL' in xhr) {
return xhr.responseURL
}

// Avoid security warnings on getResponseHeader when not allowed by CORS
if (/^X-Request-URL:/mi.test(xhr.getAllResponseHeaders())) {
return xhr.getResponseHeader('X-Request-URL')
}

return
}

xhr.onload = function() {
var options = {
status: xhr.status,
statusText: xhr.statusText,
headers: headers(xhr),
url: responseURL()
headers: parseHeaders(xhr.getAllResponseHeaders() || '')
}
options.url = 'responseURL' in xhr ? xhr.responseURL : options.headers.get('X-Request-URL')
var body = 'response' in xhr ? xhr.response : xhr.responseText
resolve(new Response(body, options))
}
Expand Down

0 comments on commit 57565a1

Please sign in to comment.