HTTP_Handler.cpp

Go to the documentation of this file.
00001 // $Id: HTTP_Handler.cpp 79829 2007-10-23 12:39:52Z johnnyw $
00002 
00003 #include "tao/HTTP_Handler.h"
00004 
00005 #if (TAO_HAS_HTTP_PARSER == 1)
00006 
00007 #include "ace/OS_NS_stdio.h"
00008 #include "ace/OS_NS_string.h"
00009 #include "ace/OS_NS_strings.h"
00010 
00011 TAO_BEGIN_VERSIONED_NAMESPACE_DECL
00012 
00013 TAO_HTTP_Handler::TAO_HTTP_Handler (void)
00014 {
00015 }
00016 
00017 TAO_HTTP_Handler::TAO_HTTP_Handler (ACE_Message_Block * mb,
00018                             ACE_TCHAR *filename) :
00019   mb_ (mb),
00020   filename_ (ACE_OS::strdup (filename)),
00021   bytecount_ (0)
00022 {
00023 }
00024 
00025 TAO_HTTP_Handler::~TAO_HTTP_Handler (void)
00026 {
00027   if (this->filename_)
00028     {
00029       ACE_OS::free (this->filename_);
00030       filename_ = 0;
00031     }
00032 }
00033 
00034 int
00035 TAO_HTTP_Handler::open (void *)
00036 {
00037   if (this->send_request () != 0)
00038     ACE_ERROR_RETURN ((LM_ERROR, "TAO (%P|%t) - HTTP_Handler::open, send_request failed\n"), -1);
00039 
00040   if (this->receive_reply () != 0)
00041     ACE_ERROR_RETURN ((LM_ERROR, "TAO (%P|%t) - HTTP_Handler::open, receive_reply failed\n"), -1);
00042   return 0;
00043 
00044 }
00045 
00046 int
00047 TAO_HTTP_Handler::close (u_long)
00048 {
00049   return 0;
00050 }
00051 
00052 int
00053 TAO_HTTP_Handler::send_request (void)
00054 {
00055   return -1;
00056 }
00057 
00058 int
00059 TAO_HTTP_Handler::receive_reply (void)
00060 {
00061   return -1;
00062 }
00063 
00064 size_t
00065 TAO_HTTP_Handler::byte_count (void) const
00066 {
00067   return bytecount_;
00068 }
00069 
00070 // Reader **************************************************
00071 
00072 TAO_HTTP_Reader::TAO_HTTP_Reader (ACE_Message_Block * mb,
00073                                   ACE_TCHAR *filename,
00074                                   const char *request_prefix,
00075                                   const char *request_suffix) :
00076   TAO_HTTP_Handler (mb, filename),
00077   request_prefix_ (request_prefix),
00078   request_suffix_ (request_suffix)
00079 {
00080 }
00081 
00082 int
00083 TAO_HTTP_Reader::send_request (void)
00084 {
00085   char mesg [MAX_HEADER_SIZE];
00086 
00087   // Check to see if the request is too big
00088   if (MAX_HEADER_SIZE < (ACE_OS::strlen (request_prefix_)
00089                          + ACE_OS::strlen (filename_)
00090                          + ACE_OS::strlen (request_suffix_) + 4))
00091     ACE_ERROR_RETURN((LM_ERROR,"TAO (%P|%t) - HTTP_Reader::send_request, request too large!"), -1);
00092 
00093   // Create a message to send to the server requesting retrieval of the file
00094   int const len = ACE_OS::sprintf (mesg, "%s %s %s", request_prefix_, filename_, request_suffix_);
00095 
00096   // Send the message to server
00097   if (peer ().send_n (mesg, len) != len)
00098     ACE_ERROR_RETURN((LM_ERROR,"TAO (%P|%t) - HTTP_Reader::send_request, error sending request\n"), -1);
00099 
00100   return 0;
00101 }
00102 
00103 int
00104 TAO_HTTP_Reader::receive_reply (void)
00105 {
00106   size_t num_recvd = 0;
00107   char buf [MTU+1];
00108   char *buf_ptr = 0;
00109   size_t bytes_read = 0;
00110 
00111   // Receive the first MTU bytes and strip the header off.
00112   // Note that we assume that the header will fit into MTU bytes.
00113   if (peer ().recv_n (buf, MTU, 0, &num_recvd) >= 0)
00114     {
00115       //Make sure that response type is 200 OK
00116       if (ACE_OS::strstr (buf,"200 OK") == 0)
00117         ACE_ERROR_RETURN ((LM_ERROR,
00118                             "TAO (%P|%t) - HTTP_Reader::receive_reply, Response is not 200 OK\n" ), -1);
00119 
00120       // Search for the header termination string "\r\n\r\n", or "\n\n". If
00121       // found, move past it to get to the data portion.
00122       if ((buf_ptr = ACE_OS::strstr (buf,"\r\n\r\n")) != 0)
00123         buf_ptr += 4;
00124       else if ((buf_ptr = ACE_OS::strstr (buf, "\n\n")) != 0)     //for compatibility with JAWS
00125         buf_ptr += 2;
00126       else
00127         buf_ptr = buf;
00128 
00129       // Determine number of data bytes read. This is equal to the
00130       // total bytes read minus number of header bytes.
00131       bytes_read = num_recvd - (buf_ptr - buf);
00132 
00133     }
00134   else
00135     {
00136       ACE_ERROR_RETURN ((LM_ERROR,
00137                          "TAO (%P|%t) - HTTP_Reader::receive_reply, error while reading header\n"), -1);
00138     }
00139 
00140   // ***************************************************************
00141   // At this point, we have stripped off the header and are ready to
00142   // process data. buf_ptr points to the data
00143 
00144   ACE_Message_Block* temp = 0;
00145   ACE_Message_Block* curr = this->mb_;
00146 
00147   ACE_NEW_RETURN (temp,
00148                   ACE_Message_Block (bytes_read),
00149                   -1);
00150   curr->cont (temp);
00151   curr = curr->cont ();
00152 
00153   // Copy over all the data bytes into our message buffer.
00154   if (curr->copy (buf_ptr, bytes_read) == -1)
00155     {
00156       ACE_ERROR_RETURN ((LM_ERROR, "TAO (%P|%t) - HTTP_Reader::receive_reply, error copying data into Message_Block\n"), -1);
00157     }
00158 
00159   // read the rest of the data into a number of ACE_Message_Blocks and
00160   // chain them together in a link list fashion
00161   num_recvd = 0;
00162 
00163   do
00164   {
00165     if (curr->space () == 0)
00166     {
00167       ACE_NEW_RETURN (temp,
00168                       ACE_Message_Block (MTU),
00169                       -1);
00170       curr->cont (temp);
00171       curr = curr->cont ();
00172     }
00173 
00174   if (peer ().recv_n (curr->wr_ptr (), curr->space (), 0, &num_recvd) >= 0)
00175     {
00176       // Move the write pointer
00177       curr->wr_ptr (num_recvd);
00178 
00179       // Increment bytes_read
00180       bytes_read += num_recvd;
00181 
00182     }
00183   else
00184     {
00185       ACE_ERROR_RETURN ((LM_ERROR,
00186                          "TAO (%P|%t) - HTTP_Reader::receive_reply, Error while reading header\n"), -1);
00187     }
00188   } while (num_recvd != 0);
00189 
00190   // Set the byte count to number of bytes received
00191   this->bytecount_ = bytes_read;
00192 
00193   return 0;
00194 }
00195 
00196 TAO_END_VERSIONED_NAMESPACE_DECL
00197 
00198 #endif /* TAO_HAS_HTTP_PARSER == 1*/
00199 

Generated on Tue Feb 2 17:37:52 2010 for TAO by  doxygen 1.4.7