cannam@147: // Copyright (c) 2017 Sandstorm Development Group, Inc. and contributors cannam@147: // Licensed under the MIT License: cannam@147: // cannam@147: // Permission is hereby granted, free of charge, to any person obtaining a copy cannam@147: // of this software and associated documentation files (the "Software"), to deal cannam@147: // in the Software without restriction, including without limitation the rights cannam@147: // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell cannam@147: // copies of the Software, and to permit persons to whom the Software is cannam@147: // furnished to do so, subject to the following conditions: cannam@147: // cannam@147: // The above copyright notice and this permission notice shall be included in cannam@147: // all copies or substantial portions of the Software. cannam@147: // cannam@147: // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR cannam@147: // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, cannam@147: // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE cannam@147: // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER cannam@147: // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, cannam@147: // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN cannam@147: // THE SOFTWARE. cannam@147: cannam@147: #ifndef KJ_COMPAT_HTTP_H_ cannam@147: #define KJ_COMPAT_HTTP_H_ cannam@147: // The KJ HTTP client/server library. cannam@147: // cannam@147: // This is a simple library which can be used to implement an HTTP client or server. Properties cannam@147: // of this library include: cannam@147: // - Uses KJ async framework. cannam@147: // - Agnostic to transport layer -- you can provide your own. cannam@147: // - Header parsing is zero-copy -- it results in strings that point directly into the buffer cannam@147: // received off the wire. cannam@147: // - Application code which reads and writes headers refers to headers by symbolic names, not by cannam@147: // string literals, with lookups being array-index-based, not map-based. To make this possible, cannam@147: // the application announces what headers it cares about in advance, in order to assign numeric cannam@147: // values to them. cannam@147: // - Methods are identified by an enum. cannam@147: cannam@147: #include cannam@147: #include cannam@147: #include cannam@147: #include cannam@147: #include cannam@147: cannam@147: namespace kj { cannam@147: cannam@147: #define KJ_HTTP_FOR_EACH_METHOD(MACRO) \ cannam@147: MACRO(GET) \ cannam@147: MACRO(HEAD) \ cannam@147: MACRO(POST) \ cannam@147: MACRO(PUT) \ cannam@147: MACRO(DELETE) \ cannam@147: MACRO(PATCH) \ cannam@147: MACRO(PURGE) \ cannam@147: MACRO(OPTIONS) \ cannam@147: MACRO(TRACE) \ cannam@147: /* standard methods */ \ cannam@147: /* */ \ cannam@147: /* (CONNECT is intentionally omitted since it is handled specially in HttpHandler) */ \ cannam@147: \ cannam@147: MACRO(COPY) \ cannam@147: MACRO(LOCK) \ cannam@147: MACRO(MKCOL) \ cannam@147: MACRO(MOVE) \ cannam@147: MACRO(PROPFIND) \ cannam@147: MACRO(PROPPATCH) \ cannam@147: MACRO(SEARCH) \ cannam@147: MACRO(UNLOCK) \ cannam@147: /* WebDAV */ \ cannam@147: \ cannam@147: MACRO(REPORT) \ cannam@147: MACRO(MKACTIVITY) \ cannam@147: MACRO(CHECKOUT) \ cannam@147: MACRO(MERGE) \ cannam@147: /* Subversion */ \ cannam@147: \ cannam@147: MACRO(MSEARCH) \ cannam@147: MACRO(NOTIFY) \ cannam@147: MACRO(SUBSCRIBE) \ cannam@147: MACRO(UNSUBSCRIBE) cannam@147: /* UPnP */ cannam@147: cannam@147: #define KJ_HTTP_FOR_EACH_CONNECTION_HEADER(MACRO) \ cannam@147: MACRO(connection, "Connection") \ cannam@147: MACRO(contentLength, "Content-Length") \ cannam@147: MACRO(keepAlive, "Keep-Alive") \ cannam@147: MACRO(te, "TE") \ cannam@147: MACRO(trailer, "Trailer") \ cannam@147: MACRO(transferEncoding, "Transfer-Encoding") \ cannam@147: MACRO(upgrade, "Upgrade") cannam@147: cannam@147: enum class HttpMethod { cannam@147: // Enum of known HTTP methods. cannam@147: // cannam@147: // We use an enum rather than a string to allow for faster parsing and switching and to reduce cannam@147: // ambiguity. cannam@147: cannam@147: #define DECLARE_METHOD(id) id, cannam@147: KJ_HTTP_FOR_EACH_METHOD(DECLARE_METHOD) cannam@147: #undef DECALRE_METHOD cannam@147: }; cannam@147: cannam@147: kj::StringPtr KJ_STRINGIFY(HttpMethod method); cannam@147: kj::Maybe tryParseHttpMethod(kj::StringPtr name); cannam@147: cannam@147: class HttpHeaderTable; cannam@147: cannam@147: class HttpHeaderId { cannam@147: // Identifies an HTTP header by numeric ID that indexes into an HttpHeaderTable. cannam@147: // cannam@147: // The KJ HTTP API prefers that headers be identified by these IDs for a few reasons: cannam@147: // - Integer lookups are much more efficient than string lookups. cannam@147: // - Case-insensitivity is awkward to deal with when const strings are being passed to the lookup cannam@147: // method. cannam@147: // - Writing out strings less often means fewer typos. cannam@147: // cannam@147: // See HttpHeaderTable for usage hints. cannam@147: cannam@147: public: cannam@147: HttpHeaderId() = default; cannam@147: cannam@147: inline bool operator==(const HttpHeaderId& other) const { return id == other.id; } cannam@147: inline bool operator!=(const HttpHeaderId& other) const { return id != other.id; } cannam@147: inline bool operator< (const HttpHeaderId& other) const { return id < other.id; } cannam@147: inline bool operator> (const HttpHeaderId& other) const { return id > other.id; } cannam@147: inline bool operator<=(const HttpHeaderId& other) const { return id <= other.id; } cannam@147: inline bool operator>=(const HttpHeaderId& other) const { return id >= other.id; } cannam@147: cannam@147: inline size_t hashCode() const { return id; } cannam@147: cannam@147: kj::StringPtr toString() const; cannam@147: cannam@147: void requireFrom(HttpHeaderTable& table) const; cannam@147: // In debug mode, throws an exception if the HttpHeaderId is not from the given table. cannam@147: // cannam@147: // In opt mode, no-op. cannam@147: cannam@147: #define KJ_HTTP_FOR_EACH_BUILTIN_HEADER(MACRO) \ cannam@147: MACRO(HOST, "Host") \ cannam@147: MACRO(DATE, "Date") \ cannam@147: MACRO(LOCATION, "Location") \ cannam@147: MACRO(CONTENT_TYPE, "Content-Type") cannam@147: // For convenience, these very-common headers are valid for all HttpHeaderTables. You can refer cannam@147: // to them like: cannam@147: // cannam@147: // HttpHeaderId::HOST cannam@147: // cannam@147: // TODO(0.7): Fill this out with more common headers. cannam@147: cannam@147: #define DECLARE_HEADER(id, name) \ cannam@147: static const HttpHeaderId id; cannam@147: // Declare a constant for each builtin header, e.g.: HttpHeaderId::CONNECTION cannam@147: cannam@147: KJ_HTTP_FOR_EACH_BUILTIN_HEADER(DECLARE_HEADER); cannam@147: #undef DECLARE_HEADER cannam@147: cannam@147: private: cannam@147: HttpHeaderTable* table; cannam@147: uint id; cannam@147: cannam@147: inline explicit constexpr HttpHeaderId(HttpHeaderTable* table, uint id): table(table), id(id) {} cannam@147: friend class HttpHeaderTable; cannam@147: friend class HttpHeaders; cannam@147: }; cannam@147: cannam@147: class HttpHeaderTable { cannam@147: // Construct an HttpHeaderTable to declare which headers you'll be interested in later on, and cannam@147: // to manufacture IDs for them. cannam@147: // cannam@147: // Example: cannam@147: // cannam@147: // // Build a header table with the headers we are interested in. cannam@147: // kj::HttpHeaderTable::Builder builder; cannam@147: // const HttpHeaderId accept = builder.add("Accept"); cannam@147: // const HttpHeaderId contentType = builder.add("Content-Type"); cannam@147: // kj::HttpHeaderTable table(kj::mv(builder)); cannam@147: // cannam@147: // // Create an HTTP client. cannam@147: // auto client = kj::newHttpClient(table, network); cannam@147: // cannam@147: // // Get http://example.com. cannam@147: // HttpHeaders headers(table); cannam@147: // headers.set(accept, "text/html"); cannam@147: // auto response = client->send(kj::HttpMethod::GET, "http://example.com", headers) cannam@147: // .wait(waitScope); cannam@147: // auto msg = kj::str("Response content type: ", response.headers.get(contentType)); cannam@147: cannam@147: struct IdsByNameMap; cannam@147: cannam@147: public: cannam@147: HttpHeaderTable(); cannam@147: // Constructs a table that only contains the builtin headers. cannam@147: cannam@147: class Builder { cannam@147: public: cannam@147: Builder(); cannam@147: HttpHeaderId add(kj::StringPtr name); cannam@147: Own build(); cannam@147: cannam@147: HttpHeaderTable& getFutureTable(); cannam@147: // Get the still-unbuilt header table. You cannot actually use it until build() has been cannam@147: // called. cannam@147: // cannam@147: // This method exists to help when building a shared header table -- the Builder may be passed cannam@147: // to several components, each of which will register the headers they need and get a reference cannam@147: // to the future table. cannam@147: cannam@147: private: cannam@147: kj::Own table; cannam@147: }; cannam@147: cannam@147: KJ_DISALLOW_COPY(HttpHeaderTable); // Can't copy because HttpHeaderId points to the table. cannam@147: ~HttpHeaderTable() noexcept(false); cannam@147: cannam@147: uint idCount(); cannam@147: // Return the number of IDs in the table. cannam@147: cannam@147: kj::Maybe stringToId(kj::StringPtr name); cannam@147: // Try to find an ID for the given name. The matching is case-insensitive, per the HTTP spec. cannam@147: // cannam@147: // Note: if `name` contains characters that aren't allowed in HTTP header names, this may return cannam@147: // a bogus value rather than null, due to optimizations used in case-insensitive matching. cannam@147: cannam@147: kj::StringPtr idToString(HttpHeaderId id); cannam@147: // Get the canonical string name for the given ID. cannam@147: cannam@147: private: cannam@147: kj::Vector namesById; cannam@147: kj::Own idsByName; cannam@147: }; cannam@147: cannam@147: class HttpHeaders { cannam@147: // Represents a set of HTTP headers. cannam@147: // cannam@147: // This class guards against basic HTTP header injection attacks: Trying to set a header name or cannam@147: // value containing a newline, carriage return, or other invalid character will throw an cannam@147: // exception. cannam@147: cannam@147: public: cannam@147: explicit HttpHeaders(HttpHeaderTable& table); cannam@147: cannam@147: KJ_DISALLOW_COPY(HttpHeaders); cannam@147: HttpHeaders(HttpHeaders&&) = default; cannam@147: HttpHeaders& operator=(HttpHeaders&&) = default; cannam@147: cannam@147: void clear(); cannam@147: // Clears all contents, as if the object was freshly-allocated. However, calling this rather cannam@147: // than actually re-allocating the object may avoid re-allocation of internal objects. cannam@147: cannam@147: HttpHeaders clone() const; cannam@147: // Creates a deep clone of the HttpHeaders. The returned object owns all strings it references. cannam@147: cannam@147: HttpHeaders cloneShallow() const; cannam@147: // Creates a shallow clone of the HttpHeaders. The returned object references the same strings cannam@147: // as the original, owning none of them. cannam@147: cannam@147: kj::Maybe get(HttpHeaderId id) const; cannam@147: // Read a header. cannam@147: cannam@147: template cannam@147: void forEach(Func&& func) const; cannam@147: // Calls `func(name, value)` for each header in the set -- including headers that aren't mapped cannam@147: // to IDs in the header table. Both inputs are of type kj::StringPtr. cannam@147: cannam@147: void set(HttpHeaderId id, kj::StringPtr value); cannam@147: void set(HttpHeaderId id, kj::String&& value); cannam@147: // Sets a header value, overwriting the existing value. cannam@147: // cannam@147: // The String&& version is equivalent to calling the other version followed by takeOwnership(). cannam@147: // cannam@147: // WARNING: It is the caller's responsibility to ensure that `value` remains valid until the cannam@147: // HttpHeaders object is destroyed. This allows string literals to be passed without making a cannam@147: // copy, but complicates the use of dynamic values. Hint: Consider using `takeOwnership()`. cannam@147: cannam@147: void add(kj::StringPtr name, kj::StringPtr value); cannam@147: void add(kj::StringPtr name, kj::String&& value); cannam@147: void add(kj::String&& name, kj::String&& value); cannam@147: // Append a header. `name` will be looked up in the header table, but if it's not mapped, the cannam@147: // header will be added to the list of unmapped headers. cannam@147: // cannam@147: // The String&& versions are equivalent to calling the other version followed by takeOwnership(). cannam@147: // cannam@147: // WARNING: It is the caller's responsibility to ensure that `name` and `value` remain valid cannam@147: // until the HttpHeaders object is destroyed. This allows string literals to be passed without cannam@147: // making a copy, but complicates the use of dynamic values. Hint: Consider using cannam@147: // `takeOwnership()`. cannam@147: cannam@147: void unset(HttpHeaderId id); cannam@147: // Removes a header. cannam@147: // cannam@147: // It's not possible to remove a header by string name because non-indexed headers would take cannam@147: // O(n) time to remove. Instead, construct a new HttpHeaders object and copy contents. cannam@147: cannam@147: void takeOwnership(kj::String&& string); cannam@147: void takeOwnership(kj::Array&& chars); cannam@147: void takeOwnership(HttpHeaders&& otherHeaders); cannam@147: // Takes overship of a string so that it lives until the HttpHeaders object is destroyed. Useful cannam@147: // when you've passed a dynamic value to set() or add() or parse*(). cannam@147: cannam@147: struct ConnectionHeaders { cannam@147: // These headers govern details of the specific HTTP connection or framing of the content. cannam@147: // Hence, they are managed internally within the HTTP library, and never appear in an cannam@147: // HttpHeaders structure. cannam@147: cannam@147: #define DECLARE_HEADER(id, name) \ cannam@147: kj::StringPtr id; cannam@147: KJ_HTTP_FOR_EACH_CONNECTION_HEADER(DECLARE_HEADER) cannam@147: #undef DECLARE_HEADER cannam@147: }; cannam@147: cannam@147: struct Request { cannam@147: HttpMethod method; cannam@147: kj::StringPtr url; cannam@147: ConnectionHeaders connectionHeaders; cannam@147: }; cannam@147: struct Response { cannam@147: uint statusCode; cannam@147: kj::StringPtr statusText; cannam@147: ConnectionHeaders connectionHeaders; cannam@147: }; cannam@147: cannam@147: kj::Maybe tryParseRequest(kj::ArrayPtr content); cannam@147: kj::Maybe tryParseResponse(kj::ArrayPtr content); cannam@147: // Parse an HTTP header blob and add all the headers to this object. cannam@147: // cannam@147: // `content` should be all text from the start of the request to the first occurrance of two cannam@147: // newlines in a row -- including the first of these two newlines, but excluding the second. cannam@147: // cannam@147: // The parse is performed with zero copies: The callee clobbers `content` with '\0' characters cannam@147: // to split it into a bunch of shorter strings. The caller must keep `content` valid until the cannam@147: // `HttpHeaders` is destroyed, or pass it to `takeOwnership()`. cannam@147: cannam@147: kj::String serializeRequest(HttpMethod method, kj::StringPtr url, cannam@147: const ConnectionHeaders& connectionHeaders) const; cannam@147: kj::String serializeResponse(uint statusCode, kj::StringPtr statusText, cannam@147: const ConnectionHeaders& connectionHeaders) const; cannam@147: // Serialize the headers as a complete request or response blob. The blob uses '\r\n' newlines cannam@147: // and includes the double-newline to indicate the end of the headers. cannam@147: cannam@147: kj::String toString() const; cannam@147: cannam@147: private: cannam@147: HttpHeaderTable* table; cannam@147: cannam@147: kj::Array indexedHeaders; cannam@147: // Size is always table->idCount(). cannam@147: cannam@147: struct Header { cannam@147: kj::StringPtr name; cannam@147: kj::StringPtr value; cannam@147: }; cannam@147: kj::Vector
unindexedHeaders; cannam@147: cannam@147: kj::Vector> ownedStrings; cannam@147: cannam@147: kj::Maybe addNoCheck(kj::StringPtr name, kj::StringPtr value); cannam@147: cannam@147: kj::StringPtr cloneToOwn(kj::StringPtr str); cannam@147: cannam@147: kj::String serialize(kj::ArrayPtr word1, cannam@147: kj::ArrayPtr word2, cannam@147: kj::ArrayPtr word3, cannam@147: const ConnectionHeaders& connectionHeaders) const; cannam@147: cannam@147: bool parseHeaders(char* ptr, char* end, ConnectionHeaders& connectionHeaders); cannam@147: cannam@147: // TODO(perf): Arguably we should store a map, but header sets are never very long cannam@147: // TODO(perf): We could optimize for common headers by storing them directly as fields. We could cannam@147: // also add direct accessors for those headers. cannam@147: }; cannam@147: cannam@147: class WebSocket { cannam@147: public: cannam@147: WebSocket(kj::Own stream); cannam@147: // Create a WebSocket wrapping the given I/O stream. cannam@147: cannam@147: kj::Promise send(kj::ArrayPtr message); cannam@147: kj::Promise send(kj::ArrayPtr message); cannam@147: }; cannam@147: cannam@147: class HttpClient { cannam@147: // Interface to the client end of an HTTP connection. cannam@147: // cannam@147: // There are two kinds of clients: cannam@147: // * Host clients are used when talking to a specific host. The `url` specified in a request cannam@147: // is actually just a path. (A `Host` header is still required in all requests.) cannam@147: // * Proxy clients are used when the target could be any arbitrary host on the internet. cannam@147: // The `url` specified in a request is a full URL including protocol and hostname. cannam@147: cannam@147: public: cannam@147: struct Response { cannam@147: uint statusCode; cannam@147: kj::StringPtr statusText; cannam@147: const HttpHeaders* headers; cannam@147: kj::Own body; cannam@147: // `statusText` and `headers` remain valid until `body` is dropped. cannam@147: }; cannam@147: cannam@147: struct Request { cannam@147: kj::Own body; cannam@147: // Write the request entity body to this stream, then drop it when done. cannam@147: // cannam@147: // May be null for GET and HEAD requests (which have no body) and requests that have cannam@147: // Content-Length: 0. cannam@147: cannam@147: kj::Promise response; cannam@147: // Promise for the eventual respnose. cannam@147: }; cannam@147: cannam@147: virtual Request request(HttpMethod method, kj::StringPtr url, const HttpHeaders& headers, cannam@147: kj::Maybe expectedBodySize = nullptr) = 0; cannam@147: // Perform an HTTP request. cannam@147: // cannam@147: // `url` may be a full URL (with protocol and host) or it may be only the path part of the URL, cannam@147: // depending on whether the client is a proxy client or a host client. cannam@147: // cannam@147: // `url` and `headers` need only remain valid until `request()` returns (they can be cannam@147: // stack-allocated). cannam@147: // cannam@147: // `expectedBodySize`, if provided, must be exactly the number of bytes that will be written to cannam@147: // the body. This will trigger use of the `Content-Length` connection header. Otherwise, cannam@147: // `Transfer-Encoding: chunked` will be used. cannam@147: cannam@147: struct WebSocketResponse { cannam@147: uint statusCode; cannam@147: kj::StringPtr statusText; cannam@147: const HttpHeaders* headers; cannam@147: kj::OneOf, kj::Own> upstreamOrBody; cannam@147: // `statusText` and `headers` remain valid until `upstreamOrBody` is dropped. cannam@147: }; cannam@147: virtual kj::Promise openWebSocket( cannam@147: kj::StringPtr url, const HttpHeaders& headers, kj::Own downstream); cannam@147: // Tries to open a WebSocket. Default implementation calls send() and never returns a WebSocket. cannam@147: // cannam@147: // `url` and `headers` are invalidated when the returned promise resolves. cannam@147: cannam@147: virtual kj::Promise> connect(kj::String host); cannam@147: // Handles CONNECT requests. Only relevant for proxy clients. Default implementation throws cannam@147: // UNIMPLEMENTED. cannam@147: }; cannam@147: cannam@147: class HttpService { cannam@147: // Interface which HTTP services should implement. cannam@147: // cannam@147: // This interface is functionally equivalent to HttpClient, but is intended for applications to cannam@147: // implement rather than call. The ergonomics and performance of the method signatures are cannam@147: // optimized for the serving end. cannam@147: // cannam@147: // As with clients, there are two kinds of services: cannam@147: // * Host services are used when talking to a specific host. The `url` specified in a request cannam@147: // is actually just a path. (A `Host` header is still required in all requests, and the service cannam@147: // may in fact serve multiple origins via this header.) cannam@147: // * Proxy services are used when the target could be any arbitrary host on the internet, i.e. to cannam@147: // implement an HTTP proxy. The `url` specified in a request is a full URL including protocol cannam@147: // and hostname. cannam@147: cannam@147: public: cannam@147: class Response { cannam@147: public: cannam@147: virtual kj::Own send( cannam@147: uint statusCode, kj::StringPtr statusText, const HttpHeaders& headers, cannam@147: kj::Maybe expectedBodySize = nullptr) = 0; cannam@147: // Begin the response. cannam@147: // cannam@147: // `statusText` and `headers` need only remain valid until send() returns (they can be cannam@147: // stack-allocated). cannam@147: }; cannam@147: cannam@147: virtual kj::Promise request( cannam@147: HttpMethod method, kj::StringPtr url, const HttpHeaders& headers, cannam@147: kj::AsyncInputStream& requestBody, Response& response) = 0; cannam@147: // Perform an HTTP request. cannam@147: // cannam@147: // `url` may be a full URL (with protocol and host) or it may be only the path part of the URL, cannam@147: // depending on whether the service is a proxy service or a host service. cannam@147: // cannam@147: // `url` and `headers` are invalidated on the first read from `requestBody` or when the returned cannam@147: // promise resolves, whichever comes first. cannam@147: cannam@147: class WebSocketResponse: public Response { cannam@147: public: cannam@147: kj::Own startWebSocket( cannam@147: uint statusCode, kj::StringPtr statusText, const HttpHeaders& headers, cannam@147: WebSocket& upstream); cannam@147: // Begin the response. cannam@147: // cannam@147: // `statusText` and `headers` need only remain valid until startWebSocket() returns (they can cannam@147: // be stack-allocated). cannam@147: }; cannam@147: cannam@147: virtual kj::Promise openWebSocket( cannam@147: kj::StringPtr url, const HttpHeaders& headers, WebSocketResponse& response); cannam@147: // Tries to open a WebSocket. Default implementation calls request() and never returns a cannam@147: // WebSocket. cannam@147: // cannam@147: // `url` and `headers` are invalidated when the returned promise resolves. cannam@147: cannam@147: virtual kj::Promise> connect(kj::String host); cannam@147: // Handles CONNECT requests. Only relevant for proxy services. Default implementation throws cannam@147: // UNIMPLEMENTED. cannam@147: }; cannam@147: cannam@147: kj::Own newHttpClient(HttpHeaderTable& responseHeaderTable, kj::Network& network, cannam@147: kj::Maybe tlsNetwork = nullptr); cannam@147: // Creates a proxy HttpClient that connects to hosts over the given network. cannam@147: // cannam@147: // `responseHeaderTable` is used when parsing HTTP responses. Requests can use any header table. cannam@147: // cannam@147: // `tlsNetwork` is required to support HTTPS destination URLs. Otherwise, only HTTP URLs can be cannam@147: // fetched. cannam@147: cannam@147: kj::Own newHttpClient(HttpHeaderTable& responseHeaderTable, kj::AsyncIoStream& stream); cannam@147: // Creates an HttpClient that speaks over the given pre-established connection. The client may cannam@147: // be used as a proxy client or a host client depending on whether the peer is operating as cannam@147: // a proxy. cannam@147: // cannam@147: // Note that since this client has only one stream to work with, it will try to pipeline all cannam@147: // requests on this stream. If one request or response has an I/O failure, all subsequent requests cannam@147: // fail as well. If the destination server chooses to close the connection after a response, cannam@147: // subsequent requests will fail. If a response takes a long time, it blocks subsequent responses. cannam@147: // If a WebSocket is opened successfully, all subsequent requests fail. cannam@147: cannam@147: kj::Own newHttpClient(HttpService& service); cannam@147: kj::Own newHttpService(HttpClient& client); cannam@147: // Adapts an HttpClient to an HttpService and vice versa. cannam@147: cannam@147: struct HttpServerSettings { cannam@147: kj::Duration headerTimeout = 15 * kj::SECONDS; cannam@147: // After initial connection open, or after receiving the first byte of a pipelined request, cannam@147: // the client must send the complete request within this time. cannam@147: cannam@147: kj::Duration pipelineTimeout = 5 * kj::SECONDS; cannam@147: // After one request/response completes, we'll wait up to this long for a pipelined request to cannam@147: // arrive. cannam@147: }; cannam@147: cannam@147: class HttpServer: private kj::TaskSet::ErrorHandler { cannam@147: // Class which listens for requests on ports or connections and sends them to an HttpService. cannam@147: cannam@147: public: cannam@147: typedef HttpServerSettings Settings; cannam@147: cannam@147: HttpServer(kj::Timer& timer, HttpHeaderTable& requestHeaderTable, HttpService& service, cannam@147: Settings settings = Settings()); cannam@147: // Set up an HttpServer that directs incoming connections to the given service. The service cannam@147: // may be a host service or a proxy service depending on whether you are intending to implement cannam@147: // an HTTP server or an HTTP proxy. cannam@147: cannam@147: kj::Promise drain(); cannam@147: // Stop accepting new connections or new requests on existing connections. Finish any requests cannam@147: // that are already executing, then close the connections. Returns once no more requests are cannam@147: // in-flight. cannam@147: cannam@147: kj::Promise listenHttp(kj::ConnectionReceiver& port); cannam@147: // Accepts HTTP connections on the given port and directs them to the handler. cannam@147: // cannam@147: // The returned promise never completes normally. It may throw if port.accept() throws. Dropping cannam@147: // the returned promise will cause the server to stop listening on the port, but already-open cannam@147: // connections will continue to be served. Destroy the whole HttpServer to cancel all I/O. cannam@147: cannam@147: kj::Promise listenHttp(kj::Own connection); cannam@147: // Reads HTTP requests from the given connection and directs them to the handler. A successful cannam@147: // completion of the promise indicates that all requests received on the connection resulted in cannam@147: // a complete response, and the client closed the connection gracefully or drain() was called. cannam@147: // The promise throws if an unparseable request is received or if some I/O error occurs. Dropping cannam@147: // the returned promise will cancel all I/O on the connection and cancel any in-flight requests. cannam@147: cannam@147: private: cannam@147: class Connection; cannam@147: cannam@147: kj::Timer& timer; cannam@147: HttpHeaderTable& requestHeaderTable; cannam@147: HttpService& service; cannam@147: Settings settings; cannam@147: cannam@147: bool draining = false; cannam@147: kj::ForkedPromise onDrain; cannam@147: kj::Own> drainFulfiller; cannam@147: cannam@147: uint connectionCount = 0; cannam@147: kj::Maybe>> zeroConnectionsFulfiller; cannam@147: cannam@147: kj::TaskSet tasks; cannam@147: cannam@147: HttpServer(kj::Timer& timer, HttpHeaderTable& requestHeaderTable, HttpService& service, cannam@147: Settings settings, kj::PromiseFulfillerPair paf); cannam@147: cannam@147: kj::Promise listenLoop(kj::ConnectionReceiver& port); cannam@147: cannam@147: void taskFailed(kj::Exception&& exception) override; cannam@147: }; cannam@147: cannam@147: // ======================================================================================= cannam@147: // inline implementation cannam@147: cannam@147: inline void HttpHeaderId::requireFrom(HttpHeaderTable& table) const { cannam@147: KJ_IREQUIRE(this->table == nullptr || this->table == &table, cannam@147: "the provided HttpHeaderId is from the wrong HttpHeaderTable"); cannam@147: } cannam@147: cannam@147: inline kj::Own HttpHeaderTable::Builder::build() { return kj::mv(table); } cannam@147: inline HttpHeaderTable& HttpHeaderTable::Builder::getFutureTable() { return *table; } cannam@147: cannam@147: inline uint HttpHeaderTable::idCount() { return namesById.size(); } cannam@147: cannam@147: inline kj::StringPtr HttpHeaderTable::idToString(HttpHeaderId id) { cannam@147: id.requireFrom(*this); cannam@147: return namesById[id.id]; cannam@147: } cannam@147: cannam@147: inline kj::Maybe HttpHeaders::get(HttpHeaderId id) const { cannam@147: id.requireFrom(*table); cannam@147: auto result = indexedHeaders[id.id]; cannam@147: return result == nullptr ? kj::Maybe(nullptr) : result; cannam@147: } cannam@147: cannam@147: inline void HttpHeaders::unset(HttpHeaderId id) { cannam@147: id.requireFrom(*table); cannam@147: indexedHeaders[id.id] = nullptr; cannam@147: } cannam@147: cannam@147: template cannam@147: inline void HttpHeaders::forEach(Func&& func) const { cannam@147: for (auto i: kj::indices(indexedHeaders)) { cannam@147: if (indexedHeaders[i] != nullptr) { cannam@147: func(table->idToString(HttpHeaderId(table, i)), indexedHeaders[i]); cannam@147: } cannam@147: } cannam@147: cannam@147: for (auto& header: unindexedHeaders) { cannam@147: func(header.name, header.value); cannam@147: } cannam@147: } cannam@147: cannam@147: } // namespace kj cannam@147: cannam@147: #endif // KJ_COMPAT_HTTP_H_