Skip to content

Commit b4c2908

Browse files
Revert "Removing unnecessary memory allocations."
1 parent ff27daa commit b4c2908

File tree

1 file changed

+51
-12
lines changed

1 file changed

+51
-12
lines changed

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

Lines changed: 51 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -44,34 +44,73 @@ 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 (pszUserAgent == NULL || cchUserAgent == 0)
47+
if (cchUserAgent == 0)
4848
{
4949
// Return a status message.
5050
WriteResponseMessage(pHttpContext,
5151
"User-Agent: ","(none)");
5252
}
5353
else
5454
{
55-
// Return the header information.
56-
WriteResponseMessage(pHttpContext,
57-
"User-Agent: ",pszUserAgent);
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+
}
5877
}
5978

6079
// Look for the "Accept-Language" header.
6180
pszAcceptLanguage = pHttpRequest->GetHeader(HttpHeaderAcceptLanguage,&cchAcceptLanguage);
6281

6382
// The header length will be 0 if the header was not found.
64-
if (pszAcceptLanguage == NULL || cchAcceptLanguage == 0)
83+
if (cchAcceptLanguage == 0)
6584
{
6685
// Return a status message.
6786
WriteResponseMessage(pHttpContext,
6887
"\nAccept-Language: ","(none)");
6988
}
7089
else
7190
{
72-
// Return the header information.
73-
WriteResponseMessage(pHttpContext,
74-
"\nAccept-Language: ",pszAcceptLanguage);
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+
}
75114
}
76115
// End additional processing.
77116
return RQ_NOTIFICATION_FINISH_REQUEST;
@@ -92,7 +131,7 @@ class MyHttpModule : public CHttpModule
92131
{
93132
// Create an HRESULT to receive return values from methods.
94133
HRESULT hr;
95-
134+
96135
// Create a data chunk.
97136
HTTP_DATA_CHUNK dataChunk;
98137
// Set the chunk to a chunk in memory.
@@ -143,7 +182,7 @@ class MyHttpModuleFactory : public IHttpModuleFactory
143182
public:
144183
HRESULT
145184
GetHttpModule(
146-
OUT CHttpModule ** ppModule,
185+
OUT CHttpModule ** ppModule,
147186
IN IModuleAllocator * pAllocator
148187
)
149188
{
@@ -165,7 +204,7 @@ class MyHttpModuleFactory : public IHttpModuleFactory
165204
pModule = NULL;
166205
// Return a success status.
167206
return S_OK;
168-
}
207+
}
169208
}
170209

171210
void Terminate()
@@ -194,4 +233,4 @@ RegisterModule(
194233
0
195234
);
196235
}
197-
// </Snippet1>
236+
// </Snippet1>

0 commit comments

Comments
 (0)