Atomize an incoming HTTP request
This commit is contained in:
parent
996eec6152
commit
c65858db6e
|
@ -3,63 +3,63 @@ package com.spookyinternet.cobweb;
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
|
||||||
class BufferManager {
|
|
||||||
private BufferedReader reader;
|
|
||||||
|
|
||||||
public BufferManager(BufferedReader reader) {
|
|
||||||
this.reader = reader;
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean runesMatch() {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void advance() {
|
|
||||||
}
|
|
||||||
|
|
||||||
public String str() {
|
|
||||||
return "";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
class HttpTokenStream implements Iterator<HttpToken> {
|
class HttpTokenStream implements Iterator<HttpToken> {
|
||||||
private BufferedReader reader;
|
private String payload;
|
||||||
private BufferManager buff;
|
private int idx;
|
||||||
int line = 1;
|
private int marker;
|
||||||
int seq = 0;
|
private int line;
|
||||||
|
private int seq;
|
||||||
|
|
||||||
public HttpTokenStream(BufferedReader reader) {
|
public HttpTokenStream(BufferedReader reader) {
|
||||||
this.reader = reader;
|
char[] buff = new char[4096];
|
||||||
this.buff = new BufferManager(reader);
|
int amt = 0;
|
||||||
|
|
||||||
|
try {
|
||||||
|
amt = reader.read(buff, 0, buff.length);
|
||||||
|
} catch(Exception _) {
|
||||||
|
// :)
|
||||||
|
}
|
||||||
|
|
||||||
|
this.payload = new String(buff, 0, amt);
|
||||||
|
this.idx = 1;
|
||||||
|
this.marker = 0;
|
||||||
|
this.line = 1;
|
||||||
|
this.seq = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean hasNext() {
|
public boolean hasNext() {
|
||||||
try {
|
return this.idx < this.payload.length();
|
||||||
return this.reader.ready();
|
|
||||||
} catch(Exception _) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public HttpToken next() {
|
public HttpToken next() {
|
||||||
while(this.buff.runesMatch()) {
|
HttpToken token = new HttpToken(this.line, null, this.seq == 1, false);
|
||||||
this.buff.advance();
|
|
||||||
}
|
|
||||||
|
|
||||||
this.seq += 1;
|
this.seq += 1;
|
||||||
int snapshot_line = this.line;
|
|
||||||
int snapshot_seq = this.seq;
|
|
||||||
String value = this.buff.str();
|
|
||||||
boolean eol = false;
|
|
||||||
|
|
||||||
if(value.endsWith("\r\n")) {
|
while(this.idx < this.payload.length()) {
|
||||||
|
char rune1 = this.payload.charAt(this.idx - 1);
|
||||||
|
char rune2 = this.payload.charAt(this.idx);
|
||||||
|
int snapshot_idx = this.idx;
|
||||||
|
this.idx += 1;
|
||||||
|
|
||||||
|
if(Character.isWhitespace(rune1) != Character.isWhitespace(rune2)) {
|
||||||
|
token.value = this.payload.substring(this.marker, snapshot_idx);
|
||||||
|
this.marker = snapshot_idx;
|
||||||
|
return token;
|
||||||
|
} else if(rune2 == '\n') {
|
||||||
|
token.value = this.payload.substring(this.marker, snapshot_idx);
|
||||||
|
this.marker = this.idx;
|
||||||
|
this.idx += 1;
|
||||||
this.line += 1;
|
this.line += 1;
|
||||||
this.seq = 0;
|
this.seq = 1;
|
||||||
eol = true;
|
token.eol = true;
|
||||||
|
return token;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return new HttpToken(snapshot_line, value, snapshot_seq == 1, eol);
|
token.value = this.payload.substring(this.marker);
|
||||||
|
token.eol = token.value.endsWith("\n");
|
||||||
|
return token;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue