generated from vilor/hyde-template
Ivan Polyakov
3 years ago
19 changed files with 720 additions and 231 deletions
@ -0,0 +1,30 @@
@@ -0,0 +1,30 @@
|
||||
# Copyright (C) 2022 Ivan Polyakov
|
||||
#
|
||||
# This file is part of vilor's website.
|
||||
#
|
||||
# Vilor's website is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU General Public License as published by
|
||||
# the Free Software Foundation, either version 3 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# Vilor's website is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU General Public License
|
||||
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
include config.mk |
||||
|
||||
all: wwwvilorcgi |
||||
|
||||
wwwvilorcgi: query.o template.o routes.o utils.o main.o |
||||
$(CC) $^ -o $@ $(LDFLAGS)
|
||||
|
||||
%.o: src/%.c |
||||
$(CC) $(CFLAGS) $^ -c -fPIC
|
||||
|
||||
clean: |
||||
rm -f *.o wwwvilorcgi
|
||||
|
||||
.PHONY: all clean |
@ -0,0 +1,26 @@
@@ -0,0 +1,26 @@
|
||||
# Copyright (C) 2022 Ivan Polyakov
|
||||
#
|
||||
# This file is part of vilor's website.
|
||||
#
|
||||
# Vilor's website is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU General Public License as published by
|
||||
# the Free Software Foundation, either version 3 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# Vilor's website is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU General Public License
|
||||
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
CC=cc
|
||||
CFLAGS=-ansi -pedantic
|
||||
LDFLAGS=-lfcgi
|
||||
|
||||
DEBUG=
|
||||
ifdef DEBUG |
||||
CFLAGS+=-Wall -g -DDEBUG_MODE
|
||||
else |
||||
CFLAGS+=-O3
|
||||
endif |
@ -0,0 +1,86 @@
@@ -0,0 +1,86 @@
|
||||
/*
|
||||
Copyright (C) 2022 Ivan Polyakov |
||||
|
||||
This file is part of vilor's website. |
||||
|
||||
Vilor's website is free software: you can redistribute it and/or modify |
||||
it under the terms of the GNU General Public License as published by |
||||
the Free Software Foundation, either version 3 of the License, or |
||||
(at your option) any later version. |
||||
|
||||
Vilor's website is distributed in the hope that it will be useful, |
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of |
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||||
GNU General Public License for more details. |
||||
|
||||
You should have received a copy of the GNU General Public License |
||||
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/ |
||||
#include <sys/types.h> |
||||
#include <stdio.h> |
||||
#include <fcgi_config.h> |
||||
#include <fcgiapp.h> |
||||
#include <string.h> |
||||
#include "server.h" |
||||
#include "routes.h" |
||||
#include "utils.h" |
||||
|
||||
static int socketId;
|
||||
|
||||
static int send_response(FCGX_Request *req, struct route *route) |
||||
{ |
||||
char resbuff[FBUFFSZ]; |
||||
|
||||
memset(resbuff, '\0', FBUFFSZ); |
||||
|
||||
if (route->resh(route, req, resbuff)) { |
||||
fputs("Can't create response\n", stderr); |
||||
return 2; |
||||
} |
||||
|
||||
/* sending response */ |
||||
FCGX_PutS("Content-type: text/html" CRLF CRLF, req->out); |
||||
FCGX_PutS(resbuff, req->out); |
||||
FCGX_PutS(CRLF CRLF, req->out); |
||||
|
||||
return 0; |
||||
} |
||||
|
||||
int main()
|
||||
{
|
||||
FCGX_Request req; |
||||
|
||||
FCGX_Init();
|
||||
|
||||
if ((socketId = FCGX_OpenSocket(SOCKET_PATH, 20)) < 0) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (FCGX_InitRequest(&req, socketId, 0)) {
|
||||
fputs("Can't init request\n", stderr);
|
||||
return 2;
|
||||
} |
||||
|
||||
while (1) { |
||||
int rc; |
||||
struct route *route; |
||||
|
||||
rc = FCGX_Accept_r(&req); |
||||
|
||||
if (rc < 0) { |
||||
fputs("Can't accept new request\n", stderr); |
||||
break; |
||||
} |
||||
|
||||
if (!(route = find_route(&req))) { |
||||
fputs("Invalid URI\n", stderr); |
||||
continue; |
||||
} |
||||
|
||||
send_response(&req, route); |
||||
|
||||
FCGX_Finish_r(&req); |
||||
} |
||||
|
||||
return 0;
|
||||
} |
@ -0,0 +1,70 @@
@@ -0,0 +1,70 @@
|
||||
/*
|
||||
Copyright (C) 2022 Ivan Polyakov |
||||
|
||||
This file is part of vilor's website. |
||||
|
||||
Vilor's website is free software: you can redistribute it and/or modify |
||||
it under the terms of the GNU General Public License as published by |
||||
the Free Software Foundation, either version 3 of the License, or |
||||
(at your option) any later version. |
||||
|
||||
Vilor's website is distributed in the hope that it will be useful, |
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of |
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||||
GNU General Public License for more details. |
||||
|
||||
You should have received a copy of the GNU General Public License |
||||
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/ |
||||
#include "query.h" |
||||
#include "utils.h" |
||||
#include <string.h> |
||||
#include <stdio.h> |
||||
|
||||
void read_params(char *query, struct param *params) |
||||
{ |
||||
struct param *param = NULL; |
||||
|
||||
query = strtok(query, PARAM_DEL); |
||||
while (query) { |
||||
if ((param = find_param_by_key(params, query))) { |
||||
size_t valsz = 0; |
||||
|
||||
if (!(query = strtok(NULL, PARAM_DEL))) { |
||||
break; |
||||
} |
||||
|
||||
valsz = strlen(query) + 1; |
||||
if (valsz <= MAX_PARAMVAL_SZ) { |
||||
memcpy(param->val, query, valsz); |
||||
strreplace(param->val, "%23", "#"); |
||||
} |
||||
} |
||||
|
||||
query = strtok(NULL, PARAM_DEL); |
||||
} |
||||
} |
||||
|
||||
struct param *find_param_by_key(struct param *params, const char *key) |
||||
{ |
||||
int i = 0; |
||||
|
||||
while (params[i].key) { |
||||
if (!strcmp(key, params[i].key)) { |
||||
return ¶ms[i]; |
||||
} |
||||
|
||||
i++; |
||||
} |
||||
|
||||
return NULL; |
||||
} |
||||
|
||||
void reset_params(struct param *params) |
||||
{ |
||||
int i = 0; |
||||
while (params[i].key) { |
||||
strcpy(params[i].val, params[i].defaultval); |
||||
i++; |
||||
} |
||||
} |
@ -0,0 +1,46 @@
@@ -0,0 +1,46 @@
|
||||
/*
|
||||
Copyright (C) 2022 Ivan Polyakov |
||||
|
||||
This file is part of vilor's website. |
||||
|
||||
Vilor's website is free software: you can redistribute it and/or modify |
||||
it under the terms of the GNU General Public License as published by |
||||
the Free Software Foundation, either version 3 of the License, or |
||||
(at your option) any later version. |
||||
|
||||
Vilor's website is distributed in the hope that it will be useful, |
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of |
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||||
GNU General Public License for more details. |
||||
|
||||
You should have received a copy of the GNU General Public License |
||||
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/ |
||||
#ifndef QUERY_H_ENTRY |
||||
#define QUERY_H_ENTRY |
||||
|
||||
#ifdef __cplusplus |
||||
extern "C" { |
||||
#endif |
||||
|
||||
#define PARAM_DEL "&=" |
||||
#define PARAM_BUFFSZ 100 |
||||
#define MAX_PARAMVAL_SZ 10 |
||||
|
||||
struct param { |
||||
const char *key; |
||||
char *defaultval; |
||||
char val[MAX_PARAMVAL_SZ]; |
||||
}; |
||||
|
||||
struct param *find_param_by_key(struct param *params, const char *key); |
||||
|
||||
void read_params(char *query, struct param *params); |
||||
|
||||
void reset_params(struct param *params); |
||||
|
||||
#ifdef __cplusplus |
||||
}; |
||||
#endif |
||||
|
||||
#endif /* QUERY_H_ENTRY */ |
@ -0,0 +1,70 @@
@@ -0,0 +1,70 @@
|
||||
/*
|
||||
Copyright (C) 2022 Ivan Polyakov |
||||
|
||||
This file is part of vilor's website. |
||||
|
||||
Vilor's website is free software: you can redistribute it and/or modify |
||||
it under the terms of the GNU General Public License as published by |
||||
the Free Software Foundation, either version 3 of the License, or |
||||
(at your option) any later version. |
||||
|
||||
Vilor's website is distributed in the hope that it will be useful, |
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of |
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||||
GNU General Public License for more details. |
||||
|
||||
You should have received a copy of the GNU General Public License |
||||
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/ |
||||
#include "routes.h" |
||||
#include "server.h" |
||||
#include "template.h" |
||||
#include <string.h> |
||||
#include <stdio.h> |
||||
|
||||
int res_scrollbar(struct route *route, FCGX_Request *req, char buff[FBUFFSZ]) |
||||
{ |
||||
char fpath[ROUTESZ]; |
||||
|
||||
sprintf(fpath, "%s%s", STATIC_PATH, route->uri); |
||||
|
||||
read_params(FCGX_GetParam("QUERY_STRING", req->envp), route->params); |
||||
|
||||
if (rendertpl(fpath, buff, route->params)) { |
||||
return 1; |
||||
} |
||||
|
||||
reset_params(route->params); |
||||
|
||||
return 0; |
||||
} |
||||
|
||||
struct route *find_route(FCGX_Request *request) |
||||
{ |
||||
char buff[ROUTESZ], *uri; |
||||
int i = 0; |
||||
|
||||
uri = FCGX_GetParam("REQUEST_URI", request->envp); |
||||
|
||||
while (routes[i].uri) { |
||||
size_t routelen, urilen; |
||||
|
||||
routelen = strlen(routes[i].uri); |
||||
urilen = strlen(uri); |
||||
|
||||
if (urilen < routelen) { |
||||
continue; |
||||
} |
||||
|
||||
memcpy(buff, uri, routelen); |
||||
buff[routelen] = '\0'; |
||||
|
||||
if (!strcmp(buff, routes[i].uri)) { |
||||
return &routes[i]; |
||||
} |
||||
|
||||
i++; |
||||
} |
||||
|
||||
return NULL; |
||||
} |
@ -0,0 +1,74 @@
@@ -0,0 +1,74 @@
|
||||
/*
|
||||
Copyright (C) 2022 Ivan Polyakov |
||||
|
||||
This file is part of vilor's website. |
||||
|
||||
Vilor's website is free software: you can redistribute it and/or modify |
||||
it under the terms of the GNU General Public License as published by |
||||
the Free Software Foundation, either version 3 of the License, or |
||||
(at your option) any later version. |
||||
|
||||
Vilor's website is distributed in the hope that it will be useful, |
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of |
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||||
GNU General Public License for more details. |
||||
|
||||
You should have received a copy of the GNU General Public License |
||||
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/ |
||||
#ifndef ROUTES_H_ENTRY |
||||
#define ROUTES_H_ENTRY |
||||
|
||||
#include <stddef.h> |
||||
#include <fcgiapp.h> |
||||
#include "server.h" |
||||
#include "query.h" |
||||
|
||||
#define ROUTESZ 255 |
||||
|
||||
#ifdef __cplusplus |
||||
extern "C" { |
||||
#endif |
||||
|
||||
struct route { |
||||
const char *uri; |
||||
int (*resh)(struct route *route, FCGX_Request *req, char dest[FBUFFSZ]); |
||||
struct param params[15]; |
||||
}; |
||||
|
||||
/* responses */ |
||||
int res_scrollbar(struct route *route, FCGX_Request *req, char dest[FBUFFSZ]); |
||||
|
||||
static struct route routes[2] = { |
||||
{ |
||||
"/webapps/scrollbar.xhtml", |
||||
&res_scrollbar, |
||||
{ |
||||
/* key defaultval val */ |
||||
{ "sbw", "10px", "" }, |
||||
|
||||
{ "thumbclr", "#9b3e46", "" }, |
||||
|
||||
{ "thumbbstl", "solid", "" }, |
||||
{ "thumbbrad", "8px", "" }, |
||||
{ "thumbbw", "1px", "" }, |
||||
{ "thumbbclr", "#ffffff", "" }, |
||||
|
||||
|
||||
{ "trackclr", "#3b4252", "" }, |
||||
{ "trackbrad", "8px", "" }, |
||||
{ "trackmt", "0px", "" }, |
||||
{ "trackmb", "0px", "" }, |
||||
NULL |
||||
} |
||||
}, |
||||
NULL |
||||
}; |
||||
|
||||
struct route *find_route(FCGX_Request *req); |
||||
|
||||
#ifdef __cplusplus |
||||
}; |
||||
#endif |
||||
|
||||
#endif /* ROUTES_H_ENTRY */ |
@ -0,0 +1,30 @@
@@ -0,0 +1,30 @@
|
||||
/*
|
||||
Copyright (C) 2022 Ivan Polyakov |
||||
|
||||
This file is part of vilor's website. |
||||
|
||||
Vilor's website is free software: you can redistribute it and/or modify |
||||
it under the terms of the GNU General Public License as published by |
||||
the Free Software Foundation, either version 3 of the License, or |
||||
(at your option) any later version. |
||||
|
||||
Vilor's website is distributed in the hope that it will be useful, |
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of |
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||||
GNU General Public License for more details. |
||||
|
||||
You should have received a copy of the GNU General Public License |
||||
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/ |
||||
#ifndef SERVER_H_ENTRY |
||||
#define SERVER_H_ENTRY |
||||
|
||||
#define SOCKET_PATH "/tmp/vilor.one.sock" |
||||
#define STATIC_PATH "/var/www/html/vilor" |
||||
|
||||
#define CRLF "\r\n" |
||||
#define XHTML_DOCTYPE "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">"
|
||||
|
||||
#define FBUFFSZ 8192 |
||||
|
||||
#endif /* SERVER_H_ENTRY */ |
@ -0,0 +1,60 @@
@@ -0,0 +1,60 @@
|
||||
/*
|
||||
Copyright (C) 2022 Ivan Polyakov |
||||
|
||||
This file is part of vilor's website. |
||||
|
||||
Vilor's website is free software: you can redistribute it and/or modify |
||||
it under the terms of the GNU General Public License as published by |
||||
the Free Software Foundation, either version 3 of the License, or |
||||
(at your option) any later version. |
||||
|
||||
Vilor's website is distributed in the hope that it will be useful, |
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of |
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||||
GNU General Public License for more details. |
||||
|
||||
You should have received a copy of the GNU General Public License |
||||
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/ |
||||
#include "template.h" |
||||
#include "utils.h" |
||||
#include <stddef.h> |
||||
#include <stdio.h> |
||||
#include <string.h> |
||||
|
||||
int rendertpl(const char *path, char dest[FBUFFSZ], struct param *params) |
||||
{ |
||||
FILE *fp = NULL; |
||||
int i = 0; |
||||
char paramtpl[100]; |
||||
|
||||
if (!(fp = fopen(path, "r"))) { |
||||
fputs("Can't open the file\n", stderr); |
||||
return 1; |
||||
} |
||||
|
||||
if (!fread(dest, sizeof(char), FBUFFSZ, fp)) { |
||||
fputs("Can't read file contents\n", stderr); |
||||
fclose(fp); |
||||
return 2; |
||||
} |
||||
fclose(fp); |
||||
|
||||
#ifdef DEBUG_MODE |
||||
puts("Interpolation"); |
||||
#endif |
||||
while (params[i].key) { |
||||
char *val = strlen(params[i].val) |
||||
? params[i].val |
||||
: params[i].defaultval; |
||||
|
||||
sprintf(paramtpl, "{{ %s }}", params[i].key); |
||||
#ifdef DEBUG_MODE |
||||
printf("Key: %s; Val: %s; Real Val: %s\n", paramtpl, params[i].val, val); |
||||
#endif |
||||
while(strreplace(dest, paramtpl, val)) {} |
||||
i++; |
||||
} |
||||
|
||||
return 0; |
||||
} |
@ -0,0 +1,35 @@
@@ -0,0 +1,35 @@
|
||||
/*
|
||||
Copyright (C) 2022 Ivan Polyakov |
||||
|
||||
This file is part of vilor's website. |
||||
|
||||
Vilor's website is free software: you can redistribute it and/or modify |
||||
it under the terms of the GNU General Public License as published by |
||||
the Free Software Foundation, either version 3 of the License, or |
||||
(at your option) any later version. |
||||
|
||||
Vilor's website is distributed in the hope that it will be useful, |
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of |
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||||
GNU General Public License for more details. |
||||
|
||||
You should have received a copy of the GNU General Public License |
||||
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/ |
||||
#ifndef TEMPLATE_H_ENTRY |
||||
#define TEMPLATE_H_ENTRY |
||||
|
||||
#include "query.h" |
||||
#include "server.h" |
||||
|
||||
#ifdef __cplusplus |
||||
extern "C" { |
||||
#endif |
||||
|
||||
int rendertpl(const char *path, char dest[FBUFFSZ], struct param *params); |
||||
|
||||
#ifdef __cplusplus |
||||
}; |
||||
#endif |
||||
|
||||
#endif /* TEMPLATE_H_ENTRY */ |
@ -0,0 +1,35 @@
@@ -0,0 +1,35 @@
|
||||
/*
|
||||
Copyright (C) 2022 Ivan Polyakov |
||||
|
||||
This file is part of vilor's website. |
||||
|
||||
Vilor's website is free software: you can redistribute it and/or modify |
||||
it under the terms of the GNU General Public License as published by |
||||
the Free Software Foundation, either version 3 of the License, or |
||||
(at your option) any later version. |
||||
|
||||
Vilor's website is distributed in the hope that it will be useful, |
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of |
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||||
GNU General Public License for more details. |
||||
|
||||
You should have received a copy of the GNU General Public License |
||||
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/ |
||||
#include <string.h> |
||||
#include "utils.h" |
||||
|
||||
char *strreplace(char *s, const char *s1, const char *s2) { |
||||
char *p = strstr(s, s1); |
||||
|
||||
if (p) { |
||||
size_t len1 = strlen(s1); |
||||
size_t len2 = strlen(s2); |
||||
if (len1 != len2) { |
||||
memmove(p + len2, p + len1, strlen(p + len1) + 1); |
||||
} |
||||
memcpy(p, s2, len2); |
||||
} |
||||
|
||||
return p; |
||||
} |
@ -0,0 +1,24 @@
@@ -0,0 +1,24 @@
|
||||
/*
|
||||
Copyright (C) 2022 Ivan Polyakov |
||||
|
||||
This file is part of vilor's website. |
||||
|
||||
Vilor's website is free software: you can redistribute it and/or modify |
||||
it under the terms of the GNU General Public License as published by |
||||
the Free Software Foundation, either version 3 of the License, or |
||||
(at your option) any later version. |
||||
|
||||
Vilor's website is distributed in the hope that it will be useful, |
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of |
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||||
GNU General Public License for more details. |
||||
|
||||
You should have received a copy of the GNU General Public License |
||||
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/ |
||||
#ifndef UTILS_H_ENTRY |
||||
#define UTILS_H_ENTRY |
||||
|
||||
char *strreplace(char *s, const char *s1, const char *s2); |
||||
|
||||
#endif /* UTILS_H_ENTRY */ |
Loading…
Reference in new issue