Skip to content
Snippets Groups Projects
Commit e599f686 authored by Utkarsh Gupta's avatar Utkarsh Gupta
Browse files

Import Upstream version 1.2.1

parents
No related branches found
No related tags found
No related merge requests found
LICENSE 0 → 100644
The MIT License (MIT)
Copyright (c) 2013 CoTag Media
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
# http-parser
Ruby FFI bindings to [http-parser](https://github.com/joyent/http-parser) [![Build Status](https://travis-ci.org/cotag/http-parser.png)](https://travis-ci.org/cotag/http-parser)
## Install
```shell
gem install http-parser
```
This gem will compile a local copy of http-parser
## Usage
```ruby
require 'rubygems'
require 'http-parser'
#
# Create a shared parser
#
parser = HttpParser::Parser.new do |parser|
parser.on_message_begin do |inst|
puts "message begin"
end
parser.on_message_complete do |inst|
puts "message end"
end
parser.on_url do |inst, data|
puts "url: #{data}"
end
parser.on_header_field do |inst, data|
puts "field: #{data}"
end
parser.on_header_value do |inst, data|
puts "value: #{data}"
end
end
#
# Create state objects to track requests through the parser
#
request = HttpParser::Parser.new_instance do |inst|
inst.type = :request
end
#
# Parse requests
#
parser.parse request, "GET /foo HTTP/1.1\r\n"
sleep 3
parser.parse request, "Host: example.com\r\n"
sleep 3
parser.parse request, "\r\n"
#
# Re-use the memory for another request
#
request.reset!
```
## Acknowledgements
* https://github.com/joyent/http-parser#readme
* https://github.com/postmodern/ffi-http-parser#readme
* https://github.com/deepfryed/http-parser-lite#readme
\ No newline at end of file
Rakefile 0 → 100644
require 'rubygems'
require 'rake'
require 'rspec/core/rake_task'
task :default => [:compile, :test]
task :compile do
protect = ['http_parser.c', 'http_parser.h']
Dir["ext/http-parser/**/*"].each do |file|
begin
next if protect.include? File.basename(file)
FileUtils.rm file
rescue
end
end
system 'cd ext && rake'
end
RSpec::Core::RakeTask.new(:test)
require 'ffi-compiler/compile_task'
FFI::Compiler::CompileTask.new('http-parser-ext') do |t|
t.cflags << "-Wall -Wextra -O3"
t.cflags << "-D_GNU_SOURCE=1" if RbConfig::CONFIG["host_os"].downcase =~ /mingw/
t.cflags << "-arch x86_64" if t.platform.mac?
t.ldflags << "-arch x86_64" if t.platform.mac?
end
This diff is collapsed.
/* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/
#ifndef http_parser_h
#define http_parser_h
#ifdef __cplusplus
extern "C" {
#endif
/* Also update SONAME in the Makefile whenever you change these. */
#define HTTP_PARSER_VERSION_MAJOR 2
#define HTTP_PARSER_VERSION_MINOR 8
#define HTTP_PARSER_VERSION_PATCH 1
#include <stddef.h>
#if defined(_WIN32) && !defined(__MINGW32__) && \
(!defined(_MSC_VER) || _MSC_VER<1600) && !defined(__WINE__)
#include <BaseTsd.h>
typedef __int8 int8_t;
typedef unsigned __int8 uint8_t;
typedef __int16 int16_t;
typedef unsigned __int16 uint16_t;
typedef __int32 int32_t;
typedef unsigned __int32 uint32_t;
typedef __int64 int64_t;
typedef unsigned __int64 uint64_t;
#else
#include <stdint.h>
#endif
/* Compile with -DHTTP_PARSER_STRICT=0 to make less checks, but run
* faster
*/
#ifndef HTTP_PARSER_STRICT
# define HTTP_PARSER_STRICT 1
#endif
/* Maximium header size allowed. If the macro is not defined
* before including this header then the default is used. To
* change the maximum header size, define the macro in the build
* environment (e.g. -DHTTP_MAX_HEADER_SIZE=<value>). To remove
* the effective limit on the size of the header, define the macro
* to a very large number (e.g. -DHTTP_MAX_HEADER_SIZE=0x7fffffff)
*/
#ifndef HTTP_MAX_HEADER_SIZE
# define HTTP_MAX_HEADER_SIZE (80*1024)
#endif
typedef struct http_parser http_parser;
typedef struct http_parser_settings http_parser_settings;
/* Callbacks should return non-zero to indicate an error. The parser will
* then halt execution.
*
* The one exception is on_headers_complete. In a HTTP_RESPONSE parser
* returning '1' from on_headers_complete will tell the parser that it
* should not expect a body. This is used when receiving a response to a
* HEAD request which may contain 'Content-Length' or 'Transfer-Encoding:
* chunked' headers that indicate the presence of a body.
*
* Returning `2` from on_headers_complete will tell parser that it should not
* expect neither a body nor any futher responses on this connection. This is
* useful for handling responses to a CONNECT request which may not contain
* `Upgrade` or `Connection: upgrade` headers.
*
* http_data_cb does not return data chunks. It will be called arbitrarily
* many times for each string. E.G. you might get 10 callbacks for "on_url"
* each providing just a few characters more data.
*/
typedef int (*http_data_cb) (http_parser*, const char *at, size_t length);
typedef int (*http_cb) (http_parser*);
/* Status Codes */
#define HTTP_STATUS_MAP(XX) \
XX(100, CONTINUE, Continue) \
XX(101, SWITCHING_PROTOCOLS, Switching Protocols) \
XX(102, PROCESSING, Processing) \
XX(200, OK, OK) \
XX(201, CREATED, Created) \
XX(202, ACCEPTED, Accepted) \
XX(203, NON_AUTHORITATIVE_INFORMATION, Non-Authoritative Information) \
XX(204, NO_CONTENT, No Content) \
XX(205, RESET_CONTENT, Reset Content) \
XX(206, PARTIAL_CONTENT, Partial Content) \
XX(207, MULTI_STATUS, Multi-Status) \
XX(208, ALREADY_REPORTED, Already Reported) \
XX(226, IM_USED, IM Used) \
XX(300, MULTIPLE_CHOICES, Multiple Choices) \
XX(301, MOVED_PERMANENTLY, Moved Permanently) \
XX(302, FOUND, Found) \
XX(303, SEE_OTHER, See Other) \
XX(304, NOT_MODIFIED, Not Modified) \
XX(305, USE_PROXY, Use Proxy) \
XX(307, TEMPORARY_REDIRECT, Temporary Redirect) \
XX(308, PERMANENT_REDIRECT, Permanent Redirect) \
XX(400, BAD_REQUEST, Bad Request) \
XX(401, UNAUTHORIZED, Unauthorized) \
XX(402, PAYMENT_REQUIRED, Payment Required) \
XX(403, FORBIDDEN, Forbidden) \
XX(404, NOT_FOUND, Not Found) \
XX(405, METHOD_NOT_ALLOWED, Method Not Allowed) \
XX(406, NOT_ACCEPTABLE, Not Acceptable) \
XX(407, PROXY_AUTHENTICATION_REQUIRED, Proxy Authentication Required) \
XX(408, REQUEST_TIMEOUT, Request Timeout) \
XX(409, CONFLICT, Conflict) \
XX(410, GONE, Gone) \
XX(411, LENGTH_REQUIRED, Length Required) \
XX(412, PRECONDITION_FAILED, Precondition Failed) \
XX(413, PAYLOAD_TOO_LARGE, Payload Too Large) \
XX(414, URI_TOO_LONG, URI Too Long) \
XX(415, UNSUPPORTED_MEDIA_TYPE, Unsupported Media Type) \
XX(416, RANGE_NOT_SATISFIABLE, Range Not Satisfiable) \
XX(417, EXPECTATION_FAILED, Expectation Failed) \
XX(421, MISDIRECTED_REQUEST, Misdirected Request) \
XX(422, UNPROCESSABLE_ENTITY, Unprocessable Entity) \
XX(423, LOCKED, Locked) \
XX(424, FAILED_DEPENDENCY, Failed Dependency) \
XX(426, UPGRADE_REQUIRED, Upgrade Required) \
XX(428, PRECONDITION_REQUIRED, Precondition Required) \
XX(429, TOO_MANY_REQUESTS, Too Many Requests) \
XX(431, REQUEST_HEADER_FIELDS_TOO_LARGE, Request Header Fields Too Large) \
XX(451, UNAVAILABLE_FOR_LEGAL_REASONS, Unavailable For Legal Reasons) \
XX(500, INTERNAL_SERVER_ERROR, Internal Server Error) \
XX(501, NOT_IMPLEMENTED, Not Implemented) \
XX(502, BAD_GATEWAY, Bad Gateway) \
XX(503, SERVICE_UNAVAILABLE, Service Unavailable) \
XX(504, GATEWAY_TIMEOUT, Gateway Timeout) \
XX(505, HTTP_VERSION_NOT_SUPPORTED, HTTP Version Not Supported) \
XX(506, VARIANT_ALSO_NEGOTIATES, Variant Also Negotiates) \
XX(507, INSUFFICIENT_STORAGE, Insufficient Storage) \
XX(508, LOOP_DETECTED, Loop Detected) \
XX(510, NOT_EXTENDED, Not Extended) \
XX(511, NETWORK_AUTHENTICATION_REQUIRED, Network Authentication Required) \
enum http_status
{
#define XX(num, name, string) HTTP_STATUS_##name = num,
HTTP_STATUS_MAP(XX)
#undef XX
};
/* Request Methods */
#define HTTP_METHOD_MAP(XX) \
XX(0, DELETE, DELETE) \
XX(1, GET, GET) \
XX(2, HEAD, HEAD) \
XX(3, POST, POST) \
XX(4, PUT, PUT) \
/* pathological */ \
XX(5, CONNECT, CONNECT) \
XX(6, OPTIONS, OPTIONS) \
XX(7, TRACE, TRACE) \
/* WebDAV */ \
XX(8, COPY, COPY) \
XX(9, LOCK, LOCK) \
XX(10, MKCOL, MKCOL) \
XX(11, MOVE, MOVE) \
XX(12, PROPFIND, PROPFIND) \
XX(13, PROPPATCH, PROPPATCH) \
XX(14, SEARCH, SEARCH) \
XX(15, UNLOCK, UNLOCK) \
XX(16, BIND, BIND) \
XX(17, REBIND, REBIND) \
XX(18, UNBIND, UNBIND) \
XX(19, ACL, ACL) \
/* subversion */ \
XX(20, REPORT, REPORT) \
XX(21, MKACTIVITY, MKACTIVITY) \
XX(22, CHECKOUT, CHECKOUT) \
XX(23, MERGE, MERGE) \
/* upnp */ \
XX(24, MSEARCH, M-SEARCH) \
XX(25, NOTIFY, NOTIFY) \
XX(26, SUBSCRIBE, SUBSCRIBE) \
XX(27, UNSUBSCRIBE, UNSUBSCRIBE) \
/* RFC-5789 */ \
XX(28, PATCH, PATCH) \
XX(29, PURGE, PURGE) \
/* CalDAV */ \
XX(30, MKCALENDAR, MKCALENDAR) \
/* RFC-2068, section 19.6.1.2 */ \
XX(31, LINK, LINK) \
XX(32, UNLINK, UNLINK) \
/* icecast */ \
XX(33, SOURCE, SOURCE) \
enum http_method
{
#define XX(num, name, string) HTTP_##name = num,
HTTP_METHOD_MAP(XX)
#undef XX
};
enum http_parser_type { HTTP_REQUEST, HTTP_RESPONSE, HTTP_BOTH };
/* Flag values for http_parser.flags field */
enum flags
{ F_CHUNKED = 1 << 0
, F_CONNECTION_KEEP_ALIVE = 1 << 1
, F_CONNECTION_CLOSE = 1 << 2
, F_CONNECTION_UPGRADE = 1 << 3
, F_TRAILING = 1 << 4
, F_UPGRADE = 1 << 5
, F_SKIPBODY = 1 << 6
, F_CONTENTLENGTH = 1 << 7
};
/* Map for errno-related constants
*
* The provided argument should be a macro that takes 2 arguments.
*/
#define HTTP_ERRNO_MAP(XX) \
/* No error */ \
XX(OK, "success") \
\
/* Callback-related errors */ \
XX(CB_message_begin, "the on_message_begin callback failed") \
XX(CB_url, "the on_url callback failed") \
XX(CB_header_field, "the on_header_field callback failed") \
XX(CB_header_value, "the on_header_value callback failed") \
XX(CB_headers_complete, "the on_headers_complete callback failed") \
XX(CB_body, "the on_body callback failed") \
XX(CB_message_complete, "the on_message_complete callback failed") \
XX(CB_status, "the on_status callback failed") \
XX(CB_chunk_header, "the on_chunk_header callback failed") \
XX(CB_chunk_complete, "the on_chunk_complete callback failed") \
\
/* Parsing-related errors */ \
XX(INVALID_EOF_STATE, "stream ended at an unexpected time") \
XX(HEADER_OVERFLOW, \
"too many header bytes seen; overflow detected") \
XX(CLOSED_CONNECTION, \
"data received after completed connection: close message") \
XX(INVALID_VERSION, "invalid HTTP version") \
XX(INVALID_STATUS, "invalid HTTP status code") \
XX(INVALID_METHOD, "invalid HTTP method") \
XX(INVALID_URL, "invalid URL") \
XX(INVALID_HOST, "invalid host") \
XX(INVALID_PORT, "invalid port") \
XX(INVALID_PATH, "invalid path") \
XX(INVALID_QUERY_STRING, "invalid query string") \
XX(INVALID_FRAGMENT, "invalid fragment") \
XX(LF_EXPECTED, "LF character expected") \
XX(INVALID_HEADER_TOKEN, "invalid character in header") \
XX(INVALID_CONTENT_LENGTH, \
"invalid character in content-length header") \
XX(UNEXPECTED_CONTENT_LENGTH, \
"unexpected content-length header") \
XX(INVALID_CHUNK_SIZE, \
"invalid character in chunk size header") \
XX(INVALID_CONSTANT, "invalid constant string") \
XX(INVALID_INTERNAL_STATE, "encountered unexpected internal state")\
XX(STRICT, "strict mode assertion failed") \
XX(PAUSED, "parser is paused") \
XX(UNKNOWN, "an unknown error occurred")
/* Define HPE_* values for each errno value above */
#define HTTP_ERRNO_GEN(n, s) HPE_##n,
enum http_errno {
HTTP_ERRNO_MAP(HTTP_ERRNO_GEN)
};
#undef HTTP_ERRNO_GEN
/* Get an http_errno value from an http_parser */
#define HTTP_PARSER_ERRNO(p) ((enum http_errno) (p)->http_errno)
struct http_parser {
/** PRIVATE **/
unsigned int type : 2; /* enum http_parser_type */
unsigned int flags : 8; /* F_* values from 'flags' enum; semi-public */
unsigned int state : 7; /* enum state from http_parser.c */
unsigned int header_state : 7; /* enum header_state from http_parser.c */
unsigned int index : 7; /* index into current matcher */
unsigned int lenient_http_headers : 1;
uint32_t nread; /* # bytes read in various scenarios */
uint64_t content_length; /* # bytes in body (0 if no Content-Length header) */
/** READ-ONLY **/
unsigned short http_major;
unsigned short http_minor;
unsigned int status_code : 16; /* responses only */
unsigned int method : 8; /* requests only */
unsigned int http_errno : 7;
/* 1 = Upgrade header was present and the parser has exited because of that.
* 0 = No upgrade header present.
* Should be checked when http_parser_execute() returns in addition to
* error checking.
*/
unsigned int upgrade : 1;
/** PUBLIC **/
void *data; /* A pointer to get hook to the "connection" or "socket" object */
};
struct http_parser_settings {
http_cb on_message_begin;
http_data_cb on_url;
http_data_cb on_status;
http_data_cb on_header_field;
http_data_cb on_header_value;
http_cb on_headers_complete;
http_data_cb on_body;
http_cb on_message_complete;
/* When on_chunk_header is called, the current chunk length is stored
* in parser->content_length.
*/
http_cb on_chunk_header;
http_cb on_chunk_complete;
};
enum http_parser_url_fields
{ UF_SCHEMA = 0
, UF_HOST = 1
, UF_PORT = 2
, UF_PATH = 3
, UF_QUERY = 4
, UF_FRAGMENT = 5
, UF_USERINFO = 6
, UF_MAX = 7
};
/* Result structure for http_parser_parse_url().
*
* Callers should index into field_data[] with UF_* values iff field_set
* has the relevant (1 << UF_*) bit set. As a courtesy to clients (and
* because we probably have padding left over), we convert any port to
* a uint16_t.
*/
struct http_parser_url {
uint16_t field_set; /* Bitmask of (1 << UF_*) values */
uint16_t port; /* Converted UF_PORT string */
struct {
uint16_t off; /* Offset into buffer in which field starts */
uint16_t len; /* Length of run in buffer */
} field_data[UF_MAX];
};
/* Returns the library version. Bits 16-23 contain the major version number,
* bits 8-15 the minor version number and bits 0-7 the patch level.
* Usage example:
*
* unsigned long version = http_parser_version();
* unsigned major = (version >> 16) & 255;
* unsigned minor = (version >> 8) & 255;
* unsigned patch = version & 255;
* printf("http_parser v%u.%u.%u\n", major, minor, patch);
*/
unsigned long http_parser_version(void);
void http_parser_init(http_parser *parser, enum http_parser_type type);
/* Initialize http_parser_settings members to 0
*/
void http_parser_settings_init(http_parser_settings *settings);
/* Executes the parser. Returns number of parsed bytes. Sets
* `parser->http_errno` on error. */
size_t http_parser_execute(http_parser *parser,
const http_parser_settings *settings,
const char *data,
size_t len);
/* If http_should_keep_alive() in the on_headers_complete or
* on_message_complete callback returns 0, then this should be
* the last message on the connection.
* If you are the server, respond with the "Connection: close" header.
* If you are the client, close the connection.
*/
int http_should_keep_alive(const http_parser *parser);
/* Returns a string version of the HTTP method. */
const char *http_method_str(enum http_method m);
/* Returns a string version of the HTTP status code. */
const char *http_status_str(enum http_status s);
/* Return a string name of the given error */
const char *http_errno_name(enum http_errno err);
/* Return a string description of the given error */
const char *http_errno_description(enum http_errno err);
/* Initialize all http_parser_url members to 0 */
void http_parser_url_init(struct http_parser_url *u);
/* Parse a URL; return nonzero on failure */
int http_parser_parse_url(const char *buf, size_t buflen,
int is_connect,
struct http_parser_url *u);
/* Pause or un-pause the parser; a nonzero value pauses */
void http_parser_pause(http_parser *parser, int paused);
/* Checks if this is the final chunk of the body. */
int http_body_is_final(const http_parser *parser);
#ifdef __cplusplus
}
#endif
#endif
# frozen_string_literal: true
# -*- encoding: utf-8 -*-
$:.push File.expand_path("../lib", __FILE__)
require "http-parser/version"
Gem::Specification.new do |s|
s.name = "http-parser"
s.version = HttpParser::VERSION
s.authors = ["Stephen von Takach"]
s.email = ["steve@cotag.me"]
s.license = 'MIT'
s.homepage = "https://github.com/cotag/http-parser"
s.summary = "Ruby bindings to joyent/http-parser"
s.description = <<-EOF
A super fast http parser for ruby.
Cross platform and multiple ruby implementation support thanks to ffi.
EOF
s.add_dependency 'ffi-compiler', '>= 1.0', '< 2.0'
s.add_development_dependency 'rake', '~> 11.2'
s.add_development_dependency 'rspec', '~> 3.5'
s.add_development_dependency 'yard', '~> 0.9'
s.files = Dir["{lib}/**/*"] + %w(Rakefile http-parser.gemspec README.md LICENSE)
s.files += ["ext/http-parser/http_parser.c", "ext/http-parser/http_parser.h"]
s.test_files = Dir["spec/**/*"]
s.extra_rdoc_files = ["README.md"]
s.extensions << "ext/Rakefile"
s.require_paths = ["lib"]
end
# frozen_string_literal: true
require "ffi" # Bindings to C libraries
require "http-parser/ext" # Loads http-parser ext
require "http-parser/errors" # http-parser error definitions
require "http-parser/types" # http-parser data structures
require "http-parser/parser" # the core http-parser abstraction
module HttpParser
end
# frozen_string_literal: true
module HttpParser
class Error < StandardError
class OK < Error; end
# Any callback-related errors
class CALLBACK < Error; end
# Parsing-related errors
class INVALID_EOF_STATE < Error; end
class HEADER_OVERFLOW < Error; end
class CLOSED_CONNECTION < Error; end
class INVALID_VERSION < Error; end
class INVALID_STATUS < Error; end
class INVALID_METHOD < Error; end
class INVALID_URL < Error; end
class INVALID_HOST < Error; end
class INVALID_PORT < Error; end
class INVALID_PATH < Error; end
class INVALID_QUERY_STRING < Error; end
class INVALID_FRAGMENT < Error; end
class LF_EXPECTED < Error; end
class INVALID_HEADER_TOKEN < Error; end
class INVALID_CONTENT_LENGTH < Error; end
class INVALID_CHUNK_SIZE < Error; end
class INVALID_CONSTANT < Error; end
class INVALID_INTERNAL_STATE < Error; end
class STRICT < Error; end
class PAUSED < Error; end
class UNKNOWN < Error; end
end
ERRORS = {
:OK => Error::OK,
:CB_message_begin => Error::CALLBACK,
:CB_url => Error::CALLBACK,
:CB_header_field => Error::CALLBACK,
:CB_header_value => Error::CALLBACK,
:CB_headers_complete => Error::CALLBACK,
:CB_body => Error::CALLBACK,
:CB_message_complete => Error::CALLBACK,
:CB_status => Error::CALLBACK,
:CB_chunk_header => Error::CALLBACK,
:CB_chunk_complete => Error::CALLBACK,
:INVALID_EOF_STATE => Error::INVALID_EOF_STATE,
:HEADER_OVERFLOW => Error::HEADER_OVERFLOW,
:CLOSED_CONNECTION => Error::CLOSED_CONNECTION,
:INVALID_VERSION => Error::INVALID_VERSION,
:INVALID_STATUS => Error::INVALID_STATUS,
:INVALID_METHOD => Error::INVALID_METHOD,
:INVALID_URL => Error::INVALID_URL,
:INVALID_HOST => Error::INVALID_HOST,
:INVALID_PORT => Error::INVALID_PORT,
:INVALID_PATH => Error::INVALID_PATH,
:INVALID_QUERY_STRING => Error::INVALID_QUERY_STRING,
:INVALID_FRAGMENT => Error::INVALID_FRAGMENT,
:LF_EXPECTED => Error::LF_EXPECTED,
:INVALID_HEADER_TOKEN => Error::INVALID_HEADER_TOKEN,
:INVALID_CONTENT_LENGTH => Error::INVALID_CONTENT_LENGTH,
:INVALID_CHUNK_SIZE => Error::INVALID_CHUNK_SIZE,
:INVALID_CONSTANT => Error::INVALID_CONSTANT,
:INVALID_INTERNAL_STATE => Error::INVALID_INTERNAL_STATE,
:STRICT => Error::STRICT,
:PAUSED => Error::PAUSED,
:UNKNOWN => Error::UNKNOWN
}
attach_function :err_desc, :http_errno_description, [:int], :string
attach_function :err_name, :http_errno_name, [:int], :string
end
# frozen_string_literal: true
require 'ffi'
require 'ffi-compiler/loader'
module HttpParser
extend FFI::Library
ffi_lib FFI::Compiler::Loader.find('http-parser-ext')
end
# frozen_string_literal: true
module HttpParser
class Parser
CALLBACKS = [
:on_message_begin, :on_url, :on_status, :on_header_field, :on_header_value,
:on_headers_complete, :on_body, :on_message_complete, :on_chunk_header, :on_chunk_complete
]
#
# Returns a new request/response instance variable
#
def self.new_instance &block
::HttpParser::Instance.new &block
end
#
# Initializes the Parser instance.
#
def initialize(callback_obj = nil)
@settings = ::HttpParser::Settings.new
@callbacks = {} # so GC doesn't clean them up on java
if not callback_obj.nil?
CALLBACKS.each do |callback|
self.__send__(callback, &callback_obj.method(callback)) if callback_obj.respond_to? callback
end
end
yield self if block_given?
end
#
# Registers an `on_message_begin` callback.
#
# @yield [instance]
# The given block will be called when the HTTP message begins.
#
# @yieldparam [HttpParser::Instance] instance
# The state so far of the request / response being processed.
#
def on_message_begin(&block)
cb = Callback.new(&block)
@callbacks[:on_message_begin] = cb
@settings[:on_message_begin] = cb
end
#
# Registers an `on_url` callback.
#
# @yield [instance, url]
# The given block will be called when the Request URI is recognized
# within the Request-Line.
#
# @yieldparam [HttpParser::Instance] instance
# The state so far of the request / response being processed.
#
# @yieldparam [String] url
# The recognized Request URI.
#
# @see http://www.w3.org/Protocols/rfc2616/rfc2616-sec5.html#sec5.1.2
#
def on_url(&block)
cb = DataCallback.new(&block)
@callbacks[:on_url] = cb
@settings[:on_url] = cb
end
#
# Registers an `on_status_complete` callback.
#
# @yield [instance]
# The given block will be called when the status is recognized.
#
# @yieldparam [HttpParser::Instance] instance
# The state so far of the request / response being processed.
#
def on_status(&block)
cb = DataCallback.new(&block)
@callbacks[:on_status] = cb
@settings[:on_status] = cb
end
#
# Registers an `on_header_field` callback.
#
# @yield [instance, field]
# The given block will be called when a Header name is recognized
# in the Headers.
#
# @yieldparam [HttpParser::Instance] instance
# The state so far of the request / response being processed.
#
# @yieldparam [String] field
# A recognized Header name.
#
# @see http://www.w3.org/Protocols/rfc2616/rfc2616-sec4.html#sec4.5
#
def on_header_field(&block)
cb = DataCallback.new(&block)
@callbacks[:on_header_field] = cb
@settings[:on_header_field] = cb
end
#
# Registers an `on_header_value` callback.
#
# @yield [instance, value]
# The given block will be called when a Header value is recognized
# in the Headers.
#
# @yieldparam [HttpParser::Instance] instance
# The state so far of the request / response being processed.
#
# @yieldparam [String] value
# A recognized Header value.
#
# @see http://www.w3.org/Protocols/rfc2616/rfc2616-sec4.html#sec4.5
#
def on_header_value(&block)
cb = DataCallback.new(&block)
@callbacks[:on_header_value] = cb
@settings[:on_header_value] = cb
end
#
# Registers an `on_headers_complete` callback.
#
# @yield [instance]
# The given block will be called when the Headers stop.
#
# @yieldparam [HttpParser::Instance] instance
# The state so far of the request / response being processed.
#
def on_headers_complete(&block)
cb = Callback.new(&block)
@callbacks[:on_headers_complete] = cb
@settings[:on_headers_complete] = cb
end
#
# Registers an `on_body` callback.
#
# @yield [instance, body]
# The given block will be called when the body is recognized in the
# message body.
#
# @yieldparam [HttpParser::Instance] instance
# The state so far of the request / response being processed.
#
# @yieldparam [String] body
# The full body or a chunk of the body from a chunked
# Transfer-Encoded stream.
#
# @see http://www.w3.org/Protocols/rfc2616/rfc2616-sec4.html#sec4.5
#
def on_body(&block)
cb = DataCallback.new(&block)
@callbacks[:on_body] = cb
@settings[:on_body] = cb
end
#
# Registers an `on_message_begin` callback.
#
# @yield [instance]
# The given block will be called when the message completes.
#
# @yieldparam [HttpParser::Instance] instance
# The state so far of the request / response being processed.
#
def on_message_complete(&block)
cb = Callback.new(&block)
@callbacks[:on_message_complete] = cb
@settings[:on_message_complete] = cb
end
#
# Registers an `on_chunk_header` callback.
#
# @yield [instance]
# The given block will be called when a new chunk header is received.
#
# @yieldparam [HttpParser::Instance] instance
# The state so far of the request / response being processed.
#
def on_chunk_header(&block)
cb = Callback.new(&block)
@callbacks[:on_message_complete] = cb
@settings[:on_message_complete] = cb
end
#
# Registers an `on_chunk_complete` callback.
#
# @yield [instance]
# The given block will be called when the current chunk completes.
#
# @yieldparam [HttpParser::Instance] instance
# The state so far of the request / response being processed.
#
def on_chunk_complete(&block)
cb = Callback.new(&block)
@callbacks[:on_message_complete] = cb
@settings[:on_message_complete] = cb
end
#
# Parses data.
#
# @param [HttpParser::Instance] inst
# The state so far of the request / response being processed.
#
# @param [String] data
# The data to parse against the instance specified.
#
# @return [Boolean]
# Returns true if the data was parsed successfully.
#
def parse(inst, data)
::HttpParser.http_parser_execute(inst, @settings, data, data.length)
return inst.error?
end
protected
class Callback < ::FFI::Function
#
# Creates a new Parser callback.
#
def self.new(&block)
super(:int, [::HttpParser::Instance.ptr]) do |parser|
begin
catch(:return) { yield(parser); 0 }
rescue
-1
end
end
end
end
class DataCallback < ::FFI::Function
def self.new(&block)
super(:int, [::HttpParser::Instance.ptr, :pointer, :size_t]) do |parser, buffer, length|
begin
data = buffer.get_bytes(0, length)
catch(:return) { yield(parser, data); 0 }
rescue
-1
end
end
end
end
end
end
# frozen_string_literal: true
module HttpParser
HTTP_MAX_HEADER_SIZE = (80 * 1024)
#
# These share a byte of data as a bitmap
#
TYPES = enum :http_parser_type, [
:request, 0,
:response,
:both
]
FLAG = {
:CHUNKED => 1 << 2,
:CONNECTION_KEEP_ALIVE => 1 << 3,
:CONNECTION_CLOSE => 1 << 4,
:CONNECTION_UPGRADE => 1 << 5,
:TRAILING => 1 << 6,
:UPGRADE => 1 << 7,
:SKIPBODY => 1 << 8
}
#
# Request Methods
#
METHODS = enum :http_method, [
:DELETE, 0,
:GET,
:HEAD,
:POST,
:PUT,
# pathological
:CONNECT,
:OPTIONS,
:TRACE,
# webdav
:COPY,
:LOCK,
:MKCOL,
:MOVE,
:PROPFIND,
:PROPPATCH,
:SEARCH,
:UNLOCK,
:BIND,
:REBIND,
:UNBIND,
:ACL,
# subversion
:REPORT,
:MKACTIVITY,
:CHECKOUT,
:MERGE,
# upnp
:MSEARCH,
:NOTIFY,
:SUBSCRIBE,
:UNSUBSCRIBE,
# RFC-5789
:PATCH,
:PURGE,
# CalDAV
:MKCALENDAR,
# RFC-2068, section 19.6.1.2
:LINK,
:UNLINK
]
UrlFields = enum :http_parser_url_fields, [
:SCHEMA, 0,
:HOST,
:PORT,
:PATH,
:QUERY,
:FRAGMENT,
:USERINFO,
:MAX
]
#
# Effectively this represents a request instance
#
class Instance < FFI::Struct
layout :type_flags, :uchar,
:state, :uchar,
:header_state, :uchar,
:index, :uchar,
:nread, :uint32,
:content_length, :uint64,
# READ-ONLY
:http_major, :ushort,
:http_minor, :ushort,
:status_code, :ushort, # responses only
:method, :uchar, # requests only
:error_upgrade, :uchar, # errno = first 7bits, upgrade = last bit
# PUBLIC
:data, :pointer
def initialize(ptr = nil)
if ptr then super(ptr)
else
super()
self.type = :both
end
yield self if block_given?
::HttpParser.http_parser_init(self, self.type) unless ptr
end
#
# Resets the parser.
#
# @param [:request, :response, :both] new_type
# The new type for the parser.
#
def reset!(new_type = type)
::HttpParser.http_parser_init(self, new_type)
end
#
# The type of the parser.
#
# @return [:request, :response, :both]
# The parser type.
#
def type
TYPES[self[:type_flags] & 0x3]
end
#
# Sets the type of the parser.
#
# @param [:request, :response, :both] new_type
# The new parser type.
#
def type=(new_type)
self[:type_flags] = (flags | TYPES[new_type])
end
#
# Flags for the parser.
#
# @return [Integer]
# Parser flags.
#
def flags
(self[:type_flags] & 0xfc)
end
#
# The parsed HTTP major version number.
#
# @return [Integer]
# The HTTP major version number.
#
def http_major
self[:http_major]
end
#
# The parsed HTTP minor version number.
#
# @return [Integer]
# The HTTP minor version number.
#
def http_minor
self[:http_minor]
end
#
# The parsed HTTP version.
#
# @return [String]
# The HTTP version.
#
def http_version
"%d.%d" % [self[:http_major], self[:http_minor]]
end
#
# The parsed HTTP response Status Code.
#
# @return [Integer]
# The HTTP Status Code.
#
# @see http://www.w3.org/Protocols/rfc2616/rfc2616-sec6.html#sec6.1.1
#
def http_status
self[:status_code]
end
#
# The parsed HTTP Method.
#
# @return [Symbol]
# The HTTP Method name.
#
# @see http://www.w3.org/Protocols/rfc2616/rfc2616-sec5.html#sec5.1.1
#
def http_method
METHODS[self[:method]]
end
#
# Determines whether the `Upgrade` header has been parsed.
#
# @return [Boolean]
# Specifies whether the `Upgrade` header has been seen.
#
# @see http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.42
#
def upgrade?
(self[:error_upgrade] & 0b10000000) > 0
end
#
# Determines whether an error occurred during processing.
#
# @return [Boolean]
# Did a parsing error occur with the request?
#
def error?
error = (self[:error_upgrade] & 0b1111111)
return error != 0
end
#
# Returns the error that occurred during processing.
#
# @return [StandarError]
# Returns the error that occurred.
#
def error
error = (self[:error_upgrade] & 0b1111111)
return nil if error == 0
err = ::HttpParser.err_name(error)[4..-1] # HPE_ is at the start of all these errors
klass = ERRORS[err.to_sym]
err = "#{::HttpParser.err_desc(error)} (#{err})"
return klass.nil? ? Error::UNKNOWN.new(err) : klass.new(err)
end
#
# Additional data attached to the parser.
#
# @return [FFI::Pointer]
# Pointer to the additional data.
#
def data
self[:data]
end
#
# Determines whether the `Connection: keep-alive` header has been
# parsed.
#
# @return [Boolean]
# Specifies whether the Connection should be kept alive.
#
# @see http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.10
#
def keep_alive?
::HttpParser.http_should_keep_alive(self) > 0
end
#
# Determines if a chunked response has completed
#
# @return [Boolean]
# Specifies whether the chunked response has completed
#
def final_chunk?
::HttpParser.http_body_is_final(self) > 0
end
#
# Halts the parser if called in a callback
#
def stop!
throw :return, 1
end
#
# Indicates an error has occurred when called in a callback
#
def error!
throw :return, -1
end
end
class FieldData < FFI::Struct
layout :off, :uint16,
:len, :uint16
end
class HttpParserUrl < FFI::Struct
layout :field_set, :uint16,
:port, :uint16,
:field_data, [FieldData, UrlFields[:MAX]]
end
callback :http_data_cb, [Instance.ptr, :pointer, :size_t], :int
callback :http_cb, [Instance.ptr], :int
class Settings < FFI::Struct
layout :on_message_begin, :http_cb,
:on_url, :http_data_cb,
:on_status, :http_data_cb,
:on_header_field, :http_data_cb,
:on_header_value, :http_data_cb,
:on_headers_complete, :http_cb,
:on_body, :http_data_cb,
:on_message_complete, :http_cb,
:on_chunk_header, :http_cb,
:on_chunk_complete, :http_cb
end
attach_function :http_parser_init, [Instance.by_ref, :http_parser_type], :void
attach_function :http_parser_execute, [Instance.by_ref, Settings.by_ref, :pointer, :size_t], :size_t
attach_function :http_should_keep_alive, [Instance.by_ref], :int
# Checks if this is the final chunk of the body
attach_function :http_body_is_final, [Instance.by_ref], :int
end
# frozen_string_literal: true
module HttpParser
VERSION = "1.2.1"
end
require 'http-parser'
describe HttpParser::Parser, "#initialize" do
before :each do
@inst = HttpParser::Parser.new_instance
end
it "should return true when error" do
expect(subject.parse(@inst, "GETS / HTTP/1.1\r\n")).to eq(true)
expect(@inst.error?).to eq(true)
end
it "should return false on success" do
expect(subject.parse(@inst, "GET / HTTP/1.1\r\n")).to eq(false)
expect(@inst.error?).to eq(false)
end
it "the error should be inspectable" do
expect(subject.parse(@inst, "GETS / HTTP/1.1\r\n")).to eq(true)
expect(@inst.error).to be_kind_of(::HttpParser::Error::INVALID_METHOD)
expect(@inst.error?).to eq(true)
end
it "raises different error types depending on the error" do
expect(subject.parse(@inst, "GET / HTTP/23\r\n")).to eq(true)
expect(@inst.error).to be_kind_of(::HttpParser::Error::INVALID_VERSION)
expect(@inst.error?).to eq(true)
end
context "callback errors" do
subject do
described_class.new do |parser|
parser.on_url { |inst, data| raise 'unhandled' }
end
end
it "should handle unhandled errors gracefully" do
expect(subject.parse(@inst, "GET /foo?q=1 HTTP/1.1")).to eq(true)
expect(@inst.error?).to eq(true)
expect(@inst.error).to be_kind_of(::HttpParser::Error::CALLBACK)
end
end
end
require 'http-parser'
describe ::HttpParser::Instance, "#initialize" do
context "when given a block" do
it "should yield the new Instance" do
expected = nil
described_class.new { |inst| expected = inst }
expect(expected).to be_kind_of(described_class)
end
it "should allow changing the parser type" do
inst = described_class.new do |inst|
inst.type = :request
end
expect(inst.type).to eq(:request)
end
end
describe "#type" do
it "should default to :both" do
expect(subject.type).to eq(:both)
end
it "should convert the type to a Symbol" do
subject[:type_flags] = ::HttpParser::TYPES[:request]
expect(subject.type).to eq(:request)
end
it "should extract the type from the type_flags field" do
subject[:type_flags] = ((0xff & ~0x3) | ::HttpParser::TYPES[:response])
expect(subject.type).to eq(:response)
end
end
describe "#type=" do
it "should set the type" do
subject.type = :response
expect(subject.type).to eq(:response)
end
it "should not change flags" do
flags = (0xff & ~0x3)
subject[:type_flags] = flags
subject.type = :request
expect(subject[:type_flags]).to eq((flags | ::HttpParser::TYPES[:request]))
end
end
describe "#stop!" do
it "should throw :return, 1" do
expect { subject.stop! }.to throw_symbol(:return,1)
end
end
describe "#error!" do
it "should throw :return, -1" do
expect { subject.error! }.to throw_symbol(:return,-1)
end
end
it "should not change the type" do
inst = described_class.new do |inst|
inst.type = :request
end
inst.reset!
expect(inst.type).to eq(:request)
end
end
require 'http-parser'
describe HttpParser::Parser, "#initialize" do
before :each do
@inst = HttpParser::Parser.new_instance
end
describe "callbacks" do
describe "on_message_begin" do
subject do
described_class.new do |parser|
parser.on_message_begin { @begun = true }
end
end
it "should trigger on a new request" do
subject.parse @inst, "GET / HTTP/1.1"
expect(@begun).to eq(true)
end
end
describe "on_url" do
let(:expected) { '/foo?q=1' }
subject do
described_class.new do |parser|
parser.on_url { |inst, data| @url = data }
end
end
it "should pass the recognized url" do
subject.parse @inst, "GET "
expect(@url).to be_nil
subject.parse @inst, "#{expected} HTTP/1.1"
expect(@url).to eq(expected)
end
end
describe "on_header_field" do
let(:expected) { 'Host' }
subject do
described_class.new do |parser|
parser.on_header_field { |inst, data| @header_field = data }
end
end
it "should pass the recognized header-name" do
subject.parse @inst, "GET /foo HTTP/1.1\r\n"
expect(@header_field).to be_nil
subject.parse @inst, "#{expected}: example.com\r\n"
expect(@header_field).to eq(expected)
end
end
describe "on_header_value" do
let(:expected) { 'example.com' }
subject do
described_class.new do |parser|
parser.on_header_value { |inst, data| @header_value = data }
end
end
it "should pass the recognized header-value" do
subject.parse @inst, "GET /foo HTTP/1.1\r\n"
expect(@header_value).to be_nil
subject.parse @inst, "Host: #{expected}\r\n"
expect(@header_value).to eq(expected)
end
end
describe "on_headers_complete" do
subject do
described_class.new do |parser|
parser.on_headers_complete { @header_complete = true }
end
end
it "should trigger on the last header" do
subject.parse @inst, "GET / HTTP/1.1\r\nHost: example.com\r\n"
expect(@header_complete).to be_nil
subject.parse @inst, "\r\n"
expect(@header_complete).to eq(true)
end
context "when #stop! is called" do
subject do
described_class.new do |parser|
parser.on_headers_complete { @inst.stop! }
parser.on_body { |inst, data| @body = data }
end
end
it "should indicate there is no request body to parse" do
subject.parse @inst, "GET / HTTP/1.1\r\nHost: example.com\r\n\r\nBody"
expect(@body).to be_nil
end
end
end
describe "on_body" do
let(:expected) { "Body" }
subject do
described_class.new do |parser|
parser.on_body { |inst, data| @body = data }
end
end
it "should trigger on the body" do
subject.parse @inst, "POST / HTTP/1.1\r\nTransfer-Encoding: chunked\r\n\r\n"
expect(@body).to be_nil
subject.parse @inst, "#{"%x" % expected.length}\r\n#{expected}"
expect(@body).to eq(expected)
end
end
describe "on_message_complete" do
subject do
described_class.new do |parser|
parser.on_message_complete { @message_complete = true }
end
end
it "should trigger at the end of the message" do
subject.parse @inst, "GET / HTTP/1.1\r\n"
expect(@message_complete).to be_nil
subject.parse @inst, "Host: example.com\r\n\r\n"
expect(@message_complete).to eq(true)
end
end
end
describe "#http_method" do
let(:expected) { :POST }
it "should set the http_method field" do
subject.parse @inst, "#{expected} / HTTP/1.1\r\n"
expect(@inst.http_method).to eq(expected)
end
end
describe "#http_major" do
let(:expected) { 1 }
before do
@inst.type = :request
end
context "when parsing requests" do
it "should set the http_major field" do
subject.parse @inst, "GET / HTTP/#{expected}."
expect(@inst.http_major).to eq(expected)
end
end
context "when parsing responses" do
before do
@inst.type = :response
end
it "should set the http_major field" do
subject.parse @inst, "HTTP/#{expected}."
expect(@inst.http_major).to eq(expected)
end
end
end
describe "#http_minor" do
let(:expected) { 2 }
context "when parsing requests" do
it "should set the http_minor field" do
subject.parse @inst, "GET / HTTP/1.#{expected}\r\n"
expect(@inst.http_minor).to eq(expected)
end
end
context "when parsing responses" do
before do
@inst.type = :response
end
it "should set the http_major field" do
subject.parse @inst, "HTTP/1.#{expected} "
expect(@inst.http_minor).to eq(expected)
end
end
end
describe "#http_version" do
let(:expected) { '1.1' }
before do
subject.parse @inst, "GET / HTTP/#{expected}\r\n"
end
it "should combine #http_major and #http_minor" do
expect(@inst.http_version).to eq(expected)
end
end
describe "#http_status" do
context "when parsing requests" do
before do
subject.parse @inst, "GET / HTTP/1.1\r\nHost: example.com\r\n\r\n"
end
it "should not be set" do
expect(@inst.http_status).to be_zero
end
end
context "when parsing responses" do
let(:expected) { 200 }
before do
@inst.type = :response
subject.parse @inst, "HTTP/1.1 #{expected} OK\r\n"
subject.parse @inst, "Location: http://example.com/\r\n\r\n"
end
it "should set the http_status field" do
expect(@inst.http_status).to eq(expected)
end
end
end
describe "#upgrade?" do
let(:upgrade) { 'WebSocket' }
before do
subject.parse @inst, "GET /demo HTTP/1.1\r\n"
subject.parse @inst, "Upgrade: #{upgrade}\r\n"
subject.parse @inst, "Connection: Upgrade\r\n"
subject.parse @inst, "Host: example.com\r\n"
subject.parse @inst, "Origin: http://example.com\r\n"
subject.parse @inst, "WebSocket-Protocol: sample\r\n"
subject.parse @inst, "\r\n"
end
it "should return true if the Upgrade header was set" do
expect(@inst.upgrade?).to eq(true)
end
end
describe "pipelined requests" do
subject do
@begun = 0
described_class.new do |parser|
parser.on_message_begin { @begun += 1 }
end
end
it "should trigger on a new request" do
subject.parse @inst, "GET /demo HTTP/1.1\r\n\r\nGET /demo HTTP/1.1\r\n\r\n"
expect(@begun).to eq(2)
end
end
describe "method based instead of block based" do
class SomeParserClass
attr_reader :url
def on_url(inst, data)
@url = data
end
end
let(:expected) { '/foo?q=1' }
it "should simplify the process" do
callbacks = SomeParserClass.new
parser = described_class.new(callbacks)
parser.parse @inst, "GET "
expect(callbacks.url).to be_nil
parser.parse @inst, "#{expected} HTTP/1.1"
expect(callbacks.url).to eq(expected)
end
end
end
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment