-
Notifications
You must be signed in to change notification settings - Fork 0
/
Httpf1.java
209 lines (184 loc) · 5.07 KB
/
Httpf1.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
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Iterator;
import java.util.Scanner;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class Httpf1 {
static ServerSocket socketServer;
static Socket socket;
static InputStreamReader isr;
static BufferedReader br;
static PrintWriter pw;
static String bodyInfo = "";
public static void main(String[] args) throws Exception {
while(true) {
socketServer = new ServerSocket(80);
socket = socketServer.accept();
System.out.println("Client Connected!");
InputStream inp = socket.getInputStream();
pw = new PrintWriter(socket.getOutputStream(),true);
String info ="";
String in ="";
StringBuilder response = new StringBuilder();
int data = inp.read();
int count=0;
int countLineFeed =0;
int countBody= 0;
int trackbody =0;
boolean check2 =true;
boolean leaveWhileLoop =false;
String request ="";
while(data != -1) {
if(count == 10) {
request = response.toString().substring(0,response.toString().indexOf("http:")-1);
}
if(count >10) {
if(request.equals("GET")) {
if(countLineFeed == 2)
break;
}
else if(request.equals("POST")) {
if(countLineFeed ==2 && check2) {
countBody = Integer.parseInt(response.toString().substring(response.toString().indexOf("Content-Length")+16,response.toString().length()-1));
check2 =false;
}
if(countLineFeed ==3) {
if(trackbody == countBody) {
leaveWhileLoop =true;
}
bodyInfo += String.valueOf((char)data);
trackbody++;
}
}
}
response.append((char) data);
if(leaveWhileLoop) {
break;
}
data = inp.read();
if(data == 10 ) {
countLineFeed++;
}
count++;
}
info = response.toString();
RequestFromClient(info);
socketServer.close();
socket.close();
System.out.println("Client Disconnected");
}
}
public static void RequestFromClient(String info) {
String request = "";
String path;
int indexOfEndOfURl=0;
// IS IT GET OR POST
for(int x=0;x<info.length();x++) {
if(info.charAt(x) == ' ') {
request = info.substring(0,x);
break;
}
}
//GET what inside the '?' --> http://localhost?
indexOfEndOfURl = info.indexOf("HTTP/1.0")-1;
path = info.substring(info.indexOf("http:")+16,indexOfEndOfURl);
if(path.length()>=2) {
// FOR SECURITY
if(path.charAt(1) == '.' && path.charAt(2) == '.') {
System.out.println(" The Client tried to break the security");
fileNotReadable();
}else {
redirectInfo(request,path);
}
}else {
redirectInfo(request,path);
}
}
public static void redirectInfo(String request,String path) {
switch(request) {
case "GET":
switch(path) {
case "/" :
case "\\" : getServerDefault(path);
break;
default :
getServerFile(path);
break;
}
break;
case "POST":
PostServer(path);
break;
default :
break;
}
}
//IF PATH = '/'
public static void getServerDefault(String path) {
try {
File[] allfiles = new File(Paths.get(".").toAbsolutePath().normalize().toString()).listFiles();
responseWithContentTypeTEXT(allfiles);
}catch(Exception e) {
fileNotFound();
}
}
//IF PATH = '/SOMETHING'
public static void getServerFile(String path) {
try (FileReader fileToread = new FileReader("..\\src\\"+path)){
BufferedReader brfile = new BufferedReader(fileToread);
String data="";
String line;
while ((line = brfile.readLine()) != null) {
data+= line+"\n";
}
pw.write("HTTP/1.1 200 OK\r\nContent-Type:text/html\r\nContent-Length:"+data.length()+"\r\n\r\n"+data);
pw.close();
fileToread.close();
brfile.close();
}catch(Exception e) {
fileNotFound();
}
}
//Post
public static void PostServer(String path) {
try(PrintWriter writer = new PrintWriter(new FileOutputStream("..\\src\\"+path, false))){
writer.println(bodyInfo);
writer.close();
String format = "{\n\"data\": \"{"+ bodyInfo +"}\",\n}";
pw.write("HTTP/1.1 200 OK\r\nContent-Type:text/html\r\nContent-Length:"+format.length()+"\r\n\r\n"+format);
pw.close();
}catch(Exception e) {
fileNotFound();
}
}
private static void responseWithContentTypeTEXT(File[] allfiles) {
String data="";
for(File file : allfiles) {
if(file.isFile()) {
data += file.getName() + "\n";
}
}
pw.write("HTTP/1.1 200 OK\r\nContent-Type:text/html\r\nContent-Length:"+data.length()+"\r\n\r\n"+data);
pw.close();
}
private static void fileNotFound() {
pw.write("HTTP/1.0 404 Not Found");
pw.close();
}
private static void fileNotReadable() {
pw.write("HTTP/1.0 403 Forbidden");
pw.close();
}
}