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

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