Skip to content

Instantly share code, notes, and snippets.

View cx0der's full-sized avatar

Shivakumar Thanikachalam cx0der

View GitHub Profile
@cx0der
cx0der / build.gradle
Created May 18, 2019 09:37
build.gradle
distributions {
main {
contents {
from 'server', {
into 'bin/server'
}
}
}
}
public void run() {
OutputStream dataOut = null;
try (BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
PrintWriter out = new PrintWriter(client.getOutputStream())) {
Map<String, String> requestHeaders = parseRequestHeaders(in);
String method = requestHeaders.get(METHOD);
@cx0der
cx0der / Helper.java
Created May 18, 2019 09:17
Helper methods
private Map<String, String> parseRequestHeaders(BufferedReader in) throws IOException {
Map<String, String> headers = new HashMap<>();
// parse the first line.
String header = in.readLine();
StringTokenizer tokenizer = new StringTokenizer(header);
headers.put(METHOD, tokenizer.nextToken().toUpperCase());
headers.put(RESOURCE, tokenizer.nextToken().toLowerCase());
headers.put(PROTOCOL, tokenizer.nextToken());
// Rest of the headers
String line;
@cx0der
cx0der / ResolveResource.java
Created May 18, 2019 09:07
Resolving resource
private String resolveResource(String requestedPath) {
Path resolvedPath = FileSystems.getDefault().getPath("");
Path other = FileSystems.getDefault().getPath(requestedPath);
for (Path path : other) {
if (!path.startsWith(".") && !path.startsWith("..")) {
resolvedPath = resolvedPath.resolve(path);
}
}
if (resolvedPath.startsWith("")) {
resolvedPath = resolvedPath.resolve(INDEX_HTML);
@cx0der
cx0der / HttpRequestHandler.java
Created March 17, 2019 06:54
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;
@cx0der
cx0der / HttpServer.java
Created February 9, 2019 14:25
Simple Http server in Java
package blog.devrandom.http;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
@cx0der
cx0der / 501.html
Created February 9, 2019 13:30
Sample 501.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Not Implemented</title>
</head>
<body>
<h1>Not Implemented</h1>
</body>
</html>
@cx0der
cx0der / 404.html
Created February 9, 2019 13:29
Simple 404
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Not Found</title>
</head>
<body>
<h1>Resource not found on this server.</h1>
</body>
</html>
@cx0der
cx0der / index.html
Created February 9, 2019 13:20
Simple index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Home page</title>
</head>
<body>
<h1>Simple Java HTTP server</h1>
<p>This is the home page.</p>
</body>
@cx0der
cx0der / vueselect.html
Created November 1, 2018 06:20
vueselect with event listeners and aria attributes
<div
:aria-expanded="[isOpen ? 'true' : 'false']"
:aria-owns="'lbox_' + _uid"
:class="['select__dropdown', isOpen ? 'select__dropdown--open' : 'select__dropdown--close']"
@click="toggle"
@keyup.space="toggle"
@keyup.up="moveUp"
@keyup.down="moveDown"
@keyup.enter="selectFromKeyboard"
aria-autocomplete="none"