Coverage Report

Created: 2022-02-19 20:31

/src/php-src/main/SAPI.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
   +----------------------------------------------------------------------+
3
   | Copyright (c) The PHP Group                                          |
4
   +----------------------------------------------------------------------+
5
   | This source file is subject to version 3.01 of the PHP license,      |
6
   | that is bundled with this package in the file LICENSE, and is        |
7
   | available through the world-wide-web at the following url:           |
8
   | http://www.php.net/license/3_01.txt                                  |
9
   | If you did not receive a copy of the PHP license and are unable to   |
10
   | obtain it through the world-wide-web, please send a note to          |
11
   | license@php.net so we can mail you a copy immediately.               |
12
   +----------------------------------------------------------------------+
13
   | Original design:  Shane Caraveo <shane@caraveo.com>                  |
14
   | Authors: Andi Gutmans <andi@php.net>                                 |
15
   |          Zeev Suraski <zeev@php.net>                                 |
16
   +----------------------------------------------------------------------+
17
*/
18
19
#include <ctype.h>
20
#include <sys/stat.h>
21
22
#include "php.h"
23
#include "SAPI.h"
24
#include "php_variables.h"
25
#include "php_ini.h"
26
#include "ext/standard/php_string.h"
27
#include "ext/standard/pageinfo.h"
28
#include "ext/pcre/php_pcre.h"
29
#ifdef ZTS
30
#include "TSRM.h"
31
#endif
32
#ifdef HAVE_SYS_TIME_H
33
#include <sys/time.h>
34
#elif defined(PHP_WIN32)
35
#include "win32/time.h"
36
#endif
37
38
#include "rfc1867.h"
39
40
#include "php_content_types.h"
41
42
#ifdef ZTS
43
SAPI_API int sapi_globals_id;
44
SAPI_API size_t sapi_globals_offset;
45
#else
46
sapi_globals_struct sapi_globals;
47
#endif
48
49
static void _type_dtor(zval *zv)
50
4.06k
{
51
4.06k
  free(Z_PTR_P(zv));
52
4.06k
}
53
54
static void sapi_globals_ctor(sapi_globals_struct *sapi_globals)
55
4.06k
{
56
4.06k
  memset(sapi_globals, 0, sizeof(*sapi_globals));
57
4.06k
  zend_hash_init(&sapi_globals->known_post_content_types, 8, NULL, _type_dtor, 1);
58
4.06k
  php_setup_sapi_content_types();
59
4.06k
}
60
61
static void sapi_globals_dtor(sapi_globals_struct *sapi_globals)
62
0
{
63
0
  zend_hash_destroy(&sapi_globals->known_post_content_types);
64
0
}
65
66
/* True globals (no need for thread safety) */
67
SAPI_API sapi_module_struct sapi_module;
68
69
70
SAPI_API void sapi_startup(sapi_module_struct *sf)
71
4.06k
{
72
4.06k
  sf->ini_entries = NULL;
73
4.06k
  sapi_module = *sf;
74
75
#ifdef ZTS
76
  ts_allocate_fast_id(&sapi_globals_id, &sapi_globals_offset, sizeof(sapi_globals_struct), (ts_allocate_ctor) sapi_globals_ctor, (ts_allocate_dtor) sapi_globals_dtor);
77
# ifdef PHP_WIN32
78
  _configthreadlocale(_ENABLE_PER_THREAD_LOCALE);
79
# endif
80
#else
81
4.06k
  sapi_globals_ctor(&sapi_globals);
82
4.06k
#endif
83
84
#ifdef PHP_WIN32
85
  tsrm_win32_startup();
86
#endif
87
88
4.06k
  reentrancy_startup();
89
4.06k
}
90
91
SAPI_API void sapi_shutdown(void)
92
0
{
93
#ifdef ZTS
94
  ts_free_id(sapi_globals_id);
95
#else
96
0
  sapi_globals_dtor(&sapi_globals);
97
0
#endif
98
99
0
  reentrancy_shutdown();
100
101
#ifdef PHP_WIN32
102
  tsrm_win32_shutdown();
103
#endif
104
0
}
105
106
107
SAPI_API void sapi_free_header(sapi_header_struct *sapi_header)
108
1.87M
{
109
1.87M
  efree(sapi_header->header);
110
1.87M
}
111
112
/* {{{ call a header function */
113
PHP_FUNCTION(header_register_callback)
114
0
{
115
0
  zend_fcall_info fci;
116
0
  zend_fcall_info_cache fcc;
117
118
0
  if (zend_parse_parameters(ZEND_NUM_ARGS(), "f", &fci, &fcc) == FAILURE) {
119
0
    RETURN_THROWS();
120
0
  }
121
122
0
  if (Z_TYPE(SG(callback_func)) != IS_UNDEF) {
123
0
    zval_ptr_dtor(&SG(callback_func));
124
0
    SG(fci_cache) = empty_fcall_info_cache;
125
0
  }
126
127
0
  ZVAL_COPY(&SG(callback_func), &fci.function_name);
128
129
0
  RETURN_TRUE;
130
0
}
131
/* }}} */
132
133
static void sapi_run_header_callback(zval *callback)
134
0
{
135
0
  int   error;
136
0
  zend_fcall_info fci;
137
0
  char *callback_error = NULL;
138
0
  zval retval;
139
140
0
  if (zend_fcall_info_init(callback, 0, &fci, &SG(fci_cache), NULL, &callback_error) == SUCCESS) {
141
0
    fci.retval = &retval;
142
143
0
    error = zend_call_function(&fci, &SG(fci_cache));
144
0
    if (error == FAILURE) {
145
0
      goto callback_failed;
146
0
    } else {
147
0
      zval_ptr_dtor(&retval);
148
0
    }
149
0
  } else {
150
0
callback_failed:
151
0
    php_error_docref(NULL, E_WARNING, "Could not call the sapi_header_callback");
152
0
  }
153
154
0
  if (callback_error) {
155
0
    efree(callback_error);
156
0
  }
157
0
}
158
159
SAPI_API void sapi_handle_post(void *arg)
160
0
{
161
0
  if (SG(request_info).post_entry && SG(request_info).content_type_dup) {
162
0
    SG(request_info).post_entry->post_handler(SG(request_info).content_type_dup, arg);
163
0
    efree(SG(request_info).content_type_dup);
164
0
    SG(request_info).content_type_dup = NULL;
165
0
  }
166
0
}
167
168
static void sapi_read_post_data(void)
169
0
{
170
0
  sapi_post_entry *post_entry;
171
0
  uint32_t content_type_length = (uint32_t)strlen(SG(request_info).content_type);
172
0
  char *content_type = estrndup(SG(request_info).content_type, content_type_length);
173
0
  char *p;
174
0
  char oldchar=0;
175
0
  void (*post_reader_func)(void) = NULL;
176
177
178
  /* dedicated implementation for increased performance:
179
   * - Make the content type lowercase
180
   * - Trim descriptive data, stay with the content-type only
181
   */
182
0
  for (p=content_type; p<content_type+content_type_length; p++) {
183
0
    switch (*p) {
184
0
      case ';':
185
0
      case ',':
186
0
      case ' ':
187
0
        content_type_length = p-content_type;
188
0
        oldchar = *p;
189
0
        *p = 0;
190
0
        break;
191
0
      default:
192
0
        *p = tolower(*p);
193
0
        break;
194
0
    }
195
0
  }
196
197
  /* now try to find an appropriate POST content handler */
198
0
  if ((post_entry = zend_hash_str_find_ptr(&SG(known_post_content_types), content_type,
199
0
      content_type_length)) != NULL) {
200
    /* found one, register it for use */
201
0
    SG(request_info).post_entry = post_entry;
202
0
    post_reader_func = post_entry->post_reader;
203
0
  } else {
204
    /* fallback */
205
0
    SG(request_info).post_entry = NULL;
206
0
    if (!sapi_module.default_post_reader) {
207
      /* no default reader ? */
208
0
      SG(request_info).content_type_dup = NULL;
209
0
      sapi_module.sapi_error(E_WARNING, "Unsupported content type:  '%s'", content_type);
210
0
      return;
211
0
    }
212
0
  }
213
0
  if (oldchar) {
214
0
    *(p-1) = oldchar;
215
0
  }
216
217
0
  SG(request_info).content_type_dup = content_type;
218
219
0
  if(post_reader_func) {
220
0
    post_reader_func();
221
0
  }
222
223
0
  if(sapi_module.default_post_reader) {
224
0
    sapi_module.default_post_reader();
225
0
  }
226
0
}
227
228
SAPI_API size_t sapi_read_post_block(char *buffer, size_t buflen)
229
0
{
230
0
  size_t read_bytes;
231
232
0
  if (!sapi_module.read_post) {
233
0
    return 0;
234
0
  }
235
236
0
  read_bytes = sapi_module.read_post(buffer, buflen);
237
238
0
  if (read_bytes > 0) {
239
    /* gogo */
240
0
    SG(read_post_bytes) += read_bytes;
241
0
  }
242
0
  if (read_bytes < buflen) {
243
    /* done */
244
0
    SG(post_read) = 1;
245
0
  }
246
247
0
  return read_bytes;
248
0
}
249
250
SAPI_API SAPI_POST_READER_FUNC(sapi_read_standard_form_data)
251
0
{
252
0
  if ((SG(post_max_size) > 0) && (SG(request_info).content_length > SG(post_max_size))) {
253
0
    php_error_docref(NULL, E_WARNING, "POST Content-Length of " ZEND_LONG_FMT " bytes exceeds the limit of " ZEND_LONG_FMT " bytes",
254
0
          SG(request_info).content_length, SG(post_max_size));
255
0
    return;
256
0
  }
257
258
259
0
  SG(request_info).request_body = php_stream_temp_create_ex(TEMP_STREAM_DEFAULT, SAPI_POST_BLOCK_SIZE, PG(upload_tmp_dir));
260
261
0
  if (sapi_module.read_post) {
262
0
    size_t read_bytes;
263
264
0
    for (;;) {
265
0
      char buffer[SAPI_POST_BLOCK_SIZE];
266
267
0
      read_bytes = sapi_read_post_block(buffer, SAPI_POST_BLOCK_SIZE);
268
269
0
      if (read_bytes > 0) {
270
0
        if (php_stream_write(SG(request_info).request_body, buffer, read_bytes) != read_bytes) {
271
          /* if parts of the stream can't be written, purge it completely */
272
0
          php_stream_truncate_set_size(SG(request_info).request_body, 0);
273
0
          php_error_docref(NULL, E_WARNING, "POST data can't be buffered; all data discarded");
274
0
          break;
275
0
        }
276
0
      }
277
278
0
      if ((SG(post_max_size) > 0) && (SG(read_post_bytes) > SG(post_max_size))) {
279
0
        php_error_docref(NULL, E_WARNING, "Actual POST length does not match Content-Length, and exceeds " ZEND_LONG_FMT " bytes", SG(post_max_size));
280
0
        break;
281
0
      }
282
283
0
      if (read_bytes < SAPI_POST_BLOCK_SIZE) {
284
        /* done */
285
0
        break;
286
0
      }
287
0
    }
288
0
    php_stream_rewind(SG(request_info).request_body);
289
0
  }
290
0
}
291
292
293
static inline char *get_default_content_type(uint32_t prefix_len, uint32_t *len)
294
938k
{
295
938k
  char *mimetype, *charset, *content_type;
296
938k
  uint32_t mimetype_len, charset_len;
297
298
938k
  if (SG(default_mimetype)) {
299
938k
    mimetype = SG(default_mimetype);
300
938k
    mimetype_len = (uint32_t)strlen(SG(default_mimetype));
301
0
  } else {
302
0
    mimetype = SAPI_DEFAULT_MIMETYPE;
303
0
    mimetype_len = sizeof(SAPI_DEFAULT_MIMETYPE) - 1;
304
0
  }
305
938k
  if (SG(default_charset)) {
306
938k
    charset = SG(default_charset);
307
938k
    charset_len = (uint32_t)strlen(SG(default_charset));
308
0
  } else {
309
0
    charset = SAPI_DEFAULT_CHARSET;
310
0
    charset_len = sizeof(SAPI_DEFAULT_CHARSET) - 1;
311
0
  }
312
313
938k
  if (*charset && strncasecmp(mimetype, "text/", 5) == 0) {
314
938k
    char *p;
315
316
938k
    *len = prefix_len + mimetype_len + sizeof("; charset=") - 1 + charset_len;
317
938k
    content_type = (char*)emalloc(*len + 1);
318
938k
    p = content_type + prefix_len;
319
938k
    memcpy(p, mimetype, mimetype_len);
320
938k
    p += mimetype_len;
321
938k
    memcpy(p, "; charset=", sizeof("; charset=") - 1);
322
938k
    p += sizeof("; charset=") - 1;
323
938k
    memcpy(p, charset, charset_len + 1);
324
0
  } else {
325
0
    *len = prefix_len + mimetype_len;
326
0
    content_type = (char*)emalloc(*len + 1);
327
0
    memcpy(content_type + prefix_len, mimetype, mimetype_len + 1);
328
0
  }
329
938k
  return content_type;
330
938k
}
331
332
333
SAPI_API char *sapi_get_default_content_type(void)
334
0
{
335
0
  uint32_t len;
336
337
0
  return get_default_content_type(0, &len);
338
0
}
339
340
341
SAPI_API void sapi_get_default_content_type_header(sapi_header_struct *default_header)
342
938k
{
343
938k
    uint32_t len;
344
345
938k
  default_header->header = get_default_content_type(sizeof("Content-type: ")-1, &len);
346
938k
  default_header->header_len = len;
347
938k
  memcpy(default_header->header, "Content-type: ", sizeof("Content-type: ") - 1);
348
938k
}
349
350
/*
351
 * Add charset on content-type header if the MIME type starts with
352
 * "text/", the default_charset directive is not empty and
353
 * there is not already a charset option in there.
354
 *
355
 * If "mimetype" is non-NULL, it should point to a pointer allocated
356
 * with emalloc().  If a charset is added, the string will be
357
 * re-allocated and the new length is returned.  If mimetype is
358
 * unchanged, 0 is returned.
359
 *
360
 */
361
SAPI_API size_t sapi_apply_default_charset(char **mimetype, size_t len)
362
{
363
  char *charset, *newtype;
364
  size_t newlen;
365
  charset = SG(default_charset) ? SG(default_charset) : SAPI_DEFAULT_CHARSET;
366
367
  if (*mimetype != NULL) {
368
    if (*charset && strncmp(*mimetype, "text/", 5) == 0 && strstr(*mimetype, "charset=") == NULL) {
369
      newlen = len + (sizeof(";charset=")-1) + strlen(charset);
370
      newtype = emalloc(newlen + 1);
371
      PHP_STRLCPY(newtype, *mimetype, newlen + 1, len);
372
      strlcat(newtype, ";charset=", newlen + 1);
373
      strlcat(newtype, charset, newlen + 1);
374
      efree(*mimetype);
375
      *mimetype = newtype;
376
      return newlen;
377
    }
378
  }
379
  return 0;
380
}
381
382
SAPI_API void sapi_activate_headers_only(void)
383
0
{
384
0
  if (SG(request_info).headers_read == 1)
385
0
    return;
386
0
  SG(request_info).headers_read = 1;
387
0
  zend_llist_init(&SG(sapi_headers).headers, sizeof(sapi_header_struct),
388
0
      (void (*)(void *)) sapi_free_header, 0);
389
0
  SG(sapi_headers).send_default_content_type = 1;
390
391
  /* SG(sapi_headers).http_response_code = 200; */
392
0
  SG(sapi_headers).http_status_line = NULL;
393
0
  SG(sapi_headers).mimetype = NULL;
394
0
  SG(read_post_bytes) = 0;
395
0
  SG(request_info).request_body = NULL;
396
0
  SG(request_info).current_user = NULL;
397
0
  SG(request_info).current_user_length = 0;
398
0
  SG(request_info).no_headers = 0;
399
0
  SG(request_info).post_entry = NULL;
400
0
  SG(global_request_time) = 0;
401
402
  /*
403
   * It's possible to override this general case in the activate() callback,
404
   * if necessary.
405
   */
406
0
  if (SG(request_info).request_method && !strcmp(SG(request_info).request_method, "HEAD")) {
407
0
    SG(request_info).headers_only = 1;
408
0
  } else {
409
0
    SG(request_info).headers_only = 0;
410
0
  }
411
0
  if (SG(server_context)) {
412
0
    SG(request_info).cookie_data = sapi_module.read_cookies();
413
0
    if (sapi_module.activate) {
414
0
      sapi_module.activate();
415
0
    }
416
0
  }
417
0
  if (sapi_module.input_filter_init ) {
418
0
    sapi_module.input_filter_init();
419
0
  }
420
0
}
421
422
/*
423
 * Called from php_request_startup() for every request.
424
 */
425
426
SAPI_API void sapi_activate(void)
427
943k
{
428
943k
  zend_llist_init(&SG(sapi_headers).headers, sizeof(sapi_header_struct), (void (*)(void *)) sapi_free_header, 0);
429
943k
  SG(sapi_headers).send_default_content_type = 1;
430
431
  /*
432
  SG(sapi_headers).http_response_code = 200;
433
  */
434
943k
  SG(sapi_headers).http_status_line = NULL;
435
943k
  SG(sapi_headers).mimetype = NULL;
436
943k
  SG(headers_sent) = 0;
437
943k
  ZVAL_UNDEF(&SG(callback_func));
438
943k
  SG(read_post_bytes) = 0;
439
943k
  SG(request_info).request_body = NULL;
440
943k
  SG(request_info).current_user = NULL;
441
943k
  SG(request_info).current_user_length = 0;
442
943k
  SG(request_info).no_headers = 0;
443
943k
  SG(request_info).post_entry = NULL;
444
943k
  SG(request_info).proto_num = 1000; /* Default to HTTP 1.0 */
445
943k
  SG(global_request_time) = 0;
446
943k
  SG(post_read) = 0;
447
  /* It's possible to override this general case in the activate() callback, if necessary. */
448
943k
  if (SG(request_info).request_method && !strcmp(SG(request_info).request_method, "HEAD")) {
449
0
    SG(request_info).headers_only = 1;
450
943k
  } else {
451
943k
    SG(request_info).headers_only = 0;
452
943k
  }
453
943k
  SG(rfc1867_uploaded_files) = NULL;
454
455
  /* Handle request method */
456
943k
  if (SG(server_context)) {
457
0
    if (PG(enable_post_data_reading)
458
0
    &&  SG(request_info).content_type
459
0
    &&  SG(request_info).request_method
460
0
    && !strcmp(SG(request_info).request_method, "POST")) {
461
      /* HTTP POST may contain form data to be processed into variables
462
       * depending on given content type */
463
0
      sapi_read_post_data();
464
0
    } else {
465
0
      SG(request_info).content_type_dup = NULL;
466
0
    }
467
468
    /* Cookies */
469
0
    SG(request_info).cookie_data = sapi_module.read_cookies();
470
0
  }
471
943k
  if (sapi_module.activate) {
472
0
    sapi_module.activate();
473
0
  }
474
943k
  if (sapi_module.input_filter_init) {
475
0
    sapi_module.input_filter_init();
476
0
  }
477
943k
}
478
479
480
static void sapi_send_headers_free(void)
481
1.88M
{
482
1.88M
  if (SG(sapi_headers).http_status_line) {
483
0
    efree(SG(sapi_headers).http_status_line);
484
0
    SG(sapi_headers).http_status_line = NULL;
485
0
  }
486
1.88M
}
487
488
SAPI_API void sapi_deactivate(void)
489
942k
{
490
942k
  zend_llist_destroy(&SG(sapi_headers).headers);
491
942k
  if (SG(request_info).request_body) {
492
0
    SG(request_info).request_body = NULL;
493
942k
  } else if (SG(server_context)) {
494
0
    if (!SG(post_read)) {
495
      /* make sure we've consumed all request input data */
496
0
      char dummy[SAPI_POST_BLOCK_SIZE];
497
0
      size_t read_bytes;
498
499
0
      do {
500
0
        read_bytes = sapi_read_post_block(dummy, SAPI_POST_BLOCK_SIZE);
501
0
      } while (SAPI_POST_BLOCK_SIZE == read_bytes);
502
0
    }
503
0
  }
504
942k
  if (SG(request_info).auth_user) {
505
0
    efree(SG(request_info).auth_user);
506
0
  }
507
942k
  if (SG(request_info).auth_password) {
508
0
    efree(SG(request_info).auth_password);
509
0
  }
510
942k
  if (SG(request_info).auth_digest) {
511
0
    efree(SG(request_info).auth_digest);
512
0
  }
513
942k
  if (SG(request_info).content_type_dup) {
514
0
    efree(SG(request_info).content_type_dup);
515
0
  }
516
942k
  if (SG(request_info).current_user) {
517
0
    efree(SG(request_info).current_user);
518
0
  }
519
942k
  if (sapi_module.deactivate) {
520
0
    sapi_module.deactivate();
521
0
  }
522
942k
  if (SG(rfc1867_uploaded_files)) {
523
0
    destroy_uploaded_files_hash();
524
0
  }
525
942k
  if (SG(sapi_headers).mimetype) {
526
0
    efree(SG(sapi_headers).mimetype);
527
0
    SG(sapi_headers).mimetype = NULL;
528
0
  }
529
942k
  sapi_send_headers_free();
530
942k
  SG(sapi_started) = 0;
531
942k
  SG(headers_sent) = 0;
532
942k
  SG(request_info).headers_read = 0;
533
942k
  SG(global_request_time) = 0;
534
942k
}
535
536
537
SAPI_API void sapi_initialize_empty_request(void)
538
4.06k
{
539
4.06k
  SG(server_context) = NULL;
540
4.06k
  SG(request_info).request_method = NULL;
541
4.06k
  SG(request_info).auth_digest = SG(request_info).auth_user = SG(request_info).auth_password = NULL;
542
4.06k
  SG(request_info).content_type_dup = NULL;
543
4.06k
}
544
545
546
static int sapi_extract_response_code(const char *header_line)
547
0
{
548
0
  int code = 200;
549
0
  const char *ptr;
550
551
0
  for (ptr = header_line; *ptr; ptr++) {
552
0
    if (*ptr == ' ' && *(ptr + 1) != ' ') {
553
0
      code = atoi(ptr + 1);
554
0
      break;
555
0
    }
556
0
  }
557
558
0
  return code;
559
0
}
560
561
562
static void sapi_update_response_code(int ncode)
563
0
{
564
  /* if the status code did not change, we do not want
565
     to change the status line, and no need to change the code */
566
0
  if (SG(sapi_headers).http_response_code == ncode) {
567
0
    return;
568
0
  }
569
570
0
  if (SG(sapi_headers).http_status_line) {
571
0
    efree(SG(sapi_headers).http_status_line);
572
0
    SG(sapi_headers).http_status_line = NULL;
573
0
  }
574
0
  SG(sapi_headers).http_response_code = ncode;
575
0
}
576
577
/*
578
 * since zend_llist_del_element only remove one matched item once,
579
 * we should remove them by ourself
580
 */
581
939k
static void sapi_remove_header(zend_llist *l, char *name, size_t len) {
582
939k
  sapi_header_struct *header;
583
939k
  zend_llist_element *next;
584
939k
  zend_llist_element *current=l->head;
585
586
939k
  while (current) {
587
0
    header = (sapi_header_struct *)(current->data);
588
0
    next = current->next;
589
0
    if (header->header_len > len && header->header[len] == ':'
590
0
        && !strncasecmp(header->header, name, len)) {
591
0
      if (current->prev) {
592
0
        current->prev->next = next;
593
0
      } else {
594
0
        l->head = next;
595
0
      }
596
0
      if (next) {
597
0
        next->prev = current->prev;
598
0
      } else {
599
0
        l->tail = current->prev;
600
0
      }
601
0
      sapi_free_header(header);
602
0
      efree(current);
603
0
      --l->count;
604
0
    }
605
0
    current = next;
606
0
  }
607
939k
}
608
609
SAPI_API int sapi_add_header_ex(const char *header_line, size_t header_line_len, zend_bool duplicate, zend_bool replace)
610
939k
{
611
939k
  sapi_header_line ctr = {0};
612
939k
  int r;
613
614
939k
  ctr.line = header_line;
615
939k
  ctr.line_len = header_line_len;
616
617
939k
  r = sapi_header_op(replace ? SAPI_HEADER_REPLACE : SAPI_HEADER_ADD,
618
939k
      &ctr);
619
620
939k
  if (!duplicate)
621
0
    efree((void *) header_line);
622
623
939k
  return r;
624
939k
}
625
626
static void sapi_header_add_op(sapi_header_op_enum op, sapi_header_struct *sapi_header)
627
939k
{
628
939k
  if (!sapi_module.header_handler ||
629
939k
    (SAPI_HEADER_ADD & sapi_module.header_handler(sapi_header, op, &SG(sapi_headers)))) {
630
939k
    if (op == SAPI_HEADER_REPLACE) {
631
939k
      char *colon_offset = strchr(sapi_header->header, ':');
632
633
939k
      if (colon_offset) {
634
939k
        char sav = *colon_offset;
635
636
939k
        *colon_offset = 0;
637
939k
            sapi_remove_header(&SG(sapi_headers).headers, sapi_header->header, strlen(sapi_header->header));
638
939k
        *colon_offset = sav;
639
939k
      }
640
939k
    }
641
939k
    zend_llist_add_element(&SG(sapi_headers).headers, (void *) sapi_header);
642
0
  } else {
643
0
    sapi_free_header(sapi_header);
644
0
  }
645
939k
}
646
647
SAPI_API int sapi_header_op(sapi_header_op_enum op, void *arg)
648
939k
{
649
939k
  sapi_header_struct sapi_header;
650
939k
  char *colon_offset;
651
939k
  char *header_line;
652
939k
  size_t header_line_len;
653
939k
  int http_response_code;
654
655
939k
  if (SG(headers_sent) && !SG(request_info).no_headers) {
656
0
    const char *output_start_filename = php_output_get_start_filename();
657
0
    int output_start_lineno = php_output_get_start_lineno();
658
659
0
    if (output_start_filename) {
660
0
      sapi_module.sapi_error(E_WARNING, "Cannot modify header information - headers already sent by (output started at %s:%d)",
661
0
        output_start_filename, output_start_lineno);
662
0
    } else {
663
0
      sapi_module.sapi_error(E_WARNING, "Cannot modify header information - headers already sent");
664
0
    }
665
0
    return FAILURE;
666
0
  }
667
668
939k
  switch (op) {
669
0
    case SAPI_HEADER_SET_STATUS:
670
0
      sapi_update_response_code((int)(zend_intptr_t) arg);
671
0
      return SUCCESS;
672
673
0
    case SAPI_HEADER_ADD:
674
939k
    case SAPI_HEADER_REPLACE:
675
939k
    case SAPI_HEADER_DELETE: {
676
939k
        sapi_header_line *p = arg;
677
678
939k
        if (!p->line || !p->line_len) {
679
0
          return FAILURE;
680
0
        }
681
939k
        header_line = estrndup(p->line, p->line_len);
682
939k
        header_line_len = p->line_len;
683
939k
        http_response_code = p->response_code;
684
939k
        break;
685
939k
      }
686
687
0
    case SAPI_HEADER_DELETE_ALL:
688
0
      if (sapi_module.header_handler) {
689
0
        sapi_module.header_handler(&sapi_header, op, &SG(sapi_headers));
690
0
      }
691
0
      zend_llist_clean(&SG(sapi_headers).headers);
692
0
      return SUCCESS;
693
694
0
    default:
695
0
      return FAILURE;
696
939k
  }
697
698
  /* cut off trailing spaces, linefeeds and carriage-returns */
699
939k
  if (header_line_len && isspace(header_line[header_line_len-1])) {
700
0
    do {
701
0
      header_line_len--;
702
0
    } while(header_line_len && isspace(header_line[header_line_len-1]));
703
0
    header_line[header_line_len]='\0';
704
0
  }
705
706
939k
  if (op == SAPI_HEADER_DELETE) {
707
0
    if (strchr(header_line, ':')) {
708
0
      efree(header_line);
709
0
      sapi_module.sapi_error(E_WARNING, "Header to delete may not contain colon.");
710
0
      return FAILURE;
711
0
    }
712
0
    if (sapi_module.header_handler) {
713
0
      sapi_header.header = header_line;
714
0
      sapi_header.header_len = header_line_len;
715
0
      sapi_module.header_handler(&sapi_header, op, &SG(sapi_headers));
716
0
    }
717
0
    sapi_remove_header(&SG(sapi_headers).headers, header_line, header_line_len);
718
0
    efree(header_line);
719
0
    return SUCCESS;
720
939k
  } else {
721
    /* new line/NUL character safety check */
722
939k
    uint32_t i;
723
26.2M
    for (i = 0; i < header_line_len; i++) {
724
      /* RFC 7230 ch. 3.2.4 deprecates folding support */
725
25.3M
      if (header_line[i] == '\n' || header_line[i] == '\r') {
726
0
        efree(header_line);
727
0
        sapi_module.sapi_error(E_WARNING, "Header may not contain "
728
0
            "more than a single header, new line detected");
729
0
        return FAILURE;
730
0
      }
731
25.3M
      if (header_line[i] == '\0') {
732
0
        efree(header_line);
733
0
        sapi_module.sapi_error(E_WARNING, "Header may not contain NUL bytes");
734
0
        return FAILURE;
735
0
      }
736
25.3M
    }
737
939k
  }
738
739
939k
  sapi_header.header = header_line;
740
939k
  sapi_header.header_len = header_line_len;
741
742
  /* Check the header for a few cases that we have special support for in SAPI */
743
939k
  if (header_line_len>=5
744
939k
    && !strncasecmp(header_line, "HTTP/", 5)) {
745
    /* filter out the response code */
746
0
    sapi_update_response_code(sapi_extract_response_code(header_line));
747
    /* sapi_update_response_code doesn't free the status line if the code didn't change */
748
0
    if (SG(sapi_headers).http_status_line) {
749
0
      efree(SG(sapi_headers).http_status_line);
750
0
    }
751
0
    SG(sapi_headers).http_status_line = header_line;
752
0
    return SUCCESS;
753
939k
  } else {
754
939k
    colon_offset = strchr(header_line, ':');
755
939k
    if (colon_offset) {
756
939k
      *colon_offset = 0;
757
939k
      if (!strcasecmp(header_line, "Content-Type")) {
758
0
        char *ptr = colon_offset+1, *mimetype = NULL, *newheader;
759
0
        size_t len = header_line_len - (ptr - header_line), newlen;
760
0
        while (*ptr == ' ') {
761
0
          ptr++;
762
0
          len--;
763
0
        }
764
765
        /* Disable possible output compression for images */
766
0
        if (!strncmp(ptr, "image/", sizeof("image/")-1)) {
767
0
          zend_string *key = zend_string_init("zlib.output_compression", sizeof("zlib.output_compression")-1, 0);
768
0
          zend_alter_ini_entry_chars(key, "0", sizeof("0") - 1, PHP_INI_USER, PHP_INI_STAGE_RUNTIME);
769
0
          zend_string_release_ex(key, 0);
770
0
        }
771
772
0
        mimetype = estrdup(ptr);
773
0
        newlen = sapi_apply_default_charset(&mimetype, len);
774
0
        if (!SG(sapi_headers).mimetype){
775
0
          SG(sapi_headers).mimetype = estrdup(mimetype);
776
0
        }
777
778
0
        if (newlen != 0) {
779
0
          newlen += sizeof("Content-type: ");
780
0
          newheader = emalloc(newlen);
781
0
          PHP_STRLCPY(newheader, "Content-type: ", newlen, sizeof("Content-type: ")-1);
782
0
          strlcat(newheader, mimetype, newlen);
783
0
          sapi_header.header = newheader;
784
0
          sapi_header.header_len = (uint32_t)(newlen - 1);
785
0
          efree(header_line);
786
0
        }
787
0
        efree(mimetype);
788
0
        SG(sapi_headers).send_default_content_type = 0;
789
939k
      } else if (!strcasecmp(header_line, "Content-Length")) {
790
        /* Script is setting Content-length. The script cannot reasonably
791
         * know the size of the message body after compression, so it's best
792
         * do disable compression altogether. This contributes to making scripts
793
         * portable between setups that have and don't have zlib compression
794
         * enabled globally. See req #44164 */
795
0
        zend_string *key = zend_string_init("zlib.output_compression", sizeof("zlib.output_compression")-1, 0);
796
0
        zend_alter_ini_entry_chars(key,
797
0
          "0", sizeof("0") - 1, PHP_INI_USER, PHP_INI_STAGE_RUNTIME);
798
0
        zend_string_release_ex(key, 0);
799
939k
      } else if (!strcasecmp(header_line, "Location")) {
800
0
        if ((SG(sapi_headers).http_response_code < 300 ||
801
0
          SG(sapi_headers).http_response_code > 399) &&
802
0
          SG(sapi_headers).http_response_code != 201) {
803
          /* Return a Found Redirect if one is not already specified */
804
0
          if (http_response_code) { /* user specified redirect code */
805
0
            sapi_update_response_code(http_response_code);
806
0
          } else if (SG(request_info).proto_num > 1000 &&
807
0
             SG(request_info).request_method &&
808
0
             strcmp(SG(request_info).request_method, "HEAD") &&
809
0
             strcmp(SG(request_info).request_method, "GET")) {
810
0
            sapi_update_response_code(303);
811
0
          } else {
812
0
            sapi_update_response_code(302);
813
0
          }
814
0
        }
815
939k
      } else if (!strcasecmp(header_line, "WWW-Authenticate")) { /* HTTP Authentication */
816
0
        sapi_update_response_code(401); /* authentication-required */
817
0
      }
818
939k
      if (sapi_header.header==header_line) {
819
939k
        *colon_offset = ':';
820
939k
      }
821
939k
    }
822
939k
  }
823
939k
  if (http_response_code) {
824
0
    sapi_update_response_code(http_response_code);
825
0
  }
826
939k
  sapi_header_add_op(op, &sapi_header);
827
939k
  return SUCCESS;
828
939k
}
829
830
831
SAPI_API int sapi_send_headers(void)
832
938k
{
833
938k
  int retval;
834
938k
  int ret = FAILURE;
835
836
938k
  if (SG(headers_sent) || SG(request_info).no_headers) {
837
0
    return SUCCESS;
838
0
  }
839
840
  /* Success-oriented.  We set headers_sent to 1 here to avoid an infinite loop
841
   * in case of an error situation.
842
   */
843
938k
  if (SG(sapi_headers).send_default_content_type && sapi_module.send_headers) {
844
0
      uint32_t len = 0;
845
0
    char *default_mimetype = get_default_content_type(0, &len);
846
847
0
    if (default_mimetype && len) {
848
0
      sapi_header_struct default_header;
849
850
0
      SG(sapi_headers).mimetype = default_mimetype;
851
852
0
      default_header.header_len = sizeof("Content-type: ") - 1 + len;
853
0
      default_header.header = emalloc(default_header.header_len + 1);
854
855
0
      memcpy(default_header.header, "Content-type: ", sizeof("Content-type: ") - 1);
856
0
      memcpy(default_header.header + sizeof("Content-type: ") - 1, SG(sapi_headers).mimetype, len + 1);
857
858
0
      sapi_header_add_op(SAPI_HEADER_ADD, &default_header);
859
0
    } else {
860
0
      efree(default_mimetype);
861
0
    }
862
0
    SG(sapi_headers).send_default_content_type = 0;
863
0
  }
864
865
938k
  if (Z_TYPE(SG(callback_func)) != IS_UNDEF) {
866
0
    zval cb;
867
0
    ZVAL_COPY_VALUE(&cb, &SG(callback_func));
868
0
    ZVAL_UNDEF(&SG(callback_func));
869
0
    sapi_run_header_callback(&cb);
870
0
    zval_ptr_dtor(&cb);
871
0
  }
872
873
938k
  SG(headers_sent) = 1;
874
875
938k
  if (sapi_module.send_headers) {
876
0
    retval = sapi_module.send_headers(&SG(sapi_headers));
877
938k
  } else {
878
938k
    retval = SAPI_HEADER_DO_SEND;
879
938k
  }
880
881
938k
  switch (retval) {
882
0
    case SAPI_HEADER_SENT_SUCCESSFULLY:
883
0
      ret = SUCCESS;
884
0
      break;
885
938k
    case SAPI_HEADER_DO_SEND: {
886
938k
        sapi_header_struct http_status_line;
887
938k
        char buf[255];
888
889
938k
        if (SG(sapi_headers).http_status_line) {
890
0
          http_status_line.header = SG(sapi_headers).http_status_line;
891
0
          http_status_line.header_len = (uint32_t)strlen(SG(sapi_headers).http_status_line);
892
938k
        } else {
893
938k
          http_status_line.header = buf;
894
938k
          http_status_line.header_len = slprintf(buf, sizeof(buf), "HTTP/1.0 %d X", SG(sapi_headers).http_response_code);
895
938k
        }
896
938k
        sapi_module.send_header(&http_status_line, SG(server_context));
897
938k
      }
898
938k
      zend_llist_apply_with_argument(&SG(sapi_headers).headers, (llist_apply_with_arg_func_t) sapi_module.send_header, SG(server_context));
899
938k
      if(SG(sapi_headers).send_default_content_type) {
900
938k
        sapi_header_struct default_header;
901
902
938k
        sapi_get_default_content_type_header(&default_header);
903
938k
        sapi_module.send_header(&default_header, SG(server_context));
904
938k
        sapi_free_header(&default_header);
905
938k
      }
906
938k
      sapi_module.send_header(NULL, SG(server_context));
907
938k
      ret = SUCCESS;
908
938k
      break;
909
0
    case SAPI_HEADER_SEND_FAILED:
910
0
      SG(headers_sent) = 0;
911
0
      ret = FAILURE;
912
0
      break;
913
938k
  }
914
915
938k
  sapi_send_headers_free();
916
917
938k
  return ret;
918
938k
}
919
920
921
SAPI_API int sapi_register_post_entries(const sapi_post_entry *post_entries)
922
8.13k
{
923
8.13k
  const sapi_post_entry *p=post_entries;
924
925
20.3k
  while (p->content_type) {
926
16.2k
    if (sapi_register_post_entry(p) == FAILURE) {
927
4.06k
      return FAILURE;
928
4.06k
    }
929
12.2k
    p++;
930
12.2k
  }
931
4.06k
  return SUCCESS;
932
8.13k
}
933
934
935
SAPI_API int sapi_register_post_entry(const sapi_post_entry *post_entry)
936
16.2k
{
937
16.2k
  int ret;
938
16.2k
  zend_string *key;
939
16.2k
  if (SG(sapi_started) && EG(current_execute_data)) {
940
0
    return FAILURE;
941
0
  }
942
16.2k
  key = zend_string_init(post_entry->content_type, post_entry->content_type_len, 1);
943
16.2k
  GC_MAKE_PERSISTENT_LOCAL(key);
944
16.2k
  ret = zend_hash_add_mem(&SG(known_post_content_types), key,
945
12.2k
      (void *) post_entry, sizeof(sapi_post_entry)) ? SUCCESS : FAILURE;
946
16.2k
  zend_string_release_ex(key, 1);
947
16.2k
  return ret;
948
16.2k
}
949
950
SAPI_API void sapi_unregister_post_entry(const sapi_post_entry *post_entry)
951
4.06k
{
952
4.06k
  if (SG(sapi_started) && EG(current_execute_data)) {
953
0
    return;
954
0
  }
955
4.06k
  zend_hash_str_del(&SG(known_post_content_types), post_entry->content_type,
956
4.06k
      post_entry->content_type_len);
957
4.06k
}
958
959
960
SAPI_API int sapi_register_default_post_reader(void (*default_post_reader)(void))
961
4.06k
{
962
4.06k
  if (SG(sapi_started) && EG(current_execute_data)) {
963
0
    return FAILURE;
964
0
  }
965
4.06k
  sapi_module.default_post_reader = default_post_reader;
966
4.06k
  return SUCCESS;
967
4.06k
}
968
969
970
SAPI_API int sapi_register_treat_data(void (*treat_data)(int arg, char *str, zval *destArray))
971
8.13k
{
972
8.13k
  if (SG(sapi_started) && EG(current_execute_data)) {
973
0
    return FAILURE;
974
0
  }
975
8.13k
  sapi_module.treat_data = treat_data;
976
8.13k
  return SUCCESS;
977
8.13k
}
978
979
SAPI_API int sapi_register_input_filter(unsigned int (*input_filter)(int arg, const char *var, char **val, size_t val_len, size_t *new_val_len), unsigned int (*input_filter_init)(void))
980
4.06k
{
981
4.06k
  if (SG(sapi_started) && EG(current_execute_data)) {
982
0
    return FAILURE;
983
0
  }
984
4.06k
  sapi_module.input_filter = input_filter;
985
4.06k
  sapi_module.input_filter_init = input_filter_init;
986
4.06k
  return SUCCESS;
987
4.06k
}
988
989
SAPI_API int sapi_flush(void)
990
37.5M
{
991
37.5M
  if (sapi_module.flush) {
992
37.5M
    sapi_module.flush(SG(server_context));
993
37.5M
    return SUCCESS;
994
0
  } else {
995
0
    return FAILURE;
996
0
  }
997
37.5M
}
998
999
SAPI_API zend_stat_t *sapi_get_stat(void)
1000
0
{
1001
0
  if (sapi_module.get_stat) {
1002
0
    return sapi_module.get_stat();
1003
0
  } else {
1004
0
    if (!SG(request_info).path_translated || (VCWD_STAT(SG(request_info).path_translated, &SG(global_stat)) == -1)) {
1005
0
      return NULL;
1006
0
    }
1007
0
    return &SG(global_stat);
1008
0
  }
1009
0
}
1010
1011
SAPI_API char *sapi_getenv(const char *name, size_t name_len)
1012
739
{
1013
739
  if (!strncasecmp(name, "HTTP_PROXY", name_len)) {
1014
    /* Ugly fix for HTTP_PROXY issue, see bug #72573 */
1015
15
    return NULL;
1016
15
  }
1017
724
  if (sapi_module.getenv) {
1018
0
    char *value, *tmp = sapi_module.getenv(name, name_len);
1019
0
    if (tmp) {
1020
0
      value = estrdup(tmp);
1021
#ifdef PHP_WIN32
1022
      if (strlen(sapi_module.name) == sizeof("cgi-fcgi") - 1 && !strcmp(sapi_module.name, "cgi-fcgi")) {
1023
        /* XXX more modules to go, if needed. */
1024
        free(tmp);
1025
      }
1026
#endif
1027
0
    } else {
1028
0
      return NULL;
1029
0
    }
1030
0
    if (sapi_module.input_filter) {
1031
0
      sapi_module.input_filter(PARSE_STRING, name, &value, strlen(value), NULL);
1032
0
    }
1033
0
    return value;
1034
0
  }
1035
724
  return NULL;
1036
724
}
1037
1038
SAPI_API int sapi_get_fd(int *fd)
1039
0
{
1040
0
  if (sapi_module.get_fd) {
1041
0
    return sapi_module.get_fd(fd);
1042
0
  } else {
1043
0
    return FAILURE;
1044
0
  }
1045
0
}
1046
1047
SAPI_API int sapi_force_http_10(void)
1048
0
{
1049
0
  if (sapi_module.force_http_10) {
1050
0
    return sapi_module.force_http_10();
1051
0
  } else {
1052
0
    return FAILURE;
1053
0
  }
1054
0
}
1055
1056
1057
SAPI_API int sapi_get_target_uid(uid_t *obj)
1058
0
{
1059
0
  if (sapi_module.get_target_uid) {
1060
0
    return sapi_module.get_target_uid(obj);
1061
0
  } else {
1062
0
    return FAILURE;
1063
0
  }
1064
0
}
1065
1066
SAPI_API int sapi_get_target_gid(gid_t *obj)
1067
0
{
1068
0
  if (sapi_module.get_target_gid) {
1069
0
    return sapi_module.get_target_gid(obj);
1070
0
  } else {
1071
0
    return FAILURE;
1072
0
  }
1073
0
}
1074
1075
SAPI_API double sapi_get_request_time(void)
1076
123
{
1077
123
  if(SG(global_request_time)) return SG(global_request_time);
1078
1079
123
  if (sapi_module.get_request_time && SG(server_context)) {
1080
0
    SG(global_request_time) = sapi_module.get_request_time();
1081
123
  } else {
1082
123
    struct timeval tp = {0};
1083
123
    if (!gettimeofday(&tp, NULL)) {
1084
123
      SG(global_request_time) = (double)(tp.tv_sec + tp.tv_usec / 1000000.00);
1085
0
    } else {
1086
0
      SG(global_request_time) = (double)time(0);
1087
0
    }
1088
123
  }
1089
123
  return SG(global_request_time);
1090
123
}
1091
1092
0
SAPI_API void sapi_terminate_process(void) {
1093
0
  if (sapi_module.terminate_process) {
1094
0
    sapi_module.terminate_process();
1095
0
  }
1096
0
}
1097
1098
SAPI_API void sapi_add_request_header(const char *var, unsigned int var_len, char *val, unsigned int val_len, void *arg) /* {{{ */
1099
0
{
1100
0
  zval *return_value = (zval*)arg;
1101
0
  char *buf = NULL;
1102
1103
0
  ALLOCA_FLAG(use_heap)
1104
1105
0
  if (var_len > 5 &&
1106
0
      var[0] == 'H' &&
1107
0
      var[1] == 'T' &&
1108
0
      var[2] == 'T' &&
1109
0
      var[3] == 'P' &&
1110
0
      var[4] == '_') {
1111
1112
0
    const char *p;
1113
0
    char *str;
1114
1115
0
    var_len -= 5;
1116
0
    p = var + 5;
1117
0
    var = str = buf = do_alloca(var_len + 1, use_heap);
1118
0
    *str++ = *p++;
1119
0
    while (*p) {
1120
0
      if (*p == '_') {
1121
0
        *str++ = '-';
1122
0
        p++;
1123
0
        if (*p) {
1124
0
          *str++ = *p++;
1125
0
        }
1126
0
      } else if (*p >= 'A' && *p <= 'Z') {
1127
0
        *str++ = (*p++ - 'A' + 'a');
1128
0
      } else {
1129
0
        *str++ = *p++;
1130
0
      }
1131
0
    }
1132
0
    *str = 0;
1133
0
  } else if (var_len == sizeof("CONTENT_TYPE")-1 &&
1134
0
             memcmp(var, "CONTENT_TYPE", sizeof("CONTENT_TYPE")-1) == 0) {
1135
0
    var = "Content-Type";
1136
0
  } else if (var_len == sizeof("CONTENT_LENGTH")-1 &&
1137
0
             memcmp(var, "CONTENT_LENGTH", sizeof("CONTENT_LENGTH")-1) == 0) {
1138
0
    var = "Content-Length";
1139
0
  } else {
1140
0
    return;
1141
0
  }
1142
0
  add_assoc_stringl_ex(return_value, var, var_len, val, val_len);
1143
0
  if (buf) {
1144
0
    free_alloca(buf, use_heap);
1145
0
  }
1146
0
}
1147
/* }}} */