@@ -60,13 +60,15 @@ module AP_MODULE_DECLARE_DATA redirectionio_module = {
6060};
6161
6262static void redirectionio_register_hooks (apr_pool_t * p ) {
63- ap_hook_type_checker (redirectionio_match_handler , NULL , NULL , APR_HOOK_LAST );
63+ ap_hook_type_checker (redirectionio_match_handler , NULL , NULL , APR_HOOK_FIRST );
64+ ap_hook_fixups (redirectionio_match_handler , NULL , NULL , APR_HOOK_FIRST );
6465
6566 ap_hook_handler (redirectionio_redirect_handler , NULL , NULL , APR_HOOK_FIRST );
6667 ap_hook_log_transaction (redirectionio_log_handler , NULL , NULL , APR_HOOK_MIDDLE );
6768
6869 ap_hook_insert_filter (ap_headers_insert_output_filter , NULL , NULL , APR_HOOK_LAST );
6970 ap_hook_insert_error_filter (ap_headers_insert_output_filter , NULL , NULL , APR_HOOK_LAST );
71+
7072 ap_register_output_filter ("redirectionio_redirect_filter" , redirectionio_filter_match_on_response , NULL , AP_FTYPE_CONTENT_SET );
7173 ap_register_output_filter ("redirectionio_header_filter" , redirectionio_filter_header_filtering , NULL , AP_FTYPE_CONTENT_SET );
7274 ap_register_output_filter ("redirectionio_body_filter" , redirectionio_filter_body_filtering , NULL , AP_FTYPE_CONTENT_SET );
@@ -117,7 +119,7 @@ static int redirectionio_match_handler(request_rec *r) {
117119
118120 redirectionio_release_connection (conn , config , r -> pool );
119121
120- return APR_SUCCESS ;
122+ return DECLINED ;
121123}
122124
123125static void ap_headers_insert_output_filter (request_rec * r ) {
@@ -132,17 +134,9 @@ static void ap_headers_insert_output_filter(request_rec *r) {
132134 return ;
133135 }
134136
135- if (ctx -> status != 0 && ctx -> match_on_response_status != 0 && !ctx -> is_redirected && r -> status == ctx -> match_on_response_status ) {
136- ap_add_output_filter ("redirectionio_redirect_filter" , ctx , r , r -> connection );
137- }
138-
139- if (ctx -> should_filter_headers == 1 ) {
140- ap_add_output_filter ("redirectionio_header_filter" , ctx , r , r -> connection );
141- }
142-
143- if (ctx -> should_filter_body == 1 ) {
144- ap_add_output_filter ("redirectionio_body_filter" , ctx , r , r -> connection );
145- }
137+ ap_add_output_filter ("redirectionio_redirect_filter" , ctx , r , r -> connection );
138+ ap_add_output_filter ("redirectionio_header_filter" , ctx , r , r -> connection );
139+ ap_add_output_filter ("redirectionio_body_filter" , ctx , r , r -> connection );
146140}
147141
148142static int redirectionio_redirect_handler (request_rec * r ) {
@@ -155,6 +149,10 @@ static int redirectionio_redirect_handler(request_rec *r) {
155149
156150 redirectionio_context * ctx = ap_get_module_config (r -> request_config , & redirectionio_module );
157151
152+ if (ctx == NULL ) {
153+ return DECLINED ;
154+ }
155+
158156 // No match here
159157 if (ctx -> status == 0 || ctx -> is_redirected == 1 ) {
160158 return DECLINED ;
@@ -177,6 +175,14 @@ static int redirectionio_redirect_handler(request_rec *r) {
177175static apr_status_t redirectionio_filter_match_on_response (ap_filter_t * f , apr_bucket_brigade * bb ) {
178176 redirectionio_context * ctx = (redirectionio_context * )f -> ctx ;
179177
178+ if (ctx == NULL ) {
179+ return ap_pass_brigade (f -> next , bb );
180+ }
181+
182+ if (ctx -> is_redirected || ctx -> status == 0 || (ctx -> match_on_response_status > 0 && ctx -> match_on_response_status != f -> r -> status )) {
183+ return ap_pass_brigade (f -> next , bb );
184+ }
185+
180186 if (ctx -> status != 410 ) {
181187 apr_table_setn (f -> r -> headers_out , "Location" , ctx -> target );
182188 }
@@ -194,20 +200,28 @@ static apr_status_t redirectionio_filter_header_filtering(ap_filter_t *f, apr_bu
194200 redirectionio_context * ctx = (redirectionio_context * )f -> ctx ;
195201 redirectionio_config * config = (redirectionio_config * ) ap_get_module_config (f -> r -> per_dir_config , & redirectionio_module );
196202
203+ if (ctx == NULL ) {
204+ return ap_pass_brigade (f -> next , bb );
205+ }
206+
207+ if (ctx -> should_filter_headers == 0 || (ctx -> is_redirected != 1 && ctx -> match_on_response_status > 0 && ctx -> match_on_response_status != f -> r -> status )) {
208+ return ap_pass_brigade (f -> next , bb );
209+ }
210+
197211 ap_remove_output_filter (f );
198212
199213 // Get connection
200214 redirectionio_connection * conn = redirectionio_acquire_connection (config , f -> r -> pool );
201215
202216 if (conn == NULL ) {
203- return ap_pass_brigade (f -> next , bb );;
217+ return ap_pass_brigade (f -> next , bb );
204218 }
205219
206220 // Send headers
207221 if (redirectionio_protocol_send_filter_headers (conn , ctx , f -> r , config -> project_key ) != APR_SUCCESS ) {
208222 redirectionio_invalidate_connection (conn , config , f -> r -> pool );
209223
210- return ap_pass_brigade (f -> next , bb );;
224+ return ap_pass_brigade (f -> next , bb );
211225 }
212226
213227 redirectionio_release_connection (conn , config , f -> r -> pool );
@@ -224,6 +238,14 @@ static apr_status_t redirectionio_filter_body_filtering(ap_filter_t *f, apr_buck
224238 int64_t input_size , output_size ;
225239 apr_status_t rv ;
226240
241+ if (ctx == NULL ) {
242+ return ap_pass_brigade (f -> next , bb );
243+ }
244+
245+ if (ctx -> should_filter_body == 0 || (ctx -> is_redirected != 1 && ctx -> match_on_response_status > 0 && ctx -> match_on_response_status != f -> r -> status )) {
246+ return ap_pass_brigade (f -> next , bb );
247+ }
248+
227249 // If first -> remove content_length, get_connection, init filtering command
228250 if (ctx -> body_filter_conn == NULL ) {
229251 ctx -> body_filter_conn = redirectionio_acquire_connection (config , f -> r -> pool );
0 commit comments