FAQ
Hi.

I'm working on an API server where one of the calls returns a large amount
of data streamed from disk and transformed in some way. I'm using chunked
transfer encoding to return data to the client because I don't way to keep
the entire response in memory. Hypothetically, if an errors occurs reading
from disk or something after I have already started sending the body, how
can I somehow indicate a fault to the client?

func (api *APIServer) ServeHTTP(resp http.ResponseWriter, req
*http.Request) {
     ... process request ...

     for data, err := getData(); err != io.EOF; {
         if err != nil {
             ... indicate response failure somehow ...
             break
         }
         resp.Write(data)
     }
}

With chunked encoding, one way to indicate a failure in this scenario to
avoid sending the final "0\r\n\r\n" so that the response is an invalid HTTP
response. It may not be a good way to indicate *what *error occurred, but
at least the client knows that an error *did* occur. How can I force the
ResponseWriter (which handles chunked encoding for you) to intentionally
send an invalid termination? Or what are some alternatives to report an
error in this situation.

Thanks,
JT

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

  • Brad Fitzpatrick at Jul 22, 2013 at 1:57 am
    You can hijack the connection (see net/http.Hijacker) and close the
    connection forcibly.

    A compliant HTTP/1.1 client will see that hard EOF without the required
    zero chunk as an unexpected EOF and should give an error to the application.


    On Sun, Jul 21, 2013 at 4:54 PM, wrote:

    Hi.

    I'm working on an API server where one of the calls returns a large amount
    of data streamed from disk and transformed in some way. I'm using chunked
    transfer encoding to return data to the client because I don't way to keep
    the entire response in memory. Hypothetically, if an errors occurs reading
    from disk or something after I have already started sending the body, how
    can I somehow indicate a fault to the client?

    func (api *APIServer) ServeHTTP(resp http.ResponseWriter, req
    *http.Request) {
    ... process request ...

    for data, err := getData(); err != io.EOF; {
    if err != nil {
    ... indicate response failure somehow ...
    break
    }
    resp.Write(data)
    }
    }

    With chunked encoding, one way to indicate a failure in this scenario to
    avoid sending the final "0\r\n\r\n" so that the response is an invalid
    HTTP response. It may not be a good way to indicate *what *error
    occurred, but at least the client knows that an error *did* occur. How
    can I force the ResponseWriter (which handles chunked encoding for you) to
    intentionally send an invalid termination? Or what are some alternatives to
    report an error in this situation.

    Thanks,
    JT

    --
    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.
  • Thebrokentoaster at Jul 22, 2013 at 2:31 am
    Thanks, this works.

    JT
    On Sunday, July 21, 2013 6:57:49 PM UTC-7, bradfitz wrote:

    You can hijack the connection (see net/http.Hijacker) and close the
    connection forcibly.

    A compliant HTTP/1.1 client will see that hard EOF without the required
    zero chunk as an unexpected EOF and should give an error to the application.



    On Sun, Jul 21, 2013 at 4:54 PM, <thebroke...@gmail.com <javascript:>>wrote:
    Hi.

    I'm working on an API server where one of the calls returns a large
    amount of data streamed from disk and transformed in some way. I'm using
    chunked transfer encoding to return data to the client because I don't way
    to keep the entire response in memory. Hypothetically, if an errors occurs
    reading from disk or something after I have already started sending the
    body, how can I somehow indicate a fault to the client?

    func (api *APIServer) ServeHTTP(resp http.ResponseWriter, req
    *http.Request) {
    ... process request ...

    for data, err := getData(); err != io.EOF; {
    if err != nil {
    ... indicate response failure somehow ...
    break
    }
    resp.Write(data)
    }
    }

    With chunked encoding, one way to indicate a failure in this scenario to
    avoid sending the final "0\r\n\r\n" so that the response is an invalid
    HTTP response. It may not be a good way to indicate *what *error
    occurred, but at least the client knows that an error *did* occur. How
    can I force the ResponseWriter (which handles chunked encoding for you) to
    intentionally send an invalid termination? Or what are some alternatives to
    report an error in this situation.

    Thanks,
    JT

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

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedJul 21, '13 at 11:54p
activeJul 22, '13 at 2:31a
posts3
users2
websitegolang.org

People

Translate

site design / logo © 2021 Grokbase