Skip to content

Commit 135ba35

Browse files
committed
Replaced OpenURI with NET::HTTP and introduced timeout of 3 seconds
1 parent 7b8679c commit 135ba35

File tree

1 file changed

+16
-5
lines changed

1 file changed

+16
-5
lines changed

lib/jekyll-gist/gist_tag.rb

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
require 'cgi'
2-
require 'open-uri'
2+
require 'net/http'
33

44
module Jekyll
55
module Gist
@@ -57,13 +57,24 @@ def gist_noscript_tag(gist_id, filename = nil)
5757
end
5858

5959
def fetch_raw_code(gist_id, filename = nil)
60+
url = "https://gist.githubusercontent.com/#{gist_id}/raw"
61+
url = "#{url}/#{filename}" unless filename.empty?
6062
begin
61-
url = "https://gist.githubusercontent.com/#{gist_id}/raw"
62-
url = "#{url}/#{filename}" unless filename.empty?
63-
open(url).read.chomp
63+
uri = URI(url)
64+
Net::HTTP.start(uri.host, uri.port,
65+
use_ssl: uri.scheme == 'https',
66+
read_timeout: 3, open_timeout: 3) do |http|
67+
request = Net::HTTP::Get.new uri
68+
response = http.request(request)
69+
response.body.chomp
70+
end
6471
rescue SocketError
6572
nil
66-
rescue OpenURI::HTTPError
73+
rescue Net::HTTPError
74+
nil
75+
rescue Net::OpenTimeout
76+
nil
77+
rescue Net::ReadTimeout
6778
nil
6879
end
6980
end

0 commit comments

Comments
 (0)