-
Notifications
You must be signed in to change notification settings - Fork 0
/
request.go
40 lines (33 loc) · 1.36 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
package rip
import (
"fmt"
"net/http"
)
func preprocessRequest(reqMethod, handlerMethod string, header http.Header, urlPath string, options *RouteOptions) (cleanedPath string, accept, contentType string, err error) {
accept, err = contentNegociateBestHeaderValue(header, "Accept", options.codecs.OrderedMimeTypes)
if err != nil {
return "", "", "", Error{
Status: http.StatusBadRequest,
Detail: fmt.Sprintf("bad accept header format: %v, codecs available: %v", header["Accept"], options.codecs.OrderedMimeTypes),
}
}
if accept == "" &&
(reqMethod == http.MethodPost || reqMethod == http.MethodGet) {
return "", "", "", Error{
Status: http.StatusNotAcceptable,
Detail: fmt.Sprintf("bad accept type: %v, codecs available: %v", header["Accept"], options.codecs.OrderedMimeTypes),
}
}
if reqMethod != handlerMethod {
return "", "", "", Error{Status: http.StatusMethodNotAllowed, Detail: "bad method"}
}
if reqMethod == http.MethodGet {
// We can ignore content type as there should be no body for a GET
return urlPath, accept, contentType, nil
}
contentType, err = contentNegociateBestHeaderValue(header, "Content-Type", options.codecs.OrderedMimeTypes)
if err != nil {
return "", "", "", Error{Status: http.StatusUnsupportedMediaType, Detail: fmt.Sprintf("bad content type header format: %v", err)}
}
return urlPath, accept, contentType, nil
}