Class: Iodine::Base::RackStrIO

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.



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

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

#eachObject

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



173
174
175
176
177
178
179
180
181
# File 'ext/iodine/rb-rack-io.c', line 173

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



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

static VALUE strio_gets(VALUE self) {
  char *str = get_str(self);
  size_t pos = get_pos(self);
  size_t end = get_end(self);
  if (str == NULL || pos == end)
    return Qnil;
  size_t pos_e = pos;

  while ((pos_e < end) && str[pos_e] != '\n')
    pos_e++;
  set_pos(self, pos_e + 1);
  return rb_enc_str_new(str + pos, pos_e - pos, BinaryEncoding);
}

#read(*args) ⇒ Object

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



104
105
106
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
152
153
154
155
156
157
158
159
160
161
# File 'ext/iodine/rb-rack-io.c', line 104

static VALUE strio_read(int argc, VALUE *argv, VALUE self) {
  char *str = get_str(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 (str == NULL)
    goto no_data;
  // calculate length if it wasn't specified.
  if (len == 0) {
    // make sure we're not reading more then we have (string buffer)
    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);
  }
  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.
  memcpy(RSTRING_PTR(buffer), str + pos, len);
  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;
}