* configure.ac: New switch for disabling -O2 (--disable-optimizations).
[cacao.git] / tests / network / httpd / httpd.java
1 import java.net.*;
2 import java.io.*;
3
4 class ServerThread extends Thread
5 {
6     httpd server;
7
8     ServerThread (httpd the_server)
9     {
10         server = the_server;
11     }
12
13     synchronized void wait_till_finished ()
14     {
15         try
16         {
17             wait();
18         }
19         catch (Exception exc)
20         {
21             System.out.println("Exception " + exc + " in wait");
22         }
23     }
24
25     boolean is_info_line (String str)
26     {
27         return str.startsWith("User-Agent: ") || str.startsWith("Host: ")
28             || str.startsWith("Accept: ");
29     }
30
31     static void write_string (OutputStream os, String str) throws IOException
32     {
33         byte buf[] = new byte[str.length()];
34
35         str.getBytes(0, str.length(), buf, 0);
36         os.write(buf);
37     }
38
39     synchronized void handle_request (Socket socket)
40     {
41         System.out.println("begin handle_request");
42
43         try
44         {
45             InputStream is = socket.getInputStream();
46             DataInputStream dis = new DataInputStream(is);
47             String request;
48
49             request = dis.readLine();
50             if (request.startsWith("GET "))
51             {
52                 String doc = request.substring(4);
53
54                 System.out.println("request: " + doc);
55
56                 boolean is_10 = false;
57
58                 if (doc.endsWith(" HTTP/1.0"))
59                 {
60                     is_10 = true;
61                     
62                     String info;
63
64                     do
65                     {
66                         info = dis.readLine();
67                     } while (is_info_line(info));
68
69                     doc = doc.substring(0, doc.length() - 9);
70                 }
71
72                 String filename = httpd.document_root + doc;
73                 File file = new File(filename);
74
75                 if (file.isDirectory())
76                     file = new File(filename + "/index.html");
77                 if (file.exists() && file.isFile() && file.canRead())
78                 {
79                     OutputStream os = socket.getOutputStream();
80
81                     System.out.println("kurde 1");
82
83                     FileInputStream fis = new FileInputStream(file);
84
85                     System.out.println("kurde 2");
86                     System.out.println("kurde 2a");
87
88                     byte buffer[] = new byte[8192];
89                     byte heusl[] = new byte[8192];
90
91                     System.out.println("kurde 3");
92
93                     if (is_10)
94                         write_string(os,
95                                      "HTTP/1.0 200 OK\n"
96                                      //+ "Content-Length: " + file.length() + "\n"
97                                      + "Content-Type: text/html\n"
98                                      + "Server: Schani's Kurden-Server\n"
99                                      + "\n");
100
101                     System.out.println("begin copy");
102
103                     int result;
104
105                     do
106                     {
107                         result = fis.read(buffer);
108                         if (result > 0)
109                             os.write(buffer, 0, result);
110                     } while (result != -1);
111                     System.out.println("end copy");
112                     
113                     fis.close();
114                 }
115                 else if (is_10)
116                     write_string(socket.getOutputStream(),
117                                  "HTTP/1.0 404 Not Found\n"
118                                  + "\n");
119             }
120             else
121                 System.out.println("malformed request: " + request);
122         }
123         catch (Exception exc)
124         {
125             System.out.println("Exception " + exc + " in thread");
126         }
127
128         try
129         {
130             InputStream is = socket.getInputStream();
131             int len;
132
133             while ((len = is.available()) > 0)
134             {
135                 for (int i = 0; i < len; ++i)
136                     is.read();
137             }
138
139             socket.close();
140         }
141         catch (Exception exc)
142         {
143             System.out.println("Exception " + exc + " in close");
144         }
145
146         server.thread_ready(this);
147         notify();
148
149         System.out.println("end handle_request");
150     }
151 }
152
153 public class httpd
154 {
155
156     static String document_root;
157     static int num_threads = 10;
158
159     ServerThread threads[] = null;
160     boolean threads_ready[] = null;
161
162     void run ()
163     {
164         threads = new ServerThread[num_threads];
165         threads_ready = new boolean[num_threads];
166
167         for (int i = 0; i < num_threads; ++i)
168         {
169             threads[i] = new ServerThread(this);
170             threads_ready[i] = true;
171         }
172
173         try
174         {
175             ServerSocket ss = new ServerSocket(8001, 5);
176
177             while (true)
178             {
179                 Socket socket = ss.accept();
180                 ServerThread thread = get_ready_thread();
181
182                 thread.handle_request(socket);
183             }
184         }
185         catch (Exception exc)
186         {
187             System.out.println("Exception " + exc);
188         }
189     }
190
191     public static void main (String args[])
192     {
193         document_root=args[0];
194         new httpd().run();
195     }
196
197     synchronized void thread_ready (ServerThread thread)
198     {
199         for (int i = 0; i < num_threads; ++i)
200             if (threads[i] == thread)
201             {
202                 threads_ready[i] = true;
203                 break;
204             }
205     }
206
207     ServerThread get_ready_thread ()
208     {
209         for (int i = 0; i < num_threads; ++i)
210             if (threads_ready[i])
211             {
212                 threads_ready[i] = false;
213                 return threads[i];
214             }
215
216         threads[0].wait_till_finished();
217         threads_ready[0] = false;
218         return threads[0];
219     }
220 }