- Forward


Design and Implementation of an HTTP Server
A Simple Network Application in Java


Prof. David Bernstein
James Madison University

Computer Science Department
bernstdh@jmu.edu

Print

Hypertext Transfer Protocol
Back SMYC Forward
  • Some Properties:
    • Simple
    • Stateless
  • For Our Current Purposes:
    • An easy way to "GET" text content from a host computer
The Process
Back SMYC Forward
  1. The client opens a connection
  2. The client sends a request (GET, HEAD, or POST)
  3. The client waits for a response
  4. The server processes the request
  5. The server sends a response
  6. The connection is closed
Handling Special Characters
Back SMYC Forward
  • Encoding:
    • URLEncoder java.net.URLEncoder
  • Decoding:
    • URLDecoder java.net.URLDecoder
Uniform Resource Identifiers
Back SMYC Forward
  • Defined:
    • A means for identifying a resource
    • Details are discussed in RFC 2396 and RFC 2732
  • HTTP URIs:
    • http://Host[:Port]/Path[?QueryString]
    • where the QueryString consists of name=value pairs delimited by the '&' character (i.e., name=value[&...])
A Sequence Diagram
Back SMYC Forward
images/http-example-v2_sequence.gif
HTTP 1.0 GET/HEAD Requests without Headers
Back SMYC Forward
  • GET Without Headers (a.k.a., Simple-Request):
    • GET URI HTTP/1.0 CRLF
      CRLF
  • HEAD:
    • HEAD URI HTTP/1.0 CRLF
      CRLF
HTTP 1.0 GET without Headers (cont.)
Back SMYC Forward

Ignoring the Query String

javaexamples/http/v0/HttpRequest.java
 
HTTP 1.0 Responses without Headers
Back SMYC Forward

HTTP/1.0 ResponseCode ResponseText CRLF
CRLF
Content-Length:length CRLF
Content-Type:type CRLF
CRLF
Data

javaexamples/http/v0/HttpResponse.java
 
Name=Value Pairs
Back SMYC Forward
  • Use:
    • The Query String uses name=value pairs
    • Headers use name=value pairs
    • Content Types are often represented as name=value pairs
  • An Observation:
    • We could use a Map but they really have more funtionality than we need (making them unsafe)
    • We sometimes need thread safety
    • We sometimes needs speed (and not thread safety)
A NameValueMap Class
Back SMYC Forward
  • Options for Limiting the Functionality:
    • Delegate to a Map but don't support the entire interface (as you would with the decorator)
    • Specialize a Map but override the unnecessary methods
  • The Better Option in this Case:
    • Since we want to support two versions (one that is thread-safe and one that is not) -- use a Factory and delegation
A NameValueMap Class (cont.)
Back SMYC Forward
javaexamples/http/v1/NameValueMap.java
 
HTTP 1.0 GET Requests with Headers
Back SMYC Forward

GET URI HTTP/1.0 CRLF
Name1: Value1 CRLF
Name2: Value2 CRLF
.
.
.
NameN: ValueN CRLF
CRLF

HTTP 1.0 GET with Headers (cont.)
Back SMYC Forward
javaexamples/http/v1/HttpRequest.java
 
HTTP 1.0 Responses with Headers
Back SMYC Forward

HTTP/1.0 ResponseCode ResponseText CRLF
Name1: Value1 CRLF
Name2: Value2 CRLF
.
.
.
NameN: ValueN CRLF
CRLF
Data

javaexamples/http/v1/HttpResponse.java
 
A Shortcoming of the Current Design
Back SMYC Forward
  • Code Duplication:
    • In the HttpRequest and HttpResponse classes
  • An Improvement:
    • Have an abstract common parent
An Improved Design
Back SMYC Forward
javaexamples/http/v2/HttpMessage.java
 
An Improved Design (cont.)
Back SMYC Forward
javaexamples/http/v2/HttpRequest.java
 
An Improved Design (cont.)
Back SMYC Forward
javaexamples/http/v2/HttpResponse.java
 
Content Types
Back SMYC Forward
  • Specification:
    • Use the Multipurpose Internet Mail Extensions (MIME) descibed in RFC 1521
  • Designing a MIMETyper Class:
    • Needs to read MIME types from a file into a NameValueMap so we don't want to construct a new one every time it is needed (hence, should use the Singleton pattern)
    • Each connection will need to use it so it needs to be thread safe
A MIMETyper Class
Back SMYC Forward
javaexamples/http/v2/MIMETyper.java
 
An HTTP Connection Handler
Back SMYC Forward
javaexamples/http/v2/HttpConnectionHandler.java
 
An HTTP Server
Back SMYC Forward
  • Using Multiple Threads:
    • Each connection can be handled in its own independent thread of execution
  • Using a Thread Pool:
    • Use of a fixed thread pool allows the server to degrade gracefully (i.e., the system won't stop responding when capacity is exceeded, it will service them as fast as it can)
An HTTP Server
Back SMYC Forward
javaexamples/http/v2/HttpServer.java
 
HTTP 1.0 POST Requests
Back SMYC Forward

POST URI HTTP/1.0 CRLF
Content-type: Type CRLF
Content-length: Bytes CRLF
CRLF
Data

There's Always More to Learn
Back -