2017-01-28 17:06:12 +00:00
|
|
|
// Copyright 2017 Alexander Palaistras. All rights reserved.
|
2015-10-04 18:28:44 +00:00
|
|
|
// Use of this source code is governed by the MIT license that can be found in
|
|
|
|
// the LICENSE file.
|
|
|
|
|
2015-09-17 21:22:07 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
#include <main/php.h>
|
|
|
|
#include <main/SAPI.h>
|
|
|
|
#include <main/php_main.h>
|
|
|
|
#include <main/php_variables.h>
|
|
|
|
|
|
|
|
#include "context.h"
|
2015-10-01 23:48:33 +00:00
|
|
|
#include "engine.h"
|
2015-12-02 20:53:07 +00:00
|
|
|
#include "_cgo_export.h"
|
2015-09-17 21:22:07 +00:00
|
|
|
|
2016-01-09 17:27:38 +00:00
|
|
|
// The php.ini defaults for the Go-PHP engine.
|
2015-12-05 15:03:28 +00:00
|
|
|
const char engine_ini_defaults[] = {
|
2015-11-07 00:43:58 +00:00
|
|
|
"expose_php = 0\n"
|
|
|
|
"default_mimetype =\n"
|
2015-09-17 21:22:07 +00:00
|
|
|
"html_errors = 0\n"
|
|
|
|
"register_argc_argv = 1\n"
|
|
|
|
"implicit_flush = 1\n"
|
|
|
|
"output_buffering = 0\n"
|
|
|
|
"max_execution_time = 0\n"
|
|
|
|
"max_input_time = -1\n\0"
|
2015-12-05 15:03:28 +00:00
|
|
|
};
|
2015-09-17 21:22:07 +00:00
|
|
|
|
2016-05-03 16:42:28 +00:00
|
|
|
static int engine_ub_write(const char *str, uint len) {
|
2015-12-28 23:34:15 +00:00
|
|
|
engine_context *context = SG(server_context);
|
2015-10-01 23:48:33 +00:00
|
|
|
|
2016-02-21 19:11:50 +00:00
|
|
|
int written = engineWriteOut(context, (void *) str, len);
|
2016-01-09 17:27:38 +00:00
|
|
|
if (written != len) {
|
2015-10-01 23:48:33 +00:00
|
|
|
php_handle_aborted_connection();
|
|
|
|
}
|
|
|
|
|
2016-01-09 17:27:38 +00:00
|
|
|
return len;
|
2015-09-17 21:22:07 +00:00
|
|
|
}
|
|
|
|
|
2016-01-09 02:20:13 +00:00
|
|
|
static int engine_header_handler(sapi_header_struct *sapi_header, sapi_header_op_enum op, sapi_headers_struct *sapi_headers) {
|
2015-12-28 23:34:15 +00:00
|
|
|
engine_context *context = SG(server_context);
|
2015-11-07 00:43:58 +00:00
|
|
|
|
|
|
|
switch (op) {
|
2015-12-27 22:44:22 +00:00
|
|
|
case SAPI_HEADER_REPLACE:
|
2015-12-28 23:34:15 +00:00
|
|
|
case SAPI_HEADER_ADD:
|
2015-12-27 22:44:22 +00:00
|
|
|
case SAPI_HEADER_DELETE:
|
2016-02-21 19:11:50 +00:00
|
|
|
engineSetHeader(context, op, (void *) sapi_header->header, sapi_header->header_len);
|
2015-11-07 00:43:58 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-01-09 02:20:13 +00:00
|
|
|
static void engine_send_header(sapi_header_struct *sapi_header, void *server_context) {
|
2015-09-17 21:22:07 +00:00
|
|
|
// Do nothing.
|
|
|
|
}
|
|
|
|
|
2016-01-09 17:27:38 +00:00
|
|
|
static char *engine_read_cookies() {
|
2015-09-17 21:22:07 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2016-01-09 02:20:13 +00:00
|
|
|
static void engine_register_variables(zval *track_vars_array) {
|
|
|
|
php_import_environment_variables(track_vars_array);
|
2015-09-17 21:22:07 +00:00
|
|
|
}
|
|
|
|
|
2018-10-01 18:19:14 +00:00
|
|
|
#if PHP_VERSION_ID < 70100
|
|
|
|
static void engine_log_message(char *str) {
|
|
|
|
#else
|
2018-10-01 18:09:50 +00:00
|
|
|
static void engine_log_message(char *str, int syslog_type_int) {
|
2018-10-01 18:19:14 +00:00
|
|
|
#endif
|
2015-12-28 23:34:15 +00:00
|
|
|
engine_context *context = SG(server_context);
|
2015-12-01 22:05:44 +00:00
|
|
|
|
2016-02-21 19:11:50 +00:00
|
|
|
engineWriteLog(context, (void *) str, strlen(str));
|
2015-09-17 21:22:07 +00:00
|
|
|
}
|
|
|
|
|
2015-12-18 21:13:03 +00:00
|
|
|
static sapi_module_struct engine_module = {
|
2015-09-26 19:41:02 +00:00
|
|
|
"gophp-engine", // Name
|
|
|
|
"Go PHP Engine Library", // Pretty Name
|
2015-09-17 21:22:07 +00:00
|
|
|
|
|
|
|
NULL, // Startup
|
|
|
|
php_module_shutdown_wrapper, // Shutdown
|
|
|
|
|
|
|
|
NULL, // Activate
|
|
|
|
NULL, // Deactivate
|
|
|
|
|
2016-05-05 21:37:40 +00:00
|
|
|
_engine_ub_write, // Unbuffered Write
|
2015-09-17 21:22:07 +00:00
|
|
|
NULL, // Flush
|
|
|
|
NULL, // Get UID
|
|
|
|
NULL, // Getenv
|
|
|
|
|
|
|
|
php_error, // Error Handler
|
|
|
|
|
2015-11-07 00:43:58 +00:00
|
|
|
engine_header_handler, // Header Handler
|
2015-09-17 21:22:07 +00:00
|
|
|
NULL, // Send Headers Handler
|
|
|
|
engine_send_header, // Send Header Handler
|
|
|
|
|
|
|
|
NULL, // Read POST Data
|
|
|
|
engine_read_cookies, // Read Cookies
|
|
|
|
|
|
|
|
engine_register_variables, // Register Server Variables
|
|
|
|
engine_log_message, // Log Message
|
|
|
|
NULL, // Get Request Time
|
2016-01-09 02:20:13 +00:00
|
|
|
NULL, // Child Terminate
|
2015-09-17 21:22:07 +00:00
|
|
|
|
|
|
|
STANDARD_SAPI_MODULE_PROPERTIES
|
|
|
|
};
|
|
|
|
|
|
|
|
php_engine *engine_init(void) {
|
|
|
|
php_engine *engine;
|
|
|
|
|
|
|
|
#ifdef HAVE_SIGNAL_H
|
|
|
|
#if defined(SIGPIPE) && defined(SIG_IGN)
|
|
|
|
signal(SIGPIPE, SIG_IGN);
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
sapi_startup(&engine_module);
|
|
|
|
|
|
|
|
engine_module.ini_entries = malloc(sizeof(engine_ini_defaults));
|
|
|
|
memcpy(engine_module.ini_entries, engine_ini_defaults, sizeof(engine_ini_defaults));
|
|
|
|
|
|
|
|
if (php_module_startup(&engine_module, NULL, 0) == FAILURE) {
|
|
|
|
sapi_shutdown();
|
|
|
|
|
2015-10-01 23:48:33 +00:00
|
|
|
errno = 1;
|
|
|
|
return NULL;
|
2015-09-17 21:22:07 +00:00
|
|
|
}
|
|
|
|
|
2015-12-28 23:34:15 +00:00
|
|
|
engine = malloc((sizeof(php_engine)));
|
2015-09-17 21:22:07 +00:00
|
|
|
|
2015-10-01 23:48:33 +00:00
|
|
|
errno = 0;
|
|
|
|
return engine;
|
2015-09-17 21:22:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void engine_shutdown(php_engine *engine) {
|
2016-01-09 02:20:13 +00:00
|
|
|
php_module_shutdown();
|
2015-09-17 21:22:07 +00:00
|
|
|
sapi_shutdown();
|
|
|
|
|
|
|
|
free(engine_module.ini_entries);
|
|
|
|
free(engine);
|
2016-01-09 02:20:13 +00:00
|
|
|
}
|
2016-05-03 16:42:28 +00:00
|
|
|
|
|
|
|
#include "_engine.c"
|