HttpdClient.cxx 9.55 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2020 The Music Player Daemon Project
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
 * http://www.musicpd.org
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include "HttpdClient.hxx"
#include "HttpdInternal.hxx"
22
#include "util/ASCII.hxx"
23
#include "util/AllocatedString.hxx"
Max Kellermann's avatar
Max Kellermann committed
24
#include "Page.hxx"
25
#include "IcyMetaDataServer.hxx"
26
#include "net/SocketError.hxx"
27
#include "net/UniqueSocketDescriptor.hxx"
28
#include "Log.hxx"
29 30 31

#include <assert.h>
#include <string.h>
32
#include <stdio.h>
33

34
HttpdClient::~HttpdClient() noexcept
35
{
36 37
	if (IsDefined())
		BufferedSocket::Close();
38 39 40
}

void
41
HttpdClient::Close() noexcept
42
{
43
	httpd.RemoveClient(*this);
44 45 46
}

void
47
HttpdClient::LockClose() noexcept
48
{
49
	const std::lock_guard<Mutex> protect(httpd.mutex);
50 51 52 53
	Close();
}

void
54
HttpdClient::BeginResponse() noexcept
55
{
56
	assert(state != State::RESPONSE);
57

58
	state = State::RESPONSE;
59 60
	current_page = nullptr;

61
	if (!head_method)
62
		httpd.SendHeader(*this);
63 64 65 66 67 68
}

/**
 * Handle a line of the HTTP request.
 */
bool
69
HttpdClient::HandleLine(const char *line) noexcept
70
{
71
	assert(state != State::RESPONSE);
72

73
	if (state == State::REQUEST) {
74
		if (strncmp(line, "HEAD /", 6) == 0) {
75 76
			line += 6;
			head_method = true;
77
		} else if (strncmp(line, "GET /", 5) == 0) {
78 79
			line += 5;
		} else {
80
			/* only GET is supported */
81 82
			LogWarning(httpd_output_domain,
				   "malformed request line from client");
83 84 85
			return false;
		}

86 87 88 89 90 91 92 93 94 95 96
		/* blacklist some well-known request paths */
		if ((strncmp(line, "favicon.ico", 11) == 0 &&
		     (line[11] == '\0' || line[11] == ' ')) ||
		    (strncmp(line, "robots.txt", 10) == 0 &&
		     (line[10] == '\0' || line[10] == ' ')) ||
		    (strncmp(line, "sitemap.xml", 11) == 0 &&
		     (line[11] == '\0' || line[11] == ' ')) ||
		    (strncmp(line, ".well-known/", 12) == 0)) {
			should_reject = true;
		}

97
		line = strchr(line, ' ');
98
		if (line == nullptr || strncmp(line + 1, "HTTP/", 5) != 0) {
99
			/* HTTP/0.9 without request headers */
100 101 102 103

			if (head_method)
				return false;

104 105 106 107 108
			BeginResponse();
			return true;
		}

		/* after the request line, request headers follow */
109
		state = State::HEADERS;
110 111 112 113
		return true;
	} else {
		if (*line == 0) {
			/* empty line: request is finished */
114

115 116 117 118
			BeginResponse();
			return true;
		}

119 120
		if (StringEqualsCaseASCII(line, "Icy-MetaData: 1", 15) ||
		    StringEqualsCaseASCII(line, "Icy-MetaData:1", 14)) {
121 122 123 124 125 126 127 128 129 130 131 132 133 134
			/* Send icy metadata */
			metadata_requested = metadata_supported;
			return true;
		}

		/* expect more request headers */
		return true;
	}
}

/**
 * Sends the status line and response headers to the client.
 */
bool
135
HttpdClient::SendResponse() noexcept
136
{
137 138
	char buffer[1024];
	AllocatedString<> allocated = nullptr;
139 140
	const char *response;

141
	assert(state == State::RESPONSE);
142

143 144 145 146 147 148 149 150
	if (should_reject) {
		response =
			"HTTP/1.1 404 not found\r\n"
			"Content-Type: text/plain\r\n"
			"Connection: close\r\n"
			"\r\n"
			"404 not found";
	} else if (metadata_requested) {
151
		allocated =
152 153 154
			icy_server_metadata_header(httpd.name, httpd.genre,
						   httpd.website,
						   httpd.content_type,
155
						   metaint);
156
		response = allocated.c_str();
157
	} else { /* revert to a normal HTTP request */
158 159 160 161 162 163 164
		snprintf(buffer, sizeof(buffer),
			 "HTTP/1.1 200 OK\r\n"
			 "Content-Type: %s\r\n"
			 "Connection: close\r\n"
			 "Pragma: no-cache\r\n"
			 "Cache-Control: no-cache, no-store\r\n"
			 "\r\n",
165
			 httpd.content_type);
166
		response = buffer;
167 168
	}

169
	ssize_t nbytes = GetSocket().Write(response, strlen(response));
170 171
	if (gcc_unlikely(nbytes < 0)) {
		const SocketErrorMessage msg;
172 173 174
		FormatWarning(httpd_output_domain,
			      "failed to write to client: %s",
			      (const char *)msg);
175
		LockClose();
176 177 178 179 180 181
		return false;
	}

	return true;
}

182
HttpdClient::HttpdClient(HttpdOutput &_httpd, UniqueSocketDescriptor _fd,
183
			 EventLoop &_loop,
184
			 bool _metadata_supported)
185
	:BufferedSocket(_fd.Release(), _loop),
186
	 httpd(_httpd),
187
	 metadata_supported(_metadata_supported)
188 189 190 191
{
}

void
192
HttpdClient::ClearQueue() noexcept
193
{
194
	assert(state == State::RESPONSE);
195

196 197
	while (!pages.empty()) {
#ifndef NDEBUG
198
		auto &page = pages.front();
199 200
		assert(queue_size >= page->GetSize());
		queue_size -= page->GetSize();
201 202
#endif

203
		pages.pop();
204 205 206
	}

	assert(queue_size == 0);
207 208 209
}

void
210
HttpdClient::CancelQueue() noexcept
211
{
212
	if (state != State::RESPONSE)
213 214 215
		return;

	ClearQueue();
216

217 218
	if (current_page == nullptr)
		CancelWrite();
219 220
}

221
ssize_t
222
HttpdClient::TryWritePage(const Page &page, size_t position) noexcept
223
{
224
	assert(position < page.GetSize());
225

226 227
	return GetSocket().Write(page.GetData() + position,
				 page.GetSize() - position);
228 229
}

230
ssize_t
231 232
HttpdClient::TryWritePageN(const Page &page,
			   size_t position, ssize_t n) noexcept
233
{
234
	return n >= 0
235
		? GetSocket().Write(page.GetData() + position, n)
236
		: TryWritePage(page, position);
237 238
}

239
ssize_t
240
HttpdClient::GetBytesTillMetaData() const noexcept
241 242
{
	if (metadata_requested &&
243
	    current_page->GetSize() - current_position > metaint - metadata_fill)
244 245 246 247 248 249
		return metaint - metadata_fill;

	return -1;
}

inline bool
250
HttpdClient::TryWrite() noexcept
251
{
252
	const std::lock_guard<Mutex> protect(httpd.mutex);
253

254
	assert(state == State::RESPONSE);
255 256

	if (current_page == nullptr) {
257 258 259
		if (pages.empty()) {
			/* another thread has removed the event source
			   while this thread was waiting for
260
			   httpd.mutex */
261 262 263 264
			CancelWrite();
			return true;
		}

265
		current_page = pages.front();
266
		pages.pop();
267
		current_position = 0;
268

269 270
		assert(queue_size >= current_page->GetSize());
		queue_size -= current_page->GetSize();
271 272
	}

273
	const ssize_t bytes_to_write = GetBytesTillMetaData();
274 275
	if (bytes_to_write == 0) {
		if (!metadata_sent) {
276 277 278 279 280 281 282 283 284
			ssize_t nbytes = TryWritePage(*metadata,
						      metadata_current_position);
			if (nbytes < 0) {
				auto e = GetSocketError();
				if (IsSocketErrorAgain(e))
					return true;

				if (!IsSocketErrorClosed(e)) {
					SocketErrorMessage msg(e);
285 286 287
					FormatWarning(httpd_output_domain,
						      "failed to write to client: %s",
						      (const char *)msg);
288 289 290 291 292
				}

				Close();
				return false;
			}
293

294
			metadata_current_position += nbytes;
295

296
			if (metadata->GetSize() - metadata_current_position == 0) {
297 298 299 300 301
				metadata_fill = 0;
				metadata_current_position = 0;
				metadata_sent = true;
			}
		} else {
302
			char empty_data = 0;
303

304
			ssize_t nbytes = GetSocket().Write(&empty_data, 1);
305 306 307 308
			if (nbytes < 0) {
				auto e = GetSocketError();
				if (IsSocketErrorAgain(e))
					return true;
309

310 311
				if (!IsSocketErrorClosed(e)) {
					SocketErrorMessage msg(e);
312 313 314
					FormatWarning(httpd_output_domain,
						      "failed to write to client: %s",
						      (const char *)msg);
315
				}
316

317 318
				Close();
				return false;
319
			}
Max Kellermann's avatar
Max Kellermann committed
320

321 322
			metadata_fill = 0;
			metadata_current_position = 0;
323 324
		}
	} else {
325 326 327 328 329 330 331 332 333 334
		ssize_t nbytes =
			TryWritePageN(*current_page, current_position,
				      bytes_to_write);
		if (nbytes < 0) {
			auto e = GetSocketError();
			if (IsSocketErrorAgain(e))
				return true;

			if (!IsSocketErrorClosed(e)) {
				SocketErrorMessage msg(e);
335 336 337
				FormatWarning(httpd_output_domain,
					      "failed to write to client: %s",
					      (const char *)msg);
338
			}
339

340 341 342 343 344
			Close();
			return false;
		}

		current_position += nbytes;
345
		assert(current_position <= current_page->GetSize());
346 347

		if (metadata_requested)
348
			metadata_fill += nbytes;
349

350
		if (current_position >= current_page->GetSize()) {
351
			current_page.reset();
352

353
			if (pages.empty())
354 355
				/* all pages are sent: remove the
				   event source */
356
				CancelWrite();
357 358 359
		}
	}

360
	return true;
361 362 363
}

void
364
HttpdClient::PushPage(PagePtr page) noexcept
365
{
366
	if (state != State::RESPONSE)
367 368 369
		/* the client is still writing the HTTP request */
		return;

370 371 372 373 374 375
	if (queue_size > 256 * 1024) {
		FormatDebug(httpd_output_domain,
			    "client is too slow, flushing its queue");
		ClearQueue();
	}

376
	queue_size += page->GetSize();
377
	pages.emplace(std::move(page));
378

379
	ScheduleWrite();
380 381 382
}

void
383
HttpdClient::PushMetaData(PagePtr page) noexcept
384
{
385 386
	assert(page != nullptr);

387
	metadata = std::move(page);
388 389
	metadata_sent = false;
}
390 391

bool
392
HttpdClient::OnSocketReady(unsigned flags) noexcept
393 394 395 396 397 398 399 400 401 402 403 404
{
	if (!BufferedSocket::OnSocketReady(flags))
		return false;

	if (flags & WRITE)
		if (!TryWrite())
			return false;

	return true;
}

BufferedSocket::InputResult
405
HttpdClient::OnSocketInput(void *data, size_t length) noexcept
406
{
407
	if (state == State::RESPONSE) {
408 409
		LogWarning(httpd_output_domain,
			   "unexpected input from client");
410 411 412 413
		LockClose();
		return InputResult::CLOSED;
	}

414 415
	char *line = (char *)data;
	char *newline = (char *)memchr(line, '\n', length);
416 417 418 419 420 421 422 423
	if (newline == nullptr)
		return InputResult::MORE;

	ConsumeInput(newline + 1 - line);

	if (newline > line && newline[-1] == '\r')
		--newline;

424 425
	/* terminate the string at the end of the line */
	*newline = 0;
426 427 428 429 430 431

	if (!HandleLine(line)) {
		LockClose();
		return InputResult::CLOSED;
	}

432
	if (state == State::RESPONSE) {
433 434 435
		if (!SendResponse())
			return InputResult::CLOSED;

436
		if (head_method || should_reject) {
437 438 439 440
			LockClose();
			return InputResult::CLOSED;
		}
	}
441 442 443 444 445

	return InputResult::AGAIN;
}

void
446
HttpdClient::OnSocketError(std::exception_ptr ep) noexcept
447
{
448
	LogError(ep);
449
	LockClose();
450 451 452
}

void
453
HttpdClient::OnSocketClosed() noexcept
454 455 456
{
	LockClose();
}