mirror of
https://github.com/owntone/owntone-server.git
synced 2025-12-05 23:33:13 -05:00
[httpd] Implement httpd_send_error, a modified evhttp_send_error, which
can include CORS headers (credit @bjoernricks and libevent)
This commit is contained in:
39
src/httpd.c
39
src/httpd.c
@@ -86,6 +86,11 @@
|
||||
|
||||
#define STREAM_CHUNK_SIZE (64 * 1024)
|
||||
#define WEBFACE_ROOT DATADIR "/webface/"
|
||||
#define ERR_PAGE "<html>\n<head>\n" \
|
||||
"<title>%d %s</title>\n" \
|
||||
"</head>\n<body>\n" \
|
||||
"<h1>%s</h1>\n" \
|
||||
"</body>\n</html>\n"
|
||||
|
||||
struct content_type_map {
|
||||
char *ext;
|
||||
@@ -758,7 +763,6 @@ httpd_gzip_deflate(struct evbuffer *in)
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* Thread: httpd */
|
||||
void
|
||||
httpd_send_reply(struct evhttp_request *req, int code, const char *reason, struct evbuffer *evbuf, enum httpd_send_flags flags)
|
||||
{
|
||||
@@ -800,6 +804,39 @@ httpd_send_reply(struct evhttp_request *req, int code, const char *reason, struc
|
||||
}
|
||||
}
|
||||
|
||||
// This is a modified version of evhttp_send_error (credit libevent)
|
||||
void
|
||||
httpd_send_error(struct evhttp_request* req, int error, const char* reason)
|
||||
{
|
||||
struct evkeyvalq *output_headers;
|
||||
struct evbuffer *evbuf;
|
||||
|
||||
if (!allow_origin)
|
||||
{
|
||||
evhttp_send_error(req, error, reason);
|
||||
return;
|
||||
}
|
||||
|
||||
output_headers = evhttp_request_get_output_headers(req);
|
||||
|
||||
evhttp_clear_headers(output_headers);
|
||||
|
||||
evhttp_add_header(output_headers, "Access-Control-Allow-Origin", allow_origin);
|
||||
evhttp_add_header(output_headers, "Content-Type", "text/html");
|
||||
evhttp_add_header(output_headers, "Connection", "close");
|
||||
|
||||
evbuf = evbuffer_new();
|
||||
if (!evbuf)
|
||||
DPRINTF(E_LOG, L_HTTPD, "Could not allocate evbuffer for error page\n");
|
||||
else
|
||||
evbuffer_add_printf(evbuf, ERR_PAGE, error, reason, reason);
|
||||
|
||||
evhttp_send_reply(req, error, reason, evbuf);
|
||||
|
||||
if (evbuf)
|
||||
evbuffer_free(evbuf);
|
||||
}
|
||||
|
||||
/* Thread: httpd */
|
||||
static int
|
||||
path_is_legal(char *path)
|
||||
|
||||
Reference in New Issue
Block a user