68 lines
1.3 KiB
Go
68 lines
1.3 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"io"
|
|
"log"
|
|
"net/http"
|
|
"strings"
|
|
)
|
|
|
|
func handler(w http.ResponseWriter, r *http.Request) {
|
|
fmt.Println(strings.Repeat("~", 80))
|
|
|
|
if r.Host != "" {
|
|
fmt.Printf("Host: %s\n\n", r.Host)
|
|
}
|
|
|
|
if len(r.Header) > 0 {
|
|
fmt.Println("--- Headers ---")
|
|
for name, values := range r.Header {
|
|
for _, value := range values {
|
|
fmt.Printf("%s: %s\n", name, value)
|
|
}
|
|
}
|
|
fmt.Println()
|
|
}
|
|
|
|
if len(r.URL.Query()) > 0 {
|
|
fmt.Println("--- URL Parameters ---")
|
|
r.ParseForm()
|
|
for key, values := range r.URL.Query() {
|
|
for _, value := range values {
|
|
fmt.Printf("%s: %s\n", key, value)
|
|
}
|
|
}
|
|
fmt.Println()
|
|
}
|
|
|
|
if len(r.Cookies()) > 0 {
|
|
fmt.Println("--- Cookies ---")
|
|
for _, cookie := range r.Cookies() {
|
|
fmt.Printf("%s: %s\n", cookie.Name, cookie.Value)
|
|
}
|
|
fmt.Println()
|
|
}
|
|
|
|
bodyBytes, err := io.ReadAll(r.Body)
|
|
if err != nil {
|
|
fmt.Println("Error reading body:", err)
|
|
} else {
|
|
if bodyStr := string(bodyBytes); bodyStr != "" {
|
|
fmt.Println("--- Body ---")
|
|
fmt.Println(bodyStr)
|
|
fmt.Println()
|
|
}
|
|
}
|
|
defer r.Body.Close()
|
|
|
|
fmt.Println("--- Client IP ---")
|
|
fmt.Println(r.RemoteAddr)
|
|
}
|
|
|
|
func main() {
|
|
http.HandleFunc("/", handler)
|
|
fmt.Println("Server started at http://localhost:8080")
|
|
log.Fatal(http.ListenAndServe(":8080", nil))
|
|
}
|