/
common.go
282 lines (237 loc) · 8.33 KB
/
common.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
// Copyright (c) 2016 OpenM++
// This code is licensed under the MIT license (see LICENSE.txt for details)
package main
import (
"container/list"
"encoding/json"
"errors"
"io"
"mime/multipart"
"net/http"
"os"
"path/filepath"
"strconv"
"strings"
"github.com/husobee/vestigo"
"golang.org/x/text/language"
"github.com/openmpp/go/ompp/omppLog"
)
// logRequest is a middelware to log http request
func logRequest(next http.HandlerFunc) http.HandlerFunc {
if isLogRequest {
return func(w http.ResponseWriter, r *http.Request) {
omppLog.Log(r.Method, ": ", r.Host, r.URL)
next(w, r)
}
} // else
return next
}
// match request language with UI supported languages and return canonic language name
func matchRequestToUiLang(r *http.Request) string {
rqLangTags, _, _ := language.ParseAcceptLanguage(r.Header.Get("Accept-Language"))
tag, _, _ := uiLangMatcher.Match(rqLangTags...)
return tag.String()
}
// get value of url parameter ?name or router parameter /:name
func getRequestParam(r *http.Request, name string) string {
v := r.URL.Query().Get(name)
if v == "" {
v = vestigo.Param(r, name)
}
return v
}
// get boolean value of url parameter ?name or router parameter /:name
func getBoolRequestParam(r *http.Request, name string) (bool, bool) {
v := r.URL.Query().Get(name)
if v == "" {
v = vestigo.Param(r, name)
}
if v == "" {
return false, true // no such parameter: return = false by default
}
if isVal, err := strconv.ParseBool(v); err == nil {
return isVal, true // return result: value is boolean
}
return false, false // value is not boolean
}
// get integer value of url parameter ?name or router parameter /:name
func getIntRequestParam(r *http.Request, name string, defaultVal int) (int, bool) {
v := r.URL.Query().Get(name)
if v == "" {
v = vestigo.Param(r, name)
}
if v == "" {
return defaultVal, true // no such parameter: return defult value
}
if nVal, err := strconv.Atoi(v); err == nil {
return nVal, true // return result: value is integer
}
return defaultVal, false // value is not integer
}
// get int64 value of url parameter ?name or router parameter /:name
func getInt64RequestParam(r *http.Request, name string, defaultVal int64) (int64, bool) {
v := r.URL.Query().Get(name)
if v == "" {
v = vestigo.Param(r, name)
}
if v == "" {
return defaultVal, true // no such parameter: return defult value
}
if nVal, err := strconv.ParseInt(v, 0, 64); err == nil {
return nVal, true // return result: value is integer
}
return defaultVal, false // value is not integer
}
// get languages accepted by browser and
// append optional language argument on top of the list
func getRequestLang(r *http.Request, name string) []language.Tag {
// browser languages
rqLangTags, _, _ := language.ParseAcceptLanguage(r.Header.Get("Accept-Language"))
// if optional url parameter ?lang or router parameter /:lang specified
ln := r.URL.Query().Get(name)
if ln == "" {
ln = vestigo.Param(r, name)
}
// add lang parameter as top language
if ln != "" {
if t := language.Make(ln); t != language.Und {
rqLangTags = append([]language.Tag{t}, rqLangTags...)
}
}
return rqLangTags
}
// set Content-Type header by extension and invoke next handler.
// This function exist to suppress Windows registry content type overrides
func setContentType(next http.Handler) http.Handler {
var ctDef = map[string]string{
".css": "text/css; charset=utf-8",
".js": "text/javascript",
}
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if ext := filepath.Ext(r.URL.Path); ext != "" {
if ct := ctDef[strings.ToLower(ext)]; ct != "" {
w.Header().Set("Content-Type", ct)
}
}
next.ServeHTTP(w, r) // invoke next handler
})
}
// jsonSetHeaders set response headers: Content-Type: application/json and Access-Control-Allow-Origin
func jsonSetHeaders(w http.ResponseWriter, r *http.Request) {
// if Content-Type not set then use json
if _, isSet := w.Header()["Content-Type"]; !isSet {
w.Header().Set("Content-Type", "application/json")
}
// if request from localhost then allow response to any protocol or port
/*
if strings.HasPrefix(r.Host, "localhost") {
if _, isSet := w.Header()["Access-Control-Allow-Origin"]; !isSet {
w.Header().Set("Access-Control-Allow-Origin", "*")
}
}
*/
}
// jsonResponse set response headers and writes src as json into w response writer.
// On error it writes 500 internal server error response.
func jsonResponse(w http.ResponseWriter, r *http.Request, src interface{}) {
jsonSetHeaders(w, r)
err := json.NewEncoder(w).Encode(src)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
// jsonAppendListToResponse writes srcLst as json array into w response writer.
// It is an append to response and response headers must already be set.
// On error it writes 500 internal server error response.
func jsonAppendListToResponse(
w http.ResponseWriter, r *http.Request, srcLst *list.List, cvt func(interface{}) (interface{}, error)) {
w.Write([]byte{'['}) // start json output array
enc := json.NewEncoder(w)
isNext := false
for src := srcLst.Front(); src != nil; src = src.Next() {
if isNext {
w.Write([]byte{','}) // until the last separate array items with , comma
}
val := src.Value // id's cell
var err error
// convert cell from id's to code if converter specified
if cvt != nil {
if val, err = cvt(val); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
// write actual value
if err := enc.Encode(val); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
isNext = true
}
w.Write([]byte{']'}) // end of array
}
// jsonRequestDecode validate Content-Type: application/json and decode json body.
// Destination for json decode: dst must be a pointer.
// On error it writes error response 400 or 415 and return false.
func jsonRequestDecode(w http.ResponseWriter, r *http.Request, dst interface{}) bool {
// json body expected
if !strings.Contains(r.Header.Get("Content-Type"), "application/json") {
http.Error(w, "Expected Content-Type: application/json", http.StatusUnsupportedMediaType)
return false
}
// decode json
err := json.NewDecoder(r.Body).Decode(dst)
if err != nil {
if err == io.EOF {
http.Error(w, "Invalid (empty) json at "+r.URL.String(), http.StatusBadRequest)
return false
}
http.Error(w, "Json decode error at "+r.URL.String(), http.StatusBadRequest)
return false
}
return true // completed OK
}
// jsonMultipartDecode decode json from multipart form reader.
// It does move to next part, check part form name and decode json from part body.
// Destination for json decode: dst must be a pointer.
// On error it writes error response 400 or 415 and return false.
func jsonMultipartDecode(w http.ResponseWriter, mr *multipart.Reader, name string, dst interface{}) bool {
// open next part and check part name
part, err := mr.NextPart()
if err == io.EOF {
http.Error(w, "Invalid (empty) next part of multipart form "+name, http.StatusBadRequest)
return false
}
if err != nil {
http.Error(w, "Failed to get next part of multipart form "+name+" : "+err.Error(), http.StatusBadRequest)
return false
}
defer part.Close()
if part.FormName() != name {
http.Error(w, "Invalid part of multipart form, expected name: "+name, http.StatusBadRequest)
return false
}
// decode json
err = json.NewDecoder(part).Decode(dst)
if err != nil {
if err == io.EOF {
http.Error(w, "Invalid (empty) json part of multipart form "+name, http.StatusBadRequest)
return false
}
http.Error(w, "Json decode error at part of multipart form "+name, http.StatusBadRequest)
return false
}
return true // completed OK
}
// isDirExist return error if directory does not exist or not accessible
func isDirExist(dirPath string) error {
stat, err := os.Stat(dirPath)
if err != nil {
if os.IsNotExist(err) {
return errors.New("Error: directory not exist: " + dirPath)
}
return errors.New("Error: unable to access directory: " + dirPath + " : " + err.Error())
}
if !stat.IsDir() {
return errors.New("Error: directory expected: " + dirPath)
}
return nil
}