Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Helper methods
private Map<String, String> parseRequestHeaders(BufferedReader in) throws IOException {
Map<String, String> headers = new HashMap<>();
String line;
while ((line = in.readLine()) != null && !line.isEmpty()) {
int idx = line.indexOf(':');
if (idx > 0) {
headers.put(line.substring(0, idx).toLowerCase(), line.substring(idx + 1).trim());
}
}
return headers;
}
private byte[] readFile(File file) throws IOException {
byte[] res;
try (FileInputStream fis = new FileInputStream(file)) {
int length = (int) file.length();
res = new byte[length];
//noinspection ResultOfMethodCallIgnored
fis.read(res, 0, length);
}
return res;
}
private void writeResponseHeaders(PrintWriter out, String protocol, String status, String date, int length) {
out.println(protocol + status);
out.println(SERVER + config.getProperty(SERVER_VERSION_PARAM));
out.println(DATE + date);
out.println(CONTENT_TYPE + "text/html; charset=utf-8");
out.println(CONTENT_LENGTH + length);
out.println(CONNECTION + "close");
out.println();
out.flush();
}
private void log(InetAddress remoteAddress, String date, String request, String status, String ua) {
logger.printf("%s [%s] \"%s\"%s %s\n", remoteAddress.getHostAddress(), date, request, status, ua);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment