src/server/safs/service.c

Thu, 15 Aug 2024 22:38:03 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Thu, 15 Aug 2024 22:38:03 +0200
changeset 549
b26bec196a2e
parent 548
40ecc0a6b280
permissions
-rw-r--r--

fix http_stream_parse_chunk_header: check if the chunk starts with a digit

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"
428
ab58e46b50a5 improve error handling in send_file if the file is a directory
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
34 #include "objecttype.h"
14
b8bf95b39952 New source folder layout
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 12
diff changeset
35 #include "../util/io.h"
b8bf95b39952 New source folder layout
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 12
diff changeset
36 #include "../util/pblock.h"
104
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
37 #include "../util/util.h"
14
b8bf95b39952 New source folder layout
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 12
diff changeset
38 #include "../daemon/protocol.h"
54
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 48
diff changeset
39 #include "../daemon/vfs.h"
3
137197831306 minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
40
14
b8bf95b39952 New source folder layout
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 12
diff changeset
41 #include "../util/strbuf.h"
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 346
diff changeset
42 #include <cx/string.h>
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 346
diff changeset
43 #include <cx/utils.h>
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 346
diff changeset
44 #include <cx/printf.h>
6
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
45
44
3da1f7b6847f added some error messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
46 #include <errno.h>
3da1f7b6847f added some error messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
47
6
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
48 /*
104
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
49 * prepares servicing a file
6
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
50 *
104
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
51 * adds content-length header
6
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
52 *
54
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 48
diff changeset
53 * return the opened file
6
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
54 */
428
ab58e46b50a5 improve error handling in send_file if the file is a directory
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
55 SYS_FILE prepare_service_file(Session *sn, Request *rq, VFSContext *vfs, struct stat *s, int *ret) {
116
d7a186cf87f6 adds find-pathinfo saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 104
diff changeset
56 char *path = pblock_findkeyval(pb_key_path, rq->vars);
6
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
57
77
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 70
diff changeset
58 // open the file
116
d7a186cf87f6 adds find-pathinfo saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 104
diff changeset
59 SYS_FILE fd = vfs_open(vfs, path, O_RDONLY);
54
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 48
diff changeset
60 if(!fd) {
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 48
diff changeset
61 // vfs_open sets http status code
429
25c8e8021156 fix prepare_service_file error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 428
diff changeset
62 *ret = REQ_ABORTED;
54
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 48
diff changeset
63 return NULL;
6
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
64 }
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
65
77
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 70
diff changeset
66 // get stat
59
ab25c0a231d0 some fixes and new public APIs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 58
diff changeset
67 if(vfs_fstat(vfs, fd, s) != 0) {
48
37a512d7b8f6 fixed some memory leaks
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 47
diff changeset
68 //perror("prepare_service_file: stat");
6
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
69 protocol_status(sn, rq, 500, NULL);
429
25c8e8021156 fix prepare_service_file error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 428
diff changeset
70 *ret = REQ_ABORTED;
54
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 48
diff changeset
71 return NULL;
6
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
72 }
59
ab25c0a231d0 some fixes and new public APIs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 58
diff changeset
73
ab25c0a231d0 some fixes and new public APIs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 58
diff changeset
74 // check if the file is a directory
ab25c0a231d0 some fixes and new public APIs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 58
diff changeset
75 if(S_ISDIR(s->st_mode)) {
ab25c0a231d0 some fixes and new public APIs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 58
diff changeset
76 pblock_removekey(pb_key_content_type, rq->srvhdrs);
ab25c0a231d0 some fixes and new public APIs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 58
diff changeset
77 char *uri = pblock_findkeyval(pb_key_uri, rq->reqpb);
ab25c0a231d0 some fixes and new public APIs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 58
diff changeset
78 size_t urilen = strlen(uri);
428
ab58e46b50a5 improve error handling in send_file if the file is a directory
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
79 if(urilen > 0 && uri[urilen-1] != '/') {
ab58e46b50a5 improve error handling in send_file if the file is a directory
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
80 pblock_nvinsert("content-length", "0", rq->srvhdrs);
ab58e46b50a5 improve error handling in send_file if the file is a directory
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
81 char *location = pool_malloc(sn->pool, urilen + 2);
ab58e46b50a5 improve error handling in send_file if the file is a directory
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
82 memcpy(location, uri, urilen);
ab58e46b50a5 improve error handling in send_file if the file is a directory
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
83 location[urilen] = '/';
ab58e46b50a5 improve error handling in send_file if the file is a directory
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
84 location[urilen+1] = '\0';
ab58e46b50a5 improve error handling in send_file if the file is a directory
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
85 pblock_kvinsert(pb_key_location, location, urilen + 1, rq->srvhdrs);
ab58e46b50a5 improve error handling in send_file if the file is a directory
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
86 protocol_status(sn, rq, 302, NULL);
ab58e46b50a5 improve error handling in send_file if the file is a directory
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
87 http_start_response(sn, rq);
ab58e46b50a5 improve error handling in send_file if the file is a directory
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
88 *ret = REQ_PROCEED;
ab58e46b50a5 improve error handling in send_file if the file is a directory
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
89 } else {
ab58e46b50a5 improve error handling in send_file if the file is a directory
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
90 // set content-type to "internal/directory" and ret to REQ_NOACTION
ab58e46b50a5 improve error handling in send_file if the file is a directory
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
91 // maybe a SAF will respond to that
ab58e46b50a5 improve error handling in send_file if the file is a directory
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
92 pblock_kvinsert(
ab58e46b50a5 improve error handling in send_file if the file is a directory
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
93 pb_key_content_type,
ab58e46b50a5 improve error handling in send_file if the file is a directory
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
94 OBJTYPE_INTERNAL_DIRECTORY,
ab58e46b50a5 improve error handling in send_file if the file is a directory
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
95 sizeof(OBJTYPE_INTERNAL_DIRECTORY)-1,
ab58e46b50a5 improve error handling in send_file if the file is a directory
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
96 rq->srvhdrs);
ab58e46b50a5 improve error handling in send_file if the file is a directory
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
97 *ret = REQ_NOACTION;
ab58e46b50a5 improve error handling in send_file if the file is a directory
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
98 }
59
ab25c0a231d0 some fixes and new public APIs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 58
diff changeset
99 vfs_close(fd);
102
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 94
diff changeset
100 return NULL;
59
ab25c0a231d0 some fixes and new public APIs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 58
diff changeset
101 }
102
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 94
diff changeset
102
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 94
diff changeset
103 // sets last-modified, content-length and checks conditions
346
784b24381bed extend postgresql vfs to store an etag in the file
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 263
diff changeset
104 const char *etag = vfs_getetag(fd); // optionally, get etag from file
784b24381bed extend postgresql vfs to store an etag in the file
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 263
diff changeset
105 if(http_set_finfo_etag(sn, rq, s, etag) != REQ_PROCEED) {
102
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 94
diff changeset
106 vfs_close(fd);
428
ab58e46b50a5 improve error handling in send_file if the file is a directory
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
107 *ret = REQ_ABORTED;
102
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 94
diff changeset
108 return NULL;
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 94
diff changeset
109 }
104
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
110
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
111 // TODO: check if vfs can seek
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
112 pblock_kvinsert(pb_key_accept_ranges, "bytes", 5, rq->srvhdrs);
6
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
113
77
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 70
diff changeset
114 // start response
6
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
115 protocol_status(sn, rq, 200, NULL);
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
116
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
117 return fd;
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
118 }
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
119
104
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
120 static void free_range(Session *sn, HttpRange *range) {
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
121 HttpRange *elm = range;
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
122 while(elm) {
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
123 HttpRange *next = elm->next;
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
124 pool_free(sn->pool, elm);
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
125 elm = next;
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
126 }
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
127 }
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
128
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
129 static HttpRange* parse_range(Session *sn, char *header, int *status) {
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
130 *status = PROTOCOL_OK;
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
131
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 346
diff changeset
132 cxstring range = cx_strtrim(cx_str(header));
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 346
diff changeset
133 if(!cx_strprefix(range, (cxstring)CX_STR("bytes="))) {
104
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
134 // unknown range unit - ignore range header
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
135 return NULL;
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
136 }
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
137
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
138 // get byte-range-set
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 346
diff changeset
139 range = cx_strsubs(range, 6);
104
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
140 if(range.length < 1) {
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
141 return NULL;
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
142 }
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
143
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
144 HttpRange *range_list = NULL;
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
145 HttpRange *last = NULL;
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
146 off_t begin = -1;
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
147 int start = 0;
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
148 int hasbegin = 0;
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
149 for(int i=0;i<=range.length;i++) {
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
150 char c = range.ptr[i];
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
151 if(c == '-') {
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 346
diff changeset
152 cxstring num = cx_strsubsl(range, start, i-start);
104
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
153 if(num.length == 0) {
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
154 // empty string before '-' is legal
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
155 hasbegin = 1;
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
156 begin = -1;
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
157 start = i+1;
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
158 continue;
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
159 }
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
160 char *end;
548
40ecc0a6b280 fix strtoll result check
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 496
diff changeset
161 errno = 0;
104
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
162 long long n = strtoll(num.ptr, &end, 10);
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
163 if(errno == 0 && end == range.ptr + i && n >= 0) {
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
164 begin = n;
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
165 hasbegin = 1;
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
166 start = i+1;
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
167 } else {
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
168 // syntax error
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
169 free_range(sn, range_list);
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
170 return NULL;
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
171 }
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
172 } else if(c == ',' || c == '\0') {
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 346
diff changeset
173 cxstring num = cx_strsubsl(range, start, i-start);
104
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
174 if(hasbegin) {
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
175 long long n;
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
176 if(num.length == 0) {
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
177 // empty string after '-' is legal
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
178 n = -1;
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
179 } else {
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
180 char *end;
548
40ecc0a6b280 fix strtoll result check
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 496
diff changeset
181 errno = 0;
104
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
182 n = strtoll(num.ptr, &end, 10);
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
183 if(errno != 0 || end != range.ptr + i || n < 0) {
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
184 // syntax error
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
185 free_range(sn, range_list);
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
186 return NULL;
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
187 }
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
188 }
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
189
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
190 if(!(begin < 0 && n < 0)) {
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
191 // range: begin - n
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
192 HttpRange *rangeelm = pool_malloc(sn->pool, sizeof(HttpRange));
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
193 if(!rangeelm) {
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
194 free_range(sn, range_list);
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
195 *status = PROTOCOL_SERVER_ERROR;
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
196 return NULL;
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
197 }
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
198 rangeelm->begin = begin;
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
199 rangeelm->end = n;
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
200 rangeelm->next = NULL;
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
201 if(!last) {
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
202 range_list = rangeelm;
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
203 last = rangeelm;
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
204 } else {
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
205 last->next = rangeelm;
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
206 last = rangeelm;
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
207 }
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
208
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
209 hasbegin = 0;
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
210 start = i+1;
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
211 continue;
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
212 }
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
213 }
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
214
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
215 // syntax error
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
216 free_range(sn, range_list);
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
217 return NULL;
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
218 }
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
219 }
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
220
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
221 return range_list;
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
222 }
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
223
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
224 static int validate_range(HttpRange *range, struct stat *finfo, int *status) {
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
225 off_t max_len = finfo->st_size;
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
226 while(range) {
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
227 if(range->begin > 0 && range->end > 0) {
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
228 if(range->end < range->begin) {
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
229 *status = PROTOCOL_REQUESTED_RANGE_NOT_SATISFIABLE;
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
230 return 0;
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
231 }
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
232 }
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
233 if(range->begin >= max_len) {
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
234 *status = PROTOCOL_REQUESTED_RANGE_NOT_SATISFIABLE;
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
235 return 0;
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
236 }
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
237 if(range->end >= max_len) {
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
238 *status = PROTOCOL_REQUESTED_RANGE_NOT_SATISFIABLE;
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
239 return 0;
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
240 }
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
241
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
242 range = range->next;
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
243 }
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
244
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
245 // TODO: check for Denial-of-Service Attacks
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
246
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
247 return 1;
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
248 }
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
249
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
250 /*
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
251 * translates a HttpRange element to a begin offset and a length
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
252 * the HttpRange must be validated
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
253 */
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
254 static void range2off(HttpRange *range, off_t filelen, off_t *begin, off_t *length) {
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
255 if(range->begin < 0) {
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
256 // bytes=-a
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
257 *begin = filelen - range->end;
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
258 *length = range->end;
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
259 } else if(range->end < 0) {
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
260 // bytes=a-
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
261 *begin = range->begin;
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
262 *length = filelen - range->begin;
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
263 } else {
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
264 // bytes=a-b
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
265 *begin = range->begin;
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
266 *length = range->end + 1 - range->begin;
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
267 }
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
268 }
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
269
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
270 #define SF_MAX_LEN 0x8000000
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
271
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
272 static int send_range(Session *sn, SYS_FILE fd, off_t offset, off_t length, char *header, int headerlen) {
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
273 off_t remaining = length;
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
274
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
275 sendfiledata sfd;
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
276 sfd.fd = fd;
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
277 sfd.header = header;
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
278 sfd.hlen = headerlen;
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
279 sfd.trailer = NULL;
493
56cf890dd9ed fix net_sendfile not counting number of written bytes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 490
diff changeset
280 sfd.tlen = 0;
104
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
281
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
282 while(remaining > 0) {
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
283 size_t sflen = remaining < SF_MAX_LEN ? remaining : SF_MAX_LEN;
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
284 sfd.offset = offset;
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
285 sfd.len = sflen;
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
286
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
287 ssize_t r = net_sendfile(sn->csd, &sfd);
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
288 if(r < 0) {
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
289 return -1;
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
290 }
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
291
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
292 sfd.header = NULL; // make sure the header is only sent once
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
293 offset += r;
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
294 remaining -= r;
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
295 }
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
296
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
297 return 0;
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
298 }
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
299
172
5580517faafc adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 127
diff changeset
300
5580517faafc adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 127
diff changeset
301 static void send_range_cleanup(AsyncSendRange *asr) {
5580517faafc adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 127
diff changeset
302 WSBool error = asr->error;
5580517faafc adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 127
diff changeset
303 Session *sn = asr->sn;
5580517faafc adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 127
diff changeset
304 Request *rq = asr->rq;
5580517faafc adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 127
diff changeset
305
5580517faafc adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 127
diff changeset
306 pool_handle_t *pool = asr->sn->pool;
5580517faafc adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 127
diff changeset
307 vfs_close(asr->in);
5580517faafc adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 127
diff changeset
308 pool_free(pool, asr->aio->buf);
5580517faafc adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 127
diff changeset
309 pool_free(pool, asr->aio);
5580517faafc adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 127
diff changeset
310 pool_free(pool, asr->readev);
5580517faafc adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 127
diff changeset
311 pool_free(pool, asr->writeev);
5580517faafc adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 127
diff changeset
312 pool_free(pool, asr);
5580517faafc adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 127
diff changeset
313
5580517faafc adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 127
diff changeset
314 int ret = REQ_PROCEED;
5580517faafc adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 127
diff changeset
315 if(error) {
5580517faafc adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 127
diff changeset
316 rq->rq_attr.keep_alive = 0;
5580517faafc adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 127
diff changeset
317 ret = REQ_ABORTED;
5580517faafc adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 127
diff changeset
318 }
5580517faafc adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 127
diff changeset
319 // return to nsapi loop
5580517faafc adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 127
diff changeset
320 nsapi_function_return(sn, rq, ret);
5580517faafc adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 127
diff changeset
321 }
5580517faafc adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 127
diff changeset
322
5580517faafc adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 127
diff changeset
323 static int send_buf(
5580517faafc adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 127
diff changeset
324 SYS_NETFD out,
5580517faafc adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 127
diff changeset
325 char *restrict buf,
5580517faafc adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 127
diff changeset
326 size_t len,
5580517faafc adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 127
diff changeset
327 size_t *restrict pos)
5580517faafc adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 127
diff changeset
328 {
5580517faafc adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 127
diff changeset
329 while(*pos < len) {
5580517faafc adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 127
diff changeset
330 ssize_t w = net_write(out, buf + *pos, len - *pos);
5580517faafc adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 127
diff changeset
331 if(w <= 0) {
5580517faafc adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 127
diff changeset
332 return -1;
5580517faafc adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 127
diff changeset
333 }
5580517faafc adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 127
diff changeset
334 *pos += w;
5580517faafc adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 127
diff changeset
335 }
5580517faafc adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 127
diff changeset
336 return 0;
5580517faafc adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 127
diff changeset
337 }
5580517faafc adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 127
diff changeset
338
5580517faafc adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 127
diff changeset
339 static int send_bytes(AsyncSendRange *asr, WSBool *completed) {
5580517faafc adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 127
diff changeset
340 *completed = FALSE;
184
a2a15ad6e4b9 fixes async sendfile
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 172
diff changeset
341 if(asr->header) {
a2a15ad6e4b9 fixes async sendfile
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 172
diff changeset
342 if(send_buf(asr->out, asr->header, asr->headerlen, &asr->headerpos)) {
a2a15ad6e4b9 fixes async sendfile
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 172
diff changeset
343 if(net_errno(asr->out) == EAGAIN) {
a2a15ad6e4b9 fixes async sendfile
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 172
diff changeset
344 return 0;
a2a15ad6e4b9 fixes async sendfile
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 172
diff changeset
345 } else {
a2a15ad6e4b9 fixes async sendfile
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 172
diff changeset
346 asr->error = TRUE;
a2a15ad6e4b9 fixes async sendfile
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 172
diff changeset
347 return 1;
a2a15ad6e4b9 fixes async sendfile
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 172
diff changeset
348 }
a2a15ad6e4b9 fixes async sendfile
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 172
diff changeset
349 }
a2a15ad6e4b9 fixes async sendfile
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 172
diff changeset
350 if(asr->headerpos >= asr->headerlen) {
a2a15ad6e4b9 fixes async sendfile
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 172
diff changeset
351 asr->header = NULL;
172
5580517faafc adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 127
diff changeset
352 }
5580517faafc adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 127
diff changeset
353 }
5580517faafc adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 127
diff changeset
354
5580517faafc adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 127
diff changeset
355 if(send_buf(asr->out, asr->aio->buf, asr->aio->result, &asr->wpos)) {
5580517faafc adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 127
diff changeset
356 if(net_errno(asr->out) == EAGAIN) {
5580517faafc adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 127
diff changeset
357 return 0;
5580517faafc adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 127
diff changeset
358 } else {
5580517faafc adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 127
diff changeset
359 asr->error = TRUE;
5580517faafc adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 127
diff changeset
360 return 1;
5580517faafc adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 127
diff changeset
361 }
5580517faafc adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 127
diff changeset
362 }
5580517faafc adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 127
diff changeset
363
5580517faafc adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 127
diff changeset
364 if(!asr->read_complete) {
5580517faafc adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 127
diff changeset
365 // write completed => new asynchronous read
5580517faafc adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 127
diff changeset
366 asr->aio->offset += asr->aio->result;
184
a2a15ad6e4b9 fixes async sendfile
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 172
diff changeset
367 size_t length = asr->end - asr->offset;
a2a15ad6e4b9 fixes async sendfile
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 172
diff changeset
368 asr->aio->nbytes = AIO_BUF_SIZE < length ? AIO_BUF_SIZE : length;
186
8041ac363f26 fixes aio error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 184
diff changeset
369 asr->read_inprogress = TRUE;
172
5580517faafc adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 127
diff changeset
370 if(system_aio_read(asr->aio)) {
5580517faafc adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 127
diff changeset
371 asr->error = TRUE;
5580517faafc adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 127
diff changeset
372 return 1;
5580517faafc adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 127
diff changeset
373 }
5580517faafc adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 127
diff changeset
374 }
5580517faafc adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 127
diff changeset
375 *completed = TRUE;
5580517faafc adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 127
diff changeset
376 return 0;
5580517faafc adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 127
diff changeset
377 }
5580517faafc adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 127
diff changeset
378
5580517faafc adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 127
diff changeset
379 static int send_range_readevent(EventHandler *ev, Event *event) {
5580517faafc adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 127
diff changeset
380 AsyncSendRange *asr = event->cookie;
5580517faafc adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 127
diff changeset
381 asr->read_inprogress = FALSE;
5580517faafc adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 127
diff changeset
382 asr->wpos = 0;
184
a2a15ad6e4b9 fixes async sendfile
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 172
diff changeset
383 asr->offset += asr->aio->result;
a2a15ad6e4b9 fixes async sendfile
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 172
diff changeset
384 if(asr->error || asr->aio->result < 0) {
172
5580517faafc adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 127
diff changeset
385 return 0;
5580517faafc adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 127
diff changeset
386 }
5580517faafc adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 127
diff changeset
387
5580517faafc adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 127
diff changeset
388 int ret = 1;
184
a2a15ad6e4b9 fixes async sendfile
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 172
diff changeset
389 if(asr->aio->result == 0 || asr->offset >= asr->end) {
172
5580517faafc adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 127
diff changeset
390 asr->read_complete = TRUE;
5580517faafc adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 127
diff changeset
391 ret = 0;
5580517faafc adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 127
diff changeset
392 }
5580517faafc adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 127
diff changeset
393
5580517faafc adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 127
diff changeset
394 WSBool completed;
5580517faafc adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 127
diff changeset
395 if(send_bytes(asr, &completed)) {
5580517faafc adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 127
diff changeset
396 return 0;
5580517faafc adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 127
diff changeset
397 }
5580517faafc adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 127
diff changeset
398 if(!completed && !asr->write_inprogress) {
5580517faafc adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 127
diff changeset
399 asr->write_inprogress = TRUE;
5580517faafc adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 127
diff changeset
400 if(event_pollout(ev, asr->out, asr->writeev)) {
5580517faafc adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 127
diff changeset
401 asr->error = TRUE;
5580517faafc adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 127
diff changeset
402 return 0;
5580517faafc adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 127
diff changeset
403 }
5580517faafc adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 127
diff changeset
404 }
5580517faafc adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 127
diff changeset
405
5580517faafc adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 127
diff changeset
406 return ret;
5580517faafc adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 127
diff changeset
407 }
5580517faafc adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 127
diff changeset
408
5580517faafc adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 127
diff changeset
409 static int send_range_writeevent(EventHandler *ev, Event *event) {
5580517faafc adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 127
diff changeset
410 AsyncSendRange *asr = event->cookie;
5580517faafc adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 127
diff changeset
411 if(asr->error) {
184
a2a15ad6e4b9 fixes async sendfile
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 172
diff changeset
412 return 1;
172
5580517faafc adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 127
diff changeset
413 }
5580517faafc adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 127
diff changeset
414
5580517faafc adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 127
diff changeset
415 WSBool completed;
5580517faafc adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 127
diff changeset
416 if(send_bytes(asr, &completed)) {
184
a2a15ad6e4b9 fixes async sendfile
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 172
diff changeset
417 return 1;
172
5580517faafc adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 127
diff changeset
418 }
5580517faafc adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 127
diff changeset
419
184
a2a15ad6e4b9 fixes async sendfile
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 172
diff changeset
420 if(completed) {
172
5580517faafc adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 127
diff changeset
421 return 0;
5580517faafc adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 127
diff changeset
422 }
5580517faafc adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 127
diff changeset
423
5580517faafc adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 127
diff changeset
424 return 1;
5580517faafc adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 127
diff changeset
425 }
5580517faafc adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 127
diff changeset
426
5580517faafc adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 127
diff changeset
427 static int send_range_aio_finish(EventHandler *ev, Event *event) {
5580517faafc adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 127
diff changeset
428 AsyncSendRange *asr = event->cookie;
5580517faafc adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 127
diff changeset
429 if(!asr->write_inprogress) {
5580517faafc adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 127
diff changeset
430 send_range_cleanup(asr);
5580517faafc adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 127
diff changeset
431 }
5580517faafc adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 127
diff changeset
432 asr->read_inprogress = FALSE;
5580517faafc adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 127
diff changeset
433 return 0;
5580517faafc adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 127
diff changeset
434 }
5580517faafc adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 127
diff changeset
435
5580517faafc adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 127
diff changeset
436 static int send_range_poll_finish(EventHandler *ev, Event *event) {
5580517faafc adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 127
diff changeset
437 AsyncSendRange *asr = event->cookie;
5580517faafc adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 127
diff changeset
438 if(!asr->read_inprogress) {
5580517faafc adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 127
diff changeset
439 send_range_cleanup(asr);
5580517faafc adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 127
diff changeset
440 }
5580517faafc adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 127
diff changeset
441 asr->write_inprogress = FALSE;
5580517faafc adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 127
diff changeset
442 return 0;
5580517faafc adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 127
diff changeset
443 }
5580517faafc adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 127
diff changeset
444
5580517faafc adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 127
diff changeset
445 static int send_range_aio(Session *sn, Request *rq, SYS_FILE fd, off_t offset, off_t length, char *header, int headerlen) {
5580517faafc adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 127
diff changeset
446 net_setnonblock(sn->csd, TRUE);
5580517faafc adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 127
diff changeset
447
5580517faafc adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 127
diff changeset
448 // try to send the header
5580517faafc adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 127
diff changeset
449 ssize_t hw = net_write(sn->csd, header, headerlen);
5580517faafc adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 127
diff changeset
450 if(hw < 0) {
5580517faafc adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 127
diff changeset
451 if(net_errno(sn->csd) == EAGAIN) {
5580517faafc adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 127
diff changeset
452 hw = 0;
5580517faafc adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 127
diff changeset
453 } else {
5580517faafc adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 127
diff changeset
454 return REQ_ABORTED;
5580517faafc adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 127
diff changeset
455 }
5580517faafc adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 127
diff changeset
456 }
5580517faafc adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 127
diff changeset
457
5580517faafc adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 127
diff changeset
458 AsyncSendRange *asr = pool_malloc(sn->pool, sizeof(AsyncSendRange));
5580517faafc adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 127
diff changeset
459 asr->sn = sn;
5580517faafc adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 127
diff changeset
460 asr->rq = rq;
5580517faafc adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 127
diff changeset
461 asr->in = fd;
5580517faafc adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 127
diff changeset
462 asr->out = sn->csd;
5580517faafc adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 127
diff changeset
463 asr->offset = offset;
184
a2a15ad6e4b9 fixes async sendfile
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 172
diff changeset
464 asr->end = offset + length;
a2a15ad6e4b9 fixes async sendfile
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 172
diff changeset
465 //asr->length = length;
172
5580517faafc adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 127
diff changeset
466 asr->pos = offset;
5580517faafc adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 127
diff changeset
467 asr->read_complete = FALSE;
5580517faafc adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 127
diff changeset
468 asr->read_inprogress = FALSE;
5580517faafc adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 127
diff changeset
469 asr->write_inprogress = FALSE;
5580517faafc adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 127
diff changeset
470 asr->error = FALSE;
5580517faafc adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 127
diff changeset
471 if(hw == headerlen) {
5580517faafc adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 127
diff changeset
472 asr->header = NULL;
5580517faafc adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 127
diff changeset
473 asr->headerlen = 0;
5580517faafc adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 127
diff changeset
474 asr->headerpos = 0;
5580517faafc adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 127
diff changeset
475 } else {
5580517faafc adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 127
diff changeset
476 asr->header = header;
5580517faafc adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 127
diff changeset
477 asr->headerlen = headerlen;
5580517faafc adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 127
diff changeset
478 asr->headerpos = hw;
5580517faafc adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 127
diff changeset
479 }
5580517faafc adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 127
diff changeset
480
5580517faafc adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 127
diff changeset
481 Event *readev = pool_malloc(sn->pool, sizeof(Event));
5580517faafc adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 127
diff changeset
482 ZERO(readev, sizeof(Event));
5580517faafc adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 127
diff changeset
483 readev->cookie = asr;
5580517faafc adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 127
diff changeset
484 readev->fn = send_range_readevent;
5580517faafc adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 127
diff changeset
485 readev->finish = send_range_aio_finish;
5580517faafc adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 127
diff changeset
486
5580517faafc adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 127
diff changeset
487 Event *writeev = pool_malloc(sn->pool, sizeof(Event));
5580517faafc adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 127
diff changeset
488 ZERO(writeev, sizeof(Event));
5580517faafc adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 127
diff changeset
489 writeev->cookie = asr;
5580517faafc adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 127
diff changeset
490 writeev->fn = send_range_writeevent;
5580517faafc adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 127
diff changeset
491 writeev->finish = send_range_poll_finish;
5580517faafc adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 127
diff changeset
492
5580517faafc adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 127
diff changeset
493 asr->readev = readev;
5580517faafc adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 127
diff changeset
494 asr->writeev = writeev;
5580517faafc adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 127
diff changeset
495
5580517faafc adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 127
diff changeset
496 aiocb_s *aio = pool_malloc(sn->pool, sizeof(aiocb_s));
5580517faafc adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 127
diff changeset
497 aio->buf = pool_malloc(sn->pool, AIO_BUF_SIZE);
5580517faafc adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 127
diff changeset
498 aio->nbytes = AIO_BUF_SIZE < length ? AIO_BUF_SIZE : length;
5580517faafc adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 127
diff changeset
499 aio->filedes = fd;
5580517faafc adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 127
diff changeset
500 aio->offset = offset;
5580517faafc adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 127
diff changeset
501 aio->evhandler = sn->ev;
5580517faafc adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 127
diff changeset
502 aio->event = readev;
5580517faafc adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 127
diff changeset
503
5580517faafc adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 127
diff changeset
504 asr->aio = aio;
5580517faafc adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 127
diff changeset
505 asr->wpos = 0;
5580517faafc adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 127
diff changeset
506
186
8041ac363f26 fixes aio error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 184
diff changeset
507 asr->read_inprogress = TRUE;
172
5580517faafc adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 127
diff changeset
508 if(system_aio_read(aio)) {
5580517faafc adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 127
diff changeset
509 send_range_cleanup(asr);
5580517faafc adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 127
diff changeset
510 return REQ_ABORTED;
5580517faafc adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 127
diff changeset
511 }
5580517faafc adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 127
diff changeset
512 asr->read_inprogress = TRUE;
5580517faafc adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 127
diff changeset
513
5580517faafc adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 127
diff changeset
514 return REQ_PROCESSING;
5580517faafc adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 127
diff changeset
515 }
5580517faafc adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 127
diff changeset
516
104
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
517 struct multi_range_elm {
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 346
diff changeset
518 cxmutstr header;
104
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
519 off_t offset;
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
520 off_t length;
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
521 };
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
522
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
523 static int send_multi_range(Session *sn, Request *rq, SYS_FILE fd, off_t filelen, HttpRange *range) {
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 346
diff changeset
524 CxAllocator *a = pool_allocator(sn->pool);
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 346
diff changeset
525
104
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
526 pb_param *content_type = pblock_remove("content-type", rq->srvhdrs);
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
527
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
528 char sep[64];
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
529 int seplen = util_mime_separator(sep);
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
530
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 346
diff changeset
531 cxmutstr newct = cx_asprintf_a(a, "multipart/byteranges; boundary=%s", sep+4);
104
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
532 pblock_kvinsert(
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
533 pb_key_content_type,
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
534 newct.ptr,
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
535 newct.length,
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
536 rq->srvhdrs);
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 346
diff changeset
537 cxFree(a, newct.ptr);
104
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
538
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
539 // calculate content-length
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
540 off_t response_len = 0;
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
541
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
542 int nrange = 0;
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
543 HttpRange *rangeelm = range;
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
544 while(rangeelm) {
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
545 nrange++;
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
546 rangeelm = rangeelm->next;
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
547 }
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
548
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 346
diff changeset
549 struct multi_range_elm *r = pool_calloc(sn->pool, nrange, sizeof(struct multi_range_elm));
104
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
550 rangeelm = range;
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
551 int i=0;
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
552 while(rangeelm) {
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
553 range2off(rangeelm, filelen, &(r[i].offset), &(r[i].length));
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 346
diff changeset
554 r[i].header = cx_asprintf_a(
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 346
diff changeset
555 a,
104
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
556 "%s\r\nContent-Type: %s\r\nContent-Range: bytes %lld-%lld/%lld\r\n\r\n",
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
557 sep,
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
558 content_type->value,
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
559 (long long)r[i].offset,
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
560 (long long)r[i].offset+r[i].length - 1,
490
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 429
diff changeset
561 (long long)filelen);
104
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
562
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
563 response_len += r[i].header.length + r[i].length;
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
564
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
565 rangeelm = rangeelm->next;
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
566 i++;
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
567 }
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
568
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
569 response_len += seplen + 4; // trailer: sep + '--' + CRLF
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
570
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
571 // finally, set the content-length header
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
572 pblock_kllinsert(
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
573 pb_key_content_length,
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
574 (long long)response_len,
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
575 rq->srvhdrs);
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
576
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
577 // and start the response
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
578 http_start_response(sn, rq);
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
579
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
580 rangeelm = range;
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
581 i = 0;
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
582 while(rangeelm) {
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
583 if(send_range(sn, fd, r[i].offset, r[i].length, r[i].header.ptr, r[i].header.length)) {
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
584 // TODO: error
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
585 }
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
586 rangeelm = rangeelm->next;
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
587 i++;
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
588 }
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
589 net_printf(sn->csd, "%s--\r\n", sep);
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
590
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 346
diff changeset
591 pool_free(sn->pool, r);
104
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
592 return 0;
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
593 }
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
594
10
e3ae779232a9 Added some safs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 8
diff changeset
595 int send_file(pblock *pb, Session *sn, Request *rq) {
428
ab58e46b50a5 improve error handling in send_file if the file is a directory
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
596 int ret = REQ_NOACTION;
54
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 48
diff changeset
597 struct stat s;
104
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
598 VFSContext *vfs = vfs_request_context(sn, rq);
428
ab58e46b50a5 improve error handling in send_file if the file is a directory
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
599 SYS_FILE fd = prepare_service_file(sn, rq, vfs, &s, &ret);
54
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 48
diff changeset
600 if(!fd) {
44
3da1f7b6847f added some error messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
601 // if an error occurs, prepare_service_file sets the http status code
428
ab58e46b50a5 improve error handling in send_file if the file is a directory
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
602 // in case fd is a directory and the uri already ends with an trailing
ab58e46b50a5 improve error handling in send_file if the file is a directory
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
603 // '/', ret is set to REQ_NOACTION
ab58e46b50a5 improve error handling in send_file if the file is a directory
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
604 return ret;
6
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
605 }
54
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 48
diff changeset
606
104
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
607 // get and validate range header
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
608 char *range_header = pblock_findkeyval(pb_key_range, rq->headers);
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
609 HttpRange *range = NULL;
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
610 if(range_header) {
496
d511c13ba68d add http range debug logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 493
diff changeset
611 log_ereport(LOG_DEBUG, "send_file: range: %s", range_header);
d511c13ba68d add http range debug logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 493
diff changeset
612
104
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
613 int status;
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
614 range = parse_range(sn, range_header, &status);
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
615 if(status != PROTOCOL_OK) {
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
616 protocol_status(sn, rq, status, NULL);
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
617 vfs_close(fd);
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
618 return REQ_ABORTED;
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
619 }
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
620
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
621 if(!validate_range(range, &s, &status)) {
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
622 protocol_status(sn, rq, status, NULL);
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
623 free_range(sn, range);
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
624 vfs_close(fd);
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
625 return REQ_ABORTED;
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
626 }
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
627 }
54
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 48
diff changeset
628
104
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
629 int single_range = 1;
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
630 off_t offset;
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
631 off_t length;
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
632 if(range) {
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
633 protocol_status(sn, rq, 206, NULL);
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
634 pblock_removekey(pb_key_content_length, rq->srvhdrs);
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
635
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
636 if(range->next) {
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
637 single_range = 0;
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
638 } else {
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
639 range2off(range, s.st_size, &offset, &length);
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
640
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
641 pblock_kllinsert(
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
642 pb_key_content_length,
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
643 (long long)length,
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
644 rq->srvhdrs);
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
645
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 346
diff changeset
646 cxmutstr content_range = cx_asprintf(
104
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
647 "%lld-%lld/%lld",
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
648 (long long)offset,
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
649 (long long)offset+length - 1,
490
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 429
diff changeset
650 (long long)s.st_size);
104
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
651 pblock_kvinsert(
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
652 pb_key_content_range,
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
653 content_range.ptr,
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
654 content_range.length,
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
655 rq->srvhdrs);
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
656 free(content_range.ptr);
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
657 }
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
658 } else {
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
659 offset = 0;
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
660 length = s.st_size;
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
661 }
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
662
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
663 if(single_range) {
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
664 // send response header
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
665 http_start_response(sn, rq);
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
666 // send content
263
533f9097d712 fix kqueue event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 199
diff changeset
667 // TODO: fix: send_range_aio is unstable #96
199
d62f2016cfe5 back to synchronous sendfile
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 192
diff changeset
668 //ret = send_range_aio(sn, rq, fd, offset, length, NULL, 0);
d62f2016cfe5 back to synchronous sendfile
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 192
diff changeset
669 //if(ret == REQ_PROCESSING) {
d62f2016cfe5 back to synchronous sendfile
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 192
diff changeset
670 // return ret;
d62f2016cfe5 back to synchronous sendfile
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 192
diff changeset
671 //}
d62f2016cfe5 back to synchronous sendfile
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 192
diff changeset
672
104
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
673 if(send_range(sn, fd, offset, length, NULL, 0)) {
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
674 // TODO: error
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
675 }
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
676 } else {
192
6a145e13d933 replaces eventfd with pipe and closes aio branch
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 189
diff changeset
677 ret = send_multi_range(sn, rq, fd, s.st_size, range);
6a145e13d933 replaces eventfd with pipe and closes aio branch
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 189
diff changeset
678 // TODO: error
104
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
679 }
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
680
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
681 // cleanup
54
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 48
diff changeset
682 vfs_close(fd);
104
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
683 free_range(sn, range);
3
137197831306 minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
684
192
6a145e13d933 replaces eventfd with pipe and closes aio branch
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 189
diff changeset
685 return ret;
3
137197831306 minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
686 }
10
e3ae779232a9 Added some safs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 8
diff changeset
687
104
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
688
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
689
10
e3ae779232a9 Added some safs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 8
diff changeset
690 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
691 pblock_removekey(pb_key_content_type, rq->srvhdrs);
7b235fa88008 Some fixes for mod_jk
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
692 pblock_nvinsert("content-type", "text/plain", rq->srvhdrs);
10
e3ae779232a9 Added some safs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 8
diff changeset
693 pblock_nninsert("content-length", 13, rq->srvhdrs);
e3ae779232a9 Added some safs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 8
diff changeset
694 protocol_status(sn, rq, 200, NULL);
e3ae779232a9 Added some safs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 8
diff changeset
695 http_start_response(sn, rq);
e3ae779232a9 Added some safs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 8
diff changeset
696 net_write(sn->csd, "Hello World!\n", 13);
e3ae779232a9 Added some safs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 8
diff changeset
697 return REQ_PROCEED;
e3ae779232a9 Added some safs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 8
diff changeset
698 }
12
34aa8001ea53 Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
699
127
84e206063b64 adds minimal websocket implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 116
diff changeset
700 static int ws_msghandler(WebSocket *ws, WSMessage *msg) {
84e206063b64 adds minimal websocket implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 116
diff changeset
701 if(msg->type == 1) {
84e206063b64 adds minimal websocket implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 116
diff changeset
702 printf("Message(text): %.*s\n", (int)msg->length, msg->data);
84e206063b64 adds minimal websocket implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 116
diff changeset
703 websocket_send_text(ws->userdata, "hello", 5);
84e206063b64 adds minimal websocket implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 116
diff changeset
704 } else {
84e206063b64 adds minimal websocket implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 116
diff changeset
705 printf("Message: opcode: %d | length: %d\n", msg->type, (int)msg->length);
84e206063b64 adds minimal websocket implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 116
diff changeset
706 }
84e206063b64 adds minimal websocket implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 116
diff changeset
707 return 0;
84e206063b64 adds minimal websocket implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 116
diff changeset
708 }
84e206063b64 adds minimal websocket implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 116
diff changeset
709
84e206063b64 adds minimal websocket implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 116
diff changeset
710 int service_ws_hello(pblock *pb, Session *sn, Request *rq) {
84e206063b64 adds minimal websocket implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 116
diff changeset
711 WebSocket ws;
84e206063b64 adds minimal websocket implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 116
diff changeset
712 ZERO(&ws, sizeof(WebSocket));
84e206063b64 adds minimal websocket implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 116
diff changeset
713 ws.userdata = sn->csd;
84e206063b64 adds minimal websocket implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 116
diff changeset
714
84e206063b64 adds minimal websocket implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 116
diff changeset
715 ws.on_message = ws_msghandler;
84e206063b64 adds minimal websocket implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 116
diff changeset
716 return http_handle_websocket(sn, rq, &ws);
84e206063b64 adds minimal websocket implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 116
diff changeset
717 }
84e206063b64 adds minimal websocket implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 116
diff changeset
718
12
34aa8001ea53 Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
719 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
720 //printf("service_index\n");
12
34aa8001ea53 Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
721
116
d7a186cf87f6 adds find-pathinfo saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 104
diff changeset
722 char *path = pblock_findkeyval(pb_key_path, rq->vars);
12
34aa8001ea53 Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
723 char *uri = pblock_findkeyval(pb_key_uri, rq->reqpb);
34aa8001ea53 Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
724
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 346
diff changeset
725 cxstring r_uri = cx_str(uri);
12
34aa8001ea53 Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
726
77
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 70
diff changeset
727 // open the file
55
b7908bf38f9f vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 54
diff changeset
728 VFSContext *vfs = vfs_request_context(sn, rq);
116
d7a186cf87f6 adds find-pathinfo saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 104
diff changeset
729 VFS_DIR dir = vfs_opendir(vfs, path);
55
b7908bf38f9f vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 54
diff changeset
730 if(!dir) {
12
34aa8001ea53 Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
731 return REQ_ABORTED;
34aa8001ea53 Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
732 }
34aa8001ea53 Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
733
77
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 70
diff changeset
734 sbuf_t *out = sbuf_new(1024); // output buffer
12
34aa8001ea53 Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
735
77
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 70
diff changeset
736 // write html header
12
34aa8001ea53 Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
737 sbuf_puts(out, "<html>\n<head>\n<title>Index of ");
34aa8001ea53 Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
738 sbuf_puts(out, uri);
34aa8001ea53 Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
739 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
740 sbuf_puts(out, uri);
34aa8001ea53 Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
741 sbuf_puts(out, "</h1><hr>\n\n");
34aa8001ea53 Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
742
77
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 70
diff changeset
743 // list directory
55
b7908bf38f9f vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 54
diff changeset
744 VFS_ENTRY f;
57
b3a89736b23e some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 56
diff changeset
745 while(vfs_readdir(dir, &f)) {
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 346
diff changeset
746 cxstring filename = cx_str(f.name);
12
34aa8001ea53 Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
747
34aa8001ea53 Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
748 sbuf_puts(out, "<a href=\"");
34aa8001ea53 Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
749 sbuf_append(out, r_uri);
34aa8001ea53 Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
750 sbuf_append(out, filename);
34aa8001ea53 Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
751 sbuf_puts(out, "\">");
34aa8001ea53 Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
752 sbuf_append(out, filename);
34aa8001ea53 Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
753 sbuf_puts(out, "</a><br>\n");
34aa8001ea53 Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
754 }
34aa8001ea53 Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
755
34aa8001ea53 Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
756 sbuf_puts(out, "\n</body>\n</html>\n");
34aa8001ea53 Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
757
77
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 70
diff changeset
758 // send stuff to client
12
34aa8001ea53 Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
759 pblock_removekey(pb_key_content_type, rq->srvhdrs);
34aa8001ea53 Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
760 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
761 pblock_nninsert("content-length", out->length, rq->srvhdrs);
34aa8001ea53 Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
762 protocol_status(sn, rq, 200, NULL);
34aa8001ea53 Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
763 http_start_response(sn, rq);
34aa8001ea53 Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
764
34aa8001ea53 Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
765 net_write(sn->csd, out->ptr, out->length);
34aa8001ea53 Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
766
77
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 70
diff changeset
767 // close
55
b7908bf38f9f vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 54
diff changeset
768 vfs_closedir(dir);
48
37a512d7b8f6 fixed some memory leaks
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 47
diff changeset
769 sbuf_free(out);
12
34aa8001ea53 Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
770
34aa8001ea53 Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
771 return REQ_PROCEED;
34aa8001ea53 Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
772 }
20
7b235fa88008 Some fixes for mod_jk
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
773
7b235fa88008 Some fixes for mod_jk
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
774 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
775 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
776 "PROPFIND, PROPPATCH, MKCOL, LOCK, UNLOCK, ACL, REPORT";
7b235fa88008 Some fixes for mod_jk
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
777 char *dav = "1,2,access-control";
7b235fa88008 Some fixes for mod_jk
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
778
7b235fa88008 Some fixes for mod_jk
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
779 pblock_removekey(pb_key_content_type, rq->srvhdrs);
7b235fa88008 Some fixes for mod_jk
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
780 pblock_nvinsert("allow", allow, rq->srvhdrs);
7b235fa88008 Some fixes for mod_jk
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
781 pblock_nvinsert("dav", dav, rq->srvhdrs);
7b235fa88008 Some fixes for mod_jk
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
782 pblock_nninsert("content-length", 0, rq->srvhdrs);
87
bdec069d2239 fixed pathcheck behavior
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
783 protocol_status(sn, rq, 204, NULL);
20
7b235fa88008 Some fixes for mod_jk
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
784 http_start_response(sn, rq);
7b235fa88008 Some fixes for mod_jk
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
785
7b235fa88008 Some fixes for mod_jk
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
786 return REQ_PROCEED;
7b235fa88008 Some fixes for mod_jk
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
787 }

mercurial