Browse Source

added DELETE and MKCOL to mod_webdav

git-svn-id: svn://svn.lighttpd.net/lighttpd/branches/lighttpd-merge-1.4.x@584 152afb58-edef-0310-8abb-c4023f1b3aa9
svn/tags/lighttpd-1.4.2
Jan Kneschke 17 years ago
parent
commit
e37805c9b4
  1. 2
      src/connections.c
  2. 241
      src/mod_webdav.c

2
src/connections.c

@ -304,6 +304,8 @@ static int connection_handle_write_prepare(server *srv, connection *con) {
case HTTP_METHOD_POST:
case HTTP_METHOD_HEAD:
case HTTP_METHOD_PUT:
case HTTP_METHOD_MKCOL:
case HTTP_METHOD_DELETE:
break;
case HTTP_METHOD_OPTIONS:
if (con->uri.path->ptr[0] != '*') {

241
src/mod_webdav.c

@ -4,6 +4,8 @@
#include <stdlib.h>
#include <string.h>
#include <dirent.h>
#include <errno.h>
#include <unistd.h>
#include "base.h"
#include "log.h"
@ -27,7 +29,8 @@
/* plugin config for all request/connections */
typedef struct {
unsigned short *enabled;
unsigned short enabled;
unsigned short is_readonly;
} plugin_config;
typedef struct {
@ -86,6 +89,7 @@ SETDEFAULTS_FUNC(mod_webdav_set_defaults) {
config_values_t cv[] = {
{ "webdav.activate", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 0 */
{ "webdav.is-readonly", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 1 */
{ NULL, NULL, T_CONFIG_UNSET, T_CONFIG_SCOPE_UNSET }
};
@ -99,6 +103,7 @@ SETDEFAULTS_FUNC(mod_webdav_set_defaults) {
s = calloc(1, sizeof(plugin_config));
cv[0].destination = &(s->enabled);
cv[1].destination = &(s->is_readonly);
p->config_storage[i] = s;
@ -117,6 +122,7 @@ static int mod_webdav_patch_connection(server *srv, connection *con, plugin_data
plugin_config *s = p->config_storage[0];
PATCH(enabled);
PATCH(is_readonly);
/* skip the first, the global context */
for (i = 1; i < srv->config_context->used; i++) {
@ -132,6 +138,8 @@ static int mod_webdav_patch_connection(server *srv, connection *con, plugin_data
if (buffer_is_equal_string(du->key, CONST_STR_LEN("webdav.activate"))) {
PATCH(enabled);
} else if (buffer_is_equal_string(du->key, CONST_STR_LEN("webdav.is-readonly"))) {
PATCH(is_readonly);
}
}
}
@ -156,7 +164,12 @@ URIHANDLER_FUNC(mod_webdav_uri_handler) {
/* we fake a little bit but it makes MS W2k happy and it let's us mount the volume */
response_header_overwrite(srv, con, CONST_STR_LEN("DAV"), CONST_STR_LEN("1,2"));
response_header_overwrite(srv, con, CONST_STR_LEN("MS-Author-Via"), CONST_STR_LEN("DAV"));
response_header_insert(srv, con, CONST_STR_LEN("Allow"), CONST_STR_LEN("PROPFIND"));
if (p->conf.is_readonly) {
response_header_insert(srv, con, CONST_STR_LEN("Allow"), CONST_STR_LEN("PROPFIND"));
} else {
response_header_insert(srv, con, CONST_STR_LEN("Allow"), CONST_STR_LEN("PROPFIND, DELETE, MKCOL"));
}
break;
default:
break;
@ -169,6 +182,8 @@ URIHANDLER_FUNC(mod_webdav_uri_handler) {
static int get_response_entry(server *srv, connection *con, plugin_data *p, buffer *b, const char *filename) {
struct stat st;
UNUSED(srv);
buffer_append_string(b,"<D:response xmlns:ns0=\"urn:uuid:c2f41010-65b3-11d1-a29f-00aa00c14882/\">\n");
buffer_append_string(b,"<D:href>\n");
@ -239,25 +254,138 @@ static int get_response_entry(server *srv, connection *con, plugin_data *p, buff
return 0;
}
static int webdav_unlink_dir(server *srv, connection *con, buffer *subdir, buffer *b) {
DIR *dir;
buffer *dirname;
int have_multi_status = 0;
dirname = buffer_init();
buffer_copy_string_buffer(dirname, con->physical.path);
BUFFER_APPEND_SLASH(dirname);
if (subdir) buffer_append_string_buffer(dirname, subdir);
if (NULL != (dir = opendir(dirname->ptr))) {
struct dirent *de;
while(NULL != (de = readdir(dir))) {
if (de->d_name[0] == '.' && de->d_name[1] == '\0') {
/* ignore the current dir */
} else if (de->d_name[0] == '.' && de->d_name[1] == '.' && de->d_name[2] == '\0') {
/* ignore the parent dir */
} else {
struct stat st;
buffer *next_subdir = buffer_init();
int status = 0;
if (subdir) {
buffer_copy_string_buffer(next_subdir, subdir);
} else {
buffer_copy_string(next_subdir, "/");
}
BUFFER_APPEND_SLASH(next_subdir);
buffer_append_string(next_subdir, de->d_name);
/* physical name of the resource */
buffer_copy_string_buffer(dirname, con->physical.path);
BUFFER_APPEND_SLASH(dirname);
buffer_append_string_buffer(dirname, next_subdir);
/* stat and unlink afterwards */
if (-1 == stat(dirname->ptr, &st)) {
/* don't about it yet, unlink will fail too */
} else if (S_ISDIR(st.st_mode)) {
have_multi_status = webdav_unlink_dir(srv, con, next_subdir, b);
/* try to unlink it */
if (-1 == rmdir(dirname->ptr)) {
switch(errno) {
case EACCES:
case EPERM:
/* 403 */
status = 403;
break;
default:
status = 501;
break;
}
} else {
status = 0;
}
} else {
/* try to unlink it */
if (-1 == unlink(dirname->ptr)) {
switch(errno) {
case EACCES:
case EPERM:
/* 403 */
status = 403;
break;
default:
status = 501;
break;
}
} else {
status = 0;
}
}
if (status) {
have_multi_status = 1;
buffer_append_string(b,"<D:response xmlns:ns0=\"urn:uuid:c2f41010-65b3-11d1-a29f-00aa00c14882/\">\n");
buffer_append_string(b,"<D:href>\n");
buffer_append_string_buffer(b, con->uri.path);
BUFFER_APPEND_SLASH(b);
buffer_append_string_buffer(b, next_subdir);
buffer_append_string(b,"</D:href>\n");
buffer_append_string(b,"<D:status>\n");
if (con->request.http_version == HTTP_VERSION_1_1) {
BUFFER_COPY_STRING_CONST(b, "HTTP/1.1 ");
} else {
BUFFER_COPY_STRING_CONST(b, "HTTP/1.0 ");
}
buffer_append_long(b, status);
BUFFER_APPEND_STRING_CONST(b, " ");
buffer_append_string(b, get_http_status_name(status));
buffer_append_string(b,"</D:status>\n");
buffer_append_string(b,"</D:response>\n");
}
buffer_free(next_subdir);
}
}
closedir(dir);
}
buffer_free(dirname);
return have_multi_status;
}
URIHANDLER_FUNC(mod_webdav_subrequest_handler) {
plugin_data *p = p_d;
buffer *b;
DIR *dir;
data_string *ds;
int depth = -1;
struct stat st;
UNUSED(srv);
if (!p->conf.enabled) return HANDLER_GO_ON;
/* physical path is setup */
if (con->physical.path->used == 0) return HANDLER_GO_ON;
/* PROPFIND and DELETE need them */
if (NULL != (ds = (data_string *)array_get_element(con->request.headers, "Depth"))) {
depth = strtol(ds->value->ptr, NULL, 10);
}
switch (con->request.http_method) {
case HTTP_METHOD_PROPFIND:
/* they want to know the properties of the directory */
if (NULL != (ds = (data_string *)array_get_element(con->request.headers, "Depth"))) {
depth = strtol(ds->value->ptr, NULL, 10);
}
con->http_status = 207;
@ -265,8 +393,7 @@ URIHANDLER_FUNC(mod_webdav_subrequest_handler) {
b = chunkqueue_get_append_buffer(con->write_queue);
buffer_copy_string(b,
"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
buffer_copy_string(b, "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
buffer_append_string(b,"<D:multistatus xmlns:D=\"DAV:\">\n");
@ -295,6 +422,104 @@ URIHANDLER_FUNC(mod_webdav_subrequest_handler) {
con->file_finished = 1;
return HANDLER_FINISHED;
case HTTP_METHOD_MKCOL:
if (p->conf.is_readonly) break;
if (con->request.content_length != 0) {
/* we don't support MKCOL with a body */
con->http_status = 415;
return HANDLER_FINISHED;
}
/* let's create the directory */
if (-1 == mkdir(con->physical.path->ptr, 0700)) {
switch(errno) {
case EPERM:
con->http_status = 403;
break;
case ENOENT:
case ENOTDIR:
con->http_status = 409;
break;
case EEXIST:
default:
con->http_status = 405; /* not allowed */
break;
}
} else {
con->http_status = 201;
}
return HANDLER_FINISHED;
case HTTP_METHOD_DELETE:
if (p->conf.is_readonly) break;
/* stat and unlink afterwards */
if (-1 == stat(con->physical.path->ptr, &st)) {
/* don't about it yet, unlink will fail too */
switch(errno) {
case ENOENT:
con->http_status = 404;
break;
default:
con->http_status = 403;
break;
}
} else if (S_ISDIR(st.st_mode)) {
buffer *multi_status_resp = buffer_init();
if (webdav_unlink_dir(srv, con, NULL, multi_status_resp)) {
/* we got an error somewhere in between, build a 207 */
response_header_overwrite(srv, con, CONST_STR_LEN("Content-Type"), CONST_STR_LEN("text/xml; charset=\"utf-8\""));
b = chunkqueue_get_append_buffer(con->write_queue);
buffer_copy_string(b, "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
buffer_append_string(b,"<D:multistatus xmlns:D=\"DAV:\">\n");
buffer_append_string_buffer(b, multi_status_resp);
buffer_append_string(b,"</D:multistatus>\n");
con->http_status = 207;
con->file_finished = 1;
} else {
/* everything went fine, remove the directory */
if (-1 == rmdir(con->physical.path->ptr)) {
switch(errno) {
case ENOENT:
con->http_status = 404;
break;
default:
con->http_status = 501;
break;
}
} else {
con->http_status = 204;
}
}
buffer_free(multi_status_resp);
} else if (-1 == unlink(con->physical.path->ptr)) {
switch(errno) {
case EPERM:
con->http_status = 403;
break;
case ENOENT:
con->http_status = 404;
break;
default:
con->http_status = 501;
break;
}
} else {
con->http_status = 204;
}
return HANDLER_FINISHED;
default:
break;

Loading…
Cancel
Save