Class Http::NativeParser
In: lib/http/native_parser.rb
Parent: Object

This is a native ruby implementation of the http parser. It is also the reference implementation for this library. Later there will be one written in C for performance reasons, and it will have to pass the same specs as this one.

Methods

Constants

DefaultOptions = { # maximum length of an individual header line. :max_header_length => 10240, # maximum number of headers that can be passed to the server :max_headers => 100, # the size of the request body before it will be spilled # to a tempfile instead of being stored in memory. :min_tempfile_size => 1048576, # the class to use to create and manage the temporary file. # Must conform to the same interface as the stdlib Tempfile class :tempfile_class => Tempfile, }   The default set of parse options for the request.
MethodInfo = Struct.new(:must_have_body, :can_have_body)   Constants for method information
Methods = { "OPTIONS" => MethodInfo[false, true], "GET" => MethodInfo[false, false], "HEAD" => MethodInfo[false, false], "POST" => MethodInfo[true, true], "PUT" => MethodInfo[true, true], "DELETE" => MethodInfo[false, false], "TRACE" => MethodInfo[false, false], "CONNECT" => MethodInfo[false, false], }
RequestLineMatch = %r{^([a-zA-Z]+) (.+) HTTP/([0-9]+)\.([0-9]+)\r?\n}   Regex used to match the Request-Line
HeaderLineMatch = %r{^([a-zA-Z-]+):[ \t]*([[:print:]]+?)\r?\n}   Regex used to match a header line. Lines suspected of being headers are also checked against the HeaderContinueMatch to deal with multiline headers
HeaderContinueMatch = %r{^[ \t]+([[:print:]]+?)\r?\n}
EmptyLineMatch = %r{^\r?\n}
ChunkSizeLineMatch = %r{^[0-9a-fA-F]+\r?\n}   Regex used to match a size specification for a chunked segment
AnyLineMatch = %r{^.+?\r?\n}   Used as a fallback in error detection for a malformed request line or header.

Attributes

body  [R]  The body of the request as a stream object. May be either a StringIO or a TempFile, depending on request length.
headers  [R]  A hash of headers passed to the server with the request. All headers will be normalized to ALLCAPS_WITH_UNDERSCORES for consistency‘s sake.
method  [R]  The HTTP method string used. Will always be a string and all-capsed. Valid values are: "GET", "HEAD", "POST", "PUT", "DELETE". Other values will cause an exception since then we don‘t know whether the request has a body.
path  [R]  The path given by the client as a string. No processing is done on this and nearly anything is considered valid.
version  [R]  The HTTP version of the request as an array of two integers.
1,0
and [1,1] are the most likely values currently.

Public Class methods

Public Instance methods

Returns true if the http method being parsed (if known at this point in the parse) can have a body. If the method hasn‘t been determined yet, returns false.

Returns true if the request is completely done.

Returns true if the request‘s body has been consumed (really the same as done?)

Returns true if all the headers from the request have been consumed.

Returns true if the request has parsed the request-line (GET / HTTP/1.1)

Given a basic rack environment, will properly fill it in with the information gleaned from the parsed request. Note that this only fills the subset that can be determined by the parser library. Namely, the only rack. variable set is rack.input. You should also have defaults in place for SERVER_NAME and SERVER_PORT, as they are required.

Returns true if the request has a body.

Returns true if the http method being parsed (if known at this point in the parse) must have a body. If the method hasn‘t been determined yet, returns false.

Takes a string and runs it through the parser. Note that it does not consume anything it can‘t completely parse, so you should always pass complete request chunks (lines or body data) to this method. It‘s mostly for testing and convenience. In practical use, you want to use parse!, which will remove parsed data from the string you pass in.

Consumes as much of str as it can and then removes it from str. This allows you to iteratively pass data into the parser as it comes from the client.

[Validate]