Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

split the cgi / commandline behaviour into separate _main() functions…

… for

clarity


git-svn-id: http://svn.kfish.org/fastphoto/trunk@149 9c49b5d1-7df3-0310-bce2-b7278e68f44c
  • Loading branch information...
commit 13ae7d9b02e2b42cda1850bb880dfdbb12d0f369 1 parent b5d877a
conrad authored
Showing with 154 additions and 168 deletions.
  1. +60 −39 src/cgi.c
  2. +3 −3 src/cgi.h
  3. +0 −2  src/fastphoto.h
  4. +91 −124 src/main.c
View
99 src/cgi.c
@@ -9,6 +9,7 @@
#include "header.h"
#include "httpdate.h"
#include "photo.h"
+#include "resize.h"
static void
set_param (fastphoto_t * params, char * key, char * val)
@@ -71,54 +72,17 @@ parse_query (fastphoto_t * params, char * query)
}
int
-cgi_init (fastphoto_t * params)
+cgi_test (void)
{
char * gateway_interface;
- char * path_info;
- char * path_translated;
- char * query_string;
- char * if_modified_since;
- time_t since_time;
gateway_interface = getenv ("GATEWAY_INTERFACE");
if (gateway_interface == NULL) {
return 0;
}
- httpdate_init ();
-
- path_info = getenv ("PATH_INFO");
- path_translated = getenv ("PATH_TRANSLATED");
- query_string = getenv ("QUERY_STRING");
- if_modified_since = getenv ("HTTP_IF_MODIFIED_SINCE");
-
- photo_init (&params->in, path_translated);
-
- if (if_modified_since != NULL) {
- int len;
-
- fprintf (stderr, "If-Modified-Since: %s\n", if_modified_since);
-
- len = strlen (if_modified_since) + 1;
- since_time = httpdate_parse (if_modified_since, len);
-
- if (params->in.mtime <= since_time) {
- return HTTP_NOT_MODIFIED;
- }
- }
-
- parse_query (params, query_string);
-
- if (params->x || params->y || params->scale || params->gray ||
- params->quality) {
- cache_init (params, path_info);
- } else {
- params->nochange = 1;
- }
-
return 1;
}
-
size_t
send_memory (fastphoto_t * params)
{
@@ -127,7 +91,7 @@ send_memory (fastphoto_t * params)
return n;
}
-int
+static int
cgi_send_photo (photo_t * photo)
{
header_content_length (photo->size);
@@ -156,3 +120,60 @@ cgi_send (fastphoto_t * params)
return 0;
}
+
+int
+cgi_main (fastphoto_t * params)
+{
+ int err = 0;
+ char * path_info;
+ char * path_translated;
+ char * query_string;
+ char * if_modified_since;
+ time_t since_time;
+
+ httpdate_init ();
+
+ path_info = getenv ("PATH_INFO");
+ path_translated = getenv ("PATH_TRANSLATED");
+ query_string = getenv ("QUERY_STRING");
+ if_modified_since = getenv ("HTTP_IF_MODIFIED_SINCE");
+
+ photo_init (&params->in, path_translated);
+
+ if (if_modified_since != NULL) {
+ int len;
+
+ fprintf (stderr, "If-Modified-Since: %s\n", if_modified_since);
+
+ len = strlen (if_modified_since) + 1;
+ since_time = httpdate_parse (if_modified_since, len);
+
+ if (params->in.mtime <= since_time) {
+ header_not_modified();
+ header_end();
+ return 1;
+ }
+ }
+
+ header_content_type_jpeg ();
+
+ parse_query (params, query_string);
+
+ if (params->x || params->y || params->scale || params->gray ||
+ params->quality) {
+ cache_init (params, path_info);
+ } else {
+ params->nochange = 1;
+ }
+
+ err = 0;
+ if (!(params->nochange || params->cached)) {
+ err = resize (params);
+ }
+
+ if (!err) {
+ cgi_send (params);
+ }
+
+ return err;
+}
View
6 src/cgi.h
@@ -3,9 +3,9 @@
#include "fastphoto.h"
-int cgi_init (fastphoto_t * params);
-int cgi_send_photo (photo_t * photo);
-int cgi_send (fastphoto_t * params);
+int cgi_test (void);
+
+int cgi_main (fastphoto_t * params);
size_t send_memory (fastphoto_t * params);
View
2  src/fastphoto.h
@@ -8,8 +8,6 @@
#define FASTPHOTO_DEFAULT_CACHEDIR "/var/cache/fastphoto"
-#define HTTP_NOT_MODIFIED 304
-
typedef struct fastphoto_s fastphoto_t;
typedef struct photo_s photo_t;
View
215 src/main.c
@@ -39,153 +39,120 @@ usage (void)
}
static int
-cmd_init (fastphoto_t * params, int argc, char * argv[])
+cmd_main (fastphoto_t * params, int argc, char * argv[])
{
- int show_help = 0;
- int show_version = 0;
- int i;
+ int err = 0;
+ int show_help = 0;
+ int show_version = 0;
+ int i;
- params->data = NULL;
- params->data_size = 0;
-
- params->cached = 0;
- params->info = 0;
- params->x = 0;
- params->y = 0;
- params->scale = 0;
- params->quality = 0; /* default */
- params->gray = 0;
-
- while (1) {
- char * optstring = "hvx:y:s:gq:i";
+ while (1) {
+ char * optstring = "hvx:y:s:gq:i";
#ifdef HAVE_GETOPT_LONG
- static struct option long_options[] = {
- {"help", no_argument, 0, 'h'},
- {"version", no_argument, 0, 'v'},
- {"width", required_argument, 0, 'x'},
- {"height", required_argument, 0, 'y'},
- {"scale", required_argument, 0, 's'},
- {"gray", no_argument, 0, 'g'},
- {"quality", required_argument, 0, 'q'},
- {"info", no_argument, 0, 'i'},
- {0,0,0,0}
- };
-
- i = getopt_long (argc, argv, optstring, long_options, NULL);
+ static struct option long_options[] = {
+ {"help", no_argument, 0, 'h'},
+ {"version", no_argument, 0, 'v'},
+ {"width", required_argument, 0, 'x'},
+ {"height", required_argument, 0, 'y'},
+ {"scale", required_argument, 0, 's'},
+ {"gray", no_argument, 0, 'g'},
+ {"quality", required_argument, 0, 'q'},
+ {"info", no_argument, 0, 'i'},
+ {0,0,0,0}
+ };
+
+ i = getopt_long (argc, argv, optstring, long_options, NULL);
#else
- i = getopt (argc, argv, optstring);
+ i = getopt (argc, argv, optstring);
#endif
- if (i == -1) break;
- if (i == ':') {
- return -1;
- }
-
- switch (i) {
- case 'h': /* help */
- show_help = 1;
- break;
- case 'v': /* version */
- show_version = 1;
- break;
- case 'x': /* width */
- params->x = atoi (optarg);
- break;
- case 'y': /* height */
- params->y = atoi (optarg);
- break;
- case 's': /* scale */
- params->scale = atoi (optarg);
- break;
- case 'g': /* gray */
- params->gray = 1;
- break;
- case 'q': /* quality */
- params->quality = atoi (optarg);
- break;
- case 'i': /* info */
- params->info = 1;
- break;
- default:
- break;
- }
-
- }
-
- if (show_version) {
- version ();
+ if (i == -1) break;
+ if (i == ':') {
+ usage ();
+ return 1;
}
- if (show_help) {
- usage ();
+ switch (i) {
+ case 'h': /* help */
+ show_help = 1;
+ break;
+ case 'v': /* version */
+ show_version = 1;
+ break;
+ case 'x': /* width */
+ params->x = atoi (optarg);
+ break;
+ case 'y': /* height */
+ params->y = atoi (optarg);
+ break;
+ case 's': /* scale */
+ params->scale = atoi (optarg);
+ break;
+ case 'g': /* gray */
+ params->gray = 1;
+ break;
+ case 'q': /* quality */
+ params->quality = atoi (optarg);
+ break;
+ case 'i': /* info */
+ params->info = 1;
+ break;
+ default:
+ break;
}
- if (show_version || show_help) {
- return 0;
- }
+ }
- if (optind >= argc) {
- return -1;
- }
+ if (show_version) {
+ version ();
+ }
- params->in.name = argv[optind++];
+ if (show_help) {
+ usage ();
+ }
- if (optind >= argc) {
- memory_init (params);
- } else {
- params->out.name = argv[optind++];
- }
+ if (show_version || show_help) {
+ return 0;
+ }
+ if (optind >= argc) {
+ usage ();
return 1;
+ }
+
+ params->in.name = argv[optind++];
+
+ if (optind >= argc) {
+ memory_init (params);
+ } else {
+ params->out.name = argv[optind++];
+ }
+
+ err = resize (params);
+
+ if (!err)
+ send_memory (params);
+
+ return err;
}
int
main (int argc, char * argv[])
{
- int cgi = 0, err = 0;
- fastphoto_t params;
+ int err = 0;
+ fastphoto_t params;
- memset (&params, 0, sizeof (fastphoto_t));
+ memset (&params, 0, sizeof (fastphoto_t));
- cgi = cgi_init(&params);
+ if (cgi_test ()) {
+ err = cgi_main (&params);
+ } else {
+ err = cmd_main (&params, argc, argv);
+ }
- if (cgi == 1) {
- header_content_type_jpeg ();
- } else if (cgi == HTTP_NOT_MODIFIED) {
- header_not_modified();
- header_end();
- goto cleanup;
- } else {
- err = cmd_init(&params, argc, argv);
-
- if (err == 0) {
- /* Just handled --help or --version */
- exit (0);
- } else if (err == -1) {
- /* Usage error */
- usage ();
- exit (1);
- }
- }
-
- err = 0;
- if (!(params.nochange || params.cached)) {
- err = resize (&params);
- }
-
- if (!err) {
- if (cgi) {
- cgi_send (&params);
- } else if (!params.out.name) {
- send_memory (&params);
- }
- }
-
- cleanup:
-
- if (params.data) free (params.data);
+ if (params.data) free (params.data);
- if (err) return 1;
- else return 0;
+ if (err) return 1;
+ else return 0;
}
Please sign in to comment.
Something went wrong with that request. Please try again.