99
1010#include "common.h"
1111#include "global.h"
12+ #include "streams/registry.h"
1213#include "streams/tls.h"
1314#include "streams/mbedtls.h"
1415#include "streams/openssl.h"
1516#include "streams/stransport.h"
1617
17- struct git_tls_stream_registration {
18- git_rwlock lock ;
19- git_stream_registration callbacks ;
20- };
21-
22- static struct git_tls_stream_registration stream_registration ;
23-
24- static void shutdown_ssl (void )
25- {
26- git_rwlock_free (& stream_registration .lock );
27- }
28-
29- int git_tls_stream_global_init (void )
30- {
31- if (git_rwlock_init (& stream_registration .lock ) < 0 )
32- return -1 ;
33-
34- git__on_shutdown (shutdown_ssl );
35- return 0 ;
36- }
37-
38- int git_stream_register_tls (git_stream_registration * registration )
39- {
40- assert (!registration || registration -> init );
41-
42- if (git_rwlock_wrlock (& stream_registration .lock ) < 0 ) {
43- giterr_set (GITERR_OS , "failed to lock stream registration" );
44- return -1 ;
45- }
46-
47- if (registration )
48- memcpy (& stream_registration .callbacks , registration ,
49- sizeof (git_stream_registration ));
50- else
51- memset (& stream_registration .callbacks , 0 ,
52- sizeof (git_stream_registration ));
53-
54- git_rwlock_wrunlock (& stream_registration .lock );
55- return 0 ;
56- }
57-
5818int git_tls_stream_new (git_stream * * out , const char * host , const char * port )
5919{
6020 int (* init )(git_stream * * , const char * , const char * ) = NULL ;
21+ git_stream_registration custom = {0 };
22+ int error ;
6123
6224 assert (out && host && port );
6325
64- if (git_rwlock_rdlock (& stream_registration .lock ) < 0 ) {
65- giterr_set (GITERR_OS , "failed to lock stream registration" );
66- return -1 ;
67- }
68-
69- if (stream_registration .callbacks .init ) {
70- init = stream_registration .callbacks .init ;
71- } else {
26+ if ((error = git_stream_registry_lookup (& custom , 1 )) == 0 ) {
27+ init = custom .init ;
28+ } else if (error == GIT_ENOTFOUND ) {
7229#ifdef GIT_SECURE_TRANSPORT
7330 init = git_stransport_stream_new ;
7431#elif defined(GIT_OPENSSL )
7532 init = git_openssl_stream_new ;
7633#elif defined(GIT_MBEDTLS )
7734 init = git_mbedtls_stream_new ;
7835#endif
79- }
80-
81- if (git_rwlock_rdunlock (& stream_registration .lock ) < 0 ) {
82- giterr_set (GITERR_OS , "failed to unlock stream registration" );
83- return -1 ;
36+ } else {
37+ return error ;
8438 }
8539
8640 if (!init ) {
@@ -94,16 +48,12 @@ int git_tls_stream_new(git_stream **out, const char *host, const char *port)
9448int git_tls_stream_wrap (git_stream * * out , git_stream * in , const char * host )
9549{
9650 int (* wrap )(git_stream * * , git_stream * , const char * ) = NULL ;
51+ git_stream_registration custom = {0 };
9752
9853 assert (out && in );
9954
100- if (git_rwlock_rdlock (& stream_registration .lock ) < 0 ) {
101- giterr_set (GITERR_OS , "failed to lock stream registration" );
102- return -1 ;
103- }
104-
105- if (stream_registration .callbacks .wrap ) {
106- wrap = stream_registration .callbacks .wrap ;
55+ if (git_stream_registry_lookup (& custom , 1 ) == 0 ) {
56+ wrap = custom .wrap ;
10757 } else {
10858#ifdef GIT_SECURE_TRANSPORT
10959 wrap = git_stransport_stream_wrap ;
@@ -114,11 +64,6 @@ int git_tls_stream_wrap(git_stream **out, git_stream *in, const char *host)
11464#endif
11565 }
11666
117- if (git_rwlock_rdunlock (& stream_registration .lock ) < 0 ) {
118- giterr_set (GITERR_OS , "failed to unlock stream registration" );
119- return -1 ;
120- }
121-
12267 if (!wrap ) {
12368 giterr_set (GITERR_SSL , "there is no TLS stream available" );
12469 return -1 ;
0 commit comments