summaryrefslogtreecommitdiffstats
path: root/org.eclipse.jgit.http.server
diff options
context:
space:
mode:
authorMatthias Sohn <matthias.sohn@sap.com>2022-05-09 22:09:05 +0200
committerMatthias Sohn <matthias.sohn@sap.com>2022-05-09 22:09:05 +0200
commite1efd4dd1fd4d2683c3340d005d8a965f987b00b (patch)
tree3d11bbcab23b8af1936eb4148471c9b0fe8fc8e9 /org.eclipse.jgit.http.server
parentb969c44c00895a4ffe532502be535aee4d5d2444 (diff)
parent8984e1f663b5b58f74cd0381043133f0059e004d (diff)
downloadjgit-e1efd4dd1fd4d2683c3340d005d8a965f987b00b.tar.gz
jgit-e1efd4dd1fd4d2683c3340d005d8a965f987b00b.zip
Merge branch 'stable-6.1' into stable-6.2
* stable-6.1: HTTP Smart: set correct HTTP status on error Change-Id: I792d6cdfe9e76a3d2e6d1e01ec1dc96805ed2900
Diffstat (limited to 'org.eclipse.jgit.http.server')
-rw-r--r--org.eclipse.jgit.http.server/src/org/eclipse/jgit/http/server/GitSmartHttpTools.java25
-rw-r--r--org.eclipse.jgit.http.server/src/org/eclipse/jgit/http/server/UploadPackServlet.java21
2 files changed, 32 insertions, 14 deletions
diff --git a/org.eclipse.jgit.http.server/src/org/eclipse/jgit/http/server/GitSmartHttpTools.java b/org.eclipse.jgit.http.server/src/org/eclipse/jgit/http/server/GitSmartHttpTools.java
index 012f9a3dc0..f1155dcf57 100644
--- a/org.eclipse.jgit.http.server/src/org/eclipse/jgit/http/server/GitSmartHttpTools.java
+++ b/org.eclipse.jgit.http.server/src/org/eclipse/jgit/http/server/GitSmartHttpTools.java
@@ -158,11 +158,11 @@ public class GitSmartHttpTools {
}
if (isInfoRefs(req)) {
- sendInfoRefsError(req, res, textForGit);
+ sendInfoRefsError(req, res, textForGit, httpStatus);
} else if (isUploadPack(req)) {
- sendUploadPackError(req, res, textForGit);
+ sendUploadPackError(req, res, textForGit, httpStatus);
} else if (isReceivePack(req)) {
- sendReceivePackError(req, res, textForGit);
+ sendReceivePackError(req, res, textForGit, httpStatus);
} else {
if (httpStatus < 400)
ServletUtils.consumeRequestBody(req);
@@ -171,29 +171,32 @@ public class GitSmartHttpTools {
}
private static void sendInfoRefsError(HttpServletRequest req,
- HttpServletResponse res, String textForGit) throws IOException {
+ HttpServletResponse res, String textForGit, int httpStatus)
+ 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());
+ send(req, res, infoRefsResultType(svc), buf.toByteArray(), httpStatus);
}
private static void sendUploadPackError(HttpServletRequest req,
- HttpServletResponse res, String textForGit) throws IOException {
+ HttpServletResponse res, String textForGit, int httpStatus)
+ throws IOException {
// Do not use sideband. Sideband is acceptable only while packfile is
// being sent. Other places, like acknowledgement section, do not
// support sideband. Use an error packet.
ByteArrayOutputStream buf = new ByteArrayOutputStream(128);
PacketLineOut pckOut = new PacketLineOut(buf);
writePacket(pckOut, textForGit);
- send(req, res, UPLOAD_PACK_RESULT_TYPE, buf.toByteArray());
+ send(req, res, UPLOAD_PACK_RESULT_TYPE, buf.toByteArray(), httpStatus);
}
private static void sendReceivePackError(HttpServletRequest req,
- HttpServletResponse res, String textForGit) throws IOException {
+ HttpServletResponse res, String textForGit, int httpStatus)
+ throws IOException {
ByteArrayOutputStream buf = new ByteArrayOutputStream(128);
PacketLineOut pckOut = new PacketLineOut(buf);
@@ -212,7 +215,7 @@ public class GitSmartHttpTools {
writeSideBand(buf, textForGit);
else
writePacket(pckOut, textForGit);
- send(req, res, RECEIVE_PACK_RESULT_TYPE, buf.toByteArray());
+ send(req, res, RECEIVE_PACK_RESULT_TYPE, buf.toByteArray(), httpStatus);
}
private static boolean isReceivePackSideBand(HttpServletRequest req) {
@@ -246,9 +249,9 @@ public class GitSmartHttpTools {
}
private static void send(HttpServletRequest req, HttpServletResponse res,
- String type, byte[] buf) throws IOException {
+ String type, byte[] buf, int httpStatus) throws IOException {
ServletUtils.consumeRequestBody(req);
- res.setStatus(HttpServletResponse.SC_OK);
+ res.setStatus(httpStatus);
res.setContentType(type);
res.setContentLength(buf.length);
try (OutputStream os = res.getOutputStream()) {
diff --git a/org.eclipse.jgit.http.server/src/org/eclipse/jgit/http/server/UploadPackServlet.java b/org.eclipse.jgit.http.server/src/org/eclipse/jgit/http/server/UploadPackServlet.java
index 77ed09cdfb..d3437b7eb9 100644
--- a/org.eclipse.jgit.http.server/src/org/eclipse/jgit/http/server/UploadPackServlet.java
+++ b/org.eclipse.jgit.http.server/src/org/eclipse/jgit/http/server/UploadPackServlet.java
@@ -10,6 +10,7 @@
package org.eclipse.jgit.http.server;
+import static javax.servlet.http.HttpServletResponse.SC_BAD_REQUEST;
import static javax.servlet.http.HttpServletResponse.SC_FORBIDDEN;
import static javax.servlet.http.HttpServletResponse.SC_INTERNAL_SERVER_ERROR;
import static javax.servlet.http.HttpServletResponse.SC_UNAUTHORIZED;
@@ -48,6 +49,7 @@ import org.eclipse.jgit.transport.RefAdvertiser.PacketLineOutRefAdvertiser;
import org.eclipse.jgit.transport.ServiceMayNotContinueException;
import org.eclipse.jgit.transport.UploadPack;
import org.eclipse.jgit.transport.UploadPackInternalServerErrorException;
+import org.eclipse.jgit.transport.WantNotValidException;
import org.eclipse.jgit.transport.resolver.ServiceNotAuthorizedException;
import org.eclipse.jgit.transport.resolver.ServiceNotEnabledException;
import org.eclipse.jgit.transport.resolver.UploadPackFactory;
@@ -151,6 +153,16 @@ class UploadPackServlet extends HttpServlet {
}
}
+ private static int statusCodeForThrowable(Throwable error) {
+ if (error instanceof ServiceNotEnabledException) {
+ return SC_FORBIDDEN;
+ }
+ if (error instanceof WantNotValidException) {
+ return SC_BAD_REQUEST;
+ }
+ return SC_INTERNAL_SERVER_ERROR;
+ }
+
private final UploadPackErrorHandler handler;
UploadPackServlet(@Nullable UploadPackErrorHandler handler) {
@@ -216,9 +228,12 @@ class UploadPackServlet extends HttpServlet {
log(up.getRepository(), e);
if (!rsp.isCommitted()) {
rsp.reset();
- String msg = e instanceof PackProtocolException ? e.getMessage()
- : null;
- sendError(req, rsp, SC_INTERNAL_SERVER_ERROR, msg);
+ String msg = null;
+ if (e instanceof PackProtocolException
+ || e instanceof ServiceNotEnabledException) {
+ msg = e.getMessage();
+ }
+ sendError(req, rsp, statusCodeForThrowable(e), msg);
}
}
}