Class: Knjappserver::Httpsession::Http_request

Inherits:
Object
  • Object
show all
Defined in:
lib/include/class_httpsession_http_request.rb

Overview

This class parses the various HTTP requests into easy programmable objects. Get, post, cookie, meta and so on…

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ Http_request

Sets the various required data on the object. Knjappserver, crlf and arguments.



13
14
15
16
17
# File 'lib/include/class_httpsession_http_request.rb', line 13

def initialize(args)
	@args = args
	@kas = @args[:kas]
	@crlf = "\r\n"
end

Instance Attribute Details

#clengthObject (readonly)

Returns the value of attribute clength.



10
11
12
# File 'lib/include/class_httpsession_http_request.rb', line 10

def clength
  @clength
end

Returns the value of attribute cookie.



10
11
12
# File 'lib/include/class_httpsession_http_request.rb', line 10

def cookie
  @cookie
end

#getObject (readonly)

Returns the value of attribute get.



10
11
12
# File 'lib/include/class_httpsession_http_request.rb', line 10

def get
  @get
end

#headersObject (readonly)

Returns the value of attribute headers.



10
11
12
# File 'lib/include/class_httpsession_http_request.rb', line 10

def headers
  @headers
end

#http_versionObject (readonly)

Returns the value of attribute http_version.



10
11
12
# File 'lib/include/class_httpsession_http_request.rb', line 10

def http_version
  @http_version
end

#metaObject (readonly)

Returns the value of attribute meta.



10
11
12
# File 'lib/include/class_httpsession_http_request.rb', line 10

def meta
  @meta
end

#page_pathObject (readonly)

Returns the value of attribute page_path.



10
11
12
# File 'lib/include/class_httpsession_http_request.rb', line 10

def page_path
  @page_path
end

#percentObject (readonly)

Returns the value of attribute percent.



10
11
12
# File 'lib/include/class_httpsession_http_request.rb', line 10

def percent
  @percent
end

#postObject (readonly)

Returns the value of attribute post.



10
11
12
# File 'lib/include/class_httpsession_http_request.rb', line 10

def post
  @post
end

#readObject (readonly)

Returns the value of attribute read.



10
11
12
# File 'lib/include/class_httpsession_http_request.rb', line 10

def read
  @read
end

#secs_leftObject (readonly)

Returns the value of attribute secs_left.



10
11
12
# File 'lib/include/class_httpsession_http_request.rb', line 10

def secs_left
  @secs_left
end

#speedObject (readonly)

Returns the value of attribute speed.



10
11
12
# File 'lib/include/class_httpsession_http_request.rb', line 10

def speed
  @speed
end

Instance Method Details

#convert_post(seton, post_val, args = {}) ⇒ Object

Converts post-result to the right type of hash.



214
215
216
217
218
# File 'lib/include/class_httpsession_http_request.rb', line 214

def convert_post(seton, post_val, args = {})
	post_val.each do |varname, value|
		Knj::Web.parse_name(seton, varname, value, args)
	end
end

#modified_sinceObject

Parses the if-modified-since header and returns it as a Time-object. Returns false is no if-modified-since-header is given or raises an RuntimeError if it cant be parsed.



200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/include/class_httpsession_http_request.rb', line 200

def modified_since
   return @modified_since if @modified_since
   return false if !@meta["HTTP_IF_MODIFIED_SINCE"]
   
   mod_match = @meta["HTTP_IF_MODIFIED_SINCE"].match(/^([A-z]+),\s+(\d+)\s+([A-z]+)\s+(\d+)\s+(\d+):(\d+):(\d+)\s+(.+)$/)
   raise "Could not parse 'HTTP_IF_MODIFIED_SINCE'." if !mod_match
   
   month_no = Datet.month_str_to_no(mod_match[3])
   @modified_since = Time.utc(mod_match[4].to_i, month_no, mod_match[2].to_i, mod_match[5].to_i, mod_match[6].to_i, mod_match[7].to_i)
   
   return @modified_since
end

#read_socket(socket, cont) ⇒ Object

Reads content from the socket until the end of headers. Also does various error-checks.



20
21
22
23
24
25
26
27
28
# File 'lib/include/class_httpsession_http_request.rb', line 20

def read_socket(socket, cont)
	loop do
		raise Errno::ECONNRESET, "Socket closed." if socket.closed?
		read = socket.gets
		raise Errno::ECONNRESET, "Socket returned non-string: '#{read.class.name}'." if !read.is_a?(String)
		cont << read
		break if cont[-4..-1] == "\r\n\r\n" or cont[-2..-1] == "\n\n"
	end
end

#resetObject



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/include/class_httpsession_http_request.rb', line 30

def reset
   @modified_since = nil
   @get = nil
   @post = nil
   @cookie = nil
   @meta = nil
   @page_path = nil
   @headers = nil
   @http_version = nil
   @read = nil
   @clength = nil
   @speec = nil
   @percent = nil
   @secs_left = nil
end

#socket_parse(socket) ⇒ Object

Generates data on object from the given socket.



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/include/class_httpsession_http_request.rb', line 47

def socket_parse(socket)
   self.reset
	cont = ""
	self.read_socket(socket, cont)
	
	#Parse URI (page_path and get).
	match = cont.match(/^(GET|POST|HEAD)\s+(.+)\s+HTTP\/1\.(\d+)\s*/)
	raise "Could not parse request: '#{cont.split("\n").first}'." if !match
   
	@http_version = "1.#{match[3]}"
	
	method = match[1]
	cont = cont.gsub(match[0], "")
	
	uri = Knj::Web.parse_uri(match[2])
	
	page_filepath = Knj::Web.urldec(uri[:path])
	if page_filepath.length <= 0 or page_filepath == "/" or File.directory?("#{@kas.config[:doc_root]}/#{page_filepath}")
		page_filepath = "#{page_filepath}/#{@kas.config[:default_page]}"
	end
	
	@page_path = "#{@kas.config[:doc_root]}/#{page_filepath}"
	@get = Knj::Web.parse_urlquery(uri[:query], {:urldecode => true, :force_utf8 => true})
	
   if @get["_kas_httpsession_id"]
     @kas.httpsessions_ids[@get["_kas_httpsession_id"]] = @args[:httpsession]
	end
   
	begin
     #Parse headers, cookies and meta.
     @headers = {}
     @cookie = {}
     @meta = {
       "REQUEST_METHOD" => method,
       "QUERY_STRING" => uri[:query],
       "REQUEST_URI" => match[2],
       "SCRIPT_NAME" => uri[:path]
     }
     
     cont.scan(/^(\S+):\s*(.+)\r\n/) do |header_match|
       key = header_match[0].downcase
       val = header_match[1]
       
       @headers[key] = [] if !@headers.has_key?(key)
       @headers[key] << val
       
       case key
         when "cookie"
           Knj::Web.parse_cookies(val).each do |key, val|
             @cookie[key] = val
           end
         when "content-length"
           @clength = val.to_i
         else
           key = key.upcase.gsub("-", "_")
           @meta["HTTP_#{key}"] = val
       end
     end
     
     
     #Parse post
     @post = {}
     
     if method == "POST"
       post_treated = {}
       
       @speed = nil
       @read = 0
       post_data = ""
       
       Thread.new do
         begin
           time_cur = Time.now
           read_last = 0
           sleep 0.1
           
           while @clength and @read != nil and @read < @clength
             break if !@clength or !@read
             
             time_now = Time.now
             time_betw = time_now.to_f - time_cur.to_f
             read_betw = @read - read_last
             
             time_cur = time_now
             read_last = @read
             
             @percent = @read.to_f / @clength.to_f
             @speed = read_betw.to_f / time_betw.to_f
             
             bytes_left = @clength - read
             
             if @speed > 0 and bytes_left > 0
               @secs_left = bytes_left.to_f / @speed
             else
               @secs_left = false
             end
             
             sleep 2
           end
         rescue => e
           if @kas
             @kas.handle_error(e)
           else
             STDOUT.print Knj::Errors.error_str(e)
           end
         end
       end
       
       while @read < @clength
         read_size = @clength - @read
         read_size = 4096 if read_size > 4096
         
         raise Errno::ECONNRESET, "Socket closed." if socket.closed?
         read = socket.read(read_size)
         raise Errno::ECONNRESET, "Socket returned non-string: '#{read.class.name}'." if !read.is_a?(String)
         post_data << read
         @read += read.length
       end
       
       if @headers["content-type"] and match = @headers["content-type"].first.match(/^multipart\/form-data; boundary=(.+)\Z/)
         post_treated = Knjappserver::Httpsession::Post_multipart.new(
           "io" => StringIO.new("#{post_data}"),
           "boundary" => match[1],
           "crlf" => @crlf
         ).return
         
         self.convert_post(@post, post_treated, {:urldecode => false})
       else
         post_data.split("&").each do |splitted|
           splitted = splitted.split("=")
           key = Knj::Web.urldec(splitted[0]).to_s.encode("utf-8")
           val = splitted[1].to_s.encode("utf-8")
           post_treated[key] = val
         end
         
         self.convert_post(@post, post_treated, {:urldecode => true})
       end
     end
	ensure
     @read = nil
     @speed = nil
     @clength = nil
     @percent = nil
     @secs_left = nil
     
     #If it doesnt get unset we could have a serious memory reference GC problem.
     if @get["_kas_httpsession_id"]
       @kas.httpsessions_ids.delete(@get["_kas_httpsession_id"])
     end
   end
end