diff --git a/src/AsyncSocket.h b/src/AsyncSocket.h index a3e0728..d99d886 100644 --- a/src/AsyncSocket.h +++ b/src/AsyncSocket.h @@ -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 getSendBuffer(size_t size) { /* If we are corked and we have room, return the cork buffer itself */ diff --git a/src/HttpResponse.h b/src/HttpResponse.h index 44902b9..3170bac 100644 --- a/src/HttpResponse.h +++ b/src/HttpResponse.h @@ -268,6 +268,28 @@ public: return !(httpResponseData->state & HttpResponseData::HTTP_RESPONSE_PENDING); } + /* EXPERIMENTAL - corks the response if possible */ + HttpResponse *cork(fu2::unique_function &&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 *) s)->timeout(HTTP_IDLE_TIMEOUT_S); + } + } + + return this; + } + /* Attach handler for writable HTTP response */ HttpResponse *onWritable(fu2::unique_function &&handler) { HttpResponseData *httpResponseData = getHttpResponseData();