mirror of
https://github.com/jorisvink/kore
synced 2025-03-09 12:39:01 -04:00
JSONRPC Reverted explicit deallocation calls
The possibility to call jsonrpc_destroy_request was left. Someone may want to abruptly interrupt the process of its request for some odd reason, in that case an exlicit call still would be to be made.
This commit is contained in:
parent
7a4e4223c4
commit
32ac27d4c3
@ -18,7 +18,7 @@ Run:
|
||||
Test:
|
||||
```
|
||||
$ curl -i -k \
|
||||
-d '{"jsonrpc":"2.0","method":"echo","params":"Hello world"}' \
|
||||
-d '{"id":1,"jsonrpc":"2.0","method":"echo","params":["Hello world"]}' \
|
||||
https://127.0.0.1:8888/v1
|
||||
```
|
||||
The result should echo back the string at `params`: Hello world.
|
||||
@ -33,6 +33,20 @@ Will run a small test suite.
|
||||
The yajl repo is available @ https://github.com/lloyd/yajl
|
||||
|
||||
|
||||
JSONRPC Request Lifetime
|
||||
------------------------
|
||||
|
||||
Currently, one HTTP request will (in most cases) provoke one and only one
|
||||
response. Batch mode is not supported yet, neither is websocket.
|
||||
|
||||
As such `jsonrpc\_error` and `jsonrpc\_result` do clean the request after call.
|
||||
|
||||
If however you want to abort the processed request, like by returning
|
||||
`KORE\_RESULT\_ERROR`, after it having been read, you need to clean it by
|
||||
calling `jsonrpc\_destroy\_request`. Other than that you shouldn't think about
|
||||
this function.
|
||||
|
||||
|
||||
Message Handling Log
|
||||
--------------------
|
||||
|
||||
|
@ -6,9 +6,9 @@ int homepage(struct http_request *);
|
||||
int
|
||||
homepage(struct http_request *req)
|
||||
{
|
||||
static char response_body[] = "JSON-RPC API\n";
|
||||
static const char response_body[] = "JSON-RPC API\n";
|
||||
|
||||
http_response_header(req, "content-type", "text/plain");
|
||||
http_response(req, 200, (void*)response_body, sizeof(response_body) - 1);
|
||||
http_response(req, 200, response_body, sizeof(response_body) - 1);
|
||||
return (KORE_RESULT_OK);
|
||||
}
|
||||
|
@ -37,14 +37,6 @@ write_string_array_params(struct jsonrpc_request *req, void *ctx)
|
||||
return status;
|
||||
}
|
||||
|
||||
static int
|
||||
error(struct jsonrpc_request *req, int code, const char *name)
|
||||
{
|
||||
jsonrpc_error(req, code, name);
|
||||
jsonrpc_destroy_request(req);
|
||||
return (KORE_RESULT_OK);
|
||||
}
|
||||
|
||||
int
|
||||
v1(struct http_request *http_req)
|
||||
{
|
||||
@ -61,26 +53,25 @@ v1(struct http_request *http_req)
|
||||
|
||||
/* Read JSON-RPC request. */
|
||||
if ((ret = jsonrpc_read_request(http_req, &req)) != 0)
|
||||
return error(&req, ret, NULL);
|
||||
return jsonrpc_error(&req, ret, NULL);
|
||||
|
||||
/* Echo command takes and gives back params. */
|
||||
if (strcmp(req.method, "echo") == 0) {
|
||||
if (!YAJL_IS_ARRAY(req.params)) {
|
||||
jsonrpc_log(&req, LOG_ERR,
|
||||
"Echo only accepts array params");
|
||||
return error(&req, JSONRPC_INVALID_PARAMS, NULL);
|
||||
"Echo only accepts positional params");
|
||||
return jsonrpc_error(&req, JSONRPC_INVALID_PARAMS, NULL);
|
||||
}
|
||||
for (size_t i = 0; i < req.params->u.array.len; i++) {
|
||||
yajl_val v = req.params->u.array.values[i];
|
||||
if (!YAJL_IS_STRING(v)) {
|
||||
jsonrpc_log(&req, -3,
|
||||
"Echo only accepts strings");
|
||||
return error(&req, JSONRPC_INVALID_PARAMS, NULL);
|
||||
return jsonrpc_error(&req,
|
||||
JSONRPC_INVALID_PARAMS, NULL);
|
||||
}
|
||||
}
|
||||
jsonrpc_result(&req, write_string_array_params, NULL);
|
||||
jsonrpc_destroy_request(&req);
|
||||
return (KORE_RESULT_OK);
|
||||
return jsonrpc_result(&req, write_string_array_params, NULL);
|
||||
}
|
||||
|
||||
/* Date command displays date and time according to parameters. */
|
||||
@ -88,25 +79,38 @@ v1(struct http_request *http_req)
|
||||
time_t time_value;
|
||||
struct tm time_info;
|
||||
char timestamp[33];
|
||||
char *args[2] = {NULL, NULL};
|
||||
struct tm *(*gettm)(const time_t *, struct tm *) =
|
||||
localtime_r;
|
||||
|
||||
if (YAJL_IS_OBJECT(req.params)) {
|
||||
const char *path[] = {"local", NULL};
|
||||
yajl_val bf;
|
||||
|
||||
bf = yajl_tree_get(req.params, path, yajl_t_false);
|
||||
if (bf != NULL)
|
||||
gettm = gmtime_r;
|
||||
} else if (req.params != NULL) {
|
||||
jsonrpc_log(&req, LOG_ERR,
|
||||
"Date only accepts named params");
|
||||
return jsonrpc_error(&req, JSONRPC_INVALID_PARAMS, NULL);
|
||||
}
|
||||
|
||||
if ((time_value = time(NULL)) == -1)
|
||||
return error(&req, -2, "Failed to get date time");
|
||||
return jsonrpc_error(&req, -2,
|
||||
"Failed to get date time");
|
||||
|
||||
//gmtime_r(time_value, &time_info);
|
||||
if (localtime_r(&time_value, &time_info) == NULL)
|
||||
return error(&req, -3, "Failed to get date time info");
|
||||
if (gettm(&time_value, &time_info) == NULL)
|
||||
return jsonrpc_error(&req, -3,
|
||||
"Failed to get date time info");
|
||||
|
||||
memset(timestamp, 0, sizeof(timestamp));
|
||||
if (strftime_l(timestamp, sizeof(timestamp) - 1, "%c",
|
||||
&time_info, LC_GLOBAL_LOCALE) == 0)
|
||||
return error(&req, -4,
|
||||
return jsonrpc_error(&req, -4,
|
||||
"Failed to get printable date time");
|
||||
|
||||
jsonrpc_result(&req, write_string, timestamp);
|
||||
jsonrpc_destroy_request(&req);
|
||||
return (KORE_RESULT_OK);
|
||||
return jsonrpc_result(&req, write_string, timestamp);
|
||||
}
|
||||
|
||||
return error(&req, JSONRPC_METHOD_NOT_FOUND, NULL);
|
||||
return jsonrpc_error(&req, JSONRPC_METHOD_NOT_FOUND, NULL);
|
||||
}
|
||||
|
@ -426,9 +426,11 @@ succeeded:
|
||||
http_response(req->http, 200, body, body_len);
|
||||
if (req->gen != NULL)
|
||||
yajl_gen_clear(req->gen);
|
||||
jsonrpc_destroy_request(req);
|
||||
return (KORE_RESULT_OK);
|
||||
failed:
|
||||
http_response(req->http, 500, NULL, 0);
|
||||
jsonrpc_destroy_request(req);
|
||||
return (KORE_RESULT_OK);
|
||||
}
|
||||
|
||||
@ -467,8 +469,10 @@ succeeded:
|
||||
http_response(req->http, 200, body, body_len);
|
||||
if (req->gen != NULL)
|
||||
yajl_gen_clear(req->gen);
|
||||
jsonrpc_destroy_request(req);
|
||||
return (KORE_RESULT_OK);
|
||||
failed:
|
||||
http_response(req->http, 500, NULL, 0);
|
||||
jsonrpc_destroy_request(req);
|
||||
return (KORE_RESULT_OK);
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user