Advertisement
Guest User

WebServerThread

a guest
Dec 4th, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.28 KB | None | 0 0
  1. package Web;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.File;
  5. import java.io.FileInputStream;
  6. import java.io.FileNotFoundException;
  7. import java.io.IOException;
  8. import java.io.InputStream;
  9. import java.io.InputStreamReader;
  10. import java.io.OutputStream;
  11. import java.io.PrintStream;
  12. import java.net.InetAddress;
  13. import java.net.Socket;
  14. import java.util.ArrayList;
  15. import java.util.HashMap;
  16.  
  17. public class WebServerThread extends Thread {
  18. private Socket socket;
  19. private HashMap<String, String> paramsHttp;
  20. private HashMap<String, String> headerHttp;
  21.  
  22. public WebServerThread(Socket skt) {
  23. this.socket = skt;
  24. this.paramsHttp = new HashMap<>();
  25. this.headerHttp = new HashMap<>();
  26.  
  27. try {
  28. start();
  29. } catch (Exception ex) {
  30. ex.printStackTrace();
  31. }
  32. }
  33.  
  34. public void run() {
  35. try {
  36. // InetAddress addr = socket.getInetAddress();
  37.  
  38. String resource = this.getResource(socket.getInputStream());
  39.  
  40. if (resource != null) {
  41. if (resource.equals(""))
  42. resource = "index.html";
  43. }
  44. // System.out.println("Request from " + addr.getHostName() + ": " + resource);
  45.  
  46. sendResponse(resource, socket.getOutputStream());
  47. socket.close();
  48. socket = null;
  49. } catch (Exception ex) {
  50. ex.printStackTrace();
  51. }
  52. }
  53.  
  54. String getResource(InputStream is) throws IOException {
  55. BufferedReader dis = new BufferedReader(new InputStreamReader(is));
  56. String s = dis.readLine();
  57. System.out.println(s);
  58.  
  59. String[] tokens = s.split(" ");
  60. // prva linija HTTP zahteva: METOD /resurs HTTP/verzija
  61. // obradjujemo samo GET metodu
  62. String method = tokens[0];
  63. // if (!method.equals("GET")) {
  64. // return null;
  65. // }
  66.  
  67. String rsrc = tokens[1];
  68.  
  69. // izbacimo znak '/' sa pocetka
  70. rsrc = rsrc.substring(1);
  71. if (method.equals("GET")) {
  72. if (!rsrc.equals("")) {
  73. if (rsrc.contains("?")) {
  74. extractGetParameters(rsrc);
  75. int indeks = rsrc.indexOf("?");
  76. rsrc = rsrc.substring(0, indeks);
  77. System.out.println("\n\n\n" + rsrc);
  78. }
  79. }
  80. }
  81. // ignorisemo ostatak zaglavlja
  82. String s1;
  83. String[] s2;
  84. while (!(s1 = dis.readLine()).equals("")) {
  85. System.out.println(s1);
  86. s2 = s1.split(": ");
  87. this.headerHttp.put(s2[0], s2[1]);
  88. }
  89. if (method.equals("POST")) {
  90. extractPostParameters(dis);
  91. }
  92.  
  93. return rsrc;
  94. }
  95.  
  96. private void extractGetParameters(String rsrc) {
  97. int indeks = rsrc.indexOf("?");
  98. String line = rsrc.substring(indeks + 1);
  99. String[] params = line.split("&");
  100. for (String string : params) {
  101. String[] p = string.split("=");
  102. if (p.length == 2) {
  103. this.getParamsHttp().put(p[0], p[1]);
  104. }
  105. }
  106.  
  107. }
  108.  
  109. private void extractPostParameters(BufferedReader dis) throws IOException {
  110. String lenS = this.getHeaderHttp().get("Content-Length");
  111. String type = this.getHeaderHttp().get("Content-Type");
  112. int len = 0;
  113. try {
  114. len = Integer.parseInt(lenS);
  115. } catch (Exception e) {
  116. // TODO: handle exception
  117. }
  118. if (len > 0 && type.equalsIgnoreCase("application/x-www-form-urlencoded")) {
  119.  
  120. char[] buff = new char[len];
  121. String s1;
  122.  
  123. dis.read(buff, 0, len);
  124. s1 = new String(buff);
  125. String[] params = s1.split("&");
  126. for (String line : params) {
  127. String[] vr = line.split("=");
  128. System.out.println("\n\n " + vr[0] + " " + vr[1]);
  129. this.getParamsHttp().put(vr[0].trim(), vr[1].trim());
  130. }
  131. }
  132.  
  133. }
  134.  
  135. void sendResponse(String resource, OutputStream os) throws IOException {
  136.  
  137. if (resource.equals("index.html")) {
  138. fileResponse(resource, os);
  139. } else if (resource.equals("reguser.html")) {
  140. fileResponse(resource, os);
  141. } else if (resource.equals("reguser")) {
  142. onPostMethod(resource, os);
  143. } else if (resource.equals("finduser")) {
  144. onGettMethod(resource, os);
  145. } else if (resource.equals("finduser.html")) {
  146. fileResponse(resource, os);
  147.  
  148. } else {
  149. PrintStream ps = new PrintStream(os);
  150. ps.print("HTTP/1.0 404 Error\r\n" + "Content-type: text/html; charset=UTF-8\r\n\r\n<b>404 nisam nasao:"
  151. + "</b>");
  152. ps.close();
  153. ps.flush();
  154. }
  155.  
  156. }
  157.  
  158. private void onGettMethod(String resource, OutputStream os) {
  159.  
  160. PrintStream ps = new PrintStream(os);
  161. String response = "";
  162. response += "HTTP/1.1 200 OK \r\n\r\n";
  163. response += "<html><head></head><body>";
  164. User user = null;
  165. if (!WebServer.users.isEmpty()) {
  166. for (User s : WebServer.users) {
  167. if(!WebServer.users.isEmpty()) {
  168. System.out.println("\n\n\n" + this.getParamsHttp().get("username"));
  169. if ((s.getUsername().equals(this.getParamsHttp().get("username")))) {
  170. user = s;
  171. break;
  172. }
  173. }
  174.  
  175. }
  176. }
  177. if (user == null) {
  178. //response = "";
  179. // response += "HTTP/1.1 400 Bad Request \r\n\r\n";
  180. // response += "<html><head></head><body>";
  181. response += "<p> Korisnik nije pronadjen.</p>";
  182. } else {
  183. response += "<p> Username : " + user.getUsername() + " <br>Password : " + user.getPassword() + "</p><br>";
  184. }
  185. response += "<a href=index.html><button>Home></button></a></body></html>";
  186. ps.println(response);
  187. ps.flush();
  188. }
  189.  
  190. private void onPostMethod(String resource, OutputStream os) {
  191. PrintStream ps = new PrintStream(os);
  192. String response = "";
  193. response += "HTTP/1.0 200 OK\r\n\r\n";
  194. response += "<html><head></head><body>";
  195.  
  196. User user = null;
  197. for (User u : WebServer.users) {
  198.  
  199. if (u.getUsername().equals(this.getParamsHttp().get("username"))) {
  200.  
  201. user = u;
  202. break;
  203. }
  204. }
  205. if (user == null) {
  206. User n = new User();
  207. n.setUsername(this.getParamsHttp().get("username"));
  208. n.setPassword(this.getParamsHttp().get("password"));
  209. WebServer.users.add(n);
  210. response += "<p> Korisnik uspesno dodat </p> <br> <a href =" + "index.html" + "><button>Home</button></a>";
  211. } else
  212. response += "<p> Korisnik vec postoji</p> <br> <a href =" + "index.html" + "><button>Home</button></a>";
  213. response += "</body></html>";
  214. ps.println(response);
  215. ps.flush();
  216.  
  217. }
  218.  
  219. private void fileResponse(String resource, OutputStream os) throws IOException {
  220. PrintStream ps = new PrintStream(os);
  221. // zamenimo web separator sistemskim separatorom
  222. resource = resource.replace('/', File.separatorChar);
  223. File file = new File(resource);
  224.  
  225. if (!file.exists()) {
  226. // ako datoteka ne postoji, vratimo kod za gresku
  227. ps.print("HTTP/1.0 404 File not found\r\n"
  228. + "Content-type: text/html; charset=UTF-8\r\n\r\n<b>404 Нисам нашао:" + file.getName() + "</b>");
  229. // ps.flush();
  230. System.out.println("Could not find resource: " + file);
  231. return;
  232. }
  233.  
  234. // ispisemo zaglavlje HTTP odgovora
  235. ps.print("HTTP/1.0 200 OK\r\n\r\n");
  236.  
  237. // a, zatim datoteku
  238. FileInputStream fis = new FileInputStream(file);
  239. byte[] data = new byte[8192];
  240. int len;
  241.  
  242. while ((len = fis.read(data)) != -1) {
  243. ps.write(data, 0, len);
  244. }
  245.  
  246. ps.flush();
  247. fis.close();
  248. }
  249.  
  250. public Socket getSocket() {
  251. return socket;
  252. }
  253.  
  254. public void setSocket(Socket socket) {
  255. this.socket = socket;
  256. }
  257.  
  258. public HashMap<String, String> getParamsHttp() {
  259. return paramsHttp;
  260. }
  261.  
  262. public void setParamsHttp(HashMap<String, String> paramsHttp) {
  263. this.paramsHttp = paramsHttp;
  264. }
  265.  
  266. public HashMap<String, String> getHeaderHttp() {
  267. return headerHttp;
  268. }
  269.  
  270. public void setHeaderHttp(HashMap<String, String> headerHttp) {
  271. this.headerHttp = headerHttp;
  272. }
  273.  
  274. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement