Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support reading empty file payload #976

Merged
merged 1 commit into from
Apr 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import java.util.function.LongUnaryOperator;
import java.util.function.Supplier;

import org.agrona.BitUtil;
import org.agrona.DirectBuffer;
import org.agrona.MutableDirectBuffer;
import org.agrona.collections.Long2ObjectHashMap;
Expand Down Expand Up @@ -385,9 +386,7 @@ private String calculateTag()
{
final byte[] readArray = readBuffer.byteArray();
int bytesRead = input.read(readArray, 0, readArray.length);
byte[] content = new byte[bytesRead];
readBuffer.getBytes(0, content, 0, bytesRead);
newTag = calculateHash(content);
newTag = calculateHash(readArray, 0, Math.max(bytesRead, 0));
}
}
catch (IOException ex)
Expand All @@ -412,16 +411,14 @@ private InputStream getInputStream()
}

private String calculateHash(
byte[] content)
byte[] input,
int offset,
int length)
{

byte[] hash = md5.digest(content);
StringBuilder sb = new StringBuilder(hash.length * 2);
for (byte b: hash)
{
sb.append(String.format("%02x", b));
}
return sb.toString();
md5.reset();
md5.update(input, offset, length);
byte[] hash = md5.digest();
return BitUtil.toHex(hash);
}

private void onAppEnd(
Expand Down Expand Up @@ -631,12 +628,13 @@ private void flushAppData(
if (replyWin > 0)
{
InputStream input = getInputStream();
boolean replyClosable = input == null;
if (input != null)
try
{
try
int available = input != null ? input.available() : 0;

if (available > 0)
{
int reserved = Math.min(replyWin, input.available() + replyPad);
int reserved = Math.min(replyWin, available + replyPad);
int length = Math.max(reserved - replyPad, 0);

if (length > 0 && replyDebIndex != NO_DEBITOR_INDEX && replyDeb != null)
Expand All @@ -657,25 +655,25 @@ private void flushAppData(
doAppData(traceId, reserved, payload);

replyBytes += bytesRead;
replyClosable = replyBytes == attributes.size();

if (replyBytes == attributes.size())
{
input.close();
input = null;
}
}
}
if (replyClosable)
{
input.close();
doAppEnd(traceId);
}
}
catch (IOException ex)

if (available <= 0 || input == null)
{
doAppAbort(traceId);
doAppEnd(traceId);
}
}
else
catch (IOException ex)
{
doAppEnd(traceId);
doAppAbort(traceId);
}

}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,16 @@ public void shouldReadFileExtensionDefault() throws Exception
k3po.finish();
}

@Test
@Configuration("server.yaml")
@Specification({
"${app}/read.file.payload.empty/client",
})
public void shouldReadFilePayloadEmpty() throws Exception
{
k3po.finish();
}

@Test
@Configuration("server.yaml")
@Specification({
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#
# Copyright 2021-2023 Aklivity Inc
#
# Licensed under the Aklivity Community License (the "License"); you may not use
# this file except in compliance with the License. You may obtain a copy of the
# License at
#
# https://www.aklivity.io/aklivity-community-license/
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OF ANY KIND, either express or implied. See the License for the
# specific language governing permissions and limitations under the License.
#

connect "zilla://streams/app0"
option zilla:window 8192
option zilla:transmission "half-duplex"

write zilla:begin.ext ${filesystem:beginEx()
.typeId(zilla:id("filesystem"))
.capabilities("READ_PAYLOAD")
.path("empty.txt")
.build()}
connected

write close

read zilla:begin.ext ${filesystem:matchBeginEx()
.typeId(zilla:id("filesystem"))
.capabilities("READ_PAYLOAD")
.path("empty.txt")
.type("text/plain")
.payloadSize(0)
.tag("d41d8cd98f00b204e9800998ecf8427e")
.build()}

read closed
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#
# Copyright 2021-2023 Aklivity Inc
#
# Licensed under the Aklivity Community License (the "License"); you may not use
# this file except in compliance with the License. You may obtain a copy of the
# License at
#
# https://www.aklivity.io/aklivity-community-license/
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OF ANY KIND, either express or implied. See the License for the
# specific language governing permissions and limitations under the License.
#

accept "zilla://streams/app0"
option zilla:window 8192
option zilla:transmission "half-duplex"
accepted

read zilla:begin.ext ${filesystem:matchBeginEx()
.typeId(zilla:id("filesystem"))
.capabilities("READ_PAYLOAD")
.path("empty.txt")
.build()}
connected

read closed

write zilla:begin.ext ${filesystem:beginEx()
.typeId(zilla:id("filesystem"))
.capabilities("READ_PAYLOAD")
.path("empty.txt")
.type("text/plain")
.payloadSize(0)
.tag("d41d8cd98f00b204e9800998ecf8427e")
.build()}
write flush

write close
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,16 @@ public void shouldReadFileExtensionDefault() throws Exception
k3po.finish();
}

@Test
@Specification({
"${app}/read.file.payload.empty/client",
"${app}/read.file.payload.empty/server",
})
public void shouldReadFilePayloadEmpty() throws Exception
{
k3po.finish();
}

@Test
@Specification({
"${app}/read.file.payload/client",
Expand Down