2018-07-10 23:36:42 +00:00
|
|
|
package apirequest
|
2018-06-01 08:51:51 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2018-07-08 23:00:45 +00:00
|
|
|
"git.xdrm.io/go/aicra/err"
|
2018-07-08 22:15:29 +00:00
|
|
|
"git.xdrm.io/go/aicra/response"
|
2018-06-01 08:51:51 +00:00
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
"plugin"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2018-07-11 17:02:33 +00:00
|
|
|
// FromHTTP builds an interface request from a http.Request
|
|
|
|
func FromHTTP(req *http.Request) (*Request, error) {
|
2018-06-01 08:51:51 +00:00
|
|
|
|
|
|
|
/* (1) Get useful data */
|
2018-07-08 23:34:21 +00:00
|
|
|
uri := normaliseURI(req.URL.Path)
|
2018-06-01 08:51:51 +00:00
|
|
|
uriparts := strings.Split(uri, "/")
|
|
|
|
|
|
|
|
/* (2) Init request */
|
|
|
|
inst := &Request{
|
2018-07-08 23:34:21 +00:00
|
|
|
URI: uriparts,
|
2018-06-01 08:51:51 +00:00
|
|
|
Path: make([]string, 0, len(uriparts)),
|
|
|
|
Data: NewDataset(),
|
|
|
|
}
|
|
|
|
|
|
|
|
/* (3) Build dataset */
|
|
|
|
inst.Data.Build(req)
|
|
|
|
|
|
|
|
return inst, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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{})
|
|
|
|
|
|
|
|
// Abort if GET request
|
|
|
|
if req.Method == "GET" {
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
startn := time.Now().UnixNano()
|
|
|
|
// 1. Parse form-data
|
|
|
|
if err := req.ParseMultipartForm(req.ContentLength + 1); err != nil {
|
|
|
|
log.Printf("[read.multipart] %s\n", err)
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
|
|
|
// 2. Extract values
|
|
|
|
for name, value := range req.PostForm {
|
|
|
|
res[name] = value
|
|
|
|
}
|
|
|
|
fmt.Printf("* %.3f us\n", float64(time.Now().UnixNano()-startn)/1e3)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
|
|
|
// LoadController tries to load a controller from its uri
|
|
|
|
// checks for its given method ('Get', 'Post', 'Put', or 'Delete')
|
2018-07-08 23:00:45 +00:00
|
|
|
func (i *Request) LoadController(method string) (func(response.Arguments, *response.Response) response.Response, err.Error) {
|
2018-06-01 08:51:51 +00:00
|
|
|
|
|
|
|
/* (1) Build controller path */
|
2018-07-08 09:30:53 +00:00
|
|
|
path := strings.Join(i.Path, "-")
|
|
|
|
if len(path) == 0 {
|
2018-07-08 09:44:39 +00:00
|
|
|
path = fmt.Sprintf(".build/controller/ROOT.so")
|
2018-07-08 09:30:53 +00:00
|
|
|
} else {
|
2018-07-08 09:44:39 +00:00
|
|
|
path = fmt.Sprintf(".build/controller/%s.so", path)
|
2018-07-08 09:30:53 +00:00
|
|
|
}
|
2018-06-01 08:51:51 +00:00
|
|
|
|
|
|
|
/* (2) Format url */
|
|
|
|
tmp := []byte(strings.ToLower(method))
|
|
|
|
tmp[0] = tmp[0] - ('a' - 'A')
|
|
|
|
method = string(tmp)
|
|
|
|
|
|
|
|
/* (2) Try to load plugin */
|
|
|
|
p, err2 := plugin.Open(path)
|
|
|
|
if err2 != nil {
|
2018-07-08 23:00:45 +00:00
|
|
|
return nil, err.UncallableController
|
2018-06-01 08:51:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* (3) Try to extract method */
|
|
|
|
m, err2 := p.Lookup(method)
|
|
|
|
if err2 != nil {
|
2018-07-08 23:00:45 +00:00
|
|
|
return nil, err.UncallableMethod
|
2018-06-01 08:51:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* (4) Check signature */
|
2018-07-08 22:15:29 +00:00
|
|
|
callable, validSignature := m.(func(response.Arguments, *response.Response) response.Response)
|
2018-06-01 08:51:51 +00:00
|
|
|
if !validSignature {
|
2018-07-08 23:00:45 +00:00
|
|
|
return nil, err.UncallableMethod
|
2018-06-01 08:51:51 +00:00
|
|
|
}
|
|
|
|
|
2018-07-08 23:00:45 +00:00
|
|
|
return callable, err.Success
|
2018-06-01 08:51:51 +00:00
|
|
|
|
|
|
|
}
|