[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,43 +25,36 @@ 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")
} }
} }
i := 1 if requestMessageLen >= 0 {
for x := range client.Messages { i := 1
w.Write([]byte(circolog.FormatSyslog(x))) for x := range client.Messages {
w.Write([]byte("\n")) w.Write([]byte(circolog.FormatSyslog(x)))
if requestMessageLen != 0 && i >= requestMessageLen { w.Write([]byte("\n"))
break if requestMessageLen != 0 && i >= requestMessageLen {
break
}
i++
} }
i++
} }
} }
} }