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.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> {
|
||||
private BufferedReader reader;
|
||||
private BufferManager buff;
|
||||
int line = 1;
|
||||
int seq = 0;
|
||||
private String payload;
|
||||
private int idx;
|
||||
private int marker;
|
||||
private int line;
|
||||
private int seq;
|
||||
|
||||
public HttpTokenStream(BufferedReader reader) {
|
||||
this.reader = reader;
|
||||
this.buff = new BufferManager(reader);
|
||||
char[] buff = new char[4096];
|
||||
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
|
||||
public boolean hasNext() {
|
||||
try {
|
||||
return this.reader.ready();
|
||||
} catch(Exception _) {
|
||||
return false;
|
||||
}
|
||||
return this.idx < this.payload.length();
|
||||
}
|
||||
|
||||
@Override
|
||||
public HttpToken next() {
|
||||
while(this.buff.runesMatch()) {
|
||||
this.buff.advance();
|
||||
}
|
||||
|
||||
HttpToken token = new HttpToken(this.line, null, this.seq == 1, false);
|
||||
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")) {
|
||||
this.line += 1;
|
||||
this.seq = 0;
|
||||
eol = true;
|
||||
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.seq = 1;
|
||||
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