-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWebServer.java
More file actions
executable file
·55 lines (48 loc) · 1.53 KB
/
WebServer.java
File metadata and controls
executable file
·55 lines (48 loc) · 1.53 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
import java.io.IOException;
import java.net.*;
import java.io.*;
public class WebServer{
static HttpdConf httpdConf;
static MimeTypes mimeTypes;
static Request request;
static ServerSocket socket;
static ResponseFactory responseFactory;
static Response response;
static Resource resource;
public static void main( String[] args ) throws IOException,Exception{
try{
responseFactory = new ResponseFactory();
httpdConf = new HttpdConf("conf/httpd.conf");
mimeTypes = new MimeTypes("conf/mime.types");
start();
}catch(Exception ServerException){
startWith500();
ServerException.printStackTrace();
}
}
public static void start()throws IOException, Exception{
socket = new ServerSocket( httpdConf.listen );
while( true ){
Socket client = socket.accept();
request = new Request(client.getInputStream());
resource = new Resource(request.getUriString(), httpdConf);
try{
response = responseFactory.getResponse(request,resource,mimeTypes);
}catch(Exception e){
response = new Response(400,"Bad Request");
}
request.logRequest(httpdConf.logFile,response.code,response.size,client.getRemoteSocketAddress().toString());
response.send(client.getOutputStream(),response.isScript);
client.close();
}
}
public static void startWith500()throws IOException{
socket = new ServerSocket( httpdConf.listen );
while( true ){
Socket client = socket.accept();
response = new Response(500,"Internal Server Error");
response.send(client.getOutputStream(),false);
client.close();
}
}
}