2018-01-13 03:57:58 +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.
|
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-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-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
|
|
|
|
const struct opts *opts { &opts_default };
|
|
|
|
|
2018-01-17 14:58:44 +01:00
|
|
|
request(const net::hostport &, server::out, server::in);
|
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-01-16 03:04:23 +01:00
|
|
|
inline
|
|
|
|
ircd::server::request::request(const net::hostport &hostport,
|
|
|
|
server::out out,
|
|
|
|
server::in in)
|
|
|
|
:tag{nullptr}
|
|
|
|
,out{std::move(out)}
|
|
|
|
,in{std::move(in)}
|
|
|
|
{
|
|
|
|
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-01-17 10:31:34 +01:00
|
|
|
,opts{std::move(o.opts)}
|
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-01-17 10:31:34 +01:00
|
|
|
opts = std::move(o.opts);
|
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-01-17 06:23:15 +01:00
|
|
|
if(tag && !tag->committed())
|
|
|
|
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);
|
|
|
|
}
|