Skip to content

Commit

Permalink
Cross-proxy reduce payload of http error responses.
Browse files Browse the repository at this point in the history
See core-wg/corrclar#25

Signed-off-by: Achim Kraus <achim.kraus@cloudcoap.net>
  • Loading branch information
boaks committed Sep 8, 2022
1 parent 44c03a1 commit 6d6e240
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
import org.eclipse.californium.core.coap.OptionNumberRegistry;
import org.eclipse.californium.core.coap.OptionNumberRegistry.OptionFormat;
import org.eclipse.californium.core.coap.OptionSet;
import org.eclipse.californium.core.coap.Response;
import org.eclipse.californium.elements.util.Bytes;
import org.eclipse.californium.elements.util.StringUtil;
import org.eclipse.californium.proxy2.InvalidMethodException;
Expand Down Expand Up @@ -407,7 +408,8 @@ public void setCoapPayload(ContentTypedEntity httpBody, Message coapMessage) thr
byte[] payload = httpBody.getContent();
if (payload != null) {
ContentType contentType = httpBody.getContentType();
int coapContentType = getCoapMediaType(contentType.getMimeType());
String mimeType = contentType.getMimeType();
int coapContentType = getCoapMediaType(mimeType);
coapMessage.getOptions().setContentFormat(coapContentType);
if (MediaTypeRegistry.isCharsetConvertible(coapContentType)) {
try {
Expand All @@ -426,6 +428,26 @@ public void setCoapPayload(ContentTypedEntity httpBody, Message coapMessage) thr
throw new TranslationException("Cannot get the content of the http entity", e);
}
}
if (payload.length > 256) {
if (coapMessage instanceof Response) {
if (!((Response) coapMessage).isSuccess()) {
if (ContentType.TEXT_HTML.getMimeType().equals(mimeType)) {
// blockwise is not supported for error responses
// https://github.com/core-wg/corrclar/issues/25
// reduce payload size
String page = new String(payload, UTF_8);
int start = page.indexOf("<body>");
if (start >= 0) {
int end = page.indexOf("</body>", start);
if (end >= 0) {
page = page.substring(start + 6, end);
payload = page.getBytes(UTF_8);
}
}
}
}
}
}
coapMessage.setPayload(payload);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,13 +156,7 @@ public void completed(Message<HttpResponse, ContentTypedEntity> result) {
try {
long timestamp = ClockUtil.nanoRealtime();
LOGGER.debug("Incoming http response: {}", status);
// the entity of the response, if non repeatable,
// could be
// consumed only one time, so do not debug it!
// System.out.println(EntityUtils.toString(httpResponse.getEntity()));

// translate the received http response in a coap
// response
// translate the received http response in a coap response
Response coapResponse = translator.getCoapResponse(result, incomingCoapRequest);
coapResponse.setNanoTimestamp(timestamp);
if (cache != null) {
Expand Down

0 comments on commit 6d6e240

Please sign in to comment.