A server that delivers content, such as web pages, using the HTTP protocol.
The HttpServer is a Stream that provides HttpRequest objects. Each HttpRequest has an associated HttpResponse object. The server responds to a request by writing to that HttpResponse object. The following example shows how to bind an HttpServer to an IPv6 InternetAddress on port 80 (the standard port for HTTP servers) and how to listen for requests. Port 80 is the default HTTP port. However, on most systems accessing this requires super-user privileges. For local testing consider using a non-reserved port (1024 and above).
import 'dart:io';
main() {
HttpServer
.bind(InternetAddress.anyIPv6, 80)
.then((server) {
server.listen((HttpRequest request) {
request.response.write('Hello, world!');
request.response.close();
});
});
}
Incomplete requests, in which all or part of the header is missing, are ignored, and no exceptions or HttpRequest objects are generated for them. Likewise, when writing to an HttpResponse, any Socket exceptions are ignored and any future writes are ignored.
The HttpRequest exposes the request headers and provides the request body, if it exists, as a Stream of data. If the body is unread, it is drained when the server writes to the HttpResponse or closes it.
Use bindSecure to create an HTTPS server.
The server presents a certificate to the client. The certificate chain and the private key are set in the SecurityContext object that is passed to bindSecure.
import 'dart:io';
import "dart:isolate";
main() {
SecurityContext context = new SecurityContext();
var chain =
Platform.script.resolve('certificates/server_chain.pem')
.toFilePath();
var key =
Platform.script.resolve('certificates/server_key.pem')
.toFilePath();
context.useCertificateChain(chain);
context.usePrivateKey(key, password: 'dartdart');
HttpServer
.bindSecure(InternetAddress.anyIPv6,
443,
context)
.then((server) {
server.listen((HttpRequest request) {
request.response.write('Hello, world!');
request.response.close();
});
});
}
The certificates and keys are PEM files, which can be created and managed with the tools in OpenSSL.
You can use the listenOn constructor to attach an HTTP server to a ServerSocket.
import 'dart:io';
main() {
ServerSocket.bind(InternetAddress.anyIPv6, 80)
.then((serverSocket) {
HttpServer httpserver = new HttpServer.listenOn(serverSocket);
serverSocket.listen((Socket socket) {
socket.write('Hello, client.');
});
});
}
HttpServer is a Stream. Refer to the Stream class for information about the streaming qualities of an HttpServer. Pausing the subscription of the stream, pauses at the OS level.
The shelf package on pub.dartlang.org contains a set of high-level classes that, together with this class, makes it easy to provide content through HTTP servers.
serverSocket
.
port
is
specified in the bind or bindSecure call.
Server
header for all responses
generated by this HttpServer. [...]
test
accepts any element provided by this stream. [...]
Stream<R>
. [...]
needle
occurs in the elements provided by this stream. [...]
index
th data event of this stream. [...]
test
accepts all elements provided by this stream. [...]
test
. [...]
combine
. [...]
action
on each element of this stream. [...]
test
. [...]
streamConsumer
. [...]
combine
. [...]
test
. [...]
count
data events from this stream. [...]
test
. [...]
count
data events of this stream. [...]
test
is successful. [...]
streamTransformer
to this stream. [...]
address
and
port
. [...]
address
can either be a String or an
InternetAddress. If address
is a String, bind will
perform a InternetAddress.lookup and use the first value in the
list. To listen on the loopback adapter, which will allow only
incoming connections from the local host, use the value
InternetAddress.loopbackIPv4 or
InternetAddress.loopbackIPv6. To allow for incoming
connection from the network use either one of the values
InternetAddress.anyIPv4 or InternetAddress.anyIPv6 to
bind to all interfaces or the IP address of a specific interface. [...]