peer-at-code-framework/src/dev/peerat/framework/HttpReader.java

169 lines
No EOL
4.1 KiB
Java

package dev.peerat.framework;
import java.io.IOException;
import java.io.InputStream;
import java.net.Socket;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import dev.peerat.framework.utils.json.Json;
import dev.peerat.framework.utils.json.JsonParser;
public class HttpReader{
private static Pattern HEADER_PATTERN = Pattern.compile("^([^:]*):\\s+(.*)$");
static JsonParser JSON_PARSER = new JsonParser();
private Socket socket;
private InputStream in;
private StrictReader reader;
private Map<String, String> headers;
public HttpReader(Socket socket) throws Exception{
this.socket = socket;
this.in = socket.getInputStream();
this.reader = new StrictReader(this.in);
this.headers = new HashMap<>();
}
public HttpReader(HttpReader origin) throws Exception{
this.socket = origin.socket;
this.in = origin.in;
this.reader = origin.reader;
}
public boolean isClosed(){
return this.socket.isClosed();
}
void readHeaders() throws Exception{
String line;
while(((line = reader.readLine()) != null) && (line.length() > 0)){
Matcher matcher = HEADER_PATTERN.matcher(line);
matcher.matches();
this.headers.put(matcher.group(1).toLowerCase(), matcher.group(2));
}
}
public Set<String> getHeaders(){
return this.headers.keySet();
}
public String getHeader(String key){
return this.headers.get(key.toLowerCase());
}
public int read(byte[] buffer) throws IOException{
return this.in.read(buffer);
}
public int read(char[] buffer) throws IOException{
return this.reader.read(buffer, 0, buffer.length);
}
public String readLine() throws IOException{
return this.reader.readLine();
}
public boolean ready() throws IOException{
return this.reader.ready();
}
public int readInt() throws Exception{
int result = 0;
result += this.in.read() << 24;
result += this.in.read() << 16;
result += this.in.read() << 8;
result += this.in.read();
return result;
}
public <J extends Json> J readJson() throws Exception{
int length = Integer.parseInt(this.headers.get("content-length"));
//to limit
char[] content = new char[length];
read(content);
return JSON_PARSER.parse(new String(content));
}
/*
*
------WebKitFormBoundaryNUjiLBAMuX2dhxU7
Content-Disposition: form-data; name="answer"
12
------WebKitFormBoundaryNUjiLBAMuX2dhxU7
Content-Disposition: form-data; name="filename"
webpack.js
------WebKitFormBoundaryNUjiLBAMuX2dhxU7
Content-Disposition: form-data; name="code_file"; filename="webpack.js"
Content-Type: text/javascript
------WebKitFormBoundaryNUjiLBAMuX2dhxU7--
*
*/
public List<String> readMultiPartData() throws Exception{
List<String> list = new ArrayList<>();
String boundary = getHeader("content-type");
if(boundary == null) return list;
boundary = boundary.split(";")[1].split("=")[1];
readLine();
while(ready()){
String line;
while (((line = readLine()) != null) && (line.length() > 0));
String buffer = "";
while (((line = readLine()) != null) && (!line.startsWith("--"+boundary))){
buffer += line+"\n";
}
if(buffer.length() < 1) buffer = "\n";
list.add(buffer.substring(0, buffer.length()-1));
}
return list;
}
public byte[] readMultipartFile() throws Exception{
int length = Integer.parseInt(getHeader("content-length"));
int headerLength = readLine().length()+2;
length-=headerLength;
for(int i = 0; i < 2; i++){
length-=(readLine().length()+2);
}
byte[] ptn = new byte[2];
read(ptn);
length-=2;
if(ptn[0] == 10){
read(new byte[1]);
length-=1;
}
byte[] file = new byte[0];
while(length > 0){
byte[] buffer = new byte[length];
int read = read(buffer);
if(read < 0) throw new IndexOutOfBoundsException("read -1 on HttpReader");
length-=read;
byte[] copy = new byte[file.length+read];
System.arraycopy(file, 0, copy, 0, file.length);
System.arraycopy(buffer, 0, copy, file.length, read);
file = copy;
}
byte[] cleaned = new byte[file.length-(headerLength+4)];
System.arraycopy(file, 0, cleaned, 0, cleaned.length);
return cleaned;
}
}