Add all HTTP methods
This commit is contained in:
@@ -28,12 +28,47 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
TemplatedApp &get(std::string pattern, std::function<void(HttpResponse<SSL> *, HttpRequest *)> handler) {
|
TemplatedApp &get(std::string pattern, std::function<void(HttpResponse<SSL> *, HttpRequest *)> handler) {
|
||||||
httpContext->onGet(pattern, handler);
|
httpContext->onHttp("get", pattern, handler);
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
TemplatedApp &post(std::string pattern, std::function<void(HttpResponse<SSL> *, HttpRequest *)> handler) {
|
TemplatedApp &post(std::string pattern, std::function<void(HttpResponse<SSL> *, HttpRequest *)> handler) {
|
||||||
httpContext->onPost(pattern, handler);
|
httpContext->onHttp("post", pattern, handler);
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
TemplatedApp &options(std::string pattern, std::function<void(HttpResponse<SSL> *, HttpRequest *)> handler) {
|
||||||
|
httpContext->onHttp("options", pattern, handler);
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
TemplatedApp &del(std::string pattern, std::function<void(HttpResponse<SSL> *, HttpRequest *)> handler) {
|
||||||
|
httpContext->onHttp("delete", pattern, handler);
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
TemplatedApp &patch(std::string pattern, std::function<void(HttpResponse<SSL> *, HttpRequest *)> handler) {
|
||||||
|
httpContext->onHttp("patch", pattern, handler);
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
TemplatedApp &put(std::string pattern, std::function<void(HttpResponse<SSL> *, HttpRequest *)> handler) {
|
||||||
|
httpContext->onHttp("put", pattern, handler);
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
TemplatedApp &head(std::string pattern, std::function<void(HttpResponse<SSL> *, HttpRequest *)> handler) {
|
||||||
|
httpContext->onHttp("head", pattern, handler);
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
TemplatedApp &connect(std::string pattern, std::function<void(HttpResponse<SSL> *, HttpRequest *)> handler) {
|
||||||
|
httpContext->onHttp("connect", pattern, handler);
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
TemplatedApp &trace(std::string pattern, std::function<void(HttpResponse<SSL> *, HttpRequest *)> handler) {
|
||||||
|
httpContext->onHttp("trace", pattern, handler);
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+19
-19
@@ -12,6 +12,9 @@
|
|||||||
#include <string_view>
|
#include <string_view>
|
||||||
#include <functional>
|
#include <functional>
|
||||||
|
|
||||||
|
/* This is a hack for now on, update uSockets */
|
||||||
|
extern "C" int us_internal_socket_is_closed(struct us_socket *s);
|
||||||
|
|
||||||
namespace uWS {
|
namespace uWS {
|
||||||
template<bool> struct HttpResponse;
|
template<bool> struct HttpResponse;
|
||||||
|
|
||||||
@@ -114,11 +117,10 @@ private:
|
|||||||
httpResponseData->offset = 0;
|
httpResponseData->offset = 0;
|
||||||
httpResponseData->state = 0;
|
httpResponseData->state = 0;
|
||||||
|
|
||||||
// route it!
|
/* Route the method and URL */
|
||||||
typename uWS::HttpContextData<SSL>::UserData userData = {
|
httpContextData->router.route(httpRequest->getMethod(), httpRequest->getUrl(), {
|
||||||
(HttpResponse<SSL> *) s, httpRequest
|
(HttpResponse<SSL> *) s, httpRequest
|
||||||
};
|
});
|
||||||
httpContextData->router.route(httpRequest->getMethod(), httpRequest->getUrl(), &userData);
|
|
||||||
|
|
||||||
// here we can be closed and in shutdown?
|
// here we can be closed and in shutdown?
|
||||||
|
|
||||||
@@ -129,8 +131,14 @@ private:
|
|||||||
}, [](void *user) {
|
}, [](void *user) {
|
||||||
// close any socket on HTTP errors
|
// close any socket on HTTP errors
|
||||||
//static_dispatch(us_ssl_socket_close, us_socket_close)((SOCKET_TYPE *) user);
|
//static_dispatch(us_ssl_socket_close, us_socket_close)((SOCKET_TYPE *) user);
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
if (us_internal_socket_is_closed((struct us_socket *) s)) {
|
||||||
|
// do you really return s? I guess so?
|
||||||
|
return s;
|
||||||
|
}
|
||||||
|
|
||||||
// uncork only if not closed
|
// uncork only if not closed
|
||||||
((AsyncSocket<SSL> *) s)->uncork();
|
((AsyncSocket<SSL> *) s)->uncork();
|
||||||
|
|
||||||
@@ -219,31 +227,23 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* Register an HTTP GET route handler acording to URL pattern */
|
/* Register an HTTP GET route handler acording to URL pattern */
|
||||||
void onGet(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();
|
||||||
|
|
||||||
httpContextData->router.add("get", pattern, [handler](typename HttpContextData<SSL>::UserData *user, std::pair<int, std::string_view *> params) {
|
httpContextData->router.add(method, pattern, [handler](typename HttpContextData<SSL>::RouterData user, std::pair<int, std::string_view *> params) {
|
||||||
|
|
||||||
// todo: attach params to the req here!
|
// todo: attach params to the req here!
|
||||||
user->httpRequest->setParameters(params);
|
user.httpRequest->setParameters(params);
|
||||||
|
|
||||||
handler(user->httpResponse, user->httpRequest);
|
handler(user.httpResponse, user.httpRequest);
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
void onPost(std::string pattern, std::function<void(uWS::HttpResponse<SSL> *, uWS::HttpRequest *)> handler) {
|
|
||||||
HttpContextData<SSL> *httpContextData = getSocketContextData();
|
|
||||||
|
|
||||||
httpContextData->router.add("post", pattern, [handler](typename HttpContextData<SSL>::UserData *user, std::pair<int, std::string_view *> params) {
|
|
||||||
handler(user->httpResponse, user->httpRequest);
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
void onUnhandled(std::function<void(uWS::HttpResponse<SSL> *, uWS::HttpRequest *)> handler) {
|
void onUnhandled(std::function<void(uWS::HttpResponse<SSL> *, uWS::HttpRequest *)> handler) {
|
||||||
HttpContextData<SSL> *httpContextData = getSocketContextData();
|
HttpContextData<SSL> *httpContextData = getSocketContextData();
|
||||||
|
|
||||||
httpContextData->router.unhandled([handler](typename HttpContextData<SSL>::UserData *user, std::pair<int, std::string_view *> params) {
|
httpContextData->router.unhandled([handler](typename HttpContextData<SSL>::RouterData user, std::pair<int, std::string_view *> params) {
|
||||||
handler(user->httpResponse, user->httpRequest);
|
handler(user.httpResponse, user.httpRequest);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -11,15 +11,14 @@ struct HttpRequest;
|
|||||||
|
|
||||||
template <bool SSL>
|
template <bool SSL>
|
||||||
struct HttpContextData {
|
struct HttpContextData {
|
||||||
|
template <bool> friend struct HttpContext;
|
||||||
private:
|
private:
|
||||||
|
struct RouterData {
|
||||||
public:
|
|
||||||
struct UserData {
|
|
||||||
HttpResponse<SSL> *httpResponse;
|
HttpResponse<SSL> *httpResponse;
|
||||||
HttpRequest *httpRequest;
|
HttpRequest *httpRequest;
|
||||||
};
|
};
|
||||||
|
|
||||||
HttpRouter<UserData *> router;
|
HttpRouter<RouterData> router;
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user