Mini Kabibi Habibi
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Security.Cryptography;
using System.Text;
using System.Web;
using System.Diagnostics;
using System.Runtime.InteropServices;
using Microsoft.Win32;
namespace DemoLauncher.Web {
public class DemosMainHandler : IHttpHandler {
public void ProcessRequest(HttpContext context) {
string request = HttpUtility.UrlDecode(context.Request.QueryString.ToString());
byte[] data = GetData(request);
if(data == null)
data = new byte[] { };
string hash = GetHash(data);
string ifNoneMatch = context.Request.Headers["If-None-Match"];
if(ifNoneMatch == hash) {
context.Response.StatusCode = 304;
context.Response.StatusDescription = "Not Modified";
return;
}
context.Response.ContentType = "application/octet-stream";
context.Response.BinaryWrite(data);
context.Response.Cache.SetCacheability(HttpCacheability.Public);
context.Response.Cache.SetRevalidation(HttpCacheRevalidation.AllCaches);
context.Response.Cache.SetETag(hash);
}
public bool IsReusable { get { return true; } }
static string GetHash(byte[] data) {
using(SHA1Managed sha1 = new SHA1Managed()) {
byte[] hash = sha1.ComputeHash(data);
StringBuilder formatted = new StringBuilder(2 * hash.Length);
foreach(byte b in hash)
formatted.AppendFormat("{0:X2}", b);
return formatted.ToString();
}
}
byte[] GetData(string request) {
try {
string[] parts = request.Split(':');
if(parts.Length == 0) return null;
switch(parts[0]) {
case "lp": return GetLicensedProducts(parts);
case "source": return GetSourceData(parts);
case "bin": return GetBinData(parts);
case "run": return RunApplication(parts);
case "de": return DoEvent(parts); //DEMO_REMOVE
}
} catch { }
return null;
}
static byte[] GetSourceData(string[] parts) {
if(parts.Length != 4) return null;
string filePath = FindSourceFile(HttpContext.Current, parts[1], parts[2], parts[3]);
return ReadFile(filePath);
}
static byte[] GetBinData(string[] parts) {
if(parts.Length != 3) return null;
string demoName = parts[1];
string partName = parts[2];
if(string.IsNullOrEmpty(partName))
partName = demoName + ".xap";
else
partName = partName + ".zip";
string filePath = FindBinFile(HttpContext.Current, partName);
return ReadFile(filePath);
}
static byte[] ReadFile(string path) {
try {
if(string.IsNullOrEmpty(path)) return null;
using(FileStream stream = new FileStream(path, FileMode.Open, FileAccess.Read)) {
byte[] data = new byte[(int)stream.Length];
stream.Read(data, 0, data.Length);
return data;
}
} catch {
return null;
}
}
static string[] GetDirectories(string baseDir, string pathPattern) {
string[] pathParts = pathPattern.Split(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar);
List<string> resolvedPaths = new List<string>();
resolvedPaths.Add(baseDir);
foreach(string pathPart in pathParts) {
List<string> nextResolvedPaths = new List<string>();
foreach(string resolvedPath in resolvedPaths) {
if(pathPart == "..")
nextResolvedPaths.Add(Path.GetFullPath(Path.Combine(resolvedPath, "..")));
else
nextResolvedPaths.AddRange(Directory.GetDirectories(resolvedPath, pathPart, SearchOption.TopDirectoryOnly));
}
resolvedPaths = nextResolvedPaths;
}
return resolvedPaths.ToArray();
}
static string FindBinFile(HttpContext context, string partName) {
string appPath = context.Server.MapPath("~");
if(appPath == null) return null;
string baseDir = Path.GetFullPath(appPath);
foreach(string binPath in new string[] { "ClientBin", "..\\..\\XtraReports\\CS\\ReportService\\ClientBin", "..\\..\\*\\*.Web\\ClientBin", "..\\..\\..\\Demos.Win\\ReportsDemos\\CS\\ReportSilverlightDemo\\ReportService\\ClientBin" }) {
foreach(string dir in GetDirectories(baseDir, binPath)) {
string[] files = Directory.GetFiles(dir, partName, SearchOption.TopDirectoryOnly);
if(files.Length != 0) return files[0];
}
}
return null;
}
static string FindSourceFile(HttpContext context, string directoryName, string demoName, string fileName) {
string s = FindSourceFileCore(context, directoryName, demoName, fileName);
if(s == null)
s = FindSourceFileCore(context, "XtraReports\\" + directoryName, demoName, fileName);
return s;
}
static string FindSourceFileCore(HttpContext context, string directoryName, string demoName, string fileName) {
try {
string baseDir = FindDirectory(context, directoryName);
string[] demoDirs = Directory.GetDirectories(baseDir, demoName + "*", SearchOption.TopDirectoryOnly);
if(demoDirs.Length == 0) return null;
string[] files = Directory.GetFiles(demoDirs[0], fileName, SearchOption.AllDirectories);
if(files.Length == 0) return null;
return files[0];
} catch {
return null;
}
}
static string FindFile(HttpContext context, string fileName, string directoryName) {
string appPath = context.Server.MapPath("~");
if(appPath == null) return null;
string dirName = Path.GetFullPath(appPath);
for(int n = 0; n < 9; n++) {
string path = dirName + "\\" + directoryName + "\\" + fileName;
try {
if(File.Exists(path))
return path;
} catch { }
dirName += @"\..";
}
throw new FileNotFoundException(fileName + " not found");
}
static string FindDirectory(HttpContext context, string directoryName) {
string appPath = context.Server.MapPath("~");
if(appPath == null) return null;
string dirName = Path.GetFullPath(appPath);
for(int n = 0; n < 9; n++) {
string path = dirName + "\\" + directoryName;
try {
if(Directory.Exists(path))
return path;
} catch { }
dirName += @"\..";
}
throw new DirectoryNotFoundException(directoryName + " not found");
}
static byte[] GetLicensedProducts(string[] parts) {
DevExpress.Internal.UserData userData = DevExpress.Utils.About.Utility.GetInfo(); //DEMO_REMOVE
if(userData == null) return null; //DEMO_REMOVE
long licensedProducts = long.MaxValue;
licensedProducts = (long)userData.GetType().GetProperty("LicensedProducts", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(userData, null); //DEMO_REMOVE
List<byte> data = new List<byte>();
while(licensedProducts > 0) {
data.Add((byte)(licensedProducts & 0xFF));
licensedProducts = licensedProducts >> 8;
}
return data.ToArray();
}
static byte[] RunApplication(string[] parts) {
if(parts.Length != 2) return null;
string file = DevExpress.DemoData.Helpers.StartApplicationHelper.UrlHelper.Unscreen(parts[1]);
string error = DevExpress.DemoData.Helpers.StartApplicationHelper.Start(file, null, false);
return string.IsNullOrEmpty(error) ? null : Encoding.UTF8.GetBytes(error);
}
static byte[] DoEvent(string[] parts) { //DEMO_REMOVE
if(parts.Length != 4) return null; //DEMO_REMOVE
byte kind = byte.Parse(parts[1]); //DEMO_REMOVE
byte platform = byte.Parse(parts[2]); //DEMO_REMOVE
string action = DevExpress.DemoData.Helpers.StartApplicationHelper.UrlHelper.Unscreen(parts[3]); //DEMO_REMOVE
DevExpress.Utils.About.UAlgo.Default.DoEvent(kind, platform, action); //DEMO_REMOVE
return null; //DEMO_REMOVE
} //DEMO_REMOVE
}
}