Serve http:h2

Learn how to serve Http2.

This document talks about ordinary htt/h2 services rather than protobuf services accessible via http/h2. http/h2 services in brpc have to declare interfaces with empty request and response in a .proto file. This requirement keeps all service declarations inside proto files rather than scattering in code, configurations, and proto files.

Example

http_server.cpp

About h2

brpc names the HTTP/2 protocol to “h2”, no matter encrypted or not. However HTTP/2 connections without SSL are shown on /connections with the official name “h2c”, and the ones with SSL are shown as “h2”.

The APIs for http and h2 in brpc are basically same. Without explicit statement, mentioned http features work for h2 as well.

URL types

/ServiceName/MethodName as the prefix

Define a service named ServiceName(not including the package name), with a method named MethodName and empty request/response, the service will provide http/h2 service on /ServiceName/MethodName by default.

The reason that request and response can be empty is that all data are in Controller:

  • Header of the http/h2 request is in Controller.http_request() and the body is in Controller.request_attachment().
  • Header of the http/h2 response is in Controller.http_response() and the body is in Controller.response_attachment().

Implementation steps:

  1. Add the service declaration in a proto file.
  1. option cc_generic_services = true;
  2. message HttpRequest { };
  3. message HttpResponse { };
  4. service HttpService {
  5. rpc Echo(HttpRequest) returns (HttpResponse);
  6. };
  1. Implement the service by inheriting the base class generated in .pb.h, which is same as protobuf services.
  1. class HttpServiceImpl : public HttpService {
  2. public:
  3. ...
  4. virtual void Echo(google::protobuf::RpcController* cntl_base,
  5. const HttpRequest* /*request*/,
  6. HttpResponse* /*response*/,
  7. google::protobuf::Closure* done) {
  8. brpc::ClosureGuard done_guard(done);
  9. brpc::Controller* cntl = static_cast<brpc::Controller*>(cntl_base);
  10. // body is plain text
  11. cntl->http_response().set_content_type("text/plain");
  12. // Use printed query string and body as the response.
  13. butil::IOBufBuilder os;
  14. os << "queries:";
  15. for (brpc::URI::QueryIterator it = cntl->http_request().uri().QueryBegin();
  16. it != cntl->http_request().uri().QueryEnd(); ++it) {
  17. os << ' ' << it->first << '=' << it->second;
  18. }
  19. os << "\nbody: " << cntl->request_attachment() << '\n';
  20. os.move_to(cntl->response_attachment());
  21. }
  22. };
  1. After adding the implemented instance into the server, the service is accessible via following URLs (Note that the path after /HttpService/Echo is filled into cntl->http_request().unresolved_path(), which is always normalized):
URLProtobuf Methodcntl->http_request().uri().path()cntl->http_request().unresolved_path()
/HttpService/EchoHttpService.Echo“/HttpService/Echo”“”
/HttpService/Echo/FooHttpService.Echo“/HttpService/Echo/Foo”“Foo”
/HttpService/Echo/Foo/BarHttpService.Echo“/HttpService/Echo/Foo/Bar”“Foo/Bar”
/HttpService//Echo///Foo//HttpService.Echo“/HttpService//Echo///Foo//”“Foo”
/HttpServiceNo such method

/ServiceName as the prefix

http/h2 services for managing resources may need this kind of URL, such as /FileService/foobar.txt represents ./foobar.txt and /FileService/app/data/boot.cfg represents ./app/data/boot.cfg.

Implementation steps:

  1. Use FileService as the service name and default_method as the method name in the proto file.
  1. option cc_generic_services = true;
  2. message HttpRequest { };
  3. message HttpResponse { };
  4. service FileService {
  5. rpc default_method(HttpRequest) returns (HttpResponse);
  6. }
  1. Implement the service.
  1. class FileServiceImpl: public FileService {
  2. public:
  3. ...
  4. virtual void default_method(google::protobuf::RpcController* cntl_base,
  5. const HttpRequest* /*request*/,
  6. HttpResponse* /*response*/,
  7. google::protobuf::Closure* done) {
  8. brpc::ClosureGuard done_guard(done);
  9. brpc::Controller* cntl = static_cast<brpc::Controller*>(cntl_base);
  10. cntl->response_attachment().append("Getting file: ");
  11. cntl->response_attachment().append(cntl->http_request().unresolved_path());
  12. }
  13. };
  1. After adding the implemented instance into the server, the service is accessible via following URLs (the path after /FileService is filled in cntl->http_request().unresolved_path(), which is always normalized):
URLProtobuf Methodcntl->http_request().uri().path()cntl->http_request().unresolved_path()
/FileServiceFileService.default_method“/FileService”“”
/FileService/123.txtFileService.default_method“/FileService/123.txt”“123.txt”
/FileService/mydir/123.txtFileService.default_method“/FileService/mydir/123.txt”“mydir/123.txt”
/FileService//mydir///123.txt//FileService.default_method“/FileService//mydir///123.txt//”“mydir/123.txt”

Restful URL

brpc supports specifying a URL for each method in a service. The API is as follows:

  1. // If `restful_mappings' is non-empty, the method in service can
  2. // be accessed by the specified URL rather than /ServiceName/MethodName.
  3. // Mapping rules: "PATH1 => NAME1, PATH2 => NAME2 ..."
  4. // where `PATH' is a valid path and `NAME' is the method name.
  5. int AddService(google::protobuf::Service* service,
  6. ServiceOwnership ownership,
  7. butil::StringPiece restful_mappings);

QueueService defined below contains several methods. If the service is added into the server normally, it’s accessible via URLs like /QueueService/start and /QueueService/stop.

  1. service QueueService {
  2. rpc start(HttpRequest) returns (HttpResponse);
  3. rpc stop(HttpRequest) returns (HttpResponse);
  4. rpc get_stats(HttpRequest) returns (HttpResponse);
  5. rpc download_data(HttpRequest) returns (HttpResponse);
  6. };

By specifying the 3rd parameter restful_mappings to AddService, the URL can be customized:

  1. if (server.AddService(&queue_svc,
  2. brpc::SERVER_DOESNT_OWN_SERVICE,
  3. "/v1/queue/start => start,"
  4. "/v1/queue/stop => stop,"
  5. "/v1/queue/stats/* => get_stats") != 0) {
  6. LOG(ERROR) << "Fail to add queue_svc";
  7. return -1;
  8. }
  9. if (server.AddService(&queue_svc,
  10. brpc::SERVER_DOESNT_OWN_SERVICE,
  11. "/v1/*/start => start,"
  12. "/v1/*/stop => stop,"
  13. "*.data => download_data") != 0) {
  14. LOG(ERROR) << "Fail to add queue_svc";
  15. return -1;
  16. }

There are 3 mappings separated by comma in the 3rd parameter (which is a string spanning 3 lines) to the AddService. Each mapping tells brpc to call the method at right side of the arrow if the left side matches the URL. The asterisk in /v1/queue/stats/* matches any string.

More about mapping rules:

  • Multiple paths can be mapped to a same method.
  • Both http/h2 and protobuf services are supported.
  • Un-mapped methods are still accessible via /ServiceName/MethodName. Mapped methods are not accessible via /ServiceName/MethodName anymore.
  • ==> and ===> are both OK, namely extra spaces at the beginning or the end, extra slashes, extra commas at the end, are all accepted.
  • Pattern PATH and PATH/* can coexist.
  • Support suffix matching: characters can appear after the asterisk.
  • At most one asterisk is allowed in a path.

The path after asterisk can be obtained by cntl.http_request().unresolved_path(), which is always normalized, namely no slashes at the beginning or the end, and no repeated slashes in the middle. For example:

img

or:

img

in which unresolved_path are both foo/bar. The extra slashes at the left, the right, or the middle are removed.

Note that cntl.http_request().uri().path() is not ensured to be normalized, which is "//v1//queue//stats//foo///bar//////" and "//vars///foo////bar/////" respectively in the above example.

The built-in service page of /status shows customized URLs after the methods, in form of @URL1 @URL2

img

HTTP Parameters

HTTP headers

HTTP headers are a series of key/value pairs, some of them are defined by the HTTP specification, while others are free to use.

Query strings are also key/value pairs. Differences between HTTP headers and query strings:

  • Although operations on HTTP headers are accurately defined by the http specification, but http headers cannot be modified directly from an address bar, they are often used for passing parameters of a protocol or framework.
  • Query strings is part of the URL and often in form of key1=value1&key2=value2&..., which is easy to read and modify. They’re often used for passing application-level parameters. However format of query strings is not defined in HTTP spec, just a convention.
  1. // Get value for header "User-Agent" (case insensitive)
  2. const std::string* user_agent_str = cntl->http_request().GetHeader("User-Agent");
  3. if (user_agent_str != NULL) { // has the header
  4. LOG(TRACE) << "User-Agent is " << *user_agent_str;
  5. }
  6. ...
  7. // Add a header "Accept-encoding: gzip" (case insensitive)
  8. cntl->http_response().SetHeader("Accept-encoding", "gzip");
  9. // Overwrite the previous header "Accept-encoding: deflate"
  10. cntl->http_response().SetHeader("Accept-encoding", "deflate");
  11. // Append value to the previous header so that it becomes
  12. // "Accept-encoding: deflate,gzip" (values separated by comma)
  13. cntl->http_response().AppendHeader("Accept-encoding", "gzip");

Content-Type

Content-type is a frequently used header for storing type of the HTTP body, and specially processed in brpc and accessible by cntl->http_request().content_type() . As a correspondence, cntl->GetHeader("Content-Type") returns nothing.

  1. // Get Content-Type
  2. if (cntl->http_request().content_type() == "application/json") {
  3. ...
  4. }
  5. ...
  6. // Set Content-Type
  7. cntl->http_response().set_content_type("text/html");

If the RPC fails (Controller has been SetFailed), the framework overwrites Content-Type with text/plain and sets the response body with Controller::ErrorText().

Status Code

Status code is a special field in HTTP response to store processing result of the http request. Possible values are defined in http_status_code.h.

  1. // Get Status Code
  2. if (cntl->http_response().status_code() == brpc::HTTP_STATUS_NOT_FOUND) {
  3. LOG(FATAL) << "FAILED: " << controller.http_response().reason_phrase();
  4. }
  5. ...
  6. // Set Status code
  7. cntl->http_response().set_status_code(brpc::HTTP_STATUS_INTERNAL_SERVER_ERROR);
  8. cntl->http_response().set_status_code(brpc::HTTP_STATUS_INTERNAL_SERVER_ERROR, "My explanation of the error...");

For example, following code implements redirection with status code 302:

  1. cntl->http_response().set_status_code(brpc::HTTP_STATUS_FOUND);
  2. cntl->http_response().SetHeader("Location", "http://bj.bs.bae.baidu.com/family/image001(4979).jpg");

img

Query String

As mentioned in above HTTP headers, query strings are interpreted in common convention, whose form is key1=value1&key2=value2&…. Keys without values are acceptable as well and accessible by GetQuery which returns an empty string. Such keys are often used as boolean flags. Full API are defined in uri.h.

  1. const std::string* time_value = cntl->http_request().uri().GetQuery("time");
  2. if (time_value != NULL) { // the query string is present
  3. LOG(TRACE) << "time = " << *time_value;
  4. }
  5. ...
  6. cntl->http_request().uri().SetQuery("time", "2015/1/2");

Debugging

Turn on -http_verbose to print contents of all http requests and responses. Note that this should only be used for debugging rather than online services.

Compress the response body

HTTP services often compress http bodies to reduce transmission latency of web pages and speed up the presentations to end users.

Call Controller::set_response_compress_type(brpc::COMPRESS_TYPE_GZIP) to try to compress the http body with gzip. “Try to” means the compression may not happen in following conditions:

  • The request does not set Accept-encoding or the value does not contain “gzip”. For example, curl does not support compression without option --compressed, in which case the server always returns uncompressed results.

  • Body size is less than the bytes specified by -http_body_compress_threshold (512 by default). gzip is not a very fast compression algorithm. When the body is small, the delay added by compression may be larger than the time saved by network transmission. No compression when the body is relatively small is probably a better choice.

    NameValueDescriptionDefined At
    http_body_compress_threshold512Not compress http body when it’s less than so many bytes.src/brpc/policy/http_rpc_protocol.cpp

Decompress the request body

Due to generality, brpc does not decompress request bodies automatically, but users can do the job by themselves as follows:

  1. #include <brpc/policy/gzip_compress.h>
  2. ...
  3. const std::string* encoding = cntl->http_request().GetHeader("Content-Encoding");
  4. if (encoding != NULL && *encoding == "gzip") {
  5. butil::IOBuf uncompressed;
  6. if (!brpc::policy::GzipDecompress(cntl->request_attachment(), &uncompressed)) {
  7. LOG(ERROR) << "Fail to un-gzip request body";
  8. return;
  9. }
  10. cntl->request_attachment().swap(uncompressed);
  11. }
  12. // cntl->request_attachment() contains the data after decompression

Serve https requests

https is short for “http over SSL”, SSL is not exclusive for http, but effective for all protocols. The generic method for turning on server-side SSL is here.

Performance

Productions without extreme performance requirements tend to use HTTP protocol, especially mobile products. Thus we put great emphasis on implementation qualities of HTTP. To be more specific:

  • Use http parser of node.js to parse http messages, which is a lightweight, well-written, and extensively used implementation.
  • Use rapidjson to parse json, which is a json library focuses on performance.
  • In the worst case, the time complexity of parsing http requests is still O(N), where N is byte size of the request. As a contrast, parsing code that requires the http request to be complete, may cost O(N^2) time in the worst case. This feature is very helpful since many HTTP requests are large.
  • Processing HTTP messages from different clients is highly concurrent, even a pretty complicated http message does not block responding other clients. It’s difficult to achieve this for other RPC implementations and http servers often based on single-threaded reactor.

Progressive sending

brpc server is capable of sending large or infinite sized body, in following steps:

  1. Call Controller::CreateProgressiveAttachment() to create a body that can be written progressively. The returned ProgressiveAttachment object should be managed by intrusive_ptr
  1. #include <brpc/progressive_attachment.h>
  2. ...
  3. butil::intrusive_ptr<brpc::ProgressiveAttachment> pa = cntl->CreateProgressiveAttachment();
  1. Call ProgressiveAttachment::Write() to send the data.

    • If the write occurs before running of the server-side done, the sent data is cached until the done is called.
    • If the write occurs after running of the server-side done, the sent data is written out in chunked mode immediately.
  2. After usage, destruct all butil::intrusive_ptr<brpc::ProgressiveAttachment> to release related resources.

Progressive receiving

Currently brpc server doesn’t support calling the service callback once header part in the http request is parsed. In other words, brpc server is not suitable for receiving large or infinite sized body.

FAQ

Q: The nginx before brpc encounters final fail

The error is caused by that brpc server closes the http connection directly without sending a response.

brpc server supports a variety of protocols on the same port. When a request is failed to be parsed in HTTP, it’s hard to tell that the request is definitely in HTTP. If the request is very likely to be one, the server sends HTTP 400 errors and closes the connection. However, if the error is caused HTTP method(at the beginning) or ill-formed serialization (may be caused by bugs at the HTTP client), the server still closes the connection without sending a response, which leads to “final fail” at nginx.

Solution: When using Nginx to forward traffic, set $HTTP_method to allowed HTTP methods or simply specify the HTTP method in proxy_method.

Q: Does brpc support http chunked mode

Yes.

Q: Why do HTTP requests containing BASE64 encoded query string fail to parse sometimes?

According to the HTTP specification, following characters need to be encoded with %.

  1. reserved = gen-delims / sub-delims
  2. gen-delims = ":" / "/" / "?" / "#" / "[" / "]" / "@"
  3. sub-delims = "!" / "$" / "&" / "'" / "(" / ")"
  4. / "*" / "+" / "," / ";" / "="

Base64 encoded string may end with = which is a reserved character (take ?wi=NDgwMDB8dGVzdA==&anothorkey=anothervalue as an example). The strings may be parsed successfully, or may be not, depending on the implementation which should not be assumed in principle.

One solution is to remove the trailing = which does not affect the Base64 decoding. Another method is to percent-encode the URI, and do percent-decoding before Base64 decoding.


Last modified October 7, 2024: Oncall report (1b7065e)