Skip to content

Commit

Permalink
Support param in sinatra analyzer
Browse files Browse the repository at this point in the history
  • Loading branch information
hahwul committed Aug 6, 2023
1 parent 0b33d67 commit dd4bd2c
Showing 1 changed file with 69 additions and 44 deletions.
113 changes: 69 additions & 44 deletions src/analyzer/analyzers/analyzer_sinatra.cr
Original file line number Diff line number Diff line change
Expand Up @@ -4,51 +4,21 @@ class AnalyzerSinatra < Analyzer
def analyze
# Source Analysis
Dir.glob("#{@base_path}/**/*") do |path|
spawn do
next if File.directory?(path)
if File.exists?(path)
File.open(path, "r") do |file|
file.each_line do |line|
line.scan(/get\s+['"](.+?)['"]/) do |match|
if match.size > 1
@result << Endpoint.new("#{@url}#{match[1]}", "GET")
end
end

line.scan(/post\s+['"](.+?)['"]/) do |match|
if match.size > 1
@result << Endpoint.new("#{@url}#{match[1]}", "POST")
end
end

line.scan(/put\s+['"](.+?)['"]/) do |match|
if match.size > 1
@result << Endpoint.new("#{@url}#{match[1]}", "PUT")
end
end

line.scan(/delete\s+['"](.+?)['"]/) do |match|
if match.size > 1
@result << Endpoint.new("#{@url}#{match[1]}", "DELETE")
end
end

line.scan(/patch\s+['"](.+?)['"]/) do |match|
if match.size > 1
@result << Endpoint.new("#{@url}#{match[1]}", "PATCH")
end
end

line.scan(/head\s+['"](.+?)['"]/) do |match|
if match.size > 1
@result << Endpoint.new("#{@url}#{match[1]}", "HEAD")
end
end
next if File.directory?(path)
if File.exists?(path)
File.open(path, "r") do |file|
last_endpoint = Endpoint.new("", "")
file.each_line do |line|
endpoint = line_to_endpoint(line)
if endpoint.method != ""
@result << endpoint
last_endpoint = endpoint
end

line.scan(/options\s+['"](.+?)['"]/) do |match|
if match.size > 1
@result << Endpoint.new("#{@url}#{match[1]}", "OPTIONS")
end
param = line_to_param(line)
if param.name != ""
if last_endpoint.method != ""
last_endpoint.push_param(param)
end
end
end
Expand All @@ -58,6 +28,61 @@ class AnalyzerSinatra < Analyzer

@result
end

def line_to_param(content : String) : Param
if content.includes? "param["
param = content.split("param[")[1].split("]")[0].gsub("\"", "").gsub("'", "")
return Param.new(param, "", "query")
end

return Param.new("", "", "")
end

def line_to_endpoint(content : String) : Endpoint
content.scan(/get\s+['"](.+?)['"]/) do |match|
if match.size > 1
return Endpoint.new("#{@url}#{match[1]}", "GET")
end
end

content.scan(/post\s+['"](.+?)['"]/) do |match|
if match.size > 1
return Endpoint.new("#{@url}#{match[1]}", "POST")
end
end

content.scan(/put\s+['"](.+?)['"]/) do |match|
if match.size > 1
return Endpoint.new("#{@url}#{match[1]}", "PUT")
end
end

content.scan(/delete\s+['"](.+?)['"]/) do |match|
if match.size > 1
return Endpoint.new("#{@url}#{match[1]}", "DELETE")
end
end

content.scan(/patch\s+['"](.+?)['"]/) do |match|
if match.size > 1
return Endpoint.new("#{@url}#{match[1]}", "PATCH")
end
end

content.scan(/head\s+['"](.+?)['"]/) do |match|
if match.size > 1
return Endpoint.new("#{@url}#{match[1]}", "HEAD")
end
end

content.scan(/options\s+['"](.+?)['"]/) do |match|
if match.size > 1
return Endpoint.new("#{@url}#{match[1]}", "OPTIONS")
end
end

return Endpoint.new("", "")
end
end

def analyzer_sinatra(options : Hash(Symbol, String))
Expand Down

0 comments on commit dd4bd2c

Please sign in to comment.