]> git.proxmox.com Git - libgit2.git/blame - src/netops.c
install as examples
[libgit2.git] / src / netops.c
CommitLineData
1b4f8140 1/*
359fc2d2 2 * Copyright (C) the libgit2 contributors. All rights reserved.
1b4f8140 3 *
bb742ede
VM
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.
1b4f8140 6 */
6e34111e 7
eae0bfdc
PP
8#include "netops.h"
9
dbb36e1b 10#include <ctype.h>
1b4f8140
CMN
11#include "git2/errors.h"
12
34bfb4b0 13#include "posix.h"
bd6585a7 14#include "buffer.h"
c227c173 15#include "http_parser.h"
1d3364ac 16#include "global.h"
bd6585a7 17
b49c8f71
CMN
18int gitno_recv(gitno_buffer *buf)
19{
20 return buf->recv(buf);
66024c7c
CMN
21}
22
e25dda51 23void gitno_buffer_setup_callback(
e25dda51
VM
24 gitno_buffer *buf,
25 char *data,
26 size_t len,
27 int (*recv)(gitno_buffer *buf), void *cb_data)
8861d32f 28{
8861d32f
CMN
29 memset(data, 0x0, len);
30 buf->data = data;
31 buf->len = len;
32 buf->offset = 0;
8861d32f
CMN
33 buf->recv = recv;
34 buf->cb_data = cb_data;
35}
36
1b75c29e 37static int recv_stream(gitno_buffer *buf)
8861d32f 38{
1b75c29e 39 git_stream *io = (git_stream *) buf->cb_data;
0c9c969a
UG
40 size_t readlen = buf->len - buf->offset;
41 ssize_t ret;
41fb1ca0 42
0c9c969a
UG
43 readlen = min(readlen, INT_MAX);
44
45 ret = git_stream_read(io, buf->data + buf->offset, (int)readlen);
1b75c29e
CMN
46 if (ret < 0)
47 return -1;
48
49 buf->offset += ret;
0c9c969a 50 return (int)ret;
8861d32f
CMN
51}
52
02b4c1e2
CMN
53void gitno_buffer_setup_fromstream(git_stream *st, gitno_buffer *buf, char *data, size_t len)
54{
55 memset(data, 0x0, len);
56 buf->data = data;
57 buf->len = len;
58 buf->offset = 0;
1b75c29e
CMN
59 buf->recv = recv_stream;
60 buf->cb_data = st;
02b4c1e2
CMN
61}
62
ea7a5452 63/* Consume up to ptr and move the rest of the buffer to the beginning */
c7c787ce 64void gitno_consume(gitno_buffer *buf, const char *ptr)
ea7a5452 65{
0bd594b6 66 size_t consumed;
ea7a5452 67
0bd594b6 68 assert(ptr - buf->data >= 0);
ea7a5452
CMN
69 assert(ptr - buf->data <= (int) buf->len);
70
c7c787ce 71 consumed = ptr - buf->data;
ea7a5452 72
c7c787ce
CMN
73 memmove(buf->data, ptr, buf->offset - consumed);
74 memset(buf->data + buf->offset, 0x0, buf->len - buf->offset);
75 buf->offset -= consumed;
ea7a5452
CMN
76}
77
78/* Consume const bytes and move the rest of the buffer to the beginning */
0bd594b6 79void gitno_consume_n(gitno_buffer *buf, size_t cons)
ea7a5452
CMN
80{
81 memmove(buf->data, buf->data + cons, buf->len - buf->offset);
82 memset(buf->data + cons, 0x0, buf->len - buf->offset);
83 buf->offset -= cons;
84}
85
16768191 86/* Match host names according to RFC 2818 rules */
1f0d4f3d 87int gitno__match_host(const char *pattern, const char *host)
dbb36e1b
CMN
88{
89 for (;;) {
75a4636f 90 char c = git__tolower(*pattern++);
dbb36e1b
CMN
91
92 if (c == '\0')
93 return *host ? -1 : 0;
94
95 if (c == '*') {
96 c = *pattern;
97 /* '*' at the end matches everything left */
98 if (c == '\0')
99 return 0;
100
16768191
CMN
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 while(*host) {
75a4636f 108 char h = git__tolower(*host);
16768191 109 if (c == h)
1f0d4f3d 110 return gitno__match_host(pattern, host++);
16768191 111 if (h == '.')
1f0d4f3d 112 return gitno__match_host(pattern, host);
16768191 113 host++;
dbb36e1b 114 }
16768191 115 return -1;
dbb36e1b
CMN
116 }
117
75a4636f 118 if (c != git__tolower(*host++))
dbb36e1b
CMN
119 return -1;
120 }
121
122 return -1;
123}