2009-10-08 19:59:10 +02:00
|
|
|
/*
|
2010-02-28 01:08:10 +01:00
|
|
|
* This file is a part of Winix
|
2014-10-04 20:04:03 +02:00
|
|
|
* and is distributed under the 2-Clause BSD licence.
|
|
|
|
* Author: Tomasz Sowa <t.sowa@ttmath.org>
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2022-02-02 17:58:27 +01:00
|
|
|
* Copyright (c) 2008-2022, Tomasz Sowa
|
2009-10-08 19:59:10 +02:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
2014-10-04 20:04:03 +02:00
|
|
|
* 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.
|
|
|
|
*
|
2009-10-08 19:59:10 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "acceptbaseparser.h"
|
2022-02-02 17:58:27 +01:00
|
|
|
#include "convert/convert.h"
|
2009-10-08 19:59:10 +02:00
|
|
|
|
|
|
|
|
2014-02-12 17:30:49 +01:00
|
|
|
namespace Winix
|
|
|
|
{
|
|
|
|
|
|
|
|
|
2022-02-02 17:58:27 +01:00
|
|
|
AcceptBaseParser::AcceptBaseParser()
|
2009-10-08 19:59:10 +02:00
|
|
|
{
|
2022-02-02 17:58:27 +01:00
|
|
|
}
|
2009-10-08 19:59:10 +02:00
|
|
|
|
2022-02-02 17:58:27 +01:00
|
|
|
AcceptBaseParser::~AcceptBaseParser()
|
|
|
|
{
|
2009-10-08 19:59:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-02-02 17:58:27 +01:00
|
|
|
|
|
|
|
bool AcceptBaseParser::is_delimiter(wchar_t c, wchar_t delimiter)
|
2009-10-08 19:59:10 +02:00
|
|
|
{
|
2022-02-02 17:58:27 +01:00
|
|
|
return delimiter != 0 && delimiter == c;
|
2009-10-08 19:59:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-02-02 17:58:27 +01:00
|
|
|
bool AcceptBaseParser::is_delimiter(wchar_t c, wchar_t delimiter1, wchar_t delimiter2, wchar_t delimiter3)
|
|
|
|
{
|
|
|
|
return is_delimiter(c, delimiter1) || is_delimiter(c, delimiter2) || is_delimiter(c, delimiter3);
|
|
|
|
}
|
2009-10-08 19:59:10 +02:00
|
|
|
|
|
|
|
|
2022-02-02 17:58:27 +01:00
|
|
|
void AcceptBaseParser::read_token(std::wstring & token, size_t max_len, wchar_t delimiter1, wchar_t delimiter2, wchar_t delimiter3)
|
2009-10-08 19:59:10 +02:00
|
|
|
{
|
2022-02-02 17:58:27 +01:00
|
|
|
token.clear();
|
|
|
|
text = pt::skip_white(text, false, false);
|
2009-10-08 19:59:10 +02:00
|
|
|
|
2022-02-02 17:58:27 +01:00
|
|
|
while( *text!=0 && !is_delimiter(*text, delimiter1, delimiter2, delimiter3) )
|
2009-10-08 19:59:10 +02:00
|
|
|
{
|
2022-02-02 17:58:27 +01:00
|
|
|
if( token.size() < max_len )
|
|
|
|
{
|
|
|
|
token += *text;
|
|
|
|
}
|
|
|
|
|
2009-10-08 19:59:10 +02:00
|
|
|
++text;
|
|
|
|
}
|
|
|
|
|
2022-02-02 17:58:27 +01:00
|
|
|
pt::trim_white(token);
|
2009-10-08 19:59:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-02-02 17:58:27 +01:00
|
|
|
void AcceptBaseParser::read_name()
|
2009-10-08 19:59:10 +02:00
|
|
|
{
|
2022-02-02 17:58:27 +01:00
|
|
|
read_token(name, MAX_NAME_LENGTH, ',', ';');
|
|
|
|
|
|
|
|
if( !name.empty() )
|
|
|
|
{
|
|
|
|
parsed_name(name);
|
|
|
|
}
|
|
|
|
}
|
2009-10-08 19:59:10 +02:00
|
|
|
|
|
|
|
|
2022-02-02 17:58:27 +01:00
|
|
|
void AcceptBaseParser::read_parameter()
|
|
|
|
{
|
|
|
|
param_value.clear();
|
|
|
|
read_token(param, MAX_PARAM_LENGTH, '=', ',', ';');
|
2009-10-08 19:59:10 +02:00
|
|
|
|
2022-02-02 17:58:27 +01:00
|
|
|
if( *text == '=' )
|
|
|
|
{
|
2009-10-08 19:59:10 +02:00
|
|
|
++text;
|
2022-02-02 17:58:27 +01:00
|
|
|
read_token(param_value, MAX_PARAM_VALUE_LENGTH, ';', ',');
|
|
|
|
}
|
2009-10-08 19:59:10 +02:00
|
|
|
|
2022-02-02 17:58:27 +01:00
|
|
|
if( !name.empty() && !param.empty() )
|
|
|
|
{
|
|
|
|
parsed_param(param, param_value);
|
|
|
|
}
|
2009-10-08 19:59:10 +02:00
|
|
|
|
2022-02-02 17:58:27 +01:00
|
|
|
if( param == L"q" && !param_value.empty() )
|
|
|
|
{
|
|
|
|
q = pt::to_double(param_value);
|
|
|
|
}
|
2009-10-08 19:59:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-02-02 17:58:27 +01:00
|
|
|
void AcceptBaseParser::read_loop(std::vector<HeaderValue> * header_values, size_t max_len)
|
2009-10-08 19:59:10 +02:00
|
|
|
{
|
2022-02-02 17:58:27 +01:00
|
|
|
while( *text != 0 )
|
|
|
|
{
|
|
|
|
q = 1.0;
|
|
|
|
read_name();
|
|
|
|
|
|
|
|
while( *text == ';' )
|
|
|
|
{
|
|
|
|
++text;
|
|
|
|
read_parameter();
|
|
|
|
}
|
|
|
|
|
|
|
|
if( !name.empty() && q > 0.0 )
|
|
|
|
{
|
|
|
|
if( q > 1.0 )
|
|
|
|
q = 1.0;
|
|
|
|
|
|
|
|
parsed_name_q(name, q);
|
|
|
|
|
|
|
|
if( header_values && (max_len == 0 || header_values->size() < max_len) )
|
|
|
|
{
|
|
|
|
header_values->resize(header_values->size() + 1);
|
|
|
|
header_values->back().value = name;
|
|
|
|
header_values->back().weight = q;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if( *text != 0 )
|
|
|
|
++text;
|
|
|
|
}
|
2009-10-08 19:59:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-02-02 17:58:27 +01:00
|
|
|
void AcceptBaseParser::read(const wchar_t * str, std::vector<HeaderValue> * header_values, size_t max_len)
|
2009-10-08 19:59:10 +02:00
|
|
|
{
|
|
|
|
text = str;
|
2022-02-02 17:11:46 +01:00
|
|
|
init();
|
2022-02-02 17:58:27 +01:00
|
|
|
|
|
|
|
read_loop(header_values, max_len);
|
|
|
|
|
|
|
|
name.clear();
|
|
|
|
param.clear();
|
|
|
|
param_value.clear();
|
2009-10-08 19:59:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-02-02 17:58:27 +01:00
|
|
|
void AcceptBaseParser::parse(const wchar_t * str)
|
|
|
|
{
|
|
|
|
read(str, nullptr, 0);
|
|
|
|
}
|
|
|
|
|
2014-02-12 17:30:49 +01:00
|
|
|
|
2022-02-02 17:11:46 +01:00
|
|
|
void AcceptBaseParser::parse(const std::wstring & str)
|
2014-10-09 22:44:56 +02:00
|
|
|
{
|
2022-02-02 17:11:46 +01:00
|
|
|
parse(str.c_str());
|
2014-10-09 22:44:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-02-02 17:58:27 +01:00
|
|
|
void AcceptBaseParser::parse(const wchar_t * str, std::vector<HeaderValue> & header_values, size_t max_len, bool clear_header_values)
|
|
|
|
{
|
|
|
|
if( clear_header_values )
|
|
|
|
header_values.clear();
|
|
|
|
|
|
|
|
AcceptBaseParser::read(str, &header_values, max_len);
|
|
|
|
|
|
|
|
std::sort(header_values.begin(), header_values.end(), [](HeaderValue & h1, HeaderValue & h2) -> bool {
|
|
|
|
return h1.weight > h2.weight;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void AcceptBaseParser::parse(const std::wstring & str, std::vector<HeaderValue> & header_values, size_t max_len, bool clear_header_values)
|
|
|
|
{
|
|
|
|
return parse(str.c_str(), header_values, max_len, clear_header_values);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-02-12 17:30:49 +01:00
|
|
|
|
|
|
|
} // namespace Winix
|
|
|
|
|