changed the way how the request's answer is created,
now winix can return json, xml, csv from out_main_stream or from frames and json from models removed from Request: bool send_bin_stream bool return_json bool return_info_only pt::Space info bool page_generated bool out_main_stream_use_html_filter bool out_streams_use_html_filter added to Request: enum AnswerSource enum AnswerContainer AnswerSource answer_source AnswerContainer answer_container bool use_ezc_engine std::wstring frame bool send_all_frames bool use_html_filter added to Config: // the name of the url parameter for returning all frames, e.g. https://domain.tld/mydir/myfunction/allframes // default: allframes std::wstring request_all_frames_parameter; // the name of the root element when serializing request answer to xml // default: winix std::wstring xml_root; algorithm (the whole algorithm is described in core/request.h): at the beginning of a request winix sets answer_source to models answer_container to text use_ezc_engine to true next answer_container and use_ezc_engine can be changed in the following way: 1. winix will look for 'Accept' http header and depending on the header winix will set: (not implemented yet) Accept | answer_container | use_ezc_engine ------------------------------------|----------------- application/json | json | false application/xml | xml | false text/csv | csv | false 2. next answer_container is set depending on 'container' url parameter container | answer_container --------------------------------------------------------- not present | don't change the value text | text json | json xml | xml csv | csv use_ezc_engine is set depending on 'answer' url parameter: answer | use_ezc_engine --------------------------------- not present | don't change the value html | true data | false if 'answer' is html then we take into account two more parameters: frame: frame_name (empty default) - if set then winix returns this specific frame allframes: (if present then winix returns all frames)
This commit is contained in:
@@ -711,8 +711,9 @@ void App::ClearAfterRequest()
|
||||
cur.session->ClearAfterRequest();
|
||||
cur.session = session_manager.GetTmpSession();
|
||||
output_8bit.clear();
|
||||
output_8bit2.clear();
|
||||
compressed_output.clear();
|
||||
html_filtered.clear();
|
||||
//html_filtered.clear();
|
||||
aheader_name.clear();
|
||||
aheader_value.clear();
|
||||
cur.mount = system.mounts.GetEmptyMount();
|
||||
@@ -775,77 +776,11 @@ void App::SaveSessionsIfNeeded()
|
||||
}
|
||||
|
||||
|
||||
|
||||
pt::WTextStream * App::CreateFrameAnswer()
|
||||
{
|
||||
Request & req = *cur.request;
|
||||
auto i = req.out_streams.streams_map.begin();
|
||||
const std::wstring * frame = cur.request->ParamValuep(config.request_frame_parameter);
|
||||
|
||||
if( frame )
|
||||
{
|
||||
for( ; i != req.out_streams.streams_map.end() ; ++i)
|
||||
{
|
||||
if( i->first == *frame )
|
||||
{
|
||||
return &i->second->get_buffer();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
|
||||
pt::WTextStream * App::CreateJSONAnswer()
|
||||
{
|
||||
Request & req = *cur.request;
|
||||
json_out_stream.clear();
|
||||
|
||||
if( !req.return_info_only )
|
||||
{
|
||||
json_out_stream << L"{\n\"frames\": {\n";
|
||||
|
||||
auto i = req.out_streams.streams_map.begin();
|
||||
bool is_first = true;
|
||||
|
||||
for( ; i != req.out_streams.streams_map.end() ; ++i)
|
||||
{
|
||||
if( !is_first )
|
||||
{
|
||||
json_out_stream << L",\n";
|
||||
}
|
||||
|
||||
json_out_stream << L"\"";
|
||||
JSONescape(i->first, json_out_stream);
|
||||
json_out_stream << L"\": \"";
|
||||
JSONescapeStream(i->second->get_buffer(), json_out_stream);
|
||||
json_out_stream << L"\"";
|
||||
is_first = false;
|
||||
}
|
||||
|
||||
json_out_stream << L"}\n,\n\"info\": ";
|
||||
}
|
||||
|
||||
req.info.serialize_to_json_stream(json_out_stream, false);
|
||||
log << log3 << "App: sending JSON answer";
|
||||
|
||||
if( !req.return_info_only )
|
||||
json_out_stream << L"}\n";
|
||||
else
|
||||
log << " (Request::info only)";
|
||||
|
||||
log << logend;
|
||||
|
||||
return &json_out_stream;
|
||||
}
|
||||
|
||||
|
||||
// !! IMPROVE ME change to a better name
|
||||
void App::MakePage()
|
||||
void App::MakeEzcGenerator()
|
||||
{
|
||||
if( cur.request->page_generated || !cur.request->redirect_to.empty() || !cur.request->x_sendfile.empty() )
|
||||
return;
|
||||
// if( cur.request->page_generated || !cur.request->redirect_to.empty() || !cur.request->x_sendfile.empty() )
|
||||
// return;
|
||||
|
||||
|
||||
clock_gettime(CLOCK_REALTIME, &cur.request->timespec_ezc_engine_start);
|
||||
@@ -888,10 +823,12 @@ void App::CheckPostRedirect()
|
||||
|
||||
void App::AddDefaultModels()
|
||||
{
|
||||
if( !cur.request->send_bin_stream && !cur.request->return_json )
|
||||
// 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 )
|
||||
{
|
||||
// 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 )
|
||||
@@ -920,8 +857,8 @@ void App::Make()
|
||||
return;
|
||||
}
|
||||
|
||||
if( cur.request->ParamValue(L"reqtype") == L"json" )
|
||||
cur.request->return_json = true;
|
||||
if( !cur.request->PrepareAnswerType() )
|
||||
return;
|
||||
|
||||
if( cur.session->ip_ban && cur.session->ip_ban->IsIPBanned() )
|
||||
{
|
||||
@@ -973,9 +910,6 @@ void App::Make()
|
||||
log << log1 << "App: there is no a root dir (dir_tab is empty -- after calling a function)" << logend;
|
||||
return;
|
||||
}
|
||||
|
||||
plugin.Call(WINIX_CONTENT_MAKE);
|
||||
MakePage();
|
||||
}
|
||||
|
||||
|
||||
@@ -1316,9 +1250,9 @@ void App::ReadPostVars()
|
||||
post_multi_parser.Parse(fcgi_request.in, cur.request->post_tab, cur.request->post_file_tab);
|
||||
}
|
||||
else
|
||||
if( pt::is_substr_nc(L"application/json", cur.request->env_content_type.c_str()) )
|
||||
if( pt::is_substr_nc(Winix::Header::application_json, cur.request->env_content_type.c_str()) )
|
||||
{
|
||||
log << log3 << "App: post content type: application/json" << logend;
|
||||
log << log3 << "App: post content type: " << Winix::Header::application_json << logend;
|
||||
ReadPostJson();
|
||||
}
|
||||
else
|
||||
@@ -1498,27 +1432,42 @@ void App::PrepareHeaderContentType()
|
||||
{
|
||||
if( !cur.request->out_headers.has_key(Winix::Header::content_type) )
|
||||
{
|
||||
if( !cur.request->send_bin_stream )
|
||||
if( cur.request->answer_source == Request::AnswerSource::answer_bin_stream )
|
||||
{
|
||||
if( cur.request->return_json )
|
||||
cur.request->out_headers.add(Winix::Header::content_type, Winix::Header::application_octet_stream);
|
||||
}
|
||||
else
|
||||
{
|
||||
if( cur.request->answer_container == Request::AnswerContainer::answer_json )
|
||||
{
|
||||
cur.request->out_headers.add(Winix::Header::content_type, L"application/json; charset=UTF-8");
|
||||
cur.request->out_headers.add(Winix::Header::content_type, Winix::Header::application_json_utf8);
|
||||
}
|
||||
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 )
|
||||
{
|
||||
case 1:
|
||||
cur.request->out_headers.add(Winix::Header::content_type, L"application/xhtml+xml; charset=UTF-8");
|
||||
cur.request->out_headers.add(Winix::Header::content_type, Winix::Header::application_xhtml_xml_utf8);
|
||||
break;
|
||||
|
||||
case 2:
|
||||
cur.request->out_headers.add(Winix::Header::content_type, L"application/xml; charset=UTF-8");
|
||||
cur.request->out_headers.add(Winix::Header::content_type, Winix::Header::application_xml_utf8);
|
||||
break;
|
||||
|
||||
case 0:
|
||||
default:
|
||||
cur.request->out_headers.add(Winix::Header::content_type, L"text/html; charset=UTF-8");
|
||||
cur.request->out_headers.add(Winix::Header::content_type, Winix::Header::text_html_utf8);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1689,7 +1638,8 @@ void App::PrepareHeaders(bool compressing, int compress_encoding, Header header,
|
||||
if( cur.request->send_as_attachment )
|
||||
AddHeader(L"Content-Disposition", L"attachment");
|
||||
|
||||
if( !cur.request->redirect_to.empty() && !cur.request->return_json )
|
||||
//if( !cur.request->redirect_to.empty() && !cur.request->return_json )
|
||||
if( !cur.request->redirect_to.empty() )
|
||||
{
|
||||
PrepareHeadersRedirect();
|
||||
}
|
||||
@@ -1713,82 +1663,6 @@ void App::PrepareHeaders(bool compressing, int compress_encoding, Header header,
|
||||
}
|
||||
|
||||
|
||||
void App::PrepareStandardJSONFields()
|
||||
{
|
||||
pt::Space & info = cur.request->info;
|
||||
|
||||
if( !info.has_key(L"status") )
|
||||
{
|
||||
info.add(L"status", cur.request->status);
|
||||
}
|
||||
|
||||
if( !cur.request->redirect_to.empty() && !info.has_key(L"redirect_to") )
|
||||
{
|
||||
info.add(L"redirect_to", cur.request->redirect_to);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
void App::FilterContent()
|
||||
{
|
||||
Request & req = *cur.request;
|
||||
|
||||
bool filter_main_stream = false;
|
||||
bool filter_json = false;
|
||||
|
||||
if( config.html_filter && !req.send_bin_stream )
|
||||
{
|
||||
if( req.return_json )
|
||||
{
|
||||
if( !req.return_info_only && req.out_streams_use_html_filter )
|
||||
{
|
||||
filter_json = true;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if( req.out_main_stream_use_html_filter )
|
||||
filter_main_stream = true;
|
||||
}
|
||||
}
|
||||
|
||||
if( filter_main_stream )
|
||||
{
|
||||
std::wstring tmp_out_main_stream;
|
||||
req.out_main_stream.to_str(tmp_out_main_stream, false);
|
||||
TemplatesFunctions::html_filter.Filter(tmp_out_main_stream, html_filtered); // IMPROVEME let Filter take pt::WTextStream
|
||||
|
||||
/*
|
||||
* it would be better to just return a pointer from this method
|
||||
*
|
||||
*/
|
||||
req.out_main_stream.clear();
|
||||
req.out_main_stream.PutText(html_filtered);
|
||||
log << log3 << "App: html in the main stream has been filtered" << logend;
|
||||
}
|
||||
|
||||
if( filter_json )
|
||||
{
|
||||
for(auto i = req.out_streams.streams_map.begin() ; i != req.out_streams.streams_map.end() ; ++i)
|
||||
{
|
||||
HtmlTextStream & stream = *i->second;
|
||||
|
||||
std::wstring tmp_stream;
|
||||
stream.to_str(tmp_stream, false);
|
||||
|
||||
TemplatesFunctions::html_filter.Filter(tmp_stream, html_filtered);
|
||||
|
||||
stream.clear();
|
||||
stream.PutText(html_filtered);
|
||||
}
|
||||
|
||||
log << log3 << "App: html in json out streams have been filtered" << logend;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
int App::SelectDeflateVersion()
|
||||
@@ -1856,11 +1730,11 @@ bool App::CanSendContent()
|
||||
return false;
|
||||
}
|
||||
|
||||
if( cur.request->return_json )
|
||||
{
|
||||
// if there is a redirect flag then it will be put to info struct
|
||||
return true;
|
||||
}
|
||||
// if( cur.request->return_json )
|
||||
// {
|
||||
// // if there is a redirect flag then it will be put to info struct
|
||||
// return true;
|
||||
// }
|
||||
|
||||
if( !cur.request->redirect_to.empty() )
|
||||
{
|
||||
@@ -1918,79 +1792,9 @@ bool App::IsRequestedFrame()
|
||||
}
|
||||
|
||||
|
||||
void App::SendTextAnswer()
|
||||
{
|
||||
const pt::WTextStream * source = nullptr;
|
||||
bool compressing = false;
|
||||
int compress_encoding = 0;
|
||||
size_t output_size = 0;
|
||||
|
||||
Header header = GetHTTPStatusCode();
|
||||
|
||||
if( CanSendContent() )
|
||||
{
|
||||
/*
|
||||
* FIXME frames are not filtered (when is_htmx_request is true)
|
||||
*
|
||||
* FilterContent() should be combined with CreateJSONAnswer() and CreateFrameAnswer() somehow
|
||||
*/
|
||||
FilterContent();
|
||||
|
||||
// cur.request->return_json is depracated, we will create json from models/space registered through request->models object
|
||||
if( cur.request->return_json )
|
||||
{
|
||||
source = CreateJSONAnswer();
|
||||
}
|
||||
else
|
||||
if( IsRequestedFrame() )
|
||||
{
|
||||
source = CreateFrameAnswer();
|
||||
|
||||
if( !source )
|
||||
{
|
||||
empty_response.clear();
|
||||
source = &empty_response;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
source = &cur.request->out_main_stream.get_buffer();
|
||||
}
|
||||
|
||||
SelectCompression(source->size(), compressing, compress_encoding);
|
||||
|
||||
pt::wide_stream_to_utf8(*source, output_8bit);
|
||||
|
||||
// !! IMPROVE ME add to log the binary stream as well
|
||||
if( config.log_server_answer )
|
||||
log << log1 << "App: the server's answer is:\n" << output_8bit << "\nApp: end of the server's answer" << logend;
|
||||
|
||||
if( compressing )
|
||||
{
|
||||
compress.Compressing(output_8bit.c_str(), output_8bit.length(), compressed_output, compress_encoding);
|
||||
output_size = compressed_output.size();
|
||||
}
|
||||
else
|
||||
{
|
||||
output_size = output_8bit.size();
|
||||
}
|
||||
}
|
||||
|
||||
PrepareHeaders(compressing, compress_encoding, header, output_size);
|
||||
SendHeaders();
|
||||
SendCookies();
|
||||
FCGX_PutS("\r\n", fcgi_request.out);
|
||||
|
||||
if( CanSendContent() )
|
||||
{
|
||||
if( compressing )
|
||||
SendData(compressed_output, fcgi_request.out);
|
||||
else
|
||||
FCGX_PutStr(output_8bit.c_str(), output_8bit.size(), fcgi_request.out);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// IMPROVEME
|
||||
// we can send directly from BinaryPage without copying to a temporary buffer
|
||||
// (but there is no an interface in BinaryPage yet)
|
||||
void App::SendData(const BinaryPage & page, FCGX_Stream * out)
|
||||
{
|
||||
const size_t buf_size = 4096;
|
||||
@@ -2014,28 +1818,410 @@ void App::SendData(const BinaryPage & page, FCGX_Stream * out)
|
||||
}
|
||||
|
||||
|
||||
void App::SendBinaryAnswer()
|
||||
|
||||
void App::SendAnswer()
|
||||
{
|
||||
BinaryPage & source = cur.request->out_bin_stream;
|
||||
Header header = h_200;
|
||||
Error status = cur.request->status;
|
||||
bool compressing;
|
||||
int compress_encoding;
|
||||
output_8bit.clear();
|
||||
output_8bit2.clear();
|
||||
compressed_output.clear();
|
||||
|
||||
SelectCompression(source.size(), compressing, compress_encoding);
|
||||
// may use CanSendContent() method?
|
||||
// what about method HEAD?
|
||||
if( !cur.request->redirect_to.empty() || !cur.request->x_sendfile.empty() )
|
||||
{
|
||||
Send8bitOutput(output_8bit);
|
||||
return;
|
||||
}
|
||||
|
||||
if( status == WINIX_ERR_NO_ITEM || status == WINIX_ERR_NO_FUNCTION || status == WINIX_ERR_UNKNOWN_PARAM )
|
||||
header = h_404;
|
||||
if( cur.request->answer_source == Request::AnswerSource::answer_bin_stream )
|
||||
{
|
||||
Send8bitOutput(cur.request->out_bin_stream);
|
||||
}
|
||||
else
|
||||
{
|
||||
// is this plugin call correct here?
|
||||
plugin.Call(WINIX_CONTENT_MAKE);
|
||||
|
||||
if( status == WINIX_ERR_PERMISSION_DENIED || status == WINIX_ERR_CANT_CHANGE_USER || status == WINIX_ERR_CANT_CHANGE_GROUP )
|
||||
header = h_403;
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// !! IMPROVE ME add header: content-length (size from Item struct)
|
||||
// warning: if someone changed a file on the disk (in the real os)
|
||||
// then winix would send an incorrect content-lenght header,
|
||||
// we are waiting for the fsck winix function to be implemented
|
||||
PrepareHeaders(compressing, compress_encoding, header, static_cast<size_t>(-1));
|
||||
void App::SerializeStream(const pt::WTextStream & input_stream, const wchar_t * field_name)
|
||||
{
|
||||
switch( cur.request->answer_container )
|
||||
{
|
||||
case Request::AnswerContainer::answer_json:
|
||||
SerializeStreamJson(input_stream, field_name);
|
||||
break;
|
||||
|
||||
case Request::AnswerContainer::answer_xml:
|
||||
SerializeStreamXml(input_stream, field_name);
|
||||
break;
|
||||
|
||||
case Request::AnswerContainer::answer_csv:
|
||||
SerializeStreamCsv(input_stream, field_name);
|
||||
break;
|
||||
|
||||
case Request::AnswerContainer::answer_text:
|
||||
default:
|
||||
FilterHtmlIfNeeded(input_stream, output_8bit);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void App::SerializeStreamJson(const pt::WTextStream & input_stream, const wchar_t * field_name)
|
||||
{
|
||||
if( field_name )
|
||||
{
|
||||
output_8bit << '"';
|
||||
pt::esc_to_json(field_name, output_8bit);
|
||||
output_8bit << "\":";
|
||||
}
|
||||
|
||||
output_8bit << '"';
|
||||
FilterHtmlIfNeeded(input_stream, output_8bit2);
|
||||
pt::esc_to_json(output_8bit2, output_8bit);
|
||||
output_8bit << '"';
|
||||
}
|
||||
|
||||
|
||||
void App::SerializeStreamXml(const pt::WTextStream & input_stream, const wchar_t * field_name)
|
||||
{
|
||||
if( field_name )
|
||||
{
|
||||
output_8bit << '<';
|
||||
pt::esc_to_xml(field_name, output_8bit);
|
||||
output_8bit << '>';
|
||||
}
|
||||
|
||||
FilterHtmlIfNeeded(input_stream, output_8bit2);
|
||||
pt::esc_to_xml(output_8bit2, output_8bit);
|
||||
|
||||
if( field_name )
|
||||
{
|
||||
output_8bit << "</";
|
||||
pt::esc_to_xml(field_name, output_8bit);
|
||||
output_8bit << '>';
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void App::SerializeStreamCsv(const pt::WTextStream & input_stream, const wchar_t * field_name)
|
||||
{
|
||||
if( field_name )
|
||||
{
|
||||
output_8bit << '"';
|
||||
pt::esc_to_csv(field_name, output_8bit);
|
||||
output_8bit << "\";";
|
||||
}
|
||||
|
||||
FilterHtmlIfNeeded(input_stream, output_8bit2);
|
||||
output_8bit << '"';
|
||||
pt::esc_to_csv(output_8bit2, output_8bit);
|
||||
output_8bit << "\";\n";
|
||||
}
|
||||
|
||||
|
||||
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();
|
||||
bool is_first = true;
|
||||
|
||||
for( ; i != cur.request->out_streams.streams_map.end() ; ++i)
|
||||
{
|
||||
if( cur.request->answer_container == Request::AnswerContainer::answer_json && !is_first )
|
||||
{
|
||||
output_8bit << ',';
|
||||
}
|
||||
|
||||
if( cur.request->answer_container == Request::AnswerContainer::answer_xml && i->first.empty() )
|
||||
{
|
||||
log << log2 << "App: I cannot serialize a frame with an empty name to xml (frame skipped)" << logend;
|
||||
}
|
||||
else
|
||||
{
|
||||
SerializeStream(i->second->get_buffer(), i->first.c_str());
|
||||
}
|
||||
|
||||
is_first = false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void App::SerializeOneFrame()
|
||||
{
|
||||
auto i = cur.request->out_streams.streams_map.find(cur.request->frame);
|
||||
|
||||
if( i != cur.request->out_streams.streams_map.end() )
|
||||
{
|
||||
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?
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
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 )
|
||||
{
|
||||
output_8bit << ',';
|
||||
}
|
||||
|
||||
if( cur.request->answer_container == Request::AnswerContainer::answer_xml && i->first.empty() )
|
||||
{
|
||||
log << log2 << "App: I cannot serialize a model with an empty name to xml (model skipped)" << logend;
|
||||
}
|
||||
else
|
||||
{
|
||||
SerializeModel(i->second, i->first.c_str());
|
||||
}
|
||||
|
||||
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 )
|
||||
{
|
||||
case Request::AnswerContainer::answer_json:
|
||||
SerializeModelJson(wrapper, field_name);
|
||||
break;
|
||||
|
||||
case Request::AnswerContainer::answer_xml:
|
||||
SerializeModelXml(wrapper, field_name);
|
||||
break;
|
||||
|
||||
case Request::AnswerContainer::answer_csv:
|
||||
SerializeModelCsv(wrapper, field_name);
|
||||
break;
|
||||
|
||||
case Request::AnswerContainer::answer_text:
|
||||
default:
|
||||
SerializeModelCsv(wrapper, field_name);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
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 << "\":";
|
||||
}
|
||||
|
||||
if( wrapper.model )
|
||||
{
|
||||
serialized_model.clear();
|
||||
wrapper.model->set_connector(model_connector);
|
||||
wrapper.model->to_text(serialized_model);
|
||||
output_8bit << serialized_model;
|
||||
}
|
||||
|
||||
if( wrapper.date )
|
||||
{
|
||||
output_8bit << '"';
|
||||
wrapper.date->SerializeISO(output_8bit);
|
||||
output_8bit << '"';
|
||||
}
|
||||
|
||||
if( wrapper.space_wrapper )
|
||||
{
|
||||
wrapper.space_wrapper->get_space()->serialize_to_json_stream(output_8bit, false);
|
||||
}
|
||||
|
||||
if( wrapper.model_container_wrapper )
|
||||
{
|
||||
wrapper.model_container_wrapper->set_iterator_at_first_model();
|
||||
bool is_first = true;
|
||||
output_8bit << '[';
|
||||
|
||||
while( wrapper.model_container_wrapper->is_iterator_correct() )
|
||||
{
|
||||
if( !is_first )
|
||||
output_8bit << ',';
|
||||
|
||||
morm::Model * model = wrapper.model_container_wrapper->get_model();
|
||||
serialized_model.clear();
|
||||
model->set_connector(model_connector);
|
||||
model->to_text(serialized_model);
|
||||
output_8bit << serialized_model;
|
||||
|
||||
wrapper.model_container_wrapper->increment_iterator();
|
||||
is_first = false;
|
||||
}
|
||||
|
||||
output_8bit << ']';
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void App::SerializeModelXml(morm::Wrapper & wrapper, const wchar_t * field_name)
|
||||
{
|
||||
// IMPROVEME
|
||||
log << log2 << "App: serializing models to xml not implemented yet" << logend;
|
||||
}
|
||||
|
||||
|
||||
void App::SerializeModelCsv(morm::Wrapper & wrapper, const wchar_t * field_name)
|
||||
{
|
||||
// IMPROVEME
|
||||
log << log2 << "App: serializing models to csv not implemented yet" << logend;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
// IMPROVEME
|
||||
// gime me a better name
|
||||
void App::FilterHtmlIfNeeded(const pt::WTextStream & input_stream, BinaryPage & output, bool clear_stream)
|
||||
{
|
||||
if( config.html_filter && cur.request->use_html_filter )
|
||||
{
|
||||
TemplatesFunctions::html_filter.filter(input_stream, output, clear_stream);
|
||||
}
|
||||
else
|
||||
{
|
||||
pt::wide_stream_to_utf8(input_stream, output, clear_stream);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void App::Send8bitOutput(BinaryPage & output)
|
||||
{
|
||||
bool compressing = false;
|
||||
int compress_encoding = 0;
|
||||
Header header = GetHTTPStatusCode();
|
||||
size_t output_size = 0;
|
||||
|
||||
SelectCompression(output.size(), compressing, compress_encoding);
|
||||
|
||||
if( config.log_server_answer )
|
||||
{
|
||||
log << log1 << "App: the server's answer is:\n" << output << "\nApp: end of the server's answer" << logend;
|
||||
}
|
||||
|
||||
if( compressing )
|
||||
{
|
||||
compress.Compressing(output, compressed_output, compress_encoding);
|
||||
output_size = compressed_output.size();
|
||||
}
|
||||
else
|
||||
{
|
||||
output_size = output.size();
|
||||
}
|
||||
|
||||
PrepareHeaders(compressing, compress_encoding, header, output_size);
|
||||
SendHeaders();
|
||||
SendCookies();
|
||||
FCGX_PutS("\r\n", fcgi_request.out);
|
||||
@@ -2043,29 +2229,13 @@ int compress_encoding;
|
||||
if( CanSendContent() )
|
||||
{
|
||||
if( compressing )
|
||||
{
|
||||
compress.Compressing(source, compressed_output, compress_encoding);
|
||||
SendData(compressed_output, fcgi_request.out);
|
||||
}
|
||||
else
|
||||
{
|
||||
SendData(source, fcgi_request.out);
|
||||
}
|
||||
SendData(output, fcgi_request.out);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void App::SendAnswer()
|
||||
{
|
||||
if( cur.request->return_json )
|
||||
PrepareStandardJSONFields();
|
||||
|
||||
if( cur.request->send_bin_stream )
|
||||
SendBinaryAnswer();
|
||||
else
|
||||
SendTextAnswer();
|
||||
}
|
||||
|
||||
|
||||
void App::LogUser(const char * msg, uid_t id)
|
||||
{
|
||||
|
Reference in New Issue
Block a user