Merge pull request #2216 from jwillemsen/jwi-cxxversionchecks
[ACE_TAO.git] / TAO / tao / HTTP_Handler.cpp
bloba2b9b3a2c10dd9df0e063dc1ae0d4cf8662ed3db
1 #include "tao/HTTP_Handler.h"
3 #if (TAO_HAS_HTTP_PARSER == 1)
5 #include "ace/OS_NS_stdio.h"
6 #include "ace/OS_NS_string.h"
7 #include "ace/OS_NS_strings.h"
8 #include "tao/debug.h"
9 #include <cstring>
11 TAO_BEGIN_VERSIONED_NAMESPACE_DECL
13 TAO_HTTP_Handler::TAO_HTTP_Handler () :
14 mb_ (nullptr),
15 filename_ (nullptr),
16 bytecount_ (0)
20 TAO_HTTP_Handler::TAO_HTTP_Handler (ACE_Message_Block * mb,
21 ACE_TCHAR *filename) :
22 mb_ (mb),
23 filename_ (ACE_OS::strdup (filename)),
24 bytecount_ (0)
28 TAO_HTTP_Handler::~TAO_HTTP_Handler ()
30 if (this->filename_)
32 ACE_OS::free (this->filename_);
33 filename_ = nullptr;
37 int
38 TAO_HTTP_Handler::open (void *)
40 if (this->send_request () != 0)
41 TAOLIB_ERROR_RETURN ((LM_ERROR, "TAO (%P|%t) - HTTP_Handler::open, send_request failed\n"), -1);
43 if (this->receive_reply () != 0)
44 TAOLIB_ERROR_RETURN ((LM_ERROR, "TAO (%P|%t) - HTTP_Handler::open, receive_reply failed\n"), -1);
45 return 0;
48 int
49 TAO_HTTP_Handler::close (u_long)
51 return 0;
54 int
55 TAO_HTTP_Handler::send_request ()
57 return -1;
60 int
61 TAO_HTTP_Handler::receive_reply ()
63 return -1;
66 size_t
67 TAO_HTTP_Handler::byte_count () const
69 return bytecount_;
72 // Reader **************************************************
74 TAO_HTTP_Reader::TAO_HTTP_Reader (ACE_Message_Block * mb,
75 ACE_TCHAR *filename,
76 const char *request_prefix,
77 const char *request_suffix) :
78 TAO_HTTP_Handler (mb, filename),
79 request_prefix_ (request_prefix),
80 request_suffix_ (request_suffix)
84 int
85 TAO_HTTP_Reader::send_request ()
87 char mesg [MAX_HEADER_SIZE];
89 // Check to see if the request is too big
90 if (MAX_HEADER_SIZE < (std::strlen (request_prefix_)
91 + ACE_OS::strlen (filename_)
92 + std::strlen (request_suffix_) + 4))
93 TAOLIB_ERROR_RETURN((LM_ERROR,"TAO (%P|%t) - HTTP_Reader::send_request, request too large!"), -1);
95 // Create a message to send to the server requesting retrieval of the file
96 int const len = ACE_OS::sprintf (mesg, "%s %s %s", request_prefix_,
97 ACE_TEXT_ALWAYS_CHAR (filename_),
98 request_suffix_);
100 // Send the message to server
101 if (peer ().send_n (mesg, len) != len)
102 TAOLIB_ERROR_RETURN((LM_ERROR,"TAO (%P|%t) - HTTP_Reader::send_request, error sending request\n"), -1);
104 return 0;
108 TAO_HTTP_Reader::receive_reply ()
110 size_t num_recvd = 0;
111 char buf [MTU+1];
112 char *buf_ptr = nullptr;
113 size_t bytes_read = 0;
115 // Receive the first MTU bytes and strip the header off.
116 // Note that we assume that the header will fit into MTU bytes.
117 if (peer ().recv_n (buf, MTU, nullptr, &num_recvd) >= 0)
119 //Make sure that response type is 200 OK
120 if (ACE_OS::strstr (buf,"200 OK") == nullptr)
121 TAOLIB_ERROR_RETURN ((LM_ERROR,
122 "TAO (%P|%t) - HTTP_Reader::receive_reply, Response is not 200 OK\n" ), -1);
124 // Search for the header termination string "\r\n\r\n", or "\n\n". If
125 // found, move past it to get to the data portion.
126 if ((buf_ptr = ACE_OS::strstr (buf,"\r\n\r\n")) != nullptr)
127 buf_ptr += 4;
128 else if ((buf_ptr = ACE_OS::strstr (buf, "\n\n")) != nullptr) //for compatibility with JAWS
129 buf_ptr += 2;
130 else
131 buf_ptr = buf;
133 // Determine number of data bytes read. This is equal to the
134 // total bytes read minus number of header bytes.
135 bytes_read = num_recvd - (buf_ptr - buf);
137 else
139 TAOLIB_ERROR_RETURN ((LM_ERROR,
140 "TAO (%P|%t) - HTTP_Reader::receive_reply, error while reading header\n"), -1);
143 // ***************************************************************
144 // At this point, we have stripped off the header and are ready to
145 // process data. buf_ptr points to the data
147 ACE_Message_Block* temp = nullptr;
148 ACE_Message_Block* curr = this->mb_;
150 ACE_NEW_RETURN (temp,
151 ACE_Message_Block (bytes_read),
152 -1);
153 curr->cont (temp);
154 curr = curr->cont ();
156 // Copy over all the data bytes into our message buffer.
157 if (curr->copy (buf_ptr, bytes_read) == -1)
159 TAOLIB_ERROR_RETURN ((LM_ERROR, "TAO (%P|%t) - HTTP_Reader::receive_reply, error copying data into Message_Block\n"), -1);
162 // read the rest of the data into a number of ACE_Message_Blocks and
163 // chain them together in a link list fashion
164 num_recvd = 0;
168 if (curr->space () == 0)
170 ACE_NEW_RETURN (temp,
171 ACE_Message_Block (MTU),
172 -1);
173 curr->cont (temp);
174 curr = curr->cont ();
177 if (peer ().recv_n (curr->wr_ptr (), curr->space (), nullptr, &num_recvd) >= 0)
179 // Move the write pointer
180 curr->wr_ptr (num_recvd);
182 // Increment bytes_read
183 bytes_read += num_recvd;
185 else
187 TAOLIB_ERROR_RETURN ((LM_ERROR,
188 "TAO (%P|%t) - HTTP_Reader::receive_reply, Error while reading header\n"), -1);
190 } while (num_recvd != 0);
192 // Set the byte count to number of bytes received
193 this->bytecount_ = bytes_read;
195 return 0;
198 TAO_END_VERSIONED_NAMESPACE_DECL
200 #endif /* TAO_HAS_HTTP_PARSER == 1*/