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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828
  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.Collection;
  73. import java.util.Map;
  74. import java.util.Set;
  75. import java.util.TreeMap;
  76. import java.util.zip.GZIPInputStream;
  77. import java.util.zip.GZIPOutputStream;
  78. import javax.net.ssl.HttpsURLConnection;
  79. import javax.net.ssl.SSLContext;
  80. import javax.net.ssl.TrustManager;
  81. import javax.net.ssl.X509TrustManager;
  82. import org.eclipse.jgit.JGitText;
  83. import org.eclipse.jgit.errors.NoRemoteRepositoryException;
  84. import org.eclipse.jgit.errors.NotSupportedException;
  85. import org.eclipse.jgit.errors.PackProtocolException;
  86. import org.eclipse.jgit.errors.TransportException;
  87. import org.eclipse.jgit.lib.Config;
  88. import org.eclipse.jgit.lib.Config.SectionParser;
  89. import org.eclipse.jgit.lib.Constants;
  90. import org.eclipse.jgit.lib.ObjectId;
  91. import org.eclipse.jgit.lib.ObjectIdRef;
  92. import org.eclipse.jgit.lib.ProgressMonitor;
  93. import org.eclipse.jgit.lib.Ref;
  94. import org.eclipse.jgit.lib.Repository;
  95. import org.eclipse.jgit.lib.SymbolicRef;
  96. import org.eclipse.jgit.storage.file.RefDirectory;
  97. import org.eclipse.jgit.util.HttpSupport;
  98. import org.eclipse.jgit.util.IO;
  99. import org.eclipse.jgit.util.RawParseUtils;
  100. import org.eclipse.jgit.util.TemporaryBuffer;
  101. import org.eclipse.jgit.util.io.DisabledOutputStream;
  102. import org.eclipse.jgit.util.io.UnionInputStream;
  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 String SVC_UPLOAD_PACK = "git-upload-pack"; //$NON-NLS-1$
  122. private static final String SVC_RECEIVE_PACK = "git-receive-pack"; //$NON-NLS-1$
  123. private static final String userAgent = computeUserAgent();
  124. static boolean canHandle(final URIish uri) {
  125. if (!uri.isRemote())
  126. return false;
  127. final String s = uri.getScheme();
  128. return "http".equals(s) || "https".equals(s) || "ftp".equals(s); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
  129. }
  130. private static String computeUserAgent() {
  131. String version;
  132. final Package pkg = TransportHttp.class.getPackage();
  133. if (pkg != null && pkg.getImplementationVersion() != null) {
  134. version = pkg.getImplementationVersion();
  135. } else {
  136. version = "unknown"; //$NON-NLS-1$
  137. }
  138. return "JGit/" + version; //$NON-NLS-1$
  139. }
  140. private static final Config.SectionParser<HttpConfig> HTTP_KEY = new SectionParser<HttpConfig>() {
  141. public HttpConfig parse(final Config cfg) {
  142. return new HttpConfig(cfg);
  143. }
  144. };
  145. private static class HttpConfig {
  146. final int postBuffer;
  147. final boolean sslVerify;
  148. HttpConfig(final Config rc) {
  149. postBuffer = rc.getInt("http", "postbuffer", 1 * 1024 * 1024); //$NON-NLS-1$ //$NON-NLS-2$
  150. sslVerify = rc.getBoolean("http", "sslVerify", true);
  151. }
  152. }
  153. private final URL baseUrl;
  154. private final URL objectsUrl;
  155. private final HttpConfig http;
  156. private final ProxySelector proxySelector;
  157. private boolean useSmartHttp = true;
  158. private HttpAuthMethod authMethod = HttpAuthMethod.NONE;
  159. TransportHttp(final Repository local, final URIish uri)
  160. throws NotSupportedException {
  161. super(local, uri);
  162. try {
  163. String uriString = uri.toString();
  164. if (!uriString.endsWith("/")) //$NON-NLS-1$
  165. uriString += "/"; //$NON-NLS-1$
  166. baseUrl = new URL(uriString);
  167. objectsUrl = new URL(baseUrl, "objects/"); //$NON-NLS-1$
  168. } catch (MalformedURLException e) {
  169. throw new NotSupportedException(MessageFormat.format(JGitText.get().invalidURL, uri), e);
  170. }
  171. http = local.getConfig().get(HTTP_KEY);
  172. proxySelector = ProxySelector.getDefault();
  173. }
  174. /**
  175. * Toggle whether or not smart HTTP transport should be used.
  176. * <p>
  177. * This flag exists primarily to support backwards compatibility testing
  178. * within a testing framework, there is no need to modify it in most
  179. * applications.
  180. *
  181. * @param on
  182. * if {@code true} (default), smart HTTP is enabled.
  183. */
  184. public void setUseSmartHttp(final boolean on) {
  185. useSmartHttp = on;
  186. }
  187. @Override
  188. public FetchConnection openFetch() throws TransportException,
  189. NotSupportedException {
  190. final String service = SVC_UPLOAD_PACK;
  191. try {
  192. final HttpURLConnection c = connect(service);
  193. final InputStream in = openInputStream(c);
  194. try {
  195. if (isSmartHttp(c, service)) {
  196. readSmartHeaders(in, service);
  197. return new SmartHttpFetchConnection(in);
  198. } else {
  199. // Assume this server doesn't support smart HTTP fetch
  200. // and fall back on dumb object walking.
  201. //
  202. return newDumbConnection(in);
  203. }
  204. } finally {
  205. in.close();
  206. }
  207. } catch (NotSupportedException err) {
  208. throw err;
  209. } catch (TransportException err) {
  210. throw err;
  211. } catch (IOException err) {
  212. throw new TransportException(uri, JGitText.get().errorReadingInfoRefs, err);
  213. }
  214. }
  215. private FetchConnection newDumbConnection(InputStream in)
  216. throws IOException, PackProtocolException {
  217. HttpObjectDB d = new HttpObjectDB(objectsUrl);
  218. BufferedReader br = toBufferedReader(in);
  219. Map<String, Ref> refs;
  220. try {
  221. refs = d.readAdvertisedImpl(br);
  222. } finally {
  223. br.close();
  224. }
  225. if (!refs.containsKey(Constants.HEAD)) {
  226. // If HEAD was not published in the info/refs file (it usually
  227. // is not there) download HEAD by itself as a loose file and do
  228. // the resolution by hand.
  229. //
  230. HttpURLConnection conn = httpOpen(new URL(baseUrl, Constants.HEAD));
  231. int status = HttpSupport.response(conn);
  232. switch (status) {
  233. case HttpURLConnection.HTTP_OK: {
  234. br = toBufferedReader(openInputStream(conn));
  235. try {
  236. String line = br.readLine();
  237. if (line != null && line.startsWith(RefDirectory.SYMREF)) {
  238. String target = line.substring(RefDirectory.SYMREF.length());
  239. Ref r = refs.get(target);
  240. if (r == null)
  241. r = new ObjectIdRef.Unpeeled(Ref.Storage.NEW, target, null);
  242. r = new SymbolicRef(Constants.HEAD, r);
  243. refs.put(r.getName(), r);
  244. } else if (line != null && ObjectId.isId(line)) {
  245. Ref r = new ObjectIdRef.Unpeeled(Ref.Storage.NETWORK,
  246. Constants.HEAD, ObjectId.fromString(line));
  247. refs.put(r.getName(), r);
  248. }
  249. } finally {
  250. br.close();
  251. }
  252. break;
  253. }
  254. case HttpURLConnection.HTTP_NOT_FOUND:
  255. break;
  256. default:
  257. throw new TransportException(uri, MessageFormat.format(
  258. JGitText.get().cannotReadHEAD, status, conn.getResponseMessage()));
  259. }
  260. }
  261. WalkFetchConnection wfc = new WalkFetchConnection(this, d);
  262. wfc.available(refs);
  263. return wfc;
  264. }
  265. private BufferedReader toBufferedReader(InputStream in) {
  266. return new BufferedReader(new InputStreamReader(in, Constants.CHARSET));
  267. }
  268. @Override
  269. public PushConnection openPush() throws NotSupportedException,
  270. TransportException {
  271. final String service = SVC_RECEIVE_PACK;
  272. try {
  273. final HttpURLConnection c = connect(service);
  274. final InputStream in = openInputStream(c);
  275. try {
  276. if (isSmartHttp(c, service)) {
  277. readSmartHeaders(in, service);
  278. return new SmartHttpPushConnection(in);
  279. } else if (!useSmartHttp) {
  280. final String msg = JGitText.get().smartHTTPPushDisabled;
  281. throw new NotSupportedException(msg);
  282. } else {
  283. final String msg = JGitText.get().remoteDoesNotSupportSmartHTTPPush;
  284. throw new NotSupportedException(msg);
  285. }
  286. } finally {
  287. in.close();
  288. }
  289. } catch (NotSupportedException err) {
  290. throw err;
  291. } catch (TransportException err) {
  292. throw err;
  293. } catch (IOException err) {
  294. throw new TransportException(uri, JGitText.get().errorReadingInfoRefs, err);
  295. }
  296. }
  297. @Override
  298. public void close() {
  299. // No explicit connections are maintained.
  300. }
  301. private HttpURLConnection connect(final String service)
  302. throws TransportException, NotSupportedException {
  303. final URL u;
  304. try {
  305. final StringBuilder b = new StringBuilder();
  306. b.append(baseUrl);
  307. if (b.charAt(b.length() - 1) != '/')
  308. b.append('/');
  309. b.append(Constants.INFO_REFS);
  310. if (useSmartHttp) {
  311. b.append(b.indexOf("?") < 0 ? '?' : '&'); //$NON-NLS-1$
  312. b.append("service="); //$NON-NLS-1$
  313. b.append(service);
  314. }
  315. u = new URL(b.toString());
  316. } catch (MalformedURLException e) {
  317. throw new NotSupportedException(MessageFormat.format(JGitText.get().invalidURL, uri), e);
  318. }
  319. try {
  320. int authAttempts = 1;
  321. for (;;) {
  322. final HttpURLConnection conn = httpOpen(u);
  323. if (useSmartHttp) {
  324. String exp = "application/x-" + service + "-advertisement"; //$NON-NLS-1$ //$NON-NLS-2$
  325. conn.setRequestProperty(HDR_ACCEPT, exp + ", */*"); //$NON-NLS-1$
  326. } else {
  327. conn.setRequestProperty(HDR_ACCEPT, "*/*"); //$NON-NLS-1$
  328. }
  329. final int status = HttpSupport.response(conn);
  330. switch (status) {
  331. case HttpURLConnection.HTTP_OK:
  332. return conn;
  333. case HttpURLConnection.HTTP_NOT_FOUND:
  334. throw new NoRemoteRepositoryException(uri,
  335. MessageFormat.format(JGitText.get().uriNotFound, u));
  336. case HttpURLConnection.HTTP_UNAUTHORIZED:
  337. authMethod = HttpAuthMethod.scanResponse(conn);
  338. if (authMethod == HttpAuthMethod.NONE)
  339. throw new TransportException(uri, MessageFormat.format(
  340. JGitText.get().authenticationNotSupported, uri));
  341. if (1 < authAttempts
  342. || !authMethod.authorize(uri,
  343. getCredentialsProvider())) {
  344. throw new TransportException(uri,
  345. JGitText.get().notAuthorized);
  346. }
  347. authAttempts++;
  348. continue;
  349. case HttpURLConnection.HTTP_FORBIDDEN:
  350. throw new TransportException(uri, MessageFormat.format(
  351. JGitText.get().serviceNotPermitted, service));
  352. default:
  353. String err = status + " " + conn.getResponseMessage(); //$NON-NLS-1$
  354. throw new TransportException(uri, err);
  355. }
  356. }
  357. } catch (NotSupportedException e) {
  358. throw e;
  359. } catch (TransportException e) {
  360. throw e;
  361. } catch (IOException e) {
  362. throw new TransportException(uri, MessageFormat.format(JGitText.get().cannotOpenService, service), e);
  363. }
  364. }
  365. final HttpURLConnection httpOpen(URL u) throws IOException {
  366. return httpOpen(METHOD_GET, u);
  367. }
  368. final HttpURLConnection httpOpen(String method, URL u) throws IOException {
  369. final Proxy proxy = HttpSupport.proxyFor(proxySelector, u);
  370. HttpURLConnection conn = (HttpURLConnection) u.openConnection(proxy);
  371. if (!http.sslVerify && "https".equals(u.getProtocol())) {
  372. disableSslVerify(conn);
  373. }
  374. conn.setRequestMethod(method);
  375. conn.setUseCaches(false);
  376. conn.setRequestProperty(HDR_ACCEPT_ENCODING, ENCODING_GZIP);
  377. conn.setRequestProperty(HDR_PRAGMA, "no-cache"); //$NON-NLS-1$
  378. conn.setRequestProperty(HDR_USER_AGENT, userAgent);
  379. conn.setConnectTimeout(getTimeout() * 1000);
  380. conn.setReadTimeout(getTimeout() * 1000);
  381. authMethod.configureRequest(conn);
  382. return conn;
  383. }
  384. private void disableSslVerify(URLConnection conn)
  385. throws IOException {
  386. final TrustManager[] trustAllCerts = new TrustManager[] { new DummyX509TrustManager() };
  387. try {
  388. SSLContext ctx = SSLContext.getInstance("SSL");
  389. ctx.init(null, trustAllCerts, null);
  390. final HttpsURLConnection sslConn = (HttpsURLConnection) conn;
  391. sslConn.setSSLSocketFactory(ctx.getSocketFactory());
  392. } catch (KeyManagementException e) {
  393. throw new IOException(e);
  394. } catch (NoSuchAlgorithmException e) {
  395. throw new IOException(e);
  396. }
  397. }
  398. final InputStream openInputStream(HttpURLConnection conn)
  399. throws IOException {
  400. InputStream input = conn.getInputStream();
  401. if (ENCODING_GZIP.equals(conn.getHeaderField(HDR_CONTENT_ENCODING)))
  402. input = new GZIPInputStream(input);
  403. return input;
  404. }
  405. IOException wrongContentType(String expType, String actType) {
  406. final String why = MessageFormat.format(JGitText.get().expectedReceivedContentType, expType, actType);
  407. return new TransportException(uri, why);
  408. }
  409. private boolean isSmartHttp(final HttpURLConnection c, final String service) {
  410. final String expType = "application/x-" + service + "-advertisement"; //$NON-NLS-1$ //$NON-NLS-2$
  411. final String actType = c.getContentType();
  412. return expType.equals(actType);
  413. }
  414. private void readSmartHeaders(final InputStream in, final String service)
  415. throws IOException {
  416. // A smart reply will have a '#' after the first 4 bytes, but
  417. // a dumb reply cannot contain a '#' until after byte 41. Do a
  418. // quick check to make sure its a smart reply before we parse
  419. // as a pkt-line stream.
  420. //
  421. final byte[] magic = new byte[5];
  422. IO.readFully(in, magic, 0, magic.length);
  423. if (magic[4] != '#') {
  424. throw new TransportException(uri, MessageFormat.format(
  425. JGitText.get().expectedPktLineWithService, RawParseUtils.decode(magic)));
  426. }
  427. final PacketLineIn pckIn = new PacketLineIn(new UnionInputStream(
  428. new ByteArrayInputStream(magic), in));
  429. final String exp = "# service=" + service; //$NON-NLS-1$
  430. final String act = pckIn.readString();
  431. if (!exp.equals(act)) {
  432. throw new TransportException(uri, MessageFormat.format(
  433. JGitText.get().expectedGot, exp, act));
  434. }
  435. while (pckIn.readString() != PacketLineIn.END) {
  436. // for now, ignore the remaining header lines
  437. }
  438. }
  439. class HttpObjectDB extends WalkRemoteObjectDatabase {
  440. private final URL objectsUrl;
  441. HttpObjectDB(final URL b) {
  442. objectsUrl = b;
  443. }
  444. @Override
  445. URIish getURI() {
  446. return new URIish(objectsUrl);
  447. }
  448. @Override
  449. Collection<WalkRemoteObjectDatabase> getAlternates() throws IOException {
  450. try {
  451. return readAlternates(INFO_HTTP_ALTERNATES);
  452. } catch (FileNotFoundException err) {
  453. // Fall through.
  454. }
  455. try {
  456. return readAlternates(INFO_ALTERNATES);
  457. } catch (FileNotFoundException err) {
  458. // Fall through.
  459. }
  460. return null;
  461. }
  462. @Override
  463. WalkRemoteObjectDatabase openAlternate(final String location)
  464. throws IOException {
  465. return new HttpObjectDB(new URL(objectsUrl, location));
  466. }
  467. @Override
  468. Collection<String> getPackNames() throws IOException {
  469. final Collection<String> packs = new ArrayList<String>();
  470. try {
  471. final BufferedReader br = openReader(INFO_PACKS);
  472. try {
  473. for (;;) {
  474. final String s = br.readLine();
  475. if (s == null || s.length() == 0)
  476. break;
  477. if (!s.startsWith("P pack-") || !s.endsWith(".pack")) //$NON-NLS-1$ //$NON-NLS-2$
  478. throw invalidAdvertisement(s);
  479. packs.add(s.substring(2));
  480. }
  481. return packs;
  482. } finally {
  483. br.close();
  484. }
  485. } catch (FileNotFoundException err) {
  486. return packs;
  487. }
  488. }
  489. @Override
  490. FileStream open(final String path) throws IOException {
  491. final URL base = objectsUrl;
  492. final URL u = new URL(base, path);
  493. final HttpURLConnection c = httpOpen(u);
  494. switch (HttpSupport.response(c)) {
  495. case HttpURLConnection.HTTP_OK:
  496. final InputStream in = openInputStream(c);
  497. final int len = c.getContentLength();
  498. return new FileStream(in, len);
  499. case HttpURLConnection.HTTP_NOT_FOUND:
  500. throw new FileNotFoundException(u.toString());
  501. default:
  502. throw new IOException(u.toString() + ": " //$NON-NLS-1$
  503. + HttpSupport.response(c) + " " //$NON-NLS-1$
  504. + c.getResponseMessage());
  505. }
  506. }
  507. Map<String, Ref> readAdvertisedImpl(final BufferedReader br)
  508. throws IOException, PackProtocolException {
  509. final TreeMap<String, Ref> avail = new TreeMap<String, Ref>();
  510. for (;;) {
  511. String line = br.readLine();
  512. if (line == null)
  513. break;
  514. final int tab = line.indexOf('\t');
  515. if (tab < 0)
  516. throw invalidAdvertisement(line);
  517. String name;
  518. final ObjectId id;
  519. name = line.substring(tab + 1);
  520. id = ObjectId.fromString(line.substring(0, tab));
  521. if (name.endsWith("^{}")) { //$NON-NLS-1$
  522. name = name.substring(0, name.length() - 3);
  523. final Ref prior = avail.get(name);
  524. if (prior == null)
  525. throw outOfOrderAdvertisement(name);
  526. if (prior.getPeeledObjectId() != null)
  527. throw duplicateAdvertisement(name + "^{}"); //$NON-NLS-1$
  528. avail.put(name, new ObjectIdRef.PeeledTag(
  529. Ref.Storage.NETWORK, name,
  530. prior.getObjectId(), id));
  531. } else {
  532. Ref prior = avail.put(name, new ObjectIdRef.PeeledNonTag(
  533. Ref.Storage.NETWORK, name, id));
  534. if (prior != null)
  535. throw duplicateAdvertisement(name);
  536. }
  537. }
  538. return avail;
  539. }
  540. private PackProtocolException outOfOrderAdvertisement(final String n) {
  541. return new PackProtocolException(MessageFormat.format(JGitText.get().advertisementOfCameBefore, n, n));
  542. }
  543. private PackProtocolException invalidAdvertisement(final String n) {
  544. return new PackProtocolException(MessageFormat.format(JGitText.get().invalidAdvertisementOf, n));
  545. }
  546. private PackProtocolException duplicateAdvertisement(final String n) {
  547. return new PackProtocolException(MessageFormat.format(JGitText.get().duplicateAdvertisementsOf, n));
  548. }
  549. @Override
  550. void close() {
  551. // We do not maintain persistent connections.
  552. }
  553. }
  554. class SmartHttpFetchConnection extends BasePackFetchConnection {
  555. SmartHttpFetchConnection(final InputStream advertisement)
  556. throws TransportException {
  557. super(TransportHttp.this);
  558. statelessRPC = true;
  559. init(advertisement, DisabledOutputStream.INSTANCE);
  560. outNeedsEnd = false;
  561. readAdvertisedRefs();
  562. }
  563. @Override
  564. protected void doFetch(final ProgressMonitor monitor,
  565. final Collection<Ref> want, final Set<ObjectId> have)
  566. throws TransportException {
  567. final Service svc = new Service(SVC_UPLOAD_PACK);
  568. init(svc.in, svc.out);
  569. super.doFetch(monitor, want, have);
  570. }
  571. }
  572. class SmartHttpPushConnection extends BasePackPushConnection {
  573. SmartHttpPushConnection(final InputStream advertisement)
  574. throws TransportException {
  575. super(TransportHttp.this);
  576. statelessRPC = true;
  577. init(advertisement, DisabledOutputStream.INSTANCE);
  578. outNeedsEnd = false;
  579. readAdvertisedRefs();
  580. }
  581. protected void doPush(final ProgressMonitor monitor,
  582. final Map<String, RemoteRefUpdate> refUpdates)
  583. throws TransportException {
  584. final Service svc = new Service(SVC_RECEIVE_PACK);
  585. init(svc.in, svc.out);
  586. super.doPush(monitor, refUpdates);
  587. }
  588. }
  589. /**
  590. * State required to speak multiple HTTP requests with the remote.
  591. * <p>
  592. * A service wrapper provides a normal looking InputStream and OutputStream
  593. * pair which are connected via HTTP to the named remote service. Writing to
  594. * the OutputStream is buffered until either the buffer overflows, or
  595. * reading from the InputStream occurs. If overflow occurs HTTP/1.1 and its
  596. * chunked transfer encoding is used to stream the request data to the
  597. * remote service. If the entire request fits in the memory buffer, the
  598. * older HTTP/1.0 standard and a fixed content length is used instead.
  599. * <p>
  600. * It is an error to attempt to read without there being outstanding data
  601. * ready for transmission on the OutputStream.
  602. * <p>
  603. * No state is preserved between write-read request pairs. The caller is
  604. * responsible for replaying state vector information as part of the request
  605. * data written to the OutputStream. Any session HTTP cookies may or may not
  606. * be preserved between requests, it is left up to the JVM's implementation
  607. * of the HTTP client.
  608. */
  609. class Service {
  610. private final String serviceName;
  611. private final String requestType;
  612. private final String responseType;
  613. private final HttpExecuteStream execute;
  614. final UnionInputStream in;
  615. final HttpOutputStream out;
  616. HttpURLConnection conn;
  617. Service(final String serviceName) {
  618. this.serviceName = serviceName;
  619. this.requestType = "application/x-" + serviceName + "-request"; //$NON-NLS-1$ //$NON-NLS-2$
  620. this.responseType = "application/x-" + serviceName + "-result"; //$NON-NLS-1$ //$NON-NLS-2$
  621. this.execute = new HttpExecuteStream();
  622. this.in = new UnionInputStream(execute);
  623. this.out = new HttpOutputStream();
  624. }
  625. void openStream() throws IOException {
  626. conn = httpOpen(METHOD_POST, new URL(baseUrl, serviceName));
  627. conn.setInstanceFollowRedirects(false);
  628. conn.setDoOutput(true);
  629. conn.setRequestProperty(HDR_CONTENT_TYPE, requestType);
  630. conn.setRequestProperty(HDR_ACCEPT, responseType);
  631. }
  632. void execute() throws IOException {
  633. out.close();
  634. if (conn == null) {
  635. // Output hasn't started yet, because everything fit into
  636. // our request buffer. Send with a Content-Length header.
  637. //
  638. if (out.length() == 0) {
  639. throw new TransportException(uri,
  640. JGitText.get().startingReadStageWithoutWrittenRequestDataPendingIsNotSupported);
  641. }
  642. // Try to compress the content, but only if that is smaller.
  643. TemporaryBuffer buf = new TemporaryBuffer.Heap(http.postBuffer);
  644. try {
  645. GZIPOutputStream gzip = new GZIPOutputStream(buf);
  646. out.writeTo(gzip, null);
  647. gzip.close();
  648. if (out.length() < buf.length())
  649. buf = out;
  650. } catch (IOException err) {
  651. // Most likely caused by overflowing the buffer, meaning
  652. // its larger if it were compressed. Don't compress.
  653. buf = out;
  654. }
  655. openStream();
  656. if (buf != out)
  657. conn.setRequestProperty(HDR_CONTENT_ENCODING, ENCODING_GZIP);
  658. conn.setFixedLengthStreamingMode((int) buf.length());
  659. final OutputStream httpOut = conn.getOutputStream();
  660. try {
  661. buf.writeTo(httpOut, null);
  662. } finally {
  663. httpOut.close();
  664. }
  665. }
  666. out.reset();
  667. final int status = HttpSupport.response(conn);
  668. if (status != HttpURLConnection.HTTP_OK) {
  669. throw new TransportException(uri, status + " " //$NON-NLS-1$
  670. + conn.getResponseMessage());
  671. }
  672. final String contentType = conn.getContentType();
  673. if (!responseType.equals(contentType)) {
  674. conn.getInputStream().close();
  675. throw wrongContentType(responseType, contentType);
  676. }
  677. in.add(openInputStream(conn));
  678. in.add(execute);
  679. conn = null;
  680. }
  681. class HttpOutputStream extends TemporaryBuffer {
  682. HttpOutputStream() {
  683. super(http.postBuffer);
  684. }
  685. @Override
  686. protected OutputStream overflow() throws IOException {
  687. openStream();
  688. conn.setChunkedStreamingMode(0);
  689. return conn.getOutputStream();
  690. }
  691. }
  692. class HttpExecuteStream extends InputStream {
  693. public int available() throws IOException {
  694. execute();
  695. return 0;
  696. }
  697. public int read() throws IOException {
  698. execute();
  699. return -1;
  700. }
  701. public int read(byte[] b, int off, int len) throws IOException {
  702. execute();
  703. return -1;
  704. }
  705. public long skip(long n) throws IOException {
  706. execute();
  707. return 0;
  708. }
  709. }
  710. }
  711. private static class DummyX509TrustManager implements X509TrustManager {
  712. public X509Certificate[] getAcceptedIssuers() {
  713. return null;
  714. }
  715. public void checkClientTrusted(X509Certificate[] certs, String authType) {
  716. // no check
  717. }
  718. public void checkServerTrusted(X509Certificate[] certs, String authType) {
  719. // no check
  720. }
  721. }
  722. }