public static HTTPResponseHeaders ParseResponse(string sResponse) { int index = sResponse.IndexOf("\r\n\r\n", StringComparison.Ordinal); if (index < 1) { index = sResponse.Length; } if (index >= 1) { string[] sHeaderLines = sResponse.Substring(0, index).Replace("\r\n", "\n").Split(new char[] { '\n' }); if (sHeaderLines.Length < 1) { return null; } HTTPResponseHeaders oHeaders = new HTTPResponseHeaders(Config.HeaderEncoding); int length = sHeaderLines[0].IndexOf(' '); if (length > 0) { oHeaders.HTTPVersion = sHeaderLines[0].Substring(0, length).ToUpper(); sHeaderLines[0] = sHeaderLines[0].Substring(length + 1).Trim(); if (!oHeaders.HTTPVersion.StartsWith("HTTP/", StringComparison.OrdinalIgnoreCase)) { return null; } oHeaders.HTTPResponseStatus = sHeaderLines[0]; bool flag = false; length = sHeaderLines[0].IndexOf(' '); if (length > 0) { flag = int.TryParse(sHeaderLines[0].Substring(0, length).Trim(), NumberStyles.Integer, NumberFormatInfo.InvariantInfo, out oHeaders.HTTPResponseCode); } else { flag = int.TryParse(sHeaderLines[0].Trim(), NumberStyles.Integer, NumberFormatInfo.InvariantInfo, out oHeaders.HTTPResponseCode); } if (!flag) { return null; } string sErrors = string.Empty; ParseNVPHeaders(oHeaders, sHeaderLines, 1, ref sErrors); return oHeaders; } } return null; }
private bool ParseResponseForHeaders() { if ((this.m_responseData != null) && (this.iEntityBodyOffset >= 4)) { this._inHeaders = new HTTPResponseHeaders(Config.HeaderEncoding); byte[] bytes = this.m_responseData.GetBuffer(); string str = Config.HeaderEncoding.GetString(bytes, 0, this.iEntityBodyOffset).Trim(); if ((str == null) || (str.Length < 1)) { this._inHeaders = null; return false; } string[] sHeaderLines = str.Replace("\r\n", "\n").Split(new char[] { '\n' }); if (sHeaderLines.Length >= 1) { int index = sHeaderLines[0].IndexOf(' '); if (index > 0) { this._inHeaders.HTTPVersion = sHeaderLines[0].Substring(0, index).ToUpper(); sHeaderLines[0] = sHeaderLines[0].Substring(index + 1).Trim(); if (!this._inHeaders.HTTPVersion.StartsWith("HTTP/", StringComparison.OrdinalIgnoreCase)) { return false; } this._inHeaders.HTTPResponseStatus = sHeaderLines[0]; bool flag = false; index = sHeaderLines[0].IndexOf(' '); if (index > 0) { flag = int.TryParse(sHeaderLines[0].Substring(0, index).Trim(), NumberStyles.Integer, NumberFormatInfo.InvariantInfo, out this._inHeaders.HTTPResponseCode); } else { flag = int.TryParse(sHeaderLines[0].Trim(), NumberStyles.Integer, NumberFormatInfo.InvariantInfo, out this._inHeaders.HTTPResponseCode); } if (!flag) { return false; } string sErrors = string.Empty; if (!Parser.ParseNVPHeaders(this._inHeaders, sHeaderLines, 1, ref sErrors)) { } return true; } } } return false; }
private void deleteInformationalMessage() { this._inHeaders = null; byte[] buffer = new byte[this.m_responseData.Length - this.iEntityBodyOffset]; this.m_responseData.Position = this.iEntityBodyOffset; this.m_responseData.Read(buffer, 0, buffer.Length); this.m_responseData.Dispose(); this.m_responseData = new MemoryStream(buffer.Length); this.m_responseData.Write(buffer, 0, buffer.Length); this.m_responseTotalDataCount = 0L; this.iEntityBodyOffset = this._iBodySeekProgress = 0; }
internal ServerChatter(Session oSession, string sHeaders) { this._lngLastChunkInfoOffset = -1L; this.m_session = oSession; this._inHeaders = Parser.ParseResponse(sHeaders); }
internal bool ReadResponse() { int iMaxByteCount = 0; bool flag = false; bool flag2 = false; bool flag3 = false; byte[] arrBuffer = new byte[_cbServerReadBuffer]; do { try { iMaxByteCount = this.ServerPipe.Receive(arrBuffer); if (0L == this.m_session.Timers.ServerBeginResponse.Ticks) { this.m_session.Timers.ServerBeginResponse = DateTime.Now; } if (iMaxByteCount <= 0) { flag = true; } else { this.m_responseData.Write(arrBuffer, 0, iMaxByteCount); this.m_responseTotalDataCount += iMaxByteCount; if ((this._inHeaders == null) && this.GetHeaders()) { if ((this.m_session.State == SessionStates.Aborted) && this.m_session.isAnyFlagSet(SessionFlags.ProtocolViolationInResponse)) { return false; } } } } catch (Exception exception) { flag2 = true; if (exception is OperationCanceledException) { this.m_session.State = SessionStates.Aborted; } else if (exception is OutOfMemoryException) { this.m_session.State = SessionStates.Aborted; } else { } } } while ((!flag && !flag2) && ((this._inHeaders == null) || !this.isResponseBodyComplete())); // isResponseBodyComplete ��һ���ܹؼ��ķ���������ϸ�о������ this.m_session.Timers.ServerDoneResponse = DateTime.Now; if ((this.m_responseTotalDataCount == 0L) && (this._inHeaders == null)) { flag2 = true; } arrBuffer = null; if (flag2) { this.m_responseData.Dispose(); this.m_responseData = null; return false; } if (this._inHeaders == null) { this.m_session.SetBitFlag(SessionFlags.ResponseStreamed, false); this._inHeaders = new HTTPResponseHeaders(Config.HeaderEncoding); this._inHeaders.HTTPVersion = "HTTP/1.0"; this._inHeaders.HTTPResponseCode = 200; this._inHeaders.HTTPResponseStatus = "200 This buggy server did not return headers"; this.iEntityBodyOffset = 0; return true; } return true; }