xref: /curl/lib/dynhds.h (revision c074ba64)
1 #ifndef HEADER_CURL_DYNHDS_H
2 #define HEADER_CURL_DYNHDS_H
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 #ifdef UNITTESTS
99 /* used by unit2602.c */
100 
101 /**
102  * Return TRUE iff one or more headers with the given name exist.
103  */
104 bool Curl_dynhds_contains(struct dynhds *dynhds,
105                           const char *name, size_t namelen);
106 bool Curl_dynhds_ccontains(struct dynhds *dynhds, const char *name);
107 
108 /**
109  * Return how often the given name appears in `dynhds`.
110  * Names are case-insensitive.
111  */
112 size_t Curl_dynhds_count_name(struct dynhds *dynhds,
113                               const char *name, size_t namelen);
114 
115 /**
116  * Return how often the given 0-terminated name appears in `dynhds`.
117  * Names are case-insensitive.
118  */
119 size_t Curl_dynhds_ccount_name(struct dynhds *dynhds, const char *name);
120 
121 /**
122  * Remove all entries with the given name.
123  * Returns number of entries removed.
124  */
125 size_t Curl_dynhds_remove(struct dynhds *dynhds,
126                           const char *name, size_t namelen);
127 size_t Curl_dynhds_cremove(struct dynhds *dynhds, const char *name);
128 
129 
130 /**
131  * Set the give header name and value, replacing any entries with
132  * the same name. The header is added at the end of all (remaining)
133  * entries.
134  */
135 CURLcode Curl_dynhds_set(struct dynhds *dynhds,
136                          const char *name, size_t namelen,
137                          const char *value, size_t valuelen);
138 #endif
139 
140 CURLcode Curl_dynhds_cset(struct dynhds *dynhds,
141                           const char *name, const char *value);
142 
143 /**
144  * Add a header, name + value, to `dynhds` at the end. Does *not*
145  * check for duplicate names.
146  */
147 CURLcode Curl_dynhds_add(struct dynhds *dynhds,
148                          const char *name, size_t namelen,
149                          const char *value, size_t valuelen);
150 
151 /**
152  * Add a header, c-string name + value, to `dynhds` at the end.
153  */
154 CURLcode Curl_dynhds_cadd(struct dynhds *dynhds,
155                           const char *name, const char *value);
156 
157 /**
158  * Add a single header from an HTTP/1.1 formatted line at the end. Line
159  * may contain a delimiting \r\n or just \n. Any characters after
160  * that will be ignored.
161  */
162 CURLcode Curl_dynhds_h1_cadd_line(struct dynhds *dynhds, const char *line);
163 
164 /**
165  * Add a single header from an HTTP/1.1 formatted line at the end. Line
166  * may contain a delimiting \r\n or just \n. Any characters after
167  * that will be ignored.
168  */
169 CURLcode Curl_dynhds_h1_add_line(struct dynhds *dynhds,
170                                  const char *line, size_t line_len);
171 
172 /**
173  * Add the headers to the given `dynbuf` in HTTP/1.1 format with
174  * cr+lf line endings. Will NOT output a last empty line.
175  */
176 CURLcode Curl_dynhds_h1_dprint(struct dynhds *dynhds, struct dynbuf *dbuf);
177 
178 #ifdef USE_NGHTTP2
179 
180 #include <stdint.h>
181 #include <nghttp2/nghttp2.h>
182 
183 nghttp2_nv *Curl_dynhds_to_nva(struct dynhds *dynhds, size_t *pcount);
184 
185 #endif /* USE_NGHTTP2 */
186 
187 #endif /* HEADER_CURL_DYNHDS_H */
188