Main Page   Modules   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members   Related Pages  

ACEXML_HttpCharStream Class Reference

#include "ACEXML/common/HttpCharStream.h"

Inheritance diagram for ACEXML_HttpCharStream:

Inheritance graph
[legend]
Collaboration diagram for ACEXML_HttpCharStream:

Collaboration graph
[legend]
List of all members.

Public Methods

 ACEXML_HttpCharStream (void)
 Default constructor. More...

virtual ~ACEXML_HttpCharStream (void)
 Destructor. More...

int open (const ACEXML_Char *url)
 Open an URL. More...

virtual int available (void)
virtual int close (void)
virtual int get (ACEXML_Char &ch)
virtual int read (ACEXML_Char *str, size_t len)
virtual int peek (void)
virtual void rewind (void)
virtual const ACEXML_ChargetEncoding (void)

Private Methods

int send_request (void)
int get_url (size_t &len)

Private Attributes

ACEXML_Charurl_
ACEXML_URL_Addrurl_addr_
ACEXML_Mem_Map_Streamstream_
Connectorconnector_
off_t size_
ACEXML_Charencoding_

Detailed Description

An implementation of ACEXML_CharStream for reading input from a URL.

Definition at line 33 of file HttpCharStream.h.


Constructor & Destructor Documentation

ACEXML_HttpCharStream::ACEXML_HttpCharStream void   
 

Default constructor.

Definition at line 21 of file HttpCharStream.cpp.

00022   : url_(0),
00023     url_addr_(0),
00024     stream_(0),
00025     connector_(0),
00026     size_(0),
00027     encoding_ (0)
00028 {
00029 
00030 }

ACEXML_HttpCharStream::~ACEXML_HttpCharStream void    [virtual]
 

Destructor.

Definition at line 32 of file HttpCharStream.cpp.

References close.

00033 {
00034   this->close ();
00035 }


Member Function Documentation

int ACEXML_HttpCharStream::available void    [virtual]
 

Returns the available ACEXML_Char in the buffer. -1 if the object is not initialized properly.

Implements ACEXML_CharStream.

Definition at line 276 of file HttpCharStream.cpp.

00277 {
00278   return ACE_static_cast(int, this->stream_->available());
00279 }

int ACEXML_HttpCharStream::close void    [virtual]
 

Close this stream and release all resources used by it.

Implements ACEXML_CharStream.

Definition at line 282 of file HttpCharStream.cpp.

References connector_, encoding_, size_, stream_, url_, and url_addr_.

Referenced by ACE_TMAIN, open, and ~ACEXML_HttpCharStream.

00283 {
00284   delete[] this->url_;
00285   this->url_ = 0;
00286 
00287   delete this->url_addr_;
00288   this->url_addr_ = 0;
00289 
00290   delete this->stream_;
00291   this->stream_ = 0;
00292 
00293   delete this->connector_;
00294   this->connector_ = 0;
00295 
00296   this->size_ = 0;
00297 
00298   delete[] this->encoding_;
00299   this->encoding_ = 0;
00300 
00301   return 0;
00302 }

int ACEXML_HttpCharStream::get ACEXML_Char   ch [virtual]
 

Read the next ACEXML_Char. Return -1 if we are not able to return an ACEXML_Char, 0 if EOS is reached, or 1 if succeed.

Implements ACEXML_CharStream.

Definition at line 305 of file HttpCharStream.cpp.

References ACEXML_Char, ACEXML_Mem_Map_Stream::get_char, and stream_.

Referenced by ACE_TMAIN.

00306 {
00307   ch = (ACEXML_Char) this->stream_->get_char();
00308   return (ch == (ACEXML_Char)EOF ? -1 :0);
00309 }

int ACEXML_HttpCharStream::get_url size_t &    len [private]
 

Fetch the URL and save it in backing store.

Definition at line 110 of file HttpCharStream.cpp.

References ACE_ERROR_RETURN, HDST_BOL, HDST_CR, HDST_CRLF, HDST_CRLFCR, HDST_LF, HDST_LINE1_PROTOCOL, HDST_LINE1_STATUS, HDST_LINE1_WHITESPACE, HDST_TEXT, LM_ERROR, ACEXML_Mem_Map_Stream::recv, ACEXML_Mem_Map_Stream::rewind, ACEXML_Mem_Map_Stream::seek, and stream_.

Referenced by open.

00111 {
00112   int header_state = HDST_LINE1_PROTOCOL;
00113   int status = 0;
00114   size_t b = 0;
00115   char* buf = 0;
00116   size_t buflen = BUFSIZ;
00117   for (;;)
00118     {
00119       if ((buf = ACE_const_cast (char*,
00120                                  this->stream_->recv (buflen))) == 0)
00121         if (buflen <= 0)
00122           break;
00123 
00124       for (b = 0; b < buflen; ++b)
00125         {
00126           switch ( header_state )
00127             {
00128             case HDST_LINE1_PROTOCOL:
00129               switch ( buf[b] )
00130                 {
00131                 case ' ': case '\t':
00132                   header_state = HDST_LINE1_WHITESPACE; break;
00133                 case '\n': header_state = HDST_LF ; break;
00134                 case '\r': header_state = HDST_CR; break;
00135                 }
00136               break;
00137             case HDST_LINE1_WHITESPACE:
00138               switch ( buf[b] )
00139                 {
00140                 case '0': case '1': case '2': case '3': case '4':
00141                 case '5': case '6': case '7': case '8': case '9':
00142                   status = buf[b] - '0';
00143                   header_state = HDST_LINE1_STATUS;
00144                   break;
00145                 case '\n': header_state = HDST_LF ; break;
00146                 case '\r': header_state = HDST_CR; break;
00147                 default: header_state = HDST_TEXT; break;
00148                 }
00149               break;
00150             case HDST_LINE1_STATUS:
00151               switch ( buf[b] )
00152                 {
00153                 case '0': case '1': case '2': case '3': case '4':
00154                 case '5': case '6': case '7': case '8': case '9':
00155                   status = status * 10 + buf[b] - '0';
00156                   break;
00157                 case '\n': header_state = HDST_LF ; break;
00158                 case '\r': header_state = HDST_CR; break;
00159                 default: header_state = HDST_TEXT; break;
00160                 }
00161               break;
00162             case HDST_BOL:
00163               switch ( buf[b] )
00164                 {
00165                 case '\n': header_state = HDST_LF; break;
00166                 case '\r': header_state = HDST_CR; break;
00167                 default: header_state = HDST_TEXT; break;
00168                 }
00169               break;
00170             case HDST_TEXT:
00171               switch ( buf[b] )
00172                 {
00173                 case '\n': header_state = HDST_LF; break;
00174                 case '\r': header_state = HDST_CR; break;
00175                 }
00176               break;
00177 
00178             case HDST_LF:
00179               switch ( buf[b] )
00180                 {
00181                 case '\n': goto end_of_headers;
00182                 case '\r': header_state = HDST_CR; break;
00183                 default: header_state = HDST_TEXT; break;
00184                 }
00185               break;
00186 
00187             case HDST_CR:
00188               switch ( buf[b] )
00189                 {
00190                 case '\n': header_state = HDST_CRLF; break;
00191                 case '\r': goto end_of_headers;
00192                 default: header_state = HDST_TEXT; break;
00193                 }
00194               break;
00195 
00196             case HDST_CRLF:
00197               switch ( buf[b] )
00198                 {
00199                 case '\n': goto end_of_headers;
00200                 case '\r': header_state = HDST_CRLFCR; break;
00201                 default: header_state = HDST_TEXT; break;
00202                 }
00203               break;
00204 
00205             case HDST_CRLFCR:
00206               switch ( buf[b] )
00207                 {
00208                 case '\n': case '\r': goto end_of_headers;
00209                 default: header_state = HDST_TEXT; break;
00210                 }
00211               break;
00212             }
00213         }
00214     }
00215  end_of_headers:
00216   ++b;
00217   // Store the address of the beginning of data. We will use it to seek to
00218   // beginning of the data in the URL.
00219   char* data_beg = buf + b;
00220   buflen = BUFSIZ;
00221 
00222   // Get all of the data. Since this is backed by file store, we won't lose
00223   // any of the data.
00224   while (( buf = ACE_const_cast (char*,
00225                                  this->stream_->recv (buflen))) != 0)
00226     ;
00227 
00228   // Length of data in the URL.
00229   len = this->stream_->recv() - data_beg;
00230 
00231   // Move the pointer to the beginning of the file store.
00232   this->stream_->rewind();
00233 
00234   off_t data_offset = data_beg - this->stream_->recv();
00235   // Forward to the beginning of data.
00236   if (this->stream_->seek (data_offset, SEEK_SET) == -1)
00237     ACE_ERROR_RETURN ((LM_ERROR, "%s: %m",
00238                        "Error in seeking to beginning of data"), -1);
00239   return status;
00240 }

const ACEXML_Char * ACEXML_HttpCharStream::getEncoding void    [virtual]
 

Get the encoding of the file

Implements ACEXML_CharStream.

Definition at line 335 of file HttpCharStream.cpp.

References encoding_.

00336 {
00337   return this->encoding_;
00338 }

int ACEXML_HttpCharStream::open const ACEXML_Char   url
 

Open an URL.

Definition at line 38 of file HttpCharStream.cpp.

References ACE_ERROR_RETURN, ACE_NEW_RETURN, ACE_NONBLOCK, ACEXML_Char, close, get_url, LM_ERROR, ACEXML_Mem_Map_Stream::open, send_request, size_, stream_, ACEXML_URL_Addr::string_to_addr, ACE::strnew, url_, and url_addr_.

Referenced by ACE_TMAIN, and ACEXML_StreamFactory::create_stream.

00039 {
00040   this->url_ = ACE::strnew (url);
00041 
00042   ACE_NEW_RETURN (this->url_addr_, ACEXML_URL_Addr, -1);
00043   ACE_NEW_RETURN (this->stream_, ACEXML_Mem_Map_Stream, -1);
00044 
00045   if (this->url_addr_->string_to_addr (this->url_) == -1) {
00046     this->close();
00047     ACE_ERROR_RETURN ((LM_ERROR, "%p\n", "cannot convert URL"), -1);
00048   }
00049 
00050   ACE_NEW_RETURN (this->connector_,
00051                   Connector (0, ACE_NONBLOCK),
00052                   -1);
00053 
00054   if (this->stream_->open (this->connector_, *this->url_addr_) == -1) {
00055     this->close();
00056     ACE_ERROR_RETURN ((LM_ERROR, "%p\n", "cannot open backing store"), -1);
00057   }
00058 
00059   int result = this->send_request();
00060   if (result == -1) {
00061     this->close();
00062     ACE_ERROR_RETURN ((LM_ERROR, "%p\n", "send_request"), -1);
00063   }
00064 
00065   size_t len = 0;
00066   result = this->get_url(len);
00067   if (result == -1) {
00068     this->close();
00069     ACE_ERROR_RETURN ((LM_ERROR, "%p\n", "get_url"), -1);
00070   }
00071   if (result != 200) {
00072     this->close();
00073     ACE_ERROR_RETURN ((LM_ERROR, "Server returned status %d : %s\n",
00074                        result,
00075                        "Refer HTTP/1.1 for details"), -1);
00076   }
00077 
00078   this->size_ = ACE_static_cast(off_t, len);
00079   return 0;
00080 }

int ACEXML_HttpCharStream::peek void    [virtual]
 

Peek the next ACEXML_Char in the CharStream. Return the character if succeess, -1 if EOS is reached.

Implements ACEXML_CharStream.

Definition at line 323 of file HttpCharStream.cpp.

References ACEXML_Mem_Map_Stream::peek_char, and stream_.

00324 {
00325   return this->stream_->peek_char (0);
00326 }

int ACEXML_HttpCharStream::read ACEXML_Char   str,
size_t    len
[virtual]
 

Read the next batch of ACEXML_Char strings

Implements ACEXML_CharStream.

Definition at line 312 of file HttpCharStream.cpp.

References ACE_TEXT_CHAR_TO_TCHAR, and ACEXML_Char.

00314 {
00315   char* temp = ACE_const_cast (char*, this->stream_->recv (len));
00316   str = ACE_TEXT_CHAR_TO_TCHAR (temp);
00317   if (str == 0)
00318     return -1;
00319   return ACE_static_cast(int, len);
00320 }

void ACEXML_HttpCharStream::rewind void    [virtual]
 

Resets the file pointer to the beginning of the stream.

Definition at line 329 of file HttpCharStream.cpp.

References ACEXML_Mem_Map_Stream::rewind, and stream_.

00330 {
00331   this->stream_->rewind();
00332 }

int ACEXML_HttpCharStream::send_request void    [private]
 

Send a HTTP/1.1 request to fetch the contents of the URL.

Definition at line 244 of file HttpCharStream.cpp.

References ACE_DEFAULT_TIMEOUT, ACE_NEW_RETURN, ACE_TEXT_ALWAYS_CHAR, ACEXML_Mem_Map_Stream::send_n, ACE_OS::sprintf, stream_, ACE_OS_String::strlen, and ACE::strnew.

Referenced by open.

00245 {
00246   char* path = ACE::strnew (ACE_TEXT_ALWAYS_CHAR (this->url_addr_->get_path_name()));
00247   ACE_Auto_Basic_Array_Ptr<char> path_ptr (path);
00248   size_t commandsize = ACE_OS::strlen (path)
00249                        + ACE_OS::strlen (this->url_addr_->get_host_name ())
00250                        + 20     // Extra
00251                        + 1      // NUL byte
00252                        + 16 ;   // Protocol filler...
00253 
00254   char* command;
00255   ACE_NEW_RETURN (command, char[commandsize], -1);
00256 
00257   // Ensure that the <command> memory is deallocated.
00258   ACE_Auto_Basic_Array_Ptr<char> cmd_ptr (command);
00259 
00260   int bytes = ACE_OS::sprintf (command, "GET %s HTTP/1.1\r\n", path);
00261   bytes += ACE_OS::sprintf (&command[bytes], "Host: %s\r\n",
00262                             this->url_addr_->get_host_name ());
00263   bytes += ACE_OS::sprintf (&command[bytes], "\r\n");
00264 
00265   ACE_Time_Value tv (ACE_DEFAULT_TIMEOUT);
00266 
00267   // Send the command to the connected server.
00268   int retval = this->stream_->send_n (command, bytes, &tv);
00269   if (retval <= 0)
00270     return -1;
00271   return retval;
00272 }


Member Data Documentation

Connector* ACEXML_HttpCharStream::connector_ [private]
 

Definition at line 102 of file HttpCharStream.h.

Referenced by close.

ACEXML_Char* ACEXML_HttpCharStream::encoding_ [private]
 

Definition at line 106 of file HttpCharStream.h.

Referenced by close, and getEncoding.

off_t ACEXML_HttpCharStream::size_ [private]
 

Definition at line 104 of file HttpCharStream.h.

Referenced by close, and open.

ACEXML_Mem_Map_Stream* ACEXML_HttpCharStream::stream_ [private]
 

Definition at line 100 of file HttpCharStream.h.

Referenced by close, get, get_url, open, peek, rewind, and send_request.

ACEXML_Char* ACEXML_HttpCharStream::url_ [private]
 

Definition at line 96 of file HttpCharStream.h.

Referenced by close, and open.

ACEXML_URL_Addr* ACEXML_HttpCharStream::url_addr_ [private]
 

Definition at line 98 of file HttpCharStream.h.

Referenced by close, and open.


The documentation for this class was generated from the following files:
Generated on Mon Jun 16 13:27:10 2003 for ACEXML by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002