2 Commits

Author SHA1 Message Date
de44f4a3fe Cache dynamic mem 2021-05-05 18:17:57 +02:00
cc29250d76 FastCGI cleanup 2021-05-05 18:15:20 +02:00
3 changed files with 16 additions and 12 deletions

View File

@@ -16,6 +16,7 @@
#include <errno.h> #include <errno.h>
#include <signal.h> #include <signal.h>
#include <openssl/sha.h> #include <openssl/sha.h>
#include <malloc.h>
int cache_continue = 1; int cache_continue = 1;
magic_t magic; magic_t magic;
@@ -74,8 +75,8 @@ int cache_process() {
} }
FILE *file; FILE *file;
char buf[16384]; char *buf = malloc(CACHE_BUF_SIZE);
char comp_buf[16384]; char *comp_buf = malloc(CACHE_BUF_SIZE);
char filename_comp_gz[256]; char filename_comp_gz[256];
char filename_comp_br[256]; char filename_comp_br[256];
unsigned long read; unsigned long read;
@@ -144,23 +145,23 @@ int cache_process() {
} }
} }
while ((read = fread(buf, 1, sizeof(buf), file)) > 0) { while ((read = fread(buf, 1, CACHE_BUF_SIZE, file)) > 0) {
SHA1_Update(&ctx, buf, read); SHA1_Update(&ctx, buf, read);
if (compress) { if (compress) {
unsigned long avail_in, avail_out; unsigned long avail_in, avail_out;
avail_in = read; avail_in = read;
do { do {
avail_out = sizeof(comp_buf); avail_out = CACHE_BUF_SIZE;
compress_compress_mode(&comp_ctx, COMPRESS_GZ,buf + read - avail_in, &avail_in, compress_compress_mode(&comp_ctx, COMPRESS_GZ,buf + read - avail_in, &avail_in,
comp_buf, &avail_out, feof(file)); comp_buf, &avail_out, feof(file));
fwrite(comp_buf, 1, sizeof(comp_buf) - avail_out, comp_file_gz); fwrite(comp_buf, 1, CACHE_BUF_SIZE - avail_out, comp_file_gz);
} while (avail_in != 0); } while (avail_in != 0);
avail_in = read; avail_in = read;
do { do {
avail_out = sizeof(comp_buf); avail_out = CACHE_BUF_SIZE;
compress_compress_mode(&comp_ctx, COMPRESS_BR, buf + read - avail_in, &avail_in, compress_compress_mode(&comp_ctx, COMPRESS_BR, buf + read - avail_in, &avail_in,
comp_buf, &avail_out, feof(file)); comp_buf, &avail_out, feof(file));
fwrite(comp_buf, 1, sizeof(comp_buf) - avail_out, comp_file_br); fwrite(comp_buf, 1, CACHE_BUF_SIZE - avail_out, comp_file_br);
} while (avail_in != 0); } while (avail_in != 0);
} }
} }
@@ -193,6 +194,8 @@ int cache_process() {
cache_file = fopen("/var/necronda-server/cache", "wb"); cache_file = fopen("/var/necronda-server/cache", "wb");
if (cache_file == NULL) { if (cache_file == NULL) {
fprintf(stderr, ERR_STR "Unable to open cache file: %s" CLR_STR "\n", strerror(errno)); fprintf(stderr, ERR_STR "Unable to open cache file: %s" CLR_STR "\n", strerror(errno));
free(buf);
free(comp_buf);
return -1; return -1;
} }
fwrite(cache, sizeof(cache_entry), CACHE_ENTRIES, cache_file); fwrite(cache, sizeof(cache_entry), CACHE_ENTRIES, cache_file);
@@ -201,6 +204,8 @@ int cache_process() {
sleep(1); sleep(1);
} }
} }
free(buf);
free(comp_buf);
return 0; return 0;
} }

View File

@@ -12,6 +12,7 @@
#define CACHE_SHM_KEY 255641 #define CACHE_SHM_KEY 255641
#define CACHE_ENTRIES 1024 #define CACHE_ENTRIES 1024
#define CACHE_BUF_SIZE 16384
#ifndef CACHE_MAGIC_FILE #ifndef CACHE_MAGIC_FILE
# define CACHE_MAGIC_FILE "/usr/share/file/misc/magic.mgc" # define CACHE_MAGIC_FILE "/usr/share/file/misc/magic.mgc"

View File

@@ -457,7 +457,7 @@ int fastcgi_send(fastcgi_conn *conn, sock *client, int flags) {
content_len = 0; content_len = 0;
goto out; goto out;
finish: finish:
if (flags & FASTCGI_COMPRESS) compress_free(&comp_ctx); compress_free(&comp_ctx);
} }
if (flags & FASTCGI_CHUNKED) { if (flags & FASTCGI_CHUNKED) {
@@ -476,10 +476,8 @@ int fastcgi_send(fastcgi_conn *conn, sock *client, int flags) {
int buf_len = content_len; int buf_len = content_len;
if (flags & FASTCGI_COMPRESS) { if (flags & FASTCGI_COMPRESS) {
avail_out = sizeof(comp_out); avail_out = sizeof(comp_out);
if (flags & FASTCGI_COMPRESS) { compress_compress(&comp_ctx, next_in + content_len - avail_in, &avail_in, comp_out, &avail_out,
compress_compress(&comp_ctx, next_in + content_len - avail_in, &avail_in, finish_comp);
comp_out, &avail_out, finish_comp);
}
ptr = comp_out; ptr = comp_out;
buf_len = (int) (sizeof(comp_out) - avail_out); buf_len = (int) (sizeof(comp_out) - avail_out);
} }