-
Notifications
You must be signed in to change notification settings - Fork 0
/
content-type.go
143 lines (123 loc) · 2.29 KB
/
content-type.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
package h3
import (
"bytes"
"encoding/json"
"io"
"mime/multipart"
"net/url"
"os"
)
const (
json_type = "application/json"
urlencoded_type = "application/x-www-form-urlencoded"
)
type ContentBody struct {
*bytes.Buffer
Type string
}
func (c ContentBody) Read(p []byte) (n int, err error) {
if c.Buffer == nil {
return 0, nil
}
return c.Buffer.Read(p)
}
type Urlencoded struct {
v url.Values
}
func (u *Urlencoded) init() {
if u.v == nil {
u.v = make(url.Values)
}
}
func (u *Urlencoded) Add(key string, value string) {
u.init()
u.v.Add(key, value)
}
func (u *Urlencoded) Set(key string, value string) {
u.init()
u.v.Set(key, value)
}
func (u *Urlencoded) Get(key string) string {
if u.v == nil {
return ""
}
return u.v.Get(key)
}
func (u *Urlencoded) Del(key string) {
if u.v != nil {
u.v.Del(key)
}
}
func (u Urlencoded) Form() ContentBody {
if u.v == nil {
return ContentBody{
Type: urlencoded_type,
Buffer: bytes.NewBuffer([]byte("")),
}
}
return ContentBody{
Type: urlencoded_type,
Buffer: bytes.NewBuffer([]byte(u.v.Encode())),
}
}
type Json struct {
buf *bytes.Buffer
}
func (j *Json) Set(v interface{}) error {
if s, ok := v.(string); ok {
j.buf = bytes.NewBuffer([]byte(s))
return nil
} else if b, ok := v.([]byte); ok {
j.buf = bytes.NewBuffer(b)
return nil
}
buf, err := json.Marshal(v)
if err != nil {
return err
}
j.buf = bytes.NewBuffer(buf)
return nil
}
func (j Json) Form() ContentBody {
if j.buf == nil {
return ContentBody{
Type: json_type,
Buffer: bytes.NewBuffer([]byte("")),
}
}
return ContentBody{
Type: json_type,
Buffer: j.buf,
}
}
type FormData struct {
buf *bytes.Buffer
writer *multipart.Writer
}
func (f *FormData) init() {
if f.writer == nil {
f.buf = &bytes.Buffer{}
f.writer = multipart.NewWriter(f.buf)
}
}
func (f *FormData) Add(key string, value string) error {
f.init()
return f.writer.WriteField(key, value)
}
func (f *FormData) AddFile(key string, filename string, file *os.File) error {
f.init()
part, err := f.writer.CreateFormFile(key, filename)
if err != nil {
return err
}
_, err = io.Copy(part, file)
return err
}
func (f FormData) Form() ContentBody {
f.init()
f.writer.Close()
return ContentBody{
Type: f.writer.FormDataContentType(),
Buffer: f.buf,
}
}