Skip to content

Instantly share code, notes, and snippets.

@noqqe noqqe/maxupload.go
Last active Apr 8, 2020

Embed
What would you like to do?
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
You can’t perform that action at this time.