HTTP and some other odds and ends Nelson Padua-Perez Bill Pugh

advertisement
HTTP and some other
odds and ends
Nelson Padua-Perez
Bill Pugh
Department of Computer Science
University of Maryland, College Park
1
For your amusement
Original 1991 HTTP spec
http://www.w3.org/Protocols/HTTP/AsImplemented.html
barely one page
As defined in 1992
http://www.w3.org/Protocols/HTTP/HTTP2.html
2
HTTP GET Request
HTTP GET Request
GET request HTTP/1.0
or
GET request HTTP/1.1
Followed by any number of other HTTP request
lines
Followed by a blank line
3
Other sample headers
Accept: */*
Accept-Language: en
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/5.0 (Macintosh; U; Intel Mac OS X; en) AppleWebKit/417.9
(KHTML, like Gecko) Safari/417.8
Connection: keep-alive
Host: localhost:8888
4
Other HTTP requests
HEAD
just requests information about a page (size, last
time it was modified)
body of page not sent
POST
should be used for any HTTP request that will
modify state
should always be OK to repeat a GET request, not so
with POST
parameters sent in body of request, after blank line,
rather than in URL
5
Obscure HTTP requests
The HTTP protocol defines some additional
requests (e.g., DELETE) that almost nobody
uses or supports
6
HTTP Response
status line
any number of response headers
a blank line
response data (binary format)
7
HTTP Response Status
HTTP/1.0 200 OK
the http version the server is speaking
a numeric status code
a string description of the reason for the status
8
HTTP response headers
Sample:
Date: Wed, 01 Mar 2006 03:27:02 GMT
Server: Apache
Accept-Ranges: bytes
Connection: close
Content-Type: text/html; charset=ISO-8859-1
Content-Type is the only one that is really
required
9
HTTP body
Body needs to be treated as binary data
If you open a JPEG file as a text file, read lines of
text from it and print them to a writer wrapped
around the socket output stream
it will not work
So, how do you handle this?
10
Mixed text/binary output
PrintWriter
PrintWriter w = new PrintWriter(out);
w.println(“HTTP/1.0 200 OK”);
Text
// PrintWriter autoflushes
w.println(“Content-Type: text/html”);
OutputStream
// PrintWriter autoflushes again
w.println();
// just one byte of data for this example
out.write(data);
bytes
out.close();
Network
11
One more thing...
Reflection
The ability of Java programs to look at what classes,
methods and fields are defined
and manipulate/invoke/access
classes/methods/fields specified by strings rather
than by compiled code
Among other things
allows your code to load/invoke code written after
your code was compiled and shipped
has to have access to new code
has to get names of code to invoke
12
Class objects
For each class in Java, there is a Class object
associated with that class
Use x.getClass() to get Class object associated with
object referenced by x
Use, for example, String.class to get Class object
associated with class String
Use Class.forName(s) to get the class named by the
String s
13
newInstance()
Given a Class object, you can create an
instance of that class by invoking
newInstance() on the Class object
you will usually want to cast the result to some type
Creates the instance using the void constructor
for the class.
Runtime error if there is no accessible void
constructor
14
Download