Fix HttpResponse::write with timeout & internalEnd comments

This commit is contained in:
Alex Hultman
2018-10-28 03:54:16 +01:00
parent ba83c9156a
commit c0eaf297ed
+13 -21
View File
@@ -7,6 +7,8 @@
#include "HttpResponseData.h" #include "HttpResponseData.h"
#include "Utilities.h" #include "Utilities.h"
/* todo: tryWrite is missing currently, only send smaller segments with write */
namespace uWS { namespace uWS {
/* Some pre-defined status constants to use with writeStatus */ /* Some pre-defined status constants to use with writeStatus */
@@ -55,7 +57,7 @@ private:
HttpResponseData<SSL> *httpResponseData = getHttpResponseData(); HttpResponseData<SSL> *httpResponseData = getHttpResponseData();
if (httpResponseData->state & HttpResponseData<SSL>::HTTP_WRITE_CALLED) { if (httpResponseData->state & HttpResponseData<SSL>::HTTP_WRITE_CALLED) {
// we do not listen to optional here! /* We do not have tryWrite-like functionalities, so ignore optional in this path */
/* Do not allow sending 0 chunk here */ /* Do not allow sending 0 chunk here */
if (data.length()) { if (data.length()) {
@@ -63,21 +65,16 @@ private:
writeUnsignedHex(data.length()); writeUnsignedHex(data.length());
Super::write("\r\n", 2); Super::write("\r\n", 2);
// should be optional /* Ignoring optional for now */
Super::write(data.data(), data.length()); Super::write(data.data(), data.length());
} }
/* Terminating 0 chunk */ /* Terminating 0 chunk */
Super::write("\r\n0\r\n\r\n", 7); Super::write("\r\n0\r\n\r\n", 7);
// what about timeout here!? /* tryEnd can never fail when in chunked mode, since we do not have tryWrite (yet), only write */
// always start timeout here!
Super::timeout(HTTP_TIMEOUT_S); Super::timeout(HTTP_TIMEOUT_S);
// unclear about this path really
return true; return true;
} else { } else {
/* Write content-length on first call */ /* Write content-length on first call */
if (!(httpResponseData->state & HttpResponseData<SSL>::HTTP_END_CALLED)) { if (!(httpResponseData->state & HttpResponseData<SSL>::HTTP_END_CALLED)) {
@@ -167,15 +164,10 @@ public:
/* Try and end the response. Returns true on success. Starts a timeout in some cases. */ /* Try and end the response. Returns true on success. Starts a timeout in some cases. */
bool tryEnd(std::string_view data, int totalSize = 0) { bool tryEnd(std::string_view data, int totalSize = 0) {
bool succeeded = internalEnd(data, totalSize, true); return internalEnd(data, totalSize, true);
std::cout << "tryEnd with size " << data.length() << " returned " << succeeded << std::endl;
return succeeded;
} }
/* Write parts of the response in chunking fashion */ /* Write parts of the response in chunking fashion. Starts timeout if failed. */
// fic this up and add tryWrite (will require more state!)
bool write(std::string_view data) { bool write(std::string_view data) {
writeStatus(HTTP_200_OK); writeStatus(HTTP_200_OK);
@@ -196,13 +188,13 @@ public:
writeUnsignedHex(data.length()); writeUnsignedHex(data.length());
Super::write("\r\n", 2); Super::write("\r\n", 2);
// this should essentially return what we want to return from here! auto [written, failed] = Super::write(data.data(), data.length());
Super::write(data.data(), data.length()); if (failed) {
Super::timeout(HTTP_TIMEOUT_S);
}
// we want write to return whether the user may call write again, basically if we are polling for writable /* If we did not fail the write, accept more */
return !failed;
// are we corked still?
return true;
} }
/* Get the current byte write offset for this Http response */ /* Get the current byte write offset for this Http response */