コード例 #1
0
ファイル: HttpClient.cs プロジェクト: Shine6Z/GenXSource
        void OnReceiveData(IAsyncResult ar)
        {
            if (processingThread == null)
            {
                processingThread = System.Threading.Thread.CurrentThread;
            }
            lock (socket)
            {
                try
                {
                    idleTime = 0;
                    if (!isConnected)
                    {
                        return;
                    }

                    int dataLen = stream.EndRead(ar);

#if DUMP
                    System.Diagnostics.Debug.Write(Encoding.Default.GetString(buffer, 0, dataLen));
                    System.Diagnostics.Debug.WriteLine("+++++++++++++++++++++++");
#endif

                    if (dataLen < 1)
                    {
                        Disconnect();
                        return;
                    }

                    if (textBuf != null)
                    {
                        if (textBuf.Length > 0x8000)
                        {
                            Disconnect();
                            return;
                        }
                    }

                    for (int bufPos = 0; bufPos < dataLen; bufPos++)
                    {
                        if (request == null)
                        {
                            request = new HttpRequest(this);
                            textBuf = new StringBuilder(buffer.Length);
                        }

                        if (request.Mode == HttpRequest.DataMode.Text)
                        {
                            int len = FindByte(buffer, bufPos, dataLen, 10);
                            if (len != -1)
                            {
                                textBuf.Append(Encoding.Default.GetString(buffer, bufPos, len + 1 - bufPos));
                                request.ProcessLine(textBuf.ToString().Trim('\r', '\n'));
                                textBuf.Length = 0;
                                bufPos         = len;
                            }
                            else
                            {
                                textBuf.Append(Encoding.Default.GetString(buffer, bufPos, dataLen - bufPos));
                                bufPos = dataLen;
                            }
                        }

                        if (request.Mode == HttpRequest.DataMode.Binary)
                        {
                            request.ProcessData(buffer, bufPos, dataLen - bufPos);
                            bufPos = dataLen;
                        }

                        if (request.IsRequestFinished)
                        {
                            preventTimeout = true;
                            bool forceDisconnect = false;
                            try
                            {
                                server.OnRequestReceived(this, request);
                            }
                            catch (HttpRequestException e)
                            {
                                request.Response.ResponseCode = e.ResponseCode;
                                //TODO: custom error messages, fix forceDisconnect prior to implementing HTTPS
                                if (e.ResponseCode == "500")
                                {
                                    server.Log.WriteLine(e.ToString());
                                }
                                forceDisconnect = true;
                            }
                            request.SendResponse();
                            ConnectionMode modeTemp = request.ConnectionMode;

                            string requestVer = request.HttpVersion;

                            request.Dispose();
                            request        = null;
                            idleTime       = 0;
                            preventTimeout = false;

                            //TODO: don't disconnect 1.0... wget is being sending weird headers, do this workaround for now
                            if (modeTemp == ConnectionMode.Close || forceDisconnect || requestVer == "1.0")
                            {
                                Disconnect();
                                return;
                            }
                        }
                    }

                    if (isConnected)
                    {
                        stream.BeginRead(buffer, 0, buffer.Length, new AsyncCallback(OnReceiveData), this);
                    }
                }
                catch (SocketException)
                {
                    Disconnect();
                }
                catch (IOException e)
                {
                    Disconnect();
                    if (!(e.InnerException is SocketException))
                    {
#if !DEBUG
                        server.Log.WriteLine("Error: " + e.ToString());
#else
                        throw;
#endif
                    }
                }
                catch (System.Threading.ThreadAbortException)
                {
                    Disconnect();
                }
#if !DEBUG
                catch (Exception e)
                {
                    server.Log.WriteLine("Error: " + e.ToString());
                    Disconnect();
                }
#endif
            }
        }