CURLOPT_TRAILERFUNCTION.md (3040B)
1 --- 2 c: Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al. 3 SPDX-License-Identifier: curl 4 Title: CURLOPT_TRAILERFUNCTION 5 Section: 3 6 Source: libcurl 7 See-also: 8 - CURLOPT_TRAILERDATA (3) 9 - CURLOPT_WRITEFUNCTION (3) 10 Protocol: 11 - HTTP 12 Added-in: 7.64.0 13 --- 14 15 # NAME 16 17 CURLOPT_TRAILERFUNCTION - callback for sending trailing headers 18 19 # SYNOPSIS 20 21 ~~~c 22 #include <curl.h> 23 24 int curl_trailer_callback(struct curl_slist ** list, void *userdata); 25 26 CURLcode curl_easy_setopt(CURL *handle, CURLOPT_TRAILERFUNCTION, 27 curl_trailer_callback *func); 28 ~~~ 29 30 # DESCRIPTION 31 32 Pass a pointer to a callback function. 33 34 This callback function is called once right before sending the final CR LF in 35 an HTTP chunked transfer to fill a list of trailing headers to be sent before 36 finishing the HTTP transfer. 37 38 You can set the userdata argument with the CURLOPT_TRAILERDATA(3) 39 option. 40 41 The trailing headers included in the linked list must not be CRLF-terminated, 42 because libcurl adds the appropriate line termination characters after each 43 header item. 44 45 If you use curl_slist_append(3) to add trailing headers to the *curl_slist* 46 then libcurl duplicates the strings, and frees the *curl_slist* once the 47 trailers have been sent. 48 49 If one of the trailing header fields is not formatted correctly it is ignored 50 and an info message is emitted. 51 52 The return value can either be **CURL_TRAILERFUNC_OK** or 53 **CURL_TRAILERFUNC_ABORT** which would respectively instruct libcurl to 54 either continue with sending the trailers or to abort the request. 55 56 If you set this option to NULL, then the transfer proceeds as usual 57 without any interruptions. 58 59 # DEFAULT 60 61 NULL 62 63 # %PROTOCOLS% 64 65 # EXAMPLE 66 ~~~c 67 extern size_t read_cb(char *ptr, size_t size, 68 size_t nmemb, void *userdata); 69 70 static int trailer_cb(struct curl_slist **tr, void *data) 71 { 72 /* libcurl frees the list */ 73 *tr = curl_slist_append(*tr, "My-super-awesome-trailer: trailer-stuff"); 74 return CURL_TRAILERFUNC_OK; 75 } 76 77 int main(void) 78 { 79 CURL *curl = curl_easy_init(); 80 if(curl) { 81 CURLcode res; 82 83 /* Set the URL of the request */ 84 curl_easy_setopt(curl, CURLOPT_URL, "https://example.com/"); 85 /* Now set it as a put */ 86 curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L); 87 88 /* Assuming we have a function that returns the data to be pushed 89 Let that function be read_cb */ 90 curl_easy_setopt(curl, CURLOPT_READFUNCTION, read_cb); 91 92 struct curl_slist *headers = NULL; 93 headers = curl_slist_append(headers, "Trailer: My-super-awesome-trailer"); 94 res = curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers); 95 96 /* Set the trailers filling callback */ 97 curl_easy_setopt(curl, CURLOPT_TRAILERFUNCTION, trailer_cb); 98 99 /* Perform the transfer */ 100 res = curl_easy_perform(curl); 101 102 curl_easy_cleanup(curl); 103 104 curl_slist_free_all(headers); 105 } 106 } 107 ~~~ 108 109 # %AVAILABILITY% 110 111 # RETURN VALUE 112 113 curl_easy_setopt(3) returns a CURLcode indicating success or error. 114 115 CURLE_OK (0) means everything was OK, non-zero means an error occurred, see 116 libcurl-errors(3).