1
+ /* *
2
+ * Copyright (c) 2025 Varnish Software AS
3
+ * All rights reserved.
4
+ *
5
+ * Redistribution and use in source and binary forms, with or without
6
+ * modification, are permitted provided that the following conditions
7
+ * are met:
8
+ * 1. Redistributions of source code must retain the above copyright
9
+ * notice, this list of conditions and the following disclaimer.
10
+ * 2. Redistributions in binary form must reproduce the above copyright
11
+ * notice, this list of conditions and the following disclaimer in the
12
+ * documentation and/or other materials provided with the distribution.
13
+ *
14
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17
+ * ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
18
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24
+ * SUCH DAMAGE.
25
+ **/
1
26
#include " kvm_api.h"
2
27
#include < array>
3
28
#include < cstring>
@@ -14,22 +39,22 @@ struct Backend
14
39
backend_response (status, ct.begin (), ct.size (), cont, cont_len);
15
40
}
16
41
static void response (uint16_t status, std::string_view ct, const void *cont, size_t cont_len,
17
- const std::vector<std::string> &headers, float ttl = 0 .0f , float grace = 0 .0f , float keep = 0 .0f )
42
+ const std::vector<std::string> &headers, float ttl = 0 .0f , float grace = 0 .0f , float keep = 0 .0f )
18
43
{
19
- std::array<ResponseHeader, 64 > header_array;
20
- size_t i = 0 ;
21
- for (const auto & header : headers) {
22
- header_array.at (i).field = header.data ();
23
- header_array.at (i).field_len = header.size ();
24
- i++;
25
- }
26
- BackendResponseExtra extra = {};
27
- extra.headers = header_array.data ();
28
- extra.num_headers = i;
29
- extra.cached = ttl > 0 .0f ;
30
- extra.ttl = ttl;
31
- extra.grace = grace;
32
- extra.keep = keep;
44
+ std::array<ResponseHeader, 64 > header_array;
45
+ size_t i = 0 ;
46
+ for (const auto & header : headers) {
47
+ header_array.at (i).field = header.data ();
48
+ header_array.at (i).field_len = header.size ();
49
+ i++;
50
+ }
51
+ BackendResponseExtra extra = {};
52
+ extra.headers = header_array.data ();
53
+ extra.num_headers = i;
54
+ extra.cached = ttl > 0 .0f ;
55
+ extra.ttl = ttl;
56
+ extra.grace = grace;
57
+ extra.keep = keep;
33
58
backend_response_extra (status, ct.begin (), ct.size (), cont, cont_len, &extra);
34
59
}
35
60
@@ -38,7 +63,7 @@ struct Backend
38
63
backend_response (status, ct.begin (), ct.size (), cont.begin (), cont.size ());
39
64
}
40
65
static void response (uint16_t status, std::string_view ct, std::string_view cont,
41
- const std::vector<std::string> &headers, float ttl = 0 .0f , float grace = 0 .0f , float keep = 0 .0f )
66
+ const std::vector<std::string> &headers, float ttl = 0 .0f , float grace = 0 .0f , float keep = 0 .0f )
42
67
{
43
68
response (status, ct, cont.begin (), cont.size (), headers, ttl, grace, keep);
44
69
}
@@ -47,17 +72,18 @@ struct Backend
47
72
struct Storage
48
73
{
49
74
template <size_t Size = 16UL << 20 > /* 16MB buffer */
50
- static std::string get (std::string_view arg, storage_func func)
75
+ static std::string get (std::string_view arg, storage_func func)
76
+ {
77
+ std::string buffer;
78
+ buffer.resize (Size );
79
+ const long len = storage_call (func, arg.begin (), arg.size (), buffer.data (), buffer.size ());
80
+ if (len >= 0 )
51
81
{
52
- std::string buffer;
53
- buffer.resize (Size );
54
- const long len = storage_call (func, arg.begin (), arg.size (), buffer.data (), buffer.size ());
55
- if (len >= 0 ) {
56
- buffer.resize (len);
57
- return buffer;
58
- }
59
- throw std::runtime_error (" Storage access failed" );
82
+ buffer.resize (len);
83
+ return buffer;
60
84
}
85
+ throw std::runtime_error (" Storage access failed" );
86
+ }
61
87
62
88
template <size_t Size = 16UL << 20 > /* 16MB buffer */
63
89
static std::string get (const std::vector<std::string>& arg, storage_func func)
0 commit comments