src/server/service.c

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

mercurial