Guest User

Untitled

a guest
Feb 1st, 2016
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var server = http.createServer(function(req, res) {
  2.     //1. Setting up a server, getting information for each request and listen to a port
  3.     //Internal processing
  4.     //Retrieving download piece of information
  5.     //Retrieving client piece of information
  6.     //declaring stream function
  7.     var stream = function(data){
  8.         var options = {
  9.           url: data.url,
  10.           pool: {maxSockets: Infinity},
  11.           localAddress: data.bind,
  12.           headers:{
  13.             'Cookie': data.parsed.cookie,
  14.             'User-Agent': data.parsed.agent,
  15.             'Connection': 'close'
  16.           },
  17.           timeout: 5000
  18.         }
  19.         try{
  20.           //Streams
  21.           var remote = request(options, function (error, response, body) {
  22.             if(error) {
  23.               console.log(error)
  24.             }
  25.           })
  26.           var stream = req.pipe(remote).pipe(res)
  27.           //Event handlers (on error/finish event)
  28.           remote.on('error',function(){
  29.             console.log([res],"Remote: error happened")
  30.           });
  31.           stream.on('error',function(){
  32.             console.log([res,stream],"Stream: error happened")
  33.           })
  34.           stream.on('finish',function(){
  35.             console.log([res,stream],"Stream: finished/ended")
  36.           })
  37.         } catch (error) {
  38.           console.log([res],"Remote/Stream: "+error)
  39.         }
  40.     }
  41.     //Parsing url and parameters
  42.     //2. Getting remote info
  43.     //Using exec to call a php file and get remote info
  44.     //3. Serve the content
  45.     stream({url: downloadInfos.u,bind: downloadInfos.bind, parsed: parsed},req,res)
  46.   })
Add Comment
Please, Sign In to add comment