winix/winixd/core/request.cpp

512 lines
9.3 KiB
C++

/*
* 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) 2008-2021, 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.
*
*/
#include "request.h"
#include "log.h"
#include "plugin.h"
#include "misc.h"
#include "functions/functionbase.h"
namespace Winix
{
Request::Request()
{
id = 0;
config = 0;
}
void Request::fields()
{
field(L"", L"dirs", dir_tab);
field(L"", L"is_item", is_item);
field(L"", L"current_dir", &Request::current_dir);
field(L"", L"last_item", &Request::last_item_wrapper);
}
void Request::SetConfig(Config * pconfig)
{
config = pconfig;
}
void Request::ClearOutputStreams()
{
size_t len = 0;
out_main_stream.clear();
if( config )
len = config->ezc_out_streams_size;
/*
* clearing buffers and setting 'escape' flag to true
* for all streams which were used in the map
*/
out_streams.ClearMap();
out_streams.ResizeTab(len);
}
void Request::Clear()
{
// id is never 0
if( ++id == 0 )
++id;
RemovePostFileTmp(post_file_tab);
ClearOutputStreams();
if( function )
function->Clear();
post_tab.clear();
post_file_tab.clear();
cookie_tab.clear();
post_in.clear();
is_postin_used = false;
method = unknown_method;
headers_in.clear();
out_headers.clear();
out_cookies.clear();
env_request_method.clear();
env_request_uri.clear();
env_http_cookie.clear();
env_http_host.clear();
env_http_user_agent.clear();
env_http_accept_encoding.clear();
env_fcgi_role.clear();
env_content_type.clear();
env_https.clear();
item_tab.clear();
item.Clear();
item.set_connector(nullptr);
dir_tab.clear();
last_item = &item;
is_item = false;
function = nullptr;
param_tab.clear();
anchor.clear();
answer_source = AnswerSource::answer_models;
answer_container = AnswerContainer::answer_text;
use_ezc_engine = true;
frame.clear();
send_all_frames = false;
status = WINIX_ERR_OK;
browser_msie = false;
redirect_to.clear();
redirect_type = 303;
x_sendfile.clear();
send_as_attachment = false;
using_ssl = false;
is_htmx_request = false;
start_time = 0;
start_date.Clear();
timespec_req_start.tv_sec = 0;
timespec_req_start.tv_nsec = 0;
timespec_req_stop.tv_sec = 0;
timespec_req_stop.tv_nsec = 0;
timespec_req_diff.tv_sec = 0;
timespec_req_diff.tv_nsec = 0;
timespec_ezc_engine_start.tv_sec = 0;
timespec_ezc_engine_start.tv_nsec = 0;
timespec_ezc_engine_stop.tv_sec = 0;
timespec_ezc_engine_stop.tv_nsec = 0;
subdomain.clear();
models.Clear();
out_bin_stream.clear();
gen_trim_white = false;
gen_skip_new_line = false;
gen_use_special_chars = false;
ip = 0;
ip_str.clear();
use_200_status_for_not_found_and_permission_denied = false;
html_template.clear();
use_html_filter = false;
}
void Request::RequestStarts()
{
// clearing it is better to use at the end of a request
// so starting is much faster
clock_gettime(CLOCK_REALTIME, &timespec_req_start);
timespec_req_stop = timespec_req_start;
start_time = timespec_req_start.tv_sec;
start_date = start_time;
}
void Request::RequestEnds()
{
clock_gettime(CLOCK_REALTIME, &timespec_req_stop);
calculate_timespec_diff(timespec_req_start, timespec_req_stop, timespec_req_diff);
}
bool Request::PrepareAnswerType()
{
answer_source = AnswerSource::answer_models;
answer_container = AnswerContainer::answer_text;
use_ezc_engine = true;
frame = ParamValue(config->request_frame_parameter);
send_all_frames = (ParamValuep(config->request_all_frames_parameter) != nullptr);
use_html_filter = config->html_filter;
bool ok = true;
// IMPLEMENT ME add checking for Accept header;
ok = ok && CheckContainerParameter();
ok = ok && CheckAnswerParameter();
return ok;
}
// IMPROVE ME give me a better name
bool Request::CheckContainerParameter()
{
std::wstring * container = ParamValuep(L"container");
if( container )
{
// IMPROVEME do a plugin call here
// if a plugin can consume this then don't check text/json/xml/csv and just return true
if( *container == L"text" )
{
answer_container = Request::AnswerContainer::answer_text;
}
else
if( *container == L"json" )
{
answer_container = Request::AnswerContainer::answer_json;
}
else
if( *container == L"xml" )
{
answer_container = Request::AnswerContainer::answer_xml;
}
else
if( *container == L"csv" )
{
answer_container = Request::AnswerContainer::answer_csv;
}
else
{
Log * log = get_logger();
if( log )
{
(*log) << log2 << "App: an unknown 'container' parameter: " << *container << logend;
// IMPROVE ME set status to 404
return false;
}
}
}
return true;
}
// IMPROVE ME give me a better name
bool Request::CheckAnswerParameter()
{
std::wstring * answer = ParamValuep(L"answer");
if( answer )
{
// IMPROVEME do a plugin call here
// if a plugin can consume this then don't check html/data and just return true
if( *answer == L"html" )
{
use_ezc_engine = true;
}
else
if( *answer == L"data" )
{
use_ezc_engine = false;
}
else
{
Log * log = get_logger();
if( log )
{
(*log) << log2 << "App: an unknown 'answer' parameter: " << *answer << logend;
// IMPROVE ME set status to 404
return false;
}
}
}
return true;
}
bool Request::IsPostVar(const wchar_t * var)
{
PostTab::iterator p;
p = post_tab.find(var);
if( p == post_tab.end() )
return false;
return true;
}
bool Request::IsPostVar(const std::wstring & var)
{
PostTab::iterator p;
p = post_tab.find(var);
if( p == post_tab.end() )
return false;
return true;
}
const std::wstring & Request::PostVar(const wchar_t * var)
{
PostTab::iterator p = post_tab.find(var);
if( p == post_tab.end() )
return str_empty;
return p->second;
}
const std::wstring & Request::PostVar(const std::wstring & var)
{
PostTab::iterator p = post_tab.find(var);
if( p == post_tab.end() )
return str_empty;
return p->second;
}
bool Request::PostVar(const wchar_t * var, std::wstring & result)
{
PostTab::iterator p = post_tab.find(var);
if( p == post_tab.end() )
{
result.clear();
return false;
}
result = p->second;
return true;
}
bool Request::PostVar(const std::wstring & var, std::wstring & result)
{
PostTab::iterator p = post_tab.find(var);
if( p == post_tab.end() )
{
result.clear();
return false;
}
result = p->second;
return true;
}
std::wstring * Request::PostVarp(const wchar_t * var)
{
PostTab::iterator p = post_tab.find(var);
if( p == post_tab.end() )
return 0;
return &p->second;
}
std::wstring * Request::PostVarp(const std::wstring & var)
{
PostTab::iterator p = post_tab.find(var);
if( p == post_tab.end() )
return 0;
return &p->second;
}
bool Request::AllPostVarEmpty()
{
PostTab::iterator i;
for(i=post_tab.begin() ; i!=post_tab.end() ; ++i)
if( !i->second.empty() )
return false;
return true;
}
std::wstring * Request::ParamValuep(const wchar_t * param_name)
{
ParamTab::iterator i;
for(i=param_tab.begin() ; i!=param_tab.end() ; ++i)
{
if( i->name == param_name )
return &i->value;
}
return nullptr;
}
std::wstring * Request::ParamValuep(const std::wstring & param_name)
{
ParamTab::iterator i;
for(i=param_tab.begin() ; i!=param_tab.end() ; ++i)
{
if( i->name == param_name )
return &i->value;
}
return nullptr;
}
bool Request::IsParam(const wchar_t * param_name)
{
return ParamValuep(param_name) != nullptr;
}
bool Request::IsParam(const std::wstring & param_name)
{
return ParamValuep(param_name) != nullptr;
}
const std::wstring & Request::ParamValue(const wchar_t * param_name)
{
const std::wstring * val = ParamValuep(param_name);
if( val != nullptr )
return *val;
return str_empty;
}
const std::wstring & Request::ParamValue(const std::wstring & param_name)
{
const std::wstring * val = ParamValuep(param_name);
if( val != nullptr )
return *val;
return str_empty;
}
void Request::current_dir(morm::Wrapper & wrapper)
{
wrapper.model = dir_tab.back();
}
void Request::last_item_wrapper(morm::Wrapper & wrapper)
{
wrapper.model = last_item;
}
} // namespace Winix