Class: HTTP::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/http-parser.rb,
ext/http-parser/ruby_http_parser.c

Defined Under Namespace

Classes: Error

Constant Summary collapse

TYPE_REQUEST =
0
TYPE_RESPONSE =
1
TYPE_BOTH =
2
CALLBACKS =
%w(
  on_body
  on_header_field
  on_header_value
  on_headers_complete
  on_message_begin
  on_message_complete
  on_status_complete
  on_url
)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type = TYPE_BOTH) ⇒ Parser

Returns a new instance of Parser.



28
29
30
31
# File 'lib/http-parser.rb', line 28

def initialize type = TYPE_BOTH
  @callbacks = {}
  reset(type)
end

Instance Attribute Details

#typeObject (readonly)

Returns the value of attribute type.



26
27
28
# File 'lib/http-parser.rb', line 26

def type
  @type
end

Instance Method Details

#<<(data) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'ext/http-parser/ruby_http_parser.c', line 86

VALUE rb_parser_parse(VALUE self, VALUE data) {
    http_parser *parser = rb_http_parser_handle(self);
    http_parser_settings settings = {
        .on_url              = (http_data_cb)rb_parser_on_url,
        .on_status_complete  = (http_cb)rb_parser_on_status_complete,
        .on_header_field     = (http_data_cb)rb_parser_on_header_field,
        .on_header_value     = (http_data_cb)rb_parser_on_header_value,
        .on_headers_complete = (http_cb)rb_parser_on_headers_complete,
        .on_body             = (http_data_cb)rb_parser_on_body,
        .on_message_begin    = (http_cb)rb_parser_on_message_begin,
        .on_message_complete = (http_cb)rb_parser_on_message_complete
    };

    size_t parsed = http_parser_execute(parser, &settings, RSTRING_PTR(data), RSTRING_LEN(data));
    if (parsed != (size_t)RSTRING_LEN(data))
        rb_raise(eParserError, "Error Parsing data: %s", http_errno_description(HTTP_PARSER_ERRNO(parser)));
    return Qtrue;
}

#errorObject



150
151
152
153
154
# File 'ext/http-parser/ruby_http_parser.c', line 150

VALUE rb_parser_error(VALUE self) {
    http_parser *parser = rb_http_parser_handle(self);
    int errno = HTTP_PARSER_ERRNO(parser);
    return errno != HPE_OK ? rb_str_new2(http_errno_description(errno)) : Qnil;
}

#error?Boolean

Returns:

  • (Boolean)


145
146
147
148
# File 'ext/http-parser/ruby_http_parser.c', line 145

VALUE rb_parser_error_q(VALUE self) {
    http_parser *parser = rb_http_parser_handle(self);
    return HTTP_PARSER_ERRNO(parser) != HPE_OK ? Qtrue : Qfalse;
}

#http_methodObject



128
129
130
131
# File 'ext/http-parser/ruby_http_parser.c', line 128

VALUE rb_parser_http_method(VALUE self) {
    http_parser *parser = rb_http_parser_handle(self);
    return rb_str_new2(http_method_str(parser->method));
}

#http_statusObject



140
141
142
143
# File 'ext/http-parser/ruby_http_parser.c', line 140

VALUE rb_parser_http_status(VALUE self) {
    http_parser *parser = rb_http_parser_handle(self);
    return INT2NUM(parser->status_code);
}

#http_versionObject



133
134
135
136
137
138
# File 'ext/http-parser/ruby_http_parser.c', line 133

VALUE rb_parser_http_version(VALUE self) {
    char version[16];
    http_parser *parser = rb_http_parser_handle(self);
    snprintf(version, 16, "%d.%d", parser->http_major, parser->http_minor);
    return rb_str_new2(version);
}

#parse(data) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'ext/http-parser/ruby_http_parser.c', line 86

VALUE rb_parser_parse(VALUE self, VALUE data) {
    http_parser *parser = rb_http_parser_handle(self);
    http_parser_settings settings = {
        .on_url              = (http_data_cb)rb_parser_on_url,
        .on_status_complete  = (http_cb)rb_parser_on_status_complete,
        .on_header_field     = (http_data_cb)rb_parser_on_header_field,
        .on_header_value     = (http_data_cb)rb_parser_on_header_value,
        .on_headers_complete = (http_cb)rb_parser_on_headers_complete,
        .on_body             = (http_data_cb)rb_parser_on_body,
        .on_message_begin    = (http_cb)rb_parser_on_message_begin,
        .on_message_complete = (http_cb)rb_parser_on_message_complete
    };

    size_t parsed = http_parser_execute(parser, &settings, RSTRING_PTR(data), RSTRING_LEN(data));
    if (parsed != (size_t)RSTRING_LEN(data))
        rb_raise(eParserError, "Error Parsing data: %s", http_errno_description(HTTP_PARSER_ERRNO(parser)));
    return Qtrue;
}

#pauseObject



111
112
113
114
115
# File 'ext/http-parser/ruby_http_parser.c', line 111

VALUE rb_parser_pause(VALUE self) {
    http_parser *parser = rb_http_parser_handle(self);
    http_parser_pause(parser, 1);
    return Qtrue;
}

#paused?Boolean

Returns:

  • (Boolean)


123
124
125
126
# File 'ext/http-parser/ruby_http_parser.c', line 123

VALUE rb_parser_is_paused(VALUE self) {
    http_parser *parser = rb_http_parser_handle(self);
    return HTTP_PARSER_ERRNO(parser) == HPE_PAUSED ? Qtrue : Qfalse;
}

#reset(value = nil) ⇒ Object



33
34
35
36
37
38
39
40
# File 'lib/http-parser.rb', line 33

def reset value = nil
  if value
    raise ArgumentError, "Invalid parser type #{value}" unless [0, 1, 2].include?(value)
    @type = value
  end

  reset!(@type)
end

#resumeObject



117
118
119
120
121
# File 'ext/http-parser/ruby_http_parser.c', line 117

VALUE rb_parser_resume(VALUE self) {
    http_parser *parser = rb_http_parser_handle(self);
    http_parser_pause(parser, 0);
    return Qtrue;
}