Revert "Use a variable on the stack to not have a temporary in the call"
[ACE_TAO.git] / ACE / protocols / ace / HTBP / HTBP_ID_Requestor.cpp
blob9171492ce73ecf7926c7aea7b9b86ea6f7b44fad
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"
8 #include "ace/UUID.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)
16 : port_ (0),
17 host_ (),
18 url_()
20 if (env)
22 env->get_htid_url(url_);
23 int via_proxy = 0;
24 env->get_htid_via_proxy (via_proxy);
25 if (via_proxy && env->get_proxy_host (host_) == 0)
26 env->get_proxy_port (port_);
30 int
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;
36 int port_sep = 0;
37 int sep = 0;
38 if (host_start == -1)
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"),
43 url_.c_str()),
44 -1);
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"),
52 url_.c_str()),
53 -1);
54 if (port_sep == -1)
56 port_sep = sep;
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,
65 remote_addr) == -1)
66 ACE_ERROR_RETURN ((LM_ERROR,
67 ACE_TEXT("(%P|%t) ACE::HTBP::ID_Requestor::")
68 ACE_TEXT("connect_to_server: ")
69 ACE_TEXT("%p\n"),
70 ACE_TEXT("socket connect")),
71 -1);
72 return 0;
75 int
76 ACE::HTBP::ID_Requestor::send_request (ACE_SOCK_Stream *cli_stream)
78 char *buffer;
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));
84 if (result == -1)
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);
89 return 0;
92 ACE_TCHAR *
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());
114 delete uuid;
115 return ACE::HTBP::ID_Requestor::htid_.rep();
117 iovec recv_buf;
118 ssize_t result = cli_stream.recvv (&recv_buf);
119 cli_stream.close();
121 if (result > 0)
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)
126 start = 0;
127 else
128 start++;
129 ACE::HTBP::ID_Requestor::htid_ = ACE_TEXT_CHAR_TO_TCHAR(answer.substr (start).c_str());
130 htid = ACE::HTBP::ID_Requestor::htid_.rep();
132 return htid;
135 ACE_END_VERSIONED_NAMESPACE_DECL