We need HttpResponse::hasResponded
This commit is contained in:
@@ -53,6 +53,11 @@ private:
|
|||||||
using StaticDispatch<SSL>::static_dispatch;
|
using StaticDispatch<SSL>::static_dispatch;
|
||||||
public:
|
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() {
|
void registerTag() {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -70,6 +70,14 @@ private:
|
|||||||
/* Init socket ext */
|
/* Init socket ext */
|
||||||
new (static_dispatch(us_ssl_socket_ext, us_socket_ext)(s)) HttpResponseData<SSL>;
|
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;
|
return s;
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -78,6 +86,12 @@ private:
|
|||||||
/* Get socket ext */
|
/* Get socket ext */
|
||||||
HttpResponseData<SSL> *httpResponseData = (HttpResponseData<SSL> *) static_dispatch(us_ssl_socket_ext, us_socket_ext)(s);
|
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 */
|
/* Signal broken HTTP request only if we have a pending request */
|
||||||
if (httpResponseData->onAborted) {
|
if (httpResponseData->onAborted) {
|
||||||
httpResponseData->onAborted();
|
httpResponseData->onAborted();
|
||||||
@@ -145,6 +159,13 @@ private:
|
|||||||
return nullptr;
|
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 */
|
/* Continue parsing */
|
||||||
return s;
|
return s;
|
||||||
|
|
||||||
@@ -269,6 +290,10 @@ public:
|
|||||||
static_dispatch(us_ssl_socket_context_free, us_socket_context_free)(getSocketContext());
|
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 */
|
/* 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) {
|
void onHttp(std::string method, std::string pattern, std::function<void(uWS::HttpResponse<SSL> *, uWS::HttpRequest *)> handler) {
|
||||||
HttpContextData<SSL> *httpContextData = getSocketContextData();
|
HttpContextData<SSL> *httpContextData = getSocketContextData();
|
||||||
|
|||||||
@@ -21,6 +21,9 @@
|
|||||||
#include "HttpRouter.h"
|
#include "HttpRouter.h"
|
||||||
|
|
||||||
#include <functional>
|
#include <functional>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
#include "f2/function2.hpp"
|
||||||
|
|
||||||
namespace uWS {
|
namespace uWS {
|
||||||
template<bool> struct HttpResponse;
|
template<bool> struct HttpResponse;
|
||||||
@@ -31,6 +34,13 @@ struct HttpContextData {
|
|||||||
template <bool> friend struct HttpContext;
|
template <bool> friend struct HttpContext;
|
||||||
template <bool> friend struct HttpResponse;
|
template <bool> friend struct HttpResponse;
|
||||||
private:
|
private:
|
||||||
|
std::vector<fu2::unique_function<void(HttpResponse<SSL> *, int)>> filterHandlers;
|
||||||
|
|
||||||
|
/*HttpContextData(const HttpContextData&) = delete;
|
||||||
|
HttpContextData() {
|
||||||
|
|
||||||
|
}*/
|
||||||
|
|
||||||
struct RouterData {
|
struct RouterData {
|
||||||
HttpResponse<SSL> *httpResponse;
|
HttpResponse<SSL> *httpResponse;
|
||||||
HttpRequest *httpRequest;
|
HttpRequest *httpRequest;
|
||||||
|
|||||||
+1
-1
@@ -71,7 +71,7 @@ public:
|
|||||||
currentParameters = parameters;
|
currentParameters = parameters;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string_view getParameter(int index) {
|
std::string_view getParameter(unsigned int index) {
|
||||||
if (currentParameters.first < index) {
|
if (currentParameters.first < index) {
|
||||||
return {};
|
return {};
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@@ -92,6 +92,8 @@ private:
|
|||||||
/* Terminating 0 chunk */
|
/* Terminating 0 chunk */
|
||||||
Super::write("\r\n0\r\n\r\n", 7);
|
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 */
|
/* tryEnd can never fail when in chunked mode, since we do not have tryWrite (yet), only write */
|
||||||
Super::timeout(HTTP_TIMEOUT_S);
|
Super::timeout(HTTP_TIMEOUT_S);
|
||||||
return true;
|
return true;
|
||||||
@@ -132,6 +134,8 @@ private:
|
|||||||
httpResponseData->onAborted = nullptr;
|
httpResponseData->onAborted = nullptr;
|
||||||
/* Also remove onWritable so that we do not emit when draining behind the scenes. */
|
/* Also remove onWritable so that we do not emit when draining behind the scenes. */
|
||||||
httpResponseData->onWritable = nullptr;
|
httpResponseData->onWritable = nullptr;
|
||||||
|
|
||||||
|
// todo: set HTTP_RESPONDED_TO here and use in the emittance of new requests
|
||||||
}
|
}
|
||||||
|
|
||||||
return success;
|
return success;
|
||||||
@@ -230,6 +234,12 @@ public:
|
|||||||
return httpResponseData->offset;
|
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 */
|
/* Attach handler for writable HTTP response */
|
||||||
HttpResponse *onWritable(fu2::unique_function<bool(int)> &&handler) {
|
HttpResponse *onWritable(fu2::unique_function<bool(int)> &&handler) {
|
||||||
HttpResponseData<SSL> *httpResponseData = getHttpResponseData();
|
HttpResponseData<SSL> *httpResponseData = getHttpResponseData();
|
||||||
|
|||||||
@@ -38,17 +38,14 @@ private:
|
|||||||
HTTP_STATUS_CALLED = 1, // used
|
HTTP_STATUS_CALLED = 1, // used
|
||||||
HTTP_WRITE_CALLED = 2, // used
|
HTTP_WRITE_CALLED = 2, // used
|
||||||
HTTP_END_CALLED = 4, // 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
|
HTTP_ENDED_STREAM_OUT = 16 // not used
|
||||||
};
|
};
|
||||||
|
|
||||||
/* Per socket event handlers */
|
/* Per socket event handlers */
|
||||||
fu2::unique_function<bool(int)> onWritable;
|
fu2::unique_function<bool(int)> onWritable;
|
||||||
fu2::unique_function<void()> onAborted;
|
fu2::unique_function<void()> onAborted;
|
||||||
//std::function<void()> onData;
|
fu2::unique_function<void(std::string_view, bool)> inStream; // onData
|
||||||
|
|
||||||
fu2::unique_function<void(std::string_view, bool)> inStream;
|
|
||||||
//std::function<std::pair<bool, std::string_view>(int)> outStream;
|
|
||||||
/* Outgoing offset */
|
/* Outgoing offset */
|
||||||
int offset = 0;
|
int offset = 0;
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user