]> git.ipfire.org Git - thirdparty/cups.git/blame - cgi-bin/ipp-var.c
Copyright update...
[thirdparty/cups.git] / cgi-bin / ipp-var.c
CommitLineData
13c1a6d9 1/*
efb2f309 2 * "$Id: ipp-var.c,v 1.24 2002/01/02 17:58:36 mike Exp $"
13c1a6d9 3 *
4 * IPP variable routines for the Common UNIX Printing System (CUPS).
5 *
efb2f309 6 * Copyright 1997-2002 by Easy Software Products.
13c1a6d9 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 *
31a80a7f 26 * ippGetTemplateDir() - Get the templates directory...
3d9e2586 27 * ippSetServerVersion() - Set the server name and CUPS version...
28 * ippSetCGIVars() - Set CGI variables from an IPP response.
13c1a6d9 29 */
30
31/*
32 * Include necessary headers...
33 */
34
35#include "ipp-var.h"
36
37
7e59c3e3 38/*
39 * 'ippGetTemplateDir()' - Get the templates directory...
40 */
41
42char * /* O - Template directory */
43ippGetTemplateDir(void)
44{
45 const char *datadir; /* CUPS_DATADIR env var */
46 static char templates[1024] = ""; /* Template directory */
47
48
49 if (!templates[0])
50 {
51 /*
52 * Build the template directory pathname...
53 */
54
55 if ((datadir = getenv("CUPS_DATADIR")) == NULL)
56 datadir = CUPS_DATADIR;
57
58 snprintf(templates, sizeof(templates), "%s/templates", datadir);
59 }
60
61 return (templates);
62}
63
64
3d9e2586 65/*
66 * 'ippSetServerVersion()' - Set the server name and CUPS version...
67 */
68
69void
70ippSetServerVersion(void)
71{
72 cgiSetVariable("SERVER_NAME", getenv("SERVER_NAME"));
73 cgiSetVariable("REMOTE_USER", getenv("REMOTE_USER"));
74 cgiSetVariable("CUPS_VERSION", CUPS_SVERSION);
75}
76
77
13c1a6d9 78/*
79 * 'ippSetCGIVars()' - Set CGI variables from an IPP response.
80 */
81
82void
3d9e2586 83ippSetCGIVars(ipp_t *response, /* I - Response data to be copied... */
84 const char *filter_name, /* I - Filter name */
85 const char *filter_value) /* I - Filter value */
13c1a6d9 86{
87 int element; /* Element in CGI array */
3d9e2586 88 ipp_attribute_t *attr, /* Attribute in response... */
89 *filter; /* Filtering attribute */
13c1a6d9 90 int i; /* Looping var */
91 char name[1024], /* Name of attribute */
92 value[16384], /* Value(s) */
93 *valptr; /* Pointer into value */
100cd8c8 94 char method[HTTP_MAX_URI],
95 username[HTTP_MAX_URI],
96 hostname[HTTP_MAX_URI],
97 resource[HTTP_MAX_URI],
98 uri[HTTP_MAX_URI];
fa779d48 99 int port; /* URI data */
30d60919 100 int ishttps; /* Using encryption? */
aa66d75f 101 const char *server; /* Name of server */
358cc876 102 struct tm *date; /* Date information */
13c1a6d9 103
104
3d9e2586 105 ippSetServerVersion();
13c1a6d9 106
30d60919 107 server = getenv("SERVER_NAME");
108 ishttps = getenv("HTTPS") != NULL;
fa779d48 109
100cd8c8 110 for (attr = response->attrs;
111 attr && attr->group_tag == IPP_TAG_OPERATION;
112 attr = attr->next);
113
114 for (element = 0; attr != NULL; attr = attr->next, element ++)
13c1a6d9 115 {
116 /*
117 * Copy attributes to a separator...
118 */
119
3d9e2586 120 if (filter_name)
121 {
122 for (filter = attr;
123 filter != NULL && filter->group_tag != IPP_TAG_ZERO;
124 filter = filter->next)
125 if (filter->name && strcmp(filter->name, filter_name) == 0 &&
31a80a7f 126 (filter->value_tag == IPP_TAG_STRING ||
127 (filter->value_tag >= IPP_TAG_TEXTLANG &&
128 filter->value_tag <= IPP_TAG_MIMETYPE)) &&
129 filter->values[0].string.text != NULL &&
3d9e2586 130 strcasecmp(filter->values[0].string.text, filter_value) == 0)
131 break;
132
133 if (!filter)
134 return;
135
136 if (filter->group_tag == IPP_TAG_ZERO)
137 {
138 attr = filter;
139 element --;
140 continue;
141 }
142 }
143
13c1a6d9 144 for (; attr != NULL && attr->group_tag != IPP_TAG_ZERO; attr = attr->next)
145 {
146 /*
147 * Copy the attribute name, substituting "_" for "-"...
148 */
149
150 if (attr->name == NULL)
151 continue;
152
153 for (i = 0; attr->name[i]; i ++)
154 if (attr->name[i] == '-')
155 name[i] = '_';
156 else
157 name[i] = attr->name[i];
158
159 name[i] = '\0';
160
df7507f5 161 /*
162 * Add "job_printer_name" variable if we have a "job_printer_uri"
163 * attribute...
164 */
165
166 if (strcmp(name, "job_printer_uri") == 0)
167 {
168 if ((valptr = strrchr(attr->values[0].string.text, '/')) == NULL)
169 valptr = "unknown";
170 else
171 valptr ++;
172
173 cgiSetArray("job_printer_name", element, valptr);
174 }
175
13c1a6d9 176 /*
177 * Copy values...
178 */
179
0819478b 180 value[0] = '\0'; /* Initially an empty string */
181 value[sizeof(value) - 1] = '\0'; /* In case string gets full */
182 valptr = value; /* Start at the beginning */
13c1a6d9 183
184 for (i = 0; i < attr->num_values; i ++)
185 {
186 if (i)
ae40876c 187 strncat(valptr, ",", sizeof(value) - (valptr - value) - 1);
13c1a6d9 188
189 valptr += strlen(valptr);
190
191 switch (attr->value_tag)
192 {
193 case IPP_TAG_INTEGER :
194 case IPP_TAG_ENUM :
358cc876 195 if (strncmp(name, "time_at_", 8) == 0)
196 {
333f3652 197 date = localtime((time_t *)&(attr->values[i].integer));
0819478b 198 strftime(valptr, sizeof(value) - (valptr - value),
8a32fc08 199 CUPS_STRFTIME_FORMAT, date);
358cc876 200 }
201 else
0819478b 202 snprintf(valptr, sizeof(value) - (valptr - value),
203 "%d", attr->values[i].integer);
13c1a6d9 204 break;
205
206 case IPP_TAG_BOOLEAN :
0819478b 207 snprintf(valptr, sizeof(value) - (valptr - value),
208 "%d", attr->values[i].boolean);
f63a2256 209 break;
13c1a6d9 210
211 case IPP_TAG_NOVALUE :
ae40876c 212 strncat(valptr, "novalue", sizeof(value) - (valptr - value) - 1);
13c1a6d9 213 break;
214
215 case IPP_TAG_RANGE :
0819478b 216 snprintf(valptr, sizeof(value) - (valptr - value),
217 "%d-%d", attr->values[i].range.lower,
218 attr->values[i].range.upper);
13c1a6d9 219 break;
220
221 case IPP_TAG_RESOLUTION :
0819478b 222 snprintf(valptr, sizeof(value) - (valptr - value),
223 "%dx%d%s", attr->values[i].resolution.xres,
224 attr->values[i].resolution.yres,
225 attr->values[i].resolution.units == IPP_RES_PER_INCH ?
226 "dpi" : "dpc");
13c1a6d9 227 break;
228
100cd8c8 229 case IPP_TAG_URI :
082b40d2 230 if (strchr(attr->values[i].string.text, ':') != NULL)
231 {
232 httpSeparate(attr->values[i].string.text, method, username,
233 hostname, &port, resource);
234
235 if (strcmp(method, "ipp") == 0 ||
236 strcmp(method, "http") == 0)
237 {
238 /*
30d60919 239 * Map localhost access to localhost and local port...
082b40d2 240 */
241
242 if (strcasecmp(hostname, server) == 0 &&
243 (strcmp(getenv("REMOTE_HOST"), "127.0.0.1") == 0 ||
96aa5c42 244 strcmp(getenv("REMOTE_HOST"), "localhost") == 0 ||
245 strcmp(getenv("REMOTE_HOST"), server) == 0))
30d60919 246 {
082b40d2 247 strcpy(hostname, "localhost");
30d60919 248 port = atoi(getenv("SERVER_PORT"));
249 }
082b40d2 250
251 /*
252 * Rewrite URI with HTTP address...
253 */
254
255 if (username[0])
30d60919 256 snprintf(uri, sizeof(uri), "%s://%s@%s:%d%s",
257 ishttps ? "https" : "http",
258 username, hostname, port, resource);
082b40d2 259 else
30d60919 260 snprintf(uri, sizeof(uri), "%s://%s:%d%s",
261 ishttps ? "https" : "http",
262 hostname, port, resource);
082b40d2 263
ae40876c 264 strncat(valptr, uri, sizeof(value) - (valptr - value) - 1);
082b40d2 265 break;
266 }
fa779d48 267 }
100cd8c8 268
13c1a6d9 269 case IPP_TAG_STRING :
270 case IPP_TAG_TEXT :
271 case IPP_TAG_NAME :
272 case IPP_TAG_KEYWORD :
273 case IPP_TAG_CHARSET :
274 case IPP_TAG_LANGUAGE :
0819478b 275 strncat(valptr, attr->values[i].string.text,
ae40876c 276 sizeof(value) - (valptr - value) - 1);
13c1a6d9 277 break;
d21a7597 278
279 default :
280 break; /* anti-compiler-warning-code */
13c1a6d9 281 }
282 }
283
284 /*
285 * Add the element...
286 */
287
288 cgiSetArray(name, element, value);
289 }
290
291 if (attr == NULL)
292 break;
293 }
294}
295
296
297/*
efb2f309 298 * End of "$Id: ipp-var.c,v 1.24 2002/01/02 17:58:36 mike Exp $".
13c1a6d9 299 */