|
| 1 | +#include "handler.hpp" |
| 2 | + |
| 3 | +#include <vector> |
| 4 | + |
| 5 | +#include "../../common/db_helpers.hpp" |
| 6 | + |
| 7 | +#include <userver/components/component_context.hpp> |
| 8 | +#include <userver/storages/postgres/postgres.hpp> |
| 9 | + |
| 10 | +namespace userver_techempower::fortunes { |
| 11 | + |
| 12 | +namespace { |
| 13 | + |
| 14 | +struct Fortune final { |
| 15 | + int id; |
| 16 | + std::string message; |
| 17 | +}; |
| 18 | + |
| 19 | +constexpr std::string_view kResultingHtmlHeader{ |
| 20 | + "<!DOCTYPE " |
| 21 | + "html><html><head><title>Fortunes</title></head><body><table><tr><th>id</" |
| 22 | + "th><th>message</th></tr>"}; |
| 23 | +constexpr std::string_view kResultingHtmlFooter{"</table></body></html>"}; |
| 24 | + |
| 25 | +constexpr std::string_view kNewRowStart{"<tr><td>"}; |
| 26 | +constexpr std::string_view kColumnsSeparator{"</td><td>"}; |
| 27 | +constexpr std::string_view kNewRowEnd{"</td></tr>"}; |
| 28 | + |
| 29 | +constexpr std::string_view kEscapedQuote{"""}; |
| 30 | +constexpr std::string_view kEscapedAmpersand{"&"}; |
| 31 | +constexpr std::string_view kEscapedLessThanSign{"<"}; |
| 32 | +constexpr std::string_view kEscapedMoreThanSign{">"}; |
| 33 | + |
| 34 | +void AppendFortune(std::string& result, const Fortune& fortune) { |
| 35 | + { |
| 36 | + auto old_size = result.size(); |
| 37 | + const auto fortune_id = std::to_string(fortune.id); |
| 38 | + |
| 39 | + const auto first_step_size = |
| 40 | + kNewRowStart.size() + fortune_id.size() + kColumnsSeparator.size(); |
| 41 | + |
| 42 | + result.resize(old_size + first_step_size); |
| 43 | + char* append_position = result.data() + old_size; |
| 44 | + |
| 45 | + // this is just faster than std::string::append if we know the resulting |
| 46 | + // size upfront, because there are a lot of not inlined calls otherwise |
| 47 | + const auto append = [&append_position](std::string_view what) { |
| 48 | + std::memcpy(append_position, what.data(), what.size()); |
| 49 | + append_position += what.size(); |
| 50 | + }; |
| 51 | + append(kNewRowStart); |
| 52 | + append(fortune_id); |
| 53 | + append(kColumnsSeparator); |
| 54 | + } |
| 55 | + |
| 56 | + { |
| 57 | + std::string_view message{fortune.message}; |
| 58 | + |
| 59 | + const auto do_append = [&result](std::string_view unescaped, |
| 60 | + std::string_view escaped) { |
| 61 | + const auto old_size = result.size(); |
| 62 | + const auto added_size = unescaped.size() + escaped.size(); |
| 63 | + |
| 64 | + result.resize(result.size() + added_size); |
| 65 | + char* append_position = result.data() + old_size; |
| 66 | + if (!unescaped.empty()) { |
| 67 | + std::memcpy(append_position, unescaped.data(), unescaped.size()); |
| 68 | + append_position += unescaped.size(); |
| 69 | + } |
| 70 | + std::memcpy(append_position, escaped.data(), escaped.size()); |
| 71 | + }; |
| 72 | + |
| 73 | + std::size_t unescaped_len = 0; |
| 74 | + const auto append = [&unescaped_len, &message, |
| 75 | + &do_append](std::string_view escaped) { |
| 76 | + do_append(message.substr(0, unescaped_len), escaped); |
| 77 | + message = message.substr(std::exchange(unescaped_len, 0) + 1); |
| 78 | + }; |
| 79 | + |
| 80 | + while (unescaped_len < message.size()) { |
| 81 | + const auto c = message[unescaped_len]; |
| 82 | + switch (c) { |
| 83 | + case '"': { |
| 84 | + append(kEscapedQuote); |
| 85 | + break; |
| 86 | + } |
| 87 | + case '&': { |
| 88 | + append(kEscapedAmpersand); |
| 89 | + break; |
| 90 | + } |
| 91 | + case '<': { |
| 92 | + append(kEscapedLessThanSign); |
| 93 | + break; |
| 94 | + } |
| 95 | + case '>': { |
| 96 | + append(kEscapedMoreThanSign); |
| 97 | + break; |
| 98 | + } |
| 99 | + default: |
| 100 | + ++unescaped_len; |
| 101 | + } |
| 102 | + } |
| 103 | + result.append(message); |
| 104 | + } |
| 105 | + |
| 106 | + { result.append(kNewRowEnd); } |
| 107 | +} |
| 108 | + |
| 109 | +std::string FormatFortunes(const std::vector<Fortune>& fortunes) { |
| 110 | + std::string result{}; |
| 111 | + // Wild guess, seems reasonable. Could be the exact value needed, but that |
| 112 | + // looks kinda cheating. |
| 113 | + result.reserve(2048); |
| 114 | + |
| 115 | + result.append(kResultingHtmlHeader); |
| 116 | + for (const auto& fortune : fortunes) { |
| 117 | + AppendFortune(result, fortune); |
| 118 | + } |
| 119 | + result.append(kResultingHtmlFooter); |
| 120 | + |
| 121 | + return result; |
| 122 | +} |
| 123 | + |
| 124 | +} // namespace |
| 125 | + |
| 126 | +Handler::Handler(const userver::components::ComponentConfig& config, |
| 127 | + const userver::components::ComponentContext& context) |
| 128 | + : userver::server::handlers::HttpHandlerBase{config, context}, |
| 129 | + pg_{context |
| 130 | + .FindComponent<userver::components::Postgres>( |
| 131 | + db_helpers::kDbComponentName) |
| 132 | + .GetCluster()}, |
| 133 | + select_all_fortunes_query_{"SELECT id, message FROM Fortune"} {} |
| 134 | + |
| 135 | +std::string Handler::HandleRequestThrow( |
| 136 | + const userver::server::http::HttpRequest& request, |
| 137 | + userver::server::request::RequestContext&) const { |
| 138 | + request.GetHttpResponse().SetContentType("text/html; charset=utf-8"); |
| 139 | + return GetResponse(); |
| 140 | +} |
| 141 | + |
| 142 | +std::string Handler::GetResponse() const { |
| 143 | + auto fortunes = |
| 144 | + pg_->Execute(db_helpers::kClusterHostType, select_all_fortunes_query_) |
| 145 | + .AsContainer<std::vector<Fortune>>( |
| 146 | + userver::storages::postgres::kRowTag); |
| 147 | + |
| 148 | + fortunes.push_back({0, "Additional fortune added at request time."}); |
| 149 | + |
| 150 | + std::sort(fortunes.begin(), fortunes.end(), |
| 151 | + [](const auto& lhs, const auto& rhs) { |
| 152 | + return lhs.message < rhs.message; |
| 153 | + }); |
| 154 | + |
| 155 | + return FormatFortunes(fortunes); |
| 156 | +} |
| 157 | + |
| 158 | +} // namespace userver_techempower::fortunes |
0 commit comments