コード例 #1
0
ファイル: main.c プロジェクト: kl3mz/webserver
static ret_t
do_download__has_headers (cherokee_downloader_t *downloader, void *param)
{
    cherokee_url_t    *url;
    cherokee_buffer_t *req;
    cherokee_header_t *hdr;

    UNUSED(param);

    url = &downloader->request.url;
    req = &url->request;
    hdr = downloader->header;

    TRACE(ENTRIES, "quiet=%d http_code=%d\n", quiet, hdr->response);

    /* Check the response
     */
    if (quiet == false) {
        cherokee_buffer_t tmp = CHEROKEE_BUF_INIT;

        cherokee_http_code_copy (HDR_RESPONSE(hdr), &tmp);
        print_tuple_str ("Response", tmp.buf);
        cherokee_buffer_mrproper (&tmp);
    }

    /* Open a new file if needed
     */
    if (global_fd == UNSET_FD) {
        char *name;

        name = strrchr (req->buf, '/');
        if (name == NULL) {
            name = "index.html";
        }

        output_fd = open (name, O_WRONLY, O_CREAT);
        if (output_fd < 0) {
            return ret_error;
        }

    } else {
        output_fd = global_fd;
    }

    /* Save the headers?
     */
    if (save_headers == true) {
        ssize_t  written;
        uint32_t len;

        cherokee_header_get_length (hdr, &len);
        written = write (output_fd, hdr->input_buffer->buf, len);
        if (written < 0) {
            PRINT_MSG_S ("ERROR: Can not write to output file\n");
            return ret_error;
        }
    }

    return ret_ok;
}
コード例 #2
0
ファイル: handler_error.c プロジェクト: BeQ/webserver
static ret_t
build_hardcoded_response_page (cherokee_connection_t *conn, cherokee_buffer_t *buffer)
{
	/* Avoid too many reallocations.
	 */
	cherokee_buffer_ensure_addlen (buffer, 1000);

	/* Add document header
	 */
	cherokee_buffer_add_str (buffer, "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">" CRLF);

	/* Add page title
	 */
	cherokee_buffer_add_str (buffer, "<html>" CRLF "<head><title>");
	cherokee_http_code_copy (conn->error_code, buffer);

	/* Add big banner
	 */
	cherokee_buffer_add_str (buffer, "</title>" CRLF
				 "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />" CRLF
				 "</head>" CRLF "<body>" CRLF "<h1>");
	cherokee_http_code_copy (conn->error_code, buffer);
	cherokee_buffer_add_str (buffer, "</h1>" CRLF);

	/* Maybe add some info
	 */
	switch (conn->error_code) {
	case http_not_found:
	case http_gone:
		cherokee_buffer_add_str (buffer, "The requested URL ");
		if (! cherokee_buffer_is_empty (&conn->request_original)) {
			cherokee_buffer_add_escape_html (buffer, &conn->request_original);
		}
		else if (! cherokee_buffer_is_empty (&conn->request)) {
			if (cherokee_connection_use_webdir (conn)) {
				cherokee_buffer_add_buffer (buffer, &conn->web_directory);
			}
			cherokee_buffer_add_escape_html (buffer, &conn->request);
		}

		if (conn->error_code == http_not_found) {
			cherokee_buffer_add_str (buffer, " was not found on this server.");
		} else {
			cherokee_buffer_add_str (buffer,
				" is no longer available on this server and there is no forwarding address.");
		}
		break;

	case http_bad_request:
		cherokee_buffer_add_str (buffer,
			"Your browser sent a request that this server could not understand.");
		cherokee_buffer_add_str   (buffer, "<p><pre>");
		cherokee_buffer_add_escape_html (buffer, conn->header.input_buffer);
		cherokee_buffer_add_str   (buffer, "</pre>");
		break;

	case http_access_denied:
		cherokee_buffer_add_str (buffer,
			"You have no access to the requested URL");
		break;

	case http_request_entity_too_large:
		cherokee_buffer_add_str (buffer,
			"The length of request entity exceeds the capacity limit for this server.");
		break;

	case http_request_uri_too_long:
		cherokee_buffer_add_str (buffer,
			"The length of requested URL exceeds the capacity limit for this server.");
		break;

	case http_range_not_satisfiable:
		cherokee_buffer_add_str (buffer,
			"The requested range was not satisfiable.");
		break;

	case http_moved_permanently:
	case http_moved_temporarily:
		cherokee_buffer_add_str         (buffer, "The document has moved <a href=\"");
		cherokee_buffer_add_escape_html (buffer, &conn->redirect);
		cherokee_buffer_add_str         (buffer, "\">here</a>.");
		break;

	case http_unauthorized:
		cherokee_buffer_add_str (buffer,
			"This server could not verify that you are authorized to access the requested URL.  "
			"Either you supplied the wrong credentials (e.g., bad password), "
			"or your browser doesn't know how to supply the credentials required.");
		break;

	case http_upgrade_required:
		cherokee_buffer_add_str (buffer,
			"The requested resource can only be retrieved using SSL.  The server is "
			"willing to upgrade the current connection to SSL, but your client doesn't "
			"support it. Either upgrade your client, or try requesting the page "
			"using https://");
		break;

	case http_unset:
		SHOULDNT_HAPPEN;
		break;

	default:
		break;
	}

	/* Add page footer
	 */
	cherokee_buffer_add_str (buffer, CRLF "<p><hr>" CRLF);
	cherokee_buffer_add_buffer (buffer, &CONN_BIND(conn)->server_string_w_port);
	cherokee_buffer_add_str (buffer, CRLF "</body>" CRLF "</html>" CRLF);

	return ret_ok;
}