AnCH REST library declares REST APIs independently of HTTP servers, and provides an HTTP client over native HTTP libraries (see anch-rest.todo for the work in progress).
anch-rest is built by default (option ANCH_BUILD_REST , since 0.3). Optional libraries are skipped with a CMake status message when their dependencies are not found. Codecs (media types, JSON, Protocol Buffers, Thrift) are in anch-codec (see AnCH codec library documentation , since 0.3).
anch-rest-common : HTTP model (Method, Status, Headers, MediaType, Request, Response) and forms codecs (FormUrlEncoded, Multipart) ; it depends on anch-codec-json .anch-rest-server : endpoints declaration and routing (Api).anch-rest-client : HTTP client (Client) over a Transport.anch-rest-curl : libcurl transport (CurlTransport), option ANCH_REST_CURL (built when libcurl is found).anch-rest-nginx : nginx applications bridge (ANCH_REST_APPLICATION ), option ANCH_REST_NGINX , and nginx module sources (nginx/ , installed in share/anch/nginx-module ).anch-rest-protobuf : Protocol Buffers RPC services (RpcService) served in an API with Connect and gRPC-Web protocols, Connect client ; option ANCH_REST_PROTOBUF (built with anch-codec-protobuf , when Protobuf is found).anch-rest-grpc : gRPC server (GrpcServer) for the same RPC services ; option ANCH_REST_GRPC (built when gRPC is found).anch-rest-thrift (header only): Thrift services in an API (installThrift) and Thrift transport over the client (ThriftTransport) ; option ANCH_REST_THRIFT (built with anch-codec-thrift , when Thrift and Boost headers are found).Endpoints are declared with a method and a path pattern. Endpoint function arguments are bound from the request: [const Request&], path parameters..., [Body<T>]. The result is serialized with the codec negotiated with Accept header:
Errors: invalid path parameter or body 400 , unknown path 404 , unknown method 405 (with Allow ), not acceptable 406 , unsupported media type 415 , HttpException status, other exceptions 500 . OPTIONS without endpoint is answered with 204 and Allow .
Media types without codec are read as a stream: a std::istream& last argument receives the raw body whatever its Content-Type is (absent included, empty stream without body). The client sends a stream with Call::body(input, contentType, size) or anch::rest::RawBody (streamed by the libcurl transport when the size is known):
Form is an ordered list of Part (fields and files, a name can be repeated). FormUrlEncoded codec reads and writes application/x-www-form-urlencoded bodies and Multipart codec reads and writes multipart/form-data bodies (with files); fields are converted on server side like query parameters:
Other types are read and written through a Form with their FormMapper (written by hand or generated by anch-oas): helpers convert fields ( addFormField , formField , formFields ), files ( addFormFile , requiredFormFile ), JSON parts ( addJsonFormField , jsonFormField ) and check anch-json constraints on server side ( checkFormConstraints ):
Bodies are limited by side (FormLimits : body size, number of parts, part size, part headers size): over a limit, server answers 413 .
Large files do not need memory: multipart parts larger than FormLimits::maxMemoryPartSize (1 MiB) are read in temporary files (deleted with their last part copy), parts are written from their file and the client streams multipart bodies ( Content-Length is known, libcurl transport reads the stream):
Part::open() reads a part in memory or in a file; Part::content() loads a file in memory.
Cross-Origin Resource Sharing is enabled on the API. Allowed methods are the methods of the routes matching the path. Preflight requests are answered before filters (they have no credentials) and CORS headers are added to every response, errors included:
A preflight with a not allowed origin, method or header is answered with 403 without CORS headers. Vary: Origin is added when origins are listed.
Path placeholders are replaced in order and percent-encoded. Accepted codecs are part of the call type: as<T>() is checked at compile time and deserializes the response with the codec matching its Content-Type :
Network errors (connection refused, timeout, TLS) are reported with TransportError.
anch-json fields constraints ( anch::val::required , anch::val::minLength , ...) are checked by the Json codec according to the side which uses it ( Side ):
400 response, its body is the error message with the path of the invalid value, such as /name: Length 0 is lower than minLength 1), responses are not ;body<Json>(value) raises anch::json::MappingError , nothing is sent), server responses are trusted and read with tolerance (unknown fields are ignored, indented responses are accepted).Each side has its own mapping options (server and client can run in the same process), which can be changed at application initialization:
Other codecs can behave according to the side with serialize(value, out, side) and deserialize(value, input, side) functions.
protoc messages are read and written by anch-codec-protobuf Protobuf ( application/x-protobuf ) and ProtobufJson ( application/json , Protobuf JSON mapping) codecs:
RPC services are declared once and served with three protocols: Connect and gRPC-Web unary calls in an API (HTTP/1.1: nginx, curl, browsers) and gRPC (HTTP/2) with GrpcServer:
Handler HttpException status is converted ( 400 is INVALID_ARGUMENT , 404 is NOT_FOUND , ...), other exceptions are INTERNAL without message. Request and response metadata (HTTP headers, gRPC metadata) are given by RpcContext. Native gRPC clients use grpc_cpp_plugin stubs. Only unary calls are supported.
Structures generated by thrift –gen cpp are read and written by anch-codec-thrift ThriftBinary ( application/vnd.apache.thrift.binary ), ThriftCompact ( .compact ) and ThriftJson ( .json ) codecs. Thrift services are served over HTTP by an API, as Thrift THttpServer (the protocol is chosen with the Content-Type , application/x-thrift is the binary protocol), and generated clients call them through an anch-rest client:
An application is a shared library which declares its API factory:
The module is built with nginx (--with-threads to execute handlers in a thread pool):
nginx configuration:
Libraries are loaded when configuration is read (nginx -t reports loading errors) and APIs are created in each worker process.
Request and response bodies are streamed (anch::cutils::CIStream and anch::cutils::COStream on the module C functions):
client_body_buffer_size bytes are waiting for the handler), and its response body is sent while it is written, with at most anch_rest_buffers in memory: the handler waits while they are being sent ;Content-Length when it fits in one buffer. An error raised before the start is answered with 500 , an error raised after closes the connection (truncated response) ;client_body_timeout and send_timeout bound it ;anch_rest_thread_pool off ), the handler is executed in the event loop: the request body is buffered by nginx (memory, then temporary file) and read in blocks, and the response body is given to nginx as it is written, without flow control (a large response stays in memory until it has been sent).