Wed, 16 May 2012 12:47:28 +0200
added event handler
1 | 1 | /* |
2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. | |
3 | * | |
4 | * Copyright 2011 Olaf Wintermann. All rights reserved. | |
5 | * | |
6 | * Redistribution and use in source and binary forms, with or without | |
7 | * modification, are permitted provided that the following conditions are met: | |
8 | * | |
9 | * 1. Redistributions of source code must retain the above copyright | |
10 | * notice, this list of conditions and the following disclaimer. | |
11 | * | |
12 | * 2. Redistributions in binary form must reproduce the above copyright | |
13 | * notice, this list of conditions and the following disclaimer in the | |
14 | * documentation and/or other materials provided with the distribution. | |
15 | * | |
16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | |
17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE | |
20 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | |
21 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | |
22 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | |
23 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | |
24 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
25 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | |
26 | * POSSIBILITY OF SUCH DAMAGE. | |
27 | */ | |
28 | ||
29 | #include <stdio.h> | |
30 | #include <stdlib.h> | |
24
1a7853a4257e
removed some NSPR dependencies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
23
diff
changeset
|
31 | #include <arpa/inet.h> |
9
30e51941a673
Added mod_jk dependencies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
8
diff
changeset
|
32 | |
14
b8bf95b39952
New source folder layout
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
12
diff
changeset
|
33 | #include "../public/nsapi.h" |
b8bf95b39952
New source folder layout
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
12
diff
changeset
|
34 | #include "../util/pool.h" |
b8bf95b39952
New source folder layout
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
12
diff
changeset
|
35 | #include "../util/pblock.h" |
b8bf95b39952
New source folder layout
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
12
diff
changeset
|
36 | #include "../util/io.h" |
b8bf95b39952
New source folder layout
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
12
diff
changeset
|
37 | #include "../util/util.h" |
1 | 38 | #include "httprequest.h" |
18
73aacbf6e492
Added server.conf parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
39 | #include "config.h" |
3
137197831306
minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
40 | #include "vserver.h" |
19
d680536f8c2f
Added configuration manager
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
18
diff
changeset
|
41 | #include "httplistener.h" |
1 | 42 | |
43 | HTTPRequest *http_request_new() { | |
44 | HTTPRequest *req = malloc(sizeof(HTTPRequest)); | |
45 | req->connection = NULL; | |
46 | req->uri.ptr = NULL; | |
47 | ||
48 | HeaderArray *hd = malloc(sizeof(HeaderArray)); | |
49 | hd->next = NULL; | |
50 | hd->len = 0; | |
51 | hd->headers = calloc(16, sizeof(Header)); | |
52 | hd->alloc = 16; | |
53 | ||
54 | req->headers = hd; | |
55 | ||
56 | return req; | |
57 | } | |
58 | ||
59 | int handle_request(HTTPRequest *request) { | |
60 | // handle nsapi request | |
61 | ||
62 | // create pool | |
63 | request->pool = pool_create(); | |
64 | ||
65 | // create nsapi data structures | |
66 | NSAPISession *sn = malloc(sizeof(NSAPISession)); | |
67 | NSAPIRequest *rq = malloc(sizeof(NSAPIRequest)); | |
68 | request->rq = rq; | |
3
137197831306
minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
69 | rq->phase = NSAPIAuthTrans; |
1 | 70 | |
71 | // fill session structure | |
6
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
72 | sn->sys_fd = request->connection->fd; |
1 | 73 | sn->sn.pool = pool_create(); |
74 | sn->sn.csd = stream_new_from_fd(request->connection->fd); | |
9
30e51941a673
Added mod_jk dependencies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
8
diff
changeset
|
75 | sn->sn.client = pblock_create_pool(sn->sn.pool, 8); |
1 | 76 | sn->sn.next = NULL; |
77 | sn->sn.fill = 1; | |
78 | sn->sn.subject = NULL; | |
79 | ||
9
30e51941a673
Added mod_jk dependencies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
8
diff
changeset
|
80 | /* add ip to sn->client pblock */ |
30e51941a673
Added mod_jk dependencies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
8
diff
changeset
|
81 | char ip_str[INET_ADDRSTRLEN]; |
30e51941a673
Added mod_jk dependencies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
8
diff
changeset
|
82 | if(inet_ntop( |
30e51941a673
Added mod_jk dependencies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
8
diff
changeset
|
83 | AF_INET, |
30e51941a673
Added mod_jk dependencies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
8
diff
changeset
|
84 | &request->connection->address.sin_addr, |
30e51941a673
Added mod_jk dependencies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
8
diff
changeset
|
85 | ip_str, |
30e51941a673
Added mod_jk dependencies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
8
diff
changeset
|
86 | INET_ADDRSTRLEN) != NULL) |
30e51941a673
Added mod_jk dependencies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
8
diff
changeset
|
87 | { |
30e51941a673
Added mod_jk dependencies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
8
diff
changeset
|
88 | pblock_kvinsert(pb_key_ip, ip_str, INET_ADDRSTRLEN, sn->sn.client); |
30e51941a673
Added mod_jk dependencies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
8
diff
changeset
|
89 | } |
30e51941a673
Added mod_jk dependencies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
8
diff
changeset
|
90 | |
1 | 91 | // init NSAPI request structure |
92 | if(request_initialize(request->pool, request, rq) != 0) { | |
3
137197831306
minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
93 | printf("Cannot initialize request structure\n"); |
1 | 94 | return 1; |
95 | } | |
96 | ||
3
137197831306
minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
97 | // set default virtual server |
19
d680536f8c2f
Added configuration manager
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
18
diff
changeset
|
98 | rq->vs = request->connection->listener->default_vs.vs; |
3
137197831306
minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
99 | |
1 | 100 | /* Pass request line as "clf-request" */ |
20
7b235fa88008
Some fixes for mod_jk
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
19
diff
changeset
|
101 | /* TODO: with or without \r\n ? */ |
1 | 102 | pblock_kvinsert( |
103 | pb_key_clf_request, | |
104 | request->request_line.ptr, | |
105 | request->request_line.length, | |
106 | rq->rq.reqpb); | |
107 | ||
108 | /* Pass method as "method" in reqpb, and also as method_num */ | |
109 | pblock_kvinsert( | |
110 | pb_key_method, | |
111 | request->method.ptr, | |
112 | request->method.length, | |
113 | rq->rq.reqpb); | |
114 | // TODO: method num | |
115 | //rqRq.rq.method_num = rqHdr->GetMethodNumber(); | |
116 | //PR_ASSERT(rqRq.rq.method_num != -1 || iStatus); | |
117 | ||
118 | /* Pass protocol as "protocol" in reqpb, and also in protv_num */ | |
119 | pblock_kvinsert( | |
120 | pb_key_protocol, | |
121 | request->httpv.ptr, | |
122 | request->httpv.length, | |
123 | rq->rq.reqpb); | |
124 | // TODO: protocol num | |
125 | ||
20
7b235fa88008
Some fixes for mod_jk
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
19
diff
changeset
|
126 | /* |
7b235fa88008
Some fixes for mod_jk
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
19
diff
changeset
|
127 | * get absolute path and query of the request uri |
7b235fa88008
Some fixes for mod_jk
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
19
diff
changeset
|
128 | */ |
7b235fa88008
Some fixes for mod_jk
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
19
diff
changeset
|
129 | // TODO: check for '#' |
7b235fa88008
Some fixes for mod_jk
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
19
diff
changeset
|
130 | sstr_t absPath = request->uri; |
7b235fa88008
Some fixes for mod_jk
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
19
diff
changeset
|
131 | sstr_t query; |
7b235fa88008
Some fixes for mod_jk
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
19
diff
changeset
|
132 | query.length = 0; |
7b235fa88008
Some fixes for mod_jk
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
19
diff
changeset
|
133 | |
7b235fa88008
Some fixes for mod_jk
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
19
diff
changeset
|
134 | for(int i=0;i<request->uri.length;i++) { |
7b235fa88008
Some fixes for mod_jk
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
19
diff
changeset
|
135 | if(request->uri.ptr[i] == '?') { |
7b235fa88008
Some fixes for mod_jk
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
19
diff
changeset
|
136 | /* split uri in path and query */ |
7b235fa88008
Some fixes for mod_jk
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
19
diff
changeset
|
137 | if(absPath.length > i + 2) { |
7b235fa88008
Some fixes for mod_jk
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
19
diff
changeset
|
138 | query.length = absPath.length - i - 1; |
7b235fa88008
Some fixes for mod_jk
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
19
diff
changeset
|
139 | query.ptr = absPath.ptr + i + 1; |
7b235fa88008
Some fixes for mod_jk
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
19
diff
changeset
|
140 | } |
7b235fa88008
Some fixes for mod_jk
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
19
diff
changeset
|
141 | absPath.length = i; |
7b235fa88008
Some fixes for mod_jk
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
19
diff
changeset
|
142 | |
7b235fa88008
Some fixes for mod_jk
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
19
diff
changeset
|
143 | /* Pass any query as 'query' in reqpb */ |
7b235fa88008
Some fixes for mod_jk
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
19
diff
changeset
|
144 | pblock_kvinsert( |
7b235fa88008
Some fixes for mod_jk
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
19
diff
changeset
|
145 | pb_key_query, |
7b235fa88008
Some fixes for mod_jk
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
19
diff
changeset
|
146 | query.ptr, |
7b235fa88008
Some fixes for mod_jk
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
19
diff
changeset
|
147 | query.length, |
7b235fa88008
Some fixes for mod_jk
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
19
diff
changeset
|
148 | rq->rq.reqpb); |
7b235fa88008
Some fixes for mod_jk
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
19
diff
changeset
|
149 | |
7b235fa88008
Some fixes for mod_jk
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
19
diff
changeset
|
150 | break; |
7b235fa88008
Some fixes for mod_jk
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
19
diff
changeset
|
151 | } |
7b235fa88008
Some fixes for mod_jk
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
19
diff
changeset
|
152 | } |
26
37ff8bf54b89
Minimal webdav support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
24
diff
changeset
|
153 | |
1 | 154 | /* Get abs_path part of request URI, and canonicalize the path */ |
155 | absPath.ptr = util_canonicalize_uri( | |
156 | request->pool, | |
157 | absPath.ptr, | |
158 | absPath.length, | |
159 | (int*)&absPath.length); | |
160 | ||
161 | /* Decode the abs_path */ | |
23
a2c8fc23c90e
Added basic authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
22
diff
changeset
|
162 | // TODO: decode abs_path (done?) |
1 | 163 | |
20
7b235fa88008
Some fixes for mod_jk
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
19
diff
changeset
|
164 | /* Pass the abs_path as 'uri' in reqpb */ |
1 | 165 | pblock_kvinsert( |
166 | pb_key_uri, | |
167 | absPath.ptr, | |
168 | absPath.length, | |
169 | rq->rq.reqpb); | |
170 | ||
171 | // pass http header to the NSAPI request structure | |
7
3c2ed7a7a5fd
request and response header now used
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
6
diff
changeset
|
172 | int hlen = request->headers->len; |
3c2ed7a7a5fd
request and response header now used
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
6
diff
changeset
|
173 | HeaderArray *ha = request->headers; |
3c2ed7a7a5fd
request and response header now used
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
6
diff
changeset
|
174 | for(int i=0;i<=hlen;i++) { |
3c2ed7a7a5fd
request and response header now used
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
6
diff
changeset
|
175 | if(i == hlen) { |
3c2ed7a7a5fd
request and response header now used
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
6
diff
changeset
|
176 | ha = ha->next; |
3c2ed7a7a5fd
request and response header now used
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
6
diff
changeset
|
177 | if(ha == NULL) { |
3c2ed7a7a5fd
request and response header now used
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
6
diff
changeset
|
178 | break; |
3c2ed7a7a5fd
request and response header now used
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
6
diff
changeset
|
179 | } |
3c2ed7a7a5fd
request and response header now used
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
6
diff
changeset
|
180 | i = 0; |
3c2ed7a7a5fd
request and response header now used
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
6
diff
changeset
|
181 | hlen = ha->len; |
3c2ed7a7a5fd
request and response header now used
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
6
diff
changeset
|
182 | } |
3c2ed7a7a5fd
request and response header now used
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
6
diff
changeset
|
183 | |
8
f4d56bf9de40
Added request body reader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
7
diff
changeset
|
184 | if(ha->headers[i].name[0] < 90) { |
f4d56bf9de40
Added request body reader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
7
diff
changeset
|
185 | ha->headers[i].name[0] += 32; |
f4d56bf9de40
Added request body reader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
7
diff
changeset
|
186 | } |
20
7b235fa88008
Some fixes for mod_jk
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
19
diff
changeset
|
187 | |
7b235fa88008
Some fixes for mod_jk
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
19
diff
changeset
|
188 | /* change to lower case */ |
7b235fa88008
Some fixes for mod_jk
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
19
diff
changeset
|
189 | char *header_name = ha->headers[i].name; |
7b235fa88008
Some fixes for mod_jk
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
19
diff
changeset
|
190 | for(int i=0;header_name[i]!=0;i++) { |
7b235fa88008
Some fixes for mod_jk
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
19
diff
changeset
|
191 | if(header_name[i] > 64 && header_name[i] < 97) { |
7b235fa88008
Some fixes for mod_jk
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
19
diff
changeset
|
192 | header_name[i] += 32; |
7b235fa88008
Some fixes for mod_jk
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
19
diff
changeset
|
193 | } |
7b235fa88008
Some fixes for mod_jk
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
19
diff
changeset
|
194 | } |
7b235fa88008
Some fixes for mod_jk
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
19
diff
changeset
|
195 | |
7b235fa88008
Some fixes for mod_jk
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
19
diff
changeset
|
196 | pblock_nvinsert( |
7b235fa88008
Some fixes for mod_jk
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
19
diff
changeset
|
197 | ha->headers[i].name, |
7b235fa88008
Some fixes for mod_jk
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
19
diff
changeset
|
198 | ha->headers[i].value, |
7b235fa88008
Some fixes for mod_jk
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
19
diff
changeset
|
199 | rq->rq.headers); |
7
3c2ed7a7a5fd
request and response header now used
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
6
diff
changeset
|
200 | } |
26
37ff8bf54b89
Minimal webdav support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
24
diff
changeset
|
201 | |
8
f4d56bf9de40
Added request body reader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
7
diff
changeset
|
202 | /* check for request body and prepare input buffer */ |
f4d56bf9de40
Added request body reader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
7
diff
changeset
|
203 | char *ctlen_str = pblock_findkeyval(pb_key_content_length, rq->rq.headers); |
f4d56bf9de40
Added request body reader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
7
diff
changeset
|
204 | if(ctlen_str) { |
f4d56bf9de40
Added request body reader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
7
diff
changeset
|
205 | int ctlen = atoi(ctlen_str); |
26
37ff8bf54b89
Minimal webdav support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
24
diff
changeset
|
206 | |
8
f4d56bf9de40
Added request body reader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
7
diff
changeset
|
207 | printf("request body length: %d\n", ctlen); |
f4d56bf9de40
Added request body reader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
7
diff
changeset
|
208 | |
f4d56bf9de40
Added request body reader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
7
diff
changeset
|
209 | netbuf *nb = request->netbuf; |
f4d56bf9de40
Added request body reader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
7
diff
changeset
|
210 | |
f4d56bf9de40
Added request body reader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
7
diff
changeset
|
211 | /* create new netbuf */ |
f4d56bf9de40
Added request body reader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
7
diff
changeset
|
212 | NetIOStream *net_io = (NetIOStream*)net_stream_from_fd( |
f4d56bf9de40
Added request body reader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
7
diff
changeset
|
213 | request->connection->fd); |
f4d56bf9de40
Added request body reader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
7
diff
changeset
|
214 | net_io->max_read = ctlen; |
26
37ff8bf54b89
Minimal webdav support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
24
diff
changeset
|
215 | |
8
f4d56bf9de40
Added request body reader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
7
diff
changeset
|
216 | sn->sn.inbuf = malloc(sizeof(netbuf)); |
f4d56bf9de40
Added request body reader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
7
diff
changeset
|
217 | sn->sn.inbuf->sd = net_io; |
f4d56bf9de40
Added request body reader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
7
diff
changeset
|
218 | sn->sn.inbuf->pos = 0; |
f4d56bf9de40
Added request body reader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
7
diff
changeset
|
219 | |
f4d56bf9de40
Added request body reader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
7
diff
changeset
|
220 | /* prepare buffer */ |
f4d56bf9de40
Added request body reader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
7
diff
changeset
|
221 | int cur_input_len = nb->cursize - nb->pos; |
26
37ff8bf54b89
Minimal webdav support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
24
diff
changeset
|
222 | |
8
f4d56bf9de40
Added request body reader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
7
diff
changeset
|
223 | if(cur_input_len >= ctlen) { |
f4d56bf9de40
Added request body reader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
7
diff
changeset
|
224 | /* |
f4d56bf9de40
Added request body reader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
7
diff
changeset
|
225 | * all data is already in the primary input buffer |
f4d56bf9de40
Added request body reader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
7
diff
changeset
|
226 | * just link the new netbuf to the primary buffer |
f4d56bf9de40
Added request body reader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
7
diff
changeset
|
227 | */ |
f4d56bf9de40
Added request body reader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
7
diff
changeset
|
228 | sn->sn.inbuf->maxsize = ctlen; |
f4d56bf9de40
Added request body reader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
7
diff
changeset
|
229 | sn->sn.inbuf->cursize = ctlen; |
f4d56bf9de40
Added request body reader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
7
diff
changeset
|
230 | sn->sn.inbuf->inbuf = nb->inbuf + nb->pos; |
f4d56bf9de40
Added request body reader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
7
diff
changeset
|
231 | } else { |
f4d56bf9de40
Added request body reader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
7
diff
changeset
|
232 | sn->sn.inbuf->maxsize = (ctlen > 2048) ? (2048) : (ctlen); |
26
37ff8bf54b89
Minimal webdav support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
24
diff
changeset
|
233 | sn->sn.inbuf->inbuf = malloc(sn->sn.inbuf->maxsize); |
8
f4d56bf9de40
Added request body reader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
7
diff
changeset
|
234 | |
f4d56bf9de40
Added request body reader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
7
diff
changeset
|
235 | if(cur_input_len > 0) { |
f4d56bf9de40
Added request body reader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
7
diff
changeset
|
236 | /* we have read a part of the request body -> copy to netbuf */ |
f4d56bf9de40
Added request body reader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
7
diff
changeset
|
237 | memcpy(sn->sn.inbuf->inbuf, nb->inbuf+nb->pos, cur_input_len); |
f4d56bf9de40
Added request body reader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
7
diff
changeset
|
238 | } |
f4d56bf9de40
Added request body reader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
7
diff
changeset
|
239 | |
f4d56bf9de40
Added request body reader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
7
diff
changeset
|
240 | sn->sn.inbuf->cursize = cur_input_len; |
f4d56bf9de40
Added request body reader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
7
diff
changeset
|
241 | } |
f4d56bf9de40
Added request body reader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
7
diff
changeset
|
242 | } else { |
f4d56bf9de40
Added request body reader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
7
diff
changeset
|
243 | sn->sn.inbuf = NULL; |
f4d56bf9de40
Added request body reader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
7
diff
changeset
|
244 | } |
26
37ff8bf54b89
Minimal webdav support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
24
diff
changeset
|
245 | |
37ff8bf54b89
Minimal webdav support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
24
diff
changeset
|
246 | |
1 | 247 | // Send the request to the NSAPI system |
3
137197831306
minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
248 | nsapi_handle_request(sn, rq); |
1 | 249 | |
250 | return 0; | |
251 | } | |
252 | ||
253 | ||
254 | ||
255 | void header_add(HeaderArray *hd, char *name, char *value) { | |
256 | while(hd->len >= hd->alloc) { | |
257 | if(hd->next == NULL) { | |
258 | HeaderArray *block = malloc(sizeof(HeaderArray)); | |
259 | block->next = NULL; | |
260 | block->len = 0; | |
261 | block->headers = calloc(16, sizeof(Header)); | |
262 | block->alloc = 16; | |
263 | hd->next = block; | |
264 | } | |
265 | hd = hd->next; | |
266 | } | |
267 | hd->headers[hd->len].name = name; | |
268 | hd->headers[hd->len].value = value; | |
269 | hd->len++; | |
270 | } | |
271 | ||
3
137197831306
minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
272 | |
137197831306
minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
273 | /* |
137197831306
minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
274 | * NSAPI Processing |
137197831306
minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
275 | * TODO: add this to new file |
137197831306
minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
276 | */ |
137197831306
minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
277 | |
137197831306
minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
278 | int nsapi_handle_request(NSAPISession *sn, NSAPIRequest *rq) { |
137197831306
minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
279 | // TODO: threadpool |
137197831306
minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
280 | |
137197831306
minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
281 | int r = REQ_NOACTION; |
137197831306
minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
282 | |
137197831306
minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
283 | do { |
137197831306
minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
284 | switch(rq->phase) { |
137197831306
minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
285 | case NSAPIAuthTrans: { |
22
adb0bda54e6b
Server can run as daemon
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
21
diff
changeset
|
286 | r = nsapi_authtrans(sn, rq); |
adb0bda54e6b
Server can run as daemon
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
21
diff
changeset
|
287 | if(r != REQ_PROCEED) { |
adb0bda54e6b
Server can run as daemon
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
21
diff
changeset
|
288 | break; |
adb0bda54e6b
Server can run as daemon
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
21
diff
changeset
|
289 | } |
3
137197831306
minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
290 | rq->phase++; |
6
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
291 | nsapi_context_next_stage(&rq->context); |
3
137197831306
minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
292 | } |
137197831306
minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
293 | case NSAPINameTrans: { |
21
627b09ee74e4
New configuration loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
20
diff
changeset
|
294 | //printf(">>> NameTrans\n"); |
3
137197831306
minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
295 | r = nsapi_nametrans(sn, rq); |
137197831306
minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
296 | if(r != REQ_PROCEED) { |
137197831306
minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
297 | break; |
137197831306
minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
298 | } |
137197831306
minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
299 | rq->phase++; |
6
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
300 | nsapi_context_next_stage(&rq->context); |
3
137197831306
minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
301 | } |
137197831306
minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
302 | case NSAPIPathCheck: { |
21
627b09ee74e4
New configuration loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
20
diff
changeset
|
303 | //printf(">>> PathCheck\n"); |
23
a2c8fc23c90e
Added basic authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
22
diff
changeset
|
304 | r = nsapi_pathcheck(sn, rq); |
a2c8fc23c90e
Added basic authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
22
diff
changeset
|
305 | if(r != REQ_PROCEED) { |
a2c8fc23c90e
Added basic authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
22
diff
changeset
|
306 | break; |
a2c8fc23c90e
Added basic authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
22
diff
changeset
|
307 | } |
3
137197831306
minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
308 | rq->phase++; |
6
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
309 | nsapi_context_next_stage(&rq->context); |
3
137197831306
minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
310 | } |
5
dbc01588686e
Added parser for obj.conf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
3
diff
changeset
|
311 | case NSAPIObjectType: { |
21
627b09ee74e4
New configuration loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
20
diff
changeset
|
312 | //printf(">>> ObjectType\n"); |
10 | 313 | r = nsapi_objecttype(sn, rq); |
314 | if(r != REQ_PROCEED) { | |
315 | break; | |
316 | } | |
5
dbc01588686e
Added parser for obj.conf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
3
diff
changeset
|
317 | rq->phase++; |
6
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
318 | nsapi_context_next_stage(&rq->context); |
5
dbc01588686e
Added parser for obj.conf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
3
diff
changeset
|
319 | } |
3
137197831306
minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
320 | case NSAPIService: { |
21
627b09ee74e4
New configuration loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
20
diff
changeset
|
321 | //printf(">>> Service\n"); |
3
137197831306
minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
322 | r = nsapi_service(sn, rq); |
137197831306
minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
323 | if(r != REQ_PROCEED) { |
137197831306
minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
324 | break; |
137197831306
minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
325 | } |
137197831306
minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
326 | rq->phase++; |
6
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
327 | nsapi_context_next_stage(&rq->context); |
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
328 | } |
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
329 | case NSAPIAddLog: { |
21
627b09ee74e4
New configuration loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
20
diff
changeset
|
330 | //printf(">>> AddLog\n"); |
6
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
331 | rq->phase++; |
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
332 | nsapi_context_next_stage(&rq->context); |
3
137197831306
minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
333 | } |
137197831306
minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
334 | case REQ_FINISH: { |
21
627b09ee74e4
New configuration loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
20
diff
changeset
|
335 | //printf(">>> Finish\n"); |
3
137197831306
minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
336 | r = nsapi_finish_request(sn, rq); |
137197831306
minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
337 | } |
137197831306
minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
338 | } |
137197831306
minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
339 | } while (r == REQ_RESTART); |
137197831306
minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
340 | |
23
a2c8fc23c90e
Added basic authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
22
diff
changeset
|
341 | r = nsapi_finish_request(sn, rq); |
3
137197831306
minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
342 | |
137197831306
minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
343 | return r; |
137197831306
minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
344 | } |
137197831306
minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
345 | |
137197831306
minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
346 | int nsapi_finish_request(NSAPISession *sn, NSAPIRequest *rq) { |
6
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
347 | // TODO: free memory |
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
348 | close(sn->sys_fd); |
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
349 | |
3
137197831306
minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
350 | return 0; |
137197831306
minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
351 | } |
137197831306
minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
352 | |
22
adb0bda54e6b
Server can run as daemon
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
21
diff
changeset
|
353 | int nsapi_authtrans(NSAPISession *sn, NSAPIRequest *rq) { |
adb0bda54e6b
Server can run as daemon
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
21
diff
changeset
|
354 | HTTPObjectConfig *objconf = rq->vs->objects; |
adb0bda54e6b
Server can run as daemon
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
21
diff
changeset
|
355 | httpd_object *obj = objconf->objects[0]; |
adb0bda54e6b
Server can run as daemon
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
21
diff
changeset
|
356 | dtable *dt = object_get_dtable(obj, NSAPIAuthTrans); |
adb0bda54e6b
Server can run as daemon
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
21
diff
changeset
|
357 | |
adb0bda54e6b
Server can run as daemon
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
21
diff
changeset
|
358 | int ret = rq->context.last_req_code; |
adb0bda54e6b
Server can run as daemon
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
21
diff
changeset
|
359 | for(int i=NCX_DI(rq);i<dt->ndir;i++) { |
adb0bda54e6b
Server can run as daemon
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
21
diff
changeset
|
360 | directive *d = dt->dirs[i]; |
adb0bda54e6b
Server can run as daemon
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
21
diff
changeset
|
361 | |
adb0bda54e6b
Server can run as daemon
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
21
diff
changeset
|
362 | ret = d->func->func(d->param, (Session*)sn, (Request*)rq); |
adb0bda54e6b
Server can run as daemon
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
21
diff
changeset
|
363 | if(ret != REQ_NOACTION) { |
adb0bda54e6b
Server can run as daemon
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
21
diff
changeset
|
364 | /* |
adb0bda54e6b
Server can run as daemon
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
21
diff
changeset
|
365 | * if a saf is still processing, we need to save the context, to |
adb0bda54e6b
Server can run as daemon
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
21
diff
changeset
|
366 | * process this object at a later time |
adb0bda54e6b
Server can run as daemon
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
21
diff
changeset
|
367 | */ |
adb0bda54e6b
Server can run as daemon
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
21
diff
changeset
|
368 | if(ret == REQ_PROCESSING) { |
adb0bda54e6b
Server can run as daemon
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
21
diff
changeset
|
369 | /* save nsapi context */ |
adb0bda54e6b
Server can run as daemon
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
21
diff
changeset
|
370 | /* add +1 to start next round with next function */ |
adb0bda54e6b
Server can run as daemon
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
21
diff
changeset
|
371 | rq->context.dtable_index = i + 1; |
adb0bda54e6b
Server can run as daemon
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
21
diff
changeset
|
372 | } |
adb0bda54e6b
Server can run as daemon
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
21
diff
changeset
|
373 | |
adb0bda54e6b
Server can run as daemon
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
21
diff
changeset
|
374 | return ret; |
adb0bda54e6b
Server can run as daemon
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
21
diff
changeset
|
375 | } |
adb0bda54e6b
Server can run as daemon
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
21
diff
changeset
|
376 | } |
adb0bda54e6b
Server can run as daemon
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
21
diff
changeset
|
377 | |
adb0bda54e6b
Server can run as daemon
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
21
diff
changeset
|
378 | |
adb0bda54e6b
Server can run as daemon
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
21
diff
changeset
|
379 | return REQ_PROCEED; |
adb0bda54e6b
Server can run as daemon
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
21
diff
changeset
|
380 | } |
adb0bda54e6b
Server can run as daemon
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
21
diff
changeset
|
381 | |
3
137197831306
minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
382 | int nsapi_nametrans(NSAPISession *sn, NSAPIRequest *rq) { |
6
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
383 | HTTPObjectConfig *objconf = rq->vs->objects; |
21
627b09ee74e4
New configuration loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
20
diff
changeset
|
384 | //printf("nsapi_nametrans\n"); |
6
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
385 | httpd_objset *objset = objset_create(sn->sn.pool); |
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
386 | rq->rq.os = objset; |
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
387 | /* first object in objconf is the default object TODO: make sure it is */ |
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
388 | objset_add_object(sn->sn.pool, objset, objconf->objects[0]); |
3
137197831306
minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
389 | |
6
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
390 | httpd_object *obj = objset->obj[0]; /* nametrans only in default object */ |
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
391 | dtable *dt = object_get_dtable(obj, NSAPINameTrans); |
3
137197831306
minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
392 | |
6
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
393 | /* execute directives */ |
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
394 | int ret = rq->context.last_req_code; |
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
395 | char *name = NULL; |
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
396 | char *ppath = NULL; |
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
397 | for(int i=NCX_DI(rq);i<dt->ndir;i++) { |
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
398 | directive *d = dt->dirs[i]; |
3
137197831306
minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
399 | |
6
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
400 | ret = d->func->func(d->param, (Session*)sn, (Request*)rq); |
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
401 | |
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
402 | /* check for name or ppath */ |
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
403 | name = pblock_findkeyval(pb_key_name, rq->rq.vars); |
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
404 | ppath = pblock_findkeyval(pb_key_ppath, rq->rq.vars); |
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
405 | |
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
406 | /* add additional objects to the objset */ |
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
407 | if(add_objects(objconf, objset, sn, rq, name, ppath) == REQ_ABORTED) { |
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
408 | fprintf(stderr, "add_objects failed\n"); |
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
409 | return REQ_ABORTED; |
3
137197831306
minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
410 | } |
137197831306
minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
411 | |
6
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
412 | if(ret != REQ_NOACTION) { |
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
413 | /* |
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
414 | * if a saf is still processing, we need to save the context, to |
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
415 | * process this object at a later time |
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
416 | */ |
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
417 | if(ret == REQ_PROCESSING) { |
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
418 | /* save nsapi context */ |
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
419 | /* add +1 to start next round with next function */ |
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
420 | rq->context.dtable_index = i + 1; |
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
421 | } |
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
422 | |
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
423 | return ret; |
3
137197831306
minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
424 | } |
137197831306
minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
425 | } |
137197831306
minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
426 | |
6
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
427 | /* if no function has set the ppath var, translate it to docroot */ |
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
428 | if(ret == REQ_NOACTION && ppath == NULL) { |
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
429 | sstr_t docroot = rq->vs->document_root; |
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
430 | if(docroot.length < 1) { |
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
431 | printf("docroot too short\n"); |
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
432 | return REQ_ABORTED; /* docroot too short */ |
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
433 | } |
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
434 | /* if there is a trailing '/', remove it */ |
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
435 | if(docroot.ptr[docroot.length - 1] == '/') { |
20
7b235fa88008
Some fixes for mod_jk
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
19
diff
changeset
|
436 | docroot.ptr[docroot.length - 1] = 0; // TODO: can I do this? |
6
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
437 | docroot.length--; |
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
438 | } |
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
439 | |
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
440 | sstr_t uri = sstr(pblock_findkeyval(pb_key_uri, rq->rq.reqpb)); |
3
137197831306
minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
441 | |
6
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
442 | sstr_t translated; |
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
443 | translated.length = docroot.length + uri.length; |
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
444 | translated.ptr = alloca(translated.length + 1); |
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
445 | translated = sstrncat(2, translated, docroot, uri); |
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
446 | |
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
447 | pblock_kvinsert( |
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
448 | pb_key_ppath, |
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
449 | translated.ptr, |
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
450 | translated.length, |
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
451 | rq->rq.vars); |
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
452 | } |
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
453 | |
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
454 | return REQ_PROCEED; |
3
137197831306
minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
455 | } |
137197831306
minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
456 | |
23
a2c8fc23c90e
Added basic authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
22
diff
changeset
|
457 | int nsapi_pathcheck(NSAPISession *sn, NSAPIRequest *rq) { |
a2c8fc23c90e
Added basic authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
22
diff
changeset
|
458 | //printf("nsapi_pathcheck\n"); |
a2c8fc23c90e
Added basic authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
22
diff
changeset
|
459 | httpd_objset *objset = rq->rq.os; |
a2c8fc23c90e
Added basic authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
22
diff
changeset
|
460 | |
a2c8fc23c90e
Added basic authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
22
diff
changeset
|
461 | if(NCX_OI(rq) == -1) { |
a2c8fc23c90e
Added basic authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
22
diff
changeset
|
462 | NCX_OI(rq) = objset->pos - 1; |
a2c8fc23c90e
Added basic authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
22
diff
changeset
|
463 | } |
a2c8fc23c90e
Added basic authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
22
diff
changeset
|
464 | |
a2c8fc23c90e
Added basic authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
22
diff
changeset
|
465 | int ret = rq->context.last_req_code; |
a2c8fc23c90e
Added basic authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
22
diff
changeset
|
466 | for(int i=NCX_OI(rq);i>=0;i--) { |
a2c8fc23c90e
Added basic authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
22
diff
changeset
|
467 | httpd_object *obj = objset->obj[i]; |
a2c8fc23c90e
Added basic authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
22
diff
changeset
|
468 | dtable *dt = object_get_dtable(obj, NSAPIPathCheck); |
a2c8fc23c90e
Added basic authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
22
diff
changeset
|
469 | |
a2c8fc23c90e
Added basic authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
22
diff
changeset
|
470 | // execute directives |
a2c8fc23c90e
Added basic authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
22
diff
changeset
|
471 | for(int j=0;j<dt->ndir;j++) { |
a2c8fc23c90e
Added basic authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
22
diff
changeset
|
472 | directive *d = dt->dirs[j]; |
a2c8fc23c90e
Added basic authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
22
diff
changeset
|
473 | |
a2c8fc23c90e
Added basic authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
22
diff
changeset
|
474 | /* execute the saf */ |
a2c8fc23c90e
Added basic authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
22
diff
changeset
|
475 | ret = d->func->func(d->param, (Session*)sn, (Request*)rq); |
a2c8fc23c90e
Added basic authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
22
diff
changeset
|
476 | if(ret != REQ_NOACTION) { |
a2c8fc23c90e
Added basic authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
22
diff
changeset
|
477 | if(ret == REQ_PROCESSING) { |
a2c8fc23c90e
Added basic authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
22
diff
changeset
|
478 | /* save nsapi context */ |
a2c8fc23c90e
Added basic authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
22
diff
changeset
|
479 | rq->context.objset_index = i; |
a2c8fc23c90e
Added basic authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
22
diff
changeset
|
480 | |
a2c8fc23c90e
Added basic authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
22
diff
changeset
|
481 | /* add +1 to start next round with next function */ |
a2c8fc23c90e
Added basic authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
22
diff
changeset
|
482 | rq->context.dtable_index = j + 1; |
a2c8fc23c90e
Added basic authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
22
diff
changeset
|
483 | } else if(ret == REQ_ABORTED) { |
a2c8fc23c90e
Added basic authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
22
diff
changeset
|
484 | if(rq->rq.status_num == PROTOCOL_UNAUTHORIZED) { |
a2c8fc23c90e
Added basic authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
22
diff
changeset
|
485 | // TODO: unify error handling |
a2c8fc23c90e
Added basic authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
22
diff
changeset
|
486 | protocol_start_response((Session*)sn, (Request*)rq); |
a2c8fc23c90e
Added basic authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
22
diff
changeset
|
487 | } |
a2c8fc23c90e
Added basic authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
22
diff
changeset
|
488 | } |
a2c8fc23c90e
Added basic authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
22
diff
changeset
|
489 | |
a2c8fc23c90e
Added basic authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
22
diff
changeset
|
490 | return ret; |
a2c8fc23c90e
Added basic authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
22
diff
changeset
|
491 | } |
a2c8fc23c90e
Added basic authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
22
diff
changeset
|
492 | } |
a2c8fc23c90e
Added basic authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
22
diff
changeset
|
493 | } |
a2c8fc23c90e
Added basic authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
22
diff
changeset
|
494 | |
24
1a7853a4257e
removed some NSPR dependencies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
23
diff
changeset
|
495 | return REQ_PROCEED; |
23
a2c8fc23c90e
Added basic authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
22
diff
changeset
|
496 | } |
a2c8fc23c90e
Added basic authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
22
diff
changeset
|
497 | |
10 | 498 | int nsapi_objecttype(NSAPISession *sn, NSAPIRequest *rq) { |
21
627b09ee74e4
New configuration loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
20
diff
changeset
|
499 | //printf("nsapi_objecttype\n"); |
10 | 500 | httpd_objset *objset = rq->rq.os; |
501 | ||
502 | if(NCX_OI(rq) == -1) { | |
503 | /* object index is undefined -> define correct object index */ | |
504 | NCX_OI(rq) = objset->pos - 1; | |
505 | } | |
506 | ||
507 | int ret = rq->context.last_req_code; | |
508 | for(int i=NCX_OI(rq);i>=0;i--) { | |
509 | httpd_object *obj = objset->obj[i]; | |
510 | dtable *dt = object_get_dtable(obj, NSAPIObjectType); | |
511 | ||
512 | // execute directives | |
513 | for(int j=0;j<dt->ndir;j++) { | |
514 | directive *d = dt->dirs[j]; | |
515 | ||
516 | ret = d->func->func(d->param, (Session*)sn, (Request*)rq); | |
517 | switch(ret) { | |
518 | case REQ_PROCEED: { | |
519 | char *type = pblock_findkeyval( | |
520 | pb_key_content_type, | |
521 | rq->rq.srvhdrs); | |
522 | if(type == NULL) { | |
523 | ret = REQ_NOACTION; | |
524 | break; | |
525 | } | |
526 | return ret; | |
527 | } | |
528 | case REQ_PROCESSING: { | |
529 | /* save nsapi context */ | |
530 | rq->context.objset_index = i; | |
531 | ||
532 | /* add +1 to start next round with next function */ | |
533 | rq->context.dtable_index = j + 1; | |
534 | return ret; | |
535 | } | |
536 | case REQ_NOACTION: { | |
537 | break; | |
538 | } | |
539 | default: { | |
540 | return ret; | |
541 | } | |
542 | } | |
543 | } | |
544 | } | |
545 | ||
546 | /* | |
547 | * No function returned with REQ_PROCEED, but we need a content type. | |
548 | * If the path ends with a '/', we set the content type to | |
549 | * 'internal/directory' so that 'index-common' can serve the content. | |
550 | * Otherwise we set the content type to text/plain | |
551 | */ | |
552 | sstr_t path = sstr(pblock_findkeyval(pb_key_ppath, rq->rq.vars)); | |
553 | sstr_t ct; | |
554 | if(path.ptr[path.length - 1] == '/') { | |
555 | /* directory */ | |
556 | ct = sstrn("internal/directory", 18); | |
557 | } else { | |
558 | ct = sstrn("text/plain", 10); | |
559 | } | |
560 | pblock_kvinsert(pb_key_content_type, ct.ptr, ct.length, rq->rq.srvhdrs); | |
561 | ||
562 | return REQ_PROCEED; | |
563 | } | |
564 | ||
3
137197831306
minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
565 | int nsapi_service(NSAPISession *sn, NSAPIRequest *rq) { |
21
627b09ee74e4
New configuration loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
20
diff
changeset
|
566 | //printf("nsapi_service\n"); |
6
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
567 | httpd_objset *objset = rq->rq.os; |
3
137197831306
minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
568 | |
6
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
569 | if(NCX_OI(rq) == -1) { |
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
570 | NCX_OI(rq) = objset->pos - 1; |
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
571 | } |
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
572 | |
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
573 | int ret = rq->context.last_req_code; |
12
34aa8001ea53
Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
10
diff
changeset
|
574 | char *content_type = NULL; |
20
7b235fa88008
Some fixes for mod_jk
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
19
diff
changeset
|
575 | char *method = NULL; |
6
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
576 | for(int i=NCX_OI(rq);i>=0;i--) { |
12
34aa8001ea53
Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
10
diff
changeset
|
577 | httpd_object *obj = objset->obj[i]; |
3
137197831306
minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
578 | dtable *dt = object_get_dtable(obj, NSAPIService); |
137197831306
minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
579 | |
137197831306
minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
580 | // execute directives |
137197831306
minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
581 | for(int j=0;j<dt->ndir;j++) { |
5
dbc01588686e
Added parser for obj.conf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
3
diff
changeset
|
582 | directive *d = dt->dirs[j]; |
3
137197831306
minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
583 | |
12
34aa8001ea53
Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
10
diff
changeset
|
584 | /* check type parameter */ |
34aa8001ea53
Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
10
diff
changeset
|
585 | char *dtp = pblock_findkeyval(pb_key_type, d->param); |
34aa8001ea53
Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
10
diff
changeset
|
586 | if(dtp) { |
34aa8001ea53
Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
10
diff
changeset
|
587 | /* type parameter for directive */ |
34aa8001ea53
Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
10
diff
changeset
|
588 | if(!content_type) { |
34aa8001ea53
Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
10
diff
changeset
|
589 | content_type = pblock_findkeyval( |
34aa8001ea53
Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
10
diff
changeset
|
590 | pb_key_content_type, |
34aa8001ea53
Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
10
diff
changeset
|
591 | rq->rq.srvhdrs); |
34aa8001ea53
Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
10
diff
changeset
|
592 | } |
34aa8001ea53
Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
10
diff
changeset
|
593 | /* compare types */ |
34aa8001ea53
Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
10
diff
changeset
|
594 | if(strcmp(dtp, content_type) != 0) { |
34aa8001ea53
Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
10
diff
changeset
|
595 | continue; |
34aa8001ea53
Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
10
diff
changeset
|
596 | } |
34aa8001ea53
Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
10
diff
changeset
|
597 | } |
34aa8001ea53
Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
10
diff
changeset
|
598 | |
20
7b235fa88008
Some fixes for mod_jk
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
19
diff
changeset
|
599 | /* check method parameter */ |
7b235fa88008
Some fixes for mod_jk
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
19
diff
changeset
|
600 | char *dmt = pblock_findkeyval(pb_key_method, d->param); |
7b235fa88008
Some fixes for mod_jk
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
19
diff
changeset
|
601 | if(dmt) { |
7b235fa88008
Some fixes for mod_jk
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
19
diff
changeset
|
602 | if(!method) { |
7b235fa88008
Some fixes for mod_jk
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
19
diff
changeset
|
603 | method = pblock_findkeyval(pb_key_method, rq->rq.reqpb); |
7b235fa88008
Some fixes for mod_jk
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
19
diff
changeset
|
604 | } |
7b235fa88008
Some fixes for mod_jk
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
19
diff
changeset
|
605 | |
7b235fa88008
Some fixes for mod_jk
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
19
diff
changeset
|
606 | if(!method_match(dmt, method)) { |
7b235fa88008
Some fixes for mod_jk
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
19
diff
changeset
|
607 | continue; |
7b235fa88008
Some fixes for mod_jk
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
19
diff
changeset
|
608 | } |
7b235fa88008
Some fixes for mod_jk
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
19
diff
changeset
|
609 | } |
7b235fa88008
Some fixes for mod_jk
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
19
diff
changeset
|
610 | |
21
627b09ee74e4
New configuration loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
20
diff
changeset
|
611 | /* execute the saf */ |
3
137197831306
minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
612 | ret = d->func->func(d->param, (Session*)sn, (Request*)rq); |
20
7b235fa88008
Some fixes for mod_jk
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
19
diff
changeset
|
613 | if(ret != REQ_PROCEED) { |
7b235fa88008
Some fixes for mod_jk
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
19
diff
changeset
|
614 | fprintf(stderr, "saf not proceed\n"); |
7b235fa88008
Some fixes for mod_jk
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
19
diff
changeset
|
615 | } |
6
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
616 | if(ret != REQ_NOACTION) { |
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
617 | if(ret == REQ_PROCESSING) { |
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
618 | /* save nsapi context */ |
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
619 | rq->context.objset_index = i; |
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
620 | |
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
621 | /* add +1 to start next round with next function */ |
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
622 | rq->context.dtable_index = j + 1; |
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
623 | } |
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
624 | |
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
625 | return ret; |
3
137197831306
minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
626 | } |
137197831306
minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
627 | } |
137197831306
minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
628 | } |
137197831306
minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
629 | |
137197831306
minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
630 | return ret; |
137197831306
minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
631 | } |
137197831306
minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
632 | |
20
7b235fa88008
Some fixes for mod_jk
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
19
diff
changeset
|
633 | |
7b235fa88008
Some fixes for mod_jk
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
19
diff
changeset
|
634 | /* |
7b235fa88008
Some fixes for mod_jk
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
19
diff
changeset
|
635 | * checks if the method matches the cmp string |
7b235fa88008
Some fixes for mod_jk
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
19
diff
changeset
|
636 | * the format of cmp is a single method string or a list of methods |
7b235fa88008
Some fixes for mod_jk
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
19
diff
changeset
|
637 | * (method1|method2|method3|...) |
7b235fa88008
Some fixes for mod_jk
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
19
diff
changeset
|
638 | */ |
7b235fa88008
Some fixes for mod_jk
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
19
diff
changeset
|
639 | int method_match(char *cmp, char *method) { |
7b235fa88008
Some fixes for mod_jk
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
19
diff
changeset
|
640 | if(cmp[0] != '(') { |
7b235fa88008
Some fixes for mod_jk
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
19
diff
changeset
|
641 | /* not a list of methods, so just compare the 2 strings */ |
7b235fa88008
Some fixes for mod_jk
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
19
diff
changeset
|
642 | if(!strcmp(cmp, method)) { |
7b235fa88008
Some fixes for mod_jk
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
19
diff
changeset
|
643 | return 1; |
7b235fa88008
Some fixes for mod_jk
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
19
diff
changeset
|
644 | } |
7b235fa88008
Some fixes for mod_jk
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
19
diff
changeset
|
645 | } else if(cmp[0] == 0) { |
7b235fa88008
Some fixes for mod_jk
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
19
diff
changeset
|
646 | /* empty string */ |
7b235fa88008
Some fixes for mod_jk
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
19
diff
changeset
|
647 | return 0; |
7b235fa88008
Some fixes for mod_jk
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
19
diff
changeset
|
648 | } |
7b235fa88008
Some fixes for mod_jk
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
19
diff
changeset
|
649 | |
7b235fa88008
Some fixes for mod_jk
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
19
diff
changeset
|
650 | size_t method_len = strlen(method); |
7b235fa88008
Some fixes for mod_jk
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
19
diff
changeset
|
651 | size_t last_pos = 0; |
7b235fa88008
Some fixes for mod_jk
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
19
diff
changeset
|
652 | cmp++; /* skip '(' */ |
7b235fa88008
Some fixes for mod_jk
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
19
diff
changeset
|
653 | for(int i=0;cmp[i]!=0;i++) { |
7b235fa88008
Some fixes for mod_jk
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
19
diff
changeset
|
654 | char c = cmp[i]; |
7b235fa88008
Some fixes for mod_jk
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
19
diff
changeset
|
655 | if(c == '|' || c == ')') { |
7b235fa88008
Some fixes for mod_jk
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
19
diff
changeset
|
656 | size_t len = i - last_pos; |
7b235fa88008
Some fixes for mod_jk
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
19
diff
changeset
|
657 | if(len == method_len) { |
7b235fa88008
Some fixes for mod_jk
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
19
diff
changeset
|
658 | char *cp = cmp + last_pos; |
7b235fa88008
Some fixes for mod_jk
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
19
diff
changeset
|
659 | if(!memcmp(cp, method, len)) { |
7b235fa88008
Some fixes for mod_jk
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
19
diff
changeset
|
660 | return 1; |
7b235fa88008
Some fixes for mod_jk
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
19
diff
changeset
|
661 | } |
7b235fa88008
Some fixes for mod_jk
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
19
diff
changeset
|
662 | } |
7b235fa88008
Some fixes for mod_jk
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
19
diff
changeset
|
663 | last_pos = i + 1; |
7b235fa88008
Some fixes for mod_jk
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
19
diff
changeset
|
664 | } |
7b235fa88008
Some fixes for mod_jk
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
19
diff
changeset
|
665 | } |
7b235fa88008
Some fixes for mod_jk
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
19
diff
changeset
|
666 | |
7b235fa88008
Some fixes for mod_jk
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
19
diff
changeset
|
667 | return 0; |
7b235fa88008
Some fixes for mod_jk
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
19
diff
changeset
|
668 | } |
7b235fa88008
Some fixes for mod_jk
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
19
diff
changeset
|
669 | |
6
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
670 | /* |
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
671 | * adds objects with specific name or path to the httpd_objset |
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
672 | */ |
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
673 | int add_objects( |
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
674 | HTTPObjectConfig *objs, |
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
675 | httpd_objset *os, |
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
676 | NSAPISession *sn, |
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
677 | NSAPIRequest *rq, |
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
678 | char *name, |
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
679 | char *path) |
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
680 | { |
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
681 | /* first, add all objects with a matching path */ |
10 | 682 | /* TODO */ |
6
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
683 | |
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
684 | |
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
685 | /* add object with object with matching name */ |
10 | 686 | if(name == NULL) { |
687 | return REQ_PROCEED; | |
688 | } | |
689 | ||
690 | for(int i=0;i<objs->nobj;i++) { | |
691 | httpd_object *obj = objs->objects[i]; | |
692 | ||
693 | if(obj->name && !strcmp(name, obj->name)) { | |
694 | objset_add_object(sn->sn.pool, os, obj); | |
695 | } | |
696 | } | |
6
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
697 | |
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
698 | return REQ_PROCEED; |
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
699 | } |