Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

nsqd: fix mpub binary param parsing #921

Merged
merged 1 commit into from
Aug 4, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 34 additions & 7 deletions nsqd/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@ import (
"github.com/nsqio/nsq/internal/version"
)

var boolParams = map[string]bool{
"true": true,
"1": true,
"false": false,
"0": false,
}

type httpServer struct {
ctx *context
tlsEnabled bool
Expand Down Expand Up @@ -241,15 +248,35 @@ func (s *httpServer) doMPUB(w http.ResponseWriter, req *http.Request, ps httprou
return nil, err
}

_, ok := reqParams["binary"]
// if `binary` param is not specified, `text` mode is used.
//
// if `binary` param is specified:
// 1. "true" or "1", use `binary` mode
// 2. "false" or "0", use `text` mode
// 3. All other **empty** or **non-empty** values will be logged as deprecated
// and will be parsed as `true`, i.e., `binary` mode
//
vals, ok := reqParams["binary"]

binaryMode := false
if ok {
tmp := make([]byte, 4)
msgs, err = readMPUB(req.Body, tmp, topic,
s.ctx.nsqd.getOpts().MaxMsgSize, s.ctx.nsqd.getOpts().MaxBodySize)
if err != nil {
return nil, http_api.Err{413, err.(*protocol.FatalClientErr).Code[2:]}
var exists bool
if binaryMode, exists = boolParams[vals[0]]; !exists {
binaryMode = true
s.ctx.nsqd.logf(LOG_WARN, "deprecated value '%s' used for /mpub binary param", vals[0])
}
} else {

if binaryMode {
tmp := make([]byte, 4)
msgs, err = readMPUB(req.Body, tmp, topic,
s.ctx.nsqd.getOpts().MaxMsgSize, s.ctx.nsqd.getOpts().MaxBodySize)
if err != nil {
return nil, http_api.Err{413, err.(*protocol.FatalClientErr).Code[2:]}
}
}
}

if !binaryMode {
// add 1 so that it's greater than our max when we test for it
// (LimitReader returns a "fake" EOF)
readMax := s.ctx.nsqd.getOpts().MaxBodySize + 1
Expand Down
29 changes: 29 additions & 0 deletions nsqd/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,35 @@ func TestHTTPmpubBinary(t *testing.T) {
test.Equal(t, int64(5), topic.Depth())
}

func TestHTTPmpubForNonNormalizedBinaryParam(t *testing.T) {
opts := NewOptions()
opts.Logger = test.NewTestLogger(t)
_, httpAddr, nsqd := mustStartNSQD(opts)
defer os.RemoveAll(opts.DataPath)
defer nsqd.Exit()

topicName := "test_http_mpub_bin" + strconv.Itoa(int(time.Now().Unix()))
topic := nsqd.GetTopic(topicName)

mpub := make([][]byte, 5)
for i := range mpub {
mpub[i] = make([]byte, 100)
}
cmd, _ := nsq.MultiPublish(topicName, mpub)
buf := bytes.NewBuffer(cmd.Body)

url := fmt.Sprintf("http://%s/mpub?topic=%s&binary=non_normalized_binary_param", httpAddr, topicName)
resp, err := http.Post(url, "application/octet-stream", buf)
test.Nil(t, err)
defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body)
test.Equal(t, "OK", string(body))

time.Sleep(5 * time.Millisecond)

test.Equal(t, int64(5), topic.Depth())
}

func TestHTTPpubDefer(t *testing.T) {
opts := NewOptions()
opts.Logger = test.NewTestLogger(t)
Expand Down