1 /* |
|
2 * Copyright (c) 1997, 2013, 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 |
|
26 |
|
27 /* |
|
28 * The Original Code is HAT. The Initial Developer of the |
|
29 * Original Code is Bill Foote, with contributions from others |
|
30 * at JavaSoft/Sun. |
|
31 */ |
|
32 |
|
33 package com.sun.tools.hat.internal.server; |
|
34 |
|
35 /** |
|
36 * Reads a single HTTP query from a socket, and starts up a QueryHandler |
|
37 * to server it. |
|
38 * |
|
39 * @author Bill Foote |
|
40 */ |
|
41 |
|
42 |
|
43 import java.net.Socket; |
|
44 |
|
45 import java.io.InputStream; |
|
46 import java.io.BufferedInputStream; |
|
47 import java.io.IOException; |
|
48 import java.io.BufferedWriter; |
|
49 import java.io.PrintWriter; |
|
50 import java.io.OutputStreamWriter; |
|
51 |
|
52 import com.sun.tools.hat.internal.model.Snapshot; |
|
53 import com.sun.tools.hat.internal.oql.OQLEngine; |
|
54 import com.sun.tools.hat.internal.util.Misc; |
|
55 |
|
56 public class HttpReader implements Runnable { |
|
57 |
|
58 |
|
59 private Socket socket; |
|
60 private PrintWriter out; |
|
61 private Snapshot snapshot; |
|
62 private OQLEngine engine; |
|
63 |
|
64 public HttpReader (Socket s, Snapshot snapshot, OQLEngine engine) { |
|
65 this.socket = s; |
|
66 this.snapshot = snapshot; |
|
67 this.engine = engine; |
|
68 } |
|
69 |
|
70 public void run() { |
|
71 InputStream in = null; |
|
72 try { |
|
73 in = new BufferedInputStream(socket.getInputStream()); |
|
74 out = new PrintWriter(new BufferedWriter( |
|
75 new OutputStreamWriter( |
|
76 socket.getOutputStream()))); |
|
77 out.println("HTTP/1.0 200 OK"); |
|
78 out.println("Cache-Control: no-cache"); |
|
79 out.println("Pragma: no-cache"); |
|
80 out.println(); |
|
81 if (in.read() != 'G' || in.read() != 'E' |
|
82 || in.read() != 'T' || in.read() != ' ') { |
|
83 outputError("Protocol error"); |
|
84 } |
|
85 int data; |
|
86 StringBuilder queryBuf = new StringBuilder(); |
|
87 while ((data = in.read()) != -1 && data != ' ') { |
|
88 char ch = (char) data; |
|
89 queryBuf.append(ch); |
|
90 } |
|
91 String query = queryBuf.toString(); |
|
92 query = java.net.URLDecoder.decode(query, "UTF-8"); |
|
93 QueryHandler handler = null; |
|
94 if (snapshot == null) { |
|
95 outputError("The heap snapshot is still being read."); |
|
96 return; |
|
97 } else if (query.equals("/")) { |
|
98 handler = new AllClassesQuery(true, engine != null); |
|
99 handler.setUrlStart(""); |
|
100 handler.setQuery(""); |
|
101 } else if (query.startsWith("/oql/")) { |
|
102 if (engine != null) { |
|
103 handler = new OQLQuery(engine); |
|
104 handler.setUrlStart(""); |
|
105 handler.setQuery(query.substring(5)); |
|
106 } |
|
107 } else if (query.startsWith("/oqlhelp/")) { |
|
108 if (engine != null) { |
|
109 handler = new OQLHelp(); |
|
110 handler.setUrlStart(""); |
|
111 handler.setQuery(""); |
|
112 } |
|
113 } else if (query.equals("/allClassesWithPlatform/")) { |
|
114 handler = new AllClassesQuery(false, engine != null); |
|
115 handler.setUrlStart("../"); |
|
116 handler.setQuery(""); |
|
117 } else if (query.equals("/showRoots/")) { |
|
118 handler = new AllRootsQuery(); |
|
119 handler.setUrlStart("../"); |
|
120 handler.setQuery(""); |
|
121 } else if (query.equals("/showInstanceCounts/includePlatform/")) { |
|
122 handler = new InstancesCountQuery(false); |
|
123 handler.setUrlStart("../../"); |
|
124 handler.setQuery(""); |
|
125 } else if (query.equals("/showInstanceCounts/")) { |
|
126 handler = new InstancesCountQuery(true); |
|
127 handler.setUrlStart("../"); |
|
128 handler.setQuery(""); |
|
129 } else if (query.startsWith("/instances/")) { |
|
130 handler = new InstancesQuery(false); |
|
131 handler.setUrlStart("../"); |
|
132 handler.setQuery(query.substring(11)); |
|
133 } else if (query.startsWith("/newInstances/")) { |
|
134 handler = new InstancesQuery(false, true); |
|
135 handler.setUrlStart("../"); |
|
136 handler.setQuery(query.substring(14)); |
|
137 } else if (query.startsWith("/allInstances/")) { |
|
138 handler = new InstancesQuery(true); |
|
139 handler.setUrlStart("../"); |
|
140 handler.setQuery(query.substring(14)); |
|
141 } else if (query.startsWith("/allNewInstances/")) { |
|
142 handler = new InstancesQuery(true, true); |
|
143 handler.setUrlStart("../"); |
|
144 handler.setQuery(query.substring(17)); |
|
145 } else if (query.startsWith("/object/")) { |
|
146 handler = new ObjectQuery(); |
|
147 handler.setUrlStart("../"); |
|
148 handler.setQuery(query.substring(8)); |
|
149 } else if (query.startsWith("/class/")) { |
|
150 handler = new ClassQuery(); |
|
151 handler.setUrlStart("../"); |
|
152 handler.setQuery(query.substring(7)); |
|
153 } else if (query.startsWith("/roots/")) { |
|
154 handler = new RootsQuery(false); |
|
155 handler.setUrlStart("../"); |
|
156 handler.setQuery(query.substring(7)); |
|
157 } else if (query.startsWith("/allRoots/")) { |
|
158 handler = new RootsQuery(true); |
|
159 handler.setUrlStart("../"); |
|
160 handler.setQuery(query.substring(10)); |
|
161 } else if (query.startsWith("/reachableFrom/")) { |
|
162 handler = new ReachableQuery(); |
|
163 handler.setUrlStart("../"); |
|
164 handler.setQuery(query.substring(15)); |
|
165 } else if (query.startsWith("/rootStack/")) { |
|
166 handler = new RootStackQuery(); |
|
167 handler.setUrlStart("../"); |
|
168 handler.setQuery(query.substring(11)); |
|
169 } else if (query.startsWith("/histo/")) { |
|
170 handler = new HistogramQuery(); |
|
171 handler.setUrlStart("../"); |
|
172 handler.setQuery(query.substring(7)); |
|
173 } else if (query.startsWith("/refsByType/")) { |
|
174 handler = new RefsByTypeQuery(); |
|
175 handler.setUrlStart("../"); |
|
176 handler.setQuery(query.substring(12)); |
|
177 } else if (query.startsWith("/finalizerSummary/")) { |
|
178 handler = new FinalizerSummaryQuery(); |
|
179 handler.setUrlStart("../"); |
|
180 handler.setQuery(""); |
|
181 } else if (query.startsWith("/finalizerObjects/")) { |
|
182 handler = new FinalizerObjectsQuery(); |
|
183 handler.setUrlStart("../"); |
|
184 handler.setQuery(""); |
|
185 } |
|
186 |
|
187 if (handler != null) { |
|
188 handler.setOutput(out); |
|
189 handler.setSnapshot(snapshot); |
|
190 handler.run(); |
|
191 } else { |
|
192 outputError("Query '" + query + "' not implemented"); |
|
193 } |
|
194 } catch (IOException ex) { |
|
195 ex.printStackTrace(); |
|
196 } finally { |
|
197 if (out != null) { |
|
198 out.close(); |
|
199 } |
|
200 try { |
|
201 if (in != null) { |
|
202 in.close(); |
|
203 } |
|
204 } catch (IOException ignored) { |
|
205 } |
|
206 try { |
|
207 socket.close(); |
|
208 } catch (IOException ignored) { |
|
209 } |
|
210 } |
|
211 } |
|
212 |
|
213 private void outputError(String msg) { |
|
214 out.println(); |
|
215 out.println("<html><body bgcolor=\"#ffffff\">"); |
|
216 out.println(Misc.encodeHtml(msg)); |
|
217 out.println("</body></html>"); |
|
218 } |
|
219 |
|
220 } |
|