Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 285 lines (229 sloc) 6.992 kb
a6bbd8a @erh license headers
erh authored
1 <% /**
2 * Copyright (C) 2008 10gen Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */ %>
16
bc7d90f first webdav - ro works
Eliot Horowitz authored
17 <%
18
b09e4d2 logging and fix
Eliot Horowitz authored
19 var log = log.webdav;
106c63d turned off debugging
Eliot Horowitz authored
20 log.level = log.LEVEL.ERROR;
1cb0951 msft stuff
Eliot Horowitz authored
21
640754c fix
Eliot Horowitz authored
22 core.user.auth();
23 core.content.xml();
f3acf19 webdav working
Eliot Horowitz authored
24
1cb0951 msft stuff
Eliot Horowitz authored
25 var method = request.getMethod();
26
b09e4d2 logging and fix
Eliot Horowitz authored
27 log.debug( "-----" );
28 log.debug( "entering webdav" );
106c63d turned off debugging
Eliot Horowitz authored
29 log.info( request.getRawHeader() );
1cb0951 msft stuff
Eliot Horowitz authored
30
31 if ( method == "OPTIONS" ){
32 // OPTIONS bypases security b/c some clients need it
33 response.setHeader( "Allow" , "OPTIONS,GET,PROPFIND,PUT,PROPPATCH,LOCK,UNLOCK" );
34 response.setHeader( "Dav" , "1,2" );
35 response.setHeader( "MS-Author-Via" , "DAV" );
b09e4d2 logging and fix
Eliot Horowitz authored
36 log.debug( response );
1cb0951 msft stuff
Eliot Horowitz authored
37 return;
38 }
39
40
37977fd cleaning up auth system
Eliot Horowitz authored
41 var user = Auth.getUser( request );
1cb0951 msft stuff
Eliot Horowitz authored
42 if ( ! user ) {
b09e4d2 logging and fix
Eliot Horowitz authored
43 log.debug( "no user" );
1cb0951 msft stuff
Eliot Horowitz authored
44 return Auth.reject( request , response );
45 }
46 if ( ! user.isAdmin() ){
b09e4d2 logging and fix
Eliot Horowitz authored
47 log.debug( "not admin" );
1cb0951 msft stuff
Eliot Horowitz authored
48 return Auth.reject( request , response );
49 }
bc7d90f first webdav - ro works
Eliot Horowitz authored
50
51
792ea82 added webdav support for windows xp
Igor Bernstein authored
52 request.applyServletParams( /(.(?:_|~~.)webdav)(.*)/ , [ "root" , "file" ] );
53
4202569 windows read working
Eliot Horowitz authored
54 var root = request.root;
792ea82 added webdav support for windows xp
Igor Bernstein authored
55 if ( request.file == null){
5bf261c webdav working on os x
Eliot Horowitz authored
56 request.applyServletParams( /(.*)/ , [ "file" ] );
4202569 windows read working
Eliot Horowitz authored
57 root = "";
58 }
5bf261c webdav working on os x
Eliot Horowitz authored
59
b09e4d2 logging and fix
Eliot Horowitz authored
60 log.debug( "file:" + request.file );
bc7d90f first webdav - ro works
Eliot Horowitz authored
61
792ea82 added webdav support for windows xp
Igor Bernstein authored
62 var theFile = openFile( request.file || "/");
bc7d90f first webdav - ro works
Eliot Horowitz authored
63
7c98a94 hid git related files
Igor Bernstein authored
64 if(isRestricted(theFile)) {
65 response.setResponseCode( 403 );
66 return;
67 }
1cb0951 msft stuff
Eliot Horowitz authored
68
69 if ( method == "PROPFIND" ){
5bf261c webdav working on os x
Eliot Horowitz authored
70
71 if ( theFile.exists() ){
72 response.setResponseCode( 207 );
73 response.setHeader( "Content-Type" , "text/xml; charset=iso-8859-1" );
74
75 xmlHeader();
76
77 var ms = davMultiStatus();
78
4202569 windows read working
Eliot Horowitz authored
79 propfind( ms , request.file , theFile, request.getIntHeader( "Depth" , 1 ) );
5bf261c webdav working on os x
Eliot Horowitz authored
80
792ea82 added webdav support for windows xp
Igor Bernstein authored
81 var xmlToPrint = "";
82 function nonbreakingPrinter(str) {
83 xmlToPrint += str;
84 }
85 xml.to(nonbreakingPrinter, null, ms, 0, false);
86 print(xmlToPrint);
87 //xml.to( print , null , ms );
5bf261c webdav working on os x
Eliot Horowitz authored
88 }
89 else {
90 response.setResponseCode( 404 );
91 }
bc7d90f first webdav - ro works
Eliot Horowitz authored
92 }
f3acf19 webdav working
Eliot Horowitz authored
93 else if ( method == "PROPPATCH" ){
94 // TODO
95 }
96 else if ( method == "PROPFIND" ){
97 // TODO
98 }
bc7d90f first webdav - ro works
Eliot Horowitz authored
99 else if ( method == "GET" ){
100 if ( theFile.exists() ){
f3acf19 webdav working
Eliot Horowitz authored
101 if ( theFile.isDirectory() ){
102 }
103 else {
b09e4d2 logging and fix
Eliot Horowitz authored
104 log.debug( "sending " + theFile + " length::" + theFile.length );
f3acf19 webdav working
Eliot Horowitz authored
105 response.sendFile( theFile );
106 }
bc7d90f first webdav - ro works
Eliot Horowitz authored
107 }
108 else
109 response.setResponseCode( 404 );
110 }
5bf261c webdav working on os x
Eliot Horowitz authored
111 else if ( method == "DELETE" ){
112 if ( theFile.exists() ){
113 if ( ! theFile.remove( true ) ){
114 throw "couldn't delete : " + theFile;
115 }
116 }
117 else {
118 response.setResponseCode( 404 );
119 }
120 }
121 else if ( method == "MKCOL" ){
122 if ( theFile.exists() ){
123 response.setResponseCode( 405 );
124 }
125 else {
126 if ( ! theFile.mkdirs() ){
127 throw "can't make directory : " + theFile;
128 }
129 }
130 }
1cb0951 msft stuff
Eliot Horowitz authored
131 else if ( method == "PUT" || method == "POST" ){
5bf261c webdav working on os x
Eliot Horowitz authored
132 var pd = request.getPostData();
b09e4d2 logging and fix
Eliot Horowitz authored
133 log.debug( "PUT : pd=" + ( ! ( ! pd ) ) );
5bf261c webdav working on os x
Eliot Horowitz authored
134 if ( ! pd ){
135 theFile.touch();
136 }
137 else {
138 try {
b09e4d2 logging and fix
Eliot Horowitz authored
139 log.debug( "writing to " + theFile );
5bf261c webdav working on os x
Eliot Horowitz authored
140 pd.writeTo( theFile );
141 }
142 catch ( e ){
143 throw "couldn't do PUT of [" + theFile + "] b/c : " + e;
144 }
145 response.setResponseCode( 201 );
146 }
147 }
148 else if ( method == "MOVE" ){
149 var to = request.getHeader( "Destination" );
150 if ( ! to )
151 throw ( "no to" );
152
b09e4d2 logging and fix
Eliot Horowitz authored
153 log.debug( "a:" + to );
5bf261c webdav working on os x
Eliot Horowitz authored
154 to = to.replace( /^https?:..[^\/]+/ , "" );
b09e4d2 logging and fix
Eliot Horowitz authored
155 log.debug( "b:" + to );
5bf261c webdav working on os x
Eliot Horowitz authored
156 to = to.replace( /\/~~.webdav/ , "" );
b09e4d2 logging and fix
Eliot Horowitz authored
157 log.debug( "c:" + to );
5bf261c webdav working on os x
Eliot Horowitz authored
158
7c98a94 hid git related files
Igor Bernstein authored
159 var toFile = openFile( to );
160
161 if(isRestricted(toFile)) {
162 response.setResponseCode( 403 );
163 return;
164 }
165 theFile.renameTo( toFile );
5bf261c webdav working on os x
Eliot Horowitz authored
166
167 }
f3acf19 webdav working
Eliot Horowitz authored
168 else if ( method == "LOCK" ){
169 xmlHeader();
170
171 var myLock = lock( theFile );
172
173 xml.to( print , null , myLock.obj );
174 response.setHeader( "Lock-Token" , "<" + myLock.token + ">" );
175 }
176 else if ( method == "UNLOCK" ) {
177
5bf261c webdav working on os x
Eliot Horowitz authored
178 }
bc7d90f first webdav - ro works
Eliot Horowitz authored
179 else {
5bf261c webdav working on os x
Eliot Horowitz authored
180 throw "can't handle : " + method;
bc7d90f first webdav - ro works
Eliot Horowitz authored
181 }
182
183 function xmlHeader(){
89355a5 fixing webdav to keep up w. eliot
Igor Bernstein authored
184 response.getJxpWriter().reset();
bc7d90f first webdav - ro works
Eliot Horowitz authored
185 print( "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" );
186 }
187
188 function davMultiStatus(){
189 var o = Array();
190
191 o._name = "D:multistatus";
192 o._props = {};
193 o._props["xmlns:D"] = "DAV:";
194
195 return o;
196 }
197
198 function davResponse(){
199 var o = Array();
200 o._name = "D:response";
201 o._props = {};
202 o._props["xmlns:lp1"] = "DAV:";
203 o._props["xmlns:lp2"] = "http://apache.org/dav/props/";
204 return o;
205 }
206
207
208 function propfind( msg , uri , f , depth ){
209 if ( ! depth )
210 depth = 0;
211
212 if ( ! f || ! f.exists() )
213 return;
214
215 var r = davResponse();
216 msg.push( r );
217
4202569 windows read working
Eliot Horowitz authored
218 r["D:href"] = root + uri;
106c63d turned off debugging
Eliot Horowitz authored
219
bc7d90f first webdav - ro works
Eliot Horowitz authored
220 var ps = Object();
221 r["D:propstat"] = ps;
222
4202569 windows read working
Eliot Horowitz authored
223 var prop = {
224 "lp1:getlastmodified" : f.lastModified().webFormat() ,
225 "lp1:getetag" : f.lastModified().getTime()
226 };
227 ps["D:prop"] = prop;
228
bc7d90f first webdav - ro works
Eliot Horowitz authored
229 if ( f.isDirectory() ){
4202569 windows read working
Eliot Horowitz authored
230 prop["lp1:resourcetype"] = { "D:collection" : null };
231 prop["D:getcontenttype"] = "httpd/unix-directory";
bc7d90f first webdav - ro works
Eliot Horowitz authored
232
233 if ( depth > 0 ){
234 var children = f.listFiles();
4202569 windows read working
Eliot Horowitz authored
235 for ( var i=0; i<children.length; i++ ){
7c98a94 hid git related files
Igor Bernstein authored
236 if(isRestricted(children[i]))
237 continue;
238
4202569 windows read working
Eliot Horowitz authored
239 var nextURI = uri;
240 if ( ! nextURI.endsWith( "/" ) )
241 nextURI += "/";
242 nextURI += children[i].getName();
243 propfind( msg , nextURI , children[i] , depth - 1 );
244 }
bc7d90f first webdav - ro works
Eliot Horowitz authored
245 }
246 }
247 else {
4202569 windows read working
Eliot Horowitz authored
248 prop["lp1:resourcetype"] = null;
249 prop["D:getcontenttype"] = "text/plain";
250
251 prop["lp1:getcontentlength"] = f.length;
252
bc7d90f first webdav - ro works
Eliot Horowitz authored
253 }
254
f3acf19 webdav working
Eliot Horowitz authored
255 prop["D:supportedlock"] = null;
256 prop["D:lockdiscovery"] = null;
4202569 windows read working
Eliot Horowitz authored
257
bc7d90f first webdav - ro works
Eliot Horowitz authored
258 ps["D:status"] = "HTTP/1.1 200 OK";
259
260 }
261
f3acf19 webdav working
Eliot Horowitz authored
262 function lock( file ){
263 var prop = { _name : "D:prop" , _props : { "xmlns:D" : "DAV:" } };
264
265 var myLock = {};
266 prop["D:lockdiscovery"] = { "D:activelock" : myLock };
267
268 myLock["D:locktype"] = { "D:write" : null };
269 myLock["D:lockscope"] = { "D:exclusive" : null };
270 myLock["D:depth"] = { "infinity" : null };
271
272 var token = "opaquelocktoken:" + Math.random();
273 myLock["D:locktoken"] = { "D:href" : token };
274
275 //<ns0:owner xmlns:ns0="DAV:"><ns0:href>erh-x61s\erh</ns0:href></ns0:owner><D:timeout>Second-3600</D:timeout>
276
277 return { obj : prop , token : token };
278 }
279
7c98a94 hid git related files
Igor Bernstein authored
280 function isRestricted(f) {
a9c25a1 unhid .gitignore from webdav
Igor Bernstein authored
281 return ".git" == f.getName();
7c98a94 hid git related files
Igor Bernstein authored
282 }
5bf261c webdav working on os x
Eliot Horowitz authored
283
bc7d90f first webdav - ro works
Eliot Horowitz authored
284 %>
Something went wrong with that request. Please try again.