2 //****************************************************************************
3 // Programmer: Duane M. Gran, ragnar@cs.bsu.edu
4 // Program: JhttpServer
5 // Date: April 24, 1998
6 //****************************************************************************
13 //****************************************************************************
14 // Class: httpResponse
15 // Purpose: constructs the header to be returned by the server
16 //****************************************************************************
18 public class HTTPHeader {
20 // make a hashtable of return codes to messages
21 static private HashStrings rc = new HashStrings();
25 rc.put("403", "Fobidden");
26 rc.put("404", "Not found");
27 rc.put("501", "Method not implemented");
30 // hashtable of content type matchings
31 static private HashStrings ct = new HashStrings(); // p. 817
34 ct.put("txt", "text/plain");
35 ct.put("text", "text/plain");
36 ct.put("log", "text/plain");
37 ct.put("htm", "text/html");
38 ct.put("html", "text/html");
39 ct.put("gif", "image/gif");
40 ct.put("jpg", "image/jpg");
41 ct.put("jpeg", "image/jpg");
42 ct.put("jpe", "image/jpg");
43 ct.put("mpg", "video/mpeg");
44 ct.put("mpeg", "video/mpeg");
45 ct.put("mpe", "video/mpeg");
46 ct.put("qt", "video/quicktime");
47 ct.put("mov", "video/quicktime");
48 ct.put("au", "audio/basic");
49 ct.put("snd", "audio/basic");
50 ct.put("wav", "audio/x-wave");
51 ct.put("class", "application/octet-stream");
52 ct.put("ps", "application/postscript");
55 //*************************************************************************
56 // Constructor: send_header(int, String, int)
57 // Purpose: Send an HTTP header
58 //*************************************************************************
60 static public void send_header(OutputStream out, int returnCode,
61 String filename, long fileLength) {
62 String contentType = getContentTypeFor(filename);
63 String returnString = (String) rc.get(String.valueOf(returnCode));
66 header = "HTTP/1.0 " + returnCode + " " + returnString + "\n" +
67 "Date: " + "1/1/01" + "\n" + // date
69 "Allow: GET\n" + // allowed methods
70 "MIME-Version: 1.0\n" + // mime version
71 "Server : SpinWeb Custom HTTP Server\n" + // server type
72 "Content-Type: " + contentType + "\n" + // type
73 "Content-Length: "+ fileLength + "\n\n"; // length
75 out.write(header.getBytes());
76 } catch(IOException e) {
77 e.printStackTrace(); // do nothing!
81 //*************************************************************************
82 // Method: getContentTypeFor(String)
83 // Purpose: Looks up the content type (MIME) in a hashtable for the given
84 // file suffix. It removes any anchors (#) in case the string is
85 // a URL and then operates on the name without path.
86 //*************************************************************************
88 static private String getContentTypeFor(String filename) {
89 int position = filename.lastIndexOf('#');
91 filename = filename.substring(0, position - 1);
93 File f = new File(filename);
94 String name = f.getName(); // name w/o directory
96 position = name.lastIndexOf('.');
100 if (position == -1) // if no extension, txt is assigned by default
103 contentType = name.substring(position + 1);
105 return (String) ct.get(contentType);