-
-
Notifications
You must be signed in to change notification settings - Fork 70
Add functions which return displayable addresses #1234
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add functions which return displayable addresses #1234
Conversation
|
@kelson42 Please do have a look at the two pull requests |
|
Is this fine? It works well on kiwix-serve and kiwix-desktop. |
There was a problem hiding this 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 toInternalServerclass - Implemented
getDisplayableAddress()andgetDisplayableAddress6()methods inServerclass 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 |
Copilot
AI
Oct 27, 2025
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 |
Copilot
AI
Oct 27, 2025
There was a problem hiding this comment.
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.
36c5df8 to
361c6ce
Compare
|
@kelson42 Made the changes please ask copilot to rereview( It does not allow me to do that) |
There was a problem hiding this 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.
src/server/internalServer.cpp
Outdated
| std::string InternalServer::getSuffixOfRootUrlWithPort() | ||
| { | ||
| std::string suffix; | ||
| if (m_port == 80) |
Copilot
AI
Oct 27, 2025
There was a problem hiding this comment.
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.
src/server.cpp
Outdated
|
|
||
| std::string Server::getDisplayableAddress() | ||
| { | ||
| kiwix::IpAddress addresses = Server::getAddress(); |
Copilot
AI
Oct 27, 2025
There was a problem hiding this comment.
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.
| kiwix::IpAddress addresses = Server::getAddress(); | |
| kiwix::IpAddress addresses = Server::getAddress(); |
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 |
Copilot
AI
Oct 27, 2025
There was a problem hiding this comment.
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.
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 |
Copilot
AI
Oct 27, 2025
There was a problem hiding this comment.
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.
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(); |
Copilot
AI
Oct 27, 2025
There was a problem hiding this comment.
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.
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(); |
Copilot
AI
Oct 27, 2025
There was a problem hiding this comment.
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.
| std::string suffix=mp_server->getSuffixOfRootUrlWithPort(); | |
| std::string suffix = mp_server->getSuffixOfRootUrlWithPort(); |
Codecov Report❌ Patch coverage is
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. 🚀 New features to boost your workflow:
|
361c6ce to
41d8d9b
Compare
|
@kelson42 |
There was a problem hiding this 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
| std::string getDisplayableAddress(); | ||
| std::string getDisplayableAddress6(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- These methods can (and, therefore, should) be declared
const - I would name these functions
getServerAccessUrl()andgetServerAccessIPv6Url(). BTW, maybe it's better to have a single public functionstd::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://"; |
There was a problem hiding this comment.
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(); |
There was a problem hiding this comment.
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.
src/server/internalServer.cpp
Outdated
| 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; | ||
| } |
There was a problem hiding this comment.
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).
166db82 to
91efcdc
Compare
|
Incorporated all changes. |
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(); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
| const kiwix::IpAddress addresses = Server::getAddress(); | ||
| const int port = Server::getPort(); |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
ea9f93d to
ce2c929
Compare
f549764 to
200bf7a
Compare
|
Please look at the replies to the comments! |
200bf7a to
9dd0853
Compare
veloman-yunkan
left a comment
There was a problem hiding this 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:
Server::m_addrstays in sync withInternalServer::m_addrServer::m_rootis fully normalizedServer::getServerAccessUrls()
7aada0c to
5914ecb
Compare
|
Made the three commits as you suggested! |
veloman-yunkan
left a comment
There was a problem hiding this 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
| const kiwix::IpAddress addresses = m_addr; | ||
| const int port = m_port; | ||
| const std::string root = m_root; |
There was a problem hiding this comment.
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; | ||
| } | ||
|
|
||
| } |
There was a problem hiding this comment.
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.
|
@vighnesh-sawant If fixed quickly, glad to ship it in next libkiwix realease. Only a few hours left. |
10df8ce to
c238181
Compare
c238181 to
4928509
Compare
|
@kelson42 Addressed the issues |
|
@vighnesh-sawant Thx! Waiting @veloman-yunkan's approval and will be merged. |
|
Merging, so I guess we can move on the PR at the reader level now. |
Fixes kiwix/kiwix-tools#764
Reference ->kiwix/kiwix-tools#763