src/server/safs/service.c

changeset 44
3da1f7b6847f
parent 21
627b09ee74e4
child 47
ce9790523346
equal deleted inserted replaced
43:8ac56edb4e94 44:3da1f7b6847f
1 /* 1 /*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3 * 3 *
4 * Copyright 2011 Olaf Wintermann. All rights reserved. 4 * Copyright 2013 Olaf Wintermann. All rights reserved.
5 * 5 *
6 * Redistribution and use in source and binary forms, with or without 6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met: 7 * modification, are permitted provided that the following conditions are met:
8 * 8 *
9 * 1. Redistributions of source code must retain the above copyright 9 * 1. Redistributions of source code must retain the above copyright
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE. 26 * POSSIBILITY OF SUCH DAMAGE.
27 */ 27 */
28 28
29 #include <stdio.h> 29 #include <stdio.h>
30 #include <errno.h>
31 #include <sys/file.h> 30 #include <sys/file.h>
32 #include <sys/stat.h> 31 #include <sys/stat.h>
33 32
34 #include "service.h" 33 #include "service.h"
35 #include "../util/io.h" 34 #include "../util/io.h"
37 #include "../daemon/protocol.h" 36 #include "../daemon/protocol.h"
38 37
39 #include <sys/sendfile.h> 38 #include <sys/sendfile.h>
40 #include "../util/strbuf.h" 39 #include "../util/strbuf.h"
41 40
41 #include <errno.h>
42
42 // TODO: system sendfile Abstraktionen in neue Datei auslagern 43 // TODO: system sendfile Abstraktionen in neue Datei auslagern
43 /* 44 /*
44 ssize_t sys_sendfile(int out_fd, int in_fd, off_t *off, size_t len) { 45 ssize_t sys_sendfile(int out_fd, int in_fd, off_t *off, size_t len) {
45 46
46 } 47 }
51 /* 52 /*
52 * prepares for servicing a file 53 * prepares for servicing a file
53 * 54 *
54 * adds content-length header and starts the response 55 * adds content-length header and starts the response
55 * 56 *
56 * return the file descriptor or an error code 57 * return the file descriptor or -1
57 */ 58 */
58 int prepare_service_file(Session *sn, Request *rq) { 59 int prepare_service_file(Session *sn, Request *rq) {
59 char *ppath = pblock_findkeyval(pb_key_ppath, rq->vars); 60 char *ppath = pblock_findkeyval(pb_key_ppath, rq->vars);
60 61
61 /* open the file */ 62 /* open the file */
62 int fd = open(ppath, O_RDONLY); 63 int fd = open(ppath, O_RDONLY);
63 if(fd < 0) { 64 if(fd < 0) {
64 perror("prepare_service_file: open"); 65 perror("prepare_service_file: open");
65 66
66 int status = 500; 67 int status = 500;
67 switch(errno) { 68 int en = errno;
69 switch(en) {
68 case EACCES: { 70 case EACCES: {
69 status = 403; 71 status = 403;
70 break; 72 break;
71 } 73 }
72 case ENOENT: { 74 case ENOENT: {
101 } 103 }
102 104
103 int send_file(pblock *pb, Session *sn, Request *rq) { 105 int send_file(pblock *pb, Session *sn, Request *rq) {
104 int fd = prepare_service_file(sn, rq); 106 int fd = prepare_service_file(sn, rq);
105 if(fd < 0) { 107 if(fd < 0) {
106 /* TODO: service error */ 108 // if an error occurs, prepare_service_file sets the http status code
107 http_start_response(sn, rq); 109 // we can just return REQ_ABORTED
108 return REQ_PROCEED; 110 return REQ_ABORTED;
109 } 111 }
110 112
111 /* send file*/ 113 /* send file*/
112 SystemIOStream *io = (SystemIOStream*) sn->csd; 114 SystemIOStream *io = (SystemIOStream*) sn->csd;
113 115

mercurial