[xiph-commits] r18644 - icecast/trunk/icecast/src

ph3-der-loewe at svn.xiph.org ph3-der-loewe at svn.xiph.org
Wed Oct 10 16:15:05 PDT 2012


Author: ph3-der-loewe
Date: 2012-10-10 16:15:05 -0700 (Wed, 10 Oct 2012)
New Revision: 18644

Modified:
   icecast/trunk/icecast/src/client.c
Log:
Improved handling of HTTP client errors:
 * Make the internal API more uniform,
 * Improved error pages slightly (See: #1889).


Modified: icecast/trunk/icecast/src/client.c
===================================================================
--- icecast/trunk/icecast/src/client.c	2012-10-10 22:48:15 UTC (rev 18643)
+++ icecast/trunk/icecast/src/client.c	2012-10-10 23:15:05 UTC (rev 18644)
@@ -37,6 +37,8 @@
 #include "client.h"
 #include "logging.h"
 
+#include "util.h"
+
 #ifdef _WIN32
 #define snprintf _snprintf
 #endif
@@ -180,59 +182,43 @@
     return bytes;
 }
 
-
-void client_send_400(client_t *client, char *message) {
+static void client_send_error0(client_t *client, int status, int plain, char *message)
+{
     ssize_t ret;
 
     ret = util_http_build_header(client->refbuf->data, PER_CLIENT_REFBUF_SIZE, 0,
-                                 0, 400, NULL,
-                                 "text/html", NULL,
-                                 "");
+                                 0, status, NULL,
+                                 plain ? "text/plain" : "text/html", NULL,
+                                 plain ? message : "");
 
-    snprintf(client->refbuf->data + ret, PER_CLIENT_REFBUF_SIZE - ret,
-             "<b>%s</b>\r\n", message);
+    if (!plain)
+        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);
     fserve_add_client (client, NULL);
 }
 
-void client_send_404(client_t *client, char *message) {
-    ssize_t ret;
+void client_send_400(client_t *client, char *message)
+{
+    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) {
-    util_http_build_header(client->refbuf->data, PER_CLIENT_REFBUF_SIZE, 0,
-                           0, 401, NULL,
-			   "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_401(client_t *client)
+{
+    client_send_error0(client, 401, 1, "You need to authenticate\r\n");
 }
 
 void client_send_403(client_t *client, const char *reason)
 {
-    util_http_build_header(client->refbuf->data, PER_CLIENT_REFBUF_SIZE, 0,
-                           0, 403, reason,
-			   "text/plain", NULL,
-			   "Forbidden");
-    client->respcode = 403;
-    client->refbuf->len = strlen (client->refbuf->data);
-    fserve_add_client (client, NULL);
+    client_send_error0(client, 403, 1, "Forbidden");
 }
 
 



More information about the commits mailing list