Skip to content

Commit

Permalink
init commit
Browse files Browse the repository at this point in the history
  • Loading branch information
robin committed Sep 3, 2009
0 parents commit 557d50f
Show file tree
Hide file tree
Showing 10 changed files with 467 additions and 0 deletions.
23 changes: 23 additions & 0 deletions .autotest
@@ -0,0 +1,23 @@
# -*- ruby -*-

require 'autotest/restart'

# Autotest.add_hook :initialize do |at|
# at.extra_files << "../some/external/dependency.rb"
#
# at.libs << ":../some/external"
#
# at.add_exception 'vendor'
#
# at.add_mapping(/dependency.rb/) do |f, _|
# at.files_matching(/test_.*rb$/)
# end
#
# %w(TestA TestB).each do |klass|
# at.extra_class_map[klass] = "test/test_misc.rb"
# end
# end

# Autotest.add_hook :run_command do |at|
# system "rake build"
# end
6 changes: 6 additions & 0 deletions History.txt
@@ -0,0 +1,6 @@
=== 1.0.0 / 2009-09-03

* 1 major enhancement

* Birthday!

7 changes: 7 additions & 0 deletions Manifest.txt
@@ -0,0 +1,7 @@
History.txt
Manifest.txt
README.txt
Rakefile
bin/url_checker
lib/url_checker.rb
test/test_url_checker.rb
48 changes: 48 additions & 0 deletions README.txt
@@ -0,0 +1,48 @@
= url_checker

* FIX (url)

== DESCRIPTION:

url_checker is tool to check if the given url is accessible.

== FEATURES/PROBLEMS:

* check url

== SYNOPSIS:

FIX (code sample of usage)

== REQUIREMENTS:

* FIX (list of requirements)

== INSTALL:

* FIX (sudo gem install, anything else)

== LICENSE:

(The MIT License)

Copyright (c) 2009 Robin Lu

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
'Software'), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
12 changes: 12 additions & 0 deletions Rakefile
@@ -0,0 +1,12 @@
# -*- ruby -*-

require 'rubygems'
require 'hoe'
require './lib/url_checker.rb'

Hoe.new('url_checker', UrlChecker::VERSION) do |p|
# p.rubyforge_name = 'url_checkerx' # if different than lowercase project name
p.developer('Robin Lu', 'robinlu@in-src.com')
end

# vim: syntax=Ruby
3 changes: 3 additions & 0 deletions bin/url_checker
@@ -0,0 +1,3 @@
#!/usr/bin/env ruby

abort "you need to write me"
73 changes: 73 additions & 0 deletions lib/error_code.rb
@@ -0,0 +1,73 @@
module URLChecker
ERR_UNKNOWN = 0
ERR_CONNECTION_LOST = 1
ERR_MISFORMAT_RESPONSE = 2
ERR_TIMEOUT = 3
ERR_HTTP_STATUS_MISMATCH = 4
ERR_HTTP_CONTENT_MISMATCH = 5
ERR_DNS_FAIL = 6
ERR_NO_CONNECTION = 7
ERR_HTTP_REDIRECTION_MISMATCH = 8

def self.message(err_code)
case err_code
when ERR_UNKNOWN
'Unknown'
when ERR_CONNECTION_LOST
'Connection Lost'
when ERR_MISFORMAT_RESPONSE
'Mistformat Response'
when ERR_TIMEOUT
'Timeout'
when ERR_HTTP_STATUS_MISMATCH
'HTTP Status Mismatch'
when ERR_HTTP_CONTENT_MISMATCH
'HTTP Content Mismatch'
when ERR_DNS_FAIL
'DNS错误'
when ERR_NO_CONNECTION
'No Connection'
when ERR_HTTP_REDIRECTION_MISMATCH
'Rerediction Mismatch'
end
end

def self.map_dns_error(err)
case err
when Dnsruby::ResolvError
1
#A timeout error raised while querying for a resource
when Dnsruby::ResolvTimeout
2
#The requested domain does not exist
when Dnsruby::NXDomain
3
#A format error in a received DNS message
when Dnsruby::FormErr
4
#Indicates a failure in the remote resolver
when Dnsruby::ServFail
5
#The requested operation is not implemented in the remote resolver
when Dnsruby::NotImp
6
#The requested operation was refused by the remote resolver
when Dnsruby::Refused
7
#Another kind of resolver error has occurred
when Dnsruby::OtherResolvError
8
#Indicates an error in decoding an incoming DNS message
when Dnsruby::DecodeError
9
#Indicates an error encoding a DNS message for transmission
when Dnsruby::EncodeError
10
#Indicates an error verifying
when Dnsruby::VerifyError
11
else
0
end
end
end
144 changes: 144 additions & 0 deletions lib/http_checker.rb
@@ -0,0 +1,144 @@
require 'eventmachine'
require 'error_code'

class HttpChecker < EventMachine::Protocols::HttpClient

attr_writer :time_out
def time_out
@time_out || 30
end

def time_used
@end_time ||= Time.now
@end_time - @start_time
end

def self.request( args = {} )
args[:port] ||= 80
EventMachine.connect( args[:host], args[:port], self ) {|c|
if args[:ssl]
c.start_tls
end
# According to the docs, we will get here AFTER post_init is called.
c.instance_eval {@args = args}
}
end

# HACK:
# derived from original HttpClient from em 0.10 to support timeout
def post_init
super
@is_time_out = false
EventMachine::add_timer(time_out) {
@is_time_out = true
}
end

# HACK:
# derived from original HttpClient from em 0.10 to support errorcode
def parse_response_line
if @headers.first =~ /\AHTTP\/1\.[01] ([\d]{3})/
@status = $1.to_i
else
set_deferred_status :failed, {
:status => 0,
:error => URLChecker::ERR_MISFORMAT_RESPONSE
}
close_connection
end
end

# HACK:
# derived from original HttpClient from em 0.10 to support errorcode
def unbind
if !@connected
set_deferred_status :failed, {:status => 0, :error => URLChecker::ERR_CONNECTION_LOST}
elsif (@read_state == :content and @content_length == nil)
dispatch_response
elsif (@read_state == :content && @content.size>0 && @headers.size>0 && @status)
dispatch_response
elsif @is_time_out
set_deferred_status :failed, {:status => 0, :error => URLChecker::ERR_TIMEOUT }
end
end

# HACK:
# derived from original HttpClient from em 0.10 to support benchmark
def dispatch_response
@end_time = Time.now
super
end

# HACK:
# derived from original HttpClient from em 0.10 to modify user agent
def send_request args
args[:verb] ||= args[:method] # Support :method as an alternative to :verb.
args[:verb] ||= :get # IS THIS A GOOD IDEA, to default to GET if nothing was specified?

verb = args[:verb].to_s.upcase
unless ["GET", "POST", "PUT", "DELETE", "HEAD"].include?(verb)
set_deferred_status :failed, {:status => 0} # TODO, not signalling the error type
return # NOTE THE EARLY RETURN, we're not sending any data.
end

request = args[:request] || "/"
unless request[0,1] == "/"
request = "/" + request
end

qs = args[:query_string] || ""
if qs.length > 0 and qs[0,1] != '?'
qs = "?" + qs
end

# Allow an override for the host header if it's not the connect-string.
host = args[:host_header] || args[:host] || "_"
# For now, ALWAYS tuck in the port string, although we may want to omit it if it's the default.
port = args[:port]

# POST items.
postcontenttype = args[:contenttype] || "application/octet-stream"
postcontent = args[:content] || ""
raise "oversized content in HTTP POST" if postcontent.length > MaxPostContentLength

# ESSENTIAL for the request's line-endings to be CRLF, not LF. Some servers misbehave otherwise.
# TODO: We ASSUME the caller wants to send a 1.1 request. May not be a good assumption.
req = [
"#{verb} #{request}#{qs} HTTP/1.1",
"Host: #{host}:#{port}",
"User-agent: Watchdog/0.1",
"Accept: text/html,application/xhtml+xml,application/xml"
]

if verb == "POST" || verb == "PUT"
req << "Content-type: #{postcontenttype}"
req << "Content-length: #{postcontent.length}"
end

# TODO, this cookie handler assumes it's getting a single, semicolon-delimited string.
# Eventually we will want to deal intelligently with arrays and hashes.
if args[:cookie]
req << "Cookie: #{args[:cookie]}"
end

req << ""
reqstring = req.map {|l| "#{l}\r\n"}.join
send_data reqstring

if verb == "POST" || verb == "PUT"
send_data postcontent
end
end

def self.parse_header(header)
header.inject({}) {|h, sec|
if sec =~ /(.+?):(.+)/
k = $1.downcase
v = $2.strip
h[k] = v
end
h
}
end

end

0 comments on commit 557d50f

Please sign in to comment.