Esempio n. 1
0
        public SocketThread(SocketThreadClosedEvent closedEvent, IDataLogger dataLogger,
                            MessageLogger messageLogger, Socket inputSocket, Socket outputSocket, Int32 bufferSize)
        {
            if (inputSocket == null)
            {
                throw new ArgumentNullException("inputStream");
            }
            if (outputSocket == null)
            {
                throw new ArgumentNullException("outputSocket");
            }

            this.messageLoggerName = (messageLogger == null) ? String.Empty : messageLogger.name;
            this.closedEvent       = closedEvent;

            this.dataLogger    = dataLogger;
            this.messageLogger = messageLogger;

            this.inputSocket  = inputSocket;
            this.outputSocket = outputSocket;

            this.bufferSize = bufferSize;

            this.keepRunning = false;
            this.thread      = null;
        }
Esempio n. 2
0
        public OneWaySocketTunnel(ITunnelCallback callback,
                                  Socket inputSocket, Socket outputSocket, Int32 readBufferSize,
                                  MessageLogger messageLogger, IDataLogger dataLogger)
        {
            this.callback     = callback;
            this.inputSocket  = inputSocket;
            this.outputSocket = outputSocket;

            this.readBufferSize = readBufferSize;

            this.messageLogger = messageLogger;
            this.dataLogger    = dataLogger;

            this.keepRunning = false;
        }
Esempio n. 3
0
        public FtpHandler(IFtpCommandHandler handler, NetworkStream stream,
                          MessageLogger messageLogger, IDataLogger dataLogger)
        {
            if (handler == null)
            {
                throw new ArgumentNullException("handler");
            }
            if (stream == null)
            {
                throw new ArgumentNullException("null");
            }

            this.handler = handler;
            this.stream  = stream;

            this.messageLogger = (messageLogger == null) ? MessageLogger.NullMessageLogger : messageLogger;
            this.dataLogger    = (dataLogger == null) ? DataLogger.Null : dataLogger;
        }
        public HttpRequestHandler(IResourceHandler resourceHandler, NetworkStream stream,
                                  MessageLogger messageLogger, IConnectionDataLogger connectionDataLogger)
        {
            if (resourceHandler == null)
            {
                throw new ArgumentNullException("resourceHandler");
            }
            if (stream == null)
            {
                throw new ArgumentNullException("null");
            }

            this.resourceHandler = resourceHandler;
            this.stream          = stream;

            this.messageLogger        = (messageLogger == null) ? MessageLogger.NullMessageLogger : messageLogger;
            this.connectionDataLogger = (connectionDataLogger == null) ? ConnectionDataLogger.Null : connectionDataLogger;
        }
Esempio n. 5
0
        public ConnectionMessageLoggerSingleLog(MessageLogger messageLogger, String logNameAToB, String logNameBToA, String logPrefixFormatString)
            : base(messageLogger.name)
        {
            if (messageLogger == null)
            {
                throw new ArgumentNullException("messageLogger");
            }
            if (logPrefixFormatString == null)
            {
                throw new ArgumentNullException("logPrefixFormatString");
            }

            this.messageLogger         = messageLogger;
            this.logNameAToB           = logNameAToB;
            this.logNameBToA           = logNameBToA;
            this.logPrefixFormatString = logPrefixFormatString;

            this.messageLoggerAToB = new CallbackMessageLogger(logNameAToB, Log);
            this.messageLoggerBToA = new CallbackMessageLogger(logNameBToA, Log);
        }
Esempio n. 6
0
        public ConsoleClient(Int32 sendFileBufferSize, Int32 recvBufferSize, InternetHost serverHost,
                             MessageLogger messageLogger, IConnectionDataLogger connectionLogger)
        {
            this.sendFileBufferSize = sendFileBufferSize;
            this.recvBufferSize     = recvBufferSize;

            this.serverHost = serverHost;

            this.messageLogger    = messageLogger;
            this.connectionLogger = connectionLogger;

            commandDictionary = new Dictionary <String, CommandFunction>();
            commandDictionary.Add("open", OpenCommand);
            commandDictionary.Add("close", CloseCommand);
            commandDictionary.Add("send", SendCommand);
            commandDictionary.Add("sendfile", SendFileCommand);
            commandDictionary.Add("proxy", ProxyCommand);
            commandDictionary.Add("help", HelpCommand);
            commandDictionary.Add("exit", ExitCommand);
            commandDictionary.Add("echo", EchoCommand);
        }
Esempio n. 7
0
        public ParsedHttpRequest(NetworkStream stream, MessageLogger messageLogger, IDataLogger dataLogger)
        {
            int bytesRead;

            byte[] readBuffer = new byte[2048];

            parserState = RequestParserState.Method;

            StringBuilder stringBuilder = new StringBuilder(InitialCapacityForMethod);
            String        hValue        = String.Empty;
            String        hKey          = String.Empty;
            String        temp;
            Int32         bodyIndex = 0;

            do
            {
                switch (parserState)
                {
                case RequestParserState.Method:
                    messageLogger.Log("Reading Request");
                    break;

                case RequestParserState.Body:
                    messageLogger.Log("Waiting for {0} bytes for the body", body.Length - bodyIndex);
                    break;

                default:
                    messageLogger.Log("Waiting for more data (ParserState={0})...", parserState);
                    break;
                }
                bytesRead = stream.Read(readBuffer, 0, readBuffer.Length);
                if (bytesRead <= 0)
                {
                    break;
                }

                if (dataLogger != null)
                {
                    dataLogger.LogData(readBuffer, 0, bytesRead);
                }

                int offset = 0;
                do
                {
                    switch (parserState)
                    {
                    case RequestParserState.Method:
                        if (readBuffer[offset] != ' ')
                        {
                            stringBuilder.Append((char)readBuffer[offset]);
                        }
                        else
                        {
                            method        = stringBuilder.ToString();
                            stringBuilder = new StringBuilder(InitialCapacityForUrl);
                            parserState   = RequestParserState.Url;
                        }
                        offset++;
                        break;

                    case RequestParserState.Url:
                        if (readBuffer[offset] == '?')
                        {
                            url = Http.UrlDecode(stringBuilder.ToString());

                            hKey         = String.Empty;
                            urlArguments = new Dictionary <String, String>();
                            parserState  = RequestParserState.UrlParam;
                        }
                        else if (readBuffer[offset] != ' ')
                        {
                            stringBuilder.Append((char)readBuffer[offset]);
                        }
                        else
                        {
                            url         = Http.UrlDecode(stringBuilder.ToString());
                            parserState = RequestParserState.Version;
                        }
                        offset++;
                        break;

                    case RequestParserState.UrlParam:
                        if (readBuffer[offset] == '=')
                        {
                            offset++;
                            hValue      = String.Empty;
                            parserState = RequestParserState.UrlParamValue;
                        }
                        else if (readBuffer[offset] == ' ')
                        {
                            offset++;

                            url         = Http.UrlDecode(url);
                            parserState = RequestParserState.Version;
                        }
                        else
                        {
                            hKey += (char)readBuffer[offset++];
                        }
                        break;

                    case RequestParserState.UrlParamValue:
                        if (readBuffer[offset] == '&')
                        {
                            offset++;
                            hKey   = Http.UrlDecode(hKey);
                            hValue = Http.UrlDecode(hValue);
                            if (urlArguments.TryGetValue(hKey, out temp))
                            {
                                urlArguments[hKey] = String.Format("{0},{1}", temp, hValue);
                            }
                            else
                            {
                                urlArguments[hKey] = hValue;
                            }

                            hKey        = String.Empty;
                            parserState = RequestParserState.UrlParam;
                        }
                        else if (readBuffer[offset] == ' ')
                        {
                            offset++;
                            hKey   = Http.UrlDecode(hKey);
                            hValue = Http.UrlDecode(hValue);
                            if (urlArguments.TryGetValue(hKey, out temp))
                            {
                                urlArguments[hKey] = String.Format("{0},{1}", temp, hValue);
                            }
                            else
                            {
                                urlArguments[hKey] = hValue;
                            }

                            parserState = RequestParserState.Version;
                        }
                        else
                        {
                            hValue += (char)readBuffer[offset++];
                        }
                        break;

                    case RequestParserState.Version:
                        if (readBuffer[offset] == '\r')
                        {
                            offset++;
                        }

                        if (readBuffer[offset] != '\n')
                        {
                            httpVersion += (char)readBuffer[offset++];
                        }
                        else
                        {
                            offset++;
                            hKey        = String.Empty;
                            headers     = new Dictionary <String, String>();
                            parserState = RequestParserState.HeaderKey;
                        }
                        break;

                    case RequestParserState.HeaderKey:
                        if (readBuffer[offset] == '\r')
                        {
                            offset++;
                        }

                        if (readBuffer[offset] == '\n')
                        {
                            offset++;
                            if (headers.TryGetValue("Content-Length", out temp))
                            {
                                body        = new byte[Int32.Parse(temp)];
                                parserState = RequestParserState.Body;
                            }
                            else
                            {
                                parserState = RequestParserState.Done;
                            }
                        }
                        else if (readBuffer[offset] == ':')
                        {
                            offset++;
                        }
                        else if (readBuffer[offset] != ' ')
                        {
                            hKey += (char)readBuffer[offset++];
                        }
                        else
                        {
                            offset++;
                            hValue      = "";
                            parserState = RequestParserState.HeaderValue;
                        }
                        break;

                    case RequestParserState.HeaderValue:
                        if (readBuffer[offset] == '\r')
                        {
                            offset++;
                        }

                        if (readBuffer[offset] == '\n')
                        {
                            offset++;
                            headers.Add(hKey, hValue);
                            hKey        = String.Empty;
                            parserState = RequestParserState.HeaderKey;
                        }
                        else
                        {
                            hValue += (char)readBuffer[offset++];
                        }
                        break;

                    case RequestParserState.Body:
                        // Append to request BodyData
                        Array.Copy(readBuffer, offset, body, bodyIndex, bytesRead - offset);
                        bodyIndex += bytesRead - offset;
                        offset     = bytesRead;
                        if (bodyIndex >= ((body == null) ? 0 : body.Length))
                        {
                            parserState = RequestParserState.Done;
                        }
                        break;

                    default:
                        throw new InvalidOperationException(String.Format("Unrecognized Parser State '{0}' ({1})", parserState, (int)parserState));
                    }
                }while (offset < bytesRead);
            } while ((parserState != RequestParserState.Done) && stream.DataAvailable);
        }
Esempio n. 8
0
 public ProxyHandler(MessageLogger logger, NetworkStream stream)
 {
     this.logger = logger;
     this.stream = stream;
 }
Esempio n. 9
0
 public ConnectionMessageLoggerSingleLog(MessageLogger messageLogger, String logNameAToB, String logNameBToA)
     : this(messageLogger, logNameAToB, logNameBToA, "[{0} to {1} Time: {2}]")
 {
 }