Project

General

Profile

lighttpd-1.4.26.mod_deflate.patch

Updated patch for 1.4.26. patches against the released tarball. - toxic, 2010-02-18 03:35

View differences:

lighttpd-1.4.26-mod_deflate/configure 2010-02-17 16:17:10.812189420 -0800
17393 17393

  
17394 17394

  
17395 17395
do_build="mod_cgi mod_fastcgi mod_extforward mod_proxy mod_evhost mod_simple_vhost mod_access mod_alias mod_setenv mod_usertrack mod_auth mod_status mod_accesslog"
17396
do_build="$do_build mod_rrdtool mod_secdownload mod_expire mod_compress mod_dirlisting mod_indexfile mod_userdir mod_webdav mod_staticfile mod_scgi mod_flv_streaming"
17396
do_build="$do_build mod_rrdtool mod_secdownload mod_expire mod_compress mod_dirlisting mod_indexfile mod_userdir mod_webdav mod_staticfile mod_scgi mod_flv_streaming mod_deflate"
17397 17397

  
17398 17398
plugins="mod_rewrite mod_redirect mod_ssi mod_trigger_b4_dl"
17399 17399
features="regex-conditionals"
lighttpd-1.4.26-mod_deflate/src/base.h 2010-02-17 16:17:49.836439336 -0800
153 153

  
154 154
	http_method_t  http_method;
155 155
	http_version_t http_version;
156
	int true_http_10_client;
156 157

  
157 158
	buffer *request_line;
158 159

  
......
377 378

  
378 379
	int file_started;
379 380
	int file_finished;
381
	int end_chunk; /* used for chunked transfer encoding. */
380 382

  
381
	chunkqueue *write_queue;      /* a large queue for low-level write ( HTTP response ) [ file, mem ] */
383
	chunkqueue *write_queue;  /* a large queue for HTTP response content [ file, mem ] */
384
	chunkqueue *output_queue; /* a large queue for low-level write ( HTTP response ) [ file, mem ] */
382 385
	chunkqueue *read_queue;       /* a small queue for low-level read ( HTTP request ) [ mem ] */
383 386
	chunkqueue *request_content_queue; /* takes request-content into tempfile if necessary [ tempfile, mem ]*/
384 387

  
......
620 623

  
621 624
	connections *conns;
622 625
	connections *joblist;
626
	connections *joblist_prev;
623 627
	connections *fdwaitqueue;
624 628

  
625 629
	stat_cache  *stat_cache;
lighttpd-1.4.26-mod_deflate/src/chunk.c 2010-02-17 16:17:49.838439897 -0800
4 4
 *
5 5
 */
6 6

  
7
#include "server.h"
7 8
#include "chunk.h"
9
#include "log.h"
8 10

  
9 11
#include <sys/types.h>
10 12
#include <sys/stat.h>
......
239 241
	return 0;
240 242
}
241 243

  
244
int chunkqueue_append_chunkqueue(chunkqueue *cq, chunkqueue *src) {
245
	if(src == NULL) return 0;
246
	chunkqueue_append_chunk(cq, src->first);
247
	cq->last = src->last;
248
	src->first = NULL;
249
	src->last = NULL;
250

  
251
	return 0;
252
}
253

  
242 254
buffer * chunkqueue_get_prepend_buffer(chunkqueue *cq) {
243 255
	chunk *c;
244 256

  
......
398 410

  
399 411
	return 0;
400 412
}
413

  
414
/**
415
 * the HTTP chunk-API
416
 * 
417
 * 
418
 */
419

  
420
static int chunk_encode_append_len(chunkqueue *cq, size_t len) {
421
	size_t i, olen = len, j;
422
	buffer *b;
423
	
424
	/*b = srv->tmp_chunk_len;*/
425
	/*b = buffer_init();*/
426
	b = chunkqueue_get_append_buffer(cq);
427
	
428
	if (len == 0) {
429
		buffer_copy_string(b, "0");
430
	} else {
431
		for (i = 0; i < 8 && len; i++) {
432
			len >>= 4;
433
		}
434
		
435
		/* i is the number of hex digits we have */
436
		buffer_prepare_copy(b, i + 1);
437
		
438
		for (j = i-1, len = olen; j+1 > 0; j--) {
439
			b->ptr[j] = (len & 0xf) + (((len & 0xf) <= 9) ? '0' : 'a' - 10);
440
			len >>= 4;
441
		}
442
		b->used = i;
443
		b->ptr[b->used++] = '\0';
444
	}
445
		
446
	buffer_append_string(b, "\r\n");
447
	/*
448
	chunkqueue_append_buffer(cq, b);
449
	buffer_free(b);
450
	*/
451
	
452
	return 0;
453
}
454

  
455

  
456
int chunk_encode_append_file(chunkqueue *cq, buffer *fn, off_t offset, off_t len) {
457
	if (!cq) return -1;
458
	if (len == 0) return 0;
459
	
460
	chunk_encode_append_len(cq, len);
461
	
462
	chunkqueue_append_file(cq, fn, offset, len);
463
	
464
	chunkqueue_append_mem(cq, "\r\n", 2 + 1);
465
	
466
	return 0;
467
}
468

  
469
int chunk_encode_append_buffer(chunkqueue *cq, buffer *mem) {
470
	if (!cq) return -1;
471
	if (mem->used <= 1) return 0;
472
	
473
	chunk_encode_append_len(cq, mem->used - 1);
474
	
475
	chunkqueue_append_buffer(cq, mem);
476
	
477
	chunkqueue_append_mem(cq, "\r\n", 2 + 1);
478
	
479
	return 0;
480
}
481

  
482
int chunk_encode_append_mem(chunkqueue *cq, const char * mem, size_t len) {
483
	if (!cq) return -1;
484
	if (len <= 1) return 0;
485
	
486
	chunk_encode_append_len(cq, len - 1);
487
	
488
	chunkqueue_append_mem(cq, mem, len);
489
	
490
	chunkqueue_append_mem(cq, "\r\n", 2 + 1);
491
	
492
	return 0;
493
}
494

  
495
int chunk_encode_append_queue(chunkqueue *cq, chunkqueue *src) {
496
	int len = chunkqueue_length(src);
497
	if (!cq) return -1;
498
	if (len == 0) return 0;
499
	
500
	chunk_encode_append_len(cq, len);
501
	
502
	chunkqueue_append_chunkqueue(cq, src);
503
	
504
	chunkqueue_append_mem(cq, "\r\n", 2 + 1);
505
	
506
	return 0;
507
}
508

  
509
int chunk_encode_end(chunkqueue *cq) {
510
	chunk_encode_append_len(cq, 0);
511
	chunkqueue_append_mem(cq, "\r\n", 2 + 1);
512
	return 0;
513
}
514

  
lighttpd-1.4.26-mod_deflate/src/chunk.h 2010-02-17 16:17:49.840439830 -0800
53 53
int chunkqueue_append_mem(chunkqueue *c, const char *mem, size_t len);
54 54
int chunkqueue_append_buffer(chunkqueue *c, buffer *mem);
55 55
int chunkqueue_append_buffer_weak(chunkqueue *c, buffer *mem);
56
int chunkqueue_append_chunkqueue(chunkqueue *cq, chunkqueue *src);
56 57
int chunkqueue_prepend_buffer(chunkqueue *c, buffer *mem);
57 58

  
58 59
buffer * chunkqueue_get_append_buffer(chunkqueue *c);
......
68 69

  
69 70
int chunkqueue_is_empty(chunkqueue *c);
70 71

  
72
int chunk_encode_append_mem(chunkqueue *cq, const char * mem, size_t len);
73
int chunk_encode_append_buffer(chunkqueue *cq, buffer *mem);
74
int chunk_encode_append_file(chunkqueue *cq, buffer *fn, off_t offset, off_t len);
75
int chunk_encode_append_queue(chunkqueue *cq, chunkqueue *src);
76
int chunk_encode_end(chunkqueue *cq);
77

  
71 78
#endif
lighttpd-1.4.26-mod_deflate/src/configparser.c 2010-02-17 16:46:52.563459514 -0800
4 4
/* First off, code is include which follows the "include" declaration
5 5
** in the input file. */
6 6
#include <stdio.h>
7
#line 5 "../../src/configparser.y"
7
#line 5 "./configparser.y"
8 8

  
9 9
#include "configfile.h"
10 10
#include "buffer.h"
......
518 518
    case 23:
519 519
    case 24:
520 520
    case 25:
521
#line 144 "../../src/configparser.y"
521
#line 144 "./configparser.y"
522 522
{ buffer_free((yypminor->yy0)); }
523 523
#line 523 "configparser.c"
524 524
      break;
525 525
    case 35:
526
#line 135 "../../src/configparser.y"
526
#line 135 "./configparser.y"
527 527
{ (yypminor->yy41)->free((yypminor->yy41)); }
528 528
#line 528 "configparser.c"
529 529
      break;
530 530
    case 36:
531
#line 136 "../../src/configparser.y"
531
#line 136 "./configparser.y"
532 532
{ (yypminor->yy41)->free((yypminor->yy41)); }
533 533
#line 533 "configparser.c"
534 534
      break;
535 535
    case 37:
536
#line 137 "../../src/configparser.y"
536
#line 137 "./configparser.y"
537 537
{ (yypminor->yy41)->free((yypminor->yy41)); }
538 538
#line 538 "configparser.c"
539 539
      break;
540 540
    case 39:
541
#line 138 "../../src/configparser.y"
541
#line 138 "./configparser.y"
542 542
{ array_free((yypminor->yy40)); }
543 543
#line 543 "configparser.c"
544 544
      break;
545 545
    case 40:
546
#line 139 "../../src/configparser.y"
546
#line 139 "./configparser.y"
547 547
{ array_free((yypminor->yy40)); }
548 548
#line 548 "configparser.c"
549 549
      break;
550 550
    case 41:
551
#line 140 "../../src/configparser.y"
551
#line 140 "./configparser.y"
552 552
{ buffer_free((yypminor->yy43)); }
553 553
#line 553 "configparser.c"
554 554
      break;
555 555
    case 42:
556
#line 141 "../../src/configparser.y"
556
#line 141 "./configparser.y"
557 557
{ buffer_free((yypminor->yy43)); }
558 558
#line 558 "configparser.c"
559 559
      break;
......
823 823
        /* No destructor defined for global */
824 824
        break;
825 825
      case 5:
826
#line 117 "../../src/configparser.y"
826
#line 117 "./configparser.y"
827 827
{ yymsp[-1].minor.yy78 = NULL; }
828 828
#line 828 "configparser.c"
829 829
  yy_destructor(1,&yymsp[0].minor);
......
838 838
  yy_destructor(1,&yymsp[0].minor);
839 839
        break;
840 840
      case 9:
841
#line 146 "../../src/configparser.y"
841
#line 146 "./configparser.y"
842 842
{
843 843
  if (ctx->ok) {
844 844
    buffer_copy_string_buffer(yymsp[0].minor.yy41->key, yymsp[-2].minor.yy43);
......
866 866
  yy_destructor(2,&yymsp[-1].minor);
867 867
        break;
868 868
      case 10:
869
#line 170 "../../src/configparser.y"
869
#line 170 "./configparser.y"
870 870
{
871 871
  array *vars = ctx->current->value;
872 872
  data_unset *du;
......
909 909
  yy_destructor(3,&yymsp[-1].minor);
910 910
        break;
911 911
      case 11:
912
#line 209 "../../src/configparser.y"
912
#line 209 "./configparser.y"
913 913
{
914 914
  if (strchr(yymsp[0].minor.yy0->ptr, '.') == NULL) {
915 915
    yygotominor.yy43 = buffer_init_string("var.");
......
924 924
#line 924 "configparser.c"
925 925
        break;
926 926
      case 12:
927
#line 221 "../../src/configparser.y"
927
#line 221 "./configparser.y"
928 928
{
929 929
  yygotominor.yy41 = configparser_merge_data(yymsp[-2].minor.yy41, yymsp[0].minor.yy41);
930 930
  if (NULL == yygotominor.yy41) {
......
938 938
  yy_destructor(5,&yymsp[-1].minor);
939 939
        break;
940 940
      case 13:
941
#line 231 "../../src/configparser.y"
941
#line 231 "./configparser.y"
942 942
{
943 943
  yygotominor.yy41 = yymsp[0].minor.yy41;
944 944
  yymsp[0].minor.yy41 = NULL;
......
946 946
#line 946 "configparser.c"
947 947
        break;
948 948
      case 14:
949
#line 236 "../../src/configparser.y"
949
#line 236 "./configparser.y"
950 950
{
951 951
  yygotominor.yy41 = NULL;
952 952
  if (strncmp(yymsp[0].minor.yy43->ptr, "env.", sizeof("env.") - 1) == 0) {
......
976 976
#line 976 "configparser.c"
977 977
        break;
978 978
      case 15:
979
#line 263 "../../src/configparser.y"
979
#line 263 "./configparser.y"
980 980
{
981 981
  yygotominor.yy41 = (data_unset *)data_string_init();
982 982
  buffer_copy_string_buffer(((data_string *)(yygotominor.yy41))->value, yymsp[0].minor.yy0);
......
986 986
#line 986 "configparser.c"
987 987
        break;
988 988
      case 16:
989
#line 270 "../../src/configparser.y"
989
#line 270 "./configparser.y"
990 990
{
991 991
  yygotominor.yy41 = (data_unset *)data_integer_init();
992 992
  ((data_integer *)(yygotominor.yy41))->value = strtol(yymsp[0].minor.yy0->ptr, NULL, 10);
......
996 996
#line 996 "configparser.c"
997 997
        break;
998 998
      case 17:
999
#line 276 "../../src/configparser.y"
999
#line 276 "./configparser.y"
1000 1000
{
1001 1001
  yygotominor.yy41 = (data_unset *)data_array_init();
1002 1002
  array_free(((data_array *)(yygotominor.yy41))->value);
......
1006 1006
#line 1006 "configparser.c"
1007 1007
        break;
1008 1008
      case 18:
1009
#line 282 "../../src/configparser.y"
1009
#line 282 "./configparser.y"
1010 1010
{
1011 1011
  yygotominor.yy40 = array_init();
1012 1012
}
......
1015 1015
  yy_destructor(9,&yymsp[0].minor);
1016 1016
        break;
1017 1017
      case 19:
1018
#line 285 "../../src/configparser.y"
1018
#line 285 "./configparser.y"
1019 1019
{
1020 1020
  yygotominor.yy40 = yymsp[-1].minor.yy40;
1021 1021
  yymsp[-1].minor.yy40 = NULL;
......
1025 1025
  yy_destructor(9,&yymsp[0].minor);
1026 1026
        break;
1027 1027
      case 20:
1028
#line 290 "../../src/configparser.y"
1028
#line 290 "./configparser.y"
1029 1029
{
1030 1030
  if (buffer_is_empty(yymsp[0].minor.yy41->key) ||
1031 1031
      NULL == array_get_element(yymsp[-2].minor.yy40, yymsp[0].minor.yy41->key->ptr)) {
......
1046 1046
  yy_destructor(10,&yymsp[-1].minor);
1047 1047
        break;
1048 1048
      case 21:
1049
#line 307 "../../src/configparser.y"
1049
#line 307 "./configparser.y"
1050 1050
{
1051 1051
  yygotominor.yy40 = yymsp[-1].minor.yy40;
1052 1052
  yymsp[-1].minor.yy40 = NULL;
......
1055 1055
  yy_destructor(10,&yymsp[0].minor);
1056 1056
        break;
1057 1057
      case 22:
1058
#line 312 "../../src/configparser.y"
1058
#line 312 "./configparser.y"
1059 1059
{
1060 1060
  yygotominor.yy40 = array_init();
1061 1061
  array_insert_unique(yygotominor.yy40, yymsp[0].minor.yy41);
......
1064 1064
#line 1064 "configparser.c"
1065 1065
        break;
1066 1066
      case 23:
1067
#line 318 "../../src/configparser.y"
1067
#line 318 "./configparser.y"
1068 1068
{
1069 1069
  yygotominor.yy41 = yymsp[0].minor.yy41;
1070 1070
  yymsp[0].minor.yy41 = NULL;
......
1072 1072
#line 1072 "configparser.c"
1073 1073
        break;
1074 1074
      case 24:
1075
#line 322 "../../src/configparser.y"
1075
#line 322 "./configparser.y"
1076 1076
{
1077 1077
  buffer_copy_string_buffer(yymsp[0].minor.yy41->key, yymsp[-2].minor.yy43);
1078 1078
  buffer_free(yymsp[-2].minor.yy43);
......
1090 1090
      case 26:
1091 1091
        break;
1092 1092
      case 27:
1093
#line 334 "../../src/configparser.y"
1093
#line 334 "./configparser.y"
1094 1094
{
1095 1095
  data_config *dc;
1096 1096
  dc = (data_config *)array_get_element(ctx->srv->config_context, "global");
......
1101 1101
  yy_destructor(12,&yymsp[0].minor);
1102 1102
        break;
1103 1103
      case 28:
1104
#line 341 "../../src/configparser.y"
1104
#line 341 "./configparser.y"
1105 1105
{
1106 1106
  data_config *cur;
1107 1107

  
......
1119 1119
  yy_destructor(14,&yymsp[0].minor);
1120 1120
        break;
1121 1121
      case 29:
1122
#line 352 "../../src/configparser.y"
1122
#line 352 "./configparser.y"
1123 1123
{
1124 1124
  if (yymsp[-3].minor.yy78->context_ndx >= yymsp[0].minor.yy78->context_ndx) {
1125 1125
    fprintf(stderr, "unreachable else condition\n");
......
1136 1136
  yy_destructor(15,&yymsp[-1].minor);
1137 1137
        break;
1138 1138
      case 30:
1139
#line 364 "../../src/configparser.y"
1139
#line 364 "./configparser.y"
1140 1140
{
1141 1141
  yygotominor.yy78 = yymsp[0].minor.yy78;
1142 1142
  yymsp[0].minor.yy78 = NULL;
......
1144 1144
#line 1144 "configparser.c"
1145 1145
        break;
1146 1146
      case 31:
1147
#line 369 "../../src/configparser.y"
1147
#line 369 "./configparser.y"
1148 1148
{
1149 1149
  data_config *cur;
1150 1150

  
......
1162 1162
  yy_destructor(14,&yymsp[0].minor);
1163 1163
        break;
1164 1164
      case 32:
1165
#line 380 "../../src/configparser.y"
1165
#line 380 "./configparser.y"
1166 1166
{
1167 1167
  data_config *dc;
1168 1168
  buffer *b, *rvalue, *op;
......
1318 1318
  yy_destructor(19,&yymsp[-2].minor);
1319 1319
        break;
1320 1320
      case 33:
1321
#line 529 "../../src/configparser.y"
1321
#line 529 "./configparser.y"
1322 1322
{
1323 1323
  yygotominor.yy27 = CONFIG_COND_EQ;
1324 1324
}
......
1326 1326
  yy_destructor(20,&yymsp[0].minor);
1327 1327
        break;
1328 1328
      case 34:
1329
#line 532 "../../src/configparser.y"
1329
#line 532 "./configparser.y"
1330 1330
{
1331 1331
  yygotominor.yy27 = CONFIG_COND_MATCH;
1332 1332
}
......
1334 1334
  yy_destructor(21,&yymsp[0].minor);
1335 1335
        break;
1336 1336
      case 35:
1337
#line 535 "../../src/configparser.y"
1337
#line 535 "./configparser.y"
1338 1338
{
1339 1339
  yygotominor.yy27 = CONFIG_COND_NE;
1340 1340
}
......
1342 1342
  yy_destructor(22,&yymsp[0].minor);
1343 1343
        break;
1344 1344
      case 36:
1345
#line 538 "../../src/configparser.y"
1345
#line 538 "./configparser.y"
1346 1346
{
1347 1347
  yygotominor.yy27 = CONFIG_COND_NOMATCH;
1348 1348
}
......
1350 1350
  yy_destructor(23,&yymsp[0].minor);
1351 1351
        break;
1352 1352
      case 37:
1353
#line 542 "../../src/configparser.y"
1353
#line 542 "./configparser.y"
1354 1354
{
1355 1355
  yygotominor.yy43 = NULL;
1356 1356
  if (ctx->ok) {
......
1370 1370
#line 1370 "configparser.c"
1371 1371
        break;
1372 1372
      case 38:
1373
#line 559 "../../src/configparser.y"
1373
#line 559 "./configparser.y"
1374 1374
{
1375 1375
  if (ctx->ok) {
1376 1376
    if (0 != config_parse_file(ctx->srv, ctx, yymsp[0].minor.yy43->ptr)) {
......
1384 1384
  yy_destructor(24,&yymsp[-1].minor);
1385 1385
        break;
1386 1386
      case 39:
1387
#line 569 "../../src/configparser.y"
1387
#line 569 "./configparser.y"
1388 1388
{
1389 1389
  if (ctx->ok) {
1390 1390
    if (0 != config_parse_cmd(ctx->srv, ctx, yymsp[0].minor.yy43->ptr)) {
......
1424 1424
  while( yypParser->yyidx>=0 ) yy_pop_parser_stack(yypParser);
1425 1425
  /* Here code is inserted which will be executed whenever the
1426 1426
  ** parser fails */
1427
#line 108 "../../src/configparser.y"
1427
#line 108 "./configparser.y"
1428 1428

  
1429 1429
  ctx->ok = 0;
1430 1430

  
lighttpd-1.4.26-mod_deflate/src/connections.c 2010-02-17 16:17:49.843439904 -0800
8 8
#include "response.h"
9 9
#include "network.h"
10 10
#include "http_chunk.h"
11
#include "chunk.h"
11 12
#include "stat_cache.h"
12 13
#include "joblist.h"
13 14

  
......
146 147
	return 0;
147 148
}
148 149

  
150
int connection_queue_is_empty(connection *con) {
151
	if(!chunkqueue_is_empty(con->write_queue)) return 0;
152
	if(!chunkqueue_is_empty(con->output_queue)) return 0;
153
	return 1;
154
}
155

  
149 156
#if 0
150 157
static void dump_packet(const unsigned char *data, size_t len) {
151 158
	size_t i, j;
......
432 439
				con->file_finished = 1;
433 440

  
434 441
				chunkqueue_reset(con->write_queue);
442
				chunkqueue_reset(con->output_queue);
435 443
			}
436 444
			break;
437 445
		default:
......
461 469
		con->response.transfer_encoding &= ~HTTP_TRANSFER_ENCODING_CHUNKED;
462 470
		con->parsed_response &= ~HTTP_CONTENT_LENGTH;
463 471
		chunkqueue_reset(con->write_queue);
472
		chunkqueue_reset(con->output_queue);
464 473

  
465 474
		con->file_finished = 1;
466 475
		break;
......
529 538
		break;
530 539
	}
531 540

  
541
	/* Allow filter plugins to change response headers before they are written. */
542
	switch(plugins_call_handle_response_start(srv, con)) {
543
	case HANDLER_GO_ON:
544
	case HANDLER_FINISHED:
545
		/* response start is finished */
546
		break;
547
	default:
548
		/* something strange happend */
549
		log_error_write(srv, __FILE__, __LINE__, "s", "Filter plugin failed.");
550
		connection_set_state(srv, con, CON_STATE_ERROR);
551
		joblist_append(srv, con);
552
		break;
553
	}
554

  
532 555
	if (con->file_finished) {
533 556
		/* we have all the content and chunked encoding is not used, set a content-length */
534 557

  
......
600 623
		con->file_finished = 1;
601 624

  
602 625
		chunkqueue_reset(con->write_queue);
626
		chunkqueue_reset(con->output_queue);
603 627
		con->response.transfer_encoding &= ~HTTP_TRANSFER_ENCODING_CHUNKED;
604 628
	}
605 629

  
......
609 633
}
610 634

  
611 635
static int connection_handle_write(server *srv, connection *con) {
612
	switch(network_write_chunkqueue(srv, con, con->write_queue)) {
636
	int finished = 0;
637
	int len;
638

  
639
	/* Allow filter plugins to modify response conent */
640
	switch(plugins_call_handle_response_filter(srv, con)) {
641
	case HANDLER_GO_ON:
642
		finished = con->file_finished;
643
		/* response content not changed */
644
		break;
645
	case HANDLER_COMEBACK:
646
		/* response filter has more work */
647
		finished = 0;
648
		break;
649
	case HANDLER_FINISHED:
650
		/* response filter is finished */
651
		finished = 1;
652
		break;
653
	default:
654
		/* something strange happend */
655
		log_error_write(srv, __FILE__, __LINE__, "s", "Filter plugin failed.");
656
		connection_set_state(srv, con, CON_STATE_ERROR);
657
		joblist_append(srv, con);
658
		finished = 1;
659
		break;
660
	}
661

  
662
	/* move chunks from write_queue to output_queue. */
663
	if (con->request.http_method == HTTP_METHOD_HEAD) {
664
		chunkqueue_reset(con->write_queue);
665
	} else {
666
		len = chunkqueue_length(con->write_queue);
667
		if(con->response.transfer_encoding & HTTP_TRANSFER_ENCODING_CHUNKED) {
668
			chunk_encode_append_queue(con->output_queue, con->write_queue);
669
			if(finished && !con->end_chunk) {
670
				con->end_chunk = 1;
671
				chunk_encode_end(con->output_queue);
672
			}
673
		} else {
674
			chunkqueue_append_chunkqueue(con->output_queue, con->write_queue);
675
		}
676
		con->write_queue->bytes_out += len;
677
	}
678
	/* write chunks from output_queue to network */
679
	switch(network_write_chunkqueue(srv, con, con->output_queue)) {
613 680
	case 0:
614
		if (con->file_finished) {
681
		if (finished) {
615 682
			connection_set_state(srv, con, CON_STATE_RESPONSE_END);
616 683
			joblist_append(srv, con);
684
		} else {
685
			/* not finished yet -> WRITE */
686
			con->is_writable = 1;
617 687
		}
618 688
		break;
619 689
	case -1: /* error on our side */
......
686 756

  
687 757
#undef CLEAN
688 758
	con->write_queue = chunkqueue_init();
759
	con->output_queue = chunkqueue_init();
689 760
	con->read_queue = chunkqueue_init();
690 761
	con->request_content_queue = chunkqueue_init();
691 762
	chunkqueue_set_tempdirs(con->request_content_queue, srv->srvconf.upload_tempdirs);
......
714 785
		connection_reset(srv, con);
715 786

  
716 787
		chunkqueue_free(con->write_queue);
788
		chunkqueue_free(con->output_queue);
717 789
		chunkqueue_free(con->read_queue);
718 790
		chunkqueue_free(con->request_content_queue);
719 791
		array_free(con->request.headers);
......
771 843
	con->http_status = 0;
772 844
	con->file_finished = 0;
773 845
	con->file_started = 0;
846
	con->end_chunk = 0;
774 847
	con->got_response = 0;
848
//	con->use_cache_file = 0;
849
//	con->write_cache_file = 0;
775 850

  
776 851
	con->parsed_response = 0;
777 852

  
......
844 919
	array_reset(con->environment);
845 920

  
846 921
	chunkqueue_reset(con->write_queue);
922
	chunkqueue_reset(con->output_queue);
847 923
	chunkqueue_reset(con->request_content_queue);
848 924

  
849 925
	/* the plugins should cleanup themself */
......
1229 1305
	}
1230 1306

  
1231 1307
	if (con->state == CON_STATE_WRITE &&
1232
	    !chunkqueue_is_empty(con->write_queue) &&
1233 1308
	    con->is_writable) {
1234 1309

  
1235 1310
		if (-1 == connection_handle_write(srv, con)) {
......
1640 1715
			}
1641 1716

  
1642 1717
			/* only try to write if we have something in the queue */
1643
			if (!chunkqueue_is_empty(con->write_queue)) {
1644 1718
#if 0
1719
			if (!connection_queue_is_empty(con)) {
1645 1720
				log_error_write(srv, __FILE__, __LINE__, "dsd",
1646 1721
						con->fd,
1647 1722
						"packets to write:",
1648
						con->write_queue->used);
1649
#endif
1723
						con->output_queue->used);
1650 1724
			}
1651
			if (!chunkqueue_is_empty(con->write_queue) && con->is_writable) {
1725
#endif
1726
			if (con->is_writable) {
1652 1727
				if (-1 == connection_handle_write(srv, con)) {
1653 1728
					log_error_write(srv, __FILE__, __LINE__, "ds",
1654 1729
							con->fd,
......
1787 1862
		 * - if we have data to write
1788 1863
		 * - if the socket is not writable yet
1789 1864
		 */
1790
		if (!chunkqueue_is_empty(con->write_queue) &&
1791
		    (con->is_writable == 0) &&
1792
		    (con->traffic_limit_reached == 0)) {
1865
		if ((con->is_writable == 0) &&
1866
		    (con->traffic_limit_reached == 0) &&
1867
				!connection_queue_is_empty(con)) {
1793 1868
			fdevent_event_add(srv->ev, &(con->fde_ndx), con->fd, FDEVENT_OUT);
1794 1869
		} else {
1795 1870
			fdevent_event_del(srv->ev, &(con->fde_ndx), con->fd);
lighttpd-1.4.26-mod_deflate/src/http_chunk.c 2010-02-17 16:17:49.844438858 -0800
58 58

  
59 59
	cq = con->write_queue;
60 60

  
61
	if (con->response.transfer_encoding & HTTP_TRANSFER_ENCODING_CHUNKED) {
62
		http_chunk_append_len(srv, con, len);
63
	}
64 61

  
65 62
	chunkqueue_append_file(cq, fn, offset, len);
66 63

  
67
	if (con->response.transfer_encoding & HTTP_TRANSFER_ENCODING_CHUNKED && len > 0) {
68
		chunkqueue_append_mem(cq, "\r\n", 2 + 1);
69
	}
70

  
71 64
	return 0;
72 65
}
73 66

  
......
78 71

  
79 72
	cq = con->write_queue;
80 73

  
81
	if (con->response.transfer_encoding & HTTP_TRANSFER_ENCODING_CHUNKED) {
82
		http_chunk_append_len(srv, con, mem->used - 1);
83
	}
84 74

  
85 75
	chunkqueue_append_buffer(cq, mem);
86 76

  
87
	if (con->response.transfer_encoding & HTTP_TRANSFER_ENCODING_CHUNKED && mem->used > 0) {
88
		chunkqueue_append_mem(cq, "\r\n", 2 + 1);
89
	}
90

  
91 77
	return 0;
92 78
}
93 79

  
......
99 85
	cq = con->write_queue;
100 86

  
101 87
	if (len == 0) {
102
		if (con->response.transfer_encoding & HTTP_TRANSFER_ENCODING_CHUNKED) {
103
			chunkqueue_append_mem(cq, "0\r\n\r\n", 5 + 1);
104
		} else {
105
			chunkqueue_append_mem(cq, "", 1);
106
		}
107 88
		return 0;
108 89
	}
109 90

  
110
	if (con->response.transfer_encoding & HTTP_TRANSFER_ENCODING_CHUNKED) {
111
		http_chunk_append_len(srv, con, len - 1);
112
	}
113

  
114 91
	chunkqueue_append_mem(cq, mem, len);
115 92

  
116
	if (con->response.transfer_encoding & HTTP_TRANSFER_ENCODING_CHUNKED) {
117
		chunkqueue_append_mem(cq, "\r\n", 2 + 1);
118
	}
119

  
120 93
	return 0;
121 94
}
122 95

  
lighttpd-1.4.26-mod_deflate/src/joblist.c 2010-02-17 16:17:49.844438858 -0800
7 7

  
8 8
int joblist_append(server *srv, connection *con) {
9 9
	if (con->in_joblist) return 0;
10
	con->in_joblist = 1;
10 11

  
11 12
	if (srv->joblist->size == 0) {
12 13
		srv->joblist->size  = 16;
lighttpd-1.4.26-mod_deflate/src/Makefile.am 2010-02-17 16:17:49.844438858 -0800
264 264
mod_accesslog_la_LDFLAGS = -module -export-dynamic -avoid-version -no-undefined
265 265
mod_accesslog_la_LIBADD = $(common_libadd)
266 266

  
267
lib_LTLIBRARIES += mod_deflate.la
268
mod_deflate_la_SOURCES = mod_deflate.c 
269
mod_deflate_la_LDFLAGS = -module -export-dynamic -avoid-version -no-undefined
270
mod_deflate_la_LIBADD = $(Z_LIB) $(BZ_LIB) $(common_libadd)
271

  
267 272

  
268 273
hdr = server.h buffer.h network.h log.h keyvalue.h \
269 274
      response.h request.h fastcgi.h chunk.h \
lighttpd-1.4.26-mod_deflate/src/Makefile.in 2010-02-17 16:26:06.434189740 -0800
184 184
	$(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=link $(CCLD) \
185 185
	$(AM_CFLAGS) $(CFLAGS) $(mod_compress_la_LDFLAGS) $(LDFLAGS) \
186 186
	-o $@
187
mod_deflate_la_DEPENDENCIES = $(am__DEPENDENCIES_1) \
188
       $(am__DEPENDENCIES_1) $(am__DEPENDENCIES_2)
189
am_mod_deflate_la_OBJECTS = mod_deflate.lo
190
mod_deflate_la_OBJECTS = $(am_mod_deflate_la_OBJECTS)
191
mod_deflate_la_LINK = $(LIBTOOL) --tag=CC $(AM_LIBTOOLFLAGS) \
192
       $(LIBTOOLFLAGS) --mode=link $(CCLD) $(AM_CFLAGS) $(CFLAGS) \
193
       $(mod_deflate_la_LDFLAGS) $(LDFLAGS) -o $@
187 194
mod_dirlisting_la_DEPENDENCIES = $(am__DEPENDENCIES_2) \
188 195
	$(am__DEPENDENCIES_1)
189 196
am_mod_dirlisting_la_OBJECTS = mod_dirlisting.lo
......
448 455
	$(mod_accesslog_la_SOURCES) $(mod_alias_la_SOURCES) \
449 456
	$(mod_auth_la_SOURCES) $(mod_cgi_la_SOURCES) \
450 457
	$(mod_cml_la_SOURCES) $(mod_compress_la_SOURCES) \
458
	$(mod_deflate_la_SOURCES) \
451 459
	$(mod_dirlisting_la_SOURCES) $(mod_evasive_la_SOURCES) \
452 460
	$(mod_evhost_la_SOURCES) $(mod_expire_la_SOURCES) \
453 461
	$(mod_extforward_la_SOURCES) $(mod_fastcgi_la_SOURCES) \
......
669 677
	mod_ssi.la mod_secdownload.la mod_expire.la mod_evhost.la \
670 678
	mod_simple_vhost.la mod_fastcgi.la mod_extforward.la \
671 679
	mod_access.la mod_compress.la mod_auth.la mod_rewrite.la \
672
	mod_redirect.la mod_status.la mod_accesslog.la
680
	mod_redirect.la mod_status.la mod_accesslog.la mod_deflate.la
673 681
@NO_RDYNAMIC_TRUE@liblightcomp_la_SOURCES = $(common_src)
674 682
@NO_RDYNAMIC_TRUE@liblightcomp_la_CFLAGS = $(AM_CFLAGS)
675 683
@NO_RDYNAMIC_TRUE@liblightcomp_la_LDFLAGS = -avoid-version -no-undefined
......
776 784
mod_accesslog_la_SOURCES = mod_accesslog.c
777 785
mod_accesslog_la_LDFLAGS = -module -export-dynamic -avoid-version -no-undefined
778 786
mod_accesslog_la_LIBADD = $(common_libadd)
787
mod_deflate_la_SOURCES = mod_deflate.c 
788
mod_deflate_la_LDFLAGS = -module -export-dynamic -no-undefined
789
mod_deflate_la_LIBADD = $(Z_LIB) $(BZ_LIB) $(common_libadd)
779 790
hdr = server.h buffer.h network.h log.h keyvalue.h \
780 791
      response.h request.h fastcgi.h chunk.h \
781 792
      settings.h http_chunk.h http_auth_digest.h \
......
894 905
	$(AM_V_CCLD)$(mod_cml_la_LINK) -rpath $(libdir) $(mod_cml_la_OBJECTS) $(mod_cml_la_LIBADD) $(LIBS)
895 906
mod_compress.la: $(mod_compress_la_OBJECTS) $(mod_compress_la_DEPENDENCIES) 
896 907
	$(AM_V_CCLD)$(mod_compress_la_LINK) -rpath $(libdir) $(mod_compress_la_OBJECTS) $(mod_compress_la_LIBADD) $(LIBS)
908
mod_deflate.la: $(mod_deflate_la_OBJECTS) $(mod_deflate_la_DEPENDENCIES) 
909
	$(AM_V_CCLD)$(mod_deflate_la_LINK) -rpath $(libdir) $(mod_deflate_la_LDFLAGS) $(mod_deflate_la_OBJECTS) $(mod_deflate_la_LIBADD) $(LIBS)
897 910
mod_dirlisting.la: $(mod_dirlisting_la_OBJECTS) $(mod_dirlisting_la_DEPENDENCIES) 
898 911
	$(AM_V_CCLD)$(mod_dirlisting_la_LINK) -rpath $(libdir) $(mod_dirlisting_la_OBJECTS) $(mod_dirlisting_la_LIBADD) $(LIBS)
899 912
mod_evasive.la: $(mod_evasive_la_OBJECTS) $(mod_evasive_la_DEPENDENCIES) 
......
1098 1111
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/mod_cml_la-mod_cml_funcs.Plo@am__quote@
1099 1112
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/mod_cml_la-mod_cml_lua.Plo@am__quote@
1100 1113
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/mod_compress.Plo@am__quote@
1114
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/mod_deflate.Plo@am__quote@
1101 1115
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/mod_dirlisting.Plo@am__quote@
1102 1116
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/mod_evasive.Plo@am__quote@
1103 1117
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/mod_evhost.Plo@am__quote@
lighttpd-1.4.26-mod_deflate/src/mod_deflate.c 2010-02-17 16:17:49.847437953 -0800
1
/* bug fix on Robert Jakabosky from alphatrade.com's lighttp 1.4.10 mod_deflate path
2
 *
3
 * new module option:
4
 * deflate.nocompress-url = "^/nocompressurl/" # pcre regex which don't compress
5
 *
6
 * Bug fix and new features:
7
 * 1) fix loop bug when content-length is bigger than work-block-size*k
8
 * 2) prevent compress on buggy http 1.0 client with Accept Encoding: gzip, deflate
9
 * 3) fix bug with chunk transfer encoding (under mod_fastcgi+php environment)
10
 * 
11
 * deflate.sync-flush = "enable" is buggy on chunk encoding transfer. Use it carefully,
12
 */
13
#include <sys/types.h>
14
#include <sys/stat.h>
15

  
16
#include <fcntl.h>
17
#include <unistd.h>
18
#include <ctype.h>
19
#include <stdlib.h>
20
#include <string.h>
21
#include <errno.h>
22
#include <time.h>
23
#include <assert.h>
24

  
25
#if defined(HAVE_PCRE_H)
26
#include <pcre.h>
27
#endif
28

  
29
#include "base.h"
30
#include "log.h"
31
#include "buffer.h"
32
#include "response.h"
33
#include "joblist.h"
34
#include "stat_cache.h"
35

  
36
#include "plugin.h"
37

  
38
#include "crc32.h"
39
#include "etag.h"
40
#include "inet_ntop_cache.h"
41

  
42
#if defined HAVE_ZLIB_H && defined HAVE_LIBZ
43
# define USE_ZLIB
44
# include <zlib.h>
45
#else
46
# define Z_DEFAULT_COMPRESSION 1
47
#endif
48

  
49
#if defined HAVE_BZLIB_H && defined HAVE_LIBBZ2
50
# define USE_BZ2LIB
51
/* we don't need stdio interface */
52
# define BZ_NO_STDIO
53
# include <bzlib.h>
54
#endif
55

  
56
#include "sys-mmap.h"
57

  
58
/* request: accept-encoding */
59
#define HTTP_ACCEPT_ENCODING_IDENTITY BV(0)
60
#define HTTP_ACCEPT_ENCODING_GZIP     BV(1)
61
#define HTTP_ACCEPT_ENCODING_DEFLATE  BV(2)
62
#define HTTP_ACCEPT_ENCODING_COMPRESS BV(3)
63
#define HTTP_ACCEPT_ENCODING_BZIP2    BV(4)
64

  
65
#define KByte * 1024
66
#define MByte * 1024 KByte
67
#define GByte * 1024 MByte
68

  
69
typedef struct {
70
	unsigned short	debug;
71
	unsigned short	enabled;
72
	unsigned short	bzip2;
73
	unsigned short	sync_flush;
74
	unsigned short	output_buffer_size;
75
	unsigned short	min_compress_size;
76
	unsigned short	work_block_size;
77
	short		mem_level;
78
	short		compression_level;
79
	short		window_size;
80
	array		*mimetypes;
81
	buffer 		*nocompress_url;
82
#if defined(HAVE_PCRE_H)
83
	pcre	*nocompress_regex;
84
#endif
85
} plugin_config;
86

  
87
typedef struct {
88
	PLUGIN_DATA;
89
	buffer *tmp_buf;
90
	
91
	plugin_config **config_storage;
92
	plugin_config conf; 
93
} plugin_data;
94

  
95
typedef struct {
96
	int bytes_in;
97
	int bytes_out;
98
	chunkqueue *in_queue;
99
	buffer *output;
100
	/* compression type & state */
101
	int compression_type;
102
	int stream_open;
103
#ifdef USE_ZLIB
104
	unsigned long crc;
105
	z_stream z;
106
	unsigned short gzip_header;
107
#endif
108
#ifdef USE_BZ2LIB
109
	bz_stream bz;
110
#endif
111
	plugin_data *plugin_data;
112
} handler_ctx;
113

  
114
static handler_ctx *handler_ctx_init() {
115
	handler_ctx *hctx;
116

  
117
	hctx = calloc(1, sizeof(*hctx));
118
	hctx->in_queue = chunkqueue_init();
119

  
120
	return hctx;
121
}
122

  
123
static void handler_ctx_free(handler_ctx *hctx) {
124
	chunkqueue_free(hctx->in_queue);
125
	free(hctx);
126
}
127

  
128
INIT_FUNC(mod_deflate_init) {
129
	plugin_data *p;
130
	
131
	p = calloc(1, sizeof(*p));
132

  
133
	p->tmp_buf = buffer_init();
134
	
135
	return p;
136
}
137

  
138
FREE_FUNC(mod_deflate_free) {
139
	plugin_data *p = p_d;
140
	
141
	UNUSED(srv);
142

  
143
	if (!p) return HANDLER_GO_ON;
144
	
145
	if (p->config_storage) {
146
		size_t i;
147
		for (i = 0; i < srv->config_context->used; i++) {
148
			plugin_config *s = p->config_storage[i];
149

  
150
			if (!s) continue;
151
			
152
			array_free(s->mimetypes);
153
			buffer_free(s->nocompress_url);
154
#if defined(HAVE_PCRE_H)
155
		       	if (s->nocompress_regex) pcre_free(s->nocompress_regex);
156
#endif
157
			free(s);
158
		}
159
		free(p->config_storage);
160
	}
161

  
162
	buffer_free(p->tmp_buf);
163
	
164
	free(p);
165
	
166
	return HANDLER_GO_ON;
167
}
168

  
169
SETDEFAULTS_FUNC(mod_deflate_setdefaults) {
170
	plugin_data *p = p_d;
171
	size_t i = 0;
172
	
173
	config_values_t cv[] = { 
174
		{ "deflate.output-buffer-size",    NULL, T_CONFIG_SHORT, T_CONFIG_SCOPE_CONNECTION },
175
		{ "deflate.mimetypes",             NULL, T_CONFIG_ARRAY, T_CONFIG_SCOPE_CONNECTION },
176
		{ "deflate.compression-level",     NULL, T_CONFIG_SHORT, T_CONFIG_SCOPE_CONNECTION },
177
		{ "deflate.mem-level",             NULL, T_CONFIG_SHORT, T_CONFIG_SCOPE_CONNECTION },
178
		{ "deflate.window-size",           NULL, T_CONFIG_SHORT, T_CONFIG_SCOPE_CONNECTION },
179
		{ "deflate.min-compress-size",     NULL, T_CONFIG_SHORT, T_CONFIG_SCOPE_CONNECTION },
180
		{ "deflate.work-block-size",       NULL, T_CONFIG_SHORT, T_CONFIG_SCOPE_CONNECTION },
181
		{ "deflate.enabled",               NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION },
182
		{ "deflate.debug",                 NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION },
183
		{ "deflate.bzip2",                 NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION },
184
		{ "deflate.sync-flush",            NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION },
185
		{ "deflate.nocompress-url",        NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_CONNECTION },
186
		{ NULL,                            NULL, T_CONFIG_UNSET, T_CONFIG_SCOPE_UNSET }
187
	};
188
	
189
	p->config_storage = calloc(1, srv->config_context->used * sizeof(specific_config *));
190
	
191
	for (i = 0; i < srv->config_context->used; i++) {
192
		plugin_config *s;
193
#if defined(HAVE_PCRE_H)
194
		const char *errptr;
195
		int erroff;
196
#endif
197
		
198
		s = calloc(1, sizeof(plugin_config));
199
		s->enabled = 1;
200
		s->bzip2 = 1;
201
		s->sync_flush = 0;
202
		s->debug = 0;
203
		s->output_buffer_size = 0;
204
		s->mem_level = 9;
205
		s->window_size = 15;
206
		s->min_compress_size = 0;
207
		s->work_block_size = 2048;
208
		s->compression_level = Z_DEFAULT_COMPRESSION;
209
		s->mimetypes = array_init();
210
		s->nocompress_url = buffer_init();
211
#if defined(HAVE_PCRE_H)
212
		s->nocompress_regex = NULL;
213
#endif
214

  
215
		cv[0].destination = &(s->output_buffer_size);
216
		cv[1].destination = s->mimetypes;
217
		cv[2].destination = &(s->compression_level);
218
		cv[3].destination = &(s->mem_level);
219
		cv[4].destination = &(s->window_size);
220
		cv[5].destination = &(s->min_compress_size);
221
		cv[6].destination = &(s->work_block_size);
222
		cv[7].destination = &(s->enabled);
223
		cv[8].destination = &(s->debug);
224
		cv[9].destination = &(s->bzip2);
225
		cv[10].destination = &(s->sync_flush);
226
		cv[11].destination = s->nocompress_url;
227
		
228
		p->config_storage[i] = s;
229
	
230
		if (0 != config_insert_values_global(srv, ((data_config *)srv->config_context->data[i])->value, cv)) {
231
			return HANDLER_ERROR;
232
		}
233

  
234
#if defined(HAVE_PCRE_H)		
235
		if (!buffer_is_empty(s->nocompress_url)) {
236
			if (NULL == (s->nocompress_regex = pcre_compile(s->nocompress_url->ptr,
237
								      0, &errptr, &erroff, NULL))) {
238
				
239
				log_error_write(srv, __FILE__, __LINE__, "sbss", 
240
						"compiling regex for nocompress-url failed:", 
241
						s->nocompress_url, "pos:", erroff);
242
				return HANDLER_ERROR;
243
			}
244
		}
245
#endif
246
		if((s->compression_level < 1 || s->compression_level > 9) &&
247
				s->compression_level != Z_DEFAULT_COMPRESSION) {
248
			log_error_write(srv, __FILE__, __LINE__, "sd", 
249
				"compression-level must be between 1 and 9:", s->compression_level);
250
			return HANDLER_ERROR;
251
		}
252

  
253
		if(s->mem_level < 1 || s->mem_level > 9) {
254
			log_error_write(srv, __FILE__, __LINE__, "sd", 
255
				"mem-level must be between 1 and 9:", s->mem_level);
256
			return HANDLER_ERROR;
257
		}
258

  
259
		if(s->window_size < 1 || s->window_size > 15) {
260
			log_error_write(srv, __FILE__, __LINE__, "sd", 
261
				"window-size must be between 1 and 15:", s->window_size);
262
			return HANDLER_ERROR;
263
		}
264
		s->window_size = 0 - s->window_size;
265

  
266
		if(s->sync_flush) {
267
			s->output_buffer_size = 0;
268
		}
269
	}
270
	
271
	return HANDLER_GO_ON;
272
	
273
}
274

  
275
#ifdef USE_ZLIB
276
/* Copied gzip_header from apache 2.2's mod_deflate.c */
277
/* RFC 1952 Section 2.3 defines the gzip header:
278
 *
279
 * +---+---+---+---+---+---+---+---+---+---+
280
 * |ID1|ID2|CM |FLG|     MTIME     |XFL|OS |
281
 * +---+---+---+---+---+---+---+---+---+---+
282
 */
283
static const char gzip_header[10] =
284
{ '\037', '\213', Z_DEFLATED, 0,
285
  0, 0, 0, 0, /* mtime */
286
  0, 0x03 /* Unix OS_CODE */
287
};
288
static int stream_deflate_init(server *srv, connection *con, handler_ctx *hctx) {
289
	plugin_data *p = hctx->plugin_data;
290
	z_stream *z;
291

  
292
	UNUSED(srv);
293
	UNUSED(con);
294

  
295
	z = &(hctx->z);
296
	z->zalloc = Z_NULL;
297
	z->zfree = Z_NULL;
298
	z->opaque = Z_NULL;
299
	z->total_in = 0;
300
	z->total_out = 0;
301
	z->next_out = NULL;
302
	z->avail_out = 0;
303
	
304
	if(p->conf.debug) {
305
		log_error_write(srv, __FILE__, __LINE__, "sd", 
306
			"output-buffer-size:", p->conf.output_buffer_size);
307
		log_error_write(srv, __FILE__, __LINE__, "sd", 
308
			"compression-level:", p->conf.compression_level);
309
		log_error_write(srv, __FILE__, __LINE__, "sd", 
310
			"mem-level:", p->conf.mem_level);
311
		log_error_write(srv, __FILE__, __LINE__, "sd", 
312
			"window-size:", p->conf.window_size);
313
		log_error_write(srv, __FILE__, __LINE__, "sd", 
314
			"min-compress-size:", p->conf.min_compress_size);
315
		log_error_write(srv, __FILE__, __LINE__, "sd", 
316
			"work-block-size:", p->conf.work_block_size);
317
	}
318
	if (Z_OK != deflateInit2(z, 
319
				 p->conf.compression_level,
320
				 Z_DEFLATED, 
321
				 p->conf.window_size,  /* supress zlib-header */
322
				 p->conf.mem_level,
323
				 Z_DEFAULT_STRATEGY)) {
324
		return -1;
325
	}
326
	hctx->stream_open = 1;
327
		
328
	return 0;
329
}
330

  
331
static int stream_deflate_compress(server *srv, connection *con, handler_ctx *hctx, unsigned char *start, off_t st_size) {
332
	plugin_data *p = hctx->plugin_data;
333
	z_stream *z;
334
	int len;
335
	int in = 0, out = 0;
336

  
337
	UNUSED(srv);
338
	z = &(hctx->z);
339

  
340
	if(z->next_out == NULL) {
341
		z->next_out = (unsigned char *)hctx->output->ptr;
342
		z->avail_out = hctx->output->size;
343
	}
344
	
345
	if(hctx->compression_type == HTTP_ACCEPT_ENCODING_GZIP) {
346
		if(hctx->gzip_header == 0) {
347
			hctx->gzip_header = 1;
348
			/* copy gzip header into output buffer */
349
			buffer_copy_memory(hctx->output, gzip_header, sizeof(gzip_header));
350
			if(p->conf.debug) {
351
				log_error_write(srv, __FILE__, __LINE__, "sd",
352
						"gzip_header len=", sizeof(gzip_header));
353
			}
354
			/* initialize crc32 */
355
			hctx->crc = crc32(0L, Z_NULL, 0);
356
			z->next_out = (unsigned char *)(hctx->output->ptr + sizeof(gzip_header));
357
			z->avail_out = hctx->output->size - sizeof(gzip_header);
358
		}
359
		hctx->crc = crc32(hctx->crc, start, st_size);
360
	}
361

  
362
	z->next_in = start;
363
	z->avail_in = st_size;
364
	hctx->bytes_in += st_size;
365
		
366
	/* compress data */
367
	in = z->avail_in;
368
	do {
369
		if (Z_OK != deflate(z, Z_NO_FLUSH)) {
370
			deflateEnd(z);
371
			hctx->stream_open = 0;
372
			return -1;
373
		}
374

  
375
		if(z->avail_out == 0 || z->avail_in > 0) {
376
			len = hctx->output->size - z->avail_out;
377
			hctx->bytes_out += len;
378
			out += len;
379
			chunkqueue_append_mem(con->write_queue, hctx->output->ptr, len+1);
380
			z->next_out = (unsigned char *)hctx->output->ptr;
381
			z->avail_out = hctx->output->size;
382
		}
383
	} while (z->avail_in > 0);
384

  
385
	if(p->conf.debug) {
386
		log_error_write(srv, __FILE__, __LINE__, "sdsd",
387
				"compress: in=", in, ", out=", out);
388
	}
389
	return 0;
390
}
391

  
392
static int stream_deflate_flush(server *srv, connection *con, handler_ctx *hctx, int end) {
393
	plugin_data *p = hctx->plugin_data;
394
	z_stream *z;
395
	int len;
396
	int rc = 0;
397
	int done;
398
	int flush = 1;
399
	int in = 0, out = 0;
400

  
401
	UNUSED(srv);
402

  
403
	z = &(hctx->z);
404

  
405
	if(z->next_out == NULL) {
406
		z->next_out = (unsigned char *)hctx->output->ptr;
407
		z->avail_out = hctx->output->size;
408
	}
409
	/* compress data */
410
	in = z->avail_in;
411
	do {
412
		done = 1;
413
		if(end) {
414
			rc = deflate(z, Z_FINISH);
415
			if (rc == Z_OK) {
416
				done = 0;
417
			} else if (rc != Z_STREAM_END) {
418
				deflateEnd(z);
419
				hctx->stream_open = 0;
420
				return -1;
421
			}
422
		} else {
423
			if(p->conf.sync_flush) {
424
				rc = deflate(z, Z_SYNC_FLUSH);
425
			} else if(z->avail_in > 0) {
426
				if(p->conf.output_buffer_size > 0) flush = 0;
427
				rc = deflate(z, Z_NO_FLUSH);
428
			} else {
429
				if(p->conf.output_buffer_size > 0) flush = 0;
430
				rc = Z_OK;
431
			}
432
			if (rc != Z_OK) {
433
				deflateEnd(z);
434
				hctx->stream_open = 0;
435
				return -1;
436
			}
437
		}
438

  
439
		len = hctx->output->size - z->avail_out;
440
		if(z->avail_out == 0 || (flush && len > 0)) {
441
			hctx->bytes_out += len;
442
			out += len;
443
			chunkqueue_append_mem(con->write_queue, hctx->output->ptr, len+1);
444
			z->next_out = (unsigned char *)hctx->output->ptr;
445
			z->avail_out = hctx->output->size;
446
		}
447
	} while (z->avail_in != 0 || !done);
448

  
449

  
450
	if(p->conf.debug) {
451
		log_error_write(srv, __FILE__, __LINE__, "sdsd",
452
				"flush: in=", in, ", out=", out);
453
	}
454
	if(p->conf.sync_flush) {
455
		z->next_out = NULL;
456
		z->avail_out = 0;
457
	}
458
	return 0;
459
}
460

  
461
static int stream_deflate_end(server *srv, connection *con, handler_ctx *hctx) {
462
	plugin_data *p = hctx->plugin_data;
463
	z_stream *z;
464
	int rc;
465

  
466
	UNUSED(srv);
467

  
468
	z = &(hctx->z);
469
	if(!hctx->stream_open) return 0;
470
	hctx->stream_open = 0;
471

  
472
	if(hctx->compression_type == HTTP_ACCEPT_ENCODING_GZIP && hctx->bytes_out > 0 &&
473
			(unsigned int )hctx->bytes_out >= sizeof(gzip_header)) {
474
		/* write gzip footer */
475
		unsigned char c[8];
476

  
477
		c[0] = (hctx->crc >>  0) & 0xff;
478
		c[1] = (hctx->crc >>  8) & 0xff;
479
		c[2] = (hctx->crc >> 16) & 0xff;
480
		c[3] = (hctx->crc >> 24) & 0xff;
481
		c[4] = (z->total_in >>  0) & 0xff;
482
		c[5] = (z->total_in >>  8) & 0xff;
483
		c[6] = (z->total_in >> 16) & 0xff;
484
		c[7] = (z->total_in >> 24) & 0xff;
485
		/* append footer to write_queue */
486
		chunkqueue_append_mem(con->write_queue, (char *)c, 9);
487
		hctx->bytes_out += 8;
488
		if(p->conf.debug) {
489
			log_error_write(srv, __FILE__, __LINE__, "sd",
490
					"gzip_footer len=", 8);
491
		}
492
	}
493

  
494
	if ((rc = deflateEnd(z)) != Z_OK) {
495
		if(rc == Z_DATA_ERROR) return 0;
496
		if(z->msg != NULL) {
497
			log_error_write(srv, __FILE__, __LINE__, "sdss",
498
					"deflateEnd error ret=", rc, ", msg=", z->msg);
499
		} else {
500
			log_error_write(srv, __FILE__, __LINE__, "sd",
501
					"deflateEnd error ret=", rc);
502
		}
503
		return -1;
504
	}
505
	return 0;
506
}
507

  
508
#endif
509

  
510
#ifdef USE_BZ2LIB
511
static int stream_bzip2_init(server *srv, connection *con, handler_ctx *hctx) {
512
	plugin_data *p = hctx->plugin_data;
513
	bz_stream *bz;
514

  
515
	UNUSED(srv);
516
	UNUSED(con);
517

  
518
	bz = &(hctx->bz);
519
	bz->bzalloc = NULL;
520
	bz->bzfree = NULL;
521
	bz->opaque = NULL;
522
	bz->total_in_lo32 = 0;
523
	bz->total_in_hi32 = 0;
524
	bz->total_out_lo32 = 0;
525
	bz->total_out_hi32 = 0;
526
	
527
	if(p->conf.debug) {
528
		log_error_write(srv, __FILE__, __LINE__, "sd", 
529
			"output-buffer-size:", p->conf.output_buffer_size);
530
		log_error_write(srv, __FILE__, __LINE__, "sd", 
531
			"compression-level:", p->conf.compression_level);
532
		log_error_write(srv, __FILE__, __LINE__, "sd", 
533
			"mem-level:", p->conf.mem_level);
534
		log_error_write(srv, __FILE__, __LINE__, "sd", 
535
			"window-size:", p->conf.window_size);
536
		log_error_write(srv, __FILE__, __LINE__, "sd", 
537
			"min-compress-size:", p->conf.min_compress_size);
538
		log_error_write(srv, __FILE__, __LINE__, "sd", 
539
			"work-block-size:", p->conf.work_block_size);
540
	}
541
	if (BZ_OK != BZ2_bzCompressInit(bz, 
542
					p->conf.compression_level, /* blocksize = 900k */
543
					0, /* no output */
544
					30)) { /* workFactor: default */
545
		return -1;
546
	}
547
	hctx->stream_open = 1;
548
		
549
	return 0;
550
}
551

  
552
static int stream_bzip2_compress(server *srv, connection *con, handler_ctx *hctx, unsigned char *start, off_t st_size) {
553
	plugin_data *p = hctx->plugin_data;
554
	bz_stream *bz;
555
	int len;
556
	int rc;
557
	int in = 0, out = 0;
558

  
559
	UNUSED(srv);
560

  
561
	bz = &(hctx->bz);
562

  
563
	if(bz->next_out == NULL) {
564
		bz->next_out = hctx->output->ptr;
565
		bz->avail_out = hctx->output->size;
566
	}
567
	
568
	bz->next_in = (char *)start;
569
	bz->avail_in = st_size;
570
	hctx->bytes_in += st_size;
571
		
572
	/* compress data */
573
	in = bz->avail_in;
574
	do {
575
		rc = BZ2_bzCompress(bz, BZ_RUN);
576
		if (rc != BZ_RUN_OK) {
577
			BZ2_bzCompressEnd(bz);
578
			hctx->stream_open = 0;
579
			return -1;
580
		}
581

  
582
		if(bz->avail_out == 0 || bz->avail_in > 0) {
583
			len = hctx->output->size - bz->avail_out;
584
			hctx->bytes_out += len;
585
			out += len;
586
			chunkqueue_append_mem(con->write_queue, hctx->output->ptr, len+1);
587
			bz->next_out = hctx->output->ptr;
588
			bz->avail_out = hctx->output->size;
589
		}
590
	} while (bz->avail_in > 0);
591
	if(p->conf.debug) {
592
		log_error_write(srv, __FILE__, __LINE__, "sdsd",
593
				"compress: in=", in, ", out=", out);
594
	}
595
	return 0;
596
}
597

  
598
static int stream_bzip2_flush(server *srv, connection *con, handler_ctx *hctx, int end) {
599
	plugin_data *p = hctx->plugin_data;
600
	bz_stream *bz;
... This diff was truncated because it exceeds the maximum size that can be displayed.