3 Common parts of the online interface
6 Copyright (C) 2013, 2014, 2015, 2022 Balthasar SzczepaĆski
8 This program is free software: you can redistribute it and/or modify
9 it under the terms of the GNU Affero General Public License as
10 published by the Free Software Foundation, either version 3 of the
11 License, or (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU Affero General Public License for more details.
18 You should have received a copy of the GNU Affero General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
21 Requires cgilib (http://www.infodrom.org/projects/cgilib/)
37 #include "online-core.h"
39 #define FILE_CHUNK 1024
42 int cp (char *src, char *dst)
50 r = execl(CP_PATH, CP_PATH, src, dst, (char *)0);
66 r = execl(RM_PATH, RM_PATH, "-f", dst, (char *)0);
74 int wget (char *url, char *dst)
82 r = execl(WGET_PATH, WGET_PATH, "-q", "-t", "2", "-U", USERAGENT, "-O", dst, url, (char *)0);
90 void make_tmp_path(char *str, size_t n, unsigned int i, char *ext)
94 gettimeofday(&tv, NULL);
99 "%s/%lu_%u_%llu_%lu%s",
101 (long unsigned) getpid(),
103 (long long unsigned) (tv.tv_sec),
104 (long unsigned) (tv.tv_usec),
109 int get_file(s_cgi *cgi, char *name, char *dest, char **path)
117 vl = cgiGetFiles(cgi);
120 for (i=0; vl[i]!= NULL; ++i)
122 if (strcmp(vl[i], name)==0)
124 f = cgiGetFile(cgi, vl[i]);
129 r = cp(f->tmpfile, dest);
144 v = cgiGetValue(cgi, name);
154 int send_file (char *path, char *content_type, unsigned status)
160 uint8_t buffer[FILE_CHUNK];
162 f = fopen(path, "rb");
165 fstat(fileno(f), &st);
169 fprintf(stdout, "Status: %u\n", status);
170 if (content_type!=NULL)
171 fprintf(stdout, "Content-type: %s\n", content_type);
172 fprintf(stdout, "Content-Length: %" PRId64 "\n\n", size);
176 rd = fread(buffer, 1, FILE_CHUNK, f);
182 wr = fwrite(buffer+off, 1, rd - off, stdout);
196 int send_data (const uint8_t *address, size_t size, char *content_type, unsigned status)
201 fprintf(stdout, "Status: %u\n", status);
202 if (content_type!=NULL)
203 fprintf(stdout, "Content-type: %s\n", content_type);
204 fprintf(stdout, "Content-Length: %" PRId64 "\n\n", (uint64_t)size);
212 wr = fwrite(address +off, 1, wr, stdout);