Skip to content

Commit

Permalink
origin tweak
Browse files Browse the repository at this point in the history
  • Loading branch information
defunkt committed Jul 20, 2011
1 parent b3bcd30 commit 0841f18
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions bin/djsd
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -23,14 +23,22 @@ dotjs = Class.new(WEBrick::HTTPServlet::AbstractServlet) do
body << File.read(file) if File.file?(file) body << File.read(file) if File.file?(file)


response.status = body.empty? ? 204 : 200 response.status = body.empty? ? 204 : 200
if request.header['origin'].length == 1 and if origin = detect_origin(request)
request.path.length != 1 and response['Access-Control-Allow-Origin'] = origin
request.header['origin'][0].match(request.path.gsub('/','').gsub(/\.js$/,'') + '$')
response['Access-Control-Allow-Origin'] = request.header['origin'][0]
end end
response['Content-Type'] = 'text/javascript' response['Content-Type'] = 'text/javascript'
response.body = body response.body = body
end end

def detect_origin(req)
path = req.path
origin = req.header['origin']
search = path.gsub('/','').gsub(/\.js$/,'') + '$'

if origin.length == 1 && path.length != 1 && origin[0].match(search)
origin[0]
end
end
end end


server = WEBrick::HTTPServer.new(:Port => 3131) server = WEBrick::HTTPServer.new(:Port => 3131)
Expand Down

0 comments on commit 0841f18

Please sign in to comment.