Finished up WebServer-Protocol

This commit is contained in:
Finn
2025-12-12 18:19:56 +01:00
parent 4108b04582
commit e37a76af56
9 changed files with 213 additions and 563 deletions

View File

@@ -39,7 +39,7 @@ public final class INSQueryPacket extends OACPacket {
* @param clientId Sender client ID for routing.
*/
public INSQueryPacket(String tln, String name, String sub, INSRecordType type, int clientId) {
super(6, ProtocolVersion.PV_1_0_0_BETA);
super(5, ProtocolVersion.PV_1_0_0_BETA);
this.tln = tln;
this.name = name;
this.sub = sub;
@@ -51,7 +51,7 @@ public final class INSQueryPacket extends OACPacket {
* Registration constructor
*/
public INSQueryPacket() {
super(6, ProtocolVersion.PV_1_0_0_BETA);
super(5, ProtocolVersion.PV_1_0_0_BETA);
}
/**

View File

@@ -41,7 +41,7 @@ public final class INSResponsePacket extends OACPacket {
* @param bridge Protocol runtime context.
*/
public INSResponsePacket(INSResponseStatus status, List<INSRecord> records, int clientId, ProtocolBridge bridge) {
super(7, ProtocolVersion.PV_1_0_0_BETA);
super(6, ProtocolVersion.PV_1_0_0_BETA);
this.status = status;
this.records = records;
this.clientId = clientId;
@@ -54,7 +54,7 @@ public final class INSResponsePacket extends OACPacket {
* @param bridge Protocol runtime context.
*/
public INSResponsePacket(ProtocolBridge bridge) {
super(7, ProtocolVersion.PV_1_0_0_BETA);
super(6, ProtocolVersion.PV_1_0_0_BETA);
this.bridge = bridge;
}

View File

@@ -45,7 +45,7 @@ public final class UnsupportedClassicPacket extends OACPacket {
* Registration Constructor
*/
public UnsupportedClassicPacket() {
super(5, ProtocolVersion.PV_1_0_0_BETA);
super(7, ProtocolVersion.PV_1_0_0_BETA);
}
/**

View File

@@ -0,0 +1,56 @@
package org.openautonomousconnection.protocol.packets.v1_0_0.beta;
import dev.unlegitdqrk.unlegitlibrary.network.system.packets.PacketHandler;
import org.openautonomousconnection.protocol.packets.OACPacket;
import org.openautonomousconnection.protocol.versions.ProtocolVersion;
import org.openautonomousconnection.protocol.versions.v1_0_0.beta.WebRequestMethod;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Map;
public final class WebRequestPacket extends OACPacket {
private String path;
private WebRequestMethod method;
private Map<String,String> headers;
private byte[] body;
public WebRequestPacket() {
super(8, ProtocolVersion.PV_1_0_0_BETA);
}
public WebRequestPacket(String path, WebRequestMethod method, Map<String, String> headers, byte[] body) {
super(8, ProtocolVersion.PV_1_0_0_BETA);
this.path = path;
this.method = method;
this.headers = headers;
this.body = (body != null) ? body : new byte[0];
}
@Override
public void onWrite(PacketHandler handler, ObjectOutputStream out) throws IOException {
out.writeUTF(path != null ? path : "/");
out.writeUTF(method != null ? method.name() : WebRequestMethod.GET.name());
out.writeObject(headers);
if (body == null) body = new byte[0];
out.writeInt(body.length);
out.write(body);
}
@SuppressWarnings("unchecked")
@Override
public void onRead(PacketHandler handler, ObjectInputStream in) throws IOException, ClassNotFoundException {
this.path = in.readUTF();
this.method = WebRequestMethod.valueOf(in.readUTF());
this.headers = (Map<String, String>) in.readObject();
int len = in.readInt();
if (len < 0) {
throw new IOException("Negative body length in WebRequestPacket");
}
this.body = in.readNBytes(len);
}
}

View File

@@ -0,0 +1,54 @@
package org.openautonomousconnection.protocol.packets.v1_0_0.beta;
import dev.unlegitdqrk.unlegitlibrary.network.system.packets.PacketHandler;
import org.openautonomousconnection.protocol.packets.OACPacket;
import org.openautonomousconnection.protocol.versions.ProtocolVersion;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Map;
public final class WebResponsePacket extends OACPacket {
private int statusCode; // 200, 404, 500 ...
private String contentType; // text/ohtml, text/plain, application/json, text/py
private Map<String,String> headers;
private byte[] body;
public WebResponsePacket() {
super(9, ProtocolVersion.PV_1_0_0_BETA);
}
public WebResponsePacket(int statusCode, String contentType, Map<String, String> headers, byte[] body) {
super(9, ProtocolVersion.PV_1_0_0_BETA);
this.statusCode = statusCode;
this.contentType = contentType;
this.headers = headers;
this.body = (body != null) ? body : new byte[0];
}
@Override
public void onWrite(PacketHandler handler, ObjectOutputStream out) throws IOException {
out.writeInt(statusCode);
out.writeUTF(contentType != null ? contentType : "text/plain");
out.writeObject(headers);
if (body == null) body = new byte[0];
out.writeInt(body.length);
out.write(body);
}
@Override
public void onRead(PacketHandler handler, ObjectInputStream in) throws IOException, ClassNotFoundException {
this.statusCode = in.readInt();
this.contentType = in.readUTF();
this.headers = (Map<String, String>) in.readObject();
int len = in.readInt();
if (len < 0) {
throw new IOException("Negative body length in WebResponsePacket");
}
this.body = in.readNBytes(len);
}
}