From 09ea924eb25de649740bda73453cd86c22f82651 Mon Sep 17 00:00:00 2001 From: Sandro Santilli Date: Thu, 20 Feb 2014 10:17:48 +0100 Subject: [PATCH] Allow using GET with sql-api for queries shorter than configured len Introduces new sqlapi.max_get_sql_length directive, defaults to 2048. Closes #155 Includes testcases. --- NEWS.md | 2 ++ config/environments/development.js.example | 6 +++++- config/environments/production.js.example | 6 +++++- config/environments/staging.js.example | 6 +++++- config/environments/test.js.example | 6 +++++- lib/cartodb/server_options.js | 25 +++++++++++++++------- test/acceptance/multilayer.js | 4 +++- test/acceptance/server.js | 1 + 8 files changed, 43 insertions(+), 13 deletions(-) diff --git a/NEWS.md b/NEWS.md index 03971323..544a43f2 100644 --- a/NEWS.md +++ b/NEWS.md @@ -5,6 +5,8 @@ Enhancements: * Allow using ":host" as part of statsd.prefix (#153) * Expand "addCacheChannel" stats + * Allow using GET with sql-api for queries shorter than configured len (#155) + [ new sqlapi.max_get_sql_length directive, defaults to 2048 ] Bug fixes: diff --git a/config/environments/development.js.example b/config/environments/development.js.example index b646be3d..ec1feba4 100644 --- a/config/environments/development.js.example +++ b/config/environments/development.js.example @@ -102,7 +102,11 @@ var config = { // the cartodb username and passed to // SQL-API requests in the Host HTTP header domain: 'localhost.lan', - version: 'v1' + version: 'v1', + // Maximum lenght of SQL query for GET + // requests. Longer queries will be sent + // using POST. Defaults to 2048 + max_get_sql_length: 2048 } ,varnish: { host: 'localhost', diff --git a/config/environments/production.js.example b/config/environments/production.js.example index da9d6e9d..fab36404 100644 --- a/config/environments/production.js.example +++ b/config/environments/production.js.example @@ -96,7 +96,11 @@ var config = { // the cartodb username and passed to // SQL-API requests in the Host HTTP header domain: 'cartodb.com', - version: 'v2' + version: 'v2', + // Maximum lenght of SQL query for GET + // requests. Longer queries will be sent + // using POST. Defaults to 2048 + max_get_sql_length: 2048 } ,varnish: { host: 'localhost', diff --git a/config/environments/staging.js.example b/config/environments/staging.js.example index 953ea97e..35f356d0 100644 --- a/config/environments/staging.js.example +++ b/config/environments/staging.js.example @@ -96,7 +96,11 @@ var config = { // the cartodb username and passed to // SQL-API requests in the Host HTTP header domain: 'cartodb.com', - version: 'v2' + version: 'v2', + // Maximum lenght of SQL query for GET + // requests. Longer queries will be sent + // using POST. Defaults to 2048 + max_get_sql_length: 2048 } ,varnish: { host: 'localhost', diff --git a/config/environments/test.js.example b/config/environments/test.js.example index 5c603efd..f5a3a77d 100644 --- a/config/environments/test.js.example +++ b/config/environments/test.js.example @@ -98,7 +98,11 @@ var config = { domain: 'donot_look_this_up', // This port will be used by "make check" for testing purposes // It must be available - version: 'v1' + version: 'v1', + // Maximum lenght of SQL query for GET + // requests. Longer queries will be sent + // using POST. Defaults to 2048 + max_get_sql_length: 2048 } ,varnish: { host: '', diff --git a/lib/cartodb/server_options.js b/lib/cartodb/server_options.js index 359fd6f5..6888700a 100644 --- a/lib/cartodb/server_options.js +++ b/lib/cartodb/server_options.js @@ -115,14 +115,23 @@ module.exports = function(){ // See http://nodejs.org/api/http.html#http_agent_maxsockets // var maxSockets = global.environment.maxConnections || 128; - request.post({ - url:sqlapi, body:qs, json:true, - headers:{host: sqlapihostname} - // http://nodejs.org/api/http.html#http_agent_maxsockets - ,pool:{maxSockets:maxSockets} - //,timeout:100 - }, function(err, res, body) - { + var maxGetLen = api.max_get_sql_length || 2048; + var reqSpec = { + url:sqlapi, + json:true, + headers:{host: sqlapihostname} + // http://nodejs.org/api/http.html#http_agent_maxsockets + ,pool:{maxSockets:maxSockets} + //,timeout:100 + } + if ( sql.length > maxGetLen ) { + reqSpec.method = 'POST'; + reqSpec.body = qs; + } else { + reqSpec.method = 'GET'; + reqSpec.qs = qs; + } + request(reqSpec, function(err, res, body) { if (err){ console.log('ERROR connecting to SQL API on ' + sqlapi + ': ' + err); callback(err); diff --git a/test/acceptance/multilayer.js b/test/acceptance/multilayer.js index 07f094b5..78519a4d 100644 --- a/test/acceptance/multilayer.js +++ b/test/acceptance/multilayer.js @@ -1059,10 +1059,12 @@ suite('multilayer', function() { var parsedBody = JSON.parse(res.body); var token_components = parsedBody.layergroupid.split(':'); expected_token = token_components[0]; + var last_request = sqlapi_server.getLastRequest(); + assert.equal(last_request.method, 'POST'); return null; }, function cleanup(err) { - if ( err ) errors.push(err.message); + if ( err ) errors.push('' + err); if ( ! expected_token ) return null; var next = this; redis_client.keys("map_cfg|" + expected_token, function(err, matches) { diff --git a/test/acceptance/server.js b/test/acceptance/server.js index fed167c6..9a0a2ee5 100644 --- a/test/acceptance/server.js +++ b/test/acceptance/server.js @@ -1148,6 +1148,7 @@ suite('server', function() { assert.ok(last_request); var host = last_request.headers['host']; assert.ok(host); + assert.equal(last_request.method, 'GET'); assert.equal(host, 'localhost.donot_look_this_up'); return null; },