1:  
2:   /**
3:    * class HttpProtocol.
4:    
5:    * Pfff....c'est quoi exactement un protocol
6:    
7:    @author Laurent Dehoey I177416 
8:    @version 23/01/2003
9:    */
10:   public class HttpProtocol implements Protocol
11:   {
12:      /**
13:       * Socket of a request.
14:       */
15:      protected java.net.Socket s = null;
16:  
17:      /**
18:       * Document root.
19:       */
20:      protected static java.io.File docRoot;
21:  
22:      /**
23:       * Canonical document root.
24:       */
25:      protected static String canonicalDocRoot;
26:  
27:      /**
28:       * The port the server will listen to
29:       */
30:      public final static int HTTP_PORT = 8080;
31:  
32:      /**
33:       * CRLF
34:       */
35:      public final static String CRLF = "\r\n";
36:  
37:      /**
38:       * Protocol out server understands.
39:       */
40:      public final static String PROTOCOL = "HTTP/1.0 ";
41:  
42:      /**
43:       * Status code: All OK.
44:       */
45:      public final static String SC_OK = "200 OK";
46:  
47:      /**
48:       * Status code: Bad request.
49:       */
50:      public final static String SC_BAD_REQUEST = "400 Bad Request";
51:  
52:      /**
53:       * Status code: Forbidden request.
54:       */
55:      public final static String SC_FORBIDDEN = "403 Forbidden";
56:  
57:      /**
58:       * Status code: Resource not found.
59:       */
60:      public final static String SC_NOT_FOUND = "404 Not Found";
61:  
62:      /**
63:       * Current status code.
64:       */
65:      protected String statusCode = SC_OK;
66:  
67:      /**
68:       * Current header.
69:       */
70:      protected java.util.Hashtable myHeaders = new java.util.Hashtable();
71:  
72:       /**
73:        * Da String Constructor
74:        */
75:       public HttpProtocol(String doc_root)
76:       {
77:         try
78:         {
79:       //    this.docRoot=doc_root;
80:            docRoot = new java.io.File(".");
81:            canonicalDocRoot = docRoot.getCanonicalPath();
82:            java.net.ServerSocket listen = new java.net.ServerSocket(HTTP_PORT);
83:       //     while(true)
84:       //     {
85:       //        SimpleHttpd2 aRequest = new SimpleHttpd2(listen.accept());
86:       //     }
87:         }
88:         catch(java.io.IOException e)
89:         {
90:            System.err.println("Error: " e.toString());
91:         }
92:      
93:            setHeader("Server","SimpleHttpd2");
94:      
95:       }
96:  
97:       public void service(java.net.Socket sthrows java.lang.Exception
98:       {
99:           this.s=s;
100:  
101:         try
102:         {
103:   //         setHeader("Server","SimpleHttpd2");
104:            java.io.BufferedReader is = new java.io.BufferedReader(new java.io.InputStreamReader(s.getInputStream()));
105:            java.io.DataOutputStream os = new java.io.DataOutputStream(s.getOutputStream());
106:            String request = is.readLine();
107:            System.out.println("Request: " request);
108:            java.util.StringTokenizer st = new java.util.StringTokenizer(request);
109:            if((st.countTokens() == 3&& st.nextToken().equals("GET"))
110:            {
111:               String filename = docRoot.getPath() + st.nextToken();
112:               if(filename.endsWith("/") || filename.equals(""))
113:                  filename +"index.html";
114:               java.io.File file = new java.io.File(filename);
115:               if(file.getCanonicalPath().startsWith(canonicalDocRoot))
116:                  sendDocument(os,file);
117:               else
118:                  sendError(SC_FORBIDDEN,os);
119:            }
120:            else
121:            {
122:               sendError(SC_BAD_REQUEST,os);
123:            }
124:            is.close();
125:            os.close();
126:            s.close();
127:         }
128:         catch(java.io.IOException ioe)
129:         {
130:            System.err.println("Error: " ioe.toString());
131:         }
132:      
133:  
134:  
135:  
136:  
137:       }
138:  
139:  
140:      /**
141:       * Reads the file, specified in <code>request</code> and writes it to
142:       * the OutputStream.<br>
143:       * If the file could not be found, the error message 404,
144:       * <code>Not Found</code>, is returned.
145:       *
146:       @exception IOException in case writing to the <code>DataOutputStream</code>
147:       *    fails.
148:       @param os Stream, where the requested object is to be copied to.
149:       @param file file to copy.
150:       */
151:      protected void sendDocument(java.io.DataOutputStream osjava.io.File filethrows java.io.IOException
152:      {
153:         try
154:         {
155:            java.io.BufferedInputStream in = new java.io.BufferedInputStream(new java.io.FileInputStream(file));
156:            sendStatusLine(os);
157:            setHeader("Content-Length",(new Long(file.length())).toString());
158:            setHeader("Content-Type",guessType(file.getPath()));
159:            sendHeader(os);
160:            os.writeBytes(CRLF);
161:            byte[] buf = new byte[1024];
162:            int len;
163:            while((len = in.read(buf,0,1024)) !-1)
164:            {
165:               os.write(buf,0,len);
166:            }
167:            in.close();
168:         }
169:         catch(java.io.FileNotFoundException fnfe)
170:         {
171:            sendError(SC_NOT_FOUND,os);
172:         }
173:      }
174:  
175:      /**
176:       * Sets a status code.
177:       *
178:       @param statusCode status code
179:       */
180:      protected void setStatusCode(String statusCode)
181:      {
182:         this.statusCode = statusCode;
183:      }
184:  
185:      /**
186:       * Gets the status code.
187:       *
188:       @return status code
189:       */
190:      protected String getStatusCode()
191:      {
192:         return statusCode;
193:      }
194:  
195:      /**
196:       * Writes the status line to the consigned <code>DataOutputStream</code>.
197:       *
198:       @param out DataOutputStream where the line is to be written.
199:       @exception IOException in case writing fails.
200:       */
201:      protected void sendStatusLine(java.io.DataOutputStream outthrows java.io.IOException
202:      {
203:         out.writeBytes(PROTOCOL getStatusCode() + CRLF);
204:      }
205:  
206:      /**
207:       * Sets an header value.
208:       *
209:       @param key key of the header value.
210:       @param value the header value.
211:       */
212:      protected void setHeader(String keyString value)
213:      {
214:         myHeaders.put(key,value);
215:      }
216:  
217:      /**
218:       * Writes the header to the consigned <code>DataOutputStream</code>.
219:       *
220:       @param out DataOutputStream where the header is to be written.
221:       @exception IOException in case writing fails.
222:       */
223:      protected void sendHeader(java.io.DataOutputStream outthrows java.io.IOException
224:      {
225:         String line;
226:         String key;
227:         java.util.Enumeration e = myHeaders.keys();
228:         while(e.hasMoreElements())
229:         {
230:            key (String)e.nextElement();
231:            out.writeBytes(key ": " myHeaders.get(key) + CRLF);
232:         }
233:      }
234:  
235:      /**
236:       * Writes an error message to the consigned <code>DataOutputStream</code>.
237:       *
238:       @param out DataOutputStream where the error message is to be written.
239:       @param statusCode status code.
240:       @exception IOException in case writing fails.
241:       */
242:      protected void sendError(String statusCodejava.io.DataOutputStream outthrows java.io.IOException
243:      {
244:         setStatusCode(statusCode);
245:         sendStatusLine(out);
246:         out.writeBytes(CRLF "<html>" "<head><title>" getStatusCode() + "</title></head>" "<body><h1>" getStatusCode() + "</h1></body>" "</html>");
247:         System.err.println(getStatusCode());
248:      }
249:  
250:      /**
251:       * Surmise the <code>Content-Type</code> of the file by means
252:       * of the file extension.
253:       *
254:       @param filename file name
255:       @return Content-Type or "unknown/unknown" in case no
256:       *   appropriate type is found.
257:       *   
258:       *   **always unknown for the moment**
259:       */
260:      public String guessType(String filename)
261:      {
262:         String type = null;
263:         int i = filename.lastIndexOf(".");
264:   //      if(i > 0)
265:   //         type = typeMap.getProperty(filename.substring(i));
266:   //     if(type == null)
267:            type = "unknown/unknown";
268:         return type;
269:      }
270:  
271:  
272:   }

This page was automatically generated by SharpDevelop.