Skip to content

Commit

Permalink
Add struct _u_request.timeout to set a timeout on sending http request
Browse files Browse the repository at this point in the history
  • Loading branch information
babelouest committed Apr 10, 2017
1 parent d08f7c2 commit 229a310
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 0 deletions.
2 changes: 2 additions & 0 deletions API.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
Expand Down
2 changes: 2 additions & 0 deletions src/u_request.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
11 changes: 11 additions & 0 deletions src/u_send_request.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 2 additions & 0 deletions src/ulfius.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
Expand Down

0 comments on commit 229a310

Please sign in to comment.