diff --git a/include/libhttp.h b/include/libhttp.h index afcbed0a..89a333b9 100644 --- a/include/libhttp.h +++ b/include/libhttp.h @@ -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 ); diff --git a/src/httplib_connect_websocket_client.c b/src/httplib_connect_websocket_client.c index 46ef7c30..1832e346 100644 --- a/src/httplib_connect_websocket_client.c +++ b/src/httplib_connect_websocket_client.c @@ -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__ ); diff --git a/src/httplib_download.c b/src/httplib_download.c index 0c52ce31..1603b15b 100644 --- a/src/httplib_download.c +++ b/src/httplib_download.c @@ -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; diff --git a/src/httplib_get_response.c b/src/httplib_get_response.c index 6c524c17..5ce9646f 100644 --- a/src/httplib_get_response.c +++ b/src/httplib_get_response.c @@ -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; /* diff --git a/src/httplib_getreq.c b/src/httplib_getreq.c index 777d9e3e..aa15fca6 100644 --- a/src/httplib_getreq.c +++ b/src/httplib_getreq.c @@ -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 */ diff --git a/src/httplib_main.h b/src/httplib_main.h index 58ff40e2..582797ac 100644 --- a/src/httplib_main.h +++ b/src/httplib_main.h @@ -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 ); diff --git a/src/httplib_process_new_connection.c b/src/httplib_process_new_connection.c index 90688ae5..2c558fec 100644 --- a/src/httplib_process_new_connection.c +++ b/src/httplib_process_new_connection.c @@ -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 ) {