Skip to content
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

Added follow redirects feature. #363

Merged
merged 8 commits into from Mar 20, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
56 changes: 53 additions & 3 deletions modules/engine/lib/engine/http/request.js
Expand Up @@ -81,8 +81,58 @@ function putInCache(key, cache, result, res, timeout) {
} }
} }


function sendHttpRequest(client, options, args, start, timings, reqStart, key, cache, timeout, uniqueId, status, retry) { var followRedirects = true, maxRedirects = 10;

function sendHttpRequest(client, options, args, start, timings, reqStart, key, cache, timeout, uniqueId, status, retry, redirects) {
var clientRequest = client.request(options, function (res) { var clientRequest = client.request(options, function (res) {
if (followRedirects && (res.statusCode >= 301 && res.statusCode <= 307) &&
(options.method.toUpperCase() === 'GET' || options.method.toUpperCase() === 'HEAD')) {
res.socket.destroy();
if (res.statusCode === 305) { // Log but don't follow
args.logEmitter.emitWarning(args.httpReqTx.event, 'Warning with uri - ' + args.uri + ' - ' +
'Received status code 305 ' + (Date.now() - start) + 'msec');
var err = new Error('Received status code 305 from downstream server');
err.uri = args.uri;
err.status = 502;
return args.httpReqTx.cb(err);
} else if (res.statusCode !== 304 && res.statusCode !== 306) { // Only follow 301, 302, 303, 307
if (res.headers.location) {
if (redirects++ >= maxRedirects) {
args.logEmitter.emitError(args.httpReqTx.event, 'Error with uri - ' + args.uri + ' - ' +
'Exceeded max redirects (' + maxRedirects + '). In a loop? ' + (Date.now() - start) + 'msec');
var err = new Error('Exceeded max redirects');
err.uri = args.uri;
err.status = 502;
return args.httpReqTx.cb(err);
}

var location = new URI(res.headers.location);

if (location.isAbsolute()) {
options.host = location.heirpart().authority().host();
options.port = location.heirpart().authority().port();
} else {
location = new URI(args.uri);
location = location.resolveReference(res.headers.location);
}
options.path = location.heirpart().path();

args.logEmitter.emitEvent(args.httpReqTx.event, 'being redirected for the ' + redirects + ' time, ' +
'going to ' + options.host + ':' + options.port + options.path + ' - ' + args.uri + ' - ' + (Date.now() - start) + 'msec');
sendHttpRequest(client, options, args, start, timings, reqStart, key, cache, timeout, uniqueId, status, retry, redirects);
return;
} else {
args.logEmitter.emitError(args.httpReqTx.event, 'Error with uri - ' + args.uri + ' - ' +
'Received status code ' + res.statusCode + ', but Location header was not provided' +
' ' + (Date.now() - start) + 'msec');
var err = new Error('Missing Location header in redirect');
err.uri = args.uri;
err.status = 502;
return args.httpReqTx.cb(err);
}
}
}

var bufs = []; // array for bufs for each chunk var bufs = []; // array for bufs for each chunk
var responseLength = 0; var responseLength = 0;
var contentEncoding = res.headers['content-encoding']; var contentEncoding = res.headers['content-encoding'];
Expand Down Expand Up @@ -228,15 +278,15 @@ function sendMessage(args, client, options, retry) {
cache.get(key,function(err,result){ cache.get(key,function(err,result){
if(err || !result.data){ if(err || !result.data){
sendHttpRequest(client, options, args, start, timings, reqStart, sendHttpRequest(client, options, args, start, timings, reqStart,
key, cache, timeout, uniqueId, status, retry); key, cache, timeout, uniqueId, status, retry, 0);
} }
else { else {
response.exec(timings, reqStart, args, uniqueId, res, result.start, result.result, options, status); response.exec(timings, reqStart, args, uniqueId, res, result.start, result.result, options, status);
} }
}); });
} }
else { else {
sendHttpRequest(client, options, args, start, timings, reqStart, key, cache, timeout, uniqueId, status, retry); sendHttpRequest(client, options, args, start, timings, reqStart, key, cache, timeout, uniqueId, status, retry, 0);
} }
} }


Expand Down
2 changes: 1 addition & 1 deletion modules/engine/package.json
@@ -1,7 +1,7 @@
{ {
"author": "ql.io", "author": "ql.io",
"name": "ql.io-engine", "name": "ql.io-engine",
"version": "0.4.19", "version": "0.4.20",
"repository": { "repository": {
"type": "git", "type": "git",
"url": "https://github.com/ql-io/ql.io" "url": "https://github.com/ql-io/ql.io"
Expand Down
5 changes: 5 additions & 0 deletions modules/engine/test/mock/redirect-rel.ql
@@ -0,0 +1,5 @@
create table redirect
on select get from "http://127.0.0.1:8300/rel/redirect-response.json"
response = select * from redirect;

return response;
4 changes: 4 additions & 0 deletions modules/engine/test/mock/redirect-response.json
@@ -0,0 +1,4 @@
{
"id": "42",
"title":"Redirect Response"
}
6 changes: 6 additions & 0 deletions modules/engine/test/mock/redirect.ql
@@ -0,0 +1,6 @@
create table redirect
on select get from "http://127.0.0.1:8300/redirect-response.json"

response = select * from redirect;

return response;