@@ -21,6 +21,7 @@ public enum PinType {all, direct, indirect, recursive}
21
21
22
22
public final String host ;
23
23
public final int port ;
24
+ public final String protocol ;
24
25
private final String version ;
25
26
public final Key key = new Key ();
26
27
public final Pin pin = new Pin ();
@@ -39,22 +40,29 @@ public enum PinType {all, direct, indirect, recursive}
39
40
public final Stats stats = new Stats ();
40
41
public final Name name = new Name ();
41
42
public final Pubsub pubsub = new Pubsub ();
42
-
43
+
43
44
public IPFS (String host , int port ) {
44
- this (host , port , "/api/v0/" );
45
+ this (host , port , "/api/v0/" , false );
45
46
}
46
47
47
48
public IPFS (String multiaddr ) {
48
49
this (new MultiAddress (multiaddr ));
49
50
}
50
51
51
52
public IPFS (MultiAddress addr ) {
52
- this (addr .getHost (), addr .getTCPPort (), "/api/v0/" );
53
+ this (addr .getHost (), addr .getTCPPort (), "/api/v0/" , detectSSL ( addr ) );
53
54
}
54
55
55
- public IPFS (String host , int port , String version ) {
56
+ public IPFS (String host , int port , String version , boolean ssl ) {
56
57
this .host = host ;
57
58
this .port = port ;
59
+
60
+ if (ssl ) {
61
+ this .protocol = "https" ;
62
+ } else {
63
+ this .protocol = "http" ;
64
+ }
65
+
58
66
this .version = version ;
59
67
// Check IPFS is sufficiently recent
60
68
try {
@@ -65,7 +73,7 @@ public IPFS(String host, int port, String version) {
65
73
throw new RuntimeException (e );
66
74
}
67
75
}
68
-
76
+
69
77
public List <MerkleNode > add (NamedStreamable file ) throws IOException {
70
78
return add (file , false );
71
79
}
@@ -79,7 +87,7 @@ public List<MerkleNode> add(NamedStreamable file, boolean wrap, boolean hashOnly
79
87
}
80
88
81
89
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" );
83
91
for (NamedStreamable file : files ) {
84
92
if (file .isDirectory ()) {
85
93
m .addSubtree (Paths .get ("" ), file );
@@ -285,7 +293,7 @@ public List<MerkleNode> put(List<byte[]> data, Optional<String> format) throws I
285
293
286
294
public MerkleNode put (byte [] data , Optional <String > format ) {
287
295
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" );
289
297
m .addFilePart ("file" , Paths .get ("" ), new NamedStreamable .ByteArrayWrapper (data ));
290
298
String res = m .finish ();
291
299
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 {
300
308
*/
301
309
public class IPFSObject {
302
310
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" );
304
312
for (byte [] f : data )
305
313
m .addFilePart ("file" , Paths .get ("" ), new NamedStreamable .ByteArrayWrapper (f ));
306
314
String res = m .finish ();
@@ -310,7 +318,7 @@ public List<MerkleNode> put(List<byte[]> data) throws IOException {
310
318
public List <MerkleNode > put (String encoding , List <byte []> data ) throws IOException {
311
319
if (!"json" .equals (encoding ) && !"protobuf" .equals (encoding ))
312
320
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" );
314
322
for (byte [] f : data )
315
323
m .addFilePart ("file" , Paths .get ("" ), new NamedStreamable .ByteArrayWrapper (f ));
316
324
String res = m .finish ();
@@ -364,7 +372,7 @@ public MerkleNode patch(Multihash base, String command, Optional<byte[]> data, O
364
372
case "append-data" :
365
373
if (!data .isPresent ())
366
374
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" );
368
376
m .addFilePart ("file" , Paths .get ("" ), new NamedStreamable .ByteArrayWrapper (data .get ()));
369
377
String res = m .finish ();
370
378
return MerkleNode .fromJSON (JSONParser .parse (res ));
@@ -502,7 +510,7 @@ public MerkleNode put(byte[] object, String outputFormat) throws IOException {
502
510
503
511
public MerkleNode put (String inputFormat , byte [] object , String outputFormat ) throws IOException {
504
512
block .put (Arrays .asList (object ));
505
- String prefix = "http ://" + host + ":" + port + version ;
513
+ String prefix = protocol + " ://" + host + ":" + port + version ;
506
514
Multipart m = new Multipart (prefix + "block/put/?stream-channels=true&input-enc=" + inputFormat + "&f=" + outputFormat , "UTF-8" );
507
515
m .addFilePart ("file" , Paths .get ("" ), new NamedStreamable .ByteArrayWrapper (object ));
508
516
String res = m .finish ();
@@ -558,7 +566,7 @@ public Map show() throws IOException {
558
566
}
559
567
560
568
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" );
562
570
m .addFilePart ("file" , Paths .get ("" ), file );
563
571
String res = m .finish ();
564
572
}
@@ -629,7 +637,7 @@ private void retrieveAndParseStream(String path, Consumer<Object> results, Consu
629
637
}
630
638
631
639
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 );
633
641
return IPFS .get (target );
634
642
}
635
643
@@ -688,7 +696,7 @@ private static InputStream getStream(URL target) throws IOException {
688
696
}
689
697
690
698
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 );
692
700
return (Map ) JSONParser .parse (new String (post (target , body , headers )));
693
701
}
694
702
@@ -716,4 +724,8 @@ private static final byte[] readFully(InputStream in) throws IOException {
716
724
resp .write (buf , 0 , r );
717
725
return resp .toByteArray ();
718
726
}
727
+
728
+ private static boolean detectSSL (MultiAddress multiaddress ) {
729
+ return multiaddress .toString ().contains ("/https" );
730
+ }
719
731
}
0 commit comments