/Users/lyon/j4p/src/net/web/WebMapper.java
|
1 package net.web;
2
3 import java.io.FileReader;
4 import java.io.IOException;
5 import java.io.InputStreamReader;
6
7 public class WebMapper {
8 public static void main(String args[]) {
9
10 WebMapper ws = new WebMapper(8080);
11 }
12
13 public WebMapper(int port) {
14
15 try {
16 java.net.ServerSocket ss
17 = new java.net.ServerSocket(port);
18 while (true) {
19 startClient(ss.accept());
20 }
21 } catch (IOException e) {
22 e.printStackTrace();
23 }
24
25 }
26
27 public void startClient(java.net.Socket s)
28 throws IOException {
29 ClientThread
30 ct = new ClientThread(s);
31 Thread t = new Thread(ct);
32 t.start();
33 }
34
35
36 class ClientThread implements
37 Runnable {
38 private java.net.Socket s;
39 private java.io.BufferedReader br;
40 private java.io.PrintWriter pw;
41 private static final String
42 notFoundString =
43 "HTTP/1.0 501 Not Implemented\n"
44 + "Content-type: text/plain\n\n";
45 private static final String
46 okString =
47 "HTTP/1.0 200 OK\n"
48 + "Content-type: text/html\n\n";
49
50
51 ClientThread(java.net.Socket _s) {
52 s = _s;
53 try {
54 br =
55 new java.io.BufferedReader(
56 new InputStreamReader(
57 s.getInputStream()));
58 pw =
59 new java.io.PrintWriter(
60 s.getOutputStream(), true);
61 } catch (IOException e) {
62 e.printStackTrace();
63 }
64 }
65
66 public void run() {
67 try {
68 String line =
69 br.readLine();
70 java.util.StringTokenizer
71 st =
72 new java.util.StringTokenizer(
73 line);
74 System.out.println(line);
75 if (st.nextToken().equals("GET"))
76 //countTo10();
77 getAUrl(st);
78 else
79 pw.println(notFoundString);
80 pw.close();
81 s.close();
82 } catch (Exception e) {
83 }
84 }
85
86 public void countTo10() {
87 pw.println("HTTP/1.0 200 OK");
88 pw.println("Content-type: text/plain");
89 pw.println();
90 for (int i = 0; i < 1000; i++)
91 pw.println("i=" + i);
92 }
93
94 public String stripSlash(String s) {
95 int i = s.indexOf('/');
96 if (i < 0) return s;
97 return s.substring(i + 1);
98 }
99
100 public void getAUrl(java.util.StringTokenizer st)
101 throws java.io.FileNotFoundException,
102 IOException {
103 pw.println(okString);
104 String fileName
105 = st.nextToken();
106 pw.println("date=" + new java.util.Date());
107 fileName = stripSlash(fileName);
108 pw.println("url=" + fileName);
109 net.proxy.Proxy.setSoeProxy();
110 String s[] = UrlUtils.getUrlString(fileName);
111 for (int i = 0; i < s.length; i++)
112 pw.println(s[i]);
113 //String fileLine = null;
114 //while (
115 // (fileLine =
116 // fileReader.readLine())
117 // != null)
118 // pw.println(fileLine);
119 }
120
121 public void getAFile(java.util.StringTokenizer st)
122 throws java.io.FileNotFoundException,
123 IOException {
124 pw.println(okString);
125 String fileName
126 = st.nextToken();
127 pw.println("date=" + new java.util.Date());
128 java.io.BufferedReader
129 fileReader = new
130 java.io.BufferedReader(new
131 FileReader(
132 "c:\\lyon\\www" + fileName));
133 String fileLine = null;
134 while (
135 (fileLine =
136 fileReader.readLine())
137 != null)
138 pw.println(fileLine);
139 }
140 }
141 }
142