Why forbid http.ResponseWriter.WriteHeader after a Hijack?

1,867 views
Skip to first unread message

bmizerany

unread,
Aug 3, 2011, 10:31:45 PM8/3/11
to golang-nuts
I've run into a case where I don't know the header to write until
after I've hijacked a connection.

Observe: https://gist.github.com/1c6c841fee2f3c674848

I find the guard to be a limiting feature rather than liberating. May
I ask the motivation behind this?

Brad Fitzpatrick

unread,
Aug 3, 2011, 10:44:44 PM8/3/11
to bmizerany, golang-nuts
Calling Hijack is declaring that you're taking ownership of the TCP connection from the http library.

The http library wants to operate under conditions that are known, and that it setup.  Once we've given you the TCP connection, all bets are off.  Who knows what you've done to it.  The safest thing we can do (from a minimizing-confusion-and-misleading-bug-reports standpoint, especially) is to stay away.

If you want to write some headers, go for it.  An http.Response or http.Headers can write itself wherever you tell them to.

Brad Fitzpatrick

unread,
Aug 3, 2011, 10:51:15 PM8/3/11
to bmizerany, golang-nuts
On Wed, Aug 3, 2011 at 7:31 PM, bmizerany <blake.m...@gmail.com> wrote:

Observe:  https://gist.github.com/1c6c841fee2f3c674848

It looks like what you don't actually care about owning the TCP connection and ideally just want something like:

 var clientClosed chan bool
 cn, ok := w.(http.CloseNotifier)
 if ok {
     clientClosed = cn.CloseChannel() // returns a chan bool, written to when/if this http request is closed
 }

 select {
  case <-clientClosed:
     // cancel expensive stuff
  case ....
  }

?

bmizerany

unread,
Aug 3, 2011, 10:56:05 PM8/3/11
to golan...@googlegroups.com, bmizerany
Whoa. I didn't notice the CloseNotifier which is exactly what I was wanting.

Thank you.

-Blake

bmizerany

unread,
Aug 3, 2011, 11:02:17 PM8/3/11
to golan...@googlegroups.com, bmizerany
Damn.  You were not saying it exists.

Yes. That is something that would be very useful.

Bhaskar teja Yerneni

unread,
Mar 9, 2013, 2:21:56 PM3/9/13
to golan...@googlegroups.com, bmizerany
I have tried the CloseNotifier, but it doesnt send any response headers back to browser,
 which I need for setting Access-Control-Allow-Origin header for cross origin communication.

Any way we can set the header on response? I am using nginx as a proxy in front of my go App server 

Bhaskar teja Yerneni

unread,
Mar 9, 2013, 3:08:16 PM3/9/13
to golan...@googlegroups.com, bmizerany
Hi,

How can we write headers to hijacked connection since I want set  headers like Access-Control-Allow-Origin for my comet app?

Thanks,
Bhaskar

Robert

unread,
Mar 9, 2013, 3:12:48 PM3/9/13
to golan...@googlegroups.com
On Thursday, 4 August 2011 04:31:45 UTC+2, bmizerany wrote:
Observe:  https://gist.github.com/1c6c841fee2f3c674848
>  _, err := rwc.Read(make([]byte, 1))
If the connection isn't closed, why doesn't this read a single byte sometime later, maybe during handling of the next request on the same connection? I've seen a similar pattern somewhere and couldn't figure it out then, too.

Robert

Bhaskar teja Yerneni

unread,
Mar 11, 2013, 3:15:08 PM3/11/13
to golan...@googlegroups.com, bmizerany
Gary,

Thanks you, Is it important that a comet app needs to know when a client has closed the connection as in to check user's away status

On Sunday, March 10, 2013 4:48:13 AM UTC+5:30, gary b wrote:
On Saturday, March 9, 2013 12:08:16 PM UTC-8, Bhaskar teja Yerneni wrote:
How can we write headers to hijacked connection since I want set  headers like Access-Control-Allow-Origin for my comet app?

You don't need to hijack for comet, but if you do:

- write status line.
- construct header and write with http://godoc.org/net/http#Header.Write
- write blank line
- write body 

Reply all
Reply to author
Forward
Message has been deleted
0 new messages