Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Add support for redirect URL with content length exceeds limit #1

Open
wants to merge 1 commit into from

1 participant

@qiushihe

Specify CAMO_DEFAULT_URL in the environment variables to set a URL to redirect to when content length exceeds limit.

Also updated node version requirement to 0.8.x in order to be able to deploy to heroku.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
Showing with 26 additions and 6 deletions.
  1. +2 −2 package.json
  2. +7 −1 server.coffee
  3. +17 −3 server.js
View
4 package.json
@@ -4,6 +4,6 @@
"dependencies": {
},
"engines": {
- "node": ">=0.4.10 <0.5.0"
+ "node": "0.8.x"
}
-}
+}
View
8 server.coffee
@@ -12,6 +12,9 @@ max_redirects = process.env.CAMO_MAX_REDIRECTS || 4
camo_hostname = process.env.CAMO_HOSTNAME || "unknown"
logging_enabled = process.env.CAMO_LOGGING_ENABLED || "disabled"
+default_url = process.env.CAMO_DEFAULT_URL || null
+default_url = Url.parse default_url if default_url
+
log = (msg) ->
unless logging_enabled == "disabled"
console.log("--------------------------------------------")
@@ -63,7 +66,10 @@ process_url = (url, transferred_headers, resp, remaining_redirects) ->
content_length = srcResp.headers['content-length']
if content_length > 5242880
- four_oh_four(resp, "Content-Length exceeded")
+ if default_url
+ process_url default_url, transferred_headers, resp, remaining_redirects - 1
+ else
+ four_oh_four(resp, "Content-Length exceeded")
else
newHeaders =
'content-type' : srcResp.headers['content-type']
View
20 server.js
@@ -1,6 +1,6 @@
-// Generated by CoffeeScript 1.4.0
+// Generated by CoffeeScript 1.6.2
(function() {
- var Crypto, EXCLUDED_HOSTS, Fs, Http, QueryString, RESTRICTED_IPS, Url, camo_hostname, current_connections, excluded, finish, four_oh_four, hexdec, log, logging_enabled, max_redirects, port, process_url, server, shared_key, started_at, total_connections, version;
+ var Crypto, EXCLUDED_HOSTS, Fs, Http, QueryString, RESTRICTED_IPS, Url, camo_hostname, current_connections, default_url, excluded, finish, four_oh_four, hexdec, log, logging_enabled, max_redirects, port, process_url, server, shared_key, started_at, total_connections, version;
Fs = require('fs');
@@ -26,6 +26,12 @@
logging_enabled = process.env.CAMO_LOGGING_ENABLED || "disabled";
+ default_url = process.env.CAMO_DEFAULT_URL || null;
+
+ if (default_url) {
+ default_url = Url.parse(default_url);
+ }
+
log = function(msg) {
if (logging_enabled !== "disabled") {
console.log("--------------------------------------------");
@@ -60,6 +66,7 @@
process_url = function(url, transferred_headers, resp, remaining_redirects) {
var query_path, src, srcReq;
+
if ((url.host != null) && !url.host.match(RESTRICTED_IPS)) {
if (url.host.match(EXCLUDED_HOSTS)) {
return four_oh_four(resp, "Hitting excluded hostnames");
@@ -77,11 +84,16 @@
srcReq = src.request('GET', query_path, transferred_headers);
srcReq.on('response', function(srcResp) {
var content_length, is_finished, newHeaders, newUrl;
+
is_finished = true;
log(srcResp.headers);
content_length = srcResp.headers['content-length'];
if (content_length > 5242880) {
- return four_oh_four(resp, "Content-Length exceeded");
+ if (default_url) {
+ return process_url(default_url, transferred_headers, resp, remaining_redirects - 1);
+ } else {
+ return four_oh_four(resp, "Content-Length exceeded");
+ }
} else {
newHeaders = {
'content-type': srcResp.headers['content-type'],
@@ -153,6 +165,7 @@
hexdec = function(str) {
var buf, i, _i, _ref;
+
if (str && str.length > 0 && str.length % 2 === 0 && !str.match(/[^0-9a-f]/)) {
buf = new Buffer(str.length / 2);
for (i = _i = 0, _ref = str.length; _i < _ref; i = _i += 2) {
@@ -164,6 +177,7 @@
server = Http.createServer(function(req, resp) {
var dest_url, encoded_url, hmac, hmac_digest, query_digest, transferred_headers, url, url_type, user_agent, _base, _ref, _ref1;
+
if (req.method !== 'GET' || req.url === '/') {
resp.writeHead(200);
return resp.end('hwhat');
Something went wrong with that request. Please try again.