client.c 4.61 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).
 */

Jack Moffitt's avatar
Jack Moffitt committed
13 14 15 16 17 18
/* client.c
**
** client interface implementation
**
*/

19 20 21 22
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

Jack Moffitt's avatar
Jack Moffitt committed
23 24 25
#include <stdlib.h>
#include <string.h>

Karl Heyes's avatar
Karl Heyes committed
26 27 28
#include "thread/thread.h"
#include "avl/avl.h"
#include "httpp/httpp.h"
Jack Moffitt's avatar
Jack Moffitt committed
29

30
#include "cfgfile.h"
Jack Moffitt's avatar
Jack Moffitt committed
31 32
#include "connection.h"
#include "refbuf.h"
33
#include "stats.h"
Jack Moffitt's avatar
Jack Moffitt committed
34 35 36 37

#include "client.h"
#include "logging.h"

38 39 40
#undef CATMODULE
#define CATMODULE "client"

Jack Moffitt's avatar
Jack Moffitt committed
41 42
client_t *client_create(connection_t *con, http_parser_t *parser)
{
43
    ice_config_t *config = config_get_config ();
44
    client_t *client = (client_t *)calloc(1, sizeof(client_t));
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
    int client_limit = config->client_limit;
    config_release_config ();

    global_lock();
    if (global.clients >= client_limit || client == NULL)
    {
        client_limit = global.clients;
        global_unlock();
        free (client);
        WARN1 ("server client limit reached (%d clients)", client_limit);
        return NULL;
    }
    global.clients++;
    stats_event_args (NULL, "clients", "%d", global.clients);
    global_unlock();
Jack Moffitt's avatar
Jack Moffitt committed
60

61 62
    client->con = con;
    client->parser = parser;
Karl Heyes's avatar
Karl Heyes committed
63
    client->refbuf = NULL;
64
    client->pos = 0;
Jack Moffitt's avatar
Jack Moffitt committed
65

66
    return client;
Jack Moffitt's avatar
Jack Moffitt committed
67 68 69 70
}

void client_destroy(client_t *client)
{
Karl Heyes's avatar
Karl Heyes committed
71 72
    if (client == NULL)
        return;
73
    /* write log entry if ip is set (some things don't set it, like outgoing 
74 75 76
     * slave requests
     */
    if(client->con->ip)
77 78 79 80
        logging_access(client);
    
    connection_close(client->con);
    httpp_destroy(client->parser);
Jack Moffitt's avatar
Jack Moffitt committed
81

82 83 84 85 86
    global_lock ();
    global.clients--;
    stats_event_args (NULL, "clients", "%d", global.clients);
    global_unlock ();

Karl Heyes's avatar
Karl Heyes committed
87 88 89
    /* drop ref counts if need be */
    if (client->refbuf)
        refbuf_release (client->refbuf);
90 91 92 93
    /* we need to free client specific format data (if any) */
    if (client->free_client_data)
        client->free_client_data (client);

Michael Smith's avatar
Michael Smith committed
94 95
    free(client->username);

96
    free(client);
Jack Moffitt's avatar
Jack Moffitt committed
97
}
98

99 100
void client_send_400(client_t *client, char *message) {
    int bytes;
101
    bytes = sock_write(client->con->sock, "HTTP/1.0 400 Bad Request\r\n"
102 103 104
            "Content-Type: text/html\r\n\r\n"
            "<b>%s</b>\r\n", message);
    if(bytes > 0) client->con->sent_bytes = bytes;
105
    client->respcode = 400;
106 107 108
    client_destroy(client);
}

109 110 111 112 113 114 115
void client_send_404(client_t *client, char *message) {

    int bytes;
    bytes = sock_write(client->con->sock, "HTTP/1.0 404 File Not Found\r\n"
            "Content-Type: text/html\r\n\r\n"
            "<b>%s</b>\r\n", message);
    if(bytes > 0) client->con->sent_bytes = bytes;
116
    client->respcode = 404;
117 118 119
    client_destroy(client);
}

Michael Smith's avatar
Michael Smith committed
120 121 122
void client_send_504(client_t *client, char *message) {
    int bytes;
    client->respcode = 504;
123
    bytes = sock_write(client->con->sock, 
Michael Smith's avatar
Michael Smith committed
124 125 126
            "HTTP/1.0 504 Server Full\r\n"
            "Content-Type: text/html\r\n\r\n"
            "<b>%s</b>\r\n", message);
127 128
       if (bytes > 0) client->con->sent_bytes = bytes;
    client_destroy(client);
Michael Smith's avatar
Michael Smith committed
129 130
}

131 132 133 134 135 136 137
void client_send_401(client_t *client) {
    int bytes = sock_write(client->con->sock, 
            "HTTP/1.0 401 Authentication Required\r\n"
            "WWW-Authenticate: Basic realm=\"Icecast2 Server\"\r\n"
            "\r\n"
            "You need to authenticate\r\n");
    if(bytes > 0) client->con->sent_bytes = bytes;
138
    client->respcode = 401;
139 140
    client_destroy(client);
}
141 142 143 144 145 146 147 148 149 150

void client_send_403(client_t *client) {
    int bytes = sock_write(client->con->sock, 
            "HTTP/1.0 403 Forbidden\r\n"
            "\r\n"
            "Access restricted.\r\n");
    if(bytes > 0) client->con->sent_bytes = bytes;
    client->respcode = 403;
    client_destroy(client);
}
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166


/* helper function for sending the data to a client */
int client_send_bytes (client_t *client, const void *buf, unsigned len)
{
    int ret = sock_write_bytes (client->con->sock, buf, len);
    if (ret < 0 && !sock_recoverable (sock_error()))
    {
        DEBUG0 ("Client connection died");
        client->con->error = 1;
    }
    if (ret > 0)
        client->con->sent_bytes += ret;
    return ret;
}

Karl Heyes's avatar
Karl Heyes committed
167 168 169 170 171
void client_set_queue (client_t *client, refbuf_t *refbuf)
{
    refbuf_t *to_release = client->refbuf;

    client->refbuf = refbuf;
Karl Heyes's avatar
Karl Heyes committed
172 173
    if (refbuf)
        refbuf_addref (client->refbuf);
Karl Heyes's avatar
Karl Heyes committed
174 175 176 177 178
    client->pos = 0;
    if (to_release)
        refbuf_release (to_release);
}