Class: Iodine::Base::RackTmpFileIO

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



329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
# File 'ext/iodine/rb-rack-io.c', line 329

static VALUE rio_get_io(int argc, VALUE *argv, VALUE self) {
  if (TCPSOCKET_CLASS == Qnil)
    return Qfalse;
  intptr_t fduuid = get_uuid(self);
  // hijack the IO object
  VALUE fd = INT2FIX(sock_uuid2fd(fduuid));
  VALUE env = rb_ivar_get(self, env_id);
  // make sure we're not repeating ourselves
  VALUE new_io = rb_hash_aref(env, IODINE_R_HIJACK_IO);
  if (new_io != Qnil)
    return new_io;
  // VALUE new_io = how the fuck do we create a new IO from the fd?
  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.



304
305
306
307
# File 'ext/iodine/rb-rack-io.c', line 304

static VALUE tfio_close(VALUE self) {
  (void)self;
  return Qnil;
}

#eachObject

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



310
311
312
313
314
315
316
317
318
# File 'ext/iodine/rb-rack-io.c', line 310

static VALUE tfio_each(VALUE self) {
  rb_need_block();
  rio_rewind(self);
  VALUE str = Qnil;
  while ((str = tfio_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.



214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
# File 'ext/iodine/rb-rack-io.c', line 214

static VALUE tfio_gets(VALUE self) {
  int fd = get_tmpfile(self);
  size_t pos = get_pos(self);
  size_t end = get_end(self);
  if (pos == end)
    return Qnil;
  size_t pos_e = pos;
  char c;
  int ret;
  VALUE buffer;

  do {
    ret = pread(fd, &c, 1, pos_e);
  } while (ret > 0 && c != '\n' && (++pos_e < end));
  set_pos(self, pos_e + 1);
  if (pos > pos_e) {
    buffer = rb_str_buf_new(pos_e - pos);
    // make sure the buffer is binary encoded.
    rb_enc_associate(buffer, IodineBinaryEncoding);
    if (pread(fd, RSTRING_PTR(buffer), pos_e - pos, pos) < 0)
      return Qnil;
    rb_str_set_len(buffer, pos_e - pos);
    return buffer;
  }
  return Qnil;
}

#read(*args) ⇒ Object

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



242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
# File 'ext/iodine/rb-rack-io.c', line 242

static VALUE tfio_read(int argc, VALUE *argv, VALUE self) {
  int fd = get_tmpfile(self);
  size_t pos = get_pos(self);
  size_t end = get_end(self);
  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.");
    ret_nil = 1;
  }
  // return if we're at the EOF.
  if (pos == end)
    goto no_data;
  // calculate length if it wasn't specified.
  if (len == 0) {
    // make sure we're not reading more then we have
    len = end - pos;
    // set position for future reads
    set_pos(self, end);
    if (len == 0)
      goto no_data;
  } else {
    // set position for future reads
    set_pos(self, pos + len);
  }
  // limit read to what we have
  if (len + pos > end)
    len = end - pos;
  // create the buffer if we don't have one.
  if (buffer == Qnil) {
    buffer = rb_str_buf_new(len);
    // make sure the buffer is binary encoded.
    rb_enc_associate(buffer, IodineBinaryEncoding);
  } else {
    // make sure the buffer is binary encoded.
    rb_enc_associate(buffer, IodineBinaryEncoding);
    if (rb_str_capacity(buffer) < (size_t)len)
      rb_str_resize(buffer, len);
  }
  // read the data.
  if (pread(fd, RSTRING_PTR(buffer), len, pos) <= 0)
    goto no_data;
  rb_str_set_len(buffer, len);
  return buffer;
no_data:
  if (ret_nil)
    return Qnil;
  else
    return rb_str_buf_new(0);
}

#rewindObject

Rewinds the IO, so that it is read from the begining.



182
183
184
185
# File 'ext/iodine/rb-rack-io.c', line 182

static VALUE rio_rewind(VALUE self) {
  set_pos(self, 0);
  return self;
}