Stripping some proxy response headers

1,531 views
Skip to first unread message

Miki Tebeka

unread,
Sep 16, 2014, 2:23:15 AM9/16/14
to golan...@googlegroups.com
Greetings,

I'd like to strip some headers from the proxy target response. I've the below but the headers still show up.

func (fh *FilteringHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
    fh.handler.ServeHTTP(w, r)
    w.Header().Del("X-MIKI")
}


You can view the whole code at http://play.golang.org/p/2tokpPCZC-

Currently the only option I see to to provide my own http.ResponseWriter that has a custom Header function.

Any ideas?

Thanks,
--
Miki


Jesse McNelis

unread,
Sep 16, 2014, 2:44:18 AM9/16/14
to Miki Tebeka, golang-nuts
By the time the ResponseWriter has the header deleted it's already
been sent to the client.
You should make your own ResponseWriter that removes the header you
want to remove in it's Write() method (so the header is removed before
it's sent by the write call) and pass that down.

Miki Tebeka

unread,
Sep 17, 2014, 1:45:21 AM9/17/14
to golan...@googlegroups.com, miki....@gmail.com, jes...@jessta.id.au
Greetings,

>> I'd like to strip some headers from the proxy target response.
 
You should make your own ResponseWriter that removes the header you
want to remove in it's Write() method (so the header is removed before
it's sent by the write call) and pass that down.
Thanks Jesse, however from what I saw the HTTP headers are written by w.Header().WriteSubset (at least that's what I understand from server.go). Sadly it looks like I need to provide my own transport.

Any better ideas?

All the best,
--
Miki

James Bardin

unread,
Sep 17, 2014, 9:23:59 AM9/17/14
to golan...@googlegroups.com, miki....@gmail.com, jes...@jessta.id.au
What's wrong with proving a Transport? You don't need to implement the entire thing, just wrap RoundTrip (untested, but compiles):

type myTransport struct {
*http.Transport
stripHeaders []string
}

func (t *myTransport) RoundTrip(req *http.Request) (*http.Response, error) {
resp, err := t.Transport.RoundTrip(req)
if err != nil {
return nil, err
}

for _, hdr := range t.stripHeaders {
resp.Header.Del(hdr)
}
return resp, nil
}

Miki Tebeka

unread,
Sep 17, 2014, 1:28:42 PM9/17/14
to golan...@googlegroups.com, miki....@gmail.com, jes...@jessta.id.au
Greetings,

>> I'd like to strip some headers from the proxy target response.
> What's wrong with proving a Transport? You don't need to implement the entire thing, just wrap RoundTrip (untested, but compiles):

That worked, and as a bonus I get the Response which has the cookies parsed already.

 Thanks,
--
Miki

Henrik Johansson

unread,
Sep 17, 2014, 1:56:17 PM9/17/14
to Miki Tebeka, golang-nuts, Jesse McNelis

I just have to say again that these things are so nice about embedding in Go.  Once I got this I started to organize my stuff with this in mind constantly.

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Miki Tebeka

unread,
Sep 17, 2014, 1:59:57 PM9/17/14
to golan...@googlegroups.com, miki....@gmail.com, jes...@jessta.id.au
> What's wrong with proving a Transport? You don't need to implement the entire thing, just wrap RoundTrip (untested, but compiles):

That worked, and as a bonus I get the Response which has the cookies parsed already.

Hmm, for some reason resp.ContentLength is -1, any idea why? (resp.Header["Content-Length"] is empty). Though I can see that Apache returning this header.

James Bardin

unread,
Sep 17, 2014, 2:21:22 PM9/17/14
to Miki Tebeka, golan...@googlegroups.com, Jesse McNelis

On Wed, Sep 17, 2014 at 1:59 PM, Miki Tebeka <miki....@gmail.com> wrote:

Hmm, for some reason resp.ContentLength is -1, any idea why? (resp.Header["Content-Length"] is empty). Though I can see that Apache returning this header.


I it should be there if it was set coming in. Can you verify it was sent on the wire, or that Apache didn't go and use chunked encoding anyway? 

Is there a load-balancer between you and the apache server?

Tamás Gulácsi

unread,
Sep 17, 2014, 3:22:39 PM9/17/14
to golan...@googlegroups.com
It's -1 till set. net/http provides some buffering so for small body it will fill the length eventually, but for bigger responses it will use chunked encoding.

Miki Tebeka

unread,
Sep 18, 2014, 3:14:57 AM9/18/14
to golan...@googlegroups.com, miki....@gmail.com, jes...@jessta.id.au
Greetings,



Hmm, for some reason resp.ContentLength is -1, any idea why? (resp.Header["Content-Length"] is empty). Though I can see that Apache returning this header.


I it should be there if it was set coming in. Can you verify it was sent on the wire, or that Apache didn't go and use chunked encoding anyway? 
My bad, it was a 404 that I missed. Once I placed the right path I get a content length.
 
Is there a load-balancer between you and the apache server?
Nope.
Reply all
Reply to author
Forward
0 new messages