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; 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
|
---|
22 |
|
---|
23 | class EOFError < StandardError; end
|
---|
24 |
|
---|
25 | StatusMessage = {
|
---|
26 | 100, 'Continue',
|
---|
27 | 101, 'Switching Protocols',
|
---|
28 | 200, 'OK',
|
---|
29 | 201, 'Created',
|
---|
30 | 202, 'Accepted',
|
---|
31 | 203, 'Non-Authoritative Information',
|
---|
32 | 204, 'No Content',
|
---|
33 | 205, 'Reset Content',
|
---|
34 | 206, 'Partial Content',
|
---|
35 | 300, 'Multiple Choices',
|
---|
36 | 301, 'Moved Permanently',
|
---|
37 | 302, 'Found',
|
---|
38 | 303, 'See Other',
|
---|
39 | 304, 'Not Modified',
|
---|
40 | 305, 'Use Proxy',
|
---|
41 | 307, 'Temporary Redirect',
|
---|
42 | 400, 'Bad Request',
|
---|
43 | 401, 'Unauthorized',
|
---|
44 | 402, 'Payment Required',
|
---|
45 | 403, 'Forbidden',
|
---|
46 | 404, 'Not Found',
|
---|
47 | 405, 'Method Not Allowed',
|
---|
48 | 406, 'Not Acceptable',
|
---|
49 | 407, 'Proxy Authentication Required',
|
---|
50 | 408, 'Request Timeout',
|
---|
51 | 409, 'Conflict',
|
---|
52 | 410, 'Gone',
|
---|
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',
|
---|
62 | 502, 'Bad Gateway',
|
---|
63 | 503, 'Service Unavailable',
|
---|
64 | 504, 'Gateway Timeout',
|
---|
65 | 505, 'HTTP Version Not Supported'
|
---|
66 | }
|
---|
67 |
|
---|
68 | CodeToError = {}
|
---|
69 |
|
---|
70 | StatusMessage.each{|code, message|
|
---|
71 | var_name = message.gsub(/[ \-]/,'_').upcase
|
---|
72 | err_name = message.gsub(/[ \-]/,'')
|
---|
73 |
|
---|
74 | case code
|
---|
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
|
---|
80 | end
|
---|
81 |
|
---|
82 | eval %-
|
---|
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
|
---|
89 | alias to_i code
|
---|
90 | end
|
---|
91 | -
|
---|
92 |
|
---|
93 | CodeToError[code] = const_get(err_name)
|
---|
94 | }
|
---|
95 |
|
---|
96 | def reason_phrase(code)
|
---|
97 | StatusMessage[code.to_i]
|
---|
98 | end
|
---|
99 | def info?(code)
|
---|
100 | code.to_i >= 100 and code.to_i < 200
|
---|
101 | end
|
---|
102 | def success?(code)
|
---|
103 | code.to_i >= 200 and code.to_i < 300
|
---|
104 | end
|
---|
105 | def redirect?(code)
|
---|
106 | code.to_i >= 300 and code.to_i < 400
|
---|
107 | end
|
---|
108 | def error?(code)
|
---|
109 | code.to_i >= 400 and code.to_i < 600
|
---|
110 | end
|
---|
111 | def client_error?(code)
|
---|
112 | code.to_i >= 400 and code.to_i < 500
|
---|
113 | end
|
---|
114 | def server_error?(code)
|
---|
115 | code.to_i >= 500 and code.to_i < 600
|
---|
116 | end
|
---|
117 |
|
---|
118 | def self.[](code)
|
---|
119 | CodeToError[code]
|
---|
120 | end
|
---|
121 |
|
---|
122 | module_function :reason_phrase
|
---|
123 | module_function :info?, :success?, :redirect?, :error?
|
---|
124 | module_function :client_error?, :server_error?
|
---|
125 | end
|
---|
126 | end
|
---|