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