Dear Wiki user,

You have subscribed to a wiki page or wiki category on "Httpcomponents Wiki" 
for change notification.

The following page has been changed by OlegKalnichevski:
http://wiki.apache.org/HttpComponents/HttpCoreTutorial

------------------------------------------------------------------------------
  }}}
  
  HTTP headers get tokenized into individual header elements on demand only. 
HTTP headers received over an HTTP connection are stored internally as an array 
of chars and parsed lazily only then their properties are accessed. 
- 
- The char array backing the header can be obtained using the optional 
!FormattedHeader interface.
- 
- {{{
- Header h1 = response.getFirstHeader("Content-Type");
- if (h1 instanceof FormattedHeader) {
-     CharArrayBuffer buf = ((FormattedHeader) h1).getBuffer();
-     System.out.println(buf);
- }
- }}}
- 
  
  == HTTP entity ==
  
@@ -1631, +1620 @@

  
  === Compatibility with blocking I/O ===
  
- In addition to asynchronous protocol handlers described above !HttpCore ships 
two variants of HTTP protocol handlers that emulate blocking I/O model on top 
of non-blocking one and allow message content to be produced and consumed using 
standard !OutputStream / !InputStream API. Compatibilty protocol handlers can 
work with HTTP request handlers and request executors that rely on blocking 
!HttpEntity implementations.
+ In addition to asynchronous protocol handlers described above HttpCore ships 
two variants of HTTP protocol handlers that emulate blocking I/O model on top 
of non-blocking one and allow message content to be produced and consumed using 
standard OutputStream / InputStream API. Compatibilty protocol handlers can 
work with HTTP request handlers and request executors that rely on blocking 
HttpEntity implementations.
  
  Compatibility protocol handlers rely on !HttpProcessor to generate mandatory 
protocol headers for all outgoing messages and apply common, cross-cutting 
message transformations to all all incoming and outgoing messages, whereas 
individual HTTP request executors / HTTP request processors are expected to 
take care of application specific content generation and processing.
  
@@ -1720, +1709 @@

  
  !HttpCore provides !SSLClientIOEventDispatch and !SSLServerIOEventDispatch 
I/O dispatch implementations that can be used to SSL enable connections managed 
by any arbitrary I/O reactor. The dispatches take all the necessary actions to 
wrap active I/O sessions with the SSL I/O session decorator and ensure correct 
handling of the SSL protocol handshaking.
  
+ = Advanced topics =
+ 
+ == HTTP message parsing and formatting framework ==
+ 
+ HTTP message processing framework has been designed to be expressive and 
flexible while remaining memory efficient and fast. !HttpCore HTTP message 
processing achieves near zero intermediate garbage and near zero-copy buffering 
for most of its parsing and formatting operations. The same HTTP message 
parsing and formatting API and implementations are used by both blocking and 
non-blocking transport implementations, which helps ensure a consistent 
behaviour of HTTP services regardless of I/O model.
+ 
+ === HTTP line parsing and formatting ===
+ 
+ !HttpCore consistently utilizes a small number of low level components for 
all its line parsing and formatting methods. 
+ 
+ !CharArrayBuffer represents a sequence of characters, usually a single line 
in an HTTP message stream such as a request line, a status line or a header. 
Internally !CharArrayBuffer is backed by an array of chars, which can be 
expanded to accommodate more input if needed. !CharArrayBuffer also provides a 
number of utility methods for manipulating content of the buffer, storing more 
data and retrieving subsets of data.
+ 
+ {{{
+ CharArrayBuffer buf = new CharArrayBuffer(64); 
+ buf.append("header:  data ");
+ int i = buf.indexOf(':');
+ String s = buf.substringTrimmed(i + 1, buf.length());
+ System.out.println(s);
+ System.out.println(s.length());
+ }}}
+ 
+ stdout >
+ {{{
+ data
+ 4
+ }}}
+ 
+ !ParserCursor represents a context of a parsing operation: the bounds 
limiting the scope of the parsing operation and the current position the 
parsing operation is expected to start at. 
+ 
+ {{{
+ CharArrayBuffer buf = new CharArrayBuffer(64); 
+ buf.append("header:  data ");
+ int i = buf.indexOf(':');
+ ParserCursor cursor = new ParserCursor(0, buf.length()); 
+ cursor.updatePos(i + 1);
+ System.out.println(cursor);
+ }}}
+ 
+ stdout >
+ {{{
+ [0>7>14]
+ }}}
+ 
+ !LineParser is the interface for parsing lines in the HEAD section of an HTTP 
message. There are individual methods for parsing a request line, a status 
line, or a header line. The lines to parse are passed in memory, the parser 
does not depend on any specific I/O mechanism. Instances of this interface are 
expected to be stateless and thread-safe.
+ 
+ {{{
+ CharArrayBuffer buf = new CharArrayBuffer(64); 
+ buf.append("HTTP/1.1 200");
+ ParserCursor cursor = new ParserCursor(0, buf.length()); 
+ 
+ LineParser parser = new BasicLineParser();
+ ProtocolVersion ver = parser.parseProtocolVersion(buf, cursor);
+ System.out.println(ver);
+ System.out.println(buf.substringTrimmed(cursor.getPos(), 
cursor.getUpperBound()));
+ }}}
+ 
+ stdout >
+ {{{
+ HTTP/1.1
+ 200
+ }}}
+ 
+ {{{
+ CharArrayBuffer buf = new CharArrayBuffer(64); 
+ buf.append("HTTP/1.1 200 OK");
+ ParserCursor cursor = new ParserCursor(0, buf.length()); 
+ LineParser parser = new BasicLineParser();
+ StatusLine sl = parser.parseStatusLine(buf, cursor);
+ System.out.println(sl.getReasonPhrase());
+ }}}
+ 
+ stdout >
+ {{{
+ OK
+ }}}
+ 
+ !LineFormatter for formatting elements of the HEAD section of an HTTP 
message. This is the complement to !LineParser. There are individual methods 
for formatting a request line, a status line, or a header line. Instances of 
this interface are expected to be stateless and thread-safe.
+ 
+ Please note the formatting does not include the trailing line break sequence 
CR-LF.
+ 
+ {{{
+ CharArrayBuffer buf = new CharArrayBuffer(64); 
+ LineFormatter formatter = new BasicLineFormatter();
+ formatter.formatRequestLine(buf, new BasicRequestLine("GET", "/", 
HttpVersion.HTTP_1_1));
+ System.out.println(buf.toString());
+ formatter.formatHeader(buf, new BasicHeader("Content-Type", "text/plain"));
+ System.out.println(buf.toString());
+ }}}
+ 
+ stdout >
+ {{{
+ GET / HTTP/1.1
+ Content-Type: text/plain
+ }}}
+ 
+ !HeaderValueParser is the interface for parsing header values into elements. 
Instances of this interface are expected to be stateless and thread-safe.
+ 
+ {{{
+ CharArrayBuffer buf = new CharArrayBuffer(64); 
+ HeaderValueParser parser = new BasicHeaderValueParser();
+ buf.append("name1=value1; param1=p1, name2 = \"value2\", name3  = value3");
+ ParserCursor cursor = new ParserCursor(0, buf.length()); 
+ System.out.println(parser.parseHeaderElement(buf, cursor));
+ System.out.println(parser.parseHeaderElement(buf, cursor));
+ System.out.println(parser.parseHeaderElement(buf, cursor));
+ }}}
+ 
+ stdout >
+ {{{
+ name1=value1; param1=p1
+ name2=value2
+ name3=value3
+ }}}
+ 
+ !HeaderValueFormatter is the interface for formatting elements of a header 
value. This is the complement to !HeaderValueParser. Instances of this 
interface are expected to be stateless and thread-safe.
+ 
+ {{{
+ CharArrayBuffer buf = new CharArrayBuffer(64); 
+ HeaderValueFormatter formatter = new BasicHeaderValueFormatter();
+ HeaderElement[] hes = new HeaderElement[] {
+         new BasicHeaderElement("name1", "value1", 
+                 new NameValuePair[] {new BasicNameValuePair("param1", "p1")} 
),
+         new BasicHeaderElement("name2", "value2"), 
+         new BasicHeaderElement("name3", "value3"), 
+ };
+ formatter.formatElements(buf, hes, true);
+ System.out.println(buf.toString());
+ }}}
+ 
+ stdout >
+ {{{
+ name1="value1"; param1="p1", name2="value2", name3="value3"
+ }}}
+ 
+ === HTTP message streams and session I/O buffers ===
+ 
+ !HttpCore provides a number of utility classes for blocking and non-blocking 
I/O models that facilitate the processing of HTTP message streams, simplify 
handling of line-based structures in HTTP messages and manage intermediate data 
buffering. 
+ 
+ HTTP connection implementations usually rely on session input/output buffers 
for reading and writing data from and to an HTTP message stream. Session 
input/output buffer implementations are I/O model specific and are optimized 
either for blocking or non-blocking operations.
+ 
+ Blocking HTTP connections use socket bound session buffers to transfer data. 
Session buffer interfaces are similar to !InputStream / !OutputStream classes, 
but they also provide methods for reading and writing CRLF delimited lines. 
+ 
+ {{{
+ Socket socket1;
+ Socket socket2;
+ HttpParams params = new BasicHttpParams(); 
+ SessionInputBuffer inbuffer = new SocketInputBuffer(socket1, 4096, params);
+ SessionOutputBuffer outbuffer = new SocketOutputBuffer(socket2, 4096, params);
+ 
+ CharArrayBuffer linebuf = new CharArrayBuffer(1024); 
+ inbuffer.readLine(linebuf);
+ outbuffer.writeLine(linebuf);
+ }}}
+ 
+ Non-blocking HTTP connections use session buffers optimized for reading and 
writing data from and to non-blocking NIO channels. NIO session input/output 
sessions help deal with CRLF delimited lines in a non-blocking I/O model.  
+ 
+ {{{
+ ReadableByteChannel channel1;
+ WritableByteChannel channel2;
+ 
+ HttpParams params = new BasicHttpParams(); 
+ SessionInputBuffer inbuffer = new SessionInputBufferImpl(4096, 1024, params);
+ SessionOutputBuffer outbuffer = new SessionOutputBufferImpl(4096, 1024, 
params);
+ 
+ CharArrayBuffer linebuf = new CharArrayBuffer(1024); 
+ boolean endOfStream = false;
+ int bytesRead = inbuffer.fill(channel1);
+ if (bytesRead == -1) {
+     endOfStream = true;
+ }
+ if (inbuffer.readLine(linebuf, endOfStream)) {
+     outbuffer.writeLine(linebuf);
+ }
+ if (outbuffer.hasData()) {
+     outbuffer.flush(channel2);
+ }
+ }}}
+ 
+ === HTTP header parsing on demand ===
+ 
+ The char array backing the header can be obtained using the optional 
!FormattedHeader interface.
+ 
+ {{{
+ Header h1 = response.getFirstHeader("Content-Type");
+ if (h1 instanceof FormattedHeader) {
+     CharArrayBuffer buf = ((FormattedHeader) h1).getBuffer();
+     System.out.println(buf);
+ }
+ }}}
+ 

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to