- 
      
- 
        Save seecinq/9201762 to your computer and use it in GitHub Desktop. 
  
    
      This file contains hidden or 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
    
  
  
    
  | var http = require('http'), | |
| url = require('url'), | |
| fs = require('fs'); | |
| var port = 9000; | |
| var httpServer = http.createServer(function(request, response) { | |
| var parsedUrl = url.parse(request.url); | |
| var requestData; | |
| var requestOpts = { | |
| headers: request.headers, | |
| hostname: request.headers.host, | |
| method: request.method, | |
| path: parsedUrl.path | |
| }; | |
| var mp3FileName = 'temp-mp3-file.mp3'; | |
| var mp3File = null; | |
| var proxy = http.request(requestOpts, function(res) { | |
| console.log(res); | |
| // Check to see if this is an mp3 file | |
| // TODO: verify its from rdio and not something random | |
| if (parsedUrl.path.indexOf('.mp3') !== -1) { | |
| console.log('FOUND MP3 FILE!!!'); | |
| // Prepare the stream | |
| mp3File = new Buffer(''); | |
| console.log('CREATED WRITE STREAM'); | |
| } | |
| res.on('data', function(chunk) { | |
| if (mp3File) { | |
| mp3File = Buffer.concat([mp3File, chunk]); | |
| } | |
| response.write(chunk, 'binary'); | |
| }); | |
| res.on('end', function() { | |
| if (mp3File) { | |
| console.log('FINISHED GETTING MP3 FILE'); | |
| fs.writeFile('./mp3/' + mp3FileName, mp3File, function(err) { | |
| if (err) { | |
| return console.log('Error writing file', err); | |
| } | |
| }); | |
| mp3File = null; | |
| } | |
| response.end(); | |
| }); | |
| res.on('error', function(err) { | |
| console.log('ERROR SOMEWHERE'); | |
| }); | |
| response.writeHead(res.statusCode, res.headers); | |
| }); | |
| request.on('data', function(chunk) { | |
| requestData = requestData + chunk; | |
| proxy.write(chunk, 'binary'); | |
| }); | |
| request.on('end', function() { | |
| proxy.end(); | |
| }); | |
| request.on('error', function(err) { | |
| console.log('Error'); | |
| }); | |
| }); | |
| httpServer.listen(port, function() { | |
| console.log('HTTP Proxy Server is listening on port: ' + port); | |
| }); | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment