2020-03-03 13:47:51 +00:00
/*
Copyright ( c ) 2016 - 2020 Chung , Hyung - Hwan . All rights reserved .
Redistribution and use in source and binary forms , with or without
modification , are permitted provided that the following conditions
are met :
1. Redistributions of source code must retain the above copyright
notice , this list of conditions and the following disclaimer .
2. Redistributions in binary form must reproduce the above copyright
notice , this list of conditions and the following disclaimer in the
documentation and / or other materials provided with the distribution .
THIS SOFTWARE IS PROVIDED BY THE AUTHOR " AS IS " AND ANY EXPRESS OR
IMPLIED WARRANTIES , INCLUDING , BUT NOT LIMITED TO , THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED .
IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT , INDIRECT ,
INCIDENTAL , SPECIAL , EXEMPLARY , OR CONSEQUENTIAL DAMAGES ( INCLUDING , BUT
NOT LIMITED TO , PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES ; LOSS OF USE ,
DATA , OR PROFITS ; OR BUSINESS INTERRUPTION ) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY , WHETHER IN CONTRACT , STRICT LIABILITY , OR TORT
( INCLUDING NEGLIGENCE OR OTHERWISE ) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE , EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE .
*/
2021-07-22 07:30:20 +00:00
# include <hio-htrd.h>
# include <hio-chr.h>
# include <hio-path.h>
# include "hio-prv.h"
2020-03-03 13:47:51 +00:00
2021-07-22 07:30:20 +00:00
static const hio_bch_t NUL = ' \0 ' ;
2020-03-03 13:47:51 +00:00
/* for htrd->fed.s.flags */
# define CONSUME_UNTIL_CLOSE (1 << 0)
/* for htrd->flags */
# define FEEDING_SUSPENDED (1 << 0)
# define FEEDING_DUMMIFIED (1 << 1)
2021-07-22 07:30:20 +00:00
static HIO_INLINE int is_whspace_octet ( hio_bch_t c )
2020-03-03 13:47:51 +00:00
{
return c = = ' ' | | c = = ' \t ' | | c = = ' \r ' | | c = = ' \n ' ;
}
2021-07-22 07:30:20 +00:00
static HIO_INLINE int is_space_octet ( hio_bch_t c )
2020-03-03 13:47:51 +00:00
{
return c = = ' ' | | c = = ' \t ' | | c = = ' \r ' ;
}
2021-07-22 07:30:20 +00:00
static HIO_INLINE int is_purespace_octet ( hio_bch_t c )
2020-03-03 13:47:51 +00:00
{
return c = = ' ' | | c = = ' \t ' ;
}
2021-07-22 07:30:20 +00:00
static HIO_INLINE int is_upalpha_octet ( hio_bch_t c )
2020-03-03 13:47:51 +00:00
{
return c > = ' A ' & & c < = ' Z ' ;
}
2021-07-22 07:30:20 +00:00
static HIO_INLINE int is_loalpha_octet ( hio_bch_t c )
2020-03-03 13:47:51 +00:00
{
return c > = ' a ' & & c < = ' z ' ;
}
2021-07-22 07:30:20 +00:00
static HIO_INLINE int is_alpha_octet ( hio_bch_t c )
2020-03-03 13:47:51 +00:00
{
return ( c > = ' A ' & & c < = ' Z ' ) | | ( c > = ' a ' & & c < = ' z ' ) ;
}
2021-07-22 07:30:20 +00:00
static HIO_INLINE int is_digit_octet ( hio_bch_t c )
2020-03-03 13:47:51 +00:00
{
return c > = ' 0 ' & & c < = ' 9 ' ;
}
2021-07-22 07:30:20 +00:00
static HIO_INLINE int is_xdigit_octet ( hio_bch_t c )
2020-03-03 13:47:51 +00:00
{
return ( c > = ' 0 ' & & c < = ' 9 ' ) | | ( c > = ' A ' & & c < = ' F ' ) | | ( c > = ' a ' & & c < = ' f ' ) ;
}
2021-07-22 07:30:20 +00:00
static HIO_INLINE int digit_to_num ( hio_bch_t c )
2020-03-03 13:47:51 +00:00
{
2021-07-22 07:30:20 +00:00
return HIO_DIGIT_TO_NUM ( c ) ;
2020-03-03 13:47:51 +00:00
}
2021-07-22 07:30:20 +00:00
static HIO_INLINE int xdigit_to_num ( hio_bch_t c )
2020-03-03 13:47:51 +00:00
{
2021-07-22 07:30:20 +00:00
return HIO_XDIGIT_TO_NUM ( c ) ;
2020-03-03 13:47:51 +00:00
}
2021-07-22 07:30:20 +00:00
static HIO_INLINE int push_to_buffer ( hio_htrd_t * htrd , hio_becs_t * octb , const hio_bch_t * ptr , hio_oow_t len )
2020-03-03 13:47:51 +00:00
{
2023-01-11 14:59:41 +00:00
if ( hio_becs_ncat ( octb , ptr , len ) = = ( hio_oow_t ) - 1 )
2020-03-03 13:47:51 +00:00
{
2021-07-22 07:30:20 +00:00
htrd - > errnum = HIO_HTRD_ENOMEM ;
2020-03-03 13:47:51 +00:00
return - 1 ;
}
return 0 ;
}
2021-07-22 07:30:20 +00:00
static HIO_INLINE int push_content ( hio_htrd_t * htrd , const hio_bch_t * ptr , hio_oow_t len )
2020-03-03 13:47:51 +00:00
{
2021-07-22 07:30:20 +00:00
HIO_ASSERT ( htrd - > hio , len > 0 ) ;
2020-03-03 13:47:51 +00:00
2020-05-17 18:09:19 +00:00
if ( htrd - > recbs . push_content ) return htrd - > recbs . push_content ( htrd , & htrd - > re , ptr , len ) ;
2020-05-16 19:12:10 +00:00
2023-01-11 14:59:41 +00:00
if ( hio_htre_addcontent ( & htrd - > re , ptr , len ) < = - 1 )
2020-03-03 13:47:51 +00:00
{
2021-07-22 07:30:20 +00:00
htrd - > errnum = HIO_HTRD_ENOMEM ;
2020-03-03 13:47:51 +00:00
return - 1 ;
}
2023-01-11 14:59:41 +00:00
/* hio_htre_addcontent() returns 1 on full success and 0 if adding is
2020-03-03 13:47:51 +00:00
* skipped . i treat both as success */
return 0 ;
}
2021-07-22 07:30:20 +00:00
static HIO_INLINE void clear_feed ( hio_htrd_t * htrd )
2020-03-03 13:47:51 +00:00
{
2023-01-11 14:59:41 +00:00
/* clear necessary part of the request/response before
2020-03-03 13:47:51 +00:00
* reading the next request / response */
htrd - > clean = 1 ;
2021-07-22 07:30:20 +00:00
hio_htre_clear ( & htrd - > re ) ;
2020-03-03 13:47:51 +00:00
2021-07-22 07:30:20 +00:00
hio_becs_clear ( & htrd - > fed . b . tra ) ;
hio_becs_clear ( & htrd - > fed . b . raw ) ;
2020-03-03 13:47:51 +00:00
2021-07-22 07:30:20 +00:00
HIO_MEMSET ( & htrd - > fed . s , 0 , HIO_SIZEOF ( htrd - > fed . s ) ) ;
2020-03-03 13:47:51 +00:00
}
2021-07-22 07:30:20 +00:00
hio_htrd_t * hio_htrd_open ( hio_t * hio , hio_oow_t xtnsize )
2020-03-03 13:47:51 +00:00
{
2021-07-22 07:30:20 +00:00
hio_htrd_t * htrd ;
2020-03-03 13:47:51 +00:00
2021-07-22 07:30:20 +00:00
htrd = ( hio_htrd_t * ) hio_allocmem ( hio , HIO_SIZEOF ( hio_htrd_t ) + xtnsize ) ;
if ( HIO_LIKELY ( htrd ) )
2020-03-03 13:47:51 +00:00
{
2021-07-22 07:30:20 +00:00
if ( HIO_UNLIKELY ( hio_htrd_init ( htrd , hio ) < = - 1 ) )
2020-03-03 13:47:51 +00:00
{
2021-07-22 07:30:20 +00:00
hio_freemem ( hio , htrd ) ;
return HIO_NULL ;
2020-03-03 13:47:51 +00:00
}
2021-07-22 07:30:20 +00:00
else HIO_MEMSET ( htrd + 1 , 0 , xtnsize ) ;
2020-03-03 13:47:51 +00:00
}
return htrd ;
}
2021-07-22 07:30:20 +00:00
void hio_htrd_close ( hio_htrd_t * htrd )
2020-03-03 13:47:51 +00:00
{
2021-07-22 07:30:20 +00:00
hio_htrd_fini ( htrd ) ;
hio_freemem ( htrd - > hio , htrd ) ;
2020-03-03 13:47:51 +00:00
}
2021-07-22 07:30:20 +00:00
int hio_htrd_init ( hio_htrd_t * htrd , hio_t * hio )
2020-03-03 13:47:51 +00:00
{
2021-07-22 07:30:20 +00:00
HIO_MEMSET ( htrd , 0 , HIO_SIZEOF ( * htrd ) ) ;
htrd - > hio = hio ;
htrd - > option = HIO_HTRD_REQUEST | HIO_HTRD_RESPONSE ;
2020-03-03 13:47:51 +00:00
#if 0
2021-07-22 07:30:20 +00:00
hio_becs_init ( & htrd - > tmp . qparam , htrd - > hio , 0 ) ;
2020-03-03 13:47:51 +00:00
# endif
2021-07-22 07:30:20 +00:00
hio_becs_init ( & htrd - > fed . b . raw , htrd - > hio , 0 ) ;
hio_becs_init ( & htrd - > fed . b . tra , htrd - > hio , 0 ) ;
2020-03-03 13:47:51 +00:00
2021-07-22 07:30:20 +00:00
if ( hio_htre_init ( & htrd - > re , hio ) < = - 1 )
2020-03-03 13:47:51 +00:00
{
2021-07-22 07:30:20 +00:00
hio_becs_fini ( & htrd - > fed . b . tra ) ;
hio_becs_fini ( & htrd - > fed . b . raw ) ;
2020-03-03 13:47:51 +00:00
#if 0
2021-07-22 07:30:20 +00:00
hio_becs_fini ( & htrd - > tmp . qparam ) ;
2020-03-03 13:47:51 +00:00
# endif
return - 1 ;
}
htrd - > clean = 1 ;
return 0 ;
}
2021-07-22 07:30:20 +00:00
void hio_htrd_fini ( hio_htrd_t * htrd )
2020-03-03 13:47:51 +00:00
{
2021-07-22 07:30:20 +00:00
hio_htre_fini ( & htrd - > re ) ;
2020-03-03 13:47:51 +00:00
2021-07-22 07:30:20 +00:00
hio_becs_fini ( & htrd - > fed . b . tra ) ;
hio_becs_fini ( & htrd - > fed . b . raw ) ;
2020-03-03 13:47:51 +00:00
#if 0
2021-07-22 07:30:20 +00:00
hio_becs_fini ( & htrd - > tmp . qparam ) ;
2020-03-03 13:47:51 +00:00
# endif
}
2021-07-22 07:30:20 +00:00
static hio_bch_t * parse_initial_line ( hio_htrd_t * htrd , hio_bch_t * line )
2020-03-03 13:47:51 +00:00
{
2021-07-22 07:30:20 +00:00
hio_bch_t * p = line ;
hio_bcs_t tmp ;
2020-03-03 13:47:51 +00:00
#if 0
/* ignore leading spaces excluding crlf */
while ( is_space_octet ( * p ) ) p + + ;
# endif
/* the method should start with an alphabet */
if ( ! is_alpha_octet ( * p ) ) goto badre ;
/* get the method name */
tmp . ptr = p ;
do { p + + ; } while ( is_alpha_octet ( * p ) ) ;
tmp . len = p - tmp . ptr ;
2021-07-22 07:30:20 +00:00
htrd - > re . type = HIO_HTRE_Q ;
if ( htrd - > option & HIO_HTRD_REQUEST )
2020-03-03 13:47:51 +00:00
{
/* method name must be followed by space */
if ( ! is_space_octet ( * p ) ) goto badre ;
* p = ' \0 ' ; /* null-terminate the method name */
2021-07-22 07:30:20 +00:00
htrd - > re . u . q . method . type = hio_bchars_to_http_method ( tmp . ptr , tmp . len ) ;
2020-03-03 13:47:51 +00:00
htrd - > re . u . q . method . name = tmp . ptr ;
2023-01-08 16:09:34 +00:00
htrd - > re . u . q . method . len = tmp . len ;
2020-03-03 13:47:51 +00:00
}
2021-07-22 07:30:20 +00:00
else if ( ( htrd - > option & HIO_HTRD_RESPONSE ) & & hio_comp_bchars_bcstr ( tmp . ptr , tmp . len , " HTTP " , 1 ) = = 0 )
2020-03-03 13:47:51 +00:00
{
/* it begins with HTTP. it may be a response */
2021-07-22 07:30:20 +00:00
htrd - > re . type = HIO_HTRE_S ;
2020-03-03 13:47:51 +00:00
}
else goto badre ;
2021-07-22 07:30:20 +00:00
if ( htrd - > re . type = = HIO_HTRE_S )
2020-03-03 13:47:51 +00:00
{
/* response */
int n , status ;
if ( * p = = ' / ' & & p [ 1 ] ! = ' \0 ' & & p [ 2 ] = = ' . ' )
{
int q = digit_to_num ( p [ 1 ] ) ;
int w = digit_to_num ( p [ 3 ] ) ;
if ( q > = 0 & & w > = 0 )
{
htrd - > re . version . major = q ;
htrd - > re . version . minor = w ;
p + = 4 ;
}
else goto badre ;
}
else goto badre ;
/* version must be followed by space */
if ( ! is_space_octet ( * p ) ) goto badre ;
* p = ' \0 ' ; /* null-terminate version string */
htrd - > re . verstr = tmp . ptr ;
/* skip spaces */
do p + + ; while ( is_space_octet ( * p ) ) ;
2023-01-11 14:59:41 +00:00
2020-03-03 13:47:51 +00:00
n = digit_to_num ( * p ) ;
if ( n < = - 1 ) goto badre ;
tmp . ptr = p ;
status = 0 ;
do
{
status = status * 10 + n ;
p + + ;
2023-01-11 14:59:41 +00:00
}
2020-03-03 13:47:51 +00:00
while ( ( n = digit_to_num ( * p ) ) > = 0 ) ;
if ( ! is_space_octet ( * p ) ) goto badre ;
* p = ' \0 ' ; /* null-terminate the status code */
htrd - > re . u . s . code . val = status ;
htrd - > re . u . s . code . str = tmp . ptr ;
/* i don't treat the following weird messages as bad message:
* no status message follows the status code
*/
/* skip spaces */
do p + + ; while ( is_space_octet ( * p ) ) ;
2023-01-11 14:59:41 +00:00
2020-03-03 13:47:51 +00:00
tmp . ptr = p ;
tmp . len = 0 ;
2023-01-11 14:59:41 +00:00
while ( * p ! = ' \0 ' & & * p ! = ' \n ' )
2020-03-03 13:47:51 +00:00
{
if ( ! is_space_octet ( * p ) ) tmp . len = p - tmp . ptr + 1 ;
p + + ;
}
2023-01-11 14:59:41 +00:00
2020-03-03 13:47:51 +00:00
/* if the line does not end with a new line, it is a bad request */
if ( * p ! = ' \n ' ) goto badre ;
/* null-terminate the message */
2021-07-22 07:30:20 +00:00
( ( hio_bch_t * ) tmp . ptr ) [ tmp . len ] = ' \0 ' ;
2020-03-03 13:47:51 +00:00
htrd - > re . u . s . mesg = tmp . ptr ;
}
else
{
2021-07-22 07:30:20 +00:00
hio_bcs_t param ;
hio_bcs_t anchor ;
2020-03-03 13:47:51 +00:00
/* skip spaces */
do p + + ; while ( is_space_octet ( * p ) ) ;
/* process the url part */
2020-06-02 06:41:20 +00:00
tmp . ptr = p ; tmp . len = 0 ;
2021-07-22 07:30:20 +00:00
param . ptr = HIO_NULL ; param . len = 0 ;
anchor . ptr = HIO_NULL ; anchor . len = 0 ;
2020-06-02 06:41:20 +00:00
while ( 1 )
2020-03-03 13:47:51 +00:00
{
2021-07-22 07:30:20 +00:00
if ( HIO_UNLIKELY ( * p = = ' \0 ' ) ) goto badre ;
2020-06-02 06:41:20 +00:00
else if ( is_space_octet ( * p ) | | * p = = ' ? ' | | * p = = ' # ' )
2020-03-03 13:47:51 +00:00
{
2023-01-11 14:59:41 +00:00
tmp . len = p - tmp . ptr ;
2020-06-02 06:41:20 +00:00
if ( tmp . len < = 0 ) goto badre ;
break ;
2020-03-03 13:47:51 +00:00
}
2020-06-02 06:41:20 +00:00
else p + + ;
}
if ( * p = = ' ? ' )
{
param . ptr = + + p ;
while ( 1 )
2020-03-03 13:47:51 +00:00
{
2021-07-22 07:30:20 +00:00
if ( HIO_UNLIKELY ( * p = = ' \0 ' ) ) goto badre ;
2020-06-02 06:41:20 +00:00
else if ( is_space_octet ( * p ) | | * p = = ' # ' )
2020-03-03 13:47:51 +00:00
{
2020-06-02 06:41:20 +00:00
param . len = p - param . ptr ;
break ;
2020-03-03 13:47:51 +00:00
}
2020-06-02 06:41:20 +00:00
else p + + ;
2020-03-03 13:47:51 +00:00
}
}
2020-06-02 06:41:20 +00:00
if ( * p = = ' # ' )
2020-03-03 13:47:51 +00:00
{
2020-06-02 06:41:20 +00:00
anchor . ptr = + + p ;
while ( 1 )
2020-03-03 13:47:51 +00:00
{
2021-07-22 07:30:20 +00:00
if ( HIO_UNLIKELY ( * p = = ' \0 ' ) ) goto badre ;
2020-06-02 06:41:20 +00:00
else if ( is_space_octet ( * p ) )
{
anchor . len = p - anchor . ptr ;
break ;
}
else p + + ;
2020-03-03 13:47:51 +00:00
}
}
2020-06-02 06:41:20 +00:00
tmp . ptr [ tmp . len ] = ' \0 ' ;
if ( param . ptr ) param . ptr [ param . len ] = ' \0 ' ;
if ( anchor . ptr ) anchor . ptr [ anchor . len ] = ' \0 ' ;
2020-03-03 13:47:51 +00:00
2020-06-02 06:41:20 +00:00
htrd - > re . u . q . path = tmp ;
htrd - > re . u . q . param = param ;
htrd - > re . u . q . anchor = anchor ;
2020-03-03 13:47:51 +00:00
2021-07-22 07:30:20 +00:00
if ( htrd - > option & HIO_HTRD_CANONQPATH )
2020-03-03 13:47:51 +00:00
{
2021-07-22 07:30:20 +00:00
hio_bch_t * qpath = htrd - > re . u . q . path . ptr ;
2020-03-03 13:47:51 +00:00
/* if the url begins with xxx://,
* skip xxx : / and canonicalize from the second slash */
while ( is_alpha_octet ( * qpath ) ) qpath + + ;
2021-07-22 07:30:20 +00:00
if ( hio_comp_bcstr_limited ( qpath , " :// " , 3 , 1 ) = = 0 )
2020-03-03 13:47:51 +00:00
{
qpath = qpath + 2 ; /* set the position to the second / in :// */
2021-07-22 07:30:20 +00:00
htrd - > re . u . q . path . len = hio_canon_bcstr_path ( qpath , qpath , 0 ) ;
2020-03-03 13:47:51 +00:00
htrd - > re . u . q . path . len + = qpath - htrd - > re . u . q . path . ptr ;
}
else
{
qpath = htrd - > re . u . q . path . ptr ;
2021-07-22 07:30:20 +00:00
htrd - > re . u . q . path . len = hio_canon_bcstr_path ( qpath , qpath , 0 ) ;
2020-03-03 13:47:51 +00:00
}
}
2023-01-11 14:59:41 +00:00
2020-03-03 13:47:51 +00:00
/* skip spaces after the url part */
do { p + + ; } while ( is_space_octet ( * p ) ) ;
2023-01-11 14:59:41 +00:00
2020-03-03 13:47:51 +00:00
tmp . ptr = p ;
/* check protocol version */
if ( ( p [ 0 ] = = ' H ' | | p [ 0 ] = = ' h ' ) & &
( p [ 1 ] = = ' T ' | | p [ 1 ] = = ' t ' ) & &
( p [ 2 ] = = ' T ' | | p [ 2 ] = = ' t ' ) & &
( p [ 3 ] = = ' P ' | | p [ 3 ] = = ' p ' ) & &
p [ 4 ] = = ' / ' & & p [ 6 ] = = ' . ' )
{
int q = digit_to_num ( p [ 5 ] ) ;
int w = digit_to_num ( p [ 7 ] ) ;
if ( q > = 0 & & w > = 0 )
{
htrd - > re . version . major = q ;
htrd - > re . version . minor = w ;
p + = 8 ;
}
else goto badre ;
}
else goto badre ;
2023-01-11 14:59:41 +00:00
2020-03-03 13:47:51 +00:00
tmp . len = p - tmp . ptr ;
/* skip trailing spaces on the line */
while ( is_space_octet ( * p ) ) p + + ;
/* if the line does not end with a new line, it is a bad request */
if ( * p ! = ' \n ' ) goto badre ;
2021-07-22 07:30:20 +00:00
( ( hio_bch_t * ) tmp . ptr ) [ tmp . len ] = ' \0 ' ;
2020-03-03 13:47:51 +00:00
htrd - > re . verstr = tmp . ptr ;
}
2023-01-11 14:59:41 +00:00
2020-03-03 13:47:51 +00:00
/* adjust Connection: Keep-Alive for HTTP 1.1 or later.
* this is initial . it can be adjusted further in capture_connection ( ) . */
2023-01-11 14:59:41 +00:00
if ( htrd - > re . version . major > 1 | |
2020-03-03 13:47:51 +00:00
( htrd - > re . version . major = = 1 & & htrd - > re . version . minor > = 1 ) )
{
2021-07-22 07:30:20 +00:00
htrd - > re . flags | = HIO_HTRE_ATTR_KEEPALIVE ;
2020-03-03 13:47:51 +00:00
}
return + + p ;
badre :
2021-07-22 07:30:20 +00:00
htrd - > errnum = HIO_HTRD_EBADRE ;
return HIO_NULL ;
2020-03-03 13:47:51 +00:00
}
2021-07-22 07:30:20 +00:00
void hio_htrd_clear ( hio_htrd_t * htrd )
2020-03-03 13:47:51 +00:00
{
clear_feed ( htrd ) ;
htrd - > flags = 0 ;
}
2021-07-22 07:30:20 +00:00
hio_htrd_errnum_t hio_htrd_geterrnum ( hio_htrd_t * htrd )
2020-03-03 13:47:51 +00:00
{
return htrd - > errnum ;
}
2021-07-22 07:30:20 +00:00
hio_bitmask_t hio_htrd_getoption ( hio_htrd_t * htrd )
2020-03-03 13:47:51 +00:00
{
return htrd - > option ;
}
2021-07-22 07:30:20 +00:00
void hio_htrd_setoption ( hio_htrd_t * htrd , hio_bitmask_t mask )
2020-03-03 13:47:51 +00:00
{
2021-07-14 05:18:19 +00:00
htrd - > option = mask ;
2020-03-03 13:47:51 +00:00
}
2021-07-22 07:30:20 +00:00
const hio_htrd_recbs_t * hio_htrd_getrecbs ( hio_htrd_t * htrd )
2020-03-03 13:47:51 +00:00
{
2020-05-17 18:09:19 +00:00
return & htrd - > recbs ;
2020-03-03 13:47:51 +00:00
}
2021-07-22 07:30:20 +00:00
void hio_htrd_setrecbs ( hio_htrd_t * htrd , const hio_htrd_recbs_t * recbs )
2020-03-03 13:47:51 +00:00
{
2020-05-17 18:09:19 +00:00
htrd - > recbs = * recbs ;
2020-03-03 13:47:51 +00:00
}
2021-07-22 07:30:20 +00:00
static int capture_connection ( hio_htrd_t * htrd , hio_htb_pair_t * pair )
2020-03-03 13:47:51 +00:00
{
2021-07-22 07:30:20 +00:00
hio_htre_hdrval_t * val ;
2020-03-03 13:47:51 +00:00
2021-07-22 07:30:20 +00:00
val = HIO_HTB_VPTR ( pair ) ;
2020-03-03 13:47:51 +00:00
while ( val - > next ) val = val - > next ;
2023-01-11 14:59:41 +00:00
/* The value for Connection: may get comma-separated.
2021-07-22 07:30:20 +00:00
* so use hio_find_bcstr_word_in_bcstr ( ) instead of hio_comp_bcstr ( ) . */
2020-03-03 13:47:51 +00:00
2021-07-22 07:30:20 +00:00
if ( hio_find_bcstr_word_in_bcstr ( val - > ptr , " close " , ' , ' , 1 ) )
2020-03-03 13:47:51 +00:00
{
2021-07-22 07:30:20 +00:00
htrd - > re . flags & = ~ HIO_HTRE_ATTR_KEEPALIVE ;
2020-03-03 13:47:51 +00:00
return 0 ;
}
2021-07-22 07:30:20 +00:00
if ( hio_find_bcstr_word_in_bcstr ( val - > ptr , " keep-alive " , ' , ' , 1 ) )
2020-03-03 13:47:51 +00:00
{
2021-07-22 07:30:20 +00:00
htrd - > re . flags | = HIO_HTRE_ATTR_KEEPALIVE ;
2020-03-03 13:47:51 +00:00
return 0 ;
}
/* Basically i don't care about other values.
* but for HTTP 1.0 , other values will set connection to ' close ' .
*
* Other values include even Keep - Alive specified multiple times .
* Connection : Keep - Alive
* Connection : Keep - Alive
* For the second Keep - Alive , this function sees ' Keep - Alive , Keep - Alive '
* That ' s because values of the same keys are concatenated .
*/
2020-05-01 18:01:29 +00:00
if ( htrd - > re . version . major < 1 | | ( htrd - > re . version . major = = 1 & & htrd - > re . version . minor < = 0 ) )
2020-03-03 13:47:51 +00:00
{
2021-07-22 07:30:20 +00:00
htrd - > re . flags & = ~ HIO_HTRE_ATTR_KEEPALIVE ;
2020-03-03 13:47:51 +00:00
}
return 0 ;
}
2021-07-22 07:30:20 +00:00
static int capture_content_length ( hio_htrd_t * htrd , hio_htb_pair_t * pair )
2020-03-03 13:47:51 +00:00
{
2021-07-22 07:30:20 +00:00
hio_oow_t len = 0 , off = 0 , tmp ;
const hio_bch_t * ptr ;
hio_htre_hdrval_t * val ;
2020-03-03 13:47:51 +00:00
/* get the last content_length */
2021-07-22 07:30:20 +00:00
val = HIO_HTB_VPTR ( pair ) ;
2020-03-03 13:47:51 +00:00
while ( val - > next ) val = val - > next ;
ptr = val - > ptr ;
while ( off < val - > len )
{
2020-05-17 18:09:19 +00:00
int num = digit_to_num ( ptr [ off ] ) ;
2020-03-03 13:47:51 +00:00
if ( num < = - 1 )
{
/* the length contains a non-digit */
2021-07-22 07:30:20 +00:00
htrd - > errnum = HIO_HTRD_EBADRE ;
2020-03-03 13:47:51 +00:00
return - 1 ;
}
tmp = len * 10 + num ;
if ( tmp < len )
{
/* the length has overflown */
2021-07-22 07:30:20 +00:00
htrd - > errnum = HIO_HTRD_EBADRE ;
2020-03-03 13:47:51 +00:00
return - 1 ;
}
len = tmp ;
off + + ;
}
if ( off = = 0 )
{
/* no length was provided */
2021-07-22 07:30:20 +00:00
htrd - > errnum = HIO_HTRD_EBADRE ;
2020-03-03 13:47:51 +00:00
return - 1 ;
}
2021-07-22 07:30:20 +00:00
if ( ( htrd - > re . flags & HIO_HTRE_ATTR_CHUNKED ) & & len > 0 )
2020-03-03 13:47:51 +00:00
{
2023-01-11 14:59:41 +00:00
/* content-length is greater than 0
2020-03-03 13:47:51 +00:00
* while transfer - encoding : chunked is specified . */
2021-07-22 07:30:20 +00:00
htrd - > errnum = HIO_HTRD_EBADRE ;
2020-03-03 13:47:51 +00:00
return - 1 ;
}
2021-07-22 07:30:20 +00:00
htrd - > re . flags | = HIO_HTRE_ATTR_LENGTH ;
2020-03-03 13:47:51 +00:00
htrd - > re . attr . content_length = len ;
return 0 ;
}
2021-07-22 07:30:20 +00:00
static int capture_expect ( hio_htrd_t * htrd , hio_htb_pair_t * pair )
2020-03-03 13:47:51 +00:00
{
2021-07-22 07:30:20 +00:00
hio_htre_hdrval_t * val ;
2020-03-03 13:47:51 +00:00
/* Expect is included */
2023-01-11 14:59:41 +00:00
htrd - > re . flags | = HIO_HTRE_ATTR_EXPECT ;
2020-03-03 13:47:51 +00:00
2021-07-22 07:30:20 +00:00
val = HIO_HTB_VPTR ( pair ) ;
2023-01-11 14:59:41 +00:00
while ( val )
2022-10-09 16:41:07 +00:00
{
2020-03-03 13:47:51 +00:00
/* Expect: 100-continue is included */
2023-01-11 14:59:41 +00:00
if ( hio_comp_bcstr ( val - > ptr , " 100-continue " , 1 ) = = 0 ) htrd - > re . flags | = HIO_HTRE_ATTR_EXPECT100 ;
2020-03-03 13:47:51 +00:00
val = val - > next ;
}
return 0 ;
}
2021-07-22 07:30:20 +00:00
static int capture_status ( hio_htrd_t * htrd , hio_htb_pair_t * pair )
2020-03-03 13:47:51 +00:00
{
2021-07-22 07:30:20 +00:00
hio_htre_hdrval_t * val ;
2020-03-03 13:47:51 +00:00
2021-07-22 07:30:20 +00:00
val = HIO_HTB_VPTR ( pair ) ;
2020-03-03 13:47:51 +00:00
while ( val - > next ) val = val - > next ;
htrd - > re . attr . status = val - > ptr ;
return 0 ;
}
2021-07-22 07:30:20 +00:00
static int capture_transfer_encoding ( hio_htrd_t * htrd , hio_htb_pair_t * pair )
2020-03-03 13:47:51 +00:00
{
int n ;
2021-07-22 07:30:20 +00:00
hio_htre_hdrval_t * val ;
2020-03-03 13:47:51 +00:00
2021-07-22 07:30:20 +00:00
val = HIO_HTB_VPTR ( pair ) ;
2020-03-03 13:47:51 +00:00
while ( val - > next ) val = val - > next ;
2021-07-22 07:30:20 +00:00
n = hio_comp_bcstr ( val - > ptr , " chunked " , 1 ) ;
2020-03-03 13:47:51 +00:00
if ( n = = 0 )
{
/* if (htrd->re.attr.content_length > 0) */
2021-07-22 07:30:20 +00:00
if ( htrd - > re . flags & HIO_HTRE_ATTR_LENGTH )
2020-03-03 13:47:51 +00:00
{
/* both content-length and 'transfer-encoding: chunked' are specified. */
goto badre ;
}
2021-07-22 07:30:20 +00:00
htrd - > re . flags | = HIO_HTRE_ATTR_CHUNKED ;
2020-03-03 13:47:51 +00:00
return 0 ;
}
/* other encoding type not supported yet */
badre :
2021-07-22 07:30:20 +00:00
htrd - > errnum = HIO_HTRD_EBADRE ;
2020-03-03 13:47:51 +00:00
return - 1 ;
}
2021-07-22 07:30:20 +00:00
static HIO_INLINE int capture_key_header ( hio_htrd_t * htrd , hio_htb_pair_t * pair )
2020-03-03 13:47:51 +00:00
{
static struct
{
2021-07-22 07:30:20 +00:00
const hio_bch_t * ptr ;
hio_oow_t len ;
int ( * handler ) ( hio_htrd_t * , hio_htb_pair_t * ) ;
2023-01-11 14:59:41 +00:00
} hdrtab [ ] =
2020-03-03 13:47:51 +00:00
{
{ " Connection " , 10 , capture_connection } ,
{ " Content-Length " , 14 , capture_content_length } ,
{ " Expect " , 6 , capture_expect } ,
{ " Status " , 6 , capture_status } ,
{ " Transfer-Encoding " , 17 , capture_transfer_encoding }
} ;
int n ;
2021-07-22 07:30:20 +00:00
hio_oow_t mid , count , base = 0 ;
2020-03-03 13:47:51 +00:00
/* perform binary search */
2021-07-22 07:30:20 +00:00
for ( count = HIO_COUNTOF ( hdrtab ) ; count > 0 ; count / = 2 )
2020-03-03 13:47:51 +00:00
{
mid = base + count / 2 ;
2021-07-22 07:30:20 +00:00
n = hio_comp_bchars ( HIO_HTB_KPTR ( pair ) , HIO_HTB_KLEN ( pair ) , hdrtab [ mid ] . ptr , hdrtab [ mid ] . len , 1 ) ;
2020-03-03 13:47:51 +00:00
if ( n = = 0 )
{
/* bingo! */
2020-05-01 18:01:29 +00:00
return hdrtab [ mid ] . handler ( htrd , pair ) ;
2020-03-03 13:47:51 +00:00
}
if ( n > 0 ) { base = mid + 1 ; count - - ; }
}
/* No callback functions were interested in this header field. */
return 0 ;
}
struct hdr_cbserter_ctx_t
{
2021-07-22 07:30:20 +00:00
hio_htrd_t * htrd ;
2020-03-03 13:47:51 +00:00
void * vptr ;
2021-07-22 07:30:20 +00:00
hio_oow_t vlen ;
2020-03-03 13:47:51 +00:00
} ;
2023-11-16 03:29:54 +00:00
static hio_htb_pair_t * hdr_cbserter ( hio_htb_t * htb , hio_htb_pair_t * pair , void * kptr , hio_oow_t klen , void * ctx )
2020-03-03 13:47:51 +00:00
{
struct hdr_cbserter_ctx_t * tx = ( struct hdr_cbserter_ctx_t * ) ctx ;
2021-07-22 07:30:20 +00:00
if ( pair = = HIO_NULL )
2020-03-03 13:47:51 +00:00
{
/* the key is new. let's create a new pair. */
2023-01-11 14:59:41 +00:00
hio_htb_pair_t * p ;
2021-07-22 07:30:20 +00:00
hio_htre_hdrval_t * val ;
2020-03-03 13:47:51 +00:00
2021-07-22 07:30:20 +00:00
val = hio_allocmem ( htb - > hio , HIO_SIZEOF ( * val ) ) ;
if ( HIO_UNLIKELY ( ! val ) )
2020-03-03 13:47:51 +00:00
{
2021-07-22 07:30:20 +00:00
tx - > htrd - > errnum = HIO_HTRD_ENOMEM ;
return HIO_NULL ;
2020-03-03 13:47:51 +00:00
}
2021-07-22 07:30:20 +00:00
HIO_MEMSET ( val , 0 , HIO_SIZEOF ( * val ) ) ;
2020-03-03 13:47:51 +00:00
val - > ptr = tx - > vptr ;
val - > len = tx - > vlen ;
2021-07-22 07:30:20 +00:00
val - > next = HIO_NULL ;
2020-03-03 13:47:51 +00:00
2021-07-22 07:30:20 +00:00
p = hio_htb_allocpair ( htb , kptr , klen , val , 0 ) ;
2023-01-11 14:59:41 +00:00
if ( HIO_UNLIKELY ( ! p ) )
2020-03-03 13:47:51 +00:00
{
2021-07-22 07:30:20 +00:00
hio_freemem ( htb - > hio , val ) ;
tx - > htrd - > errnum = HIO_HTRD_ENOMEM ;
2020-03-03 13:47:51 +00:00
}
2023-01-11 14:59:41 +00:00
else
2020-03-03 13:47:51 +00:00
{
2020-05-15 06:18:49 +00:00
if ( capture_key_header ( tx - > htrd , p ) < = - 1 )
2020-03-03 13:47:51 +00:00
{
/* Destroy the pair created here
* as it is not added to the hash table yet */
2021-07-22 07:30:20 +00:00
hio_htb_freepair ( htb , p ) ;
p = HIO_NULL ;
2020-03-03 13:47:51 +00:00
}
}
return p ;
}
else
{
2023-01-11 14:59:41 +00:00
/* RFC2616
* Multiple message - header fields with the same field - name
* MAY be present in a message if and only if the entire
* field - value for that header field is defined as a
* comma - separated list [ i . e . , # ( values ) ] . It MUST be possible
* to combine the multiple header fields into one
2020-03-03 13:47:51 +00:00
* " field-name: field-value " pair , without changing the semantics
2023-01-11 14:59:41 +00:00
* of the message , by appending each subsequent field - value
* to the first , each separated by a comma . The order in which
2020-03-03 13:47:51 +00:00
* header fields with the same field - name are received is therefore
* significant to the interpretation of the combined field value ,
2023-01-11 14:59:41 +00:00
* and thus a proxy MUST NOT change the order of these field values
* when a message is forwarded .
2020-03-03 13:47:51 +00:00
* RFC6265 defines the syntax for Set - Cookie and Cookie .
* this seems to be conflicting with RFC2616 .
2023-01-11 14:59:41 +00:00
*
* Origin servers SHOULD NOT fold multiple Set - Cookie header fields
* into a single header field . The usual mechanism for folding HTTP
* headers fields ( i . e . , as defined in [ RFC2616 ] ) might change the
2020-03-03 13:47:51 +00:00
* semantics of the Set - Cookie header field because the % x2C ( " , " )
2023-01-11 14:59:41 +00:00
* character is used by Set - Cookie in a way that conflicts with
2020-03-03 13:47:51 +00:00
* such folding .
2023-01-11 14:59:41 +00:00
*
2020-03-03 13:47:51 +00:00
* So i just maintain the list of values for a key instead of
* folding them .
*/
2021-07-22 07:30:20 +00:00
hio_htre_hdrval_t * val ;
hio_htre_hdrval_t * tmp ;
2020-03-03 13:47:51 +00:00
2021-07-22 07:30:20 +00:00
val = ( hio_htre_hdrval_t * ) hio_allocmem ( tx - > htrd - > hio , HIO_SIZEOF ( * val ) ) ;
if ( HIO_UNLIKELY ( ! val ) )
2020-03-03 13:47:51 +00:00
{
2021-07-22 07:30:20 +00:00
tx - > htrd - > errnum = HIO_HTRD_ENOMEM ;
return HIO_NULL ;
2020-03-03 13:47:51 +00:00
}
2021-07-22 07:30:20 +00:00
HIO_MEMSET ( val , 0 , HIO_SIZEOF ( * val ) ) ;
2020-03-03 13:47:51 +00:00
val - > ptr = tx - > vptr ;
val - > len = tx - > vlen ;
2021-07-22 07:30:20 +00:00
val - > next = HIO_NULL ;
2020-03-03 13:47:51 +00:00
/* TODO: doubly linked list for speed-up??? */
2021-07-22 07:30:20 +00:00
tmp = HIO_HTB_VPTR ( pair ) ;
HIO_ASSERT ( tx - > htrd - > hio , tmp ! = HIO_NULL ) ;
2020-03-03 13:47:51 +00:00
/* find the tail */
while ( tmp - > next ) tmp = tmp - > next ;
/* append it to the list*/
2023-01-11 14:59:41 +00:00
tmp - > next = val ;
2020-03-03 13:47:51 +00:00
2021-07-22 07:30:20 +00:00
if ( capture_key_header ( tx - > htrd , pair ) < = - 1 ) return HIO_NULL ;
2020-03-03 13:47:51 +00:00
return pair ;
}
}
2021-07-22 07:30:20 +00:00
hio_bch_t * parse_header_field ( hio_htrd_t * htrd , hio_bch_t * line , hio_htb_t * tab )
2020-03-03 13:47:51 +00:00
{
2021-07-22 07:30:20 +00:00
hio_bch_t * p = line , * last ;
2020-03-03 13:47:51 +00:00
struct
{
2021-07-22 07:30:20 +00:00
hio_bch_t * ptr ;
hio_oow_t len ;
2020-03-03 13:47:51 +00:00
} name , value ;
#if 0
/* ignore leading spaces excluding crlf */
while ( is_space_octet ( * p ) ) p + + ;
# endif
2021-07-22 07:30:20 +00:00
HIO_ASSERT ( htrd - > hio , ! is_whspace_octet ( * p ) ) ;
2020-03-03 13:47:51 +00:00
/* check the field name */
name . ptr = last = p ;
while ( * p ! = ' \0 ' & & * p ! = ' \n ' & & * p ! = ' : ' )
{
if ( ! is_space_octet ( * p + + ) ) last = p ;
}
name . len = last - name . ptr ;
2023-01-11 14:59:41 +00:00
if ( * p ! = ' : ' )
2020-03-03 13:47:51 +00:00
{
2021-07-22 07:30:20 +00:00
if ( ! ( htrd - > option & HIO_HTRD_STRICT ) )
2020-03-03 13:47:51 +00:00
{
while ( is_space_octet ( * p ) ) p + + ;
2023-01-11 14:59:41 +00:00
if ( * p = = ' \n ' )
2020-03-03 13:47:51 +00:00
{
/* ignore a line without a colon */
p + + ;
return p ;
}
}
goto badhdr ;
}
* last = ' \0 ' ;
/* skip the colon and spaces after it */
do { p + + ; } while ( is_space_octet ( * p ) ) ;
value . ptr = last = p ;
while ( * p ! = ' \0 ' & & * p ! = ' \n ' )
{
if ( ! is_space_octet ( * p + + ) ) last = p ;
}
value . len = last - value . ptr ;
if ( * p ! = ' \n ' ) goto badhdr ; /* not ending with a new line */
2023-01-11 14:59:41 +00:00
/* peep at the beginning of the next line to check if it is
2020-03-03 13:47:51 +00:00
* the continuation */
if ( is_purespace_octet ( * + + p ) )
{
2023-01-11 14:59:41 +00:00
/* RFC: HTTP/1.0 headers may be folded onto multiple lines if
* each continuation line begins with a space or horizontal tab .
* All linear whitespace , including folding , has the same semantics
2020-03-03 13:47:51 +00:00
* as SP . */
2021-07-22 07:30:20 +00:00
hio_bch_t * cpydst ;
2020-03-03 13:47:51 +00:00
cpydst = p - 1 ;
if ( * ( cpydst - 1 ) = = ' \r ' ) cpydst - - ;
/* process all continued lines */
2023-01-11 14:59:41 +00:00
do
2020-03-03 13:47:51 +00:00
{
while ( * p ! = ' \0 ' & & * p ! = ' \n ' )
{
2023-01-11 14:59:41 +00:00
* cpydst = * p + + ;
2020-03-03 13:47:51 +00:00
if ( ! is_space_octet ( * cpydst + + ) ) last = cpydst ;
2023-01-11 14:59:41 +00:00
}
2020-03-03 13:47:51 +00:00
value . len = last - value . ptr ;
if ( * p ! = ' \n ' ) goto badhdr ;
if ( * ( cpydst - 1 ) = = ' \r ' ) cpydst - - ;
}
while ( is_purespace_octet ( * + + p ) ) ;
}
* last = ' \0 ' ;
/* insert the new field to the header table */
{
struct hdr_cbserter_ctx_t ctx ;
ctx . htrd = htrd ;
ctx . vptr = value . ptr ;
ctx . vlen = value . len ;
2021-07-22 07:30:20 +00:00
htrd - > errnum = HIO_HTRD_ENOERR ;
if ( hio_htb_cbsert (
2023-01-11 14:59:41 +00:00
tab , name . ptr , name . len ,
2021-07-22 07:30:20 +00:00
hdr_cbserter , & ctx ) = = HIO_NULL )
2020-03-03 13:47:51 +00:00
{
2023-01-11 14:59:41 +00:00
if ( htrd - > errnum = = HIO_HTRD_ENOERR )
2021-07-22 07:30:20 +00:00
htrd - > errnum = HIO_HTRD_ENOMEM ;
return HIO_NULL ;
2020-03-03 13:47:51 +00:00
}
}
return p ;
badhdr :
2021-07-22 07:30:20 +00:00
htrd - > errnum = HIO_HTRD_EBADHDR ;
return HIO_NULL ;
2020-03-03 13:47:51 +00:00
}
2021-07-22 07:30:20 +00:00
static HIO_INLINE int parse_initial_line_and_headers ( hio_htrd_t * htrd , const hio_bch_t * req , hio_oow_t rlen )
2020-03-03 13:47:51 +00:00
{
2021-07-22 07:30:20 +00:00
hio_bch_t * p ;
2020-03-03 13:47:51 +00:00
/* add the actual request */
if ( push_to_buffer ( htrd , & htrd - > fed . b . raw , req , rlen ) < = - 1 ) return - 1 ;
/* add the terminating null for easier parsing */
if ( push_to_buffer ( htrd , & htrd - > fed . b . raw , & NUL , 1 ) < = - 1 ) return - 1 ;
2021-07-22 07:30:20 +00:00
p = HIO_BECS_PTR ( & htrd - > fed . b . raw ) ;
2020-03-03 13:47:51 +00:00
#if 0
2021-07-22 07:30:20 +00:00
if ( htrd - > option & HIO_HTRD_SKIP_EMPTY_LINES )
2020-03-03 13:47:51 +00:00
while ( is_whspace_octet ( * p ) ) p + + ;
else
# endif
while ( is_space_octet ( * p ) ) p + + ;
2023-01-11 14:59:41 +00:00
2021-07-22 07:30:20 +00:00
HIO_ASSERT ( htrd - > hio , * p ! = ' \0 ' ) ;
2020-03-03 13:47:51 +00:00
/* parse the initial line */
2021-07-22 07:30:20 +00:00
if ( ! ( htrd - > option & HIO_HTRD_SKIP_INITIAL_LINE ) )
2020-03-03 13:47:51 +00:00
{
2020-05-16 19:12:10 +00:00
p = parse_initial_line ( htrd , p ) ;
2021-07-22 07:30:20 +00:00
if ( HIO_UNLIKELY ( ! p ) ) return - 1 ;
2020-03-03 13:47:51 +00:00
}
/* parse header fields */
do
{
while ( is_whspace_octet ( * p ) ) p + + ;
if ( * p = = ' \0 ' ) break ;
/* TODO: return error if protocol is 0.9.
* HTTP / 0.9 must not get headers . . . */
2020-05-16 19:12:10 +00:00
p = parse_header_field ( htrd , p , & htrd - > re . hdrtab ) ;
2021-07-22 07:30:20 +00:00
if ( HIO_UNLIKELY ( ! p ) ) return - 1 ;
2020-03-03 13:47:51 +00:00
}
while ( 1 ) ;
return 0 ;
}
/* chunk parsing phases */
# define GET_CHUNK_DONE 0
# define GET_CHUNK_LEN 1
# define GET_CHUNK_DATA 2
# define GET_CHUNK_CRLF 3
# define GET_CHUNK_TRAILERS 4
2021-07-22 07:30:20 +00:00
static const hio_bch_t * getchunklen ( hio_htrd_t * htrd , const hio_bch_t * ptr , hio_oow_t len )
2020-03-03 13:47:51 +00:00
{
2021-07-22 07:30:20 +00:00
const hio_bch_t * end = ptr + len ;
2020-03-03 13:47:51 +00:00
/* this function must be called in the GET_CHUNK_LEN context */
2021-07-22 07:30:20 +00:00
HIO_ASSERT ( htrd - > hio , htrd - > fed . s . chunk . phase = = GET_CHUNK_LEN ) ;
2020-03-03 13:47:51 +00:00
if ( htrd - > fed . s . chunk . count < = 0 )
{
/* skip leading spaces if the first character of
* the chunk length has not been read yet */
while ( ptr < end & & is_space_octet ( * ptr ) ) ptr + + ;
}
while ( ptr < end )
{
int n = xdigit_to_num ( * ptr ) ;
if ( n < = - 1 ) break ;
htrd - > fed . s . chunk . len = htrd - > fed . s . chunk . len * 16 + n ;
htrd - > fed . s . chunk . count + + ;
ptr + + ;
}
/* skip trailing spaces if the length has been read */
while ( ptr < end & & is_space_octet ( * ptr ) ) ptr + + ;
if ( ptr < end )
{
2023-01-11 14:59:41 +00:00
if ( * ptr = = ' \n ' )
2020-03-03 13:47:51 +00:00
{
/* the chunk length line ended properly */
if ( htrd - > fed . s . chunk . count < = 0 )
{
/* empty line - no more chunk */
htrd - > fed . s . chunk . phase = GET_CHUNK_DONE ;
}
else if ( htrd - > fed . s . chunk . len < = 0 )
{
/* length explicity specified to 0
get trailing headers . . . . */
htrd - > fed . s . chunk . phase = GET_CHUNK_TRAILERS ;
}
else
{
/* ready to read the chunk data... */
htrd - > fed . s . chunk . phase = GET_CHUNK_DATA ;
}
htrd - > fed . s . need = htrd - > fed . s . chunk . len ;
ptr + + ;
}
else
{
2021-07-22 07:30:20 +00:00
htrd - > errnum = HIO_HTRD_EBADRE ;
return HIO_NULL ;
2020-03-03 13:47:51 +00:00
}
}
return ptr ;
}
2021-07-22 07:30:20 +00:00
static const hio_bch_t * get_trailing_headers ( hio_htrd_t * htrd , const hio_bch_t * req , const hio_bch_t * end )
2020-03-03 13:47:51 +00:00
{
2021-07-22 07:30:20 +00:00
const hio_bch_t * ptr = req ;
2020-03-03 13:47:51 +00:00
while ( ptr < end )
{
2021-07-22 07:30:20 +00:00
register hio_bch_t b = * ptr + + ;
2020-03-03 13:47:51 +00:00
switch ( b )
{
case ' \0 ' :
2023-01-11 14:59:41 +00:00
/* guarantee that the request does not contain a null
2020-03-03 13:47:51 +00:00
* character */
2021-07-22 07:30:20 +00:00
htrd - > errnum = HIO_HTRD_EBADRE ;
return HIO_NULL ;
2020-03-03 13:47:51 +00:00
case ' \n ' :
2023-01-11 14:59:41 +00:00
if ( htrd - > fed . s . crlf < = 1 )
2020-03-03 13:47:51 +00:00
{
htrd - > fed . s . crlf = 2 ;
break ;
}
else
{
2021-07-22 07:30:20 +00:00
hio_bch_t * p ;
2020-05-17 18:09:19 +00:00
2021-07-22 07:30:20 +00:00
HIO_ASSERT ( htrd - > hio , htrd - > fed . s . crlf < = 3 ) ;
2020-03-03 13:47:51 +00:00
htrd - > fed . s . crlf = 0 ;
2020-05-17 18:09:19 +00:00
if ( push_to_buffer ( htrd , & htrd - > fed . b . tra , req , ptr - req ) < = - 1 | |
2021-07-22 07:30:20 +00:00
push_to_buffer ( htrd , & htrd - > fed . b . tra , & NUL , 1 ) < = - 1 ) return HIO_NULL ;
2020-05-17 18:09:19 +00:00
2021-07-22 07:30:20 +00:00
p = HIO_BECS_PTR ( & htrd - > fed . b . tra ) ;
2020-05-17 18:09:19 +00:00
2020-03-03 13:47:51 +00:00
do
{
while ( is_whspace_octet ( * p ) ) p + + ;
if ( * p = = ' \0 ' ) break ;
/* TODO: return error if protocol is 0.9.
* HTTP / 0.9 must not get headers . . . */
2021-07-22 07:30:20 +00:00
p = parse_header_field ( htrd , p , ( ( htrd - > option & HIO_HTRD_TRAILERS ) ? & htrd - > re . trailers : & htrd - > re . hdrtab ) ) ;
if ( HIO_UNLIKELY ( ! p ) ) return HIO_NULL ;
2020-03-03 13:47:51 +00:00
}
while ( 1 ) ;
htrd - > fed . s . chunk . phase = GET_CHUNK_DONE ;
goto done ;
}
case ' \r ' :
2023-01-11 14:59:41 +00:00
if ( htrd - > fed . s . crlf = = 0 | | htrd - > fed . s . crlf = = 2 )
2020-03-03 13:47:51 +00:00
htrd - > fed . s . crlf + + ;
else htrd - > fed . s . crlf = 1 ;
break ;
default :
/* mark that neither CR nor LF was seen */
htrd - > fed . s . crlf = 0 ;
break ;
}
}
2023-01-11 14:59:41 +00:00
if ( push_to_buffer ( htrd , & htrd - > fed . b . tra , req , ptr - req ) < = - 1 )
2021-07-22 07:30:20 +00:00
return HIO_NULL ;
2020-03-03 13:47:51 +00:00
done :
return ptr ;
}
/* feed the percent encoded string */
2021-07-22 07:30:20 +00:00
int hio_htrd_feed ( hio_htrd_t * htrd , const hio_bch_t * req , hio_oow_t len , hio_oow_t * rem )
2020-03-03 13:47:51 +00:00
{
2021-07-22 07:30:20 +00:00
const hio_bch_t * end = req + len ;
const hio_bch_t * ptr = req ;
2023-01-11 14:59:41 +00:00
#if 0
2020-03-03 13:47:51 +00:00
int header_completed_during_this_feed = 0 ;
2023-01-11 14:59:41 +00:00
# endif
2021-07-22 07:30:20 +00:00
hio_oow_t avail ;
2020-03-03 13:47:51 +00:00
2021-07-22 07:30:20 +00:00
HIO_ASSERT ( htrd - > hio , len > 0 ) ;
2020-03-03 13:47:51 +00:00
if ( htrd - > flags & FEEDING_SUSPENDED )
{
2021-07-22 07:30:20 +00:00
htrd - > errnum = HIO_HTRD_ESUSPENDED ;
2020-03-03 13:47:51 +00:00
return - 1 ;
}
2021-07-22 07:30:20 +00:00
/*if (htrd->option & HIO_HTRD_DUMMY)*/
2020-03-03 13:47:51 +00:00
if ( htrd - > flags & FEEDING_DUMMIFIED )
{
/* treat everything as contents.
* i don ' t care about headers or whatsoever . */
2020-05-15 06:18:49 +00:00
return push_content ( htrd , req , len ) ;
2020-03-03 13:47:51 +00:00
}
/* does this goto drop code maintainability? */
2023-01-11 14:59:41 +00:00
if ( htrd - > fed . s . need > 0 )
2020-03-03 13:47:51 +00:00
{
2023-01-11 14:59:41 +00:00
/* we're in need of as many octets as htrd->fed.s.need
2020-03-03 13:47:51 +00:00
* for contents body . make a proper jump to resume
* content handling */
goto content_resume ;
}
switch ( htrd - > fed . s . chunk . phase )
{
case GET_CHUNK_LEN :
goto dechunk_resume ;
case GET_CHUNK_DATA :
2023-01-11 14:59:41 +00:00
/* this won't be reached as htrd->fed.s.need
2020-03-03 13:47:51 +00:00
* is greater than 0 if GET_CHUNK_DATA is true */
goto content_resume ;
case GET_CHUNK_CRLF :
goto dechunk_crlf ;
case GET_CHUNK_TRAILERS :
goto dechunk_get_trailers ;
}
htrd - > clean = 0 ; /* mark that htrd is in need of some data */
while ( ptr < end )
{
2021-07-22 07:30:20 +00:00
register hio_bch_t b = * ptr + + ;
2020-03-03 13:47:51 +00:00
#if 0
2021-07-22 07:30:20 +00:00
if ( htrd - > option & HIO_HTRD_SKIP_EMPTY_LINES & &
2023-01-11 14:59:41 +00:00
htrd - > fed . s . plen < = 0 & & is_whspace_octet ( b ) )
2020-03-03 13:47:51 +00:00
{
/* let's drop leading whitespaces across multiple
* lines */
req + + ;
continue ;
}
# endif
switch ( b )
{
case ' \0 ' :
/* guarantee that the request does not contain
* a null character */
2021-07-22 07:30:20 +00:00
htrd - > errnum = HIO_HTRD_EBADRE ;
2020-03-03 13:47:51 +00:00
return - 1 ;
case ' \n ' :
{
2023-01-11 14:59:41 +00:00
if ( htrd - > fed . s . crlf < = 1 )
2020-03-03 13:47:51 +00:00
{
/* htrd->fed.s.crlf == 0
* = > CR was not seen
* htrd - > fed . s . crlf = = 1
2023-01-11 14:59:41 +00:00
* = > CR was seen
* whatever the current case is ,
2020-03-03 13:47:51 +00:00
* mark the first LF is seen here .
*/
htrd - > fed . s . crlf = 2 ;
}
else
{
/* htrd->fed.s.crlf == 2
* = > no 2 nd CR before LF
* htrd - > fed . s . crlf = = 3
* = > 2 nd CR before LF
*/
/* we got a complete request header. */
2021-07-22 07:30:20 +00:00
HIO_ASSERT ( htrd - > hio , htrd - > fed . s . crlf < = 3 ) ;
2020-05-15 06:18:49 +00:00
2020-03-03 13:47:51 +00:00
/* reset the crlf state */
htrd - > fed . s . crlf = 0 ;
/* reset the raw request length */
htrd - > fed . s . plen = 0 ;
2023-01-11 14:59:41 +00:00
if ( parse_initial_line_and_headers ( htrd , req , ptr - req ) < = - 1 )
2020-07-16 10:46:17 +00:00
{
return - 1 ;
}
2020-03-03 13:47:51 +00:00
2023-01-11 14:59:41 +00:00
#if 0
2020-03-03 13:47:51 +00:00
/* compelete request header is received */
header_completed_during_this_feed = 1 ;
2023-01-11 14:59:41 +00:00
# endif
2020-05-16 19:52:50 +00:00
////////////////////////////////////////////////////////////////////////////////////////////////////
2020-05-17 18:09:19 +00:00
if ( htrd - > recbs . peek ( htrd , & htrd - > re ) < = - 1 )
2020-05-16 19:52:50 +00:00
{
2023-01-11 14:59:41 +00:00
/* need to clear request on error?
2020-05-16 19:52:50 +00:00
clear_feed ( htrd ) ; */
return - 1 ;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
2020-03-03 13:47:51 +00:00
/* carry on processing content body fed together with the header */
2021-07-22 07:30:20 +00:00
if ( htrd - > re . flags & HIO_HTRE_ATTR_CHUNKED )
2020-03-03 13:47:51 +00:00
{
/* transfer-encoding: chunked */
2021-07-22 07:30:20 +00:00
/*HIO_ASSERT (htrd->hio, !(htrd->re.flags & HIO_HTRE_ATTR_LENGTH)); <- this assertion is wrong. non-conforming client may include content-length while transfer-encoding is chunked*/
2020-03-03 13:47:51 +00:00
dechunk_start :
htrd - > fed . s . chunk . phase = GET_CHUNK_LEN ;
htrd - > fed . s . chunk . len = 0 ;
htrd - > fed . s . chunk . count = 0 ;
dechunk_resume :
2020-05-15 06:18:49 +00:00
ptr = getchunklen ( htrd , ptr , end - ptr ) ;
2021-07-22 07:30:20 +00:00
if ( HIO_UNLIKELY ( ! ptr ) ) return - 1 ;
2020-03-03 13:47:51 +00:00
if ( htrd - > fed . s . chunk . phase = = GET_CHUNK_LEN )
{
/* still in the GET_CHUNK_LEN state.
* the length has been partially read . */
goto feedme_more ;
}
else if ( htrd - > fed . s . chunk . phase = = GET_CHUNK_TRAILERS )
{
/* this state is reached after the
* last chunk length 0 is read . The next
2023-01-11 14:59:41 +00:00
* empty line immediately completes
2020-03-03 13:47:51 +00:00
* a content body . so i need to adjust
* this crlf status to 2 as if a trailing
* header line has been read . */
htrd - > fed . s . crlf = 2 ;
dechunk_get_trailers :
2020-05-15 06:18:49 +00:00
ptr = get_trailing_headers ( htrd , ptr , end ) ;
2021-07-22 07:30:20 +00:00
if ( ! HIO_UNLIKELY ( ptr ) ) return - 1 ;
2020-03-03 13:47:51 +00:00
if ( htrd - > fed . s . chunk . phase = = GET_CHUNK_TRAILERS )
{
/* still in the same state.
* the trailers have not been processed fully */
goto feedme_more ;
}
}
}
else
{
/* we need to read as many octets as
* Content - Length */
2023-01-11 14:59:41 +00:00
if ( ( htrd - > option & HIO_HTRD_RESPONSE ) & &
2021-07-22 07:30:20 +00:00
! ( htrd - > re . flags & HIO_HTRE_ATTR_LENGTH ) & &
! ( htrd - > re . flags & HIO_HTRE_ATTR_KEEPALIVE ) )
2020-03-03 13:47:51 +00:00
{
2023-01-11 14:59:41 +00:00
/* for a response, no content-length and
* no chunk are specified and ' connection '
* is to close . i must read until the
* connection is closed . however , there isn ' t
* any good way to know when to stop from
2020-03-03 13:47:51 +00:00
* within this function . so the caller
2021-07-22 07:30:20 +00:00
* can call hio_htrd_halt ( ) for this . */
2020-03-03 13:47:51 +00:00
/* set this to the maximum in a type safe way
* assuming it ' s unsigned . the problem of
2023-01-11 14:59:41 +00:00
* the current implementation is that
2020-03-03 13:47:51 +00:00
* it can ' t receive more than */
htrd - > fed . s . need = 0 ;
2023-01-11 14:59:41 +00:00
htrd - > fed . s . need = ~ htrd - > fed . s . need ;
2020-03-03 13:47:51 +00:00
htrd - > fed . s . flags | = CONSUME_UNTIL_CLOSE ;
}
2021-07-22 07:30:20 +00:00
else if ( ( htrd - > option & HIO_HTRD_RESPONSE ) & &
! ( htrd - > re . flags & HIO_HTRE_ATTR_LENGTH ) & &
( htrd - > re . flags & HIO_HTRE_ATTR_KEEPALIVE ) )
2020-03-03 13:47:51 +00:00
{
2023-01-11 14:59:41 +00:00
/*
* what the hell !
2020-03-03 13:47:51 +00:00
* no content - length , but keep - alive and not chunked .
* there ' s no way to know how large the contents is .
*
* For a request ' http : //php.net/manual/en/function.curl-strerror.php' containing the following header fields:
* If - Modified - Since : Fri , 31 Oct 2014 11 : 12 : 47 GMT
* Accept - Encoding : gzip , deflate
*
* the service gave this response as of this writing :
2023-01-11 14:59:41 +00:00
*
2020-03-03 13:47:51 +00:00
HTTP / 1.1 304 Not Modified
Server : nginx / 1.6 .2
Date : Tue , 04 Nov 2014 15 : 45 : 46 GMT
Connection : keep - alive
Vary : Accept - Encoding
Set - Cookie : LAST_LANG = en ; expires = Wed , 04 - Nov - 2015 15 : 45 : 46 GMT ; Max - Age = 31536000 ; path = / ; domain = . php . net
Set - Cookie : COUNTRY = KOR % 2 C220 .121 .110 .171 ; expires = Tue , 11 - Nov - 2014 15 : 45 : 46 GMT ; Max - Age = 604800 ; path = / ; domain = . php . net
XXXXXXXX
2023-01-11 14:59:41 +00:00
*
2020-03-03 13:47:51 +00:00
* XXXXXXX is some compressed garbage included in the contents - body .
* why does the service behave this way ? is it a server bug or am i doing anything wrong ?
*
* < < WORKAROUND > >
* i decided to drop whatever trailing data avaiable
* after the header fields for this feeding .
* if more contents are fed in later , it will still
* end up with a bad request error . */
ptr = end ;
htrd - > fed . s . need = 0 ;
}
else
{
htrd - > fed . s . need = htrd - > re . attr . content_length ;
}
}
if ( htrd - > fed . s . need > 0 )
{
2023-01-11 14:59:41 +00:00
/* content-length or chunked data length
2020-03-03 13:47:51 +00:00
* specified */
content_resume :
avail = end - ptr ;
if ( avail < = 0 )
{
/* we didn't get a complete content yet */
/* avail can be 0 if data fed ends with
2023-01-11 14:59:41 +00:00
* a chunk length withtout actual data .
2020-03-03 13:47:51 +00:00
* so i check if avail is greater than 0
* in order not to push empty content . */
2023-01-11 14:59:41 +00:00
goto feedme_more ;
2020-03-03 13:47:51 +00:00
}
else if ( avail < htrd - > fed . s . need )
{
/* the data is not as large as needed */
2023-01-11 14:59:41 +00:00
if ( push_content ( htrd , ptr , avail ) < = - 1 )
2020-07-16 10:46:17 +00:00
{
return - 1 ;
}
2020-03-03 13:47:51 +00:00
2023-01-11 14:59:41 +00:00
if ( ! ( htrd - > fed . s . flags & CONSUME_UNTIL_CLOSE ) )
2020-03-03 13:47:51 +00:00
{
/* i don't decrement htrd->fed.s.need
* if i should read until connection is closed .
* well , unless set your own callback ,
2023-01-11 14:59:41 +00:00
* push_content ( ) above will fail
2020-03-03 13:47:51 +00:00
* if too much has been received already */
htrd - > fed . s . need - = avail ;
}
/* we didn't get a complete content yet */
2023-01-11 14:59:41 +00:00
goto feedme_more ;
2020-03-03 13:47:51 +00:00
}
2023-01-11 14:59:41 +00:00
else
2020-03-03 13:47:51 +00:00
{
/* we got all or more than needed */
2023-01-11 14:59:41 +00:00
if ( push_content ( htrd , ptr , htrd - > fed . s . need ) < = - 1 )
2020-07-16 10:46:17 +00:00
{
return - 1 ;
}
2020-03-03 13:47:51 +00:00
ptr + = htrd - > fed . s . need ;
2023-01-11 14:59:41 +00:00
if ( ! ( htrd - > fed . s . flags & CONSUME_UNTIL_CLOSE ) )
2020-03-03 13:47:51 +00:00
htrd - > fed . s . need = 0 ;
}
}
if ( htrd - > fed . s . chunk . phase = = GET_CHUNK_DATA )
{
2021-07-22 07:30:20 +00:00
HIO_ASSERT ( htrd - > hio , htrd - > fed . s . need = = 0 ) ;
2020-03-03 13:47:51 +00:00
htrd - > fed . s . chunk . phase = GET_CHUNK_CRLF ;
dechunk_crlf :
while ( ptr < end & & is_space_octet ( * ptr ) ) ptr + + ;
if ( ptr < end )
{
2023-01-11 14:59:41 +00:00
if ( * ptr = = ' \n ' )
2020-03-03 13:47:51 +00:00
{
/* end of chunk data. */
ptr + + ;
2023-01-11 14:59:41 +00:00
/* more octets still available.
* let it decode the next chunk
2020-03-03 13:47:51 +00:00
*/
2023-01-11 14:59:41 +00:00
if ( ptr < end ) goto dechunk_start ;
2020-03-03 13:47:51 +00:00
2023-01-11 14:59:41 +00:00
/* no more octets available after
2020-03-03 13:47:51 +00:00
* chunk data . the chunk state variables
* need to be reset when a jump is made
* to dechunk_resume upon the next call
*/
htrd - > fed . s . chunk . phase = GET_CHUNK_LEN ;
htrd - > fed . s . chunk . len = 0 ;
htrd - > fed . s . chunk . count = 0 ;
goto feedme_more ;
}
else
{
/* redundant character ... */
2021-07-22 07:30:20 +00:00
htrd - > errnum = HIO_HTRD_EBADRE ;
2020-03-03 13:47:51 +00:00
return - 1 ;
}
}
else
{
/* data not enough */
goto feedme_more ;
}
}
/* the content has been received fully */
2021-07-22 07:30:20 +00:00
hio_htre_completecontent ( & htrd - > re ) ;
2020-03-03 13:47:51 +00:00
2020-05-16 19:52:50 +00:00
#if 0 // XXXX
2020-05-17 18:09:19 +00:00
if ( header_completed_during_this_feed & & htrd - > recbs . peek )
2020-03-03 13:47:51 +00:00
{
/* the peek handler has not been executed.
* this can happen if this function is fed with
* at least the ending part of a complete header
2023-01-11 14:59:41 +00:00
* plus complete content body and the header
2020-03-03 13:47:51 +00:00
* of the next request . */
int n ;
2020-05-17 18:09:19 +00:00
n = htrd - > recbs . peek ( htrd , & htrd - > re ) ;
2020-03-03 13:47:51 +00:00
if ( n < = - 1 )
{
2023-01-11 14:59:41 +00:00
/* need to clear request on error?
2020-03-03 13:47:51 +00:00
clear_feed ( htrd ) ; */
return - 1 ;
}
header_completed_during_this_feed = 0 ;
}
2020-05-16 19:52:50 +00:00
# endif
2020-03-03 13:47:51 +00:00
2020-05-17 18:09:19 +00:00
if ( htrd - > recbs . poke )
2020-03-03 13:47:51 +00:00
{
int n ;
2020-05-17 18:09:19 +00:00
n = htrd - > recbs . poke ( htrd , & htrd - > re ) ;
2020-03-03 13:47:51 +00:00
if ( n < = - 1 )
{
2023-01-11 14:59:41 +00:00
/* need to clear request on error?
2020-03-03 13:47:51 +00:00
clear_feed ( htrd ) ; */
return - 1 ;
}
}
#if 0
2023-01-11 14:59:41 +00:00
hio_printf ( HIO_T ( " CONTENT_LENGTH %d, RAW HEADER LENGTH %d \n " ) ,
2021-07-22 07:30:20 +00:00
( int ) HIO_BECS_LEN ( & htrd - > re . content ) ,
( int ) HIO_BECS_LEN ( & htrd - > fed . b . raw ) ) ;
2020-03-03 13:47:51 +00:00
# endif
clear_feed ( htrd ) ;
2023-01-11 14:59:41 +00:00
if ( rem )
2020-05-15 06:18:49 +00:00
{
/* stop even if there are fed data left */
* rem = end - ptr ; /* remaining feeds */
return 0 ; /* to indicate completed when the 'rem' is not NULL */
}
else if ( ptr > = end )
{
/* no more feeds to handle */
return 0 ;
}
2021-07-22 07:30:20 +00:00
if ( htrd - > flags & FEEDING_SUSPENDED ) /* in case the callback called hio_htrd_suspend() */
2020-03-03 13:47:51 +00:00
{
2021-07-22 07:30:20 +00:00
htrd - > errnum = HIO_HTRD_ESUSPENDED ;
2020-03-03 13:47:51 +00:00
return - 1 ;
}
2021-07-22 07:30:20 +00:00
/*if (htrd->option & HIO_HTRD_DUMMY)*/
if ( htrd - > flags & FEEDING_DUMMIFIED ) /* in case the callback called hio_htrd_dummify() */
2020-03-03 13:47:51 +00:00
{
/* once the mode changes to RAW in a callback,
* left - over is pushed as contents */
if ( ptr < end )
2020-05-15 06:18:49 +00:00
return push_content ( htrd , ptr , end - ptr ) ;
2020-03-03 13:47:51 +00:00
else
return 0 ;
}
2023-01-11 14:59:41 +00:00
/* let ptr point to the next character to LF or
2020-03-03 13:47:51 +00:00
* the optional contents */
2023-01-11 14:59:41 +00:00
req = ptr ;
2020-03-03 13:47:51 +00:00
/* since there are more to handle, i mark that
* htrd is in need of some data . this may
2023-01-11 14:59:41 +00:00
* not be really compatible with SKIP_EMPTY_LINES .
2020-03-03 13:47:51 +00:00
* SHOULD I simply remove the option ? */
2023-01-11 14:59:41 +00:00
htrd - > clean = 0 ;
2020-03-03 13:47:51 +00:00
}
break ;
}
case ' \r ' :
2023-01-11 14:59:41 +00:00
if ( htrd - > fed . s . crlf = = 0 | | htrd - > fed . s . crlf = = 2 )
2020-03-03 13:47:51 +00:00
htrd - > fed . s . crlf + + ;
else htrd - > fed . s . crlf = 1 ;
break ;
default :
2023-01-11 14:59:41 +00:00
/* increment length of a request in raw
2020-03-03 13:47:51 +00:00
* excluding crlf */
2023-01-11 14:59:41 +00:00
htrd - > fed . s . plen + + ;
2020-03-03 13:47:51 +00:00
/* mark that neither CR nor LF was seen */
htrd - > fed . s . crlf = 0 ;
}
}
if ( ptr > req )
{
/* enbuffer the incomplete request */
2023-01-11 14:59:41 +00:00
if ( push_to_buffer ( htrd , & htrd - > fed . b . raw , req , ptr - req ) < = - 1 )
2020-07-16 10:46:17 +00:00
{
return - 1 ;
}
2020-03-03 13:47:51 +00:00
}
feedme_more :
2020-05-16 19:52:50 +00:00
#if 0 //XXXX
2020-05-17 18:09:19 +00:00
if ( header_completed_during_this_feed & & htrd - > recbs . peek )
2020-03-03 13:47:51 +00:00
{
int n ;
2020-05-17 18:09:19 +00:00
n = htrd - > recbs . peek ( htrd , & htrd - > re ) ;
2020-03-03 13:47:51 +00:00
if ( n < = - 1 )
{
2023-01-11 14:59:41 +00:00
/* need to clear request on error?
2020-03-03 13:47:51 +00:00
clear_feed ( htrd ) ; */
return - 1 ;
}
}
2020-05-16 19:52:50 +00:00
# endif
2020-03-03 13:47:51 +00:00
2020-05-15 06:18:49 +00:00
if ( rem ) * rem = 0 ;
2020-03-03 13:47:51 +00:00
return 0 ;
}
2021-07-22 07:30:20 +00:00
int hio_htrd_halt ( hio_htrd_t * htrd )
2020-03-03 13:47:51 +00:00
{
2020-05-15 06:18:49 +00:00
if ( ( htrd - > fed . s . flags & CONSUME_UNTIL_CLOSE ) | | ! htrd - > clean )
2020-03-03 13:47:51 +00:00
{
2021-07-22 07:30:20 +00:00
hio_htre_completecontent ( & htrd - > re ) ;
2020-03-03 13:47:51 +00:00
2020-05-17 18:09:19 +00:00
if ( htrd - > recbs . poke )
2020-03-03 13:47:51 +00:00
{
int n ;
2020-05-17 18:09:19 +00:00
n = htrd - > recbs . poke ( htrd , & htrd - > re ) ;
2020-03-03 13:47:51 +00:00
if ( n < = - 1 )
{
2023-01-11 14:59:41 +00:00
/* need to clear request on error?
2020-03-03 13:47:51 +00:00
clear_feed ( htrd ) ; */
return - 1 ;
}
}
clear_feed ( htrd ) ;
}
return 0 ;
}
2021-07-22 07:30:20 +00:00
void hio_htrd_suspend ( hio_htrd_t * htrd )
2020-03-03 13:47:51 +00:00
{
htrd - > flags | = FEEDING_SUSPENDED ;
}
2021-07-22 07:30:20 +00:00
void hio_htrd_resume ( hio_htrd_t * htrd )
2020-03-03 13:47:51 +00:00
{
htrd - > flags & = ~ FEEDING_SUSPENDED ;
}
2021-07-22 07:30:20 +00:00
void hio_htrd_dummify ( hio_htrd_t * htrd )
2020-03-03 13:47:51 +00:00
{
htrd - > flags | = FEEDING_DUMMIFIED ;
}
2021-07-22 07:30:20 +00:00
void hio_htrd_undummify ( hio_htrd_t * htrd )
2020-03-03 13:47:51 +00:00
{
htrd - > flags & = ~ FEEDING_DUMMIFIED ;
}