CURLOPT_HSTSWRITEFUNCTION.md (2605B)
1 --- 2 c: Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al. 3 SPDX-License-Identifier: curl 4 Title: CURLOPT_HSTSWRITEFUNCTION 5 Section: 3 6 Source: libcurl 7 Protocol: 8 - HTTP 9 See-also: 10 - CURLOPT_HSTS (3) 11 - CURLOPT_HSTSWRITEDATA (3) 12 - CURLOPT_HSTSWRITEFUNCTION (3) 13 - CURLOPT_HSTS_CTRL (3) 14 Added-in: 7.74.0 15 --- 16 17 # NAME 18 19 CURLOPT_HSTSWRITEFUNCTION - write callback for HSTS hosts 20 21 # SYNOPSIS 22 23 ~~~c 24 #include <curl/curl.h> 25 26 struct curl_hstsentry { 27 char *name; 28 size_t namelen; 29 unsigned int includeSubDomains:1; 30 char expire[18]; /* YYYYMMDD HH:MM:SS [null-terminated] */ 31 }; 32 33 struct curl_index { 34 size_t index; /* the provided entry's "index" or count */ 35 size_t total; /* total number of entries to save */ 36 }; 37 38 CURLSTScode hstswrite(CURL *easy, struct curl_hstsentry *sts, 39 struct curl_index *count, void *clientp); 40 41 CURLcode curl_easy_setopt(CURL *handle, CURLOPT_HSTSWRITEFUNCTION, hstswrite); 42 ~~~ 43 44 # DESCRIPTION 45 46 Pass a pointer to your callback function, as the prototype shows above. 47 48 This callback function gets called by libcurl repeatedly to allow the 49 application to store the in-memory HSTS cache when libcurl is about to discard 50 it. 51 52 Set the *clientp* argument with the CURLOPT_HSTSWRITEDATA(3) option 53 or it is NULL. 54 When the callback is invoked, the *sts* pointer points to a populated 55 struct: Read the hostname to 'name' (it is *namelen* bytes long and null 56 terminated. The *includeSubDomains* field is non-zero if the entry matches 57 subdomains. The *expire* string is a date stamp null-terminated string 58 using the syntax YYYYMMDD HH:MM:SS. 59 60 The callback should return *CURLSTS_OK* if it succeeded and is prepared to 61 be called again (for another host) or *CURLSTS_DONE* if there is nothing 62 more to do. It can also return *CURLSTS_FAIL* to signal error. 63 64 This option does not enable HSTS, you need to use CURLOPT_HSTS_CTRL(3) to 65 do that. 66 67 # DEFAULT 68 69 NULL - no callback. 70 71 # %PROTOCOLS% 72 73 # EXAMPLE 74 75 ~~~c 76 struct priv { 77 void *custom; 78 }; 79 80 static CURLSTScode hswr_cb(CURL *easy, struct curl_hstsentry *sts, 81 struct curl_index *count, void *clientp) 82 { 83 /* save the passed in HSTS data somewhere */ 84 return CURLSTS_OK; 85 } 86 87 int main(void) 88 { 89 CURL *curl = curl_easy_init(); 90 if(curl) { 91 struct priv my_stuff; 92 CURLcode res; 93 94 /* set HSTS read callback */ 95 curl_easy_setopt(curl, CURLOPT_HSTSWRITEFUNCTION, hswr_cb); 96 97 /* pass in suitable argument to the callback */ 98 curl_easy_setopt(curl, CURLOPT_HSTSWRITEDATA, &my_stuff); 99 100 res = curl_easy_perform(curl); 101 } 102 } 103 ~~~ 104 105 # %AVAILABILITY% 106 107 # RETURN VALUE 108 109 This returns CURLE_OK.