void Instance_OnBeforeLoadExistingFile(HttpApplication app, string sprocketPath, string[] pathSections, HandleFlag handled) { if (sprocketPath.EndsWith(".js")) { if (SprocketSettings.GetBooleanValue("CompressJavaScript")) { HttpContext.Current.Response.Write(WebUtility.CacheTextFile(sprocketPath, true)); handled.Set(); } } }
private SmtpClient GetClient() { string mailserver = SprocketSettings.GetValue("MailServer"); int port; int.TryParse(SprocketSettings.GetValue("MailServerPort"), out port); string useAuthentication = SprocketSettings.GetValue("MailServerAuthentication"); string authUsername = SprocketSettings.GetValue("MailServerUsername"); string authPassword = SprocketSettings.GetValue("MailServerPassword"); SmtpClient client = new SmtpClient(); client.EnableSsl = SprocketSettings.GetBooleanValue("MailServerSSL"); if (mailserver != null) { client.Host = mailserver; } else { client.Host = "localhost"; } if (port > 0) { client.Port = port; } if (useAuthentication != null && authUsername != null && authPassword != null) { if (StringUtilities.MatchesAny(useAuthentication.ToLower(), "true", "yes", "1")) { client.Credentials = new NetworkCredential(authUsername, authPassword); } } return(client); }
/// <summary> /// Writes all registered scripts into a single string surrounded by html script tags /// </summary> /// <returns>HTML script tags with containing javascript</returns> public string CreateScriptTags() { StringBuilder sb = new StringBuilder(); foreach (KeyValuePair <string, string> script in scripts) { string js = script.Value; foreach (KeyValuePair <string, object> key in keys) { js = js.Replace(key.Key, key.Value.ToString()); } sb.Append("<script language=\"JavaScript\">"); sb.Append(Environment.NewLine); if (SprocketSettings.GetBooleanValue("CompressJavaScript")) { js = JavaScriptCondenser.Condense(js); } sb.Append(js); sb.Append(Environment.NewLine); sb.Append("</script>"); sb.Append(Environment.NewLine); } return(sb.ToString()); }
/// <summary> /// This method is called in response to any request where the URL ends with a .ajax /// extension, which Sprocket uses to designate an Ajax request from an XmlHttpRequest /// call in the browser. The posted data contains information that this method uses to /// find the correct ISprocketModule implementation. It then uses reflection to get /// the requested method from the module, converts the javascript arguments into native /// CLR types and passes them to the method, after which it takes the returned data and /// writes it to the output stream ready for the XmlHttpRequest object to complete its /// call. Note that data transport uses JSON encoding. See the JSON class in the /// Sprocket.System namespace for information. /// </summary> /// <param name="context">The current HttpContext object.</param> internal void ProcessRequest(HttpContext context) { isAjaxRequest = true; System.Diagnostics.Debug.WriteLine("Start of AJAX page request..."); Dictionary <string, object> responseData = new Dictionary <string, object>(); try { // load the post data from the http stream byte[] posted = new byte[context.Request.InputStream.Length]; context.Request.InputStream.Read(posted, 0, posted.Length); // interpret the stream as a JSON string and parse it into a dictionary string strData = System.Text.Encoding.ASCII.GetString(posted); IDictionary <string, object> data = (IDictionary <string, object>)JSON.Parse(strData); // extract the base page time stamp //pageTimeStamp = new DateTime(long.Parse(data["LoadTimeStamp"].ToString())); //System.Diagnostics.Debug.WriteLine("Extracted page time stamp of " + pageTimeStamp.Ticks.ToString()); //if (OnAjaxRequestTimeStampCheck != null) //{ // Result result = new Result(); // OnAjaxRequestTimeStampCheck(pageTimeStamp, result); // if (!result.Succeeded) // throw new AjaxSessionExpiredException(result.Message); //} // extract the module and method name string fullname; try { fullname = data["ModuleName"].ToString(); } catch (Exception ex) { throw; } int n = fullname.LastIndexOf("."); if (n == -1) { throw new AjaxException("Method name specified incorrectly. Expected format ModuleNamespace.MethodName.\nThe following incorrect format was supplied: " + data["ModuleName"]); } string moduleNamespace = fullname.Substring(0, n); string methodName = fullname.Substring(n + 1, fullname.Length - (n + 1)); // extract the authentication key if (data["AuthKey"].ToString() != WebAuthentication.AuthKeyPlaceholder) { authKey = new Guid(data["AuthKey"].ToString()); } // extract the source URL SprocketPath.Parse(data["SourceURL"].ToString()); // extract the arguments List <object> parsedArguments = (List <object>)data["MethodArgs"]; // find and verify the module/method that should handle this request ISprocketModule module = Core.Instance[moduleNamespace].Module; if (module == null) { throw new AjaxException("The specified module \"" + moduleNamespace + "\" was not found."); } if (Attribute.GetCustomAttribute(module.GetType(), typeof(AjaxMethodHandlerAttribute), false) == null) { throw new SprocketException("The specified module is not marked with AjaxMethodHandlerAttribute. (" + data["ModuleName"] + ")"); } MethodInfo info = module.GetType().GetMethod(methodName); if (info == null) { throw new AjaxException("Failed to find an instance of the specified method. (" + data["ModuleName"] + ")"); } Attribute ajaxMethodAttr = Attribute.GetCustomAttribute(info, typeof(AjaxMethodAttribute)); if (ajaxMethodAttr == null) { throw new AjaxException("Specified method is not marked with AjaxMethodAttribute. (" + data["ModuleName"] + ")"); } AjaxMethodAttribute attr = (AjaxMethodAttribute)ajaxMethodAttr; if (attr.RequiresAuthentication) { if (!WebAuthentication.IsLoggedIn) { AjaxRequestHandler.AbortAjaxCall("You're not currently logged in. Please refresh the page."); } if (AjaxAuthenticate != null) { Result result = AjaxAuthenticate(info); if (!result.Succeeded) { throw new AjaxException(result.Message); } } } // get all of the parameters that the method requires ParameterInfo[] methodParamInfos = info.GetParameters(); // funcinfo is a string representation of the method format and is used for displaying meaningful errors string funcinfo = data["ModuleName"] + "("; if (methodParamInfos.Length > 0) { funcinfo += methodParamInfos[0].ParameterType.Name + " " + methodParamInfos[0].Name; } for (int j = 1; j < methodParamInfos.Length; j++) { funcinfo += ", " + methodParamInfos[j].ParameterType.Name + " " + methodParamInfos[j].Name; } funcinfo += ")"; if (methodParamInfos.Length != parsedArguments.Count) { throw new AjaxException("Method expects " + methodParamInfos.Length + " argument(s) but instead received " + (parsedArguments.Count) + ". Expected format is:\n" + funcinfo); } // create the parameter array and convert each supplied value to its native type object[] prmValuesForMethod = new object[methodParamInfos.Length]; for (int i = 0; i < prmValuesForMethod.Length; i++) { Type t = methodParamInfos[i].ParameterType; try { if (parsedArguments[i] == null) { prmValuesForMethod[i] = null; } else if (t.Name == "Object") { prmValuesForMethod[i] = parsedArguments[i]; } else if (t.Name == "DateTime") { prmValuesForMethod[i] = DateTime.Parse(parsedArguments[i].ToString()); } else if (t.IsArray || t.IsSubclassOf(typeof(IList))) { int elementCount = ((List <object>)parsedArguments[i]).Count; Type arrType = t.GetElementType().MakeArrayType(elementCount); object arr = Array.CreateInstance(t.GetElementType(), ((List <object>)parsedArguments[i]).Count); for (int k = 0; k < ((IList <object>)parsedArguments[i]).Count; k++) { ((IList)arr)[k] = ((IList <object>)parsedArguments[i])[k]; } prmValuesForMethod[i] = arr; } else if (t.GetInterface("IJSONReader") != null) { object obj = Activator.CreateInstance(t); ((IJSONReader)obj).LoadJSON(parsedArguments[i]); prmValuesForMethod[i] = obj; } else if (t.IsAssignableFrom(typeof(Guid)) || t.IsAssignableFrom(typeof(Guid?))) { prmValuesForMethod[i] = parsedArguments[i] == null ? (Guid?)null : new Guid(parsedArguments[i].ToString()); } else if (t.IsAssignableFrom(typeof(long)) || t.IsAssignableFrom(typeof(long?))) { prmValuesForMethod[i] = parsedArguments[i] == null ? (long?)null : long.Parse(parsedArguments[i].ToString()); } else { prmValuesForMethod[i] = Convert.ChangeType(parsedArguments[i], t); } } catch (Exception ex) { string err = "Error converting parameter {0} to type {1}. Expected format is:\n{2}\nReceived Value:\n{3}\nException message:\n{4}"; throw new AjaxException(string.Format(err, i, methodParamInfos[i].ParameterType.Name, funcinfo, parsedArguments[i], ex)); } } // invoke the method if (info.ReturnType == typeof(void)) { info.Invoke(module, prmValuesForMethod); } else { object returnVal = info.Invoke(module, prmValuesForMethod); responseData["Data"] = returnVal; } //context.Response.Write(JSON.Encode(responseData)); } catch (Exception e) { if (!(e is AjaxUserMessageException) && SprocketSettings.GetBooleanValue("CatchExceptions")) { if (e.InnerException != null) { throw e.InnerException; } throw e; } else if (e.InnerException != null) { e = e.InnerException; } responseData["__error"] = e; responseData["__exceptionType"] = e.GetType().FullName; } if (!responseData.ContainsKey("Data")) { responseData["Data"] = null; } //responseData["__timeStamp"] = pageTimeStamp.Ticks; context.Response.Write(JSON.Encode(responseData)); }