forked from mojombo/egitd
/
server.erl
252 lines (220 loc) · 6.97 KB
/
server.erl
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
-module(server).
-export([start_link/0, init/1]).
start_link() ->
proc_lib:start_link(?MODULE, init, [self()]).
init(Parent) ->
read_conf(),
LSock = try_listen(10),
proc_lib:init_ack(Parent, {ok, self()}),
loop(LSock).
read_conf() ->
{ok, Conf} = application:get_env(conf),
io:format("Using conf file ~p~n", [Conf]),
conf:read_conf(Conf).
try_listen(0) ->
io:format("Could not listen on port 9418~n");
try_listen(Times) ->
Res = gen_tcp:listen(9418, [list, {packet, 0}, {active, false}]),
case Res of
{ok, LSock} ->
io:format("Listening on port 9418~n"),
LSock;
{error, Reason} ->
io:format("Could not listen on port 9418: ~p~n", [Reason]),
timer:sleep(5000),
try_listen(Times - 1)
end.
loop(LSock) ->
{ok, Sock} = gen_tcp:accept(LSock),
spawn(fun() -> handle_method(Sock) end),
loop(LSock).
handle_method(Sock) ->
% get the requested host and method
{ok, Header} = gen_tcp:recv(Sock, 0),
% io:format("header = ~p~n", [Header]),
{ok, Host} = extract_host(Header),
Method = extract_method_name(Header),
% dispatch
case Method of
{ok, "upload-pack"} ->
handle_upload_pack(Sock, Host, Header);
{ok, "receive-pack"} ->
receive_pack:handle(Sock, Host, Header);
invalid ->
gen_tcp:send(Sock, "Invalid method declaration. Upgrade to the latest git.\n"),
ok = gen_tcp:close(Sock)
end.
handle_upload_pack(Sock, Host, Header) ->
try
handle_upload_pack_impl(Sock, Host, Header)
catch
throw:{no_such_repo, Repo} ->
handle_upload_pack_nosuchrepo(Sock, Repo);
throw:{permission_denied, Repo} ->
handle_upload_pack_permission_denied(Sock, Repo)
end.
handle_upload_pack_impl(Sock, Host, Header) ->
% extract and normalize the repo path
{ok, Path} = extract_repo_path(Header),
{ok, FullPath} = conf:convert_path(Host, Path),
% io:format("fullpath = ~p~n", [FullPath]),
% check for repo existence
case file_exists(FullPath) of
false -> throw({no_such_repo, FullPath});
true -> ok
end,
% check for git-daemon-export-ok file
GitDaemonExportOkFilePath = filename:join([FullPath, "git-daemon-export-ok"]),
case file_exists(GitDaemonExportOkFilePath) of
false -> throw({permission_denied, GitDaemonExportOkFilePath});
true -> ok
end,
% make the port
Command = "git upload-pack " ++ FullPath,
Port = open_port({spawn, Command}, [binary]),
% the initial output from git-upload-pack lists the SHA1s of each head.
% data completion is denoted by "0000" on it's own line.
% this is sent back immediately to the client.
Index = gather_out(Port),
% io:format("index = ~p~n", [Index]),
gen_tcp:send(Sock, Index),
% once the client receives the index data, it will demand that specific
% revisions be packaged and sent back. this demand will be forwarded to
% git-upload-pack.
case gather_demand(Sock) of
{ok, Demand} ->
% io:format("+++~n~p~n+++~n", [Demand]),
port_command(Port, Demand),
% in response to the demand, git-upload-pack will stream out the requested
% pack information. data completion is denoted by "0000".
stream_out(Port, Sock),
% close connection
ok = gen_tcp:close(Sock);
{error, closed} ->
port_command(Port, "0000"),
port_close(Port),
ok;
{error, Reason} ->
io:format("Client closed socket because: ~p~n", [Reason]),
port_command(Port, "0000"),
port_close(Port),
ok = gen_tcp:close(Sock)
end.
handle_upload_pack_nosuchrepo(Sock, Repo) ->
io:format("no such repo: ~p~n", [Repo]),
ok = gen_tcp:close(Sock).
handle_upload_pack_permission_denied(Sock, Repo) ->
io:format("permission denied to repo: ~p~n", [Repo]),
ok = gen_tcp:close(Sock).
gather_demand(Sock) ->
gather_demand(Sock, "").
gather_demand(Sock, DataSoFar) ->
case gen_tcp:recv(Sock, 0) of
{ok, Data} ->
% io:format("data = ~p~n", [Data]),
TotalData = DataSoFar ++ Data,
case regexp:first_match(TotalData, "0009done\n$") of
{match, _Start, _Length} ->
{ok, TotalData};
_Else ->
gather_demand(Sock, TotalData)
end;
{error, Reason} ->
{error, Reason}
end.
gather_out(Port) ->
gather_out(Port, [], pipe:new()).
gather_out(Port, DataSoFar, Pipe) ->
% io:format("gather-out "),
{ok, Data, P2} = read_chunk(Port, Pipe),
% io:format("+++~n~p~n+++~n", [Data]),
TotalData = lists:append(DataSoFar, [Data]),
case Data =:= <<"0000">> of
true ->
list_to_binary(TotalData);
false ->
gather_out(Port, TotalData, P2)
end.
stream_out(Port, Sock) ->
stream_out(Port, Sock, pipe:new()).
stream_out(Port, Sock, Pipe) ->
% io:format("stream out "),
% io:format("+++~n~p~n+++~n", [Data]),
{ok, Data, P2} = read_chunk(Port, Pipe),
gen_tcp:send(Sock, Data),
case Data =:= <<"0000">> of
true ->
done;
false ->
stream_out(Port, Sock, P2)
end.
read_chunk(Port, Pipe) ->
case pipe:size(Pipe) >= 4 of
true ->
{ok, ChunkSizeHex, P2} = pipe:read(4, Pipe),
% io:format("chunk size hex = ~p~n", [ChunkSizeHex]),
read_chunk_body(ChunkSizeHex, Port, P2);
false ->
{data, Data} = readline(Port),
{ok, P2} = pipe:write(Data, Pipe),
read_chunk(Port, P2)
end.
read_chunk_body(<<"0000">>, _Port, Pipe) ->
{ok, <<"0000">>, Pipe};
read_chunk_body(ChunkSizeHex, Port, Pipe) ->
ChunkSize = convert_chunk_size(ChunkSizeHex),
% io:format("chunk size = ~p~n", [ChunkSize]),
case pipe:read(ChunkSize, Pipe) of
{ok, Bin, P2} ->
% io:format("chunk body = ~p~n", [Bin]),
Data = concat_binary([ChunkSizeHex, Bin]),
{ok, Data, P2};
eof ->
% io:format("chunk body eof~n", []),
{data, Data} = readline(Port),
{ok, P2} = pipe:write(Data, Pipe),
read_chunk_body(ChunkSizeHex, Port, P2)
end.
convert_chunk_size(ChunkSizeHex) ->
{ok, [Size], []} = io_lib:fread("~16u", binary_to_list(ChunkSizeHex)),
Size - 4.
readline(Port) ->
receive
{Port, {data, Data}} ->
% io:format("readline = ~p~n", [Data]),
{data, Data};
Msg ->
io:format("unknown message ~p~n", [Msg]),
{error, Msg}
after 15000 ->
io:format("timed out waiting for port~n"),
{error, timeout}
end.
extract_method_name(Header) ->
case regexp:match(Header, "....git[ -][a-z\-]+ ") of
{match, Start, Length} ->
{ok, string:substr(Header, Start + 8, Length - 9)};
_Else ->
invalid
end.
extract_host(Header) ->
case regexp:match(string:to_lower(Header), "\000host=[^\000]+\000") of
{match, Start, Length} ->
{ok, string:substr(Header, Start + 6, Length - 7)};
_Else ->
{ok, "invalid"}
end.
extract_repo_path(Header) ->
case regexp:match(Header, " /[^\000]+\000") of
{match, Start, Length} ->
{ok, string:substr(Header, Start + 2, Length - 3)};
_Else ->
invalid
end.
file_exists(FullPath) ->
case file:read_file_info(FullPath) of
{ok, _Info} ->
true;
{error, _Reason} ->
false
end.