Sha256: 24a8f690b78a349a6c64fd6707d6a08d4e6ca5a18bc2f338872a766cd5da8790

Contents?: true

Size: 1.63 KB

Versions: 1

Compression:

Stored size: 1.63 KB

Contents

require 'uri'
require 'socket'

module UDPRest

    class UDPRest::UHTTPRequest
        attr_accessor :req_method
        attr_accessor :path
        attr_accessor :protocol

        def initialize
            self.req_method = 'GET'
            self.protocol = 'UHTTP/1.0'
            self.path ='/'
        end

        def self.from_packet(p)
            text = p
            text = p.text if text.is_a? UDPPacket
            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]
            return req
        end

        def to_s
            self.path = '/' if path.nil? || path.empty?
            "#{req_method} #{path} #{protocol}\n"
        end
    end

    class UDPRest::UHTTPResponse
        attr_accessor :code
        attr_accessor :protocol
        attr_accessor :text

        def initialize(code, options = {})
            self.code = code.to_i
            self.protocol = options[:protocol] || 'UHTTP/1.0'
            self.text = options[:text] || ''
        end

        def self.parse(s)
            data = s.split("\n\n")
            status = data[0].split(' ')
            text = data[1] if data.length > 1
            self.new(status[1], :text => text || '')
        end

        def ok?
            code == 200
        end

        def status_text
            return 'OK' if ok?
            return 'FAILED'
        end

        def status_line
            "#{protocol} #{code} #{status_text}"
        end

        def to_s
            "#{status_line}\n\n#{text}"
        end
    end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
udp_rest-0.9.0 lib/udp_rest/uhttp.rb