Class: Quaff::SipParser

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#stateObject (readonly)

Returns the value of attribute state.



8
9
10
# File 'lib/sip_parser.rb', line 8

def state
  @state
end

Instance Method Details

#message_identifier(msg) ⇒ Object



34
35
36
# File 'lib/sip_parser.rb', line 34

def message_identifier(msg)
  msg.header("Call-ID")
end

#parse_line_blank(line) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/sip_parser.rb', line 38

def parse_line_blank line
  if line == ""
    # skip empty lines
    return
  else
    parts = line.split " ", 3
    if parts[2] == "SIP/2.0"
      # Looks like a SIP request
      @msg.method = parts[0]
      @msg.requri = parts[1]
      @state = :first_line_parsed
      return
    elsif parts[0] == "SIP/2.0"
      # Looks like a SIP response
      @msg.status_code = parts[1]
      @msg.reason = parts[2] || ""
      @state = :first_line_parsed
      return
    else
      raise parts.inspect
    end
  end
  # We haven't returned, so looks like a malformed line
  raise line.inspect
end

#parse_line_body(line) ⇒ Object



92
93
94
95
96
97
98
# File 'lib/sip_parser.rb', line 92

def parse_line_body line
  @msg.body << line
  @msg.body << "\r\n"
  if line == "" or @msg.body.length >= @msg.header("Content-Length").to_i
    @state = :done
  end
end

#parse_line_first_line_parsed(line) ⇒ Object



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
# File 'lib/sip_parser.rb', line 64

def parse_line_first_line_parsed line
  if line.start_with? " "
    @msg.headers[@cur_hdr][-1] += " "
    @msg.headers[@cur_hdr][-1] += line.lstrip
  elsif line.include? ":"
    parts = line.split ":", 2
    header_name, header_value = parts[0].rstrip, parts[1].lstrip
    @msg.headers[header_name] ||= []
    @msg.headers[header_name].push header_value
    @cur_hdr = header_name
    if header_name == "Content-Length"
      @state = :got_content_length
      @msg.headers[header_name] = [header_value]
    else
      if (@state != :got_content_length)
        @state = :middle_of_headers
      end
    end
  elsif line == ""
    if (@state == :got_content_length) and (@msg.header("Content-Length").to_i > 0)
      @state = :parsing_body
    else
      @state = :done
    end
  else raise line.inspect
  end
end

#parse_partial(data) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/sip_parser.rb', line 15

def parse_partial(data)
  return nil if data.nil?
  data.lines.each do |line|
    line.rstrip!
    if @state == :blank
      parse_line_blank line
    elsif @state == :parsing_body
      parse_line_body line
    else
      parse_line_first_line_parsed line
    end
  end
  if @state == :done
    return @msg
  else
    return nil
  end
end

#parse_startObject



9
10
11
12
13
# File 'lib/sip_parser.rb', line 9

def parse_start
  @buf = ""
  @msg = SipMessage.new
  @state = :blank
end