picohttpparser.h revision 1.1 1 1.1 christos /* $NetBSD: picohttpparser.h,v 1.1 2024/02/21 21:54:48 christos Exp $ */
2 1.1 christos
3 1.1 christos /*
4 1.1 christos * Copyright (c) 2009-2014 Kazuho Oku, Tokuhiro Matsuno, Daisuke Murase,
5 1.1 christos * Shigeo Mitsunari
6 1.1 christos *
7 1.1 christos * SPDX-License-Identifier: MIT
8 1.1 christos *
9 1.1 christos * The software is licensed under either the MIT License (below) or the Perl
10 1.1 christos * license.
11 1.1 christos *
12 1.1 christos * Permission is hereby granted, free of charge, to any person obtaining a copy
13 1.1 christos * of this software and associated documentation files (the "Software"), to
14 1.1 christos * deal in the Software without restriction, including without limitation the
15 1.1 christos * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
16 1.1 christos * sell copies of the Software, and to permit persons to whom the Software is
17 1.1 christos * furnished to do so, subject to the following conditions:
18 1.1 christos *
19 1.1 christos * The above copyright notice and this permission notice shall be included in
20 1.1 christos * all copies or substantial portions of the Software.
21 1.1 christos *
22 1.1 christos * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 1.1 christos * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 1.1 christos * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25 1.1 christos * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 1.1 christos * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
27 1.1 christos * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
28 1.1 christos * IN THE SOFTWARE.
29 1.1 christos */
30 1.1 christos
31 1.1 christos #ifndef picohttpparser_h
32 1.1 christos #define picohttpparser_h
33 1.1 christos
34 1.1 christos #include <sys/types.h>
35 1.1 christos
36 1.1 christos #ifdef _MSC_VER
37 1.1 christos #define ssize_t intptr_t
38 1.1 christos #endif
39 1.1 christos
40 1.1 christos #ifdef __cplusplus
41 1.1 christos extern "C" {
42 1.1 christos #endif
43 1.1 christos
44 1.1 christos /* contains name and value of a header (name == NULL if is a continuing line
45 1.1 christos * of a multiline header */
46 1.1 christos struct phr_header {
47 1.1 christos const char *name;
48 1.1 christos size_t name_len;
49 1.1 christos const char *value;
50 1.1 christos size_t value_len;
51 1.1 christos };
52 1.1 christos
53 1.1 christos /* returns number of bytes consumed if successful, -2 if request is partial,
54 1.1 christos * -1 if failed */
55 1.1 christos int
56 1.1 christos phr_parse_request(const char *buf, size_t len, const char **method,
57 1.1 christos size_t *method_len, const char **path, size_t *path_len,
58 1.1 christos int *minor_version, struct phr_header *headers,
59 1.1 christos size_t *num_headers, size_t last_len);
60 1.1 christos
61 1.1 christos /* ditto */
62 1.1 christos int
63 1.1 christos phr_parse_response(const char *_buf, size_t len, int *minor_version,
64 1.1 christos int *status, const char **msg, size_t *msg_len,
65 1.1 christos struct phr_header *headers, size_t *num_headers,
66 1.1 christos size_t last_len);
67 1.1 christos
68 1.1 christos /* ditto */
69 1.1 christos int
70 1.1 christos phr_parse_headers(const char *buf, size_t len, struct phr_header *headers,
71 1.1 christos size_t *num_headers, size_t last_len);
72 1.1 christos
73 1.1 christos /* should be zero-filled before start */
74 1.1 christos struct phr_chunked_decoder {
75 1.1 christos size_t bytes_left_in_chunk; /* number of bytes left in current chunk */
76 1.1 christos char consume_trailer; /* if trailing headers should be consumed */
77 1.1 christos char _hex_count;
78 1.1 christos char _state;
79 1.1 christos };
80 1.1 christos
81 1.1 christos /* the function rewrites the buffer given as (buf, bufsz) removing the chunked-
82 1.1 christos * encoding headers. When the function returns without an error, bufsz is
83 1.1 christos * updated to the length of the decoded data available. Applications should
84 1.1 christos * repeatedly call the function while it returns -2 (incomplete) every time
85 1.1 christos * supplying newly arrived data. If the end of the chunked-encoded data is
86 1.1 christos * found, the function returns a non-negative number indicating the number of
87 1.1 christos * octets left undecoded, that starts from the offset returned by `*bufsz`.
88 1.1 christos * Returns -1 on error.
89 1.1 christos */
90 1.1 christos ssize_t
91 1.1 christos phr_decode_chunked(struct phr_chunked_decoder *decoder, char *buf,
92 1.1 christos size_t *bufsz);
93 1.1 christos
94 1.1 christos /* returns if the chunked decoder is in middle of chunked data */
95 1.1 christos int
96 1.1 christos phr_decode_chunked_is_in_data(struct phr_chunked_decoder *decoder);
97 1.1 christos
98 1.1 christos #ifdef __cplusplus
99 1.1 christos }
100 1.1 christos #endif
101 1.1 christos
102 1.1 christos #endif
103