remove debug scaffolding, enable pledge and unveil - frontends - front-ends for some sites (experiment)
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) README
 (DIR) LICENSE
       ---
 (DIR) commit e5718bba1e03ba5fe62eb60b525fe1d501b07249
 (DIR) parent 0c9d43bad837b65578b89a797fd122115223e1d1
 (HTM) Author: Hiltjo Posthuma <hiltjo@codemadness.org>
       Date:   Sun, 12 Jan 2020 12:06:32 +0100
       
       remove debug scaffolding, enable pledge and unveil
       
       Diffstat:
         M reddit/cli.c                        |       4 ----
         M reddit/gopher.c                     |       6 ------
         M reddit/reddit.c                     |      15 ---------------
         M twitch/cgi.c                        |       2 --
         M twitch/gopher.c                     |      31 ++++++++++++-------------------
         M twitch/twitch.c                     |      46 ++-----------------------------
       
       6 files changed, 15 insertions(+), 89 deletions(-)
       ---
 (DIR) diff --git a/reddit/cli.c b/reddit/cli.c
       @@ -60,12 +60,10 @@ render(struct list_response *r)
        {
                size_t i;
        
       -#if 0
                if (pledge("stdio", NULL) == -1) {
                        fprintf(stderr, "pledge: %s\n", strerror(errno));
                        exit(1);
                }
       -#endif
        
                for (i = 0; i < r->nitems; i++)
                        printitem(&(r->items[i]));
       @@ -91,7 +89,6 @@ main(int argc, char *argv[])
                struct list_response *r;
                char subreddit[1024] = "";
        
       -#if 1
                if (pledge("stdio dns inet rpath unveil", NULL) == -1) {
                        fprintf(stderr, "pledge: %s\n", strerror(errno));
                        exit(1);
       @@ -104,7 +101,6 @@ main(int argc, char *argv[])
                        fprintf(stderr, "unveil: %s\n", strerror(errno));
                        exit(1);
                }
       -#endif
        
                if (argc > 1) {
                        if (!uriencode(argv[1], subreddit, sizeof(subreddit)))
 (DIR) diff --git a/reddit/gopher.c b/reddit/gopher.c
       @@ -108,8 +108,6 @@ printitem(struct item *item)
                    strcmp(item->thumbnail, "default")) {
                        putchar('h');
                        OUT("Thumbnail");
       -//                OUT("Thumbnail: ");
       -//                OUTTEXT(item->thumbnail);
                        OUT("\tURL:");
                        OUTTEXT(item->thumbnail);
                        printf("\t%s\t%s\r\n", host, port);
       @@ -163,12 +161,10 @@ render(struct list_response *r)
        {
                size_t i;
        
       -#if 0
                if (pledge("stdio", NULL) == -1) {
                        fprintf(stderr, "pledge: %s\n", strerror(errno));
                        exit(1);
                }
       -#endif
        
                if (render_pagination(r))
                        info("");
       @@ -195,7 +191,6 @@ main(int argc, char *argv[])
                struct list_response *r;
                char *querystring, *p, *search;
        
       -#if 1
                if (pledge("stdio dns inet rpath unveil", NULL) == -1) {
                        fprintf(stderr, "pledge: %s\n", strerror(errno));
                        exit(1);
       @@ -208,7 +203,6 @@ main(int argc, char *argv[])
                        fprintf(stderr, "unveil: %s\n", strerror(errno));
                        exit(1);
                }
       -#endif
        
                if ((p = getenv("SERVER_NAME")))
                        host = p;
 (DIR) diff --git a/reddit/reddit.c b/reddit/reddit.c
       @@ -17,8 +17,6 @@
        #include "reddit.h"
        #include "util.h"
        
       -//#define DEBUG_MODE 1
       -
        static char *
        reddit_request(const char *path)
        {
       @@ -26,7 +24,6 @@ reddit_request(const char *path)
        }
        
        /* unmarshal JSON response, skip HTTP headers */
       -/* TODO: add to util.c or json.c ? */
        int
        json_unmarshal(const char *data,
                void (*cb)(struct json_node *, size_t, const char *, void *),
       @@ -34,16 +31,12 @@ json_unmarshal(const char *data,
        {
                const char *s;
        
       -#ifdef DEBUG_MODE
       -        s = data; /* DEBUG: has no headers */
       -#else
                /* strip/skip header part */
                if (!(s = strstr(data, "\r\n\r\n"))) {
                        fprintf(stderr, "error parsing HTTP response header\n");
                        return -1; /* invalid response */
                }
                s += strlen("\r\n\r\n");
       -#endif
        
                /* parse */
                if (parsejson(s, strlen(s), cb, pp) < 0) {
       @@ -79,14 +72,6 @@ reddit_list_data(const char *subreddit, int limit,
                        strlcat(path, after, sizeof(path));
                }
        
       -#ifdef DEBUG_MODE
       -        printf("DEBUG: %s: path: %s\n", __func__, path);
       -
       -//        data = readfile("test.json");
       -//        data = readfile("poe.json");
       -        return readfile("nl.json");
       -#endif
       -
                if (r < 0 || (size_t)r >= sizeof(path))
                        return NULL;
        
 (DIR) diff --git a/twitch/cgi.c b/twitch/cgi.c
       @@ -458,14 +458,12 @@ main(void)
        {
                char *path, *pathinfo;
        
       -#if 1
                if (pledge("stdio dns inet rpath unveil", NULL) == -1 ||
                    unveil(TLS_CA_CERT_FILE, "r") == -1 ||
                    unveil(NULL, NULL) == -1) {
                        OUT("Status: 500 Internal Server Error\r\n\r\n");
                        exit(1);
                }
       -#endif
        
                if (!(pathinfo = getenv("PATH_INFO")))
                        pathinfo = "/";
 (DIR) diff --git a/twitch/gopher.c b/twitch/gopher.c
       @@ -41,6 +41,12 @@ line(int _type, const char *username, const char *selector)
        }
        
        void
       +error(const char *s)
       +{
       +        line('3', s, "");
       +}
       +
       +void
        info(const char *s)
        {
                line('i', s, "");
       @@ -311,10 +317,8 @@ handle_streams(void)
                /* find detailed user data with streams */
                ru = twitch_streams_users(r);
        
       -        if (pledge("stdio", NULL) == -1) {
       -//                OUT("Status: 500 Internal Server Error\r\n\r\n");
       +        if (pledge("stdio", NULL) == -1)
                        exit(1);
       -        }
        
                render_streams(r, game_id);
        
       @@ -358,10 +362,8 @@ handle_videos(void)
        
                /* no parameter given, show form */
                if (!user_id[0] && !login[0]) {
       -                if (pledge("stdio", NULL) == -1) {
       -//                        OUT("Status: 500 Internal Server Error\r\n\r\n");
       +                if (pledge("stdio", NULL) == -1)
                                exit(1);
       -                }
        
                        render_videos(r, "");
                        return;
       @@ -375,10 +377,8 @@ handle_videos(void)
                                r = twitch_videos_byuserid(ru->data[0].id);
                }
        
       -        if (pledge("stdio", NULL) == -1) {
       -//                OUT("Status: 500 Internal Server Error\r\n\r\n");
       +        if (pledge("stdio", NULL) == -1)
                        exit(1);
       -        }
        
                if (r && r->nitems > 0)
                        title = r->data[0].user_name;
       @@ -402,10 +402,8 @@ handle_games_top(void)
                if (!(r = twitch_games_top()))
                        return;
        
       -        if (pledge("stdio", NULL) == -1) {
       -//                OUT("Status: 500 Internal Server Error\r\n\r\n");
       +        if (pledge("stdio", NULL) == -1)
                        exit(1);
       -        }
        
                /* sort by name alphabetically, NOTE: the results are the top 100
                   sorted by viewcount. View counts are not visible in the new
       @@ -420,10 +418,8 @@ handle_games_top(void)
        void
        handle_links(void)
        {
       -        if (pledge("stdio", NULL) == -1) {
       -//                OUT("Status: 500 Internal Server Error\r\n\r\n");
       +        if (pledge("stdio", NULL) == -1)
                        exit(1);
       -        }
        
                pagetitle = "Links";
        
       @@ -437,14 +433,11 @@ main(int argc, char *argv[])
        
                setlocale(LC_CTYPE, "");
        
       -#if 1
                if (pledge("stdio dns inet rpath unveil", NULL) == -1 ||
                    unveil(TLS_CA_CERT_FILE, "r") == -1 ||
                    unveil(NULL, NULL) == -1) {
       -//                OUT("Status: 500 Internal Server Error\r\n\r\n");
                        exit(1);
                }
       -#endif
        
                if ((p = getenv("SERVER_NAME")))
                        host = p;
       @@ -473,7 +466,7 @@ main(int argc, char *argv[])
                } else if (!strcmp(path, "links")) {
                        handle_links();
                } else {
       -//                OUT("Status: 404 Not Found\r\n\r\n");
       +                error("Not Found");
                        exit(1);
                }
        
 (DIR) diff --git a/twitch/twitch.c b/twitch/twitch.c
       @@ -17,27 +17,19 @@
        #include "twitch.h"
        #include "util.h"
        
       -//#define DEBUG_MODE 1
       -
        #ifndef TWITCH_API_KEY
        #error "make sure set a TWITCH_API_KEY in twitch.c"
       +#define TWITCH_API_KEY "API key here"
        #endif
       -static char *twitch_headers = "Client-ID: " TWITCH_API_KEY "\r\n";
       +static const char *twitch_headers = "Client-ID: " TWITCH_API_KEY "\r\n";
        
        static char *
        twitch_request(const char *path)
        {
       -        char *data;
       -
       -        data = request("api.twitch.tv", path, twitch_headers);
       -//        if (data) {
       -//                printf("DEBUG: path=%s\ndata: %s\n", path, data);
       -//        }
       -        return data;
       +        return request("api.twitch.tv", path, twitch_headers);
        }
        
        /* unmarshal JSON response, skip HTTP headers */
       -/* TODO: add to util.c or json.c ? */
        int
        json_unmarshal(const char *data,
                void (*cb)(struct json_node *, size_t, const char *, void *),
       @@ -45,16 +37,12 @@ json_unmarshal(const char *data,
        {
                const char *s;
        
       -#ifdef DEBUG_MODE
       -        s = data; /* DEBUG: has no headers */
       -#else
                /* strip/skip header part */
                if (!(s = strstr(data, "\r\n\r\n"))) {
                        fprintf(stderr, "error parsing HTTP response header\n");
                        return -1; /* invalid response */
                }
                s += strlen("\r\n\r\n");
       -#endif
        
                /* parse */
                if (parsejson(s, strlen(s), cb, pp) < 0) {
       @@ -75,11 +63,7 @@ twitch_games_bygameids_data(const char *param)
                if (r < 0 || (size_t)r >= sizeof(path))
                        return NULL;
        
       -#ifdef DEBUG_MODE
       -        return readfile("examples/games.json"); /* DEBUG */
       -#else
                return twitch_request(path);
       -#endif
        }
        
        char *
       @@ -92,11 +76,7 @@ twitch_users_byuserids_data(const char *param)
                if (r < 0 || (size_t)r >= sizeof(path))
                        return NULL;
        
       -#ifdef DEBUG_MODE
       -        return readfile("examples/users_byid.json"); /* DEBUG */
       -#else
                return twitch_request(path);
       -#endif
        }
        
        char *
       @@ -109,11 +89,7 @@ twitch_users_bylogin_data(const char *login)
                if (r < 0 || (size_t)r >= sizeof(path))
                        return NULL;
        
       -#ifdef DEBUG_MODE
       -        return readfile("examples/users.json"); /* DEBUG */
       -#else
                return twitch_request(path);
       -#endif
        }
        
        char *
       @@ -127,21 +103,13 @@ twitch_videos_byuserid_data(const char *user_id)
                if (r < 0 || (size_t)r >= sizeof(path))
                        return NULL;
        
       -#ifdef DEBUG_MODE
       -        return readfile("examples/videos.json"); /* DEBUG */
       -#else
                return twitch_request(path);
       -#endif
        }
        
        char *
        twitch_streams_data(void)
        {
       -#ifdef DEBUG_MODE
       -        return readfile("examples/streams.json"); /* DEBUG */
       -#else
                return twitch_request("/helix/streams?first=100");
       -#endif
        }
        
        char *
       @@ -155,21 +123,13 @@ twitch_streams_game_data(const char *game_id)
                if (r < 0 || (size_t)r >= sizeof(path))
                        return NULL;
        
       -#ifdef DEBUG_MODE
       -        return readfile("examples/streams_game_30921.json"); /* DEBUG */
       -#else
                return twitch_request(path);
       -#endif
        }
        
        char *
        twitch_games_top_data(void)
        {
       -#ifdef DEBUG_MODE
       -        return readfile("examples/topgames.json"); /* DEBUG */
       -#else
                return twitch_request("/helix/games/top?first=100");
       -#endif
        }
        
        void