forked from phusion/passenger
/
ContentHandler.c
1442 lines (1171 loc) · 48.4 KB
/
ContentHandler.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) Igor Sysoev
* Copyright (C) 2007 Manlio Perillo (manlio.perillo@gmail.com)
* Copyright (C) 2010 Phusion
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <nginx.h>
#include "ngx_http_passenger_module.h"
#include "ContentHandler.h"
#include "StaticContentHandler.h"
#include "Configuration.h"
#include "../common/Constants.h"
#define NGX_HTTP_SCGI_PARSE_NO_HEADER 20
typedef enum {
FT_ERROR,
FT_FILE,
FT_DIRECTORY,
FT_OTHER
} FileType;
static ngx_int_t reinit_request(ngx_http_request_t *r);
static ngx_int_t process_status_line(ngx_http_request_t *r);
static ngx_int_t parse_status_line(ngx_http_request_t *r,
passenger_context_t *context);
static ngx_int_t process_header(ngx_http_request_t *r);
static void abort_request(ngx_http_request_t *r);
static void finalize_request(ngx_http_request_t *r, ngx_int_t rc);
static void
uint_to_str(ngx_uint_t i, u_char *str, ngx_uint_t size) {
ngx_memzero(str, size);
ngx_snprintf(str, size, "%ui", i);
}
static FileType
get_file_type(const u_char *filename, unsigned int throttle_rate) {
struct stat buf;
int ret;
ret = cached_file_stat_perform(passenger_stat_cache,
(const char *) filename,
&buf,
throttle_rate);
if (ret == 0) {
if (S_ISREG(buf.st_mode)) {
return FT_FILE;
} else if (S_ISDIR(buf.st_mode)) {
return FT_DIRECTORY;
} else {
return FT_OTHER;
}
} else {
return FT_ERROR;
}
}
static int
file_exists(const u_char *filename, unsigned int throttle_rate) {
return get_file_type(filename, throttle_rate) == FT_FILE;
}
static passenger_app_type_t
detect_application_type(const ngx_str_t *public_dir) {
u_char filename[NGX_MAX_PATH];
ngx_memzero(filename, sizeof(filename));
ngx_snprintf(filename, sizeof(filename), "%s/%s",
public_dir->data, "../config.ru");
if (file_exists(filename, 1)) {
return AP_RACK;
}
ngx_memzero(filename, sizeof(filename));
ngx_snprintf(filename, sizeof(filename), "%s/%s",
public_dir->data, "../config/environment.rb");
if (file_exists(filename, 1)) {
return AP_RAILS;
}
ngx_memzero(filename, sizeof(filename));
ngx_snprintf(filename, sizeof(filename), "%s/%s",
public_dir->data, "../passenger_wsgi.py");
if (file_exists(filename, 1)) {
return AP_WSGI;
}
return AP_NONE;
}
/**
* Maps the URI for the given request to a page cache file, if possible.
*
* @return Whether the URI has been successfully mapped to a page cache file.
* @param r The corresponding request.
* @param public_dir The web application's 'public' directory.
* @param filename The filename that the URI normally maps to.
* @param filename_len The length of the <tt>filename</tt> string.
* @param root The size of the root path in <tt>filename</tt>.
* @param page_cache_file If mapping was successful, then the page cache
* file's filename will be stored in here.
* <tt>page_cache_file.data</tt> must already point to
* a buffer, and <tt>page_cache_file.len</tt> must be set
* to the size of this buffer, including terminating NUL.
*/
static int
map_uri_to_page_cache_file(ngx_http_request_t *r, ngx_str_t *public_dir,
const u_char *filename, size_t filename_len,
ngx_str_t *page_cache_file)
{
u_char *end;
if ((r->method != NGX_HTTP_GET && r->method != NGX_HTTP_HEAD) || filename_len == 0) {
return 0;
}
/* From this point on we know that filename is not an empty string. */
/* Check whether filename is equal to public_dir. filename may also be equal to
* public_dir + "/" so check for that as well.
*/
if ((public_dir->len == filename_len && memcmp(public_dir->data, filename,
filename_len) == 0) ||
(public_dir->len == filename_len - 1 &&
filename[filename_len - 1] == '/' &&
memcmp(public_dir->data, filename, filename_len - 1) == 0)
) {
/* If the URI maps to the 'public' directory (i.e. the request is the
* base URI) then index.html is the page cache file.
*/
if (filename_len + sizeof("/index.html") > page_cache_file->len) {
/* Page cache filename doesn't fit in the buffer. */
return 0;
}
end = ngx_copy(page_cache_file->data, filename, filename_len);
if (filename[filename_len - 1] != '/') {
end = ngx_copy(end, "/", 1);
}
end = ngx_copy(end, "index.html", sizeof("index.html"));
} else {
/* Otherwise, the page cache file is just filename + ".html". */
if (filename_len + sizeof(".html") > page_cache_file->len) {
/* Page cache filename doesn't fit in the buffer. */
return 0;
}
end = ngx_copy(page_cache_file->data, filename, filename_len);
end = ngx_copy(end, ".html", sizeof(".html"));
}
if (file_exists(page_cache_file->data, 0)) {
page_cache_file->len = end - page_cache_file->data - 1;
return 1;
} else {
return 0;
}
}
static int
find_base_uri(ngx_http_request_t *r, const passenger_loc_conf_t *loc,
ngx_str_t *found_base_uri)
{
ngx_uint_t i;
ngx_str_t *base_uris, *base_uri, *uri;
if (loc->base_uris == NGX_CONF_UNSET_PTR) {
return 0;
} else {
base_uris = (ngx_str_t *) loc->base_uris->elts;
for (i = 0; i < loc->base_uris->nelts; i++) {
base_uri = &base_uris[i];
uri = &r->uri;
if (uri->len == 1 && uri->data[0] == '/') {
/* Ignore 'passenger_base_uri /' options. Users usually
* specify this out of ignorance.
*/
continue;
}
if (( uri->len == base_uri->len
&& ngx_strncmp(uri->data, base_uri->data, uri->len) == 0 )
|| ( uri->len > base_uri->len
&& ngx_strncmp(uri->data, base_uri->data, base_uri->len) == 0
&& uri->data[base_uri->len] == (u_char) '/' )) {
*found_base_uri = base_uris[i];
return 1;
}
}
return 0;
}
}
static void
set_upstream_server_address(ngx_http_upstream_t *upstream, ngx_http_upstream_conf_t *upstream_config) {
ngx_http_upstream_server_t *servers = upstream_config->upstream->servers->elts;
#if NGINX_VERSION_NUM >= 8000
ngx_addr_t *address = &servers[0].addrs[0];
#else
ngx_peer_addr_t *address = &servers[0].addrs[0];
#endif
const char *request_socket_filename;
unsigned int request_socket_filename_len;
struct sockaddr_un *sockaddr;
/* The Nginx API makes it extremely difficult to register an upstream server
* address outside of the configuration loading phase. However we don't know
* the helper agent's request socket filename until we're done with loading
* the configuration. So during configuration loading we register a placeholder
* address for the upstream configuration, and while processing requests
* we substitute the placeholder filename with the real helper agent request
* socket filename.
*/
if (address->name.data == passenger_placeholder_upstream_address.data) {
sockaddr = (struct sockaddr_un *) address->sockaddr;
request_socket_filename =
agents_starter_get_request_socket_filename(passenger_agents_starter,
&request_socket_filename_len);
address->name.data = (u_char *) request_socket_filename;
address->name.len = request_socket_filename_len;
strncpy(sockaddr->sun_path, request_socket_filename, sizeof(sockaddr->sun_path));
sockaddr->sun_path[sizeof(sockaddr->sun_path) - 1] = '\0';
}
}
/* Convenience macros for building the SCGI header in create_request(). */
#define ANALYZE_BOOLEAN_CONFIG_LENGTH(name, container, config_field) \
do { \
if (container->config_field) { \
len += sizeof(name) + sizeof("true"); \
} else { \
len += sizeof(name) + sizeof("false"); \
} \
} while (0)
#define SERIALIZE_BOOLEAN_CONFIG_DATA(name, container, config_field) \
do { \
b->last = ngx_copy(b->last, name, sizeof(name)); \
if (container->config_field) { \
b->last = ngx_copy(b->last, "true", sizeof("true")); \
} else { \
b->last = ngx_copy(b->last, "false", sizeof("false")); \
} \
} while (0)
#define PREPARE_INT_CONFIG_DATA(name, container, config_field) \
do { \
end = ngx_snprintf(config_field ## _string, \
sizeof(config_field ## _string) - 1, \
"%d", \
container->config_field); \
*end = '\0'; \
len += sizeof(name) + (end - config_field ## _string) + 1; \
} while (0)
#define SERIALIZE_INT_CONFIG_DATA(name, container, config_field) \
do { \
b->last = ngx_copy(b->last, name, sizeof(name)); \
b->last = ngx_copy(b->last, config_field ## _string, \
ngx_strlen(config_field ## _string) + 1); \
} while (0)
#define ANALYZE_STR_CONFIG_LENGTH(name, container, config_field) \
do { \
if (container->config_field.data != NULL) { \
len += sizeof(name) + (container->config_field.len) + 1; \
} \
} while (0)
#define SERIALIZE_STR_CONFIG_DATA(name, container, config_field) \
do { \
if (container->config_field.data != NULL) { \
b->last = ngx_copy(b->last, name, sizeof(name)); \
b->last = ngx_copy(b->last, container->config_field.data, \
container->config_field.len); \
*b->last = '\0'; \
b->last++; \
} \
} while (0)
static ngx_int_t
create_request(ngx_http_request_t *r)
{
u_char ch;
const char * helper_agent_request_socket_password_data;
unsigned int helper_agent_request_socket_password_len;
u_char buf[sizeof("4294967296")];
size_t len, size, key_len, val_len, content_length;
const u_char *app_type_string;
size_t app_type_string_len;
int server_name_len;
ngx_str_t escaped_uri;
ngx_str_t *union_station_filters = NULL;
u_char min_instances_string[12];
u_char framework_spawner_idle_time_string[12];
u_char app_spawner_idle_time_string[12];
u_char *end;
void *tmp;
ngx_uint_t i, n;
ngx_buf_t *b;
ngx_chain_t *cl, *body;
ngx_list_part_t *part;
ngx_table_elt_t *header;
ngx_http_script_code_pt code;
ngx_http_script_engine_t e, le;
ngx_http_core_srv_conf_t *cscf;
passenger_loc_conf_t *slcf;
passenger_main_conf_t *main_conf;
passenger_context_t *context;
ngx_http_script_len_code_pt lcode;
#if (NGX_HTTP_SSL)
ngx_http_ssl_srv_conf_t *ssl_conf;
#endif
cscf = ngx_http_get_module_srv_conf(r, ngx_http_core_module);
slcf = ngx_http_get_module_loc_conf(r, ngx_http_passenger_module);
main_conf = &passenger_main_conf;
context = ngx_http_get_module_ctx(r, ngx_http_passenger_module);
if (context == NULL) {
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
switch (context->app_type) {
case AP_RAILS:
app_type_string = (const u_char *) "rails";
app_type_string_len = sizeof("rails");
break;
case AP_RACK:
app_type_string = (const u_char *) "rack";
app_type_string_len = sizeof("rack");
break;
case AP_WSGI:
app_type_string = (const u_char *) "wsgi";
app_type_string_len = sizeof("wsgi");
break;
default:
app_type_string = (const u_char *) "rails";
app_type_string_len = sizeof("rails");
break;
}
/*
* Nginx unescapes URI's before passing them to Phusion Passenger,
* but backend processes expect the escaped version.
* http://code.google.com/p/phusion-passenger/issues/detail?id=404
*/
escaped_uri.len =
2 * ngx_escape_uri(NULL, r->uri.data, r->uri.len, NGX_ESCAPE_URI)
+ r->uri.len;
escaped_uri.data = ngx_pnalloc(r->pool, escaped_uri.len + 1);
escaped_uri.data[escaped_uri.len] = '\0';
ngx_escape_uri(escaped_uri.data, r->uri.data, r->uri.len, NGX_ESCAPE_URI);
/**************************************************
* Determine the request header length.
**************************************************/
/* Length of the Content-Length header. */
if (r->headers_in.content_length_n < 0) {
content_length = 0;
} else {
content_length = r->headers_in.content_length_n;
}
uint_to_str(content_length, buf, sizeof(buf));
/* +1 for trailing null */
len = sizeof("CONTENT_LENGTH") + ngx_strlen(buf) + 1;
/* DOCUMENT_ROOT, SCRIPT_NAME, RAILS_RELATIVE_URL_ROOT, PATH_INFO and REQUEST_URI. */
len += sizeof("DOCUMENT_ROOT") + context->public_dir.len + 1;
if (context->base_uri.len > 0) {
len += sizeof("SCRIPT_NAME") + context->base_uri.len + 1;
len += sizeof("RAILS_RELATIVE_URL_ROOT") +
context->base_uri.len + 1;
len += sizeof("PATH_INFO") + escaped_uri.len - context->base_uri.len + 1;
} else {
len += sizeof("SCRIPT_NAME") + sizeof("");
len += sizeof("PATH_INFO") + escaped_uri.len + 1;
}
len += sizeof("REQUEST_URI") + escaped_uri.len + 1;
if (r->args.len > 0) {
len += 1 + r->args.len;
}
/* SERVER_NAME; must be equal to HTTP_HOST without the port part */
if (r->headers_in.host != NULL) {
tmp = memchr(r->headers_in.host->value.data, ':', r->headers_in.host->value.len);
if (tmp == NULL) {
server_name_len = r->headers_in.host->value.len;
} else {
server_name_len = (int) ((const u_char *) tmp - r->headers_in.host->value.data);
}
} else {
server_name_len = cscf->server_name.len;
}
len += sizeof("SERVER_NAME") + server_name_len + 1;
/* Various other HTTP headers. */
if (r->headers_in.content_type != NULL
&& r->headers_in.content_type->value.len > 0) {
len += sizeof("CONTENT_TYPE") + r->headers_in.content_type->value.len + 1;
}
#if (NGX_HTTP_SSL)
ssl_conf = ngx_http_get_module_srv_conf(r, ngx_http_ssl_module);
if (ssl_conf->enable) {
len += sizeof("HTTPS") + sizeof("on");
}
#endif
/* Lengths of Passenger application pool options. */
ANALYZE_BOOLEAN_CONFIG_LENGTH("PASSENGER_USE_GLOBAL_QUEUE",
slcf, use_global_queue);
ANALYZE_BOOLEAN_CONFIG_LENGTH("PASSENGER_FRIENDLY_ERROR_PAGES",
slcf, friendly_error_pages);
ANALYZE_BOOLEAN_CONFIG_LENGTH("UNION_STATION_SUPPORT",
slcf, union_station_support);
ANALYZE_BOOLEAN_CONFIG_LENGTH("PASSENGER_DEBUGGER",
slcf, debugger);
ANALYZE_BOOLEAN_CONFIG_LENGTH("PASSENGER_SHOW_VERSION_IN_HEADER",
slcf, show_version_in_header);
len += sizeof("PASSENGER_ENVIRONMENT") + slcf->environment.len + 1;
len += sizeof("PASSENGER_SPAWN_METHOD") + slcf->spawn_method.len + 1;
len += sizeof("PASSENGER_APP_TYPE") + app_type_string_len;
ANALYZE_STR_CONFIG_LENGTH("PASSENGER_APP_GROUP_NAME", slcf, app_group_name);
ANALYZE_STR_CONFIG_LENGTH("PASSENGER_APP_RIGHTS", slcf, app_rights);
ANALYZE_STR_CONFIG_LENGTH("PASSENGER_USER", slcf, user);
ANALYZE_STR_CONFIG_LENGTH("PASSENGER_GROUP", slcf, group);
ANALYZE_STR_CONFIG_LENGTH("PASSENGER_UNION_STATION_KEY", slcf, union_station_key);
end = ngx_snprintf(min_instances_string,
sizeof(min_instances_string) - 1,
"%d",
(slcf->min_instances == (ngx_int_t) -1) ? 1 : slcf->min_instances);
*end = '\0';
len += sizeof("PASSENGER_MIN_INSTANCES") +
ngx_strlen(min_instances_string) + 1;
end = ngx_snprintf(framework_spawner_idle_time_string,
sizeof(framework_spawner_idle_time_string) - 1,
"%d",
(slcf->framework_spawner_idle_time == (ngx_int_t) -1) ?
-1 : slcf->framework_spawner_idle_time);
*end = '\0';
len += sizeof("PASSENGER_FRAMEWORK_SPAWNER_IDLE_TIME") +
ngx_strlen(framework_spawner_idle_time_string) + 1;
end = ngx_snprintf(app_spawner_idle_time_string,
sizeof(app_spawner_idle_time_string) - 1,
"%d",
(slcf->app_spawner_idle_time == (ngx_int_t) -1) ?
-1 : slcf->app_spawner_idle_time);
*end = '\0';
len += sizeof("PASSENGER_APP_SPAWNER_IDLE_TIME") +
ngx_strlen(app_spawner_idle_time_string) + 1;
if (slcf->union_station_filters != NGX_CONF_UNSET_PTR && slcf->union_station_filters->nelts > 0) {
len += sizeof("UNION_STATION_FILTERS");
union_station_filters = (ngx_str_t *) slcf->union_station_filters->elts;
for (i = 0; i < slcf->union_station_filters->nelts; i++) {
if (i != 0) {
len++;
}
len += union_station_filters[i].len;
}
len++;
}
/***********************/
/***********************/
/* Lengths of various CGI variables. */
if (slcf->vars_len) {
ngx_memzero(&le, sizeof(ngx_http_script_engine_t));
ngx_http_script_flush_no_cacheable_variables(r, slcf->flushes);
le.flushed = 1;
le.ip = slcf->vars_len->elts;
le.request = r;
while (*(uintptr_t *) le.ip) {
lcode = *(ngx_http_script_len_code_pt *) le.ip;
key_len = lcode(&le);
for (val_len = 0; *(uintptr_t *) le.ip; val_len += lcode(&le)) {
lcode = *(ngx_http_script_len_code_pt *) le.ip;
}
le.ip += sizeof(uintptr_t);
len += key_len + val_len;
}
}
/* Lengths of HTTP headers. */
if (slcf->upstream_config.pass_request_headers) {
part = &r->headers_in.headers.part;
header = part->elts;
for (i = 0; /* void */; i++) {
if (i >= part->nelts) {
if (part->next == NULL) {
break;
}
part = part->next;
header = part->elts;
i = 0;
}
len += sizeof("HTTP_") - 1 + header[i].key.len + 1
+ header[i].value.len + 1;
}
}
/* Trailing dummy header.
*
* If the last header value is an empty string, then the buffer
* will end with "\0\0". For example, if 'SSL_CLIENT_CERT'
* is the last header and it has an empty value, then the SCGI header
* will end with:
*
* "SSL_CLIENT_CERT\0\0"
*
* The data in the buffer will be processed by the AbstractRequestHandler class,
* which is implemented in Ruby. But it uses Hash[*data.split("\0")] to
* unserialize the data. Unfortunately String#split will not transform
* the trailing "\0\0" into an empty string:
*
* "SSL_CLIENT_CERT\0\0".split("\0")
* # => desired result: ["SSL_CLIENT_CERT", ""]
* # => actual result: ["SSL_CLIENT_CERT"]
*
* When that happens, Hash[..] will raise an ArgumentError because
* data.split("\0") does not return an array with a length that is a
* multiple of 2.
*
* So here, we add a dummy header to prevent situations like that from
* happening.
*/
len += sizeof("_") + sizeof("_");
/**************************************************
* Build the request header data.
**************************************************/
helper_agent_request_socket_password_data =
agents_starter_get_request_socket_password(passenger_agents_starter,
&helper_agent_request_socket_password_len);
size = helper_agent_request_socket_password_len +
/* netstring length + ":" + trailing "," */
/* note: 10 == sizeof("4294967296") - 1 */
len + 10 + 1 + 1;
b = ngx_create_temp_buf(r->pool, size);
if (b == NULL) {
return NGX_ERROR;
}
cl = ngx_alloc_chain_link(r->pool);
if (cl == NULL) {
return NGX_ERROR;
}
cl->buf = b;
/* Build SCGI header netstring length part. */
b->last = ngx_copy(b->last, helper_agent_request_socket_password_data,
helper_agent_request_socket_password_len);
b->last = ngx_snprintf(b->last, 10, "%ui", len);
*b->last++ = (u_char) ':';
/* Build CONTENT_LENGTH header. This must always be sent, even if 0. */
b->last = ngx_copy(b->last, "CONTENT_LENGTH",
sizeof("CONTENT_LENGTH"));
b->last = ngx_snprintf(b->last, 10, "%ui", content_length);
*b->last++ = (u_char) 0;
/* Build DOCUMENT_ROOT, SCRIPT_NAME, RAILS_RELATIVE_URL_ROOT, PATH_INFO and REQUEST_URI. */
b->last = ngx_copy(b->last, "DOCUMENT_ROOT", sizeof("DOCUMENT_ROOT"));
b->last = ngx_copy(b->last, context->public_dir.data,
context->public_dir.len + 1);
if (context->base_uri.len > 0) {
b->last = ngx_copy(b->last, "SCRIPT_NAME", sizeof("SCRIPT_NAME"));
b->last = ngx_copy(b->last, context->base_uri.data,
context->base_uri.len + 1);
b->last = ngx_copy(b->last, "RAILS_RELATIVE_URL_ROOT",
sizeof("RAILS_RELATIVE_URL_ROOT"));
b->last = ngx_copy(b->last, context->base_uri.data,
context->base_uri.len + 1);
b->last = ngx_copy(b->last, "PATH_INFO", sizeof("PATH_INFO"));
b->last = ngx_copy(b->last, escaped_uri.data + context->base_uri.len,
escaped_uri.len - context->base_uri.len);
b->last = ngx_copy(b->last, "", 1);
} else {
b->last = ngx_copy(b->last, "SCRIPT_NAME", sizeof("SCRIPT_NAME"));
b->last = ngx_copy(b->last, "", sizeof(""));
b->last = ngx_copy(b->last, "PATH_INFO", sizeof("PATH_INFO"));
b->last = ngx_copy(b->last, escaped_uri.data, escaped_uri.len);
b->last = ngx_copy(b->last, "", 1);
}
b->last = ngx_copy(b->last, "REQUEST_URI", sizeof("REQUEST_URI"));
b->last = ngx_copy(b->last, escaped_uri.data, escaped_uri.len);
if (r->args.len > 0) {
b->last = ngx_copy(b->last, "?", 1);
b->last = ngx_copy(b->last, r->args.data, r->args.len);
}
b->last = ngx_copy(b->last, "", 1);
/* SERVER_NAME */
b->last = ngx_copy(b->last, "SERVER_NAME", sizeof("SERVER_NAME"));
if (r->headers_in.host != NULL) {
b->last = ngx_copy(b->last, r->headers_in.host->value.data,
server_name_len);
} else {
b->last = ngx_copy(b->last, cscf->server_name.data,
server_name_len);
}
b->last = ngx_copy(b->last, "", 1);
/* Various other HTTP headers. */
if (r->headers_in.content_type != NULL
&& r->headers_in.content_type->value.len > 0) {
b->last = ngx_copy(b->last, "CONTENT_TYPE", sizeof("CONTENT_TYPE"));
b->last = ngx_copy(b->last, r->headers_in.content_type->value.data,
r->headers_in.content_type->value.len);
b->last = ngx_copy(b->last, "", 1);
}
#if (NGX_HTTP_SSL)
ssl_conf = ngx_http_get_module_srv_conf(r, ngx_http_ssl_module);
if (ssl_conf->enable) {
b->last = ngx_copy(b->last, "HTTPS", sizeof("HTTPS"));
b->last = ngx_copy(b->last, "on", sizeof("on"));
}
#endif
/* Build Passenger application pool option headers. */
SERIALIZE_BOOLEAN_CONFIG_DATA("PASSENGER_USE_GLOBAL_QUEUE",
slcf, use_global_queue);
SERIALIZE_BOOLEAN_CONFIG_DATA("PASSENGER_FRIENDLY_ERROR_PAGES",
slcf, friendly_error_pages);
SERIALIZE_BOOLEAN_CONFIG_DATA("UNION_STATION_SUPPORT",
slcf, union_station_support);
SERIALIZE_BOOLEAN_CONFIG_DATA("PASSENGER_DEBUGGER",
slcf, debugger);
SERIALIZE_BOOLEAN_CONFIG_DATA("PASSENGER_SHOW_VERSION_IN_HEADER",
slcf, show_version_in_header);
b->last = ngx_copy(b->last, "PASSENGER_ENVIRONMENT",
sizeof("PASSENGER_ENVIRONMENT"));
b->last = ngx_copy(b->last, slcf->environment.data,
slcf->environment.len + 1);
b->last = ngx_copy(b->last, "PASSENGER_SPAWN_METHOD",
sizeof("PASSENGER_SPAWN_METHOD"));
b->last = ngx_copy(b->last, slcf->spawn_method.data,
slcf->spawn_method.len + 1);
SERIALIZE_STR_CONFIG_DATA("PASSENGER_APP_GROUP_NAME",
slcf, app_group_name);
SERIALIZE_STR_CONFIG_DATA("PASSENGER_APP_RIGHTS",
slcf, app_rights);
SERIALIZE_STR_CONFIG_DATA("PASSENGER_USER",
slcf, user);
SERIALIZE_STR_CONFIG_DATA("PASSENGER_GROUP",
slcf, group);
SERIALIZE_STR_CONFIG_DATA("PASSENGER_UNION_STATION_KEY",
slcf, union_station_key);
b->last = ngx_copy(b->last, "PASSENGER_APP_TYPE",
sizeof("PASSENGER_APP_TYPE"));
b->last = ngx_copy(b->last, app_type_string, app_type_string_len);
b->last = ngx_copy(b->last, "PASSENGER_MIN_INSTANCES",
sizeof("PASSENGER_MIN_INSTANCES"));
b->last = ngx_copy(b->last, min_instances_string,
ngx_strlen(min_instances_string) + 1);
b->last = ngx_copy(b->last, "PASSENGER_FRAMEWORK_SPAWNER_IDLE_TIME",
sizeof("PASSENGER_FRAMEWORK_SPAWNER_IDLE_TIME"));
b->last = ngx_copy(b->last, framework_spawner_idle_time_string,
ngx_strlen(framework_spawner_idle_time_string) + 1);
b->last = ngx_copy(b->last, "PASSENGER_APP_SPAWNER_IDLE_TIME",
sizeof("PASSENGER_APP_SPAWNER_IDLE_TIME"));
b->last = ngx_copy(b->last, app_spawner_idle_time_string,
ngx_strlen(app_spawner_idle_time_string) + 1);
if (slcf->union_station_filters != NGX_CONF_UNSET_PTR && slcf->union_station_filters->nelts > 0) {
b->last = ngx_copy(b->last, "UNION_STATION_FILTERS",
sizeof("UNION_STATION_FILTERS"));
for (i = 0; i < slcf->union_station_filters->nelts; i++) {
if (i != 0) {
b->last = ngx_copy(b->last, "\1", 1);
}
b->last = ngx_copy(b->last, union_station_filters[i].data,
union_station_filters[i].len);
}
b->last = ngx_copy(b->last, "\0", 1);
}
/***********************/
/***********************/
if (slcf->vars_len) {
ngx_memzero(&e, sizeof(ngx_http_script_engine_t));
e.ip = slcf->vars->elts;
e.pos = b->last;
e.request = r;
e.flushed = 1;
le.ip = slcf->vars_len->elts;
while (*(uintptr_t *) le.ip) {
lcode = *(ngx_http_script_len_code_pt *) le.ip;
(void) lcode(&le);
for (val_len = 0; *(uintptr_t *) le.ip; val_len += lcode(&le)) {
lcode = *(ngx_http_script_len_code_pt *) le.ip;
}
le.ip += sizeof(uintptr_t);
while (*(uintptr_t *) e.ip) {
code = *(ngx_http_script_code_pt *) e.ip;
code((ngx_http_script_engine_t *) &e);
}
e.ip += sizeof(uintptr_t);
}
b->last = e.pos;
}
if (slcf->upstream_config.pass_request_headers) {
part = &r->headers_in.headers.part;
header = part->elts;
for (i = 0; /* void */; i++) {
if (i >= part->nelts) {
if (part->next == NULL) {
break;
}
part = part->next;
header = part->elts;
i = 0;
}
b->last = ngx_cpymem(b->last, "HTTP_", sizeof("HTTP_") - 1);
for (n = 0; n < header[i].key.len; n++) {
ch = header[i].key.data[n];
if (ch >= 'a' && ch <= 'z') {
ch &= ~0x20;
} else if (ch == '-') {
ch = '_';
}
*b->last++ = ch;
}
*b->last++ = (u_char) 0;
b->last = ngx_copy(b->last, header[i].value.data,
header[i].value.len);
*b->last++ = (u_char) 0;
}
}
/* Trailing dummy header. See earlier comment for explanation. */
b->last = ngx_copy(b->last, "_\0_", sizeof("_\0_"));
*b->last++ = (u_char) ',';
if (slcf->upstream_config.pass_request_body) {
body = r->upstream->request_bufs;
r->upstream->request_bufs = cl;
while (body) {
b = ngx_alloc_buf(r->pool);
if (b == NULL) {
return NGX_ERROR;
}
ngx_memcpy(b, body->buf, sizeof(ngx_buf_t));
cl->next = ngx_alloc_chain_link(r->pool);
if (cl->next == NULL) {
return NGX_ERROR;
}
cl = cl->next;
cl->buf = b;
body = body->next;
}
b->flush = 1;
} else {
r->upstream->request_bufs = cl;
}
cl->next = NULL;
return NGX_OK;
}
static ngx_int_t
reinit_request(ngx_http_request_t *r)
{
passenger_context_t *context;
context = ngx_http_get_module_ctx(r, ngx_http_passenger_module);
if (context == NULL) {
return NGX_OK;
}
context->status = 0;
context->status_count = 0;
context->status_start = NULL;
context->status_end = NULL;
r->upstream->process_header = process_status_line;
return NGX_OK;
}
static ngx_int_t
process_status_line(ngx_http_request_t *r)
{
ngx_int_t rc;
ngx_http_upstream_t *u;
passenger_context_t *context;
context = ngx_http_get_module_ctx(r, ngx_http_passenger_module);
if (context == NULL) {
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
rc = parse_status_line(r, context);
if (rc == NGX_AGAIN) {
return rc;
}
u = r->upstream;
if (rc == NGX_HTTP_SCGI_PARSE_NO_HEADER) {
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
"upstream sent no valid HTTP/1.0 header");
#if 0
if (u->accel) {
return NGX_HTTP_UPSTREAM_INVALID_HEADER;
}
#endif
r->http_version = NGX_HTTP_VERSION_9;
u->headers_in.status_n = NGX_HTTP_OK;
u->state->status = NGX_HTTP_OK;
return NGX_OK;
}
u->headers_in.status_n = context->status;
u->state->status = context->status;
u->headers_in.status_line.len = context->status_end - context->status_start;
u->headers_in.status_line.data = ngx_palloc(r->pool,
u->headers_in.status_line.len);
if (u->headers_in.status_line.data == NULL) {
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
ngx_memcpy(u->headers_in.status_line.data, context->status_start,
u->headers_in.status_line.len);
ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"http scgi status %ui \"%V\"",
u->headers_in.status_n, &u->headers_in.status_line);
u->process_header = process_header;
return process_header(r);
}
static ngx_int_t
parse_status_line(ngx_http_request_t *r, passenger_context_t *context)
{
u_char ch;
u_char *pos;
ngx_http_upstream_t *u;
enum {
sw_start = 0,
sw_H,
sw_HT,
sw_HTT,
sw_HTTP,
sw_first_major_digit,
sw_major_digit,
sw_first_minor_digit,
sw_minor_digit,
sw_status,
sw_space_after_status,
sw_status_text,
sw_almost_done
} state;
u = r->upstream;
state = r->state;
for (pos = u->buffer.pos; pos < u->buffer.last; pos++) {
ch = *pos;
switch (state) {
/* "HTTP/" */
case sw_start:
switch (ch) {
case 'H':
state = sw_H;
break;
default:
return NGX_HTTP_SCGI_PARSE_NO_HEADER;
}
break;
case sw_H:
switch (ch) {
case 'T':
state = sw_HT;
break;
default:
return NGX_HTTP_SCGI_PARSE_NO_HEADER;