source src/netops.c
Line | Flow | Count | Block(s) | Source |
---|---|---|---|---|
1 | - | /* | ||
2 | - | * Copyright (C) the libgit2 contributors. All rights reserved. | ||
3 | - | * | ||
4 | - | * This file is part of libgit2, distributed under the GNU GPL v2 with | ||
5 | - | * a Linking Exception. For full terms see the included COPYING file. | ||
6 | - | */ | ||
7 | - | |||
8 | - | #include "netops.h" | ||
9 | - | |||
10 | - | #include <ctype.h> | ||
11 | - | #include "git2/errors.h" | ||
12 | - | |||
13 | - | #include "posix.h" | ||
14 | - | #include "buffer.h" | ||
15 | - | #include "http_parser.h" | ||
16 | - | #include "global.h" | ||
17 | - | |||
18 | 744 | 2 | int gitno_recv(gitno_buffer *buf) | |
19 | - | { | ||
20 | 744 | 2 | return buf->recv(buf); | |
21 | - | } | ||
22 | - | |||
23 | 103 | 2 | void gitno_buffer_setup_callback( | |
24 | - | gitno_buffer *buf, | ||
25 | - | char *data, | ||
26 | - | size_t len, | ||
27 | - | int (*recv)(gitno_buffer *buf), void *cb_data) | ||
28 | - | { | ||
29 | 103 | 2 | memset(data, 0x0, len); | |
30 | 103 | 2 | buf->data = data; | |
31 | 103 | 2 | buf->len = len; | |
32 | 103 | 2 | buf->offset = 0; | |
33 | 103 | 2 | buf->recv = recv; | |
34 | 103 | 2 | buf->cb_data = cb_data; | |
35 | 103 | 2 | } | |
36 | - | |||
37 | 101 | 2 | static int recv_stream(gitno_buffer *buf) | |
38 | - | { | ||
39 | 101 | 2 | git_stream *io = (git_stream *) buf->cb_data; | |
40 | 101 | 2 | size_t readlen = buf->len - buf->offset; | |
41 | - | ssize_t ret; | ||
42 | - | |||
43 | 101 | 2 | readlen = min(readlen, INT_MAX); | |
44 | - | |||
45 | 101 | 2 | ret = git_stream_read(io, buf->data + buf->offset, (int)readlen); | |
46 | 101 | 3 | if (ret < 0) | |
47 | ##### | 4 | return -1; | |
48 | - | |||
49 | 101 | 5 | buf->offset += ret; | |
50 | 101 | 5 | return (int)ret; | |
51 | - | } | ||
52 | - | |||
53 | 101 | 2 | void gitno_buffer_setup_fromstream(git_stream *st, gitno_buffer *buf, char *data, size_t len) | |
54 | - | { | ||
55 | 101 | 2 | memset(data, 0x0, len); | |
56 | 101 | 2 | buf->data = data; | |
57 | 101 | 2 | buf->len = len; | |
58 | 101 | 2 | buf->offset = 0; | |
59 | 101 | 2 | buf->recv = recv_stream; | |
60 | 101 | 2 | buf->cb_data = st; | |
61 | 101 | 2 | } | |
62 | - | |||
63 | - | /* Consume up to ptr and move the rest of the buffer to the beginning */ | ||
64 | 1022 | 2 | void gitno_consume(gitno_buffer *buf, const char *ptr) | |
65 | - | { | ||
66 | - | size_t consumed; | ||
67 | - | |||
68 | 1022 | 2,3 | assert(ptr - buf->data >= 0); | |
69 | 1022 | 4,5 | assert(ptr - buf->data <= (int) buf->len); | |
70 | - | |||
71 | 1022 | 6 | consumed = ptr - buf->data; | |
72 | - | |||
73 | 1022 | 6 | memmove(buf->data, ptr, buf->offset - consumed); | |
74 | 1022 | 6 | memset(buf->data + buf->offset, 0x0, buf->len - buf->offset); | |
75 | 1022 | 6 | buf->offset -= consumed; | |
76 | 1022 | 6 | } | |
77 | - | |||
78 | - | /* Consume const bytes and move the rest of the buffer to the beginning */ | ||
79 | ##### | 2 | void gitno_consume_n(gitno_buffer *buf, size_t cons) | |
80 | - | { | ||
81 | ##### | 2 | memmove(buf->data, buf->data + cons, buf->len - buf->offset); | |
82 | ##### | 2 | memset(buf->data + cons, 0x0, buf->len - buf->offset); | |
83 | ##### | 2 | buf->offset -= cons; | |
84 | ##### | 2 | } | |
85 | - | |||
86 | - | /* Match host names according to RFC 2818 rules */ | ||
87 | 99 | 2 | int gitno__match_host(const char *pattern, const char *host) | |
88 | - | { | ||
89 | - | for (;;) { | ||
90 | 99 | 2 | char c = git__tolower(*pattern++); | |
91 | - | |||
92 | 99 | 2 | if (c == '\0') | |
93 | 3 | 3-6 | return *host ? -1 : 0; | |
94 | - | |||
95 | 96 | 7 | if (c == '*') { | |
96 | 16 | 8 | c = *pattern; | |
97 | - | /* '*' at the end matches everything left */ | ||
98 | 16 | 8 | if (c == '\0') | |
99 | ##### | 9 | return 0; | |
100 | - | |||
101 | - | /* | ||
102 | - | * We've found a pattern, so move towards the next matching | ||
103 | - | * char. The '.' is handled specially because wildcards aren't | ||
104 | - | * allowed to cross subdomains. | ||
105 | - | */ | ||
106 | - | |||
107 | 108 | 10,16 | while(*host) { | |
108 | 108 | 11 | char h = git__tolower(*host); | |
109 | 108 | 11 | if (c == h) | |
110 | 16 | 12 | return gitno__match_host(pattern, host++); | |
111 | 92 | 13 | if (h == '.') | |
112 | ##### | 14 | return gitno__match_host(pattern, host); | |
113 | 92 | 15 | host++; | |
114 | - | } | ||
115 | ##### | 17 | return -1; | |
116 | - | } | ||
117 | - | |||
118 | 80 | 18 | if (c != git__tolower(*host++)) | |
119 | 15 | 19 | return -1; | |
120 | 65 | 20 | } | |
121 | - | |||
122 | - | return -1; | ||
123 | - | } |