-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWorker.java
More file actions
61 lines (54 loc) · 1.9 KB
/
Worker.java
File metadata and controls
61 lines (54 loc) · 1.9 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
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package webserver;
import Config.HttpdConf;
import Config.MimeTypes;
import Response.ResponseBase;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.lang.ProcessBuilder.Redirect;
import java.net.Socket;
/**
*
* @author Sapan
*/
public class Worker implements Runnable {
PrintWriter out;
BufferedReader in;
Socket clientSocket = null;
MimeTypes mime_object;
HttpdConf httpd_object;
Worker(Socket clientSocket, HttpdConf httpd_object, MimeTypes mime_object) {
this.clientSocket = clientSocket;
this.httpd_object = httpd_object;
this.mime_object = mime_object;
}
@Override
public void run() {
try {
out = new PrintWriter(clientSocket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
Request request = new Request(in);
Resource resource = new Resource(request, httpd_object.Aliases, httpd_object.ScriptAliases);
ResponseFactory responseFactory = new ResponseFactory(resource);
ResponseBase response = responseFactory.create(resource);
if (response.byteData != null) { //checking if requested file has images
clientSocket.getOutputStream().write(response.writeString().getBytes());
clientSocket.getOutputStream().write(response.byteData);
clientSocket.getOutputStream().flush();
}
else {
out.print(response.writeString());
out.flush();
}
out.close();
}
catch (Exception e) {
System.out.println(e);
}
}
}