diff --git a/core/app.cpp b/core/app.cpp index 23c3e51..df4ab3c 100755 --- a/core/app.cpp +++ b/core/app.cpp @@ -780,6 +780,53 @@ void App::PrepareSessionCookie() } +bool App::AddHeader(const wchar_t * name, const wchar_t * value) +{ + if( !cur.request->out_headers.GetValue(name) ) + { + cur.request->out_headers.Add(name, value); + return true; + } + +return false; +} + + +bool App::AddHeader(const std::wstring & name, const std::wstring & value) +{ + if( !cur.request->out_headers.GetValue(name) ) + { + cur.request->out_headers.Add(name, value); + return true; + } + +return false; +} + + +bool App::AddHeader(const wchar_t * name, const PT::WTextStream & value) +{ + if( !cur.request->out_headers.GetValue(name) ) + { + cur.request->out_headers.Add(name, value); + return true; + } + +return false; +} + + +bool App::AddHeader(const std::wstring & name, const PT::WTextStream & value) +{ + if( !cur.request->out_headers.GetValue(name) ) + { + cur.request->out_headers.Add(name, value); + return true; + } + +return false; +} + bool App::PrepareHeadersStaticCreateResource(PT::WTextStream & out_path) { @@ -837,48 +884,51 @@ void App::PrepareHeadersStatic() return; } - cur.request->out_headers.Add(config.http_header_send_file, path); - cur.request->out_headers.Add(L"Status", L"200 OK"); - log << log2 << "App: sending a file from a static mountpoint: " << path << logend; -} + AddHeader(L"Status", L"200 OK"); + if( AddHeader(config.http_header_send_file, path) ) + log << log2 << "App: sending a file from a static mountpoint: " << path << logend; +} void App::PrepareHeaderContentType() { std::wstring * value = 0; - if( cur.request->return_json ) + if( !cur.request->out_headers.GetValue(L"Content-Type") ) { - value = &cur.request->out_headers.Add(L"Content-Type", L"application/json"); - } - else - { - switch( config.content_type_header ) + if( cur.request->return_json ) { - case 1: - value = &cur.request->out_headers.Add(L"Content-Type", L"application/xhtml+xml"); - break; - - case 2: - value = &cur.request->out_headers.Add(L"Content-Type", L"application/xml"); - break; - - case 0: - default: - value = &cur.request->out_headers.Add(L"Content-Type", L"text/html"); + value = &cur.request->out_headers.Add(L"Content-Type", L"application/json"); } - } + else + { + switch( config.content_type_header ) + { + case 1: + value = &cur.request->out_headers.Add(L"Content-Type", L"application/xhtml+xml"); + break; + + case 2: + value = &cur.request->out_headers.Add(L"Content-Type", L"application/xml"); + break; - if( value && config.utf8 ) - *value += L"; charset=UTF-8"; + case 0: + default: + value = &cur.request->out_headers.Add(L"Content-Type", L"text/html"); + } + } + + if( value && config.utf8 ) + *value += L"; charset=UTF-8"; + } } void App::PrepareHeadersForbidden() { - cur.request->out_headers.Add(L"Status", L"403 Forbidden"); + AddHeader(L"Status", L"403 Forbidden"); PrepareHeaderContentType(); } @@ -888,47 +938,47 @@ void App::PrepareHeadersRedirect() switch(cur.request->redirect_type) { case 300: - cur.request->out_headers.Add(L"Status", L"300 Multiple Choices"); + AddHeader(L"Status", L"300 Multiple Choices"); break; case 301: - cur.request->out_headers.Add(L"Status", L"301 Moved Permanently"); + AddHeader(L"Status", L"301 Moved Permanently"); break; case 302: - cur.request->out_headers.Add(L"Status", L"302 Found"); + AddHeader(L"Status", L"302 Found"); break; case 307: - cur.request->out_headers.Add(L"Status", L"307 Temporary Redirect"); + AddHeader(L"Status", L"307 Temporary Redirect"); break; case 303: default: - cur.request->out_headers.Add(L"Status", L"303 See Other"); + AddHeader(L"Status", L"303 See Other"); break; } - cur.request->out_headers.Add(L"Location", cur.request->redirect_to); + AddHeader(L"Location", cur.request->redirect_to); log << log2 << "App: redirect to: " << cur.request->redirect_to << logend; } void App::PrepareHeadersSendFile() { - cur.request->out_headers.Add(L"Status", L"200 OK"); - cur.request->out_headers.Add(config.http_header_send_file, cur.request->x_sendfile); + AddHeader(L"Status", L"200 OK"); - log << log2 << "App: sending file: " << cur.request->x_sendfile << logend; + if( AddHeader(config.http_header_send_file, cur.request->x_sendfile) ) + log << log2 << "App: sending file: " << cur.request->x_sendfile << logend; } void App::PrepareHeadersCompression(int compress_encoding) { if( compress_encoding == 0 || compress_encoding == 1 ) - cur.request->out_headers.Add(L"Content-Encoding", L"deflate"); + AddHeader(L"Content-Encoding", L"deflate"); else - cur.request->out_headers.Add(L"Content-Encoding", L"gzip"); + AddHeader(L"Content-Encoding", L"gzip"); } @@ -937,7 +987,7 @@ void App::PrepareHeadersNormal(Header header, size_t output_size) switch( header ) { case h_404: - cur.request->out_headers.Add(L"Status", L"404 Not Found"); + AddHeader(L"Status", L"404 Not Found"); PrepareHeaderContentType(); break; @@ -946,12 +996,16 @@ void App::PrepareHeadersNormal(Header header, size_t output_size) break; default: - cur.request->out_headers.Add(L"Status", L"200 OK"); + AddHeader(L"Status", L"200 OK"); PrepareHeaderContentType(); } if( output_size != static_cast(-1) ) - cur.request->out_headers.Add(L"Content-Length", output_size); + { + PT::WTextStream buf; + buf << output_size; + AddHeader(L"Content-Length", buf); + } } @@ -1011,7 +1065,7 @@ void App::PrepareHeaders(bool compressing, int compress_encoding, Header header, PrepareSessionCookie(); if( cur.request->send_as_attachment ) - cur.request->out_headers.Add(L"Content-Disposition", L"attachment"); + AddHeader(L"Content-Disposition", L"attachment"); if( !cur.request->redirect_to.empty() ) { @@ -1287,6 +1341,9 @@ int compress_encoding; // 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(-1)); + SendHeaders(); + SendCookies(); + FCGX_PutS("\r\n", fcgi_request.out); if( CanSendContent() ) { diff --git a/core/app.h b/core/app.h index dac3697..0790882 100755 --- a/core/app.h +++ b/core/app.h @@ -163,6 +163,10 @@ private: void FilterContent(); void SendHeaders(); void SendCookies(); + bool AddHeader(const wchar_t * name, const wchar_t * value); + bool AddHeader(const std::wstring & name, const std::wstring & value); + bool AddHeader(const wchar_t * name, const PT::WTextStream & value); + bool AddHeader(const std::wstring & name, const PT::WTextStream & value); bool PrepareHeadersStaticCreateResource(PT::WTextStream & out_path); void PrepareHeadersStatic(); void PrepareHeaderContentType();