This repository was archived by the owner on Jan 12, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmethod-override.go
114 lines (104 loc) · 2.72 KB
/
method-override.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
// go-rs/method-override
// Copyright(c) 2019 Roshan Gade. All rights reserved.
// MIT Licensed
package methodoverride
import (
"encoding/json"
"net/http"
"net/url"
"reflect"
"strings"
"github.com/go-rs/rest-api-framework"
)
var (
header = "X-HTTP-Method-Override"
methods = []string{
http.MethodGet,
http.MethodPost,
http.MethodPut,
http.MethodDelete,
http.MethodOptions,
http.MethodHead,
http.MethodPatch,
}
)
// validate method
func isValidMethod(method string) bool {
for _, v := range methods {
if strings.EqualFold(v, method) {
return true
}
}
return false
}
// convert json to string
func jsonToString(val interface{}) string {
b, _ := json.Marshal(val)
return string(b)
}
// High-level merge of request json into query
func mergeQuery(query url.Values, body map[string]interface{}) url.Values {
for key, val := range body {
str, ok := val.(string)
if ok {
query.Add(key, str)
} else {
if reflect.TypeOf(val).String() == "[]interface {}" {
for _, val := range val.([]interface{}) {
str, ok = val.(string)
if ok {
query.Add(key, str)
} else {
query.Add(key, jsonToString(val))
}
}
} else {
query.Add(key, jsonToString(val))
}
}
}
return query
}
// Possible error codes expose to handle errors
const (
ErrCodeMalformedBody = "MALFORMED_BODY"
ErrCodeFormParse = "FORM_PARSE_ERROR"
)
// Load method override middleware/interceptor.
func Load() rest.Handler {
return func(ctx *rest.Context) {
method := ctx.Request.Header.Get(header)
if method != "" && isValidMethod(method) {
ctx.Set("OriginalMethod", ctx.Request.Method)
if strings.EqualFold(method, http.MethodGet) && strings.EqualFold(ctx.Request.Method, http.MethodPost) {
// parsing only json request body and form-data
contentType := strings.ToLower(ctx.Request.Header.Get("content-type"))
if strings.Contains(contentType, "application/json") {
var body map[string]interface{}
if ctx.Body != nil && reflect.TypeOf(ctx.Body).String() == "map[string]interface {}" {
body = ctx.Body
} else {
err := json.NewDecoder(ctx.Request.Body).Decode(&body)
if err != nil {
ctx.Status(400).ThrowWithError(ErrCodeMalformedBody, err)
return
}
}
ctx.Query = mergeQuery(ctx.Query, body)
} else if strings.Contains(contentType, "application/x-www-form-urlencoded") || strings.Contains(contentType, "multipart/form-data") {
if ctx.Request.Form != nil {
ctx.Query = ctx.Request.Form
} else {
err := ctx.Request.ParseForm()
if err != nil {
ctx.Status(400).ThrowWithError(ErrCodeFormParse, err)
return
}
ctx.Query = ctx.Request.Form
}
}
}
ctx.Request.Method = method
}
}
}