static int ht_process(struct channel *ch) { char buf[4096]; struct buffer bu = buffer_wrap(buf); struct env env; char method[12]; char uri[512]; char version[10]; const char *host; if (ht_method(ch, &bu, sizeof(method), method, sizeof(uri), uri, sizeof(version), version)) return -1; if (strcasecmp(version, "HTTP/1.1")) /* require HTTP/1.1 */ return -1; if (ht_headers(ch, &bu, &env)) return -1; host = env_get(&env, "Host"); if (!host) { ht_response(ch, 400); ch_puts(ch, "Content-Type: text/plain\r\n"); ch_puts(ch, "Connection: close\r\n"); ch_puts(ch, "\r\n"); ch_puts(ch, "require Host in HTTP/1.1 headers.\n"); return 0; } printf("method=%s\n", method); printf("uri=%s\n", uri); printf("version=%s\n", version); printf("Host=%s\n", host); ht_response(ch, 200); /* send some basic headers */ ch_puts(ch, "Content-Type: text/plain\r\n"); ch_puts(ch, "Connection: close\r\n"); ch_puts(ch, "\r\n"); // TODO: output a real response ch_puts(ch, "Hello World!\n"); ch_printf(ch, "\n\turi=%s\n\thost=%s\n", uri, host); return 0; }
void ht_response(struct channel *ch, int status_code) { const char *resp; switch (status_code) { case 200: resp = "HTTP/1.1 200 OK\r\n"; break; case 400: resp = "HTTP/1.1 400 Bad Request\r\n"; break; case 403: resp = "HTTP/1.1 403 Forbidden\r\n"; case 404: resp = "HTTP/1.1 404 Not Found\r\n"; default: case 500: resp = "HTTP/1.1 500 Internal Server Error\r\n"; break; case 501: resp = "HTTP/1.1 501 Not Implemented\r\n"; break; case 502: resp = "HTTP/1.1 502 Bad Gateway\r\n"; break; case 503: resp = "HTTP/1.1 503 Service Unavailable\r\n"; break; case 504: resp = "HTTP/1.1 504 Gateway Timeout\r\n"; break; case 505: resp = "HTTP/1.1 505 HTTP Version Not Supported\r\n"; break; } Debug("CHAN=%s status_code=%d\n", ch->desc, status_code); ch_puts(ch, resp); }
static int ht_process(struct channel *ch) { char buf[4096]; struct buffer bu = buffer_wrap(buf); struct env env; char method[12]; char uri[512]; char version[10]; const char *host; struct service *se; struct module *mo; struct domain *dom; const char *sub; if (ht_method(ch, &bu, sizeof(method), method, sizeof(uri), uri, sizeof(version), version)) return -1; if (strcasecmp(version, "HTTP/1.1")) /* require HTTP/1.1 */ return -1; if (ht_headers(ch, &bu, &env)) return -1; host = env_get(&env, "Host"); if (!host) { ht_response(ch, 400); ch_puts(ch, "Content-Type: text/plain\r\n"); ch_puts(ch, "Connection: close\r\n"); ch_puts(ch, "\r\n"); ch_puts(ch, "require Host in HTTP/1.1 headers.\n"); return 0; } dom = ha_find_dom(host); if (!dom) { ht_response(ch, 403); ch_puts(ch, "Content-Type: text/plain\r\n"); ch_puts(ch, "Connection: close\r\n"); ch_puts(ch, "\r\n"); ch_printf(ch, "Host \"%s\" is unavailable.\n\n", host); return 0; } Debug("method=\"%s\"\n", method); Debug("uri=\"%s\"\n", uri); Debug("version=\"%s\"\n", version); Debug("Host=\"%s\" (%s)\n", host, dom->name); se = se_find(dom, uri, &sub); if (!se) { ht_response(ch, 404); ch_puts(ch, "Content-Type: text/plain\r\n"); ch_puts(ch, "Connection: close\r\n"); ch_puts(ch, "\r\n"); ch_printf(ch, "Request-URI \"%s\" is unavailable.\n\n", uri); ch_printf(ch, "dom=\"%s\"\n", dom->name); ch_printf(ch, "host=\"%s\"\n", host); return 0; } mo = se->module; ht_response(ch, 200); /* send some basic headers */ ch_puts(ch, "Content-Type: text/plain\r\n"); ch_puts(ch, "Connection: close\r\n"); ch_puts(ch, "\r\n"); // TODO: output a real response ch_puts(ch, "Hello World!\n"); ch_printf(ch, "\n\turi=%s\n\thost=%s\n\tmodule=%s\n\tsubpath=%s\n", uri, dom->name, mo ? mo->name : "(none)", sub); return 0; }