DaemonClient.java

  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. package org.eclipse.jgit.transport;

  11. import java.io.BufferedInputStream;
  12. import java.io.BufferedOutputStream;
  13. import java.io.IOException;
  14. import java.io.InputStream;
  15. import java.io.OutputStream;
  16. import java.net.InetAddress;
  17. import java.net.Socket;
  18. import java.util.Arrays;
  19. import java.util.Collection;

  20. import org.eclipse.jgit.transport.resolver.ServiceNotAuthorizedException;
  21. import org.eclipse.jgit.transport.resolver.ServiceNotEnabledException;

  22. /**
  23.  * Active network client of {@link org.eclipse.jgit.transport.Daemon}.
  24.  */
  25. public class DaemonClient {
  26.     private final Daemon daemon;

  27.     private InetAddress peer;

  28.     private InputStream rawIn;

  29.     private OutputStream rawOut;

  30.     DaemonClient(Daemon d) {
  31.         daemon = d;
  32.     }

  33.     void setRemoteAddress(InetAddress ia) {
  34.         peer = ia;
  35.     }

  36.     /**
  37.      * Get the daemon which spawned this client.
  38.      *
  39.      * @return the daemon which spawned this client.
  40.      */
  41.     public Daemon getDaemon() {
  42.         return daemon;
  43.     }

  44.     /**
  45.      * Get Internet address of the remote client.
  46.      *
  47.      * @return Internet address of the remote client.
  48.      */
  49.     public InetAddress getRemoteAddress() {
  50.         return peer;
  51.     }

  52.     /**
  53.      * Get input stream to read from the connected client.
  54.      *
  55.      * @return input stream to read from the connected client.
  56.      */
  57.     public InputStream getInputStream() {
  58.         return rawIn;
  59.     }

  60.     /**
  61.      * Get output stream to send data to the connected client.
  62.      *
  63.      * @return output stream to send data to the connected client.
  64.      */
  65.     public OutputStream getOutputStream() {
  66.         return rawOut;
  67.     }

  68.     void execute(Socket sock) throws IOException,
  69.             ServiceNotEnabledException, ServiceNotAuthorizedException {
  70.         rawIn = new BufferedInputStream(sock.getInputStream());
  71.         rawOut = new BufferedOutputStream(sock.getOutputStream());

  72.         if (0 < daemon.getTimeout())
  73.             sock.setSoTimeout(daemon.getTimeout() * 1000);
  74.         String cmd = new PacketLineIn(rawIn).readStringRaw();

  75.         Collection<String> extraParameters = null;

  76.         int nulnul = cmd.indexOf("\0\0"); //$NON-NLS-1$
  77.         if (nulnul != -1) {
  78.             extraParameters = Arrays.asList(cmd.substring(nulnul + 2).split("\0")); //$NON-NLS-1$
  79.         }

  80.         final int nul = cmd.indexOf('\0');
  81.         if (nul >= 0) {
  82.             // Newer clients hide a "host" header behind this byte.
  83.             // Currently we don't use it for anything, so we ignore
  84.             // this portion of the command.
  85.             //
  86.             cmd = cmd.substring(0, nul);
  87.         }

  88.         final DaemonService srv = getDaemon().matchService(cmd);
  89.         if (srv == null)
  90.             return;
  91.         sock.setSoTimeout(0);
  92.         srv.execute(this, cmd, extraParameters);
  93.     }
  94. }