Skip to content

Commit

Permalink
Merge pull request #1 from lukaszbudnik/sorting-headers-ignoring-favicon
Browse files Browse the repository at this point in the history
implemented sorting headers, added ignoring (404) /favicon.ico requests
  • Loading branch information
lukaszbudnik committed Feb 3, 2021
2 parents c6c3cd5 + 3b5c522 commit b013a03
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 8 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
FROM golang:1.13.5-alpine3.10 as builder

MAINTAINER Łukasz Budnik [email protected]
LABEL maintainer="Łukasz Budnik [email protected]"

# build yosoy
RUN apk add git
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Typical use cases include:
* testing HTTP routing & ingress
* testing HTTP load balancing
* testing HTTP caching
* stubbing and prototyping the infrastructure
* stubbing and prototyping distributed applications

yosoy will provide information like:

Expand Down Expand Up @@ -38,7 +38,7 @@ It exposes HTTP service on port 80.

Let's take a look at a sample Kubernetes deployment file. It uses both `YOSOY_SHOW_ENVS` and `YOSOY_SHOW_FILES`.

> To illustrate `YOSOY_SHOW_FILES` functionality it uses Kubernetes Downward API to expose labels and annotations as volume files which are then read by yosoy.
> To illustrate `YOSOY_SHOW_FILES` functionality Kubernetes Downward API is used to expose labels and annotations as volume files which are then returned by yosoy.
```
apiVersion: apps/v1
Expand Down Expand Up @@ -116,8 +116,8 @@ Remote IP: 172.18.0.1
Called: 2
HTTP headers:
User-Agent: curl/7.58.0
Accept: */*
User-Agent: curl/7.64.1
Env variables:
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
Expand Down
19 changes: 15 additions & 4 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"io/ioutil"
"net/http"
"os"
"sort"
"strings"
"time"
)
Expand All @@ -15,13 +16,17 @@ var showEnvs = os.Getenv("YOSOY_SHOW_ENVS")
var showFiles = os.Getenv("YOSOY_SHOW_FILES")

func handler(w http.ResponseWriter, req *http.Request) {
if req.RequestURI == "/favicon.ico" {
w.WriteHeader(http.StatusNotFound)
return
}

remoteAddr := req.RemoteAddr
// LastIndex works better with IPv6
if index := strings.LastIndex(remoteAddr, ":"); index > 0 {
remoteAddr = remoteAddr[0:index]
}
fmt.Printf("[%v] - %v - %v - \"%v %v\"\n", hostname, time.Now().Format(time.RFC3339), remoteAddr, req.Method, req.RequestURI)
w.WriteHeader(200)
w.WriteHeader(http.StatusOK)
w.Header().Add("Content-Type", "text/plain")
fmt.Fprintf(w, "Request URI: %v\n", req.RequestURI)
fmt.Fprintf(w, "Hostname: %v\n", hostname)
Expand All @@ -30,9 +35,15 @@ func handler(w http.ResponseWriter, req *http.Request) {
fmt.Fprintf(w, "Called: %v\n", counter)
fmt.Fprintln(w)
fmt.Fprintf(w, "HTTP headers:\n")
for name, headers := range req.Header {
headers := make([]string, 0, len(req.Header))
for k := range req.Header {
headers = append(headers, k)
}
sort.Strings(headers)
for _, header := range headers {
headers := req.Header[header]
for _, h := range headers {
fmt.Fprintf(w, "%v: %v\n", name, h)
fmt.Fprintf(w, "%v: %v\n", header, h)
}
}
if strings.ToLower(showEnvs) == "true" || strings.ToLower(showEnvs) == "yes" || strings.ToLower(showEnvs) == "on" || showEnvs == "1" {
Expand Down

0 comments on commit b013a03

Please sign in to comment.