1
0
mirror of https://github.com/golang/go synced 2024-11-21 10:34:40 -07:00

http: add Hijacker type; remove Hijack from ResponseWriter

The Hijack functionality wasn't removed, but now you have
to test if your ResponseWriter is also a Hijacker:

func ServeHTTP(rw http.ResponseWriter, req *http.Request) {
    if hj, ok := rw.(http.Hijacker); ok {
        hj.Hijack(..)
    }
}

R=rsc
CC=golang-dev
https://golang.org/cl/4245064
This commit is contained in:
Brad Fitzpatrick 2011-03-06 18:59:50 -08:00
parent 8b432848f4
commit 545a1eef6a
4 changed files with 6 additions and 12 deletions

View File

@ -6,17 +6,13 @@
package httptest package httptest
import ( import (
"bufio"
"bytes" "bytes"
"http" "http"
"io"
"os" "os"
) )
// ResponseRecorder is an implementation of http.ResponseWriter that // ResponseRecorder is an implementation of http.ResponseWriter that
// records its mutations for later inspection in tests. // records its mutations for later inspection in tests.
//
// Note that Hijack is not implemented and simply panics.
type ResponseRecorder struct { type ResponseRecorder struct {
Code int // the HTTP response code from WriteHeader Code int // the HTTP response code from WriteHeader
Header http.Header // if non-nil, the headers to populate Header http.Header // if non-nil, the headers to populate
@ -81,8 +77,3 @@ func (rw *ResponseRecorder) WriteHeader(code int) {
func (rw *ResponseRecorder) Flush() { func (rw *ResponseRecorder) Flush() {
rw.Flushed = true rw.Flushed = true
} }
// Hijack is not implemented in ResponseRecorder and instead panics.
func (rw *ResponseRecorder) Hijack() (io.ReadWriteCloser, *bufio.ReadWriter, os.Error) {
panic("Hijack not implemented in ResponseRecorder")
}

View File

@ -81,7 +81,10 @@ type ResponseWriter interface {
// Flush sends any buffered data to the client. // Flush sends any buffered data to the client.
Flush() Flush()
}
// A Hijacker is an HTTP request which be taken over by an HTTP handler.
type Hijacker interface {
// Hijack lets the caller take over the connection. // Hijack lets the caller take over the connection.
// After a call to Hijack(), the HTTP server library // After a call to Hijack(), the HTTP server library
// will not do anything else with the connection. // will not do anything else with the connection.

View File

@ -527,7 +527,7 @@ func (server *Server) ServeHTTP(w http.ResponseWriter, req *http.Request) {
io.WriteString(w, "405 must CONNECT\n") io.WriteString(w, "405 must CONNECT\n")
return return
} }
conn, _, err := w.Hijack() conn, _, err := w.(http.Hijacker).Hijack()
if err != nil { if err != nil {
log.Print("rpc hijacking ", w.RemoteAddr(), ": ", err.String()) log.Print("rpc hijacking ", w.RemoteAddr(), ": ", err.String())
return return

View File

@ -58,7 +58,7 @@ func getKeyNumber(s string) (r uint32) {
// ServeHTTP implements the http.Handler interface for a Web Socket // ServeHTTP implements the http.Handler interface for a Web Socket
func (f Handler) ServeHTTP(w http.ResponseWriter, req *http.Request) { func (f Handler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
rwc, buf, err := w.Hijack() rwc, buf, err := w.(http.Hijacker).Hijack()
if err != nil { if err != nil {
panic("Hijack failed: " + err.String()) panic("Hijack failed: " + err.String())
return return
@ -184,7 +184,7 @@ func (f Draft75Handler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
return return
} }
rwc, buf, err := w.Hijack() rwc, buf, err := w.(http.Hijacker).Hijack()
if err != nil { if err != nil {
panic("Hijack failed: " + err.String()) panic("Hijack failed: " + err.String())
return return