change the way how winix answer is created

Now we can return ezc content and models serialized in the same json structure,
Xml and Csv are not implemented yet.
Ezc frames are returned in 'ezc_frames' field.
Main ezc stream is returned in 'main_stream' field.
Frame url parameter can take more than one frame (names separated by commas).
Honor Accept http header (AcceptParser).

Samples:
--------
http://domain.tld/dir/controller
returns html answer from the main ezc stream

http://domain.tld/dir/controller/container:raw
returns html answer from the main ezc stream (the same as above)

http://domain.tld/dir/controller/frame:abc
returns "abc" frame as html

http://domain.tld/dir/controller/container:json
returns all serialized models to json and no ezc streams

http://domain.tld/dir/controller/container:xml
returns all serialized models to xml and no ezc streams (not implemented yet)

http://domain.tld/dir/controller/container:json/frame:abc,xyz
returns all serialized models to json and two frames in 'ezc_frames' object

http://domain.tld/dir/controller/container:json/all_frames
returns all serialized models to json and all frames in 'ezc_frames' object

http://domain.tld/dir/controller/container:json/main_stream
returns all serialized models and the main ezc stream in 'main_stream' field

http://domain.tld/dir/controller/container:json/main_stream/all_frames
returns all serialized models to json, all frames and the main stream
This commit is contained in:
2022-02-01 18:44:23 +01:00
parent 227dd923d6
commit f7b5ac0dc8
17 changed files with 695 additions and 395 deletions

View File

@@ -120,6 +120,7 @@ void Request::Clear()
env_http_host.clear();
env_http_user_agent.clear();
env_http_accept_encoding.clear();
env_http_accept.clear();
env_fcgi_role.clear();
env_content_type.clear();
env_https.clear();
@@ -134,11 +135,16 @@ void Request::Clear()
param_tab.clear();
anchor.clear();
answer_source = AnswerSource::answer_models;
answer_container = AnswerContainer::answer_text;
use_ezc_engine = true;
frame.clear();
send_bin_stream = false;
send_main_stream = false;
send_all_frames = false;
send_frames.clear();
use_ezc_engine = false;
serialize_models = false;
accept_mime_types.clear();
container_type = ContainerType::container_raw;
status = WINIX_ERR_OK;
@@ -206,30 +212,81 @@ void Request::RequestEnds()
bool Request::PrepareAnswerType()
void Request::PrepareAnswerType()
{
answer_source = AnswerSource::answer_models;
answer_container = AnswerContainer::answer_text;
use_ezc_engine = true;
CheckAcceptHeader();
CheckContainerParameter();
frame = ParamValue(config->request_frame_parameter);
serialize_models = (container_type != ContainerType::container_raw);
send_all_frames = (ParamValuep(config->request_all_frames_parameter) != nullptr);
send_main_stream = (ParamValuep(config->request_main_stream_parameter) != nullptr);
use_html_filter = config->html_filter;
PrepareFrameNames();
bool ok = true;
if( container_type == ContainerType::container_raw && !send_all_frames && send_frames.empty() )
{
send_main_stream = true;
}
// IMPLEMENT ME add checking for Accept header;
ok = ok && CheckContainerParameter();
ok = ok && CheckAnswerParameter();
return ok;
use_ezc_engine = send_main_stream || send_all_frames || !send_frames.empty();
}
// IMPROVE ME give me a better name
bool Request::CheckContainerParameter()
void Request::CheckAcceptHeader()
{
if( !accept_mime_types.empty() )
{
bool found = false;
for(HeaderValue & h: accept_mime_types)
{
if( h.value == Header::text_html || h.value == Header::application_xhtml_xml ||
h.value == Header::text_all || h.value == Header::all_all)
{
container_type = ContainerType::container_raw;
found = true;
break;
}
else
if( h.value == Header::application_json || h.value == Header::application_all )
{
container_type = ContainerType::container_json;
found = true;
break;
}
else
if( h.value == Header::application_xml )
{
container_type = ContainerType::container_xml;
found = true;
break;
}
else
if( h.value == Header::text_csv )
{
container_type = ContainerType::container_csv;
found = true;
break;
}
}
if( !found )
{
Log * log = get_logger();
if( log )
{
(*log) << log2 << "App: an unknown " << Header::accept << " headers: ";
HeaderValue::log_values(accept_mime_types, *log);
(*log) << " (skipping)" << logend;
}
}
}
}
void Request::CheckContainerParameter()
{
std::wstring * container = ParamValuep(L"container");
@@ -238,24 +295,24 @@ bool Request::CheckContainerParameter()
// 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" )
if( *container == L"raw" )
{
answer_container = Request::AnswerContainer::answer_text;
container_type = ContainerType::container_raw;
}
else
if( *container == L"json" )
{
answer_container = Request::AnswerContainer::answer_json;
container_type = ContainerType::container_json;
}
else
if( *container == L"xml" )
{
answer_container = Request::AnswerContainer::answer_xml;
container_type = ContainerType::container_xml;
}
else
if( *container == L"csv" )
{
answer_container = Request::AnswerContainer::answer_csv;
container_type = ContainerType::container_csv;
}
else
{
@@ -263,50 +320,43 @@ bool Request::CheckContainerParameter()
if( log )
{
(*log) << log2 << "App: an unknown 'container' parameter: " << *container << logend;
// IMPROVE ME set status to 404
return false;
(*log) << log2 << "App: an unknown container url parameter: " << *container << " (skipping)" << logend;
}
}
}
return true;
}
// IMPROVE ME give me a better name
bool Request::CheckAnswerParameter()
void Request::PrepareFrameNames()
{
std::wstring * answer = ParamValuep(L"answer");
Config * config = get_config();
Log * log = get_logger();
if( answer )
if( config && log )
{
// IMPROVEME do a plugin call here
// if a plugin can consume this then don't check html/data and just return true
const std::wstring & frame = ParamValue(config->request_frame_parameter);
if( *answer == L"html" )
if( frame.size() <= config->request_frame_parameter_max_length )
{
use_ezc_engine = true;
}
else
if( *answer == L"data" )
{
use_ezc_engine = false;
}
else
{
Log * log = get_logger();
send_frames.clear();
slice_by(frame, ',', send_frames);
std::sort(send_frames.begin(), send_frames.end());
auto frames_end = std::unique(send_frames.begin(), send_frames.end());
send_frames.erase(frames_end, send_frames.end());
if( log )
if( send_frames.size() > config->request_frame_parameter_max_frames )
{
(*log) << log2 << "App: an unknown 'answer' parameter: " << *answer << logend;
// IMPROVE ME set status to 404
return false;
send_frames.clear();
(*log) << log2 << "Request: the number of frames exceeds " << config->request_frame_parameter_max_frames
<< " (skipping frames)" << logend;
}
}
else
{
(*log) << log2 << "Request: the length of the frame url parameter exceeds " << config->request_frame_parameter_max_length
<< " characters (skiping frames)" << logend;
}
}
return true;
}
@@ -504,6 +554,31 @@ void Request::last_item_wrapper(morm::Wrapper & wrapper)
}
bool Request::has_frame(const wchar_t * frame)
{
for(std::wstring & f: send_frames)
{
if( f == frame )
return true;
}
return false;
}
bool Request::has_frame(const std::wstring & frame)
{
for(std::wstring & f: send_frames)
{
if( f == frame )
return true;
}
return false;
}
} // namespace Winix