Skip to content

Instantly share code, notes, and snippets.

@noqqe
Last active April 8, 2020 13:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save noqqe/f5cb617897eb495ea60a9f68be5ca412 to your computer and use it in GitHub Desktop.
Save noqqe/f5cb617897eb495ea60a9f68be5ca412 to your computer and use it in GitHub Desktop.
Tiny HTTP Server to test MaxUploadSize behind a Loadbalancer
package main
import (
"os"
"fmt"
"bytes"
"log"
"strconv"
"net/http"
)
func post(w http.ResponseWriter, r *http.Request) {
// fetch header size
size, err := strconv.Atoi(r.Header["Content-Length"][0])
if err != nil {
log.Fatal(err)
}
// fetch body size
buf := new(bytes.Buffer)
buf.ReadFrom(r.Body)
body := len(buf.String())
// bytes to mb
mbh := float64(size)/1000000
mbb := float64(body)/1000000
w.WriteHeader(200)
fmt.Fprintf(w, "Header: %.2f MB, Body: %.2f MB!\n", mbh, mbb)
fmt.Printf("Header: %.2f, Body: %.2f MB!\n", mbh, mbb)
}
func get(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(200)
fmt.Fprintf(w, "Try:\n curl -X POST --data-binary @<file> <host>:<port>\n" )
}
func handler(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case "GET":
get(w, r)
case "POST":
post(w, r)
default:
return
}
}
func main() {
port := ":" + os.Args[1]
http.HandleFunc("/", handler)
log.Fatal(http.ListenAndServe(port, nil))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment