src/server/safs/service.c

Sat, 19 Jan 2013 20:51:16 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sat, 19 Jan 2013 20:51:16 +0100
changeset 49
1fd94945796e
parent 48
37a512d7b8f6
child 54
3a1d5a52adfc
permissions
-rw-r--r--

some fixes

3
137197831306 minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1 /*
137197831306 minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
137197831306 minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
3 *
44
3da1f7b6847f added some error messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
4 * Copyright 2013 Olaf Wintermann. All rights reserved.
3
137197831306 minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
5 *
137197831306 minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
6 * Redistribution and use in source and binary forms, with or without
137197831306 minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
7 * modification, are permitted provided that the following conditions are met:
137197831306 minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
8 *
137197831306 minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
9 * 1. Redistributions of source code must retain the above copyright
137197831306 minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
10 * notice, this list of conditions and the following disclaimer.
137197831306 minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
11 *
137197831306 minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
12 * 2. Redistributions in binary form must reproduce the above copyright
137197831306 minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
13 * notice, this list of conditions and the following disclaimer in the
137197831306 minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
14 * documentation and/or other materials provided with the distribution.
137197831306 minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
15 *
137197831306 minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
137197831306 minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
137197831306 minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
137197831306 minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
137197831306 minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
137197831306 minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
137197831306 minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
137197831306 minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
137197831306 minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
137197831306 minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
137197831306 minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
26 * POSSIBILITY OF SUCH DAMAGE.
137197831306 minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
27 */
137197831306 minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
28
137197831306 minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
29 #include <stdio.h>
12
34aa8001ea53 Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
30 #include <sys/file.h>
34aa8001ea53 Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
31 #include <sys/stat.h>
3
137197831306 minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
32
137197831306 minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
33 #include "service.h"
14
b8bf95b39952 New source folder layout
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 12
diff changeset
34 #include "../util/io.h"
b8bf95b39952 New source folder layout
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 12
diff changeset
35 #include "../util/pblock.h"
b8bf95b39952 New source folder layout
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 12
diff changeset
36 #include "../daemon/protocol.h"
3
137197831306 minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
37
6
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
38 #include <sys/sendfile.h>
14
b8bf95b39952 New source folder layout
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 12
diff changeset
39 #include "../util/strbuf.h"
6
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
40
44
3da1f7b6847f added some error messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
41 #include <errno.h>
3da1f7b6847f added some error messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
42
6
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
43 // TODO: system sendfile Abstraktionen in neue Datei auslagern
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
44 /*
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
45 ssize_t sys_sendfile(int out_fd, int in_fd, off_t *off, size_t len) {
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
46
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
47 }
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
48 */
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
49 #define sys_sendfile sendfile
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
50
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
51
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
52 /*
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
53 * prepares for servicing a file
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
54 *
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
55 * adds content-length header and starts the response
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
56 *
44
3da1f7b6847f added some error messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
57 * return the file descriptor or -1
6
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
58 */
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
59 int prepare_service_file(Session *sn, Request *rq) {
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
60 char *ppath = pblock_findkeyval(pb_key_ppath, rq->vars);
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
61
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
62 /* open the file */
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
63 int fd = open(ppath, O_RDONLY);
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
64 if(fd < 0) {
48
37a512d7b8f6 fixed some memory leaks
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 47
diff changeset
65 //perror("prepare_service_file: open");
6
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
66
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
67 int status = 500;
44
3da1f7b6847f added some error messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
68 int en = errno;
3da1f7b6847f added some error messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
69 switch(en) {
6
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
70 case EACCES: {
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
71 status = 403;
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
72 break;
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
73 }
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
74 case ENOENT: {
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
75 status = 404;
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
76 break;
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
77 }
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
78 }
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
79 protocol_status(sn, rq, status, NULL);
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
80 return -1;
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
81 }
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
82
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
83 /* get stat */
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
84 struct stat stat;
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
85 if (fstat(fd, &stat) != 0) {
48
37a512d7b8f6 fixed some memory leaks
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 47
diff changeset
86 //perror("prepare_service_file: stat");
6
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
87
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
88 protocol_status(sn, rq, 500, NULL);
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
89 return -1;
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
90 }
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
91
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
92 /* add content-length header*/
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
93 char contentLength[32];
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
94 int len = snprintf(contentLength, 32, "%d", stat.st_size);
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
95
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
96 pblock_kvinsert(pb_key_content_length, contentLength, len, rq->srvhdrs);
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
97
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
98 /* start response */
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
99 protocol_status(sn, rq, 200, NULL);
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
100 http_start_response(sn, rq);
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
101
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
102 return fd;
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
103 }
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
104
10
e3ae779232a9 Added some safs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 8
diff changeset
105 int send_file(pblock *pb, Session *sn, Request *rq) {
6
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
106 int fd = prepare_service_file(sn, rq);
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
107 if(fd < 0) {
44
3da1f7b6847f added some error messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
108 // if an error occurs, prepare_service_file sets the http status code
3da1f7b6847f added some error messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
109 // we can just return REQ_ABORTED
3da1f7b6847f added some error messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
110 return REQ_ABORTED;
6
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
111 }
4
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 3
diff changeset
112
6
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
113 /* send file*/
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
114 SystemIOStream *io = (SystemIOStream*) sn->csd;
3
137197831306 minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
115
6
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
116 off_t fileoffset = 0;
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
117 int len = atoi(pblock_findkeyval(pb_key_content_length, rq->srvhdrs));
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
118 sendfile(io->fd, fd, &fileoffset, len);
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
119
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
120 close(fd);
3
137197831306 minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
121
137197831306 minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
122 return REQ_PROCEED;
137197831306 minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
123 }
10
e3ae779232a9 Added some safs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 8
diff changeset
124
e3ae779232a9 Added some safs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 8
diff changeset
125 int service_hello(pblock *pb, Session *sn, Request *rq) {
20
7b235fa88008 Some fixes for mod_jk
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
126 pblock_removekey(pb_key_content_type, rq->srvhdrs);
7b235fa88008 Some fixes for mod_jk
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
127 pblock_nvinsert("content-type", "text/plain", rq->srvhdrs);
10
e3ae779232a9 Added some safs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 8
diff changeset
128 pblock_nninsert("content-length", 13, rq->srvhdrs);
e3ae779232a9 Added some safs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 8
diff changeset
129 protocol_status(sn, rq, 200, NULL);
e3ae779232a9 Added some safs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 8
diff changeset
130 http_start_response(sn, rq);
e3ae779232a9 Added some safs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 8
diff changeset
131 net_write(sn->csd, "Hello World!\n", 13);
e3ae779232a9 Added some safs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 8
diff changeset
132 return REQ_PROCEED;
e3ae779232a9 Added some safs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 8
diff changeset
133 }
12
34aa8001ea53 Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
134
34aa8001ea53 Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
135 int service_index(pblock *pb, Session *sn, Request *rq) {
47
ce9790523346 server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 44
diff changeset
136 //printf("service_index\n");
12
34aa8001ea53 Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
137
34aa8001ea53 Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
138 char *ppath = pblock_findkeyval(pb_key_ppath, rq->vars);
34aa8001ea53 Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
139 char *uri = pblock_findkeyval(pb_key_uri, rq->reqpb);
34aa8001ea53 Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
140
34aa8001ea53 Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
141 sstr_t r_uri = sstr(uri);
34aa8001ea53 Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
142
34aa8001ea53 Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
143 /* open the file */
34aa8001ea53 Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
144 int fd = open(ppath, O_RDONLY);
34aa8001ea53 Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
145 if(fd < 0) {
48
37a512d7b8f6 fixed some memory leaks
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 47
diff changeset
146 //perror("service_index: open");
12
34aa8001ea53 Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
147
34aa8001ea53 Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
148 int status = 500;
34aa8001ea53 Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
149 switch(errno) {
34aa8001ea53 Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
150 case EACCES: {
34aa8001ea53 Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
151 status = 403;
34aa8001ea53 Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
152 break;
34aa8001ea53 Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
153 }
34aa8001ea53 Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
154 case ENOENT: {
34aa8001ea53 Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
155 status = 404;
34aa8001ea53 Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
156 break;
34aa8001ea53 Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
157 }
34aa8001ea53 Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
158 }
34aa8001ea53 Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
159 protocol_status(sn, rq, status, NULL);
48
37a512d7b8f6 fixed some memory leaks
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 47
diff changeset
160 //printf("REQ_ABORTED\n");
12
34aa8001ea53 Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
161 return REQ_ABORTED;
34aa8001ea53 Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
162 }
34aa8001ea53 Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
163
34aa8001ea53 Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
164 DIR *dir = fdopendir(fd);
34aa8001ea53 Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
165 if(dir == NULL) {
34aa8001ea53 Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
166 protocol_status(sn, rq, 500, NULL);
48
37a512d7b8f6 fixed some memory leaks
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 47
diff changeset
167 //printf("DIR is null\n");
12
34aa8001ea53 Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
168 return REQ_ABORTED;
34aa8001ea53 Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
169 }
34aa8001ea53 Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
170
34aa8001ea53 Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
171 sbuf_t *out = sbuf_new(1024); /* output buffer */
34aa8001ea53 Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
172
34aa8001ea53 Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
173 /* write html header */
34aa8001ea53 Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
174 sbuf_puts(out, "<html>\n<head>\n<title>Index of ");
34aa8001ea53 Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
175 sbuf_puts(out, uri);
34aa8001ea53 Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
176 sbuf_puts(out, "</title>\n</head><body>\n<h1>Index of ");
34aa8001ea53 Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
177 sbuf_puts(out, uri);
34aa8001ea53 Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
178 sbuf_puts(out, "</h1><hr>\n\n");
34aa8001ea53 Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
179
34aa8001ea53 Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
180 struct dirent *f;
34aa8001ea53 Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
181 while((f = readdir(dir)) != NULL) {
34aa8001ea53 Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
182 if(strcmp(f->d_name, ".") == 0 || strcmp(f->d_name, "..") == 0) {
34aa8001ea53 Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
183 continue;
34aa8001ea53 Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
184 }
34aa8001ea53 Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
185
34aa8001ea53 Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
186 sstr_t filename = sstr(f->d_name);
34aa8001ea53 Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
187
34aa8001ea53 Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
188 sbuf_puts(out, "<a href=\"");
34aa8001ea53 Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
189 sbuf_append(out, r_uri);
34aa8001ea53 Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
190 sbuf_append(out, filename);
34aa8001ea53 Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
191 sbuf_puts(out, "\">");
34aa8001ea53 Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
192 sbuf_append(out, filename);
34aa8001ea53 Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
193 sbuf_puts(out, "</a><br>\n");
34aa8001ea53 Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
194 }
34aa8001ea53 Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
195
34aa8001ea53 Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
196 sbuf_puts(out, "\n</body>\n</html>\n");
34aa8001ea53 Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
197
34aa8001ea53 Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
198 /* send stuff to client */
34aa8001ea53 Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
199 pblock_removekey(pb_key_content_type, rq->srvhdrs);
34aa8001ea53 Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
200 pblock_kvinsert(pb_key_content_type, "text/html", 9, rq->srvhdrs);
34aa8001ea53 Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
201 pblock_nninsert("content-length", out->length, rq->srvhdrs);
34aa8001ea53 Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
202 protocol_status(sn, rq, 200, NULL);
34aa8001ea53 Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
203 http_start_response(sn, rq);
34aa8001ea53 Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
204
34aa8001ea53 Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
205 net_write(sn->csd, out->ptr, out->length);
34aa8001ea53 Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
206
34aa8001ea53 Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
207 /* close */
34aa8001ea53 Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
208 closedir(dir);
48
37a512d7b8f6 fixed some memory leaks
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 47
diff changeset
209 sbuf_free(out);
12
34aa8001ea53 Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
210
34aa8001ea53 Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
211 return REQ_PROCEED;
34aa8001ea53 Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
212 }
20
7b235fa88008 Some fixes for mod_jk
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
213
7b235fa88008 Some fixes for mod_jk
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
214 int send_options(pblock *pb, Session *sn, Request *rq) {
7b235fa88008 Some fixes for mod_jk
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
215 char *allow = "HEAD, GET, PUT, DELETE, TRACE, OPTIONS, MOVE, COPY, "
7b235fa88008 Some fixes for mod_jk
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
216 "PROPFIND, PROPPATCH, MKCOL, LOCK, UNLOCK, ACL, REPORT";
7b235fa88008 Some fixes for mod_jk
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
217 char *dav = "1,2,access-control";
7b235fa88008 Some fixes for mod_jk
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
218
7b235fa88008 Some fixes for mod_jk
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
219 pblock_removekey(pb_key_content_type, rq->srvhdrs);
7b235fa88008 Some fixes for mod_jk
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
220 pblock_nvinsert("allow", allow, rq->srvhdrs);
7b235fa88008 Some fixes for mod_jk
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
221 pblock_nvinsert("dav", dav, rq->srvhdrs);
7b235fa88008 Some fixes for mod_jk
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
222 pblock_nninsert("content-length", 0, rq->srvhdrs);
7b235fa88008 Some fixes for mod_jk
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
223 protocol_status(sn, rq, 200, NULL);
7b235fa88008 Some fixes for mod_jk
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
224 http_start_response(sn, rq);
7b235fa88008 Some fixes for mod_jk
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
225
7b235fa88008 Some fixes for mod_jk
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
226 return REQ_PROCEED;
7b235fa88008 Some fixes for mod_jk
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
227 }

mercurial