Bug 1522705 - Add /data/<count> path to HttpBinHandler r=geckoview-reviewers,esawin

This simply lets you request 'count' random bytes. A SHA-256 digest is
included for verifying the integrity of the response.

Differential Revision: https://phabricator.services.mozilla.com/D19503

--HG--
extra : moz-landing-system : lando
This commit is contained in:
James Willcox 2019-02-22 21:45:57 +00:00
parent 709be5f5e2
commit 58a8fb5571

View File

@ -26,7 +26,11 @@ import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Enumeration;
import java.util.Random;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
@ -162,6 +166,27 @@ class HttpBinHandler extends AbstractHandler {
response.put("data", data.toString("UTF-8"));
respondJSON(servletResponse, os, response);
baseRequest.setHandled(true);
} else if (uri.startsWith("/bytes")) {
pipe(is);
final int count = Integer.parseInt(uri.substring("/bytes/".length()));
final Random random = new Random(System.currentTimeMillis());
final byte[] payload = new byte[count];
random.nextBytes(payload);
servletResponse.setStatus(HttpServletResponse.SC_OK);
servletResponse.setContentLength(count);
servletResponse.setContentType("application/octet-stream");
final byte[] digest = MessageDigest.getInstance("SHA-256").digest(payload);
servletResponse.addHeader("X-SHA-256",
String.format("%064x", new BigInteger(1, digest)));
os.write(payload);
os.flush();
baseRequest.setHandled(true);
}
@ -173,6 +198,10 @@ class HttpBinHandler extends AbstractHandler {
Log.e(LOGTAG, "JSON error while handling response", e);
servletResponse.setStatus(500);
baseRequest.setHandled(true);
} catch (NoSuchAlgorithmException e) {
Log.e(LOGTAG, "Failed to generate digest", e);
servletResponse.setStatus(500);
baseRequest.setHandled(true);
}
}
}