using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using System.Net;
using System.Collections.Specialized;
using System.Diagnostics;
using System.Text;
///
/// Summary description for Dlogger
///
public class DLogger : IHttpHandler
{
public DLogger()
{
//
// TODO: Add constructor logic here
//
}
public void ProcessRequest(HttpContext context)
{
HttpRequest Request = context.Request;
HttpResponse Response = context.Response;
// This handler is called whenever a file ending
// in .sample is requested. A file with that extension
// does not need to exist.
//Response.Write("");
//Response.Write("
");
//Response.Write("Hello from a synchronous custom HTTP handler.
");
//Response.Write("");
//Response.Write("");
//logWrite(Request.Url+Request.UserAgent);
string UA = Request.UserAgent;
string testUA = UA;
if (testUA == null)
{
logWrite("UAERROR: UA null");
testUA = ";;no ua;;";
}
if (testUA.ToLower() == null)
{
logWrite("UAERROR: ToLower null|" + testUA);
}
else
{
testUA = testUA.ToLower();
}
if (testUA.Contains("opera mini") || testUA.Contains("operamini"))
UA = (Request.Params["HTTP_X_OPERAMINI_PHONE_UA"] == null || Request.Params["HTTP_X_OPERAMINI_PHONE_UA"].Equals("")) ? Request.UserAgent : Request.Params["HTTP_X_OPERAMINI_PHONE_UA"];
logWrite(Request.UserHostAddress + "|" + Request.UrlReferrer + "|" + Request.Url + "|" + UA);
try
{
string filePath = HttpContext.Current.Server.MapPath("~/") + Request.Url.ToString().Substring(Request.Url.ToString().LastIndexOf("mobile") + "mobile".Length + 1); // get full path to a file
if (File.Exists(filePath)) // check if the file exists
{
logWrite("Path|"+filePath);
if (filePath.ToLower().Trim().EndsWith(".sis")) // set mine type if it's "sis"
{
logWrite("Setting mime type to sis.");
Response.ContentType = "application/vnd.symbian.install";
}
if (filePath.ToLower().Trim().EndsWith(".apk")) // set mine type if it's "apk"
{
logWrite("Setting mime type to apk.");
Response.ContentType = "application/vnd.android.package-archive";
}
logWrite("Sending.");
Response.WriteFile(filePath); // send file to an output
sendData("" +UA, "" +Request.UserHostAddress, "" +Request.UrlReferrer, "" +Request.Url, filePath);
}
else
{
logWrite(filePath + "|File not found.");
sendData("" +UA, "" + Request.UserHostAddress, "" +Request.UrlReferrer, "" +Request.Url, "File not found.");
throwError(Response, 404);
}
}
catch (Exception e)
{
logWrite("EX: "+e.Message);
throwError(Response, 404);
}
logWrite("End.");
}
public bool IsReusable
{
// To enable pooling, return true here.
// This keeps the handler in memory.
get { return false; }
}
public void logWrite(String s)
{
try
{
StreamWriter log = File.AppendText(HttpContext.Current.Server.MapPath("~/logs/") + "dllog.txt");
log.WriteLine(DateTime.Now.ToString() + "|" + this.GetHashCode() + "|" + s + "|");
log.Close();
}
catch (Exception) { }
}
public void sendData(string uua, string iip, string uurl, string ffile, string lfile)
{
string URLAuth = "http://www.musicjet.cz/MobileLog.aspx";
WebClient webClient = new WebClient();
NameValueCollection formData = new NameValueCollection();
formData["Action"] = "DOWNLOAD";
formData["SessionID"] = "" + this.GetHashCode();
formData["Agent"] = "" + uua;
formData["IPAddress"] = "" + iip;
formData["URL"] = "" + uurl;
formData["RequestedFile"] = "" + ffile;
formData["ProvidedFile"] = "" + lfile;
byte[] responseBytes = webClient.UploadValues(URLAuth, "POST", formData);
string result = Encoding.UTF8.GetString(responseBytes);
webClient.Dispose();
logWrite("DATAOUT|" + result);
}
public void throwError(HttpResponse Response, int error)
{
Response.StatusCode = error;
Response.SuppressContent = true;
HttpContext.Current.ApplicationInstance.CompleteRequest();
//Response.End();
}
}