/
cgi.c
105 lines (81 loc) · 1.81 KB
/
cgi.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "fastphoto.h"
#define CONTENT_TYPE_JPEG "Content-Type: image/jpeg\n"
#define BUFSIZE 4096
static void
set_param (fastphoto_t * params, char * key, char * val)
{
if (!strcmp ("x", key)) params->x = atoi(val);
if (!strcmp ("y", key)) params->y = atoi(val);
}
/**
* Parse the name=value pairs in the query string and set parameters
* @param params The fastphoto parameters to set
* @param query The query string
*/
static void
parse_query (fastphoto_t * params, char * query)
{
char * key, * val, * end;
if (!query) return;
key = query;
do {
val = strchr (key, '=');
end = strchr (key, '&');
if (end) {
if (val) {
if (val < end) {
*val++ = '\0';
} else {
val = NULL;
}
}
*end++ = '\0';
} else {
if (val) *val++ = '\0';
}
/* fprintf (stderr, "%s = %s\n", key, val);*/
set_param (params, key, val);
key = end;
} while (end != NULL);
return;
}
int
cgi_init (fastphoto_t * params)
{
char * gateway_interface;
char * path_translated;
char * query_string;
gateway_interface = getenv ("GATEWAY_INTERFACE");
if (gateway_interface == NULL) {
return 0;
}
path_translated = getenv ("PATH_TRANSLATED");
query_string = getenv ("QUERY_STRING");
params->infile = path_translated;
params->outfile = "/tmp/cache.jpg";
params->x = 0;
params->y = 0;
parse_query (params, query_string);
return 1;
}
int
content_type_jpeg ()
{
return puts (CONTENT_TYPE_JPEG);
}
int
cgi_send (fastphoto_t * params)
{
unsigned char buf[BUFSIZE];
FILE * fd;
size_t n;
fd = fopen (params->outfile, "rb");
while ((n = fread (buf, 1, BUFSIZE, fd)) > 0) {
fwrite (buf, 1, n, stdout);
}
fclose (fd);
return 1;
}