Skip to content

Commit

Permalink
Fix "direct buffer access" issue for Plugin SDK (#2511)
Browse files Browse the repository at this point in the history
* Add test for direct buffer in request

* Safely access request byte buffer

* Fix formatting

---------

Co-authored-by: Naman Nandan <namankt55@gmail.com>
  • Loading branch information
marrodion and namannandan committed Aug 4, 2023
1 parent 53ff6a5 commit 34617bb
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.util.List;
import java.util.Map;
import org.pytorch.serve.servingsdk.http.Request;
import org.pytorch.serve.util.NettyUtils;

public class ModelServerRequest implements Request {
private FullHttpRequest req;
Expand Down Expand Up @@ -45,6 +46,6 @@ public String getContentType() {

@Override
public ByteArrayInputStream getInputStream() {
return new ByteArrayInputStream(req.content().array());
return new ByteArrayInputStream(NettyUtils.getBytes(req.content()));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package org.pytorch.serve.util;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.handler.codec.http.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import org.pytorch.serve.servingsdk.http.Request;
import org.pytorch.serve.servingsdk.impl.ModelServerRequest;
import org.testng.Assert;
import org.testng.annotations.Test;

public class PluginSdkTest {
@Test
public void testReadRequestBodyFromPlugin() throws IOException {
ByteBuf buf = Unpooled.directBuffer();
buf.writeBytes("test".getBytes());
FullHttpRequest req =
new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, "", buf);
Request request = new ModelServerRequest(req, new QueryStringDecoder(""));
String line =
new BufferedReader(new InputStreamReader(request.getInputStream())).readLine();
Assert.assertEquals(line, "test");
}
}
1 change: 1 addition & 0 deletions frontend/server/testng.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<classes>
<class name="org.pytorch.serve.util.ConfigManagerTest"/>
<class name="org.pytorch.serve.util.ConnectorTest"/>
<class name="org.pytorch.serve.util.PluginSdkTest"/>
<class name="org.pytorch.serve.CoverageTest"/>
<class name="org.pytorch.serve.ModelServerTest"/>
<class name="org.pytorch.serve.SnapshotTest"/>
Expand Down

0 comments on commit 34617bb

Please sign in to comment.