Skip to content

Commit

Permalink
Fix bug with ":" symbol in http headers (#29)
Browse files Browse the repository at this point in the history
Add support for `:` in http headers.

Closes: #28
  • Loading branch information
adidenko authored Jun 1, 2022
1 parent a3d8cc9 commit 7185caa
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 4 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# IDE
.vscode

# Ignore vendor to reduce repo size
vendor

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
9 changes: 6 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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")
}

Expand All @@ -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
Expand Down
26 changes: 26 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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])
}
}
}

0 comments on commit 7185caa

Please sign in to comment.