diff options
author | Eric Wong <normalperson@yhbt.net> | 2008-06-30 02:42:45 +0000 |
---|---|---|
committer | Eric Wong <normalperson@yhbt.net> | 2008-06-30 02:42:45 +0000 |
commit | ec1eeeeaff10dbb93024adbe0d7288c9b6a92a7f (patch) | |
tree | f7743559042de5f578757a88283c4c0c79c1ed67 | |
parent | bec08358369930d4631f472fce94f8adb31cdc6b (diff) | |
download | mpd-ec1eeeeaff10dbb93024adbe0d7288c9b6a92a7f.tar.gz mpd-ec1eeeeaff10dbb93024adbe0d7288c9b6a92a7f.tar.xz mpd-ec1eeeeaff10dbb93024adbe0d7288c9b6a92a7f.zip |
http: split out auth code since it's mostly uninteresting for now
The auth code also has some ugly usages of string generation
which I will eventually replace with something nicer...
git-svn-id: https://svn.musicpd.org/mpd/trunk@7387 09075e82-0dd4-0310-85a5-a0d7c8717e4f
Diffstat (limited to '')
-rw-r--r-- | src/Makefile.am | 1 | ||||
-rw-r--r-- | src/inputStream_http.c | 76 | ||||
-rw-r--r-- | src/inputStream_http_auth.h | 101 |
3 files changed, 103 insertions, 75 deletions
diff --git a/src/Makefile.am b/src/Makefile.am index 9e053ef3e..a1809976a 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -47,6 +47,7 @@ mpd_headers = \ inputStream.h \ inputStream_file.h \ inputStream_http.h \ + inputStream_http_auth.h \ interface.h \ list.h \ listen.h \ diff --git a/src/inputStream_http.c b/src/inputStream_http.c index 51e3b63f3..370027dcd 100644 --- a/src/inputStream_http.c +++ b/src/inputStream_http.c @@ -17,6 +17,7 @@ */ #include "inputStream_http.h" +#include "inputStream_http_auth.h" #include "utils.h" #include "log.h" @@ -143,81 +144,6 @@ void inputStream_initHttp(void) assert(prebufferSize > 0 && "http prebufferSize too small"); } -/* base64 code taken from xmms */ - -#define BASE64_LENGTH(len) (4 * (((len) + 2) / 3)) - -static char *base64Dup(char *s) -{ - int i; - int len = strlen(s); - char *ret = xcalloc(BASE64_LENGTH(len) + 1, 1); - unsigned char *p = (unsigned char *)ret; - - static const char tbl[64] = { - 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', - 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', - 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', - 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', - 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', - 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', - 'w', 'x', 'y', 'z', '0', '1', '2', '3', - '4', '5', '6', '7', '8', '9', '+', '/' - }; - - /* Transform the 3x8 bits to 4x6 bits, as required by base64. */ - for (i = 0; i < len; i += 3) { - *p++ = tbl[s[0] >> 2]; - *p++ = tbl[((s[0] & 3) << 4) + (s[1] >> 4)]; - *p++ = tbl[((s[1] & 0xf) << 2) + (s[2] >> 6)]; - *p++ = tbl[s[2] & 0x3f]; - s += 3; - } - /* Pad the result if necessary... */ - if (i == len + 1) - *(p - 1) = '='; - else if (i == len + 2) - *(p - 1) = *(p - 2) = '='; - /* ...and zero-terminate it. */ - *p = '\0'; - - return ret; -} - -static char *authString(const char *header, - const char *user, const char *password) -{ - char *ret = NULL; - int templen; - char *temp; - char *temp64; - - if (!user || !password) - return NULL; - - templen = strlen(user) + strlen(password) + 2; - temp = xmalloc(templen); - strcpy(temp, user); - strcat(temp, ":"); - strcat(temp, password); - temp64 = base64Dup(temp); - free(temp); - - ret = xmalloc(strlen(temp64) + strlen(header) + 3); - strcpy(ret, header); - strcat(ret, temp64); - strcat(ret, "\r\n"); - free(temp64); - - return ret; -} - -#define PROXY_AUTH_HEADER "Proxy-Authorization: Basic " -#define HTTP_AUTH_HEADER "Authorization: Basic " - -#define proxyAuthString(x, y) authString(PROXY_AUTH_HEADER, x, y) -#define httpAuthString(x, y) authString(HTTP_AUTH_HEADER, x, y) - static InputStreamHTTPData *newInputStreamHTTPData(void) { InputStreamHTTPData *ret = xmalloc(sizeof(InputStreamHTTPData)); diff --git a/src/inputStream_http_auth.h b/src/inputStream_http_auth.h new file mode 100644 index 000000000..db4fb4584 --- /dev/null +++ b/src/inputStream_http_auth.h @@ -0,0 +1,101 @@ +/* the Music Player Daemon (MPD) + * Copyright (C) 2003-2007 by Warren Dukes (warren.dukes@gmail.com) + * This project's homepage is: http://www.musicpd.org + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +/* This file is only included by inputStream_http.c */ +#ifndef INPUT_STREAM_HTTP_AUTH_H +#define INPUT_STREAM_HTTP_AUTH_H + +#include "os_compat.h" +#include "utils.h" + +/* base64 code taken from xmms */ + +#define BASE64_LENGTH(len) (4 * (((len) + 2) / 3)) + +static char *base64Dup(char *s) +{ + int i; + int len = strlen(s); + char *ret = xcalloc(BASE64_LENGTH(len) + 1, 1); + unsigned char *p = (unsigned char *)ret; + + static const char tbl[64] = { + 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', + 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', + 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', + 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', + 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', + 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', + 'w', 'x', 'y', 'z', '0', '1', '2', '3', + '4', '5', '6', '7', '8', '9', '+', '/' + }; + + /* Transform the 3x8 bits to 4x6 bits, as required by base64. */ + for (i = 0; i < len; i += 3) { + *p++ = tbl[s[0] >> 2]; + *p++ = tbl[((s[0] & 3) << 4) + (s[1] >> 4)]; + *p++ = tbl[((s[1] & 0xf) << 2) + (s[2] >> 6)]; + *p++ = tbl[s[2] & 0x3f]; + s += 3; + } + /* Pad the result if necessary... */ + if (i == len + 1) + *(p - 1) = '='; + else if (i == len + 2) + *(p - 1) = *(p - 2) = '='; + /* ...and zero-terminate it. */ + *p = '\0'; + + return ret; +} + +static char *authString(const char *header, + const char *user, const char *password) +{ + char *ret = NULL; + int templen; + char *temp; + char *temp64; + + if (!user || !password) + return NULL; + + templen = strlen(user) + strlen(password) + 2; + temp = xmalloc(templen); + strcpy(temp, user); + strcat(temp, ":"); + strcat(temp, password); + temp64 = base64Dup(temp); + free(temp); + + ret = xmalloc(strlen(temp64) + strlen(header) + 3); + strcpy(ret, header); + strcat(ret, temp64); + strcat(ret, "\r\n"); + free(temp64); + + return ret; +} + +#define PROXY_AUTH_HEADER "Proxy-Authorization: Basic " +#define HTTP_AUTH_HEADER "Authorization: Basic " + +#define proxyAuthString(x, y) authString(PROXY_AUTH_HEADER, x, y) +#define httpAuthString(x, y) authString(HTTP_AUTH_HEADER, x, y) + +#endif /* INPUT_STREAM_HTTP_AUTH_H */ |