Skip to content

Commit

Permalink
Fix issue where cached REST request would always return with code 200
Browse files Browse the repository at this point in the history
  • Loading branch information
SanderMertens committed Jan 30, 2024
1 parent ca9b34a commit 17681dc
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 8 deletions.
8 changes: 5 additions & 3 deletions flecs.c
Original file line number Diff line number Diff line change
Expand Up @@ -25980,6 +25980,7 @@ typedef struct ecs_http_request_key_t {
typedef struct ecs_http_request_entry_t {
char *content;
int32_t content_length;
int code;
ecs_ftime_t time;
} ecs_http_request_entry_t;

Expand Down Expand Up @@ -26437,6 +26438,7 @@ void http_insert_request_entry(
entry->time = (ecs_ftime_t)ecs_time_measure(&t);
entry->content_length = ecs_strbuf_written(&reply->body);
entry->content = ecs_strbuf_get(&reply->body);
entry->code = reply->code;
ecs_strbuf_appendstrn(&reply->body,
entry->content, entry->content_length);
}
Expand Down Expand Up @@ -26927,7 +26929,7 @@ void http_recv_connection(
if (entry) {
ecs_http_reply_t reply;
reply.body = ECS_STRBUF_INIT;
reply.code = 200;
reply.code = entry->code;
reply.content_type = "application/json";
reply.headers = ECS_STRBUF_INIT;
reply.status = "OK";
Expand Down Expand Up @@ -27504,7 +27506,7 @@ int ecs_http_server_http_request(
http_find_request_entry(srv, request.res, request.req_len);
if (entry) {
reply_out->body = ECS_STRBUF_INIT;
reply_out->code = 200;
reply_out->code = entry->code;
reply_out->content_type = "application/json";
reply_out->headers = ECS_STRBUF_INIT;
reply_out->status = "OK";
Expand Down Expand Up @@ -34501,6 +34503,7 @@ void flecs_rest_reply_set_captured_log(
{
char *err = flecs_rest_get_captured_log();
if (err) {
printf("ERROR!\n");
char *escaped_err = ecs_astresc('"', err);
flecs_reply_error(reply, escaped_err);
reply->code = 400;
Expand Down Expand Up @@ -34529,7 +34532,6 @@ int flecs_rest_iter_to_reply(

if (offset < 0 || limit < 0) {
flecs_reply_error(reply, "invalid offset/limit parameter");
reply->code = 400;
return -1;
}

Expand Down
6 changes: 4 additions & 2 deletions src/addons/http.c
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ typedef struct ecs_http_request_key_t {
typedef struct ecs_http_request_entry_t {
char *content;
int32_t content_length;
int code;
ecs_ftime_t time;
} ecs_http_request_entry_t;

Expand Down Expand Up @@ -590,6 +591,7 @@ void http_insert_request_entry(
entry->time = (ecs_ftime_t)ecs_time_measure(&t);
entry->content_length = ecs_strbuf_written(&reply->body);
entry->content = ecs_strbuf_get(&reply->body);
entry->code = reply->code;
ecs_strbuf_appendstrn(&reply->body,
entry->content, entry->content_length);
}
Expand Down Expand Up @@ -1080,7 +1082,7 @@ void http_recv_connection(
if (entry) {
ecs_http_reply_t reply;
reply.body = ECS_STRBUF_INIT;
reply.code = 200;
reply.code = entry->code;
reply.content_type = "application/json";
reply.headers = ECS_STRBUF_INIT;
reply.status = "OK";
Expand Down Expand Up @@ -1657,7 +1659,7 @@ int ecs_http_server_http_request(
http_find_request_entry(srv, request.res, request.req_len);
if (entry) {
reply_out->body = ECS_STRBUF_INIT;
reply_out->code = 200;
reply_out->code = entry->code;
reply_out->content_type = "application/json";
reply_out->headers = ECS_STRBUF_INIT;
reply_out->status = "OK";
Expand Down
2 changes: 1 addition & 1 deletion src/addons/rest.c
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,7 @@ void flecs_rest_reply_set_captured_log(
{
char *err = flecs_rest_get_captured_log();
if (err) {
printf("ERROR!\n");
char *escaped_err = ecs_astresc('"', err);
flecs_reply_error(reply, escaped_err);
reply->code = 400;
Expand Down Expand Up @@ -389,7 +390,6 @@ int flecs_rest_iter_to_reply(

if (offset < 0 || limit < 0) {
flecs_reply_error(reply, "invalid offset/limit parameter");
reply->code = 400;
return -1;
}

Expand Down
3 changes: 2 additions & 1 deletion test/addons/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -1818,7 +1818,8 @@
"testcases": [
"teardown",
"get",
"get_cached"
"get_cached",
"get_cached_invalid"
]
}, {
"id": "Metrics",
Expand Down
40 changes: 40 additions & 0 deletions test/addons/src/Rest.c
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,43 @@ void Rest_get_cached(void) {

ecs_fini(world);
}

void Rest_get_cached_invalid(void) {
ecs_world_t *world = ecs_init();

ecs_http_server_t *srv = ecs_rest_server_init(world,
&(ecs_http_server_desc_t){
.cache_timeout = 1.0
});
test_assert(srv != NULL);

{
ecs_http_reply_t reply = ECS_HTTP_REPLY_INIT;
test_int(-1, ecs_http_server_request(srv, "GET",
"/entity/flecs/core/Wor", &reply));
test_int(reply.code, 404);

char *reply_str = ecs_strbuf_get(&reply.body);
test_assert(reply_str != NULL);
test_str(reply_str,
"{\"error\":\"entity \'flecs/core/Wor\' not found\"}");
ecs_os_free(reply_str);
}

{
ecs_http_reply_t reply = ECS_HTTP_REPLY_INIT;
test_int(-1, ecs_http_server_request(srv, "GET",
"/entity/flecs/core/Wor", &reply));
test_int(reply.code, 404);

char *reply_str = ecs_strbuf_get(&reply.body);
test_assert(reply_str != NULL);
test_str(reply_str,
"{\"error\":\"entity \'flecs/core/Wor\' not found\"}");
ecs_os_free(reply_str);
}

ecs_rest_server_fini(srv);

ecs_fini(world);
}
7 changes: 6 additions & 1 deletion test/addons/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -1748,6 +1748,7 @@ void Http_stop_start(void);
void Rest_teardown(void);
void Rest_get(void);
void Rest_get_cached(void);
void Rest_get_cached_invalid(void);

// Testsuite 'Metrics'
void Metrics_member_gauge_1_entity(void);
Expand Down Expand Up @@ -8585,6 +8586,10 @@ bake_test_case Rest_testcases[] = {
{
"get_cached",
Rest_get_cached
},
{
"get_cached_invalid",
Rest_get_cached_invalid
}
};

Expand Down Expand Up @@ -9123,7 +9128,7 @@ static bake_test_suite suites[] = {
"Rest",
NULL,
NULL,
3,
4,
Rest_testcases
},
{
Expand Down

0 comments on commit 17681dc

Please sign in to comment.