33 lines
601 B
Go
33 lines
601 B
Go
|
package httpconn
|
||
|
|
||
|
import (
|
||
|
"io"
|
||
|
"net"
|
||
|
"net/http"
|
||
|
"time"
|
||
|
)
|
||
|
|
||
|
func Accept(w http.ResponseWriter, r *http.Request) (net.Conn, error) {
|
||
|
if r.Method != "CONNECT" {
|
||
|
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
|
||
|
w.WriteHeader(http.StatusMethodNotAllowed)
|
||
|
io.WriteString(w, "405 must CONNECT\n")
|
||
|
return nil, http.ErrNotSupported
|
||
|
}
|
||
|
|
||
|
hj, ok := w.(http.Hijacker)
|
||
|
if !ok {
|
||
|
return nil, http.ErrNotSupported
|
||
|
}
|
||
|
|
||
|
conn, _, err := hj.Hijack()
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
|
||
|
_, _ = io.WriteString(conn, "HTTP/1.0 200 OK\n\n")
|
||
|
conn.SetDeadline(time.Time{})
|
||
|
|
||
|
return conn, nil
|
||
|
}
|