static int child(void) { struct kreq r; const char *page[] = { "index", "test" }; struct kfcgi *fcgi; enum kcgi_err er; if (KCGI_OK != khttp_fcgi_init(&fcgi, NULL, 0, page, 2, 0)) return(0); while (KCGI_OK == (er = khttp_fcgi_parse(fcgi, &r))) { if (1 != r.page) { khttp_free(&r); khttp_fcgi_free(fcgi); return(0); } khttp_head(&r, kresps[KRESP_STATUS], "%s", khttps[KHTTP_200]); khttp_head(&r, kresps[KRESP_CONTENT_TYPE], "%s", kmimetypes[KMIME_TEXT_HTML]); khttp_body(&r); khttp_free(&r); } khttp_fcgi_free(fcgi); return(KCGI_HUP == er ? 1 : 0); }
static int child(void) { struct kreq r; const char *page = "index"; int rc; rc = 0; if (khttp_fcgi_test()) return(0); if (KCGI_OK != khttp_parse(&r, NULL, 0, &page, 1, 0)) return(0); if (KAUTH_DIGEST != r.rawauth.type) goto out; else if (0 == r.rawauth.authorised) goto out; else if (strcmp(r.rawauth.d.digest.user, "Mufasa")) goto out; else if (strcmp(r.rawauth.d.digest.realm, "*****@*****.**")) goto out; else if (strcmp(r.rawauth.d.digest.uri, "/dir/index.html")) goto out; else if (khttpdigest_validate(&r, "Circle Of Life") <= 0) goto out; khttp_head(&r, kresps[KRESP_STATUS], "%s", khttps[KHTTP_200]); khttp_head(&r, kresps[KRESP_CONTENT_TYPE], "%s", kmimetypes[KMIME_TEXT_HTML]); khttp_body(&r); rc = 1; out: khttp_free(&r); return(rc); }
static int child(void) { struct kreq r; const char *page = "index"; struct kvalid valid = { NULL, "tag" }; if (KCGI_OK != khttp_parse(&r, &valid, 1, &page, 1, 0)) return(0); if (NULL == r.fieldmap[0]) { khttp_free(&r); return(0); } else if (BUFSZ != r.fieldmap[0]->valsz) { khttp_free(&r); return(0); } khttp_head(&r, kresps[KRESP_STATUS], "%s", khttps[KHTTP_200]); khttp_head(&r, kresps[KRESP_CONTENT_TYPE], "%s", kmimetypes[KMIME_TEXT_HTML]); khttp_body(&r); khttp_write(&r, r.fieldmap[0]->val, r.fieldmap[0]->valsz); khttp_free(&r); return(1); }
static int child(void) { struct kreq r; const char *page = "index"; size_t i, found1, found2; if (KCGI_OK != khttp_parse(&r, NULL, 0, &page, 1, 0)) return(0); found1 = found2 = 0; for (i = 0; i < r.reqsz; i++) { if (0 == strcmp(r.reqs[i].key, "Testing")) found1 += 0 == strcmp(r.reqs[i].val, "123"); else if (0 == strcmp(r.reqs[i].key, "Testing-Test")) found2 += 0 == strcmp(r.reqs[i].val, "321"); } if (1 != found1 || 1 != found2) return(0); khttp_head(&r, kresps[KRESP_STATUS], "%s", khttps[KHTTP_200]); khttp_head(&r, kresps[KRESP_CONTENT_TYPE], "%s", kmimetypes[KMIME_TEXT_HTML]); khttp_body(&r); khttp_free(&r); return(1); }
/* * Open an HTTP response with a status code and a particular * content-type, then open the HTTP content body. * We'll usually just use the same content type... */ static void resp_open(struct kreq *req, enum khttp http) { khttp_head(req, kresps[KRESP_STATUS], "%s", khttps[http]); khttp_head(req, kresps[KRESP_CONTENT_TYPE], "%s", kmimetypes[req->mime]); khttp_body(req); }
void method_options(struct kreq *r) { khttp_head(r, kresps[KRESP_STATUS], "%s", khttps[KHTTP_200]); khttp_head(r, kresps[KRESP_ALLOW], "%s", "OPTIONS, GET, PUT, PROPFIND, PROPPATCH"); khttp_head(r, "DAV", "%s", "1, access-control, calendar-access, calendar-proxy"); khttp_body(r); }
int main(int argc, char *argv[]) { struct kreq req; struct kfcgi *fcgi; enum kcgi_err er; const char *pname; int rc, c, debug; if ((pname = strrchr(argv[0], '/')) == NULL) pname = argv[0]; else ++pname; debug = 0; while (-1 != (c = getopt(argc, argv, "d:"))) switch (c) { case ('d'): debug = atoi(optarg); break; default: return(EXIT_FAILURE); } if (KCGI_OK != khttp_fcgi_init(&fcgi, NULL, 0, NULL, 0, 0)) return(EXIT_FAILURE); for (rc = 0;;) { er = khttp_fcgi_parse(fcgi, &req); if (KCGI_HUP == er) { rc = 1; khttp_free(&req); break; } else if (KCGI_OK != er) { khttp_free(&req); break; } khttp_head(&req, kresps[KRESP_STATUS], "%s", khttps[KHTTP_200]); khttp_head(&req, kresps[KRESP_CONTENT_TYPE], "%s", kmimetypes[req.mime]); khttp_body(&req); khttp_puts(&req, "Hello, world!\n"); khttp_free(&req); if (debug > 0 && 0 == --debug) break; } khttp_fcgi_free(fcgi); return(rc ? EXIT_SUCCESS : EXIT_FAILURE); }
static int child(void) { struct kreq r; const char *page = "index"; struct kvalid valid = { NULL, "tag" }; size_t i; struct kfcgi *fcgi; enum kcgi_err er; if (KCGI_OK != khttp_fcgi_init(&fcgi, &valid, 1, &page, 1, 0)) return(0); while (KCGI_OK == (er = khttp_fcgi_parse(fcgi, &r))) { if (NULL == r.fieldmap[0]) { khttp_free(&r); khttp_fcgi_free(fcgi); return(0); } else if (1024 * 1024 != r.fieldmap[0]->valsz) { khttp_free(&r); khttp_fcgi_free(fcgi); return(0); } for (i = 0; i < 1024 * 1024; i++) if (r.fieldmap[0]->val[i] != (i % 10) + 65) { khttp_free(&r); khttp_fcgi_free(fcgi); return(0); } khttp_head(&r, kresps[KRESP_STATUS], "%s", khttps[KHTTP_200]); khttp_head(&r, kresps[KRESP_CONTENT_TYPE], "%s", kmimetypes[KMIME_TEXT_HTML]); khttp_body(&r); khttp_write(&r, r.fieldmap[0]->val, r.fieldmap[0]->valsz); khttp_free(&r); } khttp_free(&r); khttp_fcgi_free(fcgi); return(KCGI_HUP == er ? 1 : 0); }
static int child(void) { struct kreq r; const char *page = "index"; if (KCGI_OK != khttp_parse(&r, NULL, 0, &page, 1, 0)) return(0); khttp_head(&r, kresps[KRESP_STATUS], "%s", khttps[KHTTP_200]); khttp_head(&r, kresps[KRESP_CONTENT_TYPE], "%s", kmimetypes[KMIME_TEXT_HTML]); if ( ! khttp_body(&r)) return(0); khttp_puts(&r, "1234567890"); khttp_free(&r); return(1); }
void http_error(struct kreq *r, enum khttp c) { char nonce[17]; size_t i; khttp_head(r, kresps[KRESP_STATUS], "%s", khttps[c]); switch (c) { case (KHTTP_200): case (KHTTP_201): case (KHTTP_204): case (KHTTP_207): case (KHTTP_304): khttp_head(r, "DAV", "1, access-control, calendar-access"); break; case (KHTTP_401): /* * FIXME: we essentially throw away the nonce * We should be keeping nonces in a database of client * sessions, then using the increasing (not necessarily * linearly, apparently) nonce-count value. */ for (i = 0; i < sizeof(nonce) - 1; i++) snprintf(nonce + i, 2, "%01X", arc4random_uniform(128)); khttp_head(r, kresps[KRESP_WWW_AUTHENTICATE], "Digest realm=\"%s\", " "algorithm=\"MD5-sess\", " "qop=\"auth,auth-int\", " "nonce=\"%s\"", KREALM, nonce); break; default: break; } khttp_body(r); }
static int child(void) { struct kreq r; const char *page = "index"; struct kfcgi *fcgi; enum kcgi_err er; size_t i; struct stat st; if (KCGI_OK != khttp_fcgi_init(&fcgi, NULL, 0, &page, 1, 0)) return(0); while (KCGI_OK == (er = khttp_fcgi_parse(fcgi, &r))) { khttp_head(&r, kresps[KRESP_STATUS], "%s", khttps[KHTTP_200]); khttp_head(&r, kresps[KRESP_CONTENT_TYPE], "%s", kmimetypes[KMIME_TEXT_HTML]); khttp_body(&r); for (i = 0; i < r.fieldsz; i++) { if (strcmp(r.fields[i].key, "picture")) continue; if (NULL == r.fields[i].file) return(0); if (-1 == stat(r.fields[i].file, &st)) { perror(r.fields[i].file); return(0); } else if ((size_t)st.st_size != r.fields[i].valsz) return(0); } khttp_free(&r); } khttp_free(&r); khttp_fcgi_free(fcgi); return(KCGI_HUP == er ? 1 : 0); }