예제 #1
0
        public override HttpResponse Invoke(Dictionary <string, string> arguments, ClientContainer client)
        {
            HttpResponse response;

            helper = new HelperClass(client.getClient().GetStream(), client.getClient());

            if (arguments == null || arguments.Count < 1 || !arguments.ContainsKey(filePathVariable) || !arguments.ContainsKey(fileNameVariable))
            {
                procedureFail(client);
                return(FormatInvokeFailure());
            }
            string path = (arguments[filePathVariable]).Replace('/', '\\').Trim();

            if (!path.EndsWith("\\"))
            {
                path += '\\';
            }
            path += arguments[fileNameVariable];
            try
            {
                FileInfo thisfile = new FileInfo(path);
                Process  p        = new Process()
                {
                    StartInfo = new ProcessStartInfo(thisfile.FullName)
                };
                if (p.Start())
                {
                    return(FormatInvokeSuccess());
                }
                else
                {
                    return(FormatInvokeFailure());
                }
                //helper.SocketWriteLine("");
            }
            catch (IOException)
            {
                return(procedureFail(client));
            }
            catch (UnauthorizedAccessException)
            {
                return(procedureFail(client));
            }

            return(null); // do not send out a response from the engine
        }
예제 #2
0
        public override HttpResponse Invoke(Dictionary <string, string> arguments, ClientContainer client)
        {
            HttpResponse response;

            helper = new HelperClass(client.getClient().GetStream(), client.getClient());

            if (arguments == null || arguments.Count < 1 || !arguments.ContainsKey(filePathVariable))
            {
                procedureFail(client);
                return(FormatInvokeFailure());
            }
            string path = ((string)arguments[filePathVariable]).Replace('/', '\\').Trim();

            response = new HttpResponse(HttpResponse.ConnectionStatus.OK, "keep-alive", null);
            if (attatchment)
            {
                response.addHeader("Content-Disposition", "attachment; filename=\"" + new FileInfo(path).Name + '"');
            }
            try
            {
                FileStream file = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
                response.addHeader("Content-Length", file.Length.ToString());
                helper.SocketWriteLine(response.ToString());
                file.CopyTo(client.getClient().GetStream());
                file.Close();
                //helper.SocketWriteLine("");
            }
            catch (IOException ex)
            {
                return(procedureFail(client));
            }
            catch (UnauthorizedAccessException)
            {
                return(procedureFail(client));
            }

            return(null); // do not send out a response from the engine
        }
        public override HttpResponse Invoke(Dictionary <string, string> arguments, ClientContainer client)
        {
            Stream       pipe         = client.getClient().GetStream();
            HttpResponse thisResponse = new HttpResponse(HttpResponse.ConnectionStatus.OK, "keep-alive", null);
            var          helper       = new HelperClass(pipe, client.getClient());

            var imageHolder = new MemoryStream();

            Rectangle rect = screenDem; // get screen resolution

            if (keyCenter != null)
            {
                if (!arguments.ContainsKey(keyCenter))
                {
                    return(FormatInvokeFailure());
                }
                if (!arguments.ContainsKey(keyDemensions))
                {
                    return(FormatInvokeFailure());
                }

                string Size = arguments[keyDemensions];
                string Center = arguments[keyCenter];
                int    imageHeight, imageWidth;
                int    centerX, centerY;
                try
                {
                    string[] peices = Size.Split(',');
                    imageWidth  = Convert.ToInt32(peices[0]);
                    imageHeight = Convert.ToInt32(peices[1]);

                    peices  = Center.Split(',');
                    centerX = Convert.ToInt32(peices[0]);
                    centerY = Convert.ToInt32(peices[1]);
                }
                catch (FormatException)
                { return(FormatInvokeFailure()); }

                double scale = rect.Width / Convert.ToDouble(imageWidth);

                int realX = Convert.ToInt32((centerX * scale) - (imageWidth / 2));
                int realY = Convert.ToInt32((centerY * scale) - (imageHeight / 2));

                int xbound = rect.Width - imageWidth;
                int ybound = rect.Height - imageHeight;

                if (realX > xbound)
                {
                    realX = xbound;
                }
                if (realY > ybound)
                {
                    realY = ybound;
                }
                if (realX < 0)
                {
                    realX = 0;
                }
                if (realY < 0)
                {
                    realY = 0;
                }

                rect = new Rectangle(realX, realY, imageWidth, imageHeight);
            }
            if (keyCorner != null)
            {
                int cornerX, cornerY;
                int imageHeight, imageWidth;
                try
                {
                    string[] peices = arguments[keyCorner].Split(',');
                    cornerX = Convert.ToInt32(peices[0]);
                    cornerY = Convert.ToInt32(peices[1]);

                    peices      = arguments[keyDemensions].Split(',');
                    imageWidth  = Convert.ToInt32(peices[0]);
                    imageHeight = Convert.ToInt32(peices[1]);
                }
                catch (FormatException)
                { return(FormatInvokeFailure()); }
                catch (KeyNotFoundException)
                { return(FormatInvokeFailure()); }

                rect = new Rectangle(cornerX, cornerY, imageWidth, imageHeight);
            }

            var      bmp = new Bitmap(rect.Width, rect.Height, PixelFormat.Format32bppArgb); // needs updated rect
            Graphics g   = Graphics.FromImage(bmp);

            try
            {
                g.CopyFromScreen(rect.Left, rect.Top, 0, 0, bmp.Size, CopyPixelOperation.SourceCopy);
            }
            catch (System.ComponentModel.Win32Exception)
            {
                SizeF PainInTheNeck = g.MeasureString("Error", SystemFonts.DefaultFont);
                g = Graphics.FromImage((bmp = new Bitmap((int)PainInTheNeck.Width, (int)PainInTheNeck.Height, PixelFormat.Format32bppArgb)));
                g.DrawString("Error", SystemFonts.DefaultFont, Brushes.Red, new PointF(0, 0));
            }
            switch (FileType)
            {
            case FileFormat.JPG:
                bmp.Save(imageHolder, ImageFormat.Jpeg);
                thisResponse.addHeader("Content-Type", "image/png");
                break;

            case FileFormat.PNG:
                bmp.Save(imageHolder, ImageFormat.Png);
                thisResponse.addHeader("Content-Type", "image/png");
                break;
            }

            imageHolder.Position = 0;

            try
            {
                thisResponse.addHeader("Content-Length", imageHolder.Length.ToString());
                helper.SocketWriteLine(thisResponse.ToString());
                imageHolder.CopyTo(pipe);
            }
            catch (IOException) { }
            return(null);
        }
예제 #4
0
        public override HttpResponse Invoke(Dictionary <string, string> arguments, ClientContainer client)
        {
            Stream inStream = client.getClient().GetStream();

            if (arguments == null || arguments.Count < 1 || !arguments.ContainsKey(filePathVariable))
            {
                return(FormatInvokeFailure());
            }
            string     path = ((string)arguments[filePathVariable]).Replace('/', '\\').Trim();
            FileStream filestream;

            if (File.Exists(path))
            {
                return(FormatInvokeFailure(",because of an invalid path, has"));
            }
            try
            {
                int contentlen = 0;
                try
                {
                    contentlen = Convert.ToInt32(client.CurrentRequest.requestMetaInfo("Content-Length").Trim());
                }
                catch (KeyNotFoundException)
                {
                    return(FormatInvokeFailure(", because content-length is not supported by this browser, has"));
                }
                int bytesReceived = 0;

                string boundary     = client.CurrentRequest.requestMetaInfo("Content-Type").Split(new string[] { "boundary=" }, StringSplitOptions.None)[1];
                String strMultiPart = client.CurrentRequest.MessageBody;
                byte[] multiPartRAW = client.CurrentRAW;

                byte[] buffer    = new byte[1024];
                bool   isChunked = client.CurrentRequest.MessageBody.Length < 5;
                if (isChunked) // the multipart form data was not included in the origonal data
                {
                    int endread = inStream.Read(buffer, 0, buffer.Length);
                    multiPartRAW = new byte[endread];
                    for (int i = 0; i < endread; i++)
                    {
                        multiPartRAW[i] = buffer[i];
                    }
                    strMultiPart = Encoding.UTF8.GetString(multiPartRAW);
                }

                string filename = HelperClass.getIsolatedString("filename=\"", "\"", strMultiPart);//client.CurrentRequest.ToString().Split(new string[] { "filename=\"", "\"" },StringSplitOptions.None)[0];

                if (!path.EndsWith("\\"))
                {
                    path += '\\';
                }

                filestream = File.Create(path + filename);

                int startfileindex = strMultiPart.IndexOf("\r\n\r\n") + "\r\n\r\n".Length;
                int sy             = strMultiPart.IndexOf(boundary, startfileindex + 1);
                int filelen        = sy - startfileindex;
                filelen -= Environment.NewLine.Length; // there is a line break right before the boundary
                filelen -= "--".Length;                // this has to be there for some reason !!!!!!re-examine later

                bytesReceived += startfileindex + 1;   // include the multipart headers/junk all the way up to the begining of the file binary
                if (!isChunked)
                {
                    startfileindex += client.CurrentRequest.HeaderLength; //needed to set the offset for the httprequest RAW
                }

                // if the end of the file is not in the request
                if (filelen < 1)
                {
                    filelen = multiPartRAW.Length - startfileindex; //until the end of the buffer
                }
                else
                {
                    bytesReceived = contentlen; // make sure the loop does not go through. All the file is here.
                }

                filestream.Write(multiPartRAW, startfileindex, filelen);

                bytesReceived += filelen;
                //bytesReceived += (Environment.NewLine.Length * 2) + boundary.Length;

                byte[] boundaryBytes = Encoding.UTF8.GetBytes(boundary);
                // REMEMBER that the first character can randomly come before the actual boundary making fail

                List <int> boundaryCounters = new List <int>();

                buffer = new byte[1048576];
                bool eof = false;
                while (bytesReceived < contentlen && !eof)
                {
                    int read = inStream.Read(buffer, 0, buffer.Length);
                    for (int i = 0; i < read; i++)
                    {
                        for (int j = 0; j < boundaryCounters.Count; j++)
                        {
                            int boundaryCounter = boundaryCounters[j];
                            if (buffer[i] == boundaryBytes[boundaryCounter])
                            {
                                boundaryCounters.Remove(boundaryCounter);
                                boundaryCounter++;
                                boundaryCounters.Insert(j, boundaryCounter);
                                if (boundaryCounter >= boundaryBytes.Length)
                                {
                                    eof = true;
                                    break;
                                }
                            }
                            else
                            {
                                boundaryCounters.Remove(boundaryCounter);
                                j--;
                            }
                        }
                        if (buffer[i] == boundaryBytes[0])
                        {
                            boundaryCounters.Add(1);
                        }
                        if (eof)   //this is here to prevent the last bad bit from being added
                        {
                            break; // will cause buffer[i] == boundaryBytes[boundaryCounter] to have index out of bounds exception
                        }
                        else
                        {
                            filestream.WriteByte(buffer[i]);
                        }
                        bytesReceived++;
                    }
                    filestream.Flush();
                }
                if (eof)
                {
                    filestream.SetLength(filestream.Length - (boundary.Length + Environment.NewLine.Length + 1));
                }
                inStream.Flush();
                filestream.Close();
            }
            catch (IOException)
            {
                return(FormatInvokeFailure(", because of a file/permission error, has"));
            }

            HttpResponse r = new HttpResponse(HttpResponse.ConnectionStatus.FOUND, "keep-alive", null);

            r.addHeader("Content-Length", "0");
            r.addHeader("Location", client.CurrentRequest.requestMetaInfo("Referer"));
            return(r);
        }