@@ -656,8 +656,41 @@ static ngx_int_t ngx_http_clojure_postconfiguration(ngx_conf_t *cf) {
656656
657657static void ngx_http_clojure_client_body_handler (ngx_http_request_t * r ) {
658658 ngx_http_clojure_loc_conf_t * lcf = ngx_http_get_module_loc_conf (r , ngx_http_clojure_module );
659- int rc = ngx_http_clojure_eval (lcf -> handler_id , r );
660- ngx_http_finalize_request (r , rc );
659+ ngx_http_clojure_module_ctx_t * ctx = ngx_http_get_module_ctx (r , ngx_http_clojure_module );
660+ ngx_int_t handler_id ;
661+ int rc = NGX_DECLINED ;
662+ int rewrite_phase = 0 ;
663+ ctx -> client_body_done = 1 ;
664+
665+ if (!ctx -> async_body_read ) {
666+ ctx -> phrase = -1 ;
667+ return ;
668+ }
669+
670+ ctx -> async_body_read = 0 ;
671+
672+ if (ctx -> phrase == NGX_HTTP_REWRITE_PHASE ) {
673+ handler_id = lcf -> rewrite_handler_id ;
674+ rewrite_phase = 1 ;
675+ r -> write_event_handler = ngx_http_core_run_phases ;
676+ }else {
677+ handler_id = lcf -> handler_id ;
678+ }
679+
680+ if (handler_id > 0 ) {
681+ rc = ngx_http_clojure_eval (handler_id , r );
682+ ngx_log_debug2 (NGX_LOG_DEBUG_HTTP , ngx_http_clojure_global_cycle -> log , 0 , "ngx clojure rewrite (body done callback) request: %" PRIu64 ", rc: %d" , (jlong )(uintptr_t )r , rc );
683+ }
684+
685+ if (rewrite_phase ) {
686+ r -> main -> count -- ;
687+ if (rc != NGX_DONE ) {
688+ ctx -> phrase = ~ctx -> phrase ;
689+ r -> write_event_handler (r );
690+ }
691+ }else {
692+ ngx_http_finalize_request (r , rc );
693+ }
661694}
662695
663696
@@ -706,21 +739,24 @@ static ngx_int_t ngx_http_clojure_handler(ngx_http_request_t * r) {
706739 r -> request_body_in_single_buf = 1 ;
707740 r -> request_body_in_clean_file = 1 ;
708741 r -> request_body_in_persistent_file = 1 ;
742+
709743 rc = ngx_http_read_client_request_body (r , ngx_http_clojure_client_body_handler );
710- if (rc >= NGX_HTTP_SPECIAL_RESPONSE ) {
744+ if (rc == NGX_ERROR || rc >= NGX_HTTP_SPECIAL_RESPONSE ) {
711745 return rc ;
712746 }
713- return NGX_DONE ;
747+
748+ if (rc == NGX_AGAIN ) {
749+ ctx -> async_body_read = 1 ;
750+ return NGX_DONE ;
751+ }
714752 }else {
715753 rc = ngx_http_discard_request_body (r );
716754 if (rc != NGX_OK && rc != NGX_AGAIN ) {
717755 return rc ;
718756 }
719- rc = ngx_http_clojure_eval (lcf -> handler_id , r );
720757 }
721- //for debug
722- //ngx_log_error(NGX_LOG_DEBUG, r->connection->log, 0, "finished one request \n==============================================================\n\n");
723758
759+ rc = ngx_http_clojure_eval (lcf -> handler_id , r );
724760 return rc ;
725761}
726762
@@ -731,13 +767,43 @@ static ngx_int_t ngx_http_clojure_rewrite_handler(ngx_http_request_t * r) {
731767 ngx_http_clojure_module_ctx_t * ctx ;
732768 ngx_http_clojure_loc_conf_t * lcf = ngx_http_get_module_loc_conf (r , ngx_http_clojure_module );
733769
734- if (!lcf -> enable || (lcf -> rewrite_handler_code .len == 0 && lcf -> rewrite_handler_name .len == 0 )) {
735- return NGX_DECLINED ;
770+ if (lcf -> enable && (lcf -> rewrite_handler_code .len > 0 || lcf -> rewrite_handler_name .len > 0 )) {
771+ rc = ngx_http_clojure_init_clojure_script ("rewrite handler" , & lcf -> handler_type , & lcf -> rewrite_handler_name ,
772+ & lcf -> rewrite_handler_code , & lcf -> rewrite_handler_id , ngx_http_clojure_global_cycle -> log );
773+ if (rc != NGX_HTTP_CLOJURE_JVM_OK ) {
774+ return rc ;
775+ }
736776 }
737777
738- rc = ngx_http_clojure_init_clojure_script ("rewrite handler" , & lcf -> handler_type , & lcf -> rewrite_handler_name , & lcf -> rewrite_handler_code , & lcf -> rewrite_handler_id , ngx_http_clojure_global_cycle -> log );
739- if (rc != NGX_HTTP_CLOJURE_JVM_OK ) {
740- return rc ;
778+ if (lcf -> always_read_body ) {
779+ if ((ctx = ngx_http_get_module_ctx (r , ngx_http_clojure_module )) == NULL ) {
780+ ctx = ngx_palloc (r -> pool , sizeof (ngx_http_clojure_module_ctx_t ));
781+ if (ctx == NULL ) {
782+ ngx_log_error (NGX_LOG_ERR , r -> connection -> log , 0 , "OutOfMemory of create ngx_http_clojure_module_ctx_t" );
783+ return NGX_HTTP_INTERNAL_SERVER_ERROR ;
784+ }
785+
786+ ngx_http_clojure_init_ctx (ctx , NGX_HTTP_REWRITE_PHASE );
787+ ngx_http_set_ctx (r , ctx , ngx_http_clojure_module );
788+
789+ if (!ctx -> client_body_done ) {
790+ r -> request_body_in_single_buf = 1 ;
791+ r -> request_body_in_clean_file = 1 ;
792+ r -> request_body_in_persistent_file = 1 ;
793+ rc = ngx_http_read_client_request_body (r , ngx_http_clojure_client_body_handler );
794+ if (rc >= NGX_HTTP_SPECIAL_RESPONSE || rc == NGX_ERROR ) {
795+ return rc ;
796+ }
797+ if (rc == NGX_AGAIN ) {
798+ ctx -> async_body_read = 1 ;
799+ return NGX_DONE ;
800+ }
801+ }
802+ }
803+ }
804+
805+ if (!lcf -> enable || (lcf -> rewrite_handler_code .len == 0 && lcf -> rewrite_handler_name .len == 0 )) {
806+ return NGX_DECLINED ;
741807 }
742808
743809 if ((ctx = ngx_http_get_module_ctx (r , ngx_http_clojure_module )) == NULL ) {
0 commit comments