@@ -20,6 +20,7 @@ module http_client
2020 CURLOPT_POSTFIELDS, CURLOPT_POSTFIELDSIZE_LARGE, curl_easy_escape, &
2121 curl_mime_init, curl_mime_addpart, curl_mime_filedata,curl_mime_name, &
2222 CURLOPT_MIMEPOST,curl_mime_data, CURL_ZERO_TERMINATED, &
23+ CURLOPT_TIMEOUT, CURLOPT_CONNECTTIMEOUT, &
2324 CURLOPT_HTTPAUTH, CURLAUTH_BASIC, CURLOPT_USERNAME, CURLOPT_PASSWORD
2425 use stdlib_optval, only: optval
2526 use http_request, only: request_type
@@ -54,7 +55,7 @@ module http_client
5455 ! new client_type object using the request object as a parameter and sends the request to the server
5556 ! using the client_get_response method. The function returns the response_type object containing the
5657 ! 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)
5859 ! ! This function creates a new HTTP request object of the request_type type and sends
5960 ! ! the request to the server using the client_type object. The function takes the URL,
6061 ! ! 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
7273 ! ! An optional array of pair_type objects that specifies the form data to send in the request body.
7374 type (pair_type), intent (in ), optional :: file
7475 ! ! 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
7578 type (pair_type), intent (in ), optional :: auth
7679 ! ! An optional pair_type object that stores the username and password for Authentication
7780 type (response_type) :: response
@@ -112,6 +115,9 @@ function new_request(url, method, header, data, form, file, auth) result(respons
112115 request% file = file
113116 end if
114117
118+ ! Set request timeout.
119+ request% timeout = optval(timeout, - 1 )
120+
115121 ! setting username and password for Authentication
116122 if (present (auth)) then
117123 request% auth = auth
@@ -168,6 +174,9 @@ & function failed. This can occur due to insufficient memory available in the sy
168174 ! setting request method
169175 rc = set_method(curl_ptr, this% request% method, response)
170176
177+ ! setting request timeout
178+ rc = set_timeout(curl_ptr, this% request% timeout)
179+
171180 ! setting request body
172181 rc = set_body(curl_ptr, this% request)
173182
@@ -300,6 +309,25 @@ function set_method(curl_ptr, method, response) result(status)
300309 end select
301310 end function set_method
302311
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+
303331 ! The set_body function determines the type of data to include in the request body
304332 ! based on the inputs provided. If data is provided, it is sent as the body of the
305333 ! request. If form is provided without a file, the form data is URL encoded and sent
0 commit comments