This repository has been archived by the owner on Nov 22, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
request.go
112 lines (86 loc) · 2.19 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
package httpc
import (
"bytes"
"io"
)
// @author valor.
// ReqBody need by stdlib HTTP client
type ReqBody struct {
// Request body
Content io.Reader
// Content-Length
Length int64
// Content-type
Type string
}
// ReqBodyPublisher processing request body
type ReqBodyPublisher interface {
Subscribe() ReqBody
}
// ------ empty body ------
// PublisherNoBody is the implement of ReqBodyPublisher
// -> No request body
type PublisherNoBody struct{}
// Subscribe the empty request body
func (e PublisherNoBody) Subscribe() ReqBody {
return ReqBody{}
}
// ------ raw body ------
// RawType HTTP content-type enum
type RawType int
const (
// RawTypeText "text/plain; charset=utf-8"
RawTypeText RawType = iota
// RawTypeHtml "text/html; charset=utf-8"
RawTypeHtml
// RawTypeJson "application/json; charset=utf-8"
RawTypeJson
// RawTypeXml "application/xml; charset=utf-8"
RawTypeXml
// RawTypeUrlEncodedForm "application/x-www-form-urlencoded; charset=utf-8"
RawTypeUrlEncodedForm
)
// RawTypesMap HTTP content-type map
var RawTypesMap = map[RawType]string{
RawTypeText: "text/plain; charset=utf-8",
RawTypeHtml: "text/html; charset=utf-8",
RawTypeJson: "application/json; charset=utf-8",
RawTypeXml: "application/xml; charset=utf-8",
RawTypeUrlEncodedForm: "application/x-www-form-urlencoded; charset=utf-8",
}
// PublisherRawBytesBody is the implement of ReqBodyPublisher
// -> Byte array request body
type PublisherRawBytesBody struct {
Body []byte
Type RawType
}
// Subscribe the byte array request body
func (raw PublisherRawBytesBody) Subscribe() ReqBody {
length := len(raw.Body)
buf := bytes.Buffer{}
buf.Grow(length)
buf.Write(raw.Body)
return ReqBody{
Content: &buf,
Length: int64(length),
Type: RawTypesMap[raw.Type],
}
}
// PublisherRawStringBody is the implement of ReqBodyPublisher
// -> String request body
type PublisherRawStringBody struct {
Body string
Type RawType
}
// Subscribe the string request body
func (raw PublisherRawStringBody) Subscribe() ReqBody {
length := len(raw.Body)
buf := bytes.Buffer{}
buf.Grow(length)
buf.WriteString(raw.Body)
return ReqBody{
Content: &buf,
Length: int64(length),
Type: RawTypesMap[raw.Type],
}
}