mirror of https://gitee.com/bigwinds/arangodb
171 lines
4.6 KiB
Plaintext
171 lines
4.6 KiB
Plaintext
################################################################################
|
|
### @brief error handling
|
|
###
|
|
### @file
|
|
###
|
|
### DISCLAIMER
|
|
###
|
|
### Copyright 2012 triagens GmbH, Cologne, Germany
|
|
###
|
|
### Licensed under the Apache License, Version 2.0 (the "License");
|
|
### you may not use this file except in compliance with the License.
|
|
### You may obtain a copy of the License at
|
|
###
|
|
### http://www.apache.org/licenses/LICENSE-2.0
|
|
###
|
|
### Unless required by applicable law or agreed to in writing, software
|
|
### distributed under the License is distributed on an "AS IS" BASIS,
|
|
### WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
### See the License for the specific language governing permissions and
|
|
### limitations under the License.
|
|
###
|
|
### Copyright holder is triAGENS GmbH, Cologne, Germany
|
|
###
|
|
### @author Dr. Frank Celler
|
|
### @author Copyright 2012, triAGENS GmbH, Cologne, Germany
|
|
################################################################################
|
|
|
|
module Arango
|
|
|
|
## -----------------------------------------------------------------------------
|
|
## --SECTION-- HttpRequest
|
|
## -----------------------------------------------------------------------------
|
|
|
|
class HttpRequest
|
|
# TODO: attr_reader :body, :headers, :parameters, :request_type, :suffix
|
|
|
|
def body()
|
|
return @body
|
|
end
|
|
|
|
def headers()
|
|
return @headers
|
|
end
|
|
|
|
def parameters()
|
|
return @parameters
|
|
end
|
|
|
|
def request_type()
|
|
return @request_type
|
|
end
|
|
|
|
def suffix()
|
|
return @suffix
|
|
end
|
|
end
|
|
|
|
## -----------------------------------------------------------------------------
|
|
## --SECTION-- HttpResponse
|
|
## -----------------------------------------------------------------------------
|
|
|
|
class HttpResponse
|
|
# TODO: attr_accessor :content_type, :body
|
|
# TODO: attr_reader :status
|
|
|
|
def content_type()
|
|
return @content_type
|
|
end
|
|
|
|
def content_type=(type)
|
|
@content_type = type
|
|
end
|
|
|
|
def body()
|
|
return @body
|
|
end
|
|
|
|
def body=(text)
|
|
@body = text.to_s
|
|
end
|
|
def status()
|
|
return @status
|
|
end
|
|
|
|
def status=(code)
|
|
@status = case new_status
|
|
when :ok then 200
|
|
when :created then 201
|
|
when :accepted then 202
|
|
when :partial then 203
|
|
when :no_content then 204
|
|
|
|
when :moved_permanently then 301
|
|
when :found then 302
|
|
when :see_other then 303
|
|
when :not_modified then 304
|
|
when :temporary_redirect then 307
|
|
|
|
when :http_bad then 400
|
|
when :unauthorized then 401
|
|
when :payment then 402
|
|
when :forbidden then 403
|
|
when :not_found then 404
|
|
when :method_not_allowed then 405
|
|
when :conflict then 409
|
|
when :precondition_failed then 412
|
|
when :unprocessable_entity then 422
|
|
|
|
when :server_error then 500
|
|
when :not_implemented then 501
|
|
when :bad_gateway then 502
|
|
when :service_unavailable then 503
|
|
|
|
else new_status.to_i
|
|
end
|
|
end
|
|
end
|
|
|
|
## -----------------------------------------------------------------------------
|
|
## --SECTION-- AbstractServlet
|
|
## -----------------------------------------------------------------------------
|
|
|
|
class AbstractServlet
|
|
HTTP_METHODS = [:get, :put, :post, :delete, :head]
|
|
|
|
def service(req, res)
|
|
p "Body: <#{req.body}>"
|
|
p "Headers: <#{req.headers}>"
|
|
p "Parameters: <#{req.parameters}>"
|
|
p "RequestType: <#{req.request_type}>"
|
|
p "Suffix: <#{req.suffix}>"
|
|
|
|
requested_method = req.request_type.to_s.downcase.to_sym
|
|
|
|
p "Request Method: #{requested_method}"
|
|
|
|
if HTTP_METHODS.include? requested_method
|
|
function_name = ("do_" + requested_method.to_s.upcase).to_sym
|
|
p "Function Name: #{function_name}"
|
|
self.send(function_name, req, res)
|
|
else
|
|
self.unknown_method(req, res, requested_method)
|
|
end
|
|
end
|
|
|
|
HTTP_METHODS.each do |meth|
|
|
function_name = ("do_" + meth.to_s.upcase).to_sym
|
|
p "HTTP METHODS: #{HTTP_METHODS}"
|
|
p "Define Function Name: #{function_name}"
|
|
|
|
define_method(function_name) do |req, res|
|
|
res.status = :method_not_allowed
|
|
end
|
|
end
|
|
|
|
def generate_unknown_method(req, res, method)
|
|
res.status = :method_not_allowed
|
|
end
|
|
end
|
|
|
|
end
|
|
|
|
## -----------------------------------------------------------------------------
|
|
## --SECTION-- END-OF-FILE
|
|
## -----------------------------------------------------------------------------
|
|
|
|
## Local Variables:
|
|
## mode: outline-minor
|
|
## outline-regexp: "^\\(### @brief\\|## --SECTION--\\|# -\\*- \\)"
|
|
## End:
|