ssl: use the callback instead of ifs to determine how to get data Using the callbacks makes it clearer and reduces the amount of #ifdefs and ifs and we need in the code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
diff --git a/src/netops.c b/src/netops.c
index 918ca1d..303dfe8 100644
--- a/src/netops.c
+++ b/src/netops.c
@@ -61,45 +61,26 @@ static int ssl_set_error(gitno_ssl *ssl, int error)
}
#endif
-void gitno_buffer_setup_callback(git_transport *t, gitno_buffer *buf, char *data, unsigned int len, int (*recv)(gitno_buffer *buf), void *cb_data)
-{
- memset(buf, 0x0, sizeof(gitno_buffer));
- memset(data, 0x0, len);
- buf->data = data;
- buf->len = len;
- buf->offset = 0;
- buf->fd = t->socket;
-#ifdef GIT_SSL
- if (t->encrypt)
- buf->ssl = &t->ssl;
-#endif
-
- buf->recv = recv;
- buf->cb_data = cb_data;
-}
-
-void gitno_buffer_setup(git_transport *t, gitno_buffer *buf, char *data, unsigned int len)
-{
- gitno_buffer_setup_callback(t, buf, data, len, gitno__recv, NULL);
-}
-
int gitno_recv(gitno_buffer *buf)
{
return buf->recv(buf);
}
#ifdef GIT_SSL
-static int ssl_recv(gitno_ssl *ssl, void *data, size_t len)
+static int gitno__recv_ssl(gitno_buffer *buf)
{
int ret;
do {
- ret = SSL_read(ssl->ssl, data, len);
- } while (SSL_get_error(ssl->ssl, ret) == SSL_ERROR_WANT_READ);
+ ret = SSL_read(buf->ssl->ssl, buf->data + buf->offset, buf->len - buf->offset);
+ } while (SSL_get_error(buf->ssl->ssl, ret) == SSL_ERROR_WANT_READ);
- if (ret < 0)
- return ssl_set_error(ssl, ret);
+ if (ret < 0) {
+ net_set_error("Error receiving socket data");
+ return -1;
+ }
+ buf->offset += ret;
return ret;
}
#endif
@@ -108,29 +89,39 @@ int gitno__recv(gitno_buffer *buf)
{
int ret;
-#ifdef GIT_SSL
- if (buf->ssl != NULL) {
- if ((ret = ssl_recv(buf->ssl, buf->data + buf->offset, buf->len - buf->offset)) < 0)
- return -1;
- } else {
- ret = p_recv(buf->fd, buf->data + buf->offset, buf->len - buf->offset, 0);
- if (ret < 0) {
- net_set_error("Error receiving socket data");
- return -1;
- }
- }
-#else
ret = p_recv(buf->fd, buf->data + buf->offset, buf->len - buf->offset, 0);
if (ret < 0) {
net_set_error("Error receiving socket data");
return -1;
}
-#endif
buf->offset += ret;
return ret;
}
+void gitno_buffer_setup_callback(git_transport *t, gitno_buffer *buf, char *data, unsigned int len, int (*recv)(gitno_buffer *buf), void *cb_data)
+{
+ memset(buf, 0x0, sizeof(gitno_buffer));
+ memset(data, 0x0, len);
+ buf->data = data;
+ buf->len = len;
+ buf->offset = 0;
+ buf->fd = t->socket;
+ buf->recv = recv;
+ buf->cb_data = cb_data;
+}
+
+void gitno_buffer_setup(git_transport *t, gitno_buffer *buf, char *data, unsigned int len)
+{
+#ifdef GIT_SSL
+ if (t->encrypt) {
+ gitno_buffer_setup_callback(t, buf, data, len, gitno__recv_ssl, NULL);
+ buf->ssl = &t->ssl;
+ } else
+#endif
+ gitno_buffer_setup_callback(t, buf, data, len, gitno__recv, NULL);
+}
+
/* Consume up to ptr and move the rest of the buffer to the beginning */
void gitno_consume(gitno_buffer *buf, const char *ptr)
{