2 * "$Id: classes.c,v 1.20 2002/01/02 17:58:36 mike Exp $"
4 * Class status CGI for the Common UNIX Printing System (CUPS).
6 * Copyright 1997-2002 by Easy Software Products.
8 * These coded instructions, statements, and computer programs are the
9 * property of Easy Software Products and are protected by Federal
10 * copyright law. Distribution and use rights are outlined in the file
11 * "LICENSE.txt" which should have been included with this file. If this
12 * file is missing or damaged please contact Easy Software Products
15 * Attn: CUPS Licensing Information
16 * Easy Software Products
17 * 44141 Airport View Drive, Suite 204
18 * Hollywood, Maryland 20636-3111 USA
20 * Voice: (301) 373-9603
21 * EMail: cups-info@cups.org
22 * WWW: http://www.cups.org
26 * main() - Main entry for CGI.
30 * Include necessary headers...
37 * 'main()' - Main entry for CGI.
40 int /* O - Exit status */
41 main(int argc
, /* I - Number of command-line arguments */
42 char *argv
[]) /* I - Command-line arguments */
44 cups_lang_t
*language
; /* Language information */
45 char *pclass
; /* Printer class name */
46 http_t
*http
; /* Connection to the server */
47 ipp_t
*request
, /* IPP request */
48 *response
; /* IPP response */
49 ipp_attribute_t
*attr
; /* IPP attribute */
50 ipp_status_t status
; /* Operation status... */
51 char uri
[HTTP_MAX_URI
];
53 const char *which_jobs
; /* Which jobs to show */
54 const char *op
; /* Operation to perform, if any */
58 * Get any form variables...
62 op
= cgiGetVariable("OP");
65 * Get the request language...
68 language
= cupsLangDefault();
71 * Connect to the HTTP server...
74 http
= httpConnectEncrypt("localhost", ippPort(), cupsEncryption());
77 * Tell the client to expect HTML...
80 printf("Content-Type: text/html;charset=%s\n\n", cupsLangEncoding(language
));
83 * See if we need to show a list of printers or the status of a
87 ippSetServerVersion();
90 if (strcmp(pclass
, "/") == 0 || strcmp(pclass
, "classes.cgi") == 0)
93 cgiSetVariable("TITLE", cupsLangString(language
, CUPS_MSG_CLASS
));
96 cgiSetVariable("TITLE", pclass
);
98 cgiCopyTemplateLang(stdout
, TEMPLATES
, "header.tmpl", getenv("LANG"));
100 if (op
== NULL
|| strcasecmp(op
, "print-test-page") != 0)
103 * Get the default destination...
107 request
->request
.op
.operation_id
= CUPS_GET_DEFAULT
;
108 request
->request
.op
.request_id
= 1;
110 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
111 "attributes-charset", NULL
, cupsLangEncoding(language
));
113 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
114 "attributes-natural-language", NULL
, language
->language
);
116 if ((response
= cupsDoRequest(http
, request
, "/")) != NULL
)
118 if ((attr
= ippFindAttribute(response
, "printer-name", IPP_TAG_NAME
)) != NULL
)
119 cgiSetVariable("DEFAULT_NAME", attr
->values
[0].string
.text
);
121 if ((attr
= ippFindAttribute(response
, "printer-uri-supported", IPP_TAG_URI
)) != NULL
)
123 char method
[HTTP_MAX_URI
],
124 username
[HTTP_MAX_URI
],
125 hostname
[HTTP_MAX_URI
],
126 resource
[HTTP_MAX_URI
],
128 int port
; /* URI data */
129 const char *server
; /* Name of server */
133 * Map localhost access to localhost...
136 server
= getenv("SERVER_NAME");
138 httpSeparate(attr
->values
[0].string
.text
, method
, username
,
139 hostname
, &port
, resource
);
141 if (strcasecmp(hostname
, server
) == 0 &&
142 (strcmp(getenv("REMOTE_HOST"), "127.0.0.1") == 0 ||
143 strcmp(getenv("REMOTE_HOST"), "localhost") == 0 ||
144 strcmp(getenv("REMOTE_HOST"), server
) == 0))
145 strcpy(hostname
, "localhost");
148 * Rewrite URI with HTTP address...
151 snprintf(uri
, sizeof(uri
), "http://%s:%d%s", hostname
, port
,
154 cgiSetVariable("DEFAULT_URI", uri
);
161 * Get the class info...
166 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
167 "attributes-charset", NULL
, cupsLangEncoding(language
));
169 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
170 "attributes-natural-language", NULL
, language
->language
);
175 * Build a CUPS_GET_CLASSES request, which requires the following
179 * attributes-natural-language
182 request
->request
.op
.operation_id
= CUPS_GET_CLASSES
;
183 request
->request
.op
.request_id
= 1;
188 * Build an IPP_GET_PRINTER_ATTRIBUTES request, which requires the following
192 * attributes-natural-language
196 request
->request
.op
.operation_id
= IPP_GET_PRINTER_ATTRIBUTES
;
197 request
->request
.op
.request_id
= 1;
199 snprintf(uri
, sizeof(uri
), "ipp://%s/classes/%s", getenv("SERVER_NAME"),
201 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri", NULL
,
206 * Do the request and get back a response...
209 if ((response
= cupsDoRequest(http
, request
, "/")) != NULL
)
211 ippSetCGIVars(response
, NULL
, NULL
);
216 * Write the report...
219 cgiCopyTemplateLang(stdout
, TEMPLATES
, "classes.tmpl", getenv("LANG"));
222 * Get jobs for the specified class if a class has been chosen...
228 * Build an IPP_GET_JOBS request, which requires the following
232 * attributes-natural-language
238 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
239 "attributes-charset", NULL
, cupsLangEncoding(language
));
241 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
242 "attributes-natural-language", NULL
, language
->language
);
244 request
->request
.op
.operation_id
= IPP_GET_JOBS
;
245 request
->request
.op
.request_id
= 1;
247 snprintf(uri
, sizeof(uri
), "ipp://%s/classes/%s", getenv("SERVER_NAME"),
249 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri", NULL
,
252 if ((which_jobs
= cgiGetVariable("which_jobs")) != NULL
)
253 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
, "which-jobs",
257 * Do the request and get back a response...
260 if ((response
= cupsDoRequest(http
, request
, "/")) != NULL
)
262 ippSetCGIVars(response
, NULL
, NULL
);
265 cgiCopyTemplateLang(stdout
, TEMPLATES
, "jobs.tmpl", getenv("LANG"));
272 * Print a test page...
275 snprintf(uri
, sizeof(uri
), "ipp://localhost/classes/%s", pclass
);
278 * Build an IPP_PRINT_JOB request, which requires the following
282 * attributes-natural-language
284 * requesting-user-name
290 request
->request
.op
.operation_id
= IPP_PRINT_JOB
;
291 request
->request
.op
.request_id
= 1;
293 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
294 "attributes-charset", NULL
, cupsLangEncoding(language
));
296 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
297 "attributes-natural-language", NULL
, language
->language
);
299 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
302 if (getenv("REMOTE_USER") != NULL
)
303 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name",
304 NULL
, getenv("REMOTE_USER"));
306 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name",
309 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "job-name",
312 ippAddString(request
, IPP_TAG_JOB
, IPP_TAG_MIMETYPE
, "document-format",
313 NULL
, "application/postscript");
316 * Do the request and get back a response...
319 if ((response
= cupsDoFileRequest(http
, request
, uri
+ 15,
320 CUPS_DATADIR
"/data/testprint.ps")) != NULL
)
322 status
= response
->request
.status
.status_code
;
323 ippSetCGIVars(response
, NULL
, NULL
);
330 cgiSetVariable("PRINTER_NAME", pclass
);
332 if (status
> IPP_OK_CONFLICT
)
334 cgiSetVariable("ERROR", ippErrorString(status
));
335 cgiCopyTemplateLang(stdout
, TEMPLATES
, "error.tmpl", getenv("LANG"));
338 cgiCopyTemplateLang(stdout
, TEMPLATES
, "test-page.tmpl", getenv("LANG"));
341 cgiCopyTemplateLang(stdout
, TEMPLATES
, "trailer.tmpl", getenv("LANG"));
344 * Close the HTTP server connection...
348 cupsLangFree(language
);
351 * Return with no errors...
359 * End of "$Id: classes.c,v 1.20 2002/01/02 17:58:36 mike Exp $".