Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 HTTPCore, Cookies Managing Data on the World Wide-Web Elad Kravi.

Similar presentations


Presentation on theme: "1 HTTPCore, Cookies Managing Data on the World Wide-Web Elad Kravi."— Presentation transcript:

1 1 HTTPCore, Cookies Managing Data on the World Wide-Web Elad Kravi

2 2 Overview HttpCore – handlers 4 4 HttpCore - connections 2 2 HttpCore - basics 1 1 HttpCore - demo 5 5 6 6 Cookies HttpCore - processors 3 3

3 3 Apache Software Foundation HttpCore is a set of components implementing the most fundamental aspects of the HTTP protocol that are nonetheless sufficient to develop full-featured client-side and server-side HTTP services with a minimal footprint. blocking I/O model and and non-blocking I/O. HttpCore - Intro

4 4 Manipulate http properties: –Client –Server –Proxy –236369 home work… My example of downloading data from the web –Messages from Social Networks (Twitter) When would you use HttpCore

5 5 Generic-message = start-line *(message-header CRLF) CRLF [ message- body ] Start-line = Request-Line | Status-Line Request = Request-Line *(( general-header | request-header | entity-header ) CRLF) CRLF [ message-body ] Response = Status-Line *(( general-header | response-header | entity-header ) CRLF) CRLF [ message-body ] HttpMessage, HttpRequest, HttpResponse

6 6 AbstractHttpMessage – base classAbstractHttpMessage –Handle headers: setHeader, getHeaders, etc.. –More elegant – use Interceptors (in the following) BasicHttpRequest HttpRequest request = new BasicHttpRequest("GET", "/", HttpVersion.HTTP_1_1); BasicHttpResponse HttpRequest request = new BasicHttpRequest("GET", "/", HttpVersion.HTTP_1_1); AbstractHttpMessage, BasicHttpRequest, BasicHttpResponse

7 7 Setting a header response.addHeader("Set-Cookie", "c1=a; path=/; domain=localhost"); Reading the headers 1)Header h1 = response.getFirstHeader("Set-Cookie"); 2)HeaderIterator it = response.headerIterator("Set-Cookie"); while (it.hasNext()) { System.out.println(it.next()); } HTTP headers can be tokenized into individual header elements. –Header elements are properties of the header, e.g. response.addHeader("Set-Cookie", "c2=b; path=\"/\", c3=c; domain=\"localhost\""); –Parameters can be parsed as name-value pairs. Setting headers - explicitly

8 8 HTTP messages can carry a content entity associated with the request or response. –Responses are usually expected to enclose a content entity Repeatable Entity - its content can be read more than once –only possible with self contained entities (ByteArrayEntity & StringEntity) Streamed Entity: The content is received from a stream, or generated on the fly. Http Entities - message payload

9 9 Http Entities (cont)

10 10 BasicHttpEntity - A generic streamed, non-repeatable entity that obtains its content from an InputStream. ByteArrayEntity - A self contained, repeatable entity that obtains its content from a byte array String myData = "Hello world on the other side!!"; ByteArrayEntity myEntity = new ByteArrayEntity(myData.getBytes()); StringEntity - A self contained, repeatable entity that obtains its content from java.lang.String object FileEntity - a self contained, repeatable entity that obtains its content from a file –for instance, sending a zip file with content type application/zip HttpEntity entity = new FileEntity(staticFile, "application/java-archive"); (Some) Types of Entities

11 11 Overview HttpCore – handlers 4 4 HttpCore - connections 2 2 HttpCore - basics 1 1 HttpCore - demo 5 5 6 6 Cookies HttpCore - processors 3 3 HttpCore - connections

12 12 A generic HTTP connection, useful on client and server side. –AbstractHttpClientConnectionAbstractHttpClientConnection sendRequestHeader, sendRequestEntity, receiveResponseHeader, flush –AbstractHttpServerConnectionAbstractHttpServerConnection receiveRequestHeader, receiveRequestEntity, sendResponseHeader, sendResponseEntity, flush –Which kind a proxy should have? Concrete types define ‘bind’ method to bind a socket. HttpConnection (extends closable)

13 13 Terminating HTTP connections –Gracefully by calling HttpConnection#close() –Forcibly by calling HttpConnection#shutdown() HTTP exception handling –IOException in case of an I/O failure such as socket timeout or an socket reset –HttpException that signals an Http failure such as a violation of the Http protocol Closing connection and Exceptions

14 14 Overview HttpCore – handlers 4 4 HttpCore - connections 2 2 HttpCore - basics 1 1 HttpCore - demo 5 5 6 6 Cookies HttpCore - processors 3 3

15 15 A routine that implements a specific aspect of the Http protocol using protocol interceptors –protocol interceptors act upon specific header(s) of the incoming or outgoing message. –http protocol processor is a collection of protocol interceptors that implements the 'Chain of Responsibility' pattern each individual protocol interceptor is expected to work on the particular aspect of the Http protocol it is responsible for. –If the interceptors must be executed in a particular order, they should be added to the processor in the same sequence. HttpProcessor

16 16 RequestContent / ResponseContent – important interceptors for outgoing requests / responses. –Responsible for delimiting content length by adding Content-Length or Transfer-Content headers based on the properties of the enclosed entity and the protocol version. RequestConnControl / ResponseConnControl – –responsible for adding Connection header to the outgoing requests / responses. RequestTargetHost – required for client side protocol –responsible for adding Host header RequestUserAgent – recommended for client side protocol –responsible for adding User-Agent header (Some) Protocol interceptors

17 17 Using Protocol Processors Send the request to the target host and get a response.

18 18 Protocol interceptors collaborate by sharing information such as processing state through an Http execution context. –HttpContext is a structure that can be used to map an attribute name to an attribute value. Http Context

19 19 Overview HttpCore – handlers 4 4 HttpCore - connections 2 2 HttpCore - basics 1 1 HttpCore - demo 5 5 6 6 Cookies HttpCore - processors 3 3 HttpCore - handlers

20 20 a client side HTTP protocol handler based on the blocking I/O model relies on HttpProcessor to generate mandatory protocol headers for all outgoing messagesHttpProcessor apply common, cross-cutting message transformations to all incoming and outgoing messages HttpRequestExecutor httpexecutor = new HttpRequestExecutor(); … httpexecutor.preProcess(request, httpproc, context); HttpResponse response = httpexecutor.execute(request, conn, context); … httpexecutor.postProcess(response, httpproc, context); HttpRequestExecutor

21 21 a server side HTTP protocol handler based on the blocking I/O model relies on HttpProcessor to generate mandatory protocol headers for all outgoing messagesHttpProcessor apply common, cross-cutting message transformations to all incoming and outgoing messages –whereas individual HttpRequestHandlers are expected to take care of application specific content generation and processingHttpRequestHandler –relies on HttpRequestHandler to resolve matching request handler for a particular request URI of an incoming HTTP requestHttpRequestHandler HttpService

22 22 Overview HttpCore – c & s handlers 4 4 HttpCore - headers 2 2 HttpCore - basics 1 1 HttpCore - demo 5 5 6 6 Cookies HttpCore - entities 3 3

23 23 A client implemented using HttpCore –Configuration HttpRequestExecutor HttpProcessor HttpContext HttpRequest –Main logic – “GET” some files from the server Using HttpRequestExecutor –preProcess, execute and postProcess Print some usefull information from the response What we will see in this example?

24 24 A server implemented using HttpCore –Configuration HttpService HttpProcessor HttpContext HttpRequest What we will see in this example? –Main logic – Upon receiving a new request Create a working thread –Handles the request Wait for the next request

25 25 HttpClient is a HTTP/1.1 compliant Http agent implementation based on HttpCore. It also provides reusable components for client-side authentication, Http state management, and Http connection management. –e.g., parsing cookies Self learning HttpClient

26 26 Overview HttpCore – c & s handlers 4 4 HttpCore - headers 2 2 HttpCore - basics 1 1 HttpCore - demo 5 5 6 6 Cookies HttpCore - entities 3 3

27 27 An Http cookie is a token or short packet of state information that the Http agent (e.g. browser) and the target server can exchange to maintain a session. In its simplest form an Http cookie is merely a name / value pair. –Usually a cookie also contains a number of attributes such as: version, a domain for which is valid, a path that specifies the subset of URLs on the origin server to which this cookie applies, and the maximum period of time for which the cookie is valid Http Cookies

28 28 Cookies allow to save state using stateless http protocol. Usefull scenario: a shopping cart Privacy hazzard: –Consider the following scenario: A cookie is set on the user’s browser Every site the user visits the cookie is sent to the server Cookies can help monitoring your browsing history Are they really ‘fortune cookies’?


Download ppt "1 HTTPCore, Cookies Managing Data on the World Wide-Web Elad Kravi."

Similar presentations


Ads by Google