/// <summary> /// Raises the on init. /// </summary> /// <param name="args">The <see cref="Oda.HttpEventArgs"/> instance containing the event data.</param> internal void RaiseOnInitialize(HttpEventArgs args) { if(Initialize != null) Initialize(this, args); }
/// <summary> /// Raises the on dispose. /// </summary> /// <param name="args">The <see cref="Oda.HttpEventArgs"/> instance containing the event data.</param> internal void RaiseOnDispose(HttpEventArgs args) { if(DisposeHttpApplication != null) DisposeHttpApplication(this, args); }
/// <summary> /// Raises the on end request. /// </summary> /// <param name="args">The <see cref="Oda.HttpEventArgs"/> instance containing the event data.</param> internal void RaiseOnEndRequest(HttpEventArgs args) { if(EndHttpRequest != null) EndHttpRequest(this, args); }
/// <summary> /// Raises the on app error. /// </summary> /// <param name="args">The <see cref="Oda.HttpEventArgs"/> instance containing the event data.</param> internal void RaiseOnApplicationError(HttpEventArgs args) { if(ApplicationError != null) ApplicationError(this, args); }
/// <summary> /// Raises the on begin request. /// </summary> /// <param name="args">The <see cref="Oda.HttpEventArgs"/> instance containing the event data.</param> internal void RaiseOnBeginRequest(HttpEventArgs args) { if(BeginHttpRequest != null) BeginHttpRequest(this, args); }
/// <summary> /// Starts up Oda HttpModule. Starts logging, instantiates plugins, connects to events and creates Json mapper. /// </summary> /// <param name="sender">The HTTP application.</param> public void Init(HttpApplication sender) { // bind events sender.BeginRequest += BeginRequest; sender.EndRequest += EndRequest; sender.Error += AppError; // only init the http module one time if (HttpApplication != null) return; HttpApplication = sender; var domain = AppDomain.CurrentDomain; BaseDirectory = domain.BaseDirectory; RelativeSearchPath = domain.RelativeSearchPath; DynamicDirectory = domain.DynamicDirectory; GetWebConfigSettings(); // start logging var logFileName = string.Format("{0}.log", DateTime.Now.ToString("G").Replace("/", ".").Replace(":", ".").Replace(" ", "_")); Log = new Log(System.IO.Path.Combine(LogPath, logFileName), LogVerbosity, LogTimestamp); // write some info about the assembly LogVersionInformation(); // resolve embedded assemblies AppDomain.CurrentDomain.AssemblyResolve += ResolveEmbeddedAssembiles; if (StartupState != StartupState.NotYetStarted) return; StartupState = StartupState.Starting; // create box to put stuff in _items = new List<object>(); // init plugins Plugin.InternalPlugins = new List<object>(); Plugin.InternalJsonMethods = new Dictionary<string, System.Reflection.MethodInfo>(); Plugin.ActivatePlugins(); // raise init event var args = new HttpEventArgs(HttpApplication); RaiseOnInitialize(args); UploadStatus += (o, eventArgs) => { var ev = (HttpUploadStatusEventArgs) eventArgs; if(UploadStatuses.ContainsKey(ev.Id)) { // update existing key UploadStatuses[ev.Id].BytesRead = ev.BytesRead; UploadStatuses[ev.Id].BytesTotal = ev.BytesTotal; UploadStatuses[ev.Id].Complete = ev.Complete; UploadStatuses[ev.Id].LastUpdated = ev.LastUpdated; UploadStatuses[ev.Id].StartedOn = ev.StartedOn; UploadStatuses[ev.Id].Message = ev.Message; UploadStatuses[ev.Id].CurrentFile = ev.CurrentFile; if (ev.Complete) { UploadStatuses.Remove(ev.Id); } }else { UploadStatuses.Add(ev.Id, new UploadStatus() { BytesRead = ev.BytesRead, BytesTotal = ev.BytesTotal, Complete = ev.Complete, Id = ev.Id, LastUpdated = ev.LastUpdated, StartedOn = ev.StartedOn, Message = ev.Message, CurrentFile = ev.CurrentFile }); } }; StartupState = StartupState.Started; }
/// <summary> /// The end of the Http request /// </summary> /// <param name="sender">The sender.</param> /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param> public void EndRequest(object sender, EventArgs e) { var args = new HttpEventArgs(HttpApplication); RaiseOnEndRequest(args); }
/// <summary> /// Disposes of the resources (other than memory) used by the module that implements <see cref="T:System.Web.IHttpModule"/>. /// </summary> public void Dispose() { var args = new HttpEventArgs(HttpApplication); RaiseOnDispose(args); }
/// <summary> /// The start of an Http request /// </summary> /// <param name="sender">The sender.</param> /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param> public void BeginRequest(object sender, EventArgs e) { // begin request events var hArgs = new HttpEventArgs(HttpApplication); RaiseOnBeginRequest(hArgs); var results = new Dictionary<string, JsonResponse>(); var request = HttpContext.Current.Request; var response = HttpContext.Current.Response; // check for Json requests made to the JsonMethodUrl // and clear the requests if(request.FilePath.Equals(JsonMethodUrl)) { // a request was made to the responder Url // deserialize the request and execute the requested methods // gather the results and add them to the results collection // do post - Don't use the request.Form object because it is silly. var workerRequest = (HttpWorkerRequest)HttpContext.Current.GetType().GetProperty("WorkerRequest",BindingFlags.Instance | BindingFlags.NonPublic).GetValue(HttpContext.Current, null); var bodyLength = workerRequest.GetPreloadedEntityBodyLength(); if (bodyLength>0) { var contentType = workerRequest.GetKnownRequestHeader(HttpWorkerRequest.HeaderContentType); if (contentType == null) { throw new NullReferenceException("Header Content-Type cannot be empty. Acceptable post values are multipart/form-data or application/json."); } var filePathsAndMap = ParseRequest(workerRequest); try { JsonResponse.InvokeJsonMethods(filePathsAndMap, ref results); // after methods have been invoked, remove files from temp foreach (var f in filePathsAndMap.Files) { File.Delete(f.Path); } } catch(Exception ex) { var iex = GetInnermostException(ex); var errorResult = new JsonResponse(3, string.Format("Error invoking method. Source: {1}{0} Message: {2}{0} Stack Trace: {3}", Environment.NewLine, iex.Source ,iex.Message, iex.StackTrace)); results.Add("Exception", errorResult); } } else { // if there was no post // do QueryString (get) if (request.QueryString.Keys.Count == 1) { try { var m = new Mapper() {Map = HttpUtility.UrlDecode(request.QueryString.ToString())}; JsonResponse.InvokeJsonMethods(m, ref results); } catch (Exception ex) { var iex = GetInnermostException(ex); var errorResult = new JsonResponse(3, string.Format("Error invoking method. Source: {1}{0} Message: {2}{0} Stack Trace: {3}", Environment.NewLine, iex.Source, iex.Message, iex.StackTrace)); results.Add("Exception", errorResult); } } } } // if one or more Json methods returned a result // respond with that result if (results.Count <= 0) return; foreach(var r in results) { var result = r.Value; if (result.SupressResponse) continue; response.Status = result.Status; response.StatusCode = result.StatusCode; response.ContentType = result.ContentType; response.ContentEncoding = result.ContentEncoding; response.HeaderEncoding = result.HeaderEncoding; if(result.HttpCookie != null) { response.AppendCookie(result.HttpCookie); } if(result.ContentDisposition == JsonContentDisposition.Attachment || result.ContentDisposition == JsonContentDisposition.Normal) { if(result.ContentDisposition == JsonContentDisposition.Attachment) { response.AddHeader("Content-Disposition", string.Format(@"attachment; filename=""{0}""", result.AttachmentFileName)); } if(result.AttachmentContent.Length > 0) { result.ContentLength = result.AttachmentContent.Length; response.AddHeader("Content-Length", result.ContentLength.ToString(CultureInfo.InvariantCulture)); response.Write(result.AttachmentContent); } else if(result.AttachmentStream.Length > 0) { result.ContentLength = (int)result.AttachmentStream.Length; response.AddHeader("Content-Length", result.ContentLength.ToString(CultureInfo.InvariantCulture)); var bytes = new byte[result.AttachmentStream.Length]; result.AttachmentStream.Read(bytes, 0, (int)result.AttachmentStream.Length); response.BinaryWrite(bytes); } response.Flush(); HttpContext.Current.ApplicationInstance.CompleteRequest(); return;// Only one file can be output at a time. } } response.Write(JsonResponse.ToJson(results)); response.Flush(); HttpContext.Current.ApplicationInstance.CompleteRequest(); }
/// <summary> /// Application error /// </summary> /// <param name="sender">The sender.</param> /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param> public void AppError(object sender, EventArgs e) { var args = new HttpEventArgs(HttpApplication); RaiseOnApplicationError(args); }