-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproxy.go
57 lines (49 loc) · 1.17 KB
/
proxy.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
package main
import (
"bytes"
"encoding/json"
"fmt"
"log"
"net/http"
)
type ProxyResponseWriter struct {
StatusCode int
Body *bytes.Buffer
header http.Header
}
func NewProxyResponseWriter() *ProxyResponseWriter {
return &ProxyResponseWriter{
Body: bytes.NewBuffer(nil),
header: http.Header{},
}
}
func (w *ProxyResponseWriter) Header() http.Header {
return w.header
}
func (w *ProxyResponseWriter) Write(b []byte) (int, error) {
return w.Body.Write(b)
}
func (w *ProxyResponseWriter) WriteHeader(statusCode int) {
w.StatusCode = statusCode
}
func (w *ProxyResponseWriter) Dump(dest http.ResponseWriter) error {
for key, values := range w.Header() {
for _, value := range values {
dest.Header().Add(key, value)
}
}
dest.WriteHeader(w.StatusCode)
data := w.Body.Bytes()
dest.Header().Set("Content-Length", fmt.Sprint(len(data)))
_, err := dest.Write(data)
if err != nil {
log.Fatal(err)
}
return err
}
func (w *ProxyResponseWriter) ReadJSONRPCResponse() (result JSONRPCResponse, err error) {
if err = json.NewDecoder(bytes.NewReader(w.Body.Bytes())).Decode(&result); err != nil {
w.WriteHeader(http.StatusInternalServerError)
}
return
}