Refactor HTTP::Client calls into make_client

This commit is contained in:
Omar Roth 2019-04-17 09:06:31 -05:00
parent ae7b5fac74
commit b6fff53b21
4 changed files with 19 additions and 12 deletions

View File

@ -45,7 +45,7 @@ ARCHIVE_URL = URI.parse("https://archive.org")
LOGIN_URL = URI.parse("https://accounts.google.com") LOGIN_URL = URI.parse("https://accounts.google.com")
PUBSUB_URL = URI.parse("https://pubsubhubbub.appspot.com") PUBSUB_URL = URI.parse("https://pubsubhubbub.appspot.com")
REDDIT_URL = URI.parse("https://www.reddit.com") REDDIT_URL = URI.parse("https://www.reddit.com")
TEXTCAPTCHA_URL = URI.parse("http://textcaptcha.com/omarroth@protonmail.com.json") TEXTCAPTCHA_URL = URI.parse("http://textcaptcha.com")
YT_URL = URI.parse("https://www.youtube.com") YT_URL = URI.parse("https://www.youtube.com")
CHARS_SAFE = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_" CHARS_SAFE = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_"
CURRENT_BRANCH = {{ "#{`git branch | sed -n '/\* /s///p'`.strip}" }} CURRENT_BRANCH = {{ "#{`git branch | sed -n '/\* /s///p'`.strip}" }}
@ -2972,7 +2972,7 @@ get "/api/v1/annotations/:id" do |env|
env.response.status_code = location.status_code env.response.status_code = location.status_code
end end
response = HTTP::Client.get(URI.parse(location.headers["Location"])) response = make_client(URI.parse(location.headers["Location"])).get(location.headers["Location"])
if response.body.empty? if response.body.empty?
env.response.status_code = 404 env.response.status_code = 404

View File

@ -619,6 +619,8 @@ def cache_annotation(db, id, annotations)
end end
end end
if has_legacy_annotations
# TODO: Update on conflict? # TODO: Update on conflict?
db.exec("INSERT INTO annotations VALUES ($1, $2) ON CONFLICT DO NOTHING", id, annotations) db.exec("INSERT INTO annotations VALUES ($1, $2) ON CONFLICT DO NOTHING", id, annotations)
end
end end

View File

@ -18,13 +18,18 @@ def elapsed_text(elapsed)
"#{(millis * 1000).round(2)}µs" "#{(millis * 1000).round(2)}µs"
end end
def make_client(url, proxies = {} of String => Array({ip: String, port: Int32}), region = nil) def make_client(url : URI, proxies = {} of String => Array({ip: String, port: Int32}), region = nil)
context = nil
if url.scheme == "https"
context = OpenSSL::SSL::Context::Client.new context = OpenSSL::SSL::Context::Client.new
context.add_options( context.add_options(
OpenSSL::SSL::Options::ALL | OpenSSL::SSL::Options::ALL |
OpenSSL::SSL::Options::NO_SSL_V2 | OpenSSL::SSL::Options::NO_SSL_V2 |
OpenSSL::SSL::Options::NO_SSL_V3 OpenSSL::SSL::Options::NO_SSL_V3
) )
end
client = HTTPClient.new(url, context) client = HTTPClient.new(url, context)
client.read_timeout = 10.seconds client.read_timeout = 10.seconds
client.connect_timeout = 10.seconds client.connect_timeout = 10.seconds

View File

@ -331,7 +331,7 @@ def generate_captcha(key, db)
end end
def generate_text_captcha(key, db) def generate_text_captcha(key, db)
response = HTTP::Client.get(TEXTCAPTCHA_URL).body response = make_client(TEXTCAPTCHA_URL).get("/omarroth@protonmail.com.json").body
response = JSON.parse(response) response = JSON.parse(response)
tokens = response["a"].as_a.map do |answer| tokens = response["a"].as_a.map do |answer|