We will see how HTTP server/proxy works and how to implement
basic servers.
May be FTP will be added in the future ...
This tutorial is based on Tutorial 01.
A proxy takes place between a client and the server.
The proxy is the server for the client, he is the client for the server.
Proxy
Proxy can be use to limite access to the net (prohibit
some site ...), anonymous surfing (IP is the proxy IP not yours) ...
The majority of anonymous internet surfing applications are based on using
proxies for hiding the IP of the client. Destination server should see the IP
of the proxy.
The proxy is between the client and the server, so
all data flow pass throw the proxy. So, we can catch queries of the client and
the response of the server.
So, if you set up your internet browser to use your proxy, you can catch the
queries of your navigator with its associted response (will be used bellow).
This can be done for FTP and other kind of connections.
First a proxy have a server side, effectively he's the server
for the client.
A ServerSocket is listening on a defined
port, for client connection. HTTP proxies usually listen on port
8080.
Server side (1) |
//Connecting server |
Processing of the query of the client is done in the listener
loop. Just after the connection is done, the thread is starten and the server
continue listening for another client request.
Using thread allow multiple connection of clients at a time.
When a client is connected (acceptConnection
above), a ListenerServerLink object is
created storing the connection Socket.
We add to this object a listener thread in which we wait for the request of the
client. Then, when the datas are received, we process inputs usinf the proxy
protocol (ie process what should be done with datas).
Server side (2) |
proxy.setServerLinkCreator(new
ServerLinkCreator<ListenerServerLink>(){ |
Here, we have received the query of the client. The proxy should transmit it to the server :
The destination server is usually specified in the client's
query. So, todetermine it, we have to know which kind of query (HTTP/1.1, FTP
...) depending on the proxy kind.
For an HTTP proxy protocol, the first line of the query is :
Query http://host:port/path/file.ext HTTP/1.1
We just have to take the second token an extract host:port from itand we have
the destination !
Here is the simplified code of the proxy protocol, to view full code read the
source of this tutorial.
Proxy Protocol |
//Read query |
Run the http proxy on the server machine :
HTTP Proxy start-up |
//Bind the proxy
on the port 888 |
In the client machine, set-up your web browser by entering the proxy server
IP and the proxy port.
The proxy server is the ip of the server machine (127.0.0.1, server and client
are the same machine here), the port is the port passed above (8888 here).
Here is a (french) screenshot of the connection settings of firefox :
Proxy settings under Firefox
Before writting an http server, you should know a little on HTTP 1.1 protocol
(see RFC 2616 for more informations).
The objectif of this tutorial is not to write a full HTTP server, we will only
see basis of an http server (most used features).
This query is used to retrieve ressources from the server.
It is basically used for asking some web page, pictures, dowload files ...
Form of the query :
GET ressource
HTTP/1.1
... //some options in relation to the query
Here is a capture of the traffic generated by your browser for requesting this
page.
This capture was taken from the HTTP proxy from the first part of this tutorial.
Query |
GET /Java/Network/Tutorials/Tutorial3.php
HTTP/1.1 |
Reply |
HTTP/1.1 200 OK |
The first line contains the reply code, here it is
200 OK (no error). There are other codes like
file not found (404 Not Found) ...
Following lines indicates the date of reply. It also indicates that the server
is an Apache powered by PHP.
Next two lines defines the transfert type and the MIME format of the ressource.
With the MIME format, the navigator can determinate which application to use
when receiving the file (ex: mime type of jnlp file is
application/x-java-jnlp-file associated to
Java Web Start).
This is followed by a blank line, and the number of bytes (only for chunked
transfert type) of the ressources in hexadecimal base (base 16).
Afterwards, the ressource is sended and followed by 0 (only for chunked
transfert type).
The ressource asked can be a folder (ex:
GET / HTTP/1.1).
Datas to send is either the index.html file in this folder or, if this file
don't exists, an html file generated listing files and directories.
Here is an exemple of an html page generated by the server listing files and
dirs :
Listing file & directories |
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"> |
Here is a capture for larger files like image, archives ...
GET /images/Fond.png HTTP/1.1 |
HTTP/1.1 200 OK |
Content length indicates the number of bytes of the file.
Accept ranges allow partial transfert of a ressource. ETag is a tag associated
to the ressource. The navigator can only ask a piece of a file, usefull for
incomplete transfert (see 206 Partial Content).
Here is an example of partial transfert :
206 Partial Content |
//Query |
This query used to send some datas to a ressource. This is
commonly used by form in html page.
This following form ask for the pseudo of the visitor and send it to the page
form.php
Form (example) |
<html> |
The query generated by the navigator look like :
Query |
POST /Form/form.php HTTP/1.1 |
Content type is application/x-www-form-urlencoded,
length is 15 bytes.
The server should send "Pseudo=mypseudo" to the page
form.php, then send the result to the client. This is commonly used
associated with php, cgi ...
Here, we write a basic server with no extensions like php ... We will send error
code 405 Method Not Allowed.
405 Method Not Allowed |
HTTP/1.1 405 Method Not Allowed |
Here is the synthesis of the implemented part of HTTP 1.1 for
our server.
HTTP Protocol |
//Read input datas |
Last modified on 01/07/2010 | |
Copyright © 2004-2010 Jérôme JOUVIE - All rights reserved. | http://jerome.jouvie.free.fr/ |