2004-01-28 20:02:12 -05:00
|
|
|
/* Icecast
|
|
|
|
*
|
|
|
|
* This program is distributed under the GNU General Public License, version 2.
|
|
|
|
* A copy of this license is included with this source.
|
|
|
|
*
|
|
|
|
* Copyright 2000-2004, Jack Moffitt <jack@xiph.org,
|
|
|
|
* Michael Smith <msmith@xiph.org>,
|
|
|
|
* oddsock <oddsock@xiph.org>,
|
|
|
|
* Karl Heyes <karl@xiph.org>
|
|
|
|
* and others (see AUTHORS for details).
|
|
|
|
*/
|
|
|
|
|
2003-07-20 21:58:54 -04:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include <config.h>
|
|
|
|
#endif
|
|
|
|
|
2002-08-18 01:06:58 -04:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/types.h>
|
2004-11-11 11:21:01 -05:00
|
|
|
#include <sys/stat.h>
|
2005-08-08 14:39:34 -04:00
|
|
|
#include <errno.h>
|
2002-08-18 05:38:45 -04:00
|
|
|
|
|
|
|
#ifdef HAVE_POLL
|
2002-08-18 04:49:25 -04:00
|
|
|
#include <sys/poll.h>
|
2002-08-18 05:38:45 -04:00
|
|
|
#endif
|
2002-08-18 01:06:58 -04:00
|
|
|
|
|
|
|
#ifndef _WIN32
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#else
|
|
|
|
#include <winsock2.h>
|
|
|
|
#include <windows.h>
|
2005-06-11 13:21:00 -04:00
|
|
|
#define snprintf _snprintf
|
2005-06-17 18:55:59 -04:00
|
|
|
#define S_ISREG(mode) ((mode) & _S_IFREG)
|
2002-08-18 01:06:58 -04:00
|
|
|
#endif
|
|
|
|
|
2003-07-16 15:41:59 -04:00
|
|
|
#include "thread/thread.h"
|
|
|
|
#include "avl/avl.h"
|
|
|
|
#include "httpp/httpp.h"
|
|
|
|
#include "net/sock.h"
|
2002-08-18 01:06:58 -04:00
|
|
|
|
|
|
|
#include "connection.h"
|
|
|
|
#include "global.h"
|
|
|
|
#include "refbuf.h"
|
|
|
|
#include "client.h"
|
|
|
|
#include "stats.h"
|
|
|
|
#include "format.h"
|
|
|
|
#include "logging.h"
|
2003-07-20 21:58:54 -04:00
|
|
|
#include "cfgfile.h"
|
2002-08-18 01:06:58 -04:00
|
|
|
#include "util.h"
|
2004-11-11 17:36:03 -05:00
|
|
|
#include "compat.h"
|
2002-08-18 01:06:58 -04:00
|
|
|
|
|
|
|
#include "fserve.h"
|
|
|
|
|
|
|
|
#undef CATMODULE
|
|
|
|
#define CATMODULE "fserve"
|
|
|
|
|
|
|
|
#define BUFSIZE 4096
|
|
|
|
|
2002-08-18 09:38:51 -04:00
|
|
|
#ifdef _WIN32
|
|
|
|
#define MIMETYPESFILE ".\\mime.types"
|
|
|
|
#else
|
|
|
|
#define MIMETYPESFILE "/etc/mime.types"
|
|
|
|
#endif
|
|
|
|
|
2004-10-23 20:34:15 -04:00
|
|
|
static fserve_t *active_list = NULL;
|
2005-08-08 14:39:34 -04:00
|
|
|
static volatile fserve_t *pending_list = NULL;
|
2004-10-23 20:34:15 -04:00
|
|
|
|
|
|
|
static mutex_t pending_lock;
|
2002-08-28 09:50:58 -04:00
|
|
|
static avl_tree *mimetypes = NULL;
|
2002-08-18 01:06:58 -04:00
|
|
|
|
2002-12-29 10:46:32 -05:00
|
|
|
static thread_type *fserv_thread;
|
2004-10-23 20:34:15 -04:00
|
|
|
static int run_fserv = 0;
|
|
|
|
static unsigned int fserve_clients;
|
2002-08-18 04:49:25 -04:00
|
|
|
static int client_tree_changed=0;
|
|
|
|
|
2002-08-18 05:38:45 -04:00
|
|
|
#ifdef HAVE_POLL
|
2002-08-18 04:49:25 -04:00
|
|
|
static struct pollfd *ufds = NULL;
|
2002-08-18 05:38:45 -04:00
|
|
|
#else
|
|
|
|
static fd_set fds;
|
2004-10-23 20:34:15 -04:00
|
|
|
static int fd_max = -1;
|
2002-08-18 05:38:45 -04:00
|
|
|
#endif
|
2002-08-18 01:06:58 -04:00
|
|
|
|
2002-08-18 09:38:51 -04:00
|
|
|
typedef struct {
|
|
|
|
char *ext;
|
|
|
|
char *type;
|
|
|
|
} mime_type;
|
|
|
|
|
2005-05-09 20:28:37 -04:00
|
|
|
static void fserve_client_destroy(fserve_t *fclient);
|
2002-08-18 09:38:51 -04:00
|
|
|
static int _delete_mapping(void *mapping);
|
|
|
|
static void *fserv_thread_function(void *arg);
|
2004-12-17 19:11:56 -05:00
|
|
|
static void create_mime_mappings(const char *fn);
|
2002-08-18 01:06:58 -04:00
|
|
|
|
|
|
|
void fserve_initialize(void)
|
|
|
|
{
|
2002-08-18 09:38:51 -04:00
|
|
|
create_mime_mappings(MIMETYPESFILE);
|
|
|
|
|
2004-10-23 20:34:15 -04:00
|
|
|
thread_mutex_create (&pending_lock);
|
2002-08-18 01:06:58 -04:00
|
|
|
|
|
|
|
run_fserv = 1;
|
2005-05-09 20:28:37 -04:00
|
|
|
stats_event (NULL, "file_connections", "0");
|
2002-08-18 01:06:58 -04:00
|
|
|
|
|
|
|
fserv_thread = thread_create("File Serving Thread",
|
|
|
|
fserv_thread_function, NULL, THREAD_ATTACHED);
|
|
|
|
}
|
|
|
|
|
|
|
|
void fserve_shutdown(void)
|
|
|
|
{
|
2002-08-28 09:50:58 -04:00
|
|
|
if(!run_fserv)
|
|
|
|
return;
|
|
|
|
|
2002-08-18 01:06:58 -04:00
|
|
|
run_fserv = 0;
|
|
|
|
thread_join(fserv_thread);
|
2004-10-23 20:34:15 -04:00
|
|
|
INFO0("file serving thread stopped");
|
|
|
|
avl_tree_free(mimetypes, _delete_mapping);
|
2002-08-18 01:06:58 -04:00
|
|
|
}
|
|
|
|
|
2002-08-18 05:38:45 -04:00
|
|
|
#ifdef HAVE_POLL
|
2004-10-23 20:34:15 -04:00
|
|
|
int fserve_client_waiting (void)
|
|
|
|
{
|
|
|
|
fserve_t *fclient;
|
|
|
|
unsigned int i = 0;
|
2002-08-18 04:49:25 -04:00
|
|
|
|
2004-10-23 20:34:15 -04:00
|
|
|
/* only rebuild ufds if there are clients added/removed */
|
|
|
|
if (client_tree_changed)
|
|
|
|
{
|
|
|
|
client_tree_changed = 0;
|
|
|
|
ufds = realloc(ufds, fserve_clients * sizeof(struct pollfd));
|
|
|
|
fclient = active_list;
|
|
|
|
while (fclient)
|
|
|
|
{
|
|
|
|
ufds[i].fd = fclient->client->con->sock;
|
|
|
|
ufds[i].events = POLLOUT;
|
|
|
|
ufds[i].revents = 0;
|
|
|
|
fclient = fclient->next;
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
}
|
2005-02-19 17:44:09 -05:00
|
|
|
if (!ufds)
|
2005-05-09 20:28:37 -04:00
|
|
|
thread_sleep(200000);
|
2005-02-19 17:44:09 -05:00
|
|
|
else if (poll(ufds, fserve_clients, 200) > 0)
|
2004-10-23 20:34:15 -04:00
|
|
|
{
|
|
|
|
/* mark any clients that are ready */
|
|
|
|
fclient = active_list;
|
|
|
|
for (i=0; i<fserve_clients; i++)
|
|
|
|
{
|
|
|
|
if (ufds[i].revents & (POLLOUT|POLLHUP|POLLERR))
|
|
|
|
fclient->ready = 1;
|
|
|
|
fclient = fclient->next;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2002-08-18 05:38:45 -04:00
|
|
|
#else
|
2004-10-23 20:34:15 -04:00
|
|
|
int fserve_client_waiting (void)
|
|
|
|
{
|
|
|
|
fserve_t *fclient;
|
|
|
|
fd_set realfds;
|
|
|
|
|
|
|
|
/* only rebuild fds if there are clients added/removed */
|
|
|
|
if(client_tree_changed) {
|
|
|
|
client_tree_changed = 0;
|
|
|
|
FD_ZERO(&fds);
|
|
|
|
fd_max = -1;
|
|
|
|
fclient = active_list;
|
|
|
|
while (fclient) {
|
|
|
|
FD_SET (fclient->client->con->sock, &fds);
|
|
|
|
if (fclient->client->con->sock > fd_max)
|
|
|
|
fd_max = fclient->client->con->sock;
|
|
|
|
fclient = fclient->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* hack for windows, select needs at least 1 descriptor */
|
|
|
|
if (fd_max == -1)
|
|
|
|
thread_sleep (200000);
|
|
|
|
else
|
|
|
|
{
|
2002-08-18 05:38:45 -04:00
|
|
|
struct timeval tv;
|
|
|
|
tv.tv_sec = 0;
|
|
|
|
tv.tv_usec = 200000;
|
2004-10-23 20:34:15 -04:00
|
|
|
/* make a duplicate of the set so we do not have to rebuild it
|
|
|
|
* each time around */
|
2002-08-28 09:50:58 -04:00
|
|
|
memcpy(&realfds, &fds, sizeof(fd_set));
|
|
|
|
if(select(fd_max+1, NULL, &realfds, NULL, &tv) > 0)
|
2004-10-23 20:34:15 -04:00
|
|
|
{
|
|
|
|
/* mark any clients that are ready */
|
|
|
|
fclient = active_list;
|
|
|
|
while (fclient)
|
|
|
|
{
|
|
|
|
if (FD_ISSET (fclient->client->con->sock, &realfds))
|
|
|
|
fclient->ready = 1;
|
|
|
|
fclient = fclient->next;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2002-08-18 05:38:45 -04:00
|
|
|
#endif
|
2004-10-23 20:34:15 -04:00
|
|
|
|
2005-12-17 07:23:09 -05:00
|
|
|
static void wait_for_fds(void) {
|
2004-10-23 20:34:15 -04:00
|
|
|
fserve_t *fclient;
|
|
|
|
|
|
|
|
while (run_fserv)
|
|
|
|
{
|
|
|
|
/* add any new clients here */
|
|
|
|
if (pending_list)
|
|
|
|
{
|
|
|
|
thread_mutex_lock (&pending_lock);
|
|
|
|
|
|
|
|
fclient = (fserve_t*)pending_list;
|
|
|
|
while (fclient)
|
|
|
|
{
|
|
|
|
fserve_t *to_move = fclient;
|
|
|
|
fclient = fclient->next;
|
|
|
|
to_move->next = active_list;
|
|
|
|
active_list = to_move;
|
|
|
|
client_tree_changed = 1;
|
|
|
|
fserve_clients++;
|
|
|
|
}
|
|
|
|
pending_list = NULL;
|
|
|
|
thread_mutex_unlock (&pending_lock);
|
2002-08-18 04:49:25 -04:00
|
|
|
}
|
2005-06-17 18:55:59 -04:00
|
|
|
/* drop out of here if someone is ready */
|
2004-10-23 20:34:15 -04:00
|
|
|
if (fserve_client_waiting())
|
2005-05-09 20:28:37 -04:00
|
|
|
break;
|
2002-08-18 04:49:25 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-08-18 09:38:51 -04:00
|
|
|
static void *fserv_thread_function(void *arg)
|
2002-08-18 01:06:58 -04:00
|
|
|
{
|
2004-10-23 20:34:15 -04:00
|
|
|
fserve_t *fclient, **trail;
|
2005-08-08 14:39:34 -04:00
|
|
|
int bytes;
|
2002-08-18 01:06:58 -04:00
|
|
|
|
2004-10-23 20:34:15 -04:00
|
|
|
INFO0("file serving thread started");
|
2002-08-18 01:06:58 -04:00
|
|
|
while (run_fserv) {
|
2002-08-18 04:49:25 -04:00
|
|
|
wait_for_fds();
|
|
|
|
|
2004-10-23 20:34:15 -04:00
|
|
|
fclient = active_list;
|
|
|
|
trail = &active_list;
|
|
|
|
|
|
|
|
while (fclient)
|
|
|
|
{
|
|
|
|
/* process this client, if it is ready */
|
|
|
|
if (fclient->ready)
|
|
|
|
{
|
2005-06-11 13:21:00 -04:00
|
|
|
client_t *client = fclient->client;
|
|
|
|
refbuf_t *refbuf = client->refbuf;
|
2004-10-23 20:34:15 -04:00
|
|
|
fclient->ready = 0;
|
2005-06-11 13:21:00 -04:00
|
|
|
if (client->pos == refbuf->len)
|
|
|
|
{
|
2004-10-23 20:34:15 -04:00
|
|
|
/* Grab a new chunk */
|
2005-06-17 18:55:59 -04:00
|
|
|
if (fclient->file)
|
|
|
|
bytes = fread (refbuf->data, 1, BUFSIZE, fclient->file);
|
|
|
|
else
|
|
|
|
bytes = 0;
|
2004-10-23 20:34:15 -04:00
|
|
|
if (bytes == 0)
|
|
|
|
{
|
|
|
|
fserve_t *to_go = fclient;
|
|
|
|
fclient = fclient->next;
|
|
|
|
*trail = fclient;
|
2005-05-09 20:28:37 -04:00
|
|
|
fserve_client_destroy (to_go);
|
2004-10-23 20:34:15 -04:00
|
|
|
fserve_clients--;
|
|
|
|
client_tree_changed = 1;
|
|
|
|
continue;
|
|
|
|
}
|
2005-06-11 13:21:00 -04:00
|
|
|
refbuf->len = bytes;
|
|
|
|
client->pos = 0;
|
2004-10-23 20:34:15 -04:00
|
|
|
}
|
2002-08-18 04:49:25 -04:00
|
|
|
|
2004-10-23 20:34:15 -04:00
|
|
|
/* Now try and send current chunk. */
|
2005-08-08 14:39:34 -04:00
|
|
|
format_generic_write_to_client (client);
|
2002-08-18 01:06:58 -04:00
|
|
|
|
2005-06-11 13:21:00 -04:00
|
|
|
if (client->con->error)
|
2004-10-23 20:34:15 -04:00
|
|
|
{
|
|
|
|
fserve_t *to_go = fclient;
|
|
|
|
fclient = fclient->next;
|
|
|
|
*trail = fclient;
|
|
|
|
fserve_clients--;
|
2005-05-09 20:28:37 -04:00
|
|
|
fserve_client_destroy (to_go);
|
2004-10-23 20:34:15 -04:00
|
|
|
client_tree_changed = 1;
|
2002-08-18 01:06:58 -04:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2004-10-23 20:34:15 -04:00
|
|
|
trail = &fclient->next;
|
|
|
|
fclient = fclient->next;
|
2002-08-18 01:06:58 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Shutdown path */
|
2004-10-23 20:34:15 -04:00
|
|
|
thread_mutex_lock (&pending_lock);
|
|
|
|
while (pending_list)
|
|
|
|
{
|
|
|
|
fserve_t *to_go = (fserve_t *)pending_list;
|
|
|
|
pending_list = to_go->next;
|
2005-03-14 18:07:34 -05:00
|
|
|
|
2005-05-09 20:28:37 -04:00
|
|
|
fserve_client_destroy (to_go);
|
2004-10-23 20:34:15 -04:00
|
|
|
}
|
|
|
|
thread_mutex_unlock (&pending_lock);
|
2002-08-18 01:06:58 -04:00
|
|
|
|
2004-10-23 20:34:15 -04:00
|
|
|
while (active_list)
|
|
|
|
{
|
|
|
|
fserve_t *to_go = active_list;
|
|
|
|
active_list = to_go->next;
|
2005-05-09 20:28:37 -04:00
|
|
|
fserve_client_destroy (to_go);
|
2004-10-23 20:34:15 -04:00
|
|
|
}
|
2002-08-18 01:06:58 -04:00
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2005-06-10 11:42:06 -04:00
|
|
|
char *fserve_content_type (const char *path)
|
2002-08-18 01:06:58 -04:00
|
|
|
{
|
|
|
|
char *ext = util_get_extension(path);
|
2002-08-18 09:38:51 -04:00
|
|
|
mime_type exttype = {ext, NULL};
|
2003-07-24 12:21:22 -04:00
|
|
|
void *result;
|
2002-08-18 09:38:51 -04:00
|
|
|
|
2003-07-24 12:21:22 -04:00
|
|
|
if (!avl_get_by_key (mimetypes, &exttype, &result))
|
|
|
|
{
|
|
|
|
mime_type *mime = result;
|
|
|
|
return mime->type;
|
|
|
|
}
|
2002-08-18 09:38:51 -04:00
|
|
|
else {
|
|
|
|
/* Fallbacks for a few basic ones */
|
|
|
|
if(!strcmp(ext, "ogg"))
|
2003-02-11 07:18:22 -05:00
|
|
|
return "application/ogg";
|
2002-08-18 09:38:51 -04:00
|
|
|
else if(!strcmp(ext, "mp3"))
|
|
|
|
return "audio/mpeg";
|
|
|
|
else if(!strcmp(ext, "html"))
|
|
|
|
return "text/html";
|
2004-11-01 18:41:58 -05:00
|
|
|
else if(!strcmp(ext, "css"))
|
|
|
|
return "text/css";
|
2002-08-18 09:38:51 -04:00
|
|
|
else if(!strcmp(ext, "txt"))
|
|
|
|
return "text/plain";
|
2005-06-11 13:21:00 -04:00
|
|
|
else if(!strcmp(ext, "jpg"))
|
|
|
|
return "image/jpeg";
|
|
|
|
else if(!strcmp(ext, "png"))
|
|
|
|
return "image/png";
|
2005-06-17 18:55:59 -04:00
|
|
|
else if(!strcmp(ext, "m3u"))
|
|
|
|
return "audio/x-mpegurl";
|
2002-08-18 09:38:51 -04:00
|
|
|
else
|
|
|
|
return "application/octet-stream";
|
|
|
|
}
|
2002-08-18 01:06:58 -04:00
|
|
|
}
|
|
|
|
|
2005-06-11 13:21:00 -04:00
|
|
|
static void fserve_client_destroy(fserve_t *fclient)
|
2002-08-18 01:06:58 -04:00
|
|
|
{
|
2005-06-11 13:21:00 -04:00
|
|
|
if (fclient)
|
|
|
|
{
|
|
|
|
if (fclient->file)
|
|
|
|
fclose (fclient->file);
|
|
|
|
|
2005-08-12 11:27:32 -04:00
|
|
|
if (fclient->callback)
|
|
|
|
fclient->callback (fclient->client, fclient->arg);
|
|
|
|
else
|
|
|
|
if (fclient->client)
|
|
|
|
client_destroy (fclient->client);
|
2005-06-11 13:21:00 -04:00
|
|
|
free (fclient);
|
2002-08-18 01:06:58 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-05-09 20:28:37 -04:00
|
|
|
|
2005-08-08 14:39:34 -04:00
|
|
|
/* client has requested a file, so check for it and send the file. Do not
|
|
|
|
* refer to the client_t afterwards. return 0 for success, -1 on error.
|
|
|
|
*/
|
|
|
|
int fserve_client_create (client_t *httpclient, const char *path)
|
2002-08-18 01:06:58 -04:00
|
|
|
{
|
2002-08-18 04:49:25 -04:00
|
|
|
int bytes;
|
2004-11-11 11:21:01 -05:00
|
|
|
struct stat file_buf;
|
2004-11-14 22:50:42 -05:00
|
|
|
char *range = NULL;
|
2004-11-14 22:53:16 -05:00
|
|
|
int64_t new_content_len = 0;
|
2005-06-17 18:55:59 -04:00
|
|
|
int64_t rangenumber = 0, content_length;
|
2004-11-14 22:50:42 -05:00
|
|
|
int rangeproblem = 0;
|
|
|
|
int ret = 0;
|
2005-06-17 18:55:59 -04:00
|
|
|
char *fullpath;
|
|
|
|
int m3u_requested = 0, m3u_file_available = 1;
|
|
|
|
ice_config_t *config;
|
|
|
|
FILE *file;
|
|
|
|
|
|
|
|
fullpath = util_get_path_from_normalised_uri (path);
|
|
|
|
INFO2 ("checking for file %s (%s)", path, fullpath);
|
|
|
|
|
|
|
|
if (strcmp (util_get_extension (fullpath), "m3u") == 0)
|
|
|
|
m3u_requested = 1;
|
|
|
|
|
|
|
|
/* check for the actual file */
|
|
|
|
if (stat (fullpath, &file_buf) != 0)
|
|
|
|
{
|
|
|
|
/* the m3u can be generated, but send an m3u file if available */
|
|
|
|
if (m3u_requested == 0)
|
|
|
|
{
|
2005-08-08 14:39:34 -04:00
|
|
|
WARN2 ("req for file \"%s\" %s", fullpath, strerror (errno));
|
|
|
|
client_send_404 (httpclient, "The file you requested could not be found");
|
2005-06-17 18:55:59 -04:00
|
|
|
free (fullpath);
|
2005-08-08 14:39:34 -04:00
|
|
|
return -1;
|
2005-06-17 18:55:59 -04:00
|
|
|
}
|
|
|
|
m3u_file_available = 0;
|
|
|
|
}
|
|
|
|
|
2005-08-12 11:27:32 -04:00
|
|
|
httpclient->refbuf->len = PER_CLIENT_REFBUF_SIZE;
|
2005-06-17 18:55:59 -04:00
|
|
|
|
|
|
|
if (m3u_requested && m3u_file_available == 0)
|
|
|
|
{
|
|
|
|
char *host = httpp_getvar (httpclient->parser, "host");
|
|
|
|
char *sourceuri = strdup (path);
|
|
|
|
char *dot = strrchr(sourceuri, '.');
|
2005-09-08 09:43:33 -04:00
|
|
|
|
|
|
|
/* at least a couple of players (fb2k/winamp) are reported to send a
|
|
|
|
* host header but without the port number. So if we are missing the
|
|
|
|
* port then lets treat it as if no host line was sent */
|
|
|
|
if (host && strchr (host, ':') == NULL)
|
|
|
|
host = NULL;
|
|
|
|
|
2005-06-17 18:55:59 -04:00
|
|
|
*dot = 0;
|
|
|
|
httpclient->respcode = 200;
|
|
|
|
if (host == NULL)
|
|
|
|
{
|
|
|
|
config = config_get_config();
|
|
|
|
snprintf (httpclient->refbuf->data, BUFSIZE,
|
|
|
|
"HTTP/1.0 200 OK\r\n"
|
|
|
|
"Content-Type: audio/x-mpegurl\r\n\r\n"
|
|
|
|
"http://%s:%d%s\r\n",
|
|
|
|
config->hostname, config->port,
|
|
|
|
sourceuri
|
|
|
|
);
|
|
|
|
config_release_config();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
snprintf (httpclient->refbuf->data, BUFSIZE,
|
|
|
|
"HTTP/1.0 200 OK\r\n"
|
|
|
|
"Content-Type: audio/x-mpegurl\r\n\r\n"
|
|
|
|
"http://%s%s\r\n",
|
|
|
|
host,
|
|
|
|
sourceuri
|
|
|
|
);
|
|
|
|
}
|
|
|
|
httpclient->refbuf->len = strlen (httpclient->refbuf->data);
|
|
|
|
fserve_add_client (httpclient, NULL);
|
|
|
|
free (sourceuri);
|
|
|
|
free (fullpath);
|
2005-08-08 14:39:34 -04:00
|
|
|
return 0;
|
2005-06-17 18:55:59 -04:00
|
|
|
}
|
2003-03-05 08:03:35 -05:00
|
|
|
|
2005-06-17 18:55:59 -04:00
|
|
|
/* on demand file serving check */
|
|
|
|
config = config_get_config();
|
|
|
|
if (config->fileserve == 0)
|
2005-06-11 13:21:00 -04:00
|
|
|
{
|
2005-06-17 18:55:59 -04:00
|
|
|
DEBUG1 ("on demand file \"%s\" refused", fullpath);
|
2005-06-11 13:21:00 -04:00
|
|
|
client_send_404 (httpclient, "The file you requested could not be found");
|
2005-06-17 18:55:59 -04:00
|
|
|
config_release_config();
|
|
|
|
free (fullpath);
|
2005-08-08 14:39:34 -04:00
|
|
|
return -1;
|
2005-06-11 13:21:00 -04:00
|
|
|
}
|
2005-06-17 18:55:59 -04:00
|
|
|
config_release_config();
|
2002-08-18 01:06:58 -04:00
|
|
|
|
2005-06-17 18:55:59 -04:00
|
|
|
if (S_ISREG (file_buf.st_mode) == 0)
|
2005-06-11 13:21:00 -04:00
|
|
|
{
|
2005-06-17 18:55:59 -04:00
|
|
|
client_send_404 (httpclient, "The file you requested could not be found");
|
|
|
|
WARN1 ("found requested file but there is no handler for it: %s", fullpath);
|
|
|
|
free (fullpath);
|
2005-08-08 14:39:34 -04:00
|
|
|
return -1;
|
2005-06-11 13:21:00 -04:00
|
|
|
}
|
2005-06-17 18:55:59 -04:00
|
|
|
|
|
|
|
file = fopen (fullpath, "rb");
|
|
|
|
if (file == NULL)
|
2005-06-11 13:21:00 -04:00
|
|
|
{
|
2005-06-17 18:55:59 -04:00
|
|
|
WARN1 ("Problem accessing file \"%s\"", fullpath);
|
2005-06-11 13:21:00 -04:00
|
|
|
client_send_404 (httpclient, "File not readable");
|
2006-05-18 18:06:32 -04:00
|
|
|
free (fullpath);
|
2005-08-08 14:39:34 -04:00
|
|
|
return -1;
|
2002-08-18 01:06:58 -04:00
|
|
|
}
|
2006-05-18 18:06:32 -04:00
|
|
|
free (fullpath);
|
2002-08-18 04:49:25 -04:00
|
|
|
|
2005-06-17 18:55:59 -04:00
|
|
|
content_length = (int64_t)file_buf.st_size;
|
|
|
|
range = httpp_getvar (httpclient->parser, "range");
|
2004-11-14 22:50:42 -05:00
|
|
|
|
|
|
|
if (range != NULL) {
|
|
|
|
ret = sscanf(range, "bytes=" FORMAT_INT64 "-", &rangenumber);
|
|
|
|
if (ret != 1) {
|
|
|
|
/* format not correct, so lets just assume
|
|
|
|
we start from the beginning */
|
|
|
|
rangeproblem = 1;
|
|
|
|
}
|
|
|
|
if (rangenumber < 0) {
|
|
|
|
rangeproblem = 1;
|
|
|
|
}
|
|
|
|
if (!rangeproblem) {
|
2005-06-17 18:55:59 -04:00
|
|
|
ret = fseek (file, rangenumber, SEEK_SET);
|
2004-11-14 22:50:42 -05:00
|
|
|
if (ret != -1) {
|
2005-06-17 18:55:59 -04:00
|
|
|
new_content_len = content_length - rangenumber;
|
2004-11-14 22:50:42 -05:00
|
|
|
if (new_content_len < 0) {
|
|
|
|
rangeproblem = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
rangeproblem = 1;
|
|
|
|
}
|
|
|
|
if (!rangeproblem) {
|
|
|
|
/* Date: is required on all HTTP1.1 responses */
|
|
|
|
char currenttime[50];
|
|
|
|
time_t now;
|
|
|
|
int strflen;
|
2004-11-15 10:50:59 -05:00
|
|
|
struct tm result;
|
|
|
|
int64_t endpos = rangenumber+new_content_len-1;
|
|
|
|
if (endpos < 0) {
|
|
|
|
endpos = 0;
|
|
|
|
}
|
2004-11-15 10:55:43 -05:00
|
|
|
time(&now);
|
2004-11-14 22:50:42 -05:00
|
|
|
strflen = strftime(currenttime, 50, "%a, %d-%b-%Y %X GMT",
|
2004-11-15 10:50:59 -05:00
|
|
|
gmtime_r(&now, &result));
|
2004-11-14 22:50:42 -05:00
|
|
|
httpclient->respcode = 206;
|
2005-06-11 13:21:00 -04:00
|
|
|
bytes = snprintf (httpclient->refbuf->data, BUFSIZE,
|
2004-11-14 22:50:42 -05:00
|
|
|
"HTTP/1.1 206 Partial Content\r\n"
|
|
|
|
"Date: %s\r\n"
|
2004-11-15 10:50:59 -05:00
|
|
|
"Content-Length: " FORMAT_INT64 "\r\n"
|
2004-11-14 22:50:42 -05:00
|
|
|
"Content-Range: bytes " FORMAT_INT64 \
|
|
|
|
"-" FORMAT_INT64 "/" FORMAT_INT64 "\r\n"
|
|
|
|
"Content-Type: %s\r\n\r\n",
|
|
|
|
currenttime,
|
|
|
|
new_content_len,
|
|
|
|
rangenumber,
|
2004-11-15 10:50:59 -05:00
|
|
|
endpos,
|
2005-06-17 18:55:59 -04:00
|
|
|
content_length,
|
2004-11-14 22:50:42 -05:00
|
|
|
fserve_content_type(path));
|
|
|
|
}
|
|
|
|
else {
|
2006-03-07 14:28:41 -05:00
|
|
|
goto fail;
|
2004-11-14 22:50:42 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2006-03-07 14:28:41 -05:00
|
|
|
goto fail;
|
2004-11-14 22:50:42 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
|
|
|
|
httpclient->respcode = 200;
|
2005-06-11 13:21:00 -04:00
|
|
|
bytes = snprintf (httpclient->refbuf->data, BUFSIZE,
|
2002-08-18 04:49:25 -04:00
|
|
|
"HTTP/1.0 200 OK\r\n"
|
2004-11-11 17:36:03 -05:00
|
|
|
"Content-Length: " FORMAT_INT64 "\r\n"
|
2002-08-18 04:49:25 -04:00
|
|
|
"Content-Type: %s\r\n\r\n",
|
2005-06-17 18:55:59 -04:00
|
|
|
content_length,
|
2002-08-18 04:49:25 -04:00
|
|
|
fserve_content_type(path));
|
2004-11-14 22:50:42 -05:00
|
|
|
}
|
2005-06-11 13:21:00 -04:00
|
|
|
httpclient->refbuf->len = bytes;
|
2005-06-17 18:55:59 -04:00
|
|
|
httpclient->pos = 0;
|
|
|
|
|
2005-05-09 20:28:37 -04:00
|
|
|
stats_event_inc (NULL, "file_connections");
|
2005-06-17 18:55:59 -04:00
|
|
|
fserve_add_client (httpclient, file);
|
|
|
|
|
2005-08-08 14:39:34 -04:00
|
|
|
return 0;
|
2006-03-07 14:28:41 -05:00
|
|
|
|
|
|
|
fail:
|
|
|
|
fclose (file);
|
|
|
|
httpclient->respcode = 416;
|
|
|
|
sock_write (httpclient->con->sock,
|
|
|
|
"HTTP/1.0 416 Request Range Not Satisfiable\r\n\r\n");
|
|
|
|
client_destroy (httpclient);
|
|
|
|
return -1;
|
2005-06-17 18:55:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Add client to fserve thread, client needs to have refbuf set and filled
|
|
|
|
* but may provide a NULL file if no data needs to be read
|
|
|
|
*/
|
|
|
|
int fserve_add_client (client_t *client, FILE *file)
|
|
|
|
{
|
|
|
|
fserve_t *fclient = calloc (1, sizeof(fserve_t));
|
|
|
|
|
|
|
|
DEBUG0 ("Adding client to file serving engine");
|
|
|
|
if (fclient == NULL)
|
|
|
|
{
|
|
|
|
client_send_404 (client, "memory exhausted");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
fclient->file = file;
|
|
|
|
fclient->client = client;
|
|
|
|
fclient->ready = 0;
|
|
|
|
|
2004-10-23 20:34:15 -04:00
|
|
|
thread_mutex_lock (&pending_lock);
|
2005-06-17 18:55:59 -04:00
|
|
|
fclient->next = (fserve_t *)pending_list;
|
|
|
|
pending_list = fclient;
|
2004-10-23 20:34:15 -04:00
|
|
|
thread_mutex_unlock (&pending_lock);
|
2002-08-18 01:06:58 -04:00
|
|
|
|
2003-03-14 21:10:19 -05:00
|
|
|
return 0;
|
2002-08-18 01:06:58 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-08-12 11:27:32 -04:00
|
|
|
/* add client to file serving engine, but just write out the buffer contents,
|
|
|
|
* then pass the client to the callback with the provided arg
|
|
|
|
*/
|
|
|
|
void fserve_add_client_callback (client_t *client, fserve_callback_t callback, void *arg)
|
|
|
|
{
|
|
|
|
fserve_t *fclient = calloc (1, sizeof(fserve_t));
|
|
|
|
|
|
|
|
DEBUG0 ("Adding client to file serving engine");
|
|
|
|
if (fclient == NULL)
|
|
|
|
{
|
|
|
|
client_send_404 (client, "memory exhausted");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
fclient->file = NULL;
|
|
|
|
fclient->client = client;
|
|
|
|
fclient->ready = 0;
|
|
|
|
fclient->callback = callback;
|
|
|
|
fclient->arg = arg;
|
|
|
|
|
|
|
|
thread_mutex_lock (&pending_lock);
|
|
|
|
fclient->next = (fserve_t *)pending_list;
|
|
|
|
pending_list = fclient;
|
|
|
|
thread_mutex_unlock (&pending_lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-08-18 09:38:51 -04:00
|
|
|
static int _delete_mapping(void *mapping) {
|
|
|
|
mime_type *map = mapping;
|
|
|
|
free(map->ext);
|
|
|
|
free(map->type);
|
|
|
|
free(map);
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int _compare_mappings(void *arg, void *a, void *b)
|
|
|
|
{
|
|
|
|
return strcmp(
|
|
|
|
((mime_type *)a)->ext,
|
|
|
|
((mime_type *)b)->ext);
|
|
|
|
}
|
|
|
|
|
2004-12-17 19:11:56 -05:00
|
|
|
static void create_mime_mappings(const char *fn) {
|
2002-08-18 09:38:51 -04:00
|
|
|
FILE *mimefile = fopen(fn, "r");
|
|
|
|
char line[4096];
|
|
|
|
char *type, *ext, *cur;
|
2003-07-24 12:21:22 -04:00
|
|
|
mime_type *mapping;
|
2002-08-18 09:38:51 -04:00
|
|
|
|
|
|
|
mimetypes = avl_tree_new(_compare_mappings, NULL);
|
|
|
|
|
2005-06-11 13:21:00 -04:00
|
|
|
if (mimefile == NULL)
|
|
|
|
{
|
|
|
|
WARN1 ("Cannot open mime type file %s", fn);
|
2002-08-18 09:38:51 -04:00
|
|
|
return;
|
2005-06-11 13:21:00 -04:00
|
|
|
}
|
2002-08-18 09:38:51 -04:00
|
|
|
|
|
|
|
while(fgets(line, 4096, mimefile))
|
|
|
|
{
|
|
|
|
line[4095] = 0;
|
|
|
|
|
|
|
|
if(*line == 0 || *line == '#')
|
|
|
|
continue;
|
|
|
|
|
|
|
|
type = line;
|
|
|
|
|
|
|
|
cur = line;
|
|
|
|
|
|
|
|
while(*cur != ' ' && *cur != '\t' && *cur)
|
|
|
|
cur++;
|
|
|
|
if(*cur == 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
*cur++ = 0;
|
|
|
|
|
|
|
|
while(1) {
|
|
|
|
while(*cur == ' ' || *cur == '\t')
|
|
|
|
cur++;
|
|
|
|
if(*cur == 0)
|
|
|
|
break;
|
|
|
|
|
|
|
|
ext = cur;
|
|
|
|
while(*cur != ' ' && *cur != '\t' && *cur != '\n' && *cur)
|
|
|
|
cur++;
|
|
|
|
*cur++ = 0;
|
2003-07-24 12:21:22 -04:00
|
|
|
if(*ext)
|
|
|
|
{
|
|
|
|
void *tmp;
|
2002-08-18 09:38:51 -04:00
|
|
|
/* Add a new extension->type mapping */
|
|
|
|
mapping = malloc(sizeof(mime_type));
|
|
|
|
mapping->ext = strdup(ext);
|
|
|
|
mapping->type = strdup(type);
|
2003-07-24 12:21:22 -04:00
|
|
|
if(!avl_get_by_key(mimetypes, mapping, &tmp))
|
2002-08-18 09:38:51 -04:00
|
|
|
avl_delete(mimetypes, mapping, _delete_mapping);
|
|
|
|
avl_insert(mimetypes, mapping);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fclose(mimefile);
|
|
|
|
}
|
|
|
|
|