1 | #ifndef HEADER_CURL_DYNHDS_H |
2 | #define |
3 | /*************************************************************************** |
4 | * _ _ ____ _ |
5 | * Project ___| | | | _ \| | |
6 | * / __| | | | |_) | | |
7 | * | (__| |_| | _ <| |___ |
8 | * \___|\___/|_| \_\_____| |
9 | * |
10 | * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al. |
11 | * |
12 | * This software is licensed as described in the file COPYING, which |
13 | * you should have received as part of this distribution. The terms |
14 | * are also available at https://curl.se/docs/copyright.html. |
15 | * |
16 | * You may opt to use, copy, modify, merge, publish, distribute and/or sell |
17 | * copies of the Software, and permit persons to whom the Software is |
18 | * furnished to do so, under the terms of the COPYING file. |
19 | * |
20 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY |
21 | * KIND, either express or implied. |
22 | * |
23 | * SPDX-License-Identifier: curl |
24 | * |
25 | ***************************************************************************/ |
26 | #include "curl_setup.h" |
27 | |
28 | #include <curl/curl.h> |
29 | #include "dynbuf.h" |
30 | |
31 | struct dynbuf; |
32 | |
33 | /** |
34 | * A single header entry. |
35 | * `name` and `value` are non-NULL and always NUL terminated. |
36 | */ |
37 | struct dynhds_entry { |
38 | char *name; |
39 | char *value; |
40 | size_t namelen; |
41 | size_t valuelen; |
42 | }; |
43 | |
44 | struct dynhds { |
45 | struct dynhds_entry **hds; |
46 | size_t hds_len; /* number of entries in hds */ |
47 | size_t hds_allc; /* size of hds allocation */ |
48 | size_t max_entries; /* size limit number of entries */ |
49 | size_t strs_len; /* length of all strings */ |
50 | size_t max_strs_size; /* max length of all strings */ |
51 | int opts; |
52 | }; |
53 | |
54 | #define DYNHDS_OPT_NONE (0) |
55 | #define DYNHDS_OPT_LOWERCASE (1 << 0) |
56 | |
57 | /** |
58 | * Init for use on first time or after a reset. |
59 | * Allow `max_entries` headers to be added, 0 for unlimited. |
60 | * Allow size of all name and values added to not exceed `max_strs_size`` |
61 | */ |
62 | void Curl_dynhds_init(struct dynhds *dynhds, size_t max_entries, |
63 | size_t max_strs_size); |
64 | /** |
65 | * Frees all data held in `dynhds`, but not the struct itself. |
66 | */ |
67 | void Curl_dynhds_free(struct dynhds *dynhds); |
68 | |
69 | /** |
70 | * Reset `dyndns` to the initial init state. May keep allocations |
71 | * around. |
72 | */ |
73 | void Curl_dynhds_reset(struct dynhds *dynhds); |
74 | |
75 | /** |
76 | * Return the number of header entries. |
77 | */ |
78 | size_t Curl_dynhds_count(struct dynhds *dynhds); |
79 | |
80 | /** |
81 | * Set the options to use, replacing any existing ones. |
82 | * This will not have an effect on already existing headers. |
83 | */ |
84 | void Curl_dynhds_set_opts(struct dynhds *dynhds, int opts); |
85 | |
86 | /** |
87 | * Return the n-th header entry or NULL if it does not exist. |
88 | */ |
89 | struct dynhds_entry *Curl_dynhds_getn(struct dynhds *dynhds, size_t n); |
90 | |
91 | /** |
92 | * Return the 1st header entry of the name or NULL if none exists. |
93 | */ |
94 | struct dynhds_entry *Curl_dynhds_get(struct dynhds *dynhds, |
95 | const char *name, size_t namelen); |
96 | struct dynhds_entry *Curl_dynhds_cget(struct dynhds *dynhds, const char *name); |
97 | |
98 | /** |
99 | * Return TRUE iff one or more headers with the given name exist. |
100 | */ |
101 | bool Curl_dynhds_contains(struct dynhds *dynhds, |
102 | const char *name, size_t namelen); |
103 | bool Curl_dynhds_ccontains(struct dynhds *dynhds, const char *name); |
104 | |
105 | /** |
106 | * Return how often the given name appears in `dynhds`. |
107 | * Names are case-insensitive. |
108 | */ |
109 | size_t Curl_dynhds_count_name(struct dynhds *dynhds, |
110 | const char *name, size_t namelen); |
111 | |
112 | /** |
113 | * Return how often the given 0-terminated name appears in `dynhds`. |
114 | * Names are case-insensitive. |
115 | */ |
116 | size_t Curl_dynhds_ccount_name(struct dynhds *dynhds, const char *name); |
117 | |
118 | /** |
119 | * Add a header, name + value, to `dynhds` at the end. Does *not* |
120 | * check for duplicate names. |
121 | */ |
122 | CURLcode Curl_dynhds_add(struct dynhds *dynhds, |
123 | const char *name, size_t namelen, |
124 | const char *value, size_t valuelen); |
125 | |
126 | /** |
127 | * Add a header, c-string name + value, to `dynhds` at the end. |
128 | */ |
129 | CURLcode Curl_dynhds_cadd(struct dynhds *dynhds, |
130 | const char *name, const char *value); |
131 | |
132 | /** |
133 | * Remove all entries with the given name. |
134 | * Returns number of entries removed. |
135 | */ |
136 | size_t Curl_dynhds_remove(struct dynhds *dynhds, |
137 | const char *name, size_t namelen); |
138 | size_t Curl_dynhds_cremove(struct dynhds *dynhds, const char *name); |
139 | |
140 | |
141 | /** |
142 | * Set the give header name and value, replacing any entries with |
143 | * the same name. The header is added at the end of all (remaining) |
144 | * entries. |
145 | */ |
146 | CURLcode Curl_dynhds_set(struct dynhds *dynhds, |
147 | const char *name, size_t namelen, |
148 | const char *value, size_t valuelen); |
149 | |
150 | CURLcode Curl_dynhds_cset(struct dynhds *dynhds, |
151 | const char *name, const char *value); |
152 | |
153 | /** |
154 | * Add a single header from a HTTP/1.1 formatted line at the end. Line |
155 | * may contain a delimiting \r\n or just \n. Any characters after |
156 | * that will be ignored. |
157 | */ |
158 | CURLcode Curl_dynhds_h1_cadd_line(struct dynhds *dynhds, const char *line); |
159 | |
160 | /** |
161 | * Add a single header from a HTTP/1.1 formatted line at the end. Line |
162 | * may contain a delimiting \r\n or just \n. Any characters after |
163 | * that will be ignored. |
164 | */ |
165 | CURLcode Curl_dynhds_h1_add_line(struct dynhds *dynhds, |
166 | const char *line, size_t line_len); |
167 | |
168 | /** |
169 | * Add the headers to the given `dynbuf` in HTTP/1.1 format with |
170 | * cr+lf line endings. Will NOT output a last empty line. |
171 | */ |
172 | CURLcode Curl_dynhds_h1_dprint(struct dynhds *dynhds, struct dynbuf *dbuf); |
173 | |
174 | #endif /* HEADER_CURL_DYNHDS_H */ |
175 | |