Created
December 17, 2009 04:42
Revisions
-
h0rs3r4dish revised this gist
Dec 19, 2009 . 1 changed file with 2 additions and 2 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 charactersOriginal file line number Diff line number Diff line change @@ -59,12 +59,12 @@ def initialize(req, res, block) args = @req.query.keys.sort.map { |k| @req.query[k] } # POSTed vars instance_exec args, &block res["Content-type"] = Content_Types[@format] @req.cookies.each { |c| @res.cookies << c } unless @ditch_cookies end end Content_Types = { :html => 'text/html', :json => 'text/json', :xml => 'text/xml', :js => 'text/javascript' } -
h0rs3r4dish revised this gist
Dec 17, 2009 . 2 changed files with 2 additions and 2 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 charactersOriginal file line number Diff line number Diff line change @@ -56,7 +56,7 @@ def do_not_save_cookies def initialize(req, res, block) @req = req; @res = res; @ditch_cookies = false @format = (@req.path =~ /\.([^\.]+)$/) ? $1.to_s : :html args = @req.query.keys.sort.map { |k| @req.query[k] } # POSTed vars instance_exec args, &block res["Content-type"] = Content_Types[@format] @res.cookies += @req.cookies unless @ditch_cookies 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 charactersOriginal file line number Diff line number Diff line change @@ -21,7 +21,7 @@ Meticulous documentation Okay, maybe *meticulous* isn't the right word. But I digress. * `Server#path(url, &block)` This is the big one. Give it a URL and a block and let her rip. If you've got stuff POSTed to your page, then have at'em, in alphabetical order. That's the real one you need to worry about. But if you're interested, there are a bunch of helpers out there. -
h0rs3r4dish revised this gist
Dec 17, 2009 . 1 changed file with 1 addition and 1 deletion.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 charactersOriginal file line number Diff line number Diff line change @@ -56,9 +56,9 @@ def do_not_save_cookies def initialize(req, res, block) @req = req; @res = res; @ditch_cookies = false @format = (@req.path =~ /\.([^\.]+)$/) ? $1.to_s : :html args = @req.query.keys.map { |k| @req.query[k] } # POSTed vars instance_exec args, &block res["Content-type"] = Content_Types[@format] @res.cookies += @req.cookies unless @ditch_cookies end end -
h0rs3r4dish revised this gist
Dec 17, 2009 . 1 changed file with 1 addition and 1 deletion.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 charactersOriginal file line number Diff line number Diff line change @@ -59,7 +59,7 @@ def initialize(req, res, block) res["Content-type"] = Content_Types[@format] args = @req.query.keys.map { |k| @req.query[k] } # POSTed vars instance_exec args, &block @res.cookies += @req.cookies unless @ditch_cookies end end -
h0rs3r4dish revised this gist
Dec 17, 2009 . 2 changed files with 3 additions and 2 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 charactersOriginal file line number Diff line number Diff line change @@ -3,8 +3,8 @@ server = HB::Server.new # use "include HB" for even more fun server.path "/" do # Hello world, HB style write true, "h1. Hello, world!" # write() = puts() for the web; hx. = <hx></hx> write true, "The URL you entered was '%s'" % url # Just pop that word in there and enjoy end server.path "/submit" do |name| # auto-magically gives you POST/GET-ed variables 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 charactersOriginal file line number Diff line number Diff line change @@ -27,6 +27,7 @@ That's the real one you need to worry about. But if you're interested, there are ### If you're defining a page... * `write(parse,text)` drops the text into the browser's lap. Set `parse` to true if you want auto-paragraph/header wrappings, or just ignore it and have your words repeated verbatim. * `redirect_to(url)` sends the browser to the new location. * `url` hands you the path (ex, "/index.html") * `posted?(varname)` is kind of important, since it'll tell you whether or not a certain variable was POSTed -
h0rs3r4dish revised this gist
Dec 17, 2009 . 1 changed file with 2 additions and 2 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 charactersOriginal file line number Diff line number Diff line change @@ -8,13 +8,13 @@ end server.path "/submit" do |name| # auto-magically gives you POST/GET-ed variables if not posted? :name then # this could be "view 'index'", if you had views/index.html write <<EOF <form method="POST" action="/submit"> Enter your name: <input type='text' name='name' /> <input type='submit' /> </form> EOF else write "Hi there %s!" % name # Don't you love cheerful webpages? end -
h0rs3r4dish revised this gist
Dec 17, 2009 . 2 changed files with 8 additions and 3 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 charactersOriginal file line number Diff line number Diff line change @@ -14,13 +14,12 @@ Enter your name: <input type='text' name='name' /> <input type='submit' /> </form> EOF # this could've been "view 'index'", if you had views/index.html else write "Hi there %s!" % name # Don't you love cheerful webpages? end end server.path "/cookie" do # Let's have some fun with these if (c = read_cookie "grah") then write "Cookie found." @@ -31,4 +30,9 @@ end end server.path "/ajax" do # Who doesn't like AJAX sometimes? @format = :json # XMLHttpRequest.new.open("GET") this and see for yourself write "{ 'source': 'HumaneBrick' }" end server.start! # The exclamation point is because you just want it to work 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 charactersOriginal file line number Diff line number Diff line change @@ -21,7 +21,7 @@ Meticulous documentation Okay, maybe *meticulous* isn't the right word. But I digress. * `Server#path(url, &block)` This is the big one. Give it a URL and a block and let her rip. That's the real one you need to worry about. But if you're interested, there are a bunch of helpers out there. @@ -30,6 +30,7 @@ That's the real one you need to worry about. But if you're interested, there are * `redirect_to(url)` sends the browser to the new location. * `url` hands you the path (ex, "/index.html") * `posted?(varname)` is kind of important, since it'll tell you whether or not a certain variable was POSTed * There are times you don't want to talk HTML (say, a JSON response). For that, to ahead and set `@format` to the desired value (see `HB.Content_Types`) Fine, let's talk cookies while we're here too: -
h0rs3r4dish revised this gist
Dec 17, 2009 . 3 changed files with 49 additions and 34 deletions.There are no files selected for viewing
File renamed without changes.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 charactersOriginal file line number Diff line number Diff line change @@ -2,41 +2,32 @@ require 'webrick' module HB VERSION = "1.1" class Server def initialize(config={:Port => 8000}) @server = WEBrick::HTTPServer.new(config) ['INT','TERM'].each { |s| trap(s) { @server.shutdown; Process.exit } } end def path(str,&block) str = '/'+str if str[0].chr != '/' @server.mount_proc(str, HB.wrap_proc(&block)) end def start! @server.start end end class Page def write(p=false,*str) if p.class == String then str = [p] + str; p = false; end str.each { |line| if p then found = line =~ /^h(\d)\. (.+)/ line = "<h#{$1}>#{$2}</h#{$1}>" if found line = "<p>#{line}</p>" if not found end @res.body << line+"\n" } end @@ -49,32 +40,40 @@ def posted?(name) def url @req.path end def view(name) IO.readlines("views/%s.%s" % [name, @format]).join end def add_cookie(name,value) @res.cookies.push WEBrick::Cookie.new(name,value) end def read_cookie(name) @req.cookies.each { |c| return c.value if c.name == name } return false end def do_not_save_cookies @ditch_cookies = true end def initialize(req, res, block) @req = req; @res = res; @ditch_cookies = false @format = (@req.path =~ /\.([^\.]+)$/) ? $1.to_s : :html res["Content-type"] = Content_Types[@format] args = @req.query.keys.map { |k| @req.query[k] } # POSTed vars instance_exec args, &block @res.cookies += @req.cookies end end Content_Types = { :html => 'text/html', :json => 'application/json', :xml => 'text/xml', :js => 'text/javascript' } def HB.content_type(values) Content_Types.merge! values end def HB.wrap_proc(&block) Proc.new { |req, res| Page.new(req,res,block) } 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 charactersOriginal file line number Diff line number Diff line change @@ -14,7 +14,7 @@ That's when you need to assert yourself as a human, and write in English: end end Wouldn't that just be great? **HumaneBrick does that**. You can practically write in English, if you do things right. See the `humane-examples.rb` file at the end if you really want a treat. As for the code, it manages to hold itself at **80 lines of code**, without any extra compression. Enjoy. Meticulous documentation ------------------------ @@ -23,8 +23,24 @@ Okay, maybe *meticulous* isn't the right word. But I digress. * `Server#path(url, type=:html, &block)` This is the big one. Give it a URL and a block and let her rip. Adding in a content type (`:xml`, `:javascript`, or another one you make up) is just icing on the cake. That's the real one you need to worry about. But if you're interested, there are a bunch of helpers out there. ### If you're defining a page... * `redirect_to(url)` sends the browser to the new location. * `url` hands you the path (ex, "/index.html") * `posted?(varname)` is kind of important, since it'll tell you whether or not a certain variable was POSTed Fine, let's talk cookies while we're here too: * `add_cookie(name,value)` makes a new one * `read_cookie(name)` will give them back * If you feel like holding onto some cookies instead of passing them back to the browser, make sure to call `do_not_save_cookies` for good measure. Otherwise, they'll be saved & sent back. There's another handy utility if you don't feel like writing it all in the current file: `view(name)` loads `HUMANEDIR/views/name.@format`, for either executing or `write`ing ### But what about the other stuff? If you really want to, you can add new content types with `HB.content_type(hash)` (for example, `content_type :js => "text/javascript"`) Nothing to see, really. Go back to writing fun code again. -
h0rs3r4dish revised this gist
Dec 17, 2009 . 3 changed files with 40 additions and 26 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 charactersOriginal file line number Diff line number Diff line change @@ -20,4 +20,15 @@ end end server.path "/cookie" do # Let's have some fun with these if (c = read_cookie "grah") then write "Cookie found." write "Value: '%s'" % c # c = WEBrick::Cookie#value else write "Creating cookie." # Refresh the page to see the result if you get this add_cookie "grah", "Hello, cookies!" # Nice and easy end end server.start! # The exclamation point is because you just want it to work 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 charactersOriginal file line number Diff line number Diff line change @@ -2,22 +2,19 @@ require 'webrick' # Let's do some guerilla patching class Object; def instance_exec(*args, &block) mname = "__instance_exec_#{Thread.current.object_id.abs}" class << self; self end.class_eval{ define_method(mname, &block) } begin ret = send(mname, *args) ensure class << self; self end.class_eval{ undef_method(mname) } rescue nil end ret end; end module HB VERSION = "1.0" class Server @@ -29,35 +26,39 @@ def path(str,format=:html,&block) str = '/'+str if str[0].chr != '/' @server.mount_proc(str, HB.wrap_proc(format,&block)) end def start! @server.start end end class Page def write(*str) str.map { |l| l.split("\n\n") }.flatten.each { |line| found = line =~ /^h(\d)\. (.+)$/ line = "<h#{$1}>#{$2}</h#{$1}>" if found line = "<p>"+line+"</p>" if not found @res.body << line+"\n" } end def redirect_to(path) @res.set_redirect(WEBrick::HTTPStatus::Found,path) end def posted?(name) @req.query.key? name.to_s end def url @req.path end def add_cookie(name,value) @res.cookies.push WEBrick::Cookie.new(name,value) end def read_cookie(name) @req.cookies.each { |c| return c.value if c.name == name } return false end def save_cookies @res.cookies = @req.cookies end def initialize(req, res, type, block) @req = req; @res = res; res["Content-type"] = Content_Types[type] 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 charactersOriginal file line number Diff line number Diff line change @@ -3,7 +3,7 @@ HumaneBrick = some WEBrick + some sanity It's hard to argue that WEBrick is good at documentation. So, there are other servers. But how many come built-in? None. As a result, lot of my projects use WB. It's a pain in the ass. It's a very verbose and painful way to write code, especially when you're trying to test an idea that doesn't quite agree with WB's way of doing things. That's when you need to assert yourself as a human, and write in English: server.path "/" do |name| @@ -14,7 +14,7 @@ That's when you need to assert yourself as a human, and write in English: end end Wouldn't that just be great? **HumaneBrick does that**. You can practically write in English, if you do things right. See the `examples.rb` file at the end if you really want a treat. As for the code, it manages to hold itself at **80 lines of code**, without any extra compression. Enjoy. Meticulous documentation ------------------------ @@ -25,4 +25,6 @@ Okay, maybe *meticulous* isn't the right word. But I digress. Wait, that's about it. There's no real need to mention `redirect_to(url)`, a method (in a page definition) that sends the reader somewhere else. `url` doesn't need any more publicity, since it's a fairly straightforward concept (type it out and get back the WEBrick::HTTPRequest#path). There's also `posted?(varname)`, which just checks to see if `varname` was in the GET/POST-ed variables. Well, there are cookie things too: `add_cookie(name,value)` makes a new one for you, which can be read with `read_cookie(name)` (the value is returned). WEBrick's a mean piece of software, so you'll have to `save_cookies` every time you want to keep the already-made cookies in the browser. Nothing to see, really. Go back to writing fun code again. -
h0rs3r4dish revised this gist
Dec 17, 2009 . 1 changed file with 1 addition and 1 deletion.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 charactersOriginal file line number Diff line number Diff line change @@ -21,7 +21,7 @@ Meticulous documentation Okay, maybe *meticulous* isn't the right word. But I digress. * `Server#path(url, type=:html, &block)` This is the big one. Give it a URL and a block and let her rip. Adding in a content type (`:xml`, `:javascript`, or another one you make up) is just icing on the cake. Wait, that's about it. There's no real need to mention `redirect_to(url)`, a method (in a page definition) that sends the reader somewhere else. `url` doesn't need any more publicity, since it's a fairly straightforward concept (type it out and get back the WEBrick::HTTPRequest#path). There's also `posted?(varname)`, which just checks to see if `varname` was in the GET/POST-ed variables. -
h0rs3r4dish revised this gist
Dec 17, 2009 . 2 changed files with 13 additions and 3 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 charactersOriginal file line number Diff line number Diff line change @@ -2,8 +2,7 @@ server = HB::Server.new # use "include HB" for even more fun server.path "/" do # Hello world, HB style write "h1. Hello, world!" # write() = puts() for the web; hx. = <hx></hx> write "The URL you entered was '%s'" % url # Just pop that word in there and enjoy 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 charactersOriginal file line number Diff line number Diff line change @@ -14,4 +14,15 @@ That's when you need to assert yourself as a human, and write in English: end end Wouldn't that just be great? **HumaneBrick does that**. You can practically write in English, if you do things right. See the `examples.rb` file at the end if you really want a treat. As for the code, it manages to hold itself **under 80 lines**, without any extra compression. Enjoy. Meticulous documentation ------------------------ Okay, maybe *meticulous* isn't the right word. But I digress. * `Server#path(url, type=:html, &block)` This is the big one. Give it a URL and a block and let her rip. Adding in a content type (:xml, :javascript, or another one you make up) is just icing on the cake. Wait, that's about it. There's no real need to mention `redirect_to(url)`, a method (in a page definition) that sends the reader somewhere else. `url` doesn't need any more publicity, since it's a fairly straightforward concept (type it out and get back the WEBrick::HTTPRequest#path). There's also `posted?(varname)`, which just checks to see if `varname` was in the GET/POST-ed variables. Nothing to see, really. Go back to writing fun code again. -
h0rs3r4dish created this gist
Dec 17, 2009 .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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,24 @@ require 'humane' server = HB::Server.new # use "include HB" for even more fun server.path "/" do # Hello world, HB style write "h1. Hello, world!" # write() = puts() for the web; hx. = <hx></hx> write "The URL you entered was '%s'" % url # Just pop that word in there and enjoy end server.path "/submit" do |name| # auto-magically gives you POST/GET-ed variables if not posted? :name then write <<EOF <form method="POST" action="/submit"> Enter your name: <input type='text' name='name' /> <input type='submit' /> </form> EOF else write "Hi there %s!" % name # Don't you love cheerful webpages? end end server.start! # The exclamation point is because you just want it to work 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,79 @@ #!/usr/bin/ruby require 'webrick' # Let's do some guerilla patching for 1.8 class Object def instance_exec(*args, &block) mname = "__instance_exec_#{Thread.current.object_id.abs}" class << self; self end.class_eval{ define_method(mname, &block) } begin ret = send(mname, *args) ensure class << self; self end.class_eval{ undef_method(mname) } rescue nil end ret end end module HB VERSION = "1.0" class Server def initialize(config={:Port => 8000}) @server = WEBrick::HTTPServer.new(config) ['INT','TERM'].each { |s| trap(s) { @server.shutdown; Process.exit } } end def path(str,format=:html,&block) str = '/'+str if str[0].chr != '/' @server.mount_proc(str, HB.wrap_proc(format,&block)) end def []=(str,&block) path(str,block) end def start! @server.start end end class Page def write(*str) str = str.map { |l| l.split("\n") }.flatten str.each { |line| if line =~ /^h(\d)\. (.+)$/ then line = "<h#{$1}>#{$2}</h#{$1}>" else line = "<p>"+line+"</p>" end @res.body << line+"\n" } end def redirect_to(path) @res.set_redirect(WEBrick::HTTPStatus::Found,path) end def posted?(name) return @req.query.key? name.to_s end def url @req.path end def initialize(req, res, type, block) @req = req; @res = res; res["Content-type"] = Content_Types[type] args = req.query.keys.map { |k| req.query[k] } # POSTed vars instance_exec args, &block end end Content_Types = { :html => 'text/html', :javascript => 'text/javascript', :xml => 'text/xml' } def HB.wrap_proc(type=:html,&block) Proc.new { |req, res| Page.new(req,res,type,block) } 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,17 @@ HumaneBrick = some WEBrick + some sanity ======================================== It's hard to argue that WEBrick is good at documentation. So, there are other servers. But how many come built-in? None. So, a lot of my projects use WB. It's a pain in the ass. It's a very verbose and painful way to write code, especially when you're trying to test an idea that doesn't quite agree with WB's way of doing things. That's when you need to assert yourself as a human, and write in English: server.path "/" do |name| if not posted? :name then write "You need to POST name!" else write "Hi there, %s" % name end end Wouldn't that just be great? **HumaneBrick does that**. You can practically write in English, if you do things right. See the `examples.rb` file at the end if you really want a treat. As for the code, it manages to hold itself **under 80 lines**, without any extra compression. Enjoy.