-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.java
More file actions
217 lines (155 loc) · 4.17 KB
/
server.java
File metadata and controls
217 lines (155 loc) · 4.17 KB
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
package lab4;
import java.io.*;
import java.net.*;
import java.util.*;
public class server extends Thread {
static final String s = "<html>" + "<body>";
static final String e = "</body>" + "</html>";
static Socket conn;
Socket client1 = null;
BufferedReader in = null;
DataOutputStream out = null;
public static void main(String args[]) throws Exception
{
int port =1205;
ServerSocket Server = new ServerSocket(port, 10, InetAddress.getByName("127.0.0.1"));
while (true)
{
conn = Server.accept();
(new server(conn)).start();
}
}
public void run()
{
try {
in = new BufferedReader(new InputStreamReader(client1.getInputStream()));
out = new DataOutputStream(client1.getOutputStream());
String request = in.readLine();
String header = request;
StringBuffer response = new StringBuffer();
response.append("<b>Server Page.... </b><BR> Client request is ....<BR>");
StringTokenizer tokenizer = new StringTokenizer(header);
String method = tokenizer.nextToken();
String query = tokenizer.nextToken();
while (in.ready())
{
response.append(request + "<BR>");
request = in.readLine();
}
if (method.equals("GET"))
{
if (query.equals("/"))
{
int y=200;
send_client(y, response.toString(), 0);
}
else
{
String name = query.replaceFirst("/", "");
name = URLDecoder.decode(name);
if(name.equals("redirect"))
{
send_client(302, name, 0);
}
if(name.equals("forbidden"))
{
send_client(403,"<b> 403 Error ...The Requested is Forbidden ....",0);
}
if (new File(name).isFile())
{
send_client(200, name, 1);
}
else
{
send_client(404,"<b> 404 Error ...The Requested resource not found ....",0);
}
}
}
else if(method.equals("POST"))
{
if (query.equals("/")) {
int y=200;
send_client(y, response.toString(), 0);
}
else
{
String name = query.replaceFirst("/", "");
name = URLDecoder.decode(name);
if (new File(name).isFile())
{
send_client(200, name, 1);
}
if (new File(name).isFile())
{
send_client(200, name, 1);
} else
{
send_client(404,"<b> 404 ERROR ...The Requested resource not found ....",0);
}
}
}
else
send_client(404,"<b>404 ERROR ...The Requested resource not found ....",0);
} catch (Exception e) {
e.printStackTrace();
}
}
public void send_client(int statusCode, String response, int a) throws Exception
{
int j=0;
String statusLine = null;
String serverdetails = "Server: Java HTTPServer";
String contentLengthLine = null;
String contentTypeLine = "Content-Type: text/html" + "\r\n";
FileInputStream fin = null;
if (statusCode == 200)
statusLine = "HTTP/1.1 200 OK \r\n";
else if (statusCode == 302 )
{
statusLine = "Found " + "lms.nust.edu.pk" ;
}
else if (statusCode == 404 )
{statusLine = "HTTP/1.1 404 Not Found\r\n" ;}
else if (statusCode == 403 )
{statusLine = "Forbidden Request \r\n" ;}
String Name = null;
if (a==1)
{
Name = response;
fin = new FileInputStream(Name);
contentLengthLine = "Content-Length: "+ Integer.toString(fin.available()) + "\r\n";
if (!Name.endsWith(".htm") && !Name.endsWith(".html"))
contentTypeLine = "Content-Type: \r\n";
}
else
{
response = server.s + response + server.e;
contentLengthLine = "Content-Length: " + response.length() + "\r\n";
}
out.writeBytes(statusLine);
out.writeBytes(serverdetails);
out.writeBytes(contentTypeLine);
out.writeBytes(contentLengthLine);
out.writeBytes("Connection: close\r\n");
out.writeBytes("\r\n");
if (a==1)
send(fin, out);
else
out.writeBytes(response);
out.close();
conn.close();
}
public void send(FileInputStream fin1, DataOutputStream out)
throws Exception {
byte[] buf = new byte[1024];
int bytesRead;
while ((bytesRead = fin1.read(buf)) != -1)
{
out.write(buf, 0, bytesRead);
}
fin1.close();
}
public server(Socket client) {
client1 = client;
}
}