From 830a806fad625186b513a6b48834bfe84c74f2fd Mon Sep 17 00:00:00 2001 From: sbluhm Date: Sun, 20 Sep 2020 20:18:50 +0200 Subject: [PATCH] Added custom header handler. --- Configuration.md | 13 +++++++++++++ configuration.c | 19 ++++++++++++++++++- configuration.h | 9 +++++++++ package-with/conf.xml | 7 ++++++- webdavd.c | 5 +++++ 5 files changed, 51 insertions(+), 2 deletions(-) diff --git a/Configuration.md b/Configuration.md index b66d79b..18a8814 100644 --- a/Configuration.md +++ b/Configuration.md @@ -177,6 +177,19 @@ Example 80 + +## `` +It is possible to add custom response headers to the outgoing transactions by using `` tags. This can for example be useful if you need to configure Cross Origin Resource Sharing (CORS).The headers are added to all outgoing reponses of the instance. + +The header name needs to be added to the tag attribute `name`. Its corresponding value is included inside the tags. + +Example + + + 'https://www.example.com' + 80 + + ## `` The location to write the error log. If unspecified the error log will be written to the stderr. diff --git a/configuration.c b/configuration.c index abf59d4..63901a3 100644 --- a/configuration.c +++ b/configuration.c @@ -278,7 +278,23 @@ static int configUnprotectOptions(WebdavdConfiguration * config, xmlTextReaderPt config->unprotectOptions = 1; } else { config->unprotectOptions = 0; - } + } + return result; +} + +static int configAddHeader(WebdavdConfiguration * config, xmlTextReaderPtr reader, const char * configFile) { + //cache-control + + // keep track of the number of headers + int index = config->addHeadersCount++; + + // increase the size of the config-->addHeaders memory area + config->addHeaders = reallocSafe(config->addHeaders, sizeof(*config->addHeaders) * config->addHeadersCount); + memset(&config->addHeaders[index], 0, sizeof(config->addHeaders[index])); + + // add the values to the configurations + config->addHeaders[index].name = xmlTextReaderGetAttribute(reader, "name"); + int result = readConfigString(reader, &config->addHeaders[index].value); return result; } @@ -303,6 +319,7 @@ static int compareConfigFunction(const void * a, const void * b) { // This MUST be sorted in aplabetical order (for nodeName). The array is binary-searched. static const ConfigurationFunction configFunctions[] = { { .nodeName = "access-log", .func = &configAccessLog }, // + { .nodeName = "add-header", .func = &configAddHeader }, // { .nodeName = "chroot-path", .func = &configChroot }, // { .nodeName = "error-log", .func = &configErrorLog }, // { .nodeName = "listen", .func = &configListen }, // diff --git a/configuration.h b/configuration.h index 881a02c..1ab72b8 100644 --- a/configuration.h +++ b/configuration.h @@ -16,6 +16,11 @@ typedef struct DaemonConfig { const char * forwardToHost; } DaemonConfig; +typedef struct AddHeaders { + const char * name; + const char * value; +} AddHeaders; + typedef struct SSLConfig { int chainFileCount; const char * keyFile; @@ -55,6 +60,10 @@ typedef struct WebdavdConfiguration { // OPTIONS Requests int unprotectOptions; + // Additional Headers + int addHeadersCount; + AddHeaders * addHeaders; + } WebdavdConfiguration; extern WebdavdConfiguration config; diff --git a/package-with/conf.xml b/package-with/conf.xml index b7bce47..18858c0 100644 --- a/package-with/conf.xml +++ b/package-with/conf.xml @@ -53,7 +53,7 @@ - + + + + diff --git a/webdavd.c b/webdavd.c index e9c288c..35da78a 100644 --- a/webdavd.c +++ b/webdavd.c @@ -1216,6 +1216,11 @@ static Response * createFdResponse(int fd, uint64_t offset, uint64_t size, const addHeader(response, "Expires", "Thu, 19 Nov 1980 00:00:00 GMT"); addHeader(response, "Cache-Control", "no-store, no-cache, must-revalidate, post-check=0, pre-check=0"); addHeader(response, "Pragma", "no-cache"); + // Adding additional headers from the config file + for (int i = 0; i < config.addHeadersCount; i++) { + addHeader(response, config.addHeaders[i].name, config.addHeaders[i].value); + } + return response; }