]>
Commit | Line | Data |
---|---|---|
ef416fc2 | 1 | /* |
503b54c9 | 2 | * HTML support functions for CUPS. |
ef416fc2 | 3 | * |
503b54c9 MS |
4 | * Copyright 2007-2011 by Apple Inc. |
5 | * Copyright 1997-2006 by Easy Software Products. | |
ef416fc2 | 6 | * |
e3101897 | 7 | * Licensed under Apache License v2.0. See the file "LICENSE" for more information. |
ef416fc2 | 8 | */ |
9 | ||
10 | /* | |
11 | * Include necessary headers... | |
12 | */ | |
13 | ||
14 | #include "cgi-private.h" | |
15 | ||
16 | ||
58dc1933 MS |
17 | /* |
18 | * Local globals... | |
19 | */ | |
20 | ||
21 | static const char *cgi_multipart = NULL; | |
22 | /* Multipart separator, if any */ | |
23 | ||
24 | ||
ef416fc2 | 25 | /* |
26 | * Local functions... | |
27 | */ | |
28 | ||
29 | static const char *cgi_null_passwd(const char *prompt); | |
30 | ||
31 | ||
32 | /* | |
33 | * 'cgiEndHTML()' - End a HTML page. | |
34 | */ | |
35 | ||
36 | void | |
37 | cgiEndHTML(void) | |
38 | { | |
39 | /* | |
40 | * Send the standard trailer... | |
41 | */ | |
42 | ||
43 | cgiCopyTemplateLang("trailer.tmpl"); | |
44 | } | |
45 | ||
46 | ||
58dc1933 MS |
47 | /* |
48 | * 'cgiEndMultipart()' - End the delivery of a multipart web page. | |
49 | */ | |
50 | ||
51 | void | |
52 | cgiEndMultipart(void) | |
53 | { | |
54 | if (cgi_multipart) | |
d1c13e16 | 55 | { |
58dc1933 | 56 | printf("\n%s--\n", cgi_multipart); |
d1c13e16 MS |
57 | fflush(stdout); |
58 | } | |
58dc1933 MS |
59 | } |
60 | ||
61 | ||
ef416fc2 | 62 | /* |
d2354e63 | 63 | * 'cgiFormEncode()' - Encode a string as a form variable. |
ef416fc2 | 64 | */ |
65 | ||
66 | char * /* O - Destination string */ | |
67 | cgiFormEncode(char *dst, /* I - Destination string */ | |
68 | const char *src, /* I - Source string */ | |
69 | size_t dstsize) /* I - Size of destination string */ | |
70 | { | |
71 | char *dstptr, /* Pointer into destination */ | |
72 | *dstend; /* End of destination */ | |
73 | static const char *hex = /* Hexadecimal characters */ | |
74 | "0123456789ABCDEF"; | |
75 | ||
76 | ||
77 | /* | |
78 | * Mark the end of the string... | |
79 | */ | |
80 | ||
81 | dstend = dst + dstsize - 1; | |
82 | ||
83 | /* | |
84 | * Loop through the source string and copy... | |
85 | */ | |
86 | ||
87 | for (dstptr = dst; *src && dstptr < dstend;) | |
88 | { | |
89 | switch (*src) | |
57b7b66b | 90 | { |
ef416fc2 | 91 | case ' ' : |
92 | /* | |
93 | * Encode spaces with a "+"... | |
94 | */ | |
95 | ||
96 | *dstptr++ = '+'; | |
97 | src ++; | |
98 | break; | |
99 | ||
100 | case '&' : | |
101 | case '%' : | |
102 | case '+' : | |
103 | /* | |
104 | * Encode special characters with %XX escape... | |
105 | */ | |
106 | ||
107 | if (dstptr < (dstend - 2)) | |
108 | { | |
109 | *dstptr++ = '%'; | |
110 | *dstptr++ = hex[(*src & 255) >> 4]; | |
111 | *dstptr++ = hex[*src & 15]; | |
112 | src ++; | |
113 | } | |
114 | break; | |
115 | ||
116 | default : | |
117 | /* | |
118 | * Copy other characters literally... | |
119 | */ | |
120 | ||
121 | *dstptr++ = *src++; | |
122 | break; | |
123 | } | |
124 | } | |
125 | ||
126 | /* | |
127 | * Nul-terminate the destination string... | |
128 | */ | |
129 | ||
130 | *dstptr = '\0'; | |
131 | ||
132 | /* | |
133 | * Return the encoded string... | |
134 | */ | |
135 | ||
136 | return (dst); | |
137 | } | |
138 | ||
139 | ||
140 | /* | |
141 | * 'cgiStartHTML()' - Start a HTML page. | |
142 | */ | |
143 | ||
144 | void | |
145 | cgiStartHTML(const char *title) /* I - Title of page */ | |
146 | { | |
147 | /* | |
148 | * Disable any further authentication attempts... | |
149 | */ | |
150 | ||
151 | cupsSetPasswordCB(cgi_null_passwd); | |
152 | ||
153 | /* | |
154 | * Tell the client to expect UTF-8 encoded HTML... | |
155 | */ | |
156 | ||
58dc1933 MS |
157 | if (cgi_multipart) |
158 | puts(cgi_multipart); | |
159 | ||
ef416fc2 | 160 | puts("Content-Type: text/html;charset=utf-8\n"); |
161 | ||
162 | /* | |
163 | * Send a standard header... | |
164 | */ | |
165 | ||
166 | cgiSetVariable("TITLE", title); | |
167 | cgiSetServerVersion(); | |
168 | ||
169 | cgiCopyTemplateLang("header.tmpl"); | |
170 | } | |
171 | ||
172 | ||
58dc1933 | 173 | /* |
d2354e63 | 174 | * 'cgiStartMultipart()' - Start a multipart delivery of a web page. |
58dc1933 MS |
175 | */ |
176 | ||
177 | void | |
178 | cgiStartMultipart(void) | |
179 | { | |
d1c13e16 MS |
180 | puts("MIME-Version: 1.0\n" |
181 | "Content-Type: multipart/x-mixed-replace; boundary=\"CUPS-MULTIPART\"\n"); | |
182 | fflush(stdout); | |
183 | ||
58dc1933 MS |
184 | cgi_multipart = "--CUPS-MULTIPART"; |
185 | } | |
186 | ||
187 | ||
d2354e63 MS |
188 | /* |
189 | * 'cgiSupportsMultipart()' - Does the browser support multi-part documents? | |
190 | */ | |
191 | ||
192 | int /* O - 1 if multi-part supported, 0 otherwise */ | |
193 | cgiSupportsMultipart(void) | |
194 | { | |
0268488e MS |
195 | /* |
196 | * Too many bug reports for browsers that don't support it, and too much pain | |
197 | * to whitelist known-good browsers, so for now we just punt on multi-part | |
198 | * support... :( | |
199 | */ | |
d2354e63 | 200 | |
0268488e | 201 | return (0); |
d2354e63 MS |
202 | } |
203 | ||
204 | ||
ef416fc2 | 205 | /* |
206 | * 'cgi_null_passwd()' - Return a NULL password for authentication. | |
207 | */ | |
208 | ||
209 | static const char * /* O - NULL */ | |
210 | cgi_null_passwd(const char *prompt) /* I - Prompt string (unused) */ | |
211 | { | |
212 | (void)prompt; | |
213 | ||
f301802f | 214 | fprintf(stderr, "DEBUG: cgi_null_passwd(prompt=\"%s\") called!\n", |
215 | prompt ? prompt : "(null)"); | |
ef416fc2 | 216 | |
217 | return (NULL); | |
218 | } |