-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEcho_Server.java
305 lines (183 loc) · 4.96 KB
/
Echo_Server.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
package testPrint;
import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Arrays;
public class Echo_Server {
public static int port=80;
private static void dnsLookup(byte[] host,byte[] path, Socket m, int thePort) throws IOException //dns_lookup for host.
{
InetAddress temp = null;
try
{
InetAddress lookup[] = InetAddress.getAllByName(new String(host));
temp = lookup[0];
clientSocket(temp, m, path, host, thePort);
}
catch(Exception e)
{
m.getOutputStream().write(" INVALID WEB PAGE HAS BEEN REQUESTED\r\n".getBytes("UTF-8"));
m.getOutputStream().close();
m.close();
}
}
private static void clientSocket(InetAddress host_name, Socket m, byte[] path_name, byte[] url, int thePort) throws IOException
{
Socket example = null;
try
{
example= new Socket(host_name, port);
//System.out.println("testttttt--->"+port);
//System.out.println("urllllllllllllllllllll--->"+"GET " +new String(path_name) + "HTTP/1.1\r\n" + "Host: " +new String(url)+":"+String.valueOf(thePort) + "\r\n" + "Connection: close\r\n\r\n");
if(port==80)
{
example.getOutputStream().write(("GET " +new String(path_name) + "HTTP/1.1\r\n" + "Host: " +new String(url) + "\r\n" + "Connection: close\r\n\r\n").getBytes("UTF-8"));
}
else
{
example.getOutputStream().write(("GET " +new String(path_name) + "HTTP/1.1\r\n" + "Host: " +new String(url)+":"+String.valueOf(port) + "\r\n" + "Connection: close\r\n\r\n").getBytes("UTF-8"));
}
int byte_reader = 0;
byte reader[] = new byte[1400];
while(byte_reader!=-1)
{
byte_reader = example.getInputStream().read(reader, 0, reader.length);
System.out.println("+++++++++++++++++++++"+byte_reader);
if(byte_reader == -1)
{
continue;
}
else {
m.getOutputStream().write(reader, 0, byte_reader);
for(int i = 0; i < reader.length; i++)
System.out.print((char)reader[i]);
}
}
}
catch(Exception E)
{
//m.getOutputStream().write("HTTP/1.1 403 Forbidden".getBytes());
m.getOutputStream().write("HTTP/1.1 400 Bad Request\r\n Content-type: text/plain\r\n\r\n UNKNOWN HOST HAS BEEN REQUESTED\r\n".getBytes());
E.printStackTrace();
}
}
private static byte[] path_parser(byte[] h, int s, int counter)
{
byte path_name[] = new byte[s];
boolean checker = false;
int a = counter+10;
int k = 0;
int m = 0;
for(; (a<s); a++)
{
if(h[a] == '/' )
{
while(!checker)
{
path_name[k] = h[a];
if(h[a] == ' ')
checker = true;
k++;
a++;
m++;
}
}
else
{
continue;
}
{
}
}
byte[] path_parser_dup = Arrays.copyOf(path_name, m);
return path_parser_dup;
}
private static byte[] doParser(byte[] h , int s)
{
int r = 0;
int y = 0;
boolean checker = true;
boolean flag = true;
byte[] dns = new byte[s];
for(r = 0; (r<s)&&(checker); r++)
{
if(h[r] == '\r' && h[r+1] == '\n' && h[r+2] == 'H')
{
r = r+8;
while(flag)
{
if(h[r]=='\r')
{
checker = false;
flag = false;
}
else
{
dns[y] = h[r];
y++;
r++;
}
}
}
}
byte[] dns1 = Arrays.copyOf(dns, y);
byte[] b=null;
for(int i=0;i<dns1.length;i++)
{
if((char) dns1[i]==':')
{
b=Arrays.copyOfRange(dns1, i+1, dns1.length);
dns1=Arrays.copyOfRange(dns1, 0, i);
break;
}
}
if(b!=null)
{
port=Integer.parseInt(new String(b));
}
else
{
port=80;
}
return dns1;
}
public static void main(String[] args) throws IOException
{
//int Clientport = Integer.parseInt(args[0]);
//if(Clientport < 0 || Clientport > 65535)
//System.exit(1);
ServerSocket r = new ServerSocket();
r.bind(new InetSocketAddress(4367));
System.out.print("stage 1 program by(djn26) listening on port: " );
for(;;)
{
try(Socket m = r.accept())
{
int size = 1;
byte b[] = new byte[65535];
byte a[] = new byte[size];
int s = 0;
try
{
s = m.getInputStream().read(b, 0, 65535);
if(s == -1)
{
s = 0;
}
int thePort = port;
byte[] parser_array = doParser(b,s);
System.out.println(new String(parser_array) + new String(path_parser(b, s, parser_array.length)));
byte[] path_parser_array = path_parser(b,s, parser_array.length);
dnsLookup(parser_array, path_parser_array, m, thePort);
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
}
}