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