Sun, 23 Nov 2025 13:48:29 +0100
respect DirectoryIndex location setting in the service_index SAF
|
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 | 39 | #include "../daemon/vfs.h" |
|
639
1e8416350254
respect DirectoryIndex location setting in the service_index SAF
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
601
diff
changeset
|
40 | #include "../daemon/httprequest.h" |
|
3
137197831306
minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
41 | |
|
14
b8bf95b39952
New source folder layout
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
12
diff
changeset
|
42 | #include "../util/strbuf.h" |
|
415
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
346
diff
changeset
|
43 | #include <cx/string.h> |
|
596
dc7cfde0f3bc
implement new directory listing, that outputs size/lastmodified and has a sorted output
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
584
diff
changeset
|
44 | #include <cx/linked_list.h> |
|
415
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
346
diff
changeset
|
45 | #include <cx/printf.h> |
|
6
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
4
diff
changeset
|
46 | |
|
44
3da1f7b6847f
added some error messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
21
diff
changeset
|
47 | #include <errno.h> |
|
3da1f7b6847f
added some error messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
21
diff
changeset
|
48 | |
|
6
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
4
diff
changeset
|
49 | /* |
|
104
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
50 | * prepares servicing a file |
|
6
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
4
diff
changeset
|
51 | * |
|
104
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
52 | * adds content-length header |
|
6
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
4
diff
changeset
|
53 | * |
| 54 | 54 | * return the opened file |
|
6
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
4
diff
changeset
|
55 | */ |
|
428
ab58e46b50a5
improve error handling in send_file if the file is a directory
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
56 | 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
|
57 | char *path = pblock_findkeyval(pb_key_path, rq->vars); |
|
6
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
4
diff
changeset
|
58 | |
| 77 | 59 | // open the file |
|
116
d7a186cf87f6
adds find-pathinfo saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
104
diff
changeset
|
60 | SYS_FILE fd = vfs_open(vfs, path, O_RDONLY); |
| 54 | 61 | if(!fd) { |
| 62 | // vfs_open sets http status code | |
|
429
25c8e8021156
fix prepare_service_file error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
428
diff
changeset
|
63 | *ret = REQ_ABORTED; |
| 54 | 64 | return NULL; |
|
6
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
4
diff
changeset
|
65 | } |
|
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
4
diff
changeset
|
66 | |
| 77 | 67 | // get stat |
|
59
ab25c0a231d0
some fixes and new public APIs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
58
diff
changeset
|
68 | if(vfs_fstat(vfs, fd, s) != 0) { |
|
48
37a512d7b8f6
fixed some memory leaks
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
47
diff
changeset
|
69 | //perror("prepare_service_file: stat"); |
|
6
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
4
diff
changeset
|
70 | protocol_status(sn, rq, 500, NULL); |
|
429
25c8e8021156
fix prepare_service_file error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
428
diff
changeset
|
71 | *ret = REQ_ABORTED; |
| 54 | 72 | return NULL; |
|
6
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
4
diff
changeset
|
73 | } |
|
59
ab25c0a231d0
some fixes and new public APIs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
58
diff
changeset
|
74 | |
|
ab25c0a231d0
some fixes and new public APIs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
58
diff
changeset
|
75 | // check if the file is a directory |
|
ab25c0a231d0
some fixes and new public APIs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
58
diff
changeset
|
76 | if(S_ISDIR(s->st_mode)) { |
|
ab25c0a231d0
some fixes and new public APIs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
58
diff
changeset
|
77 | 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
|
78 | 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
|
79 | 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
|
80 | 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
|
81 | 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
|
82 | 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
|
83 | 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
|
84 | 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
|
85 | 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
|
86 | 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
|
87 | 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
|
88 | 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
|
89 | *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
|
90 | } else { |
|
ab58e46b50a5
improve error handling in send_file if the file is a directory
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
91 | // 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
|
92 | // 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
|
93 | 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
|
94 | 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
|
95 | 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
|
96 | 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
|
97 | 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
|
98 | *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
|
99 | } |
|
59
ab25c0a231d0
some fixes and new public APIs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
58
diff
changeset
|
100 | 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
|
101 | return NULL; |
|
59
ab25c0a231d0
some fixes and new public APIs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
58
diff
changeset
|
102 | } |
|
102
136a76e293b5
added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
94
diff
changeset
|
103 | |
|
136a76e293b5
added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
94
diff
changeset
|
104 | // 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
|
105 | 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
|
106 | 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
|
107 | 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
|
108 | *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
|
109 | return NULL; |
|
136a76e293b5
added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
94
diff
changeset
|
110 | } |
|
104
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
111 | |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
112 | // TODO: check if vfs can seek |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
113 | 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
|
114 | |
|
564
f19fd264c69c
fix wrong column name usage by pg prop extensions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
548
diff
changeset
|
115 | // everything ok for now |
|
6
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
4
diff
changeset
|
116 | protocol_status(sn, rq, 200, NULL); |
|
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
4
diff
changeset
|
117 | |
|
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
4
diff
changeset
|
118 | return fd; |
|
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
4
diff
changeset
|
119 | } |
|
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
4
diff
changeset
|
120 | |
|
104
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
121 | static void free_range(Session *sn, HttpRange *range) { |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
122 | HttpRange *elm = range; |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
123 | while(elm) { |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
124 | HttpRange *next = elm->next; |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
125 | pool_free(sn->pool, elm); |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
126 | elm = next; |
|
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 | |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
130 | static HttpRange* parse_range(Session *sn, char *header, int *status) { |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
131 | *status = PROTOCOL_OK; |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
132 | |
|
415
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
346
diff
changeset
|
133 | cxstring range = cx_strtrim(cx_str(header)); |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
346
diff
changeset
|
134 | if(!cx_strprefix(range, (cxstring)CX_STR("bytes="))) { |
|
104
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
135 | // unknown range unit - ignore range header |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
136 | return NULL; |
|
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 | |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
139 | // get byte-range-set |
|
415
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
346
diff
changeset
|
140 | range = cx_strsubs(range, 6); |
|
104
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
141 | if(range.length < 1) { |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
142 | return NULL; |
|
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 | |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
145 | HttpRange *range_list = NULL; |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
146 | HttpRange *last = NULL; |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
147 | off_t begin = -1; |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
148 | int start = 0; |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
149 | int hasbegin = 0; |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
150 | for(int i=0;i<=range.length;i++) { |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
151 | char c = range.ptr[i]; |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
152 | if(c == '-') { |
|
415
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
346
diff
changeset
|
153 | cxstring num = cx_strsubsl(range, start, i-start); |
|
104
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
154 | if(num.length == 0) { |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
155 | // empty string before '-' is legal |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
156 | hasbegin = 1; |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
157 | begin = -1; |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
158 | start = i+1; |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
159 | continue; |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
160 | } |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
161 | char *end; |
|
548
40ecc0a6b280
fix strtoll result check
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
496
diff
changeset
|
162 | errno = 0; |
|
104
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
163 | long long n = strtoll(num.ptr, &end, 10); |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
164 | if(errno == 0 && end == range.ptr + i && n >= 0) { |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
165 | begin = n; |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
166 | hasbegin = 1; |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
167 | start = i+1; |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
168 | } else { |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
169 | // syntax error |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
170 | free_range(sn, range_list); |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
171 | return NULL; |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
172 | } |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
173 | } else if(c == ',' || c == '\0') { |
|
415
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
346
diff
changeset
|
174 | cxstring num = cx_strsubsl(range, start, i-start); |
|
104
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
175 | if(hasbegin) { |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
176 | long long n; |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
177 | if(num.length == 0) { |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
178 | // empty string after '-' is legal |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
179 | n = -1; |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
180 | } else { |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
181 | char *end; |
|
548
40ecc0a6b280
fix strtoll result check
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
496
diff
changeset
|
182 | errno = 0; |
|
104
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
183 | n = strtoll(num.ptr, &end, 10); |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
184 | if(errno != 0 || end != range.ptr + i || n < 0) { |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
185 | // syntax error |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
186 | free_range(sn, range_list); |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
187 | return NULL; |
|
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 | |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
191 | if(!(begin < 0 && n < 0)) { |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
192 | // range: begin - n |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
193 | HttpRange *rangeelm = pool_malloc(sn->pool, sizeof(HttpRange)); |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
194 | if(!rangeelm) { |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
195 | free_range(sn, range_list); |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
196 | *status = PROTOCOL_SERVER_ERROR; |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
197 | return NULL; |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
198 | } |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
199 | rangeelm->begin = begin; |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
200 | rangeelm->end = n; |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
201 | rangeelm->next = NULL; |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
202 | if(!last) { |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
203 | range_list = rangeelm; |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
204 | last = rangeelm; |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
205 | } else { |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
206 | last->next = rangeelm; |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
207 | last = rangeelm; |
|
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 | |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
210 | hasbegin = 0; |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
211 | start = i+1; |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
212 | continue; |
|
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 | |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
216 | // syntax error |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
217 | free_range(sn, range_list); |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
218 | return NULL; |
|
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 | |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
222 | return range_list; |
|
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 | |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
225 | 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
|
226 | off_t max_len = finfo->st_size; |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
227 | while(range) { |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
228 | if(range->begin > 0 && range->end > 0) { |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
229 | if(range->end < range->begin) { |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
230 | *status = PROTOCOL_REQUESTED_RANGE_NOT_SATISFIABLE; |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
231 | return 0; |
|
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 | } |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
234 | if(range->begin >= max_len) { |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
235 | *status = PROTOCOL_REQUESTED_RANGE_NOT_SATISFIABLE; |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
236 | return 0; |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
237 | } |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
238 | if(range->end >= max_len) { |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
239 | *status = PROTOCOL_REQUESTED_RANGE_NOT_SATISFIABLE; |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
240 | return 0; |
|
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 | |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
243 | range = range->next; |
|
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 | |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
246 | // TODO: check for Denial-of-Service Attacks |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
247 | |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
248 | return 1; |
|
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 | /* |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
252 | * 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
|
253 | * the HttpRange must be validated |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
254 | */ |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
255 | 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
|
256 | if(range->begin < 0) { |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
257 | // bytes=-a |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
258 | *begin = filelen - range->end; |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
259 | *length = range->end; |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
260 | } else if(range->end < 0) { |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
261 | // bytes=a- |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
262 | *begin = range->begin; |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
263 | *length = filelen - range->begin; |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
264 | } else { |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
265 | // bytes=a-b |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
266 | *begin = range->begin; |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
267 | *length = range->end + 1 - range->begin; |
|
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 | |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
271 | #define SF_MAX_LEN 0x8000000 |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
272 | |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
273 | 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
|
274 | off_t remaining = length; |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
275 | |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
276 | sendfiledata sfd; |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
277 | sfd.fd = fd; |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
278 | sfd.header = header; |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
279 | sfd.hlen = headerlen; |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
280 | sfd.trailer = NULL; |
|
493
56cf890dd9ed
fix net_sendfile not counting number of written bytes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
490
diff
changeset
|
281 | sfd.tlen = 0; |
|
104
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
282 | |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
283 | while(remaining > 0) { |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
284 | 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
|
285 | sfd.offset = offset; |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
286 | sfd.len = sflen; |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
287 | |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
288 | ssize_t r = net_sendfile(sn->csd, &sfd); |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
289 | if(r < 0) { |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
290 | return -1; |
|
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 | |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
293 | 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
|
294 | offset += r; |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
295 | remaining -= r; |
|
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 | |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
298 | return 0; |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
299 | } |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
300 | |
|
172
5580517faafc
adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
127
diff
changeset
|
301 | |
|
5580517faafc
adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
127
diff
changeset
|
302 | 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
|
303 | 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
|
304 | 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
|
305 | 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
|
306 | |
|
5580517faafc
adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
127
diff
changeset
|
307 | 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
|
308 | 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
|
309 | 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
|
310 | 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
|
311 | 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
|
312 | 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
|
313 | 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
|
314 | |
|
5580517faafc
adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
127
diff
changeset
|
315 | 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
|
316 | if(error) { |
|
5580517faafc
adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
127
diff
changeset
|
317 | 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
|
318 | 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
|
319 | } |
|
5580517faafc
adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
127
diff
changeset
|
320 | // 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
|
321 | 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
|
322 | } |
|
5580517faafc
adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
127
diff
changeset
|
323 | |
|
5580517faafc
adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
127
diff
changeset
|
324 | 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
|
325 | 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
|
326 | 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
|
327 | 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
|
328 | 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
|
329 | { |
|
5580517faafc
adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
127
diff
changeset
|
330 | 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
|
331 | 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
|
332 | 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
|
333 | return -1; |
|
5580517faafc
adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
127
diff
changeset
|
334 | } |
|
5580517faafc
adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
127
diff
changeset
|
335 | *pos += w; |
|
5580517faafc
adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
127
diff
changeset
|
336 | } |
|
5580517faafc
adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
127
diff
changeset
|
337 | return 0; |
|
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 | |
|
5580517faafc
adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
127
diff
changeset
|
340 | 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
|
341 | *completed = FALSE; |
|
184
a2a15ad6e4b9
fixes async sendfile
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
172
diff
changeset
|
342 | if(asr->header) { |
|
a2a15ad6e4b9
fixes async sendfile
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
172
diff
changeset
|
343 | 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
|
344 | if(net_errno(asr->out) == EAGAIN) { |
|
a2a15ad6e4b9
fixes async sendfile
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
172
diff
changeset
|
345 | return 0; |
|
a2a15ad6e4b9
fixes async sendfile
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
172
diff
changeset
|
346 | } else { |
|
a2a15ad6e4b9
fixes async sendfile
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
172
diff
changeset
|
347 | asr->error = TRUE; |
|
a2a15ad6e4b9
fixes async sendfile
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
172
diff
changeset
|
348 | return 1; |
|
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 | } |
|
a2a15ad6e4b9
fixes async sendfile
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
172
diff
changeset
|
351 | if(asr->headerpos >= asr->headerlen) { |
|
a2a15ad6e4b9
fixes async sendfile
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
172
diff
changeset
|
352 | 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
|
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 | |
|
5580517faafc
adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
127
diff
changeset
|
356 | 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
|
357 | 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
|
358 | return 0; |
|
5580517faafc
adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
127
diff
changeset
|
359 | } else { |
|
5580517faafc
adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
127
diff
changeset
|
360 | 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
|
361 | return 1; |
|
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 | |
|
5580517faafc
adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
127
diff
changeset
|
365 | 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
|
366 | // 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
|
367 | asr->aio->offset += asr->aio->result; |
|
184
a2a15ad6e4b9
fixes async sendfile
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
172
diff
changeset
|
368 | size_t length = asr->end - asr->offset; |
|
a2a15ad6e4b9
fixes async sendfile
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
172
diff
changeset
|
369 | 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
|
370 | 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
|
371 | 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
|
372 | 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
|
373 | return 1; |
|
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 | } |
|
5580517faafc
adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
127
diff
changeset
|
376 | *completed = TRUE; |
|
5580517faafc
adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
127
diff
changeset
|
377 | return 0; |
|
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 | |
|
5580517faafc
adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
127
diff
changeset
|
380 | 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
|
381 | 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
|
382 | 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
|
383 | asr->wpos = 0; |
|
184
a2a15ad6e4b9
fixes async sendfile
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
172
diff
changeset
|
384 | asr->offset += asr->aio->result; |
|
a2a15ad6e4b9
fixes async sendfile
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
172
diff
changeset
|
385 | 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
|
386 | return 0; |
|
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 | |
|
5580517faafc
adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
127
diff
changeset
|
389 | int ret = 1; |
|
184
a2a15ad6e4b9
fixes async sendfile
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
172
diff
changeset
|
390 | 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
|
391 | 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
|
392 | ret = 0; |
|
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 | |
|
5580517faafc
adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
127
diff
changeset
|
395 | WSBool completed; |
|
5580517faafc
adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
127
diff
changeset
|
396 | 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
|
397 | return 0; |
|
5580517faafc
adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
127
diff
changeset
|
398 | } |
|
5580517faafc
adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
127
diff
changeset
|
399 | 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
|
400 | 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
|
401 | 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
|
402 | 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
|
403 | return 0; |
|
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 | |
|
5580517faafc
adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
127
diff
changeset
|
407 | return ret; |
|
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 | |
|
5580517faafc
adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
127
diff
changeset
|
410 | 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
|
411 | 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
|
412 | if(asr->error) { |
|
184
a2a15ad6e4b9
fixes async sendfile
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
172
diff
changeset
|
413 | 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
|
414 | } |
|
5580517faafc
adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
127
diff
changeset
|
415 | |
|
5580517faafc
adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
127
diff
changeset
|
416 | WSBool completed; |
|
5580517faafc
adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
127
diff
changeset
|
417 | if(send_bytes(asr, &completed)) { |
|
184
a2a15ad6e4b9
fixes async sendfile
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
172
diff
changeset
|
418 | 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
|
419 | } |
|
5580517faafc
adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
127
diff
changeset
|
420 | |
|
184
a2a15ad6e4b9
fixes async sendfile
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
172
diff
changeset
|
421 | 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
|
422 | return 0; |
|
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 | |
|
5580517faafc
adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
127
diff
changeset
|
425 | return 1; |
|
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 | |
|
5580517faafc
adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
127
diff
changeset
|
428 | 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
|
429 | 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
|
430 | 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
|
431 | 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
|
432 | } |
|
5580517faafc
adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
127
diff
changeset
|
433 | 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
|
434 | return 0; |
|
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 | |
|
5580517faafc
adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
127
diff
changeset
|
437 | 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
|
438 | 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
|
439 | 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
|
440 | 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
|
441 | } |
|
5580517faafc
adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
127
diff
changeset
|
442 | 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
|
443 | return 0; |
|
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 | |
|
5580517faafc
adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
127
diff
changeset
|
446 | 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
|
447 | 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
|
448 | |
|
5580517faafc
adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
127
diff
changeset
|
449 | // 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
|
450 | 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
|
451 | 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
|
452 | 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
|
453 | hw = 0; |
|
5580517faafc
adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
127
diff
changeset
|
454 | } else { |
|
5580517faafc
adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
127
diff
changeset
|
455 | 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
|
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 | |
|
5580517faafc
adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
127
diff
changeset
|
459 | 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
|
460 | 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
|
461 | 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
|
462 | 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
|
463 | 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
|
464 | asr->offset = offset; |
|
184
a2a15ad6e4b9
fixes async sendfile
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
172
diff
changeset
|
465 | asr->end = offset + length; |
|
a2a15ad6e4b9
fixes async sendfile
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
172
diff
changeset
|
466 | //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
|
467 | 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
|
468 | 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
|
469 | 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
|
470 | 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
|
471 | 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
|
472 | 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
|
473 | 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
|
474 | 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
|
475 | 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
|
476 | } else { |
|
5580517faafc
adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
127
diff
changeset
|
477 | 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
|
478 | 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
|
479 | 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
|
480 | } |
|
5580517faafc
adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
127
diff
changeset
|
481 | |
|
5580517faafc
adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
127
diff
changeset
|
482 | 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
|
483 | 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
|
484 | 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
|
485 | 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
|
486 | 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
|
487 | |
|
5580517faafc
adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
127
diff
changeset
|
488 | 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
|
489 | 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
|
490 | 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
|
491 | 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
|
492 | 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
|
493 | |
|
5580517faafc
adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
127
diff
changeset
|
494 | 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
|
495 | 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
|
496 | |
|
5580517faafc
adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
127
diff
changeset
|
497 | 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
|
498 | 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
|
499 | 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
|
500 | 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
|
501 | 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
|
502 | 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
|
503 | 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
|
504 | |
|
5580517faafc
adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
127
diff
changeset
|
505 | 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
|
506 | 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
|
507 | |
|
186
8041ac363f26
fixes aio error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
184
diff
changeset
|
508 | 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
|
509 | 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
|
510 | 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
|
511 | 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
|
512 | } |
|
5580517faafc
adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
127
diff
changeset
|
513 | 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
|
514 | |
|
5580517faafc
adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
127
diff
changeset
|
515 | 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
|
516 | } |
|
5580517faafc
adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
127
diff
changeset
|
517 | |
|
104
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
518 | struct multi_range_elm { |
|
415
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
346
diff
changeset
|
519 | cxmutstr header; |
|
104
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
520 | off_t offset; |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
521 | off_t length; |
|
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 | |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
524 | 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
|
525 | CxAllocator *a = pool_allocator(sn->pool); |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
346
diff
changeset
|
526 | |
|
104
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
527 | pb_param *content_type = pblock_remove("content-type", rq->srvhdrs); |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
528 | |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
529 | char sep[64]; |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
530 | int seplen = util_mime_separator(sep); |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
531 | |
|
415
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
346
diff
changeset
|
532 | 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
|
533 | pblock_kvinsert( |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
534 | pb_key_content_type, |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
535 | newct.ptr, |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
536 | newct.length, |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
537 | rq->srvhdrs); |
|
415
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
346
diff
changeset
|
538 | cxFree(a, newct.ptr); |
|
104
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
539 | |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
540 | // calculate content-length |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
541 | off_t response_len = 0; |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
542 | |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
543 | int nrange = 0; |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
544 | HttpRange *rangeelm = range; |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
545 | while(rangeelm) { |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
546 | nrange++; |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
547 | rangeelm = rangeelm->next; |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
548 | } |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
549 | |
|
415
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
346
diff
changeset
|
550 | 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
|
551 | rangeelm = range; |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
552 | int i=0; |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
553 | while(rangeelm) { |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
554 | 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
|
555 | r[i].header = cx_asprintf_a( |
|
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
346
diff
changeset
|
556 | a, |
|
104
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
557 | "%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
|
558 | sep, |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
559 | content_type->value, |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
560 | (long long)r[i].offset, |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
561 | (long long)r[i].offset+r[i].length - 1, |
| 490 | 562 | (long long)filelen); |
|
104
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
563 | |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
564 | response_len += r[i].header.length + r[i].length; |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
565 | |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
566 | rangeelm = rangeelm->next; |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
567 | i++; |
|
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 | |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
570 | response_len += seplen + 4; // trailer: sep + '--' + CRLF |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
571 | |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
572 | // finally, set the content-length header |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
573 | pblock_kllinsert( |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
574 | pb_key_content_length, |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
575 | (long long)response_len, |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
576 | rq->srvhdrs); |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
577 | |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
578 | // and start the response |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
579 | http_start_response(sn, rq); |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
580 | |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
581 | rangeelm = range; |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
582 | i = 0; |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
583 | while(rangeelm) { |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
584 | 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
|
585 | // TODO: error |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
586 | } |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
587 | rangeelm = rangeelm->next; |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
588 | i++; |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
589 | } |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
590 | net_printf(sn->csd, "%s--\r\n", sep); |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
591 | |
|
415
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
346
diff
changeset
|
592 | pool_free(sn->pool, r); |
|
104
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
593 | return 0; |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
594 | } |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
595 | |
| 10 | 596 | 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
|
597 | int ret = REQ_NOACTION; |
| 54 | 598 | struct stat s; |
|
104
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
599 | 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
|
600 | SYS_FILE fd = prepare_service_file(sn, rq, vfs, &s, &ret); |
| 54 | 601 | if(!fd) { |
|
44
3da1f7b6847f
added some error messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
21
diff
changeset
|
602 | // 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
|
603 | // 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
|
604 | // '/', 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
|
605 | return ret; |
|
6
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
4
diff
changeset
|
606 | } |
| 54 | 607 | |
|
104
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
608 | // get and validate range header |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
609 | char *range_header = pblock_findkeyval(pb_key_range, rq->headers); |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
610 | HttpRange *range = NULL; |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
611 | if(range_header) { |
|
496
d511c13ba68d
add http range debug logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
493
diff
changeset
|
612 | 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
|
613 | |
|
104
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
614 | int status; |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
615 | range = parse_range(sn, range_header, &status); |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
616 | if(status != PROTOCOL_OK) { |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
617 | protocol_status(sn, rq, status, NULL); |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
618 | vfs_close(fd); |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
619 | return REQ_ABORTED; |
|
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 | |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
622 | if(!validate_range(range, &s, &status)) { |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
623 | protocol_status(sn, rq, status, NULL); |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
624 | free_range(sn, range); |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
625 | vfs_close(fd); |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
626 | return REQ_ABORTED; |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
627 | } |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
628 | } |
| 54 | 629 | |
|
104
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
630 | int single_range = 1; |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
631 | off_t offset; |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
632 | off_t length; |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
633 | if(range) { |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
634 | protocol_status(sn, rq, 206, NULL); |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
635 | pblock_removekey(pb_key_content_length, rq->srvhdrs); |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
636 | |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
637 | if(range->next) { |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
638 | single_range = 0; |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
639 | } else { |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
640 | range2off(range, s.st_size, &offset, &length); |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
641 | |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
642 | pblock_kllinsert( |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
643 | pb_key_content_length, |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
644 | (long long)length, |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
645 | rq->srvhdrs); |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
646 | |
|
415
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
346
diff
changeset
|
647 | cxmutstr content_range = cx_asprintf( |
|
104
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
648 | "%lld-%lld/%lld", |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
649 | (long long)offset, |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
650 | (long long)offset+length - 1, |
| 490 | 651 | (long long)s.st_size); |
|
104
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
652 | pblock_kvinsert( |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
653 | pb_key_content_range, |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
654 | content_range.ptr, |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
655 | content_range.length, |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
656 | rq->srvhdrs); |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
657 | free(content_range.ptr); |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
658 | } |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
659 | } else { |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
660 | offset = 0; |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
661 | length = s.st_size; |
|
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 | |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
664 | if(single_range) { |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
665 | // send response header |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
666 | http_start_response(sn, rq); |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
667 | // send content |
|
263
533f9097d712
fix kqueue event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
199
diff
changeset
|
668 | // TODO: fix: send_range_aio is unstable #96 |
|
199
d62f2016cfe5
back to synchronous sendfile
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
192
diff
changeset
|
669 | //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
|
670 | //if(ret == REQ_PROCESSING) { |
|
d62f2016cfe5
back to synchronous sendfile
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
192
diff
changeset
|
671 | // return ret; |
|
d62f2016cfe5
back to synchronous sendfile
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
192
diff
changeset
|
672 | //} |
|
d62f2016cfe5
back to synchronous sendfile
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
192
diff
changeset
|
673 | |
|
104
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
674 | if(send_range(sn, fd, offset, length, NULL, 0)) { |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
675 | // TODO: error |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
676 | } |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
677 | } else { |
|
192
6a145e13d933
replaces eventfd with pipe and closes aio branch
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
189
diff
changeset
|
678 | 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
|
679 | // TODO: error |
|
104
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 | |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
682 | // cleanup |
| 54 | 683 | vfs_close(fd); |
|
104
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
684 | free_range(sn, range); |
|
3
137197831306
minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
685 | |
|
192
6a145e13d933
replaces eventfd with pipe and closes aio branch
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
189
diff
changeset
|
686 | return ret; |
|
3
137197831306
minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
687 | } |
| 10 | 688 | |
|
104
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
689 | |
|
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
690 | |
| 10 | 691 | 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
|
692 | pblock_removekey(pb_key_content_type, rq->srvhdrs); |
|
7b235fa88008
Some fixes for mod_jk
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
693 | pblock_nvinsert("content-type", "text/plain", rq->srvhdrs); |
| 10 | 694 | pblock_nninsert("content-length", 13, rq->srvhdrs); |
| 695 | protocol_status(sn, rq, 200, NULL); | |
| 696 | http_start_response(sn, rq); | |
| 697 | net_write(sn->csd, "Hello World!\n", 13); | |
| 698 | return REQ_PROCEED; | |
| 699 | } | |
|
12
34aa8001ea53
Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
11
diff
changeset
|
700 | |
|
127
84e206063b64
adds minimal websocket implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
116
diff
changeset
|
701 | static int ws_msghandler(WebSocket *ws, WSMessage *msg) { |
|
84e206063b64
adds minimal websocket implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
116
diff
changeset
|
702 | if(msg->type == 1) { |
|
84e206063b64
adds minimal websocket implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
116
diff
changeset
|
703 | 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
|
704 | websocket_send_text(ws->userdata, "hello", 5); |
|
84e206063b64
adds minimal websocket implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
116
diff
changeset
|
705 | } else { |
|
84e206063b64
adds minimal websocket implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
116
diff
changeset
|
706 | 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
|
707 | } |
|
84e206063b64
adds minimal websocket implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
116
diff
changeset
|
708 | return 0; |
|
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 | |
|
84e206063b64
adds minimal websocket implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
116
diff
changeset
|
711 | 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
|
712 | WebSocket ws; |
|
84e206063b64
adds minimal websocket implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
116
diff
changeset
|
713 | ZERO(&ws, sizeof(WebSocket)); |
|
84e206063b64
adds minimal websocket implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
116
diff
changeset
|
714 | ws.userdata = sn->csd; |
|
84e206063b64
adds minimal websocket implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
116
diff
changeset
|
715 | |
|
84e206063b64
adds minimal websocket implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
116
diff
changeset
|
716 | ws.on_message = ws_msghandler; |
|
84e206063b64
adds minimal websocket implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
116
diff
changeset
|
717 | return http_handle_websocket(sn, rq, &ws); |
|
84e206063b64
adds minimal websocket implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
116
diff
changeset
|
718 | } |
|
84e206063b64
adds minimal websocket implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
116
diff
changeset
|
719 | |
|
596
dc7cfde0f3bc
implement new directory listing, that outputs size/lastmodified and has a sorted output
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
584
diff
changeset
|
720 | |
|
dc7cfde0f3bc
implement new directory listing, that outputs size/lastmodified and has a sorted output
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
584
diff
changeset
|
721 | // TODO: maybe move these util functions to another file or library |
|
dc7cfde0f3bc
implement new directory listing, that outputs size/lastmodified and has a sorted output
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
584
diff
changeset
|
722 | static char* util_size_str2(const CxAllocator *a, WSBool iscollection, uint64_t contentlength, uint64_t dimension, int precision) { |
|
dc7cfde0f3bc
implement new directory listing, that outputs size/lastmodified and has a sorted output
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
584
diff
changeset
|
723 | char *str = cxMalloc(a, 16); |
|
dc7cfde0f3bc
implement new directory listing, that outputs size/lastmodified and has a sorted output
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
584
diff
changeset
|
724 | uint64_t size = contentlength; |
|
dc7cfde0f3bc
implement new directory listing, that outputs size/lastmodified and has a sorted output
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
584
diff
changeset
|
725 | |
|
dc7cfde0f3bc
implement new directory listing, that outputs size/lastmodified and has a sorted output
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
584
diff
changeset
|
726 | if(iscollection) { |
|
dc7cfde0f3bc
implement new directory listing, that outputs size/lastmodified and has a sorted output
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
584
diff
changeset
|
727 | str[0] = '\0'; // currently no information for collections |
|
dc7cfde0f3bc
implement new directory listing, that outputs size/lastmodified and has a sorted output
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
584
diff
changeset
|
728 | } else if(dimension < 0x400) { |
|
dc7cfde0f3bc
implement new directory listing, that outputs size/lastmodified and has a sorted output
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
584
diff
changeset
|
729 | snprintf(str, 16, "%" PRIu64 " bytes", size); |
|
dc7cfde0f3bc
implement new directory listing, that outputs size/lastmodified and has a sorted output
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
584
diff
changeset
|
730 | } else if(dimension < 0x100000) { |
|
dc7cfde0f3bc
implement new directory listing, that outputs size/lastmodified and has a sorted output
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
584
diff
changeset
|
731 | float s = (float)size/0x400; |
|
dc7cfde0f3bc
implement new directory listing, that outputs size/lastmodified and has a sorted output
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
584
diff
changeset
|
732 | int diff = (s*100 - (int)s*100); |
|
dc7cfde0f3bc
implement new directory listing, that outputs size/lastmodified and has a sorted output
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
584
diff
changeset
|
733 | if(diff > 90) { |
|
dc7cfde0f3bc
implement new directory listing, that outputs size/lastmodified and has a sorted output
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
584
diff
changeset
|
734 | diff = 0; |
|
dc7cfde0f3bc
implement new directory listing, that outputs size/lastmodified and has a sorted output
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
584
diff
changeset
|
735 | s += 0.10f; |
|
dc7cfde0f3bc
implement new directory listing, that outputs size/lastmodified and has a sorted output
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
584
diff
changeset
|
736 | } |
|
dc7cfde0f3bc
implement new directory listing, that outputs size/lastmodified and has a sorted output
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
584
diff
changeset
|
737 | if(dimension < 0x2800 && diff != 0) { |
|
dc7cfde0f3bc
implement new directory listing, that outputs size/lastmodified and has a sorted output
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
584
diff
changeset
|
738 | // size < 10 KiB |
|
dc7cfde0f3bc
implement new directory listing, that outputs size/lastmodified and has a sorted output
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
584
diff
changeset
|
739 | snprintf(str, 16, "%.*f KiB", precision, s); |
|
dc7cfde0f3bc
implement new directory listing, that outputs size/lastmodified and has a sorted output
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
584
diff
changeset
|
740 | } else { |
|
dc7cfde0f3bc
implement new directory listing, that outputs size/lastmodified and has a sorted output
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
584
diff
changeset
|
741 | snprintf(str, 16, "%.0f KiB", s); |
|
dc7cfde0f3bc
implement new directory listing, that outputs size/lastmodified and has a sorted output
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
584
diff
changeset
|
742 | } |
|
dc7cfde0f3bc
implement new directory listing, that outputs size/lastmodified and has a sorted output
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
584
diff
changeset
|
743 | } else if(dimension < 0x40000000) { |
|
dc7cfde0f3bc
implement new directory listing, that outputs size/lastmodified and has a sorted output
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
584
diff
changeset
|
744 | float s = (float)size/0x100000; |
|
dc7cfde0f3bc
implement new directory listing, that outputs size/lastmodified and has a sorted output
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
584
diff
changeset
|
745 | int diff = (s*100 - (int)s*100); |
|
dc7cfde0f3bc
implement new directory listing, that outputs size/lastmodified and has a sorted output
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
584
diff
changeset
|
746 | if(diff > 90) { |
|
dc7cfde0f3bc
implement new directory listing, that outputs size/lastmodified and has a sorted output
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
584
diff
changeset
|
747 | diff = 0; |
|
dc7cfde0f3bc
implement new directory listing, that outputs size/lastmodified and has a sorted output
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
584
diff
changeset
|
748 | s += 0.10f; |
|
dc7cfde0f3bc
implement new directory listing, that outputs size/lastmodified and has a sorted output
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
584
diff
changeset
|
749 | } |
|
dc7cfde0f3bc
implement new directory listing, that outputs size/lastmodified and has a sorted output
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
584
diff
changeset
|
750 | if(dimension < 0xa00000 && diff != 0) { |
|
dc7cfde0f3bc
implement new directory listing, that outputs size/lastmodified and has a sorted output
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
584
diff
changeset
|
751 | // size < 10 MiB |
|
dc7cfde0f3bc
implement new directory listing, that outputs size/lastmodified and has a sorted output
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
584
diff
changeset
|
752 | snprintf(str, 16, "%.*f MiB", precision, s); |
|
dc7cfde0f3bc
implement new directory listing, that outputs size/lastmodified and has a sorted output
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
584
diff
changeset
|
753 | } else { |
|
dc7cfde0f3bc
implement new directory listing, that outputs size/lastmodified and has a sorted output
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
584
diff
changeset
|
754 | size /= 0x100000; |
|
dc7cfde0f3bc
implement new directory listing, that outputs size/lastmodified and has a sorted output
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
584
diff
changeset
|
755 | snprintf(str, 16, "%.0f MiB", s); |
|
dc7cfde0f3bc
implement new directory listing, that outputs size/lastmodified and has a sorted output
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
584
diff
changeset
|
756 | } |
|
dc7cfde0f3bc
implement new directory listing, that outputs size/lastmodified and has a sorted output
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
584
diff
changeset
|
757 | } else if(dimension < 0x1000000000ULL) { |
|
dc7cfde0f3bc
implement new directory listing, that outputs size/lastmodified and has a sorted output
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
584
diff
changeset
|
758 | float s = (float)size/0x40000000; |
|
dc7cfde0f3bc
implement new directory listing, that outputs size/lastmodified and has a sorted output
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
584
diff
changeset
|
759 | int diff = (s*100 - (int)s*100); |
|
dc7cfde0f3bc
implement new directory listing, that outputs size/lastmodified and has a sorted output
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
584
diff
changeset
|
760 | if(diff > 90) { |
|
dc7cfde0f3bc
implement new directory listing, that outputs size/lastmodified and has a sorted output
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
584
diff
changeset
|
761 | diff = 0; |
|
dc7cfde0f3bc
implement new directory listing, that outputs size/lastmodified and has a sorted output
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
584
diff
changeset
|
762 | s += 0.10f; |
|
dc7cfde0f3bc
implement new directory listing, that outputs size/lastmodified and has a sorted output
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
584
diff
changeset
|
763 | } |
|
dc7cfde0f3bc
implement new directory listing, that outputs size/lastmodified and has a sorted output
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
584
diff
changeset
|
764 | if(dimension < 0x280000000 && diff != 0) { |
|
dc7cfde0f3bc
implement new directory listing, that outputs size/lastmodified and has a sorted output
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
584
diff
changeset
|
765 | // size < 10 GiB |
|
dc7cfde0f3bc
implement new directory listing, that outputs size/lastmodified and has a sorted output
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
584
diff
changeset
|
766 | snprintf(str, 16, "%.*f GiB", precision, s); |
|
dc7cfde0f3bc
implement new directory listing, that outputs size/lastmodified and has a sorted output
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
584
diff
changeset
|
767 | } else { |
|
dc7cfde0f3bc
implement new directory listing, that outputs size/lastmodified and has a sorted output
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
584
diff
changeset
|
768 | size /= 0x40000000; |
|
dc7cfde0f3bc
implement new directory listing, that outputs size/lastmodified and has a sorted output
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
584
diff
changeset
|
769 | snprintf(str, 16, "%.0f GiB", s); |
|
dc7cfde0f3bc
implement new directory listing, that outputs size/lastmodified and has a sorted output
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
584
diff
changeset
|
770 | } |
|
dc7cfde0f3bc
implement new directory listing, that outputs size/lastmodified and has a sorted output
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
584
diff
changeset
|
771 | } else { |
|
dc7cfde0f3bc
implement new directory listing, that outputs size/lastmodified and has a sorted output
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
584
diff
changeset
|
772 | size /= 1024; |
|
dc7cfde0f3bc
implement new directory listing, that outputs size/lastmodified and has a sorted output
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
584
diff
changeset
|
773 | float s = (float)size/0x40000000; |
|
dc7cfde0f3bc
implement new directory listing, that outputs size/lastmodified and has a sorted output
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
584
diff
changeset
|
774 | int diff = (s*100 - (int)s*100); |
|
dc7cfde0f3bc
implement new directory listing, that outputs size/lastmodified and has a sorted output
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
584
diff
changeset
|
775 | if(diff > 90) { |
|
dc7cfde0f3bc
implement new directory listing, that outputs size/lastmodified and has a sorted output
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
584
diff
changeset
|
776 | diff = 0; |
|
dc7cfde0f3bc
implement new directory listing, that outputs size/lastmodified and has a sorted output
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
584
diff
changeset
|
777 | s += 0.10f; |
|
dc7cfde0f3bc
implement new directory listing, that outputs size/lastmodified and has a sorted output
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
584
diff
changeset
|
778 | } |
|
dc7cfde0f3bc
implement new directory listing, that outputs size/lastmodified and has a sorted output
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
584
diff
changeset
|
779 | if(dimension < 0x280000000 && diff != 0) { |
|
dc7cfde0f3bc
implement new directory listing, that outputs size/lastmodified and has a sorted output
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
584
diff
changeset
|
780 | // size < 10 TiB |
|
dc7cfde0f3bc
implement new directory listing, that outputs size/lastmodified and has a sorted output
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
584
diff
changeset
|
781 | snprintf(str, 16, "%.*f TiB", precision, s); |
|
dc7cfde0f3bc
implement new directory listing, that outputs size/lastmodified and has a sorted output
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
584
diff
changeset
|
782 | } else { |
|
dc7cfde0f3bc
implement new directory listing, that outputs size/lastmodified and has a sorted output
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
584
diff
changeset
|
783 | size /= 0x40000000; |
|
dc7cfde0f3bc
implement new directory listing, that outputs size/lastmodified and has a sorted output
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
584
diff
changeset
|
784 | snprintf(str, 16, "%.0f TiB", s); |
|
dc7cfde0f3bc
implement new directory listing, that outputs size/lastmodified and has a sorted output
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
584
diff
changeset
|
785 | } |
|
dc7cfde0f3bc
implement new directory listing, that outputs size/lastmodified and has a sorted output
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
584
diff
changeset
|
786 | } |
|
dc7cfde0f3bc
implement new directory listing, that outputs size/lastmodified and has a sorted output
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
584
diff
changeset
|
787 | return str; |
|
dc7cfde0f3bc
implement new directory listing, that outputs size/lastmodified and has a sorted output
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
584
diff
changeset
|
788 | } |
|
dc7cfde0f3bc
implement new directory listing, that outputs size/lastmodified and has a sorted output
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
584
diff
changeset
|
789 | |
|
dc7cfde0f3bc
implement new directory listing, that outputs size/lastmodified and has a sorted output
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
584
diff
changeset
|
790 | static char* util_size_str(const CxAllocator *a, WSBool iscollection, uint64_t contentlength) { |
|
dc7cfde0f3bc
implement new directory listing, that outputs size/lastmodified and has a sorted output
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
584
diff
changeset
|
791 | return util_size_str2(a, iscollection, contentlength, contentlength, 1); |
|
dc7cfde0f3bc
implement new directory listing, that outputs size/lastmodified and has a sorted output
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
584
diff
changeset
|
792 | } |
|
dc7cfde0f3bc
implement new directory listing, that outputs size/lastmodified and has a sorted output
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
584
diff
changeset
|
793 | |
|
dc7cfde0f3bc
implement new directory listing, that outputs size/lastmodified and has a sorted output
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
584
diff
changeset
|
794 | static char* util_date_str(const CxAllocator *a, time_t tm) { |
|
dc7cfde0f3bc
implement new directory listing, that outputs size/lastmodified and has a sorted output
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
584
diff
changeset
|
795 | struct tm t; |
|
dc7cfde0f3bc
implement new directory listing, that outputs size/lastmodified and has a sorted output
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
584
diff
changeset
|
796 | struct tm n; |
|
dc7cfde0f3bc
implement new directory listing, that outputs size/lastmodified and has a sorted output
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
584
diff
changeset
|
797 | time_t now = time(NULL); |
|
dc7cfde0f3bc
implement new directory listing, that outputs size/lastmodified and has a sorted output
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
584
diff
changeset
|
798 | #ifdef _WIN32 |
|
dc7cfde0f3bc
implement new directory listing, that outputs size/lastmodified and has a sorted output
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
584
diff
changeset
|
799 | memcpy(&t, localtime(&tm), sizeof(struct tm)); |
|
dc7cfde0f3bc
implement new directory listing, that outputs size/lastmodified and has a sorted output
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
584
diff
changeset
|
800 | memcpy(&n, localtime(&now), sizeof(struct tm)); |
|
dc7cfde0f3bc
implement new directory listing, that outputs size/lastmodified and has a sorted output
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
584
diff
changeset
|
801 | #else |
|
dc7cfde0f3bc
implement new directory listing, that outputs size/lastmodified and has a sorted output
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
584
diff
changeset
|
802 | localtime_r(&tm, &t); |
|
dc7cfde0f3bc
implement new directory listing, that outputs size/lastmodified and has a sorted output
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
584
diff
changeset
|
803 | localtime_r(&now, &n); |
|
dc7cfde0f3bc
implement new directory listing, that outputs size/lastmodified and has a sorted output
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
584
diff
changeset
|
804 | #endif /* _WIN32 */ |
|
dc7cfde0f3bc
implement new directory listing, that outputs size/lastmodified and has a sorted output
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
584
diff
changeset
|
805 | char *str = cxMalloc(a, 16); |
|
dc7cfde0f3bc
implement new directory listing, that outputs size/lastmodified and has a sorted output
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
584
diff
changeset
|
806 | if(t.tm_year == n.tm_year) { |
|
dc7cfde0f3bc
implement new directory listing, that outputs size/lastmodified and has a sorted output
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
584
diff
changeset
|
807 | strftime(str, 16, "%b %d %H:%M", &t); |
|
dc7cfde0f3bc
implement new directory listing, that outputs size/lastmodified and has a sorted output
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
584
diff
changeset
|
808 | } else { |
|
dc7cfde0f3bc
implement new directory listing, that outputs size/lastmodified and has a sorted output
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
584
diff
changeset
|
809 | strftime(str, 16, "%b %d %Y", &t); |
|
dc7cfde0f3bc
implement new directory listing, that outputs size/lastmodified and has a sorted output
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
584
diff
changeset
|
810 | } |
|
dc7cfde0f3bc
implement new directory listing, that outputs size/lastmodified and has a sorted output
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
584
diff
changeset
|
811 | return str; |
|
dc7cfde0f3bc
implement new directory listing, that outputs size/lastmodified and has a sorted output
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
584
diff
changeset
|
812 | } |
|
dc7cfde0f3bc
implement new directory listing, that outputs size/lastmodified and has a sorted output
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
584
diff
changeset
|
813 | |
|
dc7cfde0f3bc
implement new directory listing, that outputs size/lastmodified and has a sorted output
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
584
diff
changeset
|
814 | static int cmp_file_type_name(IndexEntry *a, IndexEntry *b) { |
|
dc7cfde0f3bc
implement new directory listing, that outputs size/lastmodified and has a sorted output
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
584
diff
changeset
|
815 | if(a->isdir != b->isdir) { |
|
dc7cfde0f3bc
implement new directory listing, that outputs size/lastmodified and has a sorted output
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
584
diff
changeset
|
816 | return a->isdir ? -1 : 1; |
|
dc7cfde0f3bc
implement new directory listing, that outputs size/lastmodified and has a sorted output
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
584
diff
changeset
|
817 | } |
|
598
184b6890921a
make directory listing sorting case insensitive
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
597
diff
changeset
|
818 | int ret = strcasecmp(a->name, b->name); |
|
184b6890921a
make directory listing sorting case insensitive
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
597
diff
changeset
|
819 | if(ret != 0) { |
|
184b6890921a
make directory listing sorting case insensitive
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
597
diff
changeset
|
820 | return ret; |
|
184b6890921a
make directory listing sorting case insensitive
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
597
diff
changeset
|
821 | } |
|
596
dc7cfde0f3bc
implement new directory listing, that outputs size/lastmodified and has a sorted output
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
584
diff
changeset
|
822 | return strcmp(a->name, b->name); |
|
dc7cfde0f3bc
implement new directory listing, that outputs size/lastmodified and has a sorted output
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
584
diff
changeset
|
823 | } |
|
dc7cfde0f3bc
implement new directory listing, that outputs size/lastmodified and has a sorted output
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
584
diff
changeset
|
824 | |
|
12
34aa8001ea53
Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
11
diff
changeset
|
825 | int service_index(pblock *pb, Session *sn, Request *rq) { |
|
639
1e8416350254
respect DirectoryIndex location setting in the service_index SAF
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
601
diff
changeset
|
826 | NSAPIRequest *req = (NSAPIRequest*)rq; |
|
1e8416350254
respect DirectoryIndex location setting in the service_index SAF
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
601
diff
changeset
|
827 | if(req->location && req->location->set_dirindex && !req->location->dirindex) { |
|
1e8416350254
respect DirectoryIndex location setting in the service_index SAF
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
601
diff
changeset
|
828 | return REQ_NOACTION; |
|
1e8416350254
respect DirectoryIndex location setting in the service_index SAF
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
601
diff
changeset
|
829 | } |
|
1e8416350254
respect DirectoryIndex location setting in the service_index SAF
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
601
diff
changeset
|
830 | |
|
47
ce9790523346
server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
831 | //printf("service_index\n"); |
|
596
dc7cfde0f3bc
implement new directory listing, that outputs size/lastmodified and has a sorted output
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
584
diff
changeset
|
832 | const CxAllocator *a = pool_allocator(sn->pool); |
|
12
34aa8001ea53
Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
11
diff
changeset
|
833 | |
|
116
d7a186cf87f6
adds find-pathinfo saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
104
diff
changeset
|
834 | char *path = pblock_findkeyval(pb_key_path, rq->vars); |
|
600
94c01980e3e5
add navigation to directory listing and improve css
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
599
diff
changeset
|
835 | cxstring uri = cx_str(pblock_findkeyval(pb_key_uri, rq->reqpb)); |
|
94c01980e3e5
add navigation to directory listing and improve css
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
599
diff
changeset
|
836 | if(uri.length == 0) { |
|
94c01980e3e5
add navigation to directory listing and improve css
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
599
diff
changeset
|
837 | return REQ_ABORTED; |
|
94c01980e3e5
add navigation to directory listing and improve css
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
599
diff
changeset
|
838 | } |
|
94c01980e3e5
add navigation to directory listing and improve css
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
599
diff
changeset
|
839 | if(uri.ptr[uri.length-1] != '/') { |
|
94c01980e3e5
add navigation to directory listing and improve css
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
599
diff
changeset
|
840 | cxmutstr newuri = cx_strcat_a(a, 2, uri, CX_STR("/")); |
|
94c01980e3e5
add navigation to directory listing and improve css
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
599
diff
changeset
|
841 | uri = cx_strcast(newuri); |
|
94c01980e3e5
add navigation to directory listing and improve css
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
599
diff
changeset
|
842 | } |
|
94c01980e3e5
add navigation to directory listing and improve css
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
599
diff
changeset
|
843 | |
|
597
ca54033c7db1
add show-emojis, dir-icon, file-icon, show-hidden parameters to common-index SAF
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
596
diff
changeset
|
844 | |
|
ca54033c7db1
add show-emojis, dir-icon, file-icon, show-hidden parameters to common-index SAF
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
596
diff
changeset
|
845 | // params |
|
ca54033c7db1
add show-emojis, dir-icon, file-icon, show-hidden parameters to common-index SAF
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
596
diff
changeset
|
846 | char *useemojis = pblock_findval("use-emojis", pb); |
|
ca54033c7db1
add show-emojis, dir-icon, file-icon, show-hidden parameters to common-index SAF
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
596
diff
changeset
|
847 | char *diricon = pblock_findval("dir-icon", pb); |
|
ca54033c7db1
add show-emojis, dir-icon, file-icon, show-hidden parameters to common-index SAF
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
596
diff
changeset
|
848 | char *fileicon = pblock_findval("file-icon", pb); |
|
ca54033c7db1
add show-emojis, dir-icon, file-icon, show-hidden parameters to common-index SAF
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
596
diff
changeset
|
849 | char *hidden = pblock_findval("show-hidden", pb); |
|
ca54033c7db1
add show-emojis, dir-icon, file-icon, show-hidden parameters to common-index SAF
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
596
diff
changeset
|
850 | |
|
ca54033c7db1
add show-emojis, dir-icon, file-icon, show-hidden parameters to common-index SAF
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
596
diff
changeset
|
851 | WSBool show_hidden = TRUE; |
|
ca54033c7db1
add show-emojis, dir-icon, file-icon, show-hidden parameters to common-index SAF
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
596
diff
changeset
|
852 | char *dir_str = "<dir>"; |
|
ca54033c7db1
add show-emojis, dir-icon, file-icon, show-hidden parameters to common-index SAF
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
596
diff
changeset
|
853 | char *file_str = "<file>"; |
|
ca54033c7db1
add show-emojis, dir-icon, file-icon, show-hidden parameters to common-index SAF
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
596
diff
changeset
|
854 | |
|
ca54033c7db1
add show-emojis, dir-icon, file-icon, show-hidden parameters to common-index SAF
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
596
diff
changeset
|
855 | if(hidden) { |
|
ca54033c7db1
add show-emojis, dir-icon, file-icon, show-hidden parameters to common-index SAF
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
596
diff
changeset
|
856 | show_hidden = util_getboolean(hidden, FALSE); |
|
ca54033c7db1
add show-emojis, dir-icon, file-icon, show-hidden parameters to common-index SAF
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
596
diff
changeset
|
857 | } |
|
ca54033c7db1
add show-emojis, dir-icon, file-icon, show-hidden parameters to common-index SAF
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
596
diff
changeset
|
858 | if(useemojis && util_getboolean(useemojis, FALSE)) { |
|
ca54033c7db1
add show-emojis, dir-icon, file-icon, show-hidden parameters to common-index SAF
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
596
diff
changeset
|
859 | dir_str = "📁"; |
|
ca54033c7db1
add show-emojis, dir-icon, file-icon, show-hidden parameters to common-index SAF
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
596
diff
changeset
|
860 | file_str = "📄"; |
|
ca54033c7db1
add show-emojis, dir-icon, file-icon, show-hidden parameters to common-index SAF
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
596
diff
changeset
|
861 | } else { |
|
ca54033c7db1
add show-emojis, dir-icon, file-icon, show-hidden parameters to common-index SAF
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
596
diff
changeset
|
862 | if(diricon) { |
|
ca54033c7db1
add show-emojis, dir-icon, file-icon, show-hidden parameters to common-index SAF
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
596
diff
changeset
|
863 | dir_str = cx_asprintf_a(a, "<img src=\"%s\" alt=\"directory\" border=\"0\"/>", diricon).ptr; |
|
ca54033c7db1
add show-emojis, dir-icon, file-icon, show-hidden parameters to common-index SAF
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
596
diff
changeset
|
864 | } |
|
ca54033c7db1
add show-emojis, dir-icon, file-icon, show-hidden parameters to common-index SAF
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
596
diff
changeset
|
865 | if(fileicon) { |
|
ca54033c7db1
add show-emojis, dir-icon, file-icon, show-hidden parameters to common-index SAF
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
596
diff
changeset
|
866 | file_str = cx_asprintf_a(a, "<img src=\"%s\" alt=\"directory\" border=\"0\"/>", fileicon).ptr; |
|
ca54033c7db1
add show-emojis, dir-icon, file-icon, show-hidden parameters to common-index SAF
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
596
diff
changeset
|
867 | } |
|
ca54033c7db1
add show-emojis, dir-icon, file-icon, show-hidden parameters to common-index SAF
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
596
diff
changeset
|
868 | } |
|
12
34aa8001ea53
Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
11
diff
changeset
|
869 | |
| 77 | 870 | // open the file |
|
55
b7908bf38f9f
vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
54
diff
changeset
|
871 | VFSContext *vfs = vfs_request_context(sn, rq); |
|
116
d7a186cf87f6
adds find-pathinfo saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
104
diff
changeset
|
872 | VFS_DIR dir = vfs_opendir(vfs, path); |
|
55
b7908bf38f9f
vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
54
diff
changeset
|
873 | if(!dir) { |
|
12
34aa8001ea53
Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
11
diff
changeset
|
874 | return REQ_ABORTED; |
|
34aa8001ea53
Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
11
diff
changeset
|
875 | } |
|
597
ca54033c7db1
add show-emojis, dir-icon, file-icon, show-hidden parameters to common-index SAF
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
596
diff
changeset
|
876 | |
|
ca54033c7db1
add show-emojis, dir-icon, file-icon, show-hidden parameters to common-index SAF
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
596
diff
changeset
|
877 | CxList *files = cxLinkedListCreate(a, (cx_compare_func)cmp_file_type_name, sizeof(IndexEntry)); |
|
ca54033c7db1
add show-emojis, dir-icon, file-icon, show-hidden parameters to common-index SAF
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
596
diff
changeset
|
878 | if(!files) { |
|
ca54033c7db1
add show-emojis, dir-icon, file-icon, show-hidden parameters to common-index SAF
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
596
diff
changeset
|
879 | vfs_closedir(dir); |
|
ca54033c7db1
add show-emojis, dir-icon, file-icon, show-hidden parameters to common-index SAF
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
596
diff
changeset
|
880 | return REQ_ABORTED; |
|
ca54033c7db1
add show-emojis, dir-icon, file-icon, show-hidden parameters to common-index SAF
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
596
diff
changeset
|
881 | } |
|
ca54033c7db1
add show-emojis, dir-icon, file-icon, show-hidden parameters to common-index SAF
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
596
diff
changeset
|
882 | |
|
12
34aa8001ea53
Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
11
diff
changeset
|
883 | |
| 77 | 884 | sbuf_t *out = sbuf_new(1024); // output buffer |
|
12
34aa8001ea53
Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
11
diff
changeset
|
885 | |
| 77 | 886 | // write html header |
|
596
dc7cfde0f3bc
implement new directory listing, that outputs size/lastmodified and has a sorted output
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
584
diff
changeset
|
887 | sbuf_puts(out, "<!DOCTYPE html>\n<html>\n<head>\n<title>Index of "); |
|
600
94c01980e3e5
add navigation to directory listing and improve css
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
599
diff
changeset
|
888 | sbuf_append(out, uri); |
|
596
dc7cfde0f3bc
implement new directory listing, that outputs size/lastmodified and has a sorted output
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
584
diff
changeset
|
889 | sbuf_puts(out, "</title>\n"); |
|
dc7cfde0f3bc
implement new directory listing, that outputs size/lastmodified and has a sorted output
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
584
diff
changeset
|
890 | sbuf_puts(out, "<style>\n"); |
|
600
94c01980e3e5
add navigation to directory listing and improve css
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
599
diff
changeset
|
891 | sbuf_puts(out, "body { font-family: sans; }\n"); |
|
94c01980e3e5
add navigation to directory listing and improve css
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
599
diff
changeset
|
892 | sbuf_puts(out, "h1 { font-size: 1.1em; }\n"); |
|
596
dc7cfde0f3bc
implement new directory listing, that outputs size/lastmodified and has a sorted output
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
584
diff
changeset
|
893 | sbuf_puts(out, "th { text-align: left; }\n"); |
|
597
ca54033c7db1
add show-emojis, dir-icon, file-icon, show-hidden parameters to common-index SAF
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
596
diff
changeset
|
894 | sbuf_puts(out, "td { padding-right: 2em; }\n"); |
|
600
94c01980e3e5
add navigation to directory listing and improve css
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
599
diff
changeset
|
895 | sbuf_puts(out, "a { text-decoration: none; }\n"); |
|
597
ca54033c7db1
add show-emojis, dir-icon, file-icon, show-hidden parameters to common-index SAF
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
596
diff
changeset
|
896 | sbuf_puts(out, ".type { padding-right: 0em; }\n"); |
|
600
94c01980e3e5
add navigation to directory listing and improve css
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
599
diff
changeset
|
897 | sbuf_puts(out, ".path { background-color: #e0e0e0; border-radius: 0.5em; padding: 0.25em 0.75em 0.25em 0.75em; }"); |
|
596
dc7cfde0f3bc
implement new directory listing, that outputs size/lastmodified and has a sorted output
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
584
diff
changeset
|
898 | sbuf_puts(out, "</style>\n"); |
|
600
94c01980e3e5
add navigation to directory listing and improve css
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
599
diff
changeset
|
899 | sbuf_puts(out, "</head><body><h1>"); |
|
94c01980e3e5
add navigation to directory listing and improve css
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
599
diff
changeset
|
900 | |
|
94c01980e3e5
add navigation to directory listing and improve css
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
599
diff
changeset
|
901 | if(uri.length > 0) { |
|
94c01980e3e5
add navigation to directory listing and improve css
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
599
diff
changeset
|
902 | sbuf_puts(out, "<span class=\"path\"><a href=\"/\">/</a></span>\n"); |
|
94c01980e3e5
add navigation to directory listing and improve css
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
599
diff
changeset
|
903 | size_t start = 1; |
|
94c01980e3e5
add navigation to directory listing and improve css
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
599
diff
changeset
|
904 | for(size_t i=1;i<uri.length;i++) { |
|
94c01980e3e5
add navigation to directory listing and improve css
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
599
diff
changeset
|
905 | if(uri.ptr[i] == '/') { |
|
94c01980e3e5
add navigation to directory listing and improve css
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
599
diff
changeset
|
906 | sbuf_puts(out, "<span class=\"path\"><a href=\""); |
|
94c01980e3e5
add navigation to directory listing and improve css
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
599
diff
changeset
|
907 | sbuf_write(out, uri.ptr, i); |
|
94c01980e3e5
add navigation to directory listing and improve css
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
599
diff
changeset
|
908 | sbuf_puts(out, "\">"); |
|
94c01980e3e5
add navigation to directory listing and improve css
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
599
diff
changeset
|
909 | sbuf_write(out, uri.ptr+start, i-start); |
|
94c01980e3e5
add navigation to directory listing and improve css
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
599
diff
changeset
|
910 | sbuf_puts(out, "</a></span>\n"); |
|
94c01980e3e5
add navigation to directory listing and improve css
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
599
diff
changeset
|
911 | start = i+1; |
|
94c01980e3e5
add navigation to directory listing and improve css
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
599
diff
changeset
|
912 | } |
|
94c01980e3e5
add navigation to directory listing and improve css
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
599
diff
changeset
|
913 | } |
|
94c01980e3e5
add navigation to directory listing and improve css
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
599
diff
changeset
|
914 | } |
|
94c01980e3e5
add navigation to directory listing and improve css
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
599
diff
changeset
|
915 | |
|
94c01980e3e5
add navigation to directory listing and improve css
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
599
diff
changeset
|
916 | sbuf_puts(out, "</h1><hr>"); |
|
12
34aa8001ea53
Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
11
diff
changeset
|
917 | |
|
596
dc7cfde0f3bc
implement new directory listing, that outputs size/lastmodified and has a sorted output
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
584
diff
changeset
|
918 | // read directory at store entries in the files list |
|
dc7cfde0f3bc
implement new directory listing, that outputs size/lastmodified and has a sorted output
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
584
diff
changeset
|
919 | int ret = REQ_PROCEED; |
|
dc7cfde0f3bc
implement new directory listing, that outputs size/lastmodified and has a sorted output
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
584
diff
changeset
|
920 | VFS_ENTRY f; |
|
dc7cfde0f3bc
implement new directory listing, that outputs size/lastmodified and has a sorted output
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
584
diff
changeset
|
921 | while(vfs_readdir_stat(dir, &f)) { |
|
597
ca54033c7db1
add show-emojis, dir-icon, file-icon, show-hidden parameters to common-index SAF
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
596
diff
changeset
|
922 | if(!show_hidden && f.name[0] == '.') { |
|
ca54033c7db1
add show-emojis, dir-icon, file-icon, show-hidden parameters to common-index SAF
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
596
diff
changeset
|
923 | continue; |
|
ca54033c7db1
add show-emojis, dir-icon, file-icon, show-hidden parameters to common-index SAF
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
596
diff
changeset
|
924 | } |
|
ca54033c7db1
add show-emojis, dir-icon, file-icon, show-hidden parameters to common-index SAF
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
596
diff
changeset
|
925 | |
|
596
dc7cfde0f3bc
implement new directory listing, that outputs size/lastmodified and has a sorted output
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
584
diff
changeset
|
926 | IndexEntry entry; |
|
dc7cfde0f3bc
implement new directory listing, that outputs size/lastmodified and has a sorted output
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
584
diff
changeset
|
927 | entry.name = pool_strdup(sn->pool, f.name); |
|
dc7cfde0f3bc
implement new directory listing, that outputs size/lastmodified and has a sorted output
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
584
diff
changeset
|
928 | if(!entry.name) { |
|
dc7cfde0f3bc
implement new directory listing, that outputs size/lastmodified and has a sorted output
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
584
diff
changeset
|
929 | ret = REQ_ABORTED; |
|
dc7cfde0f3bc
implement new directory listing, that outputs size/lastmodified and has a sorted output
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
584
diff
changeset
|
930 | break; |
|
dc7cfde0f3bc
implement new directory listing, that outputs size/lastmodified and has a sorted output
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
584
diff
changeset
|
931 | } |
|
dc7cfde0f3bc
implement new directory listing, that outputs size/lastmodified and has a sorted output
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
584
diff
changeset
|
932 | if(f.stat_errno == 0) { |
|
dc7cfde0f3bc
implement new directory listing, that outputs size/lastmodified and has a sorted output
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
584
diff
changeset
|
933 | entry.isdir = S_ISDIR(f.stat.st_mode); |
|
dc7cfde0f3bc
implement new directory listing, that outputs size/lastmodified and has a sorted output
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
584
diff
changeset
|
934 | entry.size = (size_t)f.stat.st_size; |
|
dc7cfde0f3bc
implement new directory listing, that outputs size/lastmodified and has a sorted output
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
584
diff
changeset
|
935 | entry.size_str = util_size_str(a, entry.isdir, entry.size); |
|
dc7cfde0f3bc
implement new directory listing, that outputs size/lastmodified and has a sorted output
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
584
diff
changeset
|
936 | entry.lastmodified = util_date_str(a, f.stat.st_mtime); |
|
dc7cfde0f3bc
implement new directory listing, that outputs size/lastmodified and has a sorted output
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
584
diff
changeset
|
937 | } else { |
|
dc7cfde0f3bc
implement new directory listing, that outputs size/lastmodified and has a sorted output
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
584
diff
changeset
|
938 | entry.isdir = 0; |
|
dc7cfde0f3bc
implement new directory listing, that outputs size/lastmodified and has a sorted output
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
584
diff
changeset
|
939 | entry.lastmodified = NULL; |
|
dc7cfde0f3bc
implement new directory listing, that outputs size/lastmodified and has a sorted output
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
584
diff
changeset
|
940 | entry.size_str = NULL; |
|
dc7cfde0f3bc
implement new directory listing, that outputs size/lastmodified and has a sorted output
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
584
diff
changeset
|
941 | entry.size = 0; |
|
dc7cfde0f3bc
implement new directory listing, that outputs size/lastmodified and has a sorted output
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
584
diff
changeset
|
942 | } |
|
dc7cfde0f3bc
implement new directory listing, that outputs size/lastmodified and has a sorted output
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
584
diff
changeset
|
943 | |
|
dc7cfde0f3bc
implement new directory listing, that outputs size/lastmodified and has a sorted output
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
584
diff
changeset
|
944 | if(cxListAdd(files, &entry)) { |
|
dc7cfde0f3bc
implement new directory listing, that outputs size/lastmodified and has a sorted output
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
584
diff
changeset
|
945 | ret = REQ_ABORTED; |
|
dc7cfde0f3bc
implement new directory listing, that outputs size/lastmodified and has a sorted output
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
584
diff
changeset
|
946 | break; |
|
dc7cfde0f3bc
implement new directory listing, that outputs size/lastmodified and has a sorted output
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
584
diff
changeset
|
947 | } |
|
dc7cfde0f3bc
implement new directory listing, that outputs size/lastmodified and has a sorted output
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
584
diff
changeset
|
948 | } |
|
dc7cfde0f3bc
implement new directory listing, that outputs size/lastmodified and has a sorted output
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
584
diff
changeset
|
949 | |
|
dc7cfde0f3bc
implement new directory listing, that outputs size/lastmodified and has a sorted output
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
584
diff
changeset
|
950 | // generate html output |
|
597
ca54033c7db1
add show-emojis, dir-icon, file-icon, show-hidden parameters to common-index SAF
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
596
diff
changeset
|
951 | sbuf_puts(out, "<table>\n<tr><th colspan=\"2\">Name</th><th>Size</th><th>Last Modified</th></tr>\n"); |
|
596
dc7cfde0f3bc
implement new directory listing, that outputs size/lastmodified and has a sorted output
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
584
diff
changeset
|
952 | cxListSort(files); |
|
dc7cfde0f3bc
implement new directory listing, that outputs size/lastmodified and has a sorted output
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
584
diff
changeset
|
953 | CxIterator i = cxListIterator(files); |
|
dc7cfde0f3bc
implement new directory listing, that outputs size/lastmodified and has a sorted output
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
584
diff
changeset
|
954 | cx_foreach(IndexEntry *, entry, i) { |
|
601
d9bc7c2dfae2
remove unwanted linebreak from output
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
600
diff
changeset
|
955 | sbuf_puts(out, "<tr>"); |
|
596
dc7cfde0f3bc
implement new directory listing, that outputs size/lastmodified and has a sorted output
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
584
diff
changeset
|
956 | |
|
597
ca54033c7db1
add show-emojis, dir-icon, file-icon, show-hidden parameters to common-index SAF
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
596
diff
changeset
|
957 | sbuf_puts(out, "<td class=\"type\">"); |
|
ca54033c7db1
add show-emojis, dir-icon, file-icon, show-hidden parameters to common-index SAF
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
596
diff
changeset
|
958 | sbuf_puts(out, entry->isdir ? dir_str : file_str); |
|
596
dc7cfde0f3bc
implement new directory listing, that outputs size/lastmodified and has a sorted output
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
584
diff
changeset
|
959 | sbuf_puts(out, "</td>"); |
|
dc7cfde0f3bc
implement new directory listing, that outputs size/lastmodified and has a sorted output
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
584
diff
changeset
|
960 | |
|
dc7cfde0f3bc
implement new directory listing, that outputs size/lastmodified and has a sorted output
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
584
diff
changeset
|
961 | sbuf_puts(out, "<td>"); |
|
dc7cfde0f3bc
implement new directory listing, that outputs size/lastmodified and has a sorted output
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
584
diff
changeset
|
962 | sbuf_puts(out, "<a href=\""); |
|
600
94c01980e3e5
add navigation to directory listing and improve css
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
599
diff
changeset
|
963 | sbuf_append(out, uri); |
|
596
dc7cfde0f3bc
implement new directory listing, that outputs size/lastmodified and has a sorted output
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
584
diff
changeset
|
964 | sbuf_puts(out, entry->name); |
|
dc7cfde0f3bc
implement new directory listing, that outputs size/lastmodified and has a sorted output
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
584
diff
changeset
|
965 | sbuf_puts(out, "\">"); |
|
dc7cfde0f3bc
implement new directory listing, that outputs size/lastmodified and has a sorted output
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
584
diff
changeset
|
966 | sbuf_puts(out, entry->name); |
|
dc7cfde0f3bc
implement new directory listing, that outputs size/lastmodified and has a sorted output
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
584
diff
changeset
|
967 | sbuf_puts(out, "</a>"); |
|
dc7cfde0f3bc
implement new directory listing, that outputs size/lastmodified and has a sorted output
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
584
diff
changeset
|
968 | sbuf_puts(out, "</td>"); |
|
dc7cfde0f3bc
implement new directory listing, that outputs size/lastmodified and has a sorted output
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
584
diff
changeset
|
969 | |
|
dc7cfde0f3bc
implement new directory listing, that outputs size/lastmodified and has a sorted output
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
584
diff
changeset
|
970 | sbuf_puts(out, "<td>"); |
|
dc7cfde0f3bc
implement new directory listing, that outputs size/lastmodified and has a sorted output
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
584
diff
changeset
|
971 | if(entry->size_str) { |
|
599
a5339182e67f
add size in bytes as tooltip to the directory listing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
598
diff
changeset
|
972 | char buf[64]; |
|
a5339182e67f
add size in bytes as tooltip to the directory listing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
598
diff
changeset
|
973 | snprintf(buf, 64, "<span title=\"%zu bytes\">", entry->size); |
|
a5339182e67f
add size in bytes as tooltip to the directory listing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
598
diff
changeset
|
974 | sbuf_puts(out, buf); |
|
596
dc7cfde0f3bc
implement new directory listing, that outputs size/lastmodified and has a sorted output
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
584
diff
changeset
|
975 | sbuf_puts(out, entry->size_str); |
|
599
a5339182e67f
add size in bytes as tooltip to the directory listing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
598
diff
changeset
|
976 | sbuf_puts(out, "</span>"); |
|
596
dc7cfde0f3bc
implement new directory listing, that outputs size/lastmodified and has a sorted output
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
584
diff
changeset
|
977 | } |
|
dc7cfde0f3bc
implement new directory listing, that outputs size/lastmodified and has a sorted output
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
584
diff
changeset
|
978 | sbuf_puts(out, "</td>"); |
|
dc7cfde0f3bc
implement new directory listing, that outputs size/lastmodified and has a sorted output
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
584
diff
changeset
|
979 | |
|
dc7cfde0f3bc
implement new directory listing, that outputs size/lastmodified and has a sorted output
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
584
diff
changeset
|
980 | sbuf_puts(out, "<td>"); |
|
dc7cfde0f3bc
implement new directory listing, that outputs size/lastmodified and has a sorted output
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
584
diff
changeset
|
981 | if(entry->size_str) { |
|
dc7cfde0f3bc
implement new directory listing, that outputs size/lastmodified and has a sorted output
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
584
diff
changeset
|
982 | sbuf_puts(out, entry->lastmodified); |
|
dc7cfde0f3bc
implement new directory listing, that outputs size/lastmodified and has a sorted output
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
584
diff
changeset
|
983 | } |
|
dc7cfde0f3bc
implement new directory listing, that outputs size/lastmodified and has a sorted output
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
584
diff
changeset
|
984 | sbuf_puts(out, "</td>"); |
|
dc7cfde0f3bc
implement new directory listing, that outputs size/lastmodified and has a sorted output
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
584
diff
changeset
|
985 | |
|
dc7cfde0f3bc
implement new directory listing, that outputs size/lastmodified and has a sorted output
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
584
diff
changeset
|
986 | sbuf_puts(out, "</tr>\n"); |
|
dc7cfde0f3bc
implement new directory listing, that outputs size/lastmodified and has a sorted output
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
584
diff
changeset
|
987 | } |
|
dc7cfde0f3bc
implement new directory listing, that outputs size/lastmodified and has a sorted output
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
584
diff
changeset
|
988 | |
|
dc7cfde0f3bc
implement new directory listing, that outputs size/lastmodified and has a sorted output
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
584
diff
changeset
|
989 | sbuf_puts(out, "</table>\n</body>\n</html>\n"); |
|
12
34aa8001ea53
Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
11
diff
changeset
|
990 | |
| 77 | 991 | // send stuff to client |
|
596
dc7cfde0f3bc
implement new directory listing, that outputs size/lastmodified and has a sorted output
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
584
diff
changeset
|
992 | if(ret == REQ_PROCEED) { |
|
dc7cfde0f3bc
implement new directory listing, that outputs size/lastmodified and has a sorted output
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
584
diff
changeset
|
993 | pblock_removekey(pb_key_content_type, rq->srvhdrs); |
|
dc7cfde0f3bc
implement new directory listing, that outputs size/lastmodified and has a sorted output
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
584
diff
changeset
|
994 | pblock_kvinsert(pb_key_content_type, "text/html; charset=utf-8", 24, rq->srvhdrs); |
|
dc7cfde0f3bc
implement new directory listing, that outputs size/lastmodified and has a sorted output
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
584
diff
changeset
|
995 | pblock_nninsert("content-length", out->length, rq->srvhdrs); |
|
dc7cfde0f3bc
implement new directory listing, that outputs size/lastmodified and has a sorted output
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
584
diff
changeset
|
996 | protocol_status(sn, rq, 200, NULL); |
|
dc7cfde0f3bc
implement new directory listing, that outputs size/lastmodified and has a sorted output
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
584
diff
changeset
|
997 | http_start_response(sn, rq); |
|
12
34aa8001ea53
Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
11
diff
changeset
|
998 | |
|
596
dc7cfde0f3bc
implement new directory listing, that outputs size/lastmodified and has a sorted output
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
584
diff
changeset
|
999 | net_write(sn->csd, out->ptr, out->length); |
|
dc7cfde0f3bc
implement new directory listing, that outputs size/lastmodified and has a sorted output
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
584
diff
changeset
|
1000 | } |
|
12
34aa8001ea53
Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
11
diff
changeset
|
1001 | |
| 77 | 1002 | // close |
|
55
b7908bf38f9f
vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
54
diff
changeset
|
1003 | vfs_closedir(dir); |
|
48
37a512d7b8f6
fixed some memory leaks
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
47
diff
changeset
|
1004 | sbuf_free(out); |
|
12
34aa8001ea53
Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
11
diff
changeset
|
1005 | |
|
596
dc7cfde0f3bc
implement new directory listing, that outputs size/lastmodified and has a sorted output
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
584
diff
changeset
|
1006 | return ret; |
|
12
34aa8001ea53
Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
11
diff
changeset
|
1007 | } |
|
20
7b235fa88008
Some fixes for mod_jk
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
1008 | |
|
7b235fa88008
Some fixes for mod_jk
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
1009 | 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
|
1010 | 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
|
1011 | "PROPFIND, PROPPATCH, MKCOL, LOCK, UNLOCK, ACL, REPORT"; |
|
7b235fa88008
Some fixes for mod_jk
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
1012 | char *dav = "1,2,access-control"; |
|
7b235fa88008
Some fixes for mod_jk
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
1013 | |
|
7b235fa88008
Some fixes for mod_jk
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
1014 | pblock_removekey(pb_key_content_type, rq->srvhdrs); |
|
7b235fa88008
Some fixes for mod_jk
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
1015 | pblock_nvinsert("allow", allow, rq->srvhdrs); |
|
7b235fa88008
Some fixes for mod_jk
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
1016 | pblock_nvinsert("dav", dav, rq->srvhdrs); |
|
7b235fa88008
Some fixes for mod_jk
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
1017 | pblock_nninsert("content-length", 0, rq->srvhdrs); |
|
87
bdec069d2239
fixed pathcheck behavior
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
77
diff
changeset
|
1018 | protocol_status(sn, rq, 204, NULL); |
|
20
7b235fa88008
Some fixes for mod_jk
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
1019 | http_start_response(sn, rq); |
|
7b235fa88008
Some fixes for mod_jk
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
1020 | |
|
7b235fa88008
Some fixes for mod_jk
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
1021 | return REQ_PROCEED; |
|
7b235fa88008
Some fixes for mod_jk
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
1022 | } |