2018-05-21 10:02:24 +00:00
|
|
|
package gfw
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2018-05-21 11:02:15 +00:00
|
|
|
"fmt"
|
2018-06-01 07:09:26 +00:00
|
|
|
"git.xdrm.io/xdrm-brackets/gfw/err"
|
2018-05-21 10:02:24 +00:00
|
|
|
"log"
|
|
|
|
"net/http"
|
2018-06-01 07:09:26 +00:00
|
|
|
"plugin"
|
2018-05-28 16:25:17 +00:00
|
|
|
"reflect"
|
2018-05-21 10:02:24 +00:00
|
|
|
"strings"
|
2018-05-21 11:02:15 +00:00
|
|
|
"time"
|
2018-05-21 10:02:24 +00:00
|
|
|
)
|
|
|
|
|
2018-05-21 11:02:15 +00:00
|
|
|
// buildRequest builds an interface request
|
|
|
|
// from a http.Request
|
2018-05-21 10:02:24 +00:00
|
|
|
func buildRequest(req *http.Request) (*Request, error) {
|
|
|
|
|
2018-05-29 13:42:41 +00:00
|
|
|
/* (1) Get useful data */
|
2018-05-21 10:02:24 +00:00
|
|
|
uri := NormaliseUri(req.URL.Path)
|
2018-05-29 13:42:41 +00:00
|
|
|
uriparts := strings.Split(uri, "/")
|
2018-05-28 16:25:17 +00:00
|
|
|
|
2018-05-29 13:42:41 +00:00
|
|
|
/* (2) Init request */
|
|
|
|
inst := &Request{
|
|
|
|
Uri: uriparts,
|
|
|
|
ControllerUri: make([]string, 0, len(uriparts)),
|
|
|
|
Data: buildRequestDataFromRequest(req),
|
2018-05-22 07:10:10 +00:00
|
|
|
}
|
|
|
|
|
2018-05-21 10:02:24 +00:00
|
|
|
return inst, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// NormaliseUri removes the trailing '/' to always
|
|
|
|
// have the same Uri format for later processing
|
|
|
|
func NormaliseUri(uri string) string {
|
|
|
|
|
|
|
|
if len(uri) < 1 {
|
|
|
|
return uri
|
|
|
|
}
|
|
|
|
|
|
|
|
if uri[0] == '/' {
|
|
|
|
uri = uri[1:]
|
|
|
|
}
|
|
|
|
|
2018-05-24 14:00:31 +00:00
|
|
|
if len(uri) > 1 && uri[len(uri)-1] == '/' {
|
2018-05-21 10:02:24 +00:00
|
|
|
uri = uri[0 : len(uri)-1]
|
|
|
|
}
|
|
|
|
|
|
|
|
return uri
|
|
|
|
}
|
|
|
|
|
|
|
|
// FetchFormData extracts FORM data
|
|
|
|
//
|
|
|
|
// - parse 'form-data' if not supported (not POST requests)
|
|
|
|
// - parse 'x-www-form-urlencoded'
|
|
|
|
// - parse 'application/json'
|
|
|
|
func FetchFormData(req *http.Request) map[string]interface{} {
|
|
|
|
|
|
|
|
res := make(map[string]interface{})
|
|
|
|
|
2018-05-24 14:18:14 +00:00
|
|
|
// Abort if GET request
|
|
|
|
if req.Method == "GET" {
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
2018-05-21 10:02:24 +00:00
|
|
|
ct := req.Header.Get("Content-Type")
|
|
|
|
|
|
|
|
if strings.HasPrefix(ct, "application/json") {
|
|
|
|
|
|
|
|
receiver := make(map[string]interface{}, 0)
|
|
|
|
|
|
|
|
// 1. Init JSON reader
|
|
|
|
decoder := json.NewDecoder(req.Body)
|
|
|
|
if err := decoder.Decode(&receiver); err != nil {
|
|
|
|
log.Printf("[parse.json] %s\n", err)
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
|
|
|
// 2. Return result
|
|
|
|
return receiver
|
|
|
|
|
|
|
|
} else if strings.HasPrefix(ct, "application/x-www-form-urlencoded") {
|
|
|
|
|
|
|
|
// 1. Parse url encoded data
|
|
|
|
req.ParseForm()
|
|
|
|
|
|
|
|
// 2. Extract values
|
|
|
|
for name, value := range req.PostForm {
|
|
|
|
res[name] = value
|
|
|
|
}
|
|
|
|
|
|
|
|
} else { // form-data or anything
|
|
|
|
|
2018-05-21 11:02:15 +00:00
|
|
|
startn := time.Now().UnixNano()
|
2018-05-21 10:02:24 +00:00
|
|
|
// 1. Parse form-data
|
2018-05-21 11:02:15 +00:00
|
|
|
if err := req.ParseMultipartForm(req.ContentLength + 1); err != nil {
|
2018-05-21 10:02:24 +00:00
|
|
|
log.Printf("[read.multipart] %s\n", err)
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
|
|
|
// 2. Extract values
|
|
|
|
for name, value := range req.PostForm {
|
|
|
|
res[name] = value
|
|
|
|
}
|
2018-05-21 11:02:15 +00:00
|
|
|
fmt.Printf("* %.3f us\n", float64(time.Now().UnixNano()-startn)/1e3)
|
2018-05-21 10:02:24 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return res
|
|
|
|
}
|
2018-05-22 07:28:30 +00:00
|
|
|
|
2018-05-28 16:25:17 +00:00
|
|
|
// parseHttpData parses http GET/POST data
|
2018-05-29 13:42:41 +00:00
|
|
|
// - []string
|
|
|
|
// - size = 1 : return json of first element
|
|
|
|
// - size > 1 : return array of json elements
|
2018-05-28 16:25:17 +00:00
|
|
|
// - string : return json if valid, else return raw string
|
|
|
|
func parseHttpData(data interface{}) interface{} {
|
|
|
|
dtype := reflect.TypeOf(data)
|
|
|
|
dvalue := reflect.ValueOf(data)
|
|
|
|
|
|
|
|
switch dtype.Kind() {
|
|
|
|
|
|
|
|
/* (1) []string -> recursive */
|
|
|
|
case reflect.Slice:
|
|
|
|
|
|
|
|
// 1. Return nothing if empty
|
|
|
|
if dvalue.Len() == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// 2. only return first element if alone
|
|
|
|
if dvalue.Len() == 1 {
|
|
|
|
|
|
|
|
element := dvalue.Index(0)
|
|
|
|
if element.Kind() != reflect.String {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return parseHttpData(element.String())
|
|
|
|
|
|
|
|
// 3. Return all elements if more than 1
|
|
|
|
} else {
|
|
|
|
|
|
|
|
result := make([]interface{}, dvalue.Len())
|
|
|
|
|
|
|
|
for i, l := 0, dvalue.Len(); i < l; i++ {
|
|
|
|
element := dvalue.Index(i)
|
|
|
|
|
|
|
|
// ignore non-string
|
|
|
|
if element.Kind() != reflect.String {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
result[i] = parseHttpData(element.String())
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/* (2) string -> parse */
|
|
|
|
case reflect.String:
|
|
|
|
|
|
|
|
// build json wrapper
|
|
|
|
wrapper := fmt.Sprintf("{\"wrapped\":%s}", dvalue.String())
|
|
|
|
|
|
|
|
// try to parse as json
|
|
|
|
var result interface{}
|
|
|
|
err := json.Unmarshal([]byte(wrapper), &result)
|
|
|
|
|
|
|
|
// return if success
|
|
|
|
if err == nil {
|
|
|
|
|
|
|
|
mapval, ok := result.(map[string]interface{})
|
|
|
|
if !ok {
|
|
|
|
return dvalue.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
wrapped, ok := mapval["wrapped"]
|
|
|
|
if !ok {
|
|
|
|
return dvalue.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
return wrapped
|
|
|
|
}
|
|
|
|
|
|
|
|
// else return as string
|
|
|
|
return dvalue.String()
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/* (3) NIL if unknown type */
|
2018-05-29 13:42:41 +00:00
|
|
|
return dvalue
|
2018-05-28 16:25:17 +00:00
|
|
|
|
|
|
|
}
|
2018-06-01 07:09:26 +00:00
|
|
|
|
|
|
|
// loadController tries to load a controller from its uri
|
|
|
|
// checks for its given method ('Get', 'Post', 'Put', or 'Delete')
|
|
|
|
func (i *Request) loadController(method string) (func(map[string]interface{}) (map[string]interface{}, err.Error), error) {
|
|
|
|
|
|
|
|
/* (1) Build controller path */
|
|
|
|
path := fmt.Sprintf("%si.so", i.ControllerUri)
|
|
|
|
|
|
|
|
/* (2) Format url */
|
|
|
|
tmp := []byte(strings.ToLower(method))
|
|
|
|
tmp[0] = tmp[0] - ('a' - 'A')
|
|
|
|
method = string(tmp)
|
|
|
|
|
|
|
|
fmt.Printf("method is '%s'\n", method)
|
|
|
|
return nil, nil
|
|
|
|
|
|
|
|
/* (2) Try to load plugin */
|
|
|
|
p, err2 := plugin.Open(path)
|
|
|
|
if err2 != nil {
|
|
|
|
return nil, err2
|
|
|
|
}
|
|
|
|
|
|
|
|
/* (3) Try to extract method */
|
|
|
|
m, err2 := p.Lookup(method)
|
|
|
|
if err2 != nil {
|
|
|
|
return nil, err2
|
|
|
|
}
|
|
|
|
|
|
|
|
/* (4) Check signature */
|
|
|
|
callable, validSignature := m.(func(map[string]interface{}) (map[string]interface{}, err.Error))
|
|
|
|
if !validSignature {
|
|
|
|
return nil, fmt.Errorf("Invalid signature for method %s", method)
|
|
|
|
}
|
|
|
|
|
|
|
|
return callable, nil
|
|
|
|
|
|
|
|
}
|