/* * Boa, an http server * Copyright (C) 1995 Paul Phillips * Some changes Copyright (C) 1996 Larry Doolittle * Some changes Copyright (C) 1996-99 Jon Nelson * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 1, or (at your option) * any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. * */ /* $Id: response.c,v 1.41.2.1 2002/06/06 05:08:54 jnelson Exp $*/ #include "boa.h" #define HTML "text/html; charset=ISO-8859-1" #define CRLF "\r\n" void print_content_type(request * req) { req_write(req, "Content-Type: "); req_write(req, get_mime_type(req->request_uri)); req_write(req, "\r\n"); } void print_content_length(request * req) { req_write(req, "Content-Length: "); req_write(req, simple_itoa(req->filesize)); req_write(req, "\r\n"); } void print_last_modified(request * req) { static char lm[] = "Last-Modified: " " " "\r\n"; rfc822_time_buf(lm + 15, req->last_modified); req_write(req, lm); } void print_ka_phrase(request * req) { if (req->kacount > 0 && req->keepalive == KA_ACTIVE && req->response_status < 500) { req_write(req, "Connection: Keep-Alive\r\nKeep-Alive: timeout="); req_write(req, simple_itoa(ka_timeout)); req_write(req, ", max="); req_write(req, simple_itoa(req->kacount)); req_write(req, "\r\n"); } else req_write(req, "Connection: close\r\n"); } void print_http_headers(request * req) { static char stuff[] = "Date: " " " "\r\nServer: " SERVER_VERSION "\r\n"; rfc822_time_buf(stuff + 6, 0); req_write(req, stuff); print_ka_phrase(req); } /* The routines above are only called by the routines below. * The rest of Boa only enters through the routines below. */ /* R_REQUEST_OK: 200 */ void send_r_request_ok(request * req) { req->response_status = R_REQUEST_OK; if (req->simple) return; req_write(req, "HTTP/1.0 200 OK\r\n"); print_http_headers(req); if (!req->is_cgi) { print_content_length(req); print_last_modified(req); print_content_type(req); req_write(req, "\r\n"); } } /* R_MOVED_PERM: 301 */ void send_r_moved_perm(request * req, char *url) { SQUASH_KA(req); req->response_status = R_MOVED_PERM; if (!req->simple) { req_write(req, "HTTP/1.0 301 Moved Permanently\r\n"); print_http_headers(req); req_write(req, "Content-Type: " HTML "\r\n"); req_write(req, "Location: "); req_write_escape_http(req, url); req_write(req, "\r\n\r\n"); } if (req->method != M_HEAD) { req_write(req, "301 Moved Permanently\n" "\n

301 Moved

The document has moved\n" "here.\n\n"); } req_flush(req); } /* R_MOVED_TEMP: 302 */ void send_r_moved_temp(request * req, char *url, char *more_hdr) { SQUASH_KA(req); req->response_status = R_MOVED_TEMP; if (!req->simple) { req_write(req, "HTTP/1.0 302 Moved Temporarily\r\n"); print_http_headers(req); req_write(req, "Content-Type: " HTML "\r\n"); req_write(req, "Location: "); req_write_escape_http(req, url); req_write(req, "\r\n"); req_write(req, more_hdr); req_write(req, "\r\n\r\n"); } if (req->method != M_HEAD) { req_write(req, "302 Moved Temporarily\n" "\n

302 Moved

The document has moved\n" "here.\n\n"); } req_flush(req); } /* R_NOT_MODIFIED: 304 */ void send_r_not_modified(request * req) { SQUASH_KA(req); req->response_status = R_NOT_MODIFIED; req_write(req, "HTTP/1.0 304 Not Modified\r\n"); print_http_headers(req); print_content_type(req); req_write(req, "\r\n"); req_flush(req); } /* R_BAD_REQUEST: 400 */ void send_r_bad_request(request * req) { SQUASH_KA(req); req->response_status = R_BAD_REQUEST; if (!req->simple) { req_write(req, "HTTP/1.0 400 Bad Request\r\n"); print_http_headers(req); req_write(req, "Content-Type: " HTML "\r\n\r\n"); /* terminate header */ } if (req->method != M_HEAD) req_write(req, "400 Bad Request\n" "

400 Bad Request

\nYour client has issued " "a malformed or illegal request.\n\n"); req_flush(req); } /* R_UNAUTHORIZED: 401 */ void send_r_unauthorized(request * req, char *realm_name) { SQUASH_KA(req); req->response_status = R_UNAUTHORIZED; if (!req->simple) { req_write(req, "HTTP/1.0 401 Unauthorized\r\n"); print_http_headers(req); req_write(req, "WWW-Authenticate: Basic realm=\""); req_write(req, realm_name); req_write(req, "\"\r\n"); req_write(req, "Content-Type: " HTML "\r\n\r\n"); /* terminate header */ } if (req->method != M_HEAD) { req_write(req, "401 Unauthorized\n" "

401 Unauthorized

\nYour client does not " "have permission to get URL "); req_write_escape_html(req, req->request_uri); req_write(req, " from this server.\n\n"); } req_flush(req); } /* R_FORBIDDEN: 403 */ void send_r_forbidden(request * req) { SQUASH_KA(req); req->response_status = R_FORBIDDEN; if (!req->simple) { req_write(req, "HTTP/1.0 403 Forbidden\r\n"); print_http_headers(req); req_write(req, "Content-Type: " HTML "\r\n\r\n"); /* terminate header */ } if (req->method != M_HEAD) { req_write(req, "403 Forbidden\n" "

403 Forbidden

\nYour client does not " "have permission to get URL "); req_write_escape_html(req, req->request_uri); req_write(req, " from this server.\n\n"); } req_flush(req); } /* R_NOT_FOUND: 404 */ void send_r_not_found(request * req) { SQUASH_KA(req); req->response_status = R_NOT_FOUND; if (!req->simple) { req_write(req, "HTTP/1.0 404 Not Found\r\n"); print_http_headers(req); req_write(req, "Content-Type: " HTML "\r\n\r\n"); /* terminate header */ } if (req->method != M_HEAD) { req_write(req, "404 Not Found\n" "

404 Not Found

\nThe requested URL "); req_write_escape_html(req, req->request_uri); req_write(req, " was not found on this server.\n\n"); } req_flush(req); } /* R_ERROR: 500 */ void send_r_error(request * req) { SQUASH_KA(req); req->response_status = R_ERROR; if (!req->simple) { req_write(req, "HTTP/1.0 500 Server Error\r\n"); print_http_headers(req); req_write(req, "Content-Type: " HTML "\r\n\r\n"); /* terminate header */ } if (req->method != M_HEAD) { req_write(req, "500 Server Error\n" "

500 Server Error

\nThe server encountered " "an internal error and could not complete your request.\n" "\n"); } req_flush(req); } /* R_NOT_IMP: 501 */ void send_r_not_implemented(request * req) { SQUASH_KA(req); req->response_status = R_NOT_IMP; if (!req->simple) { req_write(req, "HTTP/1.0 501 Not Implemented\r\n"); print_http_headers(req); req_write(req, "Content-Type: " HTML "\r\n\r\n"); /* terminate header */ } if (req->method != M_HEAD) { req_write(req, "501 Not Implemented\n" "

501 Not Implemented

\nPOST to non-script " "is not supported in Boa.\n\n"); } req_flush(req); } /* R_BAD_GATEWAY: 502 */ void send_r_bad_gateway(request * req) { SQUASH_KA(req); req->response_status = R_BAD_GATEWAY; if (!req->simple) { req_write(req, "HTTP/1.0 502 Bad Gateway" CRLF); print_http_headers(req); req_write(req, "Content-Type: " HTML CRLF CRLF); /* terminate header */ } if (req->method != M_HEAD) { req_write(req, "502 Bad Gateway\n" "

502 Bad Gateway

\nThe CGI was " "not CGI/1.1 compliant.\n" "\n"); } req_flush(req); } /* R_SERVICE_UNAVAILABLE: 503 */ void send_r_service_unavailable(request * req) /* 503 */ { static char body[] = "503 Service Unavailable\n" "

503 Service Unavailable

\n" "There are too many connections in use right now.\r\n" "Please try again later.\r\n\n"; static int _body_len; static char *body_len; if (!_body_len) _body_len = strlen(body); if (!body_len) body_len = strdup(simple_itoa(_body_len)); if (!body_len) { log_error_time(); perror("strdup of _body_len from simple_itoa"); } SQUASH_KA(req); req->response_status = R_SERVICE_UNAV; if (!req->simple) { req_write(req, "HTTP/1.0 503 Service Unavailable\r\n"); print_http_headers(req); if (body_len) { req_write(req, "Content-Length: "); req_write(req, body_len); req_write(req, "\r\n"); } req_write(req, "Content-Type: " HTML "\r\n\r\n"); /* terminate header */ } if (req->method != M_HEAD) { req_write(req, body); } req_flush(req); } /* R_NOT_IMP: 505 */ void send_r_bad_version(request * req) { SQUASH_KA(req); req->response_status = R_BAD_VERSION; if (!req->simple) { req_write(req, "HTTP/1.0 505 HTTP Version Not Supported\r\n"); print_http_headers(req); req_write(req, "Content-Type: " HTML "\r\n\r\n"); /* terminate header */ } if (req->method != M_HEAD) { req_write(req, "505 HTTP Version Not Supported\n" "

505 HTTP Version Not Supported

\nHTTP versions " "other than 0.9 and 1.0 " "are not supported in Boa.\n

Version encountered: "); req_write(req, req->http_version); req_write(req, "

\n"); } req_flush(req); }