Writing a web server that supports chunked transfers

1,547 views
Skip to first unread message

Josh

unread,
Dec 5, 2010, 10:02:45 PM12/5/10
to golang-nuts
Hi -

I'm new to Go and trying to write a simple web server that supports
chunked transfers. I started with the sample web server from the http
package documentation, but I can't seem to get the Transfer-Encoding
header set to "chunked" no matter what I try.

Here is the code I have so far:

package main
import (
"http"
"log"
)

func MyServer(writer http.ResponseWriter, req *http.Request) {
writer.SetHeader("Transfer-Encoding", "chunked")
writeCloser := http.NewChunkedWriter(writer)
msg := []byte("Testing\n")
writeCloser.Write(msg)
writeCloser.Close()
}

func main() {
http.HandleFunc("/", MyServer)
error := http.ListenAndServe(":9999", nil)
if error != nil {
log.Exit("ListenAndServe: ", error.String())
}
}

When I hit http://localhost:9999, I see the output I would expect in
the body, but the Transfer-Encoding header is still set to "Identity",
not "chunked".

What's the right way to go about doing this? Thanks in advance for
any help.
Message has been deleted

Andrew Gerrand

unread,
Dec 5, 2010, 10:34:37 PM12/5/10
to Josh, golang-nuts
I could be wrong, but it looks like setting the Transfer-Encoding
header with SetHeader doesn't update the TransferEncoding element of
the underlying Response struct. This means that when the response is
being finally written, the Transfer-Encoding header is removed and
ignored. This might be an artefact of some refactoring that happened
in the http libraries a few months ago.

Please file an issue at the issue tracker:
http://code.google.com/p/go/issues/entry

Andrew

Josh

unread,
Dec 5, 2010, 11:27:16 PM12/5/10
to golang-nuts
Thanks for the feedback guys. Much appreciated. I've gone ahead and
filed an issue, per your suggestion Andrew.

I also tried out your suggestion Gary, but so far, no luck.

On Dec 5, 7:34 pm, Andrew Gerrand <a...@golang.org> wrote:
> I could be wrong, but it looks like setting the Transfer-Encoding
> header with SetHeader doesn't update the TransferEncoding element of
> the underlying Response struct. This means that when the response is
> being finally written, the Transfer-Encoding header is removed and
> ignored. This might be an artefact of some refactoring that happened
> in the http libraries a few months ago.
>
> Please file an issue at the issue tracker:
>  http://code.google.com/p/go/issues/entry
>
> Andrew
>
> On 6 December 2010 14:02, Josh <raskcha...@gmail.com> wrote:
>
>
>
> > Hi -
>
> > I'm new to Go and trying to write a simple web server that supports
> > chunked transfers.  I started with the sample web server from the http
> > package documentation, but I can't seem to get the Transfer-Encoding
> > header set to "chunked" no matter what I try.
>
> > Here is the code I have so far:
>
> > package main
> > import (
> >  "http"
> >  "log"
> > )
>
> > func MyServer(writer http.ResponseWriter, req *http.Request) {
> >  writer.SetHeader("Transfer-Encoding", "chunked")
> >  writeCloser := http.NewChunkedWriter(writer)
> >  msg := []byte("Testing\n")
> >  writeCloser.Write(msg)
> >  writeCloser.Close()
> > }
>
> > func main() {
> >  http.HandleFunc("/", MyServer)
> >  error := http.ListenAndServe(":9999", nil)
> >  if error != nil {
> >    log.Exit("ListenAndServe: ", error.String())
> >  }
> > }
>
> > When I hithttp://localhost:9999, I see the output I would expect in
Message has been deleted

Josh

unread,
Dec 6, 2010, 12:10:06 AM12/6/10
to golang-nuts
Curious....when I use curl, as you suggest, I also see Transfer-
Encoding set to "chunked". But, when I use the web inspector in
Safari, it shows Identity. It also shows Identity when I use
HTTPClient (http://ditchnet.org/httpclient/). Any idea on why it
would be different?

On Dec 5, 8:48 pm, gary b <gary.b...@gmail.com> wrote:
> The Go http server either sets the response transfer encoding header
> to "chunked" or it does not set the header.  If you are seeing the
> response transfer encoding header set to "identity", then I suspect
> that there's something between your client and the server.
>
> When I dump the headers from your  application using "curl -D -http://localhost:9999/", I see the transfer encoding header set to
> "chunked".
>
> How are you examining the output from the server?
Reply all
Reply to author
Forward
Message has been deleted
0 new messages