summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJordan <me@jordan.im>2022-12-31 14:52:04 -0700
committerJordan <me@jordan.im>2022-12-31 14:52:04 -0700
commit7c1d3208374e36f2d3c3a16c10ab59adc98b46ba (patch)
tree6125eabbf1c3ee8c89b31267c1ade93b2c3bd820
downloadwhoami-7c1d3208374e36f2d3c3a16c10ab59adc98b46ba.tar.gz
whoami-7c1d3208374e36f2d3c3a16c10ab59adc98b46ba.zip
initial commit
-rw-r--r--Makefile23
-rw-r--r--go.mod3
-rw-r--r--whoami.go43
3 files changed, 69 insertions, 0 deletions
diff --git a/Makefile b/Makefile
new file mode 100644
index 0000000..1f0220a
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,23 @@
+.POSIX:
+.SUFFIXES:
+
+GO = go
+RM = rm
+GOFLAGS =
+PREFIX = /usr/local
+BINDIR = $(PREFIX)/bin
+
+goflags = $(GOFLAGS)
+
+all: whoami
+
+whoami:
+ $(GO) build $(goflags)
+
+clean:
+ $(RM) -f whoami
+
+install: all
+ mkdir -p $(DESTDIR)$(BINDIR)
+ cp -f whoami $(DESTDIR)$(BINDIR)
+
diff --git a/go.mod b/go.mod
new file mode 100644
index 0000000..89caab4
--- /dev/null
+++ b/go.mod
@@ -0,0 +1,3 @@
+module whoami
+
+go 1.19
diff --git a/whoami.go b/whoami.go
new file mode 100644
index 0000000..de583e9
--- /dev/null
+++ b/whoami.go
@@ -0,0 +1,43 @@
+package main
+
+import (
+ "flag"
+ "fmt"
+ "html"
+ "log"
+ "net/http"
+ "sort"
+)
+
+func RequestHandler(w http.ResponseWriter, r *http.Request) {
+
+ fmt.Fprintf(w, "\"Host\": %q\n", html.EscapeString(r.Host))
+ fmt.Fprintf(w, "\"IP\": %q\n", html.EscapeString(r.RemoteAddr))
+ fmt.Fprintf(w, "\"URI\": %q\n", html.EscapeString(r.RequestURI))
+
+ keys := make([]string, 0)
+ for k, _ := range r.Header {
+ keys = append(keys, k)
+ }
+ sort.Strings(keys)
+ for _, k := range keys {
+ for _, value := range r.Header[k] {
+ fmt.Fprintf(w, "%q: %q\n", html.EscapeString(k), html.EscapeString(value))
+ }
+ }
+}
+
+func main() {
+
+ var (
+ host string
+ port uint64
+ )
+ flag.StringVar(&host, "host", "127.0.0.1", "IP address to listen on")
+ flag.Uint64Var(&port, "port", 8080, "Port to listen on (default 8080)")
+ flag.Parse()
+
+ http.HandleFunc("/", RequestHandler)
+ fmt.Printf("Listening on %v port %v (http://%v:%v/)\n", host, port, host, port)
+ log.Fatal(http.ListenAndServe(fmt.Sprintf("%s:%d", host, port), nil))
+}