-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathheaders.go
107 lines (91 loc) · 2.58 KB
/
headers.go
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package main
import (
// "bytes"
// "context"
// "encoding/json"
"fmt"
"html"
"net"
"time"
// "io/ioutil"
// "net"
"net/http"
// "net/url"
// "strings"
)
func headersHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/html; charset=utf8")
w.Write([]byte(`<html>
<head>
<meta charset="utf-8">
<title>HTTP Headers - Resolve.rs</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/kognise/water.css@latest/dist/light.min.css" />
</head>
<body>
<h1>
Debug page for HTTP headers
</h1>
<p>
These are the HTTP headers seen by this server.
</p>
<p>
Raw IP address:`))
rawIp, _, _ := net.SplitHostPort(r.RemoteAddr)
fmt.Fprintf(w, "%s", rawIp)
w.Write([]byte(`<br/>Calculated IP address: `))
fmt.Fprintf(w, "%s", getIpAddress(r))
w.Write([]byte(`<br/>Raw Host: `))
fmt.Fprintf(w, "%s", html.EscapeString(r.Host))
w.Write([]byte(`<br/>Calculated Host: `))
fmt.Fprintf(w, "%s", html.EscapeString(getHost(r)))
w.Write([]byte(`</p>
<table>
<thead>
<tr>
<th>Name</th>
<th>Value(s)</th>
</tr>
</thead>
<tbody>
`))
for name, values := range r.Header {
fmt.Fprintf(w, "<tr><td>%s</td><td>", html.EscapeString(name))
if len(values) == 1 {
fmt.Fprintf(w, "%s", html.EscapeString(values[0]))
} else {
for index, value := range values {
fmt.Fprintf(w, "%d: %s<br/>", index, html.EscapeString(value))
}
}
w.Write([]byte(`</td></tr>`))
}
w.Write([]byte(` </tbody>
</table>
<p>
<a href="https://github.com/redirect2me/cdn-geolocation">How this works</a>, including API details and source code!
</p>
<p>
<a href="https://resolve.rs/">Resolve.rs</a>
has more
<a href="https://resolve.rs/tools.html#http">HTTP troubleshooting tools</a>.
</p>
</body>
</html>`))
}
type headersApiResponse struct {
Success bool `json:"success"`
Message string `json:"message"`
Headers map[string][]string `json:"headers"`
Timestamp string `json:"timestamp"`
IpAddress string `json:"ip"`
}
func headersApiHandler(w http.ResponseWriter, r *http.Request) {
result := headersApiResponse{}
result.Timestamp = time.Now().UTC().Format(time.RFC3339)
result.IpAddress = getIpAddress(r)
result.Success = true
result.Message = "Free for light, non-commercial use"
result.Headers = getHeaders(r)
write_with_callback(w, r, result)
}