From 5fdb8d8f43e15b9581e8320153eb2e8c24cedac8 Mon Sep 17 00:00:00 2001 From: sijanec Date: Tue, 1 Dec 2020 23:50:20 +0100 Subject: =?UTF-8?q?dodal=20mo=C5=BEnost=20komentiranja?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/Makefile | 6 -- api/api | Bin 17992 -> 0 bytes api/api.c | 244 -------------------------------------------------------- api/c/Makefile | 7 ++ api/c/api | Bin 0 -> 17992 bytes api/c/api.c | 244 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ api/c/urlcode.c | 48 +++++++++++ api/php/api.php | 60 ++++++++++++++ api/urlcode.c | 48 ----------- 9 files changed, 359 insertions(+), 298 deletions(-) delete mode 100644 api/Makefile delete mode 100755 api/api delete mode 100644 api/api.c create mode 100644 api/c/Makefile create mode 100755 api/c/api create mode 100644 api/c/api.c create mode 100644 api/c/urlcode.c create mode 100644 api/php/api.php delete mode 100644 api/urlcode.c (limited to 'api') diff --git a/api/Makefile b/api/Makefile deleted file mode 100644 index 7b8ab7d..0000000 --- a/api/Makefile +++ /dev/null @@ -1,6 +0,0 @@ -compile: - gcc api.c -Wall -lfcgi --pedantic -lpthread -o api -prepare: - wget http://www.geekhideout.com/downloads/urlcode.c -clean: - rm urlencode.c api diff --git a/api/api b/api/api deleted file mode 100755 index cd48526..0000000 Binary files a/api/api and /dev/null differ diff --git a/api/api.c b/api/api.c deleted file mode 100644 index e452617..0000000 --- a/api/api.c +++ /dev/null @@ -1,244 +0,0 @@ -/* - * threaded.c -- A simple multi-threaded FastCGI application. - */ - -#ifndef lint -// static const char rcsid[] = "$Id: threaded.c,v 1.9 2001/11/20 03:23:21 robs Exp $"; // just popps up warnings -#endif /* not lint */ - -#include "fcgi_config.h" - -#include -#include - -#ifdef HAVE_UNISTD_H -#include -#endif - -#include "fcgiapp.h" - -#include -#include -#include -#include "urlcode.c" -#include -#include -#define MALLOC_RETURNED_NULL fprintf(stderr, "%s()@" __FILE__ ":%d: malloc() returned NULL\n", __func__, __LINE__); - - -#define THREAD_COUNT 20 - -static int counts[THREAD_COUNT]; - -struct assoc_s { - char * key; - char * value; -}; // value je lahko NULL - -void free_assoc_s(struct assoc_s * s) { - if (s->key != NULL) { - free(s->key); - s->key = NULL; - } - if (s->value != NULL) { - free(s->value); - s->value = NULL; - } - free(s); - s = NULL; - return; -} - -struct request_uri_s { - char * path; - struct assoc_s ** params; // to je "array". zadnji naj bo NULL. -}; - -void free_request_uri_s(struct request_uri_s * s) { - int unsigned i = 0; - while (s->params[i] != NULL) { - free_assoc_s(s->params[i]); - s->params[i++] = NULL; - } - free(s->path); - s->path = NULL; - free(s->params); - s->params = NULL; - free(s); - s = NULL; - return; -} - -struct assoc_s ** parse_html_keyvalue(const char * t) { - struct assoc_s ** params = malloc(sizeof(void *)); - if (params == NULL) { - MALLOC_RETURNED_NULL - return NULL; - } - unsigned int o = 0; // številka parametra od 0 vključno z 0 - unsigned int n = 0; // 0 če smo v key, 1 če smo v value, 3 če je konec urija - unsigned int a = 0; // nakazuje pozicijo naslednjega znaka, ki ga bomo napisa. - params[0] = malloc(sizeof(struct assoc_s)); - if (params[0] == NULL) { - MALLOC_RETURNED_NULL - return NULL; - } - for (unsigned int e = 0; n != 3 && e <= UINT_MAX; e++) { - switch (t[e]) { - case '\0': - params = realloc(params, sizeof(struct assoc_s)*(o+1)); - if (params == NULL) { - MALLOC_RETURNED_NULL - return NULL; - } // zadnji mora biti null - params[o+1] = NULL; - n = 3; - break; - case '=': - if (n == 0) { // ni treba mallocat, realloca se pred vsakim writom bajta - n = 1; - a = 0; - } else { // this is some undocumented behaviour, adding = to param value - params[o]->value = realloc(params[o]->value, a+1); - if (params[o]->value == NULL) { - MALLOC_RETURNED_NULL - return NULL; - } - params[o]->value[a++] = '='; - } - break; - case '&': // tu je treba mallocat, ker assoc_s še ne obstaja za naslednji - n = 0; - a = 0; - o++; - params = realloc(params, sizeof(struct assoc_s)*(o+1)); - if (params == NULL) { - MALLOC_RETURNED_NULL - return NULL; - } - params[o] = malloc(sizeof(struct assoc_s)); // value kasneje - if (params[o] == NULL) { - MALLOC_RETURNED_NULL - return NULL; - } - break; - default: - if (n == 0) { - params[o]->key = realloc(params[o]->key, a+1); - if (params[o]->key == NULL) { - MALLOC_RETURNED_NULL - return NULL; - } - params[o]->key[a++] = t[e]; - } else { - params[o]->value = realloc(params[o]->value, a+1); - if (params[o]->value == NULL) { - MALLOC_RETURNED_NULL - return NULL; - } - params[o]->value[a++] = t[e]; - } - break; - } - } - return params; -} - -struct request_uri_s * parse_request_uri(char * uri) { // returns NULL on fail! - struct request_uri_s * parsed = malloc(sizeof(struct request_uri_s)); - if (parsed == NULL) { // and returns pointer to ONE request_uri_s on success^ - MALLOC_RETURNED_NULL - return NULL; - } - char * t = strchr(uri, '?'); // remember to free /\ ! - if (t != NULL) - t[0] = '\0'; // uri vedno vseeno pride ven nespremenjen/popravljen - parsed->path = malloc(sizeof(char)*strlen(uri)+1); //znak po path je itak NULL - if (parsed->path == NULL) { // malloc returna NULL, fucking panic! - if (t != NULL) // popravimo še \0 nazaj na ? - t[0] = '?'; - MALLOC_RETURNED_NULL - return NULL; - } - strncpy(parsed->path, uri, strlen(uri)+1); // torej zadnji znak patha bo NULL - parsed->params = NULL; // če slučajno ni query_stringa in kmalu returnamo - if (t == NULL) // ni query stringa zato je path parse dovoljšen - return parsed; // parsed.params je NULL, to pred uporabo PREVERIMO! - t[0] = '?'; // ker smo prej zaradi strlen in strncpy dali na NULL, sedaj spre. - t++; // da ne začnemo z vprašajem ampak z prvim znakom - parsed->params = parse_html_keyvalue(t); - return parsed; -} - -static void *doit(void *a) { - intptr_t rc, i, thread_id = (intptr_t)a; - pid_t pid = getpid(); - FCGX_Request request; - char *server_name; - char * uri_to_parse; - struct request_uri_s * parseduri; - - FCGX_InitRequest(&request, 0, 0); - - for (;;) { - static pthread_mutex_t accept_mutex = PTHREAD_MUTEX_INITIALIZER; - static pthread_mutex_t counts_mutex = PTHREAD_MUTEX_INITIALIZER; - - /* Some platforms require accept() serialization, some don't.. */ - pthread_mutex_lock(&accept_mutex); - rc = FCGX_Accept_r(&request); - pthread_mutex_unlock(&accept_mutex); - - if (rc < 0) - break; - - server_name = FCGX_GetParam("SERVER_NAME", request.envp); - uri_to_parse = FCGX_GetParam("REQUEST_URI", request.envp); - parseduri = parse_request_uri(uri_to_parse); - if (strchr(parseduri->path, 'S') != NULL) { - exit(0); - } - if (parseduri == NULL) { - FCGX_FPrintF(request.out, "Status: 500 Zaledna napaka\r\n" - "Content-Type: text/plain; charset=utf-8\r\n\r\n" - "zgodila se je napaka na strežniku med procesiranjem URIja.\r\n"); - free_request_uri_s(parseduri); - goto doit_finish; - } - FCGX_FPrintF(request.out, - "Status: 200 V redu\r\n" - "Content-type: text/html; charset=utf-8\r\n" - "\r\n" - "FastCGI Pozdravljen svet! (večnitenje v C, knjižnica fcgiapp)" - "

FastCGI Pozdravljen svet! (večnitenje v C, knjižnica fcgiapp)

" - "Nit %d, Proces %ld

" - "Števci zahtev za %d niti na strežniku %s

", - thread_id, pid, - THREAD_COUNT, server_name ? server_name : "?"); - free_request_uri_s(parseduri); - pthread_mutex_lock(&counts_mutex); - ++counts[thread_id]; - for (i = 0; i < THREAD_COUNT; i++) - FCGX_FPrintF(request.out, "%5d " , counts[i]); - pthread_mutex_unlock(&counts_mutex); - -doit_finish: - FCGX_Finish_r(&request); - } - - return NULL; -} - -int main(void) { - intptr_t i; - pthread_t id[THREAD_COUNT]; - - FCGX_Init(); - - for (i = 1; i < THREAD_COUNT; i++) - pthread_create(&id[i], NULL, doit, (void*)i); - - doit(0); // trenutno nit uporabimo tudi kot handler, zakaj pa ne (: - - return 0; -} diff --git a/api/c/Makefile b/api/c/Makefile new file mode 100644 index 0000000..5f4f70a --- /dev/null +++ b/api/c/Makefile @@ -0,0 +1,7 @@ +compile: + gcc api.c -Wall -lfcgi --pedantic -lpthread -o api +prepare: + wget http://www.geekhideout.com/downloads/urlcode.c +clean: + rm urlencode.c api + diff --git a/api/c/api b/api/c/api new file mode 100755 index 0000000..cd48526 Binary files /dev/null and b/api/c/api differ diff --git a/api/c/api.c b/api/c/api.c new file mode 100644 index 0000000..e452617 --- /dev/null +++ b/api/c/api.c @@ -0,0 +1,244 @@ +/* + * threaded.c -- A simple multi-threaded FastCGI application. + */ + +#ifndef lint +// static const char rcsid[] = "$Id: threaded.c,v 1.9 2001/11/20 03:23:21 robs Exp $"; // just popps up warnings +#endif /* not lint */ + +#include "fcgi_config.h" + +#include +#include + +#ifdef HAVE_UNISTD_H +#include +#endif + +#include "fcgiapp.h" + +#include +#include +#include +#include "urlcode.c" +#include +#include +#define MALLOC_RETURNED_NULL fprintf(stderr, "%s()@" __FILE__ ":%d: malloc() returned NULL\n", __func__, __LINE__); + + +#define THREAD_COUNT 20 + +static int counts[THREAD_COUNT]; + +struct assoc_s { + char * key; + char * value; +}; // value je lahko NULL + +void free_assoc_s(struct assoc_s * s) { + if (s->key != NULL) { + free(s->key); + s->key = NULL; + } + if (s->value != NULL) { + free(s->value); + s->value = NULL; + } + free(s); + s = NULL; + return; +} + +struct request_uri_s { + char * path; + struct assoc_s ** params; // to je "array". zadnji naj bo NULL. +}; + +void free_request_uri_s(struct request_uri_s * s) { + int unsigned i = 0; + while (s->params[i] != NULL) { + free_assoc_s(s->params[i]); + s->params[i++] = NULL; + } + free(s->path); + s->path = NULL; + free(s->params); + s->params = NULL; + free(s); + s = NULL; + return; +} + +struct assoc_s ** parse_html_keyvalue(const char * t) { + struct assoc_s ** params = malloc(sizeof(void *)); + if (params == NULL) { + MALLOC_RETURNED_NULL + return NULL; + } + unsigned int o = 0; // številka parametra od 0 vključno z 0 + unsigned int n = 0; // 0 če smo v key, 1 če smo v value, 3 če je konec urija + unsigned int a = 0; // nakazuje pozicijo naslednjega znaka, ki ga bomo napisa. + params[0] = malloc(sizeof(struct assoc_s)); + if (params[0] == NULL) { + MALLOC_RETURNED_NULL + return NULL; + } + for (unsigned int e = 0; n != 3 && e <= UINT_MAX; e++) { + switch (t[e]) { + case '\0': + params = realloc(params, sizeof(struct assoc_s)*(o+1)); + if (params == NULL) { + MALLOC_RETURNED_NULL + return NULL; + } // zadnji mora biti null + params[o+1] = NULL; + n = 3; + break; + case '=': + if (n == 0) { // ni treba mallocat, realloca se pred vsakim writom bajta + n = 1; + a = 0; + } else { // this is some undocumented behaviour, adding = to param value + params[o]->value = realloc(params[o]->value, a+1); + if (params[o]->value == NULL) { + MALLOC_RETURNED_NULL + return NULL; + } + params[o]->value[a++] = '='; + } + break; + case '&': // tu je treba mallocat, ker assoc_s še ne obstaja za naslednji + n = 0; + a = 0; + o++; + params = realloc(params, sizeof(struct assoc_s)*(o+1)); + if (params == NULL) { + MALLOC_RETURNED_NULL + return NULL; + } + params[o] = malloc(sizeof(struct assoc_s)); // value kasneje + if (params[o] == NULL) { + MALLOC_RETURNED_NULL + return NULL; + } + break; + default: + if (n == 0) { + params[o]->key = realloc(params[o]->key, a+1); + if (params[o]->key == NULL) { + MALLOC_RETURNED_NULL + return NULL; + } + params[o]->key[a++] = t[e]; + } else { + params[o]->value = realloc(params[o]->value, a+1); + if (params[o]->value == NULL) { + MALLOC_RETURNED_NULL + return NULL; + } + params[o]->value[a++] = t[e]; + } + break; + } + } + return params; +} + +struct request_uri_s * parse_request_uri(char * uri) { // returns NULL on fail! + struct request_uri_s * parsed = malloc(sizeof(struct request_uri_s)); + if (parsed == NULL) { // and returns pointer to ONE request_uri_s on success^ + MALLOC_RETURNED_NULL + return NULL; + } + char * t = strchr(uri, '?'); // remember to free /\ ! + if (t != NULL) + t[0] = '\0'; // uri vedno vseeno pride ven nespremenjen/popravljen + parsed->path = malloc(sizeof(char)*strlen(uri)+1); //znak po path je itak NULL + if (parsed->path == NULL) { // malloc returna NULL, fucking panic! + if (t != NULL) // popravimo še \0 nazaj na ? + t[0] = '?'; + MALLOC_RETURNED_NULL + return NULL; + } + strncpy(parsed->path, uri, strlen(uri)+1); // torej zadnji znak patha bo NULL + parsed->params = NULL; // če slučajno ni query_stringa in kmalu returnamo + if (t == NULL) // ni query stringa zato je path parse dovoljšen + return parsed; // parsed.params je NULL, to pred uporabo PREVERIMO! + t[0] = '?'; // ker smo prej zaradi strlen in strncpy dali na NULL, sedaj spre. + t++; // da ne začnemo z vprašajem ampak z prvim znakom + parsed->params = parse_html_keyvalue(t); + return parsed; +} + +static void *doit(void *a) { + intptr_t rc, i, thread_id = (intptr_t)a; + pid_t pid = getpid(); + FCGX_Request request; + char *server_name; + char * uri_to_parse; + struct request_uri_s * parseduri; + + FCGX_InitRequest(&request, 0, 0); + + for (;;) { + static pthread_mutex_t accept_mutex = PTHREAD_MUTEX_INITIALIZER; + static pthread_mutex_t counts_mutex = PTHREAD_MUTEX_INITIALIZER; + + /* Some platforms require accept() serialization, some don't.. */ + pthread_mutex_lock(&accept_mutex); + rc = FCGX_Accept_r(&request); + pthread_mutex_unlock(&accept_mutex); + + if (rc < 0) + break; + + server_name = FCGX_GetParam("SERVER_NAME", request.envp); + uri_to_parse = FCGX_GetParam("REQUEST_URI", request.envp); + parseduri = parse_request_uri(uri_to_parse); + if (strchr(parseduri->path, 'S') != NULL) { + exit(0); + } + if (parseduri == NULL) { + FCGX_FPrintF(request.out, "Status: 500 Zaledna napaka\r\n" + "Content-Type: text/plain; charset=utf-8\r\n\r\n" + "zgodila se je napaka na strežniku med procesiranjem URIja.\r\n"); + free_request_uri_s(parseduri); + goto doit_finish; + } + FCGX_FPrintF(request.out, + "Status: 200 V redu\r\n" + "Content-type: text/html; charset=utf-8\r\n" + "\r\n" + "FastCGI Pozdravljen svet! (večnitenje v C, knjižnica fcgiapp)" + "

FastCGI Pozdravljen svet! (večnitenje v C, knjižnica fcgiapp)

" + "Nit %d, Proces %ld

" + "Števci zahtev za %d niti na strežniku %s

", + thread_id, pid, + THREAD_COUNT, server_name ? server_name : "?"); + free_request_uri_s(parseduri); + pthread_mutex_lock(&counts_mutex); + ++counts[thread_id]; + for (i = 0; i < THREAD_COUNT; i++) + FCGX_FPrintF(request.out, "%5d " , counts[i]); + pthread_mutex_unlock(&counts_mutex); + +doit_finish: + FCGX_Finish_r(&request); + } + + return NULL; +} + +int main(void) { + intptr_t i; + pthread_t id[THREAD_COUNT]; + + FCGX_Init(); + + for (i = 1; i < THREAD_COUNT; i++) + pthread_create(&id[i], NULL, doit, (void*)i); + + doit(0); // trenutno nit uporabimo tudi kot handler, zakaj pa ne (: + + return 0; +} diff --git a/api/c/urlcode.c b/api/c/urlcode.c new file mode 100644 index 0000000..ecf34e1 --- /dev/null +++ b/api/c/urlcode.c @@ -0,0 +1,48 @@ +/* Converts a hex character to its integer value */ +char from_hex(char ch) { + return isdigit(ch) ? ch - '0' : tolower(ch) - 'a' + 10; +} + +/* Converts an integer value to its hex character*/ +char to_hex(char code) { + static char hex[] = "0123456789abcdef"; + return hex[code & 15]; +} + +/* Returns a url-encoded version of str */ +/* IMPORTANT: be sure to free() the returned string after use */ +char *url_encode(char *str) { + char *pstr = str, *buf = malloc(strlen(str) * 3 + 1), *pbuf = buf; + while (*pstr) { + if (isalnum(*pstr) || *pstr == '-' || *pstr == '_' || *pstr == '.' || *pstr == '~') + *pbuf++ = *pstr; + else if (*pstr == ' ') + *pbuf++ = '+'; + else + *pbuf++ = '%', *pbuf++ = to_hex(*pstr >> 4), *pbuf++ = to_hex(*pstr & 15); + pstr++; + } + *pbuf = '\0'; + return buf; +} + +/* Returns a url-decoded version of str */ +/* IMPORTANT: be sure to free() the returned string after use */ +char *url_decode(char *str) { + char *pstr = str, *buf = malloc(strlen(str) + 1), *pbuf = buf; + while (*pstr) { + if (*pstr == '%') { + if (pstr[1] && pstr[2]) { + *pbuf++ = from_hex(pstr[1]) << 4 | from_hex(pstr[2]); + pstr += 2; + } + } else if (*pstr == '+') { + *pbuf++ = ' '; + } else { + *pbuf++ = *pstr; + } + pstr++; + } + *pbuf = '\0'; + return buf; +} diff --git a/api/php/api.php b/api/php/api.php new file mode 100644 index 0000000..5c5ec82 --- /dev/null +++ b/api/php/api.php @@ -0,0 +1,60 @@ + 1024 || strlen($parent) > 7+1+256+1+19 || strlen($vzdevek) > 256) { + http_response_code(400); + header("Content-Type: text/plain"); + exit("400: Napaka. Preveč besedila - vzdevek je lahko dolg 256, sporočilo pa 1024 znakov."); + } + $datum = date("Y-m-d H:i:s"); + $cm = yaml_emit(array( + "author" => $vzdevek, + "date" => $datum, + "message" => $sporocilo, + "parent" => $parent, + "ip" => $_SERVER['REMOTE_ADDR'], + "addedby" => "PHP API 0.0.0", + "nonce" => hash('sha256', $vzdevek."|".$datum."|".random_bytes(69)) + )); + file_put_contents($commentsfile, $cm, FILE_APPEND); // pri append ni locka + break; + case "commentedit": + http_response_code(400); + header("Content-Type: text/plain"); + exit("400: Napaka. Urejanje komentarjev še ni izdelano. Napišite elektronsko sporočilo na anton+blog@sijanec.eu."); + break; + default: + http_response_code(400); + header("Content-Type: text/plain"); + exit("400: Napaka. Izbrali ste neobstoječo API metodo."); + break; + } +?> diff --git a/api/urlcode.c b/api/urlcode.c deleted file mode 100644 index ecf34e1..0000000 --- a/api/urlcode.c +++ /dev/null @@ -1,48 +0,0 @@ -/* Converts a hex character to its integer value */ -char from_hex(char ch) { - return isdigit(ch) ? ch - '0' : tolower(ch) - 'a' + 10; -} - -/* Converts an integer value to its hex character*/ -char to_hex(char code) { - static char hex[] = "0123456789abcdef"; - return hex[code & 15]; -} - -/* Returns a url-encoded version of str */ -/* IMPORTANT: be sure to free() the returned string after use */ -char *url_encode(char *str) { - char *pstr = str, *buf = malloc(strlen(str) * 3 + 1), *pbuf = buf; - while (*pstr) { - if (isalnum(*pstr) || *pstr == '-' || *pstr == '_' || *pstr == '.' || *pstr == '~') - *pbuf++ = *pstr; - else if (*pstr == ' ') - *pbuf++ = '+'; - else - *pbuf++ = '%', *pbuf++ = to_hex(*pstr >> 4), *pbuf++ = to_hex(*pstr & 15); - pstr++; - } - *pbuf = '\0'; - return buf; -} - -/* Returns a url-decoded version of str */ -/* IMPORTANT: be sure to free() the returned string after use */ -char *url_decode(char *str) { - char *pstr = str, *buf = malloc(strlen(str) + 1), *pbuf = buf; - while (*pstr) { - if (*pstr == '%') { - if (pstr[1] && pstr[2]) { - *pbuf++ = from_hex(pstr[1]) << 4 | from_hex(pstr[2]); - pstr += 2; - } - } else if (*pstr == '+') { - *pbuf++ = ' '; - } else { - *pbuf++ = *pstr; - } - pstr++; - } - *pbuf = '\0'; - return buf; -} -- cgit v1.2.3