1 #include "HTBP_ID_Requestor.h"
2 #include "HTBP_Environment.h"
4 #include "ace/OS_NS_stdio.h"
5 #include "ace/OS_NS_string.h"
6 #include "ace/SOCK_Stream.h"
7 #include "ace/SOCK_Connector.h"
10 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
12 ACE_TString
ACE::HTBP::ID_Requestor::htid_
;
13 ACE_SYNCH_MUTEX
ACE::HTBP::ID_Requestor::htid_lock_
;
15 ACE::HTBP::ID_Requestor::ID_Requestor (ACE::HTBP::Environment
*env
)
22 env
->get_htid_url(url_
);
24 env
->get_htid_via_proxy (via_proxy
);
25 if (via_proxy
&& env
->get_proxy_host (host_
) == 0)
26 env
->get_proxy_port (port_
);
31 ACE::HTBP::ID_Requestor::connect_to_server (ACE_SOCK_Stream
*cli_stream
)
33 if (port_
== 0 || host_
.length() == 0)
35 int host_start
= url_
.find (ACE_TEXT("http://")) + 7;
39 ACE_ERROR_RETURN ((LM_ERROR
,
40 ACE_TEXT("(%P|%t) ACE::HTBP::ID_Requestor::")
41 ACE_TEXT("connect_to_server: ")
42 ACE_TEXT("invalid URL: \"%s\"\n"),
45 port_sep
= url_
.find (ACE_TEXT(":"),(size_t)host_start
);
46 sep
= url_
.find (ACE_TEXT("/"),(size_t)host_start
);
47 if (sep
== -1 || sep
== host_start
+1)
48 ACE_ERROR_RETURN ((LM_ERROR
,
49 ACE_TEXT("(%P|%t) ACE::HTBP::ID_Requestor::")
50 ACE_TEXT("connect_to_server: ")
51 ACE_TEXT("invalid URL: \"%s\"\n"),
57 port_
= 80; // should be a default?
59 host_
= url_
.substr(host_start
,port_sep
- host_start
);
62 ACE_INET_Addr
remote_addr (static_cast<u_short
> (port_
), host_
.c_str());
63 ACE_SOCK_Connector con
;
64 if (con
.connect (*cli_stream
,
66 ACE_ERROR_RETURN ((LM_ERROR
,
67 ACE_TEXT("(%P|%t) ACE::HTBP::ID_Requestor::")
68 ACE_TEXT("connect_to_server: ")
70 ACE_TEXT("socket connect")),
76 ACE::HTBP::ID_Requestor::send_request (ACE_SOCK_Stream
*cli_stream
)
79 ACE_NEW_RETURN (buffer
, char[this->url_
.length()+16],-1);
80 std::unique_ptr
<char[]> guard (buffer
);
81 ACE_OS::sprintf (buffer
,"GET %s HTTP/1.0\n\n",
82 ACE_TEXT_ALWAYS_CHAR(url_
.c_str()));
83 int result
= cli_stream
->send_n (buffer
,ACE_OS::strlen(buffer
));
85 ACE_ERROR_RETURN ((LM_ERROR
,
86 ACE_TEXT("(%P|%t) ACE::HTBP::ID_Requestor::")
87 ACE_TEXT("send_request %p\n"),
88 ACE_TEXT("socket send")), -1);
93 ACE::HTBP::ID_Requestor::get_HTID ()
95 if (ACE::HTBP::ID_Requestor::htid_
.length() != 0)
96 return ACE::HTBP::ID_Requestor::htid_
.rep();
98 ACE_GUARD_RETURN (ACE_SYNCH_MUTEX
, guard
, ACE::HTBP::ID_Requestor::htid_lock_
, 0);
100 if (ACE::HTBP::ID_Requestor::htid_
.length() != 0)
101 return ACE::HTBP::ID_Requestor::htid_
.rep();
103 ACE_SOCK_Stream cli_stream
;
104 ACE_TCHAR
* htid
= 0;
106 if (this->url_
.length() == 0 ||
107 this->connect_to_server (&cli_stream
) == -1 ||
108 this->send_request (&cli_stream
) == -1)
110 ACE_Utils::UUID_Generator gen
;
111 ACE_Utils::UUID
*uuid
= gen
.generate_UUID ();
112 const ACE_CString
*uuidstr
= uuid
->to_string();
113 ACE::HTBP::ID_Requestor::htid_
= ACE_TEXT_CHAR_TO_TCHAR (uuidstr
->c_str());
115 return ACE::HTBP::ID_Requestor::htid_
.rep();
118 ssize_t result
= cli_stream
.recvv (&recv_buf
);
123 ACE_CString
answer ((char *)recv_buf
.iov_base
,recv_buf
.iov_len
);
124 ACE_CString::size_type start
= answer
.rfind (ACE_TEXT('\n'));
125 if (start
== ACE_CString::npos
)
129 ACE::HTBP::ID_Requestor::htid_
= ACE_TEXT_CHAR_TO_TCHAR(answer
.substr (start
).c_str());
130 htid
= ACE::HTBP::ID_Requestor::htid_
.rep();
135 ACE_END_VERSIONED_NAMESPACE_DECL