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.c File Reference
#include <assert.h>
#include <stddef.h>
#include <string.h>
#include "picohttpparser.h"

Macros

#define likely(x)
 
#define unlikely(x)
 
#define ALIGNED(n)
 
#define IS_PRINTABLE_ASCII(c)
 
#define CHECK_EOF()
 
#define EXPECT_CHAR_NO_CHECK(ch)
 
#define EXPECT_CHAR(ch)
 
#define ADVANCE_TOKEN(tok, toklen)
 
#define DOIT()
 
#define PARSE_INT(valp_, mul_)
 
#define PARSE_INT_3(valp_)
 

Enumerations

enum  {
  CHUNKED_IN_CHUNK_SIZE , CHUNKED_IN_CHUNK_EXT , CHUNKED_IN_CHUNK_DATA , CHUNKED_IN_CHUNK_CRLF ,
  CHUNKED_IN_TRAILERS_LINE_HEAD , CHUNKED_IN_TRAILERS_LINE_MIDDLE
}
 

Functions

static const char * findchar_fast_generic (const char *buf, const char *buf_end, const char *ranges, size_t ranges_size, int *found)
 
static const char * findchar_fast (const char *buf, const char *buf_end, const char *ranges, size_t ranges_size, int *found)
 
static const char * get_token_to_eol (const char *buf, const char *buf_end, const char **token, size_t *token_len, int *ret)
 
static const char * is_complete (const char *buf, const char *buf_end, size_t last_len, int *ret)
 
static const char * parse_token (const char *buf, const char *buf_end, const char **token, size_t *token_len, char next_char, int *ret)
 
static const char * parse_http_version (const char *buf, const char *buf_end, int *minor_version, int *ret)
 
static const char * parse_headers (const char *buf, const char *buf_end, struct phr_header *headers, size_t *num_headers, size_t max_headers, int *ret)
 
static const char * parse_request (const char *buf, const char *buf_end, 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 max_headers, int *ret)
 
int phr_parse_request (const char *buf_start, 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)
 
static const char * parse_response (const char *buf, const char *buf_end, int *minor_version, int *status, const char **msg, size_t *msg_len, struct phr_header *headers, size_t *num_headers, size_t max_headers, int *ret)
 
int phr_parse_response (const char *buf_start, 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)
 
int phr_parse_headers (const char *buf_start, size_t len, struct phr_header *headers, size_t *num_headers, size_t last_len)
 
static int decode_hex (int ch)
 
ssize_t phr_decode_chunked (struct phr_chunked_decoder *decoder, char *buf, size_t *_bufsz)
 
int phr_decode_chunked_is_in_data (struct phr_chunked_decoder *decoder)
 

Variables

static const char * token_char_map
 

Macro Definition Documentation

◆ ADVANCE_TOKEN

#define ADVANCE_TOKEN ( tok,
toklen )
Value:
do { \
const char *tok_start = buf; \
static const char ALIGNED(16) ranges2[16] = "\000\040\177\177"; \
int found2; \
buf = findchar_fast(buf, buf_end, ranges2, 4, &found2); \
if (!found2) { \
CHECK_EOF(); \
} \
while (1) { \
if (*buf == ' ') { \
break; \
} else if (unlikely(!IS_PRINTABLE_ASCII(*buf))) { \
if ((unsigned char)*buf < '\040' || *buf == '\177') { \
*ret = -1; \
return NULL; \
} \
} \
++buf; \
CHECK_EOF(); \
} \
tok = tok_start; \
toklen = buf - tok_start; \
} while (0)
#define IS_PRINTABLE_ASCII(c)
Definition picohttpparser.c:71
static const char * findchar_fast(const char *buf, const char *buf_end, const char *ranges, size_t ranges_size, int *found)
Definition picohttpparser.c:232
#define ALIGNED(n)
Definition picohttpparser.c:68
#define unlikely(x)
Definition picohttpparser.c:62

◆ ALIGNED

#define ALIGNED ( n)
Value:
__attribute__((aligned(n)))

◆ CHECK_EOF

#define CHECK_EOF ( )
Value:
if (buf == buf_end) { \
*ret = -2; \
return NULL; \
}

◆ DOIT

#define DOIT ( )
Value:
do { \
goto NonPrintable; \
++buf; \
} while (0)

◆ EXPECT_CHAR

#define EXPECT_CHAR ( ch)
Value:
CHECK_EOF(); \
EXPECT_CHAR_NO_CHECK(ch);
#define CHECK_EOF()
Definition picohttpparser.c:73

◆ EXPECT_CHAR_NO_CHECK

#define EXPECT_CHAR_NO_CHECK ( ch)
Value:
if (*buf++ != ch) { \
*ret = -1; \
return NULL; \
}

◆ IS_PRINTABLE_ASCII

#define IS_PRINTABLE_ASCII ( c)
Value:
((unsigned char)(c) - 040u < 0137u)

◆ likely

#define likely ( x)
Value:
(x)

◆ PARSE_INT

#define PARSE_INT ( valp_,
mul_ )
Value:
if (*buf < '0' || '9' < *buf) { \
buf++; \
*ret = -1; \
return NULL; \
} \
*(valp_) = (mul_) * (*buf++ - '0');

◆ PARSE_INT_3

#define PARSE_INT_3 ( valp_)
Value:
do { \
int res_ = 0; \
PARSE_INT(&res_, 100) \
*valp_ = res_; \
PARSE_INT(&res_, 10) \
*valp_ += res_; \
PARSE_INT(&res_, 1) \
*valp_ += res_; \
} while (0)

◆ unlikely

#define unlikely ( x)
Value:
(x)

Enumeration Type Documentation

◆ anonymous enum

anonymous enum
Enumerator
CHUNKED_IN_CHUNK_SIZE 
CHUNKED_IN_CHUNK_EXT 
CHUNKED_IN_CHUNK_DATA 
CHUNKED_IN_CHUNK_CRLF 
CHUNKED_IN_TRAILERS_LINE_HEAD 
CHUNKED_IN_TRAILERS_LINE_MIDDLE 

Function Documentation

◆ decode_hex()

static int decode_hex ( int ch)
static

◆ findchar_fast()

static const char * findchar_fast ( const char * buf,
const char * buf_end,
const char * ranges,
size_t ranges_size,
int * found )
static

◆ findchar_fast_generic()

static const char * findchar_fast_generic ( const char * buf,
const char * buf_end,
const char * ranges,
size_t ranges_size,
int * found )
static

◆ get_token_to_eol()

static const char * get_token_to_eol ( const char * buf,
const char * buf_end,
const char ** token,
size_t * token_len,
int * ret )
static

◆ is_complete()

static const char * is_complete ( const char * buf,
const char * buf_end,
size_t last_len,
int * ret )
static

◆ parse_headers()

static const char * parse_headers ( const char * buf,
const char * buf_end,
struct phr_header * headers,
size_t * num_headers,
size_t max_headers,
int * ret )
static

◆ parse_http_version()

static const char * parse_http_version ( const char * buf,
const char * buf_end,
int * minor_version,
int * ret )
static

◆ parse_request()

static const char * parse_request ( const char * buf,
const char * buf_end,
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 max_headers,
int * ret )
static

◆ parse_response()

static const char * parse_response ( const char * buf,
const char * buf_end,
int * minor_version,
int * status,
const char ** msg,
size_t * msg_len,
struct phr_header * headers,
size_t * num_headers,
size_t max_headers,
int * ret )
static

◆ parse_token()

static const char * parse_token ( const char * buf,
const char * buf_end,
const char ** token,
size_t * token_len,
char next_char,
int * ret )
static

◆ phr_decode_chunked()

ssize_t phr_decode_chunked ( struct phr_chunked_decoder * decoder,
char * buf,
size_t * _bufsz )

◆ phr_decode_chunked_is_in_data()

int phr_decode_chunked_is_in_data ( struct phr_chunked_decoder * decoder)

◆ phr_parse_headers()

int phr_parse_headers ( const char * buf_start,
size_t len,
struct phr_header * headers,
size_t * num_headers,
size_t last_len )

◆ phr_parse_request()

int phr_parse_request ( const char * buf_start,
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 )

◆ phr_parse_response()

int phr_parse_response ( const char * buf_start,
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 )

Variable Documentation

◆ token_char_map

const char* token_char_map
static
Initial value:
=
"\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
"\0\1\0\1\1\1\1\1\0\0\1\1\0\1\1\0\1\1\1\1\1\1\1\1\1\1\0\0\0\0\0\0"
"\0\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\0\0\0\1\1"
"\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\0\1\0\1\0"
"\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
"\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
"\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
"\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"