diff options
Diffstat (limited to 'org.eclipse.jgit.http.server/src/org')
35 files changed, 1161 insertions, 1843 deletions
diff --git a/org.eclipse.jgit.http.server/src/org/eclipse/jgit/http/server/AsIsFileFilter.java b/org.eclipse.jgit.http.server/src/org/eclipse/jgit/http/server/AsIsFileFilter.java index b2250e3ef3..add535307f 100644 --- a/org.eclipse.jgit.http.server/src/org/eclipse/jgit/http/server/AsIsFileFilter.java +++ b/org.eclipse.jgit.http.server/src/org/eclipse/jgit/http/server/AsIsFileFilter.java @@ -1,62 +1,29 @@ /* - * Copyright (C) 2009-2010, Google Inc. - * and other copyright owners as documented in the project's IP log. + * Copyright (C) 2009-2010, Google Inc. and others * - * This program and the accompanying materials are made available - * under the terms of the Eclipse Distribution License v1.0 which - * accompanies this distribution, is reproduced below, and is - * available at http://www.eclipse.org/org/documents/edl-v10.php + * This program and the accompanying materials are made available under the + * terms of the Eclipse Distribution License v. 1.0 which is available at + * https://www.eclipse.org/org/documents/edl-v10.php. * - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or - * without modification, are permitted provided that the following - * conditions are met: - * - * - Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * - Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following - * disclaimer in the documentation and/or other materials provided - * with the distribution. - * - * - Neither the name of the Eclipse Foundation, Inc. nor the - * names of its contributors may be used to endorse or promote - * products derived from this software without specific prior - * written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND - * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES - * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR - * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT - * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; - * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER - * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, - * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF - * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * SPDX-License-Identifier: BSD-3-Clause */ package org.eclipse.jgit.http.server; -import static javax.servlet.http.HttpServletResponse.SC_FORBIDDEN; -import static javax.servlet.http.HttpServletResponse.SC_UNAUTHORIZED; +import static jakarta.servlet.http.HttpServletResponse.SC_FORBIDDEN; +import static jakarta.servlet.http.HttpServletResponse.SC_UNAUTHORIZED; import static org.eclipse.jgit.http.server.ServletUtils.getRepository; import java.io.IOException; -import javax.servlet.Filter; -import javax.servlet.FilterChain; -import javax.servlet.FilterConfig; -import javax.servlet.ServletException; -import javax.servlet.ServletRequest; -import javax.servlet.ServletResponse; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; +import jakarta.servlet.Filter; +import jakarta.servlet.FilterChain; +import jakarta.servlet.FilterConfig; +import jakarta.servlet.ServletException; +import jakarta.servlet.ServletRequest; +import jakarta.servlet.ServletResponse; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; import org.eclipse.jgit.http.server.resolver.AsIsFileService; import org.eclipse.jgit.lib.Repository; @@ -66,28 +33,33 @@ import org.eclipse.jgit.transport.resolver.ServiceNotEnabledException; class AsIsFileFilter implements Filter { private final AsIsFileService asIs; - AsIsFileFilter(final AsIsFileService getAnyFile) { + AsIsFileFilter(AsIsFileService getAnyFile) { this.asIs = getAnyFile; } + @Override public void init(FilterConfig config) throws ServletException { // Do nothing. } + @Override public void destroy() { // Do nothing. } + @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { + HttpServletRequest req = (HttpServletRequest) request; + HttpServletResponse res = (HttpServletResponse) response; try { final Repository db = getRepository(request); - asIs.access((HttpServletRequest) request, db); + asIs.access(req, db); chain.doFilter(request, response); } catch (ServiceNotAuthorizedException e) { - ((HttpServletResponse) response).sendError(SC_UNAUTHORIZED); + res.sendError(SC_UNAUTHORIZED, e.getMessage()); } catch (ServiceNotEnabledException e) { - ((HttpServletResponse) response).sendError(SC_FORBIDDEN); + res.sendError(SC_FORBIDDEN, e.getMessage()); } } } diff --git a/org.eclipse.jgit.http.server/src/org/eclipse/jgit/http/server/ClientVersionUtil.java b/org.eclipse.jgit.http.server/src/org/eclipse/jgit/http/server/ClientVersionUtil.java index c0d4b5e4db..22fa345942 100644 --- a/org.eclipse.jgit.http.server/src/org/eclipse/jgit/http/server/ClientVersionUtil.java +++ b/org.eclipse.jgit.http.server/src/org/eclipse/jgit/http/server/ClientVersionUtil.java @@ -1,59 +1,26 @@ /* - * Copyright (C) 2012, Google Inc. - * and other copyright owners as documented in the project's IP log. + * Copyright (C) 2012, Google Inc. and others * - * This program and the accompanying materials are made available - * under the terms of the Eclipse Distribution License v1.0 which - * accompanies this distribution, is reproduced below, and is - * available at http://www.eclipse.org/org/documents/edl-v10.php + * This program and the accompanying materials are made available under the + * terms of the Eclipse Distribution License v. 1.0 which is available at + * https://www.eclipse.org/org/documents/edl-v10.php. * - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or - * without modification, are permitted provided that the following - * conditions are met: - * - * - Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * - Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following - * disclaimer in the documentation and/or other materials provided - * with the distribution. - * - * - Neither the name of the Eclipse Foundation, Inc. nor the - * names of its contributors may be used to endorse or promote - * products derived from this software without specific prior - * written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND - * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES - * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR - * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT - * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; - * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER - * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, - * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF - * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * SPDX-License-Identifier: BSD-3-Clause */ package org.eclipse.jgit.http.server; -import static org.eclipse.jgit.http.server.ServletUtils.isChunked; - -import javax.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletRequest; -/** Parses Git client User-Agent strings. */ +/** + * Parses Git client User-Agent strings. + */ public class ClientVersionUtil { - private static final int[] v1_7_5 = { 1, 7, 5 }; - private static final int[] v1_7_8_6 = { 1, 7, 8, 6 }; - private static final int[] v1_7_9 = { 1, 7, 9 }; - - /** @return maximum version array, indicating an invalid version of Git. */ + /** + * An invalid version of Git + * + * @return maximum version array, indicating an invalid version of Git. + */ public static int[] invalidVersion() { return new int[] { Integer.MAX_VALUE }; } @@ -168,17 +135,11 @@ public class ClientVersionUtil { * @param version * parsed version of the Git client software. * @return true if the bug is present. + * @deprecated no widely used Git versions need this any more */ + @Deprecated public static boolean hasPushStatusBug(int[] version) { - int cmp = compare(version, v1_7_8_6); - if (cmp < 0) - return true; // Everything before 1.7.8.6 is known broken. - else if (cmp == 0) - return false; // 1.7.8.6 contained the bug fix. - - if (compare(version, v1_7_9) <= 0) - return true; // 1.7.9 shipped before 1.7.8.6 and has the bug. - return false; // 1.7.9.1 and later are fixed. + return false; } /** @@ -192,10 +153,13 @@ public class ClientVersionUtil { * @param request * incoming HTTP request. * @return true if the client has the chunked encoding bug. + * @deprecated no widely used Git versions need this any more + * @since 7.0 */ + @Deprecated public static boolean hasChunkedEncodingRequestBug( int[] version, HttpServletRequest request) { - return compare(version, v1_7_5) == 0 && isChunked(request); + return false; } private ClientVersionUtil() { diff --git a/org.eclipse.jgit.http.server/src/org/eclipse/jgit/http/server/FileSender.java b/org.eclipse.jgit.http.server/src/org/eclipse/jgit/http/server/FileSender.java index 91e749e046..941e8adae0 100644 --- a/org.eclipse.jgit.http.server/src/org/eclipse/jgit/http/server/FileSender.java +++ b/org.eclipse.jgit.http.server/src/org/eclipse/jgit/http/server/FileSender.java @@ -1,50 +1,17 @@ /* - * Copyright (C) 2009-2010, Google Inc. - * and other copyright owners as documented in the project's IP log. + * Copyright (C) 2009-2010, Google Inc. and others * - * This program and the accompanying materials are made available - * under the terms of the Eclipse Distribution License v1.0 which - * accompanies this distribution, is reproduced below, and is - * available at http://www.eclipse.org/org/documents/edl-v10.php + * This program and the accompanying materials are made available under the + * terms of the Eclipse Distribution License v. 1.0 which is available at + * https://www.eclipse.org/org/documents/edl-v10.php. * - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or - * without modification, are permitted provided that the following - * conditions are met: - * - * - Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * - Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following - * disclaimer in the documentation and/or other materials provided - * with the distribution. - * - * - Neither the name of the Eclipse Foundation, Inc. nor the - * names of its contributors may be used to endorse or promote - * products derived from this software without specific prior - * written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND - * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES - * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR - * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT - * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; - * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER - * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, - * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF - * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * SPDX-License-Identifier: BSD-3-Clause */ package org.eclipse.jgit.http.server; -import static javax.servlet.http.HttpServletResponse.SC_PARTIAL_CONTENT; -import static javax.servlet.http.HttpServletResponse.SC_REQUESTED_RANGE_NOT_SATISFIABLE; +import static jakarta.servlet.http.HttpServletResponse.SC_PARTIAL_CONTENT; +import static jakarta.servlet.http.HttpServletResponse.SC_REQUESTED_RANGE_NOT_SATISFIABLE; import static org.eclipse.jgit.util.HttpSupport.HDR_ACCEPT_RANGES; import static org.eclipse.jgit.util.HttpSupport.HDR_CONTENT_LENGTH; import static org.eclipse.jgit.util.HttpSupport.HDR_CONTENT_RANGE; @@ -58,12 +25,14 @@ import java.io.IOException; import java.io.OutputStream; import java.io.RandomAccessFile; import java.text.MessageFormat; +import java.time.Instant; import java.util.Enumeration; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; import org.eclipse.jgit.lib.ObjectId; +import org.eclipse.jgit.util.FS; /** * Dumps a file over HTTP GET (or its information via HEAD). @@ -76,7 +45,7 @@ final class FileSender { private final RandomAccessFile source; - private final long lastModified; + private final Instant lastModified; private final long fileLen; @@ -84,12 +53,12 @@ final class FileSender { private long end; - FileSender(final File path) throws FileNotFoundException { + FileSender(File path) throws FileNotFoundException { this.path = path; this.source = new RandomAccessFile(path, "r"); try { - this.lastModified = path.lastModified(); + this.lastModified = FS.DETECTED.lastModifiedInstant(path); this.fileLen = source.getChannel().size(); this.end = fileLen; } catch (IOException e) { @@ -114,7 +83,7 @@ final class FileSender { } } - long getLastModified() { + Instant getLastModified() { return lastModified; } @@ -137,8 +106,7 @@ final class FileSender { rsp.setHeader(HDR_CONTENT_LENGTH, Long.toString(end - pos)); if (sendBody) { - final OutputStream out = rsp.getOutputStream(); - try { + try (OutputStream out = rsp.getOutputStream()) { final byte[] buf = new byte[4096]; source.seek(pos); while (pos < end) { @@ -151,8 +119,6 @@ final class FileSender { pos += n; } out.flush(); - } finally { - out.close(); } } } @@ -220,7 +186,7 @@ final class FileSender { return true; } - private static Enumeration<String> getRange(final HttpServletRequest req) { + private static Enumeration<String> getRange(HttpServletRequest req) { return req.getHeaders(HDR_RANGE); } } diff --git a/org.eclipse.jgit.http.server/src/org/eclipse/jgit/http/server/GitFilter.java b/org.eclipse.jgit.http.server/src/org/eclipse/jgit/http/server/GitFilter.java index 529b8391fa..8adeddd9d3 100644 --- a/org.eclipse.jgit.http.server/src/org/eclipse/jgit/http/server/GitFilter.java +++ b/org.eclipse.jgit.http.server/src/org/eclipse/jgit/http/server/GitFilter.java @@ -1,58 +1,25 @@ /* - * Copyright (C) 2009-2010, Google Inc. - * and other copyright owners as documented in the project's IP log. + * Copyright (C) 2009-2010, Google Inc. and others * - * This program and the accompanying materials are made available - * under the terms of the Eclipse Distribution License v1.0 which - * accompanies this distribution, is reproduced below, and is - * available at http://www.eclipse.org/org/documents/edl-v10.php + * This program and the accompanying materials are made available under the + * terms of the Eclipse Distribution License v. 1.0 which is available at + * https://www.eclipse.org/org/documents/edl-v10.php. * - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or - * without modification, are permitted provided that the following - * conditions are met: - * - * - Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * - Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following - * disclaimer in the documentation and/or other materials provided - * with the distribution. - * - * - Neither the name of the Eclipse Foundation, Inc. nor the - * names of its contributors may be used to endorse or promote - * products derived from this software without specific prior - * written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND - * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES - * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR - * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT - * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; - * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER - * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, - * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF - * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * SPDX-License-Identifier: BSD-3-Clause */ package org.eclipse.jgit.http.server; import java.io.File; import java.text.MessageFormat; -import java.util.LinkedList; +import java.util.ArrayList; import java.util.List; -import javax.servlet.Filter; -import javax.servlet.FilterConfig; -import javax.servlet.ServletException; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; +import jakarta.servlet.Filter; +import jakarta.servlet.FilterConfig; +import jakarta.servlet.ServletException; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; import org.eclipse.jgit.http.server.glue.ErrorServlet; import org.eclipse.jgit.http.server.glue.MetaFilter; @@ -62,8 +29,6 @@ import org.eclipse.jgit.http.server.resolver.AsIsFileService; import org.eclipse.jgit.http.server.resolver.DefaultReceivePackFactory; import org.eclipse.jgit.http.server.resolver.DefaultUploadPackFactory; import org.eclipse.jgit.lib.Constants; -import org.eclipse.jgit.transport.ReceivePack; -import org.eclipse.jgit.transport.UploadPack; import org.eclipse.jgit.transport.resolver.FileResolver; import org.eclipse.jgit.transport.resolver.ReceivePackFactory; import org.eclipse.jgit.transport.resolver.RepositoryResolver; @@ -74,15 +39,16 @@ import org.eclipse.jgit.util.StringUtils; * Handles Git repository access over HTTP. * <p> * Applications embedding this filter should map a directory path within the - * application to this filter. For a servlet version, see {@link GitServlet}. + * application to this filter. For a servlet version, see + * {@link org.eclipse.jgit.http.server.GitServlet}. * <p> * Applications may wish to add additional repository action URLs to this - * servlet by taking advantage of its extension from {@link MetaFilter}. - * Callers may register their own URL suffix translations through - * {@link #serve(String)}, or their regex translations through - * {@link #serveRegex(String)}. Each translation should contain a complete - * filter pipeline which ends with the HttpServlet that should handle the - * requested action. + * servlet by taking advantage of its extension from + * {@link org.eclipse.jgit.http.server.glue.MetaFilter}. Callers may register + * their own URL suffix translations through {@link #serve(String)}, or their + * regex translations through {@link #serveRegex(String)}. Each translation + * should contain a complete filter pipeline which ends with the HttpServlet + * that should handle the requested action. */ public class GitFilter extends MetaFilter { private volatile boolean initialized; @@ -93,11 +59,15 @@ public class GitFilter extends MetaFilter { private UploadPackFactory<HttpServletRequest> uploadPackFactory = new DefaultUploadPackFactory(); + private UploadPackErrorHandler uploadPackErrorHandler; + private ReceivePackFactory<HttpServletRequest> receivePackFactory = new DefaultReceivePackFactory(); - private final List<Filter> uploadPackFilters = new LinkedList<Filter>(); + private ReceivePackErrorHandler receivePackErrorHandler; - private final List<Filter> receivePackFilters = new LinkedList<Filter>(); + private final List<Filter> uploadPackFilters = new ArrayList<>(); + + private final List<Filter> receivePackFilters = new ArrayList<>(); /** * New servlet that will load its base directory from {@code web.xml}. @@ -124,6 +94,8 @@ public class GitFilter extends MetaFilter { } /** + * Set AsIsFileService + * * @param f * the filter to validate direct access to repository files * through a dumb client. If {@code null} then dumb client @@ -135,9 +107,12 @@ public class GitFilter extends MetaFilter { } /** + * Set upload-pack factory + * * @param f - * the factory to construct and configure an {@link UploadPack} - * session when a fetch or clone is requested by a client. + * the factory to construct and configure an + * {@link org.eclipse.jgit.transport.UploadPack} session when a + * fetch or clone is requested by a client. */ @SuppressWarnings("unchecked") public void setUploadPackFactory(UploadPackFactory<HttpServletRequest> f) { @@ -146,10 +121,25 @@ public class GitFilter extends MetaFilter { } /** + * Set a custom error handler for git-upload-pack. + * + * @param h + * A custom error handler for git-upload-pack. + * @since 5.6 + */ + public void setUploadPackErrorHandler(UploadPackErrorHandler h) { + assertNotInitialized(); + this.uploadPackErrorHandler = h; + } + + /** + * Add upload-pack filter + * * @param filter * filter to apply before any of the UploadPack operations. The * UploadPack instance is available in the request attribute - * {@link ServletUtils#ATTRIBUTE_HANDLER}. + * {@link org.eclipse.jgit.http.server.ServletUtils#ATTRIBUTE_HANDLER}. + * @since 7.0 */ public void addUploadPackFilter(Filter filter) { assertNotInitialized(); @@ -157,9 +147,12 @@ public class GitFilter extends MetaFilter { } /** + * Set the receive-pack factory + * * @param f - * the factory to construct and configure a {@link ReceivePack} - * session when a push is requested by a client. + * the factory to construct and configure a + * {@link org.eclipse.jgit.transport.ReceivePack} session when a + * push is requested by a client. */ @SuppressWarnings("unchecked") public void setReceivePackFactory(ReceivePackFactory<HttpServletRequest> f) { @@ -168,10 +161,25 @@ public class GitFilter extends MetaFilter { } /** + * Set a custom error handler for git-receive-pack. + * + * @param h + * A custom error handler for git-receive-pack. + * @since 5.7 + */ + public void setReceivePackErrorHandler(ReceivePackErrorHandler h) { + assertNotInitialized(); + this.receivePackErrorHandler = h; + } + + /** + * Add receive-pack filter + * * @param filter * filter to apply before any of the ReceivePack operations. The * ReceivePack instance is available in the request attribute - * {@link ServletUtils#ATTRIBUTE_HANDLER}. + * {@link org.eclipse.jgit.http.server.ServletUtils#ATTRIBUTE_HANDLER}. + * @since 7.0 */ public void addReceivePackFilter(Filter filter) { assertNotInitialized(); @@ -190,7 +198,7 @@ public class GitFilter extends MetaFilter { if (resolver == null) { File root = getFile(filterConfig, "base-path"); boolean exportAll = getBoolean(filterConfig, "export-all"); - setRepositoryResolver(new FileResolver<HttpServletRequest>(root, exportAll)); + setRepositoryResolver(new FileResolver<>(root, exportAll)); } initialized = true; @@ -200,7 +208,7 @@ public class GitFilter extends MetaFilter { b = b.through(new UploadPackServlet.Factory(uploadPackFactory)); for (Filter f : uploadPackFilters) b = b.through(f); - b.with(new UploadPackServlet()); + b.with(new UploadPackServlet(uploadPackErrorHandler)); } if (receivePackFactory != ReceivePackFactory.DISABLED) { @@ -208,7 +216,7 @@ public class GitFilter extends MetaFilter { b = b.through(new ReceivePackServlet.Factory(receivePackFactory)); for (Filter f : receivePackFilters) b = b.through(f); - b.with(new ReceivePackServlet()); + b.with(new ReceivePackServlet(receivePackErrorHandler)); } ServletBinder refs = serve("*/" + Constants.INFO_REFS); @@ -236,13 +244,13 @@ public class GitFilter extends MetaFilter { .through(enabled)// .with(new TextFileServlet(Constants.HEAD)); - final String info_alternates = "objects/info/alternates"; + final String info_alternates = Constants.OBJECTS + "/" + Constants.INFO_ALTERNATES; serve("*/" + info_alternates)// .through(mustBeLocal)// .through(enabled)// .with(new TextFileServlet(info_alternates)); - final String http_alternates = "objects/info/http-alternates"; + final String http_alternates = Constants.OBJECTS + "/" + Constants.INFO_HTTP_ALTERNATES; serve("*/" + http_alternates)// .through(mustBeLocal)// .through(enabled)// @@ -293,7 +301,8 @@ public class GitFilter extends MetaFilter { try { return StringUtils.toBoolean(n); } catch (IllegalArgumentException err) { - throw new ServletException(MessageFormat.format(HttpServerText.get().invalidBoolean, param, n)); + throw new ServletException(MessageFormat.format( + HttpServerText.get().invalidBoolean, param, n), err); } } diff --git a/org.eclipse.jgit.http.server/src/org/eclipse/jgit/http/server/GitServlet.java b/org.eclipse.jgit.http.server/src/org/eclipse/jgit/http/server/GitServlet.java index bca521025d..eadab21846 100644 --- a/org.eclipse.jgit.http.server/src/org/eclipse/jgit/http/server/GitServlet.java +++ b/org.eclipse.jgit.http.server/src/org/eclipse/jgit/http/server/GitServlet.java @@ -1,61 +1,26 @@ /* - * Copyright (C) 2009-2010, Google Inc. - * and other copyright owners as documented in the project's IP log. + * Copyright (C) 2009-2010, Google Inc. and others * - * This program and the accompanying materials are made available - * under the terms of the Eclipse Distribution License v1.0 which - * accompanies this distribution, is reproduced below, and is - * available at http://www.eclipse.org/org/documents/edl-v10.php + * This program and the accompanying materials are made available under the + * terms of the Eclipse Distribution License v. 1.0 which is available at + * https://www.eclipse.org/org/documents/edl-v10.php. * - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or - * without modification, are permitted provided that the following - * conditions are met: - * - * - Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * - Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following - * disclaimer in the documentation and/or other materials provided - * with the distribution. - * - * - Neither the name of the Eclipse Foundation, Inc. nor the - * names of its contributors may be used to endorse or promote - * products derived from this software without specific prior - * written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND - * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES - * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR - * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT - * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; - * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER - * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, - * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF - * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * SPDX-License-Identifier: BSD-3-Clause */ package org.eclipse.jgit.http.server; import java.util.Enumeration; -import javax.servlet.Filter; -import javax.servlet.FilterConfig; -import javax.servlet.ServletConfig; -import javax.servlet.ServletContext; -import javax.servlet.ServletException; -import javax.servlet.http.HttpServletRequest; +import jakarta.servlet.Filter; +import jakarta.servlet.FilterConfig; +import jakarta.servlet.ServletConfig; +import jakarta.servlet.ServletContext; +import jakarta.servlet.ServletException; +import jakarta.servlet.http.HttpServletRequest; import org.eclipse.jgit.http.server.glue.MetaServlet; import org.eclipse.jgit.http.server.resolver.AsIsFileService; -import org.eclipse.jgit.transport.ReceivePack; -import org.eclipse.jgit.transport.UploadPack; import org.eclipse.jgit.transport.resolver.ReceivePackFactory; import org.eclipse.jgit.transport.resolver.RepositoryResolver; import org.eclipse.jgit.transport.resolver.UploadPackFactory; @@ -87,12 +52,12 @@ import org.eclipse.jgit.transport.resolver.UploadPackFactory; * * <p> * Applications may wish to add additional repository action URLs to this - * servlet by taking advantage of its extension from {@link MetaServlet}. - * Callers may register their own URL suffix translations through - * {@link #serve(String)}, or their regex translations through - * {@link #serveRegex(String)}. Each translation should contain a complete - * filter pipeline which ends with the HttpServlet that should handle the - * requested action. + * servlet by taking advantage of its extension from + * {@link org.eclipse.jgit.http.server.glue.MetaServlet}. Callers may register + * their own URL suffix translations through {@link #serve(String)}, or their + * regex translations through {@link #serveRegex(String)}. Each translation + * should contain a complete filter pipeline which ends with the HttpServlet + * that should handle the requested action. */ public class GitServlet extends MetaServlet { private static final long serialVersionUID = 1L; @@ -124,6 +89,8 @@ public class GitServlet extends MetaServlet { } /** + * Set AsIsFileService + * * @param f * the filter to validate direct access to repository files * through a dumb client. If {@code null} then dumb client @@ -134,58 +101,96 @@ public class GitServlet extends MetaServlet { } /** + * Set upload-pack factory + * * @param f - * the factory to construct and configure an {@link UploadPack} - * session when a fetch or clone is requested by a client. + * the factory to construct and configure an + * {@link org.eclipse.jgit.transport.UploadPack} session when a + * fetch or clone is requested by a client. */ public void setUploadPackFactory(UploadPackFactory<HttpServletRequest> f) { gitFilter.setUploadPackFactory(f); } /** + * Set a custom error handler for git-upload-pack. + * + * @param h + * A custom error handler for git-upload-pack. + * @since 5.9.1 + */ + public void setUploadPackErrorHandler(UploadPackErrorHandler h) { + gitFilter.setUploadPackErrorHandler(h); + } + + /** + * Add upload-pack filter + * * @param filter * filter to apply before any of the UploadPack operations. The * UploadPack instance is available in the request attribute - * {@link ServletUtils#ATTRIBUTE_HANDLER}. + * {@link org.eclipse.jgit.http.server.ServletUtils#ATTRIBUTE_HANDLER}. + * @since 7.0 */ public void addUploadPackFilter(Filter filter) { gitFilter.addUploadPackFilter(filter); } /** + * Set receive-pack factory + * * @param f - * the factory to construct and configure a {@link ReceivePack} - * session when a push is requested by a client. + * the factory to construct and configure a + * {@link org.eclipse.jgit.transport.ReceivePack} session when a + * push is requested by a client. */ public void setReceivePackFactory(ReceivePackFactory<HttpServletRequest> f) { gitFilter.setReceivePackFactory(f); } /** + * Set a custom error handler for git-receive-pack. + * + * @param h + * A custom error handler for git-receive-pack. + * @since 5.9.1 + */ + public void setReceivePackErrorHandler(ReceivePackErrorHandler h) { + gitFilter.setReceivePackErrorHandler(h); + } + + /** + * Add receive-pack filter + * * @param filter * filter to apply before any of the ReceivePack operations. The * ReceivePack instance is available in the request attribute - * {@link ServletUtils#ATTRIBUTE_HANDLER}. + * {@link org.eclipse.jgit.http.server.ServletUtils#ATTRIBUTE_HANDLER}. + * @since 7.0 */ public void addReceivePackFilter(Filter filter) { gitFilter.addReceivePackFilter(filter); } @Override - public void init(final ServletConfig config) throws ServletException { + public void init(ServletConfig config) throws ServletException { gitFilter.init(new FilterConfig() { + @Override public String getFilterName() { return gitFilter.getClass().getName(); } + @Override public String getInitParameter(String name) { return config.getInitParameter(name); } + @Override public Enumeration<String> getInitParameterNames() { return config.getInitParameterNames(); } + @Override public ServletContext getServletContext() { return config.getServletContext(); } 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 d8cd61df81..bf3da4b5d0 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 @@ -1,57 +1,22 @@ /* - * Copyright (C) 2011, Google Inc. - * and other copyright owners as documented in the project's IP log. + * Copyright (C) 2011, Google Inc. and others * - * This program and the accompanying materials are made available - * under the terms of the Eclipse Distribution License v1.0 which - * accompanies this distribution, is reproduced below, and is - * available at http://www.eclipse.org/org/documents/edl-v10.php + * This program and the accompanying materials are made available under the + * terms of the Eclipse Distribution License v. 1.0 which is available at + * https://www.eclipse.org/org/documents/edl-v10.php. * - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or - * without modification, are permitted provided that the following - * conditions are met: - * - * - Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * - Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following - * disclaimer in the documentation and/or other materials provided - * with the distribution. - * - * - Neither the name of the Eclipse Foundation, Inc. nor the - * names of its contributors may be used to endorse or promote - * products derived from this software without specific prior - * written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND - * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES - * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR - * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT - * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; - * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER - * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, - * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF - * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * SPDX-License-Identifier: BSD-3-Clause */ package org.eclipse.jgit.http.server; +import static jakarta.servlet.http.HttpServletResponse.SC_FORBIDDEN; +import static jakarta.servlet.http.HttpServletResponse.SC_INTERNAL_SERVER_ERROR; +import static jakarta.servlet.http.HttpServletResponse.SC_NOT_FOUND; import static org.eclipse.jgit.http.server.ServletUtils.ATTRIBUTE_HANDLER; -import static org.eclipse.jgit.transport.GitProtocolConstants.OPTION_SIDE_BAND; -import static org.eclipse.jgit.transport.GitProtocolConstants.OPTION_SIDE_BAND_64K; import static org.eclipse.jgit.transport.GitProtocolConstants.CAPABILITY_SIDE_BAND_64K; import static org.eclipse.jgit.transport.SideBandOutputStream.CH_ERROR; import static org.eclipse.jgit.transport.SideBandOutputStream.SMALL_BUF; -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_NOT_FOUND; import java.io.ByteArrayOutputStream; import java.io.IOException; @@ -60,16 +25,16 @@ import java.util.Arrays; import java.util.Collections; import java.util.List; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; +import org.eclipse.jgit.internal.transport.parser.FirstCommand; import org.eclipse.jgit.lib.Constants; import org.eclipse.jgit.transport.PacketLineIn; import org.eclipse.jgit.transport.PacketLineOut; import org.eclipse.jgit.transport.ReceivePack; import org.eclipse.jgit.transport.RequestNotYetReadException; import org.eclipse.jgit.transport.SideBandOutputStream; -import org.eclipse.jgit.transport.UploadPack; /** * Utility functions for handling the Git-over-HTTP protocol. @@ -118,6 +83,7 @@ public class GitSmartHttpTools { * @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. + * @since 7.0 */ public static boolean isGitClient(HttpServletRequest req) { return isInfoRefs(req) || isUploadPack(req) || isReceivePack(req); @@ -139,6 +105,7 @@ public class GitSmartHttpTools { * HTTP status code to set if the client is not a Git client. * @throws IOException * the response cannot be sent. + * @since 7.0 */ public static void sendError(HttpServletRequest req, HttpServletResponse res, int httpStatus) throws IOException { @@ -154,9 +121,9 @@ public class GitSmartHttpTools { * an HTTP response code is returned instead. * <p> * This method may only be called before handing off the request to - * {@link UploadPack#upload(java.io.InputStream, OutputStream, OutputStream)} + * {@link org.eclipse.jgit.transport.UploadPack#upload(java.io.InputStream, OutputStream, OutputStream)} * or - * {@link ReceivePack#receive(java.io.InputStream, OutputStream, OutputStream)}. + * {@link org.eclipse.jgit.transport.ReceivePack#receive(java.io.InputStream, OutputStream, OutputStream)}. * * @param req * current request. @@ -171,6 +138,7 @@ public class GitSmartHttpTools { * response code. * @throws IOException * the response cannot be sent. + * @since 7.0 */ public static void sendError(HttpServletRequest req, HttpServletResponse res, int httpStatus, String textForGit) @@ -193,71 +161,45 @@ 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); - res.sendError(httpStatus); + res.sendError(httpStatus, textForGit); } } 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); - - 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 static boolean isUploadPackSideBand(HttpServletRequest req) { - try { - // The client may be in a state where they have sent the sideband - // capability and are expecting a response in the sideband, but we might - // not have an UploadPack, or it might not have read any of the request. - // So, cheat and read the first line. - String line = new PacketLineIn(req.getInputStream()).readString(); - UploadPack.FirstLine parsed = new UploadPack.FirstLine(line); - return (parsed.getOptions().contains(OPTION_SIDE_BAND) - || parsed.getOptions().contains(OPTION_SIDE_BAND_64K)); - } catch (IOException e) { - // Probably the connection is closed and a subsequent write will fail, but - // try it just in case. - return false; - } + writePacket(pckOut, textForGit); + 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); @@ -276,7 +218,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) { @@ -286,8 +228,9 @@ public class GitSmartHttpTools { // not have a ReceivePack, or it might not have read any of the request. // So, cheat and read the first line. String line = new PacketLineIn(req.getInputStream()).readString(); - ReceivePack.FirstLine parsed = new ReceivePack.FirstLine(line); - return parsed.getCapabilities().contains(CAPABILITY_SIDE_BAND_64K); + FirstCommand parsed = FirstCommand.fromLine(line); + return parsed.getCapabilities() + .containsKey(CAPABILITY_SIDE_BAND_64K); } catch (IOException e) { // Probably the connection is closed and a subsequent write will fail, but // try it just in case. @@ -297,28 +240,28 @@ public class GitSmartHttpTools { private static void writeSideBand(OutputStream out, String textForGit) throws IOException { - @SuppressWarnings("resource" /* java 7 */) - OutputStream msg = new SideBandOutputStream(CH_ERROR, SMALL_BUF, out); - msg.write(Constants.encode("error: " + textForGit)); - msg.flush(); + try (OutputStream msg = new SideBandOutputStream(CH_ERROR, SMALL_BUF, + out)) { + msg.write(Constants.encode("error: " + textForGit)); + msg.flush(); + } } private static void writePacket(PacketLineOut pckOut, String textForGit) throws IOException { - pckOut.writeString("error: " + textForGit); + pckOut.writeString("ERR " + textForGit); } 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.setContentType(type); - res.setContentLength(buf.length); - OutputStream os = res.getOutputStream(); - try { + if (!res.isCommitted()) { + res.setStatus(httpStatus); + res.setContentType(type); + res.setContentLength(buf.length); + } + try (OutputStream os = res.getOutputStream()) { os.write(buf); - } finally { - os.close(); } } @@ -334,6 +277,7 @@ public class GitSmartHttpTools { * @throws IllegalArgumentException * the request is not a Git client request. See * {@link #isGitClient(HttpServletRequest)}. + * @since 7.0 */ public static String getResponseContentType(HttpServletRequest req) { if (isInfoRefs(req)) @@ -375,6 +319,7 @@ public class GitSmartHttpTools { * @param req * current request. * @return true if the request is for the /info/refs service. + * @since 7.0 */ public static boolean isInfoRefs(HttpServletRequest req) { return req.getRequestURI().endsWith(INFO_REFS_PATH) @@ -398,6 +343,7 @@ public class GitSmartHttpTools { * @param req * current request. * @return true if the request is for the /git-upload-pack handler. + * @since 7.0 */ public static boolean isUploadPack(HttpServletRequest req) { return isUploadPack(req.getRequestURI()) @@ -410,6 +356,7 @@ public class GitSmartHttpTools { * @param req * current request. * @return true if the request is for the /git-receive-pack handler. + * @since 7.0 */ public static boolean isReceivePack(HttpServletRequest req) { String uri = req.getRequestURI(); diff --git a/org.eclipse.jgit.http.server/src/org/eclipse/jgit/http/server/HttpServerText.java b/org.eclipse.jgit.http.server/src/org/eclipse/jgit/http/server/HttpServerText.java index dff90a6c8b..5939920fbf 100644 --- a/org.eclipse.jgit.http.server/src/org/eclipse/jgit/http/server/HttpServerText.java +++ b/org.eclipse.jgit.http.server/src/org/eclipse/jgit/http/server/HttpServerText.java @@ -1,44 +1,11 @@ /* - * Copyright (C) 2010, Sasa Zivkov <sasa.zivkov@sap.com> - * and other copyright owners as documented in the project's IP log. + * Copyright (C) 2010, Sasa Zivkov <sasa.zivkov@sap.com> and others * - * This program and the accompanying materials are made available - * under the terms of the Eclipse Distribution License v1.0 which - * accompanies this distribution, is reproduced below, and is - * available at http://www.eclipse.org/org/documents/edl-v10.php + * This program and the accompanying materials are made available under the + * terms of the Eclipse Distribution License v. 1.0 which is available at + * https://www.eclipse.org/org/documents/edl-v10.php. * - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or - * without modification, are permitted provided that the following - * conditions are met: - * - * - Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * - Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following - * disclaimer in the documentation and/or other materials provided - * with the distribution. - * - * - Neither the name of the Eclipse Foundation, Inc. nor the - * names of its contributors may be used to endorse or promote - * products derived from this software without specific prior - * written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND - * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES - * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR - * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT - * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; - * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER - * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, - * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF - * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * SPDX-License-Identifier: BSD-3-Clause */ package org.eclipse.jgit.http.server; @@ -49,9 +16,12 @@ import org.eclipse.jgit.nls.TranslationBundle; /** * Translation bundle for JGit http server */ +@SuppressWarnings("MissingSummary") public class HttpServerText extends TranslationBundle { /** + * Get an instance of this translation bundle + * * @return an instance of this translation bundle */ public static HttpServerText get() { diff --git a/org.eclipse.jgit.http.server/src/org/eclipse/jgit/http/server/InfoPacksServlet.java b/org.eclipse.jgit.http.server/src/org/eclipse/jgit/http/server/InfoPacksServlet.java index bb6efb84b2..74a6da9669 100644 --- a/org.eclipse.jgit.http.server/src/org/eclipse/jgit/http/server/InfoPacksServlet.java +++ b/org.eclipse.jgit.http.server/src/org/eclipse/jgit/http/server/InfoPacksServlet.java @@ -1,44 +1,11 @@ /* - * Copyright (C) 2009-2010, Google Inc. - * and other copyright owners as documented in the project's IP log. + * Copyright (C) 2009-2010, Google Inc. and others * - * This program and the accompanying materials are made available - * under the terms of the Eclipse Distribution License v1.0 which - * accompanies this distribution, is reproduced below, and is - * available at http://www.eclipse.org/org/documents/edl-v10.php + * This program and the accompanying materials are made available under the + * terms of the Eclipse Distribution License v. 1.0 which is available at + * https://www.eclipse.org/org/documents/edl-v10.php. * - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or - * without modification, are permitted provided that the following - * conditions are met: - * - * - Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * - Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following - * disclaimer in the documentation and/or other materials provided - * with the distribution. - * - * - Neither the name of the Eclipse Foundation, Inc. nor the - * names of its contributors may be used to endorse or promote - * products derived from this software without specific prior - * written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND - * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES - * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR - * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT - * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; - * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER - * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, - * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF - * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * SPDX-License-Identifier: BSD-3-Clause */ package org.eclipse.jgit.http.server; @@ -48,28 +15,29 @@ import static org.eclipse.jgit.http.server.ServletUtils.sendPlainText; import java.io.IOException; -import javax.servlet.http.HttpServlet; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; +import jakarta.servlet.http.HttpServlet; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; import org.eclipse.jgit.internal.storage.file.ObjectDirectory; -import org.eclipse.jgit.internal.storage.file.PackFile; +import org.eclipse.jgit.internal.storage.file.Pack; import org.eclipse.jgit.lib.ObjectDatabase; /** Sends the current list of pack files, sorted most recent first. */ class InfoPacksServlet extends HttpServlet { private static final long serialVersionUID = 1L; + @Override public void doGet(final HttpServletRequest req, final HttpServletResponse rsp) throws IOException { sendPlainText(packList(req), req, rsp); } - private static String packList(final HttpServletRequest req) { + private static String packList(HttpServletRequest req) { final StringBuilder out = new StringBuilder(); final ObjectDatabase db = getRepository(req).getObjectDatabase(); if (db instanceof ObjectDirectory) { - for (PackFile pack : ((ObjectDirectory) db).getPacks()) { + for (Pack pack : ((ObjectDirectory) db).getPacks()) { out.append("P "); out.append(pack.getPackFile().getName()); out.append('\n'); diff --git a/org.eclipse.jgit.http.server/src/org/eclipse/jgit/http/server/InfoRefsServlet.java b/org.eclipse.jgit.http.server/src/org/eclipse/jgit/http/server/InfoRefsServlet.java index 446f6a2c8d..632cbaf915 100644 --- a/org.eclipse.jgit.http.server/src/org/eclipse/jgit/http/server/InfoRefsServlet.java +++ b/org.eclipse.jgit.http.server/src/org/eclipse/jgit/http/server/InfoRefsServlet.java @@ -1,61 +1,26 @@ /* - * Copyright (C) 2009-2010, Google Inc. - * and other copyright owners as documented in the project's IP log. + * Copyright (C) 2009-2010, Google Inc. and others * - * This program and the accompanying materials are made available - * under the terms of the Eclipse Distribution License v1.0 which - * accompanies this distribution, is reproduced below, and is - * available at http://www.eclipse.org/org/documents/edl-v10.php + * This program and the accompanying materials are made available under the + * terms of the Eclipse Distribution License v. 1.0 which is available at + * https://www.eclipse.org/org/documents/edl-v10.php. * - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or - * without modification, are permitted provided that the following - * conditions are met: - * - * - Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * - Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following - * disclaimer in the documentation and/or other materials provided - * with the distribution. - * - * - Neither the name of the Eclipse Foundation, Inc. nor the - * names of its contributors may be used to endorse or promote - * products derived from this software without specific prior - * written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND - * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES - * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR - * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT - * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; - * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER - * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, - * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF - * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * SPDX-License-Identifier: BSD-3-Clause */ package org.eclipse.jgit.http.server; +import static java.nio.charset.StandardCharsets.UTF_8; import static org.eclipse.jgit.http.server.ServletUtils.getRepository; -import static org.eclipse.jgit.lib.RefDatabase.ALL; import java.io.IOException; import java.io.OutputStreamWriter; -import java.util.Map; -import javax.servlet.http.HttpServlet; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; +import jakarta.servlet.http.HttpServlet; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; import org.eclipse.jgit.lib.Constants; -import org.eclipse.jgit.lib.Ref; import org.eclipse.jgit.lib.Repository; import org.eclipse.jgit.transport.RefAdvertiser; import org.eclipse.jgit.util.HttpSupport; @@ -64,37 +29,36 @@ import org.eclipse.jgit.util.HttpSupport; class InfoRefsServlet extends HttpServlet { private static final long serialVersionUID = 1L; + @Override public void doGet(final HttpServletRequest req, final HttpServletResponse rsp) throws IOException { // Assume a dumb client and send back the dumb client // version of the info/refs file. rsp.setContentType(HttpSupport.TEXT_PLAIN); - rsp.setCharacterEncoding(Constants.CHARACTER_ENCODING); + rsp.setCharacterEncoding(UTF_8.name()); final Repository db = getRepository(req); - final OutputStreamWriter out = new OutputStreamWriter( + try (OutputStreamWriter out = new OutputStreamWriter( new SmartOutputStream(req, rsp, true), - Constants.CHARSET); - final RefAdvertiser adv = new RefAdvertiser() { - @Override - protected void writeOne(final CharSequence line) throws IOException { - // Whoever decided that info/refs should use a different - // delimiter than the native git:// protocol shouldn't - // be allowed to design this sort of stuff. :-( - out.append(line.toString().replace(' ', '\t')); - } - - @Override - protected void end() { - // No end marker required for info/refs format. - } - }; - adv.init(db); - adv.setDerefTags(true); + UTF_8)) { + final RefAdvertiser adv = new RefAdvertiser() { + @Override + protected void writeOne(CharSequence line) + throws IOException { + // Whoever decided that info/refs should use a different + // delimiter than the native git:// protocol shouldn't + // be allowed to design this sort of stuff. :-( + out.append(line.toString().replace(' ', '\t')); + } - Map<String, Ref> refs = db.getRefDatabase().getRefs(ALL); - refs.remove(Constants.HEAD); - adv.send(refs); - out.close(); + @Override + protected void end() { + // No end marker required for info/refs format. + } + }; + adv.init(db); + adv.setDerefTags(true); + adv.send(db.getRefDatabase().getRefsByPrefix(Constants.R_REFS)); + } } } diff --git a/org.eclipse.jgit.http.server/src/org/eclipse/jgit/http/server/IsLocalFilter.java b/org.eclipse.jgit.http.server/src/org/eclipse/jgit/http/server/IsLocalFilter.java index 511cdf1935..2a65a7fba8 100644 --- a/org.eclipse.jgit.http.server/src/org/eclipse/jgit/http/server/IsLocalFilter.java +++ b/org.eclipse.jgit.http.server/src/org/eclipse/jgit/http/server/IsLocalFilter.java @@ -1,60 +1,27 @@ /* - * Copyright (C) 2009-2010, Google Inc. - * and other copyright owners as documented in the project's IP log. + * Copyright (C) 2009-2010, Google Inc. and others * - * This program and the accompanying materials are made available - * under the terms of the Eclipse Distribution License v1.0 which - * accompanies this distribution, is reproduced below, and is - * available at http://www.eclipse.org/org/documents/edl-v10.php + * This program and the accompanying materials are made available under the + * terms of the Eclipse Distribution License v. 1.0 which is available at + * https://www.eclipse.org/org/documents/edl-v10.php. * - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or - * without modification, are permitted provided that the following - * conditions are met: - * - * - Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * - Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following - * disclaimer in the documentation and/or other materials provided - * with the distribution. - * - * - Neither the name of the Eclipse Foundation, Inc. nor the - * names of its contributors may be used to endorse or promote - * products derived from this software without specific prior - * written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND - * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES - * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR - * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT - * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; - * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER - * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, - * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF - * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * SPDX-License-Identifier: BSD-3-Clause */ package org.eclipse.jgit.http.server; -import static javax.servlet.http.HttpServletResponse.SC_FORBIDDEN; +import static jakarta.servlet.http.HttpServletResponse.SC_FORBIDDEN; import static org.eclipse.jgit.http.server.ServletUtils.getRepository; import java.io.IOException; -import javax.servlet.Filter; -import javax.servlet.FilterChain; -import javax.servlet.FilterConfig; -import javax.servlet.ServletException; -import javax.servlet.ServletRequest; -import javax.servlet.ServletResponse; -import javax.servlet.http.HttpServletResponse; +import jakarta.servlet.Filter; +import jakarta.servlet.FilterChain; +import jakarta.servlet.FilterConfig; +import jakarta.servlet.ServletException; +import jakarta.servlet.ServletRequest; +import jakarta.servlet.ServletResponse; +import jakarta.servlet.http.HttpServletResponse; import org.eclipse.jgit.internal.storage.file.ObjectDirectory; import org.eclipse.jgit.lib.Repository; @@ -66,14 +33,17 @@ import org.eclipse.jgit.lib.Repository; * downstream servlet can directly access its contents on disk. */ class IsLocalFilter implements Filter { + @Override public void init(FilterConfig config) throws ServletException { // Do nothing. } + @Override public void destroy() { // Do nothing. } + @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { if (isLocal(getRepository(request))) @@ -82,7 +52,7 @@ class IsLocalFilter implements Filter { ((HttpServletResponse) response).sendError(SC_FORBIDDEN); } - private static boolean isLocal(final Repository db) { + private static boolean isLocal(Repository db) { return db.getObjectDatabase() instanceof ObjectDirectory; } } diff --git a/org.eclipse.jgit.http.server/src/org/eclipse/jgit/http/server/NoCacheFilter.java b/org.eclipse.jgit.http.server/src/org/eclipse/jgit/http/server/NoCacheFilter.java index 6a23cb95f5..65ec579fd8 100644 --- a/org.eclipse.jgit.http.server/src/org/eclipse/jgit/http/server/NoCacheFilter.java +++ b/org.eclipse.jgit.http.server/src/org/eclipse/jgit/http/server/NoCacheFilter.java @@ -1,44 +1,11 @@ /* - * Copyright (C) 2010, Google Inc. - * and other copyright owners as documented in the project's IP log. + * Copyright (C) 2010, Google Inc. and others * - * This program and the accompanying materials are made available - * under the terms of the Eclipse Distribution License v1.0 which - * accompanies this distribution, is reproduced below, and is - * available at http://www.eclipse.org/org/documents/edl-v10.php + * This program and the accompanying materials are made available under the + * terms of the Eclipse Distribution License v. 1.0 which is available at + * https://www.eclipse.org/org/documents/edl-v10.php. * - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or - * without modification, are permitted provided that the following - * conditions are met: - * - * - Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * - Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following - * disclaimer in the documentation and/or other materials provided - * with the distribution. - * - * - Neither the name of the Eclipse Foundation, Inc. nor the - * names of its contributors may be used to endorse or promote - * products derived from this software without specific prior - * written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND - * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES - * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR - * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT - * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; - * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER - * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, - * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF - * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * SPDX-License-Identifier: BSD-3-Clause */ package org.eclipse.jgit.http.server; @@ -49,24 +16,27 @@ import static org.eclipse.jgit.util.HttpSupport.HDR_PRAGMA; import java.io.IOException; -import javax.servlet.Filter; -import javax.servlet.FilterChain; -import javax.servlet.FilterConfig; -import javax.servlet.ServletException; -import javax.servlet.ServletRequest; -import javax.servlet.ServletResponse; -import javax.servlet.http.HttpServletResponse; +import jakarta.servlet.Filter; +import jakarta.servlet.FilterChain; +import jakarta.servlet.FilterConfig; +import jakarta.servlet.ServletException; +import jakarta.servlet.ServletRequest; +import jakarta.servlet.ServletResponse; +import jakarta.servlet.http.HttpServletResponse; -/** Adds HTTP response headers to prevent caching by proxies/browsers. */ +/** Add HTTP response headers to prevent caching by proxies/browsers. */ class NoCacheFilter implements Filter { + @Override public void init(FilterConfig config) throws ServletException { // Do nothing. } + @Override public void destroy() { // Do nothing. } + @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { HttpServletResponse rsp = (HttpServletResponse) response; diff --git a/org.eclipse.jgit.http.server/src/org/eclipse/jgit/http/server/ObjectFileServlet.java b/org.eclipse.jgit.http.server/src/org/eclipse/jgit/http/server/ObjectFileServlet.java index babc036dfc..81ff9630de 100644 --- a/org.eclipse.jgit.http.server/src/org/eclipse/jgit/http/server/ObjectFileServlet.java +++ b/org.eclipse.jgit.http.server/src/org/eclipse/jgit/http/server/ObjectFileServlet.java @@ -1,50 +1,17 @@ /* - * Copyright (C) 2009-2010, Google Inc. - * and other copyright owners as documented in the project's IP log. + * Copyright (C) 2009-2010, Google Inc. and others * - * This program and the accompanying materials are made available - * under the terms of the Eclipse Distribution License v1.0 which - * accompanies this distribution, is reproduced below, and is - * available at http://www.eclipse.org/org/documents/edl-v10.php + * This program and the accompanying materials are made available under the + * terms of the Eclipse Distribution License v. 1.0 which is available at + * https://www.eclipse.org/org/documents/edl-v10.php. * - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or - * without modification, are permitted provided that the following - * conditions are met: - * - * - Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * - Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following - * disclaimer in the documentation and/or other materials provided - * with the distribution. - * - * - Neither the name of the Eclipse Foundation, Inc. nor the - * names of its contributors may be used to endorse or promote - * products derived from this software without specific prior - * written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND - * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES - * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR - * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT - * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; - * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER - * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, - * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF - * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * SPDX-License-Identifier: BSD-3-Clause */ package org.eclipse.jgit.http.server; -import static javax.servlet.http.HttpServletResponse.SC_NOT_FOUND; -import static javax.servlet.http.HttpServletResponse.SC_NOT_MODIFIED; +import static jakarta.servlet.http.HttpServletResponse.SC_NOT_FOUND; +import static jakarta.servlet.http.HttpServletResponse.SC_NOT_MODIFIED; import static org.eclipse.jgit.http.server.ServletUtils.getRepository; import static org.eclipse.jgit.util.HttpSupport.HDR_ETAG; import static org.eclipse.jgit.util.HttpSupport.HDR_IF_MODIFIED_SINCE; @@ -54,11 +21,12 @@ import static org.eclipse.jgit.util.HttpSupport.HDR_LAST_MODIFIED; import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; +import java.time.Instant; -import javax.servlet.ServletException; -import javax.servlet.http.HttpServlet; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; +import jakarta.servlet.ServletException; +import jakarta.servlet.http.HttpServlet; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; import org.eclipse.jgit.internal.storage.file.ObjectDirectory; import org.eclipse.jgit.lib.Repository; @@ -75,12 +43,14 @@ abstract class ObjectFileServlet extends HttpServlet { } @Override - String etag(final FileSender sender) throws IOException { - return Long.toHexString(sender.getLastModified()); + String etag(FileSender sender) throws IOException { + Instant lastModified = sender.getLastModified(); + return Long.toHexString(lastModified.getEpochSecond()) + + Long.toHexString(lastModified.getNano()); } } - private static abstract class PackData extends ObjectFileServlet { + private abstract static class PackData extends ObjectFileServlet { private static final long serialVersionUID = 1L; PackData(String contentType) { @@ -88,7 +58,7 @@ abstract class ObjectFileServlet extends HttpServlet { } @Override - String etag(final FileSender sender) throws IOException { + String etag(FileSender sender) throws IOException { return sender.getTailChecksum(); } } @@ -111,7 +81,7 @@ abstract class ObjectFileServlet extends HttpServlet { private final String contentType; - ObjectFileServlet(final String contentType) { + ObjectFileServlet(String contentType) { this.contentType = contentType; } @@ -143,7 +113,9 @@ abstract class ObjectFileServlet extends HttpServlet { try { final String etag = etag(sender); - final long lastModified = (sender.getLastModified() / 1000) * 1000; + // HTTP header Last-Modified header has a resolution of 1 sec, see + // https://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.29 + final long lastModified = sender.getLastModified().getEpochSecond(); String ifNoneMatch = req.getHeader(HDR_IF_NONE_MATCH); if (etag != null && etag.equals(ifNoneMatch)) { @@ -168,7 +140,7 @@ abstract class ObjectFileServlet extends HttpServlet { } } - private static File objects(final HttpServletRequest req) { + private static File objects(HttpServletRequest req) { final Repository db = getRepository(req); return ((ObjectDirectory) db.getObjectDatabase()).getDirectory(); } diff --git a/org.eclipse.jgit.http.server/src/org/eclipse/jgit/http/server/ReceivePackErrorHandler.java b/org.eclipse.jgit.http.server/src/org/eclipse/jgit/http/server/ReceivePackErrorHandler.java new file mode 100644 index 0000000000..843a11556e --- /dev/null +++ b/org.eclipse.jgit.http.server/src/org/eclipse/jgit/http/server/ReceivePackErrorHandler.java @@ -0,0 +1,66 @@ +/* + * Copyright (c) 2019, Google LLC and others + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Distribution License v. 1.0 which is available at + * http://www.eclipse.org/org/documents/edl-v10.php. + * + * SPDX-License-Identifier: BSD-3-Clause + */ +package org.eclipse.jgit.http.server; + +import java.io.IOException; + +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; + +import org.eclipse.jgit.transport.ReceivePack; +import org.eclipse.jgit.transport.ServiceMayNotContinueException; + +/** + * Handle git-receive-pack errors. + * + * <p> + * This is an entry point for customizing an error handler for git-receive-pack. + * Right before calling {@link ReceivePack#receiveWithExceptionPropagation}, + * JGit will call this handler if specified through {@link GitFilter}. The + * implementation of this handler is responsible for calling + * {@link ReceivePackRunnable} and handling exceptions for clients. + * + * <p> + * If a custom handler is not specified, JGit will use the default error + * handler. + * + * @since 5.7 + */ +public interface ReceivePackErrorHandler { + /** + * Receive pack + * + * @param req + * The HTTP request + * @param rsp + * The HTTP response + * @param r + * A continuation that handles a git-receive-pack request. + * @throws IOException + * if an IO error occurred + * @since 7.0 + */ + void receive(HttpServletRequest req, HttpServletResponse rsp, + ReceivePackRunnable r) throws IOException; + + /** Process a git-receive-pack request. */ + public interface ReceivePackRunnable { + /** + * See {@link ReceivePack#receiveWithExceptionPropagation}. + * + * @throws ServiceMayNotContinueException + * if transport service cannot continue + * @throws IOException + * if an IO error occurred + */ + void receive() throws ServiceMayNotContinueException, IOException; + } + +} diff --git a/org.eclipse.jgit.http.server/src/org/eclipse/jgit/http/server/ReceivePackServlet.java b/org.eclipse.jgit.http.server/src/org/eclipse/jgit/http/server/ReceivePackServlet.java index 030287b5cf..959d6a54cb 100644 --- a/org.eclipse.jgit.http.server/src/org/eclipse/jgit/http/server/ReceivePackServlet.java +++ b/org.eclipse.jgit.http.server/src/org/eclipse/jgit/http/server/ReceivePackServlet.java @@ -1,56 +1,19 @@ /* - * Copyright (C) 2009-2010, Google Inc. - * and other copyright owners as documented in the project's IP log. + * Copyright (C) 2009-2010, Google Inc. and others * - * This program and the accompanying materials are made available - * under the terms of the Eclipse Distribution License v1.0 which - * accompanies this distribution, is reproduced below, and is - * available at http://www.eclipse.org/org/documents/edl-v10.php + * This program and the accompanying materials are made available under the + * terms of the Eclipse Distribution License v. 1.0 which is available at + * https://www.eclipse.org/org/documents/edl-v10.php. * - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or - * without modification, are permitted provided that the following - * conditions are met: - * - * - Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * - Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following - * disclaimer in the documentation and/or other materials provided - * with the distribution. - * - * - Neither the name of the Eclipse Foundation, Inc. nor the - * names of its contributors may be used to endorse or promote - * products derived from this software without specific prior - * written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND - * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES - * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR - * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT - * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; - * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER - * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, - * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF - * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * SPDX-License-Identifier: BSD-3-Clause */ 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; -import static javax.servlet.http.HttpServletResponse.SC_UNSUPPORTED_MEDIA_TYPE; -import static org.eclipse.jgit.http.server.ClientVersionUtil.hasChunkedEncodingRequestBug; -import static org.eclipse.jgit.http.server.ClientVersionUtil.hasPushStatusBug; -import static org.eclipse.jgit.http.server.ClientVersionUtil.parseVersion; +import static jakarta.servlet.http.HttpServletResponse.SC_FORBIDDEN; +import static jakarta.servlet.http.HttpServletResponse.SC_INTERNAL_SERVER_ERROR; +import static jakarta.servlet.http.HttpServletResponse.SC_UNAUTHORIZED; +import static jakarta.servlet.http.HttpServletResponse.SC_UNSUPPORTED_MEDIA_TYPE; import static org.eclipse.jgit.http.server.GitSmartHttpTools.RECEIVE_PACK; import static org.eclipse.jgit.http.server.GitSmartHttpTools.RECEIVE_PACK_REQUEST_TYPE; import static org.eclipse.jgit.http.server.GitSmartHttpTools.RECEIVE_PACK_RESULT_TYPE; @@ -65,17 +28,19 @@ import java.io.IOException; import java.text.MessageFormat; import java.util.List; -import javax.servlet.Filter; -import javax.servlet.FilterChain; -import javax.servlet.FilterConfig; -import javax.servlet.ServletException; -import javax.servlet.ServletRequest; -import javax.servlet.ServletResponse; -import javax.servlet.http.HttpServlet; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; - +import jakarta.servlet.Filter; +import jakarta.servlet.FilterChain; +import jakarta.servlet.FilterConfig; +import jakarta.servlet.ServletException; +import jakarta.servlet.ServletRequest; +import jakarta.servlet.ServletResponse; +import jakarta.servlet.http.HttpServlet; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; + +import org.eclipse.jgit.annotations.Nullable; import org.eclipse.jgit.errors.CorruptObjectException; +import org.eclipse.jgit.errors.PackProtocolException; import org.eclipse.jgit.errors.UnpackException; import org.eclipse.jgit.lib.Repository; import org.eclipse.jgit.transport.InternalHttpServerGlue; @@ -129,6 +94,7 @@ class ReceivePackServlet extends HttpServlet { this.receivePackFactory = receivePackFactory; } + @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { HttpServletRequest req = (HttpServletRequest) request; @@ -137,11 +103,10 @@ class ReceivePackServlet extends HttpServlet { try { rp = receivePackFactory.create(req, getRepository(req)); } catch (ServiceNotAuthorizedException e) { - rsp.sendError(SC_UNAUTHORIZED); + rsp.sendError(SC_UNAUTHORIZED, e.getMessage()); return; - } catch (ServiceNotEnabledException e) { - sendError(req, rsp, SC_FORBIDDEN); + sendError(req, rsp, SC_FORBIDDEN, e.getMessage()); return; } @@ -153,15 +118,24 @@ class ReceivePackServlet extends HttpServlet { } } + @Override public void init(FilterConfig filterConfig) throws ServletException { // Nothing. } + @Override public void destroy() { // Nothing. } } + @Nullable + private final ReceivePackErrorHandler handler; + + ReceivePackServlet(@Nullable ReceivePackErrorHandler handler) { + this.handler = handler; + } + @Override public void doPost(final HttpServletRequest req, final HttpServletResponse rsp) throws IOException { @@ -170,13 +144,6 @@ class ReceivePackServlet extends HttpServlet { return; } - int[] version = parseVersion(req.getHeader(HDR_USER_AGENT)); - if (hasChunkedEncodingRequestBug(version, req)) { - GitSmartHttpTools.sendError(req, rsp, SC_BAD_REQUEST, "\n\n" - + HttpServerText.get().clientHas175ChunkedEncodingBug); - return; - } - SmartOutputStream out = new SmartOutputStream(req, rsp, false) { @Override public void flush() throws IOException { @@ -185,35 +152,42 @@ class ReceivePackServlet extends HttpServlet { }; ReceivePack rp = (ReceivePack) req.getAttribute(ATTRIBUTE_HANDLER); - try { - rp.setBiDirectionalPipe(false); - rp.setEchoCommandFailures(hasPushStatusBug(version)); - rsp.setContentType(RECEIVE_PACK_RESULT_TYPE); - - rp.receive(getInputStream(req), out, null); - out.close(); - } catch (CorruptObjectException e ) { - // This should be already reported to the client. - getServletContext().log(MessageFormat.format( - HttpServerText.get().receivedCorruptObject, - e.getMessage(), - ServletUtils.identify(rp.getRepository()))); - consumeRequestBody(req); - out.close(); - - } catch (UnpackException e) { - // This should be already reported to the client. - log(rp.getRepository(), e.getCause()); - consumeRequestBody(req); - out.close(); - - } catch (Throwable e) { - log(rp.getRepository(), e); - if (!rsp.isCommitted()) { - rsp.reset(); - sendError(req, rsp, SC_INTERNAL_SERVER_ERROR); + rp.setBiDirectionalPipe(false); + rsp.setContentType(RECEIVE_PACK_RESULT_TYPE); + + if (handler != null) { + handler.receive(req, rsp, () -> { + rp.receiveWithExceptionPropagation(getInputStream(req), out, + null); + out.close(); + }); + } else { + try { + rp.receive(getInputStream(req), out, null); + out.close(); + } catch (CorruptObjectException e ) { + // This should be already reported to the client. + getServletContext().log(MessageFormat.format( + HttpServerText.get().receivedCorruptObject, + e.getMessage(), + ServletUtils.identify(rp.getRepository()))); + consumeRequestBody(req); + out.close(); + + } catch (UnpackException | PackProtocolException e) { + // This should be already reported to the client. + log(rp.getRepository(), e.getCause()); + consumeRequestBody(req); + out.close(); + + } catch (Throwable e) { + log(rp.getRepository(), e); + if (!rsp.isCommitted()) { + rsp.reset(); + sendError(req, rsp, SC_INTERNAL_SERVER_ERROR); + } + return; } - return; } } diff --git a/org.eclipse.jgit.http.server/src/org/eclipse/jgit/http/server/RepositoryFilter.java b/org.eclipse.jgit.http.server/src/org/eclipse/jgit/http/server/RepositoryFilter.java index 58404020cd..ab45b460f9 100644 --- a/org.eclipse.jgit.http.server/src/org/eclipse/jgit/http/server/RepositoryFilter.java +++ b/org.eclipse.jgit.http.server/src/org/eclipse/jgit/http/server/RepositoryFilter.java @@ -1,67 +1,34 @@ /* - * Copyright (C) 2009-2010, Google Inc. - * and other copyright owners as documented in the project's IP log. + * Copyright (C) 2009-2010, Google Inc. and others * - * This program and the accompanying materials are made available - * under the terms of the Eclipse Distribution License v1.0 which - * accompanies this distribution, is reproduced below, and is - * available at http://www.eclipse.org/org/documents/edl-v10.php + * This program and the accompanying materials are made available under the + * terms of the Eclipse Distribution License v. 1.0 which is available at + * https://www.eclipse.org/org/documents/edl-v10.php. * - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or - * without modification, are permitted provided that the following - * conditions are met: - * - * - Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * - Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following - * disclaimer in the documentation and/or other materials provided - * with the distribution. - * - * - Neither the name of the Eclipse Foundation, Inc. nor the - * names of its contributors may be used to endorse or promote - * products derived from this software without specific prior - * written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND - * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES - * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR - * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT - * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; - * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER - * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, - * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF - * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * SPDX-License-Identifier: BSD-3-Clause */ package org.eclipse.jgit.http.server; -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_NOT_FOUND; -import static javax.servlet.http.HttpServletResponse.SC_UNAUTHORIZED; +import static jakarta.servlet.http.HttpServletResponse.SC_FORBIDDEN; +import static jakarta.servlet.http.HttpServletResponse.SC_INTERNAL_SERVER_ERROR; +import static jakarta.servlet.http.HttpServletResponse.SC_NOT_FOUND; +import static jakarta.servlet.http.HttpServletResponse.SC_UNAUTHORIZED; import static org.eclipse.jgit.http.server.GitSmartHttpTools.sendError; import static org.eclipse.jgit.http.server.ServletUtils.ATTRIBUTE_REPOSITORY; import java.io.IOException; import java.text.MessageFormat; -import javax.servlet.Filter; -import javax.servlet.FilterChain; -import javax.servlet.FilterConfig; -import javax.servlet.ServletContext; -import javax.servlet.ServletException; -import javax.servlet.ServletRequest; -import javax.servlet.ServletResponse; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; +import jakarta.servlet.Filter; +import jakarta.servlet.FilterChain; +import jakarta.servlet.FilterConfig; +import jakarta.servlet.ServletContext; +import jakarta.servlet.ServletException; +import jakarta.servlet.ServletRequest; +import jakarta.servlet.ServletResponse; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; import org.eclipse.jgit.errors.RepositoryNotFoundException; import org.eclipse.jgit.lib.Repository; @@ -71,16 +38,19 @@ import org.eclipse.jgit.transport.resolver.ServiceNotAuthorizedException; import org.eclipse.jgit.transport.resolver.ServiceNotEnabledException; /** - * Opens a repository named by the path info through {@link RepositoryResolver}. + * Open a repository named by the path info through + * {@link org.eclipse.jgit.transport.resolver.RepositoryResolver}. * <p> - * This filter assumes it is invoked by {@link GitServlet} and is likely to not - * work as expected if called from any other class. This filter assumes the path - * info of the current request is a repository name which can be used by the - * configured {@link RepositoryResolver} to open a {@link Repository} and attach - * it to the current request. + * This filter assumes it is invoked by + * {@link org.eclipse.jgit.http.server.GitServlet} and is likely to not work as + * expected if called from any other class. This filter assumes the path info of + * the current request is a repository name which can be used by the configured + * {@link org.eclipse.jgit.transport.resolver.RepositoryResolver} to open a + * {@link org.eclipse.jgit.lib.Repository} and attach it to the current request. * <p> - * This filter sets request attribute {@link ServletUtils#ATTRIBUTE_REPOSITORY} - * when it discovers the repository, and automatically closes and removes the + * This filter sets request attribute + * {@link org.eclipse.jgit.http.server.ServletUtils#ATTRIBUTE_REPOSITORY} when + * it discovers the repository, and automatically closes and removes the * attribute when the request is complete. */ public class RepositoryFilter implements Filter { @@ -93,21 +63,25 @@ public class RepositoryFilter implements Filter { * * @param resolver * the resolver which will be used to translate the URL name - * component to the actual {@link Repository} instance for the + * component to the actual + * {@link org.eclipse.jgit.lib.Repository} instance for the * current web request. */ - public RepositoryFilter(final RepositoryResolver<HttpServletRequest> resolver) { + public RepositoryFilter(RepositoryResolver<HttpServletRequest> resolver) { this.resolver = resolver; } - public void init(final FilterConfig config) throws ServletException { + @Override + public void init(FilterConfig config) throws ServletException { context = config.getServletContext(); } + @Override public void destroy() { context = null; } + @Override public void doFilter(final ServletRequest request, final ServletResponse response, final FilterChain chain) throws IOException, ServletException { @@ -130,28 +104,23 @@ public class RepositoryFilter implements Filter { return; } - final Repository db; - try { - db = resolver.open(req, name); + try (Repository db = resolver.open(req, name)) { + request.setAttribute(ATTRIBUTE_REPOSITORY, db); + chain.doFilter(request, response); } catch (RepositoryNotFoundException e) { sendError(req, res, SC_NOT_FOUND); return; } catch (ServiceNotEnabledException e) { - sendError(req, res, SC_FORBIDDEN); + sendError(req, res, SC_FORBIDDEN, e.getMessage()); return; } catch (ServiceNotAuthorizedException e) { - res.sendError(SC_UNAUTHORIZED); + res.sendError(SC_UNAUTHORIZED, e.getMessage()); return; } catch (ServiceMayNotContinueException e) { - sendError(req, res, SC_FORBIDDEN, e.getMessage()); + sendError(req, res, e.getStatusCode(), e.getMessage()); return; - } - try { - request.setAttribute(ATTRIBUTE_REPOSITORY, db); - chain.doFilter(request, response); } finally { request.removeAttribute(ATTRIBUTE_REPOSITORY); - db.close(); } } } diff --git a/org.eclipse.jgit.http.server/src/org/eclipse/jgit/http/server/ServletUtils.java b/org.eclipse.jgit.http.server/src/org/eclipse/jgit/http/server/ServletUtils.java index 042ccf3dd7..79815c2c06 100644 --- a/org.eclipse.jgit.http.server/src/org/eclipse/jgit/http/server/ServletUtils.java +++ b/org.eclipse.jgit.http.server/src/org/eclipse/jgit/http/server/ServletUtils.java @@ -1,49 +1,18 @@ /* - * Copyright (C) 2009-2010, Google Inc. - * and other copyright owners as documented in the project's IP log. + * Copyright (C) 2009-2010, Google Inc. and others * - * This program and the accompanying materials are made available - * under the terms of the Eclipse Distribution License v1.0 which - * accompanies this distribution, is reproduced below, and is - * available at http://www.eclipse.org/org/documents/edl-v10.php + * This program and the accompanying materials are made available under the + * terms of the Eclipse Distribution License v. 1.0 which is available at + * https://www.eclipse.org/org/documents/edl-v10.php. * - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or - * without modification, are permitted provided that the following - * conditions are met: - * - * - Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * - Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following - * disclaimer in the documentation and/or other materials provided - * with the distribution. - * - * - Neither the name of the Eclipse Foundation, Inc. nor the - * names of its contributors may be used to endorse or promote - * products derived from this software without specific prior - * written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND - * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES - * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR - * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT - * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; - * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER - * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, - * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF - * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * SPDX-License-Identifier: BSD-3-Clause */ package org.eclipse.jgit.http.server; +import static java.nio.charset.StandardCharsets.UTF_8; import static org.eclipse.jgit.util.HttpSupport.ENCODING_GZIP; +import static org.eclipse.jgit.util.HttpSupport.ENCODING_X_GZIP; import static org.eclipse.jgit.util.HttpSupport.HDR_ACCEPT_ENCODING; import static org.eclipse.jgit.util.HttpSupport.HDR_CONTENT_ENCODING; import static org.eclipse.jgit.util.HttpSupport.HDR_ETAG; @@ -58,16 +27,17 @@ import java.text.MessageFormat; import java.util.zip.GZIPInputStream; import java.util.zip.GZIPOutputStream; -import javax.servlet.ServletRequest; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; +import jakarta.servlet.ServletRequest; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; -import org.eclipse.jgit.internal.storage.dfs.DfsRepository; import org.eclipse.jgit.lib.Constants; import org.eclipse.jgit.lib.ObjectId; import org.eclipse.jgit.lib.Repository; -/** Common utility functions for servlets. */ +/** + * Common utility functions for servlets. + */ public final class ServletUtils { /** Request attribute which stores the {@link Repository} instance. */ public static final String ATTRIBUTE_REPOSITORY = "org.eclipse.jgit.Repository"; @@ -86,8 +56,9 @@ public final class ServletUtils { * being invoked incorrectly and the programmer should ensure * the filter runs before the servlet. * @see #ATTRIBUTE_REPOSITORY + * @since 7.0 */ - public static Repository getRepository(final ServletRequest req) { + public static Repository getRepository(ServletRequest req) { Repository db = (Repository) req.getAttribute(ATTRIBUTE_REPOSITORY); if (db == null) throw new IllegalStateException(HttpServerText.get().expectedRepositoryAttribute); @@ -106,12 +77,13 @@ public final class ServletUtils { * @return an input stream to read the raw, uncompressed request body. * @throws IOException * if an input or output exception occurred. + * @since 7.0 */ - public static InputStream getInputStream(final HttpServletRequest req) + public static InputStream getInputStream(HttpServletRequest req) throws IOException { InputStream in = req.getInputStream(); final String enc = req.getHeader(HDR_CONTENT_ENCODING); - if (ENCODING_GZIP.equals(enc) || "x-gzip".equals(enc)) //$NON-NLS-1$ + if (ENCODING_GZIP.equals(enc) || ENCODING_X_GZIP.equals(enc)) in = new GZIPInputStream(in); else if (enc != null) throw new IOException(MessageFormat.format(HttpServerText.get().encodingNotSupportedByThisLibrary @@ -124,6 +96,7 @@ public final class ServletUtils { * * @param req * the request whose body must be consumed. + * @since 7.0 */ public static void consumeRequestBody(HttpServletRequest req) { if (0 < req.getContentLength() || isChunked(req)) { @@ -184,13 +157,14 @@ public final class ServletUtils { * the outgoing response. * @throws IOException * the servlet API rejected sending the body. + * @since 7.0 */ public static void sendPlainText(final String content, final HttpServletRequest req, final HttpServletResponse rsp) throws IOException { - final byte[] raw = content.getBytes(Constants.CHARACTER_ENCODING); + final byte[] raw = content.getBytes(UTF_8); rsp.setContentType(TEXT_PLAIN); - rsp.setCharacterEncoding(Constants.CHARACTER_ENCODING); + rsp.setCharacterEncoding(UTF_8.name()); send(raw, req, rsp); } @@ -213,16 +187,14 @@ public final class ServletUtils { * the outgoing response. * @throws IOException * the servlet API rejected sending the body. + * @since 7.0 */ public static void send(byte[] content, final HttpServletRequest req, final HttpServletResponse rsp) throws IOException { content = sendInit(content, req, rsp); - final OutputStream out = rsp.getOutputStream(); - try { + try (OutputStream out = rsp.getOutputStream()) { out.write(content); out.flush(); - } finally { - out.close(); } } @@ -238,7 +210,7 @@ public final class ServletUtils { return content; } - static boolean acceptsGzipEncoding(final HttpServletRequest req) { + static boolean acceptsGzipEncoding(HttpServletRequest req) { return acceptsGzipEncoding(req.getHeader(HDR_ACCEPT_ENCODING)); } @@ -258,7 +230,7 @@ public final class ServletUtils { return false; } - private static byte[] compress(final byte[] raw) throws IOException { + private static byte[] compress(byte[] raw) throws IOException { final int maxLen = raw.length + 32; final ByteArrayOutputStream out = new ByteArrayOutputStream(maxLen); final GZIPOutputStream gz = new GZIPOutputStream(out); @@ -268,19 +240,18 @@ public final class ServletUtils { return out.toByteArray(); } - private static String etag(final byte[] content) { + private static String etag(byte[] content) { final MessageDigest md = Constants.newMessageDigest(); md.update(content); return ObjectId.fromRaw(md.digest()).getName(); } static String identify(Repository git) { - if (git instanceof DfsRepository) { - return ((DfsRepository) git).getDescription().getRepositoryName(); - } else if (git.getDirectory() != null) { - return git.getDirectory().getPath(); + String identifier = git.getIdentifier(); + if (identifier == null) { + return "unknown"; } - return "unknown"; + return identifier; } private ServletUtils() { diff --git a/org.eclipse.jgit.http.server/src/org/eclipse/jgit/http/server/SmartOutputStream.java b/org.eclipse.jgit.http.server/src/org/eclipse/jgit/http/server/SmartOutputStream.java index 145c63bcaf..19df895aac 100644 --- a/org.eclipse.jgit.http.server/src/org/eclipse/jgit/http/server/SmartOutputStream.java +++ b/org.eclipse.jgit.http.server/src/org/eclipse/jgit/http/server/SmartOutputStream.java @@ -1,44 +1,11 @@ /* - * Copyright (C) 2010, Google Inc. - * and other copyright owners as documented in the project's IP log. + * Copyright (C) 2010, Google Inc. and others * - * This program and the accompanying materials are made available - * under the terms of the Eclipse Distribution License v1.0 which - * accompanies this distribution, is reproduced below, and is - * available at http://www.eclipse.org/org/documents/edl-v10.php + * This program and the accompanying materials are made available under the + * terms of the Eclipse Distribution License v. 1.0 which is available at + * https://www.eclipse.org/org/documents/edl-v10.php. * - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or - * without modification, are permitted provided that the following - * conditions are met: - * - * - Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * - Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following - * disclaimer in the documentation and/or other materials provided - * with the distribution. - * - * - Neither the name of the Eclipse Foundation, Inc. nor the - * names of its contributors may be used to endorse or promote - * products derived from this software without specific prior - * written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND - * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES - * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR - * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT - * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; - * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER - * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, - * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF - * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * SPDX-License-Identifier: BSD-3-Clause */ package org.eclipse.jgit.http.server; @@ -51,8 +18,8 @@ import java.io.IOException; import java.io.OutputStream; import java.util.zip.GZIPOutputStream; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; import org.eclipse.jgit.util.TemporaryBuffer; @@ -95,6 +62,7 @@ class SmartOutputStream extends TemporaryBuffer { return out; } + @Override public void close() throws IOException { super.close(); @@ -102,20 +70,16 @@ class SmartOutputStream extends TemporaryBuffer { // If output hasn't started yet, the entire thing fit into our // buffer. Try to use a proper Content-Length header, and also // deflate the response with gzip if it will be smaller. - TemporaryBuffer out = this; - - if (256 < out.length() && acceptsGzipEncoding(req)) { + if (256 < this.length() && acceptsGzipEncoding(req)) { TemporaryBuffer gzbuf = new TemporaryBuffer.Heap(LIMIT); try { - GZIPOutputStream gzip = new GZIPOutputStream(gzbuf); - try { - out.writeTo(gzip, null); - } finally { - gzip.close(); + try (GZIPOutputStream gzip = new GZIPOutputStream(gzbuf)) { + this.writeTo(gzip, null); } - if (gzbuf.length() < out.length()) { - out = gzbuf; + if (gzbuf.length() < this.length()) { rsp.setHeader(HDR_CONTENT_ENCODING, ENCODING_GZIP); + writeResponse(gzbuf); + return; } } catch (IOException err) { // Most likely caused by overflowing the buffer, meaning @@ -123,18 +87,18 @@ class SmartOutputStream extends TemporaryBuffer { // copy and use the original. } } + writeResponse(this); + } + } - // The Content-Length cannot overflow when cast to an int, our - // hardcoded LIMIT constant above assures us we wouldn't store - // more than 2 GiB of content in memory. - rsp.setContentLength((int) out.length()); - final OutputStream os = rsp.getOutputStream(); - try { - out.writeTo(os, null); - os.flush(); - } finally { - os.close(); - } + private void writeResponse(TemporaryBuffer out) throws IOException { + // The Content-Length cannot overflow when cast to an int, our + // hardcoded LIMIT constant above assures us we wouldn't store + // more than 2 GiB of content in memory. + rsp.setContentLength((int) out.length()); + try (OutputStream os = rsp.getOutputStream()) { + out.writeTo(os, null); + os.flush(); } } } diff --git a/org.eclipse.jgit.http.server/src/org/eclipse/jgit/http/server/SmartServiceInfoRefs.java b/org.eclipse.jgit.http.server/src/org/eclipse/jgit/http/server/SmartServiceInfoRefs.java index 51332194b8..d2d6015379 100644 --- a/org.eclipse.jgit.http.server/src/org/eclipse/jgit/http/server/SmartServiceInfoRefs.java +++ b/org.eclipse.jgit.http.server/src/org/eclipse/jgit/http/server/SmartServiceInfoRefs.java @@ -1,50 +1,17 @@ /* - * Copyright (C) 2009-2010, Google Inc. - * and other copyright owners as documented in the project's IP log. + * Copyright (C) 2009-2010, Google Inc. and others * - * This program and the accompanying materials are made available - * under the terms of the Eclipse Distribution License v1.0 which - * accompanies this distribution, is reproduced below, and is - * available at http://www.eclipse.org/org/documents/edl-v10.php + * This program and the accompanying materials are made available under the + * terms of the Eclipse Distribution License v. 1.0 which is available at + * https://www.eclipse.org/org/documents/edl-v10.php. * - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or - * without modification, are permitted provided that the following - * conditions are met: - * - * - Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * - Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following - * disclaimer in the documentation and/or other materials provided - * with the distribution. - * - * - Neither the name of the Eclipse Foundation, Inc. nor the - * names of its contributors may be used to endorse or promote - * products derived from this software without specific prior - * written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND - * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES - * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR - * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT - * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; - * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER - * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, - * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF - * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * SPDX-License-Identifier: BSD-3-Clause */ package org.eclipse.jgit.http.server; -import static javax.servlet.http.HttpServletResponse.SC_FORBIDDEN; -import static javax.servlet.http.HttpServletResponse.SC_UNAUTHORIZED; +import static jakarta.servlet.http.HttpServletResponse.SC_FORBIDDEN; +import static jakarta.servlet.http.HttpServletResponse.SC_UNAUTHORIZED; import static org.eclipse.jgit.http.server.GitSmartHttpTools.infoRefsResultType; import static org.eclipse.jgit.http.server.GitSmartHttpTools.sendError; import static org.eclipse.jgit.http.server.ServletUtils.ATTRIBUTE_HANDLER; @@ -53,14 +20,14 @@ import static org.eclipse.jgit.http.server.ServletUtils.getRepository; import java.io.IOException; import java.util.List; -import javax.servlet.Filter; -import javax.servlet.FilterChain; -import javax.servlet.FilterConfig; -import javax.servlet.ServletException; -import javax.servlet.ServletRequest; -import javax.servlet.ServletResponse; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; +import jakarta.servlet.Filter; +import jakarta.servlet.FilterChain; +import jakarta.servlet.FilterConfig; +import jakarta.servlet.ServletException; +import jakarta.servlet.ServletRequest; +import jakarta.servlet.ServletResponse; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; import org.eclipse.jgit.lib.Repository; import org.eclipse.jgit.transport.PacketLineOut; @@ -75,19 +42,22 @@ abstract class SmartServiceInfoRefs implements Filter { private final Filter[] filters; - SmartServiceInfoRefs(final String service, final List<Filter> filters) { + SmartServiceInfoRefs(String service, List<Filter> filters) { this.svc = service; - this.filters = filters.toArray(new Filter[filters.size()]); + this.filters = filters.toArray(new Filter[0]); } + @Override public void init(FilterConfig config) throws ServletException { // Do nothing. } + @Override public void destroy() { // Do nothing. } + @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { final HttpServletRequest req = (HttpServletRequest) request; @@ -98,7 +68,7 @@ abstract class SmartServiceInfoRefs implements Filter { try { begin(req, db); } catch (ServiceNotAuthorizedException e) { - res.sendError(SC_UNAUTHORIZED); + res.sendError(SC_UNAUTHORIZED, e.getMessage()); return; } catch (ServiceNotEnabledException e) { sendError(req, res, SC_FORBIDDEN, e.getMessage()); @@ -127,35 +97,96 @@ abstract class SmartServiceInfoRefs implements Filter { res.setContentType(infoRefsResultType(svc)); final PacketLineOut out = new PacketLineOut(buf); - out.writeString("# service=" + svc + "\n"); - out.end(); - advertise(req, new PacketLineOutRefAdvertiser(out)); + respond(req, out, svc); buf.close(); } catch (ServiceNotAuthorizedException e) { - res.sendError(SC_UNAUTHORIZED); - + res.sendError(SC_UNAUTHORIZED, e.getMessage()); } catch (ServiceNotEnabledException e) { - sendError(req, res, SC_FORBIDDEN); - + sendError(req, res, SC_FORBIDDEN, e.getMessage()); } catch (ServiceMayNotContinueException e) { if (e.isOutput()) buf.close(); else - sendError(req, res, SC_FORBIDDEN, e.getMessage()); + sendError(req, res, e.getStatusCode(), e.getMessage()); } } + /** + * Begin service. + * + * @param req + * request + * @param db + * repository + * @throws IOException + * if an IO error occurred + * @throws ServiceNotEnabledException + * if a service is not available + * @throws ServiceNotAuthorizedException + * if service requires authentication and the current user + * didn't provide credentials + */ protected abstract void begin(HttpServletRequest req, Repository db) throws IOException, ServiceNotEnabledException, ServiceNotAuthorizedException; + /** + * Advertise. + * + * @param req + * request + * @param pck + * used to frame lines in PacketLineOut format + * @throws IOException + * if an IO error occurred + * @throws ServiceNotEnabledException + * if a service is not available + * @throws ServiceNotAuthorizedException + * if service requires authentication and the current user + * didn't provide credentials + */ protected abstract void advertise(HttpServletRequest req, PacketLineOutRefAdvertiser pck) throws IOException, ServiceNotEnabledException, ServiceNotAuthorizedException; + /** + * Writes the appropriate response to an info/refs request received by a + * smart service. In protocol v0, this starts with "# service=serviceName" + * followed by a flush packet, but this is not necessarily the case in other + * protocol versions. + * <p> + * The default implementation writes "# service=serviceName" and a flush + * packet, then calls {@link #advertise}. Subclasses should override this + * method if they support protocol versions other than protocol v0. + * + * @param req + * request + * @param pckOut + * destination of response + * @param serviceName + * service name to be written out in protocol v0; may or may not + * be used in other versions + * @throws IOException + * if an IO error occurred + * @throws ServiceNotEnabledException + * if a service is not available + * @throws ServiceNotAuthorizedException + * if service requires authentication and the current user + * didn't provide credentials + */ + protected void respond(HttpServletRequest req, + PacketLineOut pckOut, String serviceName) + throws IOException, ServiceNotEnabledException, + ServiceNotAuthorizedException { + pckOut.writeString("# service=" + svc + '\n'); //$NON-NLS-1$ + pckOut.end(); + advertise(req, new PacketLineOutRefAdvertiser(pckOut)); + } + private class Chain implements FilterChain { private int filterIdx; + @Override public void doFilter(ServletRequest req, ServletResponse rsp) throws IOException, ServletException { if (filterIdx < filters.length) diff --git a/org.eclipse.jgit.http.server/src/org/eclipse/jgit/http/server/TextFileServlet.java b/org.eclipse.jgit.http.server/src/org/eclipse/jgit/http/server/TextFileServlet.java index 650059bd38..b857827339 100644 --- a/org.eclipse.jgit.http.server/src/org/eclipse/jgit/http/server/TextFileServlet.java +++ b/org.eclipse.jgit.http.server/src/org/eclipse/jgit/http/server/TextFileServlet.java @@ -1,49 +1,16 @@ /* - * Copyright (C) 2009-2010, Google Inc. - * and other copyright owners as documented in the project's IP log. + * Copyright (C) 2009-2010, Google Inc. and others * - * This program and the accompanying materials are made available - * under the terms of the Eclipse Distribution License v1.0 which - * accompanies this distribution, is reproduced below, and is - * available at http://www.eclipse.org/org/documents/edl-v10.php + * This program and the accompanying materials are made available under the + * terms of the Eclipse Distribution License v. 1.0 which is available at + * https://www.eclipse.org/org/documents/edl-v10.php. * - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or - * without modification, are permitted provided that the following - * conditions are met: - * - * - Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * - Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following - * disclaimer in the documentation and/or other materials provided - * with the distribution. - * - * - Neither the name of the Eclipse Foundation, Inc. nor the - * names of its contributors may be used to endorse or promote - * products derived from this software without specific prior - * written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND - * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES - * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR - * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT - * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; - * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER - * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, - * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF - * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * SPDX-License-Identifier: BSD-3-Clause */ package org.eclipse.jgit.http.server; -import static javax.servlet.http.HttpServletResponse.SC_NOT_FOUND; +import static jakarta.servlet.http.HttpServletResponse.SC_NOT_FOUND; import static org.eclipse.jgit.http.server.ServletUtils.getRepository; import static org.eclipse.jgit.http.server.ServletUtils.send; @@ -51,9 +18,9 @@ import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; -import javax.servlet.http.HttpServlet; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; +import jakarta.servlet.http.HttpServlet; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; import org.eclipse.jgit.util.HttpSupport; import org.eclipse.jgit.util.IO; @@ -64,10 +31,11 @@ class TextFileServlet extends HttpServlet { private final String fileName; - TextFileServlet(final String name) { + TextFileServlet(String name) { this.fileName = name; } + @Override public void doGet(final HttpServletRequest req, final HttpServletResponse rsp) throws IOException { try { @@ -78,7 +46,7 @@ class TextFileServlet extends HttpServlet { } } - private byte[] read(final HttpServletRequest req) throws IOException { + private byte[] read(HttpServletRequest req) throws IOException { final File gitdir = getRepository(req).getDirectory(); if (gitdir == null) throw new FileNotFoundException(fileName); diff --git a/org.eclipse.jgit.http.server/src/org/eclipse/jgit/http/server/UploadPackErrorHandler.java b/org.eclipse.jgit.http.server/src/org/eclipse/jgit/http/server/UploadPackErrorHandler.java new file mode 100644 index 0000000000..5159f6cba0 --- /dev/null +++ b/org.eclipse.jgit.http.server/src/org/eclipse/jgit/http/server/UploadPackErrorHandler.java @@ -0,0 +1,90 @@ +/* + * Copyright (c) 2019, Google LLC and others + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Distribution License v. 1.0 which is available at + * http://www.eclipse.org/org/documents/edl-v10.php. + * + * SPDX-License-Identifier: BSD-3-Clause + */ +package org.eclipse.jgit.http.server; + +import static jakarta.servlet.http.HttpServletResponse.SC_FORBIDDEN; +import static jakarta.servlet.http.HttpServletResponse.SC_INTERNAL_SERVER_ERROR; +import static jakarta.servlet.http.HttpServletResponse.SC_OK; + +import java.io.IOException; + +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; + +import org.eclipse.jgit.errors.PackProtocolException; +import org.eclipse.jgit.transport.ServiceMayNotContinueException; +import org.eclipse.jgit.transport.UploadPack; +import org.eclipse.jgit.transport.resolver.ServiceNotEnabledException; + +/** + * Handle git-upload-pack errors. + * + * <p> + * This is an entry point for customizing an error handler for git-upload-pack. + * Right before calling {@link UploadPack#uploadWithExceptionPropagation}, JGit + * will call this handler if specified through {@link GitFilter}. The + * implementation of this handler is responsible for calling + * {@link UploadPackRunnable} and handling exceptions for clients. + * + * <p> + * If a custom handler is not specified, JGit will use the default error + * handler. + * + * @since 5.6 + */ +public interface UploadPackErrorHandler { + /** + * Maps a thrown git related Exception to an appropriate HTTP status code. + * + * @param error + * The thrown Exception. + * @return the HTTP status code as an int + * @since 6.1.1 + */ + public static int statusCodeForThrowable(Throwable error) { + if (error instanceof ServiceNotEnabledException) { + return SC_FORBIDDEN; + } + if (error instanceof PackProtocolException) { + // Internal git errors are not errors from an HTTP standpoint. + return SC_OK; + } + return SC_INTERNAL_SERVER_ERROR; + } + + /** + * Upload pack + * + * @param req + * The HTTP request + * @param rsp + * The HTTP response + * @param r + * A continuation that handles a git-upload-pack request. + * @throws IOException + * if an IO error occurred + * @since 7.0 + */ + void upload(HttpServletRequest req, HttpServletResponse rsp, + UploadPackRunnable r) throws IOException; + + /** Process a git-upload-pack request. */ + public interface UploadPackRunnable { + /** + * See {@link UploadPack#uploadWithExceptionPropagation}. + * + * @throws ServiceMayNotContinueException + * transport service cannot continue + * @throws IOException + * if an IO error occurred + */ + void upload() throws ServiceMayNotContinueException, IOException; + } +} 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 44d4b1bcfa..3665d35c0e 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 @@ -1,55 +1,18 @@ /* - * Copyright (C) 2009-2010, Google Inc. - * and other copyright owners as documented in the project's IP log. + * Copyright (C) 2009-2010, Google Inc. and others * - * This program and the accompanying materials are made available - * under the terms of the Eclipse Distribution License v1.0 which - * accompanies this distribution, is reproduced below, and is - * available at http://www.eclipse.org/org/documents/edl-v10.php + * This program and the accompanying materials are made available under the + * terms of the Eclipse Distribution License v. 1.0 which is available at + * https://www.eclipse.org/org/documents/edl-v10.php. * - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or - * without modification, are permitted provided that the following - * conditions are met: - * - * - Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * - Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following - * disclaimer in the documentation and/or other materials provided - * with the distribution. - * - * - Neither the name of the Eclipse Foundation, Inc. nor the - * names of its contributors may be used to endorse or promote - * products derived from this software without specific prior - * written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND - * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES - * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR - * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT - * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; - * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER - * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, - * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF - * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * SPDX-License-Identifier: BSD-3-Clause */ 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; -import static javax.servlet.http.HttpServletResponse.SC_UNSUPPORTED_MEDIA_TYPE; -import static org.eclipse.jgit.http.server.ClientVersionUtil.hasChunkedEncodingRequestBug; -import static org.eclipse.jgit.http.server.ClientVersionUtil.parseVersion; +import static jakarta.servlet.http.HttpServletResponse.SC_FORBIDDEN; +import static jakarta.servlet.http.HttpServletResponse.SC_UNAUTHORIZED; +import static jakarta.servlet.http.HttpServletResponse.SC_UNSUPPORTED_MEDIA_TYPE; import static org.eclipse.jgit.http.server.GitSmartHttpTools.UPLOAD_PACK; import static org.eclipse.jgit.http.server.GitSmartHttpTools.UPLOAD_PACK_REQUEST_TYPE; import static org.eclipse.jgit.http.server.GitSmartHttpTools.UPLOAD_PACK_RESULT_TYPE; @@ -58,24 +21,29 @@ import static org.eclipse.jgit.http.server.ServletUtils.ATTRIBUTE_HANDLER; import static org.eclipse.jgit.http.server.ServletUtils.consumeRequestBody; import static org.eclipse.jgit.http.server.ServletUtils.getInputStream; import static org.eclipse.jgit.http.server.ServletUtils.getRepository; +import static org.eclipse.jgit.http.server.UploadPackErrorHandler.statusCodeForThrowable; import static org.eclipse.jgit.util.HttpSupport.HDR_USER_AGENT; import java.io.IOException; import java.text.MessageFormat; import java.util.List; -import javax.servlet.Filter; -import javax.servlet.FilterChain; -import javax.servlet.FilterConfig; -import javax.servlet.ServletException; -import javax.servlet.ServletRequest; -import javax.servlet.ServletResponse; -import javax.servlet.http.HttpServlet; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; - +import jakarta.servlet.Filter; +import jakarta.servlet.FilterChain; +import jakarta.servlet.FilterConfig; +import jakarta.servlet.ServletException; +import jakarta.servlet.ServletRequest; +import jakarta.servlet.ServletResponse; +import jakarta.servlet.http.HttpServlet; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; + +import org.eclipse.jgit.annotations.Nullable; +import org.eclipse.jgit.errors.PackProtocolException; +import org.eclipse.jgit.http.server.UploadPackErrorHandler.UploadPackRunnable; import org.eclipse.jgit.lib.Repository; import org.eclipse.jgit.transport.InternalHttpServerGlue; +import org.eclipse.jgit.transport.PacketLineOut; import org.eclipse.jgit.transport.RefAdvertiser.PacketLineOutRefAdvertiser; import org.eclipse.jgit.transport.ServiceMayNotContinueException; import org.eclipse.jgit.transport.UploadPack; @@ -117,6 +85,25 @@ class UploadPackServlet extends HttpServlet { up.setBiDirectionalPipe(false); up.sendAdvertisedRefs(pck); } finally { + // TODO(jonathantanmy): Move responsibility for closing the + // RevWalk to UploadPack, either by making it AutoCloseable + // or by making sendAdvertisedRefs clean up after itself. + up.getRevWalk().close(); + } + } + + @Override + protected void respond(HttpServletRequest req, + PacketLineOut pckOut, String serviceName) throws IOException, + ServiceNotEnabledException, ServiceNotAuthorizedException { + UploadPack up = (UploadPack) req.getAttribute(ATTRIBUTE_HANDLER); + try { + up.setBiDirectionalPipe(false); + up.sendAdvertisedRefs(new PacketLineOutRefAdvertiser(pckOut), serviceName); + } finally { + // TODO(jonathantanmy): Move responsibility for closing the + // RevWalk to UploadPack, either by making it AutoCloseable + // or by making sendAdvertisedRefs clean up after itself. up.getRevWalk().close(); } } @@ -129,6 +116,7 @@ class UploadPackServlet extends HttpServlet { this.uploadPackFactory = uploadPackFactory; } + @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { HttpServletRequest req = (HttpServletRequest) request; @@ -137,11 +125,10 @@ class UploadPackServlet extends HttpServlet { try { rp = uploadPackFactory.create(req, getRepository(req)); } catch (ServiceNotAuthorizedException e) { - rsp.sendError(SC_UNAUTHORIZED); + rsp.sendError(SC_UNAUTHORIZED, e.getMessage()); return; - } catch (ServiceNotEnabledException e) { - sendError(req, rsp, SC_FORBIDDEN); + sendError(req, rsp, SC_FORBIDDEN, e.getMessage()); return; } @@ -153,68 +140,91 @@ class UploadPackServlet extends HttpServlet { } } + @Override public void init(FilterConfig filterConfig) throws ServletException { // Nothing. } + @Override public void destroy() { // Nothing. } } + private final UploadPackErrorHandler handler; + + UploadPackServlet(@Nullable UploadPackErrorHandler handler) { + this.handler = handler != null ? handler + : this::defaultUploadPackHandler; + } + @Override - public void doPost(final HttpServletRequest req, - final HttpServletResponse rsp) throws IOException { + public void doPost(HttpServletRequest req, HttpServletResponse rsp) + throws IOException { if (!UPLOAD_PACK_REQUEST_TYPE.equals(req.getContentType())) { rsp.sendError(SC_UNSUPPORTED_MEDIA_TYPE); return; } - int[] version = parseVersion(req.getHeader(HDR_USER_AGENT)); - if (hasChunkedEncodingRequestBug(version, req)) { - GitSmartHttpTools.sendError(req, rsp, SC_BAD_REQUEST, "\n\n" - + HttpServerText.get().clientHas175ChunkedEncodingBug); - return; - } + UploadPackRunnable r = () -> { + upload(req, rsp); + }; + + handler.upload(req, rsp, r); + } + private void upload(HttpServletRequest req, HttpServletResponse rsp) + throws IOException, ServiceMayNotContinueException { + // to be explicitly closed by caller + @SuppressWarnings("resource") SmartOutputStream out = new SmartOutputStream(req, rsp, false) { @Override public void flush() throws IOException { doFlush(); } }; - - UploadPack up = (UploadPack) req.getAttribute(ATTRIBUTE_HANDLER); - try { + Repository repo = null; + try (UploadPack up = (UploadPack) req.getAttribute(ATTRIBUTE_HANDLER)) { up.setBiDirectionalPipe(false); rsp.setContentType(UPLOAD_PACK_RESULT_TYPE); - - up.upload(getInputStream(req), out, null); + repo = up.getRepository(); + up.uploadWithExceptionPropagation(getInputStream(req), out, null); out.close(); - } catch (ServiceMayNotContinueException e) { if (e.isOutput()) { consumeRequestBody(req); out.close(); - } else if (!rsp.isCommitted()) { - rsp.reset(); - sendError(req, rsp, SC_FORBIDDEN, e.getMessage()); } - return; - + throw e; } catch (UploadPackInternalServerErrorException e) { // Special case exception, error message was sent to client. - log(up.getRepository(), e.getCause()); + log(repo, e.getCause()); consumeRequestBody(req); out.close(); + } + } + private void defaultUploadPackHandler(HttpServletRequest req, + HttpServletResponse rsp, UploadPackRunnable r) throws IOException { + try { + r.upload(); + } catch (ServiceMayNotContinueException e) { + if (!e.isOutput() && !rsp.isCommitted()) { + rsp.reset(); + sendError(req, rsp, e.getStatusCode(), e.getMessage()); + } } catch (Throwable e) { + UploadPack up = (UploadPack) req.getAttribute(ATTRIBUTE_HANDLER); log(up.getRepository(), e); if (!rsp.isCommitted()) { rsp.reset(); - sendError(req, rsp, SC_INTERNAL_SERVER_ERROR); + String msg = null; + if (e instanceof PackProtocolException + || e instanceof ServiceNotEnabledException) { + msg = e.getMessage(); + } + sendError(req, rsp, statusCodeForThrowable(e), msg); } - return; } } diff --git a/org.eclipse.jgit.http.server/src/org/eclipse/jgit/http/server/glue/ErrorServlet.java b/org.eclipse.jgit.http.server/src/org/eclipse/jgit/http/server/glue/ErrorServlet.java index c0a9e0e608..1852f6c564 100644 --- a/org.eclipse.jgit.http.server/src/org/eclipse/jgit/http/server/glue/ErrorServlet.java +++ b/org.eclipse.jgit.http.server/src/org/eclipse/jgit/http/server/glue/ErrorServlet.java @@ -1,56 +1,25 @@ /* - * Copyright (C) 2010, Google Inc. - * and other copyright owners as documented in the project's IP log. + * Copyright (C) 2010, Google Inc. and others * - * This program and the accompanying materials are made available - * under the terms of the Eclipse Distribution License v1.0 which - * accompanies this distribution, is reproduced below, and is - * available at http://www.eclipse.org/org/documents/edl-v10.php + * This program and the accompanying materials are made available under the + * terms of the Eclipse Distribution License v. 1.0 which is available at + * https://www.eclipse.org/org/documents/edl-v10.php. * - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or - * without modification, are permitted provided that the following - * conditions are met: - * - * - Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * - Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following - * disclaimer in the documentation and/or other materials provided - * with the distribution. - * - * - Neither the name of the Eclipse Foundation, Inc. nor the - * names of its contributors may be used to endorse or promote - * products derived from this software without specific prior - * written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND - * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES - * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR - * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT - * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; - * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER - * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, - * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF - * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * SPDX-License-Identifier: BSD-3-Clause */ package org.eclipse.jgit.http.server.glue; import java.io.IOException; -import javax.servlet.ServletException; -import javax.servlet.http.HttpServlet; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; +import jakarta.servlet.ServletException; +import jakarta.servlet.http.HttpServlet; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; -/** Sends a fixed status code to the client. */ +/** + * Send a fixed status code to the client. + */ public class ErrorServlet extends HttpServlet { private static final long serialVersionUID = 1L; @@ -62,7 +31,7 @@ public class ErrorServlet extends HttpServlet { * @param status * the HTTP status code to always send. */ - public ErrorServlet(final int status) { + public ErrorServlet(int status) { this.status = status; } diff --git a/org.eclipse.jgit.http.server/src/org/eclipse/jgit/http/server/glue/MetaFilter.java b/org.eclipse.jgit.http.server/src/org/eclipse/jgit/http/server/glue/MetaFilter.java index 2187cfaa42..98111050f3 100644 --- a/org.eclipse.jgit.http.server/src/org/eclipse/jgit/http/server/glue/MetaFilter.java +++ b/org.eclipse.jgit.http.server/src/org/eclipse/jgit/http/server/glue/MetaFilter.java @@ -1,44 +1,11 @@ /* - * Copyright (C) 2009-2010, Google Inc. - * and other copyright owners as documented in the project's IP log. + * Copyright (C) 2009-2010, Google Inc. and others * - * This program and the accompanying materials are made available - * under the terms of the Eclipse Distribution License v1.0 which - * accompanies this distribution, is reproduced below, and is - * available at http://www.eclipse.org/org/documents/edl-v10.php + * This program and the accompanying materials are made available under the + * terms of the Eclipse Distribution License v. 1.0 which is available at + * https://www.eclipse.org/org/documents/edl-v10.php. * - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or - * without modification, are permitted provided that the following - * conditions are met: - * - * - Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * - Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following - * disclaimer in the documentation and/or other materials provided - * with the distribution. - * - * - Neither the name of the Eclipse Foundation, Inc. nor the - * names of its contributors may be used to endorse or promote - * products derived from this software without specific prior - * written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND - * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES - * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR - * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT - * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; - * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER - * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, - * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF - * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * SPDX-License-Identifier: BSD-3-Clause */ package org.eclipse.jgit.http.server.glue; @@ -50,19 +17,18 @@ import java.util.ArrayList; import java.util.IdentityHashMap; import java.util.Iterator; import java.util.List; -import java.util.Map; import java.util.Set; import java.util.regex.Pattern; -import javax.servlet.Filter; -import javax.servlet.FilterChain; -import javax.servlet.FilterConfig; -import javax.servlet.ServletContext; -import javax.servlet.ServletException; -import javax.servlet.ServletRequest; -import javax.servlet.ServletResponse; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; +import jakarta.servlet.Filter; +import jakarta.servlet.FilterChain; +import jakarta.servlet.FilterConfig; +import jakarta.servlet.ServletContext; +import jakarta.servlet.ServletException; +import jakarta.servlet.ServletRequest; +import jakarta.servlet.ServletResponse; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; import org.eclipse.jgit.http.server.HttpServerText; @@ -87,9 +53,11 @@ public class MetaFilter implements Filter { private volatile UrlPipeline[] pipelines; - /** Empty filter with no bindings. */ + /** + * Empty filter with no bindings. + */ public MetaFilter() { - this.bindings = new ArrayList<ServletBinderImpl>(); + this.bindings = new ArrayList<>(); } /** @@ -128,10 +96,12 @@ public class MetaFilter implements Filter { return register(new RegexPipeline.Binder(pattern)); } + @Override public void init(FilterConfig filterConfig) throws ServletException { servletContext = filterConfig.getServletContext(); } + @Override public void destroy() { if (pipelines != null) { Set<Object> destroyed = newIdentitySet(); @@ -142,8 +112,9 @@ public class MetaFilter implements Filter { } private static Set<Object> newIdentitySet() { - final Map<Object, Object> m = new IdentityHashMap<Object, Object>(); - return new AbstractSet<Object>() { + final IdentityHashMap<Object, Object> m = new IdentityHashMap<>(); + return new AbstractSet<>() { + @Override public boolean add(Object o) { return m.put(o, o) == null; @@ -151,7 +122,7 @@ public class MetaFilter implements Filter { @Override public boolean contains(Object o) { - return m.keySet().contains(o); + return m.containsKey(o); } @Override @@ -166,6 +137,7 @@ public class MetaFilter implements Filter { }; } + @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { HttpServletRequest req = (HttpServletRequest) request; diff --git a/org.eclipse.jgit.http.server/src/org/eclipse/jgit/http/server/glue/MetaServlet.java b/org.eclipse.jgit.http.server/src/org/eclipse/jgit/http/server/glue/MetaServlet.java index 05060658d6..f577c6950f 100644 --- a/org.eclipse.jgit.http.server/src/org/eclipse/jgit/http/server/glue/MetaServlet.java +++ b/org.eclipse.jgit.http.server/src/org/eclipse/jgit/http/server/glue/MetaServlet.java @@ -1,61 +1,27 @@ /* - * Copyright (C) 2009-2010, Google Inc. - * and other copyright owners as documented in the project's IP log. + * Copyright (C) 2009-2010, Google Inc. and others * - * This program and the accompanying materials are made available - * under the terms of the Eclipse Distribution License v1.0 which - * accompanies this distribution, is reproduced below, and is - * available at http://www.eclipse.org/org/documents/edl-v10.php + * This program and the accompanying materials are made available under the + * terms of the Eclipse Distribution License v. 1.0 which is available at + * https://www.eclipse.org/org/documents/edl-v10.php. * - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or - * without modification, are permitted provided that the following - * conditions are met: - * - * - Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * - Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following - * disclaimer in the documentation and/or other materials provided - * with the distribution. - * - * - Neither the name of the Eclipse Foundation, Inc. nor the - * names of its contributors may be used to endorse or promote - * products derived from this software without specific prior - * written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND - * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES - * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR - * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT - * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; - * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER - * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, - * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF - * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * SPDX-License-Identifier: BSD-3-Clause */ package org.eclipse.jgit.http.server.glue; -import static javax.servlet.http.HttpServletResponse.SC_NOT_FOUND; +import static jakarta.servlet.http.HttpServletResponse.SC_NOT_FOUND; import java.io.IOException; -import javax.servlet.FilterChain; -import javax.servlet.ServletConfig; -import javax.servlet.ServletContext; -import javax.servlet.ServletException; -import javax.servlet.ServletRequest; -import javax.servlet.ServletResponse; -import javax.servlet.http.HttpServlet; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; +import jakarta.servlet.ServletConfig; +import jakarta.servlet.ServletContext; +import jakarta.servlet.ServletException; +import jakarta.servlet.ServletRequest; +import jakarta.servlet.ServletResponse; +import jakarta.servlet.http.HttpServlet; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; /** * Generic container servlet to manage routing to different pipelines. @@ -74,7 +40,9 @@ public class MetaServlet extends HttpServlet { private final MetaFilter filter; - /** Empty servlet with no bindings. */ + /** + * Empty servlet with no bindings. + */ public MetaServlet() { this(new MetaFilter()); } @@ -89,7 +57,11 @@ public class MetaServlet extends HttpServlet { filter = delegateFilter; } - /** @return filter this servlet delegates all routing logic to. */ + /** + * Get delegate filter + * + * @return filter this servlet delegates all routing logic to. + */ protected MetaFilter getDelegateFilter() { return filter; } @@ -123,6 +95,7 @@ public class MetaServlet extends HttpServlet { filter.init(new NoParameterFilterConfig(name, ctx)); } + @Override public void destroy() { filter.destroy(); } @@ -130,13 +103,10 @@ public class MetaServlet extends HttpServlet { @Override protected void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { - filter.doFilter(req, res, new FilterChain() { - public void doFilter(ServletRequest request, - ServletResponse response) throws IOException, - ServletException { - ((HttpServletResponse) response).sendError(SC_NOT_FOUND); - } - }); + filter.doFilter(req, res, + (ServletRequest request, ServletResponse response) -> { + ((HttpServletResponse) response).sendError(SC_NOT_FOUND); + }); } /** diff --git a/org.eclipse.jgit.http.server/src/org/eclipse/jgit/http/server/glue/NoParameterFilterConfig.java b/org.eclipse.jgit.http.server/src/org/eclipse/jgit/http/server/glue/NoParameterFilterConfig.java index 8dfcc4d149..82cf7a8857 100644 --- a/org.eclipse.jgit.http.server/src/org/eclipse/jgit/http/server/glue/NoParameterFilterConfig.java +++ b/org.eclipse.jgit.http.server/src/org/eclipse/jgit/http/server/glue/NoParameterFilterConfig.java @@ -1,44 +1,11 @@ /* - * Copyright (C) 2009-2010, Google Inc. - * and other copyright owners as documented in the project's IP log. + * Copyright (C) 2009-2010, Google Inc. and others * - * This program and the accompanying materials are made available - * under the terms of the Eclipse Distribution License v1.0 which - * accompanies this distribution, is reproduced below, and is - * available at http://www.eclipse.org/org/documents/edl-v10.php + * This program and the accompanying materials are made available under the + * terms of the Eclipse Distribution License v. 1.0 which is available at + * https://www.eclipse.org/org/documents/edl-v10.php. * - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or - * without modification, are permitted provided that the following - * conditions are met: - * - * - Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * - Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following - * disclaimer in the documentation and/or other materials provided - * with the distribution. - * - * - Neither the name of the Eclipse Foundation, Inc. nor the - * names of its contributors may be used to endorse or promote - * products derived from this software without specific prior - * written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND - * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES - * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR - * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT - * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; - * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER - * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, - * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF - * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * SPDX-License-Identifier: BSD-3-Clause */ package org.eclipse.jgit.http.server.glue; @@ -46,8 +13,8 @@ package org.eclipse.jgit.http.server.glue; import java.util.Enumeration; import java.util.NoSuchElementException; -import javax.servlet.FilterConfig; -import javax.servlet.ServletContext; +import jakarta.servlet.FilterConfig; +import jakarta.servlet.ServletContext; final class NoParameterFilterConfig implements FilterConfig { private final String filterName; @@ -59,26 +26,33 @@ final class NoParameterFilterConfig implements FilterConfig { this.context = context; } + @Override public String getInitParameter(String name) { return null; } + @Override public Enumeration<String> getInitParameterNames() { - return new Enumeration<String>() { + return new Enumeration<>() { + + @Override public boolean hasMoreElements() { return false; } + @Override public String nextElement() { throw new NoSuchElementException(); } }; } + @Override public ServletContext getServletContext() { return context; } + @Override public String getFilterName() { return filterName; } diff --git a/org.eclipse.jgit.http.server/src/org/eclipse/jgit/http/server/glue/RegexGroupFilter.java b/org.eclipse.jgit.http.server/src/org/eclipse/jgit/http/server/glue/RegexGroupFilter.java index 2d466e2bc8..1c540714f6 100644 --- a/org.eclipse.jgit.http.server/src/org/eclipse/jgit/http/server/glue/RegexGroupFilter.java +++ b/org.eclipse.jgit.http.server/src/org/eclipse/jgit/http/server/glue/RegexGroupFilter.java @@ -1,44 +1,11 @@ /* - * Copyright (C) 2009-2010, Google Inc. - * and other copyright owners as documented in the project's IP log. + * Copyright (C) 2009-2010, Google Inc. and others * - * This program and the accompanying materials are made available - * under the terms of the Eclipse Distribution License v1.0 which - * accompanies this distribution, is reproduced below, and is - * available at http://www.eclipse.org/org/documents/edl-v10.php + * This program and the accompanying materials are made available under the + * terms of the Eclipse Distribution License v. 1.0 which is available at + * https://www.eclipse.org/org/documents/edl-v10.php. * - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or - * without modification, are permitted provided that the following - * conditions are met: - * - * - Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * - Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following - * disclaimer in the documentation and/or other materials provided - * with the distribution. - * - * - Neither the name of the Eclipse Foundation, Inc. nor the - * names of its contributors may be used to endorse or promote - * products derived from this software without specific prior - * written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND - * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES - * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR - * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT - * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; - * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER - * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, - * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF - * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * SPDX-License-Identifier: BSD-3-Clause */ package org.eclipse.jgit.http.server.glue; @@ -46,14 +13,12 @@ package org.eclipse.jgit.http.server.glue; import java.io.IOException; import java.text.MessageFormat; -import javax.servlet.Filter; -import javax.servlet.FilterChain; -import javax.servlet.FilterConfig; -import javax.servlet.ServletException; -import javax.servlet.ServletRequest; -import javax.servlet.ServletResponse; - -import static java.lang.Integer.valueOf; +import jakarta.servlet.Filter; +import jakarta.servlet.FilterChain; +import jakarta.servlet.FilterConfig; +import jakarta.servlet.ServletException; +import jakarta.servlet.ServletRequest; +import jakarta.servlet.ServletResponse; import org.eclipse.jgit.http.server.HttpServerText; @@ -61,32 +26,39 @@ import org.eclipse.jgit.http.server.HttpServerText; * Switch servlet path and path info to use another regex match group. * <p> * This filter is meant to be installed in the middle of a pipeline created by - * {@link MetaServlet#serveRegex(String)}. The passed request's servlet path is - * updated to be all text up to the start of the designated capture group, and - * the path info is changed to the contents of the capture group. - **/ + * {@link org.eclipse.jgit.http.server.glue.MetaServlet#serveRegex(String)}. The + * passed request's servlet path is updated to be all text up to the start of + * the designated capture group, and the path info is changed to the contents of + * the capture group. + */ public class RegexGroupFilter implements Filter { private final int groupIdx; /** + * Constructor for RegexGroupFilter + * * @param groupIdx * capture group number, 1 through the number of groups. */ - public RegexGroupFilter(final int groupIdx) { + public RegexGroupFilter(int groupIdx) { if (groupIdx < 1) throw new IllegalArgumentException(MessageFormat.format( - HttpServerText.get().invalidIndex, valueOf(groupIdx))); + HttpServerText.get().invalidIndex, + Integer.valueOf(groupIdx))); this.groupIdx = groupIdx - 1; } + @Override public void init(FilterConfig config) throws ServletException { // Do nothing. } + @Override public void destroy() { // Do nothing. } + @Override public void doFilter(final ServletRequest request, final ServletResponse rsp, final FilterChain chain) throws IOException, ServletException { @@ -96,10 +68,10 @@ public class RegexGroupFilter implements Filter { else throw new ServletException(MessageFormat.format( HttpServerText.get().invalidRegexGroup, - valueOf(groupIdx + 1))); + Integer.valueOf(groupIdx + 1))); } - private static WrappedRequest[] groupsFor(final ServletRequest r) { + private static WrappedRequest[] groupsFor(ServletRequest r) { return (WrappedRequest[]) r.getAttribute(MetaFilter.REGEX_GROUPS); } } diff --git a/org.eclipse.jgit.http.server/src/org/eclipse/jgit/http/server/glue/RegexPipeline.java b/org.eclipse.jgit.http.server/src/org/eclipse/jgit/http/server/glue/RegexPipeline.java index 2ef71368d0..7b856c742c 100644 --- a/org.eclipse.jgit.http.server/src/org/eclipse/jgit/http/server/glue/RegexPipeline.java +++ b/org.eclipse.jgit.http.server/src/org/eclipse/jgit/http/server/glue/RegexPipeline.java @@ -1,60 +1,27 @@ /* - * Copyright (C) 2009-2010, Google Inc. - * and other copyright owners as documented in the project's IP log. + * Copyright (C) 2009-2010, Google Inc. and others * - * This program and the accompanying materials are made available - * under the terms of the Eclipse Distribution License v1.0 which - * accompanies this distribution, is reproduced below, and is - * available at http://www.eclipse.org/org/documents/edl-v10.php + * This program and the accompanying materials are made available under the + * terms of the Eclipse Distribution License v. 1.0 which is available at + * https://www.eclipse.org/org/documents/edl-v10.php. * - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or - * without modification, are permitted provided that the following - * conditions are met: - * - * - Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * - Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following - * disclaimer in the documentation and/or other materials provided - * with the distribution. - * - * - Neither the name of the Eclipse Foundation, Inc. nor the - * names of its contributors may be used to endorse or promote - * products derived from this software without specific prior - * written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND - * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES - * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR - * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT - * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; - * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER - * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, - * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF - * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * SPDX-License-Identifier: BSD-3-Clause */ package org.eclipse.jgit.http.server.glue; -import static javax.servlet.http.HttpServletResponse.SC_NOT_FOUND; +import static jakarta.servlet.http.HttpServletResponse.SC_NOT_FOUND; import static org.eclipse.jgit.http.server.glue.MetaFilter.REGEX_GROUPS; import java.io.IOException; import java.util.regex.Matcher; import java.util.regex.Pattern; -import javax.servlet.Filter; -import javax.servlet.ServletException; -import javax.servlet.http.HttpServlet; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; +import jakarta.servlet.Filter; +import jakarta.servlet.ServletException; +import jakarta.servlet.http.HttpServlet; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; /** * Selects requests by matching the URI against a regular expression. @@ -87,14 +54,15 @@ class RegexPipeline extends UrlPipeline { static class Binder extends ServletBinderImpl { private final Pattern pattern; - Binder(final String p) { + Binder(String p) { pattern = Pattern.compile(p); } - Binder(final Pattern p) { + Binder(Pattern p) { pattern = p; } + @Override UrlPipeline create() { return new RegexPipeline(pattern, getFilters(), getServlet()); } @@ -108,7 +76,8 @@ class RegexPipeline extends UrlPipeline { this.pattern = pattern; } - boolean match(final HttpServletRequest req) { + @Override + boolean match(HttpServletRequest req) { final String pathInfo = req.getPathInfo(); return pathInfo != null && pattern.matcher(pathInfo).matches(); } @@ -136,7 +105,7 @@ class RegexPipeline extends UrlPipeline { // build a request for them so RegexGroupFilter can pick // a different capture group later. Continue using the // first capture group as the path info. - WrappedRequest groups[] = new WrappedRequest[cur.groupCount()]; + WrappedRequest[] groups = new WrappedRequest[cur.groupCount()]; for (int groupId = 1; groupId <= cur.groupCount(); groupId++) { final int s = cur.start(groupId); final String path, info; diff --git a/org.eclipse.jgit.http.server/src/org/eclipse/jgit/http/server/glue/ServletBinder.java b/org.eclipse.jgit.http.server/src/org/eclipse/jgit/http/server/glue/ServletBinder.java index 9c3ed50d08..06993eae97 100644 --- a/org.eclipse.jgit.http.server/src/org/eclipse/jgit/http/server/glue/ServletBinder.java +++ b/org.eclipse.jgit.http.server/src/org/eclipse/jgit/http/server/glue/ServletBinder.java @@ -1,63 +1,38 @@ /* - * Copyright (C) 2009-2010, Google Inc. - * and other copyright owners as documented in the project's IP log. + * Copyright (C) 2009-2010, Google Inc. and others * - * This program and the accompanying materials are made available - * under the terms of the Eclipse Distribution License v1.0 which - * accompanies this distribution, is reproduced below, and is - * available at http://www.eclipse.org/org/documents/edl-v10.php + * This program and the accompanying materials are made available under the + * terms of the Eclipse Distribution License v. 1.0 which is available at + * https://www.eclipse.org/org/documents/edl-v10.php. * - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or - * without modification, are permitted provided that the following - * conditions are met: - * - * - Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * - Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following - * disclaimer in the documentation and/or other materials provided - * with the distribution. - * - * - Neither the name of the Eclipse Foundation, Inc. nor the - * names of its contributors may be used to endorse or promote - * products derived from this software without specific prior - * written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND - * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES - * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR - * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT - * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; - * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER - * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, - * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF - * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * SPDX-License-Identifier: BSD-3-Clause */ package org.eclipse.jgit.http.server.glue; -import javax.servlet.Filter; -import javax.servlet.http.HttpServlet; +import jakarta.servlet.Filter; +import jakarta.servlet.http.HttpServlet; -/** Binds a servlet to a URL. */ +/** + * Binds a servlet to a URL. + */ public interface ServletBinder { /** + * Set the filter to trigger while processing the path. + * * @param filter * the filter to trigger while processing the path. * @return {@code this}. + * @since 7.0 */ - public ServletBinder through(Filter filter); + ServletBinder through(Filter filter); /** + * Set the servlet to execute on this path + * * @param servlet * the servlet to execute on this path. + * @since 7.0 */ - public void with(HttpServlet servlet); -}
\ No newline at end of file + void with(HttpServlet servlet); +} diff --git a/org.eclipse.jgit.http.server/src/org/eclipse/jgit/http/server/glue/ServletBinderImpl.java b/org.eclipse.jgit.http.server/src/org/eclipse/jgit/http/server/glue/ServletBinderImpl.java index e96fc59dc3..814a9003a6 100644 --- a/org.eclipse.jgit.http.server/src/org/eclipse/jgit/http/server/glue/ServletBinderImpl.java +++ b/org.eclipse.jgit.http.server/src/org/eclipse/jgit/http/server/glue/ServletBinderImpl.java @@ -1,44 +1,11 @@ /* - * Copyright (C) 2009-2010, Google Inc. - * and other copyright owners as documented in the project's IP log. + * Copyright (C) 2009-2010, Google Inc. and others * - * This program and the accompanying materials are made available - * under the terms of the Eclipse Distribution License v1.0 which - * accompanies this distribution, is reproduced below, and is - * available at http://www.eclipse.org/org/documents/edl-v10.php + * This program and the accompanying materials are made available under the + * terms of the Eclipse Distribution License v. 1.0 which is available at + * https://www.eclipse.org/org/documents/edl-v10.php. * - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or - * without modification, are permitted provided that the following - * conditions are met: - * - * - Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * - Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following - * disclaimer in the documentation and/or other materials provided - * with the distribution. - * - * - Neither the name of the Eclipse Foundation, Inc. nor the - * names of its contributors may be used to endorse or promote - * products derived from this software without specific prior - * written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND - * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES - * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR - * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT - * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; - * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER - * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, - * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF - * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * SPDX-License-Identifier: BSD-3-Clause */ package org.eclipse.jgit.http.server.glue; @@ -46,9 +13,9 @@ package org.eclipse.jgit.http.server.glue; import java.util.ArrayList; import java.util.List; -import javax.servlet.Filter; -import javax.servlet.http.HttpServlet; -import javax.servlet.http.HttpServletResponse; +import jakarta.servlet.Filter; +import jakarta.servlet.http.HttpServlet; +import jakarta.servlet.http.HttpServletResponse; import org.eclipse.jgit.http.server.HttpServerText; @@ -58,9 +25,10 @@ abstract class ServletBinderImpl implements ServletBinder { private HttpServlet httpServlet; ServletBinderImpl() { - this.filters = new ArrayList<Filter>(); + this.filters = new ArrayList<>(); } + @Override public ServletBinder through(Filter filter) { if (filter == null) throw new NullPointerException(HttpServerText.get().filterMustNotBeNull); @@ -68,6 +36,7 @@ abstract class ServletBinderImpl implements ServletBinder { return this; } + @Override public void with(HttpServlet servlet) { if (servlet == null) throw new NullPointerException(HttpServerText.get().servletMustNotBeNull); @@ -76,19 +45,31 @@ abstract class ServletBinderImpl implements ServletBinder { httpServlet = servlet; } - /** @return the configured servlet, or singleton returning 404 if none. */ + /** + * Get the servlet + * + * @return the configured servlet, or singleton returning 404 if none. + */ protected HttpServlet getServlet() { - if (httpServlet != null) + if (httpServlet != null) { return httpServlet; - else - return new ErrorServlet(HttpServletResponse.SC_NOT_FOUND); + } + return new ErrorServlet(HttpServletResponse.SC_NOT_FOUND); } - /** @return the configured filters; zero-length array if none. */ + /** + * Get filters + * + * @return the configured filters; zero-length array if none. + */ protected Filter[] getFilters() { - return filters.toArray(new Filter[filters.size()]); + return filters.toArray(new Filter[0]); } - /** @return the pipeline that matches and executes this chain. */ + /** + * Create UrlPipeline + * + * @return the pipeline that matches and executes this chain. + */ abstract UrlPipeline create(); } diff --git a/org.eclipse.jgit.http.server/src/org/eclipse/jgit/http/server/glue/SuffixPipeline.java b/org.eclipse.jgit.http.server/src/org/eclipse/jgit/http/server/glue/SuffixPipeline.java index b942016259..f2db9c7b3a 100644 --- a/org.eclipse.jgit.http.server/src/org/eclipse/jgit/http/server/glue/SuffixPipeline.java +++ b/org.eclipse.jgit.http.server/src/org/eclipse/jgit/http/server/glue/SuffixPipeline.java @@ -1,55 +1,22 @@ /* - * Copyright (C) 2009-2010, Google Inc. - * and other copyright owners as documented in the project's IP log. + * Copyright (C) 2009-2010, Google Inc. and others * - * This program and the accompanying materials are made available - * under the terms of the Eclipse Distribution License v1.0 which - * accompanies this distribution, is reproduced below, and is - * available at http://www.eclipse.org/org/documents/edl-v10.php + * This program and the accompanying materials are made available under the + * terms of the Eclipse Distribution License v. 1.0 which is available at + * https://www.eclipse.org/org/documents/edl-v10.php. * - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or - * without modification, are permitted provided that the following - * conditions are met: - * - * - Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * - Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following - * disclaimer in the documentation and/or other materials provided - * with the distribution. - * - * - Neither the name of the Eclipse Foundation, Inc. nor the - * names of its contributors may be used to endorse or promote - * products derived from this software without specific prior - * written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND - * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES - * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR - * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT - * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; - * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER - * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, - * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF - * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * SPDX-License-Identifier: BSD-3-Clause */ package org.eclipse.jgit.http.server.glue; import java.io.IOException; -import javax.servlet.Filter; -import javax.servlet.ServletException; -import javax.servlet.http.HttpServlet; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; +import jakarta.servlet.Filter; +import jakarta.servlet.ServletException; +import jakarta.servlet.http.HttpServlet; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; /** * Selects requests by matching the suffix of the URI. @@ -67,10 +34,11 @@ class SuffixPipeline extends UrlPipeline { static class Binder extends ServletBinderImpl { private final String suffix; - Binder(final String suffix) { + Binder(String suffix) { this.suffix = suffix; } + @Override UrlPipeline create() { return new SuffixPipeline(suffix, getFilters(), getServlet()); } @@ -87,7 +55,8 @@ class SuffixPipeline extends UrlPipeline { this.suffixLen = suffix.length(); } - boolean match(final HttpServletRequest req) { + @Override + boolean match(HttpServletRequest req) { final String pathInfo = req.getPathInfo(); return pathInfo != null && pathInfo.endsWith(suffix); } diff --git a/org.eclipse.jgit.http.server/src/org/eclipse/jgit/http/server/glue/UrlPipeline.java b/org.eclipse.jgit.http.server/src/org/eclipse/jgit/http/server/glue/UrlPipeline.java index 2d0b844072..a27935f828 100644 --- a/org.eclipse.jgit.http.server/src/org/eclipse/jgit/http/server/glue/UrlPipeline.java +++ b/org.eclipse.jgit.http.server/src/org/eclipse/jgit/http/server/glue/UrlPipeline.java @@ -1,44 +1,11 @@ /* - * Copyright (C) 2009-2010, Google Inc. - * and other copyright owners as documented in the project's IP log. + * Copyright (C) 2009-2010, Google Inc. and others * - * This program and the accompanying materials are made available - * under the terms of the Eclipse Distribution License v1.0 which - * accompanies this distribution, is reproduced below, and is - * available at http://www.eclipse.org/org/documents/edl-v10.php + * This program and the accompanying materials are made available under the + * terms of the Eclipse Distribution License v. 1.0 which is available at + * https://www.eclipse.org/org/documents/edl-v10.php. * - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or - * without modification, are permitted provided that the following - * conditions are met: - * - * - Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * - Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following - * disclaimer in the documentation and/or other materials provided - * with the distribution. - * - * - Neither the name of the Eclipse Foundation, Inc. nor the - * names of its contributors may be used to endorse or promote - * products derived from this software without specific prior - * written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND - * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES - * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR - * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT - * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; - * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER - * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, - * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF - * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * SPDX-License-Identifier: BSD-3-Clause */ package org.eclipse.jgit.http.server.glue; @@ -48,16 +15,16 @@ import java.util.Enumeration; import java.util.NoSuchElementException; import java.util.Set; -import javax.servlet.Filter; -import javax.servlet.FilterChain; -import javax.servlet.ServletConfig; -import javax.servlet.ServletContext; -import javax.servlet.ServletException; -import javax.servlet.ServletRequest; -import javax.servlet.ServletResponse; -import javax.servlet.http.HttpServlet; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; +import jakarta.servlet.Filter; +import jakarta.servlet.FilterChain; +import jakarta.servlet.ServletConfig; +import jakarta.servlet.ServletContext; +import jakarta.servlet.ServletException; +import jakarta.servlet.ServletRequest; +import jakarta.servlet.ServletResponse; +import jakarta.servlet.http.HttpServlet; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; /** * Encapsulates the entire serving stack for a single URL. @@ -79,7 +46,7 @@ abstract class UrlPipeline { /** Instance that must generate the response; never null. */ private final HttpServlet servlet; - UrlPipeline(final Filter[] filters, final HttpServlet servlet) { + UrlPipeline(Filter[] filters, HttpServlet servlet) { this.filters = filters; this.servlet = servlet; } @@ -99,7 +66,7 @@ abstract class UrlPipeline { * @throws ServletException * a filter or servlet is unable to initialize. */ - void init(final ServletContext context, final Set<Object> inited) + void init(ServletContext context, Set<Object> inited) throws ServletException { for (Filter ref : filters) initFilter(ref, context, inited); @@ -121,26 +88,33 @@ abstract class UrlPipeline { throws ServletException { if (!inited.contains(ref)) { ref.init(new ServletConfig() { + @Override public String getInitParameter(String name) { return null; } + @Override public Enumeration<String> getInitParameterNames() { - return new Enumeration<String>() { + return new Enumeration<>() { + + @Override public boolean hasMoreElements() { return false; } + @Override public String nextElement() { throw new NoSuchElementException(); } }; } + @Override public ServletContext getServletContext() { return context; } + @Override public String getServletName() { return ref.getClass().getName(); } @@ -159,7 +133,7 @@ abstract class UrlPipeline { * destroyed a second time. Filters and servlets that are first * destroyed by this pipeline will be added to this set. */ - void destroy(final Set<Object> destroyed) { + void destroy(Set<Object> destroyed) { for (Filter ref : filters) destroyFilter(ref, destroyed); destroyServlet(servlet, destroyed); @@ -224,11 +198,12 @@ abstract class UrlPipeline { private int filterIdx; - Chain(final Filter[] filters, final HttpServlet servlet) { + Chain(Filter[] filters, HttpServlet servlet) { this.filters = filters; this.servlet = servlet; } + @Override public void doFilter(ServletRequest req, ServletResponse rsp) throws IOException, ServletException { if (filterIdx < filters.length) diff --git a/org.eclipse.jgit.http.server/src/org/eclipse/jgit/http/server/glue/WrappedRequest.java b/org.eclipse.jgit.http.server/src/org/eclipse/jgit/http/server/glue/WrappedRequest.java index 7f8da7c440..5d41791da3 100644 --- a/org.eclipse.jgit.http.server/src/org/eclipse/jgit/http/server/glue/WrappedRequest.java +++ b/org.eclipse.jgit.http.server/src/org/eclipse/jgit/http/server/glue/WrappedRequest.java @@ -1,52 +1,21 @@ /* - * Copyright (C) 2009-2010, Google Inc. - * and other copyright owners as documented in the project's IP log. + * Copyright (C) 2009-2010, Google Inc. and others * - * This program and the accompanying materials are made available - * under the terms of the Eclipse Distribution License v1.0 which - * accompanies this distribution, is reproduced below, and is - * available at http://www.eclipse.org/org/documents/edl-v10.php + * This program and the accompanying materials are made available under the + * terms of the Eclipse Distribution License v. 1.0 which is available at + * https://www.eclipse.org/org/documents/edl-v10.php. * - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or - * without modification, are permitted provided that the following - * conditions are met: - * - * - Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * - Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following - * disclaimer in the documentation and/or other materials provided - * with the distribution. - * - * - Neither the name of the Eclipse Foundation, Inc. nor the - * names of its contributors may be used to endorse or promote - * products derived from this software without specific prior - * written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND - * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES - * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR - * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT - * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; - * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER - * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, - * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF - * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * SPDX-License-Identifier: BSD-3-Clause */ package org.eclipse.jgit.http.server.glue; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletRequestWrapper; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletRequestWrapper; -/** Overrides the path and path info. */ +/** + * Overrides the path and path info. + */ public class WrappedRequest extends HttpServletRequestWrapper { private final String path; @@ -61,6 +30,7 @@ public class WrappedRequest extends HttpServletRequestWrapper { * new servlet path to report to callers. * @param pathInfo * new path info to report to callers. + * @since 7.0 */ public WrappedRequest(final HttpServletRequest originalRequest, final String path, final String pathInfo) { @@ -72,7 +42,7 @@ public class WrappedRequest extends HttpServletRequestWrapper { @Override public String getPathTranslated() { final String p = getPathInfo(); - return p != null ? getRealPath(p) : null; + return p != null ? getSession().getServletContext().getRealPath(p) : null; } @Override diff --git a/org.eclipse.jgit.http.server/src/org/eclipse/jgit/http/server/resolver/AsIsFileService.java b/org.eclipse.jgit.http.server/src/org/eclipse/jgit/http/server/resolver/AsIsFileService.java index 43a7e246d6..69918c0158 100644 --- a/org.eclipse.jgit.http.server/src/org/eclipse/jgit/http/server/resolver/AsIsFileService.java +++ b/org.eclipse.jgit.http.server/src/org/eclipse/jgit/http/server/resolver/AsIsFileService.java @@ -1,54 +1,19 @@ /* - * Copyright (C) 2009-2010, Google Inc. - * and other copyright owners as documented in the project's IP log. + * Copyright (C) 2009-2010, Google Inc. and others * - * This program and the accompanying materials are made available - * under the terms of the Eclipse Distribution License v1.0 which - * accompanies this distribution, is reproduced below, and is - * available at http://www.eclipse.org/org/documents/edl-v10.php + * This program and the accompanying materials are made available under the + * terms of the Eclipse Distribution License v. 1.0 which is available at + * https://www.eclipse.org/org/documents/edl-v10.php. * - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or - * without modification, are permitted provided that the following - * conditions are met: - * - * - Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * - Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following - * disclaimer in the documentation and/or other materials provided - * with the distribution. - * - * - Neither the name of the Eclipse Foundation, Inc. nor the - * names of its contributors may be used to endorse or promote - * products derived from this software without specific prior - * written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND - * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES - * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR - * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT - * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; - * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER - * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, - * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF - * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * SPDX-License-Identifier: BSD-3-Clause */ package org.eclipse.jgit.http.server.resolver; -import javax.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletRequest; -import org.eclipse.jgit.http.server.GitServlet; import org.eclipse.jgit.lib.Config; import org.eclipse.jgit.lib.Repository; -import org.eclipse.jgit.lib.Config.SectionParser; import org.eclipse.jgit.transport.resolver.ServiceNotAuthorizedException; import org.eclipse.jgit.transport.resolver.ServiceNotEnabledException; @@ -58,8 +23,9 @@ import org.eclipse.jgit.transport.resolver.ServiceNotEnabledException; * Older HTTP clients which do not speak the smart HTTP variant of the Git * protocol fetch from a repository by directly getting its objects and pack * files. This class, along with the {@code http.getanyfile} per-repository - * configuration setting, can be used by {@link GitServlet} to control whether - * or not these older clients are permitted to read these direct files. + * configuration setting, can be used by + * {@link org.eclipse.jgit.http.server.GitServlet} to control whether or not + * these older clients are permitted to read these direct files. */ public class AsIsFileService { /** Always throws {@link ServiceNotEnabledException}. */ @@ -71,16 +37,10 @@ public class AsIsFileService { } }; - private static final SectionParser<ServiceConfig> CONFIG = new SectionParser<ServiceConfig>() { - public ServiceConfig parse(final Config cfg) { - return new ServiceConfig(cfg); - } - }; - private static class ServiceConfig { final boolean enabled; - ServiceConfig(final Config cfg) { + ServiceConfig(Config cfg) { enabled = cfg.getBoolean("http", "getanyfile", true); } } @@ -95,7 +55,7 @@ public class AsIsFileService { * {@code true}. */ protected static boolean isEnabled(Repository db) { - return db.getConfig().get(CONFIG).enabled; + return db.getConfig().get(ServiceConfig::new).enabled; } /** @@ -105,8 +65,10 @@ public class AsIsFileService { * throwing a checked exception if access should be denied. * <p> * The default implementation of this method checks {@code http.getanyfile}, - * throwing {@link ServiceNotEnabledException} if it was explicitly set to - * {@code false}, and otherwise succeeding silently. + * throwing + * {@link org.eclipse.jgit.transport.resolver.ServiceNotEnabledException} if + * it was explicitly set to {@code false}, and otherwise succeeding + * silently. * * @param req * current HTTP request, in case information from the request may @@ -119,6 +81,7 @@ public class AsIsFileService { * @throws ServiceNotAuthorizedException * bare file access is not allowed for this HTTP request and * repository, such as due to a permission error. + * @since 7.0 */ public void access(HttpServletRequest req, Repository db) throws ServiceNotEnabledException, ServiceNotAuthorizedException { diff --git a/org.eclipse.jgit.http.server/src/org/eclipse/jgit/http/server/resolver/DefaultReceivePackFactory.java b/org.eclipse.jgit.http.server/src/org/eclipse/jgit/http/server/resolver/DefaultReceivePackFactory.java index df876ba436..0bf09f7ede 100644 --- a/org.eclipse.jgit.http.server/src/org/eclipse/jgit/http/server/resolver/DefaultReceivePackFactory.java +++ b/org.eclipse.jgit.http.server/src/org/eclipse/jgit/http/server/resolver/DefaultReceivePackFactory.java @@ -1,66 +1,34 @@ /* - * Copyright (C) 2009-2010, Google Inc. - * and other copyright owners as documented in the project's IP log. + * Copyright (C) 2009-2010, Google Inc. and others * - * This program and the accompanying materials are made available - * under the terms of the Eclipse Distribution License v1.0 which - * accompanies this distribution, is reproduced below, and is - * available at http://www.eclipse.org/org/documents/edl-v10.php + * This program and the accompanying materials are made available under the + * terms of the Eclipse Distribution License v. 1.0 which is available at + * https://www.eclipse.org/org/documents/edl-v10.php. * - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or - * without modification, are permitted provided that the following - * conditions are met: - * - * - Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * - Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following - * disclaimer in the documentation and/or other materials provided - * with the distribution. - * - * - Neither the name of the Eclipse Foundation, Inc. nor the - * names of its contributors may be used to endorse or promote - * products derived from this software without specific prior - * written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND - * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES - * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR - * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT - * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; - * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER - * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, - * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF - * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * SPDX-License-Identifier: BSD-3-Clause */ package org.eclipse.jgit.http.server.resolver; -import javax.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletRequest; import org.eclipse.jgit.lib.Config; import org.eclipse.jgit.lib.PersonIdent; import org.eclipse.jgit.lib.Repository; -import org.eclipse.jgit.lib.Config.SectionParser; import org.eclipse.jgit.transport.ReceivePack; import org.eclipse.jgit.transport.resolver.ReceivePackFactory; import org.eclipse.jgit.transport.resolver.ServiceNotAuthorizedException; import org.eclipse.jgit.transport.resolver.ServiceNotEnabledException; /** - * Create and configure {@link ReceivePack} service instance. + * Create and configure {@link org.eclipse.jgit.transport.ReceivePack} service + * instance. * <p> * Writing by receive-pack is permitted if any of the following is true: * <ul> * <li>The container has authenticated the user and set - * {@link HttpServletRequest#getRemoteUser()} to the authenticated name. + * {@link jakarta.servlet.http.HttpServletRequest#getRemoteUser()} to the + * authenticated name. * <li>The repository configuration file has {@code http.receivepack} explicitly * set to true. * </ul> @@ -68,26 +36,24 @@ import org.eclipse.jgit.transport.resolver.ServiceNotEnabledException; */ public class DefaultReceivePackFactory implements ReceivePackFactory<HttpServletRequest> { - private static final SectionParser<ServiceConfig> CONFIG = new SectionParser<ServiceConfig>() { - public ServiceConfig parse(final Config cfg) { - return new ServiceConfig(cfg); - } - }; - private static class ServiceConfig { final boolean set; final boolean enabled; - ServiceConfig(final Config cfg) { + ServiceConfig(Config cfg) { set = cfg.getString("http", null, "receivepack") != null; enabled = cfg.getBoolean("http", "receivepack", false); } } - public ReceivePack create(final HttpServletRequest req, final Repository db) + /** + * @since 7.0 + */ + @Override + public ReceivePack create(HttpServletRequest req, Repository db) throws ServiceNotEnabledException, ServiceNotAuthorizedException { - final ServiceConfig cfg = db.getConfig().get(CONFIG); + final ServiceConfig cfg = db.getConfig().get(ServiceConfig::new); String user = req.getRemoteUser(); if (cfg.set) { diff --git a/org.eclipse.jgit.http.server/src/org/eclipse/jgit/http/server/resolver/DefaultUploadPackFactory.java b/org.eclipse.jgit.http.server/src/org/eclipse/jgit/http/server/resolver/DefaultUploadPackFactory.java index 76f40c58a9..10a1b3e5f2 100644 --- a/org.eclipse.jgit.http.server/src/org/eclipse/jgit/http/server/resolver/DefaultUploadPackFactory.java +++ b/org.eclipse.jgit.http.server/src/org/eclipse/jgit/http/server/resolver/DefaultUploadPackFactory.java @@ -1,85 +1,58 @@ /* - * Copyright (C) 2009-2010, Google Inc. - * and other copyright owners as documented in the project's IP log. + * Copyright (C) 2009-2010, Google Inc. and others * - * This program and the accompanying materials are made available - * under the terms of the Eclipse Distribution License v1.0 which - * accompanies this distribution, is reproduced below, and is - * available at http://www.eclipse.org/org/documents/edl-v10.php + * This program and the accompanying materials are made available under the + * terms of the Eclipse Distribution License v. 1.0 which is available at + * https://www.eclipse.org/org/documents/edl-v10.php. * - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or - * without modification, are permitted provided that the following - * conditions are met: - * - * - Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * - Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following - * disclaimer in the documentation and/or other materials provided - * with the distribution. - * - * - Neither the name of the Eclipse Foundation, Inc. nor the - * names of its contributors may be used to endorse or promote - * products derived from this software without specific prior - * written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND - * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES - * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR - * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT - * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; - * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER - * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, - * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF - * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * SPDX-License-Identifier: BSD-3-Clause */ package org.eclipse.jgit.http.server.resolver; -import javax.servlet.http.HttpServletRequest; +import java.util.Arrays; + +import jakarta.servlet.http.HttpServletRequest; import org.eclipse.jgit.lib.Config; import org.eclipse.jgit.lib.Repository; -import org.eclipse.jgit.lib.Config.SectionParser; import org.eclipse.jgit.transport.UploadPack; import org.eclipse.jgit.transport.resolver.ServiceNotAuthorizedException; import org.eclipse.jgit.transport.resolver.ServiceNotEnabledException; import org.eclipse.jgit.transport.resolver.UploadPackFactory; /** - * Create and configure {@link UploadPack} service instance. + * Create and configure {@link org.eclipse.jgit.transport.UploadPack} service + * instance. * <p> * Reading by upload-pack is permitted unless {@code http.uploadpack} is * explicitly set to false. */ public class DefaultUploadPackFactory implements UploadPackFactory<HttpServletRequest> { - private static final SectionParser<ServiceConfig> CONFIG = new SectionParser<ServiceConfig>() { - public ServiceConfig parse(final Config cfg) { - return new ServiceConfig(cfg); - } - }; - private static class ServiceConfig { final boolean enabled; - ServiceConfig(final Config cfg) { + ServiceConfig(Config cfg) { enabled = cfg.getBoolean("http", "uploadpack", true); } } - public UploadPack create(final HttpServletRequest req, final Repository db) + /** + * @since 7.0 + */ + @Override + public UploadPack create(HttpServletRequest req, Repository db) throws ServiceNotEnabledException, ServiceNotAuthorizedException { - if (db.getConfig().get(CONFIG).enabled) - return new UploadPack(db); - else - throw new ServiceNotEnabledException(); + if (db.getConfig().get(ServiceConfig::new).enabled) { + UploadPack up = new UploadPack(db); + String header = req.getHeader("Git-Protocol"); //$NON-NLS-1$ + if (header != null) { + String[] params = header.split(":"); //$NON-NLS-1$ + up.setExtraParameters(Arrays.asList(params)); + } + return up; + } + throw new ServiceNotEnabledException(); } } |