From 7185caa17988632f4b155640081625883b630ec0 Mon Sep 17 00:00:00 2001 From: Alexander Didenko Date: Wed, 1 Jun 2022 13:28:49 +0200 Subject: [PATCH] Fix bug with ":" symbol in http headers (#29) Add support for `:` in http headers. Closes: https://github.com/wayfair-incubator/minigun/issues/28 --- .gitignore | 3 +++ README.md | 2 +- main.go | 9 ++++++--- main_test.go | 26 ++++++++++++++++++++++++++ 4 files changed, 36 insertions(+), 4 deletions(-) diff --git a/.gitignore b/.gitignore index 2ba8ffc..9873473 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,6 @@ +# IDE +.vscode + # Ignore vendor to reduce repo size vendor diff --git a/README.md b/README.md index f1a9019..5ed1715 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # Minigun -[![Minigun Version](https://img.shields.io/badge/Minigun-0.4.1-7f187f.svg)](https://github.com/wayfair-incubator/minigun/blob/main/CHANGELOG.md) +[![Minigun Version](https://img.shields.io/badge/Minigun-0.4.2-7f187f.svg)](https://github.com/wayfair-incubator/minigun/blob/main/CHANGELOG.md) [![Contributor Covenant](https://img.shields.io/badge/Contributor%20Covenant-2.0-4baaaa.svg)](CODE_OF_CONDUCT.md) ## About The Project diff --git a/main.go b/main.go index 220c9ca..313f895 100644 --- a/main.go +++ b/main.go @@ -35,7 +35,7 @@ import ( ) // Constants and vars -const version = "0.4.1" +const version = "0.4.2" const workersCannelSize = 1024 const errorBadHTTPCode = "Bad HTTP status code" @@ -120,7 +120,7 @@ func (headers *httpHeaders) Set(value string) error { var tmpHeaders httpHeaders s := strings.Split(value, ":") - if len(s) != 2 { + if len(s) < 2 { return fmt.Errorf("Wrong header argument") } @@ -130,7 +130,10 @@ func (headers *httpHeaders) Set(value string) error { tmpHeaders = *headers } - tmpHeaders[s[0]] = s[1] + tmpHeader := strings.TrimSpace(s[0]) + tmpValue := strings.TrimSpace(strings.Join(s[1:], ":")) + + tmpHeaders[tmpHeader] = tmpValue *headers = tmpHeaders return nil diff --git a/main_test.go b/main_test.go index 4c1bc90..4da3c92 100644 --- a/main_test.go +++ b/main_test.go @@ -17,3 +17,29 @@ func TestRandomBytes(t *testing.T) { len(result)) } } + +func TestHeadersParsing(t *testing.T) { + var sendHTTPHeaders httpHeaders + + sendHTTPHeaders.Set("Host: hostname.local") + sendHTTPHeaders.Set("H1 : V1,V2 ") + sendHTTPHeaders.Set("Authorization: id:hash") + sendHTTPHeaders.Set("Keep-Alive: timeout=2, max=100") + sendHTTPHeaders.Set("Content-Type: application/xml") + sendHTTPHeaders.Set("Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 ") + + expected := httpHeaders{ + "Authorization": "id:hash", + "H1": "V1,V2", + "Host": "hostname.local", + "Keep-Alive": "timeout=2, max=100", + "Content-Type": "application/xml", + "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8", + } + + for k, v := range expected { + if sendHTTPHeaders[k] != v { + t.Errorf("Expected header: %q => %q, got header: %q => %q", k, v, k, sendHTTPHeaders[k]) + } + } +}