Skip to content

Commit

Permalink
add Host header manually (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
bukowa authored Oct 10, 2022
1 parent 44d191b commit 8ce8694
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 27 deletions.
55 changes: 29 additions & 26 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,42 +1,45 @@
package main

import (
"fmt"
"log"
"net/http"
"sort"
"flag"
"strings"
"flag"
"fmt"
"log"
"net/http"
"sort"
"strings"
)

var Name = flag.String("name", "example", "name of instance")

func headers(w http.ResponseWriter, req *http.Request) {
log.Println("Hello! There's a request incoming!")
log.Printf("%v %v %v %v", req.RemoteAddr, req.URL, req.Host, req.Header)
sortedKeys := make([]string, 0, len(req.Header))

fmt.Fprintf(w, "Instance name: %s\n", *Name)
for name, _ := range req.Header {
sortedKeys = append(sortedKeys, name)
}
sort.Strings(sortedKeys)
for _, v := range sortedKeys {
fmt.Fprintf(w, "%v: %v\n", v, req.Header[v])
}
log.Println("Hello! There's a request incoming!")
log.Printf("%v %v %v %v", req.RemoteAddr, req.URL, req.Host, req.Header)

// For incoming requests, the Host header is promoted to the
// Request.Host field and removed from the Header map.
req.Header.Set("host", req.Host)
sortedKeys := make([]string, 0, len(req.Header))

fmt.Fprintf(w, "Instance name: %s\n", *Name)
for name, _ := range req.Header {
sortedKeys = append(sortedKeys, name)
}
sort.Strings(sortedKeys)
for _, v := range sortedKeys {
fmt.Fprintf(w, "%v: %v\n", v, req.Header[v])
}
}


func main() {
http.HandleFunc("/", headers)
http.HandleFunc("/", headers)

var host = flag.String("host", "", "host to run on")
var port = flag.String("port", "8080", "port to run on")
var host = flag.String("host", "", "host to run on")
var port = flag.String("port", "8080", "port to run on")

flag.Parse()
flag.Parse()

var address = strings.Join([]string{*host, *port}, ":")
var address = strings.Join([]string{*host, *port}, ":")

log.Printf("Starting instance %s server at %s", *Name, address)
http.ListenAndServe(address, nil)
log.Printf("Starting instance %s server at %s", *Name, address)
http.ListenAndServe(address, nil)
}
3 changes: 2 additions & 1 deletion test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ RESULT=$(curl -H 'User-Agent:' -H 'Header1: header1' -H 'Header2: header2' local
WANT='Instance name: example
Accept: [*/*]
Header1: [header1]
Header2: [header2]'
Header2: [header2]
Host: [localhost:9001]'

printf "===\nResult:\n===\n$RESULT\n"; printf "Want:\n===\n$WANT\n";

Expand Down

0 comments on commit 8ce8694

Please sign in to comment.