forked from foomo/gotsrpc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransport.go
79 lines (65 loc) · 1.57 KB
/
transport.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package gotsrpc
import (
"net/http"
"github.com/ugorji/go/codec"
)
type ClientEncoding int
const (
EncodingMsgpack = ClientEncoding(0)
EncodingJson = ClientEncoding(1)
)
type clientHandle struct {
handle codec.Handle
contentType string
}
var msgpackClientHandle = &clientHandle{
handle: &codec.MsgpackHandle{},
contentType: "application/msgpack; charset=utf-8",
}
func init() {
mh := new(codec.MsgpackHandle)
msgpackClientHandle.handle = mh
// attempting to set promoted field in literal will cause a compiler error
mh.RawToString = true
}
var jsonClientHandle = &clientHandle{
handle: &codec.JsonHandle{
MapKeyAsString: true,
},
contentType: "application/json; charset=utf-8",
}
func getHandleForEncoding(encoding ClientEncoding) *clientHandle {
switch encoding {
case EncodingMsgpack:
return msgpackClientHandle
case EncodingJson:
return jsonClientHandle
default:
return jsonClientHandle
}
}
func getHandlerForContentType(contentType string) *clientHandle {
switch contentType {
case msgpackClientHandle.contentType:
return msgpackClientHandle
case jsonClientHandle.contentType:
return jsonClientHandle
default:
return jsonClientHandle
}
}
type responseWriterWithLength struct {
http.ResponseWriter
length int
}
func newResponseWriterWithLength(w http.ResponseWriter) *responseWriterWithLength {
return &responseWriterWithLength{w, 0}
}
func (w *responseWriterWithLength) Write(b []byte) (n int, err error) {
n, err = w.ResponseWriter.Write(b)
w.length += n
return
}
func (w *responseWriterWithLength) Length() int {
return w.length
}