winix/winixd/functions/functionbase.cpp

346 lines
7.1 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) 2010-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.
*
*/
#include "functionbase.h"
#include "functions.h"
#include "templates/templates.h"
namespace Winix
{
FunctionBase::FunctionBase()
{
follow_symlinks = true;
template_index = size_t(-1);
need_ssl = false;
need_session = true;
register_default_models = true;
post_max_object_items = 0;
post_max_table_items = 0;
post_max_all_items = 0;
post_max_nested_objects = 0;
fun.item_content.user_id = -1;
fun.item_content.group_id = -1;
fun.item_content.privileges = 07555;
fun.parent_id = -1;
fun.id = -1;
fun.type = Item::file;
db = nullptr;
functions = nullptr;
templates = nullptr;
}
FunctionBase::~FunctionBase()
{
}
//void FunctionBase::SetConfig(Config * pconfig)
//{
// config = pconfig;
//}
//void FunctionBase::SetCur(Cur * pcur)
//{
// cur = pcur;
//}
void FunctionBase::SetDb(Db * pdb)
{
db = pdb;
}
//void FunctionBase::SetSystem(System * psystem)
//{
// system = psystem;
//}
void FunctionBase::SetFunctions(Functions * pfunctions)
{
functions = pfunctions;
}
void FunctionBase::SetTemplates(Templates * ptemplates)
{
templates = ptemplates;
}
//void FunctionBase::SetSynchro(Synchro * psynchro)
//{
// synchro = psynchro;
//}
//void FunctionBase::SetSessionManager(SessionManager * pmanager)
//{
// session_manager = pmanager;
//}
void FunctionBase::Init()
{
// this method is called only once at the beginning
// when winix starts
}
void FunctionBase::Finish()
{
// this method is called only once at the end
// when winix finishes
}
bool FunctionBase::HasAccess()
{
// true by default
return true;
}
bool FunctionBase::IsCorsMethodAvailable(Request::Method method)
{
return method == Request::get || method == Request::head || method == Request::post || method == Request::put ||
method == Request::delete_ ||method == Request::patch;
}
bool FunctionBase::IsCorsOriginAvailable(const std::wstring & origin_url)
{
// true by default for all urles
return true;
}
bool FunctionBase::AreCorsHeadersAvailable(const std::wstring & headers)
{
// true by default for all headers
// headers are comma separated
return true;
}
/*
* method is the value of Access-Control-Request-Method header sent by the client
*/
void FunctionBase::AddAccessControlAllowMethodsHeader(Request::Method method)
{
cur->request->AddHeader(Header::access_control_allow_methods, L"GET, HEAD, POST, PUT, DELETE, OPTIONS, PATCH");
}
/*
* origin_url is the value of Origin header sent by the client
*/
void FunctionBase::AddAccessControlAllowOriginHeader(const std::wstring & origin_url)
{
cur->request->AddHeader(Header::access_control_allow_origin, origin_url);
}
/*
* headers is the value of Access-Control-Request-Headers header sent by the client
*/
void FunctionBase::AddAccessControlAllowHeadersHeader(const std::wstring & headers)
{
cur->request->AddHeader(Header::access_control_allow_headers, headers);
}
void FunctionBase::AddAccessControlMaxAgeHeader()
{
// default 24 hours
cur->request->AddHeader(Header::access_control_max_age, 86400);
}
void FunctionBase::MakeGet()
{
// do nothing by default
}
void FunctionBase::MakeHead()
{
// by default call MakeGet() but do not return any content at the end of the request
MakeGet();
}
void FunctionBase::MakePost()
{
// do nothing by default
}
void FunctionBase::MakePut()
{
// do nothing by default
}
void FunctionBase::MakeDelete()
{
// do nothing by default
}
void FunctionBase::MakeConnect()
{
// do nothing by default
}
void FunctionBase::MakeOptions()
{
cur->request->http_status = Header::status_204_no_content;
pt::Space * cors_method = cur->request->headers_in.get_space_nc(L"Access_Control_Request_Method"); // FastCGI changes '-' to '_'
pt::Space * cors_headers = cur->request->headers_in.get_space_nc(L"Access_Control_Request_Headers");
pt::Space * cors_origin = cur->request->headers_in.get_space_nc(L"Origin");
if( cors_method && cors_origin && cors_method->is_wstr() && cors_origin->is_wstr() )
{
/*
* this is a preflight request
* https://developer.mozilla.org/en-US/docs/Glossary/Preflight_request
* (we allow Access-Control-Request-Headers not to be present)
*/
Request::Method method = Request::CheckRequestMethod(cors_method->get_wstr()->c_str());
if( IsCorsMethodAvailable(method) && IsCorsOriginAvailable(*cors_origin->get_wstr()) )
{
bool cors_available = true;
if( cors_headers && cors_headers->is_wstr() )
{
cors_available = AreCorsHeadersAvailable(*cors_headers->get_wstr());
}
if( cors_available )
{
AddAccessControlAllowMethodsHeader(method);
AddAccessControlAllowOriginHeader(*cors_origin->get_wstr());
AddAccessControlMaxAgeHeader();
if( cors_headers && cors_headers->is_wstr() )
{
AddAccessControlAllowHeadersHeader(*cors_headers->get_wstr());
}
}
}
}
else
{
cur->request->out_headers.add(Header::allow, L"GET, HEAD, POST, PUT, DELETE, OPTIONS, PATCH");
}
}
void FunctionBase::MakeTrace()
{
// do nothing by default
}
void FunctionBase::MakePatch()
{
// do nothing by default
}
void FunctionBase::Clear()
{
// do nothing by default
}
void FunctionBase::ContinueMakeGet()
{
// do nothing by default
}
void FunctionBase::ContinueMakeHead()
{
// do nothing by default
}
void FunctionBase::ContinueMakePost()
{
// do nothing by default
}
void FunctionBase::ContinueMakePut()
{
// do nothing by default
}
void FunctionBase::ContinueMakeDelete()
{
// do nothing by default
}
void FunctionBase::ContinueMakeConnect()
{
// do nothing by default
}
void FunctionBase::ContinueMakeOptions()
{
// do nothing by default
}
void FunctionBase::ContinueMakeTrace()
{
// do nothing by default
}
void FunctionBase::ContinueMakePatch()
{
// do nothing by default
}
} // namespace Winix