Add Web server test part
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
NADAL Jean-Baptiste
2020-02-20 19:44:55 +01:00
parent b9beca6d72
commit af35811644
6 changed files with 680 additions and 16 deletions

View File

@@ -182,7 +182,7 @@ static int notify_loopexit(restd_server_t *server);
static void notify_cb(struct bufferevent *buffer, void *userdata);
void rest_request_cb(struct evhttp_request *req, void *arg);
void print_request_info(struct evhttp_request *req);
void restd_http_response_from_file(struct evhttp_request *req, int code, int fd, const char *content_type);
void restd_http_response_from_file(struct evhttp_request *req, int code, FILE *f, const char *content_type);
bool manage_hook(restd_hook_t *hook, restd_resp_t *response, const char *request_path);
/*--------------------------- PUBLIC FUNCTIONS -------------------------------*/
@@ -831,19 +831,20 @@ void rest_request_cb(struct evhttp_request *req, void *arg)
}
}
}
// No Hook Found check if it's a real file into document root.
root_path = restd_server_get_option(server, "server.root_path");
if ((root_path != NULL) && (strlen(root_path) != 0))
{
int fd;
FILE *f;
char buf[1024] = "";
qstrcatf(buf, "%s%s", root_path, request_path);
fd = open(buf, 0);
if (fd != -1)
f = fopen(buf, "rb");
if (f != NULL)
{
restd_http_response_from_file(req, 200, fd, file_mime_lookup(buf));
restd_http_response_from_file(req, 200, f, file_mime_lookup(buf));
restd_resp_free(response);
fclose(f);
return;
}
else
@@ -920,22 +921,29 @@ void print_request_info(struct evhttp_request *req)
/*--------------------------------------------------------------------------*/
void restd_http_response_from_file(struct evhttp_request *req, int code, int fd, const char *content_type)
void restd_http_response_from_file(struct evhttp_request *req, int code, FILE *f, const char *content_type)
{
struct evbuffer *resp_buf;
struct evkeyvalq *resp_headers;
ev_off_t len;
struct evbuffer *output_buffer;
struct evkeyvalq *output_headers;
char buf[1024];
size_t s;
size_t bytes = 0;
resp_buf = evhttp_request_get_output_buffer(req);
resp_headers = evhttp_request_get_output_headers(req);
output_buffer = evhttp_request_get_output_buffer(req);
output_headers = evhttp_request_get_output_headers(req);
len = lseek(fd, 0, SEEK_END);
while ((s = fread(buf, 1, sizeof(buf), f)) > 0)
{
evbuffer_add(output_buffer, buf, s);
bytes += s;
}
evbuffer_add_file(resp_buf, fd, 0, len);
evutil_snprintf(buf, sizeof(buf) - 1, "%lu", (unsigned long)bytes);
evhttp_add_header(resp_headers, "Content-Type", content_type);
evhttp_add_header(output_headers, "Content-Length", buf);
evhttp_add_header(output_headers, "Content-Type", content_type);
evhttp_send_reply(req, code, NULL, resp_buf);
evhttp_send_reply(req, code, NULL, output_buffer);
}
/*--------------------------------------------------------------------------*/