forked from gsamokovarov/web-console
-
Notifications
You must be signed in to change notification settings - Fork 178
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Render the console inside the body tag
Before this commit, the console was rendered straight after the closing </html> tag, which made the page HTML invalid. However, browsers are used to be molested and they are really good at displaying whatever you throw at them, so that wasn't big problem anyway. Still it was something that bug me quite a bit. I'm trying to redeem myself here. Because I had to change the newly introduce `WebConsole::Response` class I had cleaned the workspace a bit: * `WebConsole::Response` no longer depends on a logger. The detection should be better now, so we don't need to couple the query check in it. * `WebConsole::Response` does one thing only. It used to check the acceptance of a given `Content-Type`. I moved this to the middleware class. * I have reverted the `WebConsole::WhinyRequest` to keep the logging out of the logic in the request class. We no longer need to log in two separate classes, so no need to break `Liskov` or introduce logging into simple query methods.
- Loading branch information
1 parent
b9ce945
commit a7b28cc
Showing
10 changed files
with
126 additions
and
154 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,23 @@ | ||
module WebConsole | ||
# Web Console tailored response object. | ||
class Response < Rack::Response | ||
def initialize(body, status, headers, logger = nil) | ||
@logger = logger | ||
super(body, status, headers) | ||
end | ||
# A response object that writes content before the closing </body> tag, if | ||
# possible. | ||
# | ||
# The object quacks like Rack::Response. | ||
class Response < Struct.new(:body, :status, :headers) | ||
def write(content) | ||
raw_body = Array(body).first.to_s | ||
|
||
# Returns whether the response is from an acceptable content type. | ||
# | ||
# We can render a console only for HTML responses. | ||
def acceptable_content_type? | ||
if content_type == Mime::HTML | ||
true | ||
if position = raw_body.rindex('</body>') | ||
raw_body.insert(position, content) | ||
else | ||
log_not_acceptable_content_type | ||
false | ||
raw_body << content | ||
end | ||
end | ||
|
||
def content_type | ||
formats = Mime::Type.parse(headers['Content-Type']) | ||
formats.first | ||
self.body = raw_body | ||
end | ||
|
||
private | ||
|
||
attr_reader :logger | ||
|
||
def log_not_acceptable_content_type | ||
if logger | ||
logger.info "Cannot render console with content type " \ | ||
"#{content_type}. Console can be rendered only in HTML responses" | ||
end | ||
end | ||
def finish | ||
Rack::Response.new(body, status, headers).finish | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
module WebConsole | ||
# Noisy wrapper around +Request+. | ||
# | ||
# If any calls to +from_whitelisted_ip?+ and +acceptable_content_type?+ | ||
# return false, an info log message will be displayed in users' logs. | ||
class WhinyRequest < SimpleDelegator | ||
def from_whitelited_ip? | ||
whine_unless request.from_whitelited_ip? do | ||
"Cannot render console from #{request.remote_ip}! " \ | ||
"Allowed networks: #{request.whitelisted_ips}" | ||
end | ||
end | ||
|
||
private | ||
|
||
def whine_unless(condition) | ||
unless condition | ||
logger.info { yield } | ||
end | ||
condition | ||
end | ||
|
||
def logger | ||
env['action_dispatch.logger'] || WebConsole.logger | ||
end | ||
|
||
def request | ||
__getobj__ | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.