improve AcceptBaseParser parsing algorithm

remove AcceptParser - not needed now, AcceptBaseParser can prepare a table now
This commit is contained in:
2022-02-02 17:58:27 +01:00
parent d0d2cfb22c
commit 75daf37bbd
10 changed files with 230 additions and 207 deletions

View File

@@ -145,7 +145,8 @@ app.o: ../../../winix/winixd/templates/indexpatterns.h
app.o: ../../../winix/winixd/templates/patterns.h
app.o: ../../../winix/winixd/templates/changepatterns.h compress.h
app.o: postparser.h httpsimpleparser.h cookieparser.h postmultiparser.h
app.o: acceptencodingparser.h acceptparser.h winixrequest.h
app.o: acceptencodingparser.h ../../../winix/winixd/utils/acceptbaseparser.h
app.o: ../../../winix/winixd/core/header.h winixrequest.h
app.o: ../../../winix/winixd/models/migration.h
basethread.o: basethread.h synchro.h winixmodeldeprecated.h
basethread.o: ../../../winix/winixd/core/winixbase.h

View File

@@ -5,7 +5,7 @@
*/
/*
* Copyright (c) 2008-2014, Tomasz Sowa
* Copyright (c) 2008-2022, Tomasz Sowa
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@@ -60,7 +60,7 @@ public:
}
void ParseAndLog(const wchar_t * str)
void ParseAndLog(const wchar_t * str, Log & log)
{
parse(str);
@@ -84,9 +84,9 @@ public:
}
void ParseAndLog(const std::wstring & str)
void ParseAndLog(const std::wstring & str, Log & log)
{
ParseAndLog(str.c_str());
ParseAndLog(str.c_str(), log);
}
@@ -99,7 +99,7 @@ private:
}
void Param(const std::wstring & param, double q)
void parsed_name_q(const std::wstring & param, double q)
{
if( param == L"deflate" && q!=0.0 )
{

View File

@@ -1,124 +0,0 @@
/*
* This file is a part of Winix
* and is distributed under the 2-Clause BSD licence.
* Author: Tomasz Sowa <t.sowa@ttmath.org>
*/
/*
* 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<HeaderValue> & 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<HeaderValue> & header_values, bool clear_header_values = true)
{
Parse(str.c_str(), header_values, clear_header_values);
}
private:
std::vector<HeaderValue> * 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<HeaderValue> & 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

View File

@@ -385,9 +385,6 @@ bool App::Init()
cookie_parser.set_dependency(&winix_model);
accept_encoding_parser.set_dependency(&winix_base);
accept_parser.set_dependency(&winix_base);
plugin.Call((Session*)0, WINIX_PLUGIN_INIT);
return true;
@@ -935,6 +932,23 @@ void App::LogEnvironmentHTTPVariables()
}
void App::ParseAcceptHeader()
{
accept_base_parser.parse(cur.request->env_http_accept, cur.request->accept_mime_types, 16);
if( !cur.request->accept_mime_types.empty() )
{
log << log3 << "App: " << Winix::Header::accept << " header consists of: ";
HeaderValue::log_values(cur.request->accept_mime_types, log);
log << logend;
}
else
{
log << log3 << "App: there is no " << Winix::Header::accept << " header" << logend;
}
}
/*
* reading the request (without GET parameters in the URL)
@@ -952,16 +966,16 @@ void App::ReadRequest()
ReadPostVars();
cookie_parser.Parse(cur.request->env_http_cookie, cur.request->cookie_tab);
accept_encoding_parser.ParseAndLog(cur.request->env_http_accept_encoding);
accept_parser.Parse(cur.request->env_http_accept, cur.request->accept_mime_types);
if( config.log_env_variables )
LogEnvironmentVariables();
if( config.log_env_http_variables )
LogEnvironmentHTTPVariables();
ParseAcceptHeader();
accept_encoding_parser.ParseAndLog(cur.request->env_http_accept_encoding, log);
cookie_parser.Parse(cur.request->env_http_cookie, cur.request->cookie_tab);
CheckIE();
CheckKonqueror();
CheckHtmx();

View File

@@ -140,8 +140,8 @@ private:
std::string post_buffer;
CookieParser cookie_parser;
AcceptBaseParser accept_base_parser;
AcceptEncodingParser accept_encoding_parser;
AcceptParser accept_parser;
Compress compress;
FCGX_Request fcgi_request;
int fcgi_socket;
@@ -240,6 +240,7 @@ private:
void LogEnvironmentVariables();
void LogEnvironmentHTTPVariables();
void ParseAcceptHeader();
void SetEnv(const char * name, std::wstring & env);
void ReadEnvVariables();