-
Notifications
You must be signed in to change notification settings - Fork 115
/
ngx_http_dav_ext_module.c
2181 lines (1621 loc) · 60.3 KB
/
ngx_http_dav_ext_module.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) Roman Arutyunyan
*/
#include <ngx_config.h>
#include <ngx_core.h>
#include <ngx_http.h>
#include <libxml/parser.h>
#define NGX_HTTP_DAV_EXT_OFF 2
#define NGX_HTTP_DAV_EXT_PREALLOCATE 50
#define NGX_HTTP_DAV_EXT_NODE_PROPFIND 0x01
#define NGX_HTTP_DAV_EXT_NODE_PROP 0x02
#define NGX_HTTP_DAV_EXT_NODE_PROPNAME 0x04
#define NGX_HTTP_DAV_EXT_NODE_ALLPROP 0x08
#define NGX_HTTP_DAV_EXT_PROP_DISPLAYNAME 0x01
#define NGX_HTTP_DAV_EXT_PROP_GETCONTENTLENGTH 0x02
#define NGX_HTTP_DAV_EXT_PROP_GETLASTMODIFIED 0x04
#define NGX_HTTP_DAV_EXT_PROP_RESOURCETYPE 0x08
#define NGX_HTTP_DAV_EXT_PROP_LOCKDISCOVERY 0x10
#define NGX_HTTP_DAV_EXT_PROP_SUPPORTEDLOCK 0x20
#define NGX_HTTP_DAV_EXT_PROP_ALL 0x7f
#define NGX_HTTP_DAV_EXT_PROP_NAMES 0x80
typedef struct {
ngx_str_t uri;
ngx_str_t name;
time_t mtime;
off_t size;
time_t lock_expire;
ngx_str_t lock_root;
uint32_t lock_token;
unsigned dir:1;
unsigned lock_supported:1;
unsigned lock_infinite:1;
} ngx_http_dav_ext_entry_t;
typedef struct {
ngx_uint_t nodes;
ngx_uint_t props;
} ngx_http_dav_ext_xml_ctx_t;
typedef struct {
ngx_uint_t methods;
ngx_shm_zone_t *shm_zone;
} ngx_http_dav_ext_loc_conf_t;
typedef struct {
ngx_queue_t queue;
uint32_t token;
time_t expire;
ngx_uint_t infinite; /* unsigned infinite:1; */
size_t len;
u_char data[1];
} ngx_http_dav_ext_node_t;
typedef struct {
ngx_queue_t queue;
} ngx_http_dav_ext_lock_sh_t;
typedef struct {
time_t timeout;
ngx_slab_pool_t *shpool;
ngx_http_dav_ext_lock_sh_t *sh;
} ngx_http_dav_ext_lock_t;
static ngx_int_t ngx_http_dav_ext_precontent_handler(ngx_http_request_t *r);
static ngx_int_t ngx_http_dav_ext_strip_uri(ngx_http_request_t *r,
ngx_str_t *uri);
static ngx_int_t ngx_http_dav_ext_verify_lock(ngx_http_request_t *r,
ngx_str_t *uri, ngx_uint_t delete_lock);
static ngx_http_dav_ext_node_t *ngx_http_dav_ext_lock_lookup(
ngx_http_request_t *r, ngx_http_dav_ext_lock_t *lock, ngx_str_t *uri,
ngx_int_t depth);
static ngx_int_t ngx_http_dav_ext_content_handler(ngx_http_request_t *r);
static void ngx_http_dav_ext_propfind_handler(ngx_http_request_t *r);
static void ngx_http_dav_ext_propfind_xml_start(void *data,
const xmlChar *localname, const xmlChar *prefix, const xmlChar *uri,
int nb_namespaces, const xmlChar **namespaces, int nb_attributes,
int nb_defaulted, const xmlChar **attributes);
static void ngx_http_dav_ext_propfind_xml_end(void *data,
const xmlChar *localname, const xmlChar *prefix, const xmlChar *uri);
static ngx_int_t ngx_http_dav_ext_propfind(ngx_http_request_t *r,
ngx_uint_t props);
static ngx_int_t ngx_http_dav_ext_set_locks(ngx_http_request_t *r,
ngx_http_dav_ext_entry_t *entry);
static ngx_int_t ngx_http_dav_ext_propfind_response(ngx_http_request_t *r,
ngx_array_t *entries, ngx_uint_t props);
static ngx_int_t ngx_http_dav_ext_lock_handler(ngx_http_request_t *r);
static ngx_int_t ngx_http_dav_ext_lock_response(ngx_http_request_t *r,
ngx_uint_t status, time_t timeout, ngx_uint_t depth, uint32_t token);
static ngx_int_t ngx_http_dav_ext_unlock_handler(ngx_http_request_t *r);
static ngx_int_t ngx_http_dav_ext_depth(ngx_http_request_t *r,
ngx_int_t default_depth);
static uint32_t ngx_http_dav_ext_lock_token(ngx_http_request_t *r);
static uint32_t ngx_http_dav_ext_if(ngx_http_request_t *r, ngx_str_t *uri);
static uintptr_t ngx_http_dav_ext_format_propfind(ngx_http_request_t *r,
u_char *dst, ngx_http_dav_ext_entry_t *entry, ngx_uint_t props);
static uintptr_t ngx_http_dav_ext_format_lockdiscovery(ngx_http_request_t *r,
u_char *dst, ngx_http_dav_ext_entry_t *entry);
static uintptr_t ngx_http_dav_ext_format_token(u_char *dst, uint32_t token,
ngx_uint_t brackets);
static ngx_int_t ngx_http_dav_ext_init_zone(ngx_shm_zone_t *shm_zone,
void *data);
static void *ngx_http_dav_ext_create_loc_conf(ngx_conf_t *cf);
static char *ngx_http_dav_ext_merge_loc_conf(ngx_conf_t *cf, void *parent,
void *child);
static char *ngx_http_dav_ext_lock_zone(ngx_conf_t *cf, ngx_command_t *cmd,
void *conf);
static char *ngx_http_dav_ext_lock(ngx_conf_t *cf, ngx_command_t *cmd,
void *conf);
static ngx_int_t ngx_http_dav_ext_init(ngx_conf_t *cf);
static ngx_conf_bitmask_t ngx_http_dav_ext_methods_mask[] = {
{ ngx_string("off"), NGX_HTTP_DAV_EXT_OFF },
{ ngx_string("propfind"), NGX_HTTP_PROPFIND },
{ ngx_string("options"), NGX_HTTP_OPTIONS },
{ ngx_string("lock"), NGX_HTTP_LOCK },
{ ngx_string("unlock"), NGX_HTTP_UNLOCK },
{ ngx_null_string, 0 }
};
static ngx_command_t ngx_http_dav_ext_commands[] = {
{ ngx_string("dav_ext_methods"),
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_1MORE,
ngx_conf_set_bitmask_slot,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_http_dav_ext_loc_conf_t, methods),
&ngx_http_dav_ext_methods_mask },
{ ngx_string("dav_ext_lock_zone"),
NGX_HTTP_MAIN_CONF|NGX_CONF_TAKE12,
ngx_http_dav_ext_lock_zone,
0,
0,
NULL },
{ ngx_string("dav_ext_lock"),
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
ngx_http_dav_ext_lock,
NGX_HTTP_LOC_CONF_OFFSET,
0,
NULL },
ngx_null_command
};
static ngx_http_module_t ngx_http_dav_ext_module_ctx = {
NULL, /* preconfiguration */
ngx_http_dav_ext_init, /* postconfiguration */
NULL, /* create main configuration */
NULL, /* init main configuration */
NULL, /* create server configuration */
NULL, /* merge server configuration */
ngx_http_dav_ext_create_loc_conf, /* create location configuration */
ngx_http_dav_ext_merge_loc_conf, /* merge location configuration */
};
ngx_module_t ngx_http_dav_ext_module = {
NGX_MODULE_V1,
&ngx_http_dav_ext_module_ctx, /* module context */
ngx_http_dav_ext_commands, /* module directives */
NGX_HTTP_MODULE, /* module type */
NULL, /* init master */
NULL, /* init module */
NULL, /* init process */
NULL, /* init thread */
NULL, /* exit thread */
NULL, /* exit process */
NULL, /* exit master */
NGX_MODULE_V1_PADDING
};
static ngx_int_t
ngx_http_dav_ext_precontent_handler(ngx_http_request_t *r)
{
ngx_str_t uri;
ngx_int_t rc;
ngx_uint_t delete_lock;
ngx_table_elt_t *dest;
ngx_http_dav_ext_loc_conf_t *dlcf;
dlcf = ngx_http_get_module_loc_conf(r, ngx_http_dav_ext_module);
if (dlcf->shm_zone == NULL) {
return NGX_DECLINED;
}
if (r->method & (NGX_HTTP_PUT|NGX_HTTP_DELETE|NGX_HTTP_MKCOL|NGX_HTTP_MOVE))
{
delete_lock = (r->method & (NGX_HTTP_DELETE|NGX_HTTP_MOVE)) ? 1 : 0;
rc = ngx_http_dav_ext_verify_lock(r, &r->uri, delete_lock);
if (rc != NGX_OK) {
return rc;
}
}
if (r->method & (NGX_HTTP_MOVE|NGX_HTTP_COPY)) {
dest = r->headers_in.destination;
if (dest == NULL) {
return NGX_DECLINED;
}
uri.data = dest->value.data;
uri.len = dest->value.len;
if (ngx_http_dav_ext_strip_uri(r, &uri) != NGX_OK) {
return NGX_DECLINED;
}
rc = ngx_http_dav_ext_verify_lock(r, &uri, 0);
if (rc != NGX_OK) {
return rc;
}
}
return NGX_DECLINED;
}
static ngx_int_t
ngx_http_dav_ext_strip_uri(ngx_http_request_t *r, ngx_str_t *uri)
{
u_char *p, *last, *host;
size_t len;
if (uri->data[0] == '/') {
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"http dav_ext strip uri:\"%V\" unchanged", uri);
return NGX_OK;
}
len = r->headers_in.server.len;
if (len == 0) {
goto failed;
}
#if (NGX_HTTP_SSL)
if (r->connection->ssl) {
if (ngx_strncmp(uri->data, "https://", sizeof("https://") - 1) != 0) {
goto failed;
}
host = uri->data + sizeof("https://") - 1;
} else
#endif
{
if (ngx_strncmp(uri->data, "http://", sizeof("http://") - 1) != 0) {
goto failed;
}
host = uri->data + sizeof("http://") - 1;
}
if (ngx_strncmp(host, r->headers_in.server.data, len) != 0) {
goto failed;
}
last = uri->data + uri->len;
for (p = host + len; p != last; p++) {
if (*p == '/') {
ngx_log_debug3(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"http dav_ext strip uri \"%V\" \"%*s\"",
uri, last - p, p);
uri->data = p;
uri->len = last - p;
return NGX_OK;
}
}
failed:
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"http dav_ext strip uri \"%V\" failed", uri);
return NGX_DECLINED;
}
static ngx_int_t
ngx_http_dav_ext_verify_lock(ngx_http_request_t *r, ngx_str_t *uri,
ngx_uint_t delete_lock)
{
uint32_t token;
ngx_http_dav_ext_node_t *node;
ngx_http_dav_ext_lock_t *lock;
ngx_http_dav_ext_loc_conf_t *dlcf;
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"http dav_ext verify lock \"%V\"", uri);
token = ngx_http_dav_ext_if(r, uri);
dlcf = ngx_http_get_module_loc_conf(r, ngx_http_dav_ext_module);
lock = dlcf->shm_zone->data;
ngx_shmtx_lock(&lock->shpool->mutex);
node = ngx_http_dav_ext_lock_lookup(r, lock, uri, -1);
if (node == NULL) {
ngx_shmtx_unlock(&lock->shpool->mutex);
return NGX_OK;
}
if (token == 0) {
ngx_shmtx_unlock(&lock->shpool->mutex);
return 423; /* Locked */
}
if (token != node->token) {
ngx_shmtx_unlock(&lock->shpool->mutex);
return NGX_HTTP_PRECONDITION_FAILED;
}
/*
* RFC4918:
* If a request causes the lock-root of any lock to become an
* unmapped URL, then the lock MUST also be deleted by that request.
*/
if (delete_lock && node->len == uri->len) {
ngx_queue_remove(&node->queue);
ngx_slab_free_locked(lock->shpool, node);
}
ngx_shmtx_unlock(&lock->shpool->mutex);
return NGX_OK;
}
static ngx_http_dav_ext_node_t *
ngx_http_dav_ext_lock_lookup(ngx_http_request_t *r,
ngx_http_dav_ext_lock_t *lock, ngx_str_t *uri, ngx_int_t depth)
{
time_t now;
ngx_queue_t *q;
ngx_http_dav_ext_node_t *node;
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"http dav_ext lock lookup \"%V\"", uri);
if (uri->len == 0) {
return NULL;
}
now = ngx_time();
while (!ngx_queue_empty(&lock->sh->queue)) {
q = ngx_queue_head(&lock->sh->queue);
node = (ngx_http_dav_ext_node_t *) q;
if (node->expire >= now) {
break;
}
ngx_queue_remove(q);
ngx_slab_free_locked(lock->shpool, node);
}
for (q = ngx_queue_head(&lock->sh->queue);
q != ngx_queue_sentinel(&lock->sh->queue);
q = ngx_queue_next(q))
{
node = (ngx_http_dav_ext_node_t *) q;
if (uri->len >= node->len) {
if (ngx_memcmp(uri->data, node->data, node->len)) {
continue;
}
if (uri->len > node->len) {
if (node->data[node->len - 1] != '/') {
continue;
}
if (!node->infinite
&& ngx_strlchr(uri->data + node->len,
uri->data + uri->len - 1, '/'))
{
continue;
}
}
ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"http dav_ext lock found \"%*s\"",
node->len, node->data);
return node;
}
/* uri->len < node->len */
if (depth >= 0) {
if (ngx_memcmp(node->data, uri->data, uri->len)) {
continue;
}
if (uri->data[uri->len - 1] != '/') {
continue;
}
if (depth == 0
&& ngx_strlchr(node->data + uri->len,
node->data + node->len - 1, '/'))
{
continue;
}
ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"http dav_ext lock found \"%*s\"",
node->len, node->data);
return node;
}
}
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"http dav_ext lock not found");
return NULL;
}
static ngx_int_t
ngx_http_dav_ext_content_handler(ngx_http_request_t *r)
{
ngx_int_t rc;
ngx_table_elt_t *h;
ngx_http_dav_ext_loc_conf_t *dlcf;
dlcf = ngx_http_get_module_loc_conf(r, ngx_http_dav_ext_module);
if (!(r->method & dlcf->methods)) {
return NGX_DECLINED;
}
switch (r->method) {
case NGX_HTTP_PROPFIND:
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"http dav_ext propfind");
rc = ngx_http_read_client_request_body(r,
ngx_http_dav_ext_propfind_handler);
if (rc >= NGX_HTTP_SPECIAL_RESPONSE) {
return rc;
}
return NGX_DONE;
case NGX_HTTP_OPTIONS:
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"http dav_ext options");
rc = ngx_http_discard_request_body(r);
if (rc != NGX_OK) {
return rc;
}
h = ngx_list_push(&r->headers_out.headers);
if (h == NULL) {
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
ngx_str_set(&h->key, "DAV");
h->value.len = 1;
h->value.data = (u_char *) (dlcf->shm_zone ? "2" : "1");
h->hash = 1;
h = ngx_list_push(&r->headers_out.headers);
if (h == NULL) {
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
/* XXX */
ngx_str_set(&h->key, "Allow");
ngx_str_set(&h->value,
"GET,HEAD,PUT,DELETE,MKCOL,COPY,MOVE,PROPFIND,OPTIONS,LOCK,UNLOCK");
h->hash = 1;
r->headers_out.status = NGX_HTTP_OK;
r->headers_out.content_length_n = 0;
rc = ngx_http_send_header(r);
if (rc == NGX_ERROR || rc > NGX_OK || r->header_only) {
return rc;
}
return ngx_http_send_special(r, NGX_HTTP_LAST);
case NGX_HTTP_LOCK:
if (dlcf->shm_zone == NULL) {
return NGX_HTTP_NOT_ALLOWED;
}
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"http dav_ext lock");
/*
* Body is expected to carry the requested lock type, but
* since we only support write/exclusive locks, we ignore it.
* Ideally we could throw an error if a lock of another type
* is requested, but the amount of work required for that is
* not worth it.
*/
rc = ngx_http_discard_request_body(r);
if (rc != NGX_OK) {
return rc;
}
return ngx_http_dav_ext_lock_handler(r);
case NGX_HTTP_UNLOCK:
if (dlcf->shm_zone == NULL) {
return NGX_HTTP_NOT_ALLOWED;
}
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"http dav_ext unlock");
rc = ngx_http_discard_request_body(r);
if (rc != NGX_OK) {
return rc;
}
return ngx_http_dav_ext_unlock_handler(r);
}
return NGX_DECLINED;
}
static void
ngx_http_dav_ext_propfind_handler(ngx_http_request_t *r)
{
off_t len;
ngx_buf_t *b;
ngx_chain_t *cl;
xmlSAXHandler sax;
xmlParserCtxtPtr pctx;
ngx_http_dav_ext_xml_ctx_t xctx;
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"http dav_ext propfind handler");
ngx_memzero(&xctx, sizeof(ngx_http_dav_ext_xml_ctx_t));
ngx_memzero(&sax, sizeof(xmlSAXHandler));
sax.initialized = XML_SAX2_MAGIC;
sax.startElementNs = ngx_http_dav_ext_propfind_xml_start;
sax.endElementNs = ngx_http_dav_ext_propfind_xml_end;
pctx = xmlCreatePushParserCtxt(&sax, &xctx, NULL, 0, NULL);
if (pctx == NULL) {
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
"xmlCreatePushParserCtxt() failed");
ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
return;
}
len = 0;
for (cl = r->request_body->bufs; cl; cl = cl->next) {
b = cl->buf;
if (b->in_file) {
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
"PROPFIND client body is in file, "
"you may want to increase client_body_buffer_size");
xmlFreeParserCtxt(pctx);
ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
return;
}
if (ngx_buf_special(b)) {
continue;
}
len += b->last - b->pos;
if (xmlParseChunk(pctx, (const char *) b->pos, b->last - b->pos,
b->last_buf))
{
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
"xmlParseChunk() failed");
xmlFreeParserCtxt(pctx);
ngx_http_finalize_request(r, NGX_HTTP_BAD_REQUEST);
return;
}
}
xmlFreeParserCtxt(pctx);
if (len == 0) {
/*
* For easier debugging treat bodiless requests
* as if they expect all properties.
*/
xctx.props = NGX_HTTP_DAV_EXT_PROP_ALL;
}
ngx_http_finalize_request(r, ngx_http_dav_ext_propfind(r, xctx.props));
}
static void
ngx_http_dav_ext_propfind_xml_start(void *data, const xmlChar *localname,
const xmlChar *prefix, const xmlChar *uri, int nb_namespaces,
const xmlChar **namespaces, int nb_attributes, int nb_defaulted,
const xmlChar **attributes)
{
ngx_http_dav_ext_xml_ctx_t *xctx = data;
if (ngx_strcmp(localname, "propfind") == 0) {
xctx->nodes ^= NGX_HTTP_DAV_EXT_NODE_PROPFIND;
}
if (ngx_strcmp(localname, "prop") == 0) {
xctx->nodes ^= NGX_HTTP_DAV_EXT_NODE_PROP;
}
if (ngx_strcmp(localname, "propname") == 0) {
xctx->nodes ^= NGX_HTTP_DAV_EXT_NODE_PROPNAME;
}
if (ngx_strcmp(localname, "allprop") == 0) {
xctx->nodes ^= NGX_HTTP_DAV_EXT_NODE_ALLPROP;
}
}
static void
ngx_http_dav_ext_propfind_xml_end(void *data, const xmlChar *localname,
const xmlChar *prefix, const xmlChar *uri)
{
ngx_http_dav_ext_xml_ctx_t *xctx = data;
if (xctx->nodes & NGX_HTTP_DAV_EXT_NODE_PROPFIND) {
if (xctx->nodes & NGX_HTTP_DAV_EXT_NODE_PROP) {
if (ngx_strcmp(localname, "displayname") == 0) {
xctx->props |= NGX_HTTP_DAV_EXT_PROP_DISPLAYNAME;
}
if (ngx_strcmp(localname, "getcontentlength") == 0) {
xctx->props |= NGX_HTTP_DAV_EXT_PROP_GETCONTENTLENGTH;
}
if (ngx_strcmp(localname, "getlastmodified") == 0) {
xctx->props |= NGX_HTTP_DAV_EXT_PROP_GETLASTMODIFIED;
}
if (ngx_strcmp(localname, "resourcetype") == 0) {
xctx->props |= NGX_HTTP_DAV_EXT_PROP_RESOURCETYPE;
}
if (ngx_strcmp(localname, "lockdiscovery") == 0) {
xctx->props |= NGX_HTTP_DAV_EXT_PROP_LOCKDISCOVERY;
}
if (ngx_strcmp(localname, "supportedlock") == 0) {
xctx->props |= NGX_HTTP_DAV_EXT_PROP_SUPPORTEDLOCK;
}
}
if (xctx->nodes & NGX_HTTP_DAV_EXT_NODE_PROPNAME) {
xctx->props |= NGX_HTTP_DAV_EXT_PROP_NAMES;
}
if (xctx->nodes & NGX_HTTP_DAV_EXT_NODE_ALLPROP) {
xctx->props = NGX_HTTP_DAV_EXT_PROP_ALL;
}
}
ngx_http_dav_ext_propfind_xml_start(data, localname, prefix, uri,
0, NULL, 0, 0, NULL);
}
static ngx_int_t
ngx_http_dav_ext_propfind(ngx_http_request_t *r, ngx_uint_t props)
{
size_t root, allocated;
u_char *p, *last, *filename;
ngx_int_t rc;
ngx_err_t err;
ngx_str_t path, name;
ngx_dir_t dir;
ngx_uint_t depth;
ngx_array_t entries;
ngx_file_info_t fi;
ngx_http_dav_ext_entry_t *entry;
if (ngx_array_init(&entries, r->pool, 40, sizeof(ngx_http_dav_ext_entry_t))
!= NGX_OK)
{
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
rc = ngx_http_dav_ext_depth(r, 0);
if (rc == NGX_ERROR) {
return NGX_HTTP_BAD_REQUEST;
}
if (rc == NGX_MAX_INT_T_VALUE) {
/*
* RFC4918:
* 403 Forbidden - A server MAY reject PROPFIND requests on
* collections with depth header of "Infinity", in which case
* it SHOULD use this error with the precondition code
* 'propfind-finite-depth' inside the error body.
*/
return NGX_HTTP_FORBIDDEN;
}
depth = rc;
last = ngx_http_map_uri_to_path(r, &path, &root,
NGX_HTTP_DAV_EXT_PREALLOCATE);
if (last == NULL) {
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
allocated = path.len;
path.len = last - path.data;
if (path.len > 1 && path.data[path.len - 1] == '/') {
path.len--;
} else {
last++;
}
path.data[path.len] = '\0';
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"http dav_ext propfind path: \"%s\"", path.data);
if (ngx_file_info(path.data, &fi) == NGX_FILE_ERROR) {
return NGX_HTTP_NOT_FOUND;
}
if (r->uri.len < 2) {
name = r->uri;
} else {
name.data = &r->uri.data[r->uri.len - 1];
name.len = (name.data[0] == '/') ? 0 : 1;
while (name.data != r->uri.data) {
p = name.data - 1;
if (*p == '/') {
break;
}
name.data--;
name.len++;
}
}
entry = ngx_array_push(&entries);
if (entry == NULL) {
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
ngx_memzero(entry, sizeof(ngx_http_dav_ext_entry_t));
entry->uri = r->uri;
entry->name = name;
entry->dir = ngx_is_dir(&fi);
entry->mtime = ngx_file_mtime(&fi);
entry->size = ngx_file_size(&fi);
if (ngx_http_dav_ext_set_locks(r, entry) != NGX_OK) {
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"http dav_ext propfind name:\"%V\", uri:\"%V\"",
&entry->name, &entry->uri);
if (depth == 0 || !entry->dir) {
return ngx_http_dav_ext_propfind_response(r, &entries, props);
}
if (ngx_open_dir(&path, &dir) == NGX_ERROR) {
ngx_log_error(NGX_LOG_CRIT, r->connection->log, ngx_errno,
ngx_open_dir_n " \"%s\" failed", path.data);
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
rc = NGX_OK;
filename = path.data;
filename[path.len] = '/';
for ( ;; ) {
ngx_set_errno(0);
if (ngx_read_dir(&dir) == NGX_ERROR) {
err = ngx_errno;
if (err != NGX_ENOMOREFILES) {
ngx_log_error(NGX_LOG_CRIT, r->connection->log, err,
ngx_read_dir_n " \"%V\" failed", &path);
rc = NGX_HTTP_INTERNAL_SERVER_ERROR;
}
break;
}
name.len = ngx_de_namelen(&dir);
name.data = ngx_de_name(&dir);
if (name.data[0] == '.') {
continue;
}
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"http dav_ext propfind child path: \"%s\"", name.data);
entry = ngx_array_push(&entries);
if (entry == NULL) {
rc = NGX_HTTP_INTERNAL_SERVER_ERROR;
break;
}
ngx_memzero(entry, sizeof(ngx_http_dav_ext_entry_t));
if (!dir.valid_info) {
if (path.len + 1 + name.len + 1 > allocated) {
allocated = path.len + 1 + name.len + 1
+ NGX_HTTP_DAV_EXT_PREALLOCATE;
filename = ngx_pnalloc(r->pool, allocated);
if (filename == NULL) {
rc = NGX_HTTP_INTERNAL_SERVER_ERROR;
break;
}
last = ngx_cpystrn(filename, path.data, path.len + 1);
*last++ = '/';
}
ngx_cpystrn(last, name.data, name.len + 1);
if (ngx_de_info(filename, &dir) == NGX_FILE_ERROR) {
ngx_log_error(NGX_LOG_CRIT, r->connection->log, ngx_errno,
ngx_de_info_n " \"%s\" failed", filename);
rc = NGX_HTTP_INTERNAL_SERVER_ERROR;
break;
}
}
p = ngx_pnalloc(r->pool, name.len);
if (p == NULL) {
rc = NGX_HTTP_INTERNAL_SERVER_ERROR;
break;
}
ngx_memcpy(p, name.data, name.len);
entry->name.data = p;
entry->name.len = name.len;
p = ngx_pnalloc(r->pool, r->uri.len + 1 + name.len + 1);
if (p == NULL) {
rc = NGX_HTTP_INTERNAL_SERVER_ERROR;
break;
}
entry->uri.data = p;
p = ngx_cpymem(p, r->uri.data, r->uri.len);
if (r->uri.len && r->uri.data[r->uri.len - 1] != '/') {
*p++ = '/';
}
p = ngx_cpymem(p, name.data, name.len);
if (ngx_de_is_dir(&dir)) {
*p++ = '/';
}
entry->uri.len = p - entry->uri.data;
entry->dir = ngx_de_is_dir(&dir);
entry->mtime = ngx_de_mtime(&dir);
entry->size = ngx_de_size(&dir);
if (ngx_http_dav_ext_set_locks(r, entry) != NGX_OK) {
rc = NGX_HTTP_INTERNAL_SERVER_ERROR;
break;
}
ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"http dav_ext propfind child name:\"%V\", uri:\"%V\"",
&entry->name, &entry->uri);
}
if (ngx_close_dir(&dir) == NGX_ERROR) {
ngx_log_error(NGX_LOG_ALERT, r->connection->log, ngx_errno,
ngx_close_dir_n " \"%V\" failed", &path);
}
if (rc != NGX_OK) {
return rc;
}
return ngx_http_dav_ext_propfind_response(r, &entries, props);
}
static ngx_int_t
ngx_http_dav_ext_set_locks(ngx_http_request_t *r,
ngx_http_dav_ext_entry_t *entry)
{
ngx_http_dav_ext_node_t *node;
ngx_http_dav_ext_lock_t *lock;
ngx_http_dav_ext_loc_conf_t *dlcf;
dlcf = ngx_http_get_module_loc_conf(r, ngx_http_dav_ext_module);
if (dlcf->shm_zone == NULL) {
entry->lock_supported = 0;
return NGX_OK;
}
entry->lock_supported = 1;
lock = dlcf->shm_zone->data;
ngx_shmtx_lock(&lock->shpool->mutex);
node = ngx_http_dav_ext_lock_lookup(r, lock, &entry->uri, -1);
if (node == NULL) {
ngx_shmtx_unlock(&lock->shpool->mutex);
return NGX_OK;
}
entry->lock_infinite = node->infinite ? 1 : 0;
entry->lock_expire = node->expire;
entry->lock_token = node->token;
entry->lock_root.data = ngx_pnalloc(r->pool, node->len);
if (entry->lock_root.data == NULL) {
ngx_shmtx_unlock(&lock->shpool->mutex);
return NGX_ERROR;
}
ngx_memcpy(entry->lock_root.data, node->data, node->len);
entry->lock_root.len = node->len;