Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

« Newer Snippets
Older Snippets »
Showing 1-10 of 80 total  RSS 

One-line web server in Ruby


# From: http://www.ntecs.de/blog/articles/2008/02/09/the-worlds-smallest-webserver
# Author: Michael Neumann
# ... point your browser to http://localhost:3125/etc/motd

ruby -rsocket -e 's=TCPServer.new(5**5);loop{_=s.accept;_<<"HTTP/1.0 200 OK\r\n\r\n#{File.read(_.gets.split[1])rescue nil}";_.close}'

Run TCPServer as a simple Web server

A TCPServer accepts incoming TCP connections. Here is a Web server that listens on a given port and returns the time.

require 'socket'
port = (ARGV[0] || 80).to_i
server = TCPServer.new('localhost', port)
while (session = server.accept)
  puts "Request: #{session.gets}"
  session.print "HTTP/1.1 200/OK\r\nContent-type: text/html\r\n\r\n"
  session.print "<html><body><h1>#{Time.now}</h1></body></html>\r\n"
  session.close
end

This code was copied from Programming Ruby: The Pragmatic Programmer's Guide [rubycentral.com] while looking for information on Ruby CGI global variables.

Upload a file using Ruby

The following code was used to upload an image file to the web server. Source code origin: Ruby Language Stuff | mod_ruby File upload scripts [zytrax.com]

file: file_upload.cgi
#!/usr/bin/ruby

# ruby script fragment
require 'cgi'
require 'stringio'

cgi = CGI.new()  # New CGI object
puts "Content-Type: text/plain"
puts
print '<result>'

# get uri of tx'd file (in tmp normally)
tmpfile = cgi.params['myfile'].first.path

# OR (functionally the same)
tmpfile = cgi.params['myfile'][0].path

# create a Tempfile reference
fromfile = cgi.params['myfile'].first

#displays the original file name as supplied in the form
puts fromfile.original_filename

# displays the content (mime) type e.g. text/html
puts fromfile.content_type

# create output file reference as original filename in our chosen directory
tofile = '/var/www/yourdomain.com/htdocs/r/'+fromfile.original_filename

# copy the file
# note the untaint prevents a security error
# cgi sets up an StringIO object if file < 10240
# or a Tempfile object following works for both
File.open(tofile.untaint, 'w') { |file| file << fromfile.read}
# when the page finishes the Tempfile/StringIO!) thing is deleted automatically

print '</result>'

file: file_upload.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  <head>
    <title>File upload</title>
    <meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
  </head>
  <body>
    <form name='fileupload' enctype="multipart/form-data" 
    action='/p/file_upload.cgi' method='post'>
    <input type='file' name='myfile' size="40" />
    <input type='submit' value"Send it"/>
    </form>
  </body>
</html>
  

Getting Started With WWW::Mechanize

This Ruby code uses WWW:mechanize to act like a web browser.

 require 'rubygems'
 require 'mechanize'

 agent = WWW::Mechanize.new
 page = agent.get('http://google.com/')


Refer to the documentation at http://mechanize.rubyforge.org/mechanize/. Then gem install mechanize, and try running the code in an irb session.

output (extract):
=> #<WWW::Mechanize::Page
 {url #<URI::HTTP:0xfdbbbb286 URL:http://www.google.com/>}
 {meta}
 {title "Google"}
 {iframes}
 {frames}
 {links
  #<WWW::Mechanize::Page::Link
   "Images"
   "http://images.google.com/imghp?hl=en&tab=wi">
  #<WWW::Mechanize::Page::Link
   "Maps"
   "http://maps.google.com/maps?hl=en&tab=wl">
  #<WWW::Mechanize::Page::Link
   "News"
   "http://news.google.com/nwshp?hl=en&tab=wn">
  #<WWW::Mechanize::Page::Link
   "Shopping"
   "http://www.google.com/prdhp?hl=en&tab=wf">
  #<WWW::Mechanize::Page::Link
   "Gmail"
   "http://mail.google.com/mail/?hl=en&tab=wm">

bScan - Simple Web Aplications Scanner

// Web application scanner (ex: phpBB, myCMS, myBlog, mySite etc..) - Only in PHP !
// Find XSS, sql injection, remote file inclusion

#####################################################################################
#	Black_H  / Nooz -- 30:01:07 
#	Bl4ck.H<>gmail<>com
#

class BScan

#####################################################################################
#	Regex
#

@@space    = '([[:space:]]*)'

@@userdat  = '('
@@userdat += '(\$_SERVER\[([\'\"]*)HTTP_)|'
@@userdat += '(\$_GET)|'
@@userdat += '(\$_POST)|'
@@userdat += '(\$_COOKIE)|'
@@userdat += '(\$_REQUEST)|'
@@userdat += '(\$_FILES)|'
@@userdat += '(\$_ENV)|'
@@userdat += '(\$_HTTP_COOKIE_VARS)|'
@@userdat += '(\$_HTTP_ENV_VARS)|'
@@userdat += '(\$_HTTP_GET_VARS)|'
@@userdat += '(\$_HTTP_POST_FILES)|'
@@userdat += '(\$_HTTP_POST_VARS)|'
@@userdat += '(\$_HTTP_SERVER_VARS\[([\'\"]*)HTTP_)'
@@userdat += ')'

@@regex = Hash.new
@@regex = 
	{'TYPE' => 'vars overwrite','LEVEL' => '2','REGEX' => /extract#{@@space}\((.*)#{@@userdat}(.*)\)/i},
	{'TYPE' => 'vars overwrite','LEVEL' => '2','REGEX' => /import_request_variables#{@@space}\((.*)\)/i},
	{'TYPE' => 'fopen vuln','LEVEL' => '3','REGEX' => /fopen#{@@space}\((.*)#{@@userdat}(.*)\)/i},
	{'TYPE' => 'copy vuln','LEVEL' => '3','REGEX' => /copy#{@@space}\((.*)#{@@userdat}(.*)\)/i},
	{'TYPE' => 'fwrite vuln','LEVEL' => '3','REGEX' => /fwrite#{@@space}\((.*)#{@@userdat}(.*)\)/i},
	{'TYPE' => 'sql injection','LEVEL' => '2','REGEX' => /(mysql_query|mssql_query|mysqli_query)#{@@space}\((.*)#{@@userdat}(.*)\)/i},
	{'TYPE' => 'crlf injection','LEVEL' => '1','REGEX' => /mail#{@@space}\((.*)#{@@userdat}(.*)\)/i},
	{'TYPE' => 'cross site scripting','LEVEL' => '1','REGEX' => /\<\?\=#{@@space}(.*)#{@@userdat}/i},
	{'TYPE' => 'cross site scripting','LEVEL' => '1','REGEX' => /(print|echo|print_r|var_dump)#{@@space}(|\(|\")(.*)#{@@userdat}/i},
	{'TYPE' => 'php code execution','LEVEL' => '3','REGEX' => /eval#{@@space}\((.*)#{@@userdat}(.*)\)/i},
	{'TYPE' => 'php code execution','LEVEL' => '3','REGEX' => /file_put_contents#{@@space}\((.*)#{@@userdat}(.*)\)/i},
	{'TYPE' => 'variable attribution', 'LEVEL' => '2','REGEX' => /(.*)\$#{@@userdat}(.*)/i},
	{'TYPE' => 'chmod affectation','LEVEL' => '1','REGEX' => /chmod#{@@space}\((.*)#{@@userdat}(.*)\)/i},
	{'TYPE' => 'file disclosure','LEVEL' => '2','REGEX' => /(readfile|file_get_contents|file)#{@@space}\((.*)#{@@userdat}(.*)\)/i},
	{'TYPE' => 'file disclosure','LEVEL' => '2','REGEX' => /(show_source|highlight_file)#{@@space}\((.*)#{@@userdat}(.*)\)/i},
	{'TYPE' => 'bzopen vuln','LEVEL' => '2','REGEX' => /bzopen#{@@space}\((.*)#{@@userdat}(.*)\)/i},
	{'TYPE' => 'file deletion','LEVEL' => '2','REGEX' => /(rmdir|unlink|delete)#{@@space}\((.*)#{@@userdat}(.*)\)/i},
	{'TYPE' => 'command execution','LEVEL' => '3','REGEX' => /(exec|system|passthru|shell_exec|proc_open|pcntl_exec)#{@@space}\((.*)#{@@userdat}(.*)\)/i},
	{'TYPE' => 'buffer overflow','LEVEL' => '3','REGEX' => /(confirm_phpdoc_compiled|mssql_pconnect|mssql_connect|crack_opendict|snmpget|ibase_connect)#{@@space}\((.*)#{@@userdat}(.*)\)/i},
	{'TYPE' => 'ip falsification','LEVEL' => '1','REGEX' => /(.*)(HTTP_CLIENT_IP|HTTP_X_FORWARDED_FOR|HTTP_PC_REMOTE_ADDR)(.*)/i},
	{'TYPE' => 'putenv vuln','LEVEL' => '2','REGEX' => /putenv#{@@space}\((.*)#{@@userdat}(.*)\)/i},
	{'TYPE' => 'full path disclosure','LEVEL' => '1','REGEX' => /(htmlentities|htmlspecialchars)#{@@space}\((.*)#{@@userdat}(.*)\)/i},
	{'TYPE' => 'magic_quotes_gpc bypass','LEVEL' => '1','REGEX' => /(stripslashes|urldecode)#{@@space}\((.*)#{@@userdat}(.*)\)/i},
	{'TYPE' => 'file inclusion','LEVEL' => '3','REGEX' => /(include|include_once|require|require_once)#{@@space}(|\(|\")(.*)#{@@userdat}/i}

#####################################################################################
#	Main
#

  def initialize()

	################
	#	Usage

if (ARGV.length < 4)
puts  '
 ---------------------------------------------------------------------
|             Credits: Black_H <bl4ck.h@gmail.com>                    |
|                 URL: Lemon-Inside.sup.fr                            |
|                Note: Premier code Ruby                              |
 ---------------------------------------------------------------------

 ---------------------------------------------------------------------
|   Usage:  scan.rb -d <Dossier> -i <Save.html>                       |
|   Ex:  scan.rb -d ./ -i output.html                                 |
 ---------------------------------------------------------------------		
 '
 end
 
	################
	#	Options & Vars
	
	@@scan_alldir =  self.options('d')
	@@out_file =  self.options('i')
	
	@@ban = [".", "..", "scan.rb", @@out_file.to_s]

	@@scan_buffer = Array.new
	
	################
	#	Options Error ?
	
	if (@@scan_alldir != false and @@scan_alldir.empty? == false)
	self.dscan(@@scan_alldir)
	self.output(@@scan_buffer)
	@@scan_buffer = ''
	end


	

  end

#####################################################################################
#	Dir Scan 
#
  
  def dscan(dir)
      
	d = Dir.open(dir.to_s)
	d = d.sort - @@ban
	
      d.each { |fichier|

      case File.ftype(dir+fichier)
        when "directory"
          self.dscan(dir + fichier + "/")
        when "file"
		  puts  'Scan => ' + dir + fichier 
          self.fscan(dir + fichier)
      end

	  }
  end

#####################################################################################
#	File Scan 
#
  
  def fscan(file)

	fichier = File.readlines(file)
	i = 1

	fichier.each { |line|
						
		@@regex.each  { |info|
			
			test = (line  =~ info['REGEX']) 
		
				if (test) 
			
				@@scan_buffer += ['FILE' => file, 'LINE' => i.to_s, 'MATCH' => line, 'LEVEL' => info['LEVEL'], 'TYPE' => info['TYPE']]
				#	5 , 1 , 3 , 4 , 2
				next @@scan_buffer
				end
		}

	i += 1
  	} 
	
  end

#####################################################################################
#	Output buffer
#
  
  def output(buffer)
  
	@html_hmodel = '<html>'
	@html_hmodel += '<style type="text/css">'
	@html_hmodel += '<!--'
	@html_hmodel += '.level0 {background-color: #CCCCCC;}'
	@html_hmodel += '.level1 {background-color: #33FF66;}'
	@html_hmodel += '.level2 {background-color: #FFFF33;}'
	@html_hmodel += '.level3 {background-color: #FF0000;}'
	@html_hmodel += '--></style><body><h1>BScan v1.0</h1><pre>'

	code = @html_hmodel
	
	buffer.each { |infos|
	
	keys = infos.keys
	code += "<span class='level" + infos["LEVEL"] + "'>" + keys[1].to_s + ' : ' + infos["TYPE"] + '</span><br />'
	code += "<span class='" + infos["LEVEL"] + "'>" + keys[3].to_s + ' : ' + infos["LEVEL"] + '</span><br />'
	code += "<span class='" + infos["LEVEL"] + "'>" + keys[4].to_s + ' : ' + infos["FILE"] + '</span><br />'
	code += "<span class='" + infos["LEVEL"] + "'>" + keys[0].to_s + ' : ' + infos["LINE"] + '</span><br />'
	code += "<span class='" + infos["LEVEL"] + "'>" + keys[2].to_s + ' : ' + infos["MATCH"] + '</span><br />'
	

	}
		code += "</pre></body></html>"
		fhtml = File.open(@@out_file.to_s, "w")
		fhtml.write code
		code = ''

	
  end
#####################################################################################
#	Parse & Get Options
#
 
  def options(param)
  
	i = 0
		ARGV.each  { |valeur|
		
    		if (valeur == '-' + param.to_s)
				return ARGV[i+1]
			elseif (valeur != '-' + param.to_s)
				return false
			end
		i += 1
		}
		
	end
  
end

scan = BScan.new

Runt.rb

// A tiny Ruby web server.

#!/usr/bin/env ruby

require "webrick"

s=WEBrick::HTTPServer.new(
        :BindAddress => "localhost",
        :Port => 8080,
        :DocumentRoot => File.dirname($0)+"/"+"www/"
)

trap("INT") { s.shutdown }

s.start

How to detect a browser iPhone ?

The browser is :


Mozilla/5.0 (iPhone; U; CPU like Mac OS X; fr) AppleWebKit/420.1 (KHTML, like Gecko) Version/3.0 Mobile/3B48b Safari/419.3



Source: ab-d.fr
Internet with iPhone

I can't be bothered going downstairs to watch the daily show

scraper.rb - scrapes metadata from thedailyshow.com.
Just run it - creates videos/index.yml
#!/usr/bin/ruby

require 'rubygems'
require 'leecher'
require 'open-uri'
require 'hpricot'

class Scraper
	SEARCH_PAGE="http://www.thedailyshow.com/tds_files/includes/search/search_results.jhtml"
	def scrape_day(site, date, force=false)
		return if site.skip.include?(date) and not force
		url = sprintf("#{SEARCH_PAGE}?searchterm=%02d-%02d-%04d",date.month,date.day,date.year)
		puts "Fetching #{url}" if $DEBUG

		data = open(url) {|f| f.read }
		h = Hpricot(data)

		# can't use a real xpath - they all use an _id_ istead of a class
		results = (h/"div").find_all {|x| x['id'] == "videoListItem_1" }
		results.each {|result|
			url = result.at("a[1]")['href']
			vid = if url =~ /videoId=(\d+)/
				$1.to_i
			else
				raise "Failed to parse link #{url}"
			end

			title = ((result/"a")[1]/"text()").to_s
			date = Date.parse((result/"a[@onclick][1]/text()").to_s)

			descr = result/"div.video_description"
			description = (descr/"div[1]/text()").to_s
			tags = (descr/".tags/a/text()").map {|x| unescape(x.to_s) }
			
			vid = Video.new(site, vid, date, unescape(title), unescape(description), tags)
			puts vid
		}
	
		results.length
	end

	def unescape(t)
		t.gsub(/&([^;]{1,5});/) {|ent|
			case $1
				when /^#(\d+)$/
					[$1.to_i].pack('C')
				when /^#x([0-9a-zA-Z]+)$/
					[$1].pack('H2')
				when 'amp'
					'&'
				when 'gt'
					'>'
				when 'quot'
					'"'
				when 'apos'
					"'"
				when 'lt'
					'<'
				else
					$stderr.puts "Unknown entity #{$1.inspect}"
					'?'
			end
		}
	end
end

if __FILE__ == $0
	Site.load
	tds = Site.sites.find {|s| s.short_name == "tds" }
	tds ||= Site.new("The Daily Show","tds")

	day = Date.today
	start = Date.new(1999)

	counter=0

	s = Scraper.new
	while day >= start
		results = s.scrape_day(tds, day)
		puts "#{results} results for #{day}" if results # else skipped

		# Mark days as done once we've scraped them a month after air
		if (Date.today - day) > 30 and not results.nil?
			tds.skip!(day)
		end

		day -= 1
		unless results.nil? or results.zero?
			Site.save if (counter += 1)%10 == 0
		end
	end
	Site.save
end


leecher.rb - downloads/searches/plays videos
./leecher.rb [download/list/play] [searchterm ... ]
Search terms can be
date: 2007 or 2007-10 or 2007-10-01
tag: interview
id: 31723
already downloaded?: downloaded or !downloaded
#!/usr/bin/ruby

MEDIA_PLAYER = %w{mplayer -fs}

require 'rubygems'
require 'open-uri'
require 'rexml/document'
require 'rexml/xpath'
require 'fileutils'
require 'yaml'
require 'rio'
require 'set'

class Site
	class << self
		attr_reader :base
		attr_reader :alternates
		attr_reader :sites
		
		def init
			unless self.base or self.sites
				@base = "./videos"
				@alternates = []
				@sites = []
			end
		end

		def load(stream=nil)
			if stream
				stuff = YAML::load(stream)
				@base = stuff['base'] || "./videos"
				@sites = stuff['sites'] || []
				@alternates = stuff['alternates'] || []
			else
				init
				begin
					File.open(File.join(base,'index.yml')) {|f|
						load(f)
					}
				rescue Errno::ENOENT
					$stderr.puts "Warning, no database found, starting a new one"
				end
			end
		end
		
		def save(stream=nil)
			if stream
				YAML::dump({'base' => base, 'sites' => sites, 'alternates' => 'alternates'},stream)
			else
				File.open(File.join(base,'index.yml_'),'w') {|f|
					save(f)
				}
				FileUtils.mv(File.join(base,'index.yml_'),File.join(base,'index.yml'))
			end
		end

		def each(&block)
			sites.each(&block)
		end
	end

	def initialize(name, short_name=name)
		Site.init
		@videos = {}
		@name, @short_name = name, short_name
		@skip = Set.new
		Site.sites << self
	end

	attr_reader :videos
	attr_reader :name
	attr_reader :short_name
	attr_reader :skip

	def directory
		File.join(Site.base, short_name)
	end
	def directory_alternates
		Site.alternates.map {|d| File.join(d, short_name) }
	end

	def ensure_dir_exists!
		FileUtils.mkpath(directory)
	end

	def <<(vid)
		self.videos[vid.id] = vid
	end

	def skip!(date)
		self.skip << date
	end

	def [](id)
		self.videos[id]
	end

	def to_s
		name
	end

	def each 
		self.videos.each {|k,v| yield v }	
	end
end

class Video
	attr_reader :tags
	attr_reader :site
	attr_reader :id
	attr_reader :date
	attr_reader :title
	attr_reader :description

	def initialize(site, id, date=nil, title = nil, description=nil, tags=[]) 
 		@site = site
		@id = id
		@title = title
		@tags = tags
		@date = date
		@description = description

		site << self
	end

	def filename
		site.directory_alternates.map{|x| 
			File.join(x,"#{id}.flv")
		}.find {|f| 
			File.exists?(f) 
		} || File.join(site.directory, "#{id}.flv")
	end

	def downloaded?
		File.exists?(filename)
	end

	def download
		download! unless downloaded?
	end

	SHARED_DATA = "http://www.comedycentral.com/sitewide/video_player/shared/data"
	def download!
		site.ensure_dir_exists!
		url = download_url()
		begin
			rio(url) > rio(filename)
			File.size(filename)
		rescue Exception => x
			begin
				File.delete(filename)
			rescue Exception
			end
			raise x
		end
	end

	def to_s 
		sprintf("[%1s %7d - %s - %s - %20s]",(downloaded?? 'D' : ' '), id, date, site, title) 
	end

	def download_url
		manifest = open("#{SHARED_DATA}/flv_xml_gen.jhtml?ml_video=#{id}&hiLoPref=hi") {|f| f.read }
		doc = REXML::Document.new(manifest)
		REXML::XPath.first(doc, "/package/video/item/src/text()").to_s
	end
end

class Filter
	class << self
		def method_missing(sym,*args,&block)
			if sym.to_s =~ /^by/
				new.send(sym,*args,&block)
			else
				super
			end
		end
	end

	def initialize(parent=nil,&block)
		@parent = parent
		@test = block
	end
	
	def [](video)
		case video
			when Video
				video if (@test.nil? or @test[video]) and (@parent.nil? or @parent[video])
			when Site
				if block_given?
					video.each {|v| yield v if self[v] }
				else
					video.find_all {|v| self[v] }
				end
			else
				raise ArgumentError
		end
	end

	def each(&block)
		Site.each {|show| self.send(:[], show, &block) }
	end

	def filter(&block)
		Filter.new(self,&block)
	end

	def by_downloaded(dl=true)
		filter {|v| v.downloaded? == !!dl }
	end

	def by_date(y, m=nil, d=nil)
		if m.nil? and d.nil?
			filter {|v| v.date and v.date.year == y }
		elsif d.nil?
			filter {|v| v.date and v.date.year == y and v.date.month == m }
		else
			filter {|v| v.date and v.date.year == y and v.date.month == m and v.date.day == d }
		end
	end

	def by_id(vid)
		filter {|v| v.id == vid }
	end

	def by_tag(tag)
		filter {|v| v.tags.include? tag }
	end

	def by_text(text) 
		filter {|v| 
			v.title && v.title.downcase.include?(text.downcase) or 
			v.description && v.description.downcase.include?(text.downcase) or
			v.tags.include? text
		}
	end

	def by(arg)
		arg = arg.to_s
		case arg
			when 'downloaded'
				by_downloaded(true)
			when '!downloaded'
				by_downloaded(false)
			when /^(\d{4})$/
				by_date($1.to_i)
			when /^(\d{4})-(\d{1,2})$/
				by_date($1.to_i, $2.to_i)
			when /^(\d{4})-(\d{1,2})-(\d{1,2})$/
				by_date($1.to_i, $2.to_i, $3.to_i)
			when /^\d{5,}$/
				by_id(arg.to_i)
			else
				by_text(arg)
		end
	end
end

if __FILE__ == $0
	command = ARGV.shift or raise "Usage: leecher <download/list> [filters...]"
	action = case command.downcase
		when 'download'
			proc {|v| 
				puts "Fetching #{v}" unless v.downloaded?
				begin
					v.download
				rescue OpenURI::HTTPError => x
					$stderr.puts "Download failed, skipping: #{x}"
				rescue Errno::ENOENT => xm
					$stderr.puts "Download failed, skipping: #{x}"
				end
			}
		when 'list'
			proc {|v|
				puts "#{v} #{v.tags.join(', ')}"
			}
		when 'play'
			proc {|v|
				puts v
				system(*MEDIA_PLAYER, v.filename)
			}
		else
			raise "Unknown command #{command}"
	end

	filter = ARGV.inject(Filter.new) {|f, arg| f.by(arg)}

	Site.load
	filter.each(&action)
end


Punycoded URLs in Ruby

This is just a proof-of-concept snippet for how to internationalize domain names using punycode4r (sudo gem install punycode4r).

For more information please see:
- Punycode
- Internationalized domain name



#!/usr/local/bin/ruby -Ku

# NOTE: The following is not the complete source code by Kazuhiro NISHIYAMA.
#       For the full source code with more features, comments & test cases please see: 
#       open -e `gem environment gemdir`/gems/punycode4r-0.2.0/lib/punycode.rb
#
# This is pure Ruby implementing Punycode (RFC 3492).
# (original ANSI C code (C89) implementing Punycode is in RFC 3492)
#
# copyright (c) 2005 Kazuhiro NISHIYAMA<