curl/lib/dynhds.h
Stefan Eissing 61f52a97e9
lib: add bufq and dynhds
Adding `bufq`:
- at init() time configured to hold up to `n` chunks of `m` bytes each.
- various methods for reading from and writing to it.
- `peek` support to get access to buffered data without copy
- `pass` support to allow buffer flushing on write if it becomes full
- use case: IO buffers for dynamic reads and writes that do not blow up
- distinct from `dynbuf` in that:
  - it maintains a read position
  - writes on a full bufq return CURLE_AGAIN instead of nuking itself
- Init options:
  - SOFT_LIMIT: allow writes into a full bufq
  - NO_SPARES: free empty chunks right away
- a `bufc_pool` that can keep a number of spare chunks to
  be shared between different `bufq` instances

Adding `dynhds`:
- a straightforward list of name+value pairs as used for HTTP headers
- headers can be appended dynamically
- headers can be removed again
- headers can be replaced
- headers can be looked up
- http/1.1 formatting into a `dynbuf`
- configured at init() with limits on header counts and total string
  sizes
- use case: pass a HTTP request or response around without being version
  specific
- express a HTTP request without a curl easy handle (used in h2 proxy
  tunnels)
- future extension possibilities:
  - conversions of `dynhds` to nghttp2/nghttp3 name+value arrays

Closes #10720
2023-03-30 09:08:05 +02:00

156 lines
5.0 KiB
C

#ifndef HEADER_CURL_DYNHDS_H
#define HEADER_CURL_DYNHDS_H
/***************************************************************************
* _ _ ____ _
* Project ___| | | | _ \| |
* / __| | | | |_) | |
* | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____|
*
* Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
* are also available at https://curl.se/docs/copyright.html.
*
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
* copies of the Software, and permit persons to whom the Software is
* furnished to do so, under the terms of the COPYING file.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
* SPDX-License-Identifier: curl
*
***************************************************************************/
#include "curl_setup.h"
#include <curl/curl.h>
#include "dynbuf.h"
struct dynbuf;
/**
* A single header entry.
* `name` and `value` are non-NULL and always NUL terminated.
*/
struct dynhds_entry {
char *name;
char *value;
size_t namelen;
size_t valuelen;
};
struct dynhds {
struct dynhds_entry **hds;
size_t hds_len; /* number of entries in hds */
size_t hds_allc; /* size of hds allocation */
size_t max_entries; /* size limit number of entries */
size_t strs_len; /* length of all strings */
size_t max_strs_size; /* max length of all strings */
};
/**
* Init for use on first time or after a reset.
* Allow `max_entries` headers to be added, 0 for unlimited.
* Allow size of all name and values added to not exceed `max_strs_size``
*/
void Curl_dynhds_init(struct dynhds *dynhds, size_t max_entries,
size_t max_strs_size);
/**
* Frees all data held in `dynhds`, but not the struct itself.
*/
void Curl_dynhds_free(struct dynhds *dynhds);
/**
* Reset `dyndns` to the initial init state. May keep allocations
* around.
*/
void Curl_dynhds_reset(struct dynhds *dynhds);
/**
* Return the number of header entries.
*/
size_t Curl_dynhds_count(struct dynhds *dynhds);
/**
* Return the n-th header entry or NULL if it does not exist.
*/
struct dynhds_entry *Curl_dynhds_getn(struct dynhds *dynhds, size_t n);
/**
* Return the 1st header entry of the name or NULL if none exists.
*/
struct dynhds_entry *Curl_dynhds_get(struct dynhds *dynhds,
const char *name, size_t namelen);
struct dynhds_entry *Curl_dynhds_cget(struct dynhds *dynhds, const char *name);
/**
* Return TRUE iff one or more headers with the given name exist.
*/
bool Curl_dynhds_contains(struct dynhds *dynhds,
const char *name, size_t namelen);
bool Curl_dynhds_ccontains(struct dynhds *dynhds, const char *name);
/**
* Return how often the given name appears in `dynhds`.
* Names are case-insensitive.
*/
size_t Curl_dynhds_count_name(struct dynhds *dynhds,
const char *name, size_t namelen);
/**
* Return how often the given 0-terminated name appears in `dynhds`.
* Names are case-insensitive.
*/
size_t Curl_dynhds_ccount_name(struct dynhds *dynhds, const char *name);
/**
* Add a header, name + value, to `dynhds` at the end. Does *not*
* check for duplicate names.
*/
CURLcode Curl_dynhds_add(struct dynhds *dynhds,
const char *name, size_t namelen,
const char *value, size_t valuelen);
/**
* Add a header, c-string name + value, to `dynhds` at the end.
*/
CURLcode Curl_dynhds_cadd(struct dynhds *dynhds,
const char *name, const char *value);
/**
* Remove all entries with the given name.
* Returns number of entries removed.
*/
size_t Curl_dynhds_remove(struct dynhds *dynhds,
const char *name, size_t namelen);
size_t Curl_dynhds_cremove(struct dynhds *dynhds, const char *name);
/**
* Set the give header name and value, replacing any entries with
* the same name. The header is added at the end of all (remaining)
* entries.
*/
CURLcode Curl_dynhds_set(struct dynhds *dynhds,
const char *name, size_t namelen,
const char *value, size_t valuelen);
CURLcode Curl_dynhds_cset(struct dynhds *dynhds,
const char *name, const char *value);
/**
* Add a single header from a HTTP/1.1 formatted line at the end. Line
* may contain a delimiting \r\n or just \n. And characters after
* that will be ignored.
*/
CURLcode Curl_dynhds_h1_cadd_line(struct dynhds *dynhds, const char *line);
/**
* Add the headers to the given `dynbuf` in HTTP/1.1 format with
* cr+lf line endings. Will NOT output a last empty line.
*/
CURLcode Curl_dynhds_h1_dprint(struct dynhds *dynhds, struct dynbuf *dbuf);
#endif /* HEADER_CURL_DYNHDS_H */