This commit was manufactured by cvs2svn to create tag 'buildscript'.
[IRC.git] /
1 package Interface;
2 //****************************************************************************
3 // Programmer: Duane M. Gran, ragnar@cs.bsu.edu
4 // Program:    JhttpServer
5 // Date:       April 24, 1998
6 //****************************************************************************
7
8
9 import java.net.*;
10 import java.util.*;
11 import java.io.*;
12
13 //****************************************************************************
14 // Class:   httpResponse
15 // Purpose: constructs the header to be returned by the server
16 //****************************************************************************
17
18 public class HTTPHeader {
19
20   // make a hashtable of return codes to messages
21   static private HashStrings rc = new HashStrings();
22   static
23   {
24     rc.put("200", "OK");
25     rc.put("403", "Fobidden");
26     rc.put("404", "Not found");
27     rc.put("501", "Method not implemented");
28   }
29
30   // hashtable of content type matchings
31   static private HashStrings ct = new HashStrings();   // p. 817
32   static
33   {
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");
53   }
54
55 //*************************************************************************
56 // Constructor: send_header(int, String, int)
57 // Purpose:     Send an HTTP header
58 //*************************************************************************
59
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));
64     String header;
65
66     header = "HTTP/1.0 " + returnCode + " " + returnString + "\n" +
67              "Date: " + "1/1/01" + "\n" +            // date
68              "Expires: 1/1/00\n"+
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
74     try{
75       out.write(header.getBytes());
76     }
77     catch(IOException e){
78       e.printStackTrace();     // do nothing!
79     }
80   }
81
82 //*************************************************************************
83 // Method:  getContentTypeFor(String)
84 // Purpose: Looks up the content type (MIME) in a hashtable for the given
85 //          file suffix.  It removes any anchors (#) in case the string is
86 //          a URL and then operates on the name without path.
87 //*************************************************************************
88
89   static private String getContentTypeFor(String filename) {
90     int position = filename.lastIndexOf('#');
91     if (position != -1)
92       filename = filename.substring(0, position - 1);
93
94     File f      = new File(filename);
95     String name = f.getName();         // name w/o directory
96
97     position = name.lastIndexOf('.');
98
99     String contentType;
100
101     if (position == -1)  // if no extension, txt is assigned by default
102       contentType = "txt";
103     else
104       contentType = name.substring(position + 1);
105
106     return (String) ct.get(contentType);
107   }
108
109 }