summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorSergey Poznyakoff <gray@gnu.org>2018-02-13 09:28:20 +0200
committerSergey Poznyakoff <gray@gnu.org>2018-02-13 09:38:37 +0200
commitf4943a7aa280404648ff2862ef3c3f476b939194 (patch)
tree2b736811fa162608c0fd08e3200cf90ec0807d9f /src
parent89aa3e4621f3ab255a59091d3a4d6686fd89516a (diff)
downloadfileserv-f4943a7aa280404648ff2862ef3c3f476b939194.tar.gz
fileserv-f4943a7aa280404648ff2862ef3c3f476b939194.tar.bz2
Minor improvements
* fileserv.c (get_file_name): Rename to get_file_resp; Determine file type and return it in FILE_RESP. Make redirect location absolute.
Diffstat (limited to 'src')
-rw-r--r--src/fileserv.c49
1 files changed, 35 insertions, 14 deletions
diff --git a/src/fileserv.c b/src/fileserv.c
index f25b6ad..9fe6772 100644
--- a/src/fileserv.c
+++ b/src/fileserv.c
@@ -327,6 +327,7 @@ typedef struct file_resp {
char *file_name;
DIRCONFIG *conf;
struct stat st;
+ char const *type;
char *location;
} FILE_RESP;
@@ -347,8 +348,30 @@ file_resp_free(FILE_RESP *resp)
free(resp->location);
}
+static char *
+make_location(char const *proto, char const *host, char const *url)
+{
+ size_t len;
+ char *buf;
+
+ len = strlen(proto) + 3 + strlen(host) + strlen(url) + 1;
+ if (url[0] != '/')
+ len++;
+ buf = malloc(len);
+ if (buf) {
+ strcpy(buf, proto);
+ strcat(buf, "://");
+ strcat(buf, host);
+ if (url[0] != '/')
+ strcat(buf, "/");
+ strcat(buf, url);
+ strcat(buf, "/");
+ }
+ return buf;
+}
+
int
-get_file_name(char const *host, char const *url, FILE_RESP *resp)
+get_file_resp(char const *host, char const *url, FILE_RESP *resp)
{
struct urimap const *map;
char *cf;
@@ -368,7 +391,11 @@ get_file_name(char const *host, char const *url, FILE_RESP *resp)
int res;
if (url[strlen(url) - 1] != '/') {
- resp->location = catfile(url, "");
+ resp->location = make_location("http",
+ host, url);
+ if (!resp->location)
+ return MHD_HTTP_INTERNAL_SERVER_ERROR;
+
return MHD_HTTP_MOVED_PERMANENTLY;
}
@@ -378,13 +405,11 @@ get_file_name(char const *host, char const *url, FILE_RESP *resp)
free(resp->file_name);
resp->file_name = cf;
} else if (resp->conf->listing) {
- //FIXME
char *template = catfile(tmpdir, "idxXXXXXX");
int fd = mkstemp(template);
if (fd == -1) {
error("can't create temporary file name: %s",
strerror(errno));
- /*FIXME: leak */
return MHD_HTTP_INTERNAL_SERVER_ERROR;
}
unlink(template);
@@ -397,6 +422,7 @@ get_file_name(char const *host, char const *url, FILE_RESP *resp)
}
fstat(fd, &resp->st);
resp->fd = fd;
+ resp->type = "text/html";
return MHD_HTTP_OK;
} else
return res;
@@ -427,7 +453,8 @@ get_file_name(char const *host, char const *url, FILE_RESP *resp)
return errno_to_http_code(errno);
}
fstat(resp->fd, &resp->st);
-
+ resp->type = get_file_type(resp->file_name);
+
return MHD_HTTP_OK;
}
@@ -824,7 +851,6 @@ fileserv_handler(void *cls,
FILE_RESP resp;
struct MHD_Response *response;
int ret;
- char const *type;
int status;
if (strcmp(method, MHD_HTTP_METHOD_GET) &&
@@ -837,7 +863,7 @@ fileserv_handler(void *cls,
}
*con_cls = NULL;
- status = get_file_name(host, url, &resp);
+ status = get_file_resp(host, url, &resp);
switch (status) {
case MHD_HTTP_OK:
break;
@@ -852,18 +878,13 @@ fileserv_handler(void *cls,
return http_error(conn, method, url, status, NULL);
}
- if (resp.file_name)
- type = get_file_type(resp.file_name);
- else
- type = NULL;
-
response = MHD_create_response_from_fd64(resp.st.st_size, resp.fd);
if (response) {
resp.fd = -1;
- if (type)
+ if (resp.type)
MHD_add_response_header(response,
MHD_HTTP_HEADER_CONTENT_TYPE,
- type);
+ resp.type);
ret = MHD_queue_response(conn, MHD_HTTP_OK, response);
MHD_destroy_response(response);
http_log(conn, method, url, MHD_HTTP_OK, &resp.st);

Return to:

Send suggestions and report system problems to the System administrator.