client.c 2.35 KB
Newer Older
Jack Moffitt's avatar
Jack Moffitt committed
1 2 3 4 5 6
/* client.c
**
** client interface implementation
**
*/

7 8 9 10
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

Jack Moffitt's avatar
Jack Moffitt committed
11 12 13
#include <stdlib.h>
#include <string.h>

Karl Heyes's avatar
Karl Heyes committed
14 15 16
#include "thread/thread.h"
#include "avl/avl.h"
#include "httpp/httpp.h"
Jack Moffitt's avatar
Jack Moffitt committed
17 18 19 20 21 22 23 24 25

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

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

client_t *client_create(connection_t *con, http_parser_t *parser)
{
26
    client_t *client = (client_t *)calloc(1, sizeof(client_t));
Jack Moffitt's avatar
Jack Moffitt committed
27

28 29 30 31
    client->con = con;
    client->parser = parser;
    client->queue = NULL;
    client->pos = 0;
Jack Moffitt's avatar
Jack Moffitt committed
32

33
    return client;
Jack Moffitt's avatar
Jack Moffitt committed
34 35 36 37
}

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

40
    /* write log entry if ip is set (some things don't set it, like outgoing 
41 42 43
     * slave requests
     */
    if(client->con->ip)
44 45 46 47
        logging_access(client);
    
    connection_close(client->con);
    httpp_destroy(client->parser);
Jack Moffitt's avatar
Jack Moffitt committed
48

49 50
    while ((refbuf = refbuf_queue_remove(&client->queue)))
        refbuf_release(refbuf);
Jack Moffitt's avatar
Jack Moffitt committed
51

52
    free(client);
Jack Moffitt's avatar
Jack Moffitt committed
53
}
54

55 56 57 58 59 60 61 62 63 64
void client_send_400(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;
    client->respcode = 404;
    client_destroy(client);
}

65 66 67 68 69 70 71
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;
72
    client->respcode = 404;
73 74 75
    client_destroy(client);
}

Michael Smith's avatar
Michael Smith committed
76 77 78
void client_send_504(client_t *client, char *message) {
    int bytes;
    client->respcode = 504;
79
    bytes = sock_write(client->con->sock, 
Michael Smith's avatar
Michael Smith committed
80 81 82
            "HTTP/1.0 504 Server Full\r\n"
            "Content-Type: text/html\r\n\r\n"
            "<b>%s</b>\r\n", message);
83 84
       if (bytes > 0) client->con->sent_bytes = bytes;
    client_destroy(client);
Michael Smith's avatar
Michael Smith committed
85 86
}

87 88 89 90 91 92 93
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;
94
    client->respcode = 401;
95 96
    client_destroy(client);
}