/* * This file is a part of Winix * and is distributed under the 2-Clause BSD licence. * Author: Tomasz Sowa */ /* * Copyright (c) 2022, Tomasz Sowa * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * 1. Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. * */ #ifndef headerfile_winix_core_acceptparser #define headerfile_winix_core_acceptparser #include "utils/acceptbaseparser.h" #include "log.h" #include "header.h" namespace Winix { class AcceptParser : public AcceptBaseParser { public: static constexpr size_t MAX_CONTAINER_LENGTH = 16; /* * IMPROVEME add support for something like "text/html;level=1" (skip the level part) * * https://developer.mozilla.org/en-US/docs/Glossary/Quality_values * Some syntax, like the one of Accept, allow additional specifiers like text/html;level=1. * These increase the specificity of the value. Their use is extremely rare. * * */ void Parse(const wchar_t * str, std::vector & header_values, bool clear_header_values = true) { if( clear_header_values ) header_values.clear(); this->header_values = &header_values; AcceptBaseParser::parse(str); std::sort(header_values.begin(), header_values.end(), [](HeaderValue & h1, HeaderValue & h2) -> bool { return h1.weight > h2.weight; }); PutToLog(header_values); } void Parse(const std::wstring & str, std::vector & header_values, bool clear_header_values = true) { Parse(str.c_str(), header_values, clear_header_values); } private: std::vector * header_values; void Param(const std::wstring & param, double q) { if( header_values->size() < MAX_CONTAINER_LENGTH && q > 0.0 ) { if( q > 1.0 ) q = 1.0; header_values->resize(header_values->size() + 1); header_values->back().value = param; header_values->back().weight = q; } } void PutToLog(std::vector & header_values) { if( !header_values.empty() ) { log << log3 << "AP: " << Header::accept << " header consists of: "; HeaderValue::log_values(header_values, log); log << logend; } else { log << log3 << "AP: there is no " << Header::accept << " header" << logend; } } }; } // namespace Winix #endif