Add all HTTP methods

This commit is contained in:
Alex Hultman
2018-10-16 23:55:25 +02:00
parent bb77fac1fc
commit 8105c65fa6
3 changed files with 59 additions and 25 deletions
+37 -2
View File
@@ -28,12 +28,47 @@ public:
}
TemplatedApp &get(std::string pattern, std::function<void(HttpResponse<SSL> *, HttpRequest *)> handler) {
httpContext->onGet(pattern, handler);
httpContext->onHttp("get", pattern, handler);
return *this;
}
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;
}
+19 -19
View File
@@ -12,6 +12,9 @@
#include <string_view>
#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 {
template<bool> struct HttpResponse;
@@ -114,11 +117,10 @@ private:
httpResponseData->offset = 0;
httpResponseData->state = 0;
// route it!
typename uWS::HttpContextData<SSL>::UserData userData = {
(HttpResponse<SSL> *) s, httpRequest
};
httpContextData->router.route(httpRequest->getMethod(), httpRequest->getUrl(), &userData);
/* Route the method and URL */
httpContextData->router.route(httpRequest->getMethod(), httpRequest->getUrl(), {
(HttpResponse<SSL> *) s, httpRequest
});
// here we can be closed and in shutdown?
@@ -129,8 +131,14 @@ private:
}, [](void *user) {
// close any socket on HTTP errors
//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
((AsyncSocket<SSL> *) s)->uncork();
@@ -219,31 +227,23 @@ public:
}
/* 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->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!
user->httpRequest->setParameters(params);
user.httpRequest->setParameters(params);
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);
handler(user.httpResponse, user.httpRequest);
});
}
void onUnhandled(std::function<void(uWS::HttpResponse<SSL> *, uWS::HttpRequest *)> handler) {
HttpContextData<SSL> *httpContextData = getSocketContextData();
httpContextData->router.unhandled([handler](typename HttpContextData<SSL>::UserData *user, std::pair<int, std::string_view *> params) {
handler(user->httpResponse, user->httpRequest);
httpContextData->router.unhandled([handler](typename HttpContextData<SSL>::RouterData user, std::pair<int, std::string_view *> params) {
handler(user.httpResponse, user.httpRequest);
});
}
+3 -4
View File
@@ -11,15 +11,14 @@ struct HttpRequest;
template <bool SSL>
struct HttpContextData {
template <bool> friend struct HttpContext;
private:
public:
struct UserData {
struct RouterData {
HttpResponse<SSL> *httpResponse;
HttpRequest *httpRequest;
};
HttpRouter<UserData *> router;
HttpRouter<RouterData> router;
};
}