1/*
2 * "$Id: html.c 11093 2013-07-03 20:48:42Z msweet $"
3 *
4 *   HTML support functions for CUPS.
5 *
6 *   Copyright 2007-2011 by Apple Inc.
7 *   Copyright 1997-2006 by Easy Software Products.
8 *
9 *   These coded instructions, statements, and computer programs are the
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/".
14 *
15 * Contents:
16 *
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.
24 */
25
26/*
27 * Include necessary headers...
28 */
29
30#include "cgi-private.h"
31
32
33/*
34 * Local globals...
35 */
36
37static const char	*cgi_multipart = NULL;
38					/* Multipart separator, if any */
39
40
41/*
42 * Local functions...
43 */
44
45static const char	*cgi_null_passwd(const char *prompt);
46
47
48/*
49 * 'cgiEndHTML()' - End a HTML page.
50 */
51
52void
53cgiEndHTML(void)
54{
55 /*
56  * Send the standard trailer...
57  */
58
59  cgiCopyTemplateLang("trailer.tmpl");
60}
61
62
63/*
64 * 'cgiEndMultipart()' - End the delivery of a multipart web page.
65 */
66
67void
68cgiEndMultipart(void)
69{
70  if (cgi_multipart)
71  {
72    printf("\n%s--\n", cgi_multipart);
73    fflush(stdout);
74  }
75}
76
77
78/*
79 * 'cgiFormEncode()' - Encode a string as a form variable.
80 */
81
82char *					/* O - Destination string */
83cgiFormEncode(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
160void
161cgiStartHTML(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
173  if (cgi_multipart)
174    puts(cgi_multipart);
175
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
189/*
190 * 'cgiStartMultipart()' - Start a multipart delivery of a web page.
191 */
192
193void
194cgiStartMultipart(void)
195{
196  puts("MIME-Version: 1.0\n"
197       "Content-Type: multipart/x-mixed-replace; boundary=\"CUPS-MULTIPART\"\n");
198  fflush(stdout);
199
200  cgi_multipart = "--CUPS-MULTIPART";
201}
202
203
204/*
205 * 'cgiSupportsMultipart()' - Does the browser support multi-part documents?
206 */
207
208int					/* O - 1 if multi-part supported, 0 otherwise */
209cgiSupportsMultipart(void)
210{
211 /*
212  * Too many bug reports for browsers that don't support it, and too much pain
213  * to whitelist known-good browsers, so for now we just punt on multi-part
214  * support... :(
215  */
216
217  return (0);
218}
219
220
221/*
222 * 'cgi_null_passwd()' - Return a NULL password for authentication.
223 */
224
225static const char *			/* O - NULL */
226cgi_null_passwd(const char *prompt)	/* I - Prompt string (unused) */
227{
228  (void)prompt;
229
230  fprintf(stderr, "DEBUG: cgi_null_passwd(prompt=\"%s\") called!\n",
231          prompt ? prompt : "(null)");
232
233  return (NULL);
234}
235
236
237/*
238 * End of "$Id: html.c 11093 2013-07-03 20:48:42Z msweet $".
239 */
240