src/server/service.c

Sun, 08 Jan 2012 15:46:47 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sun, 08 Jan 2012 15:46:47 +0100
changeset 13
1fdbf4170ef4
parent 12
34aa8001ea53
permissions
-rw-r--r--

Added webdav code

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 *
137197831306 minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
4 * Copyright 2011 Olaf Wintermann. All rights reserved.
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>
6
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
30 #include <errno.h>
12
34aa8001ea53 Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
31 #include <sys/file.h>
34aa8001ea53 Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
32 #include <sys/stat.h>
3
137197831306 minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
33
137197831306 minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
34 #include "service.h"
137197831306 minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
35 #include "io.h"
137197831306 minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
36 #include "pblock.h"
4
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 3
diff changeset
37 #include "protocol.h"
3
137197831306 minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
38
6
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
39 #include <sys/sendfile.h>
12
34aa8001ea53 Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
40 #include "strbuf.h"
6
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
41
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
42 // TODO: system sendfile Abstraktionen in neue Datei auslagern
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
43 /*
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
44 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
45
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 #define sys_sendfile sendfile
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
49
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 * prepares for servicing a file
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
53 *
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
54 * adds content-length header and starts the response
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
55 *
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
56 * return the file descriptor or an error code
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
57 */
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
58 int prepare_service_file(Session *sn, Request *rq) {
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
59 char *ppath = pblock_findkeyval(pb_key_ppath, rq->vars);
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
60
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
61 /* open the file */
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
62 int fd = open(ppath, O_RDONLY);
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
63 if(fd < 0) {
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
64 perror("prepare_service_file: open");
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
65
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
66 int status = 500;
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
67 switch(errno) {
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
68 case EACCES: {
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
69 status = 403;
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
70 break;
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
71 }
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
72 case ENOENT: {
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
73 status = 404;
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
74 break;
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
75 }
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
76 }
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
77 protocol_status(sn, rq, status, NULL);
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
78 return -1;
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
79 }
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
80
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
81 /* get stat */
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
82 struct stat stat;
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
83 if (fstat(fd, &stat) != 0) {
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
84 perror("prepare_service_file: stat");
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
85
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
86 protocol_status(sn, rq, 500, NULL);
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
87 return -1;
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
88 }
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
89
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
90 /* add content-length header*/
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
91 char contentLength[32];
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
92 int len = snprintf(contentLength, 32, "%d", stat.st_size);
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
93
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
94 pblock_kvinsert(pb_key_content_length, contentLength, len, rq->srvhdrs);
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 /* start response */
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
97 protocol_status(sn, rq, 200, NULL);
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
98 http_start_response(sn, rq);
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
99
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
100 return fd;
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
10
e3ae779232a9 Added some safs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 8
diff changeset
103 int send_file(pblock *pb, Session *sn, Request *rq) {
3
137197831306 minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
104 printf("test_service\n");
137197831306 minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
105
8
f4d56bf9de40 Added request body reader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
106 // request body test begin
f4d56bf9de40 Added request body reader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
107 char *ctval = pblock_findkeyval(pb_key_content_length, rq->headers);
f4d56bf9de40 Added request body reader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
108 if(ctval != NULL) {
f4d56bf9de40 Added request body reader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
109 printf("read request body\n");
f4d56bf9de40 Added request body reader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
110
f4d56bf9de40 Added request body reader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
111 printf("netbuf{%d}\n", sn->inbuf);
f4d56bf9de40 Added request body reader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
112
f4d56bf9de40 Added request body reader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
113 int c;
f4d56bf9de40 Added request body reader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
114 while((c = netbuf_getc(sn->inbuf)) != IO_EOF) {
f4d56bf9de40 Added request body reader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
115 putchar(c);
f4d56bf9de40 Added request body reader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
116 }
f4d56bf9de40 Added request body reader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
117 printf("\n");
f4d56bf9de40 Added request body reader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
118 }
f4d56bf9de40 Added request body reader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
119
f4d56bf9de40 Added request body reader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
120
f4d56bf9de40 Added request body reader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
121 // end test
f4d56bf9de40 Added request body reader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
122
6
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
123 int fd = prepare_service_file(sn, rq);
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
124 if(fd < 0) {
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
125 /* TODO: service error */
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
126 http_start_response(sn, rq);
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
127 return REQ_PROCEED;
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
128 }
4
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 3
diff changeset
129
6
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
130 /* send file*/
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
131 SystemIOStream *io = (SystemIOStream*) sn->csd;
3
137197831306 minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
132
6
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
133 off_t fileoffset = 0;
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
134 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
135 printf("content-length: %d\n", len);
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
136 sendfile(io->fd, fd, &fileoffset, len);
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
137
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
138 close(fd);
3
137197831306 minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
139
137197831306 minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
140 return REQ_PROCEED;
137197831306 minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
141 }
10
e3ae779232a9 Added some safs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 8
diff changeset
142
e3ae779232a9 Added some safs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 8
diff changeset
143 int service_hello(pblock *pb, Session *sn, Request *rq) {
e3ae779232a9 Added some safs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 8
diff changeset
144 pblock_nninsert("content-length", 13, rq->srvhdrs);
e3ae779232a9 Added some safs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 8
diff changeset
145 protocol_status(sn, rq, 200, NULL);
e3ae779232a9 Added some safs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 8
diff changeset
146 http_start_response(sn, rq);
e3ae779232a9 Added some safs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 8
diff changeset
147 net_write(sn->csd, "Hello World!\n", 13);
e3ae779232a9 Added some safs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 8
diff changeset
148 return REQ_PROCEED;
e3ae779232a9 Added some safs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 8
diff changeset
149 }
12
34aa8001ea53 Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
150
34aa8001ea53 Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
151 int service_index(pblock *pb, Session *sn, Request *rq) {
34aa8001ea53 Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
152 printf("service_index\n");
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 char *ppath = pblock_findkeyval(pb_key_ppath, rq->vars);
34aa8001ea53 Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
155 char *uri = pblock_findkeyval(pb_key_uri, rq->reqpb);
34aa8001ea53 Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
156
34aa8001ea53 Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
157 sstr_t r_uri = sstr(uri);
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 /* open the file */
34aa8001ea53 Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
160 int fd = open(ppath, O_RDONLY);
34aa8001ea53 Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
161 if(fd < 0) {
34aa8001ea53 Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
162 perror("service_index: open");
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 int status = 500;
34aa8001ea53 Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
165 switch(errno) {
34aa8001ea53 Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
166 case EACCES: {
34aa8001ea53 Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
167 status = 403;
34aa8001ea53 Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
168 break;
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 case ENOENT: {
34aa8001ea53 Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
171 status = 404;
34aa8001ea53 Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
172 break;
34aa8001ea53 Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
173 }
34aa8001ea53 Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
174 }
34aa8001ea53 Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
175 protocol_status(sn, rq, status, NULL);
34aa8001ea53 Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
176 printf("REQ_ABORTED\n");
34aa8001ea53 Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
177 return REQ_ABORTED;
34aa8001ea53 Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
178 }
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 DIR *dir = fdopendir(fd);
34aa8001ea53 Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
181 if(dir == NULL) {
34aa8001ea53 Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
182 protocol_status(sn, rq, 500, NULL);
34aa8001ea53 Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
183 printf("DIR is null\n");
34aa8001ea53 Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
184 return REQ_ABORTED;
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
34aa8001ea53 Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
187 sbuf_t *out = sbuf_new(1024); /* output buffer */
34aa8001ea53 Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
188
34aa8001ea53 Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
189 /* write html header */
34aa8001ea53 Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
190 sbuf_puts(out, "<html>\n<head>\n<title>Index of ");
34aa8001ea53 Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
191 sbuf_puts(out, uri);
34aa8001ea53 Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
192 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
193 sbuf_puts(out, uri);
34aa8001ea53 Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
194 sbuf_puts(out, "</h1><hr>\n\n");
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 struct dirent *f;
34aa8001ea53 Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
197 while((f = readdir(dir)) != NULL) {
34aa8001ea53 Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
198 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
199 continue;
34aa8001ea53 Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
200 }
34aa8001ea53 Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
201
34aa8001ea53 Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
202 sstr_t filename = sstr(f->d_name);
34aa8001ea53 Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
203
34aa8001ea53 Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
204 sbuf_puts(out, "<a href=\"");
34aa8001ea53 Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
205 sbuf_append(out, r_uri);
34aa8001ea53 Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
206 sbuf_append(out, filename);
34aa8001ea53 Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
207 sbuf_puts(out, "\">");
34aa8001ea53 Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
208 sbuf_append(out, filename);
34aa8001ea53 Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
209 sbuf_puts(out, "</a><br>\n");
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
34aa8001ea53 Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
212 sbuf_puts(out, "\n</body>\n</html>\n");
34aa8001ea53 Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
213
34aa8001ea53 Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
214 /* send stuff to client */
34aa8001ea53 Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
215 pblock_removekey(pb_key_content_type, rq->srvhdrs);
34aa8001ea53 Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
216 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
217 pblock_nninsert("content-length", out->length, rq->srvhdrs);
34aa8001ea53 Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
218 protocol_status(sn, rq, 200, NULL);
34aa8001ea53 Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
219 http_start_response(sn, rq);
34aa8001ea53 Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
220
34aa8001ea53 Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
221 net_write(sn->csd, out->ptr, out->length);
34aa8001ea53 Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
222
34aa8001ea53 Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
223 /* close */
34aa8001ea53 Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
224 closedir(dir);
34aa8001ea53 Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
225
34aa8001ea53 Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
226 return REQ_PROCEED;
34aa8001ea53 Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
227 }

mercurial