~gz/ubuntu/wily/steam/new_rel_udev_rules

« back to all changes in this revision

Viewing changes to server/net/webdav.pike

  • Committer: Package Import Robot
  • Author(s): Felix Geyer
  • Date: 2013-10-29 19:51:18 UTC
  • mfrom: (1.1.4) (0.1.4 trusty-proposed)
  • Revision ID: package-import@ubuntu.com-20131029195118-b9bxciz5hwx5z459
Tags: 1:1.0.0.39-2ubuntu1
Add an epoch to the version number as there was an unrelated steam package
in the archive with a higher version number.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* Copyright (C) 2000-2006  Thomas Bopp, Thorsten Hampel, Ludger Merkens
2
 
 *
3
 
 *  This program is free software; you can redistribute it and/or modify
4
 
 *  it under the terms of the GNU General Public License as published by
5
 
 *  the Free Software Foundation; either version 2 of the License, or
6
 
 *  (at your option) any later version.
7
 
 *
8
 
 *  This program is distributed in the hope that it will be useful,
9
 
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
10
 
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
 
 *  GNU General Public License for more details.
12
 
 *
13
 
 *  You should have received a copy of the GNU General Public License
14
 
 *  along with this program; if not, write to the Free Software
15
 
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16
 
 * 
17
 
 * $Id: webdav.pike,v 1.3 2006/05/17 20:41:49 exodusd Exp $
18
 
 */
19
 
 
20
 
constant cvs_version="$Id: webdav.pike,v 1.3 2006/05/17 20:41:49 exodusd Exp $";
21
 
 
22
 
inherit "http";
23
 
 
24
 
import webdavlib;
25
 
 
26
 
#include <macros.h>
27
 
#include <classes.h>
28
 
#include <attributes.h>
29
 
#include <config.h>
30
 
#include <database.h>
31
 
 
32
 
//#define WEBDAV_DEBUG
33
 
 
34
 
#ifdef WEBDAV_DEBUG
35
 
#define DAV_WERR(s, args...) werror(s+"\n", args)
36
 
#else
37
 
#define DAV_WERR(s, args...)
38
 
#endif
39
 
 
40
 
import httplib;
41
 
 
42
 
 
43
 
class steamDAV {
44
 
  inherit WebdavHandler;
45
 
 
46
 
  object _dav;
47
 
  object __fp;
48
 
 
49
 
  mapping null_ressources = ([ ]);
50
 
 
51
 
  void create(object dav, object fp) {
52
 
    _dav = dav;
53
 
    __fp = fp;
54
 
  }
55
 
  string url_name(string fname) {
56
 
      return replace_uml(fname);
57
 
  }
58
 
 
59
 
  static string generate_token(object ctx) {
60
 
    int id = 0;
61
 
    
62
 
    if ( objectp(ctx) ) 
63
 
      id = ctx->get_object_id();
64
 
    
65
 
    string token = sprintf("%08x", random(time()));
66
 
    string ttoken = sprintf("%08x", time())  + sprintf("%08x", random(time())) + sprintf("%08x", random(time()));
67
 
    
68
 
    return "opaquelocktoken:" + token + "-" + 
69
 
      ttoken[0..3] + "-" + ttoken[4..7] + "-" + ttoken[8..11] + "-" +
70
 
      ttoken[12..];
71
 
  }
72
 
 
73
 
  string lock(mixed ctx, string fname, mapping lock_data) {
74
 
    string token;
75
 
 
76
 
    if ( !lock_data->token ) {
77
 
        token = generate_token(ctx);
78
 
        lock_data->token = token;
79
 
    }
80
 
    else
81
 
        token = lock_data->token;
82
 
    
83
 
    if ( objectp(ctx) ) {
84
 
        mapping ldata = ctx->query_attribute(OBJ_LOCK) || ([ ]);
85
 
        ldata[token] = lock_data;
86
 
        ctx->set_attribute(OBJ_LOCK, ldata);
87
 
    }
88
 
    else {
89
 
        mapping ldata = null_ressources[fname];
90
 
        ldata[token] = lock_data;
91
 
        null_ressources[fname] = ldata | ([ "isnull": 1, ]) ;
92
 
    }
93
 
    return lock_data->token;
94
 
  }
95
 
  void unlock(mixed ctx, string fname, void|string token)
96
 
  {
97
 
      if ( !stringp(token) )
98
 
          ctx->set_attribute(OBJ_LOCK, 0);
99
 
      else {
100
 
          mapping ldata = ctx->query_attribute(OBJ_LOCK);
101
 
          if ( mappingp(ldata) ) {
102
 
              m_delete(ldata, token);
103
 
              ctx->set_attribute(OBJ_LOCK, ldata);
104
 
          }
105
 
      }
106
 
  }
107
 
  mapping get_locks(mixed ctx, string fname) {
108
 
      if ( !objectp(ctx) )
109
 
          return null_ressources[fname];
110
 
      return ctx->query_attribute(OBJ_LOCK) || ([ ]);
111
 
  }
112
 
  mapping is_locked(mixed ctx, string fname, void|string gottoken) {
113
 
    mapping ldata;
114
 
    if ( !objectp(ctx) ) 
115
 
        ldata =  null_ressources[fname] || ([ ]);
116
 
    else
117
 
        ldata = ctx->query_attribute(OBJ_LOCK) || ([ ]);
118
 
    DAV_WERR("is_locked(%O, %s)", ctx, fname);
119
 
    
120
 
    foreach(indices(ldata), string token) {
121
 
        mapping lockdata = ldata[token];
122
 
        if ( mappingp(lockdata) ) {
123
 
            int timeout = 180;
124
 
            if ( stringp(lockdata["timeout"]) ) {
125
 
                sscanf(lockdata["timeout"], "Second-%d", timeout);
126
 
            }
127
 
            if ( lockdata->locktime > 0 && (time() - lockdata->locktime) < timeout )
128
 
            {
129
 
                DAV_WERR("active lock found...");
130
 
                if ( !stringp(gottoken) || lockdata->token == gottoken )
131
 
                    return lockdata;
132
 
            }
133
 
        }
134
 
    }
135
 
    if ( objectp(ctx) ) {
136
 
        object env = ctx->get_environment();
137
 
        ldata = is_locked(env, "", gottoken);
138
 
        if ( mappingp(ldata) )
139
 
            return ldata;
140
 
    }
141
 
    if ( !stringp(gottoken) ) {
142
 
        if ( objectp(ctx) )
143
 
            ctx->set_attribute(OBJ_LOCK, 0);
144
 
        else
145
 
            null_ressources[fname] = 0;
146
 
    }
147
 
    return 0;
148
 
  }
149
 
  string get_user_href() {
150
 
    return _Server->get_server_name() + "/~" + this_user()->get_user_name();
151
 
  }
152
 
 
153
 
  string get_etag(mixed ctx) {
154
 
    return ctx->get_etag();
155
 
  }
156
 
 
157
 
  object get_object_id() { return _dav->get_object_id(); }
158
 
  object this() { return _dav->get_user_object(); }
159
 
}
160
 
 
161
 
static object __webdavHandler;
162
 
 
163
 
int check_lock(object obj, mapping vars, void|string fname) 
164
 
{
165
 
    if ( !objectp(obj) && !stringp(fname) )
166
 
        return 1;
167
 
    if ( !stringp(fname) )
168
 
        fname = "";
169
 
    string token = get_opaquelocktoken(__request->request_headers->if);
170
 
    if ( stringp(token) ) {
171
 
        if ( mappingp(__webdavHandler->is_locked(obj, fname, token)) )
172
 
            return 1;
173
 
    }
174
 
    mapping res = __webdavHandler->is_locked(obj, fname);
175
 
    DAV_WERR("Checking lock (current token=%O) locked=%O", token, res);
176
 
    return res == 0;
177
 
}
178
 
 
179
 
mapping handle_OPTIONS(object obj, mapping variables)
180
 
{       
181
 
    mapping result = ::handle_OPTIONS(obj, variables);
182
 
    result->extra_heads += ([ 
183
 
        "MS-Author-Via": "DAV",
184
 
#ifdef WEBDAV_CLASS2
185
 
        "DAV": "1,2",
186
 
#else
187
 
        "DAV": "1", 
188
 
#endif
189
 
    ]);
190
 
    return result;
191
 
}
192
 
 
193
 
#ifdef WEBDAV_CLASS2
194
 
mapping handle_LOCK(object obj, mapping variables)
195
 
{
196
 
    if ( this_user() == USER("guest") )
197
 
        return response_noaccess(obj, variables);
198
 
    obj = _fp->path_to_object(__request->not_query, 1);
199
 
 
200
 
    return lock(__request->not_query, __request->request_headers, 
201
 
                __request->body_raw,
202
 
                __webdavHandler, obj);
203
 
}
204
 
 
205
 
mapping handle_UNLOCK(object obj, mapping variables)
206
 
{
207
 
    if ( this_user() == USER("guest") )
208
 
        return response_noaccess(obj, variables);
209
 
    obj = _fp->path_to_object(__request->not_query, 1);
210
 
 
211
 
    return unlock(__request->not_query, __request->request_headers,
212
 
                  __request->body_raw,
213
 
                  __webdavHandler, obj);
214
 
}
215
 
#endif
216
 
 
217
 
static bool move_and_rename(object obj, string name)
218
 
{
219
 
    string fname, dname, sname;
220
 
 
221
 
    sname = _Server->get_server_name();
222
 
    sscanf(name, "%*s://" + _Server->get_server_name() + "%s", name);
223
 
 
224
 
    if ( name[-1] == '/' )
225
 
      name = dirname(name);
226
 
 
227
 
    fname = basename(name);
228
 
    dname = dirname(name);
229
 
    object target = _fp->path_to_object(dname);
230
 
    if ( !objectp(target) ) {
231
 
      DAV_WERR("No Target directory found at %s", dname);
232
 
      return false;
233
 
    }
234
 
    if ( strlen(fname) > 0 ) 
235
 
      obj->set_attribute(OBJ_NAME, fname);
236
 
    obj->move(target);
237
 
    return true;
238
 
}
239
 
 
240
 
mapping|void handle_MOVE(object obj, mapping variables)
241
 
{
242
 
    string destination = __request->request_headers->destination;
243
 
    string overwrite   = __request->request_headers->overwrite;
244
 
 
245
 
    if ( !check_lock(obj, variables) ) 
246
 
        return low_answer(423, "Locked");
247
 
 
248
 
    if ( !stringp(overwrite) )
249
 
        overwrite = "T";
250
 
    __request->misc->overwrite = overwrite;
251
 
    __request->misc->destination = resolve_destination(
252
 
        destination,  __request->request_headers->host);
253
 
 
254
 
    int res = 201;
255
 
    // create copy variables before calling filesystem module
256
 
    if ( mappingp(__request->misc->destination) )
257
 
        return __request->misc->destination;
258
 
    else if ( stringp(__request->misc->destination) )
259
 
        __request->misc["new-uri"] = __request->misc->destination;
260
 
    DAV_WERR("Handling move:misc=\n"+sprintf("%O\n", __request->misc));
261
 
    
262
 
    destination = __request->misc["new-uri"];
263
 
    if ( catch(destination = url_to_string(destination)) )
264
 
        FATAL("Failed to convert destination %s", destination);
265
 
    
266
 
    object dest = _fp->path_to_object(destination);
267
 
    if ( objectp(dest) ) {
268
 
      if ( __request->misc->overwrite == "F" ) {
269
 
        DAV_WERR("overwritting failed !");
270
 
        return low_answer(412, "Pre-Condition Failed");
271
 
      }
272
 
      else {
273
 
          if ( !check_lock(dest, variables) )
274
 
              return low_answer(423, "Locked");
275
 
          res = 204;
276
 
          dest->delete();
277
 
      }
278
 
    }    
279
 
    if ( !move_and_rename(obj, destination) ) 
280
 
      return low_answer(409, "conflict");
281
 
    return low_answer(res, "moved");
282
 
}
283
 
 
284
 
mapping|void handle_MKCOL(object obj, mapping variables)
285
 
{
286
 
    if ( strlen(__request->body_raw) > 0 )
287
 
      return low_answer(415, "unsupported type");
288
 
    // todo: read the body ?!
289
 
    mapping result = ::handle_MKDIR(obj, variables);
290
 
    if ( mappingp(result) && (result->error == 200 || !result->error) )
291
 
        return low_answer(201, "Created");
292
 
    return result;
293
 
}
294
 
 
295
 
mapping|void handle_COPY(object obj, mapping variables)
296
 
{
297
 
    string destination = __request->request_headers->destination;
298
 
    string overwrite   = __request->request_headers->overwrite;
299
 
    string dest_host;
300
 
 
301
 
 
302
 
    if ( !stringp(overwrite) )
303
 
        overwrite = "T";
304
 
    __request->misc->overwrite = overwrite;
305
 
    __request->misc->destination = resolve_destination(
306
 
        destination, __request->request_headers->host);
307
 
    if ( mappingp(__request->misc->destination) )
308
 
        return __request->misc->destination;
309
 
 
310
 
    mixed result =  ([ ]); // should now how to copy handle_http();
311
 
    object duplicate;
312
 
 
313
 
    duplicate = _fp->path_to_object(__request->misc->destination);
314
 
 
315
 
    DAV_WERR("Handling COPY:misc=\n"+sprintf("%O\n", __request->misc));
316
 
    DAV_WERR("Found dest resource = %s !",
317
 
             (objectp(duplicate) ? "yes" : "no"));
318
 
    int res = 201;
319
 
    if ( objectp(duplicate) ) {
320
 
      if ( __request->misc->overwrite == "F" ) {
321
 
        DAV_WERR("overwritting failed !");
322
 
        return low_answer(412, "conflict");
323
 
      }
324
 
      else {
325
 
          if ( !check_lock(duplicate, variables) )
326
 
              return low_answer(423, "Locked");
327
 
          res = 204;
328
 
          duplicate->delete();
329
 
      }
330
 
    }    
331
 
    if ( objectp(obj) ) 
332
 
    {
333
 
        if ( obj->get_object_class() & CLASS_CONTAINER )
334
 
          duplicate = obj->duplicate(true);
335
 
        else
336
 
          duplicate= obj->duplicate();
337
 
 
338
 
        // dirname and fname
339
 
        if ( !move_and_rename(duplicate, __request->misc->destination) )
340
 
          return low_answer(409, "conflict");
341
 
        duplicate->set_attribute(OBJ_LOCK, 0);
342
 
        return low_answer(res, "copied");
343
 
    }
344
 
    else {
345
 
        FATAL("Resource could not be found !");
346
 
        return low_answer(404, "not found");
347
 
    }
348
 
}
349
 
 
350
 
mapping|void handle_PROPPATCH(object obj, mapping variables)
351
 
{
352
 
    obj = _fp->path_to_object(__request->not_query, 1);
353
 
 
354
 
    if ( !check_lock(obj, variables) ) 
355
 
        return low_answer(423, "Locked");
356
 
 
357
 
    return proppatch(__request->not_query, __request->request_headers,
358
 
                     __request->body_raw, __webdavHandler, obj);
359
 
}
360
 
 
361
 
mapping handle_DELETE(object obj, mapping vars)
362
 
{
363
 
 
364
 
    if ( !check_lock(obj, vars) ) 
365
 
        return low_answer(423, "Locked");
366
 
    return ::handle_DELETE(obj, vars);
367
 
}
368
 
 
369
 
mapping handle_PUT(object obj, mapping vars)
370
 
{
371
 
    string fname = __request->not_query;
372
 
 
373
 
    obj = _fp->path_to_object(__request->not_query, 1);
374
 
 
375
 
        
376
 
    if ( !check_lock(obj, vars, fname) ) 
377
 
        return low_answer(423, "Locked");
378
 
    
379
 
    mapping result = ::handle_PUT(obj, vars);
380
 
    mixed err = catch {
381
 
        mapping locks = __webdavHandler->get_locks(0, fname);
382
 
        if ( mappingp(locks) && sizeof(locks) > 0 ) {
383
 
            // locked null resources
384
 
            object fp = vars->fp;
385
 
            if ( !objectp(fp) )
386
 
            fp = get_module("filepath:tree");
387
 
            obj = fp->path_to_object(fname);
388
 
            if ( objectp(obj) ) {
389
 
                if ( !mappingp(obj->query_attribute(OBJ_LOCK)) )
390
 
                    obj->set_attribute(OBJ_LOCK, locks);
391
 
            }
392
 
        }
393
 
    };
394
 
    if ( err ) {
395
 
        FATAL("While setting lock for previous null resource: %O", err);
396
 
    }
397
 
    return result;
398
 
}
399
 
 
400
 
mapping|void handle_PROPFIND(object obj, mapping variables)
401
 
{
402
 
    isWebDAV = 1; // heuristics ;-)
403
 
    
404
 
    obj = _fp->path_to_object(__request->not_query, 1);
405
 
    
406
 
    if ( !objectp(obj) )
407
 
        return low_answer(404, "not found");
408
 
    
409
 
    return propfind(__request->not_query, __request->request_headers, 
410
 
                    __request->body_raw, __webdavHandler, obj);
411
 
}    
412
 
 
413
 
mixed get_property(object obj, Property property)
414
 
{
415
 
  if ( !objectp(obj) )
416
 
    return 0;
417
 
  if ( !objectp(property) )
418
 
    error("No property found, null-pointer !");
419
 
  DAV_WERR("Get property %s, val=%O, ns=%O", property->get_name(),obj->query_attribute(property->get_name()), property->describe_namespace());
420
 
  string pname = property->get_ns_name();
421
 
 
422
 
#if 0
423
 
  switch( property->get_name() ) {
424
 
  case "displayname":
425
 
    return obj->query_attribute(OBJ_NAME);
426
 
  case "name":
427
 
    return obj->get_identifier();
428
 
  }
429
 
#endif
430
 
  mixed res = obj->query_attribute(pname);  
431
 
  if ( stringp(res) )
432
 
    return replace(res, ({ "<", ">" }), ({ "&lt;", "&gt;" }));
433
 
  return res;
434
 
}
435
 
 
436
 
int set_property(object obj, Property property, mapping namespaces)
437
 
{
438
 
  string val = property->get_value();
439
 
  string xmlns = property->describe_namespace();
440
 
  DAV_WERR("Set property %s", property->_sprintf());
441
 
 
442
 
  obj->set_attribute(property->get_ns_name(), val);
443
 
  return 1;
444
 
}
445
 
 
446
 
string resolve_redirect(object link)
447
 
{
448
 
  object res = link->get_link_object();
449
 
  return _fp->object_to_filename(res);
450
 
}
451
 
 
452
 
object get_context(object ctx, string f)
453
 
{
454
 
  if ( objectp(ctx) )
455
 
    return ctx->get_object_byname(f);
456
 
  return 0;
457
 
}
458
 
 
459
 
int is_link(object ctx)
460
 
{
461
 
  if ( objectp(ctx) && ctx->get_object_class() & CLASS_LINK ) 
462
 
    return 1;
463
 
  return 0;
464
 
}
465
 
 
466
 
static mapping 
467
 
call_command(string cmd, object obj, mapping variables)
468
 
{
469
 
    mapping result = ([ ]);
470
 
 
471
 
    // overwritten - must not forward requests without trailing /
472
 
    DAV_WERR("RAW: %s", __request->raw);
473
 
    float f = gauge {
474
 
    function call = this_object()["handle_"+cmd];
475
 
    if ( functionp(call) ) {
476
 
        result = call(obj, variables);
477
 
    }
478
 
    else {
479
 
        result->error = 501;
480
 
        result->data = "Not implemented";
481
 
    }
482
 
    if ( mappingp(result) ) {
483
 
      if ( stringp(result->data) && strlen(result->data) > 0 && 
484
 
           !result->encoding && !result->type ) 
485
 
      {
486
 
        result->tags = 0;
487
 
        result->encoding = "utf-8";
488
 
        result->type = "text/xml";
489
 
      }
490
 
    }
491
 
    };
492
 
    
493
 
    DAV_WERR("DAV: %s %s %f seconds", cmd, __request->not_query, f);
494
 
    return result;
495
 
}
496
 
 
497
 
void create(object fp, bool admin_port)
498
 
{
499
 
    ::create(fp, admin_port);
500
 
    __webdavHandler = steamDAV(this_object(), fp);
501
 
    __webdavHandler->get_directory = fp->get_directory;
502
 
    __webdavHandler->stat_file = fp->stat_file;
503
 
    __webdavHandler->set_property = set_property;
504
 
    __webdavHandler->get_property = get_property;
505
 
    __webdavHandler->resolve_redirect = resolve_redirect;
506
 
    __webdavHandler->get_context = get_context;
507
 
    __webdavHandler->is_link = is_link;
508
 
}
509
 
 
510
 
void respond(object req, mapping result)
511
 
{
512
 
    DAV_WERR("Respond: %O", result);
513
 
    if ( stringp(result->data) )
514
 
        result->length = strlen(result->data);
515
 
 
516
 
    ::respond(req, result);
517
 
}
518
 
 
519
 
string get_identifier() 
520
 
521
 
    if ( is_dav() ) 
522
 
        return "webdav"; 
523
 
    else
524
 
        return "http";
525
 
}
526
 
 
527
 
string get_socket_name() { if ( is_dav() ) return "webdav"; else return "http"; }