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.

Re: curl_easy_cmdline ?

From: Timothe Litt <litt_at_acm.org>
Date: Thu, 3 Dec 2020 07:36:22 -0500

As others have pointed out, blurring the division between the command
line tool and the library has significant architectural and practical
disadvantages.

I think there is a better approach.  Here is a rough outline.

I suggest implementing some more generally useful functions:

    int curl_easy_getopt(CURL *handle, CURLoption option, size_t
    bufsize, void *buffer)

        Returns any settable option's current value in buffer, with the
        same type as curl_easy_setopt().  Returns CURLE_UNKNOWN_OPTION,
        CURLE_NOT_BUILT_IN, CURLE_OVERFLOW, or CURLE_OK.  OVERFLOW if
        buffer too small for value.

    int curl_easy_listopt(CurlOption last, Curloption *next, size_t
    bufsize, char *name, CurlPtype *ptype)

        Returns the code and/or name of the next CurlOption (following
        "last").  CURLOPT_FIRST provides the first available option. 
        NOT_BUILT_IN options are skipped.   name is the stringified
        option name (e.g. "CURLOPT_TIMEOUT"). CurlPtype is an enum for
        CURL_PAR_{LONG, FUNCP, OBJP, OFF,STRING}.

        NULL for ptype, name or next won't return that value.  Returns
        CURL_E_UNKNOWN at end of list.  The order of return is not
        specified, disabled options are not returned.

Obtaining the state of a handle then becomes a loop over
curl_easy_listopt, calling curl_easy_getopt for each option.  Note that
"last" can never be "UNKNOWN" if it is initialized with CURLOPT_FIRST
and set to the value returned in "next".

Recording state is a simple matter of writing a file that's an array of
CurlOption, length, value items.  This can be a library function:

    curl_easy_write_handle_state(CURL *handle, FILE *fh), where fh is
    open in binary mode.

Then, the cURL tool gets a function to read a state file and generate a
command line:

    curl --gencmd statefile

    This is implemented with a table that knows how cURL uses the
    options and how to format the values.  Or it can use the CurlPtype
    to generate a default  format (decimal, hex, UTF string).

Another useful function might be

    curl --genxml statefile

    This would make a pass over curl_easy_listopt to obtain the mapping
    of codes to names and types.

    Then read the file and write an XML mapping from option name to the
    indicated types.  Many programming languages make it easy to convert
    XML to their structures.  You could even provide an XSL  stylesheet...

This approach keeps the library and cURL tool separate.  The library
knows absolutely nothing about the cURL command line.  The tool knows
nothing about the library version or configuration.

It adds minimal code to the library.  It also allows any library users
to generate state files for the cURL tool.  But they can also generate C
code, Perl code, their own command line, debug text - or whatever any
user would like to consume.

The curl_easy_getopt function has other uses - e.g. it can eliminate the
requirement for an application to keep its own copy of dynamic values.

This approach should require roughly the same implementation effort as
the original proposal, but would provide much more flexible results
while maintaining architectural consistency.


Timothe Litt
ACM Distinguished Engineer
--------------------------
This communication may not represent the ACM or my employer's views,
if any, on the matters discussed.






-------------------------------------------------------------------
Unsubscribe: https://cool.haxx.se/list/listinfo/curl-library
Etiquette: https://curl.se/mail/etiquette.html
Received on 2020-12-03