[http] adding first draft of server-side message number limit.
This commit is contained in:
parent
d380deae37
commit
89c59e5713
1 changed files with 39 additions and 0 deletions
|
@ -1,7 +1,10 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"os"
|
||||||
|
"strconv"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"git.lattuga.net/boyska/circolog"
|
"git.lattuga.net/boyska/circolog"
|
||||||
|
@ -21,9 +24,45 @@ func getHTTPHandler(hub circolog.Hub) http.HandlerFunc {
|
||||||
Nofollow: true}
|
Nofollow: true}
|
||||||
hub.Register <- client
|
hub.Register <- client
|
||||||
|
|
||||||
|
err := r.ParseForm()
|
||||||
|
if err != nil {
|
||||||
|
fmt.Fprintln(os.Stderr, "error parsing http request", err)
|
||||||
|
}
|
||||||
|
// Looking for known parameter in the request
|
||||||
|
// TODO: write specialized function
|
||||||
|
var requestMessageLen int
|
||||||
|
if reqL, ok := r.Form["l"]; ok {
|
||||||
|
var err error
|
||||||
|
switch {
|
||||||
|
case len(reqL) == 1:
|
||||||
|
requestMessageLen, err = strconv.Atoi(reqL[0])
|
||||||
|
if requestMessageLen <= 0 {
|
||||||
|
fmt.Fprintln(os.Stderr, "malformed request, l non positive:", requestMessageLen)
|
||||||
|
//requestMessageLen := 0
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
fmt.Fprintln(os.Stderr, "malformed request on parameter l:", err)
|
||||||
|
}
|
||||||
|
case len(reqL) > 1:
|
||||||
|
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
|
||||||
for x := range client.Messages {
|
for x := range client.Messages {
|
||||||
w.Write([]byte(circolog.FormatSyslog(x)))
|
w.Write([]byte(circolog.FormatSyslog(x)))
|
||||||
w.Write([]byte("\n"))
|
w.Write([]byte("\n"))
|
||||||
|
if requestMessageLen != 0 && i >= requestMessageLen {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
i++
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue