[http] Stricter behaviour with l=

This commit is contained in:
Blallo 2018-11-05 11:53:26 +01:00
parent 89c59e5713
commit 4838f45f13
No known key found for this signature in database
GPG key ID: 0CBE577C9B72DC3F

View file

@ -1,9 +1,8 @@
package main package main
import ( import (
"fmt" "log"
"net/http" "net/http"
"os"
"strconv" "strconv"
"time" "time"
@ -26,35 +25,27 @@ func getHTTPHandler(hub circolog.Hub) http.HandlerFunc {
err := r.ParseForm() err := r.ParseForm()
if err != nil { if err != nil {
fmt.Fprintln(os.Stderr, "error parsing http request", err) log.Println("error parsing http request", err)
} }
// Looking for known parameter in the request // Looking for known parameter in the request
// TODO: write specialized function // TODO: write specialized function
var requestMessageLen int var requestMessageLen int
if reqL, ok := r.Form["l"]; ok { if reqL, ok := r.Form["l"]; ok {
var err error if len(reqL) == 1 {
switch {
case len(reqL) == 1:
requestMessageLen, err = strconv.Atoi(reqL[0]) requestMessageLen, err = strconv.Atoi(reqL[0])
if requestMessageLen <= 0 { if err != nil || requestMessageLen <= 0 {
fmt.Fprintln(os.Stderr, "malformed request, l non positive:", requestMessageLen) log.Println("malformed request on parameter l")
//requestMessageLen := 0 requestMessageLen = -1
w.WriteHeader(400)
} }
if err != nil { } else {
fmt.Fprintln(os.Stderr, "malformed request on parameter l:", err) log.Println("malformed request on parameter l")
} requestMessageLen = -1
case len(reqL) > 1: w.WriteHeader(400)
requestMessageLen, err = strconv.Atoi(reqL[len(reqL)-1])
fmt.Fprintln(os.Stderr, "multiple values of l parameter, taking last:",
requestMessageLen)
if err != nil {
fmt.Fprintln(os.Stderr, "malformed request on parameter l:", err)
}
default:
fmt.Fprintln(os.Stderr, "empty parameter l in request")
} }
} }
if requestMessageLen >= 0 {
i := 1 i := 1
for x := range client.Messages { for x := range client.Messages {
w.Write([]byte(circolog.FormatSyslog(x))) w.Write([]byte(circolog.FormatSyslog(x)))
@ -65,6 +56,7 @@ func getHTTPHandler(hub circolog.Hub) http.HandlerFunc {
i++ i++
} }
} }
}
} }
func getWSHandler(hub circolog.Hub) http.HandlerFunc { func getWSHandler(hub circolog.Hub) http.HandlerFunc {