/* normalize a file name : also called by robots.txt parser
* return true if it is ok, false otherwise (cgi-bin)
*/
bool fileNormalize (char *file) {
int i=0;
while (file[i] != 0 && file[i] != '#') {
if (file[i] == '/') {
if (file[i+1] == '.' && file[i+2] == '/') {
// suppress /./
int j=i+3;
while (file[j] != 0) {
file[j-2] = file[j];
j++;
}
file[j-2] = 0;
} else if (file[i+1] == '/') {
// replace // by /
int j=i+2;
while (file[j] != 0) {
file[j-1] = file[j];
j++;
}
file[j-1] = 0;
} else if (file[i+1] == '.' && file[i+2] == '.' && file[i+3] == '/') {
// suppress /../
if (i == 0) {
// the file name starts with /../ : error
return false;
} else {
int j = i+4, dec;
i--;
while (file[i] != '/') { i--; }
dec = i+1-j; // dec < 0
while (file[j] != 0) {
file[j+dec] = file[j];
j++;
}
file[j+dec] = 0;
}
} else if (file[i+1] == '.' && file[i+2] == 0) {
// suppress /.
file[i+1] = 0;
return true;
} else if (file[i+1] == '.' && file[i+2] == '.' && file[i+3] == 0) {
// suppress /..
if (i == 0) {
// the file name starts with /.. : error
return false;
} else {
i--;
while (file[i] != '/') {
i--;
}
file[i+1] = 0;
return true;
}
} else { // nothing special, go forward
i++;
}
} else if (file[i] == '%') {
int v1 = int_of_hexa(file[i+1]);
int v2 = int_of_hexa(file[i+2]);
if (v1 < 0 || v2 < 0) return false;
char c = 16 * v1 + v2;
if (isgraph(c)) {
file[i] = c;
int j = i+3;
while (file[j] != 0) {
file[j-2] = file[j];
j++;
}
file[j-2] = 0;
i++;s
} else if (c == ' ' || c == '/') { // keep it with the % notation
i += 3;
} else { // bad url
return false;
}
} else { // nothing special, go forward
i++;
}
}
file[i] = 0;
return true;
}
对url格式化
最新推荐文章于 2022-07-24 18:09:55 发布