forked from qustavo/httplab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
response.go
334 lines (281 loc) · 6 KB
/
response.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
package httplab
import (
"bytes"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"sort"
"strconv"
"strings"
"time"
)
// BodyMode represent the current Body mode
type BodyMode uint
func (m BodyMode) String() string {
switch m {
case BodyInput:
return "Input"
case BodyFile:
return "File"
}
return ""
}
const (
BodyInput BodyMode = iota + 1
BodyFile
)
type Body struct {
Mode BodyMode
Input []byte
File *os.File
}
func (body *Body) Payload() []byte {
switch body.Mode {
case BodyInput:
return body.Input
case BodyFile:
if body.File == nil {
return nil
}
// XXX: Handle this error
bytes, _ := ioutil.ReadAll(body.File)
body.File.Seek(0, 0)
return bytes
}
return nil
}
func (body *Body) Info() []byte {
switch body.Mode {
case BodyInput:
return body.Input
case BodyFile:
if body.File == nil {
return nil
}
// XXX: Handle this error
stats, _ := body.File.Stat()
w := &bytes.Buffer{}
fmt.Fprintf(w, "file: %s\n", body.File.Name())
fmt.Fprintf(w, "size: %d bytes\n", stats.Size())
fmt.Fprintf(w, "perm: %s\n", stats.Mode())
return w.Bytes()
}
return nil
}
func (body *Body) SetFile(path string) error {
file, err := os.Open(ExpandPath(path))
if err != nil {
return err
}
body.File = file
body.Mode = BodyFile
return nil
}
type Response struct {
Status int
Headers http.Header
Body Body
Delay time.Duration
}
func (r *Response) UnmarshalJSON(data []byte) error {
type alias Response
v := struct {
alias
Body string
File string
Headers map[string]string
}{}
if err := json.Unmarshal(data, &v); err != nil {
return err
}
r.Status = v.Status
r.Delay = v.Delay
r.Body.Input = []byte(v.Body)
if v.File != "" {
if err := r.Body.SetFile(v.File); err != nil {
return err
}
}
if r.Body.File != nil {
r.Body.Mode = BodyFile
} else {
r.Body.Mode = BodyInput
}
if r.Headers == nil {
r.Headers = http.Header{}
}
for key := range v.Headers {
r.Headers.Set(key, v.Headers[key])
}
return nil
}
func (r *Response) MarshalJSON() ([]byte, error) {
type alias Response
v := struct {
alias
Body string
File string
Headers map[string]string
}{
Headers: make(map[string]string),
}
v.Delay = time.Duration(r.Delay) / time.Millisecond
v.Status = r.Status
if len(r.Body.Input) > 0 {
v.Body = string(r.Body.Input)
}
if r.Body.File != nil {
v.File = r.Body.File.Name()
}
for key := range r.Headers {
v.Headers[key] = r.Headers.Get(key)
}
return json.MarshalIndent(v, "", " ")
}
func NewResponse(status, headers, body string) (*Response, error) {
// Parse Status
status = strings.Trim(status, " \r\n")
if status == "" {
status = "200"
}
code, err := strconv.Atoi(status)
if err != nil {
return nil, fmt.Errorf("Status: %v", err)
}
if code < 100 || code > 599 {
return nil, fmt.Errorf("Status should be between 100 and 599")
}
// Parse Headers
hdr := http.Header{}
lines := strings.Split(headers, "\n")
for _, line := range lines {
if line == "" {
continue
}
kv := strings.SplitN(line, ":", 2)
if len(kv) != 2 {
continue
}
key := strings.TrimSpace(kv[0])
val := strings.TrimSpace(kv[1])
hdr.Set(key, val)
}
return &Response{
Status: code,
Headers: hdr,
Body: Body{
Input: []byte(body),
},
}, nil
}
func (r *Response) Write(w http.ResponseWriter) error {
for key := range r.Headers {
w.Header().Set(key, r.Headers.Get(key))
}
w.WriteHeader(r.Status)
_, err := w.Write(r.Body.Payload())
return err
}
type ResponsesList struct {
List map[string]*Response
keys []string
current int
}
func NewResponsesList() *ResponsesList {
return (&ResponsesList{}).reset()
}
func (rl *ResponsesList) reset() *ResponsesList {
rl.current = 0
rl.List = make(map[string]*Response)
rl.keys = nil
return rl
}
func (rl *ResponsesList) load(path string) (map[string]*Response, error) {
f, err := openConfigFile(path)
if err != nil {
return nil, err
}
rs := struct {
Responses map[string]*Response
}{}
if err := json.NewDecoder(f).Decode(&rs); err != nil {
if err == io.EOF {
return nil, nil
}
return nil, err
}
return rs.Responses, nil
}
func (rl *ResponsesList) Load(path string) error {
rs, err := rl.load(path)
if err != nil {
return err
}
rl.reset()
if rs != nil {
rl.List = rs
}
for key, _ := range rs {
rl.keys = append(rl.keys, key)
}
sort.Strings(rl.keys)
return nil
}
func (rl *ResponsesList) Save(path string) error {
f, err := openConfigFile(path)
if err != nil {
return err
}
defer f.Close()
stat, err := f.Stat()
if err != nil {
return err
}
if err := f.Truncate(stat.Size()); err != nil {
return err
}
buf, err := json.MarshalIndent(struct {
Responses map[string]*Response
}{rl.List}, "", " ")
if err != nil {
return err
}
if _, err := f.Write(buf); err != nil {
return err
}
return nil
}
func (rl *ResponsesList) Next() { rl.current = (rl.current + 1) % len(rl.keys) }
func (rl *ResponsesList) Prev() { rl.current = (rl.current - 1 + len(rl.keys)) % len(rl.keys) }
func (rl *ResponsesList) Cur() *Response { return rl.List[rl.keys[rl.current]] }
func (rl *ResponsesList) Index() int { return rl.current }
func (rl *ResponsesList) Len() int { return len(rl.keys) }
func (rl *ResponsesList) Keys() []string { return rl.keys }
func (rl *ResponsesList) Get(key string) *Response { return rl.List[key] }
func (rl *ResponsesList) Add(key string, r *Response) *ResponsesList {
rl.keys = append(rl.keys, key)
sort.Strings(rl.keys)
rl.List[key] = r
return rl
}
func (rl *ResponsesList) Del(key string) bool {
if _, ok := rl.List[key]; !ok {
return false
}
delete(rl.List, key)
i := sort.SearchStrings(rl.keys, key)
rl.keys = append(rl.keys[:i], rl.keys[i+1:]...)
return true
}
func ExpandPath(path string) string {
if path[0] == '~' {
path = "$HOME" + path[1:len(path)]
}
return os.ExpandEnv(path)
}
func openConfigFile(path string) (*os.File, error) {
return os.OpenFile(path, os.O_RDWR|os.O_CREATE, 0666)
}