No9 / harmon

middleware for node-http-proxy to modify the remote website response with trumpet
Other
424 stars 62 forks source link

Do you filter if the response was actually html? #10

Closed AoDev closed 10 years ago

AoDev commented 10 years ago

Hello,

I was trying harmon and the example works very well. I'm pretty new to this but I then tried it with a real server and I get a oob exception from trumpet (Out of buffer). I am not sure, but I think that it's because harmon does not check if the response is actually html and will run trumpet on any proxied file. But I am very new to this so maybe it's not that.

It probably should check for Content-Type: text/html in headers or simply can you tell me where I'm wrong.

Here is the exception:

/Users/AoDev/git-repos/ssp-proxy/node_modules/harmon/node_modules/trumpet/node_modules/buffers/index.js:169
    if (i < 0 || i >= this.length) throw new Error('oob');
                                   ^
Error: oob
    at Buffers.pos (/Users/AoDev/git-repos/ssp-proxy/node_modules/harmon/node_modules/trumpet/node_modules/buffers/index.js:169:42)
    at Buffers.get (/Users/AoDev/git-repos/ssp-proxy/node_modules/harmon/node_modules/trumpet/node_modules/buffers/index.js:183:20)
    at makeFn (/Users/AoDev/git-repos/ssp-proxy/node_modules/harmon/node_modules/trumpet/lib/tokenize.js:57:26)
    at SAXStream.parser.(anonymous function) (/Users/AoDev/git-repos/ssp-proxy/node_modules/harmon/node_modules/trumpet/lib/tokenize.js:26:50)
    at SAXStream.EventEmitter.emit (events.js:95:17)
    at Object.me._parser.(anonymous function) [as onopentag] (/Users/AoDev/git-repos/ssp-proxy/node_modules/harmon/node_modules/trumpet/node_modules/sax/lib/sax.js:245:15)
    at emit (/Users/AoDev/git-repos/ssp-proxy/node_modules/harmon/node_modules/trumpet/node_modules/sax/lib/sax.js:615:33)
    at emitNode (/Users/AoDev/git-repos/ssp-proxy/node_modules/harmon/node_modules/trumpet/node_modules/sax/lib/sax.js:620:3)
    at openTag (/Users/AoDev/git-repos/ssp-proxy/node_modules/harmon/node_modules/trumpet/node_modules/sax/lib/sax.js:801:3)
    at Object.write (/Users/AoDev/git-repos/ssp-proxy/node_modules/harmon/node_modules/trumpet/node_modules/sax/lib/sax.js:1206:11)
    at SAXStream.write (/Users/AoDev/git-repos/ssp-proxy/node_modules/harmon/node_modules/trumpet/node_modules/sax/lib/sax.js:227:16)
No9 commented 10 years ago

Hi @AoDev You are correct the mime-type isn't checked. If you can provide the HTML file/site you are trying to proxy and the selector code you are using we can have a closer look. Thanks

AoDev commented 10 years ago

Hello,

Here is the whole story:

What I want to do is to inject some code to the body of a webpage, for development purpose. Because it's the only way we found to test a feature in mobile browsers. So we want to intercept the response and inject our development code.

To test Harmon, let's take this site: http://kevinpurnelle.com/ (it's mine) And for this page for example: http://kevinpurnelle.com/jobs/yuhubox/

Replace the element with the 'date' class.

simpleselect.query = '.date';
simpleselect.func = function (node) {
    node.createWriteStream().end('<div>Proxied</div>');
}

Just now, I saw I got a different error. The buffer error was with a local version of the same site. With this online version I get:

events.js:72
        throw er; // Unhandled 'error' event
              ^
Error: Unexpected end
Line: 3
Column: 117
Char:
    at error (/Users/AoDev/git-repos/ssp-proxy/node_modules/harmon/node_modules/trumpet/node_modules/sax/lib/sax.js:642:8)
    at end (/Users/AoDev/git-repos/ssp-proxy/node_modules/harmon/node_modules/trumpet/node_modules/sax/lib/sax.js:650:64)
    at Object.SAXParser.end (/Users/AoDev/git-repos/ssp-proxy/node_modules/harmon/node_modules/trumpet/node_modules/sax/lib/sax.js:149:24)
    at SAXStream.end (/Users/AoDev/git-repos/ssp-proxy/node_modules/harmon/node_modules/trumpet/node_modules/sax/lib/sax.js:234:16)
    at Stream.<anonymous> (/Users/AoDev/git-repos/ssp-proxy/node_modules/harmon/node_modules/trumpet/lib/tokenize.js:20:30)
    at _end (/Users/AoDev/git-repos/ssp-proxy/node_modules/harmon/node_modules/trumpet/node_modules/through/index.js:65:9)
    at Stream.stream.end (/Users/AoDev/git-repos/ssp-proxy/node_modules/harmon/node_modules/trumpet/node_modules/through/index.js:74:5)
    at Stream.<anonymous> (/Users/AoDev/git-repos/ssp-proxy/node_modules/harmon/node_modules/trumpet/index.js:22:30)
    at _end (/Users/AoDev/git-repos/ssp-proxy/node_modules/harmon/node_modules/trumpet/node_modules/through/index.js:65:9)
    at Stream.stream.end (/Users/AoDev/git-repos/ssp-proxy/node_modules/harmon/node_modules/trumpet/node_modules/through/index.js:74:5)
No9 commented 10 years ago

So I replicated the error and the response doesn't seem to be getting past

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <title>Kevin Purnelle Personal website</title>
  <meta name="viewport" content="width=device-width">

  <!-- Custom CSS -->
  <link rel="stylesheet"

I can't seem to replicate this on any other site. Can you give some indication to the type of server that is serving this content?

Thanks

No9 commented 10 years ago

And when I use the content from my browser directly into the default HTTP server given in the sample I can't replicate the issue.

No9 commented 10 years ago

So I have added the option to ignore CSS and JS files that should help with general site work. It inspects the file extension as mime-type was proving flaky Reference implementation here https://github.com/No9/harmon/blob/master/examples/doge.js Closing this