-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
190 changed files
with
8,546 additions
and
0 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
#Webserver Config File | ||
max_concurrent_thread=3 | ||
blockIp=172.16.191.125,172.16.191.20,172.16.191.33,172.16.186.33 | ||
defaultFile=./index.html |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<html> | ||
<header><title>This is title</title></header> | ||
<body> | ||
<H1>Hello world</H1> | ||
</body> | ||
</html> |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
import java.io.BufferedReader; | ||
import java.io.DataOutputStream; | ||
import java.io.File; | ||
import java.io.FileInputStream; | ||
import java.io.FileReader; | ||
import java.io.IOException; | ||
import java.io.InputStreamReader; | ||
import java.io.PrintStream; | ||
import java.net.ServerSocket; | ||
import java.net.Socket; | ||
import java.util.Scanner; | ||
|
||
public class HttpServer | ||
{ | ||
public static void main(String[] args) | ||
{ | ||
ServerSocket server = null; | ||
int port = 1234; | ||
Scanner in = new Scanner(System.in); | ||
System.out.println("Press any key to start the server"); | ||
in.next(); | ||
try | ||
{ | ||
server = new ServerSocket(port); | ||
System.out.println("Server is started at port "+port); | ||
} | ||
catch (IOException e) | ||
{ | ||
// TODO Auto-generated catch block | ||
System.out.println("Error: Server with port "+port +" cannot be created"); | ||
e.printStackTrace(); | ||
return; | ||
} | ||
|
||
while(true) | ||
{ | ||
Socket client = null; | ||
try | ||
{ | ||
client = server.accept(); | ||
String clientIp = client.getRemoteSocketAddress().toString(); | ||
clientIp = clientIp.substring(1, clientIp.indexOf(':')); | ||
System.out.println("client request accepted at port "+port +" and ClienIp is "+clientIp); | ||
} | ||
catch (IOException e) | ||
{ | ||
// TODO Auto-generated catch block | ||
System.out.println("ERROR : client request can't be accepted at port "+port); | ||
e.printStackTrace(); | ||
return; | ||
} | ||
|
||
try { | ||
processClientHtmlRequest(client); | ||
} catch (IOException e) { | ||
// TODO Auto-generated catch block | ||
e.printStackTrace(); | ||
} | ||
} | ||
} | ||
|
||
private static void processClientHtmlRequest(Socket client) throws IOException | ||
{ | ||
// TODO Auto-generated method stub | ||
DataOutputStream printStream = new DataOutputStream(client.getOutputStream()); | ||
InputStreamReader inputReader = new InputStreamReader(client.getInputStream()); | ||
BufferedReader bufferReader = new BufferedReader(inputReader); | ||
|
||
String msg = bufferReader.readLine(); | ||
System.out.print("Request from client :" + msg+"\n"); | ||
String[] tokens = msg.split("[ /]"); | ||
String data = ""; | ||
if(tokens[2].equals("HttpServer.java") || tokens[2].equals("MultiThreadedHttpServer.java")) | ||
{ | ||
tokens[2] = "xyz"; | ||
} | ||
FileInputStream fi = null; | ||
try { | ||
String val =""+ System.getProperty("user.dir"); | ||
//System.out.println(val); | ||
File file = new File(val+"/"+tokens[2]); | ||
fi= new FileInputStream(file); | ||
} catch (Exception e) { | ||
// TODO: handle exception | ||
System.out.println("no file"); | ||
printStream.write(("HTTP/1.0 404 Not Found\r\n\r\n").getBytes()); | ||
printStream.close(); | ||
inputReader.close(); | ||
bufferReader.close(); | ||
System.out.println("connection close for this client\n"); | ||
return; | ||
} | ||
printStream.write(("HTTP/1.1 200 OK\r\n").getBytes()); | ||
if(tokens[2].contains("jpg")) | ||
printStream.write(("Content-Type: image/jpeg\r\n\r\n").getBytes()); | ||
else | ||
printStream.write(("Content-Type: text/html\r\n\r\n").getBytes()); | ||
//printStream.println("Content-Length: " + data.length()); | ||
int totalByte; | ||
byte[] buffer = new byte[2048]; | ||
while((totalByte = fi.read(buffer))!=-1) | ||
{ | ||
printStream.write(buffer,0,totalByte); | ||
} | ||
printStream.flush(); | ||
printStream.close(); | ||
inputReader.close(); | ||
bufferReader.close(); | ||
System.out.println("connection close for this client\n"); | ||
} | ||
} | ||
|
Binary file not shown.
231 changes: 231 additions & 0 deletions
231
Network Lab/HTTP Web Server/Code&File/MultiThreadedHttpServer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,231 @@ | ||
import java.io.BufferedReader; | ||
import java.io.DataOutputStream; | ||
import java.io.File; | ||
import java.io.FileInputStream; | ||
import java.io.FileNotFoundException; | ||
import java.io.FileReader; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.OutputStream; | ||
import java.io.InputStreamReader; | ||
import java.io.PrintStream; | ||
import java.net.ServerSocket; | ||
import java.net.Socket; | ||
import java.util.Properties; | ||
import java.util.Scanner; | ||
import java.util.Date; | ||
|
||
|
||
|
||
class htmlRequestHandler implements Runnable | ||
{ | ||
private Socket client ; | ||
private String serverText; | ||
|
||
//constructor | ||
htmlRequestHandler(Socket client,String serverText) | ||
{ | ||
this.client = client; | ||
this.serverText = serverText; | ||
} | ||
public htmlRequestHandler() { | ||
|
||
} | ||
|
||
//overriding run | ||
public void run() | ||
{ | ||
try | ||
{ | ||
//Increasing active thread count by 1 | ||
MultiThreadedHttpServer.threadCount++; | ||
DataOutputStream printStream = new DataOutputStream(client.getOutputStream()); | ||
InputStreamReader inputReader = new InputStreamReader(client.getInputStream()); | ||
BufferedReader bufferReader = new BufferedReader(inputReader); | ||
|
||
//Reading Request from client | ||
String msg = bufferReader.readLine(); | ||
System.out.print("Request message : "+msg + "\n"); | ||
String[] tokens = msg.split("[ /]"); | ||
if(tokens[2].length() == 0) | ||
{ | ||
tokens[2] = this.serverText; | ||
} | ||
if(tokens[2].equals("HttpServer.java") || tokens[2].equals("MultiThreadedHttpServer.java")) | ||
{ | ||
tokens[2] = "xyz"; | ||
} | ||
String data = ""; | ||
FileInputStream fi = null; | ||
try | ||
{ | ||
String val =""+ System.getProperty("user.dir"); | ||
File file = new File(val+"/"+tokens[2]); | ||
fi= new FileInputStream(file); | ||
} | ||
catch (Exception e) | ||
{ | ||
System.out.println("no file"); | ||
printStream.write(("HTTP/1.1 404 Not Found\r\n\r\n").getBytes()); | ||
printStream.close(); | ||
inputReader.close(); | ||
bufferReader.close(); | ||
Thread.sleep(10000); | ||
MultiThreadedHttpServer.threadCount--; | ||
return; | ||
} | ||
printStream.write(("HTTP/1.1 200 OK\r\n").getBytes()); | ||
if(tokens[2].contains("jpg")) | ||
printStream.write(("Content-Type: image/jpeg\r\n\r\n").getBytes()); | ||
else | ||
printStream.write(("Content-Type: text/html\r\n\r\n").getBytes()); | ||
//printStream.write(("Content-Length: " + data.length()+"\n\n").getBytes());; | ||
int totalByte; | ||
byte[] buffer = new byte[2048]; | ||
while((totalByte = fi.read(buffer))!=-1) | ||
{ | ||
printStream.write(buffer,0,totalByte); | ||
} | ||
printStream.flush(); | ||
printStream.close(); | ||
inputReader.close(); | ||
bufferReader.close(); | ||
client.close(); | ||
//make thread sleep for 10s so that we can see effect of max allowed thread | ||
Thread.sleep(10000); | ||
MultiThreadedHttpServer.threadCount--; | ||
} | ||
catch (Exception e) | ||
{ | ||
// TODO: handle exception | ||
} | ||
} | ||
} | ||
|
||
public class MultiThreadedHttpServer | ||
{ | ||
public static int threadCount = 0; | ||
public static void main(String[] args) | ||
{ | ||
ServerSocket server = null; | ||
int port = 1234; | ||
Scanner in = new Scanner(System.in); | ||
Properties prop = new Properties(); | ||
|
||
//loading Config.config file | ||
String val =""+ System.getProperty("user.dir"); | ||
String configFile = val+"/Config.config"; | ||
try | ||
{ | ||
InputStream is = new FileInputStream(configFile); | ||
prop.load(is); | ||
} | ||
catch (FileNotFoundException e1) | ||
{ | ||
System.out.println("Config File not found"); | ||
e1.printStackTrace(); | ||
return; | ||
} | ||
catch (IOException e) | ||
{ | ||
e.printStackTrace(); | ||
return; | ||
} | ||
|
||
System.out.println("Press any key to start the server........"); | ||
in.next(); | ||
try | ||
{ | ||
//ServerSocket at port 1234 created | ||
server = new ServerSocket(port); | ||
System.out.println("Server is started at port "+port); | ||
} | ||
catch (IOException e) | ||
{ | ||
System.out.println("Error: Server with port "+port +" cannot be created"); | ||
e.printStackTrace(); | ||
return; | ||
} | ||
|
||
while(true) | ||
{ | ||
Socket client = null; | ||
try | ||
{ | ||
client = server.accept(); | ||
System.out.println("client request accepted at port "+port); | ||
} | ||
catch (IOException e) | ||
{ | ||
System.out.println("ERROR : client request can't be accepted at port "+port); | ||
e.printStackTrace(); | ||
return; | ||
} | ||
String ip = prop.getProperty("blockIp"); | ||
String[] ipTokens = ip.split(","); | ||
String clientIp = client.getRemoteSocketAddress().toString(); | ||
clientIp = clientIp.substring(1, clientIp.indexOf(':')); | ||
System.out.println("Client ip is : "+clientIp); | ||
boolean block = false; | ||
|
||
//Checking if Client ip is block | ||
for(int i=0;i<ipTokens.length;i++) | ||
{ | ||
if(clientIp.equals(ipTokens[i])) | ||
{ | ||
block = true; | ||
break; | ||
} | ||
} | ||
int allowedThread = Integer.parseInt(prop.getProperty("max_concurrent_thread")); | ||
String defaultPath = prop.getProperty("defaultFile"); | ||
String[] fle = defaultPath.split("/"); | ||
defaultPath = fle[1]; | ||
System.out.println("Totthread = "+threadCount); | ||
|
||
//If valid connection then create thread and process | ||
if(threadCount < allowedThread && block == false) | ||
{ | ||
new Thread(new htmlRequestHandler(client, defaultPath)).start(); | ||
} | ||
else if(block == true) | ||
{ | ||
//Case of blocked IP | ||
System.out.println("This Ip is Blocked"); | ||
try | ||
{ | ||
DataOutputStream printStream = new DataOutputStream(client.getOutputStream()); | ||
printStream.write(("HTTP/1.1 403.6 IP address rejected\r\n\r\n").getBytes()); | ||
printStream.close(); | ||
client.close(); | ||
} | ||
catch (IOException e) | ||
{ | ||
e.printStackTrace(); | ||
return; | ||
} | ||
|
||
} | ||
else | ||
{ | ||
System.out.println("Allowed Thread limit reached"); | ||
//Condition for thread limit reached | ||
try | ||
{ | ||
DataOutputStream printStream = new DataOutputStream(client.getOutputStream()); | ||
Date today = new Date(); | ||
printStream.write(("HTTP/1.1 403 Access Denied\r\n"+"Connection: close\r\n\r\n").getBytes()); | ||
printStream.close(); | ||
client.close(); | ||
} | ||
catch (IOException e) | ||
{ | ||
e.printStackTrace(); | ||
return; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
The Transmission Control Protocol (TCP) is one of the main protocols of the Internet protocol suite. It originated in the initial network implementation in which it complemented the Internet Protocol (IP). Therefore, the entire suite is commonly referred to as TCP/IP. TCP provides reliable, ordered, and error-checked delivery of a stream of octets (bytes) between applications running on hosts communicating by an IP network. Major Internet applications such as the World Wide Web, email, remote administration, and file transfer rely on TCP. Applications that do not require reliable data stream service may use the User Datagram Protocol (UDP), which provides a connectionless datagram service that emphasizes reduced latency over reliability. |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<html> | ||
<header><title>Index.html(default)</title></header> | ||
<body> | ||
<H1>Available File<br></H1> | ||
<h3>1.HelloWorld.html<br><h3> | ||
<h3>2.messi.jpg<br></h3> | ||
<h3>3.TCP.txt<br></h3> | ||
</body> | ||
</html> |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+191 KB
Network Lab/HTTP Web Server/ScreenShot/Part1_ScreenShot/Request_1_HelloWorld.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+239 KB
Network Lab/HTTP Web Server/ScreenShot/Part1_ScreenShot/Request_5_ayush.jpg.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+207 KB
...Lab/HTTP Web Server/ScreenShot/Part2_ScreenShot/DefaultPage_demo_ScreenShot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+226 KB
...ork Lab/HTTP Web Server/ScreenShot/Part2_ScreenShot/IpBlock_demo_ScreenShot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+265 KB
...Lab/HTTP Web Server/ScreenShot/Part2_ScreenShot/ThreadLimit_demo_ScreenShot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Oops, something went wrong.