2018-05-21 10:02:24 +00:00
|
|
|
package gfw
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2018-05-21 11:02:15 +00:00
|
|
|
"git.xdrm.io/gfw/internal/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-21 11:02:15 +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
|
|
|
// fmt.Printf("Uri: %v\n", request.Uri)
|
|
|
|
// fmt.Printf("GET: %v\n", request.GetData)
|
|
|
|
// fmt.Printf("POST: %v\n", request.FormData)
|
2018-05-21 10:02:24 +00:00
|
|
|
|
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-22 07:10:10 +00:00
|
|
|
/* (3) Extract URI params */
|
|
|
|
uriParams := request.Uri[uriIndex:]
|
|
|
|
|
|
|
|
/* (4) Store them as Data */
|
|
|
|
for i, data := range uriParams {
|
|
|
|
request.UrlData[i] = data
|
|
|
|
request.Data[fmt.Sprintf("URL%d", i)] = data
|
|
|
|
}
|
|
|
|
|
2018-05-21 11:02:15 +00:00
|
|
|
/* (3) Check method
|
|
|
|
---------------------------------------------------------*/
|
|
|
|
/* (1) Unavailable method */
|
|
|
|
if req.Method == "GET" && ctl.GET == nil ||
|
|
|
|
req.Method == "POST" && ctl.POST == nil ||
|
|
|
|
req.Method == "PUT" && ctl.PUT == nil ||
|
|
|
|
req.Method == "DELETE" && ctl.DELETE == nil {
|
|
|
|
|
|
|
|
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 */
|
|
|
|
var method *config.Method
|
2018-05-21 10:02:24 +00:00
|
|
|
|
2018-05-21 11:02:15 +00:00
|
|
|
if req.Method == "GET" {
|
|
|
|
method = ctl.GET
|
|
|
|
} else if req.Method == "POST" {
|
|
|
|
method = ctl.POST
|
|
|
|
} else if req.Method == "PUT" {
|
|
|
|
method = ctl.PUT
|
|
|
|
} else if req.Method == "DELETE" {
|
|
|
|
method = ctl.DELETE
|
|
|
|
}
|
|
|
|
|
|
|
|
/* (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-21 11:02:15 +00:00
|
|
|
/* (4) Check arguments
|
|
|
|
---------------------------------------------------------*/
|
2018-05-22 07:10:10 +00:00
|
|
|
fmt.Printf("OK\nplugin: '%si.so'\n", strings.Join(request.ControllerUri, "/"))
|
2018-05-21 11:02:15 +00:00
|
|
|
return
|
2018-05-21 10:02:24 +00:00
|
|
|
}
|