This repository has been archived by the owner on Jul 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
request.go
191 lines (160 loc) · 3.86 KB
/
request.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
package etp
import (
"bufio"
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"net/textproto"
"net/url"
"strconv"
"strings"
"github.com/jmoiron/jsonq"
)
type Request struct {
Method string
URL *url.URL
Proto string // "ETP/1.0"
ProtoMajor int // 1
ProtoMinor int // 0
PathArgs []string
QueryArgs map[string][]string // TODO:
ExtHeader http.Header // 扩展头部
Header http.Header
Body []byte
ContentLength int64 // TODO: needed?
RequestURI string
}
func NewRequest(method, urlStr string, body []byte) (req *Request, err error) {
u, err := url.Parse(urlStr)
if err != nil {
return nil, err
}
req = &Request{
Method: method,
URL: u,
Proto: "ETP/1.0",
ProtoMajor: 1,
ProtoMinor: 1,
Header: make(http.Header),
Body: body,
ExtHeader: make(http.Header),
}
req.ContentLength = int64(len(body))
if req.ContentLength > 0 {
req.Header.Set("Content-Length", strconv.FormatInt(req.ContentLength, 10))
}
return req, nil
}
func (req *Request) Query() *jsonq.JsonQuery {
data := map[string]interface{}{}
// url
for k, v := range req.URL.Query() {
switch len(v) {
case 0:
continue
case 1:
data[k] = v[0]
default:
data[k] = v
}
}
// body
obj := req.getBodyObj()
if obj != nil {
for k, v := range obj {
data[k] = v
}
}
return jsonq.NewQuery(data)
}
func (req *Request) getBodyObj() map[string]interface{} {
t := req.Header.Get("Content-Type")
if strings.Contains(t, "application/json") {
data := map[string]interface{}{}
dec := json.NewDecoder(bytes.NewBuffer(req.Body))
dec.Decode(&data)
return data
}
return nil
}
func (req *Request) BodyQuery() *jsonq.JsonQuery {
// TODO: 根据 Header["Content-Type"] 判断
data := map[string]interface{}{}
dec := json.NewDecoder(bytes.NewBuffer(req.Body))
dec.Decode(&data)
return jsonq.NewQuery(data)
}
func (req *Request) Bytes() []byte {
buf := new(bytes.Buffer)
io.WriteString(buf, fmt.Sprintf("%s %s ETP/1.0\r\n", req.Method, req.URL.RequestURI()))
if req.Header != nil {
for k, v := range req.Header {
joinStr := ", "
if k == "COOKIE" {
joinStr = "; "
}
io.WriteString(buf, fmt.Sprintf("%s: %s\r\n", k, strings.Join(v, joinStr)))
}
}
if req.Body != nil {
//io.WriteString(buf, fmt.Sprintf("Content-Length: %d\r\n", len(req.Body)))
io.WriteString(buf, "\r\n")
buf.Write(req.Body)
}
return buf.Bytes()
}
func ReadRequest(data []byte) (req *Request, err error) {
tp := textproto.NewReader(bufio.NewReader(bytes.NewReader(data)))
req = new(Request)
// TODO: 初始化
req.Header = make(http.Header)
req.ExtHeader = make(http.Header)
// read first line: GET /index.html ETP/1.0
var s string
if s, err = tp.ReadLine(); err != nil {
return nil, err
}
var ok bool
req.Method, req.RequestURI, req.Proto, ok = parseRequestLine(s)
if !ok {
return nil, &badStringError{"malformed ETP request", s}
}
if req.URL, err = url.ParseRequestURI(req.RequestURI); err != nil {
return nil, err
}
mimeHeader, err := tp.ReadMIMEHeader()
// TODO: Important! Add First!
if mimeHeader != nil {
req.Header = http.Header(mimeHeader)
}
if err != nil {
// TODO: etp 定制? 无 headers
if err == io.EOF {
return req, nil
}
return nil, err
}
if clen := req.Header.Get("Content-Length"); len(clen) > 0 {
req.ContentLength, err = strconv.ParseInt(clen, 10, 64)
if err != nil {
fmt.Printf("parse Content-Length (%s): %s\n", clen, err)
return nil, err
}
}
// TODO: ugly?
before := int64(len(data)) - req.ContentLength
req.Body = data[before:]
return req, nil
}
// parseRequestLine parses "GET /foo ETP/1.0" into its three parts.
func parseRequestLine(line string) (method, requestURI, proto string, ok bool) {
s1 := strings.Index(line, " ")
s2 := strings.Index(line[s1+1:], " ")
if s1 < 0 || s2 < 0 {
return
}
s2 += s1 + 1
return line[:s1], line[s1+1 : s2], line[s2+1:], true
}