From 3b5c522e2fdf5650876fddde9c16a12e82e0f273 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Budnik?= Date: Wed, 3 Feb 2021 06:34:15 +0100 Subject: [PATCH] implemented sorting headers, added ignoring (404) /favicon.ico requests --- Dockerfile | 2 +- README.md | 6 +++--- server.go | 19 +++++++++++++++---- 3 files changed, 19 insertions(+), 8 deletions(-) diff --git a/Dockerfile b/Dockerfile index ee1101c..32251c0 100644 --- a/Dockerfile +++ b/Dockerfile @@ -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 diff --git a/README.md b/README.md index 939146b..aa065c1 100644 --- a/README.md +++ b/README.md @@ -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: @@ -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 @@ -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 diff --git a/server.go b/server.go index 3066b6e..eb706e6 100644 --- a/server.go +++ b/server.go @@ -5,6 +5,7 @@ import ( "io/ioutil" "net/http" "os" + "sort" "strings" "time" ) @@ -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) @@ -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" {