@@ -314,58 +314,45 @@ static int basic_auth_header(const char* user, const char* pass,
314314/* ---- TCP + TLS I/O ------------------------------------------------------ */
315315
316316typedef struct {
317- const WolfCertTransport * t ;
317+ WolfCertTransport t ;
318318 void * handle ;
319319 WOLFSSL * ssl ;
320320 int io_timeout_ms ; /* 0 nonblocking, < 0 unbounded */
321321 unsigned int connected : 1 ;
322322} WolfCertConn ;
323323
324- /* Open a connection and attach the transport that owns it. An explicit
325- * transport wins; a legacy connect_cb is dialled here and adapted, since it
326- * yields a descriptor rather than opening through a vtable. */
324+ /* Open a connection and take a copy of the transport that owns it. */
327325static int dial (WolfCertConn * c , const char * host , int port , int timeout_ms ,
328- const WolfCertTransport * transport ,
329- WolfCertConnectFn connect_cb , void * connect_ctx )
326+ const WolfCertTransport * transport )
330327{
331- const WolfCertTransport * t = transport ;
328+ int cbs ;
332329 int rc ;
333330
334- #ifndef WOLFCERT_HAVE_BUILTIN_TRANSPORT
335- (void )connect_cb ;
336- (void )connect_ctx ;
331+ cbs = (transport -> connect != NULL ) + (transport -> read != NULL ) +
332+ (transport -> write != NULL ) + (transport -> disconnect != NULL );
337333
338- if (t == NULL )
334+ if (cbs == 4 ) {
335+ c -> t = * transport ;
336+ }
337+ else if (cbs != 0 || transport -> ctx != NULL ) {
339338 return WOLFCERT_ERR (WOLFCERT_ERR_BAD_ARG , "http" ,
340- "this build has no built-in transport" );
341- #else
342- if (t == NULL && connect_cb != NULL ) {
343- rc = wolfcert_legacy_connect (connect_cb , connect_ctx , host , port ,
344- timeout_ms , & c -> handle );
345- if (rc != WOLFCERT_OK )
346- return rc ;
347-
348- c -> t = & wolfcert_legacy_transport ;
349- c -> connected = 1 ;
350- return WOLFCERT_OK ;
339+ "transport must set all four callbacks, or nothing at all to take "
340+ "the built-in one" );
351341 }
352-
353- if (t == NULL )
354- t = & wolfcert_posix_transport ;
355- #endif
356-
357- /* Validate the whole vtable */
358- if (t -> connect == NULL || t -> read == NULL || t -> write == NULL ||
359- t -> disconnect == NULL )
342+ else {
343+ #ifdef WOLFCERT_HAVE_BUILTIN_TRANSPORT
344+ c -> t = wolfcert_posix_transport ;
345+ #else
360346 return WOLFCERT_ERR (WOLFCERT_ERR_BAD_ARG , "http" ,
361- "transport must supply connect, read, write and disconnect" );
347+ "this build has no built-in transport" );
348+ #endif
349+ }
362350
363- rc = t -> connect (t -> ctx , host , port , timeout_ms , & c -> handle );
351+ rc = c -> t . connect (c -> t . ctx , host , port , timeout_ms , & c -> handle );
364352 if (rc != WOLFCERT_OK )
365353 return rc < 0 ? rc : WOLFCERT_ERR (WOLFCERT_ERR_IO , "http" ,
366354 "transport connect returned %d, not 0 or a WOLFCERT_ERR_*" , rc );
367355
368- c -> t = t ;
369356 c -> connected = 1 ;
370357 return WOLFCERT_OK ;
371358}
@@ -374,7 +361,7 @@ static int dial(WolfCertConn* c, const char* host, int port, int timeout_ms,
374361static void conn_close (WolfCertConn * c )
375362{
376363 if (c -> connected ) {
377- (void )c -> t -> disconnect (c -> t -> ctx , c -> handle );
364+ (void )c -> t . disconnect (c -> t . ctx , c -> handle );
378365 c -> connected = 0 ;
379366 }
380367}
@@ -440,8 +427,8 @@ static int conn_write(WolfCertConn* c, const void* buf, size_t len)
440427 w = wolfSSL_write (c -> ssl , p + n , (int )(len - n ));
441428 }
442429 else {
443- w = c -> t -> write (c -> t -> ctx , c -> handle , p + n , len - n ,
444- c -> io_timeout_ms );
430+ w = c -> t . write (c -> t . ctx , c -> handle , p + n , len - n ,
431+ c -> io_timeout_ms );
445432 if (w > 0 && (size_t )w > len - n )
446433 return WOLFCERT_ERR_IO ;
447434 }
@@ -467,8 +454,8 @@ static int wolfcert_cbio_recv(WOLFSSL* ssl, char* buf, int sz, void* ctx)
467454 if (c == NULL || sz <= 0 )
468455 return WOLFSSL_CBIO_ERR_GENERAL ;
469456
470- r = c -> t -> read (c -> t -> ctx , c -> handle , (uint8_t * )buf , (size_t )sz ,
471- c -> io_timeout_ms );
457+ r = c -> t . read (c -> t . ctx , c -> handle , (uint8_t * )buf , (size_t )sz ,
458+ c -> io_timeout_ms );
472459 if (r > sz )
473460 return WOLFSSL_CBIO_ERR_GENERAL ;
474461 if (r > 0 )
@@ -499,8 +486,8 @@ static int wolfcert_cbio_send(WOLFSSL* ssl, char* buf, int sz, void* ctx)
499486 if (c == NULL || sz <= 0 )
500487 return WOLFSSL_CBIO_ERR_GENERAL ;
501488
502- r = c -> t -> write (c -> t -> ctx , c -> handle , (const uint8_t * )buf , (size_t )sz ,
503- c -> io_timeout_ms );
489+ r = c -> t . write (c -> t . ctx , c -> handle , (const uint8_t * )buf , (size_t )sz ,
490+ c -> io_timeout_ms );
504491 if (r > sz )
505492 return WOLFSSL_CBIO_ERR_GENERAL ;
506493 if (r > 0 )
@@ -524,10 +511,9 @@ static int conn_read(WolfCertConn* c, void* buf, size_t len)
524511 int r ;
525512
526513 if (c -> ssl == NULL ) {
527- r = c -> t -> read (c -> t -> ctx , c -> handle , (uint8_t * )buf , len ,
528- c -> io_timeout_ms );
529- /* A 0 spins the read-until-close loop; a count above len means the
530- * transport already overran the buffer. Neither is passed on. */
514+ r = c -> t .read (c -> t .ctx , c -> handle , (uint8_t * )buf , len ,
515+ c -> io_timeout_ms );
516+ /* Turn a 0 into a close, and refuse a count larger than len. */
531517 if (r == 0 )
532518 return WOLFCERT_ERR_CONN_CLOSED ;
533519 if (r > 0 && (size_t )r > len )
@@ -1167,10 +1153,6 @@ int wolfcert_http_request(const WolfCertHttpRequest* req, WolfCertHttpResponse*
11671153 if (req == NULL || resp == NULL || req -> url == NULL || req -> method == NULL )
11681154 return WOLFCERT_ERR_BAD_ARG ;
11691155
1170- if (req -> connect_cb != NULL && req -> transport != NULL )
1171- return WOLFCERT_ERR (WOLFCERT_ERR_BAD_ARG , "http" ,
1172- "set either connect_cb or transport, not both" );
1173-
11741156 memset (resp , 0 , sizeof (* resp ));
11751157 void * heap = req -> heap ? req -> heap : wolfcert_default_heap ();
11761158 resp -> heap = heap ;
@@ -1182,12 +1164,12 @@ int wolfcert_http_request(const WolfCertHttpRequest* req, WolfCertHttpResponse*
11821164 if (rc != WOLFCERT_OK )
11831165 return rc ;
11841166
1185- WolfCertConn c = { 0 } ;
1167+ WolfCertConn c ;
11861168 WOLFSSL_CTX * ctx = NULL ;
11871169
1170+ memset (& c , 0 , sizeof (c ));
11881171 c .io_timeout_ms = -1 ;
1189- rc = dial (& c , u .host , u .port , req -> timeout_ms , req -> transport ,
1190- req -> connect_cb , req -> connect_ctx );
1172+ rc = dial (& c , u .host , u .port , req -> timeout_ms , & req -> transport );
11911173 if (rc != WOLFCERT_OK ) {
11921174 wolfcert_http_url_free (& u );
11931175 return rc ;
@@ -1243,10 +1225,6 @@ int wolfcert_http_session_open(const WolfCertHttpSessionCfg* cfg,
12431225 if (cfg == NULL || cfg -> base_url == NULL || out == NULL )
12441226 return WOLFCERT_ERR_BAD_ARG ;
12451227
1246- if (cfg -> connect_cb != NULL && cfg -> transport != NULL )
1247- return WOLFCERT_ERR (WOLFCERT_ERR_BAD_ARG , "http" ,
1248- "set either connect_cb or transport, not both" );
1249-
12501228 void * heap = cfg -> heap ? cfg -> heap : wolfcert_default_heap ();
12511229
12521230 WolfCertHttpSession * s = (WolfCertHttpSession * )WOLFCERT_XMALLOC (sizeof (* s ), heap );
@@ -1266,7 +1244,7 @@ int wolfcert_http_session_open(const WolfCertHttpSessionCfg* cfg,
12661244
12671245 s -> conn .io_timeout_ms = cfg -> nonblocking ? 0 : -1 ;
12681246 rc = dial (& s -> conn , s -> base .host , s -> base .port , cfg -> timeout_ms ,
1269- cfg -> transport , cfg -> connect_cb , cfg -> connect_ctx );
1247+ & cfg -> transport );
12701248 if (rc != WOLFCERT_OK ) {
12711249 wolfcert_http_session_close (s );
12721250 return rc ;
@@ -1320,12 +1298,10 @@ int wolfcert_http_session_fd(const WolfCertHttpSession* s)
13201298#ifdef WOLFCERT_HAVE_BUILTIN_TRANSPORT
13211299 if (s == NULL || !s -> conn .connected )
13221300 return -1 ;
1323- if (!wolfcert_transport_is_fd_backed (s -> conn .t ))
1324- return -1 ;
13251301
1326- return ( int )( intptr_t ) s -> conn .handle ;
1302+ return wolfcert_transport_fd ( & s -> conn .t , s -> conn . handle ) ;
13271303#else
1328- (void )s ; /* no fd -backed transport exists in this build */
1304+ (void )s ; /* no descriptor -backed transport exists in this build */
13291305 return -1 ;
13301306#endif
13311307}
@@ -1457,8 +1433,8 @@ static int nb_write(WolfCertConn* c, const uint8_t* buf, size_t len, size_t* off
14571433 return WOLFCERT_ERR_IO ;
14581434 }
14591435
1460- int r = c -> t -> write (c -> t -> ctx , c -> handle , buf + * off , len - * off ,
1461- c -> io_timeout_ms );
1436+ int r = c -> t . write (c -> t . ctx , c -> handle , buf + * off , len - * off ,
1437+ c -> io_timeout_ms );
14621438 if (r > 0 && (size_t )r > len - * off )
14631439 return WOLFCERT_ERR_IO ;
14641440 if (r > 0 ) {
@@ -1539,9 +1515,9 @@ static int nb_read_some(WolfCertHttpSession* s, int* ended)
15391515 return WOLFCERT_ERR_IO ;
15401516 }
15411517
1542- int r = s -> conn .t -> read (s -> conn .t -> ctx , s -> conn .handle ,
1543- s -> sm_rx + s -> sm_rx_len ,
1544- WOLFCERT_HTTP_READ_CHUNK , s -> conn .io_timeout_ms );
1518+ int r = s -> conn .t . read (s -> conn .t . ctx , s -> conn .handle ,
1519+ s -> sm_rx + s -> sm_rx_len ,
1520+ WOLFCERT_HTTP_READ_CHUNK , s -> conn .io_timeout_ms );
15451521 if (r > 0 ) {
15461522 if ((size_t )r > WOLFCERT_HTTP_READ_CHUNK )
15471523 return WOLFCERT_ERR_IO ;
0 commit comments