comparison src/http/modules/ngx_http_uwsgi_module.c @ 3546:3a9fca8cbdfc

simplify binary little endian length processing
author Igor Sysoev <igor@sysoev.ru>
date Tue, 01 Jun 2010 17:04:56 +0000
parents 11c4f3a1e439
children c2da61796410
comparison
equal deleted inserted replaced
3545:11c4f3a1e439 3546:3a9fca8cbdfc
61 61
62 62
63 #define NGX_HTTP_UWSGI_PARSE_NO_HEADER 20 63 #define NGX_HTTP_UWSGI_PARSE_NO_HEADER 20
64 64
65 65
66 #ifndef NGX_HAVE_LITTLE_ENDIAN
67 static uint16_t
68 uwsgi_swap16(uint16_t x)
69 {
70 return (uint16_t) ((x & 0xff) << 8 | (x & 0xff00) >> 8);
71 }
72 #endif
73
74
75
76 static ngx_int_t ngx_http_uwsgi_eval(ngx_http_request_t *r, 66 static ngx_int_t ngx_http_uwsgi_eval(ngx_http_request_t *r,
77 ngx_http_uwsgi_loc_conf_t *uwcf); 67 ngx_http_uwsgi_loc_conf_t *uwcf);
78 static ngx_int_t ngx_http_uwsgi_create_request(ngx_http_request_t *r); 68 static ngx_int_t ngx_http_uwsgi_create_request(ngx_http_request_t *r);
79 static ngx_int_t ngx_http_uwsgi_reinit_request(ngx_http_request_t *r); 69 static ngx_int_t ngx_http_uwsgi_reinit_request(ngx_http_request_t *r);
80 static ngx_int_t ngx_http_uwsgi_process_status_line(ngx_http_request_t *r); 70 static ngx_int_t ngx_http_uwsgi_process_status_line(ngx_http_request_t *r);
460 450
461 static ngx_int_t 451 static ngx_int_t
462 ngx_http_uwsgi_create_request(ngx_http_request_t *r) 452 ngx_http_uwsgi_create_request(ngx_http_request_t *r)
463 { 453 {
464 u_char ch, *pos, tmp[2]; 454 u_char ch, *pos, tmp[2];
465 size_t key_len, val_len; 455 size_t key_len, val_len, len;
466 uint16_t uwsgi_pkt_size, uwsgi_strlen;
467 ngx_uint_t i, n; 456 ngx_uint_t i, n;
468 ngx_buf_t *b; 457 ngx_buf_t *b;
469 ngx_chain_t *cl, *body; 458 ngx_chain_t *cl, *body;
470 ngx_list_part_t *part; 459 ngx_list_part_t *part;
471 ngx_table_elt_t *header; 460 ngx_table_elt_t *header;
472 ngx_http_script_code_pt code; 461 ngx_http_script_code_pt code;
473 ngx_http_script_engine_t e, le; 462 ngx_http_script_engine_t e, le;
474 ngx_http_uwsgi_loc_conf_t *uwcf; 463 ngx_http_uwsgi_loc_conf_t *uwcf;
475 ngx_http_script_len_code_pt lcode; 464 ngx_http_script_len_code_pt lcode;
476 465
477 uwsgi_pkt_size = 0; 466 len = 0;
478 467
479 uwcf = ngx_http_get_module_loc_conf(r, ngx_http_uwsgi_module); 468 uwcf = ngx_http_get_module_loc_conf(r, ngx_http_uwsgi_module);
480 469
481 if (uwcf->params_len) { 470 if (uwcf->params_len) {
482 ngx_memzero(&le, sizeof(ngx_http_script_engine_t)); 471 ngx_memzero(&le, sizeof(ngx_http_script_engine_t));
495 for (val_len = 0; *(uintptr_t *) le.ip; val_len += lcode (&le)) { 484 for (val_len = 0; *(uintptr_t *) le.ip; val_len += lcode (&le)) {
496 lcode = *(ngx_http_script_len_code_pt *) le.ip; 485 lcode = *(ngx_http_script_len_code_pt *) le.ip;
497 } 486 }
498 le.ip += sizeof(uintptr_t); 487 le.ip += sizeof(uintptr_t);
499 488
500 uwsgi_pkt_size = (uint16_t) 489 len += 2 + key_len + 2 + val_len;
501 (uwsgi_pkt_size + 2 + key_len + 2 + val_len);
502 } 490 }
503 } 491 }
504 492
505 if (uwcf->upstream.pass_request_headers) { 493 if (uwcf->upstream.pass_request_headers) {
506 494
517 part = part->next; 505 part = part->next;
518 header = part->elts; 506 header = part->elts;
519 i = 0; 507 i = 0;
520 } 508 }
521 509
522 uwsgi_pkt_size = (uint16_t) (uwsgi_pkt_size 510 len += 2 + 5 + header[i].key.len + 2 + header[i].value.len;
523 + 2 + 5 + header[i].key.len + 2 + header[i].value.len);
524 } 511 }
525 } 512 }
526 513
527 if (uwcf->uwsgi_string.data && uwcf->uwsgi_string.len) { 514 if (uwcf->uwsgi_string.data && uwcf->uwsgi_string.len) {
528 uwsgi_pkt_size = (uint16_t) (uwsgi_pkt_size + uwcf->uwsgi_string.len); 515 len += uwcf->uwsgi_string.len;
529 } 516 }
530 517
531 #if 0 518 #if 0
532 /* allow custom uwsgi packet */ 519 /* allow custom uwsgi packet */
533 if (uwsgi_pkt_size > 0 && uwsgi_pkt_size < 2) { 520 if (len > 0 && len < 2) {
534 ngx_log_error (NGX_LOG_ALERT, r->connection->log, 0, 521 ngx_log_error (NGX_LOG_ALERT, r->connection->log, 0,
535 "uwsgi request is too little: %uz", uwsgi_pkt_size); 522 "uwsgi request is too little: %uz", len);
536 return NGX_ERROR; 523 return NGX_ERROR;
537 } 524 }
538 #endif 525 #endif
539 526
540 b = ngx_create_temp_buf(r->pool, uwsgi_pkt_size + 4); 527 b = ngx_create_temp_buf(r->pool, len + 4);
541 if (b == NULL) { 528 if (b == NULL) {
542 return NGX_ERROR; 529 return NGX_ERROR;
543 } 530 }
544 531
545 cl = ngx_alloc_chain_link(r->pool); 532 cl = ngx_alloc_chain_link(r->pool);
547 return NGX_ERROR; 534 return NGX_ERROR;
548 } 535 }
549 536
550 cl->buf = b; 537 cl->buf = b;
551 538
552 *b->pos = uwcf->modifier1; 539 *b->last++ = uwcf->modifier1;
553 #ifndef NGX_HAVE_LITTLE_ENDIAN 540 *b->last++ = (u_char) (len & 0xff);
554 uwsgi_pkt_size = uwsgi_swap16(uwsgi_pkt_size); 541 *b->last++ = (u_char) ((len >> 8) & 0xff);
555 #endif 542 *b->last++ = uwcf->modifier2;
556 b->last = ngx_cpymem(b->pos + 1, &uwsgi_pkt_size, 2);
557 *(b->pos + 3) = uwcf->modifier2;
558 b->last++;
559 543
560 if (uwcf->params_len) { 544 if (uwcf->params_len) {
561 ngx_memzero(&e, sizeof(ngx_http_script_engine_t)); 545 ngx_memzero(&e, sizeof(ngx_http_script_engine_t));
562 546
563 e.ip = uwcf->params->elts; 547 e.ip = uwcf->params->elts;
575 for (val_len = 0; *(uintptr_t *) le.ip; val_len += lcode(&le)) { 559 for (val_len = 0; *(uintptr_t *) le.ip; val_len += lcode(&le)) {
576 lcode = *(ngx_http_script_len_code_pt *) le.ip; 560 lcode = *(ngx_http_script_len_code_pt *) le.ip;
577 } 561 }
578 le.ip += sizeof(uintptr_t); 562 le.ip += sizeof(uintptr_t);
579 563
580 #ifndef NGX_HAVE_LITTLE_ENDIAN 564 *e.pos++ = (u_char) (key_len & 0xff);
581 uwsgi_strlen = uwsgi_swap16(key_len); 565 *e.pos++ = (u_char) ((key_len >> 8) & 0xff);
582 #else
583 uwsgi_strlen = (uint16_t) key_len;
584 #endif
585 e.pos = ngx_cpymem(e.pos, &uwsgi_strlen, 2);
586 566
587 while (*(uintptr_t *) e.ip) { 567 while (*(uintptr_t *) e.ip) {
588 code = *(ngx_http_script_code_pt *) e.ip; 568 code = *(ngx_http_script_code_pt *) e.ip;
589 code((ngx_http_script_engine_t *) & e); 569 code((ngx_http_script_engine_t *) & e);
590 } 570 }
597 e.pos = ngx_cpymem(pos, tmp, 2); 577 e.pos = ngx_cpymem(pos, tmp, 2);
598 } 578 }
599 579
600 e.pos = pos; 580 e.pos = pos;
601 581
602 #ifndef NGX_HAVE_LITTLE_ENDIAN 582 *e.pos++ = (u_char) (val_len & 0xff);
603 uwsgi_strlen = uwsgi_swap16(val_len); 583 *e.pos++ = (u_char) ((val_len >> 8) & 0xff);
604 #else
605 uwsgi_strlen = (uint16_t) val_len;
606 #endif
607 e.pos = ngx_cpymem(e.pos, &uwsgi_strlen, 2);
608 e.pos += val_len; 584 e.pos += val_len;
609 585
610 e.ip += sizeof(uintptr_t); 586 e.ip += sizeof(uintptr_t);
611 } 587 }
612 588
628 part = part->next; 604 part = part->next;
629 header = part->elts; 605 header = part->elts;
630 i = 0; 606 i = 0;
631 } 607 }
632 608
633 #ifndef NGX_HAVE_LITTLE_ENDIAN 609 key_len = 5 + header[i].key.len;
634 uwsgi_strlen = uwsgi_swap16(5 + header[i].key.len); 610 *b->last++ = (u_char) (key_len & 0xff);
635 #else 611 *b->last++ = (u_char) ((key_len >> 8) & 0xff);
636 uwsgi_strlen = (uint16_t) (5 + header[i].key.len); 612
637 #endif
638 b->last = ngx_cpymem(b->last, &uwsgi_strlen, 2);
639 b->last = ngx_cpymem(b->last, "HTTP_", 5); 613 b->last = ngx_cpymem(b->last, "HTTP_", 5);
640 for (n = 0; n < header[i].key.len; n++) { 614 for (n = 0; n < header[i].key.len; n++) {
641 ch = header[i].key.data[n]; 615 ch = header[i].key.data[n];
642 616
643 if (ch >= 'a' && ch <= 'z') { 617 if (ch >= 'a' && ch <= 'z') {
647 ch = '_'; 621 ch = '_';
648 } 622 }
649 623
650 *b->last++ = ch; 624 *b->last++ = ch;
651 } 625 }
652 #ifndef NGX_HAVE_LITTLE_ENDIAN 626
653 uwsgi_strlen = uwsgi_swap16 (header[i].value.len); 627 val_len = header[i].value.len;
654 #else 628 *b->last++ = (u_char) (val_len & 0xff);
655 uwsgi_strlen = (uint16_t) header[i].value.len; 629 *b->last++ = (u_char) ((val_len >> 8) & 0xff);
656 #endif 630 b->last = ngx_copy(b->last, header[i].value.data, val_len);
657 b->last = ngx_cpymem(b->last, &uwsgi_strlen, 2);
658 b->last =
659 ngx_copy(b->last, header[i].value.data, header[i].value.len);
660 } 631 }
661 } 632 }
662 633
663 if (uwcf->uwsgi_string.data && uwcf->uwsgi_string.len) { 634 if (uwcf->uwsgi_string.data && uwcf->uwsgi_string.len) {
664 b->last = ngx_copy(b->last, uwcf->uwsgi_string.data, 635 b->last = ngx_copy(b->last, uwcf->uwsgi_string.data,