class RProxy::HttpProxyParser

Attributes

password[R]
username[R]

Public Class Methods

new(usage_manager) click to toggle source
# File lib/r_proxy/http_proxy_parser.rb, line 8
def initialize(usage_manager)
  @usage_manager = usage_manager
  @max_connection_size = 4 * 1024
end

Public Instance Methods

parse(data, need_auth) click to toggle source
# File lib/r_proxy/http_proxy_parser.rb, line 13
def parse(data, need_auth)
  parse_connect_request(data)
  remain = 0
  remain = auth_user if need_auth

  [@schema.host, @schema.port, remain]
end

Private Instance Methods

auth_user() click to toggle source
# File lib/r_proxy/http_proxy_parser.rb, line 23
def auth_user
  begin
    temp = @headers['proxy-authorization']
    raise RProxy::HTTPNotSupport if temp.nil?
    pattern = /^Basic /
    token = temp.gsub(pattern, '')
    str = Base64.decode64(token)
    @username, @password = str.split(':')
  rescue
    raise RProxy::HTTPNotSupport, "token parse failed #{token}"
  end

  auth_result = @usage_manager.auth_user(@username, @password)
  raise RProxy::HTTPAuthFailed if auth_result.nil?
  auth_result
end
check_is_valid_request(s) click to toggle source
# File lib/r_proxy/http_proxy_parser.rb, line 64
def check_is_valid_request(s)
  # hold for heath check, if needed.
  # s[0...4] == "GET\s"
  s == RProxy::Constants::HTTP_CONNECT_TITLE
end
parse_connect_request(data) click to toggle source
# File lib/r_proxy/http_proxy_parser.rb, line 40
def parse_connect_request(data)
  size_of_data = data.bytesize
  raise RProxy::HTTPNotSupport unless
    size_of_data <= @max_connection_size && check_is_valid_request(data[0...8])
  temp = data.split("\r\n")
  @schema = parse_connect_target(temp.shift)
  @headers = parse_header(temp)
end
parse_connect_target(data) click to toggle source
# File lib/r_proxy/http_proxy_parser.rb, line 59
def parse_connect_target(data)
  temp = data.split("\s")
  URI("tcp://#{temp[1]}/")
end
parse_header(arr) click to toggle source
# File lib/r_proxy/http_proxy_parser.rb, line 49
def parse_header(arr)
  headers = {}
  arr.each do |val|
    name, value = val.split(':')
    next if name.nil?
    headers[name.strip.downcase] = value&.strip
  end
  headers
end