From ec019a71e665764c7f5f69737b18f4bf85e84d36 Mon Sep 17 00:00:00 2001 From: eeff <33566201+eeff@users.noreply.github.com> Date: Wed, 7 Dec 2022 18:17:54 +0800 Subject: [PATCH] feat: put `utils/http.h` functions in namespace `neu` --- include/neuron/utils/http.h | 41 ++++++------ plugins/monitor/metric_handle.c | 5 +- plugins/monitor/metric_handle.h | 16 ----- plugins/restful/adapter_handle.c | 44 ++++++------- plugins/restful/datatag_handle.c | 22 +++---- plugins/restful/group_config_handle.c | 30 ++++----- plugins/restful/handle.h | 89 ++++++++++++++------------- plugins/restful/log_handle.c | 18 +++--- plugins/restful/normal_handle.c | 25 ++++---- plugins/restful/plugin_handle.c | 8 +-- plugins/restful/proxy.c | 2 +- plugins/restful/rest.c | 2 +- plugins/restful/rw_handle.c | 6 +- plugins/restful/version_handle.c | 4 +- src/utils/http.c | 52 ++++++++-------- tests/http_test.cc | 10 +-- 16 files changed, 182 insertions(+), 192 deletions(-) diff --git a/include/neuron/utils/http.h b/include/neuron/utils/http.h index 0cf68425..57308c8a 100644 --- a/include/neuron/utils/http.h +++ b/include/neuron/utils/http.h @@ -33,9 +33,9 @@ extern "C" { // Return -1 if decode fails, otherwise return the number of bytes written to // `buf` excluding the terminating NULL byte. A return value of `size` indicates // buffer overflow. -ssize_t url_decode(const char *s, size_t len, char *buf, size_t size); +ssize_t neu_url_decode(const char *s, size_t len, char *buf, size_t size); -int http_get_body(nng_aio *aio, void **data, size_t *data_size); +int neu_http_get_body(nng_aio *aio, void **data, size_t *data_size); // Find query parameter value of the given name. // @@ -51,35 +51,36 @@ int http_get_body(nng_aio *aio, void **data, size_t *data_size); // and store 0 in len. // 3. http_get_param("/?foo=bar", "key", &len) will return NULL and will not // touch len. -const char *http_get_param(nng_aio *aio, const char *name, size_t *len); +const char *neu_http_get_param(nng_aio *aio, const char *name, size_t *len); // Get query parameter as url decoded string. // Return -2 if param does not exist, -1 if url decode fail, otherwise return // the number of bytes written to `buf` excluding the terminating NULL byte. A // return value of `size` indicates buffer overflow. -ssize_t http_get_param_str(nng_aio *aio, const char *name, char *buf, - size_t size); +ssize_t neu_http_get_param_str(nng_aio *aio, const char *name, char *buf, + size_t size); // Returns 0 on success. // On failure returns // NEU_ERR_ENOENT if no query parameter named `name`. // NEU_ERR_EINVAL if the query parameter value is not valid. -int http_get_param_uintmax(nng_aio *aio, const char *name, uintmax_t *param); -int http_get_param_uint32(nng_aio *aio, const char *name, uint32_t *param); -int http_get_param_uint64(nng_aio *aio, const char *name, uint64_t *param); -int http_get_param_node_type(nng_aio *aio, const char *name, - neu_node_type_e *param); +int neu_http_get_param_uintmax(nng_aio *aio, const char *name, + uintmax_t *param); +int neu_http_get_param_uint32(nng_aio *aio, const char *name, uint32_t *param); +int neu_http_get_param_uint64(nng_aio *aio, const char *name, uint64_t *param); +int neu_http_get_param_node_type(nng_aio *aio, const char *name, + neu_node_type_e *param); -int http_response(nng_aio *aio, neu_err_code_e code, char *content); -const char *http_get_header(nng_aio *aio, char *name); -int http_ok(nng_aio *aio, char *content); -int http_created(nng_aio *aio, char *content); -int http_partial(nng_aio *aio, char *content); -int http_bad_request(nng_aio *aio, char *content); -int http_unauthorized(nng_aio *aio, char *content); -int http_not_found(nng_aio *aio, char *content); -int http_conflict(nng_aio *aio, char *content); -int http_internal_error(nng_aio *aio, char *content); +int neu_http_response(nng_aio *aio, neu_err_code_e code, char *content); +const char *neu_http_get_header(nng_aio *aio, char *name); +int neu_http_ok(nng_aio *aio, char *content); +int neu_http_created(nng_aio *aio, char *content); +int neu_http_partial(nng_aio *aio, char *content); +int neu_http_bad_request(nng_aio *aio, char *content); +int neu_http_unauthorized(nng_aio *aio, char *content); +int neu_http_not_found(nng_aio *aio, char *content); +int neu_http_conflict(nng_aio *aio, char *content); +int neu_http_internal_error(nng_aio *aio, char *content); #ifdef __cplusplus } diff --git a/plugins/monitor/metric_handle.c b/plugins/monitor/metric_handle.c index 560c01e0..997f1518 100644 --- a/plugins/monitor/metric_handle.c +++ b/plugins/monitor/metric_handle.c @@ -245,7 +245,7 @@ void handle_get_metric(nng_aio *aio) VALIDATE_JWT(aio); neu_metrics_category_e cat = NEU_METRICS_CATEGORY_ALL; size_t cat_param_len = 0; - const char *cat_param = http_get_param(aio, "category", &cat_param_len); + const char *cat_param = neu_http_get_param(aio, "category", &cat_param_len); if (NULL != cat_param && !parse_metrics_catgory(cat_param, cat_param_len, &cat)) { plog_info(plugin, "invalid metrics category: %.*s", (int) cat_param_len, @@ -255,7 +255,8 @@ void handle_get_metric(nng_aio *aio) } char node_name[NEU_NODE_NAME_LEN] = { 0 }; - ssize_t rv = http_get_param_str(aio, "node", node_name, sizeof(node_name)); + ssize_t rv = + neu_http_get_param_str(aio, "node", node_name, sizeof(node_name)); if (-1 == rv || rv >= NEU_NODE_NAME_LEN || (0 < rv && NEU_METRICS_CATEGORY_GLOBAL == cat)) { status = NNG_HTTP_STATUS_BAD_REQUEST; diff --git a/plugins/monitor/metric_handle.h b/plugins/monitor/metric_handle.h index 94dd4e25..dcad7739 100644 --- a/plugins/monitor/metric_handle.h +++ b/plugins/monitor/metric_handle.h @@ -24,22 +24,6 @@ #include "json/neu_json_fn.h" -#define VALIDATE_JWT(aio) \ - { \ - if (!disable_jwt) { \ - char *jwt = \ - (char *) http_get_header(aio, (char *) "Authorization"); \ - \ - NEU_JSON_RESPONSE_ERROR(neu_jwt_validate(jwt), { \ - if (error_code.error != NEU_ERR_SUCCESS) { \ - http_response(aio, error_code.error, result_error); \ - free(result_error); \ - return; \ - } \ - }); \ - } \ - } - void handle_get_metric(nng_aio *aio); #endif diff --git a/plugins/restful/adapter_handle.c b/plugins/restful/adapter_handle.c index 8289ee77..612461c6 100644 --- a/plugins/restful/adapter_handle.c +++ b/plugins/restful/adapter_handle.c @@ -39,8 +39,8 @@ void handle_add_adapter(nng_aio *aio) aio, neu_json_add_node_req_t, neu_json_decode_add_node_req, { if (strlen(req->name) >= NEU_NODE_NAME_LEN) { NEU_JSON_RESPONSE_ERROR(NEU_ERR_NODE_NAME_TOO_LONG, { - http_response(aio, NEU_ERR_NODE_NAME_TOO_LONG, - result_error); + neu_http_response(aio, NEU_ERR_NODE_NAME_TOO_LONG, + result_error); }); } else { @@ -55,7 +55,7 @@ void handle_add_adapter(nng_aio *aio) ret = neu_plugin_op(plugin, header, &cmd); if (ret != 0) { NEU_JSON_RESPONSE_ERROR(NEU_ERR_IS_BUSY, { - http_response(aio, NEU_ERR_IS_BUSY, result_error); + neu_http_response(aio, NEU_ERR_IS_BUSY, result_error); }); } } @@ -78,7 +78,7 @@ void handle_del_adapter(nng_aio *aio) ret = neu_plugin_op(plugin, header, &cmd); if (ret != 0) { NEU_JSON_RESPONSE_ERROR(NEU_ERR_IS_BUSY, { - http_response(aio, NEU_ERR_IS_BUSY, result_error); + neu_http_response(aio, NEU_ERR_IS_BUSY, result_error); }); } }) @@ -100,19 +100,19 @@ void handle_get_adapter(nng_aio *aio) VALIDATE_JWT(aio); - if (http_get_param_node_type(aio, "type", &node_type) != 0) { + if (neu_http_get_param_node_type(aio, "type", &node_type) != 0) { NEU_JSON_RESPONSE_ERROR(NEU_ERR_PARAM_IS_WRONG, { - http_response(aio, error_code.error, result_error); + neu_http_response(aio, error_code.error, result_error); }) return; } - if (http_get_param_str(aio, "plugin", plugin_name, sizeof(plugin_name)) > - 0) { + if (neu_http_get_param_str(aio, "plugin", plugin_name, + sizeof(plugin_name)) > 0) { strcpy(cmd.plugin, plugin_name); } - if (http_get_param_str(aio, "node", node_name, sizeof(node_name)) > 0) { + if (neu_http_get_param_str(aio, "node", node_name, sizeof(node_name)) > 0) { strcpy(cmd.node, node_name); } @@ -120,7 +120,7 @@ void handle_get_adapter(nng_aio *aio) ret = neu_plugin_op(plugin, header, &cmd); if (ret != 0) { NEU_JSON_RESPONSE_ERROR(NEU_ERR_IS_BUSY, { - http_response(aio, NEU_ERR_IS_BUSY, result_error); + neu_http_response(aio, NEU_ERR_IS_BUSY, result_error); }); } } @@ -144,7 +144,7 @@ void handle_get_adapter_resp(nng_aio *aio, neu_resp_get_node_t *nodes) neu_json_encode_by_fn(&nodes_res, neu_json_encode_get_nodes_resp, &result); - http_ok(aio, result); + neu_http_ok(aio, result); free(result); free(nodes_res.nodes); @@ -170,7 +170,7 @@ void handle_set_node_setting(nng_aio *aio) ret = neu_plugin_op(plugin, header, &cmd); if (ret != 0) { NEU_JSON_RESPONSE_ERROR(NEU_ERR_IS_BUSY, { - http_response(aio, NEU_ERR_IS_BUSY, result_error); + neu_http_response(aio, NEU_ERR_IS_BUSY, result_error); }); } }) @@ -186,9 +186,10 @@ void handle_get_node_setting(nng_aio *aio) VALIDATE_JWT(aio); - if (http_get_param_str(aio, "node", node_name, sizeof(node_name)) <= 0) { + if (neu_http_get_param_str(aio, "node", node_name, sizeof(node_name)) <= + 0) { NEU_JSON_RESPONSE_ERROR(NEU_ERR_PARAM_IS_WRONG, { - http_response(aio, error_code.error, result_error); + neu_http_response(aio, error_code.error, result_error); }) return; } @@ -200,7 +201,7 @@ void handle_get_node_setting(nng_aio *aio) ret = neu_plugin_op(plugin, header, &cmd); if (ret != 0) { NEU_JSON_RESPONSE_ERROR(NEU_ERR_IS_BUSY, { - http_response(aio, NEU_ERR_IS_BUSY, result_error); + neu_http_response(aio, NEU_ERR_IS_BUSY, result_error); }); } } @@ -208,7 +209,7 @@ void handle_get_node_setting(nng_aio *aio) void handle_get_node_setting_resp(nng_aio * aio, neu_resp_get_node_setting_t *setting) { - http_ok(aio, setting->setting); + neu_http_ok(aio, setting->setting); free(setting->setting); } @@ -230,7 +231,7 @@ void handle_node_ctl(nng_aio *aio) ret = neu_plugin_op(plugin, header, &cmd); if (ret != 0) { NEU_JSON_RESPONSE_ERROR(NEU_ERR_IS_BUSY, { - http_response(aio, NEU_ERR_IS_BUSY, result_error); + neu_http_response(aio, NEU_ERR_IS_BUSY, result_error); }); } }) @@ -248,7 +249,8 @@ void handle_get_node_state(nng_aio *aio) header.ctx = aio; - if (http_get_param_str(aio, "node", node_name, sizeof(node_name)) <= 0) { + if (neu_http_get_param_str(aio, "node", node_name, sizeof(node_name)) <= + 0) { header.type = NEU_REQ_GET_NODES_STATE; } else { header.type = NEU_REQ_GET_NODE_STATE; @@ -258,7 +260,7 @@ void handle_get_node_state(nng_aio *aio) ret = neu_plugin_op(plugin, header, &cmd); if (ret != 0) { NEU_JSON_RESPONSE_ERROR(NEU_ERR_IS_BUSY, { - http_response(aio, NEU_ERR_IS_BUSY, result_error); + neu_http_response(aio, NEU_ERR_IS_BUSY, result_error); }); } } @@ -274,7 +276,7 @@ void handle_get_node_state_resp(nng_aio *aio, neu_resp_get_node_state_t *state) neu_json_encode_by_fn(&res, neu_json_encode_get_node_state_resp, &result); - http_ok(aio, result); + neu_http_ok(aio, result); free(result); } @@ -300,7 +302,7 @@ void handle_get_nodes_state_resp(nng_aio * aio, neu_json_encode_by_fn(&states_res, neu_json_encode_get_nodes_state_resp, &result); - http_ok(aio, result); + neu_http_ok(aio, result); free(result); free(states_res.nodes); diff --git a/plugins/restful/datatag_handle.c b/plugins/restful/datatag_handle.c index 1eeea056..29d9df74 100644 --- a/plugins/restful/datatag_handle.c +++ b/plugins/restful/datatag_handle.c @@ -64,7 +64,7 @@ void handle_add_tags(nng_aio *aio) ret = neu_plugin_op(plugin, header, &cmd); if (ret != 0) { NEU_JSON_RESPONSE_ERROR(NEU_ERR_IS_BUSY, { - http_response(aio, NEU_ERR_IS_BUSY, result_error); + neu_http_response(aio, NEU_ERR_IS_BUSY, result_error); }); } }) @@ -80,7 +80,7 @@ void handle_add_tags_resp(nng_aio *aio, neu_resp_add_tag_t *resp) neu_json_encode_by_fn(&res, neu_json_encode_au_tags_resp, &result); - http_ok(aio, result); + neu_http_ok(aio, result); free(result); } @@ -108,7 +108,7 @@ void handle_del_tags(nng_aio *aio) ret = neu_plugin_op(plugin, header, &cmd); if (ret != 0) { NEU_JSON_RESPONSE_ERROR(NEU_ERR_IS_BUSY, { - http_response(aio, NEU_ERR_IS_BUSY, result_error); + neu_http_response(aio, NEU_ERR_IS_BUSY, result_error); }); } }) @@ -148,7 +148,7 @@ void handle_update_tags(nng_aio *aio) ret = neu_plugin_op(plugin, header, &cmd); if (ret != 0) { NEU_JSON_RESPONSE_ERROR(NEU_ERR_IS_BUSY, { - http_response(aio, NEU_ERR_IS_BUSY, result_error); + neu_http_response(aio, NEU_ERR_IS_BUSY, result_error); }); } }) @@ -174,20 +174,20 @@ void handle_get_tags(nng_aio *aio) VALIDATE_JWT(aio); - if (http_get_param_str(aio, "node", node, sizeof(node)) <= 0) { + if (neu_http_get_param_str(aio, "node", node, sizeof(node)) <= 0) { NEU_JSON_RESPONSE_ERROR(NEU_ERR_PARAM_IS_WRONG, { - http_response(aio, NEU_ERR_PARAM_IS_WRONG, result_error); + neu_http_response(aio, NEU_ERR_PARAM_IS_WRONG, result_error); }) return; } - if (http_get_param_str(aio, "group", group, sizeof(group)) <= 0) { + if (neu_http_get_param_str(aio, "group", group, sizeof(group)) <= 0) { NEU_JSON_RESPONSE_ERROR(NEU_ERR_PARAM_IS_WRONG, { - http_response(aio, NEU_ERR_PARAM_IS_WRONG, result_error); + neu_http_response(aio, NEU_ERR_PARAM_IS_WRONG, result_error); }) return; } - if (http_get_param_str(aio, "name", tag_name, sizeof(tag_name)) >= 0) { + if (neu_http_get_param_str(aio, "name", tag_name, sizeof(tag_name)) >= 0) { strcpy(cmd.name, tag_name); } @@ -197,7 +197,7 @@ void handle_get_tags(nng_aio *aio) ret = neu_plugin_op(plugin, header, &cmd); if (ret != 0) { NEU_JSON_RESPONSE_ERROR(NEU_ERR_IS_BUSY, { - http_response(aio, NEU_ERR_IS_BUSY, result_error); + neu_http_response(aio, NEU_ERR_IS_BUSY, result_error); }); } } @@ -226,7 +226,7 @@ void handle_get_tags_resp(nng_aio *aio, neu_resp_get_tag_t *tags) neu_json_encode_by_fn(&tags_res, neu_json_encode_get_tags_resp, &result); - http_ok(aio, result); + neu_http_ok(aio, result); free(result); free(tags_res.tags); diff --git a/plugins/restful/group_config_handle.c b/plugins/restful/group_config_handle.c index a2d14db2..8b4a8bd2 100644 --- a/plugins/restful/group_config_handle.c +++ b/plugins/restful/group_config_handle.c @@ -38,8 +38,8 @@ void handle_add_group_config(nng_aio *aio) neu_json_decode_add_group_config_req, { if (strlen(req->group) >= NEU_GROUP_NAME_LEN) { NEU_JSON_RESPONSE_ERROR(NEU_ERR_GROUP_NAME_TOO_LONG, { - http_response(aio, NEU_ERR_GROUP_NAME_TOO_LONG, - result_error); + neu_http_response(aio, NEU_ERR_GROUP_NAME_TOO_LONG, + result_error); }); } else { int ret = 0; @@ -54,7 +54,7 @@ void handle_add_group_config(nng_aio *aio) ret = neu_plugin_op(plugin, header, &cmd); if (ret != 0) { NEU_JSON_RESPONSE_ERROR(NEU_ERR_IS_BUSY, { - http_response(aio, NEU_ERR_IS_BUSY, result_error); + neu_http_response(aio, NEU_ERR_IS_BUSY, result_error); }); } } @@ -80,7 +80,7 @@ void handle_update_group(nng_aio *aio) ret = neu_plugin_op(plugin, header, &cmd); if (ret != 0) { NEU_JSON_RESPONSE_ERROR(NEU_ERR_IS_BUSY, { - http_response(aio, NEU_ERR_IS_BUSY, result_error); + neu_http_response(aio, NEU_ERR_IS_BUSY, result_error); }); } }) @@ -104,7 +104,7 @@ void handle_del_group_config(nng_aio *aio) ret = neu_plugin_op(plugin, header, &cmd); if (ret != 0) { NEU_JSON_RESPONSE_ERROR(NEU_ERR_IS_BUSY, { - http_response(aio, NEU_ERR_IS_BUSY, result_error); + neu_http_response(aio, NEU_ERR_IS_BUSY, result_error); }); } }) @@ -123,7 +123,7 @@ void handle_get_group_config(nng_aio *aio) VALIDATE_JWT(aio); - http_get_param_str(aio, "node", node_name, sizeof(node_name)); + neu_http_get_param_str(aio, "node", node_name, sizeof(node_name)); strcpy(cmd.driver, node_name); if (strlen(cmd.driver) == 0) { header.type = NEU_REQ_GET_DRIVER_GROUP; @@ -132,7 +132,7 @@ void handle_get_group_config(nng_aio *aio) ret = neu_plugin_op(plugin, header, &cmd); if (ret != 0) { NEU_JSON_RESPONSE_ERROR(NEU_ERR_IS_BUSY, { - http_response(aio, NEU_ERR_IS_BUSY, result_error); + neu_http_response(aio, NEU_ERR_IS_BUSY, result_error); }); } } @@ -158,7 +158,7 @@ void handle_get_group_resp(nng_aio *aio, neu_resp_get_group_t *groups) neu_json_encode_by_fn(&gconfig_res, neu_json_encode_get_group_config_resp, &result); - http_ok(aio, result); + neu_http_ok(aio, result); free(result); free(gconfig_res.group_configs); @@ -187,7 +187,7 @@ void handle_get_driver_group_resp(nng_aio * aio, neu_json_encode_by_fn(&gconfig_res, neu_json_encode_get_driver_group_resp, &result); - http_ok(aio, result); + neu_http_ok(aio, result); free(result); free(gconfig_res.groups); @@ -211,7 +211,7 @@ void handle_grp_subscribe(nng_aio *aio) ret = neu_plugin_op(plugin, header, &cmd); if (ret != 0) { NEU_JSON_RESPONSE_ERROR(NEU_ERR_IS_BUSY, { - http_response(aio, NEU_ERR_IS_BUSY, result_error); + neu_http_response(aio, NEU_ERR_IS_BUSY, result_error); }); } }) @@ -234,7 +234,7 @@ void handle_grp_unsubscribe(nng_aio *aio) ret = neu_plugin_op(plugin, header, &cmd); if (ret != 0) { NEU_JSON_RESPONSE_ERROR(NEU_ERR_IS_BUSY, { - http_response(aio, NEU_ERR_IS_BUSY, result_error); + neu_http_response(aio, NEU_ERR_IS_BUSY, result_error); }); } }) @@ -253,9 +253,9 @@ void handle_grp_get_subscribe(nng_aio *aio) VALIDATE_JWT(aio); - if (http_get_param_str(aio, "app", node_name, sizeof(node_name)) <= 0) { + if (neu_http_get_param_str(aio, "app", node_name, sizeof(node_name)) <= 0) { NEU_JSON_RESPONSE_ERROR(NEU_ERR_PARAM_IS_WRONG, { - http_response(aio, error_code.error, result_error); + neu_http_response(aio, error_code.error, result_error); }) return; } @@ -264,7 +264,7 @@ void handle_grp_get_subscribe(nng_aio *aio) ret = neu_plugin_op(plugin, header, &cmd); if (ret != 0) { NEU_JSON_RESPONSE_ERROR(NEU_ERR_IS_BUSY, { - http_response(aio, NEU_ERR_IS_BUSY, result_error); + neu_http_response(aio, NEU_ERR_IS_BUSY, result_error); }); } } @@ -289,7 +289,7 @@ void handle_grp_get_subscribe_resp(nng_aio * aio, neu_json_encode_by_fn(&sub_grp_configs, neu_json_encode_get_subscribe_resp, &result); - http_ok(aio, result); + neu_http_ok(aio, result); free(result); free(sub_grp_configs.groups); utarray_free(groups->groups); diff --git a/plugins/restful/handle.h b/plugins/restful/handle.h index 3c9ea5a6..d438a180 100644 --- a/plugins/restful/handle.h +++ b/plugins/restful/handle.h @@ -29,54 +29,55 @@ #include "utils/neu_jwt.h" #include "json/neu_json_error.h" -#define REST_PROCESS_HTTP_REQUEST(aio, req_type, decode_fun, func) \ - { \ - char * req_data = NULL; \ - size_t req_data_size = 0; \ - req_type *req = NULL; \ - \ - if (http_get_body((aio), (void **) &req_data, &req_data_size) == 0 && \ - decode_fun(req_data, &req) == 0) { \ - { func }; \ - decode_fun##_free(req); \ - } else { \ - http_bad_request(aio, "{\"error\": 1002}"); \ - } \ - free(req_data); \ +#define REST_PROCESS_HTTP_REQUEST(aio, req_type, decode_fun, func) \ + { \ + char * req_data = NULL; \ + size_t req_data_size = 0; \ + req_type *req = NULL; \ + \ + if (neu_http_get_body((aio), (void **) &req_data, &req_data_size) == \ + 0 && \ + decode_fun(req_data, &req) == 0) { \ + { func }; \ + decode_fun##_free(req); \ + } else { \ + neu_http_bad_request(aio, "{\"error\": 1002}"); \ + } \ + free(req_data); \ } -#define REST_PROCESS_HTTP_REQUEST_VALIDATE_JWT(aio, req_type, decode_fun, \ - func) \ - { \ - if (!disable_jwt) { \ - char *jwt = \ - (char *) http_get_header(aio, (char *) "Authorization"); \ - \ - NEU_JSON_RESPONSE_ERROR(neu_jwt_validate(jwt), { \ - if (error_code.error != NEU_ERR_SUCCESS) { \ - http_response(aio, error_code.error, result_error); \ - free(result_error); \ - return; \ - } \ - }) \ - } \ - REST_PROCESS_HTTP_REQUEST(aio, req_type, decode_fun, func); \ +#define REST_PROCESS_HTTP_REQUEST_VALIDATE_JWT(aio, req_type, decode_fun, \ + func) \ + { \ + if (!disable_jwt) { \ + char *jwt = \ + (char *) neu_http_get_header(aio, (char *) "Authorization"); \ + \ + NEU_JSON_RESPONSE_ERROR(neu_jwt_validate(jwt), { \ + if (error_code.error != NEU_ERR_SUCCESS) { \ + neu_http_response(aio, error_code.error, result_error); \ + free(result_error); \ + return; \ + } \ + }) \ + } \ + REST_PROCESS_HTTP_REQUEST(aio, req_type, decode_fun, func); \ } -#define VALIDATE_JWT(aio) \ - { \ - if (!disable_jwt) { \ - char *jwt = \ - (char *) http_get_header(aio, (char *) "Authorization"); \ - \ - NEU_JSON_RESPONSE_ERROR(neu_jwt_validate(jwt), { \ - if (error_code.error != NEU_ERR_SUCCESS) { \ - http_response(aio, error_code.error, result_error); \ - free(result_error); \ - return; \ - } \ - }); \ - } \ +#define VALIDATE_JWT(aio) \ + { \ + if (!disable_jwt) { \ + char *jwt = \ + (char *) neu_http_get_header(aio, (char *) "Authorization"); \ + \ + NEU_JSON_RESPONSE_ERROR(neu_jwt_validate(jwt), { \ + if (error_code.error != NEU_ERR_SUCCESS) { \ + neu_http_response(aio, error_code.error, result_error); \ + free(result_error); \ + return; \ + } \ + }); \ + } \ } enum neu_rest_method { diff --git a/plugins/restful/log_handle.c b/plugins/restful/log_handle.c index d74e1c60..a7704a6e 100644 --- a/plugins/restful/log_handle.c +++ b/plugins/restful/log_handle.c @@ -71,7 +71,7 @@ void handle_logs_files(nng_aio *aio) if (0 != rv) { nlog_error("failed to copy coredump file"); NEU_JSON_RESPONSE_ERROR(NEU_ERR_COMMAND_EXECUTION_FAILED, { - http_response(aio, error_code.error, result_error); + neu_http_response(aio, error_code.error, result_error); }); return; } @@ -81,7 +81,7 @@ void handle_logs_files(nng_aio *aio) if (0 != rv) { nlog_error("failed to copy logs file"); NEU_JSON_RESPONSE_ERROR(NEU_ERR_COMMAND_EXECUTION_FAILED, { - http_response(aio, error_code.error, result_error); + neu_http_response(aio, error_code.error, result_error); }); return; } @@ -90,7 +90,7 @@ void handle_logs_files(nng_aio *aio) if (0 != rv) { nlog_error("failed to create neuron_logs.tar.gz"); NEU_JSON_RESPONSE_ERROR(NEU_ERR_COMMAND_EXECUTION_FAILED, { - http_response(aio, error_code.error, result_error); + neu_http_response(aio, error_code.error, result_error); }); return; } @@ -99,7 +99,7 @@ void handle_logs_files(nng_aio *aio) rv = read_file(path, &data, &len); if (0 != rv) { NEU_JSON_RESPONSE_ERROR( - rv, { http_response(aio, error_code.error, result_error); }); + rv, { neu_http_response(aio, error_code.error, result_error); }); return; } @@ -108,7 +108,7 @@ void handle_logs_files(nng_aio *aio) if (0 != rv) { nlog_error("failed to delete neuron_logs folder"); NEU_JSON_RESPONSE_ERROR(NEU_ERR_COMMAND_EXECUTION_FAILED, { - http_response(aio, error_code.error, result_error); + neu_http_response(aio, error_code.error, result_error); }); return; } @@ -142,7 +142,7 @@ void handle_log_level(nng_aio *aio) if (strlen(node_name) == 0) { NEU_JSON_RESPONSE_ERROR(NEU_ERR_NODE_NOT_EXIST, { - http_response(aio, error_code.error, result_error); + neu_http_response(aio, error_code.error, result_error); }); } else { int ret = 0; @@ -157,7 +157,7 @@ void handle_log_level(nng_aio *aio) "Modify node log level fail, node_name:%s, ret: %d,", node_name, ret); NEU_JSON_RESPONSE_ERROR(NEU_ERR_EINTERNAL, { - http_response(aio, error_code.error, result_error); + neu_http_response(aio, error_code.error, result_error); }); } @@ -165,7 +165,7 @@ void handle_log_level(nng_aio *aio) if (ret != 0) { nlog_error("Modify neuron log level fail, ret: %d", ret); NEU_JSON_RESPONSE_ERROR(NEU_ERR_EINTERNAL, { - http_response(aio, error_code.error, result_error); + neu_http_response(aio, error_code.error, result_error); }); } @@ -176,7 +176,7 @@ void handle_log_level(nng_aio *aio) ret = neu_plugin_op(plugin, header, &cmd); if (ret != 0) { NEU_JSON_RESPONSE_ERROR(NEU_ERR_IS_BUSY, { - http_response(aio, NEU_ERR_IS_BUSY, result_error); + neu_http_response(aio, NEU_ERR_IS_BUSY, result_error); }); } } diff --git a/plugins/restful/normal_handle.c b/plugins/restful/normal_handle.c index c92756f7..54f37df7 100644 --- a/plugins/restful/normal_handle.c +++ b/plugins/restful/normal_handle.c @@ -45,7 +45,7 @@ void handle_ping(nng_aio *aio) { - http_ok(aio, "{}"); + neu_http_ok(aio, "{}"); } void handle_login(nng_aio *aio) @@ -58,7 +58,7 @@ void handle_login(nng_aio *aio) if (NULL == user) { nlog_warn("could not find user `%s`", req->name); NEU_JSON_RESPONSE_ERROR(NEU_ERR_INVALID_USER_OR_PASSWORD, { - http_response(aio, error_code.error, result_error); + neu_http_response(aio, error_code.error, result_error); }); } else if (neu_user_check_password(user, req->pass)) { @@ -68,7 +68,7 @@ void handle_login(nng_aio *aio) int ret = neu_jwt_new(&token); if (ret != 0) { NEU_JSON_RESPONSE_ERROR(NEU_ERR_NEED_TOKEN, { - http_response(aio, error_code.error, result_error); + neu_http_response(aio, error_code.error, result_error); jwt_free_str(token); }); } @@ -77,13 +77,13 @@ void handle_login(nng_aio *aio) neu_json_encode_by_fn(&login_resp, neu_json_encode_login_resp, &result); - http_ok(aio, result); + neu_http_ok(aio, result); jwt_free_str(token); free(result); } else { nlog_warn("user `%s` password check fail", req->name); NEU_JSON_RESPONSE_ERROR(NEU_ERR_INVALID_USER_OR_PASSWORD, { - http_response(aio, error_code.error, result_error); + neu_http_response(aio, error_code.error, result_error); }); } @@ -117,8 +117,9 @@ void handle_password(nng_aio *aio) nlog_error("user `%s` persist fail", req->name); } - NEU_JSON_RESPONSE_ERROR( - rv, { http_response(aio, error_code.error, result_error); }); + NEU_JSON_RESPONSE_ERROR(rv, { + neu_http_response(aio, error_code.error, result_error); + }); neu_user_free(user); }) @@ -166,16 +167,16 @@ void handle_get_plugin_schema(nng_aio *aio) VALIDATE_JWT(aio); - const char *plugin_name = http_get_param(aio, "plugin_name", &len); + const char *plugin_name = neu_http_get_param(aio, "plugin_name", &len); if (plugin_name == NULL || len == 0) { - http_bad_request(aio, "{\"error\": 1002}"); + neu_http_bad_request(aio, "{\"error\": 1002}"); return; } if (0 > neu_asprintf(&schema_path, "%s/schema/%s.json", g_plugin_dir, plugin_name)) { NEU_JSON_RESPONSE_ERROR(NEU_ERR_EINTERNAL, { - http_response(aio, error_code.error, result_error); + neu_http_response(aio, error_code.error, result_error); }); return; } @@ -184,12 +185,12 @@ void handle_get_plugin_schema(nng_aio *aio) buf = file_string_read(&len, schema_path); if (NULL == buf) { nlog_info("open %s error: %d", schema_path, errno); - http_not_found(aio, "{\"status\": \"error\"}"); + neu_http_not_found(aio, "{\"status\": \"error\"}"); free(schema_path); return; } - http_ok(aio, buf); + neu_http_ok(aio, buf); free(buf); free(schema_path); } diff --git a/plugins/restful/plugin_handle.c b/plugins/restful/plugin_handle.c index 112f8182..9e919ae9 100644 --- a/plugins/restful/plugin_handle.c +++ b/plugins/restful/plugin_handle.c @@ -44,7 +44,7 @@ void handle_add_plugin(nng_aio *aio) ret = neu_plugin_op(plugin, header, &cmd); if (ret != 0) { NEU_JSON_RESPONSE_ERROR(NEU_ERR_IS_BUSY, { - http_response(aio, NEU_ERR_IS_BUSY, result_error); + neu_http_response(aio, NEU_ERR_IS_BUSY, result_error); }); } }) @@ -66,7 +66,7 @@ void handle_del_plugin(nng_aio *aio) ret = neu_plugin_op(plugin, header, &cmd); if (ret != 0) { NEU_JSON_RESPONSE_ERROR(NEU_ERR_IS_BUSY, { - http_response(aio, NEU_ERR_IS_BUSY, result_error); + neu_http_response(aio, NEU_ERR_IS_BUSY, result_error); }); } }) @@ -86,7 +86,7 @@ void handle_get_plugin(nng_aio *aio) ret = neu_plugin_op(plugin, header, NULL); if (ret != 0) { NEU_JSON_RESPONSE_ERROR(NEU_ERR_IS_BUSY, { - http_response(aio, NEU_ERR_IS_BUSY, result_error); + neu_http_response(aio, NEU_ERR_IS_BUSY, result_error); }); } } @@ -116,7 +116,7 @@ void handle_get_plugin_resp(nng_aio *aio, neu_resp_get_plugin_t *plugins) neu_json_encode_by_fn(&plugin_res, neu_json_encode_get_plugin_resp, &result); - http_ok(aio, result); + neu_http_ok(aio, result); free(result); free(plugin_res.plugin_libs); diff --git a/plugins/restful/proxy.c b/plugins/restful/proxy.c index 774c06ae..35d41014 100644 --- a/plugins/restful/proxy.c +++ b/plugins/restful/proxy.c @@ -315,6 +315,6 @@ void handle_proxy(nng_aio *aio) error: NEU_JSON_RESPONSE_ERROR(NEU_ERR_EINTERNAL, { - http_response(aio, error_code.error, result_error); + neu_http_response(aio, error_code.error, result_error); }); } diff --git a/plugins/restful/rest.c b/plugins/restful/rest.c index 648089ec..1ffcf1f5 100644 --- a/plugins/restful/rest.c +++ b/plugins/restful/rest.c @@ -234,7 +234,7 @@ static int dashb_plugin_request(neu_plugin_t * plugin, case NEU_RESP_ERROR: { neu_resp_error_t *error = (neu_resp_error_t *) data; NEU_JSON_RESPONSE_ERROR(error->error, { - http_response(header->ctx, error->error, result_error); + neu_http_response(header->ctx, error->error, result_error); }); break; } diff --git a/plugins/restful/rw_handle.c b/plugins/restful/rw_handle.c index 8f26dcd1..a04064d5 100644 --- a/plugins/restful/rw_handle.c +++ b/plugins/restful/rw_handle.c @@ -46,7 +46,7 @@ void handle_read(nng_aio *aio) ret = neu_plugin_op(plugin, header, &cmd); if (ret != 0) { NEU_JSON_RESPONSE_ERROR(NEU_ERR_IS_BUSY, { - http_response(aio, NEU_ERR_IS_BUSY, result_error); + neu_http_response(aio, NEU_ERR_IS_BUSY, result_error); }); } }) @@ -94,7 +94,7 @@ void handle_write(nng_aio *aio) ret = neu_plugin_op(plugin, header, &cmd); if (ret != 0) { NEU_JSON_RESPONSE_ERROR(NEU_ERR_IS_BUSY, { - http_response(aio, NEU_ERR_IS_BUSY, result_error); + neu_http_response(aio, NEU_ERR_IS_BUSY, result_error); }); } }) @@ -181,7 +181,7 @@ void handle_read_resp(nng_aio *aio, neu_resp_read_group_t *resp) } neu_json_encode_by_fn(&api_res, neu_json_encode_read_resp, &result); - http_ok(aio, result); + neu_http_ok(aio, result); free(api_res.tags); free(result); free(resp->tags); diff --git a/plugins/restful/version_handle.c b/plugins/restful/version_handle.c index 1a9ef14a..4411e7ed 100644 --- a/plugins/restful/version_handle.c +++ b/plugins/restful/version_handle.c @@ -39,11 +39,11 @@ void handle_get_version(nng_aio *aio) if (ret < 0) { NEU_JSON_RESPONSE_ERROR(NEU_ERR_EINTERNAL, { - http_response(aio, error_code.error, result_error); + neu_http_response(aio, error_code.error, result_error); }); return; } - http_response(aio, 0, result); + neu_http_response(aio, 0, result); free(result); } diff --git a/src/utils/http.c b/src/utils/http.c index 440f908a..3d253ae0 100644 --- a/src/utils/http.c +++ b/src/utils/http.c @@ -60,7 +60,7 @@ static int response(nng_aio *aio, char *content, enum nng_http_status status) return 0; } -ssize_t url_decode(const char *s, size_t len, char *buf, size_t size) +ssize_t neu_url_decode(const char *s, size_t len, char *buf, size_t size) { size_t i = 0, j = 0; int n = 0; @@ -81,14 +81,14 @@ ssize_t url_decode(const char *s, size_t len, char *buf, size_t size) return j; } -const char *http_get_header(nng_aio *aio, char *name) +const char *neu_http_get_header(nng_aio *aio, char *name) { nng_http_req *req = nng_aio_get_input(aio, 0); return nng_http_req_get_header(req, name); } -int http_get_body(nng_aio *aio, void **data, size_t *data_size) +int neu_http_get_body(nng_aio *aio, void **data, size_t *data_size) { nng_http_req *req = nng_aio_get_input(aio, 0); @@ -167,7 +167,7 @@ static char *get_param(const char *url, const char *name, size_t *len_p) return p; } -const char *http_get_param(nng_aio *aio, const char *name, size_t *len) +const char *neu_http_get_param(nng_aio *aio, const char *name, size_t *len) { nng_http_req *nng_req = nng_aio_get_input(aio, 0); @@ -177,21 +177,21 @@ const char *http_get_param(nng_aio *aio, const char *name, size_t *len) return val; } -ssize_t http_get_param_str(nng_aio *aio, const char *name, char *buf, - size_t size) +ssize_t neu_http_get_param_str(nng_aio *aio, const char *name, char *buf, + size_t size) { size_t len = 0; - const char *s = http_get_param(aio, name, &len); + const char *s = neu_http_get_param(aio, name, &len); if (NULL == s) { return -2; } - return url_decode(s, len, buf, size); + return neu_url_decode(s, len, buf, size); } -int http_get_param_uintmax(nng_aio *aio, const char *name, uintmax_t *param) +int neu_http_get_param_uintmax(nng_aio *aio, const char *name, uintmax_t *param) { size_t len = 0; - const char *tmp = http_get_param(aio, name, &len); + const char *tmp = neu_http_get_param(aio, name, &len); char * end = NULL; uintmax_t result = 0; @@ -214,11 +214,11 @@ int http_get_param_uintmax(nng_aio *aio, const char *name, uintmax_t *param) return 0; } -int http_get_param_uint64(nng_aio *aio, const char *name, uint64_t *param) +int neu_http_get_param_uint64(nng_aio *aio, const char *name, uint64_t *param) { uintmax_t val; - int rv = http_get_param_uintmax(aio, name, &val); + int rv = neu_http_get_param_uintmax(aio, name, &val); if (0 != rv) { return rv; } @@ -231,11 +231,11 @@ int http_get_param_uint64(nng_aio *aio, const char *name, uint64_t *param) return 0; } -int http_get_param_uint32(nng_aio *aio, const char *name, uint32_t *param) +int neu_http_get_param_uint32(nng_aio *aio, const char *name, uint32_t *param) { uintmax_t val; - int rv = http_get_param_uintmax(aio, name, &val); + int rv = neu_http_get_param_uintmax(aio, name, &val); if (0 != rv) { return rv; } @@ -248,12 +248,12 @@ int http_get_param_uint32(nng_aio *aio, const char *name, uint32_t *param) return 0; } -int http_get_param_node_type(nng_aio *aio, const char *name, - neu_node_type_e *param) +int neu_http_get_param_node_type(nng_aio *aio, const char *name, + neu_node_type_e *param) { uintmax_t val; - int rv = http_get_param_uintmax(aio, name, &val); + int rv = neu_http_get_param_uintmax(aio, name, &val); if (0 != rv) { return rv; } @@ -266,7 +266,7 @@ int http_get_param_node_type(nng_aio *aio, const char *name, return 0; } -int http_response(nng_aio *aio, neu_err_code_e code, char *content) +int neu_http_response(nng_aio *aio, neu_err_code_e code, char *content) { enum nng_http_status status = NNG_HTTP_STATUS_OK; @@ -372,42 +372,42 @@ int http_response(nng_aio *aio, neu_err_code_e code, char *content) return response(aio, content, status); } -int http_ok(nng_aio *aio, char *content) +int neu_http_ok(nng_aio *aio, char *content) { return response(aio, content, NNG_HTTP_STATUS_OK); } -int http_created(nng_aio *aio, char *content) +int neu_http_created(nng_aio *aio, char *content) { return response(aio, content, NNG_HTTP_STATUS_CREATED); } -int http_partial(nng_aio *aio, char *content) +int neu_http_partial(nng_aio *aio, char *content) { return response(aio, content, NNG_HTTP_STATUS_PARTIAL_CONTENT); } -int http_bad_request(nng_aio *aio, char *content) +int neu_http_bad_request(nng_aio *aio, char *content) { return response(aio, content, NNG_HTTP_STATUS_BAD_REQUEST); } -int http_unauthorized(nng_aio *aio, char *content) +int neu_http_unauthorized(nng_aio *aio, char *content) { return response(aio, content, NNG_HTTP_STATUS_UNAUTHORIZED); } -int http_not_found(nng_aio *aio, char *content) +int neu_http_not_found(nng_aio *aio, char *content) { return response(aio, content, NNG_HTTP_STATUS_NOT_FOUND); } -int http_conflict(nng_aio *aio, char *content) +int neu_http_conflict(nng_aio *aio, char *content) { return response(aio, content, NNG_HTTP_STATUS_CONFLICT); } -int http_internal_error(nng_aio *aio, char *content) +int neu_http_internal_error(nng_aio *aio, char *content) { return response(aio, content, NNG_HTTP_STATUS_INTERNAL_SERVER_ERROR); } diff --git a/tests/http_test.cc b/tests/http_test.cc index 6c202a4f..31f4af0c 100644 --- a/tests/http_test.cc +++ b/tests/http_test.cc @@ -62,7 +62,7 @@ TEST(HTTPTest, http_get_param) nng_http_req_set_uri(req, (char *) data[i].uri); nng_aio_set_input(aio, 0, req); len = 0; - const char *val = http_get_param(aio, data[i].name, &len); + const char *val = neu_http_get_param(aio, data[i].name, &len); EXPECT_EQ(len, data[i].len); EXPECT_EQ(strncmp(val, data[i].val, len), 0); } @@ -86,21 +86,21 @@ TEST(HTTPTest, http_get_param_str) nng_aio_set_input(aio, 0, req); nng_http_req_set_uri(req, "/?node_id"); - ret = http_get_param_str(aio, "group", buf, sizeof(buf)); + ret = neu_http_get_param_str(aio, "group", buf, sizeof(buf)); EXPECT_EQ(ret, -2); nng_http_req_set_uri(req, "/?group"); - ret = http_get_param_str(aio, "group", buf, sizeof(buf)); + ret = neu_http_get_param_str(aio, "group", buf, sizeof(buf)); EXPECT_EQ(ret, 0); EXPECT_EQ(0, strcmp("", buf)); nng_http_req_set_uri(req, "/?group=%EZ%BB%84"); - ret = http_get_param_str(aio, "group", buf, 3); + ret = neu_http_get_param_str(aio, "group", buf, 3); EXPECT_EQ(ret, -1); EXPECT_EQ(0, strcmp("", buf)); nng_http_req_set_uri(req, "/?group=%E7%BB%84"); - ret = http_get_param_str(aio, "group", buf, 4); + ret = neu_http_get_param_str(aio, "group", buf, 4); EXPECT_EQ(ret, 3); EXPECT_EQ(0, strcmp("组", buf));