📄 warurl.cpp
字号:
#include "StdAfx.h"#ifndef WAR_ASSERT_H_INCLUDED# define WAR_ASSERT_H_INCLUDED# include <assert.h>#endif#ifndef WAR_MALLOC_H_INCLUDED# define WAR_MALLOC_H_INCLUDED# include <malloc.h>#endif#include "WarUrl.h" // class implemented#ifndef WAR_VECTOR_INCLUDED# include <vector>#endif#ifndef WAR_UTF8_H# include "WarUtf8.h"#endifusing namespace std;/////////////////////////////// PUBLIC ///////////////////////////////////////WarUrl& operator << (WarUrl& path, const war_svrpath_t& from){ path.Add(from); return path;}WarUrl& operator << (WarUrl& path, const WarPathE from){ path.Add(from); return path;}const char WarUrl::msWarHexToCharMap[127] ={ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, // 0 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, // 10 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, // 20 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, // 30 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01, // 40 0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x00,0x00, // 50 0x00,0x00,0x00,0x00,0x00,0x0a,0x0b,0x0c,0x0d,0x0e, // 60 0x0f,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, // 70 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, // 80 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x0a,0x0b,0x0c, // 90 0x0d,0x0e,0x0f,0x00,0x00,0x00,0x00,0x00,0x00,0x00, // 100 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, // 110 0x00,0x00,0x00,0x00,0x00,0x00,0x00 // 120};//============================= LIFECYCLE ====================================//============================= OPERATORS ====================================//============================= OPERATIONS ===================================void WarUrl::SetHost(war_ccstr_t newHost) throw (WarException){ mHostName = ""; if (!newHost || !*newHost) goto rebuild_url; if (isalnum(*newHost)) { while ((*newHost != ':') && (*newHost != '/')) mHostName += *newHost++; } if (':' == *newHost) mPort = atoi(++newHost);rebuild_url: SetFlagsFromValues(); BuildUrl();}void WarUrl::Create(const WarUrl& defaultUrl, war_ccstr_t strUrl) throw(WarException){ operator = (defaultUrl); ParseString(strUrl, true);}void WarUrl::Create(war_ccstr_t type_name, war_ccstr_t UserName, war_ccstr_t Password, war_port_t Port, war_ccstr_t Host, war_ccstr_t UrlPath, war_ccstr_t Command) throw(WarException){ WarUrlType url_type; url_type.Create(type_name); Create(url_type, UserName, Password, Port, Host, UrlPath, Command);}void WarUrl::Create(const WarUrlType& Type, war_ccstr_t UserName, war_ccstr_t Password, war_port_t Port, war_ccstr_t Host, war_ccstr_t UrlPath, war_ccstr_t Command) throw(WarException){ mUrlType = Type; if (UserName && *UserName) { mUserName = UserName; mHaveUserName = true; } if (Password && *Password) { mPassword = Password; mHavePassword = true; } if (Port) { mPort = Port; mHavePort = true; } if (Host && *Host) { mHostName = Host; mHaveHostName = true; } if (UrlPath && *UrlPath) { mUrlPath = UrlPath; mHaveUrlPath = true; } if (Command && *Command) { mCommand = Command; mHaveCommand = true; } BuildUrl();}void WarUrl::Reset(){ mUrlType.Reset(); mPort = 0; mUserName = ""; mPassword = ""; mHostName = ""; mUrlPath = ""; mUrl = ""; mCommand = ""; mFilePath.Reset(); mQueryList.erase(mQueryList.begin(), mQueryList.end()); mHavePort = 0; mHaveUserName = 0; mHavePassword = 0; mHaveHostName = 0; mHaveUrlPath = 0; mHaveCommand = 0;}void WarUrl::Normalize(){ mFilePath.Normalize(); mFilePath.RemovePaddingSlash(); if (mFilePath.mString.find(mFilePath.GetSlash()) == mFilePath.mString.npos) mFilePath << WAR_SYSSLASH;}void WarUrl::ErasePath(){ SetFlagsFromValues(); mHaveUrlPath = false; BuildUrl();}//============================= ACCESS ===================================//============================= INQUIRY ===================================/////////////////////////////// PROTECTED ////////////////////////////////////////////////////////////////// PRIVATE ///////////////////////////////////void WarUrl::ParseString(war_ccstr_t src, bool doRelative)throw(WarException){ if (doRelative) { if (!src || !*src) return; // Use the current value } else Reset(); size_t url_len = 0; war_ccstr_t p = src; size_t len = strlen(src) + 8; vector<char> tmp_buffer(len); war_cstr_t buffer = (war_cstr_t)&tmp_buffer.front(); war_cstr_t pp = buffer; war_ccstr_t pseg1 = NULL, pseg2 = NULL; if (doRelative) { // See if the protocol is identified war_ccstr_t p = src; if (isalpha(*p)) { while(isalpha(*p)) ++p; if ((*p == ':') || ((p[0] == '/') && (p[1] == '/') && (p[2] == ':'))) { // Looks like a protocol identifier // Use default processing. ; } else {consider_relative_path: // The protocol identifier is missing, and we // are doing relative URL's. This is probarbly // path and/or command/query, so we goes directly // to that part. if (!mUrlType.IsEmpty()) { SetFlagsFromValues(); goto skip_hostname; } } } else goto consider_relative_path; } try { WarUrlType url_type; url_type.Create(src); mUrlType = url_type; url_len = mUrlType.GetLenght(); } catch(WarException& ex) { if (!mUrlType.IsEmpty()) { // Use default mUrlType.Create(GetTypeName().c_str()); p += mUrlType.GetLenght(); goto fetch_path; } else { // Assume file if no type is set#ifdef WIN32 if (ex == WAR_ERR_INVALID_URL) { if (!(src && isalpha(*src) && (src[1] == ':') && ((src[2] == '\\') || (src[2] == '/')) && ((src[3] != '\\') && (src[3] != '/')))) throw ex; } else#endif { if (ex != WAR_ERR_NOT_AN_URL) throw ex; } mUrlType.Create("file:///"); goto skip_hostname; } } p += url_len; if (mUrlType.IsHostscanRequiered() && (*p != '/')) { // We have host, or username, or password, or something... // Extract the segment... pseg1 = pp = buffer; bool bHaveAlpha = false, bHaveCol = false; while(*p && (*p != '/')) { switch(*p) { case ':': ++p; if (bHaveCol) WarThrow(WarError(WAR_ERR_INVALID_URL), NULL); bHaveCol = true; *pp++ = 0; pseg2 = pp; break; case '@': ++p; if (bHaveAlpha) WarThrow(WarError(WAR_ERR_INVALID_URL), NULL); bHaveAlpha = true; *pp++ = 0; mHaveUserName = true; if (*pseg1) Decode(mUserName, pseg1); if (bHaveCol) { // second segment is password mHavePassword = true; assert(pseg2 != NULL); if (*pseg2) Decode(mPassword.mString, pseg2); } // Reset bHaveCol = false; pseg1 = pp; pseg2 = NULL; break; default: *pp++ = *p++; break; } } *pp = 0; if (*pseg1) { mHaveHostName = true; Decode(mHostName, pseg1); } if (bHaveCol) { mHavePort = true; if (!*pseg2) WarThrow(WarError(WAR_ERR_INVALID_URL), NULL); mPort = atoi(pseg2);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -