We need HttpResponse::hasResponded

This commit is contained in:
Alex Hultman
2019-01-18 16:35:25 +01:00
parent 3c3819fe3c
commit 2caefa9d96
6 changed files with 53 additions and 6 deletions
+5
View File
@@ -53,6 +53,11 @@ private:
using StaticDispatch<SSL>::static_dispatch;
public:
/* Attaches a "filter" function to track socket connections/disconnections */
void filter(fu2::unique_function<void(HttpResponse<SSL> *, int)> &&filterHandler) {
httpContext->filter(std::move(filterHandler));
}
void registerTag() {
}
+25
View File
@@ -70,6 +70,14 @@ private:
/* Init socket ext */
new (static_dispatch(us_ssl_socket_ext, us_socket_ext)(s)) HttpResponseData<SSL>;
/* Call filter */
HttpContextData<SSL> *httpContextData = getSocketContextDataS(s);
for (auto &f : httpContextData->filterHandlers) {
f((HttpResponse<SSL> *) s, 1);
}
// todo: handle filter closing the socket?
return s;
});
@@ -78,6 +86,12 @@ private:
/* Get socket ext */
HttpResponseData<SSL> *httpResponseData = (HttpResponseData<SSL> *) static_dispatch(us_ssl_socket_ext, us_socket_ext)(s);
/* Call filter */
HttpContextData<SSL> *httpContextData = getSocketContextDataS(s);
for (auto &f : httpContextData->filterHandlers) {
f((HttpResponse<SSL> *) s, -1);
}
/* Signal broken HTTP request only if we have a pending request */
if (httpResponseData->onAborted) {
httpResponseData->onAborted();
@@ -145,6 +159,13 @@ private:
return nullptr;
}
/* Returning from a request handler without responding or attaching an onAborted handler is ill-use */
if (!((HttpResponse<SSL> *) s)->hasResponded() && !httpResponseData->onAborted) {
/* Throw exception here? */
std::cerr << "µWebSockets ill-use: Returning from a request handler without responding or attaching an abort handler is forbidden." << std::endl;
std::terminate();
}
/* Continue parsing */
return s;
@@ -269,6 +290,10 @@ public:
static_dispatch(us_ssl_socket_context_free, us_socket_context_free)(getSocketContext());
}
void filter(fu2::unique_function<void(HttpResponse<SSL> *, int)> &&filterHandler) {
getSocketContextData()->filterHandlers.emplace_back(std::move(filterHandler));
}
/* Register an HTTP route handler acording to URL pattern */
void onHttp(std::string method, std::string pattern, std::function<void(uWS::HttpResponse<SSL> *, uWS::HttpRequest *)> handler) {
HttpContextData<SSL> *httpContextData = getSocketContextData();
+10
View File
@@ -21,6 +21,9 @@
#include "HttpRouter.h"
#include <functional>
#include <vector>
#include "f2/function2.hpp"
namespace uWS {
template<bool> struct HttpResponse;
@@ -31,6 +34,13 @@ struct HttpContextData {
template <bool> friend struct HttpContext;
template <bool> friend struct HttpResponse;
private:
std::vector<fu2::unique_function<void(HttpResponse<SSL> *, int)>> filterHandlers;
/*HttpContextData(const HttpContextData&) = delete;
HttpContextData() {
}*/
struct RouterData {
HttpResponse<SSL> *httpResponse;
HttpRequest *httpRequest;
+1 -1
View File
@@ -71,7 +71,7 @@ public:
currentParameters = parameters;
}
std::string_view getParameter(int index) {
std::string_view getParameter(unsigned int index) {
if (currentParameters.first < index) {
return {};
} else {
+10
View File
@@ -92,6 +92,8 @@ private:
/* Terminating 0 chunk */
Super::write("\r\n0\r\n\r\n", 7);
// todo: here we reach the end, so remove onAborted, onWritable, and set HTTP_RESPONDED_TO
/* tryEnd can never fail when in chunked mode, since we do not have tryWrite (yet), only write */
Super::timeout(HTTP_TIMEOUT_S);
return true;
@@ -132,6 +134,8 @@ private:
httpResponseData->onAborted = nullptr;
/* Also remove onWritable so that we do not emit when draining behind the scenes. */
httpResponseData->onWritable = nullptr;
// todo: set HTTP_RESPONDED_TO here and use in the emittance of new requests
}
return success;
@@ -230,6 +234,12 @@ public:
return httpResponseData->offset;
}
/* Checking if we have fully responded and are ready for another request */
bool hasResponded() {
// todo: implement
return true;
}
/* Attach handler for writable HTTP response */
HttpResponse *onWritable(fu2::unique_function<bool(int)> &&handler) {
HttpResponseData<SSL> *httpResponseData = getHttpResponseData();
+2 -5
View File
@@ -38,17 +38,14 @@ private:
HTTP_STATUS_CALLED = 1, // used
HTTP_WRITE_CALLED = 2, // used
HTTP_END_CALLED = 4, // used
HTTP_UPGRADED_TO_WEBSOCKET = 8, // not used
HTTP_RESPONDED_TO = 8, // used
HTTP_ENDED_STREAM_OUT = 16 // not used
};
/* Per socket event handlers */
fu2::unique_function<bool(int)> onWritable;
fu2::unique_function<void()> onAborted;
//std::function<void()> onData;
fu2::unique_function<void(std::string_view, bool)> inStream;
//std::function<std::pair<bool, std::string_view>(int)> outStream;
fu2::unique_function<void(std::string_view, bool)> inStream; // onData
/* Outgoing offset */
int offset = 0;