|
| 1 | +--- |
| 2 | +layout: post |
| 3 | +nav-class: dark |
| 4 | +categories: mohammad |
| 5 | +title: Boost.Http.Io and Boost.Http.Proto Project Highlights |
| 6 | +author-id: mohammad |
| 7 | +--- |
| 8 | + |
| 9 | +Here’s a look at some recent projects I've been focusing on: |
| 10 | + |
| 11 | +## Boost.Http.Io |
| 12 | + |
| 13 | +### `burl` project |
| 14 | + |
| 15 | +We've recently started adding a new example to the |
| 16 | +[http-io](https://github.com/cppalliance/http_io) library called |
| 17 | +[burl](https://github.com/cppalliance/http_io/tree/develop/example/client/burl). |
| 18 | +This is a relatively large example application designed to serve as a drop-in |
| 19 | +replacement for Curl's HTTP functionality. |
| 20 | + |
| 21 | +The primary goal of the project is to ensure that |
| 22 | +[http-proto](https://github.com/cppalliance/http_proto) and |
| 23 | +[http-io](https://github.com/cppalliance/http_io) provide all the necessary |
| 24 | +features for building a curl-like application. It also aims to demonstrate how |
| 25 | +these libraries can be leveraged to perform common HTTP tasks. The project has |
| 26 | +made excellent progress so far, with support for around 90 Curl command-line |
| 27 | +options, covering nearly all HTTP and TLS-related features provided by Curl. |
| 28 | + |
| 29 | +During development, we identified the need for three additional libraries (or |
| 30 | +http-proto services) that could benefit users: |
| 31 | + |
| 32 | +- **Multipart/form-data Library**: A container and parser/serializer for working |
| 33 | + with form data on both the client and server sides. |
| 34 | +- **CookieJar Library**: A utility for storing cookies and tracking |
| 35 | + modifications made to the jar. |
| 36 | +- **Public Suffix List Library**: A library that utilizes Mozilla’s Public |
| 37 | + Suffix List to accurately and efficiently identify a domain suffix. This is |
| 38 | + crucial for enhancing the CookieJar implementation by preventing supercookie |
| 39 | + vulnerabilities and proper validation of wildcard SSL/TLS certificates. |
| 40 | + |
| 41 | + |
| 42 | +## Boost.Http.Proto |
| 43 | + |
| 44 | +### Different Styles of Body Attachment in the Parser Interface |
| 45 | + |
| 46 | +The [http-proto](https://github.com/cppalliance/http_proto) library is a sans-IO |
| 47 | +implementation of the HTTP protocol. It is designed to facilitate the reception |
| 48 | +of HTTP message bodies with minimal bookkeeping required from the user at the |
| 49 | +call site. |
| 50 | + |
| 51 | +Currently, the library supports three distinct styles of body attachment: |
| 52 | + |
| 53 | +#### In-Place Body |
| 54 | + |
| 55 | +This style allows users to leverage the parser's internal buffer to read the |
| 56 | +body either in chunks or as a complete view if it fits entirely within the |
| 57 | +internal buffer. This approach is efficient for scenarios where the body size is |
| 58 | +known to be small or when incremental processing is required. |
| 59 | + |
| 60 | +```C++ |
| 61 | +read_header(stream, parser); |
| 62 | + |
| 63 | +// When the entire body fits in the internal buffer |
| 64 | +read(stream, parser); |
| 65 | +string_view body = parser.body(); |
| 66 | + |
| 67 | +// Reading the body in chunks |
| 68 | +while (!parser.is_complete()) |
| 69 | +{ |
| 70 | + read_some(stream, parser); |
| 71 | + auto buf = parser.pull_body(); |
| 72 | + parser.consume_body(buffer::buffer_size(buf)); |
| 73 | +} |
| 74 | +``` |
| 75 | +
|
| 76 | +
|
| 77 | +#### Sink Body |
| 78 | +
|
| 79 | +The sink body style allows users to process body content directly from the |
| 80 | +parser's internal buffer, either in one step or through multiple iterations. |
| 81 | +This method is particularly useful for writing body data to external storage, |
| 82 | +such as a file. The parser takes ownership of the sink object, driving the |
| 83 | +processing logic by invoking its virtual interfaces. This style is ideal for |
| 84 | +streaming large bodies directly to a sink without needing to hold the entire |
| 85 | +body in memory. |
| 86 | +
|
| 87 | +```C++ |
| 88 | +read_header(stream, parser); |
| 89 | +
|
| 90 | +http_proto::file file; |
| 91 | +system::error_code ec; |
| 92 | +file.open("./index.html", file_mode::write_new, ec); |
| 93 | +if (ec.failed()) |
| 94 | + return ec; |
| 95 | +
|
| 96 | +parser.set_body<file_body>(std::move(file)); |
| 97 | +
|
| 98 | +read(stream, parser); |
| 99 | +``` |
| 100 | + |
| 101 | +#### Dynamic Buffer |
| 102 | + |
| 103 | +The dynamic buffer interface allows the parser to write body content directly |
| 104 | +into a user-provided buffer or container, reducing the need for additional |
| 105 | +copying and intermediate buffering. |
| 106 | + |
| 107 | +```C++ |
| 108 | +read_header(stream, parser); |
| 109 | + |
| 110 | +std::string body; |
| 111 | +parser.set_body(buffers::dynamic_for(body)); |
| 112 | + |
| 113 | +read(stream, parser); |
| 114 | +``` |
0 commit comments