diff options
| author | Rafael G. Martins <rafael@rafaelmartins.eng.br> | 2017-03-04 01:14:00 +0100 | 
|---|---|---|
| committer | Rafael G. Martins <rafael@rafaelmartins.eng.br> | 2017-03-04 01:29:43 +0100 | 
| commit | 8c320bfe295ac1cbd1833d7fba5c54a325875add (patch) | |
| tree | 88589ebb027a1cb7ba1e700d5ac3aa01304fd5ec /src/blogc-make | |
| parent | 4c00812c31dfa42183c8b6586ccf102a6d65f61c (diff) | |
| download | blogc-feature/runserver-lighttpd.tar.gz blogc-feature/runserver-lighttpd.tar.bz2 blogc-feature/runserver-lighttpd.zip | |
make: implemented lighttpd support to runserver rulefeature/runserver-lighttpd
Diffstat (limited to 'src/blogc-make')
| -rw-r--r-- | src/blogc-make/ctx.c | 2 | ||||
| -rw-r--r-- | src/blogc-make/ctx.h | 1 | ||||
| -rw-r--r-- | src/blogc-make/exec.c | 43 | ||||
| -rw-r--r-- | src/blogc-make/lighttpd.c | 135 | ||||
| -rw-r--r-- | src/blogc-make/lighttpd.h | 16 | 
5 files changed, 196 insertions, 1 deletions
| diff --git a/src/blogc-make/ctx.c b/src/blogc-make/ctx.c index 62b40b8..0aaf472 100644 --- a/src/blogc-make/ctx.c +++ b/src/blogc-make/ctx.c @@ -143,6 +143,7 @@ bm_ctx_new(bm_ctx_t *base, const char *settings_file, const char *argv0,          rv->blogc = bm_exec_find_binary(argv0, "blogc", "BLOGC");          rv->blogc_runserver = bm_exec_find_binary(argv0, "blogc-runserver",              "BLOGC_RUNSERVER"); +        rv->lighttpd = bm_exec_find_binary(argv0, "lighttpd", "LIGHTTPD");          rv->verbose = false;      }      else { @@ -290,5 +291,6 @@ bm_ctx_free(bm_ctx_t *ctx)      bm_ctx_free_internal(ctx);      free(ctx->blogc);      free(ctx->blogc_runserver); +    free(ctx->lighttpd);      free(ctx);  } diff --git a/src/blogc-make/ctx.h b/src/blogc-make/ctx.h index 77e8088..290a6b7 100644 --- a/src/blogc-make/ctx.h +++ b/src/blogc-make/ctx.h @@ -31,6 +31,7 @@ typedef struct {  typedef struct {      char *blogc;      char *blogc_runserver; +    char *lighttpd;      bool verbose;      bm_settings_t *settings; diff --git a/src/blogc-make/exec.c b/src/blogc-make/exec.c index 5f508e6..28bab58 100644 --- a/src/blogc-make/exec.c +++ b/src/blogc-make/exec.c @@ -19,6 +19,7 @@  #include "../common/utils.h"  #include "ctx.h"  #include "exec.h" +#include "lighttpd.h"  #include "settings.h" @@ -343,6 +344,38 @@ bm_exec_blogc_runserver(bm_ctx_t *ctx, const char *host, const char *port,      if (ctx == NULL)          return 3; +    int rv = 0; + +    // check if lighttpd exists + +    // we need to mangle path, because lighttpd is usually installed to +    // /usr/sbin +    char *old_path = getenv("PATH"); +    char *path = NULL; +    if (old_path != NULL) { +        path = bc_strdup_printf("/usr/sbin:%s", path); +        setenv("PATH", path, 1); +    } + +    char *cmd_lighttpd = bc_strdup_printf("%s -v 2> /dev/null > /dev/null", +        ctx->lighttpd); +    int status_lighttpd = system(cmd_lighttpd); +    free(cmd_lighttpd); + +    // if it exists, use it! :D +    if (127 != WEXITSTATUS(status_lighttpd)) { +        char *conf = bm_lighttpd_deploy(ctx->output_dir, host, port); +        if (conf == NULL) +            return 3; + +        cmd_lighttpd = bc_strdup_printf("%s -D -f %s", ctx->lighttpd, conf); +        int status = system(cmd_lighttpd); +        free(cmd_lighttpd); +        bm_lighttpd_destroy(conf); +        rv = WEXITSTATUS(status); +        goto eval_rv; +    } +      bc_string_t *cmd = bc_string_new();      bc_string_append(cmd, ctx->blogc_runserver); @@ -377,9 +410,17 @@ bm_exec_blogc_runserver(bm_ctx_t *ctx, const char *host, const char *port,      // we don't need pipes to run blogc-runserver, because it is "interactive"      int status = system(cmd->str); -    int rv = WEXITSTATUS(status); +    rv = WEXITSTATUS(status);      bc_string_free(cmd, true); +eval_rv: + +    // cleanup path changes +    if (old_path != NULL) { +        setenv("PATH", old_path, 1); +        free(path); +    } +      if (rv != 0) {          if (rv == 127) {              fprintf(stderr, diff --git a/src/blogc-make/lighttpd.c b/src/blogc-make/lighttpd.c new file mode 100644 index 0000000..e1ba079 --- /dev/null +++ b/src/blogc-make/lighttpd.c @@ -0,0 +1,135 @@ +/* + * blogc: A blog compiler. + * Copyright (C) 2014-2017 Rafael G. Martins <rafael@rafaelmartins.eng.br> + * + * This program can be distributed under the terms of the BSD License. + * See the file LICENSE. + */ + +#include <stdlib.h> +#include <stdio.h> +#include <string.h> +#include <errno.h> +#include <unistd.h> +#include "../common/utils.h" +#include "lighttpd.h" + +static const char conf[] = +    "server.document-root = \"%s\"\n" +    "server.bind = \"%s\"\n" +    "server.port = %hu\n" +    "server.errorlog = \"/dev/stderr\"\n" +    "\n" +    "mimetype.assign = (\n" +    "    \".html\" => \"text/html\",\n" +    "    \".htm\" => \"text/html\",\n" +    "    \".shtml\" => \"text/html\",\n" +    "    \".xml\" => \"text/xml\",\n" +    "    \".txt\" => \"text/plain\",\n" +    "    \".xhtml\" => \"application/xhtml+xml\",\n" +    "    \".css\" => \"text/css\",\n" +    "    \".gif\" => \"image/gif\",\n" +    "    \".jpeg\" => \"image/jpeg\",\n" +    "    \".jpg\" => \"image/jpeg\",\n" +    "    \".js\" => \"application/javascript\",\n" +    "    \".atom\" => \"application/atom+xml\",\n" +    "    \".rss\" => \"application/rss+xml\",\n" +    "    \".mml\" => \"text/mathml\",\n" +    "    \".jad\" => \"text/vnd.sun.j2me.app-descriptor\",\n" +    "    \".wml\" => \"text/vnd.wap.wml\",\n" +    "    \".htc\" => \"text/x-component\",\n" +    "    \".png\" => \"image/png\",\n" +    "    \".tif\" => \"image/tiff\",\n" +    "    \".tiff\" => \"image/tiff\",\n" +    "    \".wbmp\" => \"image/vnd.wap.wbmp\",\n" +    "    \".ico\" => \"image/x-icon\",\n" +    "    \".jng\" => \"image/x-jng\",\n" +    "    \".bmp\" => \"image/x-ms-bmp\",\n" +    "    \".svg\" => \"image/svg+xml\",\n" +    "    \".svgz\" => \"image/svg+xml\",\n" +    "    \".webp\" => \"image/webp\",\n" +    "    \".woff\" => \"application/font-woff\",\n" +    "    \".jar\" => \"application/java-archive\",\n" +    "    \".war\" => \"application/java-archive\",\n" +    "    \".ear\" => \"application/java-archive\",\n" +    "    \".json\" => \"application/json\",\n" +    "    \".hqx\" => \"application/mac-binhex40\",\n" +    "    \".doc\" => \"application/msword\",\n" +    "    \".pdf\" => \"application/pdf\",\n" +    "    \".ps\" => \"application/postscript\",\n" +    "    \".eps\" => \"application/postscript\",\n" +    "    \".ai\" => \"application/postscript\",\n" +    "    \".rtf\" => \"application/rtf\",\n" +    "    \".m3u8\" => \"application/vnd.apple.mpegurl\",\n" +    "    \".xls\" => \"application/vnd.ms-excel\",\n" +    "    \".eot\" => \"application/vnd.ms-fontobject\",\n" +    "    \".ppt\" => \"application/vnd.ms-powerpoint\",\n" +    "    \".wmlc\" => \"application/vnd.wap.wmlc\",\n" +    "    \".kml\" => \"application/vnd.google-earth.kml+xml\",\n" +    "    \".kmz\" => \"application/vnd.google-earth.kmz\",\n" +    "    \".7z\" => \"application/x-7z-compressed\",\n" +    "    \".cco\" => \"application/x-cocoa\",\n" +    ")\n" +    "\n" +    "index-file.names = (\n" +    "    \"index.html\",\n" +    "    \"index.htm\",\n" +    "    \"index.shtml\",\n" +    "    \"index.xml\",\n" +    "    \"index.txt\",\n" +    "    \"index.xhtml\",\n" +    ")\n"; + + +char* +bm_lighttpd_deploy(const char *output_dir, const char *host, const char *port) +{ +    if (output_dir == NULL) +        return NULL; + +    unsigned short port_int = 8080; +    if (port  != NULL) { +        char *endptr; +        port_int = strtoul(port, &endptr, 10); +        if (*port != '\0' && *endptr != '\0') { +            fprintf(stderr, "blogc-make: error: Failed to parse lighttpd port " +                "value: %s\n", port); +            return NULL; +        } +    } + +    // this is not really portable +    char fname[] = "/tmp/blogc-make_XXXXXX"; +    int fd; +    if (-1 == (fd = mkstemp(fname))) { +        fprintf(stderr, "blogc-make: error: Failed to create temporary " +            "lighttpd config: %s", strerror(errno)); +        return NULL; +    } + +    const char *host_str = host == NULL ? "127.0.0.1": host; +    char *content = bc_strdup_printf(conf, output_dir, host_str, port_int); + +    if (-1 == write(fd, content, strlen(content))) { +        fprintf(stderr, "blogc-make: error: Failed to write to temporary " +            "lighttpd config: %s", strerror(errno)); +        free(content); +        close(fd); +        unlink(fname); +        return NULL; +    } + +    free(content); +    close(fd); + +    fprintf(stderr, " * Running on http://%s:%hu/\n\n", host_str, port_int); + +    return bc_strdup(fname); +} + + +void +bm_lighttpd_destroy(const char *fname) +{ +    unlink(fname); +} diff --git a/src/blogc-make/lighttpd.h b/src/blogc-make/lighttpd.h new file mode 100644 index 0000000..6b7279d --- /dev/null +++ b/src/blogc-make/lighttpd.h @@ -0,0 +1,16 @@ +/* + * blogc: A blog compiler. + * Copyright (C) 2014-2017 Rafael G. Martins <rafael@rafaelmartins.eng.br> + * + * This program can be distributed under the terms of the BSD License. + * See the file LICENSE. + */ + +#ifndef _MAKE_LIGHTTPD_H +#define _MAKE_LIGHTTPD_H + +char* bm_lighttpd_deploy(const char *output_dir, const char *host, +    const char *port); +void bm_lighttpd_destroy(const char *fname); + +#endif /* _MAKE_LIGHTTPD_H */ | 
