|
NAMEkcgi —
minimal CGI and FastCGI library in C
LIBRARYlibrary “libkcgi”SYNOPSIS#include <sys/types.h>
#include <stdarg.h>
#include <stdint.h>
#include <kcgi.h>
DESCRIPTIONThekcgi library handles the CGI or FastCGI environment
for C web applications. Interfacing applications generally work as follows:
It can also accept FastCGI connections in a manner similar to the above, except that the parse routine is run within a loop.
To compile applications with #include <sys/types.h> /* size_t, ssize_t */ #include <stdarg.h> /* va_list */ #include <stdint.h> /* int64_t */ #include <kcgi.h> To compile and link, use pkg-config(1): % cc `pkg-config --cflags kcgi` -c -o sample.o sample.c % cc -o sample sample.o `pkg-config --libs kcgi` If the library will be statically linked (e.g., for running within a chroot(2)), link as follows: % cc `pkg-config --static --cflags kcgi` -c -o sample.o sample.c % cc -static -o sample sample.o `pkg-config --static --libs kcgi` The current version of the library is defined in
Pledge PromisesThekcgi library is built to operate in
security-sensitive environments, including
pledge(2)
on OpenBSD. The following is a list of all required
promises.
EXAMPLESThe following simple example assumes that kcgi.h is in the compiler's include path. None of them perform error checking: each call into thekcgi library should have its error code
checked!
The following does nothing but emit “Hello, world!” to the output. #include <sys/types.h> /* size_t, ssize_t */ #include <stdarg.h> /* va_list */ #include <stddef.h> /* NULL */ #include <stdint.h> /* int64_t */ #include <kcgi.h> int main(void) { struct kreq r; const char *page = "index"; if (khttp_parse(&r, NULL, 0, &page, 1, 0) != KCGI_OK) return 1; khttp_head(&r, kresps[KRESP_STATUS], "%s", khttps[KHTTP_200]); khttp_head(&r, kresps[KRESP_CONTENT_TYPE], "%s", kmimetypes[r.mime]); khttp_body(&r); khttp_puts(&r, "Hello, world!"); khttp_free(&r); return 0; } This can be extended to work with the FastCGI interface by allowing the request parser to operate within a loop. #include <sys/types.h> /* size_t, ssize_t */ #include <stdarg.h> /* va_list */ #include <stddef.h> /* NULL */ #include <stdint.h> /* int64_t */ #include <kcgi.h> int main(void) { struct kreq r; struct kfcgi *fcgi; const char *page = "index"; if (khttp_fcgi_init(&fcgi, NULL, 0, &page, 1, 0) != KCGI_OK) return 1; while (khttp_fcgi_parse(fcgi, &r) == KCGI_OK) { khttp_head(&r, kresps[KRESP_STATUS], "%s", khttps[KHTTP_200]); khttp_head(&r, kresps[KRESP_CONTENT_TYPE], "%s", kmimetypes[r.mime]); khttp_body(&r); khttp_puts(&r, "Hello, world!"); khttp_free(&r); } khttp_fcgi_free(fcgi); return 0; } In a more complicated example, #include <sys/types.h> /* size_t, ssize_t */ #include <stdarg.h> /* va_list */ #include <stdint.h> /* int64_t */ #include <kcgi.h> int main(void) { struct kreq r; struct kpair *p; const char *page = "index"; struct kvalid key = { kvalid_stringne, "string" }; if (khttp_parse(&r, &key, 1, &page, 1, 0) != KCGI_OK) return 1; khttp_head(&r, kresps[KRESP_STATUS], "%s", khttps[KHTTP_200]); khttp_head(&r, kresps[KRESP_CONTENT_TYPE], "%s", kmimetypes[r.mime]); khttp_body(&r); khttp_puts(&r, "Result: "); if ((p = r.fieldmap[0])) khttp_puts(&r, p->parsed.s); else if (r.fieldnmap[0]) khttp_puts(&r, "bad parse"); else khttp_puts(&r, "no value"); khttp_free(&r); return 0; } Applications will usually specify an array of key-value pairs to validate; or in the event of web services, a default validator (empty string) for the full HTTP message body. SEE ALSOkcgi_buf_printf(3), kcgi_buf_putc(3), kcgi_buf_puts(3), kcgi_buf_write(3), kcgi_strerror(3), kcgi_writer_disable(3), kcgihtml(3), kcgijson(3), kcgiregress(3), kcgixml(3), khttp_body(3), khttp_epoch2str(3), khttp_fcgi_free(3), khttp_fcgi_init(3), khttp_fcgi_parse(3), khttp_fcgi_test(3), khttp_free(3), khttp_head(3), khttp_parse(3), khttp_printf(3), khttp_putc(3), khttp_puts(3), khttp_template(3), khttp_templatex(3), khttp_urlencode(3), khttp_write(3), khttpbasic_validate(3), khttpdigest_validate(3), kmalloc(3), kutil_invalidate(3), kutil_log(3), kutil_openlog(3), kvalid_string(3), kfcgi(8)STANDARDSMany standards are involved in thekcgi library, most
significantly being draft RFC 3875, “The Common Gateway Interface (CGI)
Version 1.1”, and the “FastCGI Specification”, version
1.0, published 29 April 1996.
Additional HTTP methods are defined by RFC 4918, “HTTP Extensions for Web Distributed Authoring and Versioning”; and RFC 4791 , “Calendaring Extensions to WebDAV”. AUTHORSThekcgi library was written by
Kristaps Dzonsons
<kristaps@bsd.lv>.
Visit the GSP FreeBSD Man Page Interface. |