From 792b72e71a57ef17558dec230f8ef685810c4684 Mon Sep 17 00:00:00 2001 From: pan-wang Date: Tue, 27 Feb 2018 09:41:09 -0800 Subject: [PATCH] adding error description which was commented out previously (#613) --- src/RequestHandler/dllmain.cxx | 3 + .../outofprocess/forwardinghandler.cpp | 114 +++++++++--------- .../outofprocess/forwardinghandler.h | 4 +- 3 files changed, 64 insertions(+), 57 deletions(-) diff --git a/src/RequestHandler/dllmain.cxx b/src/RequestHandler/dllmain.cxx index cd61126507..1167833d43 100644 --- a/src/RequestHandler/dllmain.cxx +++ b/src/RequestHandler/dllmain.cxx @@ -18,6 +18,7 @@ SRWLOCK g_srwLockRH; HINTERNET g_hWinhttpSession = NULL; IHttpServer * g_pHttpServer = NULL; HINSTANCE g_hWinHttpModule; +HINSTANCE g_hAspNetCoreModule; HANDLE g_hEventLog = NULL; @@ -160,6 +161,8 @@ EnsureOutOfProcessInitializtion() g_hWinHttpModule = GetModuleHandle(TEXT("winhttp.dll")); + g_hAspNetCoreModule = GetModuleHandle(TEXT("aspnetcore.dll")); + hr = WINHTTP_HELPER::StaticInitialize(); if (FAILED(hr)) { diff --git a/src/RequestHandler/outofprocess/forwardinghandler.cpp b/src/RequestHandler/outofprocess/forwardinghandler.cpp index 759a1bb0f8..658dab4306 100644 --- a/src/RequestHandler/outofprocess/forwardinghandler.cpp +++ b/src/RequestHandler/outofprocess/forwardinghandler.cpp @@ -572,32 +572,32 @@ Failure: pResponse->SetStatus(502, "Bad Gateway", 3, hr); -// if (!(hr > HRESULT_FROM_WIN32(WINHTTP_ERROR_BASE) && -// hr <= HRESULT_FROM_WIN32(WINHTTP_ERROR_LAST)) || -//#pragma prefast (suppress : __WARNING_FUNCTION_NEEDS_REVIEW, "Function and parameters reviewed.") -// FormatMessage( -// FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_FROM_HMODULE, -// g_hWinHttpModule, -// HRESULT_CODE(hr), -// 0, -// strDescription.QueryStr(), -// strDescription.QuerySizeCCH(), -// NULL) == 0) -// { -// /*LoadString(g_hModule, -// IDS_SERVER_ERROR, -// strDescription.QueryStr(), -// strDescription.QuerySizeCCH());*/ -// } -// -// (VOID)strDescription.SyncWithBuffer(); -// if (strDescription.QueryCCH() != 0) -// { -// pResponse->SetErrorDescription( -// strDescription.QueryStr(), -// strDescription.QueryCCH(), -// FALSE); -// } + if (!(hr > HRESULT_FROM_WIN32(WINHTTP_ERROR_BASE) && + hr <= HRESULT_FROM_WIN32(WINHTTP_ERROR_LAST)) || +#pragma prefast (suppress : __WARNING_FUNCTION_NEEDS_REVIEW, "Function and parameters reviewed.") + FormatMessage( + FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_FROM_HMODULE, + g_hWinHttpModule, + HRESULT_CODE(hr), + 0, + strDescription.QueryStr(), + strDescription.QuerySizeCCH(), + NULL) == 0) + { + LoadString(g_hAspNetCoreModule, + IDS_SERVER_ERROR, + strDescription.QueryStr(), + strDescription.QuerySizeCCH()); + } + + (VOID)strDescription.SyncWithBuffer(); + if (strDescription.QueryCCH() != 0) + { + pResponse->SetErrorDescription( + strDescription.QueryStr(), + strDescription.QueryCCH(), + FALSE); + } } // @@ -1421,40 +1421,42 @@ None Failure: - m_RequestStatus = FORWARDER_DONE; - m_fHasError = TRUE; - - pResponse->DisableKernelCache(); - pResponse->GetRawHttpResponse()->EntityChunkCount = 0; - - if (hr == HRESULT_FROM_WIN32(ERROR_WINHTTP_INVALID_SERVER_RESPONSE)) + if (!m_fHasError) { - m_fResetConnection = TRUE; - } + m_RequestStatus = FORWARDER_DONE; + m_fHasError = TRUE; - if (fClientError || m_fHandleClosedDueToClient) - { - if (!m_fResponseHeadersReceivedAndSet) + pResponse->DisableKernelCache(); + pResponse->GetRawHttpResponse()->EntityChunkCount = 0; + + if (hr == HRESULT_FROM_WIN32(ERROR_WINHTTP_INVALID_SERVER_RESPONSE)) { - pResponse->SetStatus(400, "Bad Request", 0, HRESULT_FROM_WIN32(WSAECONNRESET)); + m_fResetConnection = TRUE; + } + + if (fClientError || m_fHandleClosedDueToClient) + { + if (!m_fResponseHeadersReceivedAndSet) + { + pResponse->SetStatus(400, "Bad Request", 0, HRESULT_FROM_WIN32(WSAECONNRESET)); + } + else + { + // + // Response headers from origin server were + // already received and set for the current response. + // Honor the response status. + // + } } else { - // - // Response headers from origin server were - // already received and set for the current response. - // Honor the response status. - // - } - } - else - { - STACK_STRU(strDescription, 128); + STACK_STRU(strDescription, 128); - pResponse->SetStatus(502, "Bad Gateway", 3, hr); - /* - if (!(hr > HRESULT_FROM_WIN32(WINHTTP_ERROR_BASE) && - hr <= HRESULT_FROM_WIN32(WINHTTP_ERROR_LAST)) || + pResponse->SetStatus(502, "Bad Gateway", 3, hr); + + if (!(hr > HRESULT_FROM_WIN32(WINHTTP_ERROR_BASE) && + hr <= HRESULT_FROM_WIN32(WINHTTP_ERROR_LAST)) || #pragma prefast (suppress : __WARNING_FUNCTION_NEEDS_REVIEW, "Function and parameters reviewed.") FormatMessage( FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_FROM_HMODULE, @@ -1465,7 +1467,7 @@ Failure: strDescription.QuerySizeCCH(), NULL) == 0) { - LoadString(g_hModule, + LoadString(g_hAspNetCoreModule, IDS_SERVER_ERROR, strDescription.QueryStr(), strDescription.QuerySizeCCH()); @@ -1478,9 +1480,9 @@ Failure: strDescription.QueryStr(), strDescription.QueryCCH(), FALSE); - }*/ + } + } } - //} // FREB log if (ANCMEvents::ANCM_REQUEST_FORWARD_FAIL::IsEnabled(m_pW3Context->GetTraceContext())) diff --git a/src/RequestHandler/outofprocess/forwardinghandler.h b/src/RequestHandler/outofprocess/forwardinghandler.h index 1db353019b..1a995d829f 100644 --- a/src/RequestHandler/outofprocess/forwardinghandler.h +++ b/src/RequestHandler/outofprocess/forwardinghandler.h @@ -1,6 +1,8 @@ #pragma once extern DWORD g_OptionalWinHttpFlags; +extern HINSTANCE g_hWinHttpModule; +extern HINSTANCE g_hAspNetCoreModule; enum FORWARDING_REQUEST_STATUS @@ -196,4 +198,4 @@ private: static TRACE_LOG * sm_pTraceLog; static STRA sm_pStra502ErrorMsg; -}; \ No newline at end of file +};