James Moger
2013-07-02 2e4b03f7fe33ed5b84ec98ce689f3e1cabf97bff
commit | author | age
40aa84 1 package com.gitblit.git;
JM 2
3 /*
4  * Copyright (C) 2008-2009, Google Inc.
5  * and other copyright owners as documented in the project's IP log.
6  *
7  * This program and the accompanying materials are made available
8  * under the terms of the Eclipse Distribution License v1.0 which
9  * accompanies this distribution, is reproduced below, and is
10  * available at http://www.eclipse.org/org/documents/edl-v10.php
11  *
12  * All rights reserved.
13  *
14  * Redistribution and use in source and binary forms, with or
15  * without modification, are permitted provided that the following
16  * conditions are met:
17  *
18  * - Redistributions of source code must retain the above copyright
19  *   notice, this list of conditions and the following disclaimer.
20  *
21  * - Redistributions in binary form must reproduce the above
22  *   copyright notice, this list of conditions and the following
23  *   disclaimer in the documentation and/or other materials provided
24  *   with the distribution.
25  *
26  * - Neither the name of the Eclipse Foundation, Inc. nor the
27  *   names of its contributors may be used to endorse or promote
28  *   products derived from this software without specific prior
29  *   written permission.
30  *
31  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
32  * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
33  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
34  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
35  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
36  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
37  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
38  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
39  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
40  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
41  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
42  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
43  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
44  */
45
46 import java.io.BufferedInputStream;
47 import java.io.IOException;
48 import java.io.InputStream;
49 import java.io.OutputStream;
50 import java.net.InetAddress;
51 import java.net.Socket;
52
6ef8d7 53 import org.eclipse.jgit.transport.Daemon;
40aa84 54 import org.eclipse.jgit.transport.PacketLineIn;
JM 55 import org.eclipse.jgit.transport.resolver.ServiceNotAuthorizedException;
56 import org.eclipse.jgit.transport.resolver.ServiceNotEnabledException;
57 import org.eclipse.jgit.util.io.SafeBufferedOutputStream;
58
59 /** Active network client of {@link Daemon}. */
60 public class GitDaemonClient {
61     private final GitDaemon daemon;
62
63     private InetAddress peer;
64
65     private InputStream rawIn;
66
67     private OutputStream rawOut;
68     
69     private String repositoryName;
70
71     GitDaemonClient(final GitDaemon d) {
72         daemon = d;
73     }
74
75     void setRemoteAddress(final InetAddress ia) {
76         peer = ia;
77     }
78
79     /** @return the daemon which spawned this client. */
80     public GitDaemon getDaemon() {
81         return daemon;
82     }
83
84     /** @return Internet address of the remote client. */
85     public InetAddress getRemoteAddress() {
86         return peer;
87     }
88
89     /** @return input stream to read from the connected client. */
90     public InputStream getInputStream() {
91         return rawIn;
92     }
93
94     /** @return output stream to send data to the connected client. */
95     public OutputStream getOutputStream() {
96         return rawOut;
97     }
98     
99     public void setRepositoryName(String repositoryName) {
100         this.repositoryName = repositoryName;
101     }
102     
103     /** @return the name of the requested repository. */
104     public String getRepositoryName() {
105         return repositoryName;
106     }
107
108     void execute(final Socket sock) throws IOException,
109             ServiceNotEnabledException, ServiceNotAuthorizedException {
110         rawIn = new BufferedInputStream(sock.getInputStream());
111         rawOut = new SafeBufferedOutputStream(sock.getOutputStream());
112
113         if (0 < daemon.getTimeout())
114             sock.setSoTimeout(daemon.getTimeout() * 1000);
115         String cmd = new PacketLineIn(rawIn).readStringRaw();
116         final int nul = cmd.indexOf('\0');
117         if (nul >= 0) {
118             // Newer clients hide a "host" header behind this byte.
119             // Currently we don't use it for anything, so we ignore
120             // this portion of the command.
121             //
122             cmd = cmd.substring(0, nul);
123         }
124
125         final GitDaemonService srv = getDaemon().matchService(cmd);
126         if (srv == null)
127             return;
128         sock.setSoTimeout(0);
129         srv.execute(this, cmd);
130     }
131 }