From 525e9433703a00644ad757af3c256d86be21b62d Mon Sep 17 00:00:00 2001 From: Gabriel Arazas Date: Mon, 6 Mar 2023 23:43:08 +0800 Subject: [PATCH] Update OpenURI.open_uri handling of headers --- .../github-raw-content-include-processor/extension.rb | 11 +++++++---- .../gitlab-raw-content-include-processor/extension.rb | 11 +++++------ 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/lib/asciidoctor/github-raw-content-include-processor/extension.rb b/lib/asciidoctor/github-raw-content-include-processor/extension.rb index ede0cac..32af086 100644 --- a/lib/asciidoctor/github-raw-content-include-processor/extension.rb +++ b/lib/asciidoctor/github-raw-content-include-processor/extension.rb @@ -33,11 +33,14 @@ class GitHubRawIncludeProcessor < Asciidoctor::Extensions::IncludeProcessor end begin - OpenURI.open_uri( - uri, + headers = { 'Header' => 'application/vnd.github+json', - 'X-GitHub-Api-Version' => '2022-11-28' - ) do |f| + 'X-GitHub-Api-Version' => '2022-11-28', + } + + headers['Authorization'] = "Token #{ENV['GITHUB_API_BEARER_TOKEN']}" if ENV['GITHUB_API_BEARER_TOKEN'] + + OpenURI.open_uri(uri, headers) do |f| response = JSON.parse(f.read) # If the response is an array, it is likely to be a directory. In this diff --git a/lib/asciidoctor/gitlab-raw-content-include-processor/extension.rb b/lib/asciidoctor/gitlab-raw-content-include-processor/extension.rb index d6422b9..a5f0280 100644 --- a/lib/asciidoctor/gitlab-raw-content-include-processor/extension.rb +++ b/lib/asciidoctor/gitlab-raw-content-include-processor/extension.rb @@ -45,12 +45,11 @@ class GitLabRawIncludeProcessor < Asciidoctor::Extensions::IncludeProcessor query = { :ref => rev } uri.query = URI.encode_www_form query - begin - OpenURI.open_uri( - uri, - 'Content-Type' => 'application/json', - 'PRIVATE-TOKEN' => ENV['GITLAB_API_PERSONAL_ACCESS_TOKEN'], - ) do |f| + content = begin + headers = { 'Content-Type' => 'application-json' } + header['PRIVATE-TOKEN'] = ENV['GITLAB_API_PERSONAL_ACCESS_TOKEN'] if ENV['GITLAB_API_PERSONAL_ACCESS_TOKEN'] + + OpenURI.open_uri(uri, headers) do |f| response = JSON.parse(f.read) if response['content'] && response['encoding'] == 'base64'