This repository has been archived by the owner on Sep 18, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
request.c
194 lines (166 loc) · 4.66 KB
/
request.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/* Copyright © 2012 euphoria GmbH
* Author: Lukas Martini <[email protected]>
*
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by the Free
* Software Foundation, either version 3 of the License, or (at your option) any
* later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
#include <syslog.h>
#include <fcgiapp.h>
#include <sys/stat.h>
#include <string.h>
#include <errno.h>
#include <assert.h>
#include "global.h"
#include "request.h"
#include "resize.h"
static int do_mkdir(const char *path, mode_t mode)
{
struct stat st;
int status = 0;
if (stat(path, &st) != 0)
{
/* Directory does not exist */
if (mkdir(path, mode) != 0)
status = -1;
}
else if (!S_ISDIR(st.st_mode))
{
errno = ENOTDIR;
status = -1;
}
return status;
}
int mkpath(const char *path, mode_t mode)
{
char* pp;
char* sp;
int status;
char* copypath = strdup(path);
status = 0;
pp = copypath;
while (status == 0 && (sp = strchr(pp, '/')) != 0)
{
if (sp != pp)
{
// Neither root nor double slash in path
*sp = 0;
status = do_mkdir(copypath, mode);
*sp = '/';
}
pp = sp + 1;
}
if(status == 0)
status = do_mkdir(path, mode);
free(copypath);
return status;
}
void handle_request(FCGX_Request* request, char* root, char* thumbnail_root)
{
char* basename = FCGX_GetParam("BASENAME", request->envp);
char* extension = FCGX_GetParam("EXTENSION", request->envp);
char* size_str = FCGX_GetParam("SIZE", request->envp);
char* mode = FCGX_GetParam("MODE", request->envp);
char* req_file = FCGX_GetParam("FILENAME", request->envp);
if(!size_str || !basename || !extension || !req_file)
{
log_request(500, LOG_CRIT, "Invalid FastCGI parameters from server!");
http_error_c(500);
}
if(!mode)
mode = "default";
// Build absolute file path
char* req_path = calloc(strlen(thumbnail_root) + strlen(req_file) + 1, sizeof(char));
if(!req_path)
{
log_request(500, LOG_EMERG, "Allocation failure for req_path!");
http_error_c(500);
}
sprintf(req_path, "%s%s", thumbnail_root, req_file);
// Check if the requested file exists already
struct stat check_stat;
if(stat(req_path, &check_stat) == 0)
{
free(req_path);
if(check_stat.st_mode & S_IFREG)
{
http_sendfile(req_file);
log_request(200, LOG_INFO, "Already exists in file system");
return;
} else {
// Directory, pipe, symlink or similar.
log_request(403, LOG_WARNING, "Request for existing file of wrong type (directory, symlink, ...)");
http_error_c(403);
}
}
// Get directory path
char* dir_req_path = calloc(strlen(req_path) + 1, sizeof(char));
if(!req_path)
{
log_request(500, LOG_EMERG, "Allocation failure for dir_req_path!");
free(req_path);
http_error_c(500);
}
strcpy(dir_req_path, req_path);
char* p = strrchr(dir_req_path, (int)'/');
if(p != NULL) *++p = 0;
// Create thumbnail directory if neccessary
if(stat(dir_req_path, &check_stat) != 0 && errno == ENOENT)
if(mkpath(dir_req_path, S_IREAD | S_IWRITE | S_IEXEC | S_IRGRP | S_IWGRP | S_IXGRP | S_IROTH | S_IXOTH) != 0)
{
log_request(500, LOG_ERR, "Couldn't mkpath() %s\n", dir_req_path);
free(req_path);
free(dir_req_path);
http_error_c(500);
}
free(dir_req_path);
// Convert size to an integer
size_t size = atoi(size_str);
if(size <= 0)
{
free(req_path);
http_error_c(404);
}
// Get source path
char* path = calloc(strlen(root) + strlen(basename) + strlen(extension) + 2, sizeof(char));
if(!req_path)
{
log_request(500, LOG_EMERG, "Allocation failure for path!");
free(req_path);
http_error_c(500);
}
sprintf(path, "%s%s.%s", root, basename, extension);
// Read the image, resize it and write it
int resize_status = resize_image(path, req_path, size, mode);
if(resize_status == 1)
{
FCGX_FPrintF(request->out, "Location: ../%s.%s\r\n\r\n", basename, extension);
free(path);
free(req_path);
log_request(203, LOG_INFO, "Requested image size bigger than original - redirecting");
return;
} else if(resize_status < 0)
{
free(path);
free(req_path);
log_request(404, LOG_WARNING, "File not found");
http_error_c(404);
}
http_sendfile(req_file);
log_request(200, LOG_INFO, "Generated from %s.%s", basename, extension);
free(path);
free(req_path);
}