Mon, 16 Jan 2017 14:41:20 +0100
fixes cgi cleanup
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" |
14
b8bf95b39952
New source folder layout
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
12
diff
changeset
|
34 | #include "../util/io.h" |
b8bf95b39952
New source folder layout
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
12
diff
changeset
|
35 | #include "../util/pblock.h" |
104
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
36 | #include "../util/util.h" |
14
b8bf95b39952
New source folder layout
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
12
diff
changeset
|
37 | #include "../daemon/protocol.h" |
54 | 38 | #include "../daemon/vfs.h" |
3
137197831306
minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
39 | |
14
b8bf95b39952
New source folder layout
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
12
diff
changeset
|
40 | #include "../util/strbuf.h" |
104
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
41 | #include <ucx/string.h> |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
42 | #include <ucx/utils.h> |
6
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
4
diff
changeset
|
43 | |
44
3da1f7b6847f
added some error messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
21
diff
changeset
|
44 | #include <errno.h> |
3da1f7b6847f
added some error messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
21
diff
changeset
|
45 | |
6
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
4
diff
changeset
|
46 | |
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
4
diff
changeset
|
47 | /* |
104
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
48 | * prepares servicing a file |
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 | * adds content-length header |
6
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
4
diff
changeset
|
51 | * |
54 | 52 | * return the opened file |
6
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
4
diff
changeset
|
53 | */ |
104
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
54 | SYS_FILE prepare_service_file(Session *sn, Request *rq, VFSContext *vfs, struct stat *s) { |
116
d7a186cf87f6
adds find-pathinfo saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
104
diff
changeset
|
55 | char *path = pblock_findkeyval(pb_key_path, rq->vars); |
6
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
4
diff
changeset
|
56 | |
77 | 57 | // open the file |
116
d7a186cf87f6
adds find-pathinfo saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
104
diff
changeset
|
58 | SYS_FILE fd = vfs_open(vfs, path, O_RDONLY); |
54 | 59 | if(!fd) { |
60 | // vfs_open sets http status code | |
61 | return NULL; | |
6
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
4
diff
changeset
|
62 | } |
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
4
diff
changeset
|
63 | |
77 | 64 | // get stat |
59
ab25c0a231d0
some fixes and new public APIs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
58
diff
changeset
|
65 | if(vfs_fstat(vfs, fd, s) != 0) { |
48
37a512d7b8f6
fixed some memory leaks
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
47
diff
changeset
|
66 | //perror("prepare_service_file: stat"); |
6
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
4
diff
changeset
|
67 | protocol_status(sn, rq, 500, NULL); |
54 | 68 | return NULL; |
6
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
4
diff
changeset
|
69 | } |
59
ab25c0a231d0
some fixes and new public APIs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
58
diff
changeset
|
70 | |
ab25c0a231d0
some fixes and new public APIs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
58
diff
changeset
|
71 | // check if the file is a directory |
ab25c0a231d0
some fixes and new public APIs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
58
diff
changeset
|
72 | if(S_ISDIR(s->st_mode)) { |
ab25c0a231d0
some fixes and new public APIs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
58
diff
changeset
|
73 | pblock_nvinsert("content-length", "0", rq->srvhdrs); |
ab25c0a231d0
some fixes and new public APIs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
58
diff
changeset
|
74 | 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
|
75 | 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
|
76 | size_t urilen = strlen(uri); |
ab25c0a231d0
some fixes and new public APIs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
58
diff
changeset
|
77 | char *location = pool_malloc(sn->pool, urilen + 2); |
ab25c0a231d0
some fixes and new public APIs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
58
diff
changeset
|
78 | memcpy(location, uri, urilen); |
ab25c0a231d0
some fixes and new public APIs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
58
diff
changeset
|
79 | location[urilen] = '/'; |
ab25c0a231d0
some fixes and new public APIs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
58
diff
changeset
|
80 | location[urilen+1] = '\0'; |
ab25c0a231d0
some fixes and new public APIs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
58
diff
changeset
|
81 | pblock_kvinsert(pb_key_location, location, urilen + 1, rq->srvhdrs); |
ab25c0a231d0
some fixes and new public APIs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
58
diff
changeset
|
82 | protocol_status(sn, rq, 302, NULL); |
ab25c0a231d0
some fixes and new public APIs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
58
diff
changeset
|
83 | http_start_response(sn, rq); |
ab25c0a231d0
some fixes and new public APIs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
58
diff
changeset
|
84 | 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
|
85 | return NULL; |
59
ab25c0a231d0
some fixes and new public APIs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
58
diff
changeset
|
86 | } |
102
136a76e293b5
added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
94
diff
changeset
|
87 | |
136a76e293b5
added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
94
diff
changeset
|
88 | // sets last-modified, content-length and checks conditions |
136a76e293b5
added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
94
diff
changeset
|
89 | if(http_set_finfo(sn, rq, s) != REQ_PROCEED) { |
136a76e293b5
added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
94
diff
changeset
|
90 | vfs_close(fd); |
136a76e293b5
added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
94
diff
changeset
|
91 | return NULL; |
136a76e293b5
added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
94
diff
changeset
|
92 | } |
104
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
93 | |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
94 | // TODO: check if vfs can seek |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
95 | 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
|
96 | |
77 | 97 | // start response |
6
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
4
diff
changeset
|
98 | protocol_status(sn, rq, 200, NULL); |
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
4
diff
changeset
|
99 | |
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
4
diff
changeset
|
100 | return fd; |
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
4
diff
changeset
|
101 | } |
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
4
diff
changeset
|
102 | |
104
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
103 | static void free_range(Session *sn, HttpRange *range) { |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
104 | HttpRange *elm = range; |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
105 | while(elm) { |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
106 | HttpRange *next = elm->next; |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
107 | pool_free(sn->pool, elm); |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
108 | elm = next; |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
109 | } |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
110 | } |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
111 | |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
112 | static HttpRange* parse_range(Session *sn, char *header, int *status) { |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
113 | *status = PROTOCOL_OK; |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
114 | |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
115 | sstr_t range = sstrtrim(sstr(header)); |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
116 | if(!sstrprefix(range, S("bytes="))) { |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
117 | // unknown range unit - ignore range header |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
118 | return NULL; |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
119 | } |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
120 | |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
121 | // get byte-range-set |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
122 | range = sstrsubs(range, 6); |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
123 | if(range.length < 1) { |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
124 | return NULL; |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
125 | } |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
126 | |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
127 | HttpRange *range_list = NULL; |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
128 | HttpRange *last = NULL; |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
129 | off_t begin = -1; |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
130 | int start = 0; |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
131 | int hasbegin = 0; |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
132 | for(int i=0;i<=range.length;i++) { |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
133 | char c = range.ptr[i]; |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
134 | if(c == '-') { |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
135 | sstr_t num = sstrsubsl(range, start, i-start); |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
136 | if(num.length == 0) { |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
137 | // empty string before '-' is legal |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
138 | hasbegin = 1; |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
139 | begin = -1; |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
140 | start = i+1; |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
141 | continue; |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
142 | } |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
143 | char *end; |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
144 | long long n = strtoll(num.ptr, &end, 10); |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
145 | if(errno == 0 && end == range.ptr + i && n >= 0) { |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
146 | begin = n; |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
147 | hasbegin = 1; |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
148 | start = i+1; |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
149 | } else { |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
150 | // syntax error |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
151 | free_range(sn, range_list); |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
152 | return NULL; |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
153 | } |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
154 | } else if(c == ',' || c == '\0') { |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
155 | sstr_t num = sstrsubsl(range, start, i-start); |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
156 | if(hasbegin) { |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
157 | long long n; |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
158 | if(num.length == 0) { |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
159 | // empty string after '-' is legal |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
160 | n = -1; |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
161 | } else { |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
162 | char *end; |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
163 | 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 | // syntax error |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
166 | free_range(sn, range_list); |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
167 | return NULL; |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
168 | } |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
169 | } |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
170 | |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
171 | if(!(begin < 0 && n < 0)) { |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
172 | // range: begin - n |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
173 | HttpRange *rangeelm = pool_malloc(sn->pool, sizeof(HttpRange)); |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
174 | if(!rangeelm) { |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
175 | free_range(sn, range_list); |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
176 | *status = PROTOCOL_SERVER_ERROR; |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
177 | return NULL; |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
178 | } |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
179 | rangeelm->begin = begin; |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
180 | rangeelm->end = n; |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
181 | rangeelm->next = NULL; |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
182 | if(!last) { |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
183 | range_list = rangeelm; |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
184 | last = rangeelm; |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
185 | } else { |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
186 | last->next = rangeelm; |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
187 | last = rangeelm; |
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 | hasbegin = 0; |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
191 | start = i+1; |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
192 | continue; |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
193 | } |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
194 | } |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
195 | |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
196 | // syntax error |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
197 | free_range(sn, range_list); |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
198 | return NULL; |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
199 | } |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
200 | } |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
201 | |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
202 | return range_list; |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
203 | } |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
204 | |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
205 | 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
|
206 | off_t max_len = finfo->st_size; |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
207 | while(range) { |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
208 | if(range->begin > 0 && range->end > 0) { |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
209 | if(range->end < range->begin) { |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
210 | *status = PROTOCOL_REQUESTED_RANGE_NOT_SATISFIABLE; |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
211 | return 0; |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
212 | } |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
213 | } |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
214 | if(range->begin >= max_len) { |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
215 | *status = PROTOCOL_REQUESTED_RANGE_NOT_SATISFIABLE; |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
216 | return 0; |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
217 | } |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
218 | if(range->end >= max_len) { |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
219 | *status = PROTOCOL_REQUESTED_RANGE_NOT_SATISFIABLE; |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
220 | return 0; |
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 | |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
223 | range = range->next; |
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 | |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
226 | // TODO: check for Denial-of-Service Attacks |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
227 | |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
228 | return 1; |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
229 | } |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
230 | |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
231 | /* |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
232 | * 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
|
233 | * the HttpRange must be validated |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
234 | */ |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
235 | 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
|
236 | if(range->begin < 0) { |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
237 | // bytes=-a |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
238 | *begin = filelen - range->end; |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
239 | *length = range->end; |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
240 | } else if(range->end < 0) { |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
241 | // bytes=a- |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
242 | *begin = range->begin; |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
243 | *length = filelen - range->begin; |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
244 | } else { |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
245 | // bytes=a-b |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
246 | *begin = range->begin; |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
247 | *length = range->end + 1 - range->begin; |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
248 | } |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
249 | } |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
250 | |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
251 | #define SF_MAX_LEN 0x8000000 |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
252 | |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
253 | 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
|
254 | off_t remaining = length; |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
255 | |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
256 | sendfiledata sfd; |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
257 | sfd.fd = fd; |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
258 | sfd.header = header; |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
259 | sfd.hlen = headerlen; |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
260 | sfd.trailer = NULL; |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
261 | |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
262 | while(remaining > 0) { |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
263 | 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
|
264 | sfd.offset = offset; |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
265 | sfd.len = sflen; |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
266 | |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
267 | ssize_t r = net_sendfile(sn->csd, &sfd); |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
268 | if(r < 0) { |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
269 | return -1; |
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 | |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
272 | 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
|
273 | offset += r; |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
274 | remaining -= r; |
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 | |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
277 | return 0; |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
278 | } |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
279 | |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
280 | struct multi_range_elm { |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
281 | sstr_t header; |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
282 | off_t offset; |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
283 | off_t length; |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
284 | }; |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
285 | |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
286 | static int send_multi_range(Session *sn, Request *rq, SYS_FILE fd, off_t filelen, HttpRange *range) { |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
287 | pb_param *content_type = pblock_remove("content-type", rq->srvhdrs); |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
288 | |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
289 | char sep[64]; |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
290 | int seplen = util_mime_separator(sep); |
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 | sstr_t newct = ucx_sprintf("multipart/byteranges; boundary=%s", sep+4); |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
293 | pblock_kvinsert( |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
294 | pb_key_content_type, |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
295 | newct.ptr, |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
296 | newct.length, |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
297 | rq->srvhdrs); |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
298 | free(newct.ptr); |
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 | // calculate content-length |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
301 | off_t response_len = 0; |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
302 | |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
303 | int nrange = 0; |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
304 | HttpRange *rangeelm = range; |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
305 | while(rangeelm) { |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
306 | nrange++; |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
307 | rangeelm = rangeelm->next; |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
308 | } |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
309 | |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
310 | struct multi_range_elm *r = calloc(nrange, sizeof(struct multi_range_elm)); |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
311 | rangeelm = range; |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
312 | int i=0; |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
313 | while(rangeelm) { |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
314 | range2off(rangeelm, filelen, &(r[i].offset), &(r[i].length)); |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
315 | r[i].header = ucx_sprintf( |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
316 | "%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
|
317 | sep, |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
318 | content_type->value, |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
319 | (long long)r[i].offset, |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
320 | (long long)r[i].offset+r[i].length - 1, |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
321 | filelen); |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
322 | |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
323 | response_len += r[i].header.length + r[i].length; |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
324 | |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
325 | rangeelm = rangeelm->next; |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
326 | i++; |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
327 | } |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
328 | |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
329 | response_len += seplen + 4; // trailer: sep + '--' + CRLF |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
330 | |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
331 | // finally, set the content-length header |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
332 | pblock_kllinsert( |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
333 | pb_key_content_length, |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
334 | (long long)response_len, |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
335 | rq->srvhdrs); |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
336 | |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
337 | // and start the response |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
338 | http_start_response(sn, rq); |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
339 | |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
340 | rangeelm = range; |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
341 | i = 0; |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
342 | while(rangeelm) { |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
343 | 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
|
344 | // TODO: error |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
345 | } |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
346 | rangeelm = rangeelm->next; |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
347 | i++; |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
348 | } |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
349 | net_printf(sn->csd, "%s--\r\n", sep); |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
350 | |
116
d7a186cf87f6
adds find-pathinfo saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
104
diff
changeset
|
351 | free(r); |
104
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
352 | return 0; |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
353 | } |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
354 | |
10 | 355 | int send_file(pblock *pb, Session *sn, Request *rq) { |
54 | 356 | struct stat s; |
104
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
357 | VFSContext *vfs = vfs_request_context(sn, rq); |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
358 | SYS_FILE fd = prepare_service_file(sn, rq, vfs, &s); |
54 | 359 | if(!fd) { |
44
3da1f7b6847f
added some error messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
21
diff
changeset
|
360 | // if an error occurs, prepare_service_file sets the http status code |
3da1f7b6847f
added some error messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
21
diff
changeset
|
361 | // we can just return REQ_ABORTED |
3da1f7b6847f
added some error messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
21
diff
changeset
|
362 | return REQ_ABORTED; |
6
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
4
diff
changeset
|
363 | } |
54 | 364 | |
104
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
365 | // get and validate range header |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
366 | char *range_header = pblock_findkeyval(pb_key_range, rq->headers); |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
367 | HttpRange *range = NULL; |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
368 | if(range_header) { |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
369 | int status; |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
370 | range = parse_range(sn, range_header, &status); |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
371 | if(status != PROTOCOL_OK) { |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
372 | protocol_status(sn, rq, status, NULL); |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
373 | vfs_close(fd); |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
374 | return REQ_ABORTED; |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
375 | } |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
376 | |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
377 | if(!validate_range(range, &s, &status)) { |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
378 | protocol_status(sn, rq, status, NULL); |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
379 | free_range(sn, range); |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
380 | vfs_close(fd); |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
381 | return REQ_ABORTED; |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
382 | } |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
383 | } |
54 | 384 | |
104
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
385 | int single_range = 1; |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
386 | off_t offset; |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
387 | off_t length; |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
388 | if(range) { |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
389 | protocol_status(sn, rq, 206, NULL); |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
390 | pblock_removekey(pb_key_content_length, rq->srvhdrs); |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
391 | |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
392 | if(range->next) { |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
393 | single_range = 0; |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
394 | } else { |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
395 | range2off(range, s.st_size, &offset, &length); |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
396 | |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
397 | pblock_kllinsert( |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
398 | pb_key_content_length, |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
399 | (long long)length, |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
400 | rq->srvhdrs); |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
401 | |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
402 | sstr_t content_range = ucx_sprintf( |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
403 | "%lld-%lld/%lld", |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
404 | (long long)offset, |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
405 | (long long)offset+length - 1, |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
406 | s.st_size); |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
407 | pblock_kvinsert( |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
408 | pb_key_content_range, |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
409 | content_range.ptr, |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
410 | content_range.length, |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
411 | rq->srvhdrs); |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
412 | free(content_range.ptr); |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
413 | } |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
414 | } else { |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
415 | offset = 0; |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
416 | length = s.st_size; |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
417 | } |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
418 | |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
419 | if(single_range) { |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
420 | // send response header |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
421 | http_start_response(sn, rq); |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
422 | // send content |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
423 | if(send_range(sn, fd, offset, length, NULL, 0)) { |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
424 | // TODO: error |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
425 | } |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
426 | } else { |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
427 | if(send_multi_range(sn, rq, fd, s.st_size, range)) { |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
428 | // TODO: error |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
429 | } |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
430 | } |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
431 | |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
432 | // cleanup |
54 | 433 | vfs_close(fd); |
104
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
434 | free_range(sn, range); |
3
137197831306
minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
435 | |
137197831306
minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
436 | return REQ_PROCEED; |
137197831306
minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
437 | } |
10 | 438 | |
104
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
439 | |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
102
diff
changeset
|
440 | |
10 | 441 | 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
|
442 | pblock_removekey(pb_key_content_type, rq->srvhdrs); |
7b235fa88008
Some fixes for mod_jk
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
443 | pblock_nvinsert("content-type", "text/plain", rq->srvhdrs); |
10 | 444 | pblock_nninsert("content-length", 13, rq->srvhdrs); |
445 | protocol_status(sn, rq, 200, NULL); | |
446 | http_start_response(sn, rq); | |
447 | net_write(sn->csd, "Hello World!\n", 13); | |
448 | return REQ_PROCEED; | |
449 | } | |
12
34aa8001ea53
Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
11
diff
changeset
|
450 | |
127
84e206063b64
adds minimal websocket implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
116
diff
changeset
|
451 | static int ws_msghandler(WebSocket *ws, WSMessage *msg) { |
84e206063b64
adds minimal websocket implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
116
diff
changeset
|
452 | if(msg->type == 1) { |
84e206063b64
adds minimal websocket implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
116
diff
changeset
|
453 | 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
|
454 | websocket_send_text(ws->userdata, "hello", 5); |
84e206063b64
adds minimal websocket implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
116
diff
changeset
|
455 | } else { |
84e206063b64
adds minimal websocket implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
116
diff
changeset
|
456 | 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
|
457 | } |
84e206063b64
adds minimal websocket implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
116
diff
changeset
|
458 | return 0; |
84e206063b64
adds minimal websocket implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
116
diff
changeset
|
459 | } |
84e206063b64
adds minimal websocket implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
116
diff
changeset
|
460 | |
84e206063b64
adds minimal websocket implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
116
diff
changeset
|
461 | 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
|
462 | WebSocket ws; |
84e206063b64
adds minimal websocket implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
116
diff
changeset
|
463 | ZERO(&ws, sizeof(WebSocket)); |
84e206063b64
adds minimal websocket implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
116
diff
changeset
|
464 | ws.userdata = sn->csd; |
84e206063b64
adds minimal websocket implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
116
diff
changeset
|
465 | |
84e206063b64
adds minimal websocket implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
116
diff
changeset
|
466 | ws.on_message = ws_msghandler; |
84e206063b64
adds minimal websocket implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
116
diff
changeset
|
467 | return http_handle_websocket(sn, rq, &ws); |
84e206063b64
adds minimal websocket implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
116
diff
changeset
|
468 | } |
84e206063b64
adds minimal websocket implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
116
diff
changeset
|
469 | |
12
34aa8001ea53
Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
11
diff
changeset
|
470 | int service_index(pblock *pb, Session *sn, Request *rq) { |
47
ce9790523346
server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
471 | //printf("service_index\n"); |
12
34aa8001ea53
Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
11
diff
changeset
|
472 | |
116
d7a186cf87f6
adds find-pathinfo saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
104
diff
changeset
|
473 | char *path = pblock_findkeyval(pb_key_path, rq->vars); |
12
34aa8001ea53
Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
11
diff
changeset
|
474 | char *uri = pblock_findkeyval(pb_key_uri, rq->reqpb); |
34aa8001ea53
Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
11
diff
changeset
|
475 | |
34aa8001ea53
Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
11
diff
changeset
|
476 | sstr_t r_uri = sstr(uri); |
34aa8001ea53
Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
11
diff
changeset
|
477 | |
77 | 478 | // open the file |
55
b7908bf38f9f
vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
54
diff
changeset
|
479 | VFSContext *vfs = vfs_request_context(sn, rq); |
116
d7a186cf87f6
adds find-pathinfo saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
104
diff
changeset
|
480 | VFS_DIR dir = vfs_opendir(vfs, path); |
55
b7908bf38f9f
vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
54
diff
changeset
|
481 | if(!dir) { |
12
34aa8001ea53
Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
11
diff
changeset
|
482 | return REQ_ABORTED; |
34aa8001ea53
Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
11
diff
changeset
|
483 | } |
34aa8001ea53
Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
11
diff
changeset
|
484 | |
77 | 485 | sbuf_t *out = sbuf_new(1024); // output buffer |
12
34aa8001ea53
Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
11
diff
changeset
|
486 | |
77 | 487 | // write html header |
12
34aa8001ea53
Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
11
diff
changeset
|
488 | sbuf_puts(out, "<html>\n<head>\n<title>Index of "); |
34aa8001ea53
Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
11
diff
changeset
|
489 | sbuf_puts(out, uri); |
34aa8001ea53
Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
11
diff
changeset
|
490 | sbuf_puts(out, "</title>\n</head><body>\n<h1>Index of "); |
34aa8001ea53
Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
11
diff
changeset
|
491 | sbuf_puts(out, uri); |
34aa8001ea53
Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
11
diff
changeset
|
492 | sbuf_puts(out, "</h1><hr>\n\n"); |
34aa8001ea53
Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
11
diff
changeset
|
493 | |
77 | 494 | // list directory |
55
b7908bf38f9f
vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
54
diff
changeset
|
495 | VFS_ENTRY f; |
57 | 496 | while(vfs_readdir(dir, &f)) { |
55
b7908bf38f9f
vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
54
diff
changeset
|
497 | sstr_t filename = sstr(f.name); |
12
34aa8001ea53
Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
11
diff
changeset
|
498 | |
34aa8001ea53
Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
11
diff
changeset
|
499 | sbuf_puts(out, "<a href=\""); |
34aa8001ea53
Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
11
diff
changeset
|
500 | sbuf_append(out, r_uri); |
34aa8001ea53
Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
11
diff
changeset
|
501 | sbuf_append(out, filename); |
34aa8001ea53
Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
11
diff
changeset
|
502 | sbuf_puts(out, "\">"); |
34aa8001ea53
Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
11
diff
changeset
|
503 | sbuf_append(out, filename); |
34aa8001ea53
Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
11
diff
changeset
|
504 | sbuf_puts(out, "</a><br>\n"); |
34aa8001ea53
Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
11
diff
changeset
|
505 | } |
34aa8001ea53
Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
11
diff
changeset
|
506 | |
34aa8001ea53
Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
11
diff
changeset
|
507 | sbuf_puts(out, "\n</body>\n</html>\n"); |
34aa8001ea53
Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
11
diff
changeset
|
508 | |
77 | 509 | // send stuff to client |
12
34aa8001ea53
Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
11
diff
changeset
|
510 | pblock_removekey(pb_key_content_type, rq->srvhdrs); |
34aa8001ea53
Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
11
diff
changeset
|
511 | pblock_kvinsert(pb_key_content_type, "text/html", 9, rq->srvhdrs); |
34aa8001ea53
Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
11
diff
changeset
|
512 | pblock_nninsert("content-length", out->length, rq->srvhdrs); |
34aa8001ea53
Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
11
diff
changeset
|
513 | protocol_status(sn, rq, 200, NULL); |
34aa8001ea53
Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
11
diff
changeset
|
514 | http_start_response(sn, rq); |
34aa8001ea53
Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
11
diff
changeset
|
515 | |
34aa8001ea53
Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
11
diff
changeset
|
516 | net_write(sn->csd, out->ptr, out->length); |
34aa8001ea53
Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
11
diff
changeset
|
517 | |
77 | 518 | // close |
55
b7908bf38f9f
vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
54
diff
changeset
|
519 | vfs_closedir(dir); |
48
37a512d7b8f6
fixed some memory leaks
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
47
diff
changeset
|
520 | sbuf_free(out); |
12
34aa8001ea53
Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
11
diff
changeset
|
521 | |
34aa8001ea53
Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
11
diff
changeset
|
522 | return REQ_PROCEED; |
34aa8001ea53
Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
11
diff
changeset
|
523 | } |
20
7b235fa88008
Some fixes for mod_jk
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
524 | |
7b235fa88008
Some fixes for mod_jk
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
525 | 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
|
526 | 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
|
527 | "PROPFIND, PROPPATCH, MKCOL, LOCK, UNLOCK, ACL, REPORT"; |
7b235fa88008
Some fixes for mod_jk
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
528 | char *dav = "1,2,access-control"; |
7b235fa88008
Some fixes for mod_jk
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
529 | |
7b235fa88008
Some fixes for mod_jk
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
530 | pblock_removekey(pb_key_content_type, rq->srvhdrs); |
7b235fa88008
Some fixes for mod_jk
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
531 | pblock_nvinsert("allow", allow, rq->srvhdrs); |
7b235fa88008
Some fixes for mod_jk
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
532 | pblock_nvinsert("dav", dav, rq->srvhdrs); |
7b235fa88008
Some fixes for mod_jk
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
533 | pblock_nninsert("content-length", 0, rq->srvhdrs); |
87
bdec069d2239
fixed pathcheck behavior
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
77
diff
changeset
|
534 | protocol_status(sn, rq, 204, NULL); |
20
7b235fa88008
Some fixes for mod_jk
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
535 | http_start_response(sn, rq); |
7b235fa88008
Some fixes for mod_jk
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
536 | |
7b235fa88008
Some fixes for mod_jk
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
537 | return REQ_PROCEED; |
7b235fa88008
Some fixes for mod_jk
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
538 | } |