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.
Merge pull request #162 from gsamokovarov/content-inside-of-body
Render the console inside the body tag
- Loading branch information
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.