|
| 1 | +// |
| 2 | +// Copyright (c) 2025 Vinnie Falco (vinnie dot falco at gmail dot com) |
| 3 | +// |
| 4 | +// Distributed under the Boost Software License, Version 1.0. (See accompanying |
| 5 | +// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) |
| 6 | +// |
| 7 | +// Official repository: https://github.com/cppalliance/http |
| 8 | +// |
| 9 | + |
| 10 | +#ifndef BOOST_HTTP_SERVER_ETAG_HPP |
| 11 | +#define BOOST_HTTP_SERVER_ETAG_HPP |
| 12 | + |
| 13 | +#include <boost/http/detail/config.hpp> |
| 14 | +#include <boost/core/detail/string_view.hpp> |
| 15 | +#include <cstdint> |
| 16 | +#include <string> |
| 17 | + |
| 18 | +namespace boost { |
| 19 | +namespace http { |
| 20 | + |
| 21 | +/** Options for ETag generation. |
| 22 | +*/ |
| 23 | +struct etag_options |
| 24 | +{ |
| 25 | + /// Generate a weak ETag (prefixed with W/). |
| 26 | + bool weak = false; |
| 27 | +}; |
| 28 | + |
| 29 | +/** Generate an ETag from content. |
| 30 | +
|
| 31 | + Creates an ETag by computing a hash of the provided |
| 32 | + content. The resulting ETag can be used in HTTP |
| 33 | + responses to enable caching. |
| 34 | +
|
| 35 | + @par Example |
| 36 | + @code |
| 37 | + std::string content = "Hello, World!"; |
| 38 | + std::string tag = etag( content ); |
| 39 | + // tag == "\"d-3/1gIbsr1bCvZ2KQgJ7DpTGR3YH\"" |
| 40 | + @endcode |
| 41 | +
|
| 42 | + @param body The content to hash. |
| 43 | +
|
| 44 | + @param opts Options controlling ETag generation. |
| 45 | +
|
| 46 | + @return An ETag string suitable for use in the ETag header. |
| 47 | +*/ |
| 48 | +BOOST_HTTP_DECL |
| 49 | +std::string |
| 50 | +etag(core::string_view body, etag_options opts = {}); |
| 51 | + |
| 52 | +/** Generate an ETag from file metadata. |
| 53 | +
|
| 54 | + Creates an ETag based on a file's size and modification |
| 55 | + time. This is more efficient than hashing file content |
| 56 | + and is suitable for static file serving. |
| 57 | +
|
| 58 | + @par Example |
| 59 | + @code |
| 60 | + std::uint64_t size = 1234; |
| 61 | + std::uint64_t mtime = 1704067200; // Unix timestamp |
| 62 | + std::string tag = etag( size, mtime ); |
| 63 | + // tag == "\"4d2-65956a00\"" |
| 64 | + @endcode |
| 65 | +
|
| 66 | + @param size The file size in bytes. |
| 67 | +
|
| 68 | + @param mtime The file modification time (typically Unix timestamp). |
| 69 | +
|
| 70 | + @param opts Options controlling ETag generation. |
| 71 | +
|
| 72 | + @return An ETag string suitable for use in the ETag header. |
| 73 | +*/ |
| 74 | +BOOST_HTTP_DECL |
| 75 | +std::string |
| 76 | +etag( |
| 77 | + std::uint64_t size, |
| 78 | + std::uint64_t mtime, |
| 79 | + etag_options opts = {}); |
| 80 | + |
| 81 | +} // http |
| 82 | +} // boost |
| 83 | + |
| 84 | +#endif |
0 commit comments