Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import std.stdio, std.socket, core.thread;
- class HttpClientThread : Thread {
- Socket client;
- this(Socket clientSocket) {
- super(&Func);
- client = clientSocket;
- }
- string receiveString() {
- string str = "";
- while(str.length < 5 || str[$ - 4 .. $] != "\r\n\r\n") {
- char[1] c = void;
- client.receive(c);
- str ~= c;
- }
- return str;
- }
- void Func() {
- writeln("Connection accepted from ", client.remoteAddress);
- string request = receiveString();
- writeln("Request: ", request);
- client.send(`HTTP/1.1 300 OK
- Content-Type: text/html; charset=UTF-8
- <b>OMG!</b> this shit works O_o
- `);
- client.close();
- }
- }
- class HttpServer {
- TcpSocket tcpSocket;
- ~this() {
- if(tcpSocket.isAlive)
- tcpSocket.close();
- }
- void Start(string ip, ushort port) {
- tcpSocket = new TcpSocket();
- tcpSocket.bind(parseAddress(ip, port));
- tcpSocket.listen(0);
- writeln("Listening on ", ip, ":", port);
- while(true) {
- new HttpClientThread(tcpSocket.accept()).start();
- }
- }
- }
- void main()
- {
- auto http = new HttpServer();
- http.Start("192.168.1.101", 8888);
- }
Advertisement
Add Comment
Please, Sign In to add comment