mirror of
				https://github.com/scm-manager/scm-manager.git
				synced 2025-11-03 20:15:52 +01:00 
			
		
		
		
	improve GitCGIServlet
This commit is contained in:
		@@ -14,6 +14,7 @@ import com.google.inject.Singleton;
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
import sonia.scm.repository.GitConfig;
 | 
					import sonia.scm.repository.GitConfig;
 | 
				
			||||||
import sonia.scm.repository.GitRepositoryHandler;
 | 
					import sonia.scm.repository.GitRepositoryHandler;
 | 
				
			||||||
 | 
					import sonia.scm.util.IOUtil;
 | 
				
			||||||
import sonia.scm.web.cgi.AbstractCGIServlet;
 | 
					import sonia.scm.web.cgi.AbstractCGIServlet;
 | 
				
			||||||
import sonia.scm.web.cgi.EnvList;
 | 
					import sonia.scm.web.cgi.EnvList;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -21,9 +22,12 @@ import sonia.scm.web.cgi.EnvList;
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
import java.io.File;
 | 
					import java.io.File;
 | 
				
			||||||
import java.io.IOException;
 | 
					import java.io.IOException;
 | 
				
			||||||
 | 
					import java.io.InputStream;
 | 
				
			||||||
 | 
					import java.io.OutputStream;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import javax.servlet.ServletException;
 | 
					import javax.servlet.ServletException;
 | 
				
			||||||
import javax.servlet.http.HttpServletRequest;
 | 
					import javax.servlet.http.HttpServletRequest;
 | 
				
			||||||
 | 
					import javax.servlet.http.HttpServletResponse;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/**
 | 
					/**
 | 
				
			||||||
 *
 | 
					 *
 | 
				
			||||||
@@ -39,6 +43,16 @@ public class GitCGIServlet extends AbstractCGIServlet
 | 
				
			|||||||
  /** Field description */
 | 
					  /** Field description */
 | 
				
			||||||
  public static final String ENV_PROJECT_ROOT = "GIT_PROJECT_ROOT";
 | 
					  public static final String ENV_PROJECT_ROOT = "GIT_PROJECT_ROOT";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  /** Field description */
 | 
				
			||||||
 | 
					  public static final String MIMETYPE_HTML = "text/html";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  /** Field description */
 | 
				
			||||||
 | 
					  public static final String REGEX_GITHTTPBACKEND =
 | 
				
			||||||
 | 
					    "(?x)^/git/(.*/(HEAD|info/refs|objects/(info/[^/]+|[0-9a-f]{2}/[0-9a-f]{38}|pack/pack-[0-9a-f]{40}\\.(pack|idx))|git-(upload|receive)-pack))$";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  /** Field description */
 | 
				
			||||||
 | 
					  public static final String RESOURCE_GITINDEX = "/sonia/scm/git.index.html";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  /** Field description */
 | 
					  /** Field description */
 | 
				
			||||||
  private static final long serialVersionUID = 9147517765161830847L;
 | 
					  private static final long serialVersionUID = 9147517765161830847L;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -90,13 +104,40 @@ public class GitCGIServlet extends AbstractCGIServlet
 | 
				
			|||||||
    return env;
 | 
					    return env;
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  /**
 | 
				
			||||||
 | 
					   * Method description
 | 
				
			||||||
 | 
					   *
 | 
				
			||||||
 | 
					   *
 | 
				
			||||||
 | 
					   * @param request
 | 
				
			||||||
 | 
					   * @param response
 | 
				
			||||||
 | 
					   *
 | 
				
			||||||
 | 
					   * @throws IOException
 | 
				
			||||||
 | 
					   * @throws ServletException
 | 
				
			||||||
 | 
					   */
 | 
				
			||||||
 | 
					  @Override
 | 
				
			||||||
 | 
					  protected void service(HttpServletRequest request,
 | 
				
			||||||
 | 
					                         HttpServletResponse response)
 | 
				
			||||||
 | 
					          throws ServletException, IOException
 | 
				
			||||||
 | 
					  {
 | 
				
			||||||
 | 
					    String uri = getRelativePath(request);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    if (uri.matches(REGEX_GITHTTPBACKEND))
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					      super.service(request, response);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    else
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					      printGitInformation(response);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  //~--- get methods ----------------------------------------------------------
 | 
					  //~--- get methods ----------------------------------------------------------
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  /**
 | 
					  /**
 | 
				
			||||||
   * Method description
 | 
					   * Method description
 | 
				
			||||||
   *
 | 
					   *
 | 
				
			||||||
   *
 | 
					   *
 | 
				
			||||||
   * @param req
 | 
					   * @param request
 | 
				
			||||||
   *
 | 
					   *
 | 
				
			||||||
   * @return
 | 
					   * @return
 | 
				
			||||||
   *
 | 
					   *
 | 
				
			||||||
@@ -104,7 +145,7 @@ public class GitCGIServlet extends AbstractCGIServlet
 | 
				
			|||||||
   * @throws ServletException
 | 
					   * @throws ServletException
 | 
				
			||||||
   */
 | 
					   */
 | 
				
			||||||
  @Override
 | 
					  @Override
 | 
				
			||||||
  protected File getCommand(HttpServletRequest req)
 | 
					  protected File getCommand(HttpServletRequest request)
 | 
				
			||||||
          throws ServletException, IOException
 | 
					          throws ServletException, IOException
 | 
				
			||||||
  {
 | 
					  {
 | 
				
			||||||
    GitConfig config = repositoryHandler.getConfig();
 | 
					    GitConfig config = repositoryHandler.getConfig();
 | 
				
			||||||
@@ -117,6 +158,53 @@ public class GitCGIServlet extends AbstractCGIServlet
 | 
				
			|||||||
    return new File(config.getGitHttpBackend());
 | 
					    return new File(config.getGitHttpBackend());
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  //~--- methods --------------------------------------------------------------
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  /**
 | 
				
			||||||
 | 
					   * Method description
 | 
				
			||||||
 | 
					   *
 | 
				
			||||||
 | 
					   *
 | 
				
			||||||
 | 
					   * @param response
 | 
				
			||||||
 | 
					   *
 | 
				
			||||||
 | 
					   * @throws IOException
 | 
				
			||||||
 | 
					   * @throws ServletException
 | 
				
			||||||
 | 
					   */
 | 
				
			||||||
 | 
					  private void printGitInformation(HttpServletResponse response)
 | 
				
			||||||
 | 
					          throws ServletException, IOException
 | 
				
			||||||
 | 
					  {
 | 
				
			||||||
 | 
					    response.setContentType(MIMETYPE_HTML);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    InputStream input = null;
 | 
				
			||||||
 | 
					    OutputStream output = null;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    try
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					      input = GitCGIServlet.class.getResourceAsStream(RESOURCE_GITINDEX);
 | 
				
			||||||
 | 
					      output = response.getOutputStream();
 | 
				
			||||||
 | 
					      IOUtil.copy(input, output);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    finally
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					      IOUtil.close(input);
 | 
				
			||||||
 | 
					      IOUtil.close(output);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  //~--- get methods ----------------------------------------------------------
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  /**
 | 
				
			||||||
 | 
					   * Method description
 | 
				
			||||||
 | 
					   *
 | 
				
			||||||
 | 
					   *
 | 
				
			||||||
 | 
					   * @param request
 | 
				
			||||||
 | 
					   *
 | 
				
			||||||
 | 
					   * @return
 | 
				
			||||||
 | 
					   */
 | 
				
			||||||
 | 
					  private String getRelativePath(HttpServletRequest request)
 | 
				
			||||||
 | 
					  {
 | 
				
			||||||
 | 
					    return request.getRequestURI().substring(request.getContextPath().length());
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  //~--- fields ---------------------------------------------------------------
 | 
					  //~--- fields ---------------------------------------------------------------
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  /** Field description */
 | 
					  /** Field description */
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -0,0 +1,58 @@
 | 
				
			|||||||
 | 
					<html>
 | 
				
			||||||
 | 
					  <head>
 | 
				
			||||||
 | 
					    <title>SCM :: Manager - Git Repository</title>
 | 
				
			||||||
 | 
					    <style type="text/css">
 | 
				
			||||||
 | 
					      body {
 | 
				
			||||||
 | 
					        background-color: #ffffff;
 | 
				
			||||||
 | 
					        font-size: 12px;
 | 
				
			||||||
 | 
					        font-family: Verdana, "Verdana CE",  Arial, "Arial CE", "Lucida Grande CE", lucida, "Helvetica CE", sans-serif;
 | 
				
			||||||
 | 
					        color: #000000;
 | 
				
			||||||
 | 
					        margin: 10px;
 | 
				
			||||||
 | 
					      }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					      h1, h2, h3, h4, h5 {
 | 
				
			||||||
 | 
					        font-family: Arial, "Arial CE", "Lucida Grande CE", lucida, "Helvetica CE", sans-serif;
 | 
				
			||||||
 | 
					        font-weight: bold;
 | 
				
			||||||
 | 
					        margin: 0px;
 | 
				
			||||||
 | 
					        padding: 0px;
 | 
				
			||||||
 | 
					        color: #D20005;
 | 
				
			||||||
 | 
					      }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					      h1 {
 | 
				
			||||||
 | 
					        font-size:  18px;
 | 
				
			||||||
 | 
					        border-bottom: 1px solid #AFAFAF;
 | 
				
			||||||
 | 
					      }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					      h2 {
 | 
				
			||||||
 | 
					        font-size:  16px;
 | 
				
			||||||
 | 
					      }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					      h3 {
 | 
				
			||||||
 | 
					        font-size:  14px;
 | 
				
			||||||
 | 
					      }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					      a:link, a:visited {
 | 
				
			||||||
 | 
					        color: #045491;
 | 
				
			||||||
 | 
					        font-weight : bold;
 | 
				
			||||||
 | 
					        text-decoration: none;
 | 
				
			||||||
 | 
					      }
 | 
				
			||||||
 | 
					      
 | 
				
			||||||
 | 
					      a:link:hover, a:visited:hover  {
 | 
				
			||||||
 | 
					        color: #045491;
 | 
				
			||||||
 | 
					        font-weight : bold;
 | 
				
			||||||
 | 
					        text-decoration : underline;
 | 
				
			||||||
 | 
					      }
 | 
				
			||||||
 | 
					    </style>
 | 
				
			||||||
 | 
					  </head>
 | 
				
			||||||
 | 
					  <body>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    <h1>SCM :: Manager - Git Repository</h1>
 | 
				
			||||||
 | 
					    
 | 
				
			||||||
 | 
					    <ul>
 | 
				
			||||||
 | 
					      <li><a href="http://git-scm.com/" target="_blank">Git Homepage</a></li>
 | 
				
			||||||
 | 
					      <li><a href="http://www.kernel.org/pub/software/scm/git/docs/gittutorial.html" target="_blank">Git Tutorial</a></li>
 | 
				
			||||||
 | 
					      <li><a href="http://www.kernel.org/pub/software/scm/git/docs/user-manual.html" target="_blank">Git User's Manual</a></li>
 | 
				
			||||||
 | 
					    </ul>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  </body>
 | 
				
			||||||
 | 
					</html>
 | 
				
			||||||
		Reference in New Issue
	
	Block a user