覆盖Node.js HTTP解析器 [英] Overriding Node.js HTTP parser

查看:149
本文介绍了覆盖Node.js HTTP解析器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在普通的HTTP服务器上使用Node的基本 http.request()函数没有问题。我需要在SHOUTcast服务器上使用 http.request()(或类似)。 SHOUTcast协议与HTTP完全兼容,除了一个细节......第一个响应行。

I am using Node's basic http.request() function without problem on normal HTTP servers. I need to use http.request() (or similar) with SHOUTcast servers. The SHOUTcast "protocol" is fully compatible with HTTP, except for one detail... the first response line.

普通HTTP服务器响应:

HTTP/1.1 200 OK

SHOUTcast服务器回复:

ICY 200 OK

同样,协议的其余部分是相同的。唯一的区别是 HTTP / 1.x ICY

Again, the rest of the protocol is the same. The only difference is HTTP/1.x vs. ICY.

我想扩展,子类或以某种方式修改Node的 http.request()函数,以便我可以使它与SHOUTcast服务器一起使用。通过节点连接到SHOUTcast已经完成,但只能重新发明整个轮子。我宁愿不这样做,因为这是一个小的协议差异。

I would like to extend, subclass, or somehow modify Node's http.request() function so that I can make it work with SHOUTcast servers. Connecting to SHOUTcast with Node has been done before, but only by re-inventing the entire wheel. I'd rather not do that for what amounts to be a minor protocol difference.


  1. 扩展或覆盖 Node的HTTP解析器的相关部分。 (我怀疑这是可能的,因为似乎解析器是本机代码。 )

  1. Extend or override the relevant parts of Node's HTTP parser. (I doubt this is possible, as it seems the parser is native code.)

创建我自己的位代码来解析HTTP的相关部分,但重用HTTP的现有Node组件数量为可能。

Create my own bit of code that parses the relevant parts of HTTP, but reuses as many existing Node components for HTTP as possible.

创建一个简单的内部代理(或以某种方式中继数据),以便我可以修改之前的第一个服务器响应行它到达了Node的HTTP解析器。

Create a simple internal proxy (or somehow relay data) so that I can modify that first server response line prior to it reaching Node's HTTP parser.

还有什么?

我还考虑过使用 Shred ,但它没有提供选项流响应。 (它等待整个服务器响应在触发事件之前完成,这对于数据无限期运行的流服务器不起作用。)沿着同样的路线,我尝试了请求,但它使用Node自己的HTTP解析器,因此我得到的解析错误与本机HTTP客户端相同。

I have also considered using Shred, but it does not offer an option to stream the response. (It waits for the entire server response to be complete before firing an event, which won't work for streaming servers where the data can run indefinitely.) Along those same lines, I tried Request, but it uses Node's own HTTP parser, so I get the same parse errors as I would with the native HTTP client.

推荐答案

我想出了另一种方法,类似于内部代理,但没有额外的连接。似乎可以覆盖HTTP客户端的内部使用的套接字。完成后,在将数据传递到原始内部套接字 ondata 函数之前,很容易挂钩并修改数据。

I've come up with another way to do this, similar to the internal proxy, but without the extra connection. It seems that it is possible to override the internally used socket for the HTTP client. When this is done, it is easy to hook in and modify data before passing it off to the original internal socket ondata function.

var httpicy = new HttpIcyClient();
httpicy.request(/* your normal request parameters here */);



来源



Source

var http = require('http');

var HttpIcyClient = function () {};
HttpIcyClient.prototype.request = function (options, callback) {
    var req = http.request(options, callback),
        originalOnDataFunction,
        receiveBuffer = new Buffer(0);

    req.on('socket', function (socket) {
        originalOnDataFunction = socket.ondata;
        socket.ondata = function (d, start, end) {
            receiveBuffer = Buffer.concat([receiveBuffer, d.slice(start, end)]);
            if (receiveBuffer.length >= 4) {
                socket.ondata = originalOnDataFunction;
                if (receiveBuffer.toString('ascii', 0, 4) === 'ICY ') {
                    receiveBuffer = Buffer.concat([new Buffer('HTTP/1.0 ', 'ascii'), receiveBuffer.slice(4)]);
                }
                socket.ondata.apply(this, [receiveBuffer, 0, receiveBuffer.length]);
            }
        };
    });
    return req;
}

这篇关于覆盖Node.js HTTP解析器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆