MPD  0.20.23
HttpdClient.hxx
Go to the documentation of this file.
1 /*
2  * Copyright 2003-2017 The Music Player Daemon Project
3  * http://www.musicpd.org
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19 
20 #ifndef MPD_OUTPUT_HTTPD_CLIENT_HXX
21 #define MPD_OUTPUT_HTTPD_CLIENT_HXX
22 
23 #include "event/BufferedSocket.hxx"
24 #include "Compiler.h"
25 
26 #include <boost/intrusive/link_mode.hpp>
27 #include <boost/intrusive/list_hook.hpp>
28 
29 #include <queue>
30 #include <list>
31 
32 #include <stddef.h>
33 
34 class HttpdOutput;
35 class Page;
36 
37 class HttpdClient final
39  public boost::intrusive::list_base_hook<boost::intrusive::link_mode<boost::intrusive::normal_link>> {
43  HttpdOutput &httpd;
44 
48  enum {
50  REQUEST,
51 
53  HEADERS,
54 
56  RESPONSE,
57  } state;
58 
62  std::queue<Page *, std::list<Page *>> pages;
63 
67  size_t queue_size;
68 
72  Page *current_page;
73 
78  size_t current_position;
79 
83  bool head_method;
84 
85  /* ICY */
86 
91  bool metadata_supported;
92 
96  bool metadata_requested;
97 
101  bool metadata_sent;
102 
106  unsigned metaint;
107 
111  Page *metadata;
112 
113  /*
114  * The amount of bytes which were already sent from the metadata.
115  */
116  size_t metadata_current_position;
117 
122  unsigned metadata_fill;
123 
124 public:
129  HttpdClient(HttpdOutput &httpd, int _fd, EventLoop &_loop,
130  bool _metadata_supported);
131 
136  ~HttpdClient();
137 
141  void Close();
142 
143  void LockClose();
144 
148  void CancelQueue();
149 
153  bool HandleLine(const char *line);
154 
158  void BeginResponse();
159 
163  bool SendResponse();
164 
165  gcc_pure
166  ssize_t GetBytesTillMetaData() const noexcept;
167 
168  ssize_t TryWritePage(const Page &page, size_t position);
169  ssize_t TryWritePageN(const Page &page, size_t position, ssize_t n);
170 
171  bool TryWrite();
172 
176  void PushPage(Page *page);
177 
181  void PushMetaData(Page *page);
182 
183 private:
184  void ClearQueue();
185 
186 protected:
187  virtual bool OnSocketReady(unsigned flags) override;
188  virtual InputResult OnSocketInput(void *data, size_t length) override;
189  void OnSocketError(std::exception_ptr ep) override;
190  virtual void OnSocketClosed() override;
191 };
192 
193 #endif
gcc_pure ssize_t GetBytesTillMetaData() const noexcept
void PushPage(Page *page)
Appends a page to the client's queue.
void OnSocketError(std::exception_ptr ep) override
void LockClose()
HttpdClient(HttpdOutput &httpd, int _fd, EventLoop &_loop, bool _metadata_supported)
bool TryWrite()
An event loop that polls for events on file/socket descriptors.
Definition: Loop.hxx:52
std::make_signed< size_t >::type ssize_t
void CancelQueue()
Clears the page queue.
void PushMetaData(Page *page)
Sends the passed metadata.
void BeginResponse()
Switch the client to the "RESPONSE" state.
~HttpdClient()
Note: this does not remove the client from the HttpdOutput object.
virtual InputResult OnSocketInput(void *data, size_t length) override
Data has been received on the socket.
bool SendResponse()
Sends the status line and response headers to the client.
virtual void OnSocketClosed() override
ssize_t TryWritePageN(const Page &page, size_t position, ssize_t n)
A SocketMonitor specialization that adds an input buffer.
virtual bool OnSocketReady(unsigned flags) override
ssize_t TryWritePage(const Page &page, size_t position)
#define gcc_pure
Definition: Compiler.h:116
bool HandleLine(const char *line)
Handle a line of the HTTP request.
void Close()
Frees the client and removes it from the server's client list.
A dynamically allocated buffer which keeps track of its reference count.
Definition: Page.hxx:37