From 2c8f58f10388f796fe5d2f0c8fd87534fb7df9de Mon Sep 17 00:00:00 2001 From: Igor Khasilev Date: Fri, 29 Apr 2016 17:48:26 +0300 Subject: [PATCH] added proxy; fix for parsing url --- debian/changelog | 6 ++++++ source/requests/http.d | 20 +++++++++++++++++--- source/requests/uri.d | 4 +++- 3 files changed, 26 insertions(+), 4 deletions(-) diff --git a/debian/changelog b/debian/changelog index 0cbd384..b4924a4 100644 --- a/debian/changelog +++ b/debian/changelog @@ -1,3 +1,9 @@ +dlang-requests (0.0.7-0) unstable; urgency=low + + * added proxy; fix for parsing url + + -- igor Tue, 29 Apr 2016 12:11:22 -0400 + dlang-requests (0.0.6-0) unstable; urgency=low * Initial release (Closes: #nnnn) diff --git a/source/requests/http.d b/source/requests/http.d index 3664754..e33aabf 100644 --- a/source/requests/http.d +++ b/source/requests/http.d @@ -366,6 +366,7 @@ public struct Request { uint __verbosity = 0; // 0 - no output, 1 - headers, 2 - headers+body info DataPipe!ubyte __bodyDecoder; DecodeChunked __unChunker; + string __proxy; } mixin(getter("keepAlive")); @@ -385,6 +386,7 @@ public struct Request { mixin(setter("bufferSize")); mixin(getter("verbosity")); mixin(setter("verbosity")); + mixin(setter("proxy")); this(string uri) { __uri = URI(uri); @@ -425,6 +427,9 @@ public struct Request { } @property string requestString(string[string] params = null) { + if ( __proxy ) { + return "%s %s HTTP/1.1\r\n".format(__method, __uri.uri); + } if ( __method != "GET" ) { // encode params into url only for GET return "%s %s HTTP/1.1\r\n".format(__method, __uri.path); @@ -584,12 +589,20 @@ public struct Request { void setupConnection() { if ( !__stream || !__stream.isConnected ) { tracef("Set up new connection"); - final switch (__uri.scheme) { + URI uri; + if ( __proxy ) { + // use proxy uri to connect + uri.uri_parse(__proxy); + } else { + // use original uri + uri = __uri; + } + final switch (uri.scheme) { case "http": - __stream = new TCPSocketStream().connect(__uri.host, __uri.port, __timeout); + __stream = new TCPSocketStream().connect(uri.host, uri.port, __timeout); break; case "https": - __stream = new SSLSocketStream().connect(__uri.host, __uri.port, __timeout); + __stream = new SSLSocketStream().connect(uri.host, uri.port, __timeout); break; } } else { @@ -1178,6 +1191,7 @@ unittest { assert(rs.code==200); // rq = Request(); rq.keepAlive = 5; +// rq.proxy = "http://localhost:8888/"; rs = rq.get("http://httpbin.org/absolute-redirect/2"); assert(rs.history.length == 2); assert(rs.code==200); diff --git a/source/requests/uri.d b/source/requests/uri.d index d5a0c8d..e44a130 100644 --- a/source/requests/uri.d +++ b/source/requests/uri.d @@ -76,7 +76,9 @@ struct URI { if ( path_and_query.length ) { i = path_and_query.findSplit("?"); __path = "/" ~ i[0]; - __query = "?" ~ i[2]; + if ( i[2].length) { + __query = "?" ~ i[2]; + } } // return true;