@@ -135,29 +135,40 @@ NGX_MRUBY_DEFINE_METHOD_NGX_GET_REQUEST_MEMBER_STR(content_type,
135135NGX_MRUBY_DEFINE_METHOD_NGX_SET_REQUEST_MEMBER_STR (content_type ,
136136 r -> headers_out .content_type );
137137
138-
139- static mrb_value ngx_mrb_get_request_body (mrb_state * mrb , mrb_value self )
138+ static void read_request_body_cb (ngx_http_request_t * r )
140139{
141140 ngx_chain_t * cl ;
142141 size_t len ;
143142 u_char * p ;
144143 u_char * buf ;
145- ngx_http_request_t * r = ngx_mrb_get_request ( );
144+ ngx_http_mruby_ctx_t * ctx = ngx_http_get_module_ctx ( r , ngx_http_mruby_module );
146145
147- if (r -> request_body == NULL || r -> request_body -> temp_file
148- || r -> request_body -> bufs == NULL ) {
149- mrb_raise (mrb , E_RUNTIME_ERROR , "This pahse don't have request_body" );
146+ if (r -> request_body == NULL || r -> request_body -> bufs == NULL ) {
147+ ngx_log_error (NGX_LOG_ERR , r -> connection -> log , 0 ,
148+ "This pahse don't have request_body" );
149+ ngx_http_finalize_request (r , NGX_HTTP_INTERNAL_SERVER_ERROR );
150+ return ;
150151 }
151152
152153 cl = r -> request_body -> bufs ;
153154
154155 if (cl -> next == NULL ) {
155156 len = cl -> buf -> last - cl -> buf -> pos ;
156157 if (len == 0 ) {
157- return mrb_nil_value () ;
158+ return ;
158159 }
159160
160- return mrb_str_new (mrb , (const char * )cl -> buf -> pos , len );
161+ ctx -> request_body_ctx .data = cl -> buf -> pos ;
162+ ctx -> request_body_ctx .len = len ;
163+ ngx_log_error (NGX_LOG_INFO , r -> connection -> log , 0 ,
164+ "request_body(%d): %V" , len , & ctx -> request_body_ctx );
165+ if (ctx -> request_body_more ) {
166+ ctx -> request_body_more = 0 ;
167+ ngx_http_core_run_phases (r );
168+ } else {
169+ ngx_http_finalize_request (r , NGX_DONE );
170+ }
171+ return ;
161172 }
162173
163174 len = 0 ;
@@ -167,17 +178,62 @@ static mrb_value ngx_mrb_get_request_body(mrb_state *mrb, mrb_value self)
167178 }
168179
169180 if (len == 0 ) {
170- return mrb_nil_value () ;
181+ return ;
171182 }
172183
173- buf = ( u_char * ) mrb_malloc ( mrb , len );
184+ buf = ngx_palloc ( r -> pool , len );
174185
175186 p = buf ;
176187 for (cl = r -> request_body -> bufs ; cl ; cl = cl -> next ) {
177188 p = ngx_copy (p , cl -> buf -> pos , cl -> buf -> last - cl -> buf -> pos );
178189 }
179190
180- return mrb_str_new (mrb , (const char * )buf , len );
191+ ctx -> request_body_ctx .data = buf ;
192+ ctx -> request_body_ctx .len = len ;
193+ ngx_log_error (NGX_LOG_INFO , r -> connection -> log , 0 ,
194+ "multi request_body(%d): %V" , len , & ctx -> request_body_ctx );
195+ if (ctx -> request_body_more ) {
196+ ctx -> request_body_more = 0 ;
197+ ngx_http_core_run_phases (r );
198+ } else {
199+ ngx_http_finalize_request (r , NGX_DONE );
200+ }
201+ return ;
202+ }
203+
204+ static mrb_value ngx_mrb_read_request_body (mrb_state * mrb , mrb_value self )
205+ {
206+ ngx_http_request_t * r = ngx_mrb_get_request ();
207+ ngx_int_t rc ;
208+ ngx_http_mruby_ctx_t * ctx = ngx_http_get_module_ctx (r , ngx_http_mruby_module );
209+
210+ if (r -> method != NGX_HTTP_POST && r -> method != NGX_HTTP_PUT ) {
211+ mrb_raise (mrb , E_RUNTIME_ERROR , "ngx_mrb_read_request_body can't read"
212+ " when r->mehtod is neither POST nor PUT" );
213+ }
214+
215+ rc = ngx_http_read_client_request_body (r , read_request_body_cb );
216+ if (rc == NGX_ERROR || rc >= NGX_HTTP_SPECIAL_RESPONSE ) {
217+ mrb_raise (mrb , E_RUNTIME_ERROR , "ngx_http_read_client_request_body failed" );
218+ }
219+ if (rc == NGX_AGAIN ) {
220+ ctx -> request_body_more = 1 ;
221+ }
222+
223+ return mrb_fixnum_value (rc );
224+ }
225+
226+ static mrb_value ngx_mrb_get_request_body (mrb_state * mrb , mrb_value self )
227+ {
228+ ngx_http_request_t * r = ngx_mrb_get_request ();
229+ ngx_http_mruby_ctx_t * ctx = ngx_http_get_module_ctx (r , ngx_http_mruby_module );
230+
231+ if (ctx -> request_body_ctx .len != 0 ) {
232+ return mrb_str_new (mrb , (const char * )ctx -> request_body_ctx .data ,
233+ ctx -> request_body_ctx .len );
234+ } else {
235+ return mrb_nil_value ();
236+ }
181237}
182238
183239static mrb_value ngx_mrb_get_request_header (mrb_state * mrb , ngx_list_t * headers )
@@ -376,7 +432,8 @@ void ngx_mrb_request_class_init(mrb_state *mrb, struct RClass *class)
376432 struct RClass * class_headers_out ;
377433
378434 class_request = mrb_define_class_under (mrb , class , "Request" , mrb -> object_class );
379- mrb_define_method (mrb , class_request , "body" , ngx_mrb_get_request_body , MRB_ARGS_NONE ());
435+ mrb_define_method (mrb , class_request , "get_body" , ngx_mrb_get_request_body , MRB_ARGS_NONE ());
436+ mrb_define_method (mrb , class_request , "read_body" , ngx_mrb_read_request_body , MRB_ARGS_NONE ());
380437 mrb_define_method (mrb , class_request , "content_type=" , ngx_mrb_set_content_type , MRB_ARGS_ANY ());
381438 mrb_define_method (mrb , class_request , "content_type" , ngx_mrb_get_content_type , MRB_ARGS_NONE ());
382439 mrb_define_method (mrb , class_request , "request_line" , ngx_mrb_get_request_request_line , MRB_ARGS_NONE ());
0 commit comments