nxt_ruby_stream_io.c (584:28e8e1877e62) nxt_ruby_stream_io.c (743:e0f0cd7d244a)
1
2/*
3 * Copyright (C) Alexander Borisov
4 * Copyright (C) NGINX, Inc.
5 */
6
7#include <ruby/nxt_ruby.h>
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>
8
9
10static VALUE nxt_ruby_stream_io_new(VALUE class, VALUE wrap);
11static VALUE nxt_ruby_stream_io_initialize(int argc, VALUE *argv, VALUE self);
12static VALUE nxt_ruby_stream_io_gets(VALUE obj, VALUE args);
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);
13static size_t nxt_ruby_stream_io_read_line(nxt_app_rmsg_t *rmsg, VALUE str);
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{
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 nxt_ruby_run_ctx_t *run_ctx;
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;
90
91 Data_Get_Struct(obj, nxt_ruby_run_ctx_t, run_ctx);
92
94
95 Data_Get_Struct(obj, nxt_ruby_run_ctx_t, run_ctx);
96
93 if (run_ctx->body_preread_size == 0) {
94 return Qnil;
95 }
97 req = run_ctx->req;
96
98
97 buf = rb_str_buf_new(1);
98
99 if (buf == Qnil) {
99 if (req->content_length == 0) {
100 return Qnil;
101 }
102
100 return Qnil;
101 }
102
103 run_ctx->body_preread_size -= nxt_ruby_stream_io_read_line(run_ctx->rmsg,
104 buf);
103 size = 0;
105
104
106 return buf;
107}
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
108
109
110static size_t
111nxt_ruby_stream_io_read_line(nxt_app_rmsg_t *rmsg, VALUE str)
112{
113 size_t len, size;
114 u_char *p;
115 nxt_buf_t *buf;
116
117 len = 0;
118
119 for (buf = rmsg->buf; buf != NULL; buf = buf->next) {
120
121 size = nxt_buf_mem_used_size(&buf->mem);
122 p = memchr(buf->mem.pos, '\n', size);
123
124 if (p != NULL) {
125 p++;
109 if (p != NULL) {
110 p++;
126 size = p - buf->mem.pos;
127
128 rb_str_cat(str, (const char *) buf->mem.pos, size);
129
130 len += size;
131 buf->mem.pos = p;
132
111 size += p - b->free;
133 break;
134 }
135
112 break;
113 }
114
136 rb_str_cat(str, (const char *) buf->mem.pos, size);
115 size += b_size;
116 }
137
117
138 len += size;
139 buf->mem.pos = buf->mem.free;
118 buf = rb_str_buf_new(size);
119
120 if (buf == Qnil) {
121 return Qnil;
140 }
141
122 }
123
142 rmsg->buf = buf;
124 size = nxt_unit_request_read(req, RSTRING_PTR(buf), size);
143
125
144 return len;
126 rb_str_set_len(buf, size);
127
128 return buf;
145}
146
147
148static VALUE
149nxt_ruby_stream_io_each(VALUE obj, VALUE args)
150{
151 VALUE chunk;
152

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

168}
169
170
171static VALUE
172nxt_ruby_stream_io_read(VALUE obj, VALUE args)
173{
174 VALUE buf;
175 long copy_size, u_size;
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;
176 size_t len;
177 nxt_ruby_run_ctx_t *run_ctx;
178
179 Data_Get_Struct(obj, nxt_ruby_run_ctx_t, run_ctx);
180
160 nxt_ruby_run_ctx_t *run_ctx;
161
162 Data_Get_Struct(obj, nxt_ruby_run_ctx_t, run_ctx);
163
181 copy_size = run_ctx->body_preread_size;
164 copy_size = run_ctx->req->content_length;
182
183 if (RARRAY_LEN(args) > 0 && TYPE(RARRAY_PTR(args)[0]) == T_FIXNUM) {
184 u_size = NUM2LONG(RARRAY_PTR(args)[0]);
185
186 if (u_size < 0 || copy_size == 0) {
187 return Qnil;
188 }
189

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

197 }
198
199 buf = rb_str_buf_new(copy_size);
200
201 if (nxt_slow_path(buf == Qnil)) {
202 return Qnil;
203 }
204
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
205 len = nxt_app_msg_read_raw(run_ctx->task, run_ctx->rmsg,
206 RSTRING_PTR(buf), (size_t) copy_size);
188 copy_size = nxt_unit_request_read(run_ctx->req, RSTRING_PTR(buf),
189 copy_size);
207
208 if (RARRAY_LEN(args) > 1 && TYPE(RARRAY_PTR(args)[1]) == T_STRING) {
209
210 rb_str_set_len(RARRAY_PTR(args)[1], 0);
211 rb_str_cat(RARRAY_PTR(args)[1], RSTRING_PTR(buf), copy_size);
212 }
213
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
214 rb_str_set_len(buf, (long) len);
197 rb_str_set_len(buf, copy_size);
215
198
216 run_ctx->body_preread_size -= len;
217
218 return buf;
219}
220
221
222static VALUE
223nxt_ruby_stream_io_rewind(VALUE obj, VALUE args)
224{
225 return Qnil;

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

271 if (TYPE(val) != T_STRING) {
272 val = rb_funcall(val, rb_intern("to_s"), 0);
273
274 if (TYPE(val) != T_STRING) {
275 return 0;
276 }
277 }
278
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
279 nxt_log_error(NXT_LOG_ERR, run_ctx->task->log, "Ruby: %s",
280 RSTRING_PTR(val));
260 nxt_unit_req_error(run_ctx->req, "Ruby: %s", RSTRING_PTR(val));
281
282 return RSTRING_LEN(val);
283}
284
285
286static VALUE
287nxt_ruby_stream_io_flush(VALUE obj, VALUE args)
288{
289 return Qnil;
290}
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}