-
Notifications
You must be signed in to change notification settings - Fork 238
Fix redirect with head with body #444
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
tomas
merged 5 commits into
tomas:master
from
cristiklein:fix-redirect-with-head-with-body
Nov 19, 2025
+118
−6
Merged
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
ff1223c
Add editorconfig
cristiklein 1af2d01
Add redirect_with_bad_redirector test
cristiklein e51e67b
Add redirect test with follow_keep_method
cristiklein 4f3771b
Avoid calling back with errors after redirect
cristiklein a52baad
Rename to unlisten_errors
cristiklein File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,7 @@ | ||
| root = true | ||
|
|
||
| [*] | ||
| charset = utf-8 | ||
| end_of_line = lf | ||
| indent_style = space | ||
| indent_size = 2 |
This file contains hidden or 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 hidden or 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,65 @@ | ||
| var helpers = require('./helpers'), | ||
| should = require('should'), | ||
| sinon = require('sinon'), | ||
| http = require('http'), | ||
| needle = require('./../'); | ||
|
|
||
| const port = 1234; | ||
|
|
||
| describe('redirects with bad redirector', function() { | ||
|
|
||
| var spies = {}, | ||
| servers = {}; | ||
|
|
||
| before(function(done) { | ||
| servers.http = http.createServer(function(req, res) { | ||
| if (req.url == '/foo/bar') { | ||
| res.end('Redirected successfully!'); | ||
| return; | ||
| } | ||
|
|
||
| /* Don't judge me. I found at least one server on the | ||
| * Internet doing this and it triggered a bug. */ | ||
| const body = 'Let me send you a body, although you only asked for a HEAD.'; | ||
|
|
||
| const headers = | ||
| 'HTTP/1.1 302 Found\r\n' + | ||
| 'Connection: close\r\n' + | ||
| 'Location: /foo/bar\r\n' + | ||
| `Content-Length: ${Buffer.byteLength(body)}\r\n` + | ||
| '\r\n'; | ||
|
|
||
| res.socket.write(headers + body); | ||
| res.socket.destroy(); | ||
| }).listen(port, done); | ||
| }) | ||
|
|
||
| after(function(done) { | ||
| servers.http.close(done); | ||
| }) | ||
|
|
||
| it('calls back exactly once', function (done) { | ||
| const opts = { | ||
| follow: 5, | ||
| } | ||
|
|
||
| const url = `http://localhost:${port}` | ||
| needle.head(url, opts, function (err, resp, body) { | ||
| //should(body && body.toString()).eql('Redirected successfully!'); | ||
| done(); | ||
| }); | ||
| }); | ||
|
|
||
| it('calls back exactly once with follow_keep_method', function (done) { | ||
| const opts = { | ||
| follow: 5, | ||
| follow_keep_method: true, | ||
| } | ||
|
|
||
| const url = `http://localhost:${port}` | ||
| needle.head(url, opts, function (err, resp, body) { | ||
| //should(body && body.toString()).eql('Redirected successfully!'); | ||
| done(); | ||
| }); | ||
| }); | ||
| }); |
This file contains hidden or 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,34 @@ | ||
| var http = require('http'), | ||
| https = require('https'), | ||
| url = require('url'); | ||
|
|
||
| var port = 1234, | ||
| log = true, | ||
| request_auth = false; | ||
|
|
||
| http.createServer(function(req, res) { | ||
|
|
||
| console.log(req.headers); | ||
| console.log("Got request: " + req.method + " " + req.url); | ||
|
|
||
| /* Don't judge me. I found at least one server on the | ||
| * Internet doing this and it triggered a bug. */ | ||
| const body = 'Let me send you a body, although you only asked for a HEAD.'; | ||
|
|
||
| const headers = | ||
| 'HTTP/1.1 302 Found\r\n' + | ||
| 'Connection: close\r\n' + | ||
| 'Location: /foo/bar\r\n' + | ||
| `Content-Length: ${Buffer.byteLength(body)}\r\n` + | ||
| '\r\n'; | ||
|
|
||
| res.socket.write(headers + body); | ||
| res.socket.destroy(); | ||
| }).listen(port); | ||
|
|
||
| process.on('uncaughtException', function(err){ | ||
| console.log('Uncaught exception!'); | ||
| console.log(err); | ||
| }); | ||
|
|
||
| console.log("Bad redirector listening on port " + port); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.