Null Webserver

L

Luc The Perverse

Hi!

I have a new project.

To reduce ads that I see, I have downloaded a 1/2 mb hosts file which routes
most DNS to localhost 127.0.0.1

Problem is, sometimes it displays "cannot find page" and it tries and waits
for a picture to load.

I'd like to fill the void, with a webserver that serves nothing but blank
pages, and null media. (1x1 transparent gif, 1x1 JPG, Empty do nothing
Flash etc.)

I plan on running a real webserver (like Apache) and grabbing actual empty
files, and then hardcoding the server response into my application. I will
then feed this information whenever a request comes in.

I found this,
http://java.sun.com/developer/technicalArticles/Networking/Webserver/WebServer.java
Do you think I could convert this into what I need, or is it simpler than
this?
 
C

Chris Uppal

Luc said:
I'd like to fill the void, with a webserver that serves nothing but blank
pages, and null media. (1x1 transparent gif, 1x1 JPG, Empty do nothing
Flash etc.)
[...]
I found this,
http://java.sun.com/developer/technicalArticles/Networking/Webserver/WebServer.java
Do you think I could convert this into what I need, or is it simpler than
this?

From looking at the code, it is both more verbose/complicated than it needs to
be, it even manages to be over-featured (who needs properties for something so
simple?). It is also -- at the same time -- rather inflexible and limited.
(I'm not exactly overwhelmed by the code either, but that's not unusual).

Anyway, it would be a reasonable starting point if you prefer to work by
reading and modifying existing code. If not, then creating a simple HTTP
server from scratch would be an interesting and informative exercise, one I
suspect you would enjoy -- the Sun code gives you an idea of the scale of the
problem ;-)

-- chris
 
L

Luc The Perverse

Chris Uppal said:
Anyway, it would be a reasonable starting point if you prefer to work by
reading and modifying existing code. If not, then creating a simple HTTP
server from scratch would be an interesting and informative exercise, one
I
suspect you would enjoy -- the Sun code gives you an idea of the scale of
the
problem ;-)

You see though, that's the thing - I don't know where to look for the
simplest protocal instructions. I search for HTML specification and find a
W3C subsite that is at least hundreds of pages long - and it still doesn't
answer the most basic questions.

Then I realize HTML specification isn't what I'm looking for, so I search
for web server specifications and get nothing related to what I need. I
assume it's a TCP connection, but I'm not positive.

I could mindlessly hack away at the example code, but I don't think that
would do much.

Or I could start at the other end and use a packet sniffer and try to
determine what it is doing. This would be a good learning experience, but
I'm not sure I'm that bored.

What I'd like is a build your own webserver tutorial - or at least an
incremental approach to understanding webserver communications. If that
doesn't exist, where can I find a mature full specification of the
communication?
 
G

Gordon Beaton

You see though, that's the thing - I don't know where to look for the
simplest protocal instructions. I search for HTML specification and find a
W3C subsite that is at least hundreds of pages long - and it still doesn't
answer the most basic questions.

Then I realize HTML specification isn't what I'm looking for, so I
search for web server specifications and get nothing related to what
I need.

HTTP is described in rfc2616. Some extensions are described in other
documents but rfc2616 is all you need to write a basic web server.

/gordon
 
G

Gordon Beaton

To reduce ads that I see, I have downloaded a 1/2 mb hosts file
which routes most DNS to localhost 127.0.0.1

Problem is, sometimes it displays "cannot find page" and it tries
and waits for a picture to load.

I'd like to fill the void, with a webserver that serves nothing but blank
pages, and null media. (1x1 transparent gif, 1x1 JPG, Empty do nothing
Flash etc.)

I've done something similar to generate random pages. All that's
necessary is that you read (and ignore) the request, then respond with
whatever content you like.

Run it from inetd or similar and you don't even need to deal with the
socket connections, you can read from System.in and write to
System.out. It really doesn't need to be more complicated than this
(this code is untested):

// get request
br = new BufferedReader(...(System.in));
while (((line = br.readLine()) != null) && (line.length != 0));

// response header
System.out.println("HTTP/1.1 200 OK");
System.out.println("Date: %s" + rfcDate());
System.out.println("Server: Apache/2.0.50 (Unix) DAV/2");
System.out.println("Connection: close");
System.out.println("Content-Type: text/html; charset=iso-8859-1");
System.out.println();

// response body
System.out.println("<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">");

// add your null content here
System.out.println(getNullContent());

/gordon
 
T

Thomas Fritsch

Luc The Perverse said:
You see though, that's the thing - I don't know where to look for the
simplest protocal instructions. I search for HTML specification and find
a W3C subsite that is at least hundreds of pages long - and it still
doesn't answer the most basic questions.

Then I realize HTML specification isn't what I'm looking for, so I search
for web server specifications and get nothing related to what I need. I
assume it's a TCP connection, but I'm not positive. [...]
What I'd like is a build your own webserver tutorial - or at least an
incremental approach to understanding webserver communications. If that
doesn't exist, where can I find a mature full specification of the
communication?

You need the HTTP/1.0 specification (RFC 1945), at least some parts of it.
See for example http://www.faqs.org/rfcs/rfc1945.html .
You don't need the HTTP/1.1 specification (RFC 2616). That would be kind of
overkill for your goal, because every web-browser is required to understand
HTTP/1.0 responses.
And you don't need the HTML specification, because your server can always
send a simplistic HTML content:
<HTML>
</HTML>

Before starting your implemention make sure to understand the class
java.net.ServerSocket. Google for any Java examples using ServerSocket, and
you will have half the solution for your problem.
 
R

Roedy Green

You need the HTTP/1.0 specification (RFC 1945), at least some parts of it.
See for example http://www.faqs.org/rfcs/rfc1945.html .

For a quick overview of HTTP see http://mindprod.com/jgloss/http.html

also get a packet sniffer and watch your browser and server talk to
each other. You will learn more in 30 minute than 5 hours pouring over
the RFCs.

See http://mindprod.com/jgloss/sniffer.html

Once you get the general idea, read the RFCs. See
http://mindprod.com/jgloss/rfc.html
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top