-
Notifications
You must be signed in to change notification settings - Fork 13
/
http.go
57 lines (51 loc) · 1.14 KB
/
http.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package spdy
import (
"net/http"
"fmt"
"log"
)
type ResponseWriter struct {
*Stream
headers *http.Header
sentHeaders bool
}
func (w *ResponseWriter) Header() http.Header {
if w.headers == nil {
headers := make(http.Header)
w.headers = &headers
}
return *w.headers
}
func (w *ResponseWriter) Write(data []byte) (int, error) {
if !w.sentHeaders {
w.WriteHeader(http.StatusOK)
}
debug("Sending %v\n", data)
err := w.WriteDataFrame(data, false)
if err != nil {
debug("error: %s", err)
return 0, err
}
return len(data), nil
}
func (w *ResponseWriter) WriteHeader(status int) {
fin := status == 0 // Status=0 will half-close the stream
debug("WriteHeader() header = %v\n", w.Header())
if w.output.Headers.Get("status") == "" {
w.Header().Set("status", fmt.Sprintf("%d", status))
}
if w.output.NFrames == 0 {
if w.local {
w.Syn(w.headers, fin)
} else {
w.Reply(w.headers, fin)
}
} else if w.headers != nil {
debug("Sending headers frame: %v\n", w.headers)
if err := w.WriteHeadersFrame(w.headers, fin); err != nil {
log.Printf("Error while writing headers frame: %s\n", err)
return
}
}
w.sentHeaders = true
}