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.

TransportHttp.java 45KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450
  1. /*
  2. * Copyright (C) 2008-2010, Google Inc.
  3. * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
  4. * Copyright (C) 2013, Matthias Sohn <matthias.sohn@sap.com>
  5. * Copyright (C) 2017, Thomas Wolf <thomas.wolf@paranor.ch>
  6. * and other copyright owners as documented in the project's IP log.
  7. *
  8. * This program and the accompanying materials are made available
  9. * under the terms of the Eclipse Distribution License v1.0 which
  10. * accompanies this distribution, is reproduced below, and is
  11. * available at http://www.eclipse.org/org/documents/edl-v10.php
  12. *
  13. * All rights reserved.
  14. *
  15. * Redistribution and use in source and binary forms, with or
  16. * without modification, are permitted provided that the following
  17. * conditions are met:
  18. *
  19. * - Redistributions of source code must retain the above copyright
  20. * notice, this list of conditions and the following disclaimer.
  21. *
  22. * - Redistributions in binary form must reproduce the above
  23. * copyright notice, this list of conditions and the following
  24. * disclaimer in the documentation and/or other materials provided
  25. * with the distribution.
  26. *
  27. * - Neither the name of the Eclipse Foundation, Inc. nor the
  28. * names of its contributors may be used to endorse or promote
  29. * products derived from this software without specific prior
  30. * written permission.
  31. *
  32. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  33. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  34. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  35. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  36. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  37. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  38. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  39. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  40. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  41. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  42. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  43. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  44. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  45. */
  46. package org.eclipse.jgit.transport;
  47. import static java.nio.charset.StandardCharsets.UTF_8;
  48. import static org.eclipse.jgit.lib.Constants.HEAD;
  49. import static org.eclipse.jgit.util.HttpSupport.ENCODING_GZIP;
  50. import static org.eclipse.jgit.util.HttpSupport.ENCODING_X_GZIP;
  51. import static org.eclipse.jgit.util.HttpSupport.HDR_ACCEPT;
  52. import static org.eclipse.jgit.util.HttpSupport.HDR_ACCEPT_ENCODING;
  53. import static org.eclipse.jgit.util.HttpSupport.HDR_CONTENT_ENCODING;
  54. import static org.eclipse.jgit.util.HttpSupport.HDR_CONTENT_TYPE;
  55. import static org.eclipse.jgit.util.HttpSupport.HDR_LOCATION;
  56. import static org.eclipse.jgit.util.HttpSupport.HDR_PRAGMA;
  57. import static org.eclipse.jgit.util.HttpSupport.HDR_USER_AGENT;
  58. import static org.eclipse.jgit.util.HttpSupport.HDR_WWW_AUTHENTICATE;
  59. import static org.eclipse.jgit.util.HttpSupport.METHOD_GET;
  60. import static org.eclipse.jgit.util.HttpSupport.METHOD_POST;
  61. import java.io.BufferedReader;
  62. import java.io.ByteArrayInputStream;
  63. import java.io.FileNotFoundException;
  64. import java.io.IOException;
  65. import java.io.InputStream;
  66. import java.io.InputStreamReader;
  67. import java.io.OutputStream;
  68. import java.net.MalformedURLException;
  69. import java.net.Proxy;
  70. import java.net.ProxySelector;
  71. import java.net.URISyntaxException;
  72. import java.net.URL;
  73. import java.security.cert.CertPathBuilderException;
  74. import java.security.cert.CertPathValidatorException;
  75. import java.security.cert.CertificateException;
  76. import java.text.MessageFormat;
  77. import java.util.ArrayList;
  78. import java.util.Arrays;
  79. import java.util.Collection;
  80. import java.util.Collections;
  81. import java.util.EnumSet;
  82. import java.util.HashSet;
  83. import java.util.LinkedHashSet;
  84. import java.util.Locale;
  85. import java.util.Map;
  86. import java.util.Set;
  87. import java.util.TreeMap;
  88. import java.util.zip.GZIPInputStream;
  89. import java.util.zip.GZIPOutputStream;
  90. import javax.net.ssl.SSLHandshakeException;
  91. import org.eclipse.jgit.errors.ConfigInvalidException;
  92. import org.eclipse.jgit.errors.NoRemoteRepositoryException;
  93. import org.eclipse.jgit.errors.NotSupportedException;
  94. import org.eclipse.jgit.errors.PackProtocolException;
  95. import org.eclipse.jgit.errors.TransportException;
  96. import org.eclipse.jgit.internal.JGitText;
  97. import org.eclipse.jgit.internal.storage.file.RefDirectory;
  98. import org.eclipse.jgit.lib.Constants;
  99. import org.eclipse.jgit.lib.ObjectId;
  100. import org.eclipse.jgit.lib.ObjectIdRef;
  101. import org.eclipse.jgit.lib.ProgressMonitor;
  102. import org.eclipse.jgit.lib.Ref;
  103. import org.eclipse.jgit.lib.Repository;
  104. import org.eclipse.jgit.lib.StoredConfig;
  105. import org.eclipse.jgit.lib.SymbolicRef;
  106. import org.eclipse.jgit.transport.HttpAuthMethod.Type;
  107. import org.eclipse.jgit.transport.HttpConfig.HttpRedirectMode;
  108. import org.eclipse.jgit.transport.http.HttpConnection;
  109. import org.eclipse.jgit.util.HttpSupport;
  110. import org.eclipse.jgit.util.IO;
  111. import org.eclipse.jgit.util.RawParseUtils;
  112. import org.eclipse.jgit.util.SystemReader;
  113. import org.eclipse.jgit.util.TemporaryBuffer;
  114. import org.eclipse.jgit.util.io.DisabledOutputStream;
  115. import org.eclipse.jgit.util.io.UnionInputStream;
  116. import org.slf4j.Logger;
  117. import org.slf4j.LoggerFactory;
  118. /**
  119. * Transport over HTTP and FTP protocols.
  120. * <p>
  121. * If the transport is using HTTP and the remote HTTP service is Git-aware
  122. * (speaks the "smart-http protocol") this client will automatically take
  123. * advantage of the additional Git-specific HTTP extensions. If the remote
  124. * service does not support these extensions, the client will degrade to direct
  125. * file fetching.
  126. * <p>
  127. * If the remote (server side) repository does not have the specialized Git
  128. * support, object files are retrieved directly through standard HTTP GET (or
  129. * binary FTP GET) requests. This make it easy to serve a Git repository through
  130. * a standard web host provider that does not offer specific support for Git.
  131. *
  132. * @see WalkFetchConnection
  133. */
  134. public class TransportHttp extends HttpTransport implements WalkTransport,
  135. PackTransport {
  136. private static final Logger LOG = LoggerFactory
  137. .getLogger(TransportHttp.class);
  138. private static final String SVC_UPLOAD_PACK = "git-upload-pack"; //$NON-NLS-1$
  139. private static final String SVC_RECEIVE_PACK = "git-receive-pack"; //$NON-NLS-1$
  140. /**
  141. * Accept-Encoding header in the HTTP request
  142. * (https://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html).
  143. *
  144. * @since 4.6
  145. */
  146. public enum AcceptEncoding {
  147. /**
  148. * Do not specify an Accept-Encoding header. In most servers this
  149. * results in the content being transmitted as-is.
  150. */
  151. UNSPECIFIED,
  152. /**
  153. * Accept gzip content encoding.
  154. */
  155. GZIP
  156. }
  157. static final TransportProtocol PROTO_HTTP = new TransportProtocol() {
  158. private final String[] schemeNames = { "http", "https" }; //$NON-NLS-1$ //$NON-NLS-2$
  159. private final Set<String> schemeSet = Collections
  160. .unmodifiableSet(new LinkedHashSet<>(Arrays
  161. .asList(schemeNames)));
  162. @Override
  163. public String getName() {
  164. return JGitText.get().transportProtoHTTP;
  165. }
  166. @Override
  167. public Set<String> getSchemes() {
  168. return schemeSet;
  169. }
  170. @Override
  171. public Set<URIishField> getRequiredFields() {
  172. return Collections.unmodifiableSet(EnumSet.of(URIishField.HOST,
  173. URIishField.PATH));
  174. }
  175. @Override
  176. public Set<URIishField> getOptionalFields() {
  177. return Collections.unmodifiableSet(EnumSet.of(URIishField.USER,
  178. URIishField.PASS, URIishField.PORT));
  179. }
  180. @Override
  181. public int getDefaultPort() {
  182. return 80;
  183. }
  184. @Override
  185. public Transport open(URIish uri, Repository local, String remoteName)
  186. throws NotSupportedException {
  187. return new TransportHttp(local, uri);
  188. }
  189. @Override
  190. public Transport open(URIish uri) throws NotSupportedException {
  191. return new TransportHttp(uri);
  192. }
  193. };
  194. static final TransportProtocol PROTO_FTP = new TransportProtocol() {
  195. @Override
  196. public String getName() {
  197. return JGitText.get().transportProtoFTP;
  198. }
  199. @Override
  200. public Set<String> getSchemes() {
  201. return Collections.singleton("ftp"); //$NON-NLS-1$
  202. }
  203. @Override
  204. public Set<URIishField> getRequiredFields() {
  205. return Collections.unmodifiableSet(EnumSet.of(URIishField.HOST,
  206. URIishField.PATH));
  207. }
  208. @Override
  209. public Set<URIishField> getOptionalFields() {
  210. return Collections.unmodifiableSet(EnumSet.of(URIishField.USER,
  211. URIishField.PASS, URIishField.PORT));
  212. }
  213. @Override
  214. public int getDefaultPort() {
  215. return 21;
  216. }
  217. @Override
  218. public Transport open(URIish uri, Repository local, String remoteName)
  219. throws NotSupportedException {
  220. return new TransportHttp(local, uri);
  221. }
  222. };
  223. /**
  224. * The current URI we're talking to. The inherited (final) field
  225. * {@link #uri} stores the original URI; {@code currentUri} may be different
  226. * after redirects.
  227. */
  228. private URIish currentUri;
  229. private URL baseUrl;
  230. private URL objectsUrl;
  231. private final HttpConfig http;
  232. private final ProxySelector proxySelector;
  233. private boolean useSmartHttp = true;
  234. private HttpAuthMethod authMethod = HttpAuthMethod.Type.NONE.method(null);
  235. private Map<String, String> headers;
  236. private boolean sslVerify;
  237. private boolean sslFailure = false;
  238. TransportHttp(Repository local, URIish uri)
  239. throws NotSupportedException {
  240. super(local, uri);
  241. setURI(uri);
  242. http = new HttpConfig(local.getConfig(), uri);
  243. proxySelector = ProxySelector.getDefault();
  244. sslVerify = http.isSslVerify();
  245. }
  246. private URL toURL(URIish urish) throws MalformedURLException {
  247. String uriString = urish.toString();
  248. if (!uriString.endsWith("/")) { //$NON-NLS-1$
  249. uriString += '/';
  250. }
  251. return new URL(uriString);
  252. }
  253. /**
  254. * Set uri a {@link org.eclipse.jgit.transport.URIish} object.
  255. *
  256. * @param uri
  257. * a {@link org.eclipse.jgit.transport.URIish} object.
  258. * @throws org.eclipse.jgit.errors.NotSupportedException
  259. * @since 4.9
  260. */
  261. protected void setURI(URIish uri) throws NotSupportedException {
  262. try {
  263. currentUri = uri;
  264. baseUrl = toURL(uri);
  265. objectsUrl = new URL(baseUrl, "objects/"); //$NON-NLS-1$
  266. } catch (MalformedURLException e) {
  267. throw new NotSupportedException(MessageFormat.format(JGitText.get().invalidURL, uri), e);
  268. }
  269. }
  270. /**
  271. * Create a minimal HTTP transport with default configuration values.
  272. *
  273. * @param uri
  274. * @throws NotSupportedException
  275. */
  276. TransportHttp(URIish uri) throws NotSupportedException {
  277. super(uri);
  278. setURI(uri);
  279. http = new HttpConfig(uri);
  280. proxySelector = ProxySelector.getDefault();
  281. sslVerify = http.isSslVerify();
  282. }
  283. /**
  284. * Toggle whether or not smart HTTP transport should be used.
  285. * <p>
  286. * This flag exists primarily to support backwards compatibility testing
  287. * within a testing framework, there is no need to modify it in most
  288. * applications.
  289. *
  290. * @param on
  291. * if {@code true} (default), smart HTTP is enabled.
  292. */
  293. public void setUseSmartHttp(boolean on) {
  294. useSmartHttp = on;
  295. }
  296. @SuppressWarnings("resource") // Closed by caller
  297. private FetchConnection getConnection(HttpConnection c, InputStream in,
  298. String service) throws IOException {
  299. BaseConnection f;
  300. if (isSmartHttp(c, service)) {
  301. readSmartHeaders(in, service);
  302. f = new SmartHttpFetchConnection(in);
  303. } else {
  304. // Assume this server doesn't support smart HTTP fetch
  305. // and fall back on dumb object walking.
  306. f = newDumbConnection(in);
  307. }
  308. f.setPeerUserAgent(c.getHeaderField(HttpSupport.HDR_SERVER));
  309. return (FetchConnection) f;
  310. }
  311. /** {@inheritDoc} */
  312. @Override
  313. public FetchConnection openFetch() throws TransportException,
  314. NotSupportedException {
  315. final String service = SVC_UPLOAD_PACK;
  316. try {
  317. final HttpConnection c = connect(service);
  318. try (InputStream in = openInputStream(c)) {
  319. return getConnection(c, in, service);
  320. }
  321. } catch (NotSupportedException err) {
  322. throw err;
  323. } catch (TransportException err) {
  324. throw err;
  325. } catch (IOException err) {
  326. throw new TransportException(uri, JGitText.get().errorReadingInfoRefs, err);
  327. }
  328. }
  329. private WalkFetchConnection newDumbConnection(InputStream in)
  330. throws IOException, PackProtocolException {
  331. HttpObjectDB d = new HttpObjectDB(objectsUrl);
  332. Map<String, Ref> refs;
  333. try (BufferedReader br = toBufferedReader(in)) {
  334. refs = d.readAdvertisedImpl(br);
  335. }
  336. if (!refs.containsKey(HEAD)) {
  337. // If HEAD was not published in the info/refs file (it usually
  338. // is not there) download HEAD by itself as a loose file and do
  339. // the resolution by hand.
  340. //
  341. HttpConnection conn = httpOpen(
  342. METHOD_GET,
  343. new URL(baseUrl, HEAD),
  344. AcceptEncoding.GZIP);
  345. int status = HttpSupport.response(conn);
  346. switch (status) {
  347. case HttpConnection.HTTP_OK: {
  348. try (BufferedReader br = toBufferedReader(
  349. openInputStream(conn))) {
  350. String line = br.readLine();
  351. if (line != null && line.startsWith(RefDirectory.SYMREF)) {
  352. String target = line.substring(RefDirectory.SYMREF.length());
  353. Ref r = refs.get(target);
  354. if (r == null)
  355. r = new ObjectIdRef.Unpeeled(Ref.Storage.NEW, target, null);
  356. r = new SymbolicRef(HEAD, r);
  357. refs.put(r.getName(), r);
  358. } else if (line != null && ObjectId.isId(line)) {
  359. Ref r = new ObjectIdRef.Unpeeled(Ref.Storage.NETWORK,
  360. HEAD, ObjectId.fromString(line));
  361. refs.put(r.getName(), r);
  362. }
  363. }
  364. break;
  365. }
  366. case HttpConnection.HTTP_NOT_FOUND:
  367. break;
  368. default:
  369. throw new TransportException(uri, MessageFormat.format(
  370. JGitText.get().cannotReadHEAD, Integer.valueOf(status),
  371. conn.getResponseMessage()));
  372. }
  373. }
  374. WalkFetchConnection wfc = new WalkFetchConnection(this, d);
  375. wfc.available(refs);
  376. return wfc;
  377. }
  378. private BufferedReader toBufferedReader(InputStream in) {
  379. return new BufferedReader(new InputStreamReader(in, UTF_8));
  380. }
  381. /** {@inheritDoc} */
  382. @Override
  383. public PushConnection openPush() throws NotSupportedException,
  384. TransportException {
  385. final String service = SVC_RECEIVE_PACK;
  386. try {
  387. final HttpConnection c = connect(service);
  388. try (InputStream in = openInputStream(c)) {
  389. if (isSmartHttp(c, service)) {
  390. return smartPush(service, c, in);
  391. } else if (!useSmartHttp) {
  392. final String msg = JGitText.get().smartHTTPPushDisabled;
  393. throw new NotSupportedException(msg);
  394. } else {
  395. final String msg = JGitText.get().remoteDoesNotSupportSmartHTTPPush;
  396. throw new NotSupportedException(msg);
  397. }
  398. }
  399. } catch (NotSupportedException err) {
  400. throw err;
  401. } catch (TransportException err) {
  402. throw err;
  403. } catch (IOException err) {
  404. throw new TransportException(uri, JGitText.get().errorReadingInfoRefs, err);
  405. }
  406. }
  407. private PushConnection smartPush(String service, HttpConnection c,
  408. InputStream in) throws IOException, TransportException {
  409. readSmartHeaders(in, service);
  410. SmartHttpPushConnection p = new SmartHttpPushConnection(in);
  411. p.setPeerUserAgent(c.getHeaderField(HttpSupport.HDR_SERVER));
  412. return p;
  413. }
  414. /** {@inheritDoc} */
  415. @Override
  416. public void close() {
  417. // No explicit connections are maintained.
  418. }
  419. /**
  420. * Set additional headers on the HTTP connection
  421. *
  422. * @param headers
  423. * a map of name:values that are to be set as headers on the HTTP
  424. * connection
  425. * @since 3.4
  426. */
  427. public void setAdditionalHeaders(Map<String, String> headers) {
  428. this.headers = headers;
  429. }
  430. private NoRemoteRepositoryException createNotFoundException(URIish u,
  431. URL url, String msg) {
  432. String text;
  433. if (msg != null && !msg.isEmpty()) {
  434. text = MessageFormat.format(JGitText.get().uriNotFoundWithMessage,
  435. url, msg);
  436. } else {
  437. text = MessageFormat.format(JGitText.get().uriNotFound, url);
  438. }
  439. return new NoRemoteRepositoryException(u, text);
  440. }
  441. private HttpConnection connect(String service)
  442. throws TransportException, NotSupportedException {
  443. URL u = getServiceURL(service);
  444. int authAttempts = 1;
  445. int redirects = 0;
  446. Collection<Type> ignoreTypes = null;
  447. for (;;) {
  448. try {
  449. final HttpConnection conn = httpOpen(METHOD_GET, u, AcceptEncoding.GZIP);
  450. if (useSmartHttp) {
  451. String exp = "application/x-" + service + "-advertisement"; //$NON-NLS-1$ //$NON-NLS-2$
  452. conn.setRequestProperty(HDR_ACCEPT, exp + ", */*"); //$NON-NLS-1$
  453. } else {
  454. conn.setRequestProperty(HDR_ACCEPT, "*/*"); //$NON-NLS-1$
  455. }
  456. final int status = HttpSupport.response(conn);
  457. switch (status) {
  458. case HttpConnection.HTTP_OK:
  459. // Check if HttpConnection did some authentication in the
  460. // background (e.g Kerberos/SPNEGO).
  461. // That may not work for streaming requests and jgit
  462. // explicit authentication would be required
  463. if (authMethod.getType() == HttpAuthMethod.Type.NONE
  464. && conn.getHeaderField(HDR_WWW_AUTHENTICATE) != null)
  465. authMethod = HttpAuthMethod.scanResponse(conn, ignoreTypes);
  466. return conn;
  467. case HttpConnection.HTTP_NOT_FOUND:
  468. throw createNotFoundException(uri, u,
  469. conn.getResponseMessage());
  470. case HttpConnection.HTTP_UNAUTHORIZED:
  471. authMethod = HttpAuthMethod.scanResponse(conn, ignoreTypes);
  472. if (authMethod.getType() == HttpAuthMethod.Type.NONE)
  473. throw new TransportException(uri, MessageFormat.format(
  474. JGitText.get().authenticationNotSupported, uri));
  475. CredentialsProvider credentialsProvider = getCredentialsProvider();
  476. if (credentialsProvider == null)
  477. throw new TransportException(uri,
  478. JGitText.get().noCredentialsProvider);
  479. if (authAttempts > 1)
  480. credentialsProvider.reset(currentUri);
  481. if (3 < authAttempts
  482. || !authMethod.authorize(currentUri,
  483. credentialsProvider)) {
  484. throw new TransportException(uri,
  485. JGitText.get().notAuthorized);
  486. }
  487. authAttempts++;
  488. continue;
  489. case HttpConnection.HTTP_FORBIDDEN:
  490. throw new TransportException(uri, MessageFormat.format(
  491. JGitText.get().serviceNotPermitted, baseUrl,
  492. service));
  493. case HttpConnection.HTTP_MOVED_PERM:
  494. case HttpConnection.HTTP_MOVED_TEMP:
  495. case HttpConnection.HTTP_SEE_OTHER:
  496. case HttpConnection.HTTP_11_MOVED_TEMP:
  497. // SEE_OTHER should actually never be sent by a git server,
  498. // and in general should occur only on POST requests. But it
  499. // doesn't hurt to accept it here as a redirect.
  500. if (http.getFollowRedirects() == HttpRedirectMode.FALSE) {
  501. throw new TransportException(uri,
  502. MessageFormat.format(
  503. JGitText.get().redirectsOff,
  504. Integer.valueOf(status)));
  505. }
  506. URIish newUri = redirect(conn.getHeaderField(HDR_LOCATION),
  507. Constants.INFO_REFS, redirects++);
  508. setURI(newUri);
  509. u = getServiceURL(service);
  510. authAttempts = 1;
  511. break;
  512. default:
  513. String err = status + " " + conn.getResponseMessage(); //$NON-NLS-1$
  514. throw new TransportException(uri, err);
  515. }
  516. } catch (NotSupportedException e) {
  517. throw e;
  518. } catch (TransportException e) {
  519. throw e;
  520. } catch (SSLHandshakeException e) {
  521. handleSslFailure(e);
  522. continue; // Re-try
  523. } catch (IOException e) {
  524. if (authMethod.getType() != HttpAuthMethod.Type.NONE) {
  525. if (ignoreTypes == null) {
  526. ignoreTypes = new HashSet<>();
  527. }
  528. ignoreTypes.add(authMethod.getType());
  529. // reset auth method & attempts for next authentication type
  530. authMethod = HttpAuthMethod.Type.NONE.method(null);
  531. authAttempts = 1;
  532. continue;
  533. }
  534. throw new TransportException(uri, MessageFormat.format(JGitText.get().cannotOpenService, service), e);
  535. }
  536. }
  537. }
  538. private static class CredentialItems {
  539. CredentialItem.InformationalMessage message;
  540. /** Trust the server for this git operation */
  541. CredentialItem.YesNoType now;
  542. /**
  543. * Trust the server for all git operations from this repository; may be
  544. * {@code null} if the transport was created via
  545. * {@link #TransportHttp(URIish)}.
  546. */
  547. CredentialItem.YesNoType forRepo;
  548. /** Always trust the server from now on. */
  549. CredentialItem.YesNoType always;
  550. public CredentialItem[] items() {
  551. if (forRepo == null) {
  552. return new CredentialItem[] { message, now, always };
  553. } else {
  554. return new CredentialItem[] { message, now, forRepo, always };
  555. }
  556. }
  557. }
  558. private void handleSslFailure(Throwable e) throws TransportException {
  559. if (sslFailure || !trustInsecureSslConnection(e.getCause())) {
  560. throw new TransportException(uri,
  561. MessageFormat.format(
  562. JGitText.get().sslFailureExceptionMessage,
  563. currentUri.setPass(null)),
  564. e);
  565. }
  566. sslFailure = true;
  567. }
  568. private boolean trustInsecureSslConnection(Throwable cause) {
  569. if (cause instanceof CertificateException
  570. || cause instanceof CertPathBuilderException
  571. || cause instanceof CertPathValidatorException) {
  572. // Certificate expired or revoked, PKIX path building not
  573. // possible, self-signed certificate, host does not match ...
  574. CredentialsProvider provider = getCredentialsProvider();
  575. if (provider != null) {
  576. CredentialItems trust = constructSslTrustItems(cause);
  577. CredentialItem[] items = trust.items();
  578. if (provider.supports(items)) {
  579. boolean answered = provider.get(uri, items);
  580. if (answered) {
  581. // Not canceled
  582. boolean trustNow = trust.now.getValue();
  583. boolean trustLocal = trust.forRepo != null
  584. && trust.forRepo.getValue();
  585. boolean trustAlways = trust.always.getValue();
  586. if (trustNow || trustLocal || trustAlways) {
  587. sslVerify = false;
  588. if (trustAlways) {
  589. updateSslVerifyUser(false);
  590. } else if (trustLocal) {
  591. updateSslVerify(local.getConfig(), false);
  592. }
  593. return true;
  594. }
  595. }
  596. }
  597. }
  598. }
  599. return false;
  600. }
  601. private CredentialItems constructSslTrustItems(Throwable cause) {
  602. CredentialItems items = new CredentialItems();
  603. String info = MessageFormat.format(JGitText.get().sslFailureInfo,
  604. currentUri.setPass(null));
  605. String sslMessage = cause.getLocalizedMessage();
  606. if (sslMessage == null) {
  607. sslMessage = cause.toString();
  608. }
  609. sslMessage = MessageFormat.format(JGitText.get().sslFailureCause,
  610. sslMessage);
  611. items.message = new CredentialItem.InformationalMessage(info + '\n'
  612. + sslMessage + '\n'
  613. + JGitText.get().sslFailureTrustExplanation);
  614. items.now = new CredentialItem.YesNoType(JGitText.get().sslTrustNow);
  615. if (local != null) {
  616. items.forRepo = new CredentialItem.YesNoType(
  617. MessageFormat.format(JGitText.get().sslTrustForRepo,
  618. local.getDirectory()));
  619. }
  620. items.always = new CredentialItem.YesNoType(
  621. JGitText.get().sslTrustAlways);
  622. return items;
  623. }
  624. private void updateSslVerify(StoredConfig config, boolean value) {
  625. // Since git uses the original URI for matching, we must also use the
  626. // original URI and cannot use the current URI (which might be different
  627. // after redirects).
  628. String uriPattern = uri.getScheme() + "://" + uri.getHost(); //$NON-NLS-1$
  629. int port = uri.getPort();
  630. if (port > 0) {
  631. uriPattern += ":" + port; //$NON-NLS-1$
  632. }
  633. config.setBoolean(HttpConfig.HTTP, uriPattern,
  634. HttpConfig.SSL_VERIFY_KEY, value);
  635. try {
  636. config.save();
  637. } catch (IOException e) {
  638. LOG.error(JGitText.get().sslVerifyCannotSave, e);
  639. }
  640. }
  641. private void updateSslVerifyUser(boolean value) {
  642. StoredConfig userConfig = null;
  643. try {
  644. userConfig = SystemReader.getInstance().getUserConfig();
  645. updateSslVerify(userConfig, value);
  646. } catch (IOException | ConfigInvalidException e) {
  647. // Log it, but otherwise ignore here.
  648. LOG.error(e.getMessage(), e);
  649. }
  650. }
  651. private URIish redirect(String location, String checkFor, int redirects)
  652. throws TransportException {
  653. if (location == null || location.isEmpty()) {
  654. throw new TransportException(uri,
  655. MessageFormat.format(JGitText.get().redirectLocationMissing,
  656. baseUrl));
  657. }
  658. if (redirects >= http.getMaxRedirects()) {
  659. throw new TransportException(uri,
  660. MessageFormat.format(JGitText.get().redirectLimitExceeded,
  661. Integer.valueOf(http.getMaxRedirects()), baseUrl,
  662. location));
  663. }
  664. try {
  665. if (!isValidRedirect(baseUrl, location, checkFor)) {
  666. throw new TransportException(uri,
  667. MessageFormat.format(JGitText.get().redirectBlocked,
  668. baseUrl, location));
  669. }
  670. location = location.substring(0, location.indexOf(checkFor));
  671. URIish result = new URIish(location);
  672. if (LOG.isInfoEnabled()) {
  673. LOG.info(MessageFormat.format(JGitText.get().redirectHttp,
  674. uri.setPass(null),
  675. Integer.valueOf(redirects), baseUrl, result));
  676. }
  677. return result;
  678. } catch (URISyntaxException e) {
  679. throw new TransportException(uri,
  680. MessageFormat.format(JGitText.get().invalidRedirectLocation,
  681. baseUrl, location),
  682. e);
  683. }
  684. }
  685. private boolean isValidRedirect(URL current, String next, String checkFor) {
  686. // Protocols must be the same, or current is "http" and next "https". We
  687. // do not follow redirects from https back to http.
  688. String oldProtocol = current.getProtocol().toLowerCase(Locale.ROOT);
  689. int schemeEnd = next.indexOf("://"); //$NON-NLS-1$
  690. if (schemeEnd < 0) {
  691. return false;
  692. }
  693. String newProtocol = next.substring(0, schemeEnd)
  694. .toLowerCase(Locale.ROOT);
  695. if (!oldProtocol.equals(newProtocol)) {
  696. if (!"https".equals(newProtocol)) { //$NON-NLS-1$
  697. return false;
  698. }
  699. }
  700. // git allows only rewriting the root, i.e., everything before INFO_REFS
  701. // or the service name
  702. if (next.indexOf(checkFor) < 0) {
  703. return false;
  704. }
  705. // Basically we should test here that whatever follows INFO_REFS is
  706. // unchanged. But since we re-construct the query part
  707. // anyway, it doesn't matter.
  708. return true;
  709. }
  710. private URL getServiceURL(String service)
  711. throws NotSupportedException {
  712. try {
  713. final StringBuilder b = new StringBuilder();
  714. b.append(baseUrl);
  715. if (b.charAt(b.length() - 1) != '/') {
  716. b.append('/');
  717. }
  718. b.append(Constants.INFO_REFS);
  719. if (useSmartHttp) {
  720. b.append(b.indexOf("?") < 0 ? '?' : '&'); //$NON-NLS-1$
  721. b.append("service="); //$NON-NLS-1$
  722. b.append(service);
  723. }
  724. return new URL(b.toString());
  725. } catch (MalformedURLException e) {
  726. throw new NotSupportedException(MessageFormat.format(JGitText.get().invalidURL, uri), e);
  727. }
  728. }
  729. /**
  730. * Open an HTTP connection.
  731. *
  732. * @param method HTTP request method
  733. * @param u url of the HTTP connection
  734. * @param acceptEncoding accept-encoding header option
  735. * @return the HTTP connection
  736. * @throws java.io.IOException
  737. * @since 4.6
  738. */
  739. protected HttpConnection httpOpen(String method, URL u,
  740. AcceptEncoding acceptEncoding) throws IOException {
  741. if (method == null || u == null || acceptEncoding == null) {
  742. throw new NullPointerException();
  743. }
  744. final Proxy proxy = HttpSupport.proxyFor(proxySelector, u);
  745. HttpConnection conn = connectionFactory.create(u, proxy);
  746. if (!sslVerify && "https".equals(u.getProtocol())) { //$NON-NLS-1$
  747. HttpSupport.disableSslVerify(conn);
  748. }
  749. // We must do our own redirect handling to implement git rules and to
  750. // handle http->https redirects
  751. conn.setInstanceFollowRedirects(false);
  752. conn.setRequestMethod(method);
  753. conn.setUseCaches(false);
  754. if (acceptEncoding == AcceptEncoding.GZIP) {
  755. conn.setRequestProperty(HDR_ACCEPT_ENCODING, ENCODING_GZIP);
  756. }
  757. conn.setRequestProperty(HDR_PRAGMA, "no-cache"); //$NON-NLS-1$
  758. if (UserAgent.get() != null) {
  759. conn.setRequestProperty(HDR_USER_AGENT, UserAgent.get());
  760. }
  761. int timeOut = getTimeout();
  762. if (timeOut != -1) {
  763. int effTimeOut = timeOut * 1000;
  764. conn.setConnectTimeout(effTimeOut);
  765. conn.setReadTimeout(effTimeOut);
  766. }
  767. if (this.headers != null && !this.headers.isEmpty()) {
  768. for (Map.Entry<String, String> entry : this.headers.entrySet())
  769. conn.setRequestProperty(entry.getKey(), entry.getValue());
  770. }
  771. authMethod.configureRequest(conn);
  772. return conn;
  773. }
  774. final InputStream openInputStream(HttpConnection conn)
  775. throws IOException {
  776. InputStream input = conn.getInputStream();
  777. if (isGzipContent(conn))
  778. input = new GZIPInputStream(input);
  779. return input;
  780. }
  781. IOException wrongContentType(String expType, String actType) {
  782. final String why = MessageFormat.format(JGitText.get().expectedReceivedContentType, expType, actType);
  783. return new TransportException(uri, why);
  784. }
  785. private boolean isSmartHttp(HttpConnection c, String service) {
  786. final String expType = "application/x-" + service + "-advertisement"; //$NON-NLS-1$ //$NON-NLS-2$
  787. final String actType = c.getContentType();
  788. return expType.equals(actType);
  789. }
  790. private boolean isGzipContent(HttpConnection c) {
  791. return ENCODING_GZIP.equals(c.getHeaderField(HDR_CONTENT_ENCODING))
  792. || ENCODING_X_GZIP.equals(c.getHeaderField(HDR_CONTENT_ENCODING));
  793. }
  794. private void readSmartHeaders(InputStream in, String service)
  795. throws IOException {
  796. // A smart reply will have a '#' after the first 4 bytes, but
  797. // a dumb reply cannot contain a '#' until after byte 41. Do a
  798. // quick check to make sure its a smart reply before we parse
  799. // as a pkt-line stream.
  800. //
  801. final byte[] magic = new byte[5];
  802. IO.readFully(in, magic, 0, magic.length);
  803. if (magic[4] != '#') {
  804. throw new TransportException(uri, MessageFormat.format(
  805. JGitText.get().expectedPktLineWithService, RawParseUtils.decode(magic)));
  806. }
  807. final PacketLineIn pckIn = new PacketLineIn(new UnionInputStream(
  808. new ByteArrayInputStream(magic), in));
  809. final String exp = "# service=" + service; //$NON-NLS-1$
  810. final String act = pckIn.readString();
  811. if (!exp.equals(act)) {
  812. throw new TransportException(uri, MessageFormat.format(
  813. JGitText.get().expectedGot, exp, act));
  814. }
  815. while (pckIn.readString() != PacketLineIn.END) {
  816. // for now, ignore the remaining header lines
  817. }
  818. }
  819. class HttpObjectDB extends WalkRemoteObjectDatabase {
  820. private final URL httpObjectsUrl;
  821. HttpObjectDB(URL b) {
  822. httpObjectsUrl = b;
  823. }
  824. @Override
  825. URIish getURI() {
  826. return new URIish(httpObjectsUrl);
  827. }
  828. @Override
  829. Collection<WalkRemoteObjectDatabase> getAlternates() throws IOException {
  830. try {
  831. return readAlternates(INFO_HTTP_ALTERNATES);
  832. } catch (FileNotFoundException err) {
  833. // Fall through.
  834. }
  835. try {
  836. return readAlternates(INFO_ALTERNATES);
  837. } catch (FileNotFoundException err) {
  838. // Fall through.
  839. }
  840. return null;
  841. }
  842. @Override
  843. WalkRemoteObjectDatabase openAlternate(String location)
  844. throws IOException {
  845. return new HttpObjectDB(new URL(httpObjectsUrl, location));
  846. }
  847. @Override
  848. BufferedReader openReader(String path) throws IOException {
  849. // Line oriented readable content is likely to compress well.
  850. // Request gzip encoding.
  851. InputStream is = open(path, AcceptEncoding.GZIP).in;
  852. return new BufferedReader(new InputStreamReader(is, UTF_8));
  853. }
  854. @Override
  855. Collection<String> getPackNames() throws IOException {
  856. final Collection<String> packs = new ArrayList<>();
  857. try (BufferedReader br = openReader(INFO_PACKS)) {
  858. for (;;) {
  859. final String s = br.readLine();
  860. if (s == null || s.length() == 0)
  861. break;
  862. if (!s.startsWith("P pack-") || !s.endsWith(".pack")) //$NON-NLS-1$ //$NON-NLS-2$
  863. throw invalidAdvertisement(s);
  864. packs.add(s.substring(2));
  865. }
  866. return packs;
  867. } catch (FileNotFoundException err) {
  868. return packs;
  869. }
  870. }
  871. @Override
  872. FileStream open(String path) throws IOException {
  873. return open(path, AcceptEncoding.UNSPECIFIED);
  874. }
  875. FileStream open(String path, AcceptEncoding acceptEncoding)
  876. throws IOException {
  877. final URL base = httpObjectsUrl;
  878. final URL u = new URL(base, path);
  879. final HttpConnection c = httpOpen(METHOD_GET, u, acceptEncoding);
  880. switch (HttpSupport.response(c)) {
  881. case HttpConnection.HTTP_OK:
  882. final InputStream in = openInputStream(c);
  883. // If content is being gzipped and then transferred, the content
  884. // length in the header is the zipped content length, not the
  885. // actual content length.
  886. if (!isGzipContent(c)) {
  887. final int len = c.getContentLength();
  888. return new FileStream(in, len);
  889. }
  890. return new FileStream(in);
  891. case HttpConnection.HTTP_NOT_FOUND:
  892. throw new FileNotFoundException(u.toString());
  893. default:
  894. throw new IOException(u.toString() + ": " //$NON-NLS-1$
  895. + HttpSupport.response(c) + " " //$NON-NLS-1$
  896. + c.getResponseMessage());
  897. }
  898. }
  899. Map<String, Ref> readAdvertisedImpl(final BufferedReader br)
  900. throws IOException, PackProtocolException {
  901. final TreeMap<String, Ref> avail = new TreeMap<>();
  902. for (;;) {
  903. String line = br.readLine();
  904. if (line == null)
  905. break;
  906. final int tab = line.indexOf('\t');
  907. if (tab < 0)
  908. throw invalidAdvertisement(line);
  909. String name;
  910. final ObjectId id;
  911. name = line.substring(tab + 1);
  912. id = ObjectId.fromString(line.substring(0, tab));
  913. if (name.endsWith("^{}")) { //$NON-NLS-1$
  914. name = name.substring(0, name.length() - 3);
  915. final Ref prior = avail.get(name);
  916. if (prior == null)
  917. throw outOfOrderAdvertisement(name);
  918. if (prior.getPeeledObjectId() != null)
  919. throw duplicateAdvertisement(name + "^{}"); //$NON-NLS-1$
  920. avail.put(name, new ObjectIdRef.PeeledTag(
  921. Ref.Storage.NETWORK, name,
  922. prior.getObjectId(), id));
  923. } else {
  924. Ref prior = avail.put(name, new ObjectIdRef.PeeledNonTag(
  925. Ref.Storage.NETWORK, name, id));
  926. if (prior != null)
  927. throw duplicateAdvertisement(name);
  928. }
  929. }
  930. return avail;
  931. }
  932. private PackProtocolException outOfOrderAdvertisement(String n) {
  933. return new PackProtocolException(MessageFormat.format(JGitText.get().advertisementOfCameBefore, n, n));
  934. }
  935. private PackProtocolException invalidAdvertisement(String n) {
  936. return new PackProtocolException(MessageFormat.format(JGitText.get().invalidAdvertisementOf, n));
  937. }
  938. private PackProtocolException duplicateAdvertisement(String n) {
  939. return new PackProtocolException(MessageFormat.format(JGitText.get().duplicateAdvertisementsOf, n));
  940. }
  941. @Override
  942. void close() {
  943. // We do not maintain persistent connections.
  944. }
  945. }
  946. class SmartHttpFetchConnection extends BasePackFetchConnection {
  947. private MultiRequestService svc;
  948. SmartHttpFetchConnection(InputStream advertisement)
  949. throws TransportException {
  950. super(TransportHttp.this);
  951. statelessRPC = true;
  952. init(advertisement, DisabledOutputStream.INSTANCE);
  953. outNeedsEnd = false;
  954. readAdvertisedRefs();
  955. }
  956. @Override
  957. protected void doFetch(final ProgressMonitor monitor,
  958. final Collection<Ref> want, final Set<ObjectId> have,
  959. final OutputStream outputStream) throws TransportException {
  960. try {
  961. svc = new MultiRequestService(SVC_UPLOAD_PACK);
  962. init(svc.getInputStream(), svc.getOutputStream());
  963. super.doFetch(monitor, want, have, outputStream);
  964. } finally {
  965. svc = null;
  966. }
  967. }
  968. @Override
  969. protected void onReceivePack() {
  970. svc.finalRequest = true;
  971. }
  972. }
  973. class SmartHttpPushConnection extends BasePackPushConnection {
  974. SmartHttpPushConnection(InputStream advertisement)
  975. throws TransportException {
  976. super(TransportHttp.this);
  977. statelessRPC = true;
  978. init(advertisement, DisabledOutputStream.INSTANCE);
  979. outNeedsEnd = false;
  980. readAdvertisedRefs();
  981. }
  982. @Override
  983. protected void doPush(final ProgressMonitor monitor,
  984. final Map<String, RemoteRefUpdate> refUpdates,
  985. OutputStream outputStream) throws TransportException {
  986. final Service svc = new MultiRequestService(SVC_RECEIVE_PACK);
  987. init(svc.getInputStream(), svc.getOutputStream());
  988. super.doPush(monitor, refUpdates, outputStream);
  989. }
  990. }
  991. /** Basic service for sending and receiving HTTP requests. */
  992. abstract class Service {
  993. protected final String serviceName;
  994. protected final String requestType;
  995. protected final String responseType;
  996. protected HttpConnection conn;
  997. protected HttpOutputStream out;
  998. protected final HttpExecuteStream execute;
  999. final UnionInputStream in;
  1000. Service(String serviceName) {
  1001. this.serviceName = serviceName;
  1002. this.requestType = "application/x-" + serviceName + "-request"; //$NON-NLS-1$ //$NON-NLS-2$
  1003. this.responseType = "application/x-" + serviceName + "-result"; //$NON-NLS-1$ //$NON-NLS-2$
  1004. this.out = new HttpOutputStream();
  1005. this.execute = new HttpExecuteStream();
  1006. this.in = new UnionInputStream(execute);
  1007. }
  1008. void openStream() throws IOException {
  1009. conn = httpOpen(METHOD_POST, new URL(baseUrl, serviceName),
  1010. AcceptEncoding.GZIP);
  1011. conn.setInstanceFollowRedirects(false);
  1012. conn.setDoOutput(true);
  1013. conn.setRequestProperty(HDR_CONTENT_TYPE, requestType);
  1014. conn.setRequestProperty(HDR_ACCEPT, responseType);
  1015. }
  1016. void sendRequest() throws IOException {
  1017. // Try to compress the content, but only if that is smaller.
  1018. TemporaryBuffer buf = new TemporaryBuffer.Heap(
  1019. http.getPostBuffer());
  1020. try (GZIPOutputStream gzip = new GZIPOutputStream(buf)) {
  1021. out.writeTo(gzip, null);
  1022. if (out.length() < buf.length())
  1023. buf = out;
  1024. } catch (IOException err) {
  1025. // Most likely caused by overflowing the buffer, meaning
  1026. // its larger if it were compressed. Don't compress.
  1027. buf = out;
  1028. }
  1029. HttpAuthMethod authenticator = null;
  1030. Collection<Type> ignoreTypes = EnumSet.noneOf(Type.class);
  1031. // Counts number of repeated authentication attempts using the same
  1032. // authentication scheme
  1033. int authAttempts = 1;
  1034. int redirects = 0;
  1035. for (;;) {
  1036. try {
  1037. // The very first time we will try with the authentication
  1038. // method used on the initial GET request. This is a hint
  1039. // only; it may fail. If so, we'll then re-try with proper
  1040. // 401 handling, going through the available authentication
  1041. // schemes.
  1042. openStream();
  1043. if (buf != out) {
  1044. conn.setRequestProperty(HDR_CONTENT_ENCODING,
  1045. ENCODING_GZIP);
  1046. }
  1047. conn.setFixedLengthStreamingMode((int) buf.length());
  1048. try (OutputStream httpOut = conn.getOutputStream()) {
  1049. buf.writeTo(httpOut, null);
  1050. }
  1051. final int status = HttpSupport.response(conn);
  1052. switch (status) {
  1053. case HttpConnection.HTTP_OK:
  1054. // We're done.
  1055. return;
  1056. case HttpConnection.HTTP_NOT_FOUND:
  1057. throw createNotFoundException(uri, conn.getURL(),
  1058. conn.getResponseMessage());
  1059. case HttpConnection.HTTP_FORBIDDEN:
  1060. throw new TransportException(uri,
  1061. MessageFormat.format(
  1062. JGitText.get().serviceNotPermitted,
  1063. baseUrl, serviceName));
  1064. case HttpConnection.HTTP_MOVED_PERM:
  1065. case HttpConnection.HTTP_MOVED_TEMP:
  1066. case HttpConnection.HTTP_11_MOVED_TEMP:
  1067. // SEE_OTHER after a POST doesn't make sense for a git
  1068. // server, so we don't handle it here and thus we'll
  1069. // report an error in openResponse() later on.
  1070. if (http.getFollowRedirects() != HttpRedirectMode.TRUE) {
  1071. // Let openResponse() issue an error
  1072. return;
  1073. }
  1074. currentUri = redirect(conn.getHeaderField(HDR_LOCATION),
  1075. '/' + serviceName, redirects++);
  1076. try {
  1077. baseUrl = toURL(currentUri);
  1078. } catch (MalformedURLException e) {
  1079. throw new TransportException(uri,
  1080. MessageFormat.format(
  1081. JGitText.get().invalidRedirectLocation,
  1082. baseUrl, currentUri),
  1083. e);
  1084. }
  1085. continue;
  1086. case HttpConnection.HTTP_UNAUTHORIZED:
  1087. HttpAuthMethod nextMethod = HttpAuthMethod
  1088. .scanResponse(conn, ignoreTypes);
  1089. switch (nextMethod.getType()) {
  1090. case NONE:
  1091. throw new TransportException(uri,
  1092. MessageFormat.format(
  1093. JGitText.get().authenticationNotSupported,
  1094. conn.getURL()));
  1095. case NEGOTIATE:
  1096. // RFC 4559 states "When using the SPNEGO [...] with
  1097. // [...] POST, the authentication should be complete
  1098. // [...] before sending the user data." So in theory
  1099. // the initial GET should have been authenticated
  1100. // already. (Unless there was a redirect?)
  1101. //
  1102. // We try this only once:
  1103. ignoreTypes.add(HttpAuthMethod.Type.NEGOTIATE);
  1104. if (authenticator != null) {
  1105. ignoreTypes.add(authenticator.getType());
  1106. }
  1107. authAttempts = 1;
  1108. // We only do the Kerberos part of SPNEGO, which
  1109. // requires only one round.
  1110. break;
  1111. default:
  1112. // DIGEST or BASIC. Let's be sure we ignore
  1113. // NEGOTIATE; if it was available, we have tried it
  1114. // before.
  1115. ignoreTypes.add(HttpAuthMethod.Type.NEGOTIATE);
  1116. if (authenticator == null || authenticator
  1117. .getType() != nextMethod.getType()) {
  1118. if (authenticator != null) {
  1119. ignoreTypes.add(authenticator.getType());
  1120. }
  1121. authAttempts = 1;
  1122. }
  1123. break;
  1124. }
  1125. authMethod = nextMethod;
  1126. authenticator = nextMethod;
  1127. CredentialsProvider credentialsProvider = getCredentialsProvider();
  1128. if (credentialsProvider == null) {
  1129. throw new TransportException(uri,
  1130. JGitText.get().noCredentialsProvider);
  1131. }
  1132. if (authAttempts > 1) {
  1133. credentialsProvider.reset(currentUri);
  1134. }
  1135. if (3 < authAttempts || !authMethod
  1136. .authorize(currentUri, credentialsProvider)) {
  1137. throw new TransportException(uri,
  1138. JGitText.get().notAuthorized);
  1139. }
  1140. authAttempts++;
  1141. continue;
  1142. default:
  1143. // Just return here; openResponse() will report an
  1144. // appropriate error.
  1145. return;
  1146. }
  1147. } catch (SSLHandshakeException e) {
  1148. handleSslFailure(e);
  1149. continue; // Re-try
  1150. } catch (IOException e) {
  1151. if (authenticator == null || authMethod
  1152. .getType() != HttpAuthMethod.Type.NONE) {
  1153. // Can happen for instance if the server advertises
  1154. // Negotiate, but the client isn't configured for
  1155. // Kerberos. The first time (authenticator == null) we
  1156. // must re-try even if the authMethod was NONE: this may
  1157. // occur if the server advertised NTLM on the GET
  1158. // and the HttpConnection managed to successfully
  1159. // authenticate under the hood with NTLM. We might not
  1160. // have picked this up on the GET's 200 response.
  1161. if (authMethod.getType() != HttpAuthMethod.Type.NONE) {
  1162. ignoreTypes.add(authMethod.getType());
  1163. }
  1164. // Start over with the remaining available methods.
  1165. authMethod = HttpAuthMethod.Type.NONE.method(null);
  1166. authenticator = authMethod;
  1167. authAttempts = 1;
  1168. continue;
  1169. }
  1170. throw e;
  1171. }
  1172. }
  1173. }
  1174. void openResponse() throws IOException {
  1175. final int status = HttpSupport.response(conn);
  1176. if (status != HttpConnection.HTTP_OK) {
  1177. throw new TransportException(uri, status + " " //$NON-NLS-1$
  1178. + conn.getResponseMessage());
  1179. }
  1180. final String contentType = conn.getContentType();
  1181. if (!responseType.equals(contentType)) {
  1182. conn.getInputStream().close();
  1183. throw wrongContentType(responseType, contentType);
  1184. }
  1185. }
  1186. HttpOutputStream getOutputStream() {
  1187. return out;
  1188. }
  1189. InputStream getInputStream() {
  1190. return in;
  1191. }
  1192. abstract void execute() throws IOException;
  1193. class HttpExecuteStream extends InputStream {
  1194. @Override
  1195. public int read() throws IOException {
  1196. execute();
  1197. return -1;
  1198. }
  1199. @Override
  1200. public int read(byte[] b, int off, int len) throws IOException {
  1201. execute();
  1202. return -1;
  1203. }
  1204. @Override
  1205. public long skip(long n) throws IOException {
  1206. execute();
  1207. return 0;
  1208. }
  1209. }
  1210. class HttpOutputStream extends TemporaryBuffer {
  1211. HttpOutputStream() {
  1212. super(http.getPostBuffer());
  1213. }
  1214. @Override
  1215. protected OutputStream overflow() throws IOException {
  1216. openStream();
  1217. conn.setChunkedStreamingMode(0);
  1218. return conn.getOutputStream();
  1219. }
  1220. }
  1221. }
  1222. /**
  1223. * State required to speak multiple HTTP requests with the remote.
  1224. * <p>
  1225. * A service wrapper provides a normal looking InputStream and OutputStream
  1226. * pair which are connected via HTTP to the named remote service. Writing to
  1227. * the OutputStream is buffered until either the buffer overflows, or
  1228. * reading from the InputStream occurs. If overflow occurs HTTP/1.1 and its
  1229. * chunked transfer encoding is used to stream the request data to the
  1230. * remote service. If the entire request fits in the memory buffer, the
  1231. * older HTTP/1.0 standard and a fixed content length is used instead.
  1232. * <p>
  1233. * It is an error to attempt to read without there being outstanding data
  1234. * ready for transmission on the OutputStream.
  1235. * <p>
  1236. * No state is preserved between write-read request pairs. The caller is
  1237. * responsible for replaying state vector information as part of the request
  1238. * data written to the OutputStream. Any session HTTP cookies may or may not
  1239. * be preserved between requests, it is left up to the JVM's implementation
  1240. * of the HTTP client.
  1241. */
  1242. class MultiRequestService extends Service {
  1243. boolean finalRequest;
  1244. MultiRequestService(String serviceName) {
  1245. super(serviceName);
  1246. }
  1247. /** Keep opening send-receive pairs to the given URI. */
  1248. @Override
  1249. void execute() throws IOException {
  1250. out.close();
  1251. if (conn == null) {
  1252. if (out.length() == 0) {
  1253. // Request output hasn't started yet, but more data is being
  1254. // requested. If there is no request data buffered and the
  1255. // final request was already sent, do nothing to ensure the
  1256. // caller is shown EOF on the InputStream; otherwise an
  1257. // programming error has occurred within this module.
  1258. if (finalRequest)
  1259. return;
  1260. throw new TransportException(uri,
  1261. JGitText.get().startingReadStageWithoutWrittenRequestDataPendingIsNotSupported);
  1262. }
  1263. sendRequest();
  1264. }
  1265. out.reset();
  1266. openResponse();
  1267. in.add(openInputStream(conn));
  1268. if (!finalRequest)
  1269. in.add(execute);
  1270. conn = null;
  1271. }
  1272. }
  1273. /** Service for maintaining a single long-poll connection. */
  1274. class LongPollService extends Service {
  1275. /**
  1276. * @param serviceName
  1277. */
  1278. LongPollService(String serviceName) {
  1279. super(serviceName);
  1280. }
  1281. /** Only open one send-receive request. */
  1282. @Override
  1283. void execute() throws IOException {
  1284. out.close();
  1285. if (conn == null)
  1286. sendRequest();
  1287. openResponse();
  1288. in.add(openInputStream(conn));
  1289. }
  1290. }
  1291. }