Skip to content

Instantly share code, notes, and snippets.

@dgjustice
Created August 11, 2023 20:06
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 dgjustice/613b6a8c6be489fef0331753646e3961 to your computer and use it in GitHub Desktop.
Save dgjustice/613b6a8c6be489fef0331753646e3961 to your computer and use it in GitHub Desktop.
Golang http vomit comet
package main
import (
"crypto/rand"
"errors"
"fmt"
"io"
"net/http"
"strconv"
)
func writeRand(s int, writer io.Writer) error {
b := make([]byte, s)
_, err := rand.Read(b)
if err != nil {
return err
}
writer.Write(b)
return nil
}
func getRoot(w http.ResponseWriter, r *http.Request) {
hasSize := r.URL.Query().Has("size")
size := r.URL.Query().Get("size")
if hasSize {
s, err := strconv.Atoi(size)
if err != nil {
http.Error(w, fmt.Sprintf("unable to convert size to integer: %v", err), http.StatusBadRequest)
}
err = writeRand(s, w)
if err != nil {
http.Error(w, fmt.Sprintf("unable to copy data to socket: %v", err), http.StatusBadRequest)
}
return
}
io.WriteString(w, "Hello world\n")
}
func main() {
mux := http.NewServeMux()
mux.HandleFunc("/", getRoot)
server := &http.Server{
Addr: ":3333",
Handler: mux,
}
err := server.ListenAndServe()
if errors.Is(err, http.ErrServerClosed) {
fmt.Printf("server closed\n")
} else if err != nil {
fmt.Printf("error listening for server: %s\n", err)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment