Created
October 17, 2011 13:03
-
-
Save azproduction/1292557 to your computer and use it in GitHub Desktop.
Simple Google Dart HTTP Server
This file contains hidden or 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
/** | |
* Simple HTTP server on Dart | |
* @see http://code.google.com/p/dart/source/browse/branches/bleeding_edge/dart/runtime/bin/socket_impl.dart | |
* http://code.google.com/p/dart/source/browse/branches/bleeding_edge/dart/runtime/bin/socket_stream.dart | |
* | |
* Based on http://code.google.com/p/dart/source/browse/branches/bleeding_edge/dart/samples/socket/SocketExample.dart | |
* @author azproduction | |
*/ | |
class HttpExample { | |
final String host = "127.0.0.1"; | |
final int port = 50500; | |
ServerSocket serverSocket; | |
List receiveBuffer; | |
List<int> output; | |
HttpExample() { | |
output = "HTTP/1.1 200 OK\nServer:Dart\n\n<h1>Works!</h1>".charCodes(); | |
} | |
void go() { | |
// initialize the server | |
serverSocket = new ServerSocket(host, port, 5); | |
if (serverSocket == null) { | |
throw "can't get server socket"; | |
} | |
serverSocket.setConnectionHandler(onConnect); | |
print("accepting connections on ${host}:${port}"); | |
} | |
void onSend() { | |
} | |
void onConnect() { | |
List<int> receiveBuffer = new List(); | |
Socket receiveSocket = serverSocket.accept(); | |
InputStream inputStream = receiveSocket.inputStream; | |
OutputStream outputStream = receiveSocket.outputStream; | |
inputStream.readUntil(receiveBuffer, bufferReady); | |
outputStream.write(output, 0, output.length, onSend); | |
receiveSocket.close(); | |
} | |
// receive buffer has been filled | |
void bufferReady() { | |
} | |
} | |
main() { | |
new HttpExample().go(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
ab test
There is a huge chance to get an error "apr_socket_recv: Connection reset by peer (54)" I wonder, why?