Class: Raptor::Http2Parser

Inherits:
Object
  • Object
show all
Defined in:
ext/raptor_http2/raptor_http2.c

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.connection_preface ⇒ Object



737
738
739
740
# File 'ext/raptor_http2/raptor_http2.c', line 737

static VALUE h2_connection_preface(VALUE self) {
  (void)self;
  return rb_str_new(HTTP2_CONNECTION_PREFACE, HTTP2_CONNECTION_PREFACE_LEN);
}

Instance Method Details

#build_frame(type, flags, stream_id, payload) ⇒ Object



697
698
699
700
701
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
# File 'ext/raptor_http2/raptor_http2.c', line 697

static VALUE h2_build_frame(VALUE self, VALUE type, VALUE flags, VALUE stream_id, VALUE payload) {
  (void)self;

  uint8_t frame_type;
  ID type_id = SYM2ID(type);

  if (type_id == rb_intern("data"))               frame_type = FRAME_DATA;
  else if (type_id == rb_intern("headers"))        frame_type = FRAME_HEADERS;
  else if (type_id == rb_intern("priority"))       frame_type = FRAME_PRIORITY;
  else if (type_id == rb_intern("rst_stream"))     frame_type = FRAME_RST_STREAM;
  else if (type_id == rb_intern("settings"))       frame_type = FRAME_SETTINGS;
  else if (type_id == rb_intern("push_promise"))   frame_type = FRAME_PUSH_PROMISE;
  else if (type_id == rb_intern("ping"))           frame_type = FRAME_PING;
  else if (type_id == rb_intern("goaway"))         frame_type = FRAME_GOAWAY;
  else if (type_id == rb_intern("window_update"))  frame_type = FRAME_WINDOW_UPDATE;
  else if (type_id == rb_intern("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[HTTP2_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, HTTP2_FRAME_HEADER_SIZE);
  rb_str_buf_cat(result, payload_ptr, payload_len);
  return result;
}

#build_settings(settings) ⇒ Object



778
779
780
781
782
783
784
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
# File 'ext/raptor_http2/raptor_http2.c', line 778

static VALUE h2_build_settings(VALUE self, VALUE settings) {
  (void)self;
  Check_Type(settings, T_HASH);

  VALUE buf = rb_str_buf_new(36);
  VALUE keys = rb_funcall(settings, rb_intern("keys"), 0);

  for (long i = 0; i < RARRAY_LEN(keys); i++) {
    VALUE key = rb_ary_entry(keys, i);
    VALUE val = rb_hash_aref(settings, key);
    ID key_id = SYM2ID(key);

    uint16_t id;
    if (key_id == rb_intern("header_table_size"))           id = SETTINGS_HEADER_TABLE_SIZE;
    else if (key_id == rb_intern("enable_push"))            id = SETTINGS_ENABLE_PUSH;
    else if (key_id == rb_intern("max_concurrent_streams")) id = SETTINGS_MAX_CONCURRENT_STREAMS;
    else if (key_id == rb_intern("initial_window_size"))    id = SETTINGS_INITIAL_WINDOW_SIZE;
    else if (key_id == rb_intern("max_frame_size"))         id = SETTINGS_MAX_FRAME_SIZE;
    else if (key_id == rb_intern("max_header_list_size"))   id = SETTINGS_MAX_HEADER_LIST_SIZE;
    else continue;

    uint32_t v = NUM2UINT(val);
    uint8_t entry[6];
    entry[0] = (id >> 8) & 0xff;
    entry[1] = id & 0xff;
    entry[2] = (v >> 24) & 0xff;
    entry[3] = (v >> 16) & 0xff;
    entry[4] = (v >> 8) & 0xff;
    entry[5] = v & 0xff;
    rb_str_buf_cat(buf, (const char *)entry, 6);
  }

  return buf;
}

#encode_headers(headers) ⇒ Object



596
597
598
599
600
# File 'ext/raptor_http2/raptor_http2.c', line 596

static VALUE h2_encode_headers(VALUE self, VALUE headers) {
  (void)self;
  Check_Type(headers, T_ARRAY);
  return hpack_encode_header_block(headers);
}

#encode_response_headers(status, headers) ⇒ Object



651
652
653
654
655
656
657
658
659
660
# File 'ext/raptor_http2/raptor_http2.c', line 651

static VALUE h2_encode_response_headers(VALUE self, VALUE status, VALUE headers) {
  (void)self;
  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



662
663
664
665
666
667
668
669
670
# File 'ext/raptor_http2/raptor_http2.c', line 662

static VALUE h2_encode_response_trailers(VALUE self, VALUE trailers) {
  (void)self;
  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



672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
# File 'ext/raptor_http2/raptor_http2.c', line 672

static VALUE h2_parse_frame(VALUE self, VALUE buffer) {
  (void)self;
  Check_Type(buffer, T_STRING);

  const uint8_t *buf = (const uint8_t *)RSTRING_PTR(buffer);
  size_t len = RSTRING_LEN(buffer);

  if (len < HTTP2_FRAME_HEADER_SIZE) return Qnil;

  frame_header fh;
  parse_frame_header(buf, &fh);

  size_t total = HTTP2_FRAME_HEADER_SIZE + fh.length;
  if (len < total) return Qnil;

  VALUE frame = rb_hash_new();
  rb_hash_aset(frame, ID2SYM(rb_intern("type")),      frame_type_sym(fh.type));
  rb_hash_aset(frame, ID2SYM(rb_intern("length")),     UINT2NUM(fh.length));
  rb_hash_aset(frame, ID2SYM(rb_intern("flags")),      UINT2NUM(fh.flags));
  rb_hash_aset(frame, ID2SYM(rb_intern("stream_id")),  UINT2NUM(fh.stream_id));
  rb_hash_aset(frame, ID2SYM(rb_intern("payload")),    rb_str_new((const char *)(buf + HTTP2_FRAME_HEADER_SIZE), fh.length));

  return rb_ary_new_from_args(2, frame, SIZET2NUM(total));
}

#parse_headers(header_block, dyn_table) ⇒ Object



575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
# File 'ext/raptor_http2/raptor_http2.c', line 575

static VALUE h2_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



742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
# File 'ext/raptor_http2/raptor_http2.c', line 742

static VALUE h2_parse_settings(VALUE self, VALUE payload) {
  (void)self;
  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(rb_intern("header_table_size")), UINT2NUM(val));       break;
      case SETTINGS_ENABLE_PUSH:
        rb_hash_aset(settings, ID2SYM(rb_intern("enable_push")), UINT2NUM(val));              break;
      case SETTINGS_MAX_CONCURRENT_STREAMS:
        rb_hash_aset(settings, ID2SYM(rb_intern("max_concurrent_streams")), UINT2NUM(val));   break;
      case SETTINGS_INITIAL_WINDOW_SIZE:
        rb_hash_aset(settings, ID2SYM(rb_intern("initial_window_size")), UINT2NUM(val));      break;
      case SETTINGS_MAX_FRAME_SIZE:
        rb_hash_aset(settings, ID2SYM(rb_intern("max_frame_size")), UINT2NUM(val));           break;
      case SETTINGS_MAX_HEADER_LIST_SIZE:
        rb_hash_aset(settings, ID2SYM(rb_intern("max_header_list_size")), UINT2NUM(val));     break;
    }
  }

  return settings;
}

#parse_window_update(payload) ⇒ Object



813
814
815
816
817
818
819
820
821
822
823
824
825
# File 'ext/raptor_http2/raptor_http2.c', line 813

static VALUE h2_parse_window_update(VALUE self, VALUE payload) {
  (void)self;
  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);
}