View Javadoc
1   /*
2    * Copyright (C) 2008-2009, Google Inc. and others
3    *
4    * This program and the accompanying materials are made available under the
5    * terms of the Eclipse Distribution License v. 1.0 which is available at
6    * https://www.eclipse.org/org/documents/edl-v10.php.
7    *
8    * SPDX-License-Identifier: BSD-3-Clause
9    */
10  
11  package org.eclipse.jgit.transport;
12  
13  import java.io.BufferedInputStream;
14  import java.io.BufferedOutputStream;
15  import java.io.IOException;
16  import java.io.InputStream;
17  import java.io.OutputStream;
18  import java.net.InetAddress;
19  import java.net.Socket;
20  import java.util.Arrays;
21  import java.util.Collection;
22  
23  import org.eclipse.jgit.transport.resolver.ServiceNotAuthorizedException;
24  import org.eclipse.jgit.transport.resolver.ServiceNotEnabledException;
25  
26  /**
27   * Active network client of {@link org.eclipse.jgit.transport.Daemon}.
28   */
29  public class DaemonClient {
30  	private final Daemon daemon;
31  
32  	private InetAddress peer;
33  
34  	private InputStream rawIn;
35  
36  	private OutputStream rawOut;
37  
38  	DaemonClient(Daemon d) {
39  		daemon = d;
40  	}
41  
42  	void setRemoteAddress(InetAddress ia) {
43  		peer = ia;
44  	}
45  
46  	/**
47  	 * Get the daemon which spawned this client.
48  	 *
49  	 * @return the daemon which spawned this client.
50  	 */
51  	public Daemon getDaemon() {
52  		return daemon;
53  	}
54  
55  	/**
56  	 * Get Internet address of the remote client.
57  	 *
58  	 * @return Internet address of the remote client.
59  	 */
60  	public InetAddress getRemoteAddress() {
61  		return peer;
62  	}
63  
64  	/**
65  	 * Get input stream to read from the connected client.
66  	 *
67  	 * @return input stream to read from the connected client.
68  	 */
69  	public InputStream getInputStream() {
70  		return rawIn;
71  	}
72  
73  	/**
74  	 * Get output stream to send data to the connected client.
75  	 *
76  	 * @return output stream to send data to the connected client.
77  	 */
78  	public OutputStream getOutputStream() {
79  		return rawOut;
80  	}
81  
82  	void execute(Socket sock) throws IOException,
83  			ServiceNotEnabledException, ServiceNotAuthorizedException {
84  		rawIn = new BufferedInputStream(sock.getInputStream());
85  		rawOut = new BufferedOutputStream(sock.getOutputStream());
86  
87  		if (0 < daemon.getTimeout())
88  			sock.setSoTimeout(daemon.getTimeout() * 1000);
89  		String cmd = new PacketLineIn(rawIn).readStringRaw();
90  
91  		Collection<String> extraParameters = null;
92  
93  		int nulnul = cmd.indexOf("\0\0"); //$NON-NLS-1$
94  		if (nulnul != -1) {
95  			extraParameters = Arrays.asList(cmd.substring(nulnul + 2).split("\0")); //$NON-NLS-1$
96  		}
97  
98  		final int nul = cmd.indexOf('\0');
99  		if (nul >= 0) {
100 			// Newer clients hide a "host" header behind this byte.
101 			// Currently we don't use it for anything, so we ignore
102 			// this portion of the command.
103 			//
104 			cmd = cmd.substring(0, nul);
105 		}
106 
107 		final DaemonService srv = getDaemon().matchService(cmd);
108 		if (srv == null)
109 			return;
110 		sock.setSoTimeout(0);
111 		srv.execute(this, cmd, extraParameters);
112 	}
113 }