2

パーサーに送信する前に node.js http ストリームを編集しようとしています。次のコードを実装しました

//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]);
      }
    }
  }
});

ここの回答から得たNode.js HTTPパーサーのオーバーライド

上記のコードは、約 95% の確率で機能するようです。ただし、リクエストをドロップし続け、タイムアウトになります。どのように、またはどこにドロップしているのかわかりません。誰でも助けることができます。

ありがとう、

マーク

on secureConnection が node.js https.createServer( 呼び出しから来ていることに注意してください

4

2 に答える 2

0

本当に欲しいですか

if (receiveBuffer.length >= 4)

最初のバッファが少なくとも最初の 4 バイトを含む部分的な MKCALENDAR である場合、上記のコードは MKCOL への変換を行いません。

于 2013-04-16T12:38:55.140 に答える