lib/udp_rest/uhttp.rb in udp_rest-0.9.0 vs lib/udp_rest/uhttp.rb in udp_rest-0.9.2

- old
+ new

@@ -1,13 +1,15 @@ require 'uri' +require 'cgi' require 'socket' module UDPRest class UDPRest::UHTTPRequest attr_accessor :req_method attr_accessor :path + attr_accessor :query attr_accessor :protocol def initialize self.req_method = 'GET' self.protocol = 'UHTTP/1.0' @@ -20,17 +22,41 @@ data = text.split(' ') raise 'invalid request' if data.length != 3 req = self.new req.req_method = data[0] - req.path = data[1] req.protocol = data[2] + + path_data = data[1].split('?') + req.path = path_data[0] + req.query = path_data[1] || '' if path_data.length > 1 + return req end def to_s self.path = '/' if path.nil? || path.empty? - "#{req_method} #{path} #{protocol}\n" + "#{req_method} #{path_and_query} #{protocol}\n" + end + + def path_and_query + if query.nil? || query.strip == '' + path + else + path + '?' + query + end + end + + def params + return {} if query.nil? || query.strip == '' + + if @params.nil? + @params = {} + p = CGI.parse(self.query) + p.each {|k,v| @params[k] = v.first } + end + + @params end end class UDPRest::UHTTPResponse attr_accessor :code \ No newline at end of file