UNIXworkcode

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 stbuf; 101 struct stat *finfo = NULL; 102 103 rq->directive_is_cacheable = 1; 104 105 if (path_info != NULL || script_name != NULL || path == NULL) 106 return REQ_NOACTION;// ruslan: bail out right away for performance (no need to go into file cache) 107 108 if (!*path) 109 return REQ_NOACTION; 110 111 /* 112 if ((INTrequest_info_path(path, rq, NULL) == PR_SUCCESS)) 113 return REQ_NOACTION; 114 */ 115 116 rq->directive_is_cacheable = 0; 117 118 path_info = &path[strlen(path) - 1]; 119 120 char *forward = pblock_findkeyval(pb_key_find_pathinfo_forward, rq->vars); 121 char *base = NULL; 122 if (forward) { 123 base = pblock_findval("ntrans-base" , rq -> vars); 124 if (!base) forward = NULL; 125 } 126 127 int path_info_depth = 0; 128 129 if(!forward) { 130 while (1) { 131 /* Change all occurrences of '/' to FILE_PATHSEP for WIN32 */ 132 for( ; path_info != path; --path_info) 133 if (*path_info == FILE_PATHSEP) 134 break; 135 for( ; path_info != path; --path_info) { 136 ++path_info_depth; 137 if (*(path_info - 1) != FILE_PATHSEP) 138 break; 139 } 140 141 if (path_info == path) 142 break; 143 144 *path_info = '\0'; 145 //if ((INTrequest_info_path(path, rq, &finfo) == PR_SUCCESS) && finfo) { 146 struct stat *st = request_stat_path(path, rq); 147 if(st) { 148 finfo = &stbuf; 149 stbuf = *st; 150 free(st); 151 } 152 if(finfo) { 153 //if (finfo->pr.type != PR_FILE_FILE) { 154 if(S_ISDIR(finfo->st_mode)) { 155 *path_info = FILE_PATHSEP; 156 if (set_path_info(rq, pblock_findkeyval(pb_key_uri, rq->reqpb), 0)) { 157 return REQ_PROCEED; 158 } 159 break; 160 } else { 161 set_path_info(rq, pblock_findkeyval(pb_key_uri, rq->reqpb), path_info_depth); 162 return REQ_PROCEED; 163 } 164 } else { 165 *path_info-- = FILE_PATHSEP; 166 } 167 } 168 /* This was changed to support virtual documents */ 169 return REQ_NOACTION; 170 } else { 171 int baselen = strlen(base); 172 if (strncmp(path, base, baselen)) 173 return REQ_NOACTION; 174 175 path_info = &path[baselen]; 176 if (*path_info == '/') 177 path_info++; 178 179 while (1) { 180 for( ; *path_info; ++path_info) 181 if (*path_info == FILE_PATHSEP) 182 break; 183 184 if (!*path_info) { 185 if (set_path_info(rq, pblock_findkeyval(pb_key_uri, rq->reqpb), 0)) { 186 return REQ_PROCEED; 187 } 188 break; 189 } 190 191 *path_info = '\0'; 192 //if ((INTrequest_info_path(path, rq, &finfo) == PR_SUCCESS) && finfo) { 193 struct stat *st = request_stat_path(path, rq); 194 if(st) { 195 stbuf = *st; 196 finfo = &stbuf; 197 free(st); 198 } 199 if(finfo) { 200 //if (finfo->pr.type != PR_FILE_FILE) { 201 if(S_ISDIR(finfo->st_mode)) { 202 *path_info++ = FILE_PATHSEP; 203 } else { 204 char *uri = pblock_findkeyval(pb_key_uri, rq->reqpb); 205 *path_info = FILE_PATHSEP; 206 #ifdef XP_WIN32 207 char *unmpath = pblock_findval("unmuri", rq->vars); 208 if (unmpath) { 209 char *nm = &path_info[strlen(path_info)-1]; 210 char *unm = &unmpath[strlen(unmpath)-1]; 211 while(nm != path_info && unm != unmpath) { 212 if (*nm != *unm) { 213 if (*unm == '\\' && *nm == '/') 214 *nm = *unm; 215 else 216 PR_ASSERT(0); 217 } 218 nm--; 219 unm--; 220 } 221 uri = unmpath; 222 } 223 #endif 224 char *t = path_info; 225 for(; *t; ++t) 226 if (*t == FILE_PATHSEP) 227 ++path_info_depth; 228 *path_info = '\0'; 229 set_path_info(rq, uri, path_info_depth); 230 return REQ_PROCEED; 231 } 232 } 233 else { 234 *path_info = FILE_PATHSEP; 235 break; 236 } 237 } 238 239 /* This was changed to support virtual documents */ 240 return REQ_NOACTION; 241 } 242 } 243