Skip to content

Conversation

@vighnesh-sawant
Copy link
Contributor

@vighnesh-sawant
Copy link
Contributor Author

vighnesh-sawant commented Oct 26, 2025

@kelson42 Please do have a look at the two pull requests

@vighnesh-sawant
Copy link
Contributor Author

Is this fine? It works well on kiwix-serve and kiwix-desktop.
I'll apply this to the remaining platforms soon!

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR adds functionality to return displayable HTTP addresses (URLs) from the server, supporting both IPv4 and IPv6 formats. The implementation includes proper URL formatting with port numbers and root prefixes.

Key Changes:

  • Added getRootPrefixOfDecodedURL() getter to InternalServer class
  • Implemented getDisplayableAddress() and getDisplayableAddress6() methods in Server class for formatted URL output

Reviewed Changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 6 comments.

File Description
src/server/internalServer.h Added getter method for accessing the root prefix of decoded URLs
src/server.cpp Implemented IPv4 and IPv6 displayable address formatting methods with fallback logic
include/server.h Added public method declarations for displayable address retrieval

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

src/server.cpp Outdated
std::string Server::getDisplayableAddress()
{
kiwix::IpAddress addresses = Server::getAddress();
if(addresses.addr.empty()) return getDisplayableAddress6(); //as a fallback return ipv6 address as one of them has to be set
Copy link

Copilot AI Oct 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These two methods can create an infinite recursion loop if both addr and addr6 are empty. Add a check to prevent mutual recursion or handle the case where both addresses are empty.

Copilot uses AI. Check for mistakes.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The server would not start right if both addresses are missing? Do i need to handle this?

src/server.cpp Outdated
std::string Server::getDisplayableAddress6()
{
kiwix::IpAddress addresses = Server::getAddress();
if(addresses.addr6.empty()) return getDisplayableAddress(); //as a fallback return ipv4 address as one of them has to be set
Copy link

Copilot AI Oct 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This creates potential for infinite recursion with getDisplayableAddress(). If both IPv4 and IPv6 addresses are empty, the methods will call each other indefinitely.

Copilot uses AI. Check for mistakes.
@vighnesh-sawant vighnesh-sawant force-pushed the standard-port-enhancment branch from 36c5df8 to 361c6ce Compare October 27, 2025 07:17
@vighnesh-sawant
Copy link
Contributor Author

@kelson42 Made the changes please ask copilot to rereview( It does not allow me to do that)

@kelson42 kelson42 requested a review from Copilot October 27, 2025 07:30
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

Copilot reviewed 4 out of 4 changed files in this pull request and generated 6 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

std::string InternalServer::getSuffixOfRootUrlWithPort()
{
std::string suffix;
if (m_port == 80)
Copy link

Copilot AI Oct 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The magic number 80 should be defined as a named constant (e.g., HTTP_DEFAULT_PORT) to improve code clarity and maintainability.

Copilot uses AI. Check for mistakes.
src/server.cpp Outdated

std::string Server::getDisplayableAddress()
{
kiwix::IpAddress addresses = Server::getAddress();
Copy link

Copilot AI Oct 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Trailing whitespace should be removed from this line.

Suggested change
kiwix::IpAddress addresses = Server::getAddress();
kiwix::IpAddress addresses = Server::getAddress();

Copilot uses AI. Check for mistakes.
src/server.cpp Outdated
std::string Server::getDisplayableAddress()
{
kiwix::IpAddress addresses = Server::getAddress();
if(addresses.addr.empty()) return getDisplayableAddress6(); //as a fallback return ipv6 address as one of them has to be set
Copy link

Copilot AI Oct 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These mutual fallback calls between getDisplayableAddress() and getDisplayableAddress6() create infinite recursion when both addr and addr6 are empty. Add a check to prevent this or return an error/empty string when both addresses are unavailable.

Copilot uses AI. Check for mistakes.
src/server.cpp Outdated
std::string Server::getDisplayableAddress6()
{
kiwix::IpAddress addresses = Server::getAddress();
if(addresses.addr6.empty()) return getDisplayableAddress(); //as a fallback return ipv4 address as one of them has to be set
Copy link

Copilot AI Oct 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These mutual fallback calls between getDisplayableAddress() and getDisplayableAddress6() create infinite recursion when both addr and addr6 are empty. Add a check to prevent this or return an error/empty string when both addresses are unavailable.

Copilot uses AI. Check for mistakes.
src/server.cpp Outdated
if(addresses.addr.empty()) return getDisplayableAddress6(); //as a fallback return ipv6 address as one of them has to be set

std::string result = "http://";
std::string suffix=mp_server->getSuffixOfRootUrlWithPort();
Copy link

Copilot AI Oct 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing space around the assignment operator. Should be std::string suffix = mp_server->getSuffixOfRootUrlWithPort(); for consistency with coding style.

Copilot uses AI. Check for mistakes.
src/server.cpp Outdated
if(addresses.addr6.empty()) return getDisplayableAddress(); //as a fallback return ipv4 address as one of them has to be set

std::string result = "http://";
std::string suffix=mp_server->getSuffixOfRootUrlWithPort();
Copy link

Copilot AI Oct 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing space around the assignment operator. Should be std::string suffix = mp_server->getSuffixOfRootUrlWithPort(); for consistency with coding style.

Suggested change
std::string suffix=mp_server->getSuffixOfRootUrlWithPort();
std::string suffix = mp_server->getSuffixOfRootUrlWithPort();

Copilot uses AI. Check for mistakes.
@codecov
Copy link

codecov bot commented Oct 27, 2025

Codecov Report

❌ Patch coverage is 0% with 22 lines in your changes missing coverage. Please review.
✅ Project coverage is 42.84%. Comparing base (dd09e3c) to head (361c6ce).

Files with missing lines Patch % Lines
src/server.cpp 0.00% 15 Missing ⚠️
src/server/internalServer.cpp 0.00% 7 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1234      +/-   ##
==========================================
- Coverage   43.05%   42.84%   -0.21%     
==========================================
  Files          60       60              
  Lines        4734     4757      +23     
  Branches     2492     2505      +13     
==========================================
  Hits         2038     2038              
- Misses       1075     1098      +23     
  Partials     1621     1621              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@vighnesh-sawant
Copy link
Contributor Author

@kelson42
I think the PR should be ready now

@kelson42 kelson42 requested a review from Copilot October 27, 2025 12:10
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

Copilot reviewed 4 out of 4 changed files in this pull request and generated no new comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

include/server.h Outdated
Comment on lines 72 to 73
std::string getDisplayableAddress();
std::string getDisplayableAddress6();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. These methods can (and, therefore, should) be declared const
  2. I would name these functions getServerAccessUrl() and getServerAccessIPv6Url(). BTW, maybe it's better to have a single public function std::vector<std::string> getServerAccessUrls() const? @kelson42

src/server.cpp Outdated
kiwix::IpAddress addresses = Server::getAddress();
if(addresses.addr.empty()) return getDisplayableAddress6(); //as a fallback return ipv6 address as one of them has to be set

std::string result = "http://";
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This variable is not justified. return "http://" + addresses.addr + suffix; is quite readable.

src/server.cpp Outdated

std::string Server::getDisplayableAddress()
{
kiwix::IpAddress addresses = Server::getAddress();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I advise you to start practising defensive programming. A powerful technique is to declare const whatever can be declared const, which protects you against carelessly modifying things that are supposed to be immutable.

In particular, both addresses and suffix in this function can be declared const.

Comment on lines 276 to 287
std::string InternalServer::getSuffixOfRootUrlWithPort()
{
std::string suffix;
if (m_port == HTTP_DEFAULT_PORT)
{
suffix = m_rootPrefixOfDecodedURL;
} else
{
suffix = ":" + std::to_string(m_port) + m_rootPrefixOfDecodedURL;
}
return suffix;
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please replace this member function of InternalServer with a free function in an unnamed namespace in server.cpp std::string makeServerUrl(std::string host, int port, std::string root).

@vighnesh-sawant
Copy link
Contributor Author

Incorporated all changes.
i have kept the httpPrefix variable since the other alternative will be to convert "[" to std::string
what is better?
httpPrefix + "[" + ....
"http://" + std::string("[") .....

src/server.cpp Outdated
std::vector<std::string> serverAccessUrls;
const kiwix::IpAddress addresses = Server::getAddress();
const int port = Server::getPort();
const std::string root = mp_server->getRootPrefixOfDecodedURL();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is available in Server as m_root. So there is no need for the getRootPrefixOfDecodedURL() accessor in InternalServer.

Copy link
Contributor Author

@vighnesh-sawant vighnesh-sawant Oct 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

m_root does not get normalized completely
m_rootPrefixOfDecodedURL of internal server is normalized and decoded
m_root of internalserver gets encoded

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see. I was lost in the multitude of root and m_root variables.

Regarding normalization of root location - Server::setRoot() performs some steps in that direction but since it contains a loophole InternalServer has to correct. So I think that the code will be simpler if normalization is fully performed in Server::setRoot() and InternalServer receives a fully normalized value. Please make this change in a separate refactoring commit.

src/server.cpp Outdated
Comment on lines 125 to 129
const kiwix::IpAddress addresses = Server::getAddress();
const int port = Server::getPort();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why don't you use m_addr and m_port directly?

Copy link
Contributor Author

@vighnesh-sawant vighnesh-sawant Oct 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

m_addr of server class may be left empty so it wont work correctly
m_addr of internal server gets initialized correctly thats why
m_port works but i think its better they both are same

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right regarding m_addr. This is a pitfall (I tell you so after emerging from it with your help 😃 ). It would be better if Server::m_addr and InternalServer::m_addr don't diverge. Please update Server::m_addr in Server::start() and make Server::getAddress() a simple accessor. Let this be another refactoring commit. Then you can use m_addr and m_port directly in this function.

@vighnesh-sawant vighnesh-sawant force-pushed the standard-port-enhancment branch 2 times, most recently from ea9f93d to ce2c929 Compare October 27, 2025 18:11
@vighnesh-sawant vighnesh-sawant force-pushed the standard-port-enhancment branch 3 times, most recently from f549764 to 200bf7a Compare October 27, 2025 18:59
@vighnesh-sawant
Copy link
Contributor Author

Please look at the replies to the comments!
Thank you @veloman-yunkan for guiding me, I learnt a lot. The code looks so much better.
After this I'll go and improve this #1229 also

Copy link
Collaborator

@veloman-yunkan veloman-yunkan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please improve the PR by adding the two refactoring changes proposed in my replies to your clarifications in the previous review. There should be three commits:

  1. Server::m_addr stays in sync with InternalServer::m_addr
  2. Server::m_root is fully normalized
  3. Server::getServerAccessUrls()

@vighnesh-sawant vighnesh-sawant force-pushed the standard-port-enhancment branch 2 times, most recently from 7aada0c to 5914ecb Compare October 30, 2025 18:07
@vighnesh-sawant
Copy link
Contributor Author

Made the three commits as you suggested!

Copy link
Collaborator

@veloman-yunkan veloman-yunkan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are almost there. Please remove the commit that sneaked into this PR from #1229 and address the last two minor issues.

src/server.cpp Outdated
Comment on lines 134 to 136
const kiwix::IpAddress addresses = m_addr;
const int port = m_port;
const std::string root = m_root;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now these local variables are not justified. Use m_addr, m_port and m_root below directly.

src/server.cpp Outdated
return result;
}

}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am bringing to your attention that GitHub's diff visualization makes it conspicuous that the last line here doesn't end with a new-line symbol. Let's pay our tribute to all the man-hours invested in implementing that feature by addressing this otherwise invisible formatting issue.

@kelson42
Copy link
Collaborator

@vighnesh-sawant If fixed quickly, glad to ship it in next libkiwix realease. Only a few hours left.

@vighnesh-sawant vighnesh-sawant force-pushed the standard-port-enhancment branch 3 times, most recently from 10df8ce to c238181 Compare October 31, 2025 12:38
@vighnesh-sawant
Copy link
Contributor Author

@kelson42 Addressed the issues

@kelson42 kelson42 added this to the 14.1.0 milestone Oct 31, 2025
@kelson42
Copy link
Collaborator

@vighnesh-sawant Thx! Waiting @veloman-yunkan's approval and will be merged.

@kelson42
Copy link
Collaborator

Merging, so I guess we can move on the PR at the reader level now.

@kelson42 kelson42 merged commit 17f0ad2 into kiwix:main Oct 31, 2025
13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Hide port 80 on URL

3 participants