/** * Send an error to the Git client or browser. * <p> * 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. * * @param req * current request. * @param res * current response. * @param httpStatus * HTTP status code to set if the client is not a Git client. * @throws IOException * the response cannot be sent. */ public static void sendError(HttpServletRequest req, HttpServletResponse res, int httpStatus) throws IOException { sendError(req, res, httpStatus, null); }
/** * Get the response Content-Type a client expects for the request. * <p> * This method should only be invoked if * {@link #isGitClient(HttpServletRequest)} is true. * * @param req * current request. * @return the Content-Type the client expects. * @throws IllegalArgumentException * the request is not a Git client request. See * {@link #isGitClient(HttpServletRequest)}. */ public static String getResponseContentType(HttpServletRequest req) { if (isInfoRefs(req)) return infoRefsResultType(req.getParameter("service")); else if (isUploadPack(req)) return UPLOAD_PACK_RESULT_TYPE; else if (isReceivePack(req)) return RECEIVE_PACK_RESULT_TYPE; else throw new IllegalArgumentException(); }
private static void sendReceivePackError(HttpServletRequest req, HttpServletResponse res, String textForGit) throws IOException { ByteArrayOutputStream buf = new ByteArrayOutputStream(128); PacketLineOut pckOut = new PacketLineOut(buf); boolean sideband; ReceivePack rp = (ReceivePack) req.getAttribute(ATTRIBUTE_HANDLER); if (rp != null) { try { sideband = rp.isSideBand(); } catch (RequestNotYetReadException e) { sideband = isReceivePackSideBand(req); } } else sideband = isReceivePackSideBand(req); if (sideband) writeSideBand(buf, textForGit); else writePacket(pckOut, textForGit); send(req, res, RECEIVE_PACK_RESULT_TYPE, buf.toByteArray()); }
/** * Check a request for Git-over-HTTP indicators. * * @param req * the current HTTP request that may have been made by Git. * @return true if the request is likely made by a Git client program. */ public static boolean isGitClient(HttpServletRequest req) { return isInfoRefs(req) || isUploadPack(req) || isReceivePack(req); }
if (isInfoRefs(req)) { sendInfoRefsError(req, res, textForGit); } else if (isUploadPack(req)) { sendUploadPackError(req, res, textForGit); } else if (isReceivePack(req)) { sendReceivePackError(req, res, textForGit); } else { if (httpStatus < 400)
@Sessional @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { HttpServletRequest httpRequest = (HttpServletRequest) request; HttpServletResponse httpResponse = (HttpServletResponse) response; try { if (GitSmartHttpTools.isInfoRefs(httpRequest)) { if (oneDev.isReady()) processRefs(httpRequest, httpResponse); else throw new GitException("Server is not ready"); } else if (GitSmartHttpTools.isReceivePack(httpRequest) || GitSmartHttpTools.isUploadPack(httpRequest)) { if (oneDev.isReady()) processPacks(httpRequest, httpResponse); else throw new GitException("Server is not ready"); } else { chain.doFilter(request, response); } } catch (GitException|InterruptedException|ExecutionException e) { logger.error("Error serving git request", e); GitSmartHttpTools.sendError(httpRequest, httpResponse, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e.getMessage()); } }
private static void sendUploadPackError(HttpServletRequest req, HttpServletResponse res, String textForGit) throws IOException { ByteArrayOutputStream buf = new ByteArrayOutputStream(128); PacketLineOut pckOut = new PacketLineOut(buf); boolean sideband; UploadPack up = (UploadPack) req.getAttribute(ATTRIBUTE_HANDLER); if (up != null) { try { sideband = up.isSideBand(); } catch (RequestNotYetReadException e) { sideband = isUploadPackSideBand(req); } } else sideband = isUploadPackSideBand(req); if (sideband) writeSideBand(buf, textForGit); else writePacket(pckOut, textForGit); send(req, res, UPLOAD_PACK_RESULT_TYPE, buf.toByteArray()); }
private void service(ServletRequest request, ServletResponse response) throws IOException { final HttpServletRequest req = (HttpServletRequest) request; final HttpServletResponse res = (HttpServletResponse) response; final SmartOutputStream buf = new SmartOutputStream(req, res, true); try { res.setContentType(infoRefsResultType(svc)); final PacketLineOut out = new PacketLineOut(buf); out.writeString("# service=" + svc + "\n"); out.end(); advertise(req, new PacketLineOutRefAdvertiser(out)); buf.close(); } catch (ServiceNotAuthorizedException e) { res.sendError(SC_UNAUTHORIZED); } catch (ServiceNotEnabledException e) { sendError(req, res, SC_FORBIDDEN); } catch (ServiceMayNotContinueException e) { if (e.isOutput()) buf.close(); else sendError(req, res, SC_FORBIDDEN, e.getMessage()); } }
private static void sendInfoRefsError(HttpServletRequest req, HttpServletResponse res, String textForGit) throws IOException { ByteArrayOutputStream buf = new ByteArrayOutputStream(128); PacketLineOut pck = new PacketLineOut(buf); String svc = req.getParameter("service"); pck.writeString("# service=" + svc + "\n"); pck.end(); pck.writeString("ERR " + textForGit); send(req, res, infoRefsResultType(svc), buf.toByteArray()); }
/** * Check if the HTTP request was for the /git-upload-pack Git handler. * * @param req * current request. * @return true if the request is for the /git-upload-pack handler. */ public static boolean isUploadPack(HttpServletRequest req) { return isUploadPack(req.getRequestURI()) && UPLOAD_PACK_REQUEST_TYPE.equals(req.getContentType()); }
if (isInfoRefs(req)) { sendInfoRefsError(req, res, textForGit); } else if (isUploadPack(req)) { sendUploadPackError(req, res, textForGit); } else if (isReceivePack(req)) { sendReceivePackError(req, res, textForGit); } else { if (httpStatus < 400)
/** * Check a request for Git-over-HTTP indicators. * * @param req * the current HTTP request that may have been made by Git. * @return true if the request is likely made by a Git client program. */ public static boolean isGitClient(HttpServletRequest req) { return isInfoRefs(req) || isUploadPack(req) || isReceivePack(req); }
private static void sendUploadPackError(HttpServletRequest req, HttpServletResponse res, String textForGit) throws IOException { ByteArrayOutputStream buf = new ByteArrayOutputStream(128); PacketLineOut pckOut = new PacketLineOut(buf); boolean sideband; UploadPack up = (UploadPack) req.getAttribute(ATTRIBUTE_HANDLER); if (up != null) { try { sideband = up.isSideBand(); } catch (RequestNotYetReadException e) { sideband = isUploadPackSideBand(req); } } else sideband = isUploadPackSideBand(req); if (sideband) writeSideBand(buf, textForGit); else writePacket(pckOut, textForGit); send(req, res, UPLOAD_PACK_RESULT_TYPE, buf.toByteArray()); }
private void service(ServletRequest request, ServletResponse response) throws IOException { final HttpServletRequest req = (HttpServletRequest) request; final HttpServletResponse res = (HttpServletResponse) response; final SmartOutputStream buf = new SmartOutputStream(req, res, true); try { res.setContentType(infoRefsResultType(svc)); final PacketLineOut out = new PacketLineOut(buf); out.writeString("# service=" + svc + "\n"); out.end(); advertise(req, new PacketLineOutRefAdvertiser(out)); buf.close(); } catch (ServiceNotAuthorizedException e) { res.sendError(SC_UNAUTHORIZED, e.getMessage()); } catch (ServiceNotEnabledException e) { sendError(req, res, SC_FORBIDDEN, e.getMessage()); } catch (ServiceMayNotContinueException e) { if (e.isOutput()) buf.close(); else sendError(req, res, e.getStatusCode(), e.getMessage()); } }
private static void sendInfoRefsError(HttpServletRequest req, HttpServletResponse res, String textForGit) throws IOException { ByteArrayOutputStream buf = new ByteArrayOutputStream(128); PacketLineOut pck = new PacketLineOut(buf); String svc = req.getParameter("service"); pck.writeString("# service=" + svc + "\n"); pck.end(); pck.writeString("ERR " + textForGit); send(req, res, infoRefsResultType(svc), buf.toByteArray()); }
/** * Check if the HTTP request was for the /git-upload-pack Git handler. * * @param req * current request. * @return true if the request is for the /git-upload-pack handler. */ public static boolean isUploadPack(HttpServletRequest req) { return isUploadPack(req.getRequestURI()) && UPLOAD_PACK_REQUEST_TYPE.equals(req.getContentType()); }
/** * Get the response Content-Type a client expects for the request. * <p> * This method should only be invoked if * {@link #isGitClient(HttpServletRequest)} is true. * * @param req * current request. * @return the Content-Type the client expects. * @throws IllegalArgumentException * the request is not a Git client request. See * {@link #isGitClient(HttpServletRequest)}. */ public static String getResponseContentType(HttpServletRequest req) { if (isInfoRefs(req)) return infoRefsResultType(req.getParameter("service")); else if (isUploadPack(req)) return UPLOAD_PACK_RESULT_TYPE; else if (isReceivePack(req)) return RECEIVE_PACK_RESULT_TYPE; else throw new IllegalArgumentException(); }
/** * Send an error to the Git client or browser. * <p> * 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. * * @param req * current request. * @param res * current response. * @param httpStatus * HTTP status code to set if the client is not a Git client. * @throws IOException * the response cannot be sent. */ public static void sendError(HttpServletRequest req, HttpServletResponse res, int httpStatus) throws IOException { sendError(req, res, httpStatus, null); }
private static void sendReceivePackError(HttpServletRequest req, HttpServletResponse res, String textForGit) throws IOException { ByteArrayOutputStream buf = new ByteArrayOutputStream(128); PacketLineOut pckOut = new PacketLineOut(buf); boolean sideband; ReceivePack rp = (ReceivePack) req.getAttribute(ATTRIBUTE_HANDLER); if (rp != null) { try { sideband = rp.isSideBand(); } catch (RequestNotYetReadException e) { sideband = isReceivePackSideBand(req); } } else sideband = isReceivePackSideBand(req); if (sideband) writeSideBand(buf, textForGit); else writePacket(pckOut, textForGit); send(req, res, RECEIVE_PACK_RESULT_TYPE, buf.toByteArray()); }
if (GitSmartHttpTools.isUploadPack(request)) { if (!SecurityUtils.canReadCode(project.getFacade())) throw new UnauthorizedException("You do not have permission to pull from this project.");