feat(host_name): add free function returning the local hostname#248
Conversation
Adds boost::corosio::host_name(), a synchronous free function returning the local machine's hostname as a UTF-8 std::string. Common uses: logging, default Host: headers, cluster registration, identifying a node. A single overload that throws std::runtime_error on failure. No std::error_code variant: failures of the underlying syscalls are pathological on a configured machine, and the throwing form is sufficient. POSIX path uses gethostname(2) with a 256-byte stack buffer, which comfortably exceeds the _POSIX_HOST_NAME_MAX floor of 255 and every mainstream OS's actual cap. errno is captured immediately on failure. POSIX does not guarantee NUL termination on truncation, so the implementation checks for a NUL anywhere in the buffer and throws otherwise. Windows path uses GetComputerNameExW(ComputerNameDnsHostname, ...) followed by WideCharToMultiByte(CP_UTF8, ...) to produce UTF-8. Chosen over winsock gethostname() so the function works without WSAStartup(); in corosio winsock is initialized lazily inside io_context, so a winsock-based implementation would tie host_name() to io_context lifetime, contradicting the "no io_context needed" design goal. Adds five unit tests covering: non-empty result, stability across calls, reasonable length (<= 255 octets), no-io_context-needed (load-bearing regression guard for the Windows implementation choice), and charset sanity (catches UTF-16 -> UTF-8 regressions).
|
An automated preview of the documentation is available at https://248.corosio.prtest3.cppalliance.org/index.html If more commits are pushed to the pull request, the docs will rebuild at the same URL. 2026-05-13 18:38:28 UTC |
|
GCOVR code coverage report https://248.corosio.prtest3.cppalliance.org/gcovr/index.html Build time: 2026-05-13 18:46:52 UTC |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## develop #248 +/- ##
========================================
Coverage 77.71% 77.71%
========================================
Files 96 96
Lines 7298 7298
Branches 1787 1787
========================================
Hits 5672 5672
Misses 1108 1108
Partials 518 518 Continue to review full report in Codecov by Sentry.
🚀 New features to boost your workflow:
|
Adds boost::corosio::host_name(), a synchronous free function returning the local machine's hostname as a UTF-8 std::string. Common uses: logging, default Host: headers, cluster registration, identifying a node.
A single overload that throws std::runtime_error on failure. No std::error_code variant: failures of the underlying syscalls are pathological on a configured machine, and the throwing form is sufficient.
POSIX path uses gethostname(2). The buffer is sized via sysconf(_SC_HOST_NAME_MAX), falling back to HOST_NAME_MAX or 256. errno is captured immediately on failure. POSIX does not guarantee NUL termination on truncation, so the implementation verifies the buffer's final byte is NUL and throws otherwise.
Windows path uses GetComputerNameExW(ComputerNameDnsHostname, ...) followed by WideCharToMultiByte(CP_UTF8, ...) to produce UTF-8. Chosen over winsock gethostname() so the function works without WSAStartup(); in corosio winsock is initialized lazily inside io_context, so a winsock-based implementation would tie host_name() to io_context lifetime, contradicting the "no io_context needed" design goal.
Adds five unit tests covering: non-empty result, stability across calls, reasonable length (<= 255 octets), no-io_context-needed (load-bearing regression guard for the Windows implementation choice), and charset sanity (catches UTF-16 -> UTF-8 regressions).
Resolves #247.