knjrbfw 0.0.59 → 0.0.62

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/lib/knj/http.rb DELETED
@@ -1,228 +0,0 @@
1
- class Knj::Http
2
- attr_reader :cookies
3
-
4
- def self.isgdlink(url)
5
- http = Knj::Http.new("host" => "is.gd")
6
- http.connect
7
- resp = http.get("/api.php?longurl=" + url)
8
-
9
- return resp["data"]
10
- end
11
-
12
- def initialize(opts = {})
13
- require "net/http"
14
-
15
- @opts = opts
16
- @cookies = {}
17
- @mutex = Mutex.new
18
-
19
- if opts["useragent"]
20
- @useragent = opts["useragent"]
21
- else
22
- @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"
23
- end
24
-
25
- if block_given?
26
- begin
27
- self.check_connected
28
- yield(self)
29
- ensure
30
- self.destroy
31
- end
32
- end
33
- end
34
-
35
- #Finished the HTTP-object and unsets all variables to free memory.
36
- def destroy
37
- begin
38
- @http.finish if @http
39
- rescue IOError
40
- #ignore - happens when the connection is not started yet.
41
- end
42
-
43
- @http = nil
44
- @opts = nil
45
- @cookies = nil
46
- @mutex = nil
47
- @useragent = nil
48
- @lasturl = nil
49
- end
50
-
51
- def opts
52
- return @opts
53
- end
54
-
55
- def connect
56
- require "net/https" if @opts["ssl"]
57
-
58
- if @opts["port"]
59
- port = @opts["port"]
60
- elsif @opts["ssl"]
61
- port = 443
62
- else
63
- port = 80
64
- end
65
-
66
- raise "Invalid host: " + @opts["host"].to_s if !@opts["host"]
67
-
68
- @http = Net::HTTP.new(@opts["host"], port)
69
- @http.set_debug_output($stderr) if @opts["debug"]
70
- @http.use_ssl = true if @opts["ssl"]
71
-
72
- if @opts.key?("validate") and !@opts["validate"]
73
- @http.verify_mode = OpenSSL::SSL::VERIFY_NONE
74
- end
75
-
76
- return self
77
- end
78
-
79
- def check_connected
80
- self.connect if !@http
81
- end
82
-
83
- def cookiestr
84
- cookiestr = ""
85
- @cookies.each do |key, value|
86
- cookiestr << "; " if cookiestr != ""
87
- cookiestr << "#{Knj::Php.urlencode(key)}=#{Knj::Php.urlencode(value.to_s)}"
88
- end
89
-
90
- return cookiestr
91
- end
92
-
93
- def cookie_add(cgi_cookie)
94
- if cgi_cookie.class.name == "CGI::Cookie"
95
- @cookies[cgi_cookie.name] = cgi_cookie.value
96
- elsif cgi_cookie.is_a?(Hash)
97
- @cookies[cgi_cookie["name"]] = cgi_cookie["value"]
98
- else
99
- raise "Unknown object: '#{cgi_cookie.class.name}'."
100
- end
101
- end
102
-
103
- def headers
104
- tha_headers = {"User-Agent" => @useragent}
105
- tha_headers["Referer"] = @lasturl if @lasturl
106
- tha_headers["Cookie"] = cookiestr if cookiestr != ""
107
- return tha_headers
108
- end
109
-
110
- def setcookie(set_data)
111
- return nil if !set_data
112
-
113
- set_data.each do |cookie_str|
114
- Knj::Web.parse_set_cookies(cookie_str.to_s).each do |cookie|
115
- @cookies[cookie["name"]] = cookie["value"]
116
- end
117
- end
118
- end
119
-
120
- #Shortcut to spawn a new Http-object and running get on the path.
121
- def self.get(url)
122
- data = URI.parse(url)
123
-
124
- args = {"host" => data.host}
125
- args["ssl"] = true if data.scheme == "https"
126
- args["port"] = data.port if data.port
127
-
128
- http = Knj::Http.new(args)
129
- return http.get(data.path)
130
- end
131
-
132
- def get(addr)
133
- self.check_connected
134
-
135
- @mutex.synchronize do
136
- resp, data = @http.get(addr, self.headers)
137
- self.setcookie(resp.response.to_hash["set-cookie"])
138
- raise Knj::Errors::NotFound, "Could not find that page: '#{addr}'." if resp.is_a?(Net::HTTPNotFound)
139
-
140
- #in some cases (like in IronRuby) the data is set like this.
141
- data = resp.body if !data
142
-
143
- return Knj::Http.get(resp["location"]) if resp["location"]
144
-
145
- return {
146
- "response" => resp,
147
- "data" => data
148
- }
149
- end
150
- end
151
-
152
- def head(addr)
153
- self.check_connected
154
-
155
- @mutex.synchronize do
156
- resp, data = @http.head(addr, self.headers)
157
- self.setcookie(resp.response.to_hash["set-cookie"])
158
-
159
- raise "Could not find that page: '#{addr}'." if resp.is_a?(Net::HTTPNotFound)
160
-
161
- #in some cases (like in IronRuby) the data is set like this.
162
- data = resp.body if !data
163
-
164
- return {
165
- "response" => resp,
166
- "data" => data
167
- }
168
- end
169
- end
170
-
171
- def post(addr, posthash, files = [])
172
- check_connected
173
-
174
- postdata = ""
175
- posthash.each do |key, value|
176
- postdata << "&" if postdata != ""
177
- postdata << "#{Knj::Web.urlenc(key)}=#{Knj::Web.urlenc(value)}"
178
- end
179
-
180
- @mutex.synchronize do
181
- resp, data = @http.post2(addr, postdata, self.headers)
182
- self.setcookie(resp.response.to_hash["set-cookie"])
183
-
184
- return {
185
- "response" => resp,
186
- "data" => data
187
- }
188
- end
189
- end
190
-
191
- def post_file(addr, files)
192
- check_connected
193
-
194
- boundary = "HJyakstdASDTuyatdtasdtASDTASDasduyAS"
195
- postdata = ""
196
-
197
- files.each do |file|
198
- if file.is_a?(String)
199
- file = {
200
- "pname" => "fileupload",
201
- "fname" => File.basename(file),
202
- "path" => file
203
- }
204
- end
205
-
206
- postdata << "--#{boundary}\r\n"
207
- postdata << "Content-Disposition: form-data; name=\"#{file["pname"]}\"; filename=\"#{file["fname"]}\"\r\n"
208
- postdata << "Content-Type: text/plain\r\n"
209
- postdata << "\r\n"
210
- postdata << File.read(file["path"])
211
- postdata << "\r\n--#{boundary}--\r\n"
212
- end
213
-
214
- @mutex.synchronize do
215
- request = Net::HTTP::Post.new(addr)
216
- request.body = postdata
217
- request["Content-Type"] = "multipart/form-data, boundary=#{boundary}"
218
-
219
- resp, data = @http.request(request)
220
- self.setcookie(resp.response.to_hash["set-cookie"])
221
-
222
- return {
223
- "response" => resp,
224
- "data" => data
225
- }
226
- end
227
- end
228
- end