Created
January 4, 2014 18:13
-
-
Save Overv/8258424 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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