Coverage Report

Created: 2025-09-27 06:26

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
556k
{
107
556k
  efree(sapi_header->header);
108
556k
}
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
278k
{
301
278k
  char *mimetype, *charset, *content_type;
302
278k
  uint32_t mimetype_len, charset_len;
303
304
278k
  if (SG(default_mimetype)) {
305
278k
    mimetype = SG(default_mimetype);
306
278k
    mimetype_len = (uint32_t)strlen(SG(default_mimetype));
307
278k
  } else {
308
0
    mimetype = SAPI_DEFAULT_MIMETYPE;
309
0
    mimetype_len = sizeof(SAPI_DEFAULT_MIMETYPE) - 1;
310
0
  }
311
278k
  if (SG(default_charset)) {
312
278k
    charset = SG(default_charset);
313
278k
    charset_len = (uint32_t)strlen(SG(default_charset));
314
278k
  } else {
315
0
    charset = SAPI_DEFAULT_CHARSET;
316
0
    charset_len = sizeof(SAPI_DEFAULT_CHARSET) - 1;
317
0
  }
318
319
278k
  if (*charset && strncasecmp(mimetype, "text/", 5) == 0) {
320
278k
    char *p;
321
322
278k
    *len = prefix_len + mimetype_len + sizeof("; charset=") - 1 + charset_len;
323
278k
    content_type = (char*)emalloc(*len + 1);
324
278k
    p = content_type + prefix_len;
325
278k
    p = zend_mempcpy(p, mimetype, mimetype_len);
326
278k
    p = zend_mempcpy(p, "; charset=", sizeof("; charset=") - 1);
327
278k
    memcpy(p, charset, charset_len + 1);
328
278k
  } 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
278k
  return content_type;
334
278k
}
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
278k
{
347
278k
  uint32_t len;
348
349
278k
  default_header->header = get_default_content_type(sizeof("Content-type: ")-1, &len);
350
278k
  default_header->header_len = len;
351
278k
  memcpy(default_header->header, "Content-type: ", sizeof("Content-type: ") - 1);
352
278k
}
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
278k
{
432
278k
  zend_llist_init(&SG(sapi_headers).headers, sizeof(sapi_header_struct), (void (*)(void *)) sapi_free_header, 0);
433
278k
  SG(sapi_headers).send_default_content_type = 1;
434
435
  /*
436
  SG(sapi_headers).http_response_code = 200;
437
  */
438
278k
  SG(sapi_headers).http_status_line = NULL;
439
278k
  SG(sapi_headers).mimetype = NULL;
440
278k
  SG(headers_sent) = 0;
441
278k
  ZVAL_UNDEF(&SG(callback_func));
442
278k
  SG(read_post_bytes) = 0;
443
278k
  SG(request_info).request_body = NULL;
444
278k
  SG(request_info).current_user = NULL;
445
278k
  SG(request_info).current_user_length = 0;
446
278k
  SG(request_info).no_headers = 0;
447
278k
  SG(request_info).post_entry = NULL;
448
278k
  SG(request_info).proto_num = 1000; /* Default to HTTP 1.0 */
449
278k
  SG(global_request_time) = 0;
450
278k
  SG(post_read) = 0;
451
  /* It's possible to override this general case in the activate() callback, if necessary. */
452
278k
  if (SG(request_info).request_method && !strcmp(SG(request_info).request_method, "HEAD")) {
453
0
    SG(request_info).headers_only = 1;
454
278k
  } else {
455
278k
    SG(request_info).headers_only = 0;
456
278k
  }
457
278k
  SG(rfc1867_uploaded_files) = NULL;
458
278k
  SG(request_parse_body_context).throw_exceptions = false;
459
278k
  memset(&SG(request_parse_body_context).options_cache, 0, sizeof(SG(request_parse_body_context).options_cache));
460
461
278k
  if (sapi_module.pre_request_init) {
462
0
    sapi_module.pre_request_init();
463
0
  }
464
465
  /* Handle request method */
466
278k
  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
278k
  if (sapi_module.activate) {
482
0
    sapi_module.activate();
483
0
  }
484
278k
  if (sapi_module.input_filter_init) {
485
0
    sapi_module.input_filter_init();
486
0
  }
487
278k
}
488
489
490
static void sapi_send_headers_free(void)
491
556k
{
492
556k
  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
556k
}
497
498
SAPI_API void sapi_deactivate_module(void)
499
278k
{
500
278k
  zend_llist_destroy(&SG(sapi_headers).headers);
501
278k
  if (SG(request_info).request_body) {
502
0
    SG(request_info).request_body = NULL;
503
278k
  } 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
278k
  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
278k
  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
278k
  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
278k
  if (SG(request_info).content_type_dup) {
527
0
    efree(SG(request_info).content_type_dup);
528
0
  }
529
278k
  if (SG(request_info).current_user) {
530
0
    efree(SG(request_info).current_user);
531
0
  }
532
278k
  if (sapi_module.deactivate) {
533
0
    sapi_module.deactivate();
534
0
  }
535
278k
}
536
537
SAPI_API void sapi_deactivate_destroy(void)
538
278k
{
539
278k
  if (SG(rfc1867_uploaded_files)) {
540
0
    destroy_uploaded_files_hash();
541
0
  }
542
278k
  if (SG(sapi_headers).mimetype) {
543
0
    efree(SG(sapi_headers).mimetype);
544
0
    SG(sapi_headers).mimetype = NULL;
545
0
  }
546
278k
  sapi_send_headers_free();
547
278k
  SG(sapi_started) = 0;
548
278k
  SG(headers_sent) = 0;
549
278k
  SG(request_info).headers_read = 0;
550
278k
  SG(global_request_time) = 0;
551
278k
}
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
10
{
571
10
  int code = 200;
572
10
  const char *ptr;
573
574
748
  for (ptr = header_line; *ptr; ptr++) {
575
742
    if (*ptr == ' ' && *(ptr + 1) != ' ') {
576
4
      code = atoi(ptr + 1);
577
4
      break;
578
4
    }
579
742
  }
580
581
10
  return code;
582
10
}
583
584
585
static void sapi_update_response_code(int ncode)
586
10
{
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
10
  if (SG(sapi_headers).http_response_code == ncode) {
590
2
    return;
591
2
  }
592
593
8
  if (SG(sapi_headers).http_status_line) {
594
4
    efree(SG(sapi_headers).http_status_line);
595
4
    SG(sapi_headers).http_status_line = NULL;
596
4
  }
597
8
  SG(sapi_headers).http_response_code = ncode;
598
8
}
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 header_len)
605
278k
{
606
278k
  sapi_header_struct *header;
607
278k
  zend_llist_element *next;
608
278k
  zend_llist_element *current=l->head;
609
610
278k
  while (current) {
611
4
    header = (sapi_header_struct *)(current->data);
612
4
    next = current->next;
613
4
    if (header->header_len > header_len
614
4
        && (header->header[header_len] == ':' || len > header_len)
615
4
        && !strncasecmp(header->header, name, len)) {
616
0
      if (current->prev) {
617
0
        current->prev->next = next;
618
0
      } else {
619
0
        l->head = next;
620
0
      }
621
0
      if (next) {
622
0
        next->prev = current->prev;
623
0
      } else {
624
0
        l->tail = current->prev;
625
0
      }
626
0
      sapi_free_header(header);
627
0
      efree(current);
628
0
      --l->count;
629
0
    }
630
4
    current = next;
631
4
  }
632
278k
}
633
634
SAPI_API int sapi_add_header_ex(const char *header_line, size_t header_line_len, bool duplicate, bool replace)
635
278k
{
636
278k
  sapi_header_line ctr = {0};
637
278k
  int r;
638
639
278k
  ctr.line = header_line;
640
278k
  ctr.line_len = header_line_len;
641
642
278k
  r = sapi_header_op(replace ? SAPI_HEADER_REPLACE : SAPI_HEADER_ADD,
643
278k
      &ctr);
644
645
278k
  if (!duplicate)
646
0
    efree((void *) header_line);
647
648
278k
  return r;
649
278k
}
650
651
static void sapi_header_add_op(sapi_header_op_enum op, sapi_header_struct *sapi_header)
652
278k
{
653
278k
  if (!sapi_module.header_handler ||
654
278k
    (SAPI_HEADER_ADD & sapi_module.header_handler(sapi_header, op, &SG(sapi_headers)))) {
655
278k
    if (op == SAPI_HEADER_REPLACE) {
656
278k
      char *colon_offset = strchr(sapi_header->header, ':');
657
658
278k
      if (colon_offset) {
659
278k
        char sav = *colon_offset;
660
661
278k
        *colon_offset = 0;
662
278k
        sapi_remove_header(&SG(sapi_headers).headers, sapi_header->header, strlen(sapi_header->header), 0);
663
278k
        *colon_offset = sav;
664
278k
      }
665
278k
    }
666
278k
    zend_llist_add_element(&SG(sapi_headers).headers, (void *) sapi_header);
667
278k
  } else {
668
0
    sapi_free_header(sapi_header);
669
0
  }
670
278k
}
671
672
SAPI_API int sapi_header_op(sapi_header_op_enum op, void *arg)
673
278k
{
674
278k
  sapi_header_struct sapi_header;
675
278k
  char *colon_offset;
676
278k
  char *header_line;
677
278k
  size_t header_line_len, header_len;
678
278k
  int http_response_code;
679
680
278k
  if (SG(headers_sent) && !SG(request_info).no_headers) {
681
12
    const char *output_start_filename = php_output_get_start_filename();
682
12
    int output_start_lineno = php_output_get_start_lineno();
683
684
12
    if (output_start_filename) {
685
12
      sapi_module.sapi_error(E_WARNING, "Cannot modify header information - headers already sent by (output started at %s:%d)",
686
12
        output_start_filename, output_start_lineno);
687
12
    } else {
688
0
      sapi_module.sapi_error(E_WARNING, "Cannot modify header information - headers already sent");
689
0
    }
690
12
    return FAILURE;
691
12
  }
692
693
278k
  switch (op) {
694
0
    case SAPI_HEADER_SET_STATUS:
695
0
      sapi_update_response_code((int)(intptr_t) arg);
696
0
      return SUCCESS;
697
698
0
    case SAPI_HEADER_ADD:
699
278k
    case SAPI_HEADER_REPLACE:
700
278k
    case SAPI_HEADER_DELETE_PREFIX:
701
278k
    case SAPI_HEADER_DELETE: {
702
278k
        sapi_header_line *p = arg;
703
704
278k
        if (!p->line || !p->line_len) {
705
0
          return FAILURE;
706
0
        }
707
278k
        header_line = estrndup(p->line, p->line_len);
708
278k
        header_line_len = p->line_len;
709
278k
        if (op == SAPI_HEADER_DELETE_PREFIX) {
710
0
          header_len = p->header_len;
711
0
          http_response_code = 0;
712
278k
        } else {
713
278k
          header_len = 0;
714
278k
          http_response_code = p->response_code;
715
278k
        }
716
278k
        break;
717
278k
      }
718
719
0
    case SAPI_HEADER_DELETE_ALL:
720
0
      if (sapi_module.header_handler) {
721
0
        sapi_module.header_handler(&sapi_header, op, &SG(sapi_headers));
722
0
      }
723
0
      zend_llist_clean(&SG(sapi_headers).headers);
724
0
      return SUCCESS;
725
726
0
    default:
727
0
      return FAILURE;
728
278k
  }
729
730
  /* cut off trailing spaces, linefeeds and carriage-returns */
731
278k
  if (header_line_len && isspace(header_line[header_line_len-1])) {
732
24
    do {
733
24
      header_line_len--;
734
24
    } while(header_line_len && isspace(header_line[header_line_len-1]));
735
24
    header_line[header_line_len]='\0';
736
24
  }
737
738
278k
  if (op == SAPI_HEADER_DELETE || op == SAPI_HEADER_DELETE_PREFIX) {
739
0
    if (op == SAPI_HEADER_DELETE && strchr(header_line, ':')) {
740
0
      efree(header_line);
741
0
      sapi_module.sapi_error(E_WARNING, "Header to delete may not contain colon.");
742
0
      return FAILURE;
743
0
    }
744
0
    if (sapi_module.header_handler) {
745
0
      sapi_header.header = header_line;
746
0
      sapi_header.header_len = header_line_len;
747
0
      sapi_module.header_handler(&sapi_header, op, &SG(sapi_headers));
748
0
    }
749
0
    sapi_remove_header(&SG(sapi_headers).headers, header_line, header_line_len, header_len);
750
0
    efree(header_line);
751
0
    return SUCCESS;
752
278k
  } else {
753
    /* new line/NUL character safety check */
754
278k
    uint32_t i;
755
7.79M
    for (i = 0; i < header_line_len; i++) {
756
      /* RFC 7230 ch. 3.2.4 deprecates folding support */
757
7.52M
      if (header_line[i] == '\n' || header_line[i] == '\r') {
758
2
        efree(header_line);
759
2
        sapi_module.sapi_error(E_WARNING, "Header may not contain "
760
2
            "more than a single header, new line detected");
761
2
        return FAILURE;
762
2
      }
763
7.52M
      if (header_line[i] == '\0') {
764
11
        efree(header_line);
765
11
        sapi_module.sapi_error(E_WARNING, "Header may not contain NUL bytes");
766
11
        return FAILURE;
767
11
      }
768
7.52M
    }
769
278k
  }
770
771
278k
  sapi_header.header = header_line;
772
278k
  sapi_header.header_len = header_line_len;
773
774
  /* Check the header for a few cases that we have special support for in SAPI */
775
278k
  if (header_line_len>=5
776
278k
    && !strncasecmp(header_line, "HTTP/", 5)) {
777
    /* filter out the response code */
778
10
    sapi_update_response_code(sapi_extract_response_code(header_line));
779
    /* sapi_update_response_code doesn't free the status line if the code didn't change */
780
10
    if (SG(sapi_headers).http_status_line) {
781
0
      efree(SG(sapi_headers).http_status_line);
782
0
    }
783
10
    SG(sapi_headers).http_status_line = header_line;
784
10
    return SUCCESS;
785
278k
  } else {
786
278k
    colon_offset = strchr(header_line, ':');
787
278k
    if (colon_offset) {
788
278k
      *colon_offset = 0;
789
278k
      if (!strcasecmp(header_line, "Content-Type")) {
790
0
        char *ptr = colon_offset+1, *mimetype = NULL, *newheader;
791
0
        size_t len = header_line_len - (ptr - header_line), newlen;
792
0
        while (*ptr == ' ') {
793
0
          ptr++;
794
0
          len--;
795
0
        }
796
797
0
        mimetype = estrdup(ptr);
798
0
        newlen = sapi_apply_default_charset(&mimetype, len);
799
0
        if (!SG(sapi_headers).mimetype){
800
0
          SG(sapi_headers).mimetype = estrdup(mimetype);
801
0
        }
802
803
0
        if (newlen != 0) {
804
0
          newlen += sizeof("Content-type: ");
805
0
          newheader = emalloc(newlen);
806
0
          PHP_STRLCPY(newheader, "Content-type: ", newlen, sizeof("Content-type: ")-1);
807
0
          strlcat(newheader, mimetype, newlen);
808
0
          sapi_header.header = newheader;
809
0
          sapi_header.header_len = (uint32_t)(newlen - 1);
810
0
          efree(header_line);
811
0
        }
812
0
        efree(mimetype);
813
0
        SG(sapi_headers).send_default_content_type = 0;
814
278k
      } else if (!strcasecmp(header_line, "Content-Length")) {
815
        /* Script is setting Content-length. The script cannot reasonably
816
         * know the size of the message body after compression, so it's best
817
         * to disable compression altogether. This contributes to making scripts
818
         * portable between setups that have and don't have zlib compression
819
         * enabled globally. See req #44164 */
820
0
        zend_string *key = ZSTR_INIT_LITERAL("zlib.output_compression", 0);
821
0
        zend_alter_ini_entry_chars(key,
822
0
          "0", sizeof("0") - 1, PHP_INI_USER, PHP_INI_STAGE_RUNTIME);
823
0
        zend_string_release_ex(key, 0);
824
278k
      } else if (!strcasecmp(header_line, "Location")) {
825
0
        if ((SG(sapi_headers).http_response_code < 300 ||
826
0
          SG(sapi_headers).http_response_code > 399) &&
827
0
          SG(sapi_headers).http_response_code != 201) {
828
          /* Return a Found Redirect if one is not already specified */
829
0
          if (http_response_code) { /* user specified redirect code */
830
0
            sapi_update_response_code(http_response_code);
831
0
          } else if (SG(request_info).proto_num > 1000 &&
832
0
             SG(request_info).request_method &&
833
0
             strcmp(SG(request_info).request_method, "HEAD") &&
834
0
             strcmp(SG(request_info).request_method, "GET")) {
835
0
            sapi_update_response_code(303);
836
0
          } else {
837
0
            sapi_update_response_code(302);
838
0
          }
839
0
        }
840
278k
      } else if (!strcasecmp(header_line, "WWW-Authenticate")) { /* HTTP Authentication */
841
0
        sapi_update_response_code(401); /* authentication-required */
842
0
      }
843
278k
      if (sapi_header.header==header_line) {
844
278k
        *colon_offset = ':';
845
278k
      }
846
278k
    }
847
278k
  }
848
278k
  if (http_response_code) {
849
0
    sapi_update_response_code(http_response_code);
850
0
  }
851
278k
  sapi_header_add_op(op, &sapi_header);
852
278k
  return SUCCESS;
853
278k
}
854
855
856
SAPI_API int sapi_send_headers(void)
857
278k
{
858
278k
  int retval;
859
278k
  int ret = FAILURE;
860
861
278k
  if (SG(headers_sent) || SG(request_info).no_headers) {
862
0
    return SUCCESS;
863
0
  }
864
865
  /* Success-oriented. We set headers_sent to 1 here to avoid an infinite loop
866
   * in case of an error situation.
867
   */
868
278k
  if (SG(sapi_headers).send_default_content_type && sapi_module.send_headers) {
869
0
      uint32_t len = 0;
870
0
    char *default_mimetype = get_default_content_type(0, &len);
871
872
0
    if (default_mimetype && len) {
873
0
      sapi_header_struct default_header;
874
875
0
      SG(sapi_headers).mimetype = default_mimetype;
876
877
0
      default_header.header_len = sizeof("Content-type: ") - 1 + len;
878
0
      default_header.header = emalloc(default_header.header_len + 1);
879
880
0
      memcpy(default_header.header, "Content-type: ", sizeof("Content-type: ") - 1);
881
0
      memcpy(default_header.header + sizeof("Content-type: ") - 1, SG(sapi_headers).mimetype, len + 1);
882
883
0
      sapi_header_add_op(SAPI_HEADER_ADD, &default_header);
884
0
    } else {
885
0
      efree(default_mimetype);
886
0
    }
887
0
    SG(sapi_headers).send_default_content_type = 0;
888
0
  }
889
890
278k
  if (Z_TYPE(SG(callback_func)) != IS_UNDEF) {
891
0
    zval cb;
892
0
    ZVAL_COPY_VALUE(&cb, &SG(callback_func));
893
0
    ZVAL_UNDEF(&SG(callback_func));
894
0
    sapi_run_header_callback(&cb);
895
0
    zval_ptr_dtor(&cb);
896
0
  }
897
898
278k
  SG(headers_sent) = 1;
899
900
278k
  if (sapi_module.send_headers) {
901
0
    retval = sapi_module.send_headers(&SG(sapi_headers));
902
278k
  } else {
903
278k
    retval = SAPI_HEADER_DO_SEND;
904
278k
  }
905
906
278k
  switch (retval) {
907
0
    case SAPI_HEADER_SENT_SUCCESSFULLY:
908
0
      ret = SUCCESS;
909
0
      break;
910
278k
    case SAPI_HEADER_DO_SEND: {
911
278k
        sapi_header_struct http_status_line;
912
278k
        char buf[255];
913
914
278k
        if (SG(sapi_headers).http_status_line) {
915
6
          http_status_line.header = SG(sapi_headers).http_status_line;
916
6
          http_status_line.header_len = (uint32_t)strlen(SG(sapi_headers).http_status_line);
917
278k
        } else {
918
278k
          http_status_line.header = buf;
919
278k
          http_status_line.header_len = slprintf(buf, sizeof(buf), "HTTP/1.0 %d X", SG(sapi_headers).http_response_code);
920
278k
        }
921
278k
        sapi_module.send_header(&http_status_line, SG(server_context));
922
278k
      }
923
278k
      zend_llist_apply_with_argument(&SG(sapi_headers).headers, (llist_apply_with_arg_func_t) sapi_module.send_header, SG(server_context));
924
278k
      if(SG(sapi_headers).send_default_content_type) {
925
278k
        sapi_header_struct default_header;
926
927
278k
        sapi_get_default_content_type_header(&default_header);
928
278k
        sapi_module.send_header(&default_header, SG(server_context));
929
278k
        sapi_free_header(&default_header);
930
278k
      }
931
278k
      sapi_module.send_header(NULL, SG(server_context));
932
278k
      ret = SUCCESS;
933
278k
      break;
934
0
    case SAPI_HEADER_SEND_FAILED:
935
0
      SG(headers_sent) = 0;
936
0
      ret = FAILURE;
937
0
      break;
938
278k
  }
939
940
278k
  sapi_send_headers_free();
941
942
278k
  return ret;
943
278k
}
944
945
946
SAPI_API int sapi_register_post_entries(const sapi_post_entry *post_entries)
947
16
{
948
16
  const sapi_post_entry *p=post_entries;
949
950
48
  while (p->content_type) {
951
32
    if (sapi_register_post_entry(p) == FAILURE) {
952
0
      return FAILURE;
953
0
    }
954
32
    p++;
955
32
  }
956
16
  return SUCCESS;
957
16
}
958
959
960
SAPI_API int sapi_register_post_entry(const sapi_post_entry *post_entry)
961
32
{
962
32
  int ret;
963
32
  zend_string *key;
964
32
  if (SG(sapi_started) && EG(current_execute_data)) {
965
0
    return FAILURE;
966
0
  }
967
32
  key = zend_string_init(post_entry->content_type, post_entry->content_type_len, 1);
968
32
  GC_MAKE_PERSISTENT_LOCAL(key);
969
32
  ret = zend_hash_add_mem(&SG(known_post_content_types), key,
970
32
      (void *) post_entry, sizeof(sapi_post_entry)) ? SUCCESS : FAILURE;
971
32
  zend_string_release_ex(key, 1);
972
32
  return ret;
973
32
}
974
975
SAPI_API void sapi_unregister_post_entry(const sapi_post_entry *post_entry)
976
0
{
977
0
  if (SG(sapi_started) && EG(current_execute_data)) {
978
0
    return;
979
0
  }
980
0
  zend_hash_str_del(&SG(known_post_content_types), post_entry->content_type,
981
0
      post_entry->content_type_len);
982
0
}
983
984
985
SAPI_API int sapi_register_default_post_reader(void (*default_post_reader)(void))
986
16
{
987
16
  if (SG(sapi_started) && EG(current_execute_data)) {
988
0
    return FAILURE;
989
0
  }
990
16
  sapi_module.default_post_reader = default_post_reader;
991
16
  return SUCCESS;
992
16
}
993
994
995
SAPI_API int sapi_register_treat_data(void (*treat_data)(int arg, char *str, zval *destArray))
996
16
{
997
16
  if (SG(sapi_started) && EG(current_execute_data)) {
998
0
    return FAILURE;
999
0
  }
1000
16
  sapi_module.treat_data = treat_data;
1001
16
  return SUCCESS;
1002
16
}
1003
1004
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))
1005
16
{
1006
16
  if (SG(sapi_started) && EG(current_execute_data)) {
1007
0
    return FAILURE;
1008
0
  }
1009
16
  sapi_module.input_filter = input_filter;
1010
16
  sapi_module.input_filter_init = input_filter_init;
1011
16
  return SUCCESS;
1012
16
}
1013
1014
SAPI_API int sapi_flush(void)
1015
50.7M
{
1016
50.7M
  if (sapi_module.flush) {
1017
50.7M
    sapi_module.flush(SG(server_context));
1018
50.7M
    return SUCCESS;
1019
50.7M
  } else {
1020
0
    return FAILURE;
1021
0
  }
1022
50.7M
}
1023
1024
SAPI_API zend_stat_t *sapi_get_stat(void)
1025
0
{
1026
0
  if (sapi_module.get_stat) {
1027
0
    return sapi_module.get_stat();
1028
0
  } else {
1029
0
    if (!SG(request_info).path_translated || (VCWD_STAT(SG(request_info).path_translated, &SG(global_stat)) == -1)) {
1030
0
      return NULL;
1031
0
    }
1032
0
    return &SG(global_stat);
1033
0
  }
1034
0
}
1035
1036
SAPI_API char *sapi_getenv(const char *name, size_t name_len)
1037
44
{
1038
44
  char *value, *tmp;
1039
1040
44
  if (!sapi_module.getenv) {
1041
44
    return NULL;
1042
44
  }
1043
0
  if (!strncasecmp(name, "HTTP_PROXY", name_len)) {
1044
    /* Ugly fix for HTTP_PROXY issue, see bug #72573 */
1045
0
    return NULL;
1046
0
  }
1047
0
  tmp = sapi_module.getenv(name, name_len);
1048
0
  if (!tmp) {
1049
0
    return NULL;
1050
0
  }
1051
0
  value = estrdup(tmp);
1052
#ifdef PHP_WIN32
1053
  if (strlen(sapi_module.name) == sizeof("cgi-fcgi") - 1 && !strcmp(sapi_module.name, "cgi-fcgi")) {
1054
    /* XXX more modules to go, if needed. */
1055
    free(tmp);
1056
  }
1057
#endif
1058
0
  if (sapi_module.input_filter) {
1059
0
    sapi_module.input_filter(PARSE_STRING, name, &value, strlen(value), NULL);
1060
0
  }
1061
0
  return value;
1062
0
}
1063
1064
SAPI_API int sapi_get_fd(int *fd)
1065
0
{
1066
0
  if (sapi_module.get_fd) {
1067
0
    return sapi_module.get_fd(fd);
1068
0
  } else {
1069
0
    return FAILURE;
1070
0
  }
1071
0
}
1072
1073
SAPI_API int sapi_force_http_10(void)
1074
0
{
1075
0
  if (sapi_module.force_http_10) {
1076
0
    return sapi_module.force_http_10();
1077
0
  } else {
1078
0
    return FAILURE;
1079
0
  }
1080
0
}
1081
1082
1083
SAPI_API int sapi_get_target_uid(uid_t *obj)
1084
0
{
1085
0
  if (sapi_module.get_target_uid) {
1086
0
    return sapi_module.get_target_uid(obj);
1087
0
  } else {
1088
0
    return FAILURE;
1089
0
  }
1090
0
}
1091
1092
SAPI_API int sapi_get_target_gid(gid_t *obj)
1093
0
{
1094
0
  if (sapi_module.get_target_gid) {
1095
0
    return sapi_module.get_target_gid(obj);
1096
0
  } else {
1097
0
    return FAILURE;
1098
0
  }
1099
0
}
1100
1101
SAPI_API double sapi_get_request_time(void)
1102
278k
{
1103
278k
  if(SG(global_request_time)) return SG(global_request_time);
1104
1105
278k
  if (!sapi_module.get_request_time
1106
278k
      || sapi_module.get_request_time(&SG(global_request_time)) == FAILURE) {
1107
278k
    struct timeval tp = {0};
1108
278k
    if (!gettimeofday(&tp, NULL)) {
1109
278k
      SG(global_request_time) = (double)(tp.tv_sec + tp.tv_usec / 1000000.00);
1110
278k
    } else {
1111
0
      SG(global_request_time) = (double)time(0);
1112
0
    }
1113
278k
  }
1114
278k
  return SG(global_request_time);
1115
278k
}
1116
1117
0
SAPI_API void sapi_terminate_process(void) {
1118
0
  if (sapi_module.terminate_process) {
1119
0
    sapi_module.terminate_process();
1120
0
  }
1121
0
}
1122
1123
SAPI_API void sapi_add_request_header(const char *var, unsigned int var_len, char *val, unsigned int val_len, void *arg) /* {{{ */
1124
0
{
1125
0
  zval *return_value = (zval*)arg;
1126
0
  char *buf = NULL;
1127
1128
0
  ALLOCA_FLAG(use_heap)
1129
1130
0
  if (var_len > 5 &&
1131
0
      var[0] == 'H' &&
1132
0
      var[1] == 'T' &&
1133
0
      var[2] == 'T' &&
1134
0
      var[3] == 'P' &&
1135
0
      var[4] == '_') {
1136
1137
0
    const char *p;
1138
0
    char *str;
1139
1140
0
    var_len -= 5;
1141
0
    p = var + 5;
1142
0
    var = str = buf = do_alloca(var_len + 1, use_heap);
1143
0
    *str++ = *p++;
1144
0
    while (*p) {
1145
0
      if (*p == '_') {
1146
0
        *str++ = '-';
1147
0
        p++;
1148
0
        if (*p) {
1149
0
          *str++ = *p++;
1150
0
        }
1151
0
      } else if (*p >= 'A' && *p <= 'Z') {
1152
0
        *str++ = (*p++ - 'A' + 'a');
1153
0
      } else {
1154
0
        *str++ = *p++;
1155
0
      }
1156
0
    }
1157
0
    *str = 0;
1158
0
  } else if (var_len == sizeof("CONTENT_TYPE")-1 &&
1159
0
             memcmp(var, "CONTENT_TYPE", sizeof("CONTENT_TYPE")-1) == 0) {
1160
0
    var = "Content-Type";
1161
0
  } else if (var_len == sizeof("CONTENT_LENGTH")-1 &&
1162
0
             memcmp(var, "CONTENT_LENGTH", sizeof("CONTENT_LENGTH")-1) == 0) {
1163
0
    var = "Content-Length";
1164
0
  } else {
1165
0
    return;
1166
0
  }
1167
0
  add_assoc_stringl_ex(return_value, var, var_len, val, val_len);
1168
0
  if (buf) {
1169
    free_alloca(buf, use_heap);
1170
0
  }
1171
0
}
1172
/* }}} */