Skip to content

Commit

Permalink
implemented sorting headers, added ignoring (404) /favicon.ico requests
Browse files Browse the repository at this point in the history
  • Loading branch information
lukaszbudnik committed Feb 3, 2021
1 parent c6c3cd5 commit 3b5c522
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 lukasz.budnik@gmail.com
LABEL maintainer="Łukasz Budnik lukasz.budnik@gmail.com"

# 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 3b5c522

Please sign in to comment.