-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
77 lines (65 loc) · 2.19 KB
/
main.go
File metadata and controls
77 lines (65 loc) · 2.19 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
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
package main
import (
"fmt"
"net/http"
"os"
"sort"
"strings"
)
func main() {
// register the handler for all requests
http.HandleFunc("/", handleRequest)
// get port from environment variable, default to 80
port := os.Getenv("PORT")
if port == "" {
port = "80"
}
// start the server
fmt.Printf("Server starting on http://localhost:%s\n", port)
if err := http.ListenAndServe(":"+port, nil); err != nil {
fmt.Printf("Error starting server: %v\n", err)
}
}
// handleRequest displays all request information including headers
func handleRequest(w http.ResponseWriter, r *http.Request) {
// set content type to plain text for easy reading
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
var sb strings.Builder
// basic request info
sb.WriteString("=== REQUEST INFO ===\n\n")
sb.WriteString(fmt.Sprintf("Method: %s\n", r.Method))
sb.WriteString(fmt.Sprintf("URL: %s\n", r.URL.String()))
sb.WriteString(fmt.Sprintf("Path: %s\n", r.URL.Path))
sb.WriteString(fmt.Sprintf("Raw Query: %s\n", r.URL.RawQuery))
sb.WriteString(fmt.Sprintf("Protocol: %s\n", r.Proto))
sb.WriteString(fmt.Sprintf("Host: %s\n", r.Host))
sb.WriteString(fmt.Sprintf("Remote Address: %s\n", r.RemoteAddr))
sb.WriteString(fmt.Sprintf("Request URI: %s\n", r.RequestURI))
// headers section - sorted alphabetically for consistent output
sb.WriteString("\n=== HEADERS ===\n\n")
headerNames := make([]string, 0, len(r.Header))
for name := range r.Header {
headerNames = append(headerNames, name)
}
sort.Strings(headerNames)
for _, name := range headerNames {
// headers can have multiple values
values := r.Header[name]
sb.WriteString(fmt.Sprintf("%s: %s\n", name, strings.Join(values, ", ")))
}
// query parameters if any
if len(r.URL.Query()) > 0 {
sb.WriteString("\n=== QUERY PARAMETERS ===\n\n")
queryKeys := make([]string, 0, len(r.URL.Query()))
for key := range r.URL.Query() {
queryKeys = append(queryKeys, key)
}
sort.Strings(queryKeys)
for _, key := range queryKeys {
values := r.URL.Query()[key]
sb.WriteString(fmt.Sprintf("%s: %s\n", key, strings.Join(values, ", ")))
}
}
// write the response
fmt.Fprint(w, sb.String())
}