diff options
| author | Jules Laplace <jules@okfoc.us> | 2015-11-04 01:27:57 -0500 |
|---|---|---|
| committer | Jules Laplace <jules@okfoc.us> | 2015-11-04 01:27:57 -0500 |
| commit | 9dd3ae274650fba0deff45f04a666fe3ec5828a9 (patch) | |
| tree | 9e9af338d35eefe99f33c185192f1a0fe7dd0140 /proxy | |
| parent | bd475ae6ae43cbe1d937ab7c4326ac0dda9bc81f (diff) | |
| parent | 80f1271c496e8e9af6c3b746d1481e23363add61 (diff) | |
merge
Diffstat (limited to 'proxy')
| -rw-r--r-- | proxy/index.js | 172 |
1 files changed, 110 insertions, 62 deletions
diff --git a/proxy/index.js b/proxy/index.js index 159712e3..d2d4e738 100644 --- a/proxy/index.js +++ b/proxy/index.js @@ -1,69 +1,49 @@ var http = require('http') -var najax = require('najax') -var PORT = 4567 +var https = require('https') +var path = require('path') +var fs = require('fs') +var url = require('url') -var endpoint = "https://secure.api.yoox.biz" +var PORT = 9090 -var server = http.createServer(function (req, res){ - console.log("_________________") - console.log("[200] " + req.method + " to " + req.url); - var headers = parse_headers(req) - console.log(headers) - - if (req.method != 'GET') { - var fullBody = ''; - - req.on('data', function(chunk) { - fullBody += chunk.toString(); - }); - - req.on('end', function() { - res.writeHead(200, "OK", {'Content-Type': 'text/html'}); - - // console.log(fullBody) +var server = http.createServer().listen(PORT, function(){ + console.log("Proxy listening on: http://lvh.me:%s", PORT) +}) - najax({ - method: req.method, - url: endpoint + req.url, - data: fullBody, - headers: headers, - success: respond(res), - error: respond(res), - }) - }) - } - else { -// console.log(req) - najax({ - method: req.method, - url: endpoint + req.url, - headers: headers, - success: respond(res), - error: respond(res), - }) +server.on('request', function (req, res){ + + if ( req.method == 'GET' && ! req.url.match(/API/) ) { + // public... + return stream(req, res) } -}) -server.listen(PORT, function(){ - console.log("Proxy listening on: http://localhost:%s", PORT) -}) + console.log(req.method, req.url) + + // console.log(req.headers) + + var options = {} + options.headers = get_headers(req.headers) + options.method = req.method + options.port = 443 + options.hostname = "secure.api.yoox.biz" + options.path = req.url -function parse_headers (req) { - var headers = {} - var fields = "x-yoox-appname x-yoox-account-token x-yoox-device x-yoox-api-key x-yoox-cart-token".split(" ") - fields.forEach(function(field){ - if (req.headers[field]) headers[field] = field + req.pause() + var connector = https.request(options, function(server_res) { + console.log(">> GOT", server_res.statusCode) + + server_res.pause() + res.writeHeader(server_res.statusCode, server_res.headers) + // server_res.on("data", function(s){ console.log("<<", s.toString()) }) + server_res.pipe(res) + server_res.resume() }) - return headers -} + // req.on("data", function(s){ console.log(">>", s.toString()) }) + req.on("error", function(s){ console.log("/!\\ ERROR /!\\"); console.log(s) }) + req.pipe(connector) + req.resume() -function respond(res){ - return function(data, status, xhr){ - res.writeHead(xhr.status, "OK", { 'Content-Type': 'application/json' }) - res.end(data) - console.log(xhr.status, data.length) - } -} +}) /* headers: { @@ -73,8 +53,76 @@ function respond(res){ "x-yoox-api-key": auth.apikey, "x-yoox-cart-token": cart.token, }, - - proxy content body - proxy method - proxy query string -*/
\ No newline at end of file +*/ + +function get_headers (h){ + var hh = {} + "appname account-token device api-key cart-token".split(" ").forEach(function(s){ + var key = "x-yoox-" + s + if (key in h) hh[key] = h[key] + }) + hh['Content-Type'] = "application/json" // h['content-type'] + if ('content-length' in h) hh['Content-Length'] = h['content-length'] + if ('connection' in h) hh['Connection'] = h['connection'] + return hh +} + +var mimes = { + "gif": "image/gif", + "png": "image/png", + "jpg": "image/jpeg", + "jpeg": "image/jpeg", + "html": "text/html", + "js": "application/javascript", + "css": "text/css", + "woff": "application/font-woff", + "ttf": "application/font-woff", +} + +function stream (req, res) { + var url = req.url.toLowerCase().split("?")[0] + var ext_partz = url.split("."), ext = ext_partz[ext_partz.length-1] + var mime = mimes[ext] || "application/octet-stream" + + if (! url || url == "/") { + url = "index.html" + mime = "text/html" + } + + var file = path.resolve("../StoneIsland/www/" + url) + + fs.stat(file, function(err, stats) { + if (! stats) { + // console.log(404) + res.writeHead(404) + res.end("404") + return + } + + var headers = { + "Content-Length": stats.size, + "Content-Type": mime, + } + + res.writeHead(200, "OK", headers) + + var stream = fs.createReadStream(file, { start: 0, end: stats.size }) + stream.on("open", function() { + stream.pipe(res) + }) + stream.on("error", function(err) { + console.log(err) + stream.destroy() + res.end() + }) + stream.on("close", function(){ + stream.destroy() + res.end() + }) + req.connection.setMaxListeners(50) + req.connection.on('close', function(){ + stream.destroy() + res.end() + }) + }) +} |
