forked from 1buran/rHttp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsession.go
106 lines (89 loc) · 2.3 KB
/
session.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
package main
import (
"encoding/json"
"io"
"net/http"
)
// Request reflects the [http.Request] params.
type Request struct {
Host string `json:"host"`
Scheme string `json:"scheme"`
Method string `json:"method"`
UrlPath string `json:"url"`
Headers map[string][]string `json:"headers"`
FormValues map[string][]string `json:"form"`
RawQuery string `json:"qs"`
}
// Response reflects the [http.Response] data.
type Response struct {
Status string `json:"status"`
Proto string `json:"proto"`
Headers map[string][]string `json:"headers"`
BodyLines []string `json:"body"`
}
// Session reflect current state: some stats, request settings and last response (with data).
type Session struct {
ReqCount int `json:"reqCount"`
ResTime string `json:"resTime"`
Request Request `json:"req"`
Response Response `json:"res"`
}
// Create a new session.
func NewSession(rq *http.Request, rs *http.Response, rqc int, rqt string, rqf map[string][]string, rsb []string) (*Session, error) {
req := Request{Headers: make(map[string][]string), FormValues: make(map[string][]string)}
res := Response{Headers: make(map[string][]string)}
// populate Request with [http.Request] data
if rq != nil {
req.Method = rq.Method
req.Scheme = rq.URL.Scheme
req.Host = rq.URL.Host
req.UrlPath = rq.URL.Path
for k, v := range rq.Header {
req.Headers[k] = v
}
req.RawQuery = rq.URL.RawQuery
req.FormValues = rqf
}
// populate Response with [http.Response] data
if rs != nil {
res.Status = rs.Status
res.Proto = rs.Proto
for k, v := range rs.Header {
res.Headers[k] = v
}
res.BodyLines = rsb
}
return &Session{ReqCount: rqc, ResTime: rqt, Request: req, Response: res}, nil
}
// Save the session.
func (s *Session) Save(o io.WriteCloser) error {
b, err := json.Marshal(s)
if err != nil {
return err
}
_, err = o.Write(b)
if err != nil {
return err
}
err = o.Close()
if err != nil {
return err
}
return nil
}
// Load the session.
func (s *Session) Load(r io.ReadCloser) error {
b, err := io.ReadAll(r)
if err != nil {
return err
}
err = r.Close()
if err != nil {
return err
}
err = json.Unmarshal(b, s)
if err != nil {
return err
}
return nil
}