HttpdClient.hxx 4.26 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2017 The Music Player Daemon Project
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
 * 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.
 */

#ifndef MPD_OUTPUT_HTTPD_CLIENT_HXX
#define MPD_OUTPUT_HTTPD_CLIENT_HXX

23
#include "Page.hxx"
24
#include "event/BufferedSocket.hxx"
25
#include "Compiler.h"
26

27 28
#include <boost/intrusive/link_mode.hpp>
#include <boost/intrusive/list_hook.hpp>
29

30
#include <queue>
31 32 33 34
#include <list>

#include <stddef.h>

35
class UniqueSocketDescriptor;
36
class HttpdOutput;
37

38 39 40
class HttpdClient final
	: BufferedSocket,
	  public boost::intrusive::list_base_hook<boost::intrusive::link_mode<boost::intrusive::normal_link>> {
41 42 43
	/**
	 * The httpd output object this client is connected to.
	 */
44
	HttpdOutput &httpd;
45 46 47 48

	/**
	 * The current state of the client.
	 */
49
	enum class State {
50 51 52 53 54 55 56 57
		/** reading the request line */
		REQUEST,

		/** reading the request headers */
		HEADERS,

		/** sending the HTTP response */
		RESPONSE,
58
	} state = State::REQUEST;
59 60

	/**
Max Kellermann's avatar
Max Kellermann committed
61
	 * A queue of #Page objects to be sent to the client.
62
	 */
63
	std::queue<PagePtr, std::list<PagePtr>> pages;
64

65 66 67
	/**
	 * The sum of all page sizes in #pages.
	 */
68
	size_t queue_size = 0;
69

70 71 72
	/**
	 * The #page which is currently being sent to the client.
	 */
73
	PagePtr current_page;
74 75 76 77 78 79 80

	/**
	 * The amount of bytes which were already sent from
	 * #current_page.
	 */
	size_t current_position;

81 82 83
	/**
	 * Is this a HEAD request?
	 */
84
	bool head_method = false;
85

86 87 88 89 90 91 92 93 94 95 96
	/* ICY */

	/**
	 * Do we support sending Icy-Metadata to the client?  This is
	 * disabled if the httpd audio output uses encoder tags.
	 */
	bool metadata_supported;

	/**
	 * If we should sent icy metadata.
	 */
97
	bool metadata_requested = false;
98 99 100 101

	/**
	 * If the current metadata was already sent to the client.
	 */
102
	bool metadata_sent = false;
103 104 105 106

	/**
	 * The amount of streaming data between each metadata block
	 */
107
	unsigned metaint = 8192; /*TODO: just a std value */
108 109

	/**
Max Kellermann's avatar
Max Kellermann committed
110
	 * The metadata as #Page which is currently being sent to the client.
111
	 */
112
	PagePtr metadata;
113 114 115 116

	/*
	 * The amount of bytes which were already sent from the metadata.
	 */
117
	size_t metadata_current_position = 0;
118 119 120 121 122

	/**
	 * The amount of streaming data sent to the client
	 * since the last icy information was sent.
	 */
123
	unsigned metadata_fill = 0;
124 125 126 127

public:
	/**
	 * @param httpd the HTTP output device
Max Kellermann's avatar
Max Kellermann committed
128
	 * @param _fd the socket file descriptor
129
	 */
130
	HttpdClient(HttpdOutput &httpd, UniqueSocketDescriptor _fd,
131
		    EventLoop &_loop,
132
		    bool _metadata_supported);
133 134 135

	/**
	 * Note: this does not remove the client from the
136
	 * #HttpdOutput object.
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
	 */
	~HttpdClient();

	/**
	 * Frees the client and removes it from the server's client list.
	 */
	void Close();

	void LockClose();

	/**
	 * Clears the page queue.
	 */
	void CancelQueue();

	/**
	 * Handle a line of the HTTP request.
	 */
	bool HandleLine(const char *line);

	/**
158
	 * Switch the client to #State::RESPONSE.
159 160 161 162 163 164 165 166 167
	 */
	void BeginResponse();

	/**
	 * Sends the status line and response headers to the client.
	 */
	bool SendResponse();

	gcc_pure
168
	ssize_t GetBytesTillMetaData() const noexcept;
169

170 171 172 173
	ssize_t TryWritePage(const Page &page, size_t position);
	ssize_t TryWritePageN(const Page &page, size_t position, ssize_t n);

	bool TryWrite();
174 175 176 177

	/**
	 * Appends a page to the client's queue.
	 */
178
	void PushPage(PagePtr page);
179 180 181 182

	/**
	 * Sends the passed metadata.
	 */
183
	void PushMetaData(PagePtr page);
184

185 186 187
private:
	void ClearQueue();

188
protected:
189 190 191
	/* virtual methods from class SocketMonitor */
	bool OnSocketReady(unsigned flags) noexcept override;

192 193 194
	InputResult OnSocketInput(void *data, size_t length) noexcept override;
	void OnSocketError(std::exception_ptr ep) noexcept override;
	void OnSocketClosed() noexcept override;
195 196 197
};

#endif