Skip to content

Commit

Permalink
Fixing case where encoding is set. Also cleaning up trailing whitespa…
Browse files Browse the repository at this point in the history
…ce because my editor likes to do that now.
  • Loading branch information
mikeal committed Sep 9, 2011
1 parent 604f1fb commit 08ca561
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 42 deletions.
89 changes: 47 additions & 42 deletions main.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
// Copyright 2010-2011 Mikeal Rogers
//
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//
// http://www.apache.org/licenses/LICENSE-2.0
//
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Expand Down Expand Up @@ -51,7 +51,7 @@ if (https && !https.Agent) {

function isReadStream (rs) {
if (rs.readable && rs.path && rs.mode) {
return true
return true
}
}

Expand All @@ -69,11 +69,11 @@ function Request (options) {
stream.Stream.call(this)
this.readable = true
this.writable = true

if (typeof options === 'string') {
options = {uri:options}
}

for (var i in options) {
this[i] = options[i]
}
Expand All @@ -88,7 +88,7 @@ Request.prototype.getAgent = function (host, port) {
}
return this.pool[host+':'+port]
}
Request.prototype.request = function () {
Request.prototype.request = function () {
var options = this
if (options.url) {
// People use this property instead all the time so why not just support it.
Expand All @@ -108,7 +108,7 @@ Request.prototype.request = function () {
options._redirectsFollowed = options._redirectsFollowed || 0
options.maxRedirects = (options.maxRedirects !== undefined) ? options.maxRedirects : 10
options.followRedirect = (options.followRedirect !== undefined) ? options.followRedirect : true

options.headers = options.headers ? copy(options.headers) : {}

var setHost = false
Expand All @@ -132,27 +132,27 @@ Request.prototype.request = function () {
console.error('options.bodyStream and options.responseBodyStream is deprecated. You should now send the request object to stream.pipe()')
this.pipe(options.responseBodyStream || options.bodyStream)
}

if (options.proxy) {
options.port = options.proxy.port
options.host = options.proxy.hostname
} else {
options.port = options.uri.port
options.host = options.uri.hostname
}

if (options.onResponse === true) {
options.onResponse = options.callback
delete options.callback
}

var clientErrorHandler = function (error) {
if (setHost) delete options.headers.host
options.emit('error', error)
}
if (options.onResponse) options.on('error', function (e) {options.onResponse(e)})
if (options.onResponse) options.on('error', function (e) {options.onResponse(e)})
if (options.callback) options.on('error', function (e) {options.callback(e)})


if (options.uri.auth && !options.headers.authorization) {
options.headers.authorization = "Basic " + toBase64(options.uri.auth.split(':').map(function(item){ return qs.unescape(item)}).join(':'))
Expand All @@ -173,17 +173,17 @@ Request.prototype.request = function () {
} else {
options.body = JSON.stringify(options.json)
}

} else if (options.multipart) {
options.body = ''
options.headers['content-type'] = 'multipart/related;boundary="frontier"'
if (!options.multipart.forEach) throw new Error('Argument error, options.multipart.')

options.multipart.forEach(function (part) {
var body = part.body
if(!body) throw Error('Body attribute missing in multipart.')
delete part.body
options.body += '--frontier\r\n'
options.body += '--frontier\r\n'
Object.keys(part).forEach(function(key){
options.body += key + ': ' + part[key] + '\r\n'
})
Expand All @@ -202,12 +202,12 @@ Request.prototype.request = function () {
throw new Error('Argument error, options.body.')
}
}
options.httpModule =

options.httpModule =
{"http:":http, "https:":https}[options.proxy ? options.proxy.protocol : options.uri.protocol]

if (!options.httpModule) throw new Error("Invalid protocol")

if (options.pool === false) {
options.agent = false
} else {
Expand All @@ -222,7 +222,7 @@ Request.prototype.request = function () {
options.agent.maxSockets = options.pool.maxSockets
}
}

options.start = function () {
options._started = true
options.method = options.method || 'GET'
Expand All @@ -240,18 +240,18 @@ Request.prototype.request = function () {
if (setHost) delete options.headers.host
if (options.timeout && options.timeoutTimer) clearTimeout(options.timeoutTimer)

if (response.statusCode >= 300 &&
response.statusCode < 400 &&
options.followRedirect &&
options.method !== 'PUT' &&
if (response.statusCode >= 300 &&
response.statusCode < 400 &&
options.followRedirect &&
options.method !== 'PUT' &&
options.method !== 'POST' &&
response.headers.location) {
if (options._redirectsFollowed >= options.maxRedirects) {
options.emit('error', new Error("Exceeded maxRedirects. Probably stuck in a redirect loop."))
return
}
options._redirectsFollowed += 1

if (!isUrl.test(response.headers.location)) {
response.headers.location = url.resolve(options.uri.href, response.headers.location)
}
Expand Down Expand Up @@ -286,7 +286,7 @@ Request.prototype.request = function () {
if (response.headers['content-length']) {
dest.headers['content-length'] = response.headers['content-length']
}
}
}
if (dest.setHeader) {
for (var i in response.headers) {
dest.setHeader(i, response.headers[i])
Expand All @@ -298,7 +298,7 @@ Request.prototype.request = function () {

response.on("data", function (chunk) {options.emit("data", chunk)})
response.on("end", function (chunk) {
options._ended = true
options._ended = true
options.emit("end", chunk)
})
response.on("close", function () {options.emit("close")})
Expand All @@ -309,25 +309,30 @@ Request.prototype.request = function () {
if (options.callback) {
var buffer = []
var bodyLen = 0
options.on("data", function (chunk) {
options.on("data", function (chunk) {
buffer.push(chunk)
bodyLen += chunk.length
})
options.on("end", function () {
var body = new Buffer(bodyLen)
var i = 0
buffer.forEach(function (chunk) {
chunk.copy(body, i, 0, chunk.length)
i += chunk.length
})
response.body = body.toString()
options.on("end", function () {
if (Buffer.isBuffer(buffer[0])) {
var body = new Buffer(bodyLen)
var i = 0
buffer.forEach(function (chunk) {
chunk.copy(body, i, 0, chunk.length)
i += chunk.length
})
response.body = body.toString()
} else {
response.body = buffer.join('')
}

if (options.json) {
try {
response.body = JSON.parse(response.body)
} catch (e) {}
}
options.callback(null, response, response.body)
})
options.callback(null, response, response.body)
})
}
}
})
Expand All @@ -342,8 +347,8 @@ Request.prototype.request = function () {
}

options.req.on('error', clientErrorHandler)
}
}

options.once('pipe', function (src) {
if (options.ntick) throw new Error("You cannot pipe to this stream after the first nextTick() after creation of the request stream.")
options.src = src
Expand All @@ -362,12 +367,12 @@ Request.prototype.request = function () {
options.method = src.method
}
}

options.on('pipe', function () {
console.error("You have already piped to this stream. Pipeing twice is likely to break the request.")
})
})

process.nextTick(function () {
if (options.body) {
options.write(options.body)
Expand Down
Empty file modified tests/run.sh
100644 → 100755
Empty file.

0 comments on commit 08ca561

Please sign in to comment.