Beispiel #1
0
void die_with_error(meta_error e, const char *fmt, ...)
{
    va_list ap;
    int rc;

    va_start(ap, fmt);

    if (is_tcpip_error(e))
        syslog(LOG_ERR, "A tcp/ip error has occured");
    else if (is_protocol_error(e))
        syslog(LOG_ERR, "A protocol error has occured");
    else if (is_app_error(e))
        syslog(LOG_ERR, "A application error has occured");
    else if (is_os_error(e))
        syslog(LOG_ERR, "A os error has occured");
    else if (is_db_error(e))
        syslog(LOG_ERR, "A database error has occured");
    else if (is_other_error(e))
        syslog(LOG_ERR, "An unknown error has occured");

    if (has_error_message(e))
        syslog(LOG_ERR, "Error message: %s", get_error_message(e));
    else if ((rc = get_error_code(e)) != 0)
        syslog(LOG_ERR, "Possible error: %d %s\n", rc, strerror(rc));

    meta_vsyslog(LOG_ERR, fmt, ap);
    va_end(ap);

    exit(EXIT_FAILURE);
}
Beispiel #2
0
static int serviceConnection2(
	http_server srv,
	connection conn,
	http_request request,
	http_response response,
	meta_error e)
{
	dynamic_page dp;
	size_t cbSent;
	int status, error = 0;
	size_t max_posted_content = http_server_get_post_limit(srv);

	while(!http_server_shutting_down(srv)) { 
		if(!data_on_socket(conn)) 
			return set_tcpip_error(e, EAGAIN); 

		/* Were we able to read a valid http request? 
		 * If not, what is the cause of the error? If it is a http 
		 * protocol error, we try to send a response back to the client
		 * and close the connection. If it is anything else(tcp/ip, os)
		 * we stop processing. 
		 */
		error = !request_receive(request, conn, max_posted_content, e);

		/* So far, so good. We have a valid HTTP request.
		 * Now see if we can locate a page handler function for it.
		 * If we do, call it. If not, see if it on disk or if the
		 * http_server has a default page handler. If neither is true,
		 * then the page was not found(404).
		 */
		if(error)
			;
		else if( (dp = http_server_lookup(srv, request)) != NULL) {
			if(!handle_dynamic(srv, dp, request, response, e)) {
				error = 1;
			}
		}
		else if(http_server_can_read_files(srv)) {
			if(!send_disk_file(srv, conn, request, response, e)) {
				error = 1;
			}
		}
		else if(http_server_has_default_page_handler(srv)) {
			if(!http_server_run_default_page_handler(srv, request, response, e)) {
				error = 1;
			}
		}
		else {
			/* We didn't find the page */
			response_set_status(response, HTTP_404_NOT_FOUND);
			response_set_connection(response, "close");
		}

		if(error) {
			if(is_protocol_error(e)) {
				status = get_error_code(e);
				response_set_status(response, status);
				response_set_connection(response, "close");
				cbSent = response_send(response, conn, e);
				http_server_add_logentry(srv, conn, request, status, cbSent);
			}

			return 0;
		}

		/*
		 * Some extra stuff for HTTP 1.0 clients. If client is 1.0
		 * and connection_close() == 1 and connection header field
		 * isn't set, then we set the connection flag to close.
		 * Done so that 1.0 clients (Lynx) can detect closure.
		 */
		if(request_get_version(request) != VERSION_11
		&& !connection_is_persistent(conn)
		&& strlen(response_get_connection(response)) == 0) 
			response_set_connection(response, "close");
		
		cbSent = response_send(response, conn, e);
		http_server_add_logentry(srv, conn, request, response_get_status(response), cbSent);
		if(cbSent == 0)
			return 0;

		/* Did the user set the Connection header field to "close" */
		if(strcmp(response_get_connection(response), "close") == 0)
			return 1;

		if(!connection_is_persistent(conn))
			return 1;

		/**
		 * NOTE: Her må/bør vi legge inn ny funksjonalitet:
		 * Disconnect connections som 
		 * a) Har kjørt lengst i tid
		 * b) Har overført mest bytes (opp eller ned)
		 * eller c) har dårligst transfer rate
		 * Grunnen er at dersom vi har n worker threads og n 
		 * persistent connections, havner alle nye connections i kø.
		 * De får aldri kjøretid. Så disconnect-regelen over gjelder derfor
		 * kun om køen har > 0 entries. 
		 */

		connection_flush(conn);
		request_recycle(request);
		response_recycle(response);
	}

	/* Shutdown detected */
	return 1;
}