00001
00002
00003
00004
00005
00006 #include "stdafx.h"
00007 #include "downloadproperties.h"
00008 #include "misc.h"
00009
00010 void DecodeURLPath (LPSTR pszPath)
00011 {
00012 char szURL [10000];
00013 strcpy (szURL, "ftp://f.c");
00014 if (*pszPath != '/' && *pszPath != '\\')
00015 strcat (szURL, "/");
00016 strcat (szURL, pszPath);
00017
00018 fsURL url;
00019 if (IR_SUCCESS == url.Crack (szURL))
00020 {
00021 if (*pszPath != '/' && *pszPath != '\\')
00022 strcpy (pszPath, url.GetPath ()+1);
00023 else
00024 strcpy (pszPath, url.GetPath ());
00025 }
00026 }
00027
00028 BOOL fsFilePathFromUrlPath (LPCSTR pszUrl, BOOL bUsingFTP, BOOL bDecode, LPSTR pszBuf, UINT uSize)
00029 {
00030 *pszBuf = 0;
00031
00032 if (*pszUrl == 0)
00033 return TRUE;
00034
00035 int end = bUsingFTP ? strlen (pszUrl)-1 : strcspn (pszUrl, "?=#") - 1;
00036
00037 if (end >= (int)uSize)
00038 return FALSE;
00039
00040 CopyMemory (pszBuf, pszUrl, end+1);
00041 pszBuf [end+1] = 0;
00042
00043 if (bUsingFTP == FALSE && bDecode)
00044 DecodeURLPath (pszBuf);
00045
00046 return TRUE;
00047 }
00048
00049 BOOL fsPathFromUrlPath (LPCSTR pszUrl, BOOL bUsingFTP, BOOL bDecode, LPSTR pszBuf, UINT uSize)
00050 {
00051 *pszBuf = 0;
00052
00053 if (*pszUrl == 0)
00054 return TRUE;
00055
00056 int end = bUsingFTP ? strlen (pszUrl)-1 : strcspn (pszUrl, "?#") - 1;
00057
00058 if (end >= (int)uSize)
00059 return FALSE;
00060
00061 CopyMemory (pszBuf, pszUrl, end+1);
00062 pszBuf [end+1] = 0;
00063
00064 LPSTR pszEnd = max (strrchr (pszBuf, '/'), strrchr (pszBuf, '\\'));
00065
00066 if (pszEnd == NULL)
00067 pszEnd = pszBuf;
00068
00069 *pszEnd = 0;
00070
00071 if (bUsingFTP == FALSE && bDecode)
00072 DecodeURLPath (pszBuf);
00073
00074 return TRUE;
00075 }
00076
00077 BOOL fsIsAnchorInUrl (LPCSTR pszFullUrl, LPSTR* ppszWithoutAnchor, LPCSTR* ppszAnchor = NULL)
00078 {
00079 fsURL url;
00080 if (url.Crack (pszFullUrl) != IR_SUCCESS)
00081 return FALSE;
00082
00083 if (url.GetInternetScheme () != INTERNET_SCHEME_HTTP &&
00084 url.GetInternetScheme () != INTERNET_SCHEME_FTP)
00085 return FALSE;
00086
00087 LPCSTR pszPath = url.GetPath ();
00088 LPCSTR pszA = strchr (pszPath, '#');
00089
00090 if (pszA == NULL)
00091 return FALSE;
00092
00093 int len = pszA - pszPath;
00094
00095 char szUrl [10000]; DWORD dw = sizeof (szUrl);
00096 fsURL url2;
00097 url2.Create (url.GetInternetScheme (), url.GetHostName (), url.GetPort (),
00098 url.GetUserName (), url.GetPassword (), url.GetPath (), szUrl, &dw);
00099
00100 if (ppszWithoutAnchor)
00101 {
00102 int oldlen = strlen (szUrl);
00103 int oldpathlen = strlen (pszPath);
00104 fsnew (*ppszWithoutAnchor, char, oldlen - oldpathlen + len + 1);
00105 CopyMemory (*ppszWithoutAnchor, szUrl, oldlen - oldpathlen + len);
00106 (*ppszWithoutAnchor) [oldlen - oldpathlen + len] = 0;
00107
00108 if (ppszAnchor)
00109 *ppszAnchor = szUrl + oldlen - oldpathlen + len;
00110 }
00111
00112 return TRUE;
00113 }
00114
00115 BOOL fsFileNameFromUrlPath (LPCSTR pszUrl, BOOL bUsingFTP, BOOL bDecode, LPSTR pszBuf, UINT uSize)
00116 {
00117 *pszBuf = 0;
00118
00119 int len = strlen (pszUrl);
00120 int pos;
00121
00122 if (len == 0)
00123 return TRUE;
00124
00125 int end = bUsingFTP ? strlen (pszUrl)-1 : strcspn (pszUrl, "?#") - 1;
00126
00127 if (end < 0)
00128 return FALSE;
00129
00130 if (end >= len)
00131 end = len - 1;
00132
00133 pos = end;
00134
00135
00136 while (pos && pszUrl [pos] != '/' && pszUrl [pos] != '\\')
00137 pos--;
00138
00139 if (pszUrl [pos] != '/' && pszUrl [pos] != '\\')
00140 return FALSE;
00141
00142 if (UINT(len - pos) > uSize)
00143 return FALSE;
00144
00145 strcpy (pszBuf, pszUrl + pos + 1);
00146
00147 pszBuf [end - pos] = 0;
00148
00149 if (bUsingFTP == FALSE && bDecode)
00150 DecodeURLPath (pszBuf);
00151
00152 if (strchr (pszBuf, '\\') || strchr (pszBuf, '/'))
00153 {
00154
00155 char sz [1000] = "";
00156 fsGetFileName (pszBuf, sz);
00157 lstrcpy (pszBuf, sz);
00158 }
00159
00160 return TRUE;
00161 }
00162
00163 BOOL fsIRToStr (fsInternetResult ir, LPSTR pszStr, UINT uMaxSize)
00164 {
00165 LPCSTR pszDesc;
00166
00167 switch (ir)
00168 {
00169 case IR_WININETUNKERROR:
00170 case IR_ERROR:
00171 pszDesc = LS (L_UNKNETERR);
00172 break;
00173
00174 case IR_LOGINFAILURE:
00175 pszDesc = LS (L_INVLOGIN);
00176 break;
00177
00178 case IR_INVALIDPASSWORD:
00179 pszDesc = LS (L_INVPWD);
00180 break;
00181
00182 case IR_SUCCESS:
00183 pszDesc = LS (L_SUCCESS);
00184 break;
00185
00186 case IR_CANTCONNECT:
00187 pszDesc = LS (L_CANTCONNECT);
00188 break;
00189
00190 case IR_FILENOTFOUND:
00191 pszDesc = LS (L_NOTFOUND);
00192 break;
00193
00194 case IR_LOSTCONNECTION:
00195 pszDesc = LS (L_CONNWASLOST);
00196 break;
00197
00198 case IR_TIMEOUT:
00199 pszDesc = LS (L_NOTRESPONSE);
00200 break;
00201
00202 case IR_NAMENOTRESOLVED:
00203 pszDesc = LS (L_NOTRESOLVED);
00204 break;
00205
00206 case IR_RANGESNOTAVAIL:
00207 pszDesc = LS (L_NORESUME);
00208 break;
00209
00210 case IR_DOUBTFUL_RANGESRESPONSE:
00211 pszDesc = LS (L_DOUBTFULRANGESRESPONSE);
00212 break;
00213
00214 case IR_PROXYAUTHREQ:
00215 pszDesc = LS (L_PROXYLOGINREQ);
00216 break;
00217
00218 case IR_EXTERROR:
00219 pszDesc = LS (L_EXTERR);
00220 break;
00221
00222 case IR_SERVERBADREQUEST:
00223 pszDesc = LS (L_BADREQ);
00224 break;
00225
00226 case IR_SERVERUNKERROR:
00227 pszDesc = LS (L_UNKSERVERR);
00228 break;
00229
00230 case IR_CONNECTIONABORTED:
00231 pszDesc = LS (L_CONNABORTED);
00232 break;
00233
00234 case IR_BADURL:
00235 pszDesc = LS (L_BADURL);
00236 break;
00237
00238 case IR_NOINTERNETCONNECTION:
00239 pszDesc = LS (L_NOINETCONN);
00240 break;
00241
00242 case IR_HTTPVERNOTSUP:
00243 pszDesc = LS (L_HTTPNOTSUPP);
00244 break;
00245
00246 default:
00247 pszDesc = LS (L_UNKERR);
00248 }
00249
00250 if (strlen (pszDesc) >= uMaxSize)
00251 return FALSE;
00252
00253 strcpy (pszStr, pszDesc);
00254
00255 return TRUE;
00256 }
00257
00258 #ifndef FDM_DLDR__RAWCODEONLY
00259 DWORD fsNPToSiteValidFor (fsNetworkProtocol np)
00260 {
00261 switch (np)
00262 {
00263 case NP_HTTP:
00264 return SITE_VALIDFOR_HTTP;
00265
00266 case NP_HTTPS:
00267 return SITE_VALIDFOR_HTTPS;
00268
00269 case NP_FTP:
00270 return SITE_VALIDFOR_FTP;
00271
00272 default:
00273 return 0;
00274 }
00275 }
00276 #endif
00277
00278 ULONG fsGetSiteIp (LPCSTR pszSite)
00279 {
00280 hostent* he = gethostbyname (pszSite);
00281
00282 if (he == NULL)
00283 return 0;
00284
00285 return *((ULONG*) he->h_addr_list [0]);
00286 }
00287
00288 void vmsMakeWinInetProxy (LPCSTR pszProxy, fsNetworkProtocol npConnection, fsNetworkProtocol npProxy, LPSTR pszWProxy)
00289 {
00290 switch (npConnection)
00291 {
00292 case NP_FTP:
00293 lstrcpy (pszWProxy, "ftp=");
00294 break;
00295
00296 case NP_HTTP:
00297 case NP_FILE:
00298 lstrcpy (pszWProxy, "http=");
00299 break;
00300
00301 case NP_HTTPS:
00302 lstrcpy (pszWProxy, "https=");
00303 break;
00304
00305 default:
00306 ASSERT (FALSE);
00307 return;
00308 }
00309
00310 switch (npProxy)
00311 {
00312 case NP_FTP:
00313 lstrcat (pszWProxy, "ftp://");
00314 break;
00315
00316 case NP_HTTP:
00317 case NP_FILE:
00318 lstrcat (pszWProxy, "http://");
00319 break;
00320
00321 case NP_HTTPS:
00322 lstrcat (pszWProxy, "https://");
00323 break;
00324
00325 default:
00326 ASSERT (FALSE);
00327 return;
00328 }
00329
00330 lstrcat (pszWProxy, pszProxy);
00331 }
00332
00333 void fsDecodeHtmlText (fsString &str)
00334 {
00335 CString str2 = str;
00336 str2.Replace ("&", "&");
00337 str2.Replace ("<", "<");
00338 str2.Replace (">", ">");
00339 str2.Replace (""", "\"");
00340 str2.Replace (" ", " ");
00341 str = str2;
00342 }
00343
00344 void fsDecodeHtmlUrl (fsString &str)
00345 {
00346 CString str2;
00347 int len = str.GetLength ();
00348 for (int i = 0; i < len; )
00349 {
00350 if (str [i] == '\\' && str [i+1] == 'u' && str.GetLength () - i >= 6)
00351 {
00352
00353 char sz [5];
00354 sz [0] = str [i+2];
00355 sz [1] = str [i+3];
00356 sz [2] = str [i+4];
00357 sz [3] = str [i+5];
00358 sz [4] = 0;
00359 int c;
00360 sscanf (sz, "%x", &c);
00361 if (c < 127)
00362 str2 += (char)c;
00363 i += 6;
00364 }
00365 else if (str [i] == '%' && str [i+1] != '%' && str.GetLength () - i >= 3)
00366 {
00367
00368 char sz [3];
00369 sz [0] = str [i+1];
00370 sz [1] = str [i+2];
00371 sz [2] = 0;
00372 int c;
00373 sscanf (sz, "%x", &c);
00374 str2 += (char)c;
00375 i += 3;
00376 }
00377 else if (str [i] == '&' && str [i+1] == '#')
00378 {
00379 CString strC;
00380 int j = i+2;
00381 while (isdigit (str [j]))
00382 strC += str [j++];
00383 if (str [j] == ';')
00384 {
00385
00386 int c = atoi (strC);
00387 str2 += (char)c;
00388 i = j + 1;
00389 }
00390 else
00391 {
00392 str2 += str [i++];
00393 }
00394 }
00395 else
00396 {
00397 str2 += str [i++];
00398 }
00399 }
00400
00401 str = str2;
00402
00403 fsDecodeHtmlText (str);
00404 }