winix/winixd/core/request.cpp

405 lines
7.2 KiB
C++
Raw Normal View History

/*
* 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"dirs", dir_tab);
field(L"is_item", is_item);
field(L"current_dir", &Request::current_dir);
}
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;
removed: Request::debug all stream used for debugging info some environment variables were put there removed: config variable: debug_info removed: Request::role (responder, authorizer) now we have only one role: responder added: new config variables: log_env_variables (default false) - when true then fastcgi environment variables are logged to the log file log_http_answer_headers (default false) - when true all http headers created by winix ale logged (note that the www server can add/adjust other headers) changed: some refactoring in Request struct changed: CookieTab to std::map<std::wstring, std::wstring> beforehand std::string was used (changed CookieParser as well) changed: Request::SetCookie() to AddCookie() added: Request::out_headers (a PT::Space struct) http headers (without cookies) send back to the client added: Request::out_cookies (a PT::Space struct) cookies send to the client changed: App class to use Request::out_headers and Request::out_cookies some SendHeaders...() methods were renamed to PrepareHeaders...() and they create output in Request::out_headers first (and out_cookies) and later it is sent added: two plugin messages: // http headers (without cookies) were created and are ready to send // here you can make some changes to them // in p1 you have a pointer to the PT::Space (Request::out_headers) #define WINIX_PREPARE_TO_SEND_HTTP_HEADERS 31070 // http cookies were created and are ready to send // here you can make some changes to them // in p1 you have a pointer to the PT::Space (Request::out_cookies) #define WINIX_PREPARE_TO_SEND_HTTP_COOKIES 31080 added: config variable: // how many output streams do we have in Request class // default: 16 (64 maximum) size_t ezc_out_streams_size; git-svn-id: svn://ttmath.org/publicrep/winix/trunk@940 e52654a7-88a9-db11-a3e9-0013d4bc506e
2013-11-29 22:39:23 +01:00
/*
* clearing buffers and setting 'escape' flag to true
* for all streams which were used in the map
*/
out_streams.ClearMap();
out_streams.ResizeTab(len);
out_main_stream_use_html_filter = true;
out_streams_use_html_filter = true;
}
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;
removed: Request::debug all stream used for debugging info some environment variables were put there removed: config variable: debug_info removed: Request::role (responder, authorizer) now we have only one role: responder added: new config variables: log_env_variables (default false) - when true then fastcgi environment variables are logged to the log file log_http_answer_headers (default false) - when true all http headers created by winix ale logged (note that the www server can add/adjust other headers) changed: some refactoring in Request struct changed: CookieTab to std::map<std::wstring, std::wstring> beforehand std::string was used (changed CookieParser as well) changed: Request::SetCookie() to AddCookie() added: Request::out_headers (a PT::Space struct) http headers (without cookies) send back to the client added: Request::out_cookies (a PT::Space struct) cookies send to the client changed: App class to use Request::out_headers and Request::out_cookies some SendHeaders...() methods were renamed to PrepareHeaders...() and they create output in Request::out_headers first (and out_cookies) and later it is sent added: two plugin messages: // http headers (without cookies) were created and are ready to send // here you can make some changes to them // in p1 you have a pointer to the PT::Space (Request::out_headers) #define WINIX_PREPARE_TO_SEND_HTTP_HEADERS 31070 // http cookies were created and are ready to send // here you can make some changes to them // in p1 you have a pointer to the PT::Space (Request::out_cookies) #define WINIX_PREPARE_TO_SEND_HTTP_COOKIES 31080 added: config variable: // how many output streams do we have in Request class // default: 16 (64 maximum) size_t ezc_out_streams_size; git-svn-id: svn://ttmath.org/publicrep/winix/trunk@940 e52654a7-88a9-db11-a3e9-0013d4bc506e
2013-11-29 22:39:23 +01:00
method = unknown_method;
headers_in.clear();
out_headers.clear();
out_cookies.clear();
page_generated = false;
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();
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();
return_info_only = false;
info.clear();
return_json = false;
models.Clear();
out_bin_stream.clear();
send_bin_stream = false;
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();
}
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::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::ModelWrapper ** model_wrapper)
{
*model_wrapper = new morm::ModelWrapperModel(dir_tab.back());
}
} // namespace Winix