-
Notifications
You must be signed in to change notification settings - Fork 26
/
response.go
53 lines (46 loc) · 1.3 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
// 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 (
"bytes"
"encoding/json"
"io"
"net/http"
)
type response struct {
Body []byte `json:"body"`
Code int `json:"code"`
Handle string `json:"handle"`
Header http.Header `json:"header"`
}
type body struct {
io.Reader
}
func (*body) Close() error { return nil }
func resMarshal(group string, res *response) []byte {
// This will never fail to marshal, so error can be ignored.
marshalled, _ := json.Marshal(res)
return append([]byte(group+recv), zip(marshalled)...)
}
func resUnmarshal(p []byte) (string, *http.Response, error) {
var handle string
var res *http.Response
unzipped, err := unzip(p)
if err != nil {
return handle, res, err.(*Error).escalate(errResUnmarshal)
}
in := new(response)
in.Header = http.Header(make(map[string][]string))
if err = json.Unmarshal(unzipped, in); err != nil {
return handle, res, newError(errResUnmarshalJSON, err.Error())
}
handle = in.Handle
res = new(http.Response)
res.Body = &body{bytes.NewBuffer(in.Body)}
res.ContentLength = int64(len(in.Body))
res.Header = in.Header
res.StatusCode = in.Code
res.Status = http.StatusText(in.Code)
return handle, res, nil
}