[xiph-cvs] cvs commit: libshout/src shout.c

Brendan brendan at xiph.org
Thu Jan 16 00:05:42 PST 2003



brendan     03/01/16 03:05:42

  Modified:    src      shout.c
  Log:
  Move HTTP basic authorization into separate function.
  Add HTTP metadata update for icecast2.

Revision  Changes    Path
1.18      +54 -18    libshout/src/shout.c

Index: shout.c
===================================================================
RCS file: /usr/local/cvsroot/libshout/src/shout.c,v
retrieving revision 1.17
retrieving revision 1.18
diff -u -r1.17 -r1.18
--- shout.c	15 Jan 2003 23:48:10 -0000	1.17
+++ shout.c	16 Jan 2003 08:05:42 -0000	1.18
@@ -18,6 +18,7 @@
 static int login_xaudiocast(shout_t *self);
 static int login_icy(shout_t *self);
 static int login_http_basic(shout_t *self);
+char *http_basic_authorization(shout_t *self);
 
 /* -- public functions -- */
 
@@ -279,7 +280,7 @@
         if (self->protocol == SHOUT_PROTOCOL_ICY)
                 rv = sock_write(socket, "GET /admin.cgi?mode=updinfo&pass=%s", self->password);
         else if (self->protocol == SHOUT_PROTOCOL_HTTP)
-		rv = sock_write(socket, "GET /admin.cgi?mode=updinfo&mount=%s", self->mount);
+		rv = sock_write(socket, "GET /admin/metadata?mode=updinfo&mount=%s", self->mount);
         else
                 rv = sock_write(socket, "GET /admin.cgi?mode=updinfo&pass=%s&mount=%s", self->password, self->mount);
         if (!rv) {
@@ -313,7 +314,23 @@
                 return rv;
         }
 
-	if (!sock_write(socket, " HTTP/1.0\r\nUser-Agent: %s\r\n\r\n", shout_get_agent(self))) {
+	if (!sock_write(socket, " HTTP/1.0\r\nUser-Agent: %s\r\n", shout_get_agent(self))) {
+		sock_close(socket);
+		return SHOUTERR_SOCKET;
+	}
+	if (self->protocol == SHOUT_PROTOCOL_HTTP) {
+		char *auth = http_basic_authorization(self);
+
+		if (auth) {
+			if (!sock_write(socket, auth)) {
+				sock_close(socket);
+				free(auth);
+				return SHOUTERR_SOCKET;
+			}
+			free (auth);
+		}
+	}
+	if (!sock_write(socket, "\r\n")) {
                 sock_close(socket);
                 return SHOUTERR_SOCKET;
         }
@@ -716,9 +733,19 @@
 
 static int send_http_request(shout_t *self, char *username, char *password)
 {
+	char *auth;
+
         if (!sock_write(self->socket, "SOURCE %s HTTP/1.0\r\n", self->mount))
                 return SHOUTERR_SOCKET;
 
+	if (self->password && (auth = http_basic_authorization(self))) {
+		if (!sock_write(self->socket, auth)) {
+			free(auth);
+			return SHOUTERR_SOCKET;
+		}
+		free(auth);
+	}
+
         if (!sock_write(self->socket, "ice-name: %s\r\n", self->name != NULL ? self->name : "no name"))
                 return SHOUTERR_SOCKET;
         if (self->url) {
@@ -748,22 +775,6 @@
                 if (!sock_write(self->socket, "Content-Type: audio/mpeg\r\n"))
                         return SHOUTERR_SOCKET;
         }
-	if (username && password) {
-		char *data;
-		int len = strlen(username) + strlen(password) + 2;
-		char *orig = malloc(len);
-		strcpy(orig, username);
-		strcat(orig, ":");
-		strcat(orig, password);
-
-		data = util_base64_encode(orig);
-
-		if(!sock_write(self->socket, "Authorization: Basic %s\r\n", data)) {
-			free(data);
-			return SHOUTERR_SOCKET;
-		}
-		free(data);
-	}
 
         if (!sock_write(self->socket, "\r\n"))
                 return SHOUTERR_SOCKET;
@@ -771,6 +782,31 @@
         return SHOUTERR_SUCCESS;
 }
 
+char *http_basic_authorization(shout_t *self)
+{
+	char *out, *in;
+	int len;
+
+	if (!self || !self->user || !self->password)
+		return NULL;
+
+	len = strlen(self->user) + strlen(self->password) + 2;
+	if (!(in = malloc(len)))
+		return NULL;
+	sprintf(in, "%s:%s", self->user, self->password);
+	out = util_base64_encode(in);
+	free(in);
+
+	len = strlen(out) + 24;
+	if (!(in = malloc(len))) {
+		free(out);
+		return NULL;
+	}
+	sprintf(in, "Authorization: Basic %s\r\n", out);
+	free(out);
+	
+	return in;
+}
 
 static int login_http_basic(shout_t *self)
 {

<p><p>--- >8 ----
List archives:  http://www.xiph.org/archives/
Ogg project homepage: http://www.xiph.org/ogg/
To unsubscribe from this list, send a message to 'cvs-request at xiph.org'
containing only the word 'unsubscribe' in the body.  No subject is needed.
Unsubscribe messages sent to the list will be ignored/filtered.



More information about the commits mailing list