using namespace zypp;
#include "shttpd.h"
-
+#include "boost/bind.hpp"
#include "WebServer.h"
+#define LISTENING_PORT "8080"
+
+static shttpd_ctx * do_init_ctx()
+{
+ static char *argv[] = {"a", "-ports", LISTENING_PORT, NULL};
+ static int argc = sizeof(argv) / sizeof(argv[0]) - 1;
+ shttpd_ctx *ctx;
+
+ ctx = shttpd_init(argc, argv);
+ return ctx;
+}
+
WebServer::WebServer(const Pathname root, unsigned int port)
- : _docroot(root), _port(port)
+ : _docroot(root), _port(port), _stop(false)
{
- _ctx = shttpd_init(0, NULL);
- shttpd_set_option(_ctx, "root", root.c_str());
- shttpd_set_option(_ctx, "ports", str::numstring(port).c_str());
+ // init with a user port
+ _ctx = do_init_ctx();
+ setStrOption("root", root.asString());
+ setNumOption("ports", port);
+}
+void WebServer::start()
+{
+// _thrd.reset( new boost::thread(web_thread(_ctx)) );
+ _thrd.reset( new boost::thread( boost::bind(&WebServer::worker_thread, this) ) );
}
-void WebServer::operator()()
+void WebServer::worker_thread()
{
- for (;;)
+ while( ! _stop )
shttpd_poll(_ctx, 1000);
}
+
+void WebServer::setStrOption( std::string name, std::string value)
+{
+ if ( ! shttpd_set_option(_ctx, name.c_str(),
+ value.c_str() ) )
+ ZYPP_THROW(Exception("bad option"));
+}
+
+void WebServer::setNumOption( std::string name, int value)
+{
+ if ( ! shttpd_set_option(_ctx, name.c_str(),
+ str::numstring(value).c_str()) )
+ ZYPP_THROW(Exception("bad ioption"));
+}
+
+void WebServer::stop()
+{
+ _stop = true;
+ _thrd->join();
+ _thrd.reset();
+}
+
WebServer::~WebServer()
{
shttpd_fini(_ctx);
#ifndef ZYPP_WEBSERVER_H
#define ZYPP_WEBSERVER_H
+#include "boost/thread.hpp"
+#include "boost/smart_ptr.hpp"
+
#include "zypp/Pathname.h"
+#include "zypp/base/PtrTypes.h"
struct shttpd_ctx;
class WebServer
{
public:
- WebServer(const zypp::Pathname root, unsigned int port);
+ WebServer(const zypp::Pathname root, unsigned int port=9099);
~WebServer();
void start();
-
- void operator()();
+ void stop();
+
private:
+
+ void setStrOption( std::string name, std::string value);
+ void setNumOption( std::string name, int value);
+
+ void worker_thread();
struct shttpd_ctx *_ctx;
zypp::Pathname _docroot;
unsigned int _port;
+ zypp::shared_ptr<boost::thread> _thrd;
+ bool _stop;
};
#endif
//MIL << fetcher;
}
+BOOST_AUTO_TEST_CASE(fetcher_remove)
+{
+ // at this point the key is already trusted
+ {
+ // add the key as trusted
+ //getZYpp()->keyRing()->importKey(PublicKey(DATADIR + "/complexdir/subdir1/SHA1SUMS.key"), true);
+
+ WebServer web((Pathname(TESTS_SRC_DIR) + "/zypp/data/Fetcher/remote-site").c_str() );
+ web.start();
+
+ MediaSetAccess media( Url("http://localhost:9099"), "/" );
+ Fetcher fetcher;
+ filesystem::TmpDir dest;
+
+ fetcher.enqueueDir(OnMediaLocation().setFilename("/complexdir"), true);
+ fetcher.start( dest.path(), media );
+
+ fetcher.reset();
+
+ fetcher.enqueueDir(OnMediaLocation().setFilename("/complexdir-broken"), true);
+ BOOST_CHECK_THROW( fetcher.start( dest.path(), media ), Exception);
+
+ fetcher.reset();
+
+ web.stop();
+ }
+}
// vim: set ts=2 sts=2 sw=2 ai et: