Skip to content
Browse files

Code cleanup

  • Loading branch information...
1 parent 45932f7 commit e507ef73a0087c934372949fb7ee64f4a6442974 @pp3345 committed Mar 15, 2013
Showing with 275 additions and 262 deletions.
  1. +16 −16 conf/config.example.yml
  2. +17 −17 conf/vhost.example.yml
  3. +17 −17 sys/FastCGI.class.php
  4. +225 −212 sys/threads/single/requestWorker.thread.php
View
32 conf/config.example.yml
@@ -1,39 +1,39 @@
main:
tmpPath: ../tmp/ # Path for temporary files
logging:
- system: ../log/system.log # Path to System-Log
- request: ../log/requests.log # Path to Request-Log
- error: ../log/error.log # Path to Error-Log
+ system: ../log/system.log # Path to system log file
+ request: ../log/requests.log # Path to request log file
+ error: ../log/error.log # Path to error log file
dateFormat: 'd.m.y H:i:s' # Format for timestamps - See php.net/date for more information
debugMode: false # Enable debugging (debugging can also be enabled for a single run by setting the option --debug)
- user: www-data # *NIX-User under which Pancake should run
- group: www-data # *NIX-Group under which Pancake should run
+ user: www-data # *NIX user for Pancake
+ group: www-data # *NIX group for Pancake
listenPorts: # List of ports to listen on (Recommended for HTTP: 80)
- 80
IPv4: # IPv4 network interfaces to listen on (use 0 to listen on all available interfaces)
- 0
IPv6: # IPv6 network interfaces to listen on (use ::0 to listen on all available interfaces)
- ::0
requestWorkers: 2 # Amount of RequestWorkers to run
- exposePancake: true # If true, Pancake will send a "Server"-Header with Information about Pancake
+ exposePancake: true # If true, Pancake will send a "Server" header with information about Pancake
sizePrefix: bin # "si" or "bin" - SI: 1000 Byte = 1 Kilobyte - Binary: 1024 Byte = 1 Kibibyte
- allowHEAD: true # Whether to allow the "HEAD"-requestmethod
- allowTRACE: false # Whether to allow the "TRACE"-requestmethod
- allowOPTIONS: false # Whether to allow the "OPTIONS"-requestmethod
- requestWorkerLimit: 250000 # Limit of requests a RequestWorker may process until it has to be restarted (0 for no limit)
- maxConcurrent: 0 # Limit of concurrent connections a RequestWorker may hold open (0 for no limit)
- workerBootTime: 5 # The upper timelimit for a worker to boot (raise this when you're for example caching files that take long to load)
+ allowHEAD: true # Whether to allow HEAD requests
+ allowTRACE: false # Whether to allow TRACE requests
+ allowOPTIONS: false # Whether to allow OPTIONS requests
+ requestWorkerLimit: 250000 # Limit of requests a RequestWorker may process until it will be restarted (0 for no limit)
+ maxConcurrent: 0 # Limit of concurrent connections a RequestWorker may keep open (0 for no limit)
+ workerBootTime: 5 # The upper time limit for a worker to boot (raise this when you're for example caching files that take long to load)
writeBufferMin: 10240 # Minimum write buffer size that should be held in RAM
writeBufferSoftMaxConcurrent: 50 # Soft concurrent write buffer limit - When this limit is reached, Pancake won't allocate any buffers that are bigger than the minimum size anymore
writeBufferHardMaxConcurrent: 2000 # Hard concurrent write buffer limit - When this limit is reached, Pancake will try not to allocate any buffers anymore
- waitPHPWorkerBoot: true # Wait for PHP-Workers to boot (should be disabled in shared environments)
- waitSlotTime: 5000 # When there is no PHPWorker available to serve a PHP-request, Pancake will check after waitslottime µsec if a slot became available
- waitSlotWaitLimit: 0 # See above; this is the upper limit of wait-cycles until Pancake gives up on searching for a slot (for example: waitslottime = 5000; waitslotwaitlimit = 5 => Client will receive 500 Internal Server Error after 25 ms if no PHPWorker became available in those 25 ms)
+ waitPHPWorkerBoot: true # Wait for PHPWorkers to boot (should be disabled in shared environments)
+ waitSlotTime: 5000 # When there is no PHPWorker available to serve a PHP request, Pancake will check after waitslottime µsec if a slot became available (you should not use this, set PHPSocketBacklog instead in the vHost configuration)
+ waitSlotWaitLimit: 0 # See above; this is the upper limit of wait-cycles until Pancake gives up on searching for a slot (for example: waitslottime = 5000; waitslotwaitlimit = 5 => Client will receive 500 Internal Server Error after 25 ms if no PHPWorker became available in those 25 ms) - Set this to 0 to completely deactivate wait slots
socketBacklog: 200 # Connection backlog for incoming requests
writeLimit: 1048576 # Limits the maximum data sent to the client at once - Low value = less RAM usage, but slower - High value = faster, but more RAM usage
NaglesAlgorithm: false # Enable or disable Nagle's algorithm (will set TCP_NODELAY when false)
PreBuffer: 65536 # Buffer static file data in RAM before sending HTTP headers (can save one write cycle)
-include: # Additional configuration-files to include
+include: # Additional configuration files to include
- ../conf/vhosts/
- ../conf/mime.yml
moody:
View
34 conf/vhost.example.yml
@@ -1,25 +1,25 @@
vhosts:
- default: # Name of the vHost
+ default: # Name of the vHost REMEMBER TO SET THIS WHEN CREATING A NEW VHOST
enabled: true
docRoot: ../vhosts/default/ # Path to document root
- listen: # Hosts to listen on
+ listen: # Named hosts to listen on
- 127.0.0.1
- localhost
- "[::1]"
- PHPWorkers: 2 # Amount of PHP-Workers to run
- gzipMin: 1048576 # Minimum static filesize to use GZIP-compression
- gzipLevel: 1 # Level of the GZIP-compression - Lower = bigger size, less CPU-usage
- enableGZIP: false # Enable and disable GZIP-compression - We recommend leaving it disabled, it's better to precompress big files - GZIP-compression may produce very heavy CPU-load
+ PHPWorkers: 2 # Amount of PHPWorkers to run
+ gzipMin: 1048576 # Minimum size of a static file to use GZIP compression
+ gzipLevel: 1 # Level of GZIP compression - Lower = bigger size, less CPU-usage
+ enableGZIP: false # Enable and disable GZIP compression - We recommend leaving it disabled, it's better to precompress big files - GZIP compression may create very heavy CPU load
gzipStatic: false # If enabled and for example /jquery.js is requested and /jquery.js.gz exists on the disk, Pancake will deliver the GZIP-compressed file
- gzipMimeTypes: # Allow limiting automatic GZIP compression to specific MIME types (does not apply to static GZIP compression)
+ gzipMimeTypes: # Allow limiting automatic GZIP compression to specific MIME types (does not apply to gzipStatic)
# - image/png
# - application/x-javascript
allowDirectoryListings: true # Enable directory listings
isDefault: true # Whether this vHost is the default vHost or not (one of your vHosts MUST have this settings enabled)
204OnEmptyPage: false # If true, Pancake will send 204 No Content instead of 200 OK when the page is empty
PHPWorkerLimit: 1000 # Limit of requests a PHPWorker may process until it has to be restarted (0 for no limit)
- PHPInfoPancake: true # Expose information about Pancake's configuration in phpinfo()
- PHPInfoPancakevHosts: false # Expose information about Pancake's vHosts in phpinfo() (requires phpinfopancake to be true)
+ PHPInfoPancake: true # Expose information about Pancake configuration in phpinfo()
+ PHPInfoPancakevHosts: false # Expose information about Pancake vHosts in phpinfo() (requires PHPInfoPancake to be true)
autoDelete: # Automatically delete non-cached PHP elements after a request
classes: true
functions: true
@@ -31,21 +31,21 @@ vhosts:
excludeDelete: # Exclude the specified PHP elements from automatic deletion after a request
PHPHTMLErrors: true # Enable HTML errors instead of plain text
PHPSocketBacklog: 100 # Connection backlog for internal worker communication sockets (recommendation: less workers => higher backlog; more workers => lower backlog - should not be lower than 1)
- PHPResetClassStaticObjects: true # Whether the SAPI should search for objects inside static class properties and reset them (will also look recursively in arrays and object's properties) - Try this setting when the SAPI often crashes or acts strange
- PHPResetClassStaticNonObjects: false # Whether the SAPI should set all static class properties to null after finishing a request (will still destroy objects before if phpresetclassstaticobjects is enabled)
- PHPResetFunctionStaticObjects: true # Whether the SAPI should search for objects inside static function variables and reset them (will also look recursively in arrays and object's properties) - Try this setting when the SAPI often crashes or acts strange
- PHPResetFunctionStaticNonObjects: false # Whether the SAPI should set all static function variables to null after finishing a request (will still destroy objects before if phpresetfunctionstaticobjects is enabled)
+ PHPResetClassStaticObjects: true # Whether the SAPI should search for objects inside static class properties and reset them - Try this setting when the SAPI often crashes or acts strange
+ PHPResetClassStaticNonObjects: false # Whether the SAPI should set all static class properties to null after finishing a request (will still destroy objects before if PHPResetClassStaticObjects is enabled)
+ PHPResetFunctionStaticObjects: true # Whether the SAPI should search for objects inside static function variables and reset them - Try this setting when the SAPI often crashes or acts strange
+ PHPResetFunctionStaticNonObjects: false # Whether the SAPI should set all static function variables to null after finishing a request (will still destroy objects before if PHPResetFunctionStaticObjects is enabled)
PHPResetObjectsDestroyDestructors: true # Whether the SAPI should not execute object destructors when destroying objects (true = will not execute; false = will execute)
- PHPMaxExecutionTime: 10 # Same function as PHP's max_execution_time-setting
-# PHPModules: # Load PHP modules for this vHost
+ PHPMaxExecutionTime: 10 # Same as PHPs max_execution_time setting
+# PHPModules: # Load custom PHP modules for this vHost
# - gd
# PHPINISettings: # Set custom php.ini settings for this vHost
# session.name: "myGreatSession"
# PHPPredefinedConstants: # A list of constants that should be predefined before script-execution in the SAPI
# myConst: 123
# iLovePancakes: true
# PHPDeletePredefinedConstantsAfterCodeCacheLoad: false # Whether the predefined constants should be deleted again after loading the CodeCache
-# PHPDisabledFunctions: # A list of functions that will be disabled inside the SAPI (acts like the php.ini-setting disabled_functions)
+# PHPDisabledFunctions: # A list of functions that will be disabled inside the SAPI (acts like the php.ini setting disabled_functions)
# - posix_getpid
# - posix_kill
# PHPCache: # Files or directories that should be cached by Pancake
@@ -72,7 +72,7 @@ vhosts:
# headers:
# X-Reason: 403
# - pathinfo: ~^((?U).+\.php)(/?.+)$~
- index: # List of files allowed as directory-indexes
+ index: # List of files allowed as directory indexes
- index.php
- index.html
- index.htm
View
34 sys/FastCGI.class.php
@@ -121,27 +121,27 @@ public function makeRequest(HTTPRequest $requestObject, $requestSocket) {
}
/* FCGI_PARAMS */
- $body = "\xf" . chr(strlen(/* .VHOST_DOCUMENT_ROOT */ . /* .REQUEST_FILE_PATH */)) . "SCRIPT_FILENAME" . /* .VHOST_DOCUMENT_ROOT */ . /* .REQUEST_FILE_PATH */;
- $body .= "\xc" . chr(strlen(/* .QUERY_STRING */)) . "QUERY_STRING" . /* .QUERY_STRING */;
- $body .= "\xe" . chr(strlen(/* .REQUEST_TYPE */)) . "REQUEST_METHOD" . /* .REQUEST_TYPE */;
- $body .= "\xb" . chr(strlen(/* .REQUEST_FILE_PATH */)) . "SCRIPT_NAME" . /* .REQUEST_FILE_PATH */;
- $body .= "\xf\x8SERVER_PROTOCOLHTTP/" . /* .PROTOCOL_VERSION */;
+ $body = "\xf" . chr(strlen(/* .VHOST_DOCUMENT_ROOT */ . $requestObject->requestFilePath)) . "SCRIPT_FILENAME" . /* .VHOST_DOCUMENT_ROOT */ . $requestObject->requestFilePath;
+ $body .= "\xc" . chr(strlen($requestObject->queryString)) . "QUERY_STRING" . $requestObject->queryString;
+ $body .= "\xe" . chr(strlen($requestObject->requestType)) . "REQUEST_METHOD" . $requestObject->requestType;
+ $body .= "\xb" . chr(strlen($requestObject->requestFilePath)) . "SCRIPT_NAME" . $requestObject->requestFilePath;
+ $body .= "\xf\x8SERVER_PROTOCOLHTTP/" . $requestObject->protocolVersion;
$body .= "\x11\x7GATEWAY_INTERFACECGI/1.1";
$body .= "\xb" . chr(strlen($requestObject->originalRequestURI)) . "REQUEST_URI" . $requestObject->originalRequestURI;
- $body .= "\xc" . chr(strlen(/* .REQUEST_URI */)) . "DOCUMENT_URI" . /* .REQUEST_URI */;
- $body .= "\xb" . chr(strlen(/* .REMOTE_IP */)) . "REMOTE_ADDR" . /* .REMOTE_IP */;
+ $body .= "\xc" . chr(strlen($requestObject->requestURI)) . "DOCUMENT_URI" . $requestObject->requestURI;
+ $body .= "\xb" . chr(strlen($requestObject->remoteIP)) . "REMOTE_ADDR" . $requestObject->remoteIP;
$body .= "\xb" . chr(strlen(/* .VHOST */->listen[0])) . "SERVER_NAME" . /* .VHOST */->listen[0];
- $body .= "\xb" . chr(strlen(/* .LOCAL_PORT */)) . "SERVER_PORT" . /* .LOCAL_PORT */;
+ $body .= "\xb" . chr(strlen($requestObject->localPort)) . "SERVER_PORT" . $requestObject->localPort;
$body .= /* .eval 'return "\xf" . chr(strlen("Pancake/" . \Pancake\VERSION)) . "SERVER_SOFTWAREPancake/" . \Pancake\VERSION;' */;
- $body .= "\xb" . chr(strlen(/* .LOCAL_IP */)) . "SERVER_ADDR" . /* .LOCAL_IP */;
+ $body .= "\xb" . chr(strlen($requestObject->localIP)) . "SERVER_ADDR" . $requestObject->localIP;
if($requestObject->pathInfo) {
$body .= "\x9" . chr(strlen($requestObject->pathInfo)) . "PATH_INFO" . $requestObject->pathInfo;
$body .= "\xf" . chr(strlen($requestObject->vHost->documentRoot . $requestObject->pathInfo)) . "PATH_TRANSLATED" . $requestObject->vHost->documentRoot . $requestObject->pathInfo;
}
- if(/* .RAW_POST_DATA */) {
- $body .= "\xc" . chr(strlen(/* .GET_REQUEST_HEADER '"content-type"' */)) . "CONTENT_TYPE" . /* .GET_REQUEST_HEADER '"content-type"' */;
- $body .= "\xe" . chr(strlen(/* .GET_REQUEST_HEADER '"content-length"' */)) . "CONTENT_LENGTH" . /* .GET_REQUEST_HEADER '"content-length"' */;
+ if($requestObject->rawPOSTData) {
+ $body .= "\xc" . chr(strlen($requestObject->requestHeaders["content-type"])) . "CONTENT_TYPE" . $requestObject->requestHeaders["content-type"];
+ $body .= "\xe" . chr(strlen($requestObject->requestHeaders["content-length"])) . "CONTENT_LENGTH" . $requestObject->requestHeaders["content-length"];
}
// HTTP header data
@@ -167,8 +167,8 @@ public function makeRequest(HTTPRequest $requestObject, $requestSocket) {
/* Empty FCGI_PARAMS */
Write($this->socket, "\1\4" . $requestID . "\0\0\0\0");
- if(/* .RAW_POST_DATA */) {
- $rawPostData = str_split(/* .RAW_POST_DATA */, 65535);
+ if($requestObject->rawPOSTData) {
+ $rawPostData = str_split($requestObject->rawPOSTData, 65535);
foreach($rawPostData as $recordData) {
/* FCGI_STDIN */
@@ -231,18 +231,18 @@ public function upstreamRecord($data, $socketID) {
foreach(explode("\r\n", $contentBody[0]) as $header) {
list($headerName, $headerValue) = explode(":", $header, 2);
if($headerName == 'Status') {
- /* .ANSWER_CODE */ = (int) $headerValue;
+ $requestObject->answerCode = (int) $headerValue;
continue;
}
$requestObject->setHeader(trim($headerName), trim($headerValue), false);
}
if(isset($contentBody[1]))
- /* .ANSWER_BODY */ .= $contentBody[1];
+ $requestObject->answerBody .= $contentBody[1];
return 8;
}
- /* .ANSWER_BODY */ .= $data;
+ $requestObject->answerBody .= $data;
return 8;
case /* .constant 'FCGI_END_REQUEST' */:
View
437 sys/threads/single/requestWorker.thread.php
@@ -22,7 +22,7 @@
#.endif
#.if #.bool #.call 'Pancake\Config::get' 'fastcgi'
- #.macro 'VHOST_FASTCGI' '(isset(/* .VHOST */->fastCGI[/* .MIME_TYPE */]) ? /* .VHOST */->fastCGI[/* .MIME_TYPE */] : null)'
+ #.macro 'VHOST_FASTCGI' '(isset(/* .VHOST */->fastCGI[$requestObject->mimeType]) ? /* .VHOST */->fastCGI[$requestObject->mimeType] : null)'
#.define 'SUPPORT_FASTCGI' true
#.endif
@@ -107,29 +107,6 @@
#.define 'SUPPORT_MULTIPLE_VHOSTS' true
#.endif
- #.macro 'REQUEST_TYPE' '$requestObject->requestType'
- #.macro 'GET_PARAMS' '$requestObject->getGETParams()'
- #.macro 'MIME_TYPE' '$requestObject->mimeType'
- #.macro 'VHOST' '$requestObject->vHost'
- #.macro 'REQUEST_FILE_PATH' '$requestObject->requestFilePath'
- #.macro 'RANGE_FROM' '$requestObject->rangeFrom'
- #.macro 'RANGE_TO' '$requestObject->rangeTo'
- #.macro 'BUILD_ANSWER_HEADERS' '$requestObject->buildAnswerHeaders()'
- #.macro 'ANSWER_BODY' '$requestObject->answerBody'
- #.macro 'REMOTE_IP' '$requestObject->remoteIP'
- #.macro 'REMOTE_PORT' '$requestObject->remotePort'
- #.macro 'REQUEST_LINE' '$requestObject->requestLine'
- #.macro 'ANSWER_CODE' '$requestObject->answerCode'
- #.macro 'UPLOADED_FILES' '$requestObject->uploadedFiles'
- #.macro 'GET_REQUEST_HEADER' '(isset($requestObject->requestHeaders[$headerName]) ? $requestObject->requestHeaders[$headerName] : null)' '$headerName'
- #.macro 'QUERY_STRING' '$requestObject->queryString'
- #.macro 'PROTOCOL_VERSION' '$requestObject->protocolVersion'
- #.macro 'REQUEST_URI' '$requestObject->requestURI'
- #.macro 'LOCAL_IP' '$requestObject->localIP'
- #.macro 'LOCAL_PORT' '$requestObject->localPort'
- #.macro 'RAW_POST_DATA' '$requestObject->rawPOSTData'
- #.macro 'ACCEPTS_COMPRESSION' 'isset($requestObject->acceptedCompressions[$compression])' '$compression'
-
#.longDefine 'EVAL_CODE'
global $Pancake_vHosts;
@@ -145,6 +122,7 @@
#.WRITE_LIMIT = #.eval EVAL_CODE false
+ #.macro 'VHOST' '$requestObject->vHost'
#.macro 'VHOST_PHP_WORKERS' '/* .VHOST */->phpWorkers'
#.macro 'VHOST_SOCKET_NAME' '/* .VHOST */->phpSocketName'
#.macro 'VHOST_DOCUMENT_ROOT' '/* .VHOST */->documentRoot'
@@ -184,7 +162,8 @@
Thread::clearCache();
MIME::load();
-
+
+ // Convert vHost instances to vHostInterface instances
foreach($Pancake_vHosts as $id => &$vHost) {
if($vHost instanceof vHostInterface)
break;
@@ -204,6 +183,7 @@
$Pancake_vHosts[$address] = $vHost;
}
+ // This will be an unconnected socket after startup
Close($this->localSocket);
#.ifdef 'SUPPORT_AUTHENTICATION'
@@ -266,6 +246,7 @@
unset($id, $vHost, $address);
+ // Make sure the Pancake Module Loader can not be exploited
disableModuleLoader();
Config::workerDestroy();
@@ -275,45 +256,46 @@
$listenSockets = $listenSocketsOrig = $Pancake_sockets;
// Initialize some variables
- #.ifdef 'SUPPORT_TLS'
+#.ifdef 'SUPPORT_TLS'
$TLSConnections = array();
- #.endif
- #.ifdef 'SUPPORT_FASTCGI'
+#.endif
+#.ifdef 'SUPPORT_FASTCGI'
$fastCGISockets = array();
- #.endif
- #.ifdef 'SUPPORT_AJP13'
+#.endif
+#.ifdef 'SUPPORT_AJP13'
$ajp13Sockets = array();
- #.endif
+#.endif
$liveWriteSockets = array();
$liveWriteSocketsOrig = array();
$liveReadSockets = array();
$socketData = array();
$postData = array();
- #.if 0 < #.call 'Pancake\Config::get' 'main.requestworkerlimit'
+#.if 0 < #.call 'Pancake\Config::get' 'main.requestworkerlimit'
$processedRequests = 0;
- #.endif
+#.endif
$requests = array();
$requestFileHandle = array();
- #.ifdef 'SUPPORT_GZIP'
+#.ifdef 'SUPPORT_GZIP'
$gzipPath = array();
- #.endif
+#.endif
$writeBuffer = array();
- #.ifdef 'SUPPORT_PHP'
+#.ifdef 'SUPPORT_PHP'
$phpSockets = array();
- #.endif
- #.ifdef 'SUPPORT_WAITSLOTS'
+#.endif
+#.ifdef 'SUPPORT_WAITSLOTS'
$waitSlots = array();
$waitSlotsOrig = array();
$waits = array();
- #.endif
+#.endif
- #.if BENCHMARK === true
- benchmarkFunction("hexdec");
- #.endif
+#.if BENCHMARK === true
+ benchmarkFunction("hexdec");
+#.endif
- #.if #.Pancake\Config::get("main.naglesalgorithm", 0)
- NaglesAlgorithm(/*.bool #.Pancake\Config::get("main.naglesalgorithm", 0)*/);
- #.endif
+#.if #.Pancake\Config::get("main.naglesalgorithm", 0)
+ // Disable Nagle's algorithm if requested
+ NaglesAlgorithm(/*.bool #.Pancake\Config::get("main.naglesalgorithm", 0)*/);
+#.endif
// Ready
$Pancake_currentThread->parentSignal(/* .constant 'SIGUSR1' */);
@@ -328,9 +310,9 @@
// Wait for incoming requests
while(Select($listenSockets, $liveWriteSockets
- #.ifdef 'SUPPORT_WAITSLOTS'
+#.ifdef 'SUPPORT_WAITSLOTS'
, $waitSlots ? /* .call 'Pancake\Config::get' 'main.waitslottime' */ : null
- #.endif
+#.endif
)) {
// If there are jobs left in the queue at the end of the job-run, we're going to jump back to this point to execute the jobs that are left
cycle:
@@ -341,11 +323,12 @@
$requestObject = $requests[$socket];
- #.ifdef 'SUPPORT_TLS'
+#.ifdef 'SUPPORT_TLS'
+ // For TLS handshake we need both reads and writes
if(isset($TLSConnections[$socket]) && $TLSConnections[$socket] < /* .TLS_WRITE */) {
goto TLSRead;
}
- #.endif
+#.endif
goto liveWrite;
}
@@ -358,17 +341,19 @@
}
#.endif
- // New connection, downloadable content from a client or the PHP-SAPI finished a request
+ // New connection, data from client, AJP13 or FastCGI or PHP SAPI data or command from Pancake master process
foreach($listenSockets as $socket) {
unset($listenSockets[$socket]);
#.ifdef 'SUPPORT_TLS'
+ // In case of a TLS renegotiation we might need a read event while in write step
if(isset($TLSConnections[$socket]) && $TLSConnections[$socket] == /* .TLS_WRITE */) {
$requestObject = $requests[$socket];
goto liveWrite;
}
#.endif
+ // Data from client or new request on keep-alive socket
if(isset($liveReadSockets[$socket]) || KeepAlive($socket)) {
$requestObject = $requests[$socket];
if(!isset($socketData[$socket])) {
@@ -377,7 +362,7 @@
break;
}
- #.ifdef 'SUPPORT_AJP13'
+#.ifdef 'SUPPORT_AJP13'
if(isset($ajp13Sockets[$socket])) {
$ajp13 = $ajp13Sockets[$socket];
@@ -413,9 +398,9 @@
unset($data);
goto clean;
}
- #.endif
+#.endif
- #.ifdef 'SUPPORT_FASTCGI'
+#.ifdef 'SUPPORT_FASTCGI'
if(isset($fastCGISockets[$socket])) {
$fastCGI = $fastCGISockets[$socket];
do {
@@ -453,9 +438,9 @@
unset($newData);
goto clean;
}
- #.endif
+#.endif
- #.ifdef 'SUPPORT_PHP'
+#.ifdef 'SUPPORT_PHP'
if(isset($phpSockets[$socket])) {
$packages = hexdec(Read($socket, 8));
if(!$packages) {
@@ -510,90 +495,98 @@
goto write;
}
- #.endif
+#.endif
// Internal communication
if($socket == $this->socket) {
switch(Read($this->socket, 128)) {
case "GRACEFUL_SHUTDOWN":
- // Stop listening for new connections
+ // Master wants us to shutdown - stop listening for new connections
foreach($Pancake_sockets as $socket)
unset($listenSocketsOrig[$socket]);
$doShutdown = true;
goto clean;
case "LOAD_FILE_POINTERS":
+ // Master got SIGHUP and wants us to flush the file pointers
LoadFilePointers();
goto clean;
}
}
if(
- #.if 0 != #.call 'Pancake\Config::get' 'main.maxconcurrent'
+#.if 0 != #.call 'Pancake\Config::get' 'main.maxconcurrent'
/* .call 'Pancake\Config::get' 'main.maxconcurrent' */ < count($listenSocketsOrig) - count($Pancake_sockets) ||
- #.endif
+#.endif
!($socket = @NonBlockingAccept($socket)))
goto clean;
- #.ifdef 'SUPPORT_TLS'
+#.ifdef 'SUPPORT_TLS'
GetSockName($socket, $xx, $port);
+ // Check if we got the new connection on a TLS port
if(in_array($port, Config::get('tls.ports'))) {
$TLSConnections[$socket] = /* .TLS_ACCEPT */;
TLSInitializeConnection($socket);
}
- #.endif
+#.endif
$socketData[$socket] = "";
break;
}
- #.ifdef 'SUPPORT_TLS'
+#.ifdef 'SUPPORT_TLS'
TLSRead:
if(isset($TLSConnections[$socket])) {
switch($TLSConnections[$socket]) {
case /* .TLS_ACCEPT */:
switch(TLSAccept($socket)) {
case 1:
+ // Handshake OK
$TLSConnections[$socket] = /* .TLS_READ */;
case 2:
+ // OpenSSL wants read
$liveReadSockets[$socket] = true;
$listenSocketsOrig[$socket] = $socket;
goto clean;
case 3:
+ // OpenSSL wants write
$liveWriteSocketsOrig[$socket] = $socket;
goto clean;
case 0:
+ // Error
goto close;
}
break;
case /* .TLS_READ */:
if(isset($requests[$socket]))
- $bytes = TLSRead($socket, /* .GET_REQUEST_HEADER '"content-length"' */ - strlen($postData[$socket]));
+ $bytes = TLSRead($socket, $requestObject->requestHeaders["content-length"] - strlen($postData[$socket]));
else
$bytes = TLSRead($socket, 10240);
if($bytes === 2) {
+ // OpenSSL wants read
$liveReadSockets[$socket] = true;
$listenSocketsOrig[$socket] = $socket;
goto clean;
}
if($bytes === 3) {
+ // OpenSSL wants write
$liveWriteSocketsOrig[$socket] = $socket;
goto clean;
}
break;
}
} else
- #.endif
+#.endif
// Receive data from client
if(isset($requests[$socket]))
- $bytes = @Read($socket, /* .GET_REQUEST_HEADER '"content-length"' */ - strlen($postData[$socket]));
+ $bytes = @Read($socket, $requestObject->requestHeaders["content-length"] - strlen($postData[$socket]));
else
$bytes = @Read($socket, 10240);
@@ -605,14 +598,14 @@
// Check if request was already initialized and we are only reading POST-data
if(isset($requests[$socket])) {
$postData[$socket] .= $bytes;
- if(strlen($postData[$socket]) >= /* .GET_REQUEST_HEADER '"content-length"' */)
+ if(strlen($postData[$socket]) >= $requestObject->requestHeaders["content-length"])
goto readData;
} else if($bytes) {
$socketData[$socket] .= $bytes;
// Check if all headers were received
if(strpos($socketData[$socket], "\r\n\r\n")) {
- // Check for POST
+ // Split POST data
if(strpos($socketData[$socket], "POST") === 0) {
$data = explode("\r\n\r\n", $socketData[$socket], 2);
$socketData[$socket] = $data[0];
@@ -628,7 +621,7 @@
if(strlen($socketData[$socket]) >= 10240)
goto close;
}
- // Event-based reading
+ // We are still waiting for header data - event-based read
$liveReadSockets[$socket] = true;
$listenSocketsOrig[$socket] = $socket;
goto clean;
@@ -639,10 +632,10 @@
// Get information about client
GetPeerName($socket, $ip, $port);
- // Get local IP-address and port
+ // Get local IP address and port
GetSockName($socket, $lip, $lport);
- // Create request object / Read Headers
+ // Create request object and process headers
try {
$requestObject = $requests[$socket] = new HTTPRequest($ip, $port, $lip, $lport);
$requestObject->init($socketData[$socket]);
@@ -656,64 +649,67 @@
}
}
- // Check for POST and get all POST-data
- if(/* .REQUEST_TYPE */ == 'POST') {
- if(strlen($postData[$socket]) >= /* .GET_REQUEST_HEADER '"content-length"' */) {
- if(strlen($postData[$socket]) > /* .GET_REQUEST_HEADER '"content-length"' */)
- $postData[$socket] = substr($postData[$socket], 0, /* .GET_REQUEST_HEADER '"content-length"' */);
+ // Check for POST and get all POST data
+ if($requestObject->requestType == 'POST') {
+ if(strlen($postData[$socket]) >= $requestObject->requestHeaders["content-length"]) {
+ if(strlen($postData[$socket]) > $requestObject->requestHeaders["content-length"])
+ $postData[$socket] = substr($postData[$socket], 0, $requestObject->requestHeaders["content-length"]);
unset($listenSocketsOrig[$socket]);
- /* .RAW_POST_DATA */ = $postData[$socket];
+ $requestObject->rawPOSTData = $postData[$socket];
unset($postData[$socket]);
} else {
- // Event-based reading
+ // We did not receive all POST data yet
$liveReadSockets[$socket] = true;
$listenSocketsOrig[$socket] = $socket;
goto clean;
}
} else
unset($listenSocketsOrig[$socket]);
- #.if #.call 'Pancake\Config::get' 'main.allowtrace'
- if(/* .REQUEST_TYPE */ == 'TRACE')
- goto write;
- #.endif
-
- #.if #.call 'Pancake\Config::get' 'main.allowoptions'
- // Check for "OPTIONS"-requestmethod
- if(/* .REQUEST_TYPE */ == 'OPTIONS')
- $requestObject->setHeader('Allow',
- /* .eval '$allow = "GET, POST, OPTIONS";
- if(Pancake\Config::get("main.allowhead") === true)
- $allow .= ", HEAD";
- if(Pancake\Config::get("main.allowtrace") === true)
- $allow .= ", TRACE";
- return $allow;' false
- */);
- #.endif
+#.if #.call 'Pancake\Config::get' 'main.allowtrace'
+ // If we have a TRACE request we don't need to take any further action
+ if($requestObject->requestType == 'TRACE')
+ goto write;
+#.endif
+
+#.if #.call 'Pancake\Config::get' 'main.allowoptions'
+ // Check for OPTIONS
+ #.longDefine 'EVAL_CODE'
+ $allow = "GET, POST, OPTIONS";
+ if(Pancake\Config::get("main.allowhead") === true)
+ $allow .= ", HEAD";
+ if(Pancake\Config::get("main.allowtrace") === true)
+ $allow .= ", TRACE";
+ return $allow;
+ #.endLongDefine
+ if($requestObject->requestType == 'OPTIONS')
+ $requestObject->setHeader('Allow', /* .eval EVAL_CODE false */);
+#.endif
+#.if Pancake\DEBUG_MODE === true
// Output debug information
- #.if Pancake\DEBUG_MODE === true
- if(array_key_exists('pancakedebug', /* .GET_PARAMS */)) {
+ if(isset($requestObject->getGETParams()['pancakedebug'])) {
$requestObject->setHeader('Content-Type', 'text/plain');
$body = 'Received Headers:' . "\r\n";
- $body .= /* .REQUEST_LINE */ . "\r\n";
+ $body .= $requestObject->requestLine . "\r\n";
$body .= $requestObject->getRequestHeaders() . "\r\n";
$body .= 'Received POST content:' . "\r\n";
$body .= $postData[$socket] . "\r\n\r\n";
$body .= 'Dump of RequestObject:' . "\r\n";
$body .= print_r($requestObject, true);
- /* .ANSWER_BODY */ = $body;
+ $requestObject->answerBody = $body;
unset($body);
goto write;
}
- #.endif
+#.endif
- #.if #.call 'ini_get' 'expose_php'
- if(isset(/* .GET_PARAMS*/[""])) {
- switch(/* .GET_PARAMS*/[""]) {
+#.if #.call 'ini_get' 'expose_php'
+ // PHP UUIDs
+ if(isset($requestObject->getGETParams()[""])) {
+ switch($requestObject->getGETParams()[""]) {
case 'PHPE9568F34-D428-11d2-A769-00AA001ACF42':
$logo = file_get_contents('logo/php.gif');
$requestObject->setHeader('Content-Type', 'image/gif');
@@ -742,11 +738,11 @@
default:
goto load;
}
- /* .ANSWER_BODY */ = $logo;
+ $requestObject->answerBody = $logo;
unset($logo);
goto write;
}
- #.endif
+#.endif
load:
@@ -765,54 +761,53 @@
#.endif
#.endif
- #.ifdef 'SUPPORT_FASTCGI'
- // FastCGI
- if($fastCGI = /* .VHOST_FASTCGI */) {
- if($fastCGI->makeRequest($requestObject, $socket) === false)
- goto write;
- $listenSocketsOrig[$fastCGI->socket] = $fastCGI->socket;
- $fastCGISockets[$fastCGI->socket] = $fastCGI;
- goto clean;
- }
- #.endif
+#.ifdef 'SUPPORT_FASTCGI'
+ // FastCGI
+ if($fastCGI = /* .VHOST_FASTCGI */) {
+ if($fastCGI->makeRequest($requestObject, $socket) === false)
+ goto write;
+ $listenSocketsOrig[$fastCGI->socket] = $fastCGI->socket;
+ $fastCGISockets[$fastCGI->socket] = $fastCGI;
+ goto clean;
+ }
+#.endif
- #.ifdef 'SUPPORT_PHP'
+#.ifdef 'SUPPORT_PHP'
// Check for PHP
- if(/* .MIME_TYPE */ == 'text/x-php'
- #.ifdef 'SUPPORT_MULTIPLE_VHOSTS'
+ if($requestObject->mimeType == 'text/x-php'
+#.ifdef 'SUPPORT_MULTIPLE_VHOSTS'
&& /* .VHOST_PHP_WORKERS */
- #.endif
+#.endif
) {
if(!($psocket = Socket(/* .constant 'AF_UNIX' */, /* .constant 'SOCK_SEQPACKET' */, 0))) {
$requestObject->invalidRequest(new invalidHTTPRequestException('Failed to create communication socket. Probably the server is overladed. Try again later.', 500));
goto write;
}
SetBlocking($psocket, false);
- // @ - Do not spam errorlog with Resource temporarily unavailable if there is no PHPWorker available
if(Connect($psocket, /* .AF_UNIX */, /* .VHOST_SOCKET_NAME */)) {
- #.ifdef 'SUPPORT_WAITSLOTS'
- $waits[$socket]++;
+#.ifdef 'SUPPORT_WAITSLOTS'
+ $waits[$socket]++;
- if($waits[$socket] > /* .call 'Pancake\Config::get' 'main.waitslotwaitlimit' */) {
- $requestObject->invalidRequest(new invalidHTTPRequestException('There was no worker available to serve your request. Please try again later.', 500));
- goto write;
- }
+ if($waits[$socket] > /* .call 'Pancake\Config::get' 'main.waitslotwaitlimit' */) {
+ $requestObject->invalidRequest(new invalidHTTPRequestException('There was no worker available to serve your request. Please try again later.', 500));
+ goto write;
+ }
- $waitSlotsOrig[$socket] = $socket;
+ $waitSlotsOrig[$socket] = $socket;
- goto clean;
- #.else
- $requestObject->invalidRequest(new invalidHTTPRequestException('There was no worker available to serve your request. Please try again later.', 500));
- goto write;
- #.endif
+ goto clean;
+#.else
+ $requestObject->invalidRequest(new invalidHTTPRequestException('There was no worker available to serve your request. Please try again later.', 500));
+ goto write;
+#.endif
}
- #.ifdef 'SUPPORT_WAITSLOTS'
+#.ifdef 'SUPPORT_WAITSLOTS'
unset($waitSlotsOrig[$socket]);
unset($waits[$socket]);
- #.endif
+#.endif
$data = serialize($requestObject);
@@ -840,35 +835,35 @@
goto clean;
}
- #.endif
+#.endif
// Get time of last modification
- $modified = filemtime(/* .VHOST_DOCUMENT_ROOT */ . /* .REQUEST_FILE_PATH */);
+ $modified = filemtime(/* .VHOST_DOCUMENT_ROOT */ . $requestObject->requestFilePath);
// Set Last-Modified-Header as RFC 2822
$requestObject->setHeader('Last-Modified', date('r', $modified));
// Check for If-Modified-Since
- if(strtotime(/* .GET_REQUEST_HEADER '"if-modified-since"' */) == $modified) {
- /* .ANSWER_CODE */ = 304;
+ if(isset($requestObject->requestHeaders["if-modified-since"]) && strtotime($requestObject->requestHeaders["if-modified-since"]) == $modified) {
+ $requestObject->answerCode = 304;
goto write;
}
- #.ifdef 'SUPPORT_DIRECTORY_LISTINGS'
+#.ifdef 'SUPPORT_DIRECTORY_LISTINGS'
// Check for directory
- if(is_dir(/* .VHOST_DOCUMENT_ROOT */ . /* .REQUEST_FILE_PATH */)) {
+ if(is_dir(/* .VHOST_DOCUMENT_ROOT */ . $requestObject->requestFilePath)) {
$files = array();
- foreach(scandir(/* .VHOST_DOCUMENT_ROOT */ . /* .REQUEST_FILE_PATH */) as $file) {
+ foreach(scandir(/* .VHOST_DOCUMENT_ROOT */ . $requestObject->requestFilePath) as $file) {
if($file == '.')
continue;
- $isDir = is_dir(/* .VHOST_DOCUMENT_ROOT */ . /* .REQUEST_FILE_PATH*/ . $file);
+ $isDir = is_dir(/* .VHOST_DOCUMENT_ROOT */ . $requestObject->requestFilePath . $file);
$files[] =
array('name' => $file,
- 'address' => 'http://' . /* .GET_REQUEST_HEADER '"host"' */ . /* .REQUEST_FILE_PATH*/ . $file . ($isDir ? '/' : ''),
+ 'address' => 'http://' . $requestObject->requestHeaders["host"] . $requestObject->requestFilePath . $file . ($isDir ? '/' : ''),
'directory' => $isDir,
'type' => MIME::typeOf($file),
- 'modified' => filemtime(/* .VHOST_DOCUMENT_ROOT */ . /* .REQUEST_FILE_PATH*/ . $file),
- 'size' => filesize(/* .VHOST_DOCUMENT_ROOT */ . /* .REQUEST_FILE_PATH*/ . $file));
+ 'modified' => filemtime(/* .VHOST_DOCUMENT_ROOT */ . $requestObject->requestFilePath . $file),
+ 'size' => filesize(/* .VHOST_DOCUMENT_ROOT */ . $requestObject->requestFilePath . $file));
}
$requestObject->setHeader('Content-Type', 'text/html; charset=utf-8');
@@ -878,84 +873,94 @@
if(!include(/* .VHOST_DIRECTORY_PAGE_HANDLER */))
include 'php/directoryPageHandler.php';
- /* .ANSWER_BODY */ = ob_get_clean();
+ $requestObject->answerBody = ob_get_clean();
unset($files);
} else {
- #.endif
- $requestObject->setHeader('Content-Type', /* .MIME_TYPE */);
+#.endif
+ // We have a static file requested.
+ $requestObject->setHeader('Content-Type', $requestObject->mimeType);
$requestObject->setHeader('Accept-Ranges', 'bytes');
- #.ifdef 'SUPPORT_GZIP'
- // Check if GZIP-compression should be used
- if(/* .ACCEPTS_COMPRESSION "'gzip'" */ && /* .VHOST_ALLOW_GZIP_COMPRESSION */ === true && filesize(/* .VHOST_DOCUMENT_ROOT */ . /* .REQUEST_FILE_PATH */) >= /* .VHOST_GZIP_MINIMUM */
- #.ifdef 'SUPPORT_GZIP_MIME_TYPE_LIMIT'
- && /* .VHOST */->gzipMimeTypes && in_array(/* .MIME_TYPE */, /* .VHOST */->gzipMimeTypes)
- #.endif
+#.ifdef 'SUPPORT_GZIP'
+ // Check if GZIP compression should be used
+ if(isset($requestObject->acceptedCompressions["gzip"]) && /* .VHOST_ALLOW_GZIP_COMPRESSION */ === true && filesize(/* .VHOST_DOCUMENT_ROOT */ . $requestObject->requestFilePath) >= /* .VHOST_GZIP_MINIMUM */
+#.ifdef 'SUPPORT_GZIP_MIME_TYPE_LIMIT'
+ && /* .VHOST */->gzipMimeTypes && in_array($requestObject->mimeType, /* .VHOST */->gzipMimeTypes)
+#.endif
) {
- // Set encoding-header
+ // Set encoding header
$requestObject->setHeader('Content-Encoding', 'gzip');
// Create temporary file
$gzipPath[$socket] = tempnam(/* .call 'Pancake\Config::get' 'main.tmppath' */, 'GZIP');
$gzipFileHandle = gzopen($gzipPath[$socket], 'w' . /* .VHOST_GZIP_LEVEL */);
// Load uncompressed requested file
- $requestedFileHandle = fopen(/* .VHOST_DOCUMENT_ROOT */ . /* .REQUEST_FILE_PATH */, 'r');
+ $requestedFileHandle = fopen(/* .VHOST_DOCUMENT_ROOT */ . $requestObject->requestFilePath, 'r');
// Compress file
while(!feof($requestedFileHandle))
gzwrite($gzipFileHandle, fread($requestedFileHandle, /* .WRITE_LIMIT */));
// Close GZIP-resource and open normal file-resource
gzclose($gzipFileHandle);
$requestFileHandle[$socket] = fopen($gzipPath[$socket], 'r');
// Set Content-Length
- $requestObject->setHeader('Content-Length', filesize($gzipPath[$socket]) - /* .RANGE_FROM */);
+ $requestObject->setHeader('Content-Length', filesize($gzipPath[$socket]) - $requestObject->rangeFrom);
} else {
- #.endif
- $requestObject->setHeader('Content-Length', filesize(/* .VHOST_DOCUMENT_ROOT */ . /* .REQUEST_FILE_PATH */) - /* .RANGE_FROM */);
- $requestFileHandle[$socket] = fopen(/* .VHOST_DOCUMENT_ROOT */ . /* .REQUEST_FILE_PATH */, 'r');
- #.ifdef 'SUPPORT_GZIP'
+#.endif
+ // No GZIP
+ $requestObject->setHeader('Content-Length', filesize(/* .VHOST_DOCUMENT_ROOT */ . $requestObject->requestFilePath) - $requestObject->rangeFrom);
+ $requestFileHandle[$socket] = fopen(/* .VHOST_DOCUMENT_ROOT */ . $requestObject->requestFilePath, 'r');
+#.ifdef 'SUPPORT_GZIP'
}
- #.endif
+#.endif
// Check if a specific range was requested
- if(/* .RANGE_FROM */) {
- /* .ANSWER_CODE */ = 206;
- fseek($requestFileHandle[$socket], /* .RANGE_FROM */);
- #.ifdef 'SUPPORT_GZIP'
+ if($requestObject->rangeFrom) {
+ $requestObject->answerCode = 206;
+ fseek($requestFileHandle[$socket], $requestObject->rangeFrom);
+#.ifdef 'SUPPORT_GZIP'
if($gzipPath[$socket])
- $requestObject->setHeader('Content-Range', 'bytes ' . /* .RANGE_FROM */.'-'.(filesize($gzipPath[$socket]) - 1).'/'.filesize($gzipPath[$socket]));
+ $requestObject->setHeader('Content-Range', 'bytes ' . $requestObject->rangeFrom . '-' . (filesize($gzipPath[$socket]) - 1) . '/' . filesize($gzipPath[$socket]));
else
- #.endif
- $requestObject->setHeader('Content-Range', 'bytes ' . /* .RANGE_FROM */.'-'.(filesize(/* .VHOST_DOCUMENT_ROOT */ . /* .REQUEST_FILE_PATH */) - 1).'/'.filesize(/* .VHOST_DOCUMENT_ROOT */ . /* .REQUEST_FILE_PATH */));
+#.endif
+ $requestObject->setHeader('Content-Range', 'bytes ' . $requestObject->rangeFrom . '-' . (filesize(/* .VHOST_DOCUMENT_ROOT */ . $requestObject->requestFilePath) - 1).'/'.filesize(/* .VHOST_DOCUMENT_ROOT */ . $requestObject->requestFilePath));
}
- #.ifdef 'SUPPORT_DIRECTORY_LISTINGS'
+#.ifdef 'SUPPORT_DIRECTORY_LISTINGS'
}
- #.endif
+#.endif
write:
- // Get Answer Headers
- $writeBuffer[$socket] = /* .BUILD_ANSWER_HEADERS */;
+ // Build answer headers and answer body as string
+ $writeBuffer[$socket] = $requestObject->buildAnswerHeaders();
- #.if #.Pancake\Config::get("main.prebuffer", 0)
+#.if #.Pancake\Config::get("main.prebuffer", 0)
+ // Add some data to the buffer to save a write cycle
if(isset($requestFileHandle[$socket])) {
$writeBuffer[$socket] .= fread($requestFileHandle[$socket], /* .number #.Pancake\Config::get("main.prebuffer", 0)*/);
}
- #.endif
+#.endif
// Output request information
- out('REQ './* .ANSWER_CODE */.' './* .REMOTE_IP */.': './* .REQUEST_LINE */.' on vHost '.((/* .VHOST */) ? /* .VHOST_NAME */ : null).' (via './* .GET_REQUEST_HEADER '"host"' */.' from './* .GET_REQUEST_HEADER "'referer'" */.') - './* .GET_REQUEST_HEADER '"user-agent"' */
- #.ifdef 'SUPPORT_TLS'
+ out('REQ '
+ . $requestObject->answerCode . ' '
+ . $requestObject->remoteIP . ': '
+ . $requestObject->requestLine . ' on vHost '
+ . /* .VHOST_NAME */
+ . ' (via ' . $requestObject->requestHeaders["host"]
+ . ' from ' . (isset($requestObject->requestHeaders["referer"]) ? $requestObject->requestHeaders["referer"] : "") . ') - '
+ . (isset($requestObject->requestHeaders["user-agent"]) ? $requestObject->requestHeaders["user-agent"] : "")
+#.ifdef 'SUPPORT_TLS'
. (isset($TLSConnections[$socket]) ? " - " . TLSCipherName($socket) : "")
- #.endif
+#.endif
, OUTPUT_REQUEST | OUTPUT_LOG);
// Check if user wants keep-alive connection
if($requestObject->answerHeaders["connection"] == 'keep-alive')
KeepAlive($socket, true);
- #.if 0 < #.call 'Pancake\Config::get' 'main.requestworkerlimit'
+#.if 0 < #.call 'Pancake\Config::get' 'main.requestworkerlimit'
// Increment amount of processed requests
$processedRequests++;
- #.endif
+#.endif
// Clean some data now to improve RAM usage
unset($postData[$socket]);
@@ -965,31 +970,33 @@
// The buffer should usually only be empty if the hard limit was reached - In this case Pancake won't allocate any buffers except when the client really IS ready to receive data
if(!strlen($writeBuffer[$socket])
- #.ifdef 'SUPPORT_TLS'
+#.ifdef 'SUPPORT_TLS'
&& isset($requestFileHandle[$socket])
- #.endif
+#.endif
)
$writeBuffer[$socket] = fread($requestFileHandle[$socket], /* .call 'Pancake\Config::get' 'main.writebuffermin' */);
- #.ifdef 'SUPPORT_TLS'
+#.ifdef 'SUPPORT_TLS'
if(isset($TLSConnections[$socket])) {
$TLSConnections[$socket] = /* .TLS_WRITE */;
if(($writtenLength = TLSWrite($socket, $writeBuffer[$socket])) === false)
goto close;
if($writtenLength == -1) {
+ // OpenSSL wants read (possible renegotiation)
$listenSocketsOrig[$socket] = $socket;
goto clean;
}
if($writtenLength == 0) {
+ // OpenSSL wants write
$liveWriteSocketsOrig[$socket] = $socket;
goto clean;
}
$writeBuffer[$socket] = substr($writeBuffer[$socket], $writtenLength);
} else
- #.endif
+#.endif
// Write data to socket
if(@WriteBuffer($socket, $writeBuffer[$socket]) === false)
@@ -999,26 +1006,26 @@
if(strlen($writeBuffer[$socket]) < #.call 'Pancake\Config::get' 'main.writebuffermin'
&& isset($requestFileHandle[$socket])
&& !feof($requestFileHandle[$socket])
- #.if #.call 'Pancake\Config::get' 'main.writebufferhardmaxconcurrent'
+#.if #.call 'Pancake\Config::get' 'main.writebufferhardmaxconcurrent'
&& count($writeBuffer) < #.call 'Pancake\Config::get' 'main.writebufferhardmaxconcurrent'
- #.endif
- #.if #.call 'Pancake\Config::get' 'main.allowhead'
- && /* .REQUEST_TYPE */ != 'HEAD'
- #.endif
+#.endif
+#.if #.call 'Pancake\Config::get' 'main.allowhead'
+ && $requestObject->requestType != 'HEAD'
+#.endif
)
$writeBuffer[$socket] .= fread($requestFileHandle[$socket],
- #.if #.call 'Pancake\Config::get' 'main.writebuffersoftmaxconcurrent'
+#.if #.call 'Pancake\Config::get' 'main.writebuffersoftmaxconcurrent'
(count($writeBuffer) > /* .call 'Pancake\Config::get' 'main.writebuffersoftmaxconcurrent' */ ? /* .call 'Pancake\Config::get' 'main.writebuffermin' */ : /* .WRITE_LIMIT */)
- #.else
+#.else
#.WRITE_LIMIT
- #.endif
+#.endif
- strlen($writeBuffer[$socket]));
- // Check if more data is available
+ // Check if more data is available and we should store the socket for another write cycle
if(strlen($writeBuffer[$socket]) || (isset($requestFileHandle[$socket]) && !feof($requestFileHandle[$socket])
- #.if #.call 'Pancake\Config::get' 'main.allowhead'
- && /* .REQUEST_TYPE */ != 'HEAD'
- #.endif
+#.if #.call 'Pancake\Config::get' 'main.allowhead'
+ && $requestObject->requestType != 'HEAD'
+#.endif
)) {
// Event-based writing - In the time the client is still downloading we can process other requests
$liveWriteSocketsOrig[$socket] = $socket;
@@ -1029,12 +1036,13 @@
// Close socket
if(!isset($requestObject) || $requestObject->answerHeaders["connection"] != 'keep-alive') {
- #.ifdef 'SUPPORT_TLS'
+#.ifdef 'SUPPORT_TLS'
if(isset($TLSConnections[$socket])) {
+ // Do TLS protocol shutdown
unset($TLSConnections[$socket]);
TLSShutdown($socket);
}
- #.endif
+#.endif
//@socket_shutdown($requestSocket);
Close($socket);
@@ -1043,28 +1051,29 @@
$listenSocketsOrig[$socket] = $socket;
#.ifdef 'SUPPORT_TLS'
if(isset($TLSConnections[$socket])) {
+ // Reset TLS connection status from WRITE to READ
$TLSConnections[$socket] = /* .TLS_READ */;
}
#.endif
}
- #.ifdef 'SUPPORT_WAITSLOTS'
+#.ifdef 'SUPPORT_WAITSLOTS'
unset($waitSlotsOrig[$socket]);
unset($waits[$socket]);
- #.endif
+#.endif
unset($socketData[$socket]);
unset($postData[$socket]);
unset($liveReadSockets[$socket]);
unset($requests[$socket]);
unset($writeBuffer[$socket]);
- #.ifdef 'SUPPORT_GZIP'
+#.ifdef 'SUPPORT_GZIP'
if(isset($gzipPath[$socket])) {
unlink($gzipPath[$socket]);
unset($gzipPath[$socket]);
}
- #.endif
+#.endif
unset($liveWriteSocketsOrig[$socket]);
@@ -1073,7 +1082,8 @@
unset($requestFileHandle[$socket]);
}
- #.if Pancake\DEBUG_MODE === true
+#.if Pancake\DEBUG_MODE === true
+ // Pancake profiler
if($results = benchmarkFunction(null, true)) {
foreach($results as $function => $functionResults) {
foreach($functionResults as $result)
@@ -1087,19 +1097,20 @@
unset($functionResults);
unset($results);
}
- #.endif
+#.endif
+#.if 0 < #.call 'Pancake\Config::get' 'main.requestworkerlimit'
// Check if request-limit is reached
- #.if 0 < #.call 'Pancake\Config::get' 'main.requestworkerlimit'
if($processedRequests >= /* .call 'Pancake\Config::get' 'main.requestworkerlimit' */) {
Write($Pancake_currentThread->socket, "EXPECTED_SHUTDOWN");
$doShutdown = true;
}
- #.endif
+#.endif
clean:
- #.if #.call 'Pancake\Config::get' 'main.maxconcurrent'
+#.if #.call 'Pancake\Config::get' 'main.maxconcurrent'
+ // Do concurrency limit
if(isset($decliningNewRequests) && /* .call 'Pancake\Config::get' 'main.maxconcurrent' */ > count($listenSocketsOrig)) {
foreach($Pancake_sockets as $socket)
$listenSocketsOrig[$socket] = $socket;
@@ -1111,32 +1122,34 @@
unset($listenSocketsOrig[$socket]);
$decliningNewRequests = true;
}
- #.endif
+#.endif
// Clean old data
unset($bytes);
+ // We have some checks for whether the requestObject is set so we MUST unset it
unset($requestObject);
// If jobs are waiting, execute them before select()ing again
if($listenSockets || $liveWriteSockets
- #.ifdef 'SUPPORT_WAITSLOTS'
+#.ifdef 'SUPPORT_WAITSLOTS'
|| $waitSlots
- #.endif
+#.endif
)
goto cycle;
+ // Check if we are ready to shutdown if we should
if(isset($doShutdown)
&& !$requests) {
break;
}
$listenSockets = $listenSocketsOrig;
$liveWriteSockets = $liveWriteSocketsOrig;
- #.ifdef 'SUPPORT_WAITSLOTS'
+#.ifdef 'SUPPORT_WAITSLOTS'
$waitSlots = $waitSlotsOrig;
- #.endif
+#.endif
- // Reset statcache
+ // Reset PHP statcache
clearstatcache();
}
?>

0 comments on commit e507ef7

Please sign in to comment.
Something went wrong with that request. Please try again.