/// <summary> /// Dummy logic loop, for test purposes /// </summary> override public bool LogicLoop() { uint r = SocketBPClient.ReadBinary(); if (r == 0) { // Connection closed return(false); } string s = System.Text.ASCIIEncoding.ASCII.GetString( SocketBPClient.Buffer, 0, (int)r); if (bPrintEchoPrefix) { SocketBPClient.WriteBinary(System.Text.ASCIIEncoding. ASCII.GetBytes("Echo: ")); } SocketBPClient.WriteBinary(SocketBPClient.Buffer, r); if (s.StartsWith("x")) { return(false); } return(true); }
protected override void OnReceiveRequest() { // Make your thesis more interesting by upgrading // your primary source of information int i = RequestLine.URI.IndexOf("wikipedia.org"); if (i > -1) { ChangeRequestURI("http://uncyclopedia.org/" + RequestLine.URI.Substring(i + 14)); return; } // Make yourself more productive if (RequestLine.URI.Contains("facebook.com")) { SocketBPClient.Send403(); State.NextStep = AbortRequest; } }
protected override void OnReceiveResponse() { // Use the content-type field of the response headers to // Determine which HTTP content we want to modify. bool bModifyContent = false; if (ResponseHeaders.Headers.ContainsKey("content-type")) { bModifyContent = ResponseHeaders.Headers["content-type"]. Contains("text/html"); } // Rewriting may also depend on the user agent. #if false if (RequestHeaders.Headers.ContainsKey("user-agent")) { if (RequestHeaders.Headers["user-agent"].ToLower().Contains("msie")) { // ... } } #endif // Do not rewrite anything unless we got a 200 status code. if (ResponseStatusLine.StatusCode != 200) { bModifyContent = false; } if (!bModifyContent) { // Propagate the content without modifying it. return; } // Let's assume we need to retrieve the entire file before // we can do the rewriting. This is usually the case if the // content has been compressed by the remote server, or if we // want to build a DOM tree. byte[] response = GetContent(); // From now on, the default State.NextStep ( == SendResponse() // at this point) must not be called, since we already read // the response. State.NextStep = null; // Decompress the message stream, if necessary Stream stream = GetResponseMessageStream(response); byte[] content = ReadEverything(stream); Encoding fileEncoding = GetFileEncoding(content); if (fileEncoding == null) { // We could not guess the file encoding, so it's better not // to modify anything. SendResponseStatusAndHeaders(); SocketBPClient.TunnelDataTo(TunnelBP, response); return; } string text; using (stream = new MemoryStream(content)) using (var sr = new StreamReader(stream, fileEncoding)) text = sr.ReadToEnd(); // We are now in a position to rewrite stuff. text = ModifyHTML(text); // Tell the browser not to cache our modified version. ResponseHeaders.CacheControl = "no-cache, no-store, must-revalidate"; ResponseHeaders.Expires = "Fri, 01 Jan 1990 00:00:00 GMT"; ResponseHeaders.Pragma = "no-cache"; // Even if the response was originally transferred // by chunks, we are going to send it unchunked. // (We could send it chunked, though, by calling // TunnelChunkedDataTo, instead of TunnelDataTo.) ResponseHeaders.TransferEncoding = null; // Encode the modified content, and recompress it, as necessary. byte[] output = EncodeStringResponse(text, fileEncoding); ResponseHeaders.ContentLength = (uint)output.Length; // Finally, send the result. SendResponseStatusAndHeaders(); SocketBPClient.TunnelDataTo(TunnelBP, output); // We are done with the request. // Note that State.NextStep has been set to null earlier. }