-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdebug-httpd.go
More file actions
51 lines (46 loc) · 1.32 KB
/
Copy pathdebug-httpd.go
File metadata and controls
51 lines (46 loc) · 1.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package main
import (
"fmt"
"io/ioutil"
"net/http"
"os"
"regexp"
"strings"
)
func handler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Remote-Addr: %s\n", r.RemoteAddr)
fmt.Fprintf(w, "Host: %s\n", r.Host)
fmt.Fprintf(w, "Path: %s\n", r.URL.Path)
fmt.Fprintf(w, "Query: %s\n", r.URL.RawQuery)
fmt.Fprintf(w, "Headers:\n")
for k,vs := range r.Header {
fmt.Fprintf(w, "\t%s:\n", k)
for _, v := range vs {
if (k == "Cookie") {
v = strings.Join(strings.Split(v, "; "), "\n\t\t")
}
fmt.Fprintf(w, "\t\t%s\n", v)
}
}
fmt.Fprintf(w, "Environment:\n")
for _, env := range os.Environ() {
fmt.Fprintf(w, "\t%s\n", env)
}
if os.Getenv("UPSTREAM") != "" {
fmt.Fprintf(w, "Upstream response:\n")
resp, err := http.Get(os.Getenv("UPSTREAM"))
if err != nil {
fmt.Fprintf(w, "\t%s\n", err)
return
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
lineStarts := regexp.MustCompile(`(?m:^)`)
tabbedBody := lineStarts.ReplaceAllString(string(body), "\t")
fmt.Fprintf(w, "%s\n", tabbedBody)
}
}
func main() {
http.HandleFunc("/", handler)
http.ListenAndServe(":8080", nil)
}