-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathetag.cpp
More file actions
92 lines (78 loc) · 2.04 KB
/
etag.cpp
File metadata and controls
92 lines (78 loc) · 2.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
//
// Copyright (c) 2025 Vinnie Falco (vinnie.falco@gmail.com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
// Official repository: https://github.com/cppalliance/http
//
#include <boost/http/server/etag.hpp>
#include <cstdio>
namespace boost {
namespace http {
namespace {
// Simple FNV-1a hash for content
std::uint64_t
fnv1a_hash( core::string_view data ) noexcept
{
constexpr std::uint64_t basis = 14695981039346656037ULL;
constexpr std::uint64_t prime = 1099511628211ULL;
std::uint64_t hash = basis;
for( unsigned char c : data )
{
hash ^= c;
hash *= prime;
}
return hash;
}
// Convert to null-terminated hex string
void
to_hex( std::uint64_t value, char* out ) noexcept
{
constexpr char hex[] = "0123456789abcdef";
for( int i = 15; i >= 0; --i )
{
out[i] = hex[value & 0xF];
value >>= 4;
}
out[16] = '\0';
}
} // (anon)
std::string
etag( core::string_view body, etag_options opts )
{
auto const hash = fnv1a_hash( body );
char hex[17];
to_hex( hash, hex );
char buf[64];
int n;
if( opts.weak )
n = std::snprintf( buf, sizeof(buf),
"W/\"%zx-%s\"", body.size(), hex );
else
n = std::snprintf( buf, sizeof(buf),
"\"%zx-%s\"", body.size(), hex );
return std::string( buf, static_cast<std::size_t>(n) );
}
std::string
etag(
std::uint64_t size,
std::uint64_t mtime,
etag_options opts )
{
char buf[64];
int n;
if( opts.weak )
n = std::snprintf( buf, sizeof(buf),
"W/\"%llx-%llx\"",
static_cast<unsigned long long>( size ),
static_cast<unsigned long long>( mtime ) );
else
n = std::snprintf( buf, sizeof(buf),
"\"%llx-%llx\"",
static_cast<unsigned long long>( size ),
static_cast<unsigned long long>( mtime ) );
return std::string( buf, static_cast<std::size_t>(n) );
}
} // http
} // boost