Coverage Report

Created: 2026-09-14 06:25

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