Skip to content

Commit 1fdfaeb

Browse files
committed
HDFS-15275. HttpFS: Response of Create was not correct with noredirect and data are true. Contributed by hemanthboyina.
1 parent 93b662d commit 1fdfaeb

File tree

2 files changed

+65
-22
lines changed

2 files changed

+65
-22
lines changed

hadoop-hdfs-project/hadoop-hdfs-httpfs/src/main/java/org/apache/hadoop/fs/http/server/HttpFSServer.java

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -585,23 +585,21 @@ public Response post(InputStream is,
585585
switch (op.value()) {
586586
case APPEND: {
587587
Boolean hasData = params.get(DataParam.NAME, DataParam.class);
588-
if (!hasData) {
589-
URI redirectURL = createUploadRedirectionURL(
590-
uriInfo, HttpFSFileSystem.Operation.APPEND);
591-
Boolean noRedirect = params.get(
592-
NoRedirectParam.NAME, NoRedirectParam.class);
593-
if (noRedirect) {
588+
URI redirectURL = createUploadRedirectionURL(uriInfo,
589+
HttpFSFileSystem.Operation.APPEND);
590+
Boolean noRedirect =
591+
params.get(NoRedirectParam.NAME, NoRedirectParam.class);
592+
if (noRedirect) {
594593
final String js = JsonUtil.toJsonString("Location", redirectURL);
595594
response = Response.ok(js).type(MediaType.APPLICATION_JSON).build();
596-
} else {
597-
response = Response.temporaryRedirect(redirectURL).build();
598-
}
599-
} else {
595+
} else if (hasData) {
600596
FSOperations.FSAppend command =
601597
new FSOperations.FSAppend(is, path);
602598
fsExecute(user, command);
603599
AUDIT_LOG.info("[{}]", path);
604600
response = Response.ok().type(MediaType.APPLICATION_JSON).build();
601+
} else {
602+
response = Response.temporaryRedirect(redirectURL).build();
605603
}
606604
break;
607605
}
@@ -662,7 +660,8 @@ public Response post(InputStream is,
662660
protected URI createUploadRedirectionURL(UriInfo uriInfo, Enum<?> uploadOperation) {
663661
UriBuilder uriBuilder = uriInfo.getRequestUriBuilder();
664662
uriBuilder = uriBuilder.replaceQueryParam(OperationParam.NAME, uploadOperation).
665-
queryParam(DataParam.NAME, Boolean.TRUE);
663+
queryParam(DataParam.NAME, Boolean.TRUE)
664+
.replaceQueryParam(NoRedirectParam.NAME, (Object[]) null);
666665
return uriBuilder.build(null);
667666
}
668667

@@ -726,18 +725,14 @@ public Response put(InputStream is,
726725
switch (op.value()) {
727726
case CREATE: {
728727
Boolean hasData = params.get(DataParam.NAME, DataParam.class);
729-
if (!hasData) {
730-
URI redirectURL = createUploadRedirectionURL(
731-
uriInfo, HttpFSFileSystem.Operation.CREATE);
732-
Boolean noRedirect = params.get(
733-
NoRedirectParam.NAME, NoRedirectParam.class);
734-
if (noRedirect) {
728+
URI redirectURL = createUploadRedirectionURL(uriInfo,
729+
HttpFSFileSystem.Operation.CREATE);
730+
Boolean noRedirect =
731+
params.get(NoRedirectParam.NAME, NoRedirectParam.class);
732+
if (noRedirect) {
735733
final String js = JsonUtil.toJsonString("Location", redirectURL);
736734
response = Response.ok(js).type(MediaType.APPLICATION_JSON).build();
737-
} else {
738-
response = Response.temporaryRedirect(redirectURL).build();
739-
}
740-
} else {
735+
} else if (hasData) {
741736
Short permission = params.get(PermissionParam.NAME,
742737
PermissionParam.class);
743738
Short unmaskedPermission = params.get(UnmaskedPermissionParam.NAME,
@@ -761,6 +756,8 @@ public Response put(InputStream is,
761756
"Location", uriInfo.getAbsolutePath());
762757
response = Response.created(uriInfo.getAbsolutePath())
763758
.type(MediaType.APPLICATION_JSON).entity(js).build();
759+
} else {
760+
response = Response.temporaryRedirect(redirectURL).build();
764761
}
765762
break;
766763
}

hadoop-hdfs-project/hadoop-hdfs-httpfs/src/test/java/org/apache/hadoop/fs/http/server/TestHttpFSServer.java

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1565,7 +1565,7 @@ public void testNoRedirect() throws Exception {
15651565
new InputStreamReader(conn.getInputStream()));
15661566
String location = (String)json.get("Location");
15671567
Assert.assertTrue(location.contains(DataParam.NAME));
1568-
Assert.assertTrue(location.contains(NoRedirectParam.NAME));
1568+
Assert.assertFalse(location.contains(NoRedirectParam.NAME));
15691569
Assert.assertTrue(location.contains("CREATE"));
15701570
Assert.assertTrue("Wrong location: " + location,
15711571
location.startsWith(TestJettyHelper.getJettyURL().toString()));
@@ -1834,4 +1834,50 @@ public void testStoragePolicySatisfier() throws Exception {
18341834
assertTrue(
18351835
xAttrs.containsKey(HdfsServerConstants.XATTR_SATISFY_STORAGE_POLICY));
18361836
}
1837+
1838+
@Test
1839+
@TestDir
1840+
@TestJetty
1841+
@TestHdfs
1842+
public void testNoRedirectWithData() throws Exception {
1843+
createHttpFSServer(false, false);
1844+
1845+
final String path = "/file";
1846+
final String username = HadoopUsersConfTestHelper.getHadoopUsers()[0];
1847+
// file creation which should not redirect
1848+
URL url = new URL(TestJettyHelper.getJettyURL(),
1849+
MessageFormat.format(
1850+
"/webhdfs/v1{0}?user.name={1}&op=CREATE&data=true&noredirect=true",
1851+
path, username));
1852+
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
1853+
conn.setRequestMethod(HttpMethod.PUT);
1854+
conn.setRequestProperty("Content-Type", MediaType.APPLICATION_OCTET_STREAM);
1855+
conn.setDoOutput(true);
1856+
conn.connect();
1857+
Assert.assertEquals(HttpURLConnection.HTTP_OK, conn.getResponseCode());
1858+
JSONObject json = (JSONObject) new JSONParser()
1859+
.parse(new InputStreamReader(conn.getInputStream()));
1860+
1861+
// get the location to write
1862+
String location = (String) json.get("Location");
1863+
Assert.assertTrue(location.contains(DataParam.NAME));
1864+
Assert.assertTrue(location.contains("CREATE"));
1865+
url = new URL(location);
1866+
conn = (HttpURLConnection) url.openConnection();
1867+
conn.setRequestMethod(HttpMethod.PUT);
1868+
conn.setRequestProperty("Content-Type", MediaType.APPLICATION_OCTET_STREAM);
1869+
conn.setDoOutput(true);
1870+
conn.connect();
1871+
final String writeStr = "write some content";
1872+
OutputStream os = conn.getOutputStream();
1873+
os.write(writeStr.getBytes());
1874+
os.close();
1875+
// Verify that file got created
1876+
Assert.assertEquals(HttpURLConnection.HTTP_CREATED, conn.getResponseCode());
1877+
json = (JSONObject) new JSONParser()
1878+
.parse(new InputStreamReader(conn.getInputStream()));
1879+
location = (String) json.get("Location");
1880+
Assert.assertEquals(TestJettyHelper.getJettyURL() + "/webhdfs/v1" + path,
1881+
location);
1882+
}
18371883
}

0 commit comments

Comments
 (0)