Skip to content

Instantly share code, notes, and snippets.

@roundand
Last active December 14, 2016 15:56
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 roundand/98dfcd45db60255ab04fcc4d7b972d0e to your computer and use it in GitHub Desktop.
Save roundand/98dfcd45db60255ab04fcc4d7b972d0e to your computer and use it in GitHub Desktop.
echoDump can bind to any hostname and port, and responds to all accepted queries by echoing a dump of the incoming request.
// echoDump is a minimal "echo" server that responds with a dump of the incoming request.
// (Based on https://github.com/adonovan/gopl.io/blob/master/ch1/server1/main.go)
package main
import (
"flag"
"fmt"
"log"
"net/http"
"net/http/httputil"
)
var site string // the host and port we'll listen on
func init() {
flag.StringVar(&site, "site", ":8000", "host:port value") // pick up commandline site, or default
}
func main() {
flag.Parse()
http.HandleFunc("/", handler) // each request calls handler
fmt.Println("Listening on site:", site) // let user know what we've bound to
log.Fatal(http.ListenAndServe(site, nil))
}
// handler echoes a dump of the incoming request/
func handler(w http.ResponseWriter, r *http.Request) {
dump, err := httputil.DumpRequest(r, true)
if err != nil {
fmt.Printf("DumpRequest generated err: %q\n", err)
http.Error(w, fmt.Sprint(err), http.StatusInternalServerError)
return
}
fmt.Printf("Request:\n%s\n", dump)
fmt.Fprintf(w, "Request:\n%s\n", dump)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment