diff --git a/design.dia b/design.dia index a12b6e4..85e356c 100644 Binary files a/design.dia and b/design.dia differ diff --git a/main.cpp b/main.cpp index 25ee63c..3df3b4a 100644 --- a/main.cpp +++ b/main.cpp @@ -69,7 +69,7 @@ int main(int argc, char **argv) { });*/ res->writeStatus(uWS::HTTP_200_OK)->write([](int offset) { - return std::string_view("Hello world!"); + return std::string_view("Hello world!").substr(offset); }, 12); }); diff --git a/src/new_design/AsyncSocket.h b/src/new_design/AsyncSocket.h index b993208..5934ecd 100644 --- a/src/new_design/AsyncSocket.h +++ b/src/new_design/AsyncSocket.h @@ -111,6 +111,10 @@ struct AsyncSocket : StaticDispatch { } + void *getExt() { + return static_dispatch(us_ssl_socket_ext, us_socket_ext)((SOCKET_TYPE *) this); + } + void close() { static_dispatch(us_ssl_socket_close, us_socket_close)((SOCKET_TYPE *) this); } diff --git a/src/new_design/HttpResponse.h b/src/new_design/HttpResponse.h index 320b4f8..bc7ca19 100644 --- a/src/new_design/HttpResponse.h +++ b/src/new_design/HttpResponse.h @@ -1,26 +1,26 @@ #ifndef HTTPRESPONSE_H #define HTTPRESPONSE_H +/* An HttpResponse is the channel on which you send back a response */ + #include "AsyncSocket.h" #include "HttpResponseData.h" namespace uWS { +/* Some pre-defined status constants to use with writeStatus */ const char *HTTP_200_OK = "200 OK"; template struct HttpResponse : public AsyncSocket { private: - - using SOCKET_TYPE = typename StaticDispatch::SOCKET_TYPE; - using StaticDispatch::static_dispatch; - HttpResponseData *getHttpResponseData() { - return (HttpResponseData *) static_dispatch(us_ssl_socket_ext, us_socket_ext)((SOCKET_TYPE *) this); + return (HttpResponseData *) AsyncSocket::getExt(); } public: + /* Write the HTTP status */ HttpResponse *writeStatus(std::string_view status) { AsyncSocket::write("HTTP/1.1 ", 9); AsyncSocket::write(status.data(), status.length()); @@ -28,6 +28,7 @@ public: return this; } + /* Write an HTTP header with string value */ HttpResponse *writeHeader(std::string_view key, std::string_view value) { AsyncSocket::write(key.data(), key.length()); AsyncSocket::write(": ", 2); @@ -36,12 +37,9 @@ public: return this; } + /* Attach a write handler for sending data. Length must be specified up front and chunks might be read more than once */ void write(std::function cb, int length) { - // what if the streamer cannot return any data? - // then it should return something to pause write, and then start it again - // basically we need throttling std::string_view chunk = cb(0); - AsyncSocket::write("Content-Length: ", 16); AsyncSocket::writeUnsigned(chunk.length()); AsyncSocket::write("\r\n\r\n", 4); @@ -50,19 +48,11 @@ public: } } - // this will probably not be this clean: it will most probably want to do some active pulling of data? + /* Attach a read handler for data sent. Will be called with a chunk of size 0 when FIN */ void read(std::function handler) { HttpResponseData *data = getHttpResponseData(); - data->inStream = handler; } - - // this should not be anything other than a simple convenience wrapper of streams! - void end(std::string_view data) { - writeStatus("200 OK")->write([data](int offset) { - return std::string_view(data.data() + offset, data.length() - offset); - }, data.length()); - } }; } diff --git a/src/new_design/HttpResponseData.h b/src/new_design/HttpResponseData.h index 01956c5..5d3e2e0 100644 --- a/src/new_design/HttpResponseData.h +++ b/src/new_design/HttpResponseData.h @@ -1,6 +1,8 @@ #ifndef HTTPRESPONSEDATA_H #define HTTPRESPONSEDATA_H +/* This data belongs to the HttpResponse */ + #include "HttpParser.h" #include