Ejemplo n.º 1
0
        public IContentResponse Post(string method, string rawUrl, NameValueCollection headers, Stream inputStream)
        {
            try
            {
                try
                {
                    Assert("POST", method);
                    Assert("application/xml", headers["Content-Type"]);
                    if (int.Parse(headers["Content-Length"]) > 4096)
                        throw new ArgumentException("Content exceeds limit of 4096 bytes.");

                    string xmessage;
                    using (TextReader rdr = new StreamReader(inputStream, Encoding.UTF8, false))
                        xmessage = rdr.ReadToEnd();

                    XmlLightDocument doc = new XmlLightDocument(xmessage);
                    Assert("methodCall", doc.Root.LocalName);
                    Assert("pingback.ping", doc.SelectRequiredNode("/methodCall/methodName").InnerText);
                    XmlLightElement[] args = doc.Select("/methodCall/params/param").ToArray();
                    if (args.Length != 2)
                        throw new ArgumentException("Invalid number of arguments, expected: 2");

                    Uri source, dest;
                    if (!Uri.TryCreate(args[0].InnerText, UriKind.Absolute, out source) ||
                        !Uri.TryCreate(args[1].InnerText, UriKind.Absolute, out dest))
                        throw new ArgumentException("Invalid uri format.");

                    RecordPingback(source, dest);

                    return new DynamicResponse("application/xml",
                        Encoding.UTF8.GetBytes(@"<?xml version=""1.0""?><methodResponse><params><param><value>Accepted</value></param></params></methodResponse>")
                        );
                }
                catch (ArgumentException e)
                {
                    return new DynamicResponse("application/xml",
                        Encoding.UTF8.GetBytes(
                        String.Format(
@"<?xml version=""1.0""?>
<methodResponse>
  <fault>
    <value>
      <struct>
        <member><name>faultCode</name><value><int>{0}</int></value></member>
        <member><name>faultString</name><value><string>{1}</string></value></member>
      </struct>
    </value>
  </fault>
</methodResponse>", 0, HttpUtility.HtmlEncode(e.Message))));
                }
            }
            catch (Exception err)
            {
                Log.Error(err);
                return HttpErrorHandler.InternalServerError.GetResponse(method, rawUrl, headers, inputStream);
            }
        }
Ejemplo n.º 2
0
            private bool RewriteXmlDocument(XmlLightDocument doc)
            {
                foreach (HttpCloneXPath path in _xpaths)
                {
                    foreach (XmlLightElement found in doc.Select(path.XPath))
                    {
                        _elements.Add(found, path);
                    }
                }

                return false;
            }
Ejemplo n.º 3
0
        public void ReadFrom(string versionInfo)
        {
            StringWriter asmInfo = new StringWriter();
            asmInfo.WriteLine("using System;");
            asmInfo.WriteLine();

            using (DisposingList disposable = new DisposingList())
            {
                if (versionInfo.EndsWith(".csproj", StringComparison.OrdinalIgnoreCase))
                {
                    string dir = Path.GetDirectoryName(versionInfo);
                    XmlLightDocument proj = new XmlLightDocument(File.ReadAllText(versionInfo));
                    foreach (XmlLightElement xref in proj.Select("/Project/ItemGroup/Compile"))
                    {
                        if (!xref.Attributes.ContainsKey("Include") ||
                            !xref.Attributes["Include"].EndsWith("AssemblyInfo.cs", StringComparison.OrdinalIgnoreCase))
                            continue;
                        string include = xref.Attributes["Include"];
                        versionInfo = Path.Combine(dir, include);
                        break;
                    }
                    if (versionInfo.EndsWith(".csproj", StringComparison.OrdinalIgnoreCase))
                        throw new ApplicationException("Unable to locate AssemblyInfo.cs");
                }
                if (versionInfo.EndsWith(".cs", StringComparison.OrdinalIgnoreCase))
                {
                    _assemblyInfo = File.ReadAllText(versionInfo);

                    TempFile dll = TempFile.FromExtension(".dll");
                    disposable.Add(dll);
                    dll.Delete();

                    CSharpCodeProvider csc = new CSharpCodeProvider();
                    CompilerParameters args = new CompilerParameters();
                    args.GenerateExecutable = false;
                    args.IncludeDebugInformation = false;
                    args.OutputAssembly = dll.TempPath;
                    args.ReferencedAssemblies.Add("System.dll");
                    CompilerResults results = csc.CompileAssemblyFromFile(args, versionInfo);

                    StringWriter sw = new StringWriter();
                    foreach (CompilerError ce in results.Errors)
                    {
                        if (ce.IsWarning) continue;
                        String line = String.Format("{0}({1},{2}): error {3}: {4}", ce.FileName, ce.Line, ce.Column,
                                                    ce.ErrorNumber, ce.ErrorText);
                        Trace.WriteLine(line);
                        sw.WriteLine(line);
                    }
                    string errorText = sw.ToString();
                    if (errorText.Length > 0)
                        throw new ApplicationException(errorText);
                    versionInfo = dll.TempPath;
                }
                if (!File.Exists(versionInfo) || (!versionInfo.EndsWith(".dll", StringComparison.OrdinalIgnoreCase) && !versionInfo.EndsWith(".exe", StringComparison.OrdinalIgnoreCase)))
                    throw new ApplicationException("Expected an existing dll: " + versionInfo);

                try
                {
                    Regex truefalse = new Regex(@"(?<=[^\w_\.])(?:True)|(?:False)(?=[^\w_\.])");
                    _fileVersion = FileVersionInfo.GetVersionInfo(versionInfo);

                    if (_assemblyInfo == null)
                    {
                        Assembly asm = Assembly.ReflectionOnlyLoad(File.ReadAllBytes(versionInfo));
                        IList<CustomAttributeData> attrs = CustomAttributeData.GetCustomAttributes(asm);
                        foreach (CustomAttributeData data in attrs)
                        {
                            if (!data.ToString().StartsWith("[System.Runtime.CompilerServices."))
                            {
                                string attribute = data.ToString().Trim();
                                if (attribute[0] != '[')
                                    continue; //unexpected...
                                attribute = attribute.Insert(1, "assembly: ");
                                attribute = StringUtils.Transform(attribute, truefalse,
                                                                  delegate(Match x) { return x.Value.ToLower(); });
                                asmInfo.WriteLine(attribute);
                            }
                        }
                    }
                }
                catch
                {
                }
            }

            _assemblyInfo = _assemblyInfo ?? asmInfo.ToString();
        }