LCOV - code coverage report
Current view: top level - pgm/util - ProxyUtil.java (source / functions) Hit Total Coverage
Test: _coverage_report.dat Lines: 3 19 15.8 %
Date: 2022-11-19 15:00:39 Functions: 1 2 50.0 %

          Line data    Source code
       1             : // (Copied from JGit org.eclipse.jgit.pgm.Main)
       2             : // Copyright (C) 2006, Robin Rosenberg <robin.rosenberg@dewire.com>
       3             : // Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
       4             : //
       5             : // All rights reserved.
       6             : //
       7             : // Redistribution and use in source and binary forms, with or
       8             : // without modification, are permitted provided that the following
       9             : // conditions are met:
      10             : //
      11             : // - Redistributions of source code must retain the above copyright
      12             : // notice, this list of conditions and the following disclaimer.
      13             : //
      14             : // - Redistributions in binary form must reproduce the above
      15             : // copyright notice, this list of conditions and the following
      16             : // disclaimer in the documentation and/or other materials provided
      17             : // with the distribution.
      18             : //
      19             : // - Neither the name of the Eclipse Foundation, Inc. nor the
      20             : // names of its contributors may be used to endorse or promote
      21             : // products derived from this software without specific prior
      22             : // written permission.
      23             : //
      24             : // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
      25             : // CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
      26             : // INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
      27             : // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
      28             : // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
      29             : // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
      30             : // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
      31             : // NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
      32             : // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
      33             : // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
      34             : // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
      35             : // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
      36             : // ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
      37             : 
      38             : package com.google.gerrit.pgm.util;
      39             : 
      40             : import com.google.common.base.Strings;
      41             : import java.net.MalformedURLException;
      42             : import java.net.URL;
      43             : import org.eclipse.jgit.util.CachedAuthenticator;
      44             : 
      45             : final class ProxyUtil {
      46             :   /**
      47             :    * Configure the JRE's standard HTTP based on {@code http_proxy}.
      48             :    *
      49             :    * <p>The popular libcurl library honors the {@code http_proxy} environment variable as a means of
      50             :    * specifying an HTTP proxy for requests made behind a firewall. This is not natively recognized
      51             :    * by the JRE, so this method can be used by command line utilities to configure the JRE before
      52             :    * the first request is sent.
      53             :    *
      54             :    * @throws MalformedURLException the value in {@code http_proxy} is unsupportable.
      55             :    */
      56             :   static void configureHttpProxy() throws MalformedURLException {
      57          15 :     final String s = System.getenv("http_proxy");
      58          15 :     if (Strings.isNullOrEmpty(s)) {
      59          15 :       return;
      60             :     }
      61             : 
      62           0 :     final URL u = new URL(!s.contains("://") ? "http://" + s : s);
      63           0 :     if (!"http".equals(u.getProtocol())) {
      64           0 :       throw new MalformedURLException("Invalid http_proxy: " + s + ": Only http supported.");
      65             :     }
      66             : 
      67           0 :     final String proxyHost = u.getHost();
      68           0 :     final int proxyPort = u.getPort();
      69             : 
      70           0 :     System.setProperty("http.proxyHost", proxyHost);
      71           0 :     if (proxyPort > 0) {
      72           0 :       System.setProperty("http.proxyPort", String.valueOf(proxyPort));
      73             :     }
      74             : 
      75           0 :     final String userpass = u.getUserInfo();
      76           0 :     if (userpass != null && userpass.contains(":")) {
      77           0 :       final int c = userpass.indexOf(':');
      78           0 :       final String user = userpass.substring(0, c);
      79           0 :       final String pass = userpass.substring(c + 1);
      80           0 :       CachedAuthenticator.add(
      81             :           new CachedAuthenticator.CachedAuthentication(proxyHost, proxyPort, user, pass));
      82             :     }
      83           0 :   }
      84             : 
      85           0 :   ProxyUtil() {}
      86             : }

Generated by: LCOV version 1.16+git.20220603.dfeb750