summaryrefslogtreecommitdiff
path: root/proxy/index.js
blob: 27d3156e62a636b2b458afa9acb8fd6147080f00 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
var http = require('http')
var https = require('https')
var path = require('path')
var fs = require('fs')
var url = require('url')

var PORT = 9090
var cache = {}

var DEBUG = false

var server = http.createServer().listen(PORT, function(){
  console.log('Proxy listening on: http://lvh.me:%s', PORT)
})

server.on('request', function (req, res){

  if (DEBUG) {
    console.log("\n\n___________________________")
  }
  
  if ( req.method == 'GET' && ! req.url.match(/API/) ) {
    // public...
    return stream(req, res)
  }

  console.log(req.method, req.url)
  
  if (req.method == 'GET' && req.url in cache) {
    res.writeHeader(200, { 'Content-type': 'application/json' })
    res.end(cache[req.url])
    return
  }

  // 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

  if (DEBUG) {
    console.log(options.headers)
    console.log(req.url)
  }

  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.pipe(res)
    
    cache[req.url] = ''
    server_res.on('data', function(s){
      cache[req.url] += s.toString()
    })

    if (DEBUG) {
      server_res.on('data', function(s){
        console.log( s.toString() )
      })
    }

    server_res.resume()
  })
  connector.on('error', function(e){
    console.error(e)
  })
  if (DEBUG) {
    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()

})

/*
  headers: {
    'x-yoox-appname': auth.appname,
    'x-yoox-account-token': auth.access_token,
    'x-yoox-device': auth.device,
    'x-yoox-api-key': auth.apikey,
    'x-yoox-cart-token': cart.token,
  },
*/

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-sfnt',
  'otf': 'application/font-sfnt',
  'svg': 'image/svg+xml',
}

function stream (req, res) {
  var url = req.url.split('?')[0]
  var ext_partz = url.toLowerCase().split('.'), ext = ext_partz[ext_partz.length-1]
  var mime = mimes[ext] || 'application/octet-stream'

  if (! url || url == '/') {
    url = 'index.html'
    mime = 'text/html'
  }
  if (url == '/cordova.js' || url == '/favicon.ico') {
    res.writeHead(200, 'OK', { 'Content-type': 'application/javascript' })
    res.end('{}')
    return
  }

  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()
    })
*/
  })
}