Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Web;
- import java.io.BufferedReader;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.InputStreamReader;
- import java.io.OutputStream;
- import java.io.PrintStream;
- import java.net.InetAddress;
- import java.net.Socket;
- import java.util.ArrayList;
- import java.util.HashMap;
- public class WebServerThread extends Thread {
- private Socket socket;
- private HashMap<String, String> paramsHttp;
- private HashMap<String, String> headerHttp;
- public WebServerThread(Socket skt) {
- this.socket = skt;
- this.paramsHttp = new HashMap<>();
- this.headerHttp = new HashMap<>();
- try {
- start();
- } catch (Exception ex) {
- ex.printStackTrace();
- }
- }
- public void run() {
- try {
- // InetAddress addr = socket.getInetAddress();
- String resource = this.getResource(socket.getInputStream());
- if (resource != null) {
- if (resource.equals(""))
- resource = "index.html";
- }
- // System.out.println("Request from " + addr.getHostName() + ": " + resource);
- sendResponse(resource, socket.getOutputStream());
- socket.close();
- socket = null;
- } catch (Exception ex) {
- ex.printStackTrace();
- }
- }
- String getResource(InputStream is) throws IOException {
- BufferedReader dis = new BufferedReader(new InputStreamReader(is));
- String s = dis.readLine();
- System.out.println(s);
- String[] tokens = s.split(" ");
- // prva linija HTTP zahteva: METOD /resurs HTTP/verzija
- // obradjujemo samo GET metodu
- String method = tokens[0];
- // if (!method.equals("GET")) {
- // return null;
- // }
- String rsrc = tokens[1];
- // izbacimo znak '/' sa pocetka
- rsrc = rsrc.substring(1);
- if (method.equals("GET")) {
- if (!rsrc.equals("")) {
- if (rsrc.contains("?")) {
- extractGetParameters(rsrc);
- int indeks = rsrc.indexOf("?");
- rsrc = rsrc.substring(0, indeks);
- System.out.println("\n\n\n" + rsrc);
- }
- }
- }
- // ignorisemo ostatak zaglavlja
- String s1;
- String[] s2;
- while (!(s1 = dis.readLine()).equals("")) {
- System.out.println(s1);
- s2 = s1.split(": ");
- this.headerHttp.put(s2[0], s2[1]);
- }
- if (method.equals("POST")) {
- extractPostParameters(dis);
- }
- return rsrc;
- }
- private void extractGetParameters(String rsrc) {
- int indeks = rsrc.indexOf("?");
- String line = rsrc.substring(indeks + 1);
- String[] params = line.split("&");
- for (String string : params) {
- String[] p = string.split("=");
- if (p.length == 2) {
- this.getParamsHttp().put(p[0], p[1]);
- }
- }
- }
- private void extractPostParameters(BufferedReader dis) throws IOException {
- String lenS = this.getHeaderHttp().get("Content-Length");
- String type = this.getHeaderHttp().get("Content-Type");
- int len = 0;
- try {
- len = Integer.parseInt(lenS);
- } catch (Exception e) {
- // TODO: handle exception
- }
- if (len > 0 && type.equalsIgnoreCase("application/x-www-form-urlencoded")) {
- char[] buff = new char[len];
- String s1;
- dis.read(buff, 0, len);
- s1 = new String(buff);
- String[] params = s1.split("&");
- for (String line : params) {
- String[] vr = line.split("=");
- System.out.println("\n\n " + vr[0] + " " + vr[1]);
- this.getParamsHttp().put(vr[0].trim(), vr[1].trim());
- }
- }
- }
- void sendResponse(String resource, OutputStream os) throws IOException {
- if (resource.equals("index.html")) {
- fileResponse(resource, os);
- } else if (resource.equals("reguser.html")) {
- fileResponse(resource, os);
- } else if (resource.equals("reguser")) {
- onPostMethod(resource, os);
- } else if (resource.equals("finduser")) {
- onGettMethod(resource, os);
- } else if (resource.equals("finduser.html")) {
- fileResponse(resource, os);
- } else {
- PrintStream ps = new PrintStream(os);
- ps.print("HTTP/1.0 404 Error\r\n" + "Content-type: text/html; charset=UTF-8\r\n\r\n<b>404 nisam nasao:"
- + "</b>");
- ps.close();
- ps.flush();
- }
- }
- private void onGettMethod(String resource, OutputStream os) {
- PrintStream ps = new PrintStream(os);
- String response = "";
- response += "HTTP/1.1 200 OK \r\n\r\n";
- response += "<html><head></head><body>";
- User user = null;
- if (!WebServer.users.isEmpty()) {
- for (User s : WebServer.users) {
- if(!WebServer.users.isEmpty()) {
- System.out.println("\n\n\n" + this.getParamsHttp().get("username"));
- if ((s.getUsername().equals(this.getParamsHttp().get("username")))) {
- user = s;
- break;
- }
- }
- }
- }
- if (user == null) {
- //response = "";
- // response += "HTTP/1.1 400 Bad Request \r\n\r\n";
- // response += "<html><head></head><body>";
- response += "<p> Korisnik nije pronadjen.</p>";
- } else {
- response += "<p> Username : " + user.getUsername() + " <br>Password : " + user.getPassword() + "</p><br>";
- }
- response += "<a href=index.html><button>Home></button></a></body></html>";
- ps.println(response);
- ps.flush();
- }
- private void onPostMethod(String resource, OutputStream os) {
- PrintStream ps = new PrintStream(os);
- String response = "";
- response += "HTTP/1.0 200 OK\r\n\r\n";
- response += "<html><head></head><body>";
- User user = null;
- for (User u : WebServer.users) {
- if (u.getUsername().equals(this.getParamsHttp().get("username"))) {
- user = u;
- break;
- }
- }
- if (user == null) {
- User n = new User();
- n.setUsername(this.getParamsHttp().get("username"));
- n.setPassword(this.getParamsHttp().get("password"));
- WebServer.users.add(n);
- response += "<p> Korisnik uspesno dodat </p> <br> <a href =" + "index.html" + "><button>Home</button></a>";
- } else
- response += "<p> Korisnik vec postoji</p> <br> <a href =" + "index.html" + "><button>Home</button></a>";
- response += "</body></html>";
- ps.println(response);
- ps.flush();
- }
- private void fileResponse(String resource, OutputStream os) throws IOException {
- PrintStream ps = new PrintStream(os);
- // zamenimo web separator sistemskim separatorom
- resource = resource.replace('/', File.separatorChar);
- File file = new File(resource);
- if (!file.exists()) {
- // ako datoteka ne postoji, vratimo kod za gresku
- ps.print("HTTP/1.0 404 File not found\r\n"
- + "Content-type: text/html; charset=UTF-8\r\n\r\n<b>404 Нисам нашао:" + file.getName() + "</b>");
- // ps.flush();
- System.out.println("Could not find resource: " + file);
- return;
- }
- // ispisemo zaglavlje HTTP odgovora
- ps.print("HTTP/1.0 200 OK\r\n\r\n");
- // a, zatim datoteku
- FileInputStream fis = new FileInputStream(file);
- byte[] data = new byte[8192];
- int len;
- while ((len = fis.read(data)) != -1) {
- ps.write(data, 0, len);
- }
- ps.flush();
- fis.close();
- }
- public Socket getSocket() {
- return socket;
- }
- public void setSocket(Socket socket) {
- this.socket = socket;
- }
- public HashMap<String, String> getParamsHttp() {
- return paramsHttp;
- }
- public void setParamsHttp(HashMap<String, String> paramsHttp) {
- this.paramsHttp = paramsHttp;
- }
- public HashMap<String, String> getHeaderHttp() {
- return headerHttp;
- }
- public void setHeaderHttp(HashMap<String, String> headerHttp) {
- this.headerHttp = headerHttp;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement