Mercurial > hg4j
changeset 69:5a69397f0f99
Discovery utility for Hg network protocol finally in the repo, with quick-n-dirty ConfigFile impl that helps to hide auth info
author | Artem Tikhomirov <tikhomirov.artem@gmail.com> |
---|---|
date | Sat, 22 Jan 2011 22:53:57 +0100 |
parents | 0e499fed9b3d |
children | 993f6f8e1314 |
files | src/com/tmate/hgkit/console/Remote.java src/org/tmatesoft/hg/internal/ConfigFile.java |
diffstat | 2 files changed, 203 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/com/tmate/hgkit/console/Remote.java Sat Jan 22 22:53:57 2011 +0100 @@ -0,0 +1,98 @@ +/* + * Copyright (c) 2011 Artem Tikhomirov + */ +package com.tmate.hgkit.console; + +import java.io.File; +import java.io.FileOutputStream; +import java.io.InputStream; +import java.net.URL; +import java.security.cert.CertificateException; +import java.security.cert.X509Certificate; +import java.util.List; +import java.util.Map; +import java.util.prefs.Preferences; +import java.util.zip.InflaterInputStream; + +import javax.net.ssl.HttpsURLConnection; +import javax.net.ssl.SSLContext; +import javax.net.ssl.TrustManager; +import javax.net.ssl.X509TrustManager; + +import org.tmatesoft.hg.internal.ConfigFile; + +/** + * + * @author artem + */ +public class Remote { + + /* + * @see http://mercurial.selenic.com/wiki/WireProtocol + cmd=branches gives 4 nodeids (head, root, first parent, second parent) per line (few lines possible, per branch, perhaps?) + cmd=capabilities gives lookup ...subset and 3 compress methods + // lookup changegroupsubset unbundle=HG10GZ,HG10BZ,HG10UN + cmd=heads gives space-separated list of nodeids (or just one) + nodeids are in hex (printable) format, need to convert fromAscii() + cmd=branchmap + */ + public static void main(String[] args) throws Exception { + String nid = "d6d2a630f4a6d670c90a5ca909150f2b426ec88f"; + ConfigFile cfg = new ConfigFile(); + cfg.addLocation(new File(System.getProperty("user.home"), ".hgrc")); + String svnkitServer = cfg.getSection("paths").get("svnkit"); + URL url = new URL(svnkitServer + "?cmd=changegroup&roots=a78c980749e3ccebb47138b547e9b644a22797a9"); + + SSLContext sslContext = SSLContext.getInstance("SSL"); + class TrustEveryone implements X509TrustManager { + public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException { + System.out.println("checkClientTrusted " + authType); + } + public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException { + System.out.println("checkServerTrusted" + authType); + } + public X509Certificate[] getAcceptedIssuers() { + return new X509Certificate[0]; + } + } + // + Preferences tempNode = Preferences.userRoot().node("xxx"); + tempNode.putByteArray("xxx", url.getUserInfo().getBytes()); + String authInfo = tempNode.get("xxx", null); + tempNode.removeNode(); + // + sslContext.init(null, new TrustManager[] { new TrustEveryone() }, null); + HttpsURLConnection urlConnection = (HttpsURLConnection) url.openConnection(); + urlConnection.addRequestProperty("User-Agent", "jhg/0.1.0"); + urlConnection.addRequestProperty("Accept", "application/mercurial-0.1"); + urlConnection.addRequestProperty("Authorization", "Basic " + authInfo); + urlConnection.setSSLSocketFactory(sslContext.getSocketFactory()); + urlConnection.connect(); + System.out.println("Response headers:"); + final Map<String, List<String>> headerFields = urlConnection.getHeaderFields(); + for (String s : headerFields.keySet()) { + System.out.printf("%s: %s\n", s, urlConnection.getHeaderField(s)); + } + System.out.printf("Content type is %s and its length is %d\n", urlConnection.getContentType(), urlConnection.getContentLength()); + InputStream is = urlConnection.getInputStream(); +// int b; +// while ((b =is.read()) != -1) { +// System.out.print((char) b); +// } +// System.out.println(); + InflaterInputStream zipStream = new InflaterInputStream(is); + File tf = File.createTempFile("hg-bundle-", null); + FileOutputStream fos = new FileOutputStream(tf); + int r; + byte[] buf = new byte[8*1024]; + while ((r = zipStream.read(buf)) != -1) { + fos.write(buf, 0, r); + } + fos.close(); + zipStream.close(); + System.out.println(tf); + + urlConnection.disconnect(); + // + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/org/tmatesoft/hg/internal/ConfigFile.java Sat Jan 22 22:53:57 2011 +0100 @@ -0,0 +1,105 @@ +/* + * Copyright (c) 2011 TMate Software Ltd + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; version 2 of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * For information on how to redistribute this software under + * the terms of a license other than GNU General Public License + * contact TMate Software at support@svnkit.com + */ +package org.tmatesoft.hg.internal; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Collections; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; + +/** + * + * @author Artem Tikhomirov + * @author TMate Software Ltd. + */ +public class ConfigFile { + + private List<String> sections; + private List<Map<String,String>> content; + + public ConfigFile() { + } + + public void addLocation(File path) { + read(path); + } + + public List<String> getSectionNames() { + return Collections.unmodifiableList(sections); + } + + public Map<String,String> getSection(String sectionName) { + int x = sections.indexOf(sectionName); + if (x == -1) { + return Collections.emptyMap(); + } + return Collections.unmodifiableMap(content.get(x)); + } + + private void read(File f) { + if (sections == null) { + sections = new ArrayList<String>(); + content = new ArrayList<Map<String,String>>(); + } + try { + BufferedReader br = new BufferedReader(new FileReader(f)); + String line; + String sectionName = ""; + Map<String,String> section = new LinkedHashMap<String, String>(); + while ((line = br.readLine()) != null) { + line = line.trim(); + if (line.length() <= 2) { // a=b or [a] are at least of length 3 + continue; + } + int x; + if (line.charAt(0) == '[' && line.charAt(line.length() - 1) == ']') { + sectionName = line.substring(1, line.length() - 1); + if (sections.indexOf(sectionName) == -1) { + sections.add(sectionName); + content.add(section = new LinkedHashMap<String, String>()); + } else { + section = null; // drop cached value + } + } else if ((x = line.indexOf('=')) != -1) { + String key = line.substring(0, x).trim(); + String value = line.substring(x+1).trim(); + if (section == null) { + int i = sections.indexOf(sectionName); + assert i >= 0; + section = content.get(i); + } + if (sectionName.length() == 0) { + // add fake section only if there are any values + sections.add(sectionName); + content.add(section); + } + section.put(key, value); + } + } + } catch (IOException ex) { + ex.printStackTrace(); // XXX shall outer world care? + } + ((ArrayList<?>) sections).trimToSize(); + ((ArrayList<?>) content).trimToSize(); + assert sections.size() == content.size(); + } +}