More work on new streams
This commit is contained in:
+79
-127
@@ -5,6 +5,7 @@
|
||||
|
||||
#include "AsyncSocket.h"
|
||||
#include "HttpResponseData.h"
|
||||
#include "Utilities.h"
|
||||
|
||||
namespace uWS {
|
||||
|
||||
@@ -13,62 +14,28 @@ const char *HTTP_200_OK = "200 OK";
|
||||
|
||||
template <bool SSL>
|
||||
struct HttpResponse : public AsyncSocket<SSL> {
|
||||
typedef AsyncSocket<SSL> Super;
|
||||
private:
|
||||
HttpResponseData<SSL> *getHttpResponseData() {
|
||||
return (HttpResponseData<SSL> *) AsyncSocket<SSL>::getExt();
|
||||
}
|
||||
|
||||
int u32toaHex(uint32_t value, char *dst) {
|
||||
char palette[] = "0123456789abcdef";
|
||||
char temp[10];
|
||||
char *p = temp;
|
||||
do {
|
||||
*p++ = palette[value % 16];
|
||||
value /= 16;
|
||||
} while (value > 0);
|
||||
|
||||
int ret = p - temp;
|
||||
|
||||
do {
|
||||
*dst++ = *--p;
|
||||
} while (p != temp);
|
||||
|
||||
return ret;
|
||||
return (HttpResponseData<SSL> *) Super::getExt();
|
||||
}
|
||||
|
||||
/* Write an unsigned 32-bit integer in hex */
|
||||
void writeUnsignedHex(unsigned int value) {
|
||||
char buf[10];
|
||||
int length = u32toaHex(value, buf);
|
||||
int length = utils::u32toaHex(value, buf);
|
||||
|
||||
/* For now we do this copy */
|
||||
AsyncSocket<SSL>::write(buf, length);
|
||||
}
|
||||
|
||||
int u32toa(uint32_t value, char *dst) {
|
||||
char temp[10];
|
||||
char *p = temp;
|
||||
do {
|
||||
*p++ = (char) (value % 10) + '0';
|
||||
value /= 10;
|
||||
} while (value > 0);
|
||||
|
||||
int ret = p - temp;
|
||||
|
||||
do {
|
||||
*dst++ = *--p;
|
||||
} while (p != temp);
|
||||
|
||||
return ret;
|
||||
Super::write(buf, length);
|
||||
}
|
||||
|
||||
/* Write an unsigned 32-bit integer */
|
||||
void writeUnsigned(unsigned int value) {
|
||||
char buf[10];
|
||||
int length = u32toa(value, buf);
|
||||
int length = utils::u32toa(value, buf);
|
||||
|
||||
/* For now we do this copy */
|
||||
AsyncSocket<SSL>::write(buf, length);
|
||||
Super::write(buf, length);
|
||||
}
|
||||
|
||||
public:
|
||||
@@ -77,59 +44,61 @@ public:
|
||||
HttpResponseData<SSL> *httpResponseData = getHttpResponseData();
|
||||
|
||||
/* Do not allow writing more than one status */
|
||||
if (httpResponseData->state & HttpResponseData<SSL>::HTTP_STATUS_SENT) {
|
||||
if (httpResponseData->state & HttpResponseData<SSL>::HTTP_STATUS_CALLED) {
|
||||
return this;
|
||||
}
|
||||
|
||||
/* Update status */
|
||||
httpResponseData->state |= HttpResponseData<SSL>::HTTP_STATUS_SENT;
|
||||
httpResponseData->state |= HttpResponseData<SSL>::HTTP_STATUS_CALLED;
|
||||
|
||||
AsyncSocket<SSL>::write("HTTP/1.1 ", 9);
|
||||
AsyncSocket<SSL>::write(status.data(), status.length());
|
||||
AsyncSocket<SSL>::write("\r\n", 2);
|
||||
Super::write("HTTP/1.1 ", 9);
|
||||
Super::write(status.data(), status.length());
|
||||
Super::write("\r\n", 2);
|
||||
return this;
|
||||
}
|
||||
|
||||
/* Write an HTTP header with string value */
|
||||
HttpResponse *writeHeader(std::string_view key, std::string_view value) {
|
||||
AsyncSocket<SSL>::write(key.data(), key.length());
|
||||
AsyncSocket<SSL>::write(": ", 2);
|
||||
AsyncSocket<SSL>::write(value.data(), value.length());
|
||||
AsyncSocket<SSL>::write("\r\n", 2);
|
||||
Super::write(key.data(), key.length());
|
||||
Super::write(": ", 2);
|
||||
Super::write(value.data(), value.length());
|
||||
Super::write("\r\n", 2);
|
||||
return this;
|
||||
}
|
||||
|
||||
/* Write an HTTP header with unsigned int value */
|
||||
HttpResponse *writeHeader(std::string_view key, unsigned int value) {
|
||||
AsyncSocket<SSL>::write(key.data(), key.length());
|
||||
AsyncSocket<SSL>::write(": ", 2);
|
||||
Super::write(key.data(), key.length());
|
||||
Super::write(": ", 2);
|
||||
writeUnsigned(value);
|
||||
AsyncSocket<SSL>::write("\r\n", 2);
|
||||
Super::write("\r\n", 2);
|
||||
return this;
|
||||
}
|
||||
|
||||
/* End the response with an optional data chunk */
|
||||
void end(std::string_view data = {}) {
|
||||
writeStatus(HTTP_200_OK);
|
||||
|
||||
HttpResponseData<SSL> *httpResponseData = getHttpResponseData();
|
||||
|
||||
if (httpResponseData->state & HttpResponseData<SSL>::HTTP_WRITE_CALLED) {
|
||||
/* Do not allow sending 0 chunk here */
|
||||
if (data.length()) {
|
||||
AsyncSocket<SSL>::write("\r\n", 2);
|
||||
Super::write("\r\n", 2);
|
||||
writeUnsignedHex(data.length());
|
||||
AsyncSocket<SSL>::write("\r\n", 2);
|
||||
AsyncSocket<SSL>::write(data.data(), data.length());
|
||||
Super::write("\r\n", 2);
|
||||
Super::write(data.data(), data.length());
|
||||
}
|
||||
|
||||
/* Terminating 0 chunk */
|
||||
AsyncSocket<SSL>::write("\r\n0\r\n\r\n", 7);
|
||||
Super::write("\r\n0\r\n\r\n", 7);
|
||||
} else {
|
||||
/* We have a known send size */
|
||||
AsyncSocket<SSL>::write("Content-Length: ", 16);
|
||||
Super::write("Content-Length: ", 16);
|
||||
writeUnsigned(data.length());
|
||||
AsyncSocket<SSL>::write("\r\n\r\n", 4);
|
||||
Super::write("\r\n\r\n", 4);
|
||||
|
||||
AsyncSocket<SSL>::write(data.data(), data.length());
|
||||
Super::write(data.data(), data.length());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -147,94 +116,77 @@ public:
|
||||
httpResponseData->state |= HttpResponseData<SSL>::HTTP_WRITE_CALLED;
|
||||
}
|
||||
|
||||
AsyncSocket<SSL>::write("\r\n", 2);
|
||||
Super::write("\r\n", 2);
|
||||
writeUnsignedHex(data.length());
|
||||
AsyncSocket<SSL>::write("\r\n", 2);
|
||||
AsyncSocket<SSL>::write(data.data(), data.length());
|
||||
Super::write("\r\n", 2);
|
||||
Super::write(data.data(), data.length());
|
||||
|
||||
// are we corked still?
|
||||
return true;
|
||||
}
|
||||
|
||||
// we really want tryEnd(data) integer to try and stream something with known size
|
||||
|
||||
// write/tryWrite called first should enter into chunked?
|
||||
|
||||
// tryWrite(char *, length) int
|
||||
|
||||
// write(char *, length) bool
|
||||
|
||||
|
||||
|
||||
/* Attach an output stream function. Chunks may be read more than once. Negative offset mean broken stream */
|
||||
void writeOldRemoveMe(std::function<std::pair<bool, std::string_view>(int)> cb, int length = 0) {
|
||||
HttpResponseData<SSL> *httpResponseData = getHttpResponseData();
|
||||
|
||||
/* Do not allow write if already called */
|
||||
if (httpResponseData->state & HttpResponseData<SSL>::HTTP_WRITE_CALLED) {
|
||||
return;
|
||||
}
|
||||
|
||||
/* Write 200 OK if not already written any status */
|
||||
// todo: share this code in a function
|
||||
bool tryEnd(std::string_view data, int totalSize = 0) {
|
||||
/* Write status if not already done */
|
||||
writeStatus(HTTP_200_OK);
|
||||
|
||||
/* Update status */
|
||||
httpResponseData->state |= HttpResponseData<SSL>::HTTP_WRITE_CALLED;
|
||||
if (length) {
|
||||
httpResponseData->state |= HttpResponseData<SSL>::HTTP_KNOWN_STREAM_OUT_SIZE;
|
||||
|
||||
/* Rely on FIN to signal end if we do not pass any length */
|
||||
AsyncSocket<SSL>::write("Content-Length: ", 16);
|
||||
writeUnsigned(length);
|
||||
AsyncSocket<SSL>::write("\r\n", 2);
|
||||
/* If no total size given then assume this chunk is everything */
|
||||
if (!totalSize) {
|
||||
totalSize = data.length();
|
||||
}
|
||||
|
||||
/* HTTP body separator */
|
||||
AsyncSocket<SSL>::write("\r\n", 2);
|
||||
HttpResponseData<SSL> *httpResponseData = getHttpResponseData();
|
||||
|
||||
for (int offset = 0; length == 0 || offset < length; ) {
|
||||
/* Pull a chunk from stream */
|
||||
auto [msg_more, chunk] = cb(offset);
|
||||
if (httpResponseData->state & HttpResponseData<SSL>::HTTP_WRITE_CALLED) {
|
||||
/* Do not allow sending 0 chunk here */
|
||||
if (data.length()) {
|
||||
Super::write("\r\n", 2);
|
||||
writeUnsignedHex(data.length());
|
||||
Super::write("\r\n", 2);
|
||||
|
||||
/* Handle PAUSE and FIN */
|
||||
if (chunk.length() == 0) {
|
||||
/* FIN */
|
||||
if (chunk.data()) {
|
||||
/* Try flush and shut down */
|
||||
AsyncSocket<SSL>::uncork();
|
||||
if (!AsyncSocket<SSL>::hasBuffer()) {
|
||||
/* Uncork finished with no buffered data */
|
||||
AsyncSocket<SSL>::shutdown();
|
||||
} else {
|
||||
/* Let it shut down when drained */
|
||||
httpResponseData->state |= HttpResponseData<SSL>::HTTP_ENDED_STREAM_OUT;
|
||||
}
|
||||
} else {
|
||||
/* Disable timeout and mark this stream as paused (important to silence spurious onWritable events) */
|
||||
AsyncSocket<SSL>::timeout(0);
|
||||
httpResponseData->state |= HttpResponseData<SSL>::HTTP_PAUSED_STREAM_OUT;
|
||||
|
||||
// forgot about this path, if we sent things off and then ended up pausing!
|
||||
httpResponseData->offset = offset;
|
||||
httpResponseData->outStream = cb;
|
||||
}
|
||||
return;
|
||||
// should be optional
|
||||
Super::write(data.data(), data.length());
|
||||
}
|
||||
|
||||
/* Send off the chunk */
|
||||
int written = AsyncSocket<SSL>::write(chunk.data(), chunk.length(), true);
|
||||
/* Terminating 0 chunk */
|
||||
Super::write("\r\n0\r\n\r\n", 7);
|
||||
} else {
|
||||
// if not already ended! should we call tryWrite after this? we need an extra flag! end called!
|
||||
|
||||
/* If we failed to send everything, exit */
|
||||
if (written < chunk.length()) {
|
||||
httpResponseData->offset = offset + written;
|
||||
httpResponseData->outStream = cb;
|
||||
return;
|
||||
if (!(httpResponseData->state & HttpResponseData<SSL>::HTTP_END_CALLED)) {
|
||||
/* We have a known send size */
|
||||
Super::write("Content-Length: ", 16);
|
||||
writeUnsigned(data.length());
|
||||
Super::write("\r\n\r\n", 4);
|
||||
|
||||
/* Mark end called */
|
||||
httpResponseData->state |= HttpResponseData<SSL>::HTTP_END_CALLED;
|
||||
}
|
||||
|
||||
offset += written;
|
||||
/* Write as much as possible without causing backpressure */
|
||||
httpResponseData->offset += Super::write(data.data(), data.length(), true);
|
||||
}
|
||||
|
||||
return httpResponseData->offset == totalSize;
|
||||
}
|
||||
|
||||
/* Attach handler for writable HTTP response */
|
||||
HttpResponse *onWritable(std::function<void(int)> handler) {
|
||||
HttpResponseData<SSL> *httpResponseData = getHttpResponseData();
|
||||
|
||||
httpResponseData->onWritable = handler;
|
||||
return this;
|
||||
}
|
||||
|
||||
/* Attach handler for aborted HTTP request */
|
||||
HttpResponse *onAborted(std::function<void()> handler) {
|
||||
HttpResponseData<SSL> *httpResponseData = getHttpResponseData();
|
||||
|
||||
httpResponseData->onAborted = handler;
|
||||
return this;
|
||||
}
|
||||
|
||||
// onData(chunk, remaining == -1 or 0 or actual remaining)?
|
||||
/* Attach a read handler for data sent. Will be called with a chunk of size 0 when FIN */
|
||||
void read(std::function<void(std::string_view)> handler) {
|
||||
HttpResponseData<SSL> *data = getHttpResponseData();
|
||||
|
||||
Reference in New Issue
Block a user