HTTP and Sockets Lecture 6 cs193i – Internet Technologies Summer 2004 Stanford University
Administrative Stuff Lab #2 due today HW #2 due July 21 Midterm Monday, July 19, in class
Midterm In class, Monday, July 19 Previous quarter’s exam online Closed-book 1 8x11 cheat sheet Short-answer
Today’s Discussion HTTP Wrap-up Sockets in Java HTTP 1.1 additions HTTP Proxies Sockets in Java
Persistent Connections HTTP 1.0 -- Connections close by Default No need for Content-length, end signaled by EOF (in-band signal) HTTP 1.1 -- Persistent by Default Must use Content-length
Chunked-Transfer Encoding Problem: Content-length costly for server May not know if generated dynamically Solution Server omits Content-Length Transfer-encoding: chunked Send Data in Chunks, Prefixed by length in Hex End is marked with Chunk Length 0
HTTP 1.1 Virtual Hosts Problem: Solution: Want multiple web sites hosted on single server Incoming requests must be directed to return appropriate web site’s page Solution: Request includes host name header http://www.foo.com host:www.foo.com
Try This At Home telnet cva.stanford.edu 80 GET / HTTP/1.1 Host:cva.stanford.edu Connection:Keep-Alive GET /group_mtg.html HTTP/1.1 Connection:Close
HTTP Proxy Servers "Middlemen" between clients and servers May "rewrite" HTTP requests and responses Proxy Server Looks like a server sending responses Looks like a client making requests
Why Proxy Servers? Content filtering Packet filter Prevent children from accessing sites Used to contact server in special way Firewall or circuit relay Security Rewrite content Swedish chef Redirect requests Content router To improve performance Logging activity
Why Proxy Servers? Web cache is also a type of proxy! RT latency reduced
Why Proxy Servers? Content Router
Why Proxy Servers? Content Router
Why Content Routers? Content Router
Why Proxy Servers? Content Router
Why Proxy Servers? Anonymizer
Why Proxy Servers? Translation
Why Proxy Servers? Translation http://translate.google.com/translate?hl=en&sl=es&u=http://espanol.yahoo.com/&prev=/search%3Fq%3Dspanish%26hl%3Den%26lr%3D%26ie%3DUTF-8
Requests via Proxy Client must realize talking to proxy Request header: GET path HTTP/1.0 Client must use absolute path! GET / HTTP/1.0 GET http://www.yahoo.com/ HTTP/1.0
Five Minute Break
Sockets in Java (Client) java.net.socket Create Socket Socket c = new Socket(“hostname”, port); OR Socket c = new Socket(“IP addr”, port);
Reading/Writing to Sockets Just like performing file I/O in Java Create input/output streams to read/write to/from socket Output example: PrintWriter class new PrintWriter(c.getOutputStream(), true); Input example: BufferedReader class new BufferedReader(new InputSreamReader(c.getInputStream());
Sockets in Java (Server) Create ServerSocket Socket listen = new ServerSocket(port); Create Socket ServerSocket.accept() Blocks until connection established Socket s =new listenSocket.accept(); Create input/output streams Close when finished
Exceptions Sockets throw exceptions when something goes wrong UnknownHostExcpetion Handle using try/catch pairs try{ (statement;)* }catch(ExceptionType e){ }finally{ } Can have multiple catch clauses for handling different exceptions
URL Class Automatically decomposes string into parts: getHost() getPort() getProtocol() getFile() // path
What do you do with this stuff? StringTokenizer class Allows you to grab tokenized strings one at a time You can specify what delineates strings Example: < or > could be delineators Pattern and Matcher classes Create pattern Use Matcher class to compare strings to pattern