From b6fff53b21e416ddfa9b010710786d5e23890271 Mon Sep 17 00:00:00 2001 From: Omar Roth Date: Wed, 17 Apr 2019 09:06:31 -0500 Subject: [PATCH] Refactor HTTP::Client calls into make_client --- src/invidious.cr | 4 ++-- src/invidious/helpers/helpers.cr | 6 ++++-- src/invidious/helpers/utils.cr | 19 ++++++++++++------- src/invidious/users.cr | 2 +- 4 files changed, 19 insertions(+), 12 deletions(-) diff --git a/src/invidious.cr b/src/invidious.cr index 8ed4253f..3abd9c51 100644 --- a/src/invidious.cr +++ b/src/invidious.cr @@ -45,7 +45,7 @@ ARCHIVE_URL = URI.parse("https://archive.org") LOGIN_URL = URI.parse("https://accounts.google.com") PUBSUB_URL = URI.parse("https://pubsubhubbub.appspot.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") CHARS_SAFE = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_" 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 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? env.response.status_code = 404 diff --git a/src/invidious/helpers/helpers.cr b/src/invidious/helpers/helpers.cr index ac16fc0e..9b29210d 100644 --- a/src/invidious/helpers/helpers.cr +++ b/src/invidious/helpers/helpers.cr @@ -619,6 +619,8 @@ def cache_annotation(db, id, annotations) end end - # TODO: Update on conflict? - db.exec("INSERT INTO annotations VALUES ($1, $2) ON CONFLICT DO NOTHING", id, annotations) + if has_legacy_annotations + # TODO: Update on conflict? + db.exec("INSERT INTO annotations VALUES ($1, $2) ON CONFLICT DO NOTHING", id, annotations) + end end diff --git a/src/invidious/helpers/utils.cr b/src/invidious/helpers/utils.cr index eb304d8d..c1aea5b2 100644 --- a/src/invidious/helpers/utils.cr +++ b/src/invidious/helpers/utils.cr @@ -18,13 +18,18 @@ def elapsed_text(elapsed) "#{(millis * 1000).round(2)}µs" end -def make_client(url, proxies = {} of String => Array({ip: String, port: Int32}), region = nil) - context = OpenSSL::SSL::Context::Client.new - context.add_options( - OpenSSL::SSL::Options::ALL | - OpenSSL::SSL::Options::NO_SSL_V2 | - OpenSSL::SSL::Options::NO_SSL_V3 - ) +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.add_options( + OpenSSL::SSL::Options::ALL | + OpenSSL::SSL::Options::NO_SSL_V2 | + OpenSSL::SSL::Options::NO_SSL_V3 + ) + end + client = HTTPClient.new(url, context) client.read_timeout = 10.seconds client.connect_timeout = 10.seconds diff --git a/src/invidious/users.cr b/src/invidious/users.cr index bad2b5c3..689d9434 100644 --- a/src/invidious/users.cr +++ b/src/invidious/users.cr @@ -331,7 +331,7 @@ def generate_captcha(key, db) end 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) tokens = response["a"].as_a.map do |answer|