├── README.md
├── htdocs
├── 404.html
├── 415.html
├── folder
│ └── test.txt
└── index.html
└── src
├── Client.java
├── MainServer.java
└── SingleThread.java
/README.md:
--------------------------------------------------------------------------------
1 | # Venus-HTTP-Server
2 | Venus Server - Tiny HTTP server in Java.
3 |
4 | // TODO
5 | - Apply SOLID Principles.
6 | - Add tiny compiler.
7 | - Add logger and loader.
8 |
--------------------------------------------------------------------------------
/htdocs/404.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | 404 Not Found
5 |
6 |
7 | Not Found
8 | The requested URL was not found on this server.
9 |
10 |
11 |
--------------------------------------------------------------------------------
/htdocs/415.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | 415 Unsupported Media Type
5 |
6 |
7 | Unsupported Media Type
8 | The media format of the requested data is not supported by the server.
9 |
10 |
11 |
--------------------------------------------------------------------------------
/htdocs/folder/test.txt:
--------------------------------------------------------------------------------
1 | Venus Server
--------------------------------------------------------------------------------
/htdocs/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 | Venus Server
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
Venus Server
40 |
GOOD LUCK
41 |
42 |
43 |
44 |

45 |
46 |

47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 |
55 |
113 |
114 |
115 |
116 |
117 |
118 |
119 |
--------------------------------------------------------------------------------
/src/Client.java:
--------------------------------------------------------------------------------
1 | /**
2 | * @file Client.java
3 | * @authors Mahmoud Ahmed (mahmoudahmedd) - Mostafa Omar (MostafaOmar98) - Yousef okasha (yousefokasha61)
4 | * @date 14/04/2019
5 | * @version 1.0
6 | */
7 |
8 | import java.io.IOException;
9 | import java.io.PrintStream;
10 | import java.net.Socket;
11 | import java.util.Scanner;
12 |
13 | public class Client
14 | {
15 |
16 | public static void main(String args[]) throws IOException
17 | {
18 | Socket socket = new Socket("127.0.0.1", 8080);
19 | Scanner userInput = new Scanner(System.in), socketInput = new Scanner(socket.getInputStream());
20 | PrintStream socketOutput = new PrintStream(socket.getOutputStream());
21 | System.out.println("What function would you like to do?:\n" +
22 | "1- GET\n" +
23 | "2- POST\n" +
24 | "3- DELETE\n" +
25 | "4- PUT\n");
26 | int choice;
27 | choice = userInput.nextInt();
28 | String URL, Request = "";
29 | if (choice == 1)
30 | {
31 | System.out.print("Enter URL: ");
32 | Request += "GET ";
33 | URL = userInput.next();
34 | Request += URL;
35 | Request += " HTTP/1.1";
36 | Request += "\r\n\r\n";
37 | }
38 | if (choice == 2)
39 | {
40 | System.out.println("Enter URL: ");
41 | Request += "POST ";
42 | URL = userInput.next();
43 | Request += URL;
44 | Request += " HTTP/1.1";
45 | Request += "\r\n";
46 | System.out.println("Enter Body (DONE! to stop): \n");
47 | String line;
48 | while(true)
49 | {
50 | line = userInput.nextLine();
51 | if (line.equals("DONE!"))
52 | break;
53 | Request += line;
54 | Request += "\r\n";
55 | }
56 | Request += "\r\n\r\n";
57 | }
58 | else if (choice == 3)
59 | {
60 | System.out.print("Enter URL: ");
61 | Request += "DELETE ";
62 | URL = userInput.next();
63 | Request += URL;
64 | Request += " HTTP/1.1";
65 | Request += "\r\n\r\n";
66 | }
67 | else if (choice == 4)
68 | {
69 | System.out.println("Enter URL: ");
70 | Request += "PUT ";
71 | URL = userInput.next();
72 | Request += URL;
73 | Request += " HTTP/1.1";
74 | Request += "\r\n";
75 | System.out.println("Enter Body (DONE! to stop): \n");
76 | String line;
77 | while(true)
78 | {
79 | line = userInput.nextLine();
80 | if (line.equals("DONE!"))
81 | break;
82 | Request += line;
83 | Request += "\r\n";
84 | }
85 | Request += "\r\n\r\n";
86 |
87 | }
88 | socketOutput.print(Request);
89 | while(socketInput.hasNextLine())
90 | System.out.println(socketInput.nextLine());
91 |
92 | }
93 |
94 | }
95 |
--------------------------------------------------------------------------------
/src/MainServer.java:
--------------------------------------------------------------------------------
1 | /**
2 | * @file MainServer.java
3 | * @authors Mahmoud Ahmed (mahmoudahmedd) - Mostafa Omar (MostafaOmar98) - Yousef okasha (yousefokasha61)
4 | * @date 14/04/2019
5 | * @version 1.0
6 | */
7 |
8 | import java.io.*;
9 | import java.net.*;
10 |
11 | public class MainServer
12 | {
13 | public MainServer() throws IOException
14 | {
15 | int portNumber = 8080;
16 | ServerSocket serverSocket = new ServerSocket(portNumber); //create a server socket object
17 |
18 | System.out.println("Running!");
19 | System.out.println("Point your browsers to http://localhost:" + portNumber + " \n");
20 |
21 | while(true)
22 | {
23 | Socket clientSocket = serverSocket.accept();
24 | SingleThread SingleThread = new SingleThread(clientSocket);//create a new thread for each client
25 | SingleThread.start();
26 | }
27 | }
28 |
29 | public static void main(String[] args)
30 | {
31 | try
32 | {
33 | new MainServer();
34 | }
35 | catch (IOException ioe)
36 | {
37 | System.err.println("Couldn't start server: " + ioe);
38 | }
39 | }
40 | }
41 |
--------------------------------------------------------------------------------
/src/SingleThread.java:
--------------------------------------------------------------------------------
1 | /**
2 | * @file SingleThread.java
3 | * @authors Mahmoud Ahmed (mahmoudahmedd) - Mostafa Omar (MostafaOmar98) - Yousef okasha (yousefokasha61)
4 | * @date 14/04/2019
5 | * @version 1.0
6 | */
7 |
8 | import java.io.*;
9 | import java.net.*;
10 | import java.util.Date;
11 | import java.util.Scanner;
12 |
13 | public class SingleThread extends Thread
14 | {
15 |
16 | private Socket socket;
17 | // Reads and prints to socket streams
18 | private BufferedReader in;
19 | private PrintWriter out;
20 | private String htdocsPath = "E:/Venus Server/htdocs";
21 | final static String CRLF = "\r\n";
22 |
23 | public SingleThread(Socket clientSocket)
24 | {
25 | this.socket = clientSocket;
26 | }
27 |
28 | public void run()
29 | {
30 | try
31 | {
32 | //create a buffer reader
33 | in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
34 | //create a PrintWriter
35 | out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())), true);
36 | String line;
37 | String httpHeader = "";
38 | while (((line = in.readLine()) != null) && !line.equals("") && !line.equals(CRLF))
39 | {
40 | httpHeader += line;
41 | httpHeader += CRLF;
42 | }
43 | if (!httpHeader.equals(""))
44 | {
45 | String[] request = httpHeader.split(" ");
46 | System.out.println("Client HTTP Request Header: " + CRLF + httpHeader);
47 | if (request[0].equals("GET")) // if line contains get
48 | {
49 |
50 | //extract the html filename
51 | if (request[1].equals("/"))
52 | {
53 | request[1] = "/index.html";
54 | }
55 |
56 | processRequestGET(new java.net.URI(request[1]).getPath()); // process the request
57 | }
58 | else if (request[0].equals("POST"))
59 | {
60 | String requestBody = "";
61 | while (((line = in.readLine()) != null) && !line.equals("") && !line.equals(CRLF))
62 | {
63 | requestBody += line;
64 | requestBody += CRLF;
65 | }
66 |
67 | System.out.println("Client HTTP Request Body: " + CRLF + requestBody);
68 | processRequestPOST(new java.net.URI(request[1]).getPath(), requestBody);
69 | }
70 | else if (request[0].equals("DELETE"))
71 | {
72 | //System.out.println("INSIDE DELETE: " + FilePath);
73 | File file = new File(htdocsPath + new java.net.URI(request[1]).getPath());
74 | if (file.delete())
75 | {
76 | System.out.println(htdocsPath + new java.net.URI(request[1]).getPath() + " has been deleted");
77 | }
78 | else
79 | {
80 | System.out.println("File cannot be found");
81 | }
82 | }
83 | else if (request[0].equals("PUT"))
84 | {
85 | String requestBody = "";
86 |
87 | while (((line = in.readLine()) != null) && !line.equals("") && !line.equals(CRLF))
88 | {
89 | requestBody += line;
90 | requestBody += CRLF;
91 | }
92 |
93 | System.out.println("Client HTTP Request Body: " + CRLF + requestBody);
94 | processRequestPUT(new java.net.URI(request[1]).getPath(), requestBody);
95 | }
96 | }
97 | closeConnection(); // close the socket connection and input/output buffers
98 | }
99 | catch (Exception e) //print error stack trace
100 | {
101 | e.printStackTrace();
102 | }
103 | }
104 |
105 | public void processRequestGET(String fileName) throws Exception
106 | {
107 |
108 | File file = new File(htdocsPath + fileName); //create a file variable
109 | URLConnection connection = file.toURL().openConnection();
110 | String mimeType = connection.getContentType();
111 |
112 |
113 | if(file.isDirectory())
114 | {
115 | //sent the HTTP head (404 Not Found)
116 | out.print("HTTP/1.1 200 Not Found" + CRLF);
117 | Date date = new Date();
118 | out.print("Date: " + date.toString() + CRLF);
119 | out.print("Server: Venus Web Server" + CRLF);
120 | out.print("Connection: close" + CRLF);
121 | out.println("Content-Type: text/html; charset=UTF-8" + CRLF);
122 | //end of http header
123 |
124 | File[] listOfFiles = file.listFiles();
125 |
126 | for (int i = 0; i < listOfFiles.length; i++)
127 | if (listOfFiles[i].isDirectory())
128 | out.println("" + listOfFiles[i].getName() + "
");
129 |
130 | for (int i = 0; i < listOfFiles.length; i++)
131 | if (listOfFiles[i].isFile())
132 | out.println("" + listOfFiles[i].getName() + "
");
133 |
134 | }
135 | else if (file.exists() && (fileName.contains(".html") || fileName.contains(".txt"))) // if file exists
136 | {
137 | Scanner reader = new Scanner(new File(htdocsPath + fileName));
138 | //sent the HTTP head (HTTP 200 OK)
139 | out.print("HTTP/1.0 200 OK" + CRLF); // HTTP/1.1 results in a failure in showing the content of the file
140 | Date date = new Date();
141 | out.print("Date: " + date.toString() + CRLF);
142 | out.print("Server: Venus Web Server" + CRLF);
143 | out.print("Content-Length: " + file.length() + CRLF);
144 | out.println("Content-Type: " + mimeType + "; charset=UTF-8" + CRLF);
145 | //end of http header
146 |
147 |
148 | String line = "";
149 |
150 | while (reader.hasNextLine()) //read a line from the html file
151 | {
152 | line = reader.nextLine();
153 | out.println(line); //write the line to the socket connection
154 | }
155 | reader.close(); // must close reader to allow further access of file
156 | }
157 | else if (!file.exists()) //if file does not exists
158 | {
159 | //sent the HTTP head (404 Not Found)
160 | out.print("HTTP/1.1 404 Not Found" + CRLF);
161 | Date date = new Date();
162 | out.print("Date: " + date.toString() + CRLF);
163 | out.print("Server: Venus Web Server" + CRLF);
164 | out.print("Connection: close" + CRLF);
165 | out.println("Content-Type: text/html; charset=UTF-8" + CRLF);
166 | //end of http header
167 |
168 | //send file not found message
169 | file = new File(htdocsPath + "/404.html");
170 | Scanner reader = new Scanner(new FileReader(file));
171 | String line;
172 | while (reader.hasNextLine())
173 | {
174 | line = reader.nextLine();
175 | out.println(line);
176 | }
177 | reader.close();
178 | }
179 | else
180 | {
181 | //sent the HTTP head (415 Unsupported Media Type)
182 | out.print("HTTP/1.1 415 Unsupported Media Type" + CRLF);
183 | Date date = new Date();
184 | out.print("Date: " + date.toString() + CRLF);
185 | out.print("Server: Venus Web Server" + CRLF);
186 | out.print("Connection: close" + CRLF);
187 | out.println("Content-Type: text/html; charset=UTF-8" + CRLF);
188 | //end of http header
189 |
190 | //send file not found message
191 | file = new File(htdocsPath + "/415.html");
192 | Scanner reader = new Scanner(new FileReader(file));
193 | String line;
194 | while (reader.hasNextLine())
195 | {
196 | line = reader.nextLine();
197 | out.println(line);
198 | }
199 | reader.close();
200 | }
201 | }
202 |
203 | public void processRequestPOST(String fileName, String RequestBody) throws Exception
204 | {
205 | File file = new File(htdocsPath + fileName);
206 | file.createNewFile();
207 | FileWriter writer = new FileWriter(htdocsPath + fileName, true);
208 | writer.write(RequestBody);
209 | writer.flush();
210 | processRequestGET(fileName);
211 | writer.close();
212 | }
213 |
214 | public void processRequestPUT(String fileName, String RequestBody) throws Exception
215 | {
216 | File file = new File(htdocsPath + fileName);
217 | FileWriter writer = new FileWriter(htdocsPath + fileName);
218 | if (!file.createNewFile())
219 | {
220 | writer.flush();
221 | }
222 | writer.write(RequestBody);
223 | writer.flush();
224 | processRequestGET(fileName);
225 | writer.close();
226 | }
227 |
228 | private void closeConnection()
229 | {
230 | try
231 | {
232 | out.close(); // close output stream
233 | in.close(); // close input stream
234 | socket.close(); //close socket
235 | }
236 | catch (Exception e)
237 | {
238 | System.out.println(e.toString());
239 | }
240 | }
241 | }
242 |
--------------------------------------------------------------------------------