Skip to content
This repository has been archived by the owner on May 17, 2023. It is now read-only.

Commit

Permalink
Merge pull request #18 from nolanbrown/feature/basic_authentication
Browse files Browse the repository at this point in the history
Added support for Basic Authentication
  • Loading branch information
turboladen committed Jan 14, 2013
2 parents 288c398 + 4f6d0c4 commit 6a7ea70
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
rtsp (0.4.3)
rtsp (0.4.4)
parslet (>= 1.1.0)
rtp (>= 0.1.3)
sdp (~> 0.2.6)
Expand Down
13 changes: 13 additions & 0 deletions bin/rtsp_client
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,19 @@ optparse = OptionParser.new do |opts|
exit
end

#----------------------------------------------------------------------------
# Get options
opts.on('--options [URL]', "Get options from the given URL.") do |url|
if url.nil?
puts "Must pass in a URL."
exit
end

rtsp_client = RTSP::Client.new(url)
puts rtsp_client.options.body.inspect
exit
end

#----------------------------------------------------------------------------
# Show available tracks
opts.on('--show-tracks [URL]', "Show available tracks from the given URL.") do |url|
Expand Down
35 changes: 32 additions & 3 deletions lib/rtsp/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
require 'tempfile'
require 'timeout'
require 'rtp/receiver'
require "base64"

require_relative 'transport_parser'
require_relative 'error'
Expand Down Expand Up @@ -60,7 +61,7 @@ class Client

# @return [URI] The URI that points to the RTSP server's resource.
attr_reader :server_uri

# @return [Fixnum] Also known as the "sequence" number, this starts at 1 and
# increments after every request to the server. It is reset after
# calling #teardown.
Expand Down Expand Up @@ -121,6 +122,7 @@ def initialize(server_url=nil)
@connection.do_capture ||= true
@connection.interleave ||= false

@max_authorization_tries = 3
@cseq = 1
reset_state
end
Expand All @@ -134,6 +136,20 @@ def server_url=(new_url)
@server_uri = build_resource_uri_from new_url
end

# The user to be used in Basic Authentication
#
# @param [String] user
def server_user=(user)
@server_uri.user = user
end

# The password to be used in Basic Authentication
#
# @param [String] password
def server_password=(password)
@server_uri.password = password
end

# Sends the message over the socket.
#
# @param [RTSP::Message] message
Expand Down Expand Up @@ -383,9 +399,10 @@ def request message
response = send_message message
#compare_sequence_number response.cseq
@cseq += 1

if response.code.to_s =~ /2../
yield response if block_given?
elsif response.code == 401
send_authorization(message)
elsif response.code.to_s =~ /(4|5)../
if (defined? response.connection) && response.connection == 'Close'
reset_state
Expand Down Expand Up @@ -441,7 +458,19 @@ def media_control_tracks
end

private


# Resends a message with an Authorization header when possible
# @param [RTSP:Message] message The message that must be repeated with Authorization
def send_authorization(message)
if @server_uri.user and @server_uri.password
credentials = Base64.strict_encode64("#{@server_uri.user}:#{@server_uri.password}")
headers = { :authorization => "Basic #{credentials}" }
new_message = RTSP::Message.send(message.method_type.to_sym,@server_uri.to_s).with_headers(headers)
new_message.add_headers message.headers
request(new_message)
end
end

# Sets state related variables back to their starting values;
# +@session_state+ is set to +:init+; +@session+ is set to 0.
def reset_state
Expand Down
8 changes: 8 additions & 0 deletions lib/rtsp/message.rb
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ def initialize(method_type, request_uri)
@headers = default_headers
@body = ""
@version = DEFAULT_VERSION

end

# Adds the header and its value to the list of headers for the message.
Expand Down Expand Up @@ -172,6 +173,13 @@ def message
def default_headers
headers = {}

if @request_uri.user and @request_uri.password
credentials = "#{@request_uri.user}:#{@request_uri.password}"
headers[:authorization] = "Basic #{Base64.strict_encode64(credentials)}"
@request_uri.user = nil
@request_uri.password = nil
end

headers[:cseq] ||= RTSP_DEFAULT_SEQUENCE_NUMBER
headers[:user_agent] ||= USER_AGENT

Expand Down

0 comments on commit 6a7ea70

Please sign in to comment.