Add experimental HttpResponse::cork

This commit is contained in:
Alex Hultman
2019-04-16 16:59:28 +02:00
parent c1ea38ae10
commit 799fe89a9b
2 changed files with 27 additions and 0 deletions
+5
View File
@@ -69,6 +69,11 @@ struct AsyncSocket {
return getLoopData()->corkedSocket == this;
}
/* Returns whether we could cork (it is free) */
bool canCork() {
return getLoopData()->corkedSocket == nullptr;
}
/* Returns a suitable buffer for temporary assemblation of send data */
std::pair<char *, bool> getSendBuffer(size_t size) {
/* If we are corked and we have room, return the cork buffer itself */
+22
View File
@@ -268,6 +268,28 @@ public:
return !(httpResponseData->state & HttpResponseData<SSL>::HTTP_RESPONSE_PENDING);
}
/* EXPERIMENTAL - corks the response if possible */
HttpResponse *cork(fu2::unique_function<void()> &&handler) {
bool corked = Super::isCorked();
if (!corked && Super::canCork()) {
Super::cork();
corked = true;
}
handler();
if (corked) {
/* Timeout on uncork failure (EXPERIMENTAL) */
auto [written, failed] = Super::uncork();
if (failed) {
// do we have the same timeout for websockets?
((AsyncSocket<SSL> *) s)->timeout(HTTP_IDLE_TIMEOUT_S);
}
}
return this;
}
/* Attach handler for writable HTTP response */
HttpResponse *onWritable(fu2::unique_function<bool(int)> &&handler) {
HttpResponseData<SSL> *httpResponseData = getHttpResponseData();