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