LCOV - code coverage report
Current view: top level - waltz/http - fd_http.h (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 16 16 100.0 %
Date: 2026-06-29 05:51:35 Functions: 4 6 66.7 %

          Line data    Source code
       1             : #ifndef HEADER_fd_src_waltz_http_fd_http_h
       2             : #define HEADER_fd_src_waltz_http_fd_http_h
       3             : 
       4             : /* Shared HTTP utilities for servers and clients. */
       5             : 
       6             : #include "../../util/fd_util_base.h"
       7             : 
       8          33 : #define FD_HTTP_PARSE_CONTENT_LEN_OK        ( 0)
       9           9 : #define FD_HTTP_PARSE_CONTENT_LEN_OVERFLOW  (-1)
      10           9 : #define FD_HTTP_PARSE_CONTENT_LEN_MALFORMED (-2)
      11             : 
      12             : FD_PROTOTYPES_BEGIN
      13             : 
      14             : /* fd_http_parse_content_len parses an HTTP Content-Length header
      15             :    value from the buffer s[0,s_len) into *out.
      16             : 
      17             :    Every byte in the range must be an ASCII decimal digit ('0'-'9')
      18             :    and the resulting value must fit in a ulong.  This implies the
      19             :    buffer should not be null-terminated.
      20             : 
      21             :    Returns FD_HTTP_PARSE_CONTENT_LEN_OK (0) on success (*out is
      22             :    populated), FD_HTTP_PARSE_CONTENT_LEN_OVERFLOW (-1) if all
      23             :    bytes are digits but the value exceeds ULONG_MAX (*out is not
      24             :    modified), or FD_HTTP_PARSE_CONTENT_LEN_MALFORMED (-2) on
      25             :    empty input or a non-digit byte (*out is not modified). */
      26             : 
      27             : static inline int
      28             : fd_http_parse_content_len( char const * s,
      29             :                            ulong        s_len,
      30          48 :                            ulong *      out ) {
      31          48 :   if( FD_UNLIKELY( !s_len ) ) return FD_HTTP_PARSE_CONTENT_LEN_MALFORMED;
      32          45 :   ulong val = 0UL;
      33         303 :   for( ulong i=0UL; i<s_len; i++ ) {
      34         270 :     char c = s[i];
      35         270 :     if( FD_UNLIKELY( (c<'0') | (c>'9') ) ) return FD_HTTP_PARSE_CONTENT_LEN_MALFORMED;
      36         264 :     ulong digit = (ulong)(c-'0');
      37         264 :     if( FD_UNLIKELY( val>(ULONG_MAX-digit)/10UL ) ) return FD_HTTP_PARSE_CONTENT_LEN_OVERFLOW;
      38         258 :     val = val*10UL + digit;
      39         258 :   }
      40          33 :   *out = val;
      41          33 :   return FD_HTTP_PARSE_CONTENT_LEN_OK;
      42          45 : }
      43             : 
      44             : FD_PROTOTYPES_END
      45             : 
      46             : #endif /* HEADER_fd_src_waltz_http_fd_http_h */

Generated by: LCOV version 1.14