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