r3512 - in trunk: libmnetutil libmnetutil/include libmnetutil/include/libmnetutil libmnetutil/source libmnetutil/vs8_proj libmutil/vs8_proj
erik at minisip.org
erik at minisip.org
Fri Nov 23 00:49:43 CET 2007
Author: erik
Date: 2007-11-23 00:49:43 +0100 (Fri, 23 Nov 2007)
New Revision: 3512
Added:
trunk/libmnetutil/include/libmnetutil/HttpFileSystem.h
trunk/libmnetutil/source/HttpFileSystem.cxx
Modified:
trunk/libmnetutil/Makefile.am
trunk/libmnetutil/include/Makefile.am
trunk/libmnetutil/include/libmnetutil/Downloader.h
trunk/libmnetutil/include/libmnetutil/HttpDownloader.h
trunk/libmnetutil/source/Downloader.cxx
trunk/libmnetutil/source/HttpDownloader.cxx
trunk/libmnetutil/vs8_proj/libmnetutil.vcproj
trunk/libmutil/vs8_proj/libmutil.vcproj
Log:
libmutil:
* Added FileSystem.{h,cxx} to the MSVC project.
libmnetutil:
* Made the HttpDownloader use references to sockets instead of pointers.
* Created a HttpFileSystem implementation. In this commit no write support
is enabled.
Minisip reads (and sometimes writes) several files when it is
executed:
- configuration file (if not using gconf)
- certificates
- address books
- ring tones and other UI resources
(- plugins)
We have explored the possibility to store settins online (see "Secure on-line
configuration for SIP User Agents" on the publications section of
minisip.org), and there are both the kind of files that you always would
like to store on the device (information such as what is the sound i/o to
use on this device), and information that some users would always like to
store online.
I believe that we should create two "FileSystem" objects,
- one that is device-local ones, and
- one that can be local, but also on-line.
If data is retrieved using HTTP+TLS_SRP, or opened locally will be transparent
to any code after the file systems have been initialized.
Example of code using FileSystem objects:
void process(string fileurl){
MRef<FileSystem*> fs;
if ( path.find("http://") == 0 ){
fs = new HttpFileSystem( new TCPSocket("www.minisip.org", 80) );
}else{
fs = new LocalFileSystem;
}
//Filesystem independent I/O from here
MRef<File*> f = fs->open(argv[1]);
cerr << "file size: "<< f->size() << endl;
f->read( buf, len );
}
Modified: trunk/libmnetutil/Makefile.am
===================================================================
--- trunk/libmnetutil/Makefile.am 2007-11-22 23:27:01 UTC (rev 3511)
+++ trunk/libmnetutil/Makefile.am 2007-11-22 23:49:43 UTC (rev 3512)
@@ -63,6 +63,7 @@
source/FileUrl.cxx \
source/FileDownloaderException.cxx \
source/FileDownloader.cxx \
+ source/HttpFileSystem.cxx \
source/DirectorySet.cxx \
source/DirectorySetItem.cxx
Modified: trunk/libmnetutil/include/Makefile.am
===================================================================
--- trunk/libmnetutil/include/Makefile.am 2007-11-22 23:27:01 UTC (rev 3511)
+++ trunk/libmnetutil/include/Makefile.am 2007-11-22 23:49:43 UTC (rev 3512)
@@ -25,6 +25,7 @@
libmnetutil/libmnetutil_config.h \
libmnetutil/Downloader.h \
libmnetutil/HttpDownloader.h \
+ libmnetutil/HttpFileSystem.h \
libmnetutil/FileUrl.h \
libmnetutil/FileDownloaderException.h \
libmnetutil/FileDownloader.h \
Modified: trunk/libmnetutil/include/libmnetutil/Downloader.h
===================================================================
--- trunk/libmnetutil/include/libmnetutil/Downloader.h 2007-11-22 23:27:01 UTC (rev 3511)
+++ trunk/libmnetutil/include/libmnetutil/Downloader.h 2007-11-22 23:49:43 UTC (rev 3512)
@@ -22,6 +22,7 @@
#include <libmnetutil/libmnetutil_config.h>
#include <libmutil/MemObject.h>
+#include <libmnetutil/StreamSocket.h>
#include <string>
/**
@@ -42,14 +43,20 @@
* Uses the protocol portion of the specified URI to determine
* what type of object should be created to handle the download.
*
- * Currently only supports HTTP download using the HttpDownloader class.
+ * Currently, the following downloading URIs are supported:
+ * file:// Local file download
+ * http:// Download from web server
+ * ldap:// Download from LDAP server
*
* @param uri Full URI for a remote document (e.g. "http://www.kth.se/index.html")
+ * @param conn Given if an existing connection should be used. This is currently
+ * only supported for http:// URIs.
*/
- static MRef<Downloader*> create(std::string const uri);
+ static MRef<Downloader*> create(std::string const uri, MRef<StreamSocket*> conn=NULL);
- virtual std::string getMemObjectType() const {return "Downloader";};
+ virtual std::string getMemObjectType() const {return "Downloader";}
+
/**
* Returns the document in question. In order to support both
* textual and binary documents there is no function for
Modified: trunk/libmnetutil/include/libmnetutil/HttpDownloader.h
===================================================================
--- trunk/libmnetutil/include/libmnetutil/HttpDownloader.h 2007-11-22 23:27:01 UTC (rev 3511)
+++ trunk/libmnetutil/include/libmnetutil/HttpDownloader.h 2007-11-22 23:49:43 UTC (rev 3512)
@@ -96,7 +96,7 @@
* @param url File/document to fetch.
* @param sock Pre-existing socket to use for communicating with HTTP server.
*/
- HttpDownloader(std::string url, StreamSocket * sock);
+ HttpDownloader(std::string url, MRef<StreamSocket*> sock);
/**
* The default constructor deallocates memory, if allocated.
@@ -156,7 +156,7 @@
std::map<std::string, std::string> headers;
int respCode;
bool followRedirect;
- StreamSocket * sock;
+ MRef<StreamSocket *> sock;
bool internalSocketObject;
// Functions
Added: trunk/libmnetutil/include/libmnetutil/HttpFileSystem.h
===================================================================
--- trunk/libmnetutil/include/libmnetutil/HttpFileSystem.h (rev 0)
+++ trunk/libmnetutil/include/libmnetutil/HttpFileSystem.h 2007-11-22 23:49:43 UTC (rev 3512)
@@ -0,0 +1,54 @@
+/*
+ Copyright (C) 2007 Erik Eliasson
+
+ 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
+*/
+
+/*
+ * Authors: Erik Eliasson <eliasson at it.kth.se>
+ */
+
+#ifndef HTTPFILESYSTEM_H
+#define HTTPFILESYSTEM_H
+
+#include<libmnetutil/libmnetutil_config.h>
+#include<libmutil/FileSystem.h>
+#include<libmnetutil/StreamSocket.h>
+//#include<libmnetutil/HttpDownloader.h>
+
+#include<string>
+
+/**
+ *
+ * Note:
+ * You MUST use a MRef to your HttpFileSystem object.
+ * You CAN NOT have HttpFileSystem objects on the stack.
+ * This is because if not, the garbage collector will delete it
+ * when your last file opened with the open method is deleted.
+ *
+ */
+class LIBMNETUTIL_API HttpFileSystem : public FileSystem {
+ public:
+ HttpFileSystem( MRef<StreamSocket*> conn, std::string prefix );
+ virtual void mkdir( const std::string & name );
+ virtual MRef<File*> open( const std::string& path, bool createIfNotExist=false );
+
+ private:
+ //MRef<HttpDownloader*> httpDl;
+ std::string prefix;
+ MRef<StreamSocket*> conn;
+};
+
+#endif
Modified: trunk/libmnetutil/source/Downloader.cxx
===================================================================
--- trunk/libmnetutil/source/Downloader.cxx 2007-11-22 23:27:01 UTC (rev 3511)
+++ trunk/libmnetutil/source/Downloader.cxx 2007-11-22 23:49:43 UTC (rev 3512)
@@ -32,18 +32,19 @@
#include <string>
-MRef<Downloader*> Downloader::create(std::string const uri) {
+MRef<Downloader*> Downloader::create(std::string const uri, MRef<StreamSocket*> conn) {
size_t pos = uri.find("://");
if (std::string::npos != pos) {
std::string protocol = uri.substr(0, pos);
if (protocol == "http")
- return MRef<Downloader*>(dynamic_cast<Downloader*>(new HttpDownloader(uri)));
+ return new HttpDownloader(uri, conn);
else if (protocol == "file")
- return MRef<Downloader*>(dynamic_cast<Downloader*>(new FileDownloader(uri)));
+ return new FileDownloader(uri);
#ifdef ENABLE_LDAP
else if (protocol == "ldap")
- return MRef<Downloader*>(dynamic_cast<Downloader*>(new LdapDownloader(uri)));
+ return new LdapDownloader(uri);
#endif
}
- return MRef<Downloader*>();
+ return NULL;
}
+
Modified: trunk/libmnetutil/source/HttpDownloader.cxx
===================================================================
--- trunk/libmnetutil/source/HttpDownloader.cxx 2007-11-22 23:27:01 UTC (rev 3511)
+++ trunk/libmnetutil/source/HttpDownloader.cxx 2007-11-22 23:49:43 UTC (rev 3512)
@@ -41,21 +41,18 @@
#define HTTP_METHOD_1_0 "HTTP/1.0"
#define HTTP_HEADER_CRLF "\r\n"
-HttpDownloader::HttpDownloader(std::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) {
parseUrl();
if (remotePort > 0 && remoteHostname != "") {
sock = new TCPSocket(remoteHostname, remotePort);
- internalSocketObject = true;
}
}
-HttpDownloader::HttpDownloader(std::string url, StreamSocket * sock): url (url), remotePort(80), respCode(-1), followRedirect(true), sock(sock), internalSocketObject(false) {
+HttpDownloader::HttpDownloader(std::string url, MRef<StreamSocket*> sock): url (url), remotePort(80), respCode(-1), followRedirect(true), sock(sock) {
parseUrl();
}
HttpDownloader::~HttpDownloader() {
- if (internalSocketObject)
- delete sock;
}
char* HttpDownloader::getChars(int *length) {
@@ -98,7 +95,7 @@
int HttpDownloader::fetch(std::string request, std::ostream & bodyStream) {
- if (sock == NULL) {
+ if ( sock.isNull() ) {
// TODO: Error check for socket object
}
@@ -169,8 +166,6 @@
}
}
}
- if (internalSocketObject)
- sock->close();
/*
if (bytesRead < 0) {
Added: trunk/libmnetutil/source/HttpFileSystem.cxx
===================================================================
--- trunk/libmnetutil/source/HttpFileSystem.cxx (rev 0)
+++ trunk/libmnetutil/source/HttpFileSystem.cxx 2007-11-22 23:49:43 UTC (rev 3512)
@@ -0,0 +1,87 @@
+
+#include<config.h>
+
+#include<libmnetutil/HttpFileSystem.h>
+#include<libmnetutil/HttpDownloader.h>
+
+using namespace std;
+
+class FileString : public File {
+public:
+ FileString(char *data, int len, MRef<StreamSocket*> ssock);
+ virtual void read(void *buf, int32_t count);
+ virtual void write(void *buf, int32_t count);
+ virtual void seek(int32_t pos );
+ virtual int64_t size();
+ virtual void flush();
+
+private:
+ char *data;
+ int64_t len;
+ int64_t curpos;
+ bool dirty;
+ int64_t allocSize;
+ MRef<StreamSocket*> ssock;
+
+};
+
+FileString::FileString( char *d, int l, MRef<StreamSocket*> ssock_ ) : data(d), len(l),
+ curpos(0), dirty(false),
+ allocSize(len), ssock(ssock_) { }
+
+
+void FileString::read(void *buf, int32_t count){
+ if ( count+curpos > len ){
+ //count = len-curpos;
+ throw FileException("Read beyond end of file");
+ }
+ memcpy(buf, &data[curpos], count);
+}
+
+void FileString::write(void *buf, int32_t count){
+ if (curpos+count > allocSize){
+ allocSize = curpos+count + (curpos+count)/4;
+ data = (char*)realloc(data, allocSize ); // alloc 25% more than needed
+ }
+
+ memcpy( &data[curpos], buf, count );
+
+ curpos+=count;
+ if (curpos>len)
+ len = curpos;
+}
+
+
+void FileString::seek(int32_t pos ){
+ curpos=pos;
+ if (curpos>len)
+ curpos=len;
+}
+
+int64_t FileString::size(){
+ return len;
+}
+
+void FileString::flush(){
+#warning FileString flush not implemented FIXME
+// httpDl->uploadFile(myPath, data, len);
+}
+
+HttpFileSystem::HttpFileSystem(MRef<StreamSocket*> conn_, string prefix_) :
+ prefix(prefix_), conn(conn_) { }
+
+void HttpFileSystem::mkdir( const std::string & name ){
+
+}
+
+MRef<File*> HttpFileSystem::open( const std::string& path, bool createIfNotExist ){
+ char *data=NULL;
+ int len=0;
+ HttpDownloader dl( "www.minisip.org/~erik/data.bin", conn );
+ data = dl.getChars(&len);
+#warning download data here FIXME;
+ return new FileString( data, len, conn);
+
+}
+
+
Modified: trunk/libmnetutil/vs8_proj/libmnetutil.vcproj
===================================================================
--- trunk/libmnetutil/vs8_proj/libmnetutil.vcproj 2007-11-22 23:27:01 UTC (rev 3511)
+++ trunk/libmnetutil/vs8_proj/libmnetutil.vcproj 2007-11-22 23:49:43 UTC (rev 3512)
@@ -215,6 +215,10 @@
>
</File>
<File
+ RelativePath="..\source\HttpFileSystem.cxx"
+ >
+ </File>
+ <File
RelativePath="..\inet_aton.c"
>
</File>
@@ -405,6 +409,10 @@
>
</File>
<File
+ RelativePath="..\include\libmnetutil\HttpFileSystem.h"
+ >
+ </File>
+ <File
RelativePath="..\include\inet_aton.h"
>
</File>
Modified: trunk/libmutil/vs8_proj/libmutil.vcproj
===================================================================
--- trunk/libmutil/vs8_proj/libmutil.vcproj 2007-11-22 23:27:01 UTC (rev 3511)
+++ trunk/libmutil/vs8_proj/libmutil.vcproj 2007-11-22 23:49:43 UTC (rev 3512)
@@ -206,6 +206,10 @@
>
</File>
<File
+ RelativePath="..\include\libmutil\FileSystem.h"
+ >
+ </File>
+ <File
RelativePath="..\include\libmutil\FileSystemUtils.h"
>
</File>
@@ -346,6 +350,10 @@
>
</File>
<File
+ RelativePath="..\source\FileSystem.cxx"
+ >
+ </File>
+ <File
RelativePath="..\source\FileSystemUtils.cxx"
>
</File>
More information about the Minisip-devel
mailing list