BUrl: rework handling of URL encoding

The content of a BUrl should always be in encoded form, to simplify
handling and validation.

Deprecate the UrlEncode member function and make it private. Instead aadd a new
way to handle URL encoding:
- All ways to set an URL (constructors, SetUrlString, and all setters)
  now take an extra boolean parameter indicating if the string is already
  encoded. The default value is to encode strings automatically.
- The static version of UrlEncode and UrlDecode, which operate on a
  string, are preserved and used by other parts of the API.

All unit tests adjusted to handle this, and still passing.

Fixes #12983

Change-Id: I06f06978d0d35e56d7c92b67f001856bb7dcafc8
Reviewed-on: https://review.haiku-os.org/c/haiku/+/1193
Reviewed-by: nephele nephele <[email protected]>
Reviewed-by: Adrien Destugues <[email protected]>
This commit is contained in:
athira-selvam
2025-05-27 10:43:12 +00:00
committed by Adrien Destugues
parent 60bb9a9f96
commit 1d242620b4
7 changed files with 98 additions and 56 deletions
+28 -10
View File
@@ -66,10 +66,14 @@
//! @{
/*!
\fn BUrl::BUrl(const char* url);
\fn BUrl::BUrl(const char* url, bool encode = true);
\brief Constructs a BUrl and fills it.
\param url A string to parse and populate the URL fields from.
\param encode Wether to urlencode the string.
The URL fields are extracted from the passed string and encoded. If you are constructing an
URL from a string that is already URL-encoded, set encode to false.
Call InitCheck() to verify that the string was succesfully parsed and
resulted in a valid URL.
@@ -143,10 +147,13 @@
//! @{
/*!
\fn BUrl& BUrl::SetUrlString(const BString& url);
\fn BUrl& BUrl::SetUrlString(const BString& url, bool encode = true);
\brief Parse a string and set the URL accordingly
\param url A string to parse as an absolute URL.
\param encode Wether to URL-encode the string.
Set encode to false if the URL string is already URL-encoded.
*/
/*!
@@ -225,8 +232,8 @@
\returns the string representation of the URL.
A complete URL string is of the form protocol://username:passord\@host:port/path?request#fragment . All the fields are optional, for example a file URL will
have only a protocol and a path.
A complete URL string is of the form protocol://username:passord\@host:port/path?request#fragment .
All the fields are optional, for example a file URL will have only a protocol and a path.
*/
/*!
@@ -404,20 +411,31 @@
/*!
\fn void BUrl::UrlEncode(bool strict=false)
\brief Undocumented public method
\fn static BString BUrl::UrlEncode(const BString& url, bool strict=false, bool directory=false)
\brief URL-encode a string containing an URL or URL component.
\param strict Undocumented
\param strict Use strict mode
\param directory Use directory mode
In strict mode, space are replaced by %20. In non-strict mode they are replaced by a +
character.
In directory mode, / and \\ characters are not encoded. In the other fields they are
percent-encoded.
\return The encoded URL string
\since Haiku R1
*/
/*!
\fn void BUrl::UrlDecode(bool strict=false)
\brief Undocumented public method
\fn static BString BUrl::UrlDecode(const BString& url, bool strict=false)
\brief URL-decode a string containing an URL or URL component.
\param strict Undocumented
\param strict Use strict mode.
\return The decoded URL string
\since Haiku R1
*/