Draft
feat: auto-assign free port when setPort(0) is called#848
Conversation
Open
Copilot
AI
changed the title
[WIP] Add automatic application of free port functionality
feat: auto-assign free port when setPort(0) is called
Jul 10, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
setPort()with no arguments (port=0) previously had no effect — the server would not listen on any port. This adds OS-level free-port auto-assignment by binding to port 0, then retrieving the actual assigned port viagetsockname().Semantic changes
port/https_portdefault:0→-1—-1now means "not configured";0means "let OS assign a free port" (consistent with standard socket API convention)setPort()ssl_port default:0→-1— preventssetPort(8080)from inadvertently resettinghttps_portstart()return value — now returns the actual HTTP port number on success (was always0), enablingint real_port = server.start()Usage
Affected files
http/server/HttpServer.h— default port sentinel,setPort()signature,start()return value + doc commenthttp/server/HttpServer.cpp— conditionport > 0→port >= 0;getsockname()call afterListen(0, ...);#include "hsocket.h"added; error logged ongetsocknamefailureexamples/httpd/httpd.cpp— "no port configured" guard:== 0→< 0docs/cn/HttpServer.md— updated signatures andstart()description