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

HttpCharStream.cpp

Go to the documentation of this file.
00001 // $Id: HttpCharStream.cpp,v 1.1.1.1.2.1 2003/04/21 19:14:54 chad Exp $
00002 
00003 #include "ace/ACE.h"
00004 #include "ace/ace_wchar.h"
00005 #include "ace/Auto_Ptr.h"
00006 #include "ACEXML/common/HttpCharStream.h"
00007 
00008 ACE_RCSID (common, HttpCharStream, "$Id: HttpCharStream.cpp,v 1.1.1.1.2.1 2003/04/21 19:14:54 chad Exp $")
00009 
00010 /* Header FSM states. */
00011 static const int HDST_LINE1_PROTOCOL = 0;
00012 static const int HDST_LINE1_WHITESPACE = 1;
00013 static const int HDST_LINE1_STATUS = 2;
00014 static const int HDST_BOL = 10;
00015 static const int HDST_TEXT = 11;
00016 static const int HDST_LF = 12;
00017 static const int HDST_CR = 13;
00018 static const int HDST_CRLF = 14;
00019 static const int HDST_CRLFCR = 15;
00020 
00021 ACEXML_HttpCharStream::ACEXML_HttpCharStream (void)
00022   : url_(0),
00023     url_addr_(0),
00024     stream_(0),
00025     connector_(0),
00026     size_(0),
00027     encoding_ (0)
00028 {
00029 
00030 }
00031 
00032 ACEXML_HttpCharStream::~ACEXML_HttpCharStream (void)
00033 {
00034   this->close ();
00035 }
00036 
00037 int
00038 ACEXML_HttpCharStream::open (const ACEXML_Char *url)
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 }
00081 
00082 // The FSM was taken from the implementation of http_get and that falls
00083 // under the following license:
00084 //
00085 // Copyrigh (c) 2000 by Jef Poskanzer <jef@acme.com>.  All rights reserved.
00086 
00087 // Redistribution and use in source and binary forms, with or without
00088 // modification, are permitted provided that the following conditions
00089 // are met:
00090 // 1. Redistributions of source code must retain the above copyright
00091 //    notice, this list of conditions and the following disclaimer.
00092 // 2. Redistributions in binary form must reproduce the above copyright
00093 //    notice, this list of conditions and the following disclaimer in the
00094 //    documentation and/or other materials provided with the distribution.
00095 
00096 // THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
00097 // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00098 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00099 // ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
00100 // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00101 // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00102 // OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00103 // HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00104 // LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00105 // OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00106 // SUCH DAMAGE.
00107 
00108 
00109 int
00110 ACEXML_HttpCharStream::get_url (size_t& len)
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 }
00241 
00242 
00243 int
00244 ACEXML_HttpCharStream::send_request (void)
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 }
00273 
00274 
00275 int
00276 ACEXML_HttpCharStream::available (void)
00277 {
00278   return ACE_static_cast(int, this->stream_->available());
00279 }
00280 
00281 int
00282 ACEXML_HttpCharStream::close (void)
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 }
00303 
00304 int
00305 ACEXML_HttpCharStream::get (ACEXML_Char& ch)
00306 {
00307   ch = (ACEXML_Char) this->stream_->get_char();
00308   return (ch == (ACEXML_Char)EOF ? -1 :0);
00309 }
00310 
00311 int
00312 ACEXML_HttpCharStream::read (ACEXML_Char *str,
00313                              size_t len)
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 }
00321 
00322 int
00323 ACEXML_HttpCharStream::peek (void)
00324 {
00325   return this->stream_->peek_char (0);
00326 }
00327 
00328 void
00329 ACEXML_HttpCharStream::rewind (void)
00330 {
00331   this->stream_->rewind();
00332 }
00333 
00334 const ACEXML_Char*
00335 ACEXML_HttpCharStream::getEncoding (void)
00336 {
00337   return this->encoding_;
00338 }

Generated on Mon Jun 16 13:23:22 2003 for ACEXML by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002