/*1* HTML support functions for CUPS.2*3* Copyright 2007-2011 by Apple Inc.4* Copyright 1997-2006 by Easy Software Products.5*6* Licensed under Apache License v2.0. See the file "LICENSE" for more information.7*/89/*10* Include necessary headers...11*/1213#include "cgi-private.h"141516/*17* Local globals...18*/1920static const char *cgi_multipart = NULL;21/* Multipart separator, if any */222324/*25* Local functions...26*/2728static const char *cgi_null_passwd(const char *prompt);293031/*32* 'cgiEndHTML()' - End a HTML page.33*/3435void36cgiEndHTML(void)37{38/*39* Send the standard trailer...40*/4142cgiCopyTemplateLang("trailer.tmpl");43}444546/*47* 'cgiEndMultipart()' - End the delivery of a multipart web page.48*/4950void51cgiEndMultipart(void)52{53if (cgi_multipart)54{55printf("\n%s--\n", cgi_multipart);56fflush(stdout);57}58}596061/*62* 'cgiFormEncode()' - Encode a string as a form variable.63*/6465char * /* O - Destination string */66cgiFormEncode(char *dst, /* I - Destination string */67const char *src, /* I - Source string */68size_t dstsize) /* I - Size of destination string */69{70char *dstptr, /* Pointer into destination */71*dstend; /* End of destination */72static const char *hex = /* Hexadecimal characters */73"0123456789ABCDEF";747576/*77* Mark the end of the string...78*/7980dstend = dst + dstsize - 1;8182/*83* Loop through the source string and copy...84*/8586for (dstptr = dst; *src && dstptr < dstend;)87{88switch (*src)89{90case ' ' :91/*92* Encode spaces with a "+"...93*/9495*dstptr++ = '+';96src ++;97break;9899case '&' :100case '%' :101case '+' :102/*103* Encode special characters with %XX escape...104*/105106if (dstptr < (dstend - 2))107{108*dstptr++ = '%';109*dstptr++ = hex[(*src & 255) >> 4];110*dstptr++ = hex[*src & 15];111src ++;112}113break;114115default :116/*117* Copy other characters literally...118*/119120*dstptr++ = *src++;121break;122}123}124125/*126* Nul-terminate the destination string...127*/128129*dstptr = '\0';130131/*132* Return the encoded string...133*/134135return (dst);136}137138139/*140* 'cgiStartHTML()' - Start a HTML page.141*/142143void144cgiStartHTML(const char *title) /* I - Title of page */145{146/*147* Disable any further authentication attempts...148*/149150cupsSetPasswordCB(cgi_null_passwd);151152/*153* Tell the client to expect UTF-8 encoded HTML...154*/155156if (cgi_multipart)157puts(cgi_multipart);158159puts("Content-Type: text/html;charset=utf-8\n");160161/*162* Send a standard header...163*/164165cgiSetVariable("TITLE", title);166cgiSetServerVersion();167168cgiCopyTemplateLang("header.tmpl");169}170171172/*173* 'cgiStartMultipart()' - Start a multipart delivery of a web page.174*/175176void177cgiStartMultipart(void)178{179puts("MIME-Version: 1.0\n"180"Content-Type: multipart/x-mixed-replace; boundary=\"CUPS-MULTIPART\"\n");181fflush(stdout);182183cgi_multipart = "--CUPS-MULTIPART";184}185186187/*188* 'cgiSupportsMultipart()' - Does the browser support multi-part documents?189*/190191int /* O - 1 if multi-part supported, 0 otherwise */192cgiSupportsMultipart(void)193{194/*195* Too many bug reports for browsers that don't support it, and too much pain196* to whitelist known-good browsers, so for now we just punt on multi-part197* support... :(198*/199200return (0);201}202203204/*205* 'cgi_null_passwd()' - Return a NULL password for authentication.206*/207208static const char * /* O - NULL */209cgi_null_passwd(const char *prompt) /* I - Prompt string (unused) */210{211(void)prompt;212213fprintf(stderr, "DEBUG: cgi_null_passwd(prompt=\"%s\") called!\n",214prompt ? prompt : "(null)");215216return (NULL);217}218219220