Add experimental HttpResponse::cork
This commit is contained in:
@@ -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 */
|
||||
|
||||
@@ -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();
|
||||
|
||||
Reference in New Issue
Block a user