Commit 9872182a authored by Philipp Schafft's avatar Philipp Schafft 🦁
Browse files

Improved handling of HTTP client errors:

 * Make the internal API more uniform,
 * Improved error pages slightly (See: #1889).

svn path=/icecast/trunk/icecast/; revision=18644
parent 46984437
...@@ -37,6 +37,8 @@ ...@@ -37,6 +37,8 @@
#include "client.h" #include "client.h"
#include "logging.h" #include "logging.h"
#include "util.h"
#ifdef _WIN32 #ifdef _WIN32
#define snprintf _snprintf #define snprintf _snprintf
#endif #endif
...@@ -180,59 +182,43 @@ int client_read_bytes (client_t *client, void *buf, unsigned len) ...@@ -180,59 +182,43 @@ int client_read_bytes (client_t *client, void *buf, unsigned len)
return bytes; return bytes;
} }
static void client_send_error0(client_t *client, int status, int plain, char *message)
void client_send_400(client_t *client, char *message) { {
ssize_t ret; ssize_t ret;
ret = util_http_build_header(client->refbuf->data, PER_CLIENT_REFBUF_SIZE, 0, ret = util_http_build_header(client->refbuf->data, PER_CLIENT_REFBUF_SIZE, 0,
0, 400, NULL, 0, status, NULL,
"text/html", NULL, plain ? "text/plain" : "text/html", NULL,
""); plain ? message : "");
snprintf(client->refbuf->data + ret, PER_CLIENT_REFBUF_SIZE - ret, if (!plain)
"<b>%s</b>\r\n", message); snprintf(client->refbuf->data + ret, PER_CLIENT_REFBUF_SIZE - ret,
"<html><head><title>Error %i</title></head><body><b>%i - %s</b></body></html>\r\n",
status, status, message);
client->respcode = 400; client->respcode = status;
client->refbuf->len = strlen (client->refbuf->data); client->refbuf->len = strlen (client->refbuf->data);
fserve_add_client (client, NULL); fserve_add_client (client, NULL);
} }
void client_send_404(client_t *client, char *message) { void client_send_400(client_t *client, char *message)
ssize_t ret; {
client_send_error0(client, 400, 0, message);
ret = util_http_build_header(client->refbuf->data, PER_CLIENT_REFBUF_SIZE, 0,
0, 404, NULL,
"text/html", NULL,
"");
snprintf(client->refbuf->data + ret, PER_CLIENT_REFBUF_SIZE - ret,
"<b>%s</b>\r\n", message);
client->respcode = 404;
client->refbuf->len = strlen (client->refbuf->data);
fserve_add_client (client, NULL);
} }
void client_send_404(client_t *client, char *message)
{
client_send_error0(client, 404, 0, message);
}
void client_send_401(client_t *client) { void client_send_401(client_t *client)
util_http_build_header(client->refbuf->data, PER_CLIENT_REFBUF_SIZE, 0, {
0, 401, NULL, client_send_error0(client, 401, 1, "You need to authenticate\r\n");
"text/plain", NULL,
"You need to authenticate\r\n");
client->respcode = 401;
client->refbuf->len = strlen (client->refbuf->data);
fserve_add_client (client, NULL);
} }
void client_send_403(client_t *client, const char *reason) void client_send_403(client_t *client, const char *reason)
{ {
util_http_build_header(client->refbuf->data, PER_CLIENT_REFBUF_SIZE, 0, client_send_error0(client, 403, 1, "Forbidden");
0, 403, reason,
"text/plain", NULL,
"Forbidden");
client->respcode = 403;
client->refbuf->len = strlen (client->refbuf->data);
fserve_add_client (client, NULL);
} }
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment