Search code examples
c++visual-c++utf-8iis-7httpmodule

UTF-8 Support in VC++ for web application


I have just started with creating native modules for IIS7. I am using one of the Hello World example from http://msdn.microsoft.com/en-us/library/ms689348(v=vs.90).aspx . Example code written there is working fine. Now I am trying to change this code to support UTF-8 encoding to be displayed in browser. I have made following changes:

//1. Changed here to include ;charset=UTF-8 in Http Header Content Type
pHttpResponse->SetHeader(HttpHeaderContentType, "text/plain;charset=UTF-8", (USHORT)strlen("text/plain;charset=UTF-8"), TRUE);
//2. Changed here data type as PCTSTR which is PCWSTR(16-bit) since I have UNICODE defined.
PCTSTR pszBuffer = L"Hello World!";
HTTP_DATA_CHUNK dataChunk;
dataChunk.DataChunkType = HttpDataChunkFromMemory;
DWORD cbSent;
dataChunk.FromMemory.pBuffer = (PVOID)pszBuffer;
//3. Changed here to calculate length using wcslen.
dataChunk.FromMemory.BufferLength = (USHORT)wcslen(pszBuffer);
hr = pHttpResponse->WriteEntityChunks(&dataChunk, 1, FALSE, TRUE, &cbSent);

My first doubt is how to make pszBuffer UTF-8 string?

Second doubt is how to calculate length of this correctly (code on comment number 3)?

Also is there any alternate way to write to response instead of creating buffer, chunck etc.?


Solution

  • If you want to make the string literal to be UTF-8, set encoding of source file to UTF-8 without BOM. Then you can use normal string literal as UTF-8. Use WideCharToMultiByte and MultiByteToWideChar to convert between UTF-16 and UTF-8 string.

    If your project using ATL, you can use string conversion macros that ATL provide to easily convert between UTF-16 and UTF-8; see http://msdn.microsoft.com/en-us/library/87zae4a3.aspx for how to use it.

    If you need the number of bytes in string, use strlen on UTF-8 string.

    Another way to write response are make use of HTTP_DATA_CHUNK::FromFileHandle instead HTTP_DATA_CHUNK::FromMemory. HTTP_DATA_CHUNK::FromFileHandle accept a file handle to be used as response.