13 #ifndef __PROCESS_HTTP_HPP__ 14 #define __PROCESS_HTTP_HPP__ 20 #include <initializer_list> 27 #include <boost/functional/hash.hpp> 65 namespace authentication {
79 const std::string& realm,
96 namespace authorization {
102 lambda::function<process::Future<bool>(
134 URL(
const std::string& _scheme,
135 const std::string& _domain,
136 const std::string& _path,
146 URL(
const std::string& _scheme,
147 const std::string& _domain,
148 const uint16_t _port = 80,
149 const std::string& _path =
"/",
160 URL(
const std::string& _scheme,
162 const uint16_t _port = 80,
163 const std::string& _path =
"/",
180 bool isAbsolute()
const;
194 std::ostream&
operator<<(std::ostream& stream,
const URL& url);
202 foreach (
char c, key) {
203 boost::hash_combine(seed, ::tolower(c));
212 bool operator()(
const std::string& left,
const std::string& right)
const 214 if (left.size() != right.size()) {
217 for (
size_t i = 0; i < left.size(); ++i) {
218 if (::tolower(left[i]) != ::tolower(right[i])) {
231 static const uint16_t
OK;
270 static std::string string(uint16_t code);
348 explicit Reader(
const std::shared_ptr<Data>& _data)
351 std::shared_ptr<Data> data;
360 bool write(std::string s);
370 bool fail(
const std::string& message);
390 explicit Writer(
const std::shared_ptr<Data>& _data)
393 std::shared_ptr<Data> data;
397 : data(new Data()) {}
409 : readEnd(Reader::OPEN), writeEnd(Writer::OPEN) {}
413 std::atomic_flag lock = ATOMIC_FLAG_INIT;
415 Reader::State readEnd;
416 Writer::State writeEnd;
419 std::queue<Owned<Promise<std::string>>> reads;
423 std::queue<std::string> writes;
432 std::shared_ptr<Data> data;
442 static constexpr
const char*
NAME =
"WWW-Authenticate";
445 const std::string& authScheme,
447 : authScheme_(authScheme),
448 authParam_(authParam) {}
452 std::string authScheme();
464 std::string authScheme_;
475 CaseInsensitiveEqual>
494 Headers(std::initializer_list<std::pair<std::string, std::string>>
list)
501 template <
typename T>
527 result.insert(that.begin(), that.end());
536 : keepAlive(false),
type(BODY), received(
Clock::now()) {}
587 bool acceptsEncoding(
const std::string& encoding)
const;
593 bool acceptsMediaType(
const std::string& mediaType)
const;
601 bool acceptsMediaType(
602 const std::string&
name,
603 const std::string& mediaType)
const;
606 bool _acceptsMediaType(
608 const std::string& mediaType)
const;
618 :
type(NONE), code(_code)
626 const std::string& contentType =
"text/plain; charset=utf-8")
628 body(
std::move(_body)),
631 headers[
"Content-Length"] =
stringify(body.size());
632 headers[
"Content-Type"] = contentType;
683 explicit OK(
const char* body)
686 explicit OK(std::string body)
689 explicit OK(std::string body,
const std::string& contentType)
713 headers[
"Location"] = url;
734 const std::vector<std::string>& challenges,
742 headers[
"WWW-Authenticate"] =
strings::join(
", ", challenges);
773 const std::initializer_list<std::string>& allowedMethods,
776 constructBody(allowedMethods, requestMethod),
777 Status::METHOD_NOT_ALLOWED)
783 static std::string constructBody(
784 const std::initializer_list<std::string>& allowedMethods,
788 "405 Method Not Allowed. Expecting one of { '" +
791 ?
", but received '" + requestMethod.
get() +
"'" 902 const std::string& pattern,
903 const std::string&
path);
918 const std::string& s,
919 const std::string& additional_chars =
"");
1010 std::shared_ptr<Data> data;
1054 template <
typename F>
1064 class ServerProcess;
1119 template <
typename F>
1129 const Request&)>(std::forward<F>(
f)),
1140 template <
typename F>
1150 const Request&)>(std::forward<F>(
f)),
1187 const std::string& method,
1188 bool enableSSL =
false,
1197 const std::string& method,
1212 bool streamedResponse =
false);
1295 namespace streaming {
1343 #endif // __PROCESS_HTTP_HPP__ static StopOptions DEFAULT_STOP_OPTIONS()
Definition: http.hpp:1105
Try< hashmap< std::string, std::string > > parse(const std::string &pattern, const std::string &path)
bool operator!=(const Writer &other) const
Definition: http.hpp:379
size_t operator()(const std::string &key) const
Definition: http.hpp:199
Future< Nothing > serve(network::Socket s, std::function< Future< Response >(const Request &)> &&f)
Forbidden()
Definition: http.hpp:749
bool operator!=(const Connection &c) const
Definition: http.hpp:990
static const uint16_t GATEWAY_TIMEOUT
Definition: http.hpp:267
static const uint16_t MOVED_PERMANENTLY
Definition: http.hpp:239
NotImplemented(std::string body)
Definition: http.hpp:853
static const uint16_t GONE
Definition: http.hpp:255
static const uint16_t UNSUPPORTED_MEDIA_TYPE
Definition: http.hpp:260
Definition: errorbase.hpp:36
Definition: http.hpp:1076
Definition: option.hpp:29
static const uint16_t METHOD_NOT_ALLOWED
Definition: http.hpp:250
Future< Response > request(const Request &request, bool streamedResponse=false)
Asynchronously sends an HTTP request to the process and returns the HTTP response once the entire res...
F && f
Definition: defer.hpp:270
static const uint16_t BAD_REQUEST
Definition: http.hpp:245
Conflict(std::string body)
Definition: http.hpp:813
static const uint16_t NO_CONTENT
Definition: http.hpp:235
T & get()&
Definition: try.hpp:80
URL url
Definition: http.hpp:544
static const uint16_t REQUEST_TIMEOUT
Definition: http.hpp:253
static const uint16_t LENGTH_REQUIRED
Definition: http.hpp:256
std::stringstream & join(std::stringstream &stream, const std::string &separator, T &&...args)
Definition: strings.hpp:307
static CreateOptions DEFAULT_CREATE_OPTIONS()
Definition: http.hpp:1082
std::string status
Definition: http.hpp:637
InternalServerError(std::string body)
Definition: http.hpp:843
NotAcceptable()
Definition: http.hpp:800
ServiceUnavailable(std::string body)
Definition: http.hpp:863
static const uint16_t PARTIAL_CONTENT
Definition: http.hpp:237
static const uint16_t EXPECTATION_FAILED
Definition: http.hpp:262
static const uint16_t CONFLICT
Definition: http.hpp:254
Provides timers.
Definition: clock.hpp:37
Option< Pipe::Reader > reader
Definition: http.hpp:579
Unauthorized(const std::vector< std::string > &challenges)
Definition: http.hpp:730
uint16_t code
Definition: http.hpp:674
Unauthorized(const std::vector< std::string > &challenges, std::string body)
Definition: http.hpp:733
OK()
Definition: http.hpp:680
Future< Nothing > serve(const network::Socket &s, F &&f)
Definition: http.hpp:1055
static const uint16_t NON_AUTHORITATIVE_INFORMATION
Definition: http.hpp:234
Result< ProcessStatus > status(pid_t pid)
Definition: proc.hpp:166
Option< std::string > domain
Definition: http.hpp:185
InternalServerError()
Definition: http.hpp:840
Definition: http.hpp:1067
static const uint16_t FOUND
Definition: http.hpp:240
OK(std::string body, const std::string &contentType)
Definition: http.hpp:689
bool operator==(const Writer &other) const
Definition: http.hpp:378
Definition: type_utils.hpp:619
Definition: address.hpp:324
Future< Response > get(const URL &url, const Option< Headers > &headers=None())
Option< network::Address > client
Definition: http.hpp:562
const std::string NAME
Definition: logrotate.hpp:38
static const uint16_t MULTIPLE_CHOICES
Definition: http.hpp:238
static const uint16_t REQUEST_ENTITY_TOO_LARGE
Definition: http.hpp:258
Future< Response > requestDelete(const URL &url, const Option< Headers > &headers=None())
Asynchronously sends an HTTP DELETE request to the process with the given UPID and returns the HTTP r...
std::string encode(const std::string &s, const std::string &additional_chars="")
Returns a percent-encoded string according to RFC 3986.
Option< net::IP > ip
Definition: http.hpp:186
URL(const std::string &_scheme, const std::string &_domain, const std::string &_path, const hashmap< std::string, std::string > &_query=(hashmap< std::string, std::string >()), const Option< std::string > &_fragment=None())
Definition: http.hpp:134
Definition: duration.hpp:32
Try< std::string > decode(const std::string &s)
Decode a string that is Base64-encoded with the standard Base64 alphabet.
Definition: base64.hpp:183
BadRequest()
Definition: http.hpp:720
PreconditionFailed(std::string body)
Definition: http.hpp:823
static const uint16_t RESET_CONTENT
Definition: http.hpp:236
bool isSome() const
Definition: option.hpp:116
OK(std::string body)
Definition: http.hpp:686
PreconditionFailed()
Definition: http.hpp:820
Scheme
Definition: http.hpp:56
bool operator==(const Connection &c) const
Definition: http.hpp:989
ServiceUnavailable()
Definition: http.hpp:860
hashmap< std::string, std::string > query
Definition: http.hpp:189
Definition: hashmap.hpp:38
Duration grace_period
Definition: http.hpp:1102
Accepted()
Definition: http.hpp:700
Response(std::string _body, uint16_t _code, const std::string &contentType="text/plain; charset=utf-8")
Definition: http.hpp:623
Option< Pipe::Reader > reader
Definition: http.hpp:672
std::string path
Definition: http.hpp:188
static const uint16_t PRECONDITION_FAILED
Definition: http.hpp:257
std::string body
Definition: http.hpp:670
An "untyped" PID, used to encapsulate the process ID for lower-layer abstractions (eg...
Definition: pid.hpp:39
static const uint16_t NOT_ACCEPTABLE
Definition: http.hpp:251
Option< std::string > scheme
Definition: http.hpp:182
Future< Nothing > setAuthenticator(const std::string &realm, Owned< Authenticator > authenticator)
Sets (or overwrites) the authenticator for the realm.
NotImplemented()
Definition: http.hpp:850
OK(const char *body)
Definition: http.hpp:683
static const uint16_t SERVICE_UNAVAILABLE
Definition: http.hpp:266
Try<::mesos::Value::Ranges > fragment(const ::mesos::Value::Range &bounds, size_t numRanges)
Definition: jsonify.hpp:86
Definition: duration.hpp:207
static const uint16_t PAYMENT_REQUIRED
Definition: http.hpp:247
URL(const std::string &_scheme, const net::IP &_ip, const uint16_t _port=80, const std::string &_path="/", const hashmap< std::string, std::string > &_query=(hashmap< std::string, std::string >()), const Option< std::string > &_fragment=None())
Definition: http.hpp:160
static const uint16_t USE_PROXY
Definition: http.hpp:243
Option< uint16_t > port
Definition: http.hpp:187
NotAcceptable(std::string body)
Definition: http.hpp:803
static const uint16_t ACCEPTED
Definition: http.hpp:233
NotFound()
Definition: http.hpp:759
const T & get() const &
Definition: option.hpp:119
Future< Response > post(const URL &url, const Option< Headers > &headers=None(), const Option< std::string > &body=None(), const Option< std::string > &contentType=None())
Request createRequest(const UPID &upid, const std::string &method, bool enableSSL=false, const Option< std::string > &path=None(), const Option< Headers > &headers=None(), const Option< std::string > &body=None(), const Option< std::string > &contentType=None())
bool keepAlive
Definition: http.hpp:558
bool operator==(const Pipe &other) const
Definition: http.hpp:403
static const uint16_t SEE_OTHER
Definition: http.hpp:241
bool operator!=(const Reader &other) const
Definition: http.hpp:337
static const uint16_t OK
Definition: http.hpp:231
URL(const std::string &_scheme, const std::string &_domain, const uint16_t _port=80, const std::string &_path="/", const hashmap< std::string, std::string > &_query=(hashmap< std::string, std::string >()), const Option< std::string > &_fragment=None())
Definition: http.hpp:146
static Try error(const E &e)
Definition: try.hpp:43
Scheme scheme
Definition: http.hpp:1078
Future< R > run(R(*method)())
Definition: run.hpp:55
Conflict()
Definition: http.hpp:810
Option< std::string > fragment
Definition: http.hpp:190
static const uint16_t CONTINUE
Definition: http.hpp:229
Try< std::vector< Entry > > list(const std::string &hierarchy, const std::string &cgroup)
static const uint16_t PROXY_AUTHENTICATION_REQUIRED
Definition: http.hpp:252
Future< Connection > connect(const network::Address &address, Scheme scheme, const Option< std::string > &peer_hostname)
Iterable< V > map(F &&f, const Iterable< U, Us... > &input)
Definition: lambda.hpp:46
Result< Process > process(pid_t pid)
Definition: freebsd.hpp:30
bool operator!=(const Pipe &other) const
Definition: http.hpp:404
NotFound(std::string body)
Definition: http.hpp:762
Definition: attributes.hpp:24
bool isError() const
Definition: try.hpp:78
Result< Credentials > read(const Path &path)
Definition: credentials.hpp:35
std::string path
Definition: http.hpp:671
TemporaryRedirect(const std::string &url)
Definition: http.hpp:710
std::string method
Definition: http.hpp:538
Definition: executor.hpp:48
std::ostream & operator<<(std::ostream &stream, const URL &url)
Pipe()
Definition: http.hpp:396
std::string body
Definition: http.hpp:578
static const uint16_t SWITCHING_PROTOCOLS
Definition: http.hpp:230
static const uint16_t BAD_GATEWAY
Definition: http.hpp:265
Try< std::vector< Response > > decodeResponses(const std::string &s)
Decode HTTP responses from the given string.
static const uint16_t INTERNAL_SERVER_ERROR
Definition: http.hpp:263
Protocol< WriteRequest, WriteResponse > write
MethodNotAllowed(const std::initializer_list< std::string > &allowedMethods, const Option< std::string > &requestMethod=None())
Definition: http.hpp:772
Try< uint32_t > type(const std::string &path)
network::inet::Address address()
Returns the socket address associated with this instance of the library.
static const uint16_t NOT_MODIFIED
Definition: http.hpp:242
static Try< Server > create(network::Socket socket, F &&f, const CreateOptions &options=DEFAULT_CREATE_OPTIONS())
Definition: http.hpp:1120
bool isNone() const
Definition: option.hpp:117
const network::Address peerAddress
Definition: http.hpp:993
static const uint16_t REQUESTED_RANGE_NOT_SATISFIABLE
Definition: http.hpp:261
void setCallbacks(const AuthorizationCallbacks &)
static const uint16_t CREATED
Definition: http.hpp:232
Try< Nothing > create(const std::string &hierarchy, const std::string &cgroup, bool recursive=false)
Accepted(std::string body)
Definition: http.hpp:703
Response()
Definition: http.hpp:614
static const uint16_t UNAUTHORIZED
Definition: http.hpp:246
static const uint16_t NOT_FOUND
Definition: http.hpp:249
URI http(const std::string &host, const std::string &path="/", const Option< int > &port=None(), const Option< std::string > &query=None(), const Option< std::string > &fragment=None(), const Option< std::string > &user=None(), const Option< std::string > &password=None())
Creates an http URI with the given parameters.
Definition: http.hpp:35
bool operator==(const Reader &other) const
Definition: http.hpp:336
Try< Netlink< struct nl_sock > > socket(int protocol=NETLINK_ROUTE)
Definition: internal.hpp:91
std::string stringify(int flags)
static const uint16_t NOT_IMPLEMENTED
Definition: http.hpp:264
Time received
Definition: http.hpp:581
static std::string string(uint16_t code)
BadRequest(std::string body)
Definition: http.hpp:723
Headers headers
Definition: http.hpp:639
Response(uint16_t _code)
Definition: http.hpp:617
const network::Address localAddress
Definition: http.hpp:992
size_t backlog
Definition: http.hpp:1079
static const uint16_t REQUEST_URI_TOO_LARGE
Definition: http.hpp:259
Forbidden(std::string body)
Definition: http.hpp:752
Definition: http.hpp:1093
bool isValidStatus(uint16_t code)
Represents a connection to an HTTP server.
Definition: http.hpp:965
Future< Nothing > unsetAuthenticator(const std::string &realm)
Unsets the authenticator for the realm.
bool operator()(const std::string &left, const std::string &right) const
Definition: http.hpp:212
constexpr const char * name
Definition: shell.hpp:41
static const uint16_t HTTP_VERSION_NOT_SUPPORTED
Definition: http.hpp:268
static Try< Server > create(const network::Address &address, F &&f, const CreateOptions &options=DEFAULT_CREATE_OPTIONS())
Definition: http.hpp:1141
static const uint16_t TEMPORARY_REDIRECT
Definition: http.hpp:244
static const uint16_t FORBIDDEN
Definition: http.hpp:248
Headers headers
Definition: http.hpp:546
Future< size_t > send(const int_fd &fd, const void *buf, size_t size)
Request()
Definition: http.hpp:535