Created
August 15, 2013 08:52
-
-
Save hengkiardo/6239346 to your computer and use it in GitHub Desktop.
This file contains 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
// place into the express github root folder.... | |
require.paths.unshift('lib') | |
require('express') | |
require('express/plugins') | |
require('express/http') | |
configure(function(){ | |
var fiveMinutes = 300000, | |
oneMinute = 60000 | |
use(MethodOverride) | |
use(ContentLength) | |
use(CommonLogger) | |
use(Cookie) | |
use(Cache, { lifetime: fiveMinutes, reapInterval: oneMinute }) | |
use(Session, { lifetime: fiveMinutes, reapInterval: oneMinute }) | |
set('root', __dirname) | |
}) | |
var http = require('express/http') | |
var qs = require('querystring') | |
function fixupCookies(request_headers, headers) { | |
if (!headers['set-cookie']) return; | |
var segments = headers['set-cookie'].split(/ *, */); | |
headers['set-cookie'] = segments.reduce(function(cookies, segment) { | |
if (segment.match(/^[\w\-\$]+=/)) { | |
// cookie segment | |
cookies.push(segment); | |
} else { | |
// append (e.g. expires=Wed, xxxx) | |
cookies[cookies.length-1] += ', '+segment; | |
} | |
return cookies; | |
}, []); | |
if (request_headers['user-agent'].match(/WebKit/)) { | |
// Safari 4.x | |
headers['set-cookie'] = headers['set-cookie'].join(", "); | |
} else { | |
// works in FF3.6 | |
headers['set-cookie'] = headers['set-cookie'].join("\r\nSet-Cookie: "); | |
} | |
puts ("COOKIES: " + headers['set-cookie']); | |
} | |
function handleRequest(method, url, methodName) { | |
var request = this; | |
if (request.params.get) { | |
var gets = qs.stringify(request.params.get); | |
if (gets.length) url += '?'+gets; | |
} | |
puts(methodName + ': ' + url + " headers: " + inspect(request.headers)); | |
method('http://localhost/trunk/'+url, | |
request.body, request.headers, function(e, data, response) { | |
if (e) { | |
puts("ERR DATA: " + data + "ERR: " + inspect(e)); | |
request.halt(response.statusCode, inspect(e)); | |
return; | |
} | |
// puts("OK DATA: " + response.statusCode + " data: " + data.length + " headers: " + inspect(response.headers)); | |
request.response.headers = response.headers; | |
fixupCookies(request.headers, response.headers); | |
response.headers['connection'] = 'close'; // hangups... strange, but possible | |
request.halt(response.statusCode, data, "UTF-8"); | |
}); | |
} | |
get( '/*', function(url){ | |
return handleRequest.call(this, http.get, url, 'GET'); | |
}) | |
post('/*', function(url){ | |
return handleRequest.call(this, http.post, url, 'POST'); | |
}); | |
put( '/*', function(url){ | |
return handleRequest.call(this, http.put, url, 'PUT'); | |
}); | |
del( '/*', function(url){ | |
return handleRequest.call(this, http.del, url, 'DELETE'); | |
}); | |
run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment