]>
Commit | Line | Data |
---|---|---|
53190348 | 1 | /* |
a1793153 | 2 | * "$Id: classes.c,v 1.19 2001/05/06 00:11:23 mike Exp $" |
53190348 | 3 | * |
4 | * Class status CGI for the Common UNIX Printing System (CUPS). | |
5 | * | |
d2935a0f | 6 | * Copyright 1997-2001 by Easy Software Products. |
53190348 | 7 | * |
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 | |
13 | * at: | |
14 | * | |
15 | * Attn: CUPS Licensing Information | |
16 | * Easy Software Products | |
17 | * 44141 Airport View Drive, Suite 204 | |
18 | * Hollywood, Maryland 20636-3111 USA | |
19 | * | |
20 | * Voice: (301) 373-9603 | |
21 | * EMail: cups-info@cups.org | |
22 | * WWW: http://www.cups.org | |
23 | * | |
24 | * Contents: | |
25 | * | |
f63a2256 | 26 | * main() - Main entry for CGI. |
53190348 | 27 | */ |
28 | ||
29 | /* | |
30 | * Include necessary headers... | |
31 | */ | |
32 | ||
f63a2256 | 33 | #include "ipp-var.h" |
53190348 | 34 | |
35 | ||
36 | /* | |
37 | * 'main()' - Main entry for CGI. | |
38 | */ | |
39 | ||
40 | int /* O - Exit status */ | |
41 | main(int argc, /* I - Number of command-line arguments */ | |
42 | char *argv[]) /* I - Command-line arguments */ | |
43 | { | |
44 | cups_lang_t *language; /* Language information */ | |
f63a2256 | 45 | char *pclass; /* Printer class name */ |
53190348 | 46 | http_t *http; /* Connection to the server */ |
f63a2256 | 47 | ipp_t *request, /* IPP request */ |
48 | *response; /* IPP response */ | |
96aa5c42 | 49 | ipp_attribute_t *attr; /* IPP attribute */ |
3d9e2586 | 50 | ipp_status_t status; /* Operation status... */ |
f63a2256 | 51 | char uri[HTTP_MAX_URI]; |
52 | /* Printer URI */ | |
53 | const char *which_jobs; /* Which jobs to show */ | |
3d9e2586 | 54 | const char *op; /* Operation to perform, if any */ |
55 | ||
f63a2256 | 56 | |
57 | /* | |
58 | * Get any form variables... | |
59 | */ | |
53190348 | 60 | |
f63a2256 | 61 | cgiInitialize(); |
3d9e2586 | 62 | op = cgiGetVariable("OP"); |
53190348 | 63 | |
64 | /* | |
65 | * Get the request language... | |
66 | */ | |
67 | ||
68 | language = cupsLangDefault(); | |
69 | ||
70 | /* | |
71 | * Connect to the HTTP server... | |
72 | */ | |
73 | ||
a1793153 | 74 | http = httpConnectEncrypt("localhost", ippPort(), cupsEncryption()); |
53190348 | 75 | |
76 | /* | |
77 | * Tell the client to expect HTML... | |
78 | */ | |
79 | ||
80 | printf("Content-Type: text/html;charset=%s\n\n", cupsLangEncoding(language)); | |
81 | ||
82 | /* | |
f63a2256 | 83 | * See if we need to show a list of printers or the status of a |
84 | * single printer... | |
53190348 | 85 | */ |
86 | ||
3d9e2586 | 87 | ippSetServerVersion(); |
88 | ||
f63a2256 | 89 | pclass = argv[0]; |
90 | if (strcmp(pclass, "/") == 0 || strcmp(pclass, "classes.cgi") == 0) | |
91 | { | |
92 | pclass = NULL; | |
3d9e2586 | 93 | cgiSetVariable("TITLE", cupsLangString(language, CUPS_MSG_CLASS)); |
f63a2256 | 94 | } |
53190348 | 95 | else |
f63a2256 | 96 | cgiSetVariable("TITLE", pclass); |
53190348 | 97 | |
3d9e2586 | 98 | cgiCopyTemplateLang(stdout, TEMPLATES, "header.tmpl", getenv("LANG")); |
99 | ||
100 | if (op == NULL || strcasecmp(op, "print-test-page") != 0) | |
101 | { | |
96aa5c42 | 102 | /* |
103 | * Get the default destination... | |
104 | */ | |
105 | ||
106 | request = ippNew(); | |
107 | request->request.op.operation_id = CUPS_GET_DEFAULT; | |
108 | request->request.op.request_id = 1; | |
109 | ||
110 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET, | |
111 | "attributes-charset", NULL, cupsLangEncoding(language)); | |
112 | ||
113 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE, | |
114 | "attributes-natural-language", NULL, language->language); | |
115 | ||
116 | if ((response = cupsDoRequest(http, request, "/")) != NULL) | |
117 | { | |
118 | if ((attr = ippFindAttribute(response, "printer-name", IPP_TAG_NAME)) != NULL) | |
119 | cgiSetVariable("DEFAULT_NAME", attr->values[0].string.text); | |
120 | ||
121 | if ((attr = ippFindAttribute(response, "printer-uri-supported", IPP_TAG_URI)) != NULL) | |
122 | { | |
123 | char method[HTTP_MAX_URI], | |
124 | username[HTTP_MAX_URI], | |
125 | hostname[HTTP_MAX_URI], | |
126 | resource[HTTP_MAX_URI], | |
127 | uri[HTTP_MAX_URI]; | |
128 | int port; /* URI data */ | |
aa66d75f | 129 | const char *server; /* Name of server */ |
96aa5c42 | 130 | |
131 | ||
132 | /* | |
133 | * Map localhost access to localhost... | |
134 | */ | |
135 | ||
aa66d75f | 136 | server = getenv("SERVER_NAME"); |
137 | ||
96aa5c42 | 138 | httpSeparate(attr->values[0].string.text, method, username, |
139 | hostname, &port, resource); | |
140 | ||
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"); | |
146 | ||
147 | /* | |
148 | * Rewrite URI with HTTP address... | |
149 | */ | |
150 | ||
151 | snprintf(uri, sizeof(uri), "http://%s:%d%s", hostname, port, | |
152 | resource); | |
153 | ||
154 | cgiSetVariable("DEFAULT_URI", uri); | |
155 | } | |
156 | ||
157 | ippDelete(response); | |
158 | } | |
159 | ||
3d9e2586 | 160 | /* |
161 | * Get the class info... | |
162 | */ | |
53190348 | 163 | |
3d9e2586 | 164 | request = ippNew(); |
53190348 | 165 | |
3d9e2586 | 166 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET, |
167 | "attributes-charset", NULL, cupsLangEncoding(language)); | |
53190348 | 168 | |
3d9e2586 | 169 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE, |
170 | "attributes-natural-language", NULL, language->language); | |
171 | ||
172 | if (pclass == NULL) | |
173 | { | |
174 | /* | |
175 | * Build a CUPS_GET_CLASSES request, which requires the following | |
176 | * attributes: | |
177 | * | |
178 | * attributes-charset | |
179 | * attributes-natural-language | |
180 | */ | |
181 | ||
182 | request->request.op.operation_id = CUPS_GET_CLASSES; | |
183 | request->request.op.request_id = 1; | |
184 | } | |
185 | else | |
186 | { | |
187 | /* | |
188 | * Build an IPP_GET_PRINTER_ATTRIBUTES request, which requires the following | |
189 | * attributes: | |
190 | * | |
191 | * attributes-charset | |
192 | * attributes-natural-language | |
193 | * printer-uri | |
194 | */ | |
195 | ||
196 | request->request.op.operation_id = IPP_GET_PRINTER_ATTRIBUTES; | |
197 | request->request.op.request_id = 1; | |
198 | ||
199 | snprintf(uri, sizeof(uri), "ipp://%s/classes/%s", getenv("SERVER_NAME"), | |
200 | pclass); | |
201 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", NULL, | |
202 | uri); | |
203 | } | |
53190348 | 204 | |
53190348 | 205 | /* |
3d9e2586 | 206 | * Do the request and get back a response... |
53190348 | 207 | */ |
208 | ||
3d9e2586 | 209 | if ((response = cupsDoRequest(http, request, "/")) != NULL) |
210 | { | |
211 | ippSetCGIVars(response, NULL, NULL); | |
212 | ippDelete(response); | |
213 | } | |
214 | ||
f63a2256 | 215 | /* |
3d9e2586 | 216 | * Write the report... |
f63a2256 | 217 | */ |
53190348 | 218 | |
3d9e2586 | 219 | cgiCopyTemplateLang(stdout, TEMPLATES, "classes.tmpl", getenv("LANG")); |
53190348 | 220 | |
3d9e2586 | 221 | /* |
222 | * Get jobs for the specified class if a class has been chosen... | |
223 | */ | |
53190348 | 224 | |
3d9e2586 | 225 | if (pclass != NULL) |
226 | { | |
227 | /* | |
228 | * Build an IPP_GET_JOBS request, which requires the following | |
229 | * attributes: | |
230 | * | |
231 | * attributes-charset | |
232 | * attributes-natural-language | |
233 | * printer-uri | |
234 | */ | |
235 | ||
236 | request = ippNew(); | |
237 | ||
238 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET, | |
239 | "attributes-charset", NULL, cupsLangEncoding(language)); | |
240 | ||
241 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE, | |
242 | "attributes-natural-language", NULL, language->language); | |
243 | ||
244 | request->request.op.operation_id = IPP_GET_JOBS; | |
245 | request->request.op.request_id = 1; | |
246 | ||
247 | snprintf(uri, sizeof(uri), "ipp://%s/classes/%s", getenv("SERVER_NAME"), | |
248 | pclass); | |
249 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", NULL, | |
250 | uri); | |
251 | ||
252 | if ((which_jobs = cgiGetVariable("which_jobs")) != NULL) | |
253 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD, "which-jobs", | |
254 | NULL, which_jobs); | |
255 | ||
256 | /* | |
257 | * Do the request and get back a response... | |
258 | */ | |
259 | ||
260 | if ((response = cupsDoRequest(http, request, "/")) != NULL) | |
261 | { | |
262 | ippSetCGIVars(response, NULL, NULL); | |
263 | ippDelete(response); | |
264 | ||
265 | cgiCopyTemplateLang(stdout, TEMPLATES, "jobs.tmpl", getenv("LANG")); | |
266 | } | |
267 | } | |
580af8cf | 268 | } |
3d9e2586 | 269 | else |
270 | { | |
271 | /* | |
272 | * Print a test page... | |
273 | */ | |
53190348 | 274 | |
3d9e2586 | 275 | snprintf(uri, sizeof(uri), "ipp://localhost/classes/%s", pclass); |
580af8cf | 276 | |
53190348 | 277 | /* |
3d9e2586 | 278 | * Build an IPP_PRINT_JOB request, which requires the following |
580af8cf | 279 | * attributes: |
280 | * | |
281 | * attributes-charset | |
282 | * attributes-natural-language | |
28614e4f | 283 | * printer-uri |
3d9e2586 | 284 | * requesting-user-name |
285 | * document-format | |
53190348 | 286 | */ |
287 | ||
580af8cf | 288 | request = ippNew(); |
289 | ||
3d9e2586 | 290 | request->request.op.operation_id = IPP_PRINT_JOB; |
291 | request->request.op.request_id = 1; | |
292 | ||
580af8cf | 293 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET, |
f63a2256 | 294 | "attributes-charset", NULL, cupsLangEncoding(language)); |
580af8cf | 295 | |
296 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE, | |
f63a2256 | 297 | "attributes-natural-language", NULL, language->language); |
580af8cf | 298 | |
3d9e2586 | 299 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", |
300 | NULL, uri); | |
580af8cf | 301 | |
3d9e2586 | 302 | if (getenv("REMOTE_USER") != NULL) |
303 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name", | |
304 | NULL, getenv("REMOTE_USER")); | |
305 | else | |
306 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name", | |
307 | NULL, "root"); | |
580af8cf | 308 | |
3d9e2586 | 309 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "job-name", |
310 | NULL, "Test Page"); | |
311 | ||
312 | ippAddString(request, IPP_TAG_JOB, IPP_TAG_MIMETYPE, "document-format", | |
313 | NULL, "application/postscript"); | |
580af8cf | 314 | |
f63a2256 | 315 | /* |
316 | * Do the request and get back a response... | |
317 | */ | |
580af8cf | 318 | |
3d9e2586 | 319 | if ((response = cupsDoFileRequest(http, request, uri + 15, |
320 | CUPS_DATADIR "/data/testprint.ps")) != NULL) | |
53190348 | 321 | { |
3d9e2586 | 322 | status = response->request.status.status_code; |
323 | ippSetCGIVars(response, NULL, NULL); | |
324 | ||
f63a2256 | 325 | ippDelete(response); |
3d9e2586 | 326 | } |
327 | else | |
328 | status = IPP_GONE; | |
53190348 | 329 | |
3d9e2586 | 330 | cgiSetVariable("PRINTER_NAME", pclass); |
331 | ||
332 | if (status > IPP_OK_CONFLICT) | |
333 | { | |
334 | cgiSetVariable("ERROR", ippErrorString(status)); | |
335 | cgiCopyTemplateLang(stdout, TEMPLATES, "error.tmpl", getenv("LANG")); | |
f63a2256 | 336 | } |
3d9e2586 | 337 | else |
338 | cgiCopyTemplateLang(stdout, TEMPLATES, "test-page.tmpl", getenv("LANG")); | |
53190348 | 339 | } |
580af8cf | 340 | |
a3e17a89 | 341 | cgiCopyTemplateLang(stdout, TEMPLATES, "trailer.tmpl", getenv("LANG")); |
f63a2256 | 342 | |
343 | /* | |
344 | * Close the HTTP server connection... | |
345 | */ | |
346 | ||
347 | httpClose(http); | |
348 | cupsLangFree(language); | |
580af8cf | 349 | |
f63a2256 | 350 | /* |
351 | * Return with no errors... | |
352 | */ | |
353 | ||
354 | return (0); | |
53190348 | 355 | } |
356 | ||
357 | ||
358 | /* | |
a1793153 | 359 | * End of "$Id: classes.c,v 1.19 2001/05/06 00:11:23 mike Exp $". |
53190348 | 360 | */ |