Deleted Added
1
2/*
3 * Copyright (C) Alexander Borisov
4 * Copyright (C) NGINX, Inc.
5 */
6
7#include <ruby/nxt_ruby.h>
8#include <nxt_unit.h>
9
10
11static VALUE nxt_ruby_stream_io_new(VALUE class, VALUE wrap);
12static VALUE nxt_ruby_stream_io_initialize(int argc, VALUE *argv, VALUE self);
13static VALUE nxt_ruby_stream_io_gets(VALUE obj, VALUE args);
14static VALUE nxt_ruby_stream_io_each(VALUE obj, VALUE args);
15static VALUE nxt_ruby_stream_io_read(VALUE obj, VALUE args);
16static VALUE nxt_ruby_stream_io_rewind(VALUE obj, VALUE args);
17static VALUE nxt_ruby_stream_io_puts(VALUE obj, VALUE args);
18static VALUE nxt_ruby_stream_io_write(VALUE obj, VALUE args);
19nxt_inline long nxt_ruby_stream_io_s_write(nxt_ruby_run_ctx_t *run_ctx,
20 VALUE val);
21static VALUE nxt_ruby_stream_io_flush(VALUE obj, VALUE args);

--- 58 unchanged lines hidden (view full) ---

80{
81 return self;
82}
83
84
85static VALUE
86nxt_ruby_stream_io_gets(VALUE obj, VALUE args)
87{
88 VALUE buf;
89 char *p;
90 size_t size, b_size;
91 nxt_unit_buf_t *b;
92 nxt_ruby_run_ctx_t *run_ctx;
93 nxt_unit_request_info_t *req;
94
95 Data_Get_Struct(obj, nxt_ruby_run_ctx_t, run_ctx);
96
97 req = run_ctx->req;
98
99 if (req->content_length == 0) {
100 return Qnil;
101 }
102
103 size = 0;
104
105 for (b = req->content_buf; b; b = nxt_unit_buf_next(b)) {
106 b_size = b->end - b->free;
107 p = memchr(b->free, '\n', b_size);
108
109 if (p != NULL) {
110 p++;
111 size += p - b->free;
112 break;
113 }
114
115 size += b_size;
116 }
117
118 buf = rb_str_buf_new(size);
119
120 if (buf == Qnil) {
121 return Qnil;
122 }
123
124 size = nxt_unit_request_read(req, RSTRING_PTR(buf), size);
125
126 rb_str_set_len(buf, size);
127
128 return buf;
129}
130
131
132static VALUE
133nxt_ruby_stream_io_each(VALUE obj, VALUE args)
134{
135 VALUE chunk;
136

--- 15 unchanged lines hidden (view full) ---

152}
153
154
155static VALUE
156nxt_ruby_stream_io_read(VALUE obj, VALUE args)
157{
158 VALUE buf;
159 long copy_size, u_size;
160 nxt_ruby_run_ctx_t *run_ctx;
161
162 Data_Get_Struct(obj, nxt_ruby_run_ctx_t, run_ctx);
163
164 copy_size = run_ctx->req->content_length;
165
166 if (RARRAY_LEN(args) > 0 && TYPE(RARRAY_PTR(args)[0]) == T_FIXNUM) {
167 u_size = NUM2LONG(RARRAY_PTR(args)[0]);
168
169 if (u_size < 0 || copy_size == 0) {
170 return Qnil;
171 }
172

--- 7 unchanged lines hidden (view full) ---

180 }
181
182 buf = rb_str_buf_new(copy_size);
183
184 if (nxt_slow_path(buf == Qnil)) {
185 return Qnil;
186 }
187
188 copy_size = nxt_unit_request_read(run_ctx->req, RSTRING_PTR(buf),
189 copy_size);
190
191 if (RARRAY_LEN(args) > 1 && TYPE(RARRAY_PTR(args)[1]) == T_STRING) {
192
193 rb_str_set_len(RARRAY_PTR(args)[1], 0);
194 rb_str_cat(RARRAY_PTR(args)[1], RSTRING_PTR(buf), copy_size);
195 }
196
197 rb_str_set_len(buf, copy_size);
198
199 return buf;
200}
201
202
203static VALUE
204nxt_ruby_stream_io_rewind(VALUE obj, VALUE args)
205{
206 return Qnil;

--- 45 unchanged lines hidden (view full) ---

252 if (TYPE(val) != T_STRING) {
253 val = rb_funcall(val, rb_intern("to_s"), 0);
254
255 if (TYPE(val) != T_STRING) {
256 return 0;
257 }
258 }
259
260 nxt_unit_req_error(run_ctx->req, "Ruby: %s", RSTRING_PTR(val));
261
262 return RSTRING_LEN(val);
263}
264
265
266static VALUE
267nxt_ruby_stream_io_flush(VALUE obj, VALUE args)
268{
269 return Qnil;
270}