How can i "intercept the http.Handle's response body"

2,547 views
Skip to first unread message

毛剑

unread,
Aug 20, 2013, 6:15:59 AM8/20/13
to golan...@googlegroups.com
Hi all:

  I noticed the package in "net/http" handler func(ResponseWriter, *Request) don't have any method to expose the ResponseWriter's writen body bytes.

in my opion, if ResponseWriter have a way expose the Writen Body , i can immplement like Java AOP cache policy

we use MakeHandler, after a user handler called(sth like wrote json in the body), then the MakeHandler do these things:

1. check key in the cache
2. if not call the http.Handler else return the cache result
3. after called http.Handler, we can get resp body from ResponseWriter then cache the body(like json)

the ResponseWriter use      w  *bufio.Writer // buffers output in chunks to chunkWriter  ...i have no idea to get the Writen Body.. 

Volker Dobler

unread,
Aug 20, 2013, 7:27:53 AM8/20/13
to golan...@googlegroups.com
Am Dienstag, 20. August 2013 12:15:59 UTC+2 schrieb Terry.Mao:
Hi all:

  I noticed the package in "net/http" handler func(ResponseWriter, *Request) don't have any method to expose the ResponseWriter's writen body bytes.
 
Yes, because there might be bytes already on the wire while you are still writing.
 
in my opion, if ResponseWriter have a way expose the Writen Body , i can immplement like Java AOP cache policy

we use MakeHandler, after a user handler called(sth like wrote json in the body), then the MakeHandler do these things:

1. check key in the cache
2. if not call the http.Handler else return the cache result
3. after called http.Handler, we can get resp body from ResponseWriter then cache the body(like json)

the ResponseWriter use      w  *bufio.Writer // buffers output in chunks to chunkWriter  ...i have no idea to get the Writen Body.. 

That is the reason why http.ResponseWriter is an interface type.
Just implement your own CachableResponseWriter which satisfies
http.ResponseWriter and use this.

V.

毛剑

unread,
Aug 20, 2013, 8:06:10 AM8/20/13
to Volker Dobler, golan...@googlegroups.com
thks Volker Dobler!
but need modify many things after all....
i review the code ,found is a issue 5100
 Issue 5100: bufio: no way to give/take underlying buffers


2013/8/20 Volker Dobler <dr.volke...@gmail.com>

--
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/groups/opt_out.

Jesse McNelis

unread,
Aug 20, 2013, 8:13:16 AM8/20/13
to 毛剑, golang-nuts, Volker Dobler


On 20/08/2013 10:06 PM, "毛剑" <mao...@conew.com> wrote:
>
> thks Volker Dobler!
> but need modify many things after all....

You just need a bytes.Buffer.

毛剑

unread,
Aug 20, 2013, 9:22:50 AM8/20/13
to Jesse McNelis, golang-nuts, Volker Dobler
but it's hard to reuse the bytes.Buffer, as the code wrote, should use chan to send the bufio.Writer
and need a copy of bufio.Writer's inner buf,it's a time-costed operation

but dosen't have any good way to fix this.
i'll keep a eye on the issue 5100.


2013/8/20 Jesse McNelis <jes...@jessta.id.au>

Volker Dobler

unread,
Aug 20, 2013, 9:30:28 AM8/20/13
to golan...@googlegroups.com, Jesse McNelis, Volker Dobler
Am Dienstag, 20. August 2013 15:22:50 UTC+2 schrieb Terry.Mao:
but it's hard to reuse the bytes.Buffer, as the code wrote, should use chan to send the bufio.Writer
and need a copy of bufio.Writer's inner buf,it's a time-costed operation

but dosen't have any good way to fix this.
i'll keep a eye on the issue 5100.

Don't do premature optimisations. Write your code, measure and fix where appropriate.

V. 

buzzlight

unread,
Aug 22, 2013, 10:27:55 PM8/22/13
to golan...@googlegroups.com
Just write a YourResponseWriter to wrap GO's ResponseWriter.

a demo (from gwk)
type compressResponseWriter struct {
rw http.ResponseWriter
writer compresser
contentType string
format string
headerWritten bool
}

func (crw *compressResponseWriter) Header() http.Header {
return crw.rw.Header()
}

func (crw *compressResponseWriter) WriteHeader(status int) {
crw.rw.WriteHeader(status)
}

func (crw *compressResponseWriter) Write(p []byte) (int, error) {
if !crw.headerWritten {
if crw.rw.Header().Get(HeaderContentType) == "" && crw.contentType != "" {
crw.rw.Header().Set(HeaderContentType, crw.contentType)
}
crw.headerWritten = true
}
n, err := crw.writer.Write(p)
err = crw.writer.Flush()
return n, err
}


在 2013年8月20日星期二UTC+8下午6时15分59秒,Terry.Mao写道:
Reply all
Reply to author
Forward
0 new messages