Create web response from HTML file ?

I try to create a firewall that have packet redirect for unwanted web request. The problem that I have next is how to make our HTML file as the web response for the client request. The scene like this :
1. Client request for something on web. [DONE]
2. My firewall scan for the web request from client, check if the request is not allowed [DONE]
3. Create an HTML file that will show when the request from client is not allowed. [DONE]
4. Transfer the HTML file as the web request. [NOT DONE]
5. If the request is blocked then the client web browser will display our HTML. [NOT DONE]

Anyone know to resolve this. I have analize the packet for web response, it seem encrypted or something else.

Thanks. Happy coding.



Answer this question

Create web response from HTML file ?

  • James F

    I didn't try to give you a complete solution. I gave you some sample code to get you started. You are not communicating what exactly you are wanting, so this is the best we can do to help you until you state your problem more clearly.

    Good luck.



  • Thomas_K

    How are you implementing this Are you using Sockets to accept connections an then doing your own parsing of the HTTP Protocol or are you using an existing webserver and then just trying to send the response back to the client Sounds like they are connecting to you as a proxy server, but what mechanism are you using for interpretting their request

  • Tamir Gal

    If the server is sending it back in GZip format, you can also tell HttpWebRequest to automatically unzip it when your code is reading the response stream. Check out the HttpWebRequest.AutomaticDecompression property

  • Kiran_deep

    Can you clarify what you mean by "firewall. Usually if the request is to long to respond it will show error on the browser with unknown reason (I forgot the HTTP Error Code), now I want to change that error behavior so the browser will display my HTML file." I'm not following this part of the question

  • GoodNews

    First you might have heard about SQUID on Linux, you see that this application block client request into internet not by the IP address of destination. It block client request by using the client request string (e.g we block all mp3 file request, so if client want to download mp3 the browser must send http request that contain ".mp3").
    I want to create the same as SQUID but for windows. I have an application like SQUID for windows but very difficult to understand. I want to create like that but the methods are simple so that even a colleger first degree understand. I live in Indonesia, I can create a difficult way but other of my friend will not understand. I want they to understand so that I can make a better application.
    Ok that the past. The point that for blocking of request I've been made, but then I want to tell the client that the request have been blocked by my application (in this case I can call it Firewall, for you maybe else). If client request by browser, I want to change the error "Request timeout" by HTML file that I create.

    Client Request -------------------->|---|
    | P |----------------->|----------|
    | y | | Internet |
    | B | | |
    1.My HTML Error Page | | | |
    Client Browser <--------------------| | | |
    2.Web Response | | | |
    Client Response <-------------------|---|<-----------------| |
    | | | |
    |---| |----------|
    Now my sistem like above, if client request need blocked then create packet web response with My HTML Error Page (Process 1), If not blocked then request will passed, if INTERNET response, then the response will passed to client (Process 2).

    PS: Difficult to put image in here.

    I hope anyone understand, if not you can ask me what I doing (sound silly huh).

    Thanks.

  • derekslager

    Well for me is that a simple system (I think). Because I am afraid that if I send like that the browser can not recognize that I send HTTP response for the browser. So that I want to create new network packet where the header and body I customize, then send back the packet to the client.

    Anyway thanks for reply.

  • John Talbott

    I completely understood what you are doing.

    This is what you need to do.

    1. Accept connection
    2. Parse the incoming HTTP request
    3. If it is good then connect to the real server and send the request to the real server
    4. Wait for teh response
    5. Send the response back to the client
    6. You might have to modify the headers to indicate that the request is coming via a proxy

    In case the request is not valid you are generating an HTML response your self
    This should be in the form of

    HTTP/1.1 403 Forbidden
    Content-Type: text/html
    Content-Length: <nnnnn>

    The content you want to get is blocked

    Thats all you need to do.
    What you must also do is to maintain connections on your proxy.
    Note that multiple browsers can connect to your server simultaneously.



  • Harinadh

    Wow, that was a very complex answer to a simple question. Sounds like you are using sockets directly to read the request from the client. Looks to me like Durgaprasad already answered your question.

    Durgaprasad Gorti wrote:

    HTTP/1.1 403 Forbidden
    Content-Type: text/html
    Content-Length: <nnnnn>

    The content you want to get is blocked

    Thats all you need to do.
    What you must also do is to maintain connections on your proxy.
    Note that multiple browsers can connect to your server simultaneously.

    His example is correct, all you need to do is send back the response headers followed by an empty line and then the body of the response. In your case, the response body will be the contents of an html file...

    Here is some sample code...note that I am sending back the "Connection: Close" header, which you may or may not want. You may want to read up on the HTTP RFC to give you a little more understanding as to what is happening.

    void SendHtmlResponse(Socket client, int statusCode, string statusDescription, string htmlFilePath)
    {
    FileStream htmlStream = File.OpenRead(htmlFilePath);

    try
    {
    string respHeaders = "HTTP/1.1 {0} {1}\r\nContent-Type: text/html\r\nContent-Length: {2}\r\nConnection: Close\r\n\r\n";

    respHeaders = String.Format(respHeaders,statusCode,statusDescription,htmlStream.Length);

    client.Send(Encoding.ASCII.GetBytes(respHeaders));

    byte [] buffer = new byte[1024];

    int count = htmlStream.Read(buffer,0,buffer.Length);

    while (count > 0) {
    client.Send(buffer,0,count,SocketFlags.None);
    count = htmlStream.Read(buffer,0,buffer.Length);
    }
    }
    finally
    {
    htmlStream.Close();
    }
    }



  • voidlogic

    System will be like this:
    GATEWAY
    +------+ +----------------------------------------+
    |Client|<---------------->|-| +---------------+|
    +------+ | | +----------------+ | Block using ||
    | ->| Capture Packet |->| Packet Filter ||
    +------+ | | +----------------+ | API || +----------+
    |Client|<---------------->|-| || +---------------+|<---->| INTERNET |
    +------+ | | \/ | +----------+
    | | +-------------------+ |
    +------+ | | | Modify source | |
    |Client|<---------------->|-| | address of packet |<-------------->|
    +------+ | | as Gateway IP | |
    | +-------------------+ |
    +----------------------------------------+
    (Better view copy this into notepad).
    Scene:
    1. Client request something into Internet.
    2. Capture the packet using Sniff method.
    3. We know even using Sniff method Network Packet still passed into internet, thats why we put Packet Filter API to block client packet passed into INTERNET.
    4. After capture, modify the source address of packet into GATEWAY IP, because for Packet Filter only Packet with GATEWAY IP can passed into INTERNET. PS: Don't forget save the packet into Hashtable so we can know who request this if we receive web response from INTERNET.
    5. Pass this packet into internet. (Use system.net.socket with mode Raw Socket, this way we can send custom IP header).

    I think it very simple, because I don't like make sistem complicated.
    "SIMPLE BUT SECURE"

    PS: Still difficult put image on this forum.

    I hope you understand.

  • Frenske

    What I am doing Well its very simple, I want to create a firewall like SQUID that block someone request depend on the request not the destination IP. For blocking request I use socket to read the Internet Packet Stream. Its done, I can read the request of client. Now if my application block the request it will send a stream in HTML format to client so that the browser will show the client that his/her request have been blocked by my firewall. Usually if the request is to long to respond it will show error on the browser with unknown reason (I forgot the HTTP Error Code), now I want to change that error behavior so the browser will display my HTML file.
    Thanks for tell me of System.IO.Compression Namespace, I really new on .NET Programming.

    Thank you.

  • sydneyausguy

    Well basically your firewall needs to send HTML back to the browser
    You need to send the HTTP header for response status 200 or whatever status code you want to use you may want to use 403 forbidden or so. Then set the content length header and send the content along with it.



  • Shola

    Well I have use a software called Visual Sniffer to view any HTTP packet over my network adapter. The packet that I saw have the HTTP header then followed by html data, now the data is encrypted by GZIP. I use Visual Studio 2005 and .Net Framework, Is there any library to use GZIP compression that have same characteristic with the GZIP that used for HTTP Packet

  • Keith Dorken

    Can you please clearly explain what exactly is doing on

    Did you check out the System.IO.Compression namespace



  • Create web response from HTML file ?