void AssemblyGetResponse (IAsyncResult result)
{
object[] tuple = (object []) result.AsyncState;
WebRequest wreq = (WebRequest) tuple [0];
int error_code = (int) tuple [1];
try {
HttpWebResponse wresp = (HttpWebResponse) wreq.EndGetResponse (result);
if (wresp.StatusCode != HttpStatusCode.OK) {
wresp.Close ();
EmitError (error_code, String.Format ("Error while downloading the '{0}'.", wreq.RequestUri));
return;
}
if (wresp.ResponseUri != wreq.RequestUri) {
wresp.Close ();
EmitError (error_code, "Redirection not allowed to download assemblies.");
return;
}
Stream responseStream = wresp.GetResponseStream ();
AssemblyPart a = new AssemblyPart ();
Assembly asm = a.Load (responseStream);
if (asm == null) {
// it's not a valid assembly, try to unzip it.
using (MemoryStream ms = new MemoryStream ()) {
ManagedStreamCallbacks source_cb;
ManagedStreamCallbacks dest_cb;
StreamWrapper source_wrapper;
StreamWrapper dest_wrapper;
responseStream.Seek (0, SeekOrigin.Begin);
source_wrapper = new StreamWrapper (responseStream);
dest_wrapper = new StreamWrapper (ms);
source_cb = source_wrapper.GetCallbacks ();
dest_cb = dest_wrapper.GetCallbacks ();
// the zip files I've come across have a single file in them, the
// dll. so we assume that any/every zip file will contain a single
// file, and just get the first one from the zip file directory.
if (NativeMethods.managed_unzip_stream_to_stream_first_file (ref source_cb, ref dest_cb)) {
ms.Seek (0, SeekOrigin.Begin);
asm = a.Load (ms);
}
}
}
wresp.Close ();
if (asm != null)
Dispatcher.BeginInvoke (new AssemblyRegistration (AssemblyRegister), asm);
else
EmitError (2153, String.Format ("Error while loading '{0}'.", wreq.RequestUri));
}
catch (Exception e) {
// we need to report everything since any error means CreateApplication won't be called
EmitError (error_code, e.ToString ());
}
}