Class: Iodine::Base::RackIO

Inherits:
Object
  • Object
show all
Defined in:
ext/iodine/iodine_rack_io.c

Instance Method Summary collapse

Instance Method Details

#_hijack(*args) ⇒ Object

for Rack: rack.hijack_io



185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'ext/iodine/iodine_rack_io.c', line 185

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(fio_uuid2fd(uuid));
  // VALUE new_io = how the fuck do we create a new IO from the fd?
  VALUE new_io = IodineCaller.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.



157
158
159
160
161
162
163
# File 'ext/iodine/iodine_rack_io.c', line 157

static VALUE rio_close(VALUE self) {
  // FIOBJ io = get_data(self);
  // fiobj_free(io); // we don't call fiobj_dup, do we?
  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.



166
167
168
169
170
171
172
173
174
# File 'ext/iodine/iodine_rack_io.c', line 166

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.



97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'ext/iodine/iodine_rack_io.c', line 97

static VALUE rio_gets(VALUE self) {
  FIOBJ io = get_data(self);
  if (!FIOBJ_TYPE_IS(io, FIOBJ_T_DATA))
    return Qnil;
  fio_str_info_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.



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
# File 'ext/iodine/iodine_rack_io.c', line 112

static VALUE rio_read(int argc, VALUE *argv, VALUE self) {
  FIOBJ io = get_data(self);
  VALUE buffer = Qnil;
  uint8_t ret_nil = 0;
  ssize_t len = 0;
  if (!FIOBJ_TYPE_IS(io, FIOBJ_T_DATA)) {
    return (argc > 0 && argv[0] != Qnil) ? Qnil : rb_str_buf_new(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_str_info_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;
  }
  return ret_nil ? Qnil : rb_str_buf_new(0);
}

#rewindObject

IO methods



84
85
86
87
88
89
90
# File 'ext/iodine/iodine_rack_io.c', line 84

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