vibe.d beta banner
get vibe.d
0.9.0

Asynchronous I/O that doesn’t get in your way, written in D

Class HTTPClientSettings

Defines an HTTP/HTTPS proxy request or a connection timeout for an HTTPClient.

class HTTPClientSettings ;

Fields

NameTypeDescription
connectTimeout core.time.DurationTimeout for establishing a connection to the server
defaultKeepAliveTimeout core.time.Duration
dnsAddressFamily std.socket.AddressFamilyCan be used to force looking up IPv4/IPv6 addresses for host names.
networkInterface NetworkAddressForces a specific network interface to use for outgoing connections.
proxyURL URL
readTimeout core.time.DurationTimeout during read operations on the underyling transport
tlsContextSetup nothrow @safe void delegate(TLSContext)Allows to customize the TLS context before connecting to a server.

Properties

NameTypeDescription
dup[get] HTTPClientSettings

Example

void test() {

	HTTPClientSettings settings = new HTTPClientSettings;
	settings.proxyURL = URL.parse("http://proxyuser:proxypass@192.168.2.50:3128");
	settings.defaultKeepAliveTimeout = 0.seconds; // closes connection immediately after receiving the data.
	requestHTTP("http://www.example.org",
				(scope req){
		req.method = HTTPMethod.GET;
	},
	(scope res){
		logInfo("Headers:");
		foreach(key, ref value; res.headers) {
			logInfo("%s: %s", key, value);
		}
		logInfo("Response: %s", res.bodyReader.readAllUTF8());
	}, settings);

}

Example

 // test connect timeout
import std.conv : to;
import vibe.core.stream : pipe, nullSink;

HTTPClientSettings settings = new HTTPClientSettings;
settings.connectTimeout = 50.msecs;

// Use an IP address that is guaranteed to be unassigned globally to force
// a timeout (see RFC 3330)
auto cli = connectHTTP("192.0.2.0", 80, false, settings);
auto timer = setTimer(500.msecs, { assert(false, "Connect timeout occurred too late"); });
scope (exit) timer.stop();

try {
	cli.request(
		(scope req) { assert(false, "Expected no connection"); },
		(scope res) { assert(false, "Expected no response"); }
	);
	assert(false, "Response read expected to fail due to timeout");
} catch(Exception e) {}

Example

 // test connect timeout
import std.conv : to;
import vibe.core.stream : pipe, nullSink;

HTTPClientSettings settings = new HTTPClientSettings;
settings.connectTimeout = 50.msecs;

// Use an IP address that is guaranteed to be unassigned globally to force
// a timeout (see RFC 3330)
auto cli = connectHTTP("192.0.2.0", 80, false, settings);
auto timer = setTimer(500.msecs, { assert(false, "Connect timeout occurred too late"); });
scope (exit) timer.stop();

try {
	cli.request(
		(scope req) { assert(false, "Expected no connection"); },
		(scope res) { assert(false, "Expected no response"); }
	);
	assert(false, "Response read expected to fail due to timeout");
} catch(Exception e) {}

Example

 // test read timeout
import std.conv : to;
import vibe.core.stream : pipe, nullSink;

version (VibeLibasyncDriver) {
	logInfo("Skipping HTTP client read timeout test due to buggy libasync driver.");
} else {
	HTTPClientSettings settings = new HTTPClientSettings;
	settings.readTimeout = 50.msecs;

	auto l = listenTCP(0, (conn) {
		try conn.pipe(nullSink);
		catch (Exception e) assert(false, e.msg);
		conn.close();
	}, "127.0.0.1");

	auto cli = connectHTTP("127.0.0.1", l.bindAddress.port, false, settings);
	auto timer = setTimer(500.msecs, { assert(false, "Read timeout occurred too late"); });
	scope (exit) {
		timer.stop();
		l.stopListening();
		cli.disconnect();
		sleep(10.msecs); // allow the read connection end to fully close
	}

	try {
		cli.request(
			(scope req) { req.method = HTTPMethod.GET; },
			(scope res) { assert(false, "Expected no response"); }
		);
		assert(false, "Response read expected to fail due to timeout");
	} catch(Exception e) {}
}
Authors

Sönke Ludwig, Jan Krüger

Copyright

© 2012-2014 Sönke Ludwig

License

Subject to the terms of the MIT license, as written in the included LICENSE.txt file.