/** * This file contains example code from the book * JAVA NETWORKING AND COMMUNICATIONS * by Todd Courtois * ISBN 0-13-850454-7 * * Purchasers of the book may reuse this sample code * for any purpose whatsoever as long as this header * is included. Use of this example code is subject * to the license agreement printed in the book. * * For updates see the web site * http://www.rawthought.com/JNC/ */ /** * Modified by Michael Hicks for CMSC 433, * University of Maryland, 2002. */ import java.io.*; import java.net.*; import java.util.*; /** * An object encapsulating an HTTP GET request, for 1.0 or 1.1. * Ignores the MIME headers. */ public class HttpGetRequestSimple extends HttpRequest { public static boolean fDebugOn = true; public HttpGetRequestSimple(String request, String version, String type) { super(request,version,type); } /** * Read an HTTP request and return an HttpRequest object on success, * or NULL on failure. On failure, the proper response will be sent * to the output stream. All of the MIME headers are ignored. * * @param clientInputStream The stream where the client sends us * its request * @param clientOutputStream The stream where we return content to * the client */ public static HttpRequest readRequest(InputStream clientInputStream, OutputStream clientOutputStream) { /* Parse the first line of the request, getting the filename * and HTTP version. Then call the subhandler's parsing routine. */ String path; String version = ""; try { //gets first line, should contain //"GET foo.html HTTP/1.0" (or 1.1) DataInputStream in = new DataInputStream(clientInputStream); String firstLineStr = in.readLine(); if (fDebugOn) System.out.println("read line "+firstLineStr); StringTokenizer tokSource = new StringTokenizer(firstLineStr); String curTok = tokSource.nextToken(); if (fDebugOn) System.out.println("curTok: " + curTok); // make sure it's a GET request if (curTok.equals("GET")) { curTok = tokSource.nextToken(); path = curTok; //now get the HTTP version version = tokSource.nextToken(); if (fDebugOn) System.out.println("curTok: " + version); if (!version.substring(0,4).equals("HTTP")) { if (fDebugOn) System.out.println("bad request: expecting HTTP/ver"); path = null; } } else { if (fDebugOn) System.out.println("bad request: expecting GET"); path = null; } } catch (Exception ex) { if (fDebugOn) System.out.println("bad request: got exception "+ex); path = null; } /* Return the request or null if an error, sending a message * to the client as well. */ if (path != null) { // strip off leading / if one exists while (path.length() != 0 && path.charAt(0) == '/') { path = path.substring(1); } return new HttpGetRequestSimple(path, version, "GET"); } else { HttpResponse err = HttpResponse.errorResponse("mangled request"); err.sendResponse(clientOutputStream); return null; } } }