Updated logging to include the class/method so that it is more obvious where these...
[ACE_TAO.git] / TAO / tao / HTTP_Parser.cpp
blob6f5cca88d57b78a6485448a132e14d9a987d903c
1 #include "tao/HTTP_Parser.h"
3 #if (TAO_HAS_HTTP_PARSER == 1)
5 #include "tao/HTTP_Client.h"
6 #include "tao/ORB.h"
7 #include "tao/Object.h"
8 #include "tao/SystemException.h"
10 #include "ace/Read_Buffer.h"
11 #include "ace/Malloc_Base.h"
12 #include "ace/Log_Msg.h"
13 #include "ace/OS_NS_stdio.h"
14 #include "ace/OS_NS_string.h"
15 #include "ace/CORBA_macros.h"
17 static const ACE_TCHAR file_prefix[] = ACE_TEXT ("http:");
19 TAO_BEGIN_VERSIONED_NAMESPACE_DECL
21 bool
22 TAO_HTTP_Parser::match_prefix (const char *nior_string) const
24 ACE_TString ior_string = ACE_TEXT_CHAR_TO_TCHAR (nior_string);
25 return (ACE_OS::strncmp (ior_string.c_str (),
26 ::file_prefix,
27 sizeof (::file_prefix) - 1) == 0);
30 CORBA::Object_ptr
31 TAO_HTTP_Parser::parse_string (const char *nior,
32 CORBA::ORB_ptr orb)
34 // Skip the prefix, we know it is there because this method in only
35 // called if <match_prefix> returns 1.
36 ACE_TString ior = ACE_TEXT_CHAR_TO_TCHAR (nior);
37 const ACE_TCHAR *http_url =
38 ior.c_str () + sizeof (::file_prefix) + 1;
40 ACE_TCHAR *hostname = nullptr;
41 ACE_TCHAR *filename = nullptr;
42 const ACE_TCHAR *ptr = nullptr;
43 u_short port = 80;
45 if (http_url[0] == '/')
47 filename = ACE_OS::strdup (http_url);
49 else
51 ptr = ACE_OS::strstr (http_url, ACE_TEXT (":"));
52 if (ptr)
53 port = ACE_OS::atoi (ptr + 1);
54 else
55 ptr = ACE_OS::strstr (http_url, ACE_TEXT ("/"));
57 if(!ptr)
58 return nullptr;
59 else
61 size_t const host_len = ptr - http_url;
62 ACE_NEW_RETURN (hostname, ACE_TCHAR [host_len + 1], nullptr );
63 ACE_OS::strncpy (hostname, http_url, host_len);
64 hostname [host_len] = '\0';
65 ptr = ACE_OS::strstr (ptr, ACE_TEXT ("/"));
66 if (ptr)
68 filename = ACE_OS::strdup(ptr);
70 else
72 delete [] hostname;
73 return nullptr;
78 ACE_Message_Block* mb = nullptr;
79 ACE_NEW_THROW_EX (mb,
80 ACE_Message_Block (),
81 CORBA::INTERNAL ());
83 // Create a client
84 TAO_HTTP_Client client;
86 if (TAO_debug_level > 4)
88 TAOLIB_DEBUG ((LM_DEBUG,
89 ACE_TEXT ("TAO (%P|%t) - HTTP_Parser::parse_string, getting IOR from <%s> <%s> <%d>\n"),
90 hostname, filename, port));
93 // Open the client
94 if (client.open (filename,
95 hostname,
96 port) == -1)
98 client.close ();
99 return nullptr;
102 delete [] hostname;
103 ACE_OS::free (filename);
105 // Read from it
106 if (client.read (mb) <= 0)
108 client.close ();
109 return nullptr;
112 // We get multiple message blocks back, concatenate them to
113 // one large string
114 ACE_CString string;
115 for (ACE_Message_Block * curr = mb; curr != nullptr; curr = curr->cont ())
116 string += curr->rd_ptr();
118 return orb->string_to_object (string.c_str());
121 ACE_STATIC_SVC_DEFINE (TAO_HTTP_Parser,
122 ACE_TEXT ("HTTP_Parser"),
123 ACE_SVC_OBJ_T,
124 &ACE_SVC_NAME (TAO_HTTP_Parser),
125 ACE_Service_Type::DELETE_THIS |
126 ACE_Service_Type::DELETE_OBJ,
129 ACE_FACTORY_DEFINE (TAO, TAO_HTTP_Parser)
131 TAO_END_VERSIONED_NAMESPACE_DECL
134 #endif /* TAO_HAS_HTTP_PARSER == 1 */