webserver.php
author Dan
Sun, 04 Jan 2009 22:56:57 -0500
changeset 67 c4aefad02ce4
parent 64 ee64bb096f56
child 68 32f6e2ee15ab
permissions -rw-r--r--
WebServer now does not attempt to destruct if threader is not up
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
     1
<?php
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
     2
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
     3
/**
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
     4
 * Webserver class
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
     5
 * 
6
5f35ebc4f9bb First release version. Renamed to Greyhound and readme/license files added.
Dan
parents: 5
diff changeset
     6
 * Greyhound - real web management for Amarok
5
9b96265b5918 Relicensed to GPLv2. Previous revisions should not be downloaded as they do not contain copies of appropriate licenses, which will be added in a later commit. Completed interface for mobile devices.
Dan
parents: 0
diff changeset
     7
 * Copyright (C) 2008 Dan Fuhry
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
     8
 *
5
9b96265b5918 Relicensed to GPLv2. Previous revisions should not be downloaded as they do not contain copies of appropriate licenses, which will be added in a later commit. Completed interface for mobile devices.
Dan
parents: 0
diff changeset
     9
 * This program is Free Software; you can redistribute and/or modify it under the terms of the GNU General Public License
9b96265b5918 Relicensed to GPLv2. Previous revisions should not be downloaded as they do not contain copies of appropriate licenses, which will be added in a later commit. Completed interface for mobile devices.
Dan
parents: 0
diff changeset
    10
 * as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
9b96265b5918 Relicensed to GPLv2. Previous revisions should not be downloaded as they do not contain copies of appropriate licenses, which will be added in a later commit. Completed interface for mobile devices.
Dan
parents: 0
diff changeset
    11
 *
9b96265b5918 Relicensed to GPLv2. Previous revisions should not be downloaded as they do not contain copies of appropriate licenses, which will be added in a later commit. Completed interface for mobile devices.
Dan
parents: 0
diff changeset
    12
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
9b96265b5918 Relicensed to GPLv2. Previous revisions should not be downloaded as they do not contain copies of appropriate licenses, which will be added in a later commit. Completed interface for mobile devices.
Dan
parents: 0
diff changeset
    13
 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for details.
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    14
 */
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    15
48
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
    16
require('multithreading.php');
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
    17
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    18
/**
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    19
 * Version of the server
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    20
 * @const string
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    21
 */
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    22
43
2634d550a97b Added full cookie support to webserver
Dan
parents: 40
diff changeset
    23
define('HTTPD_VERSION', '0.1b5');
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    24
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    25
/**
26
300d374d89b0 Made error pages send length of response to get keep-alive working properly; added (nonworking, sorry) keep-alive timeout support.
Dan
parents: 24
diff changeset
    26
 * Length of keep-alive connections
300d374d89b0 Made error pages send length of response to get keep-alive working properly; added (nonworking, sorry) keep-alive timeout support.
Dan
parents: 24
diff changeset
    27
 * @const int
300d374d89b0 Made error pages send length of response to get keep-alive working properly; added (nonworking, sorry) keep-alive timeout support.
Dan
parents: 24
diff changeset
    28
 */
300d374d89b0 Made error pages send length of response to get keep-alive working properly; added (nonworking, sorry) keep-alive timeout support.
Dan
parents: 24
diff changeset
    29
300d374d89b0 Made error pages send length of response to get keep-alive working properly; added (nonworking, sorry) keep-alive timeout support.
Dan
parents: 24
diff changeset
    30
define('HTTPD_KEEP_ALIVE_TIMEOUT', 300);
300d374d89b0 Made error pages send length of response to get keep-alive working properly; added (nonworking, sorry) keep-alive timeout support.
Dan
parents: 24
diff changeset
    31
300d374d89b0 Made error pages send length of response to get keep-alive working properly; added (nonworking, sorry) keep-alive timeout support.
Dan
parents: 24
diff changeset
    32
/**
21
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
    33
 * Webserver system icons
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
    34
 */
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
    35
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
    36
define('HTTPD_ICON_SCRIPT', 'iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAQAAAC1+jfqAAAABGdBTUEAAK/INwWK6QAAABl0RVh0U29mdHdhcmUAQWRvYmUgSW1hZ2VSZWFkeXHJZTwAAAGSSURBVCjPVVFNSwJhEF78Ad79Cf6PvXQRsotUlzKICosuRYmR2RJR0KE6lBFFZVEbpFBSqKu2rum6llFS9HHI4iUhT153n6ZtIWMOM+/MM88z7wwH7s9Ub16SJcnbmrNcxVm2q7Z8/QPvEOtntpj92NkCqITLepEpjix7xQtiLOoQ2b6+E7YAN/5nfOEJ2WbKqOIOJ4bYVMEQx4LfBBQDsvFMhUcCVU1/CxVXmDBGA5ZETrhDCQVcYAPbyEJBhvrnBVPiSpNr6cYDNCQwo4zzU/ySckkgDYuNuVpI42T9k4gLKGMPs/xPzzovQiY2hQYe0jlJfyNNhTqiWDYBq/wBMcSRpnyPzu1oS7WtxjVBSthU1vgVksiQ3Dn6Gp5ah2YOKQo5GiuHPA6xT1EKpxQNCNYejgIR457KKio0S56YckjSa9jo//3mrj+BV0QQagqGTOo+Y7gZIf1puP3WHoLhEb2PjTlCTCWGXtbp8DCX3hZuOdaIc9A+aQvWk4ihq95p67a7nP+u+Ws+r0dql9z/zv0NCYhdCPKZ7oYAAAAASUVORK5CYII=');
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
    37
define('HTTPD_ICON_FOLDER', 'iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAABGdBTUEAAK/INwWK6QAAABl0RVh0U29mdHdhcmUAQWRvYmUgSW1hZ2VSZWFkeXHJZTwAAAGrSURBVDjLxZO7ihRBFIa/6u0ZW7GHBUV0UQQTZzd3QdhMQxOfwMRXEANBMNQX0MzAzFAwEzHwARbNFDdwEd31Mj3X7a6uOr9BtzNjYjKBJ6nicP7v3KqcJFaxhBVtZUAK8OHlld2st7Xl3DJPVONP+zEUV4HqL5UDYHr5xvuQAjgl/Qs7TzvOOVAjxjlC+ePSwe6DfbVegLVuT4r14eTr6zvA8xSAoBLzx6pvj4l+DZIezuVkG9fY2H7YRQIMZIBwycmzH1/s3F8AapfIPNF3kQk7+kw9PWBy+IZOdg5Ug3mkAATy/t0usovzGeCUWTjCz0B+Sj0ekfdvkZ3abBv+U4GaCtJ1iEm6ANQJ6fEzrG/engcKw/wXQvEKxSEKQxRGKE7Izt+DSiwBJMUSm71rguMYhQKrBygOIRStf4TiFFRBvbRGKiQLWP29yRSHKBTtfdBmHs0BUpgvtgF4yRFR+NUKi0XZcYjCeCG2smkzLAHkbRBmP0/Uk26O5YnUActBp1GsAI+S5nRJJJal5K1aAMrq0d6Tm9uI6zjyf75dAe6tx/SsWeD//o2/Ab6IH3/h25pOAAAAAElFTkSuQmCC');
29
8210fdc7c317 Switched default theme to grey.
Dan
parents: 26
diff changeset
    38
define('HTTPD_ICON_FILE',   'iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAABGdBTUEAAK/INwWK6QAAABl0RVh0U29mdHdhcmUAQWRvYmUgSW1hZ2VSZWFkeXHJZTwAAAINSURBVBgZBcG/r55zGAfg6/4+z3va01NHlYgzEfE7MdCIGISFgS4Gk8ViYyM2Mdlsko4GSf8Do0FLRCIkghhYJA3aVBtEz3nP89wf11VJvPDepdd390+8Nso5nESBQoq0pfvXm9fzWf19453LF85vASqJlz748vInb517dIw6EyYBIIG49u+xi9/c9MdvR//99MPPZ7+4cP4IZhhTPbwzT2d+vGoaVRRp1rRliVvHq+cfvM3TD82+7mun0o/ceO7NT+/4/KOXjwZU1ekk0840bAZzMQ2mooqh0A72d5x/6sB9D5zYnff3PoYBoWBgFKPKqDKqjCpjKr//dcu9p489dra88cydps30KswACfNEKanSaxhlntjJ8Mv12Paie+vZ+0+oeSwwQ0Iw1xAR1CiFNJkGO4wu3ZMY1AAzBI0qSgmCNJsJUEOtJSMaCTBDLyQ0CknAGOgyTyFFiLI2awMzdEcSQgSAAKVUmAeNkxvWJWCGtVlDmgYQ0GFtgg4pNtOwbBcwQy/Rife/2yrRRVI0qYCEBly8Z+P4qMEMy7JaVw72N568e+iwhrXoECQkfH91kY7jwwXMsBx1L93ZruqrK6uuiAIdSnTIKKPLPFcvay8ww/Hh+ufeznTXu49v95IMoQG3784gYXdTqvRmqn/Wpa/ADFX58MW3L71SVU9ETgEIQQQIOOzub+fhIvwPRDgeVjWDahIAAAAASUVORK5CYII=');
21
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
    39
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
    40
/**
37
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
    41
 * Abstraction layer to use
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
    42
 */
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
    43
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
    44
define('HTTPD_SOCKET_LAYER', 'Stream');
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
    45
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
    46
/**
24
d275dc8f4203 Added HttpSuccess exception which can be thrown when a scriptlet is done executing normally
Dan
parents: 23
diff changeset
    47
 * Simple but full-featured embedded web server written in PHP.
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    48
 * @package Amarok
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    49
 * @subpackage WebControl
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    50
 * @author Dan Fuhry
23
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
    51
 * @license GNU General Public License <http://www.gnu.org/licenses/old-licenses/gpl-2.0.html>
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    52
 */
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    53
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    54
class WebServer
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    55
{
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    56
  
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    57
  /**
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    58
   * IP address we're bound to
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    59
   * @var string
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    60
   */
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    61
  
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    62
  var $bind_address = '127.0.0.1';
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    63
  
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    64
  /**
48
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
    65
   * Port we're listening on
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
    66
   * @var int
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
    67
   */
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
    68
   
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
    69
  var $port = 8080;
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
    70
  
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
    71
  /**
37
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
    72
   * Socket abstraction object
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
    73
   * @var object
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    74
   */
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    75
  
37
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
    76
  var $server = null;
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    77
  
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    78
  /**
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    79
   * Server string
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    80
   * @var string
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    81
   */
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    82
  
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    83
  var $server_string = 'PhpHttpd';
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    84
  
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    85
  /**
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    86
   * Default document (well default handler)
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    87
   * @var string
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    88
   */
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    89
  
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    90
  var $default_document = false;
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    91
  
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    92
  /**
21
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
    93
   * List of filenames or handlers used when a directory listing is requested
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
    94
   * @var array
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
    95
   */
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
    96
  
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
    97
  var $directory_index = array('index.html', 'index.htm', 'index', 'default.html', 'default.htm');
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
    98
  
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
    99
  /**
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   100
   * HTTP response code set by the handler function
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   101
   * @var int
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   102
   */
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   103
  
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   104
  var $response_code = 0;
40
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 38
diff changeset
   105
    
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   106
  /**
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   107
   * Content type set by the current handler function
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   108
   * @var string
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   109
   */
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   110
  
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   111
  var $content_type = '';
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   112
  
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   113
  /**
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   114
   * Response headers to send back to the client
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   115
   * @var array
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   116
   */
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   117
  
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   118
  var $response_headers = array();
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   119
  
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   120
  /**
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   121
   * List of handlers
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   122
   * @var array
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   123
   */
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   124
   
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   125
  var $handlers = array();
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   126
  
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   127
  /**
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   128
   * Switch to control if directory listing is enabled
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   129
   * @var bool
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   130
   */
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   131
  
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   132
  var $allow_dir_list = false;
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   133
  
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   134
  /**
15
2adca0f363fd Added multi-threading/forking/keep-alive support to webserver. w00t, feeling all POSIX-happy today!
Dan
parents: 13
diff changeset
   135
   * Switch to control forking support.
2adca0f363fd Added multi-threading/forking/keep-alive support to webserver. w00t, feeling all POSIX-happy today!
Dan
parents: 13
diff changeset
   136
   * @var bool
2adca0f363fd Added multi-threading/forking/keep-alive support to webserver. w00t, feeling all POSIX-happy today!
Dan
parents: 13
diff changeset
   137
   */
2adca0f363fd Added multi-threading/forking/keep-alive support to webserver. w00t, feeling all POSIX-happy today!
Dan
parents: 13
diff changeset
   138
  
2adca0f363fd Added multi-threading/forking/keep-alive support to webserver. w00t, feeling all POSIX-happy today!
Dan
parents: 13
diff changeset
   139
  var $allow_fork = true;
2adca0f363fd Added multi-threading/forking/keep-alive support to webserver. w00t, feeling all POSIX-happy today!
Dan
parents: 13
diff changeset
   140
  
2adca0f363fd Added multi-threading/forking/keep-alive support to webserver. w00t, feeling all POSIX-happy today!
Dan
parents: 13
diff changeset
   141
  /**
48
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   142
   * Multi-threading manager.
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   143
   * @var object
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   144
   */
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   145
  
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   146
  var $threader = false;
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   147
  
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   148
  /**
15
2adca0f363fd Added multi-threading/forking/keep-alive support to webserver. w00t, feeling all POSIX-happy today!
Dan
parents: 13
diff changeset
   149
   * Keep-alive support uses this to track what the client requested.
2adca0f363fd Added multi-threading/forking/keep-alive support to webserver. w00t, feeling all POSIX-happy today!
Dan
parents: 13
diff changeset
   150
   * Only used if $allow_fork is set to true.
2adca0f363fd Added multi-threading/forking/keep-alive support to webserver. w00t, feeling all POSIX-happy today!
Dan
parents: 13
diff changeset
   151
   * @var bool
2adca0f363fd Added multi-threading/forking/keep-alive support to webserver. w00t, feeling all POSIX-happy today!
Dan
parents: 13
diff changeset
   152
   */
2adca0f363fd Added multi-threading/forking/keep-alive support to webserver. w00t, feeling all POSIX-happy today!
Dan
parents: 13
diff changeset
   153
  
2adca0f363fd Added multi-threading/forking/keep-alive support to webserver. w00t, feeling all POSIX-happy today!
Dan
parents: 13
diff changeset
   154
  var $in_keepalive = false;
2adca0f363fd Added multi-threading/forking/keep-alive support to webserver. w00t, feeling all POSIX-happy today!
Dan
parents: 13
diff changeset
   155
  
2adca0f363fd Added multi-threading/forking/keep-alive support to webserver. w00t, feeling all POSIX-happy today!
Dan
parents: 13
diff changeset
   156
  /**
21
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   157
   * UUID for this server instance
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   158
   * @var string
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   159
   */
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   160
  
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   161
  var $uuid = '00000000-0000-0000-0000-000000000000';
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   162
  
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   163
  /**
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   164
   * Switch to track whether a scriptlet is running. If it is, send_http_error() does more than normal.
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   165
   * @var bool
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   166
   */
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   167
  
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   168
  var $in_scriptlet = false;
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   169
  
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   170
  /**
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   171
   * Switch to track whether headers have been sent or not.
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   172
   * @var bool
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   173
   */
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   174
  
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   175
  var $headers_sent = false;
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   176
  
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   177
  /**
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   178
   * Switch to track if the socket is bound and thus needs to be freed or not
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   179
   * @var bool
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   180
   */
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   181
  
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   182
  var $socket_initted = false;
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   183
  
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   184
  /**
32
86140ed9087d Experimental: upon change to playlist, send SIGUSR1 to parent process and branch out to all children to force a playlist refresh
Dan
parents: 26
diff changeset
   185
   * The list of child processes spawned by this server.
86140ed9087d Experimental: upon change to playlist, send SIGUSR1 to parent process and branch out to all children to force a playlist refresh
Dan
parents: 26
diff changeset
   186
   * @var array
86140ed9087d Experimental: upon change to playlist, send SIGUSR1 to parent process and branch out to all children to force a playlist refresh
Dan
parents: 26
diff changeset
   187
   */
86140ed9087d Experimental: upon change to playlist, send SIGUSR1 to parent process and branch out to all children to force a playlist refresh
Dan
parents: 26
diff changeset
   188
  
86140ed9087d Experimental: upon change to playlist, send SIGUSR1 to parent process and branch out to all children to force a playlist refresh
Dan
parents: 26
diff changeset
   189
  var $child_list = array();
86140ed9087d Experimental: upon change to playlist, send SIGUSR1 to parent process and branch out to all children to force a playlist refresh
Dan
parents: 26
diff changeset
   190
  
86140ed9087d Experimental: upon change to playlist, send SIGUSR1 to parent process and branch out to all children to force a playlist refresh
Dan
parents: 26
diff changeset
   191
  /**
86140ed9087d Experimental: upon change to playlist, send SIGUSR1 to parent process and branch out to all children to force a playlist refresh
Dan
parents: 26
diff changeset
   192
   * The parent process's PID
86140ed9087d Experimental: upon change to playlist, send SIGUSR1 to parent process and branch out to all children to force a playlist refresh
Dan
parents: 26
diff changeset
   193
   * @var int
86140ed9087d Experimental: upon change to playlist, send SIGUSR1 to parent process and branch out to all children to force a playlist refresh
Dan
parents: 26
diff changeset
   194
   */
86140ed9087d Experimental: upon change to playlist, send SIGUSR1 to parent process and branch out to all children to force a playlist refresh
Dan
parents: 26
diff changeset
   195
  
86140ed9087d Experimental: upon change to playlist, send SIGUSR1 to parent process and branch out to all children to force a playlist refresh
Dan
parents: 26
diff changeset
   196
  var $parent_pid = 0;
86140ed9087d Experimental: upon change to playlist, send SIGUSR1 to parent process and branch out to all children to force a playlist refresh
Dan
parents: 26
diff changeset
   197
  
86140ed9087d Experimental: upon change to playlist, send SIGUSR1 to parent process and branch out to all children to force a playlist refresh
Dan
parents: 26
diff changeset
   198
  /**
48
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   199
   * List of IPC request handlers
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   200
   * @var array
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   201
   * @access private
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   202
   */
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   203
  
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   204
  var $ipc_handlers = array();
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   205
  
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   206
  /**
37
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
   207
   * Sockets for parent and child to communicate
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
   208
   * @var resource
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
   209
   * @var resource
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
   210
   */
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
   211
  
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
   212
  var $parent_sock = null;
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
   213
  var $child_sock = null;
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
   214
  
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
   215
  /**
48
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   216
   * Switched on when a graceful reboot event is sent.
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   217
   * @var bool
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   218
   */
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   219
  
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   220
  var $reboot_sent = false;
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   221
  
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   222
  /**
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   223
   * Constructor.
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   224
   * @param string IPv4 address to bind to
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   225
   * @param int Port number
21
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   226
   * @param int If port is under 1024, specify a user ID/name to switch to here
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   227
   * @param int If port is under 1024, specify a group ID/name to switch to here
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   228
   */
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   229
  
21
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   230
  function __construct($address = '127.0.0.1', $port = 8080, $targetuser = null, $targetgroup = null)
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   231
  {
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   232
    @set_time_limit(0);
23
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
   233
    @ini_set('memory_limit', '128M');
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   234
    
21
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   235
    // make sure we're not running as root
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   236
    // note that if allow_root is true, you must specify a UID/GID (or user/group) to switch to once the socket is bound
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   237
    $allow_root = ( $port < 1024 ) ? true : false;
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   238
    if ( function_exists('posix_geteuid') )
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   239
    {
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   240
      $euid = posix_geteuid();
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   241
      $egid = posix_getegid();
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   242
      $username = posix_getpwuid($euid);
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   243
      $username = $username['name'];
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   244
      $group = posix_getgrgid($egid);
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   245
      $group = $group['name'];
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   246
      if ( $euid == 0 && !$allow_root )
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   247
      {
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   248
        // running as root but not on a privileged port - die for security
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   249
        burnout("Running as superuser (user \"$username\" and group \"$group\"). This is not allowed for security reasons.");
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   250
      }
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   251
      else if ( $euid == 0 && $allow_root )
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   252
      {
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   253
        // running as root and port below 1024, so notify of the switch and verify that a target UID and GID were passed
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   254
        if ( $targetuser === null || $targetgroup === null )
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   255
        {
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   256
          // no target user/group specified
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   257
          burnout("Must specify a target user and group when running server as root");
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   258
        }
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   259
        // get info about target user/group
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   260
        if ( is_string($targetuser) )
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   261
        {
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   262
          $targetuser = posix_getpwnam($targetuser);
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   263
          $targetuser = $targetuser['uid'];
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   264
        }
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   265
        if ( is_string($targetgroup) )
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   266
        {
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   267
          $targetgroup = posix_getgrnam($targetgroup);
64
ee64bb096f56 A few miscellaneous fixes including modifying WebServer to write data in chunks (improved performance and reliability on a slow connection)
Dan
parents: 63
diff changeset
   268
          $targetgroup = $targetgroup['gid'];
21
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   269
        }
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   270
        // make sure all info is valid
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   271
        if ( !is_int($targetuser) || !is_int($targetgroup) )
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   272
        {
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   273
          burnout('Invalid user or group specified');
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   274
        }
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   275
        $userinfo = posix_getpwuid($targetuser);
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   276
        $groupinfo = posix_getgrgid($targetgroup);
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   277
        if ( function_exists('status') )
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   278
          status("Will switch to user \"{$userinfo['name']}\" and group \"{$groupinfo['name']}\" shortly after binding to socket");
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   279
      }
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   280
      else if ( $allow_root && $euid > 0 )
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   281
      {
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   282
        burnout("Must be superuser to bind to ports below 1024");
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   283
      }
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   284
    }
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   285
    $socket_do_root = ( $allow_root ) ? function_exists('posix_geteuid') : false;
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   286
    
37
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
   287
    $class = 'Socket_' . HTTPD_SOCKET_LAYER;
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
   288
    $this->server = new $class();
57
31ce64a3ff6c Some backend changes to webserver to allow IPv6 binding
Dan
parents: 56
diff changeset
   289
    if ( is_array($address) )
31ce64a3ff6c Some backend changes to webserver to allow IPv6 binding
Dan
parents: 56
diff changeset
   290
    {
31ce64a3ff6c Some backend changes to webserver to allow IPv6 binding
Dan
parents: 56
diff changeset
   291
      foreach ( $address as $a )
31ce64a3ff6c Some backend changes to webserver to allow IPv6 binding
Dan
parents: 56
diff changeset
   292
      {
31ce64a3ff6c Some backend changes to webserver to allow IPv6 binding
Dan
parents: 56
diff changeset
   293
        if ( is_array($port) )
31ce64a3ff6c Some backend changes to webserver to allow IPv6 binding
Dan
parents: 56
diff changeset
   294
        {
31ce64a3ff6c Some backend changes to webserver to allow IPv6 binding
Dan
parents: 56
diff changeset
   295
          foreach ( $port as $p )
31ce64a3ff6c Some backend changes to webserver to allow IPv6 binding
Dan
parents: 56
diff changeset
   296
          {
31ce64a3ff6c Some backend changes to webserver to allow IPv6 binding
Dan
parents: 56
diff changeset
   297
            $this->server->tcp_listen($a, $p);
31ce64a3ff6c Some backend changes to webserver to allow IPv6 binding
Dan
parents: 56
diff changeset
   298
          }
31ce64a3ff6c Some backend changes to webserver to allow IPv6 binding
Dan
parents: 56
diff changeset
   299
        }
31ce64a3ff6c Some backend changes to webserver to allow IPv6 binding
Dan
parents: 56
diff changeset
   300
        else
31ce64a3ff6c Some backend changes to webserver to allow IPv6 binding
Dan
parents: 56
diff changeset
   301
        {
31ce64a3ff6c Some backend changes to webserver to allow IPv6 binding
Dan
parents: 56
diff changeset
   302
          $this->server->tcp_listen($a, $port);
31ce64a3ff6c Some backend changes to webserver to allow IPv6 binding
Dan
parents: 56
diff changeset
   303
        }
31ce64a3ff6c Some backend changes to webserver to allow IPv6 binding
Dan
parents: 56
diff changeset
   304
      }
31ce64a3ff6c Some backend changes to webserver to allow IPv6 binding
Dan
parents: 56
diff changeset
   305
    }
31ce64a3ff6c Some backend changes to webserver to allow IPv6 binding
Dan
parents: 56
diff changeset
   306
    else
31ce64a3ff6c Some backend changes to webserver to allow IPv6 binding
Dan
parents: 56
diff changeset
   307
    {
31ce64a3ff6c Some backend changes to webserver to allow IPv6 binding
Dan
parents: 56
diff changeset
   308
      if ( is_array($port) )
31ce64a3ff6c Some backend changes to webserver to allow IPv6 binding
Dan
parents: 56
diff changeset
   309
      {
31ce64a3ff6c Some backend changes to webserver to allow IPv6 binding
Dan
parents: 56
diff changeset
   310
        foreach ( $port as $p )
31ce64a3ff6c Some backend changes to webserver to allow IPv6 binding
Dan
parents: 56
diff changeset
   311
        {
31ce64a3ff6c Some backend changes to webserver to allow IPv6 binding
Dan
parents: 56
diff changeset
   312
          $this->server->tcp_listen($address, $p);
31ce64a3ff6c Some backend changes to webserver to allow IPv6 binding
Dan
parents: 56
diff changeset
   313
        }
31ce64a3ff6c Some backend changes to webserver to allow IPv6 binding
Dan
parents: 56
diff changeset
   314
      }
31ce64a3ff6c Some backend changes to webserver to allow IPv6 binding
Dan
parents: 56
diff changeset
   315
      else
31ce64a3ff6c Some backend changes to webserver to allow IPv6 binding
Dan
parents: 56
diff changeset
   316
      {
31ce64a3ff6c Some backend changes to webserver to allow IPv6 binding
Dan
parents: 56
diff changeset
   317
        $this->server->tcp_listen($address, $port);
31ce64a3ff6c Some backend changes to webserver to allow IPv6 binding
Dan
parents: 56
diff changeset
   318
      }
31ce64a3ff6c Some backend changes to webserver to allow IPv6 binding
Dan
parents: 56
diff changeset
   319
    }
21
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   320
    
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   321
    // if running as root and we made it here, switch credentials
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   322
    if ( $socket_do_root )
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   323
    {
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   324
      posix_setuid($targetuser);
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   325
      posix_setgid($targetgroup);
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   326
      posix_setegid($targetgroup);
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   327
      posix_seteuid($targetuser);
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   328
      if ( function_exists('status') )
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   329
        status('Successfully switched user ID');
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   330
    }
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   331
    
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   332
    $this->bind_address = $address;
48
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   333
    $this->port = $port;
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   334
    $this->server_string = "PhpHttpd/" . HTTPD_VERSION . " PHP/" . PHP_VERSION . "\r\n";
32
86140ed9087d Experimental: upon change to playlist, send SIGUSR1 to parent process and branch out to all children to force a playlist refresh
Dan
parents: 26
diff changeset
   335
    $this->parent_pid = getmypid();
48
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   336
    $this->threader = new Threader();
21
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   337
    
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   338
    // create a UUID
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   339
    $uuid_base = md5(microtime() . ( function_exists('mt_rand') ? mt_rand() : rand() ));
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   340
    $this->uuid = substr($uuid_base, 0,  8) . '-' .
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   341
                  substr($uuid_base, 8,  4) . '-' .
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   342
                  substr($uuid_base, 12, 4) . '-' .
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   343
                  substr($uuid_base, 16, 4) . '-' .
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   344
                  substr($uuid_base, 20, 20);
37
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
   345
                  
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   346
  }
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   347
  
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   348
  /**
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   349
   * Destructor.
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   350
   */
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   351
  
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   352
  function __destruct()
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   353
  {
67
c4aefad02ce4 WebServer now does not attempt to destruct if threader is not up
Dan
parents: 64
diff changeset
   354
    if ( !is_object($this->threader) )
c4aefad02ce4 WebServer now does not attempt to destruct if threader is not up
Dan
parents: 64
diff changeset
   355
    {
c4aefad02ce4 WebServer now does not attempt to destruct if threader is not up
Dan
parents: 64
diff changeset
   356
      return false;
c4aefad02ce4 WebServer now does not attempt to destruct if threader is not up
Dan
parents: 64
diff changeset
   357
    }
48
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   358
    if ( !$this->threader->is_child() && $this->socket_initted )
15
2adca0f363fd Added multi-threading/forking/keep-alive support to webserver. w00t, feeling all POSIX-happy today!
Dan
parents: 13
diff changeset
   359
    {
21
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   360
      if ( function_exists('status') )
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   361
        status('WebServer: destroying socket');
37
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
   362
      $this->server->destroy();
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
   363
      
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
   364
      // tell all children to shut down
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
   365
      if ( $this->allow_fork )
21
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   366
      {
37
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
   367
        if ( function_exists('status') )
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
   368
          status('WebServer: asking all children to exit');
48
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   369
        $this->threader->kill_all_children();
21
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   370
      }
48
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   371
    }
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   372
  }
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   373
  
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   374
  /**
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   375
   * Reboot the server. Useful for applying new settings.
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   376
   * @param string Optional, new IP address to bind to
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   377
   * @param int Optional, new port to bind to
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   378
   * @param bool Optional, whether to allow forking or not
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   379
   */
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   380
  
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   381
  function reboot($addr = null, $port = null, $allow_fork = null)
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   382
  {
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   383
    if ( function_exists('status') )
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   384
      status('Reboot request has been received');
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   385
    
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   386
    $addr = ( !is_string($addr) ) ? $this->bind_address : $addr;
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   387
    $port = ( !is_int($port) ) ? $this->port : $port;
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   388
    $fork = ( !is_bool($allow_fork) ) ? $this->allow_fork : $allow_fork;
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   389
    
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   390
    //
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   391
    // REBOOTING IS A COMPLICATED THING.
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   392
    // We need to ask all children to close any existing connections so
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   393
    // that all relevant socket resources can be freed. Then we need to
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   394
    // call the constructor again to respawn the server, and finally
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   395
    // re-enter the server loop.
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   396
    //
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   397
    // However, reboot() is often called from a PHP-based handler. This
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   398
    // means that some config page probably still needs to be sent. What
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   399
    // we can do here is send an IPC event that fires the actual reboot,
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   400
    // then return to allow the current page to finish up. We also need
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   401
    // to signal the current process to shut down any existing keep-
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   402
    // alive connections. This can be accomplished by setting in_keepalive
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   403
    // to false.
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   404
    //
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   405
    
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   406
    // Kill the entire child after this response is sent
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   407
    $this->in_keepalive = false;
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   408
    
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   409
    // If we're the parent process, we need to know that a reboot event
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   410
    // was fired, and thus the server's main socket needs to be destroyed
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   411
    // and recreated. This is just done with another boolean switch.
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   412
    $this->reboot_sent = true;
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   413
    
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   414
    // this is really to track if there are any children
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   415
    $oldfork = $this->allow_fork;
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   416
    
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   417
    // Set our new server flags
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   418
    $this->bind_address = $addr;
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   419
    $this->port = $port;
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   420
    $this->allow_fork = $fork;
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   421
    
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   422
    // If we're a child, we have to tell the parent what the hell is
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   423
    // going on, and then get out of here as quickly as possible
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   424
    // (and other children should do the same). If this is a child,
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   425
    // fire an IPC reboot event. Else, fire a "die all" event
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   426
    if ( $this->threader->is_child() )
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   427
    {
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   428
      if ( function_exists('status') )
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   429
        status('Signaling parent with parameter changes (fork = ' . intval($fork) . ') + reboot request');
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   430
      // this is the child
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   431
      $this->threader->ipc_send(array(
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   432
        'action' => 'ws_reboot',
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   433
        'addr' => $addr,
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   434
        'port' => $port,
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   435
        'fork' => $fork
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   436
      ));
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   437
    }
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   438
    else if ( !$this->threader->is_child() && $oldfork )
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   439
    {
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   440
      if ( function_exists('status') )
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   441
        status('Waiting on all children');
37
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
   442
      
48
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   443
      // this is the parent, and there are children present
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   444
      $this->threader->kill_all_children();
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   445
      
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   446
      // all children are dead, we are ok to respawn
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   447
      $this->respawn();
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   448
    }
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   449
    else
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   450
    {
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   451
      // this is a childless parent; delay any action until the current
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   452
      // request has been sent (do nothing now)
37
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
   453
    }
48
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   454
  }
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   455
  
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   456
  /**
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   457
   * Respawns the server. All children should be dead, and any client
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   458
   * connections must be closed.
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   459
   */
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   460
  
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   461
  function respawn()
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   462
  {
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   463
    $this->reboot_sent = false;
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   464
    
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   465
    if ( function_exists('status') )
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   466
      status('Respawn event sent');
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   467
    $this->server->destroy();
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   468
    unset($this->server);
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   469
    
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   470
    // try to spawn up to 10 times
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   471
    for ( $i = 0; $i < 10; $i++ )
37
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
   472
    {
48
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   473
      try
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   474
      {
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   475
        $this->__construct($this->bind_address, $this->port);
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   476
      }
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   477
      catch ( Exception $e )
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   478
      {
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   479
        if ( $i == 9 )
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   480
        {
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   481
          if ( function_exists('burnout') )
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   482
          {
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   483
            burnout("Couldn't respawn because one of the child processes did not die, and thus the port was not freed.");
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   484
          }
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   485
          exit(1);
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   486
        }
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   487
        if ( function_exists('status') )
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   488
        {
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   489
          status("Respawn failed, retrying in 2 seconds");
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   490
        }
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   491
        usleep(2000000);
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   492
        continue;
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   493
      }
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   494
      break;
15
2adca0f363fd Added multi-threading/forking/keep-alive support to webserver. w00t, feeling all POSIX-happy today!
Dan
parents: 13
diff changeset
   495
    }
48
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   496
    
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   497
    if ( function_exists('status') )
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   498
      status('Respawn is complete, entering server loop with bind_address = ' . $this->bind_address . ' allow_fork = ' . strval(intval($this->allow_fork)));
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   499
    
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   500
    // all handlers should already be set up, so just break out and we should automatically continue the server loop
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   501
  }
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   502
  
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   503
  /**
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   504
   * Main server loop
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   505
   */
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   506
  
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   507
  function serve()
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   508
  {
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   509
    while ( true )
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   510
    {
48
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   511
      ##
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   512
      ## STAGE 0: CLEANUP FROM PREVIOUS RUN
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   513
      ##
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   514
      
15
2adca0f363fd Added multi-threading/forking/keep-alive support to webserver. w00t, feeling all POSIX-happy today!
Dan
parents: 13
diff changeset
   515
      // if this is a child process, we're finished - close up shop
48
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   516
      if ( $this->threader->is_child() && !$this->in_keepalive )
15
2adca0f363fd Added multi-threading/forking/keep-alive support to webserver. w00t, feeling all POSIX-happy today!
Dan
parents: 13
diff changeset
   517
      {
21
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   518
        if ( function_exists('status') )
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   519
          status('Exiting child process');
37
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
   520
        
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
   521
        $remote->destroy();
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
   522
        
15
2adca0f363fd Added multi-threading/forking/keep-alive support to webserver. w00t, feeling all POSIX-happy today!
Dan
parents: 13
diff changeset
   523
        exit(0);
2adca0f363fd Added multi-threading/forking/keep-alive support to webserver. w00t, feeling all POSIX-happy today!
Dan
parents: 13
diff changeset
   524
      }
2adca0f363fd Added multi-threading/forking/keep-alive support to webserver. w00t, feeling all POSIX-happy today!
Dan
parents: 13
diff changeset
   525
      
48
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   526
      ##
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   527
      ## STAGE 1: LISTENER AND INIT
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   528
      ##
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   529
      
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   530
      // wait for connection...
48
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   531
      if ( !$this->threader->is_child() )
15
2adca0f363fd Added multi-threading/forking/keep-alive support to webserver. w00t, feeling all POSIX-happy today!
Dan
parents: 13
diff changeset
   532
      {
37
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
   533
        $remote = $this->server->accept();
12
b3fcc21e557f Backend improvement to webserver that causes interrupts to work properly; made pcntl_signal only be called if it's supported
Dan
parents: 10
diff changeset
   534
      }
37
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
   535
      
12
b3fcc21e557f Backend improvement to webserver that causes interrupts to work properly; made pcntl_signal only be called if it's supported
Dan
parents: 10
diff changeset
   536
      if ( !$remote )
b3fcc21e557f Backend improvement to webserver that causes interrupts to work properly; made pcntl_signal only be called if it's supported
Dan
parents: 10
diff changeset
   537
      {
15
2adca0f363fd Added multi-threading/forking/keep-alive support to webserver. w00t, feeling all POSIX-happy today!
Dan
parents: 13
diff changeset
   538
        $this->in_keepalive = false;
12
b3fcc21e557f Backend improvement to webserver that causes interrupts to work properly; made pcntl_signal only be called if it's supported
Dan
parents: 10
diff changeset
   539
        continue;
b3fcc21e557f Backend improvement to webserver that causes interrupts to work properly; made pcntl_signal only be called if it's supported
Dan
parents: 10
diff changeset
   540
      }
b3fcc21e557f Backend improvement to webserver that causes interrupts to work properly; made pcntl_signal only be called if it's supported
Dan
parents: 10
diff changeset
   541
      
15
2adca0f363fd Added multi-threading/forking/keep-alive support to webserver. w00t, feeling all POSIX-happy today!
Dan
parents: 13
diff changeset
   542
      // fork off if possible
2adca0f363fd Added multi-threading/forking/keep-alive support to webserver. w00t, feeling all POSIX-happy today!
Dan
parents: 13
diff changeset
   543
      if ( function_exists('pcntl_fork') && $this->allow_fork && !$this->in_keepalive )
2adca0f363fd Added multi-threading/forking/keep-alive support to webserver. w00t, feeling all POSIX-happy today!
Dan
parents: 13
diff changeset
   544
      {
56
53b0f0d7ff6f Fixed wrong PID variable in httpd fork
Dan
parents: 53
diff changeset
   545
        $fork_result = $this->threader->fork();
53b0f0d7ff6f Fixed wrong PID variable in httpd fork
Dan
parents: 53
diff changeset
   546
        if ( $fork_result == Threader::FORK_CHILD )
15
2adca0f363fd Added multi-threading/forking/keep-alive support to webserver. w00t, feeling all POSIX-happy today!
Dan
parents: 13
diff changeset
   547
        {
48
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   548
          // this is the child
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   549
          define('HTTPD_WS_CHILD', 1);
15
2adca0f363fd Added multi-threading/forking/keep-alive support to webserver. w00t, feeling all POSIX-happy today!
Dan
parents: 13
diff changeset
   550
        }
48
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   551
        else
15
2adca0f363fd Added multi-threading/forking/keep-alive support to webserver. w00t, feeling all POSIX-happy today!
Dan
parents: 13
diff changeset
   552
        {
2adca0f363fd Added multi-threading/forking/keep-alive support to webserver. w00t, feeling all POSIX-happy today!
Dan
parents: 13
diff changeset
   553
          // we are the parent, continue listening
37
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
   554
          $remote->soft_shutdown();
56
53b0f0d7ff6f Fixed wrong PID variable in httpd fork
Dan
parents: 53
diff changeset
   555
          $this->child_list[] = $fork_result;
15
2adca0f363fd Added multi-threading/forking/keep-alive support to webserver. w00t, feeling all POSIX-happy today!
Dan
parents: 13
diff changeset
   556
          continue;
2adca0f363fd Added multi-threading/forking/keep-alive support to webserver. w00t, feeling all POSIX-happy today!
Dan
parents: 13
diff changeset
   557
        }
2adca0f363fd Added multi-threading/forking/keep-alive support to webserver. w00t, feeling all POSIX-happy today!
Dan
parents: 13
diff changeset
   558
      }
2adca0f363fd Added multi-threading/forking/keep-alive support to webserver. w00t, feeling all POSIX-happy today!
Dan
parents: 13
diff changeset
   559
      
2adca0f363fd Added multi-threading/forking/keep-alive support to webserver. w00t, feeling all POSIX-happy today!
Dan
parents: 13
diff changeset
   560
      $this->in_keepalive = false;
21
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   561
      $this->headers_sent = false;
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   562
      $this->in_scriptlet = false;
15
2adca0f363fd Added multi-threading/forking/keep-alive support to webserver. w00t, feeling all POSIX-happy today!
Dan
parents: 13
diff changeset
   563
      
48
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   564
      ##
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   565
      ## STAGE 2: READ REQUEST
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   566
      ##
37
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
   567
      
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
   568
      // this is a complicated situation because we need to keep enough ticks going to properly handle
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
   569
      // signals, meaning we can't use stream_set_timeout() and instead need to rely on our own timing
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
   570
      // logic. setting the timeout to a short period, say 200,000 usec, we can minimize CPU usage and
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
   571
      // have a good response time.
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
   572
      
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
   573
      $remote->set_timeout(0, 200000);
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
   574
      $start_time = microtime(true);
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
   575
      $client_headers = '';
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
   576
      $last_line = '';
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
   577
      while ( true )
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
   578
      {
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
   579
        if ( $start_time + HTTPD_KEEP_ALIVE_TIMEOUT < microtime(true) || $remote->is_eof() )
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
   580
        {
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
   581
          // request expired -- end the process here
48
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   582
          if ( !$this->threader->is_child() )
37
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
   583
            $remote->destroy();
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
   584
          
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
   585
          continue 2;
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
   586
        }
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
   587
        $line = strval($remote->read_normal());
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
   588
        $line = str_replace("\r", "", $line);
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
   589
        
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
   590
        // raw layer wants to send 2 empty lines through, stream layer wants to send one.
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
   591
        $last_line_check = ( HTTPD_SOCKET_LAYER != 'Raw' || ( HTTPD_SOCKET_LAYER == 'Raw' && $last_line == "\n" ) );
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
   592
        if ( $line == "\n" && $last_line_check )
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
   593
          // we have two newlines in a row, break out since we have a full request
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
   594
          break;
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
   595
          
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
   596
        $client_headers .= $line;
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
   597
        $last_line = $line;
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
   598
      }
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
   599
      
48
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   600
      ##
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   601
      ## STAGE 3: PARSE REQUEST AND HEADERS
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   602
      ##
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   603
      
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   604
      // parse request
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   605
      $client_headers = trim($client_headers);
29
8210fdc7c317 Switched default theme to grey.
Dan
parents: 26
diff changeset
   606
      
48
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   607
      if ( isset($last_finish_time) && empty($client_headers) && $this->threader->is_child() && $last_finish_time + HTTPD_KEEP_ALIVE_TIMEOUT < microtime(true) )
29
8210fdc7c317 Switched default theme to grey.
Dan
parents: 26
diff changeset
   608
      {
8210fdc7c317 Switched default theme to grey.
Dan
parents: 26
diff changeset
   609
        status('[debug] keep-alive connection timed out (checkpoint 2)');
8210fdc7c317 Switched default theme to grey.
Dan
parents: 26
diff changeset
   610
        continue; // will jump back to the start of the loop and kill the child process
8210fdc7c317 Switched default theme to grey.
Dan
parents: 26
diff changeset
   611
      }
8210fdc7c317 Switched default theme to grey.
Dan
parents: 26
diff changeset
   612
      
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   613
      $client_headers = explode("\n", $client_headers);
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   614
      
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   615
      // first line
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   616
      $request = $client_headers[0];
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   617
      if ( !preg_match('/^(GET|POST) \/([^ ]*) HTTP\/1\.[01]$/', $request, $match) )
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   618
      {
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   619
        $this->send_http_error($remote, 400, 'Your client issued a malformed or illegal request.');
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   620
        continue;
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   621
      }
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   622
      $method =& $match[1];
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   623
      $uri =& $match[2];
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   624
      
48
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   625
      // set client header SERVER variables
43
2634d550a97b Added full cookie support to webserver
Dan
parents: 40
diff changeset
   626
      foreach ( $_SERVER as $key => $_ )
2634d550a97b Added full cookie support to webserver
Dan
parents: 40
diff changeset
   627
      {
2634d550a97b Added full cookie support to webserver
Dan
parents: 40
diff changeset
   628
        if ( preg_match('/^HTTP_/', $key) )
2634d550a97b Added full cookie support to webserver
Dan
parents: 40
diff changeset
   629
          unset($_SERVER[$key]);
2634d550a97b Added full cookie support to webserver
Dan
parents: 40
diff changeset
   630
      }
64
ee64bb096f56 A few miscellaneous fixes including modifying WebServer to write data in chunks (improved performance and reliability on a slow connection)
Dan
parents: 63
diff changeset
   631
      if ( !isset($_SERVER['SERVER_SOFTWARE']) )
ee64bb096f56 A few miscellaneous fixes including modifying WebServer to write data in chunks (improved performance and reliability on a slow connection)
Dan
parents: 63
diff changeset
   632
      {
ee64bb096f56 A few miscellaneous fixes including modifying WebServer to write data in chunks (improved performance and reliability on a slow connection)
Dan
parents: 63
diff changeset
   633
        $_SERVER['SERVER_SOFTWARE'] =& $this->server_string;
ee64bb096f56 A few miscellaneous fixes including modifying WebServer to write data in chunks (improved performance and reliability on a slow connection)
Dan
parents: 63
diff changeset
   634
      }
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   635
      unset($client_headers[0]);
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   636
      foreach ( $client_headers as $line )
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   637
      {
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   638
        if ( !preg_match('/^([A-z0-9-]+): (.+)$/is', $line, $match) )
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   639
          continue;
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   640
        $key = 'HTTP_' . strtoupper(str_replace('-', '_', $match[1]));
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   641
        $_SERVER[$key] = $match[2];
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   642
      }
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   643
      
15
2adca0f363fd Added multi-threading/forking/keep-alive support to webserver. w00t, feeling all POSIX-happy today!
Dan
parents: 13
diff changeset
   644
      // enable keep-alive if requested
48
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   645
      if ( isset($_SERVER['HTTP_CONNECTION']) && $this->threader->is_child() )
15
2adca0f363fd Added multi-threading/forking/keep-alive support to webserver. w00t, feeling all POSIX-happy today!
Dan
parents: 13
diff changeset
   646
      {
16
23d4cf2f183b Disabled forking/keep-alive code, firefox doesn't seem to like the way it waits for requests
Dan
parents: 15
diff changeset
   647
        $this->in_keepalive = ( strtolower($_SERVER['HTTP_CONNECTION']) === 'keep-alive' );
15
2adca0f363fd Added multi-threading/forking/keep-alive support to webserver. w00t, feeling all POSIX-happy today!
Dan
parents: 13
diff changeset
   648
      }
2adca0f363fd Added multi-threading/forking/keep-alive support to webserver. w00t, feeling all POSIX-happy today!
Dan
parents: 13
diff changeset
   649
      
43
2634d550a97b Added full cookie support to webserver
Dan
parents: 40
diff changeset
   650
      // process cookies
2634d550a97b Added full cookie support to webserver
Dan
parents: 40
diff changeset
   651
      $_COOKIE = array();
2634d550a97b Added full cookie support to webserver
Dan
parents: 40
diff changeset
   652
      if ( isset($_SERVER['HTTP_COOKIE']) )
21
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   653
      {
43
2634d550a97b Added full cookie support to webserver
Dan
parents: 40
diff changeset
   654
        preg_match_all('/([a-z0-9_-]+)=([^;]*)(?:;|$)/', trim($_SERVER['HTTP_COOKIE']), $matches);
2634d550a97b Added full cookie support to webserver
Dan
parents: 40
diff changeset
   655
        foreach ( $matches[0] as $i => $match )
2634d550a97b Added full cookie support to webserver
Dan
parents: 40
diff changeset
   656
        {
2634d550a97b Added full cookie support to webserver
Dan
parents: 40
diff changeset
   657
          $_COOKIE[$matches[1][$i]] = str_replace('\\r', "\r", str_replace('\\n', "\n", str_replace(rawurlencode(';'), ';', $matches[2][$i])));
2634d550a97b Added full cookie support to webserver
Dan
parents: 40
diff changeset
   658
        }
21
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   659
      }
43
2634d550a97b Added full cookie support to webserver
Dan
parents: 40
diff changeset
   660
      
2634d550a97b Added full cookie support to webserver
Dan
parents: 40
diff changeset
   661
      // parse authorization, if any
2634d550a97b Added full cookie support to webserver
Dan
parents: 40
diff changeset
   662
      unset($_SERVER['PHP_AUTH_USER'], $_SERVER['PHP_AUTH_PW']);
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   663
      if ( isset($_SERVER['HTTP_AUTHORIZATION']) )
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   664
      {
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   665
        $data = $_SERVER['HTTP_AUTHORIZATION'];
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   666
        $data = substr(strstr($data, ' '), 1);
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   667
        $data = base64_decode($data);
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   668
        $_SERVER['PHP_AUTH_USER'] = substr($data, 0, strpos($data, ':'));
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   669
        $_SERVER['PHP_AUTH_PW'] = substr(strstr($data, ':'), 1);
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   670
      }
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   671
      
21
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   672
      // anything on POST?
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   673
      $postdata = '';
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   674
      $_POST = array();
23
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
   675
      $_FILES = array();
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   676
      if ( $method == 'POST' )
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   677
      {
48
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   678
        $this->parse_post_data($remote);
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   679
      }
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   680
      
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   681
      // parse URI
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   682
      $params = '';
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   683
      if ( strstr($uri, '?') )
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   684
      {
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   685
        $params = substr(strstr($uri, '?'), 1);
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   686
        $uri    = substr($uri, 0, strpos($uri, '?'));
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   687
      }
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   688
      
21
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   689
      // set some server vars
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   690
      $_SERVER['REQUEST_URI'] = '/' . rawurldecode($uri);
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   691
      $_SERVER['REQUEST_METHOD'] = $method;
21
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   692
      
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   693
      // get remote IP and port
37
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
   694
      $remote->get_peer_info($_SERVER['REMOTE_ADDR'], $_SERVER['REMOTE_PORT']);
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   695
      
48
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   696
      // process $_GET
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   697
      $_GET = array();
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   698
      if ( preg_match_all('/(^|&)([a-z0-9_\.\[\]-]+)(=[^ &]+)?/', $params, $matches) )
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   699
      {
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   700
        if ( isset($matches[1]) )
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   701
        {
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   702
          foreach ( $matches[0] as $i => $_ )
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   703
          {
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   704
            $_GET[$matches[2][$i]] = ( !empty($matches[3][$i]) ) ? urldecode(substr($matches[3][$i], 1)) : true;
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   705
          }
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   706
        }
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   707
      }
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   708
      
48
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   709
      // Parse GET, POST, and FILES into multi-depth arrays
23
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
   710
      $_GET = $this->parse_multi_depth_array($_GET);
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
   711
      $_POST = $this->parse_multi_depth_array($_POST);
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
   712
      $_FILES = $this->parse_multi_depth_array($_FILES);
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
   713
      
48
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   714
      ##
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   715
      ## STAGE 4: HANDLER RESOLUTION
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   716
      ##
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   717
      
21
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   718
      // init handler
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   719
      $handler = false;
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   720
      
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   721
      if ( $uri == '' )
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   722
      {
21
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   723
        // user requested the root (/). If there's a default document, use that; else, see if we can do a directory listing
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   724
        $uri = strval($this->default_document);
21
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   725
        if ( !$this->default_document && $this->allow_dir_list )
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   726
        {
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   727
          // we can list directories and this was requested by the user, so list it out
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   728
          $handler = array('type' => 'rootdir');
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   729
        }
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   730
      }
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   731
      
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   732
      $uri_parts = explode('/', $uri);
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   733
      
21
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   734
      // hook for the special UUID handler
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   735
      if ( $uri_parts[0] === $this->uuid && !$handler )
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   736
      {
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   737
        $handler = array('type' => 'sysuuid');
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   738
      }
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   739
      
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   740
      // loop through URI parts, see if a handler is set
21
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   741
      if ( !$handler )
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   742
      {
21
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   743
        for ( $i = count($uri_parts) - 1; $i >= 0; $i-- )
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   744
        {
21
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   745
          $handler_test = implode('/', $uri_parts);
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   746
          if ( isset($this->handlers[$handler_test]) )
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   747
          {
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   748
            $handler = $this->handlers[$handler_test];
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   749
            $handler['id'] = $handler_test;
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   750
            break;
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   751
          }
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   752
          unset($uri_parts[$i]);
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   753
        }
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   754
      }
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   755
      
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   756
      if ( !$handler )
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   757
      {
21
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   758
        // try to make a fakie
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   759
        if ( $this->check_for_handler_children($uri) )
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   760
        {
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   761
          $handler = array(
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   762
            'type' => 'folder',
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   763
            'dir' => "/{$this->uuid}/__fakie",
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   764
            'id' => $uri
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   765
          );
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   766
        }
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   767
        if ( !$handler )
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   768
        {
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   769
          $this->send_http_error($remote, 404, "The requested URL /$uri was not found on this server.");
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   770
          continue;
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   771
        }
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   772
      }
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   773
      
48
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   774
      ##
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   775
      ## STAGE 5: HANDLER CALL
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   776
      ##
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   777
      
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   778
      $this->send_standard_response($remote, $handler, $uri, $params);
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   779
      
48
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   780
      ##
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   781
      ## STAGE 6: CLEANUP
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   782
      ##
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   783
      
23
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
   784
      // now that we're done sending the response, delete any temporary uploaded files
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
   785
      if ( !empty($_FILES) )
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
   786
      {
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
   787
        foreach ( $_FILES as $file_data )
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
   788
        {
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
   789
          if ( file_exists($file_data['tmp_name']) )
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
   790
          {
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
   791
            @unlink($file_data['tmp_name']);
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
   792
          }
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
   793
        }
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
   794
      }
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
   795
      
48
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   796
      if ( !$this->in_keepalive && $this->threader->is_child() )
15
2adca0f363fd Added multi-threading/forking/keep-alive support to webserver. w00t, feeling all POSIX-happy today!
Dan
parents: 13
diff changeset
   797
      {
37
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
   798
        // connection: close
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
   799
        // continue on to the shutdown handler
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
   800
        continue;
15
2adca0f363fd Added multi-threading/forking/keep-alive support to webserver. w00t, feeling all POSIX-happy today!
Dan
parents: 13
diff changeset
   801
      }
48
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   802
      else if ( $this->threader->is_child() )
15
2adca0f363fd Added multi-threading/forking/keep-alive support to webserver. w00t, feeling all POSIX-happy today!
Dan
parents: 13
diff changeset
   803
      {
48
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   804
        // if ( $this->threader->is_child() )
15
2adca0f363fd Added multi-threading/forking/keep-alive support to webserver. w00t, feeling all POSIX-happy today!
Dan
parents: 13
diff changeset
   805
        //   status('Continuing connection');
37
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
   806
        // $remote->write("\r\n\r\n");
29
8210fdc7c317 Switched default theme to grey.
Dan
parents: 26
diff changeset
   807
        $last_finish_time = microtime(true);
15
2adca0f363fd Added multi-threading/forking/keep-alive support to webserver. w00t, feeling all POSIX-happy today!
Dan
parents: 13
diff changeset
   808
      }
21
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   809
      else
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   810
      {
48
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   811
        // standalone process
37
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
   812
        $remote->destroy();
48
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   813
        
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   814
        // if a reboot was fired and we're running in single-process mode, now is the time to respawn
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   815
        if ( !$this->threader->is_child() && $this->reboot_sent )
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   816
        {
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   817
          $this->respawn();
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   818
        }
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   819
      }
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   820
    }
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   821
  }
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   822
  
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   823
  /**
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   824
   * Parse POST data and format $_POST and $_FILES.
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   825
   * @param resource Remote socket
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   826
   */
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   827
  
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   828
  function parse_post_data($remote)
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   829
  {
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   830
    $postdata = '';
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   831
    
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   832
    // read POST data
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   833
    if ( isset($_SERVER['HTTP_CONTENT_TYPE']) && preg_match('#^multipart/form-data; ?boundary=([A-z0-9_-]+)$#i', $_SERVER['HTTP_CONTENT_TYPE'], $match) )
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   834
    {
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   835
      // this is a multipart request
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   836
      $boundary =& $match[1];
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   837
      $mode = 'data';
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   838
      $last_line = '';
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   839
      $i = 0;
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   840
      while ( $data = $remote->read_normal(8388608) )
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   841
      {
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   842
        $data_trim = trim($data, "\r\n");
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   843
        if ( $mode != 'data' )
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   844
        {
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   845
          $data = str_replace("\r", '', $data);
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   846
        }
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   847
        if ( ( $data_trim === "--$boundary" || $data_trim === "--$boundary--" ) && $i > 0 )
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   848
        {
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   849
          // trim off the first LF and the last CRLF
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   850
          if ( HTTPD_SOCKET_LAYER == 'Raw' )
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   851
            $currval_data = substr($currval_data, 1, strlen($currval_data)-3);
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   852
          else
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   853
            $currval_data = substr($currval_data, 0, strlen($currval_data)-2);
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   854
          
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   855
          // this is the end of a part of the message; parse it into either $_POST or $_FILES
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   856
          if ( is_string($have_a_file) )
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   857
          {
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   858
            // write data to a temporary file
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   859
            $errcode = UPLOAD_ERR_OK;
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   860
            $tempfile = tempnam('phpupload', ( function_exists('sys_get_temp_dir') ? sys_get_temp_dir() : '/tmp' ));
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   861
            if ( $fh = @fopen($tempfile, 'w') )
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   862
            {
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   863
              if ( empty($have_a_file) )
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   864
              {
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   865
                $errcode = UPLOAD_ERR_NO_FILE;
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   866
              }
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   867
              else
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   868
              {
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   869
                fwrite($fh, $currval_data);
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   870
              }
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   871
              fclose($fh);
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   872
            }
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   873
            else
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   874
            {
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   875
              $errcode = UPLOAD_ERR_CANT_WRITE;
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   876
            }
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   877
            $_FILES[$currval_name] = array(
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   878
                'name' => $have_a_file,
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   879
                'type' => $currval_type,
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   880
                'size' => filesize($tempfile),
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   881
                'tmp_name' => $tempfile,
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   882
                'error' => $errcode
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   883
              );
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   884
          }
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   885
          else
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   886
          {
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   887
            if ( preg_match('/\[\]$/', $currval_name) )
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   888
            {
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   889
              if ( !isset($_POST[$currval_name]) || ( isset($_POST[$currval_name]) && !is_array($_POST[$currval_name]) ) )
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   890
                $_POST[$currval_name] = array();
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   891
              
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   892
              $_POST[$currval_name][] = $currval_data;
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   893
            }
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   894
            else
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   895
            {
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   896
              $_POST[$currval_name] = $currval_data;
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   897
            }
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   898
          }
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   899
        }
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   900
        
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   901
        if ( $data_trim === "--$boundary" )
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   902
        {
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   903
          // switch from "data" mode to "headers" mode
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   904
          $currval_name = '';
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   905
          $currval_data = '';
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   906
          $currval_type = '';
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   907
          $have_a_file = false;
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   908
          $mode = 'headers';
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   909
        }
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   910
        else if ( $data_trim === "--$boundary--" )
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   911
        {
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   912
          // end of request
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   913
          break;
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   914
        }
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   915
        else if ( ( empty($data_trim) && ( ( HTTPD_SOCKET_LAYER == 'Raw' && empty($last_line) ) || HTTPD_SOCKET_LAYER != 'Raw' ) ) && $mode == 'headers' )
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   916
        {
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   917
          // start of data
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   918
          $mode = 'data';
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   919
        }
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   920
        else if ( $mode == 'headers' )
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   921
        {
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   922
          // read header
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   923
          // we're only looking for Content-Disposition and Content-Type
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   924
          if ( preg_match('#^Content-Disposition: form-data; name="([^"\a\t\r\n]+)"(?:; filename="([^"\a\t\r\n]+)")?#i', $data_trim, $match) )
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   925
          {
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   926
            // content-disposition header, set name and mode.
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   927
            $currval_name = $match[1];
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   928
            if ( isset($match[2]) )
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   929
            {
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   930
              $have_a_file = $match[2];
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   931
            }
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   932
            else
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   933
            {
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   934
              $have_a_file = false;
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   935
            }
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   936
          }
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   937
          else if ( preg_match('#^Content-Type: ([a-z0-9-]+/[a-z0-9/-]+)$#i', $data_trim, $match) )
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   938
          {
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   939
            $currval_type = $match[1];
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   940
          }
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   941
        }
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   942
        else if ( $mode == 'data' )
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   943
        {
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   944
          $currval_data .= $data;
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   945
        }
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   946
        $last_line = $data_trim;
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   947
        $i++;
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   948
      }
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   949
    }
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   950
    else
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   951
    {
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   952
      if ( isset($_SERVER['HTTP_CONTENT_LENGTH']) )
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   953
      {
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   954
        $postdata = $remote->read_binary(intval($_SERVER['HTTP_CONTENT_LENGTH']));
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   955
      }
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   956
      else
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   957
      {
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   958
        $postdata = $remote->read_normal(8388608);
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   959
      }
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   960
      if ( preg_match_all('/(^|&)([a-z0-9_\.\[\]%-]+)(=[^ &]+)?/i', $postdata, $matches) )
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   961
      {
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   962
        if ( isset($matches[1]) )
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   963
        {
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   964
          foreach ( $matches[0] as $i => $_ )
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   965
          {
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   966
            $currval_name =& $matches[2][$i];
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   967
            $currval_data = ( !empty($matches[3][$i]) ) ? urldecode(substr($matches[3][$i], 1)) : true;
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   968
            $currval_name = urldecode($currval_name);
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   969
            
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   970
            if ( preg_match('/\[\]$/', $currval_name) )
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   971
            {
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   972
              $basename = preg_replace('/\[\]$/', '', $currval_name);
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   973
              if ( !isset($_POST[$basename]) || ( isset($_POST[$basename]) && !is_array($_POST[$basename]) ) )
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   974
                $_POST[$basename] = array();
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   975
              
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   976
              $_POST[$basename][] = $currval_data;
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   977
            }
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   978
            else
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   979
            {
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   980
              $_POST[$currval_name] = $currval_data;
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   981
            }
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   982
          }
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
   983
        }
21
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   984
      }
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   985
    }
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   986
  }
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   987
  
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   988
  /**
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   989
   * Sends the client appropriate response headers.
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   990
   * @param resource Socket connection to client
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   991
   * @param int HTTP status code, defaults to 200
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   992
   * @param string Content type, defaults to text/html
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   993
   * @param string Additional headers to send, optional
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   994
   */                     
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   995
  
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   996
  function send_client_headers($socket, $http_code = 200, $contenttype = 'text/html', $headers = '')
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   997
  {
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   998
    global $http_responses;
21
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   999
    if ( $this->headers_sent )
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1000
      return false;
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1001
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1002
    // this is reset after the request is completed (hopefully)    
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1003
    $this->headers_sent = true;
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1004
    
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1005
    $reason_code = ( isset($http_responses[$http_code]) ) ? $http_responses[$http_code] : 'Unknown';
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1006
    
15
2adca0f363fd Added multi-threading/forking/keep-alive support to webserver. w00t, feeling all POSIX-happy today!
Dan
parents: 13
diff changeset
  1007
    $_SERVER['HTTP_USER_AGENT'] = ( isset($_SERVER['HTTP_USER_AGENT']) ) ? $_SERVER['HTTP_USER_AGENT'] : '(no user agent)';
21
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1008
    
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1009
    if ( function_exists('status') )
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1010
      status("{$_SERVER['REMOTE_ADDR']} {$_SERVER['REQUEST_METHOD']} {$_SERVER['REQUEST_URI']} $http_code {$_SERVER['HTTP_USER_AGENT']}");
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1011
    
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1012
    $headers = str_replace("\r\n", "\n", $headers);
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1013
    $headers = str_replace("\n", "\r\n", $headers);
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1014
    $headers = preg_replace("#[\r\n]+$#", '', $headers);
15
2adca0f363fd Added multi-threading/forking/keep-alive support to webserver. w00t, feeling all POSIX-happy today!
Dan
parents: 13
diff changeset
  1015
    $connection = ( $this->in_keepalive ) ? 'keep-alive' : 'close';
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1016
    
37
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
  1017
    $socket->write("HTTP/1.1 $http_code $reason_code\r\n");
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
  1018
    $socket->write("Server: $this->server_string");
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
  1019
    $socket->write("Connection: $connection\r\n");
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
  1020
    $socket->write("Content-Type: $contenttype\r\n");
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1021
    if ( !empty($headers) )
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1022
    {
37
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
  1023
      $socket->write("$headers\r\n");
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1024
    }
37
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
  1025
    $socket->write("\r\n");
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1026
  }
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1027
  
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1028
  /**
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1029
   * Sends a normal response
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1030
   * @param resource Socket connection to client
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1031
   * @param array Handler
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1032
   */
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1033
  
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1034
  function send_standard_response($socket, $handler)
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1035
  {
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1036
    switch ( $handler['type'] )
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1037
    {
21
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1038
      case 'folder':
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1039
        // security
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1040
        $uri = str_replace("\000", '', $_SERVER['REQUEST_URI']);
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1041
        if ( preg_match('#(\.\./|\/\.\.)#', $uri) || strstr($uri, "\r") || strstr($uri, "\n") )
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1042
        {
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1043
          $this->send_http_error($socket, 403, 'Access to this resource is forbidden.');
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1044
        }
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1045
        
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1046
        // import mimetypes
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1047
        global $mime_types;
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1048
        
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1049
        // trim handler id from uri
21
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1050
        $uri_full = rtrim($uri, '/');
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1051
        $uri = substr($uri, strlen($handler['id']) + 1);
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1052
        
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1053
        // get file path
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1054
        $file_path = rtrim($handler['dir'], '/') . $uri;
21
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1055
        if ( file_exists($file_path) || $this->check_for_handler_children($uri_full) )
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1056
        {
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1057
          // found it :-D
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1058
          
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1059
          // is this a directory?
21
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1060
          if ( is_dir($file_path) || $this->check_for_handler_children($uri_full) )
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1061
          {
21
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1062
            // allowed to list?
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1063
            if ( !$this->allow_dir_list )
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1064
            {
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1065
              $this->send_http_error($socket, 403, "Directory listing is not allowed.");
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1066
              return true;
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1067
            }
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1068
            // yes, list contents
21
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1069
            try
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1070
            {
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1071
              $dir_list = $this->list_directory($uri_full, true);
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1072
            }
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1073
            catch ( Exception $e )
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1074
            {
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1075
              $this->send_http_error($socket, 500, "Directory listing failed due to an error in the listing core method. This may indicate that the webserver process does not have filesystem access to the specified directory.<br /><br />Debugging details:<pre>$e</pre>");
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1076
              return true;
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1077
            }
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1078
            
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1079
            $root = rtrim($uri_full, '/') . '/';
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1080
            $parent = rtrim(dirname(rtrim($uri_full, '/')), '/') . '/';
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1081
            
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1082
            $contents = <<<EOF
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1083
<html>
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1084
  <head>
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1085
    <title>Index of: $root</title>
21
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1086
    <link rel="stylesheet" type="text/css" href="/{$this->uuid}/dirlist.css" />
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1087
  </head>
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1088
  <body>
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1089
    <h1>Index of $root</h1>
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1090
    <ul>
21
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1091
      <li><tt><a href="$parent">Parent directory</a></tt></li>
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1092
    
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1093
EOF;
21
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1094
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1095
            foreach ( $dir_list as $filename => $info )
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1096
            {
21
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1097
              $ts = ( $info['type'] == 'folder' ) ? '/' : '';
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1098
              $contents .= '  <li><tt><a href="' . htmlspecialchars($root . basename($filename) . $ts) . '"><img alt="[   ]" src="/' . $this->uuid . '/' . $info['type'] . '.png" /> ' . htmlspecialchars($filename) . $ts . '</a></tt></li>' . "\n    ";
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1099
            }
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1100
            $contents .= "\n    </ul>\n    <address>Served by {$this->server_string}</address>\n</body>\n</html>\n\n";
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1101
            
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1102
            $sz = strlen($contents);
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1103
            $this->send_client_headers($socket, 200, 'text/html', "Content-length: $sz\r\n");
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1104
            
37
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
  1105
            $socket->write($contents);
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1106
            
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1107
            return true;
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1108
          }
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1109
          
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1110
          // try to open the file
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1111
          $fh = @fopen($file_path, 'r');
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1112
          if ( !$fh )
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1113
          {
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1114
            // can't open it, send a 404
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1115
            $this->send_http_error($socket, 404, "The requested URL " . htmlspecialchars($_SERVER['REQUEST_URI']) . " was not found on this server.");
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1116
          }
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1117
          
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1118
          // get size
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1119
          $sz = filesize($file_path);
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1120
          
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1121
          // mod time
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1122
          $time = date('r', filemtime($file_path));
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1123
          
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1124
          // all good, send headers
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1125
          $fileext = substr($file_path, strrpos($file_path, '.') + 1);
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1126
          $mimetype = ( isset($mime_types[$fileext]) ) ? $mime_types[$fileext] : 'application/octet-stream';
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1127
          $this->send_client_headers($socket, 200, $mimetype, "Content-length: $sz\r\nLast-Modified: $time\r\n");
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1128
          
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1129
          // send body
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1130
          while ( $blk = @fread($fh, 768000) )
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1131
          {
37
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
  1132
            $socket->write($blk);
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1133
          }
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1134
          fclose($fh);
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1135
          return true;
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1136
        }
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1137
        else
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1138
        {
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1139
          $this->send_http_error($socket, 404, "The requested URL " . htmlspecialchars($_SERVER['REQUEST_URI']) . " was not found on this server.");
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1140
        }
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1141
        
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1142
        break;
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1143
      case 'file':
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1144
        
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1145
        // import mimetypes
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1146
        global $mime_types;
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1147
        
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1148
        // get file path
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1149
        $file_path = $handler['file'];
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1150
        if ( file_exists($file_path) )
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1151
        {
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1152
          // found it :-D
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1153
          
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1154
          // is this a directory?
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1155
          if ( is_dir($file_path) )
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1156
          {
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1157
            $this->send_http_error($socket, 500, "Host script mapped a directory as a file entry.");
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1158
            return true;
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1159
          }
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1160
          
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1161
          // try to open the file
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1162
          $fh = @fopen($file_path, 'r');
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1163
          if ( !$fh )
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1164
          {
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1165
            // can't open it, send a 404
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1166
            $this->send_http_error($socket, 404, "The requested URL " . htmlspecialchars($_SERVER['REQUEST_URI']) . " was not found on this server.");
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1167
          }
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1168
          
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1169
          // get size
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1170
          $sz = filesize($file_path);
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1171
          
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1172
          // mod time
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1173
          $time = date('r', filemtime($file_path));
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1174
          
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1175
          // all good, send headers
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1176
          $fileext = substr($file_path, strrpos($file_path, '.') + 1);
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1177
          $mimetype = ( isset($mime_types[$fileext]) ) ? $mime_types[$fileext] : 'application/octet-stream';
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1178
          $this->send_client_headers($socket, 200, $mimetype, "Content-length: $sz\r\nLast-Modified: $time\r\n");
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1179
          
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1180
          // send body
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1181
          while ( $blk = @fread($fh, 768000) )
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1182
          {
37
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
  1183
            $socket->write($blk);
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1184
          }
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1185
          fclose($fh);
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1186
          return true;
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1187
        }
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1188
        else
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1189
        {
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1190
          $this->send_http_error($socket, 404, "The requested URL " . htmlspecialchars($_SERVER['REQUEST_URI']) . " was not found on this server.");
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1191
        }
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1192
        
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1193
        break;
21
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1194
      case 'script':
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1195
        // init vars
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1196
        $this->content_type = 'text/html';
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1197
        $this->response_code = 200;
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1198
        $this->response_headers = array();
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1199
        
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1200
        // error handling
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1201
        @set_error_handler(array($this, 'function_error_handler'), E_ALL);
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1202
        try
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1203
        {
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1204
          ob_start();
21
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1205
          $this->in_scriptlet = true;
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1206
          $result = @call_user_func($handler['function'], $this, $socket);
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1207
          $this->in_scriptlet = false;
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1208
          $output = ob_get_contents();
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1209
          ob_end_clean();
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1210
        }
23
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
  1211
        // throw an HttpExceptionFatal when you need to break out of an in-progress scriptlet due to an error, use it in place of die() or exit()
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
  1212
        catch ( HttpExceptionFatal $e )
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
  1213
        {
24
d275dc8f4203 Added HttpSuccess exception which can be thrown when a scriptlet is done executing normally
Dan
parents: 23
diff changeset
  1214
          ob_end_clean();
23
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
  1215
          restore_error_handler();
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
  1216
          $this->send_http_error($socket, 500, "A handler crashed reporting a fatal exception; see the command line for details.");
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
  1217
          if ( function_exists('status') )
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
  1218
            status("fatal exception in handler {$handler['id']}:\n$e");
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
  1219
          return true;
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
  1220
        }
24
d275dc8f4203 Added HttpSuccess exception which can be thrown when a scriptlet is done executing normally
Dan
parents: 23
diff changeset
  1221
        catch ( HttpSuccess $e )
d275dc8f4203 Added HttpSuccess exception which can be thrown when a scriptlet is done executing normally
Dan
parents: 23
diff changeset
  1222
        {
d275dc8f4203 Added HttpSuccess exception which can be thrown when a scriptlet is done executing normally
Dan
parents: 23
diff changeset
  1223
          // just finish with success
d275dc8f4203 Added HttpSuccess exception which can be thrown when a scriptlet is done executing normally
Dan
parents: 23
diff changeset
  1224
          $this->in_scriptlet = false;
d275dc8f4203 Added HttpSuccess exception which can be thrown when a scriptlet is done executing normally
Dan
parents: 23
diff changeset
  1225
          $output = ob_get_contents();
d275dc8f4203 Added HttpSuccess exception which can be thrown when a scriptlet is done executing normally
Dan
parents: 23
diff changeset
  1226
          ob_end_clean();
d275dc8f4203 Added HttpSuccess exception which can be thrown when a scriptlet is done executing normally
Dan
parents: 23
diff changeset
  1227
        }
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1228
        catch ( Exception $e )
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1229
        {
24
d275dc8f4203 Added HttpSuccess exception which can be thrown when a scriptlet is done executing normally
Dan
parents: 23
diff changeset
  1230
          ob_end_clean();
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1231
          restore_error_handler();
23
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
  1232
          $this->send_http_error($socket, 500, "There was an uncaught exception during the execution of a scripted handler function. See the command line for details.");
21
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1233
          if ( function_exists('status') )
23
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
  1234
            status("uncaught exception in handler {$handler['id']}:\n$e");
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1235
          return true;
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1236
        }
24
d275dc8f4203 Added HttpSuccess exception which can be thrown when a scriptlet is done executing normally
Dan
parents: 23
diff changeset
  1237
        
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1238
        restore_error_handler();
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1239
        
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1240
        // the handler function should return this magic string if it writes its own headers and socket data
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1241
        if ( $output == '__break__' )
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1242
        {
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1243
          return true;
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1244
        }
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1245
        
40
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 38
diff changeset
  1246
        if ( !$this->headers_sent )
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 38
diff changeset
  1247
        {
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 38
diff changeset
  1248
          // $this->header('Transfer-encoding: chunked');
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 38
diff changeset
  1249
          $this->header("Content-length: " . strlen($output));
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 38
diff changeset
  1250
          $headers = implode("\r\n", $this->response_headers);
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 38
diff changeset
  1251
          
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 38
diff changeset
  1252
          // write headers
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 38
diff changeset
  1253
          $this->send_client_headers($socket, $this->response_code, $this->content_type, $headers);
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 38
diff changeset
  1254
          
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 38
diff changeset
  1255
          // chunk output
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 38
diff changeset
  1256
          // $output = dechex(strlen($output)) . "\r\n$output";
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 38
diff changeset
  1257
          
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 38
diff changeset
  1258
          // write body
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 38
diff changeset
  1259
          $socket->write($output);
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 38
diff changeset
  1260
          
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 38
diff changeset
  1261
          $this->headers_sent = false;
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 38
diff changeset
  1262
        }
21
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1263
        
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1264
        break;
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1265
      case 'sysuuid':
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1266
        // requested one of the system's icon images
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1267
        $uri_parts = explode('/', $_SERVER['REQUEST_URI']);
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1268
        if ( count($uri_parts) != 3 )
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1269
        {
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1270
          $this->send_http_error($socket, 404, "The requested URL " . htmlspecialchars($_SERVER['REQUEST_URI']) . " was not found on this server.");
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1271
        }
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1272
        
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1273
        // load image data
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1274
        $filename =& $uri_parts[2];
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1275
        switch ( $filename )
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1276
        {
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1277
          case 'script.png':
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1278
            ( !isset($image_data) ) ? $image_data = HTTPD_ICON_SCRIPT : null;
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1279
          case 'folder.png':
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1280
            ( !isset($image_data) ) ? $image_data = HTTPD_ICON_FOLDER : null;
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1281
          case 'file.png':
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1282
            ( !isset($image_data) ) ? $image_data = HTTPD_ICON_FILE : null;
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1283
            
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1284
            $image_data = base64_decode($image_data);
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1285
            $found = true;
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1286
            $type = 'image/png';
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1287
            break;
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1288
          case 'dirlist.css':
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1289
            $type = 'text/css';
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1290
            $found = true;
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1291
            $image_data = <<<EOF
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1292
/**
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1293
 * PhpHttpd directory list visual style
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1294
 */
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1295
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1296
html {
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1297
  background-color: #c9c9c9;
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1298
  margin: 0;
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1299
  padding: 0;
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1300
}
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1301
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1302
body {
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1303
  background-color: #ffffff;
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1304
  margin: 20px;
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1305
  padding: 10px;
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1306
  border: 1px solid #aaaaaa;
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1307
}
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1308
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1309
a {
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1310
  text-decoration: none;
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1311
}
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1312
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1313
a img {
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1314
  border-width: 0;
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1315
}
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1316
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1317
ul {
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1318
  list-style-type: none;
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1319
}
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1320
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1321
EOF;
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1322
            break;
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1323
          default:
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1324
            $found = false;
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1325
        }
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1326
        
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1327
        // ship it out
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1328
        if ( $found )
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1329
        {
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1330
          $lm_date = date('r', filemtime(__FILE__));
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1331
          $size = strlen($image_data);
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1332
          $this->send_client_headers($socket, 200, $type, "Last-Modified: $lm_date\r\nContent-Length: $size");
37
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
  1333
          $socket->write($image_data);
21
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1334
        }
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1335
        else
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1336
        {
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1337
          $this->send_http_error($socket, 404, "The requested URL " . htmlspecialchars($_SERVER['REQUEST_URI']) . " was not found on this server.");
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1338
        }
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1339
        
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1340
        return true;
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1341
        
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1342
        break;
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1343
      case 'rootdir':
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1344
        //
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1345
        // list the contents of the document root
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1346
        //
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1347
        
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1348
        $handlers = $this->list_directory('/', true);
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1349
        
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1350
        $contents = <<<EOF
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1351
<html>
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1352
  <head>
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1353
    <title>Index of: /</title>
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1354
    <link rel="stylesheet" type="text/css" href="/{$this->uuid}/dirlist.css" />
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1355
  </head>
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1356
  <body>
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1357
    <h1>Index of /</h1>
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1358
    <ul>
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1359
    
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1360
EOF;
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1361
        
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1362
        $html = '';
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1363
        // generate content
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1364
        foreach ( $handlers as $uri => $handler )
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1365
        {
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1366
          switch($handler['type'])
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1367
          {
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1368
            case 'folder':
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1369
              $image = 'folder.png';
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1370
              $abbr = 'DIR';
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1371
              $add = '/';
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1372
              break;
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1373
            case 'file':
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1374
            default:
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1375
              $image = 'file.png';
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1376
              $abbr = '   ';
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1377
              $add = '';
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1378
              break;
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1379
            case 'script':
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1380
              $image = 'script.png';
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1381
              $abbr = 'CGI';
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1382
              $add = '';
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1383
              break;
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1384
          }
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1385
          $html .= "  <li><tt><a href=\"/$uri\"><img alt=\"[{$abbr}]\" src=\"/{$this->uuid}/{$image}\" /> {$uri}{$add}</a></tt></li>\n      ";
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1386
        }
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1387
        
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1388
        $contents .= $html;
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1389
        $contents .= <<<EOF
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1390
</ul>
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1391
    <address>Served by {$this->server_string}</address>
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1392
  </body>
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1393
</html>
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1394
EOF;
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1395
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1396
        // get length
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1397
        $len = strlen($contents);
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1398
        
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1399
        // send headers
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1400
        $this->send_client_headers($socket, 200, 'text/html', "Content-Length: $len");
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1401
        
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1402
        // write to the socket
37
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
  1403
        $socket->write($contents);
21
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1404
        
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1405
        return true;
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1406
        break;
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1407
    }
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1408
  }
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1409
  
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1410
  /**
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1411
   * Adds an HTTP header value to send back to the client
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1412
   * @var string Header
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1413
   */
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1414
  
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1415
  function header($str)
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1416
  {
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1417
    if ( preg_match('#HTTP/1\.[01] ([0-9]+) (.+?)[\s]*$#', $str, $match) )
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1418
    {
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1419
      $this->response_code = intval($match[1]);
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1420
      return true;
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1421
    }
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1422
    else if ( preg_match('#Content-type: ([^ ;]+)#i', $str, $match) )
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1423
    {
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1424
      $this->content_type = $match[1];
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1425
      return true;
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1426
    }
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1427
    $this->response_headers[] = $str;
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1428
    return true;
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1429
  }
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1430
  
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1431
  /**
43
2634d550a97b Added full cookie support to webserver
Dan
parents: 40
diff changeset
  1432
   * Sets a cookie. Identical to PHP's setcookie() function.
2634d550a97b Added full cookie support to webserver
Dan
parents: 40
diff changeset
  1433
   * @param string Cookie name
2634d550a97b Added full cookie support to webserver
Dan
parents: 40
diff changeset
  1434
   * @param string Cookie value
2634d550a97b Added full cookie support to webserver
Dan
parents: 40
diff changeset
  1435
   * @param int Expiration time of cookie as a UNIX timestamp; if omitted or set to zero, cookie will expire at the end of the user's browser session
2634d550a97b Added full cookie support to webserver
Dan
parents: 40
diff changeset
  1436
   * @param string Path of the cookie
2634d550a97b Added full cookie support to webserver
Dan
parents: 40
diff changeset
  1437
   * @param string Domain the cookie is available under
2634d550a97b Added full cookie support to webserver
Dan
parents: 40
diff changeset
  1438
   * @param bool If true, browser will only send the cookie through an HTTPS connection.
2634d550a97b Added full cookie support to webserver
Dan
parents: 40
diff changeset
  1439
   * @param bool If true, cookie will not be accessible to client-side code
2634d550a97b Added full cookie support to webserver
Dan
parents: 40
diff changeset
  1440
   */
2634d550a97b Added full cookie support to webserver
Dan
parents: 40
diff changeset
  1441
  
2634d550a97b Added full cookie support to webserver
Dan
parents: 40
diff changeset
  1442
  function setcookie($cookiename, $cookievalue, $expiry = false, $path = false, $domain = false, $secure = false, $httponly = false)
2634d550a97b Added full cookie support to webserver
Dan
parents: 40
diff changeset
  1443
  {
2634d550a97b Added full cookie support to webserver
Dan
parents: 40
diff changeset
  1444
    $header = "Set-Cookie: $cookiename=$cookievalue";
2634d550a97b Added full cookie support to webserver
Dan
parents: 40
diff changeset
  1445
    if ( !empty($expiry) )
63
af239423ed15 Cookies should now persist in WebServer under Safari
Dan
parents: 62
diff changeset
  1446
      $header .= "; expires=" . gmdate('D, d-M-Y H:i:s T', $expiry);
43
2634d550a97b Added full cookie support to webserver
Dan
parents: 40
diff changeset
  1447
    if ( !empty($path) )
2634d550a97b Added full cookie support to webserver
Dan
parents: 40
diff changeset
  1448
      $header .= "; path=$path";
2634d550a97b Added full cookie support to webserver
Dan
parents: 40
diff changeset
  1449
    if ( !empty($domain) )
2634d550a97b Added full cookie support to webserver
Dan
parents: 40
diff changeset
  1450
      $header .= "; domain=$domain";
2634d550a97b Added full cookie support to webserver
Dan
parents: 40
diff changeset
  1451
    if ( $secure )
2634d550a97b Added full cookie support to webserver
Dan
parents: 40
diff changeset
  1452
      $header .= "; secure";
2634d550a97b Added full cookie support to webserver
Dan
parents: 40
diff changeset
  1453
    if ( $httponly )
2634d550a97b Added full cookie support to webserver
Dan
parents: 40
diff changeset
  1454
      $header .= "; httponly";
2634d550a97b Added full cookie support to webserver
Dan
parents: 40
diff changeset
  1455
    
2634d550a97b Added full cookie support to webserver
Dan
parents: 40
diff changeset
  1456
    if ( is_int($expiry) && $expiry < time() )
2634d550a97b Added full cookie support to webserver
Dan
parents: 40
diff changeset
  1457
    {
2634d550a97b Added full cookie support to webserver
Dan
parents: 40
diff changeset
  1458
      unset($_COOKIE[$cookiename]);
2634d550a97b Added full cookie support to webserver
Dan
parents: 40
diff changeset
  1459
    }
2634d550a97b Added full cookie support to webserver
Dan
parents: 40
diff changeset
  1460
    else
2634d550a97b Added full cookie support to webserver
Dan
parents: 40
diff changeset
  1461
    {
2634d550a97b Added full cookie support to webserver
Dan
parents: 40
diff changeset
  1462
      $_COOKIE[$cookiename] = $cookievalue;
2634d550a97b Added full cookie support to webserver
Dan
parents: 40
diff changeset
  1463
    }
2634d550a97b Added full cookie support to webserver
Dan
parents: 40
diff changeset
  1464
    
2634d550a97b Added full cookie support to webserver
Dan
parents: 40
diff changeset
  1465
    $this->header($header);
63
af239423ed15 Cookies should now persist in WebServer under Safari
Dan
parents: 62
diff changeset
  1466
    return $header;
43
2634d550a97b Added full cookie support to webserver
Dan
parents: 40
diff changeset
  1467
  }
2634d550a97b Added full cookie support to webserver
Dan
parents: 40
diff changeset
  1468
  
2634d550a97b Added full cookie support to webserver
Dan
parents: 40
diff changeset
  1469
  /**
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1470
   * Sends the client an HTTP error page
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1471
   * @param resource Socket connection to client
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1472
   * @param int HTTP status code
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1473
   * @param string Detailed error string
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1474
   */
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1475
  
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1476
  function send_http_error($socket, $http_code, $errstring)
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1477
  {
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1478
    global $http_responses;
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1479
    $reason_code = ( isset($http_responses[$http_code]) ) ? $http_responses[$http_code] : 'Unknown';
21
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1480
    
26
300d374d89b0 Made error pages send length of response to get keep-alive working properly; added (nonworking, sorry) keep-alive timeout support.
Dan
parents: 24
diff changeset
  1481
    // generate error page
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1482
    $html = <<<EOF
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1483
<html>
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1484
  <head>
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1485
    <title>$http_code $reason_code</title>
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1486
  </head>
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1487
  <body>
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1488
    <h1>$http_code $reason_code</h1>
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1489
    <p>$errstring</p>
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1490
    <hr />
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1491
    <address>Served by $this->server_string</address>
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1492
  </body>
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1493
</html>
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1494
EOF;
26
300d374d89b0 Made error pages send length of response to get keep-alive working properly; added (nonworking, sorry) keep-alive timeout support.
Dan
parents: 24
diff changeset
  1495
    
300d374d89b0 Made error pages send length of response to get keep-alive working properly; added (nonworking, sorry) keep-alive timeout support.
Dan
parents: 24
diff changeset
  1496
    // length of the response (required if we want keep-alive to work)
300d374d89b0 Made error pages send length of response to get keep-alive working properly; added (nonworking, sorry) keep-alive timeout support.
Dan
parents: 24
diff changeset
  1497
    $this->header('Content-length: ' . strlen($html));
300d374d89b0 Made error pages send length of response to get keep-alive working properly; added (nonworking, sorry) keep-alive timeout support.
Dan
parents: 24
diff changeset
  1498
    
300d374d89b0 Made error pages send length of response to get keep-alive working properly; added (nonworking, sorry) keep-alive timeout support.
Dan
parents: 24
diff changeset
  1499
    // if we're in a scriptlet, include custom headers
300d374d89b0 Made error pages send length of response to get keep-alive working properly; added (nonworking, sorry) keep-alive timeout support.
Dan
parents: 24
diff changeset
  1500
    if ( $this->in_scriptlet )
300d374d89b0 Made error pages send length of response to get keep-alive working properly; added (nonworking, sorry) keep-alive timeout support.
Dan
parents: 24
diff changeset
  1501
      $headers = implode("\r\n", $this->response_headers);
300d374d89b0 Made error pages send length of response to get keep-alive working properly; added (nonworking, sorry) keep-alive timeout support.
Dan
parents: 24
diff changeset
  1502
    else
300d374d89b0 Made error pages send length of response to get keep-alive working properly; added (nonworking, sorry) keep-alive timeout support.
Dan
parents: 24
diff changeset
  1503
      $headers = 'Content-length: ' . strlen($html);
300d374d89b0 Made error pages send length of response to get keep-alive working properly; added (nonworking, sorry) keep-alive timeout support.
Dan
parents: 24
diff changeset
  1504
      
300d374d89b0 Made error pages send length of response to get keep-alive working properly; added (nonworking, sorry) keep-alive timeout support.
Dan
parents: 24
diff changeset
  1505
    $this->send_client_headers($socket, $http_code, 'text/html', $headers);
37
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
  1506
    $socket->write($html);
21
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1507
  } 
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1508
  
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1509
  /**
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1510
   * Adds a new handler
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1511
   * @param string URI, minus the initial /
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1512
   * @param string Type of handler - function or dir
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1513
   * @param string Value - function name or absolute/relative path to directory
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1514
   */
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1515
  
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1516
  function add_handler($uri, $type, $value)
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1517
  {
21
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1518
    if ( $type == 'dir' )
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1519
      $type = 'folder';
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1520
    if ( $type == 'function' )
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1521
      $type = 'script';
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1522
    switch($type)
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1523
    {
21
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1524
      case 'folder':
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1525
        $this->handlers[$uri] = array(
21
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1526
            'type' => 'folder',
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1527
            'dir' => $value
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1528
          );
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1529
        break;
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1530
      case 'file':
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1531
        $this->handlers[$uri] = array(
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1532
            'type' => 'file',
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1533
            'file' => $value
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1534
          );
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1535
        break;
21
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1536
      case 'script':
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1537
        $this->handlers[$uri] = array(
21
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1538
            'type' => 'script',
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1539
            'function' => $value
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1540
          );
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1541
        break;
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1542
    }
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1543
  }
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1544
  
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1545
  /**
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1546
   * Error handling function
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1547
   * @param see <http://us.php.net/manual/en/function.set-error-handler.php>
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1548
   */
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1549
  
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1550
  function function_error_handler($errno, $errstr, $errfile, $errline, $errcontext)
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1551
  {
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1552
    echo '<div style="border: 1px solid #AA0000; background-color: #FFF0F0; padding: 10px;">';
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1553
    echo "<b>PHP warning/error:</b> type $errno ($errstr) caught in <b>$errfile</b> on <b>$errline</b><br />";
10
d3059e20b0fa SECURITY: Fix ability to crash server in ajax.php; added playback position slider and ability to seek through current song
Dan
parents: 6
diff changeset
  1554
    // echo "Error context:<pre>" . htmlspecialchars(print_r($errcontext, true)) . "</pre>";
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1555
    echo '</div>';
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1556
  }
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1557
  
21
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1558
  /**
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1559
   * Lists out the contents of a directory, including virtual handlers.
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1560
   * @example
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1561
   * Example return data: (will be ksorted)
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1562
   <code>
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1563
   array(
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1564
       'bar' => 'folder',
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1565
       'baz' => 'script',
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1566
       'foo' => 'file'
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1567
     );
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1568
   </code>
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1569
   * @param string Directory name, relative to the server's document root
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1570
   * @param bool If true, sorts folders first (default: false)
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1571
   * @return array Exception thrown on failure
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1572
   */
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1573
  
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1574
  function list_directory($dir, $folders_first = false)
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1575
  {
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1576
    // clean slashes from the directory name
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1577
    $dir = trim($dir, '/');
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1578
    
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1579
    if ( $dir == '' )
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1580
    {
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1581
      //
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1582
      // list the root, which can consist only of handlers
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1583
      //
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1584
      
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1585
      // copy the handlers array, which we need to ksort
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1586
      $handlers = $this->handlers;
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1587
      
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1588
      // get rid of multi-depth handlers
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1589
      foreach ( $handlers as $uri => $handler )
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1590
      {
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1591
        if ( strpos($uri, '/') )
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1592
        {
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1593
          unset($handlers[$uri]);
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1594
          $newuri = explode('/', $uri);
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1595
          if ( !isset($handlers[$newuri[0]]) )
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1596
          {
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1597
            $handlers[$newuri[0]] = array(
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1598
                'type' => 'folder'
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1599
              );
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1600
          }
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1601
        }
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1602
      }
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1603
      
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1604
      ksort($handlers);
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1605
      
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1606
      if ( $folders_first )
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1607
      {
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1608
        // sort folders first
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1609
        $handlers_sorted = array();
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1610
        foreach ( $handlers as $uri => $handler )
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1611
        {
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1612
          if ( $handler['type'] == 'folder' )
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1613
            $handlers_sorted[$uri] = $handler;
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1614
        }
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1615
        foreach ( $handlers as $uri => $handler )
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1616
        {
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1617
          if ( $handler['type'] != 'folder' )
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1618
            $handlers_sorted[$uri] = $handler;
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1619
        }
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1620
        $handlers = $handlers_sorted;
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1621
        unset($handlers_sorted);
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1622
      }
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1623
      
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1624
      // done
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1625
      return $handlers;
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1626
    }
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1627
    else
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1628
    {
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1629
      // list something within the root
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1630
      $dir_stack = explode('/', $dir);
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1631
      
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1632
      // lookup handler
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1633
      $handler_search = $dir;
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1634
      $found_handler = false;
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1635
      $fake_handler = false;
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1636
      $i = 1;
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1637
      while ( $i > 0 )
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1638
      {
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1639
        if ( isset($this->handlers[$handler_search]) )
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1640
        {
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1641
          $found_handler = true;
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1642
          break;
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1643
        }
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1644
        $i = strrpos($handler_search, '/');
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1645
        $handler_search = substr($handler_search, 0, strrpos($handler_search, '/'));
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1646
      }
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1647
      if ( $this->check_for_handler_children($dir) )
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1648
      {
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1649
        $fake_handler = true;
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1650
      }
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1651
      else if ( !$found_handler )
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1652
      {
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1653
        // nope. not there.
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1654
        throw new Exception("ERR_NO_SUCH_FILE_OR_DIRECTORY");
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1655
      }
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1656
      
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1657
      // make sure this is a directory
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1658
      if ( !$fake_handler )
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1659
      {
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1660
        $handler =& $handler_search;
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1661
        if ( $this->handlers[$handler]['type'] != 'folder' )
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1662
        {
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1663
          throw new Exception("ERR_NOT_A_DIRECTORY");
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1664
        }
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1665
        
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1666
        // determine real path
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1667
        $real_path = realpath($this->handlers[$handler]['dir'] . substr($dir, strlen($handler)));
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1668
        
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1669
        // directory is resolved; list contents
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1670
        $dir_contents = array();
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1671
        
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1672
        if ( $dr = opendir($real_path) )
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1673
        {
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1674
          while ( $dh = readdir($dr) )
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1675
          {
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1676
            if ( $dh == '.' || $dh == '..' )
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1677
            {
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1678
              continue;
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1679
            }
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1680
            $dir_contents[$dh] = array(
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1681
                'type' => ( is_dir("$real_path/$dh") ) ? 'folder' : 'file',
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1682
                'size' => filesize("$real_path/$dh"),
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1683
                'time' => filemtime("$real_path/$dh")
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1684
              );
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1685
          }
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1686
        }
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1687
        else
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1688
        {
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1689
          // only if directory open failed
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1690
          throw new Exception("ERR_PERMISSION_DENIED");
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1691
        }
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1692
        
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1693
        closedir($dr);
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1694
      
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1695
        // some cleanup
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1696
        unset($handler, $handler_search);
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1697
      }
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1698
      
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1699
      // list any additional handlers in there
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1700
      foreach ( $this->handlers as $handler => $info )
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1701
      {
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1702
        // parse handler name
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1703
        $handler_name = explode('/', trim($handler, '/'));
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1704
        // is this handler in this directory?
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1705
        if ( count($handler_name) != count($dir_stack) + 1 )
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1706
        {
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1707
          continue;
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1708
        }
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1709
        foreach ( $dir_stack as $i => $_ )
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1710
        {
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1711
          if ( $dir_stack[$i] != $handler_name[$i] )
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1712
          {
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1713
            continue 2;
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1714
          }
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1715
        }
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1716
        // it's in here!
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1717
        $dir_contents[ basename($handler) ] = array(
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1718
            'type' => $info['type']
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1719
          );
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1720
      }
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1721
      
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1722
      // list "fake" handlers
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1723
      foreach ( $this->handlers as $handler => $info )
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1724
      {
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1725
        // parse handler name
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1726
        $handler_name = explode('/', trim($handler, '/'));
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1727
        // is this handler somewhere underneath this directory?
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1728
        if ( count($handler_name) < count($dir_stack) + 2 )
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1729
        {
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1730
          continue;
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1731
        }
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1732
        // path check
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1733
        foreach ( $dir_stack as $i => $_ )
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1734
        {
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1735
          if ( $dir_stack[$i] != $handler_name[$i] )
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1736
          {
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1737
            continue 2;
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1738
          }
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1739
        }
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1740
        // create a "fake" directory
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1741
        $fakie_name = $handler_name[ count($dir_stack) ];
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1742
        $dir_contents[$fakie_name] = array(
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1743
            'type' => 'folder'
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1744
          );
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1745
      }
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1746
      
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1747
      if ( $folders_first )
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1748
      {
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1749
        // perform folder sorting
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1750
        $unsorted = $dir_contents;
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1751
        ksort($unsorted);
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1752
        $dir_contents = array();
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1753
        foreach ( $unsorted as $name => $info )
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1754
        {
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1755
          if ( $info['type'] == 'folder' )
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1756
            $dir_contents[$name] = $info;
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1757
        }
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1758
        foreach ( $unsorted as $name => $info )
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1759
        {
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1760
          if ( $info['type'] != 'folder' )
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1761
            $dir_contents[$name] = $info;
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1762
        }
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1763
      }
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1764
      else
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1765
      {
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1766
        // not sorting with folders first, so just alphabetize
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1767
        ksort($dir_contents);
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1768
      }
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1769
      
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1770
      // done
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1771
      
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1772
      return $dir_contents;
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1773
    }
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1774
  }
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1775
  
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1776
  /**
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1777
   * Searches deeper to see if there are sub-handlers within a path to see if a fake handler can be created
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1778
   * @param string URI
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1779
   * @return bool
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1780
   */
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1781
  
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1782
  function check_for_handler_children($file_path)
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1783
  {
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1784
    $file_path = trim($file_path, '/');
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1785
    $dir_stack = explode('/', $file_path);
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1786
    
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1787
    // make sure this isn't a "real" handler
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1788
    if ( isset($this->handlers[$file_path]) )
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1789
    {
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1790
      return false;
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1791
    }
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1792
    
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1793
    // list any additional handlers in there
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1794
    foreach ( $this->handlers as $handler => $info )
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1795
    {
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1796
      // parse handler name
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1797
      $handler_name = explode('/', trim($handler, '/'));
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1798
      // is this handler in this directory?
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1799
      if ( count($handler_name) != count($dir_stack) + 1 )
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1800
      {
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1801
        continue;
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1802
      }
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1803
      foreach ( $dir_stack as $i => $_ )
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1804
      {
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1805
        if ( $dir_stack[$i] != $handler_name[$i] )
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1806
        {
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1807
          continue 2;
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1808
        }
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1809
      }
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1810
      // it's in here!
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1811
      return true;
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1812
    }
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1813
    
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1814
    return false;
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1815
  }
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1816
  
23
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
  1817
  /**
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
  1818
   * Takes a flat array with keys of format foo[bar] and parses it into multiple depths.
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
  1819
   * @param array
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
  1820
   * @return array
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
  1821
   */
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
  1822
  
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
  1823
  function parse_multi_depth_array($array)
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
  1824
  {
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
  1825
    foreach ( $array as $key => $value )
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
  1826
    {
48
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
  1827
      if ( preg_match('/^([^\[\]]+)\[([^\]]*)\]/', $key, $match) )
23
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
  1828
      {
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
  1829
        $parent =& $match[1];
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
  1830
        $child =& $match[2];
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
  1831
        if ( !isset($array[$parent]) || ( isset($array[$parent]) && !is_array($array[$parent]) ) )
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
  1832
        {
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
  1833
          $array[$parent] = array();
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
  1834
        }
48
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
  1835
        if ( empty($child) )
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
  1836
        {
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
  1837
          $array[$parent][] = $value;
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
  1838
        }
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
  1839
        else
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
  1840
        {
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
  1841
          $array[$parent][$child] = $value;
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
  1842
        }
23
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
  1843
        unset($array[$key]);
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
  1844
        $array[$parent] = $this->parse_multi_depth_array($array[$parent]);
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
  1845
      }
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
  1846
    }
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
  1847
    return $array;
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
  1848
  }
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
  1849
  
37
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
  1850
  /**
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
  1851
   * Handle an IPC event. Called only upon SIGUSR2.
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
  1852
   */
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
  1853
  
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
  1854
  function _ipc_event()
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
  1855
  {
48
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
  1856
    /*
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
  1857
    case 'set_addr':
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
  1858
      $this->bind_address = $param;
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
  1859
      break;
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
  1860
    case 'set_port':
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
  1861
      $this->port = intval($param);
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
  1862
      break;
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
  1863
    case 'set_fork':
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
  1864
      $this->allow_fork = ( $param == '1' );
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
  1865
      break;
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
  1866
    case 'reboot':
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
  1867
      if ( !$this->threader->is_child() )
37
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
  1868
      {
48
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
  1869
        list(, $addr, $port, $fork) = explode(' ', $line);
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
  1870
        $fork = ( $fork === '1' );
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
  1871
        $this->reboot($addr, intval($port), $fork);
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
  1872
      }
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
  1873
      break;
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
  1874
        default:
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
  1875
          if ( isset($this->ipc_handlers[$action]) )
37
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
  1876
          {
48
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
  1877
            @call_user_func($this->ipc_handlers[$action], $line);
37
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
  1878
          }
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
  1879
          break;
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
  1880
      }
48
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
  1881
      */
37
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
  1882
  }
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
  1883
}
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
  1884
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
  1885
/**
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
  1886
 * Socket abstraction layer - low-level socket functions (socket_*)
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
  1887
 */
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
  1888
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
  1889
class Socket_Raw
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
  1890
{
57
31ce64a3ff6c Some backend changes to webserver to allow IPv6 binding
Dan
parents: 56
diff changeset
  1891
  var $sock = array();
37
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
  1892
  var $socket_initted = false;
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
  1893
  
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
  1894
  function tcp_listen($address, $port)
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
  1895
  {
48
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
  1896
    // do we have socket functions?
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
  1897
    if ( !function_exists('socket_create') )
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
  1898
    {
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
  1899
      burnout('System does not support socket functions. Please rebuild your PHP or install an appropriate extension.');
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
  1900
    }
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
  1901
    
57
31ce64a3ff6c Some backend changes to webserver to allow IPv6 binding
Dan
parents: 56
diff changeset
  1902
    $sockid = count($this->sock);
31ce64a3ff6c Some backend changes to webserver to allow IPv6 binding
Dan
parents: 56
diff changeset
  1903
    
31ce64a3ff6c Some backend changes to webserver to allow IPv6 binding
Dan
parents: 56
diff changeset
  1904
    $socktype = ( strstr($address, ':') ) ? AF_INET6 : AF_INET;
31ce64a3ff6c Some backend changes to webserver to allow IPv6 binding
Dan
parents: 56
diff changeset
  1905
    $this->sock[$sockid] = @socket_create($socktype, SOCK_STREAM, getprotobyname('tcp'));
31ce64a3ff6c Some backend changes to webserver to allow IPv6 binding
Dan
parents: 56
diff changeset
  1906
    if ( !$this->sock[$sockid] )
37
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
  1907
      throw new Exception('Could not create socket');
57
31ce64a3ff6c Some backend changes to webserver to allow IPv6 binding
Dan
parents: 56
diff changeset
  1908
    $result = @socket_bind($this->sock[$sockid], $address, $port);
37
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
  1909
    if ( !$result )
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
  1910
      throw new Exception("Could not bind to $address:$port");
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
  1911
    $this->socket_initted = true;
57
31ce64a3ff6c Some backend changes to webserver to allow IPv6 binding
Dan
parents: 56
diff changeset
  1912
    $result = @socket_listen($this->sock[$sockid], SOMAXCONN);
37
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
  1913
    if ( !$result )
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
  1914
      throw new Exception("Could not listen for connections $address:$port");
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
  1915
    
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
  1916
    $this->socket_initted = true;
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
  1917
  }
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
  1918
  
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
  1919
  function destroy()
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
  1920
  {
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
  1921
    if ( $this->socket_initted )
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
  1922
    {
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
  1923
      // http://us3.php.net/manual/en/function.socket-bind.php
57
31ce64a3ff6c Some backend changes to webserver to allow IPv6 binding
Dan
parents: 56
diff changeset
  1924
      if ( is_array($this->sock) )
37
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
  1925
      {
57
31ce64a3ff6c Some backend changes to webserver to allow IPv6 binding
Dan
parents: 56
diff changeset
  1926
        foreach ( $this->sock as $sock )
31ce64a3ff6c Some backend changes to webserver to allow IPv6 binding
Dan
parents: 56
diff changeset
  1927
        {
31ce64a3ff6c Some backend changes to webserver to allow IPv6 binding
Dan
parents: 56
diff changeset
  1928
          if ( !@socket_set_option($sock, SOL_SOCKET, SO_REUSEADDR, 1) )
31ce64a3ff6c Some backend changes to webserver to allow IPv6 binding
Dan
parents: 56
diff changeset
  1929
          {
31ce64a3ff6c Some backend changes to webserver to allow IPv6 binding
Dan
parents: 56
diff changeset
  1930
            echo socket_strerror(socket_last_error($sock)) . "\n";
31ce64a3ff6c Some backend changes to webserver to allow IPv6 binding
Dan
parents: 56
diff changeset
  1931
          }
31ce64a3ff6c Some backend changes to webserver to allow IPv6 binding
Dan
parents: 56
diff changeset
  1932
          @socket_shutdown($sock, 2);
31ce64a3ff6c Some backend changes to webserver to allow IPv6 binding
Dan
parents: 56
diff changeset
  1933
          @socket_close($sock);
31ce64a3ff6c Some backend changes to webserver to allow IPv6 binding
Dan
parents: 56
diff changeset
  1934
        }
37
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
  1935
      }
57
31ce64a3ff6c Some backend changes to webserver to allow IPv6 binding
Dan
parents: 56
diff changeset
  1936
      else
31ce64a3ff6c Some backend changes to webserver to allow IPv6 binding
Dan
parents: 56
diff changeset
  1937
      {
31ce64a3ff6c Some backend changes to webserver to allow IPv6 binding
Dan
parents: 56
diff changeset
  1938
        if ( !@socket_set_option($this->sock, SOL_SOCKET, SO_REUSEADDR, 1) )
31ce64a3ff6c Some backend changes to webserver to allow IPv6 binding
Dan
parents: 56
diff changeset
  1939
        {
31ce64a3ff6c Some backend changes to webserver to allow IPv6 binding
Dan
parents: 56
diff changeset
  1940
          echo socket_strerror(socket_last_error($this->sock)) . "\n";
31ce64a3ff6c Some backend changes to webserver to allow IPv6 binding
Dan
parents: 56
diff changeset
  1941
        }
31ce64a3ff6c Some backend changes to webserver to allow IPv6 binding
Dan
parents: 56
diff changeset
  1942
        @socket_shutdown($this->sock, 2);
31ce64a3ff6c Some backend changes to webserver to allow IPv6 binding
Dan
parents: 56
diff changeset
  1943
        @socket_close($this->sock);
31ce64a3ff6c Some backend changes to webserver to allow IPv6 binding
Dan
parents: 56
diff changeset
  1944
      }
37
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
  1945
    }
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
  1946
  }
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
  1947
  
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
  1948
  function accept()
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
  1949
  {
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
  1950
    $remote = false;
57
31ce64a3ff6c Some backend changes to webserver to allow IPv6 binding
Dan
parents: 56
diff changeset
  1951
    foreach ( $this->sock as $sock )
31ce64a3ff6c Some backend changes to webserver to allow IPv6 binding
Dan
parents: 56
diff changeset
  1952
    {
31ce64a3ff6c Some backend changes to webserver to allow IPv6 binding
Dan
parents: 56
diff changeset
  1953
      $timeout = 200000;
31ce64a3ff6c Some backend changes to webserver to allow IPv6 binding
Dan
parents: 56
diff changeset
  1954
      switch(@socket_select($r = array($sock), $w = array($sock), $e = array($sock), 0, $timeout)) {
31ce64a3ff6c Some backend changes to webserver to allow IPv6 binding
Dan
parents: 56
diff changeset
  1955
        case 2:
31ce64a3ff6c Some backend changes to webserver to allow IPv6 binding
Dan
parents: 56
diff changeset
  1956
          return false;
31ce64a3ff6c Some backend changes to webserver to allow IPv6 binding
Dan
parents: 56
diff changeset
  1957
        case 1:
31ce64a3ff6c Some backend changes to webserver to allow IPv6 binding
Dan
parents: 56
diff changeset
  1958
          $remote = @socket_accept($sock);
31ce64a3ff6c Some backend changes to webserver to allow IPv6 binding
Dan
parents: 56
diff changeset
  1959
          $return = new Socket_Raw();
31ce64a3ff6c Some backend changes to webserver to allow IPv6 binding
Dan
parents: 56
diff changeset
  1960
          $return->sock = $remote;
31ce64a3ff6c Some backend changes to webserver to allow IPv6 binding
Dan
parents: 56
diff changeset
  1961
          $return->socket_initted = true;
31ce64a3ff6c Some backend changes to webserver to allow IPv6 binding
Dan
parents: 56
diff changeset
  1962
          return $return;
31ce64a3ff6c Some backend changes to webserver to allow IPv6 binding
Dan
parents: 56
diff changeset
  1963
          break;
31ce64a3ff6c Some backend changes to webserver to allow IPv6 binding
Dan
parents: 56
diff changeset
  1964
        case 0:
31ce64a3ff6c Some backend changes to webserver to allow IPv6 binding
Dan
parents: 56
diff changeset
  1965
          continue;
31ce64a3ff6c Some backend changes to webserver to allow IPv6 binding
Dan
parents: 56
diff changeset
  1966
      }
37
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
  1967
    }
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
  1968
  }
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
  1969
  
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
  1970
  /**
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
  1971
   * Closes the socket but doesn't destroy it.
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
  1972
   */
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
  1973
  
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
  1974
  function soft_shutdown()
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
  1975
  {
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
  1976
    @socket_set_option($this->sock, SOL_SOCKET, SO_REUSEADDR, 1);
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
  1977
    socket_close($this->sock);
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
  1978
  }
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
  1979
  
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
  1980
  function set_timeout($timeout, $usec = false)
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
  1981
  {
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
  1982
    // doesn't work in this.
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
  1983
  }
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
  1984
  
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
  1985
  function read_normal($length = 1024)
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
  1986
  {
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
  1987
    return @socket_read($this->sock, $length, PHP_NORMAL_READ);
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
  1988
  }
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
  1989
  
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
  1990
  function read_binary($length = 1024)
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
  1991
  {
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
  1992
    return @socket_read($this->sock, $length, PHP_BINARY_READ);
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
  1993
  }
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
  1994
  
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
  1995
  function timed_out()
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
  1996
  {
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
  1997
    $md = @socket_get_status($this->sock);
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
  1998
    return ( isset($md['timed_out']) ) ? $md['timed_out'] : false;
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
  1999
  }
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
  2000
  
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
  2001
  function get_peer_info(&$addr, &$port)
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
  2002
  {
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
  2003
    socket_getpeername($this->sock, $addr, $port);
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
  2004
  }
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
  2005
  
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
  2006
  function write($data)
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
  2007
  {
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
  2008
    return @socket_write($this->sock, $data);
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
  2009
  }
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
  2010
  
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
  2011
  function is_eof()
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
  2012
  {
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
  2013
    // feof() not supported
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
  2014
    return false;
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
  2015
  }
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
  2016
}
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
  2017
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
  2018
/**
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
  2019
 * Socket abstraction layer - PHP stream support
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
  2020
 */
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
  2021
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
  2022
class Socket_Stream
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
  2023
{
57
31ce64a3ff6c Some backend changes to webserver to allow IPv6 binding
Dan
parents: 56
diff changeset
  2024
  var $sock = array();
37
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
  2025
  var $socket_initted = false;
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
  2026
  
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
  2027
  function tcp_listen($address, $port)
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
  2028
  {
48
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
  2029
    // does PHP support this?
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
  2030
    if ( !function_exists('stream_socket_server') )
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
  2031
    {
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
  2032
      burnout('System does not support stream functions. Please rebuild your PHP or install an appropriate extension.');
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
  2033
    }
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
  2034
    
57
31ce64a3ff6c Some backend changes to webserver to allow IPv6 binding
Dan
parents: 56
diff changeset
  2035
    if ( strstr($address, ':') )
31ce64a3ff6c Some backend changes to webserver to allow IPv6 binding
Dan
parents: 56
diff changeset
  2036
    {
31ce64a3ff6c Some backend changes to webserver to allow IPv6 binding
Dan
parents: 56
diff changeset
  2037
      // ipv6 address (probably)
31ce64a3ff6c Some backend changes to webserver to allow IPv6 binding
Dan
parents: 56
diff changeset
  2038
      $address = "[$address]";
31ce64a3ff6c Some backend changes to webserver to allow IPv6 binding
Dan
parents: 56
diff changeset
  2039
    }
31ce64a3ff6c Some backend changes to webserver to allow IPv6 binding
Dan
parents: 56
diff changeset
  2040
    
31ce64a3ff6c Some backend changes to webserver to allow IPv6 binding
Dan
parents: 56
diff changeset
  2041
    $sockid = count($this->sock);
31ce64a3ff6c Some backend changes to webserver to allow IPv6 binding
Dan
parents: 56
diff changeset
  2042
    
31ce64a3ff6c Some backend changes to webserver to allow IPv6 binding
Dan
parents: 56
diff changeset
  2043
    $this->sock[$sockid] = @stream_socket_server("tcp://$address:$port", $errno, $errstr);
31ce64a3ff6c Some backend changes to webserver to allow IPv6 binding
Dan
parents: 56
diff changeset
  2044
    if ( !$this->sock[$sockid] )
31ce64a3ff6c Some backend changes to webserver to allow IPv6 binding
Dan
parents: 56
diff changeset
  2045
    {
37
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
  2046
      throw new Exception("Could not create the socket: error $errno: $errstr");
57
31ce64a3ff6c Some backend changes to webserver to allow IPv6 binding
Dan
parents: 56
diff changeset
  2047
    }
37
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
  2048
  }
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
  2049
  
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
  2050
  function destroy()
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
  2051
  {
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
  2052
    if ( $this->socket_initted )
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
  2053
    {
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
  2054
      // PHP >= 5.2.1
57
31ce64a3ff6c Some backend changes to webserver to allow IPv6 binding
Dan
parents: 56
diff changeset
  2055
      if ( is_array($this->sock) )
37
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
  2056
      {
57
31ce64a3ff6c Some backend changes to webserver to allow IPv6 binding
Dan
parents: 56
diff changeset
  2057
        foreach ( $this->sock as $sock )
31ce64a3ff6c Some backend changes to webserver to allow IPv6 binding
Dan
parents: 56
diff changeset
  2058
        {
31ce64a3ff6c Some backend changes to webserver to allow IPv6 binding
Dan
parents: 56
diff changeset
  2059
          if ( function_exists('stream_socket_shutdown') )
31ce64a3ff6c Some backend changes to webserver to allow IPv6 binding
Dan
parents: 56
diff changeset
  2060
          {
31ce64a3ff6c Some backend changes to webserver to allow IPv6 binding
Dan
parents: 56
diff changeset
  2061
            @stream_socket_shutdown($sock, STREAM_SHUT_RDWR);
31ce64a3ff6c Some backend changes to webserver to allow IPv6 binding
Dan
parents: 56
diff changeset
  2062
          }
31ce64a3ff6c Some backend changes to webserver to allow IPv6 binding
Dan
parents: 56
diff changeset
  2063
          while ( !@fclose($sock) )
31ce64a3ff6c Some backend changes to webserver to allow IPv6 binding
Dan
parents: 56
diff changeset
  2064
          {
31ce64a3ff6c Some backend changes to webserver to allow IPv6 binding
Dan
parents: 56
diff changeset
  2065
            usleep(100000);
31ce64a3ff6c Some backend changes to webserver to allow IPv6 binding
Dan
parents: 56
diff changeset
  2066
          }
31ce64a3ff6c Some backend changes to webserver to allow IPv6 binding
Dan
parents: 56
diff changeset
  2067
        }
37
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
  2068
      }
57
31ce64a3ff6c Some backend changes to webserver to allow IPv6 binding
Dan
parents: 56
diff changeset
  2069
      else
48
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
  2070
      {
57
31ce64a3ff6c Some backend changes to webserver to allow IPv6 binding
Dan
parents: 56
diff changeset
  2071
        if ( function_exists('stream_socket_shutdown') )
31ce64a3ff6c Some backend changes to webserver to allow IPv6 binding
Dan
parents: 56
diff changeset
  2072
        {
31ce64a3ff6c Some backend changes to webserver to allow IPv6 binding
Dan
parents: 56
diff changeset
  2073
          @stream_socket_shutdown($this->sock, STREAM_SHUT_RDWR);
31ce64a3ff6c Some backend changes to webserver to allow IPv6 binding
Dan
parents: 56
diff changeset
  2074
        }
31ce64a3ff6c Some backend changes to webserver to allow IPv6 binding
Dan
parents: 56
diff changeset
  2075
        while ( !@fclose($this->sock) )
31ce64a3ff6c Some backend changes to webserver to allow IPv6 binding
Dan
parents: 56
diff changeset
  2076
        {
31ce64a3ff6c Some backend changes to webserver to allow IPv6 binding
Dan
parents: 56
diff changeset
  2077
          usleep(100000);
31ce64a3ff6c Some backend changes to webserver to allow IPv6 binding
Dan
parents: 56
diff changeset
  2078
        }
48
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
  2079
      }
37
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
  2080
    }
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
  2081
  }
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
  2082
  
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
  2083
  function accept()
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
  2084
  {
48
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 43
diff changeset
  2085
    // the goal of a custom accept() with *_select() is to tick every 200ms to allow signals.
57
31ce64a3ff6c Some backend changes to webserver to allow IPv6 binding
Dan
parents: 56
diff changeset
  2086
    foreach ( $this->sock as $sock )
62
1b73be5346df Fixed a bug in get_peer_info(); accept() now fully works for multiple bound addresses
Dan
parents: 57
diff changeset
  2087
      stream_set_blocking($sock, 1);
1b73be5346df Fixed a bug in get_peer_info(); accept() now fully works for multiple bound addresses
Dan
parents: 57
diff changeset
  2088
    $timeout = 200000;
1b73be5346df Fixed a bug in get_peer_info(); accept() now fully works for multiple bound addresses
Dan
parents: 57
diff changeset
  2089
    $r = $this->sock;
1b73be5346df Fixed a bug in get_peer_info(); accept() now fully works for multiple bound addresses
Dan
parents: 57
diff changeset
  2090
    $selection = @stream_select($r, $w = array($sock), $e = array($sock), 0, $timeout);
1b73be5346df Fixed a bug in get_peer_info(); accept() now fully works for multiple bound addresses
Dan
parents: 57
diff changeset
  2091
    if ( !$selection )
37
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
  2092
    {
62
1b73be5346df Fixed a bug in get_peer_info(); accept() now fully works for multiple bound addresses
Dan
parents: 57
diff changeset
  2093
      return false;
37
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
  2094
    }
62
1b73be5346df Fixed a bug in get_peer_info(); accept() now fully works for multiple bound addresses
Dan
parents: 57
diff changeset
  2095
    $remote = stream_socket_accept($r[0]);
1b73be5346df Fixed a bug in get_peer_info(); accept() now fully works for multiple bound addresses
Dan
parents: 57
diff changeset
  2096
    $return = new Socket_Stream();
1b73be5346df Fixed a bug in get_peer_info(); accept() now fully works for multiple bound addresses
Dan
parents: 57
diff changeset
  2097
    $return->sock = $remote;
1b73be5346df Fixed a bug in get_peer_info(); accept() now fully works for multiple bound addresses
Dan
parents: 57
diff changeset
  2098
    $return->socket_initted = true;
1b73be5346df Fixed a bug in get_peer_info(); accept() now fully works for multiple bound addresses
Dan
parents: 57
diff changeset
  2099
    return $return;
37
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
  2100
  }
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
  2101
  
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
  2102
  function soft_shutdown()
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
  2103
  {
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
  2104
    fclose($this->sock);
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
  2105
  }
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
  2106
  
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
  2107
  function set_timeout($timeout, $usec = false)
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
  2108
  {
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
  2109
    return ( $usec ) ? @stream_set_timeout($this->sock, 0, $usec) : @stream_set_timeout($this->sock, $timeout);
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
  2110
  }
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
  2111
  
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
  2112
  function read_normal($length = 1024)
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
  2113
  {
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
  2114
    return @fgets($this->sock, $length);
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
  2115
  }
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
  2116
  
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
  2117
  function read_binary($length = 1024)
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
  2118
  {
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
  2119
    return @fread($this->sock, $length);
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
  2120
  }
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
  2121
  
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
  2122
  function timed_out()
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
  2123
  {
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
  2124
    $md = @stream_get_meta_data($this->sock);
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
  2125
    return ( isset($md['timed_out']) ) ? $md['timed_out'] : false;
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
  2126
  }
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
  2127
  
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
  2128
  function get_peer_info(&$addr, &$port)
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
  2129
  {
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
  2130
    $peer = stream_socket_get_name($this->sock, true);
62
1b73be5346df Fixed a bug in get_peer_info(); accept() now fully works for multiple bound addresses
Dan
parents: 57
diff changeset
  2131
    $addr = substr($peer, 0, strrpos($peer, ':'));
1b73be5346df Fixed a bug in get_peer_info(); accept() now fully works for multiple bound addresses
Dan
parents: 57
diff changeset
  2132
    $port = substr($peer, strrpos($peer, ':') + 1);
37
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
  2133
  }
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
  2134
  
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
  2135
  function write($data)
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
  2136
  {
64
ee64bb096f56 A few miscellaneous fixes including modifying WebServer to write data in chunks (improved performance and reliability on a slow connection)
Dan
parents: 63
diff changeset
  2137
    $data = str_split($data, 8096);
ee64bb096f56 A few miscellaneous fixes including modifying WebServer to write data in chunks (improved performance and reliability on a slow connection)
Dan
parents: 63
diff changeset
  2138
    foreach ( $data as $chunk )
ee64bb096f56 A few miscellaneous fixes including modifying WebServer to write data in chunks (improved performance and reliability on a slow connection)
Dan
parents: 63
diff changeset
  2139
    {
ee64bb096f56 A few miscellaneous fixes including modifying WebServer to write data in chunks (improved performance and reliability on a slow connection)
Dan
parents: 63
diff changeset
  2140
      while ( !@fwrite($this->sock, $chunk) )
ee64bb096f56 A few miscellaneous fixes including modifying WebServer to write data in chunks (improved performance and reliability on a slow connection)
Dan
parents: 63
diff changeset
  2141
      {
ee64bb096f56 A few miscellaneous fixes including modifying WebServer to write data in chunks (improved performance and reliability on a slow connection)
Dan
parents: 63
diff changeset
  2142
        usleep(50000);
ee64bb096f56 A few miscellaneous fixes including modifying WebServer to write data in chunks (improved performance and reliability on a slow connection)
Dan
parents: 63
diff changeset
  2143
      }
ee64bb096f56 A few miscellaneous fixes including modifying WebServer to write data in chunks (improved performance and reliability on a slow connection)
Dan
parents: 63
diff changeset
  2144
    }
ee64bb096f56 A few miscellaneous fixes including modifying WebServer to write data in chunks (improved performance and reliability on a slow connection)
Dan
parents: 63
diff changeset
  2145
    return true;
37
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
  2146
  }
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
  2147
  
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
  2148
  function is_eof()
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
  2149
  {
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
  2150
    return feof($this->sock);
65e70ada71c9 Major changes to webserver backend. All socket functions are abstracted to allow support for stream_* which seems to be both more widely supported and better at handling blocking and timeouts, at the cost of a small bit of speed. Keep-Alive times out properly and thanks to a bit of IPC code from stream_create_pair(), zombie children are mostly eliminated by proper pcntl_wait() being called when a child shuts down normally, and children die within 0.2sec if the parent receives a SIGTERM or SIGINT, even if the children are waiting on the socket.
Dan
parents: 34
diff changeset
  2151
  }
23
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
  2152
}
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
  2153
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
  2154
/**
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
  2155
 * Exception class that allows breaking directly out of a scriptlet.
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
  2156
 */
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
  2157
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
  2158
class HttpExceptionFatal extends Exception
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
  2159
{
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  2160
}
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  2161
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  2162
/**
24
d275dc8f4203 Added HttpSuccess exception which can be thrown when a scriptlet is done executing normally
Dan
parents: 23
diff changeset
  2163
 * Exception class that will be treated as a scriptlet ending with success.
d275dc8f4203 Added HttpSuccess exception which can be thrown when a scriptlet is done executing normally
Dan
parents: 23
diff changeset
  2164
 */
d275dc8f4203 Added HttpSuccess exception which can be thrown when a scriptlet is done executing normally
Dan
parents: 23
diff changeset
  2165
d275dc8f4203 Added HttpSuccess exception which can be thrown when a scriptlet is done executing normally
Dan
parents: 23
diff changeset
  2166
class HttpSuccess extends Exception
d275dc8f4203 Added HttpSuccess exception which can be thrown when a scriptlet is done executing normally
Dan
parents: 23
diff changeset
  2167
{
d275dc8f4203 Added HttpSuccess exception which can be thrown when a scriptlet is done executing normally
Dan
parents: 23
diff changeset
  2168
}
d275dc8f4203 Added HttpSuccess exception which can be thrown when a scriptlet is done executing normally
Dan
parents: 23
diff changeset
  2169
d275dc8f4203 Added HttpSuccess exception which can be thrown when a scriptlet is done executing normally
Dan
parents: 23
diff changeset
  2170
/**
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  2171
 * Array of known HTTP status/error codes
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  2172
 */
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  2173
 
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  2174
$http_responses = array(
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  2175
    200 => 'OK',
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  2176
    302 => 'Found',
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  2177
    307 => 'Temporary Redirect',
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  2178
    400 => 'Bad Request',
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  2179
    401 => 'Unauthorized',
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  2180
    403 => 'Forbidden',
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  2181
    404 => 'Not Found',
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  2182
    405 => 'Method Not Allowed',
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  2183
    406 => 'Not Acceptable',
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  2184
    500 => 'Internal Server Error',
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  2185
    501 => 'Not Implemented'
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  2186
  );
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  2187
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  2188
/**
24
d275dc8f4203 Added HttpSuccess exception which can be thrown when a scriptlet is done executing normally
Dan
parents: 23
diff changeset
  2189
 * Array of default extension->mimetype mappings
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  2190
 */
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  2191
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  2192
$mime_types = array(
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  2193
    'html' => 'text/html',
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  2194
    'htm'  => 'text/html',
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  2195
    'png'  => 'image/png',
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  2196
    'gif'  => 'image/gif',
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  2197
    'jpeg' => 'image/jpeg',
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  2198
    'jpg'  => 'image/jpeg',
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  2199
    'js'   => 'text/javascript',
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  2200
    'json' => 'text/x-javascript-json',
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  2201
    'css'  => 'text/css',
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  2202
    'php'  => 'application/x-httpd-php'
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  2203
  );
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  2204