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 policywe 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 cache2. if not call the http.Handler else return the cache result3. 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..
| Issue 5100: | bufio: no way to give/take underlying buffers |
--
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.
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.
but it's hard to reuse the bytes.Buffer, as the code wrote, should use chan to send the bufio.Writerand need a copy of bufio.Writer's inner buf,it's a time-costed operationbut dosen't have any good way to fix this.i'll keep a eye on the issue 5100.
type compressResponseWriter struct {rw http.ResponseWriterwriter compressercontentType stringformat stringheaderWritten 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}