static int getPath(const MyString& cmd, int pos, MyString* p) { int i = pos; while (i < cmd.size() && _istspace(cmd[i])) ++i; int j = i; // 处理以双引号包括的路径 if (cmd[j] == _T('"')) { ++j; while (j < cmd.size() && cmd[j] != _T('"')) ++j; if (j == cmd.size()) { return -1; } else { *p = cmd.substr(i + 1, j - i - 1); return j + 1; } } else { while (j < cmd.size() && !_istspace(cmd[j]) && cmd[j] != _T('/')) ++j; *p = cmd.substr(i, j - i); return j; } }
Vector<MyString> split(const MyString& path) { Vector<MyString> ret; int spos = 0; while (spos < path.size()) { int epos = path.find(_T('\\'), spos); if (epos != -1) { int i = epos + 1; while (i < path.size() && path[i] == _T('\\')) ++i; MyString name(path.substr(spos, epos - spos)); ret.append(name); spos = i; } else if (spos < path.size()) { MyString name(path.substr(spos)); ret.append(name); spos = path.size(); } } return ret; }
bool CDownload::makeAbsolutePath(const char* _myp, const char* _base,MyString& _dest) { MyString p = _myp; MyString header5 = p.Left(5); MyString header17 = p.Left(17); MyString header8 = p.Left(8); MyString header7 = p.Left(7); bool bAbsolute= true; // OG 190110 Added \ for WinCE devices if ( ( p[0]=='\\') || ( p[0]=='/') || ( p[1]==':') || (!header5.CompareNoCase("http:")) ) _dest = p; // absolute path else if (!header17.CompareNoCase("file://localhost/")) _dest = p.substr(17,p.length()-17); // absolute path for opera else if (!header8.CompareNoCase("file:///")) #ifdef WIN32 _dest = p.substr(8,p.length()-8); // absolute path #else _dest = p.substr(7,p.length()-7); // keep the / on mac #endif else if (!header7.CompareNoCase("file://"))
// 不改变路径中的大小写情况 MyString VirtualDiskNode::pathNormalize(MyString path) const { MyString ret; if (!isPath(path)) { assert(0); return ret; } if (isVolumnRelative(path)) { ret = join(m_pwd.substr(0, 2), path); } else if (isRelative(path)) { if (path == _T(".")) { ret = m_pwd; } else if (path == _T("..")) { ret = dirname(m_pwd); } else if (path.startWith(_T("..\\"))) { ret = dirname(m_pwd) + path.substr(2); } else if (path.startWith(_T(".\\"))) { ret = m_pwd + path.substr(1); } // 直接输入名字的情况 else if (!match(path, _T("?:*"))) { ret = m_pwd + _T("\\") + path; } } // 绝对路径中处理:\dir\file的情况 //else if (path.startWith(_T("\\"))) //{ // ret = _T("c:") + path; //} if (path.endWith(_T("\\"))) { ret = ret.substr(0, ret.size() - 1); } return ret; }
MyString dirname(MyString path) { assert(isPath(path)); int pos = path.size() - 1; for (; pos >= 0 && path[pos] != _T('\\'); --pos) { } return path.substr(0, pos); }
static int getSwitch(const MyString& cmd, int pos, MyString* s) { int i = pos; while (i < cmd.size() && !_istgraph(cmd[i])) ++i; // 仅有首个字符为"\" int j = i + 1; while (j < cmd.size() && _istalpha(cmd[j])) ++j; *s = cmd.substr(i, j - i); return j; }
MyString dirname(MyString path) { if (!isPath(path)) { return _T(""); } int pos = path.size() - 1; for (; pos >= 0 && path[pos] != _T('\\'); --pos) { } return path.substr(0, pos); }
void HTTPServer::onIncomingConnection ( SOCKET sock ) { //read data from socket char buffer[1024]; int n; bzero ( buffer, sizeof ( buffer ) ); n = read ( sock,buffer,1024 ); if ( n < 0 ) { mLog ( "ERROR reading from socket", LOG_PERROR ); exit ( 1 ); } //mLog((string)"Read from buffer: " + buffer); MyString data ( buffer ); //data now contains incoming data. string firstLine; firstLine = data.substr ( 0, data.find ( "\r\n" ) ); //we should check if header is correct const string prefix ( "GET" ); const string postfix ( "HTTP/1." ); #ifdef FULLDEBUG mLog ( ( "Data received: " + data ).c_str() ); #endif if ( firstLine.find ( prefix ) != 0 // doesn't start with prefix || firstLine.find ( postfix ) != firstLine.length()-1 - postfix.length() // doesn't end with postfix || firstLine.length() < 14 ) { // length is small // header is incorrect mLog ( "Bad request: " + firstLine ); exit ( 1 ); } else { // header is correct MyString req = firstLine.substr ( 4, firstLine.find ( postfix )-4 ); req.trim(); #if defined(FULLDEBUG) mLog ( "request is:" + req ); mLog ( "first line is:" + firstLine ); #endif onUrlRequested ( req, sock ); } close ( sock ); lock(); openConnCount--; unlock(); }
MyString basename(MyString path) { assert(isPath(path)); int pos = path.size() - 1; for (; pos >= 0 && path[pos] != _T('\\'); --pos) { } if (pos < 0) { return _T(""); } return path.substr(pos + 1); }
// 取得命令名 static int getCmd(const MyString& cmd, int pos, MyString* name) { int i = pos; while (i < cmd.size() && !_istgraph(cmd[i])) ++i; int j = i; while (j < cmd.size() && _istalpha(cmd[j])) ++j; // 仅在识别出类似绝对路径时进行回溯 if ((i + 1) < cmd.size() && _istalpha(cmd[i]) && cmd[i + 1] == _T(':')) return i; *name = cmd.substr(i, j - i); return j; }
// . // .. // .\ // ..\ // name bool isRelative(MyString path) { assert(!path.isEmpty()); if (path.startWith(_T("."))) { return true; } else { int pos = path.find(_T('\\')); MyString head = path.substr(0, pos); if (!head.isEmpty() && isLegalName(head)) return true; } return false; }
int parse(const MyString& cmd, MyString* name, Vector<MyString>* pathes, Vector<MyString>* switches) { if (name == nullptr || pathes == nullptr || switches == nullptr) { return -1; } pathes->clear(); switches->clear(); int i = getCmd(cmd, 0, name); if (i == cmd.size()) { return 0; } if (*name == _T("mkdir")) { while (_istspace(cmd[i])) ++i; int j = cmd.size() - 1; while (_istspace(cmd[j])) --j; MyString path = cmd.substr(i, j - i + 1); pathes->append(path); return 0; } for (; i < cmd.size();) { if (_istspace(cmd[i])) { ++i; } else if (cmd[i] != _T('/')) { MyString path; i = getPath(cmd, i, &path); if (!isPath(path) && !hasWildcard(path)) { printf("文件名、目录名或卷标语法不正确\n"); *name = _T(""); pathes->clear(); switches->clear(); return -1; } pathes->append(path); } else if (cmd[i] == _T('/')) { MyString s; i = getSwitch(cmd, i, &s); switches->append(s); } else { assert(0); return -1; } } return 0; }