2003-10-13 11:03:14 -04:00
|
|
|
/*
|
|
|
|
* $Id$
|
|
|
|
* Webserver library
|
|
|
|
*
|
2003-12-29 15:41:08 -05:00
|
|
|
* Copyright (C) 2003 Ron Pedde (ron@pedde.com)
|
2003-10-13 11:03:14 -04:00
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
|
2003-10-19 16:03:54 -04:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include "config.h"
|
|
|
|
#endif
|
|
|
|
|
2004-02-14 19:51:11 -05:00
|
|
|
#include <ctype.h>
|
2003-11-03 14:02:00 -05:00
|
|
|
#include <errno.h>
|
2003-10-13 11:03:14 -04:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <limits.h>
|
2003-10-30 17:42:11 -05:00
|
|
|
#include <pthread.h>
|
2003-10-19 16:03:54 -04:00
|
|
|
#include <regex.h>
|
2004-02-14 19:51:11 -05:00
|
|
|
#include <restart.h>
|
2003-10-13 11:03:14 -04:00
|
|
|
#include <stdarg.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2003-10-30 17:42:11 -05:00
|
|
|
#include <time.h>
|
2004-02-14 19:51:11 -05:00
|
|
|
#include <uici.h>
|
2003-10-13 11:03:14 -04:00
|
|
|
|
|
|
|
#include <sys/param.h>
|
2003-10-19 16:03:54 -04:00
|
|
|
#include <sys/types.h>
|
2004-09-18 23:05:18 -04:00
|
|
|
#include <sys/socket.h>
|
2003-10-13 11:03:14 -04:00
|
|
|
|
|
|
|
#include "err.h"
|
|
|
|
#include "webserver.h"
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Defines
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define MAX_HOSTNAME 256
|
2003-10-30 17:42:11 -05:00
|
|
|
#define MAX_LINEBUFFER 1024
|
2003-10-13 11:03:14 -04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Local (private) typedefs
|
|
|
|
*/
|
2003-10-19 16:03:54 -04:00
|
|
|
|
|
|
|
typedef struct tag_ws_handler {
|
|
|
|
regex_t regex;
|
|
|
|
void (*req_handler)(WS_CONNINFO*);
|
|
|
|
int(*auth_handler)(char *, char *);
|
2003-10-30 17:42:11 -05:00
|
|
|
int addheaders;
|
2003-10-19 16:03:54 -04:00
|
|
|
struct tag_ws_handler *next;
|
|
|
|
} WS_HANDLER;
|
|
|
|
|
2004-09-29 23:03:18 -04:00
|
|
|
typedef struct tag_ws_connlist {
|
|
|
|
WS_CONNINFO *pwsc;
|
|
|
|
char *status;
|
|
|
|
struct tag_ws_connlist *next;
|
|
|
|
} WS_CONNLIST;
|
|
|
|
|
2003-10-13 11:03:14 -04:00
|
|
|
typedef struct tag_ws_private {
|
|
|
|
WSCONFIG wsconfig;
|
2003-10-19 16:03:54 -04:00
|
|
|
WS_HANDLER handlers;
|
2004-09-29 23:03:18 -04:00
|
|
|
WS_CONNLIST connlist;
|
2003-10-13 11:03:14 -04:00
|
|
|
int server_fd;
|
|
|
|
int stop;
|
|
|
|
int running;
|
2003-10-19 16:03:54 -04:00
|
|
|
int threadno;
|
2003-11-23 01:10:25 -05:00
|
|
|
int dispatch_threads;
|
2003-10-13 11:03:14 -04:00
|
|
|
pthread_t server_tid;
|
2003-11-23 01:10:25 -05:00
|
|
|
pthread_cond_t exit_cond;
|
|
|
|
pthread_mutex_t exit_mutex;
|
2003-10-13 11:03:14 -04:00
|
|
|
} WS_PRIVATE;
|
|
|
|
|
2004-09-29 23:03:18 -04:00
|
|
|
|
2003-10-13 11:03:14 -04:00
|
|
|
/*
|
|
|
|
* Forwards
|
|
|
|
*/
|
|
|
|
void *ws_mainthread(void*);
|
|
|
|
void *ws_dispatcher(void*);
|
|
|
|
int ws_lock_unsafe(void);
|
|
|
|
int ws_unlock_unsafe(void);
|
|
|
|
void ws_defaulthandler(WS_PRIVATE *pwsp, WS_CONNINFO *pwsc);
|
2003-10-19 16:03:54 -04:00
|
|
|
int ws_addarg(ARGLIST *root, char *key, char *fmt, ...);
|
2003-10-13 11:03:14 -04:00
|
|
|
void ws_freearglist(ARGLIST *root);
|
2004-11-28 03:31:10 -05:00
|
|
|
char *ws_urldecode(char *string, int space_as_plus);
|
2003-10-13 11:03:14 -04:00
|
|
|
int ws_getheaders(WS_CONNINFO *pwsc);
|
2003-10-19 16:03:54 -04:00
|
|
|
int ws_getpostvars(WS_CONNINFO *pwsc);
|
|
|
|
int ws_getgetvars(WS_CONNINFO *pwsc, char *string);
|
|
|
|
char *ws_getarg(ARGLIST *root, char *key);
|
|
|
|
int ws_testarg(ARGLIST *root, char *key, char *value);
|
|
|
|
int ws_findhandler(WS_PRIVATE *pwsp, WS_CONNINFO *pwsc,
|
|
|
|
void(**preq)(WS_CONNINFO*),
|
2003-10-30 17:42:11 -05:00
|
|
|
int(**pauth)(char *, char *),
|
|
|
|
int *addheaders);
|
2003-10-19 16:03:54 -04:00
|
|
|
int ws_registerhandler(WSHANDLE ws, char *regex,
|
|
|
|
void(*handler)(WS_CONNINFO*),
|
2003-10-30 17:42:11 -05:00
|
|
|
int(*auth)(char *, char *),
|
|
|
|
int addheaders);
|
2003-10-19 16:03:54 -04:00
|
|
|
int ws_decodepassword(char *header, char **username, char **password);
|
2003-10-30 17:42:11 -05:00
|
|
|
int ws_testrequestheader(WS_CONNINFO *pwsc, char *header, char *value);
|
2003-12-01 01:55:36 -05:00
|
|
|
char *ws_getrequestheader(WS_CONNINFO *pwsc, char *header);
|
2004-11-28 03:31:10 -05:00
|
|
|
static void ws_add_dispatch_thread(WS_PRIVATE *pwsp, WS_CONNINFO *pwsc);
|
|
|
|
static void ws_remove_dispatch_thread(WS_PRIVATE *pwsp, WS_CONNINFO *pwsc);
|
|
|
|
static int ws_encoding_hack(WS_CONNINFO *pwsc);
|
2003-10-13 11:03:14 -04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Globals
|
|
|
|
*/
|
2003-11-23 01:10:25 -05:00
|
|
|
pthread_mutex_t ws_unsafe=PTHREAD_MUTEX_INITIALIZER;
|
|
|
|
|
2003-10-30 17:42:11 -05:00
|
|
|
char *ws_dow[] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
|
|
|
|
char *ws_moy[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul",
|
|
|
|
"Aug", "Sep", "Oct", "Nov", "Dec" };
|
2003-10-13 11:03:14 -04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* ws_lock_unsafe
|
|
|
|
*
|
|
|
|
* Lock non-thread-safe functions
|
|
|
|
*
|
|
|
|
* returns 0 on success,
|
|
|
|
* returns -1 on failure, with errno set
|
|
|
|
*/
|
|
|
|
int ws_lock_unsafe(void) {
|
|
|
|
int err;
|
2004-11-18 01:58:31 -05:00
|
|
|
int retval=0;
|
|
|
|
|
|
|
|
DPRINTF(E_SPAM,L_WS,"Entering ws_lock_unsafe\n");
|
2003-10-13 11:03:14 -04:00
|
|
|
|
2004-02-14 19:51:11 -05:00
|
|
|
if((err=pthread_mutex_lock(&ws_unsafe))) {
|
2003-10-13 11:03:14 -04:00
|
|
|
errno=err;
|
2004-11-18 01:58:31 -05:00
|
|
|
retval=-1;
|
2003-10-13 11:03:14 -04:00
|
|
|
}
|
|
|
|
|
2004-11-18 01:58:31 -05:00
|
|
|
DPRINTF(E_SPAM,L_WS,"Exiting ws_lock_unsafe with retval of %d\n",retval);
|
|
|
|
return retval;
|
2003-10-13 11:03:14 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* ws_unlock_unsafe
|
|
|
|
*
|
|
|
|
* Lock non-thread-safe functions
|
|
|
|
*
|
|
|
|
* returns 0 on success,
|
|
|
|
* returns -1 on failure, with errno set
|
|
|
|
*/
|
|
|
|
int ws_unlock_unsafe(void) {
|
|
|
|
int err;
|
2004-11-18 01:58:31 -05:00
|
|
|
int retval=0;
|
|
|
|
|
|
|
|
DPRINTF(E_SPAM,L_WS,"Entering ws_unlock_unsafe\n");
|
2003-10-13 11:03:14 -04:00
|
|
|
|
2004-02-14 19:51:11 -05:00
|
|
|
if((err=pthread_mutex_unlock(&ws_unsafe))) {
|
2003-10-13 11:03:14 -04:00
|
|
|
errno=err;
|
2004-11-18 01:58:31 -05:00
|
|
|
retval=-1;
|
2003-10-13 11:03:14 -04:00
|
|
|
}
|
|
|
|
|
2004-11-18 01:58:31 -05:00
|
|
|
DPRINTF(E_SPAM,L_WS,"Exiting ws_unlock_unsafe with a retval of %d\n",retval);
|
|
|
|
return retval;
|
2003-10-13 11:03:14 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* ws_start
|
|
|
|
*
|
|
|
|
* Start the main webserver thread. Should really
|
|
|
|
* bind and listen to the port before spawning the thread,
|
|
|
|
* since it will be hard to detect and return the error unless
|
|
|
|
* we listen first
|
|
|
|
*
|
|
|
|
* RETURNS
|
|
|
|
* Success: WSHANDLE
|
|
|
|
* Failure: NULL, with errno set
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
WSHANDLE ws_start(WSCONFIG *config) {
|
|
|
|
int err;
|
|
|
|
WS_PRIVATE *pwsp;
|
|
|
|
|
2004-11-18 01:58:31 -05:00
|
|
|
DPRINTF(E_SPAM,L_WS,"Entering ws_start\n");
|
|
|
|
|
|
|
|
if((pwsp=(WS_PRIVATE*)malloc(sizeof(WS_PRIVATE))) == NULL) {
|
|
|
|
DPRINTF(E_SPAM,L_WS,"Malloc error: %s\n",strerror(errno));
|
2003-10-13 11:03:14 -04:00
|
|
|
return NULL;
|
2004-11-18 01:58:31 -05:00
|
|
|
}
|
2003-10-13 11:03:14 -04:00
|
|
|
|
|
|
|
memcpy(&pwsp->wsconfig,config,sizeof(WS_PRIVATE));
|
2004-09-29 23:03:18 -04:00
|
|
|
pwsp->connlist.next=NULL;
|
2003-10-13 11:03:14 -04:00
|
|
|
pwsp->running=0;
|
2003-10-19 16:03:54 -04:00
|
|
|
pwsp->threadno=0;
|
2003-11-23 13:13:35 -05:00
|
|
|
pwsp->stop=0;
|
2003-11-23 01:10:25 -05:00
|
|
|
pwsp->dispatch_threads=0;
|
2003-10-19 16:03:54 -04:00
|
|
|
pwsp->handlers.next=NULL;
|
2003-10-13 11:03:14 -04:00
|
|
|
|
2004-02-14 19:51:11 -05:00
|
|
|
if((err=pthread_cond_init(&pwsp->exit_cond, NULL))) {
|
2003-11-23 01:10:25 -05:00
|
|
|
errno=err;
|
2004-11-18 01:58:31 -05:00
|
|
|
DPRINTF(E_LOG,L_WS,"Error in pthread_cond_init: %s\n",strerror(errno));
|
2003-11-23 01:10:25 -05:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2004-02-14 19:51:11 -05:00
|
|
|
if((err=pthread_mutex_init(&pwsp->exit_mutex,NULL))) {
|
2003-11-23 01:10:25 -05:00
|
|
|
errno=err;
|
2004-11-18 01:58:31 -05:00
|
|
|
DPRINTF(E_LOG,L_WS,"Error in pthread_mutex_init: %s\n",strerror(errno));
|
2003-11-23 01:10:25 -05:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2004-11-13 02:14:26 -05:00
|
|
|
DPRINTF(E_INF,L_WS,"Preparing to listen on port %d\n",pwsp->wsconfig.port);
|
2003-10-13 11:03:14 -04:00
|
|
|
|
|
|
|
if((pwsp->server_fd = u_open(pwsp->wsconfig.port)) == -1) {
|
|
|
|
err=errno;
|
2004-11-18 01:58:31 -05:00
|
|
|
DPRINTF(E_LOG,L_WS,"Could not open port: %s\n",strerror(errno));
|
2003-10-13 11:03:14 -04:00
|
|
|
errno=err;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2004-11-13 02:14:26 -05:00
|
|
|
DPRINTF(E_INF,L_WS,"Starting server thread\n");
|
2004-02-14 19:51:11 -05:00
|
|
|
if((err=pthread_create(&pwsp->server_tid,NULL,ws_mainthread,(void*)pwsp))) {
|
2004-11-18 01:58:31 -05:00
|
|
|
DPRINTF(E_LOG,L_WS,"Could not spawn thread: %s\n",strerror(err));
|
2003-11-23 13:13:35 -05:00
|
|
|
r_close(pwsp->server_fd);
|
2003-10-13 11:03:14 -04:00
|
|
|
errno=err;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* we're really running */
|
|
|
|
pwsp->running=1;
|
|
|
|
|
2004-11-18 01:58:31 -05:00
|
|
|
DPRINTF(E_SPAM,L_WS,"Exiting ws_start\n");
|
2003-10-13 11:03:14 -04:00
|
|
|
return (WSHANDLE)pwsp;
|
|
|
|
}
|
|
|
|
|
2004-09-29 23:03:18 -04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* ws_remove_dispatch_thread
|
|
|
|
*
|
|
|
|
* remove a dispatch thread from the thread list
|
|
|
|
*/
|
|
|
|
void ws_remove_dispatch_thread(WS_PRIVATE *pwsp, WS_CONNINFO *pwsc) {
|
|
|
|
WS_CONNLIST *pHead, *pTail;
|
|
|
|
|
2004-11-18 01:58:31 -05:00
|
|
|
DPRINTF(E_SPAM,L_WS,"Entering ws_remove_dispatch_thread\n");
|
|
|
|
|
2004-09-29 23:03:18 -04:00
|
|
|
if(pthread_mutex_lock(&pwsp->exit_mutex))
|
2004-11-13 02:14:26 -05:00
|
|
|
DPRINTF(E_FATAL,L_WS,"Cannot lock condition mutex\n");
|
2004-09-29 23:03:18 -04:00
|
|
|
|
2004-10-10 08:25:36 -04:00
|
|
|
pTail=&(pwsp->connlist);
|
|
|
|
pHead=pwsp->connlist.next;
|
2004-09-29 23:03:18 -04:00
|
|
|
|
|
|
|
while((pHead) && (pHead->pwsc != pwsc)) {
|
|
|
|
pTail=pHead;
|
|
|
|
pHead=pHead->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(pHead) {
|
|
|
|
pwsp->dispatch_threads--;
|
2004-11-13 02:14:26 -05:00
|
|
|
DPRINTF(E_DBG,L_WS,"With thread %d exiting, %d are still running\n",
|
2004-09-29 23:03:18 -04:00
|
|
|
pwsc->threadno,pwsp->dispatch_threads);
|
|
|
|
|
|
|
|
pTail->next = pHead->next;
|
|
|
|
|
|
|
|
if(pHead->status)
|
|
|
|
free(pHead->status);
|
|
|
|
free(pHead);
|
|
|
|
|
|
|
|
/* signal condition in case something is waiting */
|
|
|
|
pthread_cond_signal(&pwsp->exit_cond);
|
|
|
|
}
|
|
|
|
|
|
|
|
pthread_mutex_unlock(&pwsp->exit_mutex);
|
2004-11-18 01:58:31 -05:00
|
|
|
DPRINTF(E_SPAM,L_WS,"Exiting ws_remote_dispatch_thread\n");
|
2004-09-29 23:03:18 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* ws_add_dispatch_thread
|
|
|
|
*
|
|
|
|
* Add a thread to the dispatch thread list
|
|
|
|
*/
|
|
|
|
void ws_add_dispatch_thread(WS_PRIVATE *pwsp, WS_CONNINFO *pwsc) {
|
|
|
|
WS_CONNLIST *pNew;
|
|
|
|
|
2004-11-18 01:58:31 -05:00
|
|
|
DPRINTF(E_SPAM,L_WS,"Entering ws_add_dispatch_thread\n");
|
|
|
|
|
2004-09-29 23:03:18 -04:00
|
|
|
pNew=(WS_CONNLIST*)malloc(sizeof(WS_CONNLIST));
|
|
|
|
pNew->next=NULL;
|
|
|
|
pNew->pwsc=pwsc;
|
|
|
|
pNew->status=strdup("Initializing");
|
|
|
|
|
|
|
|
if(!pNew)
|
2004-11-13 02:14:26 -05:00
|
|
|
DPRINTF(E_FATAL,L_WS,"Malloc: %s\n",strerror(errno));
|
2004-09-29 23:03:18 -04:00
|
|
|
|
|
|
|
if(pthread_mutex_lock(&pwsp->exit_mutex))
|
2004-11-13 02:14:26 -05:00
|
|
|
DPRINTF(E_FATAL,L_WS,"Cannot lock condition mutex\n");
|
2004-09-29 23:03:18 -04:00
|
|
|
|
|
|
|
/* list is locked... */
|
|
|
|
pwsp->dispatch_threads++;
|
|
|
|
pNew->next = pwsp->connlist.next;
|
|
|
|
pwsp->connlist.next = pNew;
|
|
|
|
|
|
|
|
pthread_mutex_unlock(&pwsp->exit_mutex);
|
2004-11-18 01:58:31 -05:00
|
|
|
DPRINTF(E_SPAM,L_WS,"Exiting ws_add_dispatch_thread\n");
|
2004-09-29 23:03:18 -04:00
|
|
|
}
|
|
|
|
|
2003-10-13 11:03:14 -04:00
|
|
|
/*
|
|
|
|
* ws_stop
|
|
|
|
*
|
|
|
|
* Stop the web server and all the child threads
|
|
|
|
*/
|
2003-11-23 01:10:25 -05:00
|
|
|
extern int ws_stop(WSHANDLE ws) {
|
|
|
|
WS_PRIVATE *pwsp = (WS_PRIVATE*)ws;
|
2003-11-23 13:13:35 -05:00
|
|
|
WS_HANDLER *current;
|
2004-09-29 23:03:18 -04:00
|
|
|
WS_CONNLIST *pcl;
|
|
|
|
void *result;
|
2003-10-13 11:03:14 -04:00
|
|
|
|
2004-11-18 01:58:31 -05:00
|
|
|
DPRINTF(E_DBG,L_WS,"Entering ws_stop: %d threads\n",pwsp->dispatch_threads);
|
2004-09-18 23:05:18 -04:00
|
|
|
|
2003-10-19 16:03:54 -04:00
|
|
|
/* free the ws_handlers */
|
2003-11-23 13:13:35 -05:00
|
|
|
while(pwsp->handlers.next) {
|
|
|
|
current=pwsp->handlers.next;
|
|
|
|
pwsp->handlers.next=current->next;
|
2004-09-19 02:00:43 -04:00
|
|
|
regfree(¤t->regex);
|
2003-11-23 13:13:35 -05:00
|
|
|
free(current);
|
|
|
|
}
|
|
|
|
|
2003-11-23 01:10:25 -05:00
|
|
|
pwsp->stop=1;
|
2003-11-23 13:13:35 -05:00
|
|
|
pwsp->running=0;
|
|
|
|
|
2004-11-13 02:14:26 -05:00
|
|
|
DPRINTF(E_DBG,L_WS,"ws_stop: closing the server fd\n");
|
2004-09-18 23:05:18 -04:00
|
|
|
shutdown(pwsp->server_fd,SHUT_RDWR);
|
2003-11-23 13:13:35 -05:00
|
|
|
r_close(pwsp->server_fd); /* this should tick off the listener */
|
2004-09-29 23:03:18 -04:00
|
|
|
|
|
|
|
/* wait for the server thread to terminate. SHould be quick! */
|
|
|
|
pthread_join(pwsp->server_tid,&result);
|
2003-11-23 01:10:25 -05:00
|
|
|
|
2004-09-29 23:03:18 -04:00
|
|
|
/* Give the threads an extra push */
|
2003-11-23 01:10:25 -05:00
|
|
|
if(pthread_mutex_lock(&pwsp->exit_mutex))
|
2004-11-13 02:14:26 -05:00
|
|
|
DPRINTF(E_FATAL,L_WS,"Cannot lock condition mutex\n");
|
2003-11-23 01:10:25 -05:00
|
|
|
|
2004-09-29 23:03:18 -04:00
|
|
|
pcl=pwsp->connlist.next;
|
|
|
|
|
|
|
|
/* Closing the client sockets out from under the dispatch threads
|
|
|
|
* should cause the dispatch threads to exit out with an error.
|
|
|
|
*/
|
|
|
|
while(pcl) {
|
|
|
|
if(pcl->pwsc->fd) {
|
|
|
|
shutdown(pcl->pwsc->fd,SHUT_RDWR);
|
|
|
|
r_close(pcl->pwsc->fd);
|
|
|
|
}
|
|
|
|
pcl=pcl->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* wait for the threads to be done */
|
2003-11-23 01:10:25 -05:00
|
|
|
while(pwsp->dispatch_threads) {
|
2004-11-13 02:14:26 -05:00
|
|
|
DPRINTF(E_DBG,L_WS,"ws_stop: I still see %d threads\n",pwsp->dispatch_threads);
|
2003-11-23 01:10:25 -05:00
|
|
|
pthread_cond_wait(&pwsp->exit_cond, &pwsp->exit_mutex);
|
|
|
|
}
|
|
|
|
|
|
|
|
pthread_mutex_unlock(&pwsp->exit_mutex);
|
2003-11-23 13:13:35 -05:00
|
|
|
|
|
|
|
free(pwsp);
|
2004-11-18 01:58:31 -05:00
|
|
|
|
|
|
|
DPRINTF(E_SPAM,L_WS,"Exiting ws_stop\n");
|
2003-10-13 11:03:14 -04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* ws_mainthread
|
|
|
|
*
|
|
|
|
* Main thread for webserver - this accepts connections
|
|
|
|
* and spawns a handler thread for each incoming connection.
|
|
|
|
*
|
|
|
|
* For a persistant connection, these threads will be
|
|
|
|
* long-lived, otherwise, they will terminate as soon as
|
|
|
|
* the request has been honored.
|
|
|
|
*
|
|
|
|
* These client threads will, of course, be detached
|
|
|
|
*/
|
|
|
|
void *ws_mainthread(void *arg) {
|
|
|
|
int fd;
|
|
|
|
int err;
|
|
|
|
WS_PRIVATE *pwsp = (WS_PRIVATE*)arg;
|
|
|
|
WS_CONNINFO *pwsc;
|
|
|
|
pthread_t tid;
|
|
|
|
char hostname[MAX_HOSTNAME];
|
|
|
|
|
2004-11-18 01:58:31 -05:00
|
|
|
DPRINTF(E_SPAM,L_WS,"Entering ws_mainthread\n");
|
|
|
|
|
2003-10-13 11:03:14 -04:00
|
|
|
while(1) {
|
|
|
|
pwsc=(WS_CONNINFO*)malloc(sizeof(WS_CONNINFO));
|
|
|
|
if(!pwsc) {
|
|
|
|
/* can't very well service any more threads! */
|
2004-11-13 02:14:26 -05:00
|
|
|
DPRINTF(E_FATAL,L_WS,"Error: %s\n",strerror(errno));
|
2003-10-13 11:03:14 -04:00
|
|
|
pwsp->running=0;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
memset(pwsc,0,sizeof(WS_CONNINFO));
|
|
|
|
|
|
|
|
if((fd=u_accept(pwsp->server_fd,hostname,MAX_HOSTNAME)) == -1) {
|
2004-11-18 01:58:31 -05:00
|
|
|
DPRINTF(E_LOG,L_WS,"Dispatcher: accept failed: %s\n",strerror(errno));
|
2004-09-29 23:03:18 -04:00
|
|
|
shutdown(pwsp->server_fd,SHUT_RDWR);
|
2003-11-23 13:13:35 -05:00
|
|
|
r_close(pwsp->server_fd);
|
2003-10-13 11:03:14 -04:00
|
|
|
pwsp->running=0;
|
2003-11-23 13:13:35 -05:00
|
|
|
free(pwsc);
|
2003-11-26 01:12:25 -05:00
|
|
|
|
2004-11-18 01:58:31 -05:00
|
|
|
DPRINTF(E_FATAL,L_WS,"Dispatcher: Aborting\n");
|
2003-10-13 11:03:14 -04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
pwsc->hostname=strdup(hostname);
|
|
|
|
pwsc->fd=fd;
|
2003-11-20 16:58:22 -05:00
|
|
|
pwsc->pwsp = pwsp;
|
2003-10-13 11:03:14 -04:00
|
|
|
|
|
|
|
/* Spawn off a dispatcher to decide what to do with
|
|
|
|
* the request
|
|
|
|
*/
|
|
|
|
|
2003-11-20 16:58:22 -05:00
|
|
|
/* don't really care if it locks or not */
|
|
|
|
ws_lock_unsafe();
|
|
|
|
pwsc->threadno=pwsp->threadno;
|
|
|
|
pwsp->threadno++;
|
|
|
|
ws_unlock_unsafe();
|
|
|
|
|
|
|
|
/* now, throw off a dispatch thread */
|
2004-02-14 19:51:11 -05:00
|
|
|
if((err=pthread_create(&tid,NULL,ws_dispatcher,(void*)pwsc))) {
|
2003-11-20 16:58:22 -05:00
|
|
|
pwsc->error=err;
|
2004-11-18 01:58:31 -05:00
|
|
|
DPRINTF(E_FATAL,L_WS,"Could not spawn thread: %s\n",strerror(err));
|
2003-11-20 16:58:22 -05:00
|
|
|
ws_close(pwsc);
|
2003-11-23 01:10:25 -05:00
|
|
|
} else {
|
2004-09-29 23:03:18 -04:00
|
|
|
ws_add_dispatch_thread(pwsp,pwsc);
|
2003-11-23 01:10:25 -05:00
|
|
|
pthread_detach(tid);
|
2003-10-13 11:03:14 -04:00
|
|
|
}
|
|
|
|
}
|
2004-11-18 01:58:31 -05:00
|
|
|
|
|
|
|
DPRINTF(E_SPAM,L_WS,"Exiting ws_mainthred\n");
|
2003-10-13 11:03:14 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* ws_close
|
|
|
|
*
|
|
|
|
* Close the connection. This might be called when things
|
|
|
|
* are already in bad shape, so we'll ignore errors and let
|
|
|
|
* them be detected back in either the dispatch
|
|
|
|
* thread or the main server thread
|
|
|
|
*
|
|
|
|
* Mainly, we just want to make sure that any
|
|
|
|
* allocated memory has been freed
|
|
|
|
*/
|
|
|
|
void ws_close(WS_CONNINFO *pwsc) {
|
2003-11-23 01:10:25 -05:00
|
|
|
WS_PRIVATE *pwsp = (WS_PRIVATE *)pwsc->pwsp;
|
|
|
|
|
2004-11-18 01:58:31 -05:00
|
|
|
|
|
|
|
DPRINTF(E_SPAM,L_WS,"Entering ws_close\n");
|
|
|
|
|
2004-06-14 15:01:06 -04:00
|
|
|
/* DWB: update the status so it doesn't fill up with no longer
|
|
|
|
relevant entries */
|
2005-03-11 01:39:40 -05:00
|
|
|
|
|
|
|
/* FIXME: status handling should be done with a callback or something */
|
|
|
|
|
2004-06-14 15:01:06 -04:00
|
|
|
config_set_status(pwsc, 0, NULL);
|
|
|
|
|
2004-11-13 02:14:26 -05:00
|
|
|
DPRINTF(E_DBG,L_WS,"Thread %d: Terminating\n",pwsc->threadno);
|
|
|
|
DPRINTF(E_DBG,L_WS,"Thread %d: Freeing request headers\n",pwsc->threadno);
|
2003-10-13 11:03:14 -04:00
|
|
|
ws_freearglist(&pwsc->request_headers);
|
2004-11-13 02:14:26 -05:00
|
|
|
DPRINTF(E_DBG,L_WS,"Thread %d: Freeing response headers\n",pwsc->threadno);
|
2003-10-13 11:03:14 -04:00
|
|
|
ws_freearglist(&pwsc->response_headers);
|
2004-11-13 02:14:26 -05:00
|
|
|
DPRINTF(E_DBG,L_WS,"Thread %d: Freeing request vars\n",pwsc->threadno);
|
2003-10-13 11:03:14 -04:00
|
|
|
ws_freearglist(&pwsc->request_vars);
|
2004-01-04 00:27:43 -05:00
|
|
|
if(pwsc->uri) {
|
2003-11-23 13:13:35 -05:00
|
|
|
free(pwsc->uri);
|
2004-01-04 00:27:43 -05:00
|
|
|
pwsc->uri=NULL;
|
|
|
|
}
|
2003-10-13 11:03:14 -04:00
|
|
|
|
2003-10-19 16:03:54 -04:00
|
|
|
if((pwsc->close)||(pwsc->error)) {
|
2004-11-13 02:14:26 -05:00
|
|
|
DPRINTF(E_DBG,L_WS,"Thread %d: Closing fd\n",pwsc->threadno);
|
2004-09-29 23:03:18 -04:00
|
|
|
shutdown(pwsc->fd,SHUT_RDWR);
|
2003-11-23 13:13:35 -05:00
|
|
|
r_close(pwsc->fd);
|
2003-10-19 16:03:54 -04:00
|
|
|
free(pwsc->hostname);
|
2003-11-23 01:10:25 -05:00
|
|
|
|
|
|
|
/* this thread is done */
|
2004-09-29 23:03:18 -04:00
|
|
|
ws_remove_dispatch_thread(pwsp, pwsc);
|
2003-11-23 01:10:25 -05:00
|
|
|
|
2003-10-19 16:03:54 -04:00
|
|
|
free(pwsc);
|
2004-11-18 01:58:31 -05:00
|
|
|
DPRINTF(E_SPAM,L_WS,"Exiting ws_close (thread terminating)\n");
|
2004-09-29 23:03:18 -04:00
|
|
|
pthread_exit(NULL);
|
2003-10-19 16:03:54 -04:00
|
|
|
}
|
2004-11-18 01:58:31 -05:00
|
|
|
DPRINTF(E_SPAM,L_WS,"Exiting ws_close (thread continuing)\n");
|
2003-10-13 11:03:14 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* ws_freearglist
|
|
|
|
*
|
|
|
|
* Walks through an arg list freeing as it goes
|
|
|
|
*/
|
|
|
|
void ws_freearglist(ARGLIST *root) {
|
|
|
|
ARGLIST *current;
|
|
|
|
|
2004-11-18 01:58:31 -05:00
|
|
|
DPRINTF(E_SPAM,L_WS,"Entering ws_freearglist\n");
|
|
|
|
|
2003-10-13 11:03:14 -04:00
|
|
|
while(root->next) {
|
|
|
|
free(root->next->key);
|
|
|
|
free(root->next->value);
|
|
|
|
current=root->next;
|
|
|
|
root->next=current->next;
|
|
|
|
free(current);
|
|
|
|
}
|
2004-11-18 01:58:31 -05:00
|
|
|
|
|
|
|
DPRINTF(E_SPAM,L_WS,"Exiting ws_freearglist\n");
|
2003-10-13 11:03:14 -04:00
|
|
|
}
|
|
|
|
|
2003-10-19 16:03:54 -04:00
|
|
|
|
|
|
|
void ws_emitheaders(WS_CONNINFO *pwsc) {
|
|
|
|
ARGLIST *pcurrent=pwsc->response_headers.next;
|
|
|
|
|
2004-11-18 01:58:31 -05:00
|
|
|
DPRINTF(E_SPAM,L_WS,"Entering ws_emitheaders\n");
|
2003-10-19 16:03:54 -04:00
|
|
|
while(pcurrent) {
|
2004-11-13 02:14:26 -05:00
|
|
|
DPRINTF(E_DBG,L_WS,"Emitting reponse header %s: %s\n",pcurrent->key,
|
2003-10-19 16:03:54 -04:00
|
|
|
pcurrent->value);
|
|
|
|
ws_writefd(pwsc,"%s: %s\r\n",pcurrent->key,pcurrent->value);
|
|
|
|
pcurrent=pcurrent->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
ws_writefd(pwsc,"\r\n");
|
2004-11-18 01:58:31 -05:00
|
|
|
DPRINTF(E_SPAM,L_WS,"Exitin ws_emitheaders\n");
|
2003-10-19 16:03:54 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* ws_getpostvars
|
|
|
|
*
|
|
|
|
* Receive and parse headers. These will trump
|
|
|
|
* get headers
|
|
|
|
*/
|
|
|
|
int ws_getpostvars(WS_CONNINFO *pwsc) {
|
|
|
|
char *content_length;
|
|
|
|
int length;
|
|
|
|
char *buffer;
|
|
|
|
|
2004-11-18 01:58:31 -05:00
|
|
|
DPRINTF(E_SPAM,L_WS,"Entering ws_getpostvars\n");
|
|
|
|
|
2003-10-19 16:03:54 -04:00
|
|
|
content_length = ws_getarg(&pwsc->request_headers,"Content-Length");
|
|
|
|
if(!content_length) {
|
|
|
|
pwsc->error = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
length=atoi(content_length);
|
2004-11-13 02:14:26 -05:00
|
|
|
DPRINTF(E_DBG,L_WS,"Thread %d: Post var length: %d\n",
|
2003-10-19 16:03:54 -04:00
|
|
|
pwsc->threadno,length);
|
|
|
|
|
|
|
|
buffer=(char*)malloc(length+1);
|
|
|
|
|
|
|
|
if(!buffer) {
|
|
|
|
pwsc->error = errno;
|
2004-11-13 02:14:26 -05:00
|
|
|
DPRINTF(E_INF,L_WS,"Thread %d: Could not malloc %d bytes\n",
|
2003-10-19 16:03:54 -04:00
|
|
|
pwsc->threadno, length);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2004-05-21 09:56:04 -04:00
|
|
|
// make the read time out 30 minutes like we said in the
|
|
|
|
// /server-info response
|
|
|
|
if((readtimed(pwsc->fd, buffer, length, 1800.0)) == -1) {
|
2004-11-13 02:14:26 -05:00
|
|
|
DPRINTF(E_INF,L_WS,"Thread %d: Timeout reading post vars\n",
|
2003-10-19 16:03:54 -04:00
|
|
|
pwsc->threadno);
|
|
|
|
pwsc->error=errno;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2004-11-13 02:14:26 -05:00
|
|
|
DPRINTF(E_DBG,L_WS,"Thread %d: Read post vars: %s\n",pwsc->threadno,buffer);
|
2003-10-19 16:03:54 -04:00
|
|
|
|
|
|
|
pwsc->error=ws_getgetvars(pwsc,buffer);
|
|
|
|
|
|
|
|
free(buffer);
|
2004-11-18 01:58:31 -05:00
|
|
|
|
|
|
|
DPRINTF(E_SPAM,L_WS,"Exiting ws_getpostvars\n");
|
2003-10-19 16:03:54 -04:00
|
|
|
return pwsc->error;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-10-13 11:03:14 -04:00
|
|
|
/*
|
|
|
|
* ws_getheaders
|
|
|
|
*
|
|
|
|
* Receive and parse headers. This is called from
|
|
|
|
* ws_dispatcher
|
|
|
|
*/
|
|
|
|
int ws_getheaders(WS_CONNINFO *pwsc) {
|
|
|
|
char *first, *last;
|
|
|
|
int done;
|
|
|
|
char buffer[MAX_LINEBUFFER];
|
|
|
|
|
2004-11-18 01:58:31 -05:00
|
|
|
DPRINTF(E_SPAM,L_WS,"Entering ws_getheaders\n");
|
|
|
|
|
2003-10-13 11:03:14 -04:00
|
|
|
/* Break down the headers into some kind of header list */
|
|
|
|
done=0;
|
|
|
|
while(!done) {
|
|
|
|
if(readline(pwsc->fd,buffer,sizeof(buffer)) == -1) {
|
2003-10-19 16:03:54 -04:00
|
|
|
pwsc->error=errno;
|
2004-11-13 02:14:26 -05:00
|
|
|
DPRINTF(E_INF,L_WS,"Thread %d: Unexpected close\n",pwsc->threadno);
|
2003-10-13 11:03:14 -04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2004-11-13 02:14:26 -05:00
|
|
|
DPRINTF(E_DBG,L_WS,"Thread %d: Read: %s",pwsc->threadno,buffer);
|
2003-10-13 11:03:14 -04:00
|
|
|
|
|
|
|
first=buffer;
|
|
|
|
if(buffer[0] == '\r')
|
|
|
|
first=&buffer[1];
|
|
|
|
|
|
|
|
/* trim the trailing \n */
|
|
|
|
if(first[strlen(first)-1] == '\n')
|
|
|
|
first[strlen(first)-1] = '\0';
|
|
|
|
|
|
|
|
if(strlen(first) == 0) {
|
2004-11-13 02:14:26 -05:00
|
|
|
DPRINTF(E_DBG,L_WS,"Thread %d: Headers parsed!\n",pwsc->threadno);
|
2003-10-13 11:03:14 -04:00
|
|
|
done=1;
|
|
|
|
} else {
|
|
|
|
/* we have a header! */
|
|
|
|
last=first;
|
|
|
|
strsep(&last,":");
|
|
|
|
|
|
|
|
if(last==first) {
|
2004-11-13 02:14:26 -05:00
|
|
|
DPRINTF(E_WARN,L_WS,"Thread %d: Invalid header: %s\n",
|
2003-10-19 16:03:54 -04:00
|
|
|
pwsc->threadno,first);
|
2003-10-13 11:03:14 -04:00
|
|
|
} else {
|
|
|
|
while(*last==' ')
|
|
|
|
last++;
|
|
|
|
|
2003-10-19 16:03:54 -04:00
|
|
|
while(last[strlen(last)-1] == '\r')
|
|
|
|
last[strlen(last)-1] = '\0';
|
|
|
|
|
2004-11-13 02:14:26 -05:00
|
|
|
DPRINTF(E_DBG,L_WS,"Thread %d: Adding header *%s=%s*\n",
|
2003-10-19 16:03:54 -04:00
|
|
|
pwsc->threadno,first,last);
|
|
|
|
|
2004-06-14 15:01:06 -04:00
|
|
|
if(ws_addarg(&pwsc->request_headers,first,"%s",last)) {
|
2004-11-13 02:14:26 -05:00
|
|
|
DPRINTF(E_FATAL,L_WS,"Thread %d: Out of memory\n",
|
2003-10-19 16:03:54 -04:00
|
|
|
pwsc->threadno);
|
|
|
|
pwsc->error=ENOMEM;
|
2003-10-13 11:03:14 -04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-11-18 01:58:31 -05:00
|
|
|
DPRINTF(E_SPAM,L_WS,"Exiting ws_getheaders\n");
|
2003-10-13 11:03:14 -04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-11-28 03:31:10 -05:00
|
|
|
|
|
|
|
/*
|
|
|
|
* ws_encoding_hack
|
|
|
|
*/
|
|
|
|
int ws_encoding_hack(WS_CONNINFO *pwsc) {
|
|
|
|
char *user_agent;
|
|
|
|
int space_as_plus=1;
|
|
|
|
|
|
|
|
user_agent=ws_getrequestheader(pwsc, "user-agent");
|
|
|
|
if(user_agent) {
|
|
|
|
if(strncasecmp(user_agent,"Roku",4) == 0)
|
|
|
|
space_as_plus=0;
|
|
|
|
if(strncasecmp(user_agent,"iTunes",6) == 0)
|
|
|
|
space_as_plus=0;
|
|
|
|
}
|
|
|
|
return space_as_plus;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-10-19 16:03:54 -04:00
|
|
|
/*
|
|
|
|
* ws_getgetvars
|
|
|
|
*
|
|
|
|
* parse a GET string of variables (or POST)
|
2004-09-07 23:48:09 -04:00
|
|
|
*
|
2003-10-19 16:03:54 -04:00
|
|
|
*/
|
|
|
|
int ws_getgetvars(WS_CONNINFO *pwsc, char *string) {
|
|
|
|
char *first, *last, *middle;
|
2004-11-05 23:44:20 -05:00
|
|
|
char *key, *value;
|
2003-10-19 16:03:54 -04:00
|
|
|
int done;
|
|
|
|
|
2004-11-28 03:31:10 -05:00
|
|
|
int space_as_plus;
|
|
|
|
|
2004-11-18 01:58:31 -05:00
|
|
|
DPRINTF(E_DBG,L_WS,"Thread %d: Entering ws_getgetvars (%s)\n",
|
2003-10-19 16:03:54 -04:00
|
|
|
pwsc->threadno,string);
|
|
|
|
|
2004-11-28 03:31:10 -05:00
|
|
|
space_as_plus=ws_encoding_hack(pwsc);
|
|
|
|
|
2003-10-19 16:03:54 -04:00
|
|
|
done=0;
|
|
|
|
|
2004-11-05 23:44:20 -05:00
|
|
|
first=string;
|
2003-10-19 16:03:54 -04:00
|
|
|
|
|
|
|
while((!done) && (first)) {
|
|
|
|
last=middle=first;
|
|
|
|
strsep(&last,"&");
|
|
|
|
strsep(&middle,"=");
|
|
|
|
|
|
|
|
if(!middle) {
|
2004-11-13 02:14:26 -05:00
|
|
|
DPRINTF(E_WARN,L_WS,"Thread %d: Bad arg: %s\n",
|
2003-10-19 16:03:54 -04:00
|
|
|
pwsc->threadno,first);
|
|
|
|
} else {
|
2004-11-28 03:31:10 -05:00
|
|
|
key=ws_urldecode(first,space_as_plus);
|
|
|
|
value=ws_urldecode(middle,space_as_plus);
|
2004-11-05 23:44:20 -05:00
|
|
|
|
2004-11-13 02:14:26 -05:00
|
|
|
DPRINTF(E_DBG,L_WS,"Thread %d: Adding arg %s = %s\n",
|
2004-11-05 23:44:20 -05:00
|
|
|
pwsc->threadno,key,value);
|
|
|
|
ws_addarg(&pwsc->request_vars,key,"%s",value);
|
|
|
|
|
|
|
|
free(key);
|
|
|
|
free(value);
|
2003-10-19 16:03:54 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if(!last) {
|
2004-11-13 02:14:26 -05:00
|
|
|
DPRINTF(E_DBG,L_WS,"Thread %d: Done parsing GET/POST args!\n",
|
2003-10-19 16:03:54 -04:00
|
|
|
pwsc->threadno);
|
|
|
|
done=1;
|
|
|
|
} else {
|
|
|
|
first=last;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-11-18 01:58:31 -05:00
|
|
|
DPRINTF(E_SPAM,L_WS,"Exiting ws_getgetvars\n");
|
2003-10-19 16:03:54 -04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2003-10-13 11:03:14 -04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* ws_dispatcher
|
|
|
|
*
|
|
|
|
* Main dispatch thread. This gets the request, reads the
|
|
|
|
* headers, decodes the GET'd or POST'd variables,
|
|
|
|
* then decides what function should service the request
|
|
|
|
*/
|
|
|
|
void *ws_dispatcher(void *arg) {
|
2003-11-20 16:58:22 -05:00
|
|
|
WS_CONNINFO *pwsc=(WS_CONNINFO*)arg;
|
|
|
|
WS_PRIVATE *pwsp=pwsc->pwsp;
|
2003-10-13 11:03:14 -04:00
|
|
|
char buffer[MAX_LINEBUFFER];
|
2003-11-23 13:13:35 -05:00
|
|
|
char *first,*last;
|
2003-10-19 16:03:54 -04:00
|
|
|
int connection_done=0;
|
|
|
|
int can_dispatch;
|
|
|
|
char *auth, *username, *password;
|
2003-10-30 17:42:11 -05:00
|
|
|
int hdrs,handler;
|
|
|
|
time_t now;
|
|
|
|
struct tm now_tm;
|
2003-10-19 16:03:54 -04:00
|
|
|
void (*req_handler)(WS_CONNINFO*);
|
|
|
|
int(*auth_handler)(char *, char *);
|
2003-10-13 11:03:14 -04:00
|
|
|
|
2004-11-18 01:58:31 -05:00
|
|
|
DPRINTF(E_DBG,L_WS,"Thread %d: Entering ws_dispatcher (Connection from %s)\n",
|
|
|
|
pwsc->threadno, pwsc->hostname);
|
2003-10-19 16:03:54 -04:00
|
|
|
|
|
|
|
while(!connection_done) {
|
|
|
|
/* Now, get the request from the other end
|
|
|
|
* and decide where to dispatch it
|
|
|
|
*/
|
2003-10-13 11:03:14 -04:00
|
|
|
|
2004-06-14 15:01:06 -04:00
|
|
|
/* DWB: set timeout to 30 minutes as advertised in the
|
|
|
|
server-info response. */
|
|
|
|
if((readlinetimed(pwsc->fd,buffer,sizeof(buffer),1800.0)) < 1) {
|
2003-10-19 16:03:54 -04:00
|
|
|
pwsc->error=errno;
|
|
|
|
pwsc->close=1;
|
2004-11-13 02:14:26 -05:00
|
|
|
DPRINTF(E_WARN,L_WS,"Thread %d: could not read: %s\n",
|
2003-10-19 16:03:54 -04:00
|
|
|
pwsc->threadno,strerror(errno));
|
|
|
|
ws_close(pwsc);
|
|
|
|
return NULL;
|
|
|
|
}
|
2003-10-13 11:03:14 -04:00
|
|
|
|
2004-11-13 02:14:26 -05:00
|
|
|
DPRINTF(E_DBG,L_WS,"Thread %d: got request\n",pwsc->threadno);
|
|
|
|
DPRINTF(E_DBG - 1,L_WS, "Request: %s", buffer);
|
2003-10-13 11:03:14 -04:00
|
|
|
|
2003-11-23 13:13:35 -05:00
|
|
|
first=last=buffer;
|
|
|
|
strsep(&last," ");
|
|
|
|
if(!last) {
|
2004-09-12 18:58:55 -04:00
|
|
|
pwsc->close=1;
|
2003-11-23 13:13:35 -05:00
|
|
|
ws_returnerror(pwsc,400,"Bad request\n");
|
2003-10-30 17:42:11 -05:00
|
|
|
ws_close(pwsc);
|
2004-11-18 01:58:31 -05:00
|
|
|
DPRINTF(E_SPAM,L_WS,"Error: bad request. Exiting ws_dispatcher\n");
|
2003-10-19 16:03:54 -04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2003-11-23 13:13:35 -05:00
|
|
|
if(!strcasecmp(first,"get")) {
|
2003-10-19 16:03:54 -04:00
|
|
|
pwsc->request_type = RT_GET;
|
2003-11-23 13:13:35 -05:00
|
|
|
} else if(!strcasecmp(first,"post")) {
|
2003-10-19 16:03:54 -04:00
|
|
|
pwsc->request_type = RT_POST;
|
|
|
|
} else {
|
|
|
|
/* return a 501 not implemented */
|
|
|
|
pwsc->error=EINVAL;
|
2004-09-12 18:58:55 -04:00
|
|
|
pwsc->close=1;
|
2003-10-19 16:03:54 -04:00
|
|
|
ws_returnerror(pwsc,501,"Not implemented");
|
2003-10-30 17:42:11 -05:00
|
|
|
ws_close(pwsc);
|
2004-11-18 01:58:31 -05:00
|
|
|
DPRINTF(E_SPAM,L_WS,"Error: not get or post. Exiting ws_dispatcher\n");
|
2003-10-19 16:03:54 -04:00
|
|
|
return NULL;
|
|
|
|
}
|
2003-11-23 13:13:35 -05:00
|
|
|
|
|
|
|
first=last;
|
|
|
|
strsep(&last," ");
|
|
|
|
pwsc->uri=strdup(first);
|
2003-10-13 11:03:14 -04:00
|
|
|
|
2003-10-19 16:03:54 -04:00
|
|
|
/* Get headers */
|
2004-09-12 18:58:55 -04:00
|
|
|
if((ws_getheaders(pwsc)) || (!last)) { /* didn't provide a HTTP/1.x */
|
2003-10-19 16:03:54 -04:00
|
|
|
/* error already set */
|
2004-11-18 01:58:31 -05:00
|
|
|
DPRINTF(E_LOG,L_WS,"Thread %d: Couldn't parse headers - aborting\n",
|
2003-10-19 16:03:54 -04:00
|
|
|
pwsc->threadno);
|
2004-09-12 18:58:55 -04:00
|
|
|
pwsc->close=1;
|
2003-10-19 16:03:54 -04:00
|
|
|
ws_close(pwsc);
|
|
|
|
return NULL;
|
|
|
|
}
|
2003-10-13 11:03:14 -04:00
|
|
|
|
|
|
|
|
2003-10-19 16:03:54 -04:00
|
|
|
/* Now that we have the headers, we can
|
|
|
|
* decide whether or not this is a persistant
|
|
|
|
* connection */
|
2004-09-12 18:58:55 -04:00
|
|
|
if(strncasecmp(last,"HTTP/1.0",8)==0) { /* defaults to non-persistant */
|
|
|
|
pwsc->close=!ws_testarg(&pwsc->request_headers,"connection","keep-alive");
|
|
|
|
} else { /* default to persistant for HTTP/1.1 and above */
|
|
|
|
pwsc->close=ws_testarg(&pwsc->request_headers,"connection","close");
|
|
|
|
}
|
2004-11-18 01:58:31 -05:00
|
|
|
|
2004-11-13 02:14:26 -05:00
|
|
|
DPRINTF(E_DBG,L_WS,"Thread %d: Connection type %s: Connection: %s\n",
|
2004-09-12 18:58:55 -04:00
|
|
|
pwsc->threadno, last, pwsc->close ? "non-persist" : "persist");
|
2003-10-13 11:03:14 -04:00
|
|
|
|
2003-10-19 16:03:54 -04:00
|
|
|
if(!pwsc->uri) {
|
|
|
|
pwsc->error=ENOMEM;
|
2004-09-12 18:58:55 -04:00
|
|
|
pwsc->close=1; /* force a full close */
|
2004-11-18 01:58:31 -05:00
|
|
|
DPRINTF(E_LOG,L_WS,"Thread %d: Error allocation URI\n",
|
2003-10-19 16:03:54 -04:00
|
|
|
pwsc->threadno);
|
|
|
|
ws_returnerror(pwsc,500,"Internal server error");
|
2003-10-30 17:42:11 -05:00
|
|
|
ws_close(pwsc);
|
2003-10-19 16:03:54 -04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* trim the URI */
|
|
|
|
first=pwsc->uri;
|
|
|
|
strsep(&first,"?");
|
|
|
|
|
|
|
|
if(first) { /* got some GET args */
|
2004-11-13 02:14:26 -05:00
|
|
|
DPRINTF(E_DBG,L_WS,"Thread %d: parsing GET args\n",pwsc->threadno);
|
2003-10-19 16:03:54 -04:00
|
|
|
ws_getgetvars(pwsc,first);
|
|
|
|
}
|
2003-10-13 11:03:14 -04:00
|
|
|
|
2003-10-19 16:03:54 -04:00
|
|
|
/* fix the URI by un urldecoding it */
|
|
|
|
|
2004-11-13 02:14:26 -05:00
|
|
|
DPRINTF(E_DBG,L_WS,"Thread %d: Original URI: %s\n",
|
2003-10-19 16:03:54 -04:00
|
|
|
pwsc->threadno,pwsc->uri);
|
|
|
|
|
2004-11-28 03:31:10 -05:00
|
|
|
first=ws_urldecode(pwsc->uri,ws_encoding_hack(pwsc));
|
2003-10-19 16:03:54 -04:00
|
|
|
free(pwsc->uri);
|
|
|
|
pwsc->uri=first;
|
2004-04-28 14:45:56 -04:00
|
|
|
|
|
|
|
/* Strip out the proxy stuff - iTunes 4.5 */
|
|
|
|
first=strstr(pwsc->uri,"://");
|
|
|
|
if(first) {
|
|
|
|
first += 3;
|
|
|
|
first=strchr(first,'/');
|
|
|
|
if(first) {
|
|
|
|
first=strdup(first);
|
|
|
|
free(pwsc->uri);
|
|
|
|
pwsc->uri=first;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-10-19 16:03:54 -04:00
|
|
|
|
2004-11-13 02:14:26 -05:00
|
|
|
DPRINTF(E_DBG,L_WS,"Thread %d: Translated URI: %s\n",pwsc->threadno,
|
2003-10-19 16:03:54 -04:00
|
|
|
pwsc->uri);
|
|
|
|
|
|
|
|
/* now, parse POST args */
|
|
|
|
if(pwsc->request_type == RT_POST)
|
|
|
|
ws_getpostvars(pwsc);
|
|
|
|
|
2003-10-30 17:42:11 -05:00
|
|
|
hdrs=1;
|
|
|
|
|
|
|
|
handler=ws_findhandler(pwsp,pwsc,&req_handler,&auth_handler,&hdrs);
|
|
|
|
|
|
|
|
time(&now);
|
2004-11-13 02:14:26 -05:00
|
|
|
DPRINTF(E_DBG,L_WS,"Thread %d: Time is %d seconds after epoch\n",
|
2003-10-30 17:42:11 -05:00
|
|
|
pwsc->threadno,now);
|
|
|
|
gmtime_r(&now,&now_tm);
|
2004-11-13 02:14:26 -05:00
|
|
|
DPRINTF(E_DBG,L_WS,"Thread %d: Setting time header\n",pwsc->threadno);
|
2003-10-30 17:42:11 -05:00
|
|
|
ws_addarg(&pwsc->response_headers,"Date",
|
|
|
|
"%s, %d %s %d %02d:%02d:%02d GMT",
|
|
|
|
ws_dow[now_tm.tm_wday],now_tm.tm_mday,
|
|
|
|
ws_moy[now_tm.tm_mon],now_tm.tm_year + 1900,
|
|
|
|
now_tm.tm_hour,now_tm.tm_min,now_tm.tm_sec);
|
|
|
|
|
|
|
|
if(hdrs) {
|
|
|
|
ws_addarg(&pwsc->response_headers,"Connection",
|
|
|
|
pwsc->close ? "close" : "keep-alive");
|
|
|
|
|
|
|
|
ws_addarg(&pwsc->response_headers,"Server",
|
2004-11-18 01:58:31 -05:00
|
|
|
"mt-daapd/" VERSION);
|
2003-10-30 17:42:11 -05:00
|
|
|
|
|
|
|
ws_addarg(&pwsc->response_headers,"Content-Type","text/html");
|
|
|
|
ws_addarg(&pwsc->response_headers,"Content-Language","en_us");
|
|
|
|
}
|
|
|
|
|
2003-10-19 16:03:54 -04:00
|
|
|
/* Find the appropriate handler and dispatch it */
|
2003-10-30 17:42:11 -05:00
|
|
|
if(handler == -1) {
|
2004-11-13 02:14:26 -05:00
|
|
|
DPRINTF(E_DBG,L_WS,"Thread %d: Using default handler.\n",
|
2003-10-19 16:03:54 -04:00
|
|
|
pwsc->threadno);
|
|
|
|
ws_defaulthandler(pwsp,pwsc);
|
|
|
|
} else {
|
2004-11-13 02:14:26 -05:00
|
|
|
DPRINTF(E_DBG,L_WS,"Thread %d: Using non-default handler\n",
|
2003-10-19 16:03:54 -04:00
|
|
|
pwsc->threadno);
|
|
|
|
|
|
|
|
can_dispatch=0;
|
2003-12-09 00:49:00 -05:00
|
|
|
/* If an auth handler is registered, but it accepts a
|
|
|
|
* username and password of NULL, then don't bother
|
|
|
|
* authing.
|
|
|
|
*/
|
|
|
|
if((auth_handler) && (auth_handler(NULL,NULL)==0)) {
|
2003-10-19 16:03:54 -04:00
|
|
|
/* do the auth thing */
|
|
|
|
auth=ws_getarg(&pwsc->request_headers,"Authorization");
|
|
|
|
if(auth) {
|
|
|
|
ws_decodepassword(auth,&username,&password);
|
|
|
|
if(auth_handler(username,password))
|
|
|
|
can_dispatch=1;
|
2003-12-01 02:05:54 -05:00
|
|
|
ws_addarg(&pwsc->request_vars,"HTTP_USER",username);
|
2004-01-04 00:27:43 -05:00
|
|
|
ws_addarg(&pwsc->request_vars,"HTTP_PASSWD",password);
|
|
|
|
free(username); /* this frees password too */
|
2003-10-19 16:03:54 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if(!can_dispatch) { /* auth failed, or need auth */
|
2004-09-15 02:10:27 -04:00
|
|
|
//ws_addarg(&pwsc->response_headers,"Connection","close");
|
2003-10-19 16:03:54 -04:00
|
|
|
ws_addarg(&pwsc->response_headers,"WWW-Authenticate",
|
2003-10-23 17:43:01 -04:00
|
|
|
"Basic realm=\"webserver\"");
|
2003-10-19 16:03:54 -04:00
|
|
|
ws_returnerror(pwsc,401,"Unauthorized");
|
2003-12-09 00:49:00 -05:00
|
|
|
pwsc->error=0;
|
2003-10-19 16:03:54 -04:00
|
|
|
}
|
2003-12-09 00:49:00 -05:00
|
|
|
} else {
|
|
|
|
can_dispatch=1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(can_dispatch) {
|
|
|
|
if(req_handler)
|
|
|
|
req_handler(pwsc);
|
|
|
|
else
|
|
|
|
ws_defaulthandler(pwsp,pwsc);
|
2003-10-13 11:03:14 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-11-23 01:10:25 -05:00
|
|
|
if((pwsc->close) || (pwsc->error) || (pwsp->stop)) {
|
|
|
|
pwsc->close=1;
|
2003-10-19 16:03:54 -04:00
|
|
|
connection_done=1;
|
2003-11-23 01:10:25 -05:00
|
|
|
}
|
2003-10-19 16:03:54 -04:00
|
|
|
ws_close(pwsc);
|
|
|
|
}
|
2004-11-18 01:58:31 -05:00
|
|
|
DPRINTF(E_SPAM,L_WS,"Exiting ws_dispatcher\n");
|
2003-10-13 11:03:14 -04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* ws_writefd
|
|
|
|
*
|
|
|
|
* Write a printf-style output to a connfd
|
|
|
|
*/
|
2003-10-19 16:03:54 -04:00
|
|
|
int ws_writefd(WS_CONNINFO *pwsc, char *fmt, ...) {
|
|
|
|
char buffer[1024];
|
2004-11-20 12:58:24 -05:00
|
|
|
va_list ap;
|
2003-10-19 16:03:54 -04:00
|
|
|
|
2004-11-18 01:58:31 -05:00
|
|
|
DPRINTF(E_SPAM,L_WS,"Entering ws_writefd\n");
|
2003-10-13 11:03:14 -04:00
|
|
|
|
|
|
|
va_start(ap, fmt);
|
2003-10-19 16:03:54 -04:00
|
|
|
vsnprintf(buffer, 1024, fmt, ap);
|
2003-10-13 11:03:14 -04:00
|
|
|
va_end(ap);
|
|
|
|
|
2004-11-18 01:58:31 -05:00
|
|
|
DPRINTF(E_SPAM,L_WS,"Exiting ws_writefd\n");
|
2003-10-13 11:03:14 -04:00
|
|
|
return r_write(pwsc->fd,buffer,strlen(buffer));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* ws_returnerror
|
|
|
|
*
|
|
|
|
* return a particular error code to the requesting
|
|
|
|
* agent
|
|
|
|
*
|
|
|
|
* This will always succeed. If it cannot, it will
|
|
|
|
* just close the connection with prejudice.
|
|
|
|
*/
|
|
|
|
int ws_returnerror(WS_CONNINFO *pwsc,int error, char *description) {
|
2004-09-15 02:10:27 -04:00
|
|
|
char *useragent;
|
|
|
|
|
2004-11-18 01:58:31 -05:00
|
|
|
DPRINTF(E_WARN,L_WS,"Thread %d: Entering ws_returnerror (%d: %s)\n",
|
2003-10-19 16:03:54 -04:00
|
|
|
pwsc->threadno,error,description);
|
|
|
|
ws_writefd(pwsc,"HTTP/1.1 %d %s\r\n",error,description);
|
2004-09-15 02:10:27 -04:00
|
|
|
|
|
|
|
/* we'll force a close here unless the user agent is
|
|
|
|
iTunes, which seems to get pissy about it */
|
|
|
|
useragent = ws_getarg(&pwsc->request_headers,"User-Agent");
|
|
|
|
if(useragent && (strncmp(useragent,"iTunes",6))) {
|
|
|
|
pwsc->close=1;
|
|
|
|
ws_addarg(&pwsc->response_headers,"Connection","close");
|
|
|
|
}
|
2003-10-19 16:03:54 -04:00
|
|
|
|
|
|
|
ws_emitheaders(pwsc);
|
|
|
|
|
|
|
|
ws_writefd(pwsc,"<HTML>\r\n<TITLE>");
|
|
|
|
ws_writefd(pwsc,"%d %s</TITLE>\r\n<BODY>",error,description);
|
|
|
|
ws_writefd(pwsc,"\r\n<H1>%s</H1>\r\n",description);
|
|
|
|
ws_writefd(pwsc,"Error %d\r\n<hr>\r\n",error);
|
|
|
|
ws_writefd(pwsc,"<i>mt-daapd: %s\r\n<br>",VERSION);
|
|
|
|
if(errno)
|
|
|
|
ws_writefd(pwsc,"Error: %s\r\n",strerror(errno));
|
2003-10-13 11:03:14 -04:00
|
|
|
|
2003-10-19 16:03:54 -04:00
|
|
|
ws_writefd(pwsc,"</i></BODY>\r\n</HTML>\r\n");
|
2003-10-13 11:03:14 -04:00
|
|
|
|
2004-11-18 01:58:31 -05:00
|
|
|
DPRINTF(E_SPAM,L_WS,"Exiting ws_returnerror\n");
|
2003-10-13 11:03:14 -04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* ws_defaulthandler
|
|
|
|
*
|
|
|
|
* default URI handler. This simply finds the file
|
|
|
|
* and serves it up
|
|
|
|
*/
|
|
|
|
void ws_defaulthandler(WS_PRIVATE *pwsp, WS_CONNINFO *pwsc) {
|
|
|
|
char path[MAXPATHLEN];
|
|
|
|
char resolved_path[MAXPATHLEN];
|
|
|
|
int file_fd;
|
2003-10-19 16:03:54 -04:00
|
|
|
off_t len;
|
2003-10-13 11:03:14 -04:00
|
|
|
|
2004-11-18 01:58:31 -05:00
|
|
|
DPRINTF(E_SPAM,L_WS,"Entering ws_defaulthandler\n");
|
|
|
|
|
2003-10-13 11:03:14 -04:00
|
|
|
snprintf(path,MAXPATHLEN,"%s/%s",pwsp->wsconfig.web_root,pwsc->uri);
|
|
|
|
if(!realpath(path,resolved_path)) {
|
2003-10-19 16:03:54 -04:00
|
|
|
pwsc->error=errno;
|
2004-11-18 01:58:31 -05:00
|
|
|
DPRINTF(E_WARN,L_WS,"Exiting ws_defaulthandler: Cannot resolve %s\n",path);
|
2003-10-13 11:03:14 -04:00
|
|
|
ws_returnerror(pwsc,404,"Not found");
|
2003-10-30 17:42:11 -05:00
|
|
|
ws_close(pwsc);
|
2003-10-13 11:03:14 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2004-11-13 02:14:26 -05:00
|
|
|
DPRINTF(E_DBG,L_WS,"Thread %d: Preparing to serve %s\n",
|
2003-10-19 16:03:54 -04:00
|
|
|
pwsc->threadno, resolved_path);
|
2003-10-13 11:03:14 -04:00
|
|
|
|
|
|
|
if(strncmp(resolved_path,pwsp->wsconfig.web_root,
|
|
|
|
strlen(pwsp->wsconfig.web_root))) {
|
2003-10-19 16:03:54 -04:00
|
|
|
pwsc->error=EINVAL;
|
2004-11-18 01:58:31 -05:00
|
|
|
DPRINTF(E_WARN,L_WS,"Exiting ws_defaulthandler: Thread %d: "
|
|
|
|
"Requested file %s out of root\n",
|
2003-10-19 16:03:54 -04:00
|
|
|
pwsc->threadno,resolved_path);
|
2003-10-13 11:03:14 -04:00
|
|
|
ws_returnerror(pwsc,403,"Forbidden");
|
2003-10-30 17:42:11 -05:00
|
|
|
ws_close(pwsc);
|
2003-10-13 11:03:14 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
file_fd=open(resolved_path,O_RDONLY);
|
|
|
|
if(file_fd == -1) {
|
2003-10-19 16:03:54 -04:00
|
|
|
pwsc->error=errno;
|
2004-11-18 01:58:31 -05:00
|
|
|
DPRINTF(E_WARN,L_WS,"Exiting ws_defaulthandler: Thread %d: "
|
|
|
|
"Error opening %s: %s\n",
|
2003-10-19 16:03:54 -04:00
|
|
|
pwsc->threadno,resolved_path,strerror(errno));
|
2003-10-13 11:03:14 -04:00
|
|
|
ws_returnerror(pwsc,404,"Not found");
|
2003-10-30 17:42:11 -05:00
|
|
|
ws_close(pwsc);
|
2003-10-13 11:03:14 -04:00
|
|
|
return;
|
|
|
|
}
|
2003-10-19 16:03:54 -04:00
|
|
|
|
|
|
|
/* set the Content-Length response header */
|
|
|
|
len=lseek(file_fd,0,SEEK_END);
|
|
|
|
|
|
|
|
/* FIXME: assumes off_t == long */
|
|
|
|
if(len != -1) {
|
|
|
|
/* we have a real length */
|
2004-11-13 02:14:26 -05:00
|
|
|
DPRINTF(E_DBG,L_WS,"Length of file is %ld\n",(long)len);
|
2003-10-19 16:03:54 -04:00
|
|
|
ws_addarg(&pwsc->response_headers,"Content-Length","%ld",(long)len);
|
|
|
|
lseek(file_fd,0,SEEK_SET);
|
|
|
|
}
|
|
|
|
|
2003-10-13 11:03:14 -04:00
|
|
|
|
2003-10-19 16:03:54 -04:00
|
|
|
ws_writefd(pwsc,"HTTP/1.1 200 OK\r\n");
|
|
|
|
ws_emitheaders(pwsc);
|
2003-10-13 11:03:14 -04:00
|
|
|
|
|
|
|
/* now throw out the file */
|
|
|
|
copyfile(file_fd,pwsc->fd);
|
|
|
|
|
2003-11-23 13:13:35 -05:00
|
|
|
r_close(file_fd);
|
2004-11-18 01:58:31 -05:00
|
|
|
DPRINTF(E_DBG,L_WS,"Exiting ws_defaulthandler: "
|
|
|
|
"Thread %d: Served successfully\n",
|
|
|
|
pwsc->threadno);
|
2003-10-13 11:03:14 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2003-10-19 16:03:54 -04:00
|
|
|
|
2003-10-30 17:42:11 -05:00
|
|
|
/*
|
|
|
|
* ws_testrequestheader
|
|
|
|
*
|
|
|
|
* Check to see if a request header is a particular value
|
|
|
|
*
|
|
|
|
* Example:
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
int ws_testrequestheader(WS_CONNINFO *pwsc, char *header, char *value) {
|
|
|
|
return ws_testarg(&pwsc->request_headers,header,value);
|
|
|
|
}
|
|
|
|
|
2003-10-19 16:03:54 -04:00
|
|
|
/*
|
|
|
|
* ws_testarg
|
|
|
|
*
|
|
|
|
* Check an arg for a particular value
|
|
|
|
*
|
|
|
|
* Example:
|
|
|
|
*
|
|
|
|
* pwsc->close=ws_queryarg(&pwsc->request_headers,"Connection","close");
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
int ws_testarg(ARGLIST *root, char *key, char *value) {
|
2004-02-23 19:34:04 -05:00
|
|
|
char *retval;
|
2005-03-03 01:07:11 -05:00
|
|
|
int result;
|
|
|
|
|
2004-02-23 19:34:04 -05:00
|
|
|
|
2004-11-13 02:14:26 -05:00
|
|
|
DPRINTF(E_DBG,L_WS,"Checking to see if %s matches %s\n",key,value);
|
2003-10-19 16:03:54 -04:00
|
|
|
|
2004-02-23 19:34:04 -05:00
|
|
|
retval=ws_getarg(root,key);
|
2005-03-03 01:07:11 -05:00
|
|
|
if(!retval) {
|
|
|
|
DPRINTF(E_DBG,L_WS,"Nope!\n");
|
2003-10-19 16:03:54 -04:00
|
|
|
return 0;
|
2005-03-03 01:07:11 -05:00
|
|
|
}
|
2003-10-19 16:03:54 -04:00
|
|
|
|
2005-03-03 01:07:11 -05:00
|
|
|
result=!strcasecmp(value,retval);
|
|
|
|
DPRINTF(E_DBG,L_WS,"And it %s\n",result ? "DOES!" : "does NOT");
|
|
|
|
return result;
|
2003-10-19 16:03:54 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* ws_getarg
|
|
|
|
*
|
|
|
|
* Find an argument in an argument list
|
|
|
|
*
|
|
|
|
* returns a pointer to the value if successful,
|
|
|
|
* NULL otherwise.
|
|
|
|
*
|
|
|
|
* This should be passed the pointer to the
|
|
|
|
* stub in the pwsc.
|
|
|
|
*
|
|
|
|
* Ex: ws_getarg(pwsc->request_headers,"Connection");
|
|
|
|
*/
|
|
|
|
char *ws_getarg(ARGLIST *root, char *key) {
|
|
|
|
ARGLIST *pcurrent=root->next;
|
|
|
|
|
2004-04-28 14:45:56 -04:00
|
|
|
while((pcurrent)&&(strcasecmp(pcurrent->key,key)))
|
2003-10-19 16:03:54 -04:00
|
|
|
pcurrent=pcurrent->next;
|
|
|
|
|
|
|
|
if(pcurrent)
|
|
|
|
return pcurrent->value;
|
2004-04-28 14:45:56 -04:00
|
|
|
|
2003-10-19 16:03:54 -04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-10-13 11:03:14 -04:00
|
|
|
/*
|
|
|
|
* ws_addarg
|
|
|
|
*
|
|
|
|
* Add an argument to an arg list
|
|
|
|
*
|
|
|
|
* This will strdup the passed key and value.
|
|
|
|
* The arglist will then have to be freed. This should
|
|
|
|
* be done in ws_close, and handler functions will
|
|
|
|
* have to remember to ws_close them.
|
|
|
|
*
|
|
|
|
* RETURNS
|
|
|
|
* -1 on failure, with errno set (ENOMEM)
|
|
|
|
* 0 on success
|
|
|
|
*/
|
2003-10-19 16:03:54 -04:00
|
|
|
int ws_addarg(ARGLIST *root, char *key, char *fmt, ...) {
|
2003-10-13 11:03:14 -04:00
|
|
|
char *newkey;
|
|
|
|
char *newvalue;
|
|
|
|
ARGLIST *pnew;
|
2003-10-19 16:03:54 -04:00
|
|
|
ARGLIST *current;
|
|
|
|
va_list ap;
|
|
|
|
char value[MAX_LINEBUFFER];
|
|
|
|
|
|
|
|
va_start(ap,fmt);
|
|
|
|
vsnprintf(value,sizeof(value),fmt,ap);
|
|
|
|
va_end(ap);
|
2003-10-13 11:03:14 -04:00
|
|
|
|
|
|
|
newkey=strdup(key);
|
|
|
|
newvalue=strdup(value);
|
|
|
|
pnew=(ARGLIST*)malloc(sizeof(ARGLIST));
|
|
|
|
|
|
|
|
if((!pnew)||(!newkey)||(!newvalue))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
pnew->key=newkey;
|
|
|
|
pnew->value=newvalue;
|
2003-10-19 16:03:54 -04:00
|
|
|
|
|
|
|
/* first, see if the key exists... if it does, simply
|
|
|
|
* replace it rather than adding a duplicate key
|
|
|
|
*/
|
|
|
|
|
|
|
|
current=root->next;
|
|
|
|
while(current) {
|
2005-03-03 01:07:11 -05:00
|
|
|
if(!strcasecmp(current->key,key)) {
|
2003-10-19 16:03:54 -04:00
|
|
|
/* got a match! */
|
2004-11-13 02:14:26 -05:00
|
|
|
DPRINTF(E_DBG,L_WS,"Updating %s from %s to %s\n",
|
2003-10-19 16:03:54 -04:00
|
|
|
key,current->value,value);
|
|
|
|
free(current->value);
|
|
|
|
current->value = newvalue;
|
|
|
|
free(newkey);
|
|
|
|
free(pnew);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
current=current->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-10-13 11:03:14 -04:00
|
|
|
pnew->next=root->next;
|
2004-11-13 02:14:26 -05:00
|
|
|
DPRINTF(E_DBG,L_WS,"Added *%s=%s*\n",newkey,newvalue);
|
2003-10-13 11:03:14 -04:00
|
|
|
root->next=pnew;
|
2003-10-19 16:03:54 -04:00
|
|
|
|
2003-10-13 11:03:14 -04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* ws_urldecode
|
|
|
|
*
|
|
|
|
* decode a urlencoded string
|
|
|
|
*
|
|
|
|
* the returned char will be malloced -- it must be
|
|
|
|
* freed by the caller
|
|
|
|
*
|
|
|
|
* returns NULL on error (ENOMEM)
|
|
|
|
*/
|
2004-11-28 03:31:10 -05:00
|
|
|
char *ws_urldecode(char *string, int space_as_plus) {
|
2003-10-13 11:03:14 -04:00
|
|
|
char *pnew;
|
|
|
|
char *src,*dst;
|
2004-02-14 19:51:11 -05:00
|
|
|
int val=0;
|
2003-10-13 11:03:14 -04:00
|
|
|
|
2003-11-05 13:57:13 -05:00
|
|
|
pnew=(char*)malloc(strlen(string)+1);
|
2003-10-13 11:03:14 -04:00
|
|
|
if(!pnew)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
src=string;
|
|
|
|
dst=pnew;
|
|
|
|
|
|
|
|
while(*src) {
|
|
|
|
switch(*src) {
|
2004-06-14 15:01:06 -04:00
|
|
|
/* DWB - space gets converted to %20, not +, this definitely breaks compatibility with iTunes */
|
2004-11-28 03:31:10 -05:00
|
|
|
/* But the browsers encode space as plus, so when using the web interface,
|
|
|
|
* anything with a plus is broken. This will end up having to be sniffed
|
|
|
|
* by remote agent */
|
2003-10-13 11:03:14 -04:00
|
|
|
case '+':
|
2004-11-28 03:31:10 -05:00
|
|
|
if(space_as_plus) {
|
|
|
|
*dst++=' ';
|
|
|
|
} else {
|
|
|
|
*dst++=*src;
|
|
|
|
}
|
2003-10-13 11:03:14 -04:00
|
|
|
src++;
|
|
|
|
break;
|
|
|
|
case '%':
|
|
|
|
/* this is hideous */
|
|
|
|
src++;
|
|
|
|
if(*src) {
|
|
|
|
if((*src <= '9') && (*src >='0'))
|
|
|
|
val=(*src - '0');
|
|
|
|
else if((tolower(*src) <= 'f')&&(tolower(*src) >= 'a'))
|
|
|
|
val=10+(tolower(*src) - 'a');
|
|
|
|
src++;
|
|
|
|
}
|
|
|
|
if(*src) {
|
|
|
|
val *= 16;
|
|
|
|
if((*src <= '9') && (*src >='0'))
|
|
|
|
val+=(*src - '0');
|
|
|
|
else if((tolower(*src) <= 'f')&&(tolower(*src) >= 'a'))
|
|
|
|
val+=(10+(tolower(*src) - 'a'));
|
|
|
|
src++;
|
|
|
|
}
|
|
|
|
*dst++=val;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
*dst++=*src++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*dst='\0';
|
|
|
|
return pnew;
|
|
|
|
}
|
2003-10-19 16:03:54 -04:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* ws_registerhandler
|
|
|
|
*
|
|
|
|
* Register a page and auth handler. Returns 0 on success,
|
|
|
|
* returns -1 on failure.
|
|
|
|
*
|
|
|
|
* If the regex is not well-formed, it returns -1 iwth
|
|
|
|
* errno set to EINVAL. It is up to the caller to use
|
|
|
|
* regerror to display a more interesting error message,
|
|
|
|
* if appropriate.
|
|
|
|
*/
|
|
|
|
int ws_registerhandler(WSHANDLE ws, char *regex,
|
|
|
|
void(*handler)(WS_CONNINFO*),
|
2003-10-30 17:42:11 -05:00
|
|
|
int(*auth)(char *, char *),
|
|
|
|
int addheaders) {
|
2003-10-19 16:03:54 -04:00
|
|
|
WS_HANDLER *phandler;
|
|
|
|
WS_PRIVATE *pwsp = (WS_PRIVATE *)ws;
|
|
|
|
|
|
|
|
phandler=(WS_HANDLER *)malloc(sizeof(WS_HANDLER));
|
|
|
|
if(!phandler)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if(regcomp(&phandler->regex,regex,REG_EXTENDED | REG_NOSUB)) {
|
|
|
|
free(phandler);
|
|
|
|
errno=EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
phandler->req_handler=handler;
|
|
|
|
phandler->auth_handler=auth;
|
2003-10-30 17:42:11 -05:00
|
|
|
phandler->addheaders=addheaders;
|
2003-10-19 16:03:54 -04:00
|
|
|
|
|
|
|
ws_lock_unsafe();
|
|
|
|
phandler->next=pwsp->handlers.next;
|
|
|
|
pwsp->handlers.next=phandler;
|
|
|
|
ws_unlock_unsafe();
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* ws_findhandler
|
|
|
|
*
|
|
|
|
* Given a URI, determine the appropriate handler.
|
|
|
|
*
|
|
|
|
* If a handler is found, it returns 0, otherwise, returns
|
|
|
|
* -1
|
|
|
|
*/
|
|
|
|
int ws_findhandler(WS_PRIVATE *pwsp, WS_CONNINFO *pwsc,
|
|
|
|
void(**preq)(WS_CONNINFO*),
|
2003-10-30 17:42:11 -05:00
|
|
|
int(**pauth)(char *, char *),
|
|
|
|
int *addheaders) {
|
2003-10-19 16:03:54 -04:00
|
|
|
WS_HANDLER *phandler=pwsp->handlers.next;
|
|
|
|
|
|
|
|
ws_lock_unsafe();
|
|
|
|
|
2004-04-28 14:45:56 -04:00
|
|
|
*preq=NULL;
|
|
|
|
|
2004-11-13 02:14:26 -05:00
|
|
|
DPRINTF(E_DBG,L_WS,"Thread %d: Preparing to find handler\n",
|
2003-10-19 16:03:54 -04:00
|
|
|
pwsc->threadno);
|
|
|
|
|
|
|
|
while(phandler) {
|
|
|
|
if(!regexec(&phandler->regex,pwsc->uri,0,NULL,0)) {
|
|
|
|
/* that's a match */
|
2004-11-13 02:14:26 -05:00
|
|
|
DPRINTF(E_DBG,L_WS,"Thread %d: URI Match!\n",pwsc->threadno);
|
2003-10-19 16:03:54 -04:00
|
|
|
*preq=phandler->req_handler;
|
|
|
|
*pauth=phandler->auth_handler;
|
2003-10-30 17:42:11 -05:00
|
|
|
*addheaders=phandler->addheaders;
|
2003-10-19 16:03:54 -04:00
|
|
|
ws_unlock_unsafe();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
phandler=phandler->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
ws_unlock_unsafe();
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* ws_decodepassword
|
|
|
|
*
|
|
|
|
* Given a base64 encoded Authentication request header,
|
|
|
|
* decode it into the username and password
|
|
|
|
*/
|
|
|
|
int ws_decodepassword(char *header, char **username, char **password) {
|
|
|
|
static char ws_xlat[256];
|
|
|
|
static int ws_xlat_init=0;
|
|
|
|
int index;
|
|
|
|
int len;
|
|
|
|
int rack=0;
|
|
|
|
int pads=0;
|
|
|
|
unsigned char *decodebuffer;
|
|
|
|
unsigned char *pin, *pout;
|
|
|
|
int lookup;
|
|
|
|
|
|
|
|
*username=NULL;
|
|
|
|
*password=NULL;
|
|
|
|
|
|
|
|
if(ws_lock_unsafe() == -1)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if(!ws_xlat_init) {
|
|
|
|
ws_xlat_init=1;
|
|
|
|
|
|
|
|
memset((char*)&ws_xlat,0xFF,sizeof(ws_xlat));
|
|
|
|
for(index=0; index < 26; index++) {
|
|
|
|
ws_xlat['A' + index] = index;
|
|
|
|
ws_xlat['a' + index] = index + 26;
|
|
|
|
}
|
|
|
|
|
|
|
|
for(index=0; index < 10; index++) {
|
|
|
|
ws_xlat['0' + index] = index + 52;
|
|
|
|
}
|
|
|
|
|
|
|
|
ws_xlat['+'] = 62;
|
|
|
|
ws_xlat['/'] = 63;
|
|
|
|
}
|
|
|
|
if(ws_unlock_unsafe() == -1)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
/* xlat table is initialized */
|
|
|
|
while(*header != ' ')
|
|
|
|
header++;
|
|
|
|
|
|
|
|
header++;
|
|
|
|
|
|
|
|
decodebuffer=(unsigned char *)malloc(strlen(header));
|
|
|
|
if(!decodebuffer)
|
2004-02-14 19:51:11 -05:00
|
|
|
return -1;
|
2003-10-19 16:03:54 -04:00
|
|
|
|
2004-11-13 02:14:26 -05:00
|
|
|
DPRINTF(E_DBG,L_WS,"Preparing to decode %s\n",header);
|
2003-10-19 16:03:54 -04:00
|
|
|
|
|
|
|
memset(decodebuffer,0,strlen(header));
|
|
|
|
len=0;
|
|
|
|
pout=decodebuffer;
|
2005-05-29 23:41:31 -04:00
|
|
|
pin=(unsigned char *)header;
|
2003-10-19 16:03:54 -04:00
|
|
|
|
|
|
|
/* this is more than a little sloppy */
|
|
|
|
while(pin[rack]) {
|
|
|
|
if(pin[rack] != '=') {
|
|
|
|
lookup=ws_xlat[pin[rack]];
|
|
|
|
if(lookup == 0xFF) {
|
2004-11-13 02:14:26 -05:00
|
|
|
DPRINTF(E_WARN,L_WS,"Got garbage Authenticate header\n");
|
2003-10-19 16:03:54 -04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* valid character */
|
|
|
|
switch(rack) {
|
|
|
|
case 0:
|
|
|
|
pout[0]=(lookup << 2);
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
pout[0] |= (lookup >> 4);
|
|
|
|
pout[1] = (lookup << 4);
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
pout[1] |= (lookup >> 2);
|
|
|
|
pout[2] = (lookup << 6);
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
pout[2] |= lookup;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
rack++;
|
|
|
|
} else {
|
|
|
|
/* padding char */
|
|
|
|
pads++;
|
|
|
|
rack++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(rack == 4) {
|
|
|
|
pin += 4;
|
|
|
|
pout += 3;
|
|
|
|
|
|
|
|
len += (3-pads);
|
|
|
|
rack=0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* we now have the decoded string */
|
2004-11-13 02:14:26 -05:00
|
|
|
DPRINTF(E_DBG,L_WS,"Decoded %s\n",decodebuffer);
|
2003-10-19 16:03:54 -04:00
|
|
|
|
2005-05-29 23:41:31 -04:00
|
|
|
*username = (char*)decodebuffer;
|
2003-10-19 16:03:54 -04:00
|
|
|
*password = *username;
|
|
|
|
|
|
|
|
strsep(password,":");
|
|
|
|
|
2004-11-13 02:14:26 -05:00
|
|
|
DPRINTF(E_DBG,L_WS,"Decoded user=%s, pw=%s\n",*username,*password);
|
2003-10-19 16:03:54 -04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* ws_addreponseheader
|
|
|
|
*
|
|
|
|
* Simple wrapper around the CONNINFO response headers
|
|
|
|
*/
|
2003-10-30 17:42:11 -05:00
|
|
|
int ws_addresponseheader(WS_CONNINFO *pwsc, char *header, char *fmt, ...) {
|
|
|
|
va_list ap;
|
|
|
|
char value[MAX_LINEBUFFER];
|
|
|
|
|
|
|
|
va_start(ap,fmt);
|
|
|
|
vsnprintf(value,sizeof(value),fmt,ap);
|
|
|
|
va_end(ap);
|
|
|
|
|
|
|
|
return ws_addarg(&pwsc->response_headers,header,value);
|
2003-10-19 16:03:54 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* ws_getvar
|
|
|
|
*
|
|
|
|
* Simple wrapper around the CONNINFO request vars
|
|
|
|
*/
|
|
|
|
char *ws_getvar(WS_CONNINFO *pwsc, char *var) {
|
2004-04-28 14:45:56 -04:00
|
|
|
return ws_getarg(&(pwsc->request_vars),var);
|
2003-10-19 16:03:54 -04:00
|
|
|
}
|
2003-12-01 01:55:36 -05:00
|
|
|
|
|
|
|
char *ws_getrequestheader(WS_CONNINFO *pwsc, char *header) {
|
|
|
|
return ws_getarg(&pwsc->request_headers,header);
|
|
|
|
}
|