Skip to content

Commit 4ea60b5

Browse files
authored
HADOOP-17870. Http Filesystem to qualify relative paths. (#3338)
Contributed by Yellowflash
1 parent 164608b commit 4ea60b5

File tree

2 files changed

+36
-13
lines changed

2 files changed

+36
-13
lines changed

hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/fs/http/AbstractHttpFileSystem.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,8 @@ public URI getUri() {
6060

6161
@Override
6262
public FSDataInputStream open(Path path, int bufferSize) throws IOException {
63-
URLConnection conn = path.toUri().toURL().openConnection();
63+
URI pathUri = makeQualified(path).toUri();
64+
URLConnection conn = pathUri.toURL().openConnection();
6465
InputStream in = conn.getInputStream();
6566
return new FSDataInputStream(new HttpDataInputStream(in));
6667
}
@@ -111,7 +112,7 @@ public boolean mkdirs(Path path, FsPermission fsPermission)
111112

112113
@Override
113114
public FileStatus getFileStatus(Path path) throws IOException {
114-
return new FileStatus(-1, false, 1, DEFAULT_BLOCK_SIZE, 0, path);
115+
return new FileStatus(-1, false, 1, DEFAULT_BLOCK_SIZE, 0, makeQualified(path));
115116
}
116117

117118
/**

hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/fs/http/TestHttpFileSystem.java

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,42 +26,64 @@
2626
import org.apache.hadoop.fs.Path;
2727
import org.apache.hadoop.io.IOUtils;
2828
import org.junit.Test;
29+
import org.junit.jupiter.api.BeforeEach;
2930

3031
import java.io.IOException;
3132
import java.io.InputStream;
3233
import java.net.URI;
3334
import java.net.URISyntaxException;
3435
import java.net.URL;
3536
import java.nio.charset.StandardCharsets;
37+
import java.util.stream.IntStream;
3638

3739
import static org.junit.Assert.assertEquals;
3840

3941
/**
4042
* Testing HttpFileSystem.
4143
*/
4244
public class TestHttpFileSystem {
45+
private final Configuration conf = new Configuration(false);
46+
47+
@BeforeEach
48+
public void setUp() {
49+
conf.set("fs.http.impl", HttpFileSystem.class.getCanonicalName());
50+
}
51+
4352
@Test
4453
public void testHttpFileSystem() throws IOException, URISyntaxException,
4554
InterruptedException {
46-
Configuration conf = new Configuration(false);
47-
conf.set("fs.http.impl", HttpFileSystem.class.getCanonicalName());
4855
final String data = "foo";
49-
5056
try (MockWebServer server = new MockWebServer()) {
51-
server.enqueue(new MockResponse().setBody(data));
57+
IntStream.rangeClosed(1, 3).forEach(i -> server.enqueue(new MockResponse().setBody(data)));
5258
server.start();
5359
URI uri = URI.create(String.format("http://%s:%d", server.getHostName(),
5460
server.getPort()));
5561
FileSystem fs = FileSystem.get(uri, conf);
56-
try (InputStream is = fs.open(
57-
new Path(new URL(uri.toURL(), "/foo").toURI()),
58-
4096)) {
59-
byte[] buf = new byte[data.length()];
60-
IOUtils.readFully(is, buf, 0, buf.length);
61-
assertEquals(data, new String(buf, StandardCharsets.UTF_8));
62-
}
62+
assertSameData(fs, new Path(new URL(uri.toURL(), "/foo").toURI()), data);
63+
assertSameData(fs, new Path("/foo"), data);
64+
assertSameData(fs, new Path("foo"), data);
6365
RecordedRequest req = server.takeRequest();
6466
assertEquals("/foo", req.getPath());
6567
}
6668
}
69+
70+
@Test
71+
public void testHttpFileStatus() throws IOException, URISyntaxException, InterruptedException {
72+
URI uri = new URI("http://www.example.com");
73+
FileSystem fs = FileSystem.get(uri, conf);
74+
URI expectedUri = uri.resolve("/foo");
75+
assertEquals(fs.getFileStatus(new Path(new Path(uri), "/foo")).getPath().toUri(), expectedUri);
76+
assertEquals(fs.getFileStatus(new Path("/foo")).getPath().toUri(), expectedUri);
77+
assertEquals(fs.getFileStatus(new Path("foo")).getPath().toUri(), expectedUri);
78+
}
79+
80+
private void assertSameData(FileSystem fs, Path path, String data) throws IOException {
81+
try (InputStream is = fs.open(
82+
path,
83+
4096)) {
84+
byte[] buf = new byte[data.length()];
85+
IOUtils.readFully(is, buf, 0, buf.length);
86+
assertEquals(data, new String(buf, StandardCharsets.UTF_8));
87+
}
88+
}
6789
}

0 commit comments

Comments
 (0)