package ru.REStudios.basics;
import ru.REStudios.utils.Strings;
import ru.REStudios.web.Page;
import ru.REStudios.web.PageHolder;
import java.io.*;
import java.net.Socket;
import java.net.SocketException;
import java.nio.charset.StandardCharsets;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Scanner;
public class ClientHandler implements Runnable{
private final Socket socket;
public String currentURL;
public PrintStream logStream;
public ClientHandler(Socket socket) throws IOException {
this.socket = socket;
new Thread(this).start();
File logFile = new File("logs",socket.getInetAddress().toString().replace(":", ".")+".log");
if(!logFile.exists()){
boolean crashTest = logFile.createNewFile();
if(!crashTest){
System.err.println("Can't create file, logging gave up");
}
}
logStream = new PrintStream(new FileOutputStream(logFile, true), true);
Log("Connected");
}
public void Log(String message){
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");
LocalDateTime now = LocalDateTime.now();
logStream.println("["+dtf.format(now)+"] "+message);
}
@Override
public void run() {
long s = System.currentTimeMillis();
try (InputStream in = socket.getInputStream(); OutputStream out = socket.getOutputStream()){
currentURL = getRequestUrl(in);
while (currentURL.startsWith("/")){
currentURL = currentURL.substring(1);
}
while (currentURL.endsWith("/")){
currentURL = currentURL.substring(0, currentURL.length()-1);
}
currentURL = currentURL.replaceAll("/\\?", "?");
if (currentURL.equals(" ") || currentURL.isEmpty()){
currentURL = "index";
}
Page page = PageHolder.getPage(Strings.before(currentURL, '?'));
HashMap<String, Page> pages = PageHolder.getPages();
for (String s1 : pages.keySet()) {
Page p = pages.get(s1);
if(p.isSoft()){
if(currentURL.startsWith(s1)){
page = p;
break;
}
}
}
if (page == null){
sendHeader("HTTP/1.1 404 NOT FOUND");
sendHeader("Connection: keep-alive");
sendHeader(out,ContentType.TEXT);
sendHeader("");
if (!socket.isClosed()){
try {
out.write("NOT FOUND".getBytes(StandardCharsets.UTF_8));
} catch (SocketException ignored) {}
}
}
else {
sendHeader("HTTP/1.1 200 OK");
sendHeader("Connection: keep-alive");
String sendContent = page.getSendContent(Strings.before(currentURL, '?'), new UrlParameters(currentURL), this)+"";
sendHeader(out,page.getType());
sendHeader("");
if (!socket.isClosed()){
try{
out.write(sendContent.getBytes(StandardCharsets.UTF_8));
} catch (SocketException ignored) {}
}
}
} catch (IOException e){
e.printStackTrace();
}
long end = System.currentTimeMillis();
Log("Response text loaded in "+(end-s)+"ms");
}
private String getRequestUrl(InputStream input) {
var reader = new Scanner(input).useDelimiter("\r\n");
String line;
try {
line = reader.nextLine();
} catch (Exception e) {
return "index";
}
return line.split(" ")[1];
}
private void sendHeader(OutputStream output, ContentType type) {
var ps = new PrintStream(output);
ps.printf("Content-Type: %s; charset=UTF-8%n", type.getHeader());
ps.flush();
}
@SuppressWarnings("unused")
public void sendHeader(String header){
PrintStream ps;
try {
ps = new PrintStream(socket.getOutputStream());
ps.printf(header);
ps.printf("%n");
ps.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
}
package ru.REStudios.basics;
import ru.REStudios.Main;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
public class Server implements Runnable {
private static Server _instance;
@SuppressWarnings("all")
public static Server getInstance() {
if (_instance == null){
_instance = new Server();
}
return _instance;
}
public final int port = 80;
private Server() {
Thread thread = new Thread(this);
thread.setName("Server Thread");
thread.setPriority(Main.ThreadPriority.NORMAL);
thread.start();
}
public static String jsRedirect(String to){
return "<script>" +
"document.location.href='"+to+"';" +
"</script>";
}
@Override
public void run() {
try (ServerSocket socket = new ServerSocket(port)){
//noinspection InfiniteLoopStatement
while (true){
//System.out.println("Waiting for connection..");
Socket accepted = socket.accept();
System.out.println("Accepted a socket from "+accepted.getInetAddress());
/*ClientHandler handler = */new ClientHandler(accepted);
}
} catch (IOException e){
e.printStackTrace();
}
}
}
-bash: apt: command not found
-bash-4.2$ cd ..
-bash-4.2$ dir
bin dev etc home lib lib64 opt proc root run sbin tmp usr var
-bash-4.2$ ^C
-bash-4.2$ yum
-bash: yum: command not found