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



311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
# File 'ext/iodine/rb-rack-io.c', line 311

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, 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, R_HIJACK_IO, new_io);
  if (argc)
    rb_hash_aset(env, R_HIJACK_CB, *argv);
  return new_io;
}

#closeObject

Does nothing - this is controlled by the server.



289
# File 'ext/iodine/rb-rack-io.c', line 289

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

#eachObject

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



292
293
294
295
296
297
298
299
300
# File 'ext/iodine/rb-rack-io.c', line 292

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.



199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
# File 'ext/iodine/rb-rack-io.c', line 199

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, BinaryEncoding);
    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`.



227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
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
# File 'ext/iodine/rb-rack-io.c', line 227

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, BinaryEncoding);
  } else {
    // make sure the buffer is binary encoded.
    rb_enc_associate(buffer, BinaryEncoding);
    if (rb_str_capacity(buffer) < 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.



167
168
169
170
# File 'ext/iodine/rb-rack-io.c', line 167

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