lib/toycol/protocol.rb in toycol-0.3.0 vs lib/toycol/protocol.rb in toycol-0.3.1

- old
+ new

@@ -3,27 +3,34 @@ module Toycol # This class is for protocol definition and parsing request messages class Protocol @definements = {} @protocol_name = nil - @http_status_codes = Toycol::DEFAULT_HTTP_STATUS_CODES.dup - @http_request_methods = Toycol::DEFAULT_HTTP_REQUEST_METHODS.dup + @http_status_codes = DEFAULT_HTTP_STATUS_CODES.dup + @http_request_methods = DEFAULT_HTTP_REQUEST_METHODS.dup @custom_status_codes = nil @additional_request_methods = nil class << self - # For protocol definition - def define(protocol_name = nil, &block) + attr_reader :protocol_name + + # For Protocolfile to define new protocol + def define(protocol_name = :default, &block) + if @definements[protocol_name] + raise DuplicateProtocolError, + "#{protocol_name || "Anonymous"} protocol has already been defined" + end + @definements[protocol_name] = block end - # For application which use the protocol - def use(protocol_name = nil) + # For application to select which protocol to use + def use(protocol_name = :default) @protocol_name = protocol_name end - # For server which use the protocol + # For proxy server to interpret protocol definitions and parse messages def run!(message) @request_message = message.chomp return unless (block = @definements[@protocol_name]) @@ -65,55 +72,57 @@ @input = block end end end - # For server: Get the request path + # For proxy server: Fetch the request path def request_path request_path = request.instance_variable_get("@path").call(request_message) - raise UnauthorizedRequestError, "This request path is too long" if request_path.size >= 2048 - - if request_path.scan(%r{[/\w\d\-_]}).size < request_path.size - raise UnauthorizedRequestError, + if request_path.size >= 2048 + raise UnauthorizeError, + "This request path is too long" + elsif request_path.scan(%r{[/\w\d\-_]}).size < request_path.size + raise UnauthorizeError, "This request path contains unauthorized character" end request_path end - # For server: Get the request method + # For proxy server: Fetch the request method def request_method @http_request_methods.concat @additional_request_methods if @additional_request_methods request_method = request.instance_variable_get("@http_method").call(request_message) unless @http_request_methods.include? request_method - raise UndefinedRequestMethodError, "This request method is undefined" + raise UndefinementError, + "This request method is undefined" end request_method end - # For server: Get the query string + # For proxy server: Fetch the query string def query return unless (parse_query_block = request.instance_variable_get("@query")) parse_query_block.call(request_message) end - # For server: Get the input body + # For proxy server: Fetch the input body def input return unless (parsed_input_block = request.instance_variable_get("@input")) parsed_input_block.call(request_message) end - # For server: Get the message of status code + # For proxy server: fetch the message of status code def status_message(status) @http_status_codes.merge!(@custom_status_codes) if @custom_status_codes unless (message = @http_status_codes[status]) - raise UnknownStatusCodeError, "Application returns unknown status code" + raise HTTPError, "Application returns unknown status code" end message end