Skip to content

Commit

Permalink
libflux/request: Rearch flux_request_decode
Browse files Browse the repository at this point in the history
Rearchitect flux_request_decode.  User can now pass in a NULL
or non-NULL json_str pointer.  The function will succeed regardless
if a payload is available or not.  The function will no longer return
EPROTO under a payload & pointer mismatch.  User is responsible to
check for NULL or non-NULL per their expectations.

Fixes flux-framework#495
  • Loading branch information
chu11 committed Mar 17, 2017
1 parent 997cd05 commit a7c3dd9
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 15 deletions.
7 changes: 3 additions & 4 deletions doc/man3/flux_request_decode.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,9 @@ DESCRIPTION
_topic_, if non-NULL, will be set the message's topic string. The storage
for this string belongs to _msg_ and should not be freed.
_json_str_, if non-NULL, will be set to the message's JSON payload. The
storage for this string belongs to _msg_ and should not be freed.
If non-NULL, decoding fails if the message doesn't have a JSON payload.
If NULL, decoding fails if the message does have a JSON payload.
_json_str_, if non-NULL, will be set to the message's JSON payload.
If no payload exists, _json_str_ is set to NULL. The storage for this
string belongs to _msg_ and should not be freed.
`flux_request_decodef()` decodes a request message with a JSON payload as
above, parsing the payload using variable arguments with a format string
Expand Down
4 changes: 0 additions & 4 deletions src/common/libflux/request.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,6 @@ int flux_request_decode (const flux_msg_t *msg, const char **topic,
goto done;
if (flux_msg_get_json (msg, &js) < 0)
goto done;
if ((json_str && !js) || (!json_str && js)) {
errno = EPROTO;
goto done;
}
if (topic)
*topic = ts;
if (json_str)
Expand Down
5 changes: 2 additions & 3 deletions src/common/libflux/request.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@

/* Decode a request message with optional json payload.
* If topic is non-NULL, assign the request topic string.
* If json_str is non-NULL, assign the payload. This argument indicates whether
* payload is expected and it is an EPROTO error if expectations are not met.
* Returns 0 on success, or -1 on failure with errno set.
* If json_str is non-NULL, assign the payload or set to NULL if none
* exists. Returns 0 on success, or -1 on failure with errno set.
*/
int flux_request_decode (const flux_msg_t *msg, const char **topic,
const char **json_str);
Expand Down
8 changes: 4 additions & 4 deletions src/common/libflux/test/request.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ int main (int argc, char *argv[])
ok (flux_request_decode (msg, NULL, NULL) == 0,
"flux_request_decode topic is optional");
errno = 0;
ok (flux_request_decode (msg, NULL, &s) < 0 && errno == EPROTO,
"flux_request_decode returns EPROTO when expected payload is missing");
ok (flux_request_decode (msg, NULL, &s) == 0 && s == NULL,
"flux_request_decode returns s = NULL when expected payload is missing");
flux_msg_destroy(msg);

/* with JSON payload */
Expand All @@ -54,8 +54,8 @@ int main (int argc, char *argv[])
"flux_request_decodef returns encoded payload");

errno = 0;
ok (flux_request_decode (msg, NULL, NULL) < 0 && errno == EPROTO,
"flux_request_decode returns EPROTO when payload is unexpected");
ok (flux_request_decode (msg, NULL, NULL) == 0,
"flux_request_decode works with payload but don't want the payload");
flux_msg_destroy(msg);

/* without payload (raw) */
Expand Down

0 comments on commit a7c3dd9

Please sign in to comment.