FastCGI compression
This commit is contained in:
@ -112,8 +112,6 @@ int cache_process() {
|
|||||||
if (compress) {
|
if (compress) {
|
||||||
strm.avail_in = read;
|
strm.avail_in = read;
|
||||||
strm.next_in = (unsigned char *) buf;
|
strm.next_in = (unsigned char *) buf;
|
||||||
strm.avail_out = sizeof(comp_buf);
|
|
||||||
strm.next_out = (unsigned char *) comp_buf;
|
|
||||||
do {
|
do {
|
||||||
strm.avail_out = sizeof(comp_buf);
|
strm.avail_out = sizeof(comp_buf);
|
||||||
strm.next_out = (unsigned char *) comp_buf;
|
strm.next_out = (unsigned char *) comp_buf;
|
||||||
|
@ -240,7 +240,7 @@ int client_request_handler(sock *client, unsigned long client_num, unsigned int
|
|||||||
|
|
||||||
char *accept_encoding = http_get_header_field(&req.hdr, "Accept-Encoding");
|
char *accept_encoding = http_get_header_field(&req.hdr, "Accept-Encoding");
|
||||||
if (accept_encoding != NULL && strstr(accept_encoding, "deflate") != NULL) {
|
if (accept_encoding != NULL && strstr(accept_encoding, "deflate") != NULL) {
|
||||||
//http_add_header_field(&res.hdr, "Content-Encoding", "deflate");
|
http_add_header_field(&res.hdr, "Content-Encoding", "deflate");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (fastcgi_header(&php_fpm, &res, err_msg) != 0) {
|
if (fastcgi_header(&php_fpm, &res, err_msg) != 0) {
|
||||||
@ -334,7 +334,8 @@ int client_request_handler(sock *client, unsigned long client_num, unsigned int
|
|||||||
int chunked = transfer_encoding != NULL && strncmp(transfer_encoding, "chunked", 7) == 0;
|
int chunked = transfer_encoding != NULL && strncmp(transfer_encoding, "chunked", 7) == 0;
|
||||||
char *content_encoding = http_get_header_field(&res.hdr, "Content-Encoding");
|
char *content_encoding = http_get_header_field(&res.hdr, "Content-Encoding");
|
||||||
int comp = content_encoding != NULL && strncmp(content_encoding, "deflate", 7) == 0;
|
int comp = content_encoding != NULL && strncmp(content_encoding, "deflate", 7) == 0;
|
||||||
fastcgi_send(&php_fpm, client, (chunked ? FASTCGI_CHUNKED : 0) | (comp ? FASTCGI_COMPRESS : 0));
|
int flags = (chunked ? FASTCGI_CHUNKED : 0) | (comp ? FASTCGI_COMPRESS : 0);
|
||||||
|
fastcgi_send(&php_fpm, client, flags);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -361,6 +361,20 @@ int fastcgi_send(fastcgi_conn *conn, sock *client, int flags) {
|
|||||||
int len;
|
int len;
|
||||||
char *content, *ptr;
|
char *content, *ptr;
|
||||||
unsigned short req_id, content_len;
|
unsigned short req_id, content_len;
|
||||||
|
char comp_out[4096];
|
||||||
|
int finish_comp = 0;
|
||||||
|
|
||||||
|
z_stream strm;
|
||||||
|
if (flags & FASTCGI_COMPRESS) {
|
||||||
|
int level = NECRONDA_ZLIB_LEVEL;
|
||||||
|
strm.zalloc = Z_NULL;
|
||||||
|
strm.zfree = Z_NULL;
|
||||||
|
strm.opaque = Z_NULL;
|
||||||
|
if (deflateInit(&strm, level) != Z_OK) {
|
||||||
|
fprintf(stderr, ERR_STR "Unable to init deflate: %s" CLR_STR "\n", strerror(errno));
|
||||||
|
flags &= !FASTCGI_COMPRESS;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (conn->out_buf != NULL && conn->out_len > conn->out_off) {
|
if (conn->out_buf != NULL && conn->out_len > conn->out_off) {
|
||||||
content = conn->out_buf;
|
content = conn->out_buf;
|
||||||
@ -407,6 +421,13 @@ int fastcgi_send(fastcgi_conn *conn, sock *client, int flags) {
|
|||||||
conn->socket = 0;
|
conn->socket = 0;
|
||||||
free(content);
|
free(content);
|
||||||
|
|
||||||
|
if (flags & FASTCGI_COMPRESS) {
|
||||||
|
finish_comp = 1;
|
||||||
|
goto out;
|
||||||
|
finish:
|
||||||
|
deflateEnd(&strm);
|
||||||
|
}
|
||||||
|
|
||||||
if (flags & FASTCGI_CHUNKED) {
|
if (flags & FASTCGI_CHUNKED) {
|
||||||
if (client->enc) {
|
if (client->enc) {
|
||||||
SSL_write(client->ssl, "0\r\n\r\n", 5);
|
SSL_write(client->ssl, "0\r\n\r\n", 5);
|
||||||
@ -420,16 +441,34 @@ int fastcgi_send(fastcgi_conn *conn, sock *client, int flags) {
|
|||||||
print(ERR_STR "%.*s" CLR_STR, content_len, content);
|
print(ERR_STR "%.*s" CLR_STR, content_len, content);
|
||||||
} else if (header.type == FCGI_STDOUT) {
|
} else if (header.type == FCGI_STDOUT) {
|
||||||
out:
|
out:
|
||||||
len = sprintf(buf0, "%X\r\n", content_len);
|
if (flags & FASTCGI_COMPRESS) {
|
||||||
if (client->enc) {
|
strm.avail_in = content_len;
|
||||||
if (flags & FASTCGI_CHUNKED) SSL_write(client->ssl, buf0, len);
|
strm.next_in = (unsigned char *) ptr;
|
||||||
SSL_write(client->ssl, ptr, content_len);
|
|
||||||
if (flags & FASTCGI_CHUNKED) SSL_write(client->ssl, "\r\n", 2);
|
|
||||||
} else {
|
|
||||||
if (flags & FASTCGI_CHUNKED) send(client->socket, buf0, len, 0);
|
|
||||||
send(client->socket, ptr, content_len, 0);
|
|
||||||
if (flags & FASTCGI_CHUNKED) send(client->socket, "\r\n", 2, 0);
|
|
||||||
}
|
}
|
||||||
|
do {
|
||||||
|
int buf_len = content_len;
|
||||||
|
if (flags & FASTCGI_COMPRESS) {
|
||||||
|
strm.avail_out = sizeof(comp_out);
|
||||||
|
strm.next_out = (unsigned char *) comp_out;
|
||||||
|
deflate(&strm, finish_comp ? Z_FINISH : Z_NO_FLUSH);
|
||||||
|
strm.avail_in = 0;
|
||||||
|
ptr = comp_out;
|
||||||
|
buf_len = (int) (sizeof(comp_out) - strm.avail_out);
|
||||||
|
}
|
||||||
|
if (buf_len != 0) {
|
||||||
|
len = sprintf(buf0, "%X\r\n", buf_len);
|
||||||
|
if (client->enc) {
|
||||||
|
if (flags & FASTCGI_CHUNKED) SSL_write(client->ssl, buf0, len);
|
||||||
|
SSL_write(client->ssl, ptr, buf_len);
|
||||||
|
if (flags & FASTCGI_CHUNKED) SSL_write(client->ssl, "\r\n", 2);
|
||||||
|
} else {
|
||||||
|
if (flags & FASTCGI_CHUNKED) send(client->socket, buf0, len, 0);
|
||||||
|
send(client->socket, ptr, buf_len, 0);
|
||||||
|
if (flags & FASTCGI_CHUNKED) send(client->socket, "\r\n", 2, 0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} while (!(flags & FASTCGI_COMPRESS) || strm.avail_out == 0);
|
||||||
|
if (finish_comp) goto finish;
|
||||||
} else {
|
} else {
|
||||||
fprintf(stderr, ERR_STR "Unknown FastCGI type: %i" CLR_STR "\n", header.type);
|
fprintf(stderr, ERR_STR "Unknown FastCGI type: %i" CLR_STR "\n", header.type);
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user