-
-
Notifications
You must be signed in to change notification settings - Fork 122
/
response.go
367 lines (307 loc) · 7.67 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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
package httplab
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"os"
"sort"
"strconv"
"strings"
"time"
)
// BodyMode represent the current Body mode
type BodyMode uint
// String to satisfy interface fmt.Stringer
func (m BodyMode) String() string {
switch m {
case BodyInput:
return "Input"
case BodyFile:
return "File"
}
return ""
}
const (
// BodyInput takes the body input from input box
BodyInput BodyMode = iota + 1
// BodyFile takes the body input from a file
BodyFile
)
// Body is our response body content, that will either reference an local file or a runtime-supplied []byte.
type Body struct {
Mode BodyMode
Input []byte
File *os.File
}
// Payload reads out a []byte payload according to it's configuration in Body.BodyMode.
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, _ := io.ReadAll(body.File)
body.File.Seek(0, 0)
return bytes
}
return nil
}
// Info returns some basic info on the body.
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
}
// SetFile set a new source file for the body, if it exists.
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
}
// Response is the the preconfigured HTTP response that will be returned to the client.
type Response struct {
Status int
Headers http.Header
Body Body
Delay time.Duration
}
// UnmarshalJSON inflates the Response from []byte representing JSON.
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
}
// MarshalJSON serializes the response into a JSON []byte.
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, "", " ")
}
// NewResponse configures a new response. An empty status will be interpreted as 200 OK.
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{
Mode: BodyInput,
Input: []byte(body),
},
}, nil
}
// Write flushes the body into the ResponseWriter, hence sending it over the wire.
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
}
// ResponsesList holds the multiple configured responses.
type ResponsesList struct {
List map[string]*Response
keys []string
current int
}
// NewResponsesList creates a new empty response list and returns it.
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
}
// Load loads a response list from a local JSON document.
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
}
// Save saves the current response list to a JSON document on local disk.
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
}
// Next iterates to the next item in the response list.
func (rl *ResponsesList) Next() { rl.current = (rl.current + 1) % len(rl.keys) }
// Prev iterates to the previous item in the response list.
func (rl *ResponsesList) Prev() { rl.current = (rl.current - 1 + len(rl.keys)) % len(rl.keys) }
// Cur retrieves the current response from the response list.
func (rl *ResponsesList) Cur() *Response { return rl.List[rl.keys[rl.current]] }
// Index retrieves the index of the current item in the response list.
func (rl *ResponsesList) Index() int { return rl.current }
// Len reports the length of the response list.
func (rl *ResponsesList) Len() int { return len(rl.keys) }
// Keys retrieves an []string of all keys in the response list.
func (rl *ResponsesList) Keys() []string { return rl.keys }
// Get retrieves a specific response by name from the response list.
func (rl *ResponsesList) Get(key string) *Response { return rl.List[key] }
// Add appends a response item to the list. You need to supply a key for the item.
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
}
// Del removes an item spceified by its key from the response list. It returns false if the item didn't exist at all.
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
}
// ExpandPath expands a given path by replacing '~' with $HOME of the current user.
func ExpandPath(path string) string {
if path[0] == '~' {
path = "$HOME" + path[1:]
}
return os.ExpandEnv(path)
}
func openConfigFile(path string) (*os.File, error) {
return os.OpenFile(path, os.O_RDWR|os.O_CREATE, 0666)
}