fserve.c 15.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
/* 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).
 */

13 14 15 16
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

17 18 19 20
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
21
#include <sys/stat.h>
22 23

#ifdef HAVE_POLL
24
#include <sys/poll.h>
25
#endif
26 27 28 29 30 31 32 33 34 35

#ifndef _WIN32
#include <unistd.h>
#include <sys/time.h>
#include <sys/socket.h>
#else
#include <winsock2.h>
#include <windows.h>
#endif

Karl Heyes's avatar
Karl Heyes committed
36 37 38 39
#include "thread/thread.h"
#include "avl/avl.h"
#include "httpp/httpp.h"
#include "net/sock.h"
40 41 42 43 44 45 46 47

#include "connection.h"
#include "global.h"
#include "refbuf.h"
#include "client.h"
#include "stats.h"
#include "format.h"
#include "logging.h"
48
#include "cfgfile.h"
49
#include "util.h"
50
#include "compat.h"
51 52 53 54 55 56 57 58

#include "fserve.h"

#undef CATMODULE
#define CATMODULE "fserve"

#define BUFSIZE 4096

59 60 61 62 63 64
#ifdef _WIN32
#define MIMETYPESFILE ".\\mime.types"
#else
#define MIMETYPESFILE "/etc/mime.types"
#endif

65
static fserve_t *active_list = NULL;
66
volatile static fserve_t *pending_list = NULL;
67 68

static mutex_t pending_lock;
69
static avl_tree *mimetypes = NULL;
70

71
static thread_type *fserv_thread;
72 73
static int run_fserv = 0;
static unsigned int fserve_clients;
74 75
static int client_tree_changed=0;

76
#ifdef HAVE_POLL
77
static struct pollfd *ufds = NULL;
78 79
#else
static fd_set fds;
80
static int fd_max = -1;
81
#endif
82

83 84 85 86 87
typedef struct {
    char *ext;
    char *type;
} mime_type;

88
static int _free_client(void *key);
89 90 91
static int _delete_mapping(void *mapping);
static void *fserv_thread_function(void *arg);
static void create_mime_mappings(char *fn);
92 93 94

void fserve_initialize(void)
{
Michael Smith's avatar
Michael Smith committed
95 96 97 98 99 100
    ice_config_t *config = config_get_config();
    int serve = config->fileserve;

    config_release_config();

    if(!serve)
101 102
        return;

103 104
    create_mime_mappings(MIMETYPESFILE);

105
    thread_mutex_create (&pending_lock);
106 107 108 109 110 111 112 113 114

    run_fserv = 1;

    fserv_thread = thread_create("File Serving Thread", 
            fserv_thread_function, NULL, THREAD_ATTACHED);
}

void fserve_shutdown(void)
{
115 116 117
    if(!run_fserv)
        return;

118 119
    run_fserv = 0;
    thread_join(fserv_thread);
120 121
    INFO0("file serving thread stopped");
    avl_tree_free(mimetypes, _delete_mapping);
122 123
}

124
#ifdef HAVE_POLL
125 126 127 128
int fserve_client_waiting (void)
{
    fserve_t *fclient;
    unsigned int i = 0;
129

130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
    /* 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++;
        }
    }
    if (poll(ufds, fserve_clients, 200) > 0)
    {
        /* 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;
}
159
#else
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
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
    {
183 184 185
        struct timeval tv;
        tv.tv_sec = 0;
        tv.tv_usec = 200000;
186 187
        /* make a duplicate of the set so we do not have to rebuild it
         * each time around */
188 189
        memcpy(&realfds, &fds, sizeof(fd_set));
        if(select(fd_max+1, NULL, &realfds, NULL, &tv) > 0)
190 191 192 193 194 195 196 197 198 199 200 201 202 203
        {
            /* 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;
}
204
#endif
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228

static void wait_for_fds() {
    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++;
                stats_event_inc(NULL, "clients");
            }
            pending_list = NULL;
            thread_mutex_unlock (&pending_lock);
229
        }
230 231 232
        /* drop out of here is someone is ready */
        if (fserve_client_waiting())
           break;
233 234 235
    }
}

236
static void *fserv_thread_function(void *arg)
237
{
238
    fserve_t *fclient, **trail;
239 240
    int sbytes, bytes;

241
    INFO0("file serving thread started");
242
    while (run_fserv) {
243 244
        wait_for_fds();

245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
        fclient = active_list;
        trail = &active_list;

        while (fclient)
        {
            /* process this client, if it is ready */
            if (fclient->ready)
            {
                fclient->ready = 0;
                if(fclient->offset >= fclient->datasize) {
                    /* Grab a new chunk */
                    bytes = fread(fclient->buf, 1, BUFSIZE, fclient->file);
                    if (bytes == 0)
                    {
                        fserve_t *to_go = fclient;
                        fclient = fclient->next;
                        *trail = fclient;
                        _free_client (to_go);
                        fserve_clients--;
                        client_tree_changed = 1;
                        continue;
                    }
                    fclient->offset = 0;
                    fclient->datasize = bytes;
                }
270

271 272 273 274
                /* Now try and send current chunk. */
                sbytes = client_send_bytes (fclient->client, 
                        &fclient->buf[fclient->offset], 
                        fclient->datasize - fclient->offset);
275

276 277 278 279
                /* TODO: remove clients if they take too long. */
                if(sbytes > 0) {
                    fclient->offset += sbytes;
                }
280

281 282 283 284 285 286 287 288
                if (fclient->client->con->error)
                {
                    fserve_t *to_go = fclient;
                    fclient = fclient->next;
                    *trail = fclient;
                    fserve_clients--;
                    _free_client (to_go);
                    client_tree_changed = 1;
289 290 291
                    continue;
                }
            }
292 293
            trail = &fclient->next;
            fclient = fclient->next;
294 295 296 297
        }
    }

    /* Shutdown path */
298 299 300 301 302 303 304 305
    thread_mutex_lock (&pending_lock);
    while (pending_list)
    {
        fserve_t *to_go = (fserve_t *)pending_list;
        pending_list = to_go->next;
        _free_client (to_go);
    }
    thread_mutex_unlock (&pending_lock);
306

307 308 309 310 311 312
    while (active_list)
    {
        fserve_t *to_go = active_list;
        active_list = to_go->next;
        _free_client (to_go);
    }
313 314 315 316

    return NULL;
}

317
static char *fserve_content_type(char *path)
318 319
{
    char *ext = util_get_extension(path);
320
    mime_type exttype = {ext, NULL};
321
    void *result;
322

323 324 325 326 327
    if (!avl_get_by_key (mimetypes, &exttype, &result))
    {
        mime_type *mime = result;
        return mime->type;
    }
328 329 330
    else {
        /* Fallbacks for a few basic ones */
        if(!strcmp(ext, "ogg"))
331
            return "application/ogg";
332 333 334 335
        else if(!strcmp(ext, "mp3"))
            return "audio/mpeg";
        else if(!strcmp(ext, "html"))
            return "text/html";
Karl Heyes's avatar
Karl Heyes committed
336 337
        else if(!strcmp(ext, "css"))
            return "text/css";
338 339 340 341 342
        else if(!strcmp(ext, "txt"))
            return "text/plain";
        else
            return "application/octet-stream";
    }
343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361
}

static void fserve_client_destroy(fserve_t *client)
{
    if(client) {
        if(client->buf)
            free(client->buf);
        if(client->file)
            fclose(client->file);

        if(client->client)
            client_destroy(client->client);
        free(client);
    }
}

int fserve_client_create(client_t *httpclient, char *path)
{
    fserve_t *client = calloc(1, sizeof(fserve_t));
362
    int bytes;
Michael Smith's avatar
Michael Smith committed
363 364
    int client_limit;
    ice_config_t *config = config_get_config();
365
    struct stat file_buf;
366
    char *range = NULL;
367
    int64_t new_content_len = 0;
368 369 370
    int64_t rangenumber = 0;
    int rangeproblem = 0;
    int ret = 0;
Michael Smith's avatar
Michael Smith committed
371 372 373

    client_limit = config->client_limit;
    config_release_config();
374 375 376

    client->file = fopen(path, "rb");
    if(!client->file) {
377
        client_send_404(httpclient, "File not readable");
378 379
        return -1;
    }
380 381

    client->client = httpclient;
382 383
    client->offset = 0;
    client->datasize = 0;
384
    client->ready = 0;
385
    client->content_length = 0;
386
    client->buf = malloc(BUFSIZE);
387
    if (stat(path, &file_buf) == 0) {
388
        client->content_length = (int64_t)file_buf.st_size;
389
    }
390

391
    global_lock();
Michael Smith's avatar
Michael Smith committed
392
    if(global.clients >= client_limit) {
393 394 395 396 397 398 399 400 401 402 403 404 405
        httpclient->respcode = 504;
        bytes = sock_write(httpclient->con->sock,
                "HTTP/1.0 504 Server Full\r\n"
                "Content-Type: text/html\r\n\r\n"
                "<b>Server is full, try again later.</b>\r\n");
        if(bytes > 0) httpclient->con->sent_bytes = bytes;
        fserve_client_destroy(client);
        global_unlock();
        return -1;
    }
    global.clients++;
    global_unlock();

406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433
    range = httpp_getvar (client->client->parser, "range");

    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) {
            ret = fseek(client->file, rangenumber, SEEK_SET);
            if (ret != -1) {
                new_content_len = client->content_length - rangenumber;
                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;
434 435 436 437 438
                struct tm result;
                int64_t endpos = rangenumber+new_content_len-1;
                if (endpos < 0) {
                    endpos = 0;
                }
Ed "oddsock" Zaleski's avatar
Ed "oddsock" Zaleski committed
439
                time(&now);
440
                strflen = strftime(currenttime, 50, "%a, %d-%b-%Y %X GMT",
441
                                   gmtime_r(&now, &result));
442 443 444 445
                httpclient->respcode = 206;
                bytes = sock_write(httpclient->con->sock,
                    "HTTP/1.1 206 Partial Content\r\n"
                    "Date: %s\r\n"
446
                    "Content-Length: " FORMAT_INT64 "\r\n"
447 448 449 450 451 452
                    "Content-Range: bytes " FORMAT_INT64 \
                    "-" FORMAT_INT64 "/" FORMAT_INT64 "\r\n"
                    "Content-Type: %s\r\n\r\n",
                    currenttime,
                    new_content_len,
                    rangenumber,
453
                    endpos,
454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477
                    client->content_length,
                    fserve_content_type(path));
                if(bytes > 0) httpclient->con->sent_bytes = bytes;
            }
            else {
                httpclient->respcode = 416;
                bytes = sock_write(httpclient->con->sock,
                    "HTTP/1.0 416 Request Range Not Satisfiable\r\n\r\n");
                if(bytes > 0) httpclient->con->sent_bytes = bytes;
            }
        }
        else {
            /* If we run into any issues with the ranges
               we fallback to a normal/non-range request */
            httpclient->respcode = 416;
            bytes = sock_write(httpclient->con->sock,
                "HTTP/1.0 416 Request Range Not Satisfiable\r\n\r\n");
            if(bytes > 0) httpclient->con->sent_bytes = bytes;
        }
    }
    else {

        httpclient->respcode = 200;
        bytes = sock_write(httpclient->con->sock,
478
            "HTTP/1.0 200 OK\r\n"
479
            "Content-Length: " FORMAT_INT64 "\r\n"
480
            "Content-Type: %s\r\n\r\n",
481
            client->content_length,
482
            fserve_content_type(path));
483 484
        if(bytes > 0) httpclient->con->sent_bytes = bytes;
    }
485 486

    sock_set_blocking(client->client->con->sock, SOCK_NONBLOCK);
487
    sock_set_nodelay(client->client->con->sock);
488

489 490 491 492
    thread_mutex_lock (&pending_lock);
    client->next = (fserve_t *)pending_list;
    pending_list = client;
    thread_mutex_unlock (&pending_lock);
493

494
    return 0;
495 496 497 498
}

static int _free_client(void *key)
{
499
    fserve_t *client = (fserve_t *)key;
500

501
    fserve_client_destroy(client);
502 503 504 505 506
    global_lock();
    global.clients--;
    global_unlock();
    stats_event_dec(NULL, "clients");

507 508
    
    return 1;
509 510
}

511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530
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);
}

static void create_mime_mappings(char *fn) {
    FILE *mimefile = fopen(fn, "r");
    char line[4096];
    char *type, *ext, *cur;
531
    mime_type *mapping;
532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565

    mimetypes = avl_tree_new(_compare_mappings, NULL);

    if(!mimefile)
        return;

    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;
566 567 568
            if(*ext)
            {
                void *tmp;
569 570 571 572
                /* Add a new extension->type mapping */
                mapping = malloc(sizeof(mime_type));
                mapping->ext = strdup(ext);
                mapping->type = strdup(type);
573
                if(!avl_get_by_key(mimetypes, mapping, &tmp))
574 575 576 577 578 579 580 581 582
                    avl_delete(mimetypes, mapping, _delete_mapping);
                avl_insert(mimetypes, mapping);
            }
        }
    }

    fclose(mimefile);
}