-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhandler.rb
191 lines (161 loc) · 4.53 KB
/
handler.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
require 'intertwingler/error'
require 'rack/request'
require 'rack/response'
# Everything in {Intertwingler} is a handler.
#
class Intertwingler::Handler
# do this to declare the symbol
class ::Intertwingler::Engine < self
end
# This is the abstract parent {::Exception} class that acts as an escape
# hatch for responses that are something _other_ than 200-series,
# i.e. they are not-successful (albeit not strictly _unsuccessful_)
# responses.
class AnyButSuccess < Exception
STATUS = nil
def initialize message, status: nil
@status = status || self.class.const_get(:STATUS)
super message
end
attr_reader :status
alias_method :code, :status
def response
Rack::Response[status, { 'content-type' => 'text/plain' }, [message]]
end
end
# Redirects are an example of not-successful-yet-not-unsuccessful responses.
class Redirect < AnyButSuccess
# Make a new redirect "exception"
#
# @param message [#to_s] the error message
# @param status [Integer] the response code
# @param location [URI, RDF::URI, #to_s, nil]
# @param as [:uri, :rdf] URI coercion type
#
def initialize message, status: nil, location: nil, as: :uri
@location =
Intertwingler::Resolver.coerce_resource location, as: as if location
super message, status || 302
end
attr_reader :location
def response
hdr = {}
hdr['location'] = location.to_s if location
Rack::Response[status, hdr, [message]]
end
end
# This is the superclass of HTTP errors.
#
class Error < AnyButSuccess
def response
Rack::Response[status, { 'content-type' => 'text/plain' },
[message, (backtrace || []).join("\n")]]
end
class Client < self
def initialize message, status: nil
super message, status: status
end
end
class BadRequest < Client
STATUS = 400
end
class Forbidden < Client
STATUS = 403
end
class NotFound < Client
STATUS = 404
end
class NotAllowed < Client
STATUS = 405
attr_reader :request_method
def initialize message, status: 405, method: 'GET'
@request_method = method
super message, status: status
end
end
class Conflict < Client
STATUS = 409
end
class Server < self
STATUS = 500
def initialize message, status: nil
super message, status: status || 500
end
end
end
# Handle a {Rack::Request}. Return a {Rack::Response}.
#
# @param req [Rack::Request] the request.
#
# @return [Rack::Response] the response.
#
def handle req
raise NotImplementedError, 'Subclasses must implement their own `handle`'
end
# Handle a Rack request from the wire.
#
# @param env [Hash, Rack::Request] the Rack environment or request.
#
# @return [Array<(Integer, Hash, #each)>] the response.
#
def call env
# XXX maybe wrap this or put it in a base class i dunno
req = env.is_a?(Rack::Request) ? env : Rack::Request.new(env)
handle(req).finish
end
# Normalize a set of request headers into something that can be
# counted on downstream.
#
# @note This method is 100% provisional.
#
# @param req [Rack::Request] a Rack request.
# @param as_symbols [false, true] whether to coerce keys to symbols
# @param split [false, true] whether to split multi-valued headers
#
# @return [Hash] the normalized header set
#
def normalize_headers req, as_symbols: false, split: false
req.env.select do |k|
%w[CONTENT_TYPE CONTENT_LENGTH].include?(k) or k.start_with? 'HTTP'
end.reduce({}) do |hash, pair|
key = pair.first.downcase.delete_prefix('http_').tr_s(?_, ?-)
key = key.to_sym if as_symbols
val = pair.last
val = val.split(/\s*,+\s*/) if split
hash[key] = val
hash
end
end
# Initialize a handler.
#
# @param engine [Intertwingler::Engine]
# @param args [Hash{Symbol => Object}]
#
def initialize engine, **args
raise ArgumentError, 'engine must be an Intertwingler::Engine' unless
engine.is_a? ::Intertwingler::Engine
@engine = engine
end
attr_reader :engine
# Get the {Intertwingler::Resolver} for the given request.
#
# @return [Intertwingler::Resolver, nil] the resolver, maybe
#
def resolver
@engine.resolver
end
# Get the resolver's graph for the given request.
#
# @return [RDF::Repository] the graph.
#
def repo
@engine.repo
end
# Get the engine's logger.
#
# @return [Logger] the logger object.
#
def log
@engine.log
end
end