Esempio n. 1
0
        protected override bool Process(IInteraction parameters)
        {
            HttpInteraction interaction = (HttpInteraction)parameters.GetClosest(typeof(HttpInteraction));

            string mimeType;

            if (MimeType.Length > 0)
            {
                if (this.CharSet.Length > 0)
                {
                    mimeType = string.Format(
                        "{0}; charset={1}",
                        this.MimeType,
                        this.CharSet);
                }
                else
                {
                    mimeType = this.MimeType;
                }
                interaction.SetContentType(mimeType);
            }

            interaction.SetStatusCode(this.Statuscode);

            return(WithBranch.TryProcess(parameters));
        }
Esempio n. 2
0
        protected override bool Process(IInteraction parameters)
        {
            IHttpInteraction httpInteraction = Closest <IHttpInteraction> .From(parameters);

            string contentType;

            if (this.ContentType.Length > 0)
            {
                contentType = this.ContentType;
            }
            else
            {
                contentType = Fallback <string> .From(parameters, "contenttype");
            };

            httpInteraction.SetContentType(contentType);

            if (this.SendLength)
            {
                long length = Fallback <long> .From(parameters, "contentlength");

                httpInteraction.SetContentLength(length);
            }

            httpInteraction.PurgeBuffer();

            return(WithBranch.TryProcess(parameters));
        }
Esempio n. 3
0
        protected override bool Process(IInteraction parameters)
        {
            var processor = StringProcessorInteraction.From(parameters);
            var success   = WithBranch.TryProcess(processor);

            processor.Run(this.compressor.Compress);
            return(success);
        }
Esempio n. 4
0
        protected bool TryGetImage(IInteraction parameters, ImageProcessor imageCallback)
        {
            bool success = false;

            IInteraction candidateImageOut;

            if (parameters.TryGetClosest(typeof(IOutgoingBodiedInteraction), out candidateImageOut))
            {
                IOutgoingBodiedInteraction imageOut = (IOutgoingBodiedInteraction)candidateImageOut;

                if (imageOut is IHttpInteraction)
                {
                    if (this.UseJpgInsteadOfPng)
                    {
                        ((IHttpInteraction)imageOut).ResponseHeaders["Content-Type"] = "image/jpg";
                    }
                    else
                    {
                        ((IHttpInteraction)imageOut).ResponseHeaders["Content-Type"] = "image/png";
                    }
                }

                MemoryStream imageData = new MemoryStream();
                SimpleOutgoingInteraction imageSourcer = new SimpleOutgoingInteraction(
                    imageData, Encoding.Default, parameters);

                if (WithBranch.TryProcess(imageSourcer))
                {
                    Image inImage = Bitmap.FromStream(imageData);

                    Bitmap outImage = imageCallback(inImage);

                    inImage.Dispose();

                    if (outImage != emptyBitmap)
                    {
                        outImage.Save(imageOut.OutgoingBody, selectedImageformat);

                        outImage.Dispose();
                    }

                    success = true;
                }
                else
                {
                    Secretary.Report(5, "Image source failure");
                }
            }
            else
            {
                Secretary.Report(5, "No outgoing body found to write result image to.");
            }

            return(success);
        }
Esempio n. 5
0
        protected override bool Process(IInteraction parameters)
        {
            StringComposeInteraction composer = new StringComposeInteraction(parameters, Encoding.UTF8);

            bool success = WithBranch.TryProcess(composer);

            if (log)
            {
                Secretary.Report(5, composer.ToString());
            }

            return(success);
        }
Esempio n. 6
0
        protected override bool Process(IInteraction parameters)
        {
            IInteraction foundInteraction;

            if (parameters.TryGetClosest(typeof(RecursionMarkerInteraction), out foundInteraction) && ((RecursionMarkerInteraction)foundInteraction).Placer == this)
            {
                throw new Exception("Recursion halted");
            }
            else
            {
                return(WithBranch.TryProcess(new RecursionMarkerInteraction(parameters, this)));
            }
        }
Esempio n. 7
0
 private void JobWorker(object argument)
 {
     while (IsWorking)
     {
         if (jobs.Count == 0)
         {
             locker.WaitOne();
             locker.Release();
         }
         lock (jobs) {
             WithBranch.TryProcess(jobs.Dequeue());
         }
     }
 }
Esempio n. 8
0
        protected override string GetCacheName(IInteraction parameters)
        {
            NosyInteraction interaction = new NosyInteraction(includeContext, parameters);
            string          cacheName;

            if (WithBranch.TryProcess(interaction))
            {
                cacheName = interaction.Signature;
            }
            else
            {
                throw new Exception("begin-branch of nosy interaction produced false.");
            }

            return(cacheName);
        }
Esempio n. 9
0
        protected override bool Process(IInteraction parameters)
        {
            bool success = true;
            IOutgoingBodiedInteraction upstreamTarget;

            upstreamTarget = (IOutgoingBodiedInteraction)parameters.GetClosest(
                typeof(IOutgoingBodiedInteraction));

            if (timeUntilCacheDrop != TimeSpan.MaxValue)
            {
                if (DateTime.Now - lastUpdate > timeUntilCacheDrop)
                {
                    Data = null;
                }
            }

            if (Data == null)
            {
                MemoryStream targetStream = new MemoryStream();
                SimpleOutgoingInteraction downstreamTarget = new SimpleOutgoingInteraction(
                    targetStream, upstreamTarget.Encoding, parameters);

                success = WithBranch.TryProcess(downstreamTarget);
                downstreamTarget.Done();

                targetStream.Position = 0;

                if (downstreamTarget.HasWriter())
                {
                    using (StreamReader reader = new StreamReader(targetStream))
                        Data = downstreamTarget.Encoding.GetBytes(reader.ReadToEnd());
                }
                else
                {
                    Data = new byte[targetStream.Length];
                    targetStream.Read(Data, 0, Data.Length);
                    targetStream.Dispose();
                }

                lastUpdate = DateTime.Now;
            }

            upstreamTarget.OutgoingBody.Write(Data, 0, Data.Length);

            return(success);
        }
Esempio n. 10
0
        protected override bool Process(IInteraction parameters)
        {
            bool success = true;

            IDbTransaction actualTransaction = this.Connection.BeginTransaction();

            success &= WithBranch.TryProcess(new TransactionInteraction(this.Connection, this.DatasourceName, parameters));

            if (this.Rollback)
            {
                actualTransaction.Rollback();
            }
            else
            {
                actualTransaction.Commit();
            }

            return(success);
        }
Esempio n. 11
0
        void HandleNewClient(object sender, NewClientEventArgs e)
        {
            try {
                Pipe pipe = new Pipe(e.Client.Client);

                while (this.HoldTheLine)
                {
                    pipe.Handshake();
                    WithBranch.TryProcess(new ReachInteraction(pipe));
                    pipe.SendCommand(Command.Close);
                }

                e.Client.Close();
            } catch (SocketException ex) {
                Secretary.Report(5, "Connection failed at", this.Description);
            } catch (Exception ex) {
                Secretary.Report(5, "Misc. exception occured at", this.Description, ":", ex.Message);
                e.Client.Close();
            }
        }
Esempio n. 12
0
        protected override bool Process(IInteraction parameters)
        {
            if (parameters is INosyInteraction)
            {
                return(WithBranch.TryProcess(parameters));
            }
            else
            {
                IOutgoingBodiedInteraction upstreamTarget;
                SimpleOutgoingInteraction  downstreamTarget;
                MemoryStream formattables;
                bool         success;
                string       text;

                upstreamTarget   = (IOutgoingBodiedInteraction)parameters.GetClosest(typeof(IOutgoingBodiedInteraction));
                downstreamTarget = new SimpleOutgoingInteraction(
                    formattables = new MemoryStream(), upstreamTarget.Encoding, parameters);

                success = WithBranch.TryProcess(downstreamTarget);
                downstreamTarget.Done();

                formattables.Position = 0;

                using (StreamReader reader = new StreamReader(formattables))
                    text = reader.ReadToEnd();

                foreach (Format format in formats)
                {
                    text = format.Apply(text);
                }


                byte[] data = Encoding.Unicode.GetBytes(text);

                upstreamTarget.OutgoingBody.Write(data, 0, data.Length);

                return(success);
            }
        }
Esempio n. 13
0
 protected override bool Process(IInteraction parameters)
 {
     return(WithBranch.TryProcess(new CredentialInteraction(this.CurrentCredentials, parameters)));
 }
Esempio n. 14
0
		protected override bool Process (IInteraction parameters)
		{
			return WithBranch.TryProcess(parameters);
		}
Esempio n. 15
0
 public override void OnReady()
 {
     WithBranch.TryProcess(new SimpleInteraction());
 }
Esempio n. 16
0
 protected override bool Process(IInteraction parameters)
 {
     return(WithBranch.TryProcess(new CheckInteraction(parameters)));
 }
Esempio n. 17
0
        protected override bool Process(IInteraction uncastParameters)
        {
            bool success = false;

            IInteraction     candidateParameters;
            IHttpInteraction parameters;

            if (((candidateParameters = uncastParameters.Parent) is IHttpInteraction) || uncastParameters.TryGetClosest(typeof(IHttpInteraction), out candidateParameters))
            {
                parameters = (IHttpInteraction)candidateParameters;

                string givenCookie = parameters.GetCookie(CookieName);

                if (givenCookie != null)
                {
                    if (Closing)
                    {
                        SessionStates [givenCookie] = SessionState.Closed;
                    }

                    if (SessionStates [givenCookie] == SessionState.Closed)
                    {
                        givenCookie = "";
                    }
                }

                if (Closing && (givenCookie != null))
                {
                    SessionStates [givenCookie] = SessionState.Closed;
                }



                if ((givenCookie ?? "").Length == 0)                     // an empty sescookie
                // we create a cookie

                {
                    string cookieValue;

                    do                       // we have a loop here for a stupidly rare case that probably only occurs
                                             // when we don't expect it to, so there.
                                             // The Base64 thing is there to make sure only letters and numbers
                                             // end up in the cookie.

                    {
                        cookieValue = Convert.ToBase64String(GetTrue(128));

                        Console.WriteLine(string.Format("cookie length {0}", cookieValue.Length));
                    } while (SessionStates.Has(cookieValue));

                    parameters.SetCookie(CookieName, cookieValue, this.IsSecureSession);

                    givenCookie = cookieValue;

                    SessionStates [cookieValue] = SessionState.Existing;

                    // Yes, I made the Sescookie-creation loop around in case of a duplicate
                    // gloBALLY UNIQUE IDENTIFIER now hand me my tinfoil hat.
                }

                success = WithBranch.TryProcess(new SessionInteraction(uncastParameters, CookieName, givenCookie));
            }

            return(success);
        }