Permalink
Browse files

Only retrieve files from a single S3 bucket

URLs used to be of the form:

  http://pepto-symbol.example.com/http://s3bucket.s3.amazonaws.com/foo

Now they are of the form:

  http://pepto-symbol.example.com/foo

The S3 bucket must be specified ahead of time via the S3_BUCKET
environment variable.

This allows our code to be considerably simpler.
  • Loading branch information...
1 parent 3141ae7 commit aa2fd32f2fb5a763e2de845e1497a5e25d93637e @aroben committed Apr 17, 2012
Showing with 15 additions and 24 deletions.
  1. +1 −0 .gitignore
  2. +14 −24 pepto-symbol.js
View
@@ -1 +1,2 @@
+.env
node_modules
View
@@ -1,9 +1,11 @@
-const HTTP = require('http')
- , HTTPProxy = require('http-proxy')
- , URL = require('url')
- ;
+const HTTPProxy = require('http-proxy');
-HTTPProxy.createServer(function(request, response, next) {
+const TARGET_HOST = process.env.S3_BUCKET + '.s3.amazonaws.com';
+
+// S3 returns 403 errors for files that don't exist. But when symsrv.dll sees a
+// 403 it blacklists the server for the rest of the debugging session. So we
+// convert 403s to 404s so symsrv.dll doesn't freak out.
+function convert403To404(request, response, next) {
var original = response.writeHead;
response.writeHead = function() {
var args = Array.prototype.slice.call(arguments);
@@ -12,24 +14,12 @@ HTTPProxy.createServer(function(request, response, next) {
original.apply(response, args);
};
next();
-}, function(request, response, proxy) {
- var innerURL = request.url.substr(1);
- var parsedURL = URL.parse(innerURL);
- if (!parsedURL.hostname) {
- var body = 'Invalid URL ' + innerURL;
- response.writeHead(400, {
- 'Content-Length': body.length,
- 'Content-Type': 'text/plain'
- });
- response.write(body);
- response.end();
- return;
- }
+}
- request.url = innerURL.toLowerCase();
+// S3 determines the bucket from the Host header.
+function fixHostHeader(request, response, next) {
+ request.headers['host'] = TARGET_HOST;
+ next();
+}
- proxy.proxyRequest(request, response, {
- host: parsedURL.hostname,
- port: 80,
- });
-}).listen(process.env.PORT);
+HTTPProxy.createServer(TARGET_HOST, 80, convert403To404, fixHostHeader).listen(process.env.PORT);

0 comments on commit aa2fd32

Please sign in to comment.