Skip to content

Commit

Permalink
Merge 7ce305a into 5a33ce0
Browse files Browse the repository at this point in the history
  • Loading branch information
derekcollison committed Oct 21, 2020
2 parents 5a33ce0 + 7ce305a commit 6d9dbe3
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
15 changes: 13 additions & 2 deletions nats.go
Expand Up @@ -884,7 +884,7 @@ func DiscoveredServersHandler(cb ConnHandler) Option {
}
}

// ErrorHandler is an Option to set the async error handler.
// ErrorHandler is an Option to set the async error handler.
func ErrorHandler(cb ErrHandler) Option {
return func(o *Options) error {
o.AsyncErrorCB = cb
Expand Down Expand Up @@ -2769,7 +2769,9 @@ const (
crlf = "\r\n"
hdrPreEnd = len(hdrLine) - len(crlf)
statusHdr = "Status"
descrHdr = "Description"
noResponders = "503"
statusLen = 3 // e.g. 20x, 40x, 50x
)

// decodeHeadersMsg will decode and headers.
Expand All @@ -2785,7 +2787,16 @@ func decodeHeadersMsg(data []byte) (http.Header, error) {
}
// Check if we have an inlined status.
if len(l) > hdrPreEnd {
mh.Add(statusHdr, strings.TrimLeft(l[hdrPreEnd:], " "))
var description string
status := strings.TrimSpace(l[hdrPreEnd:])
if len(status) != statusLen {
description = strings.TrimSpace(status[statusLen:])
status = status[:statusLen]
}
mh.Add(statusHdr, status)
if len(description) > 0 {
mh.Add(descrHdr, description)
}
}
return http.Header(mh), nil
}
Expand Down
17 changes: 17 additions & 0 deletions nats_test.go
Expand Up @@ -31,6 +31,7 @@ import (
"reflect"
"regexp"
"runtime"
"strconv"
"strings"
"sync"
"sync/atomic"
Expand Down Expand Up @@ -2446,6 +2447,22 @@ func TestHeaderParser(t *testing.T) {
shouldErr("NATS/1.0\r\n")
shouldErr("NATS/1.0\r\nk1:v1")
shouldErr("NATS/1.0\r\nk1:v1\r\n")

// Check that we can do inline status and descriptions
checkStatus := func(hdr string, status int, description string) {
t.Helper()
hdrs, err := decodeHeadersMsg([]byte(hdr + "\r\n\r\n"))
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
if code, err := strconv.Atoi(hdrs.Get(statusHdr)); err != nil || code != status {
t.Fatalf("Expected status of %d, got %s", status, hdrs.Get(statusHdr))
}
}

checkStatus("NATS/1.0 503", 503, "")
checkStatus("NATS/1.0 503 No Responders", 503, "No Responders")
checkStatus("NATS/1.0 404 No Messages", 404, "No Messages")
}

func TestLameDuckMode(t *testing.T) {
Expand Down

0 comments on commit 6d9dbe3

Please sign in to comment.