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 Member Functions

 ACEXML_HttpCharStream (void)
 Default constructor.

virtual ~ACEXML_HttpCharStream (void)
 Destructor.

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

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 int determine_encoding (void)
virtual const ACEXML_ChargetEncoding (void)
virtual const ACEXML_ChargetSystemId (void)

Private Member Functions

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_
off_t data_offset_
ACEXML_Charencoding_

Detailed Description

An implementation of ACEXML_CharStream for reading input from a URL.

Definition at line 32 of file HttpCharStream.h.


Constructor & Destructor Documentation

ACEXML_HttpCharStream::ACEXML_HttpCharStream void   ) 
 

Default constructor.

Definition at line 24 of file HttpCharStream.cpp.

00025   : url_(0),
00026     url_addr_(0),
00027     stream_(0),
00028     connector_(0),
00029     size_(0),
00030     data_offset_ (0),
00031     encoding_ (0)
00032 {
00033 
00034 }

ACEXML_HttpCharStream::~ACEXML_HttpCharStream void   )  [virtual]
 

Destructor.

Definition at line 36 of file HttpCharStream.cpp.

References close().

00037 {
00038   this->close ();
00039 }


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 284 of file HttpCharStream.cpp.

References ACEXML_Mem_Map_Stream::available().

00285 {
00286   if (this->stream_ == 0)
00287     return -1;
00288   return static_cast<int> (this->stream_->available());
00289 }

int ACEXML_HttpCharStream::close void   )  [virtual]
 

Close this stream and release all resources used by it.

Implements ACEXML_CharStream.

Definition at line 292 of file HttpCharStream.cpp.

References data_offset_, url_, and url_addr_.

Referenced by ACE_TMAIN(), open(), and ~ACEXML_HttpCharStream().

00293 {
00294   delete[] this->url_;
00295   this->url_ = 0;
00296 
00297   delete this->url_addr_;
00298   this->url_addr_ = 0;
00299 
00300   delete this->stream_;
00301   this->stream_ = 0;
00302 
00303   delete this->connector_;
00304   this->connector_ = 0;
00305 
00306   this->size_ = 0;
00307   this->data_offset_ = 0;
00308 
00309   delete[] this->encoding_;
00310   this->encoding_ = 0;
00311 
00312   return 0;
00313 }

int ACEXML_HttpCharStream::determine_encoding void   )  [virtual]
 

Determine the encoding of the file.

Definition at line 316 of file HttpCharStream.cpp.

References ACEXML_Char, ACEXML_Mem_Map_Stream::get_char(), ACEXML_Encoding::get_encoding(), ACEXML_Mem_Map_Stream::peek_char(), and ACE::strnew().

Referenced by open(), and rewind().

00317 {
00318   if (this->stream_ == 0)
00319     return -1;
00320 
00321   char input[4] = {0, 0, 0, 0};
00322   int i = 0;
00323   for (; i < 4 && input[i] != (char)-1; ++i)
00324     input[i] = static_cast<char> (this->stream_->peek_char(i));
00325   if (i < 4)
00326     return -1;
00327   const ACEXML_Char* temp = ACEXML_Encoding::get_encoding (input);
00328   if (!temp)
00329     return -1;
00330   else
00331     {
00332       if (this->encoding_)
00333         delete [] this->encoding_;
00334       this->encoding_ = ACE::strnew (temp);
00335       //   ACE_DEBUG ((LM_DEBUG, "URI's encoding is %s\n", this->encoding_));
00336     }
00337   // Move over the byte-order-mark if present.
00338   for (int j = 0; j < 3; ++j)
00339     {
00340       if (input[i] == '\xFF' || input[i] == '\xFE' || input[i] == '\xEF' ||
00341           input[i] == '\xBB' || input[i] == '\xBF')
00342         {
00343           this->stream_->get_char();
00344           continue;
00345         }
00346       break;
00347     }
00348   return 0;
00349 }

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 393 of file HttpCharStream.cpp.

References ACEXML_Char, and ACEXML_Mem_Map_Stream::get_char().

Referenced by ACE_TMAIN().

00394 {
00395   if (this->stream_ == 0)
00396     return -1;
00397 #if defined (ACE_USES_WCHAR)
00398   return this->get_i (ch);
00399 #else
00400   ch = (ACEXML_Char) this->stream_->get_char();
00401   return (ch == (ACEXML_Char)EOF ? -1 :0);
00402 #endif /* ACE_USES_WCHAR */
00403 }

int ACEXML_HttpCharStream::get_url size_t &  len  )  [private]
 

Fetch the URL and save it in backing store.

Definition at line 114 of file HttpCharStream.cpp.

References ACE_ERROR_RETURN, data_offset_, 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(), and ACEXML_Mem_Map_Stream::seek().

Referenced by open().

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

const ACEXML_Char * ACEXML_HttpCharStream::getEncoding void   )  [virtual]
 

Get the encoding of the file

Implements ACEXML_CharStream.

Definition at line 365 of file HttpCharStream.cpp.

00366 {
00367   return this->encoding_;
00368 }

const ACEXML_Char * ACEXML_HttpCharStream::getSystemId void   )  [virtual]
 

Implements ACEXML_CharStream.

Definition at line 371 of file HttpCharStream.cpp.

References url_.

00372 {
00373   return this->url_;
00374 }

int ACEXML_HttpCharStream::open const ACEXML_Char url  ) 
 

Open an URL.

Definition at line 42 of file HttpCharStream.cpp.

References ACE_ERROR_RETURN, ACE_NEW_RETURN, ACE_NONBLOCK, ACEXML_Char, close(), Connector, determine_encoding(), get_url(), LM_ERROR, ACEXML_Mem_Map_Stream::open(), send_request(), ACEXML_URL_Addr::string_to_addr(), ACE::strnew(), url_, and url_addr_.

Referenced by ACE_TMAIN(), and ACEXML_StreamFactory::create_stream().

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

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 406 of file HttpCharStream.cpp.

References ACEXML_Mem_Map_Stream::peek_char().

00407 {
00408   if (this->stream_ == 0)
00409     return -1;
00410 
00411 #if defined (ACE_USES_WCHAR)
00412   return this->peek_i();
00413 #else
00414   return this->stream_->peek_char (0);
00415 #endif /* ACE_USES_WCHAR */
00416 }

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 378 of file HttpCharStream.cpp.

References ACE_TEXT_CHAR_TO_TCHAR, ACEXML_Char, and ACEXML_Mem_Map_Stream::recv().

00380 {
00381   if (this->stream_ == 0)
00382     return -1;
00383   len = len * sizeof (ACEXML_Char);
00384   char* temp = const_cast<char*> (this->stream_->recv (len));
00385   str = ACE_TEXT_CHAR_TO_TCHAR (temp);
00386   if (str == 0)
00387     return -1;
00388   return static_cast<int> (len);
00389 }

void ACEXML_HttpCharStream::rewind void   )  [virtual]
 

Resets the file pointer to the beginning of the stream.

Implements ACEXML_CharStream.

Definition at line 352 of file HttpCharStream.cpp.

References ACE_ERROR, determine_encoding(), LM_ERROR, ACEXML_Mem_Map_Stream::rewind(), and ACEXML_Mem_Map_Stream::seek().

00353 {
00354   if (this->stream_ == 0)
00355     return;
00356   this->stream_->rewind();
00357 
00358   // Forward to the beginning of data.
00359   if (this->stream_->seek (this->data_offset_, SEEK_SET) == -1)
00360     ACE_ERROR ((LM_ERROR, "%s: %m", "Error in seeking to beginning of data"));
00361   this->determine_encoding();
00362 }

int ACEXML_HttpCharStream::send_request void   )  [private]
 

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

Definition at line 252 of file HttpCharStream.cpp.

References ACE_DEFAULT_TIMEOUT, ACE_NEW_RETURN, ACE_TEXT_ALWAYS_CHAR, ACEXML_Mem_Map_Stream::send_n(), ACE_OS::sprintf(), ACE_OS::strlen(), and ACE::strnew().

Referenced by open().

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


Member Data Documentation

Connector* ACEXML_HttpCharStream::connector_ [private]
 

Definition at line 127 of file HttpCharStream.h.

off_t ACEXML_HttpCharStream::data_offset_ [private]
 

Definition at line 131 of file HttpCharStream.h.

Referenced by close(), and get_url().

ACEXML_Char* ACEXML_HttpCharStream::encoding_ [private]
 

Definition at line 133 of file HttpCharStream.h.

off_t ACEXML_HttpCharStream::size_ [private]
 

Definition at line 129 of file HttpCharStream.h.

ACEXML_Mem_Map_Stream* ACEXML_HttpCharStream::stream_ [private]
 

Definition at line 125 of file HttpCharStream.h.

ACEXML_Char* ACEXML_HttpCharStream::url_ [private]
 

Definition at line 121 of file HttpCharStream.h.

Referenced by close(), getSystemId(), and open().

ACEXML_URL_Addr* ACEXML_HttpCharStream::url_addr_ [private]
 

Definition at line 123 of file HttpCharStream.h.

Referenced by close(), and open().


The documentation for this class was generated from the following files:
Generated on Thu Nov 9 11:47:58 2006 for ACEXML by doxygen 1.3.6