I am new to Go, and using Mux to accept HTTP POST data. I would like to use the MaxBytesReader to ensure a client does not overwhelm my server. According to the code, there is a requestBodyLimit
boolean which indicates whether that limit has been hit.
My question is: when using MaxBytesReader, how can I determine whether I have actually hit the maximum when processing a request?
Here is my code:
package main
import (
"fmt"
"log"
"html/template"
"net/http"
"github.com/gorilla/mux"
)
func main() {
r := mux.NewRouter()
r.HandleFunc("/handle", maxBytes(PostHandler)).Methods("POST")
http.ListenAndServe(":8080", r)
}
// Middleware to enforce the maximum post body size
func maxBytes(f http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
// As an example, limit post body to 10 bytes
r.Body = http.MaxBytesReader(w, r.Body, 10)
f(w, r)
}
}
func PostHandler(w http.ResponseWriter, r *http.Request) {
// How do I know if the form data has been truncated?
book := r.FormValue("email")
fmt.Fprintf(w, "You've requested the book: %s\n", book)
}
How can I:
Determine that I have reached the max POST limit (or have access to
requestBodyLimit
Have my code be able to branch on this condition?