Class: Raptor::Http2Parser
- Inherits:
-
Object
- Object
- Raptor::Http2Parser
- Defined in:
- ext/raptor_http2/raptor_http2.c
Constant Summary collapse
- MAX_HEADER_LIST_SIZE =
INT2NUM(MAX_HEADER_LIST_SIZE)
Class Method Summary collapse
Instance Method Summary collapse
- #build_frame(type, flags, stream_id, payload) ⇒ Object
- #build_settings(settings) ⇒ Object
- #encode_headers(headers) ⇒ Object
- #encode_response_headers(status, headers) ⇒ Object
- #encode_response_trailers(trailers) ⇒ Object
- #parse_frame(buffer) ⇒ Object
- #parse_headers(header_block, dyn_table) ⇒ Object
- #parse_settings(payload) ⇒ Object
- #parse_window_update(payload) ⇒ Object
Class Method Details
.connection_preface ⇒ Object
847 848 849 |
# File 'ext/raptor_http2/raptor_http2.c', line 847 static VALUE h2_parser_connection_preface(VALUE self) { return rb_str_new_lit(CONNECTION_PREFACE); } |
Instance Method Details
#build_frame(type, flags, stream_id, payload) ⇒ Object
702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 |
# File 'ext/raptor_http2/raptor_http2.c', line 702
static VALUE h2_parser_build_frame(VALUE self, VALUE type, VALUE flags, VALUE stream_id, VALUE payload) {
uint8_t frame_type;
ID type_id = SYM2ID(type);
if (type_id == id_data) frame_type = FRAME_DATA;
else if (type_id == id_headers) frame_type = FRAME_HEADERS;
else if (type_id == id_priority) frame_type = FRAME_PRIORITY;
else if (type_id == id_rst_stream) frame_type = FRAME_RST_STREAM;
else if (type_id == id_settings) frame_type = FRAME_SETTINGS;
else if (type_id == id_push_promise) frame_type = FRAME_PUSH_PROMISE;
else if (type_id == id_ping) frame_type = FRAME_PING;
else if (type_id == id_goaway) frame_type = FRAME_GOAWAY;
else if (type_id == id_window_update) frame_type = FRAME_WINDOW_UPDATE;
else if (type_id == id_continuation) frame_type = FRAME_CONTINUATION;
else rb_raise(rb_eArgError, "unknown frame type");
uint8_t frame_flags = (uint8_t)NUM2UINT(flags);
uint32_t sid = NUM2UINT(stream_id);
const char *payload_ptr = NIL_P(payload) ? "" : RSTRING_PTR(payload);
size_t payload_len = NIL_P(payload) ? 0 : RSTRING_LEN(payload);
uint8_t header[FRAME_HEADER_SIZE];
header[0] = (payload_len >> 16) & 0xff;
header[1] = (payload_len >> 8) & 0xff;
header[2] = payload_len & 0xff;
header[3] = frame_type;
header[4] = frame_flags;
header[5] = (sid >> 24) & 0x7f;
header[6] = (sid >> 16) & 0xff;
header[7] = (sid >> 8) & 0xff;
header[8] = sid & 0xff;
VALUE result = rb_str_new((const char *)header, FRAME_HEADER_SIZE);
rb_str_cat(result, payload_ptr, payload_len);
return result;
}
|
#build_settings(settings) ⇒ Object
826 827 828 829 830 831 832 |
# File 'ext/raptor_http2/raptor_http2.c', line 826
static VALUE h2_parser_build_settings(VALUE self, VALUE settings) {
Check_Type(settings, T_HASH);
VALUE buf = rb_str_buf_new(36);
rb_hash_foreach(settings, build_settings_iter, buf);
return buf;
}
|
#encode_headers(headers) ⇒ Object
761 762 763 764 |
# File 'ext/raptor_http2/raptor_http2.c', line 761
static VALUE h2_parser_encode_headers(VALUE self, VALUE headers) {
Check_Type(headers, T_ARRAY);
return hpack_encode_header_block(headers);
}
|
#encode_response_headers(status, headers) ⇒ Object
766 767 768 769 770 771 772 773 774 |
# File 'ext/raptor_http2/raptor_http2.c', line 766
static VALUE h2_parser_encode_response_headers(VALUE self, VALUE status, VALUE headers) {
Check_Type(headers, T_HASH);
VALUE pairs = rb_ary_new();
rb_ary_push(pairs, rb_ary_new_from_args(2, rb_str_new_lit(":status"), rb_obj_as_string(status)));
rb_hash_foreach(headers, response_headers_iter, pairs);
return hpack_encode_header_block(pairs);
}
|
#encode_response_trailers(trailers) ⇒ Object
776 777 778 779 780 781 782 783 |
# File 'ext/raptor_http2/raptor_http2.c', line 776
static VALUE h2_parser_encode_response_trailers(VALUE self, VALUE trailers) {
Check_Type(trailers, T_HASH);
VALUE pairs = rb_ary_new();
rb_hash_foreach(trailers, response_headers_iter, pairs);
return hpack_encode_header_block(pairs);
}
|
#parse_frame(buffer) ⇒ Object
678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 |
# File 'ext/raptor_http2/raptor_http2.c', line 678
static VALUE h2_parser_parse_frame(VALUE self, VALUE buffer) {
Check_Type(buffer, T_STRING);
const uint8_t *buf = (const uint8_t *)RSTRING_PTR(buffer);
size_t len = RSTRING_LEN(buffer);
if (len < FRAME_HEADER_SIZE) return Qnil;
frame_header fh;
parse_frame_header(buf, &fh);
size_t total = FRAME_HEADER_SIZE + fh.length;
if (len < total) return Qnil;
VALUE frame = rb_hash_new();
rb_hash_aset(frame, ID2SYM(id_type), frame_type_sym(fh.type));
rb_hash_aset(frame, ID2SYM(id_length), UINT2NUM(fh.length));
rb_hash_aset(frame, ID2SYM(id_flags), UINT2NUM(fh.flags));
rb_hash_aset(frame, ID2SYM(id_stream_id), UINT2NUM(fh.stream_id));
rb_hash_aset(frame, ID2SYM(id_payload), rb_str_new((const char *)(buf + FRAME_HEADER_SIZE), fh.length));
return rb_ary_new_from_args(2, frame, SIZET2NUM(total));
}
|
#parse_headers(header_block, dyn_table) ⇒ Object
740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 |
# File 'ext/raptor_http2/raptor_http2.c', line 740
static VALUE h2_parser_parse_headers(VALUE self, VALUE header_block, VALUE dyn_table) {
raptor_h2_parser *parser;
TypedData_Get_Struct(self, raptor_h2_parser, &h2_parser_type, parser);
Check_Type(header_block, T_STRING);
Check_Type(dyn_table, T_ARRAY);
const uint8_t *buf = (const uint8_t *)RSTRING_PTR(header_block);
size_t len = RSTRING_LEN(header_block);
VALUE headers = rb_ary_new();
VALUE table = rb_ary_dup(dyn_table);
long max_size = parser->max_table_size;
if (hpack_decode_header_block(buf, len, headers, &table, &max_size) < 0)
rb_raise(eHttp2ParserError, "HPACK header block decode error");
parser->max_table_size = max_size;
return rb_ary_new_from_args(2, headers, table);
}
|
#parse_settings(payload) ⇒ Object
785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 |
# File 'ext/raptor_http2/raptor_http2.c', line 785
static VALUE h2_parser_parse_settings(VALUE self, VALUE payload) {
Check_Type(payload, T_STRING);
const uint8_t *buf = (const uint8_t *)RSTRING_PTR(payload);
size_t len = RSTRING_LEN(payload);
if (len % 6 != 0)
rb_raise(eHttp2ParserError, "invalid SETTINGS payload length");
VALUE settings = rb_hash_new();
for (size_t i = 0; i < len; i += 6) {
uint16_t id = ((uint16_t)buf[i] << 8) | buf[i + 1];
uint32_t val = ((uint32_t)buf[i + 2] << 24) | ((uint32_t)buf[i + 3] << 16) |
((uint32_t)buf[i + 4] << 8) | buf[i + 5];
switch (id) {
case SETTINGS_HEADER_TABLE_SIZE:
rb_hash_aset(settings, ID2SYM(id_header_table_size), UINT2NUM(val));
break;
case SETTINGS_ENABLE_PUSH:
rb_hash_aset(settings, ID2SYM(id_enable_push), UINT2NUM(val));
break;
case SETTINGS_MAX_CONCURRENT_STREAMS:
rb_hash_aset(settings, ID2SYM(id_max_concurrent_streams), UINT2NUM(val));
break;
case SETTINGS_INITIAL_WINDOW_SIZE:
rb_hash_aset(settings, ID2SYM(id_initial_window_size), UINT2NUM(val));
break;
case SETTINGS_MAX_FRAME_SIZE:
rb_hash_aset(settings, ID2SYM(id_max_frame_size), UINT2NUM(val));
break;
case SETTINGS_MAX_HEADER_LIST_SIZE:
rb_hash_aset(settings, ID2SYM(id_max_header_list_size), UINT2NUM(val));
break;
}
}
return settings;
}
|
#parse_window_update(payload) ⇒ Object
834 835 836 837 838 839 840 841 842 843 844 845 |
# File 'ext/raptor_http2/raptor_http2.c', line 834
static VALUE h2_parser_parse_window_update(VALUE self, VALUE payload) {
Check_Type(payload, T_STRING);
if (RSTRING_LEN(payload) != 4)
rb_raise(eHttp2ParserError, "invalid WINDOW_UPDATE payload length");
const uint8_t *buf = (const uint8_t *)RSTRING_PTR(payload);
uint32_t increment = ((uint32_t)(buf[0] & 0x7f) << 24) | ((uint32_t)buf[1] << 16) |
((uint32_t)buf[2] << 8) | buf[3];
return UINT2NUM(increment);
}
|