Skip to content

Instantly share code, notes, and snippets.

Created March 17, 2019 06:54
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save cx0der/badd91a9cbb2aa151d1c65f073c40e8d to your computer and use it in GitHub Desktop.
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, 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");
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