-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimpleWebServer.java
More file actions
132 lines (104 loc) · 5.46 KB
/
Copy pathSimpleWebServer.java
File metadata and controls
132 lines (104 loc) · 5.46 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
/***********************************************************************
SimpleWebServer.java
This toy web server is used to illustrate security vulnerabilities.
This web server only supports extremely simple HTTP GET requests.
This file is also available at http://www.learnsecurity.com/ntk
***********************************************************************/
package com.learnsecurity;
import java.io.*;
import java.net.*;
import java.util.*;
public class SimpleWebServer {
/* Run the HTTP server on this TCP port. */
private static final int PORT = 8080;
/* The socket used to process incoming connections
from web clients */
private static ServerSocket dServerSocket;
public SimpleWebServer () throws Exception {
dServerSocket = SSLServerSocketFactory.getDefault().createServerSocket(PORT);
}
public void run() throws Exception {
while (true) {
/* wait for a connection from a client */
Socket s = dServerSocket.accept();
/* then process the client's request */
processRequest(s);
}
}
/* Reads the HTTP request from the client, and
responds with the file the user requested or
a HTTP error code. */
public void processRequest(Socket s) throws Exception {
/* used to read data from the client */
BufferedReader br =
new BufferedReader (
new InputStreamReader (s.getInputStream()));
/* used to write data to the client */
OutputStreamWriter osw =
new OutputStreamWriter (s.getOutputStream());
/* read the HTTP request from the client */
String request = br.readLine();
String command = null;
String pathname = null;
/* parse the HTTP request */
StringTokenizer st =
new StringTokenizer (request, " ");
command = st.nextToken();
pathname = st.nextToken();
if (command.equals("GET")) {
/* if the request is a GET
try to respond with the file
the user is requesting */
serveFile (osw,pathname);
}
else {
/* if the request is a NOT a GET,
return an error saying this server
does not implement the requested command */
osw.write ("HTTP/1.0 501 Not Implemented\n\n");
}
/* close the connection to the client */
osw.close();
}
public void serveFile (OutputStreamWriter osw,
String pathname) throws Exception {
FileReader fr=null;
int c=-1;
StringBuffer sb = new StringBuffer();
/* remove the initial slash at the beginning
of the pathname in the request */
if (pathname.charAt(0)=='/')
pathname=pathname.substring(1);
/* if there was no filename specified by the
client, serve the "index.html" file */
if (pathname.equals(""))
pathname="index.html";
/* try to open file specified by pathname */
try {
fr = new FileReader (FilenameUtils.getName(pathname));
c = fr.read();
}
catch (Exception e) {
/* if the file is not found,return the
appropriate HTTP response code */
osw.write ("HTTP/1.0 404 Not Found\n\n");
return;
}
/* if the requested file can be successfully opened
and read, then return an OK response code and
send the contents of the file */
osw.write ("HTTP/1.0 200 OK\n\n");
while (c != -1) {
sb.append((char)c);
c = fr.read();
}
osw.write (sb.toString());
}
/* This method is called when the program is run from
the command line. */
public static void main (String argv[]) throws Exception {
/* Create a SimpleWebServer object, and run it */
SimpleWebServer sws = new SimpleWebServer();
sws.run();
}
}