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) ⇒ string

Resolve potential redirects and return the final URL.


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

def self.get_actual_url(url)
  redirects = 0

  loop do
    response = perform_head_request(url)

    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) ⇒ REST::response (private)

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


60
61
62
63
64
65
66
67
68
69
70
# File 'lib/cocoapods-core/http.rb', line 60

def self.perform_head_request(url)
  require 'rest'

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

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

  resp
end

.validate_url(url) ⇒ REST::response

Performs validation of a URL


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

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

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

  resp
end