/Users/lyon/j4p/src/net/web/BinaryWebServer.java
|
1 package net.web;
2
3 import java.io.*;
4 import java.net.Socket;
5 import java.net.URL;
6
7 public class BinaryWebServer {
8 public static void main(String args[]) {
9 net.proxy.Proxy.setSoeProxy();
10 new BinaryWebServer(80);
11 }
12 public BinaryWebServer(int port) {
13
14 try {
15 java.net.ServerSocket ss
16 = new java.net.ServerSocket(port);
17 while (true) {
18 new PortMappingService(ss.accept());
19 }
20 } catch (IOException e) {
21 e.printStackTrace();
22 }
23 }
24 }
25
26 class BinaryThreadedService implements
27 Runnable {
28 private Socket clientsSocket;
29 private BufferedReader socketsBufferedReader;
30 private OutputStream socketsOutputStream;
31 private static final String
32 notFoundString =
33 "HTTP/1.0 501 Not Implemented\n"
34 + "Content-type: text/plain\n";
35 private static final String
36 okString =
37 "HTTP/1.0 200 OK\n"
38 + "Content-type: text/html\n";
39 private static final String
40 gifString =
41 "HTTP/1.0 200 OK\n"
42 + "Content-type: image/gif\n";
43 private static final String
44 auString =
45 "HTTP/1.0 200 OK\n"
46 + "Content-type: sound/au\n";
47
48
49
50 BinaryThreadedService(Socket _s) {
51
52 clientsSocket = _s;
53 try {
54 socketsBufferedReader =
55 new BufferedReader(
56 new InputStreamReader(
57 clientsSocket.getInputStream()));
58 socketsOutputStream = clientsSocket.getOutputStream();
59 } catch (IOException e) {
60 e.printStackTrace();
61 }
62 Thread t = new Thread(this);
63 t.start();
64 }
65
66 public void run() {
67 try {
68 String line =
69 socketsBufferedReader.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 //getATextFile(st);
78 //getABinaryFile(st);
79 getABinaryUrl(st);
80 else
81 println(notFoundString);
82 socketsOutputStream.close();
83 clientsSocket.close();
84 } catch (Exception e) {
85 }
86 }
87
88 public void println() {
89 println("");
90 }
91
92 public void println(String s) {
93 s = s + "\n";
94 byte b[] = s.getBytes();
95 try {
96 socketsOutputStream.write(b);
97 } catch (IOException e) {
98 e.printStackTrace();
99 }
100
101 }
102
103 public void countTo10() {
104 println("HTTP/1.0 200 OK");
105 println("Content-type: text/plain");
106 println("");
107 for (int i = 0; i < 100000; i++)
108 println("i=" + i);
109 }
110
111 public void getATextFile(java.util.StringTokenizer st)
112 throws FileNotFoundException,
113 IOException {
114 println(okString);
115 String fileName
116 = st.nextToken();
117 println("date=" + new java.util.Date());
118 BufferedReader
119 fileReader = new
120 BufferedReader(new
121 FileReader(
122 "c:\\lyon\\www" + fileName));
123 String fileLine = null;
124 while (
125 (fileLine =
126 fileReader.readLine())
127 != null)
128 println(fileLine);
129 }
130
131 public void getABinaryUrl(java.util.StringTokenizer st)
132 throws FileNotFoundException,
133 IOException {
134 String urlName
135 = st.nextToken();
136 urlName = "http:/"+urlName;
137 //Vector v = UrlUtils.getUrl(urlName);
138 // System.out.println("urlName="+urlName);
139 URL url = new URL(urlName);
140 InputStream
141 urlIs = url.openStream();
142 byte b [] = new byte[1024];
143 int numberRead = -1;
144 while ( (numberRead = urlIs.read(b))>0)
145 socketsOutputStream.write(b);
146 }
147
148 public void getABinaryFile(java.util.StringTokenizer st)
149 throws FileNotFoundException,
150 IOException {
151 println(gifString);
152 String fileName
153 = st.nextToken();
154
155 BufferedInputStream
156 filesBufferedInputStream = new
157 BufferedInputStream(
158 new FileInputStream(
159 "c:\\lyon\\www" + fileName));
160 byte b [] = new byte[1024];
161 int numberRead = -1;
162 while ( (numberRead = filesBufferedInputStream.read(b))>0)
163 socketsOutputStream.write(b);
164 }
165 }
166