/usr/share/asp.net-demos/1.1/webservice/CompressExtension.cs is in asp.net-examples 4.2-2.1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 | //
// CompressExtension.cs
//
// Author:
// Lluis Sanchez Gual (lluis@ximian.com)
//
// Copyright (C) Ximian, Inc. 2003
//
using System;
using System.Text;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Web.Services.Description;
using System.Web.Services.Configuration;
using System.CodeDom;
using System.IO;
using System.Net;
using System.Xml.Serialization;
using ICSharpCode.SharpZipLib.GZip;
using System.ComponentModel;
public class CompressExtension : SoapExtension
{
Stream netStream;
MemoryStream tempStream;
int minLength;
public CompressExtension ()
{
}
public override Stream ChainStream (Stream stream)
{
netStream = stream;
tempStream = new MemoryStream ();
return tempStream;
}
public override object GetInitializer(LogicalMethodInfo methodInfo, SoapExtensionAttribute attribute)
{
return ((CompressAttribute)attribute).MinLength;
}
public override object GetInitializer (Type webServiceType)
{
return 0;
}
public override void Initialize(object initializer)
{
minLength = (int) initializer;
}
public override void ProcessMessage(SoapMessage message)
{
switch (message.Stage)
{
case SoapMessageStage.BeforeSerialize:
break;
case SoapMessageStage.AfterSerialize:
Compress (message);
break;
case SoapMessageStage.BeforeDeserialize:
Decompress (message);
break;
case SoapMessageStage.AfterDeserialize:
break;
default:
throw new Exception("invalid stage");
}
}
public void Compress (SoapMessage message)
{
if (tempStream.Length >= minLength)
{
MemoryStream mems = new MemoryStream ();
GZipOutputStream zos = new GZipOutputStream (mems);
zos.Write (tempStream.GetBuffer (), 0, (int) tempStream.Length);
zos.Finish ();
Console.WriteLine ("msg len:" + tempStream.Length);
// Convert the compressed content to a base 64 string
string compString = Convert.ToBase64String (mems.GetBuffer (), 0, (int)mems.Length);
byte[] compBytes = Encoding.UTF8.GetBytes (compString);
netStream.WriteByte ((byte)'C'); // Compressing flag
netStream.Write (compBytes, 0, compBytes.Length);
Console.WriteLine ("cmp len:" + compBytes.Length);
netStream.Flush ();
zos.Close ();
}
else
{
netStream.WriteByte ((byte)'N'); // Not Compressing flag
netStream.Write (tempStream.GetBuffer(), 0, (int) tempStream.Length);
netStream.Flush ();
}
}
public void Decompress (SoapMessage message)
{
char cf = (char) netStream.ReadByte ();
Stream sourceStream;
if (cf == 'C') {
StreamReader sr = new StreamReader (netStream, Encoding.UTF8);
string compString = sr.ReadToEnd ();
sr.Close ();
byte[] compBytes = Convert.FromBase64String (compString);
MemoryStream mems = new MemoryStream (compBytes);
sourceStream = new GZipInputStream (mems);
}
else {
sourceStream = netStream;
}
int len = 0;
byte[] buffer = new byte[1024];
while ((len = sourceStream.Read (buffer, 0, buffer.Length)) != 0)
tempStream.Write (buffer, 0, len);
// sourceStream.Close ();
tempStream.Position = 0;
}
}
[AttributeUsage(AttributeTargets.Method)]
public class CompressAttribute: SoapExtensionAttribute
{
private int priority = 0;
private int minLength = 0;
public override Type ExtensionType
{
get { return typeof (CompressExtension); }
}
public override int Priority
{
get { return priority; }
set { priority = value; }
}
public int MinLength
{
get { return minLength; }
set { minLength = value; }
}
}
public class CompressExtensionImporter : SoapExtensionImporter
{
public override void ImportMethod (CodeAttributeDeclarationCollection metadata)
{
CompressOperationBinding cob = ImportContext.OperationBinding.Extensions.Find (typeof (CompressOperationBinding)) as CompressOperationBinding;
if (cob == null) return; // Extension element not present
CodeAttributeDeclaration att = new CodeAttributeDeclaration ("Compress");
if (cob.MinLength != 0) att.Arguments.Add (new CodeAttributeArgument ("MinLength", new CodePrimitiveExpression(cob.MinLength)));
metadata.Add (att);
}
}
public class CompressExtensionReflector : SoapExtensionReflector
{
public override void ReflectMethod ()
{
object[] ats = ReflectionContext.Method.MethodInfo.GetCustomAttributes (typeof (CompressAttribute), true);
if (ats.Length > 0)
{
CompressAttribute at = (CompressAttribute) ats[0];
CompressOperationBinding opBinding = new CompressOperationBinding();
opBinding.MinLength = at.MinLength;
ReflectionContext.OperationBinding.Extensions.Add (opBinding);
}
}
}
[XmlFormatExtension ("compress", "http://www.go-mono.org/Samples", typeof (OperationBinding))]
[XmlFormatExtensionPrefix ("mono", "http://www.go-mono.org/Samples")]
public class CompressOperationBinding : ServiceDescriptionFormatExtension
{
int minLength;
[XmlAttribute]
[DefaultValue (0)]
public int MinLength
{
get { return minLength; }
set { minLength = value; }
}
}
|