Skip to content
This repository
Browse code

Code cleanup

  • Loading branch information...
commit e507ef73a0087c934372949fb7ee64f4a6442974 1 parent 45932f7
Yussuf Khalil authored
32 conf/config.example.yml
... ... @@ -1,13 +1,13 @@
1 1 main:
2 2 tmpPath: ../tmp/ # Path for temporary files
3 3 logging:
4   - system: ../log/system.log # Path to System-Log
5   - request: ../log/requests.log # Path to Request-Log
6   - error: ../log/error.log # Path to Error-Log
  4 + system: ../log/system.log # Path to system log file
  5 + request: ../log/requests.log # Path to request log file
  6 + error: ../log/error.log # Path to error log file
7 7 dateFormat: 'd.m.y H:i:s' # Format for timestamps - See php.net/date for more information
8 8 debugMode: false # Enable debugging (debugging can also be enabled for a single run by setting the option --debug)
9   - user: www-data # *NIX-User under which Pancake should run
10   - group: www-data # *NIX-Group under which Pancake should run
  9 + user: www-data # *NIX user for Pancake
  10 + group: www-data # *NIX group for Pancake
11 11 listenPorts: # List of ports to listen on (Recommended for HTTP: 80)
12 12 - 80
13 13 IPv4: # IPv4 network interfaces to listen on (use 0 to listen on all available interfaces)
@@ -15,25 +15,25 @@ main:
15 15 IPv6: # IPv6 network interfaces to listen on (use ::0 to listen on all available interfaces)
16 16 - ::0
17 17 requestWorkers: 2 # Amount of RequestWorkers to run
18   - exposePancake: true # If true, Pancake will send a "Server"-Header with Information about Pancake
  18 + exposePancake: true # If true, Pancake will send a "Server" header with information about Pancake
19 19 sizePrefix: bin # "si" or "bin" - SI: 1000 Byte = 1 Kilobyte - Binary: 1024 Byte = 1 Kibibyte
20   - allowHEAD: true # Whether to allow the "HEAD"-requestmethod
21   - allowTRACE: false # Whether to allow the "TRACE"-requestmethod
22   - allowOPTIONS: false # Whether to allow the "OPTIONS"-requestmethod
23   - requestWorkerLimit: 250000 # Limit of requests a RequestWorker may process until it has to be restarted (0 for no limit)
24   - maxConcurrent: 0 # Limit of concurrent connections a RequestWorker may hold open (0 for no limit)
25   - workerBootTime: 5 # The upper timelimit for a worker to boot (raise this when you're for example caching files that take long to load)
  20 + allowHEAD: true # Whether to allow HEAD requests
  21 + allowTRACE: false # Whether to allow TRACE requests
  22 + allowOPTIONS: false # Whether to allow OPTIONS requests
  23 + requestWorkerLimit: 250000 # Limit of requests a RequestWorker may process until it will be restarted (0 for no limit)
  24 + maxConcurrent: 0 # Limit of concurrent connections a RequestWorker may keep open (0 for no limit)
  25 + 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)
26 26 writeBufferMin: 10240 # Minimum write buffer size that should be held in RAM
27 27 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
28 28 writeBufferHardMaxConcurrent: 2000 # Hard concurrent write buffer limit - When this limit is reached, Pancake will try not to allocate any buffers anymore
29   - waitPHPWorkerBoot: true # Wait for PHP-Workers to boot (should be disabled in shared environments)
30   - waitSlotTime: 5000 # When there is no PHPWorker available to serve a PHP-request, Pancake will check after waitslottime µsec if a slot became available
31   - 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)
  29 + waitPHPWorkerBoot: true # Wait for PHPWorkers to boot (should be disabled in shared environments)
  30 + 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)
  31 + 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
32 32 socketBacklog: 200 # Connection backlog for incoming requests
33 33 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
34 34 NaglesAlgorithm: false # Enable or disable Nagle's algorithm (will set TCP_NODELAY when false)
35 35 PreBuffer: 65536 # Buffer static file data in RAM before sending HTTP headers (can save one write cycle)
36   -include: # Additional configuration-files to include
  36 +include: # Additional configuration files to include
37 37 - ../conf/vhosts/
38 38 - ../conf/mime.yml
39 39 moody:
34 conf/vhost.example.yml
... ... @@ -1,25 +1,25 @@
1 1 vhosts:
2   - default: # Name of the vHost
  2 + default: # Name of the vHost REMEMBER TO SET THIS WHEN CREATING A NEW VHOST
3 3 enabled: true
4 4 docRoot: ../vhosts/default/ # Path to document root
5   - listen: # Hosts to listen on
  5 + listen: # Named hosts to listen on
6 6 - 127.0.0.1
7 7 - localhost
8 8 - "[::1]"
9   - PHPWorkers: 2 # Amount of PHP-Workers to run
10   - gzipMin: 1048576 # Minimum static filesize to use GZIP-compression
11   - gzipLevel: 1 # Level of the GZIP-compression - Lower = bigger size, less CPU-usage
12   - 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
  9 + PHPWorkers: 2 # Amount of PHPWorkers to run
  10 + gzipMin: 1048576 # Minimum size of a static file to use GZIP compression
  11 + gzipLevel: 1 # Level of GZIP compression - Lower = bigger size, less CPU-usage
  12 + 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
13 13 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
14   - gzipMimeTypes: # Allow limiting automatic GZIP compression to specific MIME types (does not apply to static GZIP compression)
  14 + gzipMimeTypes: # Allow limiting automatic GZIP compression to specific MIME types (does not apply to gzipStatic)
15 15 # - image/png
16 16 # - application/x-javascript
17 17 allowDirectoryListings: true # Enable directory listings
18 18 isDefault: true # Whether this vHost is the default vHost or not (one of your vHosts MUST have this settings enabled)
19 19 204OnEmptyPage: false # If true, Pancake will send 204 No Content instead of 200 OK when the page is empty
20 20 PHPWorkerLimit: 1000 # Limit of requests a PHPWorker may process until it has to be restarted (0 for no limit)
21   - PHPInfoPancake: true # Expose information about Pancake's configuration in phpinfo()
22   - PHPInfoPancakevHosts: false # Expose information about Pancake's vHosts in phpinfo() (requires phpinfopancake to be true)
  21 + PHPInfoPancake: true # Expose information about Pancake configuration in phpinfo()
  22 + PHPInfoPancakevHosts: false # Expose information about Pancake vHosts in phpinfo() (requires PHPInfoPancake to be true)
23 23 autoDelete: # Automatically delete non-cached PHP elements after a request
24 24 classes: true
25 25 functions: true
@@ -31,13 +31,13 @@ vhosts:
31 31 excludeDelete: # Exclude the specified PHP elements from automatic deletion after a request
32 32 PHPHTMLErrors: true # Enable HTML errors instead of plain text
33 33 PHPSocketBacklog: 100 # Connection backlog for internal worker communication sockets (recommendation: less workers => higher backlog; more workers => lower backlog - should not be lower than 1)
34   - 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
35   - 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)
36   - 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
37   - 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)
  34 + 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
  35 + 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)
  36 + 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
  37 + 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)
38 38 PHPResetObjectsDestroyDestructors: true # Whether the SAPI should not execute object destructors when destroying objects (true = will not execute; false = will execute)
39   - PHPMaxExecutionTime: 10 # Same function as PHP's max_execution_time-setting
40   -# PHPModules: # Load PHP modules for this vHost
  39 + PHPMaxExecutionTime: 10 # Same as PHPs max_execution_time setting
  40 +# PHPModules: # Load custom PHP modules for this vHost
41 41 # - gd
42 42 # PHPINISettings: # Set custom php.ini settings for this vHost
43 43 # session.name: "myGreatSession"
@@ -45,7 +45,7 @@ vhosts:
45 45 # myConst: 123
46 46 # iLovePancakes: true
47 47 # PHPDeletePredefinedConstantsAfterCodeCacheLoad: false # Whether the predefined constants should be deleted again after loading the CodeCache
48   -# PHPDisabledFunctions: # A list of functions that will be disabled inside the SAPI (acts like the php.ini-setting disabled_functions)
  48 +# PHPDisabledFunctions: # A list of functions that will be disabled inside the SAPI (acts like the php.ini setting disabled_functions)
49 49 # - posix_getpid
50 50 # - posix_kill
51 51 # PHPCache: # Files or directories that should be cached by Pancake
@@ -72,7 +72,7 @@ vhosts:
72 72 # headers:
73 73 # X-Reason: 403
74 74 # - pathinfo: ~^((?U).+\.php)(/?.+)$~
75   - index: # List of files allowed as directory-indexes
  75 + index: # List of files allowed as directory indexes
76 76 - index.php
77 77 - index.html
78 78 - index.htm
34 sys/FastCGI.class.php
@@ -121,27 +121,27 @@ public function makeRequest(HTTPRequest $requestObject, $requestSocket) {
121 121 }
122 122
123 123 /* FCGI_PARAMS */
124   - $body = "\xf" . chr(strlen(/* .VHOST_DOCUMENT_ROOT */ . /* .REQUEST_FILE_PATH */)) . "SCRIPT_FILENAME" . /* .VHOST_DOCUMENT_ROOT */ . /* .REQUEST_FILE_PATH */;
125   - $body .= "\xc" . chr(strlen(/* .QUERY_STRING */)) . "QUERY_STRING" . /* .QUERY_STRING */;
126   - $body .= "\xe" . chr(strlen(/* .REQUEST_TYPE */)) . "REQUEST_METHOD" . /* .REQUEST_TYPE */;
127   - $body .= "\xb" . chr(strlen(/* .REQUEST_FILE_PATH */)) . "SCRIPT_NAME" . /* .REQUEST_FILE_PATH */;
128   - $body .= "\xf\x8SERVER_PROTOCOLHTTP/" . /* .PROTOCOL_VERSION */;
  124 + $body = "\xf" . chr(strlen(/* .VHOST_DOCUMENT_ROOT */ . $requestObject->requestFilePath)) . "SCRIPT_FILENAME" . /* .VHOST_DOCUMENT_ROOT */ . $requestObject->requestFilePath;
  125 + $body .= "\xc" . chr(strlen($requestObject->queryString)) . "QUERY_STRING" . $requestObject->queryString;
  126 + $body .= "\xe" . chr(strlen($requestObject->requestType)) . "REQUEST_METHOD" . $requestObject->requestType;
  127 + $body .= "\xb" . chr(strlen($requestObject->requestFilePath)) . "SCRIPT_NAME" . $requestObject->requestFilePath;
  128 + $body .= "\xf\x8SERVER_PROTOCOLHTTP/" . $requestObject->protocolVersion;
129 129 $body .= "\x11\x7GATEWAY_INTERFACECGI/1.1";
130 130 $body .= "\xb" . chr(strlen($requestObject->originalRequestURI)) . "REQUEST_URI" . $requestObject->originalRequestURI;
131   - $body .= "\xc" . chr(strlen(/* .REQUEST_URI */)) . "DOCUMENT_URI" . /* .REQUEST_URI */;
132   - $body .= "\xb" . chr(strlen(/* .REMOTE_IP */)) . "REMOTE_ADDR" . /* .REMOTE_IP */;
  131 + $body .= "\xc" . chr(strlen($requestObject->requestURI)) . "DOCUMENT_URI" . $requestObject->requestURI;
  132 + $body .= "\xb" . chr(strlen($requestObject->remoteIP)) . "REMOTE_ADDR" . $requestObject->remoteIP;
133 133 $body .= "\xb" . chr(strlen(/* .VHOST */->listen[0])) . "SERVER_NAME" . /* .VHOST */->listen[0];
134   - $body .= "\xb" . chr(strlen(/* .LOCAL_PORT */)) . "SERVER_PORT" . /* .LOCAL_PORT */;
  134 + $body .= "\xb" . chr(strlen($requestObject->localPort)) . "SERVER_PORT" . $requestObject->localPort;
135 135 $body .= /* .eval 'return "\xf" . chr(strlen("Pancake/" . \Pancake\VERSION)) . "SERVER_SOFTWAREPancake/" . \Pancake\VERSION;' */;
136   - $body .= "\xb" . chr(strlen(/* .LOCAL_IP */)) . "SERVER_ADDR" . /* .LOCAL_IP */;
  136 + $body .= "\xb" . chr(strlen($requestObject->localIP)) . "SERVER_ADDR" . $requestObject->localIP;
137 137 if($requestObject->pathInfo) {
138 138 $body .= "\x9" . chr(strlen($requestObject->pathInfo)) . "PATH_INFO" . $requestObject->pathInfo;
139 139 $body .= "\xf" . chr(strlen($requestObject->vHost->documentRoot . $requestObject->pathInfo)) . "PATH_TRANSLATED" . $requestObject->vHost->documentRoot . $requestObject->pathInfo;
140 140 }
141 141
142   - if(/* .RAW_POST_DATA */) {
143   - $body .= "\xc" . chr(strlen(/* .GET_REQUEST_HEADER '"content-type"' */)) . "CONTENT_TYPE" . /* .GET_REQUEST_HEADER '"content-type"' */;
144   - $body .= "\xe" . chr(strlen(/* .GET_REQUEST_HEADER '"content-length"' */)) . "CONTENT_LENGTH" . /* .GET_REQUEST_HEADER '"content-length"' */;
  142 + if($requestObject->rawPOSTData) {
  143 + $body .= "\xc" . chr(strlen($requestObject->requestHeaders["content-type"])) . "CONTENT_TYPE" . $requestObject->requestHeaders["content-type"];
  144 + $body .= "\xe" . chr(strlen($requestObject->requestHeaders["content-length"])) . "CONTENT_LENGTH" . $requestObject->requestHeaders["content-length"];
145 145 }
146 146
147 147 // HTTP header data
@@ -167,8 +167,8 @@ public function makeRequest(HTTPRequest $requestObject, $requestSocket) {
167 167 /* Empty FCGI_PARAMS */
168 168 Write($this->socket, "\1\4" . $requestID . "\0\0\0\0");
169 169
170   - if(/* .RAW_POST_DATA */) {
171   - $rawPostData = str_split(/* .RAW_POST_DATA */, 65535);
  170 + if($requestObject->rawPOSTData) {
  171 + $rawPostData = str_split($requestObject->rawPOSTData, 65535);
172 172
173 173 foreach($rawPostData as $recordData) {
174 174 /* FCGI_STDIN */
@@ -231,18 +231,18 @@ public function upstreamRecord($data, $socketID) {
231 231 foreach(explode("\r\n", $contentBody[0]) as $header) {
232 232 list($headerName, $headerValue) = explode(":", $header, 2);
233 233 if($headerName == 'Status') {
234   - /* .ANSWER_CODE */ = (int) $headerValue;
  234 + $requestObject->answerCode = (int) $headerValue;
235 235 continue;
236 236 }
237 237 $requestObject->setHeader(trim($headerName), trim($headerValue), false);
238 238 }
239 239
240 240 if(isset($contentBody[1]))
241   - /* .ANSWER_BODY */ .= $contentBody[1];
  241 + $requestObject->answerBody .= $contentBody[1];
242 242 return 8;
243 243 }
244 244
245   - /* .ANSWER_BODY */ .= $data;
  245 + $requestObject->answerBody .= $data;
246 246
247 247 return 8;
248 248 case /* .constant 'FCGI_END_REQUEST' */:
437 sys/threads/single/requestWorker.thread.php
@@ -22,7 +22,7 @@
22 22 #.endif
23 23
24 24 #.if #.bool #.call 'Pancake\Config::get' 'fastcgi'
25   - #.macro 'VHOST_FASTCGI' '(isset(/* .VHOST */->fastCGI[/* .MIME_TYPE */]) ? /* .VHOST */->fastCGI[/* .MIME_TYPE */] : null)'
  25 + #.macro 'VHOST_FASTCGI' '(isset(/* .VHOST */->fastCGI[$requestObject->mimeType]) ? /* .VHOST */->fastCGI[$requestObject->mimeType] : null)'
26 26 #.define 'SUPPORT_FASTCGI' true
27 27 #.endif
28 28
@@ -107,29 +107,6 @@
107 107 #.define 'SUPPORT_MULTIPLE_VHOSTS' true
108 108 #.endif
109 109
110   - #.macro 'REQUEST_TYPE' '$requestObject->requestType'
111   - #.macro 'GET_PARAMS' '$requestObject->getGETParams()'
112   - #.macro 'MIME_TYPE' '$requestObject->mimeType'
113   - #.macro 'VHOST' '$requestObject->vHost'
114   - #.macro 'REQUEST_FILE_PATH' '$requestObject->requestFilePath'
115   - #.macro 'RANGE_FROM' '$requestObject->rangeFrom'
116   - #.macro 'RANGE_TO' '$requestObject->rangeTo'
117   - #.macro 'BUILD_ANSWER_HEADERS' '$requestObject->buildAnswerHeaders()'
118   - #.macro 'ANSWER_BODY' '$requestObject->answerBody'
119   - #.macro 'REMOTE_IP' '$requestObject->remoteIP'
120   - #.macro 'REMOTE_PORT' '$requestObject->remotePort'
121   - #.macro 'REQUEST_LINE' '$requestObject->requestLine'
122   - #.macro 'ANSWER_CODE' '$requestObject->answerCode'
123   - #.macro 'UPLOADED_FILES' '$requestObject->uploadedFiles'
124   - #.macro 'GET_REQUEST_HEADER' '(isset($requestObject->requestHeaders[$headerName]) ? $requestObject->requestHeaders[$headerName] : null)' '$headerName'
125   - #.macro 'QUERY_STRING' '$requestObject->queryString'
126   - #.macro 'PROTOCOL_VERSION' '$requestObject->protocolVersion'
127   - #.macro 'REQUEST_URI' '$requestObject->requestURI'
128   - #.macro 'LOCAL_IP' '$requestObject->localIP'
129   - #.macro 'LOCAL_PORT' '$requestObject->localPort'
130   - #.macro 'RAW_POST_DATA' '$requestObject->rawPOSTData'
131   - #.macro 'ACCEPTS_COMPRESSION' 'isset($requestObject->acceptedCompressions[$compression])' '$compression'
132   -
133 110 #.longDefine 'EVAL_CODE'
134 111 global $Pancake_vHosts;
135 112
@@ -145,6 +122,7 @@
145 122
146 123 #.WRITE_LIMIT = #.eval EVAL_CODE false
147 124
  125 + #.macro 'VHOST' '$requestObject->vHost'
148 126 #.macro 'VHOST_PHP_WORKERS' '/* .VHOST */->phpWorkers'
149 127 #.macro 'VHOST_SOCKET_NAME' '/* .VHOST */->phpSocketName'
150 128 #.macro 'VHOST_DOCUMENT_ROOT' '/* .VHOST */->documentRoot'
@@ -184,7 +162,8 @@
184 162
185 163 Thread::clearCache();
186 164 MIME::load();
187   -
  165 +
  166 + // Convert vHost instances to vHostInterface instances
188 167 foreach($Pancake_vHosts as $id => &$vHost) {
189 168 if($vHost instanceof vHostInterface)
190 169 break;
@@ -204,6 +183,7 @@
204 183 $Pancake_vHosts[$address] = $vHost;
205 184 }
206 185
  186 + // This will be an unconnected socket after startup
207 187 Close($this->localSocket);
208 188
209 189 #.ifdef 'SUPPORT_AUTHENTICATION'
@@ -266,6 +246,7 @@
266 246
267 247 unset($id, $vHost, $address);
268 248
  249 + // Make sure the Pancake Module Loader can not be exploited
269 250 disableModuleLoader();
270 251
271 252 Config::workerDestroy();
@@ -275,45 +256,46 @@
275 256 $listenSockets = $listenSocketsOrig = $Pancake_sockets;
276 257
277 258 // Initialize some variables
278   - #.ifdef 'SUPPORT_TLS'
  259 +#.ifdef 'SUPPORT_TLS'
279 260 $TLSConnections = array();
280   - #.endif
281   - #.ifdef 'SUPPORT_FASTCGI'
  261 +#.endif
  262 +#.ifdef 'SUPPORT_FASTCGI'
282 263 $fastCGISockets = array();
283   - #.endif
284   - #.ifdef 'SUPPORT_AJP13'
  264 +#.endif
  265 +#.ifdef 'SUPPORT_AJP13'
285 266 $ajp13Sockets = array();
286   - #.endif
  267 +#.endif
287 268 $liveWriteSockets = array();
288 269 $liveWriteSocketsOrig = array();
289 270 $liveReadSockets = array();
290 271 $socketData = array();
291 272 $postData = array();
292   - #.if 0 < #.call 'Pancake\Config::get' 'main.requestworkerlimit'
  273 +#.if 0 < #.call 'Pancake\Config::get' 'main.requestworkerlimit'
293 274 $processedRequests = 0;
294   - #.endif
  275 +#.endif
295 276 $requests = array();
296 277 $requestFileHandle = array();
297   - #.ifdef 'SUPPORT_GZIP'
  278 +#.ifdef 'SUPPORT_GZIP'
298 279 $gzipPath = array();
299   - #.endif
  280 +#.endif
300 281 $writeBuffer = array();
301   - #.ifdef 'SUPPORT_PHP'
  282 +#.ifdef 'SUPPORT_PHP'
302 283 $phpSockets = array();
303   - #.endif
304   - #.ifdef 'SUPPORT_WAITSLOTS'
  284 +#.endif
  285 +#.ifdef 'SUPPORT_WAITSLOTS'
305 286 $waitSlots = array();
306 287 $waitSlotsOrig = array();
307 288 $waits = array();
308   - #.endif
  289 +#.endif
309 290
310   - #.if BENCHMARK === true
311   - benchmarkFunction("hexdec");
312   - #.endif
  291 +#.if BENCHMARK === true
  292 + benchmarkFunction("hexdec");
  293 +#.endif
313 294
314   - #.if #.Pancake\Config::get("main.naglesalgorithm", 0)
315   - NaglesAlgorithm(/*.bool #.Pancake\Config::get("main.naglesalgorithm", 0)*/);
316   - #.endif
  295 +#.if #.Pancake\Config::get("main.naglesalgorithm", 0)
  296 + // Disable Nagle's algorithm if requested
  297 + NaglesAlgorithm(/*.bool #.Pancake\Config::get("main.naglesalgorithm", 0)*/);
  298 +#.endif
317 299
318 300 // Ready
319 301 $Pancake_currentThread->parentSignal(/* .constant 'SIGUSR1' */);
@@ -328,9 +310,9 @@
328 310
329 311 // Wait for incoming requests
330 312 while(Select($listenSockets, $liveWriteSockets
331   - #.ifdef 'SUPPORT_WAITSLOTS'
  313 +#.ifdef 'SUPPORT_WAITSLOTS'
332 314 , $waitSlots ? /* .call 'Pancake\Config::get' 'main.waitslottime' */ : null
333   - #.endif
  315 +#.endif
334 316 )) {
335 317 // 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
336 318 cycle:
@@ -341,11 +323,12 @@
341 323
342 324 $requestObject = $requests[$socket];
343 325
344   - #.ifdef 'SUPPORT_TLS'
  326 +#.ifdef 'SUPPORT_TLS'
  327 + // For TLS handshake we need both reads and writes
345 328 if(isset($TLSConnections[$socket]) && $TLSConnections[$socket] < /* .TLS_WRITE */) {
346 329 goto TLSRead;
347 330 }
348   - #.endif
  331 +#.endif
349 332 goto liveWrite;
350 333 }
351 334
@@ -358,17 +341,19 @@
358 341 }
359 342 #.endif
360 343
361   - // New connection, downloadable content from a client or the PHP-SAPI finished a request
  344 + // New connection, data from client, AJP13 or FastCGI or PHP SAPI data or command from Pancake master process
362 345 foreach($listenSockets as $socket) {
363 346 unset($listenSockets[$socket]);
364 347
365 348 #.ifdef 'SUPPORT_TLS'
  349 + // In case of a TLS renegotiation we might need a read event while in write step
366 350 if(isset($TLSConnections[$socket]) && $TLSConnections[$socket] == /* .TLS_WRITE */) {
367 351 $requestObject = $requests[$socket];
368 352 goto liveWrite;
369 353 }
370 354 #.endif
371 355
  356 + // Data from client or new request on keep-alive socket
372 357 if(isset($liveReadSockets[$socket]) || KeepAlive($socket)) {
373 358 $requestObject = $requests[$socket];
374 359 if(!isset($socketData[$socket])) {
@@ -377,7 +362,7 @@
377 362 break;
378 363 }
379 364
380   - #.ifdef 'SUPPORT_AJP13'
  365 +#.ifdef 'SUPPORT_AJP13'
381 366 if(isset($ajp13Sockets[$socket])) {
382 367 $ajp13 = $ajp13Sockets[$socket];
383 368
@@ -413,9 +398,9 @@
413 398 unset($data);
414 399 goto clean;
415 400 }
416   - #.endif
  401 +#.endif
417 402
418   - #.ifdef 'SUPPORT_FASTCGI'
  403 +#.ifdef 'SUPPORT_FASTCGI'
419 404 if(isset($fastCGISockets[$socket])) {
420 405 $fastCGI = $fastCGISockets[$socket];
421 406 do {
@@ -453,9 +438,9 @@
453 438 unset($newData);
454 439 goto clean;
455 440 }
456   - #.endif
  441 +#.endif
457 442
458   - #.ifdef 'SUPPORT_PHP'
  443 +#.ifdef 'SUPPORT_PHP'
459 444 if(isset($phpSockets[$socket])) {
460 445 $packages = hexdec(Read($socket, 8));
461 446 if(!$packages) {
@@ -510,45 +495,47 @@
510 495
511 496 goto write;
512 497 }
513   - #.endif
  498 +#.endif
514 499
515 500 // Internal communication
516 501 if($socket == $this->socket) {
517 502 switch(Read($this->socket, 128)) {
518 503 case "GRACEFUL_SHUTDOWN":
519   - // Stop listening for new connections
  504 + // Master wants us to shutdown - stop listening for new connections
520 505 foreach($Pancake_sockets as $socket)
521 506 unset($listenSocketsOrig[$socket]);
522 507
523 508 $doShutdown = true;
524 509 goto clean;
525 510 case "LOAD_FILE_POINTERS":
  511 + // Master got SIGHUP and wants us to flush the file pointers
526 512 LoadFilePointers();
527 513 goto clean;
528 514 }
529 515 }
530 516
531 517 if(
532   - #.if 0 != #.call 'Pancake\Config::get' 'main.maxconcurrent'
  518 +#.if 0 != #.call 'Pancake\Config::get' 'main.maxconcurrent'
533 519 /* .call 'Pancake\Config::get' 'main.maxconcurrent' */ < count($listenSocketsOrig) - count($Pancake_sockets) ||
534   - #.endif
  520 +#.endif
535 521 !($socket = @NonBlockingAccept($socket)))
536 522 goto clean;
537 523
538   - #.ifdef 'SUPPORT_TLS'
  524 +#.ifdef 'SUPPORT_TLS'
539 525 GetSockName($socket, $xx, $port);
540 526
  527 + // Check if we got the new connection on a TLS port
541 528 if(in_array($port, Config::get('tls.ports'))) {
542 529 $TLSConnections[$socket] = /* .TLS_ACCEPT */;
543 530 TLSInitializeConnection($socket);
544 531 }
545   - #.endif
  532 +#.endif
546 533
547 534 $socketData[$socket] = "";
548 535 break;
549 536 }
550 537
551   - #.ifdef 'SUPPORT_TLS'
  538 +#.ifdef 'SUPPORT_TLS'
552 539 TLSRead:
553 540
554 541 if(isset($TLSConnections[$socket])) {
@@ -556,26 +543,31 @@
556 543 case /* .TLS_ACCEPT */:
557 544 switch(TLSAccept($socket)) {
558 545 case 1:
  546 + // Handshake OK
559 547 $TLSConnections[$socket] = /* .TLS_READ */;
560 548 case 2:
  549 + // OpenSSL wants read
561 550 $liveReadSockets[$socket] = true;
562 551 $listenSocketsOrig[$socket] = $socket;
563 552
564 553 goto clean;
565 554 case 3:
  555 + // OpenSSL wants write
566 556 $liveWriteSocketsOrig[$socket] = $socket;
567 557 goto clean;
568 558 case 0:
  559 + // Error
569 560 goto close;
570 561 }
571 562 break;
572 563 case /* .TLS_READ */:
573 564 if(isset($requests[$socket]))
574   - $bytes = TLSRead($socket, /* .GET_REQUEST_HEADER '"content-length"' */ - strlen($postData[$socket]));
  565 + $bytes = TLSRead($socket, $requestObject->requestHeaders["content-length"] - strlen($postData[$socket]));
575 566 else
576 567 $bytes = TLSRead($socket, 10240);
577 568
578 569 if($bytes === 2) {
  570 + // OpenSSL wants read
579 571 $liveReadSockets[$socket] = true;
580 572 $listenSocketsOrig[$socket] = $socket;
581 573
@@ -583,17 +575,18 @@
583 575 }
584 576
585 577 if($bytes === 3) {
  578 + // OpenSSL wants write
586 579 $liveWriteSocketsOrig[$socket] = $socket;
587 580 goto clean;
588 581 }
589 582 break;
590 583 }
591 584 } else
592   - #.endif
  585 +#.endif
593 586
594 587 // Receive data from client
595 588 if(isset($requests[$socket]))
596   - $bytes = @Read($socket, /* .GET_REQUEST_HEADER '"content-length"' */ - strlen($postData[$socket]));
  589 + $bytes = @Read($socket, $requestObject->requestHeaders["content-length"] - strlen($postData[$socket]));
597 590 else
598 591 $bytes = @Read($socket, 10240);
599 592
@@ -605,14 +598,14 @@
605 598 // Check if request was already initialized and we are only reading POST-data
606 599 if(isset($requests[$socket])) {
607 600 $postData[$socket] .= $bytes;
608   - if(strlen($postData[$socket]) >= /* .GET_REQUEST_HEADER '"content-length"' */)
  601 + if(strlen($postData[$socket]) >= $requestObject->requestHeaders["content-length"])
609 602 goto readData;
610 603 } else if($bytes) {
611 604 $socketData[$socket] .= $bytes;
612 605
613 606 // Check if all headers were received
614 607 if(strpos($socketData[$socket], "\r\n\r\n")) {
615   - // Check for POST
  608 + // Split POST data
616 609 if(strpos($socketData[$socket], "POST") === 0) {
617 610 $data = explode("\r\n\r\n", $socketData[$socket], 2);
618 611 $socketData[$socket] = $data[0];
@@ -628,7 +621,7 @@
628 621 if(strlen($socketData[$socket]) >= 10240)
629 622 goto close;
630 623 }
631   - // Event-based reading
  624 + // We are still waiting for header data - event-based read
632 625 $liveReadSockets[$socket] = true;
633 626 $listenSocketsOrig[$socket] = $socket;
634 627 goto clean;
@@ -639,10 +632,10 @@
639 632 // Get information about client
640 633 GetPeerName($socket, $ip, $port);
641 634
642   - // Get local IP-address and port
  635 + // Get local IP address and port
643 636 GetSockName($socket, $lip, $lport);
644 637
645   - // Create request object / Read Headers
  638 + // Create request object and process headers
646 639 try {
647 640 $requestObject = $requests[$socket] = new HTTPRequest($ip, $port, $lip, $lport);
648 641 $requestObject->init($socketData[$socket]);
@@ -656,16 +649,16 @@
656 649 }
657 650 }
658 651
659   - // Check for POST and get all POST-data
660   - if(/* .REQUEST_TYPE */ == 'POST') {
661   - if(strlen($postData[$socket]) >= /* .GET_REQUEST_HEADER '"content-length"' */) {
662   - if(strlen($postData[$socket]) > /* .GET_REQUEST_HEADER '"content-length"' */)
663   - $postData[$socket] = substr($postData[$socket], 0, /* .GET_REQUEST_HEADER '"content-length"' */);
  652 + // Check for POST and get all POST data
  653 + if($requestObject->requestType == 'POST') {
  654 + if(strlen($postData[$socket]) >= $requestObject->requestHeaders["content-length"]) {
  655 + if(strlen($postData[$socket]) > $requestObject->requestHeaders["content-length"])
  656 + $postData[$socket] = substr($postData[$socket], 0, $requestObject->requestHeaders["content-length"]);
664 657 unset($listenSocketsOrig[$socket]);
665   - /* .RAW_POST_DATA */ = $postData[$socket];
  658 + $requestObject->rawPOSTData = $postData[$socket];
666 659 unset($postData[$socket]);
667 660 } else {
668   - // Event-based reading
  661 + // We did not receive all POST data yet
669 662 $liveReadSockets[$socket] = true;
670 663 $listenSocketsOrig[$socket] = $socket;
671 664 goto clean;
@@ -673,47 +666,50 @@
673 666 } else
674 667 unset($listenSocketsOrig[$socket]);
675 668
676   - #.if #.call 'Pancake\Config::get' 'main.allowtrace'
677   - if(/* .REQUEST_TYPE */ == 'TRACE')
678   - goto write;
679   - #.endif
680   -
681   - #.if #.call 'Pancake\Config::get' 'main.allowoptions'
682   - // Check for "OPTIONS"-requestmethod
683   - if(/* .REQUEST_TYPE */ == 'OPTIONS')
684   - $requestObject->setHeader('Allow',
685   - /* .eval '$allow = "GET, POST, OPTIONS";
686   - if(Pancake\Config::get("main.allowhead") === true)
687   - $allow .= ", HEAD";
688   - if(Pancake\Config::get("main.allowtrace") === true)
689   - $allow .= ", TRACE";
690   - return $allow;' false
691   - */);
692   - #.endif
  669 +#.if #.call 'Pancake\Config::get' 'main.allowtrace'
  670 + // If we have a TRACE request we don't need to take any further action
  671 + if($requestObject->requestType == 'TRACE')
  672 + goto write;
  673 +#.endif
  674 +
  675 +#.if #.call 'Pancake\Config::get' 'main.allowoptions'
  676 + // Check for OPTIONS
  677 + #.longDefine 'EVAL_CODE'
  678 + $allow = "GET, POST, OPTIONS";
  679 + if(Pancake\Config::get("main.allowhead") === true)
  680 + $allow .= ", HEAD";
  681 + if(Pancake\Config::get("main.allowtrace") === true)
  682 + $allow .= ", TRACE";
  683 + return $allow;
  684 + #.endLongDefine
  685 + if($requestObject->requestType == 'OPTIONS')
  686 + $requestObject->setHeader('Allow', /* .eval EVAL_CODE false */);
  687 +#.endif
693 688
  689 +#.if Pancake\DEBUG_MODE === true
694 690 // Output debug information
695   - #.if Pancake\DEBUG_MODE === true
696   - if(array_key_exists('pancakedebug', /* .GET_PARAMS */)) {
  691 + if(isset($requestObject->getGETParams()['pancakedebug'])) {
697 692 $requestObject->setHeader('Content-Type', 'text/plain');
698 693
699 694 $body = 'Received Headers:' . "\r\n";
700   - $body .= /* .REQUEST_LINE */ . "\r\n";
  695 + $body .= $requestObject->requestLine . "\r\n";
701 696 $body .= $requestObject->getRequestHeaders() . "\r\n";
702 697 $body .= 'Received POST content:' . "\r\n";
703 698 $body .= $postData[$socket] . "\r\n\r\n";
704 699 $body .= 'Dump of RequestObject:' . "\r\n";
705 700 $body .= print_r($requestObject, true);
706   - /* .ANSWER_BODY */ = $body;
  701 + $requestObject->answerBody = $body;
707 702
708 703 unset($body);
709 704
710 705 goto write;
711 706 }
712   - #.endif
  707 +#.endif
713 708
714   - #.if #.call 'ini_get' 'expose_php'
715   - if(isset(/* .GET_PARAMS*/[""])) {
716   - switch(/* .GET_PARAMS*/[""]) {
  709 +#.if #.call 'ini_get' 'expose_php'
  710 + // PHP UUIDs
  711 + if(isset($requestObject->getGETParams()[""])) {
  712 + switch($requestObject->getGETParams()[""]) {
717 713 case 'PHPE9568F34-D428-11d2-A769-00AA001ACF42':
718 714 $logo = file_get_contents('logo/php.gif');
719 715 $requestObject->setHeader('Content-Type', 'image/gif');
@@ -742,11 +738,11 @@
742 738 default:
743 739 goto load;
744 740 }
745   - /* .ANSWER_BODY */ = $logo;
  741 + $requestObject->answerBody = $logo;
746 742 unset($logo);
747 743 goto write;
748 744 }
749   - #.endif
  745 +#.endif
750 746
751 747 load:
752 748
@@ -765,23 +761,23 @@
765 761 #.endif
766 762 #.endif
767 763
768   - #.ifdef 'SUPPORT_FASTCGI'
769   - // FastCGI
770   - if($fastCGI = /* .VHOST_FASTCGI */) {
771   - if($fastCGI->makeRequest($requestObject, $socket) === false)
772   - goto write;
773   - $listenSocketsOrig[$fastCGI->socket] = $fastCGI->socket;
774   - $fastCGISockets[$fastCGI->socket] = $fastCGI;
775   - goto clean;
776   - }
777   - #.endif
  764 +#.ifdef 'SUPPORT_FASTCGI'
  765 + // FastCGI
  766 + if($fastCGI = /* .VHOST_FASTCGI */) {
  767 + if($fastCGI->makeRequest($requestObject, $socket) === false)
  768 + goto write;
  769 + $listenSocketsOrig[$fastCGI->socket] = $fastCGI->socket;
  770 + $fastCGISockets[$fastCGI->socket] = $fastCGI;
  771 + goto clean;
  772 + }
  773 +#.endif
778 774
779   - #.ifdef 'SUPPORT_PHP'
  775 +#.ifdef 'SUPPORT_PHP'
780 776 // Check for PHP
781   - if(/* .MIME_TYPE */ == 'text/x-php'
782   - #.ifdef 'SUPPORT_MULTIPLE_VHOSTS'
  777 + if($requestObject->mimeType == 'text/x-php'
  778 +#.ifdef 'SUPPORT_MULTIPLE_VHOSTS'
783 779 && /* .VHOST_PHP_WORKERS */
784   - #.endif
  780 +#.endif
785 781 ) {
786 782 if(!($psocket = Socket(/* .constant 'AF_UNIX' */, /* .constant 'SOCK_SEQPACKET' */, 0))) {
787 783 $requestObject->invalidRequest(new invalidHTTPRequestException('Failed to create communication socket. Probably the server is overladed. Try again later.', 500));
@@ -789,30 +785,29 @@
789 785 }
790 786
791 787 SetBlocking($psocket, false);
792   - // @ - Do not spam errorlog with Resource temporarily unavailable if there is no PHPWorker available
793 788
794 789 if(Connect($psocket, /* .AF_UNIX */, /* .VHOST_SOCKET_NAME */)) {
795   - #.ifdef 'SUPPORT_WAITSLOTS'
796   - $waits[$socket]++;
  790 +#.ifdef 'SUPPORT_WAITSLOTS'
  791 + $waits[$socket]++;
797 792
798   - if($waits[$socket] > /* .call 'Pancake\Config::get' 'main.waitslotwaitlimit' */) {
799   - $requestObject->invalidRequest(new invalidHTTPRequestException('There was no worker available to serve your request. Please try again later.', 500));
800   - goto write;
801   - }
  793 + if($waits[$socket] > /* .call 'Pancake\Config::get' 'main.waitslotwaitlimit' */) {
  794 + $requestObject->invalidRequest(new invalidHTTPRequestException('There was no worker available to serve your request. Please try again later.', 500));
  795 + goto write;
  796 + }
802 797
803   - $waitSlotsOrig[$socket] = $socket;
  798 + $waitSlotsOrig[$socket] = $socket;
804 799
805   - goto clean;
806   - #.else
807   - $requestObject->invalidRequest(new invalidHTTPRequestException('There was no worker available to serve your request. Please try again later.', 500));
808   - goto write;
809   - #.endif
  800 + goto clean;
  801 +#.else
  802 + $requestObject->invalidRequest(new invalidHTTPRequestException('There was no worker available to serve your request. Please try again later.', 500));
  803 + goto write;
  804 +#.endif
810 805 }
811 806
812   - #.ifdef 'SUPPORT_WAITSLOTS'
  807 +#.ifdef 'SUPPORT_WAITSLOTS'
813 808 unset($waitSlotsOrig[$socket]);
814 809 unset($waits[$socket]);
815   - #.endif
  810 +#.endif
816 811
817 812 $data = serialize($requestObject);
818 813
@@ -840,35 +835,35 @@
840 835
841 836 goto clean;
842 837 }
843   - #.endif
  838 +#.endif
844 839
845 840 // Get time of last modification
846   - $modified = filemtime(/* .VHOST_DOCUMENT_ROOT */ . /* .REQUEST_FILE_PATH */);
  841 + $modified = filemtime(/* .VHOST_DOCUMENT_ROOT */ . $requestObject->requestFilePath);
847 842 // Set Last-Modified-Header as RFC 2822
848 843 $requestObject->setHeader('Last-Modified', date('r', $modified));
849 844
850 845 // Check for If-Modified-Since
851   - if(strtotime(/* .GET_REQUEST_HEADER '"if-modified-since"' */) == $modified) {
852   - /* .ANSWER_CODE */ = 304;
  846 + if(isset($requestObject->requestHeaders["if-modified-since"]) && strtotime($requestObject->requestHeaders["if-modified-since"]) == $modified) {
  847 + $requestObject->answerCode = 304;
853 848 goto write;
854 849 }
855 850
856   - #.ifdef 'SUPPORT_DIRECTORY_LISTINGS'
  851 +#.ifdef 'SUPPORT_DIRECTORY_LISTINGS'
857 852 // Check for directory
858   - if(is_dir(/* .VHOST_DOCUMENT_ROOT */ . /* .REQUEST_FILE_PATH */)) {
  853 + if(is_dir(/* .VHOST_DOCUMENT_ROOT */ . $requestObject->requestFilePath)) {
859 854 $files = array();
860 855
861   - foreach(scandir(/* .VHOST_DOCUMENT_ROOT */ . /* .REQUEST_FILE_PATH */) as $file) {
  856 + foreach(scandir(/* .VHOST_DOCUMENT_ROOT */ . $requestObject->requestFilePath) as $file) {
862 857 if($file == '.')
863 858 continue;
864   - $isDir = is_dir(/* .VHOST_DOCUMENT_ROOT */ . /* .REQUEST_FILE_PATH*/ . $file);
  859 + $isDir = is_dir(/* .VHOST_DOCUMENT_ROOT */ . $requestObject->requestFilePath . $file);
865 860 $files[] =
866 861 array('name' => $file,
867   - 'address' => 'http://' . /* .GET_REQUEST_HEADER '"host"' */ . /* .REQUEST_FILE_PATH*/ . $file . ($isDir ? '/' : ''),
  862 + 'address' => 'http://' . $requestObject->requestHeaders["host"] . $requestObject->requestFilePath . $file . ($isDir ? '/' : ''),
868 863 'directory' => $isDir,
869 864 'type' => MIME::typeOf($file),
870   - 'modified' => filemtime(/* .VHOST_DOCUMENT_ROOT */ . /* .REQUEST_FILE_PATH*/ . $file),
871   - 'size' => filesize(/* .VHOST_DOCUMENT_ROOT */ . /* .REQUEST_FILE_PATH*/ . $file));
  865 + 'modified' => filemtime(/* .VHOST_DOCUMENT_ROOT */ . $requestObject->requestFilePath . $file),
  866 + 'size' => filesize(/* .VHOST_DOCUMENT_ROOT */ . $requestObject->requestFilePath . $file));
872 867 }
873 868
874 869 $requestObject->setHeader('Content-Type', 'text/html; charset=utf-8');
@@ -878,27 +873,28 @@
878 873 if(!include(/* .VHOST_DIRECTORY_PAGE_HANDLER */))
879 874 include 'php/directoryPageHandler.php';
880 875
881   - /* .ANSWER_BODY */ = ob_get_clean();
  876 + $requestObject->answerBody = ob_get_clean();
882 877 unset($files);
883 878 } else {
884   - #.endif
885   - $requestObject->setHeader('Content-Type', /* .MIME_TYPE */);
  879 +#.endif
  880 + // We have a static file requested.
  881 + $requestObject->setHeader('Content-Type', $requestObject->mimeType);
886 882 $requestObject->setHeader('Accept-Ranges', 'bytes');
887 883
888   - #.ifdef 'SUPPORT_GZIP'
889   - // Check if GZIP-compression should be used
890   - if(/* .ACCEPTS_COMPRESSION "'gzip'" */ && /* .VHOST_ALLOW_GZIP_COMPRESSION */ === true && filesize(/* .VHOST_DOCUMENT_ROOT */ . /* .REQUEST_FILE_PATH */) >= /* .VHOST_GZIP_MINIMUM */
891   - #.ifdef 'SUPPORT_GZIP_MIME_TYPE_LIMIT'
892   - && /* .VHOST */->gzipMimeTypes && in_array(/* .MIME_TYPE */, /* .VHOST */->gzipMimeTypes)
893   - #.endif
  884 +#.ifdef 'SUPPORT_GZIP'
  885 + // Check if GZIP compression should be used
  886 + if(isset($requestObject->acceptedCompressions["gzip"]) && /* .VHOST_ALLOW_GZIP_COMPRESSION */ === true && filesize(/* .VHOST_DOCUMENT_ROOT */ . $requestObject->requestFilePath) >= /* .VHOST_GZIP_MINIMUM */
  887 +#.ifdef 'SUPPORT_GZIP_MIME_TYPE_LIMIT'
  888 + && /* .VHOST */->gzipMimeTypes && in_array($requestObject->mimeType, /* .VHOST */->gzipMimeTypes)
  889 +#.endif
894 890 ) {
895   - // Set encoding-header
  891 + // Set encoding header
896 892 $requestObject->setHeader('Content-Encoding', 'gzip');
897 893 // Create temporary file
898 894 $gzipPath[$socket] = tempnam(/* .call 'Pancake\Config::get' 'main.tmppath' */, 'GZIP');
899 895 $gzipFileHandle = gzopen($gzipPath[$socket], 'w' . /* .VHOST_GZIP_LEVEL */);
900 896 // Load uncompressed requested file
901   - $requestedFileHandle = fopen(/* .VHOST_DOCUMENT_ROOT */ . /* .REQUEST_FILE_PATH */, 'r');
  897 + $requestedFileHandle = fopen(/* .VHOST_DOCUMENT_ROOT */ . $requestObject->requestFilePath, 'r');
902 898 // Compress file
903 899 while(!feof($requestedFileHandle))
904 900 gzwrite($gzipFileHandle, fread($requestedFileHandle, /* .WRITE_LIMIT */));
@@ -906,56 +902,65 @@
906 902 gzclose($gzipFileHandle);
907 903 $requestFileHandle[$socket] = fopen($gzipPath[$socket], 'r');
908 904 // Set Content-Length
909   - $requestObject->setHeader('Content-Length', filesize($gzipPath[$socket]) - /* .RANGE_FROM */);
  905 + $requestObject->setHeader('Content-Length', filesize($gzipPath[$socket]) - $requestObject->rangeFrom);
910 906 } else {
911   - #.endif
912   - $requestObject->setHeader('Content-Length', filesize(/* .VHOST_DOCUMENT_ROOT */ . /* .REQUEST_FILE_PATH */) - /* .RANGE_FROM */);
913   - $requestFileHandle[$socket] = fopen(/* .VHOST_DOCUMENT_ROOT */ . /* .REQUEST_FILE_PATH */, 'r');
914   - #.ifdef 'SUPPORT_GZIP'
  907 +#.endif
  908 + // No GZIP
  909 + $requestObject->setHeader('Content-Length', filesize(/* .VHOST_DOCUMENT_ROOT */ . $requestObject->requestFilePath) - $requestObject->rangeFrom);
  910 + $requestFileHandle[$socket] = fopen(/* .VHOST_DOCUMENT_ROOT */ . $requestObject->requestFilePath, 'r');
  911 +#.ifdef 'SUPPORT_GZIP'
915 912 }
916   - #.endif
  913 +#.endif
917 914
918 915 // Check if a specific range was requested
919   - if(/* .RANGE_FROM */) {
920   - /* .ANSWER_CODE */ = 206;
921   - fseek($requestFileHandle[$socket], /* .RANGE_FROM */);
922   - #.ifdef 'SUPPORT_GZIP'
  916 + if($requestObject->rangeFrom) {
  917 + $requestObject->answerCode = 206;
  918 + fseek($requestFileHandle[$socket], $requestObject->rangeFrom);
  919 +#.ifdef 'SUPPORT_GZIP'
923 920 if($gzipPath[$socket])
924   - $requestObject->setHeader('Content-Range', 'bytes ' . /* .RANGE_FROM */.'-'.(filesize($gzipPath[$socket]) - 1).'/'.filesize($gzipPath[$socket]));
  921 + $requestObject->setHeader('Content-Range', 'bytes ' . $requestObject->rangeFrom . '-' . (filesize($gzipPath[$socket]) - 1) . '/' . filesize($gzipPath[$socket]));
925 922 else
926   - #.endif
927   - $requestObject->setHeader('Content-Range', 'bytes ' . /* .RANGE_FROM */.'-'.(filesize(/* .VHOST_DOCUMENT_ROOT */ . /* .REQUEST_FILE_PATH */) - 1).'/'.filesize(/* .VHOST_DOCUMENT_ROOT */ . /* .REQUEST_FILE_PATH */));
  923 +#.endif
  924 + $requestObject->setHeader('Content-Range', 'bytes ' . $requestObject->rangeFrom . '-' . (filesize(/* .VHOST_DOCUMENT_ROOT */ . $requestObject->requestFilePath) - 1).'/'.filesize(/* .VHOST_DOCUMENT_ROOT */ . $requestObject->requestFilePath));
928 925 }
929   - #.ifdef 'SUPPORT_DIRECTORY_LISTINGS'
  926 +#.ifdef 'SUPPORT_DIRECTORY_LISTINGS'
930 927 }
931   - #.endif
  928 +#.endif
932 929
933 930 write:
934 931
935   - // Get Answer Headers
936   - $writeBuffer[$socket] = /* .BUILD_ANSWER_HEADERS */;
  932 + // Build answer headers and answer body as string
  933 + $writeBuffer[$socket] = $requestObject->buildAnswerHeaders();
937 934
938   - #.if #.Pancake\Config::get("main.prebuffer", 0)
  935 +#.if #.Pancake\Config::get("main.prebuffer", 0)
  936 + // Add some data to the buffer to save a write cycle
939 937 if(isset($requestFileHandle[$socket])) {
940 938 $writeBuffer[$socket] .= fread($requestFileHandle[$socket], /* .number #.Pancake\Config::get("main.prebuffer", 0)*/);
941 939 }
942   - #.endif
  940 +#.endif
943 941
944 942 // Output request information
945   - 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"' */
946   - #.ifdef 'SUPPORT_TLS'
  943 + out('REQ '
  944 + . $requestObject->answerCode . ' '
  945 + . $requestObject->remoteIP . ': '
  946 + . $requestObject->requestLine . ' on vHost '
  947 + . /* .VHOST_NAME */
  948 + . ' (via ' . $requestObject->requestHeaders["host"]
  949 + . ' from ' . (isset($requestObject->requestHeaders["referer"]) ? $requestObject->requestHeaders["referer"] : "") . ') - '
  950 + . (isset($requestObject->requestHeaders["user-agent"]) ? $requestObject->requestHeaders["user-agent"] : "")
  951 +#.ifdef 'SUPPORT_TLS'
947 952 . (isset($TLSConnections[$socket]) ? " - " . TLSCipherName($socket) : "")
948   - #.endif
  953 +#.endif
949 954 , OUTPUT_REQUEST | OUTPUT_LOG);
950 955
951 956 // Check if user wants keep-alive connection
952 957 if($requestObject->answerHeaders["connection"] == 'keep-alive')
953 958 KeepAlive($socket, true);
954 959
955   - #.if 0 < #.call 'Pancake\Config::get' 'main.requestworkerlimit'
  960 +#.if 0 < #.call 'Pancake\Config::get' 'main.requestworkerlimit'
956 961 // Increment amount of processed requests
957 962 $processedRequests++;
958   - #.endif
  963 +#.endif
959 964
960 965 // Clean some data now to improve RAM usage
961 966 unset($postData[$socket]);
@@ -965,31 +970,33 @@
965 970
966 971 // 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
967 972 if(!strlen($writeBuffer[$socket])
968   - #.ifdef 'SUPPORT_TLS'
  973 +#.ifdef 'SUPPORT_TLS'
969 974 && isset($requestFileHandle[$socket])
970   - #.endif
  975 +#.endif
971 976 )
972 977 $writeBuffer[$socket] = fread($requestFileHandle[$socket], /* .call 'Pancake\Config::get' 'main.writebuffermin' */);
973 978
974   - #.ifdef 'SUPPORT_TLS'
  979 +#.ifdef 'SUPPORT_TLS'
975 980 if(isset($TLSConnections[$socket])) {
976 981 $TLSConnections[$socket] = /* .TLS_WRITE */;
977 982 if(($writtenLength = TLSWrite($socket, $writeBuffer[$socket])) === false)
978 983 goto close;
979 984
980 985 if($writtenLength == -1) {
  986 + // OpenSSL wants read (possible renegotiation)
981 987 $listenSocketsOrig[$socket] = $socket;
982 988 goto clean;
983 989 }
984 990
985 991 if($writtenLength == 0) {
  992 + // OpenSSL wants write
986 993 $liveWriteSocketsOrig[$socket] = $socket;
987 994 goto clean;
988 995 }
989 996
990 997 $writeBuffer[$socket] = substr($writeBuffer[$socket], $writtenLength);
991 998 } else
992   - #.endif
  999 +#.endif
993 1000
994 1001 // Write data to socket
995 1002 if(@WriteBuffer($socket, $writeBuffer[$socket]) === false)
@@ -999,26 +1006,26 @@
999 1006 if(strlen($writeBuffer[$socket]) < #.call 'Pancake\Config::get' 'main.writebuffermin'
1000 1007 && isset($requestFileHandle[$socket])
1001 1008 && !feof($requestFileHandle[$socket])
1002   - #.if #.call 'Pancake\Config::get' 'main.writebufferhardmaxconcurrent'
  1009 +#.if #.call 'Pancake\Config::get' 'main.writebufferhardmaxconcurrent'
1003 1010 && count($writeBuffer) < #.call 'Pancake\Config::get' 'main.writebufferhardmaxconcurrent'
1004   - #.endif
1005   - #.if #.call 'Pancake\Config::get' 'main.allowhead'
1006   - && /* .REQUEST_TYPE */ != 'HEAD'
1007   - #.endif
  1011 +#.endif
  1012 +#.if #.call 'Pancake\Config::get' 'main.allowhead'
  1013 + && $requestObject->requestType != 'HEAD'
  1014 +#.endif
1008 1015 )
1009 1016 $writeBuffer[$socket] .= fread($requestFileHandle[$socket],
1010   - #.if #.call 'Pancake\Config::get' 'main.writebuffersoftmaxconcurrent'
  1017 +#.if #.call 'Pancake\Config::get' 'main.writebuffersoftmaxconcurrent'
1011 1018 (count($writeBuffer) > /* .call 'Pancake\Config::get' 'main.writebuffersoftmaxconcurrent' */ ? /* .call 'Pancake\Config::get' 'main.writebuffermin' */ : /* .WRITE_LIMIT */)
1012   - #.else
  1019 +#.else
1013 1020 #.WRITE_LIMIT
1014   - #.endif
  1021 +#.endif
1015 1022 - strlen($writeBuffer[$socket]));
1016 1023
1017   - // Check if more data is available
  1024 + // Check if more data is available and we should store the socket for another write cycle
1018 1025 if(strlen($writeBuffer[$socket]) || (isset($requestFileHandle[$socket]) && !feof($requestFileHandle[$socket])
1019   - #.if #.call 'Pancake\Config::get' 'main.allowhead'
1020   - && /* .REQUEST_TYPE */ != 'HEAD'
1021   - #.endif
  1026 +#.if #.call 'Pancake\Config::get' 'main.allowhead'
  1027 + && $requestObject->requestType != 'HEAD'
  1028 +#.endif
1022 1029 )) {
1023 1030 // Event-based writing - In the time the client is still downloading we can process other requests
1024 1031 $liveWriteSocketsOrig[$socket] = $socket;
@@ -1029,12 +1036,13 @@
1029 1036
1030 1037 // Close socket
1031 1038 if(!isset($requestObject) || $requestObject->answerHeaders["connection"] != 'keep-alive') {
1032   - #.ifdef 'SUPPORT_TLS'
  1039 +#.ifdef 'SUPPORT_TLS'
1033 1040 if(isset($TLSConnections[$socket])) {
  1041 + // Do TLS protocol shutdown
1034 1042 unset($TLSConnections[$socket]);
1035 1043 TLSShutdown($socket);
1036 1044 }
1037   - #.endif
  1045 +#.endif
1038 1046 //@socket_shutdown($requestSocket);
1039 1047 Close($socket);
1040 1048
@@ -1043,28 +1051,29 @@
1043 1051 $listenSocketsOrig[$socket] = $socket;
1044 1052 #.ifdef 'SUPPORT_TLS'
1045 1053 if(isset($TLSConnections[$socket])) {
  1054 + // Reset TLS connection status from WRITE to READ
1046 1055 $TLSConnections[$socket] = /* .TLS_READ */;
1047 1056 }
1048 1057 #.endif
1049 1058 }
1050 1059
1051 1060
1052   - #.ifdef 'SUPPORT_WAITSLOTS'
  1061 +#.ifdef 'SUPPORT_WAITSLOTS'
1053 1062 unset($waitSlotsOrig[$socket]);
1054 1063 unset($waits[$socket]);
1055   - #.endif
  1064 +#.endif
1056 1065
1057 1066 unset($socketData[$socket]);
1058 1067 unset($postData[$socket]);
1059 1068 unset($liveReadSockets[$socket]);
1060 1069 unset($requests[$socket]);
1061 1070 unset($writeBuffer[$socket]);
1062   - #.ifdef 'SUPPORT_GZIP'
  1071 +#.ifdef 'SUPPORT_GZIP'
1063 1072 if(isset($gzipPath[$socket])) {
1064 1073 unlink($gzipPath[$socket]);
1065 1074 unset($gzipPath[$socket]);
1066 1075 }
1067   - #.endif
  1076 +#.endif
1068 1077
1069 1078 unset($liveWriteSocketsOrig[$socket]);
1070 1079
@@ -1073,7 +1082,8 @@
1073 1082 unset($requestFileHandle[$socket]);
1074 1083 }
1075 1084
1076   - #.if Pancake\DEBUG_MODE === true
  1085 +#.if Pancake\DEBUG_MODE === true
  1086 + // Pancake profiler
1077 1087 if($results = benchmarkFunction(null, true)) {
1078 1088 foreach($results as $function => $functionResults) {
1079 1089 foreach($functionResults as $result)
@@ -1087,19 +1097,20 @@
1087 1097 unset($functionResults);
1088 1098 unset($results);
1089 1099 }
1090   - #.endif
  1100 +#.endif