aboutsummaryrefslogtreecommitdiff
path: root/zenhttp/httpsys.cpp
diff options
context:
space:
mode:
authorStefan Boberg <[email protected]>2021-09-28 23:24:45 +0200
committerStefan Boberg <[email protected]>2021-09-28 23:24:45 +0200
commit46eebda16a2c3a66bb8ca48488e49d659556d745 (patch)
treec5fb2662c2cf5e3fb4efc2e263e8663d37723747 /zenhttp/httpsys.cpp
parentRemoved IsPointerToStack() (diff)
downloadzen-46eebda16a2c3a66bb8ca48488e49d659556d745.tar.xz
zen-46eebda16a2c3a66bb8ca48488e49d659556d745.zip
http: ReasonStringForHttpResultCode returns string_view to avoid strlen
Diffstat (limited to 'zenhttp/httpsys.cpp')
-rw-r--r--zenhttp/httpsys.cpp105
1 files changed, 51 insertions, 54 deletions
diff --git a/zenhttp/httpsys.cpp b/zenhttp/httpsys.cpp
index bccff24ab..9b2e7f832 100644
--- a/zenhttp/httpsys.cpp
+++ b/zenhttp/httpsys.cpp
@@ -18,47 +18,43 @@
# pragma comment(lib, "httpapi.lib")
std::wstring
-UTF8_to_wstring(const char* in)
+UTF8_to_UTF16(const char* InPtr)
{
- std::wstring out;
- unsigned int codepoint;
+ std::wstring OutString;
+ unsigned int Codepoint;
- while (*in != 0)
+ while (*InPtr != 0)
{
- unsigned char ch = static_cast<unsigned char>(*in);
-
- if (ch <= 0x7f)
- codepoint = ch;
- else if (ch <= 0xbf)
- codepoint = (codepoint << 6) | (ch & 0x3f);
- else if (ch <= 0xdf)
- codepoint = ch & 0x1f;
- else if (ch <= 0xef)
- codepoint = ch & 0x0f;
+ unsigned char InChar = static_cast<unsigned char>(*InPtr);
+
+ if (InChar <= 0x7f)
+ Codepoint = InChar;
+ else if (InChar <= 0xbf)
+ Codepoint = (Codepoint << 6) | (InChar & 0x3f);
+ else if (InChar <= 0xdf)
+ Codepoint = InChar & 0x1f;
+ else if (InChar <= 0xef)
+ Codepoint = InChar & 0x0f;
else
- codepoint = ch & 0x07;
+ Codepoint = InChar & 0x07;
- ++in;
+ ++InPtr;
- if (((*in & 0xc0) != 0x80) && (codepoint <= 0x10ffff))
+ if (((*InPtr & 0xc0) != 0x80) && (Codepoint <= 0x10ffff))
{
- if constexpr (sizeof(wchar_t) > 2)
+ if (Codepoint > 0xffff)
{
- out.append(1, static_cast<wchar_t>(codepoint));
+ OutString.append(1, static_cast<wchar_t>(0xd800 + (Codepoint >> 10)));
+ OutString.append(1, static_cast<wchar_t>(0xdc00 + (Codepoint & 0x03ff)));
}
- else if (codepoint > 0xffff)
+ else if (Codepoint < 0xd800 || Codepoint >= 0xe000)
{
- out.append(1, static_cast<wchar_t>(0xd800 + (codepoint >> 10)));
- out.append(1, static_cast<wchar_t>(0xdc00 + (codepoint & 0x03ff)));
- }
- else if (codepoint < 0xd800 || codepoint >= 0xe000)
- {
- out.append(1, static_cast<wchar_t>(codepoint));
+ OutString.append(1, static_cast<wchar_t>(Codepoint));
}
}
}
- return out;
+ return OutString;
}
namespace zen {
@@ -493,6 +489,7 @@ HttpMessageResponseRequest::IssueRequest(std::error_code& ErrorCode)
// Computer\HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\HTTP\Parameters
//
// Set DisableServerHeader to 1 to disable suffix, or 2 to disable the header altogether
+ // (only the latter appears to do anything in my testing, on Windows 10).
//
// (reference https://docs.microsoft.com/en-us/archive/blogs/dsnotes/wswcf-remove-server-header)
//
@@ -519,9 +516,11 @@ HttpMessageResponseRequest::IssueRequest(std::error_code& ErrorCode)
ContentTypeHeader->pRawValue = ContentTypeString.data();
ContentTypeHeader->RawValueLength = (USHORT)ContentTypeString.size();
+ std::string_view ReasonString = ReasonStringForHttpResultCode(m_ResponseCode);
+
HttpResponse.StatusCode = m_ResponseCode;
- HttpResponse.pReason = ReasonStringForHttpResultCode(m_ResponseCode);
- HttpResponse.ReasonLength = (USHORT)strlen(HttpResponse.pReason);
+ HttpResponse.pReason = ReasonString.data();
+ HttpResponse.ReasonLength = (USHORT)ReasonString.size();
// Cache policy
@@ -823,18 +822,18 @@ HttpSysServer::RegisterService(const char* UrlPath, HttpService& Service)
++UrlPath;
}
- const std::wstring Path16 = UTF8_to_wstring(UrlPath);
- Service.SetUriPrefixLength(Path16.size() + 1 /* leading slash */);
+ const std::wstring PathUtf16 = UTF8_to_UTF16(UrlPath);
+ Service.SetUriPrefixLength(PathUtf16.size() + 1 /* leading slash */);
// Convert to wide string
- std::wstring Url16 = m_BaseUri + Path16;
+ std::wstring Url16 = m_BaseUri + PathUtf16;
ULONG Result = HttpAddUrlToUrlGroup(m_HttpUrlGroupId, Url16.c_str(), HTTP_URL_CONTEXT(&Service), 0 /* Reserved */);
if (Result != NO_ERROR)
{
- ZEN_ERROR("HttpAddUrlToUrlGroup failed with result {}", Result);
+ ZEN_ERROR("HttpAddUrlToUrlGroup failed with result: '{}'", GetSystemErrorAsString(Result));
return;
}
@@ -850,17 +849,17 @@ HttpSysServer::UnregisterService(const char* UrlPath, HttpService& Service)
++UrlPath;
}
- const std::wstring Path16 = UTF8_to_wstring(UrlPath);
+ const std::wstring PathUtf16 = UTF8_to_UTF16(UrlPath);
// Convert to wide string
- std::wstring Url16 = m_BaseUri + Path16;
+ std::wstring Url16 = m_BaseUri + PathUtf16;
ULONG Result = HttpRemoveUrlFromUrlGroup(m_HttpUrlGroupId, Url16.c_str(), 0);
if (Result != NO_ERROR)
{
- ZEN_ERROR("HttpRemoveUrlFromUrlGroup failed with result {}", Result);
+ ZEN_ERROR("HttpRemoveUrlFromUrlGroup failed with result: '{}'", GetSystemErrorAsString(Result));
}
}
@@ -1269,7 +1268,7 @@ InitialRequestHandler::IssueRequest(std::error_code& ErrorCode)
ErrorCode = MakeErrorCode(HttpApiResult);
- ZEN_ERROR("HttpReceiveHttpRequest failed, error {}", ErrorCode.message());
+ ZEN_ERROR("HttpReceiveHttpRequest failed, error: '{}'", ErrorCode.message());
return;
}
@@ -1293,7 +1292,7 @@ InitialRequestHandler::HandleCompletion(ULONG IoResult, ULONG_PTR NumberOfBytesT
break;
}
- // Route requests
+ // Route request
try
{
@@ -1400,28 +1399,26 @@ InitialRequestHandler::HandleCompletion(ULONG IoResult, ULONG_PTR NumberOfBytesT
m_CurrentPayloadOffset += NumberOfBytesTransferred;
}
- if (m_CurrentPayloadOffset == m_ContentLength)
+ if (m_CurrentPayloadOffset != m_ContentLength)
{
- m_PayloadBuffer.MakeImmutable();
+ // Body not complete, issue another read request to receive more body data
+ return this;
+ }
- // Body received completely - call request handler
+ // Request body received completely
- HttpSysServerRequest& ThisRequest = Transaction().InvokeRequestHandler(*Service, m_PayloadBuffer);
+ m_PayloadBuffer.MakeImmutable();
- if (!ThisRequest.IsHandled())
- {
- return new HttpMessageResponseRequest(Transaction(), 404, "Not found"sv);
- }
+ HttpSysServerRequest& ThisRequest = Transaction().InvokeRequestHandler(*Service, m_PayloadBuffer);
- if (HttpMessageResponseRequest* Response = ThisRequest.m_Response)
- {
- return Response;
- }
+ if (!ThisRequest.IsHandled())
+ {
+ return new HttpMessageResponseRequest(Transaction(), 404, "Not found"sv);
}
- else
+
+ if (HttpMessageResponseRequest* Response = ThisRequest.m_Response)
{
- // Issue a read request for more body data
- return this;
+ return Response;
}
}
@@ -1430,7 +1427,7 @@ InitialRequestHandler::HandleCompletion(ULONG IoResult, ULONG_PTR NumberOfBytesT
}
catch (std::exception& ex)
{
- // TODO provide more meaningful error output
+ ZEN_ERROR("Caught exception while handling request: '{}'", ex.what());
return new HttpMessageResponseRequest(Transaction(), 500, ex.what());
}