 
 (def ^{:dynamic true} *cookie-store* nil)
 
-(defn- default-proxy-host-for
-  [scheme]
-  (System/getProperty (str scheme ".proxyHost")))
-
-(defn- default-proxy-port-for
-  [scheme]
-  (Integer/parseInt (System/getProperty (str scheme ".proxyPort"))))
+(defn- with-routing
+  "Use ProxySelectorRoutePlanner to choose proxy sensible based on http.nonProxyHosts"
+  [client]
+  (.setRoutePlanner client
+                    (ProxySelectorRoutePlanner.
+                     (.. client getConnectionManager getSchemeRegistry) nil))
+  client)
+
+(defn maybe-force-proxy [client request proxy-host proxy-port]
+  (let [uri (.getURI request)]
+    (when (and (nil? (#{"localhost" "127.0.0.1"} (.getHost uri))) proxy-host)
+      (let [target (HttpHost. (.getHost uri) (.getPort uri) (.getScheme uri))]
+        (set-client-param client ConnRoutePNames/FORCED_ROUTE
+                          (HttpRoute. target nil (HttpHost. proxy-host proxy-port)
+                                      (.. client getConnectionManager getSchemeRegistry
+                                          (getScheme target) isLayered)))))
+    request))
 
 (defn add-client-params!
   "Add various client params to the http-client object, if needed."
-  [http-client scheme socket-timeout conn-timeout server-name
-   proxy-host proxy-port]
+  [http-client scheme socket-timeout conn-timeout server-name]
   (doto http-client
     (set-client-param ClientPNames/COOKIE_POLICY
                       CookiePolicy/BROWSER_COMPATIBILITY)

    (set-client-param "http.socket.timeout"
                      (and socket-timeout (Integer. ^Long socket-timeout)))
    (set-client-param "http.connection.timeout"
                      (and conn-timeout (Integer. ^Long conn-timeout))))
  (when (nil? (#{"localhost" "127.0.0.1"} server-name))
    (when-let [effective-proxy-host (or proxy-host
                                        (default-proxy-host-for scheme))]
      (let [effective-proxy-port (or proxy-port
                                     (default-proxy-port-for scheme))]
        (set-client-param http-client ConnRoutePNames/DEFAULT_PROXY
                          (HttpHost. effective-proxy-host
                                     effective-proxy-port))))))

(defn- coerce-body-entity
  "Coerce the http-entity from an HttpResponse to either a byte-array, or a

                                           HttpEntityEnclosingRequestBase)
           (org.apache.http.client.params CookiePolicy ClientPNames)
           (org.apache.http.conn ClientConnectionManager)
           (org.apache.http.conn.routing HttpRoute)
           (org.apache.http.conn.params ConnRoutePNames)
           (org.apache.http.impl.client DefaultHttpClient)
           (org.apache.http.impl.conn SingleClientConnManager ProxySelectorRoutePlanner)

           (org.apache.http.util EntityUtils)))

(defn parse-headers
