LCOV - code coverage report
Current view: top level - discof/restore/utils - fd_sshttp.c (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 0 648 0.0 %
Date: 2026-11-26 02:16:43 Functions: 0 29 0.0 %

          Line data    Source code
       1             : #define _GNU_SOURCE
       2             : #include "fd_sshttp_private.h"
       3             : #include "fd_ssarchive.h"
       4             : 
       5             : #include "../../../third_party/picohttpparser/picohttpparser.h"
       6             : #include "../../../util/log/fd_log.h"
       7             : #include "../../../util/fd_util.h"
       8             : #include "../../../waltz/http/fd_http.h"
       9             : #include "../../../waltz/http/fd_url.h"
      10             : #include "../../../ballet/ed25519/fd_x25519.h"
      11             : 
      12             : FD_STATIC_ASSERT( FD_HASH_FOOTPRINT==32UL, resolved_hash_sz );
      13             : 
      14             : #include <unistd.h>
      15             : #include <errno.h>
      16             : #include <stdlib.h>
      17             : 
      18             : #include <sys/epoll.h>
      19             : #include <sys/socket.h>
      20             : #include <sys/uio.h>
      21             : #include <sys/random.h>
      22             : #include <netinet/in.h>
      23             : 
      24             : _Bool fd_sshttp_fuzz = 0;
      25             : 
      26             : static void
      27           0 : fd_sshttp_tls_init( fd_tls_t * tls, fd_sshttp_t * http ) {
      28           0 :   fd_memset( tls, 0, sizeof(fd_tls_t) );
      29             : 
      30             :   /* Seed the CSPRNG that fd_tls draws handshake randomness from */
      31             : 
      32           0 :   uchar rng_key[ FD_CHACHA_KEY_SZ ];
      33           0 :   if( FD_UNLIKELY( !fd_rng_secure( rng_key, sizeof(rng_key) ) ) ) FD_LOG_CRIT(( "fd_rng_secure failed" ));
      34           0 :   fd_chacha_rng_init( http->rng, rng_key, FD_CHACHA_RNG_ALGO_CHACHA8 );
      35           0 :   fd_memzero_explicit( rng_key, sizeof(rng_key) );
      36           0 :   tls->rng = http->rng;
      37             : 
      38           0 :   static uchar const alpn[] = { 8, 'h', 't', 't', 'p', '/', '1', '.', '1' };
      39           0 :   fd_memcpy( tls->alpn, alpn, sizeof(alpn) );
      40           0 :   tls->alpn_sz = sizeof(alpn);
      41             : 
      42           0 :   tls->quic = 0;
      43             : 
      44           0 :   tls->ca_store = &http->ca_store;
      45           0 : }
      46             : 
      47             : static int
      48           0 : http_init_tls( fd_sshttp_t * http ) {
      49           0 :   ulong hostname_len = strlen( http->hostname );
      50           0 :   if( FD_UNLIKELY( hostname_len >= sizeof(http->tls.server_name) ) ) {
      51           0 :     FD_LOG_WARNING(( "hostname too long for SNI: %s", http->hostname ));
      52           0 :     return -1;
      53           0 :   }
      54           0 :   fd_memcpy( http->tls.server_name, http->hostname, hostname_len );
      55           0 :   http->tls.server_name[ hostname_len ] = '\0';
      56           0 :   http->tls.server_name_len = (ushort)hostname_len;
      57             : 
      58             :   /* Generate a fresh ephemeral X25519 key for this handshake */
      59             : 
      60           0 :   if( FD_UNLIKELY( !fd_rng_secure( http->tls.kex_private_key, 32UL ) ) ) FD_LOG_CRIT(( "fd_rng_secure failed" ));
      61           0 :   fd_x25519_public( http->tls.kex_public_key, http->tls.kex_private_key );
      62             : 
      63           0 :   fd_tlsrec_conn_init( &http->tls_conn, &http->tls, 0 );
      64           0 :   fd_tlsrec_sock_init( http->tls_sock );
      65             : 
      66           0 :   return 0;
      67           0 : }
      68             : 
      69             : static int
      70             : http_connect_tls( fd_sshttp_t * http,
      71           0 :                   long          now ) {
      72           0 :   if( FD_UNLIKELY( now > http->deadline ) ) {
      73           0 :     FD_LOG_WARNING(( "TLS handshake timeout" ));
      74           0 :     fd_sshttp_cancel( http );
      75           0 :     return FD_SSHTTP_ADVANCE_ERROR;
      76           0 :   }
      77             : 
      78             :   /* Flush any buffered outgoing data (e.g., ClientHello from a
      79             :      previous call where the TCP connect was still in progress). */
      80             : 
      81           0 :   int flush = fd_tlsrec_sock_flush( http->tls_sock, http->sockfd );
      82           0 :   if( flush<0 ) { fd_sshttp_cancel( http ); return FD_SSHTTP_ADVANCE_ERROR; }
      83           0 :   if( flush>0 ) return FD_SSHTTP_ADVANCE_AGAIN;
      84             : 
      85             :   /* Drive the TLS handshake */
      86             : 
      87           0 :   ulong tcp_rx_sz;
      88           0 :   int err = fd_tlsrec_sock_rx( http->tls_sock, &http->tls_conn, http->sockfd, &tcp_rx_sz );
      89           0 :   if( FD_UNLIKELY( err ) ) {
      90           0 :     if( err==FD_TLSREC_SOCK_ERR_EOF ) {
      91           0 :       FD_LOG_WARNING(( "peer " FD_IP4_ADDR_FMT ":%hu closed the connection during TLS handshake",
      92           0 :                        FD_IP4_ADDR_FMT_ARGS( http->addr.addr ), fd_ushort_bswap( http->addr.port ) ));
      93           0 :     } else {
      94           0 :       FD_LOG_WARNING(( "TLS handshake failed (%d-%s)", err, fd_tlsrec_sock_strerror( err ) ));
      95           0 :     }
      96           0 :     fd_sshttp_cancel( http );
      97           0 :     return FD_SSHTTP_ADVANCE_ERROR;
      98           0 :   }
      99             : 
     100             :   /* Transition to request state once handshake completes */
     101             : 
     102           0 :   if( fd_tlsrec_conn_is_ready( &http->tls_conn ) ) {
     103           0 :     http->state    = FD_SSHTTP_STATE_REQ;
     104           0 :     http->deadline = now + FD_SSHTTP_DEADLINE_NANOS;
     105           0 :     return FD_SSHTTP_ADVANCE_AGAIN;
     106           0 :   }
     107             : 
     108           0 :   if( fd_tlsrec_conn_is_failed( &http->tls_conn ) ) {
     109           0 :     FD_LOG_WARNING(( "TLS handshake failed" ));
     110           0 :     fd_sshttp_cancel( http );
     111           0 :     return FD_SSHTTP_ADVANCE_ERROR;
     112           0 :   }
     113             : 
     114           0 :   return FD_SSHTTP_ADVANCE_AGAIN;
     115           0 : }
     116             : 
     117             : static long
     118             : http_send_tls( fd_sshttp_t * http,
     119             :                void *        buf,
     120           0 :                ulong         bufsz ) {
     121           0 :   int flush = fd_tlsrec_sock_flush( http->tls_sock, http->sockfd );
     122           0 :   if( flush<0 ) return FD_SSHTTP_ADVANCE_ERROR;
     123           0 :   if( flush>0 ) return FD_SSHTTP_ADVANCE_AGAIN;
     124             : 
     125           0 :   ulong consumed;
     126           0 :   if( FD_UNLIKELY( fd_tlsrec_sock_tx( http->tls_sock, &http->tls_conn, http->sockfd, buf, bufsz, &consumed ) ) )
     127           0 :     return FD_SSHTTP_ADVANCE_ERROR;
     128           0 :   return (long)consumed;
     129           0 : }
     130             : 
     131             : static ulong
     132             : tls_rx_pop_iov( fd_sshttp_t *        http,
     133             :                 struct iovec const * iov,
     134           0 :                 ulong                iov_cnt ) {
     135           0 :   ulong n = 0UL;
     136           0 :   for( ulong i=0UL; i<iov_cnt; i++ ) {
     137           0 :     ulong sz = fd_tlsrec_sock_rx_pop( http->tls_sock, iov[i].iov_base, iov[i].iov_len );
     138           0 :     n += sz;
     139           0 :     if( sz<iov[i].iov_len ) break;
     140           0 :   }
     141           0 :   return n;
     142           0 : }
     143             : 
     144             : static long
     145             : http_recv_tls( fd_sshttp_t *        http,
     146             :                struct iovec const * iov,
     147           0 :                ulong                iov_cnt ) {
     148             : 
     149             :   /* Drain any buffered plaintext from a previous call */
     150             : 
     151           0 :   ulong n = tls_rx_pop_iov( http, iov, iov_cnt );
     152           0 :   if( n ) {
     153           0 :     return (long)n;
     154           0 :   }
     155             : 
     156             :   /* Drain pending ciphertext before generating any new records */
     157             : 
     158           0 :   int flush = fd_tlsrec_sock_flush( http->tls_sock, http->sockfd );
     159           0 :   if( flush<0 ) return FD_SSHTTP_ADVANCE_ERROR;
     160           0 :   if( flush>0 ) return FD_SSHTTP_ADVANCE_AGAIN;
     161             : 
     162             :   /* Read and decrypt */
     163             : 
     164           0 :   ulong tcp_rx_sz;
     165           0 :   int err = fd_tlsrec_sock_rx( http->tls_sock, &http->tls_conn, http->sockfd, &tcp_rx_sz );
     166           0 :   if( FD_UNLIKELY( err ) ) {
     167           0 :     if( err==FD_TLSREC_SOCK_ERR_EOF ) {
     168           0 :       FD_LOG_WARNING(( "peer " FD_IP4_ADDR_FMT ":%hu closed the connection mid-response",
     169           0 :                        FD_IP4_ADDR_FMT_ARGS( http->addr.addr ), fd_ushort_bswap( http->addr.port ) ));
     170           0 :     } else if( err==FD_TLSREC_SOCK_ERR_RECV ) {
     171           0 :       FD_LOG_WARNING(( "recv() failed (%d-%s) from " FD_IP4_ADDR_FMT ":%hu", errno, fd_io_strerror( errno ),
     172           0 :                        FD_IP4_ADDR_FMT_ARGS( http->addr.addr ), fd_ushort_bswap( http->addr.port ) ));
     173           0 :     }
     174           0 :     return FD_SSHTTP_ADVANCE_ERROR;
     175           0 :   }
     176           0 :   if( !tcp_rx_sz ) return FD_SSHTTP_ADVANCE_AGAIN;
     177             : 
     178           0 :   n = tls_rx_pop_iov( http, iov, iov_cnt );
     179           0 :   if( !n ) return FD_SSHTTP_ADVANCE_AGAIN;
     180           0 :   return (long)n;
     181           0 : }
     182             : 
     183             : static int
     184             : setup_redirect_tls( fd_sshttp_t * http,
     185           0 :                     long          now ) {
     186           0 :   fd_sshttp_cancel( http );
     187           0 :   if( FD_UNLIKELY( fd_sshttp_init( http, http->addr, http->hostname, http->is_https, http->location, http->location_len, ULONG_MAX, now ) ) ) {
     188           0 :     return FD_SSHTTP_ADVANCE_ERROR;
     189           0 :   }
     190           0 :   return FD_SSHTTP_ADVANCE_AGAIN;
     191           0 : }
     192             : 
     193             : void
     194           0 : fd_sshttp_load_ca_store( fd_sshttp_t * sshttp ) {
     195           0 :   if( FD_UNLIKELY( fd_x509_ca_store_load_system( &sshttp->ca_store )<0L ) ) {
     196           0 :     FD_LOG_WARNING(( "No CA certificate bundle found, HTTPS snapshot sources will be refused" ));
     197           0 :     return;
     198           0 :   }
     199           0 :   sshttp->ca_store_loaded = 1;
     200           0 : }
     201             : 
     202             : FD_FN_CONST ulong
     203           0 : fd_sshttp_align( void ) {
     204           0 :   return alignof(fd_sshttp_t);
     205           0 : }
     206             : 
     207             : FD_FN_CONST ulong
     208           0 : fd_sshttp_footprint( void ) {
     209           0 :   ulong l;
     210           0 :   l = FD_LAYOUT_INIT;
     211           0 :   l = FD_LAYOUT_APPEND( l, alignof(fd_sshttp_t), sizeof(fd_sshttp_t) );
     212           0 :   return FD_LAYOUT_FINI( l, fd_sshttp_align() );
     213           0 : }
     214             : 
     215             : void *
     216             : fd_sshttp_new( void * shmem,
     217           0 :                int    epoll_fd ) {
     218           0 :   if( FD_UNLIKELY( !shmem ) ) {
     219           0 :     FD_LOG_WARNING(( "NULL shmem" ));
     220           0 :     return NULL;
     221           0 :   }
     222             : 
     223           0 :   if( FD_UNLIKELY( !fd_ulong_is_aligned( (ulong)shmem, fd_sshttp_align() ) ) ) {
     224           0 :     FD_LOG_WARNING(( "unaligned shmem" ));
     225           0 :     return NULL;
     226           0 :   }
     227             : 
     228           0 :   if( FD_UNLIKELY( epoll_fd==-1 ) ) {
     229           0 :     FD_LOG_WARNING(( "invalid epoll_fd" ));
     230           0 :     return NULL;
     231           0 :   }
     232             : 
     233           0 :   FD_SCRATCH_ALLOC_INIT( l, shmem );
     234           0 :   fd_sshttp_t * sshttp = FD_SCRATCH_ALLOC_APPEND( l, alignof(fd_sshttp_t), sizeof(fd_sshttp_t) );
     235             : 
     236           0 :   sshttp->state    = FD_SSHTTP_STATE_INIT;
     237           0 :   sshttp->sockfd   = -1;
     238           0 :   sshttp->epoll_fd = epoll_fd;
     239           0 :   sshttp->content_len = 0UL;
     240           0 :   sshttp->rx_lowat    = 0UL;
     241           0 :   sshttp->rx_parked   = 0;
     242           0 :   fd_cstr_fini( sshttp->snapshot_name );
     243           0 :   sshttp->resolved_slot = 0UL;
     244           0 :   fd_memset( sshttp->resolved_hash, 0, FD_HASH_FOOTPRINT );
     245             : 
     246             :   /* Initialize native TLS config */
     247           0 :   fd_tlsrec_sock_init( sshttp->tls_sock );
     248           0 :   fd_sshttp_tls_init( &sshttp->tls, sshttp );
     249             : 
     250             :   /* CA trust store — loaded via fd_sshttp_load_ca_store() during
     251             :      privileged_init, before the seccomp sandbox locks down. */
     252           0 :   sshttp->ca_store_loaded = 0;
     253             : 
     254           0 :   FD_COMPILER_MFENCE();
     255           0 :   sshttp->magic = FD_SSHTTP_MAGIC;
     256           0 :   FD_COMPILER_MFENCE();
     257             : 
     258           0 :   return (void *)sshttp;
     259           0 : }
     260             : 
     261             : fd_sshttp_t *
     262           0 : fd_sshttp_join( void * shhttp ) {
     263           0 :   if( FD_UNLIKELY( !shhttp ) ) {
     264           0 :     FD_LOG_WARNING(( "NULL shhttp" ));
     265           0 :     return NULL;
     266           0 :   }
     267             : 
     268           0 :   if( FD_UNLIKELY( !fd_ulong_is_aligned( (ulong)shhttp, fd_sshttp_align() ) ) ) {
     269           0 :     FD_LOG_WARNING(( "misaligned shhttp" ));
     270           0 :     return NULL;
     271           0 :   }
     272             : 
     273           0 :   fd_sshttp_t * sshttp = (fd_sshttp_t *)shhttp;
     274             : 
     275           0 :   if( FD_UNLIKELY( sshttp->magic!=FD_SSHTTP_MAGIC ) ) {
     276           0 :     FD_LOG_WARNING(( "bad magic" ));
     277           0 :     return NULL;
     278           0 :   }
     279             : 
     280           0 :   return sshttp;
     281           0 : }
     282             : 
     283             : /* http_init_ssl removed — replaced by http_init_tls (native fd_tls) */
     284             : 
     285             : int
     286             : fd_sshttp_init( fd_sshttp_t * http,
     287             :                 fd_ip4_port_t addr,
     288             :                 char const *  hostname,
     289             :                 int           is_https,
     290             :                 char const *  path,
     291             :                 ulong         path_len,
     292             :                 ulong         hops,
     293           0 :                 long          now ) {
     294           0 :   FD_TEST( http->state==FD_SSHTTP_STATE_INIT );
     295             : 
     296           0 :   ulong hostname_len = strnlen( hostname, sizeof(http->hostname) );
     297           0 :   if( FD_UNLIKELY( hostname_len==sizeof(http->hostname) ) ) {
     298           0 :     FD_LOG_WARNING(( "snapshot hostname too long" ));
     299           0 :     return -1;
     300           0 :   }
     301             :   /* Redirects reinitialize using our own hostname buffer. */
     302           0 :   memmove( http->hostname, hostname, hostname_len+1UL );
     303           0 :   http->is_https = is_https;
     304             : 
     305           0 :   if( FD_LIKELY( is_https ) ) {
     306           0 :     if( FD_UNLIKELY( !http->ca_store_loaded ) ) {
     307           0 :       FD_LOG_WARNING(( "Refusing HTTPS snapshot download from %s: no CA trust store loaded", hostname ));
     308           0 :       return -1;
     309           0 :     }
     310           0 :     if( FD_UNLIKELY( http_init_tls( http ) ) ) return -1;
     311           0 :   }
     312             : 
     313           0 :   if( hops!=ULONG_MAX ) {
     314           0 :     http->hops = hops;
     315           0 :     fd_cstr_fini( http->snapshot_name );
     316           0 :     http->resolved_slot = 0UL;
     317           0 :     fd_memset( http->resolved_hash, 0, FD_HASH_FOOTPRINT );
     318           0 :   }
     319           0 :   http->request_sent = 0UL;
     320           0 :   int fmt_ok;
     321           0 :   if( FD_LIKELY( hostname[0] ) ) {
     322           0 :     fmt_ok = fd_cstr_printf_check( http->request, sizeof(http->request), &http->request_len,
     323           0 :       "GET %.*s HTTP/1.1\r\n"
     324           0 :       "User-Agent: Firedancer\r\n"
     325           0 :       "Accept: */*\r\n"
     326           0 :       "Accept-Encoding: identity\r\n"
     327           0 :       "Host: %s:%u\r\n\r\n",
     328           0 :       (int)path_len, path, hostname, fd_ushort_bswap( addr.port ) );
     329           0 :   } else {
     330           0 :     fmt_ok = fd_cstr_printf_check( http->request, sizeof(http->request), &http->request_len,
     331           0 :       "GET %.*s HTTP/1.1\r\n"
     332           0 :       "User-Agent: Firedancer\r\n"
     333           0 :       "Accept: */*\r\n"
     334           0 :       "Accept-Encoding: identity\r\n"
     335           0 :       "Host: " FD_IP4_ADDR_FMT ":%u\r\n\r\n",
     336           0 :       (int)path_len, path, FD_IP4_ADDR_FMT_ARGS( addr.addr ), fd_ushort_bswap( addr.port ) );
     337           0 :   }
     338           0 :   if( FD_UNLIKELY( !fmt_ok ) ) {
     339           0 :     FD_LOG_WARNING(( "HTTP request too long for %.*s", (int)path_len, path ));
     340           0 :     return -1;
     341           0 :   }
     342             : 
     343           0 :   http->response_len = 0UL;
     344           0 :   http->content_len  = 0UL;
     345           0 :   http->content_read = 0UL;
     346             : 
     347           0 :   http->addr   = addr;
     348           0 :   http->sockfd = socket( AF_INET, SOCK_STREAM|SOCK_NONBLOCK, 0 );
     349           0 :   if( FD_UNLIKELY( -1==http->sockfd ) ) {
     350           0 :     FD_LOG_WARNING(( "socket() failed (%d-%s) for " FD_IP4_ADDR_FMT ":%hu", errno, fd_io_strerror( errno ),
     351           0 :                      FD_IP4_ADDR_FMT_ARGS( http->addr.addr ), fd_ushort_bswap( http->addr.port ) ));
     352           0 :     return -1;
     353           0 :   }
     354             : 
     355           0 :   int so_rcvbuf = (int)FD_SSHTTP_SO_RCVBUF;
     356           0 :   if( FD_UNLIKELY( -1==setsockopt( http->sockfd, SOL_SOCKET, SO_RCVBUF, &so_rcvbuf, sizeof(int) ) ) ) {
     357           0 :     FD_LOG_ERR(( "setsockopt(SOL_SOCKET,SO_RCVBUF,%i) failed (%i-%s)", so_rcvbuf, errno, fd_io_strerror( errno ) ));
     358           0 :   }
     359             : 
     360           0 :   struct sockaddr_in addr_in = {
     361           0 :     .sin_family = AF_INET,
     362           0 :     .sin_port   = addr.port,
     363           0 :     .sin_addr   = { .s_addr = addr.addr }
     364           0 :   };
     365             : 
     366           0 :   if( FD_LIKELY( -1==connect( http->sockfd, fd_type_pun_const( &addr_in ), sizeof(addr_in) ) ) ) {
     367           0 :     if( FD_UNLIKELY( errno!=EINPROGRESS ) ) {
     368           0 :       FD_LOG_WARNING(( "connect() failed (%d-%s) to " FD_IP4_ADDR_FMT ":%hu", errno, fd_io_strerror( errno ),
     369           0 :                        FD_IP4_ADDR_FMT_ARGS( http->addr.addr ), fd_ushort_bswap( http->addr.port ) ));
     370           0 :       if( FD_UNLIKELY( -1==close( http->sockfd ) ) ) FD_LOG_ERR(( "close() failed (%d-%s) for " FD_IP4_ADDR_FMT ":%hu", errno, fd_io_strerror( errno ),
     371           0 :                                                                   FD_IP4_ADDR_FMT_ARGS( http->addr.addr ), fd_ushort_bswap( http->addr.port ) ));
     372           0 :       http->sockfd = -1;
     373           0 :       return -1;
     374           0 :     }
     375           0 :   }
     376             : 
     377           0 :   http->epoll_events = EPOLLIN|EPOLLOUT;
     378           0 :   struct epoll_event ev = { .events = http->epoll_events, .data.fd = http->sockfd };
     379           0 :   if( FD_UNLIKELY( -1==epoll_ctl( http->epoll_fd, EPOLL_CTL_ADD, http->sockfd, &ev ) ) ) FD_LOG_ERR(( "epoll_ctl(ADD) failed (%d-%s)", errno, fd_io_strerror( errno ) ));
     380             : 
     381           0 :   if( FD_LIKELY( is_https ) ) {
     382           0 :     http->state    = FD_SSHTTP_STATE_CONNECT;
     383           0 :     http->deadline = now + FD_SSHTTP_DEADLINE_NANOS;
     384           0 :   } else {
     385           0 :     http->state    = FD_SSHTTP_STATE_REQ;
     386           0 :     http->deadline = now + FD_SSHTTP_DEADLINE_NANOS;
     387           0 :   }
     388             : 
     389           0 :   return 0;
     390           0 : }
     391             : 
     392             : long
     393           0 : fd_sshttp_deadline( fd_sshttp_t const * http ) {
     394           0 :   switch( http->state ) {
     395           0 :     case FD_SSHTTP_STATE_INIT:     return LONG_MAX;
     396           0 :     case FD_SSHTTP_STATE_REDIRECT:
     397           0 :     case FD_SSHTTP_STATE_DONE:     return 0L;       /* non-I/O work pending */
     398           0 :     case FD_SSHTTP_STATE_DL:
     399             :       /* Body bytes already buffered (response residual or decrypted TLS
     400             :          plaintext) don't keep the socket readable: run now.  Otherwise
     401             :          the caller bounds download progress. */
     402           0 :       if( http->response_len || (http->is_https && fd_tlsrec_sock_rx_avail( http->tls_sock )) ) return 0L;
     403           0 :       return LONG_MAX;
     404           0 :     default:                       return http->deadline;
     405           0 :   }
     406           0 : }
     407             : 
     408             : void
     409           0 : fd_sshttp_cancel( fd_sshttp_t * http ) {
     410           0 :   if( FD_LIKELY( http->state!=FD_SSHTTP_STATE_INIT && -1!=http->sockfd ) ) {
     411           0 :     if( FD_UNLIKELY( -1==epoll_ctl( http->epoll_fd, EPOLL_CTL_DEL, http->sockfd, NULL ) ) ) FD_LOG_ERR(( "epoll_ctl(DEL) failed (%d-%s)", errno, fd_io_strerror( errno ) ));
     412           0 :     if( FD_UNLIKELY( -1==close( http->sockfd ) ) ) FD_LOG_ERR(( "close() failed (%d-%s) for " FD_IP4_ADDR_FMT ":%hu", errno, fd_io_strerror( errno ),
     413           0 :                                                                 FD_IP4_ADDR_FMT_ARGS( http->addr.addr ), fd_ushort_bswap( http->addr.port ) ));
     414           0 :     http->sockfd = -1;
     415           0 :   }
     416           0 :   http->state = FD_SSHTTP_STATE_INIT;
     417             : 
     418             :   /* Clean up native TLS state */
     419           0 :   fd_tlsrec_conn_fini( &http->tls_conn );
     420           0 :   fd_memzero_explicit( http->tls.kex_private_key, 32UL );
     421           0 :   fd_tlsrec_sock_init( http->tls_sock );
     422           0 : }
     423             : 
     424             : static long
     425             : http_send( fd_sshttp_t * http,
     426             :            void *        buf,
     427           0 :            ulong         bufsz ) {
     428           0 :   if( FD_LIKELY( http->is_https ) )
     429           0 :     return http_send_tls( http, buf, bufsz );
     430             : 
     431           0 :   long sent = sendto( http->sockfd, buf, bufsz, MSG_NOSIGNAL, NULL, 0 );
     432           0 :   if( FD_UNLIKELY( -1==sent && errno==EAGAIN ) ) {
     433           0 :     return FD_SSHTTP_ADVANCE_AGAIN;
     434           0 :   } else if( FD_UNLIKELY( -1==sent ) ) {
     435           0 :     FD_LOG_WARNING(( "sendto() failed (%d-%s) to " FD_IP4_ADDR_FMT ":%hu", errno, fd_io_strerror( errno ),
     436           0 :                      FD_IP4_ADDR_FMT_ARGS( http->addr.addr ), fd_ushort_bswap( http->addr.port ) ));
     437           0 :     fd_sshttp_cancel( http );
     438           0 :     return FD_SSHTTP_ADVANCE_ERROR;
     439           0 :   }
     440             : 
     441           0 :   return sent;
     442           0 : }
     443             : 
     444             : /* http_recv receives at most max bytes scattered into iov. */
     445             : 
     446             : static long
     447             : http_recv( fd_sshttp_t *        http,
     448             :            struct iovec const * iov,
     449             :            ulong                iov_cnt,
     450           0 :            ulong                max ) {
     451           0 :   struct iovec clamped[ FD_SSHTTP_IOV_MAX ];
     452           0 :   ulong cnt = 0UL;
     453           0 :   for( ; cnt<iov_cnt && max; cnt++ ) {
     454           0 :     ulong len = fd_ulong_min( iov[ cnt ].iov_len, max );
     455           0 :     clamped[ cnt ].iov_base = iov[ cnt ].iov_base;
     456           0 :     clamped[ cnt ].iov_len  = len;
     457           0 :     max -= len;
     458           0 :   }
     459           0 :   if( FD_UNLIKELY( !cnt ) ) return FD_SSHTTP_ADVANCE_AGAIN;
     460             : 
     461           0 :   if( FD_LIKELY( http->is_https ) )
     462           0 :     return http_recv_tls( http, clamped, cnt );
     463             : 
     464           0 :   long read = readv( http->sockfd, clamped, (int)cnt );
     465           0 :   if( FD_UNLIKELY( -1==read && errno==EAGAIN ) ) {
     466           0 :     return FD_SSHTTP_ADVANCE_AGAIN;
     467           0 :   } else if( FD_UNLIKELY( -1==read ) ) {
     468           0 :     FD_LOG_WARNING(( "readv() failed (%d-%s) from " FD_IP4_ADDR_FMT ":%hu", errno, fd_io_strerror( errno ),
     469           0 :                      FD_IP4_ADDR_FMT_ARGS( http->addr.addr ), fd_ushort_bswap( http->addr.port ) ));
     470           0 :     fd_sshttp_cancel( http );
     471           0 :     return FD_SSHTTP_ADVANCE_ERROR;
     472           0 :   } else if( FD_UNLIKELY( !read ) ) {
     473           0 :     FD_LOG_WARNING(( "peer " FD_IP4_ADDR_FMT ":%hu closed the connection mid-response",
     474           0 :                      FD_IP4_ADDR_FMT_ARGS( http->addr.addr ), fd_ushort_bswap( http->addr.port ) ));
     475           0 :     fd_sshttp_cancel( http );
     476           0 :     return FD_SSHTTP_ADVANCE_ERROR;
     477           0 :   }
     478             : 
     479           0 :   return read;
     480           0 : }
     481             : 
     482             : static int
     483             : send_request( fd_sshttp_t * http,
     484           0 :               long          now ) {
     485           0 :   if( FD_UNLIKELY( now>http->deadline ) ) {
     486           0 :     FD_LOG_WARNING(( "timeout sending request to " FD_IP4_ADDR_FMT ":%hu",
     487           0 :                      FD_IP4_ADDR_FMT_ARGS( http->addr.addr ), fd_ushort_bswap( http->addr.port ) ));
     488           0 :     fd_sshttp_cancel( http );
     489           0 :     return FD_SSHTTP_ADVANCE_ERROR;
     490           0 :   }
     491             : 
     492           0 :   long sent = http_send( http, http->request+http->request_sent, http->request_len-http->request_sent );
     493           0 :   if( FD_UNLIKELY( sent<=0 ) ) return (int)sent;
     494             : 
     495           0 :   http->request_sent += (ulong)sent;
     496           0 :   if( FD_UNLIKELY( http->request_sent==http->request_len ) ) {
     497           0 :     http->state        = FD_SSHTTP_STATE_RESP;
     498           0 :     http->response_len = 0UL;
     499           0 :     http->deadline     = now + FD_SSHTTP_DEADLINE_NANOS;
     500           0 :   }
     501             : 
     502           0 :   return FD_SSHTTP_ADVANCE_AGAIN;
     503           0 : }
     504             : 
     505             : /* Accepts a full URL redirect to another port on the same host. */
     506             : 
     507             : static char const *
     508             : parse_redirect_url( fd_sshttp_t const * http,
     509             :                     char const *        url,
     510             :                     ulong *             len,
     511           0 :                     ushort *            port ) {
     512           0 :   fd_url_t parsed[ 1 ];
     513           0 :   _Bool    is_ssl;
     514           0 :   if( FD_UNLIKELY( fd_url_parse_endpoint( parsed, url, *len, port, &is_ssl, "redirect location" ) ) ) return NULL;
     515           0 :   if( FD_UNLIKELY( is_ssl!=(_Bool)http->is_https ) ) return NULL;
     516           0 :   if( FD_UNLIKELY( !parsed->tail_len || parsed->tail[ 0 ]!='/' ) ) return NULL;
     517             : 
     518           0 :   char host[ FD_FQDN_BUF_MAX ];
     519           0 :   fd_memcpy( host, parsed->host, parsed->host_len );
     520           0 :   host[ parsed->host_len ] = '\0';
     521           0 :   uint ip4;
     522           0 :   int same_ip   = fd_cstr_to_ip4_addr( host, &ip4 ) && ip4==http->addr.addr;
     523           0 :   int same_name = http->hostname[ 0 ] && !strcasecmp( host, http->hostname );
     524           0 :   if( FD_UNLIKELY( !same_ip && !same_name ) ) return NULL;
     525             : 
     526           0 :   *len = parsed->tail_len;
     527           0 :   return parsed->tail;
     528           0 : }
     529             : 
     530             : static int
     531             : follow_redirect( fd_sshttp_t *        http,
     532             :                   struct phr_header * headers,
     533             :                   ulong               header_cnt,
     534           0 :                   long                now ) {
     535           0 :   if( FD_UNLIKELY( !http->hops ) ) {
     536           0 :     FD_LOG_WARNING(( "too many redirects (remaining %lu) from " FD_IP4_ADDR_FMT ":%hu", http->hops,
     537           0 :                      FD_IP4_ADDR_FMT_ARGS( http->addr.addr ), fd_ushort_bswap( http->addr.port ) ));
     538           0 :     fd_sshttp_cancel( http );
     539           0 :     return FD_SSHTTP_ADVANCE_ERROR;
     540           0 :   }
     541             :   /* The check above guarantees hops>0. */
     542           0 :   http->hops--;
     543             : 
     544           0 :   ulong        location_len = 0UL;
     545           0 :   char const * location     = NULL;
     546           0 :   ushort       port         = fd_ushort_bswap( http->addr.port );
     547             : 
     548           0 :   for( ulong i=0UL; i<header_cnt; i++ ) {
     549           0 :     if( FD_UNLIKELY( headers[ i ].name_len == 8 && !strncasecmp( headers[ i ].name, "location", headers[ i ].name_len ) ) ) {
     550           0 :       location_len = headers[ i ].value_len;
     551           0 :       location     = headers[ i ].value;
     552           0 :       if( location_len && location[ 0 ]!='/' ) location = parse_redirect_url( http, location, &location_len, &port );
     553           0 :       if( FD_UNLIKELY( !location || !location_len ) ) {
     554           0 :         FD_LOG_WARNING(( "invalid location header `%.*s` from " FD_IP4_ADDR_FMT ":%hu", (int)headers[ i ].value_len, headers[ i ].value,
     555           0 :                          FD_IP4_ADDR_FMT_ARGS( http->addr.addr ), fd_ushort_bswap( http->addr.port ) ));
     556           0 :         fd_sshttp_cancel( http );
     557           0 :         return FD_SSHTTP_ADVANCE_ERROR;
     558           0 :       }
     559             : 
     560           0 :       if( FD_UNLIKELY( location_len>=PATH_MAX-1UL ) ) {
     561           0 :         FD_LOG_WARNING(( "location header too long `%.*s` from " FD_IP4_ADDR_FMT ":%hu", (int)location_len, location,
     562           0 :                          FD_IP4_ADDR_FMT_ARGS( http->addr.addr ), fd_ushort_bswap( http->addr.port ) ));
     563           0 :         fd_sshttp_cancel( http );
     564           0 :         return FD_SSHTTP_ADVANCE_ERROR;
     565           0 :       }
     566             : 
     567             :       /* The rpc tile sends the generic path on to the snapshot server,
     568             :          which then names the file.  Only a file name is resolved. */
     569           0 :       char const * file     = location+1UL;
     570           0 :       ulong        file_len = location_len-1UL;
     571           0 :       int generic = ( file_len==16UL && ( !memcmp( file, "snapshot.tar.bz2", 16UL ) || !memcmp( file, "snapshot.tar.zst", 16UL ) ) ) ||
     572           0 :                     ( file_len==28UL && ( !memcmp( file, "incremental-snapshot.tar.bz2", 28UL ) || !memcmp( file, "incremental-snapshot.tar.zst", 28UL ) ) );
     573           0 :       if( generic ) break;
     574             : 
     575           0 :       char snapshot_name[ PATH_MAX ];
     576           0 :       fd_memcpy( snapshot_name, file, file_len );
     577           0 :       snapshot_name[ file_len ] = '\0';
     578             : 
     579           0 :       int is_zstd;
     580           0 :       ulong full_entry_slot, incremental_entry_slot;
     581           0 :       uchar decoded_hash[ FD_HASH_FOOTPRINT ];
     582           0 :       int err = fd_ssarchive_parse_filename( snapshot_name, &full_entry_slot, &incremental_entry_slot, decoded_hash, &is_zstd );
     583             : 
     584           0 :       if( FD_UNLIKELY( err || !is_zstd ) ) {
     585           0 :         FD_LOG_WARNING(( "unrecognized snapshot file `%s` in redirect location header from " FD_IP4_ADDR_FMT ":%hu", snapshot_name,
     586           0 :                          FD_IP4_ADDR_FMT_ARGS( http->addr.addr ), fd_ushort_bswap( http->addr.port ) ));
     587           0 :         fd_sshttp_cancel( http );
     588           0 :         return FD_SSHTTP_ADVANCE_ERROR;
     589           0 :       }
     590             : 
     591           0 :       http->resolved_slot = (incremental_entry_slot!=ULONG_MAX)
     592           0 :                             ? incremental_entry_slot : full_entry_slot;
     593           0 :       fd_memcpy( http->resolved_hash, decoded_hash, FD_HASH_FOOTPRINT );
     594             : 
     595           0 :       char encoded_hash[ FD_BASE58_ENCODED_32_SZ ];
     596           0 :       fd_base58_encode_32( decoded_hash, NULL, encoded_hash );
     597             : 
     598           0 :       if( FD_LIKELY( incremental_entry_slot!=ULONG_MAX ) ) {
     599           0 :         FD_TEST( fd_cstr_printf_check( http->snapshot_name, PATH_MAX, NULL, "incremental-snapshot-%lu-%lu-%s.tar.zst", full_entry_slot, incremental_entry_slot, encoded_hash ) );
     600           0 :       } else {
     601           0 :         FD_TEST( fd_cstr_printf_check( http->snapshot_name, PATH_MAX, NULL, "snapshot-%lu-%s.tar.zst", full_entry_slot, encoded_hash ) );
     602           0 :       }
     603           0 :       break;
     604           0 :     }
     605           0 :   }
     606             : 
     607           0 :   if( FD_UNLIKELY( !location_len ) ) {
     608           0 :     FD_LOG_WARNING(( "no location header in redirect response from " FD_IP4_ADDR_FMT ":%hu",
     609           0 :                      FD_IP4_ADDR_FMT_ARGS( http->addr.addr ), fd_ushort_bswap( http->addr.port ) ));
     610           0 :     fd_sshttp_cancel( http );
     611           0 :     return FD_SSHTTP_ADVANCE_ERROR;
     612           0 :   }
     613             : 
     614             :   /* Pre-validate that the redirect request will fit in the request
     615             :      buffer.  The request is rebuilt from scratch by fd_sshttp_init
     616             :      during the redirect, but the format must match so that a path
     617             :      accepted here will not overflow in fd_sshttp_init. */
     618           0 :   int pre_check;
     619           0 :   if( FD_LIKELY( http->hostname[0] ) ) {
     620           0 :     pre_check = fd_cstr_printf_check( http->request, sizeof(http->request), &http->request_len,
     621           0 :       "GET %.*s HTTP/1.1\r\n"
     622           0 :       "User-Agent: Firedancer\r\n"
     623           0 :       "Accept: */*\r\n"
     624           0 :       "Accept-Encoding: identity\r\n"
     625           0 :       "Host: %s:%u\r\n\r\n",
     626           0 :       (int)location_len, location, http->hostname, port );
     627           0 :   } else {
     628           0 :     pre_check = fd_cstr_printf_check( http->request, sizeof(http->request), &http->request_len,
     629           0 :       "GET %.*s HTTP/1.1\r\n"
     630           0 :       "User-Agent: Firedancer\r\n"
     631           0 :       "Accept: */*\r\n"
     632           0 :       "Accept-Encoding: identity\r\n"
     633           0 :       "Host: " FD_IP4_ADDR_FMT ":%u\r\n\r\n",
     634           0 :       (int)location_len, location, FD_IP4_ADDR_FMT_ARGS( http->addr.addr ), port );
     635           0 :   }
     636           0 :   if( FD_UNLIKELY( !pre_check ) ) {
     637           0 :     FD_LOG_WARNING(( "redirect request too long `%.*s` from " FD_IP4_ADDR_FMT ":%hu", (int)location_len, location,
     638           0 :                      FD_IP4_ADDR_FMT_ARGS( http->addr.addr ), fd_ushort_bswap( http->addr.port ) ));
     639           0 :     fd_sshttp_cancel( http );
     640           0 :     return FD_SSHTTP_ADVANCE_ERROR;
     641           0 :   }
     642             : 
     643           0 :   http->addr.port = fd_ushort_bswap( port );
     644           0 :   FD_LOG_INFO(( "following redirect to %s://" FD_IP4_ADDR_FMT ":%hu%.*s", http->is_https ? "https" : "http",
     645           0 :                 FD_IP4_ADDR_FMT_ARGS( http->addr.addr ), fd_ushort_bswap( http->addr.port ),
     646           0 :                 (int)location_len, location ));
     647             : 
     648           0 :   if( FD_UNLIKELY( http->is_https ) ) {
     649           0 :     http->state        = FD_SSHTTP_STATE_REDIRECT;
     650           0 :     http->location_len = location_len;
     651           0 :     FD_TEST( location_len<PATH_MAX-1UL );
     652           0 :     fd_memcpy( http->location, location, location_len );
     653           0 :     http->location[ location_len ] = '\0';
     654           0 :   } else {
     655           0 :     if( FD_LIKELY( !fd_sshttp_fuzz ) ) {
     656           0 :       fd_sshttp_cancel( http );
     657           0 :       if( FD_UNLIKELY( fd_sshttp_init( http, http->addr, http->hostname, http->is_https, location, location_len, ULONG_MAX, now ) ) ) {
     658           0 :         return FD_SSHTTP_ADVANCE_ERROR;
     659           0 :       }
     660           0 :     } else {
     661           0 :       http->state = FD_SSHTTP_STATE_RESP;
     662           0 :       http->response_len = 0UL;
     663           0 :     }
     664           0 :   }
     665             : 
     666           0 :   return FD_SSHTTP_ADVANCE_AGAIN;
     667           0 : }
     668             : 
     669             : /* rx_lowat_update lowers the socket's SO_RCVLOWAT to the body bytes
     670             :    still to arrive, capped at FD_SSHTTP_SO_RCVLOWAT, so the socket
     671             :    turns readable once a full low-water mark is queued or the rest of
     672             :    the body is.  Plaintext only: TLS reads go through record
     673             :    buffering. */
     674             : 
     675             : static void
     676           0 : rx_lowat_update( fd_sshttp_t * http ) {
     677           0 :   if( FD_UNLIKELY( http->is_https ) ) return;
     678           0 :   ulong pending = http->content_len - http->content_read - http->response_len;
     679           0 :   ulong lowat   = fd_ulong_min( FD_SSHTTP_SO_RCVLOWAT, pending );
     680           0 :   if( FD_LIKELY( !lowat || ( http->rx_lowat && lowat>=http->rx_lowat ) ) ) return;
     681           0 :   int so_rcvlowat = (int)lowat;
     682           0 :   if( FD_UNLIKELY( -1==setsockopt( http->sockfd, SOL_SOCKET, SO_RCVLOWAT, &so_rcvlowat, sizeof(int) ) ) ) {
     683           0 :     FD_LOG_ERR(( "setsockopt(SOL_SOCKET,SO_RCVLOWAT,%i) failed (%i-%s)", so_rcvlowat, errno, fd_io_strerror( errno ) ));
     684           0 :   }
     685           0 :   http->rx_lowat = lowat;
     686           0 : }
     687             : 
     688             : static int
     689             : read_response( fd_sshttp_t *        http,
     690             :                struct iovec const * iov,
     691             :                ulong *              data_len,
     692           0 :                long                 now ) {
     693           0 :   if( FD_UNLIKELY( now>http->deadline ) ) {
     694           0 :     FD_LOG_WARNING(( "timeout reading response from " FD_IP4_ADDR_FMT ":%hu",
     695           0 :                      FD_IP4_ADDR_FMT_ARGS( http->addr.addr ), fd_ushort_bswap( http->addr.port ) ));
     696           0 :     fd_sshttp_cancel( http );
     697           0 :     return FD_SSHTTP_ADVANCE_ERROR;
     698           0 :   }
     699             : 
     700           0 :   if( FD_UNLIKELY( http->response_len>=sizeof(http->response) ) ) {
     701           0 :     FD_LOG_WARNING(( "response headers too large from " FD_IP4_ADDR_FMT ":%hu",
     702           0 :                      FD_IP4_ADDR_FMT_ARGS( http->addr.addr ), fd_ushort_bswap( http->addr.port ) ));
     703           0 :     fd_sshttp_cancel( http );
     704           0 :     return FD_SSHTTP_ADVANCE_ERROR;
     705           0 :   }
     706             : 
     707           0 :   struct iovec resp_iov = { .iov_base = http->response+http->response_len, .iov_len = sizeof(http->response)-http->response_len };
     708           0 :   long read = http_recv( http, &resp_iov, 1UL, resp_iov.iov_len );
     709           0 :   if( FD_UNLIKELY( read<=0 ) ) return (int)read;
     710             : 
     711           0 :   http->response_len += (ulong)read;
     712             : 
     713           0 :   int               minor_version;
     714           0 :   int               status;
     715           0 :   const char *      message;
     716           0 :   ulong             message_len;
     717           0 :   struct phr_header headers[ 128UL ];
     718           0 :   ulong             header_cnt = 128UL;
     719           0 :   int parsed = phr_parse_response( http->response,
     720           0 :                                     http->response_len,
     721           0 :                                     &minor_version,
     722           0 :                                     &status,
     723           0 :                                     &message,
     724           0 :                                     &message_len,
     725           0 :                                     headers,
     726           0 :                                     &header_cnt,
     727           0 :                                     http->response_len - (ulong)read );
     728           0 :   if( FD_UNLIKELY( parsed==-1 ) ) {
     729           0 :     FD_LOG_WARNING(( "malformed response headers from " FD_IP4_ADDR_FMT ":%hu",
     730           0 :                      FD_IP4_ADDR_FMT_ARGS( http->addr.addr ), fd_ushort_bswap( http->addr.port ) ));
     731           0 :     fd_sshttp_cancel( http );
     732           0 :     return FD_SSHTTP_ADVANCE_ERROR;
     733           0 :   } else if( parsed==-2 ) {
     734           0 :     return FD_SSHTTP_ADVANCE_AGAIN;
     735           0 :   }
     736             : 
     737           0 :   int is_redirect = (status==301) | (status==302) | (status==303) | (status==307) | (status==308);
     738           0 :   if( FD_UNLIKELY( is_redirect ) ) {
     739           0 :     return follow_redirect( http, headers, header_cnt, now );
     740           0 :   }
     741             : 
     742           0 :   if( FD_UNLIKELY( status!=200 ) ) {
     743           0 :     FD_LOG_WARNING(( "unexpected response status %d %.*s from " FD_IP4_ADDR_FMT ":%hu", status, (int)message_len, message,
     744           0 :                      FD_IP4_ADDR_FMT_ARGS( http->addr.addr ), fd_ushort_bswap( http->addr.port ) ));
     745           0 :     fd_sshttp_cancel( http );
     746           0 :     return FD_SSHTTP_ADVANCE_ERROR;
     747           0 :   }
     748             : 
     749           0 :   http->content_read = 0UL;
     750           0 :   http->content_len = ULONG_MAX;
     751           0 :   for( ulong i=0UL; i<header_cnt; i++ ) {
     752           0 :     if( FD_LIKELY( headers[i].name_len!=14UL ) ) continue;
     753           0 :     if( FD_LIKELY( strncasecmp( headers[i].name, "content-length", 14UL ) ) ) continue;
     754             : 
     755           0 :     ulong val = 0UL;
     756           0 :     if( FD_UNLIKELY( fd_http_parse_content_len( headers[i].value, (ulong)headers[i].value_len, &val ) || val==0UL ) ) {
     757           0 :       FD_LOG_WARNING(( "invalid content-length in response from " FD_IP4_ADDR_FMT ":%hu", FD_IP4_ADDR_FMT_ARGS( http->addr.addr ), fd_ushort_bswap( http->addr.port ) ));
     758           0 :       fd_sshttp_cancel( http );
     759           0 :       return FD_SSHTTP_ADVANCE_ERROR;
     760           0 :     }
     761           0 :     http->content_len = val;
     762           0 :     break;
     763           0 :   }
     764             : 
     765           0 :   if( FD_UNLIKELY( http->content_len==ULONG_MAX ) ) {
     766           0 :     FD_LOG_WARNING(( "no content-length header in response from " FD_IP4_ADDR_FMT ":%hu",
     767           0 :                      FD_IP4_ADDR_FMT_ARGS( http->addr.addr ), fd_ushort_bswap( http->addr.port ) ));
     768           0 :     fd_sshttp_cancel( http );
     769           0 :     return FD_SSHTTP_ADVANCE_ERROR;
     770           0 :   }
     771             : 
     772           0 :   http->state     = FD_SSHTTP_STATE_DL;
     773           0 :   http->rx_lowat  = 0UL;
     774           0 :   http->rx_parked = 0;
     775           0 :   if( FD_UNLIKELY( (ulong)parsed<http->response_len ) ) {
     776             :     /* Body bytes past the caller's buffer are kept in response, with
     777             :        response_len repurposed as the residual length, drained by
     778             :        read_body before it reads the socket again. */
     779           0 :     ulong leftover = fd_ulong_min( http->response_len - (ulong)parsed, http->content_len );
     780           0 :     ulong copy_len = fd_ulong_min( leftover, iov[0].iov_len );
     781           0 :     fd_memcpy( iov[0].iov_base, http->response+parsed, copy_len );
     782           0 :     memmove( http->response, http->response+(ulong)parsed+copy_len, leftover-copy_len );
     783           0 :     http->response_len  = leftover-copy_len;
     784           0 :     http->content_read += copy_len;
     785           0 :     *data_len = copy_len;
     786           0 :     rx_lowat_update( http );
     787           0 :     return FD_SSHTTP_ADVANCE_DATA;
     788           0 :   } else {
     789           0 :     FD_TEST( http->response_len==(ulong)parsed );
     790           0 :     http->response_len = 0UL;
     791           0 :     rx_lowat_update( http );
     792           0 :     return FD_SSHTTP_ADVANCE_AGAIN;
     793           0 :   }
     794           0 : }
     795             : 
     796             : static int
     797             : read_body( fd_sshttp_t *        http,
     798             :            struct iovec const * iov,
     799             :            ulong                iov_cnt,
     800             :            ulong *              data_len,
     801           0 :            long                 now ) {
     802           0 :   if( FD_UNLIKELY( http->content_read>=http->content_len ) ) {
     803           0 :     if( FD_UNLIKELY( http->is_https ) ) {
     804           0 :       http->state = FD_SSHTTP_STATE_DONE;
     805           0 :       http->deadline = now + FD_SSHTTP_DEADLINE_NANOS;
     806           0 :       return FD_SSHTTP_ADVANCE_AGAIN;
     807           0 :     } else {
     808           0 :       fd_sshttp_cancel( http );
     809           0 :       http->state = FD_SSHTTP_STATE_INIT;
     810           0 :       return FD_SSHTTP_ADVANCE_DONE;
     811           0 :     }
     812           0 :   }
     813             : 
     814           0 :   FD_TEST( http->content_read<http->content_len );
     815             : 
     816           0 :   if( FD_UNLIKELY( http->response_len ) ) { /* residual body bytes from read_response */
     817           0 :     ulong copy_len = fd_ulong_min( http->response_len, iov[0].iov_len );
     818           0 :     fd_memcpy( iov[0].iov_base, http->response, copy_len );
     819           0 :     memmove( http->response, http->response+copy_len, http->response_len-copy_len );
     820           0 :     http->response_len  -= copy_len;
     821           0 :     http->content_read  += copy_len;
     822           0 :     *data_len = copy_len;
     823           0 :     return FD_SSHTTP_ADVANCE_DATA;
     824           0 :   }
     825             : 
     826           0 :   long read = http_recv( http, iov, iov_cnt, http->content_len-http->content_read );
     827           0 :   if( FD_UNLIKELY( read<=0 ) ) {
     828           0 :     http->rx_parked = read==FD_SSHTTP_ADVANCE_AGAIN && !http->is_https;
     829           0 :     return (int)read;
     830           0 :   }
     831             : 
     832           0 :   *data_len = (ulong)read;
     833           0 :   http->content_read += (ulong)read;
     834           0 :   http->rx_parked     = 0;
     835           0 :   rx_lowat_update( http );
     836             : 
     837           0 :   return FD_SSHTTP_ADVANCE_DATA;
     838           0 : }
     839             : 
     840             : int
     841           0 : fd_sshttp_rx_parked( fd_sshttp_t const * http ) {
     842           0 :   return http->state==FD_SSHTTP_STATE_DL && http->rx_parked;
     843           0 : }
     844             : 
     845             : char const *
     846           0 : fd_sshttp_snapshot_name( fd_sshttp_t const * http ) {
     847           0 :   return http->snapshot_name;
     848           0 : }
     849             : 
     850             : ulong
     851           0 : fd_sshttp_content_len( fd_sshttp_t const * http ) {
     852           0 :   return http->content_len;
     853           0 : }
     854             : 
     855             : ulong
     856           0 : fd_sshttp_resolved_slot( fd_sshttp_t const * http ) {
     857           0 :   return http->resolved_slot;
     858           0 : }
     859             : 
     860             : uchar const *
     861           0 : fd_sshttp_resolved_hash( fd_sshttp_t const * http ) {
     862           0 :   return http->resolved_hash;
     863           0 : }
     864             : 
     865             : int
     866             : fd_sshttp_advance( fd_sshttp_t *        http,
     867             :                    struct iovec const * iov,
     868             :                    ulong                iov_cnt,
     869             :                    ulong *              data_len,
     870             :                    int *                downloading,
     871           0 :                    long                 now ) {
     872           0 :   FD_TEST( iov_cnt>=1UL && iov_cnt<=FD_SSHTTP_IOV_MAX );
     873           0 :   *downloading = 0;
     874           0 :   int res;
     875           0 :   switch( http->state ) {
     876           0 :     case FD_SSHTTP_STATE_INIT:     return FD_SSHTTP_ADVANCE_AGAIN;
     877           0 :     case FD_SSHTTP_STATE_CONNECT:  res = http_connect_tls( http, now ); break;
     878           0 :     case FD_SSHTTP_STATE_REDIRECT: res = setup_redirect_tls( http, now ); break;
     879           0 :     case FD_SSHTTP_STATE_REQ:      res = send_request( http, now ); break;
     880           0 :     case FD_SSHTTP_STATE_RESP:     res = read_response( http, iov, data_len, now ); break;
     881           0 :     case FD_SSHTTP_STATE_DL:       *downloading = 1; res = read_body( http, iov, iov_cnt, data_len, now ); break;
     882           0 :     case FD_SSHTTP_STATE_DONE:
     883           0 :       fd_sshttp_cancel( http );
     884           0 :       http->state = FD_SSHTTP_STATE_INIT;
     885           0 :       return FD_SSHTTP_ADVANCE_DONE;
     886           0 :     default:                       return FD_SSHTTP_ADVANCE_ERROR;
     887           0 :   }
     888             : 
     889             :   /* Only wait for writability while there is something to send,
     890             :      otherwise a connected socket is always ready and the tile spins. */
     891           0 :   if( FD_LIKELY( http->sockfd!=-1 ) ) {
     892           0 :     int out = http->state==FD_SSHTTP_STATE_REQ || (http->is_https && fd_tlsrec_sock_tx_pending( http->tls_sock ));
     893           0 :     uint events = EPOLLIN | (out ? EPOLLOUT : 0U);
     894           0 :     if( FD_UNLIKELY( events!=http->epoll_events ) ) {
     895           0 :       struct epoll_event ev = { .events = events, .data.fd = http->sockfd };
     896           0 :       if( FD_UNLIKELY( -1==epoll_ctl( http->epoll_fd, EPOLL_CTL_MOD, http->sockfd, &ev ) ) ) FD_LOG_ERR(( "epoll_ctl(MOD) failed (%d-%s)", errno, fd_io_strerror( errno ) ));
     897           0 :       http->epoll_events = events;
     898           0 :     }
     899           0 :   }
     900           0 :   return res;
     901           0 : }

Generated by: LCOV version 1.14