forked from pantheon-systems/nginx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnginx-dav-ext-module.patch
859 lines (859 loc) · 21.1 KB
/
nginx-dav-ext-module.patch
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
diff --git contrib/nginx-dav-ext-module/README contrib/nginx-dav-ext-module/README
new file mode 100644
index 0000000..bc727ff
--- /dev/null
+++ contrib/nginx-dav-ext-module/README
@@ -0,0 +1,29 @@
+== nginx-dav-ext-module ==
+
+NGINX WebDAV missing commands support (PROPFIND & OPTIONS)
+
+(c) 2012 Arutyunyan Roman ([email protected])
+
+
+For full WebDAV support in NGINX you need to turn on standard NGINX
+WebDAV module (providing partial WebDAV implementation) as well as
+this module for missing methods
+
+./configure --with-http_dav_module --add-module=<path-to-this-module>
+
+
+
+Requirements:
+
+ libexpat-dev
+
+
+Example config:
+
+ location / {
+
+ dav_methods PUT DELETE MKCOL COPY MOVE;
+ dav_ext_methods PROPFIND OPTIONS;
+
+ root /var/root/;
+ }
diff --git contrib/nginx-dav-ext-module/config contrib/nginx-dav-ext-module/config
new file mode 100644
index 0000000..98b2b7a
--- /dev/null
+++ contrib/nginx-dav-ext-module/config
@@ -0,0 +1,9 @@
+ngx_addon_name="ngx_http_dav_ext_module"
+
+HTTP_MODULES="$HTTP_MODULES \
+ ngx_http_dav_ext_module"
+
+NGX_ADDON_SRCS="$NGX_ADDON_SRCS \
+ $ngx_addon_dir/ngx_http_dav_ext_module.c"
+
+CORE_LIBS="$CORE_LIBS -lexpat"
diff --git contrib/nginx-dav-ext-module/ngx_http_dav_ext_module.c contrib/nginx-dav-ext-module/ngx_http_dav_ext_module.c
new file mode 100644
index 0000000..fbce309
--- /dev/null
+++ contrib/nginx-dav-ext-module/ngx_http_dav_ext_module.c
@@ -0,0 +1,803 @@
+/******************************************************************************
+ Copyright (c) 2012, Roman Arutyunyan ([email protected])
+ All rights reserved.
+
+ Redistribution and use in source and binary forms, with or without modification,
+ are permitted provided that the following conditions are met:
+
+ 1. Redistributions of source code must retain the above copyright notice,
+ this list of conditions and the following disclaimer.
+
+ 2. Redistributions in binary form must reproduce the above copyright notice,
+ this list of conditions and the following disclaimer in the documentation
+ and/or other materials provided with the distribution.
+
+ THIS SOFTWARE IS PROVIDED BY THE AUTHOR ''AS IS'' AND ANY EXPRESS OR IMPLIED
+ WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+ MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
+ SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
+ BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
+ IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
+ OF SUCH DAMAGE.
+ *******************************************************************************/
+
+/*
+
+ NGINX missing WebDAV commands support
+
+ *PROPFIND & OPTIONS*
+
+ */
+
+#include <ngx_config.h>
+#include <ngx_core.h>
+#include <ngx_http.h>
+
+#include <sys/types.h>
+#include <dirent.h>
+#include <time.h>
+
+#include <expat.h>
+
+#define NGX_HTTP_DAV_EXT_OFF 2
+
+typedef struct {
+
+ ngx_uint_t methods;
+
+} ngx_http_dav_ext_loc_conf_t;
+
+static ngx_int_t ngx_http_dav_ext_init(ngx_conf_t *cf);
+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 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_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_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
+};
+
+#define NGX_HTTP_DAV_EXT_NODE_propfind 0x001
+#define NGX_HTTP_DAV_EXT_NODE_prop 0x002
+#define NGX_HTTP_DAV_EXT_NODE_propname 0x004
+#define NGX_HTTP_DAV_EXT_NODE_allprop 0x008
+
+#define NGX_HTTP_DAV_EXT_PROP_creationdate 0x001
+#define NGX_HTTP_DAV_EXT_PROP_displayname 0x002
+#define NGX_HTTP_DAV_EXT_PROP_getcontentlanguage 0x004
+#define NGX_HTTP_DAV_EXT_PROP_getcontentlength 0x008
+#define NGX_HTTP_DAV_EXT_PROP_getcontenttype 0x010
+#define NGX_HTTP_DAV_EXT_PROP_getetag 0x020
+#define NGX_HTTP_DAV_EXT_PROP_getlastmodified 0x040
+#define NGX_HTTP_DAV_EXT_PROP_lockdiscovery 0x080
+#define NGX_HTTP_DAV_EXT_PROP_resourcetype 0x100
+#define NGX_HTTP_DAV_EXT_PROP_source 0x200
+#define NGX_HTTP_DAV_EXT_PROP_supportedlock 0x400
+
+#define NGX_HTTP_DAV_EXT_PROPFIND_SELECTED 1
+#define NGX_HTTP_DAV_EXT_PROPFIND_NAMES 2
+#define NGX_HTTP_DAV_EXT_PROPFIND_ALL 3
+
+typedef struct {
+
+ ngx_uint_t nodes;
+
+ ngx_uint_t props;
+
+ ngx_uint_t propfind;
+
+} ngx_http_dav_ext_ctx_t;
+
+static int ngx_http_dav_ext_xmlcmp(const char *xname, const char *sname) {
+
+ const char *c;
+
+ c = strrchr(xname, ':');
+
+ return strcmp(c ? c + 1 : xname, sname);
+}
+
+static void ngx_http_dav_ext_start_xml_elt(void *user_data,
+ const XML_Char *name, const XML_Char **atts)
+{
+ ngx_http_dav_ext_ctx_t *ctx = user_data;
+
+#define NGX_HTTP_DAV_EXT_SET_NODE(nm) \
+ if (!ngx_http_dav_ext_xmlcmp(name, #nm)) \
+ ctx->nodes ^= NGX_HTTP_DAV_EXT_NODE_##nm
+
+ NGX_HTTP_DAV_EXT_SET_NODE(propfind);
+ NGX_HTTP_DAV_EXT_SET_NODE(prop);
+ NGX_HTTP_DAV_EXT_SET_NODE(propname);
+ NGX_HTTP_DAV_EXT_SET_NODE(allprop);
+
+}
+
+static void ngx_http_dav_ext_end_xml_elt(void *user_data, const XML_Char *name)
+{
+ ngx_http_dav_ext_ctx_t *ctx = user_data;
+
+ if (ctx->nodes & NGX_HTTP_DAV_EXT_NODE_propfind) {
+
+ if (ctx->nodes & NGX_HTTP_DAV_EXT_NODE_prop) {
+
+ ctx->propfind = NGX_HTTP_DAV_EXT_PROPFIND_SELECTED;
+
+#define NGX_HTTP_DAV_EXT_SET_PROP(nm) \
+ if (!ngx_http_dav_ext_xmlcmp(name, #nm)) \
+ ctx->props |= NGX_HTTP_DAV_EXT_PROP_##nm
+
+ NGX_HTTP_DAV_EXT_SET_PROP(creationdate);
+ NGX_HTTP_DAV_EXT_SET_PROP(displayname);
+ NGX_HTTP_DAV_EXT_SET_PROP(getcontentlanguage);
+ NGX_HTTP_DAV_EXT_SET_PROP(getcontentlength);
+ NGX_HTTP_DAV_EXT_SET_PROP(getcontenttype);
+ NGX_HTTP_DAV_EXT_SET_PROP(getetag);
+ NGX_HTTP_DAV_EXT_SET_PROP(getlastmodified);
+ NGX_HTTP_DAV_EXT_SET_PROP(lockdiscovery);
+ NGX_HTTP_DAV_EXT_SET_PROP(resourcetype);
+ NGX_HTTP_DAV_EXT_SET_PROP(source);
+ NGX_HTTP_DAV_EXT_SET_PROP(supportedlock);
+
+ }
+
+ if (ctx->nodes & NGX_HTTP_DAV_EXT_NODE_propname) {
+
+ ctx->propfind = NGX_HTTP_DAV_EXT_PROPFIND_NAMES;
+
+ }
+
+ if (ctx->nodes & NGX_HTTP_DAV_EXT_NODE_allprop) {
+
+ ctx->propfind = NGX_HTTP_DAV_EXT_PROPFIND_ALL;
+
+ }
+
+ }
+
+ ngx_http_dav_ext_start_xml_elt(user_data, name, NULL);
+}
+
+#define NGX_HTTP_DAV_EXT_COPY 0x01
+#define NGX_HTTP_DAV_EXT_ESCAPE 0x02
+
+static void
+ngx_http_dav_ext_output(ngx_http_request_t *r, ngx_chain_t **ll,
+ ngx_int_t flags, u_char *data, ngx_uint_t len)
+{
+ ngx_chain_t *cl;
+ ngx_buf_t *b;
+
+ if (!len) {
+ return;
+ }
+
+ if (flags & NGX_HTTP_DAV_EXT_ESCAPE) {
+
+ b = ngx_create_temp_buf(r->pool, len + ngx_escape_html(NULL, data, len));
+ b->last = (u_char*)ngx_escape_html(b->pos, data, len);
+
+ } else if (flags & NGX_HTTP_DAV_EXT_COPY) {
+
+ b = ngx_create_temp_buf(r->pool, len);
+ b->last = ngx_cpymem(b->pos, data, len);
+
+ } else {
+
+ b = ngx_calloc_buf(r->pool);
+ b->memory = 1;
+ b->pos = data;
+ b->start = data;
+ b->last = b->pos + len;
+ b->end = b->last;
+ }
+
+ cl = ngx_alloc_chain_link(r->pool);
+ cl->buf = b;
+ cl->next = NULL;
+
+ if (*ll != NULL) {
+ cl->next = (*ll)->next;
+ (*ll)->next = cl;
+ *ll = cl;
+ } else {
+ *ll = cl;
+ cl->next = cl;
+ }
+}
+
+static void
+ngx_http_dav_ext_flush(ngx_http_request_t *r, ngx_chain_t **ll)
+{
+ ngx_chain_t *cl;
+
+ cl = (*ll)->next;
+ (*ll)->next = NULL;
+ ngx_http_output_filter(r, cl);
+ *ll = NULL;
+}
+
+/* output shortcuts
+
+ NB: these shortcuts assume 2 variables exist in current context:
+ r - request ptr
+ ll - chain ptr ptr
+
+ output chains are buffered in circular list & flushed on demand
+*/
+
+/* output buffer copy */
+#define NGX_HTTP_DAV_EXT_OUTCB(data, len) \
+ ngx_http_dav_ext_output(r, ll, NGX_HTTP_DAV_EXT_COPY, (data), (len))
+
+/* output string (no copy) */
+#define NGX_HTTP_DAV_EXT_OUTS(s) \
+ ngx_http_dav_ext_output(r, ll, 0, (s)->data, (s)->len)
+
+/* output escaped string */
+#define NGX_HTTP_DAV_EXT_OUTES(s) \
+ ngx_http_dav_ext_output(r, ll, NGX_HTTP_DAV_EXT_ESCAPE, (s)->data, (s)->len)
+
+/* output literal */
+#define NGX_HTTP_DAV_EXT_OUTL(s) \
+ ngx_http_dav_ext_output(r, ll, 0, (u_char*)(s), sizeof(s) - 1)
+
+static ngx_int_t
+ngx_http_dav_ext_send_propfind_atts(ngx_http_request_t *r,
+ char *path, ngx_str_t *uri, ngx_chain_t **ll, ngx_uint_t props)
+{
+ struct stat st;
+ struct tm stm;
+ u_char buf[256];
+ ngx_str_t name;
+
+ if (stat(path, &st)) {
+
+ ngx_log_error(NGX_LOG_ALERT, r->connection->log, ngx_errno,
+ "dav_ext stat failed on '%s'", path);
+
+ return NGX_HTTP_NOT_FOUND;
+ }
+
+ if (props & NGX_HTTP_DAV_EXT_PROP_creationdate) {
+
+ /* output file ctime (attr change time) as creation time */
+ if (gmtime_r(&st.st_ctime, &stm) == NULL)
+ return NGX_ERROR;
+
+ /* ISO 8601 time format
+ 2012-02-20T16:15:00Z */
+ NGX_HTTP_DAV_EXT_OUTCB(buf, strftime((char*)buf, sizeof(buf),
+ "<D:creationdate>"
+ "%Y-%m-%dT%TZ"
+ "</D:creationdate>\n",
+
+ &stm));
+ }
+
+ if (props & NGX_HTTP_DAV_EXT_PROP_displayname) {
+ NGX_HTTP_DAV_EXT_OUTL(
+ "<D:displayname>"
+ );
+
+ if (uri->len) {
+
+ for(name.data = uri->data + uri->len;
+ name.data >= uri->data + 1 && name.data[-1] != '/';
+ --name.data);
+
+ name.len = uri->data + uri->len - name.data;
+
+ NGX_HTTP_DAV_EXT_OUTES(&name);
+ }
+
+ NGX_HTTP_DAV_EXT_OUTL(
+ "</D:displayname>\n"
+ );
+ }
+
+ if (props & NGX_HTTP_DAV_EXT_PROP_getcontentlanguage) {
+ NGX_HTTP_DAV_EXT_OUTL(
+ "<D:getcontentlanguage/>\n"
+ );
+ }
+
+ if (props & NGX_HTTP_DAV_EXT_PROP_getcontentlength) {
+ NGX_HTTP_DAV_EXT_OUTCB(buf, ngx_snprintf(buf, sizeof(buf),
+ "<D:getcontentlength>"
+ "%O"
+ "</D:getcontentlength>\n",
+
+ st.st_size) - buf);
+ }
+
+ if (props & NGX_HTTP_DAV_EXT_PROP_getcontenttype) {
+ NGX_HTTP_DAV_EXT_OUTL(
+ "<D:getcontenttype/>\n"
+ );
+ }
+
+ if (props & NGX_HTTP_DAV_EXT_PROP_getetag) {
+ NGX_HTTP_DAV_EXT_OUTL(
+ "<D:getetag/>\n"
+ );
+ }
+
+ if (props & NGX_HTTP_DAV_EXT_PROP_getlastmodified) {
+
+ if (gmtime_r(&st.st_mtime, &stm) == NULL)
+ return NGX_ERROR;
+
+ /* RFC 2822 time format */
+ NGX_HTTP_DAV_EXT_OUTCB(buf, strftime((char*)buf, sizeof(buf),
+ "<D:getlastmodified>"
+ "%a, %d %b %Y %T GMT"
+ "</D:getlastmodified>\n",
+
+ &stm));
+ }
+
+ if (props & NGX_HTTP_DAV_EXT_PROP_lockdiscovery) {
+ NGX_HTTP_DAV_EXT_OUTL(
+ "<D:lockdiscovery/>\n"
+ );
+ }
+
+ if (props & NGX_HTTP_DAV_EXT_PROP_resourcetype) {
+ if (S_ISDIR(st.st_mode)) {
+ NGX_HTTP_DAV_EXT_OUTL(
+ "<D:resourcetype>"
+ "<D:collection/>"
+ "</D:resourcetype>\n"
+ );
+ } else {
+ NGX_HTTP_DAV_EXT_OUTL(
+ "<D:resourcetype/>\n"
+ );
+ }
+ }
+
+ if (props & NGX_HTTP_DAV_EXT_PROP_source) {
+ NGX_HTTP_DAV_EXT_OUTL(
+ "<D:source/>\n"
+ );
+ }
+
+ if (props & NGX_HTTP_DAV_EXT_PROP_supportedlock) {
+ NGX_HTTP_DAV_EXT_OUTL(
+ "<D:supportedlock/>\n"
+ );
+ }
+
+ return NGX_OK;
+}
+
+static ngx_int_t
+ngx_http_dav_ext_send_propfind_item(ngx_http_request_t *r,
+ char *path, ngx_str_t *uri)
+{
+ ngx_http_dav_ext_ctx_t *ctx;
+ ngx_chain_t *l = NULL, **ll = &l;
+ u_char vbuf[8];
+ ngx_str_t status_line = ngx_string("200 OK");
+
+ ctx = ngx_http_get_module_ctx(r, ngx_http_dav_ext_module);
+
+ NGX_HTTP_DAV_EXT_OUTL(
+ "<D:response>\n"
+ "<D:href>"
+ );
+
+ NGX_HTTP_DAV_EXT_OUTES(uri);
+
+ NGX_HTTP_DAV_EXT_OUTL(
+ "</D:href>\n"
+ "<D:propstat>\n"
+ "<D:prop>\n"
+ );
+
+ if (ctx->propfind == NGX_HTTP_DAV_EXT_PROPFIND_NAMES) {
+
+ NGX_HTTP_DAV_EXT_OUTL(
+ "<D:creationdate/>\n"
+ "<D:displayname/>\n"
+ "<D:getcontentlanguage/>\n"
+ "<D:getcontentlength/>\n"
+ "<D:getcontenttype/>\n"
+ "<D:getetag/>\n"
+ "<D:getlastmodified/>\n"
+ "<D:lockdiscovery/>\n"
+ "<D:resourcetype/>\n"
+ "<D:source/>\n"
+ "<D:supportedlock/>\n"
+ );
+
+ } else {
+
+ switch (ngx_http_dav_ext_send_propfind_atts(r, path, uri, ll,
+ ctx->propfind == NGX_HTTP_DAV_EXT_PROPFIND_SELECTED ?
+ ctx->props : (ngx_uint_t)-1))
+ {
+ case NGX_HTTP_NOT_FOUND:
+ ngx_str_set(&status_line, "404 Not Found");
+ break;
+
+ case NGX_OK:
+ case NGX_HTTP_OK:
+ break;
+
+ default:
+ ngx_str_set(&status_line, "500 Internal Server Error");
+ }
+ }
+
+ NGX_HTTP_DAV_EXT_OUTL(
+ "</D:prop>\n"
+ "<D:status>HTTP/"
+ );
+
+ NGX_HTTP_DAV_EXT_OUTCB(vbuf, ngx_snprintf(vbuf, sizeof(vbuf), "%d.%d ",
+ r->http_major, r->http_minor) - vbuf);
+
+ NGX_HTTP_DAV_EXT_OUTS(&status_line);
+
+ NGX_HTTP_DAV_EXT_OUTL(
+ "</D:status>\n"
+ "</D:propstat>\n"
+ "</D:response>\n"
+
+ );
+
+ ngx_http_dav_ext_flush(r, ll);
+
+ return NGX_OK;
+}
+
+/* path returned by this function is terminated
+ with a hidden (out-of-len) null */
+static void ngx_http_dav_ext_make_child(ngx_pool_t *pool, ngx_str_t *parent,
+ u_char *child, size_t chlen, ngx_str_t *path)
+{
+ u_char *s;
+
+ path->data = ngx_palloc(pool, parent->len + 2 + chlen);
+ s = path->data;
+ s = ngx_cpymem(s, parent->data, parent->len);
+ if (parent->len > 0 && s[-1] != '/')
+ *s++ = '/';
+ s = ngx_cpymem(s, child, chlen);
+ path->len = s - path->data;
+ *s = 0;
+}
+
+#define DAV_EXT_INFINITY (-1)
+
+static ngx_int_t
+ngx_http_dav_ext_send_propfind(ngx_http_request_t *r)
+{
+ size_t root;
+ ngx_str_t path, spath, suri;
+ ngx_chain_t *l = NULL, **ll = &l;
+ DIR *dir;
+ int depth;
+ struct dirent *de;
+ size_t len;
+ ngx_http_variable_value_t vv;
+ ngx_str_t depth_name = ngx_string("depth");
+ u_char *p;
+
+ if (ngx_http_variable_unknown_header(&vv, &depth_name,
+ &r->headers_in.headers.part, 0) == NGX_OK
+ && vv.valid)
+ {
+ if (vv.len == sizeof("infinity") -1
+ && !ngx_strncasecmp(vv.data, (u_char*)"infinity", vv.len))
+ {
+ depth = DAV_EXT_INFINITY;
+ } else {
+ depth = ngx_atoi(vv.data, vv.len);
+ }
+
+ } else {
+ depth = DAV_EXT_INFINITY;
+ }
+
+ p = ngx_http_map_uri_to_path(r, &path, &root, 0);
+
+ if (p == NULL || !path.len) {
+
+ ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0,
+ "dav_ext error mapping uri to path");
+
+ return NGX_ERROR;
+ }
+
+ path.len = p - path.data;
+ *p = 0;
+
+ ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
+ "http propfind path: \"%V\"", &path);
+
+ NGX_HTTP_DAV_EXT_OUTL(
+ "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n"
+ "<D:multistatus xmlns:D=\"DAV:\">\n"
+ );
+
+ ngx_http_dav_ext_flush(r, ll);
+
+ ngx_http_dav_ext_send_propfind_item(r, (char*)path.data, &r->uri);
+
+ if (depth) {
+
+ /* treat infinite depth as 1 for performance reasons */
+
+ if ((dir = opendir((char*)path.data))) {
+
+ while((de = readdir(dir))) {
+
+ if (!strcmp(de->d_name, ".")
+ || !strcmp(de->d_name, ".."))
+ {
+ continue;
+ }
+
+ len = strlen(de->d_name);
+
+ ngx_http_dav_ext_make_child(r->pool, &path,
+ (u_char*)de->d_name, len, &spath);
+
+ ngx_http_dav_ext_make_child(r->pool, &r->uri,
+ (u_char*)de->d_name, len, &suri);
+
+ ngx_http_dav_ext_send_propfind_item(r, (char*)spath.data, &suri);
+
+ }
+
+ closedir(dir);
+ }
+
+ }
+
+ NGX_HTTP_DAV_EXT_OUTL(
+ "</D:multistatus>\n"
+ );
+
+ if (*ll && (*ll)->buf) {
+ (*ll)->buf->last_buf = 1;
+ }
+
+ ngx_http_dav_ext_flush(r, ll);
+
+ return NGX_OK;
+}
+
+static void
+ngx_http_dav_ext_propfind_handler(ngx_http_request_t *r)
+{
+ ngx_chain_t *c;
+ ngx_buf_t *b;
+ XML_Parser parser;
+ ngx_uint_t status;
+ ngx_http_dav_ext_ctx_t *ctx;
+
+ ctx = ngx_http_get_module_ctx(r, ngx_http_dav_ext_module);
+
+ if (ctx == NULL) {
+ ctx = ngx_pcalloc(r->pool, sizeof(ngx_http_dav_ext_ctx_t));
+ ngx_http_set_ctx(r, ctx, ngx_http_dav_ext_module);
+ }
+
+ c = r->request_body->bufs;
+
+ status = NGX_OK;
+
+ parser = XML_ParserCreate(NULL);
+
+ XML_SetUserData(parser, ctx);
+
+ XML_SetElementHandler(parser,
+ ngx_http_dav_ext_start_xml_elt,
+ ngx_http_dav_ext_end_xml_elt);
+
+ for(; c != NULL && c->buf != NULL && !c->buf->last_buf; c = c->next) {
+
+ b = c ->buf;
+
+ if (!XML_Parse(parser, (const char*)b->pos, b->last - b->pos, b->last_buf)) {
+
+ ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0,
+ "dav_ext propfind XML error");
+
+ status = NGX_ERROR;
+
+ break;
+ }
+
+ }
+
+ XML_ParserFree(parser);
+
+ if (status == NGX_OK) {
+
+ r->headers_out.status = 207;
+
+ ngx_str_set(&r->headers_out.status_line, "207 Multi-Status");
+
+ ngx_http_send_header(r);
+
+ ngx_http_finalize_request(r, ngx_http_dav_ext_send_propfind(r));
+
+ } else {
+
+ r->headers_out.status = NGX_HTTP_INTERNAL_SERVER_ERROR;
+
+ r->header_only = 1;
+
+ r->headers_out.content_length_n = 0;
+
+ ngx_http_finalize_request(r, ngx_http_send_header(r));
+
+ }
+
+}
+
+static ngx_int_t
+ngx_http_dav_ext_handler(ngx_http_request_t *r)
+{
+ ngx_int_t rc;
+ ngx_table_elt_t *h;
+ ngx_http_dav_ext_loc_conf_t *delcf;
+
+ delcf = ngx_http_get_module_loc_conf(r, ngx_http_dav_ext_module);
+
+ if (!(r->method & delcf->methods)) {
+ return NGX_DECLINED;
+ }
+
+ switch (r->method) {
+
+ case NGX_HTTP_PROPFIND:
+
+ ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
+ "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,
+ "dav_ext options");
+
+ h = ngx_list_push(&r->headers_out.headers);
+
+ if (h == NULL) {
+ return NGX_HTTP_INTERNAL_SERVER_ERROR;
+ }
+
+ ngx_str_set(&h->key, "DAV");
+ ngx_str_set(&h->value, "1");
+ h->hash = 1;
+
+ h = ngx_list_push(&r->headers_out.headers);
+
+ if (h == NULL) {
+ return NGX_HTTP_INTERNAL_SERVER_ERROR;
+ }
+
+ /* FIXME: it looks so ugly because I cannot access nginx dav module */
+ ngx_str_set(&h->key, "Allow");
+ ngx_str_set(&h->value, "GET,HEAD,PUT,DELETE,MKCOL,COPY,MOVE,PROPFIND,OPTIONS");
+ h->hash = 1;
+
+ r->headers_out.status = NGX_HTTP_OK;
+ r->header_only = 1;
+ r->headers_out.content_length_n = 0;
+
+ ngx_http_send_header(r);
+
+ return NGX_OK;
+
+ }
+
+ return NGX_DECLINED;
+}
+
+static void *
+ngx_http_dav_ext_create_loc_conf(ngx_conf_t *cf)
+{
+ ngx_http_dav_ext_loc_conf_t *conf;
+
+ conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_dav_ext_loc_conf_t));
+ if (conf == NULL) {
+ return NULL;
+ }
+
+ return conf;
+}
+
+static char *
+ngx_http_dav_ext_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
+{
+ ngx_http_dav_ext_loc_conf_t *prev = parent;
+ ngx_http_dav_ext_loc_conf_t *conf = child;
+
+ ngx_conf_merge_bitmask_value(conf->methods, prev->methods,
+ (NGX_CONF_BITMASK_SET|NGX_HTTP_DAV_EXT_OFF));
+
+ return NGX_CONF_OK;
+}
+
+static ngx_int_t
+ngx_http_dav_ext_init(ngx_conf_t *cf)
+{
+ ngx_http_handler_pt *h;
+ ngx_http_core_main_conf_t *cmcf;
+
+ cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module);
+
+ h = ngx_array_push(&cmcf->phases[NGX_HTTP_CONTENT_PHASE].handlers);
+
+ if (h == NULL) {
+ return NGX_ERROR;
+ }
+
+ *h = ngx_http_dav_ext_handler;
+
+ return NGX_OK;
+}
+