r3306 - in trunk/libmnetutil: include/libmnetutil source

mikma at minisip.org mikma at minisip.org
Tue Jun 12 21:57:56 CEST 2007


Author: mikma
Date: 2007-06-12 21:57:56 +0200 (Tue, 12 Jun 2007)
New Revision: 3306

Modified:
   trunk/libmnetutil/include/libmnetutil/Downloader.h
   trunk/libmnetutil/include/libmnetutil/HttpDownloader.h
   trunk/libmnetutil/source/HttpDownloader.cxx
Log:
Fix Win32 compilation


Modified: trunk/libmnetutil/include/libmnetutil/Downloader.h
===================================================================
--- trunk/libmnetutil/include/libmnetutil/Downloader.h	2007-06-12 19:37:44 UTC (rev 3305)
+++ trunk/libmnetutil/include/libmnetutil/Downloader.h	2007-06-12 19:57:56 UTC (rev 3306)
@@ -22,8 +22,6 @@
 #include <libmutil/MemObject.h>
 #include <string>
 
-using namespace std;
-
 /**
  * Creates objects for downloading documents specified by and URI.
  *
@@ -46,7 +44,7 @@
 		 *
 		 * @param	uri	Full URI for a remote document (e.g. "http://www.kth.se/index.html")
 		 */
-		static MRef<Downloader*> create(string const uri);
+		static MRef<Downloader*> create(std::string const uri);
 
 		virtual std::string getMemObjectType() const {return "Downloader";};
 

Modified: trunk/libmnetutil/include/libmnetutil/HttpDownloader.h
===================================================================
--- trunk/libmnetutil/include/libmnetutil/HttpDownloader.h	2007-06-12 19:37:44 UTC (rev 3305)
+++ trunk/libmnetutil/include/libmnetutil/HttpDownloader.h	2007-06-12 19:57:56 UTC (rev 3306)
@@ -46,8 +46,6 @@
 
 #define HTTP_HEADER_CRLF			"\r\n"
 
-using namespace std;
-
 /**
  * This class is a very simple HTTP user agent that fetches web pages using the HTTP 1.0 (not 1.1) protocol.
  *
@@ -74,13 +72,7 @@
 		 *
 		 * @param	url	File/document to fetch.
 		 */
-		HttpDownloader(string url) : url (url), remotePort(80), respCode(-1), followRedirect(true), sock(NULL), internalSocketObject(false) {
-			parseUrl();
-			if (remotePort > 0 && remoteHostname != "") {
-				sock = new TCPSocket(remoteHostname, remotePort);
-				internalSocketObject = true;
-			}
-		}
+		HttpDownloader(std::string url);
 
 		/**
 		 * This constructor is useful when tunneling HTTP over other protocols than pure TCP.
@@ -94,29 +86,23 @@
 		 * @param	url	File/document to fetch.
 		 * @param	sock	Pre-existing socket to use for communicating with HTTP server.
 		 */
-		HttpDownloader(string url, StreamSocket * sock): url (url), remotePort(80), respCode(-1), followRedirect(true), sock(sock), internalSocketObject(false) {
-			parseUrl();
-		}
+		HttpDownloader(std::string url, StreamSocket * sock);
 
 		/**
 		 * The default constructor deallocates memory, if allocated.
 		 */
-		~HttpDownloader() {
-			if (internalSocketObject)
-				delete sock;
-		}
+		virtual ~HttpDownloader();
 
-
 		const char*	getChars();
 		/**
 		 * Fetch remote file and save as file on local computer.
 		 */
-		bool 	downloadToFile(string filename);
+		bool 	downloadToFile(std::string filename);
 
 		/**
 		 * Fetch remote file and return it as a single string
 		 */
-		string 	downloadToString();
+		std::string 	downloadToString();
 
 		/**
 		 * Fetch only HTTP headers for remote file.
@@ -131,7 +117,7 @@
 		 * @note	This function relies on that either downloadHeaders(), downloadToFile() or downloadToString() has been called prior.
 		 * @param	header	Name of header to fetch.
 		 */
-		string 	getHeader(string header);
+		std::string 	getHeader(std::string header);
 
 		/**
 		 * Returns the response code.
@@ -152,33 +138,33 @@
 
 	private:
 		// Variables
-		string 	url;
-		string 	remoteHostname;
-		string 	remoteProtocol;
-		string 	remoteFile;
+		std::string 	url;
+		std::string 	remoteHostname;
+		std::string 	remoteProtocol;
+		std::string 	remoteFile;
 		int 	remotePort;
-		map<string, string> headers;
+		std::map<std::string, std::string> headers;
 		int 	respCode;
 		bool	followRedirect;
 		StreamSocket * sock;
 		bool 	internalSocketObject;
 
 		// Functions
-		int 	fetch(string request, ostream & bodyStream);
+		int 	fetch(std::string request, std::ostream & bodyStream);
 
 		/**
 		 * Parse a line of the HTTP response and store the line as a key/value pair in the map of headers.
 		 *
 		 * Note that the header value is trimmed for white-space both from the right and left.
 		 */
-		void 	parseHeader(string line);
+		void 	parseHeader(std::string line);
 
 		/**
 		 * Does not take into account the following things:
 		 * - Header values might be split into multiple lines
 		 * - Header lines can end with \\n instead of \\r\\n
 		 */
-		int 	parseHeaders(stringstream & headers);
+		int 	parseHeaders(std::stringstream & headers);
 
 		/**
 		 * Used to parse the URL supplied in the constructor.
@@ -198,15 +184,15 @@
 		 * @author	Erik Ehrlund
 		 * @author	Mikael Svensson
 		 */
-		void	split(string data, string token, vector<string> &res, int maxChars = -1); // Copyright Erik Ehrlund
+		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.
 		 *
 		 * @deprecated	Use the very similar trim() from libmutil/stringutils.h instead.
 		 */
-		string	trim(string s);
-		string	buildRequestString(string method, string file);
+		std::string	trim(std::string s);
+		std::string	buildRequestString(std::string method, std::string file);
 };
 
 #endif

Modified: trunk/libmnetutil/source/HttpDownloader.cxx
===================================================================
--- trunk/libmnetutil/source/HttpDownloader.cxx	2007-06-12 19:37:44 UTC (rev 3305)
+++ trunk/libmnetutil/source/HttpDownloader.cxx	2007-06-12 19:57:56 UTC (rev 3306)
@@ -1,7 +1,4 @@
 #include <sys/types.h>
-#include <sys/socket.h>
-#include <netinet/in.h>
-#include <netdb.h>
 
 #include <libmutil/MemObject.h>
 #include <libmnetutil/HttpDownloader.h>
@@ -22,6 +19,23 @@
 
 using namespace std;
 
+HttpDownloader::HttpDownloader(string url) : url (url), remotePort(80), respCode(-1), followRedirect(true), sock(NULL), internalSocketObject(false) {
+	parseUrl();
+	if (remotePort > 0 && remoteHostname != "") {
+		sock = new TCPSocket(remoteHostname, remotePort);
+		internalSocketObject = true;
+	}
+}
+
+HttpDownloader::HttpDownloader(string url, StreamSocket * sock): url (url), remotePort(80), respCode(-1), followRedirect(true), sock(sock), internalSocketObject(false) {
+	parseUrl();
+}
+
+HttpDownloader::~HttpDownloader() {
+	if (internalSocketObject)
+		delete sock;
+}
+
 const char* HttpDownloader::getChars() {
 	int tries = 3;
 	while (tries) {
@@ -65,9 +79,8 @@
 	}
 
 	stringstream headerStream;
-	int fp = 0;
 	int32_t bytesWritten = 0, bytesRead = 0;
-	struct sockaddr_in remoteAddr;
+// 	struct sockaddr_in remoteAddr;
 	/*
 	This is what the hostent struct looks like:
 
@@ -81,31 +94,25 @@
 	#define h_addr  h_addr_list[0]  // address, for backward compatiblity
 	};
 	*/
-	struct hostent *remoteHost;
+// 	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
 
-	/* Create socket */
-	fp = socket(AF_INET, SOCK_STREAM, 0);
-	if (fp == -1) {
-		cerr << "Error: Could not create socket" << endl;
-		return false;
-	}
 	//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
+// 	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));
+// 	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());
@@ -147,7 +154,6 @@
 					case HTTP_RESPONSECODE_OK:
 						break;
 					default:
-						close(fp);
 						return headerParseResult;
 						break;
 
@@ -179,8 +185,8 @@
 }
 
 void HttpDownloader::parseUrl() {
-	int pos = 0;
-	int lastPos = 0;
+	size_t pos = 0;
+	size_t lastPos = 0;
 	// Find protocol
 	if ((pos = url.find("://", 0)) != string::npos) {
 		remoteProtocol = url.substr(lastPos, pos - lastPos);
@@ -206,9 +212,9 @@
 void HttpDownloader::split(string data, string token, vector<string> &res, int maxChars)
 {
 	int count = 0;
-	int lastpos = 0;
+	size_t lastpos = 0;
 	int tokenlen = token.length();
-	int pos = data.find(token,lastpos);
+	size_t pos = data.find(token,lastpos);
 	while(string::npos != pos && ((maxChars > 0 && pos < maxChars) || maxChars <= 0))
 	{
 		count = pos - lastpos;
@@ -227,9 +233,9 @@
 }
 
 string HttpDownloader::trim(string s) {
-	int trimLeftPos = s.find_first_not_of(" \n\t\r");
-	int trimRightPos = s.find_last_not_of(" \n\t\r");
-	int pos = 0;
+	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)
@@ -322,7 +328,7 @@
 
 	respCode = atoi(initialLine[1].c_str());
 
-	for (int i=1; i<lines.size(); i++) {
+	for (size_t i=1; i<lines.size(); i++) {
 		parseHeader(lines.at(i));
 	}
 
@@ -330,7 +336,7 @@
 }
 
 void HttpDownloader::parseHeader(string line) {
-	int pos = line.find(':');
+	size_t pos = line.find(':');
 	if (pos != 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));



More information about the Minisip-devel mailing list