Skip to content

Commit 48f56be

Browse files
author
Greg Passmore
committed
Fixing direct method request id parsing issue
1 parent 1a470ba commit 48f56be

2 files changed

Lines changed: 8 additions & 11 deletions

File tree

iotdevice/transport/mqtt/mqtt.go

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ func (tr *Transport) subDirectMethods(ctx context.Context, mux transport.MethodD
304304
tr.logger.Errorf("dispatch error: %s", err)
305305
return
306306
}
307-
dst := fmt.Sprintf("$iothub/methods/res/%d/?$rid=%d", rc, rid)
307+
dst := fmt.Sprintf("$iothub/methods/res/%d/?$rid=%s", rc, rid)
308308
if err = tr.send(ctx, dst, DefaultQoS, b); err != nil {
309309
tr.logger.Errorf("method response error: %s", err)
310310
return
@@ -316,31 +316,28 @@ func (tr *Transport) subDirectMethods(ctx context.Context, mux transport.MethodD
316316

317317
// returns method name and rid
318318
// format: $iothub/methods/POST/{method}/?$rid={rid}
319-
func parseDirectMethodTopic(s string) (string, int, error) {
319+
func parseDirectMethodTopic(s string) (string, string, error) {
320320
const prefix = "$iothub/methods/POST/"
321321

322322
s, err := url.QueryUnescape(s)
323323
if err != nil {
324-
return "", 0, err
324+
return "", "", err
325325
}
326326
u, err := url.Parse(s)
327327
if err != nil {
328-
return "", 0, err
328+
return "", "", err
329329
}
330330

331331
p := strings.TrimRight(u.Path, "/")
332332
if !strings.HasPrefix(p, prefix) {
333-
return "", 0, errors.New("malformed direct method topic")
333+
return "", "", errors.New("malformed direct method topic")
334334
}
335335

336336
q := u.Query()
337337
if len(q["$rid"]) != 1 {
338-
return "", 0, errors.New("$rid is not available")
339-
}
340-
rid, err := strconv.Atoi(q["$rid"][0])
341-
if err != nil {
342-
return "", 0, fmt.Errorf("$rid parse error: %s", err)
338+
return "", "", errors.New("$rid is not available")
343339
}
340+
rid := q["$rid"][0]
344341
return p[len(prefix):], rid, nil
345342
}
346343

iotdevice/transport/mqtt/mqtt_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ func TestParseDirectMethodTopic(t *testing.T) {
2828
if err != nil {
2929
t.Fatal(err)
3030
}
31-
if m != "add" || r != 666 {
31+
if m != "add" || r != "666" {
3232
t.Errorf("parseDirectMethodTopic(%q) = %q, %q, want %q, %q", s, m, r, "add", 666)
3333
}
3434
}

0 commit comments

Comments
 (0)