public
Description: Phusion Passenger (mod_rails)
Homepage: http://www.modrails.com/
Clone URL: git://github.com/FooBarWidget/passenger.git
Search Repo:
Click here to lend your support to: passenger and make a donation at www.pledgie.com !
Hongli Lai (Phusion) (author)
Sat Apr 26 04:58:11 -0700 2008
commit  59838b9704e6fa966b51f93379fed10d13a7dae9
tree    82b8d33536c70f355325650b005d6a3f4caf1475
parent  bd59941af79a6d1c8d2ea6e42b61ffbf5ab57c3c
passenger / ext / apache2 / Hooks.cpp
100644 683 lines (597 sloc) 20.784 kb
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
/*
* Phusion Passenger - http://www.modrails.com/
* Copyright (C) 2008 Phusion
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; version 2 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include <ap_config.h>
#include <httpd.h>
#include <http_config.h>
#include <http_core.h>
#include <http_request.h>
#include <http_protocol.h>
#include <http_log.h>
#include <util_script.h>
#include <apr_pools.h>
#include <apr_strings.h>
#include <apr_lib.h>
 
#include <sys/time.h>
#include <sys/resource.h>
#include <exception>
#include <unistd.h>
 
#include "Hooks.h"
#include "Configuration.h"
#include "Utils.h"
#include "Logging.h"
#include "ApplicationPoolClientServer.h"
#include "MessageChannel.h"
 
using namespace std;
using namespace Passenger;
 
extern "C" module AP_MODULE_DECLARE_DATA passenger_module;
 
#define DEFAULT_RUBY_COMMAND "ruby"
#define DEFAULT_RAILS_ENV "production"
 
 
class Hooks {
private:
  struct Container {
    Application::SessionPtr session;
    
    static apr_status_t cleanup(void *p) {
      delete (Container *) p;
      return APR_SUCCESS;
    }
  };
 
  ApplicationPoolServerPtr applicationPoolServer;
  ApplicationPoolPtr applicationPool;
  
  DirConfig *getDirConfig(request_rec *r) {
    return (DirConfig *) ap_get_module_config(r->per_dir_config, &passenger_module);
  }
  
  ServerConfig *getServerConfig(server_rec *s) {
    return (ServerConfig *) ap_get_module_config(s->module_config, &passenger_module);
  }
  
  /**
   * Determine whether the given HTTP request falls under one of the specified
   * RailsBaseURIs. If yes, then the first matching base URI will be returned.
   *
   * If Rails autodetection was enabled in the configuration, then this function
   * will return '/' if the document root seems to be a valid Rails 'public' folder.
   *
   * Otherwise, NULL will be returned.
   */
  const char *determineRailsBaseURI(request_rec *r, DirConfig *config) {
    set<string>::const_iterator it;
    const char *uri = r->uri;
    size_t uri_len = strlen(uri);
    
    if (uri_len == 0 || uri[0] != '/') {
      return NULL;
    }
    
    for (it = config->base_uris.begin(); it != config->base_uris.end(); it++) {
      const string &base(*it);
      if ( base == "/"
       || ( uri_len == base.size() && memcmp(uri, base.c_str(), uri_len) == 0 )
       || ( uri_len > base.size() && memcmp(uri, base.c_str(), base.size()) == 0
       && uri[base.size()] == '/' )
      ) {
        return apr_pstrdup(r->pool, base.c_str());
      }
    }
    
    if ((config->autoDetect == DirConfig::ENABLED || config->autoDetect == DirConfig::UNSET)
     && verifyRailsDir(ap_document_root(r))) {
      return "/";
    }
    
    return NULL;
  }
  
  string determineRailsDir(request_rec *r, const char *baseURI) {
    const char *docRoot = ap_document_root(r);
    size_t len = strlen(docRoot);
    if (len > 0) {
      string path;
      if (docRoot[len - 1] == '/') {
        path.assign(docRoot, len - 1);
      } else {
        path.assign(docRoot, len);
      }
      if (strcmp(baseURI, "/") != 0) {
        path.append(baseURI);
      }
      return path;
    } else {
      return "";
    }
  }
  
  char *http2env(apr_pool_t *p, const char *name) {
    char *env_name = apr_pstrcat(p, "HTTP_", name, NULL);
    char *cp;
    
    for (cp = env_name + 5; *cp != 0; cp++) {
      if (*cp == '-') {
        *cp = '_';
      } else {
        *cp = apr_toupper(*cp);
      }
    }
    
    return env_name;
  }
  
  char *lookupName(apr_table_t *t, const char *name) {
    const apr_array_header_t *hdrs_arr = apr_table_elts(t);
    apr_table_entry_t *hdrs = (apr_table_entry_t *) hdrs_arr->elts;
    int i;
    
    for (i = 0; i < hdrs_arr->nelts; ++i) {
      if (hdrs[i].key == NULL) {
        continue;
      }
      if (strcasecmp(hdrs[i].key, name) == 0) {
        return hdrs[i].val;
      }
    }
    return NULL;
  }
  
  char *lookupHeader(request_rec *r, const char *name) {
    return lookupName(r->headers_in, name);
  }
  
  char *lookupEnv(request_rec *r, const char *name) {
    return lookupName(r->subprocess_env, name);
  }
  
  // This code is a duplicate of what's in util_script.c. We can't use
  // r->unparsed_uri because it gets changed if there was a redirect.
  char *originalURI(request_rec *r) {
    char *first, *last;
 
    if (r->the_request == NULL) {
      return (char *) apr_pcalloc(r->pool, 1);
    }
    
    first = r->the_request;  // use the request-line
    
    while (*first && !apr_isspace(*first)) {
      ++first;    // skip over the method
    }
    while (apr_isspace(*first)) {
      ++first;    // and the space(s)
    }
    
    last = first;
    while (*last && !apr_isspace(*last)) {
      ++last;      // end at next whitespace
    }
    
    return apr_pstrmemdup(r->pool, first, last - first);
  }
 
  void addHeader(apr_table_t *table, const char *name, const char *value) {
    if (name != NULL && value != NULL) {
      apr_table_addn(table, name, value);
    }
  }
  
  apr_status_t sendHeaders(request_rec *r, Application::SessionPtr &session, const char *baseURI) {
    apr_table_t *headers;
    headers = apr_table_make(r->pool, 40);
    if (headers == NULL) {
      return APR_ENOMEM;
    }
    
    // Set standard CGI variables.
    addHeader(headers, "SERVER_SOFTWARE", ap_get_server_version());
    addHeader(headers, "SERVER_PROTOCOL", r->protocol);
    addHeader(headers, "SERVER_NAME", ap_get_server_name(r));
    addHeader(headers, "SERVER_ADMIN", r->server->server_admin);
    addHeader(headers, "SERVER_ADDR", r->connection->local_ip);
    addHeader(headers, "SERVER_PORT", apr_psprintf(r->pool, "%u", ap_get_server_port(r)));
    addHeader(headers, "REMOTE_ADDR", r->connection->remote_ip);
    addHeader(headers, "REMOTE_PORT", apr_psprintf(r->pool, "%d", r->connection->remote_addr->port));
    addHeader(headers, "REMOTE_USER", r->user);
    addHeader(headers, "REQUEST_METHOD", r->method);
    addHeader(headers, "REQUEST_URI", originalURI(r));
    addHeader(headers, "QUERY_STRING", r->args ? r->args : "");
    if (strcmp(baseURI, "/") != 0) {
      addHeader(headers, "SCRIPT_NAME", baseURI);
    }
    addHeader(headers, "HTTPS", lookupEnv(r, "HTTPS"));
    addHeader(headers, "CONTENT_TYPE", lookupHeader(r, "Content-type"));
    addHeader(headers, "DOCUMENT_ROOT", ap_document_root(r));
    
    // Set HTTP headers.
    const apr_array_header_t *hdrs_arr;
    apr_table_entry_t *hdrs;
    int i;
    
    hdrs_arr = apr_table_elts(r->headers_in);
    hdrs = (apr_table_entry_t *) hdrs_arr->elts;
    for (i = 0; i < hdrs_arr->nelts; ++i) {
      if (hdrs[i].key) {
        addHeader(headers, http2env(r->pool, hdrs[i].key), hdrs[i].val);
      }
    }
  
    // Add other environment variables.
    const apr_array_header_t *env_arr;
    apr_table_entry_t *env;
    
    env_arr = apr_table_elts(r->subprocess_env);
    env = (apr_table_entry_t*) env_arr->elts;
    for (i = 0; i < env_arr->nelts; ++i) {
      addHeader(headers, env[i].key, env[i].val);
    }
    
    // Now send the headers.
    string buffer;
    
    hdrs_arr = apr_table_elts(headers);
        hdrs = (apr_table_entry_t*) hdrs_arr->elts;
        buffer.reserve(1024 * 4);
    for (i = 0; i < hdrs_arr->nelts; ++i) {
      buffer.append(hdrs[i].key);
      buffer.append(1, '\0');
      buffer.append(hdrs[i].val);
      buffer.append(1, '\0');
    }
    
    /*
     * If the last header value is an empty string, then the buffer
     * will end with "\0\0". For example, if 'SSLOptions +ExportCertData'
     * is set, and there's no client certificate, and 'SSL_CLIENT_CERT'
     * is the last header, then the buffer will end with:
     *
     * "SSL_CLIENT_CERT\0\0"
     *
     * The data in the buffer will be processed by the RequestHandler 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.
     */
    buffer.append("_\0_\0", 4);
    
    session->sendHeaders(buffer);
    return APR_SUCCESS;
  }
  
  apr_status_t sendRequestBody(request_rec *r, Application::SessionPtr &session) {
    if (ap_should_client_block(r)) {
      char buf[1024 * 32];
      apr_off_t len;
      
      while ((len = ap_get_client_block(r, buf, sizeof(buf))) > 0) {
        session->sendBodyBlock(buf, len);
      }
      if (len == -1) {
        return HTTP_INTERNAL_SERVER_ERROR;
      }
    }
    session->closeWriter();
    return APR_SUCCESS;
  }
 
public:
  Hooks(apr_pool_t *pconf, apr_pool_t *plog, apr_pool_t *ptemp, server_rec *s) {
    ap_add_version_component(pconf, "Phusion_Passenger/" PASSENGER_VERSION);
    passenger_config_merge_all_servers(pconf, s);
    
    ServerConfig *config = getServerConfig(s);
    const char *ruby, *environment, *user;
    string spawnServer;
    
    ruby = (config->ruby != NULL) ? config->ruby : DEFAULT_RUBY_COMMAND;
    environment = (config->env != NULL) ? config->env : DEFAULT_RAILS_ENV;
    if (config->userSwitching) {
      user = "";
    } else if (config->defaultUser != NULL) {
      user = config->defaultUser;
    } else {
      user = "nobody";
    }
    if (config->spawnServer != NULL) {
      spawnServer = config->spawnServer;
    } else {
      spawnServer = findSpawnServer();
      if (spawnServer.empty()) {
        throw FileNotFoundException("The Passenger spawn server script "
          "could not be found. Please ensure that it can be found "
          "in $PATH, or specify it with the RailsSpawnServer "
          "configuration option.");
      }
    }
    if (!fileExists(spawnServer.c_str())) {
      string message("The specified Passenger spawn server script, '");
      message.append(spawnServer);
      message.append("', does not exist.");
      throw FileNotFoundException(message);
    }
    
    applicationPoolServer = ptr(
      new ApplicationPoolServer(spawnServer, "", environment, ruby, user)
    );
  }
  
  void initChild(apr_pool_t *pchild, server_rec *s) {
    ServerConfig *config = getServerConfig(s);
    
    try {
      applicationPool = applicationPoolServer->connect();
      applicationPoolServer->detach();
      applicationPool->setMax(config->maxPoolSize);
      applicationPool->setMaxIdleTime(config->poolIdleTime);
    } catch (const exception &e) {
      fprintf(stderr, "*** Cannot initialize Passenger: %s\n", e.what());
      fflush(stderr);
      abort();
    }
  }
  
  int handleRequest(request_rec *r) {
    DirConfig *config = getDirConfig(r);
    const char *railsBaseURI = determineRailsBaseURI(r, config);
    if (railsBaseURI == NULL || r->filename == NULL || fileExists(r->filename)) {
      return DECLINED;
    }
    
    string railsDir(determineRailsDir(r, railsBaseURI));
    if (railsDir.empty()) {
      ap_set_content_type(r, "text/html; charset=UTF-8");
      ap_rputs("<h1>Passenger error #1</h1>\n", r);
      ap_rputs("Cannot determine the location of the Rails application's \"public\" directory.", r);
      return OK;
    } else if (!verifyRailsDir(railsDir)) {
      ap_set_content_type(r, "text/html; charset=UTF-8");
      ap_rputs("<h1>Passenger error #2</h1>\n", r);
      ap_rputs("Passenger thought that the Rails application's \"public\" directory is \"", r);
      ap_rputs(ap_escape_html(r->pool, railsDir.c_str()), r);
      ap_rputs("\". But upon further inspection, it doesn't seem to be a valid Rails ", r);
      ap_rputs("\"public\" folder. It is possible that Apache doesn't have read ", r);
      ap_rputs("permissions to your Rails application's folder. Please check your ", r);
      ap_rputs("file permissions.", r);
      return OK;
    }
    
    int httpStatus = ap_setup_client_block(r, REQUEST_CHUNKED_ERROR);
        if (httpStatus != OK) {
      return httpStatus;
    }
    
    try {
      apr_bucket_brigade *bb;
      apr_bucket *b;
      Application::SessionPtr session;
      
      P_DEBUG("Processing HTTP request: " << r->uri);
      try {
        const char *defaultUser;
        ServerConfig *sconfig;
        
        sconfig = getServerConfig(r->server);
        if (sconfig->defaultUser != NULL) {
          defaultUser = sconfig->defaultUser;
        } else {
          defaultUser = "nobody";
        }
        session = applicationPool->get(canonicalizePath(railsDir + "/.."),
          true, defaultUser);
      } catch (const SpawnException &e) {
        if (e.hasErrorPage()) {
          ap_set_content_type(r, "text/html; charset=utf-8");
          ap_rputs(e.getErrorPage().c_str(), r);
          // Unfortunately we can't return a 500 Internal Server
          // Error. Apache's HTTP error handler would kick in.
          return OK;
        } else {
          throw;
        }
      }
      sendHeaders(r, session, railsBaseURI);
      sendRequestBody(r, session);
      
      apr_file_t *readerPipe = NULL;
      int reader = session->getReader();
      apr_os_pipe_put(&readerPipe, &reader, r->pool);
 
      bb = apr_brigade_create(r->connection->pool, r->connection->bucket_alloc);
      b = apr_bucket_pipe_create(readerPipe, r->connection->bucket_alloc);
      APR_BRIGADE_INSERT_TAIL(bb, b);
 
      b = apr_bucket_eos_create(r->connection->bucket_alloc);
      APR_BRIGADE_INSERT_TAIL(bb, b);
 
      ap_scan_script_header_err_brigade(r, bb, NULL);
      ap_pass_brigade(r->output_filters, bb);
      
      Container *container = new Container();
      container->session = session;
      apr_pool_cleanup_register(r->pool, container, Container::cleanup, apr_pool_cleanup_null);
 
      return OK;
    } catch (const exception &e) {
      ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, "*** Unexpected error in Passenger: %s", e.what());
      return HTTP_INTERNAL_SERVER_ERROR;
    }
  }
  
  int
  mapToStorage(request_rec *r