client_response_callback Function

private function client_response_callback(ptr, size, nmemb, client_data) bind(c)

This function is a callback function used by the fortran-curl package to handle HTTP responses. It is called for each chunk of data received from the server and appends the data to a response_type object.


It is called for each chunk of data received from the server and appends the data to a response_type object passed(i.e client_data). The function takes four input arguments: ptr, size, nmemb,
and client_data. ptr is a pointer to the received data buffer, size specifies the size of each data element, nmemb specifies the number of data elements received, and client_data is a pointer to
a response_type object. The function uses c_f_pointer to convert the C pointer to a Fortran pointer and appends the received data to the content field of the response_type object. The function returns an integer value representing the number of bytes received.

Arguments

Type IntentOptional Attributes Name
type(c_ptr), intent(in), value :: ptr

Pointer to the CURL handle.

integer(kind=c_size_t), intent(in), value :: size

Specifies the size of each data element.

integer(kind=c_size_t), intent(in), value :: nmemb

Specifies the number of data elements received.

type(c_ptr), intent(in), value :: client_data

Points to a response_type object.

Return Value integer(kind=c_size_t)

The number of bytes received.


Contents


Source Code

    function client_response_callback(ptr, size, nmemb, client_data) bind(c)
        !!> This function is a `callback` function used by the fortran-curl package to handle HTTP responses. 
        !!> It is called for each `chunk` of data received from the server and appends the data to a 
        !!> `response_type` object.
        !!>_____
        !!> It is called for each `chunk` of data received from the server and appends the data to a `response_type` 
        !!> object passed(i.e `client_data`). The function takes four input arguments: `ptr`, `size`, `nmemb`,  
        !!> and `client_data`. `ptr` is a pointer to the received data buffer, `size` specifies the size of each 
        !!> data element, `nmemb` specifies the number of data elements received, and `client_data` is a pointer to  
        !!> a `response_type` object. The function uses `c_f_pointer` to convert the C pointer to a Fortran pointer and 
        !!> appends the received data to the `content` field of the `response_type` object. The function returns an integer
        !!> value representing the **number of bytes received.**
        
        type(c_ptr), intent(in), value :: ptr 
            !! Pointer to the CURL handle.
        integer(kind=c_size_t), intent(in), value :: size 
            !! Specifies the size of each data element.
        integer(kind=c_size_t), intent(in), value :: nmemb
            !! Specifies the number of data elements received.
        type(c_ptr), intent(in), value :: client_data
            !!  Points to a response_type object.
        integer(kind=c_size_t) :: client_response_callback 
            !! The number of bytes received.
        type(response_type), pointer :: response 
        character(len=:), allocatable :: buf
      
        client_response_callback = int(0, kind=c_size_t)
      
        ! Are the passed C pointers associated?
        if (.not. c_associated(ptr)) return
        if (.not. c_associated(client_data)) return
      
        ! Convert C pointer to Fortran pointer.
        call c_f_pointer(client_data, response)
        if (.not. allocated(response%content)) response%content = ''
      
        ! Convert C pointer to Fortran allocatable character.
        call c_f_str_ptr(ptr, buf, nmemb)
        if (.not. allocated(buf)) return
        response%content = response%content // buf
        deallocate (buf)
        response%content_length = response%content_length + nmemb
        
        ! Return number of received bytes.
        client_response_callback = nmemb

    end function client_response_callback