Skip to content

Commit c4b889a

Browse files
committed
Misc updates.
1 parent d0e0b6f commit c4b889a

File tree

2 files changed

+20
-24
lines changed

2 files changed

+20
-24
lines changed

app/app.rb

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,12 @@ def halt_with_error(error)
4949
}.to_json
5050
end
5151

52-
def parser_object(url:, html:, content_type:)
52+
def parser_object(url:, html:)
5353
{
5454
url: url,
5555
options: {
5656
html: html,
57-
contentType: content_type
57+
contentType: "html"
5858
}
5959
}
6060
end
@@ -67,12 +67,20 @@ def download_with_http(url)
6767
.use(:auto_inflate)
6868
.get(url)
6969

70-
parser_object(url: url, html: response.to_s, content_type: response.headers[:content_type])
70+
parser_object(url: url, html: response.to_s)
7171
end
7272

73-
def authenticate(user, signature, url)
73+
def authenticate(user, signature)
74+
url = begin
75+
Base64.urlsafe_decode64(params["base64_url"])
76+
rescue NoMethodError
77+
halt_with_error("Invalid request. Missing base64_url parameter.")
78+
end
79+
7480
halt_with_error("User does not exist: #{user}.") unless $users.key?(user)
7581
halt_with_error("Invalid signature.") unless signature_valid?(user, signature, url)
82+
83+
url
7684
end
7785

7886
def response_error!(exception, url, user)
@@ -87,39 +95,26 @@ def response_error!(exception, url, user)
8795
end
8896

8997
get "/parser/:user/:signature" do
90-
url = begin
91-
Base64.urlsafe_decode64(params["base64_url"])
92-
rescue NoMethodError
93-
halt_with_error("Invalid request. Missing base64_url parameter.")
94-
end
95-
98+
url = authenticate(params["user"], params["signature"])
9699
logger.info "url=#{url}"
97-
98-
authenticate(params["user"], params["signature"], url)
99-
100100
payload = download_with_http(url)
101-
102101
parse_with_mercury(payload)
103102
rescue => exception
104103
response_error!(exception, url, params["user"])
105104
end
106105

107106
post "/parser/:user/:signature" do
107+
url = authenticate(params["user"], params["signature"])
108+
108109
json = begin
109110
JSON.parse(request.body.read)
110111
rescue JSON::ParserError
111112
halt_with_error("Invalid JSON body.")
112113
end
113114

114-
halt_with_error("Missing url field in JSON body.") unless json["url"]
115115
halt_with_error("Missing body field in JSON body.") unless json["body"]
116-
117-
logger.info "url=#{json["url"]}"
118-
119-
authenticate(params["user"], params["signature"], json["url"])
120-
121-
payload = parser_object(url: json["url"], html: json["body"], content_type: "text/html")
122-
116+
logger.info "url=#{url}"
117+
payload = parser_object(url: url, html: json["body"])
123118
parse_with_mercury(payload)
124119
rescue => exception
125120
response_error!(exception, url, params["user"])

test/app_test.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,14 @@ def test_parser_with_invalid_signature
4242

4343
def test_post_parser_with_valid_signature
4444
url = "https://example.com"
45+
base64_url = Base64.urlsafe_encode64(url)
4546
signature = OpenSSL::HMAC.hexdigest("sha1", @key, url)
4647
title = "The Title"
4748
html_body = "<title>#{title}</title>"
4849

49-
post "/parser/#{@user}/#{signature}", {url: url, body: html_body}.to_json, "CONTENT_TYPE" => "application/json"
50+
post "/parser/#{@user}/#{signature}?base64_url=#{base64_url}", {url: url, body: html_body}.to_json, "CONTENT_TYPE" => "application/json"
5051

51-
assert_equal "application/json; charset=utf-8", last_response.content_type
5252
assert_equal title, JSON.load(last_response.body).fetch("title")
53+
assert_equal "application/json; charset=utf-8", last_response.content_type
5354
end
5455
end

0 commit comments

Comments
 (0)