2013-04-12 50 views
1

我想在將它發送到解析器之前編輯node.js http流。我已經實現了下面的代碼在ondata事件中更改node.js流

//catch any connection event to this server 
server.on('secureConnection', function (stream) { 
    //create a new buffer to hold what we are receiving in this stream 
    var receiveBuffer = new Buffer(0); 
    //store a link to the original ondata function so we can call it and restore it 
    originalOnDataFunction = stream.ondata; 
    //declare a new ondata function for this stream 
    stream.ondata = function (d, start, end) { 
    //record what we have received 
    receiveBuffer = Buffer.concat([receiveBuffer, d.slice(start, end)]); 
    //if what we have received is greater than 4 (i.e. we have at least got a GET request) 
    //then make changes 
    if (receiveBuffer.length >= 4) { 
     //reset the streams ondata function to the original 
     //this is all we want to edit for this connection 
     stream.ondata = originalOnDataFunction; 
     //if the first 11 characters of the buffer are 'MKCALENDAR ' then make a change 
     if (receiveBuffer.toString('ascii', 0, 11) === 'MKCALENDAR ') { 
     //I change this to MKCOL /MKCALENDAR<rest of buffer> as this will work with the node.js http parser 
     //and then I can check on the other side for a MKCOL method with /MKCALENDAR as the start of the url and 
     //know that it was a MKCALENDAR method 
     var rewrittenBuffer = Buffer.concat([new Buffer('MKCOL /MKCALENDAR', 'ascii'), receiveBuffer.slice(11)]); 
     //now call the original ondata function with this new buffer 
     stream.ondata.apply(this, [rewrittenBuffer, 0, rewrittenBuffer.length]); 
     } else { 
     //no change needed just call the original ondata function with this buffer 
     stream.ondata.apply(this, [receiveBuffer, 0, receiveBuffer.length]); 
     } 
    } 
    } 
}); 

這是我從一個答案來到這裏Overriding Node.js HTTP parser

上面的代碼似乎工作的時間約爲95%。但是,它會一直放下請求,然後超時。我看不出如何或在哪裏放棄它們。任何人都可以幫忙

感謝,

馬克

註上secureConnection從node.js的https.createServer未來(稱之爲

回答

0

喬納森的第一包似乎是所有的標題,所以這條線正常工作。我確實得到它的工作,並且問題似乎與.apply聲明不總是工作。以下工作正常:

//catch any secure connection event to this server 
server.on('secureConnection', function (stream) { 
    //create a new buffer to hold what we are receiving in this stream 
    var receiveBuffer = new Buffer(0); 
    //store a link to the original ondata function so we can call it and restore it 
    stream._ondataOld = stream.ondata; 
    stream.ondata = function(d,start,end){ 
    receiveBuffer = Buffer.concat([receiveBuffer, d.slice(start, end)]); 
    //if what we have received is greater than 4 (i.e. we have at least got a GET request) 
    //then make changes 
    if (receiveBuffer.length >= 4) { 
     //reset the streams ondata function to the original 
     //this is all we want to edit for this connection 
     stream.ondata = stream._ondataOld; 
     //if the first 11 characters of the buffer are 'MKCALENDAR ' then make a change 
     if (receiveBuffer.toString('ascii', 0, 11) === 'MKCALENDAR ') { 
     //I change this to MKCOL /MKCALENDAR<rest of buffer> as this will work with the node.js http parser 
     //and then I can check on the other side for a MKCOL method with /MKCALENDAR as the start of the url and 
     //know that it was a MKCALENDAR method 
     //this facilitates MKCALENDAR calls on the CALDAV server 
     var rewrittenBuffer = Buffer.concat([new Buffer('MKCOL /MKCALENDAR', 'ascii'), receiveBuffer.slice(11)]); 
     //console.log(rewrittenBuffer.toString('ascii',0,rewrittenBuffer.length)); 
     //console.log(rewrittenBuffer.toString('ascii')); 
     //now call the original ondata function with this new buffer 
     stream.ondata(rewrittenBuffer,0,rewrittenBuffer.length); 
     } else { 
     //no change needed just call the original ondata function with this buffer 
     stream.ondata(d,start,end); 
     } 
    } else { 
     stream.ondata(d,start,end); 
    } 
    } 
}); 
0

您確定要

if (receiveBuffer.length >= 4) 

如果第一緩衝區是至少有前四個字節的部分MKCALENDAR,上面的代碼不會翻譯成MKCOL。