fix minisrv

This commit is contained in:
GiantLuigi4 2024-10-17 23:07:02 -04:00
parent 06e722cb7c
commit 6e712fc8da
3 changed files with 69 additions and 44 deletions

View File

@ -16,8 +16,8 @@ import org.cef.misc.StringRef;
import org.cef.network.CefRequest; import org.cef.network.CefRequest;
import org.cef.network.CefResponse; import org.cef.network.CefResponse;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder; import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.util.UUID; import java.util.UUID;
public class WDScheme implements CefResourceHandler { public class WDScheme implements CefResourceHandler {
@ -36,61 +36,67 @@ public class WDScheme implements CefResourceHandler {
public boolean processRequest(CefRequest cefRequest, CefCallback cefCallback) { public boolean processRequest(CefRequest cefRequest, CefCallback cefCallback) {
url = cefRequest.getURL(); url = cefRequest.getURL();
url = url.substring("webdisplays://".length());
int pos = url.indexOf('/'); int pos = url.indexOf('/');
if(pos < 0) if (pos < 0)
return false; return false;
String uuidStr = url.substring(0, pos); String uuidStr = url.substring(0, pos);
String fileStr = url.substring(pos + 1); String fileStr = url.substring(pos + 1);
try { fileStr = URLDecoder.decode(fileStr, StandardCharsets.UTF_8);
fileStr = URLDecoder.decode(fileStr, "UTF-8");
} catch(UnsupportedEncodingException ex) {
Log.warningEx("UTF-8 isn't supported... yeah... and I'm a billionaire...", ex);
}
if(uuidStr.isEmpty() || Util.isFileNameInvalid(fileStr)) if (uuidStr.isEmpty() || Util.isFileNameInvalid(fileStr))
return false; return false;
UUID uuid; UUID uuid;
try { try {
uuid = UUID.fromString(uuidStr); uuid = UUID.fromString(uuidStr);
} catch(IllegalArgumentException ex) { } catch (IllegalArgumentException ex) {
return false; //Invalid UUID return false; //Invalid UUID
} }
task = new ClientTaskGetFile(uuid, fileStr); task = new ClientTaskGetFile(uuid, fileStr);
return Client.getInstance().addTask(task) ? true : false; boolean doContinue = Client.getInstance().addTask(task);
if (doContinue) cefCallback.Continue();
return doContinue;
} }
@Override @Override
public void getResponseHeaders(CefResponse cefResponse, IntRef intRef, StringRef stringRef) { public void getResponseHeaders(CefResponse cefResponse, IntRef contentLength, StringRef redir) {
Log.info("Waiting for response..."); Log.info("Waiting for response...");
int status = task.waitForResponse(); int status = task.waitForResponse();
Log.info("Got response %d", status); Log.info("Got response %d", status);
if(status == 0) { if (status == 0) {
//OK //OK
int extPos = task.getFileName().lastIndexOf('.'); int extPos = task.getFileName().lastIndexOf('.');
if(extPos >= 0) { if (extPos >= 0) {
String mime = mapMime(task.getFileName().substring(extPos + 1)); String mime = mapMime(task.getFileName().substring(extPos + 1));
if(mime != null) if (mime != null)
cefResponse.setMimeType(mime); cefResponse.setMimeType(mime);
} }
cefResponse.setStatus(200); cefResponse.setStatus(200);
cefResponse.setStatusText("OK"); cefResponse.setStatusText("OK");
cefResponse.setHeaderByName("content-length", "" + -1, true); contentLength.set(0);
return; return;
} }
int errCode; int errCode;
String errStr; String errStr;
if(status == Constants.GETF_STATUS_NOT_FOUND) { if (status == Constants.GETF_STATUS_NOT_FOUND) {
errCode = 404; errCode = 404;
errStr = "Not Found"; errStr = "Not Found";
} else if (status == Constants.GETF_STATUS_TIMED_OUT) {
errCode = 408;
errStr = "Timed Out";
} else if (status == Constants.GETF_STATUS_BAD_NAME) {
errCode = 418;
errStr = "I'm a teapot";
} else { } else {
errCode = 500; errCode = 500;
errStr = "Internal Server Error"; errStr = "Internal Server Error";
@ -99,16 +105,11 @@ public class WDScheme implements CefResourceHandler {
cefResponse.setStatus(errCode); cefResponse.setStatus(errCode);
cefResponse.setStatusText(errStr); cefResponse.setStatusText(errStr);
try { dataToWrite = String.format(ERROR_PAGE, errCode, errStr).getBytes(StandardCharsets.UTF_8);
dataToWrite = String.format(ERROR_PAGE, errCode, errStr).getBytes("UTF-8"); dataOffset = 0;
dataOffset = 0; amountToWrite = dataToWrite.length;
amountToWrite = dataToWrite.length; isErrorPage = true;
isErrorPage = true; contentLength.set(amountToWrite);
cefResponse.setHeaderByName("content-length", "" + amountToWrite, true);
} catch(UnsupportedEncodingException ex) {
cefResponse.setHeaderByName("content-length", "" + 0, true);
// cefResponse.setResponseLength(0);
}
} }
private byte[] dataToWrite; private byte[] dataToWrite;
@ -116,36 +117,37 @@ public class WDScheme implements CefResourceHandler {
private int amountToWrite; private int amountToWrite;
@Override @Override
public boolean readResponse(byte[] bytes, int i, IntRef intRef, CefCallback cefCallback) { public boolean readResponse(byte[] output, int bytesToRead, IntRef bytesRead, CefCallback cefCallback) {
if(dataToWrite == null) { if (dataToWrite == null) {
if(isErrorPage) { if (isErrorPage) {
// data.setAmountRead(0); dataToWrite = null;
return false; bytesRead.set(0);
return true;
} }
dataToWrite = task.waitForData(); dataToWrite = task.waitForData();
dataOffset = 3; //packet ID + size dataOffset = 3; //packet ID + size
amountToWrite = task.getDataLength(); amountToWrite = task.getDataLength();
if(amountToWrite <= 0) { if (amountToWrite <= 0) {
dataToWrite = null; dataToWrite = null;
// data.setAmountRead(0); bytesRead.set(0);
return false; return true;
} }
} }
// int toWrite = data.getBytesToRead(); int toWrite = bytesToRead;
// if(toWrite > amountToWrite) if (toWrite > amountToWrite)
// toWrite = amountToWrite; toWrite = amountToWrite;
// System.arraycopy(dataToWrite, dataOffset, data.getDataArray(), 0, toWrite); System.arraycopy(dataToWrite, dataOffset, output, 0, toWrite);
// data.setAmountRead(toWrite); bytesRead.set(toWrite);
// dataOffset += toWrite; dataOffset += toWrite;
// amountToWrite -= toWrite; amountToWrite -= toWrite;
if(amountToWrite <= 0) { if (amountToWrite <= 0) {
if(!isErrorPage) if (!isErrorPage)
task.nextData(); task.nextData();
dataToWrite = null; dataToWrite = null;
@ -156,6 +158,8 @@ public class WDScheme implements CefResourceHandler {
@Override @Override
public void cancel() { public void cancel() {
System.out.println("Scheme query canceled.");
task.cancel();
} }
public static String mapMime(String ext) { public static String mapMime(String ext) {

View File

@ -51,7 +51,7 @@ public class ClientTaskCheckFile extends ClientTask<ClientTaskCheckFile> {
public String getURL() { public String getURL() {
try { try {
return ((new StringBuilder("wd://"))).append(uuid.toString()).append('/').append(URLEncoder.encode(fname, "UTF-8")).toString(); return ((new StringBuilder("webdisplays://"))).append(uuid.toString()).append('/').append(URLEncoder.encode(fname, "UTF-8")).toString();
} catch(UnsupportedEncodingException ex) { } catch(UnsupportedEncodingException ex) {
ex.printStackTrace(); ex.printStackTrace();
return "hi"; return "hi";

View File

@ -59,6 +59,24 @@
#wd { #wd {
padding-left: 20px; padding-left: 20px;
} }
.info {
position: fixed;
bottom: 0px;
right: 0px;
padding: 10px;
}
@font-face {
font-family: 'CustomFont';
src: url('https://github.com/IdreesInc/Minecraft-Font/blob/main/Minecraft.otf') format('opentype');
}
.no_margin {
margin: 0px;
font-family: 'CustomFont', Arial, sans-serif;
color: #5555ff;
}
</style> </style>
</head> </head>
<body> <body>
@ -78,5 +96,8 @@
</td> </td>
</tr> </tr>
</table> </table>
<div class="info"><p class="no_margin">
Crouch+Click on the screen to set the URL
</p></div>
</body> </body>
</html> </html>