-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasic.cpp
More file actions
105 lines (99 loc) · 2.81 KB
/
basic.cpp
File metadata and controls
105 lines (99 loc) · 2.81 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
93
94
95
96
97
98
99
100
101
102
103
104
105
#include <api.h>
#include <nlohmann/json.hpp>
#include <tinyxml2.h>
namespace varnish = api;
static varnish::Regex regex("riscv"); // matches the word "riscv"
static void on_recv(varnish::Request req)
{
const auto url = req.url();
static constexpr bool USE_FORMATTED = false;
if constexpr (USE_FORMATTED) {
req.appendf("X-Hello: url={}", url);
} else {
req.append("X-Hello: url=" + url);
}
const bool match = regex.match(url) > 0;
if constexpr (USE_FORMATTED) {
req.appendf("X-Match: {}", match ? "true" : "false");
} else {
req.append("X-Match: " + std::string(match ? "true" : "false"));
}
//varnish::hash_data(url);
forge(varnish::Cached, [] (varnish::Request bereq, varnish::Response beresp, void* body_ptr, size_t body_size) -> varnish::response {
beresp.append(bereq["X-Tenant"]);
if constexpr (false) {
// Create a simple JSON response
nlohmann::json json;
json["message"] = "Hello RISC-V World!";
return varnish::response{200, "application/json", json.dump()};
}
// Create simple XML response
tinyxml2::XMLDocument doc;
auto* root = doc.NewElement("response");
if (body_size == 0)
{
doc.InsertFirstChild(root);
auto* msg = doc.NewElement("message");
msg->SetText("Hello RISC-V World!");
root->InsertEndChild(msg);
}
else
{
// Dash XML example from POST body
doc.Parse(static_cast<const char*>(body_ptr), body_size);
if (doc.Error())
{
doc.Clear();
doc.InsertFirstChild(root);
auto* err = doc.NewElement("error");
err->SetText("Failed to parse XML");
root->InsertEndChild(err);
}
else
{
// Modify representation1 video bandwidth
auto* mpd = doc.FirstChildElement("MPD");;
if (mpd)
{
auto* period = mpd->FirstChildElement("Period");
if (period)
{
auto* adapSet = period->FirstChildElement("AdaptationSet");
if (adapSet)
{
auto* representation = adapSet->FirstChildElement("Representation");
while (representation)
{
const char* id = representation->Attribute("id");
if (id && std::string(id) == "video1")
{
representation->SetAttribute("bandwidth", "12345678");
break;
}
representation = representation->NextSiblingElement("Representation");
}
}
}
}
}
}
tinyxml2::XMLPrinter printer;
doc.Print(&printer);
return varnish::response{200, "application/xml", printer.CStr()};
});
}
static void on_deliver(varnish::Request req, varnish::Response resp)
{
resp.append("X-Goodbye: RISCV");
resp.append(req["X-Hello"]);
resp.append(req["X-Match"]);
}
int main(int, char** argv)
{
varnish::print("{} main entered{}\n{}\n",
varnish::is_storage() ? "Storage" : "Request",
(varnish::is_debug() ? " (debug)" : ""),
argv[1]);
varnish::set_on_deliver(on_deliver);
varnish::wait_for_requests(on_recv);
}