00001
00002
00003
00004
00005
00006 #include "stdafx.h"
00007 #include "Web Interface.h"
00008 #include "vmsFdmWebInterfaceServer.h"
00009 #include "..\Fdm.h"
00010 #include <comdef.h>
00011 #include <atlbase.h>
00012
00013 _COM_SMARTPTR_TYPEDEF (IWGUrlReceiver, __uuidof (IWGUrlReceiver));
00014 _COM_SMARTPTR_TYPEDEF (IFDMDownloadsStat, __uuidof (IFDMDownloadsStat));
00015 _COM_SMARTPTR_TYPEDEF (IFDMDownload, __uuidof (IFDMDownload));
00016
00017 #ifdef _DEBUG
00018 #undef THIS_FILE
00019 static char THIS_FILE[]=__FILE__;
00020 #define new DEBUG_NEW
00021 #endif
00022
00023 vmsFdmWebInterfaceServer::vmsFdmWebInterfaceServer()
00024 {
00025 LoadDocuments ();
00026 }
00027
00028 vmsFdmWebInterfaceServer::~vmsFdmWebInterfaceServer()
00029 {
00030
00031 }
00032
00033 BOOL vmsFdmWebInterfaceServer::ProcessRequest(vmsHttpRequest &request, vmsHttpResponse &response)
00034 {
00035
00036 if (lstrcmpi (request.get_RequestType (), "GET"))
00037 return FALSE;
00038
00039 CString strU = AfxGetApp ()->GetProfileString ("Network", "Login"),
00040 strP = AfxGetApp ()->GetProfileString ("Network", "Password");
00041
00042 if (strU.IsEmpty () == FALSE &&
00043 lstrcmp (request.get_Auth (), strU + ":" + strP))
00044 {
00045 response.set_ResponseCode ("401 Authorization Required");
00046 return FALSE;
00047 }
00048
00049 LPCSTR pszRes = request.get_ResourcePath ();
00050
00051 if (lstrcmp (pszRes, "/") == 0)
00052 return RequestRootPage (response);
00053
00054 if (strncmp (pszRes, "/adddownload.req?", lstrlen ("/adddownload.req?")) == 0)
00055 return RequestCreateNewDownload (pszRes, response);
00056
00057 if (strncmp (pszRes, "/compdlds.req", lstrlen ("/compdlds.req")) == 0)
00058 return RequestListOfCompletedDownloads (pszRes, response);
00059
00060 response.set_ResponseCode ("404 Not Found");
00061 return FALSE;
00062 }
00063
00064 BOOL vmsFdmWebInterfaceServer::RequestRootPage(vmsHttpResponse &response)
00065 {
00066 CoInitialize (NULL);
00067
00068 IFDMDownloadsStatPtr spDlds;
00069 spDlds.CreateInstance (__uuidof (FDMDownloadsStat));
00070 ASSERT (spDlds != NULL);
00071
00072 spDlds->BuildListOfDownloads (FALSE, TRUE);
00073
00074 long lCount = 0;
00075 spDlds->get_DownloadCount (&lCount);
00076
00077 CString strDldsHtml;
00078
00079 if (lCount == 0)
00080 {
00081 strDldsHtml = "There are no active downloads currently.";
00082 }
00083 else
00084 {
00085
00086
00087 strDldsHtml += "<table width=\"100%\" border=\"1\">";
00088 strDldsHtml += "<tr>";
00089 strDldsHtml += "<td>File name<br> </td>";
00090 strDldsHtml += "<td>Size<br> </td>";
00091 strDldsHtml += "<td>Downloaded<br> </td>";
00092 strDldsHtml += "<td>Time remaining<br> </td>";
00093 strDldsHtml += "<td>Sections<br> </td>";
00094 strDldsHtml += "<td>Speed<br> </td>";
00095 strDldsHtml += "</tr>";
00096
00097 for (long i = 0; i < lCount; i++)
00098 {
00099 IFDMDownloadPtr spDld;
00100 spDlds->get_Download (i, &spDld);
00101 ASSERT (spDld != NULL);
00102
00103 CString str;
00104
00105 str += "<tr>";
00106
00107 for (int i = 0; i < 6; i++)
00108 {
00109 str += "<td>";
00110 CComBSTR bstr;
00111 spDld->get_DownloadText (i, &bstr);
00112 CString str2 = bstr;
00113 str += str2;
00114 str += "</td>";
00115 }
00116
00117 str += "</tr>";
00118
00119 strDldsHtml += str;
00120 }
00121
00122 strDldsHtml += "</table>";
00123 }
00124
00125 CString str = m_strRootHtml.c_str ();
00126 str.Replace ("%ActiveDownloads%", strDldsHtml);
00127
00128 response.set_Body (str, str.GetLength ());
00129
00130 return TRUE;
00131 }
00132
00133 BOOL vmsFdmWebInterfaceServer::RequestCreateNewDownload(LPCSTR pszRes, vmsHttpResponse &response)
00134 {
00135 CoInitialize (NULL);
00136
00137 IWGUrlReceiverPtr spUrlRcvr;
00138 spUrlRcvr.CreateInstance (__uuidof (WGUrlReceiver));
00139 ASSERT (spUrlRcvr != NULL);
00140
00141 if (spUrlRcvr == NULL)
00142 {
00143 CoUninitialize ();
00144 response.set_Body (m_strAddDownloadRes_err.c_str (), m_strAddDownloadRes_err.length ());
00145 return TRUE;
00146 }
00147
00148 spUrlRcvr->put_ForceSilentEx (TRUE);
00149 spUrlRcvr->put_CheckExtension (FALSE);
00150 spUrlRcvr->put_ForceDownloadAutoStart (TRUE);
00151 spUrlRcvr->put_DisableMaliciousChecking (TRUE);
00152 spUrlRcvr->put_DisableURLExistsCheck (TRUE);
00153
00154 CString strUrl;
00155 pszRes += lstrlen ("/adddownload.req?");
00156 if (strnicmp (pszRes, "URL=", 4))
00157 {
00158 CoUninitialize ();
00159 response.set_Body (m_strAddDownloadRes_err.c_str (), m_strAddDownloadRes_err.length ());
00160 return FALSE;
00161 }
00162 pszRes += 4;
00163
00164
00165
00166 while (*pszRes)
00167 {
00168 char c;
00169
00170
00171 if (*pszRes == '%')
00172 {
00173 pszRes++;
00174 char sz [3];
00175 sz [0] = *pszRes++;
00176 if (*pszRes == 0)
00177 return FALSE;
00178 sz [1] = *pszRes++;
00179 sz [2] = 0;
00180 int i;
00181 sscanf (sz, "%x", &i);
00182 c = (char)i;
00183 }
00184 else
00185 {
00186 c = *pszRes++;
00187
00188 if (c == '+')
00189 c = ' ';
00190 }
00191
00192 strUrl += c;
00193 }
00194
00195
00196
00197 CString str;
00198 for (int i = 0; i < strUrl.GetLength () - 1;)
00199 {
00200 if (strUrl [i] == '&' && strUrl [i+1] == '#')
00201 {
00202 int k = i + 2;
00203 CString str2;
00204 bool bHex = false;
00205 if (k < strUrl.GetLength ())
00206 bHex = strUrl [k] == 'x';
00207 if (bHex)
00208 k++;
00209 while (isdigit (strUrl [k]))
00210 str2 += strUrl [k++];
00211 if (str2.IsEmpty () == FALSE &&
00212 (strUrl [k] == ';' || strUrl.GetLength () == k))
00213 {
00214 i = ++k;
00215 wchar_t wsz [2];
00216 if (bHex)
00217 {
00218 int j;
00219 sscanf (str2, "%x", &j);
00220 wsz [0] = (wchar_t)j;
00221 }
00222 else
00223 {
00224 wsz [0] = atoi (str2);
00225 }
00226 wsz [1] = 0;
00227 char sz [2];
00228 WideCharToMultiByte (CP_ACP, 0, wsz, -1, sz, 1, NULL, NULL);
00229 sz [1] = 0;
00230 str += sz;
00231 continue;
00232 }
00233
00234 }
00235
00236 str += strUrl [i++];
00237 }
00238 if (i == strUrl.GetLength () - 1)
00239 str += strUrl [i];
00240 strUrl = str;
00241
00242
00243 USES_CONVERSION;
00244 CComBSTR bstr = A2W (strUrl);
00245 spUrlRcvr->put_Url (bstr);
00246
00247 HRESULT hr = spUrlRcvr->AddDownload ();
00248
00249 BOOL bAdded = FALSE;
00250
00251
00252 if (SUCCEEDED (hr))
00253 {
00254 CComBSTR bstrState;
00255
00256 do {
00257 Sleep (10);
00258 spUrlRcvr->get_UIState (&bstrState);
00259 } while (bstrState == L"in_progress");
00260
00261 bAdded = bstrState == L"added";
00262 }
00263
00264 if (bAdded == FALSE)
00265 response.set_Body (m_strAddDownloadRes_err.c_str (), m_strAddDownloadRes_err.length ());
00266 else
00267 response.set_Body (m_strAddDownloadRes_ok.c_str (), m_strAddDownloadRes_ok.length ());
00268
00269 spUrlRcvr = NULL;
00270 CoUninitialize ();
00271
00272 return TRUE;
00273 }
00274
00275 void vmsFdmWebInterfaceServer::LoadDocuments()
00276 {
00277 CFile file;
00278 CString str;
00279
00280 LPCSTR pszVer = "1.0";
00281
00282 file.Open ("Server/index.html", CFile::modeRead);
00283 int nLen = file.GetLength ();
00284 LPSTR psz = new char [nLen];
00285 file.Read (psz, nLen);
00286 strncpy (str.GetBuffer (nLen + 1), psz, nLen);
00287 str.ReleaseBuffer (nLen);
00288 str.Replace ("%ver%", pszVer);
00289 m_strRootHtml = str;
00290 delete [] psz;
00291 file.Close ();
00292
00293 file.Open ("Server/adddownloadres_ok.html", CFile::modeRead);
00294 nLen = file.GetLength ();
00295 psz = new char [nLen];
00296 file.Read (psz, nLen);
00297 strncpy (str.GetBuffer (nLen + 1), psz, nLen);
00298 str.ReleaseBuffer (nLen);
00299 str.Replace ("%ver%", pszVer);
00300 m_strAddDownloadRes_ok = str;
00301 delete [] psz;
00302 file.Close ();
00303
00304 file.Open ("Server/adddownloadres_err.html", CFile::modeRead);
00305 nLen = file.GetLength ();
00306 psz = new char [nLen];
00307 file.Read (psz, nLen);
00308 strncpy (str.GetBuffer (nLen + 1), psz, nLen);
00309 str.ReleaseBuffer (nLen);
00310 str.Replace ("%ver%", pszVer);
00311 m_strAddDownloadRes_err = str;
00312 delete [] psz;
00313 file.Close ();
00314
00315 file.Open ("Server/compdlds.html", CFile::modeRead);
00316 nLen = file.GetLength ();
00317 psz = new char [nLen];
00318 file.Read (psz, nLen);
00319 strncpy (str.GetBuffer (nLen + 1), psz, nLen);
00320 str.ReleaseBuffer (nLen);
00321 str.Replace ("%ver%", pszVer);
00322 m_strCompDldsHtml = str;
00323 delete [] psz;
00324 file.Close ();
00325 }
00326
00327 BOOL vmsFdmWebInterfaceServer::RequestListOfCompletedDownloads(LPCSTR pszRes, vmsHttpResponse &response)
00328 {
00329 CoInitialize (NULL);
00330
00331 IFDMDownloadsStatPtr spDlds;
00332 spDlds.CreateInstance (__uuidof (FDMDownloadsStat));
00333 ASSERT (spDlds != NULL);
00334
00335 spDlds->BuildListOfDownloads (TRUE, FALSE);
00336
00337 long lCount = 0;
00338 spDlds->get_DownloadCount (&lCount);
00339
00340 CString strDldsHtml;
00341
00342 if (lCount == 0)
00343 {
00344 strDldsHtml = "There are no completed downloads currently.";
00345 }
00346 else
00347 {
00348
00349
00350 strDldsHtml += "<table width=\"100%\" border=\"1\">";
00351 strDldsHtml += "<tr>";
00352 strDldsHtml += "<td>File name<br> </td>";
00353 strDldsHtml += "<td>Size<br> </td>";
00354 strDldsHtml += "<td>URL<br> </td>";
00355 strDldsHtml += "</tr>";
00356
00357 for (long i = 0; i < lCount; i++)
00358 {
00359 IFDMDownloadPtr spDld;
00360 spDlds->get_Download (i, &spDld);
00361 ASSERT (spDld != NULL);
00362
00363 CString str;
00364
00365 str += "<tr>";
00366
00367 for (int i = 0; i < 3; i++)
00368 {
00369 str += "<td>";
00370 CComBSTR bstr;
00371 if (i != 2)
00372 {
00373 spDld->get_DownloadText (i, &bstr);
00374 CString str2 = bstr;
00375 str += str2;
00376 }
00377 else
00378 {
00379 spDld->get_Url (&bstr);
00380 CString str2 = bstr;
00381 str += "<a href=\""; str += str2; str += "\">";
00382 str += str2; str += "</a>";
00383 }
00384
00385 str += "</td>";
00386 }
00387
00388 str += "</tr>";
00389
00390 strDldsHtml += str;
00391 }
00392
00393 strDldsHtml += "</table>";
00394 }
00395
00396 CString str = m_strCompDldsHtml.c_str ();
00397 str.Replace ("%compdlds%", strDldsHtml);
00398
00399 response.set_Body (str, str.GetLength ());
00400
00401 return TRUE;
00402 }