2018-05-21 10:02:24 +00:00
|
|
|
package gfw
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2018-05-30 07:06:26 +00:00
|
|
|
"git.xdrm.io/xdrm-brackets/gfw/config"
|
2018-05-21 10:02:24 +00:00
|
|
|
"log"
|
|
|
|
"net/http"
|
2018-05-22 07:10:10 +00:00
|
|
|
"strings"
|
2018-05-21 10:02:24 +00:00
|
|
|
)
|
|
|
|
|
2018-05-24 14:22:29 +00:00
|
|
|
func (s *Server) route(res http.ResponseWriter, req *http.Request) {
|
2018-05-21 10:02:24 +00:00
|
|
|
|
|
|
|
/* (1) Build request
|
|
|
|
---------------------------------------------------------*/
|
|
|
|
/* (1) Try to build request */
|
|
|
|
request, err := buildRequest(req)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(req)
|
|
|
|
}
|
|
|
|
|
2018-05-21 11:02:15 +00:00
|
|
|
/* (2) Find a controller
|
|
|
|
---------------------------------------------------------*/
|
|
|
|
/* (1) Init browsing cursors */
|
|
|
|
ctl := s.config
|
|
|
|
uriIndex := 0
|
|
|
|
|
|
|
|
/* (2) Browse while there is uri parts */
|
|
|
|
for uriIndex < len(request.Uri) {
|
|
|
|
uri := request.Uri[uriIndex]
|
|
|
|
|
|
|
|
child, hasKey := ctl.Children[uri]
|
|
|
|
|
|
|
|
// stop if no matchind child
|
|
|
|
if !hasKey {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
request.ControllerUri = append(request.ControllerUri, uri)
|
|
|
|
ctl = child
|
|
|
|
uriIndex++
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-05-29 13:42:41 +00:00
|
|
|
/* (3) Extract & store URI params */
|
|
|
|
request.Data.fillUrl(request.Uri[uriIndex:])
|
2018-05-22 07:10:10 +00:00
|
|
|
|
2018-05-21 11:02:15 +00:00
|
|
|
/* (3) Check method
|
|
|
|
---------------------------------------------------------*/
|
|
|
|
/* (1) Unavailable method */
|
2018-05-24 14:22:29 +00:00
|
|
|
if !config.IsMethodAvailable(req.Method) {
|
2018-05-21 11:02:15 +00:00
|
|
|
|
|
|
|
Json, _ := ErrUnknownMethod.MarshalJSON()
|
|
|
|
res.Header().Add("Content-Type", "application/json")
|
|
|
|
res.Write(Json)
|
|
|
|
log.Printf("[err] %s\n", ErrUnknownMethod.Reason)
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
2018-05-21 10:02:24 +00:00
|
|
|
|
2018-05-21 11:02:15 +00:00
|
|
|
/* (2) Extract method cursor */
|
2018-05-24 14:22:29 +00:00
|
|
|
var method = ctl.Method(req.Method)
|
2018-05-21 11:02:15 +00:00
|
|
|
|
|
|
|
/* (3) Unmanaged HTTP method */
|
|
|
|
if method == nil { // unknown method
|
|
|
|
Json, _ := ErrUnknownMethod.MarshalJSON()
|
|
|
|
res.Header().Add("Content-Type", "application/json")
|
|
|
|
res.Write(Json)
|
|
|
|
log.Printf("[err] %s\n", ErrUnknownMethod.Reason)
|
|
|
|
return
|
|
|
|
}
|
2018-05-21 10:02:24 +00:00
|
|
|
|
2018-05-29 17:01:20 +00:00
|
|
|
/* (4) Check parameters
|
2018-05-21 11:02:15 +00:00
|
|
|
---------------------------------------------------------*/
|
2018-05-28 16:25:17 +00:00
|
|
|
var paramError Err = ErrSuccess
|
2018-05-29 17:01:20 +00:00
|
|
|
parameters := make(map[string]interface{})
|
2018-05-28 16:25:17 +00:00
|
|
|
for name, param := range method.Parameters {
|
|
|
|
|
|
|
|
/* (1) Extract value */
|
2018-05-29 13:42:41 +00:00
|
|
|
p, isset := request.Data.Set[name]
|
2018-05-28 16:25:17 +00:00
|
|
|
|
2018-05-29 17:01:20 +00:00
|
|
|
/* (2) Required & missing */
|
|
|
|
if !isset && !*param.Optional {
|
|
|
|
paramError = ErrMissingParam
|
|
|
|
paramError.BindArgument(name)
|
|
|
|
break
|
|
|
|
}
|
2018-05-28 16:25:17 +00:00
|
|
|
|
2018-05-29 17:01:20 +00:00
|
|
|
/* (3) Optional & missing: set default value */
|
|
|
|
if !isset {
|
|
|
|
p = &RequestParameter{
|
|
|
|
Parsed: true,
|
|
|
|
File: param.Type == "FILE",
|
|
|
|
Value: nil,
|
|
|
|
}
|
|
|
|
if param.Default != nil {
|
|
|
|
p.Value = *param.Default
|
2018-05-28 16:25:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-29 17:01:20 +00:00
|
|
|
/* (4) Parse parameter if not file */
|
2018-05-29 14:00:43 +00:00
|
|
|
if !p.Parsed && !p.File {
|
|
|
|
p.Value = parseHttpData(p.Value)
|
|
|
|
}
|
|
|
|
|
2018-05-29 17:01:20 +00:00
|
|
|
/* (4) Fail on unexpected multipart file */
|
|
|
|
waitFile, gotFile := param.Type == "FILE", p.File
|
|
|
|
if gotFile && !waitFile || !gotFile && waitFile {
|
|
|
|
paramError = ErrInvalidParam
|
|
|
|
paramError.BindArgument(name)
|
|
|
|
paramError.BindArgument("FILE")
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
/* (5) Do not check if file */
|
|
|
|
if gotFile {
|
|
|
|
parameters[name] = p.Value
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
/* (6) Check type */
|
|
|
|
if s.Checker.Run(param.Type, p.Value) != nil {
|
|
|
|
|
2018-05-28 16:25:17 +00:00
|
|
|
paramError = ErrInvalidParam
|
|
|
|
paramError.BindArgument(name)
|
|
|
|
paramError.BindArgument(param.Type)
|
2018-05-29 13:42:41 +00:00
|
|
|
paramError.BindArgument(p.Value)
|
2018-05-28 16:25:17 +00:00
|
|
|
break
|
2018-05-29 17:01:20 +00:00
|
|
|
|
2018-05-28 16:25:17 +00:00
|
|
|
}
|
|
|
|
|
2018-05-29 17:01:20 +00:00
|
|
|
parameters[name] = p.Value
|
|
|
|
|
2018-05-22 07:28:30 +00:00
|
|
|
}
|
|
|
|
fmt.Printf("\n")
|
|
|
|
|
2018-05-28 16:25:17 +00:00
|
|
|
// Fail if argument check failed
|
|
|
|
if paramError.Code != ErrSuccess.Code {
|
|
|
|
Json, _ := paramError.MarshalJSON()
|
|
|
|
res.Header().Add("Content-Type", "application/json")
|
|
|
|
res.Write(Json)
|
|
|
|
log.Printf("[err] %s\n", paramError.Reason)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-05-22 07:10:10 +00:00
|
|
|
fmt.Printf("OK\nplugin: '%si.so'\n", strings.Join(request.ControllerUri, "/"))
|
2018-05-29 17:01:20 +00:00
|
|
|
for name, value := range parameters {
|
|
|
|
fmt.Printf(" $%s = %v\n", name, value)
|
|
|
|
}
|
2018-05-21 11:02:15 +00:00
|
|
|
return
|
2018-05-21 10:02:24 +00:00
|
|
|
}
|