|
1 /* |
|
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
|
3 * |
|
4 * Copyright 2008 Sun Microsystems, Inc. All rights reserved. |
|
5 * |
|
6 * THE BSD LICENSE |
|
7 * |
|
8 * Redistribution and use in source and binary forms, with or without |
|
9 * modification, are permitted provided that the following conditions are met: |
|
10 * |
|
11 * Redistributions of source code must retain the above copyright notice, this |
|
12 * list of conditions and the following disclaimer. |
|
13 * Redistributions in binary form must reproduce the above copyright notice, |
|
14 * this list of conditions and the following disclaimer in the documentation |
|
15 * and/or other materials provided with the distribution. |
|
16 * |
|
17 * Neither the name of the nor the names of its contributors may be |
|
18 * used to endorse or promote products derived from this software without |
|
19 * specific prior written permission. |
|
20 * |
|
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
|
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
|
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER |
|
25 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
|
26 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
|
27 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; |
|
28 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
|
29 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
|
30 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
|
31 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
32 */ |
|
33 |
|
34 /* |
|
35 * open web server code from safs/pcheck.cpp |
|
36 */ |
|
37 |
|
38 #include "pcheck.h" |
|
39 |
|
40 #include "../util/pblock.h" |
|
41 |
|
42 /* --------------------------- pcheck_find_path --------------------------- */ |
|
43 |
|
44 |
|
45 /* |
|
46 * Takes a given path, and figures out if there is any path_info attached. |
|
47 * If no explicit path_info was provided by nametrans, and the file doesn't |
|
48 * exist as specified, it tries to find it by groping through the filesystem. |
|
49 * |
|
50 * This type of implicit path_info cannot be attached to directories. Such |
|
51 * a request will be flagged as not found. |
|
52 * |
|
53 * pb unused. |
|
54 */ |
|
55 |
|
56 static char* find_param(char *uri, char *param) |
|
57 { |
|
58 // Find ;parameters on the end of uri |
|
59 if (param > uri) { |
|
60 if (*param == FILE_PATHSEP) { |
|
61 --param; |
|
62 } |
|
63 while (param > uri && *param != FILE_PATHSEP && *param != ';') { |
|
64 --param; |
|
65 } |
|
66 if (*param == ';') { |
|
67 return param; |
|
68 } |
|
69 } |
|
70 |
|
71 return NULL; |
|
72 } |
|
73 |
|
74 static PRBool set_path_info(Request *rq, char *uri, int path_info_depth) |
|
75 { |
|
76 // Find trailing path components in uri, e.g. the "/baz;qux/quux" in |
|
77 // "/cgi-bin/foo.pl;bar/baz;qux/quux" (here path_info_depth would be 2) |
|
78 char *path_info = &uri[strlen(uri)]; |
|
79 while (path_info > uri && path_info_depth) { |
|
80 --path_info; |
|
81 if (*path_info == FILE_PATHSEP) { |
|
82 --path_info_depth; |
|
83 } |
|
84 } |
|
85 |
|
86 if (*path_info) { |
|
87 pblock_nvinsert("path-info", path_info, rq->vars); |
|
88 return PR_TRUE; |
|
89 } |
|
90 |
|
91 return PR_FALSE; |
|
92 } |
|
93 |
|
94 int pcheck_find_path(pblock *pb, Session *sn, Request *rq) |
|
95 { |
|
96 char *path = pblock_findkeyval (pb_key_path, rq -> vars); |
|
97 char *path_info = pblock_findkeyval (pb_key_path_info , rq -> vars); |
|
98 char *script_name = pblock_findkeyval (pb_key_script_name, rq -> vars); |
|
99 //NSFCFileInfo *finfo = NULL; |
|
100 struct stat *finfo; |
|
101 |
|
102 rq->directive_is_cacheable = 1; |
|
103 |
|
104 if (path_info != NULL || script_name != NULL || path == NULL) |
|
105 return REQ_NOACTION;// ruslan: bail out right away for performance (no need to go into file cache) |
|
106 |
|
107 if (!*path) |
|
108 return REQ_NOACTION; |
|
109 |
|
110 /* |
|
111 if ((INTrequest_info_path(path, rq, NULL) == PR_SUCCESS)) |
|
112 return REQ_NOACTION; |
|
113 */ |
|
114 |
|
115 rq->directive_is_cacheable = 0; |
|
116 |
|
117 path_info = &path[strlen(path) - 1]; |
|
118 |
|
119 char *forward = pblock_findkeyval(pb_key_find_pathinfo_forward, rq->vars); |
|
120 char *base = NULL; |
|
121 if (forward) { |
|
122 base = pblock_findval("ntrans-base" , rq -> vars); |
|
123 if (!base) forward = NULL; |
|
124 } |
|
125 |
|
126 int path_info_depth = 0; |
|
127 |
|
128 if(!forward) { |
|
129 while (1) { |
|
130 /* Change all occurrences of '/' to FILE_PATHSEP for WIN32 */ |
|
131 for( ; path_info != path; --path_info) |
|
132 if (*path_info == FILE_PATHSEP) |
|
133 break; |
|
134 for( ; path_info != path; --path_info) { |
|
135 ++path_info_depth; |
|
136 if (*(path_info - 1) != FILE_PATHSEP) |
|
137 break; |
|
138 } |
|
139 |
|
140 if (path_info == path) |
|
141 break; |
|
142 |
|
143 *path_info = '\0'; |
|
144 //if ((INTrequest_info_path(path, rq, &finfo) == PR_SUCCESS) && finfo) { |
|
145 if((finfo = request_stat_path(path, rq)) != NULL) { |
|
146 //if (finfo->pr.type != PR_FILE_FILE) { |
|
147 if(S_ISDIR(finfo->st_mode)) { |
|
148 *path_info = FILE_PATHSEP; |
|
149 if (set_path_info(rq, pblock_findkeyval(pb_key_uri, rq->reqpb), 0)) { |
|
150 free(finfo); |
|
151 return REQ_PROCEED; |
|
152 } |
|
153 break; |
|
154 } else { |
|
155 set_path_info(rq, pblock_findkeyval(pb_key_uri, rq->reqpb), path_info_depth); |
|
156 free(finfo); |
|
157 return REQ_PROCEED; |
|
158 } |
|
159 } else { |
|
160 *path_info-- = FILE_PATHSEP; |
|
161 } |
|
162 } |
|
163 /* This was changed to support virtual documents */ |
|
164 return REQ_NOACTION; |
|
165 } else { |
|
166 int baselen = strlen(base); |
|
167 if (strncmp(path, base, baselen)) |
|
168 return REQ_NOACTION; |
|
169 |
|
170 path_info = &path[baselen]; |
|
171 if (*path_info == '/') |
|
172 path_info++; |
|
173 |
|
174 while (1) { |
|
175 for( ; *path_info; ++path_info) |
|
176 if (*path_info == FILE_PATHSEP) |
|
177 break; |
|
178 |
|
179 if (!*path_info) { |
|
180 if (set_path_info(rq, pblock_findkeyval(pb_key_uri, rq->reqpb), 0)) { |
|
181 return REQ_PROCEED; |
|
182 } |
|
183 break; |
|
184 } |
|
185 |
|
186 *path_info = '\0'; |
|
187 //if ((INTrequest_info_path(path, rq, &finfo) == PR_SUCCESS) && finfo) { |
|
188 if((finfo = request_stat_path(path, rq)) != NULL) { |
|
189 //if (finfo->pr.type != PR_FILE_FILE) { |
|
190 if(S_ISDIR(finfo->st_mode)) { |
|
191 *path_info++ = FILE_PATHSEP; |
|
192 } else { |
|
193 char *uri = pblock_findkeyval(pb_key_uri, rq->reqpb); |
|
194 *path_info = FILE_PATHSEP; |
|
195 #ifdef XP_WIN32 |
|
196 char *unmpath = pblock_findval("unmuri", rq->vars); |
|
197 if (unmpath) { |
|
198 char *nm = &path_info[strlen(path_info)-1]; |
|
199 char *unm = &unmpath[strlen(unmpath)-1]; |
|
200 while(nm != path_info && unm != unmpath) { |
|
201 if (*nm != *unm) { |
|
202 if (*unm == '\\' && *nm == '/') |
|
203 *nm = *unm; |
|
204 else |
|
205 PR_ASSERT(0); |
|
206 } |
|
207 nm--; |
|
208 unm--; |
|
209 } |
|
210 uri = unmpath; |
|
211 } |
|
212 #endif |
|
213 char *t = path_info; |
|
214 for(; *t; ++t) |
|
215 if (*t == FILE_PATHSEP) |
|
216 ++path_info_depth; |
|
217 *path_info = '\0'; |
|
218 set_path_info(rq, uri, path_info_depth); |
|
219 free(finfo); |
|
220 return REQ_PROCEED; |
|
221 } |
|
222 free(finfo); |
|
223 } |
|
224 else { |
|
225 *path_info = FILE_PATHSEP; |
|
226 break; |
|
227 } |
|
228 } |
|
229 |
|
230 /* This was changed to support virtual documents */ |
|
231 return REQ_NOACTION; |
|
232 } |
|
233 } |