をやったあとに、ホスト名とコンテントタイプが指定したものだったときだけ
コンテンツを表示するようにしようとしたらどこを拡張していいかわからなかった。
あきらめてLittleProxyでやってみたら簡単にできた。
(defproject littleproxy-filter-example "0.1.0-SNAPSHOT" :description "FIXME: write description" :url "http://example.com/FIXME" :license {:name "Eclipse Public License" :url "http://www.eclipse.org/legal/epl-v10.html"} :dependencies [[org.clojure/clojure "1.5.1"] [org.littleshoot/littleproxy "0.5.1"]] )
(ns littleproxy-filter-example.core) (import '(org.littleshoot.proxy DefaultHttpProxyServer HttpResponseFilters HttpRequestFilter HttpFilter)) (use 'clojure.java.io) (def ^:dynamic *log-file* "/tmp/proxy-log.txt") (defn write-log [request response] (let [host (.getHeader request "Host") uri (.getUri request) content-type (.getHeader response "Content-Type")] (if (.startsWith (str content-type) "text/html") (try (spit *log-file* (str "new response -----------------------------------------------------------\n" uri "\n" content-type "\n" (.toString (.getContent response) "UTF-8") "\n" ) :append true) (catch Exception e (spit *log-file* (str e) :append true)) )))) (defn get-filter [host-and-port] (proxy [HttpFilter] [] (filterResponses [request] true ;(.endsWith (.getHeader request "Host") "clojure.org") ) (filterResponse [request response] (write-log request response) response) (getMaxResponseSize [] Integer/MAX_VALUE))) (defn create-response-filters [] (proxy [HttpResponseFilters] [] (getFilter [host-and-port] (get-filter host-and-port)))) (defn create-request-filter [] (proxy [HttpRequestFilter] [] (filter [http-request] ;; some ops ))) (def ^:dynamic *server* (DefaultHttpProxyServer. 8888 (create-request-filter) (create-response-filters))) (.start *server*) ;; (.stop *server*)
0 件のコメント:
コメントを投稿