diff --git a/API.md b/API.md index bf190b2a..5ca8159f 100644 --- a/API.md +++ b/API.md @@ -352,6 +352,7 @@ The request variable is defined as: * http_verb: http method (GET, POST, PUT, DELETE, etc.), use '*' to match all http methods * http_url: url used to call this callback function or full url to call when used in a ulfius_send_http_request * check_server_certificate: do not check server certificate and hostname if false (default true), used by ulfius_send_http_request + * timeout connection timeout used by ulfius_send_http_request, default is 0 * client_address: IP address of the client * auth_basic_user: basic authtication username * auth_basic_password: basic authtication password @@ -370,6 +371,7 @@ struct _u_request { char * http_verb; char * http_url; int check_server_certificate; + long timeout; struct sockaddr * client_address; char * auth_basic_user; char * auth_basic_password; diff --git a/src/u_request.c b/src/u_request.c index 2f90106f..97cea46e 100644 --- a/src/u_request.c +++ b/src/u_request.c @@ -228,6 +228,7 @@ int ulfius_init_request(struct _u_request * request) { u_map_init(request->map_post_body); request->http_verb = NULL; request->http_url = NULL; + request->timeout = 0L; request->check_server_certificate = 1; request->client_address = NULL; request->json_body = NULL; @@ -319,6 +320,7 @@ struct _u_request * ulfius_duplicate_request(const struct _u_request * request) memcpy(new_request->client_address, request->client_address, sizeof(struct sockaddr)); } new_request->check_server_certificate = request->check_server_certificate; + new_request->timeout = request->timeout; new_request->auth_basic_user = nstrdup(request->auth_basic_user); new_request->auth_basic_password = nstrdup(request->auth_basic_password); u_map_clean_full(new_request->map_url); diff --git a/src/u_send_request.c b/src/u_send_request.c index 6d961ee1..080641d0 100644 --- a/src/u_send_request.c +++ b/src/u_send_request.c @@ -487,6 +487,17 @@ int ulfius_send_http_streaming_request(const struct _u_request * request, struct } } + // Set request timeout value + if (copy_request->timeout) { + if (curl_easy_setopt(curl_handle, CURLOPT_TIMEOUT, copy_request->timeout) != CURLE_OK) { + ulfius_clean_request_full(copy_request); + curl_slist_free_all(header_list); + curl_easy_cleanup(curl_handle); + y_log_message(Y_LOG_LEVEL_ERROR, "Ulfius - Error setting libcurl options (5)"); + return U_ERROR_LIBCURL; + } + } + // Response parameters if (response != NULL) { if (response->map_header != NULL) { diff --git a/src/ulfius.h b/src/ulfius.h index c27cd71d..f968fe49 100644 --- a/src/ulfius.h +++ b/src/ulfius.h @@ -106,6 +106,7 @@ struct _u_cookie { * http_verb: http method (GET, POST, PUT, DELETE, etc.), use '*' to match all http methods * http_url: url used to call this callback function or full url to call when used in a ulfius_send_http_request * check_server_certificate: do not check server certificate and hostname if false (default true), used by ulfius_send_http_request + * timeout connection timeout used by ulfius_send_http_request, default is 0 * client_address: IP address of the client * auth_basic_user: basic authtication username * auth_basic_password: basic authtication password @@ -124,6 +125,7 @@ struct _u_request { char * http_verb; char * http_url; int check_server_certificate; + long timeout; struct sockaddr * client_address; char * auth_basic_user; char * auth_basic_password;