return d->vec;
}
-/** @brief Write a CGI attribute
- * @param output Where to send output
- * @param name Attribute name
- * @param value Attribute value
- */
-void cgi_attr(struct sink *output, const char *name, const char *value) {
- /* Try to avoid needless quoting */
- if(!value[strspn(value, "abcdefghijklmnopqrstuvwxyz"
- "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
- "0123456789")])
- sink_printf(output, "%s=%s", name, value);
- else
- sink_printf(output, "%s=\"%s\"", name, cgi_sgmlquote(value));
-}
-
-/** @brief Write an open tag
- * @param output Where to send output
- * @param name Element name
- * @param ... Attribute name/value pairs
- *
- * The name/value pair list is terminated by a single (char *)0.
- */
-void cgi_opentag(struct sink *output, const char *name, ...) {
- va_list ap;
- const char *n, *v;
-
- sink_printf(output, "<%s", name);
- va_start(ap, name);
- while((n = va_arg(ap, const char *))) {
- sink_printf(output, " ");
- v = va_arg(ap, const char *);
- if(v)
- cgi_attr(output, n, v);
- else
- sink_printf(output, n);
- }
- va_end(ap);
- sink_printf(output, ">");
-}
-
-/** @brief Write a close tag
- * @param output Where to send output
- * @param name Element name
- */
-void cgi_closetag(struct sink *output, const char *name) {
- sink_printf(output, "</%s>", name);
-}
-
/** @brief Construct a URL
* @param url Base URL
* @param ... Name/value pairs for constructed query string
const char *cgi_get(const char *name);
void cgi_set(const char *name, const char *value);
char *cgi_sgmlquote(const char *src);
-void cgi_attr(struct sink *output, const char *name, const char *value);
-void cgi_opentag(struct sink *output, const char *name, ...);
-void cgi_closetag(struct sink *output, const char *name);
char *cgi_makeurl(const char *url, ...);
char *cgi_thisurl(const char *url);
void cgi_clear(void);
}
static void test_cgi(void) {
- struct dynstr d[1];
setenv("REQUEST_METHOD", "GET", 1);
setenv("QUERY_STRING", "foo=bar&a=b+c&c=x%7ey", 1);
check_string(cgi_sgmlquote("\"&\""), ""&"");
check_string(cgi_sgmlquote("\xC2\xA3"), "£");
- dynstr_init(d);
- cgi_opentag(sink_dynstr(d), "element",
- "foo", "bar",
- "foo", "has space",
- "foo", "has \"quotes\"",
- (char *)NULL);
- dynstr_terminate(d);
- check_string(d->vec, "<element foo=bar foo=\"has space\" foo=\"has "quotes"\">");
-
- dynstr_init(d);
- cgi_opentag(sink_dynstr(d), "element",
- "foo", (char *)NULL,
- (char *)NULL);
- dynstr_terminate(d);
- check_string(d->vec, "<element foo>");
-
- dynstr_init(d);
- cgi_closetag(sink_dynstr(d), "element");
- dynstr_terminate(d);
- check_string(d->vec, "</element>");
-
check_string(cgi_makeurl("http://example.com/", (char *)NULL),
"http://example.com/");
check_string(cgi_makeurl("http://example.com/",