myserver-commit
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[myserver-commit] [3067] Added the `Url' class.


From: Giuseppe Scrivano
Subject: [myserver-commit] [3067] Added the `Url' class.
Date: Sat, 02 May 2009 18:09:17 +0000

Revision: 3067
          http://svn.sv.gnu.org/viewvc/?view=rev&root=myserver&revision=3067
Author:   gscrivano
Date:     2009-05-02 18:09:16 +0000 (Sat, 02 May 2009)
Log Message:
-----------
Added the `Url' class.  It separes a "Uniform Resource Locator" string in its 
tokens.

Modified Paths:
--------------
    trunk/myserver/include/protocol/Makefile.am
    trunk/myserver/src/protocol/Makefile.am
    trunk/myserver/tests/Makefile.am
    trunk/myserver/tests/test_file.cpp

Added Paths:
-----------
    trunk/myserver/include/protocol/url.h
    trunk/myserver/src/protocol/url.cpp
    trunk/myserver/tests/test_url.cpp

Modified: trunk/myserver/include/protocol/Makefile.am
===================================================================
--- trunk/myserver/include/protocol/Makefile.am 2009-05-02 15:03:39 UTC (rev 
3066)
+++ trunk/myserver/include/protocol/Makefile.am 2009-05-02 18:09:16 UTC (rev 
3067)
@@ -1,4 +1,3 @@
 protocolincludedir=$(includedir)/myserver/include/protocol
-protocolinclude_HEADERS = protocol_buffer.h protocol.h protocols_manager.h
+protocolinclude_HEADERS = protocol_buffer.h protocol.h protocols_manager.h 
url.h
 SUBDIRS = control ftp http https
-

Added: trunk/myserver/include/protocol/url.h
===================================================================
--- trunk/myserver/include/protocol/url.h                               (rev 0)
+++ trunk/myserver/include/protocol/url.h       2009-05-02 18:09:16 UTC (rev 
3067)
@@ -0,0 +1,47 @@
+/* -*- mode: c++ -*- */
+/*
+MyServer
+Copyright (C) 2009 Free Software Foundation, Inc.
+This program is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 3 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program.  If not, see <http://www.gnu.org/licenses/>.
+*/
+
+#ifndef URL_H
+#define URL_H
+#include "stdafx.h"
+
+#include <string>
+
+using namespace std;
+
+class Url
+{
+public:
+  Url (string &, u_short port);
+  Url (const char*, u_short port);
+  string& getProtocol ();
+  u_short getPort ();
+  string& getResource ();
+  string& getHost ();
+  string& getQuery ();
+  string& getCredentials ();
+protected:
+  void parse (string &, u_short port);
+  string protocol;
+  string credentials;
+  string host;
+  string query;
+  u_short port;
+  string resource;
+};
+#endif

Modified: trunk/myserver/src/protocol/Makefile.am
===================================================================
--- trunk/myserver/src/protocol/Makefile.am     2009-05-02 15:03:39 UTC (rev 
3066)
+++ trunk/myserver/src/protocol/Makefile.am     2009-05-02 18:09:16 UTC (rev 
3067)
@@ -1,5 +1,5 @@
 lib_LIBRARIES = libprotocol.a
-libprotocol_a_SOURCES = protocol_buffer.cpp protocol.cpp protocols_manager.cpp
+libprotocol_a_SOURCES = protocol_buffer.cpp protocol.cpp protocols_manager.cpp 
url.cpp
 SUBDIRS = control ftp http https
 INCLUDES = $(all_includes)
 

Added: trunk/myserver/src/protocol/url.cpp
===================================================================
--- trunk/myserver/src/protocol/url.cpp                         (rev 0)
+++ trunk/myserver/src/protocol/url.cpp 2009-05-02 18:09:16 UTC (rev 3067)
@@ -0,0 +1,143 @@
+/*
+MyServer
+Copyright (C) 2009 Free Software Foundation, Inc.
+This program is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 3 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program.  If not, see <http://www.gnu.org/licenses/>.
+*/
+
+
+#include <include/protocol/url.h>
+
+
+/*!
+ *Build an URL object.
+ *\param url The url string.
+ *\param port the default port to use in case it is not specified.
+ */
+Url::Url (string &url, u_short port)
+{
+  parse (url, port);
+}
+
+/*!
+ *Build an URL object.
+ *\param url The url string.
+ *\param port the default port to use in case it is not specified.
+ */
+Url::Url (const char* url, u_short port)
+{
+  string urlStr (url);
+  parse (urlStr, port);
+}
+
+/*!
+ *Get the protocol part of the URL.
+ */
+string& Url::getProtocol ()
+{
+  return protocol;
+}
+
+
+/*!
+ *Get the query part of the URL.
+ */
+string& Url::getQuery ()
+{
+  return query;
+}
+
+/*!
+ *Get the credentials part of the URL.
+ */
+string& Url::getCredentials ()
+{
+  return credentials;
+}
+
+/*!
+ *Get the port part of the URL.
+ */
+u_short Url::getPort ()
+{
+  return port;
+}
+
+/*!
+ *Get the resource part of the URL.
+ */
+string& Url::getResource ()
+{
+  return resource;
+}
+
+/*!
+ *Get the host part of the URL.
+ */
+string& Url::getHost ()
+{
+  return host;
+}
+
+/*!
+ *Parse the URL.  Internal function.
+ */
+void Url::parse (string &url, u_short defPort)
+{
+  size_t portLoc;
+  size_t firstSlash;
+  size_t protoEnd = url.find ("://");
+  size_t credentialStart;
+  if (protoEnd == string::npos)
+    return;
+
+  protocol = url.substr (0, protoEnd);
+
+  portLoc = url.find (':', protoEnd + 3);
+  firstSlash = url.find ('/', portLoc == string::npos ? protoEnd + 3 :
+                                                        portLoc + 1);
+
+  credentialStart = url.find ('@', protoEnd + 3);
+  size_t offsetCredentials = credentialStart != string::npos  ? 
credentialStart + 1
+                                                              : protoEnd + 3;
+
+  if (credentialStart != string::npos)
+    credentials = url.substr (protoEnd + 3, offsetCredentials - protoEnd - 4);
+  else
+    credentials = "";
+
+  /* Check if the port is specified.  */
+  if (portLoc != string::npos)
+    {
+      host = url.substr (offsetCredentials, portLoc - offsetCredentials);
+      port = atoi (url.substr (portLoc + 1, firstSlash - portLoc - 1).c_str 
());
+    }
+  else
+    {
+      host = url.substr (offsetCredentials, firstSlash - offsetCredentials);
+      port = defPort;
+    }
+
+  size_t queryStart = url.find ('?', firstSlash + 1);
+
+  if (queryStart == string::npos)
+    {
+      resource = url.substr (firstSlash + 1);
+      query = "";
+    }
+  else
+    {
+      resource = url.substr (firstSlash + 1, queryStart - firstSlash - 1);
+      query = url.substr (queryStart + 1);
+    }
+}

Modified: trunk/myserver/tests/Makefile.am
===================================================================
--- trunk/myserver/tests/Makefile.am    2009-05-02 15:03:39 UTC (rev 3066)
+++ trunk/myserver/tests/Makefile.am    2009-05-02 18:09:16 UTC (rev 3067)
@@ -2,5 +2,14 @@
 #
 
 bin_PROGRAMS = tests_suite
-tests_suite_SOURCES = main.cpp test_connection.cpp test_file.cpp test_ftp.cpp 
test_log_manager.cpp test_mime_manager.cpp test_mutex.cpp 
test_security_domain.cpp test_validator.cpp test_auth_domain.cpp 
test_connections_scheduler.cpp test_gzip.cpp test_log_stream_factory.cpp 
test_pipe.cpp test_security_manager.cpp test_validator_factory.cpp 
test_base64.cpp test_file_stream.cpp test_hashmap.cpp test_md5.cpp 
test_recursive_mutex.cpp test_semaphore.cpp test_xml.cpp 
test_cached_file_buffer.cpp test_file_stream_creator.cpp test_homedir.cpp 
test_mem_buff.cpp test_regex.cpp test_socket_stream_creator.cpp 
test_cached_file.cpp test_files_utility.cpp test_http_request.cpp 
test_http_req_security_domain.cpp test_mem_stream.cpp test_safetime.cpp 
test_thread.cpp test_cached_file_factory.cpp test_filter_chain.cpp 
test_http_response.cpp test_multicast.cpp test_security_cache.cpp 
test_security_token.cpp test_socket.cpp test_socket_pair.cpp 
test_ssl_socket.cpp test_unix_socket.cpp test_utility.cpp  
test_xml_validator.cpp test_ip.cpp test_plugin_info.cpp test_fork_server.cpp
+tests_suite_SOURCES = main.cpp test_auth_domain.cpp test_base64.cpp 
test_cached_file_buffer.cpp test_cached_file.cpp \
+test_cached_file_factory.cpp test_connection.cpp 
test_connections_scheduler.cpp test_file.cpp test_file_stream.cpp \
+test_file_stream_creator.cpp test_files_utility.cpp test_filter_chain.cpp 
test_fork_server.cpp test_ftp.cpp test_gzip.cpp \
+test_hashmap.cpp test_homedir.cpp test_http_req_security_domain.cpp 
test_http_request.cpp test_http_response.cpp test_ip.cpp \
+test_log_manager.cpp test_log_stream_factory.cpp test_md5.cpp 
test_mem_buff.cpp test_mem_stream.cpp test_mime_manager.cpp \
+test_multicast.cpp test_mutex.cpp test_pipe.cpp test_plugin_info.cpp 
test_recursive_mutex.cpp test_regex.cpp test_safetime.cpp \
+test_security_cache.cpp test_security_domain.cpp test_security_manager.cpp 
test_security_token.cpp test_semaphore.cpp test_socket.cpp \
+test_socket_pair.cpp test_socket_stream_creator.cpp test_ssl_socket.cpp 
test_thread.cpp test_unix_socket.cpp test_url.cpp \
+test_utility.cpp test_validator.cpp test_validator_factory.cpp test_xml.cpp 
test_xml_validator.cpp
+
 tests_suite_LDADD = ../src/libmyserver.a $(CPPUNIT_LDFLAGS) $(PTHREAD_LIB) 
$(IDN_LIB) $(XNET_LIB) $(EVENT_LIB) $(DL_LIB) $(SSL_LIB) $(ZLIB_LIB) 
$(XML_LIBS) $(LDFLAGS)

Modified: trunk/myserver/tests/test_file.cpp
===================================================================
--- trunk/myserver/tests/test_file.cpp  2009-05-02 15:03:39 UTC (rev 3066)
+++ trunk/myserver/tests/test_file.cpp  2009-05-02 18:09:16 UTC (rev 3067)
@@ -70,7 +70,8 @@
     u_long nbw;
     u_long nbr;
     
-    CPPUNIT_ASSERT_EQUAL( tfile->openFile( fname.c_str(), 
File::MYSERVER_OPEN_WRITE | File::MYSERVER_OPEN_READ | 
File::MYSERVER_CREATE_ALWAYS ), 0 );
+    CPPUNIT_ASSERT_EQUAL( tfile->openFile( fname.c_str(), 
File::MYSERVER_OPEN_WRITE | 
+                                           File::MYSERVER_OPEN_READ | 
File::MYSERVER_CREATE_ALWAYS ), 0 );
     
     CPPUNIT_ASSERT_EQUAL( tfile->writeToFile ( buf, bufLen, &nbw ), 0 );
     

Added: trunk/myserver/tests/test_url.cpp
===================================================================
--- trunk/myserver/tests/test_url.cpp                           (rev 0)
+++ trunk/myserver/tests/test_url.cpp   2009-05-02 18:09:16 UTC (rev 3067)
@@ -0,0 +1,104 @@
+/*
+ MyServer
+ Copyright (C) 2008, 2009 Free Software Foundation, Inc.
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 3 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful, 
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <cppunit/CompilerOutputter.h>
+#include <cppunit/extensions/TestFactoryRegistry.h>
+#include <cppunit/ui/text/TestRunner.h>
+#include <cppunit/extensions/HelperMacros.h>
+
+#include <../include/protocol/url.h>
+
+#include <string.h>
+#include <unistd.h>
+#include <string>
+
+using namespace std;
+
+class TestUrl : public CppUnit::TestFixture
+{
+  CPPUNIT_TEST_SUITE( TestUrl );
+  CPPUNIT_TEST( testUrl );
+  CPPUNIT_TEST( testUrlWithCredentials );
+  CPPUNIT_TEST( testUrlWithQuery );
+  CPPUNIT_TEST( testUrlWithoutPort );
+  CPPUNIT_TEST_SUITE_END();
+
+public:
+  void setUp ( )
+  {
+
+  }
+
+  void tearDown ( )
+  {
+
+  }
+
+  void testUrlWithCredentials ()
+  {
+    const char *urlStr = "http://address@hidden:8080/what/i/want";;
+    Url url (urlStr, 80);
+
+    CPPUNIT_ASSERT (!url.getCredentials ().compare ("root"));
+    CPPUNIT_ASSERT (!url.getHost ().compare ("foo.bar"));
+    CPPUNIT_ASSERT (!url.getResource ().compare ("what/i/want"));
+    CPPUNIT_ASSERT (!url.getProtocol ().compare ("http"));
+    CPPUNIT_ASSERT (!url.getQuery ().compare (""));
+    CPPUNIT_ASSERT_EQUAL (url.getPort (), (u_short)8080);
+  }
+
+  void testUrlWithQuery ()
+  {
+    const char *urlStr = "http://foo.bar:8080/what/i/want?my_query";;
+    Url url (urlStr, 80);
+
+    CPPUNIT_ASSERT (!url.getCredentials ().compare (""));
+    CPPUNIT_ASSERT (!url.getHost ().compare ("foo.bar"));
+    CPPUNIT_ASSERT (!url.getResource ().compare ("what/i/want"));
+    CPPUNIT_ASSERT (!url.getProtocol ().compare ("http"));
+    CPPUNIT_ASSERT (!url.getQuery ().compare ("my_query"));
+    CPPUNIT_ASSERT_EQUAL (url.getPort (), (u_short)8080);
+  }
+
+  void testUrl ()
+  {
+    const char *urlStr = "http://foo.bar:8080/what/i/want";;
+    Url url (urlStr, 80);
+
+    CPPUNIT_ASSERT (!url.getCredentials ().compare (""));
+    CPPUNIT_ASSERT (!url.getHost ().compare ("foo.bar"));
+    CPPUNIT_ASSERT (!url.getResource ().compare ("what/i/want"));
+    CPPUNIT_ASSERT (!url.getProtocol ().compare ("http"));
+    CPPUNIT_ASSERT (!url.getQuery ().compare (""));
+    CPPUNIT_ASSERT_EQUAL (url.getPort (), (u_short)8080);
+  }
+
+  void testUrlWithoutPort ()
+  {
+    const char *urlStr = "http://foo.bar/what/i/want";;
+    Url url (urlStr, 80);
+
+    CPPUNIT_ASSERT (!url.getCredentials ().compare (""));
+    CPPUNIT_ASSERT (!url.getHost ().compare ("foo.bar"));
+    CPPUNIT_ASSERT (!url.getResource ().compare ("what/i/want"));
+    CPPUNIT_ASSERT (!url.getProtocol ().compare ("http"));
+    CPPUNIT_ASSERT_EQUAL (url.getPort (), (u_short)80);
+  }
+
+};
+
+CPPUNIT_TEST_SUITE_REGISTRATION( TestUrl );





reply via email to

[Prev in Thread] Current Thread [Next in Thread]