summaryrefslogtreecommitdiffhomepage
path: root/src/ruby/nxt_ruby_stream_io.c
diff options
context:
space:
mode:
authorMax Romanov <max.romanov@nginx.com>2018-08-06 17:27:33 +0300
committerMax Romanov <max.romanov@nginx.com>2018-08-06 17:27:33 +0300
commit1bb22d1e922c87d3c86c67bdce626767ee48fb5c (patch)
tree6e067a82b309c3a0d0f592f037f26d886a7f8c13 /src/ruby/nxt_ruby_stream_io.c
parentb6ce2da65c9c5229d744b2d964623b2d0f731ee9 (diff)
downloadunit-1bb22d1e922c87d3c86c67bdce626767ee48fb5c.tar.gz
unit-1bb22d1e922c87d3c86c67bdce626767ee48fb5c.tar.bz2
Unit application library.
Library now used in all language modules. Old 'nxt_app_*' code removed. See src/test/nxt_unit_app_test.c for usage sample.
Diffstat (limited to 'src/ruby/nxt_ruby_stream_io.c')
-rw-r--r--src/ruby/nxt_ruby_stream_io.c78
1 files changed, 29 insertions, 49 deletions
diff --git a/src/ruby/nxt_ruby_stream_io.c b/src/ruby/nxt_ruby_stream_io.c
index eee11c3c..3f6cac89 100644
--- a/src/ruby/nxt_ruby_stream_io.c
+++ b/src/ruby/nxt_ruby_stream_io.c
@@ -5,12 +5,12 @@
*/
#include <ruby/nxt_ruby.h>
+#include <nxt_unit.h>
static VALUE nxt_ruby_stream_io_new(VALUE class, VALUE wrap);
static VALUE nxt_ruby_stream_io_initialize(int argc, VALUE *argv, VALUE self);
static VALUE nxt_ruby_stream_io_gets(VALUE obj, VALUE args);
-static size_t nxt_ruby_stream_io_read_line(nxt_app_rmsg_t *rmsg, VALUE str);
static VALUE nxt_ruby_stream_io_each(VALUE obj, VALUE args);
static VALUE nxt_ruby_stream_io_read(VALUE obj, VALUE args);
static VALUE nxt_ruby_stream_io_rewind(VALUE obj, VALUE args);
@@ -85,63 +85,47 @@ nxt_ruby_stream_io_initialize(int argc, VALUE *argv, VALUE self)
static VALUE
nxt_ruby_stream_io_gets(VALUE obj, VALUE args)
{
- VALUE buf;
- nxt_ruby_run_ctx_t *run_ctx;
+ VALUE buf;
+ char *p;
+ size_t size, b_size;
+ nxt_unit_buf_t *b;
+ nxt_ruby_run_ctx_t *run_ctx;
+ nxt_unit_request_info_t *req;
Data_Get_Struct(obj, nxt_ruby_run_ctx_t, run_ctx);
- if (run_ctx->body_preread_size == 0) {
- return Qnil;
- }
-
- buf = rb_str_buf_new(1);
+ req = run_ctx->req;
- if (buf == Qnil) {
+ if (req->content_length == 0) {
return Qnil;
}
- run_ctx->body_preread_size -= nxt_ruby_stream_io_read_line(run_ctx->rmsg,
- buf);
-
- return buf;
-}
-
-
-static size_t
-nxt_ruby_stream_io_read_line(nxt_app_rmsg_t *rmsg, VALUE str)
-{
- size_t len, size;
- u_char *p;
- nxt_buf_t *buf;
-
- len = 0;
+ size = 0;
- for (buf = rmsg->buf; buf != NULL; buf = buf->next) {
-
- size = nxt_buf_mem_used_size(&buf->mem);
- p = memchr(buf->mem.pos, '\n', size);
+ for (b = req->content_buf; b; b = nxt_unit_buf_next(b)) {
+ b_size = b->end - b->free;
+ p = memchr(b->free, '\n', b_size);
if (p != NULL) {
p++;
- size = p - buf->mem.pos;
-
- rb_str_cat(str, (const char *) buf->mem.pos, size);
-
- len += size;
- buf->mem.pos = p;
-
+ size += p - b->free;
break;
}
- rb_str_cat(str, (const char *) buf->mem.pos, size);
+ size += b_size;
+ }
+
+ buf = rb_str_buf_new(size);
- len += size;
- buf->mem.pos = buf->mem.free;
+ if (buf == Qnil) {
+ return Qnil;
}
- rmsg->buf = buf;
+ size = nxt_unit_request_read(req, RSTRING_PTR(buf), size);
+
+ rb_str_set_len(buf, size);
- return len;
+ return buf;
}
@@ -173,12 +157,11 @@ nxt_ruby_stream_io_read(VALUE obj, VALUE args)
{
VALUE buf;
long copy_size, u_size;
- size_t len;
nxt_ruby_run_ctx_t *run_ctx;
Data_Get_Struct(obj, nxt_ruby_run_ctx_t, run_ctx);
- copy_size = run_ctx->body_preread_size;
+ copy_size = run_ctx->req->content_length;
if (RARRAY_LEN(args) > 0 && TYPE(RARRAY_PTR(args)[0]) == T_FIXNUM) {
u_size = NUM2LONG(RARRAY_PTR(args)[0]);
@@ -202,8 +185,8 @@ nxt_ruby_stream_io_read(VALUE obj, VALUE args)
return Qnil;
}
- len = nxt_app_msg_read_raw(run_ctx->task, run_ctx->rmsg,
- RSTRING_PTR(buf), (size_t) copy_size);
+ copy_size = nxt_unit_request_read(run_ctx->req, RSTRING_PTR(buf),
+ copy_size);
if (RARRAY_LEN(args) > 1 && TYPE(RARRAY_PTR(args)[1]) == T_STRING) {
@@ -211,9 +194,7 @@ nxt_ruby_stream_io_read(VALUE obj, VALUE args)
rb_str_cat(RARRAY_PTR(args)[1], RSTRING_PTR(buf), copy_size);
}
- rb_str_set_len(buf, (long) len);
-
- run_ctx->body_preread_size -= len;
+ rb_str_set_len(buf, copy_size);
return buf;
}
@@ -276,8 +257,7 @@ nxt_ruby_stream_io_s_write(nxt_ruby_run_ctx_t *run_ctx, VALUE val)
}
}
- nxt_log_error(NXT_LOG_ERR, run_ctx->task->log, "Ruby: %s",
- RSTRING_PTR(val));
+ nxt_unit_req_error(run_ctx->req, "Ruby: %s", RSTRING_PTR(val));
return RSTRING_LEN(val);
}