some changes
This commit is contained in:
parent
23349618ff
commit
d53ed81773
@ -14,51 +14,63 @@ import java.util.*;
|
||||
public abstract class FormDataHandler implements HttpHandler {
|
||||
@Override
|
||||
public void handle(HttpExchange httpExchange) throws IOException {
|
||||
Headers headers = httpExchange.getRequestHeaders();
|
||||
String contentType = headers.getFirst("Content-Type");
|
||||
String data = new String(httpExchange.getRequestBody().readAllBytes());
|
||||
Map<String, Object> params = new HashMap<>();
|
||||
System.out.println(httpExchange);
|
||||
|
||||
if (contentType.startsWith("multipart/form-data")) {
|
||||
String boundary = "--" + contentType.substring(contentType.indexOf("boundary=") + 9) + "\r\n";
|
||||
for (String part : data.split(boundary)) {
|
||||
String[] lines = part.split("\r\n");
|
||||
if (lines.length >= 3) {
|
||||
if (lines[0].startsWith("Content-Disposition: form-data; name=\"")) {
|
||||
String name = lines[0].substring("Content-Disposition: form-data; name=\"".length());
|
||||
name = name.substring(0, name.length() - 1);
|
||||
String value = lines[2];
|
||||
params.put(name, value);
|
||||
Headers headers = httpExchange.getRequestHeaders();
|
||||
|
||||
if (headers.containsKey("Content-type")) {
|
||||
Map<String, Object> params = new HashMap<>();
|
||||
|
||||
String contentType = headers.getFirst("Content-Type");
|
||||
String data = new String(httpExchange.getRequestBody().readAllBytes());
|
||||
|
||||
if (contentType.startsWith("multipart/form-data")) {
|
||||
String boundary = "--" + contentType.substring(contentType.indexOf("boundary=") + 9) + "\r\n";
|
||||
for (String part : data.split(boundary)) {
|
||||
String[] lines = part.split("\r\n");
|
||||
if (lines.length >= 3) {
|
||||
if (lines[0].startsWith("Content-Disposition: form-data; name=\"")) {
|
||||
String name = lines[0].substring("Content-Disposition: form-data; name=\"".length());
|
||||
name = name.substring(0, name.length() - 1);
|
||||
String value = lines[2];
|
||||
params.put(name, value);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} else if (contentType.startsWith("application/json")) {
|
||||
try {
|
||||
Object o = new JSONParser().parse(data);
|
||||
if (o instanceof Map) {
|
||||
for (Map.Entry<Object, Object> e : ((Map<Object, Object>) o).entrySet())
|
||||
params.put(e.getKey().toString(), e.getValue());
|
||||
} else {
|
||||
params.put("data", o);
|
||||
} else if (contentType.startsWith("application/json")) {
|
||||
try {
|
||||
Object o = new JSONParser().parse(data);
|
||||
if (o instanceof Map) {
|
||||
for (Map.Entry<Object, Object> e : ((Map<Object, Object>) o).entrySet())
|
||||
params.put(e.getKey().toString(), e.getValue());
|
||||
} else {
|
||||
params.put("data", o);
|
||||
}
|
||||
} catch (ParseException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
} catch (ParseException e) {
|
||||
} else if (contentType.startsWith("application/x-www-form-urlencoded")) {
|
||||
if (data.startsWith("?")) data = data.substring(1);
|
||||
for (String s : data.split("&")) {
|
||||
String[] ss = s.split("=");
|
||||
String k = URLDecoder.decode(ss[0], Charsets.UTF_8);
|
||||
String v = URLDecoder.decode(ss[1], Charsets.UTF_8);
|
||||
params.put(k, v);
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
handle(httpExchange, params, data);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
} else if (contentType.startsWith("application/x-www-form-urlencoded")) {
|
||||
if (data.startsWith("?")) data = data.substring(1);
|
||||
for (String s : data.split("&")) {
|
||||
String[] ss = s.split("=");
|
||||
String k = URLDecoder.decode(ss[0], Charsets.UTF_8);
|
||||
String v = URLDecoder.decode(ss[1], Charsets.UTF_8);
|
||||
params.put(k, v);
|
||||
} else {
|
||||
try {
|
||||
handle(httpExchange, new HashMap<>(), "");
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
handle(httpExchange, params, data);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public abstract void handle(HttpExchange httpExchange,Map<String,Object> parts,String data) throws IOException;
|
||||
|
@ -47,6 +47,8 @@ public class SitePart extends FormDataHandler {
|
||||
String method = e.getRequestMethod();
|
||||
String path = e.getRequestURI().getPath();
|
||||
|
||||
System.out.println(response+" "+status_code+" "+path+" "+method);
|
||||
|
||||
if (method.equals("GET")) {
|
||||
if (path.equals(Main.me.vote_page)) {
|
||||
if (params.containsKey("nickname") &&
|
||||
|
@ -1,6 +1,6 @@
|
||||
site_host: localhost # IP адрес для сайта
|
||||
site_port: 8080 # Порт для сайта
|
||||
site_backlog: 5 # Максимальное кол-во подключений одновременно
|
||||
site_backlog: 0 # Максимальное кол-во подключений одновременно
|
||||
|
||||
secret_token: "ваш_секретный_токен" # Секретный токен с FroggyMonitor
|
||||
|
||||
|
@ -1,4 +1,4 @@
|
||||
name: MonitoringReward
|
||||
name: FroggyMonitorReward
|
||||
version: '${project.version}'
|
||||
main: ru.froggymonitor.rewardplugin.Main
|
||||
api-version: 1.13
|
||||
|
Loading…
Reference in New Issue
Block a user