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 54KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721
  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> and others
  6. *
  7. * This program and the accompanying materials are made available under the
  8. * terms of the Eclipse Distribution License v. 1.0 which is available at
  9. * https://www.eclipse.org/org/documents/edl-v10.php.
  10. *
  11. * SPDX-License-Identifier: BSD-3-Clause
  12. */
  13. package org.eclipse.jgit.transport;
  14. import static java.nio.charset.StandardCharsets.UTF_8;
  15. import static org.eclipse.jgit.lib.Constants.HEAD;
  16. import static org.eclipse.jgit.lib.Constants.INFO_ALTERNATES;
  17. import static org.eclipse.jgit.lib.Constants.INFO_HTTP_ALTERNATES;
  18. import static org.eclipse.jgit.util.HttpSupport.ENCODING_GZIP;
  19. import static org.eclipse.jgit.util.HttpSupport.ENCODING_X_GZIP;
  20. import static org.eclipse.jgit.util.HttpSupport.HDR_ACCEPT;
  21. import static org.eclipse.jgit.util.HttpSupport.HDR_ACCEPT_ENCODING;
  22. import static org.eclipse.jgit.util.HttpSupport.HDR_CONTENT_ENCODING;
  23. import static org.eclipse.jgit.util.HttpSupport.HDR_CONTENT_TYPE;
  24. import static org.eclipse.jgit.util.HttpSupport.HDR_COOKIE;
  25. import static org.eclipse.jgit.util.HttpSupport.HDR_LOCATION;
  26. import static org.eclipse.jgit.util.HttpSupport.HDR_PRAGMA;
  27. import static org.eclipse.jgit.util.HttpSupport.HDR_SET_COOKIE;
  28. import static org.eclipse.jgit.util.HttpSupport.HDR_SET_COOKIE2;
  29. import static org.eclipse.jgit.util.HttpSupport.HDR_USER_AGENT;
  30. import static org.eclipse.jgit.util.HttpSupport.HDR_WWW_AUTHENTICATE;
  31. import static org.eclipse.jgit.util.HttpSupport.METHOD_GET;
  32. import static org.eclipse.jgit.util.HttpSupport.METHOD_POST;
  33. import java.io.BufferedReader;
  34. import java.io.ByteArrayInputStream;
  35. import java.io.FileNotFoundException;
  36. import java.io.IOException;
  37. import java.io.InputStream;
  38. import java.io.InputStreamReader;
  39. import java.io.InterruptedIOException;
  40. import java.io.OutputStream;
  41. import java.net.HttpCookie;
  42. import java.net.MalformedURLException;
  43. import java.net.Proxy;
  44. import java.net.ProxySelector;
  45. import java.net.SocketException;
  46. import java.net.URI;
  47. import java.net.URISyntaxException;
  48. import java.net.URL;
  49. import java.nio.charset.StandardCharsets;
  50. import java.nio.file.InvalidPathException;
  51. import java.nio.file.Path;
  52. import java.nio.file.Paths;
  53. import java.security.cert.CertPathBuilderException;
  54. import java.security.cert.CertPathValidatorException;
  55. import java.security.cert.CertificateException;
  56. import java.text.MessageFormat;
  57. import java.util.ArrayList;
  58. import java.util.Arrays;
  59. import java.util.Collection;
  60. import java.util.Collections;
  61. import java.util.EnumSet;
  62. import java.util.HashSet;
  63. import java.util.LinkedHashSet;
  64. import java.util.LinkedList;
  65. import java.util.List;
  66. import java.util.Locale;
  67. import java.util.Map;
  68. import java.util.Set;
  69. import java.util.TreeMap;
  70. import java.util.zip.GZIPInputStream;
  71. import java.util.zip.GZIPOutputStream;
  72. import javax.net.ssl.SSLHandshakeException;
  73. import org.eclipse.jgit.errors.ConfigInvalidException;
  74. import org.eclipse.jgit.errors.NoRemoteRepositoryException;
  75. import org.eclipse.jgit.errors.NotSupportedException;
  76. import org.eclipse.jgit.errors.PackProtocolException;
  77. import org.eclipse.jgit.errors.TransportException;
  78. import org.eclipse.jgit.internal.JGitText;
  79. import org.eclipse.jgit.internal.storage.file.RefDirectory;
  80. import org.eclipse.jgit.internal.transport.http.NetscapeCookieFile;
  81. import org.eclipse.jgit.internal.transport.http.NetscapeCookieFileCache;
  82. import org.eclipse.jgit.lib.Constants;
  83. import org.eclipse.jgit.lib.ObjectId;
  84. import org.eclipse.jgit.lib.ObjectIdRef;
  85. import org.eclipse.jgit.lib.ProgressMonitor;
  86. import org.eclipse.jgit.lib.Ref;
  87. import org.eclipse.jgit.lib.Repository;
  88. import org.eclipse.jgit.lib.StoredConfig;
  89. import org.eclipse.jgit.lib.SymbolicRef;
  90. import org.eclipse.jgit.transport.HttpAuthMethod.Type;
  91. import org.eclipse.jgit.transport.HttpConfig.HttpRedirectMode;
  92. import org.eclipse.jgit.transport.http.HttpConnection;
  93. import org.eclipse.jgit.util.HttpSupport;
  94. import org.eclipse.jgit.util.IO;
  95. import org.eclipse.jgit.util.RawParseUtils;
  96. import org.eclipse.jgit.util.StringUtils;
  97. import org.eclipse.jgit.util.SystemReader;
  98. import org.eclipse.jgit.util.TemporaryBuffer;
  99. import org.eclipse.jgit.util.io.DisabledOutputStream;
  100. import org.eclipse.jgit.util.io.UnionInputStream;
  101. import org.slf4j.Logger;
  102. import org.slf4j.LoggerFactory;
  103. /**
  104. * Transport over HTTP and FTP protocols.
  105. * <p>
  106. * If the transport is using HTTP and the remote HTTP service is Git-aware
  107. * (speaks the "smart-http protocol") this client will automatically take
  108. * advantage of the additional Git-specific HTTP extensions. If the remote
  109. * service does not support these extensions, the client will degrade to direct
  110. * file fetching.
  111. * <p>
  112. * If the remote (server side) repository does not have the specialized Git
  113. * support, object files are retrieved directly through standard HTTP GET (or
  114. * binary FTP GET) requests. This make it easy to serve a Git repository through
  115. * a standard web host provider that does not offer specific support for Git.
  116. *
  117. * @see WalkFetchConnection
  118. */
  119. public class TransportHttp extends HttpTransport implements WalkTransport,
  120. PackTransport {
  121. private static final Logger LOG = LoggerFactory
  122. .getLogger(TransportHttp.class);
  123. private static final String SVC_UPLOAD_PACK = "git-upload-pack"; //$NON-NLS-1$
  124. private static final String SVC_RECEIVE_PACK = "git-receive-pack"; //$NON-NLS-1$
  125. /**
  126. * Accept-Encoding header in the HTTP request
  127. * (https://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html).
  128. *
  129. * @since 4.6
  130. */
  131. public enum AcceptEncoding {
  132. /**
  133. * Do not specify an Accept-Encoding header. In most servers this
  134. * results in the content being transmitted as-is.
  135. */
  136. UNSPECIFIED,
  137. /**
  138. * Accept gzip content encoding.
  139. */
  140. GZIP
  141. }
  142. static final TransportProtocol PROTO_HTTP = new TransportProtocol() {
  143. private final String[] schemeNames = { "http", "https" }; //$NON-NLS-1$ //$NON-NLS-2$
  144. private final Set<String> schemeSet = Collections
  145. .unmodifiableSet(new LinkedHashSet<>(Arrays
  146. .asList(schemeNames)));
  147. @Override
  148. public String getName() {
  149. return JGitText.get().transportProtoHTTP;
  150. }
  151. @Override
  152. public Set<String> getSchemes() {
  153. return schemeSet;
  154. }
  155. @Override
  156. public Set<URIishField> getRequiredFields() {
  157. return Collections.unmodifiableSet(EnumSet.of(URIishField.HOST,
  158. URIishField.PATH));
  159. }
  160. @Override
  161. public Set<URIishField> getOptionalFields() {
  162. return Collections.unmodifiableSet(EnumSet.of(URIishField.USER,
  163. URIishField.PASS, URIishField.PORT));
  164. }
  165. @Override
  166. public int getDefaultPort() {
  167. return 80;
  168. }
  169. @Override
  170. public Transport open(URIish uri, Repository local, String remoteName)
  171. throws NotSupportedException {
  172. return new TransportHttp(local, uri);
  173. }
  174. @Override
  175. public Transport open(URIish uri) throws NotSupportedException {
  176. return new TransportHttp(uri);
  177. }
  178. };
  179. static final TransportProtocol PROTO_FTP = new TransportProtocol() {
  180. @Override
  181. public String getName() {
  182. return JGitText.get().transportProtoFTP;
  183. }
  184. @Override
  185. public Set<String> getSchemes() {
  186. return Collections.singleton("ftp"); //$NON-NLS-1$
  187. }
  188. @Override
  189. public Set<URIishField> getRequiredFields() {
  190. return Collections.unmodifiableSet(EnumSet.of(URIishField.HOST,
  191. URIishField.PATH));
  192. }
  193. @Override
  194. public Set<URIishField> getOptionalFields() {
  195. return Collections.unmodifiableSet(EnumSet.of(URIishField.USER,
  196. URIishField.PASS, URIishField.PORT));
  197. }
  198. @Override
  199. public int getDefaultPort() {
  200. return 21;
  201. }
  202. @Override
  203. public Transport open(URIish uri, Repository local, String remoteName)
  204. throws NotSupportedException {
  205. return new TransportHttp(local, uri);
  206. }
  207. };
  208. /**
  209. * The current URI we're talking to. The inherited (final) field
  210. * {@link #uri} stores the original URI; {@code currentUri} may be different
  211. * after redirects.
  212. */
  213. private URIish currentUri;
  214. private URL baseUrl;
  215. private URL objectsUrl;
  216. private final HttpConfig http;
  217. private final ProxySelector proxySelector;
  218. private boolean useSmartHttp = true;
  219. private HttpAuthMethod authMethod = HttpAuthMethod.Type.NONE.method(null);
  220. private Map<String, String> headers;
  221. private boolean sslVerify;
  222. private boolean sslFailure = false;
  223. /**
  224. * All stored cookies bound to this repo (independent of the baseUrl)
  225. */
  226. private final NetscapeCookieFile cookieFile;
  227. /**
  228. * The cookies to be sent with each request to the given {@link #baseUrl}.
  229. * Filtered view on top of {@link #cookieFile} where only cookies which
  230. * apply to the current url are left. This set needs to be filtered for
  231. * expired entries each time prior to sending them.
  232. */
  233. private final Set<HttpCookie> relevantCookies;
  234. TransportHttp(Repository local, URIish uri)
  235. throws NotSupportedException {
  236. super(local, uri);
  237. setURI(uri);
  238. http = new HttpConfig(local.getConfig(), uri);
  239. proxySelector = ProxySelector.getDefault();
  240. sslVerify = http.isSslVerify();
  241. cookieFile = getCookieFileFromConfig(http);
  242. relevantCookies = filterCookies(cookieFile, baseUrl);
  243. }
  244. private URL toURL(URIish urish) throws MalformedURLException {
  245. String uriString = urish.toString();
  246. if (!uriString.endsWith("/")) { //$NON-NLS-1$
  247. uriString += '/';
  248. }
  249. return new URL(uriString);
  250. }
  251. /**
  252. * Set uri a {@link org.eclipse.jgit.transport.URIish} object.
  253. *
  254. * @param uri
  255. * a {@link org.eclipse.jgit.transport.URIish} object.
  256. * @throws org.eclipse.jgit.errors.NotSupportedException
  257. * @since 4.9
  258. */
  259. protected void setURI(URIish uri) throws NotSupportedException {
  260. try {
  261. currentUri = uri;
  262. baseUrl = toURL(uri);
  263. objectsUrl = new URL(baseUrl, "objects/"); //$NON-NLS-1$
  264. } catch (MalformedURLException e) {
  265. throw new NotSupportedException(MessageFormat.format(JGitText.get().invalidURL, uri), e);
  266. }
  267. }
  268. /**
  269. * Create a minimal HTTP transport with default configuration values.
  270. *
  271. * @param uri
  272. * @throws NotSupportedException
  273. */
  274. TransportHttp(URIish uri) throws NotSupportedException {
  275. super(uri);
  276. setURI(uri);
  277. http = new HttpConfig(uri);
  278. proxySelector = ProxySelector.getDefault();
  279. sslVerify = http.isSslVerify();
  280. cookieFile = getCookieFileFromConfig(http);
  281. relevantCookies = filterCookies(cookieFile, baseUrl);
  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 | TransportException err) {
  322. throw err;
  323. } catch (IOException err) {
  324. throw new TransportException(uri, JGitText.get().errorReadingInfoRefs, err);
  325. }
  326. }
  327. private WalkFetchConnection newDumbConnection(InputStream in)
  328. throws IOException, PackProtocolException {
  329. HttpObjectDB d = new HttpObjectDB(objectsUrl);
  330. Map<String, Ref> refs;
  331. try (BufferedReader br = toBufferedReader(in)) {
  332. refs = d.readAdvertisedImpl(br);
  333. }
  334. if (!refs.containsKey(HEAD)) {
  335. // If HEAD was not published in the info/refs file (it usually
  336. // is not there) download HEAD by itself as a loose file and do
  337. // the resolution by hand.
  338. //
  339. HttpConnection conn = httpOpen(
  340. METHOD_GET,
  341. new URL(baseUrl, HEAD),
  342. AcceptEncoding.GZIP);
  343. int status = HttpSupport.response(conn);
  344. switch (status) {
  345. case HttpConnection.HTTP_OK: {
  346. try (BufferedReader br = toBufferedReader(
  347. openInputStream(conn))) {
  348. String line = br.readLine();
  349. if (line != null && line.startsWith(RefDirectory.SYMREF)) {
  350. String target = line.substring(RefDirectory.SYMREF.length());
  351. Ref r = refs.get(target);
  352. if (r == null)
  353. r = new ObjectIdRef.Unpeeled(Ref.Storage.NEW, target, null);
  354. r = new SymbolicRef(HEAD, r);
  355. refs.put(r.getName(), r);
  356. } else if (line != null && ObjectId.isId(line)) {
  357. Ref r = new ObjectIdRef.Unpeeled(Ref.Storage.NETWORK,
  358. HEAD, ObjectId.fromString(line));
  359. refs.put(r.getName(), r);
  360. }
  361. }
  362. break;
  363. }
  364. case HttpConnection.HTTP_NOT_FOUND:
  365. break;
  366. default:
  367. throw new TransportException(uri, MessageFormat.format(
  368. JGitText.get().cannotReadHEAD, Integer.valueOf(status),
  369. conn.getResponseMessage()));
  370. }
  371. }
  372. WalkFetchConnection wfc = new WalkFetchConnection(this, d);
  373. wfc.available(refs);
  374. return wfc;
  375. }
  376. private BufferedReader toBufferedReader(InputStream in) {
  377. return new BufferedReader(new InputStreamReader(in, UTF_8));
  378. }
  379. /** {@inheritDoc} */
  380. @Override
  381. public PushConnection openPush() throws NotSupportedException,
  382. TransportException {
  383. final String service = SVC_RECEIVE_PACK;
  384. try {
  385. final HttpConnection c = connect(service);
  386. try (InputStream in = openInputStream(c)) {
  387. if (isSmartHttp(c, service)) {
  388. return smartPush(service, c, in);
  389. } else if (!useSmartHttp) {
  390. final String msg = JGitText.get().smartHTTPPushDisabled;
  391. throw new NotSupportedException(msg);
  392. } else {
  393. final String msg = JGitText.get().remoteDoesNotSupportSmartHTTPPush;
  394. throw new NotSupportedException(msg);
  395. }
  396. }
  397. } catch (NotSupportedException | TransportException err) {
  398. throw err;
  399. } catch (IOException err) {
  400. throw new TransportException(uri, JGitText.get().errorReadingInfoRefs, err);
  401. }
  402. }
  403. private PushConnection smartPush(String service, HttpConnection c,
  404. InputStream in) throws IOException, TransportException {
  405. readSmartHeaders(in, service);
  406. SmartHttpPushConnection p = new SmartHttpPushConnection(in);
  407. p.setPeerUserAgent(c.getHeaderField(HttpSupport.HDR_SERVER));
  408. return p;
  409. }
  410. /** {@inheritDoc} */
  411. @Override
  412. public void close() {
  413. // No explicit connections are maintained.
  414. }
  415. /**
  416. * Set additional headers on the HTTP connection
  417. *
  418. * @param headers
  419. * a map of name:values that are to be set as headers on the HTTP
  420. * connection
  421. * @since 3.4
  422. */
  423. public void setAdditionalHeaders(Map<String, String> headers) {
  424. this.headers = headers;
  425. }
  426. private NoRemoteRepositoryException createNotFoundException(URIish u,
  427. URL url, String msg) {
  428. String text;
  429. if (msg != null && !msg.isEmpty()) {
  430. text = MessageFormat.format(JGitText.get().uriNotFoundWithMessage,
  431. url, msg);
  432. } else {
  433. text = MessageFormat.format(JGitText.get().uriNotFound, url);
  434. }
  435. return new NoRemoteRepositoryException(u, text);
  436. }
  437. private HttpConnection connect(String service)
  438. throws TransportException, NotSupportedException {
  439. URL u = getServiceURL(service);
  440. int authAttempts = 1;
  441. int redirects = 0;
  442. Collection<Type> ignoreTypes = null;
  443. for (;;) {
  444. try {
  445. final HttpConnection conn = httpOpen(METHOD_GET, u, AcceptEncoding.GZIP);
  446. if (useSmartHttp) {
  447. String exp = "application/x-" + service + "-advertisement"; //$NON-NLS-1$ //$NON-NLS-2$
  448. conn.setRequestProperty(HDR_ACCEPT, exp + ", */*"); //$NON-NLS-1$
  449. } else {
  450. conn.setRequestProperty(HDR_ACCEPT, "*/*"); //$NON-NLS-1$
  451. }
  452. final int status = HttpSupport.response(conn);
  453. processResponseCookies(conn);
  454. switch (status) {
  455. case HttpConnection.HTTP_OK:
  456. // Check if HttpConnection did some authentication in the
  457. // background (e.g Kerberos/SPNEGO).
  458. // That may not work for streaming requests and jgit
  459. // explicit authentication would be required
  460. if (authMethod.getType() == HttpAuthMethod.Type.NONE
  461. && conn.getHeaderField(HDR_WWW_AUTHENTICATE) != null)
  462. authMethod = HttpAuthMethod.scanResponse(conn, ignoreTypes);
  463. return conn;
  464. case HttpConnection.HTTP_NOT_FOUND:
  465. throw createNotFoundException(uri, u,
  466. conn.getResponseMessage());
  467. case HttpConnection.HTTP_UNAUTHORIZED:
  468. authMethod = HttpAuthMethod.scanResponse(conn, ignoreTypes);
  469. if (authMethod.getType() == HttpAuthMethod.Type.NONE)
  470. throw new TransportException(uri, MessageFormat.format(
  471. JGitText.get().authenticationNotSupported, uri));
  472. CredentialsProvider credentialsProvider = getCredentialsProvider();
  473. if (credentialsProvider == null)
  474. throw new TransportException(uri,
  475. JGitText.get().noCredentialsProvider);
  476. if (authAttempts > 1)
  477. credentialsProvider.reset(currentUri);
  478. if (3 < authAttempts
  479. || !authMethod.authorize(currentUri,
  480. credentialsProvider)) {
  481. throw new TransportException(uri,
  482. JGitText.get().notAuthorized);
  483. }
  484. authAttempts++;
  485. continue;
  486. case HttpConnection.HTTP_FORBIDDEN:
  487. throw new TransportException(uri, MessageFormat.format(
  488. JGitText.get().serviceNotPermitted, baseUrl,
  489. service));
  490. case HttpConnection.HTTP_MOVED_PERM:
  491. case HttpConnection.HTTP_MOVED_TEMP:
  492. case HttpConnection.HTTP_SEE_OTHER:
  493. case HttpConnection.HTTP_11_MOVED_PERM:
  494. case HttpConnection.HTTP_11_MOVED_TEMP:
  495. // SEE_OTHER should actually never be sent by a git server,
  496. // and in general should occur only on POST requests. But it
  497. // doesn't hurt to accept it here as a redirect.
  498. if (http.getFollowRedirects() == HttpRedirectMode.FALSE) {
  499. throw new TransportException(uri,
  500. MessageFormat.format(
  501. JGitText.get().redirectsOff,
  502. Integer.valueOf(status)));
  503. }
  504. URIish newUri = redirect(u,
  505. conn.getHeaderField(HDR_LOCATION),
  506. Constants.INFO_REFS, redirects++);
  507. setURI(newUri);
  508. u = getServiceURL(service);
  509. authAttempts = 1;
  510. break;
  511. default:
  512. String err = status + " " + conn.getResponseMessage(); //$NON-NLS-1$
  513. throw new TransportException(uri, err);
  514. }
  515. } catch (NotSupportedException | TransportException e) {
  516. throw e;
  517. } catch (InterruptedIOException e) {
  518. // Timeout!? Don't try other authentication methods.
  519. throw new TransportException(uri, MessageFormat.format(
  520. JGitText.get().connectionTimeOut, u.getHost()), e);
  521. } catch (SocketException e) {
  522. // Nothing on other end, timeout, connection reset, ...
  523. throw new TransportException(uri,
  524. JGitText.get().connectionFailed, e);
  525. } catch (SSLHandshakeException e) {
  526. handleSslFailure(e);
  527. continue; // Re-try
  528. } catch (IOException e) {
  529. if (authMethod.getType() != HttpAuthMethod.Type.NONE) {
  530. if (ignoreTypes == null) {
  531. ignoreTypes = new HashSet<>();
  532. }
  533. ignoreTypes.add(authMethod.getType());
  534. // reset auth method & attempts for next authentication type
  535. authMethod = HttpAuthMethod.Type.NONE.method(null);
  536. authAttempts = 1;
  537. continue;
  538. }
  539. throw new TransportException(uri, MessageFormat.format(JGitText.get().cannotOpenService, service), e);
  540. }
  541. }
  542. }
  543. void processResponseCookies(HttpConnection conn) {
  544. if (cookieFile != null && http.getSaveCookies()) {
  545. List<HttpCookie> foundCookies = new LinkedList<>();
  546. List<String> cookieHeaderValues = conn
  547. .getHeaderFields(HDR_SET_COOKIE);
  548. if (!cookieHeaderValues.isEmpty()) {
  549. foundCookies.addAll(
  550. extractCookies(HDR_SET_COOKIE, cookieHeaderValues));
  551. }
  552. cookieHeaderValues = conn.getHeaderFields(HDR_SET_COOKIE2);
  553. if (!cookieHeaderValues.isEmpty()) {
  554. foundCookies.addAll(
  555. extractCookies(HDR_SET_COOKIE2, cookieHeaderValues));
  556. }
  557. if (!foundCookies.isEmpty()) {
  558. try {
  559. // update cookie lists with the newly received cookies!
  560. Set<HttpCookie> cookies = cookieFile.getCookies(false);
  561. cookies.addAll(foundCookies);
  562. cookieFile.write(baseUrl);
  563. relevantCookies.addAll(foundCookies);
  564. } catch (IOException | IllegalArgumentException
  565. | InterruptedException e) {
  566. LOG.warn(MessageFormat.format(
  567. JGitText.get().couldNotPersistCookies,
  568. cookieFile.getPath()), e);
  569. }
  570. }
  571. }
  572. }
  573. private List<HttpCookie> extractCookies(String headerKey,
  574. List<String> headerValues) {
  575. List<HttpCookie> foundCookies = new LinkedList<>();
  576. for (String headerValue : headerValues) {
  577. foundCookies
  578. .addAll(HttpCookie.parse(headerKey + ':' + headerValue));
  579. }
  580. // HttpCookies.parse(...) is only compliant with RFC 2965. Make it RFC
  581. // 6265 compliant by applying the logic from
  582. // https://tools.ietf.org/html/rfc6265#section-5.2.3
  583. for (HttpCookie foundCookie : foundCookies) {
  584. String domain = foundCookie.getDomain();
  585. if (domain != null && domain.startsWith(".")) { //$NON-NLS-1$
  586. foundCookie.setDomain(domain.substring(1));
  587. }
  588. }
  589. return foundCookies;
  590. }
  591. private static class CredentialItems {
  592. CredentialItem.InformationalMessage message;
  593. /** Trust the server for this git operation */
  594. CredentialItem.YesNoType now;
  595. /**
  596. * Trust the server for all git operations from this repository; may be
  597. * {@code null} if the transport was created via
  598. * {@link #TransportHttp(URIish)}.
  599. */
  600. CredentialItem.YesNoType forRepo;
  601. /** Always trust the server from now on. */
  602. CredentialItem.YesNoType always;
  603. public CredentialItem[] items() {
  604. if (forRepo == null) {
  605. return new CredentialItem[] { message, now, always };
  606. }
  607. return new CredentialItem[] { message, now, forRepo, always };
  608. }
  609. }
  610. private void handleSslFailure(Throwable e) throws TransportException {
  611. if (sslFailure || !trustInsecureSslConnection(e.getCause())) {
  612. throw new TransportException(uri,
  613. MessageFormat.format(
  614. JGitText.get().sslFailureExceptionMessage,
  615. currentUri.setPass(null)),
  616. e);
  617. }
  618. sslFailure = true;
  619. }
  620. private boolean trustInsecureSslConnection(Throwable cause) {
  621. if (cause instanceof CertificateException
  622. || cause instanceof CertPathBuilderException
  623. || cause instanceof CertPathValidatorException) {
  624. // Certificate expired or revoked, PKIX path building not
  625. // possible, self-signed certificate, host does not match ...
  626. CredentialsProvider provider = getCredentialsProvider();
  627. if (provider != null) {
  628. CredentialItems trust = constructSslTrustItems(cause);
  629. CredentialItem[] items = trust.items();
  630. if (provider.supports(items)) {
  631. boolean answered = provider.get(uri, items);
  632. if (answered) {
  633. // Not canceled
  634. boolean trustNow = trust.now.getValue();
  635. boolean trustLocal = trust.forRepo != null
  636. && trust.forRepo.getValue();
  637. boolean trustAlways = trust.always.getValue();
  638. if (trustNow || trustLocal || trustAlways) {
  639. sslVerify = false;
  640. if (trustAlways) {
  641. updateSslVerifyUser(false);
  642. } else if (trustLocal) {
  643. updateSslVerify(local.getConfig(), false);
  644. }
  645. return true;
  646. }
  647. }
  648. }
  649. }
  650. }
  651. return false;
  652. }
  653. private CredentialItems constructSslTrustItems(Throwable cause) {
  654. CredentialItems items = new CredentialItems();
  655. String info = MessageFormat.format(JGitText.get().sslFailureInfo,
  656. currentUri.setPass(null));
  657. String sslMessage = cause.getLocalizedMessage();
  658. if (sslMessage == null) {
  659. sslMessage = cause.toString();
  660. }
  661. sslMessage = MessageFormat.format(JGitText.get().sslFailureCause,
  662. sslMessage);
  663. items.message = new CredentialItem.InformationalMessage(info + '\n'
  664. + sslMessage + '\n'
  665. + JGitText.get().sslFailureTrustExplanation);
  666. items.now = new CredentialItem.YesNoType(JGitText.get().sslTrustNow);
  667. if (local != null) {
  668. items.forRepo = new CredentialItem.YesNoType(
  669. MessageFormat.format(JGitText.get().sslTrustForRepo,
  670. local.getDirectory()));
  671. }
  672. items.always = new CredentialItem.YesNoType(
  673. JGitText.get().sslTrustAlways);
  674. return items;
  675. }
  676. private void updateSslVerify(StoredConfig config, boolean value) {
  677. // Since git uses the original URI for matching, we must also use the
  678. // original URI and cannot use the current URI (which might be different
  679. // after redirects).
  680. String uriPattern = uri.getScheme() + "://" + uri.getHost(); //$NON-NLS-1$
  681. int port = uri.getPort();
  682. if (port > 0) {
  683. uriPattern += ":" + port; //$NON-NLS-1$
  684. }
  685. config.setBoolean(HttpConfig.HTTP, uriPattern,
  686. HttpConfig.SSL_VERIFY_KEY, value);
  687. try {
  688. config.save();
  689. } catch (IOException e) {
  690. LOG.error(JGitText.get().sslVerifyCannotSave, e);
  691. }
  692. }
  693. private void updateSslVerifyUser(boolean value) {
  694. StoredConfig userConfig = null;
  695. try {
  696. userConfig = SystemReader.getInstance().getUserConfig();
  697. updateSslVerify(userConfig, value);
  698. } catch (IOException | ConfigInvalidException e) {
  699. // Log it, but otherwise ignore here.
  700. LOG.error(e.getMessage(), e);
  701. }
  702. }
  703. private URIish redirect(URL currentUrl, String location, String checkFor,
  704. int redirects)
  705. throws TransportException {
  706. if (location == null || location.isEmpty()) {
  707. throw new TransportException(uri,
  708. MessageFormat.format(JGitText.get().redirectLocationMissing,
  709. baseUrl));
  710. }
  711. if (redirects >= http.getMaxRedirects()) {
  712. throw new TransportException(uri,
  713. MessageFormat.format(JGitText.get().redirectLimitExceeded,
  714. Integer.valueOf(http.getMaxRedirects()), baseUrl,
  715. location));
  716. }
  717. try {
  718. URI redirectTo = new URI(location);
  719. redirectTo = currentUrl.toURI().resolve(redirectTo);
  720. String redirected = redirectTo.toASCIIString();
  721. if (!isValidRedirect(baseUrl, redirected, checkFor)) {
  722. throw new TransportException(uri,
  723. MessageFormat.format(JGitText.get().redirectBlocked,
  724. baseUrl, redirected));
  725. }
  726. redirected = redirected.substring(0, redirected.indexOf(checkFor));
  727. URIish result = new URIish(redirected);
  728. if (LOG.isInfoEnabled()) {
  729. LOG.info(MessageFormat.format(JGitText.get().redirectHttp,
  730. uri.setPass(null),
  731. Integer.valueOf(redirects), baseUrl, result));
  732. }
  733. return result;
  734. } catch (URISyntaxException e) {
  735. throw new TransportException(uri,
  736. MessageFormat.format(JGitText.get().invalidRedirectLocation,
  737. baseUrl, location),
  738. e);
  739. }
  740. }
  741. private boolean isValidRedirect(URL current, String next, String checkFor) {
  742. // Protocols must be the same, or current is "http" and next "https". We
  743. // do not follow redirects from https back to http.
  744. String oldProtocol = current.getProtocol().toLowerCase(Locale.ROOT);
  745. int schemeEnd = next.indexOf("://"); //$NON-NLS-1$
  746. if (schemeEnd < 0) {
  747. return false;
  748. }
  749. String newProtocol = next.substring(0, schemeEnd)
  750. .toLowerCase(Locale.ROOT);
  751. if (!oldProtocol.equals(newProtocol)) {
  752. if (!"https".equals(newProtocol)) { //$NON-NLS-1$
  753. return false;
  754. }
  755. }
  756. // git allows only rewriting the root, i.e., everything before INFO_REFS
  757. // or the service name
  758. if (!next.contains(checkFor)) {
  759. return false;
  760. }
  761. // Basically we should test here that whatever follows INFO_REFS is
  762. // unchanged. But since we re-construct the query part
  763. // anyway, it doesn't matter.
  764. return true;
  765. }
  766. private URL getServiceURL(String service)
  767. throws NotSupportedException {
  768. try {
  769. final StringBuilder b = new StringBuilder();
  770. b.append(baseUrl);
  771. if (b.charAt(b.length() - 1) != '/') {
  772. b.append('/');
  773. }
  774. b.append(Constants.INFO_REFS);
  775. if (useSmartHttp) {
  776. b.append(b.indexOf("?") < 0 ? '?' : '&'); //$NON-NLS-1$
  777. b.append("service="); //$NON-NLS-1$
  778. b.append(service);
  779. }
  780. return new URL(b.toString());
  781. } catch (MalformedURLException e) {
  782. throw new NotSupportedException(MessageFormat.format(JGitText.get().invalidURL, uri), e);
  783. }
  784. }
  785. /**
  786. * Open an HTTP connection.
  787. *
  788. * @param method HTTP request method
  789. * @param u url of the HTTP connection
  790. * @param acceptEncoding accept-encoding header option
  791. * @return the HTTP connection
  792. * @throws java.io.IOException
  793. * @since 4.6
  794. */
  795. protected HttpConnection httpOpen(String method, URL u,
  796. AcceptEncoding acceptEncoding) throws IOException {
  797. if (method == null || u == null || acceptEncoding == null) {
  798. throw new NullPointerException();
  799. }
  800. final Proxy proxy = HttpSupport.proxyFor(proxySelector, u);
  801. HttpConnection conn = connectionFactory.create(u, proxy);
  802. if (!sslVerify && "https".equals(u.getProtocol())) { //$NON-NLS-1$
  803. HttpSupport.disableSslVerify(conn);
  804. }
  805. // We must do our own redirect handling to implement git rules and to
  806. // handle http->https redirects
  807. conn.setInstanceFollowRedirects(false);
  808. conn.setRequestMethod(method);
  809. conn.setUseCaches(false);
  810. if (acceptEncoding == AcceptEncoding.GZIP) {
  811. conn.setRequestProperty(HDR_ACCEPT_ENCODING, ENCODING_GZIP);
  812. }
  813. conn.setRequestProperty(HDR_PRAGMA, "no-cache"); //$NON-NLS-1$
  814. if (http.getUserAgent() != null) {
  815. conn.setRequestProperty(HDR_USER_AGENT, http.getUserAgent());
  816. } else if (UserAgent.get() != null) {
  817. conn.setRequestProperty(HDR_USER_AGENT, UserAgent.get());
  818. }
  819. int timeOut = getTimeout();
  820. if (timeOut != -1) {
  821. int effTimeOut = timeOut * 1000;
  822. conn.setConnectTimeout(effTimeOut);
  823. conn.setReadTimeout(effTimeOut);
  824. }
  825. addHeaders(conn, http.getExtraHeaders());
  826. // set cookie header if necessary
  827. if (!relevantCookies.isEmpty()) {
  828. setCookieHeader(conn);
  829. }
  830. if (this.headers != null && !this.headers.isEmpty()) {
  831. for (Map.Entry<String, String> entry : this.headers.entrySet()) {
  832. conn.setRequestProperty(entry.getKey(), entry.getValue());
  833. }
  834. }
  835. authMethod.configureRequest(conn);
  836. return conn;
  837. }
  838. /**
  839. * Adds a list of header strings to the connection. Headers are expected to
  840. * separate keys from values, i.e. "Key: Value". Headers without colon or
  841. * key are ignored (and logged), as are headers with keys that are not RFC
  842. * 7230 tokens or with non-ASCII values.
  843. *
  844. * @param conn
  845. * The target HttpConnection
  846. * @param headersToAdd
  847. * A list of header strings
  848. */
  849. static void addHeaders(HttpConnection conn, List<String> headersToAdd) {
  850. for (String header : headersToAdd) {
  851. // Empty values are allowed according to
  852. // https://tools.ietf.org/html/rfc7230
  853. int colon = header.indexOf(':');
  854. String key = null;
  855. if (colon > 0) {
  856. key = header.substring(0, colon).trim();
  857. }
  858. if (key == null || key.isEmpty()) {
  859. LOG.warn(MessageFormat.format(
  860. JGitText.get().invalidHeaderFormat, header));
  861. } else if (HttpSupport.scanToken(key, 0) != key.length()) {
  862. LOG.warn(MessageFormat.format(JGitText.get().invalidHeaderKey,
  863. header));
  864. } else {
  865. String value = header.substring(colon + 1).trim();
  866. if (!StandardCharsets.US_ASCII.newEncoder().canEncode(value)) {
  867. LOG.warn(MessageFormat
  868. .format(JGitText.get().invalidHeaderValue, header));
  869. } else {
  870. conn.setRequestProperty(key, value);
  871. }
  872. }
  873. }
  874. }
  875. private void setCookieHeader(HttpConnection conn) {
  876. StringBuilder cookieHeaderValue = new StringBuilder();
  877. for (HttpCookie cookie : relevantCookies) {
  878. if (!cookie.hasExpired()) {
  879. if (cookieHeaderValue.length() > 0) {
  880. cookieHeaderValue.append(';');
  881. }
  882. cookieHeaderValue.append(cookie.toString());
  883. }
  884. }
  885. if (cookieHeaderValue.length() > 0) {
  886. conn.setRequestProperty(HDR_COOKIE, cookieHeaderValue.toString());
  887. }
  888. }
  889. final InputStream openInputStream(HttpConnection conn)
  890. throws IOException {
  891. InputStream input = conn.getInputStream();
  892. if (isGzipContent(conn))
  893. input = new GZIPInputStream(input);
  894. return input;
  895. }
  896. IOException wrongContentType(String expType, String actType) {
  897. final String why = MessageFormat.format(JGitText.get().expectedReceivedContentType, expType, actType);
  898. return new TransportException(uri, why);
  899. }
  900. private static NetscapeCookieFile getCookieFileFromConfig(
  901. HttpConfig config) {
  902. if (!StringUtils.isEmptyOrNull(config.getCookieFile())) {
  903. try {
  904. Path cookieFilePath = Paths.get(config.getCookieFile());
  905. return NetscapeCookieFileCache.getInstance(config)
  906. .getEntry(cookieFilePath);
  907. } catch (InvalidPathException e) {
  908. LOG.warn(MessageFormat.format(
  909. JGitText.get().couldNotReadCookieFile,
  910. config.getCookieFile()), e);
  911. }
  912. }
  913. return null;
  914. }
  915. private static Set<HttpCookie> filterCookies(NetscapeCookieFile cookieFile,
  916. URL url) {
  917. if (cookieFile != null) {
  918. return filterCookies(cookieFile.getCookies(true), url);
  919. }
  920. return Collections.emptySet();
  921. }
  922. /**
  923. *
  924. * @param allCookies
  925. * a list of cookies.
  926. * @param url
  927. * the url for which to filter the list of cookies.
  928. * @return only the cookies from {@code allCookies} which are relevant (i.e.
  929. * are not expired, have a matching domain, have a matching path and
  930. * have a matching secure attribute)
  931. */
  932. private static Set<HttpCookie> filterCookies(Set<HttpCookie> allCookies,
  933. URL url) {
  934. Set<HttpCookie> filteredCookies = new HashSet<>();
  935. for (HttpCookie cookie : allCookies) {
  936. if (cookie.hasExpired()) {
  937. continue;
  938. }
  939. if (!matchesCookieDomain(url.getHost(), cookie.getDomain())) {
  940. continue;
  941. }
  942. if (!matchesCookiePath(url.getPath(), cookie.getPath())) {
  943. continue;
  944. }
  945. if (cookie.getSecure() && !"https".equals(url.getProtocol())) { //$NON-NLS-1$
  946. continue;
  947. }
  948. filteredCookies.add(cookie);
  949. }
  950. return filteredCookies;
  951. }
  952. /**
  953. *
  954. * The utility method to check whether a host name is in a cookie's domain
  955. * or not. Similar to {@link HttpCookie#domainMatches(String, String)} but
  956. * implements domain matching rules according to
  957. * <a href="https://tools.ietf.org/html/rfc6265#section-5.1.3">RFC 6265,
  958. * section 5.1.3</a> instead of the rules from
  959. * <a href="https://tools.ietf.org/html/rfc2965#section-3.3">RFC 2965,
  960. * section 3.3.1</a>.
  961. * <p>
  962. * The former rules are also used by libcurl internally.
  963. * <p>
  964. * The rules are as follows
  965. *
  966. * A string matches another domain string if at least one of the following
  967. * conditions holds:
  968. * <ul>
  969. * <li>The domain string and the string are identical. (Note that both the
  970. * domain string and the string will have been canonicalized to lower case
  971. * at this point.)</li>
  972. * <li>All of the following conditions hold
  973. * <ul>
  974. * <li>The domain string is a suffix of the string.</li>
  975. * <li>The last character of the string that is not included in the domain
  976. * string is a %x2E (".") character.</li>
  977. * <li>The string is a host name (i.e., not an IP address).</li>
  978. * </ul>
  979. * </li>
  980. * </ul>
  981. *
  982. * @param host
  983. * the host to compare against the cookieDomain
  984. * @param cookieDomain
  985. * the domain to compare against
  986. * @return {@code true} if they domain-match; {@code false} if not
  987. *
  988. * @see <a href= "https://tools.ietf.org/html/rfc6265#section-5.1.3">RFC
  989. * 6265, section 5.1.3 (Domain Matching)</a>
  990. * @see <a href=
  991. * "https://bugs.java.com/bugdatabase/view_bug.do?bug_id=8206092">JDK-8206092
  992. * : HttpCookie.domainMatches() does not match to sub-sub-domain</a>
  993. */
  994. static boolean matchesCookieDomain(String host, String cookieDomain) {
  995. cookieDomain = cookieDomain.toLowerCase(Locale.ROOT);
  996. host = host.toLowerCase(Locale.ROOT);
  997. if (host.equals(cookieDomain)) {
  998. return true;
  999. }
  1000. if (!host.endsWith(cookieDomain)) {
  1001. return false;
  1002. }
  1003. return host.charAt(host.length() - cookieDomain.length() - 1) == '.';
  1004. }
  1005. /**
  1006. * The utility method to check whether a path is matching a cookie path
  1007. * domain or not. The rules are defined by
  1008. * <a href="https://tools.ietf.org/html/rfc6265#section-5.1.4">RFC 6265,
  1009. * section 5.1.4</a>:
  1010. *
  1011. * A request-path path-matches a given cookie-path if at least one of the
  1012. * following conditions holds:
  1013. * <ul>
  1014. * <li>The cookie-path and the request-path are identical.</li>
  1015. * <li>The cookie-path is a prefix of the request-path, and the last
  1016. * character of the cookie-path is %x2F ("/").</li>
  1017. * <li>The cookie-path is a prefix of the request-path, and the first
  1018. * character of the request-path that is not included in the cookie- path is
  1019. * a %x2F ("/") character.</li>
  1020. * </ul>
  1021. * @param path
  1022. * the path to check
  1023. * @param cookiePath
  1024. * the cookie's path
  1025. *
  1026. * @return {@code true} if they path-match; {@code false} if not
  1027. */
  1028. static boolean matchesCookiePath(String path, String cookiePath) {
  1029. if (cookiePath.equals(path)) {
  1030. return true;
  1031. }
  1032. if (!cookiePath.endsWith("/")) { //$NON-NLS-1$
  1033. cookiePath += "/"; //$NON-NLS-1$
  1034. }
  1035. return path.startsWith(cookiePath);
  1036. }
  1037. private boolean isSmartHttp(HttpConnection c, String service) {
  1038. final String expType = "application/x-" + service + "-advertisement"; //$NON-NLS-1$ //$NON-NLS-2$
  1039. final String actType = c.getContentType();
  1040. return expType.equals(actType);
  1041. }
  1042. private boolean isGzipContent(HttpConnection c) {
  1043. return ENCODING_GZIP.equals(c.getHeaderField(HDR_CONTENT_ENCODING))
  1044. || ENCODING_X_GZIP.equals(c.getHeaderField(HDR_CONTENT_ENCODING));
  1045. }
  1046. private void readSmartHeaders(InputStream in, String service)
  1047. throws IOException {
  1048. // A smart reply will have a '#' after the first 4 bytes, but
  1049. // a dumb reply cannot contain a '#' until after byte 41. Do a
  1050. // quick check to make sure its a smart reply before we parse
  1051. // as a pkt-line stream.
  1052. //
  1053. final byte[] magic = new byte[5];
  1054. IO.readFully(in, magic, 0, magic.length);
  1055. if (magic[4] != '#') {
  1056. throw new TransportException(uri, MessageFormat.format(
  1057. JGitText.get().expectedPktLineWithService, RawParseUtils.decode(magic)));
  1058. }
  1059. final PacketLineIn pckIn = new PacketLineIn(new UnionInputStream(
  1060. new ByteArrayInputStream(magic), in));
  1061. final String exp = "# service=" + service; //$NON-NLS-1$
  1062. final String act = pckIn.readString();
  1063. if (!exp.equals(act)) {
  1064. throw new TransportException(uri, MessageFormat.format(
  1065. JGitText.get().expectedGot, exp, act));
  1066. }
  1067. while (!PacketLineIn.isEnd(pckIn.readString())) {
  1068. // for now, ignore the remaining header lines
  1069. }
  1070. }
  1071. class HttpObjectDB extends WalkRemoteObjectDatabase {
  1072. private final URL httpObjectsUrl;
  1073. HttpObjectDB(URL b) {
  1074. httpObjectsUrl = b;
  1075. }
  1076. @Override
  1077. URIish getURI() {
  1078. return new URIish(httpObjectsUrl);
  1079. }
  1080. @Override
  1081. Collection<WalkRemoteObjectDatabase> getAlternates() throws IOException {
  1082. try {
  1083. return readAlternates(INFO_HTTP_ALTERNATES);
  1084. } catch (FileNotFoundException err) {
  1085. // Fall through.
  1086. }
  1087. try {
  1088. return readAlternates(INFO_ALTERNATES);
  1089. } catch (FileNotFoundException err) {
  1090. // Fall through.
  1091. }
  1092. return null;
  1093. }
  1094. @Override
  1095. WalkRemoteObjectDatabase openAlternate(String location)
  1096. throws IOException {
  1097. return new HttpObjectDB(new URL(httpObjectsUrl, location));
  1098. }
  1099. @Override
  1100. BufferedReader openReader(String path) throws IOException {
  1101. // Line oriented readable content is likely to compress well.
  1102. // Request gzip encoding.
  1103. InputStream is = open(path, AcceptEncoding.GZIP).in;
  1104. return new BufferedReader(new InputStreamReader(is, UTF_8));
  1105. }
  1106. @Override
  1107. Collection<String> getPackNames() throws IOException {
  1108. final Collection<String> packs = new ArrayList<>();
  1109. try (BufferedReader br = openReader(INFO_PACKS)) {
  1110. for (;;) {
  1111. final String s = br.readLine();
  1112. if (s == null || s.length() == 0)
  1113. break;
  1114. if (!s.startsWith("P pack-") || !s.endsWith(".pack")) //$NON-NLS-1$ //$NON-NLS-2$
  1115. throw invalidAdvertisement(s);
  1116. packs.add(s.substring(2));
  1117. }
  1118. return packs;
  1119. } catch (FileNotFoundException err) {
  1120. return packs;
  1121. }
  1122. }
  1123. @Override
  1124. FileStream open(String path) throws IOException {
  1125. return open(path, AcceptEncoding.UNSPECIFIED);
  1126. }
  1127. FileStream open(String path, AcceptEncoding acceptEncoding)
  1128. throws IOException {
  1129. final URL base = httpObjectsUrl;
  1130. final URL u = new URL(base, path);
  1131. final HttpConnection c = httpOpen(METHOD_GET, u, acceptEncoding);
  1132. switch (HttpSupport.response(c)) {
  1133. case HttpConnection.HTTP_OK:
  1134. final InputStream in = openInputStream(c);
  1135. // If content is being gzipped and then transferred, the content
  1136. // length in the header is the zipped content length, not the
  1137. // actual content length.
  1138. if (!isGzipContent(c)) {
  1139. final int len = c.getContentLength();
  1140. return new FileStream(in, len);
  1141. }
  1142. return new FileStream(in);
  1143. case HttpConnection.HTTP_NOT_FOUND:
  1144. throw new FileNotFoundException(u.toString());
  1145. default:
  1146. throw new IOException(u.toString() + ": " //$NON-NLS-1$
  1147. + HttpSupport.response(c) + " " //$NON-NLS-1$
  1148. + c.getResponseMessage());
  1149. }
  1150. }
  1151. Map<String, Ref> readAdvertisedImpl(final BufferedReader br)
  1152. throws IOException, PackProtocolException {
  1153. final TreeMap<String, Ref> avail = new TreeMap<>();
  1154. for (;;) {
  1155. String line = br.readLine();
  1156. if (line == null)
  1157. break;
  1158. final int tab = line.indexOf('\t');
  1159. if (tab < 0)
  1160. throw invalidAdvertisement(line);
  1161. String name;
  1162. final ObjectId id;
  1163. name = line.substring(tab + 1);
  1164. id = ObjectId.fromString(line.substring(0, tab));
  1165. if (name.endsWith("^{}")) { //$NON-NLS-1$
  1166. name = name.substring(0, name.length() - 3);
  1167. final Ref prior = avail.get(name);
  1168. if (prior == null)
  1169. throw outOfOrderAdvertisement(name);
  1170. if (prior.getPeeledObjectId() != null)
  1171. throw duplicateAdvertisement(name + "^{}"); //$NON-NLS-1$
  1172. avail.put(name, new ObjectIdRef.PeeledTag(
  1173. Ref.Storage.NETWORK, name,
  1174. prior.getObjectId(), id));
  1175. } else {
  1176. Ref prior = avail.put(name, new ObjectIdRef.PeeledNonTag(
  1177. Ref.Storage.NETWORK, name, id));
  1178. if (prior != null)
  1179. throw duplicateAdvertisement(name);
  1180. }
  1181. }
  1182. return avail;
  1183. }
  1184. private PackProtocolException outOfOrderAdvertisement(String n) {
  1185. return new PackProtocolException(MessageFormat.format(JGitText.get().advertisementOfCameBefore, n, n));
  1186. }
  1187. private PackProtocolException invalidAdvertisement(String n) {
  1188. return new PackProtocolException(MessageFormat.format(JGitText.get().invalidAdvertisementOf, n));
  1189. }
  1190. private PackProtocolException duplicateAdvertisement(String n) {
  1191. return new PackProtocolException(MessageFormat.format(JGitText.get().duplicateAdvertisementsOf, n));
  1192. }
  1193. @Override
  1194. void close() {
  1195. // We do not maintain persistent connections.
  1196. }
  1197. }
  1198. class SmartHttpFetchConnection extends BasePackFetchConnection {
  1199. private MultiRequestService svc;
  1200. SmartHttpFetchConnection(InputStream advertisement)
  1201. throws TransportException {
  1202. super(TransportHttp.this);
  1203. statelessRPC = true;
  1204. init(advertisement, DisabledOutputStream.INSTANCE);
  1205. outNeedsEnd = false;
  1206. readAdvertisedRefs();
  1207. }
  1208. @Override
  1209. protected void doFetch(final ProgressMonitor monitor,
  1210. final Collection<Ref> want, final Set<ObjectId> have,
  1211. final OutputStream outputStream) throws TransportException {
  1212. try {
  1213. svc = new MultiRequestService(SVC_UPLOAD_PACK);
  1214. init(svc.getInputStream(), svc.getOutputStream());
  1215. super.doFetch(monitor, want, have, outputStream);
  1216. } finally {
  1217. svc = null;
  1218. }
  1219. }
  1220. @Override
  1221. protected void onReceivePack() {
  1222. svc.finalRequest = true;
  1223. }
  1224. }
  1225. class SmartHttpPushConnection extends BasePackPushConnection {
  1226. SmartHttpPushConnection(InputStream advertisement)
  1227. throws TransportException {
  1228. super(TransportHttp.this);
  1229. statelessRPC = true;
  1230. init(advertisement, DisabledOutputStream.INSTANCE);
  1231. outNeedsEnd = false;
  1232. readAdvertisedRefs();
  1233. }
  1234. @Override
  1235. protected void doPush(final ProgressMonitor monitor,
  1236. final Map<String, RemoteRefUpdate> refUpdates,
  1237. OutputStream outputStream) throws TransportException {
  1238. final Service svc = new MultiRequestService(SVC_RECEIVE_PACK);
  1239. init(svc.getInputStream(), svc.getOutputStream());
  1240. super.doPush(monitor, refUpdates, outputStream);
  1241. }
  1242. }
  1243. /** Basic service for sending and receiving HTTP requests. */
  1244. abstract class Service {
  1245. protected final String serviceName;
  1246. protected final String requestType;
  1247. protected final String responseType;
  1248. protected HttpConnection conn;
  1249. protected HttpOutputStream out;
  1250. protected final HttpExecuteStream execute;
  1251. final UnionInputStream in;
  1252. Service(String serviceName) {
  1253. this.serviceName = serviceName;
  1254. this.requestType = "application/x-" + serviceName + "-request"; //$NON-NLS-1$ //$NON-NLS-2$
  1255. this.responseType = "application/x-" + serviceName + "-result"; //$NON-NLS-1$ //$NON-NLS-2$
  1256. this.out = new HttpOutputStream();
  1257. this.execute = new HttpExecuteStream();
  1258. this.in = new UnionInputStream(execute);
  1259. }
  1260. void openStream() throws IOException {
  1261. conn = httpOpen(METHOD_POST, new URL(baseUrl, serviceName),
  1262. AcceptEncoding.GZIP);
  1263. conn.setInstanceFollowRedirects(false);
  1264. conn.setDoOutput(true);
  1265. conn.setRequestProperty(HDR_CONTENT_TYPE, requestType);
  1266. conn.setRequestProperty(HDR_ACCEPT, responseType);
  1267. }
  1268. void sendRequest() throws IOException {
  1269. // Try to compress the content, but only if that is smaller.
  1270. TemporaryBuffer buf = new TemporaryBuffer.Heap(
  1271. http.getPostBuffer());
  1272. try (GZIPOutputStream gzip = new GZIPOutputStream(buf)) {
  1273. out.writeTo(gzip, null);
  1274. if (out.length() < buf.length())
  1275. buf = out;
  1276. } catch (IOException err) {
  1277. // Most likely caused by overflowing the buffer, meaning
  1278. // its larger if it were compressed. Don't compress.
  1279. buf = out;
  1280. }
  1281. HttpAuthMethod authenticator = null;
  1282. Collection<Type> ignoreTypes = EnumSet.noneOf(Type.class);
  1283. // Counts number of repeated authentication attempts using the same
  1284. // authentication scheme
  1285. int authAttempts = 1;
  1286. int redirects = 0;
  1287. for (;;) {
  1288. try {
  1289. // The very first time we will try with the authentication
  1290. // method used on the initial GET request. This is a hint
  1291. // only; it may fail. If so, we'll then re-try with proper
  1292. // 401 handling, going through the available authentication
  1293. // schemes.
  1294. openStream();
  1295. if (buf != out) {
  1296. conn.setRequestProperty(HDR_CONTENT_ENCODING,
  1297. ENCODING_GZIP);
  1298. }
  1299. conn.setFixedLengthStreamingMode((int) buf.length());
  1300. try (OutputStream httpOut = conn.getOutputStream()) {
  1301. buf.writeTo(httpOut, null);
  1302. }
  1303. final int status = HttpSupport.response(conn);
  1304. switch (status) {
  1305. case HttpConnection.HTTP_OK:
  1306. // We're done.
  1307. return;
  1308. case HttpConnection.HTTP_NOT_FOUND:
  1309. throw createNotFoundException(uri, conn.getURL(),
  1310. conn.getResponseMessage());
  1311. case HttpConnection.HTTP_FORBIDDEN:
  1312. throw new TransportException(uri,
  1313. MessageFormat.format(
  1314. JGitText.get().serviceNotPermitted,
  1315. baseUrl, serviceName));
  1316. case HttpConnection.HTTP_MOVED_PERM:
  1317. case HttpConnection.HTTP_MOVED_TEMP:
  1318. case HttpConnection.HTTP_11_MOVED_PERM:
  1319. case HttpConnection.HTTP_11_MOVED_TEMP:
  1320. // SEE_OTHER after a POST doesn't make sense for a git
  1321. // server, so we don't handle it here and thus we'll
  1322. // report an error in openResponse() later on.
  1323. if (http.getFollowRedirects() != HttpRedirectMode.TRUE) {
  1324. // Let openResponse() issue an error
  1325. return;
  1326. }
  1327. currentUri = redirect(conn.getURL(),
  1328. conn.getHeaderField(HDR_LOCATION),
  1329. '/' + serviceName, redirects++);
  1330. try {
  1331. baseUrl = toURL(currentUri);
  1332. } catch (MalformedURLException e) {
  1333. throw new TransportException(uri,
  1334. MessageFormat.format(
  1335. JGitText.get().invalidRedirectLocation,
  1336. baseUrl, currentUri),
  1337. e);
  1338. }
  1339. continue;
  1340. case HttpConnection.HTTP_UNAUTHORIZED:
  1341. HttpAuthMethod nextMethod = HttpAuthMethod
  1342. .scanResponse(conn, ignoreTypes);
  1343. switch (nextMethod.getType()) {
  1344. case NONE:
  1345. throw new TransportException(uri,
  1346. MessageFormat.format(
  1347. JGitText.get().authenticationNotSupported,
  1348. conn.getURL()));
  1349. case NEGOTIATE:
  1350. // RFC 4559 states "When using the SPNEGO [...] with
  1351. // [...] POST, the authentication should be complete
  1352. // [...] before sending the user data." So in theory
  1353. // the initial GET should have been authenticated
  1354. // already. (Unless there was a redirect?)
  1355. //
  1356. // We try this only once:
  1357. ignoreTypes.add(HttpAuthMethod.Type.NEGOTIATE);
  1358. if (authenticator != null) {
  1359. ignoreTypes.add(authenticator.getType());
  1360. }
  1361. authAttempts = 1;
  1362. // We only do the Kerberos part of SPNEGO, which
  1363. // requires only one round.
  1364. break;
  1365. default:
  1366. // DIGEST or BASIC. Let's be sure we ignore
  1367. // NEGOTIATE; if it was available, we have tried it
  1368. // before.
  1369. ignoreTypes.add(HttpAuthMethod.Type.NEGOTIATE);
  1370. if (authenticator == null || authenticator
  1371. .getType() != nextMethod.getType()) {
  1372. if (authenticator != null) {
  1373. ignoreTypes.add(authenticator.getType());
  1374. }
  1375. authAttempts = 1;
  1376. }
  1377. break;
  1378. }
  1379. authMethod = nextMethod;
  1380. authenticator = nextMethod;
  1381. CredentialsProvider credentialsProvider = getCredentialsProvider();
  1382. if (credentialsProvider == null) {
  1383. throw new TransportException(uri,
  1384. JGitText.get().noCredentialsProvider);
  1385. }
  1386. if (authAttempts > 1) {
  1387. credentialsProvider.reset(currentUri);
  1388. }
  1389. if (3 < authAttempts || !authMethod
  1390. .authorize(currentUri, credentialsProvider)) {
  1391. throw new TransportException(uri,
  1392. JGitText.get().notAuthorized);
  1393. }
  1394. authAttempts++;
  1395. continue;
  1396. default:
  1397. // Just return here; openResponse() will report an
  1398. // appropriate error.
  1399. return;
  1400. }
  1401. } catch (SSLHandshakeException e) {
  1402. handleSslFailure(e);
  1403. continue; // Re-try
  1404. } catch (SocketException | InterruptedIOException e) {
  1405. // Timeout!? Must propagate; don't try other authentication
  1406. // methods.
  1407. throw e;
  1408. } catch (IOException e) {
  1409. if (authenticator == null || authMethod
  1410. .getType() != HttpAuthMethod.Type.NONE) {
  1411. // Can happen for instance if the server advertises
  1412. // Negotiate, but the client isn't configured for
  1413. // Kerberos. The first time (authenticator == null) we
  1414. // must re-try even if the authMethod was NONE: this may
  1415. // occur if the server advertised NTLM on the GET
  1416. // and the HttpConnection managed to successfully
  1417. // authenticate under the hood with NTLM. We might not
  1418. // have picked this up on the GET's 200 response.
  1419. if (authMethod.getType() != HttpAuthMethod.Type.NONE) {
  1420. ignoreTypes.add(authMethod.getType());
  1421. }
  1422. // Start over with the remaining available methods.
  1423. authMethod = HttpAuthMethod.Type.NONE.method(null);
  1424. authenticator = authMethod;
  1425. authAttempts = 1;
  1426. continue;
  1427. }
  1428. throw e;
  1429. }
  1430. }
  1431. }
  1432. void openResponse() throws IOException {
  1433. final int status = HttpSupport.response(conn);
  1434. if (status != HttpConnection.HTTP_OK) {
  1435. throw new TransportException(uri, status + " " //$NON-NLS-1$
  1436. + conn.getResponseMessage());
  1437. }
  1438. final String contentType = conn.getContentType();
  1439. if (!responseType.equals(contentType)) {
  1440. conn.getInputStream().close();
  1441. throw wrongContentType(responseType, contentType);
  1442. }
  1443. }
  1444. HttpOutputStream getOutputStream() {
  1445. return out;
  1446. }
  1447. InputStream getInputStream() {
  1448. return in;
  1449. }
  1450. abstract void execute() throws IOException;
  1451. class HttpExecuteStream extends InputStream {
  1452. @Override
  1453. public int read() throws IOException {
  1454. execute();
  1455. return -1;
  1456. }
  1457. @Override
  1458. public int read(byte[] b, int off, int len) throws IOException {
  1459. execute();
  1460. return -1;
  1461. }
  1462. @Override
  1463. public long skip(long n) throws IOException {
  1464. execute();
  1465. return 0;
  1466. }
  1467. }
  1468. class HttpOutputStream extends TemporaryBuffer {
  1469. HttpOutputStream() {
  1470. super(http.getPostBuffer());
  1471. }
  1472. @Override
  1473. protected OutputStream overflow() throws IOException {
  1474. openStream();
  1475. conn.setChunkedStreamingMode(0);
  1476. return conn.getOutputStream();
  1477. }
  1478. }
  1479. }
  1480. /**
  1481. * State required to speak multiple HTTP requests with the remote.
  1482. * <p>
  1483. * A service wrapper provides a normal looking InputStream and OutputStream
  1484. * pair which are connected via HTTP to the named remote service. Writing to
  1485. * the OutputStream is buffered until either the buffer overflows, or
  1486. * reading from the InputStream occurs. If overflow occurs HTTP/1.1 and its
  1487. * chunked transfer encoding is used to stream the request data to the
  1488. * remote service. If the entire request fits in the memory buffer, the
  1489. * older HTTP/1.0 standard and a fixed content length is used instead.
  1490. * <p>
  1491. * It is an error to attempt to read without there being outstanding data
  1492. * ready for transmission on the OutputStream.
  1493. * <p>
  1494. * No state is preserved between write-read request pairs. The caller is
  1495. * responsible for replaying state vector information as part of the request
  1496. * data written to the OutputStream. Any session HTTP cookies may or may not
  1497. * be preserved between requests, it is left up to the JVM's implementation
  1498. * of the HTTP client.
  1499. */
  1500. class MultiRequestService extends Service {
  1501. boolean finalRequest;
  1502. MultiRequestService(String serviceName) {
  1503. super(serviceName);
  1504. }
  1505. /** Keep opening send-receive pairs to the given URI. */
  1506. @Override
  1507. void execute() throws IOException {
  1508. out.close();
  1509. if (conn == null) {
  1510. if (out.length() == 0) {
  1511. // Request output hasn't started yet, but more data is being
  1512. // requested. If there is no request data buffered and the
  1513. // final request was already sent, do nothing to ensure the
  1514. // caller is shown EOF on the InputStream; otherwise an
  1515. // programming error has occurred within this module.
  1516. if (finalRequest)
  1517. return;
  1518. throw new TransportException(uri,
  1519. JGitText.get().startingReadStageWithoutWrittenRequestDataPendingIsNotSupported);
  1520. }
  1521. sendRequest();
  1522. }
  1523. out.reset();
  1524. openResponse();
  1525. in.add(openInputStream(conn));
  1526. if (!finalRequest)
  1527. in.add(execute);
  1528. conn = null;
  1529. }
  1530. }
  1531. /** Service for maintaining a single long-poll connection. */
  1532. class LongPollService extends Service {
  1533. /**
  1534. * @param serviceName
  1535. */
  1536. LongPollService(String serviceName) {
  1537. super(serviceName);
  1538. }
  1539. /** Only open one send-receive request. */
  1540. @Override
  1541. void execute() throws IOException {
  1542. out.close();
  1543. if (conn == null)
  1544. sendRequest();
  1545. openResponse();
  1546. in.add(openInputStream(conn));
  1547. }
  1548. }
  1549. }