00001
00002
00003 #include "tao/HTTP_Parser.h"
00004
00005 #if (TAO_HAS_HTTP_PARSER == 1)
00006
00007 #include "tao/HTTP_Client.h"
00008 #include "tao/ORB.h"
00009 #include "tao/Object.h"
00010 #include "tao/SystemException.h"
00011
00012 #include "ace/Read_Buffer.h"
00013 #include "ace/Malloc_Base.h"
00014 #include "ace/Log_Msg.h"
00015 #include "ace/OS_NS_stdio.h"
00016 #include "ace/OS_NS_string.h"
00017 #include "ace/CORBA_macros.h"
00018
00019 ACE_RCSID (tao,
00020 HTTP_Parser,
00021 "$Id: HTTP_Parser.cpp 79829 2007-10-23 12:39:52Z johnnyw $")
00022
00023 static const ACE_TCHAR file_prefix[] = ACE_TEXT ("http:");
00024
00025 TAO_BEGIN_VERSIONED_NAMESPACE_DECL
00026
00027 TAO_HTTP_Parser::~TAO_HTTP_Parser (void)
00028 {
00029 }
00030
00031
00032 bool
00033 TAO_HTTP_Parser::match_prefix (const char *nior_string) const
00034 {
00035 const ACE_TCHAR *ior_string = ACE_TEXT_CHAR_TO_TCHAR (nior_string);
00036 return (ACE_OS::strncmp (ior_string,
00037 ::file_prefix,
00038 sizeof (::file_prefix) - 1) == 0);
00039 }
00040
00041 CORBA::Object_ptr
00042 TAO_HTTP_Parser::parse_string (const char *nior,
00043 CORBA::ORB_ptr orb)
00044 {
00045
00046
00047 const ACE_TCHAR *ior = ACE_TEXT_CHAR_TO_TCHAR (nior);
00048 const ACE_TCHAR *http_url =
00049 ior + sizeof (::file_prefix) + 1;
00050
00051 ACE_TCHAR *hostname = 0;
00052 ACE_TCHAR *filename = 0;
00053 const ACE_TCHAR *ptr = 0;
00054 u_short port = 80;
00055
00056 if (http_url[0] == '/')
00057 {
00058 filename = ACE_OS::strdup (http_url);
00059 }
00060 else
00061 {
00062 ptr = ACE_OS::strstr (http_url, ACE_TEXT (":"));
00063 if (ptr)
00064 port = ACE_OS::atoi (ptr + 1);
00065 else
00066 ptr = ACE_OS::strstr (http_url, ACE_TEXT ("/"));
00067
00068 if(!ptr)
00069 return 0;
00070 else
00071 {
00072 size_t const host_len = ptr - http_url;
00073 ACE_NEW_RETURN (hostname, ACE_TCHAR [host_len + 1], 0 );
00074 ACE_OS::strncpy (hostname, http_url, host_len);
00075 hostname [host_len] = '\0';
00076 ptr = ACE_OS::strstr (ptr, ACE_TEXT ("/"));
00077 if (ptr)
00078 {
00079 filename = ACE_OS::strdup(ptr);
00080 }
00081 else
00082 return 0;
00083 }
00084 }
00085
00086 ACE_Message_Block* mb = 0;
00087 ACE_NEW_THROW_EX (mb,
00088 ACE_Message_Block (),
00089 CORBA::INTERNAL ());
00090
00091
00092 TAO_HTTP_Client client;
00093
00094 if (TAO_debug_level > 4)
00095 {
00096 ACE_DEBUG ((LM_DEBUG,
00097 ACE_TEXT ("TAO (%P|%t) - HTTP_Parser::parse_string, getting IOR from <%s> <%s> <%d>\n"),
00098 hostname, filename, port));
00099 }
00100
00101
00102 if (client.open (filename,
00103 hostname,
00104 port) == -1)
00105 {
00106 client.close ();
00107 return 0;
00108 }
00109
00110 delete [] hostname;
00111 ACE_OS::free (filename);
00112
00113
00114 if (client.read (mb) <= 0)
00115 {
00116 client.close ();
00117 return 0;
00118 }
00119
00120
00121
00122 ACE_CString string;
00123 for (ACE_Message_Block * curr = mb; curr != 0; curr = curr->cont ())
00124 string += curr->rd_ptr();
00125
00126 return orb->string_to_object (string.c_str());
00127 }
00128
00129 TAO_END_VERSIONED_NAMESPACE_DECL
00130
00131 ACE_STATIC_SVC_DEFINE (TAO_HTTP_Parser,
00132 ACE_TEXT ("HTTP_Parser"),
00133 ACE_SVC_OBJ_T,
00134 &ACE_SVC_NAME (TAO_HTTP_Parser),
00135 ACE_Service_Type::DELETE_THIS |
00136 ACE_Service_Type::DELETE_OBJ,
00137 0)
00138
00139 ACE_FACTORY_DEFINE (TAO, TAO_HTTP_Parser)
00140
00141 #endif