Module: Pod::HTTP

Defined in:
lib/cocoapods-core/http.rb

Overview

Handles HTTP requests

Constant Summary collapse

MAX_HTTP_REDIRECTS =
3
USER_AGENT =
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10) AppleWebKit/538.43.40 (KHTML, like Gecko) Version/8.0 Safari/538.43.40'

Class Method Summary collapse

Class Method Details

.get_actual_url(url, user_agent = nil) ⇒ string

Resolve potential redirects and return the final URL.

Returns:

  • (string)


11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/cocoapods-core/http.rb', line 11

def self.get_actual_url(url, user_agent = nil)
  redirects = 0

  loop do
    response = perform_head_request(url, user_agent)

    if [301, 302, 303, 307, 308].include? response.status_code
      location = response.headers['location'].first

      if location =~ %r{://}
        url = location
      else
        url = URI.join(url, location).to_s
      end

      redirects += 1
    else
      break
    end

    break unless redirects < MAX_HTTP_REDIRECTS
  end

  url
end

.perform_head_request(url, user_agent) ⇒ REST::response (private)

Does a HEAD request and in case of any errors a GET request

Returns:

  • (REST::response)


62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/cocoapods-core/http.rb', line 62

def self.perform_head_request(url, user_agent)
  require 'rest'

  user_agent ||= USER_AGENT

  resp = ::REST.head(url, 'User-Agent' => user_agent)

  if resp.status_code >= 400
    resp = ::REST.get(url, 'User-Agent' => user_agent,
                           'Range' => 'bytes=0-0')

    if resp.status_code >= 400
      resp = ::REST.get(url, 'User-Agent' => user_agent)
    end
  end

  resp
end

.validate_url(url, user_agent = nil) ⇒ REST::response

Performs validation of a URL

Returns:

  • (REST::response)


41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/cocoapods-core/http.rb', line 41

def self.validate_url(url, user_agent = nil)
  return nil unless url =~ /^#{URI.regexp}$/

  begin
    url = get_actual_url(url, user_agent)
    resp = perform_head_request(url, user_agent)
  rescue SocketError, URI::InvalidURIError, REST::Error, REST::Error::Connection
    resp = nil
  end

  resp
end