Last active
December 14, 2016 15:56
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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