Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

543 lines
15 KiB

  1. //+---------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1992 - 1995.
  5. //
  6. // File: resprot.cxx
  7. //
  8. // Contents:
  9. //
  10. // Classes:
  11. //
  12. // Functions:
  13. //
  14. // History: 11-07-1996 JohannP (Johann Posch) Created
  15. //
  16. //----------------------------------------------------------------------------
  17. #include <urlint.h>
  18. #include <stdio.h>
  19. #include <sem.hxx>
  20. #include <wininet.h>
  21. #include "urlcf.hxx"
  22. #include "protbase.hxx"
  23. #include "b4hook.hxx"
  24. #include <tchar.h>
  25. #define MAX_ID 10000
  26. HRESULT LookupProtocolClsIDFromReg(LPCTSTR pszUrl, CLSID *pclsid);
  27. #define SZPROTOCOLROOT "PROTOCOLS\\Handler\\"
  28. #define SZCLASS "CLSID"
  29. #define SZMYPROTOCOL "search"
  30. //+---------------------------------------------------------------------------
  31. //
  32. // Method: CB4Hook::Start
  33. //
  34. // Synopsis:
  35. //
  36. // Arguments: [pwzUrl] --
  37. // [pTrans] --
  38. // [pOIBindInfo] --
  39. // [grfSTI] --
  40. // [dwReserved] --
  41. //
  42. // Returns:
  43. //
  44. // History: 10-29-1996 JohannP (Johann Posch) Created
  45. //
  46. // Notes:
  47. //
  48. //----------------------------------------------------------------------------
  49. STDMETHODIMP CB4Hook::Start(LPCWSTR pwzUrl, IOInetProtocolSink *pTrans, IOInetBindInfo *pOIBindInfo,
  50. DWORD grfSTI, DWORD dwReserved)
  51. {
  52. TransDebugOut((DEB_PROT, "%p _IN CB4Hook::Start\n", this));
  53. HRESULT hr = NOERROR;
  54. CLSID clsid;
  55. TransAssert((!_pProtSink && pOIBindInfo && pTrans));
  56. TransAssert((_pszUrl == NULL));
  57. // have to start the base class to get the bindinfo and the full URL.
  58. hr = CBaseProtocol::Start(pwzUrl,pTrans, pOIBindInfo, grfSTI, dwReserved);
  59. if (hr == NOERROR)
  60. {
  61. // first, check if this is hookable URL.
  62. // return E_USEDEFAULTPROTOCAL if not.
  63. if ((hr = Bind()) == NOERROR)
  64. {
  65. // We need to use the new (cooked) URL if Bind() succeeded.
  66. if ((hr = LookupProtocolClsIDFromReg(_szNewUrl, &clsid)) == NOERROR)
  67. {
  68. IClassFactory *pCF = 0;
  69. hr = CoCreateInstance(clsid, NULL, CLSCTX_INPROC_SERVER,IID_IClassFactory, (void**)&pCF);
  70. if (hr == NOERROR)
  71. {
  72. // Perf:
  73. // we might want to move Create/Release of the HTTP protocl handler out of
  74. // Start/Terminate to Create/Release.
  75. hr = pCF->CreateInstance(NULL, IID_IOInetProtocol, (void **)&_pProt);
  76. if (hr == NOERROR)
  77. {
  78. // ???
  79. // We also may need to implement our own IOInetBindInfo so that we
  80. // can give it the new (cooked) BindInfo.
  81. // We need to pass down the new (cooked) URL if Bind() succeeded.
  82. LPWSTR pwzNewUrl = DupA2W(_szNewUrl);
  83. if (pwzNewUrl)
  84. {
  85. hr = _pProt->Start(pwzNewUrl, pTrans, pOIBindInfo, grfSTI, dwReserved);
  86. delete pwzNewUrl;
  87. }
  88. }
  89. pCF->Release();
  90. }
  91. }
  92. }
  93. }
  94. TransDebugOut((DEB_PROT, "%p OUT CB4Hook::Start (hr:%lx)\n",this, hr));
  95. return hr;
  96. }
  97. //+---------------------------------------------------------------------------
  98. //
  99. // Method: CB4Hook::Continue
  100. //
  101. // Synopsis:
  102. //
  103. // Arguments: [pStateInfoIn] --
  104. //
  105. // Returns:
  106. //
  107. // History: 10-29-1996 JohannP (Johann Posch) Created
  108. //
  109. // Notes:
  110. //
  111. //----------------------------------------------------------------------------
  112. STDMETHODIMP CB4Hook::Continue(PROTOCOLDATA *pStateInfoIn)
  113. {
  114. TransDebugOut((DEB_PROT, "%p _IN CB4Hook::Continue\n", this));
  115. HRESULT hr = E_FAIL;
  116. if (_pProt)
  117. hr = _pProt->Continue(pStateInfoIn);
  118. TransDebugOut((DEB_PROT, "%p OUT CB4Hook::Continue (hr:%lx)\n",this, hr));
  119. return hr;
  120. }
  121. //+---------------------------------------------------------------------------
  122. //
  123. // Method: CB4Hook::Read
  124. //
  125. // Synopsis:
  126. //
  127. // Arguments: [ULONG] --
  128. // [ULONG] --
  129. // [pcbRead] --
  130. //
  131. // Returns:
  132. //
  133. // History: 10-29-1996 JohannP (Johann Posch) Created
  134. //
  135. // Notes:
  136. //
  137. //----------------------------------------------------------------------------
  138. STDMETHODIMP CB4Hook::Read(void *pv,ULONG cb,ULONG *pcbRead)
  139. {
  140. TransDebugOut((DEB_PROT, "%p _IN CB4Hook::Read (cb:%ld)\n", this,cb));
  141. HRESULT hr = NOERROR;
  142. if (_pProt)
  143. hr = _pProt->Read(pv, cb, pcbRead);
  144. TransDebugOut((DEB_PROT, "%p OUT CB4Hook::Read (pcbRead:%ld, hr:%lx)\n",this,*pcbRead, hr));
  145. return hr;
  146. }
  147. //+---------------------------------------------------------------------------
  148. //
  149. // Method: CB4Hook::Seek
  150. //
  151. // Synopsis:
  152. //
  153. // Arguments: [DWORD] --
  154. // [ULARGE_INTEGER] --
  155. // [plibNewPosition] --
  156. //
  157. // Returns:
  158. //
  159. // History: 10-29-1996 JohannP (Johann Posch) Created
  160. //
  161. // Notes: WORK: not done
  162. //
  163. //----------------------------------------------------------------------------
  164. STDMETHODIMP CB4Hook::Seek(LARGE_INTEGER dlibMove,DWORD dwOrigin,ULARGE_INTEGER *plibNewPosition)
  165. {
  166. TransDebugOut((DEB_PROT, "%p _IN CB4Hook::Seek\n", this));
  167. HRESULT hr = NOERROR;
  168. if (_pProt)
  169. hr = _pProt->Seek(dlibMove, dwOrigin, plibNewPosition);
  170. TransDebugOut((DEB_PROT, "%p OUT CB4Hook::Seek (hr:%lx)\n",this, hr));
  171. return hr;
  172. }
  173. //+---------------------------------------------------------------------------
  174. //
  175. // Method: CB4Hook::CB4Hook
  176. //
  177. // Synopsis:
  178. //
  179. // Arguments: (none)
  180. //
  181. // Returns:
  182. //
  183. // History: 1-27-96 JohannP (Johann Posch) Created
  184. //
  185. // Notes:
  186. //
  187. //----------------------------------------------------------------------------
  188. CB4Hook::CB4Hook(REFCLSID rclsid, IUnknown *pUnkOuter, IUnknown **ppUnkInner) : CBaseProtocol(rclsid, pUnkOuter, ppUnkInner)
  189. {
  190. TransDebugOut((DEB_PROT, "%p _IN/OUT CB4Hook::CB4Hook \n", this));
  191. }
  192. //+---------------------------------------------------------------------------
  193. //
  194. // Method: CB4Hook::~CB4Hook
  195. //
  196. // Synopsis:
  197. //
  198. // Arguments: (none)
  199. //
  200. // Returns:
  201. //
  202. // History: 11-09-1996 JohannP (Johann Posch) Created
  203. //
  204. // Notes:
  205. //
  206. //----------------------------------------------------------------------------
  207. CB4Hook::~CB4Hook()
  208. {
  209. TransDebugOut((DEB_PROT, "%p _IN/OUT CB4Hook::~CB4Hook \n", this));
  210. }
  211. // SUPER HACK FUNCTION because InternetCrackUrl is not very good.
  212. // THIS FUNCTION DOES NOT CHECK THE INPUT BUFFER SIZE.
  213. // AND CALLER MUST PROVIDE lpszUrlPath buffer for us to parse.
  214. BOOL
  215. MyCrackUrl(
  216. LPCSTR lpszUrl,
  217. DWORD dwUrlLength,
  218. DWORD dwFlags,
  219. LPURL_COMPONENTSA lpUC
  220. )
  221. {
  222. LPSTR pszBuf = lpUC->lpszUrlPath;
  223. DWORD dwSize = lpUC->dwUrlPathLength;
  224. BOOL ret = FALSE;
  225. if (pszBuf)
  226. {
  227. if (InternetCanonicalizeUrl(lpszUrl, pszBuf, &dwSize, ICU_DECODE | ICU_NO_ENCODE))
  228. {
  229. // find protocol
  230. LPSTR pTmp = StrChr(pszBuf, ':');
  231. if (pTmp)
  232. {
  233. *pTmp = '\0';
  234. if (lpUC->lpszScheme)
  235. {
  236. lstrcpy(lpUC->lpszScheme, pszBuf);
  237. lpUC->dwSchemeLength = pTmp - pszBuf;
  238. }
  239. pszBuf = ++pTmp;
  240. // skip '/'s
  241. while (*pszBuf && (*pszBuf == '/'))
  242. pszBuf++;
  243. // find host name
  244. pTmp = StrChr(pszBuf, '/');
  245. if (lpUC->lpszHostName)
  246. {
  247. if (pTmp)
  248. lpUC->dwHostNameLength = pTmp - pszBuf;
  249. else
  250. lpUC->dwHostNameLength = lstrlen(pszBuf);
  251. // + 1 for the NULL terminator
  252. lstrcpyn(lpUC->lpszHostName, pszBuf, lpUC->dwHostNameLength + 1);
  253. }
  254. // if a '/' was found, the rest is URL path
  255. if (pTmp)
  256. lpUC->dwUrlPathLength = lstrlen(pTmp);
  257. else
  258. lpUC->dwUrlPathLength = 0;
  259. lpUC->lpszUrlPath = pTmp;
  260. ret = TRUE;
  261. }
  262. }
  263. }
  264. return ret;
  265. }
  266. //+---------------------------------------------------------------------------
  267. //
  268. // Method: CB4Hook::Bind
  269. //
  270. // Synopsis:
  271. //
  272. // Arguments: (none)
  273. //
  274. // Returns:
  275. //
  276. // History: 11-09-1996 JohannP (Johann Posch) Created
  277. //
  278. // Notes:
  279. //
  280. //----------------------------------------------------------------------------
  281. STDMETHODIMP CB4Hook::Bind()
  282. {
  283. TransDebugOut((DEB_PROT, "%p _IN CB4Hook::Bind (szUrl >%s< )\n", this, _pszUrl));
  284. HRESULT hr = INET_E_USE_DEFAULT_PROTOCOLHANDLER;
  285. URL_COMPONENTS uc;
  286. TCHAR szScheme[INTERNET_MAX_SCHEME_LENGTH];
  287. TCHAR szHost[INTERNET_MAX_HOST_NAME_LENGTH];
  288. TCHAR szURL[MAX_URL_SIZE];
  289. DWORD dwNeeded;
  290. LPSTR lpSearchName;
  291. LPSTR lpNewName = NULL;
  292. ZeroMemory(&uc, sizeof(uc));
  293. uc.dwStructSize = sizeof(uc);
  294. uc.lpszScheme = szScheme;
  295. uc.dwSchemeLength = ARRAYSIZE(szScheme);
  296. uc.lpszHostName = szHost;
  297. uc.dwHostNameLength = ARRAYSIZE(szHost);
  298. uc.lpszUrlPath = szURL;
  299. uc.dwUrlPathLength = ARRAYSIZE(szURL);
  300. // uc.dwExtraInfoLength ???
  301. // BUGBUG ???
  302. // InternetCrackUrl doesn't work with "search:\\..."
  303. if (MyCrackUrl(_pszUrl, 0, ICU_DECODE, &uc))
  304. {
  305. // TODO:
  306. // process the URL string
  307. if (!lstrcmpi(szScheme, SZMYPROTOCOL))
  308. {
  309. // if this is our "search:" protocol
  310. // BUGBUG
  311. // we need to look up the registry for the default protocol to use.
  312. uc.lpszScheme = NULL;
  313. uc.nScheme = INTERNET_SCHEME_HTTP;
  314. uc.nPort = INTERNET_DEFAULT_HTTP_PORT;
  315. if (uc.dwHostNameLength)
  316. {
  317. lpSearchName = uc.lpszHostName;
  318. uc.dwHostNameLength = 0;
  319. // ???
  320. // should we clear the UrlPath and ExtraInfo
  321. }
  322. else if (uc.dwUrlPathLength)
  323. {
  324. lpSearchName = uc.lpszUrlPath;
  325. uc.lpszUrlPath = NULL;
  326. }
  327. else
  328. lpSearchName = NULL;
  329. if (lpSearchName)
  330. {
  331. // apply the search.
  332. // ==========================================================
  333. if (!lstrcmpi(lpSearchName, "united airline"))
  334. lpNewName = "www.ual.com";
  335. else if (!lstrcmpi(lpSearchName, "foo bar"))
  336. lpNewName = "msw";
  337. // ==========================================================
  338. if (lpNewName)
  339. {
  340. // this is a searchable string
  341. uc.lpszHostName = lpNewName;
  342. dwNeeded = ARRAYSIZE(_szNewUrl);
  343. if (InternetCreateUrl(&uc, 0, _szNewUrl, &dwNeeded))
  344. hr = NOERROR;
  345. }
  346. }
  347. }
  348. else if (uc.nScheme == INTERNET_SCHEME_HTTP)
  349. {
  350. lpSearchName = uc.lpszHostName;
  351. // apply search
  352. // ==========================================================
  353. if (!lstrcmpi(lpSearchName, "united airline"))
  354. lpNewName = "www.ual.com";
  355. else if (!lstrcmpi(lpSearchName, "foo bar"))
  356. lpNewName = "msw";
  357. // ==========================================================
  358. if (lpNewName)
  359. {
  360. // if search succeeded
  361. uc.lpszHostName = lpNewName;
  362. uc.dwHostNameLength = 0;
  363. dwNeeded = ARRAYSIZE(_szNewUrl);
  364. if (InternetCreateUrl(&uc, 0, _szNewUrl, &dwNeeded))
  365. hr = NOERROR;
  366. }
  367. }
  368. }
  369. else
  370. {
  371. DebugBreak();
  372. DWORD dwError = GetLastError();
  373. }
  374. // if (hr == MK_E_SYNTAX)
  375. if (hr == INET_E_USE_DEFAULT_PROTOCOLHANDLER)
  376. {
  377. _pProtSink->ReportResult(hr, 0, 0);
  378. }
  379. TransDebugOut((DEB_PROT, "%p OUT CB4Hook::Bind (hr:%lx)\n", this,hr));
  380. return hr;
  381. }
  382. //+---------------------------------------------------------------------------
  383. //
  384. // Function: LookupProtocolClsIDFromReg
  385. //
  386. // Synopsis: finds a protocol handler class for a given URL
  387. //
  388. // Arguments: [pwzUrl] --
  389. // [pclsid] --
  390. //
  391. // Returns:
  392. //
  393. // History: 11-01-1996 JohannP (Johann Posch) Created
  394. //
  395. // Notes:
  396. //
  397. //----------------------------------------------------------------------------
  398. HRESULT LookupProtocolClsIDFromReg(LPCTSTR pszUrl, CLSID *pclsid)
  399. {
  400. TransDebugOut((DEB_PROT, "API _IN LookupProtocolClsIDFromReg (szUrl >%s< )\n", pszUrl));
  401. HRESULT hr = INET_E_UNKNOWN_PROTOCOL;
  402. DWORD dwType;
  403. TCHAR pszProt[MAX_URL_SIZE + 1];
  404. TransAssert((pszUrl && pclsid));
  405. if (pszUrl)
  406. {
  407. char szDelimiter = ':';
  408. lstrcpy(pszProt, pszUrl);
  409. LPSTR pszDel = StrChr(pszProt, szDelimiter);
  410. if (pszDel)
  411. {
  412. *pszDel = '\0';
  413. // fail if the protocol is "search" so we don't get call recursively.
  414. if (lstrcmpi(pszProt, SZMYPROTOCOL))
  415. {
  416. HKEY hProtocolKey = NULL;
  417. DWORD dwLen = 256;
  418. char szProtocolKey[256];
  419. lstrcpy(szProtocolKey, SZPROTOCOLROOT);
  420. lstrcat(szProtocolKey, pszProt);
  421. if (RegOpenKeyEx(HKEY_CLASSES_ROOT, szProtocolKey, 0, KEY_QUERY_VALUE, &hProtocolKey) == ERROR_SUCCESS)
  422. {
  423. if (RegQueryValueEx(hProtocolKey, SZCLASS, NULL, &dwType, (LPBYTE)szProtocolKey, &dwLen) == ERROR_SUCCESS)
  424. {
  425. LPWSTR pwzClsId = DupA2W(szProtocolKey);
  426. if (pwzClsId)
  427. {
  428. hr = CLSIDFromString(pwzClsId, pclsid);
  429. TransDebugOut((DEB_PROT, "API FOUND LookupProtocolClsIDFromReg(hr:%lx, ClsId:%ws)\n", hr,pwzClsId));
  430. delete pwzClsId;
  431. }
  432. else
  433. {
  434. hr = E_OUTOFMEMORY;
  435. }
  436. }
  437. RegCloseKey(hProtocolKey);
  438. }
  439. }
  440. }
  441. else
  442. {
  443. // look up the registry
  444. hr = MK_E_SYNTAX;
  445. }
  446. }
  447. TransDebugOut((DEB_PROT, "API OUT LookupProtocolClsIDFromReg(hr:%lx)\n", hr));
  448. return hr;
  449. }