Skip to content

Commit

Permalink
fix mput binary param parse
Browse files Browse the repository at this point in the history
  • Loading branch information
andyxning committed Aug 4, 2017
1 parent 6a237f3 commit 28389b0
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 7 deletions.
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

0 comments on commit 28389b0

Please sign in to comment.