PATH_INFO

This commit is contained in:
2018-06-04 21:36:01 +02:00
parent 0222c0e202
commit e785e3093e
2 changed files with 48 additions and 5 deletions

View File

@ -59,9 +59,27 @@ URI::URI(string webroot, string reqpath) {
} }
this->webroot = webroot; this->webroot = webroot;
this->reqpath = reqpath; this->reqpath = reqpath;
this->info = "";
string abs = reqpath; info = "";
relpath = reqpath;
while (!fileExists(webroot + relpath) && !fileExists(webroot + relpath + ".php")) {
long slash = relpath.find_last_of('/');
if (slash == string::npos) {
break;
}
info = relpath.substr(slash) + info;
relpath.erase(slash);
}
cout << "info: " << info << endl;
cout << "rel: " << relpath << endl;
if (!info.empty() && isDirectory(webroot + relpath)) {
relpath.append("/");
}
string abs = relpath;
if (fileExists(webroot + abs)) { if (fileExists(webroot + abs)) {
string ext = getExtension(abs); string ext = getExtension(abs);
if (ext == "php" || ext == "html") { if (ext == "php" || ext == "html") {
@ -74,9 +92,10 @@ URI::URI(string webroot, string reqpath) {
abs.erase(abs.length() - fname.length() - 1, abs.length()); abs.erase(abs.length() - fname.length() - 1, abs.length());
} }
this->filepath = webroot + reqpath; this->filepath = webroot + relpath;
if (isDirectory(webroot + abs)) { if (isDirectory(webroot + abs)) {
cout << "DIR" << endl;
if (abs[abs.length() - 1] != '/') { if (abs[abs.length() - 1] != '/') {
abs += "/"; abs += "/";
} }
@ -99,6 +118,29 @@ URI::URI(string webroot, string reqpath) {
} }
} }
cout << "Before rel: "<<relpath << endl;
cout << "'" << info << "'" << endl;
cout << info.empty() << endl;
if (isStatic() && !info.empty()) {
if (relpath[relpath.length() - 1] == '/') {
relpath.erase(relpath.length() - 1);
}
newpath = relpath + info;
filepath = "";
} else if (relpath != reqpath) {
if (!info.empty()) {
info.erase(0,1);
}
newpath = relpath + info;
} else {
newpath = "";
}
cout << newpath << endl;
cout << relpath << endl;
cout << reqpath << endl;
} }
string URI::getWebRoot() { string URI::getWebRoot() {
@ -129,8 +171,8 @@ string URI::getNewPath() {
return getRelativePath(); return getRelativePath();
} }
} }
if (relpath != reqpath) { if (!newpath.empty() && newpath != reqpath) {
return url_encode(relpath) + (queryinit? "?" + query : ""); return url_encode(newpath) + (queryinit? "?" + query : "");
} else { } else {
return ""; return "";
} }

View File

@ -14,6 +14,7 @@ private:
string query; string query;
string info; string info;
string filepath; string filepath;
string newpath;
bool queryinit; bool queryinit;
public: public: