1
0
mirror of https://github.com/lammertb/libhttp.git synced 2025-08-06 05:02:40 +03:00

Error handling via httplib_cry

This commit is contained in:
Lammert Bies
2017-01-01 01:26:22 +01:00
parent 52a8a20d2f
commit 3a9919eb50
7 changed files with 47 additions and 46 deletions

View File

@@ -883,7 +883,7 @@ enum { TIMEOUT_INFINITE = -1 };
On success, >= 0
On error/timeout, < 0
*/
LIBHTTP_API int httplib_get_response(struct httplib_connection *conn, char *ebuf, size_t ebuf_len, int timeout);
LIBHTTP_API int httplib_get_response( struct httplib_connection *conn, int timeout );
@@ -906,11 +906,11 @@ LIBHTTP_API unsigned httplib_check_feature( unsigned feature );
LIBHTTP_API int httplib_closedir( DIR *dir );
LIBHTTP_API struct httplib_connection * httplib_connect_client( struct httplib_context *ctx, const char *host, int port, int use_ssl );
LIBHTTP_API struct httplib_connection * httplib_connect_client_secure( struct httplib_context *ctx, const struct httplib_client_options *client_options );
LIBHTTP_API struct httplib_connection * httplib_connect_websocket_client( struct httplib_context *ctx, const char *host, int port, int use_ssl, char *error_buffer, size_t error_buffer_size, const char *path, const char *origin, httplib_websocket_data_handler data_func, httplib_websocket_close_handler close_func, void *user_data );
LIBHTTP_API struct httplib_connection * httplib_connect_websocket_client( struct httplib_context *ctx, const char *host, int port, int use_ssl, const char *path, const char *origin, httplib_websocket_data_handler data_func, httplib_websocket_close_handler close_func, void *user_data );
LIBHTTP_API struct httplib_context * httplib_create_client_context( const struct httplib_callbacks *callbacks, const struct httplib_option_t *options );
LIBHTTP_API void httplib_cry( enum debug_level_t debug_level, const struct httplib_context *ctx, const struct httplib_connection *conn, PRINTF_FORMAT_STRING(const char *fmt), ...) PRINTF_ARGS(4, 5);
LIBHTTP_API void httplib_destroy_client_context( struct httplib_context *ctx );
LIBHTTP_API struct httplib_connection * httplib_download( struct httplib_context *ctx, const char *host, int port, int use_ssl, char *error_buffer, size_t error_buffer_size, PRINTF_FORMAT_STRING(const char *request_fmt), ...) PRINTF_ARGS(7, 8);
LIBHTTP_API struct httplib_connection * httplib_download( struct httplib_context *ctx, const char *host, int port, int use_ssl, PRINTF_FORMAT_STRING(const char *request_fmt), ...) PRINTF_ARGS(5, 6);
LIBHTTP_API char * httplib_error_string( int error_code, char *buf, size_t buf_len );
LIBHTTP_API const char * httplib_get_builtin_mime_type( const char *file_name );
LIBHTTP_API enum debug_level_t httplib_get_debug_level( struct httplib_context *ctx );

View File

@@ -36,7 +36,7 @@
* returned, otherwise NULL.
*/
struct httplib_connection *httplib_connect_websocket_client( struct httplib_context *ctx, const char *host, int port, int use_ssl, char *error_buffer, size_t error_buffer_size, const char *path, const char *origin, httplib_websocket_data_handler data_func, httplib_websocket_close_handler close_func, void *user_data ) {
struct httplib_connection *httplib_connect_websocket_client( struct httplib_context *ctx, const char *host, int port, int use_ssl, const char *path, const char *origin, httplib_websocket_data_handler data_func, httplib_websocket_close_handler close_func, void *user_data ) {
struct httplib_connection *conn;
struct websocket_client_thread_data *thread_data;
@@ -72,7 +72,7 @@ struct httplib_connection *httplib_connect_websocket_client( struct httplib_cont
* Establish the client connection and request upgrade
*/
conn = httplib_download( ctx, host, port, use_ssl, error_buffer, error_buffer_size, handshake_req, path, host, magic, origin );
conn = httplib_download( ctx, host, port, use_ssl, handshake_req, path, host, magic, origin );
if ( conn == NULL ) {
httplib_cry( DEBUG_LEVEL_ERROR, ctx, NULL, "%s (%u): Init of download failed", __func__, __LINE__ );

View File

@@ -35,7 +35,7 @@
* and returns a pointer to the connection on success, or NULL on error.
*/
struct httplib_connection * httplib_download( struct httplib_context *ctx, const char *host, int port, int use_ssl, char *ebuf, size_t ebuf_len, const char *fmt, ... ) {
struct httplib_connection * httplib_download( struct httplib_context *ctx, const char *host, int port, int use_ssl, const char *fmt, ... ) {
struct httplib_connection *conn;
va_list ap;
@@ -45,7 +45,6 @@ struct httplib_connection * httplib_download( struct httplib_context *ctx, const
if ( ctx == NULL ) return NULL;
va_start( ap, fmt );
ebuf[0] = '\0';
conn = httplib_connect_client( ctx, host, port, use_ssl );
@@ -56,7 +55,7 @@ struct httplib_connection * httplib_download( struct httplib_context *ctx, const
if ( i <= 0 ) httplib_cry( DEBUG_LEVEL_ERROR, ctx, conn, "%s (%d): error sending request", __func__, __LINE__ );
else {
XX_httplib_getreq( conn, ebuf, ebuf_len, &reqerr );
XX_httplib_getreq( ctx, conn, &reqerr );
/*
* TODO: 1) uri is deprecated;

View File

@@ -29,7 +29,7 @@
#include "httplib_string.h"
/*
* int httplib_get_response( struct httplib_connection *conn, char *ebuf, size_t ebuf_len, int timeout );
* int httplib_get_response( struct httplib_connection *conn, int timeout );
*
* The function httplib_get_response() tries to get a response from a remote
* peer. This function does some dirty action by temporarily replacing the
@@ -40,7 +40,7 @@
* place.
*/
int httplib_get_response( struct httplib_connection *conn, char *ebuf, size_t ebuf_len, int timeout ) {
int httplib_get_response( struct httplib_connection *conn, int timeout ) {
int err;
int ret;
@@ -66,7 +66,7 @@ int httplib_get_response( struct httplib_connection *conn, char *ebuf, size_t eb
else rctx.request_timeout = 0;
conn->ctx = &rctx;
ret = XX_httplib_getreq( conn, ebuf, ebuf_len, &err );
ret = XX_httplib_getreq( conn->ctx, conn, &err );
conn->ctx = octx;
/*

View File

@@ -29,30 +29,28 @@
#include "httplib_string.h"
/*
* int XX_httplib_getreq( struct httplib_connection *conn, char *ebuf, size_t ebuf_len, int *err );
* bool XX_httplib_getreq( struct httplib_context *ctx, struct httplib_connection *conn, int *err );
*
* The function XX_httplib_getreq() processes a request from a remote client.
*/
int XX_httplib_getreq( struct httplib_connection *conn, char *ebuf, size_t ebuf_len, int *err ) {
bool XX_httplib_getreq( struct httplib_context *ctx, struct httplib_connection *conn, int *err ) {
const char *cl;
if ( err == NULL ) return 0;
if ( ebuf == NULL || ebuf_len < 1 ) { *err = 500; return 0; }
if ( ctx == NULL || err == NULL ) return false;
ebuf[0] = '\0';
*err = 0;
XX_httplib_reset_per_request_attributes( conn );
*err = 0;
if ( conn == NULL ) {
XX_httplib_snprintf( conn, NULL, ebuf, ebuf_len, "%s", "Internal error" );
httplib_cry( DEBUG_LEVEL_ERROR, ctx, conn, "%s (%u): internal error", __func__, __LINE__ );
*err = 500;
return 0;
return false;
}
XX_httplib_reset_per_request_attributes( conn );
/*
* Set the time the request was received. This value should be used for
* timeouts.
@@ -68,23 +66,23 @@ int XX_httplib_getreq( struct httplib_connection *conn, char *ebuf, size_t ebuf_
if ( conn->request_len >= 0 && conn->data_len < conn->request_len ) {
XX_httplib_snprintf( conn, NULL, ebuf, ebuf_len, "%s", "Invalid request size" );
httplib_cry( DEBUG_LEVEL_ERROR, ctx, conn, "%s (%u): invalid request size", __func__, __LINE__ );
*err = 500;
return 0;
return false;
}
if ( conn->request_len == 0 && conn->data_len == conn->buf_size ) {
XX_httplib_snprintf( conn, NULL, ebuf, ebuf_len, "%s", "Request Too Large" );
httplib_cry( DEBUG_LEVEL_ERROR, ctx, conn, "%s (%u): request too large", __func__, __LINE__ );
*err = 413;
return 0;
return false;
}
else if ( conn->request_len <= 0 ) {
if ( conn->data_len > 0 ) {
XX_httplib_snprintf( conn, NULL, ebuf, ebuf_len, "%s", "Client sent malformed request" );
httplib_cry( DEBUG_LEVEL_ERROR, ctx, conn, "%s (%u): client sent malformed request", __func__, __LINE__ );
*err = 400;
}
@@ -95,17 +93,17 @@ int XX_httplib_getreq( struct httplib_connection *conn, char *ebuf, size_t ebuf_
conn->must_close = true;
XX_httplib_snprintf( conn, NULL, ebuf, ebuf_len, "%s", "Client did not send a request" );
httplib_cry( DEBUG_LEVEL_WARNING, ctx, conn, "%s (%u): client did not send a request", __func__, __LINE__ );
*err = 0;
}
return 0;
return false;
}
else if ( XX_httplib_parse_http_message( conn->buf, conn->buf_size, &conn->request_info ) <= 0 ) {
XX_httplib_snprintf( conn, NULL, ebuf, ebuf_len, "%s", "Bad Request" );
httplib_cry( DEBUG_LEVEL_ERROR, ctx, conn, "%s (%u): bad request", __func__, __LINE__ );
*err = 400;
return 0;
return false;
}
else {
@@ -124,9 +122,9 @@ int XX_httplib_getreq( struct httplib_connection *conn, char *ebuf, size_t ebuf_
if ( endptr == cl ) {
XX_httplib_snprintf( conn, NULL, ebuf, ebuf_len, "%s", "Bad Request" );
httplib_cry( DEBUG_LEVEL_ERROR, ctx, conn, "%s (%u): bad request", __func__, __LINE__ );
*err = 411;
return 0;
return false;
}
/*
@@ -167,6 +165,6 @@ int XX_httplib_getreq( struct httplib_connection *conn, char *ebuf, size_t ebuf_
}
}
return 1;
return true;
} /* XX_httplib_getreq */

View File

@@ -818,7 +818,7 @@ int XX_httplib_get_request_handler( struct httplib_connection *conn, int handl
int XX_httplib_get_request_len( const char *buf, int buflen );
void XX_httplib_get_system_name( char **sysName );
enum uri_type_t XX_httplib_get_uri_type( const char *uri );
int XX_httplib_getreq( struct httplib_connection *conn, char *ebuf, size_t ebuf_len, int *err );
bool XX_httplib_getreq( struct httplib_context *ctx, struct httplib_connection *conn, int *err );
void XX_httplib_handle_cgi_request( struct httplib_connection *conn, const char *prog );
void XX_httplib_handle_directory_request( struct httplib_connection *conn, const char *dir );
void XX_httplib_handle_file_based_request( struct httplib_connection *conn, const char *path, struct file *filep );

View File

@@ -40,7 +40,7 @@ void XX_httplib_process_new_connection( struct httplib_connection *conn ) {
struct httplib_request_info *ri;
int keep_alive;
int discard_len;
char ebuf[100];
bool was_error;
const char *hostend;
int reqerr;
enum uri_type_t uri_type;
@@ -59,9 +59,10 @@ void XX_httplib_process_new_connection( struct httplib_connection *conn ) {
*/
conn->data_len = 0;
was_error = false;
do {
if ( ! XX_httplib_getreq( conn, ebuf, sizeof(ebuf), &reqerr ) ) {
if ( ! XX_httplib_getreq( conn->ctx, conn, &reqerr ) ) {
/*
* The request sent by the client could not be understood by
@@ -69,19 +70,20 @@ void XX_httplib_process_new_connection( struct httplib_connection *conn ) {
* error message and close the connection.
*/
if ( reqerr > 0 ) {
/*assert(ebuf[0] != '\0');*/
XX_httplib_send_http_error( conn, reqerr, "%s", ebuf );
}
if ( reqerr > 0 ) XX_httplib_send_http_error( conn, reqerr, "%s", httplib_get_response_code_text( conn, reqerr ) );
was_error = true;
}
else if ( strcmp( ri->http_version, "1.0" ) && strcmp( ri->http_version, "1.1" ) ) {
XX_httplib_snprintf( conn, NULL, ebuf, sizeof(ebuf), "Bad HTTP version: [%s]", ri->http_version );
XX_httplib_send_http_error( conn, 505, "%s", ebuf );
httplib_cry( DEBUG_LEVEL_ERROR, conn->ctx, conn, "%s (%u): bad HTTP version \"%s\"", __func__, __LINE__, ri->http_version );
XX_httplib_send_http_error( conn, 505, "%s", httplib_get_response_code_text( conn, 505 ) );
was_error = true;
}
if ( ebuf[0] == '\0' ) {
if ( ! was_error ) {
uri_type = XX_httplib_get_uri_type( conn->request_info.request_uri );
@@ -107,9 +109,11 @@ void XX_httplib_process_new_connection( struct httplib_connection *conn ) {
break;
default :
XX_httplib_snprintf( conn, NULL, ebuf, sizeof(ebuf), "Invalid URI" );
XX_httplib_send_http_error( conn, 400, "%s", ebuf );
httplib_cry( DEBUG_LEVEL_ERROR, conn->ctx, conn, "%s (%u): invalid URI", __func__, __LINE__ );
XX_httplib_send_http_error( conn, 400, "%s", httplib_get_response_code_text( conn, 400 ) );
conn->request_info.local_uri = NULL;
was_error = true;
break;
}
@@ -119,7 +123,7 @@ void XX_httplib_process_new_connection( struct httplib_connection *conn ) {
conn->request_info.uri = conn->request_info.local_uri;
}
if ( ebuf[0] == '\0' ) {
if ( ! was_error ) {
if ( conn->request_info.local_uri != NULL ) {