1/*
2 * Copyright (c) 2009, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.  Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26package sun.net.www.protocol.http.logging;
27
28import java.util.logging.LogRecord;
29import java.util.regex.*;
30
31/**
32 * A Formatter to make the HTTP logs a bit more palatable to the developer
33 * looking at them. The idea is to present the HTTP events in such a way that
34 * commands and headers are easily spotted (i.e. on separate lines).
35 * @author jccollet
36 */
37public class HttpLogFormatter extends java.util.logging.SimpleFormatter {
38    // Pattern for MessageHeader data. Mostly pairs within curly brackets
39    private static volatile Pattern pattern = null;
40    // Pattern for Cookies
41    private static volatile Pattern cpattern = null;
42
43    public HttpLogFormatter() {
44        if (pattern == null) {
45            pattern = Pattern.compile("\\{[^\\}]*\\}");
46            cpattern = Pattern.compile("[^,\\] ]{2,}");
47        }
48    }
49
50    @Override
51    public String format(LogRecord record) {
52        String sourceClassName = record.getSourceClassName();
53        if (sourceClassName == null ||
54            !(sourceClassName.startsWith("sun.net.www.protocol.http") ||
55              sourceClassName.startsWith("sun.net.www.http"))) {
56            return super.format(record);
57        }
58        String src = record.getMessage();
59        StringBuilder buf = new StringBuilder("HTTP: ");
60        if (src.startsWith("sun.net.www.MessageHeader@")) {
61            // MessageHeader logs are composed of pairs within curly brackets
62            // Let's extract them to make it more readable. That way we get one
63            // header pair (name, value) per line. A lot easier to read.
64            Matcher match = pattern.matcher(src);
65            while (match.find()) {
66                int i = match.start();
67                int j = match.end();
68                String s = src.substring(i + 1, j - 1);
69                if (s.startsWith("null: ")) {
70                    s = s.substring(6);
71                }
72                if (s.endsWith(": null")) {
73                    s = s.substring(0, s.length() - 6);
74                }
75                buf.append("\t").append(s).append("\n");
76            }
77        } else if (src.startsWith("Cookies retrieved: {")) {
78            // This comes from the Cookie handler, let's clean up the format a bit
79            String s = src.substring(20);
80            buf.append("Cookies from handler:\n");
81            while (s.length() >= 7) {
82                if (s.startsWith("Cookie=[")) {
83                    String s2 = s.substring(8);
84                    int c = s2.indexOf("Cookie2=[");
85                    if (c > 0) {
86                        s2 = s2.substring(0, c-1);
87                        s = s2.substring(c);
88                    } else {
89                        s = "";
90                    }
91                    if (s2.length() < 4) {
92                        continue;
93                    }
94                    Matcher m = cpattern.matcher(s2);
95                    while (m.find()) {
96                        int i = m.start();
97                        int j = m.end();
98                        if (i >= 0) {
99                            String cookie = s2.substring(i + 1, j > 0 ? j - 1 : s2.length() - 1);
100                            buf.append("\t").append(cookie).append("\n");
101                        }
102                    }
103                }
104                if (s.startsWith("Cookie2=[")) {
105                    String s2 = s.substring(9);
106                    int c = s2.indexOf("Cookie=[");
107                    if (c > 0) {
108                        s2 = s2.substring(0, c-1);
109                        s = s2.substring(c);
110                    } else {
111                        s = "";
112                    }
113                    Matcher m = cpattern.matcher(s2);
114                    while (m.find()) {
115                        int i = m.start();
116                        int j = m.end();
117                        if (i >= 0) {
118                            String cookie = s2.substring(i+1, j > 0 ? j-1 : s2.length() - 1);
119                            buf.append("\t").append(cookie).append("\n");
120                        }
121                    }
122                }
123            }
124        } else {
125            // Anything else we let as is.
126            buf.append(src).append("\n");
127        }
128        return buf.toString();
129    }
130
131}
132