You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

HttpClientConnectionFactory.java 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /*
  2. * Copyright (C) 2013, 2020 Christian Halstrick <christian.halstrick@sap.com> and others
  3. *
  4. * This program and the accompanying materials are made available under the
  5. * terms of the Eclipse Distribution License v. 1.0 which is available at
  6. * https://www.eclipse.org/org/documents/edl-v10.php.
  7. *
  8. * SPDX-License-Identifier: BSD-3-Clause
  9. */
  10. package org.eclipse.jgit.transport.http.apache;
  11. import java.io.IOException;
  12. import java.net.Proxy;
  13. import java.net.URL;
  14. import java.security.GeneralSecurityException;
  15. import java.text.MessageFormat;
  16. import javax.net.ssl.HostnameVerifier;
  17. import javax.net.ssl.SSLContext;
  18. import javax.net.ssl.SSLSocket;
  19. import javax.net.ssl.TrustManager;
  20. import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
  21. import org.eclipse.jgit.transport.http.HttpConnection;
  22. import org.eclipse.jgit.transport.http.HttpConnectionFactory2;
  23. import org.eclipse.jgit.transport.http.NoCheckX509TrustManager;
  24. import org.eclipse.jgit.transport.http.apache.internal.HttpApacheText;
  25. import org.eclipse.jgit.util.HttpSupport;
  26. /**
  27. * A factory returning instances of {@link HttpClientConnection}.
  28. *
  29. * @since 3.3
  30. */
  31. public class HttpClientConnectionFactory implements HttpConnectionFactory2 {
  32. @Override
  33. public HttpConnection create(URL url) throws IOException {
  34. return new HttpClientConnection(url.toString());
  35. }
  36. @Override
  37. public HttpConnection create(URL url, Proxy proxy) throws IOException {
  38. return new HttpClientConnection(url.toString(), proxy);
  39. }
  40. @Override
  41. public GitSession newSession() {
  42. return new HttpClientSession();
  43. }
  44. private static class HttpClientSession implements GitSession {
  45. private SSLContext securityContext;
  46. private SSLConnectionSocketFactory socketFactory;
  47. private boolean isDefault;
  48. @Override
  49. public HttpClientConnection configure(HttpConnection connection,
  50. boolean sslVerify)
  51. throws IOException, GeneralSecurityException {
  52. if (!(connection instanceof HttpClientConnection)) {
  53. throw new IllegalArgumentException(MessageFormat.format(
  54. HttpApacheText.get().httpWrongConnectionType,
  55. HttpClientConnection.class.getName(),
  56. connection.getClass().getName()));
  57. }
  58. HttpClientConnection conn = (HttpClientConnection) connection;
  59. String scheme = conn.getURL().getProtocol();
  60. if (!"https".equals(scheme)) { //$NON-NLS-1$
  61. return conn;
  62. }
  63. if (securityContext == null || isDefault != sslVerify) {
  64. isDefault = sslVerify;
  65. HostnameVerifier verifier;
  66. if (sslVerify) {
  67. securityContext = SSLContext.getDefault();
  68. verifier = SSLConnectionSocketFactory
  69. .getDefaultHostnameVerifier();
  70. } else {
  71. securityContext = SSLContext.getInstance("TLS");
  72. TrustManager[] trustAllCerts = {
  73. new NoCheckX509TrustManager() };
  74. securityContext.init(null, trustAllCerts, null);
  75. verifier = (name, session) -> true;
  76. }
  77. socketFactory = new SSLConnectionSocketFactory(securityContext,
  78. verifier) {
  79. @Override
  80. protected void prepareSocket(SSLSocket socket)
  81. throws IOException {
  82. super.prepareSocket(socket);
  83. HttpSupport.configureTLS(socket);
  84. }
  85. };
  86. }
  87. conn.setSSLSocketFactory(socketFactory, isDefault);
  88. return conn;
  89. }
  90. @Override
  91. public void close() {
  92. securityContext = null;
  93. socketFactory = null;
  94. }
  95. }
  96. }