Class: Knj::Http
Instance Attribute Summary collapse
-
#cookies ⇒ Object
readonly
Returns the value of attribute cookies.
Class Method Summary collapse
-
.get(url) ⇒ Object
Shortcut to spawn a new Http-object and running get on the path.
- .isgdlink(url) ⇒ Object
Instance Method Summary collapse
- #check_connected ⇒ Object
- #connect ⇒ Object
- #cookie_add(cgi_cookie) ⇒ Object
- #cookiestr ⇒ Object
-
#destroy ⇒ Object
Finished the HTTP-object and unsets all variables to free memory.
- #get(addr) ⇒ Object
- #head(addr) ⇒ Object
- #headers ⇒ Object
-
#initialize(opts = {}) ⇒ Http
constructor
A new instance of Http.
- #opts ⇒ Object
- #post(addr, posthash, files = []) ⇒ Object
- #post_file(addr, files) ⇒ Object
- #setcookie(set_data) ⇒ Object
Constructor Details
#initialize(opts = {}) ⇒ Http
Returns a new instance of Http.
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/knj/http.rb', line 12 def initialize(opts = {}) require "net/http" @opts = opts @cookies = {} @mutex = Mutex.new if opts["useragent"] @useragent = opts["useragent"] else @useragent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.1; knj:true) Gecko/20060111 Firefox/3.6.0.1" end if block_given? begin self.check_connected yield(self) ensure self.destroy end end end |
Instance Attribute Details
#cookies ⇒ Object (readonly)
Returns the value of attribute cookies.
2 3 4 |
# File 'lib/knj/http.rb', line 2 def @cookies end |
Class Method Details
.get(url) ⇒ Object
Shortcut to spawn a new Http-object and running get on the path.
121 122 123 124 125 126 127 128 129 130 |
# File 'lib/knj/http.rb', line 121 def self.get(url) data = URI.parse(url) args = {"host" => data.host} args["ssl"] = true if data.scheme == "https" args["port"] = data.port if data.port http = Knj::Http.new(args) return http.get(data.path) end |
Instance Method Details
#check_connected ⇒ Object
79 80 81 |
# File 'lib/knj/http.rb', line 79 def check_connected self.connect if !@http end |
#connect ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/knj/http.rb', line 55 def connect require "net/https" if @opts["ssl"] if @opts["port"] port = @opts["port"] elsif @opts["ssl"] port = 443 else port = 80 end raise "Invalid host: " + @opts["host"].to_s if !@opts["host"] @http = Net::HTTP.new(@opts["host"], port) @http.set_debug_output($stderr) if @opts["debug"] @http.use_ssl = true if @opts["ssl"] if @opts.key?("validate") and !@opts["validate"] @http.verify_mode = OpenSSL::SSL::VERIFY_NONE end return self end |
#cookie_add(cgi_cookie) ⇒ Object
93 94 95 96 97 98 99 100 101 |
# File 'lib/knj/http.rb', line 93 def () if .class.name == "CGI::Cookie" @cookies[.name] = .value elsif .is_a?(Hash) @cookies[["name"]] = ["value"] else raise "Unknown object: '#{.class.name}'." end end |
#cookiestr ⇒ Object
83 84 85 86 87 88 89 90 91 |
# File 'lib/knj/http.rb', line 83 def = "" @cookies.each do |key, value| << "; " if != "" << "#{Knj::Php.urlencode(key)}=#{Knj::Php.urlencode(value.to_s)}" end return end |
#destroy ⇒ Object
Finished the HTTP-object and unsets all variables to free memory.
36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/knj/http.rb', line 36 def destroy begin @http.finish if @http rescue IOError #ignore - happens when the connection is not started yet. end @http = nil @opts = nil @cookies = nil @mutex = nil @useragent = nil @lasturl = nil end |
#get(addr) ⇒ Object
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 |
# File 'lib/knj/http.rb', line 132 def get(addr) self.check_connected @mutex.synchronize do resp, data = @http.get(addr, self.headers) self.(resp.response.to_hash["set-cookie"]) raise Knj::Errors::NotFound, "Could not find that page: '#{addr}'." if resp.is_a?(Net::HTTPNotFound) #in some cases (like in IronRuby) the data is set like this. data = resp.body if !data return Knj::Http.get(resp["location"]) if resp["location"] return { "response" => resp, "data" => data } end end |
#head(addr) ⇒ Object
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 |
# File 'lib/knj/http.rb', line 152 def head(addr) self.check_connected @mutex.synchronize do resp, data = @http.head(addr, self.headers) self.(resp.response.to_hash["set-cookie"]) raise "Could not find that page: '#{addr}'." if resp.is_a?(Net::HTTPNotFound) #in some cases (like in IronRuby) the data is set like this. data = resp.body if !data return { "response" => resp, "data" => data } end end |
#headers ⇒ Object
103 104 105 106 107 108 |
# File 'lib/knj/http.rb', line 103 def headers tha_headers = {"User-Agent" => @useragent} tha_headers["Referer"] = @lasturl if @lasturl tha_headers["Cookie"] = if != "" return tha_headers end |
#opts ⇒ Object
51 52 53 |
# File 'lib/knj/http.rb', line 51 def opts return @opts end |
#post(addr, posthash, files = []) ⇒ Object
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 |
# File 'lib/knj/http.rb', line 171 def post(addr, posthash, files = []) check_connected postdata = "" posthash.each do |key, value| postdata << "&" if postdata != "" postdata << "#{Knj::Web.urlenc(key)}=#{Knj::Web.urlenc(value)}" end @mutex.synchronize do resp, data = @http.post2(addr, postdata, self.headers) self.(resp.response.to_hash["set-cookie"]) return { "response" => resp, "data" => data } end end |
#post_file(addr, files) ⇒ Object
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 |
# File 'lib/knj/http.rb', line 191 def post_file(addr, files) check_connected boundary = "HJyakstdASDTuyatdtasdtASDTASDasduyAS" postdata = "" files.each do |file| if file.is_a?(String) file = { "pname" => "fileupload", "fname" => File.basename(file), "path" => file } end postdata << "--#{boundary}\r\n" postdata << "Content-Disposition: form-data; name=\"#{file["pname"]}\"; filename=\"#{file["fname"]}\"\r\n" postdata << "Content-Type: text/plain\r\n" postdata << "\r\n" postdata << File.read(file["path"]) postdata << "\r\n--#{boundary}--\r\n" end @mutex.synchronize do request = Net::HTTP::Post.new(addr) request.body = postdata request["Content-Type"] = "multipart/form-data, boundary=#{boundary}" resp, data = @http.request(request) self.(resp.response.to_hash["set-cookie"]) return { "response" => resp, "data" => data } end end |