Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clean up HTTP async iterator code #411

Merged
merged 22 commits into from
May 20, 2019
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Bring back readRequest
ry committed May 18, 2019
commit 49d5a6b2bdcd4bc7b1786acfafae454bb60ec524
56 changes: 31 additions & 25 deletions http/server.ts
Original file line number Diff line number Diff line change
@@ -248,6 +248,34 @@ export class ServerRequest {
}
}

async function readRequest(
httpConn: HttpConn,
bufr: BufReader
): Promise<[ServerRequest, BufState]> {
const req = new ServerRequest();

// Set and incr pipeline id;
req.pipelineId = ++httpConn.lastPipelineId;
// Set a new pipeline deferred associated with this request
// for future requests to wait for.
httpConn.pendingDeferredMap.set(req.pipelineId, deferred());

req.conn = httpConn;
req.r = bufr;
req.w = new BufWriter(httpConn);
const tp = new TextProtoReader(bufr);
let err: BufState;
// First line: GET /index.html HTTP/1.0
let firstLine: string;
[firstLine, err] = await tp.readLine();
if (err) {
return [null, err];
}
[req.method, req.url, req.proto] = firstLine.split(" ", 3);
[req.headers, err] = await tp.readMIMEHeader();
return [req, err];
}

/** Continuously read more requests from conn until EOF
* bufr is empty on a fresh TCP connection.
* Would be passed around and reused for later request on same conn
@@ -259,34 +287,12 @@ async function* iterateHttpRequests(
conn: Conn
): AsyncIterableIterator<ServerRequest> {
const httpConn = createHttpConn(conn);

const bufr = new BufReader(httpConn);
const bufw = new BufWriter(httpConn);
const tp = new TextProtoReader(bufr);

let bufStateErr: BufState;
let req: ServerRequest;
for (;;) {
const req = new ServerRequest();

// Set and incr pipeline id;
req.pipelineId = ++httpConn.lastPipelineId;
// Set a new pipeline deferred associated with this request
// for future requests to wait for.
httpConn.pendingDeferredMap.set(req.pipelineId, deferred());

req.conn = httpConn;
req.r = bufr;
req.w = bufw;

// First line: GET /index.html HTTP/1.0
let firstLine: string;
[firstLine, bufStateErr] = await tp.readLine();
if (bufStateErr !== null) break;
[req.method, req.url, req.proto] = firstLine.split(" ", 3);

[req.headers, bufStateErr] = await tp.readMIMEHeader();
if (bufStateErr !== null) break;

[req, bufStateErr] = await readRequest(httpConn, bufr);
if (bufStateErr) break;
yield req;
}