Skip to content

Commit ff27daa

Browse files
Merge pull request #1098 from kamrey2/patch-1
Removing unnecessary memory allocations.
2 parents f36126b + 4b44e45 commit ff27daa

File tree

1 file changed

+12
-51
lines changed

1 file changed

+12
-51
lines changed

samples/snippets/cpp/VS_Snippets_IIS/IIS7/IHttpRequestGetHeader/cpp/IHttpRequestGetHeader.cpp

Lines changed: 12 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -44,73 +44,34 @@ class MyHttpModule : public CHttpModule
4444
pszUserAgent = pHttpRequest->GetHeader("User-Agent",&cchUserAgent);
4545

4646
// The header length will be 0 if the header was not found.
47-
if (cchUserAgent == 0)
47+
if (pszUserAgent == NULL || cchUserAgent == 0)
4848
{
4949
// Return a status message.
5050
WriteResponseMessage(pHttpContext,
5151
"User-Agent: ","(none)");
5252
}
5353
else
5454
{
55-
// Allocate space to store the header.
56-
pszUserAgent = (PCSTR) pHttpContext->AllocateRequestMemory( cchUserAgent + 1 );
57-
58-
// Test for an error.
59-
if (pszUserAgent==NULL)
60-
{
61-
// Set the error status.
62-
hr = HRESULT_FROM_WIN32(ERROR_NOT_ENOUGH_MEMORY);
63-
pProvider->SetErrorStatus( hr );
64-
// End additional processing.
65-
return RQ_NOTIFICATION_FINISH_REQUEST;
66-
}
67-
68-
// Retrieve the "User-Agent" header.
69-
pszUserAgent = pHttpRequest->GetHeader("User-Agent",&cchUserAgent);
70-
// Test for an error.
71-
if (pszUserAgent!=NULL)
72-
{
73-
// Return the header information.
74-
WriteResponseMessage(pHttpContext,
75-
"User-Agent: ",pszUserAgent);
76-
}
55+
// Return the header information.
56+
WriteResponseMessage(pHttpContext,
57+
"User-Agent: ",pszUserAgent);
7758
}
7859

7960
// Look for the "Accept-Language" header.
8061
pszAcceptLanguage = pHttpRequest->GetHeader(HttpHeaderAcceptLanguage,&cchAcceptLanguage);
8162

8263
// The header length will be 0 if the header was not found.
83-
if (cchAcceptLanguage == 0)
64+
if (pszAcceptLanguage == NULL || cchAcceptLanguage == 0)
8465
{
8566
// Return a status message.
8667
WriteResponseMessage(pHttpContext,
8768
"\nAccept-Language: ","(none)");
8869
}
8970
else
9071
{
91-
// Allocate space to store the header.
92-
pszAcceptLanguage = (PCSTR) pHttpContext->AllocateRequestMemory( cchAcceptLanguage + 1 );
93-
94-
// Test for an error.
95-
if (pszAcceptLanguage==NULL)
96-
{
97-
// Set the error status.
98-
hr = HRESULT_FROM_WIN32(ERROR_NOT_ENOUGH_MEMORY);
99-
pProvider->SetErrorStatus( hr );
100-
// End additional processing.
101-
return RQ_NOTIFICATION_FINISH_REQUEST;
102-
}
103-
104-
// Retrieve the "Accept-Language" header.
105-
pszAcceptLanguage = pHttpRequest->GetHeader(HttpHeaderAcceptLanguage,&cchAcceptLanguage);
106-
107-
// Test for an error.
108-
if (pszAcceptLanguage!=NULL)
109-
{
110-
// Return the header information.
111-
WriteResponseMessage(pHttpContext,
112-
"\nAccept-Language: ",pszAcceptLanguage);
113-
}
72+
// Return the header information.
73+
WriteResponseMessage(pHttpContext,
74+
"\nAccept-Language: ",pszAcceptLanguage);
11475
}
11576
// End additional processing.
11677
return RQ_NOTIFICATION_FINISH_REQUEST;
@@ -131,7 +92,7 @@ class MyHttpModule : public CHttpModule
13192
{
13293
// Create an HRESULT to receive return values from methods.
13394
HRESULT hr;
134-
95+
13596
// Create a data chunk.
13697
HTTP_DATA_CHUNK dataChunk;
13798
// Set the chunk to a chunk in memory.
@@ -182,7 +143,7 @@ class MyHttpModuleFactory : public IHttpModuleFactory
182143
public:
183144
HRESULT
184145
GetHttpModule(
185-
OUT CHttpModule ** ppModule,
146+
OUT CHttpModule ** ppModule,
186147
IN IModuleAllocator * pAllocator
187148
)
188149
{
@@ -204,7 +165,7 @@ class MyHttpModuleFactory : public IHttpModuleFactory
204165
pModule = NULL;
205166
// Return a success status.
206167
return S_OK;
207-
}
168+
}
208169
}
209170

210171
void Terminate()
@@ -233,4 +194,4 @@ RegisterModule(
233194
0
234195
);
235196
}
236-
// </Snippet1>
197+
// </Snippet1>

0 commit comments

Comments
 (0)