FAQ
I'm new to Go, so issue might actually not be an issue; I'm writing a small
web framework in which I need to pass a Response struct through a pipeline
of functions that must
be able to modify the headers and body; and at the end of the pipeline, the
response should be sent.
As I understand, the ResponseWriter passed to the http handlers write the
data to the underlying connection as soon as you write to the body, so the
body cannot be mutated
be the pipeline stages executed after the first write.
Am I right ? And if so, what could I do to meet my requirements ??

Thx in advance for your help.

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

  • Peter Bourgon at Jun 27, 2013 at 4:09 pm
    Combine the Request, the ResponseWriter, and all the state you need in
    a struct. Pass the struct through your pipeline. In the final stage of
    the pipeline, perform the write.
    On Thu, Jun 27, 2013 at 12:46 PM, wrote:
    I'm new to Go, so issue might actually not be an issue; I'm writing a small
    web framework in which I need to pass a Response struct through a pipeline
    of functions that must
    be able to modify the headers and body; and at the end of the pipeline, the
    response should be sent.
    As I understand, the ResponseWriter passed to the http handlers write the
    data to the underlying connection as soon as you write to the body, so the
    body cannot be mutated
    be the pipeline stages executed after the first write.
    Am I right ? And if so, what could I do to meet my requirements ??

    Thx in advance for your help.

    --
    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.
  • Briche Arnaud at Jun 27, 2013 at 4:21 pm
    Ok, this seems like a good option; but then should I use the Response type
    from net/http
    to act like the mutable response I need, and then write it in the end ?


    Le jeudi 27 juin 2013 18:09:38 UTC+2, Peter Bourgon a écrit :
    Combine the Request, the ResponseWriter, and all the state you need in
    a struct. Pass the struct through your pipeline. In the final stage of
    the pipeline, perform the write.

    On Thu, Jun 27, 2013 at 12:46 PM, <briche...@gmail.com <javascript:>>
    wrote:
    I'm new to Go, so issue might actually not be an issue; I'm writing a small
    web framework in which I need to pass a Response struct through a pipeline
    of functions that must
    be able to modify the headers and body; and at the end of the pipeline, the
    response should be sent.
    As I understand, the ResponseWriter passed to the http handlers write the
    data to the underlying connection as soon as you write to the body, so the
    body cannot be mutated
    be the pipeline stages executed after the first write.
    Am I right ? And if so, what could I do to meet my requirements ??

    Thx in advance for your help.

    --
    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 <javascript:>.
    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.
  • Peter Bourgon at Jun 27, 2013 at 4:36 pm

    On Thu, Jun 27, 2013 at 6:21 PM, wrote:
    Ok, this seems like a good option; but then should I use the Response type
    from net/http
    to act like the mutable response I need, and then write it in the end ?
    http.Response "represents the response from an HTTP request" -- you
    don't write it, you receive it when you make a http.Request.

    Your response to a client request is through the http.ResponseWriter,
    which only allows you to change the response header map and write
    bytes.

    So, your context struct might look like:

       type Context struct {
           Request *http.Request
           ResponseWriter http.ResponseWriter
           StatusCode int
           ResponseBody []byte
           ExtraHeaders map[string][]string
       }

    And your WriteStage might look like:

       func WriteStage(c *Context) {
           if c.StatusCode != http.StatusOK {
               http.Error(c.ResponseWriter, "oh no!", c.StatusCode)
               return
           }

           header := c.ResponseWriter.Header()
           for k, v := range c.ExtraHeaders {
               header[k] = append(header[k], v...)
           }
           if _, err := c.ResponseWriter.Write(c.ResponseBody); err != nil {
               log.Printf("WriteStage: %s", err)
           }
       }

    Le jeudi 27 juin 2013 18:09:38 UTC+2, Peter Bourgon a écrit :
    Combine the Request, the ResponseWriter, and all the state you need in
    a struct. Pass the struct through your pipeline. In the final stage of
    the pipeline, perform the write.
    On Thu, Jun 27, 2013 at 12:46 PM, wrote:
    I'm new to Go, so issue might actually not be an issue; I'm writing a
    small
    web framework in which I need to pass a Response struct through a
    pipeline
    of functions that must
    be able to modify the headers and body; and at the end of the pipeline,
    the
    response should be sent.
    As I understand, the ResponseWriter passed to the http handlers write
    the
    data to the underlying connection as soon as you write to the body, so
    the
    body cannot be mutated
    be the pipeline stages executed after the first write.
    Am I right ? And if so, what could I do to meet my requirements ??

    Thx in advance for your help.

    --
    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.
    --
    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
postedJun 27, '13 at 2:32p
activeJun 27, '13 at 4:36p
posts4
users2
websitegolang.org

2 users in discussion

Peter Bourgon: 2 posts Briche Arnaud: 2 posts

People

Translate

site design / logo © 2021 Grokbase