4 class ServerThread extends Thread
8 ServerThread (httpd the_server)
13 synchronized void wait_till_finished ()
21 System.out.println("Exception " + exc + " in wait");
25 boolean is_info_line (String str)
27 return str.startsWith("User-Agent: ") || str.startsWith("Host: ")
28 || str.startsWith("Accept: ");
31 static void write_string (OutputStream os, String str) throws IOException
33 byte buf[] = new byte[str.length()];
35 str.getBytes(0, str.length(), buf, 0);
39 synchronized void handle_request (Socket socket)
41 System.out.println("begin handle_request");
45 InputStream is = socket.getInputStream();
46 DataInputStream dis = new DataInputStream(is);
49 request = dis.readLine();
50 if (request.startsWith("GET "))
52 String doc = request.substring(4);
54 System.out.println("request: " + doc);
56 boolean is_10 = false;
58 if (doc.endsWith(" HTTP/1.0"))
66 info = dis.readLine();
67 } while (is_info_line(info));
69 doc = doc.substring(0, doc.length() - 9);
72 String filename = httpd.document_root + doc;
73 File file = new File(filename);
75 if (file.isDirectory())
76 file = new File(filename + "/index.html");
77 if (file.exists() && file.isFile() && file.canRead())
79 OutputStream os = socket.getOutputStream();
81 System.out.println("kurde 1");
83 FileInputStream fis = new FileInputStream(file);
85 System.out.println("kurde 2");
86 System.out.println("kurde 2a");
88 byte buffer[] = new byte[8192];
89 byte heusl[] = new byte[8192];
91 System.out.println("kurde 3");
96 //+ "Content-Length: " + file.length() + "\n"
97 + "Content-Type: text/html\n"
98 + "Server: Schani's Kurden-Server\n"
101 System.out.println("begin copy");
107 result = fis.read(buffer);
109 os.write(buffer, 0, result);
110 } while (result != -1);
111 System.out.println("end copy");
116 write_string(socket.getOutputStream(),
117 "HTTP/1.0 404 Not Found\n"
121 System.out.println("malformed request: " + request);
123 catch (Exception exc)
125 System.out.println("Exception " + exc + " in thread");
130 InputStream is = socket.getInputStream();
133 while ((len = is.available()) > 0)
135 for (int i = 0; i < len; ++i)
141 catch (Exception exc)
143 System.out.println("Exception " + exc + " in close");
146 server.thread_ready(this);
149 System.out.println("end handle_request");
156 static String document_root;
157 static int num_threads = 10;
159 ServerThread threads[] = null;
160 boolean threads_ready[] = null;
164 threads = new ServerThread[num_threads];
165 threads_ready = new boolean[num_threads];
167 for (int i = 0; i < num_threads; ++i)
169 threads[i] = new ServerThread(this);
170 threads_ready[i] = true;
175 ServerSocket ss = new ServerSocket(8001, 5);
179 Socket socket = ss.accept();
180 ServerThread thread = get_ready_thread();
182 thread.handle_request(socket);
185 catch (Exception exc)
187 System.out.println("Exception " + exc);
191 public static void main (String args[])
193 document_root=args[0];
197 synchronized void thread_ready (ServerThread thread)
199 for (int i = 0; i < num_threads; ++i)
200 if (threads[i] == thread)
202 threads_ready[i] = true;
207 ServerThread get_ready_thread ()
209 for (int i = 0; i < num_threads; ++i)
210 if (threads_ready[i])
212 threads_ready[i] = false;
216 threads[0].wait_till_finished();
217 threads_ready[0] = false;