View | Details | Raw Unified | Return to bug 60161
Collapse All | Expand All

(-)java/org/apache/catalina/valves/rewrite/RewriteValve.java (-20 / +40 lines)
Lines 47-52 Link Here
47
import org.apache.catalina.connector.Response;
47
import org.apache.catalina.connector.Response;
48
import org.apache.catalina.util.URLEncoder;
48
import org.apache.catalina.util.URLEncoder;
49
import org.apache.catalina.valves.ValveBase;
49
import org.apache.catalina.valves.ValveBase;
50
import org.apache.juli.logging.Log;
51
import org.apache.juli.logging.LogFactory;
50
import org.apache.tomcat.util.buf.CharChunk;
52
import org.apache.tomcat.util.buf.CharChunk;
51
import org.apache.tomcat.util.buf.MessageBytes;
53
import org.apache.tomcat.util.buf.MessageBytes;
52
import org.apache.tomcat.util.buf.UriUtil;
54
import org.apache.tomcat.util.buf.UriUtil;
Lines 91-96 Link Here
91
        // Add '/' so it isn't encoded when we encode a path
93
        // Add '/' so it isn't encoded when we encode a path
92
        ENCODER.addSafeCharacter('/');
94
        ENCODER.addSafeCharacter('/');
93
    }
95
    }
96
    
97
    /*
98
     * httpd supports configurable logging from level 0-9 for debugging
99
     * Added similar logging support for the RewriteValve to replicate httpd behaviour
100
     * Log in DEBUG and TRACE levels
101
     */
102
    
103
    private static final Log log = LogFactory.getLog(RewriteValve.class);
94
104
95
105
96
    /**
106
    /**
Lines 155-165 Link Here
155
            context = true;
165
            context = true;
156
            is = ((Context) getContainer()).getServletContext()
166
            is = ((Context) getContainer()).getServletContext()
157
                .getResourceAsStream("/WEB-INF/" + resourcePath);
167
                .getResourceAsStream("/WEB-INF/" + resourcePath);
158
            if (container.getLogger().isDebugEnabled()) {
168
            if (log.isDebugEnabled()) {
159
                if (is == null) {
169
                if (is == null) {
160
                    container.getLogger().debug("No configuration resource found: /WEB-INF/" + resourcePath);
170
                    log.debug("No configuration resource found: /WEB-INF/" + resourcePath);
161
                } else {
171
                } else {
162
                    container.getLogger().debug("Read configuration from: /WEB-INF/" + resourcePath);
172
                    log.debug("Read configuration from: /WEB-INF/" + resourcePath);
163
                }
173
                }
164
            }
174
            }
165
        } else if (getContainer() instanceof Host) {
175
        } else if (getContainer() instanceof Host) {
Lines 170-190 Link Here
170
                    // Use getResource and getResourceAsStream
180
                    // Use getResource and getResourceAsStream
171
                    is = getClass().getClassLoader()
181
                    is = getClass().getClassLoader()
172
                        .getResourceAsStream(resourceName);
182
                        .getResourceAsStream(resourceName);
173
                    if (is != null && container.getLogger().isDebugEnabled()) {
183
                    if (is != null && log.isDebugEnabled()) {
174
                        container.getLogger().debug("Read configuration from CL at " + resourceName);
184
                        log.debug("Read configuration from CL at " + resourceName);
175
                    }
185
                    }
176
                } else {
186
                } else {
177
                    if (container.getLogger().isDebugEnabled()) {
187
                    if (log.isDebugEnabled()) {
178
                        container.getLogger().debug("Read configuration from " + file.getAbsolutePath());
188
                        log.debug("Read configuration from " + file.getAbsolutePath());
179
                    }
189
                    }
180
                    is = new FileInputStream(file);
190
                    is = new FileInputStream(file);
181
                }
191
                }
182
                if ((is == null) && (container.getLogger().isDebugEnabled())) {
192
                if ((is == null) && (log.isDebugEnabled())) {
183
                    container.getLogger().debug("No configuration resource found: " + resourceName +
193
                    log.debug("No configuration resource found: " + resourceName +
184
                            " in " + getConfigBase() + " or in the classloader");
194
                            " in " + getConfigBase() + " or in the classloader");
185
                }
195
                }
186
            } catch (Exception e) {
196
            } catch (Exception e) {
187
                container.getLogger().error("Error opening configuration", e);
197
                log.error("Error opening configuration", e);
188
            }
198
            }
189
        }
199
        }
190
200
Lines 197-208 Link Here
197
                BufferedReader reader = new BufferedReader(isr)) {
207
                BufferedReader reader = new BufferedReader(isr)) {
198
            parse(reader);
208
            parse(reader);
199
        } catch (IOException ioe) {
209
        } catch (IOException ioe) {
200
            container.getLogger().error("Error closing configuration", ioe);
210
            log.error("Error closing configuration", ioe);
201
        } finally {
211
        } finally {
202
            try {
212
            try {
203
                is.close();
213
                is.close();
204
            } catch (IOException e) {
214
            } catch (IOException e) {
205
                container.getLogger().error("Error closing configuration", e);
215
                log.error("Error closing configuration", e);
206
            }
216
            }
207
        }
217
        }
208
218
Lines 238-245 Link Here
238
                Object result = parse(line);
248
                Object result = parse(line);
239
                if (result instanceof RewriteRule) {
249
                if (result instanceof RewriteRule) {
240
                    RewriteRule rule = (RewriteRule) result;
250
                    RewriteRule rule = (RewriteRule) result;
241
                    if (container.getLogger().isDebugEnabled()) {
251
                    if (log.isDebugEnabled()) {
242
                        container.getLogger().debug("Add rule with pattern " + rule.getPatternString()
252
                        log.debug("Add rule with pattern " + rule.getPatternString()
243
                                + " and substitution " + rule.getSubstitutionString());
253
                                + " and substitution " + rule.getSubstitutionString());
244
                    }
254
                    }
245
                    for (int i = (conditions.size() - 1); i > 0; i--) {
255
                    for (int i = (conditions.size() - 1); i > 0; i--) {
Lines 248-256 Link Here
248
                        }
258
                        }
249
                    }
259
                    }
250
                    for (int i = 0; i < conditions.size(); i++) {
260
                    for (int i = 0; i < conditions.size(); i++) {
251
                        if (container.getLogger().isDebugEnabled()) {
261
                        if (log.isDebugEnabled()) {
252
                            RewriteCond cond = conditions.get(i);
262
                            RewriteCond cond = conditions.get(i);
253
                            container.getLogger().debug("Add condition " + cond.getCondPattern()
263
                            log.debug("Add condition " + cond.getCondPattern()
254
                                    + " test " + cond.getTestString() + " to rule with pattern "
264
                                    + " test " + cond.getTestString() + " to rule with pattern "
255
                                    + rule.getPatternString() + " and substitution "
265
                                    + rule.getPatternString() + " and substitution "
256
                                    + rule.getSubstitutionString() + (cond.isOrnext() ? " [OR]" : "")
266
                                    + rule.getSubstitutionString() + (cond.isOrnext() ? " [OR]" : "")
Lines 271-277 Link Here
271
                    }
281
                    }
272
                }
282
                }
273
            } catch (IOException e) {
283
            } catch (IOException e) {
274
                container.getLogger().error("Error reading configuration", e);
284
                log.error("Error reading configuration", e);
275
            }
285
            }
276
        }
286
        }
277
        this.rules = rules.toArray(new RewriteRule[0]);
287
        this.rules = rules.toArray(new RewriteRule[0]);
Lines 300-305 Link Here
300
    @Override
310
    @Override
301
    public void invoke(Request request, Response response)
311
    public void invoke(Request request, Response response)
302
        throws IOException, ServletException {
312
        throws IOException, ServletException {
313
    	
314
    	long startTime = System.nanoTime();
303
315
304
        if (!getEnabled() || rules == null || rules.length == 0) {
316
        if (!getEnabled() || rules == null || rules.length == 0) {
305
            getNext().invoke(request, response);
317
            getNext().invoke(request, response);
Lines 325-330 Link Here
325
            // converted to a string
337
            // converted to a string
326
            String uriEncoding = request.getConnector().getURIEncoding();
338
            String uriEncoding = request.getConnector().getURIEncoding();
327
            String originalQueryStringEncoded = request.getQueryString();
339
            String originalQueryStringEncoded = request.getQueryString();
340
            String originalRequestURI = null;
328
            MessageBytes urlMB =
341
            MessageBytes urlMB =
329
                    context ? request.getRequestPathMB() : request.getDecodedRequestURIMB();
342
                    context ? request.getRequestPathMB() : request.getDecodedRequestURIMB();
330
            urlMB.toChars();
343
            urlMB.toChars();
Lines 337-346 Link Here
337
                RewriteRule rule = rules[i];
350
                RewriteRule rule = rules[i];
338
                CharSequence test = (rule.isHost()) ? host : urlDecoded;
351
                CharSequence test = (rule.isHost()) ? host : urlDecoded;
339
                CharSequence newtest = rule.evaluate(test, resolver);
352
                CharSequence newtest = rule.evaluate(test, resolver);
353
                if (log.isTraceEnabled()) {
354
                	log.trace("applying pattern '"+rule.getPatternString()+"' to uri '"+test+"'");
355
                }
340
                if (newtest != null && !test.equals(newtest.toString())) {
356
                if (newtest != null && !test.equals(newtest.toString())) {
341
                    if (container.getLogger().isDebugEnabled()) {
357
                    originalRequestURI = test.toString();
342
                        container.getLogger().debug("Rewrote " + test + " as " + newtest
358
                    if (log.isDebugEnabled()) {
343
                                + " with rule pattern " + rule.getPatternString());
359
                        log.debug("pattern '"+rule.getPatternString()+"' matches uri '"+test+"'");
344
                    }
360
                    }
345
                    if (rule.isHost()) {
361
                    if (rule.isHost()) {
346
                        host = newtest;
362
                        host = newtest;
Lines 564-569 Link Here
564
                        // This doesn't actually happen in the Catalina adapter implementation
580
                        // This doesn't actually happen in the Catalina adapter implementation
565
                    }
581
                    }
566
                }
582
                }
583
                if(log.isDebugEnabled()) {
584
                	log.debug("Rewrote '" + originalRequestURI + "' -> '" + urlDecoded
585
                				+" in " + ((System.nanoTime() - startTime)/1000000)+" ms");
586
                }	
567
            } else {
587
            } else {
568
                getNext().invoke(request, response);
588
                getNext().invoke(request, response);
569
            }
589
            }

Return to bug 60161