From 79eaf866f710be394c3630867326deb79c702689 Mon Sep 17 00:00:00 2001 From: Michael R Sweet Date: Fri, 5 Apr 2024 13:25:31 -0400 Subject: [PATCH] Add cupsCopyDestInfo2 API (Issue #586) --- cups/cups.h | 1 + cups/dest-options.c | 107 +++++++++++++++++++++++++++----------------- 2 files changed, 68 insertions(+), 40 deletions(-) diff --git a/cups/cups.h b/cups/cups.h index 9cb0739473..b0f15ca5f1 100644 --- a/cups/cups.h +++ b/cups/cups.h @@ -392,6 +392,7 @@ extern char *cupsCopyCredentialsRequest(const char *path, const char *common_na extern int cupsCopyDest(cups_dest_t *dest, int num_dests, cups_dest_t **dests) _CUPS_PUBLIC; extern int cupsCopyDestConflicts(http_t *http, cups_dest_t *dest, cups_dinfo_t *info, int num_options, cups_option_t *options, const char *new_option, const char *new_value, int *num_conflicts, cups_option_t **conflicts, int *num_resolved, cups_option_t **resolved) _CUPS_PUBLIC; extern cups_dinfo_t *cupsCopyDestInfo(http_t *http, cups_dest_t *dest) _CUPS_PUBLIC; +extern cups_dinfo_t *cupsCopyDestInfo2(http_t *http, cups_dest_t *dest, cups_dest_flags_t dflags) _CUPS_PUBLIC; extern size_t cupsCopyString(char *dst, const char *src, size_t dstsize) _CUPS_PUBLIC; extern bool cupsCreateCredentials(const char *path, bool ca_cert, cups_credpurpose_t purpose, cups_credtype_t type, cups_credusage_t usage, const char *organization, const char *org_unit, const char *locality, const char *state_province, const char *country, const char *common_name, const char *email, size_t num_alt_names, const char * const *alt_names, const char *root_name, time_t expiration_date) _CUPS_PUBLIC; extern bool cupsCreateCredentialsRequest(const char *path, cups_credpurpose_t purpose, cups_credtype_t type, cups_credusage_t usage, const char *organization, const char *org_unit, const char *locality, const char *state_province, const char *country, const char *common_name, const char *email, size_t num_alt_names, const char * const *alt_names) _CUPS_PUBLIC; diff --git a/cups/dest-options.c b/cups/dest-options.c index b53e7a7515..a6d8339f22 100644 --- a/cups/dest-options.c +++ b/cups/dest-options.c @@ -756,16 +756,79 @@ cupsCopyDestConflicts( * The caller is responsible for calling @link cupsFreeDestInfo@ on the return * value. `NULL` is returned on error. * - * @since CUPS 1.6/macOS 10.8@ + * @since CUPS 1.6@ */ cups_dinfo_t * /* O - Destination information */ cupsCopyDestInfo( http_t *http, /* I - Connection to destination */ cups_dest_t *dest) /* I - Destination */ +{ + unsigned dflags = CUPS_DEST_FLAGS_NONE; + /* Destination flags */ + _cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals */ + + + DEBUG_printf("cupsCopyDestInfo(http=%p, dest=%p(%s))", (void *)http, (void *)dest, dest ? dest->name : ""); + + /* + * Range check input... + */ + + if (!dest) + return (NULL); + + /* + * Guess the destination flags based on the printer URI's host and port... + */ + +#ifdef AF_LOCAL + if (http && httpAddrFamily(http->hostaddr) != AF_LOCAL) +#else + if (http) +#endif /* AF_LOCAL */ + { + const char *uri; /* Printer URI */ + char scheme[32], /* URI scheme */ + userpass[256], /* URI username:password */ + host[256], /* URI host */ + resource[1024]; /* URI resource path */ + int port; /* URI port */ + + if ((uri = cupsGetOption("printer-uri-supported", dest->num_options, dest->options)) == NULL || httpSeparateURI(HTTP_URI_CODING_ALL, uri, scheme, sizeof(scheme), userpass, sizeof(userpass), host, sizeof(host), &port, resource, sizeof(resource)) < HTTP_URI_STATUS_OK) + { + strlcpy(host, "localhost", sizeof(host)); + port = cg->ipp_port; + } + + if (strcmp(http->hostname, host) || port != httpAddrPort(http->hostaddr)) + { + DEBUG_printf("1cupsCopyDestInfo: Connection to device (%s).", http->hostname); + dflags = CUPS_DEST_FLAGS_DEVICE; + } + } + + return (cupsCopyDestInfo2(http, dest, dflags)); +} + + +/* + * 'cupsCopyDestInfo2()' - Get the supported values/capabilities for the + * destination. + * + * The caller is responsible for calling @link cupsFreeDestInfo@ on the return + * value. `NULL` is returned on error. + * + * @since CUPS 2.5@ + */ + +cups_dinfo_t * /* O - Destination information */ +cupsCopyDestInfo2( + http_t *http, /* I - Connection to destination */ + cups_dest_t *dest, /* I - Destination */ + cups_dest_flags_t dflags) /* I - Destination flags */ { cups_dinfo_t *dinfo; /* Destination information */ - unsigned dflags; /* Destination flags */ ipp_t *request, /* Get-Printer-Attributes request */ *response; /* Supported attributes */ int tries, /* Number of tries so far */ @@ -775,7 +838,6 @@ cupsCopyDestInfo( char resource[1024]; /* URI resource path */ int version; /* IPP version */ ipp_status_t status; /* Status of request */ - _cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals */ static const char * const requested_attrs[] = { /* Requested attributes */ "job-template", @@ -784,7 +846,7 @@ cupsCopyDestInfo( }; - DEBUG_printf("cupsCopyDestInfo(http=%p, dest=%p(%s))", (void *)http, (void *)dest, dest ? dest->name : ""); + DEBUG_printf("cupsCopyDestInfo2(http=%p, dest=%p(%s), dflags=%x)", (void *)http, (void *)dest, dest ? dest->name : "", dflags); /* * Range check input... @@ -800,44 +862,9 @@ cupsCopyDestInfo( if (!http) { DEBUG_puts("1cupsCopyDestInfo: Default server connection."); - http = _cupsConnect(); - dflags = CUPS_DEST_FLAGS_NONE; - - if (!http) + if ((http = _cupsConnect()) == NULL) return (NULL); } -#ifdef AF_LOCAL - else if (httpAddrFamily(http->hostaddr) == AF_LOCAL) - { - DEBUG_puts("1cupsCopyDestInfo: Connection to server (domain socket)."); - dflags = CUPS_DEST_FLAGS_NONE; - } -#endif /* AF_LOCAL */ - else - { - // Guess the destination flags based on the printer URI's host and port... - char scheme[32], /* URI scheme */ - userpass[256], /* URI username:password */ - host[256]; /* URI host */ - int port; /* URI port */ - - if ((uri = cupsGetOption("printer-uri-supported", dest->num_options, dest->options)) == NULL || httpSeparateURI(HTTP_URI_CODING_ALL, uri, scheme, sizeof(scheme), userpass, sizeof(userpass), host, sizeof(host), &port, resource, sizeof(resource)) < HTTP_URI_STATUS_OK) - { - strlcpy(host, "localhost", sizeof(host)); - port = cg->ipp_port; - } - - if (strcmp(http->hostname, host) || port != httpAddrPort(http->hostaddr)) - { - DEBUG_printf("1cupsCopyDestInfo: Connection to device (%s).", http->hostname); - dflags = CUPS_DEST_FLAGS_DEVICE; - } - else - { - DEBUG_printf("1cupsCopyDestInfo: Connection to server (%s).", http->hostname); - dflags = CUPS_DEST_FLAGS_NONE; - } - } /* * Get the printer URI and resource path... -- 2.47.2