Ejemplo n.º 1
0
Archivo: Http.cs Proyecto: Skye347/KLib
        public override void Timeout(UniNetObject connection, object Addition)
        {
            HTTPStateObject state = Addition as HTTPStateObject;

            log("id:" + state?.request.id, ERROR, "HTTP.Received");
            log("timeout:" + connection.CompleteTime, ERROR, "HTTP.Received");
            log("connection info:" + state?.Length, ERROR, "HTTP.Received");
        }
Ejemplo n.º 2
0
Archivo: Http.cs Proyecto: Skye347/KLib
 private static HTTPRequest ProcessResponse(HTTPResponse response, HTTPStateObject state)
 {
     state.request.Done = true;
     response.request   = state.request;
     if (response.bodyBytesLength == 0)
     {
         response.bodyBytesLength = Encoding.UTF8.GetByteCount(response.body);
     }
     return(state.request.Callback(response));
 }
Ejemplo n.º 3
0
Archivo: Http.cs Proyecto: Skye347/KLib
        public override void Aborted(UniNetObject connection, object Addition)
        {
            HTTPStateObject state = Addition as HTTPStateObject;

            if (state == null)
            {
                return;
            }
            if (state.request.Done == false)
            {
                HTTPOp.Request(state.request);
            }
        }
Ejemplo n.º 4
0
Archivo: Http.cs Proyecto: Skye347/KLib
        public static HTTPError Request(HTTPMethod method, string Url, object Addition, HTTPRequest.RequestCallback callback, HTTPCookie cookie = null, HTTPHeader additionHeader = null, string PostData = null)
        {
            HTTPStateObject state   = new HTTPStateObject();
            HTTPRequest     request = new HTTPRequest()
            {
                method = method
            };
            Uri path = new Uri(Url);

            request.Cookie   = cookie?.GetCookie(path.Host);
            request.Header   = additionHeader;
            request.Callback = callback;
            request.Addition = Addition;
            request.SetUrl(path.Scheme, path.PathAndQuery, path.Host);
            request.PostData = PostData;
            return(Request(request));
        }
Ejemplo n.º 5
0
Archivo: Http.cs Proyecto: Skye347/KLib
        override public object Connected(UniNetObject connection, out NetCore.Error.NetCoreException err)
        {
            err = null;
            HTTPRequest request = null;

            if (ConnectedRequest != null)
            {
                request = ConnectedRequest;
                connection.Write(ConnectedRequest.ToByte(), out err);
                ConnectedRequest = null;
            }
            lock (newConnectLock)
            {
                Monitor.Pulse(newConnectLock);
            }
            var state = new HTTPStateObject()
            {
                request = request
            };

            return(state);
        }
Ejemplo n.º 6
0
Archivo: Http.cs Proyecto: Skye347/KLib
        public object ProcessHTTPResponse(byte[] data, HTTPStateObject state, out HTTPRequest request)
        {
            //string dataAsString = Encoding.UTF8.GetString(data);
            if (state == null)
            {
                throw new InvalidOperationException();
            }
            try
            {
                //var tmpString = Encoding.UTF8.GetString(data);
                if (state.complete == HTTPStateComplete.Init)
                {//process normally
                    HTTPResponse response;
                    if (state.waitedResponse == null)
                    {
                        response = new HTTPResponse();
                    }
                    else
                    {
                        response = state.waitedResponse;
                    }
                    response.Parse(data);
                    if (response.headerDataEnds == -1)
                    {
                        state.waitedResponse = response;
                        state.complete       = HTTPStateComplete.incompletedHeader;
                        request = null;
                        return(state);
                    }
                    else if (response.header.ContainsKey("Transfer-Encoding") && response.header["Transfer-Encoding"].Contains("chunked"))
                    {
                        state.waitedResponse = response;
                        state.complete       = HTTPStateComplete.isChunking;
                        request = null;
                        return(state);
                    }
                    else if (response.header.ContainsKey("Content-Length"))
                    {
                        state.Length = int.Parse(response.header["Content-Length"]) - response.bodyBytesLength;
                        if (state.Length == 0)
                        {
                            request = ProcessResponse(response, state);
                            if (request == null)
                            {
                                state.complete = HTTPStateComplete.Closing;
                            }
                            return(state);
                        }
                        state.complete       = HTTPStateComplete.isReadingByLength;
                        state.waitedResponse = response;
                        request = null;
                        return(state);
                    }
                    else
                    {
                        request = ProcessResponse(response, state);
                        if (request == null)
                        {
                            state.complete = HTTPStateComplete.Closing;
                        }
                        return(state);
                    }
                }
                else if (state.complete == HTTPStateComplete.isChunking)
                {//process chunked
                    //state.waitedResponse.body += Encoding.UTF8.GetString(data);
                    state.waitedResponse.AddBody(data);
                    int index = data.Length - 1;
                    if (data[index] == 10 && data[index - 1] == 13 && data[index - 2] == 10 && data[index - 3] == 13 && data[index - 4] == 48)
                    {//"0\r\n\r\n"
                        state.waitedResponse.body = state.waitedResponse.body.Substring(6, state.waitedResponse.body.Length - 12);
                        request = ProcessResponse(state.waitedResponse, state);
                        state.waitedResponse = null;
                        state.complete       = HTTPStateComplete.Init;
                        if (request == null)
                        {
                            state.complete = HTTPStateComplete.Closing;
                        }
                        return(state);
                    }
                    else
                    {
                        request = null;
                        return(state);
                    }
                }
                else if (state.complete == HTTPStateComplete.isReadingByLength)
                {//
                    state.Length -= data.Length;
                    state.waitedResponse.AddBody(data);
                    //var waitedData = Encoding.UTF8.GetString(data);
                    //state.Length -= Encoding.UTF8.GetByteCount(waitedData);
                    //state.waitedResponse.body += waitedData;
                    //state.waitedResponse.bodyBytesLength += Encoding.UTF8.GetByteCount(waitedData);
                    if (state.Length <= 0)
                    {
                        request = ProcessResponse(state.waitedResponse, state);
                        state.waitedResponse = null;
                        state.complete       = HTTPStateComplete.Init;
                        if (request == null)
                        {
                            state.complete = HTTPStateComplete.Closing;
                        }
                        return(state);
                    }
                    else
                    {
                        request = null;
                        return(state);
                    }
                }
                else if (state.complete == HTTPStateComplete.incompletedHeader)//incompleted header
                {
                    state.waitedResponse.AddBody(data);
                    state.waitedResponse.Parse(state.waitedResponse.binaryData);
                    if (state.waitedResponse.headerDataEnds == -1)
                    {
                        request = null;
                        return(state);
                    }
                    else
                    {
                        state.complete = HTTPStateComplete.Init;
                        return(ProcessHTTPResponse(state.waitedResponse.binaryData, state, out request));
                    }
                }
                else
                {
                    request = ProcessResponse(state.waitedResponse, state);
                    if (request == null)
                    {
                        state.complete = HTTPStateComplete.Closing;
                    }
                    return(state);
                }
                //if (state.isChunking || state.isReadingByLength)
                //{
                //    return true;
                //}
                //else
                //{
                //    return false;
                //}
            }
            catch (IndexOutOfRangeException e)
            {
                log(e.Message, ERROR, "ProcessHTTPResponse");
                log(data.Length.ToString(), ERROR, "ProcessHTTPResponse");
                log(Encoding.UTF8.GetString(data), ERROR, "ProcessHTTPResponse");
                state.waitedResponse.AddBody(data);
                request = null;
                return(state);

                //log(state.waitedResponse.body, ERROR, "ProcessHTTPResponse");
                throw e;
                //request = null;
                //return state;
            }
        }