forked from Francesco149/sharenix
/
requests.go
200 lines (170 loc) · 4.55 KB
/
requests.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
192
193
194
195
196
197
198
199
200
/*
Copyright 2014 Franc[e]sco (lolisamurai@tfwno.gf)
This file is part of sharenix.
sharenix is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
sharenix is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with sharenix. If not, see <http://www.gnu.org/licenses/>.
*/
package sharenixlib
import (
"bytes"
"fmt"
"io"
"mime/multipart"
"net/http"
"net/http/httputil"
"net/textproto"
neturl "net/url"
"os"
"path/filepath"
"strings"
)
var quoteEscaper = strings.NewReplacer("\\", "\\\\", `"`, "\\\"")
func escapeQuotes(s string) string {
return quoteEscaper.Replace(s)
}
// SniffMimeType sniffs the mime type of a binary file by reading the
// first 512 bytes
func SniffMimeType(filePath string) (string, error) {
file, err := os.Open(filePath)
if err != nil {
return "", fmt.Errorf("SniffMimeType failed to open file: %s", err)
}
defer file.Close()
// first 512 bytes are used to evaluate mime type
first512 := make([]byte, 512)
n, err := file.Read(first512)
if err != nil {
return "", err
}
return http.DetectContentType(first512[:n]), nil
}
// SendRequest prepares HTTP request and sends it
//
// if fileParamName empty, no file field will be created and
// filePath is ignored
// if username is empty, no http auth header will be sent
//
// if method is GET or PUT, the parameters will be url-encoded, otherwise they
// will be fields of the multi-part form
//
// if method is PUT and filePath is set, the request body will be the contents
// of the file
func SendRequest(method, url, fileParamName, filePath string,
extraParams map[string]string, extraHeaders map[string]string,
username string, password string,
) (res *http.Response, filename string, err error) {
if method == "GET" || method == "PUT" {
var u *neturl.URL
u, err = neturl.Parse(url)
if err != nil {
return
}
// url-encode extra params
q := u.Query()
for name, value := range extraParams {
q.Set(name, value)
}
u.RawQuery = q.Encode()
DebugPrintln(u)
url = u.String()
}
// prepare request body buffer
buf := &bytes.Buffer{}
w := multipart.NewWriter(buf)
if fileParamName != "" && method != "PUT" {
filename = filepath.Base(filePath)
var realmime string
realmime, err = SniffMimeType(filePath)
if err != nil {
return
}
// try opening the file
var file *os.File
file, err = os.Open(filePath)
if err != nil {
return
}
defer file.Close()
// create a multipart file header with the given param name and file
h := make(textproto.MIMEHeader)
h.Set("Content-Disposition",
fmt.Sprintf(`form-data; name="%s"; filename="%s"`,
escapeQuotes(fileParamName), escapeQuotes(filename)))
h.Set("Content-Type", realmime)
var formfile io.Writer
formfile, err = w.CreatePart(h)
if err != nil {
return
}
// write the file to the file header
_, err = io.Copy(formfile, file)
}
// append extra params as form fields
if method != "GET" && method != "PUT" {
for param, val := range extraParams {
err = w.WriteField(param, val)
if err != nil {
return
}
}
}
ctype := w.FormDataContentType()
err = w.Close()
if err != nil {
return
}
// finally create the request
var req *http.Request
if method == "PUT" && filePath != "" {
var freader *os.File
freader, err = os.Open(filePath)
defer freader.Close()
if err != nil {
return
}
req, err = http.NewRequest(method, url, freader)
var realmime string
realmime, err = SniffMimeType(filePath)
if err != nil {
return
}
req.Header.Set("Content-Type", realmime)
} else {
req, err = http.NewRequest(method, url, buf)
// set type & boundary
req.Header.Set("Content-Type", ctype)
}
if err != nil {
return
}
// extra headers
req.Header.Set("User-Agent", strings.Replace(ShareNixVersion, " ", "/", 1))
for hname, hval := range extraHeaders {
req.Header.Set(hname, hval)
}
// auth
if username != "" {
req.SetBasicAuth(username, password)
}
// send request
client := &http.Client{}
requestDump, err := httputil.DumpRequest(req, false)
if err != nil {
DebugPrintln(err)
} else {
DebugPrintln(fmt.Sprintf("%q", requestDump))
}
res, err = client.Do(req)
if err != nil {
panic(err)
}
return
}