use of primal.MoreVerbs.Split in project suite by stupidsing.
the class HttpIo method readRequest.
public Request readRequest(InputStream is0) {
var ls = ReadLine.from(is0).split(" ");
var headers = readHeaders(is0);
return FixieArray.of(ls).map((method, url, protocol) -> {
Fun2<String, String, Request> requestFun = (host, pqs) -> Split.strl(pqs, "?").map((path0, query) -> {
var is1 = getContentStream(is0, headers);
var path1 = path0.startsWith("/") ? path0 : "/" + path0;
var path2 = ex(() -> URLDecoder.decode(path1, Utf8.charset));
return //
Equals.string(protocol, "HTTP/1.1") ? //
new Request(method, host, path2, query, headers, Pull.from_(is1)) : fail("only HTTP/1.1 is supported");
});
var pp = Split.string(url, "://");
return pp != null ? Split.strl(pp.v, "/").map(requestFun) : requestFun.apply("", url);
});
}
use of primal.MoreVerbs.Split in project suite by stupidsing.
the class HttpNio method listen.
private void listen(Reg reg) {
var rw = new Object() {
// 0 - read, 1 - write, 2 - close after all written
private int stage = 0;
private Bytes br = Bytes.empty;
private Bytes bw = Bytes.empty;
private Puller<Bytes> write;
private Source<Boolean> eater = () -> parseLine(line -> handleRequest1stLine(line.trim(), response -> {
var data = //
"HTTP/1.1 " + response.status + "\r\n" + response.headers.streamlet().map((k, v) -> k + ": " + v + "\r\n").toJoinedString() + "\r\n";
stage = 1;
if (response.body != null)
write = Puller.concat(Pull.from(data), response.body);
else {
bw = Bytes.of(data.getBytes(Utf8.charset));
response.write.f(bytes -> {
if (bytes != null)
bw = bw.append(bytes);
else
stage = 2;
listen();
});
}
}));
private void listen() {
if (!bw.isEmpty())
reg.listenWrite(() -> bw, this::written);
else if (stage == 0)
reg.listenRead(in -> {
read(in);
listen();
});
else if (stage == 1 && write != null)
reg.listenWrite(() -> bw = write.pull(), this::written);
else if (stage == 2)
reg.listenWrite(() -> null, null);
else if (stage == 3)
reg.listen(0, null, null, null);
}
private void read(Bytes in) {
if (in != null) {
br = br.append(in);
while (eater.g()) ;
} else
// closes connection
write = Puller.empty();
}
private void written(int n) {
if (0 <= n)
bw = bw.range(n);
else
stage = 3;
listen();
}
private void handleRequest1stLine(String line, Sink<Response> cb) {
var hrhl = handleRequestHeaderLine(lines -> handleRequestBody(line, lines, cb));
eater = () -> parseLine(hrhl);
}
private Sink<String> handleRequestHeaderLine(Sink<List<String>> cb) {
var lines = new ArrayList<String>();
return line0 -> {
var line1 = line0.trim();
if (!line1.isEmpty())
lines.add(line1);
else
cb.f(lines);
};
}
private void handleRequestBody(String line0, List<String> headerLines, Sink<Response> cb) {
eater = () -> //
FixieArray.of(//
line0.split(" ")).map((method, url, proto) -> handleRequestBody(proto, method, url, headerLines, cb));
}
private //
boolean handleRequestBody(//
String proto, //
String method, //
String url, //
List<String> lines, Sink<Response> cb) {
var headers = //
Read.from(//
lines).fold(new Header(), (headers_, line_) -> //
Split.strl(line_, //
":").map((k, v) -> headers_.put(k, v)));
var queue = new ArrayBlockingQueue<Bytes>(Buffer.size);
Sink<Bytes> offer = queue::add;
Source<Bytes> take = queue::poll;
Fun2<String, String, Request> requestFun = (host, pqs) -> Split.strl(pqs, "?").map((path0, query) -> {
var path1 = path0.startsWith("/") ? path0 : "/" + path0;
var path2 = ex(() -> URLDecoder.decode(path1, Utf8.charset));
return //
Equals.string(proto, "HTTP/1.1") ? //
new Request(method, host, path2, query, headers, Puller.of(take)) : fail("only HTTP/1.1 is supported");
});
var pp = Split.string(url, "://");
var request = pp != null ? Split.strl(pp.v, "/").map(requestFun) : requestFun.apply("", url);
var cl = request.headers.getOpt("Content-Length").map(Long::parseLong);
var te = Equals.ab(request.headers.getOpt("Transfer-Encoding"), Opt.of("chunked"));
Log_.info(request.getLogString());
if (te)
eater = handleChunkedRequestBody(request, offer, cb);
else if (cl.hasValue())
eater = handleRequestBody(request, offer, cl.g(), cb);
else
eater = handleRequestBody(request, offer, 0, cb);
return true;
}
private //
Source<Boolean> handleRequestBody(//
Request request, //
Sink<Bytes> body, //
long contentLength, Sink<Response> cb) {
return new Source<>() {
private int n;
public Boolean g() {
body.f(br);
var isOpen = br != null;
if (isOpen) {
n += br.size();
br = Bytes.empty;
}
if (!isOpen || contentLength <= n)
cb.f(handler.handle(request));
return false;
}
};
}
private Source<Boolean> handleChunkedRequestBody(Request request, Sink<Bytes> body, Sink<Response> cb) {
return () -> {
for (var i0 = 0; i0 < br.size(); i0++) if (br.get(i0) == 10) {
var line = new String(br.range(0, i0).toArray(), Utf8.charset);
var size = Integer.parseInt(line.trim(), 16);
for (var i1 = i0 + 1 + size; i1 < br.size(); i1++) if (br.get(i1) == 10) {
var chunk = br.range(i0 + 1, i1);
br = br.range(i1);
body.f(chunk);
return true;
}
if (size == 0)
cb.f(handler.handle(request));
}
return false;
};
}
private boolean parseLine(Sink<String> handleLine) {
for (var i = 0; i < br.size(); i++) if (br.get(i) == 10) {
var line = new String(br.range(0, i).toArray(), Utf8.charset);
br = br.range(i + 1);
handleLine.f(line);
return true;
}
return false;
}
};
rw.listen();
}
use of primal.MoreVerbs.Split in project suite by stupidsing.
the class AsmSl method assemble.
public Bytes assemble(String in0) {
var whitespaces = Collections.singleton('\n');
Fun<String, List<Run>> gct = CommentPreprocessor.ofGroupComment(whitespaces)::preprocess;
Fun<String, List<Run>> lct = CommentPreprocessor.ofLineComment(whitespaces)::preprocess;
var in1 = Preprocess.transform(List.of(gct, lct), in0).k;
var generalizer = new Generalizer();
var lines = List.of(in1.split("\n"));
Pair<String, String> pe;
var start = 0;
while ((pe = Split.string(lines.get(start), "=")) != null) {
generalizer.getVariable(Atom.of(pe.k)).bound(Suite.parse(pe.v));
start++;
}
var lnis = //
Read.from(//
Right.of(lines, start)).map(line -> Split.strl(line, "\t").map((label, command) -> {
var reference = //
Is.notBlank(label) ? //
generalizer.getVariable(Atom.of(label)) : new Reference();
var instruction = generalizer.generalize(Suite.parse(command));
return Pair.of(reference, instruction);
})).toList();
return assemble(generalizer, lnis);
}
use of primal.MoreVerbs.Split in project suite by stupidsing.
the class HttpIo method readResponse.
public Response readResponse(InputStream is0) {
var ls = ReadLine.from(is0).split(" ");
var headers = readHeaders(is0);
return FixieArray.of(ls).map((protocol, status) -> {
var is1 = //
headers.getOpt(//
"Content-Length").map(//
Integer::parseInt).map(//
cl -> sizeLimitedInputStream(is0, cl)).or(is0);
return //
Equals.string(protocol, "HTTP/1.1") ? //
new Response(status, headers, Pull.from(is1)) : fail("only HTTP/1.1 is supported");
});
}
Aggregations