2018-01-14 02:55:21 +01:00
|
|
|
// Copyright (C) Matrix Construct Developers, Authors & Contributors
|
|
|
|
// Copyright (C) 2016-2018 Jason Volk
|
|
|
|
//
|
|
|
|
// Permission to use, copy, modify, and/or distribute this software for any
|
|
|
|
// purpose with or without fee is hereby granted, provided that the above
|
|
|
|
// copyright notice and this permission notice is present in all copies.
|
|
|
|
//
|
|
|
|
// THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|
|
|
// IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
|
|
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
// DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
|
|
|
|
// INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
|
|
|
// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
// HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
|
|
|
// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
|
|
|
|
// IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
// POSSIBILITY OF SUCH DAMAGE.
|
2007-01-25 07:40:21 +01:00
|
|
|
|
2016-08-13 05:05:54 +02:00
|
|
|
#pragma once
|
|
|
|
#define HAVE_IRCD_CLIENT_H
|
2016-08-24 00:25:09 +02:00
|
|
|
|
2017-08-28 23:51:22 +02:00
|
|
|
namespace ircd
|
|
|
|
{
|
|
|
|
struct client;
|
|
|
|
|
|
|
|
const char *write(client &, const char *&start, const char *const &stop);
|
|
|
|
char *read(client &, char *&start, char *const &stop);
|
2017-09-30 08:04:41 +02:00
|
|
|
|
2017-08-28 23:51:22 +02:00
|
|
|
http::response::write_closure write_closure(client &);
|
|
|
|
parse::read_closure read_closure(client &);
|
2017-09-30 08:04:41 +02:00
|
|
|
|
2018-01-14 02:55:21 +01:00
|
|
|
void close_all_clients();
|
2018-01-09 03:08:26 +01:00
|
|
|
|
2017-08-28 23:51:22 +02:00
|
|
|
std::shared_ptr<client> add_client(std::shared_ptr<socket>); // Creates a client.
|
|
|
|
}
|
2016-09-12 23:07:46 +02:00
|
|
|
|
2017-08-28 23:51:22 +02:00
|
|
|
struct ircd::client
|
2017-08-23 22:33:31 +02:00
|
|
|
:std::enable_shared_from_this<client>
|
2018-01-14 02:55:21 +01:00
|
|
|
,ircd::instance_list<client>
|
2016-09-12 23:07:46 +02:00
|
|
|
{
|
2016-11-29 16:23:38 +01:00
|
|
|
struct init;
|
2018-01-14 02:55:21 +01:00
|
|
|
struct conf;
|
|
|
|
struct settings;
|
|
|
|
struct request;
|
2017-09-30 08:04:41 +02:00
|
|
|
|
2018-01-14 02:55:21 +01:00
|
|
|
static struct settings settings;
|
|
|
|
static struct conf default_conf;
|
|
|
|
static ctx::pool context;
|
2016-09-23 08:59:24 +02:00
|
|
|
|
2016-11-29 16:23:38 +01:00
|
|
|
std::shared_ptr<socket> sock;
|
2018-01-14 02:55:21 +01:00
|
|
|
struct conf *conf {&default_conf};
|
|
|
|
struct request *request {nullptr};
|
|
|
|
|
|
|
|
void close(const net::close_opts &, net::close_callback);
|
|
|
|
ctx::future<void> close(const net::close_opts & = {});
|
2016-09-23 08:59:24 +02:00
|
|
|
|
2018-01-14 02:55:21 +01:00
|
|
|
void discard_unconsumed(struct request &);
|
|
|
|
bool resource_request(struct request &);
|
|
|
|
bool handle_request(parse::capstan &pc);
|
2017-03-11 02:46:25 +01:00
|
|
|
bool main() noexcept;
|
2018-01-14 02:55:21 +01:00
|
|
|
void async();
|
2016-09-23 08:59:24 +02:00
|
|
|
|
2016-11-29 16:23:38 +01:00
|
|
|
public:
|
2017-03-14 00:11:30 +01:00
|
|
|
client(std::shared_ptr<socket>);
|
2017-03-11 02:46:25 +01:00
|
|
|
client();
|
2016-11-29 16:23:38 +01:00
|
|
|
client(client &&) = delete;
|
|
|
|
client(const client &) = delete;
|
|
|
|
client &operator=(client &&) = delete;
|
|
|
|
client &operator=(const client &) = delete;
|
2018-01-27 06:58:52 +01:00
|
|
|
~client() noexcept;
|
2017-08-28 23:51:22 +02:00
|
|
|
|
2018-01-07 06:36:17 +01:00
|
|
|
friend ipport remote(const client &);
|
|
|
|
friend ipport local(const client &);
|
2016-09-23 08:59:24 +02:00
|
|
|
};
|
|
|
|
|
2018-01-14 02:55:21 +01:00
|
|
|
/// Organizes components of an individual request. A pointer to this structure
|
|
|
|
/// is placed as a member of client when a request is being made; this allows
|
|
|
|
/// for access to it without a separate argument wherever client goes.
|
|
|
|
struct ircd::client::request
|
|
|
|
{
|
|
|
|
static constexpr size_t HEAD_MAX
|
|
|
|
{
|
|
|
|
4_KiB
|
|
|
|
};
|
|
|
|
|
|
|
|
ircd::timer timer;
|
|
|
|
http::request::head head;
|
|
|
|
size_t content_consumed {0};
|
|
|
|
string_view content_partial;
|
|
|
|
|
|
|
|
request(parse::capstan &pc);
|
|
|
|
};
|
|
|
|
|
|
|
|
/// Confs can be attached to individual clients to change their behavior
|
|
|
|
struct ircd::client::conf
|
|
|
|
{
|
|
|
|
/// Default time limit for how long a client connection can be in "async mode"
|
|
|
|
/// (or idle mode) after which it is disconnected.
|
|
|
|
seconds async_timeout {35s};
|
|
|
|
|
|
|
|
/// Time limit for how long a connected client can be in "request mode." This
|
|
|
|
/// should never be hit unless there's an error in the handling code.
|
2018-01-20 14:46:54 +01:00
|
|
|
seconds request_timeout {15s};
|
2018-01-14 02:55:21 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/// Settings apply to all clients and cannot be configured per-client
|
|
|
|
struct ircd::client::settings
|
|
|
|
{
|
|
|
|
/// TODO
|
|
|
|
size_t stack_size
|
|
|
|
{
|
|
|
|
1_MiB
|
|
|
|
};
|
|
|
|
|
|
|
|
/// TODO
|
|
|
|
size_t pool_size
|
|
|
|
{
|
|
|
|
128
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2017-08-28 23:51:22 +02:00
|
|
|
struct ircd::client::init
|
2016-08-28 10:16:24 +02:00
|
|
|
{
|
2017-11-01 23:51:24 +01:00
|
|
|
void interrupt();
|
|
|
|
|
2016-11-29 16:23:38 +01:00
|
|
|
init();
|
|
|
|
~init() noexcept;
|
|
|
|
};
|