Skip to content

Commit

Permalink
Merge pull request #1049 from lixiumei123/v2.3
Browse files Browse the repository at this point in the history
fix download log coredump
  • Loading branch information
fengzeroz authored Feb 3, 2023
2 parents 9833af0 + 5d195d5 commit b46a319
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 104 deletions.
3 changes: 3 additions & 0 deletions include/neuron/errcodes.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,9 @@ typedef enum {
NEU_ERR_PLUGIN_TAG_NOT_READY = 3011,
NEU_ERR_PLUGIN_PACKET_OUT_OF_ORDER = 3012,

NEU_ERR_FILE_OPEN_FAILURE = 4101,
NEU_ERR_FILE_READ_FAILURE = 4102,

NEU_ERR_MQTT_FAILURE = 4000,
NEU_ERR_MQTT_NO_CERTFILESET = 4001,
NEU_ERR_MQTT_CERTFILE_LOAD_FAILURE = 4002,
Expand Down
2 changes: 2 additions & 0 deletions plugins/restful/http.c
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,8 @@ int http_response(nng_aio *aio, neu_err_code_e code, char *content)
status = NNG_HTTP_STATUS_BAD_REQUEST;
break;
case NEU_ERR_FILE_NOT_EXIST:
case NEU_ERR_FILE_OPEN_FAILURE:
case NEU_ERR_FILE_READ_FAILURE:
case NEU_ERR_LIBRARY_NOT_FOUND:
case NEU_ERR_NODE_NOT_EXIST:
case NEU_ERR_TAG_NOT_EXIST:
Expand Down
107 changes: 3 additions & 104 deletions plugins/restful/log_handle.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@

UT_array *collect_log_files();

// void handle_get_log_files(nng_aio *aio);
int read_file(const char *file_name, void **datap, size_t *lenp);
static int http_resp_files(nng_aio *aio, void *data, size_t len,
const char *disposition);
Expand Down Expand Up @@ -185,106 +184,6 @@ void handle_log_level(nng_aio *aio)
})
}

// void handle_get_log(nng_aio *aio)
// {
// int rv = 0;
// char log_file[NEU_NODE_NAME_LEN + 32] = { 0 };

// VALIDATE_JWT(aio);

// // optional params
// rv = http_get_param_str(aio, "log_file", log_file, sizeof(log_file));
// if (-1 == rv) {
// nlog_error("parse query param `log_file` fail");
// NEU_JSON_RESPONSE_ERROR(NEU_ERR_PARAM_IS_WRONG, {
// http_response(aio, error_code.error, result_error);
// });
// return;
// } else if (-2 == rv) {
// // handle no param
// handle_get_log_files(aio);
// return;
// } else if ((size_t) rv >= sizeof(log_file)) {
// nlog_error("query param overflow, `log_file`:%s", log_file);
// NEU_JSON_RESPONSE_ERROR(NEU_ERR_EINTERNAL, {
// http_response(aio, error_code.error, result_error);
// });
// return;
// }

// void * data = NULL;
// size_t len = 0;
// nng_http_res *res = NULL;
// rv = read_log_file(log_file, &data, &len);
// if (0 != rv) {
// NEU_JSON_RESPONSE_ERROR(
// rv, { http_response(aio, error_code.error, result_error); });
// return;
// }

// if (((rv = nng_http_res_alloc(&res)) != 0) ||
// ((rv = nng_http_res_set_status(res, NNG_HTTP_STATUS_OK)) != 0) ||
// ((rv = nng_http_res_set_header(res, "Content-Type",
// "application/octet-stream")) != 0) ||
// ((rv = nng_http_res_set_header(res, "Access-Control-Allow-Origin",
// "*")) != 0) ||
// ((rv = nng_http_res_set_header(res, "Access-Control-Allow-Methods",
// "POST,GET,PUT,DELETE,OPTIONS")) != 0)
// ||
// ((rv = nng_http_res_set_header(res, "Access-Control-Allow-Headers",
// "*")) != 0) ||
// ((rv = nng_http_res_copy_data(res, data, len)) != 0)) {
// nng_http_res_free(res);
// free(data);
// nng_aio_finish(aio, rv);
// return;
// }

// free(data);
// nng_aio_set_output(aio, 0, res);
// nng_aio_finish(aio, 0);
// }

// void handle_get_log_files(nng_aio *aio)
// {
// UT_array *log_files = collect_log_files();
// if (NULL == log_files) {
// NEU_JSON_RESPONSE_ERROR(NEU_ERR_EINTERNAL, {
// http_response(aio, error_code.error, result_error);
// });
// return;
// }

// size_t len = 1 + strlen("{log_files:[ ]}");
// utarray_foreach(log_files, char **, log_file)
// {
// // one byte for the comma,
// // the last one will be for the terminating NULL byte
// len += strlen(*log_file) + 1;
// }

// char *res = malloc(len);
// if (NULL == res) {
// NEU_JSON_RESPONSE_ERROR(NEU_ERR_EINTERNAL, {
// http_response(aio, error_code.error, result_error);
// });
// utarray_free(log_files);
// return;
// }

// // with the space char to simplify code
// int i = snprintf(res, len, "{log_files:[ ");
// utarray_foreach(log_files, char **, log_file)
// {
// i += snprintf(res + i, len - i, "%s,", *log_file);
// }
// // will write over the last comma/space
// snprintf(res + i - 1, len, "]}");
// http_ok(aio, res);
// free(res);
// utarray_free(log_files);
// }

static int http_resp_files(nng_aio *aio, void *data, size_t len,
const char *disposition)
{
Expand Down Expand Up @@ -365,7 +264,7 @@ int read_file(const char *file_name, void **datap, size_t *lenp)

if ((f = fopen(file_name, "rb")) == NULL) {
nlog_error("open fail: %s", file_name);
return NEU_ERR_EINTERNAL;
return NEU_ERR_FILE_OPEN_FAILURE;
}

len = st.st_size;
Expand All @@ -375,8 +274,8 @@ int read_file(const char *file_name, void **datap, size_t *lenp)
goto done;
}
if (fread(data, 1, len, f) != len) {
rv = errno;
nlog_info("rv = %d", rv);
nlog_error("file read failued, errno = %d", errno);
rv = NEU_ERR_FILE_READ_FAILURE;
free(data);
goto done;
}
Expand Down

0 comments on commit b46a319

Please sign in to comment.