implemented sorting headers, added ignoring (404) /favicon.ico requests

This commit is contained in:
Łukasz Budnik
2021-02-03 06:34:15 +01:00
parent c6c3cd5ed8
commit 3b5c522e2f
3 changed files with 19 additions and 8 deletions

View File

@@ -1,6 +1,6 @@
FROM golang:1.13.5-alpine3.10 as builder 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 # build yosoy
RUN apk add git RUN apk add git

View File

@@ -9,7 +9,7 @@ Typical use cases include:
* testing HTTP routing & ingress * testing HTTP routing & ingress
* testing HTTP load balancing * testing HTTP load balancing
* testing HTTP caching * testing HTTP caching
* stubbing and prototyping the infrastructure * stubbing and prototyping distributed applications
yosoy will provide information like: yosoy will provide information like:
@@ -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`. 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 apiVersion: apps/v1
@@ -116,8 +116,8 @@ Remote IP: 172.18.0.1
Called: 2 Called: 2
HTTP headers: HTTP headers:
User-Agent: curl/7.58.0
Accept: */* Accept: */*
User-Agent: curl/7.64.1
Env variables: Env variables:
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

View File

@@ -5,6 +5,7 @@ import (
"io/ioutil" "io/ioutil"
"net/http" "net/http"
"os" "os"
"sort"
"strings" "strings"
"time" "time"
) )
@@ -15,13 +16,17 @@ var showEnvs = os.Getenv("YOSOY_SHOW_ENVS")
var showFiles = os.Getenv("YOSOY_SHOW_FILES") var showFiles = os.Getenv("YOSOY_SHOW_FILES")
func handler(w http.ResponseWriter, req *http.Request) { func handler(w http.ResponseWriter, req *http.Request) {
if req.RequestURI == "/favicon.ico" {
w.WriteHeader(http.StatusNotFound)
return
}
remoteAddr := req.RemoteAddr remoteAddr := req.RemoteAddr
// LastIndex works better with IPv6
if index := strings.LastIndex(remoteAddr, ":"); index > 0 { if index := strings.LastIndex(remoteAddr, ":"); index > 0 {
remoteAddr = remoteAddr[0:index] remoteAddr = remoteAddr[0:index]
} }
fmt.Printf("[%v] - %v - %v - \"%v %v\"\n", hostname, time.Now().Format(time.RFC3339), remoteAddr, req.Method, req.RequestURI) 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") w.Header().Add("Content-Type", "text/plain")
fmt.Fprintf(w, "Request URI: %v\n", req.RequestURI) fmt.Fprintf(w, "Request URI: %v\n", req.RequestURI)
fmt.Fprintf(w, "Hostname: %v\n", hostname) fmt.Fprintf(w, "Hostname: %v\n", hostname)
@@ -30,9 +35,15 @@ func handler(w http.ResponseWriter, req *http.Request) {
fmt.Fprintf(w, "Called: %v\n", counter) fmt.Fprintf(w, "Called: %v\n", counter)
fmt.Fprintln(w) fmt.Fprintln(w)
fmt.Fprintf(w, "HTTP headers:\n") 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 { 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" { if strings.ToLower(showEnvs) == "true" || strings.ToLower(showEnvs) == "yes" || strings.ToLower(showEnvs) == "on" || showEnvs == "1" {