onDataRequest method
The client sends a request with data.
Implementation
void onDataRequest(SocketConnect connect) {
if (dataReq != null) {
// assert: this.dataRes, '.dataReq and .dataRes should be (un)set together'
onError('data request overlap from client');
connect.request.response.statusCode = 500;
connect.close();
return;
}
var isBinary = 'application/octet-stream' ==
connect.request.headers.value('content-type');
dataReq = connect;
dynamic chunks = isBinary ? [0] : '';
var self = this;
StreamSubscription? subscription;
cleanup() {
chunks = isBinary ? [0] : '';
if (subscription != null) {
subscription.cancel();
}
self.dataReq = null;
}
onData(List<int> data) {
dynamic contentLength;
if (data is String) {
chunks += data;
contentLength = utf8.encode(chunks).length;
} else {
if (chunks is String) {
chunks += String.fromCharCodes(data);
} else {
chunks.addAll(String.fromCharCodes(data)
.split(',')
.map((s) => int.parse(s))
.toList());
}
contentLength = chunks.length;
}
if (contentLength > self.maxHttpBufferSize) {
chunks = '';
connect.close();
}
}
onEnd() {
self.onData(chunks);
var headers = {'Content-Type': 'text/html', 'Content-Length': 2};
var res = connect.request.response;
res.statusCode = 200;
res.headers.clear();
// text/html is required instead of text/plain to avoid an
// unwanted download dialog on certain user-agents (GH-43)
self.headers(connect, headers).forEach((key, value) {
res.headers.set(key, value);
});
res.write('ok');
connect.close();
cleanup();
}
subscription = connect.request.listen(onData, onDone: onEnd);
if (!isBinary) {
connect.request.headers.contentType =
ContentType.text; // for encoding utf-8
}
}