1 #
2 # httpstatus.rb -- HTTPStatus Class
3 #
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
7 # reserved.
8 #
9 # $IPR: httpstatus.rb,v 1.11 2003/03/24 20:18:55 gotoyuzo Exp $
10
11 module WEBrick
12
13 module HTTPStatus
14
15 class Status < StandardError
16 def initialize(*args)
17 args[0] = AccessLog.escape(args[0]) unless args.empty?
18 super(*args)
19 end
20 class << self
21 attr_reader :code, :reason_phrase
22 end
23 def code() self::class::code end
24 def reason_phrase() self::class::reason_phrase end
25 alias to_i code
26 end
27 class Info < Status; end
28 class Success < Status; end
29 class Redirect < Status; end
30 class Error < Status; end
31 class ClientError < Error; end
32 class ServerError < Error; end
33
34 class EOFError < StandardError; end
35
36 StatusMessage = {
37 100, 'Continue',
38 101, 'Switching Protocols',
39 200, 'OK',
40 201, 'Created',
41 202, 'Accepted',
42 203, 'Non-Authoritative Information',
43 204, 'No Content',
44 205, 'Reset Content',
45 206, 'Partial Content',
46 300, 'Multiple Choices',
47 301, 'Moved Permanently',
48 302, 'Found',
49 303, 'See Other',
50 304, 'Not Modified',
51 305, 'Use Proxy',
52 307, 'Temporary Redirect',
53 400, 'Bad Request',
54 401, 'Unauthorized',
55 402, 'Payment Required',
56 403, 'Forbidden',
57 404, 'Not Found',
58 405, 'Method Not Allowed',
59 406, 'Not Acceptable',
60 407, 'Proxy Authentication Required',
61 408, 'Request Timeout',
62 409, 'Conflict',
63 410, 'Gone',
64 411, 'Length Required',
65 412, 'Precondition Failed',
66 413, 'Request Entity Too Large',
67 414, 'Request-URI Too Large',
68 415, 'Unsupported Media Type',
69 416, 'Request Range Not Satisfiable',
70 417, 'Expectation Failed',
71 500, 'Internal Server Error',
72 501, 'Not Implemented',
73 502, 'Bad Gateway',
74 503, 'Service Unavailable',
75 504, 'Gateway Timeout',
76 505, 'HTTP Version Not Supported'
77 }
78
79 CodeToError = {}
80
81 StatusMessage.each{|code, message|
82 message.freeze
83 var_name = message.gsub(/[ \-]/,'_').upcase
84 err_name = message.gsub(/[ \-]/,'')
85
86 case code
87 when 100...200; parent = Info
88 when 200...300; parent = Success
89 when 300...400; parent = Redirect
90 when 400...500; parent = ClientError
91 when 500...600; parent = ServerError
92 end
93
94 const_set("RC_#{var_name}", code)
95 err_class = Class.new(parent)
96 err_class.instance_variable_set(:@code, code)
97 err_class.instance_variable_set(:@reason_phrase, message)
98 const_set(err_name, err_class)
99 CodeToError[code] = err_class
100 }
101
102 def reason_phrase(code)
103 StatusMessage[code.to_i]
104 end
105 def info?(code)
106 code.to_i >= 100 and code.to_i < 200
107 end
108 def success?(code)
109 code.to_i >= 200 and code.to_i < 300
110 end
111 def redirect?(code)
112 code.to_i >= 300 and code.to_i < 400
113 end
114 def error?(code)
115 code.to_i >= 400 and code.to_i < 600
116 end
117 def client_error?(code)
118 code.to_i >= 400 and code.to_i < 500
119 end
120 def server_error?(code)
121 code.to_i >= 500 and code.to_i < 600
122 end
123
124 def self.[](code)
125 CodeToError[code]
126 end
127
128 module_function :reason_phrase
129 module_function :info?, :success?, :redirect?, :error?
130 module_function :client_error?, :server_error?
131 end
132 end