@@ -20,6 +20,7 @@ module http_client
20
20
CURLOPT_POSTFIELDS, CURLOPT_POSTFIELDSIZE_LARGE, curl_easy_escape, &
21
21
curl_mime_init, curl_mime_addpart, curl_mime_filedata,curl_mime_name, &
22
22
CURLOPT_MIMEPOST,curl_mime_data, CURL_ZERO_TERMINATED, &
23
+ CURLOPT_TIMEOUT, CURLOPT_CONNECTTIMEOUT, &
23
24
CURLOPT_HTTPAUTH, CURLAUTH_BASIC, CURLOPT_USERNAME, CURLOPT_PASSWORD
24
25
use stdlib_optval, only: optval
25
26
use http_request, only: request_type
@@ -54,7 +55,7 @@ module http_client
54
55
! new client_type object using the request object as a parameter and sends the request to the server
55
56
! using the client_get_response method. The function returns the response_type object containing the
56
57
! server's response.
57
- function new_request (url , method , header , data , form , file , auth ) result(response)
58
+ function new_request (url , method , header , data , form , file , timeout , auth ) result(response)
58
59
! ! This function creates a new HTTP request object of the request_type type and sends
59
60
! ! the request to the server using the client_type object. The function takes the URL,
60
61
! ! HTTP method, request headers, request data, and form data as input arguments and returns
@@ -72,6 +73,8 @@ function new_request(url, method, header, data, form, file, auth) result(respons
72
73
! ! An optional array of pair_type objects that specifies the form data to send in the request body.
73
74
type (pair_type), intent (in ), optional :: file
74
75
! ! An optional pair_type object that specifies the file data to send in the request body.
76
+ integer , intent (in ), optional :: timeout
77
+ ! ! Timeout value for the request in seconds
75
78
type (pair_type), intent (in ), optional :: auth
76
79
! ! An optional pair_type object that stores the username and password for Authentication
77
80
type (response_type) :: response
@@ -112,6 +115,9 @@ function new_request(url, method, header, data, form, file, auth) result(respons
112
115
request% file = file
113
116
end if
114
117
118
+ ! Set request timeout.
119
+ request% timeout = optval(timeout, - 1 )
120
+
115
121
! setting username and password for Authentication
116
122
if (present (auth)) then
117
123
request% auth = auth
@@ -168,6 +174,9 @@ & function failed. This can occur due to insufficient memory available in the sy
168
174
! setting request method
169
175
rc = set_method(curl_ptr, this% request% method, response)
170
176
177
+ ! setting request timeout
178
+ rc = set_timeout(curl_ptr, this% request% timeout)
179
+
171
180
! setting request body
172
181
rc = set_body(curl_ptr, this% request)
173
182
@@ -300,6 +309,25 @@ function set_method(curl_ptr, method, response) result(status)
300
309
end select
301
310
end function set_method
302
311
312
+ function set_timeout (curl_ptr , timeout ) result(status)
313
+ ! ! This function sets the timeout value (in seconds). If the timeout value
314
+ ! ! is less than zero, it is ignored and a success status is returned.
315
+ type (c_ptr), intent (out ) :: curl_ptr
316
+ ! ! Pointer to the curl handle.
317
+ integer (kind= int64), intent (in ) :: timeout
318
+ ! ! Timeout seconds for request.
319
+ integer :: status
320
+ ! ! Status code indicating whether the operation was successful.
321
+ if (timeout < 0 ) then
322
+ status = 0
323
+ else
324
+ ! setting the maximum time allowed for the connection to established.(in seconds)
325
+ status = curl_easy_setopt(curl_ptr, CURLOPT_CONNECTTIMEOUT, timeout)
326
+ ! setting maximum time allowed for transfer operation.(in seconds)
327
+ status = curl_easy_setopt(curl_ptr, CURLOPT_TIMEOUT, timeout)
328
+ end if
329
+ end function set_timeout
330
+
303
331
! The set_body function determines the type of data to include in the request body
304
332
! based on the inputs provided. If data is provided, it is sent as the body of the
305
333
! request. If form is provided without a file, the form data is URL encoded and sent
0 commit comments