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



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.



176
177
178
179
# File 'ext/iodine/rb-rack-io.c', line 176

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

#eachObject

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



188
189
190
191
192
193
194
195
196
# File 'ext/iodine/rb-rack-io.c', line 188

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.



101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'ext/iodine/rb-rack-io.c', line 101

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, IodineBinaryEncoding);
}

#read(*args) ⇒ Object

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



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
162
163
164
165
166
167
168
169
170
171
172
173
# File 'ext/iodine/rb-rack-io.c', line 116

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, 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.
  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.



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;
}