Class: Iodine::Base::RackIO

Inherits:
Object
  • Object
show all
Defined in:
ext/iodine/rb-rack-io.c

Instance Method Summary collapse

Instance Method Details

#_hijack(*args) ⇒ Object

for Rack: rack.hijack_io



182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# File 'ext/iodine/rb-rack-io.c', line 182

static VALUE rio_get_io(int argc, VALUE *argv, VALUE self) {
  if (TCPSOCKET_CLASS == Qnil)
    return Qfalse;
  VALUE env = rb_ivar_get(self, env_id);
  http_s *h = get_handle(self);
  if (h == NULL) {
    /* we're repeating ourselves, aren't we? */
    VALUE io = rb_hash_aref(env, IODINE_R_HIJACK_IO);
    return io;
  }
  // mark update
  set_handle(self, NULL);
  // hijack the IO object
  intptr_t uuid = http_hijack(h, NULL);
  VALUE fd = INT2FIX(sock_uuid2fd(uuid));
  // VALUE new_io = how the fuck do we create a new IO from the fd?
  VALUE new_io = RubyCaller.call2(TCPSOCKET_CLASS, for_fd_id, 1,
                                  &fd); // TCPSocket.for_fd(fd) ... cool...
  rb_hash_aset(env, IODINE_R_HIJACK_IO, new_io);
  if (argc)
    rb_hash_aset(env, IODINE_R_HIJACK_CB, *argv);
  return new_io;
}

#closeObject

Does nothing - this is controlled by the server.



154
155
156
157
158
159
160
# File 'ext/iodine/rb-rack-io.c', line 154

static VALUE rio_close(VALUE self) {
  FIOBJ io = get_data(self);
  fiobj_free(io);
  rb_ivar_set(self, io_id, INT2NUM(0));
  (void)self;
  return Qnil;
}

#eachObject

Passes each line of the input to the block. This should be avoided.



163
164
165
166
167
168
169
170
171
# File 'ext/iodine/rb-rack-io.c', line 163

static VALUE rio_each(VALUE self) {
  rb_need_block();
  rio_rewind(self);
  VALUE str = Qnil;
  while ((str = rio_gets(self)) != Qnil) {
    rb_yield(str);
  }
  return self;
}

#getsObject

Gets returns a line. this is okay for small lines, but shouldn’t really be used.

Limited to ~ 1Mb of a line length.



92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'ext/iodine/rb-rack-io.c', line 92

static VALUE rio_gets(VALUE self) {
  FIOBJ io = get_data(self);
  if (!FIOBJ_TYPE_IS(io, FIOBJ_T_DATA))
    return Qnil;
  fio_cstr_s line = fiobj_data_gets(io);
  if (line.len) {
    VALUE buffer = rb_str_new(line.data, line.len);
    // make sure the buffer is binary encoded.
    rb_enc_associate(buffer, IodineBinaryEncoding);
    return buffer;
  }
  return Qnil;
}

#read(*args) ⇒ Object

Reads data from the IO, according to the Rack specifications for ‘#read`.



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'ext/iodine/rb-rack-io.c', line 107

static VALUE rio_read(int argc, VALUE *argv, VALUE self) {
  FIOBJ io = get_data(self);
  if (!FIOBJ_TYPE_IS(io, FIOBJ_T_DATA))
    return Qnil;

  VALUE buffer = Qnil;
  char ret_nil = 0;
  ssize_t len = 0;
  // get the buffer object if given
  if (argc == 2) {
    Check_Type(argv[1], T_STRING);
    buffer = argv[1];
  }
  // get the length object, if given
  if (argc > 0 && argv[0] != Qnil) {
    Check_Type(argv[0], T_FIXNUM);
    len = FIX2LONG(argv[0]);
    if (len < 0)
      rb_raise(rb_eRangeError, "length should be bigger then 0.");
    if (len == 0)
      return rb_str_buf_new(0);
    ret_nil = 1;
  }
  // return if we're at the EOF.
  fio_cstr_s buf = fiobj_data_read(io, len);
  if (buf.len) {
    // create the buffer if we don't have one.
    if (buffer == Qnil) {
      // make sure the buffer is binary encoded.
      buffer = rb_enc_str_new(buf.data, buf.len, IodineBinaryEncoding);
    } else {
      // make sure the buffer is binary encoded.
      rb_enc_associate(buffer, IodineBinaryEncoding);
      if (rb_str_capacity(buffer) < (size_t)buf.len)
        rb_str_resize(buffer, buf.len);
      memcpy(RSTRING_PTR(buffer), buf.data, buf.len);
      rb_str_set_len(buffer, buf.len);
    }
    return buffer;
  }
  if (ret_nil)
    return Qnil;
  else
    return rb_str_buf_new(0);
}

#rewindObject

IO methods



79
80
81
82
83
84
85
# File 'ext/iodine/rb-rack-io.c', line 79

static VALUE rio_rewind(VALUE self) {
  FIOBJ io = get_data(self);
  if (!FIOBJ_TYPE_IS(io, FIOBJ_T_DATA))
    return Qnil;
  fiobj_data_seek(io, 0);
  return INT2NUM(0);
}