Skip to content

Instantly share code, notes, and snippets.

@Overv
Created January 4, 2014 18:13
Show Gist options
  • Save Overv/8258424 to your computer and use it in GitHub Desktop.
Save Overv/8258424 to your computer and use it in GitHub Desktop.
import std.socket;
import std.stdio;
import std.conv;
static import file = std.file;
import gzip;
const string content = "{}";
void main() {
TcpSocket server = new TcpSocket();
server.setOption(SocketOptionLevel.SOCKET, SocketOption.REUSEADDR, true);
server.bind(new InternetAddress(8080));
server.listen(1);
writeln("Server running.");
while (true) {
Socket client = server.accept();
char buffer[1024];
client.receive(buffer);
void[] gzContent = gzip.compress(cast(void[]) content);
string headers = "HTTP/1.1 200 OK\n";
headers ~= "Content-Type: application/json\n";
headers ~= "Content-Encoding: gzip\n";
headers ~= "Content-Length: " ~ to!string(gzContent.length) ~ "\n\n";
client.send(headers ~ gzContent);
client.shutdown(SocketShutdown.BOTH);
client.close();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment