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"
11 TAO_BEGIN_VERSIONED_NAMESPACE_DECL
13 TAO_HTTP_Handler::TAO_HTTP_Handler () :
20 TAO_HTTP_Handler::TAO_HTTP_Handler (ACE_Message_Block
* mb
,
21 ACE_TCHAR
*filename
) :
23 filename_ (ACE_OS::strdup (filename
)),
28 TAO_HTTP_Handler::~TAO_HTTP_Handler ()
32 ACE_OS::free (this->filename_
);
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);
49 TAO_HTTP_Handler::close (u_long
)
55 TAO_HTTP_Handler::send_request ()
61 TAO_HTTP_Handler::receive_reply ()
67 TAO_HTTP_Handler::byte_count () const
72 // Reader **************************************************
74 TAO_HTTP_Reader::TAO_HTTP_Reader (ACE_Message_Block
* mb
,
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
)
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_
),
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);
108 TAO_HTTP_Reader::receive_reply ()
110 size_t num_recvd
= 0;
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)
128 else if ((buf_ptr
= ACE_OS::strstr (buf
, "\n\n")) != nullptr) //for compatibility with JAWS
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
);
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
),
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
168 if (curr
->space () == 0)
170 ACE_NEW_RETURN (temp
,
171 ACE_Message_Block (MTU
),
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
;
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
;
198 TAO_END_VERSIONED_NAMESPACE_DECL
200 #endif /* TAO_HAS_HTTP_PARSER == 1*/