server/frontend/submissions.go

75 lines
2.0 KiB
Go
Raw Normal View History

2017-11-22 00:52:08 +00:00
package main
import (
"log"
"net/http"
"os"
"path"
"strings"
)
type submissionHandler func(w http.ResponseWriter, r *http.Request, sURL []string)
type submissionChecker struct {
kind string
next submissionHandler
2017-11-22 00:52:08 +00:00
}
type submissionTeamHandler func(w http.ResponseWriter, r *http.Request, team string, sURL []string)
type submissionTeamChecker struct {
kind string
next submissionTeamHandler
teamsDir string
simulator string
2017-11-22 00:52:08 +00:00
}
func (c submissionChecker) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if addr := r.Header.Get("X-Forwarded-For"); addr != "" {
r.RemoteAddr = addr
}
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())
2017-11-22 00:52:08 +00:00
w.Header().Set("Content-Type", "application/json")
// Check request type and size
2021-11-29 14:26:47 +00:00
if r.Method != "POST" || r.ContentLength < 0 {
2017-11-22 00:52:08 +00:00
http.Error(w, "{\"errmsg\":\"Requête invalide.\"}", http.StatusBadRequest)
return
2021-11-29 14:26:47 +00:00
} else if r.ContentLength > 4097 {
http.Error(w, "{\"errmsg\":\"Requête trop longue\"}", http.StatusRequestEntityTooLarge)
2017-11-22 00:52:08 +00:00
return
}
// Extract URL arguments
var sURL = strings.Split(strings.TrimPrefix(r.URL.Path, "/"), "/")
2017-11-22 00:52:08 +00:00
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) {
team := c.simulator
if t := r.Header.Get("X-FIC-Team"); t != "" {
team = t
2017-11-22 00:52:08 +00:00
}
// Check team validity and existance
if len(team) < 1 || team == "-" || team == "public" {
2017-11-22 00:52:08 +00:00
log.Println("INVALID TEAM:", team)
http.Error(w, "{\"errmsg\":\"Équipe inexistante.\"}", http.StatusBadRequest)
return
} else if _, err := os.Stat(path.Join(c.teamsDir, team)); os.IsNotExist(err) {
log.Println("UNKNOWN TEAM:", team)
http.Error(w, "{\"errmsg\":\"Équipe inexistante.\"}", http.StatusBadRequest)
return
}
c.next(w, r, team, sURL)
2017-11-22 00:52:08 +00:00
}}.ServeHTTP(w, r)
}