1
0
Selaa lähdekoodia

Add option to log HTTP

bohwaz 2 vuotta sitten
vanhempi
commit
523d58ec9b
3 muutettua tiedostoa jossa 50 lisäystä ja 2 poistoa
  1. 6 0
      README.md
  2. 23 2
      index.php
  3. 21 0
      server.php

+ 6 - 0
README.md

@@ -46,6 +46,12 @@ PicoDAV accepts a configuration file named `.picodav.ini`.
 
 It should be in the same directory as `index.php`.
 
+It accepts these options:
+
+* `ANONYMOUS_READ` (boolean, see below)
+* `ANONYMOUS_WRITE` (boolean, see below)
+* `HTTP_LOG` (string, set to a file path to log HTTP requests for debug purposes)
+
 ### Users and passwords
 
 By default, the WebDAV server is accessible to everyone.

+ 23 - 2
index.php

@@ -1809,6 +1809,26 @@ namespace PicoDAV
 
 			parent::error($e);
 		}
+
+		protected string $_log = '';
+
+		public function log(string $message, ...$params): void
+		{
+			if (!HTTP_LOG_FILE) {
+				return;
+			}
+
+			$this->_log .= vsprintf($message, $params) . "\n";
+		}
+
+		public function __destruct()
+		{
+			if (!$this->_log) {
+				return;
+			}
+
+			file_put_contents(HTTP_LOG_FILE, $this->_log, \FILE_APPEND);
+		}
 	}
 }
 
@@ -1863,11 +1883,11 @@ RewriteRule ^.*$ /index.php [END]
 		$fp = fopen(__FILE__, 'r');
 
 		if ($relative_uri == '.webdav/webdav.js') {
-			fseek($fp, 50036, SEEK_SET);
+			fseek($fp, 50403, SEEK_SET);
 			echo fread($fp, 27769);
 		}
 		else {
-			fseek($fp, 50036 + 27769, SEEK_SET);
+			fseek($fp, 50403 + 27769, SEEK_SET);
 			echo fread($fp, 7004);
 		}
 
@@ -1882,6 +1902,7 @@ RewriteRule ^.*$ /index.php [END]
 	const DEFAULT_CONFIG = [
 		'ANONYMOUS_READ' => true,
 		'ANONYMOUS_WRITE' => false,
+		'HTTP_LOG_FILE' => null,
 	];
 
 	$config = [];

+ 21 - 0
server.php

@@ -533,6 +533,26 @@ namespace PicoDAV
 
 			parent::error($e);
 		}
+
+		protected string $_log = '';
+
+		public function log(string $message, ...$params): void
+		{
+			if (!HTTP_LOG_FILE) {
+				return;
+			}
+
+			$this->_log .= vsprintf($message, $params) . "\n";
+		}
+
+		public function __destruct()
+		{
+			if (!$this->_log) {
+				return;
+			}
+
+			file_put_contents(HTTP_LOG_FILE, $this->_log, \FILE_APPEND);
+		}
 	}
 }
 
@@ -592,6 +612,7 @@ namespace {
 	const DEFAULT_CONFIG = [
 		'ANONYMOUS_READ' => true,
 		'ANONYMOUS_WRITE' => false,
+		'HTTP_LOG_FILE' => null,
 	];
 
 	$config = [];