From 2e5214462e7f7d93ff39b522c2343105443393c9 Mon Sep 17 00:00:00 2001 From: "Shawn O. Pearce" Date: Wed, 6 Jan 2010 11:13:25 -0800 Subject: [PATCH] server side: smart fetch over HTTP Clients can request smart fetch support by examining the info/refs URL with the service parameter set to the magic git-upload-pack string: GET /$GIT_DIR/info/refs?service=git-upload-pack HTTP/1.1 The response is formatted with the upload pack capabilities, using the standard packet line formatter. A special header line is put in front of the standard upload-pack advertisement to let clients know the service was recognized and is supported. If the requested service is disabled an authorization status code is returned, allowing the user agent to retry once they have obtained credentials from a human, in case authentication is required by the configured UploadPackFactory implementation. Change-Id: Ib0f1a458c88b4b5509b0f882f55f83f5752bc57a Signed-off-by: Shawn O. Pearce --- .../eclipse/jgit/http/server/GitServlet.java | 24 ++++ .../jgit/http/server/UploadPackServlet.java | 128 ++++++++++++++++++ .../resolver/DefaultUploadPackFactory.java | 81 +++++++++++ .../server/resolver/UploadPackFactory.java | 79 +++++++++++ 4 files changed, 312 insertions(+) create mode 100644 org.eclipse.jgit.http.server/src/org/eclipse/jgit/http/server/UploadPackServlet.java create mode 100644 org.eclipse.jgit.http.server/src/org/eclipse/jgit/http/server/resolver/DefaultUploadPackFactory.java create mode 100644 org.eclipse.jgit.http.server/src/org/eclipse/jgit/http/server/resolver/UploadPackFactory.java 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 1e8a51192..dced779c9 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 @@ -54,12 +54,15 @@ import org.eclipse.jgit.http.server.glue.MetaServlet; import org.eclipse.jgit.http.server.glue.RegexGroupFilter; import org.eclipse.jgit.http.server.glue.ServletBinder; import org.eclipse.jgit.http.server.resolver.DefaultReceivePackFactory; +import org.eclipse.jgit.http.server.resolver.DefaultUploadPackFactory; import org.eclipse.jgit.http.server.resolver.FileResolver; import org.eclipse.jgit.http.server.resolver.AsIsFileService; import org.eclipse.jgit.http.server.resolver.ReceivePackFactory; import org.eclipse.jgit.http.server.resolver.RepositoryResolver; import org.eclipse.jgit.lib.Constants; +import org.eclipse.jgit.http.server.resolver.UploadPackFactory; import org.eclipse.jgit.transport.ReceivePack; +import org.eclipse.jgit.transport.UploadPack; import org.eclipse.jgit.util.StringUtils; /** @@ -105,6 +108,8 @@ public class GitServlet extends MetaServlet { private AsIsFileService asIs = new AsIsFileService(); + private UploadPackFactory uploadPackFactory = new DefaultUploadPackFactory(); + private ReceivePackFactory receivePackFactory = new DefaultReceivePackFactory(); /** @@ -142,6 +147,16 @@ public class GitServlet extends MetaServlet { this.asIs = f != null ? f : AsIsFileService.DISABLED; } + /** + * @param f + * the factory to construct and configure an {@link UploadPack} + * session when a fetch or clone is requested by a client. + */ + public void setUploadPackFactory(UploadPackFactory f) { + assertNotInitialized(); + this.uploadPackFactory = f != null ? f : UploadPackFactory.DISABLED; + } + /** * @param f * the factory to construct and configure a {@link ReceivePack} @@ -169,12 +184,21 @@ public class GitServlet extends MetaServlet { initialized = true; + if (uploadPackFactory != ReceivePackFactory.DISABLED) { + serve("*/git-upload-pack")// + .with(new UploadPackServlet(uploadPackFactory)); + } + if (receivePackFactory != ReceivePackFactory.DISABLED) { serve("*/git-receive-pack")// .with(new ReceivePackServlet(receivePackFactory)); } ServletBinder refs = serve("*/" + Constants.INFO_REFS); + if (uploadPackFactory != UploadPackFactory.DISABLED) { + refs = refs.through(// + new UploadPackServlet.InfoRefs(uploadPackFactory)); + } if (receivePackFactory != ReceivePackFactory.DISABLED) { refs = refs.through(// new ReceivePackServlet.InfoRefs(receivePackFactory)); 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 new file mode 100644 index 000000000..dc874fa5f --- /dev/null +++ b/org.eclipse.jgit.http.server/src/org/eclipse/jgit/http/server/UploadPackServlet.java @@ -0,0 +1,128 @@ +/* + * Copyright (C) 2009-2010, Google Inc. + * and other copyright owners as documented in the project's IP log. + * + * 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 + * + * 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. + */ + +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_UNAUTHORIZED; +import static javax.servlet.http.HttpServletResponse.SC_UNSUPPORTED_MEDIA_TYPE; +import static org.eclipse.jgit.http.server.ServletUtils.getInputStream; +import static org.eclipse.jgit.http.server.ServletUtils.getRepository; + +import java.io.IOException; + +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import org.eclipse.jgit.http.server.resolver.ServiceNotAuthorizedException; +import org.eclipse.jgit.http.server.resolver.ServiceNotEnabledException; +import org.eclipse.jgit.http.server.resolver.UploadPackFactory; +import org.eclipse.jgit.lib.Repository; +import org.eclipse.jgit.transport.UploadPack; +import org.eclipse.jgit.transport.RefAdvertiser.PacketLineOutRefAdvertiser; + +/** Server side implementation of smart fetch over HTTP. */ +class UploadPackServlet extends HttpServlet { + private static final String REQ_TYPE = "application/x-git-upload-pack-request"; + + private static final String RSP_TYPE = "application/x-git-upload-pack-result"; + + private static final long serialVersionUID = 1L; + + static class InfoRefs extends SmartServiceInfoRefs { + private final UploadPackFactory uploadPackFactory; + + InfoRefs(final UploadPackFactory uploadPackFactory) { + super("git-upload-pack"); + this.uploadPackFactory = uploadPackFactory; + } + + @Override + protected void advertise(HttpServletRequest req, Repository db, + PacketLineOutRefAdvertiser pck) throws IOException, + ServiceNotEnabledException, ServiceNotAuthorizedException { + uploadPackFactory.create(req, db).sendAdvertisedRefs(pck); + } + } + + private final UploadPackFactory uploadPackFactory; + + UploadPackServlet(final UploadPackFactory uploadPackFactory) { + this.uploadPackFactory = uploadPackFactory; + } + + @Override + public void doPost(final HttpServletRequest req, + final HttpServletResponse rsp) throws IOException { + if (!REQ_TYPE.equals(req.getContentType())) { + rsp.sendError(SC_UNSUPPORTED_MEDIA_TYPE); + return; + } + + final Repository db = getRepository(req); + try { + final UploadPack up = uploadPackFactory.create(req, db); + up.setBiDirectionalPipe(false); + rsp.setContentType(RSP_TYPE); + up.upload(getInputStream(req), rsp.getOutputStream(), null); + + } catch (ServiceNotAuthorizedException e) { + rsp.reset(); + rsp.sendError(SC_UNAUTHORIZED); + return; + + } catch (ServiceNotEnabledException e) { + rsp.reset(); + rsp.sendError(SC_FORBIDDEN); + return; + + } catch (IOException e) { + getServletContext().log("Internal error during upload-pack", e); + rsp.reset(); + rsp.sendError(SC_INTERNAL_SERVER_ERROR); + return; + } + } +} 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 new file mode 100644 index 000000000..a8c953e18 --- /dev/null +++ b/org.eclipse.jgit.http.server/src/org/eclipse/jgit/http/server/resolver/DefaultUploadPackFactory.java @@ -0,0 +1,81 @@ +/* + * Copyright (C) 2009-2010, Google Inc. + * and other copyright owners as documented in the project's IP log. + * + * 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 + * + * 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. + */ + +package org.eclipse.jgit.http.server.resolver; + +import javax.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; + +/** + * Create and configure {@link UploadPack} service instance. + *

+ * Reading by upload-pack is permitted unless {@code http.uploadpack} is + * explicitly set to false. + */ +public class DefaultUploadPackFactory implements UploadPackFactory { + private static final SectionParser CONFIG = new SectionParser() { + public ServiceConfig parse(final Config cfg) { + return new ServiceConfig(cfg); + } + }; + + private static class ServiceConfig { + final boolean enabled; + + ServiceConfig(final Config cfg) { + enabled = cfg.getBoolean("http", "uploadpack", true); + } + } + + public UploadPack create(final HttpServletRequest req, final Repository db) + throws ServiceNotEnabledException, ServiceNotAuthorizedException { + if (db.getConfig().get(CONFIG).enabled) + return new UploadPack(db); + else + throw new ServiceNotEnabledException(); + } +} diff --git a/org.eclipse.jgit.http.server/src/org/eclipse/jgit/http/server/resolver/UploadPackFactory.java b/org.eclipse.jgit.http.server/src/org/eclipse/jgit/http/server/resolver/UploadPackFactory.java new file mode 100644 index 000000000..83bf9be07 --- /dev/null +++ b/org.eclipse.jgit.http.server/src/org/eclipse/jgit/http/server/resolver/UploadPackFactory.java @@ -0,0 +1,79 @@ +/* + * Copyright (C) 2009-2010, Google Inc. + * and other copyright owners as documented in the project's IP log. + * + * 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 + * + * 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. + */ + +package org.eclipse.jgit.http.server.resolver; + +import javax.servlet.http.HttpServletRequest; + +import org.eclipse.jgit.lib.Repository; +import org.eclipse.jgit.transport.UploadPack; + +/** Create and configure {@link UploadPack} service instance. */ +public interface UploadPackFactory { + /** A factory disabling the UploadPack service for all repositories. */ + public static final UploadPackFactory DISABLED = new UploadPackFactory() { + public UploadPack create(HttpServletRequest req, Repository db) + throws ServiceNotEnabledException { + throw new ServiceNotEnabledException(); + } + }; + + /** + * Create and configure a new UploadPack instance for a repository. + * + * @param req + * current HTTP request, in case information from the request may + * help configure the UploadPack instance. + * @param db + * the repository the upload would read from. + * @return the newly configured UploadPack instance, must not be null. + * @throws ServiceNotEnabledException + * this factory refuses to create the instance because it is not + * allowed on the target repository, by any user. + * @throws ServiceNotAuthorizedException + * this factory refuses to create the instance for this HTTP + * request and repository, such as due to a permission error. + */ + UploadPack create(HttpServletRequest req, Repository db) + throws ServiceNotEnabledException, ServiceNotAuthorizedException; +}