Skip to content

Append '/' to an URL with empty path in HTTPClient::begin #5634

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

Merged
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions libraries/HTTPClient/src/HTTPClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,10 @@ bool HTTPClient::beginInternal(String url, const char* expectedProtocol)
url.remove(0, (index + 3)); // remove http:// or https://

index = url.indexOf('/');
if (index == -1) {
log_e("the path of url must not be empty");
return false;
Copy link
Collaborator

@atanisoft atanisoft Sep 8, 2021

Choose a reason for hiding this comment

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

I agree a check is needed here to prevent using -1 for index, but I don't think it should be an error. If you type in "example.com" into a browser it defaults the path to / for you. Likely this code should do the same in this case. Perhaps something like this inside this new condition block:

  url += '/';
  index = url.indexOf('/');

It will incur the cost of calling indexOf a second time but I don't think there are many options around this.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think so too. I'll fix this.

I don't think there are many options around this.

The method length can be also used.

}
String host = url.substring(0, index);
url.remove(0, index); // remove host part

Expand Down