r3307 - in trunk/libmnetutil: include/libmnetutil source

mikaelsv at minisip.org mikaelsv at minisip.org
Wed Jun 13 09:55:57 CEST 2007


Author: mikaelsv
Date: 2007-06-13 09:55:57 +0200 (Wed, 13 Jun 2007)
New Revision: 3307

Modified:
   trunk/libmnetutil/include/libmnetutil/Downloader.h
   trunk/libmnetutil/include/libmnetutil/HttpDownloader.h
   trunk/libmnetutil/include/libmnetutil/LdapConnection.h
   trunk/libmnetutil/include/libmnetutil/LdapCredentials.h
   trunk/libmnetutil/include/libmnetutil/LdapDirectoryLocator.h
   trunk/libmnetutil/include/libmnetutil/LdapEntry.h
   trunk/libmnetutil/include/libmnetutil/LdapException.h
   trunk/libmnetutil/source/Downloader.cxx
   trunk/libmnetutil/source/HttpDownloader.cxx
   trunk/libmnetutil/source/LdapEntry.cxx
Log:
* Some documentation of Downloader
* Added length attribute to Downloader.getBytes(int*)
* Removed "using namespace std" in the LDAP files


Modified: trunk/libmnetutil/include/libmnetutil/Downloader.h
===================================================================
--- trunk/libmnetutil/include/libmnetutil/Downloader.h	2007-06-12 19:57:56 UTC (rev 3306)
+++ trunk/libmnetutil/include/libmnetutil/Downloader.h	2007-06-13 07:55:57 UTC (rev 3307)
@@ -19,6 +19,8 @@
 #ifndef DOWNLOADER_H
 #define DOWNLOADER_H
 
+#include <libmnetutil/libmnetutil_config.h>
+
 #include <libmutil/MemObject.h>
 #include <string>
 
@@ -54,7 +56,7 @@
 		 * retrieving the remote document as a string (this would not
 		 * work well with binary data).
 		 */
-		virtual const char* getChars() = 0;
+		virtual char* getChars(int *length) = 0;
 
 };
 

Modified: trunk/libmnetutil/include/libmnetutil/HttpDownloader.h
===================================================================
--- trunk/libmnetutil/include/libmnetutil/HttpDownloader.h	2007-06-12 19:57:56 UTC (rev 3306)
+++ trunk/libmnetutil/include/libmnetutil/HttpDownloader.h	2007-06-13 07:55:57 UTC (rev 3307)
@@ -19,6 +19,8 @@
 #ifndef _HTTPDOWNLOAD_H_
 #define _HTTPDOWNLOAD_H_
 
+#include<libmnetutil/libmnetutil_config.h>
+
 #include <libmutil/MemObject.h>
 #include <libmnetutil/StreamSocket.h>
 #include <libmnetutil/TCPSocket.h>
@@ -27,8 +29,6 @@
 #include <vector>
 #include <map>
 
-#define HTTP_METHOD_1_0 			"HTTP/1.0"
-
 #define HTTP_RESPONSECODE_OK 			200
 #define HTTP_RESPONSECODE_NOTFOUND 		404
 #define HTTP_RESPONSECODE_MOVEDPERMANENTLY 	301
@@ -44,8 +44,6 @@
 #define HTTP_HEADER_CONTENTLENGTH 		"Content-Length"
 #define HTTP_HEADER_LASTMODIFIED 		"Last-Modified"
 
-#define HTTP_HEADER_CRLF			"\r\n"
-
 /**
  * This class is a very simple HTTP user agent that fetches web pages using the HTTP 1.0 (not 1.1) protocol.
  *
@@ -57,11 +55,23 @@
  *  - Only handles HTTP (not HTTPS)
  *
  * Usage:
- *  # Initialize the class instance by calling the constructor
- *  # Call either downloadToFile(string), downloadToString() or requestHeaders()
- *  # Work with the document using the functions getHeader(string), getBody() and getResponseCode()
+ *  - Initialize the class instance by calling the constructor
+ *  - Call either fetch(int &length), downloadToFile(std::string), downloadToString() or downloadHeaders()
+ *  - Work with the document using the functions getHeader(std::string), getBody() and getResponseCode()
  *
- * Constructors and descructors are defined in HEADER FILE!
+ * This small example shows how to use the class:
+ * @code
+const string url("http://localhost/apache2-default/");
+
+MRef<Downloader*> d = Downloader::create(url);
+if (!d.isNull()) {
+	int len;
+	char* res = d->getChars(&len);
+	string page(res, len);
+	cout << page;
+	cout << "Downloaded " << len << " bytes." << endl;
+}
+ * @endcode
  *
  * @author	Mikael Svensson
  */
@@ -77,7 +87,7 @@
 		/**
 		 * This constructor is useful when tunneling HTTP over other protocols than pure TCP.
 		 *
-		 * Since the constructor HttpDownload(string) opens up an new TCP connection directly it is not
+		 * Since the constructor HttpDownload(std::string) opens up an new TCP connection directly it is not
 		 * useful when encryption is needed (e.g. when using the TlsSrpSocket). In these cases
 		 * a StreamSocket object can be created outside the HttpDownloader object and passed to this
 		 * constructor for use by the download functions later on.
@@ -93,14 +103,14 @@
 		 */
 		virtual ~HttpDownloader();
 
-		const char*	getChars();
+		char*	getChars(int *length);
 		/**
 		 * Fetch remote file and save as file on local computer.
 		 */
 		bool 	downloadToFile(std::string filename);
 
 		/**
-		 * Fetch remote file and return it as a single string
+		 * Fetch remote file and return it as a single std::string
 		 */
 		std::string 	downloadToString();
 
@@ -180,18 +190,19 @@
 		 * @param	data		String to be split
 		 * @param	token		Separator
 		 * @param	res		The string vector where the strings found will be stored
-		 * @param	maxChars	The maximum number of characters of data that will be scanned (-1 disables feature and entire string is scanned).
+		 * @param	maxChars	The maximum number of characters of data that will be scanned (-1 disables feature and entire std::string is scanned).
 		 * @author	Erik Ehrlund
 		 * @author	Mikael Svensson
 		 */
 		void	split(std::string data, std::string token, std::vector<std::string> &res, int maxChars = -1); // Copyright Erik Ehrlund
 
 		/**
-		 * This function removes any spaces, line feeds, carrige returns and tabs from the beginning and end of the specified string.
+		 * This function removes any spaces, line feeds, carrige returns and tabs from the beginning and end of the specified std::string.
 		 *
 		 * @deprecated	Use the very similar trim() from libmutil/stringutils.h instead.
 		 */
 		std::string	trim(std::string s);
+
 		std::string	buildRequestString(std::string method, std::string file);
 };
 

Modified: trunk/libmnetutil/include/libmnetutil/LdapConnection.h
===================================================================
--- trunk/libmnetutil/include/libmnetutil/LdapConnection.h	2007-06-12 19:57:56 UTC (rev 3306)
+++ trunk/libmnetutil/include/libmnetutil/LdapConnection.h	2007-06-13 07:55:57 UTC (rev 3307)
@@ -19,6 +19,8 @@
 #ifndef LDAPCONNECTION_H_
 #define LDAPCONNECTION_H_
 
+#include<libmnetutil/libmnetutil_config.h>
+
 #include <ldap.h>
 
 #include <libmutil/MemObject.h>

Modified: trunk/libmnetutil/include/libmnetutil/LdapCredentials.h
===================================================================
--- trunk/libmnetutil/include/libmnetutil/LdapCredentials.h	2007-06-12 19:57:56 UTC (rev 3306)
+++ trunk/libmnetutil/include/libmnetutil/LdapCredentials.h	2007-06-13 07:55:57 UTC (rev 3307)
@@ -19,6 +19,8 @@
 #ifndef LDAPCREDENTIALS_H_
 #define LDAPCREDENTIALS_H_
 
+#include<libmnetutil/libmnetutil_config.h>
+
 #include <libmutil/MemObject.h>
 #include <libmcrypto/cert.h>
 

Modified: trunk/libmnetutil/include/libmnetutil/LdapDirectoryLocator.h
===================================================================
--- trunk/libmnetutil/include/libmnetutil/LdapDirectoryLocator.h	2007-06-12 19:57:56 UTC (rev 3306)
+++ trunk/libmnetutil/include/libmnetutil/LdapDirectoryLocator.h	2007-06-13 07:55:57 UTC (rev 3307)
@@ -19,6 +19,8 @@
 #ifndef LDAPDIRECTORYLOCATOR_H_
 #define LDAPDIRECTORYLOCATOR_H_
 
+#include<libmnetutil/libmnetutil_config.h>
+
 #include <libmutil/MemObject.h>
 
 /**

Modified: trunk/libmnetutil/include/libmnetutil/LdapEntry.h
===================================================================
--- trunk/libmnetutil/include/libmnetutil/LdapEntry.h	2007-06-12 19:57:56 UTC (rev 3306)
+++ trunk/libmnetutil/include/libmnetutil/LdapEntry.h	2007-06-13 07:55:57 UTC (rev 3307)
@@ -19,10 +19,13 @@
 #ifndef LDAPENTRY_H_
 #define LDAPENTRY_H_
 
+#include<libmnetutil/libmnetutil_config.h>
+
 #include <ldap.h>
 #include <lber.h>
 #include <libmnetutil/LdapException.h>
 #include <libmutil/MemObject.h>
+#include <libmutil/stringutils.h>
 
 #include <string>
 #include <map>

Modified: trunk/libmnetutil/include/libmnetutil/LdapException.h
===================================================================
--- trunk/libmnetutil/include/libmnetutil/LdapException.h	2007-06-12 19:57:56 UTC (rev 3306)
+++ trunk/libmnetutil/include/libmnetutil/LdapException.h	2007-06-13 07:55:57 UTC (rev 3307)
@@ -19,6 +19,8 @@
 #ifndef LDAPEXCEPTION_H_
 #define LDAPEXCEPTION_H_
 
+#include<libmnetutil/libmnetutil_config.h>
+
 #include <libmutil/Exception.h>
 #include <libmnetutil/libmnetutil_config.h>
 #include <string>

Modified: trunk/libmnetutil/source/Downloader.cxx
===================================================================
--- trunk/libmnetutil/source/Downloader.cxx	2007-06-12 19:57:56 UTC (rev 3306)
+++ trunk/libmnetutil/source/Downloader.cxx	2007-06-13 07:55:57 UTC (rev 3307)
@@ -2,12 +2,10 @@
 #include <libmnetutil/HttpDownloader.h>
 #include <string>
 
-using namespace std;
-
-MRef<Downloader*> Downloader::create(string const uri) {
+MRef<Downloader*> Downloader::create(std::string const uri) {
 	int pos = uri.find("://");
-	if (string::npos != pos) {
-		string protocol = uri.substr(0, pos);
+	if (std::string::npos != pos) {
+		std::string protocol = uri.substr(0, pos);
 		if (protocol == "http")
 			return MRef<Downloader*>(dynamic_cast<Downloader*>(new HttpDownloader(uri)));
 	}

Modified: trunk/libmnetutil/source/HttpDownloader.cxx
===================================================================
--- trunk/libmnetutil/source/HttpDownloader.cxx	2007-06-12 19:57:56 UTC (rev 3306)
+++ trunk/libmnetutil/source/HttpDownloader.cxx	2007-06-13 07:55:57 UTC (rev 3307)
@@ -17,9 +17,10 @@
  */
 #define BUFFERSIZE 4096
 
-using namespace std;
+#define HTTP_METHOD_1_0                         "HTTP/1.0"
+#define HTTP_HEADER_CRLF                        "\r\n"
 
-HttpDownloader::HttpDownloader(string url) : url (url), remotePort(80), respCode(-1), followRedirect(true), sock(NULL), internalSocketObject(false) {
+HttpDownloader::HttpDownloader(std::string url) : url (url), remotePort(80), respCode(-1), followRedirect(true), sock(NULL), internalSocketObject(false) {
 	parseUrl();
 	if (remotePort > 0 && remoteHostname != "") {
 		sock = new TCPSocket(remoteHostname, remotePort);
@@ -27,7 +28,7 @@
 	}
 }
 
-HttpDownloader::HttpDownloader(string url, StreamSocket * sock): url (url), remotePort(80), respCode(-1), followRedirect(true), sock(sock), internalSocketObject(false) {
+HttpDownloader::HttpDownloader(std::string url, StreamSocket * sock): url (url), remotePort(80), respCode(-1), followRedirect(true), sock(sock), internalSocketObject(false) {
 	parseUrl();
 }
 
@@ -36,25 +37,28 @@
 		delete sock;
 }
 
-const char* HttpDownloader::getChars() {
+char* HttpDownloader::getChars(int *length) {
 	int tries = 3;
 	while (tries) {
-		ostringstream body;
+		std::ostringstream body;
 		int fetchRes = fetch(buildRequestString("GET ", remoteFile), body);
 		if (fetchRes == HTTP_RESPONSECODE_MOVEDPERMANENTLY || respCode == HTTP_RESPONSECODE_MOVEDTEMPORARILY) {
 			url = getHeader("Location");
 			parseUrl();
 			if (!followRedirect) {
-				return "";
+				return NULL;
 			}
 		} else if (fetchRes == HTTP_RESPONSECODE_OK) {
-			return body.str().c_str();
+			*length = body.str().length();
+			char* res = new char[*length];
+			memcpy(res, body.str().c_str(), *length);
+			return res;
 		} else {
-			return "";
+			return NULL;
 		}
 		tries--;
 	}
-	return "";
+	return NULL;
 }
 
 
@@ -70,54 +74,24 @@
 	return followRedirect;
 }
 
-int HttpDownloader::fetch(string request, ostream & bodyStream) {
+int HttpDownloader::fetch(std::string request, std::ostream & bodyStream) {
 
-	cerr << "(FETCH)" << endl;
-
 	if (sock == NULL) {
 		// TODO: Error check for socket object
 	}
 
-	stringstream headerStream;
+	std::stringstream headerStream;
 	int32_t bytesWritten = 0, bytesRead = 0;
-// 	struct sockaddr_in remoteAddr;
-	/*
-	This is what the hostent struct looks like:
 
-	struct  hostent
-	{
-	char    *h_name;        // official name of host
-	char    **h_aliases;    // alias list
-	int     h_addrtype;     // host address type
-	int     h_length;       // length of address
-	char    **h_addr_list;  // list of addresses from name server
-	#define h_addr  h_addr_list[0]  // address, for backward compatiblity
-	};
-	*/
-// 	struct hostent *remoteHost;
-
 	/* Buffer for holding data read from the network stream */
 	char buffer[BUFFERSIZE];
 	memset(buffer, 0, sizeof(buffer)); // Zero out the buffer used when recieving data
 
-	//memset(remoteAddr, 0, sizeof(remoteAddr)); // Zero the data structure containing information about the remote host
 
-	/* Fetch information about the remote host */
-// 	remoteHost = gethostbyname(remoteHostname.c_str()); // Use host name stored in class instance
-// 	if (remoteHost == NULL) {
-// 		cerr << "Error: Could not resolve host name" << endl;
-// 		return false;
-// 	}
-// 	remoteAddr.sin_family = AF_INET;
-// 	remoteAddr.sin_port = htons(remotePort); // Use port number stored in class instance
-
-	// Copy information about remote host's IP address from one structure to another (to the one used by "connect()")
-// 	bcopy((char *)remoteHost->h_addr, (char *)&remoteAddr.sin_addr.s_addr, sizeof(remoteHost->h_length));
-
 	/* Send request */
 	bytesWritten = sock->write(request.c_str(), request.length());
 	if (bytesWritten < 0) {
-		cerr << "Error: Could not send request" << endl;
+		//cerr << "Error: Could not send request" << endl;
 		return false;
 	} else if (bytesWritten < request.length()) {
 		return false;
@@ -177,7 +151,7 @@
 		sock->close();
 
 	if (bytesRead < 0) {
-		cerr << "Error: Could not receive response" << endl;
+		//cerr << "Error: Could not receive response" << endl;
 		return 0;
 	}
 
@@ -188,12 +162,12 @@
 	size_t pos = 0;
 	size_t lastPos = 0;
 	// Find protocol
-	if ((pos = url.find("://", 0)) != string::npos) {
+	if ((pos = url.find("://", 0)) != std::string::npos) {
 		remoteProtocol = url.substr(lastPos, pos - lastPos);
 		lastPos = pos + 3;
 	}
 	// Find host (and possibly file on remote host, e.g. "index.html")
-	if ((pos = url.find("/", lastPos)) != string::npos) {
+	if ((pos = url.find("/", lastPos)) != std::string::npos) {
 		// At least the root of the webserver should be fetched (e.g. "http://www.sunet.se/", but more likely "http://www.sunet.se/index.html")
 		remoteHostname = url.substr(lastPos, pos - lastPos);
 		lastPos = pos;
@@ -203,19 +177,19 @@
 		remoteHostname = url.substr(lastPos);
 	}
 	// Find remote port
-	if ((pos = remoteHostname.find(":", 0)) != string::npos) {
+	if ((pos = remoteHostname.find(":", 0)) != std::string::npos) {
 		remoteHostname = remoteHostname.substr(0, pos);
 		remotePort = atoi(remoteHostname.substr(pos + 1).c_str());
 	}
 }
 
-void HttpDownloader::split(string data, string token, vector<string> &res, int maxChars)
+void HttpDownloader::split(std::string data, std::string token, std::vector<std::string> &res, int maxChars)
 {
 	int count = 0;
 	size_t lastpos = 0;
 	int tokenlen = token.length();
 	size_t pos = data.find(token,lastpos);
-	while(string::npos != pos && ((maxChars > 0 && pos < maxChars) || maxChars <= 0))
+	while(std::string::npos != pos && ((maxChars > 0 && pos < maxChars) || maxChars <= 0))
 	{
 		count = pos - lastpos;
 		res.push_back(data.substr(lastpos,count));
@@ -232,16 +206,16 @@
 	}
 }
 
-string HttpDownloader::trim(string s) {
+std::string HttpDownloader::trim(std::string s) {
 	size_t trimLeftPos = s.find_first_not_of(" \n\t\r");
 	size_t trimRightPos = s.find_last_not_of(" \n\t\r");
 	size_t pos = 0;
 	int len = 0;
 
-	if (trimLeftPos != string::npos)
+	if (trimLeftPos != std::string::npos)
 		pos = trimLeftPos;
 
-	if (trimRightPos != string::npos)
+	if (trimRightPos != std::string::npos)
 		len = trimRightPos + 1 - pos;
 	else
 		len = s.length() - pos;
@@ -251,7 +225,7 @@
 bool HttpDownloader::downloadHeaders() {
 	int tries = 3;
 	while (tries) {
-		ostringstream body;
+		std::ostringstream body;
 		int fetchRes = fetch(buildRequestString("HEAD ", remoteFile), body);
 		if (fetchRes == HTTP_RESPONSECODE_MOVEDPERMANENTLY || respCode == HTTP_RESPONSECODE_MOVEDTEMPORARILY) {
 			url = getHeader("Location");
@@ -269,11 +243,11 @@
 	return false;
 }
 
-string HttpDownloader::downloadToString() {
+std::string HttpDownloader::downloadToString() {
 
 	int tries = 3;
 	while (tries) {
-		ostringstream body;
+		std::ostringstream body;
 		int fetchRes = fetch(buildRequestString("GET ", remoteFile), body);
 		if (fetchRes == HTTP_RESPONSECODE_MOVEDPERMANENTLY || respCode == HTTP_RESPONSECODE_MOVEDTEMPORARILY) {
 			url = getHeader("Location");
@@ -290,12 +264,12 @@
 	}
 	return "";
 }
-bool HttpDownloader::downloadToFile(string filename) {
+bool HttpDownloader::downloadToFile(std::string filename) {
 
 	int tries = 3;
 	while (tries) {
 
-		ofstream file(filename.c_str(), ios_base::out | ios_base::trunc);
+		std::ofstream file(filename.c_str(), std::ios_base::out | std::ios_base::trunc);
 		if (!file) {
 			return false;
 		}
@@ -317,10 +291,10 @@
 	return false;
 }
 
-int HttpDownloader::parseHeaders(stringstream & headers) {
-	vector<string> lines;
-	vector<string> initialLine;
-	string bodySep = HTTP_HEADER_CRLF;
+int HttpDownloader::parseHeaders(std::stringstream & headers) {
+	std::vector<std::string> lines;
+	std::vector<std::string> initialLine;
+	std::string bodySep = HTTP_HEADER_CRLF;
 	bodySep += HTTP_HEADER_CRLF;
 
 	split(headers.str(), HTTP_HEADER_CRLF, lines);
@@ -335,26 +309,26 @@
 	return respCode;
 }
 
-void HttpDownloader::parseHeader(string line) {
+void HttpDownloader::parseHeader(std::string line) {
 	size_t pos = line.find(':');
-	if (pos != string::npos) {
-		cout << "Found header: [" << line.substr(0, pos) << " = " << trim(line.substr(pos+1)) << "]" << endl;
+	if (pos != std::string::npos) {
+		//cout << "Found header: [" << line.substr(0, pos) << " = " << trim(line.substr(pos+1)) << "]" << endl;
 		headers[line.substr(0, pos)] = trim(line.substr(pos+1));
 	} else {
-		cerr << "ERROR: Header \"" << line << "\" is not valid!" << endl;
+		//cerr << "ERROR: Header \"" << line << "\" is not valid!" << endl;
 	}
 }
 
-string HttpDownloader::getHeader(string header) {
-	map<string, string>::iterator iter = headers.find(header);
+std::string HttpDownloader::getHeader(std::string header) {
+	std::map<std::string, std::string>::iterator iter = headers.find(header);
 	if (iter != headers.end()) {
 		return iter->second;
 	} else {
 		return "";
 	}
 }
-string HttpDownloader::buildRequestString(string method, string file) {
-	string res = method + " " + file + " " + HTTP_METHOD_1_0;
+std::string HttpDownloader::buildRequestString(std::string method, std::string file) {
+	std::string res = method + " " + file + " " + HTTP_METHOD_1_0;
 	res.append(HTTP_HEADER_CRLF);
 
 	res.append(HTTP_HEADER_FROM);

Modified: trunk/libmnetutil/source/LdapEntry.cxx
===================================================================
--- trunk/libmnetutil/source/LdapEntry.cxx	2007-06-12 19:57:56 UTC (rev 3306)
+++ trunk/libmnetutil/source/LdapEntry.cxx	2007-06-13 07:55:57 UTC (rev 3307)
@@ -1,5 +1,4 @@
 #include <libmnetutil/LdapEntry.h>
-#include <libmutil/stringutils.h>
 
 LdapEntry::LdapEntry(LDAP* ld, LDAPMessage* entry) {
 	BerElement* ber;



More information about the Minisip-devel mailing list