From 4b44e45905046657de9d4504bbd0d07dbb6bdd4d Mon Sep 17 00:00:00 2001 From: kamrey2 <154378445+kamrey2@users.noreply.github.com> Date: Fri, 11 Apr 2025 16:58:25 -0700 Subject: [PATCH] Removing unnecessary memory allocations. --- .../cpp/IHttpRequestGetHeader.cpp | 63 ++++--------------- 1 file changed, 12 insertions(+), 51 deletions(-) diff --git a/samples/snippets/cpp/VS_Snippets_IIS/IIS7/IHttpRequestGetHeader/cpp/IHttpRequestGetHeader.cpp b/samples/snippets/cpp/VS_Snippets_IIS/IIS7/IHttpRequestGetHeader/cpp/IHttpRequestGetHeader.cpp index d819d158e..5ef0f8320 100644 --- a/samples/snippets/cpp/VS_Snippets_IIS/IIS7/IHttpRequestGetHeader/cpp/IHttpRequestGetHeader.cpp +++ b/samples/snippets/cpp/VS_Snippets_IIS/IIS7/IHttpRequestGetHeader/cpp/IHttpRequestGetHeader.cpp @@ -44,7 +44,7 @@ class MyHttpModule : public CHttpModule pszUserAgent = pHttpRequest->GetHeader("User-Agent",&cchUserAgent); // The header length will be 0 if the header was not found. - if (cchUserAgent == 0) + if (pszUserAgent == NULL || cchUserAgent == 0) { // Return a status message. WriteResponseMessage(pHttpContext, @@ -52,35 +52,16 @@ class MyHttpModule : public CHttpModule } else { - // Allocate space to store the header. - pszUserAgent = (PCSTR) pHttpContext->AllocateRequestMemory( cchUserAgent + 1 ); - - // Test for an error. - if (pszUserAgent==NULL) - { - // Set the error status. - hr = HRESULT_FROM_WIN32(ERROR_NOT_ENOUGH_MEMORY); - pProvider->SetErrorStatus( hr ); - // End additional processing. - return RQ_NOTIFICATION_FINISH_REQUEST; - } - - // Retrieve the "User-Agent" header. - pszUserAgent = pHttpRequest->GetHeader("User-Agent",&cchUserAgent); - // Test for an error. - if (pszUserAgent!=NULL) - { - // Return the header information. - WriteResponseMessage(pHttpContext, - "User-Agent: ",pszUserAgent); - } + // Return the header information. + WriteResponseMessage(pHttpContext, + "User-Agent: ",pszUserAgent); } // Look for the "Accept-Language" header. pszAcceptLanguage = pHttpRequest->GetHeader(HttpHeaderAcceptLanguage,&cchAcceptLanguage); // The header length will be 0 if the header was not found. - if (cchAcceptLanguage == 0) + if (pszAcceptLanguage == NULL || cchAcceptLanguage == 0) { // Return a status message. WriteResponseMessage(pHttpContext, @@ -88,29 +69,9 @@ class MyHttpModule : public CHttpModule } else { - // Allocate space to store the header. - pszAcceptLanguage = (PCSTR) pHttpContext->AllocateRequestMemory( cchAcceptLanguage + 1 ); - - // Test for an error. - if (pszAcceptLanguage==NULL) - { - // Set the error status. - hr = HRESULT_FROM_WIN32(ERROR_NOT_ENOUGH_MEMORY); - pProvider->SetErrorStatus( hr ); - // End additional processing. - return RQ_NOTIFICATION_FINISH_REQUEST; - } - - // Retrieve the "Accept-Language" header. - pszAcceptLanguage = pHttpRequest->GetHeader(HttpHeaderAcceptLanguage,&cchAcceptLanguage); - - // Test for an error. - if (pszAcceptLanguage!=NULL) - { - // Return the header information. - WriteResponseMessage(pHttpContext, - "\nAccept-Language: ",pszAcceptLanguage); - } + // Return the header information. + WriteResponseMessage(pHttpContext, + "\nAccept-Language: ",pszAcceptLanguage); } // End additional processing. return RQ_NOTIFICATION_FINISH_REQUEST; @@ -131,7 +92,7 @@ class MyHttpModule : public CHttpModule { // Create an HRESULT to receive return values from methods. HRESULT hr; - + // Create a data chunk. HTTP_DATA_CHUNK dataChunk; // Set the chunk to a chunk in memory. @@ -182,7 +143,7 @@ class MyHttpModuleFactory : public IHttpModuleFactory public: HRESULT GetHttpModule( - OUT CHttpModule ** ppModule, + OUT CHttpModule ** ppModule, IN IModuleAllocator * pAllocator ) { @@ -204,7 +165,7 @@ class MyHttpModuleFactory : public IHttpModuleFactory pModule = NULL; // Return a success status. return S_OK; - } + } } void Terminate() @@ -233,4 +194,4 @@ RegisterModule( 0 ); } -// \ No newline at end of file +//