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:
@@ -386,6 +386,7 @@ 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);
|
||||
|
||||
@@ -777,7 +778,7 @@ void App::SaveSessionsIfNeeded()
|
||||
|
||||
|
||||
// !! IMPROVE ME change to a better name
|
||||
void App::MakeEzcGenerator()
|
||||
void App::UseEzcGenerator()
|
||||
{
|
||||
// if( cur.request->page_generated || !cur.request->redirect_to.empty() || !cur.request->x_sendfile.empty() )
|
||||
// return;
|
||||
@@ -823,23 +824,19 @@ void App::CheckPostRedirect()
|
||||
|
||||
void App::AddDefaultModels()
|
||||
{
|
||||
// there is no need to add default models if we return a binary stream
|
||||
if( cur.request->answer_source != Request::AnswerSource::answer_bin_stream )
|
||||
if( cur.request->function && cur.request->function->register_default_models )
|
||||
{
|
||||
if( cur.request->function && cur.request->function->register_default_models )
|
||||
// may it would be better do not return cur.request by default?
|
||||
cur.request->models.Add(L"request", cur.request);
|
||||
|
||||
if( cur.session && cur.session->puser )
|
||||
{
|
||||
// may it would be better do not return cur.request by default?
|
||||
cur.request->models.Add(L"request", cur.request);
|
||||
cur.request->models.Add(L"user", *cur.session->puser);
|
||||
}
|
||||
|
||||
if( cur.session && cur.session->puser )
|
||||
{
|
||||
cur.request->models.Add(L"user", *cur.session->puser);
|
||||
}
|
||||
|
||||
if( cur.request->is_item )
|
||||
{
|
||||
cur.request->models.Add(L"item", cur.request->item);
|
||||
}
|
||||
if( cur.request->is_item )
|
||||
{
|
||||
cur.request->models.Add(L"item", cur.request->item);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -857,8 +854,7 @@ void App::Make()
|
||||
return;
|
||||
}
|
||||
|
||||
if( !cur.request->PrepareAnswerType() )
|
||||
return;
|
||||
cur.request->PrepareAnswerType();
|
||||
|
||||
if( cur.session->ip_ban && cur.session->ip_ban->IsIPBanned() )
|
||||
{
|
||||
@@ -958,6 +954,7 @@ void App::ReadRequest()
|
||||
|
||||
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();
|
||||
@@ -987,7 +984,9 @@ void App::SetEnv(const char * name, std::wstring & env)
|
||||
|
||||
|
||||
|
||||
|
||||
/*
|
||||
* IMPROVE ME take it from cur.request.headers_in?
|
||||
*/
|
||||
void App::ReadEnvVariables()
|
||||
{
|
||||
SetEnv("REQUEST_METHOD", cur.request->env_request_method);
|
||||
@@ -1000,6 +999,7 @@ void App::ReadEnvVariables()
|
||||
SetEnv("HTTP_USER_AGENT", cur.request->env_http_user_agent);
|
||||
SetEnv("HTTP_COOKIE", cur.request->env_http_cookie);
|
||||
SetEnv("HTTP_ACCEPT_ENCODING", cur.request->env_http_accept_encoding);
|
||||
SetEnv("HTTP_ACCEPT", cur.request->env_http_accept);
|
||||
}
|
||||
|
||||
|
||||
@@ -1116,7 +1116,6 @@ void App::ReadEnvRemoteIP()
|
||||
|
||||
|
||||
|
||||
|
||||
void App::CheckRequestMethod()
|
||||
{
|
||||
cur.request->method = Request::unknown_method;
|
||||
@@ -1432,28 +1431,28 @@ void App::PrepareHeaderContentType()
|
||||
{
|
||||
if( !cur.request->out_headers.has_key(Winix::Header::content_type) )
|
||||
{
|
||||
if( cur.request->answer_source == Request::AnswerSource::answer_bin_stream )
|
||||
if( cur.request->container_type == Request::ContainerType::container_json )
|
||||
{
|
||||
cur.request->out_headers.add(Winix::Header::content_type, Winix::Header::application_octet_stream);
|
||||
cur.request->out_headers.add(Winix::Header::content_type, Winix::Header::application_json_utf8);
|
||||
}
|
||||
else
|
||||
if( cur.request->container_type == Request::ContainerType::container_xml )
|
||||
{
|
||||
if( cur.request->answer_container == Request::AnswerContainer::answer_json )
|
||||
cur.request->out_headers.add(Winix::Header::content_type, Winix::Header::application_xml_utf8);
|
||||
}
|
||||
else
|
||||
if( cur.request->container_type == Request::ContainerType::container_csv )
|
||||
{
|
||||
cur.request->out_headers.add(Winix::Header::content_type, Winix::Header::text_csv_utf8);
|
||||
}
|
||||
else
|
||||
if( cur.request->container_type == Request::ContainerType::container_raw )
|
||||
{
|
||||
if( cur.request->send_bin_stream )
|
||||
{
|
||||
cur.request->out_headers.add(Winix::Header::content_type, Winix::Header::application_json_utf8);
|
||||
cur.request->out_headers.add(Winix::Header::content_type, Winix::Header::application_octet_stream);
|
||||
}
|
||||
else
|
||||
if( cur.request->answer_container == Request::AnswerContainer::answer_xml )
|
||||
{
|
||||
cur.request->out_headers.add(Winix::Header::content_type, Winix::Header::application_xml_utf8);
|
||||
}
|
||||
else
|
||||
if( cur.request->answer_container == Request::AnswerContainer::answer_csv )
|
||||
{
|
||||
cur.request->out_headers.add(Winix::Header::content_type, Winix::Header::text_csv_utf8);
|
||||
}
|
||||
else
|
||||
if( cur.request->answer_container == Request::AnswerContainer::answer_text )
|
||||
{
|
||||
switch( config.content_type_header )
|
||||
{
|
||||
@@ -1828,83 +1827,167 @@ void App::SendAnswer()
|
||||
// what about method HEAD?
|
||||
if( !cur.request->redirect_to.empty() || !cur.request->x_sendfile.empty() )
|
||||
{
|
||||
Send8bitOutput(output_8bit);
|
||||
Send8bitOutput(output_8bit); // send empty content
|
||||
return;
|
||||
}
|
||||
|
||||
if( cur.request->answer_source == Request::AnswerSource::answer_bin_stream )
|
||||
plugin.Call(WINIX_CONTENT_MAKE);
|
||||
|
||||
if( cur.request->use_ezc_engine )
|
||||
{
|
||||
UseEzcGenerator();
|
||||
}
|
||||
|
||||
if( cur.request->container_type == Request::ContainerType::container_raw )
|
||||
{
|
||||
PrepareRawAnswer();
|
||||
}
|
||||
else
|
||||
if( cur.request->container_type == Request::ContainerType::container_json )
|
||||
{
|
||||
PrepareJsonAnswer();
|
||||
}
|
||||
else
|
||||
if( cur.request->container_type == Request::ContainerType::container_xml )
|
||||
{
|
||||
PrepareXmlAnswer();
|
||||
}
|
||||
else
|
||||
if( cur.request->container_type == Request::ContainerType::container_csv )
|
||||
{
|
||||
PrepareCsvAnswer();
|
||||
}
|
||||
|
||||
Send8bitOutput(output_8bit);
|
||||
}
|
||||
|
||||
|
||||
void App::PrepareRawAnswer()
|
||||
{
|
||||
if( cur.request->send_bin_stream )
|
||||
{
|
||||
Send8bitOutput(cur.request->out_bin_stream);
|
||||
}
|
||||
else
|
||||
if( cur.request->send_main_stream )
|
||||
{
|
||||
// is this plugin call correct here?
|
||||
plugin.Call(WINIX_CONTENT_MAKE);
|
||||
|
||||
if( cur.request->answer_source == Request::AnswerSource::answer_models && cur.request->use_ezc_engine )
|
||||
{
|
||||
MakeEzcGenerator(); // give me a better name
|
||||
|
||||
if( !cur.request->frame.empty() || cur.request->send_all_frames )
|
||||
{
|
||||
cur.request->answer_source = Request::AnswerSource::answer_frame_streams;
|
||||
}
|
||||
else
|
||||
{
|
||||
cur.request->answer_source = Request::AnswerSource::answer_main_stream;
|
||||
}
|
||||
}
|
||||
|
||||
if( cur.request->answer_source == Request::AnswerSource::answer_main_stream )
|
||||
{
|
||||
const wchar_t * field_name = nullptr;
|
||||
|
||||
if( cur.request->answer_container == Request::AnswerContainer::answer_xml )
|
||||
field_name = config.xml_root.c_str();
|
||||
|
||||
SerializeStream(cur.request->out_main_stream.get_buffer(), field_name);
|
||||
}
|
||||
else
|
||||
if( cur.request->answer_source == Request::AnswerSource::answer_frame_streams )
|
||||
{
|
||||
SerializeFrames();
|
||||
}
|
||||
else
|
||||
if( cur.request->answer_source == Request::AnswerSource::answer_models )
|
||||
{
|
||||
SerializeModels();
|
||||
}
|
||||
|
||||
Send8bitOutput(output_8bit);
|
||||
FilterHtmlIfNeeded(cur.request->out_main_stream.get_buffer(), output_8bit, false);
|
||||
}
|
||||
else
|
||||
if( cur.request->send_all_frames )
|
||||
{
|
||||
SerializeAllFrames();
|
||||
}
|
||||
else
|
||||
if( !cur.request->send_frames.empty() )
|
||||
{
|
||||
SerializeSpecificFrames();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void App::SerializeStream(const pt::WTextStream & input_stream, const wchar_t * field_name)
|
||||
void App::PrepareJsonAnswer()
|
||||
{
|
||||
switch( cur.request->answer_container )
|
||||
output_8bit << '{';
|
||||
PrepareContenerizedAnswer();
|
||||
output_8bit << '}';
|
||||
}
|
||||
|
||||
void App::PrepareXmlAnswer()
|
||||
{
|
||||
output_8bit << '<';
|
||||
pt::esc_to_xml(config.xml_root, output_8bit);
|
||||
output_8bit << '>';
|
||||
|
||||
PrepareContenerizedAnswer();
|
||||
|
||||
output_8bit << "</";
|
||||
pt::esc_to_xml(config.xml_root, output_8bit);
|
||||
output_8bit << '>';
|
||||
}
|
||||
|
||||
|
||||
void App::PrepareCsvAnswer()
|
||||
{
|
||||
PrepareContenerizedAnswer();
|
||||
}
|
||||
|
||||
|
||||
void App::PrepareContenerizedAnswer()
|
||||
{
|
||||
bool put_separator = false;
|
||||
|
||||
if( cur.request->serialize_models )
|
||||
{
|
||||
case Request::AnswerContainer::answer_json:
|
||||
SerializeStreamJson(input_stream, field_name);
|
||||
break;
|
||||
SerializeModels();
|
||||
put_separator = true;
|
||||
}
|
||||
|
||||
case Request::AnswerContainer::answer_xml:
|
||||
SerializeStreamXml(input_stream, field_name);
|
||||
break;
|
||||
if( cur.request->send_bin_stream )
|
||||
{
|
||||
PutSeparatorIfNeeded(put_separator);
|
||||
|
||||
case Request::AnswerContainer::answer_csv:
|
||||
SerializeStreamCsv(input_stream, field_name);
|
||||
break;
|
||||
// IMPLEMENT ME serialize binary stream as base64 and put in 'bin_stream' field
|
||||
pt::WTextStream str;
|
||||
str << "NOT IMPLEMENTED YET";
|
||||
SerializeStream(str, config.bin_stream_field.c_str());
|
||||
put_separator = true;
|
||||
}
|
||||
|
||||
case Request::AnswerContainer::answer_text:
|
||||
default:
|
||||
FilterHtmlIfNeeded(input_stream, output_8bit);
|
||||
break;
|
||||
if( cur.request->send_main_stream )
|
||||
{
|
||||
PutSeparatorIfNeeded(put_separator);
|
||||
SerializeStream(cur.request->out_main_stream.get_buffer(), config.main_stream_field.c_str());
|
||||
put_separator = true;
|
||||
}
|
||||
|
||||
if( cur.request->send_all_frames || !cur.request->send_frames.empty() )
|
||||
{
|
||||
PutSeparatorIfNeeded(put_separator);
|
||||
SerializeFieldJson(config.ezc_frames_field.c_str());
|
||||
output_8bit << "{";
|
||||
|
||||
if( cur.request->send_all_frames )
|
||||
{
|
||||
SerializeAllFrames();
|
||||
}
|
||||
else
|
||||
if( !cur.request->send_frames.empty() )
|
||||
{
|
||||
SerializeSpecificFrames();
|
||||
}
|
||||
|
||||
output_8bit << "}";
|
||||
put_separator = true;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void App::SerializeStreamJson(const pt::WTextStream & input_stream, const wchar_t * field_name)
|
||||
void App::PutSeparatorIfNeeded(bool put_separator)
|
||||
{
|
||||
if( put_separator )
|
||||
{
|
||||
switch( cur.request->container_type )
|
||||
{
|
||||
case Request::ContainerType::container_json:
|
||||
output_8bit << ",";
|
||||
break;
|
||||
|
||||
case Request::ContainerType::container_xml:
|
||||
break;
|
||||
|
||||
case Request::ContainerType::container_csv:
|
||||
output_8bit << ";";
|
||||
break;
|
||||
|
||||
case Request::ContainerType::container_raw:
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void App::SerializeFieldJson(const wchar_t * field_name)
|
||||
{
|
||||
if( field_name )
|
||||
{
|
||||
@@ -1912,7 +1995,37 @@ void App::SerializeStreamJson(const pt::WTextStream & input_stream, const wchar_
|
||||
pt::esc_to_json(field_name, output_8bit);
|
||||
output_8bit << "\":";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
void App::SerializeStream(const pt::WTextStream & input_stream, const wchar_t * field_name)
|
||||
{
|
||||
switch( cur.request->container_type )
|
||||
{
|
||||
case Request::ContainerType::container_json:
|
||||
SerializeStreamJson(input_stream, field_name);
|
||||
break;
|
||||
|
||||
case Request::ContainerType::container_xml:
|
||||
SerializeStreamXml(input_stream, field_name);
|
||||
break;
|
||||
|
||||
case Request::ContainerType::container_csv:
|
||||
SerializeStreamCsv(input_stream, field_name);
|
||||
break;
|
||||
|
||||
case Request::ContainerType::container_raw:
|
||||
default:
|
||||
FilterHtmlIfNeeded(input_stream, output_8bit, false);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void App::SerializeStreamJson(const pt::WTextStream & input_stream, const wchar_t * field_name)
|
||||
{
|
||||
SerializeFieldJson(field_name);
|
||||
output_8bit << '"';
|
||||
|
||||
if( config.html_filter && cur.request->use_html_filter )
|
||||
@@ -1982,39 +2095,6 @@ void App::SerializeStreamCsv(const pt::WTextStream & input_stream, const wchar_t
|
||||
}
|
||||
|
||||
|
||||
void App::SerializeFrames()
|
||||
{
|
||||
if( cur.request->answer_container == Request::AnswerContainer::answer_json )
|
||||
{
|
||||
output_8bit << '{';
|
||||
}
|
||||
else
|
||||
if( cur.request->answer_container == Request::AnswerContainer::answer_xml )
|
||||
{
|
||||
output_8bit << '<';
|
||||
pt::esc_to_xml(config.xml_root, output_8bit);
|
||||
output_8bit << '>';
|
||||
}
|
||||
|
||||
if( cur.request->frame.empty() || cur.request->send_all_frames )
|
||||
SerializeAllFrames();
|
||||
else
|
||||
SerializeOneFrame();
|
||||
|
||||
if( cur.request->answer_container == Request::AnswerContainer::answer_json )
|
||||
{
|
||||
output_8bit << '}';
|
||||
}
|
||||
else
|
||||
if( cur.request->answer_container == Request::AnswerContainer::answer_xml )
|
||||
{
|
||||
output_8bit << "</";
|
||||
pt::esc_to_xml(config.xml_root, output_8bit);
|
||||
output_8bit << '>';
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void App::SerializeAllFrames()
|
||||
{
|
||||
auto i = cur.request->out_streams.streams_map.begin();
|
||||
@@ -2022,12 +2102,12 @@ void App::SerializeAllFrames()
|
||||
|
||||
for( ; i != cur.request->out_streams.streams_map.end() ; ++i)
|
||||
{
|
||||
if( cur.request->answer_container == Request::AnswerContainer::answer_json && !is_first )
|
||||
if( cur.request->container_type == Request::ContainerType::container_json && !is_first )
|
||||
{
|
||||
output_8bit << ',';
|
||||
}
|
||||
|
||||
if( cur.request->answer_container == Request::AnswerContainer::answer_xml && i->first.empty() )
|
||||
if( cur.request->container_type == Request::ContainerType::container_xml && i->first.empty() )
|
||||
{
|
||||
log << log2 << "App: I cannot serialize a frame with an empty name to xml (frame skipped)" << logend;
|
||||
}
|
||||
@@ -2041,51 +2121,46 @@ void App::SerializeAllFrames()
|
||||
}
|
||||
|
||||
|
||||
void App::SerializeOneFrame()
|
||||
void App::SerializeSpecificFrames()
|
||||
{
|
||||
auto i = cur.request->out_streams.streams_map.find(cur.request->frame);
|
||||
bool is_first = true;
|
||||
|
||||
if( i != cur.request->out_streams.streams_map.end() )
|
||||
for(std::wstring & frame: cur.request->send_frames)
|
||||
{
|
||||
SerializeStream(i->second->get_buffer(), cur.request->frame.c_str());
|
||||
}
|
||||
else
|
||||
{
|
||||
log << log2 << "App: there is no such a frame: " << cur.request->frame << logend;
|
||||
// return 404 in such a case?
|
||||
auto i = cur.request->out_streams.streams_map.find(frame);
|
||||
|
||||
if( i != cur.request->out_streams.streams_map.end() )
|
||||
{
|
||||
if( cur.request->container_type == Request::ContainerType::container_json && !is_first )
|
||||
{
|
||||
output_8bit << ',';
|
||||
}
|
||||
|
||||
SerializeStream(i->second->get_buffer(), frame.c_str());
|
||||
is_first = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
log << log2 << "App: there is no such a frame: " << frame << logend;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
void App::SerializeModels()
|
||||
{
|
||||
Ezc::Models::ModelsMap models_map = cur.request->models.GetMap();
|
||||
auto i = models_map.begin();
|
||||
|
||||
if( cur.request->answer_container == Request::AnswerContainer::answer_json )
|
||||
{
|
||||
output_8bit << '{';
|
||||
}
|
||||
else
|
||||
if( cur.request->answer_container == Request::AnswerContainer::answer_xml )
|
||||
{
|
||||
output_8bit << '<';
|
||||
pt::esc_to_xml(config.xml_root, output_8bit);
|
||||
output_8bit << '>';
|
||||
log << log2 << "App: serializing models to xml not implemented yet" << logend;
|
||||
}
|
||||
|
||||
bool is_first = true;
|
||||
|
||||
for( ; i != models_map.end() ; ++i)
|
||||
{
|
||||
if( cur.request->answer_container == Request::AnswerContainer::answer_json && !is_first )
|
||||
if( cur.request->container_type == Request::ContainerType::container_json && !is_first )
|
||||
{
|
||||
output_8bit << ',';
|
||||
}
|
||||
|
||||
if( cur.request->answer_container == Request::AnswerContainer::answer_xml && i->first.empty() )
|
||||
if( cur.request->container_type == Request::ContainerType::container_xml && i->first.empty() )
|
||||
{
|
||||
log << log2 << "App: I cannot serialize a model with an empty name to xml (model skipped)" << logend;
|
||||
}
|
||||
@@ -2096,40 +2171,27 @@ void App::SerializeModels()
|
||||
|
||||
is_first = false;
|
||||
}
|
||||
|
||||
if( cur.request->answer_container == Request::AnswerContainer::answer_json )
|
||||
{
|
||||
output_8bit << '}';
|
||||
}
|
||||
else
|
||||
if( cur.request->answer_container == Request::AnswerContainer::answer_xml )
|
||||
{
|
||||
output_8bit << "</";
|
||||
pt::esc_to_xml(config.xml_root, output_8bit);
|
||||
output_8bit << '>';
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void App::SerializeModel(morm::Wrapper & wrapper, const wchar_t * field_name)
|
||||
{
|
||||
switch( cur.request->answer_container )
|
||||
switch( cur.request->container_type )
|
||||
{
|
||||
case Request::AnswerContainer::answer_json:
|
||||
case Request::ContainerType::container_json:
|
||||
SerializeModelJson(wrapper, field_name);
|
||||
break;
|
||||
|
||||
case Request::AnswerContainer::answer_xml:
|
||||
case Request::ContainerType::container_xml:
|
||||
SerializeModelXml(wrapper, field_name);
|
||||
break;
|
||||
|
||||
case Request::AnswerContainer::answer_csv:
|
||||
case Request::ContainerType::container_csv:
|
||||
SerializeModelCsv(wrapper, field_name);
|
||||
break;
|
||||
|
||||
case Request::AnswerContainer::answer_text:
|
||||
case Request::ContainerType::container_raw:
|
||||
default:
|
||||
SerializeModelCsv(wrapper, field_name);
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -2137,12 +2199,7 @@ void App::SerializeModel(morm::Wrapper & wrapper, const wchar_t * field_name)
|
||||
|
||||
void App::SerializeModelJson(morm::Wrapper & wrapper, const wchar_t * field_name)
|
||||
{
|
||||
if( field_name )
|
||||
{
|
||||
output_8bit << '"';
|
||||
pt::esc_to_json(field_name, output_8bit);
|
||||
output_8bit << "\":";
|
||||
}
|
||||
SerializeFieldJson(field_name);
|
||||
|
||||
if( wrapper.model )
|
||||
{
|
||||
|
Reference in New Issue
Block a user