Permalink
Browse files

Functions to block unwanted requests, e.g. javascript that causes htm…

…lunit to throw exceptions.
  • Loading branch information...
1 parent 113081d commit 3e5a3e75d78d10133b243e440733afae416fe58f @mdiin committed Feb 16, 2012
Showing with 27 additions and 3 deletions.
  1. +1 −1 project.clj
  2. +26 −2 src/clj_htmlunit/core.clj
View
@@ -1,4 +1,4 @@
-(defproject clj-htmlunit "0.1.0"
+(defproject clj-htmlunit "0.1.1"
:description "Simple clojure wrapper for htmlunit"
:dependencies [[org.clojure/clojure "1.3.0"]
[net.sourceforge.htmlunit/htmlunit "2.9"]])
@@ -2,7 +2,9 @@
(:import (com.gargoylesoftware.htmlunit
SgmlPage WebClient BrowserVersion)
(com.gargoylesoftware.htmlunit.html
- HtmlElement)))
+ HtmlElement)
+ (com.gargoylesoftware.htmlunit.util
+ FalsifyingWebConnection)))
(defn make-client
"[]
@@ -74,4 +76,26 @@
length (.getLength attrs)
items (map #(.item attrs %) (range 0 length))
hash (reduce #(merge %1 {(keyword (.getName %2)) (.getValue %2)}) {} items)]
- hash))
+ hash))
+
+;; TODO: Find a way to make these return a fresh client object.
+(defn allow-all
+ "Modifies(!) the given client to process all requests
+ normally. Returns an instance of an anonymous class."
+ [client]
+ (proxy [FalsifyingWebConnection] [client]
+ (getResponse [request]
+ (let [res (proxy-super getResponse request)]
+ res))))
+
+(defn block-match
+ "Modifies(!) the given client to ignore all requests that contain
+ match in their URL. Returns an instance of an anonymous class."
+ [client match]
+ (proxy [FalsifyingWebConnection] [client]
+ (getResponse [request]
+ (let [res (proxy-super getResponse request)
+ req-url (.. res getWebRequest getUrl toString)]
+ (if (.contains req-url match)
+ (proxy-super createWebResponse (.getWebRequest res) "" "application/javascript" 200 "Ok")
+ res)))))

0 comments on commit 3e5a3e7

Please sign in to comment.