public HttpResponseHeader(string source) { _isBinary = false; _source = source; StringReader sreader = new StringReader(_source); try { if (ParseResponseCode(sreader.ReadLine()) == false) { _isParse = false; return; } else { _headers = HttpHeader.ParseHeader(sreader); if (_httpVersion == HTTP_VERTION.HTTP10) { if (_headers.Get(HTTP_HEADER_KEY.CONNECTION) != null) { if (_headers[HTTP_HEADER_KEY.CONNECTION] == HTTP_HEADER_VALUE.KEEPALLIVE) { _isKeepAllive = true; } } if (_headers.Get(HTTP_HEADER_KEY.PROXY_CONNECTION) != null) { if (_headers[HTTP_HEADER_KEY.PROXY_CONNECTION] == HTTP_HEADER_VALUE.KEEPALLIVE) { _isKeepAllive = true; } } } else { _isKeepAllive = true; if (_headers.Get(HTTP_HEADER_KEY.CONNECTION) != null) { if (_headers[HTTP_HEADER_KEY.CONNECTION] == HTTP_HEADER_VALUE.CLOSE) { _isKeepAllive = false; } } if (_headers.Get(HTTP_HEADER_KEY.PROXY_CONNECTION) != null) { if (_headers[HTTP_HEADER_KEY.PROXY_CONNECTION] == HTTP_HEADER_VALUE.CLOSE) { _isKeepAllive = false; } } } if (_headers.Get(HTTP_HEADER_KEY.TRANSFER_ENCODING) != null) { if (_headers[HTTP_HEADER_KEY.TRANSFER_ENCODING] == HTTP_HEADER_VALUE.CHUNKED) { _isChunked = true; } } else if (_headers.Get(HTTP_HEADER_KEY.CONTENT_LENGTH) != null) { _contentSize = Convert.ToInt32(_headers[HTTP_HEADER_KEY.CONTENT_LENGTH]); } } }catch { _isParse = false; return; } _isBinary = HttpHeader.CheckBinary(_headers); _isParse = true; }
static public void Proxy(TcpClient serverSocket, IHttpEventListener listner) { Stream server = serverSocket.GetStream(); Stream client = null; TcpClient clinetSocket = null; bool bConnect = false; string host = string.Empty; bool bSSL = false; while (true) { //----------------------------------------- // Request. // Clinet -> Proxy //----------------------------------------- HttpRequestObject request = null; try { request = HttpStream.ReadRequest(server); if (request == null) { break; } //----------------------------------------- // SSL Handshake. //----------------------------------------- if (SslHandshake.IsSslConnection(request)) { SslStreamSet sslset = SslHandshake.Handshake(clinetSocket, request, server); server = sslset._serverStream; client = sslset._clientStream; bConnect = true; bSSL = true; continue; } //----------------------------------------- // HTTP Conection. //----------------------------------------- else if (bSSL == false && (bConnect == false || host != request._header._host)) { if (clinetSocket != null) { client.Close(); clinetSocket.Close(); } clinetSocket = new TcpClient(); clinetSocket.Connect(request._header._host, request._header._port); client = clinetSocket.GetStream(); bConnect = true; } //----------------------------------------- // save host. //----------------------------------------- host = request._header._host; //----------------------------------------- // Observer. //----------------------------------------- if (listner != null) { request = listner.OnHttpRequestClient(request, server, client); } }catch (Exception ex) { Close(serverSocket, clinetSocket, server, client); throw ex; } //----------------------------------------- // Modify Request. // Proxy -> Server //----------------------------------------- try { HttpHeader.ModifyProxyRequest(request._header); }catch (Exception ex) { Close(serverSocket, clinetSocket, server, client); throw ex; } //----------------------------------------- // Request. // Proxy -> Server //----------------------------------------- try { HttpStream.Write(request, client); //----------------------------------------- // Observer. //----------------------------------------- if (listner != null) { request = listner.OnHttpRequestServer(request, server, client); } } catch (Exception ex) { Close(serverSocket, clinetSocket, server, client); throw ex; } //----------------------------------------- // Response. // Proxy <- Server //----------------------------------------- HttpResponseObject response = null; try { response = HttpStream.ReadResponse(client); if (response == null) { break; } //----------------------------------------- // Observer. //----------------------------------------- if (listner != null) { response = listner.OnHttpResponseClient(response, server, client); } } catch (Exception ex) { Close(serverSocket, clinetSocket, server, client); throw ex; } //----------------------------------------- // Response. // Client <- Proxy //----------------------------------------- try { HttpStream.Write(response, server); //----------------------------------------- // Observer. //----------------------------------------- if (listner != null) { response = listner.OnHttpResponseServer(response, server, client); } } catch (Exception ex) { Close(serverSocket, clinetSocket, server, client); throw ex; } if (request._header._isKeepAllive == false || response._header._isKeepAllive == false) { break; } } Close(serverSocket, clinetSocket, server, client); }
public HttpRequestHeader(string source) { if (source.Length == 0) { _isData = false; return; } else { _isData = true; } _isBinary = false; _source = source; StringReader sreader = new StringReader(_source); try { if (ParseRequestMethod(sreader.ReadLine()) == false) { _isParse = false; return; } else { _headers = HttpHeader.ParseHeader(sreader); if (_httpVersion == HTTP_VERTION.HTTP10) { if (_headers.Get(HTTP_HEADER_KEY.CONNECTION) != null) { if (_headers[HTTP_HEADER_KEY.CONNECTION] == HTTP_HEADER_VALUE.KEEPALLIVE) { _isKeepAllive = true; } } if (_headers.Get(HTTP_HEADER_KEY.PROXY_CONNECTION) != null) { if (_headers[HTTP_HEADER_KEY.PROXY_CONNECTION] == HTTP_HEADER_VALUE.KEEPALLIVE) { _isKeepAllive = true; } } } else { _isKeepAllive = true; if (_headers.Get(HTTP_HEADER_KEY.CONNECTION) != null) { if (_headers[HTTP_HEADER_KEY.CONNECTION] == HTTP_HEADER_VALUE.CLOSE) { _isKeepAllive = false; } } if (_headers.Get(HTTP_HEADER_KEY.PROXY_CONNECTION) != null) { if (_headers[HTTP_HEADER_KEY.PROXY_CONNECTION] == HTTP_HEADER_VALUE.CLOSE) { _isKeepAllive = false; } } } if (_headers.Get(HTTP_HEADER_KEY.CONTENT_LENGTH) != null) { _contentSize = Convert.ToInt32(_headers[HTTP_HEADER_KEY.CONTENT_LENGTH]); } if (_headers.Get(HTTP_HEADER_KEY.HOST) != null) { _host = _headers[HTTP_HEADER_KEY.HOST]; if (_host.IndexOf(":") != -1) { string[] host = _host.Split(':'); _host = host[0]; _port = Convert.ToInt32(host[1]); } else { _host = _host; _port = Convert.ToInt32(HTTP_PORT.HTTP); } } } }catch { _isParse = false; return; } _isBinary = HttpHeader.CheckBinary(_headers); _isParse = true; }