Class GitSmartHttpTools


  • public class GitSmartHttpTools
    extends java.lang.Object
    Utility functions for handling the Git-over-HTTP protocol.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private static java.lang.String INFO_REFS  
      private static java.lang.String INFO_REFS_PATH  
      static java.lang.String RECEIVE_PACK
      Name of the git-receive-pack service.
      private static java.lang.String RECEIVE_PACK_PATH  
      static java.lang.String RECEIVE_PACK_REQUEST_TYPE
      Content type supplied by the client to the /git-receive-pack handler.
      static java.lang.String RECEIVE_PACK_RESULT_TYPE
      Content type returned from the /git-receive-pack handler.
      private static java.util.List<java.lang.String> SERVICE_SUFFIXES  
      static java.lang.String UPLOAD_PACK
      Name of the git-upload-pack service.
      private static java.lang.String UPLOAD_PACK_PATH  
      static java.lang.String UPLOAD_PACK_REQUEST_TYPE
      Content type supplied by the client to the /git-upload-pack handler.
      static java.lang.String UPLOAD_PACK_RESULT_TYPE
      Content type returned from the /git-upload-pack handler.
      static java.util.List<java.lang.String> VALID_SERVICES
      Git service names accepted by the /info/refs?service= handler.
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      private GitSmartHttpTools()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.lang.String getResponseContentType​(javax.servlet.http.HttpServletRequest req)
      Get the response Content-Type a client expects for the request.
      (package private) static java.lang.String infoRefsResultType​(java.lang.String svc)  
      static boolean isGitClient​(javax.servlet.http.HttpServletRequest req)
      Check a request for Git-over-HTTP indicators.
      static boolean isInfoRefs​(javax.servlet.http.HttpServletRequest req)
      Check if the HTTP request was for the /info/refs?service= Git handler.
      static boolean isReceivePack​(javax.servlet.http.HttpServletRequest req)
      Check if the HTTP request was for the /git-receive-pack Git handler.
      private static boolean isReceivePackSideBand​(javax.servlet.http.HttpServletRequest req)  
      static boolean isUploadPack​(java.lang.String pathOrUri)
      Check if the HTTP request path ends with the /git-upload-pack handler.
      static boolean isUploadPack​(javax.servlet.http.HttpServletRequest req)
      Check if the HTTP request was for the /git-upload-pack Git handler.
      private static void send​(javax.servlet.http.HttpServletRequest req, javax.servlet.http.HttpServletResponse res, java.lang.String type, byte[] buf)  
      static void sendError​(javax.servlet.http.HttpServletRequest req, javax.servlet.http.HttpServletResponse res, int httpStatus)
      Send an error to the Git client or browser.
      static void sendError​(javax.servlet.http.HttpServletRequest req, javax.servlet.http.HttpServletResponse res, int httpStatus, java.lang.String textForGit)
      Send an error to the Git client or browser.
      private static void sendInfoRefsError​(javax.servlet.http.HttpServletRequest req, javax.servlet.http.HttpServletResponse res, java.lang.String textForGit)  
      private static void sendReceivePackError​(javax.servlet.http.HttpServletRequest req, javax.servlet.http.HttpServletResponse res, java.lang.String textForGit)  
      private static void sendUploadPackError​(javax.servlet.http.HttpServletRequest req, javax.servlet.http.HttpServletResponse res, java.lang.String textForGit)  
      static java.lang.String stripServiceSuffix​(java.lang.String path)
      Strip the Git service suffix from a request path.
      private static void writePacket​(PacketLineOut pckOut, java.lang.String textForGit)  
      private static void writeSideBand​(java.io.OutputStream out, java.lang.String textForGit)  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • UPLOAD_PACK

        public static final java.lang.String UPLOAD_PACK
        Name of the git-upload-pack service.
        See Also:
        Constant Field Values
      • RECEIVE_PACK

        public static final java.lang.String RECEIVE_PACK
        Name of the git-receive-pack service.
        See Also:
        Constant Field Values
      • UPLOAD_PACK_REQUEST_TYPE

        public static final java.lang.String UPLOAD_PACK_REQUEST_TYPE
        Content type supplied by the client to the /git-upload-pack handler.
        See Also:
        Constant Field Values
      • UPLOAD_PACK_RESULT_TYPE

        public static final java.lang.String UPLOAD_PACK_RESULT_TYPE
        Content type returned from the /git-upload-pack handler.
        See Also:
        Constant Field Values
      • RECEIVE_PACK_REQUEST_TYPE

        public static final java.lang.String RECEIVE_PACK_REQUEST_TYPE
        Content type supplied by the client to the /git-receive-pack handler.
        See Also:
        Constant Field Values
      • RECEIVE_PACK_RESULT_TYPE

        public static final java.lang.String RECEIVE_PACK_RESULT_TYPE
        Content type returned from the /git-receive-pack handler.
        See Also:
        Constant Field Values
      • VALID_SERVICES

        public static final java.util.List<java.lang.String> VALID_SERVICES
        Git service names accepted by the /info/refs?service= handler.
      • UPLOAD_PACK_PATH

        private static final java.lang.String UPLOAD_PACK_PATH
        See Also:
        Constant Field Values
      • RECEIVE_PACK_PATH

        private static final java.lang.String RECEIVE_PACK_PATH
        See Also:
        Constant Field Values
      • SERVICE_SUFFIXES

        private static final java.util.List<java.lang.String> SERVICE_SUFFIXES
    • Constructor Detail

      • GitSmartHttpTools

        private GitSmartHttpTools()
    • Method Detail

      • isGitClient

        public static boolean isGitClient​(javax.servlet.http.HttpServletRequest req)
        Check a request for Git-over-HTTP indicators.
        Parameters:
        req - the current HTTP request that may have been made by Git.
        Returns:
        true if the request is likely made by a Git client program.
      • sendError

        public static void sendError​(javax.servlet.http.HttpServletRequest req,
                                     javax.servlet.http.HttpServletResponse res,
                                     int httpStatus)
                              throws java.io.IOException
        Send an error to the Git client or browser.

        Server implementors may use this method to send customized error messages to a Git protocol client using an HTTP 200 OK response with the error embedded in the payload. If the request was not issued by a Git client, an HTTP response code is returned instead.

        Parameters:
        req - current request.
        res - current response.
        httpStatus - HTTP status code to set if the client is not a Git client.
        Throws:
        java.io.IOException - the response cannot be sent.
      • sendError

        public static void sendError​(javax.servlet.http.HttpServletRequest req,
                                     javax.servlet.http.HttpServletResponse res,
                                     int httpStatus,
                                     java.lang.String textForGit)
                              throws java.io.IOException
        Send an error to the Git client or browser.

        Server implementors may use this method to send customized error messages to a Git protocol client using an HTTP 200 OK response with the error embedded in the payload. If the request was not issued by a Git client, an HTTP response code is returned instead.

        This method may only be called before handing off the request to UploadPack.upload(java.io.InputStream, OutputStream, OutputStream) or ReceivePack.receive(java.io.InputStream, OutputStream, OutputStream).

        Parameters:
        req - current request.
        res - current response.
        httpStatus - HTTP status code to set if the client is not a Git client.
        textForGit - plain text message to display on the user's console. This is shown only if the client is likely to be a Git client. If null or the empty string a default text is chosen based on the HTTP response code.
        Throws:
        java.io.IOException - the response cannot be sent.
      • sendInfoRefsError

        private static void sendInfoRefsError​(javax.servlet.http.HttpServletRequest req,
                                              javax.servlet.http.HttpServletResponse res,
                                              java.lang.String textForGit)
                                       throws java.io.IOException
        Throws:
        java.io.IOException
      • sendUploadPackError

        private static void sendUploadPackError​(javax.servlet.http.HttpServletRequest req,
                                                javax.servlet.http.HttpServletResponse res,
                                                java.lang.String textForGit)
                                         throws java.io.IOException
        Throws:
        java.io.IOException
      • sendReceivePackError

        private static void sendReceivePackError​(javax.servlet.http.HttpServletRequest req,
                                                 javax.servlet.http.HttpServletResponse res,
                                                 java.lang.String textForGit)
                                          throws java.io.IOException
        Throws:
        java.io.IOException
      • isReceivePackSideBand

        private static boolean isReceivePackSideBand​(javax.servlet.http.HttpServletRequest req)
      • writeSideBand

        private static void writeSideBand​(java.io.OutputStream out,
                                          java.lang.String textForGit)
                                   throws java.io.IOException
        Throws:
        java.io.IOException
      • writePacket

        private static void writePacket​(PacketLineOut pckOut,
                                        java.lang.String textForGit)
                                 throws java.io.IOException
        Throws:
        java.io.IOException
      • send

        private static void send​(javax.servlet.http.HttpServletRequest req,
                                 javax.servlet.http.HttpServletResponse res,
                                 java.lang.String type,
                                 byte[] buf)
                          throws java.io.IOException
        Throws:
        java.io.IOException
      • getResponseContentType

        public static java.lang.String getResponseContentType​(javax.servlet.http.HttpServletRequest req)
        Get the response Content-Type a client expects for the request.

        This method should only be invoked if isGitClient(HttpServletRequest) is true.

        Parameters:
        req - current request.
        Returns:
        the Content-Type the client expects.
        Throws:
        java.lang.IllegalArgumentException - the request is not a Git client request. See isGitClient(HttpServletRequest).
      • infoRefsResultType

        static java.lang.String infoRefsResultType​(java.lang.String svc)
      • stripServiceSuffix

        public static java.lang.String stripServiceSuffix​(java.lang.String path)
        Strip the Git service suffix from a request path. Generally the suffix is stripped by the SuffixPipeline handling the request, so this method is rarely needed.
        Parameters:
        path - the path of the request.
        Returns:
        the path up to the last path component before the service suffix; the path as-is if it contains no service suffix.
      • isInfoRefs

        public static boolean isInfoRefs​(javax.servlet.http.HttpServletRequest req)
        Check if the HTTP request was for the /info/refs?service= Git handler.
        Parameters:
        req - current request.
        Returns:
        true if the request is for the /info/refs service.
      • isUploadPack

        public static boolean isUploadPack​(java.lang.String pathOrUri)
        Check if the HTTP request path ends with the /git-upload-pack handler.
        Parameters:
        pathOrUri - path or URI of the request.
        Returns:
        true if the request is for the /git-upload-pack handler.
      • isUploadPack

        public static boolean isUploadPack​(javax.servlet.http.HttpServletRequest req)
        Check if the HTTP request was for the /git-upload-pack Git handler.
        Parameters:
        req - current request.
        Returns:
        true if the request is for the /git-upload-pack handler.
      • isReceivePack

        public static boolean isReceivePack​(javax.servlet.http.HttpServletRequest req)
        Check if the HTTP request was for the /git-receive-pack Git handler.
        Parameters:
        req - current request.
        Returns:
        true if the request is for the /git-receive-pack handler.