fserve.c 12 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
65
66
static fserve_t *active_list = NULL;
static volatile fserve_t *pending_list = NULL;

static mutex_t pending_lock;
67
static avl_tree *mimetypes = NULL;
68

69
static thread_type *fserv_thread;
70
71
static int run_fserv = 0;
static unsigned int fserve_clients;
72
73
static int client_tree_changed=0;

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

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

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

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

    config_release_config();

    if(!serve)
99
100
        return;

101
102
    create_mime_mappings(MIMETYPESFILE);

103
    thread_mutex_create (&pending_lock);
104
105
106
107
108
109
110
111
112

    run_fserv = 1;

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

void fserve_shutdown(void)
{
113
114
115
    if(!run_fserv)
        return;

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

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

128
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
    /* 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;
}
157
#else
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
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
    {
181
182
183
        struct timeval tv;
        tv.tv_sec = 0;
        tv.tv_usec = 200000;
184
185
        /* make a duplicate of the set so we do not have to rebuild it
         * each time around */
186
187
        memcpy(&realfds, &fds, sizeof(fd_set));
        if(select(fd_max+1, NULL, &realfds, NULL, &tv) > 0)
188
189
190
191
192
193
194
195
196
197
198
199
200
201
        {
            /* 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;
}
202
#endif
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226

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);
227
        }
228
229
230
        /* drop out of here is someone is ready */
        if (fserve_client_waiting())
           break;
231
232
233
    }
}

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

239
    INFO0("file serving thread started");
240
    while (run_fserv) {
241
242
        wait_for_fds();

243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
        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;
                }
268

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

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

279
280
281
282
283
284
285
286
                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;
287
288
289
                    continue;
                }
            }
290
291
            trail = &fclient->next;
            fclient = fclient->next;
292
293
294
295
        }
    }

    /* Shutdown path */
296
297
298
299
300
301
302
303
    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);
304

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

    return NULL;
}

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

321
322
323
324
325
    if (!avl_get_by_key (mimetypes, &exttype, &result))
    {
        mime_type *mime = result;
        return mime->type;
    }
326
327
328
    else {
        /* Fallbacks for a few basic ones */
        if(!strcmp(ext, "ogg"))
329
            return "application/ogg";
330
331
332
333
334
335
336
337
338
        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";
    }
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
}

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));
358
    int bytes;
Michael Smith's avatar
Michael Smith committed
359
360
361
362
363
    int client_limit;
    ice_config_t *config = config_get_config();

    client_limit = config->client_limit;
    config_release_config();
364
365
366

    client->file = fopen(path, "rb");
    if(!client->file) {
367
        client_send_404(httpclient, "File not readable");
368
369
        return -1;
    }
370
371

    client->client = httpclient;
372
373
    client->offset = 0;
    client->datasize = 0;
374
    client->ready = 0;
375
376
    client->buf = malloc(BUFSIZE);

377
    global_lock();
Michael Smith's avatar
Michael Smith committed
378
    if(global.clients >= client_limit) {
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
        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);
400
    sock_set_nodelay(client->client->con->sock);
401

402
403
404
405
    thread_mutex_lock (&pending_lock);
    client->next = (fserve_t *)pending_list;
    pending_list = client;
    thread_mutex_unlock (&pending_lock);
406

407
    return 0;
408
409
410
411
}

static int _free_client(void *key)
{
412
    fserve_t *client = (fserve_t *)key;
413

414
    fserve_client_destroy(client);
415
416
417
418
419
    global_lock();
    global.clients--;
    global_unlock();
    stats_event_dec(NULL, "clients");

420
421
    
    return 1;
422
423
}

424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
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;
444
    mime_type *mapping;
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478

    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;
479
480
481
            if(*ext)
            {
                void *tmp;
482
483
484
485
                /* Add a new extension->type mapping */
                mapping = malloc(sizeof(mime_type));
                mapping->ext = strdup(ext);
                mapping->type = strdup(type);
486
                if(!avl_get_by_key(mimetypes, mapping, &tmp))
487
488
489
490
491
492
493
494
495
                    avl_delete(mimetypes, mapping, _delete_mapping);
                avl_insert(mimetypes, mapping);
            }
        }
    }

    fclose(mimefile);
}