-
Notifications
You must be signed in to change notification settings - Fork 26
/
writer.go
55 lines (47 loc) · 1.18 KB
/
writer.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
// Copyright 2016 Afshin Darian. All rights reserved.
// Use of this source code is governed by The MIT License
// that can be found in the LICENSE file.
package sleuth
import "net/http"
type whisperer interface {
Whisper(addr string, payload []byte) error
}
type writer struct {
http.ResponseWriter
group string
output *response
peer string
whisperer whisperer
}
func (w *writer) Header() http.Header {
return w.output.Header
}
func (w *writer) Write(data []byte) (int, error) {
if w.output.Code == 0 {
w.WriteHeader(http.StatusOK)
}
header := w.Header()
if header.Get("Content-Type") == "" {
header.Add("Content-Type", http.DetectContentType(data))
}
w.output.Body = data
payload := resMarshal(w.group, w.output)
if err := w.whisperer.Whisper(w.peer, payload); err != nil {
return 0, newError(errResWhisper, err.Error())
}
return len(data), nil
}
func (w *writer) WriteHeader(code int) {
w.output.Code = code
}
func newWriter(node whisperer, dest *destination) *writer {
return &writer{
group: dest.group,
output: &response{
Handle: dest.handle,
Header: http.Header(make(map[string][]string)),
},
peer: dest.node,
whisperer: node,
}
}