changed ctx to pointer (otherwise response header changes can't be saved)

This commit is contained in:
overflowerror 2021-06-13 12:33:21 +02:00
parent 47747af272
commit 387f79e1cf
8 changed files with 25 additions and 24 deletions

View file

@ -2,39 +2,40 @@
#include <string.h> #include <string.h>
#include <controller.h> #include <controller.h>
#include <cookies.h>
#include "entities.h" #include "entities.h"
GET("/", hello); GET("/", hello);
GET("/index.*", hello); GET("/index.*", hello);
response_t hello(ctx_t ctx) { response_t hello(ctx_t* ctx) {
return rawResponse(200, "Hello World\n"); return rawResponse(200, "Hello World\n");
} }
GET("/foobar", foobar); GET("/foobar", foobar);
response_t foobar(ctx_t ctx) { response_t foobar(ctx_t* ctx) {
return fileResponse("demo/foobar.txt"); return fileResponse("demo/foobar.txt");
} }
response_t authenticate(ctx_t ctx) { response_t authenticate(ctx_t* ctx) {
if (ctx.auth.type != BASIC) { if (ctx->auth.type != BASIC) {
return basicAuthResponse(401, "Protected Area"); return basicAuthResponse(401, "Protected Area");
} }
if (strcmp(ctx.auth.basic.user, "admin") != 0 || if (strcmp(ctx->auth.basic.user, "admin") != 0 ||
strcmp(ctx.auth.basic.password, "password") != 0 strcmp(ctx->auth.basic.password, "password") != 0
) { ) {
// username or password wrong // username or password wrong
return basicAuthResponse(401, "Protected Area"); return basicAuthResponse(401, "Protected Area");
} }
return next(); return next();
} }
GET("/user", authenticate, user); GET("/user", authenticate, user);
response_t user(ctx_t ctx) { POST("/user", authenticate, user);
response_t user(ctx_t* ctx) {
user_t user = { user_t user = {
.username = "overflowerror", .username = "overflowerror",
.github = "https://github.com/overflowerror" .github = "https://github.com/overflowerror"
@ -44,6 +45,6 @@ response_t user(ctx_t ctx) {
} }
GET("/template", template); GET("/template", template);
response_t template(ctx_t ctx) { response_t template(ctx_t* ctx) {
return templateResponse(200, "demo.templ", "Page Title", "Overflow"); return templateResponse(200, "demo.templ", "Page Title", "Overflow");
} }

View file

@ -27,7 +27,7 @@ static void handler(struct request request, struct response _response) {
responseHeaders: headers_create() responseHeaders: headers_create()
}; };
response_t response = routerHandler(ctx); response_t response = routerHandler(&ctx);
if (response.output == NULL) { if (response.output == NULL) {
response = errorResponse(500, "route did not provide a reponse handler"); response = errorResponse(500, "route did not provide a reponse handler");
} }
@ -51,7 +51,7 @@ static void handler(struct request request, struct response _response) {
return; return;
} }
response.output(out, response._userData, ctx); response.output(out, response._userData, &ctx);
fclose(out); fclose(out);
} }

View file

@ -114,7 +114,7 @@ int main(int argc, char** argv) {
responseHeaders: headers_create() responseHeaders: headers_create()
}; };
response_t response = routerHandler(ctx); response_t response = routerHandler(&ctx);
if (response.output == NULL) { if (response.output == NULL) {
response = errorResponse(500, "route did not provide a reponse handler"); response = errorResponse(500, "route did not provide a reponse handler");
} }
@ -132,7 +132,7 @@ int main(int argc, char** argv) {
headers_free(&response.headers); headers_free(&response.headers);
headers_free(&ctx.responseHeaders); headers_free(&ctx.responseHeaders);
response.output(stdout, response._userData, ctx); response.output(stdout, response._userData, &ctx);
return 0; return 0;
} }

View file

@ -3,6 +3,6 @@
#include "request.h" #include "request.h"
typedef response_t (handle_t)(ctx_t); typedef response_t (handle_t)(ctx_t*);
#endif #endif

View file

@ -31,13 +31,13 @@ void setDefaultErrorFormat(errorformat_t format) {
errorformat = format; errorformat = format;
} }
static void rawOutputAndFree(FILE* out, void* _userData, ctx_t ctx) { static void rawOutputAndFree(FILE* out, void* _userData, ctx_t* ctx) {
fprintf(out, "%s", (char*) _userData); fprintf(out, "%s", (char*) _userData);
free(_userData); free(_userData);
} }
static void rawOutput(FILE* out, void* _userData, ctx_t ctx) { static void rawOutput(FILE* out, void* _userData, ctx_t* ctx) {
fprintf(out, "%s", (const char*) _userData); fprintf(out, "%s", (const char*) _userData);
} }
@ -70,7 +70,7 @@ struct statusdata {
const char* message; const char* message;
}; };
static void statusOutput(FILE* out, void* _userData, ctx_t ctx) { static void statusOutput(FILE* out, void* _userData, ctx_t* ctx) {
struct statusdata* data = (struct statusdata*) _userData; struct statusdata* data = (struct statusdata*) _userData;
const char* statusString = getStatusStrings(data->status).statusString; const char* statusString = getStatusStrings(data->status).statusString;
@ -80,7 +80,7 @@ static void statusOutput(FILE* out, void* _userData, ctx_t ctx) {
"status", json_long(data->status), "status", json_long(data->status),
"error", json_string(statusString), "error", json_string(statusString),
"message", json_string(data->message), "message", json_string(data->message),
"path", json_string(ctx.path) "path", json_string(ctx->path)
); );
free(data); free(data);
free(tmp); free(tmp);
@ -126,7 +126,7 @@ response_t errorResponse(int status, const char* message) {
return statusResponse(status, message); return statusResponse(status, message);
} }
static void fileOutput(FILE* out, void* _userData, ctx_t ctx) { static void fileOutput(FILE* out, void* _userData, ctx_t* ctx) {
FILE* in = (FILE*) _userData; FILE* in = (FILE*) _userData;
#define READ_BUFFER_SIZE (1024) #define READ_BUFFER_SIZE (1024)
@ -171,7 +171,7 @@ response_t fileResponse(const char* file) {
return response; return response;
} }
static void jsonOutput(FILE* output, void* _userData, ctx_t ctx) { static void jsonOutput(FILE* output, void* _userData, ctx_t* ctx) {
jsonValue_t* json = (jsonValue_t*) _userData; jsonValue_t* json = (jsonValue_t*) _userData;
char* result = json_stringify(json); char* result = json_stringify(json);

View file

@ -27,7 +27,7 @@ typedef struct {
struct headers headers; struct headers headers;
void* _userData; void* _userData;
void (*output) (FILE* conenction, void* _userData, ctx_t ctx); void (*output) (FILE* conenction, void* _userData, ctx_t* ctx);
} response_t; } response_t;
typedef enum { typedef enum {

View file

@ -121,8 +121,8 @@ int registerRoute(method_t method, const char* path, ...) {
return 0; return 0;
} }
response_t routerHandler(ctx_t ctx) { response_t routerHandler(ctx_t* ctx) {
struct route* route = findRoute(ctx.method, ctx.path); struct route* route = findRoute(ctx->method, ctx->path);
if (route == NULL) { if (route == NULL) {
return errorResponse(404, "no route found"); return errorResponse(404, "no route found");
} }

View file

@ -5,6 +5,6 @@
int registerRoute(method_t method, const char* path, ...); int registerRoute(method_t method, const char* path, ...);
response_t routerHandler(ctx_t ctx); response_t routerHandler(ctx_t* ctx);
#endif #endif