From fcbdecfd1e6aaa92cb1769ed1d395d59648ad5a6 Mon Sep 17 00:00:00 2001 From: Tatsuhiro Tsujikawa Date: Sun, 8 Apr 2012 00:50:25 +0900 Subject: [PATCH] Conditional compile for WebSocket. WebSocket support depends on Message Digest support. --- README.asciidoc | 5 +-- configure.ac | 14 ++++++-- src/HttpServerCommand.cc | 24 +++++++++++-- src/Makefile.am | 15 +++++--- src/MultiUrlRequestInfo.cc | 6 +++- src/Notifier.cc | 6 +++- src/NullWebSocketSessionMan.h | 67 +++++++++++++++++++++++++++++++++++ 7 files changed, 123 insertions(+), 14 deletions(-) create mode 100644 src/NullWebSocketSessionMan.h diff --git a/README.asciidoc b/README.asciidoc index b4c952ca..c2ba1f5e 100644 --- a/README.asciidoc +++ b/README.asciidoc @@ -93,7 +93,7 @@ Dependency .External Library Dependency [options="header"] -|==================================================== +|================================================================== |features |dependency |HTTPS |GnuTLS or OpenSSL |BitTorrent |libnettle+libgmp or libgcrypt or OpenSSL @@ -103,7 +103,8 @@ Dependency |Async DNS |C-Ares |Firefox3/Chromium cookie|libsqlite3 |XML-RPC |libxml2 or Expat. -|==================================================== +|JSON-RPC over WebSocket |libnettle or libgcrypt or OpenSSL +|================================================================== Note;; diff --git a/configure.ac b/configure.ac index c9db0315..e21ff692 100644 --- a/configure.ac +++ b/configure.ac @@ -562,9 +562,15 @@ if test "x$have_option_const_name" = "xyes"; then fi AC_CONFIG_SUBDIRS([deps/wslay]) -LIBS="\$(top_builddir)/deps/wslay/lib/libwslay.la $LIBS" -# $(top_srcdir) for `make distcheck` -CPPFLAGS="-I\$(top_builddir)/deps/wslay/lib/includes -I\$(top_srcdir)/deps/wslay/lib/includes $CPPFLAGS" +if test "x$enable_message_digest" = "xyes"; then + enable_websocket=yes + AC_DEFINE([ENABLE_WEBSOCKET], [1], + [Define 1 if WebSocket support is enabled.]) + LIBS="\$(top_builddir)/deps/wslay/lib/libwslay.la $LIBS" + # $(top_srcdir) for `make distcheck` + CPPFLAGS="-I\$(top_builddir)/deps/wslay/lib/includes -I\$(top_srcdir)/deps/wslay/lib/includes $CPPFLAGS" +fi +AM_CONDITIONAL([ENABLE_WEBSOCKET], [test "x$enable_websocket" = "xyes"]) AC_CONFIG_FILES([Makefile src/Makefile @@ -599,3 +605,5 @@ echo "Epoll: $have_epoll" echo "Bittorrent: $enable_bittorrent" echo "Metalink: $enable_metalink" echo "XML-RPC: $enable_xml_rpc" +echo "Message Digest: $enable_message_digest" +echo "WebSocket: $enable_websocket" diff --git a/src/HttpServerCommand.cc b/src/HttpServerCommand.cc index 48fef909..026c2627 100644 --- a/src/HttpServerCommand.cc +++ b/src/HttpServerCommand.cc @@ -43,7 +43,6 @@ #include "RequestGroupMan.h" #include "HttpServerBodyCommand.h" #include "HttpServerResponseCommand.h" -#include "WebSocketResponseCommand.h" #include "RecoverableException.h" #include "prefs.h" #include "Option.h" @@ -51,9 +50,14 @@ #include "wallclock.h" #include "fmt.h" #include "SocketRecvBuffer.h" -#include "MessageDigest.h" -#include "message_digest_helper.h" #include "base64.h" +#ifdef ENABLE_MESSAGE_DIGEST +# include "MessageDigest.h" +# include "message_digest_helper.h" +#endif // ENABLE_MESSAGE_DIGEST +#ifdef ENABLE_WEBSOCKET +# include "WebSocketResponseCommand.h" +#endif // ENABLE_WEBSOCKET namespace aria2 { @@ -108,6 +112,8 @@ void HttpServerCommand::checkSocketRecvBuffer() } } +#ifdef ENABLE_WEBSOCKET + namespace { // Creates server's WebSocket accept key which will be sent in // Sec-WebSocket-Accept header field. The |clientKey| is the value @@ -139,6 +145,8 @@ int websocketHandshake(const SharedHandle& header) } } // namespace +#endif // ENABLE_WEBSOCKET + bool HttpServerCommand::execute() { if(e_->getRequestGroupMan()->downloadFinished() || e_->isHaltRequested()) { @@ -170,6 +178,7 @@ bool HttpServerCommand::execute() const std::string& connectionHd = header->find("connection"); if(util::strieq(upgradeHd.begin(), upgradeHd.end(), "websocket") && util::strieq(connectionHd.begin(), connectionHd.end(), "upgrade")) { +#ifdef ENABLE_WEBSOCKET int status = websocketHandshake(header); Command* command; if(status == 101) { @@ -193,6 +202,15 @@ bool HttpServerCommand::execute() e_->addCommand(command); e_->setNoWait(true); return true; +#else // !ENABLE_WEBSOCKET + httpServer_->feedResponse(400); + Command* command = new HttpServerResponseCommand(getCuid(), + httpServer_, e_, + socket_); + e_->addCommand(command); + e_->setNoWait(true); + return true; +#endif // !ENABLE_WEBSOCKET } else { if(e_->getOption()->getAsInt(PREF_RPC_MAX_REQUEST_SIZE) < httpServer_->getContentLength()) { diff --git a/src/Makefile.am b/src/Makefile.am index 39461b11..aa822f09 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -227,16 +227,23 @@ SRCS = Socket.h\ rpc_helper.cc rpc_helper.h\ WatchProcessCommand.cc WatchProcessCommand.h\ UnknownOptionException.cc UnknownOptionException.h\ - WebSocketSession.cc WebSocketSession.h\ - WebSocketSessionMan.cc WebSocketSessionMan.h\ - WebSocketResponseCommand.cc WebSocketResponseCommand.h\ - WebSocketInteractionCommand.cc WebSocketInteractionCommand.h\ Notifier.cc Notifier.h if MINGW_BUILD SRCS += WinConsoleFile.cc WinConsoleFile.h endif # MINGW_BUILD +if ENABLE_WEBSOCKET +SRCS += WebSocketSession.cc WebSocketSession.h\ + WebSocketSessionMan.cc WebSocketSessionMan.h\ + WebSocketResponseCommand.cc WebSocketResponseCommand.h\ + WebSocketInteractionCommand.cc WebSocketInteractionCommand.h +endif # ENABLE_WEBSOCKET + +if !ENABLE_WEBSOCKET +SRCS += NullWebSocketMan.h +endif # !ENABLE_WEBSOCKET + if HAVE_SOME_XMLLIB SRCS += XmlAttr.cc XmlAttr.h\ XmlParser.h\ diff --git a/src/MultiUrlRequestInfo.cc b/src/MultiUrlRequestInfo.cc index 0053a99d..c0636a06 100644 --- a/src/MultiUrlRequestInfo.cc +++ b/src/MultiUrlRequestInfo.cc @@ -63,7 +63,11 @@ #include "UriListParser.h" #include "SingletonHolder.h" #include "Notifier.h" -#include "WebSocketSessionMan.h" +#ifdef ENABLE_WEBSOCKET +# include "WebSocketSessionMan.h" +#else // !ENABLE_WEBSOCKET +# include "NullWebSocketSessionMan.h" +#endif // !ENABLE_WEBSOCKET #ifdef ENABLE_SSL # include "TLSContext.h" #endif // ENABLE_SSL diff --git a/src/Notifier.cc b/src/Notifier.cc index 9375c40d..6d843340 100644 --- a/src/Notifier.cc +++ b/src/Notifier.cc @@ -34,8 +34,12 @@ /* copyright --> */ #include "Notifier.h" #include "RequestGroup.h" -#include "WebSocketSessionMan.h" #include "LogFactory.h" +#ifdef ENABLE_WEBSOCKET +# include "WebSocketSessionMan.h" +#else // !ENABLE_WEBSOCKET +# include "NullWebSocketSessionMan.h" +#endif // !ENABLE_WEBSOCKET namespace aria2 { diff --git a/src/NullWebSocketSessionMan.h b/src/NullWebSocketSessionMan.h new file mode 100644 index 00000000..0e5e34d5 --- /dev/null +++ b/src/NullWebSocketSessionMan.h @@ -0,0 +1,67 @@ +/* */ +#ifndef D_NULL_WEB_SOCKET_SESSION_MAN_H +#define D_NULL_WEB_SOCKET_SESSION_MAN_H + +#include "common.h" + +#include + +#include "SharedHandle.h" + +// Empty implementation for build without WebSocket support. + +namespace aria2 { + +class RequestGroup; + +namespace rpc { + +class WebSocketSession {}; + +class WebSocketSessionMan { +public: + WebSocketSessionMan() {} + ~WebSocketSessionMan() {} + void addSession(const SharedHandle& wsSession) {} + void removeSession(const SharedHandle& wsSession) {} + void addNotification(const std::string& method, const RequestGroup* group) {} +}; + +} // namespace rpc + +} // aria2 + +#endif // D_NULL_WEB_SOCKET_SESSION_MAN_H