frontend: don't use path to give team's ID, use a dedicated header
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
nemunaire 2021-02-05 16:57:23 +01:00
commit 0d792dcd8f
5 changed files with 53 additions and 60 deletions

View file

@ -10,24 +10,29 @@ import (
type submissionHandler func(w http.ResponseWriter, r *http.Request, sURL []string)
type submissionChecker struct{
kind string
next submissionHandler
type submissionChecker struct {
kind string
next submissionHandler
}
type submissionTeamHandler func(w http.ResponseWriter, r *http.Request, team string, sURL []string)
type submissionTeamChecker struct{
kind string
next submissionTeamHandler
teamsDir string
type submissionTeamChecker struct {
kind string
next submissionTeamHandler
teamsDir string
simulator string
}
func (c submissionChecker) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if addr := r.Header.Get("X-Forwarded-For"); addr != "" {
r.RemoteAddr = addr
}
log.Printf("%s \"%s %s\" => %s [%s]\n", r.RemoteAddr, r.Method, r.URL.Path, c.kind, r.UserAgent())
team := "-"
if t := r.Header.Get("X-FIC-Team"); t != "" {
team = t
}
log.Printf("%s %s \"%s %s\" => %s [%s]\n", r.RemoteAddr, team, r.Method, r.URL.Path, c.kind, r.UserAgent())
w.Header().Set("Content-Type", "application/json")
@ -41,21 +46,20 @@ func (c submissionChecker) ServeHTTP(w http.ResponseWriter, r *http.Request) {
}
// Extract URL arguments
var sURL = strings.Split(r.URL.Path, "/")
var sURL = strings.Split(strings.TrimPrefix(r.URL.Path, "/"), "/")
c.next(w, r, sURL)
}
func (c submissionTeamChecker) ServeHTTP(w http.ResponseWriter, r *http.Request) {
submissionChecker{c.kind, func(w http.ResponseWriter, r *http.Request, sURL []string){
if len(sURL) < 1 {
http.Error(w, "{\"errmsg\":\"Arguments manquants.\"}", http.StatusBadRequest)
return
submissionChecker{c.kind, func(w http.ResponseWriter, r *http.Request, sURL []string) {
team := c.simulator
if t := r.Header.Get("X-FIC-Team"); t != "" {
team = t
}
team := sURL[0]
// Check team validity and existance
if len(team) < 1 || team == "public" {
if len(team) < 1 || team == "-" || team == "public" {
log.Println("INVALID TEAM:", team)
http.Error(w, "{\"errmsg\":\"Équipe inexistante.\"}", http.StatusBadRequest)
return
@ -65,6 +69,6 @@ func (c submissionTeamChecker) ServeHTTP(w http.ResponseWriter, r *http.Request)
return
}
c.next(w, r, team, sURL[1:])
c.next(w, r, team, sURL)
}}.ServeHTTP(w, r)
}