Skip to content

Instantly share code, notes, and snippets.

Avatar

Shivakumar Thanikachalam cx0der

  • Bangalore
View GitHub Profile
View build.gradle
distributions {
main {
contents {
from 'server', {
into 'bin/server'
}
}
}
}
View run.java
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
Helper methods
View Helper.java
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;
View ResolveResource.java
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);
View HttpRequestHandler.java
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 Feb 9, 2019
Simple Http server in Java
View HttpServer.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 Feb 9, 2019
Sample 501.html
View 501.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Not Implemented</title>
</head>
<body>
<h1>Not Implemented</h1>
</body>
</html>
View 404.html
<!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 Feb 9, 2019
Simple index.html
View 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 Nov 1, 2018
vueselect with event listeners and aria attributes
View vueselect.html
<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"