summaryrefslogtreecommitdiff
path: root/http_send.c
blob: 0c48934aea55747fa9e24e9393b903d0b4754263 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/*
 * Copyright (C) 2005-2009 Andre Noll <maan@systemlinux.org>
 *
 * Licensed under the GPL v2. For licencing details see COPYING.
 */

/** \file http_send.c paraslash's http sender */

#include <sys/types.h>
#include <dirent.h>

#include "para.h"
#include "error.h"
#include "string.h"
#include "server.cmdline.h"
#include "afh.h"
#include "afs.h"
#include "server.h"
#include "http.h"
#include "vss.h"
#include "list.h"
#include "send.h"
#include "close_on_fork.h"
#include "net.h"
#include "fd.h"
#include "chunk_queue.h"
#include "acl.h"

/** Message sent to clients that do not send a valid get request. */
#define HTTP_ERR_MSG "HTTP/1.0 400 Bad Request\n"

/** The possible states of a client from the server's POV. */
enum http_client_status {
	/** We accepted the connection on the tcp socket. */
	HTTP_CONNECTED,
	/** Successfully received the get request. */
	HTTP_GOT_GET_REQUEST,
	/** Connection is ready for sending audio data. */
	HTTP_STREAMING,
	/** We didn't receive a valid get request. */
	HTTP_INVALID_GET_REQUEST
};

/** For each connected client, a structure of this type is maintained. */
struct private_http_sender_data {
	/** The current state of this client. */
	enum http_client_status status;
};

static struct sender_status http_sender_status, *hss = &http_sender_status;

static int http_send_msg(struct sender_client *sc, const char *msg)
{
	int ret = send_buffer(sc->fd, msg);

	if (ret < 0)
		shutdown_client(sc, hss);
	return ret;
}

static void http_send_ok_msg(struct sender_client *sc)
{
	PARA_INFO_LOG("sending http ok message to fd %d\n", sc->fd);
	http_send_msg(sc, HTTP_OK_MSG);
}

static int http_send_err_msg(struct sender_client *sc)
{
	PARA_NOTICE_LOG("sending bad request message to fd %d\n", sc->fd);
	return http_send_msg(sc, HTTP_ERR_MSG);
}

static void http_shutdown_clients(void)
{
	shutdown_clients(hss);
}

static void http_send(long unsigned current_chunk,
	__a_unused long unsigned chunks_sent, const char *buf, size_t len,
	const char *header_buf, size_t header_len)
{
	struct sender_client *sc, *tmp;

	list_for_each_entry_safe(sc, tmp, &hss->client_list, node) {
		struct private_http_sender_data *phsd = sc->private_data;
		if (phsd->status != HTTP_STREAMING)
			continue;
		send_chunk(sc, hss, 0, current_chunk, buf, len, header_buf,
			header_len);
	}
}

static void http_post_select(fd_set *rfds, __a_unused fd_set *wfds)
{
	struct sender_client *sc, *tmp;
	struct private_http_sender_data *phsd;

	if (hss->listen_fd < 0)
		return;
	list_for_each_entry_safe(sc, tmp, &hss->client_list, node) {
		phsd = sc->private_data;
		switch (phsd->status) {
		case HTTP_STREAMING: /* nothing to do */
			break;
		case HTTP_CONNECTED: /* need to recv get request */
			if (FD_ISSET(sc->fd, rfds)) {
				if (recv_pattern(sc->fd, HTTP_GET_MSG, MAXLINE)
						< 0) {
					phsd->status = HTTP_INVALID_GET_REQUEST;
				} else {
					phsd->status = HTTP_GOT_GET_REQUEST;
					PARA_INFO_LOG("received get request\n");
				}
			}
			break;
		case HTTP_GOT_GET_REQUEST: /* need to send ok msg */
			phsd->status = HTTP_STREAMING;
			http_send_ok_msg(sc);
			break;
		case HTTP_INVALID_GET_REQUEST: /* need to send err msg */
			if (http_send_err_msg(sc) >= 0)
				shutdown_client(sc, hss);
			break;
		}
	}
	if (!FD_ISSET(hss->listen_fd, rfds))
		return;
	sc = accept_sender_client(hss);
	if (!sc)
		return;
	phsd = para_malloc(sizeof(*phsd));
	sc->private_data = phsd;
	phsd->status = HTTP_CONNECTED;
}

static void http_pre_select(int *max_fileno, fd_set *rfds, fd_set *wfds)
{
	struct sender_client *sc, *tmp;

	if (hss->listen_fd < 0)
		return;
	para_fd_set(hss->listen_fd, rfds, max_fileno);
	list_for_each_entry_safe(sc, tmp, &hss->client_list, node) {
		struct private_http_sender_data *phsd = sc->private_data;
		if (phsd->status == HTTP_CONNECTED) /* need to recv get request */
			para_fd_set(sc->fd, rfds, max_fileno);
		if (phsd->status == HTTP_GOT_GET_REQUEST ||
				phsd->status == HTTP_INVALID_GET_REQUEST)
			para_fd_set(sc->fd, wfds, max_fileno);
	}
}

static int http_com_on(__a_unused struct sender_command_data *scd)
{
	return generic_com_on(hss, IPPROTO_TCP);
}

static int http_com_off(__a_unused struct sender_command_data *scd)
{
	generic_com_off(hss);
	return 1;
}

static int http_com_deny(struct sender_command_data *scd)
{
	generic_com_deny(scd, hss);
	return 1;
}

static int http_com_allow(struct sender_command_data *scd)
{
	generic_com_allow(scd, hss);
	return 1;
}

static char *http_info(void)
{
	return get_sender_info(hss, "http");
}

/**
 * The init function of the http sender.
 *
 * \param s Pointer to the http sender struct.
 *
 * It initializes all function pointers of \a s, the client list and the access
 * control list. If the autostart option was given, the tcp port is opened.
 */
void http_send_init(struct sender *s)
{
	int ret;
	s->info = http_info;
	s->send = http_send;
	s->pre_select = http_pre_select;
	s->post_select = http_post_select;
	s->shutdown_clients = http_shutdown_clients;
	s->help = generic_sender_help;
	s->client_cmds[SENDER_ON] = http_com_on;
	s->client_cmds[SENDER_OFF] = http_com_off;
	s->client_cmds[SENDER_DENY] = http_com_deny;
	s->client_cmds[SENDER_ALLOW] = http_com_allow;
	s->client_cmds[SENDER_ADD] = NULL;
	s->client_cmds[SENDER_DELETE] = NULL;

	init_sender_status(hss, conf.http_access_arg, conf.http_access_given,
		conf.http_port_arg, conf.http_max_clients_arg,
		conf.http_default_deny_given);
	if (conf.http_no_autostart_given)
		return;
	ret = generic_com_on(hss, IPPROTO_TCP);
	if (ret < 0)
		PARA_ERROR_LOG("%s\n", para_strerror(-ret));
}