Class: OverSIP::SIP::Request

Inherits:
Message
  • Object
show all
Includes:
Core
Defined in:
lib/oversip/sip/request.rb,
ext/sip_parser/sip_parser_ruby.c

Constant Summary collapse

SECURE_TRANSPORTS =
{ :tls=>true, :wss=>true }

Constants inherited from Message

Message::DIALOG_FORMING_METHODS, Message::EMPTY_ARRAY, Message::OUTBOUND_AWARE_METHODS, Message::RECORD_ROUTING_AWARE_METHODS

Instance Attribute Summary collapse

Attributes inherited from Message

#body, #call_id, #connection, #contact, #contact_params, #content_length, #cseq, #from, #from_tag, #hdr_from, #hdr_route, #hdr_to, #hdr_via, #headers, #max_forwards, #num_vias, #proxy_require, #require, #routes, #sip_method, #sip_version, #source_ip, #source_ip_type, #source_port, #supported, #to, #to_tag, #transport, #tvars, #via_branch, #via_branch_id, #via_branch_rfc3261, #via_core_value, #via_has_alias, #via_has_rport, #via_params, #via_received, #via_rport, #via_sent_by_host, #via_sent_by_port

Instance Method Summary collapse

Methods included from Core

#check_max_forwards, #connection_outbound_flow_token, #create_transaction, #destination_myself?, #fix_nat, #incoming_outbound_requested?, #loose_route, #outgoing_outbound_requested?

Methods inherited from Message

#append_header, #close_connection, #contact_reg_id?, #delete_header, #delete_header_top, #dialog_forming?, #has_header?, #header_all, #header_top, #insert_header, #outbound_aware?, #record_routing_aware?, #replace_header_top, #set_header, #tcp?, #tls?, #udp?, #unknown_method?, #via_alias?, #via_rport?, #websocket?, #ws?, #wss?

Methods included from Logger

fg_system_msg2str, load_methods

Instance Attribute Details

#antiloop_idObject

Returns the value of attribute antiloop_id.



12
13
14
# File 'lib/oversip/sip/request.rb', line 12

def antiloop_id
  @antiloop_id
end

#cvarsObject

Connection variables (a hash).



22
23
24
# File 'lib/oversip/sip/request.rb', line 22

def cvars
  @cvars
end

#from_was_modifiedObject (readonly)

Set to true if the From / To has been modified prior to routing the request.



16
17
18
# File 'lib/oversip/sip/request.rb', line 16

def from_was_modified
  @from_was_modified
end

#in_rrObject

Used for internal purposes when doing proxy and adding the first Record-Route or Path.



20
21
22
# File 'lib/oversip/sip/request.rb', line 20

def in_rr
  @in_rr
end

#incoming_outbound_requested=(value) ⇒ Object (writeonly)

Sets the attribute incoming_outbound_requested

Parameters:

  • value

    the value to set the attribute incoming_outbound_requested to.



14
15
16
# File 'lib/oversip/sip/request.rb', line 14

def incoming_outbound_requested=(value)
  @incoming_outbound_requested = value
end

#new_max_forwardsObject (readonly)

Returns the value of attribute new_max_forwards.



11
12
13
# File 'lib/oversip/sip/request.rb', line 11

def new_max_forwards
  @new_max_forwards
end

#outgoing_outbound_requested=(value) ⇒ Object (writeonly)

Sets the attribute outgoing_outbound_requested

Parameters:

  • value

    the value to set the attribute outgoing_outbound_requested to.



14
15
16
# File 'lib/oversip/sip/request.rb', line 14

def outgoing_outbound_requested=(value)
  @outgoing_outbound_requested = value
end

#proxiedObject

If true it means that this request has been already proxied.



15
16
17
# File 'lib/oversip/sip/request.rb', line 15

def proxied
  @proxied
end

#route_outbound_flow_tokenObject

Returns the value of attribute route_outbound_flow_token.



13
14
15
# File 'lib/oversip/sip/request.rb', line 13

def route_outbound_flow_token
  @route_outbound_flow_token
end

#ruriObject

Returns the value of attribute ruri.



10
11
12
# File 'lib/oversip/sip/request.rb', line 10

def ruri
  @ruri
end

#server_transactionObject

Returns the value of attribute server_transaction.



9
10
11
# File 'lib/oversip/sip/request.rb', line 9

def server_transaction
  @server_transaction
end

#to_was_modifiedObject (readonly)

Set to true if the From / To has been modified prior to routing the request.



16
17
18
# File 'lib/oversip/sip/request.rb', line 16

def to_was_modified
  @to_was_modified
end

Instance Method Details

#in_dialog?Boolean

Returns:

  • (Boolean)


33
# File 'lib/oversip/sip/request.rb', line 33

def in_dialog?    ; @to_tag      end

#initial?Boolean

Returns:

  • (Boolean)


32
# File 'lib/oversip/sip/request.rb', line 32

def initial?      ; ! @to_tag    end

#log_idObject



25
26
27
# File 'lib/oversip/sip/request.rb', line 25

def log_id
  @log_id ||= "SIP Request #{@via_branch_id}"
end

#reply(status_code, reason_phrase = nil, extra_headers = [], body = nil) ⇒ Object



40
41
42
43
44
45
46
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
# File 'lib/oversip/sip/request.rb', line 40

def reply status_code, reason_phrase=nil, extra_headers=[], body=nil
  if @sip_method == :ACK
    log_system_error "attemtp to reply to an ACK aborted"
    return false
  end
  return false  unless @server_transaction.receive_response(status_code)  if @server_transaction

  reason_phrase ||= REASON_PHRASE[status_code] || REASON_PHRASE_NOT_SET

  if status_code > 100
    @internal_to_tag ||= @to_tag || ( @server_transaction ? ::SecureRandom.hex(6) : ::OverSIP::SIP::Tags.totag_for_sl_reply )
  end

  response = "SIP/2.0 #{status_code} #{reason_phrase}\r\n"

  @hdr_via.each do |hdr|
    response << "Via: " << hdr << CRLF
  end

  response << "From: " << @hdr_from << CRLF

  response << "To: " << @hdr_to
  response << ";tag=#{@internal_to_tag}"  if @internal_to_tag
  response << CRLF

  response << "Call-ID: " << @call_id << CRLF
  response << "CSeq: " << @cseq.to_s << " " << @sip_method.to_s << CRLF
  response << "Content-Length: #{body ? body.bytesize : "0"}\r\n"

  extra_headers.each do |header|
    response << header.to_s << CRLF
  end  if extra_headers

  response << HDR_SERVER << CRLF
  response << CRLF

  response << body  if body

  @server_transaction.last_response = response  if @server_transaction

  log_system_debug "replying #{status_code} \"#{reason_phrase}\""  if $oversip_debug

  send_response(response)
  true
end

#reply_full(response) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/oversip/sip/request.rb', line 87

def reply_full response
  return false  unless @server_transaction.receive_response(response.status_code)  if @server_transaction

  # Ensure the response has Content-Length. Add it otherwise.
  if response.body
    response.headers["Content-Length"] = [ response.body.bytesize.to_s ]
  else
    response.headers["Content-Length"] = HDR_ARRAY_CONTENT_LENGTH_0
  end

  response_leg_a = response.to_s
  @server_transaction.last_response = response_leg_a  if @server_transaction

  log_system_debug "forwarding response #{response.status_code} \"#{response.reason_phrase}\""  if $oversip_debug

  send_response(response_leg_a)
  true
end

#request?Boolean

Returns:

  • (Boolean)


29
# File 'lib/oversip/sip/request.rb', line 29

def request?      ; true         end

#response?Boolean

Returns:

  • (Boolean)


30
# File 'lib/oversip/sip/request.rb', line 30

def response?     ; false        end

#secure?Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/oversip/sip/request.rb', line 35

def secure?
  SECURE_TRANSPORTS[@transport] || false
end

#send_response(response) ⇒ Object



137
138
139
140
141
142
143
144
145
146
147
# File 'lib/oversip/sip/request.rb', line 137

def send_response(response)
  unless (case @transport
    when :udp
      @connection.send_sip_msg response, @source_ip, @via_rport || @via_sent_by_port || 5060
    else
      @connection.send_sip_msg response
    end
  )
    log_system_notice "error sending the SIP response"
  end
end

#to_sObject



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
# File 'lib/oversip/sip/request.rb', line 150

def to_s
  msg = "#{@sip_method.to_s} #{@ruri.uri} SIP/2.0\r\n"

  # Update From/To/Contact headers if modified.
  if @from.modified?
    @headers["From"] = [ @from.to_s << (@from_tag ? ";tag=#{@from_tag}" : "") ]
    @from_was_modified = true
  end
  if @to.modified?
    @headers["To"] = [ @to.to_s << (@to_tag ? ";tag=#{@to_tag}" : "") ]
    @to_was_modified = true
  end
  if @contact and @contact.modified?
    @headers["Contact"] = [ @contact.to_s << (@contact_params ? @contact_params : "") ]
  end

  @headers.each do |name, values|
    values.each do |value|
      msg << name << ": #{value}\r\n"
    end
  end

  msg << CRLF
  msg << @body  if @body
  msg
end