2525 */
2626namespace OCA \DAV \Connector \Sabre ;
2727
28+ use Sabre \DAV \Exception ;
29+ use Sabre \DAV \Version ;
30+
2831/**
2932 * Class \OCA\DAV\Connector\Sabre\Server
3033 *
@@ -43,4 +46,93 @@ public function __construct($treeOrNode = null) {
4346 self ::$ exposeVersion = false ;
4447 $ this ->enablePropfindDepthInfinity = true ;
4548 }
49+
50+ /**
51+ *
52+ * @return void
53+ */
54+ public function start () {
55+ try {
56+ // If nginx (pre-1.2) is used as a proxy server, and SabreDAV as an
57+ // origin, we must make sure we send back HTTP/1.0 if this was
58+ // requested.
59+ // This is mainly because nginx doesn't support Chunked Transfer
60+ // Encoding, and this forces the webserver SabreDAV is running on,
61+ // to buffer entire responses to calculate Content-Length.
62+ $ this ->httpResponse ->setHTTPVersion ($ this ->httpRequest ->getHTTPVersion ());
63+
64+ // Setting the base url
65+ $ this ->httpRequest ->setBaseUrl ($ this ->getBaseUri ());
66+ $ this ->invokeMethod ($ this ->httpRequest , $ this ->httpResponse );
67+ } catch (\Throwable $ e ) {
68+ if ($ e instanceof \TypeError) {
69+ /*
70+ * The TypeError includes the file path where the error occurred,
71+ * potentially revealing the installation directory.
72+ *
73+ * By re-throwing the exception, we ensure that the
74+ * default exception handler processes it.
75+ */
76+ throw $ e ;
77+ }
78+
79+ try {
80+ $ this ->emit ('exception ' , [$ e ]);
81+ } catch (\Exception $ ignore ) {
82+ }
83+
84+ $ DOM = new \DOMDocument ('1.0 ' , 'utf-8 ' );
85+ $ DOM ->formatOutput = true ;
86+
87+ $ error = $ DOM ->createElementNS ('DAV: ' , 'd:error ' );
88+ $ error ->setAttribute ('xmlns:s ' , self ::NS_SABREDAV );
89+ $ DOM ->appendChild ($ error );
90+
91+ $ h = function ($ v ) {
92+ return htmlspecialchars ((string )$ v , ENT_NOQUOTES , 'UTF-8 ' );
93+ };
94+
95+ if (self ::$ exposeVersion ) {
96+ $ error ->appendChild ($ DOM ->createElement ('s:sabredav-version ' , $ h (Version::VERSION )));
97+ }
98+
99+ $ error ->appendChild ($ DOM ->createElement ('s:exception ' , $ h (get_class ($ e ))));
100+ $ error ->appendChild ($ DOM ->createElement ('s:message ' , $ h ($ e ->getMessage ())));
101+ if ($ this ->debugExceptions ) {
102+ $ error ->appendChild ($ DOM ->createElement ('s:file ' , $ h ($ e ->getFile ())));
103+ $ error ->appendChild ($ DOM ->createElement ('s:line ' , $ h ($ e ->getLine ())));
104+ $ error ->appendChild ($ DOM ->createElement ('s:code ' , $ h ($ e ->getCode ())));
105+ $ error ->appendChild ($ DOM ->createElement ('s:stacktrace ' , $ h ($ e ->getTraceAsString ())));
106+ }
107+
108+ if ($ this ->debugExceptions ) {
109+ $ previous = $ e ;
110+ while ($ previous = $ previous ->getPrevious ()) {
111+ $ xPrevious = $ DOM ->createElement ('s:previous-exception ' );
112+ $ xPrevious ->appendChild ($ DOM ->createElement ('s:exception ' , $ h (get_class ($ previous ))));
113+ $ xPrevious ->appendChild ($ DOM ->createElement ('s:message ' , $ h ($ previous ->getMessage ())));
114+ $ xPrevious ->appendChild ($ DOM ->createElement ('s:file ' , $ h ($ previous ->getFile ())));
115+ $ xPrevious ->appendChild ($ DOM ->createElement ('s:line ' , $ h ($ previous ->getLine ())));
116+ $ xPrevious ->appendChild ($ DOM ->createElement ('s:code ' , $ h ($ previous ->getCode ())));
117+ $ xPrevious ->appendChild ($ DOM ->createElement ('s:stacktrace ' , $ h ($ previous ->getTraceAsString ())));
118+ $ error ->appendChild ($ xPrevious );
119+ }
120+ }
121+
122+ if ($ e instanceof Exception) {
123+ $ httpCode = $ e ->getHTTPCode ();
124+ $ e ->serialize ($ this , $ error );
125+ $ headers = $ e ->getHTTPHeaders ($ this );
126+ } else {
127+ $ httpCode = 500 ;
128+ $ headers = [];
129+ }
130+ $ headers ['Content-Type ' ] = 'application/xml; charset=utf-8 ' ;
131+
132+ $ this ->httpResponse ->setStatus ($ httpCode );
133+ $ this ->httpResponse ->setHeaders ($ headers );
134+ $ this ->httpResponse ->setBody ($ DOM ->saveXML ());
135+ $ this ->sapi ->sendResponse ($ this ->httpResponse );
136+ }
137+ }
46138}
0 commit comments