From 7faf2ebbb327b599e044b07996866636d0b66e81 Mon Sep 17 00:00:00 2001 From: Sage Vaillancourt Date: Tue, 15 Mar 2022 23:22:28 -0400 Subject: [PATCH] Add POST method. Rename `addroute` to `get`. --- src/env.c | 3 ++- src/examples/webby.pl | 10 +++++----- src/pebblisp.c | 15 ++++++++++++--- src/pebblisp.h | 3 ++- src/web.c | 16 ++++++++++++++-- src/web.h | 7 ++++++- 6 files changed, 41 insertions(+), 13 deletions(-) diff --git a/src/env.c b/src/env.c index f6ae16d..83def53 100644 --- a/src/env.c +++ b/src/env.c @@ -278,7 +278,8 @@ struct Environment defaultEnv() //{"sys", &systemCall}, {"loadfile", &loadFile}, {"inp", &takeInput}, - {"addroute", &addRouteO}, + {"get", &addGetRoute}, + {"post", &addPostRoute}, {"serve", &startServer} #endif }; diff --git a/src/examples/webby.pl b/src/examples/webby.pl index 36f2f83..dfd7875 100755 --- a/src/examples/webby.pl +++ b/src/examples/webby.pl @@ -1,6 +1,6 @@ #!/usr/bin/pl -(struct post +(struct Post (title body)) ;(def element (fn (type) (fn (text) (cat "<" type ">" text "")))) @@ -22,8 +22,8 @@ (h2 po's title) (p po's body)))) -(def p1 (post "Hey" "This is a post")) -(def p2 (post "This" +(def p1 (Post "Hey" "This is a post")) +(def p2 (Post "This" "Is ALSO a post. And frankly, what a great post! It's so long and flowing, and the interpreter won't even instantly crash over it! It's truly astounding stuff, when you think about it." @@ -36,9 +36,9 @@ stuff, when you think about it." (htmlize p1) (htmlize p2)))))) ) -(addroute "/" homepage) +(get "/" homepage) -(addroute "/styles.css" (fn () ( +(get "/styles.css" (fn () ( "html { background-color: #ddddff; } diff --git a/src/pebblisp.c b/src/pebblisp.c index 95566b6..3625c3e 100644 --- a/src/pebblisp.c +++ b/src/pebblisp.c @@ -664,21 +664,30 @@ Object print(Object p, Object ignore, struct Environment* env) return numberObject(0); } -Object addRouteO(Object path, Object textFunc, struct Environment* env) +void addRouteO(Object path, Object textFunc, struct Environment* env, enum RouteType type) { char* p = malloc(sizeof(char) * strlen(path.string) + 1); strcpy(p, path.string); - //const char* t = malloc(sizeof(char) * strlen(textFunc.string) + 1); - //strcpy(t, textFunc.string); struct Route r; r.path = p; r.routeAction = cloneObject(textFunc); r.env = env; + r.type = type; env->refs += 1; //r.text = t; addRoute(r); +} +Object addGetRoute(Object path, Object textFunc, struct Environment* env) +{ + addRouteO(path, textFunc, env, GET); + return numberObject(1); +} + +Object addPostRoute(Object path, Object textFunc, struct Environment* env) +{ + addRouteO(path, textFunc, env, POST); return numberObject(1); } diff --git a/src/pebblisp.h b/src/pebblisp.h index 90a2631..7bcdac8 100644 --- a/src/pebblisp.h +++ b/src/pebblisp.h @@ -105,7 +105,8 @@ Object systemCall(Object call, Object _, struct Environment* i3); Object loadFile(Object filename, Object _, struct Environment* env); Object startServer(Object path, Object textFunc, struct Environment* env); -Object addRouteO(Object path, Object textFunc, struct Environment* env); +Object addGetRoute(Object path, Object textFunc, struct Environment* env); +Object addPostRoute(Object path, Object textFunc, struct Environment* env); #endif diff --git a/src/web.c b/src/web.c index 96e5cb9..dafd722 100644 --- a/src/web.c +++ b/src/web.c @@ -24,6 +24,18 @@ int addRoute(struct Route route) { return 0; } +int methodMatches(const char* method, struct Route* route) +{ + switch (route->type) { + case GET: + return method[0] == 'G' || method[0] == 'g'; + case POST: + return method[0] == 'P' || method[0] == 'p'; + default: + return 0; + } +} + static enum MHD_Result answer_to_connection(void *cls, struct MHD_Connection *connection, const char *url, const char *method, @@ -33,8 +45,8 @@ answer_to_connection(void *cls, struct MHD_Connection *connection, const char *page = NULL; printf("%s URL: '%s' :: ", method, url); for (int i = 0; i < routeCount; i++) { - if (strcmp(url, routes[i].path) == 0) { - printf("route[%d]\n", i); + printf("route[%d]\n", i); + if (methodMatches(method, &routes[i]) && strcmp(url, routes[i].path) == 0) { //page = routes[i].routeAction(url); Object emptyList = listObject(); nf_addToList(&emptyList, numberObject(0)); diff --git a/src/web.h b/src/web.h index 99cd8ac..ab778eb 100644 --- a/src/web.h +++ b/src/web.h @@ -1,10 +1,15 @@ #include "object.h" +enum RouteType { + GET, + POST, +}; + struct Route { const char* path; Object routeAction; struct Environment* env; - //const char* text; + enum RouteType type; }; int addRoute(struct Route route);