r3345 - in trunk/libmnetutil: . include include/libmnetutil source
mikaelsv at minisip.org
mikaelsv at minisip.org
Fri Jun 29 17:23:31 CEST 2007
Author: mikaelsv
Date: 2007-06-29 17:23:30 +0200 (Fri, 29 Jun 2007)
New Revision: 3345
Added:
trunk/libmnetutil/include/libmnetutil/FileDownloader.h
trunk/libmnetutil/include/libmnetutil/FileDownloaderException.h
trunk/libmnetutil/include/libmnetutil/FileUrl.h
trunk/libmnetutil/source/FileDownloader.cxx
trunk/libmnetutil/source/FileDownloaderException.cxx
trunk/libmnetutil/source/FileUrl.cxx
Modified:
trunk/libmnetutil/Makefile.am
trunk/libmnetutil/include/Makefile.am
trunk/libmnetutil/include/libmnetutil/LdapUrl.h
trunk/libmnetutil/source/Downloader.cxx
trunk/libmnetutil/source/HttpDownloader.cxx
Log:
* New class: FileUrl represents a file://host/path URL.
* New class: FileDownloader loads a locally stored file into memory (or makes a copy of the file).
The point of this class is to make file access more uniform since one can now use Downloader
objects to fetch files both using LDAP, HTTP and the local file system.
FileDownloader will be used in the future certificate caching features of Minisip when
certificates will be authomatically retrieved from many different sources.
The class works on Linux, should work on Windows and may work on other platforms.
* Other updates: Fixes some slips in the documentation.
Modified: trunk/libmnetutil/Makefile.am
===================================================================
--- trunk/libmnetutil/Makefile.am 2007-06-29 09:18:58 UTC (rev 3344)
+++ trunk/libmnetutil/Makefile.am 2007-06-29 15:23:30 UTC (rev 3345)
@@ -62,7 +62,10 @@
$(win32_src) \
source/StreamSocket.cxx \
source/Downloader.cxx \
- source/HttpDownloader.cxx
+ source/HttpDownloader.cxx \
+ source/FileUrl.cxx \
+ source/FileDownloaderException.cxx \
+ source/FileDownloader.cxx
# maintainer rules
ACLOCAL_AMFLAGS = -I m4 ${ACLOCAL_FLAGS}
Modified: trunk/libmnetutil/include/Makefile.am
===================================================================
--- trunk/libmnetutil/include/Makefile.am 2007-06-29 09:18:58 UTC (rev 3344)
+++ trunk/libmnetutil/include/Makefile.am 2007-06-29 15:23:30 UTC (rev 3345)
@@ -27,7 +27,10 @@
libmnetutil/DatagramSocket.h \
libmnetutil/libmnetutil_config.h \
libmnetutil/Downloader.h \
- libmnetutil/HttpDownloader.h
+ libmnetutil/HttpDownloader.h \
+ libmnetutil/FileUrl.h \
+ libmnetutil/FileDownloaderException.h \
+ libmnetutil/FileDownloader.h
noinst_HEADERS = \
config.h \
Added: trunk/libmnetutil/include/libmnetutil/FileDownloader.h
===================================================================
--- trunk/libmnetutil/include/libmnetutil/FileDownloader.h (rev 0)
+++ trunk/libmnetutil/include/libmnetutil/FileDownloader.h 2007-06-29 15:23:30 UTC (rev 3345)
@@ -0,0 +1,83 @@
+/*
+ Copyright (C) 2005, 2004 Erik Eliasson, Johan Bilien
+
+ This library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public
+ License as published by the Free Software Foundation; either
+ version 2.1 of the License, or (at your option) any later version.
+
+ This library 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
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with this library; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+*/
+
+#ifndef _FILEDOWNLOADER_H_
+#define _FILEDOWNLOADER_H_
+
+#include<libmnetutil/libmnetutil_config.h>
+
+#include <libmutil/MemObject.h>
+#include <libmnetutil/FileUrl.h>
+#include <libmnetutil/FileDownloaderException.h>
+#include <libmnetutil/Downloader.h>
+#include <string>
+#include <vector>
+#include <map>
+
+/**
+ * This class simplifies retrieval of \em locally stored files using "file URLs".
+ *
+ * It is important to note that this class \em cannot retrieve files from remote hosts,
+ * even though it uses FileUrl object (which can handle URIs with remote hosts).
+ *
+ * @author Mikael Svensson
+ */
+class FileDownloader : public Downloader {
+ public:
+ /**
+ * Connects to LDAP server but does not fetch file.
+ *
+ * @param url File/document to fetch.
+ */
+ FileDownloader(std::string originalUrl);
+
+ /**
+ * Returns the data pointed to by the URL supplied to the constructor.
+ *
+ * @param length Pointer to integer that will be used to return
+ * the length of the returned character array.
+ */
+ char* getChars(int *length);
+
+ /**
+ * Fetches \em binary data from LDAP server and saves as files on local computer.
+ *
+ * @note The function replaces existing files without informing the user.
+ * @param filename The path to where the retrieved file should be stored.
+ */
+ void saveToFile(std::string filename) throw (FileDownloaderException);
+
+ /**
+ * Returns first string belonging to the specified attribute name.
+ *
+ * @todo This function is untested!
+ */
+ std::string getString() throw (FileDownloaderException);
+
+
+ virtual std::string getMemObjectType() const {return "FileDownloader";};
+
+ private:
+ // Variables
+ FileUrl url;
+
+ // Functions
+ bool fetch(std::ostream & outStream);
+};
+
+#endif
Added: trunk/libmnetutil/include/libmnetutil/FileDownloaderException.h
===================================================================
--- trunk/libmnetutil/include/libmnetutil/FileDownloaderException.h (rev 0)
+++ trunk/libmnetutil/include/libmnetutil/FileDownloaderException.h 2007-06-29 15:23:30 UTC (rev 3345)
@@ -0,0 +1,48 @@
+/*
+ Copyright (C) 2005, 2004 Erik Eliasson, Johan Bilien
+
+ This library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public
+ License as published by the Free Software Foundation; either
+ version 2.1 of the License, or (at your option) any later version.
+
+ This library 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
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with this library; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+*/
+
+#ifndef FILEDOWNLOADEREXCEPTION_H_
+#define FILEDOWNLOADEREXCEPTION_H_
+
+#include<libmnetutil/libmnetutil_config.h>
+
+#include <libmutil/Exception.h>
+#include <libmnetutil/libmnetutil_config.h>
+#include <string>
+
+/**
+ * @author Mikael Svensson
+ * @author Erik Eliasson <eliasson at it.kth.se>
+ * @author Johan Bilien <jobi at via.ecp.fr>
+ */
+class LIBMNETUTIL_API FileDownloaderException : public Exception {
+ public:
+ FileDownloaderException();
+ FileDownloaderException(std::string message);
+ virtual ~FileDownloaderException() throw() {};
+ virtual const char *what() const throw();
+ protected:
+ std::string msg;
+};
+
+class LIBMNETUTIL_API FileDownloaderNotFoundException : public FileDownloaderException {
+ public:
+ FileDownloaderNotFoundException(std::string message);
+};
+
+#endif
Added: trunk/libmnetutil/include/libmnetutil/FileUrl.h
===================================================================
--- trunk/libmnetutil/include/libmnetutil/FileUrl.h (rev 0)
+++ trunk/libmnetutil/include/libmnetutil/FileUrl.h 2007-06-29 15:23:30 UTC (rev 3345)
@@ -0,0 +1,165 @@
+/*
+ Copyright (C) 2005, 2004 Erik Eliasson, Johan Bilien
+
+ This library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public
+ License as published by the Free Software Foundation; either
+ version 2.1 of the License, or (at your option) any later version.
+
+ This library 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
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with this library; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+*/
+
+#ifndef _FILEURL_H_
+#define _FILEURL_H_
+
+#include<libmnetutil/libmnetutil_config.h>
+
+#include <string>
+#include <vector>
+
+#define FILEURL_TYPE_UNKNOWN 0
+#define FILEURL_TYPE_UNIX 1
+#define FILEURL_TYPE_WINDOWS 2
+
+/**
+ * Represents and parses file URLs.
+ *
+ * The class correctly handles Windows and UNIX addresses, with one exception: The class
+ * incorrectly considers paths string with additional slashes ("/") as valid. The
+ * incorrect URI "file:////applib/products/a%2Db/abc%5F9/4148.920a/media/start.swf"
+ * is one exampel of this, as the URI considered valid by the class.
+ *
+ * The type attribute must be set to FILEURL_TYPE_WINDOWS in order for the function to
+ * properly handle Windows file paths. Otherwise UNIX paths are assumed/returned.
+ *
+ * @author Mikael Svensson
+ */
+class FileUrl {
+ public:
+ FileUrl(const std::string url);
+ FileUrl(const std::string url, const int32_t type);
+ FileUrl();
+
+ /**
+ * Restore URL parts to their default values
+ */
+ void clear();
+
+ /**
+ * Tests whether or not the currenct FileUrl instance represents a valid file URL.
+ *
+ * At the moment a file URL is considered invalid only if it doesn't start with "file://".
+ */
+ bool isValid() const;
+
+ /**
+ * Parse URL
+ */
+ void setUrl(const std::string url);
+ void setUrl(const std::string url, const int32_t type);
+
+ /**
+ * Get string representation of URL, e.g. the actual URL.
+ */
+ std::string getString() const;
+
+ /**
+ * Prints all information about the URL. Probably only useful when debugging.
+ */
+ void printDebug();
+
+ /* Getters and setters */
+ std::string getHost() const;
+ void setHost(const std::string host);
+
+ std::string getPath() const;
+ void setPath(const std::string path);
+
+ int32_t getType() const;
+ void setType(const int32_t type);
+ private:
+ /**
+ * The type property is used to differentiate between URLs designed for
+ * different operating systems.
+ *
+ * This allows for automatic conversion of file paths that are not well-suited
+ * for simple "slash separation" (each part of the file path should be
+ * separated using the "/" character as specified by RFC 1738).
+ *
+ * Well-suited paths are e.g. UNIX and Windows paths.Not so well-suited paths
+ * are e.g. VMS paths (although this class currently does not support VMS paths).
+ *
+ * @note Windows file URLs are interpreted as noted on http://blogs.msdn.com/ie/archive/2006/12/06/file-uris-in-windows.aspx
+ */
+ int32_t type;
+
+ std::string host;
+ std::string path;
+
+ /**
+ * Tests whether or not a character is an "unreserved character".
+ *
+ * Unreserved Characters according to RFC 3986 (section 2.3):
+ *
+ * Characters that are allowed in a URI but do not have a reserved
+ * purpose are called unreserved. These include uppercase and lowercase
+ * letters, decimal digits, hyphen, period, underscore, and tilde.
+ */
+ bool isUnreservedChar(char in) const;
+
+ /**
+ * Tests whether or not a character is a "reserved character".
+ *
+ * Reserved Charachters according to RFC 3986 (section 2.2):
+ *
+ * "URIs include components and subcomponents that are delimited by
+ * characters in the 'reserved' set. These characters are called
+ * 'reserved' because they may (or may not) be defined as delimiters by
+ * the generic syntax, by each scheme-specific syntax, or by the
+ * implementation-specific syntax of a URI's dereferencing algorithm."
+ */
+ bool isReservedChar(char in) const;
+
+ /**
+ * Percent-encode a character.
+ *
+ * Converts a space to "%20" and so on. Note that the function converts the input character
+ * regardsless of whether or not a conversion is actually necessary (if an "a" is sent
+ * to the function it will be converted to "%61" even though it is within the US-ACII set).
+ */
+ std::string encodeChar(const char in) const;
+
+ /**
+ * Decode an percent-encoded character.
+ *
+ * Assumes that the input string is three characters long, that the first
+ * character is the "%" sign and that the last two charachters are hexadecimal digits.
+ */
+ char decodeChar(const std::string in) const;
+
+ /**
+ * Converts hexadecimal (or decial) digit into the corresponding integer value.
+ */
+ int32_t charToNum(const char in) const;
+
+ /**
+ * Parses string and converts all its percent-encoded characters to single characters.
+ */
+ std::string percentDecode(const std::string & in) const;
+
+ /**
+ * Percent-encodes string according to rules stated in section 2.1 of RFC 4516.
+ */
+ std::string percentEncode(const std::string & in) const;
+ std::string percentEncode(const std::string & in, bool escapeComma, bool escapeQuestionmark = true) const;
+
+ bool validUrl;
+};
+#endif
Modified: trunk/libmnetutil/include/libmnetutil/LdapUrl.h
===================================================================
--- trunk/libmnetutil/include/libmnetutil/LdapUrl.h 2007-06-29 09:18:58 UTC (rev 3344)
+++ trunk/libmnetutil/include/libmnetutil/LdapUrl.h 2007-06-29 15:23:30 UTC (rev 3345)
@@ -75,7 +75,7 @@
/**
* Get string representation of URL, e.g. the actual URL.
*
- * This representation may look exactly the same as the original URL as the parser
+ * This representation may not look exactly the same as the original URL as the parser
* replaces omitted values with default values. This is not a problem in itself as
* the generated URLs are still valid, albeit more explicit than the original ones.
*
Modified: trunk/libmnetutil/source/Downloader.cxx
===================================================================
--- trunk/libmnetutil/source/Downloader.cxx 2007-06-29 09:18:58 UTC (rev 3344)
+++ trunk/libmnetutil/source/Downloader.cxx 2007-06-29 15:23:30 UTC (rev 3345)
@@ -1,5 +1,6 @@
#include <libmnetutil/Downloader.h>
#include <libmnetutil/HttpDownloader.h>
+#include <libmnetutil/FileDownloader.h>
#ifdef ENABLE_LDAP
#include <libmnetutil/LdapDownloader.h>
@@ -13,6 +14,8 @@
std::string protocol = uri.substr(0, pos);
if (protocol == "http")
return MRef<Downloader*>(dynamic_cast<Downloader*>(new HttpDownloader(uri)));
+ else if (protocol == "file")
+ return MRef<Downloader*>(dynamic_cast<Downloader*>(new FileDownloader(uri)));
#ifdef ENABLE_LDAP
else if (protocol == "ldap")
return MRef<Downloader*>(dynamic_cast<Downloader*>(new LdapDownloader(uri)));
Added: trunk/libmnetutil/source/FileDownloader.cxx
===================================================================
--- trunk/libmnetutil/source/FileDownloader.cxx (rev 0)
+++ trunk/libmnetutil/source/FileDownloader.cxx 2007-06-29 15:23:30 UTC (rev 3345)
@@ -0,0 +1,106 @@
+#include <sys/types.h>
+
+#include <libmutil/MemObject.h>
+#include <libmnetutil/FileUrl.h>
+#include <libmnetutil/FileDownloader.h>
+
+#include <fstream>
+#include <sstream>
+
+#include <string>
+#include <iostream>
+
+#define BUFFERSIZE 4096
+
+FileDownloader::FileDownloader(std::string originalUrl) {
+#ifdef WIN32
+ url = FileUrl(originalUrl, FILEURL_TYPE_WINDOWS);
+#else
+ url = FileUrl(originalUrl, FILEURL_TYPE_UNKNOWN);
+#endif
+}
+
+char* FileDownloader::getChars(int *length) {
+ *length = 0;
+
+ if (url.isValid()) {
+
+ std::ifstream inStream(url.getPath().c_str(), std::ios::in);
+ if (inStream.is_open()) {
+
+ int32_t fileLen = 0;
+
+ inStream.seekg(0, std::ios::end);
+ fileLen = inStream.tellg();
+ inStream.seekg(0, std::ios::beg);
+
+ *length = fileLen;
+ char* res = new char[*length];
+ inStream.read(res, *length);
+ if (!inStream.good()) {
+ inStream.close();
+ return NULL;
+ }
+ inStream.close();
+ return res;
+ } else {
+ return NULL;
+ }
+ } else {
+ return NULL;
+ }
+}
+
+bool FileDownloader::fetch(std::ostream & outStream) {
+
+ if (url.isValid()) {
+
+ std::ifstream inStream(url.getPath().c_str(), std::ios::in);
+ if (inStream.is_open()) {
+
+ int32_t bytesRead = 0;
+ int32_t fileLen = 0;
+ int32_t nextReadLen = BUFFERSIZE;
+
+ inStream.seekg(0, std::ios::end);
+ fileLen = inStream.tellg();
+ inStream.seekg(0, std::ios::beg);
+
+ char buffer[BUFFERSIZE];
+ memset(buffer, 0, sizeof(buffer)); // Zero out the buffer used when recieving data
+
+ bool error = false;
+ do {
+ if (bytesRead + BUFFERSIZE > fileLen)
+ nextReadLen = fileLen - bytesRead;
+
+ inStream.read(buffer, nextReadLen);
+ outStream.write(buffer, nextReadLen);
+ if (!inStream.good() || !outStream.good()) {
+ error = true;
+ break;
+ }
+ bytesRead += nextReadLen;
+ } while (bytesRead < fileLen);
+
+ inStream.close();
+
+ return !error;
+ } else {
+ return false;
+ }
+ } else {
+ return false;
+ }
+}
+
+void FileDownloader::saveToFile(std::string filename) throw (FileDownloaderException) {
+ std::ofstream file(filename.c_str());
+ if (file.good()) {
+ if (!fetch(file)) {
+ file.close();
+ throw FileDownloaderException("Could not save file");
+ }
+ file.close();
+ }
+}
Added: trunk/libmnetutil/source/FileDownloaderException.cxx
===================================================================
--- trunk/libmnetutil/source/FileDownloaderException.cxx (rev 0)
+++ trunk/libmnetutil/source/FileDownloaderException.cxx 2007-06-29 15:23:30 UTC (rev 3345)
@@ -0,0 +1,23 @@
+#include <libmnetutil/FileDownloaderException.h>
+
+/**
+ * @author Mikael Svensson
+ * @author Erik Eliasson <eliasson at it.kth.se>
+ * @author Johan Bilien <jobi at via.ecp.fr>
+ */
+
+FileDownloaderException::FileDownloaderException() {
+ msg = "FileDownloaderException";
+};
+
+FileDownloaderException::FileDownloaderException(std::string msg) {
+ this->msg = "FileDownloaderException: " + msg;
+};
+
+const char* FileDownloaderException::what()const throw(){
+ return msg.c_str();
+}
+
+FileDownloaderNotFoundException::FileDownloaderNotFoundException(std::string message) {
+ msg = "FileDownloaderNotFoundException: Could not find file " + message;
+}
Added: trunk/libmnetutil/source/FileUrl.cxx
===================================================================
--- trunk/libmnetutil/source/FileUrl.cxx (rev 0)
+++ trunk/libmnetutil/source/FileUrl.cxx 2007-06-29 15:23:30 UTC (rev 3345)
@@ -0,0 +1,212 @@
+#include <libmnetutil/FileUrl.h>
+
+#include <iostream>
+#include <libmutil/stringutils.h>
+
+FileUrl::FileUrl(std::string url, const int32_t type) {
+ clear(); // Reset URL parts to default values
+ this->type = type;
+ setUrl(url); // Parse supplied URL
+}
+
+FileUrl::FileUrl(std::string url) {
+ clear(); // Reset URL parts to default values
+ setUrl(url); // Parse supplied URL
+}
+
+FileUrl::FileUrl() {
+ clear(); // Reset URL parts to default values
+}
+
+void FileUrl::clear() {
+ host = "";
+ type = FILEURL_TYPE_UNKNOWN;
+ path = "";
+ validUrl = false;
+}
+
+bool FileUrl::isValid() const{
+ return validUrl;
+}
+
+std::string FileUrl::getString() const {
+
+ // Start off with the schema and host name
+ std::string url("file://");
+ url += host;
+
+ // Append distinguished name (base DN)
+ url += '/';
+
+ char sep = (type == FILEURL_TYPE_WINDOWS ? '\\' : '/');
+ std::vector<std::string> parts = split(path, false, sep, true);
+
+ for (int i=0; i<parts.size(); i++) {
+ std::string decPart = percentEncode(parts.at(i));
+ url += decPart + '/';
+ }
+ url = url.substr(0, url.length() - 1);
+
+ return url;
+}
+bool FileUrl::isUnreservedChar(char in) const {
+ char* alphabetUnreserved = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-._~";
+ for (int i=0; i<66; i++) {
+ if (alphabetUnreserved[i] == in)
+ return true;
+ }
+ return false;
+}
+
+bool FileUrl::isReservedChar(char in) const {
+ char* alphabetReserved = ":/?#[]@!$&'()*+,;=";
+ for (int i=0; i<18; i++) {
+ if (alphabetReserved[i] == in)
+ return true;
+ }
+ return false;
+}
+
+void FileUrl::setUrl(const std::string url) {
+ std::string::size_type lastPos = 0, pos = 0, posTemp = 0;
+
+ if (strCaseCmp(url.substr(0, 7).c_str(), "file://") == 0) {
+ lastPos = 7;
+
+ /**************************************************************
+ * Search for host
+ */
+
+ pos = url.find('/', lastPos);
+
+ if (pos == std::string::npos) {
+ // No slash after schema specifier. This means that no path has been specified. Illegal.
+ validUrl = false;
+ return;
+ }
+
+ if (pos != lastPos) {
+ // Host found
+ host = url.substr(lastPos, pos - lastPos);
+ }
+ lastPos = pos+1;
+
+ if (lastPos < url.length()) {
+ std::string restOfUrl = url.substr(lastPos);
+
+ std::vector<std::string> parts = split(restOfUrl, false, '/', true);
+
+ for (int i=0; i<parts.size(); i++) {
+ std::string decPart = percentDecode(parts.at(i));
+ if (type == FILEURL_TYPE_WINDOWS) {
+ path += decPart + '\\';
+ } else {
+ path += decPart + '/';
+ }
+ }
+ if (path.length() > 1) {
+ path = path.substr(0, path.length() - 1);
+ } else {
+ validUrl = false;
+ }
+ }
+ validUrl = true;
+ } else {
+ validUrl = false;
+ }
+}
+void FileUrl::printDebug() {
+
+ std::cerr << " VALID? " << (validUrl ? "yes" : "NO") << std::endl;
+
+ std::cerr << " Host: [" << host << "]" << std::endl;
+
+ std::cerr << " Path: [" << path << "]" << std::endl;
+
+ std::cerr << " Type: [";
+ switch (type) {
+ case FILEURL_TYPE_UNIX:
+ std::cerr << " UNIX";
+ break;
+ case FILEURL_TYPE_WINDOWS:
+ std::cerr << " Windows";
+ break;
+ default:
+ std::cerr << " Unspecified";
+ break;
+ }
+ std::cerr << "]" << std::endl;
+}
+
+std::string FileUrl::getHost() const {
+ return host;
+}
+void FileUrl::setHost(std::string host) {
+ this->host = host;
+}
+std::string FileUrl::getPath() const {
+ return path;
+}
+void FileUrl::setPath(std::string path) {
+ this->path = path;
+}
+int32_t FileUrl::getType() const {
+ return type;
+}
+void FileUrl::setType(const int32_t type) {
+ this->type = type;
+}
+
+std::string FileUrl::encodeChar(const char in) const {
+ std::string res;
+ res += '%';
+ if (in < 10)
+ res +='0';
+ res += binToHex(reinterpret_cast<const unsigned char*>(&in), sizeof(in));
+ return res;
+}
+char FileUrl::decodeChar(const std::string in) const {
+ if (in.length() == 3) {
+ return (charToNum(in[1]) << 4) + (charToNum(in[2]));
+ } else {
+ return '0';
+ }
+}
+
+int32_t FileUrl::charToNum(const char in) const {
+ if (in >= '0' && in <= '9') {
+ return (in - '0');
+ } else if (in >= 'A' && in <= 'F') {
+ return (in - 'A' + 10);
+ } else if (in >= 'a' && in <= 'f') {
+ return (in - 'a' + 10);
+ } else {
+ return -1;
+ }
+}
+
+std::string FileUrl::percentEncode(const std::string & in) const {
+ return percentEncode(in, true, true);
+}
+
+std::string FileUrl::percentEncode(const std::string & in, bool escapeComma, bool escapeQuestionmark) const {
+ std::string res;
+ for (int i=0; i < in.length(); i++) {
+ if ((!isReservedChar(in[i]) && !isUnreservedChar(in[i])) || (escapeQuestionmark && in[i] == '?') || (escapeComma && in[i] == ','))
+ res += encodeChar(in[i]);
+ else
+ res += in[i];
+ }
+ return res;
+}
+std::string FileUrl::percentDecode(const std::string & in) const {
+ std::string res;
+ for (int i=0; i < in.length(); i++) {
+ if ('%' == in[i]) {
+ res += decodeChar(in.substr(i, 3));
+ i+=2;
+ } else
+ res += in[i];
+ }
+ return res;
+}
Modified: trunk/libmnetutil/source/HttpDownloader.cxx
===================================================================
--- trunk/libmnetutil/source/HttpDownloader.cxx 2007-06-29 09:18:58 UTC (rev 3344)
+++ trunk/libmnetutil/source/HttpDownloader.cxx 2007-06-29 15:23:30 UTC (rev 3345)
@@ -12,9 +12,6 @@
#include <map>
#include <iostream>
-/**
- * The buffer size is set to a constant value to simplify the implementation. A production version of this class cannot have this limitation.
- */
#define BUFFERSIZE 4096
#define HTTP_METHOD_1_0 "HTTP/1.0"
More information about the Minisip-devel
mailing list