Home | History | Annotate | Line # | Download | only in isc
      1  1.1  christos /*	$NetBSD: picohttpparser.h,v 1.4 2025/05/21 14:48:04 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.4  christos #include <stdint.h>
     35  1.1  christos #include <sys/types.h>
     36  1.1  christos 
     37  1.1  christos #ifdef _MSC_VER
     38  1.1  christos #define ssize_t intptr_t
     39  1.1  christos #endif
     40  1.1  christos 
     41  1.1  christos #ifdef __cplusplus
     42  1.1  christos extern "C" {
     43  1.1  christos #endif
     44  1.1  christos 
     45  1.1  christos /* contains name and value of a header (name == NULL if is a continuing line
     46  1.1  christos  * of a multiline header */
     47  1.1  christos struct phr_header {
     48  1.1  christos 	const char *name;
     49  1.1  christos 	size_t name_len;
     50  1.1  christos 	const char *value;
     51  1.1  christos 	size_t value_len;
     52  1.1  christos };
     53  1.1  christos 
     54  1.1  christos /* returns number of bytes consumed if successful, -2 if request is partial,
     55  1.1  christos  * -1 if failed */
     56  1.1  christos int
     57  1.1  christos phr_parse_request(const char *buf, size_t len, const char **method,
     58  1.1  christos 		  size_t *method_len, const char **path, size_t *path_len,
     59  1.1  christos 		  int *minor_version, struct phr_header *headers,
     60  1.1  christos 		  size_t *num_headers, size_t last_len);
     61  1.1  christos 
     62  1.1  christos /* ditto */
     63  1.1  christos int
     64  1.1  christos phr_parse_response(const char *_buf, size_t len, int *minor_version,
     65  1.1  christos 		   int *status, const char **msg, size_t *msg_len,
     66  1.1  christos 		   struct phr_header *headers, size_t *num_headers,
     67  1.1  christos 		   size_t last_len);
     68  1.1  christos 
     69  1.1  christos /* ditto */
     70  1.1  christos int
     71  1.1  christos phr_parse_headers(const char *buf, size_t len, struct phr_header *headers,
     72  1.1  christos 		  size_t *num_headers, size_t last_len);
     73  1.1  christos 
     74  1.1  christos /* should be zero-filled before start */
     75  1.1  christos struct phr_chunked_decoder {
     76  1.1  christos 	size_t bytes_left_in_chunk; /* number of bytes left in current chunk */
     77  1.1  christos 	char consume_trailer;	    /* if trailing headers should be consumed */
     78  1.1  christos 	char _hex_count;
     79  1.1  christos 	char _state;
     80  1.4  christos 	uint64_t _total_read;
     81  1.4  christos 	uint64_t _total_overhead;
     82  1.1  christos };
     83  1.1  christos 
     84  1.1  christos /* the function rewrites the buffer given as (buf, bufsz) removing the chunked-
     85  1.1  christos  * encoding headers.  When the function returns without an error, bufsz is
     86  1.1  christos  * updated to the length of the decoded data available.  Applications should
     87  1.1  christos  * repeatedly call the function while it returns -2 (incomplete) every time
     88  1.1  christos  * supplying newly arrived data.  If the end of the chunked-encoded data is
     89  1.1  christos  * found, the function returns a non-negative number indicating the number of
     90  1.1  christos  * octets left undecoded, that starts from the offset returned by `*bufsz`.
     91  1.1  christos  * Returns -1 on error.
     92  1.1  christos  */
     93  1.1  christos ssize_t
     94  1.1  christos phr_decode_chunked(struct phr_chunked_decoder *decoder, char *buf,
     95  1.1  christos 		   size_t *bufsz);
     96  1.1  christos 
     97  1.1  christos /* returns if the chunked decoder is in middle of chunked data */
     98  1.1  christos int
     99  1.1  christos phr_decode_chunked_is_in_data(struct phr_chunked_decoder *decoder);
    100  1.1  christos 
    101  1.1  christos #ifdef __cplusplus
    102  1.1  christos }
    103  1.1  christos #endif
    104  1.1  christos 
    105  1.1  christos #endif
    106