SmingHub / Sming

Sming - powerful open source framework simplifying the creation of embedded C++ applications.
https://sming.readthedocs.io
GNU Lesser General Public License v3.0
1.47k stars 348 forks source link

Fixes to Url default handling and query parameters #2828

Closed mikee47 closed 3 months ago

mikee47 commented 3 months ago

This PR addresses some inconsistencies with the Url class.

Ensure Url provides default port for default scheme

When building a URL the Scheme defaults to HTTP if not specified. This means the port should also default to 80, but it doesn't.

For example:

Url url;
url.Host = "api.thingspeak.com";
url.Path = "/update";

This produces the correct URL text http://api.thingspeak.com/update with the default scheme, but getPort() incorrectly returns 0.

See https://github.com/SmingHub/Sming/issues/1937#issuecomment-549534674, PR #1945 provided a fix but didn't actually resolve the underlying issue.

Url::toString() doesn't lowercase scheme

For example:

Url url;
url.Scheme = "HTTP";
url.Host = "api.thingspeak.com";
url.Path = "/update";

Incorrectly produces HTTP://api.thingspeak.com/update.

Add Url::getQueryParameter method

Care is required when reading query parameters as direct access to the Query member variable means non-existent keys get added. Therefore a const cast is required. This method provides a more convenient way for safely reading values, and also allows a default to be given where the value doesn't exist.