Class: Agoo::EarlyHints

Inherits:
Object
  • Object
show all
Defined in:
ext/agoo/early_hints.c,
ext/agoo/early_hints.c

Overview

Used to provide early hints (HTTP 103) responses to requests.

Instance Method Summary collapse

Instance Method Details

#call(links) ⇒ Object

call-seq: call(links)

Write early hints, response with status 103 using the provided headers in an Array of Array.

Example:

early_hints.call([
                  ["link", "</style.css>; rel=preload; as=style"],
                  ["link", "</script.js>; rel=preload; as=script"],
                 ])


53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'ext/agoo/early_hints.c', line 53

static VALUE
eh_call(VALUE self, VALUE links) {
    EarlyHints  eh = (EarlyHints)DATA_PTR(self);
    agooEarly link;
    agooEarly ll = NULL;
    int   i, cnt;
    VALUE lv;
    VALUE v;
    const char  *key;
    const char  *content;

    if (NULL == eh) {
  rb_raise(rb_eIOError, "early hints has been closed.");
    }
    rb_check_type(links, RUBY_T_ARRAY);

    // Link: </style.css>; rel=preload; as=style
    cnt = (int)RARRAY_LEN(links);
    for (i = cnt - 1; 0 <= i; i--) {
  lv = rb_ary_entry(links, i);
  switch (rb_type(lv)) {
  case RUBY_T_STRING:
      content = rb_string_value_ptr((VALUE*)&v);
      if (NULL == (link = agoo_early_create(content))) {
    rb_raise(rb_eNoMemError, "out of memory");
      }
      break;
  case RUBY_T_ARRAY:
      if (2 != RARRAY_LEN(lv)) {
    rb_raise(rb_eArgError, "early hints call argument must be an array of arrays that have 2 string members.");
      }
      v = rb_ary_entry(lv, 0);
      key = rb_string_value_ptr((VALUE*)&v);
      if (0 != strcasecmp("link", key)) {
    rb_raise(rb_eArgError, "Only a 'Link' header is allowed in early hints. '%s' is not allowed", key);
      }
      v = rb_ary_entry(lv, 1);
      content = rb_string_value_ptr((VALUE*)&v);
      if (NULL == (link = agoo_early_create(content))) {
    rb_raise(rb_eNoMemError, "out of memory");
      }
      break;
  default:
      rb_raise(rb_eArgError, "early hints call argument must be an array of arrays or an array of strings.");
  }
  link->next = ll;
  ll = link;
    }
    agoo_res_add_early(eh->req->res, ll);
    agoo_early_destroy(ll);

    return Qnil;
}