client.c 3.73 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 31 32 33 34 35

#include "connection.h"
#include "refbuf.h"

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

36 37 38
#undef CATMODULE
#define CATMODULE "client"

Jack Moffitt's avatar
Jack Moffitt committed
39 40
client_t *client_create(connection_t *con, http_parser_t *parser)
{
41
    client_t *client = (client_t *)calloc(1, sizeof(client_t));
Jack Moffitt's avatar
Jack Moffitt committed
42

43 44 45 46
    client->con = con;
    client->parser = parser;
    client->queue = NULL;
    client->pos = 0;
47
    client->burst_sent = 0;
Jack Moffitt's avatar
Jack Moffitt committed
48

49
    return client;
Jack Moffitt's avatar
Jack Moffitt committed
50 51 52 53
}

void client_destroy(client_t *client)
{
54
    refbuf_t *refbuf;
Jack Moffitt's avatar
Jack Moffitt committed
55

Karl Heyes's avatar
Karl Heyes committed
56 57
    if (client == NULL)
        return;
58
    /* write log entry if ip is set (some things don't set it, like outgoing 
59 60 61
     * slave requests
     */
    if(client->con->ip)
62 63 64 65
        logging_access(client);
    
    connection_close(client->con);
    httpp_destroy(client->parser);
Jack Moffitt's avatar
Jack Moffitt committed
66

67 68
    while ((refbuf = refbuf_queue_remove(&client->queue)))
        refbuf_release(refbuf);
Jack Moffitt's avatar
Jack Moffitt committed
69

70 71 72 73
    /* 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
74 75
    free(client->username);

76
    free(client);
Jack Moffitt's avatar
Jack Moffitt committed
77
}
78

79 80
void client_send_400(client_t *client, char *message) {
    int bytes;
81
    bytes = sock_write(client->con->sock, "HTTP/1.0 400 Bad Request\r\n"
82 83 84
            "Content-Type: text/html\r\n\r\n"
            "<b>%s</b>\r\n", message);
    if(bytes > 0) client->con->sent_bytes = bytes;
85
    client->respcode = 400;
86 87 88
    client_destroy(client);
}

89 90 91 92 93 94 95
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;
96
    client->respcode = 404;
97 98 99
    client_destroy(client);
}

Michael Smith's avatar
Michael Smith committed
100 101 102
void client_send_504(client_t *client, char *message) {
    int bytes;
    client->respcode = 504;
103
    bytes = sock_write(client->con->sock, 
Michael Smith's avatar
Michael Smith committed
104 105 106
            "HTTP/1.0 504 Server Full\r\n"
            "Content-Type: text/html\r\n\r\n"
            "<b>%s</b>\r\n", message);
107 108
       if (bytes > 0) client->con->sent_bytes = bytes;
    client_destroy(client);
Michael Smith's avatar
Michael Smith committed
109 110
}

111 112 113 114 115 116 117
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;
118
    client->respcode = 401;
119 120
    client_destroy(client);
}
121 122 123 124 125 126 127 128 129 130

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);
}
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146


/* 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;
}