The server only does the basics.
TODOs:
The header method needs expanding. The file send method can't handle too large files. Maybe write custom buffer class since some buffering is done. Keep cache of recent files.
Basic Web Server
public class WebServer {
public static String version = "Java Web Server 0.0";
public static void main(String[] args) {
try {
ServerSocket ss = new ServerSocket(8080);
System.out.println(version);
while (ss.isBound()) new Thread(new Client(ss.accept())).start();
}
catch (IOException e) { e.printStackTrace(); }
}
}
class Client implements Runnable {
private static final String userdir = System.getProperty("user.dir");
static final byte[] EOL = {(byte) '\r', (byte) '\n'};
private Socket accept;
private InputStream is;
private PrintStream os;
private enum Method {
GET,
HEAD,
UNSUPPORTED;
}
public Client(Socket accept) {
this.accept = accept;
}
public void run() {
try {
// variables
is = new BufferedInputStream(accept.getInputStream());
os = new PrintStream(accept.getOutputStream());
int character;
String req = "";
String[] request;
Method method;
File file;
// store first line of request header
character = is.read();
while (character != '\n' && character != '\r') {
req += (char) character;
character = is.read();
}
System.out.println(req);
request = req.split(" ");
// store method
if (request[0].matches("GET")) {
method = Method.GET;
} else if (request[0].matches("HEAD")) {
method = Method.HEAD;
} else {
method = Method.UNSUPPORTED;
}
// logic!
request[1] = request[1].replace("/", File.separator);
file = new File(userdir + File.separator + request[1]);
if (file.exists() && !file.isHidden()) {
if (method == Method.GET || method == Method.HEAD) {
writeHeader(file, 200);
if (method == Method.GET) {
if (file.isFile()) {
sendFile(file);
} else if (file.isDirectory()) {
listDir(request[1], file);
}
}
} else {
writeHeader(file, 501);
}
} else {
writeHeader(file, 404);
}
} catch (IOException ex) {
ex.printStackTrace();
} finally {
try {
accept.close();
} catch (IOException ex) {
System.exit(1);
}
}
}
private void sendFile(File file) throws FileNotFoundException, IOException {
int bufferlen = (int) file.length();
byte buffer[];
buffer = new byte[bufferlen];
InputStream fs = new FileInputStream(file);
int len = fs.read(buffer);
while (len < bufferlen) {
len += fs.read(buffer, len, bufferlen - len);
}
os.write(buffer);
os.flush();
}
private void listDir(String currentDir, File dir) {
currentDir = currentDir.replace(File.separator, "/");
String listing[] = dir.list();
if (!currentDir.endsWith("/")) {
currentDir += "/";
}
os.println(WebServer.version + "<br>");
for (String item : listing) {
os.println("<a href=\"" + currentDir + item + "\">" + item + "</a><br>");
}
os.flush();
}
private void writeHeader(File f, int code) throws IOException {
String fname = f.getName();
os.print("HTTP/1.0 ");
switch (code) {
case 200:
os.print("200 OK");
break;
case 400:
os.print("400 Bad Request");
break;
case 404:
os.print("404 Not Found");
break;
case 501:
os.print("501 Not Implemented");
break;
}
os.write(EOL);
if (code == 200) {
os.print("Content-Type: ");
if (fname.endsWith("html") || fname.endsWith("htm")) {
os.print("text/html");
} else if (fname.endsWith("java")) {
os.print("text/plain");
} else if (fname.endsWith("jpeg") || fname.endsWith("jpg")) {
os.print("image/gif");
} else {
os.print("text/html");
}
os.write(EOL);
}
os.write(EOL);
os.flush();
}
}
bords 1 Light Poster
seanbp 4 Junior Poster
Kris_1 0 Newbie Poster
~s.o.s~ 2,560 Failure as a human Team Colleague Featured Poster
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster
Kris_1 0 Newbie Poster
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster
Kris_1 0 Newbie Poster
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster
timetraveller1992 10 Newbie Poster
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.