No9 / harmon

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

Gzip support? #19

Closed DTrejo closed 9 years ago

DTrejo commented 9 years ago

Hi, I am using this for a site that returns gzip. I think that this was causing problems for Harmon. I have JUST realized that in order to solve this easily, we could modify the headers to show that we do not support gzip, and the server would respond appropriately. What do you think of a change like this?

I will be writing a snippet to accomplish this using http-proxy's proxyReq event, would you like a PR update to your README to teach others this strategy?

Cheers, D

PS Thanks for Harmon, it is pretty cool & useful :)

PPS Here is a change to host headers which allows another site to show at e.g. localhost:8888

The key line is

proxyReq.setHeader('host', dest.host)
var http = require('http')
var url = require('url')

var connect = require('connect')
var httpProxy = require('http-proxy')

var PORT = process.env.PORT || 8888

//
// properly configure the proxy to pretend that the request came from the
// original host.
//
var proxy = httpProxy.createProxyServer()
var dest = url.parse('http://dtrejo.com')
var proxyOptions = {
  target: dest.href
, hostRewrite: false // not super sure what this does.
}
proxy.on('proxyReq', function(proxyReq, req, res, options) {
  proxyReq.setHeader('host', dest.host)
  // console.log('proxyReq', proxyReq)
  // proxyReq.setHeader('X-Special-Proxy-Header', 'foobar')
})

// .... the rest of the examples/simple.js file ...
No9 commented 9 years ago

Thanks for the kind words. As always pull requests are greatfully recieved. Especially with additional documentation for realworld use cases :)

DTrejo commented 9 years ago

Yes I'll send over the two lines tomorrow.

Key info:

1) remove accept header before making request to remote site 2) set host header to the host of the site you are proxying to.

These things fixed my issues.

I wasnt able to use all of Harmon because I could not append HTML to an element using trumpet :(

But I still used your monkeypatching code and conditional execution. Very helpful.

I'll credit you when I publish the module, probably also tomorrow.

Cheers! David On Feb 11, 2015 2:33 PM, "Anton Whalley" notifications@github.com wrote:

Thanks for the kind words. As always pull requests are greatfully recieved. Especially with additional documentation for realworld use cases :)

— Reply to this email directly or view it on GitHub https://github.com/No9/harmon/issues/19#issuecomment-73979579.

DTrejo commented 9 years ago

For those reading this later, you can see what I came up with over here: https://github.com/DTrejo/neo-proxy

No9 commented 9 years ago

Hi @DTrejo Gzip support and a sample has been added to the repo https://github.com/No9/harmon/blob/master/examples/gzipped.js

DTrejo commented 9 years ago

Thanks man!

Also, check it out, I gave you credit :)

https://github.com/DTrejo/neo-proxy#credits

Have a great day man and thanks for saving me so much time & not having to write this myself :)

Best, D

No9 commented 9 years ago

:+1: thanks for the credit