curl / Mailing Lists / curl-library / Single Mail
Buy commercial curl support from WolfSSL. We help you work out your issues, debug your libcurl applications, use the API, port to new platforms, add new features and more. With a team lead by the curl founder himself.

Proposal: URL API extension, second setter function to allow setting from CURLU object

From: Mark Gaiser via curl-library <curl-library_at_lists.haxx.se>
Date: Sun, 22 Oct 2023 13:29:32 +0200

Hi,

This is a slightly more thought out (and scoped down) version of what I
said here [1].

In implementing IPFS URL support in curl I had to do many interactions with
the URL api. So many that it made me wonder: "can't this be done simpler?"

This code essentially needs to compose a new/updated url by mixing parts
from another url object in it. Currently this means you'd have to do the
following:

/* create your temp variable */
char *gatewayhost = NULL;

/* fill it with some value from one url object */
if(curl_url_get(gatewaysurl, CURLUPART_HOST,
                &gatewayhost, CURLU_URLDECODE)) {
  /* handle error case */
}

/* set value on destination url object */
if(curl_url_set(uh, CURLUPART_HOST, gatewayhost, CURLU_URLENCODE)) {
  /* again handle error case */
}

/* clean up your temp variable */
curl_free(gatewayhost);

I get that this is c-code and just the c-way of doing things. Still, it can
be simplified a lot, even in c.

I propose a new URL API function with the following signature:
CURLUcode curl_url_set_o(CURLU *u, CURLUPart what,
                       const CURLU *source, unsigned int flags)

The "_o" suffix is intended to indicate that this set function expects
an object as source to get its URL part from.
The "source" object would be a const CURLU object from which the CURLUPart
is retrieved as defined in the "what" argument. Its value is then set in
the destination CURLU object (the "u" in this case).

Below is an example of how it should work.
CURLU *uh ...
const CURLU *gatewaysurl ...

if(curl_url_set_o(uh, CURLUPART_HOST, gatewaysurl, CURLU_URLENCODE)) {
  /* handle error case */
}

This - near one-liner - would internally have:
- Retrieved "CURLUPART_HOST" from "gatewaysurl"
- error-handled that action
- set the value of "gatewaysurl" as "CURLUPART_HOST" in the "uh" object
- error handled that action too
- cleaned up internal temp variable

Using this API function would give a short and clean way to change a
CURLU object with data from another CURLU object. You only need to handle
the error once. Though you lose the verbosity to know if the "get" failed
or just the set. This might be solved by more values in CURLUcode
effectively doubling it.

What are your thoughts on this?

Best regards,
Mark Gaiser

[1] https://github.com/curl/curl/issues/12148#issuecomment-1768249413


-- 
Unsubscribe: https://lists.haxx.se/mailman/listinfo/curl-library
Etiquette:   https://curl.se/mail/etiquette.html
Received on 2023-10-22