Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 7fafde6

Browse files
committed
stream: allow registering a user-provided TLS constructor
This allows the application to use their own TLS stream, regardless of the capabilities of libgit2 itself.
1 parent d39f643 commit 7fafde6

File tree

4 files changed

+78
-0
lines changed

4 files changed

+78
-0
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,16 @@ v0.23 + 1
1717
the opportunity for concurrent operations and not committing any
1818
changes until the unlock.
1919

20+
2021
* `git_diff_options` added a new callback `progress_cb` to report on the
2122
progress of the diff as files are being compared. The documentation of
2223
the existing callback `notify_cb` was updated to reflect that it only
2324
gets called when new deltas are added to the diff.
2425

26+
* `git_stream_register_tls()` lets you register a callback to be used
27+
as the constructor for a TLS stream instead of the libgit2 built-in
28+
one.
29+
2530
### API removals
2631

2732
### Breaking API changes

include/git2/sys/stream.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,19 @@ typedef struct git_stream {
3939
void (*free)(struct git_stream *);
4040
} git_stream;
4141

42+
typedef int (*git_stream_cb)(git_stream **out, const char *host, const char *port);
43+
44+
/**
45+
* Register a TLS stream constructor for the library to use
46+
*
47+
* If a constructor is already set, it will be overwritten. Pass
48+
* `NULL` in order to deregister the current constructor.
49+
*
50+
* @param ctor the constructor to use
51+
* @return 0 or an error code
52+
*/
53+
GIT_EXTERN(int) git_stream_register_tls(git_stream_cb ctor);
54+
4255
GIT_END_DECL
4356

4457
#endif

src/tls_stream.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,21 @@
1111
#include "openssl_stream.h"
1212
#include "stransport_stream.h"
1313

14+
static git_stream_cb tls_ctor;
15+
16+
int git_stream_register_tls(git_stream_cb ctor)
17+
{
18+
tls_ctor = ctor;
19+
20+
return 0;
21+
}
22+
1423
int git_tls_stream_new(git_stream **out, const char *host, const char *port)
1524
{
25+
26+
if (tls_ctor)
27+
return tls_ctor(out, host, port);
28+
1629
#ifdef GIT_SECURE_TRANSPORT
1730
return git_stransport_stream_new(out, host, port);
1831
#elif defined(GIT_OPENSSL)

tests/core/stream.c

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#include "clar_libgit2.h"
2+
#include "git2/sys/stream.h"
3+
#include "tls_stream.h"
4+
#include "stream.h"
5+
6+
static git_stream test_stream;
7+
static int ctor_called;
8+
9+
static int test_ctor(git_stream **out, const char *host, const char *port)
10+
{
11+
GIT_UNUSED(host);
12+
GIT_UNUSED(port);
13+
14+
ctor_called = 1;
15+
*out = &test_stream;
16+
17+
return 0;
18+
}
19+
20+
void test_core_stream__register_tls(void)
21+
{
22+
git_stream *stream;
23+
int error;
24+
25+
ctor_called = 0;
26+
cl_git_pass(git_stream_register_tls(test_ctor));
27+
cl_git_pass(git_tls_stream_new(&stream, "localhost", "443"));
28+
cl_assert_equal_i(1, ctor_called);
29+
cl_assert_equal_p(&test_stream, stream);
30+
31+
ctor_called = 0;
32+
stream = NULL;
33+
cl_git_pass(git_stream_register_tls(NULL));
34+
error = git_tls_stream_new(&stream, "localhost", "443");
35+
36+
/* We don't have arbitrary TLS stream support on Windows */
37+
#if GIT_WIN32
38+
cl_git_fail_with(-1, error);
39+
#else
40+
cl_git_pass(error);
41+
#endif
42+
43+
cl_assert_equal_i(0, ctor_called);
44+
cl_assert(&test_stream != stream);
45+
46+
git_stream_free(stream);
47+
}

0 commit comments

Comments
 (0)