Beispiel #1
0
        //public void Execute(Request request,
        //    Tcp.Params.Buffer receiveBufferSettings, Tcp.Params.Buffer sendBufferSettings)
        //{
        //    Execute(request, receiveBufferSettings, sendBufferSettings);
        //}

        public void Execute(Request request, 
            Tcp.Params.Buffer receiveBufferSettings, Tcp.Params.Buffer sendBufferSettings,
            HttpConnection.ConnectionDelegate onConnectCallback,
            HttpConnection.ConnectionDelegate onDisconnectCallback,
            HttpConnection.ErrorDelegate onErrorCallback,
            HttpConnection.ProgressDelegate onProgressCallback,
            HttpConnection.ConnectionDelegate onTimeoutCallback,
            HttpConnection.CompletionDelegate onCompleteCallback)
        {
            HttpConnection conn;

            conn = new HttpConnection(request.RequestLine.RequestUri, receiveBufferSettings, sendBufferSettings);

            if (onDisconnectCallback != null) conn.OnDisconnect += onDisconnectCallback;
            if (onErrorCallback != null) conn.OnError += onErrorCallback;
            if (onProgressCallback != null) conn.OnProgress += onProgressCallback;
            if (onTimeoutCallback != null) conn.OnTimeout += onTimeoutCallback;
            if (onCompleteCallback != null) conn.OnComplete += onCompleteCallback;

            conn.OnConnect += delegate(HttpConnection sender)
            {
                if (onConnectCallback != null) onConnectCallback(sender);
                conn.SendRequestAsync(request);
            };

            conn.ConnectAsync();

        }
        protected static Requests.RequestBase Parse(HttpRequest dotNetRequest)
        {
            Requests.RequestBase apiRequest;
            Http.Request request;
            byte[] buffer = new byte[8192];
            int bytesRead = 0;
            int bytesToRead = 0;
            int byteValue = 0;
            int jsonLength = 0;
            int remainingJsonLength = 0;
            string temp = "";

            // Instantiate our Http.Request
            request = new Http.Request(dotNetRequest);

            // read byte by byte until we hit null
            // We are building our json length
            while ((byteValue = request.Body.ReceiveStream.ReadByte()) > 0)
            {
                temp += byteValue.ToString();
            }

            jsonLength = int.Parse(temp);
            remainingJsonLength = jsonLength;
            temp = "";

            if (jsonLength > 8192)
                bytesToRead = 8192;
            else
                bytesToRead = jsonLength;

            // We now need to read through the body to the end of the Json
            while ((bytesRead = request.Body.ReceiveStream.Read(buffer, 0, bytesToRead)) > 0)
            {
                temp += System.Text.Encoding.UTF8.GetString(buffer, 0, bytesRead);

                remainingJsonLength -= bytesRead;

                if (remainingJsonLength <= 0)
                    break;
                else if (remainingJsonLength > 8192)
                    bytesToRead = 8192;
                else
                    bytesToRead = remainingJsonLength;
            }

            // all that would remain in the stream would be data
            apiRequest = new Requests.RequestBase(JObject.Parse(temp))
            {
                Stream = request.Body.ReceiveStream
            };

            return apiRequest;
        }
Beispiel #3
0
 public Response(Request request)
     : base()
 {
     Request = request;
     StatusLine = new StatusLine();
 }
        protected static Requests.RequestBase Parse(HttpRequest dotNetRequest)
        {
            Requests.RequestBase apiRequest;
            Http.Request         request;
            byte[] buffer              = new byte[8192];
            int    bytesRead           = 0;
            int    bytesToRead         = 0;
            int    byteValue           = 0;
            int    jsonLength          = 0;
            int    remainingJsonLength = 0;
            string temp = "";

            // Instantiate our Http.Request
            request = new Http.Request(dotNetRequest);

            // read byte by byte until we hit null
            // We are building our json length
            while ((byteValue = request.Body.ReceiveStream.ReadByte()) > 0)
            {
                temp += byteValue.ToString();
            }

            jsonLength          = int.Parse(temp);
            remainingJsonLength = jsonLength;
            temp = "";

            if (jsonLength > 8192)
            {
                bytesToRead = 8192;
            }
            else
            {
                bytesToRead = jsonLength;
            }

            // We now need to read through the body to the end of the Json
            while ((bytesRead = request.Body.ReceiveStream.Read(buffer, 0, bytesToRead)) > 0)
            {
                temp += System.Text.Encoding.UTF8.GetString(buffer, 0, bytesRead);

                remainingJsonLength -= bytesRead;

                if (remainingJsonLength <= 0)
                {
                    break;
                }
                else if (remainingJsonLength > 8192)
                {
                    bytesToRead = 8192;
                }
                else
                {
                    bytesToRead = remainingJsonLength;
                }
            }

            // all that would remain in the stream would be data
            apiRequest = new Requests.RequestBase(JObject.Parse(temp))
            {
                Stream = request.Body.ReceiveStream
            };

            return(apiRequest);
        }
 public Base(Uri uri, Http.Request httpRequest)
 {
     _httpRequest = httpRequest;
     Uri          = uri;
 }
Beispiel #6
0
 public Base(Uri uri, Http.Request httpRequest)
 {
     _httpRequest = httpRequest;
     Uri = uri;
 }
        public override void Parse()
        {
            // A status line will always be first, its possible another status line will follow 
            // (consider 100 continue then a 404)

            int index;
            string newpacket;


            newpacket = BytesToString(_remainingBuffer, 0, _remainingBufferAppendPosition);

            if ((index = newpacket.IndexOf("\r\n\r\n")) < 0)
            {
                // Ending sequence is not found, we need to append to our string and wait for
                // Parse to get called again.
                _firstLineAndHeaders += newpacket;

                // Clear _remainingBuffer since it was all used
                _remainingBuffer = null;
                _remainingBufferAppendPosition = 0;


                if (!string.IsNullOrEmpty(_firstLineAndHeaders) &&
                    (newpacket.StartsWith("GET") || newpacket.StartsWith("DELETE") ||
                    newpacket.StartsWith("HEAD") || newpacket.StartsWith("POST") ||
                    newpacket.StartsWith("PUT")))
                {
                    string temp = newpacket.Substring(0, newpacket.IndexOf("\r\n")).Trim();
                    RequestLine rl = RequestLine.Parse(temp);
                    Request = new Request(rl.Method, rl.RequestUri);
                }
            }
            else
            {
                string[] parts;
                List<string> lines;

                // index + 4 = how many bytes to skip in _remainingBuffer then tie the stream to the
                // Response.Body

                // Possible index placements (index < 0 is handled and index >= newpacket.length-4 is impossible
                // 1) index == 0
                // 2) index between 0 and newpacket.length-4

                // Append the headers from newpacket to the other status and headers
                _firstLineAndHeaders += newpacket.Substring(0, index);
                // Reduce the buffer by the removed bytes
                _remainingBuffer = TrimStartBuffer(_remainingBuffer, index+4);
                // Reduce the append position by the number of removed bytes
                _remainingBufferAppendPosition -= index+4;

                lines = GetLines(_firstLineAndHeaders);

                if (Request == null || Request.RequestLine == null)
                {
                    // We have no request line yet, need to parse one
                    RequestLine rl = RequestLine.Parse(lines[0]);
                    Request = new Request(rl.Method, rl.RequestUri);
                }
                
                Request.Headers.Clear();
                for (int i = 1; i < lines.Count; i++)
                {
                    parts = new string[2];
                    parts[0] = lines[i].Substring(0, lines[i].IndexOf(':')).Trim();
                    parts[1] = lines[i].Substring(lines[i].IndexOf(':') + 1).Trim();

                    Response.Headers.Add(new Message.Token(parts[0]), parts[1]);
                }

                AllHeadersReceived = true;
            }
        }