Sha256: 762b54e4c255610043b1fb0f8e3a09568c8b4b79574c71657cf7d05c29bf7cc4

Contents?: true

Size: 1.87 KB

Versions: 14

Compression:

Stored size: 1.87 KB

Contents

# encoding: binary

module Rex
  module Proto
    # SIP protocol support
    module SIP
      SIP_STATUS_REGEX = /^SIP\/(\d\.\d) (\d{3})\s*(.*)$/

      # Represents a generic SIP message
      class Message
        attr_accessor :headers

        def initialize
          @headers = {}
        end

        # Returns a list of all values from all +name+ headers, regardless of case,
        # or nil if no matching header is found
        def header(name)
          matches = @headers.select { |k, _| k.downcase == name.downcase }
          return nil if matches.empty?
          matches.values.flatten
        end

        # Returns a hash of header name to values mapping
        # from the provided message, or nil if no headers
        # are found
        def self.extract_headers(message)
          pairs = message.scan(/^([^\s:]+):\s*(.*)$/)
          return nil if pairs.empty?
          headers = {}
          pairs.each do |pair|
            headers[pair.first] ||= []
            headers[pair.first] << pair.last.strip
          end
          headers
        end
      end

      # Represents a SIP response message
      class Response < Message
        attr_accessor :code, :message, :status_line, :version

        # Parses +data+, constructs and returns a Response
        def self.parse(data)
          response = Response.new
          # do some basic sanity checking on this response to ensure that it is SIP
          response.status_line = data.split(/\r\n/)[0]
          unless response.status_line && response.status_line =~ SIP_STATUS_REGEX
            fail(ArgumentError, "Invalid SIP status line: #{response.status_line}")
          end
          response.version = Regexp.last_match(1)
          response.code = Regexp.last_match(2)
          response.message = Regexp.last_match(3)
          response.headers = extract_headers(data)
          response
        end
      end
    end
  end
end

Version data entries

14 entries across 14 versions & 3 rubygems

Version Path
rex-2.0.13 lib/rex/proto/sip/response.rb
rex-2.0.12 lib/rex/proto/sip/response.rb
rex-2.0.11 lib/rex/proto/sip/response.rb
rex-2.0.10 lib/rex/proto/sip/response.rb
rex-2.0.9 lib/rex/proto/sip/response.rb
rex-2.0.8 lib/rex/proto/sip/response.rb
rex-2.0.7 lib/rex/proto/sip/response.rb
rex-2.0.5 lib/rex/proto/sip/response.rb
rex-2.0.4 lib/rex/proto/sip/response.rb
dstruct-0.0.1 lib/rex/proto/sip/response.rb
rex-2.0.3 lib/rex/proto/sip/response.rb
librex-0.0.999 lib/rex/proto/sip/response.rb
rex-2.0.2 lib/rex/proto/sip/response.rb
librex-0.0.71 lib/rex/proto/sip/response.rb