diff --git a/winixd/core/Makefile.dep b/winixd/core/Makefile.dep index c03d295..fc86868 100644 --- a/winixd/core/Makefile.dep +++ b/winixd/core/Makefile.dep @@ -114,6 +114,7 @@ app.o: ../../../winix/winixd/templates/htmltextstream.h app.o: ../../../winix/winixd/core/sessionmanager.h compress.h postparser.h app.o: httpsimpleparser.h plugin.h pluginmsg.h cookieparser.h app.o: postmultiparser.h acceptencodingparser.h acceptbaseparser.h +app.o: ../../../pikotools/space/jsontospaceparser.h basethread.o: basethread.h synchro.h bbcodeparser.o: bbcodeparser.h htmlfilter.h compress.o: compress.h requesttypes.h diff --git a/winixd/core/app.cpp b/winixd/core/app.cpp index ed395d9..867ba34 100644 --- a/winixd/core/app.cpp +++ b/winixd/core/app.cpp @@ -949,9 +949,48 @@ void App::LogAccess() +void App::ReadPostJson() +{ + char buffer[1024]; + const int buffer_len = sizeof(buffer) / sizeof(char) - 1; + int read_len; + + post_json_parser.SetSpace(cur.request->post_in); + post_buffer.clear(); + post_buffer.reserve(1024 * 1024 * 5); // IMPROVEME add to config? + + cur.request->is_postin_used = true; + + do + { + // IMPROVE ME + // we can read to PT::TextBuffer and make a PT::JSONToSpaceParser::Parse(PT::TextBuffer &) method + read_len = FCGX_GetStr(buffer, buffer_len, fcgi_request.in); + + if( read_len > 0 ) + post_buffer.append(buffer, read_len); + } + while( read_len == buffer_len ); + + PT::JSONToSpaceParser::Status status = post_json_parser.ParseString(post_buffer.c_str()); + post_buffer.clear(); + + if( status != PT::JSONToSpaceParser::ok ) + { + log << log1 << "App: cannot parse the input stream as a JSON object, status: " << (int)status << logend; + cur.request->post_in.Clear(); + // return an error (http error of some kind?) + } +} + + + void App::ReadPostVars() { - if( cur.request->method == Request::post ) + // CHECKME + // what about errors during parsing input? + + if( cur.request->method == Request::post || cur.request->method == Request::delete_ ) { if( IsSubStringNoCase(L"multipart/form-data", cur.request->env_content_type.c_str()) ) { @@ -959,7 +998,14 @@ void App::ReadPostVars() post_multi_parser.Parse(fcgi_request.in, cur.request->post_tab, cur.request->post_file_tab); } else + if( IsSubStringNoCase(L"application/json", cur.request->env_content_type.c_str()) ) { + log << log3 << "App: post content type: application/json" << logend; + ReadPostJson(); + } + else + { + // IMPROVE ME may to check a correct content-type header? post_parser.Parse(fcgi_request.in, cur.request->post_tab); } } diff --git a/winixd/core/app.h b/winixd/core/app.h index 2121f57..5801edd 100644 --- a/winixd/core/app.h +++ b/winixd/core/app.h @@ -59,7 +59,7 @@ #include "cookieparser.h" #include "postmultiparser.h" #include "acceptencodingparser.h" - +#include "space/jsontospaceparser.h" namespace Winix @@ -140,6 +140,9 @@ private: PostParser post_parser; PostMultiParser post_multi_parser; + PT::JSONToSpaceParser post_json_parser; + std::string post_buffer; + CookieParser cookie_parser; AcceptEncodingParser accept_encoding_parser; Compress compress; @@ -191,6 +194,7 @@ private: void ReadEnvHTTPVariables(); bool SaveEnvHTTPVariable(const char * env); void ReadEnvRemoteIP(); + void ReadPostJson(); void ReadPostVars(); void CheckIE(); diff --git a/winixd/core/request.cpp b/winixd/core/request.cpp index f6896d4..7ca9832 100644 --- a/winixd/core/request.cpp +++ b/winixd/core/request.cpp @@ -93,6 +93,8 @@ void Request::Clear() post_tab.clear(); post_file_tab.clear(); cookie_tab.clear(); + post_in.Clear(); + is_postin_used = false; method = unknown_method; diff --git a/winixd/core/request.h b/winixd/core/request.h index c560b8b..fc374a6 100644 --- a/winixd/core/request.h +++ b/winixd/core/request.h @@ -125,6 +125,8 @@ struct Request PostTab post_tab; PostFileTab post_file_tab; CookieTab cookie_tab; + PT::Space post_in; + bool is_postin_used;// temporarily, before all post variables will be put to post_in // input headers (without cookies) // at the moment we are using FastCGI and HTTP headers are prefixed with 'HTTP_' string diff --git a/winixd/main/Makefile.dep b/winixd/main/Makefile.dep index 7421833..94c1c11 100644 --- a/winixd/main/Makefile.dep +++ b/winixd/main/Makefile.dep @@ -145,5 +145,6 @@ main.o: ../../../winix/winixd/core/cookieparser.h main.o: ../../../winix/winixd/core/postmultiparser.h main.o: ../../../winix/winixd/core/acceptencodingparser.h main.o: ../../../winix/winixd/core/acceptbaseparser.h +main.o: ../../../pikotools/space/jsontospaceparser.h main.o: ../../../winix/winixd/core/plugin.h main.o: ../../../winix/winixd/core/version.h