xref: /aosp_15_r20/external/cronet/net/third_party/quiche/src/quiche/common/http/http_header_storage.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 #ifndef QUICHE_COMMON_HTTP_HTTP_HEADER_STORAGE_H_
2 #define QUICHE_COMMON_HTTP_HTTP_HEADER_STORAGE_H_
3 
4 #include "absl/container/inlined_vector.h"
5 #include "absl/strings/string_view.h"
6 #include "quiche/common/platform/api/quiche_export.h"
7 #include "quiche/common/quiche_simple_arena.h"
8 
9 namespace quiche {
10 
11 using Fragments = absl::InlinedVector<absl::string_view, 1>;
12 
13 // This class provides a backing store for absl::string_views. It previously
14 // used custom allocation logic, but now uses an UnsafeArena instead. It has the
15 // property that absl::string_views that refer to data in HttpHeaderStorage are
16 // never invalidated until the HttpHeaderStorage is deleted or Clear() is
17 // called.
18 //
19 // Write operations always append to the last block. If there is not enough
20 // space to perform the write, a new block is allocated, and any unused space
21 // is wasted.
22 class QUICHE_EXPORT HttpHeaderStorage {
23  public:
24   HttpHeaderStorage();
25 
26   HttpHeaderStorage(const HttpHeaderStorage&) = delete;
27   HttpHeaderStorage& operator=(const HttpHeaderStorage&) = delete;
28 
29   HttpHeaderStorage(HttpHeaderStorage&& other) = default;
30   HttpHeaderStorage& operator=(HttpHeaderStorage&& other) = default;
31 
32   absl::string_view Write(absl::string_view s);
33 
34   // If |s| points to the most recent allocation from arena_, the arena will
35   // reclaim the memory. Otherwise, this method is a no-op.
36   void Rewind(absl::string_view s);
37 
Clear()38   void Clear() { arena_.Reset(); }
39 
40   // Given a list of fragments and a separator, writes the fragments joined by
41   // the separator to a contiguous region of memory. Returns a absl::string_view
42   // pointing to the region of memory.
43   absl::string_view WriteFragments(const Fragments& fragments,
44                                    absl::string_view separator);
45 
bytes_allocated()46   size_t bytes_allocated() const { return arena_.status().bytes_allocated(); }
47 
48  private:
49   QuicheSimpleArena arena_;
50 };
51 
52 // Writes |fragments| to |dst|, joined by |separator|. |dst| must be large
53 // enough to hold the result. Returns the number of bytes written.
54 QUICHE_EXPORT size_t Join(char* dst, const Fragments& fragments,
55                           absl::string_view separator);
56 
57 }  // namespace quiche
58 
59 #endif  // QUICHE_COMMON_HTTP_HTTP_HEADER_STORAGE_H_
60