Skip to content

Commit

Permalink
[awscurl] Fix output for server sent event content-type (#1784)
Browse files Browse the repository at this point in the history
  • Loading branch information
frankfliu authored Apr 16, 2024
1 parent 472e117 commit 9719228
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions awscurl/src/main/java/ai/djl/awscurl/HttpClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ public static HttpResponse sendRequest(
ret = new BasicHttpResponse(status);
}
} else if ("text/event-stream".equals(contentType)) {
handleServerSentEvent(is, requestTime, begin, names, tokens, request);
handleServerSentEvent(is, requestTime, begin, names, tokens, request, ps);
} else if ("application/vnd.amazon.eventstream".equals(contentType)) {
Header header = resp.getFirstHeader("X-Amzn-SageMaker-Content-Type");
String realContentType = header == null ? null : header.getValue();
Expand Down Expand Up @@ -210,7 +210,8 @@ private static void handleServerSentEvent(
long begin,
String[] names,
AtomicInteger tokens,
SignableRequest request)
SignableRequest request,
OutputStream ps)
throws IOException {
List<String> list = new ArrayList<>();
try (BufferedReader reader =
Expand All @@ -225,10 +226,14 @@ private static void handleServerSentEvent(
requestTime[1] = System.nanoTime() - begin;
}
line = line.substring(5);
ps.write(line.getBytes(StandardCharsets.UTF_8));
ps.write(new byte[] {'\n'});
JsonElement element = JsonUtils.GSON.fromJson(line, JsonElement.class);
JsonUtils.getJsonList(element, list, names);
}
updateTokenCount(list, tokens, request);
if (tokens != null) {
updateTokenCount(list, tokens, request);
}
}
}

Expand Down Expand Up @@ -271,7 +276,7 @@ private static void handleEventStream(
byte[] bytes = bos.toByteArray();
InputStream bis = new ByteArrayInputStream(bytes);
if ("text/event-stream".equalsIgnoreCase(realContentType)) {
handleServerSentEvent(bis, requestTime, begin, names, tokens, request);
handleServerSentEvent(bis, requestTime, begin, names, tokens, request, ps);
return;
}
List<StringBuilder> list = new ArrayList<>();
Expand Down

0 comments on commit 9719228

Please sign in to comment.