nxt_ruby_stream_io.c (1398:05063d6eec8e) nxt_ruby_stream_io.c (1687:b9d99e596725)
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
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);
11static VALUE nxt_ruby_stream_io_new(VALUE class, VALUE arg);
12static VALUE nxt_ruby_stream_io_initialize(int argc, VALUE *argv, VALUE self);
13static VALUE nxt_ruby_stream_io_gets(VALUE obj);
14static VALUE nxt_ruby_stream_io_each(VALUE obj);
15static VALUE nxt_ruby_stream_io_read(VALUE obj, VALUE args);
16static VALUE nxt_ruby_stream_io_rewind(VALUE obj);
17static VALUE nxt_ruby_stream_io_puts(VALUE obj, VALUE args);
18static VALUE nxt_ruby_stream_io_write(VALUE obj, VALUE args);
12static VALUE nxt_ruby_stream_io_initialize(int argc, VALUE *argv, VALUE self);
13static VALUE nxt_ruby_stream_io_gets(VALUE obj);
14static VALUE nxt_ruby_stream_io_each(VALUE obj);
15static VALUE nxt_ruby_stream_io_read(VALUE obj, VALUE args);
16static VALUE nxt_ruby_stream_io_rewind(VALUE obj);
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);
19nxt_inline long nxt_ruby_stream_io_s_write(nxt_ruby_ctx_t *rctx, VALUE val);
21static VALUE nxt_ruby_stream_io_flush(VALUE obj);
22
23
24VALUE
25nxt_ruby_stream_io_input_init(void)
26{
27 VALUE stream_io;
28

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

58 rb_define_method(stream_io, "write", nxt_ruby_stream_io_write, -2);
59 rb_define_method(stream_io, "flush", nxt_ruby_stream_io_flush, 0);
60
61 return stream_io;
62}
63
64
65static VALUE
20static VALUE nxt_ruby_stream_io_flush(VALUE obj);
21
22
23VALUE
24nxt_ruby_stream_io_input_init(void)
25{
26 VALUE stream_io;
27

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

57 rb_define_method(stream_io, "write", nxt_ruby_stream_io_write, -2);
58 rb_define_method(stream_io, "flush", nxt_ruby_stream_io_flush, 0);
59
60 return stream_io;
61}
62
63
64static VALUE
66nxt_ruby_stream_io_new(VALUE class, VALUE wrap)
65nxt_ruby_stream_io_new(VALUE class, VALUE arg)
67{
66{
68 VALUE self;
69 nxt_ruby_run_ctx_t *run_ctx;
67 VALUE self;
70
68
71 Data_Get_Struct(wrap, nxt_ruby_run_ctx_t, run_ctx);
72 self = Data_Wrap_Struct(class, 0, 0, run_ctx);
69 self = Data_Wrap_Struct(class, 0, 0, (void *) (uintptr_t) arg);
73
74 rb_obj_call_init(self, 0, NULL);
75
76 return self;
77}
78
79
80static VALUE
81nxt_ruby_stream_io_initialize(int argc, VALUE *argv, VALUE self)
82{
83 return self;
84}
85
86
87static VALUE
88nxt_ruby_stream_io_gets(VALUE obj)
89{
90 VALUE buf;
91 ssize_t res;
70
71 rb_obj_call_init(self, 0, NULL);
72
73 return self;
74}
75
76
77static VALUE
78nxt_ruby_stream_io_initialize(int argc, VALUE *argv, VALUE self)
79{
80 return self;
81}
82
83
84static VALUE
85nxt_ruby_stream_io_gets(VALUE obj)
86{
87 VALUE buf;
88 ssize_t res;
92 nxt_ruby_run_ctx_t *run_ctx;
89 nxt_ruby_ctx_t *rctx;
93 nxt_unit_request_info_t *req;
94
90 nxt_unit_request_info_t *req;
91
95 Data_Get_Struct(obj, nxt_ruby_run_ctx_t, run_ctx);
92 Data_Get_Struct(obj, nxt_ruby_ctx_t, rctx);
93 req = rctx->req;
96
94
97 req = run_ctx->req;
98
99 if (req->content_length == 0) {
100 return Qnil;
101 }
102
103 res = nxt_unit_request_readline_size(req, SSIZE_MAX);
104 if (nxt_slow_path(res < 0)) {
105 return Qnil;
106 }

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

140
141 return Qnil;
142}
143
144
145static VALUE
146nxt_ruby_stream_io_read(VALUE obj, VALUE args)
147{
95 if (req->content_length == 0) {
96 return Qnil;
97 }
98
99 res = nxt_unit_request_readline_size(req, SSIZE_MAX);
100 if (nxt_slow_path(res < 0)) {
101 return Qnil;
102 }

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

136
137 return Qnil;
138}
139
140
141static VALUE
142nxt_ruby_stream_io_read(VALUE obj, VALUE args)
143{
148 VALUE buf;
149 long copy_size, u_size;
150 nxt_ruby_run_ctx_t *run_ctx;
144 VALUE buf;
145 long copy_size, u_size;
146 nxt_ruby_ctx_t *rctx;
151
147
152 Data_Get_Struct(obj, nxt_ruby_run_ctx_t, run_ctx);
148 Data_Get_Struct(obj, nxt_ruby_ctx_t, rctx);
153
149
154 copy_size = run_ctx->req->content_length;
150 copy_size = rctx->req->content_length;
155
156 if (RARRAY_LEN(args) > 0 && TYPE(RARRAY_PTR(args)[0]) == T_FIXNUM) {
157 u_size = NUM2LONG(RARRAY_PTR(args)[0]);
158
159 if (u_size < 0 || copy_size == 0) {
160 return Qnil;
161 }
162

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

170 }
171
172 buf = rb_str_buf_new(copy_size);
173
174 if (nxt_slow_path(buf == Qnil)) {
175 return Qnil;
176 }
177
151
152 if (RARRAY_LEN(args) > 0 && TYPE(RARRAY_PTR(args)[0]) == T_FIXNUM) {
153 u_size = NUM2LONG(RARRAY_PTR(args)[0]);
154
155 if (u_size < 0 || copy_size == 0) {
156 return Qnil;
157 }
158

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

166 }
167
168 buf = rb_str_buf_new(copy_size);
169
170 if (nxt_slow_path(buf == Qnil)) {
171 return Qnil;
172 }
173
178 copy_size = nxt_unit_request_read(run_ctx->req, RSTRING_PTR(buf),
179 copy_size);
174 copy_size = nxt_unit_request_read(rctx->req, RSTRING_PTR(buf), copy_size);
180
181 if (RARRAY_LEN(args) > 1 && TYPE(RARRAY_PTR(args)[1]) == T_STRING) {
182
183 rb_str_set_len(RARRAY_PTR(args)[1], 0);
184 rb_str_cat(RARRAY_PTR(args)[1], RSTRING_PTR(buf), copy_size);
185 }
186
187 rb_str_set_len(buf, copy_size);

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

195{
196 return Qnil;
197}
198
199
200static VALUE
201nxt_ruby_stream_io_puts(VALUE obj, VALUE args)
202{
175
176 if (RARRAY_LEN(args) > 1 && TYPE(RARRAY_PTR(args)[1]) == T_STRING) {
177
178 rb_str_set_len(RARRAY_PTR(args)[1], 0);
179 rb_str_cat(RARRAY_PTR(args)[1], RSTRING_PTR(buf), copy_size);
180 }
181
182 rb_str_set_len(buf, copy_size);

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

190{
191 return Qnil;
192}
193
194
195static VALUE
196nxt_ruby_stream_io_puts(VALUE obj, VALUE args)
197{
203 nxt_ruby_run_ctx_t *run_ctx;
198 nxt_ruby_ctx_t *rctx;
204
205 if (RARRAY_LEN(args) != 1) {
206 return Qnil;
207 }
208
199
200 if (RARRAY_LEN(args) != 1) {
201 return Qnil;
202 }
203
209 Data_Get_Struct(obj, nxt_ruby_run_ctx_t, run_ctx);
204 Data_Get_Struct(obj, nxt_ruby_ctx_t, rctx);
210
205
211 nxt_ruby_stream_io_s_write(run_ctx, RARRAY_PTR(args)[0]);
206 nxt_ruby_stream_io_s_write(rctx, RARRAY_PTR(args)[0]);
212
213 return Qnil;
214}
215
216
217static VALUE
218nxt_ruby_stream_io_write(VALUE obj, VALUE args)
219{
207
208 return Qnil;
209}
210
211
212static VALUE
213nxt_ruby_stream_io_write(VALUE obj, VALUE args)
214{
220 long len;
221 nxt_ruby_run_ctx_t *run_ctx;
215 long len;
216 nxt_ruby_ctx_t *rctx;
222
223 if (RARRAY_LEN(args) != 1) {
224 return Qnil;
225 }
226
217
218 if (RARRAY_LEN(args) != 1) {
219 return Qnil;
220 }
221
227 Data_Get_Struct(obj, nxt_ruby_run_ctx_t, run_ctx);
222 Data_Get_Struct(obj, nxt_ruby_ctx_t, rctx);
228
223
229 len = nxt_ruby_stream_io_s_write(run_ctx, RARRAY_PTR(args)[0]);
224 len = nxt_ruby_stream_io_s_write(rctx, RARRAY_PTR(args)[0]);
230
231 return LONG2FIX(len);
232}
233
234
235nxt_inline long
225
226 return LONG2FIX(len);
227}
228
229
230nxt_inline long
236nxt_ruby_stream_io_s_write(nxt_ruby_run_ctx_t *run_ctx, VALUE val)
231nxt_ruby_stream_io_s_write(nxt_ruby_ctx_t *rctx, VALUE val)
237{
238 if (nxt_slow_path(val == Qnil)) {
239 return 0;
240 }
241
242 if (TYPE(val) != T_STRING) {
243 val = rb_funcall(val, rb_intern("to_s"), 0);
244
245 if (TYPE(val) != T_STRING) {
246 return 0;
247 }
248 }
249
232{
233 if (nxt_slow_path(val == Qnil)) {
234 return 0;
235 }
236
237 if (TYPE(val) != T_STRING) {
238 val = rb_funcall(val, rb_intern("to_s"), 0);
239
240 if (TYPE(val) != T_STRING) {
241 return 0;
242 }
243 }
244
250 nxt_unit_req_error(run_ctx->req, "Ruby: %s", RSTRING_PTR(val));
245 nxt_unit_req_error(rctx->req, "Ruby: %s", RSTRING_PTR(val));
251
252 return RSTRING_LEN(val);
253}
254
255
256static VALUE
257nxt_ruby_stream_io_flush(VALUE obj)
258{
259 return Qnil;
260}
246
247 return RSTRING_LEN(val);
248}
249
250
251static VALUE
252nxt_ruby_stream_io_flush(VALUE obj)
253{
254 return Qnil;
255}