]>
Commit | Line | Data |
---|---|---|
ef416fc2 | 1 | /* |
bc44d920 | 2 | * "$Id: html.c 6649 2007-07-11 21:46:42Z mike $" |
ef416fc2 | 3 | * |
4 | * HTML support functions for the Common UNIX Printing System (CUPS). | |
5 | * | |
d2354e63 | 6 | * Copyright 2007-2009 by Apple Inc. |
ef416fc2 | 7 | * Copyright 1997-2006 by Easy Software Products. |
8 | * | |
9 | * These coded instructions, statements, and computer programs are the | |
bc44d920 | 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/". | |
ef416fc2 | 14 | * |
15 | * Contents: | |
16 | * | |
d2354e63 MS |
17 | * cgiEndHTML() - End a HTML page. |
18 | * cgiEndMultipart() - End the delivery of a multipart web page. | |
19 | * cgiFormEncode() - Encode a string as a form variable. | |
20 | * cgiStartHTML() - Start a HTML page. | |
21 | * cgiStartMultipart() - Start a multipart delivery of a web page. | |
22 | * cgiSupportsMultipart() - Does the browser support multi-part documents? | |
23 | * cgi_null_passwd() - Return a NULL password for authentication. | |
ef416fc2 | 24 | */ |
25 | ||
26 | /* | |
27 | * Include necessary headers... | |
28 | */ | |
29 | ||
30 | #include "cgi-private.h" | |
31 | ||
32 | ||
58dc1933 MS |
33 | /* |
34 | * Local globals... | |
35 | */ | |
36 | ||
37 | static const char *cgi_multipart = NULL; | |
38 | /* Multipart separator, if any */ | |
39 | ||
40 | ||
ef416fc2 | 41 | /* |
42 | * Local functions... | |
43 | */ | |
44 | ||
45 | static const char *cgi_null_passwd(const char *prompt); | |
46 | ||
47 | ||
48 | /* | |
49 | * 'cgiEndHTML()' - End a HTML page. | |
50 | */ | |
51 | ||
52 | void | |
53 | cgiEndHTML(void) | |
54 | { | |
55 | /* | |
56 | * Send the standard trailer... | |
57 | */ | |
58 | ||
59 | cgiCopyTemplateLang("trailer.tmpl"); | |
60 | } | |
61 | ||
62 | ||
58dc1933 MS |
63 | /* |
64 | * 'cgiEndMultipart()' - End the delivery of a multipart web page. | |
65 | */ | |
66 | ||
67 | void | |
68 | cgiEndMultipart(void) | |
69 | { | |
70 | if (cgi_multipart) | |
d1c13e16 | 71 | { |
58dc1933 | 72 | printf("\n%s--\n", cgi_multipart); |
d1c13e16 MS |
73 | fflush(stdout); |
74 | } | |
58dc1933 MS |
75 | } |
76 | ||
77 | ||
ef416fc2 | 78 | /* |
d2354e63 | 79 | * 'cgiFormEncode()' - Encode a string as a form variable. |
ef416fc2 | 80 | */ |
81 | ||
82 | char * /* O - Destination string */ | |
83 | cgiFormEncode(char *dst, /* I - Destination string */ | |
84 | const char *src, /* I - Source string */ | |
85 | size_t dstsize) /* I - Size of destination string */ | |
86 | { | |
87 | char *dstptr, /* Pointer into destination */ | |
88 | *dstend; /* End of destination */ | |
89 | static const char *hex = /* Hexadecimal characters */ | |
90 | "0123456789ABCDEF"; | |
91 | ||
92 | ||
93 | /* | |
94 | * Mark the end of the string... | |
95 | */ | |
96 | ||
97 | dstend = dst + dstsize - 1; | |
98 | ||
99 | /* | |
100 | * Loop through the source string and copy... | |
101 | */ | |
102 | ||
103 | for (dstptr = dst; *src && dstptr < dstend;) | |
104 | { | |
105 | switch (*src) | |
106 | { | |
107 | case ' ' : | |
108 | /* | |
109 | * Encode spaces with a "+"... | |
110 | */ | |
111 | ||
112 | *dstptr++ = '+'; | |
113 | src ++; | |
114 | break; | |
115 | ||
116 | case '&' : | |
117 | case '%' : | |
118 | case '+' : | |
119 | /* | |
120 | * Encode special characters with %XX escape... | |
121 | */ | |
122 | ||
123 | if (dstptr < (dstend - 2)) | |
124 | { | |
125 | *dstptr++ = '%'; | |
126 | *dstptr++ = hex[(*src & 255) >> 4]; | |
127 | *dstptr++ = hex[*src & 15]; | |
128 | src ++; | |
129 | } | |
130 | break; | |
131 | ||
132 | default : | |
133 | /* | |
134 | * Copy other characters literally... | |
135 | */ | |
136 | ||
137 | *dstptr++ = *src++; | |
138 | break; | |
139 | } | |
140 | } | |
141 | ||
142 | /* | |
143 | * Nul-terminate the destination string... | |
144 | */ | |
145 | ||
146 | *dstptr = '\0'; | |
147 | ||
148 | /* | |
149 | * Return the encoded string... | |
150 | */ | |
151 | ||
152 | return (dst); | |
153 | } | |
154 | ||
155 | ||
156 | /* | |
157 | * 'cgiStartHTML()' - Start a HTML page. | |
158 | */ | |
159 | ||
160 | void | |
161 | cgiStartHTML(const char *title) /* I - Title of page */ | |
162 | { | |
163 | /* | |
164 | * Disable any further authentication attempts... | |
165 | */ | |
166 | ||
167 | cupsSetPasswordCB(cgi_null_passwd); | |
168 | ||
169 | /* | |
170 | * Tell the client to expect UTF-8 encoded HTML... | |
171 | */ | |
172 | ||
58dc1933 MS |
173 | if (cgi_multipart) |
174 | puts(cgi_multipart); | |
175 | ||
ef416fc2 | 176 | puts("Content-Type: text/html;charset=utf-8\n"); |
177 | ||
178 | /* | |
179 | * Send a standard header... | |
180 | */ | |
181 | ||
182 | cgiSetVariable("TITLE", title); | |
183 | cgiSetServerVersion(); | |
184 | ||
185 | cgiCopyTemplateLang("header.tmpl"); | |
186 | } | |
187 | ||
188 | ||
58dc1933 | 189 | /* |
d2354e63 | 190 | * 'cgiStartMultipart()' - Start a multipart delivery of a web page. |
58dc1933 MS |
191 | */ |
192 | ||
193 | void | |
194 | cgiStartMultipart(void) | |
195 | { | |
d1c13e16 MS |
196 | puts("MIME-Version: 1.0\n" |
197 | "Content-Type: multipart/x-mixed-replace; boundary=\"CUPS-MULTIPART\"\n"); | |
198 | fflush(stdout); | |
199 | ||
58dc1933 MS |
200 | cgi_multipart = "--CUPS-MULTIPART"; |
201 | } | |
202 | ||
203 | ||
d2354e63 MS |
204 | /* |
205 | * 'cgiSupportsMultipart()' - Does the browser support multi-part documents? | |
206 | */ | |
207 | ||
208 | int /* O - 1 if multi-part supported, 0 otherwise */ | |
209 | cgiSupportsMultipart(void) | |
210 | { | |
211 | const char *user_agent; /* User-Agent string */ | |
212 | static int supports_multipart = -1;/* Cached value */ | |
213 | ||
214 | ||
215 | if (supports_multipart < 0) | |
216 | { | |
217 | /* | |
218 | * CUPS STR #3049: Apparently some browsers don't support multi-part | |
219 | * documents, which makes them useless for many web sites. Rather than | |
220 | * abandoning those users, we'll offer a degraded single-part mode... | |
221 | * | |
222 | * Currently we know that anything based on Gecko, MSIE, and Safari all | |
223 | * work. We'll add more as they are reported/tested. | |
224 | */ | |
225 | ||
226 | if ((user_agent = getenv("HTTP_USER_AGENT")) != NULL && | |
227 | (strstr(user_agent, " Gecko/") != NULL || | |
228 | strstr(user_agent, " MSIE ") != NULL || | |
229 | strstr(user_agent, " Safari/") != NULL)) | |
230 | supports_multipart = 1; | |
231 | else | |
232 | supports_multipart = 0; | |
233 | } | |
234 | ||
235 | return (supports_multipart); | |
236 | } | |
237 | ||
238 | ||
ef416fc2 | 239 | /* |
240 | * 'cgi_null_passwd()' - Return a NULL password for authentication. | |
241 | */ | |
242 | ||
243 | static const char * /* O - NULL */ | |
244 | cgi_null_passwd(const char *prompt) /* I - Prompt string (unused) */ | |
245 | { | |
246 | (void)prompt; | |
247 | ||
f301802f | 248 | fprintf(stderr, "DEBUG: cgi_null_passwd(prompt=\"%s\") called!\n", |
249 | prompt ? prompt : "(null)"); | |
ef416fc2 | 250 | |
251 | return (NULL); | |
252 | } | |
253 | ||
254 | ||
255 | /* | |
bc44d920 | 256 | * End of "$Id: html.c 6649 2007-07-11 21:46:42Z mike $". |
ef416fc2 | 257 | */ |