{"id":32,"date":"2008-07-23T16:38:58","date_gmt":"2008-07-23T21:38:58","guid":{"rendered":"http:\/\/factormystic.net\/blog\/?p=32"},"modified":"2009-03-04T00:26:38","modified_gmt":"2009-03-04T05:26:38","slug":"how-to-run-ruby-scripts-in-a-browser-without-mod_ruby","status":"publish","type":"post","link":"https:\/\/factormystic.net\/blog\/how-to-run-ruby-scripts-in-a-browser-without-mod_ruby","title":{"rendered":"How to run Ruby scripts in a browser without mod_ruby"},"content":{"rendered":"<p>I&#8217;m sure this is a horror in dozens of ways, but eruby sucks and my host doesn&#8217;t have mod_ruby. So I got creative.<\/p>\n<p>The .htaccess rewrites requests for any file ending in .rb to this handler.php, which confirms that the requested ruby script exists, then executes it and prints the result.<\/p>\n<p>[sourcecode language=&#8221;php&#8221;]<br \/>\n\/\/PHP script to execute ruby scripts when the host doesn&#8217;t have a cgi handler for .rb<br \/>\n\/\/Use with this .htaccess:<\/p>\n<p>\/*<br \/>\nOptions +FollowSymlinks<br \/>\nRewriteEngine on<br \/>\nRewriteRule ^(.*)\\.rb$ handler.php?rb=$1.rb [NC,QSA]<br \/>\n*\/<\/p>\n<p>$file = $_GET[&#8216;rb&#8217;];<\/p>\n<p>if(in_array($file, scandir(&#8216;.&#8217;)))<br \/>\n{<br \/>\nforeach($_REQUEST as $key=>$value) if($key != &#8216;rb&#8217;) $args .= &#8221; $key=&#8221;.urlencode($value);<br \/>\necho exec(escapeshellcmd(&#8216;.\/&#8217;.$file.$args));<br \/>\n}<br \/>\nelse<br \/>\n{<br \/>\necho &#8216;404- Page not found&#8217;;<br \/>\n}<br \/>\n?><br \/>\n[\/sourcecode]<br \/>\nHere&#8217;s an example ruby script using this hack- nothing special to do here at all:<\/p>\n<p>[sourcecode language=&#8221;ruby&#8221;]<br \/>\n#!\/usr\/local\/bin\/ruby<\/p>\n<p>require &#8216;cgi&#8217;<\/p>\n<p>print &#8220;Hello from Ruby!<br \/>&#8221;<br \/>\nprint &#8220;All of the input variables:<\/p>\n<hr>\n<p>&#8221;<\/p>\n<p>ARGV.each do |arg|<br \/>\nprint CGI::unescape(arg.to_s)<br \/>\nprint &#8220;<br \/>&#8221;<br \/>\nend<br \/>\n[\/sourcecode]<\/p>\n<p>All the GET, POST, and COOKIE variables are at your fingertips in ARGV! (You could get them with Ruby&#8217;s CGI module as well.)<\/p>\n<p>So here&#8217;s what it looks like to users:<\/p>\n<div id=\"attachment_33\" style=\"width: 510px\" class=\"wp-caption alignnone\"><a href=\"https:\/\/factormystic.net\/blog\/wp-content\/uploads\/2008\/07\/ss.jpg\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-33\" class=\"size-full wp-image-33\" title=\"Screenshot of ruby without mod_ruby hack\" src=\"https:\/\/factormystic.net\/blog\/wp-content\/uploads\/2008\/07\/ss.jpg\" alt=\"Screenshot of ruby without mod_ruby hack\" width=\"500\" height=\"194\" srcset=\"https:\/\/factormystic.net\/blog\/wp-content\/uploads\/2008\/07\/ss.jpg 798w, https:\/\/factormystic.net\/blog\/wp-content\/uploads\/2008\/07\/ss-300x116.jpg 300w\" sizes=\"auto, (max-width: 500px) 100vw, 500px\" \/><\/a><p id=\"caption-attachment-33\" class=\"wp-caption-text\">Screenshot of ruby without mod_ruby hack<\/p><\/div>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;m sure this is a horror in dozens of ways, but eruby sucks and my host doesn&#8217;t have mod_ruby. So I got creative. The .htaccess rewrites requests for any file ending in .rb to this handler.php, which confirms that the requested ruby script exists, then executes it and prints the result. [sourcecode language=&#8221;php&#8221;] \/\/PHP script [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[15,14,13,12],"tags":[],"class_list":["post-32","post","type-post","status-publish","format-standard","hentry","category-htaccess","category-php","category-ruby","category-web-stuff"],"_links":{"self":[{"href":"https:\/\/factormystic.net\/blog\/wp-json\/wp\/v2\/posts\/32","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/factormystic.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/factormystic.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/factormystic.net\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/factormystic.net\/blog\/wp-json\/wp\/v2\/comments?post=32"}],"version-history":[{"count":5,"href":"https:\/\/factormystic.net\/blog\/wp-json\/wp\/v2\/posts\/32\/revisions"}],"predecessor-version":[{"id":37,"href":"https:\/\/factormystic.net\/blog\/wp-json\/wp\/v2\/posts\/32\/revisions\/37"}],"wp:attachment":[{"href":"https:\/\/factormystic.net\/blog\/wp-json\/wp\/v2\/media?parent=32"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/factormystic.net\/blog\/wp-json\/wp\/v2\/categories?post=32"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/factormystic.net\/blog\/wp-json\/wp\/v2\/tags?post=32"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}