2 * "$Id: util.c 7850 2008-08-20 00:07:25Z mike $"
4 * Printing utilities for the Common UNIX Printing System (CUPS).
6 * Copyright 2007-2009 by Apple Inc.
7 * Copyright 1997-2006 by Easy Software Products.
9 * These coded instructions, statements, and computer programs are the
10 * property of Apple Inc. and are protected by Federal copyright
11 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
12 * which should have been included with this file. If this file is
13 * file is missing or damaged, see the license at "http://www.cups.org/".
15 * This file is subject to the Apple OS-Developed Software exception.
19 * cupsCancelJob() - Cancel a print job on the default server.
20 * cupsCancelJob2() - Cancel or purge a print job.
21 * cupsCreateJob() - Create an empty job.
22 * cupsFinishDocument() - Finish sending a document.
23 * cupsFreeJobs() - Free memory used by job data.
24 * cupsGetClasses() - Get a list of printer classes from the default
26 * cupsGetDefault() - Get the default printer or class for the default
28 * cupsGetDefault2() - Get the default printer or class for the specified
30 * cupsGetJobs() - Get the jobs from the default server.
31 * cupsGetJobs2() - Get the jobs from the specified server.
32 * cupsGetPPD() - Get the PPD file for a printer on the default
34 * cupsGetPPD2() - Get the PPD file for a printer from the specified
36 * cupsGetPPD3() - Get the PPD file for a printer on the specified
37 * server if it has changed.
38 * cupsGetPrinters() - Get a list of printers from the default server.
39 * cupsGetServerPPD() - Get an available PPD file from the server.
40 * cupsLastError() - Return the last IPP status code.
41 * cupsLastErrorString() - Return the last IPP status-message.
42 * cupsPrintFile() - Print a file to a printer or class on the default
44 * cupsPrintFile2() - Print a file to a printer or class on the
46 * cupsPrintFiles() - Print one or more files to a printer or class on
48 * cupsPrintFiles2() - Print one or more files to a printer or class on
49 * the specified server.
50 * cupsStartDocument() - Add a document to a job created with
52 * _cupsConnect() - Get the default server connection...
53 * cups_get_printer_uri() - Get the printer-uri-supported attribute for the
54 * first printer in a class.
58 * Include necessary headers...
67 #if defined(WIN32) || defined(__EMX__)
71 #endif /* WIN32 || __EMX__ */
78 static int cups_get_printer_uri(http_t
*http
, const char *name
,
79 char *host
, int hostsize
, int *port
,
80 char *resource
, int resourcesize
,
85 * 'cupsCancelJob()' - Cancel a print job on the default server.
87 * Pass @code CUPS_JOBID_ALL@ to cancel all jobs or @code CUPS_JOBID_CURRENT@
88 * to cancel the current job on the named destination.
90 * Use the @link cupsLastError@ and @link cupsLastErrorString@ functions to get
91 * the cause of any failure.
94 int /* O - 1 on success, 0 on failure */
95 cupsCancelJob(const char *name
, /* I - Name of printer or class */
96 int job_id
) /* I - Job ID, @code CUPS_JOBID_CURRENT@ for the current job, or @code CUPS_JOBID_ALL@ for all jobs */
98 return (cupsCancelJob2(CUPS_HTTP_DEFAULT
, name
, job_id
, 0)
99 < IPP_REDIRECTION_OTHER_SITE
);
104 * 'cupsCancelJob2()' - Cancel or purge a print job.
106 * Canceled jobs remain in the job history while purged jobs are removed
107 * from the job history.
109 * Pass @code CUPS_JOBID_ALL@ to cancel all jobs or @code CUPS_JOBID_CURRENT@
110 * to cancel the current job on the named destination.
112 * Use the @link cupsLastError@ and @link cupsLastErrorString@ functions to get
113 * the cause of any failure.
115 * @since CUPS 1.4/Mac OS X 10.6@
118 ipp_status_t
/* O - IPP status */
119 cupsCancelJob2(http_t
*http
, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
120 const char *name
, /* I - Name of printer or class */
121 int job_id
, /* I - Job ID, @code CUPS_JOBID_CURRENT@ for the current job, or @code CUPS_JOBID_ALL@ for all jobs */
122 int purge
) /* I - 1 to purge, 0 to cancel */
124 char uri
[HTTP_MAX_URI
]; /* Job/printer URI */
125 ipp_t
*request
; /* IPP request */
129 * Range check input...
132 if (job_id
< -1 || (!name
&& job_id
== 0))
134 _cupsSetError(IPP_INTERNAL_ERROR
, strerror(EINVAL
), 0);
139 * Connect to the default server as needed...
143 if ((http
= _cupsConnect()) == NULL
)
144 return (IPP_SERVICE_UNAVAILABLE
);
147 * Build an IPP_CANCEL_JOB or IPP_PURGE_JOBS request, which requires the following
151 * attributes-natural-language
152 * job-uri or printer-uri + job-id
153 * requesting-user-name
154 * [purge-job] or [purge-jobs]
157 request
= ippNewRequest(job_id
< 0 ? IPP_PURGE_JOBS
: IPP_CANCEL_JOB
);
161 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
162 "localhost", ippPort(), "/printers/%s", name
);
164 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri", NULL
,
166 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
, "job-id",
171 snprintf(uri
, sizeof(uri
), "ipp://localhost/jobs/%d", job_id
);
173 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "job-uri", NULL
, uri
);
176 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name",
179 if (purge
&& job_id
>= 0)
180 ippAddBoolean(request
, IPP_TAG_OPERATION
, "purge-job", 1);
181 else if (!purge
&& job_id
< 0)
182 ippAddBoolean(request
, IPP_TAG_OPERATION
, "purge-jobs", 0);
188 ippDelete(cupsDoRequest(http
, request
, "/jobs/"));
190 return (cupsLastError());
195 * 'cupsCreateJob()' - Create an empty job for streaming.
197 * Use this function when you want to stream print data using the
198 * @link cupsStartDocument@, @link cupsWriteRequestData@, and
199 * @link cupsFinishDocument@ functions. If you have one or more files to
200 * print, use the @link cupsPrintFile2@ or @link cupsPrintFiles2@ function
203 * @since CUPS 1.4/Mac OS X 10.6@
206 int /* O - Job ID or 0 on error */
208 http_t
*http
, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
209 const char *name
, /* I - Destination name */
210 const char *title
, /* I - Title of job */
211 int num_options
, /* I - Number of options */
212 cups_option_t
*options
) /* I - Options */
214 char printer_uri
[1024], /* Printer URI */
215 resource
[1024]; /* Printer resource */
216 ipp_t
*request
, /* Create-Job request */
217 *response
; /* Create-Job response */
218 ipp_attribute_t
*attr
; /* job-id attribute */
219 int job_id
= 0; /* job-id value */
222 DEBUG_printf(("cupsCreateJob(http=%p, name=\"%s\", title=\"%s\", "
223 "num_options=%d, options=%p)",
224 http
, name
, title
, num_options
, options
));
227 * Range check input...
232 _cupsSetError(IPP_INTERNAL_ERROR
, strerror(EINVAL
), 0);
237 * Build a Create-Job request...
240 if ((request
= ippNewRequest(IPP_CREATE_JOB
)) == NULL
)
242 _cupsSetError(IPP_INTERNAL_ERROR
, strerror(ENOMEM
), 0);
246 httpAssembleURIf(HTTP_URI_CODING_ALL
, printer_uri
, sizeof(printer_uri
), "ipp",
247 NULL
, "localhost", ippPort(), "/printers/%s", name
);
248 snprintf(resource
, sizeof(resource
), "/printers/%s", name
);
250 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
252 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name",
255 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "job-name", NULL
,
257 cupsEncodeOptions(request
, num_options
, options
);
260 * Send the request and get the job-id...
263 response
= cupsDoRequest(http
, request
, resource
);
265 if ((attr
= ippFindAttribute(response
, "job-id", IPP_TAG_INTEGER
)) != NULL
)
266 job_id
= attr
->values
[0].integer
;
279 * 'cupsFinishDocument()' - Finish sending a document.
281 * The document must have been started using @link cupsStartDocument@.
283 * @since CUPS 1.4/Mac OS X 10.6@
286 ipp_status_t
/* O - Status of document submission */
287 cupsFinishDocument(http_t
*http
, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
288 const char *name
) /* I - Destination name */
290 char resource
[1024]; /* Printer resource */
293 snprintf(resource
, sizeof(resource
), "/printers/%s", name
);
295 ippDelete(cupsGetResponse(http
, resource
));
297 return (cupsLastError());
302 * 'cupsFreeJobs()' - Free memory used by job data.
306 cupsFreeJobs(int num_jobs
, /* I - Number of jobs */
307 cups_job_t
*jobs
) /* I - Jobs */
309 int i
; /* Looping var */
310 cups_job_t
*job
; /* Current job */
313 if (num_jobs
<= 0 || !jobs
)
316 for (i
= num_jobs
, job
= jobs
; i
> 0; i
--, job
++)
318 _cupsStrFree(job
->dest
);
319 _cupsStrFree(job
->user
);
320 _cupsStrFree(job
->format
);
321 _cupsStrFree(job
->title
);
329 * 'cupsGetClasses()' - Get a list of printer classes from the default server.
331 * This function is deprecated - use @link cupsGetDests@ instead.
336 int /* O - Number of classes */
337 cupsGetClasses(char ***classes
) /* O - Classes */
339 int n
; /* Number of classes */
340 ipp_t
*request
, /* IPP Request */
341 *response
; /* IPP Response */
342 ipp_attribute_t
*attr
; /* Current attribute */
343 char **temp
; /* Temporary pointer */
344 http_t
*http
; /* Connection to server */
349 _cupsSetError(IPP_INTERNAL_ERROR
, strerror(EINVAL
), 0);
356 if ((http
= _cupsConnect()) == NULL
)
360 * Build a CUPS_GET_CLASSES request, which requires the following
364 * attributes-natural-language
365 * requested-attributes
368 request
= ippNewRequest(CUPS_GET_CLASSES
);
370 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
371 "requested-attributes", NULL
, "printer-name");
374 * Do the request and get back a response...
379 if ((response
= cupsDoRequest(http
, request
, "/")) != NULL
)
381 for (attr
= response
->attrs
; attr
!= NULL
; attr
= attr
->next
)
382 if (attr
->name
!= NULL
&&
383 strcasecmp(attr
->name
, "printer-name") == 0 &&
384 attr
->value_tag
== IPP_TAG_NAME
)
387 temp
= malloc(sizeof(char *));
389 temp
= realloc(*classes
, sizeof(char *) * (n
+ 1));
409 temp
[n
] = strdup(attr
->values
[0].string
.text
);
421 * 'cupsGetDefault()' - Get the default printer or class for the default server.
423 * This function returns the default printer or class as defined by
424 * the LPDEST or PRINTER environment variables. If these environment
425 * variables are not set, the server default destination is returned.
426 * Applications should use the @link cupsGetDests@ and @link cupsGetDest@
427 * functions to get the user-defined default printer, as this function does
428 * not support the lpoptions-defined default printer.
431 const char * /* O - Default printer or @code NULL@ */
435 * Return the default printer...
438 return (cupsGetDefault2(CUPS_HTTP_DEFAULT
));
443 * 'cupsGetDefault2()' - Get the default printer or class for the specified server.
445 * This function returns the default printer or class as defined by
446 * the LPDEST or PRINTER environment variables. If these environment
447 * variables are not set, the server default destination is returned.
448 * Applications should use the @link cupsGetDests@ and @link cupsGetDest@
449 * functions to get the user-defined default printer, as this function does
450 * not support the lpoptions-defined default printer.
452 * @since CUPS 1.1.21/Mac OS X 10.4@
455 const char * /* O - Default printer or @code NULL@ */
456 cupsGetDefault2(http_t
*http
) /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
458 ipp_t
*request
, /* IPP Request */
459 *response
; /* IPP Response */
460 ipp_attribute_t
*attr
; /* Current attribute */
461 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
465 * See if we have a user default printer set...
468 if (_cupsUserDefault(cg
->def_printer
, sizeof(cg
->def_printer
)))
469 return (cg
->def_printer
);
472 * Connect to the server as needed...
476 if ((http
= _cupsConnect()) == NULL
)
480 * Build a CUPS_GET_DEFAULT request, which requires the following
484 * attributes-natural-language
487 request
= ippNewRequest(CUPS_GET_DEFAULT
);
490 * Do the request and get back a response...
493 if ((response
= cupsDoRequest(http
, request
, "/")) != NULL
)
495 if ((attr
= ippFindAttribute(response
, "printer-name",
496 IPP_TAG_NAME
)) != NULL
)
498 strlcpy(cg
->def_printer
, attr
->values
[0].string
.text
,
499 sizeof(cg
->def_printer
));
501 return (cg
->def_printer
);
512 * 'cupsGetJobs()' - Get the jobs from the default server.
514 * A "whichjobs" value of @code CUPS_WHICHJOBS_ALL@ returns all jobs regardless
515 * of state, while @code CUPS_WHICHJOBS_ACTIVE@ returns jobs that are
516 * pending, processing, or held and @code CUPS_WHICHJOBS_COMPLETED@ returns
517 * jobs that are stopped, canceled, aborted, or completed.
520 int /* O - Number of jobs */
521 cupsGetJobs(cups_job_t
**jobs
, /* O - Job data */
522 const char *name
, /* I - @code NULL@ = all destinations, otherwise show jobs for named destination */
523 int myjobs
, /* I - 0 = all users, 1 = mine */
524 int whichjobs
) /* I - @code CUPS_WHICHJOBS_ALL@, @code CUPS_WHICHJOBS_ACTIVE@, or @code CUPS_WHICHJOBS_COMPLETED@ */
530 return (cupsGetJobs2(CUPS_HTTP_DEFAULT
, jobs
, name
, myjobs
, whichjobs
));
536 * 'cupsGetJobs2()' - Get the jobs from the specified server.
538 * A "whichjobs" value of @code CUPS_WHICHJOBS_ALL@ returns all jobs regardless
539 * of state, while @code CUPS_WHICHJOBS_ACTIVE@ returns jobs that are
540 * pending, processing, or held and @code CUPS_WHICHJOBS_COMPLETED@ returns
541 * jobs that are stopped, canceled, aborted, or completed.
543 * @since CUPS 1.1.21/Mac OS X 10.4@
546 int /* O - Number of jobs */
547 cupsGetJobs2(http_t
*http
, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
548 cups_job_t
**jobs
, /* O - Job data */
549 const char *name
, /* I - @code NULL@ = all destinations, otherwise show jobs for named destination */
550 int myjobs
, /* I - 0 = all users, 1 = mine */
551 int whichjobs
) /* I - @code CUPS_WHICHJOBS_ALL@, @code CUPS_WHICHJOBS_ACTIVE@, or @code CUPS_WHICHJOBS_COMPLETED@ */
553 int n
; /* Number of jobs */
554 ipp_t
*request
, /* IPP Request */
555 *response
; /* IPP Response */
556 ipp_attribute_t
*attr
; /* Current attribute */
557 cups_job_t
*temp
; /* Temporary pointer */
559 priority
, /* job-priority */
560 size
; /* job-k-octets */
561 ipp_jstate_t state
; /* job-state */
562 time_t completed_time
, /* time-at-completed */
563 creation_time
, /* time-at-creation */
564 processing_time
; /* time-at-processing */
565 const char *dest
, /* job-printer-uri */
566 *format
, /* document-format */
567 *title
, /* job-name */
568 *user
; /* job-originating-user-name */
569 char uri
[HTTP_MAX_URI
]; /* URI for jobs */
570 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
571 static const char * const attrs
[] = /* Requested attributes */
577 "job-originating-user-name",
588 * Range check input...
593 _cupsSetError(IPP_INTERNAL_ERROR
, strerror(EINVAL
), 0);
599 * Get the right URI...
604 if (httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
605 "localhost", 0, "/printers/%s", name
) != HTTP_URI_OK
)
607 _cupsSetError(IPP_INTERNAL_ERROR
, _("Unable to create printer-uri"), 1);
613 strcpy(uri
, "ipp://localhost/");
616 if ((http
= _cupsConnect()) == NULL
)
620 * Build an IPP_GET_JOBS request, which requires the following
624 * attributes-natural-language
626 * requesting-user-name
629 * requested-attributes
632 request
= ippNewRequest(IPP_GET_JOBS
);
634 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
635 "printer-uri", NULL
, uri
);
637 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
638 "requesting-user-name", NULL
, cupsUser());
641 ippAddBoolean(request
, IPP_TAG_OPERATION
, "my-jobs", 1);
643 if (whichjobs
== CUPS_WHICHJOBS_COMPLETED
)
644 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
645 "which-jobs", NULL
, "completed");
646 else if (whichjobs
== CUPS_WHICHJOBS_ALL
)
647 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
648 "which-jobs", NULL
, "all");
650 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
651 "requested-attributes", sizeof(attrs
) / sizeof(attrs
[0]),
655 * Do the request and get back a response...
661 if ((response
= cupsDoRequest(http
, request
, "/")) != NULL
)
663 for (attr
= response
->attrs
; attr
; attr
= attr
->next
)
666 * Skip leading attributes until we hit a job...
669 while (attr
&& attr
->group_tag
!= IPP_TAG_JOB
)
676 * Pull the needed attributes from this job...
682 state
= IPP_JOB_PENDING
;
685 format
= "application/octet-stream";
691 while (attr
&& attr
->group_tag
== IPP_TAG_JOB
)
693 if (!strcmp(attr
->name
, "job-id") &&
694 attr
->value_tag
== IPP_TAG_INTEGER
)
695 id
= attr
->values
[0].integer
;
696 else if (!strcmp(attr
->name
, "job-state") &&
697 attr
->value_tag
== IPP_TAG_ENUM
)
698 state
= (ipp_jstate_t
)attr
->values
[0].integer
;
699 else if (!strcmp(attr
->name
, "job-priority") &&
700 attr
->value_tag
== IPP_TAG_INTEGER
)
701 priority
= attr
->values
[0].integer
;
702 else if (!strcmp(attr
->name
, "job-k-octets") &&
703 attr
->value_tag
== IPP_TAG_INTEGER
)
704 size
= attr
->values
[0].integer
;
705 else if (!strcmp(attr
->name
, "time-at-completed") &&
706 attr
->value_tag
== IPP_TAG_INTEGER
)
707 completed_time
= attr
->values
[0].integer
;
708 else if (!strcmp(attr
->name
, "time-at-creation") &&
709 attr
->value_tag
== IPP_TAG_INTEGER
)
710 creation_time
= attr
->values
[0].integer
;
711 else if (!strcmp(attr
->name
, "time-at-processing") &&
712 attr
->value_tag
== IPP_TAG_INTEGER
)
713 processing_time
= attr
->values
[0].integer
;
714 else if (!strcmp(attr
->name
, "job-printer-uri") &&
715 attr
->value_tag
== IPP_TAG_URI
)
717 if ((dest
= strrchr(attr
->values
[0].string
.text
, '/')) != NULL
)
720 else if (!strcmp(attr
->name
, "job-originating-user-name") &&
721 attr
->value_tag
== IPP_TAG_NAME
)
722 user
= attr
->values
[0].string
.text
;
723 else if (!strcmp(attr
->name
, "document-format") &&
724 attr
->value_tag
== IPP_TAG_MIMETYPE
)
725 format
= attr
->values
[0].string
.text
;
726 else if (!strcmp(attr
->name
, "job-name") &&
727 (attr
->value_tag
== IPP_TAG_TEXT
||
728 attr
->value_tag
== IPP_TAG_NAME
))
729 title
= attr
->values
[0].string
.text
;
735 * See if we have everything needed...
747 * Allocate memory for the job...
751 temp
= malloc(sizeof(cups_job_t
));
753 temp
= realloc(*jobs
, sizeof(cups_job_t
) * (n
+ 1));
761 _cupsSetError(IPP_INTERNAL_ERROR
, NULL
, 0);
763 cupsFreeJobs(n
, *jobs
);
776 * Copy the data over...
779 temp
->dest
= _cupsStrAlloc(dest
);
780 temp
->user
= _cupsStrAlloc(user
);
781 temp
->format
= _cupsStrAlloc(format
);
782 temp
->title
= _cupsStrAlloc(title
);
784 temp
->priority
= priority
;
787 temp
->completed_time
= completed_time
;
788 temp
->creation_time
= creation_time
;
789 temp
->processing_time
= processing_time
;
798 if (n
== 0 && cg
->last_error
>= IPP_BAD_REQUEST
)
806 * 'cupsGetPPD()' - Get the PPD file for a printer on the default server.
808 * For classes, @code cupsGetPPD@ returns the PPD file for the first printer
811 * The returned filename is stored in a static buffer and is overwritten with
812 * each call to @code cupsGetPPD@ or @link cupsGetPPD2@. The caller "owns" the
813 * file that is created and must @code unlink@ the returned filename.
816 const char * /* O - Filename for PPD file */
817 cupsGetPPD(const char *name
) /* I - Destination name */
819 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
820 time_t modtime
= 0; /* Modification time */
824 * Return the PPD file...
827 cg
->ppd_filename
[0] = '\0';
829 if (cupsGetPPD3(CUPS_HTTP_DEFAULT
, name
, &modtime
, cg
->ppd_filename
,
830 sizeof(cg
->ppd_filename
)) == HTTP_OK
)
831 return (cg
->ppd_filename
);
838 * 'cupsGetPPD2()' - Get the PPD file for a printer from the specified server.
840 * For classes, @code cupsGetPPD2@ returns the PPD file for the first printer
843 * The returned filename is stored in a static buffer and is overwritten with
844 * each call to @link cupsGetPPD@ or @code cupsGetPPD2@. The caller "owns" the
845 * file that is created and must @code unlink@ the returned filename.
847 * @since CUPS 1.1.21/Mac OS X 10.4@
850 const char * /* O - Filename for PPD file */
851 cupsGetPPD2(http_t
*http
, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
852 const char *name
) /* I - Destination name */
854 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
855 time_t modtime
= 0; /* Modification time */
858 cg
->ppd_filename
[0] = '\0';
860 if (cupsGetPPD3(http
, name
, &modtime
, cg
->ppd_filename
,
861 sizeof(cg
->ppd_filename
)) == HTTP_OK
)
862 return (cg
->ppd_filename
);
869 * 'cupsGetPPD3()' - Get the PPD file for a printer on the specified
870 * server if it has changed.
872 * The "modtime" parameter contains the modification time of any
873 * locally-cached content and is updated with the time from the PPD file on
876 * The "buffer" parameter contains the local PPD filename. If it contains
877 * the empty string, a new temporary file is created, otherwise the existing
878 * file will be overwritten as needed. The caller "owns" the file that is
879 * created and must @code unlink@ the returned filename.
881 * On success, @code HTTP_OK@ is returned for a new PPD file and
882 * @code HTTP_NOT_MODIFIED@ if the existing PPD file is up-to-date. Any other
883 * status is an error.
885 * For classes, @code cupsGetPPD3@ returns the PPD file for the first printer
888 * @since CUPS 1.4/Mac OS X 10.6@
891 http_status_t
/* O - HTTP status */
892 cupsGetPPD3(http_t
*http
, /* I - HTTP connection or @code CUPS_HTTP_DEFAULT@ */
893 const char *name
, /* I - Destination name */
894 time_t *modtime
, /* IO - Modification time */
895 char *buffer
, /* I - Filename buffer */
896 size_t bufsize
) /* I - Size of filename buffer */
898 int http_port
; /* Port number */
899 char http_hostname
[HTTP_MAX_HOST
];
900 /* Hostname associated with connection */
901 http_t
*http2
; /* Alternate HTTP connection */
902 int fd
; /* PPD file */
903 char localhost
[HTTP_MAX_URI
],/* Local hostname */
904 hostname
[HTTP_MAX_URI
], /* Hostname */
905 resource
[HTTP_MAX_URI
]; /* Resource name */
906 int port
; /* Port number */
907 http_status_t status
; /* HTTP status from server */
908 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
912 * Range check input...
915 DEBUG_printf(("cupsGetPPD3(http=%p, name=\"%s\", modtime=%p(%d), buffer=%p, "
916 "bufsize=%d)", http
, name
, modtime
,
917 modtime
? (int)*modtime
: 0, buffer
, (int)bufsize
));
921 _cupsSetError(IPP_INTERNAL_ERROR
, _("No printer name"), 1);
922 return (HTTP_NOT_ACCEPTABLE
);
927 _cupsSetError(IPP_INTERNAL_ERROR
, _("No modification time"), 1);
928 return (HTTP_NOT_ACCEPTABLE
);
931 if (!buffer
|| bufsize
<= 1)
933 _cupsSetError(IPP_INTERNAL_ERROR
, _("Bad filename buffer"), 1);
934 return (HTTP_NOT_ACCEPTABLE
);
939 * See if the PPD file is available locally...
942 if (!cg
->servername
[0])
945 if (!strcasecmp(cg
->servername
, "localhost"))
947 char ppdname
[1024]; /* PPD filename */
948 struct stat ppdinfo
; /* PPD file information */
951 snprintf(ppdname
, sizeof(ppdname
), "%s/ppd/%s.ppd", cg
->cups_serverroot
,
953 if (!stat(ppdname
, &ppdinfo
))
956 * OK, the file exists, use it!
963 if (symlink(ppdname
, buffer
) && errno
!= EEXIST
)
965 _cupsSetError(IPP_INTERNAL_ERROR
, NULL
, 0);
967 return (HTTP_SERVER_ERROR
);
972 int tries
; /* Number of tries */
973 const char *tmpdir
; /* TMPDIR environment variable */
974 struct timeval curtime
; /* Current time */
977 * Previously we put root temporary files in the default CUPS temporary
978 * directory under /var/spool/cups. However, since the scheduler cleans
979 * out temporary files there and runs independently of the user apps, we
980 * don't want to use it unless specifically told to by cupsd.
983 if ((tmpdir
= getenv("TMPDIR")) == NULL
)
985 tmpdir
= "/private/tmp"; /* /tmp is a symlink to /private/tmp */
988 # endif /* __APPLE__ */
991 * Make the temporary name using the specified directory...
999 * Get the current time of day...
1002 gettimeofday(&curtime
, NULL
);
1005 * Format a string using the hex time values...
1008 snprintf(buffer
, bufsize
, "%s/%08lx%05lx", tmpdir
,
1009 (unsigned long)curtime
.tv_sec
,
1010 (unsigned long)curtime
.tv_usec
);
1013 * Try to make a symlink...
1016 if (!symlink(ppdname
, buffer
))
1021 while (tries
< 1000);
1025 _cupsSetError(IPP_INTERNAL_ERROR
, NULL
, 0);
1027 return (HTTP_SERVER_ERROR
);
1031 if (*modtime
>= ppdinfo
.st_mtime
)
1032 return (HTTP_NOT_MODIFIED
);
1035 *modtime
= ppdinfo
.st_mtime
;
1043 * Try finding a printer URI for this printer...
1047 if ((http
= _cupsConnect()) == NULL
)
1048 return (HTTP_SERVICE_UNAVAILABLE
);
1050 if (!cups_get_printer_uri(http
, name
, hostname
, sizeof(hostname
), &port
,
1051 resource
, sizeof(resource
), 0))
1052 return (HTTP_NOT_FOUND
);
1054 DEBUG_printf(("2cupsGetPPD3: Printer hostname=\"%s\", port=%d", hostname
,
1058 * Remap local hostname to localhost...
1061 httpGetHostname(NULL
, localhost
, sizeof(localhost
));
1063 DEBUG_printf(("2cupsGetPPD3: Local hostname=\"%s\"", localhost
));
1065 if (!strcasecmp(localhost
, hostname
))
1066 strcpy(hostname
, "localhost");
1069 * Get the hostname and port number we are connected to...
1072 httpGetHostname(http
, http_hostname
, sizeof(http_hostname
));
1073 http_port
= _httpAddrPort(http
->hostaddr
);
1075 DEBUG_printf(("2cupsGetPPD3: Connection hostname=\"%s\", port=%d",
1076 http_hostname
, http_port
));
1079 * Reconnect to the correct server as needed...
1082 if (!strcasecmp(http_hostname
, hostname
) && port
== http_port
)
1084 else if ((http2
= httpConnectEncrypt(hostname
, port
,
1085 cupsEncryption())) == NULL
)
1087 DEBUG_puts("1cupsGetPPD3: Unable to connect to server");
1089 return (HTTP_SERVICE_UNAVAILABLE
);
1093 * Get a temp file...
1097 fd
= open(buffer
, O_CREAT
| O_TRUNC
| O_WRONLY
, 0600);
1099 fd
= cupsTempFd(buffer
, bufsize
);
1104 * Can't open file; close the server connection and return NULL...
1107 _cupsSetError(IPP_INTERNAL_ERROR
, NULL
, 0);
1112 return (HTTP_SERVER_ERROR
);
1116 * And send a request to the HTTP server...
1119 strlcat(resource
, ".ppd", sizeof(resource
));
1122 httpSetField(http2
, HTTP_FIELD_IF_MODIFIED_SINCE
,
1123 httpGetDateString(*modtime
));
1125 status
= cupsGetFd(http2
, resource
, fd
);
1130 * See if we actually got the file or an error...
1133 if (status
== HTTP_OK
)
1134 *modtime
= httpGetDateTime(httpGetField(http2
, HTTP_FIELD_DATE
));
1135 else if (status
!= HTTP_NOT_MODIFIED
)
1137 _cupsSetHTTPError(status
);
1139 unlink(cg
->ppd_filename
);
1146 * Return the PPD file...
1149 DEBUG_printf(("1cupsGetPPD3: Returning status %d", status
));
1156 * 'cupsGetPrinters()' - Get a list of printers from the default server.
1158 * This function is deprecated - use @link cupsGetDests@ instead.
1163 int /* O - Number of printers */
1164 cupsGetPrinters(char ***printers
) /* O - Printers */
1166 int n
; /* Number of printers */
1167 ipp_t
*request
, /* IPP Request */
1168 *response
; /* IPP Response */
1169 ipp_attribute_t
*attr
; /* Current attribute */
1170 char **temp
; /* Temporary pointer */
1171 http_t
*http
; /* Connection to server */
1175 * Range check input...
1180 _cupsSetError(IPP_INTERNAL_ERROR
, strerror(EINVAL
), 0);
1188 * Try to connect to the server...
1191 if ((http
= _cupsConnect()) == NULL
)
1195 * Build a CUPS_GET_PRINTERS request, which requires the following
1198 * attributes-charset
1199 * attributes-natural-language
1200 * requested-attributes
1203 request
= ippNewRequest(CUPS_GET_PRINTERS
);
1205 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
1206 "requested-attributes", NULL
, "printer-name");
1208 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_ENUM
,
1211 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_ENUM
,
1212 "printer-type-mask", CUPS_PRINTER_CLASS
);
1215 * Do the request and get back a response...
1220 if ((response
= cupsDoRequest(http
, request
, "/")) != NULL
)
1222 for (attr
= response
->attrs
; attr
!= NULL
; attr
= attr
->next
)
1223 if (attr
->name
!= NULL
&&
1224 strcasecmp(attr
->name
, "printer-name") == 0 &&
1225 attr
->value_tag
== IPP_TAG_NAME
)
1228 temp
= malloc(sizeof(char *));
1230 temp
= realloc(*printers
, sizeof(char *) * (n
+ 1));
1235 * Ran out of memory!
1241 free((*printers
)[n
]);
1245 ippDelete(response
);
1250 temp
[n
] = strdup(attr
->values
[0].string
.text
);
1254 ippDelete(response
);
1262 * 'cupsGetServerPPD()' - Get an available PPD file from the server.
1264 * This function returns the named PPD file from the server. The
1265 * list of available PPDs is provided by the IPP @code CUPS_GET_PPDS@
1268 * You must remove (unlink) the PPD file when you are finished with
1269 * it. The PPD filename is stored in a static location that will be
1270 * overwritten on the next call to @link cupsGetPPD@, @link cupsGetPPD2@,
1271 * or @link cupsGetServerPPD@.
1273 * @since CUPS 1.3/Mac OS X 10.5@
1276 char * /* O - Name of PPD file or @code NULL@ on error */
1277 cupsGetServerPPD(http_t
*http
, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
1278 const char *name
) /* I - Name of PPD file ("ppd-name") */
1280 int fd
; /* PPD file descriptor */
1281 ipp_t
*request
; /* IPP request */
1282 _cups_globals_t
*cg
= _cupsGlobals();
1283 /* Pointer to library globals */
1287 * Range check input...
1292 _cupsSetError(IPP_INTERNAL_ERROR
, _("No PPD name"), 1);
1298 if ((http
= _cupsConnect()) == NULL
)
1302 * Get a temp file...
1305 if ((fd
= cupsTempFd(cg
->ppd_filename
, sizeof(cg
->ppd_filename
))) < 0)
1308 * Can't open file; close the server connection and return NULL...
1311 _cupsSetError(IPP_INTERNAL_ERROR
, NULL
, 0);
1317 * Get the PPD file...
1320 request
= ippNewRequest(CUPS_GET_PPD
);
1321 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "ppd-name", NULL
,
1324 ippDelete(cupsDoIORequest(http
, request
, "/", -1, fd
));
1328 if (cupsLastError() != IPP_OK
)
1330 unlink(cg
->ppd_filename
);
1334 return (cg
->ppd_filename
);
1339 * 'cupsLastError()' - Return the last IPP status code.
1342 ipp_status_t
/* O - IPP status code from last request */
1345 return (_cupsGlobals()->last_error
);
1350 * 'cupsLastErrorString()' - Return the last IPP status-message.
1352 * @since CUPS 1.2/Mac OS X 10.5@
1355 const char * /* O - status-message text from last request */
1356 cupsLastErrorString(void)
1358 return (_cupsGlobals()->last_status_message
);
1363 * 'cupsPrintFile()' - Print a file to a printer or class on the default server.
1366 int /* O - Job ID or 0 on error */
1367 cupsPrintFile(const char *name
, /* I - Destination name */
1368 const char *filename
, /* I - File to print */
1369 const char *title
, /* I - Title of job */
1370 int num_options
,/* I - Number of options */
1371 cups_option_t
*options
) /* I - Options */
1373 DEBUG_printf(("cupsPrintFile(name=\"%s\", filename=\"%s\", "
1374 "title=\"%s\", num_options=%d, options=%p)",
1375 name
, filename
, title
, num_options
, options
));
1377 return (cupsPrintFiles2(CUPS_HTTP_DEFAULT
, name
, 1, &filename
, title
,
1378 num_options
, options
));
1383 * 'cupsPrintFile2()' - Print a file to a printer or class on the specified
1386 * @since CUPS 1.1.21/Mac OS X 10.4@
1389 int /* O - Job ID or 0 on error */
1391 http_t
*http
, /* I - Connection to server */
1392 const char *name
, /* I - Destination name */
1393 const char *filename
, /* I - File to print */
1394 const char *title
, /* I - Title of job */
1395 int num_options
, /* I - Number of options */
1396 cups_option_t
*options
) /* I - Options */
1398 DEBUG_printf(("cupsPrintFile2(http=%p, name=\"%s\", filename=\"%s\", "
1399 "title=\"%s\", num_options=%d, options=%p)",
1400 http
, name
, filename
, title
, num_options
, options
));
1402 return (cupsPrintFiles2(http
, name
, 1, &filename
, title
, num_options
,
1408 * 'cupsPrintFiles()' - Print one or more files to a printer or class on the
1412 int /* O - Job ID or 0 on error */
1414 const char *name
, /* I - Destination name */
1415 int num_files
, /* I - Number of files */
1416 const char **files
, /* I - File(s) to print */
1417 const char *title
, /* I - Title of job */
1418 int num_options
, /* I - Number of options */
1419 cups_option_t
*options
) /* I - Options */
1421 DEBUG_printf(("cupsPrintFiles(name=\"%s\", num_files=%d, "
1422 "files=%p, title=\"%s\", num_options=%d, options=%p)",
1423 name
, num_files
, files
, title
, num_options
, options
));
1426 * Print the file(s)...
1429 return (cupsPrintFiles2(CUPS_HTTP_DEFAULT
, name
, num_files
, files
, title
,
1430 num_options
, options
));
1435 * 'cupsPrintFiles2()' - Print one or more files to a printer or class on the
1438 * @since CUPS 1.1.21/Mac OS X 10.4@
1441 int /* O - Job ID or 0 on error */
1443 http_t
*http
, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
1444 const char *name
, /* I - Destination name */
1445 int num_files
, /* I - Number of files */
1446 const char **files
, /* I - File(s) to print */
1447 const char *title
, /* I - Title of job */
1448 int num_options
, /* I - Number of options */
1449 cups_option_t
*options
) /* I - Options */
1451 int i
; /* Looping var */
1452 int job_id
; /* New job ID */
1453 const char *docname
; /* Basename of current filename */
1454 const char *format
; /* Document format */
1455 cups_file_t
*fp
; /* Current file */
1456 char buffer
[8192]; /* Copy buffer */
1457 ssize_t bytes
; /* Bytes in buffer */
1458 http_status_t status
; /* Status of write */
1459 _cups_globals_t
*cg
= _cupsGlobals(); /* Global data */
1460 ipp_status_t cancel_status
; /* Status code to preserve */
1461 char *cancel_message
; /* Error message to preserve */
1464 DEBUG_printf(("cupsPrintFiles2(http=%p, name=\"%s\", num_files=%d, "
1465 "files=%p, title=\"%s\", num_options=%d, options=%p)",
1466 http
, name
, num_files
, files
, title
, num_options
, options
));
1469 * Range check input...
1472 if (!name
|| num_files
< 1 || !files
)
1474 _cupsSetError(IPP_INTERNAL_ERROR
, strerror(EINVAL
), 0);
1480 * Create the print job...
1483 if ((job_id
= cupsCreateJob(http
, name
, title
, num_options
, options
)) == 0)
1487 * Send each of the files...
1490 if (cupsGetOption("raw", num_options
, options
))
1491 format
= CUPS_FORMAT_RAW
;
1492 else if ((format
= cupsGetOption("document-format", num_options
,
1494 format
= CUPS_FORMAT_AUTO
;
1496 for (i
= 0; i
< num_files
; i
++)
1499 * Start the next file...
1502 if ((docname
= strrchr(files
[i
], '/')) != NULL
)
1507 if ((fp
= cupsFileOpen(files
[i
], "rb")) == NULL
)
1510 * Unable to open print file, cancel the job and return...
1513 _cupsSetError(IPP_DOCUMENT_ACCESS_ERROR
, NULL
, 0);
1521 status
= cupsStartDocument(http
, name
, job_id
, docname
, format
,
1522 i
== (num_files
- 1));
1524 while (status
== HTTP_CONTINUE
&&
1525 (bytes
= cupsFileRead(fp
, buffer
, sizeof(buffer
))) > 0)
1526 status
= cupsWriteRequestData(http
, buffer
, bytes
);
1528 if (status
== HTTP_UNAUTHORIZED
)
1530 char resource
[1024]; /* Printer resource */
1532 snprintf(resource
, sizeof(resource
), "/printers/%s", name
);
1534 if (!cupsDoAuthentication(http
, "POST", resource
))
1536 if (httpReconnect(http
))
1538 _cupsSetError(IPP_SERVICE_UNAVAILABLE
, NULL
, 0);
1543 status
= HTTP_AUTHORIZATION_CANCELED
;
1546 while (status
== HTTP_UNAUTHORIZED
);
1550 if (status
!= HTTP_CONTINUE
|| cupsFinishDocument(http
, name
) != IPP_OK
)
1553 * Unable to queue, cancel the job and return...
1563 * If we get here, something happened while sending the print job so we need
1564 * to cancel the job without setting the last error (since we need to preserve
1565 * the current error...
1570 cancel_status
= cg
->last_error
;
1571 cancel_message
= cg
->last_status_message
?
1572 _cupsStrRetain(cg
->last_status_message
) : NULL
;
1574 cupsCancelJob2(http
, name
, job_id
, 0);
1576 cg
->last_error
= cancel_status
;
1577 cg
->last_status_message
= cancel_message
;
1584 * 'cupsStartDocument()' - Add a document to a job created with cupsCreateJob().
1586 * Use @link cupsWriteRequestData@ to write data for the document and
1587 * @link cupsFinishDocument@ to finish the document and get the submission status.
1589 * The MIME type constants @code CUPS_FORMAT_AUTO@, @code CUPS_FORMAT_PDF@,
1590 * @code CUPS_FORMAT_POSTSCRIPT@, @code CUPS_FORMAT_RAW@, and
1591 * @code CUPS_FORMAT_TEXT@ are provided for the "format" argument, although
1592 * any supported MIME type string can be supplied.
1594 * @since CUPS 1.4/Mac OS X 10.6@
1597 http_status_t
/* O - HTTP status of request */
1599 http_t
*http
, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
1600 const char *name
, /* I - Destination name */
1601 int job_id
, /* I - Job ID from @link cupsCreateJob@ */
1602 const char *docname
, /* I - Name of document */
1603 const char *format
, /* I - MIME type or @code CUPS_FORMAT_foo@ */
1604 int last_document
) /* I - 1 for last document in job, 0 otherwise */
1606 char resource
[1024], /* Resource for destinatio */
1607 printer_uri
[1024]; /* Printer URI */
1608 ipp_t
*request
; /* Send-Document request */
1609 http_status_t status
; /* HTTP status */
1613 * Create a Send-Document request...
1616 if ((request
= ippNewRequest(IPP_SEND_DOCUMENT
)) == NULL
)
1618 _cupsSetError(IPP_INTERNAL_ERROR
, strerror(ENOMEM
), 0);
1622 httpAssembleURIf(HTTP_URI_CODING_ALL
, printer_uri
, sizeof(printer_uri
), "ipp",
1623 NULL
, "localhost", ippPort(), "/printers/%s", name
);
1624 snprintf(resource
, sizeof(resource
), "/printers/%s", name
);
1626 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
1628 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
, "job-id", job_id
);
1629 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name",
1632 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "document-name",
1635 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_MIMETYPE
,
1636 "document-format", NULL
, format
);
1637 ippAddBoolean(request
, IPP_TAG_OPERATION
, "last-document", last_document
);
1640 * Send and delete the request, then return the status...
1643 status
= cupsSendRequest(http
, request
, resource
, CUPS_LENGTH_VARIABLE
);
1652 * '_cupsConnect()' - Get the default server connection...
1655 http_t
* /* O - HTTP connection */
1658 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
1662 * See if we are connected to the same server...
1668 * Compare the connection hostname, port, and encryption settings to
1669 * the cached defaults; these were initialized the first time we
1673 if (strcmp(cg
->http
->hostname
, cg
->server
) ||
1674 cg
->ipp_port
!= _httpAddrPort(cg
->http
->hostaddr
) ||
1675 (cg
->http
->encryption
!= cg
->encryption
&&
1676 cg
->http
->encryption
== HTTP_ENCRYPT_NEVER
))
1679 * Need to close the current connection because something has changed...
1682 httpClose(cg
->http
);
1688 * (Re)connect as needed...
1693 if ((cg
->http
= httpConnectEncrypt(cupsServer(), ippPort(),
1694 cupsEncryption())) == NULL
)
1697 _cupsSetError(IPP_SERVICE_UNAVAILABLE
, NULL
, 0);
1699 _cupsSetError(IPP_SERVICE_UNAVAILABLE
,
1700 _("Unable to connect to host."), 1);
1705 * Return the cached connection...
1713 * 'cups_get_printer_uri()' - Get the printer-uri-supported attribute for the
1714 * first printer in a class.
1717 static int /* O - 1 on success, 0 on failure */
1718 cups_get_printer_uri(
1719 http_t
*http
, /* I - Connection to server */
1720 const char *name
, /* I - Name of printer or class */
1721 char *host
, /* I - Hostname buffer */
1722 int hostsize
, /* I - Size of hostname buffer */
1723 int *port
, /* O - Port number */
1724 char *resource
, /* I - Resource buffer */
1725 int resourcesize
, /* I - Size of resource buffer */
1726 int depth
) /* I - Depth of query */
1728 int i
; /* Looping var */
1729 int http_port
; /* Port number */
1730 http_t
*http2
; /* Alternate HTTP connection */
1731 ipp_t
*request
, /* IPP request */
1732 *response
; /* IPP response */
1733 ipp_attribute_t
*attr
; /* Current attribute */
1734 char uri
[HTTP_MAX_URI
], /* printer-uri attribute */
1735 scheme
[HTTP_MAX_URI
], /* Scheme name */
1736 username
[HTTP_MAX_URI
], /* Username:password */
1737 classname
[255], /* Temporary class name */
1738 http_hostname
[HTTP_MAX_HOST
];
1739 /* Hostname associated with connection */
1740 static const char * const requested_attrs
[] =
1741 { /* Requested attributes */
1743 "printer-uri-supported",
1748 DEBUG_printf(("7cups_get_printer_uri(http=%p, name=\"%s\", host=%p, "
1749 "hostsize=%d, resource=%p, resourcesize=%d, depth=%d)",
1750 http
, name
, host
, hostsize
, resource
, resourcesize
, depth
));
1753 * Setup the printer URI...
1756 if (httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
1757 "localhost", 0, "/printers/%s", name
) != HTTP_URI_OK
)
1759 _cupsSetError(IPP_INTERNAL_ERROR
, _("Unable to create printer-uri"), 1);
1767 DEBUG_printf(("9cups_get_printer_uri: printer-uri=\"%s\"", uri
));
1770 * Get the hostname and port number we are connected to...
1773 httpGetHostname(http
, http_hostname
, sizeof(http_hostname
));
1774 http_port
= _httpAddrPort(http
->hostaddr
);
1777 * Build an IPP_GET_PRINTER_ATTRIBUTES request, which requires the following
1780 * attributes-charset
1781 * attributes-natural-language
1783 * requested-attributes
1786 request
= ippNewRequest(IPP_GET_PRINTER_ATTRIBUTES
);
1788 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
1791 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1792 "requested-attributes",
1793 sizeof(requested_attrs
) / sizeof(requested_attrs
[0]),
1794 NULL
, requested_attrs
);
1797 * Do the request and get back a response...
1800 if ((response
= cupsDoRequest(http
, request
, "/")) != NULL
)
1802 if ((attr
= ippFindAttribute(response
, "member-uris", IPP_TAG_URI
)) != NULL
)
1805 * Get the first actual printer name in the class...
1808 for (i
= 0; i
< attr
->num_values
; i
++)
1810 httpSeparateURI(HTTP_URI_CODING_ALL
, attr
->values
[i
].string
.text
,
1811 scheme
, sizeof(scheme
), username
, sizeof(username
),
1812 host
, hostsize
, port
, resource
, resourcesize
);
1813 if (!strncmp(resource
, "/printers/", 10))
1819 ippDelete(response
);
1826 * No printers in this class - try recursively looking for a printer,
1827 * but not more than 3 levels deep...
1832 for (i
= 0; i
< attr
->num_values
; i
++)
1834 httpSeparateURI(HTTP_URI_CODING_ALL
, attr
->values
[i
].string
.text
,
1835 scheme
, sizeof(scheme
), username
, sizeof(username
),
1836 host
, hostsize
, port
, resource
, resourcesize
);
1837 if (!strncmp(resource
, "/classes/", 9))
1840 * Found a class! Connect to the right server...
1843 if (!strcasecmp(http_hostname
, host
) && *port
== http_port
)
1845 else if ((http2
= httpConnectEncrypt(host
, *port
,
1846 cupsEncryption())) == NULL
)
1848 DEBUG_puts("8cups_get_printer_uri: Unable to connect to server");
1854 * Look up printers on that server...
1857 strlcpy(classname
, resource
+ 9, sizeof(classname
));
1859 cups_get_printer_uri(http2
, classname
, host
, hostsize
, port
,
1860 resource
, resourcesize
, depth
+ 1);
1863 * Close the connection as needed...
1875 else if ((attr
= ippFindAttribute(response
, "printer-uri-supported",
1876 IPP_TAG_URI
)) != NULL
)
1878 httpSeparateURI(HTTP_URI_CODING_ALL
,
1879 _httpResolveURI(attr
->values
[0].string
.text
, uri
,
1881 scheme
, sizeof(scheme
), username
, sizeof(username
),
1882 host
, hostsize
, port
, resource
, resourcesize
);
1883 ippDelete(response
);
1885 if (!strncmp(resource
, "/classes/", 9))
1887 _cupsSetError(IPP_INTERNAL_ERROR
,
1888 _("No printer-uri found for class"), 1);
1899 ippDelete(response
);
1902 if (cupsLastError() != IPP_NOT_FOUND
)
1903 _cupsSetError(IPP_INTERNAL_ERROR
, _("No printer-uri found"), 1);
1913 * End of "$Id: util.c 7850 2008-08-20 00:07:25Z mike $".