Skip to content

Commit ce812cf

Browse files
authored
Merge pull request #65 from gjeanmart/master
Support for SSL API endpoint such as Infura (https://ipfs.infura.io/)
2 parents 6cd98d1 + 9b4e09f commit ce812cf

File tree

1 file changed

+26
-14
lines changed

1 file changed

+26
-14
lines changed

src/main/java/io/ipfs/api/IPFS.java

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ public enum PinType {all, direct, indirect, recursive}
2121

2222
public final String host;
2323
public final int port;
24+
public final String protocol;
2425
private final String version;
2526
public final Key key = new Key();
2627
public final Pin pin = new Pin();
@@ -39,22 +40,29 @@ public enum PinType {all, direct, indirect, recursive}
3940
public final Stats stats = new Stats();
4041
public final Name name = new Name();
4142
public final Pubsub pubsub = new Pubsub();
42-
43+
4344
public IPFS(String host, int port) {
44-
this(host, port, "/api/v0/");
45+
this(host, port, "/api/v0/", false);
4546
}
4647

4748
public IPFS(String multiaddr) {
4849
this(new MultiAddress(multiaddr));
4950
}
5051

5152
public IPFS(MultiAddress addr) {
52-
this(addr.getHost(), addr.getTCPPort(), "/api/v0/");
53+
this(addr.getHost(), addr.getTCPPort(), "/api/v0/", detectSSL(addr));
5354
}
5455

55-
public IPFS(String host, int port, String version) {
56+
public IPFS(String host, int port, String version, boolean ssl) {
5657
this.host = host;
5758
this.port = port;
59+
60+
if(ssl) {
61+
this.protocol = "https";
62+
} else {
63+
this.protocol = "http";
64+
}
65+
5866
this.version = version;
5967
// Check IPFS is sufficiently recent
6068
try {
@@ -65,7 +73,7 @@ public IPFS(String host, int port, String version) {
6573
throw new RuntimeException(e);
6674
}
6775
}
68-
76+
6977
public List<MerkleNode> add(NamedStreamable file) throws IOException {
7078
return add(file, false);
7179
}
@@ -79,7 +87,7 @@ public List<MerkleNode> add(NamedStreamable file, boolean wrap, boolean hashOnly
7987
}
8088

8189
public List<MerkleNode> add(List<NamedStreamable> files, boolean wrap, boolean hashOnly) throws IOException {
82-
Multipart m = new Multipart("http://" + host + ":" + port + version + "add?w="+wrap + "&n="+hashOnly, "UTF-8");
90+
Multipart m = new Multipart(protocol + "://" + host + ":" + port + version + "add?w="+wrap + "&n="+hashOnly, "UTF-8");
8391
for (NamedStreamable file: files) {
8492
if (file.isDirectory()) {
8593
m.addSubtree(Paths.get(""), file);
@@ -285,7 +293,7 @@ public List<MerkleNode> put(List<byte[]> data, Optional<String> format) throws I
285293

286294
public MerkleNode put(byte[] data, Optional<String> format) {
287295
String fmt = format.map(f -> "&format=" + f).orElse("");
288-
Multipart m = new Multipart("http://" + host + ":" + port + version+"block/put?stream-channels=true" + fmt, "UTF-8");
296+
Multipart m = new Multipart(protocol +"://" + host + ":" + port + version+"block/put?stream-channels=true" + fmt, "UTF-8");
289297
m.addFilePart("file", Paths.get(""), new NamedStreamable.ByteArrayWrapper(data));
290298
String res = m.finish();
291299
return JSONParser.parseStream(res).stream().map(x -> MerkleNode.fromJSON((Map<String, Object>) x)).findFirst().get();
@@ -300,7 +308,7 @@ public Map stat(Multihash hash) throws IOException {
300308
*/
301309
public class IPFSObject {
302310
public List<MerkleNode> put(List<byte[]> data) throws IOException {
303-
Multipart m = new Multipart("http://" + host + ":" + port + version+"object/put?stream-channels=true", "UTF-8");
311+
Multipart m = new Multipart(protocol +"://" + host + ":" + port + version+"object/put?stream-channels=true", "UTF-8");
304312
for (byte[] f : data)
305313
m.addFilePart("file", Paths.get(""), new NamedStreamable.ByteArrayWrapper(f));
306314
String res = m.finish();
@@ -310,7 +318,7 @@ public List<MerkleNode> put(List<byte[]> data) throws IOException {
310318
public List<MerkleNode> put(String encoding, List<byte[]> data) throws IOException {
311319
if (!"json".equals(encoding) && !"protobuf".equals(encoding))
312320
throw new IllegalArgumentException("Encoding must be json or protobuf");
313-
Multipart m = new Multipart("http://" + host + ":" + port + version+"object/put?stream-channels=true&encoding="+encoding, "UTF-8");
321+
Multipart m = new Multipart(protocol +"://" + host + ":" + port + version+"object/put?stream-channels=true&encoding="+encoding, "UTF-8");
314322
for (byte[] f : data)
315323
m.addFilePart("file", Paths.get(""), new NamedStreamable.ByteArrayWrapper(f));
316324
String res = m.finish();
@@ -364,7 +372,7 @@ public MerkleNode patch(Multihash base, String command, Optional<byte[]> data, O
364372
case "append-data":
365373
if (!data.isPresent())
366374
throw new IllegalStateException("set-data requires data!");
367-
Multipart m = new Multipart("http://" + host + ":" + port + version+"object/patch/"+command+"?arg="+base.toBase58()+"&stream-channels=true", "UTF-8");
375+
Multipart m = new Multipart(protocol +"://" + host + ":" + port + version+"object/patch/"+command+"?arg="+base.toBase58()+"&stream-channels=true", "UTF-8");
368376
m.addFilePart("file", Paths.get(""), new NamedStreamable.ByteArrayWrapper(data.get()));
369377
String res = m.finish();
370378
return MerkleNode.fromJSON(JSONParser.parse(res));
@@ -502,7 +510,7 @@ public MerkleNode put(byte[] object, String outputFormat) throws IOException {
502510

503511
public MerkleNode put(String inputFormat, byte[] object, String outputFormat) throws IOException {
504512
block.put(Arrays.asList(object));
505-
String prefix = "http://" + host + ":" + port + version;
513+
String prefix = protocol +"://" + host + ":" + port + version;
506514
Multipart m = new Multipart(prefix + "block/put/?stream-channels=true&input-enc=" + inputFormat + "&f=" + outputFormat, "UTF-8");
507515
m.addFilePart("file", Paths.get(""), new NamedStreamable.ByteArrayWrapper(object));
508516
String res = m.finish();
@@ -558,7 +566,7 @@ public Map show() throws IOException {
558566
}
559567

560568
public void replace(NamedStreamable file) throws IOException {
561-
Multipart m = new Multipart("http://" + host + ":" + port + version+"config/replace?stream-channels=true", "UTF-8");
569+
Multipart m = new Multipart(protocol +"://" + host + ":" + port + version+"config/replace?stream-channels=true", "UTF-8");
562570
m.addFilePart("file", Paths.get(""), file);
563571
String res = m.finish();
564572
}
@@ -629,7 +637,7 @@ private void retrieveAndParseStream(String path, Consumer<Object> results, Consu
629637
}
630638

631639
private byte[] retrieve(String path) throws IOException {
632-
URL target = new URL("http", host, port, version + path);
640+
URL target = new URL(protocol, host, port, version + path);
633641
return IPFS.get(target);
634642
}
635643

@@ -688,7 +696,7 @@ private static InputStream getStream(URL target) throws IOException {
688696
}
689697

690698
private Map postMap(String path, byte[] body, Map<String, String> headers) throws IOException {
691-
URL target = new URL("http", host, port, version + path);
699+
URL target = new URL(protocol, host, port, version + path);
692700
return (Map) JSONParser.parse(new String(post(target, body, headers)));
693701
}
694702

@@ -716,4 +724,8 @@ private static final byte[] readFully(InputStream in) throws IOException {
716724
resp.write(buf, 0, r);
717725
return resp.toByteArray();
718726
}
727+
728+
private static boolean detectSSL(MultiAddress multiaddress) {
729+
return multiaddress.toString().contains("/https");
730+
}
719731
}

0 commit comments

Comments
 (0)