Implement basic HTTP tokenizer

This commit is contained in:
Nick Chambers 2024-04-14 09:45:10 -05:00
parent 3484070e58
commit 996eec6152
1 changed files with 65 additions and 0 deletions

View File

@ -0,0 +1,65 @@
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;
public HttpTokenStream(BufferedReader reader) {
this.reader = reader;
this.buff = new BufferManager(reader);
}
@Override
public boolean hasNext() {
try {
return this.reader.ready();
} catch(Exception _) {
return false;
}
}
@Override
public HttpToken next() {
while(this.buff.runesMatch()) {
this.buff.advance();
}
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;
}
return new HttpToken(snapshot_line, value, snapshot_seq == 1, eol);
}
}