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