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   {
91     int position = filename.lastIndexOf('#');
92     if (position != -1)
93       filename = filename.substring(0, position - 1);
94       
95     File f      = new File(filename);
96     String name = f.getName();         // name w/o directory
97
98     position = name.lastIndexOf('.');
99     
100     String contentType;
101
102     if (position == -1)  // if no extension, txt is assigned by default
103         contentType = "txt";    
104     else  
105         contentType = name.substring(position + 1);
106     
107     return (String) ct.get(contentType);
108   } 
109
110 }