2 # httpstatus.rb -- HTTPStatus Class
4 # Author: IPR -- Internet Programming with Ruby -- writers
5 # Copyright (c) 2000, 2001 TAKAHASHI Masayoshi, GOTOU Yuuzou
6 # Copyright (c) 2002 Internet Programming with Ruby writers. All rights
9 # $IPR: httpstatus.rb,v 1.11 2003/03/24 20:18:55 gotoyuzo Exp $
15 class Status < StandardError; end
16 class Info < Status; end
17 class Success < Status; end
18 class Redirect < Status; end
19 class Error < Status; end
20 class ClientError < Error; end
21 class ServerError < Error; end
23 class EOFError < StandardError; end
27 101, 'Switching Protocols',
31 203, 'Non-Authoritative Information',
34 206, 'Partial Content',
35 300, 'Multiple Choices',
36 301, 'Moved Permanently',
41 307, 'Temporary Redirect',
44 402, 'Payment Required',
47 405, 'Method Not Allowed',
48 406, 'Not Acceptable',
49 407, 'Proxy Authentication Required',
50 408, 'Request Timeout',
53 411, 'Length Required',
54 412, 'Precondition Failed',
55 413, 'Request Entity Too Large',
56 414, 'Request-URI Too Large',
57 415, 'Unsupported Media Type',
58 416, 'Request Range Not Satisfiable',
59 417, 'Expectation Failed',
60 500, 'Internal Server Error',
61 501, 'Not Implemented',
63 503, 'Service Unavailable',
64 504, 'Gateway Timeout',
65 505, 'HTTP Version Not Supported'
70 StatusMessage.each{|code, message|
71 var_name = message.gsub(/[ \-]/,'_').upcase
72 err_name = message.gsub(/[ \-]/,'')
75 when 100...200; parent = Info
76 when 200...300; parent = Success
77 when 300...400; parent = Redirect
78 when 400...500; parent = ClientError
79 when 500...600; parent = ServerError
83 RC_#{var_name} = #{code}
84 class #{err_name} < #{parent}
85 def self.code() RC_#{var_name} end
86 def self.reason_phrase() StatusMessage[code] end
87 def code() self::class::code end
88 def reason_phrase() self::class::reason_phrase end
93 CodeToError[code] = const_get(err_name)
96 def reason_phrase(code)
97 StatusMessage[code.to_i]
100 code.to_i >= 100 and code.to_i < 200
103 code.to_i >= 200 and code.to_i < 300
106 code.to_i >= 300 and code.to_i < 400
109 code.to_i >= 400 and code.to_i < 600
111 def client_error?(code)
112 code.to_i >= 400 and code.to_i < 500
114 def server_error?(code)
115 code.to_i >= 500 and code.to_i < 600
122 module_function :reason_phrase
123 module_function :info?, :success?, :redirect?, :error?
124 module_function :client_error?, :server_error?