FAQ
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..

--
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+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Search Discussions

  • Volker Dobler at Aug 20, 2013 at 11:27 am

    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.

    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • 毛剑 at Aug 20, 2013 at 12:06 pm
    thks Volker Dobler!
    but need modify many things after all....
    i review the code ,found is a issue 5100
      Issue 5100 <https://code.google.com/p/go/issues/detail?id=5100>:bufio: no
    way to give/take underlying buffers


    2013/8/20 Volker Dobler <dr.volker.dobler@gmail.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.

    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Jesse McNelis at Aug 20, 2013 at 12:13 pm

    On 20/08/2013 10:06 PM, "毛剑" wrote:
    thks Volker Dobler!
    but need modify many things after all....
    You just need a bytes.Buffer.
    i review the code ,found is a issue 5100
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • 毛剑 at Aug 20, 2013 at 1:22 pm
    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 <jessta@jessta.id.au>
    On 20/08/2013 10:06 PM, "毛剑" wrote:

    thks Volker Dobler!
    but need modify many things after all....
    You just need a bytes.Buffer.
    i review the code ,found is a issue 5100
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Volker Dobler at Aug 20, 2013 at 1:30 pm

    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.

    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Buzzlight at Aug 23, 2013 at 2:27 am
    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写道:
    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..
    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedAug 20, '13 at 10:16a
activeAug 23, '13 at 2:27a
posts7
users4
websitegolang.org

People

Translate

site design / logo © 2021 Grokbase