Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Branch: master
Fetching contributors…

Cannot retrieve contributors at this time

executable file 122 lines (100 sloc) 3.93 kB
#!/usr/bin/env ruby
if (%w( -h --help help ) & ARGV).length > 0
puts "Usage: dcssd [-hv]"
puts "Starts dotcss server in the foreground. Kill with Control+C."
exit
end
if (%w( -v --version version ) & ARGV).length > 0
puts 'dcssd 2.2.0'
exit
end
if Dir.glob('*.css').empty? and not Dir.glob(File.join(ENV['HOME'], ".css/*.css")).empty?
Dir.chdir(File.join(ENV['HOME'], ".css"))
end
require 'webrick'
require 'webrick/https'
dotcss = Class.new(WEBrick::HTTPServlet::AbstractServlet) do
def do_GET(request, response)
body = build_body(request.path)
response.status = body.empty? ? 204 : 200
if origin = detect_origin(request)
response['Access-Control-Allow-Origin'] = origin
end
response['Content-Type'] = 'text/css'
response.body = body
end
def build_body(path)
files = []
paths = path.gsub('/', '').split('.')
until paths.empty?
file = File.expand_path(paths.join('.'))
files << file if File.file?(file)
paths.shift
end
files.sort_by! { |f| f.length }
# Default CSS to the front of the line!
files.unshift File.expand_path('default.css')
body = "/* dotcss is working */\n"
files.each do |file|
body << File.read(file) + "\n" if File.file?(file)
end
body
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
ssl_info = DATA.read
ssl_cert = ssl_info.scan(/(-----BEGIN CERTIFICATE-----.+?-----END CERTIFICATE-----)/m)[0][0]
ssl_key = ssl_info.scan(/(-----BEGIN RSA PRIVATE KEY-----.+?-----END RSA PRIVATE KEY-----)/m)[0][0]
server_options = {
:BindAddress => "127.0.0.1",
:Port => 1243,
:AccessLog => [],
:SSLEnable => true,
:SSLVerifyClient => OpenSSL::SSL::VERIFY_NONE,
:SSLPrivateKey => OpenSSL::PKey::RSA.new(ssl_key),
:SSLCertificate => OpenSSL::X509::Certificate.new(ssl_cert),
:SSLCertName => [["CN", WEBrick::Utils::getservername]],
}
server = WEBrick::HTTPServer.new(server_options)
server.mount('/', dotcss)
%w( INT TERM ).each do |sig|
trap(sig) { server.shutdown }
end
server.start
__END__
-----BEGIN CERTIFICATE-----
MIICHTCCAYYCCQClZE2IvNWbtDANBgkqhkiG9w0BAQUFADBTMQswCQYDVQQGEwJV
UzETMBEGA1UECBMKQ2FsaWZvcm5pYTELMAkGA1UEBxMCTEExDjAMBgNVBAoTBWRv
dGpzMRIwEAYDVQQDEwlsb2NhbGhvc3QwHhcNMTMwMjIwMjMzNzUzWhcNMjIxMTIw
MjMzNzUzWjBTMQswCQYDVQQGEwJVUzETMBEGA1UECBMKQ2FsaWZvcm5pYTELMAkG
A1UEBxMCTEExDjAMBgNVBAoTBWRvdGpzMRIwEAYDVQQDEwlsb2NhbGhvc3QwgZ8w
DQYJKoZIhvcNAQEBBQADgY0AMIGJAoGBAMNgNijoFmD5TX7NMd2pGEeEmwRifWRc
45jVS1a5kUncfRfgr4ehygPQDS2XrUkd+OYneFIXOcANW9WAWAlfeLs8DiSgs+9m
tuVjZ58RAsRXkW7H3vqQv5sAxmmwwVGN9WfKW+II/xLhpMtVGQH+MOucGbssODzk
0vwXEGSeEqYdAgMBAAEwDQYJKoZIhvcNAQEFBQADgYEAgCW2RBZgDMikQenSF3sz
u7KDe8+t8qnEFvrwCEpLUzvedSZxkaKzHrrCnIYlDnXRZBveKngWoejGzqtyIXup
YKzBZaZWH8cV72RdDwgM1owWi3KZBKpxfphYkWSRRx59djHZY/Yjudnb3oT/3c8/
NHsFbLbrZaGriLshIwrjEGs=
-----END CERTIFICATE-----
-----BEGIN RSA PRIVATE KEY-----
MIICXAIBAAKBgQDDYDYo6BZg+U1+zTHdqRhHhJsEYn1kXOOY1UtWuZFJ3H0X4K+H
ocoD0A0tl61JHfjmJ3hSFznADVvVgFgJX3i7PA4koLPvZrblY2efEQLEV5Fux976
kL+bAMZpsMFRjfVnylviCP8S4aTLVRkB/jDrnBm7LDg85NL8FxBknhKmHQIDAQAB
AoGAZDw9LRl9Ob1No+t0VOuG+FIxEbvR5ya84dE0OMc1ofZL+28bvvMjaHdZ+3Ug
wy1sX/AKC9u8liqEXfHduNlRX59WfhS1DBIqpezpg3Hj35sCmuGvtiJVMHbZBX0I
S0P14vXxaGJ/Sw04CgbGJs08P5ITTleZ9HioHhCkUObP5kUCQQD3auQTo/oqbNXz
FbL1ckP65wUz7ean+YcXDYgKM2jnyEfATMWjjQkMEzO4MJdfuLi+5UbEfup1c1zB
SmIijzN7AkEAyicud3X+HoV2dwRPzsquvR27fjEsIttzjNJ0Kcm+YAtIQcJQti9e
E9OMjSsxa8LQ1V8HMWmDYyoAEhdYG1BtRwJAczlTmJYANmvTQ87yNf6ODDY0pReB
GO9La4AAwAdrLq6GQ9c9H8rZ0MbMilYO2SRU3Yo3Z+FXXXVpWBdFFqUsKwJAKNYn
bdx5HENLvhkx4g1RpUR3VrOqPdRlEEKHUtW9TnuY+ie91D/XWlv23aGnFyTAuQm8
U0AEWajnYMA0fTgPCwJBAI1J6nOjlE5jcKKzBAE33iL8lXj5FlGX3hhPM4jm3BCN
bpmhcfRVwyhqWwYChEQ5Y25Lv0i7Lxpud/UbLE0x/x8=
-----END RSA PRIVATE KEY-----
Jump to Line
Something went wrong with that request. Please try again.