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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004
  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. * and other copyright owners as documented in the project's IP log.
  6. *
  7. * This program and the accompanying materials are made available
  8. * under the terms of the Eclipse Distribution License v1.0 which
  9. * accompanies this distribution, is reproduced below, and is
  10. * available at http://www.eclipse.org/org/documents/edl-v10.php
  11. *
  12. * All rights reserved.
  13. *
  14. * Redistribution and use in source and binary forms, with or
  15. * without modification, are permitted provided that the following
  16. * conditions are met:
  17. *
  18. * - Redistributions of source code must retain the above copyright
  19. * notice, this list of conditions and the following disclaimer.
  20. *
  21. * - Redistributions in binary form must reproduce the above
  22. * copyright notice, this list of conditions and the following
  23. * disclaimer in the documentation and/or other materials provided
  24. * with the distribution.
  25. *
  26. * - Neither the name of the Eclipse Foundation, Inc. nor the
  27. * names of its contributors may be used to endorse or promote
  28. * products derived from this software without specific prior
  29. * written permission.
  30. *
  31. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  32. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  33. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  34. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  35. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  36. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  37. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  38. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  39. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  40. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  41. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  42. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  43. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  44. */
  45. package org.eclipse.jgit.transport;
  46. import static org.eclipse.jgit.util.HttpSupport.ENCODING_GZIP;
  47. import static org.eclipse.jgit.util.HttpSupport.HDR_ACCEPT;
  48. import static org.eclipse.jgit.util.HttpSupport.HDR_ACCEPT_ENCODING;
  49. import static org.eclipse.jgit.util.HttpSupport.HDR_CONTENT_ENCODING;
  50. import static org.eclipse.jgit.util.HttpSupport.HDR_CONTENT_TYPE;
  51. import static org.eclipse.jgit.util.HttpSupport.HDR_PRAGMA;
  52. import static org.eclipse.jgit.util.HttpSupport.HDR_USER_AGENT;
  53. import static org.eclipse.jgit.util.HttpSupport.METHOD_GET;
  54. import static org.eclipse.jgit.util.HttpSupport.METHOD_POST;
  55. import java.io.BufferedReader;
  56. import java.io.ByteArrayInputStream;
  57. import java.io.FileNotFoundException;
  58. import java.io.IOException;
  59. import java.io.InputStream;
  60. import java.io.InputStreamReader;
  61. import java.io.OutputStream;
  62. import java.net.HttpURLConnection;
  63. import java.net.MalformedURLException;
  64. import java.net.Proxy;
  65. import java.net.ProxySelector;
  66. import java.net.URL;
  67. import java.net.URLConnection;
  68. import java.security.KeyManagementException;
  69. import java.security.NoSuchAlgorithmException;
  70. import java.security.cert.X509Certificate;
  71. import java.text.MessageFormat;
  72. import java.util.ArrayList;
  73. import java.util.Arrays;
  74. import java.util.Collection;
  75. import java.util.Collections;
  76. import java.util.EnumSet;
  77. import java.util.LinkedHashSet;
  78. import java.util.Map;
  79. import java.util.Set;
  80. import java.util.TreeMap;
  81. import java.util.zip.GZIPInputStream;
  82. import java.util.zip.GZIPOutputStream;
  83. import javax.net.ssl.HostnameVerifier;
  84. import javax.net.ssl.HttpsURLConnection;
  85. import javax.net.ssl.SSLContext;
  86. import javax.net.ssl.SSLSession;
  87. import javax.net.ssl.TrustManager;
  88. import javax.net.ssl.X509TrustManager;
  89. import org.eclipse.jgit.errors.NoRemoteRepositoryException;
  90. import org.eclipse.jgit.errors.NotSupportedException;
  91. import org.eclipse.jgit.errors.PackProtocolException;
  92. import org.eclipse.jgit.errors.TransportException;
  93. import org.eclipse.jgit.internal.JGitText;
  94. import org.eclipse.jgit.internal.storage.file.RefDirectory;
  95. import org.eclipse.jgit.lib.Config;
  96. import org.eclipse.jgit.lib.Config.SectionParser;
  97. import org.eclipse.jgit.lib.Constants;
  98. import org.eclipse.jgit.lib.ObjectId;
  99. import org.eclipse.jgit.lib.ObjectIdRef;
  100. import org.eclipse.jgit.lib.ProgressMonitor;
  101. import org.eclipse.jgit.lib.Ref;
  102. import org.eclipse.jgit.lib.Repository;
  103. import org.eclipse.jgit.lib.SymbolicRef;
  104. import org.eclipse.jgit.util.HttpSupport;
  105. import org.eclipse.jgit.util.IO;
  106. import org.eclipse.jgit.util.RawParseUtils;
  107. import org.eclipse.jgit.util.TemporaryBuffer;
  108. import org.eclipse.jgit.util.io.DisabledOutputStream;
  109. import org.eclipse.jgit.util.io.UnionInputStream;
  110. /**
  111. * Transport over HTTP and FTP protocols.
  112. * <p>
  113. * If the transport is using HTTP and the remote HTTP service is Git-aware
  114. * (speaks the "smart-http protocol") this client will automatically take
  115. * advantage of the additional Git-specific HTTP extensions. If the remote
  116. * service does not support these extensions, the client will degrade to direct
  117. * file fetching.
  118. * <p>
  119. * If the remote (server side) repository does not have the specialized Git
  120. * support, object files are retrieved directly through standard HTTP GET (or
  121. * binary FTP GET) requests. This make it easy to serve a Git repository through
  122. * a standard web host provider that does not offer specific support for Git.
  123. *
  124. * @see WalkFetchConnection
  125. */
  126. public class TransportHttp extends HttpTransport implements WalkTransport,
  127. PackTransport {
  128. private static final String SVC_UPLOAD_PACK = "git-upload-pack"; //$NON-NLS-1$
  129. private static final String SVC_RECEIVE_PACK = "git-receive-pack"; //$NON-NLS-1$
  130. private static final String userAgent = computeUserAgent();
  131. static final TransportProtocol PROTO_HTTP = new TransportProtocol() {
  132. private final String[] schemeNames = { "http", "https" }; //$NON-NLS-1$ //$NON-NLS-2$
  133. private final Set<String> schemeSet = Collections
  134. .unmodifiableSet(new LinkedHashSet<String>(Arrays
  135. .asList(schemeNames)));
  136. public String getName() {
  137. return JGitText.get().transportProtoHTTP;
  138. }
  139. public Set<String> getSchemes() {
  140. return schemeSet;
  141. }
  142. public Set<URIishField> getRequiredFields() {
  143. return Collections.unmodifiableSet(EnumSet.of(URIishField.HOST,
  144. URIishField.PATH));
  145. }
  146. public Set<URIishField> getOptionalFields() {
  147. return Collections.unmodifiableSet(EnumSet.of(URIishField.USER,
  148. URIishField.PASS, URIishField.PORT));
  149. }
  150. public int getDefaultPort() {
  151. return 80;
  152. }
  153. public Transport open(URIish uri, Repository local, String remoteName)
  154. throws NotSupportedException {
  155. return new TransportHttp(local, uri);
  156. }
  157. public Transport open(URIish uri) throws NotSupportedException {
  158. return new TransportHttp(uri);
  159. }
  160. };
  161. static final TransportProtocol PROTO_FTP = new TransportProtocol() {
  162. public String getName() {
  163. return JGitText.get().transportProtoFTP;
  164. }
  165. public Set<String> getSchemes() {
  166. return Collections.singleton("ftp"); //$NON-NLS-1$
  167. }
  168. public Set<URIishField> getRequiredFields() {
  169. return Collections.unmodifiableSet(EnumSet.of(URIishField.HOST,
  170. URIishField.PATH));
  171. }
  172. public Set<URIishField> getOptionalFields() {
  173. return Collections.unmodifiableSet(EnumSet.of(URIishField.USER,
  174. URIishField.PASS, URIishField.PORT));
  175. }
  176. public int getDefaultPort() {
  177. return 21;
  178. }
  179. public Transport open(URIish uri, Repository local, String remoteName)
  180. throws NotSupportedException {
  181. return new TransportHttp(local, uri);
  182. }
  183. };
  184. private static String computeUserAgent() {
  185. String version;
  186. final Package pkg = TransportHttp.class.getPackage();
  187. if (pkg != null && pkg.getImplementationVersion() != null) {
  188. version = pkg.getImplementationVersion();
  189. } else {
  190. version = "unknown"; //$NON-NLS-1$
  191. }
  192. return "JGit/" + version; //$NON-NLS-1$
  193. }
  194. private static final Config.SectionParser<HttpConfig> HTTP_KEY = new SectionParser<HttpConfig>() {
  195. public HttpConfig parse(final Config cfg) {
  196. return new HttpConfig(cfg);
  197. }
  198. };
  199. private static class HttpConfig {
  200. final int postBuffer;
  201. final boolean sslVerify;
  202. HttpConfig(final Config rc) {
  203. postBuffer = rc.getInt("http", "postbuffer", 1 * 1024 * 1024); //$NON-NLS-1$ //$NON-NLS-2$
  204. sslVerify = rc.getBoolean("http", "sslVerify", true); //$NON-NLS-1$ //$NON-NLS-2$
  205. }
  206. private HttpConfig() {
  207. this(new Config());
  208. }
  209. }
  210. private final URL baseUrl;
  211. private final URL objectsUrl;
  212. private final HttpConfig http;
  213. private final ProxySelector proxySelector;
  214. private boolean useSmartHttp = true;
  215. private HttpAuthMethod authMethod = HttpAuthMethod.NONE;
  216. TransportHttp(final Repository local, final URIish uri)
  217. throws NotSupportedException {
  218. super(local, uri);
  219. try {
  220. String uriString = uri.toString();
  221. if (!uriString.endsWith("/")) //$NON-NLS-1$
  222. uriString += "/"; //$NON-NLS-1$
  223. baseUrl = new URL(uriString);
  224. objectsUrl = new URL(baseUrl, "objects/"); //$NON-NLS-1$
  225. } catch (MalformedURLException e) {
  226. throw new NotSupportedException(MessageFormat.format(JGitText.get().invalidURL, uri), e);
  227. }
  228. http = local.getConfig().get(HTTP_KEY);
  229. proxySelector = ProxySelector.getDefault();
  230. }
  231. /**
  232. * Create a minimal HTTP transport with default configuration values.
  233. *
  234. * @param uri
  235. * @throws NotSupportedException
  236. */
  237. TransportHttp(final URIish uri) throws NotSupportedException {
  238. super(uri);
  239. try {
  240. String uriString = uri.toString();
  241. if (!uriString.endsWith("/")) //$NON-NLS-1$
  242. uriString += "/"; //$NON-NLS-1$
  243. baseUrl = new URL(uriString);
  244. objectsUrl = new URL(baseUrl, "objects/"); //$NON-NLS-1$
  245. } catch (MalformedURLException e) {
  246. throw new NotSupportedException(MessageFormat.format(JGitText.get().invalidURL, uri), e);
  247. }
  248. http = new HttpConfig();
  249. proxySelector = ProxySelector.getDefault();
  250. }
  251. /**
  252. * Toggle whether or not smart HTTP transport should be used.
  253. * <p>
  254. * This flag exists primarily to support backwards compatibility testing
  255. * within a testing framework, there is no need to modify it in most
  256. * applications.
  257. *
  258. * @param on
  259. * if {@code true} (default), smart HTTP is enabled.
  260. */
  261. public void setUseSmartHttp(final boolean on) {
  262. useSmartHttp = on;
  263. }
  264. @Override
  265. public FetchConnection openFetch() throws TransportException,
  266. NotSupportedException {
  267. final String service = SVC_UPLOAD_PACK;
  268. try {
  269. final HttpURLConnection c = connect(service);
  270. final InputStream in = openInputStream(c);
  271. try {
  272. if (isSmartHttp(c, service)) {
  273. readSmartHeaders(in, service);
  274. return new SmartHttpFetchConnection(in);
  275. } else {
  276. // Assume this server doesn't support smart HTTP fetch
  277. // and fall back on dumb object walking.
  278. //
  279. return newDumbConnection(in);
  280. }
  281. } finally {
  282. in.close();
  283. }
  284. } catch (NotSupportedException err) {
  285. throw err;
  286. } catch (TransportException err) {
  287. throw err;
  288. } catch (IOException err) {
  289. throw new TransportException(uri, JGitText.get().errorReadingInfoRefs, err);
  290. }
  291. }
  292. private FetchConnection newDumbConnection(InputStream in)
  293. throws IOException, PackProtocolException {
  294. HttpObjectDB d = new HttpObjectDB(objectsUrl);
  295. BufferedReader br = toBufferedReader(in);
  296. Map<String, Ref> refs;
  297. try {
  298. refs = d.readAdvertisedImpl(br);
  299. } finally {
  300. br.close();
  301. }
  302. if (!refs.containsKey(Constants.HEAD)) {
  303. // If HEAD was not published in the info/refs file (it usually
  304. // is not there) download HEAD by itself as a loose file and do
  305. // the resolution by hand.
  306. //
  307. HttpURLConnection conn = httpOpen(new URL(baseUrl, Constants.HEAD));
  308. int status = HttpSupport.response(conn);
  309. switch (status) {
  310. case HttpURLConnection.HTTP_OK: {
  311. br = toBufferedReader(openInputStream(conn));
  312. try {
  313. String line = br.readLine();
  314. if (line != null && line.startsWith(RefDirectory.SYMREF)) {
  315. String target = line.substring(RefDirectory.SYMREF.length());
  316. Ref r = refs.get(target);
  317. if (r == null)
  318. r = new ObjectIdRef.Unpeeled(Ref.Storage.NEW, target, null);
  319. r = new SymbolicRef(Constants.HEAD, r);
  320. refs.put(r.getName(), r);
  321. } else if (line != null && ObjectId.isId(line)) {
  322. Ref r = new ObjectIdRef.Unpeeled(Ref.Storage.NETWORK,
  323. Constants.HEAD, ObjectId.fromString(line));
  324. refs.put(r.getName(), r);
  325. }
  326. } finally {
  327. br.close();
  328. }
  329. break;
  330. }
  331. case HttpURLConnection.HTTP_NOT_FOUND:
  332. break;
  333. default:
  334. throw new TransportException(uri, MessageFormat.format(
  335. JGitText.get().cannotReadHEAD, Integer.valueOf(status),
  336. conn.getResponseMessage()));
  337. }
  338. }
  339. WalkFetchConnection wfc = new WalkFetchConnection(this, d);
  340. wfc.available(refs);
  341. return wfc;
  342. }
  343. private BufferedReader toBufferedReader(InputStream in) {
  344. return new BufferedReader(new InputStreamReader(in, Constants.CHARSET));
  345. }
  346. @Override
  347. public PushConnection openPush() throws NotSupportedException,
  348. TransportException {
  349. final String service = SVC_RECEIVE_PACK;
  350. try {
  351. final HttpURLConnection c = connect(service);
  352. final InputStream in = openInputStream(c);
  353. try {
  354. if (isSmartHttp(c, service)) {
  355. readSmartHeaders(in, service);
  356. return new SmartHttpPushConnection(in);
  357. } else if (!useSmartHttp) {
  358. final String msg = JGitText.get().smartHTTPPushDisabled;
  359. throw new NotSupportedException(msg);
  360. } else {
  361. final String msg = JGitText.get().remoteDoesNotSupportSmartHTTPPush;
  362. throw new NotSupportedException(msg);
  363. }
  364. } finally {
  365. in.close();
  366. }
  367. } catch (NotSupportedException err) {
  368. throw err;
  369. } catch (TransportException err) {
  370. throw err;
  371. } catch (IOException err) {
  372. throw new TransportException(uri, JGitText.get().errorReadingInfoRefs, err);
  373. }
  374. }
  375. @Override
  376. public void close() {
  377. // No explicit connections are maintained.
  378. }
  379. private HttpURLConnection connect(final String service)
  380. throws TransportException, NotSupportedException {
  381. final URL u;
  382. try {
  383. final StringBuilder b = new StringBuilder();
  384. b.append(baseUrl);
  385. if (b.charAt(b.length() - 1) != '/')
  386. b.append('/');
  387. b.append(Constants.INFO_REFS);
  388. if (useSmartHttp) {
  389. b.append(b.indexOf("?") < 0 ? '?' : '&'); //$NON-NLS-1$
  390. b.append("service="); //$NON-NLS-1$
  391. b.append(service);
  392. }
  393. u = new URL(b.toString());
  394. } catch (MalformedURLException e) {
  395. throw new NotSupportedException(MessageFormat.format(JGitText.get().invalidURL, uri), e);
  396. }
  397. try {
  398. int authAttempts = 1;
  399. for (;;) {
  400. final HttpURLConnection conn = httpOpen(u);
  401. if (useSmartHttp) {
  402. String exp = "application/x-" + service + "-advertisement"; //$NON-NLS-1$ //$NON-NLS-2$
  403. conn.setRequestProperty(HDR_ACCEPT, exp + ", */*"); //$NON-NLS-1$
  404. } else {
  405. conn.setRequestProperty(HDR_ACCEPT, "*/*"); //$NON-NLS-1$
  406. }
  407. final int status = HttpSupport.response(conn);
  408. switch (status) {
  409. case HttpURLConnection.HTTP_OK:
  410. return conn;
  411. case HttpURLConnection.HTTP_NOT_FOUND:
  412. throw new NoRemoteRepositoryException(uri,
  413. MessageFormat.format(JGitText.get().uriNotFound, u));
  414. case HttpURLConnection.HTTP_UNAUTHORIZED:
  415. authMethod = HttpAuthMethod.scanResponse(conn);
  416. if (authMethod == HttpAuthMethod.NONE)
  417. throw new TransportException(uri, MessageFormat.format(
  418. JGitText.get().authenticationNotSupported, uri));
  419. if (1 < authAttempts
  420. || !authMethod.authorize(uri,
  421. getCredentialsProvider())) {
  422. throw new TransportException(uri,
  423. JGitText.get().notAuthorized);
  424. }
  425. authAttempts++;
  426. continue;
  427. case HttpURLConnection.HTTP_FORBIDDEN:
  428. throw new TransportException(uri, MessageFormat.format(
  429. JGitText.get().serviceNotPermitted, service));
  430. default:
  431. String err = status + " " + conn.getResponseMessage(); //$NON-NLS-1$
  432. throw new TransportException(uri, err);
  433. }
  434. }
  435. } catch (NotSupportedException e) {
  436. throw e;
  437. } catch (TransportException e) {
  438. throw e;
  439. } catch (IOException e) {
  440. throw new TransportException(uri, MessageFormat.format(JGitText.get().cannotOpenService, service), e);
  441. }
  442. }
  443. final HttpURLConnection httpOpen(URL u) throws IOException {
  444. return httpOpen(METHOD_GET, u);
  445. }
  446. /**
  447. * Open an HTTP connection.
  448. *
  449. * @param method
  450. * @param u
  451. * @return the connection
  452. * @throws IOException
  453. * @since 3.2
  454. */
  455. protected HttpURLConnection httpOpen(String method, URL u)
  456. throws IOException {
  457. final Proxy proxy = HttpSupport.proxyFor(proxySelector, u);
  458. HttpURLConnection conn = (HttpURLConnection) u.openConnection(proxy);
  459. if (!http.sslVerify && "https".equals(u.getProtocol())) { //$NON-NLS-1$
  460. disableSslVerify(conn);
  461. }
  462. conn.setRequestMethod(method);
  463. conn.setUseCaches(false);
  464. conn.setRequestProperty(HDR_ACCEPT_ENCODING, ENCODING_GZIP);
  465. conn.setRequestProperty(HDR_PRAGMA, "no-cache"); //$NON-NLS-1$
  466. conn.setRequestProperty(HDR_USER_AGENT, userAgent);
  467. int timeOut = getTimeout();
  468. if (timeOut != -1) {
  469. int effTimeOut = timeOut * 1000;
  470. conn.setConnectTimeout(effTimeOut);
  471. conn.setReadTimeout(effTimeOut);
  472. }
  473. authMethod.configureRequest(conn);
  474. return conn;
  475. }
  476. private void disableSslVerify(URLConnection conn)
  477. throws IOException {
  478. final TrustManager[] trustAllCerts = new TrustManager[] { new DummyX509TrustManager() };
  479. try {
  480. SSLContext ctx = SSLContext.getInstance("SSL"); //$NON-NLS-1$
  481. ctx.init(null, trustAllCerts, null);
  482. final HttpsURLConnection sslConn = (HttpsURLConnection) conn;
  483. sslConn.setSSLSocketFactory(ctx.getSocketFactory());
  484. sslConn.setHostnameVerifier(new DummyHostnameVerifier());
  485. } catch (KeyManagementException e) {
  486. throw new IOException(e.getMessage());
  487. } catch (NoSuchAlgorithmException e) {
  488. throw new IOException(e.getMessage());
  489. }
  490. }
  491. final InputStream openInputStream(HttpURLConnection conn)
  492. throws IOException {
  493. InputStream input = conn.getInputStream();
  494. if (ENCODING_GZIP.equals(conn.getHeaderField(HDR_CONTENT_ENCODING)))
  495. input = new GZIPInputStream(input);
  496. return input;
  497. }
  498. IOException wrongContentType(String expType, String actType) {
  499. final String why = MessageFormat.format(JGitText.get().expectedReceivedContentType, expType, actType);
  500. return new TransportException(uri, why);
  501. }
  502. private boolean isSmartHttp(final HttpURLConnection c, final String service) {
  503. final String expType = "application/x-" + service + "-advertisement"; //$NON-NLS-1$ //$NON-NLS-2$
  504. final String actType = c.getContentType();
  505. return expType.equals(actType);
  506. }
  507. private void readSmartHeaders(final InputStream in, final String service)
  508. throws IOException {
  509. // A smart reply will have a '#' after the first 4 bytes, but
  510. // a dumb reply cannot contain a '#' until after byte 41. Do a
  511. // quick check to make sure its a smart reply before we parse
  512. // as a pkt-line stream.
  513. //
  514. final byte[] magic = new byte[5];
  515. IO.readFully(in, magic, 0, magic.length);
  516. if (magic[4] != '#') {
  517. throw new TransportException(uri, MessageFormat.format(
  518. JGitText.get().expectedPktLineWithService, RawParseUtils.decode(magic)));
  519. }
  520. final PacketLineIn pckIn = new PacketLineIn(new UnionInputStream(
  521. new ByteArrayInputStream(magic), in));
  522. final String exp = "# service=" + service; //$NON-NLS-1$
  523. final String act = pckIn.readString();
  524. if (!exp.equals(act)) {
  525. throw new TransportException(uri, MessageFormat.format(
  526. JGitText.get().expectedGot, exp, act));
  527. }
  528. while (pckIn.readString() != PacketLineIn.END) {
  529. // for now, ignore the remaining header lines
  530. }
  531. }
  532. class HttpObjectDB extends WalkRemoteObjectDatabase {
  533. private final URL objectsUrl;
  534. HttpObjectDB(final URL b) {
  535. objectsUrl = b;
  536. }
  537. @Override
  538. URIish getURI() {
  539. return new URIish(objectsUrl);
  540. }
  541. @Override
  542. Collection<WalkRemoteObjectDatabase> getAlternates() throws IOException {
  543. try {
  544. return readAlternates(INFO_HTTP_ALTERNATES);
  545. } catch (FileNotFoundException err) {
  546. // Fall through.
  547. }
  548. try {
  549. return readAlternates(INFO_ALTERNATES);
  550. } catch (FileNotFoundException err) {
  551. // Fall through.
  552. }
  553. return null;
  554. }
  555. @Override
  556. WalkRemoteObjectDatabase openAlternate(final String location)
  557. throws IOException {
  558. return new HttpObjectDB(new URL(objectsUrl, location));
  559. }
  560. @Override
  561. Collection<String> getPackNames() throws IOException {
  562. final Collection<String> packs = new ArrayList<String>();
  563. try {
  564. final BufferedReader br = openReader(INFO_PACKS);
  565. try {
  566. for (;;) {
  567. final String s = br.readLine();
  568. if (s == null || s.length() == 0)
  569. break;
  570. if (!s.startsWith("P pack-") || !s.endsWith(".pack")) //$NON-NLS-1$ //$NON-NLS-2$
  571. throw invalidAdvertisement(s);
  572. packs.add(s.substring(2));
  573. }
  574. return packs;
  575. } finally {
  576. br.close();
  577. }
  578. } catch (FileNotFoundException err) {
  579. return packs;
  580. }
  581. }
  582. @Override
  583. FileStream open(final String path) throws IOException {
  584. final URL base = objectsUrl;
  585. final URL u = new URL(base, path);
  586. final HttpURLConnection c = httpOpen(u);
  587. switch (HttpSupport.response(c)) {
  588. case HttpURLConnection.HTTP_OK:
  589. final InputStream in = openInputStream(c);
  590. final int len = c.getContentLength();
  591. return new FileStream(in, len);
  592. case HttpURLConnection.HTTP_NOT_FOUND:
  593. throw new FileNotFoundException(u.toString());
  594. default:
  595. throw new IOException(u.toString() + ": " //$NON-NLS-1$
  596. + HttpSupport.response(c) + " " //$NON-NLS-1$
  597. + c.getResponseMessage());
  598. }
  599. }
  600. Map<String, Ref> readAdvertisedImpl(final BufferedReader br)
  601. throws IOException, PackProtocolException {
  602. final TreeMap<String, Ref> avail = new TreeMap<String, Ref>();
  603. for (;;) {
  604. String line = br.readLine();
  605. if (line == null)
  606. break;
  607. final int tab = line.indexOf('\t');
  608. if (tab < 0)
  609. throw invalidAdvertisement(line);
  610. String name;
  611. final ObjectId id;
  612. name = line.substring(tab + 1);
  613. id = ObjectId.fromString(line.substring(0, tab));
  614. if (name.endsWith("^{}")) { //$NON-NLS-1$
  615. name = name.substring(0, name.length() - 3);
  616. final Ref prior = avail.get(name);
  617. if (prior == null)
  618. throw outOfOrderAdvertisement(name);
  619. if (prior.getPeeledObjectId() != null)
  620. throw duplicateAdvertisement(name + "^{}"); //$NON-NLS-1$
  621. avail.put(name, new ObjectIdRef.PeeledTag(
  622. Ref.Storage.NETWORK, name,
  623. prior.getObjectId(), id));
  624. } else {
  625. Ref prior = avail.put(name, new ObjectIdRef.PeeledNonTag(
  626. Ref.Storage.NETWORK, name, id));
  627. if (prior != null)
  628. throw duplicateAdvertisement(name);
  629. }
  630. }
  631. return avail;
  632. }
  633. private PackProtocolException outOfOrderAdvertisement(final String n) {
  634. return new PackProtocolException(MessageFormat.format(JGitText.get().advertisementOfCameBefore, n, n));
  635. }
  636. private PackProtocolException invalidAdvertisement(final String n) {
  637. return new PackProtocolException(MessageFormat.format(JGitText.get().invalidAdvertisementOf, n));
  638. }
  639. private PackProtocolException duplicateAdvertisement(final String n) {
  640. return new PackProtocolException(MessageFormat.format(JGitText.get().duplicateAdvertisementsOf, n));
  641. }
  642. @Override
  643. void close() {
  644. // We do not maintain persistent connections.
  645. }
  646. }
  647. class SmartHttpFetchConnection extends BasePackFetchConnection {
  648. private MultiRequestService svc;
  649. SmartHttpFetchConnection(final InputStream advertisement)
  650. throws TransportException {
  651. super(TransportHttp.this);
  652. statelessRPC = true;
  653. init(advertisement, DisabledOutputStream.INSTANCE);
  654. outNeedsEnd = false;
  655. readAdvertisedRefs();
  656. }
  657. @Override
  658. protected void doFetch(final ProgressMonitor monitor,
  659. final Collection<Ref> want, final Set<ObjectId> have,
  660. final OutputStream outputStream) throws TransportException {
  661. try {
  662. svc = new MultiRequestService(SVC_UPLOAD_PACK);
  663. init(svc.getInputStream(), svc.getOutputStream());
  664. super.doFetch(monitor, want, have, outputStream);
  665. } finally {
  666. svc = null;
  667. }
  668. }
  669. @Override
  670. protected void onReceivePack() {
  671. svc.finalRequest = true;
  672. }
  673. }
  674. class SmartHttpPushConnection extends BasePackPushConnection {
  675. SmartHttpPushConnection(final InputStream advertisement)
  676. throws TransportException {
  677. super(TransportHttp.this);
  678. statelessRPC = true;
  679. init(advertisement, DisabledOutputStream.INSTANCE);
  680. outNeedsEnd = false;
  681. readAdvertisedRefs();
  682. }
  683. protected void doPush(final ProgressMonitor monitor,
  684. final Map<String, RemoteRefUpdate> refUpdates,
  685. OutputStream outputStream) throws TransportException {
  686. final Service svc = new MultiRequestService(SVC_RECEIVE_PACK);
  687. init(svc.getInputStream(), svc.getOutputStream());
  688. super.doPush(monitor, refUpdates, outputStream);
  689. }
  690. }
  691. /** Basic service for sending and receiving HTTP requests. */
  692. abstract class Service {
  693. protected final String serviceName;
  694. protected final String requestType;
  695. protected final String responseType;
  696. protected HttpURLConnection conn;
  697. protected HttpOutputStream out;
  698. protected final HttpExecuteStream execute;
  699. final UnionInputStream in;
  700. Service(String serviceName) {
  701. this.serviceName = serviceName;
  702. this.requestType = "application/x-" + serviceName + "-request"; //$NON-NLS-1$ //$NON-NLS-2$
  703. this.responseType = "application/x-" + serviceName + "-result"; //$NON-NLS-1$ //$NON-NLS-2$
  704. this.out = new HttpOutputStream();
  705. this.execute = new HttpExecuteStream();
  706. this.in = new UnionInputStream(execute);
  707. }
  708. void openStream() throws IOException {
  709. conn = httpOpen(METHOD_POST, new URL(baseUrl, serviceName));
  710. conn.setInstanceFollowRedirects(false);
  711. conn.setDoOutput(true);
  712. conn.setRequestProperty(HDR_CONTENT_TYPE, requestType);
  713. conn.setRequestProperty(HDR_ACCEPT, responseType);
  714. }
  715. void sendRequest() throws IOException {
  716. // Try to compress the content, but only if that is smaller.
  717. TemporaryBuffer buf = new TemporaryBuffer.Heap(http.postBuffer);
  718. try {
  719. GZIPOutputStream gzip = new GZIPOutputStream(buf);
  720. out.writeTo(gzip, null);
  721. gzip.close();
  722. if (out.length() < buf.length())
  723. buf = out;
  724. } catch (IOException err) {
  725. // Most likely caused by overflowing the buffer, meaning
  726. // its larger if it were compressed. Don't compress.
  727. buf = out;
  728. }
  729. openStream();
  730. if (buf != out)
  731. conn.setRequestProperty(HDR_CONTENT_ENCODING, ENCODING_GZIP);
  732. conn.setFixedLengthStreamingMode((int) buf.length());
  733. final OutputStream httpOut = conn.getOutputStream();
  734. try {
  735. buf.writeTo(httpOut, null);
  736. } finally {
  737. httpOut.close();
  738. }
  739. }
  740. void openResponse() throws IOException {
  741. final int status = HttpSupport.response(conn);
  742. if (status != HttpURLConnection.HTTP_OK) {
  743. throw new TransportException(uri, status + " " //$NON-NLS-1$
  744. + conn.getResponseMessage());
  745. }
  746. final String contentType = conn.getContentType();
  747. if (!responseType.equals(contentType)) {
  748. conn.getInputStream().close();
  749. throw wrongContentType(responseType, contentType);
  750. }
  751. }
  752. HttpOutputStream getOutputStream() {
  753. return out;
  754. }
  755. InputStream getInputStream() {
  756. return in;
  757. }
  758. abstract void execute() throws IOException;
  759. class HttpExecuteStream extends InputStream {
  760. public int read() throws IOException {
  761. execute();
  762. return -1;
  763. }
  764. public int read(byte[] b, int off, int len) throws IOException {
  765. execute();
  766. return -1;
  767. }
  768. public long skip(long n) throws IOException {
  769. execute();
  770. return 0;
  771. }
  772. }
  773. class HttpOutputStream extends TemporaryBuffer {
  774. HttpOutputStream() {
  775. super(http.postBuffer);
  776. }
  777. @Override
  778. protected OutputStream overflow() throws IOException {
  779. openStream();
  780. conn.setChunkedStreamingMode(0);
  781. return conn.getOutputStream();
  782. }
  783. }
  784. }
  785. /**
  786. * State required to speak multiple HTTP requests with the remote.
  787. * <p>
  788. * A service wrapper provides a normal looking InputStream and OutputStream
  789. * pair which are connected via HTTP to the named remote service. Writing to
  790. * the OutputStream is buffered until either the buffer overflows, or
  791. * reading from the InputStream occurs. If overflow occurs HTTP/1.1 and its
  792. * chunked transfer encoding is used to stream the request data to the
  793. * remote service. If the entire request fits in the memory buffer, the
  794. * older HTTP/1.0 standard and a fixed content length is used instead.
  795. * <p>
  796. * It is an error to attempt to read without there being outstanding data
  797. * ready for transmission on the OutputStream.
  798. * <p>
  799. * No state is preserved between write-read request pairs. The caller is
  800. * responsible for replaying state vector information as part of the request
  801. * data written to the OutputStream. Any session HTTP cookies may or may not
  802. * be preserved between requests, it is left up to the JVM's implementation
  803. * of the HTTP client.
  804. */
  805. class MultiRequestService extends Service {
  806. boolean finalRequest;
  807. MultiRequestService(final String serviceName) {
  808. super(serviceName);
  809. }
  810. /** Keep opening send-receive pairs to the given URI. */
  811. @Override
  812. void execute() throws IOException {
  813. out.close();
  814. if (conn == null) {
  815. if (out.length() == 0) {
  816. // Request output hasn't started yet, but more data is being
  817. // requested. If there is no request data buffered and the
  818. // final request was already sent, do nothing to ensure the
  819. // caller is shown EOF on the InputStream; otherwise an
  820. // programming error has occurred within this module.
  821. if (finalRequest)
  822. return;
  823. throw new TransportException(uri,
  824. JGitText.get().startingReadStageWithoutWrittenRequestDataPendingIsNotSupported);
  825. }
  826. sendRequest();
  827. }
  828. out.reset();
  829. openResponse();
  830. in.add(openInputStream(conn));
  831. if (!finalRequest)
  832. in.add(execute);
  833. conn = null;
  834. }
  835. }
  836. /** Service for maintaining a single long-poll connection. */
  837. class LongPollService extends Service {
  838. /**
  839. * @param serviceName
  840. */
  841. LongPollService(String serviceName) {
  842. super(serviceName);
  843. }
  844. /** Only open one send-receive request. */
  845. @Override
  846. void execute() throws IOException {
  847. out.close();
  848. if (conn == null)
  849. sendRequest();
  850. openResponse();
  851. in.add(openInputStream(conn));
  852. }
  853. }
  854. private static class DummyX509TrustManager implements X509TrustManager {
  855. public X509Certificate[] getAcceptedIssuers() {
  856. return null;
  857. }
  858. public void checkClientTrusted(X509Certificate[] certs, String authType) {
  859. // no check
  860. }
  861. public void checkServerTrusted(X509Certificate[] certs, String authType) {
  862. // no check
  863. }
  864. }
  865. private static class DummyHostnameVerifier implements HostnameVerifier {
  866. public boolean verify(String hostname, SSLSession session) {
  867. // always accept
  868. return true;
  869. }
  870. }
  871. }