http proxy mostly working, need to implement http authentication

git-svn-id: https://svn.musicpd.org/mpd/trunk@1588 09075e82-0dd4-0310-85a5-a0d7c8717e4f
This commit is contained in:
Warren Dukes 2004-06-20 20:15:05 +00:00
parent 6eb6c454be
commit ba798c5e65
3 changed files with 46 additions and 12 deletions

View File

@ -49,6 +49,8 @@ struct _InputStream {
char * metaTitle; char * metaTitle;
}; };
void initInputStream();
int isUrlSaneForInputStream(char * url); int isUrlSaneForInputStream(char * url);
/* if an error occurs for these 3 functions, then -1 is returned and errno /* if an error occurs for these 3 functions, then -1 is returned and errno

View File

@ -60,6 +60,9 @@ typedef struct _InputStreemHTTPData {
int icyMetaint; int icyMetaint;
int prebuffer; int prebuffer;
int icyOffset; int icyOffset;
char * proxyHost;
int proxyPort;
char * proxyAuth;
} InputStreamHTTPData; } InputStreamHTTPData;
void inputStream_initHttp() { void inputStream_initHttp() {
@ -114,7 +117,21 @@ void inputStream_initHttp() {
static InputStreamHTTPData * newInputStreamHTTPData() { static InputStreamHTTPData * newInputStreamHTTPData() {
InputStreamHTTPData * ret = malloc(sizeof(InputStreamHTTPData)); InputStreamHTTPData * ret = malloc(sizeof(InputStreamHTTPData));
ret->host = NULL; if(getConf()[CONF_HTTP_PROXY_HOST]) {
ret->proxyHost = getConf()[CONF_HTTP_PROXY_HOST];
DEBUG(__FILE__ ": Proxy host %s\n", ret->proxyHost);
ret->proxyPort = atoi(getConf()[CONF_HTTP_PROXY_PORT]);
DEBUG(__FILE__ ": Proxy port %i\n", ret->proxyPort);
ret->proxyAuth = /*proxyAuthString(
getConf()[CONF_HTTP_PROXY_USER],
getConf()[CONF_HTTP_PROXY_PASSWORD])*/ NULL;
}
else {
ret->proxyHost = NULL;
ret->proxyAuth = NULL;
}
ret->host = NULL;
ret->path = NULL; ret->path = NULL;
ret->port = 80; ret->port = 80;
ret->connState = HTTP_CONN_STATE_CLOSED; ret->connState = HTTP_CONN_STATE_CLOSED;
@ -129,6 +146,7 @@ static InputStreamHTTPData * newInputStreamHTTPData() {
static void freeInputStreamHTTPData(InputStreamHTTPData * data) { static void freeInputStreamHTTPData(InputStreamHTTPData * data) {
if(data->host) free(data->host); if(data->host) free(data->host);
if(data->path) free(data->path); if(data->path) free(data->path);
if(data->proxyAuth) free(data->proxyAuth);
free(data); free(data);
} }
@ -170,7 +188,8 @@ static int parseUrl(InputStreamHTTPData * data, char * url) {
} }
/* fetch the path */ /* fetch the path */
data->path = strdup(slash ? slash : "/"); if(data->proxyHost) data->path = strdup(url);
else data->path = strdup(slash ? slash : "/");
return 0; return 0;
} }
@ -183,17 +202,29 @@ static int initHTTPConnection(InputStream * inStream) {
InputStreamHTTPData * data = (InputStreamHTTPData *)inStream->data; InputStreamHTTPData * data = (InputStreamHTTPData *)inStream->data;
int flags; int flags;
int ret; int ret;
char * connHost;
int connPort;
#ifdef HAVE_IPV6 #ifdef HAVE_IPV6
struct sockaddr_in6 sin6; struct sockaddr_in6 sin6;
#endif #endif
if(!(he = gethostbyname(data->host))) { if(data->proxyHost) {
connHost = data->proxyHost;
connPort = data->proxyPort;
}
else {
connHost = data->host;
connPort = data->port;
}
if(!(he = gethostbyname(connHost))) {
DEBUG(__FILE__ ": failure to lookup host \"%s\"\n",connHost);
return -1; return -1;
} }
memset(&sin,0,sizeof(struct sockaddr_in)); memset(&sin,0,sizeof(struct sockaddr_in));
sin.sin_family = AF_INET; sin.sin_family = AF_INET;
sin.sin_port = htons(data->port); sin.sin_port = htons(connPort);
#ifdef HAVE_IPV6 #ifdef HAVE_IPV6
memset(&sin6,0,sizeof(struct sockaddr_in6)); memset(&sin6,0,sizeof(struct sockaddr_in6));
sin6.sin6_family = AF_INET6; sin6.sin6_family = AF_INET6;
@ -231,6 +262,7 @@ static int initHTTPConnection(InputStream * inStream) {
ret = connect(data->sock,dest,destlen); ret = connect(data->sock,dest,destlen);
if(ret < 0 && errno!=EINPROGRESS) { if(ret < 0 && errno!=EINPROGRESS) {
DEBUG(__FILE__ ": unable to connect: %s\n", strerror(errno));
close(data->sock); close(data->sock);
return -1; return -1;
} }
@ -265,6 +297,7 @@ static int finishHTTPInit(InputStream * inStream) {
if(ret == 0 || (ret < 0 && errno==EINTR)) return 0; if(ret == 0 || (ret < 0 && errno==EINTR)) return 0;
if(ret < 0) { if(ret < 0) {
DEBUG(__FILE__ ": problem select'ing: %s\n",strerror(errno));
close(data->sock); close(data->sock);
data->connState = HTTP_CONN_STATE_CLOSED; data->connState = HTTP_CONN_STATE_CLOSED;
return -1; return -1;
@ -279,15 +312,15 @@ static int finishHTTPInit(InputStream * inStream) {
memset(request, 0, 2049); memset(request, 0, 2049);
/* deal with ICY metadata later, for now its fucking up stuff! */ /* deal with ICY metadata later, for now its fucking up stuff! */
snprintf(request, 2048, "GET %s HTTP/1.1\r\n" snprintf(request, 2048, "GET %s HTTP/1.0\r\n"
"Host: %s\r\n" "Host: %s\r\n"
"Connection: close\r\n" /*"Connection: close\r\n"*/
"User-Agent: %s/%s\r\n" "User-Agent: %s/%s\r\n"
"Range: bytes=%ld-\r\n" /*"Range: bytes=%ld-\r\n"*/
"Icy-Metadata:1\r\n" "Icy-Metadata:1\r\n"
"\r\n", "\r\n",
data->path, data->host, "httpTest", "0.0.0", data->path, data->host, "httpTest", "0.0.0"/*,
inStream->offset); inStream->offset*/);
ret = write(data->sock, request, strlen(request)); ret = write(data->sock, request, strlen(request));
if(ret!=strlen(request)) { if(ret!=strlen(request)) {

View File

@ -34,6 +34,7 @@
#include "permission.h" #include "permission.h"
#include "replayGain.h" #include "replayGain.h"
#include "inputPlugin.h" #include "inputPlugin.h"
#include "inputStream.h"
#include "../config.h" #include "../config.h"
#include <stdio.h> #include <stdio.h>
@ -420,9 +421,6 @@ int main(int argc, char * argv[]) {
establishListen(&options); establishListen(&options);
/*
* lose privileges as early as possible
*/
changeToUser(&options); changeToUser(&options);
openLogFiles(&options, &out, &err); openLogFiles(&options, &out, &err);
@ -443,6 +441,7 @@ int main(int argc, char * argv[]) {
initPlayerData(); initPlayerData();
initVolume(); initVolume();
initInterfaces(); initInterfaces();
initInputStream();
daemonize(&options); daemonize(&options);