From 8e08071f6a23e0975746922420fa94cd4a4c3092 Mon Sep 17 00:00:00 2001 From: Christian Hesse Date: Tue, 3 Jun 2014 09:10:19 +0200 Subject: move code to a block, simplify fcgi compile time dependency --- cqrlogo.c | 167 ++++++++++++++++++++++++++++++-------------------------------- 1 file changed, 82 insertions(+), 85 deletions(-) diff --git a/cqrlogo.c b/cqrlogo.c index 1bafd78..9ba947d 100644 --- a/cqrlogo.c +++ b/cqrlogo.c @@ -24,105 +24,102 @@ int main(int argc, char **argv) { #if HAVE_FCGI /* loop for requests */ - while (FCGI_Accept() >= 0) { + while (FCGI_Accept() >= 0) #endif - /* do the variable initialization within the loop! */ - uri_server_name = NULL; - pattern = NULL; - stolen = NULL; - - https = 0; - - /* these default values are defined in config.h */ - conf.scale = QRCODE_SCALE; - conf.border = QRCODE_BORDER; - conf.level = QRCODE_LEVEL; - conf.overwrite = ALLOW_OVERWRITE; - - /* check if we have environment variables from CGI */ - if ((server_name = getenv("SERVER_NAME")) == NULL) { - fprintf(stderr, "This is a CGI executable. Running without a web service is not supported.\n" - "Note that SERVER_NAME needs to be defined, for full features the client has\n" - "to send referer information.\n"); - return EXIT_FAILURE; - } - - /* check if we have https connection */ - if (getenv("HTTPS") != NULL) - https = 1; - - /* assemble uri for use when referer is missing or fails */ - uri_server_name = malloc(10 + strlen(server_name)); - sprintf(uri_server_name, "http%s://%s/", https ? "s" : "", server_name); - - /* get http referer */ - if ((http_referer = getenv("HTTP_REFERER")) != NULL) { - uri = http_referer; - - /* prepare pattern matching */ - pattern = malloc(sizeof(URLPATTERN) + strlen(server_name)); - sprintf(pattern, URLPATTERN, server_name); - if (regcomp(&preg, pattern, 0) != 0) { - fprintf(stderr, "regcomp() failed, returning nonzero.\n"); + { + /* do the variable initialization within the loop! */ + uri_server_name = NULL; + pattern = NULL; + stolen = NULL; + + https = 0; + + /* these default values are defined in config.h */ + conf.scale = QRCODE_SCALE; + conf.border = QRCODE_BORDER; + conf.level = QRCODE_LEVEL; + conf.overwrite = ALLOW_OVERWRITE; + + /* check if we have environment variables from CGI */ + if ((server_name = getenv("SERVER_NAME")) == NULL) { + fprintf(stderr, "This is a CGI executable. Running without a web service is not supported.\n" + "Note that SERVER_NAME needs to be defined, for full features the client has\n" + "to send referer information.\n"); return EXIT_FAILURE; } - /* check if the QR-Code is for the correct server */ - if (regexec(&preg, http_referer, 1, pmatch, 0) != 0) { - stolen = malloc(sizeof(TEXTSTOLEN) + strlen(server_name)); - sprintf(stolen, TEXTSTOLEN, https ? "s" : "", server_name); - uri = stolen; + /* check if we have https connection */ + if (getenv("HTTPS") != NULL) + https = 1; + + /* assemble uri for use when referer is missing or fails */ + uri_server_name = malloc(10 + strlen(server_name)); + sprintf(uri_server_name, "http%s://%s/", https ? "s" : "", server_name); + + /* get http referer */ + if ((http_referer = getenv("HTTP_REFERER")) != NULL) { + uri = http_referer; + + /* prepare pattern matching */ + pattern = malloc(sizeof(URLPATTERN) + strlen(server_name)); + sprintf(pattern, URLPATTERN, server_name); + if (regcomp(&preg, pattern, 0) != 0) { + fprintf(stderr, "regcomp() failed, returning nonzero.\n"); + return EXIT_FAILURE; + } + + /* check if the QR-Code is for the correct server */ + if (regexec(&preg, http_referer, 1, pmatch, 0) != 0) { + stolen = malloc(sizeof(TEXTSTOLEN) + strlen(server_name)); + sprintf(stolen, TEXTSTOLEN, https ? "s" : "", server_name); + uri = stolen; + } + + regfree(&preg); + free(pattern); + } else { + /* use uri assembled from server name */ + uri = uri_server_name; } - regfree(&preg); - free(pattern); - } else { - /* use uri assembled from server name */ - uri = uri_server_name; - } - - cqr_conf_file(server_name, &conf); - cqr_conf_string(getenv("QUERY_STRING"), &conf); + cqr_conf_file(server_name, &conf); + cqr_conf_string(getenv("QUERY_STRING"), &conf); - /* encode the QR-Code */ - if ((bitmap = cqr_encode_qrcode_to_bitmap(uri, conf)) == NULL) { - /* uri too long? retry with uri from server name */ - uri = uri_server_name; + /* encode the QR-Code */ if ((bitmap = cqr_encode_qrcode_to_bitmap(uri, conf)) == NULL) { - fprintf(stderr, "Could not generate QR-Code.\n"); - return EXIT_FAILURE; + /* uri too long? retry with uri from server name */ + uri = uri_server_name; + if ((bitmap = cqr_encode_qrcode_to_bitmap(uri, conf)) == NULL) { + fprintf(stderr, "Could not generate QR-Code.\n"); + return EXIT_FAILURE; + } } - } - /* generate PNG data */ - if ((png = cqr_bitmap_to_png(bitmap, uri, CQR_META_ALL)) == NULL) { - fprintf(stderr, "Failed to generate PNG.\n"); - return EXIT_FAILURE; - } - - /* print HTTP header */ - fputs(CQR_MIMEHEADER, stdout); + /* generate PNG data */ + if ((png = cqr_bitmap_to_png(bitmap, uri, CQR_META_ALL)) == NULL) { + fprintf(stderr, "Failed to generate PNG.\n"); + return EXIT_FAILURE; + } - /* write PNG data to stdout */ - if (fwrite(png->buffer, png->size, 1, stdout) != 1) { - fprintf(stderr, "Failed writing PNG data to stdout.\n"); - return EXIT_FAILURE; - } + /* print HTTP header */ + fputs(CQR_MIMEHEADER, stdout); - /* free memory we no longer need */ - if (uri_server_name) - free(uri_server_name); - if (stolen) - free(stolen); - cqr_bitmap_free(bitmap); - if (png->size > 0) - free(png->buffer); - free(png); + /* write PNG data to stdout */ + if (fwrite(png->buffer, png->size, 1, stdout) != 1) { + fprintf(stderr, "Failed writing PNG data to stdout.\n"); + return EXIT_FAILURE; + } -#if HAVE_FCGI - /* end of loop */ + /* free memory we no longer need */ + if (uri_server_name) + free(uri_server_name); + if (stolen) + free(stolen); + cqr_bitmap_free(bitmap); + if (png->size > 0) + free(png->buffer); + free(png); } -#endif return EXIT_SUCCESS; } -- cgit v1.2.3-70-g09d2