fserve.c 13.1 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
22

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

#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
35
36
37
38
#include "thread/thread.h"
#include "avl/avl.h"
#include "httpp/httpp.h"
#include "net/sock.h"
39
40
41
42
43
44
45
46

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

#include "fserve.h"

#undef CATMODULE
#define CATMODULE "fserve"

#define BUFSIZE 4096

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

63
64
static avl_tree *client_tree;
static avl_tree *pending_tree;
65
static avl_tree *mimetypes = NULL;
66
67

static cond_t fserv_cond;
68
static thread_type *fserv_thread;
69
static int run_fserv;
70
71
72
static int fserve_clients;
static int client_tree_changed=0;

73
#ifdef HAVE_POLL
74
75
static struct pollfd *ufds = NULL;
static int ufdssize = 0;
76
77
78
79
#else
static fd_set fds;
static int fd_max = 0;
#endif
80

81
82
83
84
85
typedef struct {
    char *ext;
    char *type;
} mime_type;

86
87
88
89
/* avl tree helper */
static int _compare_clients(void *compare_arg, void *a, void *b);
static int _remove_client(void *key);
static int _free_client(void *key);
90
91
92
static int _delete_mapping(void *mapping);
static void *fserv_thread_function(void *arg);
static void create_mime_mappings(char *fn);
93
94
95

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

    config_release_config();

    if(!serve)
102
103
        return;

104
105
    create_mime_mappings(MIMETYPESFILE);

106
107
    client_tree = avl_tree_new(_compare_clients, NULL);
    pending_tree = avl_tree_new(_compare_clients, NULL);
108
109
110
111
112
113
114
115
116
117
    thread_cond_create(&fserv_cond);

    run_fserv = 1;

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

void fserve_shutdown(void)
{
Michael Smith's avatar
Michael Smith committed
118
119
120
121
122
123
    ice_config_t *config = config_get_config();
    int serve = config->fileserve;

    config_release_config();

    if(!serve)
124
125
        return;

126
127
128
    if(!run_fserv)
        return;

129
130
    avl_tree_free(mimetypes, _delete_mapping);

131
132
133
134
135
136
137
138
139
    run_fserv = 0;
    thread_cond_signal(&fserv_cond);
    thread_join(fserv_thread);

    thread_cond_destroy(&fserv_cond);
    avl_tree_free(client_tree, _free_client);
    avl_tree_free(pending_tree, _free_client);
}

140
141
142
143
144
145
static void wait_for_fds() {
    avl_node *client_node;
    fserve_t *client;
    int i;

    while(run_fserv) {
146
#ifdef HAVE_POLL
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
        if(client_tree_changed) {
            client_tree_changed = 0;
            i = 0;
            ufdssize = fserve_clients;
            ufds = realloc(ufds, ufdssize * sizeof(struct pollfd));
            avl_tree_rlock(client_tree);
            client_node = avl_get_first(client_tree);
            while(client_node) {
                client = client_node->key;
                ufds[i].fd = client->client->con->sock;
                ufds[i].events = POLLOUT;
                client_node = avl_get_next(client_node);
            }
            avl_tree_unlock(client_tree);
        }

163
        if(poll(ufds, ufdssize, 200) > 0)
164
            return;
165
166
#else
        struct timeval tv;
167
        fd_set realfds;
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
        tv.tv_sec = 0;
        tv.tv_usec = 200000;
        if(client_tree_changed) {
            client_tree_changed = 0;
            i=0;
            FD_ZERO(&fds);
            fd_max = 0;
            avl_tree_rlock(client_tree);
            client_node = avl_get_first(client_tree);
            while(client_node) {
                client = client_node->key;
                FD_SET(client->client->con->sock, &fds);
                if(client->client->con->sock > fd_max)
                    fd_max = client->client->con->sock;
                client_node = avl_get_next(client_node);
            }
            avl_tree_unlock(client_tree);
185
        }
186

187
188
        memcpy(&realfds, &fds, sizeof(fd_set));
        if(select(fd_max+1, NULL, &realfds, NULL, &tv) > 0)
189
190
            return;
#endif
191
192
193
194
195
196
197
198
199
200
        else {
            avl_tree_rlock(pending_tree);
            client_node = avl_get_first(pending_tree);
            avl_tree_unlock(pending_tree);
            if(client_node)
                return;
        }
    }
}

201
static void *fserv_thread_function(void *arg)
202
203
204
205
206
207
208
209
210
211
{
    avl_node *client_node, *pending_node;
    fserve_t *client;
    int sbytes, bytes;

    while (run_fserv) {
        avl_tree_rlock(client_tree);

        client_node = avl_get_first(client_tree);
        if(!client_node) {
212
            avl_tree_rlock(pending_tree);
213
214
215
            pending_node = avl_get_first(pending_tree);
            if(!pending_node) {
                /* There are no current clients. Wait until there are... */
216
                avl_tree_unlock(pending_tree);
217
218
219
220
                avl_tree_unlock(client_tree);
                thread_cond_wait(&fserv_cond);
                continue;
            }
221
            avl_tree_unlock(pending_tree);
222
223
        }

224
225
226
227
228
229
230
        /* This isn't hugely efficient, but it'll do for now */
        avl_tree_unlock(client_tree);
        wait_for_fds();

        avl_tree_rlock(client_tree);
        client_node = avl_get_first(client_tree);

231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
        while(client_node) {
            avl_node_wlock(client_node);

            client = (fserve_t *)client_node->key;

            if(client->offset >= client->datasize) {
                /* Grab a new chunk */
                bytes = fread(client->buf, 1, BUFSIZE, client->file);
                if(bytes <= 0) {
                    client->client->con->error = 1;
                    avl_node_unlock(client_node);
                    client_node = avl_get_next(client_node);
                    continue;
                }
                client->offset = 0;
                client->datasize = bytes;
            }

            /* Now try and send current chunk. */
250
            sbytes = client_send_bytes (client->client, 
251
252
253
                    &client->buf[client->offset], 
                    client->datasize - client->offset);

254
            /* TODO: remove clients if they take too long. */
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
            if(sbytes >= 0) {
                client->offset += sbytes;
            }
            avl_node_unlock(client_node);
            client_node = avl_get_next(client_node);
        }

        avl_tree_unlock(client_tree);

        /* Now we need a write lock instead, to delete done clients. */
        avl_tree_wlock(client_tree);

        client_node = avl_get_first(client_tree);
        while(client_node) {
            client = (fserve_t *)client_node->key;
            if(client->client->con->error) {
271
                fserve_clients--;
272
273
                client_node = avl_get_next(client_node);
                avl_delete(client_tree, (void *)client, _free_client);
274
                client_tree_changed = 1;
275
276
277
278
279
280
281
282
283
284
285
286
                continue;
            }
            client_node = avl_get_next(client_node);
        }

        avl_tree_wlock(pending_tree);

        /* And now insert new clients. */
        client_node = avl_get_first(pending_tree);
        while(client_node) {
            client = (fserve_t *)client_node->key;
            avl_insert(client_tree, client);
287
288
289
            client_tree_changed = 1;
            fserve_clients++;
            stats_event_inc(NULL, "clients");
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
            client_node = avl_get_next(client_node);

        }

        /* clear pending */
        while(avl_get_first(pending_tree)) {
            avl_delete(pending_tree, avl_get_first(pending_tree)->key, 
                    _remove_client);
        }

        avl_tree_unlock(pending_tree);
        avl_tree_unlock(client_tree);
    }

    /* Shutdown path */

    avl_tree_wlock(pending_tree);
    while(avl_get_first(pending_tree))
        avl_delete(pending_tree, avl_get_first(pending_tree)->key, 
                _free_client);
    avl_tree_unlock(pending_tree);

    avl_tree_wlock(client_tree);
    while(avl_get_first(client_tree))
        avl_delete(client_tree, avl_get_first(client_tree)->key, 
                _free_client);
    avl_tree_unlock(client_tree);

    thread_exit(0);
    return NULL;
}

322
static char *fserve_content_type(char *path)
323
324
{
    char *ext = util_get_extension(path);
325
    mime_type exttype = {ext, NULL};
326
    void *result;
327

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

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));
365
    int bytes;
Michael Smith's avatar
Michael Smith committed
366
367
368
369
370
    int client_limit;
    ice_config_t *config = config_get_config();

    client_limit = config->client_limit;
    config_release_config();
371
372
373

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

    client->client = httpclient;
379
380
381
382
    client->offset = 0;
    client->datasize = 0;
    client->buf = malloc(BUFSIZE);

383
    global_lock();
Michael Smith's avatar
Michael Smith committed
384
    if(global.clients >= client_limit) {
385
386
387
388
389
390
391
392
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();

    httpclient->respcode = 200;
    bytes = sock_write(httpclient->con->sock,
            "HTTP/1.0 200 OK\r\n"
            "Content-Type: %s\r\n\r\n",
            fserve_content_type(path));
    if(bytes > 0) httpclient->con->sent_bytes = bytes;

    sock_set_blocking(client->client->con->sock, SOCK_NONBLOCK);
406
    sock_set_nodelay(client->client->con->sock);
407

408
409
410
411
412
413
414
415
416
417
418
    avl_tree_wlock(pending_tree);
    avl_insert(pending_tree, client);
    avl_tree_unlock(pending_tree);

    thread_cond_signal(&fserv_cond);

    return 0;
}

static int _compare_clients(void *compare_arg, void *a, void *b)
{
419
420
421
422
423
    fserve_t *clienta = (fserve_t *)a;
    fserve_t *clientb = (fserve_t *)b;

    connection_t *cona = clienta->client->con;
    connection_t *conb = clientb->client->con;
424

425
426
    if (cona->id < conb->id) return -1;
    if (cona->id > conb->id) return 1;
427

428
    return 0;
429
430
431
432
}

static int _remove_client(void *key)
{
433
    return 1;
434
435
436
437
}

static int _free_client(void *key)
{
438
    fserve_t *client = (fserve_t *)key;
439

440
    fserve_client_destroy(client);
441
442
443
444
445
    global_lock();
    global.clients--;
    global_unlock();
    stats_event_dec(NULL, "clients");

446
447
    
    return 1;
448
449
}

450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
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;
470
    mime_type *mapping;
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504

    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;
505
506
507
            if(*ext)
            {
                void *tmp;
508
509
510
511
                /* Add a new extension->type mapping */
                mapping = malloc(sizeof(mime_type));
                mapping->ext = strdup(ext);
                mapping->type = strdup(type);
512
                if(!avl_get_by_key(mimetypes, mapping, &tmp))
513
514
515
516
517
518
519
520
521
                    avl_delete(mimetypes, mapping, _delete_mapping);
                avl_insert(mimetypes, mapping);
            }
        }
    }

    fclose(mimefile);
}