/
copy.go
187 lines (172 loc) · 4.23 KB
/
copy.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package spdy
import (
"errors"
"io"
"net"
"reflect"
"github.com/gopher-net/gopher-net/Godeps/_workspace/src/github.com/docker/libchan"
)
// copyMessage copies a key-value structure into a map, recursively
// copying any streams or channels using the provided sender.
func (c *channel) copyMessage(message interface{}) (interface{}, error) {
mapCopy, mapOk := message.(map[string]interface{})
if mapOk {
return c.copyChannelMessage(mapCopy)
}
return c.copyStructure(message)
}
func (c *channel) copyValue(v interface{}) (interface{}, error) {
switch val := v.(type) {
case *byteStream:
if val.session != c.session {
return c.copyByteStream(val)
}
case *channel:
if val.session != c.session {
if val.direction == inbound {
return c.copyReceiver(val)
}
return c.copySender(val)
}
case *channelWrapper:
if val.direction == inbound {
return c.copyReceiver(val)
}
return c.copySender(val)
case *net.TCPConn:
// Do nothing until socket support is added
case *net.UDPConn:
// Do nothing until socket support is added
case io.ReadWriteCloser:
return c.copyByteStream(val)
case io.ReadCloser:
return c.copyByteReadStream(val)
case io.WriteCloser:
return c.copyByteWriteStream(val)
case libchan.Sender:
return c.copySender(val)
case libchan.Receiver:
return c.copyReceiver(val)
case map[string]interface{}:
return c.copyChannelMessage(val)
case map[interface{}]interface{}:
return c.copyChannelInterfaceMessage(val)
default:
if rv := reflect.ValueOf(v); rv.Kind() == reflect.Ptr {
if rv.Elem().Kind() == reflect.Struct {
return c.copyStructValue(rv.Elem())
}
} else if rv.Kind() == reflect.Struct {
return c.copyStructValue(rv)
}
}
return v, nil
}
func (c *channel) copySender(val libchan.Sender) (libchan.Sender, error) {
recv, send, err := c.CreateNestedReceiver()
if err != nil {
return nil, err
}
go func() {
libchan.Copy(val, recv)
val.Close()
}()
return send, nil
}
func (c *channel) copyReceiver(val libchan.Receiver) (libchan.Receiver, error) {
send, recv, err := c.CreateNestedSender()
if err != nil {
return nil, err
}
go func() {
libchan.Copy(send, val)
send.Close()
}()
return recv, nil
}
func (c *channel) copyByteStream(stream io.ReadWriteCloser) (io.ReadWriteCloser, error) {
streamCopy, err := c.session.createByteStream()
if err != nil {
return nil, err
}
go func() {
io.Copy(streamCopy, stream)
streamCopy.Close()
}()
go func() {
io.Copy(stream, streamCopy)
stream.Close()
}()
return streamCopy, nil
}
func (c *channel) copyByteReadStream(stream io.ReadCloser) (io.ReadCloser, error) {
streamCopy, err := c.session.createByteStream()
if err != nil {
return nil, err
}
go func() {
io.Copy(streamCopy, stream)
streamCopy.Close()
stream.Close()
}()
return streamCopy, nil
}
func (c *channel) copyByteWriteStream(stream io.WriteCloser) (io.WriteCloser, error) {
streamCopy, err := c.session.createByteStream()
if err != nil {
return nil, err
}
go func() {
io.Copy(stream, streamCopy)
stream.Close()
}()
return streamCopy, nil
}
func (c *channel) copyChannelMessage(m map[string]interface{}) (interface{}, error) {
mCopy := make(map[string]interface{})
for k, v := range m {
vCopy, vErr := c.copyValue(v)
if vErr != nil {
return nil, vErr
}
mCopy[k] = vCopy
}
return mCopy, nil
}
func (c *channel) copyChannelInterfaceMessage(m map[interface{}]interface{}) (interface{}, error) {
mCopy := make(map[string]interface{})
for k, v := range m {
vCopy, vErr := c.copyValue(v)
if vErr != nil {
return nil, vErr
}
keyStr, ok := k.(string)
if !ok {
return nil, errors.New("invalid non string key")
}
mCopy[keyStr] = vCopy
}
return mCopy, nil
}
func (c *channel) copyStructure(m interface{}) (interface{}, error) {
v := reflect.ValueOf(m)
if v.Kind() == reflect.Ptr {
v = v.Elem()
}
if v.Kind() != reflect.Struct {
return nil, errors.New("invalid non struct type")
}
return c.copyStructValue(v)
}
func (c *channel) copyStructValue(v reflect.Value) (interface{}, error) {
mCopy := make(map[string]interface{})
t := v.Type()
for i := 0; i < v.NumField(); i++ {
vCopy, vErr := c.copyValue(v.Field(i).Interface())
if vErr != nil {
return nil, vErr
}
mCopy[t.Field(i).Name] = vCopy
}
return mCopy, nil
}