2018-02-04 03:22:01 +01:00
|
|
|
// Matrix Construct
|
|
|
|
//
|
2018-01-13 03:57:58 +01:00
|
|
|
// Copyright (C) Matrix Construct Developers, Authors & Contributors
|
2018-02-04 03:22:01 +01:00
|
|
|
// Copyright (C) 2016-2018 Jason Volk <jason@zemos.net>
|
2018-01-13 03:57:58 +01:00
|
|
|
//
|
|
|
|
// Permission to use, copy, modify, and/or distribute this software for any
|
|
|
|
// purpose with or without fee is hereby granted, provided that the above
|
2018-02-04 03:22:01 +01:00
|
|
|
// copyright notice and this permission notice is present in all copies. The
|
|
|
|
// full license for this software is available in the LICENSE file.
|
2017-10-25 18:39:58 +02:00
|
|
|
|
|
|
|
#pragma once
|
2018-01-14 03:03:04 +01:00
|
|
|
#define HAVE_IRCD_SERVER_REQUEST_H
|
2017-10-25 18:39:58 +02:00
|
|
|
|
|
|
|
/// The interface for when IRCd plays the role of client to other servers
|
|
|
|
///
|
2018-01-13 03:57:58 +01:00
|
|
|
namespace ircd::server
|
2017-10-25 18:39:58 +02:00
|
|
|
{
|
2018-01-13 03:57:58 +01:00
|
|
|
struct in;
|
2018-01-14 03:03:04 +01:00
|
|
|
struct out;
|
|
|
|
struct request;
|
2018-01-16 03:04:23 +01:00
|
|
|
|
|
|
|
size_t size(const in &);
|
|
|
|
size_t size(const out &);
|
|
|
|
|
|
|
|
void submit(const hostport &, request &);
|
2018-01-17 06:23:15 +01:00
|
|
|
bool cancel(request &);
|
2018-01-13 03:57:58 +01:00
|
|
|
}
|
2017-10-25 18:39:58 +02:00
|
|
|
|
2018-01-14 06:16:49 +01:00
|
|
|
/// Request data and options related to transmitting the request. This
|
|
|
|
/// is where buffers must be supplied to send data to the server.
|
|
|
|
///
|
2018-01-14 03:03:04 +01:00
|
|
|
struct ircd::server::out
|
|
|
|
{
|
|
|
|
const_buffer head;
|
|
|
|
const_buffer content;
|
2018-01-21 14:01:25 +01:00
|
|
|
|
|
|
|
/// The progress closure is an optional callback invoked every time more
|
|
|
|
/// content is written to the socket. The first argument is a view of the
|
|
|
|
/// data most recently written. The second argument is a view of all data
|
|
|
|
/// written so far. This is only invoked for content. At the first
|
|
|
|
/// invocation, the head has been fully written.
|
|
|
|
std::function<void (const_buffer, const_buffer) noexcept> progress;
|
2018-01-14 03:03:04 +01:00
|
|
|
};
|
|
|
|
|
2018-01-14 06:16:49 +01:00
|
|
|
/// Request data and options related to the receive side of the request.
|
|
|
|
/// This is where buffers are supplied to receive data from the remote
|
|
|
|
/// server.
|
|
|
|
///
|
2018-01-20 11:30:20 +01:00
|
|
|
/// As a feature, when content == head, the head buffer is considered
|
|
|
|
/// as a contiguous buffer for both head and content; the content buffer
|
|
|
|
/// will be updated to point to any data after the head is received.
|
|
|
|
///
|
2018-01-14 06:16:49 +01:00
|
|
|
struct ircd::server::in
|
|
|
|
{
|
2018-01-16 08:41:24 +01:00
|
|
|
mutable_buffer head;
|
2018-01-20 11:30:20 +01:00
|
|
|
mutable_buffer content {head};
|
2018-01-21 13:37:24 +01:00
|
|
|
|
|
|
|
/// The progress closure is an optional callback invoked every time more
|
|
|
|
/// content is read from the socket. The first argument is a view of the
|
|
|
|
/// data most recently received. The second argument is a view of all data
|
|
|
|
/// received so far. This is only invoked for content, not for the head;
|
|
|
|
/// however the first time it is invoked it is safe to view the in.head
|
|
|
|
std::function<void (const_buffer, const_buffer) noexcept> progress;
|
2018-02-27 06:56:05 +01:00
|
|
|
|
|
|
|
/// The dynamic buffer is a convenience that allows for the content buffer
|
|
|
|
/// to be allocated on demand once the head is received and the length is
|
|
|
|
/// known. To use dynamic, set the content buffer to nothing (i.e default
|
|
|
|
/// constructed mutable_buffer).
|
|
|
|
unique_buffer<mutable_buffer> dynamic;
|
2018-01-14 06:16:49 +01:00
|
|
|
};
|
|
|
|
|
2018-01-13 03:57:58 +01:00
|
|
|
/// This is a handle for being a client to another server. This handle will
|
|
|
|
/// attempt to find an existing connection pool for the remote server otherwise
|
2018-01-14 06:16:49 +01:00
|
|
|
/// one will be created. Then it will multiplex your request and demultiplex
|
|
|
|
/// your response with all the other requests pending in the pipelines to
|
|
|
|
/// the remote.
|
2018-01-13 03:57:58 +01:00
|
|
|
///
|
|
|
|
struct ircd::server::request
|
|
|
|
:ctx::future<http::code>
|
|
|
|
{
|
2018-01-17 10:31:34 +01:00
|
|
|
struct opts;
|
|
|
|
|
|
|
|
static const opts opts_default;
|
|
|
|
|
2018-01-15 09:11:32 +01:00
|
|
|
server::tag *tag {nullptr};
|
2017-11-25 22:17:22 +01:00
|
|
|
|
2018-01-13 03:57:58 +01:00
|
|
|
public:
|
2018-01-17 10:31:34 +01:00
|
|
|
/// Transmission data
|
2018-01-13 03:57:58 +01:00
|
|
|
server::out out;
|
2018-01-17 10:31:34 +01:00
|
|
|
|
|
|
|
/// Reception data
|
2018-01-13 03:57:58 +01:00
|
|
|
server::in in;
|
|
|
|
|
2018-01-17 10:31:34 +01:00
|
|
|
/// Options
|
2018-03-07 16:21:00 +01:00
|
|
|
const opts *opt { &opts_default };
|
2018-01-17 10:31:34 +01:00
|
|
|
|
2018-01-22 11:41:14 +01:00
|
|
|
request(const net::hostport &,
|
|
|
|
server::out,
|
|
|
|
server::in,
|
2018-03-07 16:21:00 +01:00
|
|
|
const opts *const & = nullptr);
|
2018-01-22 11:41:14 +01:00
|
|
|
|
2018-01-13 03:57:58 +01:00
|
|
|
request() = default;
|
|
|
|
request(request &&) noexcept;
|
|
|
|
request(const request &) = delete;
|
|
|
|
request &operator=(request &&) noexcept;
|
|
|
|
request &operator=(const request &) = delete;
|
|
|
|
~request() noexcept;
|
2017-10-25 18:39:58 +02:00
|
|
|
};
|
2018-01-16 03:04:23 +01:00
|
|
|
|
2018-01-17 10:31:34 +01:00
|
|
|
struct ircd::server::request::opts
|
|
|
|
{
|
|
|
|
/// When true, HTTP responses above the 200's are thrown as exceptions
|
|
|
|
/// from the future::get() on this object. Otherwise, if false any code
|
|
|
|
/// received is returned in the value and exceptions are thrown when no
|
|
|
|
/// code can be returned.
|
|
|
|
bool http_exceptions {true};
|
2018-02-27 06:56:05 +01:00
|
|
|
|
|
|
|
/// Only applies when using the dynamic content allocation feature; this
|
|
|
|
/// limits the size of that allocation in case the remote sends a larger
|
|
|
|
/// content-length value. If the remote sends more content, the behavior
|
|
|
|
/// is the same as if specifying an in.content buffer of this size.
|
|
|
|
size_t content_length_maxalloc {256_MiB};
|
2018-01-17 10:31:34 +01:00
|
|
|
};
|
|
|
|
|
2018-01-16 03:04:23 +01:00
|
|
|
inline
|
|
|
|
ircd::server::request::request(const net::hostport &hostport,
|
|
|
|
server::out out,
|
2018-01-22 11:41:14 +01:00
|
|
|
server::in in,
|
2018-03-07 16:21:00 +01:00
|
|
|
const opts *const &opt)
|
2018-01-16 03:04:23 +01:00
|
|
|
:tag{nullptr}
|
|
|
|
,out{std::move(out)}
|
|
|
|
,in{std::move(in)}
|
2018-03-07 16:21:00 +01:00
|
|
|
,opt{opt?: &opts_default}
|
2018-01-16 03:04:23 +01:00
|
|
|
{
|
|
|
|
submit(hostport, *this);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline
|
|
|
|
ircd::server::request::request(request &&o)
|
|
|
|
noexcept
|
|
|
|
:ctx::future<http::code>{std::move(o)}
|
|
|
|
,tag{std::move(o.tag)}
|
|
|
|
,out{std::move(o.out)}
|
|
|
|
,in{std::move(o.in)}
|
2018-03-07 16:21:00 +01:00
|
|
|
,opt{std::move(o.opt)}
|
2018-01-16 03:04:23 +01:00
|
|
|
{
|
|
|
|
if(tag)
|
|
|
|
associate(*this, *tag, std::move(o));
|
2018-01-17 12:14:14 +01:00
|
|
|
|
|
|
|
assert(!o.tag);
|
2018-01-16 03:04:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
inline ircd::server::request &
|
|
|
|
ircd::server::request::operator=(request &&o)
|
|
|
|
noexcept
|
|
|
|
{
|
2018-01-17 06:23:15 +01:00
|
|
|
this->~request();
|
|
|
|
|
2018-01-16 03:04:23 +01:00
|
|
|
ctx::future<http::code>::operator=(std::move(o));
|
|
|
|
out = std::move(o.out);
|
|
|
|
in = std::move(o.in);
|
|
|
|
tag = std::move(o.tag);
|
2018-03-07 16:21:00 +01:00
|
|
|
opt = std::move(o.opt);
|
2018-01-16 03:04:23 +01:00
|
|
|
|
|
|
|
if(tag)
|
|
|
|
associate(*this, *tag, std::move(o));
|
|
|
|
|
2018-01-17 12:14:14 +01:00
|
|
|
assert(!o.tag);
|
2018-01-16 03:04:23 +01:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline
|
|
|
|
ircd::server::request::~request()
|
|
|
|
noexcept
|
|
|
|
{
|
2018-02-27 11:41:48 +01:00
|
|
|
if(tag)
|
2018-01-17 06:23:15 +01:00
|
|
|
cancel(*this);
|
|
|
|
|
2018-01-16 03:04:23 +01:00
|
|
|
if(tag)
|
|
|
|
disassociate(*this, *tag);
|
2018-01-17 12:14:14 +01:00
|
|
|
|
|
|
|
assert(!tag);
|
2018-01-16 03:04:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
inline size_t
|
|
|
|
ircd::server::size(const in &in)
|
|
|
|
{
|
2018-01-16 08:41:24 +01:00
|
|
|
return size(in.head) + size(in.content);
|
2018-01-16 03:04:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
inline size_t
|
|
|
|
ircd::server::size(const out &out)
|
|
|
|
{
|
|
|
|
return size(out.head) + size(out.content);
|
|
|
|
}
|