Coverage Report

Created: 2026-02-14 06:52

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