From: Alexey Lebedeff Date: Fri, 16 Oct 2015 11:01:27 +0000 (+0300) Subject: Build rabbitmq 3.5.6 for ubuntu X-Git-Url: https://review.fuel-infra.org/gitweb?a=commitdiff_plain;h=09a045d595ef6938da4051226a509970406b3d95;p=packages%2Ftrusty%2Frabbitmq-server.git Build rabbitmq 3.5.6 for ubuntu debian/ from http://archive.ubuntu.com/ubuntu/pool/main/r/rabbitmq-server/rabbitmq-server_3.5.4-1.debian.tar.xz RabbitMQ source from https://www.rabbitmq.com/releases/rabbitmq-server/v3.5.6/rabbitmq-server-3.5.6.tar.gz Tune rabbitmq 3.5.6 for MOS: * Disable auto-start on package install. * Increase ulimit on number of file descriptors. Change-Id: I0c2fcc524caeb9bdc4c048166fed7299350198ec --- diff --git a/debian/changelog b/debian/changelog new file mode 100644 index 0000000..993b354 --- /dev/null +++ b/debian/changelog @@ -0,0 +1,475 @@ +rabbitmq-server (3.5.6-1~u14.04+mos1) mos8.0; urgency=medium + + * New upstream release. + * Disable auto-start on package install. + * Increase ulimit on number of file descriptors. + + -- Alexey Lebedeff Fri, 16 Oct 2015 13:59:11 +0300 + +rabbitmq-server (3.5.4-1) unstable; urgency=medium + + * New upstream release. + + -- James Page Tue, 04 Aug 2015 14:52:31 +0200 + +rabbitmq-server (3.5.1-2) unstable; urgency=medium + + [ Tony Breeds ] + * systemd: Ensure that rabbitmq has started before marking service as + running (LP: #1449056). + + [ James Page ] + * systemd: Drop use of /etc/default/rabbitmq-server. + + -- James Page Tue, 02 Jun 2015 11:40:59 +0100 + +rabbitmq-server (3.5.1-1) unstable; urgency=medium + + * New upstream release. + + -- James Page Wed, 13 May 2015 21:35:52 +0100 + +rabbitmq-server (3.4.3-2) unstable; urgency=medium + + * Restore missing changes from 3.4.2-4. + + -- James Page Mon, 02 Feb 2015 07:44:33 +0200 + +rabbitmq-server (3.4.3-1) unstable; urgency=medium + + * New upstream point release. + + -- James Page Wed, 28 Jan 2015 16:12:32 +0000 + +rabbitmq-server (3.4.2-4) unstable; urgency=medium + + * Re-added /usr/lib/erlang/lib /var/lib/rabbitmq/mnesia and + /var/log/rabbitmq which I removed form the package by mistake on the last + upload. + + -- Thomas Goirand Wed, 28 Jan 2015 13:11:02 +0000 + +rabbitmq-server (3.4.2-3) unstable; urgency=medium + + * Removes debian/README which is useless (Closes: #703021). + * Provides a default /etc/rabbitmq/rabbitmq-env.conf (Closes: #543638). + + -- Thomas Goirand Tue, 27 Jan 2015 15:08:08 +0100 + +rabbitmq-server (3.4.2-2) unstable; urgency=medium + + * d/rabbitmq-server.dirs: Restore missing /etc/rabbitmq directory + (LP: #1410155). + + -- James Page Tue, 13 Jan 2015 09:53:47 +0000 + +rabbitmq-server (3.4.2-1) unstable; urgency=medium + + [ James Page ] + * New upstream point release. + * d/control: Update for new maintainer information, add VCS repository + locations. + * d/source/format: Switch packaging to source format 3.0 (quilt). + * d/compat,control: Bump debhelper compat level to 9. + * d/*: wrap-and-sort. + * d/*: Move to standard debhelper, drop use of cdbs. + * d/rules,control,rabbitmq-server.service: Add systemd service + configuration. + * d/control: Bumped Standards-Version 3.9.6, no changes. + + [ Thomas Goirand ] + * d/copyright: Rewrote as format 1.0. + + -- James Page Fri, 19 Dec 2014 11:09:20 +0000 + +rabbitmq-server (3.4.1-1) unstable; urgency=high + + * New upstream release. + + -- Blair Hester Tue, 04 Nov 2014 07:33:44 +0100 + +rabbitmq-server (3.3.5-1) unstable; urgency=low + + * New upstream release: + - Provides unminimized versions of all bundled Javascript + libraries (Closes: #736781). + * d/control: Added Blair Hester to Uploaders, dropped Emile Joubert + (thanks for all your work Emile!). + + -- Blair Hester Tue, 12 Aug 2014 11:47:14 +0100 + +rabbitmq-server (3.3.4-1) unstable; urgency=low + + * New upstream release + + -- Emile Joubert Tue, 24 Jun 2014 18:00:48 +0100 + +rabbitmq-server (3.3.3-1) unstable; urgency=low + + * New upstream release + + -- Emile Joubert Tue, 17 Jun 2014 16:59:14 +0100 + +rabbitmq-server (3.3.1-1) unstable; urgency=low + + * New upstream release + + -- Emile Joubert Tue, 29 Apr 2014 21:05:49 +0100 + +rabbitmq-server (3.3.0-1) unstable; urgency=low + + * New upstream release + + -- Emile Joubert Wed, 02 Apr 2014 16:23:08 +0100 + +rabbitmq-server (3.2.4-1.1) unstable; urgency=high + + * Non-maintainer upload. + * Bind on 127.0.0.1 by default, to avoid listening on all ipv6 interface with + guest/guest as default configured user. Note that this only fixes *new* + installation, and that any already existing setup will have to edit the + /etc/rabbitmq/rabbitmq-env.conf manually if affected. (Closes: #727607) + * Removed useless and deprecated DM-Upload field. + * Cleans plugins-src/rabbitmq-server to be able to build twice. Also cleans + debian/postrm which is generated from debian/postrm.in and plugins/README. + + -- Thomas Goirand Mon, 31 Mar 2014 06:11:46 +0000 + +rabbitmq-server (3.2.4-1) unstable; urgency=low + + * New upstream release + + -- Emile Joubert Tue, 04 Mar 2014 13:21:45 +0000 + +rabbitmq-server (3.2.3-1) unstable; urgency=low + + * New upstream release + + -- Emile Joubert Thu, 23 Jan 2014 16:49:45 +0000 + +rabbitmq-server (3.2.2-1) unstable; urgency=low + + * New upstream release + + -- Emile Joubert Wed, 11 Dec 2013 17:31:14 +0000 + +rabbitmq-server (3.2.1-1) unstable; urgency=low + + * New upstream release + + -- Emile Joubert Mon, 11 Nov 2013 09:49:42 +0000 + +rabbitmq-server (3.2.0-1) unstable; urgency=low + + * New upstream release + + -- Emile Joubert Wed, 23 Oct 2013 15:42:19 +0100 + +rabbitmq-server (3.1.4-1) unstable; urgency=low + + * New upstream release + + -- Emile Joubert Thu, 07 Aug 2013 15:16:28 +0100 + +rabbitmq-server (3.1.3-1) unstable; urgency=low + + * New upstream release + + -- Emile Joubert Thu, 27 Jun 2013 14:06:11 +0100 + +rabbitmq-server (3.1.2-1) unstable; urgency=low + + * New upstream release + + -- Emile Joubert Thu, 25 Jun 2013 11:28:52 +0100 + +rabbitmq-server (3.1.1-1) unstable; urgency=low + + * New upstream release + + -- Emile Joubert Thu, 28 May 2013 11:15:13 +0100 + +rabbitmq-server (3.1.0-1) unstable; urgency=low + + * New upstream release + + -- Emile Joubert Thu, 02 May 2013 11:19:31 +0100 + +rabbitmq-server (3.0.4-1) unstable; urgency=low + + * New upstream release + + -- Emile Joubert Wed, 13 Mar 2013 10:53:18 +0000 + +rabbitmq-server (3.0.4-1) unstable; urgency=low + + * New upstream release + + -- Emile Joubert Wed, 13 Mar 2013 10:53:18 +0000 + +rabbitmq-server (3.0.3-1) unstable; urgency=low + + * New upstream release + + -- Emile Joubert Thu, 07 Mar 2013 10:03:31 +0000 + +rabbitmq-server (3.0.2-1) unstable; urgency=low + + * New upstream release + + -- Emile Joubert Tue, 31 Jan 2013 15:28:12 +0000 + +rabbitmq-server (3.0.1-1) unstable; urgency=low + + * New upstream release + + -- Emile Joubert Tue, 11 Dec 2012 15:47:52 +0000 + +rabbitmq-server (3.0.0-1) unstable; urgency=low + + * New upstream release + + -- Emile Joubert Mon, 19 Nov 2012 11:42:31 +0000 + +rabbitmq-server (2.8.7-1) unstable; urgency=low + + * New upstream release + + -- Emile Joubert Thu, 27 Sep 2012 16:28:21 +0100 + +rabbitmq-server (2.8.6-1) unstable; urgency=low + + * New upstream release + + -- Emile Joubert Wed, 22 Aug 2012 13:28:21 +0100 + +rabbitmq-server (2.8.5-1) unstable; urgency=low + + * New upstream release + + -- Emile Joubert Thu, 02 Aug 2012 16:12:21 +0100 + +rabbitmq-server (2.8.4-1) unstable; urgency=low + + * New upstream release + + -- Emile Joubert Fri, 22 Jun 2012 17:48:28 +0100 + +rabbitmq-server (2.8.3-1) unstable; urgency=low + + * New upstream release + + -- Emile Joubert Thu, 21 Jun 2012 13:38:57 +0100 + +rabbitmq-server (2.8.2-2) unstable; urgency=low + + * Add version numbers to plugins + + -- Emile Joubert Tue, 01 May 2012 10:48:57 +0100 + +rabbitmq-server (2.8.2-1) unstable; urgency=low + + * New upstream release + + -- Emile Joubert Mon, 30 Apr 2012 14:07:32 +0100 + +rabbitmq-server (2.8.1-1) unstable; urgency=low + + * New upstream release + + -- Emile Joubert Fri, 23 Mar 2012 10:05:24 +0000 + +rabbitmq-server (2.8.0-1) unstable; urgency=low + + * New upstream release + + -- Emile Joubert Tue, 20 Mar 2012 11:55:10 +0000 + +rabbitmq-server (2.6.1-2) unstable; urgency=low + + * Add DM-Upload-Allowed flag to control file to allow Maintainer uploads + + -- John Leuner Mon, 19 Mar 2012 21:13:54 +0200 + +rabbitmq-server (2.6.1-1) unstable; urgency=low + + * New upstream release + + -- John Leuner Tue, 27 Sep 2011 17:53:57 +0200 + +rabbitmq-server (2.5.0-1) unstable; urgency=low + + * New upstream release + + -- John Leuner Thu, 16 Jun 2011 09:55:40 +0200 + +rabbitmq-server (2.4.1-1) unstable; urgency=low + + * New upstream release + + -- John Leuner Sat, 09 Apr 2011 09:34:06 +0200 + +rabbitmq-server (2.4.0-1) unstable; urgency=low + + * New upstream release + + -- John Leuner Wed, 23 Mar 2011 21:11:17 +0200 + +rabbitmq-server (2.3.1-1) unstable; urgency=low + + * New upstream release, closes: #611253 + + -- John Leuner Sat, 05 Feb 2011 10:21:16 +0200 + +rabbitmq-server (2.2.0-1) unstable; urgency=low + + * New upstream release + + -- John Leuner Thu, 02 Dec 2010 20:41:53 +0200 + +rabbitmq-server (2.1.0-1) unstable; urgency=low + + * New upstream release + + -- John Leuner Mon, 27 Sep 2010 20:28:06 +0200 + +rabbitmq-server (2.0.0-2) unstable; urgency=low + + * Fix various scripts that were not updated correctly in + - the 2.0.0-1 package, closes: #594724 + + -- John Leuner Thu, 02 Sep 2010 18:01:37 +0200 + +rabbitmq-server (2.0.0-1) unstable; urgency=low + + * New upstream release + + -- John Leuner Sat, 28 Aug 2010 11:21:48 +0200 + +rabbitmq-server (1.8.1-1) unstable; urgency=low + + * New upstream release + + -- John Leuner Sun, 01 Aug 2010 15:47:46 +0200 + +rabbitmq-server (1.8.0-1) unstable; urgency=low + + * New upstream release + + -- John Leuner Thu, 24 Jun 2010 18:43:04 +0200 + +rabbitmq-server (1.7.0-3) unstable; urgency=low + + * Add missing entries in rabbitmq-server.init + + -- John Leuner Sun, 25 Oct 2009 10:21:25 +0200 + +rabbitmq-server (1.7.0-2) unstable; urgency=low + + * moved debian/init.d to rabbitmq-server.init + * included fixes to rabbitmq-script-wrapper + + -- John Leuner Wed, 14 Oct 2009 12:23:52 +0200 + +rabbitmq-server (1.7.0-1) unstable; urgency=low + + * New upstream release + + -- John Leuner Sat, 10 Oct 2009 13:28:39 +0200 + +rabbitmq-server (1.6.0-1) unstable; urgency=low + + * New upstream release + + -- John Leuner Sat, 20 Jun 2009 12:22:17 +0200 + +rabbitmq-server (1.5.5-3) unstable; urgency=low + + * debian/control: Reduce Erlang dependencies to just + erlang-mnesia and erlang-os-mon, closes: #532867 + + -- John Leuner Tue, 16 Jun 2009 14:15:20 +0200 + +rabbitmq-server (1.5.5-2) unstable; urgency=low + + * Include updates to debian package by rabbit team: + - quiet log rotate + - update build-depends and depends for new erlang packages + - debian/watch file + - add rabbitmq-script-wrapper + - update init.d scripts + - clean /etc/rabbitmq in postrm + + -- John Leuner Wed, 03 Jun 2009 12:23:50 +0200 + +rabbitmq-server (1.5.5-1) unstable; urgency=low + + * New upstream release + + -- John Leuner Tue, 02 Jun 2009 12:53:32 +0200 + +rabbitmq-server (1.5.4-4) unstable; urgency=low + + * Add new dependency on erlang-os-mon to work with new erlang packages in + debian + + -- John Leuner Mon, 11 May 2009 21:20:04 +0200 + +rabbitmq-server (1.5.4-3) unstable; urgency=low + + * Previous partial upload failed + + -- John Leuner Fri, 10 Apr 2009 20:25:51 +0200 + +rabbitmq-server (1.5.4-2) unstable; urgency=low + + * Fix incorrect changed-by field in .changes file + + -- John Leuner Fri, 10 Apr 2009 19:40:04 +0200 + +rabbitmq-server (1.5.4-1) unstable; urgency=low + + * New upstream release + + -- John Leuner Fri, 10 Apr 2009 17:54:21 +0200 + +rabbitmq-server (1.5.0-5) unstable; urgency=low + + * Include the full license for amqp-0.8.json in debian/copyright + + -- John Leuner Fri, 27 Feb 2009 16:16:54 +0200 + +rabbitmq-server (1.5.0-4) unstable; urgency=low + + * Clarify and explicitly list the license and copyright for + codegen/amqp-0.8.json + + * Explicitly list the authors and copyright for the rest of the codebase + at the top of debian/copyright + + * Clarify the copyright of the files in src/tcp_* . The rabbitmq authors have + confirmed that they are the original authors of this code and that the + files at http://code.google.com/p/cacherl/ were taken without their + knowledge from the rabbitmq project. (Comparing the commit dates in version + control shows that cacherl is newer) + + -- John Leuner Wed, 25 Feb 2009 13:10:15 +0200 + +rabbitmq-server (1.5.0-3) unstable; urgency=low + + * Previous changelog entry had an incorrect Maintainer name + + -- John Leuner Wed, 28 Jan 2009 16:45:33 +0200 + +rabbitmq-server (1.5.0-2) unstable; urgency=low + + * Reupload package to unstable. Mistakenly uploaded to testing last time, closes: #507902 + + -- John Leuner Mon, 19 Jan 2009 17:38:43 +0200 + +rabbitmq-server (1.5.0-1) testing; urgency=low + + * New Upstream Release + * First Debian upload, closes: #507902 + + -- John Leuner Wed, 17 Dec 2008 18:23:47 +0000 diff --git a/debian/compat b/debian/compat new file mode 100644 index 0000000..ec63514 --- /dev/null +++ b/debian/compat @@ -0,0 +1 @@ +9 diff --git a/debian/control b/debian/control new file mode 100644 index 0000000..dd61545 --- /dev/null +++ b/debian/control @@ -0,0 +1,30 @@ +Source: rabbitmq-server +Section: net +Priority: extra +Maintainer: PKG OpenStack +Uploaders: James Page , Thomas Goirand +Build-Depends: debhelper (>= 9~), + dh-systemd (>= 1.5), + erlang-dev, + erlang-nox (>= 1:13.b.3), + erlang-src (>= 1:13.b.3), + python-simplejson, + unzip, + xmlto, + xsltproc, + zip +Standards-Version: 3.9.6 +Vcs-Browser: http://anonscm.debian.org/gitweb/?p=openstack/rabbitmq-server.git +Vcs-Git: git://anonscm.debian.org/openstack/rabbitmq-server.git +Homepage: http://www.rabbitmq.com/ + +Package: rabbitmq-server +Architecture: all +Depends: adduser, + erlang-nox (>= 1:13.b.3) | esl-erlang, + logrotate, + ${misc:Depends} +Description: AMQP server written in Erlang + RabbitMQ is an implementation of AMQP, the emerging standard for high + performance enterprise messaging. The RabbitMQ server is a robust and + scalable implementation of an AMQP broker. diff --git a/debian/copyright b/debian/copyright new file mode 100644 index 0000000..3ce58ff --- /dev/null +++ b/debian/copyright @@ -0,0 +1,564 @@ +Format: http://www.debian.org/doc/packaging-manuals/copyright-format/1.0/ +Upstream-Name: RabbitMQ +Source: http://www.rabbitmq.com/ + +Files: debian/* +Copyright: (c) 2007-2013, GoPivotal, Inc. + (c) 2007, Tony Garnock-Jones + (c) 2014, Blair Hester + (c) 2012-2014, Emile Joubert + (c) 2008-2012, John Leuner + (c) 2014, James Page + (c) 2014, Thomas Goirand +License: MPL-1.1 + +Files: codegen/amqp-rabbitmq-*.json +Copyright: (c) 2008-2013, GoPivotal Inc. +License: Expat + +Files: plugins-src/rabbitmq-management/priv/www/js/jquery*.js +Copyright: (c) 2010 John Resig +License: Expat +Comments: Downloaded from http://jquery.com/ + +Files: plugins-src/rabbitmq-management/priv/www/js/ejs* + plugins-src/rabbitmq-management/priv/www/js/tmpl +Copyright: (c) 2007, Edward Benson +License: Expat +Comments: downloaded from http://embeddedjs.com/ + +Files: plugins-src/rabbitmq-management/priv/www/js/sammy*.js +Copyright: (c) 2008 Aaron Quint, Quirkey NYC, LLC +License: Expat +Comments: Downloaded from http://code.quirkey.com/sammy/ + +Files: plugins-src/rabbitmq-management/priv/www/js/excanvas*.js +Copyright: (c) 2006, Google Inc +License: Apache-2.0 +Comments: Downloaded from http://code.google.com/p/explorercanvas/ + +Files: plugins-src/rabbitmq-management/priv/www/js/jquery.flot*.js +Copyright: (c) 2007-2013, IOLA and Ole Laursen +License: Expat +Comments: Downloaded from http://www.flotcharts.org/ + +Files: plugins-src/webmachine-wrapper/* +Copyright: (c) Basho Technologies +License: Apache-2.0 +Comments: Downloaded from http://webmachine.basho.com/ + +Files: plugins-src/eldap-wrapper/* +Copyright: (c) 2010, Torbjorn Tornkvist +License: Expat +Comments: Downloaded from https://github.com/etnt/eldap + +Files: plugins-src/mochiweb-wrapper/mochiweb-git/* +Copyright: (c) 2007, Mochi Media, Inc. +License: Expat +Comments: Downloaded from http://github.com/mochi/mochiweb/ + +Files: + plugins-src/rabbitmq-management-visualiser/priv/www/visualiser/js/glMatrix*.js +Copyright: (c) 2011, Brandon Jones +License: BSD-2-Clause +Comments: Downloaded from http://code.google.com/p/glmatrix/ + +Files: * +Copyright: (c) 2007-2014 GoPivotal, Inc. +License: MPL-1.1 + +License: Expat + Permission is hereby granted, free of charge, to any person obtaining a copy + of this file (the Software), to deal in the Software without restriction, + including without limitation the rights to use, copy, modify, merge, publish, + distribute, sublicense, and/or sell copies of the Software, and to permit + persons to whom the Software is furnished to do so, subject to the following + conditions: + . + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + . + THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + IN THE SOFTWARE." + +License: BSD-2-Clause + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + . + 1. Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + . + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + . + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + POSSIBILITY OF SUCH DAMAGE." + +License: MPL-1.1 + MOZILLA PUBLIC LICENSE Version 1.1 + 1. Definitions. + . + 1.0.1. "Commercial Use" means distribution or otherwise making the Covered + Code available to a third party. + . + 1.1. "Contributor" means each entity that creates or contributes to the + creation of Modifications. + . + 1.2. "Contributor Version" means the combination of the Original Code, prior + Modifications used by a Contributor, and the Modifications made by that + particular Contributor. + . + 1.3. "Covered Code" means the Original Code or Modifications or the + combination of the Original Code and Modifications, in each case including + portions thereof. + . + 1.4. "Electronic Distribution Mechanism" means a mechanism generally accepted + in the software development community for the electronic transfer of data. + . + 1.5. "Executable" means Covered Code in any form other than Source Code. + . + 1.6. "Initial Developer" means the individual or entity identified as the + Initial Developer in the Source Code notice required by Exhibit A. + . + 1.7. "Larger Work" means a work which combines Covered Code or portions + thereof with code not governed by the terms of this License. + . + 1.8. "License" means this document. + . + 1.8.1. "Licensable" means having the right to grant, to the maximum extent + possible, whether at the time of the initial grant or subsequently acquired, + any and all of the rights conveyed herein. + . + 1.9. "Modifications" means any addition to or deletion from the substance or + structure of either the Original Code or any previous Modifications. When + Covered Code is released as a series of files, a Modification is: + . + A. Any addition to or deletion from the contents of a file containing + Original Code or previous Modifications. + . + B. Any new file that contains any part of the Original Code or previous + Modifications. + . + 1.10. "Original Code" means Source Code of computer software code which is + described in the Source Code notice required by Exhibit A as Original Code, + and which, at the time of its release under this License is not already + Covered Code governed by this License. + . + 1.10.1. "Patent Claims" means any patent claim(s), now owned or hereafter + acquired, including without limitation, method, process, and apparatus + claims, in any patent Licensable by grantor. + . + 1.11. "Source Code" means the preferred form of the Covered Code for making + modifications to it, including all modules it contains, plus any associated + interface definition files, scripts used to control compilation and + installation of an Executable, or source code differential comparisons + against either the Original Code or another well known, available Covered + Code of the Contributor's choice. The Source Code can be in a compressed or + archival form, provided the appropriate decompression or de-archiving + software is widely available for no charge. + . + 1.12. "You" (or "Your") means an individual or a legal entity exercising + rights under, and complying with all of the terms of, this License or a + future version of this License issued under Section 6.1. For legal entities, + "You" includes any entity which controls, is controlled by, or is under + common control with You. For purposes of this definition, "control" means (a) + the power, direct or indirect, to cause the direction or management of such + entity, whether by contract or otherwise, or (b) ownership of more than fifty + percent (50%) of the outstanding shares or beneficial ownership of such + entity. + . + 2. Source Code License. + . + 2.1. The Initial Developer Grant. + The Initial Developer hereby grants You a world-wide, royalty-free, + non-exclusive license, subject to third party intellectual property claims: + . + (a) under intellectual property rights (other than patent or + trademark) Licensable by Initial Developer to use, reproduce, + modify, display, perform, sublicense and distribute the Original + Code (or portions thereof) with or without Modifications, and/or + as part of a Larger Work; and + . + (b) under Patents Claims infringed by the making, using or + selling of Original Code, to make, have made, use, practice, + sell, and offer for sale, and/or otherwise dispose of the + Original Code (or portions thereof). + . + (c) the licenses granted in this Section 2.1(a) and (b) are + effective on the date Initial Developer first distributes + Original Code under the terms of this License. + . + (d) Notwithstanding Section 2.1(b) above, no patent license is + granted: 1) for code that You delete from the Original Code; 2) + separate from the Original Code; or 3) for infringements caused + by: i) the modification of the Original Code or ii) the + combination of the Original Code with other software or devices. + . + 2.2. Contributor Grant. + Subject to third party intellectual property claims, each Contributor hereby + grants You a world-wide, royalty-free, non-exclusive license + . + (a) under intellectual property rights (other than patent or + trademark) Licensable by Contributor, to use, reproduce, modify, + display, perform, sublicense and distribute the Modifications + created by such Contributor (or portions thereof) either on an + unmodified basis, with other Modifications, as Covered Code + and/or as part of a Larger Work; and + . + (b) under Patent Claims infringed by the making, using, or + selling of Modifications made by that Contributor either alone + and/or in combination with its Contributor Version (or portions + of such combination), to make, use, sell, offer for sale, have + made, and/or otherwise dispose of: 1) Modifications made by that + Contributor (or portions thereof); and 2) the combination of + Modifications made by that Contributor with its Contributor + Version (or portions of such combination). + . + (c) the licenses granted in Sections 2.2(a) and 2.2(b) are + effective on the date Contributor first makes Commercial Use of + the Covered Code. + . + (d) Notwithstanding Section 2.2(b) above, no patent license is + granted: 1) for any code that Contributor has deleted from the + Contributor Version; 2) separate from the Contributor Version; + 3) for infringements caused by: i) third party modifications of + Contributor Version or ii) the combination of Modifications made + by that Contributor with other software (except as part of the + Contributor Version) or other devices; or 4) under Patent Claims + infringed by Covered Code in the absence of Modifications made by + that Contributor. + . + 3. Distribution Obligations. + . + 3.1. Application of License. + . + The Modifications which You create or to which You contribute are governed by + the terms of this License, including without limitation Section 2.2. The + Source Code version of Covered Code may be distributed only under the terms of + this License or a future version of this License released under Section 6.1, + and You must include a copy of this License with every copy of the Source Code + You distribute. You may not offer or impose any terms on any Source Code + version that alters or restricts the applicable version of this License or the + recipients' rights hereunder. However, You may include an additional document + offering the additional rights described in Section 3.5. + . + 3.2. Availability of Source Code. + . + Any Modification which You create or to which You contribute must be made + available in Source Code form under the terms of this License either on the + same media as an Executable version or via an accepted Electronic Distribution + Mechanism to anyone to whom you made an Executable version available; and if + made available via Electronic Distribution Mechanism, must remain available + for at least twelve (12) months after the date it initially became available, + or at least six (6) months after a subsequent version of that particular + Modification has been made available to such recipients. You are responsible + for ensuring that the Source Code version remains available even if the + Electronic Distribution Mechanism is maintained by a third party. + . + 3.3. Description of Modifications. + . + You must cause all Covered Code to which You contribute to contain a file + documenting the changes You made to create that Covered Code and the date of + any change. You must include a prominent statement that the Modification is + derived, directly or indirectly, from Original Code provided by the Initial + Developer and including the name of the Initial Developer in (a) the Source + Code, and (b) in any notice in an Executable version or related documentation + in which You describe the origin or ownership of the Covered Code. + . + 3.4. Intellectual Property Matters + . + (a) Third Party Claims. + . + If Contributor has knowledge that a license under a third party's + intellectual property rights is required to exercise the rights + granted by such Contributor under Sections 2.1 or 2.2, + Contributor must include a text file with the Source Code + distribution titled "LEGAL" which describes the claim and the + party making the claim in sufficient detail that a recipient will + know whom to contact. If Contributor obtains such knowledge after + the Modification is made available as described in Section 3.2, + Contributor shall promptly modify the LEGAL file in all copies + Contributor makes available thereafter and shall take other steps + (such as notifying appropriate mailing lists or newsgroups) + reasonably calculated to inform those who received the Covered + Code that new knowledge has been obtained. + . + (b) Contributor APIs. + . + If Contributor's Modifications include an application programming + interface and Contributor has knowledge of patent licenses which + are reasonably necessary to implement that API, Contributor must + also include this information in the LEGAL file. + . + (c) Representations. + . + Contributor represents that, except as disclosed pursuant to + Section 3.4(a) above, Contributor believes that Contributor's + Modifications are Contributor's original creation(s) and/or + Contributor has sufficient rights to grant the rights conveyed by + this License. + . + 3.5. Required Notices. + . + You must duplicate the notice in Exhibit A in each file of the Source + Code. If it is not possible to put such notice in a particular Source + Code file due to its structure, then You must include such notice in a + location (such as a relevant directory) where a user would be likely + to look for such a notice. If You created one or more Modification(s) + You may add your name as a Contributor to the notice described in + Exhibit A. You must also duplicate this License in any documentation + for the Source Code where You describe recipients' rights or ownership + rights relating to Covered Code. You may choose to offer, and to + charge a fee for, warranty, support, indemnity or liability + obligations to one or more recipients of Covered Code. However, You + may do so only on Your own behalf, and not on behalf of the Initial + Developer or any Contributor. You must make it absolutely clear than + any such warranty, support, indemnity or liability obligation is + offered by You alone, and You hereby agree to indemnify the Initial + Developer and every Contributor for any liability incurred by the + Initial Developer or such Contributor as a result of warranty, + support, indemnity or liability terms You offer. + . + 3.6. Distribution of Executable Versions. + . + You may distribute Covered Code in Executable form only if the + requirements of Section 3.1-3.5 have been met for that Covered Code, + and if You include a notice stating that the Source Code version of + the Covered Code is available under the terms of this License, + including a description of how and where You have fulfilled the + obligations of Section 3.2. The notice must be conspicuously included + in any notice in an Executable version, related documentation or + collateral in which You describe recipients' rights relating to the + Covered Code. You may distribute the Executable version of Covered + Code or ownership rights under a license of Your choice, which may + contain terms different from this License, provided that You are in + compliance with the terms of this License and that the license for the + Executable version does not attempt to limit or alter the recipient's + rights in the Source Code version from the rights set forth in this + License. If You distribute the Executable version under a different + license You must make it absolutely clear that any terms which differ + from this License are offered by You alone, not by the Initial + Developer or any Contributor. You hereby agree to indemnify the + Initial Developer and every Contributor for any liability incurred by + the Initial Developer or such Contributor as a result of any such + terms You offer. + . + 3.7. Larger Works. + . + You may create a Larger Work by combining Covered Code with other code + not governed by the terms of this License and distribute the Larger + Work as a single product. In such a case, You must make sure the + requirements of this License are fulfilled for the Covered Code. + . + 4. Inability to Comply Due to Statute or Regulation. + . + If it is impossible for You to comply with any of the terms of this + License with respect to some or all of the Covered Code due to + statute, judicial order, or regulation then You must: (a) comply with + the terms of this License to the maximum extent possible; and (b) + describe the limitations and the code they affect. Such description + must be included in the LEGAL file described in Section 3.4 and must + be included with all distributions of the Source Code. Except to the + extent prohibited by statute or regulation, such description must be + sufficiently detailed for a recipient of ordinary skill to be able to + understand it. + . + 5. Application of this License. + . + This License applies to code to which the Initial Developer has + attached the notice in Exhibit A and to related Covered Code. + . + 6. Versions of the License. + . + 6.1. New Versions. + . + Netscape Communications Corporation ("Netscape") may publish revised + and/or new versions of the License from time to time. Each version + will be given a distinguishing version number. + . + 6.2. Effect of New Versions. + . + Once Covered Code has been published under a particular version of the + License, You may always continue to use it under the terms of that + version. You may also choose to use such Covered Code under the terms + of any subsequent version of the License published by Netscape. No one + other than Netscape has the right to modify the terms applicable to + Covered Code created under this License. + . + 6.3. Derivative Works. + . + If You create or use a modified version of this License (which you may + only do in order to apply it to code which is not already Covered Code + governed by this License), You must (a) rename Your license so that + the phrases "Mozilla", "MOZILLAPL", "MOZPL", "Netscape", + "MPL", "NPL" or any confusingly similar phrase do not appear in your + license (except to note that your license differs from this License) + and (b) otherwise make it clear that Your version of the license + contains terms which differ from the Mozilla Public License and + Netscape Public License. (Filling in the name of the Initial + Developer, Original Code or Contributor in the notice described in + Exhibit A shall not of themselves be deemed to be modifications of + this License.) + . + 7. DISCLAIMER OF WARRANTY. + . + COVERED CODE IS PROVIDED UNDER THIS LICENSE ON AN "AS IS" BASIS, + WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, + WITHOUT LIMITATION, WARRANTIES THAT THE COVERED CODE IS FREE OF + DEFECTS, MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE OR NON-INFRINGING. + THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE COVERED CODE + IS WITH YOU. SHOULD ANY COVERED CODE PROVE DEFECTIVE IN ANY RESPECT, + YOU (NOT THE INITIAL DEVELOPER OR ANY OTHER CONTRIBUTOR) ASSUME THE + COST OF ANY NECESSARY SERVICING, REPAIR OR CORRECTION. THIS DISCLAIMER + OF WARRANTY CONSTITUTES AN ESSENTIAL PART OF THIS LICENSE. NO USE OF + ANY COVERED CODE IS AUTHORIZED HEREUNDER EXCEPT UNDER THIS DISCLAIMER. + . + 8. TERMINATION. + . + 8.1. This License and the rights granted hereunder will terminate + automatically if You fail to comply with terms herein and fail to cure + such breach within 30 days of becoming aware of the breach. All + sublicenses to the Covered Code which are properly granted shall + survive any termination of this License. Provisions which, by their + nature, must remain in effect beyond the termination of this License + shall survive. + . + 8.2. If You initiate litigation by asserting a patent infringement + claim (excluding declatory judgment actions) against Initial Developer + or a Contributor (the Initial Developer or Contributor against whom + You file such action is referred to as "Participant") alleging that: + . + (a) such Participant's Contributor Version directly or indirectly + infringes any patent, then any and all rights granted by such + Participant to You under Sections 2.1 and/or 2.2 of this License + shall, upon 60 days notice from Participant terminate prospectively, + unless if within 60 days after receipt of notice You either: (i) + agree in writing to pay Participant a mutually agreeable reasonable + royalty for Your past and future use of Modifications made by such + Participant, or (ii) withdraw Your litigation claim with respect to + the Contributor Version against such Participant. If within 60 days + of notice, a reasonable royalty and payment arrangement are not + mutually agreed upon in writing by the parties or the litigation claim + is not withdrawn, the rights granted by Participant to You under + Sections 2.1 and/or 2.2 automatically terminate at the expiration of + the 60 day notice period specified above. + . + (b) any software, hardware, or device, other than such Participant's + Contributor Version, directly or indirectly infringes any patent, then + any rights granted to You by such Participant under Sections 2.1(b) + and 2.2(b) are revoked effective as of the date You first made, used, + sold, distributed, or had made, Modifications made by that + Participant. + . + 8.3. If You assert a patent infringement claim against Participant + alleging that such Participant's Contributor Version directly or + indirectly infringes any patent where such claim is resolved (such as + by license or settlement) prior to the initiation of patent + infringement litigation, then the reasonable value of the licenses + granted by such Participant under Sections 2.1 or 2.2 shall be taken + into account in determining the amount or value of any payment or + license. + . + 8.4. In the event of termination under Sections 8.1 or 8.2 above, + all end user license agreements (excluding distributors and resellers) + which have been validly granted by You or any distributor hereunder + prior to termination shall survive termination. + . + 9. LIMITATION OF LIABILITY. + . + UNDER NO CIRCUMSTANCES AND UNDER NO LEGAL THEORY, WHETHER TORT + (INCLUDING NEGLIGENCE), CONTRACT, OR OTHERWISE, SHALL YOU, THE INITIAL + DEVELOPER, ANY OTHER CONTRIBUTOR, OR ANY DISTRIBUTOR OF COVERED CODE, + OR ANY SUPPLIER OF ANY OF SUCH PARTIES, BE LIABLE TO ANY PERSON FOR + ANY INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES OF ANY + CHARACTER INCLUDING, WITHOUT LIMITATION, DAMAGES FOR LOSS OF GOODWILL, + WORK STOPPAGE, COMPUTER FAILURE OR MALFUNCTION, OR ANY AND ALL OTHER + COMMERCIAL DAMAGES OR LOSSES, EVEN IF SUCH PARTY SHALL HAVE BEEN + INFORMED OF THE POSSIBILITY OF SUCH DAMAGES. THIS LIMITATION OF + LIABILITY SHALL NOT APPLY TO LIABILITY FOR DEATH OR PERSONAL INJURY + RESULTING FROM SUCH PARTY'S NEGLIGENCE TO THE EXTENT APPLICABLE LAW + PROHIBITS SUCH LIMITATION. SOME JURISDICTIONS DO NOT ALLOW THE + EXCLUSION OR LIMITATION OF INCIDENTAL OR CONSEQUENTIAL DAMAGES, SO + THIS EXCLUSION AND LIMITATION MAY NOT APPLY TO YOU. + . + 10. U.S. GOVERNMENT END USERS. + . + The Covered Code is a "commercial item," as that term is defined in + 48 C.F.R. 2.101 (Oct. 1995), consisting of "commercial computer + software" and "commercial computer software documentation," as such + terms are used in 48 C.F.R. 12.212 (Sept. 1995). Consistent with 48 + C.F.R. 12.212 and 48 C.F.R. 227.7202-1 through 227.7202-4 (June 1995), + all U.S. Government End Users acquire Covered Code with only those + rights set forth herein. + . + 11. MISCELLANEOUS. + . + This License represents the complete agreement concerning subject + matter hereof. If any provision of this License is held to be + unenforceable, such provision shall be reformed only to the extent + necessary to make it enforceable. This License shall be governed by + California law provisions (except to the extent applicable law, if + any, provides otherwise), excluding its conflict-of-law provisions. + With respect to disputes in which at least one party is a citizen of, + or an entity chartered or registered to do business in the United + States of America, any litigation relating to this License shall be + subject to the jurisdiction of the Federal Courts of the Northern + District of California, with venue lying in Santa Clara County, + California, with the losing party responsible for costs, including + without limitation, court costs and reasonable attorneys' fees and + expenses. The application of the United Nations Convention on + Contracts for the International Sale of Goods is expressly excluded. + Any law or regulation which provides that the language of a contract + shall be construed against the drafter shall not apply to this + License. + . + 12. RESPONSIBILITY FOR CLAIMS. + . + As between Initial Developer and the Contributors, each party is + responsible for claims and damages arising, directly or indirectly, + out of its utilization of rights under this License and You agree to + work with Initial Developer and Contributors to distribute such + responsibility on an equitable basis. Nothing herein is intended or + shall be deemed to constitute any admission of liability. + . + 13. MULTIPLE-LICENSED CODE. + . + Initial Developer may designate portions of the Covered Code as + "Multiple-Licensed". "Multiple-Licensed" means that the Initial + Developer permits you to utilize portions of the Covered Code under + Your choice of the NPL or the alternative licenses, if any, specified + by the Initial Developer in the file described in Exhibit A. + . + EXHIBIT A -Mozilla Public License. + . + The contents of this file are subject to the Mozilla Public License Version + 1.1 (the "License"); you may not use this file except in compliance with the + License. You may obtain a copy of the License at http://www.mozilla.org/MPL/ + . + Software distributed under the License is distributed on an "AS IS" basis, + WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for + the specific language governing rights and limitations under the License. + . + The Original Code is RabbitMQ. + . + The Initial Developer of the Original Code is GoPivotal, Inc. Copyright (c) + 2007-2014 GoPivotal, Inc. All rights reserved. + +License: Apache-2.0 + On Debian GNU/Linux system you can find the complete text of the + Apache-2.0 license in '/usr/share/common-licenses/Apache-2.0' diff --git a/debian/gbp.conf b/debian/gbp.conf new file mode 100644 index 0000000..ee339ed --- /dev/null +++ b/debian/gbp.conf @@ -0,0 +1,6 @@ +[DEFAULT] +debian-branch = master +pristine-tar = True + +[buildpackage] +export-dir = ../build-area/ diff --git a/debian/ocf/rabbitmq-server b/debian/ocf/rabbitmq-server new file mode 100755 index 0000000..c927971 --- /dev/null +++ b/debian/ocf/rabbitmq-server @@ -0,0 +1,371 @@ +#!/bin/sh +## The contents of this file are subject to the Mozilla Public License +## Version 1.1 (the "License"); you may not use this file except in +## compliance with the License. You may obtain a copy of the License +## at http://www.mozilla.org/MPL/ +## +## Software distributed under the License is distributed on an "AS IS" +## basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See +## the License for the specific language governing rights and +## limitations under the License. +## +## The Original Code is RabbitMQ. +## +## The Initial Developer of the Original Code is GoPivotal, Inc. +## Copyright (c) 2007-2014 GoPivotal, Inc. All rights reserved. +## + +## +## OCF Resource Agent compliant rabbitmq-server resource script. +## + +## OCF instance parameters +## OCF_RESKEY_server +## OCF_RESKEY_ctl +## OCF_RESKEY_nodename +## OCF_RESKEY_ip +## OCF_RESKEY_port +## OCF_RESKEY_config_file +## OCF_RESKEY_log_base +## OCF_RESKEY_mnesia_base +## OCF_RESKEY_server_start_args +## OCF_RESKEY_pid_file + +####################################################################### +# Initialization: + +: ${OCF_FUNCTIONS_DIR=${OCF_ROOT}/resource.d/heartbeat} +. ${OCF_FUNCTIONS_DIR}/.ocf-shellfuncs + +####################################################################### + +OCF_RESKEY_server_default="/usr/sbin/rabbitmq-server" +OCF_RESKEY_ctl_default="/usr/sbin/rabbitmqctl" +OCF_RESKEY_nodename_default="rabbit@localhost" +OCF_RESKEY_log_base_default="/var/log/rabbitmq" +OCF_RESKEY_pid_file_default="/var/run/rabbitmq/pid" +: ${OCF_RESKEY_server=${OCF_RESKEY_server_default}} +: ${OCF_RESKEY_ctl=${OCF_RESKEY_ctl_default}} +: ${OCF_RESKEY_nodename=${OCF_RESKEY_nodename_default}} +: ${OCF_RESKEY_log_base=${OCF_RESKEY_log_base_default}} +: ${OCF_RESKEY_pid_file=${OCF_RESKEY_pid_file_default}} + +meta_data() { + cat < + + +1.0 + + +Resource agent for RabbitMQ-server + + +Resource agent for RabbitMQ-server + + + + +The path to the rabbitmq-server script + +Path to rabbitmq-server + + + + + +The path to the rabbitmqctl script + +Path to rabbitmqctl + + + + + +The node name for rabbitmq-server + +Node name + + + + + +The IP address for rabbitmq-server to listen on + +IP Address + + + + + +The IP Port for rabbitmq-server to listen on + +IP Port + + + + + +Location of the config file (without the .config suffix) + +Config file path (without the .config suffix) + + + + + +Location of the directory under which logs will be created + +Log base path + + + + + +Location of the directory under which mnesia will store data + +Mnesia base path + + + + + +Additional arguments provided to the server on startup + +Server start arguments + + + + + +Location of the file in which the pid will be stored + +Pid file path + + + + + + + + + + + + + + +END +} + +rabbit_usage() { + cat < /dev/null 2> /dev/null + rc=$? + case "$rc" in + 0) + ocf_log debug "RabbitMQ server is running normally" + return $OCF_SUCCESS + ;; + 2) + ocf_log debug "RabbitMQ server is not running" + return $OCF_NOT_RUNNING + ;; + *) + ocf_log err "Unexpected return from rabbitmqctl $NODENAME_ARG $action: $rc" + exit $OCF_ERR_GENERIC + esac +} + +rabbit_start() { + local rc + + if rabbit_status; then + ocf_log info "Resource already running." + return $OCF_SUCCESS + fi + + export_vars + + setsid sh -c "$RABBITMQ_SERVER > ${RABBITMQ_LOG_BASE}/startup_log 2> ${RABBITMQ_LOG_BASE}/startup_err" & + + # Wait for the server to come up. + # Let the CRM/LRM time us out if required + rabbit_wait $RABBITMQ_PID_FILE + rc=$? + if [ "$rc" != $OCF_SUCCESS ]; then + remove_pid + ocf_log info "rabbitmq-server start failed: $rc" + exit $OCF_ERR_GENERIC + fi + + return $OCF_SUCCESS +} + +rabbit_stop() { + local rc + + if ! rabbit_status; then + ocf_log info "Resource not running." + return $OCF_SUCCESS + fi + + $RABBITMQ_CTL stop + rc=$? + + if [ "$rc" != 0 ]; then + ocf_log err "rabbitmq-server stop command failed: $RABBITMQ_CTL stop, $rc" + return $rc + fi + + # Spin waiting for the server to shut down. + # Let the CRM/LRM time us out if required + stop_wait=1 + while [ $stop_wait = 1 ]; do + rabbit_status + rc=$? + if [ "$rc" = $OCF_NOT_RUNNING ]; then + remove_pid + stop_wait=0 + break + elif [ "$rc" != $OCF_SUCCESS ]; then + ocf_log info "rabbitmq-server stop failed: $rc" + exit $OCF_ERR_GENERIC + fi + sleep 1 + done + + return $OCF_SUCCESS +} + +rabbit_monitor() { + rabbit_status + return $? +} + +case $__OCF_ACTION in + meta-data) + meta_data + exit $OCF_SUCCESS + ;; + usage|help) + rabbit_usage + exit $OCF_SUCCESS + ;; +esac + +if ocf_is_probe; then + rabbit_validate_partial +else + rabbit_validate_full +fi + +case $__OCF_ACTION in + start) + rabbit_start + ;; + stop) + rabbit_stop + ;; + status|monitor) + rabbit_monitor + ;; + validate-all) + exit $OCF_SUCCESS + ;; + *) + rabbit_usage + exit $OCF_ERR_UNIMPLEMENTED + ;; +esac + +exit $? diff --git a/debian/rabbitmq-env.conf b/debian/rabbitmq-env.conf new file mode 100644 index 0000000..bebe2ab --- /dev/null +++ b/debian/rabbitmq-env.conf @@ -0,0 +1,13 @@ +# Defaults to rabbit. This can be useful if you want to run more than one node +# per machine - RABBITMQ_NODENAME should be unique per erlang-node-and-machine +# combination. See the clustering on a single machine guide for details: +# http://www.rabbitmq.com/clustering.html#single-machine +#NODENAME=rabbit + +# By default RabbitMQ will bind to all interfaces, on IPv4 and IPv6 if +# available. Set this if you only want to bind to one network interface or# +# address family. +#NODE_IP_ADDRESS=127.0.0.1 + +# Defaults to 5672. +#NODE_PORT=5672 diff --git a/debian/rabbitmq-script-wrapper b/debian/rabbitmq-script-wrapper new file mode 100755 index 0000000..a622ae2 --- /dev/null +++ b/debian/rabbitmq-script-wrapper @@ -0,0 +1,47 @@ +#!/bin/sh +## The contents of this file are subject to the Mozilla Public License +## Version 1.1 (the "License"); you may not use this file except in +## compliance with the License. You may obtain a copy of the License +## at http://www.mozilla.org/MPL/ +## +## Software distributed under the License is distributed on an "AS IS" +## basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See +## the License for the specific language governing rights and +## limitations under the License. +## +## The Original Code is RabbitMQ. +## +## The Initial Developer of the Original Code is GoPivotal, Inc. +## Copyright (c) 2007-2014 GoPivotal, Inc. All rights reserved. +## + +# Escape spaces and quotes, because shell is revolting. +for arg in "$@" ; do + # Escape quotes in parameters, so that they're passed through cleanly. + arg=$(sed -e 's/"/\\"/g' <<-END + $arg + END + ) + CMDLINE="${CMDLINE} \"${arg}\"" +done + +cd /var/lib/rabbitmq + +SCRIPT=`basename $0` + +if [ `id -u` = `id -u rabbitmq` -a "$SCRIPT" = "rabbitmq-server" ] ; then + /usr/lib/rabbitmq/bin/rabbitmq-server "$@" > "/var/log/rabbitmq/startup_log" 2> "/var/log/rabbitmq/startup_err" +elif [ `id -u` = `id -u rabbitmq` -o "$SCRIPT" = "rabbitmq-plugins" ] ; then + if [ -f $PWD/.erlang.cookie ] ; then + export HOME=. + fi + /usr/lib/rabbitmq/bin/${SCRIPT} "$@" +elif [ `id -u` = 0 ] ; then + su rabbitmq -s /bin/sh -c "/usr/lib/rabbitmq/bin/${SCRIPT} ${CMDLINE}" +else + /usr/lib/rabbitmq/bin/${SCRIPT} + echo + echo "Only root or rabbitmq should run ${SCRIPT}" + echo + exit 1 +fi diff --git a/debian/rabbitmq-server-wait b/debian/rabbitmq-server-wait new file mode 100755 index 0000000..cdf53e5 --- /dev/null +++ b/debian/rabbitmq-server-wait @@ -0,0 +1,22 @@ +#!/bin/sh -e +## The contents of this file are subject to the Mozilla Public License +## Version 1.1 (the "License"); you may not use this file except in +## compliance with the License. You may obtain a copy of the License +## at http://www.mozilla.org/MPL/ +## +## Software distributed under the License is distributed on an "AS IS" +## basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See +## the License for the specific language governing rights and +## limitations under the License. +## +## The Original Code is RabbitMQ. +## +## The Initial Developer of the Original Code is GoPivotal, Inc. +## Copyright (c) 2007-2014 GoPivotal, Inc. All rights reserved. +## + +# Get default settings with user overrides for (RABBITMQ_) +# Non-empty defaults should be set in rabbitmq-env +. `dirname $0`/rabbitmq-env + +/usr/lib/rabbitmq/bin/rabbitmqctl wait $RABBITMQ_PID_FILE diff --git a/debian/rabbitmq-server.default b/debian/rabbitmq-server.default new file mode 100644 index 0000000..3eebe2f --- /dev/null +++ b/debian/rabbitmq-server.default @@ -0,0 +1,16 @@ +# This file is sourced by /etc/init.d/rabbitmq-server. Its primary +# reason for existing is to allow adjustment of system limits for the +# rabbitmq-server process. +# +# Maximum number of open file handles. This will need to be increased +# to handle many simultaneous connections. Refer to the system +# documentation for ulimit (in man bash) for more information. +# +# Default value is 1024, but it's absolutely unreasonable default in a +# modern world, as rabbitmq consumes file descriptors for everything: +# for client connections, for per-queue files, etc. Its documentation +# recommends to allow for at least 65536 open file descriptors in +# production. Values were chosen in +# https://bugs.launchpad.net/fuel/+bug/1279594 +ulimit -H -n 105472 +ulimit -S -n 102400 diff --git a/debian/rabbitmq-server.dirs b/debian/rabbitmq-server.dirs new file mode 100644 index 0000000..e6127a0 --- /dev/null +++ b/debian/rabbitmq-server.dirs @@ -0,0 +1,3 @@ +usr/lib/erlang/lib +var/lib/rabbitmq/mnesia +var/log/rabbitmq diff --git a/debian/rabbitmq-server.init b/debian/rabbitmq-server.init new file mode 100644 index 0000000..b2d3f86 --- /dev/null +++ b/debian/rabbitmq-server.init @@ -0,0 +1,187 @@ +#!/bin/sh +# +# rabbitmq-server RabbitMQ broker +# +# chkconfig: - 80 05 +# description: Enable AMQP service provided by RabbitMQ +# + +### BEGIN INIT INFO +# Provides: rabbitmq-server +# Required-Start: $remote_fs $network +# Required-Stop: $remote_fs $network +# Default-Start: 2 3 4 5 +# Default-Stop: 0 1 6 +# Description: RabbitMQ broker +# Short-Description: Enable AMQP service provided by RabbitMQ broker +### END INIT INFO + +PATH=/sbin:/usr/sbin:/bin:/usr/bin +NAME=rabbitmq-server +DAEMON=/usr/sbin/${NAME} +CONTROL=/usr/sbin/rabbitmqctl +DESC="message broker" +USER=rabbitmq +ROTATE_SUFFIX= +INIT_LOG_DIR=/var/log/rabbitmq +PID_FILE=/var/run/rabbitmq/pid + + +test -x $DAEMON || exit 0 +test -x $CONTROL || exit 0 + +RETVAL=0 +set -e + +[ -f /etc/default/${NAME} ] && . /etc/default/${NAME} + +. /lib/lsb/init-functions +. /lib/init/vars.sh + +ensure_pid_dir () { + PID_DIR=`dirname ${PID_FILE}` + if [ ! -d ${PID_DIR} ] ; then + mkdir -p ${PID_DIR} + chown -R ${USER}:${USER} ${PID_DIR} + chmod 755 ${PID_DIR} + fi +} + +remove_pid () { + rm -f ${PID_FILE} + rmdir `dirname ${PID_FILE}` || : +} + +start_rabbitmq () { + status_rabbitmq quiet + if [ $RETVAL != 0 ] ; then + RETVAL=0 + ensure_pid_dir + set +e + RABBITMQ_PID_FILE=$PID_FILE start-stop-daemon --quiet \ + --chuid rabbitmq --start --exec $DAEMON \ + --pidfile "$RABBITMQ_PID_FILE" --background + $CONTROL wait $PID_FILE >/dev/null 2>&1 + RETVAL=$? + set -e + if [ $RETVAL != 0 ] ; then + remove_pid + fi + else + RETVAL=3 + fi +} + +stop_rabbitmq () { + status_rabbitmq quiet + if [ $RETVAL = 0 ] ; then + set +e + $CONTROL stop ${PID_FILE} > ${INIT_LOG_DIR}/shutdown_log 2> ${INIT_LOG_DIR}/shutdown_err + RETVAL=$? + set -e + if [ $RETVAL = 0 ] ; then + remove_pid + fi + else + RETVAL=3 + fi +} + +status_rabbitmq() { + set +e + if [ "$1" != "quiet" ] ; then + $CONTROL status 2>&1 + else + $CONTROL status > /dev/null 2>&1 + fi + if [ $? != 0 ] ; then + RETVAL=3 + fi + set -e +} + +rotate_logs_rabbitmq() { + set +e + $CONTROL -q rotate_logs ${ROTATE_SUFFIX} + if [ $? != 0 ] ; then + RETVAL=1 + fi + set -e +} + +restart_running_rabbitmq () { + status_rabbitmq quiet + if [ $RETVAL = 0 ] ; then + restart_rabbitmq + else + log_warning_msg "${DESC} not running" + fi +} + +restart_rabbitmq() { + stop_rabbitmq + start_rabbitmq +} + +restart_end() { + if [ $RETVAL = 0 ] ; then + log_end_msg 0 + else + log_end_msg 1 + fi +} + +start_stop_end() { + case "$RETVAL" in + 0) + [ -x /sbin/initctl ] && /sbin/initctl emit --no-wait "${NAME}-${1}" + log_end_msg 0 + ;; + 3) + log_warning_msg "${DESC} already ${1}" + log_end_msg 0 + RETVAL=0 + ;; + *) + log_warning_msg "FAILED - check ${INIT_LOG_DIR}/startup_\{log, _err\}" + log_end_msg 1 + ;; + esac +} + +case "$1" in + start) + log_daemon_msg "Starting ${DESC}" $NAME + start_rabbitmq + start_stop_end "running" + ;; + stop) + log_daemon_msg "Stopping ${DESC}" $NAME + stop_rabbitmq + start_stop_end "stopped" + ;; + status) + status_rabbitmq + ;; + rotate-logs) + log_action_begin_msg "Rotating log files for ${DESC}: ${NAME}" + rotate_logs_rabbitmq + log_action_end_msg $RETVAL + ;; + force-reload|reload|restart) + log_daemon_msg "Restarting ${DESC}" $NAME + restart_rabbitmq + restart_end + ;; + try-restart) + log_daemon_msg "Restarting ${DESC}" $NAME + restart_running_rabbitmq + restart_end + ;; + *) + echo "Usage: $0 {start|stop|status|rotate-logs|restart|condrestart|try-restart|reload|force-reload}" >&2 + RETVAL=1 + ;; +esac + +exit $RETVAL diff --git a/debian/rabbitmq-server.install b/debian/rabbitmq-server.install new file mode 100644 index 0000000..902f3dd --- /dev/null +++ b/debian/rabbitmq-server.install @@ -0,0 +1,4 @@ +debian/ocf/rabbitmq-server /usr/lib/ocf/resource.d/rabbitmq/ +debian/rabbitmq-server-wait /usr/lib/rabbitmq/bin +debian/rabbitmq-script-wrapper /usr/lib/rabbitmq/bin +debian/rabbitmq-env.conf /etc/rabbitmq diff --git a/debian/rabbitmq-server.links b/debian/rabbitmq-server.links new file mode 100644 index 0000000..0bfa1c5 --- /dev/null +++ b/debian/rabbitmq-server.links @@ -0,0 +1,3 @@ +/usr/lib/rabbitmq/bin/rabbitmq-script-wrapper /usr/sbin/rabbitmqctl +/usr/lib/rabbitmq/bin/rabbitmq-script-wrapper /usr/sbin/rabbitmq-server +/usr/lib/rabbitmq/bin/rabbitmq-script-wrapper /usr/sbin/rabbitmq-plugins diff --git a/debian/rabbitmq-server.logrotate b/debian/rabbitmq-server.logrotate new file mode 100644 index 0000000..c786df7 --- /dev/null +++ b/debian/rabbitmq-server.logrotate @@ -0,0 +1,12 @@ +/var/log/rabbitmq/*.log { + weekly + missingok + rotate 20 + compress + delaycompress + notifempty + sharedscripts + postrotate + /etc/init.d/rabbitmq-server rotate-logs > /dev/null + endscript +} diff --git a/debian/rabbitmq-server.postinst b/debian/rabbitmq-server.postinst new file mode 100644 index 0000000..b11340e --- /dev/null +++ b/debian/rabbitmq-server.postinst @@ -0,0 +1,60 @@ +#!/bin/sh +# postinst script for rabbitmq +# +# see: dh_installdeb(1) + +set -e + +# summary of how this script can be called: +# * `configure' +# * `abort-upgrade' +# * `abort-remove' `in-favour' +# +# * `abort-remove' +# * `abort-deconfigure' `in-favour' +# `removing' +# +# for details, see http://www.debian.org/doc/debian-policy/ or +# the debian-policy package + + +# create rabbitmq group +if ! getent group rabbitmq >/dev/null; then + addgroup --system rabbitmq +fi + +# create rabbitmq user +if ! getent passwd rabbitmq >/dev/null; then + adduser --system --ingroup rabbitmq --home /var/lib/rabbitmq \ + --no-create-home --gecos "RabbitMQ messaging server" \ + --disabled-login rabbitmq +fi + +chown -R rabbitmq:rabbitmq /var/lib/rabbitmq +chown -R rabbitmq:rabbitmq /var/log/rabbitmq + +case "$1" in + configure) + if [ -f /etc/rabbitmq/rabbitmq.conf ] && \ + [ ! -f /etc/rabbitmq/rabbitmq-env.conf ]; then + mv /etc/rabbitmq/rabbitmq.conf /etc/rabbitmq/rabbitmq-env.conf + fi + ;; + + abort-upgrade|abort-remove|abort-deconfigure) + ;; + + *) + echo "postinst called with unknown argument \`$1'" >&2 + exit 1 + ;; +esac + +# dh_installdeb will replace this with shell code automatically +# generated by other debhelper scripts. + +#DEBHELPER# + +exit 0 + + diff --git a/debian/rabbitmq-server.postrm b/debian/rabbitmq-server.postrm new file mode 100644 index 0000000..c2e9bbf --- /dev/null +++ b/debian/rabbitmq-server.postrm @@ -0,0 +1,65 @@ +#!/bin/sh +# postrm script for rabbitmq +# +# see: dh_installdeb(1) + +set -e + +# summary of how this script can be called: +# * `remove' +# * `purge' +# * `upgrade' +# * `failed-upgrade' +# * `abort-install' +# * `abort-install' +# * `abort-upgrade' +# * `disappear' +# +# for details, see http://www.debian.org/doc/debian-policy/ or +# the debian-policy package + +remove_plugin_traces() { + # Remove traces of plugins + rm -rf /var/lib/rabbitmq/plugins-scratch +} + +case "$1" in + purge) + rm -f /etc/default/rabbitmq + if [ -d /var/lib/rabbitmq ]; then + rm -r /var/lib/rabbitmq + fi + if [ -d /var/log/rabbitmq ]; then + rm -r /var/log/rabbitmq + fi + if [ -d /etc/rabbitmq ]; then + rm -r /etc/rabbitmq + fi + remove_plugin_traces + if getent passwd rabbitmq >/dev/null; then + # Stop epmd if run by the rabbitmq user + pkill -u rabbitmq epmd || : + fi + ;; + + remove|upgrade) + remove_plugin_traces + ;; + + failed-upgrade|abort-install|abort-upgrade|disappear) + ;; + + *) + echo "postrm called with unknown argument \`$1'" >&2 + exit 1 + ;; +esac + +# dh_installdeb will replace this with shell code automatically +# generated by other debhelper scripts. + +#DEBHELPER# + +exit 0 + + diff --git a/debian/rabbitmq-server.service b/debian/rabbitmq-server.service new file mode 100644 index 0000000..faa73c1 --- /dev/null +++ b/debian/rabbitmq-server.service @@ -0,0 +1,15 @@ +[Unit] +Description=RabbitMQ Messaging Server +After=network.target + +[Service] +Type=simple +User=rabbitmq +SyslogIdentifier=rabbitmq +LimitNOFILE=65536 +ExecStart=/usr/sbin/rabbitmq-server +ExecStartPost=/usr/lib/rabbitmq/bin/rabbitmq-server-wait +ExecStop=/usr/sbin/rabbitmqctl stop + +[Install] +WantedBy=multi-user.target diff --git a/debian/rules b/debian/rules new file mode 100755 index 0000000..bee4e81 --- /dev/null +++ b/debian/rules @@ -0,0 +1,31 @@ +#!/usr/bin/make -f +# -*- makefile -*- +#export DH_VERBOSE=1 + +%: + dh $@ --parallel --with systemd + +override_dh_systemd_enable: + dh_systemd_enable --no-enable + +override_dh_systemd_start: + dh_systemd_start --no-start + +override_dh_installinit: + dh_installinit --noscripts + + +DEB_UPSTREAM_VERSION=$(shell dpkg-parsechangelog | sed -rne 's,^Version: ([^+]+)-.*,\1,p') +DEB_DESTDIR=debian/rabbitmq-server +RABBIT_LIB=$(DEB_DESTDIR)/usr/lib/rabbitmq/lib/rabbitmq_server-$(DEB_UPSTREAM_VERSION) +RABBIT_BIN=$(DEB_DESTDIR)/usr/lib/rabbitmq/bin +DOCDIR=$(DEB_DESTDIR)/usr/share/doc/rabbitmq-server + +override_dh_auto_install: + dh_auto_install -- TARGET_DIR=$(RABBIT_LIB) SBIN_DIR=$(RABBIT_BIN) \ + DOC_INSTALL_DIR=$(DOCDIR) MAN_DIR=$(DEB_DESTDIR)/usr/share/man + rm -f $(RABBIT_LIB)/LICENSE* $(RABBIT_LIB)/INSTALL* + +override_dh_auto_clean: + rm -f plugins-src/rabbitmq-server plugins/README + dh_auto_clean diff --git a/debian/source/format b/debian/source/format new file mode 100644 index 0000000..163aaf8 --- /dev/null +++ b/debian/source/format @@ -0,0 +1 @@ +3.0 (quilt) diff --git a/debian/watch b/debian/watch new file mode 100644 index 0000000..e41153d --- /dev/null +++ b/debian/watch @@ -0,0 +1,2 @@ +version=3 +http://www.rabbitmq.com/releases/rabbitmq-server/v(.*)/rabbitmq-server-(\d.*)\.tar\.gz diff --git a/rabbitmq-server/INSTALL b/rabbitmq-server/INSTALL new file mode 100644 index 0000000..be34498 --- /dev/null +++ b/rabbitmq-server/INSTALL @@ -0,0 +1,2 @@ +Please see http://www.rabbitmq.com/download.html for links to guides +to installing RabbitMQ. diff --git a/rabbitmq-server/LICENSE b/rabbitmq-server/LICENSE new file mode 100644 index 0000000..9deeb23 --- /dev/null +++ b/rabbitmq-server/LICENSE @@ -0,0 +1,556 @@ +This package, the RabbitMQ server is licensed under the MPL. + +If you have any questions regarding licensing, please contact us at +info@rabbitmq.com. + +The files amqp-rabbitmq-0.8.json and amqp-rabbitmq-0.9.1.json are +"Copyright (C) 2008-2013 GoPivotal", Inc. and are covered by the MIT +license. + +jQuery is "Copyright (c) 2010 John Resig" and is covered by the MIT +license. It was downloaded from http://jquery.com/ + +EJS is "Copyright (c) 2007 Edward Benson" and is covered by the MIT +license. It was downloaded from http://embeddedjs.com/ + +Sammy is "Copyright (c) 2008 Aaron Quint, Quirkey NYC, LLC" and is +covered by the MIT license. It was downloaded from +http://code.quirkey.com/sammy/ + +ExplorerCanvas is "Copyright 2006 Google Inc" and is covered by the +Apache License version 2.0. It was downloaded from +http://code.google.com/p/explorercanvas/ + +Flot is "Copyright (c) 2007-2013 IOLA and Ole Laursen" and is covered +by the MIT license. It was downloaded from +http://www.flotcharts.org/ +Webmachine is Copyright (c) Basho Technologies and is covered by the +Apache License 2.0. It was downloaded from http://webmachine.basho.com/ + +Eldap is "Copyright (c) 2010, Torbjorn Tornkvist" and is covered by +the MIT license. It was downloaded from https://github.com/etnt/eldap + +Mochiweb is "Copyright (c) 2007 Mochi Media, Inc." and is covered by +the MIT license. It was downloaded from +http://github.com/mochi/mochiweb/ + +glMatrix is "Copyright (c) 2011, Brandon Jones" and is covered by the +BSD 2-Clause license. It was downloaded from +http://code.google.com/p/glmatrix/ + + +The MIT license is as follows: + + "Permission is hereby granted, free of charge, to any person + obtaining a copy of this file (the Software), to deal in the + Software without restriction, including without limitation the + rights to use, copy, modify, merge, publish, distribute, + sublicense, and/or sell copies of the Software, and to permit + persons to whom the Software is furnished to do so, subject to + the following conditions: + + The above copyright notice and this permission notice shall be + included in all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, + EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES + OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT + HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, + WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR + OTHER DEALINGS IN THE SOFTWARE." + + +The BSD 2-Clause license is as follows: + + "Redistribution and use in source and binary forms, with or + without modification, are permitted provided that the + following conditions are met: + + 1. Redistributions of source code must retain the above + copyright notice, this list of conditions and the following + disclaimer. + + 2. Redistributions in binary form must reproduce the above + copyright notice, this list of conditions and the following + disclaimer in the documentation and/or other materials + provided with the distribution. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND + CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR + CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, + EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." + + +The rest of this package is licensed under the Mozilla Public License 1.1 +Authors and Copyright are as described below: + + The Initial Developer of the Original Code is GoPivotal, Inc. + Copyright (c) 2007-2015 Pivotal Software, Inc. All rights reserved. + + + MOZILLA PUBLIC LICENSE + Version 1.1 + + --------------- + +1. Definitions. + + 1.0.1. "Commercial Use" means distribution or otherwise making the + Covered Code available to a third party. + + 1.1. "Contributor" means each entity that creates or contributes to + the creation of Modifications. + + 1.2. "Contributor Version" means the combination of the Original + Code, prior Modifications used by a Contributor, and the Modifications + made by that particular Contributor. + + 1.3. "Covered Code" means the Original Code or Modifications or the + combination of the Original Code and Modifications, in each case + including portions thereof. + + 1.4. "Electronic Distribution Mechanism" means a mechanism generally + accepted in the software development community for the electronic + transfer of data. + + 1.5. "Executable" means Covered Code in any form other than Source + Code. + + 1.6. "Initial Developer" means the individual or entity identified + as the Initial Developer in the Source Code notice required by Exhibit + A. + + 1.7. "Larger Work" means a work which combines Covered Code or + portions thereof with code not governed by the terms of this License. + + 1.8. "License" means this document. + + 1.8.1. "Licensable" means having the right to grant, to the maximum + extent possible, whether at the time of the initial grant or + subsequently acquired, any and all of the rights conveyed herein. + + 1.9. "Modifications" means any addition to or deletion from the + substance or structure of either the Original Code or any previous + Modifications. When Covered Code is released as a series of files, a + Modification is: + A. Any addition to or deletion from the contents of a file + containing Original Code or previous Modifications. + + B. Any new file that contains any part of the Original Code or + previous Modifications. + + 1.10. "Original Code" means Source Code of computer software code + which is described in the Source Code notice required by Exhibit A as + Original Code, and which, at the time of its release under this + License is not already Covered Code governed by this License. + + 1.10.1. "Patent Claims" means any patent claim(s), now owned or + hereafter acquired, including without limitation, method, process, + and apparatus claims, in any patent Licensable by grantor. + + 1.11. "Source Code" means the preferred form of the Covered Code for + making modifications to it, including all modules it contains, plus + any associated interface definition files, scripts used to control + compilation and installation of an Executable, or source code + differential comparisons against either the Original Code or another + well known, available Covered Code of the Contributor's choice. The + Source Code can be in a compressed or archival form, provided the + appropriate decompression or de-archiving software is widely available + for no charge. + + 1.12. "You" (or "Your") means an individual or a legal entity + exercising rights under, and complying with all of the terms of, this + License or a future version of this License issued under Section 6.1. + For legal entities, "You" includes any entity which controls, is + controlled by, or is under common control with You. For purposes of + this definition, "control" means (a) the power, direct or indirect, + to cause the direction or management of such entity, whether by + contract or otherwise, or (b) ownership of more than fifty percent + (50%) of the outstanding shares or beneficial ownership of such + entity. + +2. Source Code License. + + 2.1. The Initial Developer Grant. + The Initial Developer hereby grants You a world-wide, royalty-free, + non-exclusive license, subject to third party intellectual property + claims: + (a) under intellectual property rights (other than patent or + trademark) Licensable by Initial Developer to use, reproduce, + modify, display, perform, sublicense and distribute the Original + Code (or portions thereof) with or without Modifications, and/or + as part of a Larger Work; and + + (b) under Patents Claims infringed by the making, using or + selling of Original Code, to make, have made, use, practice, + sell, and offer for sale, and/or otherwise dispose of the + Original Code (or portions thereof). + + (c) the licenses granted in this Section 2.1(a) and (b) are + effective on the date Initial Developer first distributes + Original Code under the terms of this License. + + (d) Notwithstanding Section 2.1(b) above, no patent license is + granted: 1) for code that You delete from the Original Code; 2) + separate from the Original Code; or 3) for infringements caused + by: i) the modification of the Original Code or ii) the + combination of the Original Code with other software or devices. + + 2.2. Contributor Grant. + Subject to third party intellectual property claims, each Contributor + hereby grants You a world-wide, royalty-free, non-exclusive license + + (a) under intellectual property rights (other than patent or + trademark) Licensable by Contributor, to use, reproduce, modify, + display, perform, sublicense and distribute the Modifications + created by such Contributor (or portions thereof) either on an + unmodified basis, with other Modifications, as Covered Code + and/or as part of a Larger Work; and + + (b) under Patent Claims infringed by the making, using, or + selling of Modifications made by that Contributor either alone + and/or in combination with its Contributor Version (or portions + of such combination), to make, use, sell, offer for sale, have + made, and/or otherwise dispose of: 1) Modifications made by that + Contributor (or portions thereof); and 2) the combination of + Modifications made by that Contributor with its Contributor + Version (or portions of such combination). + + (c) the licenses granted in Sections 2.2(a) and 2.2(b) are + effective on the date Contributor first makes Commercial Use of + the Covered Code. + + (d) Notwithstanding Section 2.2(b) above, no patent license is + granted: 1) for any code that Contributor has deleted from the + Contributor Version; 2) separate from the Contributor Version; + 3) for infringements caused by: i) third party modifications of + Contributor Version or ii) the combination of Modifications made + by that Contributor with other software (except as part of the + Contributor Version) or other devices; or 4) under Patent Claims + infringed by Covered Code in the absence of Modifications made by + that Contributor. + +3. Distribution Obligations. + + 3.1. Application of License. + The Modifications which You create or to which You contribute are + governed by the terms of this License, including without limitation + Section 2.2. The Source Code version of Covered Code may be + distributed only under the terms of this License or a future version + of this License released under Section 6.1, and You must include a + copy of this License with every copy of the Source Code You + distribute. You may not offer or impose any terms on any Source Code + version that alters or restricts the applicable version of this + License or the recipients' rights hereunder. However, You may include + an additional document offering the additional rights described in + Section 3.5. + + 3.2. Availability of Source Code. + Any Modification which You create or to which You contribute must be + made available in Source Code form under the terms of this License + either on the same media as an Executable version or via an accepted + Electronic Distribution Mechanism to anyone to whom you made an + Executable version available; and if made available via Electronic + Distribution Mechanism, must remain available for at least twelve (12) + months after the date it initially became available, or at least six + (6) months after a subsequent version of that particular Modification + has been made available to such recipients. You are responsible for + ensuring that the Source Code version remains available even if the + Electronic Distribution Mechanism is maintained by a third party. + + 3.3. Description of Modifications. + You must cause all Covered Code to which You contribute to contain a + file documenting the changes You made to create that Covered Code and + the date of any change. You must include a prominent statement that + the Modification is derived, directly or indirectly, from Original + Code provided by the Initial Developer and including the name of the + Initial Developer in (a) the Source Code, and (b) in any notice in an + Executable version or related documentation in which You describe the + origin or ownership of the Covered Code. + + 3.4. Intellectual Property Matters + (a) Third Party Claims. + If Contributor has knowledge that a license under a third party's + intellectual property rights is required to exercise the rights + granted by such Contributor under Sections 2.1 or 2.2, + Contributor must include a text file with the Source Code + distribution titled "LEGAL" which describes the claim and the + party making the claim in sufficient detail that a recipient will + know whom to contact. If Contributor obtains such knowledge after + the Modification is made available as described in Section 3.2, + Contributor shall promptly modify the LEGAL file in all copies + Contributor makes available thereafter and shall take other steps + (such as notifying appropriate mailing lists or newsgroups) + reasonably calculated to inform those who received the Covered + Code that new knowledge has been obtained. + + (b) Contributor APIs. + If Contributor's Modifications include an application programming + interface and Contributor has knowledge of patent licenses which + are reasonably necessary to implement that API, Contributor must + also include this information in the LEGAL file. + + (c) Representations. + Contributor represents that, except as disclosed pursuant to + Section 3.4(a) above, Contributor believes that Contributor's + Modifications are Contributor's original creation(s) and/or + Contributor has sufficient rights to grant the rights conveyed by + this License. + + 3.5. Required Notices. + You must duplicate the notice in Exhibit A in each file of the Source + Code. If it is not possible to put such notice in a particular Source + Code file due to its structure, then You must include such notice in a + location (such as a relevant directory) where a user would be likely + to look for such a notice. If You created one or more Modification(s) + You may add your name as a Contributor to the notice described in + Exhibit A. You must also duplicate this License in any documentation + for the Source Code where You describe recipients' rights or ownership + rights relating to Covered Code. You may choose to offer, and to + charge a fee for, warranty, support, indemnity or liability + obligations to one or more recipients of Covered Code. However, You + may do so only on Your own behalf, and not on behalf of the Initial + Developer or any Contributor. You must make it absolutely clear than + any such warranty, support, indemnity or liability obligation is + offered by You alone, and You hereby agree to indemnify the Initial + Developer and every Contributor for any liability incurred by the + Initial Developer or such Contributor as a result of warranty, + support, indemnity or liability terms You offer. + + 3.6. Distribution of Executable Versions. + You may distribute Covered Code in Executable form only if the + requirements of Section 3.1-3.5 have been met for that Covered Code, + and if You include a notice stating that the Source Code version of + the Covered Code is available under the terms of this License, + including a description of how and where You have fulfilled the + obligations of Section 3.2. The notice must be conspicuously included + in any notice in an Executable version, related documentation or + collateral in which You describe recipients' rights relating to the + Covered Code. You may distribute the Executable version of Covered + Code or ownership rights under a license of Your choice, which may + contain terms different from this License, provided that You are in + compliance with the terms of this License and that the license for the + Executable version does not attempt to limit or alter the recipient's + rights in the Source Code version from the rights set forth in this + License. If You distribute the Executable version under a different + license You must make it absolutely clear that any terms which differ + from this License are offered by You alone, not by the Initial + Developer or any Contributor. You hereby agree to indemnify the + Initial Developer and every Contributor for any liability incurred by + the Initial Developer or such Contributor as a result of any such + terms You offer. + + 3.7. Larger Works. + You may create a Larger Work by combining Covered Code with other code + not governed by the terms of this License and distribute the Larger + Work as a single product. In such a case, You must make sure the + requirements of this License are fulfilled for the Covered Code. + +4. Inability to Comply Due to Statute or Regulation. + + If it is impossible for You to comply with any of the terms of this + License with respect to some or all of the Covered Code due to + statute, judicial order, or regulation then You must: (a) comply with + the terms of this License to the maximum extent possible; and (b) + describe the limitations and the code they affect. Such description + must be included in the LEGAL file described in Section 3.4 and must + be included with all distributions of the Source Code. Except to the + extent prohibited by statute or regulation, such description must be + sufficiently detailed for a recipient of ordinary skill to be able to + understand it. + +5. Application of this License. + + This License applies to code to which the Initial Developer has + attached the notice in Exhibit A and to related Covered Code. + +6. Versions of the License. + + 6.1. New Versions. + Netscape Communications Corporation ("Netscape") may publish revised + and/or new versions of the License from time to time. Each version + will be given a distinguishing version number. + + 6.2. Effect of New Versions. + Once Covered Code has been published under a particular version of the + License, You may always continue to use it under the terms of that + version. You may also choose to use such Covered Code under the terms + of any subsequent version of the License published by Netscape. No one + other than Netscape has the right to modify the terms applicable to + Covered Code created under this License. + + 6.3. Derivative Works. + If You create or use a modified version of this License (which you may + only do in order to apply it to code which is not already Covered Code + governed by this License), You must (a) rename Your license so that + the phrases "Mozilla", "MOZILLAPL", "MOZPL", "Netscape", + "MPL", "NPL" or any confusingly similar phrase do not appear in your + license (except to note that your license differs from this License) + and (b) otherwise make it clear that Your version of the license + contains terms which differ from the Mozilla Public License and + Netscape Public License. (Filling in the name of the Initial + Developer, Original Code or Contributor in the notice described in + Exhibit A shall not of themselves be deemed to be modifications of + this License.) + +7. DISCLAIMER OF WARRANTY. + + COVERED CODE IS PROVIDED UNDER THIS LICENSE ON AN "AS IS" BASIS, + WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, + WITHOUT LIMITATION, WARRANTIES THAT THE COVERED CODE IS FREE OF + DEFECTS, MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE OR NON-INFRINGING. + THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE COVERED CODE + IS WITH YOU. SHOULD ANY COVERED CODE PROVE DEFECTIVE IN ANY RESPECT, + YOU (NOT THE INITIAL DEVELOPER OR ANY OTHER CONTRIBUTOR) ASSUME THE + COST OF ANY NECESSARY SERVICING, REPAIR OR CORRECTION. THIS DISCLAIMER + OF WARRANTY CONSTITUTES AN ESSENTIAL PART OF THIS LICENSE. NO USE OF + ANY COVERED CODE IS AUTHORIZED HEREUNDER EXCEPT UNDER THIS DISCLAIMER. + +8. TERMINATION. + + 8.1. This License and the rights granted hereunder will terminate + automatically if You fail to comply with terms herein and fail to cure + such breach within 30 days of becoming aware of the breach. All + sublicenses to the Covered Code which are properly granted shall + survive any termination of this License. Provisions which, by their + nature, must remain in effect beyond the termination of this License + shall survive. + + 8.2. If You initiate litigation by asserting a patent infringement + claim (excluding declatory judgment actions) against Initial Developer + or a Contributor (the Initial Developer or Contributor against whom + You file such action is referred to as "Participant") alleging that: + + (a) such Participant's Contributor Version directly or indirectly + infringes any patent, then any and all rights granted by such + Participant to You under Sections 2.1 and/or 2.2 of this License + shall, upon 60 days notice from Participant terminate prospectively, + unless if within 60 days after receipt of notice You either: (i) + agree in writing to pay Participant a mutually agreeable reasonable + royalty for Your past and future use of Modifications made by such + Participant, or (ii) withdraw Your litigation claim with respect to + the Contributor Version against such Participant. If within 60 days + of notice, a reasonable royalty and payment arrangement are not + mutually agreed upon in writing by the parties or the litigation claim + is not withdrawn, the rights granted by Participant to You under + Sections 2.1 and/or 2.2 automatically terminate at the expiration of + the 60 day notice period specified above. + + (b) any software, hardware, or device, other than such Participant's + Contributor Version, directly or indirectly infringes any patent, then + any rights granted to You by such Participant under Sections 2.1(b) + and 2.2(b) are revoked effective as of the date You first made, used, + sold, distributed, or had made, Modifications made by that + Participant. + + 8.3. If You assert a patent infringement claim against Participant + alleging that such Participant's Contributor Version directly or + indirectly infringes any patent where such claim is resolved (such as + by license or settlement) prior to the initiation of patent + infringement litigation, then the reasonable value of the licenses + granted by such Participant under Sections 2.1 or 2.2 shall be taken + into account in determining the amount or value of any payment or + license. + + 8.4. In the event of termination under Sections 8.1 or 8.2 above, + all end user license agreements (excluding distributors and resellers) + which have been validly granted by You or any distributor hereunder + prior to termination shall survive termination. + +9. LIMITATION OF LIABILITY. + + UNDER NO CIRCUMSTANCES AND UNDER NO LEGAL THEORY, WHETHER TORT + (INCLUDING NEGLIGENCE), CONTRACT, OR OTHERWISE, SHALL YOU, THE INITIAL + DEVELOPER, ANY OTHER CONTRIBUTOR, OR ANY DISTRIBUTOR OF COVERED CODE, + OR ANY SUPPLIER OF ANY OF SUCH PARTIES, BE LIABLE TO ANY PERSON FOR + ANY INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES OF ANY + CHARACTER INCLUDING, WITHOUT LIMITATION, DAMAGES FOR LOSS OF GOODWILL, + WORK STOPPAGE, COMPUTER FAILURE OR MALFUNCTION, OR ANY AND ALL OTHER + COMMERCIAL DAMAGES OR LOSSES, EVEN IF SUCH PARTY SHALL HAVE BEEN + INFORMED OF THE POSSIBILITY OF SUCH DAMAGES. THIS LIMITATION OF + LIABILITY SHALL NOT APPLY TO LIABILITY FOR DEATH OR PERSONAL INJURY + RESULTING FROM SUCH PARTY'S NEGLIGENCE TO THE EXTENT APPLICABLE LAW + PROHIBITS SUCH LIMITATION. SOME JURISDICTIONS DO NOT ALLOW THE + EXCLUSION OR LIMITATION OF INCIDENTAL OR CONSEQUENTIAL DAMAGES, SO + THIS EXCLUSION AND LIMITATION MAY NOT APPLY TO YOU. + +10. U.S. GOVERNMENT END USERS. + + The Covered Code is a "commercial item," as that term is defined in + 48 C.F.R. 2.101 (Oct. 1995), consisting of "commercial computer + software" and "commercial computer software documentation," as such + terms are used in 48 C.F.R. 12.212 (Sept. 1995). Consistent with 48 + C.F.R. 12.212 and 48 C.F.R. 227.7202-1 through 227.7202-4 (June 1995), + all U.S. Government End Users acquire Covered Code with only those + rights set forth herein. + +11. MISCELLANEOUS. + + This License represents the complete agreement concerning subject + matter hereof. If any provision of this License is held to be + unenforceable, such provision shall be reformed only to the extent + necessary to make it enforceable. This License shall be governed by + California law provisions (except to the extent applicable law, if + any, provides otherwise), excluding its conflict-of-law provisions. + With respect to disputes in which at least one party is a citizen of, + or an entity chartered or registered to do business in the United + States of America, any litigation relating to this License shall be + subject to the jurisdiction of the Federal Courts of the Northern + District of California, with venue lying in Santa Clara County, + California, with the losing party responsible for costs, including + without limitation, court costs and reasonable attorneys' fees and + expenses. The application of the United Nations Convention on + Contracts for the International Sale of Goods is expressly excluded. + Any law or regulation which provides that the language of a contract + shall be construed against the drafter shall not apply to this + License. + +12. RESPONSIBILITY FOR CLAIMS. + + As between Initial Developer and the Contributors, each party is + responsible for claims and damages arising, directly or indirectly, + out of its utilization of rights under this License and You agree to + work with Initial Developer and Contributors to distribute such + responsibility on an equitable basis. Nothing herein is intended or + shall be deemed to constitute any admission of liability. + +13. MULTIPLE-LICENSED CODE. + + Initial Developer may designate portions of the Covered Code as + "Multiple-Licensed". "Multiple-Licensed" means that the Initial + Developer permits you to utilize portions of the Covered Code under + Your choice of the NPL or the alternative licenses, if any, specified + by the Initial Developer in the file described in Exhibit A. + +EXHIBIT A -Mozilla Public License. + + ``The contents of this file are subject to the Mozilla Public License + Version 1.1 (the "License"); you may not use this file except in + compliance with the License. You may obtain a copy of the License at + http://www.mozilla.org/MPL/ + + Software distributed under the License is distributed on an "AS IS" + basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the + License for the specific language governing rights and limitations + under the License. + + The Original Code is RabbitMQ. + + The Initial Developer of the Original Code is GoPivotal, Inc. + Copyright (c) 2007-2015 Pivotal Software, Inc. All rights reserved.'' + + [NOTE: The text of this Exhibit A may differ slightly from the text of + the notices in the Source Code files of the Original Code. You should + use the text of this Exhibit A rather than the text found in the + Original Code Source Code for Your Modifications.] diff --git a/rabbitmq-server/LICENSE-APACHE2-ExplorerCanvas b/rabbitmq-server/LICENSE-APACHE2-ExplorerCanvas new file mode 100644 index 0000000..d645695 --- /dev/null +++ b/rabbitmq-server/LICENSE-APACHE2-ExplorerCanvas @@ -0,0 +1,202 @@ + + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright [yyyy] [name of copyright owner] + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. diff --git a/rabbitmq-server/LICENSE-APL2-Stomp-Websocket b/rabbitmq-server/LICENSE-APL2-Stomp-Websocket new file mode 100644 index 0000000..ef51da2 --- /dev/null +++ b/rabbitmq-server/LICENSE-APL2-Stomp-Websocket @@ -0,0 +1,202 @@ + + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + +TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + +1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + +2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + +3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + +4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + +5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + +6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + +7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + +8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + +9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + +END OF TERMS AND CONDITIONS + +APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + +Copyright [yyyy] [name of copyright owner] + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. diff --git a/rabbitmq-server/LICENSE-Apache-Basho b/rabbitmq-server/LICENSE-Apache-Basho new file mode 100644 index 0000000..e454a52 --- /dev/null +++ b/rabbitmq-server/LICENSE-Apache-Basho @@ -0,0 +1,178 @@ + + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + diff --git a/rabbitmq-server/LICENSE-BSD-base64js b/rabbitmq-server/LICENSE-BSD-base64js new file mode 100644 index 0000000..7073704 --- /dev/null +++ b/rabbitmq-server/LICENSE-BSD-base64js @@ -0,0 +1,25 @@ +/* + * Copyright (c) 2010 Nick Galbreath + * http://code.google.com/p/stringencoders/source/browse/#svn/trunk/javascript + * + * Permission is hereby granted, free of charge, to any person + * obtaining a copy of this software and associated documentation + * files (the "Software"), to deal in the Software without + * restriction, including without limitation the rights to use, + * copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following + * conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES + * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT + * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, + * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR + * OTHER DEALINGS IN THE SOFTWARE. +*/ diff --git a/rabbitmq-server/LICENSE-BSD-glMatrix b/rabbitmq-server/LICENSE-BSD-glMatrix new file mode 100644 index 0000000..660571e --- /dev/null +++ b/rabbitmq-server/LICENSE-BSD-glMatrix @@ -0,0 +1,26 @@ +Copyright (c) 2011, Brandon Jones +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are +met: + +1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + +2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the + distribution. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/rabbitmq-server/LICENSE-MIT-EJS10 b/rabbitmq-server/LICENSE-MIT-EJS10 new file mode 100644 index 0000000..f3bdcd8 --- /dev/null +++ b/rabbitmq-server/LICENSE-MIT-EJS10 @@ -0,0 +1,23 @@ +EJS - Embedded JavaScript + +Copyright (c) 2007 Edward Benson + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. + + diff --git a/rabbitmq-server/LICENSE-MIT-Flot b/rabbitmq-server/LICENSE-MIT-Flot new file mode 100644 index 0000000..67f4625 --- /dev/null +++ b/rabbitmq-server/LICENSE-MIT-Flot @@ -0,0 +1,22 @@ +Copyright (c) 2007-2013 IOLA and Ole Laursen + +Permission is hereby granted, free of charge, to any person +obtaining a copy of this software and associated documentation +files (the "Software"), to deal in the Software without +restriction, including without limitation the rights to use, +copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the +Software is furnished to do so, subject to the following +conditions: + +The above copyright notice and this permission notice shall be +included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES +OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT +HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, +WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +OTHER DEALINGS IN THE SOFTWARE. diff --git a/rabbitmq-server/LICENSE-MIT-Mochi b/rabbitmq-server/LICENSE-MIT-Mochi new file mode 100644 index 0000000..c85b65a --- /dev/null +++ b/rabbitmq-server/LICENSE-MIT-Mochi @@ -0,0 +1,9 @@ +This is the MIT license. + +Copyright (c) 2007 Mochi Media, Inc. + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/rabbitmq-server/LICENSE-MIT-Sammy060 b/rabbitmq-server/LICENSE-MIT-Sammy060 new file mode 100644 index 0000000..3debf5a --- /dev/null +++ b/rabbitmq-server/LICENSE-MIT-Sammy060 @@ -0,0 +1,25 @@ +Copyright (c) 2008 Aaron Quint, Quirkey NYC, LLC + +Permission is hereby granted, free of charge, to any person +obtaining a copy of this software and associated documentation +files (the "Software"), to deal in the Software without +restriction, including without limitation the rights to use, +copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the +Software is furnished to do so, subject to the following +conditions: + +The above copyright notice and this permission notice shall be +included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES +OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT +HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, +WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +OTHER DEALINGS IN THE SOFTWARE. + + + diff --git a/rabbitmq-server/LICENSE-MIT-eldap b/rabbitmq-server/LICENSE-MIT-eldap new file mode 100644 index 0000000..1f62009 --- /dev/null +++ b/rabbitmq-server/LICENSE-MIT-eldap @@ -0,0 +1,21 @@ + +Copyright (c) 2010, Torbjorn Tornkvist + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. + diff --git a/rabbitmq-server/LICENSE-MIT-jQuery164 b/rabbitmq-server/LICENSE-MIT-jQuery164 new file mode 100644 index 0000000..5a87162 --- /dev/null +++ b/rabbitmq-server/LICENSE-MIT-jQuery164 @@ -0,0 +1,21 @@ +Copyright (c) 2011 John Resig, http://jquery.com/ + +Permission is hereby granted, free of charge, to any person obtaining +a copy of this software and associated documentation files (the +"Software"), to deal in the Software without restriction, including +without limitation the rights to use, copy, modify, merge, publish, +distribute, sublicense, and/or sell copies of the Software, and to +permit persons to whom the Software is furnished to do so, subject to +the following conditions: + +The above copyright notice and this permission notice shall be +included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + diff --git a/rabbitmq-server/LICENSE-MPL-RabbitMQ b/rabbitmq-server/LICENSE-MPL-RabbitMQ new file mode 100644 index 0000000..c87c1a3 --- /dev/null +++ b/rabbitmq-server/LICENSE-MPL-RabbitMQ @@ -0,0 +1,455 @@ + MOZILLA PUBLIC LICENSE + Version 1.1 + + --------------- + +1. Definitions. + + 1.0.1. "Commercial Use" means distribution or otherwise making the + Covered Code available to a third party. + + 1.1. "Contributor" means each entity that creates or contributes to + the creation of Modifications. + + 1.2. "Contributor Version" means the combination of the Original + Code, prior Modifications used by a Contributor, and the Modifications + made by that particular Contributor. + + 1.3. "Covered Code" means the Original Code or Modifications or the + combination of the Original Code and Modifications, in each case + including portions thereof. + + 1.4. "Electronic Distribution Mechanism" means a mechanism generally + accepted in the software development community for the electronic + transfer of data. + + 1.5. "Executable" means Covered Code in any form other than Source + Code. + + 1.6. "Initial Developer" means the individual or entity identified + as the Initial Developer in the Source Code notice required by Exhibit + A. + + 1.7. "Larger Work" means a work which combines Covered Code or + portions thereof with code not governed by the terms of this License. + + 1.8. "License" means this document. + + 1.8.1. "Licensable" means having the right to grant, to the maximum + extent possible, whether at the time of the initial grant or + subsequently acquired, any and all of the rights conveyed herein. + + 1.9. "Modifications" means any addition to or deletion from the + substance or structure of either the Original Code or any previous + Modifications. When Covered Code is released as a series of files, a + Modification is: + A. Any addition to or deletion from the contents of a file + containing Original Code or previous Modifications. + + B. Any new file that contains any part of the Original Code or + previous Modifications. + + 1.10. "Original Code" means Source Code of computer software code + which is described in the Source Code notice required by Exhibit A as + Original Code, and which, at the time of its release under this + License is not already Covered Code governed by this License. + + 1.10.1. "Patent Claims" means any patent claim(s), now owned or + hereafter acquired, including without limitation, method, process, + and apparatus claims, in any patent Licensable by grantor. + + 1.11. "Source Code" means the preferred form of the Covered Code for + making modifications to it, including all modules it contains, plus + any associated interface definition files, scripts used to control + compilation and installation of an Executable, or source code + differential comparisons against either the Original Code or another + well known, available Covered Code of the Contributor's choice. The + Source Code can be in a compressed or archival form, provided the + appropriate decompression or de-archiving software is widely available + for no charge. + + 1.12. "You" (or "Your") means an individual or a legal entity + exercising rights under, and complying with all of the terms of, this + License or a future version of this License issued under Section 6.1. + For legal entities, "You" includes any entity which controls, is + controlled by, or is under common control with You. For purposes of + this definition, "control" means (a) the power, direct or indirect, + to cause the direction or management of such entity, whether by + contract or otherwise, or (b) ownership of more than fifty percent + (50%) of the outstanding shares or beneficial ownership of such + entity. + +2. Source Code License. + + 2.1. The Initial Developer Grant. + The Initial Developer hereby grants You a world-wide, royalty-free, + non-exclusive license, subject to third party intellectual property + claims: + (a) under intellectual property rights (other than patent or + trademark) Licensable by Initial Developer to use, reproduce, + modify, display, perform, sublicense and distribute the Original + Code (or portions thereof) with or without Modifications, and/or + as part of a Larger Work; and + + (b) under Patents Claims infringed by the making, using or + selling of Original Code, to make, have made, use, practice, + sell, and offer for sale, and/or otherwise dispose of the + Original Code (or portions thereof). + + (c) the licenses granted in this Section 2.1(a) and (b) are + effective on the date Initial Developer first distributes + Original Code under the terms of this License. + + (d) Notwithstanding Section 2.1(b) above, no patent license is + granted: 1) for code that You delete from the Original Code; 2) + separate from the Original Code; or 3) for infringements caused + by: i) the modification of the Original Code or ii) the + combination of the Original Code with other software or devices. + + 2.2. Contributor Grant. + Subject to third party intellectual property claims, each Contributor + hereby grants You a world-wide, royalty-free, non-exclusive license + + (a) under intellectual property rights (other than patent or + trademark) Licensable by Contributor, to use, reproduce, modify, + display, perform, sublicense and distribute the Modifications + created by such Contributor (or portions thereof) either on an + unmodified basis, with other Modifications, as Covered Code + and/or as part of a Larger Work; and + + (b) under Patent Claims infringed by the making, using, or + selling of Modifications made by that Contributor either alone + and/or in combination with its Contributor Version (or portions + of such combination), to make, use, sell, offer for sale, have + made, and/or otherwise dispose of: 1) Modifications made by that + Contributor (or portions thereof); and 2) the combination of + Modifications made by that Contributor with its Contributor + Version (or portions of such combination). + + (c) the licenses granted in Sections 2.2(a) and 2.2(b) are + effective on the date Contributor first makes Commercial Use of + the Covered Code. + + (d) Notwithstanding Section 2.2(b) above, no patent license is + granted: 1) for any code that Contributor has deleted from the + Contributor Version; 2) separate from the Contributor Version; + 3) for infringements caused by: i) third party modifications of + Contributor Version or ii) the combination of Modifications made + by that Contributor with other software (except as part of the + Contributor Version) or other devices; or 4) under Patent Claims + infringed by Covered Code in the absence of Modifications made by + that Contributor. + +3. Distribution Obligations. + + 3.1. Application of License. + The Modifications which You create or to which You contribute are + governed by the terms of this License, including without limitation + Section 2.2. The Source Code version of Covered Code may be + distributed only under the terms of this License or a future version + of this License released under Section 6.1, and You must include a + copy of this License with every copy of the Source Code You + distribute. You may not offer or impose any terms on any Source Code + version that alters or restricts the applicable version of this + License or the recipients' rights hereunder. However, You may include + an additional document offering the additional rights described in + Section 3.5. + + 3.2. Availability of Source Code. + Any Modification which You create or to which You contribute must be + made available in Source Code form under the terms of this License + either on the same media as an Executable version or via an accepted + Electronic Distribution Mechanism to anyone to whom you made an + Executable version available; and if made available via Electronic + Distribution Mechanism, must remain available for at least twelve (12) + months after the date it initially became available, or at least six + (6) months after a subsequent version of that particular Modification + has been made available to such recipients. You are responsible for + ensuring that the Source Code version remains available even if the + Electronic Distribution Mechanism is maintained by a third party. + + 3.3. Description of Modifications. + You must cause all Covered Code to which You contribute to contain a + file documenting the changes You made to create that Covered Code and + the date of any change. You must include a prominent statement that + the Modification is derived, directly or indirectly, from Original + Code provided by the Initial Developer and including the name of the + Initial Developer in (a) the Source Code, and (b) in any notice in an + Executable version or related documentation in which You describe the + origin or ownership of the Covered Code. + + 3.4. Intellectual Property Matters + (a) Third Party Claims. + If Contributor has knowledge that a license under a third party's + intellectual property rights is required to exercise the rights + granted by such Contributor under Sections 2.1 or 2.2, + Contributor must include a text file with the Source Code + distribution titled "LEGAL" which describes the claim and the + party making the claim in sufficient detail that a recipient will + know whom to contact. If Contributor obtains such knowledge after + the Modification is made available as described in Section 3.2, + Contributor shall promptly modify the LEGAL file in all copies + Contributor makes available thereafter and shall take other steps + (such as notifying appropriate mailing lists or newsgroups) + reasonably calculated to inform those who received the Covered + Code that new knowledge has been obtained. + + (b) Contributor APIs. + If Contributor's Modifications include an application programming + interface and Contributor has knowledge of patent licenses which + are reasonably necessary to implement that API, Contributor must + also include this information in the LEGAL file. + + (c) Representations. + Contributor represents that, except as disclosed pursuant to + Section 3.4(a) above, Contributor believes that Contributor's + Modifications are Contributor's original creation(s) and/or + Contributor has sufficient rights to grant the rights conveyed by + this License. + + 3.5. Required Notices. + You must duplicate the notice in Exhibit A in each file of the Source + Code. If it is not possible to put such notice in a particular Source + Code file due to its structure, then You must include such notice in a + location (such as a relevant directory) where a user would be likely + to look for such a notice. If You created one or more Modification(s) + You may add your name as a Contributor to the notice described in + Exhibit A. You must also duplicate this License in any documentation + for the Source Code where You describe recipients' rights or ownership + rights relating to Covered Code. You may choose to offer, and to + charge a fee for, warranty, support, indemnity or liability + obligations to one or more recipients of Covered Code. However, You + may do so only on Your own behalf, and not on behalf of the Initial + Developer or any Contributor. You must make it absolutely clear than + any such warranty, support, indemnity or liability obligation is + offered by You alone, and You hereby agree to indemnify the Initial + Developer and every Contributor for any liability incurred by the + Initial Developer or such Contributor as a result of warranty, + support, indemnity or liability terms You offer. + + 3.6. Distribution of Executable Versions. + You may distribute Covered Code in Executable form only if the + requirements of Section 3.1-3.5 have been met for that Covered Code, + and if You include a notice stating that the Source Code version of + the Covered Code is available under the terms of this License, + including a description of how and where You have fulfilled the + obligations of Section 3.2. The notice must be conspicuously included + in any notice in an Executable version, related documentation or + collateral in which You describe recipients' rights relating to the + Covered Code. You may distribute the Executable version of Covered + Code or ownership rights under a license of Your choice, which may + contain terms different from this License, provided that You are in + compliance with the terms of this License and that the license for the + Executable version does not attempt to limit or alter the recipient's + rights in the Source Code version from the rights set forth in this + License. If You distribute the Executable version under a different + license You must make it absolutely clear that any terms which differ + from this License are offered by You alone, not by the Initial + Developer or any Contributor. You hereby agree to indemnify the + Initial Developer and every Contributor for any liability incurred by + the Initial Developer or such Contributor as a result of any such + terms You offer. + + 3.7. Larger Works. + You may create a Larger Work by combining Covered Code with other code + not governed by the terms of this License and distribute the Larger + Work as a single product. In such a case, You must make sure the + requirements of this License are fulfilled for the Covered Code. + +4. Inability to Comply Due to Statute or Regulation. + + If it is impossible for You to comply with any of the terms of this + License with respect to some or all of the Covered Code due to + statute, judicial order, or regulation then You must: (a) comply with + the terms of this License to the maximum extent possible; and (b) + describe the limitations and the code they affect. Such description + must be included in the LEGAL file described in Section 3.4 and must + be included with all distributions of the Source Code. Except to the + extent prohibited by statute or regulation, such description must be + sufficiently detailed for a recipient of ordinary skill to be able to + understand it. + +5. Application of this License. + + This License applies to code to which the Initial Developer has + attached the notice in Exhibit A and to related Covered Code. + +6. Versions of the License. + + 6.1. New Versions. + Netscape Communications Corporation ("Netscape") may publish revised + and/or new versions of the License from time to time. Each version + will be given a distinguishing version number. + + 6.2. Effect of New Versions. + Once Covered Code has been published under a particular version of the + License, You may always continue to use it under the terms of that + version. You may also choose to use such Covered Code under the terms + of any subsequent version of the License published by Netscape. No one + other than Netscape has the right to modify the terms applicable to + Covered Code created under this License. + + 6.3. Derivative Works. + If You create or use a modified version of this License (which you may + only do in order to apply it to code which is not already Covered Code + governed by this License), You must (a) rename Your license so that + the phrases "Mozilla", "MOZILLAPL", "MOZPL", "Netscape", + "MPL", "NPL" or any confusingly similar phrase do not appear in your + license (except to note that your license differs from this License) + and (b) otherwise make it clear that Your version of the license + contains terms which differ from the Mozilla Public License and + Netscape Public License. (Filling in the name of the Initial + Developer, Original Code or Contributor in the notice described in + Exhibit A shall not of themselves be deemed to be modifications of + this License.) + +7. DISCLAIMER OF WARRANTY. + + COVERED CODE IS PROVIDED UNDER THIS LICENSE ON AN "AS IS" BASIS, + WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, + WITHOUT LIMITATION, WARRANTIES THAT THE COVERED CODE IS FREE OF + DEFECTS, MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE OR NON-INFRINGING. + THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE COVERED CODE + IS WITH YOU. SHOULD ANY COVERED CODE PROVE DEFECTIVE IN ANY RESPECT, + YOU (NOT THE INITIAL DEVELOPER OR ANY OTHER CONTRIBUTOR) ASSUME THE + COST OF ANY NECESSARY SERVICING, REPAIR OR CORRECTION. THIS DISCLAIMER + OF WARRANTY CONSTITUTES AN ESSENTIAL PART OF THIS LICENSE. NO USE OF + ANY COVERED CODE IS AUTHORIZED HEREUNDER EXCEPT UNDER THIS DISCLAIMER. + +8. TERMINATION. + + 8.1. This License and the rights granted hereunder will terminate + automatically if You fail to comply with terms herein and fail to cure + such breach within 30 days of becoming aware of the breach. All + sublicenses to the Covered Code which are properly granted shall + survive any termination of this License. Provisions which, by their + nature, must remain in effect beyond the termination of this License + shall survive. + + 8.2. If You initiate litigation by asserting a patent infringement + claim (excluding declatory judgment actions) against Initial Developer + or a Contributor (the Initial Developer or Contributor against whom + You file such action is referred to as "Participant") alleging that: + + (a) such Participant's Contributor Version directly or indirectly + infringes any patent, then any and all rights granted by such + Participant to You under Sections 2.1 and/or 2.2 of this License + shall, upon 60 days notice from Participant terminate prospectively, + unless if within 60 days after receipt of notice You either: (i) + agree in writing to pay Participant a mutually agreeable reasonable + royalty for Your past and future use of Modifications made by such + Participant, or (ii) withdraw Your litigation claim with respect to + the Contributor Version against such Participant. If within 60 days + of notice, a reasonable royalty and payment arrangement are not + mutually agreed upon in writing by the parties or the litigation claim + is not withdrawn, the rights granted by Participant to You under + Sections 2.1 and/or 2.2 automatically terminate at the expiration of + the 60 day notice period specified above. + + (b) any software, hardware, or device, other than such Participant's + Contributor Version, directly or indirectly infringes any patent, then + any rights granted to You by such Participant under Sections 2.1(b) + and 2.2(b) are revoked effective as of the date You first made, used, + sold, distributed, or had made, Modifications made by that + Participant. + + 8.3. If You assert a patent infringement claim against Participant + alleging that such Participant's Contributor Version directly or + indirectly infringes any patent where such claim is resolved (such as + by license or settlement) prior to the initiation of patent + infringement litigation, then the reasonable value of the licenses + granted by such Participant under Sections 2.1 or 2.2 shall be taken + into account in determining the amount or value of any payment or + license. + + 8.4. In the event of termination under Sections 8.1 or 8.2 above, + all end user license agreements (excluding distributors and resellers) + which have been validly granted by You or any distributor hereunder + prior to termination shall survive termination. + +9. LIMITATION OF LIABILITY. + + UNDER NO CIRCUMSTANCES AND UNDER NO LEGAL THEORY, WHETHER TORT + (INCLUDING NEGLIGENCE), CONTRACT, OR OTHERWISE, SHALL YOU, THE INITIAL + DEVELOPER, ANY OTHER CONTRIBUTOR, OR ANY DISTRIBUTOR OF COVERED CODE, + OR ANY SUPPLIER OF ANY OF SUCH PARTIES, BE LIABLE TO ANY PERSON FOR + ANY INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES OF ANY + CHARACTER INCLUDING, WITHOUT LIMITATION, DAMAGES FOR LOSS OF GOODWILL, + WORK STOPPAGE, COMPUTER FAILURE OR MALFUNCTION, OR ANY AND ALL OTHER + COMMERCIAL DAMAGES OR LOSSES, EVEN IF SUCH PARTY SHALL HAVE BEEN + INFORMED OF THE POSSIBILITY OF SUCH DAMAGES. THIS LIMITATION OF + LIABILITY SHALL NOT APPLY TO LIABILITY FOR DEATH OR PERSONAL INJURY + RESULTING FROM SUCH PARTY'S NEGLIGENCE TO THE EXTENT APPLICABLE LAW + PROHIBITS SUCH LIMITATION. SOME JURISDICTIONS DO NOT ALLOW THE + EXCLUSION OR LIMITATION OF INCIDENTAL OR CONSEQUENTIAL DAMAGES, SO + THIS EXCLUSION AND LIMITATION MAY NOT APPLY TO YOU. + +10. U.S. GOVERNMENT END USERS. + + The Covered Code is a "commercial item," as that term is defined in + 48 C.F.R. 2.101 (Oct. 1995), consisting of "commercial computer + software" and "commercial computer software documentation," as such + terms are used in 48 C.F.R. 12.212 (Sept. 1995). Consistent with 48 + C.F.R. 12.212 and 48 C.F.R. 227.7202-1 through 227.7202-4 (June 1995), + all U.S. Government End Users acquire Covered Code with only those + rights set forth herein. + +11. MISCELLANEOUS. + + This License represents the complete agreement concerning subject + matter hereof. If any provision of this License is held to be + unenforceable, such provision shall be reformed only to the extent + necessary to make it enforceable. This License shall be governed by + California law provisions (except to the extent applicable law, if + any, provides otherwise), excluding its conflict-of-law provisions. + With respect to disputes in which at least one party is a citizen of, + or an entity chartered or registered to do business in the United + States of America, any litigation relating to this License shall be + subject to the jurisdiction of the Federal Courts of the Northern + District of California, with venue lying in Santa Clara County, + California, with the losing party responsible for costs, including + without limitation, court costs and reasonable attorneys' fees and + expenses. The application of the United Nations Convention on + Contracts for the International Sale of Goods is expressly excluded. + Any law or regulation which provides that the language of a contract + shall be construed against the drafter shall not apply to this + License. + +12. RESPONSIBILITY FOR CLAIMS. + + As between Initial Developer and the Contributors, each party is + responsible for claims and damages arising, directly or indirectly, + out of its utilization of rights under this License and You agree to + work with Initial Developer and Contributors to distribute such + responsibility on an equitable basis. Nothing herein is intended or + shall be deemed to constitute any admission of liability. + +13. MULTIPLE-LICENSED CODE. + + Initial Developer may designate portions of the Covered Code as + "Multiple-Licensed". "Multiple-Licensed" means that the Initial + Developer permits you to utilize portions of the Covered Code under + Your choice of the NPL or the alternative licenses, if any, specified + by the Initial Developer in the file described in Exhibit A. + +EXHIBIT A -Mozilla Public License. + + ``The contents of this file are subject to the Mozilla Public License + Version 1.1 (the "License"); you may not use this file except in + compliance with the License. You may obtain a copy of the License at + http://www.mozilla.org/MPL/ + + Software distributed under the License is distributed on an "AS IS" + basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the + License for the specific language governing rights and limitations + under the License. + + The Original Code is RabbitMQ. + + The Initial Developer of the Original Code is GoPivotal, Inc. + Copyright (c) 2007-2014 GoPivotal, Inc. All rights reserved.'' + + [NOTE: The text of this Exhibit A may differ slightly from the text of + the notices in the Source Code files of the Original Code. You should + use the text of this Exhibit A rather than the text found in the + Original Code Source Code for Your Modifications.] diff --git a/rabbitmq-server/Makefile b/rabbitmq-server/Makefile new file mode 100644 index 0000000..c2cae4a --- /dev/null +++ b/rabbitmq-server/Makefile @@ -0,0 +1,437 @@ +TMPDIR ?= /tmp + +RABBITMQ_NODENAME ?= rabbit +RABBITMQ_SERVER_START_ARGS ?= +RABBITMQ_MNESIA_DIR ?= $(TMPDIR)/rabbitmq-$(RABBITMQ_NODENAME)-mnesia +RABBITMQ_PLUGINS_EXPAND_DIR ?= $(TMPDIR)/rabbitmq-$(RABBITMQ_NODENAME)-plugins-scratch +RABBITMQ_LOG_BASE ?= $(TMPDIR) + +DEPS_FILE=deps.mk +SOURCE_DIR=src +TEST_DIR=test/src +EBIN_DIR=ebin +TEST_EBIN_DIR=test/ebin +INCLUDE_DIR=include +DOCS_DIR=docs +INCLUDES=$(wildcard $(INCLUDE_DIR)/*.hrl) $(INCLUDE_DIR)/rabbit_framing.hrl +SOURCES=$(wildcard $(SOURCE_DIR)/*.erl) $(SOURCE_DIR)/rabbit_framing_amqp_0_9_1.erl $(SOURCE_DIR)/rabbit_framing_amqp_0_8.erl $(USAGES_ERL) +TEST_SOURCES=$(wildcard $(TEST_DIR)/*.erl) +BEAM_TARGETS=$(patsubst $(SOURCE_DIR)/%.erl, $(EBIN_DIR)/%.beam, $(SOURCES)) +TEST_BEAM_TARGETS=$(patsubst $(TEST_DIR)/%.erl, $(TEST_EBIN_DIR)/%.beam, $(TEST_SOURCES)) +TARGETS=$(EBIN_DIR)/rabbit.app $(INCLUDE_DIR)/rabbit_framing.hrl $(BEAM_TARGETS) plugins +TEST_TARGETS=$(TEST_BEAM_TARGETS) +WEB_URL=http://www.rabbitmq.com/ +MANPAGES=$(patsubst %.xml, %.gz, $(wildcard $(DOCS_DIR)/*.[0-9].xml)) +WEB_MANPAGES=$(patsubst %.xml, %.man.xml, $(wildcard $(DOCS_DIR)/*.[0-9].xml) $(DOCS_DIR)/rabbitmq-service.xml $(DOCS_DIR)/rabbitmq-echopid.xml) +USAGES_XML=$(DOCS_DIR)/rabbitmqctl.1.xml $(DOCS_DIR)/rabbitmq-plugins.1.xml +USAGES_ERL=$(foreach XML, $(USAGES_XML), $(call usage_xml_to_erl, $(XML))) + +ifeq ($(shell python -c 'import simplejson' 2>/dev/null && echo yes),yes) +PYTHON=python +else +ifeq ($(shell python2.7 -c 'import json' 2>/dev/null && echo yes),yes) +PYTHON=python2.7 +else +ifeq ($(shell python2.6 -c 'import simplejson' 2>/dev/null && echo yes),yes) +PYTHON=python2.6 +else +ifeq ($(shell python2.5 -c 'import simplejson' 2>/dev/null && echo yes),yes) +PYTHON=python2.5 +else +# Hmm. Missing simplejson? +PYTHON=python +endif +endif +endif +endif + +BASIC_PLT=basic.plt +RABBIT_PLT=rabbit.plt + +ifndef USE_PROPER_QC +# PropEr needs to be installed for property checking +# http://proper.softlab.ntua.gr/ +USE_PROPER_QC=$(shell erl -noshell -eval 'io:format({module, proper} =:= code:ensure_loaded(proper)), halt().') +endif + +#other args: +native +"{hipe,[o3,verbose]}" -Ddebug=true +debug_info +no_strict_record_tests +ERLC_OPTS=-I $(INCLUDE_DIR) -Wall +warn_export_vars -v +debug_info $(call boolean_macro,$(USE_SPECS),use_specs) $(call boolean_macro,$(USE_PROPER_QC),use_proper_qc) + +# Our type specs rely on dict:dict/0 etc, which are only available in +# 17.0 upwards. +define compare_version +$(shell awk 'BEGIN { + split("$(1)", v1, "\."); + version1 = v1[1] * 1000000 + v1[2] * 10000 + v1[3] * 100 + v1[4]; + + split("$(2)", v2, "\."); + version2 = v2[1] * 1000000 + v2[2] * 10000 + v2[3] * 100 + v2[4]; + + if (version1 $(3) version2) { + print "true"; + } else { + print "false"; + } +}') +endef + +ERTS_VER = $(shell erl -version 2>&1 | sed -E 's/.* version //') +USE_SPECS_MIN_ERTS_VER = 5.11 +ifeq ($(call compare_version,$(ERTS_VER),$(USE_SPECS_MIN_ERTS_VER),>=),true) +ERLC_OPTS += -Duse_specs +endif + +ifdef INSTRUMENT_FOR_QC +ERLC_OPTS += -DINSTR_MOD=gm_qc +else +ERLC_OPTS += -DINSTR_MOD=gm +endif + +include version.mk + +PLUGINS_SRC_DIR?=$(shell [ -d "plugins-src" ] && echo "plugins-src" || echo ) +PLUGINS_DIR=plugins +TARBALL_NAME=rabbitmq-server-$(VERSION) +TARGET_SRC_DIR=dist/$(TARBALL_NAME) + +SIBLING_CODEGEN_DIR=../rabbitmq-codegen/ +AMQP_CODEGEN_DIR=$(shell [ -d $(SIBLING_CODEGEN_DIR) ] && echo $(SIBLING_CODEGEN_DIR) || echo codegen) +AMQP_SPEC_JSON_FILES_0_9_1=$(AMQP_CODEGEN_DIR)/amqp-rabbitmq-0.9.1.json $(AMQP_CODEGEN_DIR)/credit_extension.json +AMQP_SPEC_JSON_FILES_0_8=$(AMQP_CODEGEN_DIR)/amqp-rabbitmq-0.8.json + +ERL_CALL=erl_call -sname $(RABBITMQ_NODENAME) -e + +ERL_EBIN=erl -noinput -pa $(EBIN_DIR) + +define usage_xml_to_erl + $(subst __,_,$(patsubst $(DOCS_DIR)/rabbitmq%.1.xml, $(SOURCE_DIR)/rabbit_%_usage.erl, $(subst -,_,$(1)))) +endef + +define usage_dep + $(call usage_xml_to_erl, $(1)): $(1) $(DOCS_DIR)/usage.xsl +endef + +define boolean_macro +$(if $(filter true,$(1)),-D$(2)) +endef + +ifneq "$(SBIN_DIR)" "" +ifneq "$(TARGET_DIR)" "" +SCRIPTS_REL_PATH=$(shell ./calculate-relative $(TARGET_DIR)/sbin $(SBIN_DIR)) +endif +endif + +# Versions prior to this are not supported +NEED_MAKE := 3.80 +ifneq "$(NEED_MAKE)" "$(firstword $(sort $(NEED_MAKE) $(MAKE_VERSION)))" +$(error Versions of make prior to $(NEED_MAKE) are not supported) +endif + +# .DEFAULT_GOAL introduced in 3.81 +DEFAULT_GOAL_MAKE := 3.81 +ifneq "$(DEFAULT_GOAL_MAKE)" "$(firstword $(sort $(DEFAULT_GOAL_MAKE) $(MAKE_VERSION)))" +.DEFAULT_GOAL=all +endif + +all: $(TARGETS) $(TEST_TARGETS) + +.PHONY: plugins check-xref +ifneq "$(PLUGINS_SRC_DIR)" "" +plugins: + [ -d "$(PLUGINS_SRC_DIR)/rabbitmq-server" ] || ln -s "$(CURDIR)" "$(PLUGINS_SRC_DIR)/rabbitmq-server" + mkdir -p $(PLUGINS_DIR) + PLUGINS_SRC_DIR="" $(MAKE) -C "$(PLUGINS_SRC_DIR)" plugins-dist PLUGINS_DIST_DIR="$(CURDIR)/$(PLUGINS_DIR)" VERSION=$(VERSION) + echo "Put your EZs here and use rabbitmq-plugins to enable them." > $(PLUGINS_DIR)/README + rm -f $(PLUGINS_DIR)/rabbit_common*.ez + +# add -q to remove printout of warnings.... +check-xref: $(BEAM_TARGETS) $(PLUGINS_DIR) + rm -rf lib + ./check_xref $(PLUGINS_DIR) -q + +else +plugins: +# Not building plugins + +check-xref: + $(info xref checks are disabled as there is no plugins-src directory) + +endif + +$(DEPS_FILE): $(SOURCES) $(INCLUDES) + rm -f $@ + echo $(subst : ,:,$(foreach FILE,$^,$(FILE):)) | escript generate_deps $@ $(EBIN_DIR) + +$(EBIN_DIR)/rabbit.app: $(EBIN_DIR)/rabbit_app.in $(SOURCES) generate_app + escript generate_app $< $@ $(SOURCE_DIR) + +$(EBIN_DIR)/%.beam: $(SOURCE_DIR)/%.erl | $(DEPS_FILE) + erlc -o $(EBIN_DIR) $(ERLC_OPTS) -pa $(EBIN_DIR) $< + +$(TEST_EBIN_DIR)/%.beam: $(TEST_DIR)/%.erl | $(TEST_EBIN_DIR) + erlc -o $(TEST_EBIN_DIR) $(ERLC_OPTS) -pa $(EBIN_DIR) -pa $(TEST_EBIN_DIR) $< + +$(TEST_EBIN_DIR): + mkdir -p $(TEST_EBIN_DIR) + +$(INCLUDE_DIR)/rabbit_framing.hrl: codegen.py $(AMQP_CODEGEN_DIR)/amqp_codegen.py $(AMQP_SPEC_JSON_FILES_0_9_1) $(AMQP_SPEC_JSON_FILES_0_8) + $(PYTHON) codegen.py --ignore-conflicts header $(AMQP_SPEC_JSON_FILES_0_9_1) $(AMQP_SPEC_JSON_FILES_0_8) $@ + +$(SOURCE_DIR)/rabbit_framing_amqp_0_9_1.erl: codegen.py $(AMQP_CODEGEN_DIR)/amqp_codegen.py $(AMQP_SPEC_JSON_FILES_0_9_1) + $(PYTHON) codegen.py body $(AMQP_SPEC_JSON_FILES_0_9_1) $@ + +$(SOURCE_DIR)/rabbit_framing_amqp_0_8.erl: codegen.py $(AMQP_CODEGEN_DIR)/amqp_codegen.py $(AMQP_SPEC_JSON_FILES_0_8) + $(PYTHON) codegen.py body $(AMQP_SPEC_JSON_FILES_0_8) $@ + +dialyze: $(BEAM_TARGETS) $(BASIC_PLT) + dialyzer --plt $(BASIC_PLT) --no_native --fullpath \ + $(BEAM_TARGETS) + +# rabbit.plt is used by rabbitmq-erlang-client's dialyze make target +create-plt: $(RABBIT_PLT) + +$(RABBIT_PLT): $(BEAM_TARGETS) $(BASIC_PLT) + dialyzer --plt $(BASIC_PLT) --output_plt $@ --no_native \ + --add_to_plt $(BEAM_TARGETS) + +$(BASIC_PLT): $(BEAM_TARGETS) + if [ -f $@ ]; then \ + touch $@; \ + else \ + dialyzer --output_plt $@ --build_plt \ + --apps erts kernel stdlib compiler sasl os_mon mnesia tools \ + public_key crypto ssl xmerl; \ + fi + +clean: + rm -f $(EBIN_DIR)/*.beam + rm -f $(EBIN_DIR)/rabbit.app $(EBIN_DIR)/rabbit.boot $(EBIN_DIR)/rabbit.script $(EBIN_DIR)/rabbit.rel + rm -rf $(TEST_EBIN_DIR) + rm -f $(PLUGINS_DIR)/*.ez + [ -d "$(PLUGINS_SRC_DIR)" ] && PLUGINS_SRC_DIR="" PRESERVE_CLONE_DIR=1 make -C $(PLUGINS_SRC_DIR) clean || true + rm -f $(INCLUDE_DIR)/rabbit_framing.hrl $(SOURCE_DIR)/rabbit_framing_amqp_*.erl codegen.pyc + rm -f $(DOCS_DIR)/*.[0-9].gz $(DOCS_DIR)/*.man.xml $(DOCS_DIR)/*.erl $(USAGES_ERL) + rm -f $(RABBIT_PLT) + rm -f $(DEPS_FILE) + +cleandb: + rm -rf $(RABBITMQ_MNESIA_DIR)/* + +############ various tasks to interact with RabbitMQ ################### + +BASIC_SCRIPT_ENVIRONMENT_SETTINGS=\ + RABBITMQ_NODE_IP_ADDRESS="$(RABBITMQ_NODE_IP_ADDRESS)" \ + RABBITMQ_NODE_PORT="$(RABBITMQ_NODE_PORT)" \ + RABBITMQ_LOG_BASE="$(RABBITMQ_LOG_BASE)" \ + RABBITMQ_MNESIA_DIR="$(RABBITMQ_MNESIA_DIR)" \ + RABBITMQ_PLUGINS_EXPAND_DIR="$(RABBITMQ_PLUGINS_EXPAND_DIR)" + +run: all + $(BASIC_SCRIPT_ENVIRONMENT_SETTINGS) \ + RABBITMQ_ALLOW_INPUT=true \ + RABBITMQ_SERVER_START_ARGS="$(RABBITMQ_SERVER_START_ARGS)" \ + ./scripts/rabbitmq-server + +run-background: all + $(BASIC_SCRIPT_ENVIRONMENT_SETTINGS) \ + RABBITMQ_SERVER_START_ARGS="$(RABBITMQ_SERVER_START_ARGS)" \ + ./scripts/rabbitmq-server -detached + +run-node: all + $(BASIC_SCRIPT_ENVIRONMENT_SETTINGS) \ + RABBITMQ_NODE_ONLY=true \ + RABBITMQ_ALLOW_INPUT=true \ + RABBITMQ_SERVER_START_ARGS="$(RABBITMQ_SERVER_START_ARGS)" \ + ./scripts/rabbitmq-server + +run-background-node: all + $(BASIC_SCRIPT_ENVIRONMENT_SETTINGS) \ + RABBITMQ_NODE_ONLY=true \ + RABBITMQ_SERVER_START_ARGS="$(RABBITMQ_SERVER_START_ARGS)" \ + ./scripts/rabbitmq-server -detached + +run-tests: all + echo 'code:add_path("$(TEST_EBIN_DIR)").' | $(ERL_CALL) + echo 'code:add_path("$(TEST_EBIN_DIR)").' | $(ERL_CALL) -n hare || true + OUT=$$(echo "rabbit_tests:all_tests()." | $(ERL_CALL)) ; \ + echo $$OUT ; echo $$OUT | grep '^{ok, passed}$$' > /dev/null + +run-qc: all + echo 'code:add_path("$(TEST_EBIN_DIR)").' | $(ERL_CALL) + ./quickcheck $(RABBITMQ_NODENAME) rabbit_backing_queue_qc 100 40 + ./quickcheck $(RABBITMQ_NODENAME) gm_qc 1000 200 + +start-background-node: all + -rm -f $(RABBITMQ_MNESIA_DIR).pid + mkdir -p $(RABBITMQ_MNESIA_DIR) + $(BASIC_SCRIPT_ENVIRONMENT_SETTINGS) \ + RABBITMQ_NODE_ONLY=true \ + RABBITMQ_SERVER_START_ARGS="$(RABBITMQ_SERVER_START_ARGS)" \ + ./scripts/rabbitmq-server \ + > $(RABBITMQ_MNESIA_DIR)/startup_log \ + 2> $(RABBITMQ_MNESIA_DIR)/startup_err & + ./scripts/rabbitmqctl -n $(RABBITMQ_NODENAME) wait $(RABBITMQ_MNESIA_DIR).pid kernel + +start-rabbit-on-node: all + echo "rabbit:start()." | $(ERL_CALL) + ./scripts/rabbitmqctl -n $(RABBITMQ_NODENAME) wait $(RABBITMQ_MNESIA_DIR).pid + +stop-rabbit-on-node: all + echo "rabbit:stop()." | $(ERL_CALL) + +set-resource-alarm: all + echo "rabbit_alarm:set_alarm({{resource_limit, $(SOURCE), node()}, []})." | \ + $(ERL_CALL) + +clear-resource-alarm: all + echo "rabbit_alarm:clear_alarm({resource_limit, $(SOURCE), node()})." | \ + $(ERL_CALL) + +stop-node: + -( \ + pid=$$(./scripts/rabbitmqctl -n $(RABBITMQ_NODENAME) eval 'os:getpid().') && \ + $(ERL_CALL) -q && \ + while ps -p $$pid >/dev/null 2>&1; do sleep 1; done \ + ) + +# code coverage will be created for subdirectory "ebin" of COVER_DIR +COVER_DIR=. + +start-cover: all + echo "rabbit_misc:start_cover([\"rabbit\", \"hare\"])." | $(ERL_CALL) + echo "rabbit_misc:enable_cover([\"$(COVER_DIR)\"])." | $(ERL_CALL) + +stop-cover: all + echo "rabbit_misc:report_cover(), cover:stop()." | $(ERL_CALL) + cat cover/summary.txt + +######################################################################## + +srcdist: distclean + mkdir -p $(TARGET_SRC_DIR)/codegen + cp -r ebin src include LICENSE LICENSE-MPL-RabbitMQ INSTALL README $(TARGET_SRC_DIR) + sed 's/%%VSN%%/$(VERSION)/' $(TARGET_SRC_DIR)/ebin/rabbit_app.in > $(TARGET_SRC_DIR)/ebin/rabbit_app.in.tmp && \ + mv $(TARGET_SRC_DIR)/ebin/rabbit_app.in.tmp $(TARGET_SRC_DIR)/ebin/rabbit_app.in + + cp -r $(AMQP_CODEGEN_DIR)/* $(TARGET_SRC_DIR)/codegen/ + cp codegen.py Makefile generate_app generate_deps calculate-relative $(TARGET_SRC_DIR) + + echo "VERSION?=${VERSION}" > $(TARGET_SRC_DIR)/version.mk + + cp -r scripts $(TARGET_SRC_DIR) + cp -r $(DOCS_DIR) $(TARGET_SRC_DIR) + chmod 0755 $(TARGET_SRC_DIR)/scripts/* + +ifneq "$(PLUGINS_SRC_DIR)" "" + cp -r $(PLUGINS_SRC_DIR) $(TARGET_SRC_DIR)/plugins-src + rm $(TARGET_SRC_DIR)/LICENSE + cat packaging/common/LICENSE.head >> $(TARGET_SRC_DIR)/LICENSE + cat $(AMQP_CODEGEN_DIR)/license_info >> $(TARGET_SRC_DIR)/LICENSE + find $(PLUGINS_SRC_DIR)/licensing -name "license_info_*" -exec cat '{}' >> $(TARGET_SRC_DIR)/LICENSE \; + cat packaging/common/LICENSE.tail >> $(TARGET_SRC_DIR)/LICENSE + find $(PLUGINS_SRC_DIR)/licensing -name "LICENSE-*" -exec cp '{}' $(TARGET_SRC_DIR) \; + rm -rf $(TARGET_SRC_DIR)/licensing +else + @echo No plugins source distribution found +endif + + (cd dist; tar -zchf $(TARBALL_NAME).tar.gz $(TARBALL_NAME)) + (cd dist; zip -q -r $(TARBALL_NAME).zip $(TARBALL_NAME)) + rm -rf $(TARGET_SRC_DIR) + +distclean: clean + $(MAKE) -C $(AMQP_CODEGEN_DIR) distclean + rm -rf dist + find . -regex '.*\(~\|#\|\.swp\|\.dump\)' -exec rm {} \; + +# xmlto can not read from standard input, so we mess with a tmp file. +%.gz: %.xml $(DOCS_DIR)/examples-to-end.xsl + xmlto --version | grep -E '^xmlto version 0\.0\.([0-9]|1[1-8])$$' >/dev/null || opt='--stringparam man.indent.verbatims=0' ; \ + xsltproc --novalid $(DOCS_DIR)/examples-to-end.xsl $< > $<.tmp && \ + xmlto -o $(DOCS_DIR) $$opt man $<.tmp && \ + gzip -f $(DOCS_DIR)/`basename $< .xml` + rm -f $<.tmp + +# Use tmp files rather than a pipeline so that we get meaningful errors +# Do not fold the cp into previous line, it's there to stop the file being +# generated but empty if we fail +$(SOURCE_DIR)/%_usage.erl: + xsltproc --novalid --stringparam modulename "`basename $@ .erl`" \ + $(DOCS_DIR)/usage.xsl $< > $@.tmp + sed -e 's/"/\\"/g' -e 's/%QUOTE%/"/g' $@.tmp > $@.tmp2 + fold -s $@.tmp2 > $@.tmp3 + mv $@.tmp3 $@ + rm $@.tmp $@.tmp2 + +# We rename the file before xmlto sees it since xmlto will use the name of +# the file to make internal links. +%.man.xml: %.xml $(DOCS_DIR)/html-to-website-xml.xsl + cp $< `basename $< .xml`.xml && \ + xmlto xhtml-nochunks `basename $< .xml`.xml ; rm `basename $< .xml`.xml + cat `basename $< .xml`.html | \ + xsltproc --novalid $(DOCS_DIR)/remove-namespaces.xsl - | \ + xsltproc --novalid --stringparam original `basename $<` $(DOCS_DIR)/html-to-website-xml.xsl - | \ + xmllint --format - > $@ + rm `basename $< .xml`.html + +docs_all: $(MANPAGES) $(WEB_MANPAGES) + +install: install_bin install_docs + +install_bin: all install_dirs + cp -r ebin include LICENSE* INSTALL $(TARGET_DIR) + + chmod 0755 scripts/* + for script in rabbitmq-env rabbitmq-server rabbitmqctl rabbitmq-plugins rabbitmq-defaults; do \ + cp scripts/$$script $(TARGET_DIR)/sbin; \ + [ -e $(SBIN_DIR)/$$script ] || ln -s $(SCRIPTS_REL_PATH)/$$script $(SBIN_DIR)/$$script; \ + done + + mkdir -p $(TARGET_DIR)/$(PLUGINS_DIR) + [ -d "$(PLUGINS_DIR)" ] && cp $(PLUGINS_DIR)/*.ez $(PLUGINS_DIR)/README $(TARGET_DIR)/$(PLUGINS_DIR) || true + +install_docs: docs_all install_dirs + for section in 1 5; do \ + mkdir -p $(MAN_DIR)/man$$section; \ + for manpage in $(DOCS_DIR)/*.$$section.gz; do \ + cp $$manpage $(MAN_DIR)/man$$section; \ + done; \ + done + if test "$(DOC_INSTALL_DIR)"; then \ + cp $(DOCS_DIR)/rabbitmq.config.example $(DOC_INSTALL_DIR)/rabbitmq.config.example; \ + fi + +install_dirs: + @ OK=true && \ + { [ -n "$(TARGET_DIR)" ] || { echo "Please set TARGET_DIR."; OK=false; }; } && \ + { [ -n "$(SBIN_DIR)" ] || { echo "Please set SBIN_DIR."; OK=false; }; } && \ + { [ -n "$(MAN_DIR)" ] || { echo "Please set MAN_DIR."; OK=false; }; } && $$OK + + mkdir -p $(TARGET_DIR)/sbin + mkdir -p $(SBIN_DIR) + mkdir -p $(MAN_DIR) + if test "$(DOC_INSTALL_DIR)"; then \ + mkdir -p $(DOC_INSTALL_DIR); \ + fi + +$(foreach XML,$(USAGES_XML),$(eval $(call usage_dep, $(XML)))) + +# Note that all targets which depend on clean must have clean in their +# name. Also any target that doesn't depend on clean should not have +# clean in its name, unless you know that you don't need any of the +# automatic dependency generation for that target (e.g. cleandb). + +# We want to load the dep file if *any* target *doesn't* contain +# "clean" - i.e. if removing all clean-like targets leaves something. + +ifeq "$(MAKECMDGOALS)" "" +TESTABLEGOALS:=$(.DEFAULT_GOAL) +else +TESTABLEGOALS:=$(MAKECMDGOALS) +endif + +ifneq "$(strip $(patsubst clean%,,$(patsubst %clean,,$(TESTABLEGOALS))))" "" +include $(DEPS_FILE) +endif + +.PHONY: run-qc diff --git a/rabbitmq-server/README b/rabbitmq-server/README new file mode 100644 index 0000000..67e3a66 --- /dev/null +++ b/rabbitmq-server/README @@ -0,0 +1 @@ +Please see http://www.rabbitmq.com/build-server.html for build instructions. diff --git a/rabbitmq-server/calculate-relative b/rabbitmq-server/calculate-relative new file mode 100755 index 0000000..3af18e8 --- /dev/null +++ b/rabbitmq-server/calculate-relative @@ -0,0 +1,45 @@ +#!/usr/bin/env python +# +# relpath.py +# R.Barran 30/08/2004 +# Retrieved from http://code.activestate.com/recipes/302594/ + +import os +import sys + +def relpath(target, base=os.curdir): + """ + Return a relative path to the target from either the current dir or an optional base dir. + Base can be a directory specified either as absolute or relative to current dir. + """ + + if not os.path.exists(target): + raise OSError, 'Target does not exist: '+target + + if not os.path.isdir(base): + raise OSError, 'Base is not a directory or does not exist: '+base + + base_list = (os.path.abspath(base)).split(os.sep) + target_list = (os.path.abspath(target)).split(os.sep) + + # On the windows platform the target may be on a completely different drive from the base. + if os.name in ['nt','dos','os2'] and base_list[0] <> target_list[0]: + raise OSError, 'Target is on a different drive to base. Target: '+target_list[0].upper()+', base: '+base_list[0].upper() + + # Starting from the filepath root, work out how much of the filepath is + # shared by base and target. + for i in range(min(len(base_list), len(target_list))): + if base_list[i] <> target_list[i]: break + else: + # If we broke out of the loop, i is pointing to the first differing path elements. + # If we didn't break out of the loop, i is pointing to identical path elements. + # Increment i so that in all cases it points to the first differing path elements. + i+=1 + + rel_list = [os.pardir] * (len(base_list)-i) + target_list[i:] + if (len(rel_list) == 0): + return "." + return os.path.join(*rel_list) + +if __name__ == "__main__": + print(relpath(sys.argv[1], sys.argv[2])) diff --git a/rabbitmq-server/codegen.py b/rabbitmq-server/codegen.py new file mode 100644 index 0000000..fbc6f61 --- /dev/null +++ b/rabbitmq-server/codegen.py @@ -0,0 +1,595 @@ +## The contents of this file are subject to the Mozilla Public License +## Version 1.1 (the "License"); you may not use this file except in +## compliance with the License. You may obtain a copy of the License +## at http://www.mozilla.org/MPL/ +## +## Software distributed under the License is distributed on an "AS IS" +## basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See +## the License for the specific language governing rights and +## limitations under the License. +## +## The Original Code is RabbitMQ. +## +## The Initial Developer of the Original Code is Pivotal Software, Inc. +## Copyright (c) 2007-2015 Pivotal Software, Inc. All rights reserved. +## + +from __future__ import nested_scopes + +import sys +sys.path.append("../rabbitmq-codegen") # in case we're next to an experimental revision +sys.path.append("codegen") # in case we're building from a distribution package + +from amqp_codegen import * +import string +import re + +# Coming up with a proper encoding of AMQP tables in JSON is too much +# hassle at this stage. Given that the only default value we are +# interested in is for the empty table, we only support that. +def convertTable(d): + if len(d) == 0: + return "[]" + else: + raise Exception('Non-empty table defaults not supported ' + d) + +erlangDefaultValueTypeConvMap = { + bool : lambda x: str(x).lower(), + str : lambda x: "<<\"" + x + "\">>", + int : lambda x: str(x), + float : lambda x: str(x), + dict: convertTable, + unicode: lambda x: "<<\"" + x.encode("utf-8") + "\">>" +} + +def erlangize(s): + s = s.replace('-', '_') + s = s.replace(' ', '_') + return s + +AmqpMethod.erlangName = lambda m: "'" + erlangize(m.klass.name) + '.' + erlangize(m.name) + "'" + +AmqpClass.erlangName = lambda c: "'" + erlangize(c.name) + "'" + +def erlangConstantName(s): + return '_'.join(re.split('[- ]', s.upper())) + +class PackedMethodBitField: + def __init__(self, index): + self.index = index + self.domain = 'bit' + self.contents = [] + + def extend(self, f): + self.contents.append(f) + + def count(self): + return len(self.contents) + + def full(self): + return self.count() == 8 + +def multiLineFormat(things, prologue, separator, lineSeparator, epilogue, thingsPerLine = 4): + r = [prologue] + i = 0 + for t in things: + if i != 0: + if i % thingsPerLine == 0: + r += [lineSeparator] + else: + r += [separator] + r += [t] + i += 1 + r += [epilogue] + return "".join(r) + +def prettyType(typeName, subTypes, typesPerLine = 4): + """Pretty print a type signature made up of many alternative subtypes""" + sTs = multiLineFormat(subTypes, + "( ", " | ", "\n | ", " )", + thingsPerLine = typesPerLine) + return "-type(%s ::\n %s)." % (typeName, sTs) + +def printFileHeader(): + print """%% Autogenerated code. Do not edit. +%% +%% The contents of this file are subject to the Mozilla Public License +%% Version 1.1 (the "License"); you may not use this file except in +%% compliance with the License. You may obtain a copy of the License +%% at http://www.mozilla.org/MPL/ +%% +%% Software distributed under the License is distributed on an "AS IS" +%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See +%% the License for the specific language governing rights and +%% limitations under the License. +%% +%% The Original Code is RabbitMQ. +%% +%% The Initial Developer of the Original Code is Pivotal Software, Inc. +%% Copyright (c) 2007-2015 Pivotal Software, Inc. All rights reserved. +%%""" + +def genErl(spec): + def erlType(domain): + return erlangize(spec.resolveDomain(domain)) + + def fieldTypeList(fields): + return '[' + ', '.join([erlType(f.domain) for f in fields]) + ']' + + def fieldNameList(fields): + return '[' + ', '.join([erlangize(f.name) for f in fields]) + ']' + + def fieldTempList(fields): + return '[' + ', '.join(['F' + str(f.index) for f in fields]) + ']' + + def fieldMapList(fields): + return ', '.join([erlangize(f.name) + " = F" + str(f.index) for f in fields]) + + def genLookupMethodName(m): + print "lookup_method_name({%d, %d}) -> %s;" % (m.klass.index, m.index, m.erlangName()) + + def genLookupClassName(c): + print "lookup_class_name(%d) -> %s;" % (c.index, c.erlangName()) + + def genMethodId(m): + print "method_id(%s) -> {%d, %d};" % (m.erlangName(), m.klass.index, m.index) + + def genMethodHasContent(m): + print "method_has_content(%s) -> %s;" % (m.erlangName(), str(m.hasContent).lower()) + + def genMethodIsSynchronous(m): + hasNoWait = "nowait" in fieldNameList(m.arguments) + if m.isSynchronous and hasNoWait: + print "is_method_synchronous(#%s{nowait = NoWait}) -> not(NoWait);" % (m.erlangName()) + else: + print "is_method_synchronous(#%s{}) -> %s;" % (m.erlangName(), str(m.isSynchronous).lower()) + + def genMethodFieldTypes(m): + """Not currently used - may be useful in future?""" + print "method_fieldtypes(%s) -> %s;" % (m.erlangName(), fieldTypeList(m.arguments)) + + def genMethodFieldNames(m): + print "method_fieldnames(%s) -> %s;" % (m.erlangName(), fieldNameList(m.arguments)) + + def packMethodFields(fields): + packed = [] + bitfield = None + for f in fields: + if erlType(f.domain) == 'bit': + if not(bitfield) or bitfield.full(): + bitfield = PackedMethodBitField(f.index) + packed.append(bitfield) + bitfield.extend(f) + else: + bitfield = None + packed.append(f) + return packed + + def methodFieldFragment(f): + type = erlType(f.domain) + p = 'F' + str(f.index) + if type == 'shortstr': + return p+'Len:8/unsigned, '+p+':'+p+'Len/binary' + elif type == 'longstr': + return p+'Len:32/unsigned, '+p+':'+p+'Len/binary' + elif type == 'octet': + return p+':8/unsigned' + elif type == 'short': + return p+':16/unsigned' + elif type == 'long': + return p+':32/unsigned' + elif type == 'longlong': + return p+':64/unsigned' + elif type == 'timestamp': + return p+':64/unsigned' + elif type == 'bit': + return p+'Bits:8' + elif type == 'table': + return p+'Len:32/unsigned, '+p+'Tab:'+p+'Len/binary' + + def genFieldPostprocessing(packed, hasContent): + for f in packed: + type = erlType(f.domain) + if type == 'bit': + for index in range(f.count()): + print " F%d = ((F%dBits band %d) /= 0)," % \ + (f.index + index, + f.index, + 1 << index) + elif type == 'table': + print " F%d = rabbit_binary_parser:parse_table(F%dTab)," % \ + (f.index, f.index) + # We skip the check on content-bearing methods for + # speed. This is a sanity check, not a security thing. + elif type == 'shortstr' and not hasContent: + print " rabbit_binary_parser:assert_utf8(F%d)," % (f.index) + else: + pass + + def genMethodRecord(m): + print "method_record(%s) -> #%s{};" % (m.erlangName(), m.erlangName()) + + def genDecodeMethodFields(m): + packedFields = packMethodFields(m.arguments) + binaryPattern = ', '.join([methodFieldFragment(f) for f in packedFields]) + if binaryPattern: + restSeparator = ', ' + else: + restSeparator = '' + recordConstructorExpr = '#%s{%s}' % (m.erlangName(), fieldMapList(m.arguments)) + print "decode_method_fields(%s, <<%s>>) ->" % (m.erlangName(), binaryPattern) + genFieldPostprocessing(packedFields, m.hasContent) + print " %s;" % (recordConstructorExpr,) + + def genDecodeProperties(c): + def presentBin(fields): + ps = ', '.join(['P' + str(f.index) + ':1' for f in fields]) + return '<<' + ps + ', _:%d, R0/binary>>' % (16 - len(fields),) + def writePropFieldLine(field): + i = str(field.index) + if field.domain == 'bit': + print " {F%s, R%s} = {P%s =/= 0, R%s}," % \ + (i, str(field.index + 1), i, i) + else: + print " {F%s, R%s} = if P%s =:= 0 -> {undefined, R%s}; true -> ?%s_VAL(R%s, L%s, V%s, X%s) end," % \ + (i, str(field.index + 1), i, i, erlType(field.domain).upper(), i, i, i, i) + + if len(c.fields) == 0: + print "decode_properties(%d, <<>>) ->" % (c.index,) + else: + print ("decode_properties(%d, %s) ->" % + (c.index, presentBin(c.fields))) + for field in c.fields: + writePropFieldLine(field) + print " <<>> = %s," % ('R' + str(len(c.fields))) + print " #'P_%s'{%s};" % (erlangize(c.name), fieldMapList(c.fields)) + + def genFieldPreprocessing(packed): + for f in packed: + type = erlType(f.domain) + if type == 'bit': + print " F%dBits = (%s)," % \ + (f.index, + ' bor '.join(['(bitvalue(F%d) bsl %d)' % (x.index, x.index - f.index) + for x in f.contents])) + elif type == 'table': + print " F%dTab = rabbit_binary_generator:generate_table(F%d)," % (f.index, f.index) + print " F%dLen = size(F%dTab)," % (f.index, f.index) + elif type == 'shortstr': + print " F%dLen = shortstr_size(F%d)," % (f.index, f.index) + elif type == 'longstr': + print " F%dLen = size(F%d)," % (f.index, f.index) + else: + pass + + def genEncodeMethodFields(m): + packedFields = packMethodFields(m.arguments) + print "encode_method_fields(#%s{%s}) ->" % (m.erlangName(), fieldMapList(m.arguments)) + genFieldPreprocessing(packedFields) + print " <<%s>>;" % (', '.join([methodFieldFragment(f) for f in packedFields])) + + def genEncodeProperties(c): + def presentBin(fields): + ps = ', '.join(['P' + str(f.index) + ':1' for f in fields]) + return '<<' + ps + ', 0:%d>>' % (16 - len(fields),) + def writePropFieldLine(field): + i = str(field.index) + if field.domain == 'bit': + print " {P%s, R%s} = {F%s =:= 1, R%s}," % \ + (i, str(field.index + 1), i, i) + else: + print " {P%s, R%s} = if F%s =:= undefined -> {0, R%s}; true -> {1, [?%s_PROP(F%s, L%s) | R%s]} end," % \ + (i, str(field.index + 1), i, i, erlType(field.domain).upper(), i, i, i) + + print "encode_properties(#'P_%s'{%s}) ->" % (erlangize(c.name), fieldMapList(c.fields)) + if len(c.fields) == 0: + print " <<>>;" + else: + print " R0 = [<<>>]," + for field in c.fields: + writePropFieldLine(field) + print " list_to_binary([%s | lists:reverse(R%s)]);" % \ + (presentBin(c.fields), str(len(c.fields))) + + def messageConstantClass(cls): + # We do this because 0.8 uses "soft error" and 8.1 uses "soft-error". + return erlangConstantName(cls) + + def genLookupException(c,v,cls): + mCls = messageConstantClass(cls) + if mCls == 'SOFT_ERROR': genLookupException1(c,'false') + elif mCls == 'HARD_ERROR': genLookupException1(c, 'true') + elif mCls == '': pass + else: raise Exception('Unknown constant class' + cls) + + def genLookupException1(c,hardErrorBoolStr): + n = erlangConstantName(c) + print 'lookup_amqp_exception(%s) -> {%s, ?%s, <<"%s">>};' % \ + (n.lower(), hardErrorBoolStr, n, n) + + def genAmqpException(c,v,cls): + n = erlangConstantName(c) + print 'amqp_exception(?%s) -> %s;' % \ + (n, n.lower()) + + methods = spec.allMethods() + + printFileHeader() + module = "rabbit_framing_amqp_%d_%d" % (spec.major, spec.minor) + if spec.revision != 0: + module = "%s_%d" % (module, spec.revision) + if module == "rabbit_framing_amqp_8_0": + module = "rabbit_framing_amqp_0_8" + print "-module(%s)." % module + print """-include("rabbit_framing.hrl"). + +-export([version/0]). +-export([lookup_method_name/1]). +-export([lookup_class_name/1]). + +-export([method_id/1]). +-export([method_has_content/1]). +-export([is_method_synchronous/1]). +-export([method_record/1]). +-export([method_fieldnames/1]). +-export([decode_method_fields/2]). +-export([decode_properties/2]). +-export([encode_method_fields/1]). +-export([encode_properties/1]). +-export([lookup_amqp_exception/1]). +-export([amqp_exception/1]). + +""" + print "%% Various types" + print "-ifdef(use_specs)." + + print """-export_type([amqp_field_type/0, amqp_property_type/0, + amqp_table/0, amqp_array/0, amqp_value/0, + amqp_method_name/0, amqp_method/0, amqp_method_record/0, + amqp_method_field_name/0, amqp_property_record/0, + amqp_exception/0, amqp_exception_code/0, amqp_class_id/0]). + +-type(amqp_field_type() :: + 'longstr' | 'signedint' | 'decimal' | 'timestamp' | + 'table' | 'byte' | 'double' | 'float' | 'long' | + 'short' | 'bool' | 'binary' | 'void' | 'array'). +-type(amqp_property_type() :: + 'shortstr' | 'longstr' | 'octet' | 'short' | 'long' | + 'longlong' | 'timestamp' | 'bit' | 'table'). + +-type(amqp_table() :: [{binary(), amqp_field_type(), amqp_value()}]). +-type(amqp_array() :: [{amqp_field_type(), amqp_value()}]). +-type(amqp_value() :: binary() | % longstr + integer() | % signedint + {non_neg_integer(), non_neg_integer()} | % decimal + amqp_table() | + amqp_array() | + byte() | % byte + float() | % double + integer() | % long + integer() | % short + boolean() | % bool + binary() | % binary + 'undefined' | % void + non_neg_integer() % timestamp + ). +""" + + print prettyType("amqp_method_name()", + [m.erlangName() for m in methods]) + print prettyType("amqp_method()", + ["{%s, %s}" % (m.klass.index, m.index) for m in methods], + 6) + print prettyType("amqp_method_record()", + ["#%s{}" % (m.erlangName()) for m in methods]) + fieldNames = set() + for m in methods: + fieldNames.update(m.arguments) + fieldNames = [erlangize(f.name) for f in fieldNames] + print prettyType("amqp_method_field_name()", + fieldNames) + print prettyType("amqp_property_record()", + ["#'P_%s'{}" % erlangize(c.name) for c in spec.allClasses()]) + print prettyType("amqp_exception()", + ["'%s'" % erlangConstantName(c).lower() for (c, v, cls) in spec.constants]) + print prettyType("amqp_exception_code()", + ["%i" % v for (c, v, cls) in spec.constants]) + classIds = set() + for m in spec.allMethods(): + classIds.add(m.klass.index) + print prettyType("amqp_class_id()", + ["%i" % ci for ci in classIds]) + print prettyType("amqp_class_name()", + ["%s" % c.erlangName() for c in spec.allClasses()]) + print "-endif. % use_specs" + + print """ +%% Method signatures +-ifdef(use_specs). +-spec(version/0 :: () -> {non_neg_integer(), non_neg_integer(), non_neg_integer()}). +-spec(lookup_method_name/1 :: (amqp_method()) -> amqp_method_name()). +-spec(lookup_class_name/1 :: (amqp_class_id()) -> amqp_class_name()). +-spec(method_id/1 :: (amqp_method_name()) -> amqp_method()). +-spec(method_has_content/1 :: (amqp_method_name()) -> boolean()). +-spec(is_method_synchronous/1 :: (amqp_method_record()) -> boolean()). +-spec(method_record/1 :: (amqp_method_name()) -> amqp_method_record()). +-spec(method_fieldnames/1 :: (amqp_method_name()) -> [amqp_method_field_name()]). +-spec(decode_method_fields/2 :: + (amqp_method_name(), binary()) -> amqp_method_record() | rabbit_types:connection_exit()). +-spec(decode_properties/2 :: (non_neg_integer(), binary()) -> amqp_property_record()). +-spec(encode_method_fields/1 :: (amqp_method_record()) -> binary()). +-spec(encode_properties/1 :: (amqp_property_record()) -> binary()). +-spec(lookup_amqp_exception/1 :: (amqp_exception()) -> {boolean(), amqp_exception_code(), binary()}). +-spec(amqp_exception/1 :: (amqp_exception_code()) -> amqp_exception()). +-endif. % use_specs + +bitvalue(true) -> 1; +bitvalue(false) -> 0; +bitvalue(undefined) -> 0. + +shortstr_size(S) -> + case size(S) of + Len when Len =< 255 -> Len; + _ -> exit(method_field_shortstr_overflow) + end. + +-define(SHORTSTR_VAL(R, L, V, X), + begin + <> = R, + {V, X} + end). + +-define(LONGSTR_VAL(R, L, V, X), + begin + <> = R, + {V, X} + end). + +-define(SHORT_VAL(R, L, V, X), + begin + <> = R, + {V, X} + end). + +-define(LONG_VAL(R, L, V, X), + begin + <> = R, + {V, X} + end). + +-define(LONGLONG_VAL(R, L, V, X), + begin + <> = R, + {V, X} + end). + +-define(OCTET_VAL(R, L, V, X), + begin + <> = R, + {V, X} + end). + +-define(TABLE_VAL(R, L, V, X), + begin + <> = R, + {rabbit_binary_parser:parse_table(V), X} + end). + +-define(TIMESTAMP_VAL(R, L, V, X), + begin + <> = R, + {V, X} + end). + +-define(SHORTSTR_PROP(X, L), + begin + L = size(X), + if L < 256 -> <>; + true -> exit(content_properties_shortstr_overflow) + end + end). + +-define(LONGSTR_PROP(X, L), + begin + L = size(X), + <> + end). + +-define(OCTET_PROP(X, L), <>). +-define(SHORT_PROP(X, L), <>). +-define(LONG_PROP(X, L), <>). +-define(LONGLONG_PROP(X, L), <>). +-define(TIMESTAMP_PROP(X, L), <>). + +-define(TABLE_PROP(X, T), + begin + T = rabbit_binary_generator:generate_table(X), + <<(size(T)):32, T/binary>> + end). +""" + version = "{%d, %d, %d}" % (spec.major, spec.minor, spec.revision) + if version == '{8, 0, 0}': version = '{0, 8, 0}' + print "version() -> %s." % (version) + + for m in methods: genLookupMethodName(m) + print "lookup_method_name({_ClassId, _MethodId} = Id) -> exit({unknown_method_id, Id})." + + for c in spec.allClasses(): genLookupClassName(c) + print "lookup_class_name(ClassId) -> exit({unknown_class_id, ClassId})." + + for m in methods: genMethodId(m) + print "method_id(Name) -> exit({unknown_method_name, Name})." + + for m in methods: genMethodHasContent(m) + print "method_has_content(Name) -> exit({unknown_method_name, Name})." + + for m in methods: genMethodIsSynchronous(m) + print "is_method_synchronous(Name) -> exit({unknown_method_name, Name})." + + for m in methods: genMethodRecord(m) + print "method_record(Name) -> exit({unknown_method_name, Name})." + + for m in methods: genMethodFieldNames(m) + print "method_fieldnames(Name) -> exit({unknown_method_name, Name})." + + for m in methods: genDecodeMethodFields(m) + print "decode_method_fields(Name, BinaryFields) ->" + print " rabbit_misc:frame_error(Name, BinaryFields)." + + for c in spec.allClasses(): genDecodeProperties(c) + print "decode_properties(ClassId, _BinaryFields) -> exit({unknown_class_id, ClassId})." + + for m in methods: genEncodeMethodFields(m) + print "encode_method_fields(Record) -> exit({unknown_method_name, element(1, Record)})." + + for c in spec.allClasses(): genEncodeProperties(c) + print "encode_properties(Record) -> exit({unknown_properties_record, Record})." + + for (c,v,cls) in spec.constants: genLookupException(c,v,cls) + print "lookup_amqp_exception(Code) ->" + print " rabbit_log:warning(\"Unknown AMQP error code '~p'~n\", [Code])," + print " {true, ?INTERNAL_ERROR, <<\"INTERNAL_ERROR\">>}." + + for(c,v,cls) in spec.constants: genAmqpException(c,v,cls) + print "amqp_exception(_Code) -> undefined." + +def genHrl(spec): + def fieldNameList(fields): + return ', '.join([erlangize(f.name) for f in fields]) + + def fieldNameListDefaults(fields): + def fillField(field): + result = erlangize(f.name) + if field.defaultvalue != None: + conv_fn = erlangDefaultValueTypeConvMap[type(field.defaultvalue)] + result += ' = ' + conv_fn(field.defaultvalue) + return result + return ', '.join([fillField(f) for f in fields]) + + methods = spec.allMethods() + + printFileHeader() + print "-define(PROTOCOL_PORT, %d)." % (spec.port) + + for (c,v,cls) in spec.constants: + print "-define(%s, %s)." % (erlangConstantName(c), v) + + print "%% Method field records." + for m in methods: + print "-record(%s, {%s})." % (m.erlangName(), fieldNameListDefaults(m.arguments)) + + print "%% Class property records." + for c in spec.allClasses(): + print "-record('P_%s', {%s})." % (erlangize(c.name), fieldNameList(c.fields)) + + +def generateErl(specPath): + genErl(AmqpSpec(specPath)) + +def generateHrl(specPath): + genHrl(AmqpSpec(specPath)) + +if __name__ == "__main__": + do_main_dict({"header": generateHrl, + "body": generateErl}) + diff --git a/rabbitmq-server/codegen/CONTRIBUTING.md b/rabbitmq-server/codegen/CONTRIBUTING.md new file mode 100644 index 0000000..69a4b4a --- /dev/null +++ b/rabbitmq-server/codegen/CONTRIBUTING.md @@ -0,0 +1,51 @@ +## Overview + +RabbitMQ projects use pull requests to discuss, collaborate on and accept code contributions. +Pull requests is the primary place of discussing code changes. + +## How to Contribute + +The process is fairly standard: + + * Fork the repository or repositories you plan on contributing to + * Clone [RabbitMQ umbrella repository](https://github.com/rabbitmq/rabbitmq-public-umbrella) + * `cd umbrella`, `make co` + * Create a branch with a descriptive name in the relevant repositories + * Make your changes, run tests, commit with a [descriptive message](http://tbaggery.com/2008/04/19/a-note-about-git-commit-messages.html), push to your fork + * Submit pull requests with an explanation what has been changed and **why** + * Submit a filled out and signed [Contributor Agreement](https://github.com/rabbitmq/ca#how-to-submit) if needed (see below) + * Be patient. We will get to your pull request eventually + +If what you are going to work on is a substantial change, please first ask the core team +of their opinion on [RabbitMQ mailing list](https://groups.google.com/forum/#!forum/rabbitmq-users). + + +## (Brief) Code of Conduct + +In one line: don't be a dick. + +Be respectful to the maintainers and other contributors. Open source +contributors put long hours into developing projects and doing user +support. Those projects and user support are available for free. We +believe this deserves some respect. + +Be respectful to people of all races, genders, religious beliefs and +political views. Regardless of how brilliant a pull request is +technically, we will not tolerate disrespectful or aggressive +behaviour. + +Contributors who violate this straightforward Code of Conduct will see +their pull requests closed and locked. + + +## Contributor Agreement + +If you want to contribute a non-trivial change, please submit a signed copy of our +[Contributor Agreement](https://github.com/rabbitmq/ca#how-to-submit) around the time +you submit your pull request. This will make it much easier (in some cases, possible) +for the RabbitMQ team at Pivotal to merge your contribution. + + +## Where to Ask Questions + +If something isn't clear, feel free to ask on our [mailing list](https://groups.google.com/forum/#!forum/rabbitmq-users). diff --git a/rabbitmq-server/codegen/LICENSE b/rabbitmq-server/codegen/LICENSE new file mode 100644 index 0000000..c092f14 --- /dev/null +++ b/rabbitmq-server/codegen/LICENSE @@ -0,0 +1,6 @@ +This package, the RabbitMQ code generation library and associated +files, is licensed under the MPL. For the MPL, please see +LICENSE-MPL-RabbitMQ. + +If you have any questions regarding licensing, please contact us at +info@rabbitmq.com. diff --git a/rabbitmq-server/codegen/LICENSE-MPL-RabbitMQ b/rabbitmq-server/codegen/LICENSE-MPL-RabbitMQ new file mode 100644 index 0000000..c87c1a3 --- /dev/null +++ b/rabbitmq-server/codegen/LICENSE-MPL-RabbitMQ @@ -0,0 +1,455 @@ + MOZILLA PUBLIC LICENSE + Version 1.1 + + --------------- + +1. Definitions. + + 1.0.1. "Commercial Use" means distribution or otherwise making the + Covered Code available to a third party. + + 1.1. "Contributor" means each entity that creates or contributes to + the creation of Modifications. + + 1.2. "Contributor Version" means the combination of the Original + Code, prior Modifications used by a Contributor, and the Modifications + made by that particular Contributor. + + 1.3. "Covered Code" means the Original Code or Modifications or the + combination of the Original Code and Modifications, in each case + including portions thereof. + + 1.4. "Electronic Distribution Mechanism" means a mechanism generally + accepted in the software development community for the electronic + transfer of data. + + 1.5. "Executable" means Covered Code in any form other than Source + Code. + + 1.6. "Initial Developer" means the individual or entity identified + as the Initial Developer in the Source Code notice required by Exhibit + A. + + 1.7. "Larger Work" means a work which combines Covered Code or + portions thereof with code not governed by the terms of this License. + + 1.8. "License" means this document. + + 1.8.1. "Licensable" means having the right to grant, to the maximum + extent possible, whether at the time of the initial grant or + subsequently acquired, any and all of the rights conveyed herein. + + 1.9. "Modifications" means any addition to or deletion from the + substance or structure of either the Original Code or any previous + Modifications. When Covered Code is released as a series of files, a + Modification is: + A. Any addition to or deletion from the contents of a file + containing Original Code or previous Modifications. + + B. Any new file that contains any part of the Original Code or + previous Modifications. + + 1.10. "Original Code" means Source Code of computer software code + which is described in the Source Code notice required by Exhibit A as + Original Code, and which, at the time of its release under this + License is not already Covered Code governed by this License. + + 1.10.1. "Patent Claims" means any patent claim(s), now owned or + hereafter acquired, including without limitation, method, process, + and apparatus claims, in any patent Licensable by grantor. + + 1.11. "Source Code" means the preferred form of the Covered Code for + making modifications to it, including all modules it contains, plus + any associated interface definition files, scripts used to control + compilation and installation of an Executable, or source code + differential comparisons against either the Original Code or another + well known, available Covered Code of the Contributor's choice. The + Source Code can be in a compressed or archival form, provided the + appropriate decompression or de-archiving software is widely available + for no charge. + + 1.12. "You" (or "Your") means an individual or a legal entity + exercising rights under, and complying with all of the terms of, this + License or a future version of this License issued under Section 6.1. + For legal entities, "You" includes any entity which controls, is + controlled by, or is under common control with You. For purposes of + this definition, "control" means (a) the power, direct or indirect, + to cause the direction or management of such entity, whether by + contract or otherwise, or (b) ownership of more than fifty percent + (50%) of the outstanding shares or beneficial ownership of such + entity. + +2. Source Code License. + + 2.1. The Initial Developer Grant. + The Initial Developer hereby grants You a world-wide, royalty-free, + non-exclusive license, subject to third party intellectual property + claims: + (a) under intellectual property rights (other than patent or + trademark) Licensable by Initial Developer to use, reproduce, + modify, display, perform, sublicense and distribute the Original + Code (or portions thereof) with or without Modifications, and/or + as part of a Larger Work; and + + (b) under Patents Claims infringed by the making, using or + selling of Original Code, to make, have made, use, practice, + sell, and offer for sale, and/or otherwise dispose of the + Original Code (or portions thereof). + + (c) the licenses granted in this Section 2.1(a) and (b) are + effective on the date Initial Developer first distributes + Original Code under the terms of this License. + + (d) Notwithstanding Section 2.1(b) above, no patent license is + granted: 1) for code that You delete from the Original Code; 2) + separate from the Original Code; or 3) for infringements caused + by: i) the modification of the Original Code or ii) the + combination of the Original Code with other software or devices. + + 2.2. Contributor Grant. + Subject to third party intellectual property claims, each Contributor + hereby grants You a world-wide, royalty-free, non-exclusive license + + (a) under intellectual property rights (other than patent or + trademark) Licensable by Contributor, to use, reproduce, modify, + display, perform, sublicense and distribute the Modifications + created by such Contributor (or portions thereof) either on an + unmodified basis, with other Modifications, as Covered Code + and/or as part of a Larger Work; and + + (b) under Patent Claims infringed by the making, using, or + selling of Modifications made by that Contributor either alone + and/or in combination with its Contributor Version (or portions + of such combination), to make, use, sell, offer for sale, have + made, and/or otherwise dispose of: 1) Modifications made by that + Contributor (or portions thereof); and 2) the combination of + Modifications made by that Contributor with its Contributor + Version (or portions of such combination). + + (c) the licenses granted in Sections 2.2(a) and 2.2(b) are + effective on the date Contributor first makes Commercial Use of + the Covered Code. + + (d) Notwithstanding Section 2.2(b) above, no patent license is + granted: 1) for any code that Contributor has deleted from the + Contributor Version; 2) separate from the Contributor Version; + 3) for infringements caused by: i) third party modifications of + Contributor Version or ii) the combination of Modifications made + by that Contributor with other software (except as part of the + Contributor Version) or other devices; or 4) under Patent Claims + infringed by Covered Code in the absence of Modifications made by + that Contributor. + +3. Distribution Obligations. + + 3.1. Application of License. + The Modifications which You create or to which You contribute are + governed by the terms of this License, including without limitation + Section 2.2. The Source Code version of Covered Code may be + distributed only under the terms of this License or a future version + of this License released under Section 6.1, and You must include a + copy of this License with every copy of the Source Code You + distribute. You may not offer or impose any terms on any Source Code + version that alters or restricts the applicable version of this + License or the recipients' rights hereunder. However, You may include + an additional document offering the additional rights described in + Section 3.5. + + 3.2. Availability of Source Code. + Any Modification which You create or to which You contribute must be + made available in Source Code form under the terms of this License + either on the same media as an Executable version or via an accepted + Electronic Distribution Mechanism to anyone to whom you made an + Executable version available; and if made available via Electronic + Distribution Mechanism, must remain available for at least twelve (12) + months after the date it initially became available, or at least six + (6) months after a subsequent version of that particular Modification + has been made available to such recipients. You are responsible for + ensuring that the Source Code version remains available even if the + Electronic Distribution Mechanism is maintained by a third party. + + 3.3. Description of Modifications. + You must cause all Covered Code to which You contribute to contain a + file documenting the changes You made to create that Covered Code and + the date of any change. You must include a prominent statement that + the Modification is derived, directly or indirectly, from Original + Code provided by the Initial Developer and including the name of the + Initial Developer in (a) the Source Code, and (b) in any notice in an + Executable version or related documentation in which You describe the + origin or ownership of the Covered Code. + + 3.4. Intellectual Property Matters + (a) Third Party Claims. + If Contributor has knowledge that a license under a third party's + intellectual property rights is required to exercise the rights + granted by such Contributor under Sections 2.1 or 2.2, + Contributor must include a text file with the Source Code + distribution titled "LEGAL" which describes the claim and the + party making the claim in sufficient detail that a recipient will + know whom to contact. If Contributor obtains such knowledge after + the Modification is made available as described in Section 3.2, + Contributor shall promptly modify the LEGAL file in all copies + Contributor makes available thereafter and shall take other steps + (such as notifying appropriate mailing lists or newsgroups) + reasonably calculated to inform those who received the Covered + Code that new knowledge has been obtained. + + (b) Contributor APIs. + If Contributor's Modifications include an application programming + interface and Contributor has knowledge of patent licenses which + are reasonably necessary to implement that API, Contributor must + also include this information in the LEGAL file. + + (c) Representations. + Contributor represents that, except as disclosed pursuant to + Section 3.4(a) above, Contributor believes that Contributor's + Modifications are Contributor's original creation(s) and/or + Contributor has sufficient rights to grant the rights conveyed by + this License. + + 3.5. Required Notices. + You must duplicate the notice in Exhibit A in each file of the Source + Code. If it is not possible to put such notice in a particular Source + Code file due to its structure, then You must include such notice in a + location (such as a relevant directory) where a user would be likely + to look for such a notice. If You created one or more Modification(s) + You may add your name as a Contributor to the notice described in + Exhibit A. You must also duplicate this License in any documentation + for the Source Code where You describe recipients' rights or ownership + rights relating to Covered Code. You may choose to offer, and to + charge a fee for, warranty, support, indemnity or liability + obligations to one or more recipients of Covered Code. However, You + may do so only on Your own behalf, and not on behalf of the Initial + Developer or any Contributor. You must make it absolutely clear than + any such warranty, support, indemnity or liability obligation is + offered by You alone, and You hereby agree to indemnify the Initial + Developer and every Contributor for any liability incurred by the + Initial Developer or such Contributor as a result of warranty, + support, indemnity or liability terms You offer. + + 3.6. Distribution of Executable Versions. + You may distribute Covered Code in Executable form only if the + requirements of Section 3.1-3.5 have been met for that Covered Code, + and if You include a notice stating that the Source Code version of + the Covered Code is available under the terms of this License, + including a description of how and where You have fulfilled the + obligations of Section 3.2. The notice must be conspicuously included + in any notice in an Executable version, related documentation or + collateral in which You describe recipients' rights relating to the + Covered Code. You may distribute the Executable version of Covered + Code or ownership rights under a license of Your choice, which may + contain terms different from this License, provided that You are in + compliance with the terms of this License and that the license for the + Executable version does not attempt to limit or alter the recipient's + rights in the Source Code version from the rights set forth in this + License. If You distribute the Executable version under a different + license You must make it absolutely clear that any terms which differ + from this License are offered by You alone, not by the Initial + Developer or any Contributor. You hereby agree to indemnify the + Initial Developer and every Contributor for any liability incurred by + the Initial Developer or such Contributor as a result of any such + terms You offer. + + 3.7. Larger Works. + You may create a Larger Work by combining Covered Code with other code + not governed by the terms of this License and distribute the Larger + Work as a single product. In such a case, You must make sure the + requirements of this License are fulfilled for the Covered Code. + +4. Inability to Comply Due to Statute or Regulation. + + If it is impossible for You to comply with any of the terms of this + License with respect to some or all of the Covered Code due to + statute, judicial order, or regulation then You must: (a) comply with + the terms of this License to the maximum extent possible; and (b) + describe the limitations and the code they affect. Such description + must be included in the LEGAL file described in Section 3.4 and must + be included with all distributions of the Source Code. Except to the + extent prohibited by statute or regulation, such description must be + sufficiently detailed for a recipient of ordinary skill to be able to + understand it. + +5. Application of this License. + + This License applies to code to which the Initial Developer has + attached the notice in Exhibit A and to related Covered Code. + +6. Versions of the License. + + 6.1. New Versions. + Netscape Communications Corporation ("Netscape") may publish revised + and/or new versions of the License from time to time. Each version + will be given a distinguishing version number. + + 6.2. Effect of New Versions. + Once Covered Code has been published under a particular version of the + License, You may always continue to use it under the terms of that + version. You may also choose to use such Covered Code under the terms + of any subsequent version of the License published by Netscape. No one + other than Netscape has the right to modify the terms applicable to + Covered Code created under this License. + + 6.3. Derivative Works. + If You create or use a modified version of this License (which you may + only do in order to apply it to code which is not already Covered Code + governed by this License), You must (a) rename Your license so that + the phrases "Mozilla", "MOZILLAPL", "MOZPL", "Netscape", + "MPL", "NPL" or any confusingly similar phrase do not appear in your + license (except to note that your license differs from this License) + and (b) otherwise make it clear that Your version of the license + contains terms which differ from the Mozilla Public License and + Netscape Public License. (Filling in the name of the Initial + Developer, Original Code or Contributor in the notice described in + Exhibit A shall not of themselves be deemed to be modifications of + this License.) + +7. DISCLAIMER OF WARRANTY. + + COVERED CODE IS PROVIDED UNDER THIS LICENSE ON AN "AS IS" BASIS, + WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, + WITHOUT LIMITATION, WARRANTIES THAT THE COVERED CODE IS FREE OF + DEFECTS, MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE OR NON-INFRINGING. + THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE COVERED CODE + IS WITH YOU. SHOULD ANY COVERED CODE PROVE DEFECTIVE IN ANY RESPECT, + YOU (NOT THE INITIAL DEVELOPER OR ANY OTHER CONTRIBUTOR) ASSUME THE + COST OF ANY NECESSARY SERVICING, REPAIR OR CORRECTION. THIS DISCLAIMER + OF WARRANTY CONSTITUTES AN ESSENTIAL PART OF THIS LICENSE. NO USE OF + ANY COVERED CODE IS AUTHORIZED HEREUNDER EXCEPT UNDER THIS DISCLAIMER. + +8. TERMINATION. + + 8.1. This License and the rights granted hereunder will terminate + automatically if You fail to comply with terms herein and fail to cure + such breach within 30 days of becoming aware of the breach. All + sublicenses to the Covered Code which are properly granted shall + survive any termination of this License. Provisions which, by their + nature, must remain in effect beyond the termination of this License + shall survive. + + 8.2. If You initiate litigation by asserting a patent infringement + claim (excluding declatory judgment actions) against Initial Developer + or a Contributor (the Initial Developer or Contributor against whom + You file such action is referred to as "Participant") alleging that: + + (a) such Participant's Contributor Version directly or indirectly + infringes any patent, then any and all rights granted by such + Participant to You under Sections 2.1 and/or 2.2 of this License + shall, upon 60 days notice from Participant terminate prospectively, + unless if within 60 days after receipt of notice You either: (i) + agree in writing to pay Participant a mutually agreeable reasonable + royalty for Your past and future use of Modifications made by such + Participant, or (ii) withdraw Your litigation claim with respect to + the Contributor Version against such Participant. If within 60 days + of notice, a reasonable royalty and payment arrangement are not + mutually agreed upon in writing by the parties or the litigation claim + is not withdrawn, the rights granted by Participant to You under + Sections 2.1 and/or 2.2 automatically terminate at the expiration of + the 60 day notice period specified above. + + (b) any software, hardware, or device, other than such Participant's + Contributor Version, directly or indirectly infringes any patent, then + any rights granted to You by such Participant under Sections 2.1(b) + and 2.2(b) are revoked effective as of the date You first made, used, + sold, distributed, or had made, Modifications made by that + Participant. + + 8.3. If You assert a patent infringement claim against Participant + alleging that such Participant's Contributor Version directly or + indirectly infringes any patent where such claim is resolved (such as + by license or settlement) prior to the initiation of patent + infringement litigation, then the reasonable value of the licenses + granted by such Participant under Sections 2.1 or 2.2 shall be taken + into account in determining the amount or value of any payment or + license. + + 8.4. In the event of termination under Sections 8.1 or 8.2 above, + all end user license agreements (excluding distributors and resellers) + which have been validly granted by You or any distributor hereunder + prior to termination shall survive termination. + +9. LIMITATION OF LIABILITY. + + UNDER NO CIRCUMSTANCES AND UNDER NO LEGAL THEORY, WHETHER TORT + (INCLUDING NEGLIGENCE), CONTRACT, OR OTHERWISE, SHALL YOU, THE INITIAL + DEVELOPER, ANY OTHER CONTRIBUTOR, OR ANY DISTRIBUTOR OF COVERED CODE, + OR ANY SUPPLIER OF ANY OF SUCH PARTIES, BE LIABLE TO ANY PERSON FOR + ANY INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES OF ANY + CHARACTER INCLUDING, WITHOUT LIMITATION, DAMAGES FOR LOSS OF GOODWILL, + WORK STOPPAGE, COMPUTER FAILURE OR MALFUNCTION, OR ANY AND ALL OTHER + COMMERCIAL DAMAGES OR LOSSES, EVEN IF SUCH PARTY SHALL HAVE BEEN + INFORMED OF THE POSSIBILITY OF SUCH DAMAGES. THIS LIMITATION OF + LIABILITY SHALL NOT APPLY TO LIABILITY FOR DEATH OR PERSONAL INJURY + RESULTING FROM SUCH PARTY'S NEGLIGENCE TO THE EXTENT APPLICABLE LAW + PROHIBITS SUCH LIMITATION. SOME JURISDICTIONS DO NOT ALLOW THE + EXCLUSION OR LIMITATION OF INCIDENTAL OR CONSEQUENTIAL DAMAGES, SO + THIS EXCLUSION AND LIMITATION MAY NOT APPLY TO YOU. + +10. U.S. GOVERNMENT END USERS. + + The Covered Code is a "commercial item," as that term is defined in + 48 C.F.R. 2.101 (Oct. 1995), consisting of "commercial computer + software" and "commercial computer software documentation," as such + terms are used in 48 C.F.R. 12.212 (Sept. 1995). Consistent with 48 + C.F.R. 12.212 and 48 C.F.R. 227.7202-1 through 227.7202-4 (June 1995), + all U.S. Government End Users acquire Covered Code with only those + rights set forth herein. + +11. MISCELLANEOUS. + + This License represents the complete agreement concerning subject + matter hereof. If any provision of this License is held to be + unenforceable, such provision shall be reformed only to the extent + necessary to make it enforceable. This License shall be governed by + California law provisions (except to the extent applicable law, if + any, provides otherwise), excluding its conflict-of-law provisions. + With respect to disputes in which at least one party is a citizen of, + or an entity chartered or registered to do business in the United + States of America, any litigation relating to this License shall be + subject to the jurisdiction of the Federal Courts of the Northern + District of California, with venue lying in Santa Clara County, + California, with the losing party responsible for costs, including + without limitation, court costs and reasonable attorneys' fees and + expenses. The application of the United Nations Convention on + Contracts for the International Sale of Goods is expressly excluded. + Any law or regulation which provides that the language of a contract + shall be construed against the drafter shall not apply to this + License. + +12. RESPONSIBILITY FOR CLAIMS. + + As between Initial Developer and the Contributors, each party is + responsible for claims and damages arising, directly or indirectly, + out of its utilization of rights under this License and You agree to + work with Initial Developer and Contributors to distribute such + responsibility on an equitable basis. Nothing herein is intended or + shall be deemed to constitute any admission of liability. + +13. MULTIPLE-LICENSED CODE. + + Initial Developer may designate portions of the Covered Code as + "Multiple-Licensed". "Multiple-Licensed" means that the Initial + Developer permits you to utilize portions of the Covered Code under + Your choice of the NPL or the alternative licenses, if any, specified + by the Initial Developer in the file described in Exhibit A. + +EXHIBIT A -Mozilla Public License. + + ``The contents of this file are subject to the Mozilla Public License + Version 1.1 (the "License"); you may not use this file except in + compliance with the License. You may obtain a copy of the License at + http://www.mozilla.org/MPL/ + + Software distributed under the License is distributed on an "AS IS" + basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the + License for the specific language governing rights and limitations + under the License. + + The Original Code is RabbitMQ. + + The Initial Developer of the Original Code is GoPivotal, Inc. + Copyright (c) 2007-2014 GoPivotal, Inc. All rights reserved.'' + + [NOTE: The text of this Exhibit A may differ slightly from the text of + the notices in the Source Code files of the Original Code. You should + use the text of this Exhibit A rather than the text found in the + Original Code Source Code for Your Modifications.] diff --git a/rabbitmq-server/codegen/Makefile b/rabbitmq-server/codegen/Makefile new file mode 100644 index 0000000..8e028c8 --- /dev/null +++ b/rabbitmq-server/codegen/Makefile @@ -0,0 +1,8 @@ +all: + @echo "Please select a target from the Makefile." + +clean: + rm -f *.pyc + +distclean: clean + find . -regex '.*\(~\|#\|\.swp\)' -exec rm {} \; diff --git a/rabbitmq-server/codegen/README.extensions.md b/rabbitmq-server/codegen/README.extensions.md new file mode 100644 index 0000000..74b24fc --- /dev/null +++ b/rabbitmq-server/codegen/README.extensions.md @@ -0,0 +1,189 @@ +# Protocol extensions + +The `amqp_codegen.py` AMQP specification compiler has recently been +enhanced to take more than a single specification file, which allows +AMQP library authors to include extensions to the core protocol +without needing to modify the core AMQP specification file as +distributed. + +The compiler is invoked with the path to a single "main" specification +document and zero or more paths to "extension" documents. + +The order of the extensions matters: any later class property +definitions, for instance, are added to the list of definitions in +order of appearance. In general, composition of extensions with a core +specification document is therefore non-commutative. + +## The main document + +Written in the style of a +[json-shapes](http://github.com/tonyg/json-shapes) schema: + + DomainDefinition = _and(array_of(string()), array_length_equals(2)); + + ConstantDefinition = { + "name": string(), + "value": number(), + "class": optional(_or("soft-error", "hard-error")) + }; + + FieldDefinition = { + "name": string(), + "type": string(), + "default-value": optional(anything()) + }; + + MethodDefinition = { + "name": string(), + "id": number(), + "arguments": array_of(FieldDefinition), + "synchronous": optional(boolean()), + "content": optional(boolean()) + }; + + ClassDefinition = { + "name": string(), + "id": number(), + "methods": array_of(MethodDefinition), + "properties": optional(array_of(FieldDefinition)) + }; + + MainDocument = { + "major-version": number(), + "minor-version": number(), + "revision": optional(number()), + "port": number(), + "domains": array_of(DomainDefinition), + "constants": array_of(ConstantDefinition), + "classes": array_of(ClassDefinition), + } + +Within a `FieldDefinition`, the keyword `domain` can be used instead +of `type`, but `type` is preferred and `domain` is deprecated. + +Type names can either be a defined `domain` name or a built-in name +from the following list: + + - octet + - shortstr + - longstr + - short + - long + - longlong + - bit + - table + - timestamp + +Method and class IDs must be integers between 0 and 65535, +inclusive. Note that there is no specific subset of the space reserved +for experimental or site-local extensions, so be careful not to +conflict with IDs used by the AMQP core specification. + +If the `synchronous` field of a `MethodDefinition` is missing, it is +assumed to be `false`; the same applies to the `content` field. + +A `ConstantDefinition` with a `class` attribute is considered to be an +error-code definition; otherwise, it is considered to be a +straightforward numeric constant. + +## Extensions + +Written in the style of a +[json-shapes](http://github.com/tonyg/json-shapes) schema, and +referencing some of the type definitions given above: + + ExtensionDocument = { + "extension": anything(), + "domains": array_of(DomainDefinition), + "constants": array_of(ConstantDefinition), + "classes": array_of(ClassDefinition) + }; + +The `extension` keyword is used to describe the extension informally +for human readers. Typically it will be a dictionary, with members +such as: + + { + "name": "The name of the extension", + "version": "1.0", + "copyright": "Copyright (C) 1234 Yoyodyne, Inc." + } + +## Merge behaviour + +In the case of conflicts between values specified in the main document +and in any extension documents, type-specific merge operators are +invoked. + + - Any doubly-defined domain names are regarded as true + conflicts. Otherwise, all the domain definitions from all the main + and extension documents supplied to the compiler are merged into a + single dictionary. + + - Constant definitions are treated as per domain names above, + *mutatis mutandis*. + + - Classes and their methods are a little trickier: if an extension + defines a class with the same name as one previously defined, then + only the `methods` and `properties` fields of the extension's class + definition are attended to. + + - Any doubly-defined method names or property names within a class + are treated as true conflicts. + + - Properties defined in an extension are added to the end of the + extant property list for the class. + + (Extensions are of course permitted to define brand new classes as + well as to extend existing ones.) + + - Any other kind of conflict leads to a raised + `AmqpSpecFileMergeConflict` exception. + +## Invoking the spec compiler + +Your code generation code should invoke `amqp_codegen.do_main_dict` +with a dictionary of functions as the sole argument. Each will be +used for generationg a separate file. The `do_main_dict` function +will parse the command-line arguments supplied when python was +invoked. + +The command-line will be parsed as: + + python your_codegen.py [ ...] + +where `` is a key into the dictionary supplied to +`do_main_dict` and is used to select which generation function is +called. The `` and `` arguments are file names of +specification documents containing expressions in the syntax given +above. The *final* argument on the command line, ``, is the +name of the source-code file to generate. + +Here's a tiny example of the layout of a code generation module that +uses `amqp_codegen`: + + import amqp_codegen + + def generateHeader(specPath): + spec = amqp_codegen.AmqpSpec(specPath) + ... + + def generateImpl(specPath): + spec = amqp_codegen.AmqpSpec(specPath) + ... + + if __name__ == "__main__": + amqp_codegen.do_main_dict({"header": generateHeader, + "body": generateImpl}) + +The reasons for allowing more than one action, are that + + - many languages have separate "header"-type files (C and Erlang, to + name two) + - `Makefile`s often require separate rules for generating the two + kinds of file, but it's convenient to keep the generation code + together in a single python module + +The main reason things are laid out this way, however, is simply that +it's an accident of the history of the code. We may change the API to +`amqp_codegen` in future to clean things up a little. diff --git a/rabbitmq-server/codegen/amqp-rabbitmq-0.8.json b/rabbitmq-server/codegen/amqp-rabbitmq-0.8.json new file mode 100644 index 0000000..35f8856 --- /dev/null +++ b/rabbitmq-server/codegen/amqp-rabbitmq-0.8.json @@ -0,0 +1,659 @@ +{ + "name": "AMQP", + "major-version": 8, + "minor-version": 0, + "port": 5672, + "copyright": [ + "Copyright (C) 2008-2013 GoPivotal, Inc.\n", + "\n", + "Permission is hereby granted, free of charge, to any person\n", + "obtaining a copy of this file (the \"Software\"), to deal in the\n", + "Software without restriction, including without limitation the \n", + "rights to use, copy, modify, merge, publish, distribute, \n", + "sublicense, and/or sell copies of the Software, and to permit \n", + "persons to whom the Software is furnished to do so, subject to \n", + "the following conditions:\n", + "\n", + "The above copyright notice and this permission notice shall be\n", + "included in all copies or substantial portions of the Software.\n", + "\n", + "THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND,\n", + "EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES\n", + "OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND\n", + "NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT\n", + "HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,\n", + "WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING\n", + "FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR\n", + "OTHER DEALINGS IN THE SOFTWARE.\n", + "\n", + "Class information entered from amqp_xml0-8.pdf and domain types from amqp-xml-doc0-9.pdf\n", + "\n", + "b3cb053f15e7b98808c0ccc67f23cb3e amqp_xml0-8.pdf\n", + "http://www.twiststandards.org/index.php?option=com_docman&task=cat_view&gid=28&&Itemid=90\n", + "8444db91e2949dbecfb2585e9eef6d64 amqp-xml-doc0-9.pdf\n", + "https://jira.amqp.org/confluence/download/attachments/720900/amqp-xml-doc0-9.pdf?version=1\n"], + + "domains": [ + ["access-ticket", "short"], + ["bit", "bit"], + ["channel-id", "longstr"], + ["class-id", "short"], + ["consumer-tag", "shortstr"], + ["delivery-tag", "longlong"], + ["destination", "shortstr"], + ["duration", "longlong"], + ["exchange-name", "shortstr"], + ["known-hosts", "shortstr"], + ["long", "long"], + ["longlong", "longlong"], + ["longstr", "longstr"], + ["method-id", "short"], + ["no-ack", "bit"], + ["no-local", "bit"], + ["octet", "octet"], + ["offset", "longlong"], + ["path", "shortstr"], + ["peer-properties", "table"], + ["queue-name", "shortstr"], + ["redelivered", "bit"], + ["reference", "longstr"], + ["reject-code", "short"], + ["reject-text", "shortstr"], + ["reply-code", "short"], + ["reply-text", "shortstr"], + ["security-token", "longstr"], + ["short", "short"], + ["shortstr", "shortstr"], + ["table", "table"], + ["timestamp", "timestamp"] + ], + + "constants": [ + {"name": "FRAME-METHOD", "value": 1}, + {"name": "FRAME-HEADER", "value": 2}, + {"name": "FRAME-BODY", "value": 3}, + {"name": "FRAME-OOB-METHOD", "value": 4}, + {"name": "FRAME-OOB-HEADER", "value": 5}, + {"name": "FRAME-OOB-BODY", "value": 6}, + {"name": "FRAME-TRACE", "value": 7}, + {"name": "FRAME-HEARTBEAT", "value": 8}, + {"name": "FRAME-MIN-SIZE", "value": 4096}, + {"name": "FRAME-END", "value": 206}, + {"name": "REPLY-SUCCESS", "value": 200}, + {"name": "NOT-DELIVERED", "value": 310, "class": "soft-error"}, + {"name": "CONTENT-TOO-LARGE", "value": 311, "class": "soft-error"}, + {"name": "NO-ROUTE", "value": 312, "class": "soft-error"}, + {"name": "NO-CONSUMERS", "value": 313, "class": "soft-error"}, + {"name": "ACCESS-REFUSED", "value": 403, "class": "soft-error"}, + {"name": "NOT-FOUND", "value": 404, "class": "soft-error"}, + {"name": "RESOURCE-LOCKED", "value": 405, "class": "soft-error"}, + {"name": "PRECONDITION-FAILED", "value": 406, "class": "soft-error"}, + {"name": "CONNECTION-FORCED", "value": 320, "class": "hard-error"}, + {"name": "INVALID-PATH", "value": 402, "class": "hard-error"}, + {"name": "FRAME-ERROR", "value": 501, "class": "hard-error"}, + {"name": "SYNTAX-ERROR", "value": 502, "class": "hard-error"}, + {"name": "COMMAND-INVALID", "value": 503, "class": "hard-error"}, + {"name": "CHANNEL-ERROR", "value": 504, "class": "hard-error"}, + {"name": "UNEXPECTED-FRAME", "value": 505, "class": "hard-error"}, + {"name": "RESOURCE-ERROR", "value": 506, "class": "hard-error"}, + {"name": "NOT-ALLOWED", "value": 530, "class": "hard-error"}, + {"name": "NOT-IMPLEMENTED", "value": 540, "class": "hard-error"}, + {"name": "INTERNAL-ERROR", "value": 541, "class": "hard-error"} + ], + + "classes": [ + { + "id": 10, + "methods": [{"id": 10, + "arguments": [{"type": "octet", "name": "version-major", "default-value": 0}, + {"type": "octet", "name": "version-minor", "default-value": 8}, + {"domain": "peer-properties", "name": "server-properties"}, + {"type": "longstr", "name": "mechanisms", "default-value": "PLAIN"}, + {"type": "longstr", "name": "locales", "default-value": "en_US"}], + "name": "start", + "synchronous" : true}, + {"id": 11, + "arguments": [{"domain": "peer-properties", "name": "client-properties"}, + {"type": "shortstr", "name": "mechanism", "default-value": "PLAIN"}, + {"type": "longstr", "name": "response"}, + {"type": "shortstr", "name": "locale", "default-value": "en_US"}], + "name": "start-ok"}, + {"id": 20, + "arguments": [{"type": "longstr", "name": "challenge"}], + "name": "secure", + "synchronous" : true}, + {"id": 21, + "arguments": [{"type": "longstr", "name": "response"}], + "name": "secure-ok"}, + {"id": 30, + "arguments": [{"type": "short", "name": "channel-max", "default-value": 0}, + {"type": "long", "name": "frame-max", "default-value": 0}, + {"type": "short", "name": "heartbeat", "default-value": 0}], + "name": "tune", + "synchronous" : true}, + {"id": 31, + "arguments": [{"type": "short", "name": "channel-max", "default-value": 0}, + {"type": "long", "name": "frame-max", "default-value": 0}, + {"type": "short", "name": "heartbeat", "default-value": 0}], + "name": "tune-ok"}, + {"id": 40, + "arguments": [{"type": "shortstr", "name": "virtual-host", "default-value": "/"}, + {"type": "shortstr", "name": "capabilities", "default-value": ""}, + {"type": "bit", "name": "insist", "default-value": false}], + "name": "open", + "synchronous" : true}, + {"id": 41, + "arguments": [{"type": "shortstr", "name": "known-hosts", "default-value": ""}], + "name": "open-ok"}, + {"id": 50, + "arguments": [{"type": "shortstr", "name": "host"}, + {"type": "shortstr", "name": "known-hosts", "default-value": ""}], + "name": "redirect"}, + {"id": 60, + "arguments": [{"type": "short", "name": "reply-code"}, + {"type": "shortstr", "name": "reply-text", "default-value": ""}, + {"type": "short", "name": "class-id"}, + {"type": "short", "name": "method-id"}], + "name": "close", + "synchronous" : true}, + {"id": 61, + "arguments": [], + "name": "close-ok"}], + "name": "connection", + "properties": [] + }, + { + "id": 20, + "methods": [{"id": 10, + "arguments": [{"type": "shortstr", "name": "out-of-band", "default-value": ""}], + "name": "open", + "synchronous" : true}, + {"id": 11, + "arguments": [], + "name": "open-ok"}, + {"id": 20, + "arguments": [{"type": "bit", "name": "active"}], + "name": "flow", + "synchronous" : true}, + {"id": 21, + "arguments": [{"type": "bit", "name": "active"}], + "name": "flow-ok"}, + {"id": 30, + "arguments": [{"type": "short", "name": "reply-code"}, + {"type": "shortstr", "name": "reply-text", "default-value": ""}, + {"type": "table", "name": "details", "default-value": {}}], + "name": "alert"}, + {"id": 40, + "arguments": [{"type": "short", "name": "reply-code"}, + {"type": "shortstr", "name": "reply-text", "default-value": ""}, + {"type": "short", "name": "class-id"}, + {"type": "short", "name": "method-id"}], + "name": "close", + "synchronous" : true}, + {"id": 41, + "arguments": [], + "name": "close-ok"}], + "name": "channel" + }, + { + "id": 30, + "methods": [{"id": 10, + "arguments": [{"type": "shortstr", "name": "realm", "default-value": "/data"}, + {"type": "bit", "name": "exclusive", "default-value": false}, + {"type": "bit", "name": "passive", "default-value": true}, + {"type": "bit", "name": "active", "default-value": true}, + {"type": "bit", "name": "write", "default-value": true}, + {"type": "bit", "name": "read", "default-value": true}], + "name": "request", + "synchronous" : true}, + {"id": 11, + "arguments": [{"type": "short", "name": "ticket", "default-value": 1}], + "name": "request-ok"}], + "name": "access" + }, + { + "id": 40, + "methods": [{"id": 10, + "arguments": [{"type": "short", "name": "ticket", "default-value": 1}, + {"type": "shortstr", "name": "exchange"}, + {"type": "shortstr", "name": "type", "default-value": "direct"}, + {"type": "bit", "name": "passive", "default-value": false}, + {"type": "bit", "name": "durable", "default-value": false}, + {"type": "bit", "name": "auto-delete", "default-value": false}, + {"type": "bit", "name": "internal", "default-value": false}, + {"type": "bit", "name": "nowait", "default-value": false}, + {"type": "table", "name": "arguments", "default-value": {}}], + "name": "declare", + "synchronous" : true}, + {"id": 11, + "arguments": [], + "name": "declare-ok"}, + {"id": 20, + "arguments": [{"type": "short", "name": "ticket", "default-value": 1}, + {"type": "shortstr", "name": "exchange"}, + {"type": "bit", "name": "if-unused", "default-value": false}, + {"type": "bit", "name": "nowait", "default-value": false}], + "name": "delete", + "synchronous" : true}, + {"id": 21, + "arguments": [], + "name": "delete-ok"}], + "name": "exchange" + }, + { + "id": 50, + "methods": [{"id": 10, + "arguments": [{"type": "short", "name": "ticket", "default-value": 1}, + {"type": "shortstr", "name": "queue", "default-value": ""}, + {"type": "bit", "name": "passive", "default-value": false}, + {"type": "bit", "name": "durable", "default-value": false}, + {"type": "bit", "name": "exclusive", "default-value": false}, + {"type": "bit", "name": "auto-delete", "default-value": false}, + {"type": "bit", "name": "nowait", "default-value": false}, + {"type": "table", "name": "arguments", "default-value": {}}], + "name": "declare", + "synchronous" : true}, + {"id": 11, + "arguments": [{"type": "shortstr", "name": "queue"}, + {"type": "long", "name": "message-count"}, + {"type": "long", "name": "consumer-count"}], + "name": "declare-ok"}, + {"id": 20, + "arguments": [{"type": "short", "name": "ticket", "default-value": 1}, + {"type": "shortstr", "name": "queue", "default-value": ""}, + {"type": "shortstr", "name": "exchange"}, + {"type": "shortstr", "name": "routing-key", "default-value": ""}, + {"type": "bit", "name": "nowait", "default-value": false}, + {"type": "table", "name": "arguments", "default-value": {}}], + "name": "bind", + "synchronous" : true}, + {"id": 21, + "arguments": [], + "name": "bind-ok"}, + {"id": 30, + "arguments": [{"type": "short", "name": "ticket", "default-value": 1}, + {"type": "shortstr", "name": "queue", "default-value": ""}, + {"type": "bit", "name": "nowait", "default-value": false}], + "name": "purge", + "synchronous" : true}, + {"id": 31, + "arguments": [{"type": "long", "name": "message-count"}], + "name": "purge-ok"}, + {"id": 40, + "arguments": [{"type": "short", "name": "ticket", "default-value": 1}, + {"type": "shortstr", "name": "queue", "default-value": ""}, + {"type": "bit", "name": "if-unused", "default-value": false}, + {"type": "bit", "name": "if-empty", "default-value": false}, + {"type": "bit", "name": "nowait", "default-value": false}], + "name": "delete", + "synchronous" : true}, + {"id": 41, + "arguments": [{"type": "long", "name": "message-count"}], + "name": "delete-ok"}, + {"id": 50, + "arguments": [{"type": "short", "name": "ticket", "default-value": 1}, + {"type": "shortstr", "name": "queue", "default-value": ""}, + {"type": "shortstr", "name": "exchange"}, + {"type": "shortstr", "name": "routing-key", "default-value": ""}, + {"type": "table", "name": "arguments", "default-value": {}}], + "name": "unbind", + "synchronous" : true}, + {"id": 51, + "arguments": [], + "name": "unbind-ok"} + ], + "name": "queue" + }, + { + "id": 60, + "methods": [{"id": 10, + "arguments": [{"type": "long", "name": "prefetch-size", "default-value": 0}, + {"type": "short", "name": "prefetch-count", "default-value": 0}, + {"type": "bit", "name": "global", "default-value": false}], + "name": "qos", + "synchronous" : true}, + {"id": 11, + "arguments": [], + "name": "qos-ok"}, + {"id": 20, + "arguments": [{"domain": "access-ticket", "name": "ticket", "default-value": 1}, + {"type": "shortstr", "name": "queue", "default-value": ""}, + {"type": "shortstr", "name": "consumer-tag", "default-value": ""}, + {"type": "bit", "name": "no-local", "default-value": false}, + {"type": "bit", "name": "no-ack", "default-value": false}, + {"type": "bit", "name": "exclusive", "default-value": false}, + {"type": "bit", "name": "nowait", "default-value": false}], + "name": "consume", + "synchronous" : true}, + {"id": 21, + "arguments": [{"type": "shortstr", "name": "consumer-tag"}], + "name": "consume-ok"}, + {"id": 30, + "arguments": [{"type": "shortstr", "name": "consumer-tag"}, + {"type": "bit", "name": "nowait", "default-value": false}], + "name": "cancel", + "synchronous" : true}, + {"id": 31, + "arguments": [{"type": "shortstr", "name": "consumer-tag"}], + "name": "cancel-ok"}, + {"content": true, + "id": 40, + "arguments": [{"type": "short", "name": "ticket", "default-value": 1}, + {"type": "shortstr", "name": "exchange", "default-value": ""}, + {"type": "shortstr", "name": "routing-key", "default-value": ""}, + {"type": "bit", "name": "mandatory", "default-value": false}, + {"type": "bit", "name": "immediate", "default-value": false}], + "name": "publish"}, + {"content": true, + "id": 50, + "arguments": [{"type": "short", "name": "reply-code"}, + {"type": "shortstr", "name": "reply-text", "default-value": ""}, + {"type": "shortstr", "name": "exchange"}, + {"type": "shortstr", "name": "routing-key"}], + "name": "return"}, + {"content": true, + "id": 60, + "arguments": [{"type": "shortstr", "name": "consumer-tag"}, + {"type": "longlong", "name": "delivery-tag"}, + {"type": "bit", "name": "redelivered", "default-value": false}, + {"type": "shortstr", "name": "exchange"}, + {"type": "shortstr", "name": "routing-key"}], + "name": "deliver"}, + {"id": 70, + "arguments": [{"type": "short", "name": "ticket", "default-value": 1}, + {"type": "shortstr", "name": "queue", "default-value": ""}, + {"type": "bit", "name": "no-ack", "default-value": false}], + "name": "get", + "synchronous" : true}, + {"content": true, + "id": 71, + "arguments": [{"type": "longlong", "name": "delivery-tag"}, + {"type": "bit", "name": "redelivered", "default-value": false}, + {"type": "shortstr", "name": "exchange"}, + {"type": "shortstr", "name": "routing-key"}, + {"type": "long", "name": "message-count"}], + "name": "get-ok"}, + {"id": 72, + "arguments": [{"type": "shortstr", "name": "cluster-id", "default-value": ""}], + "name": "get-empty"}, + {"id": 80, + "arguments": [{"type": "longlong", "name": "delivery-tag", "default-value": 0}, + {"type": "bit", "name": "multiple", "default-value": false}], + "name": "ack"}, + {"id": 90, + "arguments": [{"type": "longlong", "name": "delivery-tag"}, + {"type": "bit", "name": "requeue", "default-value": true}], + "name": "reject"}, + {"id": 100, + "arguments": [{"type": "bit", "name": "requeue", "default-value": false}], + "name": "recover-async"}, + {"id": 110, + "arguments": [{"type": "bit", "name": "requeue", "default-value": false}], + "name": "recover", + "synchronous" : true}, + {"id": 111, + "arguments": [], + "name": "recover-ok"}], + "name": "basic", + "properties": [{"type": "shortstr", "name": "content-type"}, + {"type": "shortstr", "name": "content-encoding"}, + {"type": "table", "name": "headers"}, + {"type": "octet", "name": "delivery-mode"}, + {"type": "octet", "name": "priority"}, + {"type": "shortstr", "name": "correlation-id"}, + {"type": "shortstr", "name": "reply-to"}, + {"type": "shortstr", "name": "expiration"}, + {"type": "shortstr", "name": "message-id"}, + {"type": "timestamp", "name": "timestamp"}, + {"type": "shortstr", "name": "type"}, + {"type": "shortstr", "name": "user-id"}, + {"type": "shortstr", "name": "app-id"}, + {"type": "shortstr", "name": "cluster-id"}] + }, + { + "id": 70, + "methods": [{"id": 10, + "arguments": [{"type": "long", "name": "prefetch-size", "default-value": 0}, + {"type": "short", "name": "prefetch-count", "default-value": 0}, + {"type": "bit", "name": "global", "default-value": false}], + "name": "qos", + "synchronous" : true}, + {"id": 11, + "arguments": [], + "name": "qos-ok"}, + {"id": 20, + "arguments": [{"type": "short", "name": "ticket", "default-value": 1}, + {"type": "shortstr", "name": "queue", "default-value": ""}, + {"type": "shortstr", "name": "consumer-tag", "default-value": ""}, + {"type": "bit", "name": "no-local", "default-value": false}, + {"type": "bit", "name": "no-ack", "default-value": false}, + {"type": "bit", "name": "exclusive", "default-value": false}, + {"type": "bit", "name": "nowait", "default-value": false}], + "name": "consume", + "synchronous" : true}, + {"id": 21, + "arguments": [{"type": "shortstr", "name": "consumer-tag"}], + "name": "consume-ok"}, + {"id": 30, + "arguments": [{"type": "shortstr", "name": "consumer-tag"}, + {"type": "bit", "name": "nowait", "default-value": false}], + "name": "cancel", + "synchronous" : true}, + {"id": 31, + "arguments": [{"type": "shortstr", "name": "consumer-tag"}], + "name": "cancel-ok"}, + {"id": 40, + "arguments": [{"type": "shortstr", "name": "identifier"}, + {"type": "longlong", "name": "content-size"}], + "name": "open", + "synchronous" : true}, + {"id": 41, + "arguments": [{"type": "longlong", "name": "staged-size"}], + "name": "open-ok"}, + {"content": true, + "id": 50, + "arguments": [], + "name": "stage"}, + {"id": 60, + "arguments": [{"type": "short", "name": "ticket", "default-value": 1}, + {"type": "shortstr", "name": "exchange", "default-value": ""}, + {"type": "shortstr", "name": "routing-key", "default-value": ""}, + {"type": "bit", "name": "mandatory", "default-value": false}, + {"type": "bit", "name": "immediate", "default-value": false}, + {"type": "shortstr", "name": "identifier"}], + "name": "publish"}, + {"content": true, + "id": 70, + "arguments": [{"type": "short", "name": "reply-code", "default-value": 200}, + {"type": "shortstr", "name": "reply-text", "default-value": ""}, + {"type": "shortstr", "name": "exchange"}, + {"type": "shortstr", "name": "routing-key"}], + "name": "return"}, + {"id": 80, + "arguments": [{"type": "shortstr", "name": "consumer-tag"}, + {"type": "longlong", "name": "delivery-tag"}, + {"type": "bit", "name": "redelivered", "default-value": false}, + {"type": "shortstr", "name": "exchange"}, + {"type": "shortstr", "name": "routing-key"}, + {"type": "shortstr", "name": "identifier"}], + "name": "deliver"}, + {"id": 90, + "arguments": [{"type": "longlong", "name": "delivery-tag", "default-value": 0}, + {"type": "bit", "name": "multiple", "default-value": false}], + "name": "ack"}, + {"id": 100, + "arguments": [{"type": "longlong", "name": "delivery-tag"}, + {"type": "bit", "name": "requeue", "default-value": true}], + "name": "reject"}], + "name": "file", + "properties": [{"type": "shortstr", "name": "content-type"}, + {"type": "shortstr", "name": "content-encoding"}, + {"type": "table", "name": "headers"}, + {"type": "octet", "name": "priority"}, + {"type": "shortstr", "name": "reply-to"}, + {"type": "shortstr", "name": "message-id"}, + {"type": "shortstr", "name": "filename"}, + {"type": "timestamp", "name": "timestamp"}, + {"type": "shortstr", "name": "cluster-id"}] + }, + { + "id": 80, + "methods": [{"id": 10, + "arguments": [{"type": "long", "name": "prefetch-size", "default-value": 0}, + {"type": "short", "name": "prefetch-count", "default-value": 0}, + {"type": "long", "name": "consume-rate", "default-value": 0}, + {"type": "bit", "name": "global", "default-value": false}], + "name": "qos", + "synchronous" : true}, + {"id": 11, + "arguments": [], + "name": "qos-ok"}, + {"id": 20, + "arguments": [{"type": "short", "name": "ticket", "default-value": 1}, + {"type": "shortstr", "name": "queue", "default-value": ""}, + {"type": "shortstr", "name": "consumer-tag", "default-value": ""}, + {"type": "bit", "name": "no-local", "default-value": false}, + {"type": "bit", "name": "exclusive", "default-value": false}, + {"type": "bit", "name": "nowait", "default-value": false}], + "name": "consume", + "synchronous" : true}, + {"id": 21, + "arguments": [{"type": "shortstr", "name": "consumer-tag"}], + "name": "consume-ok"}, + {"id": 30, + "arguments": [{"type": "shortstr", "name": "consumer-tag"}, + {"type": "bit", "name": "nowait", "default-value": false}], + "name": "cancel", + "synchronous" : true}, + {"id": 31, + "arguments": [{"type": "shortstr", "name": "consumer-tag"}], + "name": "cancel-ok"}, + {"content": true, + "id": 40, + "arguments": [{"type": "short", "name": "ticket", "default-value": 1}, + {"type": "shortstr", "name": "exchange", "default-value": ""}, + {"type": "shortstr", "name": "routing-key", "default-value": ""}, + {"type": "bit", "name": "mandatory", "default-value": false}, + {"type": "bit", "name": "immediate", "default-value": false}], + "name": "publish"}, + {"content": true, + "id": 50, + "arguments": [{"type": "short", "name": "reply-code", "default-value": 200}, + {"type": "shortstr", "name": "reply-text", "default-value": ""}, + {"type": "shortstr", "name": "exchange"}, + {"type": "shortstr", "name": "routing-key"}], + "name": "return"}, + {"content": true, + "id": 60, + "arguments": [{"type": "shortstr", "name": "consumer-tag"}, + {"type": "longlong", "name": "delivery-tag"}, + {"type": "shortstr", "name": "exchange"}, + {"type": "shortstr", "name": "queue"}], + "name": "deliver"}], + "name": "stream", + "properties": [{"type": "shortstr", "name": "content-type"}, + {"type": "shortstr", "name": "content-encoding"}, + {"type": "table", "name": "headers"}, + {"type": "octet", "name": "priority"}, + {"type": "timestamp", "name": "timestamp"}] + }, + { + "id": 90, + "methods": [{"id": 10, + "arguments": [], + "name": "select", + "synchronous" : true}, + {"id": 11, + "arguments": [], + "name": "select-ok"}, + {"id": 20, + "arguments": [], + "name": "commit", + "synchronous" : true}, + {"id": 21, + "arguments": [], + "name": "commit-ok"}, + {"id": 30, + "arguments": [], + "name": "rollback", + "synchronous" : true}, + {"id": 31, + "arguments": [], + "name": "rollback-ok"}], + "name": "tx" + }, + { + "id": 100, + "methods": [{"id": 10, + "arguments": [], + "name": "select", + "synchronous" : true}, + {"id": 11, + "arguments": [], + "name": "select-ok"}, + {"id": 20, + "arguments": [{"type": "shortstr", "name": "dtx-identifier"}], + "name": "start", + "synchronous" : true}, + {"id": 21, + "arguments": [], "name": "start-ok"}], + "name": "dtx" + }, + { + "id": 110, + "methods": [{"content": true, + "id": 10, + "arguments": [{"type": "table", "name": "meta-data"}], + "name": "request"}], + "name": "tunnel", + "properties": [{"type": "table", "name": "headers"}, + {"type": "shortstr", "name": "proxy-name"}, + {"type": "shortstr", "name": "data-name"}, + {"type": "octet", "name": "durable"}, + {"type": "octet", "name": "broadcast"}] + }, + { + "id": 120, + "methods": [{"id": 10, + "arguments": [{"type": "octet", "name": "integer-1"}, + {"type": "short", "name": "integer-2"}, + {"type": "long", "name": "integer-3"}, + {"type": "longlong", "name": "integer-4"}, + {"type": "octet", "name": "operation"}], + "name": "integer", + "synchronous" : true}, + {"id": 11, + "arguments": [{"type": "longlong", "name": "result"}], + "name": "integer-ok"}, + {"id": 20, + "arguments": [{"type": "shortstr", "name": "string-1"}, + {"type": "longstr", "name": "string-2"}, + {"type": "octet", "name": "operation"}], + "name": "string", + "synchronous" : true}, + {"id": 21, + "arguments": [{"type": "longstr", "name": "result"}], + "name": "string-ok"}, + {"id": 30, + "arguments": [{"type": "table", "name": "table"}, + {"type": "octet", "name": "integer-op"}, + {"type": "octet", "name": "string-op"}], + "name": "table", + "synchronous" : true}, + {"id": 31, + "arguments": [{"type": "longlong", "name": "integer-result"}, + {"type": "longstr", "name": "string-result"}], + "name": "table-ok"}, + {"content": true, + "id": 40, + "arguments": [], + "name": "content", + "synchronous" : true}, + {"content": true, + "id": 41, + "arguments": [{"type": "long", "name": "content-checksum"}], + "name": "content-ok"}], + "name": "test" + } + ] +} diff --git a/rabbitmq-server/codegen/amqp-rabbitmq-0.9.1.json b/rabbitmq-server/codegen/amqp-rabbitmq-0.9.1.json new file mode 100644 index 0000000..0c3ee2a --- /dev/null +++ b/rabbitmq-server/codegen/amqp-rabbitmq-0.9.1.json @@ -0,0 +1,473 @@ +{ + "name": "AMQP", + "major-version": 0, + "minor-version": 9, + "revision": 1, + "port": 5672, + "copyright": [ + "Copyright (C) 2008-2013 GoPivotal, Inc.\n", + "\n", + "Permission is hereby granted, free of charge, to any person\n", + "obtaining a copy of this file (the \"Software\"), to deal in the\n", + "Software without restriction, including without limitation the \n", + "rights to use, copy, modify, merge, publish, distribute, \n", + "sublicense, and/or sell copies of the Software, and to permit \n", + "persons to whom the Software is furnished to do so, subject to \n", + "the following conditions:\n", + "\n", + "The above copyright notice and this permission notice shall be\n", + "included in all copies or substantial portions of the Software.\n", + "\n", + "THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND,\n", + "EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES\n", + "OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND\n", + "NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT\n", + "HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,\n", + "WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING\n", + "FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR\n", + "OTHER DEALINGS IN THE SOFTWARE.\n", + "\n", + "Class information entered from amqp_xml0-8.pdf and domain types from amqp-xml-doc0-9.pdf\n", + "Updated for 0-9-1 by Tony Garnock-Jones\n", + "\n", + "b3cb053f15e7b98808c0ccc67f23cb3e amqp_xml0-8.pdf\n", + "http://www.twiststandards.org/index.php?option=com_docman&task=cat_view&gid=28&&Itemid=90\n", + "8444db91e2949dbecfb2585e9eef6d64 amqp-xml-doc0-9.pdf\n", + "https://jira.amqp.org/confluence/download/attachments/720900/amqp-xml-doc0-9.pdf?version=1\n"], + + "domains": [ + ["bit", "bit"], + ["channel-id", "longstr"], + ["class-id", "short"], + ["consumer-tag", "shortstr"], + ["delivery-tag", "longlong"], + ["destination", "shortstr"], + ["duration", "longlong"], + ["exchange-name", "shortstr"], + ["long", "long"], + ["longlong", "longlong"], + ["longstr", "longstr"], + ["method-id", "short"], + ["no-ack", "bit"], + ["no-local", "bit"], + ["octet", "octet"], + ["offset", "longlong"], + ["path", "shortstr"], + ["peer-properties", "table"], + ["queue-name", "shortstr"], + ["redelivered", "bit"], + ["reference", "longstr"], + ["reject-code", "short"], + ["reject-text", "shortstr"], + ["reply-code", "short"], + ["reply-text", "shortstr"], + ["security-token", "longstr"], + ["short", "short"], + ["shortstr", "shortstr"], + ["table", "table"], + ["timestamp", "timestamp"] + ], + + "constants": [ + {"name": "FRAME-METHOD", "value": 1}, + {"name": "FRAME-HEADER", "value": 2}, + {"name": "FRAME-BODY", "value": 3}, + {"name": "FRAME-HEARTBEAT", "value": 8}, + {"name": "FRAME-MIN-SIZE", "value": 4096}, + {"name": "FRAME-END", "value": 206}, + {"name": "REPLY-SUCCESS", "value": 200}, + {"name": "CONTENT-TOO-LARGE", "value": 311, "class": "soft-error"}, + {"name": "NO-ROUTE", "value": 312, "class": "soft-error"}, + {"name": "NO-CONSUMERS", "value": 313, "class": "soft-error"}, + {"name": "ACCESS-REFUSED", "value": 403, "class": "soft-error"}, + {"name": "NOT-FOUND", "value": 404, "class": "soft-error"}, + {"name": "RESOURCE-LOCKED", "value": 405, "class": "soft-error"}, + {"name": "PRECONDITION-FAILED", "value": 406, "class": "soft-error"}, + {"name": "CONNECTION-FORCED", "value": 320, "class": "hard-error"}, + {"name": "INVALID-PATH", "value": 402, "class": "hard-error"}, + {"name": "FRAME-ERROR", "value": 501, "class": "hard-error"}, + {"name": "SYNTAX-ERROR", "value": 502, "class": "hard-error"}, + {"name": "COMMAND-INVALID", "value": 503, "class": "hard-error"}, + {"name": "CHANNEL-ERROR", "value": 504, "class": "hard-error"}, + {"name": "UNEXPECTED-FRAME", "value": 505, "class": "hard-error"}, + {"name": "RESOURCE-ERROR", "value": 506, "class": "hard-error"}, + {"name": "NOT-ALLOWED", "value": 530, "class": "hard-error"}, + {"name": "NOT-IMPLEMENTED", "value": 540, "class": "hard-error"}, + {"name": "INTERNAL-ERROR", "value": 541, "class": "hard-error"} + ], + + "classes": [ + { + "id": 10, + "methods": [{"id": 10, + "arguments": [{"type": "octet", "name": "version-major", "default-value": 0}, + {"type": "octet", "name": "version-minor", "default-value": 9}, + {"domain": "peer-properties", "name": "server-properties"}, + {"type": "longstr", "name": "mechanisms", "default-value": "PLAIN"}, + {"type": "longstr", "name": "locales", "default-value": "en_US"}], + "name": "start", + "synchronous" : true}, + {"id": 11, + "arguments": [{"domain": "peer-properties", "name": "client-properties"}, + {"type": "shortstr", "name": "mechanism", "default-value": "PLAIN"}, + {"type": "longstr", "name": "response"}, + {"type": "shortstr", "name": "locale", "default-value": "en_US"}], + "name": "start-ok"}, + {"id": 20, + "arguments": [{"type": "longstr", "name": "challenge"}], + "name": "secure", + "synchronous" : true}, + {"id": 21, + "arguments": [{"type": "longstr", "name": "response"}], + "name": "secure-ok"}, + {"id": 30, + "arguments": [{"type": "short", "name": "channel-max", "default-value": 0}, + {"type": "long", "name": "frame-max", "default-value": 0}, + {"type": "short", "name": "heartbeat", "default-value": 0}], + "name": "tune", + "synchronous" : true}, + {"id": 31, + "arguments": [{"type": "short", "name": "channel-max", "default-value": 0}, + {"type": "long", "name": "frame-max", "default-value": 0}, + {"type": "short", "name": "heartbeat", "default-value": 0}], + "name": "tune-ok"}, + {"id": 40, + "arguments": [{"type": "shortstr", "name": "virtual-host", "default-value": "/"}, + {"type": "shortstr", "name": "capabilities", "default-value": ""}, + {"type": "bit", "name": "insist", "default-value": false}], + "name": "open", + "synchronous" : true}, + {"id": 41, + "arguments": [{"type": "shortstr", "name": "known-hosts", "default-value": ""}], + "name": "open-ok"}, + {"id": 50, + "arguments": [{"type": "short", "name": "reply-code"}, + {"type": "shortstr", "name": "reply-text", "default-value": ""}, + {"type": "short", "name": "class-id"}, + {"type": "short", "name": "method-id"}], + "name": "close", + "synchronous" : true}, + {"id": 51, + "arguments": [], + "name": "close-ok"}, + {"id": 60, + "arguments": [{"type": "shortstr", "name": "reason", "default-value": ""}], + "name": "blocked"}, + {"id": 61, + "arguments": [], + "name": "unblocked"}], + "name": "connection", + "properties": [] + }, + { + "id": 20, + "methods": [{"id": 10, + "arguments": [{"type": "shortstr", "name": "out-of-band", "default-value": ""}], + "name": "open", + "synchronous" : true}, + {"id": 11, + "arguments": [{"type": "longstr", "name": "channel-id", "default-value": ""}], + "name": "open-ok"}, + {"id": 20, + "arguments": [{"type": "bit", "name": "active"}], + "name": "flow", + "synchronous" : true}, + {"id": 21, + "arguments": [{"type": "bit", "name": "active"}], + "name": "flow-ok"}, + {"id": 40, + "arguments": [{"type": "short", "name": "reply-code"}, + {"type": "shortstr", "name": "reply-text", "default-value": ""}, + {"type": "short", "name": "class-id"}, + {"type": "short", "name": "method-id"}], + "name": "close", + "synchronous" : true}, + {"id": 41, + "arguments": [], + "name": "close-ok"}], + "name": "channel" + }, + { + "id": 30, + "methods": [{"id": 10, + "arguments": [{"type": "shortstr", "name": "realm", "default-value": "/data"}, + {"type": "bit", "name": "exclusive", "default-value": false}, + {"type": "bit", "name": "passive", "default-value": true}, + {"type": "bit", "name": "active", "default-value": true}, + {"type": "bit", "name": "write", "default-value": true}, + {"type": "bit", "name": "read", "default-value": true}], + "name": "request", + "synchronous" : true}, + {"id": 11, + "arguments": [{"type": "short", "name": "ticket", "default-value": 1}], + "name": "request-ok"}], + "name": "access" + }, + { + "id": 40, + "methods": [{"id": 10, + "arguments": [{"type": "short", "name": "ticket", "default-value": 0}, + {"type": "shortstr", "name": "exchange"}, + {"type": "shortstr", "name": "type", "default-value": "direct"}, + {"type": "bit", "name": "passive", "default-value": false}, + {"type": "bit", "name": "durable", "default-value": false}, + {"type": "bit", "name": "auto-delete", "default-value": false}, + {"type": "bit", "name": "internal", "default-value": false}, + {"type": "bit", "name": "nowait", "default-value": false}, + {"type": "table", "name": "arguments", "default-value": {}}], + "name": "declare", + "synchronous" : true}, + {"id": 11, + "arguments": [], + "name": "declare-ok"}, + {"id": 20, + "arguments": [{"type": "short", "name": "ticket", "default-value": 0}, + {"type": "shortstr", "name": "exchange"}, + {"type": "bit", "name": "if-unused", "default-value": false}, + {"type": "bit", "name": "nowait", "default-value": false}], + "name": "delete", + "synchronous" : true}, + {"id": 21, + "arguments": [], + "name": "delete-ok"}, + {"id": 30, + "arguments": [{"type": "short", "name": "ticket", "default-value": 0}, + {"type": "shortstr", "name": "destination"}, + {"type": "shortstr", "name": "source"}, + {"type": "shortstr", "name": "routing-key", "default-value": ""}, + {"type": "bit", "name": "nowait", "default-value": false}, + {"type": "table", "name": "arguments", "default-value": {}}], + "name": "bind", + "synchronous" : true}, + {"id": 31, + "arguments": [], + "name": "bind-ok"}, + {"id": 40, + "arguments": [{"type": "short", "name": "ticket", "default-value": 0}, + {"type": "shortstr", "name": "destination"}, + {"type": "shortstr", "name": "source"}, + {"type": "shortstr", "name": "routing-key", "default-value": ""}, + {"type": "bit", "name": "nowait", "default-value": false}, + {"type": "table", "name": "arguments", "default-value": {}}], + "name": "unbind", + "synchronous" : true}, + {"id": 51, + "arguments": [], + "name": "unbind-ok"}], + "name": "exchange" + }, + { + "id": 50, + "methods": [{"id": 10, + "arguments": [{"type": "short", "name": "ticket", "default-value": 0}, + {"type": "shortstr", "name": "queue", "default-value": ""}, + {"type": "bit", "name": "passive", "default-value": false}, + {"type": "bit", "name": "durable", "default-value": false}, + {"type": "bit", "name": "exclusive", "default-value": false}, + {"type": "bit", "name": "auto-delete", "default-value": false}, + {"type": "bit", "name": "nowait", "default-value": false}, + {"type": "table", "name": "arguments", "default-value": {}}], + "name": "declare", + "synchronous" : true}, + {"id": 11, + "arguments": [{"type": "shortstr", "name": "queue"}, + {"type": "long", "name": "message-count"}, + {"type": "long", "name": "consumer-count"}], + "name": "declare-ok"}, + {"id": 20, + "arguments": [{"type": "short", "name": "ticket", "default-value": 0}, + {"type": "shortstr", "name": "queue", "default-value": ""}, + {"type": "shortstr", "name": "exchange"}, + {"type": "shortstr", "name": "routing-key", "default-value": ""}, + {"type": "bit", "name": "nowait", "default-value": false}, + {"type": "table", "name": "arguments", "default-value": {}}], + "name": "bind", + "synchronous" : true}, + {"id": 21, + "arguments": [], + "name": "bind-ok"}, + {"id": 30, + "arguments": [{"type": "short", "name": "ticket", "default-value": 0}, + {"type": "shortstr", "name": "queue", "default-value": ""}, + {"type": "bit", "name": "nowait", "default-value": false}], + "name": "purge", + "synchronous" : true}, + {"id": 31, + "arguments": [{"type": "long", "name": "message-count"}], + "name": "purge-ok"}, + {"id": 40, + "arguments": [{"type": "short", "name": "ticket", "default-value": 0}, + {"type": "shortstr", "name": "queue", "default-value": ""}, + {"type": "bit", "name": "if-unused", "default-value": false}, + {"type": "bit", "name": "if-empty", "default-value": false}, + {"type": "bit", "name": "nowait", "default-value": false}], + "name": "delete", + "synchronous" : true}, + {"id": 41, + "arguments": [{"type": "long", "name": "message-count"}], + "name": "delete-ok"}, + {"id": 50, + "arguments": [{"type": "short", "name": "ticket", "default-value": 0}, + {"type": "shortstr", "name": "queue", "default-value": ""}, + {"type": "shortstr", "name": "exchange"}, + {"type": "shortstr", "name": "routing-key", "default-value": ""}, + {"type": "table", "name": "arguments", "default-value": {}}], + "name": "unbind", + "synchronous" : true}, + {"id": 51, + "arguments": [], + "name": "unbind-ok"} + ], + "name": "queue" + }, + { + "id": 60, + "methods": [{"id": 10, + "arguments": [{"type": "long", "name": "prefetch-size", "default-value": 0}, + {"type": "short", "name": "prefetch-count", "default-value": 0}, + {"type": "bit", "name": "global", "default-value": false}], + "name": "qos", + "synchronous" : true}, + {"id": 11, + "arguments": [], + "name": "qos-ok"}, + {"id": 20, + "arguments": [{"domain": "short", "name": "ticket", "default-value": 0}, + {"type": "shortstr", "name": "queue", "default-value": ""}, + {"type": "shortstr", "name": "consumer-tag", "default-value": ""}, + {"type": "bit", "name": "no-local", "default-value": false}, + {"type": "bit", "name": "no-ack", "default-value": false}, + {"type": "bit", "name": "exclusive", "default-value": false}, + {"type": "bit", "name": "nowait", "default-value": false}, + {"type": "table", "name": "arguments", "default-value": {}}], + "name": "consume", + "synchronous" : true}, + {"id": 21, + "arguments": [{"type": "shortstr", "name": "consumer-tag"}], + "name": "consume-ok"}, + {"id": 30, + "arguments": [{"type": "shortstr", "name": "consumer-tag"}, + {"type": "bit", "name": "nowait", "default-value": false}], + "name": "cancel", + "synchronous" : true}, + {"id": 31, + "arguments": [{"type": "shortstr", "name": "consumer-tag"}], + "name": "cancel-ok"}, + {"content": true, + "id": 40, + "arguments": [{"type": "short", "name": "ticket", "default-value": 0}, + {"type": "shortstr", "name": "exchange", "default-value": ""}, + {"type": "shortstr", "name": "routing-key", "default-value": ""}, + {"type": "bit", "name": "mandatory", "default-value": false}, + {"type": "bit", "name": "immediate", "default-value": false}], + "name": "publish"}, + {"content": true, + "id": 50, + "arguments": [{"type": "short", "name": "reply-code"}, + {"type": "shortstr", "name": "reply-text", "default-value": ""}, + {"type": "shortstr", "name": "exchange"}, + {"type": "shortstr", "name": "routing-key"}], + "name": "return"}, + {"content": true, + "id": 60, + "arguments": [{"type": "shortstr", "name": "consumer-tag"}, + {"type": "longlong", "name": "delivery-tag"}, + {"type": "bit", "name": "redelivered", "default-value": false}, + {"type": "shortstr", "name": "exchange"}, + {"type": "shortstr", "name": "routing-key"}], + "name": "deliver"}, + {"id": 70, + "arguments": [{"type": "short", "name": "ticket", "default-value": 0}, + {"type": "shortstr", "name": "queue", "default-value": ""}, + {"type": "bit", "name": "no-ack", "default-value": false}], + "name": "get", + "synchronous" : true}, + {"content": true, + "id": 71, + "arguments": [{"type": "longlong", "name": "delivery-tag"}, + {"type": "bit", "name": "redelivered", "default-value": false}, + {"type": "shortstr", "name": "exchange"}, + {"type": "shortstr", "name": "routing-key"}, + {"type": "long", "name": "message-count"}], + "name": "get-ok"}, + {"id": 72, + "arguments": [{"type": "shortstr", "name": "cluster-id", "default-value": ""}], + "name": "get-empty"}, + {"id": 80, + "arguments": [{"type": "longlong", "name": "delivery-tag", "default-value": 0}, + {"type": "bit", "name": "multiple", "default-value": false}], + "name": "ack"}, + {"id": 90, + "arguments": [{"type": "longlong", "name": "delivery-tag"}, + {"type": "bit", "name": "requeue", "default-value": true}], + "name": "reject"}, + {"id": 100, + "arguments": [{"type": "bit", "name": "requeue", "default-value": false}], + "name": "recover-async"}, + {"id": 110, + "arguments": [{"type": "bit", "name": "requeue", "default-value": false}], + "name": "recover", + "synchronous" : true}, + {"id": 111, + "arguments": [], + "name": "recover-ok"}, + {"id": 120, + "arguments": [{"type": "longlong", "name": "delivery-tag", "default-value": 0}, + {"type": "bit", "name": "multiple", "default-value": false}, + {"type": "bit", "name": "requeue", "default-value": true}], + "name": "nack"}], + "name": "basic", + "properties": [{"type": "shortstr", "name": "content-type"}, + {"type": "shortstr", "name": "content-encoding"}, + {"type": "table", "name": "headers"}, + {"type": "octet", "name": "delivery-mode"}, + {"type": "octet", "name": "priority"}, + {"type": "shortstr", "name": "correlation-id"}, + {"type": "shortstr", "name": "reply-to"}, + {"type": "shortstr", "name": "expiration"}, + {"type": "shortstr", "name": "message-id"}, + {"type": "timestamp", "name": "timestamp"}, + {"type": "shortstr", "name": "type"}, + {"type": "shortstr", "name": "user-id"}, + {"type": "shortstr", "name": "app-id"}, + {"type": "shortstr", "name": "cluster-id"}] + }, + { + "id": 90, + "methods": [{"id": 10, + "arguments": [], + "name": "select", + "synchronous" : true}, + {"id": 11, + "arguments": [], + "name": "select-ok"}, + {"id": 20, + "arguments": [], + "name": "commit", + "synchronous" : true}, + {"id": 21, + "arguments": [], + "name": "commit-ok"}, + {"id": 30, + "arguments": [], + "name": "rollback", + "synchronous" : true}, + {"id": 31, + "arguments": [], + "name": "rollback-ok"}], + "name": "tx" + }, + { + "id": 85, + "methods": [{"id": 10, + "arguments": [ + {"type": "bit", "name": "nowait", "default-value": false}], + "name": "select", + "synchronous": true}, + {"id": 11, + "arguments": [], + "name": "select-ok"}], + "name": "confirm" + } + ] +} diff --git a/rabbitmq-server/codegen/amqp_codegen.py b/rabbitmq-server/codegen/amqp_codegen.py new file mode 100644 index 0000000..2623a5d --- /dev/null +++ b/rabbitmq-server/codegen/amqp_codegen.py @@ -0,0 +1,286 @@ +## The contents of this file are subject to the Mozilla Public License +## Version 1.1 (the "License"); you may not use this file except in +## compliance with the License. You may obtain a copy of the License +## at http://www.mozilla.org/MPL/ +## +## Software distributed under the License is distributed on an "AS IS" +## basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See +## the License for the specific language governing rights and +## limitations under the License. +## +## The Original Code is RabbitMQ. +## +## The Initial Developer of the Original Code is GoPivotal, Inc. +## Copyright (c) 2007-2014 GoPivotal, Inc. All rights reserved. +## + +from __future__ import nested_scopes +import re +import sys +import os +from optparse import OptionParser + +try: + try: + import simplejson as json + except ImportError, e: + if sys.hexversion >= 0x20600f0: + import json + else: + raise e +except ImportError: + print >> sys.stderr , " You don't appear to have simplejson.py installed" + print >> sys.stderr , " (an implementation of a JSON reader and writer in Python)." + print >> sys.stderr , " You can install it:" + print >> sys.stderr , " - by running 'apt-get install python-simplejson' on Debian-based systems," + print >> sys.stderr , " - by running 'yum install python-simplejson' on Fedora/Red Hat system," + print >> sys.stderr , " - by running 'port install py25-simplejson' on Macports on OS X" + print >> sys.stderr , " (you may need to say 'make PYTHON=python2.5', as well)," + print >> sys.stderr , " - from sources from 'http://pypi.python.org/pypi/simplejson'" + print >> sys.stderr , " - simplejson is a standard json library in the Python core since 2.6" + sys.exit(1) + +def insert_base_types(d): + for t in ['octet', 'shortstr', 'longstr', 'short', 'long', + 'longlong', 'bit', 'table', 'timestamp']: + d[t] = t + +class AmqpSpecFileMergeConflict(Exception): pass + +# If ignore_conflicts is true, then we allow acc and new to conflict, +# with whatever's already in acc winning and new being ignored. If +# ignore_conflicts is false, acc and new must not conflict. + +def default_spec_value_merger(key, acc, new, ignore_conflicts): + if acc is None or acc == new or ignore_conflicts: + return new + else: + raise AmqpSpecFileMergeConflict(key, acc, new) + +def extension_info_merger(key, acc, new, ignore_conflicts): + return acc + [new] + +def domains_merger(key, acc, new, ignore_conflicts): + merged = dict((k, v) for [k, v] in acc) + for [k, v] in new: + if merged.has_key(k): + if not ignore_conflicts: + raise AmqpSpecFileMergeConflict(key, acc, new) + else: + merged[k] = v + + return [[k, v] for (k, v) in merged.iteritems()] + +def merge_dict_lists_by(dict_key, acc, new, ignore_conflicts): + acc_index = set(v[dict_key] for v in acc) + result = list(acc) # shallow copy + for v in new: + if v[dict_key] in acc_index: + if not ignore_conflicts: + raise AmqpSpecFileMergeConflict(description, acc, new) + else: + result.append(v) + return result + +def constants_merger(key, acc, new, ignore_conflicts): + return merge_dict_lists_by("name", acc, new, ignore_conflicts) + +def methods_merger(classname, acc, new, ignore_conflicts): + return merge_dict_lists_by("name", acc, new, ignore_conflicts) + +def properties_merger(classname, acc, new, ignore_conflicts): + return merge_dict_lists_by("name", acc, new, ignore_conflicts) + +def class_merger(acc, new, ignore_conflicts): + acc["methods"] = methods_merger(acc["name"], + acc["methods"], + new["methods"], + ignore_conflicts) + acc["properties"] = properties_merger(acc["name"], + acc.get("properties", []), + new.get("properties", []), + ignore_conflicts) + +def classes_merger(key, acc, new, ignore_conflicts): + acc_dict = dict((v["name"], v) for v in acc) + result = list(acc) # shallow copy + for w in new: + if w["name"] in acc_dict: + class_merger(acc_dict[w["name"]], w, ignore_conflicts) + else: + result.append(w) + return result + +mergers = { + "extension": (extension_info_merger, []), + "domains": (domains_merger, []), + "constants": (constants_merger, []), + "classes": (classes_merger, []), +} + +def merge_load_specs(filenames, ignore_conflicts): + handles = [open(filename) for filename in filenames] + docs = [json.load(handle) for handle in handles] + spec = {} + for doc in docs: + for (key, value) in doc.iteritems(): + (merger, default_value) = mergers.get(key, (default_spec_value_merger, None)) + spec[key] = merger(key, spec.get(key, default_value), value, ignore_conflicts) + for handle in handles: handle.close() + return spec + +class AmqpSpec: + # Slight wart: use a class member rather than change the ctor signature + # to avoid breaking everyone else's code. + ignore_conflicts = False + + def __init__(self, filenames): + self.spec = merge_load_specs(filenames, AmqpSpec.ignore_conflicts) + + self.major = self.spec['major-version'] + self.minor = self.spec['minor-version'] + self.revision = self.spec.has_key('revision') and self.spec['revision'] or 0 + self.port = self.spec['port'] + + self.domains = {} + insert_base_types(self.domains) + for entry in self.spec['domains']: + self.domains[ entry[0] ] = entry[1] + + self.constants = [] + for d in self.spec['constants']: + if d.has_key('class'): + klass = d['class'] + else: + klass = '' + self.constants.append((d['name'], d['value'], klass)) + + self.classes = [] + for element in self.spec['classes']: + self.classes.append(AmqpClass(self, element)) + + def allClasses(self): + return self.classes + + def allMethods(self): + return [m for c in self.classes for m in c.allMethods()] + + def resolveDomain(self, n): + return self.domains[n] + +class AmqpEntity: + def __init__(self, element): + self.element = element + self.name = element['name'] + +class AmqpClass(AmqpEntity): + def __init__(self, spec, element): + AmqpEntity.__init__(self, element) + self.spec = spec + self.index = int(self.element['id']) + + self.methods = [] + for method_element in self.element['methods']: + self.methods.append(AmqpMethod(self, method_element)) + + self.hasContentProperties = False + for method in self.methods: + if method.hasContent: + self.hasContentProperties = True + break + + self.fields = [] + if self.element.has_key('properties'): + index = 0 + for e in self.element['properties']: + self.fields.append(AmqpField(self, e, index)) + index = index + 1 + + def allMethods(self): + return self.methods + + def __repr__(self): + return 'AmqpClass("' + self.name + '")' + +class AmqpMethod(AmqpEntity): + def __init__(self, klass, element): + AmqpEntity.__init__(self, element) + self.klass = klass + self.index = int(self.element['id']) + if self.element.has_key('synchronous'): + self.isSynchronous = self.element['synchronous'] + else: + self.isSynchronous = False + if self.element.has_key('content'): + self.hasContent = self.element['content'] + else: + self.hasContent = False + self.arguments = [] + + index = 0 + for argument in element['arguments']: + self.arguments.append(AmqpField(self, argument, index)) + index = index + 1 + + def __repr__(self): + return 'AmqpMethod("' + self.klass.name + "." + self.name + '" ' + repr(self.arguments) + ')' + +class AmqpField(AmqpEntity): + def __init__(self, method, element, index): + AmqpEntity.__init__(self, element) + self.method = method + self.index = index + + if self.element.has_key('type'): + self.domain = self.element['type'] + else: + self.domain = self.element['domain'] + + if self.element.has_key('default-value'): + self.defaultvalue = self.element['default-value'] + else: + self.defaultvalue = None + + def __repr__(self): + return 'AmqpField("' + self.name + '")' + +def do_main(header_fn, body_fn): + do_main_dict({"header": header_fn, "body": body_fn}) + +def do_main_dict(funcDict): + def usage(): + print >> sys.stderr , "Usage:" + print >> sys.stderr , " %s ... " % (sys.argv[0]) + print >> sys.stderr , " where is one of %s" % ", ".join([k for k in funcDict.keys()]) + + def execute(fn, amqp_specs, out_file): + stdout = sys.stdout + f = open(out_file, 'w') + success = False + try: + sys.stdout = f + fn(amqp_specs) + success = True + finally: + sys.stdout = stdout + f.close() + if not success: + os.remove(out_file) + + parser = OptionParser() + parser.add_option("--ignore-conflicts", action="store_true", dest="ignore_conflicts", default=False) + (options, args) = parser.parse_args() + + if len(args) < 3: + usage() + sys.exit(1) + else: + function = args[0] + sources = args[1:-1] + dest = args[-1] + AmqpSpec.ignore_conflicts = options.ignore_conflicts + if funcDict.has_key(function): + execute(funcDict[function], sources, dest) + else: + usage() + sys.exit(1) diff --git a/rabbitmq-server/codegen/credit_extension.json b/rabbitmq-server/codegen/credit_extension.json new file mode 100644 index 0000000..b74391f --- /dev/null +++ b/rabbitmq-server/codegen/credit_extension.json @@ -0,0 +1,54 @@ +{ + "extension": { + "name": "credit", + "version": "0.1", + "status": [ + "This extension is used internally by the broker and plugins. ", + "It is NOT intended to be used by regular clients over the ", + "network. This extension is subject to change without notice; ", + "hence you are strongly discouraged from building clients ", + "which use it."], + "copyright": [ + "Copyright (C) 2008-2013 GoPivotal, Inc.\n", + "\n", + "Permission is hereby granted, free of charge, to any person\n", + "obtaining a copy of this file (the \"Software\"), to deal in the\n", + "Software without restriction, including without limitation the \n", + "rights to use, copy, modify, merge, publish, distribute, \n", + "sublicense, and/or sell copies of the Software, and to permit \n", + "persons to whom the Software is furnished to do so, subject to \n", + "the following conditions:\n", + "\n", + "The above copyright notice and this permission notice shall be\n", + "included in all copies or substantial portions of the Software.\n", + "\n", + "THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND,\n", + "EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES\n", + "OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND\n", + "NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT\n", + "HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,\n", + "WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING\n", + "FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR\n", + "OTHER DEALINGS IN THE SOFTWARE.\n"] + }, + + "classes": [ + { + "id": 60, + "methods": [{"id": 200, + "arguments": [{"type": "shortstr", "name": "consumer-tag", "default-value": ""}, + {"type": "long", "name": "credit"}, + {"type": "bit", "name": "drain"}], + "name": "credit", + "synchronous" : true}, + {"id": 201, + "arguments": [{"type": "long", "name": "available"}], + "name": "credit-ok"}, + {"id": 202, + "arguments": [{"type": "shortstr", "name": "consumer-tag", "default-value": ""}, + {"type": "long", "name": "credit-drained"}], + "name": "credit-drained"}], + "name": "basic" + } + ] +} diff --git a/rabbitmq-server/codegen/demo_extension.json b/rabbitmq-server/codegen/demo_extension.json new file mode 100644 index 0000000..7a636f1 --- /dev/null +++ b/rabbitmq-server/codegen/demo_extension.json @@ -0,0 +1,18 @@ +{ + "extension": { + "name": "demo", + "version": "1.0", + "copyright": "Copyright (C) 2009-2013 GoPivotal, Inc." + }, + "domains": [ + ["foo-domain", "shortstr"] + ], + "constants": [ + {"name": "FOO-CONSTANT", "value": 121212} + ], + "classes": [ + {"name": "demo", + "id": 555, + "methods": [{"name": "one", "id": 1, "arguments": []}]} + ] +} diff --git a/rabbitmq-server/codegen/license_info b/rabbitmq-server/codegen/license_info new file mode 100644 index 0000000..1cebe90 --- /dev/null +++ b/rabbitmq-server/codegen/license_info @@ -0,0 +1,4 @@ +The files amqp-rabbitmq-0.8.json and amqp-rabbitmq-0.9.1.json are +"Copyright (C) 2008-2013 GoPivotal", Inc. and are covered by the MIT +license. + diff --git a/rabbitmq-server/docs/examples-to-end.xsl b/rabbitmq-server/docs/examples-to-end.xsl new file mode 100644 index 0000000..4db1d5c --- /dev/null +++ b/rabbitmq-server/docs/examples-to-end.xsl @@ -0,0 +1,93 @@ + + + + + + + + + + + + + + + + + + + + + + Examples + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + [] + + + + {} + + + + + + + + diff --git a/rabbitmq-server/docs/html-to-website-xml.xsl b/rabbitmq-server/docs/html-to-website-xml.xsl new file mode 100644 index 0000000..d83d507 --- /dev/null +++ b/rabbitmq-server/docs/html-to-website-xml.xsl @@ -0,0 +1,90 @@ + + + + + + + + + + + + + + + + + + + + +type="text/xml" href="page.xsl" + + + <xsl:value-of select="document($original)/refentry/refnamediv/refname"/><xsl:if test="document($original)/refentry/refmeta/manvolnum">(<xsl:value-of select="document($original)/refentry/refmeta/manvolnum"/>)</xsl:if> manual page + + + + +

+ This is the manual page for + (). +

+

+ See a list of all manual pages. +

+
+ +

+ This is the documentation for + . +

+
+
+

+ For more general documentation, please see the + administrator's guide. +

+ + + + +
+ + + + + + + + + + + + + + + + + + + + + + +
+    
+  
+
+ + +
+ +
+
+ +
+ diff --git a/rabbitmq-server/docs/rabbitmq-echopid.xml b/rabbitmq-server/docs/rabbitmq-echopid.xml new file mode 100644 index 0000000..d3dcea5 --- /dev/null +++ b/rabbitmq-server/docs/rabbitmq-echopid.xml @@ -0,0 +1,71 @@ + + + + + RabbitMQ Server + + The RabbitMQ Team <info@rabbitmq.com> + + + + + rabbitmq-echopid.bat + RabbitMQ Server + + + + rabbitmq-echopid.bat + return the process id of the Erlang runtime hosting RabbitMQ + + + + + rabbitmq-echopid.bat + sname + + + + + Description + + RabbitMQ is an implementation of AMQP, the emerging + standard for high performance enterprise messaging. The + RabbitMQ server is a robust and scalable implementation of + an AMQP broker. + + + Running rabbitmq-echopid will attempt to + discover and echo the process id (PID) of the Erlang runtime + process (erl.exe) that is hosting RabbitMQ. To allow erl.exe + time to start up and load RabbitMQ, the script will wait for + ten seconds before timing out if a suitable PID cannot be + found. + + + If a PID is discovered, the script will echo it to stdout + before exiting with a ERRORLEVEL of 0. If no PID is + discovered before the timeout, nothing is written to stdout + and the script exits setting ERRORLEVEL to 1. + + + Note that this script only exists on Windows due to the need + to wait for erl.exe and possibly time-out. To obtain the PID + on Unix set RABBITMQ_PID_FILE before starting + rabbitmq-server and do not use "-detached". + + + + + Options + + + sname + + +The short-name form of the RabbitMQ node name. + + + + + + diff --git a/rabbitmq-server/docs/rabbitmq-env.conf.5.xml b/rabbitmq-server/docs/rabbitmq-env.conf.5.xml new file mode 100644 index 0000000..c887596 --- /dev/null +++ b/rabbitmq-server/docs/rabbitmq-env.conf.5.xml @@ -0,0 +1,83 @@ + + + + + RabbitMQ Server + + The RabbitMQ Team <info@rabbitmq.com> + + + + + rabbitmq-env.conf + 5 + RabbitMQ Server + + + + rabbitmq-env.conf + default settings for RabbitMQ AMQP server + + + + Description + +/etc/rabbitmq/rabbitmq-env.conf contains variable settings that override the +defaults built in to the RabbitMQ startup scripts. + + +The file is interpreted by the system shell, and so should consist of +a sequence of shell environment variable definitions. Normal shell +syntax is permitted (since the file is sourced using the shell "." +operator), including line comments starting with "#". + + +In order of preference, the startup scripts get their values from the +environment, from /etc/rabbitmq/rabbitmq-env.conf and finally from the +built-in default values. For example, for the RABBITMQ_NODENAME +setting, + + + RABBITMQ_NODENAME + + +from the environment is checked first. If it is absent or equal to the +empty string, then + + + NODENAME + + +from /etc/rabbitmq/rabbitmq-env.conf is checked. If it is also absent +or set equal to the empty string then the default value from the +startup script is used. + + +The variable names in /etc/rabbitmq/rabbitmq-env.conf are always equal to the +environment variable names, with the RABBITMQ_ prefix removed: +RABBITMQ_NODE_PORT from the environment becomes NODE_PORT in the +/etc/rabbitmq/rabbitmq-env.conf file, etc. + + For example: + +# I am a complete /etc/rabbitmq/rabbitmq-env.conf file. +# Comment lines start with a hash character. +# This is a /bin/sh script file - use ordinary envt var syntax +NODENAME=hare + + + This is an example of a complete + /etc/rabbitmq/rabbitmq-env.conf file that overrides the default Erlang + node name from "rabbit" to "hare". + + + + + + See also + + rabbitmq-server1 + rabbitmqctl1 + + + diff --git a/rabbitmq-server/docs/rabbitmq-plugins.1.xml b/rabbitmq-server/docs/rabbitmq-plugins.1.xml new file mode 100644 index 0000000..f7be2d2 --- /dev/null +++ b/rabbitmq-server/docs/rabbitmq-plugins.1.xml @@ -0,0 +1,245 @@ + + + + + + + RabbitMQ Server + + The RabbitMQ Team <info@rabbitmq.com> + + + + + rabbitmq-plugins + 1 + RabbitMQ Service + + + + rabbitmq-plugins + command line tool for managing RabbitMQ broker plugins + + + + + rabbitmq-plugins + -n node + command + command options + + + + + Description + + rabbitmq-plugins is a command line tool for managing + RabbitMQ broker plugins. It allows one to enable, disable and browse + plugins. It must be run by a user with write permissions to the RabbitMQ + configuration directory. + + + Some plugins depend on others to work + correctly. rabbitmq-plugins traverses these + dependencies and enables all required plugins. Plugins listed on + the rabbitmq-plugins command line are marked as + explicitly enabled; dependent plugins are marked as implicitly + enabled. Implicitly enabled plugins are automatically disabled again + when they are no longer required. + + + + The enable, disable and + set commands will update the plugins file and + then attempt to connect to the broker and ensure it is running + all enabled plugins. By default if it is not possible to connect + to the running broker (for example if it is stopped) then a + warning is displayed. Specify --online or + --offline to change this behaviour. + + + + + Commands + + + + list -v -m -E -e pattern + + + + -v + Show all plugin details (verbose). + + + -m + Show only plugin names (minimal). + + + -E + Show only explicitly enabled + plugins. + + + -e + Show only explicitly or implicitly + enabled plugins. + + + pattern + Pattern to filter the plugin names by. + + + + Lists all plugins, their versions, dependencies and + descriptions. Each plugin is prefixed with two status + indicator characters inside [ ]. The first indicator can + be " " to indicate that the plugin is not enabled, "E" to + indicate that it is explicitly enabled, "e" to indicate + that it is implicitly enabled, or "!" to indicate that it + is enabled but missing and thus not operational. The + second indicator can be " " to show that the plugin is not + running, or "*" to show that it is. + + + If the optional pattern is given, only plugins whose + name matches pattern are shown. + + For example: + rabbitmq-plugins list + + This command lists all plugins, on one line each. + + rabbitmq-plugins list -v + + This command lists all plugins. + + rabbitmq-plugins list -v management + + This command lists all plugins whose name contains "management". + + rabbitmq-plugins list -e rabbit + + This command lists all implicitly or explicitly enabled + RabbitMQ plugins. + + + + + + enable --offline --online plugin ... + + + + --offline + Just modify the enabled plugins file. + + + --online + Treat failure to connect to the running broker as fatal. + + + plugin + One or more plugins to enable. + + + + Enables the specified plugins and all their dependencies. + + + For example: + rabbitmq-plugins enable rabbitmq_shovel rabbitmq_management + + This command enables the shovel and + management plugins and all their + dependencies. + + + + + + disable --offline --online plugin ... + + + + --offline + Just modify the enabled plugins file. + + + --online + Treat failure to connect to the running broker as fatal. + + + plugin + One or more plugins to disable. + + + + Disables the specified plugins and all their dependencies. + + + For example: + rabbitmq-plugins disable amqp_client + + This command disables amqp_client and + all plugins that depend on it. + + + + + + set --offline --online plugin ... + + + + --offline + Just modify the enabled plugins file. + + + --online + Treat failure to connect to the running broker as fatal. + + + plugin + Zero or more plugins to enable. + + + + Enables the specified plugins and all their + dependencies. Unlike rabbitmq-plugins + enable this command ignores and overwrites any + existing enabled plugins. rabbitmq-plugins + set with no plugin arguments is a legal command + meaning "disable all plugins". + + + For example: + rabbitmq-plugins set rabbitmq_management + + This command enables the management + plugin and its dependencies and disables everything else. + + + + + + + + + diff --git a/rabbitmq-server/docs/rabbitmq-server.1.xml b/rabbitmq-server/docs/rabbitmq-server.1.xml new file mode 100644 index 0000000..32ae842 --- /dev/null +++ b/rabbitmq-server/docs/rabbitmq-server.1.xml @@ -0,0 +1,132 @@ + + + + + RabbitMQ Server + + The RabbitMQ Team <info@rabbitmq.com> + + + + + rabbitmq-server + 1 + RabbitMQ Server + + + + rabbitmq-server + start RabbitMQ AMQP server + + + + + rabbitmq-server + -detached + + + + + Description + + RabbitMQ is an implementation of AMQP, the emerging standard for high +performance enterprise messaging. The RabbitMQ server is a robust and +scalable implementation of an AMQP broker. + + +Running rabbitmq-server in the foreground displays a banner message, +and reports on progress in the startup sequence, concluding with the +message "broker running", indicating that the RabbitMQ broker has been +started successfully. To shut down the server, just terminate the +process or use rabbitmqctl(1). + + + + + Environment + + + + RABBITMQ_MNESIA_BASE + + +Defaults to /var/lib/rabbitmq/mnesia. Set this to the directory where +Mnesia database files should be placed. + + + + + + RABBITMQ_LOG_BASE + + +Defaults to /var/log/rabbitmq. Log files generated by the server will +be placed in this directory. + + + + + + RABBITMQ_NODENAME + + +Defaults to rabbit. This can be useful if you want to run more than +one node per machine - RABBITMQ_NODENAME should be unique per +erlang-node-and-machine combination. See the +clustering on a single +machine guide for details. + + + + + + RABBITMQ_NODE_IP_ADDRESS + + +By default RabbitMQ will bind to all interfaces, on IPv4 and IPv6 if +available. Set this if you only want to bind to one network interface +or address family. + + + + + + RABBITMQ_NODE_PORT + + +Defaults to 5672. + + + + + + + + + Options + + + -detached + + + Start the server process in the background. Note that this will + cause the pid not to be written to the pid file. + + For example: + rabbitmq-server -detached + + Runs RabbitMQ AMQP server in the background. + + + + + + + + See also + + rabbitmq-env.conf5 + rabbitmqctl1 + + + diff --git a/rabbitmq-server/docs/rabbitmq-service.xml b/rabbitmq-server/docs/rabbitmq-service.xml new file mode 100644 index 0000000..3368960 --- /dev/null +++ b/rabbitmq-server/docs/rabbitmq-service.xml @@ -0,0 +1,217 @@ + + + + + RabbitMQ Server + + The RabbitMQ Team <info@rabbitmq.com> + + + + + rabbitmq-service.bat + RabbitMQ Server + + + + rabbitmq-service.bat + manage RabbitMQ AMQP service + + + + + rabbitmq-service.bat + command + + + + + Description + + RabbitMQ is an implementation of AMQP, the emerging standard for high +performance enterprise messaging. The RabbitMQ server is a robust and +scalable implementation of an AMQP broker. + + +Running rabbitmq-service allows the RabbitMQ broker to be run as a +service on NT/2000/2003/XP/Vista® environments. The RabbitMQ broker +service can be started and stopped using the Windows® services +applet. + + +By default the service will run in the authentication context of the +local system account. It is therefore necessary to synchronise Erlang +cookies between the local system account (typically +C:\WINDOWS\.erlang.cookie and the account that will be used to +run rabbitmqctl. + + + + + Commands + + + + help + + +Display usage information. + + + + + + install + + +Install the service. The service will not be started. +Subsequent invocations will update the service parameters if +relevant environment variables were modified. + + + + + + remove + + +Remove the service. If the service is running then it will +automatically be stopped before being removed. No files will be +deleted as a consequence and rabbitmq-server will remain operable. + + + + + + start + + +Start the service. The service must have been correctly installed +beforehand. + + + + + + stop + + +Stop the service. The service must be running for this command to +have any effect. + + + + + + disable + + +Disable the service. This is the equivalent of setting the startup +type to Disabled using the service control panel. + + + + + + enable + + +Enable the service. This is the equivalent of setting the startup +type to Automatic using the service control panel. + + + + + + + + Environment + + + + RABBITMQ_SERVICENAME + + +Defaults to RabbitMQ. + + + + + + RABBITMQ_BASE + + +Defaults to the application data directory of the current user. +This is the location of log and database directories. + + + + + + + RABBITMQ_NODENAME + + +Defaults to rabbit. This can be useful if you want to run more than +one node per machine - RABBITMQ_NODENAME should be unique per +erlang-node-and-machine combination. See the +clustering on a single +machine guide for details. + + + + + + RABBITMQ_NODE_IP_ADDRESS + + +By default RabbitMQ will bind to all interfaces, on IPv4 and IPv6 if +available. Set this if you only want to bind to one network interface +or address family. + + + + + + RABBITMQ_NODE_PORT + + +Defaults to 5672. + + + + + + ERLANG_SERVICE_MANAGER_PATH + + +Defaults to C:\Program Files\erl5.5.5\erts-5.5.5\bin +(or C:\Program Files (x86)\erl5.5.5\erts-5.5.5\bin for 64-bit +environments). This is the installation location of the Erlang service +manager. + + + + + + RABBITMQ_CONSOLE_LOG + + +Set this varable to new or reuse to have the console +output from the server redirected to a file named SERVICENAME.debug +in the application data directory of the user that installed the service. +Under Vista this will be C:\Users\AppData\username\SERVICENAME. +Under previous versions of Windows this will be +C:\Documents and Settings\username\Application Data\SERVICENAME. +If RABBITMQ_CONSOLE_LOG is set to new then a new file will be +created each time the service starts. If RABBITMQ_CONSOLE_LOG is +set to reuse then the file will be overwritten each time the +service starts. The default behaviour when RABBITMQ_CONSOLE_LOG is +not set or set to a value other than new or reuse is to discard +the server output. + + + + + + diff --git a/rabbitmq-server/docs/rabbitmq.config.example b/rabbitmq-server/docs/rabbitmq.config.example new file mode 100644 index 0000000..9b3855c --- /dev/null +++ b/rabbitmq-server/docs/rabbitmq.config.example @@ -0,0 +1,590 @@ +%% -*- mode: erlang -*- +%% ---------------------------------------------------------------------------- +%% RabbitMQ Sample Configuration File. +%% +%% See http://www.rabbitmq.com/configure.html for details. +%% ---------------------------------------------------------------------------- +[ + {rabbit, + [%% + %% Network Connectivity + %% ==================== + %% + + %% By default, RabbitMQ will listen on all interfaces, using + %% the standard (reserved) AMQP port. + %% + %% {tcp_listeners, [5672]}, + + %% To listen on a specific interface, provide a tuple of {IpAddress, Port}. + %% For example, to listen only on localhost for both IPv4 and IPv6: + %% + %% {tcp_listeners, [{"127.0.0.1", 5672}, + %% {"::1", 5672}]}, + + %% SSL listeners are configured in the same fashion as TCP listeners, + %% including the option to control the choice of interface. + %% + %% {ssl_listeners, [5671]}, + + %% Maximum time for AMQP 0-8/0-9/0-9-1 handshake (after socket connection + %% and SSL handshake), in milliseconds. + %% + %% {handshake_timeout, 10000}, + + %% Log levels (currently just used for connection logging). + %% One of 'debug', 'info', 'warning', 'error' or 'none', in decreasing + %% order of verbosity. Defaults to 'info'. + %% + %% {log_levels, [{connection, info}, {channel, info}]}, + + %% Set to 'true' to perform reverse DNS lookups when accepting a + %% connection. Hostnames will then be shown instead of IP addresses + %% in rabbitmqctl and the management plugin. + %% + %% {reverse_dns_lookups, true}, + + %% + %% Security / AAA + %% ============== + %% + + %% The default "guest" user is only permitted to access the server + %% via a loopback interface (e.g. localhost). + %% {loopback_users, [<<"guest">>]}, + %% + %% Uncomment the following line if you want to allow access to the + %% guest user from anywhere on the network. + %% {loopback_users, []}, + + %% Configuring SSL. + %% See http://www.rabbitmq.com/ssl.html for full documentation. + %% + %% {ssl_options, [{cacertfile, "/path/to/testca/cacert.pem"}, + %% {certfile, "/path/to/server/cert.pem"}, + %% {keyfile, "/path/to/server/key.pem"}, + %% {verify, verify_peer}, + %% {fail_if_no_peer_cert, false}]}, + + %% Choose the available SASL mechanism(s) to expose. + %% The two default (built in) mechanisms are 'PLAIN' and + %% 'AMQPLAIN'. Additional mechanisms can be added via + %% plugins. + %% + %% See http://www.rabbitmq.com/authentication.html for more details. + %% + %% {auth_mechanisms, ['PLAIN', 'AMQPLAIN']}, + + %% Select an authentication database to use. RabbitMQ comes bundled + %% with a built-in auth-database, based on mnesia. + %% + %% {auth_backends, [rabbit_auth_backend_internal]}, + + %% Configurations supporting the rabbitmq_auth_mechanism_ssl and + %% rabbitmq_auth_backend_ldap plugins. + %% + %% NB: These options require that the relevant plugin is enabled. + %% See http://www.rabbitmq.com/plugins.html for further details. + + %% The RabbitMQ-auth-mechanism-ssl plugin makes it possible to + %% authenticate a user based on the client's SSL certificate. + %% + %% To use auth-mechanism-ssl, add to or replace the auth_mechanisms + %% list with the entry 'EXTERNAL'. + %% + %% {auth_mechanisms, ['EXTERNAL']}, + + %% The rabbitmq_auth_backend_ldap plugin allows the broker to + %% perform authentication and authorisation by deferring to an + %% external LDAP server. + %% + %% For more information about configuring the LDAP backend, see + %% http://www.rabbitmq.com/ldap.html. + %% + %% Enable the LDAP auth backend by adding to or replacing the + %% auth_backends entry: + %% + %% {auth_backends, [rabbit_auth_backend_ldap]}, + + %% This pertains to both the rabbitmq_auth_mechanism_ssl plugin and + %% STOMP ssl_cert_login configurations. See the rabbitmq_stomp + %% configuration section later in this file and the README in + %% https://github.com/rabbitmq/rabbitmq-auth-mechanism-ssl for further + %% details. + %% + %% To use the SSL cert's CN instead of its DN as the username + %% + %% {ssl_cert_login_from, common_name}, + + %% SSL handshake timeout, in milliseconds. + %% + %% {ssl_handshake_timeout, 5000}, + + %% + %% Default User / VHost + %% ==================== + %% + + %% On first start RabbitMQ will create a vhost and a user. These + %% config items control what gets created. See + %% http://www.rabbitmq.com/access-control.html for further + %% information about vhosts and access control. + %% + %% {default_vhost, <<"/">>}, + %% {default_user, <<"guest">>}, + %% {default_pass, <<"guest">>}, + %% {default_permissions, [<<".*">>, <<".*">>, <<".*">>]}, + + %% Tags for default user + %% + %% For more details about tags, see the documentation for the + %% Management Plugin at http://www.rabbitmq.com/management.html. + %% + %% {default_user_tags, [administrator]}, + + %% + %% Additional network and protocol related configuration + %% ===================================================== + %% + + %% Set the default AMQP heartbeat delay (in seconds). + %% + %% {heartbeat, 600}, + + %% Set the max permissible size of an AMQP frame (in bytes). + %% + %% {frame_max, 131072}, + + %% Set the max permissible number of channels per connection. + %% 0 means "no limit". + %% + %% {channel_max, 128}, + + %% Customising Socket Options. + %% + %% See (http://www.erlang.org/doc/man/inet.html#setopts-2) for + %% further documentation. + %% + %% {tcp_listen_options, [binary, + %% {packet, raw}, + %% {reuseaddr, true}, + %% {backlog, 128}, + %% {nodelay, true}, + %% {exit_on_close, false}]}, + + %% + %% Resource Limits & Flow Control + %% ============================== + %% + %% See http://www.rabbitmq.com/memory.html for full details. + + %% Memory-based Flow Control threshold. + %% + %% {vm_memory_high_watermark, 0.4}, + + %% Fraction of the high watermark limit at which queues start to + %% page message out to disc in order to free up memory. + %% + %% {vm_memory_high_watermark_paging_ratio, 0.5}, + + %% Set disk free limit (in bytes). Once free disk space reaches this + %% lower bound, a disk alarm will be set - see the documentation + %% listed above for more details. + %% + %% {disk_free_limit, 50000000}, + + %% Alternatively, we can set a limit relative to total available RAM. + %% + %% {disk_free_limit, {mem_relative, 1.0}}, + + %% + %% Misc/Advanced Options + %% ===================== + %% + %% NB: Change these only if you understand what you are doing! + %% + + %% To announce custom properties to clients on connection: + %% + %% {server_properties, []}, + + %% How to respond to cluster partitions. + %% See http://www.rabbitmq.com/partitions.html for further details. + %% + %% {cluster_partition_handling, ignore}, + + %% Make clustering happen *automatically* at startup - only applied + %% to nodes that have just been reset or started for the first time. + %% See http://www.rabbitmq.com/clustering.html#auto-config for + %% further details. + %% + %% {cluster_nodes, {['rabbit@my.host.com'], disc}}, + + %% Interval (in milliseconds) at which we send keepalive messages + %% to other cluster members. Note that this is not the same thing + %% as net_ticktime; missed keepalive messages will not cause nodes + %% to be considered down. + %% + %% {cluster_keepalive_interval, 10000}, + + %% Set (internal) statistics collection granularity. + %% + %% {collect_statistics, none}, + + %% Statistics collection interval (in milliseconds). + %% + %% {collect_statistics_interval, 5000}, + + %% Explicitly enable/disable hipe compilation. + %% + %% {hipe_compile, true}, + + %% Timeout used when waiting for Mnesia tables in a cluster to + %% become available. + %% + %% {mnesia_table_loading_timeout, 30000}, + + %% Size in bytes below which to embed messages in the queue index. See + %% http://www.rabbitmq.com/persistence-conf.html + %% + %% {queue_index_embed_msgs_below, 4096} + + ]}, + + %% ---------------------------------------------------------------------------- + %% Advanced Erlang Networking/Clustering Options. + %% + %% See http://www.rabbitmq.com/clustering.html for details + %% ---------------------------------------------------------------------------- + {kernel, + [%% Sets the net_kernel tick time. + %% Please see http://erlang.org/doc/man/kernel_app.html and + %% http://www.rabbitmq.com/nettick.html for further details. + %% + %% {net_ticktime, 60} + ]}, + + %% ---------------------------------------------------------------------------- + %% RabbitMQ Management Plugin + %% + %% See http://www.rabbitmq.com/management.html for details + %% ---------------------------------------------------------------------------- + + {rabbitmq_management, + [%% Pre-Load schema definitions from the following JSON file. See + %% http://www.rabbitmq.com/management.html#load-definitions + %% + %% {load_definitions, "/path/to/schema.json"}, + + %% Log all requests to the management HTTP API to a file. + %% + %% {http_log_dir, "/path/to/access.log"}, + + %% Change the port on which the HTTP listener listens, + %% specifying an interface for the web server to bind to. + %% Also set the listener to use SSL and provide SSL options. + %% + %% {listener, [{port, 12345}, + %% {ip, "127.0.0.1"}, + %% {ssl, true}, + %% {ssl_opts, [{cacertfile, "/path/to/cacert.pem"}, + %% {certfile, "/path/to/cert.pem"}, + %% {keyfile, "/path/to/key.pem"}]}]}, + + %% One of 'basic', 'detailed' or 'none'. See + %% http://www.rabbitmq.com/management.html#fine-stats for more details. + %% {rates_mode, basic}, + + %% Configure how long aggregated data (such as message rates and queue + %% lengths) is retained. Please read the plugin's documentation in + %% http://www.rabbitmq.com/management.html#configuration for more + %% details. + %% + %% {sample_retention_policies, + %% [{global, [{60, 5}, {3600, 60}, {86400, 1200}]}, + %% {basic, [{60, 5}, {3600, 60}]}, + %% {detailed, [{10, 5}]}]} + ]}, + + %% ---------------------------------------------------------------------------- + %% RabbitMQ Shovel Plugin + %% + %% See http://www.rabbitmq.com/shovel.html for details + %% ---------------------------------------------------------------------------- + + {rabbitmq_shovel, + [{shovels, + [%% A named shovel worker. + %% {my_first_shovel, + %% [ + + %% List the source broker(s) from which to consume. + %% + %% {sources, + %% [%% URI(s) and pre-declarations for all source broker(s). + %% {brokers, ["amqp://user:password@host.domain/my_vhost"]}, + %% {declarations, []} + %% ]}, + + %% List the destination broker(s) to publish to. + %% {destinations, + %% [%% A singular version of the 'brokers' element. + %% {broker, "amqp://"}, + %% {declarations, []} + %% ]}, + + %% Name of the queue to shovel messages from. + %% + %% {queue, <<"your-queue-name-goes-here">>}, + + %% Optional prefetch count. + %% + %% {prefetch_count, 10}, + + %% when to acknowledge messages: + %% - no_ack: never (auto) + %% - on_publish: after each message is republished + %% - on_confirm: when the destination broker confirms receipt + %% + %% {ack_mode, on_confirm}, + + %% Overwrite fields of the outbound basic.publish. + %% + %% {publish_fields, [{exchange, <<"my_exchange">>}, + %% {routing_key, <<"from_shovel">>}]}, + + %% Static list of basic.properties to set on re-publication. + %% + %% {publish_properties, [{delivery_mode, 2}]}, + + %% The number of seconds to wait before attempting to + %% reconnect in the event of a connection failure. + %% + %% {reconnect_delay, 2.5} + + %% ]} %% End of my_first_shovel + ]} + %% Rather than specifying some values per-shovel, you can specify + %% them for all shovels here. + %% + %% {defaults, [{prefetch_count, 0}, + %% {ack_mode, on_confirm}, + %% {publish_fields, []}, + %% {publish_properties, [{delivery_mode, 2}]}, + %% {reconnect_delay, 2.5}]} + ]}, + + %% ---------------------------------------------------------------------------- + %% RabbitMQ Stomp Adapter + %% + %% See http://www.rabbitmq.com/stomp.html for details + %% ---------------------------------------------------------------------------- + + {rabbitmq_stomp, + [%% Network Configuration - the format is generally the same as for the broker + + %% Listen only on localhost (ipv4 & ipv6) on a specific port. + %% {tcp_listeners, [{"127.0.0.1", 61613}, + %% {"::1", 61613}]}, + + %% Listen for SSL connections on a specific port. + %% {ssl_listeners, [61614]}, + + %% Additional SSL options + + %% Extract a name from the client's certificate when using SSL. + %% + %% {ssl_cert_login, true}, + + %% Set a default user name and password. This is used as the default login + %% whenever a CONNECT frame omits the login and passcode headers. + %% + %% Please note that setting this will allow clients to connect without + %% authenticating! + %% + %% {default_user, [{login, "guest"}, + %% {passcode, "guest"}]}, + + %% If a default user is configured, or you have configured use SSL client + %% certificate based authentication, you can choose to allow clients to + %% omit the CONNECT frame entirely. If set to true, the client is + %% automatically connected as the default user or user supplied in the + %% SSL certificate whenever the first frame sent on a session is not a + %% CONNECT frame. + %% + %% {implicit_connect, true} + ]}, + + %% ---------------------------------------------------------------------------- + %% RabbitMQ MQTT Adapter + %% + %% See https://github.com/rabbitmq/rabbitmq-mqtt/blob/stable/README.md + %% for details + %% ---------------------------------------------------------------------------- + + {rabbitmq_mqtt, + [%% Set the default user name and password. Will be used as the default login + %% if a connecting client provides no other login details. + %% + %% Please note that setting this will allow clients to connect without + %% authenticating! + %% + %% {default_user, <<"guest">>}, + %% {default_pass, <<"guest">>}, + + %% Enable anonymous access. If this is set to false, clients MUST provide + %% login information in order to connect. See the default_user/default_pass + %% configuration elements for managing logins without authentication. + %% + %% {allow_anonymous, true}, + + %% If you have multiple chosts, specify the one to which the + %% adapter connects. + %% + %% {vhost, <<"/">>}, + + %% Specify the exchange to which messages from MQTT clients are published. + %% + %% {exchange, <<"amq.topic">>}, + + %% Specify TTL (time to live) to control the lifetime of non-clean sessions. + %% + %% {subscription_ttl, 1800000}, + + %% Set the prefetch count (governing the maximum number of unacknowledged + %% messages that will be delivered). + %% + %% {prefetch, 10}, + + %% TCP/SSL Configuration (as per the broker configuration). + %% + %% {tcp_listeners, [1883]}, + %% {ssl_listeners, []}, + + %% TCP/Socket options (as per the broker configuration). + %% + %% {tcp_listen_options, [binary, + %% {packet, raw}, + %% {reuseaddr, true}, + %% {backlog, 128}, + %% {nodelay, true}]} + ]}, + + %% ---------------------------------------------------------------------------- + %% RabbitMQ AMQP 1.0 Support + %% + %% See https://github.com/rabbitmq/rabbitmq-amqp1.0/blob/stable/README.md + %% for details + %% ---------------------------------------------------------------------------- + + {rabbitmq_amqp1_0, + [%% Connections that are not authenticated with SASL will connect as this + %% account. See the README for more information. + %% + %% Please note that setting this will allow clients to connect without + %% authenticating! + %% + %% {default_user, "guest"}, + + %% Enable protocol strict mode. See the README for more information. + %% + %% {protocol_strict_mode, false} + ]}, + + %% ---------------------------------------------------------------------------- + %% RabbitMQ LDAP Plugin + %% + %% See http://www.rabbitmq.com/ldap.html for details. + %% + %% ---------------------------------------------------------------------------- + + {rabbitmq_auth_backend_ldap, + [%% + %% Connecting to the LDAP server(s) + %% ================================ + %% + + %% Specify servers to bind to. You *must* set this in order for the plugin + %% to work properly. + %% + %% {servers, ["your-server-name-goes-here"]}, + + %% Connect to the LDAP server using SSL + %% + %% {use_ssl, false}, + + %% Specify the LDAP port to connect to + %% + %% {port, 389}, + + %% LDAP connection timeout, in milliseconds or 'infinity' + %% + %% {timeout, infinity}, + + %% Enable logging of LDAP queries. + %% One of + %% - false (no logging is performed) + %% - true (verbose logging of the logic used by the plugin) + %% - network (as true, but additionally logs LDAP network traffic) + %% + %% Defaults to false. + %% + %% {log, false}, + + %% + %% Authentication + %% ============== + %% + + %% Pattern to convert the username given through AMQP to a DN before + %% binding + %% + %% {user_dn_pattern, "cn=${username},ou=People,dc=example,dc=com"}, + + %% Alternatively, you can convert a username to a Distinguished + %% Name via an LDAP lookup after binding. See the documentation for + %% full details. + + %% When converting a username to a dn via a lookup, set these to + %% the name of the attribute that represents the user name, and the + %% base DN for the lookup query. + %% + %% {dn_lookup_attribute, "userPrincipalName"}, + %% {dn_lookup_base, "DC=gopivotal,DC=com"}, + + %% Controls how to bind for authorisation queries and also to + %% retrieve the details of users logging in without presenting a + %% password (e.g., SASL EXTERNAL). + %% One of + %% - as_user (to bind as the authenticated user - requires a password) + %% - anon (to bind anonymously) + %% - {UserDN, Password} (to bind with a specified user name and password) + %% + %% Defaults to 'as_user'. + %% + %% {other_bind, as_user}, + + %% + %% Authorisation + %% ============= + %% + + %% The LDAP plugin can perform a variety of queries against your + %% LDAP server to determine questions of authorisation. See + %% http://www.rabbitmq.com/ldap.html#authorisation for more + %% information. + + %% Set the query to use when determining vhost access + %% + %% {vhost_access_query, {in_group, + %% "ou=${vhost}-users,ou=vhosts,dc=example,dc=com"}}, + + %% Set the query to use when determining resource (e.g., queue) access + %% + %% {resource_access_query, {constant, true}}, + + %% Set queries to determine which tags a user has + %% + %% {tag_queries, []} + ]} +]. diff --git a/rabbitmq-server/docs/rabbitmqctl.1.xml b/rabbitmq-server/docs/rabbitmqctl.1.xml new file mode 100644 index 0000000..92d4846 --- /dev/null +++ b/rabbitmq-server/docs/rabbitmqctl.1.xml @@ -0,0 +1,1928 @@ + + + + + + + RabbitMQ Server + + The RabbitMQ Team <info@rabbitmq.com> + + + + + rabbitmqctl + 1 + RabbitMQ Service + + + + rabbitmqctl + command line tool for managing a RabbitMQ broker + + + + + rabbitmqctl + -n node + -t timeout + -q + command + command options + + + + + Description + + RabbitMQ is an implementation of AMQP, the emerging standard for high + performance enterprise messaging. The RabbitMQ server is a robust and + scalable implementation of an AMQP broker. + + + rabbitmqctl is a command line tool for managing a + RabbitMQ broker. It performs all actions by connecting to one of the + broker's nodes. + + + Diagnostic information is displayed if the broker was not + running, could not be reached, or rejected the connection due to + mismatching Erlang cookies. + + + + + Options + + + -n node + + + Default node is "rabbit@server", where server is the local host. On + a host named "server.example.com", the node name of the RabbitMQ + Erlang node will usually be rabbit@server (unless RABBITMQ_NODENAME + has been set to some non-default value at broker startup time). The + output of hostname -s is usually the correct suffix to use after the + "@" sign. See rabbitmq-server(1) for details of configuring the + RabbitMQ broker. + + + + + -q + + + Quiet output mode is selected with the "-q" flag. Informational + messages are suppressed when quiet mode is in effect. + + + + + -t timeout + + + Operation timeout in seconds. Only applicable to "list" commands. + Default is "infinity". + + + + + + + + Commands + + + Application and Cluster Management + + + + stop pid_file + + + Stops the Erlang node on which RabbitMQ is running. To + restart the node follow the instructions for Running + the Server in the installation + guide. + + + If a is specified, also waits + for the process specified there to terminate. See the + description of the command below + for details on this file. + + For example: + rabbitmqctl stop + + This command instructs the RabbitMQ node to terminate. + + + + + + stop_app + + + Stops the RabbitMQ application, leaving the Erlang node + running. + + + This command is typically run prior to performing other + management actions that require the RabbitMQ application + to be stopped, e.g. reset. + + For example: + rabbitmqctl stop_app + + This command instructs the RabbitMQ node to stop the + RabbitMQ application. + + + + + + start_app + + + Starts the RabbitMQ application. + + + This command is typically run after performing other + management actions that required the RabbitMQ application + to be stopped, e.g. reset. + + For example: + rabbitmqctl start_app + + This command instructs the RabbitMQ node to start the + RabbitMQ application. + + + + + + wait pid_file + + + Wait for the RabbitMQ application to start. + + + This command will wait for the RabbitMQ application to + start at the node. It will wait for the pid file to + be created, then for a process with a pid specified in the + pid file to start, and then for the RabbitMQ application + to start in that process. It will fail if the process + terminates without starting the RabbitMQ application. + + + A suitable pid file is created by + the rabbitmq-server script. By + default this is located in the Mnesia directory. Modify + the RABBITMQ_PID_FILE environment + variable to change the location. + + For example: + rabbitmqctl wait /var/run/rabbitmq/pid + + This command will return when the RabbitMQ node has + started up. + + + + + + reset + + + Return a RabbitMQ node to its virgin state. + + + Removes the node from any cluster it belongs to, removes + all data from the management database, such as configured + users and vhosts, and deletes all persistent + messages. + + + For reset and force_reset to + succeed the RabbitMQ application must have been stopped, + e.g. with stop_app. + + For example: + rabbitmqctl reset + + This command resets the RabbitMQ node. + + + + + + force_reset + + + Forcefully return a RabbitMQ node to its virgin state. + + + The force_reset command differs from + reset in that it resets the node + unconditionally, regardless of the current management + database state and cluster configuration. It should only + be used as a last resort if the database or cluster + configuration has been corrupted. + + + For reset and force_reset to + succeed the RabbitMQ application must have been stopped, + e.g. with stop_app. + + For example: + rabbitmqctl force_reset + + This command resets the RabbitMQ node. + + + + + + rotate_logs suffix + + + Instruct the RabbitMQ node to rotate the log files. + + + The RabbitMQ broker appends the contents of its log + files to files with names composed of the original name + and the suffix, and then resumes logging to freshly + created files at the original location. I.e. effectively + the current log contents are moved to the end of the + suffixed files. + + + When the target files do not exist they are created. + When no is specified, the empty + log files are simply created at the original location; + no rotation takes place. + + For example: + rabbitmqctl rotate_logs .1 + + This command instructs the RabbitMQ node to append the contents + of the log files to files with names consisting of the original logs' + names and ".1" suffix, e.g. rabbit@mymachine.log.1 and + rabbit@mymachine-sasl.log.1. Finally, logging resumes to + fresh files at the old locations. + + + + + + + + Cluster management + + + + join_cluster clusternode --ram + + + + clusternode + Node to cluster with. + + + --ram + + + If provided, the node will join the cluster as a RAM node. + + + + + + Instruct the node to become a member of the cluster that the + specified node is in. Before clustering, the node is reset, so be + careful when using this command. For this command to succeed the + RabbitMQ application must have been stopped, e.g. with stop_app. + + + Cluster nodes can be of two types: disc or RAM. Disc nodes + replicate data in RAM and on disc, thus providing redundancy in + the event of node failure and recovery from global events such + as power failure across all nodes. RAM nodes replicate data in + RAM only (with the exception of queue contents, which can reside + on disc if the queue is persistent or too big to fit in memory) + and are mainly used for scalability. RAM nodes are more + performant only when managing resources (e.g. adding/removing + queues, exchanges, or bindings). A cluster must always have at + least one disc node, and usually should have more than one. + + + The node will be a disc node by default. If you wish to + create a RAM node, provide the --ram flag. + + + After executing the cluster command, whenever + the RabbitMQ application is started on the current node it will + attempt to connect to the nodes that were in the cluster when the + node went down. + + + To leave a cluster, reset the node. You can + also remove nodes remotely with the + forget_cluster_node command. + + + For more details see the clustering + guide. + + For example: + rabbitmqctl join_cluster hare@elena --ram + + This command instructs the RabbitMQ node to join the cluster that + hare@elena is part of, as a ram node. + + + + + cluster_status + + + Displays all the nodes in the cluster grouped by node type, + together with the currently running nodes. + + For example: + rabbitmqctl cluster_status + + This command displays the nodes in the cluster. + + + + + change_cluster_node_type disc | ram + + + + Changes the type of the cluster node. The node must be stopped for + this operation to succeed, and when turning a node into a RAM node + the node must not be the only disc node in the cluster. + + For example: + rabbitmqctl change_cluster_node_type disc + + This command will turn a RAM node into a disc node. + + + + + forget_cluster_node --offline + + + + --offline + + + Enables node removal from an offline node. This is only + useful in the situation where all the nodes are offline and + the last node to go down cannot be brought online, thus + preventing the whole cluster from starting. It should not be + used in any other circumstances since it can lead to + inconsistencies. + + + + + + Removes a cluster node remotely. The node that is being removed + must be offline, while the node we are removing from must be + online, except when using the --offline flag. + + + When using the --offline flag + rabbitmqctl will not attempt to connect to a node as + normal; instead it will temporarily become the node in + order to make the change. This is useful if the node + cannot be started normally. In this case the node will + become the canonical source for cluster metadata + (e.g. which queues exist), even if it was not + before. Therefore you should use this command on the + latest node to shut down if at all possible. + + For example: + rabbitmqctl -n hare@mcnulty forget_cluster_node rabbit@stringer + + This command will remove the node + rabbit@stringer from the node + hare@mcnulty. + + + + + rename_cluster_node oldnode1 newnode1 oldnode2 newnode2 ... + + + Supports renaming of cluster nodes in the local database. + + + This subcommand causes rabbitmqctl to temporarily become + the node in order to make the change. The local cluster + node must therefore be completely stopped; other nodes + can be online or offline. + + + This subcommand takes an even number of arguments, in + pairs representing the old and new names for nodes. You + must specify the old and new names for this node and for + any other nodes that are stopped and being renamed at + the same time. + + + It is possible to stop all nodes and rename them all + simultaneously (in which case old and new names for all + nodes must be given to every node) or stop and rename + nodes one at a time (in which case each node only needs + to be told how its own name is changing). + + For example: + rabbitmqctl rename_cluster_node rabbit@misshelpful rabbit@cordelia + + This command will rename the node + rabbit@misshelpful to the node + rabbit@cordelia. + + + + + update_cluster_nodes clusternode + + + + + clusternode + + + The node to consult for up to date information. + + + + + + Instructs an already clustered node to contact + clusternode to cluster when waking up. This is + different from join_cluster since it does not + join any cluster - it checks that the node is already in a cluster + with clusternode. + + + The need for this command is motivated by the fact that clusters + can change while a node is offline. Consider the situation in + which node A and B are clustered. A goes down, C clusters with B, + and then B leaves the cluster. When A wakes up, it'll try to + contact B, but this will fail since B is not in the cluster + anymore. update_cluster_nodes -n A C will solve + this situation. + + + + + force_boot + + + Ensure that the node will start next time, even if it + was not the last to shut down. + + + Normally when you shut down a RabbitMQ cluster + altogether, the first node you restart should be the + last one to go down, since it may have seen things + happen that other nodes did not. But sometimes + that's not possible: for instance if the entire cluster + loses power then all nodes may think they were not the + last to shut down. + + + In such a case you can invoke rabbitmqctl + force_boot while the node is down. This will + tell the node to unconditionally start next time you ask + it to. If any changes happened to the cluster after this + node shut down, they will be lost. + + + If the last node to go down is permanently lost then you + should use rabbitmqctl forget_cluster_node + --offline in preference to this command, as it + will ensure that mirrored queues which were mastered on + the lost node get promoted. + + For example: + rabbitmqctl force_boot + + This will force the node not to wait for other nodes + next time it is started. + + + + + sync_queue queue + + + + + queue + + + The name of the queue to synchronise. + + + + + + Instructs a mirrored queue with unsynchronised slaves to + synchronise itself. The queue will block while + synchronisation takes place (all publishers to and + consumers from the queue will block). The queue must be + mirrored for this command to succeed. + + + Note that unsynchronised queues from which messages are + being drained will become synchronised eventually. This + command is primarily useful for queues which are not + being drained. + + + + + cancel_sync_queue queue + + + + + queue + + + The name of the queue to cancel synchronisation for. + + + + + + Instructs a synchronising mirrored queue to stop + synchronising itself. + + + + + purge_queue queue + + + + + queue + + + The name of the queue to purge. + + + + + + Purges a queue (removes all messages in it). + + + + + set_cluster_name name + + + Sets the cluster name. The cluster name is announced to + clients on connection, and used by the federation and + shovel plugins to record where a message has been. The + cluster name is by default derived from the hostname of + the first node in the cluster, but can be changed. + + For example: + rabbitmqctl set_cluster_name london + + This sets the cluster name to "london". + + + + + + + + User management + + Note that rabbitmqctl manages the RabbitMQ + internal user database. Users from any alternative + authentication backend will not be visible + to rabbitmqctl. + + + + add_user username password + + + + username + The name of the user to create. + + + password + The password the created user will use to log in to the broker. + + + For example: + rabbitmqctl add_user tonyg changeit + + This command instructs the RabbitMQ broker to create a + (non-administrative) user named tonyg with + (initial) password + changeit. + + + + + + delete_user username + + + + username + The name of the user to delete. + + + For example: + rabbitmqctl delete_user tonyg + + This command instructs the RabbitMQ broker to delete the + user named tonyg. + + + + + + change_password username newpassword + + + + username + The name of the user whose password is to be changed. + + + newpassword + The new password for the user. + + + For example: + rabbitmqctl change_password tonyg newpass + + This command instructs the RabbitMQ broker to change the + password for the user named tonyg to + newpass. + + + + + + clear_password username + + + + username + The name of the user whose password is to be cleared. + + + For example: + rabbitmqctl clear_password tonyg + + This command instructs the RabbitMQ broker to clear the + password for the user named + tonyg. This user now cannot log in with a password (but may be able to through e.g. SASL EXTERNAL if configured). + + + + + + set_user_tags username tag ... + + + + username + The name of the user whose tags are to + be set. + + + tag + Zero, one or more tags to set. Any + existing tags will be removed. + + + For example: + rabbitmqctl set_user_tags tonyg administrator + + This command instructs the RabbitMQ broker to ensure the user + named tonyg is an administrator. This has no + effect when the user logs in via AMQP, but can be used to permit + the user to manage users, virtual hosts and permissions when the + user logs in via some other means (for example with the + management plugin). + + rabbitmqctl set_user_tags tonyg + + This command instructs the RabbitMQ broker to remove any + tags from the user named tonyg. + + + + + + list_users + + + Lists users. Each result row will contain the user name + followed by a list of the tags set for that user. + + For example: + rabbitmqctl list_users + + This command instructs the RabbitMQ broker to list all + users. + + + + + + + + Access control + + Note that rabbitmqctl manages the RabbitMQ + internal user database. Permissions for users from any + alternative authorisation backend will not be visible + to rabbitmqctl. + + + + add_vhost vhostpath + + + + vhostpath + The name of the virtual host entry to create. + + + + Creates a virtual host. + + For example: + rabbitmqctl add_vhost test + + This command instructs the RabbitMQ broker to create a new + virtual host called test. + + + + + + delete_vhost vhostpath + + + + vhostpath + The name of the virtual host entry to delete. + + + + Deletes a virtual host. + + + Deleting a virtual host deletes all its exchanges, + queues, bindings, user permissions, parameters and policies. + + For example: + rabbitmqctl delete_vhost test + + This command instructs the RabbitMQ broker to delete the + virtual host called test. + + + + + + list_vhosts vhostinfoitem ... + + + Lists virtual hosts. + + + The vhostinfoitem parameter is used to indicate which + virtual host information items to include in the results. The column order in the + results will match the order of the parameters. + vhostinfoitem can take any value from + the list that follows: + + + + name + The name of the virtual host with non-ASCII characters escaped as in C. + + + tracing + Whether tracing is enabled for this virtual host. + + + + If no vhostinfoitems are specified + then the vhost name is displayed. + + For example: + rabbitmqctl list_vhosts name tracing + + This command instructs the RabbitMQ broker to list all + virtual hosts. + + + + + + set_permissions -p vhostpath user conf write read + + + + vhostpath + The name of the virtual host to which to grant the user access, defaulting to /. + + + user + The name of the user to grant access to the specified virtual host. + + + conf + A regular expression matching resource names for which the user is granted configure permissions. + + + write + A regular expression matching resource names for which the user is granted write permissions. + + + read + A regular expression matching resource names for which the user is granted read permissions. + + + + Sets user permissions. + + For example: + rabbitmqctl set_permissions -p /myvhost tonyg "^tonyg-.*" ".*" ".*" + + This command instructs the RabbitMQ broker to grant the + user named tonyg access to the virtual host + called /myvhost, with configure permissions + on all resources whose names starts with "tonyg-", and + write and read permissions on all resources. + + + + + + clear_permissions -p vhostpath username + + + + vhostpath + The name of the virtual host to which to deny the user access, defaulting to /. + + + username + The name of the user to deny access to the specified virtual host. + + + + Sets user permissions. + + For example: + rabbitmqctl clear_permissions -p /myvhost tonyg + + This command instructs the RabbitMQ broker to deny the + user named tonyg access to the virtual host + called /myvhost. + + + + + + list_permissions -p vhostpath + + + + vhostpath + The name of the virtual host for which to list the users that have been granted access to it, and their permissions. Defaults to /. + + + + Lists permissions in a virtual host. + + For example: + rabbitmqctl list_permissions -p /myvhost + + This command instructs the RabbitMQ broker to list all + the users which have been granted access to the virtual + host called /myvhost, and the + permissions they have for operations on resources in + that virtual host. Note that an empty string means no + permissions granted. + + + + + + list_user_permissions username + + + + username + The name of the user for which to list the permissions. + + + + Lists user permissions. + + For example: + rabbitmqctl list_user_permissions tonyg + + This command instructs the RabbitMQ broker to list all the + virtual hosts to which the user named tonyg + has been granted access, and the permissions the user has + for operations on resources in these virtual hosts. + + + + + + + + Parameter Management + + Certain features of RabbitMQ (such as the federation plugin) + are controlled by dynamic, + cluster-wide parameters. Each parameter + consists of a component name, a name and a value, and is + associated with a virtual host. The component name and name are + strings, and the value is an Erlang term. Parameters can be + set, cleared and listed. In general you should refer to the + documentation for the feature in question to see how to set + parameters. + + + + set_parameter -p vhostpath component_name name value + + + Sets a parameter. + + + + component_name + + The name of the component for which the + parameter is being set. + + + + name + + The name of the parameter being set. + + + + value + + The value for the parameter, as a + JSON term. In most shells you are very likely to + need to quote this. + + + + For example: + rabbitmqctl set_parameter federation local_username '"guest"' + + This command sets the parameter local_username for the federation component in the default virtual host to the JSON term "guest". + + + + + clear_parameter -p vhostpath component_name key + + + Clears a parameter. + + + + component_name + + The name of the component for which the + parameter is being cleared. + + + + name + + The name of the parameter being cleared. + + + + For example: + rabbitmqctl clear_parameter federation local_username + + This command clears the parameter local_username for the federation component in the default virtual host. + + + + + list_parameters -p vhostpath + + + Lists all parameters for a virtual host. + + For example: + rabbitmqctl list_parameters + + This command lists all parameters in the default virtual host. + + + + + + + + Policy Management + + Policies are used to control and modify the behaviour of queues + and exchanges on a cluster-wide basis. Policies apply within a + given vhost, and consist of a name, pattern, definition and an + optional priority. Policies can be set, cleared and listed. + + + + set_policy -p vhostpath --priority priority --apply-to apply-to name pattern definition + + + Sets a policy. + + + + name + + The name of the policy. + + + + pattern + + The regular expression, which when matches on a given resources causes the policy to apply. + + + + definition + + The definition of the policy, as a + JSON term. In most shells you are very likely to + need to quote this. + + + + priority + + The priority of the policy as an integer. Higher numbers indicate greater precedence. The default is 0. + + + + apply-to + + Which types of object this policy should apply to - "queues", "exchanges" or "all". The default is "all". + + + + For example: + rabbitmqctl set_policy federate-me "^amq." '{"federation-upstream-set":"all"}' + + This command sets the policy federate-me in the default virtual host so that built-in exchanges are federated. + + + + + clear_policy -p vhostpath name + + + Clears a policy. + + + + name + + The name of the policy being cleared. + + + + For example: + rabbitmqctl clear_policy federate-me + + This command clears the federate-me policy in the default virtual host. + + + + + list_policies -p vhostpath + + + Lists all policies for a virtual host. + + For example: + rabbitmqctl list_policies + + This command lists all policies in the default virtual host. + + + + + + + + Server Status + + The server status queries interrogate the server and return a list of + results with tab-delimited columns. Some queries (list_queues, + list_exchanges, list_bindings, and + list_consumers) accept an + optional vhost parameter. This parameter, if present, must be + specified immediately after the query. + + + The list_queues, list_exchanges and list_bindings commands accept an + optional virtual host parameter for which to display results. The + default value is "/". + + + + + list_queues -p vhostpath queueinfoitem ... + + + Returns queue details. Queue details of the / virtual host + are returned if the "-p" flag is absent. The "-p" flag can be used to + override this default. + + + The queueinfoitem parameter is used to indicate which queue + information items to include in the results. The column order in the + results will match the order of the parameters. + queueinfoitem can take any value from the list + that follows: + + + + name + The name of the queue with non-ASCII characters escaped as in C. + + + durable + Whether or not the queue survives server restarts. + + + auto_delete + Whether the queue will be deleted automatically when no longer used. + + + arguments + Queue arguments. + + + policy + Policy name applying to the queue. + + + pid + Id of the Erlang process associated with the queue. + + + owner_pid + Id of the Erlang process representing the connection + which is the exclusive owner of the queue. Empty if the + queue is non-exclusive. + + + exclusive_consumer_pid + Id of the Erlang process representing the channel of the + exclusive consumer subscribed to this queue. Empty if + there is no exclusive consumer. + + + exclusive_consumer_tag + Consumer tag of the exclusive consumer subscribed to + this queue. Empty if there is no exclusive consumer. + + + messages_ready + Number of messages ready to be delivered to clients. + + + messages_unacknowledged + Number of messages delivered to clients but not yet acknowledged. + + + messages + Sum of ready and unacknowledged messages + (queue depth). + + + messages_ready_ram + Number of messages from messages_ready which are resident in ram. + + + messages_unacknowledged_ram + Number of messages from messages_unacknowledged which are resident in ram. + + + messages_ram + Total number of messages which are resident in ram. + + + messages_persistent + Total number of persistent messages in the queue (will always be 0 for transient queues). + + + message_bytes + Sum of the size of all message bodies in the queue. This does not include the message properties (including headers) or any overhead. + + + message_bytes_ready + Like message_bytes but counting only those messages ready to be delivered to clients. + + + message_bytes_unacknowledged + Like message_bytes but counting only those messages delivered to clients but not yet acknowledged. + + + message_bytes_ram + Like message_bytes but counting only those messages which are in RAM. + + + message_bytes_persistent + Like message_bytes but counting only those messages which are persistent. + + + disk_reads + Total number of times messages have been read from disk by this queue since it started. + + + disk_writes + Total number of times messages have been written to disk by this queue since it started. + + + consumers + Number of consumers. + + + consumer_utilisation + Fraction of the time (between 0.0 and 1.0) + that the queue is able to immediately deliver messages to + consumers. This can be less than 1.0 if consumers are limited + by network congestion or prefetch count. + + + memory + Bytes of memory consumed by the Erlang process associated with the + queue, including stack, heap and internal structures. + + + slave_pids + If the queue is mirrored, this gives the IDs of the current slaves. + + + synchronised_slave_pids + If the queue is mirrored, this gives the IDs of + the current slaves which are synchronised with the master - + i.e. those which could take over from the master without + message loss. + + + state + The state of the queue. Normally + 'running', but may be "{syncing, MsgCount}" if the + queue is synchronising. Queues which are located on + cluster nodes that are currently down will be shown + with a status of 'down' (and most other + queueinfoitems will be + unavailable). + + + + If no queueinfoitems are specified then queue name and depth are + displayed. + + + For example: + + rabbitmqctl list_queues -p /myvhost messages consumers + + This command displays the depth and number of consumers for each + queue of the virtual host named /myvhost. + + + + + + list_exchanges -p vhostpath exchangeinfoitem ... + + + Returns exchange details. Exchange details of the / virtual host + are returned if the "-p" flag is absent. The "-p" flag can be used to + override this default. + + + The exchangeinfoitem parameter is used to indicate which + exchange information items to include in the results. The column order in the + results will match the order of the parameters. + exchangeinfoitem can take any value from the list + that follows: + + + + name + The name of the exchange with non-ASCII characters escaped as in C. + + + type + The exchange type (such as + [direct, + topic, headers, + fanout]). + + + durable + Whether or not the exchange survives server restarts. + + + auto_delete + Whether the exchange will be deleted automatically when no longer used. + + + internal + Whether the exchange is internal, i.e. cannot be directly published to by a client. + + + arguments + Exchange arguments. + + + policy + Policy name for applying to the exchange. + + + + If no exchangeinfoitems are specified then + exchange name and type are displayed. + + + For example: + + rabbitmqctl list_exchanges -p /myvhost name type + + This command displays the name and type for each + exchange of the virtual host named /myvhost. + + + + + + list_bindings -p vhostpath bindinginfoitem ... + + + Returns binding details. By default the bindings for + the / virtual host are returned. The + "-p" flag can be used to override this default. + + + The bindinginfoitem parameter is used + to indicate which binding information items to include + in the results. The column order in the results will + match the order of the parameters. + bindinginfoitem can take any value + from the list that follows: + + + + source_name + The name of the source of messages to + which the binding is attached. With non-ASCII + characters escaped as in C. + + + source_kind + The kind of the source of messages to + which the binding is attached. Currently always + exchange. With non-ASCII characters escaped as in + C. + + + destination_name + The name of the destination of + messages to which the binding is attached. With + non-ASCII characters escaped as in + C. + + + destination_kind + The kind of the destination of + messages to which the binding is attached. With + non-ASCII characters escaped as in + C. + + + routing_key + The binding's routing key, with + non-ASCII characters escaped as in C. + + + arguments + The binding's arguments. + + + + If no bindinginfoitems are specified then + all above items are displayed. + + + For example: + + rabbitmqctl list_bindings -p /myvhost exchange_name queue_name + + This command displays the exchange name and queue name + of the bindings in the virtual host + named /myvhost. + + + + + + list_connections connectioninfoitem ... + + + Returns TCP/IP connection statistics. + + + The connectioninfoitem parameter is used to indicate + which connection information items to include in the results. The + column order in the results will match the order of the parameters. + connectioninfoitem can take any value from the list + that follows: + + + + + pid + Id of the Erlang process associated with the connection. + + + name + Readable name for the connection. + + + port + Server port. + + + host + Server hostname obtained via reverse + DNS, or its IP address if reverse DNS failed or was + not enabled. + + + peer_port + Peer port. + + + peer_host + Peer hostname obtained via reverse + DNS, or its IP address if reverse DNS failed or was + not enabled. + + + ssl + Boolean indicating whether the + connection is secured with SSL. + + + ssl_protocol + SSL protocol + (e.g. tlsv1) + + + ssl_key_exchange + SSL key exchange algorithm + (e.g. rsa) + + + ssl_cipher + SSL cipher algorithm + (e.g. aes_256_cbc) + + + ssl_hash + SSL hash function + (e.g. sha) + + + peer_cert_subject + The subject of the peer's SSL + certificate, in RFC4514 form. + + + peer_cert_issuer + The issuer of the peer's SSL + certificate, in RFC4514 form. + + + peer_cert_validity + The period for which the peer's SSL + certificate is valid. + + + + state + Connection state (one of [starting, tuning, + opening, running, flow, blocking, blocked, closing, closed]). + + + channels + Number of channels using the connection. + + + protocol + Version of the AMQP protocol in use (currently one of {0,9,1} or {0,8,0}). Note that if a client requests an AMQP 0-9 connection, we treat it as AMQP 0-9-1. + + + auth_mechanism + SASL authentication mechanism used, such as PLAIN. + + + user + Username associated with the connection. + + + vhost + Virtual host name with non-ASCII characters escaped as in C. + + + timeout + Connection timeout / negotiated heartbeat interval, in seconds. + + + frame_max + Maximum frame size (bytes). + + + channel_max + Maximum number of channels on this connection. + + + client_properties + Informational properties transmitted by the client + during connection establishment. + + + recv_oct + Octets received. + + + recv_cnt + Packets received. + + + send_oct + Octets send. + + + send_cnt + Packets sent. + + + send_pend + Send queue size. + + + connected_at + Date and time this connection was established, as timestamp. + + + + If no connectioninfoitems are + specified then user, peer host, peer port, time since + flow control and memory block state are displayed. + + + + For example: + + rabbitmqctl list_connections send_pend port + + This command displays the send queue size and server port for each + connection. + + + + + + list_channels channelinfoitem ... + + + Returns information on all current channels, the logical + containers executing most AMQP commands. This includes + channels that are part of ordinary AMQP connections, and + channels created by various plug-ins and other extensions. + + + The channelinfoitem parameter is used to + indicate which channel information items to include in the + results. The column order in the results will match the + order of the parameters. + channelinfoitem can take any value from the list + that follows: + + + + + pid + Id of the Erlang process associated with the connection. + + + connection + Id of the Erlang process associated with the connection + to which the channel belongs. + + + name + Readable name for the channel. + + + number + The number of the channel, which uniquely identifies it within + a connection. + + + user + Username associated with the channel. + + + vhost + Virtual host in which the channel operates. + + + transactional + True if the channel is in transactional mode, false otherwise. + + + confirm + True if the channel is in confirm mode, false otherwise. + + + consumer_count + Number of logical AMQP consumers retrieving messages via + the channel. + + + messages_unacknowledged + Number of messages delivered via this channel but not + yet acknowledged. + + + messages_uncommitted + Number of messages received in an as yet + uncommitted transaction. + + + acks_uncommitted + Number of acknowledgements received in an as yet + uncommitted transaction. + + + messages_unconfirmed + Number of published messages not yet + confirmed. On channels not in confirm mode, this + remains 0. + + + prefetch_count + QoS prefetch limit for new consumers, 0 if unlimited. + + + global_prefetch_count + QoS prefetch limit for the entire channel, 0 if unlimited. + + + + If no channelinfoitems are specified then pid, + user, consumer_count, and messages_unacknowledged are assumed. + + + + For example: + + rabbitmqctl list_channels connection messages_unacknowledged + + This command displays the connection process and count + of unacknowledged messages for each channel. + + + + + + list_consumers -p vhostpath + + + List consumers, i.e. subscriptions to a queue's message + stream. Each line printed shows, separated by tab + characters, the name of the queue subscribed to, the id of + the channel process via which the subscription was created + and is managed, the consumer tag which uniquely identifies + the subscription within a channel, a boolean + indicating whether acknowledgements are expected for + messages delivered to this consumer, an integer indicating + the prefetch limit (with 0 meaning 'none'), and any arguments + for this consumer. + + + + + + status + + + Displays broker status information such as the running + applications on the current Erlang node, RabbitMQ and + Erlang versions, OS name, memory and file descriptor + statistics. (See the cluster_status + command to find out which nodes are clustered and + running.) + + For example: + rabbitmqctl status + + This command displays information about the RabbitMQ + broker. + + + + + + environment + + + Display the name and value of each variable in the + application environment for each running application. + + + + + + report + + + Generate a server status report containing a + concatenation of all server status information for + support purposes. The output should be redirected to a + file when accompanying a support request. + + + For example: + + rabbitmqctl report > server_report.txt + + This command creates a server report which may be + attached to a support request email. + + + + + + eval expr + + + Evaluate an arbitrary Erlang expression. + + + For example: + + rabbitmqctl eval 'node().' + + This command returns the name of the node to which rabbitmqctl has connected. + + + + + + + + Miscellaneous + + + close_connection connectionpid explanation + + + + connectionpid + Id of the Erlang process associated with the connection to close. + + + explanation + Explanation string. + + + + Instruct the broker to close the connection associated + with the Erlang process id (see also the + list_connections + command), passing the string to the + connected client as part of the AMQP connection shutdown + protocol. + + For example: + rabbitmqctl close_connection "<rabbit@tanto.4262.0>" "go away" + + This command instructs the RabbitMQ broker to close the + connection associated with the Erlang process + id <rabbit@tanto.4262.0>, passing the + explanation go away to the connected client. + + + + + trace_on -p vhost + + + + vhost + The name of the virtual host for which to start tracing. + + + + Starts tracing. Note that the trace state is not + persistent; it will revert to being off if the server is + restarted. + + + + + + trace_off -p vhost + + + + vhost + The name of the virtual host for which to stop tracing. + + + + Stops tracing. + + + + + set_vm_memory_high_watermark fraction + + + + fraction + + The new memory threshold fraction at which flow + control is triggered, as a floating point number + greater than or equal to 0. + + + + + + + + + + diff --git a/rabbitmq-server/docs/remove-namespaces.xsl b/rabbitmq-server/docs/remove-namespaces.xsl new file mode 100644 index 0000000..7f7f3c1 --- /dev/null +++ b/rabbitmq-server/docs/remove-namespaces.xsl @@ -0,0 +1,18 @@ + + + + + + + + + + + + + + + diff --git a/rabbitmq-server/docs/usage.xsl b/rabbitmq-server/docs/usage.xsl new file mode 100644 index 0000000..586f830 --- /dev/null +++ b/rabbitmq-server/docs/usage.xsl @@ -0,0 +1,74 @@ + + + + + + + + + + +%% Generated, do not edit! +-module(). +-export([usage/0]). +usage() -> %QUOTE%Usage: + + + + + + + + + + + + Options: + + + + + , + + + + + + + + + + + + + Commands: + + + + + + + + + +%QUOTE%. + + + +<> must be a member of the list [, ]. + + + + + + + + + +[] +<> + + diff --git a/rabbitmq-server/ebin/rabbit_app.in b/rabbitmq-server/ebin/rabbit_app.in new file mode 100644 index 0000000..bd4a9f3 --- /dev/null +++ b/rabbitmq-server/ebin/rabbit_app.in @@ -0,0 +1,98 @@ +{application, rabbit, %% -*- erlang -*- + [{description, "RabbitMQ"}, + {id, "RabbitMQ"}, + {vsn, "3.5.6"}, + {modules, []}, + {registered, [rabbit_amqqueue_sup, + rabbit_log, + rabbit_node_monitor, + rabbit_router, + rabbit_sup, + rabbit_tcp_client_sup, + rabbit_direct_client_sup]}, + {applications, [kernel, stdlib, sasl, mnesia, os_mon, xmerl]}, +%% we also depend on crypto, public_key and ssl but they shouldn't be +%% in here as we don't actually want to start it + {mod, {rabbit, []}}, + {env, [{tcp_listeners, [5672]}, + {ssl_listeners, []}, + {ssl_options, []}, + {vm_memory_high_watermark, 0.4}, + {vm_memory_high_watermark_paging_ratio, 0.5}, + {disk_free_limit, 50000000}, %% 50MB + {msg_store_index_module, rabbit_msg_store_ets_index}, + {backing_queue_module, rabbit_variable_queue}, + %% 0 ("no limit") would make a better default, but that + %% breaks the QPid Java client + {frame_max, 131072}, + {channel_max, 0}, + {heartbeat, 60}, + {msg_store_file_size_limit, 16777216}, + {fhc_write_buffering, true}, + {fhc_read_buffering, true}, + {queue_index_max_journal_entries, 32768}, + {queue_index_embed_msgs_below, 4096}, + {default_user, <<"guest">>}, + {default_pass, <<"guest">>}, + {default_user_tags, [administrator]}, + {default_vhost, <<"/">>}, + {default_permissions, [<<".*">>, <<".*">>, <<".*">>]}, + {loopback_users, [<<"guest">>]}, + {cluster_nodes, {[], disc}}, + {server_properties, []}, + {collect_statistics, none}, + {collect_statistics_interval, 5000}, + {mnesia_table_loading_timeout, 30000}, + {auth_mechanisms, ['PLAIN', 'AMQPLAIN']}, + {auth_backends, [rabbit_auth_backend_internal]}, + {delegate_count, 16}, + {trace_vhosts, []}, + {log_levels, [{connection, info}]}, + {ssl_cert_login_from, distinguished_name}, + {ssl_handshake_timeout, 5000}, + {ssl_allow_poodle_attack, false}, + {handshake_timeout, 10000}, + {reverse_dns_lookups, false}, + {cluster_partition_handling, ignore}, + {cluster_keepalive_interval, 10000}, + {tcp_listen_options, [binary, + {packet, raw}, + {reuseaddr, true}, + {backlog, 128}, + {nodelay, true}, + {linger, {true, 0}}, + {exit_on_close, false}]}, + {halt_on_upgrade_failure, true}, + {hipe_compile, false}, + %% see bug 24513 for how this list was created + {hipe_modules, + [rabbit_reader, rabbit_channel, gen_server2, rabbit_exchange, + rabbit_command_assembler, rabbit_framing_amqp_0_9_1, rabbit_basic, + rabbit_event, lists, queue, priority_queue, rabbit_router, + rabbit_trace, rabbit_misc, rabbit_binary_parser, + rabbit_exchange_type_direct, rabbit_guid, rabbit_net, + rabbit_amqqueue_process, rabbit_variable_queue, + rabbit_binary_generator, rabbit_writer, delegate, gb_sets, lqueue, + sets, orddict, rabbit_amqqueue, rabbit_limiter, gb_trees, + rabbit_queue_index, rabbit_exchange_decorator, gen, dict, ordsets, + file_handle_cache, rabbit_msg_store, array, + rabbit_msg_store_ets_index, rabbit_msg_file, + rabbit_exchange_type_fanout, rabbit_exchange_type_topic, mnesia, + mnesia_lib, rpc, mnesia_tm, qlc, sofs, proplists, credit_flow, + pmon, ssl_connection, tls_connection, ssl_record, tls_record, + gen_fsm, ssl]}, + {ssl_apps, [asn1, crypto, public_key, ssl]}, + %% see rabbitmq-server#114 + {mirroring_flow_control, true}, + %% see rabbitmq-server#227 and related tickets. + %% msg_store_credit_disc_bound only takes effect when + %% messages are persisted to the message store. If messages + %% are embedded on the queue index, then modifying this + %% setting has no effect because credit_flow is not used when + %% writing to the queue index. See the setting + %% queue_index_embed_msgs_below above. + {msg_store_credit_disc_bound, {2000, 500}}, + {msg_store_io_batch_size, 2048}, + %% see rabbitmq-server#143 + {credit_flow_default_credit, {200, 50}} + ]}]}. diff --git a/rabbitmq-server/generate_app b/rabbitmq-server/generate_app new file mode 100644 index 0000000..fb0eb1e --- /dev/null +++ b/rabbitmq-server/generate_app @@ -0,0 +1,16 @@ +#!/usr/bin/env escript +%% -*- erlang -*- + +main([InFile, OutFile | SrcDirs]) -> + Modules = [list_to_atom(filename:basename(F, ".erl")) || + SrcDir <- SrcDirs, + F <- filelib:wildcard("*.erl", SrcDir)], + {ok, [{application, Application, Properties}]} = file:consult(InFile), + NewProperties = + case proplists:get_value(modules, Properties) of + [] -> lists:keyreplace(modules, 1, Properties, {modules, Modules}); + _ -> Properties + end, + file:write_file( + OutFile, + io_lib:format("~p.~n", [{application, Application, NewProperties}])). diff --git a/rabbitmq-server/generate_deps b/rabbitmq-server/generate_deps new file mode 100644 index 0000000..ddfca81 --- /dev/null +++ b/rabbitmq-server/generate_deps @@ -0,0 +1,57 @@ +#!/usr/bin/env escript +%% -*- erlang -*- +-mode(compile). + +%% We expect the list of Erlang source and header files to arrive on +%% stdin, with the entries colon-separated. +main([TargetFile, EbinDir]) -> + ErlsAndHrls = [ string:strip(S,left) || + S <- string:tokens(io:get_line(""), ":\n")], + ErlFiles = [F || F <- ErlsAndHrls, lists:suffix(".erl", F)], + Modules = sets:from_list( + [list_to_atom(filename:basename(FileName, ".erl")) || + FileName <- ErlFiles]), + HrlFiles = [F || F <- ErlsAndHrls, lists:suffix(".hrl", F)], + IncludeDirs = lists:usort([filename:dirname(Path) || Path <- HrlFiles]), + Headers = sets:from_list(HrlFiles), + Deps = lists:foldl( + fun (Path, Deps1) -> + dict:store(Path, detect_deps(IncludeDirs, EbinDir, + Modules, Headers, Path), + Deps1) + end, dict:new(), ErlFiles), + {ok, Hdl} = file:open(TargetFile, [write, delayed_write]), + dict:fold( + fun (_Path, [], ok) -> + ok; + (Path, Dep, ok) -> + Module = filename:basename(Path, ".erl"), + ok = file:write(Hdl, [EbinDir, "/", Module, ".beam: ", + Path]), + ok = sets:fold(fun (E, ok) -> file:write(Hdl, [" ", E]) end, + ok, Dep), + file:write(Hdl, ["\n"]) + end, ok, Deps), + ok = file:write(Hdl, [TargetFile, ": ", escript:script_name(), "\n"]), + ok = file:sync(Hdl), + ok = file:close(Hdl). + +detect_deps(IncludeDirs, EbinDir, Modules, Headers, Path) -> + {ok, Forms} = epp:parse_file(Path, IncludeDirs, [{use_specs, true}]), + lists:foldl( + fun ({attribute, _LineNumber, Attribute, Behaviour}, Deps) + when Attribute =:= behaviour orelse Attribute =:= behavior -> + case sets:is_element(Behaviour, Modules) of + true -> sets:add_element( + [EbinDir, "/", atom_to_list(Behaviour), ".beam"], + Deps); + false -> Deps + end; + ({attribute, _LineNumber, file, {FileName, _LineNumber1}}, Deps) -> + case sets:is_element(FileName, Headers) of + true -> sets:add_element(FileName, Deps); + false -> Deps + end; + (_Form, Deps) -> + Deps + end, sets:new(), Forms). diff --git a/rabbitmq-server/include/gm_specs.hrl b/rabbitmq-server/include/gm_specs.hrl new file mode 100644 index 0000000..bc20b44 --- /dev/null +++ b/rabbitmq-server/include/gm_specs.hrl @@ -0,0 +1,28 @@ +%% The contents of this file are subject to the Mozilla Public License +%% Version 1.1 (the "License"); you may not use this file except in +%% compliance with the License. You may obtain a copy of the License at +%% http://www.mozilla.org/MPL/ +%% +%% Software distributed under the License is distributed on an "AS IS" +%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the +%% License for the specific language governing rights and limitations +%% under the License. +%% +%% The Original Code is RabbitMQ. +%% +%% The Initial Developer of the Original Code is Pivotal Software, Inc. +%% Copyright (c) 2007-2015 Pivotal Software, Inc. All rights reserved. +%% + +-ifdef(use_specs). + +-type(callback_result() :: 'ok' | {'stop', any()} | {'become', atom(), args()}). +-type(args() :: any()). +-type(members() :: [pid()]). + +-spec(joined/2 :: (args(), members()) -> callback_result()). +-spec(members_changed/3 :: (args(), members(),members()) -> callback_result()). +-spec(handle_msg/3 :: (args(), pid(), any()) -> callback_result()). +-spec(handle_terminate/2 :: (args(), term()) -> any()). + +-endif. diff --git a/rabbitmq-server/include/rabbit.hrl b/rabbitmq-server/include/rabbit.hrl new file mode 100644 index 0000000..5b90956 --- /dev/null +++ b/rabbitmq-server/include/rabbit.hrl @@ -0,0 +1,152 @@ +%% The contents of this file are subject to the Mozilla Public License +%% Version 1.1 (the "License"); you may not use this file except in +%% compliance with the License. You may obtain a copy of the License +%% at http://www.mozilla.org/MPL/ +%% +%% Software distributed under the License is distributed on an "AS IS" +%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See +%% the License for the specific language governing rights and +%% limitations under the License. +%% +%% The Original Code is RabbitMQ. +%% +%% The Initial Developer of the Original Code is Pivotal Software, Inc. +%% Copyright (c) 2007-2015 Pivotal Software, Inc. All rights reserved. +%% + +%% Passed around most places +-record(user, {username, + tags, + authz_backends}). %% List of {Module, AuthUserImpl} pairs + +%% Passed to auth backends +-record(auth_user, {username, + tags, + impl}). + +%% Implementation for the internal auth backend +-record(internal_user, {username, password_hash, tags}). +-record(permission, {configure, write, read}). +-record(user_vhost, {username, virtual_host}). +-record(user_permission, {user_vhost, permission}). + +-record(vhost, {virtual_host, dummy}). + +-record(content, + {class_id, + properties, %% either 'none', or a decoded record/tuple + properties_bin, %% either 'none', or an encoded properties binary + %% Note: at most one of properties and properties_bin can be + %% 'none' at once. + protocol, %% The protocol under which properties_bin was encoded + payload_fragments_rev %% list of binaries, in reverse order (!) + }). + +-record(resource, {virtual_host, kind, name}). + +%% fields described as 'transient' here are cleared when writing to +%% rabbit_durable_ +-record(exchange, { + name, type, durable, auto_delete, internal, arguments, %% immutable + scratches, %% durable, explicitly updated via update_scratch/3 + policy, %% durable, implicitly updated when policy changes + decorators}). %% transient, recalculated in store/1 (i.e. recovery) + +-record(amqqueue, { + name, durable, auto_delete, exclusive_owner = none, %% immutable + arguments, %% immutable + pid, %% durable (just so we know home node) + slave_pids, sync_slave_pids, %% transient + recoverable_slaves, %% durable + policy, %% durable, implicit update as above + gm_pids, %% transient + decorators, %% transient, recalculated as above + state}). %% durable (have we crashed?) + +-record(exchange_serial, {name, next}). + +%% mnesia doesn't like unary records, so we add a dummy 'value' field +-record(route, {binding, value = const}). +-record(reverse_route, {reverse_binding, value = const}). + +-record(binding, {source, key, destination, args = []}). +-record(reverse_binding, {destination, key, source, args = []}). + +-record(topic_trie_node, {trie_node, edge_count, binding_count}). +-record(topic_trie_edge, {trie_edge, node_id}). +-record(topic_trie_binding, {trie_binding, value = const}). + +-record(trie_node, {exchange_name, node_id}). +-record(trie_edge, {exchange_name, node_id, word}). +-record(trie_binding, {exchange_name, node_id, destination, arguments}). + +-record(listener, {node, protocol, host, ip_address, port}). + +-record(runtime_parameters, {key, value}). + +-record(basic_message, {exchange_name, routing_keys = [], content, id, + is_persistent}). + +-record(ssl_socket, {tcp, ssl}). +-record(delivery, {mandatory, confirm, sender, message, msg_seq_no, flow}). +-record(amqp_error, {name, explanation = "", method = none}). + +-record(event, {type, props, reference = undefined, timestamp}). + +-record(message_properties, {expiry, needs_confirming = false, size}). + +-record(plugin, {name, %% atom() + version, %% string() + description, %% string() + type, %% 'ez' or 'dir' + dependencies, %% [{atom(), string()}] + location}). %% string() + +%%---------------------------------------------------------------------------- + +-define(COPYRIGHT_MESSAGE, "Copyright (C) 2007-2015 Pivotal Software, Inc."). +-define(INFORMATION_MESSAGE, "Licensed under the MPL. See http://www.rabbitmq.com/"). +-define(ERTS_MINIMUM, "5.6.3"). + +%% EMPTY_FRAME_SIZE, 8 = 1 + 2 + 4 + 1 +%% - 1 byte of frame type +%% - 2 bytes of channel number +%% - 4 bytes of frame payload length +%% - 1 byte of payload trailer FRAME_END byte +%% See rabbit_binary_generator:check_empty_frame_size/0, an assertion +%% called at startup. +-define(EMPTY_FRAME_SIZE, 8). + +-define(MAX_WAIT, 16#ffffffff). + +-define(HIBERNATE_AFTER_MIN, 1000). +-define(DESIRED_HIBERNATE, 10000). +-define(CREDIT_DISC_BOUND, {2000, 500}). +%% When we discover that we should write some indices to disk for some +%% betas, the IO_BATCH_SIZE sets the number of betas that we must be +%% due to write indices for before we do any work at all. +-define(IO_BATCH_SIZE, 2048). %% next power-of-2 after ?CREDIT_DISC_BOUND + +-define(INVALID_HEADERS_KEY, <<"x-invalid-headers">>). +-define(ROUTING_HEADERS, [<<"CC">>, <<"BCC">>]). +-define(DELETED_HEADER, <<"BCC">>). + +%% Trying to send a term across a cluster larger than 2^31 bytes will +%% cause the VM to exit with "Absurdly large distribution output data +%% buffer". So we limit the max message size to 2^31 - 10^6 bytes (1MB +%% to allow plenty of leeway for the #basic_message{} and #content{} +%% wrapping the message body). +-define(MAX_MSG_SIZE, 2147383648). + +%% First number is maximum size in bytes before we start to +%% truncate. The following 4-tuple is: +%% +%% 1) Maximum size of printable lists and binaries. +%% 2) Maximum size of any structural term. +%% 3) Amount to decrease 1) every time we descend while truncating. +%% 4) Amount to decrease 2) every time we descend while truncating. +%% +%% Whole thing feeds into truncate:log_event/2. +-define(LOG_TRUNC, {100000, {2000, 100, 50, 5}}). + +-define(store_proc_name(N), rabbit_misc:store_proc_name(?MODULE, N)). diff --git a/rabbitmq-server/include/rabbit_cli.hrl b/rabbitmq-server/include/rabbit_cli.hrl new file mode 100644 index 0000000..737bb4e --- /dev/null +++ b/rabbitmq-server/include/rabbit_cli.hrl @@ -0,0 +1,50 @@ +%% The contents of this file are subject to the Mozilla Public License +%% Version 1.1 (the "License"); you may not use this file except in +%% compliance with the License. You may obtain a copy of the License +%% at http://www.mozilla.org/MPL/ +%% +%% Software distributed under the License is distributed on an "AS IS" +%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See +%% the License for the specific language governing rights and +%% limitations under the License. +%% +%% The Original Code is RabbitMQ. +%% +%% The Initial Developer of the Original Code is Pivotal Software, Inc. +%% Copyright (c) 2007-2015 Pivotal Software, Inc. All rights reserved. +%% + +-define(NODE_OPT, "-n"). +-define(QUIET_OPT, "-q"). +-define(VHOST_OPT, "-p"). +-define(TIMEOUT_OPT, "-t"). + +-define(VERBOSE_OPT, "-v"). +-define(MINIMAL_OPT, "-m"). +-define(ENABLED_OPT, "-E"). +-define(ENABLED_ALL_OPT, "-e"). + +-define(PRIORITY_OPT, "--priority"). +-define(APPLY_TO_OPT, "--apply-to"). +-define(RAM_OPT, "--ram"). +-define(OFFLINE_OPT, "--offline"). +-define(ONLINE_OPT, "--online"). + + +-define(NODE_DEF(Node), {?NODE_OPT, {option, Node}}). +-define(QUIET_DEF, {?QUIET_OPT, flag}). +-define(VHOST_DEF, {?VHOST_OPT, {option, "/"}}). +-define(TIMEOUT_DEF, {?TIMEOUT_OPT, {option, "infinity"}}). + +-define(VERBOSE_DEF, {?VERBOSE_OPT, flag}). +-define(MINIMAL_DEF, {?MINIMAL_OPT, flag}). +-define(ENABLED_DEF, {?ENABLED_OPT, flag}). +-define(ENABLED_ALL_DEF, {?ENABLED_ALL_OPT, flag}). + +-define(PRIORITY_DEF, {?PRIORITY_OPT, {option, "0"}}). +-define(APPLY_TO_DEF, {?APPLY_TO_OPT, {option, "all"}}). +-define(RAM_DEF, {?RAM_OPT, flag}). +-define(OFFLINE_DEF, {?OFFLINE_OPT, flag}). +-define(ONLINE_DEF, {?ONLINE_OPT, flag}). + +-define(RPC_TIMEOUT, infinity). diff --git a/rabbitmq-server/include/rabbit_msg_store.hrl b/rabbitmq-server/include/rabbit_msg_store.hrl new file mode 100644 index 0000000..8bcf2ce --- /dev/null +++ b/rabbitmq-server/include/rabbit_msg_store.hrl @@ -0,0 +1,25 @@ +%% The contents of this file are subject to the Mozilla Public License +%% Version 1.1 (the "License"); you may not use this file except in +%% compliance with the License. You may obtain a copy of the License +%% at http://www.mozilla.org/MPL/ +%% +%% Software distributed under the License is distributed on an "AS IS" +%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See +%% the License for the specific language governing rights and +%% limitations under the License. +%% +%% The Original Code is RabbitMQ. +%% +%% The Initial Developer of the Original Code is Pivotal Software, Inc. +%% Copyright (c) 2007-2015 Pivotal Software, Inc. All rights reserved. +%% + +-include("rabbit.hrl"). + +-ifdef(use_specs). + +-type(msg() :: any()). + +-endif. + +-record(msg_location, {msg_id, ref_count, file, offset, total_size}). diff --git a/rabbitmq-server/plugins-src/Makefile b/rabbitmq-server/plugins-src/Makefile new file mode 100644 index 0000000..4ab8c86 --- /dev/null +++ b/rabbitmq-server/plugins-src/Makefile @@ -0,0 +1,240 @@ +.PHONY: default +default: + @echo No default target && false + +REPOS:= \ + rabbitmq-server \ + rabbitmq-codegen \ + rabbitmq-java-client \ + rabbitmq-dotnet-client \ + rabbitmq-test \ + cowboy-wrapper \ + eldap-wrapper \ + mochiweb-wrapper \ + rabbitmq-amqp1.0 \ + rabbitmq-auth-backend-ldap \ + rabbitmq-auth-mechanism-ssl \ + rabbitmq-consistent-hash-exchange \ + rabbitmq-erlang-client \ + rabbitmq-federation \ + rabbitmq-federation-management \ + rabbitmq-management \ + rabbitmq-management-agent \ + rabbitmq-management-visualiser \ + rabbitmq-metronome \ + rabbitmq-web-dispatch \ + rabbitmq-mqtt \ + rabbitmq-shovel \ + rabbitmq-shovel-management \ + rabbitmq-stomp \ + rabbitmq-toke \ + rabbitmq-tracing \ + rabbitmq-web-stomp \ + rabbitmq-web-stomp-examples \ + sockjs-erlang-wrapper \ + toke \ + webmachine-wrapper + +BRANCH:=master + +UMBRELLA_REPO_FETCH:=$(shell git remote -v 2>/dev/null | awk '/^origin\t.+ \(fetch\)$$/ { print $$2; }') +ifdef UMBRELLA_REPO_FETCH +GIT_CORE_REPOBASE_FETCH:=$(shell dirname $(UMBRELLA_REPO_FETCH)) +GIT_CORE_SUFFIX_FETCH:=$(suffix $(UMBRELLA_REPO_FETCH)) +else +GIT_CORE_REPOBASE_FETCH:=https://github.com/rabbitmq +GIT_CORE_SUFFIX_FETCH:=.git +endif + +UMBRELLA_REPO_PUSH:=$(shell git remote -v 2>/dev/null | awk '/^origin\t.+ \(push\)$$/ { print $$2; }') +ifdef UMBRELLA_REPO_PUSH +GIT_CORE_REPOBASE_PUSH:=$(shell dirname $(UMBRELLA_REPO_PUSH)) +GIT_CORE_SUFFIX_PUSH:=$(suffix $(UMBRELLA_REPO_PUSH)) +else +GIT_CORE_REPOBASE_PUSH:=git@github.com:rabbitmq +GIT_CORE_SUFFIX_PUSH:=.git +endif + +VERSION:=0.0.0 + +ifndef VERBOSE +QUIET:=@ +endif + +#---------------------------------- + +all: + $(MAKE) -f all-packages.mk all-packages VERSION=$(VERSION) + +test: + $(MAKE) -f all-packages.mk test-all-packages VERSION=$(VERSION) + +release: + $(MAKE) -f all-packages.mk all-releasable VERSION=$(VERSION) + +clean: + $(MAKE) -f all-packages.mk clean-all-packages + +check-xref: + $(MAKE) -f all-packages.mk check-xref-packages + +plugins-dist: release + rm -rf $(PLUGINS_DIST_DIR) + mkdir -p $(PLUGINS_DIST_DIR) + $(MAKE) -f all-packages.mk copy-releasable VERSION=$(VERSION) PLUGINS_DIST_DIR=$(PLUGINS_DIST_DIR) + +plugins-srcdist: + rm -rf $(PLUGINS_SRC_DIST_DIR) + mkdir -p $(PLUGINS_SRC_DIST_DIR)/licensing + + rsync -a --exclude '.git*' rabbitmq-erlang-client $(PLUGINS_SRC_DIST_DIR)/ + touch $(PLUGINS_SRC_DIST_DIR)/rabbitmq-erlang-client/.srcdist_done + + rsync -a --exclude '.git*' rabbitmq-server $(PLUGINS_SRC_DIST_DIR)/ + touch $(PLUGINS_SRC_DIST_DIR)/rabbitmq-server/.srcdist_done + + $(MAKE) -f all-packages.mk copy-srcdist VERSION=$(VERSION) PLUGINS_SRC_DIST_DIR=$(PLUGINS_SRC_DIST_DIR) + cp Makefile *.mk generate* $(PLUGINS_SRC_DIST_DIR)/ + echo "This is the released version of rabbitmq-public-umbrella. \ +You can clone the full version with: git clone https://github.com/rabbitmq/rabbitmq-public-umbrella.git" > $(PLUGINS_SRC_DIST_DIR)/README + + PRESERVE_CLONE_DIR=1 $(MAKE) -C $(PLUGINS_SRC_DIST_DIR) clean + rm -rf $(PLUGINS_SRC_DIST_DIR)/rabbitmq-server + +#---------------------------------- +# Convenience aliases + +.PHONY: co +co: checkout + +.PHONY: ci +ci: checkin + +.PHONY: up +up: update + +.PHONY: st +st: status + +.PHONY: up_c +up_c: named_update + +#---------------------------------- + +$(REPOS): + $(QUIET)retries=5; \ + umbrella_branch="$$(git branch | awk '/^\* / { print $$2; }')"; \ + if test "$$umbrella_branch" = "stable"; then \ + branch_arg="-b $$umbrella_branch"; \ + fi; \ + while ! git clone $$branch_arg $(GIT_CORE_REPOBASE_FETCH)/$@$(GIT_CORE_SUFFIX_FETCH); do \ + retries=$$((retries - 1)); \ + if test "$$retries" = 0; then break; fi; \ + sleep 1; \ + done + $(QUIET)test -d $@ + $(QUIET)global_user_name="$$(git config --global user.name)"; \ + global_user_email="$$(git config --global user.email)"; \ + user_name="$$(git config user.name)"; \ + user_email="$$(git config user.email)"; \ + cd $@ && \ + git remote set-url --push origin $(GIT_CORE_REPOBASE_PUSH)/$@$(GIT_CORE_SUFFIX_PUSH) && \ + if test "$$global_user_name" != "$$user_name"; then git config user.name "$$user_name"; fi && \ + if test "$$global_user_email" != "$$user_email"; then git config user.email "$$user_email"; fi + + +.PHONY: checkout +checkout: $(REPOS) + +.PHONY: list-repos +list-repos: + @for repo in $(REPOS); do echo $$repo; done + +.PHONY: sync-gituser +sync-gituser: + @global_user_name="$$(git config --global user.name)"; \ + global_user_email="$$(git config --global user.email)"; \ + user_name="$$(git config user.name)"; \ + user_email="$$(git config user.email)"; \ + for repo in $(REPOS); do \ + cd $$repo && \ + git config --unset user.name && \ + git config --unset user.email && \ + if test "$$global_user_name" != "$$user_name"; then git config user.name "$$user_name"; fi && \ + if test "$$global_user_email" != "$$user_email"; then git config user.email "$$user_email"; fi && \ + cd ..; done + +.PHONY: sync-gitremote +sync-gitremote: + @for repo in $(REPOS); do \ + cd $$repo && \ + git remote set-url origin $(GIT_CORE_REPOBASE_FETCH)/$$repo$(GIT_CORE_SUFFIX_FETCH) && \ + git remote set-url --push origin $(GIT_CORE_REPOBASE_PUSH)/$$repo$(GIT_CORE_SUFFIX_PUSH) && \ + cd ..; done + +#---------------------------------- +# Subrepository management + + +# $(1) is the target +# $(2) is the target dependency. Can use % to get current REPO +# $(3) is the target body. Can use % to get current REPO +define repo_target + +.PHONY: $(1) +$(1): $(2) + $(3) + +endef + +# $(1) is the list of repos +# $(2) is the suffix +# $(3) is the target dependency. Can use % to get current REPO +# $(4) is the target body. Can use % to get current REPO +define repo_targets +$(foreach REPO,$(1),$(call repo_target,$(REPO)+$(2),\ + $(patsubst %,$(3),$(REPO)),$(patsubst %,$(4),$(REPO)))) +endef + +# Do not allow status to fork with -j otherwise output will be garbled +.PHONY: status +status: checkout + @for repo in . $(REPOS); do \ + echo "$$repo:"; \ + cd "$$repo" && git status -s && cd - >/dev/null; \ + done + +.PHONY: pull +pull: $(foreach DIR,. $(REPOS),$(DIR)+pull) + +$(eval $(call repo_targets,. $(REPOS),pull,| %,\ + (cd % && git fetch -p && \ + (! git symbolic-ref -q HEAD || git pull --ff-only)))) + +.PHONY: update +update: pull + +.PHONY: named_update +named_update: $(foreach DIR,. $(REPOS),$(DIR)+named_update) + +$(eval $(call repo_targets,. $(REPOS),named_update,| %,\ + (cd % && git fetch -p && git checkout $(BRANCH) && \ + (! git symbolic-ref -q HEAD || git pull --ff-only)))) + +.PHONY: tag +tag: $(foreach DIR,. $(REPOS),$(DIR)+tag) + +$(eval $(call repo_targets,. $(REPOS),tag,| %,\ + (cd % && git tag $(TAG)))) + +.PHONY: push +push: $(foreach DIR,. $(REPOS),$(DIR)+push) + +$(eval $(call repo_targets,. $(REPOS),push,| %,\ + (cd % && git push && git push --tags))) + +.PHONY: checkin +checkin: $(foreach DIR,. $(REPOS),$(DIR)+checkin) + +$(eval $(call repo_targets,. $(REPOS),checkin,| %,\ + (cd % && (test -z "$$$$(git status -s -uno)" || git commit -a)))) diff --git a/rabbitmq-server/plugins-src/README b/rabbitmq-server/plugins-src/README new file mode 100644 index 0000000..58177d4 --- /dev/null +++ b/rabbitmq-server/plugins-src/README @@ -0,0 +1 @@ +This is the released version of rabbitmq-public-umbrella. You can clone the full version with: git clone https://github.com/rabbitmq/rabbitmq-public-umbrella.git diff --git a/rabbitmq-server/plugins-src/all-packages.mk b/rabbitmq-server/plugins-src/all-packages.mk new file mode 100644 index 0000000..1d02a3d --- /dev/null +++ b/rabbitmq-server/plugins-src/all-packages.mk @@ -0,0 +1,13 @@ +UMBRELLA_BASE_DIR:=. + +include common.mk + +CHAIN_TESTS:=true + +# Pull in all the packages +$(foreach PACKAGE_MK,$(wildcard */package.mk),$(eval $(call do_package,$(call canonical_path,$(patsubst %/,%,$(dir $(PACKAGE_MK))))))) + +# ...and the non-integrated ones +$(foreach V,$(.VARIABLES),$(if $(filter NON_INTEGRATED_%,$(filter-out NON_INTEGRATED_DEPS_%,$V)),$(eval $(call do_package,$(subst NON_INTEGRATED_,,$V))))) + +test-all-packages: $(CHAINED_TESTS) diff --git a/rabbitmq-server/plugins-src/common.mk b/rabbitmq-server/plugins-src/common.mk new file mode 100644 index 0000000..d8ed4f8 --- /dev/null +++ b/rabbitmq-server/plugins-src/common.mk @@ -0,0 +1,143 @@ +# Various global definitions + +# UMBRELLA_BASE_DIR should be set to the path of the +# rabbitmq-public-umbrella directory before this file is included. + +# Make version check +REQUIRED_MAKE_VERSION:=3.81 +ifneq ($(shell ( echo "$(MAKE_VERSION)" ; echo "$(REQUIRED_MAKE_VERSION)" ) | sort -t. -n | head -1),$(REQUIRED_MAKE_VERSION)) +$(error GNU make version $(REQUIRED_MAKE_VERSION) required) +endif + +# This is the standard trick for making pattern substitution work +# (amongst others) when the replacement needs to include a comma. +COMMA:=, + +# Global settings that can be overridden on the command line + +# These ones are expected to be passed down to the sub-makes invoked +# for non-integrated packages +VERSION ?= 0.0.0 +ERL ?= erl +ERL_OPTS ?= +ERLC ?= erlc +ERLC_OPTS ?= -Wall +debug_info +TMPDIR ?= /tmp + +NODENAME ?= rabbit-test +ERL_CALL ?= erl_call +ERL_CALL_OPTS ?= -sname $(NODENAME) -e + +# Where we put all the files produced when running tests. +TEST_TMPDIR=$(TMPDIR)/rabbitmq-test + +# Callable functions + +# Convert a package name to the corresponding erlang app name +define package_to_app_name +$(subst -,_,$(1)) +endef + +# If the variable named $(1) holds a non-empty value, return it. +# Otherwise, set the variable to $(2) and return that value. +define memoize +$(if $($(1)),$($(1)),$(eval $(1):=$(2))$(2)) +endef + +# Return a canonical form for the path in $(1) +# +# Absolute path names can be a bit verbose. This provides a way to +# canonicalize path names with more concise results. +define canonical_path +$(call memoize,SHORT_$(realpath $(1)),$(1)) +endef + +# Convert a package name to a path name +define package_to_path +$(call canonical_path,$(UMBRELLA_BASE_DIR)/$(1)) +endef + +# Produce a cp command to copy from $(1) to $(2), unless $(1) is +# empty, in which case do nothing. +# +# The optional $(3) gives a suffix to append to the command, if a +# command is produced. +define copy +$(if $(1),cp -r $(1) $(2)$(if $(3), $(3))) +endef + +# Produce the makefile fragment for the package with path in $(1), if +# it hasn't already been visited. The path should have been +# canonicalized via canonical_path. +define do_package +# Have we already visited this package? If so, skip it +ifndef DONE_$(1) +PACKAGE_DIR:=$(1) +include $(UMBRELLA_BASE_DIR)/do-package.mk +endif +endef + +# This is used to chain test rules, so that test-all-packages works in +# the presence of 'make -j' +define chain_test +$(if $(CHAIN_TESTS),$(CHAINED_TESTS)$(eval CHAINED_TESTS+=$(1))) +endef + +# Mark the non-integrated repos +NON_INTEGRATED_$(call package_to_path,rabbitmq-server):=true +NON_INTEGRATED_$(call package_to_path,rabbitmq-erlang-client):=true +NON_INTEGRATED_$(call package_to_path,rabbitmq-java-client):=true +NON_INTEGRATED_$(call package_to_path,rabbitmq-dotnet-client):=true +NON_INTEGRATED_DEPS_$(call package_to_path,rabbitmq-erlang-client):=rabbitmq-server + +# Where the coverage package lives +COVERAGE_PATH:=$(call package_to_path,coverage) + +# Where the rabbitmq-server package lives +RABBITMQ_SERVER_PATH=$(call package_to_path,rabbitmq-server) + +# Cleaning support +ifndef MAKECMDGOALS +TESTABLEGOALS:=$(.DEFAULT_GOAL) +else +TESTABLEGOALS:=$(MAKECMDGOALS) +endif + +# The CLEANING variable can be used to determine whether the top-level +# goal is cleaning related. In particular, it can be used to prevent +# including generated files when cleaning, which might otherwise +# trigger undesirable activity. +ifeq "$(strip $(patsubst clean%,,$(patsubst %clean,,$(TESTABLEGOALS))))" "" +CLEANING:=true +endif + +# Include a generated makefile fragment +# +# Note that this includes using "-include", and thus make will proceed +# even if an error occurs while the fragment is being re-made (we +# don't use "include" becuase it will produce a superfluous error +# message when the fragment is re-made because it doesn't exist). +# Thus you should also list the fragment as a dependency of any rules +# that will refer to the contents of the fragment. +define safe_include +ifndef CLEANING +-include $(1) + +# If we fail to make the fragment, make will just loop trying to +# create it. So we have to explicitly catch that case. +$$(if $$(MAKE_RESTARTS),$$(if $$(wildcard $(1)),,$$(error Failed to produce $(1)))) + +endif +endef + +# This is not the make default, but it is a good idea +.DELETE_ON_ERROR: + +# Declarations for global targets +.PHONY: all-releasable copy-releasable copy-srcdist all-packages clean-all-packages +all-releasable:: +copy-releasable:: +copy-srcdist:: +all-packages:: +clean-all-packages:: +check-xref-packages:: diff --git a/rabbitmq-server/plugins-src/cowboy-wrapper/.srcdist_done b/rabbitmq-server/plugins-src/cowboy-wrapper/.srcdist_done new file mode 100644 index 0000000..e69de29 diff --git a/rabbitmq-server/plugins-src/cowboy-wrapper/0001-R12-fake-iodata-type.patch b/rabbitmq-server/plugins-src/cowboy-wrapper/0001-R12-fake-iodata-type.patch new file mode 100644 index 0000000..f1d8e6a --- /dev/null +++ b/rabbitmq-server/plugins-src/cowboy-wrapper/0001-R12-fake-iodata-type.patch @@ -0,0 +1,40 @@ +From c2303fb756eeb8bd92dc04764970a43f59940208 Mon Sep 17 00:00:00 2001 +From: Marek Majkowski +Date: Thu, 26 Jan 2012 12:48:41 +0000 +Subject: [PATCH 1/7] R12 - Fake iodata() type + +--- + include/http.hrl | 2 +- + src/cowboy_http.erl | 3 ++- + 2 files changed, 3 insertions(+), 2 deletions(-) + +diff --git a/include/http.hrl b/include/http.hrl +index c66f2b0..c98f873 100644 +--- a/include/http.hrl ++++ b/include/http.hrl +@@ -47,7 +47,7 @@ + %% Response. + resp_state = waiting :: locked | waiting | chunks | done, + resp_headers = [] :: cowboy_http:headers(), +- resp_body = <<>> :: iodata() | {non_neg_integer(), ++ resp_body = <<>> :: cowboy_http:fake_iodata() | {non_neg_integer(), + fun(() -> {sent, non_neg_integer()})}, + + %% Functions. +diff --git a/src/cowboy_http.erl b/src/cowboy_http.erl +index 32b0ca9..95a7334 100644 +--- a/src/cowboy_http.erl ++++ b/src/cowboy_http.erl +@@ -46,7 +46,8 @@ + | 'Expires' | 'Last-Modified' | 'Accept-Ranges' | 'Set-Cookie' + | 'Set-Cookie2' | 'X-Forwarded-For' | 'Cookie' | 'Keep-Alive' + | 'Proxy-Connection' | binary(). +--type headers() :: [{header(), iodata()}]. ++-type fake_iodata() :: iolist() | binary(). ++-type headers() :: [{header(), fake_iodata()}]. + -type status() :: non_neg_integer() | binary(). + + -export_type([method/0, uri/0, version/0, header/0, headers/0, status/0]). +-- +1.7.0.4 + diff --git a/rabbitmq-server/plugins-src/cowboy-wrapper/0002-R12-drop-all-references-to-boolean-type.patch b/rabbitmq-server/plugins-src/cowboy-wrapper/0002-R12-drop-all-references-to-boolean-type.patch new file mode 100644 index 0000000..aaeedd6 --- /dev/null +++ b/rabbitmq-server/plugins-src/cowboy-wrapper/0002-R12-drop-all-references-to-boolean-type.patch @@ -0,0 +1,165 @@ +From 257e64326ad786d19328d343da0ff7d29adbae4e Mon Sep 17 00:00:00 2001 +From: Marek Majkowski +Date: Thu, 26 Jan 2012 12:51:30 +0000 +Subject: [PATCH 2/7] R12 - drop all references to boolean() type + +--- + src/cowboy_cookies.erl | 8 -------- + src/cowboy_http.erl | 1 - + src/cowboy_http_protocol.erl | 3 +-- + src/cowboy_http_req.erl | 2 -- + src/cowboy_http_static.erl | 5 ----- + src/cowboy_http_websocket.erl | 2 +- + 6 files changed, 2 insertions(+), 19 deletions(-) + +diff --git a/src/cowboy_cookies.erl b/src/cowboy_cookies.erl +index 6818a86..7f5ab60 100644 +--- a/src/cowboy_cookies.erl ++++ b/src/cowboy_cookies.erl +@@ -112,7 +112,6 @@ cookie(Key, Value, Options) when is_binary(Key) + %% Internal. + + %% @doc Check if a character is a white space character. +--spec is_whitespace(char()) -> boolean(). + is_whitespace($\s) -> true; + is_whitespace($\t) -> true; + is_whitespace($\r) -> true; +@@ -120,7 +119,6 @@ is_whitespace($\n) -> true; + is_whitespace(_) -> false. + + %% @doc Check if a character is a seperator. +--spec is_separator(char()) -> boolean(). + is_separator(C) when C < 32 -> true; + is_separator($\s) -> true; + is_separator($\t) -> true; +@@ -144,7 +142,6 @@ is_separator($}) -> true; + is_separator(_) -> false. + + %% @doc Check if a binary has an ASCII seperator character. +--spec has_seperator(binary()) -> boolean(). + has_seperator(<<>>) -> + false; + has_seperator(<<$/, Rest/binary>>) -> +@@ -228,7 +225,6 @@ read_quoted(<>, Acc) -> + read_quoted(Rest, <>). + + %% @doc Drop characters while a function returns true. +--spec binary_dropwhile(fun((char()) -> boolean()), binary()) -> binary(). + binary_dropwhile(_F, <<"">>) -> + <<"">>; + binary_dropwhile(F, String) -> +@@ -246,8 +242,6 @@ skip_whitespace(String) -> + binary_dropwhile(fun is_whitespace/1, String). + + %% @doc Split a binary when the current character causes F to return true. +--spec binary_splitwith(fun((char()) -> boolean()), binary(), binary()) +- -> {binary(), binary()}. + binary_splitwith(_F, Head, <<>>) -> + {Head, <<>>}; + binary_splitwith(F, Head, Tail) -> +@@ -260,8 +254,6 @@ binary_splitwith(F, Head, Tail) -> + end. + + %% @doc Split a binary with a function returning true or false on each char. +--spec binary_splitwith(fun((char()) -> boolean()), binary()) +- -> {binary(), binary()}. + binary_splitwith(F, String) -> + binary_splitwith(F, <<>>, String). + +diff --git a/src/cowboy_http.erl b/src/cowboy_http.erl +index 95a7334..d7261c8 100644 +--- a/src/cowboy_http.erl ++++ b/src/cowboy_http.erl +@@ -755,7 +755,6 @@ urlencode(Bin, Opts) -> + Upper = proplists:get_value(upper, Opts, false), + urlencode(Bin, <<>>, Plus, Upper). + +--spec urlencode(binary(), binary(), boolean(), boolean()) -> binary(). + urlencode(<>, Acc, P=Plus, U=Upper) -> + if C >= $0, C =< $9 -> urlencode(Rest, <>, P, U); + C >= $A, C =< $Z -> urlencode(Rest, <>, P, U); +diff --git a/src/cowboy_http_protocol.erl b/src/cowboy_http_protocol.erl +index baee081..b80745f 100644 +--- a/src/cowboy_http_protocol.erl ++++ b/src/cowboy_http_protocol.erl +@@ -55,7 +55,7 @@ + max_line_length :: integer(), + timeout :: timeout(), + buffer = <<>> :: binary(), +- hibernate = false :: boolean(), ++ hibernate = false, + loop_timeout = infinity :: timeout(), + loop_timeout_ref :: undefined | reference() + }). +@@ -440,7 +440,6 @@ format_header(Field) when byte_size(Field) =< 20; byte_size(Field) > 32 -> + format_header(Field) -> + format_header(Field, true, <<>>). + +--spec format_header(binary(), boolean(), binary()) -> binary(). + format_header(<<>>, _Any, Acc) -> + Acc; + %% Replicate a bug in OTP for compatibility reasons when there's a - right +diff --git a/src/cowboy_http_req.erl b/src/cowboy_http_req.erl +index 92d96ad..d729d6c 100644 +--- a/src/cowboy_http_req.erl ++++ b/src/cowboy_http_req.erl +@@ -515,13 +515,11 @@ set_resp_body_fun(StreamLen, StreamFun, Req) -> + + + %% @doc Return whether the given header has been set for the response. +--spec has_resp_header(cowboy_http:header(), #http_req{}) -> boolean(). + has_resp_header(Name, #http_req{resp_headers=RespHeaders}) -> + NameBin = header_to_binary(Name), + lists:keymember(NameBin, 1, RespHeaders). + + %% @doc Return whether a body has been set for the response. +--spec has_resp_body(#http_req{}) -> boolean(). + has_resp_body(#http_req{resp_body={Length, _}}) -> + Length > 0; + has_resp_body(#http_req{resp_body=RespBody}) -> +diff --git a/src/cowboy_http_static.erl b/src/cowboy_http_static.erl +index 0ee996a..d370046 100644 +--- a/src/cowboy_http_static.erl ++++ b/src/cowboy_http_static.erl +@@ -207,8 +207,6 @@ allowed_methods(Req, State) -> + {['GET', 'HEAD'], Req, State}. + + %% @private +--spec malformed_request(#http_req{}, #state{}) -> +- {boolean(), #http_req{}, #state{}}. + malformed_request(Req, #state{filepath=error}=State) -> + {true, Req, State}; + malformed_request(Req, State) -> +@@ -216,8 +214,6 @@ malformed_request(Req, State) -> + + + %% @private Check if the resource exists under the document root. +--spec resource_exists(#http_req{}, #state{}) -> +- {boolean(), #http_req{}, #state{}}. + resource_exists(Req, #state{fileinfo={error, _}}=State) -> + {false, Req, State}; + resource_exists(Req, #state{fileinfo={ok, Fileinfo}}=State) -> +@@ -227,7 +223,6 @@ resource_exists(Req, #state{fileinfo={ok, Fileinfo}}=State) -> + %% @private + %% Access to a file resource is forbidden if it exists and the local node does + %% not have permission to read it. Directory listings are always forbidden. +--spec forbidden(#http_req{}, #state{}) -> {boolean(), #http_req{}, #state{}}. + forbidden(Req, #state{fileinfo={_, #file_info{type=directory}}}=State) -> + {true, Req, State}; + forbidden(Req, #state{fileinfo={error, eacces}}=State) -> +diff --git a/src/cowboy_http_websocket.erl b/src/cowboy_http_websocket.erl +index 0f0204c..5f59891 100644 +--- a/src/cowboy_http_websocket.erl ++++ b/src/cowboy_http_websocket.erl +@@ -54,7 +54,7 @@ + timeout = infinity :: timeout(), + timeout_ref = undefined :: undefined | reference(), + messages = undefined :: undefined | {atom(), atom(), atom()}, +- hibernate = false :: boolean(), ++ hibernate = false, + eop :: undefined | tuple(), %% hixie-76 specific. + origin = undefined :: undefined | binary() %% hixie-76 specific. + }). +-- +1.7.0.4 + diff --git a/rabbitmq-server/plugins-src/cowboy-wrapper/0003-R12-drop-all-references-to-reference-type.patch b/rabbitmq-server/plugins-src/cowboy-wrapper/0003-R12-drop-all-references-to-reference-type.patch new file mode 100644 index 0000000..e0ebae9 --- /dev/null +++ b/rabbitmq-server/plugins-src/cowboy-wrapper/0003-R12-drop-all-references-to-reference-type.patch @@ -0,0 +1,55 @@ +From 4db80ab7bacf04502ad2d29d4760e04a6d787a83 Mon Sep 17 00:00:00 2001 +From: Marek Majkowski +Date: Thu, 26 Jan 2012 12:52:23 +0000 +Subject: [PATCH 3/7] R12: drop all references to reference() type + +--- + src/cowboy_http_protocol.erl | 2 +- + src/cowboy_http_websocket.erl | 2 +- + src/cowboy_listener.erl | 2 +- + 3 files changed, 3 insertions(+), 3 deletions(-) + +diff --git a/src/cowboy_http_protocol.erl b/src/cowboy_http_protocol.erl +index b80745f..0183785 100644 +--- a/src/cowboy_http_protocol.erl ++++ b/src/cowboy_http_protocol.erl +@@ -57,7 +57,7 @@ + buffer = <<>> :: binary(), + hibernate = false, + loop_timeout = infinity :: timeout(), +- loop_timeout_ref :: undefined | reference() ++ loop_timeout_ref + }). + + %% API. +diff --git a/src/cowboy_http_websocket.erl b/src/cowboy_http_websocket.erl +index 5f59891..5100213 100644 +--- a/src/cowboy_http_websocket.erl ++++ b/src/cowboy_http_websocket.erl +@@ -52,7 +52,7 @@ + opts :: any(), + challenge = undefined :: undefined | binary() | {binary(), binary()}, + timeout = infinity :: timeout(), +- timeout_ref = undefined :: undefined | reference(), ++ timeout_ref = undefined, + messages = undefined :: undefined | {atom(), atom(), atom()}, + hibernate = false, + eop :: undefined | tuple(), %% hixie-76 specific. +diff --git a/src/cowboy_listener.erl b/src/cowboy_listener.erl +index c19d079..86e87f1 100644 +--- a/src/cowboy_listener.erl ++++ b/src/cowboy_listener.erl +@@ -23,8 +23,8 @@ + + -record(state, { + req_pools = [] :: [{atom(), non_neg_integer()}], +- reqs_table :: ets:tid(), +- queue = [] :: [{pid(), reference()}] ++ reqs_table, ++ queue = [] + }). + + %% API. +-- +1.7.0.4 + diff --git a/rabbitmq-server/plugins-src/cowboy-wrapper/0004-R12-drop-references-to-iodata-type.patch b/rabbitmq-server/plugins-src/cowboy-wrapper/0004-R12-drop-references-to-iodata-type.patch new file mode 100644 index 0000000..d6f097c --- /dev/null +++ b/rabbitmq-server/plugins-src/cowboy-wrapper/0004-R12-drop-references-to-iodata-type.patch @@ -0,0 +1,50 @@ +From dfb750f491208a8e30cab0fa701dd866d60734b8 Mon Sep 17 00:00:00 2001 +From: Marek Majkowski +Date: Thu, 26 Jan 2012 12:53:08 +0000 +Subject: [PATCH 4/7] R12: drop references to iodata() type + +--- + src/cowboy_http_req.erl | 6 ------ + 1 files changed, 0 insertions(+), 6 deletions(-) + +diff --git a/src/cowboy_http_req.erl b/src/cowboy_http_req.erl +index d729d6c..64e757c 100644 +--- a/src/cowboy_http_req.erl ++++ b/src/cowboy_http_req.erl +@@ -478,8 +478,6 @@ set_resp_cookie(Name, Value, Options, Req) -> + set_resp_header(HeaderName, HeaderValue, Req). + + %% @doc Add a header to the response. +--spec set_resp_header(cowboy_http:header(), iodata(), #http_req{}) +- -> {ok, #http_req{}}. + set_resp_header(Name, Value, Req=#http_req{resp_headers=RespHeaders}) -> + NameBin = header_to_binary(Name), + {ok, Req#http_req{resp_headers=[{NameBin, Value}|RespHeaders]}}. +@@ -489,7 +487,6 @@ set_resp_header(Name, Value, Req=#http_req{resp_headers=RespHeaders}) -> + %% The body set here is ignored if the response is later sent using + %% anything other than reply/2 or reply/3. The response body is expected + %% to be a binary or an iolist. +--spec set_resp_body(iodata(), #http_req{}) -> {ok, #http_req{}}. + set_resp_body(Body, Req) -> + {ok, Req#http_req{resp_body=Body}}. + +@@ -537,8 +534,6 @@ reply(Status, Headers, Req=#http_req{resp_body=Body}) -> + reply(Status, Headers, Body, Req). + + %% @doc Send a reply to the client. +--spec reply(cowboy_http:status(), cowboy_http:headers(), iodata(), #http_req{}) +- -> {ok, #http_req{}}. + reply(Status, Headers, Body, Req=#http_req{socket=Socket, + transport=Transport, connection=Connection, pid=ReqPid, + method=Method, resp_state=waiting, resp_headers=RespHeaders}) -> +@@ -586,7 +581,6 @@ chunked_reply(Status, Headers, Req=#http_req{socket=Socket, + %% @doc Send a chunk of data. + %% + %% A chunked reply must have been initiated before calling this function. +--spec chunk(iodata(), #http_req{}) -> ok | {error, atom()}. + chunk(_Data, #http_req{socket=_Socket, transport=_Transport, method='HEAD'}) -> + ok; + chunk(Data, #http_req{socket=Socket, transport=Transport, resp_state=chunks}) -> +-- +1.7.0.4 + diff --git a/rabbitmq-server/plugins-src/cowboy-wrapper/0005-R12-drop-references-to-Default-any-type.patch b/rabbitmq-server/plugins-src/cowboy-wrapper/0005-R12-drop-references-to-Default-any-type.patch new file mode 100644 index 0000000..5fc06fd --- /dev/null +++ b/rabbitmq-server/plugins-src/cowboy-wrapper/0005-R12-drop-references-to-Default-any-type.patch @@ -0,0 +1,52 @@ +From c7aef1d044a1e83fcd6be7a83b2c763c0366d4f8 Mon Sep 17 00:00:00 2001 +From: Marek Majkowski +Date: Thu, 26 Jan 2012 12:53:36 +0000 +Subject: [PATCH 5/7] R12: drop references to Default:any() type + +--- + src/cowboy_http_req.erl | 8 -------- + 1 files changed, 0 insertions(+), 8 deletions(-) + +diff --git a/src/cowboy_http_req.erl b/src/cowboy_http_req.erl +index 64e757c..c884f5a 100644 +--- a/src/cowboy_http_req.erl ++++ b/src/cowboy_http_req.erl +@@ -147,8 +147,6 @@ qs_val(Name, Req) when is_binary(Name) -> + + %% @doc Return the query string value for the given key, or a default if + %% missing. +--spec qs_val(binary(), #http_req{}, Default) +- -> {binary() | true | Default, #http_req{}} when Default::any(). + qs_val(Name, Req=#http_req{raw_qs=RawQs, qs_vals=undefined, + urldecode={URLDecFun, URLDecArg}}, Default) when is_binary(Name) -> + QsVals = parse_qs(RawQs, fun(Bin) -> URLDecFun(Bin, URLDecArg) end), +@@ -180,8 +178,6 @@ binding(Name, Req) when is_atom(Name) -> + + %% @doc Return the binding value for the given key obtained when matching + %% the host and path against the dispatch list, or a default if missing. +--spec binding(atom(), #http_req{}, Default) +- -> {binary() | Default, #http_req{}} when Default::any(). + binding(Name, Req, Default) when is_atom(Name) -> + case lists:keyfind(Name, 1, Req#http_req.bindings) of + {Name, Value} -> {Value, Req}; +@@ -200,8 +196,6 @@ header(Name, Req) when is_atom(Name) orelse is_binary(Name) -> + header(Name, Req, undefined). + + %% @doc Return the header value for the given key, or a default if missing. +--spec header(atom() | binary(), #http_req{}, Default) +- -> {binary() | Default, #http_req{}} when Default::any(). + header(Name, Req, Default) when is_atom(Name) orelse is_binary(Name) -> + case lists:keyfind(Name, 1, Req#http_req.headers) of + {Name, Value} -> {Value, Req}; +@@ -313,8 +307,6 @@ cookie(Name, Req) when is_binary(Name) -> + + %% @doc Return the cookie value for the given key, or a default if + %% missing. +--spec cookie(binary(), #http_req{}, Default) +- -> {binary() | true | Default, #http_req{}} when Default::any(). + cookie(Name, Req=#http_req{cookies=undefined}, Default) when is_binary(Name) -> + case header('Cookie', Req) of + {undefined, Req2} -> +-- +1.7.0.4 + diff --git a/rabbitmq-server/plugins-src/cowboy-wrapper/0006-Use-erlang-integer_to_list-and-lists-max-instead-of-.patch b/rabbitmq-server/plugins-src/cowboy-wrapper/0006-Use-erlang-integer_to_list-and-lists-max-instead-of-.patch new file mode 100644 index 0000000..183ebd2 --- /dev/null +++ b/rabbitmq-server/plugins-src/cowboy-wrapper/0006-Use-erlang-integer_to_list-and-lists-max-instead-of-.patch @@ -0,0 +1,62 @@ +From 81106c53b80f5d0fa441b893048bbdc6c9e2c4f0 Mon Sep 17 00:00:00 2001 +From: Marek Majkowski +Date: Thu, 26 Jan 2012 12:54:31 +0000 +Subject: [PATCH 6/7] Use erlang:integer_to_list and lists:max instead of bifs + +--- + src/cowboy_http_req.erl | 2 +- + src/cowboy_http_static.erl | 2 +- + src/cowboy_multipart.erl | 4 ++-- + 3 files changed, 4 insertions(+), 4 deletions(-) + +diff --git a/src/cowboy_http_req.erl b/src/cowboy_http_req.erl +index c884f5a..bf4ac7a 100644 +--- a/src/cowboy_http_req.erl ++++ b/src/cowboy_http_req.erl +@@ -576,7 +576,7 @@ chunked_reply(Status, Headers, Req=#http_req{socket=Socket, + chunk(_Data, #http_req{socket=_Socket, transport=_Transport, method='HEAD'}) -> + ok; + chunk(Data, #http_req{socket=Socket, transport=Transport, resp_state=chunks}) -> +- Transport:send(Socket, [integer_to_list(iolist_size(Data), 16), ++ Transport:send(Socket, [erlang:integer_to_list(iolist_size(Data), 16), + <<"\r\n">>, Data, <<"\r\n">>]). + + %% @doc Send an upgrade reply. +diff --git a/src/cowboy_http_static.erl b/src/cowboy_http_static.erl +index d370046..da3bd33 100644 +--- a/src/cowboy_http_static.erl ++++ b/src/cowboy_http_static.erl +@@ -412,7 +412,7 @@ attr_etag_function(Args, Attrs) -> + + -spec attr_etag_function([etagarg()], [fileattr()], [binary()]) -> binary(). + attr_etag_function(_Args, [], Acc) -> +- list_to_binary(integer_to_list(erlang:crc32(Acc), 16)); ++ list_to_binary(erlang:integer_to_list(erlang:crc32(Acc), 16)); + attr_etag_function(Args, [H|T], Acc) -> + {_, Value} = lists:keyfind(H, 1, Args), + attr_etag_function(Args, T, [term_to_binary(Value)|Acc]). +diff --git a/src/cowboy_multipart.erl b/src/cowboy_multipart.erl +index b7aeb54..c9b5b6c 100644 +--- a/src/cowboy_multipart.erl ++++ b/src/cowboy_multipart.erl +@@ -105,7 +105,7 @@ parse_boundary_eol(Bin, Pattern) -> + cowboy_http:whitespace(Rest, Fun); + nomatch -> + % CRLF not found in the given binary. +- RestStart = max(byte_size(Bin) - 1, 0), ++ RestStart = lists:max([byte_size(Bin) - 1, 0]), + <<_:RestStart/binary, Rest/binary>> = Bin, + more(Rest, fun (NewBin) -> parse_boundary_eol(NewBin, Pattern) end) + end. +@@ -175,7 +175,7 @@ skip(Bin, Pattern = {P, PSize}) -> + parse_boundary_tail(Rest, Pattern); + nomatch -> + % Boundary not found, need more data. +- RestStart = max(byte_size(Bin) - PSize + 1, 0), ++ RestStart = lists:max([byte_size(Bin) - PSize + 1, 0]), + <<_:RestStart/binary, Rest/binary>> = Bin, + more(Rest, fun (NewBin) -> skip(NewBin, Pattern) end) + end. +-- +1.7.0.4 + diff --git a/rabbitmq-server/plugins-src/cowboy-wrapper/0007-R12-type-definitions-must-be-ordered.patch b/rabbitmq-server/plugins-src/cowboy-wrapper/0007-R12-type-definitions-must-be-ordered.patch new file mode 100644 index 0000000..1b1f3de --- /dev/null +++ b/rabbitmq-server/plugins-src/cowboy-wrapper/0007-R12-type-definitions-must-be-ordered.patch @@ -0,0 +1,37 @@ +From 547731d5490b36f1239a99e6c4acc1964e724a6e Mon Sep 17 00:00:00 2001 +From: Marek Majkowski +Date: Thu, 26 Jan 2012 12:54:49 +0000 +Subject: [PATCH 7/7] R12 - type definitions must be ordered + +--- + src/cowboy_multipart.erl | 10 +++++----- + 1 files changed, 5 insertions(+), 5 deletions(-) + +diff --git a/src/cowboy_multipart.erl b/src/cowboy_multipart.erl +index c9b5b6c..0bd123a 100644 +--- a/src/cowboy_multipart.erl ++++ b/src/cowboy_multipart.erl +@@ -15,15 +15,15 @@ + %% @doc Multipart parser. + -module(cowboy_multipart). + +--type part_parser() :: parser(more(part_result())). ++-type part_parser() :: any(). + -type parser(T) :: fun((binary()) -> T). + -type more(T) :: T | {more, parser(T)}. +--type part_result() :: headers() | eof. +--type headers() :: {headers, http_headers(), body_cont()}. ++-type part_result() :: any(). ++-type headers() :: any(). + -type http_headers() :: [{atom() | binary(), binary()}]. +--type body_cont() :: cont(more(body_result())). ++-type body_cont() :: any(). + -type cont(T) :: fun(() -> T). +--type body_result() :: {body, binary(), body_cont()} | end_of_part(). ++-type body_result() :: any(). + -type end_of_part() :: {end_of_part, cont(more(part_result()))}. + -type disposition() :: {binary(), [{binary(), binary()}]}. + +-- +1.7.0.4 + diff --git a/rabbitmq-server/plugins-src/cowboy-wrapper/0008-sec-websocket-protocol.patch b/rabbitmq-server/plugins-src/cowboy-wrapper/0008-sec-websocket-protocol.patch new file mode 100644 index 0000000..494c6b8 --- /dev/null +++ b/rabbitmq-server/plugins-src/cowboy-wrapper/0008-sec-websocket-protocol.patch @@ -0,0 +1,16 @@ +diff --git a/src/cowboy_http_req.erl b/src/cowboy_http_req.erl +index 92d96ad..dd772df 100644 +--- a/src/cowboy_http_req.erl ++++ b/src/cowboy_http_req.erl +@@ -288,6 +282,11 @@ parse_header(Name, Req, Default) when Name =:= 'Upgrade' -> + fun (Value) -> + cowboy_http:nonempty_list(Value, fun cowboy_http:token_ci/2) + end); ++parse_header(Name, Req, Default) when Name =:= <<"sec-websocket-protocol">> -> ++ parse_header(Name, Req, Default, ++ fun (Value) -> ++ cowboy_http:nonempty_list(Value, fun cowboy_http:token/2) ++ end); + parse_header(Name, Req, Default) -> + {Value, Req2} = header(Name, Req, Default), + {undefined, Value, Req2}. diff --git a/rabbitmq-server/plugins-src/cowboy-wrapper/CONTRIBUTING.md b/rabbitmq-server/plugins-src/cowboy-wrapper/CONTRIBUTING.md new file mode 100644 index 0000000..69a4b4a --- /dev/null +++ b/rabbitmq-server/plugins-src/cowboy-wrapper/CONTRIBUTING.md @@ -0,0 +1,51 @@ +## Overview + +RabbitMQ projects use pull requests to discuss, collaborate on and accept code contributions. +Pull requests is the primary place of discussing code changes. + +## How to Contribute + +The process is fairly standard: + + * Fork the repository or repositories you plan on contributing to + * Clone [RabbitMQ umbrella repository](https://github.com/rabbitmq/rabbitmq-public-umbrella) + * `cd umbrella`, `make co` + * Create a branch with a descriptive name in the relevant repositories + * Make your changes, run tests, commit with a [descriptive message](http://tbaggery.com/2008/04/19/a-note-about-git-commit-messages.html), push to your fork + * Submit pull requests with an explanation what has been changed and **why** + * Submit a filled out and signed [Contributor Agreement](https://github.com/rabbitmq/ca#how-to-submit) if needed (see below) + * Be patient. We will get to your pull request eventually + +If what you are going to work on is a substantial change, please first ask the core team +of their opinion on [RabbitMQ mailing list](https://groups.google.com/forum/#!forum/rabbitmq-users). + + +## (Brief) Code of Conduct + +In one line: don't be a dick. + +Be respectful to the maintainers and other contributors. Open source +contributors put long hours into developing projects and doing user +support. Those projects and user support are available for free. We +believe this deserves some respect. + +Be respectful to people of all races, genders, religious beliefs and +political views. Regardless of how brilliant a pull request is +technically, we will not tolerate disrespectful or aggressive +behaviour. + +Contributors who violate this straightforward Code of Conduct will see +their pull requests closed and locked. + + +## Contributor Agreement + +If you want to contribute a non-trivial change, please submit a signed copy of our +[Contributor Agreement](https://github.com/rabbitmq/ca#how-to-submit) around the time +you submit your pull request. This will make it much easier (in some cases, possible) +for the RabbitMQ team at Pivotal to merge your contribution. + + +## Where to Ask Questions + +If something isn't clear, feel free to ask on our [mailing list](https://groups.google.com/forum/#!forum/rabbitmq-users). diff --git a/rabbitmq-server/plugins-src/cowboy-wrapper/Makefile b/rabbitmq-server/plugins-src/cowboy-wrapper/Makefile new file mode 100644 index 0000000..482105a --- /dev/null +++ b/rabbitmq-server/plugins-src/cowboy-wrapper/Makefile @@ -0,0 +1 @@ +include ../umbrella.mk diff --git a/rabbitmq-server/plugins-src/cowboy-wrapper/README.md b/rabbitmq-server/plugins-src/cowboy-wrapper/README.md new file mode 100644 index 0000000..e1f1d5e --- /dev/null +++ b/rabbitmq-server/plugins-src/cowboy-wrapper/README.md @@ -0,0 +1 @@ +Cowboy requires R14 diff --git a/rabbitmq-server/plugins-src/cowboy-wrapper/cowboy-git/.done b/rabbitmq-server/plugins-src/cowboy-wrapper/cowboy-git/.done new file mode 100644 index 0000000..e69de29 diff --git a/rabbitmq-server/plugins-src/cowboy-wrapper/cowboy-git/.travis.yml b/rabbitmq-server/plugins-src/cowboy-wrapper/cowboy-git/.travis.yml new file mode 100644 index 0000000..f04becf --- /dev/null +++ b/rabbitmq-server/plugins-src/cowboy-wrapper/cowboy-git/.travis.yml @@ -0,0 +1,7 @@ +language: erlang +otp_release: + - R15B + - R14B04 + - R14B03 + - R14B02 +script: "make tests" diff --git a/rabbitmq-server/plugins-src/cowboy-wrapper/cowboy-git/AUTHORS b/rabbitmq-server/plugins-src/cowboy-wrapper/cowboy-git/AUTHORS new file mode 100644 index 0000000..a07a69d --- /dev/null +++ b/rabbitmq-server/plugins-src/cowboy-wrapper/cowboy-git/AUTHORS @@ -0,0 +1,18 @@ +Cowboy is available thanks to the work of: + +Loïc Hoguin +Anthony Ramine +Magnus Klaar +Paul Oliver +Steven Gravell +Tom Burdick +Hunter Morris +Yurii Rashkovskii +Ali Sabil +Hans Ulrich Niedermann +Jesper Louis Andersen +Mathieu Lecarme +Max Lapshin +Michiel Hakvoort +Ori Bar +Alisdair Sullivan diff --git a/rabbitmq-server/plugins-src/cowboy-wrapper/cowboy-git/CHANGELOG.md b/rabbitmq-server/plugins-src/cowboy-wrapper/cowboy-git/CHANGELOG.md new file mode 100644 index 0000000..a4b815b --- /dev/null +++ b/rabbitmq-server/plugins-src/cowboy-wrapper/cowboy-git/CHANGELOG.md @@ -0,0 +1,213 @@ +CHANGELOG +========= + +0.4.0 +----- + +* Set the cowboy_listener process priority to high + + As it is the central process used by all incoming requests + we need to set its priority to high to avoid timeouts that + would happen otherwise when reaching a huge number of + concurrent requests. + +* Add cowboy:child_spec/6 for embedding in other applications + +* Add cowboy_http_rest, an experimental REST protocol support + + Based on the Webmachine diagram and documentation. It is a + new implementation, not a port, therefore a few changes have + been made. However all the callback names are the same and + should behave similarly to Webmachine. + + There is currently no documentation other than the Webmachine + resource documentation and the comments found in cowboy_http_rest, + which itself should be fairly easy to read and understand. + +* Add cowboy_http_static, an experimental static file handler + + Makes use of the aforementioned REST protocol support to + deliver files with proper content type and cache headers. + + Note that this uses the new file:sendfile support when + appropriate, which currently requires the VM to be started + with the +A option defined, else errors may randomly appear. + +* Add cowboy_bstr module for binary strings related functions + +* Add cowboy_http module for HTTP parsing functions + + This module so far contains various functions for HTTP header + parsing along with URL encoding and decoding. + +* Remove quoted from the default dependencies + + This should make Cowboy much easier to compile and use by default. + It is of course still possible to use quoted as your URL decoding + library in Cowboy thanks to the newly added urldecode option. + +* Fix supervisor spec for non dynamic modules to allow upgrades to complete + +* Add cowboy:accept_ack/1 for a cleaner handling of the shoot message + + Before, when the listener accepted a connection, the newly created + process was waiting for a message containing the atom 'shoot' before + proceeding. This has been replaced by the cowboy:accept_ack/1 function. + + This function should be used where 'shoot' was received because the + contents of the message have changed (and could change again in the + distant future). + +* Update binary parsing expressions to avoid hype crashes + + More specifically, /bits was replaced by /binary. + +* Rename the type cowboy_dispatcher:path_tokens/0 to tokens/0 + +* Remove the cowboy_clock:date/0, time/0 and datetime/0 types + + The calendar module exports those same types properly since R14B04. + +* Add cacertfile configuration option to cowboy_ssl_transport + +* Add cowboy_protocol behaviour + +* Remove -Wbehaviours dialyzer option unavailable in R15B + +* Many tests and specs improvements + +### cowboy_http_req + +* Fix a crash when reading the request body + +* Add parse_header/2 and parse_header/3 + + The following headers can now be semantically parsed: Connection, Accept, + Accept-Charset, Accept-Encoding, Accept-Language, Content-Length, + Content-Type, If-Match, If-None-Match, If-Modified-Since, + If-Unmodified-Since, Upgrade + +* Add set_resp_header/3, set_resp_cookie/4 and set_resp_body/2 + + These functions allow handlers to set response headers and body + without having to reply directly. + +* Add set_resp_body_fun/3 + + This function allows handlers to stream the body of the response + using the given fun. The size of the response must be known beforehand. + +* Add transport/1 to obtain the transport and socket for the request + + This allows handlers to have low-level socket access in those cases + where they do need it, like when streaming a response body with + set_resp_body_fun/3. + +* Add peer_addr/1 + + This function tries to guess the real peer IP based on the HTTP + headers received. + +* Add meta/2 and meta/3 to save useful protocol information + + Currently used to save the Websocket protocol version currently used, + and to save request information in the REST protocol handler. + +* Add reply/2 and reply/3 aliases to reply/4 + +* Add upgrade_reply/3 for protocol upgrades + +### cowboy_http_protocol + +* Add the {urldecode, fun urldecode/2} option + + Added when quoted was removed from the default build. Can be used to + tell Cowboy to use quoted or any other URL decoding routine. + +* Add the max_keepalive option + +* Add the max_line_length option + +* Allow HTTP handlers to stop during init/3 + + To do so they can return {shutdown, Req, State}. + +* Add loops support in HTTP handlers for proper long-polling support + + A loop can be entered by returning either of {loop, Req, State}, + {loop, Req, State, hibernate}, {loop, Req, State, Timeout} or + {loop, Req, State, Timeout, hibernate} from init/3. + + Loops are useful when we cannot reply immediately and instead + are waiting for an Erlang message to be able to complete the request, + as would typically be done for long-polling. + + Loop support in the protocol means that timeouts and hibernating + are well tested and handled so you can use those options without + worrying. It is recommended to set the timeout option. + + When a loop is started, handle/2 will never be called so it does + not need to be defined. When the request process receives an Erlang + message, it will call the info/3 function with the message as the + first argument. + + Like in OTP, you do need to set timeout and hibernate again when + returning from info/3 to enable them until the next call. + +* Fix the sending of 500 errors when handlers crash + + Now we send an error response when no response has been sent, + and do nothing more than close the connection if anything + did get sent. + +* Fix a crash when the server is sent HTTP responses + +* Fix HTTP timeouts handling when the Request-Line wasn't received + +* Fix the handling of the max number of empty lines between requests + +* Fix the handling of HEAD requests + +* Fix HTTP/1.0 Host header handling + +* Reply status 400 if we receive an unexpected value or error for headers + +* Properly close when the application sends "Connection: close" header + +* Close HTTP connections on all errors + +* Improve the error message for HTTP handlers + +### cowboy_http_websocket + +* Add websocket support for all versions up to RFC 6455 + + Support isn't perfect yet according to the specifications, but + is working against all currently known client implementations. + +* Allow websocket_init/3 to return with the hibernate option set + +* Add {shutdown, Req} return value to websocket_init/3 to fail an upgrade + +* Fix websocket timeout handling + +* Fix error messages: wrong callback name was reported on error + +* Fix byte-by-byte websocket handling + +* Fix an issue when using hixie-76 with certain proxies + +* Fix a crash in the hixie-76 handshake + +* Fix the handshake when SSL is used on port 443 + +* Fix a crash in the handshake when cowboy_http_req:compact/1 is used + +* Fix handshake when a query string is present + +* Fix a crash when the Upgrade header contains more than one token + +0.2.0 +----- + +* Initial release. diff --git a/rabbitmq-server/plugins-src/cowboy-wrapper/cowboy-git/LICENSE b/rabbitmq-server/plugins-src/cowboy-wrapper/cowboy-git/LICENSE new file mode 100644 index 0000000..7de99bb --- /dev/null +++ b/rabbitmq-server/plugins-src/cowboy-wrapper/cowboy-git/LICENSE @@ -0,0 +1,13 @@ +Copyright (c) 2011, Loïc Hoguin + +Permission to use, copy, modify, and/or distribute this software for any +purpose with or without fee is hereby granted, provided that the above +copyright notice and this permission notice appear in all copies. + +THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES +WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF +MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR +ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES +WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN +ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF +OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. diff --git a/rabbitmq-server/plugins-src/cowboy-wrapper/cowboy-git/Makefile b/rabbitmq-server/plugins-src/cowboy-wrapper/cowboy-git/Makefile new file mode 100644 index 0000000..e5524f4 --- /dev/null +++ b/rabbitmq-server/plugins-src/cowboy-wrapper/cowboy-git/Makefile @@ -0,0 +1,36 @@ +# See LICENSE for licensing information. + +DIALYZER = dialyzer +REBAR = rebar + +all: app + +app: deps + @$(REBAR) compile + +deps: + @$(REBAR) get-deps + +clean: + @$(REBAR) clean + rm -f test/*.beam + rm -f erl_crash.dump + +tests: clean app eunit ct + +eunit: + @$(REBAR) eunit skip_deps=true + +ct: + @$(REBAR) ct skip_deps=true + +build-plt: + @$(DIALYZER) --build_plt --output_plt .cowboy_dialyzer.plt \ + --apps kernel stdlib sasl inets crypto public_key ssl + +dialyze: + @$(DIALYZER) --src src --plt .cowboy_dialyzer.plt -Werror_handling \ + -Wrace_conditions -Wunmatched_returns # -Wunderspecs + +docs: + @$(REBAR) doc skip_deps=true diff --git a/rabbitmq-server/plugins-src/cowboy-wrapper/cowboy-git/README.md b/rabbitmq-server/plugins-src/cowboy-wrapper/cowboy-git/README.md new file mode 100644 index 0000000..d5950b9 --- /dev/null +++ b/rabbitmq-server/plugins-src/cowboy-wrapper/cowboy-git/README.md @@ -0,0 +1,290 @@ +Cowboy +====== + +Cowboy is a small, fast and modular HTTP server written in Erlang. + +Cowboy is also a socket acceptor pool, able to accept connections +for any kind of TCP protocol. + +Goals +----- + +Cowboy aims to provide the following advantages: + +* **Small** code base. +* Damn **fast**. +* **Modular**: transport and protocol handlers are replaceable. +* **Binary HTTP** for greater speed and lower memory usage. +* Easy to **embed** inside another application. +* Selectively **dispatch** requests to handlers, allowing you to send some + requests to your embedded code and others to a FastCGI application in + PHP or Ruby. +* No parameterized module. No process dictionary. **Clean** Erlang code. + +The server is currently in early development. Comments and suggestions are +more than welcome. To contribute, either open bug reports, or fork the project +and send us pull requests with new or improved functionality. You should +discuss your plans with us before doing any serious work, though, to avoid +duplicating efforts. + +Quick start +----------- + +* Add Cowboy as a rebar or agner dependency to your application. +* Start Cowboy and add one or more listeners. +* Write handlers for your application. +* Check out [examples](https://github.com/extend/cowboy_examples)! + +Getting Started +--------------- + +At heart, Cowboy is nothing more than an TCP acceptor pool. All it does is +accept connections received on a given port and using a given transport, +like TCP or SSL, and forward them to a request handler for the given +protocol. Acceptors and request handlers are of course supervised +automatically. + +It just so happens that Cowboy also includes an HTTP protocol handler. +But Cowboy does nothing by default. You need to explicitly ask Cowboy +to listen on a port with your chosen transport and protocol handlers. +To do so, you must start a listener. + +A listener is a special kind of supervisor that manages both the +acceptor pool and the request processes. It is named and can thus be +started and stopped at will. + +An acceptor pool is a pool of processes whose only role is to accept +new connections. It's good practice to have many of these processes +as they are very cheap and allow much quicker response when you get +many connections. Of course, as with everything else, you should +**benchmark** before you decide what's best for you. + +Cowboy includes a TCP transport handler for HTTP and an SSL transport +handler for HTTPS. The transport handlers can of course be reused for +other protocols like FTP or IRC. + +The HTTP protocol requires one last thing to continue: dispatching rules. +Don't worry about it right now though and continue reading, it'll all +be explained. + +You can start and stop listeners by calling `cowboy:start_listener/6` and +`cowboy:stop_listener/1` respectively. + +The following example demonstrates the startup of a very simple listener. + +``` erlang +application:start(cowboy), +Dispatch = [ + %% {Host, list({Path, Handler, Opts})} + {'_', [{'_', my_handler, []}]} +], +%% Name, NbAcceptors, Transport, TransOpts, Protocol, ProtoOpts +cowboy:start_listener(my_http_listener, 100, + cowboy_tcp_transport, [{port, 8080}], + cowboy_http_protocol, [{dispatch, Dispatch}] +). +``` + +This is not enough though, you must also write the my_handler module +to process the incoming HTTP requests. Of course Cowboy comes with +predefined handlers for specific tasks but most of the time you'll +want to write your own handlers for your application. + +Following is an example of a "Hello World!" HTTP handler. + +``` erlang +-module(my_handler). +-export([init/3, handle/2, terminate/2]). + +init({tcp, http}, Req, Opts) -> + {ok, Req, undefined_state}. + +handle(Req, State) -> + {ok, Req2} = cowboy_http_req:reply(200, [], <<"Hello World!">>, Req), + {ok, Req2, State}. + +terminate(Req, State) -> + ok. +``` + +You can also write handlers that do not reply directly. Instead, such handlers +will wait for an Erlang message from another process and only reply when +receiving such message, or timeout if it didn't arrive in time. + +This is especially useful for long-polling functionality, as Cowboy will handle +process hibernation and timeouts properly, preventing mistakes if you were to +write the code yourself. An handler of that kind can be defined like this: + +``` erlang +-module(my_loop_handler). +-export([init/3, info/3, terminate/2]). + +-define(TIMEOUT, 60000). + +init({tcp, http}, Req, Opts) -> + {loop, Req, undefined_state, ?TIMEOUT, hibernate}. + +info({reply, Body}, Req, State) -> + {ok, Req2} = cowboy_http_req:reply(200, [], Body, Req), + {ok, Req2, State}; +info(Message, Req, State) -> + {loop, Req, State, hibernate}. + +terminate(Req, State) -> + ok. +``` + +It is of course possible to combine both type of handlers together as long as +you return the proper tuple from init/3. + +**Note**: versions prior to `0.4.0` used the +[quoted](https://github.com/klaar/quoted.erl) library instead of the built in +`cowboy_http:urldecode/2` function. If you want to retain this you must add it +as a dependency to your application and add the following cowboy_http_protocol +option: + +``` erlang + {urldecode, {fun quoted:from_url/2, quoted:make([])}} +``` + +Continue reading to learn how to dispatch rules and handle requests. + +Dispatch rules +-------------- + +Cowboy allows you to dispatch HTTP requests directly to a specific handler +based on the hostname and path information from the request. It also lets +you define static options for the handler directly in the rules. + +To match the hostname and path, Cowboy requires a list of tokens. For +example, to match the "dev-extend.eu" domain name, you must specify +`[<<"dev-extend">>, <<"eu">>]`. Or, to match the "/path/to/my/resource" +you must use `[<<"path">>, <<"to">>, <<"my">>, <<"resource">>]`. All the +tokens must be given as binary. + +You can use the special token `'_'` (the atom underscore) to indicate that +you accept anything in that position. For example if you have both +"dev-extend.eu" and "dev-extend.fr" domains, you can use the match spec +`[<<"dev-extend">>, '_']` to match any top level extension. + +Finally, you can also match multiple leading segments of the domain name and +multiple trailing segments of the request path using the atom `'...'` (the atom +ellipsis) respectively as the first host token or the last path token. For +example, host rule `['...', <<"dev-extend">>, <<"eu">>]` can match both +"cowboy.bugs.dev-extend.eu" and "dev-extend.eu" and path rule +`[<<"projects">>, '...']` can match both "/projects" and +"/projects/cowboy/issues/42". The host leading segments and the path trailing +segments can later be retrieved through `cowboy_http_req:host_info/1` and +`cowboy_http_req:path_info/1`. + +Any other atom used as a token will bind the value to this atom when +matching. To follow on our hostnames example, `[<<"dev-extend">>, ext]` +would bind the values `<<"eu">>` and `<<"fr">>` to the ext atom, that you +can later retrieve in your handler by calling `cowboy_http_req:binding/{2,3}`. + +You can also accept any match spec by using the atom `'_'` directly instead of +a list of tokens. Our hello world example above uses this to forward all +requests to a single handler. + +There is currently no way to match multiple tokens at once. + +Requests handling +----------------- + +Requests are passed around in the Request variable. Although they are +defined as a record, it is recommended to access them only through the +cowboy_http_req module API. + +You can retrieve the HTTP method, HTTP version, peer address and port, +host tokens, raw host, used port, path tokens, raw path, query string +values, bound values from the dispatch step, header values from the +request. You can also read the request body, if any, optionally parsing +it as a query string. Finally, the request allows you to send a response +to the client. + +See the cowboy_http_req module for more information. + +Websockets +---------- + +The Websocket protocol is built upon the HTTP protocol. It first sends +an HTTP request for an handshake, performs it and then switches +to Websocket. Therefore you need to write a standard HTTP handler to +confirm the handshake should be completed and then the Websocket-specific +callbacks. + +A simple handler doing nothing but sending a repetitive message using +Websocket would look like this: + +``` erlang +-module(my_ws_handler). +-export([init/3]). +-export([websocket_init/3, websocket_handle/3, + websocket_info/3, websocket_terminate/3]). + +init({tcp, http}, Req, Opts) -> + {upgrade, protocol, cowboy_http_websocket}. + +websocket_init(TransportName, Req, _Opts) -> + erlang:start_timer(1000, self(), <<"Hello!">>), + {ok, Req, undefined_state}. + +websocket_handle({text, Msg}, Req, State) -> + {reply, {text, << "That's what she said! ", Msg/binary >>}, Req, State}; +websocket_handle(_Data, Req, State) -> + {ok, Req, State}. + +websocket_info({timeout, _Ref, Msg}, Req, State) -> + erlang:start_timer(1000, self(), <<"How' you doin'?">>), + {reply, {text, Msg}, Req, State}; +websocket_info(_Info, Req, State) -> + {ok, Req, State}. + +websocket_terminate(_Reason, _Req, _State) -> + ok. +``` + +Of course you can have an HTTP handler doing both HTTP and Websocket +handling, but for the sake of this example we're ignoring the HTTP +part entirely. + +As the Websocket protocol is still a draft the API is subject to change +regularly when support to the most recent drafts gets added. Features may +be added, changed or removed before the protocol gets finalized. Cowboy +tries to implement all drafts transparently and give a single interface to +handle them all, however. + +Using Cowboy with other protocols +--------------------------------- + +One of the strengths of Cowboy is of course that you can use it with any +protocol you want. The only downside is that if it's not HTTP, you'll +probably have to write the protocol handler yourself. + +The only exported function a protocol handler needs is the start_link/4 +function, with arguments ListenerPid, Socket, Transport and Opts. ListenerPid +is the pid to the listener's gen_server, managing the connections. Socket is of +course the client socket; Transport is the module name of the chosen transport +handler and Opts is protocol options defined when starting the listener. + +After initializing your protocol, it is recommended to call the +function cowboy:accept_ack/1 with the ListenerPid as argument, +as it will ensure Cowboy has been able to fully initialize the socket. +Anything you do past this point is up to you! + +If you need to change some socket options, like enabling raw mode for example, +you can call the Transport:setopts/2 function. It is the protocol's +responsability to manage the socket usage, there should be no need for an user +to specify that kind of options while starting a listener. + +You should definitely look at the cowboy_http_protocol module for a great +example of fast request handling if you need to. Otherwise it's probably +safe to use `{active, once}` mode and handle everything as it comes. + +Note that while you technically can run a protocol handler directly as a +gen_server or a gen_fsm, it's probably not a good idea, as the only call +you'll ever receive from Cowboy is the start_link/4 call. On the other +hand, feel free to write a very basic protocol handler which then forwards +requests to a gen_server or gen_fsm. By doing so however you must take +care to supervise their processes as Cowboy only knows about the protocol +handler itself. diff --git a/rabbitmq-server/plugins-src/cowboy-wrapper/cowboy-git/cover.spec b/rabbitmq-server/plugins-src/cowboy-wrapper/cowboy-git/cover.spec new file mode 100644 index 0000000..9dba11c --- /dev/null +++ b/rabbitmq-server/plugins-src/cowboy-wrapper/cowboy-git/cover.spec @@ -0,0 +1 @@ +{incl_app, cowboy, details}. diff --git a/rabbitmq-server/plugins-src/cowboy-wrapper/cowboy-git/doc/overview.edoc b/rabbitmq-server/plugins-src/cowboy-wrapper/cowboy-git/doc/overview.edoc new file mode 100644 index 0000000..56648c4 --- /dev/null +++ b/rabbitmq-server/plugins-src/cowboy-wrapper/cowboy-git/doc/overview.edoc @@ -0,0 +1,4 @@ +@author Loïc Hoguin +@copyright 2011 Loïc Hoguin +@version HEAD +@title Small, fast, modular HTTP server. diff --git a/rabbitmq-server/plugins-src/cowboy-wrapper/cowboy-git/include/http.hrl b/rabbitmq-server/plugins-src/cowboy-wrapper/cowboy-git/include/http.hrl new file mode 100644 index 0000000..c98f873 --- /dev/null +++ b/rabbitmq-server/plugins-src/cowboy-wrapper/cowboy-git/include/http.hrl @@ -0,0 +1,55 @@ +%% Copyright (c) 2011, Loïc Hoguin +%% Copyright (c) 2011, Anthony Ramine +%% +%% Permission to use, copy, modify, and/or distribute this software for any +%% purpose with or without fee is hereby granted, provided that the above +%% copyright notice and this permission notice appear in all copies. +%% +%% THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES +%% WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF +%% MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR +%% ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES +%% WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN +%% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF +%% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + +-record(http_req, { + %% Transport. + socket = undefined :: undefined | inet:socket(), + transport = undefined :: undefined | module(), + connection = keepalive :: keepalive | close, + + %% Request. + pid = undefined :: pid(), + method = 'GET' :: cowboy_http:method(), + version = {1, 1} :: cowboy_http:version(), + peer = undefined :: undefined | {inet:ip_address(), inet:ip_port()}, + host = undefined :: undefined | cowboy_dispatcher:tokens(), + host_info = undefined :: undefined | cowboy_dispatcher:tokens(), + raw_host = undefined :: undefined | binary(), + port = undefined :: undefined | inet:ip_port(), + path = undefined :: undefined | '*' | cowboy_dispatcher:tokens(), + path_info = undefined :: undefined | cowboy_dispatcher:tokens(), + raw_path = undefined :: undefined | binary(), + qs_vals = undefined :: undefined | list({binary(), binary() | true}), + raw_qs = undefined :: undefined | binary(), + bindings = undefined :: undefined | cowboy_dispatcher:bindings(), + headers = [] :: cowboy_http:headers(), + p_headers = [] :: [any()], %% @todo Improve those specs. + cookies = undefined :: undefined | [{binary(), binary()}], + meta = [] :: [{atom(), any()}], + + %% Request body. + body_state = waiting :: waiting | done | + {multipart, non_neg_integer(), fun()}, + buffer = <<>> :: binary(), + + %% Response. + resp_state = waiting :: locked | waiting | chunks | done, + resp_headers = [] :: cowboy_http:headers(), + resp_body = <<>> :: cowboy_http:fake_iodata() | {non_neg_integer(), + fun(() -> {sent, non_neg_integer()})}, + + %% Functions. + urldecode :: {fun((binary(), T) -> binary()), T} +}). diff --git a/rabbitmq-server/plugins-src/cowboy-wrapper/cowboy-git/rebar.config b/rabbitmq-server/plugins-src/cowboy-wrapper/cowboy-git/rebar.config new file mode 100644 index 0000000..82d1fca --- /dev/null +++ b/rabbitmq-server/plugins-src/cowboy-wrapper/cowboy-git/rebar.config @@ -0,0 +1,12 @@ +{cover_enabled, true}. +{deps, [ + {proper, "1.0", + {git, "git://github.com/manopapad/proper.git", {tag, "v1.0"}}} +]}. +{eunit_opts, [verbose, {report, {eunit_surefire, [{dir, "."}]}}]}. +{erl_opts, [ +%% bin_opt_info, +%% warn_missing_spec, + warnings_as_errors, + warn_export_all +]}. diff --git a/rabbitmq-server/plugins-src/cowboy-wrapper/cowboy-git/src/cowboy.app.src b/rabbitmq-server/plugins-src/cowboy-wrapper/cowboy-git/src/cowboy.app.src new file mode 100644 index 0000000..9b3ee50 --- /dev/null +++ b/rabbitmq-server/plugins-src/cowboy-wrapper/cowboy-git/src/cowboy.app.src @@ -0,0 +1,26 @@ +%% Copyright (c) 2011, Loïc Hoguin +%% +%% Permission to use, copy, modify, and/or distribute this software for any +%% purpose with or without fee is hereby granted, provided that the above +%% copyright notice and this permission notice appear in all copies. +%% +%% THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES +%% WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF +%% MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR +%% ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES +%% WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN +%% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF +%% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + +{application, cowboy, [ + {description, "Small, fast, modular HTTP server."}, + {vsn, "0.5.0"}, + {modules, []}, + {registered, [cowboy_clock, cowboy_sup]}, + {applications, [ + kernel, + stdlib + ]}, + {mod, {cowboy_app, []}}, + {env, []} +]}. diff --git a/rabbitmq-server/plugins-src/cowboy-wrapper/cowboy-git/src/cowboy.erl b/rabbitmq-server/plugins-src/cowboy-wrapper/cowboy-git/src/cowboy.erl new file mode 100644 index 0000000..6defeea --- /dev/null +++ b/rabbitmq-server/plugins-src/cowboy-wrapper/cowboy-git/src/cowboy.erl @@ -0,0 +1,85 @@ +%% Copyright (c) 2011, Loïc Hoguin +%% +%% Permission to use, copy, modify, and/or distribute this software for any +%% purpose with or without fee is hereby granted, provided that the above +%% copyright notice and this permission notice appear in all copies. +%% +%% THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES +%% WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF +%% MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR +%% ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES +%% WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN +%% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF +%% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + +%% @doc Cowboy API to start and stop listeners. +-module(cowboy). + +-export([start_listener/6, stop_listener/1, child_spec/6, accept_ack/1]). + +%% @doc Start a listener for the given transport and protocol. +%% +%% A listener is effectively a pool of NbAcceptors acceptors. +%% Acceptors accept connections on the given Transport and forward +%% requests to the given Protocol handler. Both transport and protocol +%% modules can be given options through the TransOpts and the +%% ProtoOpts arguments. Available options are documented in the +%% listen transport function and in the protocol module of your choice. +%% +%% All acceptor and request processes are supervised by the listener. +%% +%% It is recommended to set a large enough number of acceptors to improve +%% performance. The exact number depends of course on your hardware, on the +%% protocol used and on the number of expected simultaneous connections. +%% +%% The Transport option max_connections allows you to define +%% the maximum number of simultaneous connections for this listener. It defaults +%% to 1024. See cowboy_listener for more details on limiting the number +%% of connections. +%% +%% Although Cowboy includes a cowboy_http_protocol handler, other +%% handlers can be created for different protocols like IRC, FTP and more. +%% +%% Ref can be used to stop the listener later on. +-spec start_listener(any(), non_neg_integer(), module(), any(), module(), any()) + -> {ok, pid()}. +start_listener(Ref, NbAcceptors, Transport, TransOpts, Protocol, ProtoOpts) + when is_integer(NbAcceptors) andalso is_atom(Transport) + andalso is_atom(Protocol) -> + supervisor:start_child(cowboy_sup, child_spec(Ref, NbAcceptors, + Transport, TransOpts, Protocol, ProtoOpts)). + +%% @doc Stop a listener identified by Ref. +%% @todo Currently request processes aren't terminated with the listener. +-spec stop_listener(any()) -> ok | {error, not_found}. +stop_listener(Ref) -> + case supervisor:terminate_child(cowboy_sup, {cowboy_listener_sup, Ref}) of + ok -> + supervisor:delete_child(cowboy_sup, {cowboy_listener_sup, Ref}); + {error, Reason} -> + {error, Reason} + end. + +%% @doc Return a child spec suitable for embedding. +%% +%% When you want to embed cowboy in another application, you can use this +%% function to create a ChildSpec suitable for use in a supervisor. +%% The parameters are the same as in start_listener/6 but rather +%% than hooking the listener to the cowboy internal supervisor, it just returns +%% the spec. +-spec child_spec(any(), non_neg_integer(), module(), any(), module(), any()) + -> supervisor:child_spec(). +child_spec(Ref, NbAcceptors, Transport, TransOpts, Protocol, ProtoOpts) + when is_integer(NbAcceptors) andalso is_atom(Transport) + andalso is_atom(Protocol) -> + {{cowboy_listener_sup, Ref}, {cowboy_listener_sup, start_link, [ + NbAcceptors, Transport, TransOpts, Protocol, ProtoOpts + ]}, permanent, 5000, supervisor, [cowboy_listener_sup]}. + +%% @doc Acknowledge the accepted connection. +%% +%% Effectively used to make sure the socket control has been given to +%% the protocol process before starting to use it. +-spec accept_ack(pid()) -> ok. +accept_ack(ListenerPid) -> + receive {shoot, ListenerPid} -> ok end. diff --git a/rabbitmq-server/plugins-src/cowboy-wrapper/cowboy-git/src/cowboy_acceptor.erl b/rabbitmq-server/plugins-src/cowboy-wrapper/cowboy-git/src/cowboy_acceptor.erl new file mode 100644 index 0000000..4cb9fa7 --- /dev/null +++ b/rabbitmq-server/plugins-src/cowboy-wrapper/cowboy-git/src/cowboy_acceptor.erl @@ -0,0 +1,59 @@ +%% Copyright (c) 2011, Loïc Hoguin +%% +%% Permission to use, copy, modify, and/or distribute this software for any +%% purpose with or without fee is hereby granted, provided that the above +%% copyright notice and this permission notice appear in all copies. +%% +%% THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES +%% WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF +%% MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR +%% ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES +%% WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN +%% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF +%% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + +%% @private +-module(cowboy_acceptor). + +-export([start_link/7]). %% API. +-export([acceptor/7]). %% Internal. + +%% API. + +-spec start_link(inet:socket(), module(), module(), any(), + non_neg_integer(), pid(), pid()) -> {ok, pid()}. +start_link(LSocket, Transport, Protocol, Opts, + MaxConns, ListenerPid, ReqsSup) -> + Pid = spawn_link(?MODULE, acceptor, + [LSocket, Transport, Protocol, Opts, MaxConns, ListenerPid, ReqsSup]), + {ok, Pid}. + +%% Internal. + +-spec acceptor(inet:socket(), module(), module(), any(), + non_neg_integer(), pid(), pid()) -> no_return(). +acceptor(LSocket, Transport, Protocol, Opts, MaxConns, ListenerPid, ReqsSup) -> + case Transport:accept(LSocket, 2000) of + {ok, CSocket} -> + {ok, Pid} = supervisor:start_child(ReqsSup, + [ListenerPid, CSocket, Transport, Protocol, Opts]), + Transport:controlling_process(CSocket, Pid), + {ok, NbConns} = cowboy_listener:add_connection(ListenerPid, + default, Pid), + Pid ! {shoot, ListenerPid}, + limit_reqs(ListenerPid, NbConns, MaxConns); + {error, timeout} -> + ignore; + {error, _Reason} -> + %% @todo Probably do something here. If the socket was closed, + %% we may want to try and listen again on the port? + ignore + end, + ?MODULE:acceptor(LSocket, Transport, Protocol, Opts, + MaxConns, ListenerPid, ReqsSup). + +-spec limit_reqs(pid(), non_neg_integer(), non_neg_integer()) -> ok. +limit_reqs(_ListenerPid, NbConns, MaxConns) when NbConns =< MaxConns -> + ok; +limit_reqs(ListenerPid, _NbConns, MaxConns) -> + cowboy_listener:wait(ListenerPid, default, MaxConns). diff --git a/rabbitmq-server/plugins-src/cowboy-wrapper/cowboy-git/src/cowboy_acceptors_sup.erl b/rabbitmq-server/plugins-src/cowboy-wrapper/cowboy-git/src/cowboy_acceptors_sup.erl new file mode 100644 index 0000000..17849a6 --- /dev/null +++ b/rabbitmq-server/plugins-src/cowboy-wrapper/cowboy-git/src/cowboy_acceptors_sup.erl @@ -0,0 +1,43 @@ +%% Copyright (c) 2011, Loïc Hoguin +%% +%% Permission to use, copy, modify, and/or distribute this software for any +%% purpose with or without fee is hereby granted, provided that the above +%% copyright notice and this permission notice appear in all copies. +%% +%% THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES +%% WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF +%% MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR +%% ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES +%% WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN +%% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF +%% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + +%% @private +-module(cowboy_acceptors_sup). +-behaviour(supervisor). + +-export([start_link/7]). %% API. +-export([init/1]). %% supervisor. + +%% API. + +-spec start_link(non_neg_integer(), module(), any(), + module(), any(), pid(), pid()) -> {ok, pid()}. +start_link(NbAcceptors, Transport, TransOpts, + Protocol, ProtoOpts, ListenerPid, ReqsPid) -> + supervisor:start_link(?MODULE, [NbAcceptors, Transport, TransOpts, + Protocol, ProtoOpts, ListenerPid, ReqsPid]). + +%% supervisor. + +-spec init(list()) -> {ok, {{one_for_one, 10, 10}, list()}}. +init([NbAcceptors, Transport, TransOpts, + Protocol, ProtoOpts, ListenerPid, ReqsPid]) -> + {ok, LSocket} = Transport:listen(TransOpts), + MaxConns = proplists:get_value(max_connections, TransOpts, 1024), + Procs = [{{acceptor, self(), N}, {cowboy_acceptor, start_link, [ + LSocket, Transport, Protocol, ProtoOpts, + MaxConns, ListenerPid, ReqsPid + ]}, permanent, brutal_kill, worker, []} + || N <- lists:seq(1, NbAcceptors)], + {ok, {{one_for_one, 10, 10}, Procs}}. diff --git a/rabbitmq-server/plugins-src/cowboy-wrapper/cowboy-git/src/cowboy_app.erl b/rabbitmq-server/plugins-src/cowboy-wrapper/cowboy-git/src/cowboy_app.erl new file mode 100644 index 0000000..c7cefe4 --- /dev/null +++ b/rabbitmq-server/plugins-src/cowboy-wrapper/cowboy-git/src/cowboy_app.erl @@ -0,0 +1,53 @@ +%% Copyright (c) 2011, Loïc Hoguin +%% +%% Permission to use, copy, modify, and/or distribute this software for any +%% purpose with or without fee is hereby granted, provided that the above +%% copyright notice and this permission notice appear in all copies. +%% +%% THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES +%% WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF +%% MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR +%% ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES +%% WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN +%% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF +%% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + +%% @private +-module(cowboy_app). +-behaviour(application). + +-export([start/2, stop/1, profile_output/0]). %% API. + +-type application_start_type() :: normal + | {takeover, node()} | {failover, node()}. + +%% API. + +-spec start(application_start_type(), any()) -> {ok, pid()}. +start(_Type, _Args) -> + consider_profiling(), + cowboy_sup:start_link(). + +-spec stop(any()) -> ok. +stop(_State) -> + ok. + +-spec profile_output() -> ok. +profile_output() -> + eprof:stop_profiling(), + eprof:log("procs.profile"), + eprof:analyze(procs), + eprof:log("total.profile"), + eprof:analyze(total). + +%% Internal. + +-spec consider_profiling() -> profiling | not_profiling. +consider_profiling() -> + case application:get_env(profile) of + {ok, true} -> + {ok, _Pid} = eprof:start(), + eprof:start_profiling([self()]); + _ -> + not_profiling + end. diff --git a/rabbitmq-server/plugins-src/cowboy-wrapper/cowboy-git/src/cowboy_bstr.erl b/rabbitmq-server/plugins-src/cowboy-wrapper/cowboy-git/src/cowboy_bstr.erl new file mode 100644 index 0000000..1c702ef --- /dev/null +++ b/rabbitmq-server/plugins-src/cowboy-wrapper/cowboy-git/src/cowboy_bstr.erl @@ -0,0 +1,86 @@ +%% Copyright (c) 2011, Loïc Hoguin +%% +%% Permission to use, copy, modify, and/or distribute this software for any +%% purpose with or without fee is hereby granted, provided that the above +%% copyright notice and this permission notice appear in all copies. +%% +%% THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES +%% WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF +%% MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR +%% ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES +%% WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN +%% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF +%% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + +%% @doc Binary string manipulation. +-module(cowboy_bstr). + +-export([to_lower/1]). %% Binary strings. +-export([char_to_lower/1, char_to_upper/1]). %% Characters. + +%% @doc Convert a binary string to lowercase. +-spec to_lower(binary()) -> binary(). +to_lower(L) -> + << << (char_to_lower(C)) >> || << C >> <= L >>. + +%% @doc Convert [A-Z] characters to lowercase. +%% @end +%% We gain noticeable speed by matching each value directly. +-spec char_to_lower(char()) -> char(). +char_to_lower($A) -> $a; +char_to_lower($B) -> $b; +char_to_lower($C) -> $c; +char_to_lower($D) -> $d; +char_to_lower($E) -> $e; +char_to_lower($F) -> $f; +char_to_lower($G) -> $g; +char_to_lower($H) -> $h; +char_to_lower($I) -> $i; +char_to_lower($J) -> $j; +char_to_lower($K) -> $k; +char_to_lower($L) -> $l; +char_to_lower($M) -> $m; +char_to_lower($N) -> $n; +char_to_lower($O) -> $o; +char_to_lower($P) -> $p; +char_to_lower($Q) -> $q; +char_to_lower($R) -> $r; +char_to_lower($S) -> $s; +char_to_lower($T) -> $t; +char_to_lower($U) -> $u; +char_to_lower($V) -> $v; +char_to_lower($W) -> $w; +char_to_lower($X) -> $x; +char_to_lower($Y) -> $y; +char_to_lower($Z) -> $z; +char_to_lower(Ch) -> Ch. + +%% @doc Convert [a-z] characters to uppercase. +-spec char_to_upper(char()) -> char(). +char_to_upper($a) -> $A; +char_to_upper($b) -> $B; +char_to_upper($c) -> $C; +char_to_upper($d) -> $D; +char_to_upper($e) -> $E; +char_to_upper($f) -> $F; +char_to_upper($g) -> $G; +char_to_upper($h) -> $H; +char_to_upper($i) -> $I; +char_to_upper($j) -> $J; +char_to_upper($k) -> $K; +char_to_upper($l) -> $L; +char_to_upper($m) -> $M; +char_to_upper($n) -> $N; +char_to_upper($o) -> $O; +char_to_upper($p) -> $P; +char_to_upper($q) -> $Q; +char_to_upper($r) -> $R; +char_to_upper($s) -> $S; +char_to_upper($t) -> $T; +char_to_upper($u) -> $U; +char_to_upper($v) -> $V; +char_to_upper($w) -> $W; +char_to_upper($x) -> $X; +char_to_upper($y) -> $Y; +char_to_upper($z) -> $Z; +char_to_upper(Ch) -> Ch. diff --git a/rabbitmq-server/plugins-src/cowboy-wrapper/cowboy-git/src/cowboy_clock.erl b/rabbitmq-server/plugins-src/cowboy-wrapper/cowboy-git/src/cowboy_clock.erl new file mode 100644 index 0000000..c699f4f --- /dev/null +++ b/rabbitmq-server/plugins-src/cowboy-wrapper/cowboy-git/src/cowboy_clock.erl @@ -0,0 +1,241 @@ +%% Copyright (c) 2011, Loïc Hoguin +%% +%% Permission to use, copy, modify, and/or distribute this software for any +%% purpose with or without fee is hereby granted, provided that the above +%% copyright notice and this permission notice appear in all copies. +%% +%% THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES +%% WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF +%% MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR +%% ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES +%% WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN +%% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF +%% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + +%% @doc Date and time related functions. +%% +%% While a gen_server process runs in the background to update +%% the cache of formatted dates every second, all API calls are +%% local and directly read from the ETS cache table, providing +%% fast time and date computations. +-module(cowboy_clock). +-behaviour(gen_server). + +-export([start_link/0, stop/0, rfc1123/0, rfc2109/1]). %% API. +-export([init/1, handle_call/3, handle_cast/2, + handle_info/2, terminate/2, code_change/3]). %% gen_server. + +-record(state, { + universaltime = undefined :: undefined | calendar:datetime(), + rfc1123 = <<>> :: binary(), + tref = undefined :: undefined | timer:tref() +}). + +-define(SERVER, ?MODULE). +-define(TABLE, ?MODULE). + +-include_lib("eunit/include/eunit.hrl"). + +%% API. + +%% @private +-spec start_link() -> {ok, pid()}. +start_link() -> + gen_server:start_link({local, ?SERVER}, ?MODULE, [], []). + +%% @private +-spec stop() -> stopped. +stop() -> + gen_server:call(?SERVER, stop). + +%% @doc Return the current date and time formatted according to RFC-1123. +%% +%% This format is used in the 'Date' header sent with HTTP responses. +-spec rfc1123() -> binary(). +rfc1123() -> + ets:lookup_element(?TABLE, rfc1123, 2). + +%% @doc Return the current date and time formatted according to RFC-2109. +%% +%% This format is used in the 'Set-Cookie' header sent with +%% HTTP responses. +-spec rfc2109(calendar:datetime()) -> binary(). +rfc2109(LocalTime) -> + {{YYYY,MM,DD},{Hour,Min,Sec}} = + case calendar:local_time_to_universal_time_dst(LocalTime) of + [Gmt] -> Gmt; + [_,Gmt] -> Gmt + end, + Wday = calendar:day_of_the_week({YYYY,MM,DD}), + DayBin = pad_int(DD), + YearBin = list_to_binary(integer_to_list(YYYY)), + HourBin = pad_int(Hour), + MinBin = pad_int(Min), + SecBin = pad_int(Sec), + WeekDay = weekday(Wday), + Month = month(MM), + <>. + +%% gen_server. + +%% @private +-spec init([]) -> {ok, #state{}}. +init([]) -> + ?TABLE = ets:new(?TABLE, [set, protected, + named_table, {read_concurrency, true}]), + T = erlang:universaltime(), + B = update_rfc1123(<<>>, undefined, T), + {ok, TRef} = timer:send_interval(1000, update), + ets:insert(?TABLE, {rfc1123, B}), + {ok, #state{universaltime=T, rfc1123=B, tref=TRef}}. + +%% @private +-spec handle_call(_, _, State) + -> {reply, ignored, State} | {stop, normal, stopped, State}. +handle_call(stop, _From, State=#state{tref=TRef}) -> + {ok, cancel} = timer:cancel(TRef), + {stop, normal, stopped, State}; +handle_call(_Request, _From, State) -> + {reply, ignored, State}. + +%% @private +-spec handle_cast(_, State) -> {noreply, State}. +handle_cast(_Msg, State) -> + {noreply, State}. + +%% @private +-spec handle_info(_, State) -> {noreply, State}. +handle_info(update, #state{universaltime=Prev, rfc1123=B1, tref=TRef}) -> + T = erlang:universaltime(), + B2 = update_rfc1123(B1, Prev, T), + ets:insert(?TABLE, {rfc1123, B2}), + {noreply, #state{universaltime=T, rfc1123=B2, tref=TRef}}; +handle_info(_Info, State) -> + {noreply, State}. + +%% @private +-spec terminate(_, _) -> ok. +terminate(_Reason, _State) -> + ok. + +%% @private +-spec code_change(_, State, _) -> {ok, State}. +code_change(_OldVsn, State, _Extra) -> + {ok, State}. + +%% Internal. + +-spec update_rfc1123(binary(), undefined | calendar:datetime(), + calendar:datetime()) -> binary(). +update_rfc1123(Bin, Now, Now) -> + Bin; +update_rfc1123(<< Keep:23/binary, _/bits >>, + {Date, {H, M, _}}, {Date, {H, M, S}}) -> + << Keep/binary, (pad_int(S))/binary, " GMT" >>; +update_rfc1123(<< Keep:20/binary, _/bits >>, + {Date, {H, _, _}}, {Date, {H, M, S}}) -> + << Keep/binary, (pad_int(M))/binary, $:, (pad_int(S))/binary, " GMT" >>; +update_rfc1123(<< Keep:17/binary, _/bits >>, {Date, _}, {Date, {H, M, S}}) -> + << Keep/binary, (pad_int(H))/binary, $:, (pad_int(M))/binary, + $:, (pad_int(S))/binary, " GMT" >>; +update_rfc1123(<< _:7/binary, Keep:10/binary, _/bits >>, + {{Y, Mo, _}, _}, {Date = {Y, Mo, D}, {H, M, S}}) -> + Wday = calendar:day_of_the_week(Date), + << (weekday(Wday))/binary, ", ", (pad_int(D))/binary, Keep/binary, + (pad_int(H))/binary, $:, (pad_int(M))/binary, + $:, (pad_int(S))/binary, " GMT" >>; +update_rfc1123(<< _:11/binary, Keep:6/binary, _/bits >>, + {{Y, _, _}, _}, {Date = {Y, Mo, D}, {H, M, S}}) -> + Wday = calendar:day_of_the_week(Date), + << (weekday(Wday))/binary, ", ", (pad_int(D))/binary, " ", + (month(Mo))/binary, Keep/binary, + (pad_int(H))/binary, $:, (pad_int(M))/binary, + $:, (pad_int(S))/binary, " GMT" >>; +update_rfc1123(_, _, {Date = {Y, Mo, D}, {H, M, S}}) -> + Wday = calendar:day_of_the_week(Date), + << (weekday(Wday))/binary, ", ", (pad_int(D))/binary, " ", + (month(Mo))/binary, " ", (list_to_binary(integer_to_list(Y)))/binary, + " ", (pad_int(H))/binary, $:, (pad_int(M))/binary, + $:, (pad_int(S))/binary, " GMT" >>. + +%% Following suggestion by MononcQc on #erlounge. +-spec pad_int(0..59) -> binary(). +pad_int(X) when X < 10 -> + << $0, ($0 + X) >>; +pad_int(X) -> + list_to_binary(integer_to_list(X)). + +-spec weekday(1..7) -> <<_:24>>. +weekday(1) -> <<"Mon">>; +weekday(2) -> <<"Tue">>; +weekday(3) -> <<"Wed">>; +weekday(4) -> <<"Thu">>; +weekday(5) -> <<"Fri">>; +weekday(6) -> <<"Sat">>; +weekday(7) -> <<"Sun">>. + +-spec month(1..12) -> <<_:24>>. +month( 1) -> <<"Jan">>; +month( 2) -> <<"Feb">>; +month( 3) -> <<"Mar">>; +month( 4) -> <<"Apr">>; +month( 5) -> <<"May">>; +month( 6) -> <<"Jun">>; +month( 7) -> <<"Jul">>; +month( 8) -> <<"Aug">>; +month( 9) -> <<"Sep">>; +month(10) -> <<"Oct">>; +month(11) -> <<"Nov">>; +month(12) -> <<"Dec">>. + +%% Tests. + +-ifdef(TEST). + +update_rfc1123_test_() -> + Tests = [ + {<<"Sat, 14 May 2011 14:25:33 GMT">>, undefined, + {{2011, 5, 14}, {14, 25, 33}}, <<>>}, + {<<"Sat, 14 May 2011 14:25:33 GMT">>, {{2011, 5, 14}, {14, 25, 33}}, + {{2011, 5, 14}, {14, 25, 33}}, <<"Sat, 14 May 2011 14:25:33 GMT">>}, + {<<"Sat, 14 May 2011 14:25:34 GMT">>, {{2011, 5, 14}, {14, 25, 33}}, + {{2011, 5, 14}, {14, 25, 34}}, <<"Sat, 14 May 2011 14:25:33 GMT">>}, + {<<"Sat, 14 May 2011 14:26:00 GMT">>, {{2011, 5, 14}, {14, 25, 59}}, + {{2011, 5, 14}, {14, 26, 0}}, <<"Sat, 14 May 2011 14:25:59 GMT">>}, + {<<"Sat, 14 May 2011 15:00:00 GMT">>, {{2011, 5, 14}, {14, 59, 59}}, + {{2011, 5, 14}, {15, 0, 0}}, <<"Sat, 14 May 2011 14:59:59 GMT">>}, + {<<"Sun, 15 May 2011 00:00:00 GMT">>, {{2011, 5, 14}, {23, 59, 59}}, + {{2011, 5, 15}, { 0, 0, 0}}, <<"Sat, 14 May 2011 23:59:59 GMT">>}, + {<<"Wed, 01 Jun 2011 00:00:00 GMT">>, {{2011, 5, 31}, {23, 59, 59}}, + {{2011, 6, 1}, { 0, 0, 0}}, <<"Tue, 31 May 2011 23:59:59 GMT">>}, + {<<"Sun, 01 Jan 2012 00:00:00 GMT">>, {{2011, 5, 31}, {23, 59, 59}}, + {{2012, 1, 1}, { 0, 0, 0}}, <<"Sat, 31 Dec 2011 23:59:59 GMT">>} + ], + [{R, fun() -> R = update_rfc1123(B, P, N) end} || {R, P, N, B} <- Tests]. + +pad_int_test_() -> + Tests = [ + { 0, <<"00">>}, { 1, <<"01">>}, { 2, <<"02">>}, { 3, <<"03">>}, + { 4, <<"04">>}, { 5, <<"05">>}, { 6, <<"06">>}, { 7, <<"07">>}, + { 8, <<"08">>}, { 9, <<"09">>}, {10, <<"10">>}, {11, <<"11">>}, + {12, <<"12">>}, {13, <<"13">>}, {14, <<"14">>}, {15, <<"15">>}, + {16, <<"16">>}, {17, <<"17">>}, {18, <<"18">>}, {19, <<"19">>}, + {20, <<"20">>}, {21, <<"21">>}, {22, <<"22">>}, {23, <<"23">>}, + {24, <<"24">>}, {25, <<"25">>}, {26, <<"26">>}, {27, <<"27">>}, + {28, <<"28">>}, {29, <<"29">>}, {30, <<"30">>}, {31, <<"31">>}, + {32, <<"32">>}, {33, <<"33">>}, {34, <<"34">>}, {35, <<"35">>}, + {36, <<"36">>}, {37, <<"37">>}, {38, <<"38">>}, {39, <<"39">>}, + {40, <<"40">>}, {41, <<"41">>}, {42, <<"42">>}, {43, <<"43">>}, + {44, <<"44">>}, {45, <<"45">>}, {46, <<"46">>}, {47, <<"47">>}, + {48, <<"48">>}, {49, <<"49">>}, {50, <<"50">>}, {51, <<"51">>}, + {52, <<"52">>}, {53, <<"53">>}, {54, <<"54">>}, {55, <<"55">>}, + {56, <<"56">>}, {57, <<"57">>}, {58, <<"58">>}, {59, <<"59">>} + ], + [{I, fun() -> O = pad_int(I) end} || {I, O} <- Tests]. + +-endif. diff --git a/rabbitmq-server/plugins-src/cowboy-wrapper/cowboy-git/src/cowboy_cookies.erl b/rabbitmq-server/plugins-src/cowboy-wrapper/cowboy-git/src/cowboy_cookies.erl new file mode 100644 index 0000000..7f5ab60 --- /dev/null +++ b/rabbitmq-server/plugins-src/cowboy-wrapper/cowboy-git/src/cowboy_cookies.erl @@ -0,0 +1,392 @@ +%% Copyright 2007 Mochi Media, Inc. +%% Copyright 2011 Thomas Burdick +%% +%% Permission to use, copy, modify, and/or distribute this software for any +%% purpose with or without fee is hereby granted, provided that the above +%% copyright notice and this permission notice appear in all copies. +%% +%% THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES +%% WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF +%% MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR +%% ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES +%% WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN +%% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF +%% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + +%% @doc HTTP Cookie parsing and generating (RFC 2965). + +-module(cowboy_cookies). + +-export([parse_cookie/1, cookie/3, cookie/2]). %% API. + +%% Types. +-type kv() :: {Name::binary(), Value::binary()}. +-type kvlist() :: [kv()]. +-type cookie_option() :: {max_age, integer()} + | {local_time, calendar:datetime()} + | {domain, binary()} | {path, binary()} + | {secure, true | false} | {http_only, true | false}. +-export_type([kv/0, kvlist/0, cookie_option/0]). + +-define(QUOTE, $\"). + +-include_lib("eunit/include/eunit.hrl"). + +%% API. + +%% @doc Parse the contents of a Cookie header field, ignoring cookie +%% attributes, and return a simple property list. +-spec parse_cookie(binary()) -> kvlist(). +parse_cookie(<<>>) -> + []; +parse_cookie(Cookie) when is_binary(Cookie) -> + parse_cookie(Cookie, []). + +%% @equiv cookie(Key, Value, []) +-spec cookie(binary(), binary()) -> kv(). +cookie(Key, Value) when is_binary(Key) andalso is_binary(Value) -> + cookie(Key, Value, []). + +%% @doc Generate a Set-Cookie header field tuple. +-spec cookie(binary(), binary(), [cookie_option()]) -> kv(). +cookie(Key, Value, Options) when is_binary(Key) + andalso is_binary(Value) andalso is_list(Options) -> + Cookie = <<(any_to_binary(Key))/binary, "=", + (quote(Value))/binary, "; Version=1">>, + %% Set-Cookie: + %% Comment, Domain, Max-Age, Path, Secure, Version + ExpiresPart = + case proplists:get_value(max_age, Options) of + undefined -> + <<"">>; + RawAge -> + When = case proplists:get_value(local_time, Options) of + undefined -> + calendar:local_time(); + LocalTime -> + LocalTime + end, + Age = case RawAge < 0 of + true -> + 0; + false -> + RawAge + end, + AgeBinary = quote(Age), + CookieDate = age_to_cookie_date(Age, When), + <<"; Expires=", CookieDate/binary, + "; Max-Age=", AgeBinary/binary>> + end, + SecurePart = + case proplists:get_value(secure, Options) of + true -> + <<"; Secure">>; + _ -> + <<"">> + end, + DomainPart = + case proplists:get_value(domain, Options) of + undefined -> + <<"">>; + Domain -> + <<"; Domain=", (quote(Domain))/binary>> + end, + PathPart = + case proplists:get_value(path, Options) of + undefined -> + <<"">>; + Path -> + <<"; Path=", (quote(Path))/binary>> + end, + HttpOnlyPart = + case proplists:get_value(http_only, Options) of + true -> + <<"; HttpOnly">>; + _ -> + <<"">> + end, + CookieParts = <>, + {<<"Set-Cookie">>, CookieParts}. + +%% Internal. + +%% @doc Check if a character is a white space character. +is_whitespace($\s) -> true; +is_whitespace($\t) -> true; +is_whitespace($\r) -> true; +is_whitespace($\n) -> true; +is_whitespace(_) -> false. + +%% @doc Check if a character is a seperator. +is_separator(C) when C < 32 -> true; +is_separator($\s) -> true; +is_separator($\t) -> true; +is_separator($() -> true; +is_separator($)) -> true; +is_separator($<) -> true; +is_separator($>) -> true; +is_separator($@) -> true; +is_separator($,) -> true; +is_separator($;) -> true; +is_separator($:) -> true; +is_separator($\\) -> true; +is_separator(?QUOTE) -> true; +is_separator($/) -> true; +is_separator($[) -> true; +is_separator($]) -> true; +is_separator($?) -> true; +is_separator($=) -> true; +is_separator(${) -> true; +is_separator($}) -> true; +is_separator(_) -> false. + +%% @doc Check if a binary has an ASCII seperator character. +has_seperator(<<>>) -> + false; +has_seperator(<<$/, Rest/binary>>) -> + has_seperator(Rest); +has_seperator(<>) -> + case is_separator(C) of + true -> + true; + false -> + has_seperator(Rest) + end. + +%% @doc Convert to a binary and raise an error if quoting is required. Quoting +%% is broken in different ways for different browsers. Its better to simply +%% avoiding doing it at all. +%% @end +-spec quote(term()) -> binary(). +quote(V0) -> + V = any_to_binary(V0), + case has_seperator(V) of + true -> + erlang:error({cookie_quoting_required, V}); + false -> + V + end. + +-spec add_seconds(integer(), calendar:datetime()) -> calendar:datetime(). +add_seconds(Secs, LocalTime) -> + Greg = calendar:datetime_to_gregorian_seconds(LocalTime), + calendar:gregorian_seconds_to_datetime(Greg + Secs). + +-spec age_to_cookie_date(integer(), calendar:datetime()) -> binary(). +age_to_cookie_date(Age, LocalTime) -> + cowboy_clock:rfc2109(add_seconds(Age, LocalTime)). + +-spec parse_cookie(binary(), kvlist()) -> kvlist(). +parse_cookie(<<>>, Acc) -> + lists:reverse(Acc); +parse_cookie(String, Acc) -> + {{Token, Value}, Rest} = read_pair(String), + Acc1 = case Token of + <<"">> -> + Acc; + <<"$", _R/binary>> -> + Acc; + _ -> + [{Token, Value} | Acc] + end, + parse_cookie(Rest, Acc1). + +-spec read_pair(binary()) -> {{binary(), binary()}, binary()}. +read_pair(String) -> + {Token, Rest} = read_token(skip_whitespace(String)), + {Value, Rest1} = read_value(skip_whitespace(Rest)), + {{Token, Value}, skip_past_separator(Rest1)}. + +-spec read_value(binary()) -> {binary(), binary()}. +read_value(<<"=", Value/binary>>) -> + Value1 = skip_whitespace(Value), + case Value1 of + <> -> + read_quoted(Value1); + _ -> + read_token(Value1) + end; +read_value(String) -> + {<<"">>, String}. + +-spec read_quoted(binary()) -> {binary(), binary()}. +read_quoted(<>) -> + read_quoted(String, <<"">>). + +-spec read_quoted(binary(), binary()) -> {binary(), binary()}. +read_quoted(<<"">>, Acc) -> + {Acc, <<"">>}; +read_quoted(<>, Acc) -> + {Acc, Rest}; +read_quoted(<<$\\, Any, Rest/binary>>, Acc) -> + read_quoted(Rest, <>); +read_quoted(<>, Acc) -> + read_quoted(Rest, <>). + +%% @doc Drop characters while a function returns true. +binary_dropwhile(_F, <<"">>) -> + <<"">>; +binary_dropwhile(F, String) -> + <> = String, + case F(C) of + true -> + binary_dropwhile(F, Rest); + false -> + String + end. + +%% @doc Remove leading whitespace. +-spec skip_whitespace(binary()) -> binary(). +skip_whitespace(String) -> + binary_dropwhile(fun is_whitespace/1, String). + +%% @doc Split a binary when the current character causes F to return true. +binary_splitwith(_F, Head, <<>>) -> + {Head, <<>>}; +binary_splitwith(F, Head, Tail) -> + <> = Tail, + case F(C) of + true -> + {Head, Tail}; + false -> + binary_splitwith(F, <>, NTail) + end. + +%% @doc Split a binary with a function returning true or false on each char. +binary_splitwith(F, String) -> + binary_splitwith(F, <<>>, String). + +%% @doc Split the binary when the next seperator is found. +-spec read_token(binary()) -> {binary(), binary()}. +read_token(String) -> + binary_splitwith(fun is_separator/1, String). + +%% @doc Return string after ; or , characters. +-spec skip_past_separator(binary()) -> binary(). +skip_past_separator(<<"">>) -> + <<"">>; +skip_past_separator(<<";", Rest/binary>>) -> + Rest; +skip_past_separator(<<",", Rest/binary>>) -> + Rest; +skip_past_separator(<<_C, Rest/binary>>) -> + skip_past_separator(Rest). + +-spec any_to_binary(binary() | string() | atom() | integer()) -> binary(). +any_to_binary(V) when is_binary(V) -> + V; +any_to_binary(V) when is_list(V) -> + erlang:list_to_binary(V); +any_to_binary(V) when is_atom(V) -> + erlang:atom_to_binary(V, latin1); +any_to_binary(V) when is_integer(V) -> + list_to_binary(integer_to_list(V)). + +%% Tests. + +-ifdef(TEST). + +quote_test() -> + %% ?assertError eunit macro is not compatible with coverage module + _ = try quote(<<":wq">>) + catch error:{cookie_quoting_required, <<":wq">>} -> ok + end, + ?assertEqual(<<"foo">>,quote(foo)), + ok. + +parse_cookie_test() -> + %% RFC example + C1 = <<"$Version=\"1\"; Customer=\"WILE_E_COYOTE\"; $Path=\"/acme\"; + Part_Number=\"Rocket_Launcher_0001\"; $Path=\"/acme\"; + Shipping=\"FedEx\"; $Path=\"/acme\"">>, + ?assertEqual( + [{<<"Customer">>,<<"WILE_E_COYOTE">>}, + {<<"Part_Number">>,<<"Rocket_Launcher_0001">>}, + {<<"Shipping">>,<<"FedEx">>}], + parse_cookie(C1)), + %% Potential edge cases + ?assertEqual( + [{<<"foo">>, <<"x">>}], + parse_cookie(<<"foo=\"\\x\"">>)), + ?assertEqual( + [], + parse_cookie(<<"=">>)), + ?assertEqual( + [{<<"foo">>, <<"">>}, {<<"bar">>, <<"">>}], + parse_cookie(<<" foo ; bar ">>)), + ?assertEqual( + [{<<"foo">>, <<"">>}, {<<"bar">>, <<"">>}], + parse_cookie(<<"foo=;bar=">>)), + ?assertEqual( + [{<<"foo">>, <<"\";">>}, {<<"bar">>, <<"">>}], + parse_cookie(<<"foo = \"\\\";\";bar ">>)), + ?assertEqual( + [{<<"foo">>, <<"\";bar">>}], + parse_cookie(<<"foo=\"\\\";bar">>)), + ?assertEqual( + [], + parse_cookie(<<"">>)), + ?assertEqual( + [{<<"foo">>, <<"bar">>}, {<<"baz">>, <<"wibble">>}], + parse_cookie(<<"foo=bar , baz=wibble ">>)), + ok. + +domain_test() -> + ?assertEqual( + {<<"Set-Cookie">>, + <<"Customer=WILE_E_COYOTE; " + "Version=1; " + "Domain=acme.com; " + "HttpOnly">>}, + cookie(<<"Customer">>, <<"WILE_E_COYOTE">>, + [{http_only, true}, {domain, <<"acme.com">>}])), + ok. + +local_time_test() -> + {<<"Set-Cookie">>, B} = cookie(<<"Customer">>, <<"WILE_E_COYOTE">>, + [{max_age, 111}, {secure, true}]), + + ?assertMatch( + [<<"Customer=WILE_E_COYOTE">>, + <<" Version=1">>, + <<" Expires=", _R/binary>>, + <<" Max-Age=111">>, + <<" Secure">>], + binary:split(B, <<";">>, [global])), + ok. + +-spec cookie_test() -> no_return(). %% Not actually true, just a bad option. +cookie_test() -> + C1 = {<<"Set-Cookie">>, + <<"Customer=WILE_E_COYOTE; " + "Version=1; " + "Path=/acme">>}, + C1 = cookie(<<"Customer">>, <<"WILE_E_COYOTE">>, [{path, <<"/acme">>}]), + + C1 = cookie(<<"Customer">>, <<"WILE_E_COYOTE">>, + [{path, <<"/acme">>}, {badoption, <<"negatory">>}]), + + {<<"Set-Cookie">>,<<"=NoKey; Version=1">>} + = cookie(<<"">>, <<"NoKey">>, []), + {<<"Set-Cookie">>,<<"=NoKey; Version=1">>} + = cookie(<<"">>, <<"NoKey">>), + LocalTime = calendar:universal_time_to_local_time( + {{2007, 5, 15}, {13, 45, 33}}), + C2 = {<<"Set-Cookie">>, + <<"Customer=WILE_E_COYOTE; " + "Version=1; " + "Expires=Tue, 15 May 2007 13:45:33 GMT; " + "Max-Age=0">>}, + C2 = cookie(<<"Customer">>, <<"WILE_E_COYOTE">>, + [{max_age, -111}, {local_time, LocalTime}]), + C3 = {<<"Set-Cookie">>, + <<"Customer=WILE_E_COYOTE; " + "Version=1; " + "Expires=Wed, 16 May 2007 13:45:50 GMT; " + "Max-Age=86417">>}, + C3 = cookie(<<"Customer">>, <<"WILE_E_COYOTE">>, + [{max_age, 86417}, {local_time, LocalTime}]), + ok. + +-endif. diff --git a/rabbitmq-server/plugins-src/cowboy-wrapper/cowboy-git/src/cowboy_dispatcher.erl b/rabbitmq-server/plugins-src/cowboy-wrapper/cowboy-git/src/cowboy_dispatcher.erl new file mode 100644 index 0000000..22f6e1e --- /dev/null +++ b/rabbitmq-server/plugins-src/cowboy-wrapper/cowboy-git/src/cowboy_dispatcher.erl @@ -0,0 +1,309 @@ +%% Copyright (c) 2011, Loïc Hoguin +%% Copyright (c) 2011, Anthony Ramine +%% +%% Permission to use, copy, modify, and/or distribute this software for any +%% purpose with or without fee is hereby granted, provided that the above +%% copyright notice and this permission notice appear in all copies. +%% +%% THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES +%% WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF +%% MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR +%% ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES +%% WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN +%% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF +%% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + +%% @doc Dispatch requests according to a hostname and path. +-module(cowboy_dispatcher). + +-export([split_host/1, split_path/2, match/3]). %% API. + +-type bindings() :: list({atom(), binary()}). +-type tokens() :: list(binary()). +-type match_rule() :: '_' | '*' | list(binary() | '_' | '...' | atom()). +-type dispatch_path() :: list({match_rule(), module(), any()}). +-type dispatch_rule() :: {Host::match_rule(), Path::dispatch_path()}. +-type dispatch_rules() :: list(dispatch_rule()). + +-export_type([bindings/0, tokens/0, dispatch_rules/0]). + +-include_lib("eunit/include/eunit.hrl"). + +%% API. + +%% @doc Split a hostname into a list of tokens. +-spec split_host(binary()) + -> {tokens(), binary(), undefined | inet:ip_port()}. +split_host(<<>>) -> + {[], <<>>, undefined}; +split_host(Host) -> + case binary:split(Host, <<":">>) of + [Host] -> + {binary:split(Host, <<".">>, [global, trim]), Host, undefined}; + [Host2, Port] -> + {binary:split(Host2, <<".">>, [global, trim]), Host2, + list_to_integer(binary_to_list(Port))} + end. + +%% @doc Split a path into a list of path segments. +%% +%% Following RFC2396, this function may return path segments containing any +%% character, including / if, and only if, a / was escaped +%% and part of a path segment. +-spec split_path(binary(), fun((binary()) -> binary())) -> + {tokens(), binary(), binary()}. +split_path(Path, URLDec) -> + case binary:split(Path, <<"?">>) of + [Path] -> {do_split_path(Path, <<"/">>, URLDec), Path, <<>>}; + [<<>>, Qs] -> {[], <<>>, Qs}; + [Path2, Qs] -> {do_split_path(Path2, <<"/">>, URLDec), Path2, Qs} + end. + +-spec do_split_path(binary(), <<_:8>>, fun((binary()) -> binary())) -> tokens(). +do_split_path(RawPath, Separator, URLDec) -> + EncodedPath = case binary:split(RawPath, Separator, [global, trim]) of + [<<>>|Path] -> Path; + Path -> Path + end, + [URLDec(Token) || Token <- EncodedPath]. + +%% @doc Match hostname tokens and path tokens against dispatch rules. +%% +%% It is typically used for matching tokens for the hostname and path of +%% the request against a global dispatch rule for your listener. +%% +%% Dispatch rules are a list of {Hostname, PathRules} tuples, with +%% PathRules being a list of {Path, HandlerMod, HandlerOpts}. +%% +%% Hostname and Path are match rules and can be either the +%% atom '_', which matches everything for a single token, the atom +%% '*', which matches everything for the rest of the tokens, or a +%% list of tokens. Each token can be either a binary, the atom '_', +%% the atom '...' or a named atom. A binary token must match exactly, +%% '_' matches everything for a single token, '...' matches +%% everything for the rest of the tokens and a named atom will bind the +%% corresponding token value and return it. +%% +%% The list of hostname tokens is reversed before matching. For example, if +%% we were to match "www.dev-extend.eu", we would first match "eu", then +%% "dev-extend", then "www". This means that in the context of hostnames, +%% the '...' atom matches properly the lower levels of the domain +%% as would be expected. +%% +%% When a result is found, this function will return the handler module and +%% options found in the dispatch list, a key-value list of bindings and +%% the tokens that were matched by the '...' atom for both the +%% hostname and path. +-spec match(Host::tokens(), Path::tokens(), dispatch_rules()) + -> {ok, module(), any(), bindings(), + HostInfo::undefined | tokens(), + PathInfo::undefined | tokens()} + | {error, notfound, host} | {error, notfound, path}. +match(_Host, _Path, []) -> + {error, notfound, host}; +match(_Host, Path, [{'_', PathMatchs}|_Tail]) -> + match_path(Path, PathMatchs, [], undefined); +match(Host, Path, [{HostMatch, PathMatchs}|Tail]) -> + case try_match(host, Host, HostMatch) of + false -> + match(Host, Path, Tail); + {true, HostBinds, undefined} -> + match_path(Path, PathMatchs, HostBinds, undefined); + {true, HostBinds, HostInfo} -> + match_path(Path, PathMatchs, HostBinds, lists:reverse(HostInfo)) + end. + +-spec match_path(tokens(), dispatch_path(), bindings(), + HostInfo::undefined | tokens()) + -> {ok, module(), any(), bindings(), + HostInfo::undefined | tokens(), + PathInfo::undefined | tokens()} + | {error, notfound, path}. +match_path(_Path, [], _HostBinds, _HostInfo) -> + {error, notfound, path}; +match_path(_Path, [{'_', Handler, Opts}|_Tail], HostBinds, HostInfo) -> + {ok, Handler, Opts, HostBinds, HostInfo, undefined}; +match_path('*', [{'*', Handler, Opts}|_Tail], HostBinds, HostInfo) -> + {ok, Handler, Opts, HostBinds, HostInfo, undefined}; +match_path(Path, [{PathMatch, Handler, Opts}|Tail], HostBinds, HostInfo) -> + case try_match(path, Path, PathMatch) of + false -> + match_path(Path, Tail, HostBinds, HostInfo); + {true, PathBinds, PathInfo} -> + {ok, Handler, Opts, HostBinds ++ PathBinds, HostInfo, PathInfo} + end. + +%% Internal. + +-spec try_match(host | path, tokens(), match_rule()) + -> {true, bindings(), undefined | tokens()} | false. +try_match(host, List, Match) -> + list_match(lists:reverse(List), lists:reverse(Match), []); +try_match(path, List, Match) -> + list_match(List, Match, []). + +-spec list_match(tokens(), match_rule(), bindings()) + -> {true, bindings(), undefined | tokens()} | false. +%% Atom '...' matches any trailing path, stop right now. +list_match(List, ['...'], Binds) -> + {true, Binds, List}; +%% Atom '_' matches anything, continue. +list_match([_E|Tail], ['_'|TailMatch], Binds) -> + list_match(Tail, TailMatch, Binds); +%% Both values match, continue. +list_match([E|Tail], [E|TailMatch], Binds) -> + list_match(Tail, TailMatch, Binds); +%% Bind E to the variable name V and continue. +list_match([E|Tail], [V|TailMatch], Binds) when is_atom(V) -> + list_match(Tail, TailMatch, [{V, E}|Binds]); +%% Match complete. +list_match([], [], Binds) -> + {true, Binds, undefined}; +%% Values don't match, stop. +list_match(_List, _Match, _Binds) -> + false. + +%% Tests. + +-ifdef(TEST). + +split_host_test_() -> + %% {Host, Result} + Tests = [ + {<<"">>, {[], <<"">>, undefined}}, + {<<".........">>, {[], <<".........">>, undefined}}, + {<<"*">>, {[<<"*">>], <<"*">>, undefined}}, + {<<"cowboy.dev-extend.eu">>, + {[<<"cowboy">>, <<"dev-extend">>, <<"eu">>], + <<"cowboy.dev-extend.eu">>, undefined}}, + {<<"dev-extend..eu">>, + {[<<"dev-extend">>, <<>>, <<"eu">>], + <<"dev-extend..eu">>, undefined}}, + {<<"dev-extend.eu">>, + {[<<"dev-extend">>, <<"eu">>], <<"dev-extend.eu">>, undefined}}, + {<<"dev-extend.eu:8080">>, + {[<<"dev-extend">>, <<"eu">>], <<"dev-extend.eu">>, 8080}}, + {<<"a.b.c.d.e.f.g.h.i.j.k.l.m.n.o.p.q.r.s.t.u.v.w.x.y.z">>, + {[<<"a">>, <<"b">>, <<"c">>, <<"d">>, <<"e">>, <<"f">>, <<"g">>, + <<"h">>, <<"i">>, <<"j">>, <<"k">>, <<"l">>, <<"m">>, <<"n">>, + <<"o">>, <<"p">>, <<"q">>, <<"r">>, <<"s">>, <<"t">>, <<"u">>, + <<"v">>, <<"w">>, <<"x">>, <<"y">>, <<"z">>], + <<"a.b.c.d.e.f.g.h.i.j.k.l.m.n.o.p.q.r.s.t.u.v.w.x.y.z">>, + undefined}} + ], + [{H, fun() -> R = split_host(H) end} || {H, R} <- Tests]. + +split_host_fail_test_() -> + Tests = [ + <<"dev-extend.eu:owns">>, + <<"dev-extend.eu: owns">>, + <<"dev-extend.eu:42fun">>, + <<"dev-extend.eu: 42fun">>, + <<"dev-extend.eu:42 fun">>, + <<"dev-extend.eu:fun 42">>, + <<"dev-extend.eu: 42">>, + <<":owns">>, + <<":42 fun">> + ], + [{H, fun() -> case catch split_host(H) of + {'EXIT', _Reason} -> ok + end end} || H <- Tests]. + +split_path_test_() -> + %% {Path, Result, QueryString} + Tests = [ + {<<"?">>, [], <<"">>, <<"">>}, + {<<"???">>, [], <<"">>, <<"??">>}, + {<<"/">>, [], <<"/">>, <<"">>}, + {<<"/users">>, [<<"users">>], <<"/users">>, <<"">>}, + {<<"/users?">>, [<<"users">>], <<"/users">>, <<"">>}, + {<<"/users?a">>, [<<"users">>], <<"/users">>, <<"a">>}, + {<<"/users/42/friends?a=b&c=d&e=notsure?whatever">>, + [<<"users">>, <<"42">>, <<"friends">>], + <<"/users/42/friends">>, <<"a=b&c=d&e=notsure?whatever">>}, + {<<"/users/a+b/c%21d?e+f=g+h">>, + [<<"users">>, <<"a b">>, <<"c!d">>], + <<"/users/a+b/c%21d">>, <<"e+f=g+h">>} + ], + URLDecode = fun(Bin) -> cowboy_http:urldecode(Bin, crash) end, + [{P, fun() -> {R, RawP, Qs} = split_path(P, URLDecode) end} + || {P, R, RawP, Qs} <- Tests]. + +match_test_() -> + Dispatch = [ + {[<<"www">>, '_', <<"dev-extend">>, <<"eu">>], [ + {[<<"users">>, '_', <<"mails">>], match_any_subdomain_users, []} + ]}, + {[<<"dev-extend">>, <<"eu">>], [ + {[<<"users">>, id, <<"friends">>], match_extend_users_friends, []}, + {'_', match_extend, []} + ]}, + {[<<"dev-extend">>, var], [ + {[<<"threads">>, var], match_duplicate_vars, + [we, {expect, two}, var, here]} + ]}, + {[<<"erlang">>, ext], [ + {'_', match_erlang_ext, []} + ]}, + {'_', [ + {[<<"users">>, id, <<"friends">>], match_users_friends, []}, + {'_', match_any, []} + ]} + ], + %% {Host, Path, Result} + Tests = [ + {[<<"any">>], [], {ok, match_any, [], []}}, + {[<<"www">>, <<"any">>, <<"dev-extend">>, <<"eu">>], + [<<"users">>, <<"42">>, <<"mails">>], + {ok, match_any_subdomain_users, [], []}}, + {[<<"www">>, <<"dev-extend">>, <<"eu">>], + [<<"users">>, <<"42">>, <<"mails">>], {ok, match_any, [], []}}, + {[<<"www">>, <<"dev-extend">>, <<"eu">>], [], {ok, match_any, [], []}}, + {[<<"www">>, <<"any">>, <<"dev-extend">>, <<"eu">>], + [<<"not_users">>, <<"42">>, <<"mails">>], {error, notfound, path}}, + {[<<"dev-extend">>, <<"eu">>], [], {ok, match_extend, [], []}}, + {[<<"dev-extend">>, <<"eu">>], [<<"users">>, <<"42">>, <<"friends">>], + {ok, match_extend_users_friends, [], [{id, <<"42">>}]}}, + {[<<"erlang">>, <<"fr">>], '_', + {ok, match_erlang_ext, [], [{ext, <<"fr">>}]}}, + {[<<"any">>], [<<"users">>, <<"444">>, <<"friends">>], + {ok, match_users_friends, [], [{id, <<"444">>}]}}, + {[<<"dev-extend">>, <<"fr">>], [<<"threads">>, <<"987">>], + {ok, match_duplicate_vars, [we, {expect, two}, var, here], + [{var, <<"fr">>}, {var, <<"987">>}]}} + ], + [{lists:flatten(io_lib:format("~p, ~p", [H, P])), fun() -> + {ok, Handler, Opts, Binds, undefined, undefined} = match(H, P, Dispatch) + end} || {H, P, {ok, Handler, Opts, Binds}} <- Tests]. + +match_info_test_() -> + Dispatch = [ + {[<<"www">>, <<"dev-extend">>, <<"eu">>], [ + {[<<"pathinfo">>, <<"is">>, <<"next">>, '...'], match_path, []} + ]}, + {['...', <<"dev-extend">>, <<"eu">>], [ + {'_', match_any, []} + ]} + ], + Tests = [ + {[<<"dev-extend">>, <<"eu">>], [], + {ok, match_any, [], [], [], undefined}}, + {[<<"bugs">>, <<"dev-extend">>, <<"eu">>], [], + {ok, match_any, [], [], [<<"bugs">>], undefined}}, + {[<<"cowboy">>, <<"bugs">>, <<"dev-extend">>, <<"eu">>], [], + {ok, match_any, [], [], [<<"cowboy">>, <<"bugs">>], undefined}}, + {[<<"www">>, <<"dev-extend">>, <<"eu">>], + [<<"pathinfo">>, <<"is">>, <<"next">>], + {ok, match_path, [], [], undefined, []}}, + {[<<"www">>, <<"dev-extend">>, <<"eu">>], + [<<"pathinfo">>, <<"is">>, <<"next">>, <<"path_info">>], + {ok, match_path, [], [], undefined, [<<"path_info">>]}}, + {[<<"www">>, <<"dev-extend">>, <<"eu">>], + [<<"pathinfo">>, <<"is">>, <<"next">>, <<"foo">>, <<"bar">>], + {ok, match_path, [], [], undefined, [<<"foo">>, <<"bar">>]}} + ], + [{lists:flatten(io_lib:format("~p, ~p", [H, P])), fun() -> + R = match(H, P, Dispatch) + end} || {H, P, R} <- Tests]. + +-endif. diff --git a/rabbitmq-server/plugins-src/cowboy-wrapper/cowboy-git/src/cowboy_http.erl b/rabbitmq-server/plugins-src/cowboy-wrapper/cowboy-git/src/cowboy_http.erl new file mode 100644 index 0000000..d7261c8 --- /dev/null +++ b/rabbitmq-server/plugins-src/cowboy-wrapper/cowboy-git/src/cowboy_http.erl @@ -0,0 +1,974 @@ +%% Copyright (c) 2011, Loïc Hoguin +%% Copyright (c) 2011, Anthony Ramine +%% +%% Permission to use, copy, modify, and/or distribute this software for any +%% purpose with or without fee is hereby granted, provided that the above +%% copyright notice and this permission notice appear in all copies. +%% +%% THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES +%% WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF +%% MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR +%% ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES +%% WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN +%% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF +%% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + +%% @doc Core HTTP parsing API. +-module(cowboy_http). + +%% Parsing. +-export([list/2, nonempty_list/2, content_type/1, content_type_params/3, + media_range/2, conneg/2, language_range/2, entity_tag_match/1, + http_date/1, rfc1123_date/1, rfc850_date/1, asctime_date/1, + whitespace/2, digits/1, token/2, token_ci/2, quoted_string/2]). + +%% Interpretation. +-export([connection_to_atom/1, urldecode/1, urldecode/2, urlencode/1, + urlencode/2]). + +-type method() :: 'OPTIONS' | 'GET' | 'HEAD' + | 'POST' | 'PUT' | 'DELETE' | 'TRACE' | binary(). +-type uri() :: '*' | {absoluteURI, http | https, Host::binary(), + Port::integer() | undefined, Path::binary()} + | {scheme, Scheme::binary(), binary()} + | {abs_path, binary()} | binary(). +-type version() :: {Major::non_neg_integer(), Minor::non_neg_integer()}. +-type header() :: 'Cache-Control' | 'Connection' | 'Date' | 'Pragma' + | 'Transfer-Encoding' | 'Upgrade' | 'Via' | 'Accept' | 'Accept-Charset' + | 'Accept-Encoding' | 'Accept-Language' | 'Authorization' | 'From' | 'Host' + | 'If-Modified-Since' | 'If-Match' | 'If-None-Match' | 'If-Range' + | 'If-Unmodified-Since' | 'Max-Forwards' | 'Proxy-Authorization' | 'Range' + | 'Referer' | 'User-Agent' | 'Age' | 'Location' | 'Proxy-Authenticate' + | 'Public' | 'Retry-After' | 'Server' | 'Vary' | 'Warning' + | 'Www-Authenticate' | 'Allow' | 'Content-Base' | 'Content-Encoding' + | 'Content-Language' | 'Content-Length' | 'Content-Location' + | 'Content-Md5' | 'Content-Range' | 'Content-Type' | 'Etag' + | 'Expires' | 'Last-Modified' | 'Accept-Ranges' | 'Set-Cookie' + | 'Set-Cookie2' | 'X-Forwarded-For' | 'Cookie' | 'Keep-Alive' + | 'Proxy-Connection' | binary(). +-type fake_iodata() :: iolist() | binary(). +-type headers() :: [{header(), fake_iodata()}]. +-type status() :: non_neg_integer() | binary(). + +-export_type([method/0, uri/0, version/0, header/0, headers/0, status/0]). + +-include("include/http.hrl"). +-include_lib("eunit/include/eunit.hrl"). + +%% Parsing. + +%% @doc Parse a non-empty list of the given type. +-spec nonempty_list(binary(), fun()) -> [any(), ...] | {error, badarg}. +nonempty_list(Data, Fun) -> + case list(Data, Fun, []) of + {error, badarg} -> {error, badarg}; + [] -> {error, badarg}; + L -> lists:reverse(L) + end. + +%% @doc Parse a list of the given type. +-spec list(binary(), fun()) -> list() | {error, badarg}. +list(Data, Fun) -> + case list(Data, Fun, []) of + {error, badarg} -> {error, badarg}; + L -> lists:reverse(L) + end. + +-spec list(binary(), fun(), [binary()]) -> [any()] | {error, badarg}. +%% From the RFC: +%%
Wherever this construct is used, null elements are allowed, +%% but do not contribute to the count of elements present. +%% That is, "(element), , (element) " is permitted, but counts +%% as only two elements. Therefore, where at least one element is required, +%% at least one non-null element MUST be present.
+list(Data, Fun, Acc) -> + whitespace(Data, + fun (<<>>) -> Acc; + (<< $,, Rest/binary >>) -> list(Rest, Fun, Acc); + (Rest) -> Fun(Rest, + fun (D, I) -> whitespace(D, + fun (<<>>) -> [I|Acc]; + (<< $,, R/binary >>) -> list(R, Fun, [I|Acc]); + (_Any) -> {error, badarg} + end) + end) + end). + +%% @doc Parse a content type. +-spec content_type(binary()) -> any(). +content_type(Data) -> + media_type(Data, + fun (Rest, Type, SubType) -> + content_type_params(Rest, + fun (Params) -> {Type, SubType, Params} end, []) + end). + +-spec content_type_params(binary(), fun(), list({binary(), binary()})) + -> any(). +content_type_params(Data, Fun, Acc) -> + whitespace(Data, + fun (<< $;, Rest/binary >>) -> content_type_param(Rest, Fun, Acc); + (<<>>) -> Fun(lists:reverse(Acc)); + (_Rest) -> {error, badarg} + end). + +-spec content_type_param(binary(), fun(), list({binary(), binary()})) + -> any(). +content_type_param(Data, Fun, Acc) -> + whitespace(Data, + fun (Rest) -> + token_ci(Rest, + fun (_Rest2, <<>>) -> {error, badarg}; + (<< $=, Rest2/binary >>, Attr) -> + word(Rest2, + fun (Rest3, Value) -> + content_type_params(Rest3, Fun, + [{Attr, Value}|Acc]) + end); + (_Rest2, _Attr) -> {error, badarg} + end) + end). + +%% @doc Parse a media range. +-spec media_range(binary(), fun()) -> any(). +media_range(Data, Fun) -> + media_type(Data, + fun (Rest, Type, SubType) -> + media_range_params(Rest, Fun, Type, SubType, []) + end). + +-spec media_range_params(binary(), fun(), binary(), binary(), + [{binary(), binary()}]) -> any(). +media_range_params(Data, Fun, Type, SubType, Acc) -> + whitespace(Data, + fun (<< $;, Rest/binary >>) -> + whitespace(Rest, + fun (Rest2) -> + media_range_param_attr(Rest2, Fun, Type, SubType, Acc) + end); + (Rest) -> Fun(Rest, {{Type, SubType, lists:reverse(Acc)}, 1000, []}) + end). + +-spec media_range_param_attr(binary(), fun(), binary(), binary(), + [{binary(), binary()}]) -> any(). +media_range_param_attr(Data, Fun, Type, SubType, Acc) -> + token_ci(Data, + fun (_Rest, <<>>) -> {error, badarg}; + (<< $=, Rest/binary >>, Attr) -> + media_range_param_value(Rest, Fun, Type, SubType, Acc, Attr) + end). + +-spec media_range_param_value(binary(), fun(), binary(), binary(), + [{binary(), binary()}], binary()) -> any(). +media_range_param_value(Data, Fun, Type, SubType, Acc, <<"q">>) -> + qvalue(Data, + fun (Rest, Quality) -> + accept_ext(Rest, Fun, Type, SubType, Acc, Quality, []) + end); +media_range_param_value(Data, Fun, Type, SubType, Acc, Attr) -> + word(Data, + fun (Rest, Value) -> + media_range_params(Rest, Fun, + Type, SubType, [{Attr, Value}|Acc]) + end). + +%% @doc Parse a media type. +-spec media_type(binary(), fun()) -> any(). +media_type(Data, Fun) -> + token_ci(Data, + fun (_Rest, <<>>) -> {error, badarg}; + (<< $/, Rest/binary >>, Type) -> + token_ci(Rest, + fun (_Rest2, <<>>) -> {error, badarg}; + (Rest2, SubType) -> Fun(Rest2, Type, SubType) + end); + (_Rest, _Type) -> {error, badarg} + end). + +-spec accept_ext(binary(), fun(), binary(), binary(), + [{binary(), binary()}], 0..1000, + [{binary(), binary()} | binary()]) -> any(). +accept_ext(Data, Fun, Type, SubType, Params, Quality, Acc) -> + whitespace(Data, + fun (<< $;, Rest/binary >>) -> + whitespace(Rest, + fun (Rest2) -> + accept_ext_attr(Rest2, Fun, + Type, SubType, Params, Quality, Acc) + end); + (Rest) -> + Fun(Rest, {{Type, SubType, lists:reverse(Params)}, + Quality, lists:reverse(Acc)}) + end). + +-spec accept_ext_attr(binary(), fun(), binary(), binary(), + [{binary(), binary()}], 0..1000, + [{binary(), binary()} | binary()]) -> any(). +accept_ext_attr(Data, Fun, Type, SubType, Params, Quality, Acc) -> + token_ci(Data, + fun (_Rest, <<>>) -> {error, badarg}; + (<< $=, Rest/binary >>, Attr) -> + accept_ext_value(Rest, Fun, Type, SubType, Params, + Quality, Acc, Attr); + (Rest, Attr) -> + accept_ext(Rest, Fun, Type, SubType, Params, + Quality, [Attr|Acc]) + end). + +-spec accept_ext_value(binary(), fun(), binary(), binary(), + [{binary(), binary()}], 0..1000, + [{binary(), binary()} | binary()], binary()) -> any(). +accept_ext_value(Data, Fun, Type, SubType, Params, Quality, Acc, Attr) -> + word(Data, + fun (Rest, Value) -> + accept_ext(Rest, Fun, + Type, SubType, Params, Quality, [{Attr, Value}|Acc]) + end). + +%% @doc Parse a conneg header (Accept-Charset, Accept-Encoding), +%% followed by an optional quality value. +-spec conneg(binary(), fun()) -> any(). +conneg(Data, Fun) -> + token_ci(Data, + fun (_Rest, <<>>) -> {error, badarg}; + (Rest, Conneg) -> + maybe_qparam(Rest, + fun (Rest2, Quality) -> + Fun(Rest2, {Conneg, Quality}) + end) + end). + +%% @doc Parse a language range, followed by an optional quality value. +-spec language_range(binary(), fun()) -> any(). +language_range(<< $*, Rest/binary >>, Fun) -> + language_range_ret(Rest, Fun, '*'); +language_range(Data, Fun) -> + language_tag(Data, + fun (Rest, LanguageTag) -> + language_range_ret(Rest, Fun, LanguageTag) + end). + +-spec language_range_ret(binary(), fun(), '*' | {binary(), [binary()]}) -> any(). +language_range_ret(Data, Fun, LanguageTag) -> + maybe_qparam(Data, + fun (Rest, Quality) -> + Fun(Rest, {LanguageTag, Quality}) + end). + +-spec language_tag(binary(), fun()) -> any(). +language_tag(Data, Fun) -> + alpha(Data, + fun (_Rest, Tag) when byte_size(Tag) =:= 0; byte_size(Tag) > 8 -> + {error, badarg}; + (<< $-, Rest/binary >>, Tag) -> + language_subtag(Rest, Fun, Tag, []); + (Rest, Tag) -> + Fun(Rest, Tag) + end). + +-spec language_subtag(binary(), fun(), binary(), [binary()]) -> any(). +language_subtag(Data, Fun, Tag, Acc) -> + alpha(Data, + fun (_Rest, SubTag) when byte_size(SubTag) =:= 0; + byte_size(SubTag) > 8 -> {error, badarg}; + (<< $-, Rest/binary >>, SubTag) -> + language_subtag(Rest, Fun, Tag, [SubTag|Acc]); + (Rest, SubTag) -> + %% Rebuild the full tag now that we know it's correct + Sub = << << $-, S/binary >> || S <- lists:reverse([SubTag|Acc]) >>, + Fun(Rest, << Tag/binary, Sub/binary >>) + end). + +-spec maybe_qparam(binary(), fun()) -> any(). +maybe_qparam(Data, Fun) -> + whitespace(Data, + fun (<< $;, Rest/binary >>) -> + whitespace(Rest, + fun (Rest2) -> + qparam(Rest2, Fun) + end); + (Rest) -> + Fun(Rest, 1000) + end). + +%% @doc Parse a quality parameter string (for example q=0.500). +-spec qparam(binary(), fun()) -> any(). +qparam(<< Q, $=, Data/binary >>, Fun) when Q =:= $q; Q =:= $Q -> + qvalue(Data, Fun). + +%% @doc Parse either a list of entity tags or a "*". +-spec entity_tag_match(binary()) -> any(). +entity_tag_match(<< $*, Rest/binary >>) -> + whitespace(Rest, + fun (<<>>) -> '*'; + (_Any) -> {error, badarg} + end); +entity_tag_match(Data) -> + nonempty_list(Data, fun entity_tag/2). + +%% @doc Parse an entity-tag. +-spec entity_tag(binary(), fun()) -> any(). +entity_tag(<< "W/", Rest/binary >>, Fun) -> + opaque_tag(Rest, Fun, weak); +entity_tag(Data, Fun) -> + opaque_tag(Data, Fun, strong). + +-spec opaque_tag(binary(), fun(), weak | strong) -> any(). +opaque_tag(Data, Fun, Strength) -> + quoted_string(Data, + fun (_Rest, <<>>) -> {error, badarg}; + (Rest, OpaqueTag) -> Fun(Rest, {Strength, OpaqueTag}) + end). + +%% @doc Parse an HTTP date (RFC1123, RFC850 or asctime date). +%% @end +%% +%% While this may not be the most efficient date parsing we can do, +%% it should work fine for our purposes because all HTTP dates should +%% be sent as RFC1123 dates in HTTP/1.1. +-spec http_date(binary()) -> any(). +http_date(Data) -> + case rfc1123_date(Data) of + {error, badarg} -> + case rfc850_date(Data) of + {error, badarg} -> + case asctime_date(Data) of + {error, badarg} -> + {error, badarg}; + HTTPDate -> + HTTPDate + end; + HTTPDate -> + HTTPDate + end; + HTTPDate -> + HTTPDate + end. + +%% @doc Parse an RFC1123 date. +-spec rfc1123_date(binary()) -> any(). +rfc1123_date(Data) -> + wkday(Data, + fun (<< ", ", Rest/binary >>, _WkDay) -> + date1(Rest, + fun (<< " ", Rest2/binary >>, Date) -> + time(Rest2, + fun (<< " GMT", Rest3/binary >>, Time) -> + http_date_ret(Rest3, {Date, Time}); + (_Any, _Time) -> + {error, badarg} + end); + (_Any, _Date) -> + {error, badarg} + end); + (_Any, _WkDay) -> + {error, badarg} + end). + +%% @doc Parse an RFC850 date. +-spec rfc850_date(binary()) -> any(). +%% From the RFC: +%% HTTP/1.1 clients and caches SHOULD assume that an RFC-850 date +%% which appears to be more than 50 years in the future is in fact +%% in the past (this helps solve the "year 2000" problem). +rfc850_date(Data) -> + weekday(Data, + fun (<< ", ", Rest/binary >>, _WeekDay) -> + date2(Rest, + fun (<< " ", Rest2/binary >>, Date) -> + time(Rest2, + fun (<< " GMT", Rest3/binary >>, Time) -> + http_date_ret(Rest3, {Date, Time}); + (_Any, _Time) -> + {error, badarg} + end); + (_Any, _Date) -> + {error, badarg} + end); + (_Any, _WeekDay) -> + {error, badarg} + end). + +%% @doc Parse an asctime date. +-spec asctime_date(binary()) -> any(). +asctime_date(Data) -> + wkday(Data, + fun (<< " ", Rest/binary >>, _WkDay) -> + date3(Rest, + fun (<< " ", Rest2/binary >>, PartialDate) -> + time(Rest2, + fun (<< " ", Rest3/binary >>, Time) -> + asctime_year(Rest3, + PartialDate, Time); + (_Any, _Time) -> + {error, badarg} + end); + (_Any, _PartialDate) -> + {error, badarg} + end); + (_Any, _WkDay) -> + {error, badarg1} + end). + +-spec asctime_year(binary(), tuple(), tuple()) -> any(). +asctime_year(<< Y1, Y2, Y3, Y4, Rest/binary >>, {Month, Day}, Time) + when Y1 >= $0, Y1 =< $9, Y2 >= $0, Y2 =< $9, + Y3 >= $0, Y3 =< $9, Y4 >= $0, Y4 =< $9 -> + Year = (Y1 - $0) * 1000 + (Y2 - $0) * 100 + (Y3 - $0) * 10 + (Y4 - $0), + http_date_ret(Rest, {{Year, Month, Day}, Time}). + +-spec http_date_ret(binary(), tuple()) -> any(). +http_date_ret(Data, DateTime = {Date, _Time}) -> + whitespace(Data, + fun (<<>>) -> + case calendar:valid_date(Date) of + true -> DateTime; + false -> {error, badarg} + end; + (_Any) -> + {error, badarg} + end). + +%% We never use it, pretty much just checks the wkday is right. +-spec wkday(binary(), fun()) -> any(). +wkday(<< WkDay:3/binary, Rest/binary >>, Fun) + when WkDay =:= <<"Mon">>; WkDay =:= <<"Tue">>; WkDay =:= <<"Wed">>; + WkDay =:= <<"Thu">>; WkDay =:= <<"Fri">>; WkDay =:= <<"Sat">>; + WkDay =:= <<"Sun">> -> + Fun(Rest, WkDay); +wkday(_Any, _Fun) -> + {error, badarg}. + +%% We never use it, pretty much just checks the weekday is right. +-spec weekday(binary(), fun()) -> any(). +weekday(<< "Monday", Rest/binary >>, Fun) -> + Fun(Rest, <<"Monday">>); +weekday(<< "Tuesday", Rest/binary >>, Fun) -> + Fun(Rest, <<"Tuesday">>); +weekday(<< "Wednesday", Rest/binary >>, Fun) -> + Fun(Rest, <<"Wednesday">>); +weekday(<< "Thursday", Rest/binary >>, Fun) -> + Fun(Rest, <<"Thursday">>); +weekday(<< "Friday", Rest/binary >>, Fun) -> + Fun(Rest, <<"Friday">>); +weekday(<< "Saturday", Rest/binary >>, Fun) -> + Fun(Rest, <<"Saturday">>); +weekday(<< "Sunday", Rest/binary >>, Fun) -> + Fun(Rest, <<"Sunday">>); +weekday(_Any, _Fun) -> + {error, badarg}. + +-spec date1(binary(), fun()) -> any(). +date1(<< D1, D2, " ", M:3/binary, " ", Y1, Y2, Y3, Y4, Rest/binary >>, Fun) + when D1 >= $0, D1 =< $9, D2 >= $0, D2 =< $9, + Y1 >= $0, Y1 =< $9, Y2 >= $0, Y2 =< $9, + Y3 >= $0, Y3 =< $9, Y4 >= $0, Y4 =< $9 -> + case month(M) of + {error, badarg} -> + {error, badarg}; + Month -> + Fun(Rest, { + (Y1 - $0) * 1000 + (Y2 - $0) * 100 + (Y3 - $0) * 10 + (Y4 - $0), + Month, + (D1 - $0) * 10 + (D2 - $0) + }) + end; +date1(_Data, _Fun) -> + {error, badarg}. + +-spec date2(binary(), fun()) -> any(). +date2(<< D1, D2, "-", M:3/binary, "-", Y1, Y2, Rest/binary >>, Fun) + when D1 >= $0, D1 =< $9, D2 >= $0, D2 =< $9, + Y1 >= $0, Y1 =< $9, Y2 >= $0, Y2 =< $9 -> + case month(M) of + {error, badarg} -> + {error, badarg}; + Month -> + Year = (Y1 - $0) * 10 + (Y2 - $0), + Year2 = case Year > 50 of + true -> Year + 1900; + false -> Year + 2000 + end, + Fun(Rest, { + Year2, + Month, + (D1 - $0) * 10 + (D2 - $0) + }) + end; +date2(_Data, _Fun) -> + {error, badarg}. + +-spec date3(binary(), fun()) -> any(). +date3(<< M:3/binary, " ", D1, D2, Rest/binary >>, Fun) + when (D1 >= $0 andalso D1 =< $3) orelse D1 =:= $\s, + D2 >= $0, D2 =< $9 -> + case month(M) of + {error, badarg} -> + {error, badarg}; + Month -> + Day = case D1 of + $\s -> D2 - $0; + D1 -> (D1 - $0) * 10 + (D2 - $0) + end, + Fun(Rest, {Month, Day}) + end; +date3(_Data, _Fun) -> + {error, badarg}. + +-spec month(<< _:24 >>) -> 1..12 | {error, badarg}. +month(<<"Jan">>) -> 1; +month(<<"Feb">>) -> 2; +month(<<"Mar">>) -> 3; +month(<<"Apr">>) -> 4; +month(<<"May">>) -> 5; +month(<<"Jun">>) -> 6; +month(<<"Jul">>) -> 7; +month(<<"Aug">>) -> 8; +month(<<"Sep">>) -> 9; +month(<<"Oct">>) -> 10; +month(<<"Nov">>) -> 11; +month(<<"Dec">>) -> 12; +month(_Any) -> {error, badarg}. + +-spec time(binary(), fun()) -> any(). +time(<< H1, H2, ":", M1, M2, ":", S1, S2, Rest/binary >>, Fun) + when H1 >= $0, H1 =< $2, H2 >= $0, H2 =< $9, + M1 >= $0, M1 =< $5, M2 >= $0, M2 =< $9, + S1 >= $0, S1 =< $5, S2 >= $0, S2 =< $9 -> + Hour = (H1 - $0) * 10 + (H2 - $0), + case Hour < 24 of + true -> + Time = { + Hour, + (M1 - $0) * 10 + (M2 - $0), + (S1 - $0) * 10 + (S2 - $0) + }, + Fun(Rest, Time); + false -> + {error, badarg} + end. + +%% @doc Skip whitespace. +-spec whitespace(binary(), fun()) -> any(). +whitespace(<< C, Rest/binary >>, Fun) + when C =:= $\s; C =:= $\t -> + whitespace(Rest, Fun); +whitespace(Data, Fun) -> + Fun(Data). + +%% @doc Parse a list of digits as a non negative integer. +-spec digits(binary()) -> non_neg_integer() | {error, badarg}. +digits(Data) -> + digits(Data, + fun (Rest, I) -> + whitespace(Rest, + fun (<<>>) -> + I; + (_Rest2) -> + {error, badarg} + end) + end). + +-spec digits(binary(), fun()) -> any(). +digits(<< C, Rest/binary >>, Fun) + when C >= $0, C =< $9 -> + digits(Rest, Fun, C - $0); +digits(_Data, _Fun) -> + {error, badarg}. + +-spec digits(binary(), fun(), non_neg_integer()) -> any(). +digits(<< C, Rest/binary >>, Fun, Acc) + when C >= $0, C =< $9 -> + digits(Rest, Fun, Acc * 10 + (C - $0)); +digits(Data, Fun, Acc) -> + Fun(Data, Acc). + +%% @doc Parse a list of case-insensitive alpha characters. +%% +%% Changes all characters to lowercase. +-spec alpha(binary(), fun()) -> any(). +alpha(Data, Fun) -> + alpha(Data, Fun, <<>>). + +-spec alpha(binary(), fun(), binary()) -> any(). +alpha(<<>>, Fun, Acc) -> + Fun(<<>>, Acc); +alpha(<< C, Rest/binary >>, Fun, Acc) + when C >= $a andalso C =< $z; + C >= $A andalso C =< $Z -> + C2 = cowboy_bstr:char_to_lower(C), + alpha(Rest, Fun, << Acc/binary, C2 >>); +alpha(Data, Fun, Acc) -> + Fun(Data, Acc). + +%% @doc Parse either a token or a quoted string. +-spec word(binary(), fun()) -> any(). +word(Data = << $", _/binary >>, Fun) -> + quoted_string(Data, Fun); +word(Data, Fun) -> + token(Data, + fun (_Rest, <<>>) -> {error, badarg}; + (Rest, Token) -> Fun(Rest, Token) + end). + +%% @doc Parse a case-insensitive token. +%% +%% Changes all characters to lowercase. +-spec token_ci(binary(), fun()) -> any(). +token_ci(Data, Fun) -> + token(Data, Fun, ci, <<>>). + +%% @doc Parse a token. +-spec token(binary(), fun()) -> any(). +token(Data, Fun) -> + token(Data, Fun, cs, <<>>). + +-spec token(binary(), fun(), ci | cs, binary()) -> any(). +token(<<>>, Fun, _Case, Acc) -> + Fun(<<>>, Acc); +token(Data = << C, _Rest/binary >>, Fun, _Case, Acc) + when C =:= $(; C =:= $); C =:= $<; C =:= $>; C =:= $@; + C =:= $,; C =:= $;; C =:= $:; C =:= $\\; C =:= $"; + C =:= $/; C =:= $[; C =:= $]; C =:= $?; C =:= $=; + C =:= ${; C =:= $}; C =:= $\s; C =:= $\t; + C < 32; C =:= 127 -> + Fun(Data, Acc); +token(<< C, Rest/binary >>, Fun, Case = ci, Acc) -> + C2 = cowboy_bstr:char_to_lower(C), + token(Rest, Fun, Case, << Acc/binary, C2 >>); +token(<< C, Rest/binary >>, Fun, Case, Acc) -> + token(Rest, Fun, Case, << Acc/binary, C >>). + +%% @doc Parse a quoted string. +-spec quoted_string(binary(), fun()) -> any(). +quoted_string(<< $", Rest/binary >>, Fun) -> + quoted_string(Rest, Fun, <<>>). + +-spec quoted_string(binary(), fun(), binary()) -> any(). +quoted_string(<<>>, _Fun, _Acc) -> + {error, badarg}; +quoted_string(<< $", Rest/binary >>, Fun, Acc) -> + Fun(Rest, Acc); +quoted_string(<< $\\, C, Rest/binary >>, Fun, Acc) -> + quoted_string(Rest, Fun, << Acc/binary, C >>); +quoted_string(<< C, Rest/binary >>, Fun, Acc) -> + quoted_string(Rest, Fun, << Acc/binary, C >>). + +%% @doc Parse a quality value. +-spec qvalue(binary(), fun()) -> any(). +qvalue(<< $0, $., Rest/binary >>, Fun) -> + qvalue(Rest, Fun, 0, 100); +qvalue(<< $0, Rest/binary >>, Fun) -> + Fun(Rest, 0); +qvalue(<< $1, $., $0, $0, $0, Rest/binary >>, Fun) -> + Fun(Rest, 1000); +qvalue(<< $1, $., $0, $0, Rest/binary >>, Fun) -> + Fun(Rest, 1000); +qvalue(<< $1, $., $0, Rest/binary >>, Fun) -> + Fun(Rest, 1000); +qvalue(<< $1, Rest/binary >>, Fun) -> + Fun(Rest, 1000); +qvalue(_Data, _Fun) -> + {error, badarg}. + +-spec qvalue(binary(), fun(), integer(), 1 | 10 | 100) -> any(). +qvalue(Data, Fun, Q, 0) -> + Fun(Data, Q); +qvalue(<< C, Rest/binary >>, Fun, Q, M) + when C >= $0, C =< $9 -> + qvalue(Rest, Fun, Q + (C - $0) * M, M div 10); +qvalue(Data, Fun, Q, _M) -> + Fun(Data, Q). + + +%% Interpretation. + +%% @doc Walk through a tokens list and return whether +%% the connection is keepalive or closed. +%% +%% The connection token is expected to be lower-case. +-spec connection_to_atom([binary()]) -> keepalive | close. +connection_to_atom([]) -> + keepalive; +connection_to_atom([<<"keep-alive">>|_Tail]) -> + keepalive; +connection_to_atom([<<"close">>|_Tail]) -> + close; +connection_to_atom([_Any|Tail]) -> + connection_to_atom(Tail). + +%% @doc Decode a URL encoded binary. +%% @equiv urldecode(Bin, crash) +-spec urldecode(binary()) -> binary(). +urldecode(Bin) when is_binary(Bin) -> + urldecode(Bin, <<>>, crash). + +%% @doc Decode a URL encoded binary. +%% The second argument specifies how to handle percent characters that are not +%% followed by two valid hex characters. Use `skip' to ignore such errors, +%% if `crash' is used the function will fail with the reason `badarg'. +-spec urldecode(binary(), crash | skip) -> binary(). +urldecode(Bin, OnError) when is_binary(Bin) -> + urldecode(Bin, <<>>, OnError). + +-spec urldecode(binary(), binary(), crash | skip) -> binary(). +urldecode(<<$%, H, L, Rest/binary>>, Acc, OnError) -> + G = unhex(H), + M = unhex(L), + if G =:= error; M =:= error -> + case OnError of skip -> ok; crash -> erlang:error(badarg) end, + urldecode(<>, <>, OnError); + true -> + urldecode(Rest, <>, OnError) + end; +urldecode(<<$%, Rest/binary>>, Acc, OnError) -> + case OnError of skip -> ok; crash -> erlang:error(badarg) end, + urldecode(Rest, <>, OnError); +urldecode(<<$+, Rest/binary>>, Acc, OnError) -> + urldecode(Rest, <>, OnError); +urldecode(<>, Acc, OnError) -> + urldecode(Rest, <>, OnError); +urldecode(<<>>, Acc, _OnError) -> + Acc. + +-spec unhex(byte()) -> byte() | error. +unhex(C) when C >= $0, C =< $9 -> C - $0; +unhex(C) when C >= $A, C =< $F -> C - $A + 10; +unhex(C) when C >= $a, C =< $f -> C - $a + 10; +unhex(_) -> error. + + +%% @doc URL encode a string binary. +%% @equiv urlencode(Bin, []) +-spec urlencode(binary()) -> binary(). +urlencode(Bin) -> + urlencode(Bin, []). + +%% @doc URL encode a string binary. +%% The `noplus' option disables the default behaviour of quoting space +%% characters, `\s', as `+'. The `upper' option overrides the default behaviour +%% of writing hex numbers using lowecase letters to using uppercase letters +%% instead. +-spec urlencode(binary(), [noplus|upper]) -> binary(). +urlencode(Bin, Opts) -> + Plus = not proplists:get_value(noplus, Opts, false), + Upper = proplists:get_value(upper, Opts, false), + urlencode(Bin, <<>>, Plus, Upper). + +urlencode(<>, Acc, P=Plus, U=Upper) -> + if C >= $0, C =< $9 -> urlencode(Rest, <>, P, U); + C >= $A, C =< $Z -> urlencode(Rest, <>, P, U); + C >= $a, C =< $z -> urlencode(Rest, <>, P, U); + C =:= $.; C =:= $-; C =:= $~; C =:= $_ -> + urlencode(Rest, <>, P, U); + C =:= $ , Plus -> + urlencode(Rest, <>, P, U); + true -> + H = C band 16#F0 bsr 4, L = C band 16#0F, + H1 = if Upper -> tohexu(H); true -> tohexl(H) end, + L1 = if Upper -> tohexu(L); true -> tohexl(L) end, + urlencode(Rest, <>, P, U) + end; +urlencode(<<>>, Acc, _Plus, _Upper) -> + Acc. + +-spec tohexu(byte()) -> byte(). +tohexu(C) when C < 10 -> $0 + C; +tohexu(C) when C < 17 -> $A + C - 10. + +-spec tohexl(byte()) -> byte(). +tohexl(C) when C < 10 -> $0 + C; +tohexl(C) when C < 17 -> $a + C - 10. + + +%% Tests. + +-ifdef(TEST). + +nonempty_charset_list_test_() -> + %% {Value, Result} + Tests = [ + {<<>>, {error, badarg}}, + {<<"iso-8859-5, unicode-1-1;q=0.8">>, [ + {<<"iso-8859-5">>, 1000}, + {<<"unicode-1-1">>, 800} + ]} + ], + [{V, fun() -> R = nonempty_list(V, fun conneg/2) end} || {V, R} <- Tests]. + +nonempty_language_range_list_test_() -> + %% {Value, Result} + Tests = [ + {<<"da, en-gb;q=0.8, en;q=0.7">>, [ + {<<"da">>, 1000}, + {<<"en-gb">>, 800}, + {<<"en">>, 700} + ]}, + {<<"en, en-US, en-cockney, i-cherokee, x-pig-latin">>, [ + {<<"en">>, 1000}, + {<<"en-us">>, 1000}, + {<<"en-cockney">>, 1000}, + {<<"i-cherokee">>, 1000}, + {<<"x-pig-latin">>, 1000} + ]} + ], + [{V, fun() -> R = nonempty_list(V, fun language_range/2) end} + || {V, R} <- Tests]. + +nonempty_token_list_test_() -> + %% {Value, Result} + Tests = [ + {<<>>, {error, badarg}}, + {<<" ">>, {error, badarg}}, + {<<" , ">>, {error, badarg}}, + {<<",,,">>, {error, badarg}}, + {<<"a b">>, {error, badarg}}, + {<<"a , , , ">>, [<<"a">>]}, + {<<" , , , a">>, [<<"a">>]}, + {<<"a, , b">>, [<<"a">>, <<"b">>]}, + {<<"close">>, [<<"close">>]}, + {<<"keep-alive, upgrade">>, [<<"keep-alive">>, <<"upgrade">>]} + ], + [{V, fun() -> R = nonempty_list(V, fun token/2) end} || {V, R} <- Tests]. + +media_range_list_test_() -> + %% {Tokens, Result} + Tests = [ + {<<"audio/*; q=0.2, audio/basic">>, [ + {{<<"audio">>, <<"*">>, []}, 200, []}, + {{<<"audio">>, <<"basic">>, []}, 1000, []} + ]}, + {<<"text/plain; q=0.5, text/html, " + "text/x-dvi; q=0.8, text/x-c">>, [ + {{<<"text">>, <<"plain">>, []}, 500, []}, + {{<<"text">>, <<"html">>, []}, 1000, []}, + {{<<"text">>, <<"x-dvi">>, []}, 800, []}, + {{<<"text">>, <<"x-c">>, []}, 1000, []} + ]}, + {<<"text/*, text/html, text/html;level=1, */*">>, [ + {{<<"text">>, <<"*">>, []}, 1000, []}, + {{<<"text">>, <<"html">>, []}, 1000, []}, + {{<<"text">>, <<"html">>, [{<<"level">>, <<"1">>}]}, 1000, []}, + {{<<"*">>, <<"*">>, []}, 1000, []} + ]}, + {<<"text/*;q=0.3, text/html;q=0.7, text/html;level=1, " + "text/html;level=2;q=0.4, */*;q=0.5">>, [ + {{<<"text">>, <<"*">>, []}, 300, []}, + {{<<"text">>, <<"html">>, []}, 700, []}, + {{<<"text">>, <<"html">>, [{<<"level">>, <<"1">>}]}, 1000, []}, + {{<<"text">>, <<"html">>, [{<<"level">>, <<"2">>}]}, 400, []}, + {{<<"*">>, <<"*">>, []}, 500, []} + ]}, + {<<"text/html;level=1;quoted=\"hi hi hi\";" + "q=0.123;standalone;complex=gits, text/plain">>, [ + {{<<"text">>, <<"html">>, + [{<<"level">>, <<"1">>}, {<<"quoted">>, <<"hi hi hi">>}]}, 123, + [<<"standalone">>, {<<"complex">>, <<"gits">>}]}, + {{<<"text">>, <<"plain">>, []}, 1000, []} + ]} + ], + [{V, fun() -> R = list(V, fun media_range/2) end} || {V, R} <- Tests]. + +entity_tag_match_test_() -> + %% {Tokens, Result} + Tests = [ + {<<"\"xyzzy\"">>, [{strong, <<"xyzzy">>}]}, + {<<"\"xyzzy\", W/\"r2d2xxxx\", \"c3piozzzz\"">>, + [{strong, <<"xyzzy">>}, + {weak, <<"r2d2xxxx">>}, + {strong, <<"c3piozzzz">>}]}, + {<<"*">>, '*'} + ], + [{V, fun() -> R = entity_tag_match(V) end} || {V, R} <- Tests]. + +http_date_test_() -> + %% {Tokens, Result} + Tests = [ + {<<"Sun, 06 Nov 1994 08:49:37 GMT">>, {{1994, 11, 6}, {8, 49, 37}}}, + {<<"Sunday, 06-Nov-94 08:49:37 GMT">>, {{1994, 11, 6}, {8, 49, 37}}}, + {<<"Sun Nov 6 08:49:37 1994">>, {{1994, 11, 6}, {8, 49, 37}}} + ], + [{V, fun() -> R = http_date(V) end} || {V, R} <- Tests]. + +rfc1123_date_test_() -> + %% {Tokens, Result} + Tests = [ + {<<"Sun, 06 Nov 1994 08:49:37 GMT">>, {{1994, 11, 6}, {8, 49, 37}}} + ], + [{V, fun() -> R = rfc1123_date(V) end} || {V, R} <- Tests]. + +rfc850_date_test_() -> + %% {Tokens, Result} + Tests = [ + {<<"Sunday, 06-Nov-94 08:49:37 GMT">>, {{1994, 11, 6}, {8, 49, 37}}} + ], + [{V, fun() -> R = rfc850_date(V) end} || {V, R} <- Tests]. + +asctime_date_test_() -> + %% {Tokens, Result} + Tests = [ + {<<"Sun Nov 6 08:49:37 1994">>, {{1994, 11, 6}, {8, 49, 37}}} + ], + [{V, fun() -> R = asctime_date(V) end} || {V, R} <- Tests]. + +connection_to_atom_test_() -> + %% {Tokens, Result} + Tests = [ + {[<<"close">>], close}, + {[<<"keep-alive">>], keepalive}, + {[<<"keep-alive">>, <<"upgrade">>], keepalive} + ], + [{lists:flatten(io_lib:format("~p", [T])), + fun() -> R = connection_to_atom(T) end} || {T, R} <- Tests]. + +content_type_test_() -> + %% {ContentType, Result} + Tests = [ + {<<"text/plain; charset=iso-8859-4">>, + {<<"text">>, <<"plain">>, [{<<"charset">>, <<"iso-8859-4">>}]}}, + {<<"multipart/form-data \t;Boundary=\"MultipartIsUgly\"">>, + {<<"multipart">>, <<"form-data">>, [ + {<<"boundary">>, <<"MultipartIsUgly">>} + ]}}, + {<<"foo/bar; one=FirstParam; two=SecondParam">>, + {<<"foo">>, <<"bar">>, [ + {<<"one">>, <<"FirstParam">>}, + {<<"two">>, <<"SecondParam">>} + ]}} + ], + [{V, fun () -> R = content_type(V) end} || {V, R} <- Tests]. + +digits_test_() -> + %% {Digits, Result} + Tests = [ + {<<"42 ">>, 42}, + {<<"69\t">>, 69}, + {<<"1337">>, 1337} + ], + [{V, fun() -> R = digits(V) end} || {V, R} <- Tests]. + +urldecode_test_() -> + U = fun urldecode/2, + [?_assertEqual(<<" ">>, U(<<"%20">>, crash)), + ?_assertEqual(<<" ">>, U(<<"+">>, crash)), + ?_assertEqual(<<0>>, U(<<"%00">>, crash)), + ?_assertEqual(<<255>>, U(<<"%fF">>, crash)), + ?_assertEqual(<<"123">>, U(<<"123">>, crash)), + ?_assertEqual(<<"%i5">>, U(<<"%i5">>, skip)), + ?_assertEqual(<<"%5">>, U(<<"%5">>, skip)), + ?_assertError(badarg, U(<<"%i5">>, crash)), + ?_assertError(badarg, U(<<"%5">>, crash)) + ]. + +urlencode_test_() -> + U = fun urlencode/2, + [?_assertEqual(<<"%ff%00">>, U(<<255,0>>, [])), + ?_assertEqual(<<"%FF%00">>, U(<<255,0>>, [upper])), + ?_assertEqual(<<"+">>, U(<<" ">>, [])), + ?_assertEqual(<<"%20">>, U(<<" ">>, [noplus])), + ?_assertEqual(<<"aBc">>, U(<<"aBc">>, [])), + ?_assertEqual(<<".-~_">>, U(<<".-~_">>, [])), + ?_assertEqual(<<"%ff+">>, urlencode(<<255, " ">>)) + ]. + +-endif. diff --git a/rabbitmq-server/plugins-src/cowboy-wrapper/cowboy-git/src/cowboy_http_handler.erl b/rabbitmq-server/plugins-src/cowboy-wrapper/cowboy-git/src/cowboy_http_handler.erl new file mode 100644 index 0000000..b220b09 --- /dev/null +++ b/rabbitmq-server/plugins-src/cowboy-wrapper/cowboy-git/src/cowboy_http_handler.erl @@ -0,0 +1,48 @@ +%% Copyright (c) 2011, Loïc Hoguin +%% +%% Permission to use, copy, modify, and/or distribute this software for any +%% purpose with or without fee is hereby granted, provided that the above +%% copyright notice and this permission notice appear in all copies. +%% +%% THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES +%% WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF +%% MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR +%% ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES +%% WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN +%% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF +%% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + +%% @doc Handler for HTTP requests. +%% +%% HTTP handlers must implement three callbacks: init/3, +%% handle/2 and terminate/2, called one after another in +%% that order. +%% +%% init/3 is meant for initialization. It receives information about +%% the transport and protocol used, along with the handler options from the +%% dispatch list, and allows you to upgrade the protocol if needed. You can +%% define a request-wide state here. +%% +%% handle/2 is meant for handling the request. It receives the +%% request and the state previously defined. +%% +%% terminate/2 is meant for cleaning up. It also receives the +%% request and the state previously defined. +%% +%% You do not have to read the request body or even send a reply if you do +%% not need to. Cowboy will properly handle these cases and clean-up afterwards. +%% In doubt it'll simply close the connection. +%% +%% Note that when upgrading the connection to WebSocket you do not need to +%% define the handle/2 and terminate/2 callbacks. +-module(cowboy_http_handler). + +-export([behaviour_info/1]). + +%% @private +-spec behaviour_info(_) + -> undefined | [{handle, 2} | {init, 3} | {terminate, 2}, ...]. +behaviour_info(callbacks) -> + [{init, 3}, {handle, 2}, {terminate, 2}]; +behaviour_info(_Other) -> + undefined. diff --git a/rabbitmq-server/plugins-src/cowboy-wrapper/cowboy-git/src/cowboy_http_protocol.erl b/rabbitmq-server/plugins-src/cowboy-wrapper/cowboy-git/src/cowboy_http_protocol.erl new file mode 100644 index 0000000..0183785 --- /dev/null +++ b/rabbitmq-server/plugins-src/cowboy-wrapper/cowboy-git/src/cowboy_http_protocol.erl @@ -0,0 +1,472 @@ +%% Copyright (c) 2011, Loïc Hoguin +%% Copyright (c) 2011, Anthony Ramine +%% +%% Permission to use, copy, modify, and/or distribute this software for any +%% purpose with or without fee is hereby granted, provided that the above +%% copyright notice and this permission notice appear in all copies. +%% +%% THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES +%% WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF +%% MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR +%% ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES +%% WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN +%% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF +%% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + +%% @doc HTTP protocol handler. +%% +%% The available options are: +%%
+%%
dispatch
The dispatch list for this protocol.
+%%
max_empty_lines
Max number of empty lines before a request. +%% Defaults to 5.
+%%
timeout
Time in milliseconds before an idle +%% connection is closed. Defaults to 5000 milliseconds.
+%%
urldecode
Function and options argument to use when decoding +%% URL encoded strings. Defaults to `{fun cowboy_http:urldecode/2, crash}'. +%%
+%%
+%% +%% Note that there is no need to monitor these processes when using Cowboy as +%% an application as it already supervises them under the listener supervisor. +%% +%% @see cowboy_dispatcher +%% @see cowboy_http_handler +-module(cowboy_http_protocol). +-behaviour(cowboy_protocol). + +-export([start_link/4]). %% API. +-export([init/4, parse_request/1, handler_loop/3]). %% FSM. + +-include("include/http.hrl"). +-include_lib("eunit/include/eunit.hrl"). + +-record(state, { + listener :: pid(), + socket :: inet:socket(), + transport :: module(), + dispatch :: cowboy_dispatcher:dispatch_rules(), + handler :: {module(), any()}, + urldecode :: {fun((binary(), T) -> binary()), T}, + req_empty_lines = 0 :: integer(), + max_empty_lines :: integer(), + req_keepalive = 1 :: integer(), + max_keepalive :: integer(), + max_line_length :: integer(), + timeout :: timeout(), + buffer = <<>> :: binary(), + hibernate = false, + loop_timeout = infinity :: timeout(), + loop_timeout_ref +}). + +%% API. + +%% @doc Start an HTTP protocol process. +-spec start_link(pid(), inet:socket(), module(), any()) -> {ok, pid()}. +start_link(ListenerPid, Socket, Transport, Opts) -> + Pid = spawn_link(?MODULE, init, [ListenerPid, Socket, Transport, Opts]), + {ok, Pid}. + +%% FSM. + +%% @private +-spec init(pid(), inet:socket(), module(), any()) -> ok | none(). +init(ListenerPid, Socket, Transport, Opts) -> + Dispatch = proplists:get_value(dispatch, Opts, []), + MaxEmptyLines = proplists:get_value(max_empty_lines, Opts, 5), + MaxKeepalive = proplists:get_value(max_keepalive, Opts, infinity), + MaxLineLength = proplists:get_value(max_line_length, Opts, 4096), + Timeout = proplists:get_value(timeout, Opts, 5000), + URLDecDefault = {fun cowboy_http:urldecode/2, crash}, + URLDec = proplists:get_value(urldecode, Opts, URLDecDefault), + ok = cowboy:accept_ack(ListenerPid), + wait_request(#state{listener=ListenerPid, socket=Socket, transport=Transport, + dispatch=Dispatch, max_empty_lines=MaxEmptyLines, + max_keepalive=MaxKeepalive, max_line_length=MaxLineLength, + timeout=Timeout, urldecode=URLDec}). + +%% @private +-spec parse_request(#state{}) -> ok | none(). +%% We limit the length of the Request-line to MaxLength to avoid endlessly +%% reading from the socket and eventually crashing. +parse_request(State=#state{buffer=Buffer, max_line_length=MaxLength}) -> + case erlang:decode_packet(http_bin, Buffer, []) of + {ok, Request, Rest} -> request(Request, State#state{buffer=Rest}); + {more, _Length} when byte_size(Buffer) > MaxLength -> + error_terminate(413, State); + {more, _Length} -> wait_request(State); + {error, _Reason} -> error_terminate(400, State) + end. + +-spec wait_request(#state{}) -> ok | none(). +wait_request(State=#state{socket=Socket, transport=Transport, + timeout=T, buffer=Buffer}) -> + case Transport:recv(Socket, 0, T) of + {ok, Data} -> parse_request(State#state{ + buffer= << Buffer/binary, Data/binary >>}); + {error, _Reason} -> terminate(State) + end. + +-spec request({http_request, cowboy_http:method(), cowboy_http:uri(), + cowboy_http:version()}, #state{}) -> ok | none(). +request({http_request, _Method, _URI, Version}, State) + when Version =/= {1, 0}, Version =/= {1, 1} -> + error_terminate(505, State); +request({http_request, Method, {abs_path, AbsPath}, Version}, + State=#state{socket=Socket, transport=Transport, + urldecode={URLDecFun, URLDecArg}=URLDec}) -> + URLDecode = fun(Bin) -> URLDecFun(Bin, URLDecArg) end, + {Path, RawPath, Qs} = cowboy_dispatcher:split_path(AbsPath, URLDecode), + ConnAtom = version_to_connection(Version), + parse_header(#http_req{socket=Socket, transport=Transport, + connection=ConnAtom, pid=self(), method=Method, version=Version, + path=Path, raw_path=RawPath, raw_qs=Qs, urldecode=URLDec}, State); +request({http_request, Method, '*', Version}, + State=#state{socket=Socket, transport=Transport, urldecode=URLDec}) -> + ConnAtom = version_to_connection(Version), + parse_header(#http_req{socket=Socket, transport=Transport, + connection=ConnAtom, pid=self(), method=Method, version=Version, + path='*', raw_path= <<"*">>, raw_qs= <<>>, urldecode=URLDec}, State); +request({http_request, _Method, _URI, _Version}, State) -> + error_terminate(501, State); +request({http_error, <<"\r\n">>}, + State=#state{req_empty_lines=N, max_empty_lines=N}) -> + error_terminate(400, State); +request({http_error, <<"\r\n">>}, State=#state{req_empty_lines=N}) -> + parse_request(State#state{req_empty_lines=N + 1}); +request(_Any, State) -> + error_terminate(400, State). + +-spec parse_header(#http_req{}, #state{}) -> ok | none(). +parse_header(Req, State=#state{buffer=Buffer, max_line_length=MaxLength}) -> + case erlang:decode_packet(httph_bin, Buffer, []) of + {ok, Header, Rest} -> header(Header, Req, State#state{buffer=Rest}); + {more, _Length} when byte_size(Buffer) > MaxLength -> + error_terminate(413, State); + {more, _Length} -> wait_header(Req, State); + {error, _Reason} -> error_terminate(400, State) + end. + +-spec wait_header(#http_req{}, #state{}) -> ok | none(). +wait_header(Req, State=#state{socket=Socket, + transport=Transport, timeout=T, buffer=Buffer}) -> + case Transport:recv(Socket, 0, T) of + {ok, Data} -> parse_header(Req, State#state{ + buffer= << Buffer/binary, Data/binary >>}); + {error, timeout} -> error_terminate(408, State); + {error, closed} -> terminate(State) + end. + +-spec header({http_header, integer(), cowboy_http:header(), any(), binary()} + | http_eoh, #http_req{}, #state{}) -> ok | none(). +header({http_header, _I, 'Host', _R, RawHost}, Req=#http_req{ + transport=Transport, host=undefined}, State) -> + RawHost2 = cowboy_bstr:to_lower(RawHost), + case catch cowboy_dispatcher:split_host(RawHost2) of + {Host, RawHost3, undefined} -> + Port = default_port(Transport:name()), + dispatch(fun parse_header/2, Req#http_req{ + host=Host, raw_host=RawHost3, port=Port, + headers=[{'Host', RawHost3}|Req#http_req.headers]}, State); + {Host, RawHost3, Port} -> + dispatch(fun parse_header/2, Req#http_req{ + host=Host, raw_host=RawHost3, port=Port, + headers=[{'Host', RawHost3}|Req#http_req.headers]}, State); + {'EXIT', _Reason} -> + error_terminate(400, State) + end; +%% Ignore Host headers if we already have it. +header({http_header, _I, 'Host', _R, _V}, Req, State) -> + parse_header(Req, State); +header({http_header, _I, 'Connection', _R, Connection}, + Req=#http_req{headers=Headers}, State) -> + Req2 = Req#http_req{headers=[{'Connection', Connection}|Headers]}, + {ConnTokens, Req3} + = cowboy_http_req:parse_header('Connection', Req2), + ConnAtom = cowboy_http:connection_to_atom(ConnTokens), + parse_header(Req3#http_req{connection=ConnAtom}, State); +header({http_header, _I, Field, _R, Value}, Req, State) -> + Field2 = format_header(Field), + parse_header(Req#http_req{headers=[{Field2, Value}|Req#http_req.headers]}, + State); +%% The Host header is required in HTTP/1.1. +header(http_eoh, #http_req{version={1, 1}, host=undefined}, State) -> + error_terminate(400, State); +%% It is however optional in HTTP/1.0. +header(http_eoh, Req=#http_req{version={1, 0}, transport=Transport, + host=undefined}, State=#state{buffer=Buffer}) -> + Port = default_port(Transport:name()), + dispatch(fun handler_init/2, Req#http_req{host=[], raw_host= <<>>, + port=Port, buffer=Buffer}, State#state{buffer= <<>>}); +header(http_eoh, Req, State=#state{buffer=Buffer}) -> + handler_init(Req#http_req{buffer=Buffer}, State#state{buffer= <<>>}); +header(_Any, _Req, State) -> + error_terminate(400, State). + +-spec dispatch(fun((#http_req{}, #state{}) -> ok), + #http_req{}, #state{}) -> ok | none(). +dispatch(Next, Req=#http_req{host=Host, path=Path}, + State=#state{dispatch=Dispatch}) -> + %% @todo We should allow a configurable chain of handlers here to + %% allow things like url rewriting, site-wide authentication, + %% optional dispatching, and more. It would default to what + %% we are doing so far. + case cowboy_dispatcher:match(Host, Path, Dispatch) of + {ok, Handler, Opts, Binds, HostInfo, PathInfo} -> + Next(Req#http_req{host_info=HostInfo, path_info=PathInfo, + bindings=Binds}, State#state{handler={Handler, Opts}}); + {error, notfound, host} -> + error_terminate(400, State); + {error, notfound, path} -> + error_terminate(404, State) + end. + +-spec handler_init(#http_req{}, #state{}) -> ok | none(). +handler_init(Req, State=#state{transport=Transport, + handler={Handler, Opts}}) -> + try Handler:init({Transport:name(), http}, Req, Opts) of + {ok, Req2, HandlerState} -> + handler_handle(HandlerState, Req2, State); + {loop, Req2, HandlerState} -> + handler_before_loop(HandlerState, Req2, State); + {loop, Req2, HandlerState, hibernate} -> + handler_before_loop(HandlerState, Req2, + State#state{hibernate=true}); + {loop, Req2, HandlerState, Timeout} -> + handler_before_loop(HandlerState, Req2, + State#state{loop_timeout=Timeout}); + {loop, Req2, HandlerState, Timeout, hibernate} -> + handler_before_loop(HandlerState, Req2, + State#state{hibernate=true, loop_timeout=Timeout}); + {shutdown, Req2, HandlerState} -> + handler_terminate(HandlerState, Req2, State); + %% @todo {upgrade, transport, Module} + {upgrade, protocol, Module} -> + upgrade_protocol(Req, State, Module) + catch Class:Reason -> + error_terminate(500, State), + error_logger:error_msg( + "** Handler ~p terminating in init/3~n" + " for the reason ~p:~p~n" + "** Options were ~p~n" + "** Request was ~p~n** Stacktrace: ~p~n~n", + [Handler, Class, Reason, Opts, Req, erlang:get_stacktrace()]) + end. + +-spec upgrade_protocol(#http_req{}, #state{}, atom()) -> ok | none(). +upgrade_protocol(Req, State=#state{listener=ListenerPid, + handler={Handler, Opts}}, Module) -> + case Module:upgrade(ListenerPid, Handler, Opts, Req) of + {UpgradeRes, Req2} -> next_request(Req2, State, UpgradeRes); + _Any -> terminate(State) + end. + +-spec handler_handle(any(), #http_req{}, #state{}) -> ok | none(). +handler_handle(HandlerState, Req, State=#state{handler={Handler, Opts}}) -> + try Handler:handle(Req, HandlerState) of + {ok, Req2, HandlerState2} -> + terminate_request(HandlerState2, Req2, State) + catch Class:Reason -> + error_logger:error_msg( + "** Handler ~p terminating in handle/2~n" + " for the reason ~p:~p~n" + "** Options were ~p~n** Handler state was ~p~n" + "** Request was ~p~n** Stacktrace: ~p~n~n", + [Handler, Class, Reason, Opts, + HandlerState, Req, erlang:get_stacktrace()]), + handler_terminate(HandlerState, Req, State), + error_terminate(500, State) + end. + +%% We don't listen for Transport closes because that would force us +%% to receive data and buffer it indefinitely. +-spec handler_before_loop(any(), #http_req{}, #state{}) -> ok | none(). +handler_before_loop(HandlerState, Req, State=#state{hibernate=true}) -> + State2 = handler_loop_timeout(State), + erlang:hibernate(?MODULE, handler_loop, + [HandlerState, Req, State2#state{hibernate=false}]); +handler_before_loop(HandlerState, Req, State) -> + State2 = handler_loop_timeout(State), + handler_loop(HandlerState, Req, State2). + +%% Almost the same code can be found in cowboy_http_websocket. +-spec handler_loop_timeout(#state{}) -> #state{}. +handler_loop_timeout(State=#state{loop_timeout=infinity}) -> + State#state{loop_timeout_ref=undefined}; +handler_loop_timeout(State=#state{loop_timeout=Timeout, + loop_timeout_ref=PrevRef}) -> + _ = case PrevRef of undefined -> ignore; PrevRef -> + erlang:cancel_timer(PrevRef) end, + TRef = make_ref(), + erlang:send_after(Timeout, self(), {?MODULE, timeout, TRef}), + State#state{loop_timeout_ref=TRef}. + +-spec handler_loop(any(), #http_req{}, #state{}) -> ok | none(). +handler_loop(HandlerState, Req, State=#state{loop_timeout_ref=TRef}) -> + receive + {?MODULE, timeout, TRef} -> + terminate_request(HandlerState, Req, State); + {?MODULE, timeout, OlderTRef} when is_reference(OlderTRef) -> + handler_loop(HandlerState, Req, State); + Message -> + handler_call(HandlerState, Req, State, Message) + end. + +-spec handler_call(any(), #http_req{}, #state{}, any()) -> ok | none(). +handler_call(HandlerState, Req, State=#state{handler={Handler, Opts}}, + Message) -> + try Handler:info(Message, Req, HandlerState) of + {ok, Req2, HandlerState2} -> + terminate_request(HandlerState2, Req2, State); + {loop, Req2, HandlerState2} -> + handler_before_loop(HandlerState2, Req2, State); + {loop, Req2, HandlerState2, hibernate} -> + handler_before_loop(HandlerState2, Req2, + State#state{hibernate=true}) + catch Class:Reason -> + error_logger:error_msg( + "** Handler ~p terminating in info/3~n" + " for the reason ~p:~p~n" + "** Options were ~p~n** Handler state was ~p~n" + "** Request was ~p~n** Stacktrace: ~p~n~n", + [Handler, Class, Reason, Opts, + HandlerState, Req, erlang:get_stacktrace()]), + handler_terminate(HandlerState, Req, State), + error_terminate(500, State) + end. + +-spec handler_terminate(any(), #http_req{}, #state{}) -> ok. +handler_terminate(HandlerState, Req, #state{handler={Handler, Opts}}) -> + try + Handler:terminate(Req#http_req{resp_state=locked}, HandlerState) + catch Class:Reason -> + error_logger:error_msg( + "** Handler ~p terminating in terminate/2~n" + " for the reason ~p:~p~n" + "** Options were ~p~n** Handler state was ~p~n" + "** Request was ~p~n** Stacktrace: ~p~n~n", + [Handler, Class, Reason, Opts, + HandlerState, Req, erlang:get_stacktrace()]) + end. + +-spec terminate_request(any(), #http_req{}, #state{}) -> ok | none(). +terminate_request(HandlerState, Req, State) -> + HandlerRes = handler_terminate(HandlerState, Req, State), + next_request(Req, State, HandlerRes). + +-spec next_request(#http_req{}, #state{}, any()) -> ok | none(). +next_request(Req=#http_req{connection=Conn}, + State=#state{req_keepalive=Keepalive, max_keepalive=MaxKeepalive}, + HandlerRes) -> + RespRes = ensure_response(Req), + {BodyRes, Buffer} = ensure_body_processed(Req), + %% Flush the resp_sent message before moving on. + receive {cowboy_http_req, resp_sent} -> ok after 0 -> ok end, + case {HandlerRes, BodyRes, RespRes, Conn} of + {ok, ok, ok, keepalive} when Keepalive < MaxKeepalive -> + ?MODULE:parse_request(State#state{ + buffer=Buffer, req_empty_lines=0, + req_keepalive=Keepalive + 1}); + _Closed -> + terminate(State) + end. + +-spec ensure_body_processed(#http_req{}) -> {ok | close, binary()}. +ensure_body_processed(#http_req{body_state=done, buffer=Buffer}) -> + {ok, Buffer}; +ensure_body_processed(Req=#http_req{body_state=waiting}) -> + case cowboy_http_req:body(Req) of + {error, badarg} -> {ok, Req#http_req.buffer}; %% No body. + {error, _Reason} -> {close, <<>>}; + {ok, _, Req2} -> {ok, Req2#http_req.buffer} + end; +ensure_body_processed(Req=#http_req{body_state={multipart, _, _}}) -> + {ok, Req2} = cowboy_http_req:multipart_skip(Req), + ensure_body_processed(Req2). + +-spec ensure_response(#http_req{}) -> ok. +%% The handler has already fully replied to the client. +ensure_response(#http_req{resp_state=done}) -> + ok; +%% No response has been sent but everything apparently went fine. +%% Reply with 204 No Content to indicate this. +ensure_response(Req=#http_req{resp_state=waiting}) -> + _ = cowboy_http_req:reply(204, [], [], Req), + ok; +%% Close the chunked reply. +ensure_response(#http_req{method='HEAD', resp_state=chunks}) -> + close; +ensure_response(#http_req{socket=Socket, transport=Transport, + resp_state=chunks}) -> + Transport:send(Socket, <<"0\r\n\r\n">>), + close. + +%% Only send an error reply if there is no resp_sent message. +-spec error_terminate(cowboy_http:status(), #state{}) -> ok. +error_terminate(Code, State=#state{socket=Socket, transport=Transport}) -> + receive + {cowboy_http_req, resp_sent} -> ok + after 0 -> + _ = cowboy_http_req:reply(Code, #http_req{ + socket=Socket, transport=Transport, + connection=close, pid=self(), resp_state=waiting}), + ok + end, + terminate(State). + +-spec terminate(#state{}) -> ok. +terminate(#state{socket=Socket, transport=Transport}) -> + Transport:close(Socket), + ok. + +%% Internal. + +-spec version_to_connection(cowboy_http:version()) -> keepalive | close. +version_to_connection({1, 1}) -> keepalive; +version_to_connection(_Any) -> close. + +-spec default_port(atom()) -> 80 | 443. +default_port(ssl) -> 443; +default_port(_) -> 80. + +%% @todo While 32 should be enough for everybody, we should probably make +%% this configurable or something. +-spec format_header(atom()) -> atom(); (binary()) -> binary(). +format_header(Field) when is_atom(Field) -> + Field; +format_header(Field) when byte_size(Field) =< 20; byte_size(Field) > 32 -> + Field; +format_header(Field) -> + format_header(Field, true, <<>>). + +format_header(<<>>, _Any, Acc) -> + Acc; +%% Replicate a bug in OTP for compatibility reasons when there's a - right +%% after another. Proper use should always be 'true' instead of 'not Bool'. +format_header(<< $-, Rest/bits >>, Bool, Acc) -> + format_header(Rest, not Bool, << Acc/binary, $- >>); +format_header(<< C, Rest/bits >>, true, Acc) -> + format_header(Rest, false, << Acc/binary, (cowboy_bstr:char_to_upper(C)) >>); +format_header(<< C, Rest/bits >>, false, Acc) -> + format_header(Rest, false, << Acc/binary, (cowboy_bstr:char_to_lower(C)) >>). + +%% Tests. + +-ifdef(TEST). + +format_header_test_() -> + %% {Header, Result} + Tests = [ + {<<"Sec-Websocket-Version">>, <<"Sec-Websocket-Version">>}, + {<<"Sec-WebSocket-Version">>, <<"Sec-Websocket-Version">>}, + {<<"sec-websocket-version">>, <<"Sec-Websocket-Version">>}, + {<<"SEC-WEBSOCKET-VERSION">>, <<"Sec-Websocket-Version">>}, + %% These last tests ensures we're formatting headers exactly like OTP. + %% Even though it's dumb, it's better for compatibility reasons. + {<<"Sec-WebSocket--Version">>, <<"Sec-Websocket--version">>}, + {<<"Sec-WebSocket---Version">>, <<"Sec-Websocket---Version">>} + ], + [{H, fun() -> R = format_header(H) end} || {H, R} <- Tests]. + +-endif. diff --git a/rabbitmq-server/plugins-src/cowboy-wrapper/cowboy-git/src/cowboy_http_req.erl b/rabbitmq-server/plugins-src/cowboy-wrapper/cowboy-git/src/cowboy_http_req.erl new file mode 100644 index 0000000..dd772df --- /dev/null +++ b/rabbitmq-server/plugins-src/cowboy-wrapper/cowboy-git/src/cowboy_http_req.erl @@ -0,0 +1,820 @@ +%% Copyright (c) 2011, Loïc Hoguin +%% Copyright (c) 2011, Anthony Ramine +%% +%% Permission to use, copy, modify, and/or distribute this software for any +%% purpose with or without fee is hereby granted, provided that the above +%% copyright notice and this permission notice appear in all copies. +%% +%% THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES +%% WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF +%% MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR +%% ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES +%% WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN +%% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF +%% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + +%% @doc HTTP request manipulation API. +%% +%% Almost all functions in this module return a new Req variable. +%% It should always be used instead of the one used in your function call +%% because it keeps the state of the request. It also allows Cowboy to do +%% some lazy evaluation and cache results where possible. +-module(cowboy_http_req). + +-export([ + method/1, version/1, peer/1, peer_addr/1, + host/1, host_info/1, raw_host/1, port/1, + path/1, path_info/1, raw_path/1, + qs_val/2, qs_val/3, qs_vals/1, raw_qs/1, + binding/2, binding/3, bindings/1, + header/2, header/3, headers/1, + parse_header/2, parse_header/3, + cookie/2, cookie/3, cookies/1, + meta/2, meta/3 +]). %% Request API. + +-export([ + body/1, body/2, body_qs/1, + multipart_data/1, multipart_skip/1 +]). %% Request Body API. + +-export([ + set_resp_cookie/4, set_resp_header/3, set_resp_body/2, + set_resp_body_fun/3, has_resp_header/2, has_resp_body/1, + reply/2, reply/3, reply/4, + chunked_reply/2, chunked_reply/3, chunk/2, + upgrade_reply/3 +]). %% Response API. + +-export([ + compact/1, transport/1 +]). %% Misc API. + +-include("include/http.hrl"). +-include_lib("eunit/include/eunit.hrl"). + +%% Request API. + +%% @doc Return the HTTP method of the request. +-spec method(#http_req{}) -> {cowboy_http:method(), #http_req{}}. +method(Req) -> + {Req#http_req.method, Req}. + +%% @doc Return the HTTP version used for the request. +-spec version(#http_req{}) -> {cowboy_http:version(), #http_req{}}. +version(Req) -> + {Req#http_req.version, Req}. + +%% @doc Return the peer address and port number of the remote host. +-spec peer(#http_req{}) -> {{inet:ip_address(), inet:ip_port()}, #http_req{}}. +peer(Req=#http_req{socket=Socket, transport=Transport, peer=undefined}) -> + {ok, Peer} = Transport:peername(Socket), + {Peer, Req#http_req{peer=Peer}}; +peer(Req) -> + {Req#http_req.peer, Req}. + +%% @doc Returns the peer address calculated from headers. +-spec peer_addr(#http_req{}) -> {inet:ip_address(), #http_req{}}. +peer_addr(Req = #http_req{}) -> + {RealIp, Req1} = header(<<"X-Real-Ip">>, Req), + {ForwardedForRaw, Req2} = header(<<"X-Forwarded-For">>, Req1), + {{PeerIp, _PeerPort}, Req3} = peer(Req2), + ForwardedFor = case ForwardedForRaw of + undefined -> + undefined; + ForwardedForRaw -> + case re:run(ForwardedForRaw, "^(?[^\\,]+)", + [{capture, [first_ip], binary}]) of + {match, [FirstIp]} -> FirstIp; + _Any -> undefined + end + end, + {ok, PeerAddr} = if + is_binary(RealIp) -> inet_parse:address(binary_to_list(RealIp)); + is_binary(ForwardedFor) -> inet_parse:address(binary_to_list(ForwardedFor)); + true -> {ok, PeerIp} + end, + {PeerAddr, Req3}. + +%% @doc Return the tokens for the hostname requested. +-spec host(#http_req{}) -> {cowboy_dispatcher:tokens(), #http_req{}}. +host(Req) -> + {Req#http_req.host, Req}. + +%% @doc Return the extra host information obtained from partially matching +%% the hostname using '...'. +-spec host_info(#http_req{}) + -> {cowboy_dispatcher:tokens() | undefined, #http_req{}}. +host_info(Req) -> + {Req#http_req.host_info, Req}. + +%% @doc Return the raw host directly taken from the request. +-spec raw_host(#http_req{}) -> {binary(), #http_req{}}. +raw_host(Req) -> + {Req#http_req.raw_host, Req}. + +%% @doc Return the port used for this request. +-spec port(#http_req{}) -> {inet:ip_port(), #http_req{}}. +port(Req) -> + {Req#http_req.port, Req}. + +%% @doc Return the path segments for the path requested. +%% +%% Following RFC2396, this function may return path segments containing any +%% character, including / if, and only if, a / was escaped +%% and part of a path segment in the path requested. +-spec path(#http_req{}) -> {cowboy_dispatcher:tokens(), #http_req{}}. +path(Req) -> + {Req#http_req.path, Req}. + +%% @doc Return the extra path information obtained from partially matching +%% the patch using '...'. +-spec path_info(#http_req{}) + -> {cowboy_dispatcher:tokens() | undefined, #http_req{}}. +path_info(Req) -> + {Req#http_req.path_info, Req}. + +%% @doc Return the raw path directly taken from the request. +-spec raw_path(#http_req{}) -> {binary(), #http_req{}}. +raw_path(Req) -> + {Req#http_req.raw_path, Req}. + +%% @equiv qs_val(Name, Req, undefined) +-spec qs_val(binary(), #http_req{}) + -> {binary() | true | undefined, #http_req{}}. +qs_val(Name, Req) when is_binary(Name) -> + qs_val(Name, Req, undefined). + +%% @doc Return the query string value for the given key, or a default if +%% missing. +qs_val(Name, Req=#http_req{raw_qs=RawQs, qs_vals=undefined, + urldecode={URLDecFun, URLDecArg}}, Default) when is_binary(Name) -> + QsVals = parse_qs(RawQs, fun(Bin) -> URLDecFun(Bin, URLDecArg) end), + qs_val(Name, Req#http_req{qs_vals=QsVals}, Default); +qs_val(Name, Req, Default) -> + case lists:keyfind(Name, 1, Req#http_req.qs_vals) of + {Name, Value} -> {Value, Req}; + false -> {Default, Req} + end. + +%% @doc Return the full list of query string values. +-spec qs_vals(#http_req{}) -> {list({binary(), binary() | true}), #http_req{}}. +qs_vals(Req=#http_req{raw_qs=RawQs, qs_vals=undefined, + urldecode={URLDecFun, URLDecArg}}) -> + QsVals = parse_qs(RawQs, fun(Bin) -> URLDecFun(Bin, URLDecArg) end), + qs_vals(Req#http_req{qs_vals=QsVals}); +qs_vals(Req=#http_req{qs_vals=QsVals}) -> + {QsVals, Req}. + +%% @doc Return the raw query string directly taken from the request. +-spec raw_qs(#http_req{}) -> {binary(), #http_req{}}. +raw_qs(Req) -> + {Req#http_req.raw_qs, Req}. + +%% @equiv binding(Name, Req, undefined) +-spec binding(atom(), #http_req{}) -> {binary() | undefined, #http_req{}}. +binding(Name, Req) when is_atom(Name) -> + binding(Name, Req, undefined). + +%% @doc Return the binding value for the given key obtained when matching +%% the host and path against the dispatch list, or a default if missing. +binding(Name, Req, Default) when is_atom(Name) -> + case lists:keyfind(Name, 1, Req#http_req.bindings) of + {Name, Value} -> {Value, Req}; + false -> {Default, Req} + end. + +%% @doc Return the full list of binding values. +-spec bindings(#http_req{}) -> {list({atom(), binary()}), #http_req{}}. +bindings(Req) -> + {Req#http_req.bindings, Req}. + +%% @equiv header(Name, Req, undefined) +-spec header(atom() | binary(), #http_req{}) + -> {binary() | undefined, #http_req{}}. +header(Name, Req) when is_atom(Name) orelse is_binary(Name) -> + header(Name, Req, undefined). + +%% @doc Return the header value for the given key, or a default if missing. +header(Name, Req, Default) when is_atom(Name) orelse is_binary(Name) -> + case lists:keyfind(Name, 1, Req#http_req.headers) of + {Name, Value} -> {Value, Req}; + false -> {Default, Req} + end. + +%% @doc Return the full list of headers. +-spec headers(#http_req{}) -> {cowboy_http:headers(), #http_req{}}. +headers(Req) -> + {Req#http_req.headers, Req}. + +%% @doc Semantically parse headers. +%% +%% When the value isn't found, a proper default value for the type +%% returned is used as a return value. +%% @see parse_header/3 +-spec parse_header(cowboy_http:header(), #http_req{}) + -> {any(), #http_req{}} | {error, badarg}. +parse_header(Name, Req=#http_req{p_headers=PHeaders}) -> + case lists:keyfind(Name, 1, PHeaders) of + false -> parse_header(Name, Req, parse_header_default(Name)); + {Name, Value} -> {Value, Req} + end. + +%% @doc Default values for semantic header parsing. +-spec parse_header_default(cowboy_http:header()) -> any(). +parse_header_default('Connection') -> []; +parse_header_default(_Name) -> undefined. + +%% @doc Semantically parse headers. +%% +%% When the header is unknown, the value is returned directly without parsing. +-spec parse_header(cowboy_http:header(), #http_req{}, any()) + -> {any(), #http_req{}} | {error, badarg}. +parse_header(Name, Req, Default) when Name =:= 'Accept' -> + parse_header(Name, Req, Default, + fun (Value) -> + cowboy_http:list(Value, fun cowboy_http:media_range/2) + end); +parse_header(Name, Req, Default) when Name =:= 'Accept-Charset' -> + parse_header(Name, Req, Default, + fun (Value) -> + cowboy_http:nonempty_list(Value, fun cowboy_http:conneg/2) + end); +parse_header(Name, Req, Default) when Name =:= 'Accept-Encoding' -> + parse_header(Name, Req, Default, + fun (Value) -> + cowboy_http:list(Value, fun cowboy_http:conneg/2) + end); +parse_header(Name, Req, Default) when Name =:= 'Accept-Language' -> + parse_header(Name, Req, Default, + fun (Value) -> + cowboy_http:nonempty_list(Value, fun cowboy_http:language_range/2) + end); +parse_header(Name, Req, Default) when Name =:= 'Connection' -> + parse_header(Name, Req, Default, + fun (Value) -> + cowboy_http:nonempty_list(Value, fun cowboy_http:token_ci/2) + end); +parse_header(Name, Req, Default) when Name =:= 'Content-Length' -> + parse_header(Name, Req, Default, + fun (Value) -> + cowboy_http:digits(Value) + end); +parse_header(Name, Req, Default) when Name =:= 'Content-Type' -> + parse_header(Name, Req, Default, + fun (Value) -> + cowboy_http:content_type(Value) + end); +parse_header(Name, Req, Default) + when Name =:= 'If-Match'; Name =:= 'If-None-Match' -> + parse_header(Name, Req, Default, + fun (Value) -> + cowboy_http:entity_tag_match(Value) + end); +parse_header(Name, Req, Default) + when Name =:= 'If-Modified-Since'; Name =:= 'If-Unmodified-Since' -> + parse_header(Name, Req, Default, + fun (Value) -> + cowboy_http:http_date(Value) + end); +parse_header(Name, Req, Default) when Name =:= 'Upgrade' -> + parse_header(Name, Req, Default, + fun (Value) -> + cowboy_http:nonempty_list(Value, fun cowboy_http:token_ci/2) + end); +parse_header(Name, Req, Default) when Name =:= <<"sec-websocket-protocol">> -> + parse_header(Name, Req, Default, + fun (Value) -> + cowboy_http:nonempty_list(Value, fun cowboy_http:token/2) + end); +parse_header(Name, Req, Default) -> + {Value, Req2} = header(Name, Req, Default), + {undefined, Value, Req2}. + +parse_header(Name, Req=#http_req{p_headers=PHeaders}, Default, Fun) -> + case header(Name, Req) of + {undefined, Req2} -> + {Default, Req2#http_req{p_headers=[{Name, Default}|PHeaders]}}; + {Value, Req2} -> + case Fun(Value) of + {error, badarg} -> + {error, badarg}; + P -> + {P, Req2#http_req{p_headers=[{Name, P}|PHeaders]}} + end + end. + +%% @equiv cookie(Name, Req, undefined) +-spec cookie(binary(), #http_req{}) + -> {binary() | true | undefined, #http_req{}}. +cookie(Name, Req) when is_binary(Name) -> + cookie(Name, Req, undefined). + +%% @doc Return the cookie value for the given key, or a default if +%% missing. +cookie(Name, Req=#http_req{cookies=undefined}, Default) when is_binary(Name) -> + case header('Cookie', Req) of + {undefined, Req2} -> + {Default, Req2#http_req{cookies=[]}}; + {RawCookie, Req2} -> + Cookies = cowboy_cookies:parse_cookie(RawCookie), + cookie(Name, Req2#http_req{cookies=Cookies}, Default) + end; +cookie(Name, Req, Default) -> + case lists:keyfind(Name, 1, Req#http_req.cookies) of + {Name, Value} -> {Value, Req}; + false -> {Default, Req} + end. + +%% @doc Return the full list of cookie values. +-spec cookies(#http_req{}) -> {list({binary(), binary() | true}), #http_req{}}. +cookies(Req=#http_req{cookies=undefined}) -> + case header('Cookie', Req) of + {undefined, Req2} -> + {[], Req2#http_req{cookies=[]}}; + {RawCookie, Req2} -> + Cookies = cowboy_cookies:parse_cookie(RawCookie), + cookies(Req2#http_req{cookies=Cookies}) + end; +cookies(Req=#http_req{cookies=Cookies}) -> + {Cookies, Req}. + +%% @equiv meta(Name, Req, undefined) +-spec meta(atom(), #http_req{}) -> {any() | undefined, #http_req{}}. +meta(Name, Req) -> + meta(Name, Req, undefined). + +%% @doc Return metadata information about the request. +%% +%% Metadata information varies from one protocol to another. Websockets +%% would define the protocol version here, while REST would use it to +%% indicate which media type, language and charset were retained. +-spec meta(atom(), #http_req{}, any()) -> {any(), #http_req{}}. +meta(Name, Req, Default) -> + case lists:keyfind(Name, 1, Req#http_req.meta) of + {Name, Value} -> {Value, Req}; + false -> {Default, Req} + end. + +%% Request Body API. + +%% @doc Return the full body sent with the request, or {error, badarg} +%% if no Content-Length is available. +%% @todo We probably want to allow a max length. +%% @todo Add multipart support to this function. +-spec body(#http_req{}) -> {ok, binary(), #http_req{}} | {error, atom()}. +body(Req) -> + {Length, Req2} = cowboy_http_req:parse_header('Content-Length', Req), + case Length of + undefined -> {error, badarg}; + {error, badarg} -> {error, badarg}; + _Any -> + body(Length, Req2) + end. + +%% @doc Return Length bytes of the request body. +%% +%% You probably shouldn't be calling this function directly, as it expects the +%% Length argument to be the full size of the body, and will consider +%% the body to be fully read from the socket. +%% @todo We probably want to configure the timeout. +-spec body(non_neg_integer(), #http_req{}) + -> {ok, binary(), #http_req{}} | {error, atom()}. +body(Length, Req=#http_req{body_state=waiting, buffer=Buffer}) + when is_integer(Length) andalso Length =< byte_size(Buffer) -> + << Body:Length/binary, Rest/bits >> = Buffer, + {ok, Body, Req#http_req{body_state=done, buffer=Rest}}; +body(Length, Req=#http_req{socket=Socket, transport=Transport, + body_state=waiting, buffer=Buffer}) -> + case Transport:recv(Socket, Length - byte_size(Buffer), 5000) of + {ok, Body} -> {ok, << Buffer/binary, Body/binary >>, + Req#http_req{body_state=done, buffer= <<>>}}; + {error, Reason} -> {error, Reason} + end. + +%% @doc Return the full body sent with the reqest, parsed as an +%% application/x-www-form-urlencoded string. Essentially a POST query string. +-spec body_qs(#http_req{}) -> {list({binary(), binary() | true}), #http_req{}}. +body_qs(Req=#http_req{urldecode={URLDecFun, URLDecArg}}) -> + {ok, Body, Req2} = body(Req), + {parse_qs(Body, fun(Bin) -> URLDecFun(Bin, URLDecArg) end), Req2}. + +%% Multipart Request API. + +%% @doc Return data from the multipart parser. +%% +%% Use this function for multipart streaming. For each part in the request, +%% this function returns {headers, Headers} followed by a sequence of +%% {data, Data} tuples and finally end_of_part. When there +%% is no part to parse anymore, eof is returned. +%% +%% If the request Content-Type is not a multipart one, {error, badarg} +%% is returned. +-spec multipart_data(#http_req{}) + -> {{headers, cowboy_http:headers()} + | {data, binary()} | end_of_part | eof, + #http_req{}}. +multipart_data(Req=#http_req{body_state=waiting}) -> + {{<<"multipart">>, _SubType, Params}, Req2} = + parse_header('Content-Type', Req), + {_, Boundary} = lists:keyfind(<<"boundary">>, 1, Params), + {Length, Req3=#http_req{buffer=Buffer}} = + parse_header('Content-Length', Req2), + multipart_data(Req3, Length, cowboy_multipart:parser(Boundary), Buffer); +multipart_data(Req=#http_req{body_state={multipart, Length, Cont}}) -> + multipart_data(Req, Length, Cont()); +multipart_data(Req=#http_req{body_state=done}) -> + {eof, Req}. + +multipart_data(Req, Length, Parser, Buffer) when byte_size(Buffer) >= Length -> + << Data:Length/binary, Rest/binary >> = Buffer, + multipart_data(Req#http_req{buffer=Rest}, 0, Parser(Data)); +multipart_data(Req, Length, Parser, Buffer) -> + NewLength = Length - byte_size(Buffer), + multipart_data(Req#http_req{buffer= <<>>}, NewLength, Parser(Buffer)). + +multipart_data(Req, Length, {headers, Headers, Cont}) -> + {{headers, Headers}, Req#http_req{body_state={multipart, Length, Cont}}}; +multipart_data(Req, Length, {body, Data, Cont}) -> + {{body, Data}, Req#http_req{body_state={multipart, Length, Cont}}}; +multipart_data(Req, Length, {end_of_part, Cont}) -> + {end_of_part, Req#http_req{body_state={multipart, Length, Cont}}}; +multipart_data(Req, 0, eof) -> + {eof, Req#http_req{body_state=done}}; +multipart_data(Req=#http_req{socket=Socket, transport=Transport}, + Length, eof) -> + {ok, _Data} = Transport:recv(Socket, Length, 5000), + {eof, Req#http_req{body_state=done}}; +multipart_data(Req=#http_req{socket=Socket, transport=Transport}, + Length, {more, Parser}) when Length > 0 -> + case Transport:recv(Socket, 0, 5000) of + {ok, << Data:Length/binary, Buffer/binary >>} -> + multipart_data(Req#http_req{buffer=Buffer}, 0, Parser(Data)); + {ok, Data} -> + multipart_data(Req, Length - byte_size(Data), Parser(Data)) + end. + +%% @doc Skip a part returned by the multipart parser. +%% +%% This function repeatedly calls multipart_data/1 until +%% end_of_part or eof is parsed. +multipart_skip(Req) -> + case multipart_data(Req) of + {end_of_part, Req2} -> {ok, Req2}; + {eof, Req2} -> {ok, Req2}; + {_Other, Req2} -> multipart_skip(Req2) + end. + +%% Response API. + +%% @doc Add a cookie header to the response. +-spec set_resp_cookie(binary(), binary(), [cowboy_cookies:cookie_option()], + #http_req{}) -> {ok, #http_req{}}. +set_resp_cookie(Name, Value, Options, Req) -> + {HeaderName, HeaderValue} = cowboy_cookies:cookie(Name, Value, Options), + set_resp_header(HeaderName, HeaderValue, Req). + +%% @doc Add a header to the response. +set_resp_header(Name, Value, Req=#http_req{resp_headers=RespHeaders}) -> + NameBin = header_to_binary(Name), + {ok, Req#http_req{resp_headers=[{NameBin, Value}|RespHeaders]}}. + +%% @doc Add a body to the response. +%% +%% The body set here is ignored if the response is later sent using +%% anything other than reply/2 or reply/3. The response body is expected +%% to be a binary or an iolist. +set_resp_body(Body, Req) -> + {ok, Req#http_req{resp_body=Body}}. + + +%% @doc Add a body function to the response. +%% +%% The response body may also be set to a content-length - stream-function pair. +%% If the response body is of this type normal response headers will be sent. +%% After the response headers has been sent the body function is applied. +%% The body function is expected to write the response body directly to the +%% socket using the transport module. +%% +%% If the body function crashes while writing the response body or writes fewer +%% bytes than declared the behaviour is undefined. The body set here is ignored +%% if the response is later sent using anything other than `reply/2' or +%% `reply/3'. +%% +%% @see cowboy_http_req:transport/1. +-spec set_resp_body_fun(non_neg_integer(), fun(() -> {sent, non_neg_integer()}), + #http_req{}) -> {ok, #http_req{}}. +set_resp_body_fun(StreamLen, StreamFun, Req) -> + {ok, Req#http_req{resp_body={StreamLen, StreamFun}}}. + + +%% @doc Return whether the given header has been set for the response. +has_resp_header(Name, #http_req{resp_headers=RespHeaders}) -> + NameBin = header_to_binary(Name), + lists:keymember(NameBin, 1, RespHeaders). + +%% @doc Return whether a body has been set for the response. +has_resp_body(#http_req{resp_body={Length, _}}) -> + Length > 0; +has_resp_body(#http_req{resp_body=RespBody}) -> + iolist_size(RespBody) > 0. + +%% @equiv reply(Status, [], [], Req) +-spec reply(cowboy_http:status(), #http_req{}) -> {ok, #http_req{}}. +reply(Status, Req=#http_req{resp_body=Body}) -> + reply(Status, [], Body, Req). + +%% @equiv reply(Status, Headers, [], Req) +-spec reply(cowboy_http:status(), cowboy_http:headers(), #http_req{}) + -> {ok, #http_req{}}. +reply(Status, Headers, Req=#http_req{resp_body=Body}) -> + reply(Status, Headers, Body, Req). + +%% @doc Send a reply to the client. +reply(Status, Headers, Body, Req=#http_req{socket=Socket, + transport=Transport, connection=Connection, pid=ReqPid, + method=Method, resp_state=waiting, resp_headers=RespHeaders}) -> + RespConn = response_connection(Headers, Connection), + ContentLen = case Body of {CL, _} -> CL; _ -> iolist_size(Body) end, + Head = response_head(Status, Headers, RespHeaders, [ + {<<"Connection">>, atom_to_connection(Connection)}, + {<<"Content-Length">>, integer_to_list(ContentLen)}, + {<<"Date">>, cowboy_clock:rfc1123()}, + {<<"Server">>, <<"Cowboy">>} + ]), + case {Method, Body} of + {'HEAD', _} -> Transport:send(Socket, Head); + {_, {_, StreamFun}} -> Transport:send(Socket, Head), StreamFun(); + {_, _} -> Transport:send(Socket, [Head, Body]) + end, + ReqPid ! {?MODULE, resp_sent}, + {ok, Req#http_req{connection=RespConn, resp_state=done, + resp_headers=[], resp_body= <<>>}}. + +%% @equiv chunked_reply(Status, [], Req) +-spec chunked_reply(cowboy_http:status(), #http_req{}) -> {ok, #http_req{}}. +chunked_reply(Status, Req) -> + chunked_reply(Status, [], Req). + +%% @doc Initiate the sending of a chunked reply to the client. +%% @see cowboy_http_req:chunk/2 +-spec chunked_reply(cowboy_http:status(), cowboy_http:headers(), #http_req{}) + -> {ok, #http_req{}}. +chunked_reply(Status, Headers, Req=#http_req{socket=Socket, + transport=Transport, connection=Connection, pid=ReqPid, + resp_state=waiting, resp_headers=RespHeaders}) -> + RespConn = response_connection(Headers, Connection), + Head = response_head(Status, Headers, RespHeaders, [ + {<<"Connection">>, atom_to_connection(Connection)}, + {<<"Transfer-Encoding">>, <<"chunked">>}, + {<<"Date">>, cowboy_clock:rfc1123()}, + {<<"Server">>, <<"Cowboy">>} + ]), + Transport:send(Socket, Head), + ReqPid ! {?MODULE, resp_sent}, + {ok, Req#http_req{connection=RespConn, resp_state=chunks, + resp_headers=[], resp_body= <<>>}}. + +%% @doc Send a chunk of data. +%% +%% A chunked reply must have been initiated before calling this function. +chunk(_Data, #http_req{socket=_Socket, transport=_Transport, method='HEAD'}) -> + ok; +chunk(Data, #http_req{socket=Socket, transport=Transport, resp_state=chunks}) -> + Transport:send(Socket, [erlang:integer_to_list(iolist_size(Data), 16), + <<"\r\n">>, Data, <<"\r\n">>]). + +%% @doc Send an upgrade reply. +%% @private +-spec upgrade_reply(cowboy_http:status(), cowboy_http:headers(), #http_req{}) + -> {ok, #http_req{}}. +upgrade_reply(Status, Headers, Req=#http_req{socket=Socket, transport=Transport, + pid=ReqPid, resp_state=waiting, resp_headers=RespHeaders}) -> + Head = response_head(Status, Headers, RespHeaders, [ + {<<"Connection">>, <<"Upgrade">>} + ]), + Transport:send(Socket, Head), + ReqPid ! {?MODULE, resp_sent}, + {ok, Req#http_req{resp_state=done, resp_headers=[], resp_body= <<>>}}. + +%% Misc API. + +%% @doc Compact the request data by removing all non-system information. +%% +%% This essentially removes the host, path, query string, bindings and headers. +%% Use it when you really need to save up memory, for example when having +%% many concurrent long-running connections. +-spec compact(#http_req{}) -> #http_req{}. +compact(Req) -> + Req#http_req{host=undefined, host_info=undefined, path=undefined, + path_info=undefined, qs_vals=undefined, + bindings=undefined, headers=[], + p_headers=[], cookies=[]}. + +%% @doc Return the transport module and socket associated with a request. +%% +%% This exposes the same socket interface used internally by the HTTP protocol +%% implementation to developers that needs low level access to the socket. +%% +%% It is preferred to use this in conjuction with the stream function support +%% in `set_resp_body_fun/3' if this is used to write a response body directly +%% to the socket. This ensures that the response headers are set correctly. +-spec transport(#http_req{}) -> {ok, module(), inet:socket()}. +transport(#http_req{transport=Transport, socket=Socket}) -> + {ok, Transport, Socket}. + +%% Internal. + +-spec parse_qs(binary(), fun((binary()) -> binary())) -> + list({binary(), binary() | true}). +parse_qs(<<>>, _URLDecode) -> + []; +parse_qs(Qs, URLDecode) -> + Tokens = binary:split(Qs, <<"&">>, [global, trim]), + [case binary:split(Token, <<"=">>) of + [Token] -> {URLDecode(Token), true}; + [Name, Value] -> {URLDecode(Name), URLDecode(Value)} + end || Token <- Tokens]. + +-spec response_connection(cowboy_http:headers(), keepalive | close) + -> keepalive | close. +response_connection([], Connection) -> + Connection; +response_connection([{Name, Value}|Tail], Connection) -> + case Name of + 'Connection' -> response_connection_parse(Value); + Name when is_atom(Name) -> response_connection(Tail, Connection); + Name -> + Name2 = cowboy_bstr:to_lower(Name), + case Name2 of + <<"connection">> -> response_connection_parse(Value); + _Any -> response_connection(Tail, Connection) + end + end. + +-spec response_connection_parse(binary()) -> keepalive | close. +response_connection_parse(ReplyConn) -> + Tokens = cowboy_http:nonempty_list(ReplyConn, fun cowboy_http:token/2), + cowboy_http:connection_to_atom(Tokens). + +-spec response_head(cowboy_http:status(), cowboy_http:headers(), + cowboy_http:headers(), cowboy_http:headers()) -> iolist(). +response_head(Status, Headers, RespHeaders, DefaultHeaders) -> + StatusLine = <<"HTTP/1.1 ", (status(Status))/binary, "\r\n">>, + Headers2 = [{header_to_binary(Key), Value} || {Key, Value} <- Headers], + Headers3 = merge_headers( + merge_headers(Headers2, RespHeaders), + DefaultHeaders), + Headers4 = [[Key, <<": ">>, Value, <<"\r\n">>] + || {Key, Value} <- Headers3], + [StatusLine, Headers4, <<"\r\n">>]. + +-spec merge_headers(cowboy_http:headers(), cowboy_http:headers()) + -> cowboy_http:headers(). +merge_headers(Headers, []) -> + Headers; +merge_headers(Headers, [{Name, Value}|Tail]) -> + Headers2 = case lists:keymember(Name, 1, Headers) of + true -> Headers; + false -> Headers ++ [{Name, Value}] + end, + merge_headers(Headers2, Tail). + +-spec atom_to_connection(keepalive) -> <<_:80>>; + (close) -> <<_:40>>. +atom_to_connection(keepalive) -> + <<"keep-alive">>; +atom_to_connection(close) -> + <<"close">>. + +-spec status(cowboy_http:status()) -> binary(). +status(100) -> <<"100 Continue">>; +status(101) -> <<"101 Switching Protocols">>; +status(102) -> <<"102 Processing">>; +status(200) -> <<"200 OK">>; +status(201) -> <<"201 Created">>; +status(202) -> <<"202 Accepted">>; +status(203) -> <<"203 Non-Authoritative Information">>; +status(204) -> <<"204 No Content">>; +status(205) -> <<"205 Reset Content">>; +status(206) -> <<"206 Partial Content">>; +status(207) -> <<"207 Multi-Status">>; +status(226) -> <<"226 IM Used">>; +status(300) -> <<"300 Multiple Choices">>; +status(301) -> <<"301 Moved Permanently">>; +status(302) -> <<"302 Found">>; +status(303) -> <<"303 See Other">>; +status(304) -> <<"304 Not Modified">>; +status(305) -> <<"305 Use Proxy">>; +status(306) -> <<"306 Switch Proxy">>; +status(307) -> <<"307 Temporary Redirect">>; +status(400) -> <<"400 Bad Request">>; +status(401) -> <<"401 Unauthorized">>; +status(402) -> <<"402 Payment Required">>; +status(403) -> <<"403 Forbidden">>; +status(404) -> <<"404 Not Found">>; +status(405) -> <<"405 Method Not Allowed">>; +status(406) -> <<"406 Not Acceptable">>; +status(407) -> <<"407 Proxy Authentication Required">>; +status(408) -> <<"408 Request Timeout">>; +status(409) -> <<"409 Conflict">>; +status(410) -> <<"410 Gone">>; +status(411) -> <<"411 Length Required">>; +status(412) -> <<"412 Precondition Failed">>; +status(413) -> <<"413 Request Entity Too Large">>; +status(414) -> <<"414 Request-URI Too Long">>; +status(415) -> <<"415 Unsupported Media Type">>; +status(416) -> <<"416 Requested Range Not Satisfiable">>; +status(417) -> <<"417 Expectation Failed">>; +status(418) -> <<"418 I'm a teapot">>; +status(422) -> <<"422 Unprocessable Entity">>; +status(423) -> <<"423 Locked">>; +status(424) -> <<"424 Failed Dependency">>; +status(425) -> <<"425 Unordered Collection">>; +status(426) -> <<"426 Upgrade Required">>; +status(500) -> <<"500 Internal Server Error">>; +status(501) -> <<"501 Not Implemented">>; +status(502) -> <<"502 Bad Gateway">>; +status(503) -> <<"503 Service Unavailable">>; +status(504) -> <<"504 Gateway Timeout">>; +status(505) -> <<"505 HTTP Version Not Supported">>; +status(506) -> <<"506 Variant Also Negotiates">>; +status(507) -> <<"507 Insufficient Storage">>; +status(510) -> <<"510 Not Extended">>; +status(B) when is_binary(B) -> B. + +-spec header_to_binary(cowboy_http:header()) -> binary(). +header_to_binary('Cache-Control') -> <<"Cache-Control">>; +header_to_binary('Connection') -> <<"Connection">>; +header_to_binary('Date') -> <<"Date">>; +header_to_binary('Pragma') -> <<"Pragma">>; +header_to_binary('Transfer-Encoding') -> <<"Transfer-Encoding">>; +header_to_binary('Upgrade') -> <<"Upgrade">>; +header_to_binary('Via') -> <<"Via">>; +header_to_binary('Accept') -> <<"Accept">>; +header_to_binary('Accept-Charset') -> <<"Accept-Charset">>; +header_to_binary('Accept-Encoding') -> <<"Accept-Encoding">>; +header_to_binary('Accept-Language') -> <<"Accept-Language">>; +header_to_binary('Authorization') -> <<"Authorization">>; +header_to_binary('From') -> <<"From">>; +header_to_binary('Host') -> <<"Host">>; +header_to_binary('If-Modified-Since') -> <<"If-Modified-Since">>; +header_to_binary('If-Match') -> <<"If-Match">>; +header_to_binary('If-None-Match') -> <<"If-None-Match">>; +header_to_binary('If-Range') -> <<"If-Range">>; +header_to_binary('If-Unmodified-Since') -> <<"If-Unmodified-Since">>; +header_to_binary('Max-Forwards') -> <<"Max-Forwards">>; +header_to_binary('Proxy-Authorization') -> <<"Proxy-Authorization">>; +header_to_binary('Range') -> <<"Range">>; +header_to_binary('Referer') -> <<"Referer">>; +header_to_binary('User-Agent') -> <<"User-Agent">>; +header_to_binary('Age') -> <<"Age">>; +header_to_binary('Location') -> <<"Location">>; +header_to_binary('Proxy-Authenticate') -> <<"Proxy-Authenticate">>; +header_to_binary('Public') -> <<"Public">>; +header_to_binary('Retry-After') -> <<"Retry-After">>; +header_to_binary('Server') -> <<"Server">>; +header_to_binary('Vary') -> <<"Vary">>; +header_to_binary('Warning') -> <<"Warning">>; +header_to_binary('Www-Authenticate') -> <<"Www-Authenticate">>; +header_to_binary('Allow') -> <<"Allow">>; +header_to_binary('Content-Base') -> <<"Content-Base">>; +header_to_binary('Content-Encoding') -> <<"Content-Encoding">>; +header_to_binary('Content-Language') -> <<"Content-Language">>; +header_to_binary('Content-Length') -> <<"Content-Length">>; +header_to_binary('Content-Location') -> <<"Content-Location">>; +header_to_binary('Content-Md5') -> <<"Content-Md5">>; +header_to_binary('Content-Range') -> <<"Content-Range">>; +header_to_binary('Content-Type') -> <<"Content-Type">>; +header_to_binary('Etag') -> <<"Etag">>; +header_to_binary('Expires') -> <<"Expires">>; +header_to_binary('Last-Modified') -> <<"Last-Modified">>; +header_to_binary('Accept-Ranges') -> <<"Accept-Ranges">>; +header_to_binary('Set-Cookie') -> <<"Set-Cookie">>; +header_to_binary('Set-Cookie2') -> <<"Set-Cookie2">>; +header_to_binary('X-Forwarded-For') -> <<"X-Forwarded-For">>; +header_to_binary('Cookie') -> <<"Cookie">>; +header_to_binary('Keep-Alive') -> <<"Keep-Alive">>; +header_to_binary('Proxy-Connection') -> <<"Proxy-Connection">>; +header_to_binary(B) when is_binary(B) -> B. + +%% Tests. + +-ifdef(TEST). + +parse_qs_test_() -> + %% {Qs, Result} + Tests = [ + {<<"">>, []}, + {<<"a=b">>, [{<<"a">>, <<"b">>}]}, + {<<"aaa=bbb">>, [{<<"aaa">>, <<"bbb">>}]}, + {<<"a&b">>, [{<<"a">>, true}, {<<"b">>, true}]}, + {<<"a=b&c&d=e">>, [{<<"a">>, <<"b">>}, + {<<"c">>, true}, {<<"d">>, <<"e">>}]}, + {<<"a=b=c=d=e&f=g">>, [{<<"a">>, <<"b=c=d=e">>}, {<<"f">>, <<"g">>}]}, + {<<"a+b=c+d">>, [{<<"a b">>, <<"c d">>}]} + ], + URLDecode = fun cowboy_http:urldecode/1, + [{Qs, fun() -> R = parse_qs(Qs, URLDecode) end} || {Qs, R} <- Tests]. + +-endif. diff --git a/rabbitmq-server/plugins-src/cowboy-wrapper/cowboy-git/src/cowboy_http_rest.erl b/rabbitmq-server/plugins-src/cowboy-wrapper/cowboy-git/src/cowboy_http_rest.erl new file mode 100644 index 0000000..e6cc6ff --- /dev/null +++ b/rabbitmq-server/plugins-src/cowboy-wrapper/cowboy-git/src/cowboy_http_rest.erl @@ -0,0 +1,905 @@ +%% Copyright (c) 2011, Loïc Hoguin +%% +%% Permission to use, copy, modify, and/or distribute this software for any +%% purpose with or without fee is hereby granted, provided that the above +%% copyright notice and this permission notice appear in all copies. +%% +%% THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES +%% WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF +%% MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR +%% ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES +%% WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN +%% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF +%% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + +%% @doc Experimental REST protocol implementation. +%% +%% Based on the Webmachine Diagram from Alan Dean and Justin Sheehy, which +%% can be found in the Webmachine source tree, and on the Webmachine +%% documentation available at http://wiki.basho.com/Webmachine.html +%% at the time of writing. +-module(cowboy_http_rest). +-export([upgrade/4]). + +-record(state, { + %% Handler. + handler :: atom(), + handler_state :: any(), + + %% Media type. + content_types_p = [] :: + [{{binary(), binary(), [{binary(), binary()}]}, atom()}], + content_type_a :: undefined + | {{binary(), binary(), [{binary(), binary()}]}, atom()}, + + %% Language. + languages_p = [] :: [binary()], + language_a :: undefined | binary(), + + %% Charset. + charsets_p = [] :: [binary()], + charset_a :: undefined | binary(), + + %% Cached resource calls. + etag :: undefined | no_call | binary(), + last_modified :: undefined | no_call | calendar:datetime(), + expires :: undefined | no_call | calendar:datetime() +}). + +-include("include/http.hrl"). + +%% @doc Upgrade a HTTP request to the REST protocol. +%% +%% You do not need to call this function manually. To upgrade to the REST +%% protocol, you simply need to return {upgrade, protocol, {@module}} +%% in your cowboy_http_handler:init/3 handler function. +-spec upgrade(pid(), module(), any(), #http_req{}) + -> {ok, #http_req{}} | close. +upgrade(_ListenerPid, Handler, Opts, Req) -> + try + case erlang:function_exported(Handler, rest_init, 2) of + true -> + case Handler:rest_init(Req, Opts) of + {ok, Req2, HandlerState} -> + service_available(Req2, #state{handler=Handler, + handler_state=HandlerState}) + end; + false -> + service_available(Req, #state{handler=Handler}) + end + catch Class:Reason -> + error_logger:error_msg( + "** Handler ~p terminating in rest_init/3~n" + " for the reason ~p:~p~n** Options were ~p~n" + "** Request was ~p~n** Stacktrace: ~p~n~n", + [Handler, Class, Reason, Opts, Req, erlang:get_stacktrace()]), + {ok, _Req2} = cowboy_http_req:reply(500, Req), + close + end. + +service_available(Req, State) -> + expect(Req, State, service_available, true, fun known_methods/2, 503). + +%% known_methods/2 should return a list of atoms or binary methods. +known_methods(Req=#http_req{method=Method}, State) -> + case call(Req, State, known_methods) of + no_call when Method =:= 'HEAD'; Method =:= 'GET'; Method =:= 'POST'; + Method =:= 'PUT'; Method =:= 'DELETE'; Method =:= 'TRACE'; + Method =:= 'CONNECT'; Method =:= 'OPTIONS' -> + next(Req, State, fun uri_too_long/2); + no_call -> + next(Req, State, 501); + {halt, Req2, HandlerState} -> + terminate(Req2, State#state{handler_state=HandlerState}); + {List, Req2, HandlerState} -> + State2 = State#state{handler_state=HandlerState}, + case lists:member(Method, List) of + true -> next(Req2, State2, fun uri_too_long/2); + false -> next(Req2, State2, 501) + end + end. + +uri_too_long(Req, State) -> + expect(Req, State, uri_too_long, false, fun allowed_methods/2, 414). + +%% allowed_methods/2 should return a list of atoms or binary methods. +allowed_methods(Req=#http_req{method=Method}, State) -> + case call(Req, State, allowed_methods) of + no_call when Method =:= 'HEAD'; Method =:= 'GET' -> + next(Req, State, fun malformed_request/2); + no_call -> + method_not_allowed(Req, State, ['GET', 'HEAD']); + {halt, Req2, HandlerState} -> + terminate(Req2, State#state{handler_state=HandlerState}); + {List, Req2, HandlerState} -> + State2 = State#state{handler_state=HandlerState}, + case lists:member(Method, List) of + true -> next(Req2, State2, fun malformed_request/2); + false -> method_not_allowed(Req2, State2, List) + end + end. + +method_not_allowed(Req, State, Methods) -> + {ok, Req2} = cowboy_http_req:set_resp_header( + <<"Allow">>, method_not_allowed_build(Methods, []), Req), + respond(Req2, State, 405). + +method_not_allowed_build([], []) -> + <<>>; +method_not_allowed_build([], [_Ignore|Acc]) -> + lists:reverse(Acc); +method_not_allowed_build([Method|Tail], Acc) when is_atom(Method) -> + Method2 = list_to_binary(atom_to_list(Method)), + method_not_allowed_build(Tail, [<<", ">>, Method2|Acc]); +method_not_allowed_build([Method|Tail], Acc) -> + method_not_allowed_build(Tail, [<<", ">>, Method|Acc]). + +malformed_request(Req, State) -> + expect(Req, State, malformed_request, false, fun is_authorized/2, 400). + +%% is_authorized/2 should return true or {false, WwwAuthenticateHeader}. +is_authorized(Req, State) -> + case call(Req, State, is_authorized) of + no_call -> + forbidden(Req, State); + {halt, Req2, HandlerState} -> + terminate(Req2, State#state{handler_state=HandlerState}); + {true, Req2, HandlerState} -> + forbidden(Req2, State#state{handler_state=HandlerState}); + {{false, AuthHead}, Req2, HandlerState} -> + {ok, Req3} = cowboy_http_req:set_resp_header( + <<"Www-Authenticate">>, AuthHead, Req2), + respond(Req3, State#state{handler_state=HandlerState}, 401) + end. + +forbidden(Req, State) -> + expect(Req, State, forbidden, false, fun valid_content_headers/2, 403). + +valid_content_headers(Req, State) -> + expect(Req, State, valid_content_headers, true, + fun known_content_type/2, 501). + +known_content_type(Req, State) -> + expect(Req, State, known_content_type, true, + fun valid_entity_length/2, 413). + +valid_entity_length(Req, State) -> + expect(Req, State, valid_entity_length, true, fun options/2, 413). + +%% If you need to add additional headers to the response at this point, +%% you should do it directly in the options/2 call using set_resp_headers. +options(Req=#http_req{method='OPTIONS'}, State) -> + case call(Req, State, options) of + {halt, Req2, HandlerState} -> + terminate(Req2, State#state{handler_state=HandlerState}); + {ok, Req2, HandlerState} -> + respond(Req2, State#state{handler_state=HandlerState}, 200) + end; +options(Req, State) -> + content_types_provided(Req, State). + +%% content_types_provided/2 should return a list of content types and their +%% associated callback function as a tuple: {{Type, SubType, Params}, Fun}. +%% Type and SubType are the media type as binary. Params is a list of +%% Key/Value tuple, with Key and Value a binary. Fun is the name of the +%% callback that will be used to return the content of the response. It is +%% given as an atom. +%% +%% An example of such return value would be: +%% {{<<"text">>, <<"html">>, []}, to_html} +%% +%% Note that it is also possible to return a binary content type that will +%% then be parsed by Cowboy. However note that while this may make your +%% resources a little more readable, this is a lot less efficient. An example +%% of such a return value would be: +%% {<<"text/html">>, to_html} +content_types_provided(Req=#http_req{meta=Meta}, State) -> + case call(Req, State, content_types_provided) of + no_call -> + not_acceptable(Req, State); + {halt, Req2, HandlerState} -> + terminate(Req2, State#state{handler_state=HandlerState}); + {[], Req2, HandlerState} -> + not_acceptable(Req2, State#state{handler_state=HandlerState}); + {CTP, Req2, HandlerState} -> + CTP2 = [normalize_content_types_provided(P) || P <- CTP], + State2 = State#state{ + handler_state=HandlerState, content_types_p=CTP2}, + {Accept, Req3} = cowboy_http_req:parse_header('Accept', Req2), + case Accept of + undefined -> + {PMT, _Fun} = HeadCTP = hd(CTP2), + languages_provided( + Req3#http_req{meta=[{media_type, PMT}|Meta]}, + State2#state{content_type_a=HeadCTP}); + Accept -> + Accept2 = prioritize_accept(Accept), + choose_media_type(Req3, State2, Accept2) + end + end. + +normalize_content_types_provided({ContentType, Handler}) + when is_binary(ContentType) -> + {cowboy_http:content_type(ContentType), Handler}; +normalize_content_types_provided(Provided) -> + Provided. + +prioritize_accept(Accept) -> + lists:sort( + fun ({MediaTypeA, Quality, _AcceptParamsA}, + {MediaTypeB, Quality, _AcceptParamsB}) -> + %% Same quality, check precedence in more details. + prioritize_mediatype(MediaTypeA, MediaTypeB); + ({_MediaTypeA, QualityA, _AcceptParamsA}, + {_MediaTypeB, QualityB, _AcceptParamsB}) -> + %% Just compare the quality. + QualityA > QualityB + end, Accept). + +%% Media ranges can be overridden by more specific media ranges or +%% specific media types. If more than one media range applies to a given +%% type, the most specific reference has precedence. +%% +%% We always choose B over A when we can't decide between the two. +prioritize_mediatype({TypeA, SubTypeA, ParamsA}, {TypeB, SubTypeB, ParamsB}) -> + case TypeB of + TypeA -> + case SubTypeB of + SubTypeA -> length(ParamsA) > length(ParamsB); + <<"*">> -> true; + _Any -> false + end; + <<"*">> -> true; + _Any -> false + end. + +%% Ignoring the rare AcceptParams. Not sure what should be done about them. +choose_media_type(Req, State, []) -> + not_acceptable(Req, State); +choose_media_type(Req, State=#state{content_types_p=CTP}, + [MediaType|Tail]) -> + match_media_type(Req, State, Tail, CTP, MediaType). + +match_media_type(Req, State, Accept, [], _MediaType) -> + choose_media_type(Req, State, Accept); +match_media_type(Req, State, Accept, CTP, + MediaType = {{<<"*">>, <<"*">>, _Params_A}, _QA, _APA}) -> + match_media_type_params(Req, State, Accept, CTP, MediaType); +match_media_type(Req, State, Accept, + CTP = [{{Type, SubType_P, _PP}, _Fun}|_Tail], + MediaType = {{Type, SubType_A, _PA}, _QA, _APA}) + when SubType_P =:= SubType_A; SubType_A =:= <<"*">> -> + match_media_type_params(Req, State, Accept, CTP, MediaType); +match_media_type(Req, State, Accept, [_Any|Tail], MediaType) -> + match_media_type(Req, State, Accept, Tail, MediaType). + +match_media_type_params(Req=#http_req{meta=Meta}, State, Accept, + [Provided = {PMT = {_TP, _STP, Params_P}, _Fun}|Tail], + MediaType = {{_TA, _STA, Params_A}, _QA, _APA}) -> + case lists:sort(Params_P) =:= lists:sort(Params_A) of + true -> + languages_provided(Req#http_req{meta=[{media_type, PMT}|Meta]}, + State#state{content_type_a=Provided}); + false -> + match_media_type(Req, State, Accept, Tail, MediaType) + end. + +%% languages_provided should return a list of binary values indicating +%% which languages are accepted by the resource. +%% +%% @todo I suppose we should also ask the resource if it wants to +%% set a language itself or if it wants it to be automatically chosen. +languages_provided(Req, State) -> + case call(Req, State, languages_provided) of + no_call -> + charsets_provided(Req, State); + {halt, Req2, HandlerState} -> + terminate(Req2, State#state{handler_state=HandlerState}); + {[], Req2, HandlerState} -> + not_acceptable(Req2, State#state{handler_state=HandlerState}); + {LP, Req2, HandlerState} -> + State2 = State#state{handler_state=HandlerState, languages_p=LP}, + {AcceptLanguage, Req3} = + cowboy_http_req:parse_header('Accept-Language', Req2), + case AcceptLanguage of + undefined -> + set_language(Req3, State2#state{language_a=hd(LP)}); + AcceptLanguage -> + AcceptLanguage2 = prioritize_languages(AcceptLanguage), + choose_language(Req3, State2, AcceptLanguage2) + end + end. + +%% A language-range matches a language-tag if it exactly equals the tag, +%% or if it exactly equals a prefix of the tag such that the first tag +%% character following the prefix is "-". The special range "*", if +%% present in the Accept-Language field, matches every tag not matched +%% by any other range present in the Accept-Language field. +%% +%% @todo The last sentence probably means we should always put '*' +%% at the end of the list. +prioritize_languages(AcceptLanguages) -> + lists:sort( + fun ({_TagA, QualityA}, {_TagB, QualityB}) -> + QualityA > QualityB + end, AcceptLanguages). + +choose_language(Req, State, []) -> + not_acceptable(Req, State); +choose_language(Req, State=#state{languages_p=LP}, [Language|Tail]) -> + match_language(Req, State, Tail, LP, Language). + +match_language(Req, State, Accept, [], _Language) -> + choose_language(Req, State, Accept); +match_language(Req, State, _Accept, [Provided|_Tail], {'*', _Quality}) -> + set_language(Req, State#state{language_a=Provided}); +match_language(Req, State, _Accept, [Provided|_Tail], {Provided, _Quality}) -> + set_language(Req, State#state{language_a=Provided}); +match_language(Req, State, Accept, [Provided|Tail], + Language = {Tag, _Quality}) -> + Length = byte_size(Tag), + case Provided of + << Tag:Length/binary, $-, _Any/bits >> -> + set_language(Req, State#state{language_a=Provided}); + _Any -> + match_language(Req, State, Accept, Tail, Language) + end. + +set_language(Req=#http_req{meta=Meta}, State=#state{language_a=Language}) -> + {ok, Req2} = cowboy_http_req:set_resp_header( + <<"Content-Language">>, Language, Req), + charsets_provided(Req2#http_req{meta=[{language, Language}|Meta]}, State). + +%% charsets_provided should return a list of binary values indicating +%% which charsets are accepted by the resource. +charsets_provided(Req, State) -> + case call(Req, State, charsets_provided) of + no_call -> + set_content_type(Req, State); + {halt, Req2, HandlerState} -> + terminate(Req2, State#state{handler_state=HandlerState}); + {[], Req2, HandlerState} -> + not_acceptable(Req2, State#state{handler_state=HandlerState}); + {CP, Req2, HandlerState} -> + State2 = State#state{handler_state=HandlerState, charsets_p=CP}, + {AcceptCharset, Req3} = + cowboy_http_req:parse_header('Accept-Charset', Req2), + case AcceptCharset of + undefined -> + set_content_type(Req3, State2#state{charset_a=hd(CP)}); + AcceptCharset -> + AcceptCharset2 = prioritize_charsets(AcceptCharset), + choose_charset(Req3, State2, AcceptCharset2) + end + end. + +%% The special value "*", if present in the Accept-Charset field, +%% matches every character set (including ISO-8859-1) which is not +%% mentioned elsewhere in the Accept-Charset field. If no "*" is present +%% in an Accept-Charset field, then all character sets not explicitly +%% mentioned get a quality value of 0, except for ISO-8859-1, which gets +%% a quality value of 1 if not explicitly mentioned. +prioritize_charsets(AcceptCharsets) -> + AcceptCharsets2 = lists:sort( + fun ({_CharsetA, QualityA}, {_CharsetB, QualityB}) -> + QualityA > QualityB + end, AcceptCharsets), + case lists:keymember(<<"*">>, 1, AcceptCharsets2) of + true -> AcceptCharsets2; + false -> [{<<"iso-8859-1">>, 1000}|AcceptCharsets2] + end. + +choose_charset(Req, State, []) -> + not_acceptable(Req, State); +choose_charset(Req, State=#state{charsets_p=CP}, [Charset|Tail]) -> + match_charset(Req, State, Tail, CP, Charset). + +match_charset(Req, State, Accept, [], _Charset) -> + choose_charset(Req, State, Accept); +match_charset(Req, State, _Accept, [Provided|_Tail], + {Provided, _Quality}) -> + set_content_type(Req, State#state{charset_a=Provided}); +match_charset(Req, State, Accept, [_Provided|Tail], Charset) -> + match_charset(Req, State, Accept, Tail, Charset). + +set_content_type(Req=#http_req{meta=Meta}, State=#state{ + content_type_a={{Type, SubType, Params}, _Fun}, + charset_a=Charset}) -> + ParamsBin = set_content_type_build_params(Params, []), + ContentType = [Type, <<"/">>, SubType, ParamsBin], + ContentType2 = case Charset of + undefined -> ContentType; + Charset -> [ContentType, <<"; charset=">>, Charset] + end, + {ok, Req2} = cowboy_http_req:set_resp_header( + <<"Content-Type">>, ContentType2, Req), + encodings_provided(Req2#http_req{meta=[{charset, Charset}|Meta]}, State). + +set_content_type_build_params([], []) -> + <<>>; +set_content_type_build_params([], Acc) -> + lists:reverse(Acc); +set_content_type_build_params([{Attr, Value}|Tail], Acc) -> + set_content_type_build_params(Tail, [[Attr, <<"=">>, Value], <<";">>|Acc]). + +%% @todo Match for identity as we provide nothing else for now. +%% @todo Don't forget to set the Content-Encoding header when we reply a body +%% and the found encoding is something other than identity. +encodings_provided(Req, State) -> + variances(Req, State). + +not_acceptable(Req, State) -> + respond(Req, State, 406). + +%% variances/2 should return a list of headers that will be added +%% to the Vary response header. The Accept, Accept-Language, +%% Accept-Charset and Accept-Encoding headers do not need to be +%% specified. +%% +%% @todo Do Accept-Encoding too when we handle it. +%% @todo Does the order matter? +variances(Req, State=#state{content_types_p=CTP, + languages_p=LP, charsets_p=CP}) -> + Variances = case CTP of + [] -> []; + [_] -> []; + [_|_] -> [<<"Accept">>] + end, + Variances2 = case LP of + [] -> Variances; + [_] -> Variances; + [_|_] -> [<<"Accept-Language">>|Variances] + end, + Variances3 = case CP of + [] -> Variances2; + [_] -> Variances2; + [_|_] -> [<<"Accept-Charset">>|Variances2] + end, + {Variances4, Req3, State2} = case call(Req, State, variances) of + no_call -> + {Variances3, Req, State}; + {HandlerVariances, Req2, HandlerState} -> + {Variances3 ++ HandlerVariances, Req2, + State#state{handler_state=HandlerState}} + end, + case [[<<", ">>, V] || V <- Variances4] of + [] -> + resource_exists(Req3, State2); + [[<<", ">>, H]|Variances5] -> + {ok, Req4} = cowboy_http_req:set_resp_header( + <<"Variances">>, [H|Variances5], Req3), + resource_exists(Req4, State2) + end. + +resource_exists(Req, State) -> + expect(Req, State, resource_exists, true, + fun if_match_exists/2, fun if_match_musnt_exist/2). + +if_match_exists(Req, State) -> + case cowboy_http_req:parse_header('If-Match', Req) of + {undefined, Req2} -> + if_unmodified_since_exists(Req2, State); + {'*', Req2} -> + if_unmodified_since_exists(Req2, State); + {ETagsList, Req2} -> + if_match(Req2, State, ETagsList) + end. + +if_match(Req, State, EtagsList) -> + {Etag, Req2, State2} = generate_etag(Req, State), + case Etag of + no_call -> + precondition_failed(Req2, State2); + Etag -> + case lists:member(Etag, EtagsList) of + true -> if_unmodified_since_exists(Req2, State2); + false -> precondition_failed(Req2, State2) + end + end. + +if_match_musnt_exist(Req, State) -> + case cowboy_http_req:header('If-Match', Req) of + {undefined, Req2} -> is_put_to_missing_resource(Req2, State); + {_Any, Req2} -> precondition_failed(Req2, State) + end. + +if_unmodified_since_exists(Req, State) -> + case cowboy_http_req:parse_header('If-Unmodified-Since', Req) of + {undefined, Req2} -> + if_none_match_exists(Req2, State); + {{error, badarg}, Req2} -> + if_none_match_exists(Req2, State); + {IfUnmodifiedSince, Req2} -> + if_unmodified_since(Req2, State, IfUnmodifiedSince) + end. + +%% If LastModified is the atom 'no_call', we continue. +if_unmodified_since(Req, State, IfUnmodifiedSince) -> + {LastModified, Req2, State2} = last_modified(Req, State), + case LastModified > IfUnmodifiedSince of + true -> precondition_failed(Req2, State2); + false -> if_none_match_exists(Req2, State2) + end. + +if_none_match_exists(Req, State) -> + case cowboy_http_req:parse_header('If-None-Match', Req) of + {undefined, Req2} -> + if_modified_since_exists(Req2, State); + {'*', Req2} -> + precondition_is_head_get(Req2, State); + {EtagsList, Req2} -> + if_none_match(Req2, State, EtagsList) + end. + +if_none_match(Req, State, EtagsList) -> + {Etag, Req2, State2} = generate_etag(Req, State), + case Etag of + no_call -> + precondition_failed(Req2, State2); + Etag -> + case lists:member(Etag, EtagsList) of + true -> precondition_is_head_get(Req2, State2); + false -> if_modified_since_exists(Req2, State2) + end + end. + +precondition_is_head_get(Req=#http_req{method=Method}, State) + when Method =:= 'HEAD'; Method =:= 'GET' -> + not_modified(Req, State); +precondition_is_head_get(Req, State) -> + precondition_failed(Req, State). + +if_modified_since_exists(Req, State) -> + case cowboy_http_req:parse_header('If-Modified-Since', Req) of + {undefined, Req2} -> + method(Req2, State); + {{error, badarg}, Req2} -> + method(Req2, State); + {IfModifiedSince, Req2} -> + if_modified_since_now(Req2, State, IfModifiedSince) + end. + +if_modified_since_now(Req, State, IfModifiedSince) -> + case IfModifiedSince > erlang:universaltime() of + true -> method(Req, State); + false -> if_modified_since(Req, State, IfModifiedSince) + end. + +if_modified_since(Req, State, IfModifiedSince) -> + {LastModified, Req2, State2} = last_modified(Req, State), + case LastModified of + no_call -> + method(Req2, State2); + LastModified -> + case LastModified > IfModifiedSince of + true -> method(Req2, State2); + false -> not_modified(Req2, State2) + end + end. + +not_modified(Req=#http_req{resp_headers=RespHeaders}, State) -> + RespHeaders2 = lists:keydelete(<<"Content-Type">>, 1, RespHeaders), + Req2 = Req#http_req{resp_headers=RespHeaders2}, + {Req3, State2} = set_resp_etag(Req2, State), + {Req4, State3} = set_resp_expires(Req3, State2), + respond(Req4, State3, 304). + +precondition_failed(Req, State) -> + respond(Req, State, 412). + +is_put_to_missing_resource(Req=#http_req{method='PUT'}, State) -> + moved_permanently(Req, State, fun is_conflict/2); +is_put_to_missing_resource(Req, State) -> + previously_existed(Req, State). + +%% moved_permanently/2 should return either false or {true, Location} +%% with Location the full new URI of the resource. +moved_permanently(Req, State, OnFalse) -> + case call(Req, State, moved_permanently) of + {{true, Location}, Req2, HandlerState} -> + {ok, Req3} = cowboy_http_req:set_resp_header( + <<"Location">>, Location, Req2), + respond(Req3, State#state{handler_state=HandlerState}, 301); + {false, Req2, HandlerState} -> + OnFalse(Req2, State#state{handler_state=HandlerState}); + {halt, Req2, HandlerState} -> + terminate(Req2, State#state{handler_state=HandlerState}); + no_call -> + OnFalse(Req, State) + end. + +previously_existed(Req, State) -> + expect(Req, State, previously_existed, false, + fun (R, S) -> is_post_to_missing_resource(R, S, 404) end, + fun (R, S) -> moved_permanently(R, S, fun moved_temporarily/2) end). + +%% moved_temporarily/2 should return either false or {true, Location} +%% with Location the full new URI of the resource. +moved_temporarily(Req, State) -> + case call(Req, State, moved_temporarily) of + {{true, Location}, Req2, HandlerState} -> + {ok, Req3} = cowboy_http_req:set_resp_header( + <<"Location">>, Location, Req2), + respond(Req3, State#state{handler_state=HandlerState}, 307); + {false, Req2, HandlerState} -> + is_post_to_missing_resource(Req2, State#state{handler_state=HandlerState}, 410); + {halt, Req2, HandlerState} -> + terminate(Req2, State#state{handler_state=HandlerState}); + no_call -> + is_post_to_missing_resource(Req, State, 410) + end. + +is_post_to_missing_resource(Req=#http_req{method='POST'}, State, OnFalse) -> + allow_missing_post(Req, State, OnFalse); +is_post_to_missing_resource(Req, State, OnFalse) -> + respond(Req, State, OnFalse). + +allow_missing_post(Req, State, OnFalse) -> + expect(Req, State, allow_missing_post, true, fun post_is_create/2, OnFalse). + +method(Req=#http_req{method='DELETE'}, State) -> + delete_resource(Req, State); +method(Req=#http_req{method='POST'}, State) -> + post_is_create(Req, State); +method(Req=#http_req{method='PUT'}, State) -> + is_conflict(Req, State); +method(Req, State) -> + set_resp_body(Req, State). + +%% delete_resource/2 should start deleting the resource and return. +delete_resource(Req, State) -> + expect(Req, State, delete_resource, true, fun delete_completed/2, 500). + +%% delete_completed/2 indicates whether the resource has been deleted yet. +delete_completed(Req, State) -> + expect(Req, State, delete_completed, true, fun has_resp_body/2, 202). + +%% post_is_create/2 indicates whether the POST method can create new resources. +post_is_create(Req, State) -> + expect(Req, State, post_is_create, false, fun process_post/2, fun create_path/2). + +%% When the POST method can create new resources, create_path/2 will be called +%% and is expected to return the full path to the new resource +%% (including the leading /). +create_path(Req=#http_req{meta=Meta}, State) -> + case call(Req, State, create_path) of + {halt, Req2, HandlerState} -> + terminate(Req2, State#state{handler_state=HandlerState}); + {Path, Req2, HandlerState} -> + Location = create_path_location(Req2, Path), + State2 = State#state{handler_state=HandlerState}, + {ok, Req3} = cowboy_http_req:set_resp_header( + <<"Location">>, Location, Req2), + put_resource(Req3#http_req{meta=[{put_path, Path}|Meta]}, + State2, 303) + end. + +create_path_location(#http_req{transport=Transport, raw_host=Host, + port=Port}, Path) -> + TransportName = Transport:name(), + << (create_path_location_protocol(TransportName))/binary, "://", + Host/binary, (create_path_location_port(TransportName, Port))/binary, + Path/binary >>. + +create_path_location_protocol(ssl) -> <<"https">>; +create_path_location_protocol(_) -> <<"http">>. + +create_path_location_port(ssl, 443) -> + <<>>; +create_path_location_port(tcp, 80) -> + <<>>; +create_path_location_port(_, Port) -> + <<":", (list_to_binary(integer_to_list(Port)))/binary>>. + +%% process_post should return true when the POST body could be processed +%% and false when it hasn't, in which case a 500 error is sent. +process_post(Req, State) -> + case call(Req, State, process_post) of + {halt, Req2, HandlerState} -> + terminate(Req2, State#state{handler_state=HandlerState}); + {true, Req2, HandlerState} -> + State2 = State#state{handler_state=HandlerState}, + next(Req2, State2, 201); + {false, Req2, HandlerState} -> + State2 = State#state{handler_state=HandlerState}, + respond(Req2, State2, 500) + end. + +is_conflict(Req, State) -> + expect(Req, State, is_conflict, false, fun put_resource/2, 409). + +put_resource(Req=#http_req{raw_path=RawPath, meta=Meta}, State) -> + Req2 = Req#http_req{meta=[{put_path, RawPath}|Meta]}, + put_resource(Req2, State, fun is_new_resource/2). + +%% content_types_accepted should return a list of media types and their +%% associated callback functions in the same format as content_types_provided. +%% +%% The callback will then be called and is expected to process the content +%% pushed to the resource in the request body. The path to the new resource +%% may be different from the request path, and is stored as request metadata. +%% It is always defined past this point. It can be retrieved as demonstrated: +%% {PutPath, Req2} = cowboy_http_req:meta(put_path, Req) +put_resource(Req, State, OnTrue) -> + case call(Req, State, content_types_accepted) of + no_call -> + respond(Req, State, 415); + {halt, Req2, HandlerState} -> + terminate(Req2, State#state{handler_state=HandlerState}); + {CTA, Req2, HandlerState} -> + State2 = State#state{handler_state=HandlerState}, + {ContentType, Req3} + = cowboy_http_req:parse_header('Content-Type', Req2), + choose_content_type(Req3, State2, OnTrue, ContentType, CTA) + end. + +choose_content_type(Req, State, _OnTrue, _ContentType, []) -> + respond(Req, State, 415); +choose_content_type(Req, State, OnTrue, ContentType, + [{Accepted, Fun}|_Tail]) when ContentType =:= Accepted -> + case call(Req, State, Fun) of + {halt, Req2, HandlerState} -> + terminate(Req2, State#state{handler_state=HandlerState}); + {true, Req2, HandlerState} -> + State2 = State#state{handler_state=HandlerState}, + next(Req2, State2, OnTrue); + {false, Req2, HandlerState} -> + State2 = State#state{handler_state=HandlerState}, + respond(Req2, State2, 500) + end; +choose_content_type(Req, State, OnTrue, ContentType, [_Any|Tail]) -> + choose_content_type(Req, State, OnTrue, ContentType, Tail). + +%% Whether we created a new resource, either through PUT or POST. +%% This is easily testable because we would have set the Location +%% header by this point if we did so. +is_new_resource(Req, State) -> + case cowboy_http_req:has_resp_header(<<"Location">>, Req) of + true -> respond(Req, State, 201); + false -> has_resp_body(Req, State) + end. + +has_resp_body(Req, State) -> + case cowboy_http_req:has_resp_body(Req) of + true -> multiple_choices(Req, State); + false -> respond(Req, State, 204) + end. + +%% Set the response headers and call the callback found using +%% content_types_provided/2 to obtain the request body and add +%% it to the response. +set_resp_body(Req=#http_req{method=Method}, + State=#state{content_type_a={_Type, Fun}}) + when Method =:= 'GET'; Method =:= 'HEAD' -> + {Req2, State2} = set_resp_etag(Req, State), + {LastModified, Req3, State3} = last_modified(Req2, State2), + case LastModified of + LastModified when is_atom(LastModified) -> + Req4 = Req3; + LastModified -> + LastModifiedStr = httpd_util:rfc1123_date(LastModified), + {ok, Req4} = cowboy_http_req:set_resp_header( + <<"Last-Modified">>, LastModifiedStr, Req3) + end, + {Req5, State4} = set_resp_expires(Req4, State3), + case call(Req5, State4, Fun) of + {halt, Req6, HandlerState} -> + terminate(Req6, State4#state{handler_state=HandlerState}); + {Body, Req6, HandlerState} -> + State5 = State4#state{handler_state=HandlerState}, + {ok, Req7} = case Body of + {stream, Len, Fun1} -> + cowboy_http_req:set_resp_body_fun(Len, Fun1, Req6); + _Contents -> + cowboy_http_req:set_resp_body(Body, Req6) + end, + multiple_choices(Req7, State5) + end; +set_resp_body(Req, State) -> + multiple_choices(Req, State). + +multiple_choices(Req, State) -> + expect(Req, State, multiple_choices, false, 200, 300). + +%% Response utility functions. + +set_resp_etag(Req, State) -> + {Etag, Req2, State2} = generate_etag(Req, State), + case Etag of + undefined -> + {Req2, State2}; + Etag -> + {ok, Req3} = cowboy_http_req:set_resp_header( + <<"Etag">>, Etag, Req2), + {Req3, State2} + end. + +set_resp_expires(Req, State) -> + {Expires, Req2, State2} = expires(Req, State), + case Expires of + Expires when is_atom(Expires) -> + {Req2, State2}; + Expires -> + ExpiresStr = httpd_util:rfc1123_date(Expires), + {ok, Req3} = cowboy_http_req:set_resp_header( + <<"Expires">>, ExpiresStr, Req2), + {Req3, State2} + end. + +%% Info retrieval. No logic. + +generate_etag(Req, State=#state{etag=no_call}) -> + {undefined, Req, State}; +generate_etag(Req, State=#state{etag=undefined}) -> + case call(Req, State, generate_etag) of + no_call -> + {undefined, Req, State#state{etag=no_call}}; + {Etag, Req2, HandlerState} -> + {Etag, Req2, State#state{handler_state=HandlerState, etag=Etag}} + end; +generate_etag(Req, State=#state{etag=Etag}) -> + {Etag, Req, State}. + +last_modified(Req, State=#state{last_modified=no_call}) -> + {undefined, Req, State}; +last_modified(Req, State=#state{last_modified=undefined}) -> + case call(Req, State, last_modified) of + no_call -> + {undefined, Req, State#state{last_modified=no_call}}; + {LastModified, Req2, HandlerState} -> + {LastModified, Req2, State#state{handler_state=HandlerState, + last_modified=LastModified}} + end; +last_modified(Req, State=#state{last_modified=LastModified}) -> + {LastModified, Req, State}. + +expires(Req, State=#state{expires=no_call}) -> + {undefined, Req, State}; +expires(Req, State=#state{expires=undefined}) -> + case call(Req, State, expires) of + no_call -> + {undefined, Req, State#state{expires=no_call}}; + {Expires, Req2, HandlerState} -> + {Expires, Req2, State#state{handler_state=HandlerState, + expires=Expires}} + end; +expires(Req, State=#state{expires=Expires}) -> + {Expires, Req, State}. + +%% REST primitives. + +expect(Req, State, Callback, Expected, OnTrue, OnFalse) -> + case call(Req, State, Callback) of + no_call -> + next(Req, State, OnTrue); + {halt, Req2, HandlerState} -> + terminate(Req2, State#state{handler_state=HandlerState}); + {Expected, Req2, HandlerState} -> + next(Req2, State#state{handler_state=HandlerState}, OnTrue); + {_Unexpected, Req2, HandlerState} -> + next(Req2, State#state{handler_state=HandlerState}, OnFalse) + end. + +call(Req, #state{handler=Handler, handler_state=HandlerState}, Fun) -> + case erlang:function_exported(Handler, Fun, 2) of + true -> Handler:Fun(Req, HandlerState); + false -> no_call + end. + +next(Req, State, Next) when is_function(Next) -> + Next(Req, State); +next(Req, State, StatusCode) when is_integer(StatusCode) -> + respond(Req, State, StatusCode). + +%% @todo Allow some sort of callback for custom error pages. +respond(Req, State, StatusCode) -> + {ok, Req2} = cowboy_http_req:reply(StatusCode, Req), + terminate(Req2, State). + +terminate(Req, #state{handler=Handler, handler_state=HandlerState}) -> + case erlang:function_exported(Handler, rest_terminate, 2) of + true -> ok = Handler:rest_terminate( + Req#http_req{resp_state=locked}, HandlerState); + false -> ok + end, + {ok, Req}. diff --git a/rabbitmq-server/plugins-src/cowboy-wrapper/cowboy-git/src/cowboy_http_static.erl b/rabbitmq-server/plugins-src/cowboy-wrapper/cowboy-git/src/cowboy_http_static.erl new file mode 100644 index 0000000..da3bd33 --- /dev/null +++ b/rabbitmq-server/plugins-src/cowboy-wrapper/cowboy-git/src/cowboy_http_static.erl @@ -0,0 +1,456 @@ +%% Copyright (c) 2011, Magnus Klaar +%% +%% Permission to use, copy, modify, and/or distribute this software for any +%% purpose with or without fee is hereby granted, provided that the above +%% copyright notice and this permission notice appear in all copies. +%% +%% THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES +%% WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF +%% MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR +%% ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES +%% WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN +%% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF +%% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + +%% @doc Static resource handler. +%% +%% This built in HTTP handler provides a simple file serving capability for +%% cowboy applications. It should be considered an experimental feature because +%% of it's dependency on the experimental REST handler. It's recommended to be +%% used for small or temporary environments where it is not preferrable to set +%% up a second server just to serve files. +%% +%% If this handler is used the Erlang node running the cowboy application must +%% be configured to use an async thread pool. This is configured by adding the +%% `+A $POOL_SIZE' argument to the `erl' command used to start the node. See +%% +%% this reply from the OTP team to erlang-bugs +%% +%% == Base configuration == +%% +%% The handler must be configured with a request path prefix to serve files +%% under and the path to a directory to read files from. The request path prefix +%% is defined in the path pattern of the cowboy dispatch rule for the handler. +%% The request path pattern must end with a ``'...''' token. +%% The directory path can be set to either an absolute or relative path in the +%% form of a list or binary string representation of a file system path. A list +%% of binary path segments, as is used throughout cowboy, is also a valid +%% directory path. +%% +%% The directory path can also be set to a relative path within the `priv/' +%% directory of an application. This is configured by setting the value of the +%% directory option to a tuple of the form `{priv_dir, Application, Relpath}'. +%% +%% ==== Examples ==== +%% ``` +%% %% Serve files from /var/www/ under http://example.com/static/ +%% {[<<"static">>, '...'], cowboy_http_static, +%% [{directory, "/var/www"}]} +%% +%% %% Serve files from the current working directory under http://example.com/static/ +%% {[<<"static">>, '...'], cowboy_http_static, +%% [{directory, <<"./">>}]} +%% +%% %% Serve files from cowboy/priv/www under http://example.com/ +%% {['...'], cowboy_http_static, +%% [{directory, {priv_dir, cowboy, [<<"www">>]}}]} +%% ''' +%% +%% == Content type configuration == +%% +%% By default the content type of all static resources will be set to +%% `application/octet-stream'. This can be overriden by supplying a list +%% of filename extension to mimetypes pairs in the `mimetypes' option. +%% The filename extension should be a binary string including the leading dot. +%% The mimetypes must be of a type that the `cowboy_http_rest' protocol can +%% handle. +%% +%% The spawngrid/mimetypes +%% application, or an arbitrary function accepting the path to the file being +%% served, can also be used to generate the list of content types for a static +%% file resource. The function used must accept an additional argument after +%% the file path argument. +%% +%% ==== Example ==== +%% ``` +%% %% Use a static list of content types. +%% {[<<"static">>, '...'], cowboy_http_static, +%% [{directory, {priv_dir, cowboy, []}}, +%% {mimetypes, [ +%% {<<".css">>, [<<"text/css">>]}, +%% {<<".js">>, [<<"application/javascript">>]}]}]} +%% +%% %% Use the default database in the mimetypes application. +%% {[<<"static">>, '...', cowboy_http_static, +%% [{directory, {priv_dir, cowboy, []}}, +%% {mimetypes, {fun mimetypes:path_to_mimes/2, default}}]]} +%% ''' +%% +%% == ETag Header Function == +%% +%% The default behaviour of the static file handler is to not generate ETag +%% headers. This is because generating ETag headers based on file metadata +%% causes different servers in a cluster to generate different ETag values for +%% the same file unless the metadata is also synced. Generating strong ETags +%% based on the contents of a file is currently out of scope for this module. +%% +%% The default behaviour can be overridden to generate an ETag header based on +%% a combination of the file path, file size, inode and mtime values. If the +%% option value is a list of attribute names tagged with `attributes' a hex +%% encoded CRC32 checksum of the attribute values are used as the ETag header +%% value. +%% +%% If a strong ETag is required a user defined function for generating the +%% header value can be supplied. The function must accept a proplist of the +%% file attributes as the first argument and a second argument containing any +%% additional data that the function requires. The function must return a +%% `binary()' or `undefined'. +%% +%% ==== Examples ==== +%% ``` +%% %% A value of default is equal to not specifying the option. +%% {[<<"static">>, '...', cowboy_http_static, +%% [{directory, {priv_dir, cowboy, []}}, +%% {etag, default}]]} +%% +%% %% Use all avaliable ETag function arguments to generate a header value. +%% {[<<"static">>, '...', cowboy_http_static, +%% [{directory, {priv_dir, cowboy, []}}, +%% {etag, {attributes, [filepath, filesize, inode, mtime]}}]]} +%% +%% %% Use a user defined function to generate a strong ETag header value. +%% {[<<"static">>, '...', cowboy_http_static, +%% [{directory, {priv_dir, cowboy, []}}, +%% {etag, {fun generate_strong_etag/2, strong_etag_extra}}]]} +%% +%% generate_strong_etag(Arguments, strong_etag_extra) -> +%% {_, Filepath} = lists:keyfind(filepath, 1, Arguments), +%% {_, _Filesize} = lists:keyfind(filesize, 1, Arguments), +%% {_, _INode} = lists:keyfind(inode, 1, Arguments), +%% {_, _Modified} = lists:keyfind(mtime, 1, Arguments), +%% ChecksumCommand = lists:flatten(io_lib:format("sha1sum ~s", [Filepath])), +%% [Checksum|_] = string:tokens(os:cmd(ChecksumCommand), " "), +%% iolist_to_binary(Checksum). +%% ''' +-module(cowboy_http_static). + +%% include files +-include("http.hrl"). +-include_lib("kernel/include/file.hrl"). + +%% cowboy_http_protocol callbacks +-export([init/3]). + +%% cowboy_http_rest callbacks +-export([rest_init/2, allowed_methods/2, malformed_request/2, + resource_exists/2, forbidden/2, last_modified/2, generate_etag/2, + content_types_provided/2, file_contents/2]). + +%% internal +-export([path_to_mimetypes/2]). + +%% types +-type dirpath() :: string() | binary() | [binary()]. +-type dirspec() :: dirpath() | {priv, atom(), dirpath()}. +-type mimedef() :: {binary(), binary(), [{binary(), binary()}]}. +-type etagarg() :: {filepath, binary()} | {mtime, calendar:datetime()} + | {inode, non_neg_integer()} | {filesize, non_neg_integer()}. + +%% handler state +-record(state, { + filepath :: binary() | error, + fileinfo :: {ok, #file_info{}} | {error, _} | error, + mimetypes :: {fun((binary(), T) -> [mimedef()]), T} | undefined, + etag_fun :: {fun(([etagarg()], T) -> undefined | binary()), T}}). + + +%% @private Upgrade from HTTP handler to REST handler. +init({_Transport, http}, _Req, _Opts) -> + {upgrade, protocol, cowboy_http_rest}. + + +%% @private Set up initial state of REST handler. +-spec rest_init(#http_req{}, list()) -> {ok, #http_req{}, #state{}}. +rest_init(Req, Opts) -> + Directory = proplists:get_value(directory, Opts), + Directory1 = directory_path(Directory), + Mimetypes = proplists:get_value(mimetypes, Opts, []), + Mimetypes1 = case Mimetypes of + {_, _} -> Mimetypes; + [] -> {fun path_to_mimetypes/2, []}; + [_|_] -> {fun path_to_mimetypes/2, Mimetypes} + end, + ETagFunction = case proplists:get_value(etag, Opts) of + default -> {fun no_etag_function/2, undefined}; + undefined -> {fun no_etag_function/2, undefined}; + {attributes, Attrs} -> {fun attr_etag_function/2, Attrs}; + {_, _}=EtagFunction1 -> EtagFunction1 + end, + {Filepath, Req1} = cowboy_http_req:path_info(Req), + State = case check_path(Filepath) of + error -> + #state{filepath=error, fileinfo=error, mimetypes=undefined, + etag_fun=ETagFunction}; + ok -> + Filepath1 = join_paths(Directory1, Filepath), + Fileinfo = file:read_file_info(Filepath1), + #state{filepath=Filepath1, fileinfo=Fileinfo, mimetypes=Mimetypes1, + etag_fun=ETagFunction} + end, + {ok, Req1, State}. + + +%% @private Only allow GET and HEAD requests on files. +-spec allowed_methods(#http_req{}, #state{}) -> + {[atom()], #http_req{}, #state{}}. +allowed_methods(Req, State) -> + {['GET', 'HEAD'], Req, State}. + +%% @private +malformed_request(Req, #state{filepath=error}=State) -> + {true, Req, State}; +malformed_request(Req, State) -> + {false, Req, State}. + + +%% @private Check if the resource exists under the document root. +resource_exists(Req, #state{fileinfo={error, _}}=State) -> + {false, Req, State}; +resource_exists(Req, #state{fileinfo={ok, Fileinfo}}=State) -> + {Fileinfo#file_info.type =:= regular, Req, State}. + + +%% @private +%% Access to a file resource is forbidden if it exists and the local node does +%% not have permission to read it. Directory listings are always forbidden. +forbidden(Req, #state{fileinfo={_, #file_info{type=directory}}}=State) -> + {true, Req, State}; +forbidden(Req, #state{fileinfo={error, eacces}}=State) -> + {true, Req, State}; +forbidden(Req, #state{fileinfo={error, _}}=State) -> + {false, Req, State}; +forbidden(Req, #state{fileinfo={ok, #file_info{access=Access}}}=State) -> + {not (Access =:= read orelse Access =:= read_write), Req, State}. + + +%% @private Read the time a file system system object was last modified. +-spec last_modified(#http_req{}, #state{}) -> + {calendar:datetime(), #http_req{}, #state{}}. +last_modified(Req, #state{fileinfo={ok, #file_info{mtime=Modified}}}=State) -> + {Modified, Req, State}. + + +%% @private Generate the ETag header value for this file. +%% The ETag header value is only generated if the resource is a file that +%% exists in document root. +-spec generate_etag(#http_req{}, #state{}) -> + {undefined | binary(), #http_req{}, #state{}}. +generate_etag(Req, #state{fileinfo={_, #file_info{type=regular, inode=INode, + mtime=Modified, size=Filesize}}, filepath=Filepath, + etag_fun={ETagFun, ETagData}}=State) -> + ETagArgs = [ + {filepath, Filepath}, {filesize, Filesize}, + {inode, INode}, {mtime, Modified}], + {ETagFun(ETagArgs, ETagData), Req, State}; +generate_etag(Req, State) -> + {undefined, Req, State}. + + +%% @private Return the content type of a file. +-spec content_types_provided(#http_req{}, #state{}) -> tuple(). +content_types_provided(Req, #state{filepath=Filepath, + mimetypes={MimetypesFun, MimetypesData}}=State) -> + Mimetypes = [{T, file_contents} + || T <- MimetypesFun(Filepath, MimetypesData)], + {Mimetypes, Req, State}. + + +%% @private Return a function that writes a file directly to the socket. +-spec file_contents(#http_req{}, #state{}) -> tuple(). +file_contents(Req, #state{filepath=Filepath, + fileinfo={ok, #file_info{size=Filesize}}}=State) -> + {ok, Transport, Socket} = cowboy_http_req:transport(Req), + Writefile = content_function(Transport, Socket, Filepath), + {{stream, Filesize, Writefile}, Req, State}. + + +%% @private Return a function writing the contents of a file to a socket. +%% The function returns the number of bytes written to the socket to enable +%% the calling function to determine if the expected number of bytes were +%% written to the socket. +-spec content_function(module(), inet:socket(), binary()) -> + fun(() -> {sent, non_neg_integer()}). +content_function(Transport, Socket, Filepath) -> + %% `file:sendfile/2' will only work with the `cowboy_tcp_transport' + %% transport module. SSL or future SPDY transports that require the + %% content to be encrypted or framed as the content is sent. + case erlang:function_exported(file, sendfile, 2) of + false -> + fun() -> sfallback(Transport, Socket, Filepath) end; + _ when Transport =/= cowboy_tcp_transport -> + fun() -> sfallback(Transport, Socket, Filepath) end; + true -> + fun() -> sendfile(Socket, Filepath) end + end. + + +%% @private Sendfile fallback function. +-spec sfallback(module(), inet:socket(), binary()) -> {sent, non_neg_integer()}. +sfallback(Transport, Socket, Filepath) -> + {ok, File} = file:open(Filepath, [read,binary,raw]), + sfallback(Transport, Socket, File, 0). + +-spec sfallback(module(), inet:socket(), file:io_device(), + non_neg_integer()) -> {sent, non_neg_integer()}. +sfallback(Transport, Socket, File, Sent) -> + case file:read(File, 16#1FFF) of + eof -> + ok = file:close(File), + {sent, Sent}; + {ok, Bin} -> + ok = Transport:send(Socket, Bin), + sfallback(Transport, Socket, File, Sent + byte_size(Bin)) + end. + + +%% @private Wrapper for sendfile function. +-spec sendfile(inet:socket(), binary()) -> {sent, non_neg_integer()}. +sendfile(Socket, Filepath) -> + {ok, Sent} = file:sendfile(Filepath, Socket), + {sent, Sent}. + +-spec directory_path(dirspec()) -> dirpath(). +directory_path({priv_dir, App, []}) -> + priv_dir_path(App); +directory_path({priv_dir, App, [H|_]=Path}) when is_integer(H) -> + filename:join(priv_dir_path(App), Path); +directory_path({priv_dir, App, [H|_]=Path}) when is_binary(H) -> + filename:join(filename:split(priv_dir_path(App)) ++ Path); +directory_path({priv_dir, App, Path}) when is_binary(Path) -> + filename:join(priv_dir_path(App), Path); +directory_path(Path) -> + Path. + + +%% @private Validate a request path for unsafe characters. +%% There is no way to escape special characters in a filesystem path. +-spec check_path(Path::[binary()]) -> ok | error. +check_path([]) -> ok; +check_path([<<"">>|_T]) -> error; +check_path([<<".">>|_T]) -> error; +check_path([<<"..">>|_T]) -> error; +check_path([H|T]) -> + case binary:match(H, <<"/">>) of + {_, _} -> error; + nomatch -> check_path(T) + end. + + +%% @private Join the the directory and request paths. +-spec join_paths(dirpath(), [binary()]) -> binary(). +join_paths([H|_]=Dirpath, Filepath) when is_integer(H) -> + filename:join(filename:split(Dirpath) ++ Filepath); +join_paths([H|_]=Dirpath, Filepath) when is_binary(H) -> + filename:join(Dirpath ++ Filepath); +join_paths(Dirpath, Filepath) when is_binary(Dirpath) -> + filename:join([Dirpath] ++ Filepath); +join_paths([], Filepath) -> + filename:join(Filepath). + + +%% @private Return the path to the priv/ directory of an application. +-spec priv_dir_path(atom()) -> string(). +priv_dir_path(App) -> + case code:priv_dir(App) of + {error, bad_name} -> priv_dir_mod(App); + Dir -> Dir + end. + +-spec priv_dir_mod(atom()) -> string(). +priv_dir_mod(Mod) -> + case code:which(Mod) of + File when not is_list(File) -> "../priv"; + File -> filename:join([filename:dirname(File),"../priv"]) + end. + + +%% @private Use application/octet-stream as the default mimetype. +%% If a list of extension - mimetype pairs are provided as the mimetypes +%% an attempt to find the mimetype using the file extension. If no match +%% is found the default mimetype is returned. +-spec path_to_mimetypes(binary(), [{binary(), [mimedef()]}]) -> + [mimedef()]. +path_to_mimetypes(Filepath, Extensions) when is_binary(Filepath) -> + Ext = filename:extension(Filepath), + case Ext of + <<>> -> default_mimetype(); + _Ext -> path_to_mimetypes_(Ext, Extensions) + end. + +-spec path_to_mimetypes_(binary(), [{binary(), [mimedef()]}]) -> [mimedef()]. +path_to_mimetypes_(Ext, Extensions) -> + case lists:keyfind(Ext, 1, Extensions) of + {_, MTs} -> MTs; + _Unknown -> default_mimetype() + end. + +-spec default_mimetype() -> [mimedef()]. +default_mimetype() -> + [{<<"application">>, <<"octet-stream">>, []}]. + + +%% @private Do not send ETag headers in the default configuration. +-spec no_etag_function([etagarg()], undefined) -> undefined. +no_etag_function(_Args, undefined) -> + undefined. + +%% @private A simple alternative is to send an ETag based on file attributes. +-type fileattr() :: filepath | filesize | mtime | inode. +-spec attr_etag_function([etagarg()], [fileattr()]) -> binary(). +attr_etag_function(Args, Attrs) -> + attr_etag_function(Args, Attrs, []). + +-spec attr_etag_function([etagarg()], [fileattr()], [binary()]) -> binary(). +attr_etag_function(_Args, [], Acc) -> + list_to_binary(erlang:integer_to_list(erlang:crc32(Acc), 16)); +attr_etag_function(Args, [H|T], Acc) -> + {_, Value} = lists:keyfind(H, 1, Args), + attr_etag_function(Args, T, [term_to_binary(Value)|Acc]). + + +-ifdef(TEST). +-include_lib("eunit/include/eunit.hrl"). +-define(_eq(E, I), ?_assertEqual(E, I)). + +check_path_test_() -> + C = fun check_path/1, + [?_eq(error, C([<<>>])), + ?_eq(ok, C([<<"abc">>])), + ?_eq(error, C([<<".">>])), + ?_eq(error, C([<<"..">>])), + ?_eq(error, C([<<"/">>])) + ]. + +join_paths_test_() -> + P = fun join_paths/2, + [?_eq(<<"a">>, P([], [<<"a">>])), + ?_eq(<<"a/b/c">>, P(<<"a/b">>, [<<"c">>])), + ?_eq(<<"a/b/c">>, P("a/b", [<<"c">>])), + ?_eq(<<"a/b/c">>, P([<<"a">>, <<"b">>], [<<"c">>])) + ]. + +directory_path_test_() -> + P = fun directory_path/1, + PL = fun(I) -> length(filename:split(P(I))) end, + Base = PL({priv_dir, cowboy, []}), + [?_eq(Base + 1, PL({priv_dir, cowboy, "a"})), + ?_eq(Base + 1, PL({priv_dir, cowboy, <<"a">>})), + ?_eq(Base + 1, PL({priv_dir, cowboy, [<<"a">>]})), + ?_eq(Base + 2, PL({priv_dir, cowboy, "a/b"})), + ?_eq(Base + 2, PL({priv_dir, cowboy, <<"a/b">>})), + ?_eq(Base + 2, PL({priv_dir, cowboy, [<<"a">>, <<"b">>]})), + ?_eq("a/b", P("a/b")) + ]. + + +-endif. diff --git a/rabbitmq-server/plugins-src/cowboy-wrapper/cowboy-git/src/cowboy_http_websocket.erl b/rabbitmq-server/plugins-src/cowboy-wrapper/cowboy-git/src/cowboy_http_websocket.erl new file mode 100644 index 0000000..5100213 --- /dev/null +++ b/rabbitmq-server/plugins-src/cowboy-wrapper/cowboy-git/src/cowboy_http_websocket.erl @@ -0,0 +1,530 @@ +%% Copyright (c) 2011, Loïc Hoguin +%% +%% Permission to use, copy, modify, and/or distribute this software for any +%% purpose with or without fee is hereby granted, provided that the above +%% copyright notice and this permission notice appear in all copies. +%% +%% THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES +%% WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF +%% MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR +%% ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES +%% WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN +%% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF +%% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + +%% @doc WebSocket protocol implementation. +%% +%% Supports the protocol version 0 (hixie-76), version 7 (hybi-7) +%% and version 8 (hybi-8, hybi-9 and hybi-10). +%% +%% Version 0 is supported by the following browsers: +%%
    +%%
  • Firefox 4-5 (disabled by default)
  • +%%
  • Chrome 6-13
  • +%%
  • Safari 5.0.1+
  • +%%
  • Opera 11.00+ (disabled by default)
  • +%%
+%% +%% Version 7 is supported by the following browser: +%%
    +%%
  • Firefox 6
  • +%%
+%% +%% Version 8+ is supported by the following browsers: +%%
    +%%
  • Firefox 7+
  • +%%
  • Chrome 14+
  • +%%
+-module(cowboy_http_websocket). + +-export([upgrade/4]). %% API. +-export([handler_loop/4]). %% Internal. + +-include("include/http.hrl"). +-include_lib("eunit/include/eunit.hrl"). + +-type opcode() :: 0 | 1 | 2 | 8 | 9 | 10. +-type mask_key() :: 0..16#ffffffff. + +-record(state, { + version :: 0 | 7 | 8 | 13, + handler :: module(), + opts :: any(), + challenge = undefined :: undefined | binary() | {binary(), binary()}, + timeout = infinity :: timeout(), + timeout_ref = undefined, + messages = undefined :: undefined | {atom(), atom(), atom()}, + hibernate = false, + eop :: undefined | tuple(), %% hixie-76 specific. + origin = undefined :: undefined | binary() %% hixie-76 specific. +}). + +%% @doc Upgrade a HTTP request to the WebSocket protocol. +%% +%% You do not need to call this function manually. To upgrade to the WebSocket +%% protocol, you simply need to return {upgrade, protocol, {@module}} +%% in your cowboy_http_handler:init/3 handler function. +-spec upgrade(pid(), module(), any(), #http_req{}) -> closed | none(). +upgrade(ListenerPid, Handler, Opts, Req) -> + cowboy_listener:move_connection(ListenerPid, websocket, self()), + case catch websocket_upgrade(#state{handler=Handler, opts=Opts}, Req) of + {ok, State, Req2} -> handler_init(State, Req2); + {'EXIT', _Reason} -> upgrade_error(Req) + end. + +-spec websocket_upgrade(#state{}, #http_req{}) -> {ok, #state{}, #http_req{}}. +websocket_upgrade(State, Req) -> + {ConnTokens, Req2} + = cowboy_http_req:parse_header('Connection', Req), + true = lists:member(<<"upgrade">>, ConnTokens), + %% @todo Should probably send a 426 if the Upgrade header is missing. + {[<<"websocket">>], Req3} = cowboy_http_req:parse_header('Upgrade', Req2), + {Version, Req4} = cowboy_http_req:header(<<"Sec-Websocket-Version">>, Req3), + websocket_upgrade(Version, State, Req4). + +%% @todo Handle the Sec-Websocket-Protocol header. +%% @todo Reply a proper error, don't die, if a required header is undefined. +-spec websocket_upgrade(undefined | <<_:8>>, #state{}, #http_req{}) + -> {ok, #state{}, #http_req{}}. +%% No version given. Assuming hixie-76 draft. +%% +%% We need to wait to send a reply back before trying to read the +%% third part of the challenge key, because proxies will wait for +%% a reply before sending it. Therefore we calculate the challenge +%% key only in websocket_handshake/3. +websocket_upgrade(undefined, State, Req=#http_req{meta=Meta}) -> + {Origin, Req2} = cowboy_http_req:header(<<"Origin">>, Req), + {Key1, Req3} = cowboy_http_req:header(<<"Sec-Websocket-Key1">>, Req2), + {Key2, Req4} = cowboy_http_req:header(<<"Sec-Websocket-Key2">>, Req3), + false = lists:member(undefined, [Origin, Key1, Key2]), + EOP = binary:compile_pattern(<< 255 >>), + {ok, State#state{version=0, origin=Origin, challenge={Key1, Key2}, + eop=EOP}, Req4#http_req{meta=[{websocket_version, 0}|Meta]}}; +%% Versions 7 and 8. Implementation follows the hybi 7 through 17 drafts. +websocket_upgrade(Version, State, Req=#http_req{meta=Meta}) + when Version =:= <<"7">>; Version =:= <<"8">>; + Version =:= <<"13">> -> + {Key, Req2} = cowboy_http_req:header(<<"Sec-Websocket-Key">>, Req), + false = Key =:= undefined, + Challenge = hybi_challenge(Key), + IntVersion = list_to_integer(binary_to_list(Version)), + {ok, State#state{version=IntVersion, challenge=Challenge}, + Req2#http_req{meta=[{websocket_version, IntVersion}|Meta]}}. + +-spec handler_init(#state{}, #http_req{}) -> closed | none(). +handler_init(State=#state{handler=Handler, opts=Opts}, + Req=#http_req{transport=Transport}) -> + try Handler:websocket_init(Transport:name(), Req, Opts) of + {ok, Req2, HandlerState} -> + websocket_handshake(State, Req2, HandlerState); + {ok, Req2, HandlerState, hibernate} -> + websocket_handshake(State#state{hibernate=true}, + Req2, HandlerState); + {ok, Req2, HandlerState, Timeout} -> + websocket_handshake(State#state{timeout=Timeout}, + Req2, HandlerState); + {ok, Req2, HandlerState, Timeout, hibernate} -> + websocket_handshake(State#state{timeout=Timeout, + hibernate=true}, Req2, HandlerState); + {shutdown, Req2} -> + upgrade_denied(Req2) + catch Class:Reason -> + upgrade_error(Req), + error_logger:error_msg( + "** Handler ~p terminating in websocket_init/3~n" + " for the reason ~p:~p~n** Options were ~p~n" + "** Request was ~p~n** Stacktrace: ~p~n~n", + [Handler, Class, Reason, Opts, Req, erlang:get_stacktrace()]) + end. + +-spec upgrade_error(#http_req{}) -> closed. +upgrade_error(Req) -> + {ok, _Req2} = cowboy_http_req:reply(400, [], [], + Req#http_req{resp_state=waiting}), + closed. + +%% @see cowboy_http_protocol:ensure_response/1 +-spec upgrade_denied(#http_req{}) -> closed. +upgrade_denied(#http_req{resp_state=done}) -> + closed; +upgrade_denied(Req=#http_req{resp_state=waiting}) -> + {ok, _Req2} = cowboy_http_req:reply(400, [], [], Req), + closed; +upgrade_denied(#http_req{method='HEAD', resp_state=chunks}) -> + closed; +upgrade_denied(#http_req{socket=Socket, transport=Transport, + resp_state=chunks}) -> + Transport:send(Socket, <<"0\r\n\r\n">>), + closed. + +-spec websocket_handshake(#state{}, #http_req{}, any()) -> closed | none(). +websocket_handshake(State=#state{version=0, origin=Origin, + challenge={Key1, Key2}}, Req=#http_req{socket=Socket, + transport=Transport, raw_host=Host, port=Port, + raw_path=Path, raw_qs=QS}, HandlerState) -> + Location = hixie76_location(Transport:name(), Host, Port, Path, QS), + {ok, Req2} = cowboy_http_req:upgrade_reply( + <<"101 WebSocket Protocol Handshake">>, + [{<<"Upgrade">>, <<"WebSocket">>}, + {<<"Sec-Websocket-Location">>, Location}, + {<<"Sec-Websocket-Origin">>, Origin}], + Req#http_req{resp_state=waiting}), + %% Flush the resp_sent message before moving on. + receive {cowboy_http_req, resp_sent} -> ok after 0 -> ok end, + %% We replied with a proper response. Proxies should be happy enough, + %% we can now read the 8 last bytes of the challenge keys and send + %% the challenge response directly to the socket. + case cowboy_http_req:body(8, Req2) of + {ok, Key3, Req3} -> + Challenge = hixie76_challenge(Key1, Key2, Key3), + Transport:send(Socket, Challenge), + handler_before_loop(State#state{messages=Transport:messages()}, + Req3, HandlerState, <<>>); + _Any -> + closed %% If an error happened reading the body, stop there. + end; +websocket_handshake(State=#state{challenge=Challenge}, + Req=#http_req{transport=Transport}, HandlerState) -> + {ok, Req2} = cowboy_http_req:upgrade_reply( + 101, + [{<<"Upgrade">>, <<"websocket">>}, + {<<"Sec-Websocket-Accept">>, Challenge}], + Req#http_req{resp_state=waiting}), + %% Flush the resp_sent message before moving on. + receive {cowboy_http_req, resp_sent} -> ok after 0 -> ok end, + handler_before_loop(State#state{messages=Transport:messages()}, + Req2, HandlerState, <<>>). + +-spec handler_before_loop(#state{}, #http_req{}, any(), binary()) -> closed | none(). +handler_before_loop(State=#state{hibernate=true}, + Req=#http_req{socket=Socket, transport=Transport}, + HandlerState, SoFar) -> + Transport:setopts(Socket, [{active, once}]), + State2 = handler_loop_timeout(State), + erlang:hibernate(?MODULE, handler_loop, [State2#state{hibernate=false}, + Req, HandlerState, SoFar]); +handler_before_loop(State, Req=#http_req{socket=Socket, transport=Transport}, + HandlerState, SoFar) -> + Transport:setopts(Socket, [{active, once}]), + State2 = handler_loop_timeout(State), + handler_loop(State2, Req, HandlerState, SoFar). + +-spec handler_loop_timeout(#state{}) -> #state{}. +handler_loop_timeout(State=#state{timeout=infinity}) -> + State#state{timeout_ref=undefined}; +handler_loop_timeout(State=#state{timeout=Timeout, timeout_ref=PrevRef}) -> + _ = case PrevRef of undefined -> ignore; PrevRef -> + erlang:cancel_timer(PrevRef) end, + TRef = make_ref(), + erlang:send_after(Timeout, self(), {?MODULE, timeout, TRef}), + State#state{timeout_ref=TRef}. + +%% @private +-spec handler_loop(#state{}, #http_req{}, any(), binary()) -> closed | none(). +handler_loop(State=#state{messages={OK, Closed, Error}, timeout_ref=TRef}, + Req=#http_req{socket=Socket}, HandlerState, SoFar) -> + receive + {OK, Socket, Data} -> + websocket_data(State, Req, HandlerState, + << SoFar/binary, Data/binary >>); + {Closed, Socket} -> + handler_terminate(State, Req, HandlerState, {error, closed}); + {Error, Socket, Reason} -> + handler_terminate(State, Req, HandlerState, {error, Reason}); + {?MODULE, timeout, TRef} -> + websocket_close(State, Req, HandlerState, {normal, timeout}); + {?MODULE, timeout, OlderTRef} when is_reference(OlderTRef) -> + handler_loop(State, Req, HandlerState, SoFar); + Message -> + handler_call(State, Req, HandlerState, + SoFar, websocket_info, Message, fun handler_before_loop/4) + end. + +-spec websocket_data(#state{}, #http_req{}, any(), binary()) -> closed | none(). +%% No more data. +websocket_data(State, Req, HandlerState, <<>>) -> + handler_before_loop(State, Req, HandlerState, <<>>); +%% hixie-76 close frame. +websocket_data(State=#state{version=0}, Req, HandlerState, + << 255, 0, _Rest/binary >>) -> + websocket_close(State, Req, HandlerState, {normal, closed}); +%% hixie-76 data frame. We only support the frame type 0, same as the specs. +websocket_data(State=#state{version=0, eop=EOP}, Req, HandlerState, + Data = << 0, _/binary >>) -> + case binary:match(Data, EOP) of + {Pos, 1} -> + Pos2 = Pos - 1, + << 0, Payload:Pos2/binary, 255, Rest/bits >> = Data, + handler_call(State, Req, HandlerState, + Rest, websocket_handle, {text, Payload}, fun websocket_data/4); + nomatch -> + %% @todo We probably should allow limiting frame length. + handler_before_loop(State, Req, HandlerState, Data) + end; +%% incomplete hybi data frame. +websocket_data(State=#state{version=Version}, Req, HandlerState, Data) + when Version =/= 0, byte_size(Data) =:= 1 -> + handler_before_loop(State, Req, HandlerState, Data); +%% hybi data frame. +%% @todo Handle Fin. +websocket_data(State=#state{version=Version}, Req, HandlerState, Data) + when Version =/= 0 -> + << 1:1, 0:3, Opcode:4, Mask:1, PayloadLen:7, Rest/bits >> = Data, + case {PayloadLen, Rest} of + {126, _} when Opcode >= 8 -> websocket_close( + State, Req, HandlerState, {error, protocol}); + {127, _} when Opcode >= 8 -> websocket_close( + State, Req, HandlerState, {error, protocol}); + {126, << L:16, R/bits >>} -> websocket_before_unmask( + State, Req, HandlerState, Data, R, Opcode, Mask, L); + {126, Rest} -> websocket_before_unmask( + State, Req, HandlerState, Data, Rest, Opcode, Mask, undefined); + {127, << 0:1, L:63, R/bits >>} -> websocket_before_unmask( + State, Req, HandlerState, Data, R, Opcode, Mask, L); + {127, Rest} -> websocket_before_unmask( + State, Req, HandlerState, Data, Rest, Opcode, Mask, undefined); + {PayloadLen, Rest} -> websocket_before_unmask( + State, Req, HandlerState, Data, Rest, Opcode, Mask, PayloadLen) + end; +%% Something was wrong with the frame. Close the connection. +websocket_data(State, Req, HandlerState, _Bad) -> + websocket_close(State, Req, HandlerState, {error, badframe}). + +%% hybi routing depending on whether unmasking is needed. +-spec websocket_before_unmask(#state{}, #http_req{}, any(), binary(), + binary(), opcode(), 0 | 1, non_neg_integer() | undefined) + -> closed | none(). +websocket_before_unmask(State, Req, HandlerState, Data, + Rest, Opcode, Mask, PayloadLen) -> + case {Mask, PayloadLen} of + {0, 0} -> + websocket_dispatch(State, Req, HandlerState, Rest, Opcode, <<>>); + {1, N} when N + 4 > byte_size(Rest); N =:= undefined -> + %% @todo We probably should allow limiting frame length. + handler_before_loop(State, Req, HandlerState, Data); + {1, _N} -> + << MaskKey:32, Payload:PayloadLen/binary, Rest2/bits >> = Rest, + websocket_unmask(State, Req, HandlerState, Rest2, + Opcode, Payload, MaskKey) + end. + +%% hybi unmasking. +-spec websocket_unmask(#state{}, #http_req{}, any(), binary(), + opcode(), binary(), mask_key()) -> closed | none(). +websocket_unmask(State, Req, HandlerState, RemainingData, + Opcode, Payload, MaskKey) -> + websocket_unmask(State, Req, HandlerState, RemainingData, + Opcode, Payload, MaskKey, <<>>). + +-spec websocket_unmask(#state{}, #http_req{}, any(), binary(), + opcode(), binary(), mask_key(), binary()) -> closed | none(). +websocket_unmask(State, Req, HandlerState, RemainingData, + Opcode, << O:32, Rest/bits >>, MaskKey, Acc) -> + T = O bxor MaskKey, + websocket_unmask(State, Req, HandlerState, RemainingData, + Opcode, Rest, MaskKey, << Acc/binary, T:32 >>); +websocket_unmask(State, Req, HandlerState, RemainingData, + Opcode, << O:24 >>, MaskKey, Acc) -> + << MaskKey2:24, _:8 >> = << MaskKey:32 >>, + T = O bxor MaskKey2, + websocket_dispatch(State, Req, HandlerState, RemainingData, + Opcode, << Acc/binary, T:24 >>); +websocket_unmask(State, Req, HandlerState, RemainingData, + Opcode, << O:16 >>, MaskKey, Acc) -> + << MaskKey2:16, _:16 >> = << MaskKey:32 >>, + T = O bxor MaskKey2, + websocket_dispatch(State, Req, HandlerState, RemainingData, + Opcode, << Acc/binary, T:16 >>); +websocket_unmask(State, Req, HandlerState, RemainingData, + Opcode, << O:8 >>, MaskKey, Acc) -> + << MaskKey2:8, _:24 >> = << MaskKey:32 >>, + T = O bxor MaskKey2, + websocket_dispatch(State, Req, HandlerState, RemainingData, + Opcode, << Acc/binary, T:8 >>); +websocket_unmask(State, Req, HandlerState, RemainingData, + Opcode, <<>>, _MaskKey, Acc) -> + websocket_dispatch(State, Req, HandlerState, RemainingData, + Opcode, Acc). + +%% hybi dispatching. +-spec websocket_dispatch(#state{}, #http_req{}, any(), binary(), + opcode(), binary()) -> closed | none(). +%% @todo Fragmentation. +%~ websocket_dispatch(State, Req, HandlerState, RemainingData, 0, Payload) -> +%% Text frame. +websocket_dispatch(State, Req, HandlerState, RemainingData, 1, Payload) -> + handler_call(State, Req, HandlerState, RemainingData, + websocket_handle, {text, Payload}, fun websocket_data/4); +%% Binary frame. +websocket_dispatch(State, Req, HandlerState, RemainingData, 2, Payload) -> + handler_call(State, Req, HandlerState, RemainingData, + websocket_handle, {binary, Payload}, fun websocket_data/4); +%% Close control frame. +%% @todo Handle the optional Payload. +websocket_dispatch(State, Req, HandlerState, _RemainingData, 8, _Payload) -> + websocket_close(State, Req, HandlerState, {normal, closed}); +%% Ping control frame. Send a pong back and forward the ping to the handler. +websocket_dispatch(State, Req=#http_req{socket=Socket, transport=Transport}, + HandlerState, RemainingData, 9, Payload) -> + Len = hybi_payload_length(byte_size(Payload)), + Transport:send(Socket, << 1:1, 0:3, 10:4, 0:1, Len/bits, Payload/binary >>), + handler_call(State, Req, HandlerState, RemainingData, + websocket_handle, {ping, Payload}, fun websocket_data/4); +%% Pong control frame. +websocket_dispatch(State, Req, HandlerState, RemainingData, 10, Payload) -> + handler_call(State, Req, HandlerState, RemainingData, + websocket_handle, {pong, Payload}, fun websocket_data/4). + +-spec handler_call(#state{}, #http_req{}, any(), binary(), + atom(), any(), fun()) -> closed | none(). +handler_call(State=#state{handler=Handler, opts=Opts}, Req, HandlerState, + RemainingData, Callback, Message, NextState) -> + try Handler:Callback(Message, Req, HandlerState) of + {ok, Req2, HandlerState2} -> + NextState(State, Req2, HandlerState2, RemainingData); + {ok, Req2, HandlerState2, hibernate} -> + NextState(State#state{hibernate=true}, + Req2, HandlerState2, RemainingData); + {reply, Payload, Req2, HandlerState2} -> + websocket_send(Payload, State, Req2), + NextState(State, Req2, HandlerState2, RemainingData); + {reply, Payload, Req2, HandlerState2, hibernate} -> + websocket_send(Payload, State, Req2), + NextState(State#state{hibernate=true}, + Req2, HandlerState2, RemainingData); + {shutdown, Req2, HandlerState2} -> + websocket_close(State, Req2, HandlerState2, {normal, shutdown}) + catch Class:Reason -> + error_logger:error_msg( + "** Handler ~p terminating in ~p/3~n" + " for the reason ~p:~p~n** Message was ~p~n" + "** Options were ~p~n** Handler state was ~p~n" + "** Request was ~p~n** Stacktrace: ~p~n~n", + [Handler, Callback, Class, Reason, Message, Opts, + HandlerState, Req, erlang:get_stacktrace()]), + websocket_close(State, Req, HandlerState, {error, handler}) + end. + +-spec websocket_send(binary(), #state{}, #http_req{}) -> closed | ignore. +%% hixie-76 text frame. +websocket_send({text, Payload}, #state{version=0}, + #http_req{socket=Socket, transport=Transport}) -> + Transport:send(Socket, [0, Payload, 255]); +%% Ignore all unknown frame types for compatibility with hixie 76. +websocket_send(_Any, #state{version=0}, _Req) -> + ignore; +websocket_send({Type, Payload}, _State, + #http_req{socket=Socket, transport=Transport}) -> + Opcode = case Type of + text -> 1; + binary -> 2; + ping -> 9; + pong -> 10 + end, + Len = hybi_payload_length(iolist_size(Payload)), + Transport:send(Socket, [<< 1:1, 0:3, Opcode:4, 0:1, Len/bits >>, + Payload]). + +-spec websocket_close(#state{}, #http_req{}, any(), {atom(), atom()}) -> closed. +websocket_close(State=#state{version=0}, Req=#http_req{socket=Socket, + transport=Transport}, HandlerState, Reason) -> + Transport:send(Socket, << 255, 0 >>), + handler_terminate(State, Req, HandlerState, Reason); +%% @todo Send a Payload? Using Reason is usually good but we're quite careless. +websocket_close(State, Req=#http_req{socket=Socket, + transport=Transport}, HandlerState, Reason) -> + Transport:send(Socket, << 1:1, 0:3, 8:4, 0:8 >>), + handler_terminate(State, Req, HandlerState, Reason). + +-spec handler_terminate(#state{}, #http_req{}, + any(), atom() | {atom(), atom()}) -> closed. +handler_terminate(#state{handler=Handler, opts=Opts}, + Req, HandlerState, TerminateReason) -> + try + Handler:websocket_terminate(TerminateReason, Req, HandlerState) + catch Class:Reason -> + error_logger:error_msg( + "** Handler ~p terminating in websocket_terminate/3~n" + " for the reason ~p:~p~n** Initial reason was ~p~n" + "** Options were ~p~n** Handler state was ~p~n" + "** Request was ~p~n** Stacktrace: ~p~n~n", + [Handler, Class, Reason, TerminateReason, Opts, + HandlerState, Req, erlang:get_stacktrace()]) + end, + closed. + +%% hixie-76 specific. + +-spec hixie76_challenge(binary(), binary(), binary()) -> binary(). +hixie76_challenge(Key1, Key2, Key3) -> + IntKey1 = hixie76_key_to_integer(Key1), + IntKey2 = hixie76_key_to_integer(Key2), + erlang:md5(<< IntKey1:32, IntKey2:32, Key3/binary >>). + +-spec hixie76_key_to_integer(binary()) -> integer(). +hixie76_key_to_integer(Key) -> + Number = list_to_integer([C || << C >> <= Key, C >= $0, C =< $9]), + Spaces = length([C || << C >> <= Key, C =:= 32]), + Number div Spaces. + +-spec hixie76_location(atom(), binary(), inet:ip_port(), binary(), binary()) + -> binary(). +hixie76_location(Protocol, Host, Port, Path, <<>>) -> + << (hixie76_location_protocol(Protocol))/binary, "://", Host/binary, + (hixie76_location_port(Protocol, Port))/binary, Path/binary>>; +hixie76_location(Protocol, Host, Port, Path, QS) -> + << (hixie76_location_protocol(Protocol))/binary, "://", Host/binary, + (hixie76_location_port(Protocol, Port))/binary, Path/binary, "?", QS/binary >>. + +-spec hixie76_location_protocol(atom()) -> binary(). +hixie76_location_protocol(ssl) -> <<"wss">>; +hixie76_location_protocol(_) -> <<"ws">>. + +%% @todo We should add a secure/0 function to transports +%% instead of relying on their name. +-spec hixie76_location_port(atom(), inet:ip_port()) -> binary(). +hixie76_location_port(ssl, 443) -> + <<>>; +hixie76_location_port(tcp, 80) -> + <<>>; +hixie76_location_port(_, Port) -> + <<":", (list_to_binary(integer_to_list(Port)))/binary>>. + +%% hybi specific. + +-spec hybi_challenge(binary()) -> binary(). +hybi_challenge(Key) -> + Bin = << Key/binary, "258EAFA5-E914-47DA-95CA-C5AB0DC85B11" >>, + base64:encode(crypto:sha(Bin)). + +-spec hybi_payload_length(0..16#7fffffffffffffff) + -> << _:7 >> | << _:23 >> | << _:71 >>. +hybi_payload_length(N) -> + case N of + N when N =< 125 -> << N:7 >>; + N when N =< 16#ffff -> << 126:7, N:16 >>; + N when N =< 16#7fffffffffffffff -> << 127:7, N:64 >> + end. + +%% Tests. + +-ifdef(TEST). + +hixie76_location_test() -> + ?assertEqual(<<"ws://localhost/path">>, + hixie76_location(tcp, <<"localhost">>, 80, <<"/path">>, <<>>)), + ?assertEqual(<<"ws://localhost:443/path">>, + hixie76_location(tcp, <<"localhost">>, 443, <<"/path">>, <<>>)), + ?assertEqual(<<"ws://localhost:8080/path">>, + hixie76_location(tcp, <<"localhost">>, 8080, <<"/path">>, <<>>)), + ?assertEqual(<<"ws://localhost:8080/path?dummy=2785">>, + hixie76_location(tcp, <<"localhost">>, 8080, <<"/path">>, <<"dummy=2785">>)), + ?assertEqual(<<"wss://localhost/path">>, + hixie76_location(ssl, <<"localhost">>, 443, <<"/path">>, <<>>)), + ?assertEqual(<<"wss://localhost:8443/path">>, + hixie76_location(ssl, <<"localhost">>, 8443, <<"/path">>, <<>>)), + ?assertEqual(<<"wss://localhost:8443/path?dummy=2785">>, + hixie76_location(ssl, <<"localhost">>, 8443, <<"/path">>, <<"dummy=2785">>)), + ok. + +-endif. diff --git a/rabbitmq-server/plugins-src/cowboy-wrapper/cowboy-git/src/cowboy_http_websocket_handler.erl b/rabbitmq-server/plugins-src/cowboy-wrapper/cowboy-git/src/cowboy_http_websocket_handler.erl new file mode 100644 index 0000000..2ea0a46 --- /dev/null +++ b/rabbitmq-server/plugins-src/cowboy-wrapper/cowboy-git/src/cowboy_http_websocket_handler.erl @@ -0,0 +1,60 @@ +%% Copyright (c) 2011, Loïc Hoguin +%% +%% Permission to use, copy, modify, and/or distribute this software for any +%% purpose with or without fee is hereby granted, provided that the above +%% copyright notice and this permission notice appear in all copies. +%% +%% THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES +%% WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF +%% MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR +%% ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES +%% WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN +%% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF +%% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + +%% @doc Handler for HTTP WebSocket requests. +%% +%% WebSocket handlers must implement four callbacks: websocket_init/3, +%% websocket_handle/3, websocket_info/3 and +%% websocket_terminate/3. These callbacks will only be called if the +%% connection is upgraded to WebSocket in the HTTP handler's init/3 +%% callback. They are then called in that order, although +%% websocket_handle/3 will be called for each packet received, +%% and websocket_info for each message received. +%% +%% websocket_init/3 is meant for initialization. It receives +%% information about the transport and protocol used, along with the handler +%% options from the dispatch list. You can define a request-wide state here. +%% If you are going to want to compact the request, you should probably do it +%% here. +%% +%% websocket_handle/3 receives the data from the socket. It can reply +%% something, do nothing or close the connection. +%% +%% websocket_info/3 receives messages sent to the process. It has +%% the same reply format as websocket_handle/3 described above. Note +%% that unlike in a gen_server, when websocket_info/3 +%% replies something, it is always to the socket, not to the process that +%% originated the message. +%% +%% websocket_terminate/3 is meant for cleaning up. It also receives +%% the request and the state previously defined, along with a reason for +%% termination. +%% +%% All of websocket_init/3, websocket_handle/3 and +%% websocket_info/3 can decide to hibernate the process by adding +%% an extra element to the returned tuple, containing the atom +%% hibernate. Doing so helps save memory and improve CPU usage. +-module(cowboy_http_websocket_handler). + +-export([behaviour_info/1]). + +%% @private +-spec behaviour_info(_) + -> undefined | [{websocket_handle, 3} | {websocket_info, 3} + | {websocket_init, 3} | {websocket_terminate, 3}, ...]. +behaviour_info(callbacks) -> + [{websocket_init, 3}, {websocket_handle, 3}, + {websocket_info, 3}, {websocket_terminate, 3}]; +behaviour_info(_Other) -> + undefined. diff --git a/rabbitmq-server/plugins-src/cowboy-wrapper/cowboy-git/src/cowboy_listener.erl b/rabbitmq-server/plugins-src/cowboy-wrapper/cowboy-git/src/cowboy_listener.erl new file mode 100644 index 0000000..4565b31 --- /dev/null +++ b/rabbitmq-server/plugins-src/cowboy-wrapper/cowboy-git/src/cowboy_listener.erl @@ -0,0 +1,174 @@ +%% Copyright (c) 2011, Loïc Hoguin +%% +%% Permission to use, copy, modify, and/or distribute this software for any +%% purpose with or without fee is hereby granted, provided that the above +%% copyright notice and this permission notice appear in all copies. +%% +%% THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES +%% WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF +%% MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR +%% ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES +%% WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN +%% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF +%% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + +%% @doc Public API for managing listeners. +-module(cowboy_listener). +-behaviour(gen_server). + +-export([start_link/0, stop/1, + add_connection/3, move_connection/3, remove_connection/2, wait/3]). %% API. +-export([init/1, handle_call/3, handle_cast/2, + handle_info/2, terminate/2, code_change/3]). %% gen_server. + +-record(state, { + req_pools = [] :: [{atom(), non_neg_integer()}], + reqs_table, + queue = [] +}). + +%% API. + +%% @private +%% +%% We set the process priority to high because cowboy_listener is the central +%% gen_server in Cowboy and is used to manage all the incoming connections. +%% Setting the process priority to high ensures the connection-related code +%% will always be executed when a connection needs it, allowing Cowboy to +%% scale far beyond what it would with a normal priority. +-spec start_link() -> {ok, pid()}. +start_link() -> + gen_server:start_link(?MODULE, [], [{spawn_opt, [{priority, high}]}]). + +%% @private +-spec stop(pid()) -> stopped. +stop(ServerPid) -> + gen_server:call(ServerPid, stop). + +%% @doc Add a connection to the given pool in the listener. +%% +%% Pools of connections are used to restrict the maximum number of connections +%% depending on their type. By default, Cowboy add all connections to the +%% pool default. It also checks for the maximum number of connections +%% in that pool before accepting again. +%% +%% When a process managing a connection dies, the process is removed from the +%% pool. If the socket has been sent to another process, it is up to the +%% protocol code to inform the listener of the new ConnPid by removing +%% the previous and adding the new one. +-spec add_connection(pid(), atom(), pid()) -> {ok, non_neg_integer()}. +add_connection(ServerPid, Pool, ConnPid) -> + gen_server:call(ServerPid, {add_connection, Pool, ConnPid}). + +%% @doc Move a connection from one pool to another. +-spec move_connection(pid(), atom(), pid()) -> ok. +move_connection(ServerPid, DestPool, ConnPid) -> + gen_server:cast(ServerPid, {move_connection, DestPool, ConnPid}). + +%% @doc Remove the given connection from its pool. +-spec remove_connection(pid(), pid()) -> ok. +remove_connection(ServerPid, ConnPid) -> + gen_server:cast(ServerPid, {remove_connection, ConnPid}). + +%% @doc Wait until the number of connections in the given pool gets below +%% the given threshold. +%% +%% This function will not return until the number of connections in the pool +%% gets below MaxConns. It makes use of gen_server:reply/2 +%% to make the process wait for a reply indefinitely. +-spec wait(pid(), atom(), non_neg_integer()) -> ok. +wait(ServerPid, Pool, MaxConns) -> + gen_server:call(ServerPid, {wait, Pool, MaxConns}, infinity). + +%% gen_server. + +%% @private +-spec init([]) -> {ok, #state{}}. +init([]) -> + ReqsTablePid = ets:new(requests_table, [set, private]), + {ok, #state{reqs_table=ReqsTablePid}}. + +%% @private +-spec handle_call(_, _, State) + -> {reply, ignored, State} | {stop, normal, stopped, State}. +handle_call({add_connection, Pool, ConnPid}, _From, State=#state{ + req_pools=Pools, reqs_table=ReqsTable}) -> + MonitorRef = erlang:monitor(process, ConnPid), + {NbConnsRet, Pools2} = case lists:keyfind(Pool, 1, Pools) of + false -> + {1, [{Pool, 1}|Pools]}; + {Pool, NbConns} -> + NbConns2 = NbConns + 1, + {NbConns2, [{Pool, NbConns2}|lists:keydelete(Pool, 1, Pools)]} + end, + ets:insert(ReqsTable, {ConnPid, {MonitorRef, Pool}}), + {reply, {ok, NbConnsRet}, State#state{req_pools=Pools2}}; +handle_call({wait, Pool, MaxConns}, From, State=#state{ + req_pools=Pools, queue=Queue}) -> + case lists:keyfind(Pool, 1, Pools) of + {Pool, NbConns} when NbConns > MaxConns -> + {noreply, State#state{queue=[From|Queue]}}; + _Any -> + {reply, ok, State} + end; +handle_call(stop, _From, State) -> + {stop, normal, stopped, State}; +handle_call(_Request, _From, State) -> + {reply, ignored, State}. + +%% @private +-spec handle_cast(_, State) -> {noreply, State}. +handle_cast({move_connection, DestPool, ConnPid}, State=#state{ + req_pools=Pools, reqs_table=ReqsTable}) -> + {MonitorRef, SrcPool} = ets:lookup_element(ReqsTable, ConnPid, 2), + ets:insert(ReqsTable, {ConnPid, {MonitorRef, DestPool}}), + {SrcPool, SrcNbConns} = lists:keyfind(SrcPool, 1, Pools), + DestNbConns = case lists:keyfind(DestPool, 1, Pools) of + false -> 1; + {DestPool, NbConns} -> NbConns + 1 + end, + Pools2 = lists:keydelete(SrcPool, 1, lists:keydelete(DestPool, 1, Pools)), + Pools3 = [{SrcPool, SrcNbConns - 1}, {DestPool, DestNbConns}|Pools2], + {noreply, State#state{req_pools=Pools3}}; +handle_cast({remove_connection, ConnPid}, State) -> + State2 = remove_pid(ConnPid, State), + {noreply, State2}; +handle_cast(_Msg, State) -> + {noreply, State}. + +%% @private +-spec handle_info(_, State) -> {noreply, State}. +handle_info({'DOWN', _Ref, process, Pid, _Info}, State) -> + State2 = remove_pid(Pid, State), + {noreply, State2}; +handle_info(_Info, State) -> + {noreply, State}. + +%% @private +-spec terminate(_, _) -> ok. +terminate(_Reason, _State) -> + ok. + +%% @private +-spec code_change(_, State, _) -> {ok, State}. +code_change(_OldVsn, State, _Extra) -> + {ok, State}. + +%% Internal. + +%% @private +-spec remove_pid(pid(), State) -> State. +remove_pid(Pid, State=#state{ + req_pools=Pools, reqs_table=ReqsTable, queue=Queue}) -> + {MonitorRef, Pool} = ets:lookup_element(ReqsTable, Pid, 2), + erlang:demonitor(MonitorRef, [flush]), + {Pool, NbConns} = lists:keyfind(Pool, 1, Pools), + Pools2 = [{Pool, NbConns - 1}|lists:keydelete(Pool, 1, Pools)], + ets:delete(ReqsTable, Pid), + case Queue of + [] -> + State#state{req_pools=Pools2}; + [Client|Queue2] -> + gen_server:reply(Client, ok), + State#state{req_pools=Pools2, queue=Queue2} + end. diff --git a/rabbitmq-server/plugins-src/cowboy-wrapper/cowboy-git/src/cowboy_listener_sup.erl b/rabbitmq-server/plugins-src/cowboy-wrapper/cowboy-git/src/cowboy_listener_sup.erl new file mode 100644 index 0000000..aca2b0b --- /dev/null +++ b/rabbitmq-server/plugins-src/cowboy-wrapper/cowboy-git/src/cowboy_listener_sup.erl @@ -0,0 +1,45 @@ +%% Copyright (c) 2011, Loïc Hoguin +%% +%% Permission to use, copy, modify, and/or distribute this software for any +%% purpose with or without fee is hereby granted, provided that the above +%% copyright notice and this permission notice appear in all copies. +%% +%% THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES +%% WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF +%% MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR +%% ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES +%% WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN +%% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF +%% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + +%% @private +-module(cowboy_listener_sup). +-behaviour(supervisor). + +-export([start_link/5]). %% API. +-export([init/1]). %% supervisor. + +%% API. + +-spec start_link(non_neg_integer(), module(), any(), module(), any()) + -> {ok, pid()}. +start_link(NbAcceptors, Transport, TransOpts, Protocol, ProtoOpts) -> + {ok, SupPid} = supervisor:start_link(?MODULE, []), + {ok, ListenerPid} = supervisor:start_child(SupPid, + {cowboy_listener, {cowboy_listener, start_link, []}, + permanent, 5000, worker, [cowboy_listener]}), + {ok, ReqsPid} = supervisor:start_child(SupPid, + {cowboy_requests_sup, {cowboy_requests_sup, start_link, []}, + permanent, 5000, supervisor, [cowboy_requests_sup]}), + {ok, _PoolPid} = supervisor:start_child(SupPid, + {cowboy_acceptors_sup, {cowboy_acceptors_sup, start_link, [ + NbAcceptors, Transport, TransOpts, + Protocol, ProtoOpts, ListenerPid, ReqsPid + ]}, permanent, 5000, supervisor, [cowboy_acceptors_sup]}), + {ok, SupPid}. + +%% supervisor. + +-spec init([]) -> {ok, {{one_for_all, 10, 10}, []}}. +init([]) -> + {ok, {{one_for_all, 10, 10}, []}}. diff --git a/rabbitmq-server/plugins-src/cowboy-wrapper/cowboy-git/src/cowboy_multipart.erl b/rabbitmq-server/plugins-src/cowboy-wrapper/cowboy-git/src/cowboy_multipart.erl new file mode 100644 index 0000000..0bd123a --- /dev/null +++ b/rabbitmq-server/plugins-src/cowboy-wrapper/cowboy-git/src/cowboy_multipart.erl @@ -0,0 +1,249 @@ +%% Copyright (c) 2011, Anthony Ramine +%% +%% Permission to use, copy, modify, and/or distribute this software for any +%% purpose with or without fee is hereby granted, provided that the above +%% copyright notice and this permission notice appear in all copies. +%% +%% THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES +%% WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF +%% MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR +%% ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES +%% WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN +%% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF +%% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + +%% @doc Multipart parser. +-module(cowboy_multipart). + +-type part_parser() :: any(). +-type parser(T) :: fun((binary()) -> T). +-type more(T) :: T | {more, parser(T)}. +-type part_result() :: any(). +-type headers() :: any(). +-type http_headers() :: [{atom() | binary(), binary()}]. +-type body_cont() :: any(). +-type cont(T) :: fun(() -> T). +-type body_result() :: any(). +-type end_of_part() :: {end_of_part, cont(more(part_result()))}. +-type disposition() :: {binary(), [{binary(), binary()}]}. + +-export([parser/1, content_disposition/1]). + +-include_lib("eunit/include/eunit.hrl"). + +%% API. + +%% @doc Return a multipart parser for the given boundary. +-spec parser(binary()) -> part_parser(). +parser(Boundary) when is_binary(Boundary) -> + fun (Bin) when is_binary(Bin) -> parse(Bin, Boundary) end. + +%% @doc Parse a content disposition. +%% @todo Parse the MIME header instead of the HTTP one. +-spec content_disposition(binary()) -> disposition(). +content_disposition(Data) -> + cowboy_http:token_ci(Data, + fun (_Rest, <<>>) -> {error, badarg}; + (Rest, Disposition) -> + cowboy_http:content_type_params(Rest, + fun (Params) -> {Disposition, Params} end, []) + end). + +%% Internal. + +%% @doc Entry point of the multipart parser, skips over the preamble if any. +-spec parse(binary(), binary()) -> more(part_result()). +parse(Bin, Boundary) when byte_size(Bin) >= byte_size(Boundary) + 2 -> + BoundarySize = byte_size(Boundary), + Pattern = pattern(Boundary), + case Bin of + <<"--", Boundary:BoundarySize/binary, Rest/binary>> -> + % Data starts with initial boundary, skip preamble parsing. + parse_boundary_tail(Rest, Pattern); + _ -> + % Parse preamble. + skip(Bin, Pattern) + end; +parse(Bin, Boundary) -> + % Not enough data to know if the data begins with a boundary. + more(Bin, fun (NewBin) -> parse(NewBin, Boundary) end). + +-type pattern() :: {binary:cp(), non_neg_integer()}. + +%% @doc Return a compiled binary pattern with its size in bytes. +%% The pattern is the boundary prepended with "\r\n--". +-spec pattern(binary()) -> pattern(). +pattern(Boundary) -> + MatchPattern = <<"\r\n--", Boundary/binary>>, + {binary:compile_pattern(MatchPattern), byte_size(MatchPattern)}. + +%% @doc Parse remaining characters of a line beginning with the boundary. +%% If followed by "--", eof is returned and parsing is finished. +-spec parse_boundary_tail(binary(), pattern()) -> more(part_result()). +parse_boundary_tail(Bin, Pattern) when byte_size(Bin) >= 2 -> + case Bin of + <<"--", _Rest/binary>> -> + % Boundary is followed by "--", end parsing. + eof; + _ -> + % No dash after boundary, proceed with unknown chars and lwsp + % removal. + parse_boundary_eol(Bin, Pattern) + end; +parse_boundary_tail(Bin, Pattern) -> + % Boundary may be followed by "--", need more data. + more(Bin, fun (NewBin) -> parse_boundary_tail(NewBin, Pattern) end). + +%% @doc Skip whitespace and unknown chars until CRLF. +-spec parse_boundary_eol(binary(), pattern()) -> more(part_result()). +parse_boundary_eol(Bin, Pattern) -> + case binary:match(Bin, <<"\r\n">>) of + {CrlfStart, _Length} -> + % End of line found, remove optional whitespace. + <<_:CrlfStart/binary, Rest/binary>> = Bin, + Fun = fun (Rest2) -> parse_boundary_crlf(Rest2, Pattern) end, + cowboy_http:whitespace(Rest, Fun); + nomatch -> + % CRLF not found in the given binary. + RestStart = lists:max([byte_size(Bin) - 1, 0]), + <<_:RestStart/binary, Rest/binary>> = Bin, + more(Rest, fun (NewBin) -> parse_boundary_eol(NewBin, Pattern) end) + end. + +-spec parse_boundary_crlf(binary(), pattern()) -> more(part_result()). +parse_boundary_crlf(<<"\r\n", Rest/binary>>, Pattern) -> + % The binary is at least 2 bytes long as this function is only called by + % parse_boundary_eol/3 when CRLF has been found so a more tuple will never + % be returned from here. + parse_headers(Rest, Pattern); +parse_boundary_crlf(Bin, Pattern) -> + % Unspecified behaviour here: RFC 2046 doesn't say what to do when LWSP is + % not followed directly by a new line. In this implementation it is + % considered part of the boundary so EOL needs to be searched again. + parse_boundary_eol(Bin, Pattern). + +-spec parse_headers(binary(), pattern()) -> more(part_result()). +parse_headers(Bin, Pattern) -> + parse_headers(Bin, Pattern, []). + +-spec parse_headers(binary(), pattern(), http_headers()) -> more(part_result()). +parse_headers(Bin, Pattern, Acc) -> + case erlang:decode_packet(httph_bin, Bin, []) of + {ok, {http_header, _, Name, _, Value}, Rest} -> + parse_headers(Rest, Pattern, [{Name, Value} | Acc]); + {ok, http_eoh, Rest} -> + Headers = lists:reverse(Acc), + {headers, Headers, fun () -> parse_body(Rest, Pattern) end}; + {ok, {http_error, _}, _} -> + % Skip malformed parts. + skip(Bin, Pattern); + {more, _} -> + more(Bin, fun (NewBin) -> parse_headers(NewBin, Pattern, Acc) end) + end. + +-spec parse_body(binary(), pattern()) -> more(body_result()). +parse_body(Bin, Pattern = {P, PSize}) when byte_size(Bin) >= PSize -> + case binary:match(Bin, P) of + {0, _Length} -> + <<_:PSize/binary, Rest/binary>> = Bin, + end_of_part(Rest, Pattern); + {BoundaryStart, _Length} -> + % Boundary found, this is the latest partial body that will be + % returned for this part. + <> = Bin, + FResult = end_of_part(Rest, Pattern), + {body, PBody, fun () -> FResult end}; + nomatch -> + PartialLength = byte_size(Bin) - PSize + 1, + <> = Bin, + {body, PBody, fun () -> parse_body(Rest, Pattern) end} + end; +parse_body(Bin, Pattern) -> + more(Bin, fun (NewBin) -> parse_body(NewBin, Pattern) end). + +-spec end_of_part(binary(), pattern()) -> end_of_part(). +end_of_part(Bin, Pattern) -> + {end_of_part, fun () -> parse_boundary_tail(Bin, Pattern) end}. + +-spec skip(binary(), pattern()) -> more(part_result()). +skip(Bin, Pattern = {P, PSize}) -> + case binary:match(Bin, P) of + {BoundaryStart, _Length} -> + % Boundary found, proceed with parsing of the next part. + RestStart = BoundaryStart + PSize, + <<_:RestStart/binary, Rest/binary>> = Bin, + parse_boundary_tail(Rest, Pattern); + nomatch -> + % Boundary not found, need more data. + RestStart = lists:max([byte_size(Bin) - PSize + 1, 0]), + <<_:RestStart/binary, Rest/binary>> = Bin, + more(Rest, fun (NewBin) -> skip(NewBin, Pattern) end) + end. + +-spec more(binary(), parser(T)) -> {more, parser(T)}. +more(<<>>, F) -> + {more, F}; +more(Bin, InnerF) -> + F = fun (NewData) when is_binary(NewData) -> + InnerF(<>) + end, + {more, F}. + +%% Tests. + +-ifdef(TEST). + +multipart_test_() -> + %% {Body, Result} + Tests = [ + {<<"--boundary--">>, []}, + {<<"preamble\r\n--boundary--">>, []}, + {<<"--boundary--\r\nepilogue">>, []}, + {<<"\r\n--boundary\r\nA:b\r\nC:d\r\n\r\n\r\n--boundary--">>, + [{[{<<"A">>, <<"b">>}, {<<"C">>, <<"d">>}], <<>>}]}, + { + << + "--boundary\r\nX-Name:answer\r\n\r\n42" + "\r\n--boundary\r\nServer:Cowboy\r\n\r\nIt rocks!\r\n" + "\r\n--boundary--" + >>, + [ + {[{<<"X-Name">>, <<"answer">>}], <<"42">>}, + {[{'Server', <<"Cowboy">>}], <<"It rocks!\r\n">>} + ] + } + ], + [{title(V), fun () -> R = acc_multipart(V) end} || {V, R} <- Tests]. + +acc_multipart(V) -> + acc_multipart((parser(<<"boundary">>))(V), []). + +acc_multipart({headers, Headers, Cont}, Acc) -> + acc_multipart(Cont(), [{Headers, []}|Acc]); +acc_multipart({body, Body, Cont}, [{Headers, BodyAcc}|Acc]) -> + acc_multipart(Cont(), [{Headers, [Body|BodyAcc]}|Acc]); +acc_multipart({end_of_part, Cont}, [{Headers, BodyAcc}|Acc]) -> + Body = list_to_binary(lists:reverse(BodyAcc)), + acc_multipart(Cont(), [{Headers, Body}|Acc]); +acc_multipart(eof, Acc) -> + lists:reverse(Acc). + +content_disposition_test_() -> + %% {Disposition, Result} + Tests = [ + {<<"form-data; name=id">>, {<<"form-data">>, [{<<"name">>, <<"id">>}]}}, + {<<"inline">>, {<<"inline">>, []}}, + {<<"attachment; \tfilename=brackets-slides.pdf">>, + {<<"attachment">>, [{<<"filename">>, <<"brackets-slides.pdf">>}]}} + ], + [{title(V), fun () -> R = content_disposition(V) end} || {V, R} <- Tests]. + +title(Bin) -> + Title = lists:foldl( + fun ({T, R}, V) -> re:replace(V, T, R, [global]) end, + Bin, + [{"\t", "\\\\t"}, {"\r", "\\\\r"}, {"\n", "\\\\n"}] + ), + iolist_to_binary(Title). + +-endif. diff --git a/rabbitmq-server/plugins-src/cowboy-wrapper/cowboy-git/src/cowboy_protocol.erl b/rabbitmq-server/plugins-src/cowboy-wrapper/cowboy-git/src/cowboy_protocol.erl new file mode 100644 index 0000000..34bb1a1 --- /dev/null +++ b/rabbitmq-server/plugins-src/cowboy-wrapper/cowboy-git/src/cowboy_protocol.erl @@ -0,0 +1,61 @@ +%% Copyright (c) 2011, Loïc Hoguin +%% Copyright (c) 2011, Michiel Hakvoort +%% +%% Permission to use, copy, modify, and/or distribute this software for any +%% purpose with or without fee is hereby granted, provided that the above +%% copyright notice and this permission notice appear in all copies. +%% +%% THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES +%% WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF +%% MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR +%% ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES +%% WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN +%% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF +%% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + +%% @doc Cowboy protocol. +%% +%% A Cowboy protocol must implement one callback: start_link/4. +%% +%% start_link/4 is meant for the initialization of the +%% protocol process. +%% It receives the pid to the listener's gen_server, the client socket, +%% the module name of the chosen transport and the options defined when +%% starting the listener. The start_link/4 function must follow +%% the supervisor start function specification. +%% +%% After initializing your protocol, it is recommended to call the +%% function cowboy:accept_ack/1 with the ListenerPid as argument, +%% as it will ensure Cowboy has been able to fully initialize the socket. +%% Anything you do past this point is up to you! +%% +%% If you need to change some socket options, like enabling raw mode +%% for example, you can call the Transport:setopts/2 function. +%% It is the protocol's responsability to manage the socket usage, +%% there should be no need for an user to specify that kind of options +%% while starting a listener. +%% +%% You should definitely look at the cowboy_http_protocol module for +%% a great example of fast request handling if you need to. +%% Otherwise it's probably safe to use {active, once} mode +%% and handle everything as it comes. +%% +%% Note that while you technically can run a protocol handler directly +%% as a gen_server or a gen_fsm, it's probably not a good idea, +%% as the only call you'll ever receive from Cowboy is the +%% start_link/4 call. On the other hand, feel free to write +%% a very basic protocol handler which then forwards requests to a +%% gen_server or gen_fsm. By doing so however you must take care to +%% supervise their processes as Cowboy only knows about the protocol +%% handler itself. +-module(cowboy_protocol). + +-export([behaviour_info/1]). + +%% @private +-spec behaviour_info(_) + -> undefined | [{start_link, 4}, ...]. +behaviour_info(callbacks) -> + [{start_link, 4}]; +behaviour_info(_Other) -> + undefined. diff --git a/rabbitmq-server/plugins-src/cowboy-wrapper/cowboy-git/src/cowboy_requests_sup.erl b/rabbitmq-server/plugins-src/cowboy-wrapper/cowboy-git/src/cowboy_requests_sup.erl new file mode 100644 index 0000000..87d5352 --- /dev/null +++ b/rabbitmq-server/plugins-src/cowboy-wrapper/cowboy-git/src/cowboy_requests_sup.erl @@ -0,0 +1,38 @@ +%% Copyright (c) 2011, Loïc Hoguin +%% +%% Permission to use, copy, modify, and/or distribute this software for any +%% purpose with or without fee is hereby granted, provided that the above +%% copyright notice and this permission notice appear in all copies. +%% +%% THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES +%% WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF +%% MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR +%% ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES +%% WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN +%% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF +%% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + +%% @private +-module(cowboy_requests_sup). +-behaviour(supervisor). + +-export([start_link/0, start_request/5]). %% API. +-export([init/1]). %% supervisor. + +%% API. + +-spec start_link() -> {ok, pid()}. +start_link() -> + supervisor:start_link(?MODULE, []). + +-spec start_request(pid(), inet:socket(), module(), module(), any()) + -> {ok, pid()}. +start_request(ListenerPid, Socket, Transport, Protocol, Opts) -> + Protocol:start_link(ListenerPid, Socket, Transport, Opts). + +%% supervisor. + +-spec init([]) -> {ok, {{simple_one_for_one, 0, 1}, [{_, _, _, _, _, _}, ...]}}. +init([]) -> + {ok, {{simple_one_for_one, 0, 1}, [{?MODULE, {?MODULE, start_request, []}, + temporary, brutal_kill, worker, [?MODULE]}]}}. diff --git a/rabbitmq-server/plugins-src/cowboy-wrapper/cowboy-git/src/cowboy_ssl_transport.erl b/rabbitmq-server/plugins-src/cowboy-wrapper/cowboy-git/src/cowboy_ssl_transport.erl new file mode 100644 index 0000000..bf8b1fb --- /dev/null +++ b/rabbitmq-server/plugins-src/cowboy-wrapper/cowboy-git/src/cowboy_ssl_transport.erl @@ -0,0 +1,164 @@ +%% Copyright (c) 2011, Loïc Hoguin +%% +%% Permission to use, copy, modify, and/or distribute this software for any +%% purpose with or without fee is hereby granted, provided that the above +%% copyright notice and this permission notice appear in all copies. +%% +%% THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES +%% WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF +%% MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR +%% ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES +%% WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN +%% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF +%% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + +%% @doc SSL transport API. +%% +%% Wrapper around ssl implementing the Cowboy transport API. +%% +%% This transport requires the crypto, public_key +%% and ssl applications to be started. If they aren't started, +%% it will try to start them itself before opening a port to listen. +%% Applications aren't stopped when the listening socket is closed, though. +%% +%% @see ssl +-module(cowboy_ssl_transport). +-export([name/0, messages/0, listen/1, accept/2, recv/3, send/2, setopts/2, + controlling_process/2, peername/1, close/1]). + +%% @doc Name of this transport API, ssl. +-spec name() -> ssl. +name() -> ssl. + +%% @doc Atoms used in the process messages sent by this API. +%% +%% They identify incoming data, closed connection and errors when receiving +%% data in active mode. +-spec messages() -> {ssl, ssl_closed, ssl_error}. +messages() -> {ssl, ssl_closed, ssl_error}. + +%% @doc Setup a socket to listen on the given port on the local host. +%% +%% The available options are: +%%
+%%
port
Mandatory. TCP port number to open.
+%%
backlog
Maximum length of the pending connections queue. +%% Defaults to 1024.
+%%
ip
Interface to listen on. Listen on all interfaces +%% by default.
+%%
certfile
Mandatory. Path to a file containing the user's +%% certificate.
+%%
keyfile
Mandatory. Path to the file containing the user's +%% private PEM encoded key.
+%%
cacertfile
Optional. Path to file containing PEM encoded +%% CA certificates (trusted certificates used for verifying a peer +%% certificate).
+%%
password
Mandatory. String containing the user's password. +%% All private keyfiles must be password protected currently.
+%%
+%% +%% @see ssl:listen/2 +%% @todo The password option shouldn't be mandatory. +-spec listen([{port, inet:ip_port()} | {certfile, string()} + | {keyfile, string()} | {password, string()} + | {cacertfile, string()} | {ip, inet:ip_address()}]) + -> {ok, ssl:sslsocket()} | {error, atom()}. +listen(Opts) -> + require([crypto, public_key, ssl]), + {port, Port} = lists:keyfind(port, 1, Opts), + Backlog = proplists:get_value(backlog, Opts, 1024), + {certfile, CertFile} = lists:keyfind(certfile, 1, Opts), + {keyfile, KeyFile} = lists:keyfind(keyfile, 1, Opts), + {password, Password} = lists:keyfind(password, 1, Opts), + ListenOpts0 = [binary, {active, false}, + {backlog, Backlog}, {packet, raw}, {reuseaddr, true}, + {certfile, CertFile}, {keyfile, KeyFile}, {password, Password}], + ListenOpts1 = + case lists:keyfind(ip, 1, Opts) of + false -> ListenOpts0; + Ip -> [Ip|ListenOpts0] + end, + ListenOpts = + case lists:keyfind(cacertfile, 1, Opts) of + false -> ListenOpts1; + CACertFile -> [CACertFile|ListenOpts1] + end, + ssl:listen(Port, ListenOpts). + +%% @doc Accept an incoming connection on a listen socket. +%% +%% Note that this function does both the transport accept and +%% the SSL handshake. +%% +%% @see ssl:transport_accept/2 +%% @see ssl:ssl_accept/2 +-spec accept(ssl:sslsocket(), timeout()) + -> {ok, ssl:sslsocket()} | {error, closed | timeout | atom()}. +accept(LSocket, Timeout) -> + case ssl:transport_accept(LSocket, Timeout) of + {ok, CSocket} -> + ssl_accept(CSocket, Timeout); + {error, Reason} -> + {error, Reason} + end. + +%% @doc Receive a packet from a socket in passive mode. +%% @see ssl:recv/3 +-spec recv(ssl:sslsocket(), non_neg_integer(), timeout()) + -> {ok, any()} | {error, closed | atom()}. +recv(Socket, Length, Timeout) -> + ssl:recv(Socket, Length, Timeout). + +%% @doc Send a packet on a socket. +%% @see ssl:send/2 +-spec send(ssl:sslsocket(), iolist()) -> ok | {error, atom()}. +send(Socket, Packet) -> + ssl:send(Socket, Packet). + +%% @doc Set one or more options for a socket. +%% @see ssl:setopts/2 +-spec setopts(ssl:sslsocket(), list()) -> ok | {error, atom()}. +setopts(Socket, Opts) -> + ssl:setopts(Socket, Opts). + +%% @doc Assign a new controlling process Pid to Socket. +%% @see ssl:controlling_process/2 +-spec controlling_process(ssl:sslsocket(), pid()) + -> ok | {error, closed | not_owner | atom()}. +controlling_process(Socket, Pid) -> + ssl:controlling_process(Socket, Pid). + +%% @doc Return the address and port for the other end of a connection. +%% @see ssl:peername/1 +-spec peername(ssl:sslsocket()) + -> {ok, {inet:ip_address(), inet:ip_port()}} | {error, atom()}. +peername(Socket) -> + ssl:peername(Socket). + +%% @doc Close a TCP socket. +%% @see ssl:close/1 +-spec close(ssl:sslsocket()) -> ok. +close(Socket) -> + ssl:close(Socket). + +%% Internal. + +-spec require(list(module())) -> ok. +require([]) -> + ok; +require([App|Tail]) -> + case application:start(App) of + ok -> ok; + {error, {already_started, App}} -> ok + end, + require(Tail). + +-spec ssl_accept(ssl:sslsocket(), timeout()) + -> {ok, ssl:sslsocket()} | {error, closed | timeout | atom()}. +ssl_accept(Socket, Timeout) -> + case ssl:ssl_accept(Socket, Timeout) of + ok -> + {ok, Socket}; + {error, Reason} -> + {error, Reason} + end. diff --git a/rabbitmq-server/plugins-src/cowboy-wrapper/cowboy-git/src/cowboy_sup.erl b/rabbitmq-server/plugins-src/cowboy-wrapper/cowboy-git/src/cowboy_sup.erl new file mode 100644 index 0000000..34591bc --- /dev/null +++ b/rabbitmq-server/plugins-src/cowboy-wrapper/cowboy-git/src/cowboy_sup.erl @@ -0,0 +1,36 @@ +%% Copyright (c) 2011, Loïc Hoguin +%% +%% Permission to use, copy, modify, and/or distribute this software for any +%% purpose with or without fee is hereby granted, provided that the above +%% copyright notice and this permission notice appear in all copies. +%% +%% THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES +%% WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF +%% MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR +%% ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES +%% WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN +%% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF +%% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + +%% @private +-module(cowboy_sup). +-behaviour(supervisor). + +-export([start_link/0]). %% API. +-export([init/1]). %% supervisor. + +-define(SUPERVISOR, ?MODULE). + +%% API. + +-spec start_link() -> {ok, pid()}. +start_link() -> + supervisor:start_link({local, ?SUPERVISOR}, ?MODULE, []). + +%% supervisor. + +-spec init([]) -> {ok, {{one_for_one, 10, 10}, [{_, _, _, _, _, _}, ...]}}. +init([]) -> + Procs = [{cowboy_clock, {cowboy_clock, start_link, []}, + permanent, 5000, worker, [cowboy_clock]}], + {ok, {{one_for_one, 10, 10}, Procs}}. diff --git a/rabbitmq-server/plugins-src/cowboy-wrapper/cowboy-git/src/cowboy_tcp_transport.erl b/rabbitmq-server/plugins-src/cowboy-wrapper/cowboy-git/src/cowboy_tcp_transport.erl new file mode 100644 index 0000000..c1dad62 --- /dev/null +++ b/rabbitmq-server/plugins-src/cowboy-wrapper/cowboy-git/src/cowboy_tcp_transport.erl @@ -0,0 +1,106 @@ +%% Copyright (c) 2011, Loïc Hoguin +%% +%% Permission to use, copy, modify, and/or distribute this software for any +%% purpose with or without fee is hereby granted, provided that the above +%% copyright notice and this permission notice appear in all copies. +%% +%% THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES +%% WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF +%% MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR +%% ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES +%% WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN +%% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF +%% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + +%% @doc TCP transport API. +%% +%% Wrapper around gen_tcp implementing the Cowboy transport API. +%% +%% @see gen_tcp +-module(cowboy_tcp_transport). + +-export([name/0, messages/0, listen/1, accept/2, recv/3, send/2, setopts/2, + controlling_process/2, peername/1, close/1]). + +%% @doc Name of this transport API, tcp. +-spec name() -> tcp. +name() -> tcp. + +%% @doc Atoms used in the process messages sent by this API. +%% +%% They identify incoming data, closed connection and errors when receiving +%% data in active mode. +-spec messages() -> {tcp, tcp_closed, tcp_error}. +messages() -> {tcp, tcp_closed, tcp_error}. + +%% @doc Setup a socket to listen on the given port on the local host. +%% +%% The available options are: +%%
+%%
port
Mandatory. TCP port number to open.
+%%
backlog
Maximum length of the pending connections queue. +%% Defaults to 1024.
+%%
ip
Interface to listen on. Listen on all interfaces +%% by default.
+%%
+%% +%% @see gen_tcp:listen/2 +-spec listen([{port, inet:ip_port()} | {ip, inet:ip_address()}]) + -> {ok, inet:socket()} | {error, atom()}. +listen(Opts) -> + {port, Port} = lists:keyfind(port, 1, Opts), + Backlog = proplists:get_value(backlog, Opts, 1024), + ListenOpts0 = [binary, {active, false}, + {backlog, Backlog}, {packet, raw}, {reuseaddr, true}], + ListenOpts = + case lists:keyfind(ip, 1, Opts) of + false -> ListenOpts0; + Ip -> [Ip|ListenOpts0] + end, + gen_tcp:listen(Port, ListenOpts). + +%% @doc Accept an incoming connection on a listen socket. +%% @see gen_tcp:accept/2 +-spec accept(inet:socket(), timeout()) + -> {ok, inet:socket()} | {error, closed | timeout | atom()}. +accept(LSocket, Timeout) -> + gen_tcp:accept(LSocket, Timeout). + +%% @doc Receive a packet from a socket in passive mode. +%% @see gen_tcp:recv/3 +-spec recv(inet:socket(), non_neg_integer(), timeout()) + -> {ok, any()} | {error, closed | atom()}. +recv(Socket, Length, Timeout) -> + gen_tcp:recv(Socket, Length, Timeout). + +%% @doc Send a packet on a socket. +%% @see gen_tcp:send/2 +-spec send(inet:socket(), iolist()) -> ok | {error, atom()}. +send(Socket, Packet) -> + gen_tcp:send(Socket, Packet). + +%% @doc Set one or more options for a socket. +%% @see inet:setopts/2 +-spec setopts(inet:socket(), list()) -> ok | {error, atom()}. +setopts(Socket, Opts) -> + inet:setopts(Socket, Opts). + +%% @doc Assign a new controlling process Pid to Socket. +%% @see gen_tcp:controlling_process/2 +-spec controlling_process(inet:socket(), pid()) + -> ok | {error, closed | not_owner | atom()}. +controlling_process(Socket, Pid) -> + gen_tcp:controlling_process(Socket, Pid). + +%% @doc Return the address and port for the other end of a connection. +%% @see inet:peername/1 +-spec peername(inet:socket()) + -> {ok, {inet:ip_address(), inet:ip_port()}} | {error, atom()}. +peername(Socket) -> + inet:peername(Socket). + +%% @doc Close a TCP socket. +%% @see gen_tcp:close/1 +-spec close(inet:socket()) -> ok. +close(Socket) -> + gen_tcp:close(Socket). diff --git a/rabbitmq-server/plugins-src/cowboy-wrapper/cowboy-git/test/chunked_handler.erl b/rabbitmq-server/plugins-src/cowboy-wrapper/cowboy-git/test/chunked_handler.erl new file mode 100644 index 0000000..d246d51 --- /dev/null +++ b/rabbitmq-server/plugins-src/cowboy-wrapper/cowboy-git/test/chunked_handler.erl @@ -0,0 +1,17 @@ +%% Feel free to use, reuse and abuse the code in this file. + +-module(chunked_handler). +-behaviour(cowboy_http_handler). +-export([init/3, handle/2, terminate/2]). + +init({_Transport, http}, Req, _Opts) -> + {ok, Req, undefined}. + +handle(Req, State) -> + {ok, Req2} = cowboy_http_req:chunked_reply(200, Req), + cowboy_http_req:chunk("chunked_handler\r\n", Req2), + cowboy_http_req:chunk("works fine!", Req2), + {ok, Req2, State}. + +terminate(_Req, _State) -> + ok. diff --git a/rabbitmq-server/plugins-src/cowboy-wrapper/cowboy-git/test/dispatcher_prop.erl b/rabbitmq-server/plugins-src/cowboy-wrapper/cowboy-git/test/dispatcher_prop.erl new file mode 100644 index 0000000..b6a1c92 --- /dev/null +++ b/rabbitmq-server/plugins-src/cowboy-wrapper/cowboy-git/test/dispatcher_prop.erl @@ -0,0 +1,68 @@ +%% Copyright (c) 2011, Magnus Klaar +%% Copyright (c) 2011, Loïc Hoguin +%% +%% Permission to use, copy, modify, and/or distribute this software for any +%% purpose with or without fee is hereby granted, provided that the above +%% copyright notice and this permission notice appear in all copies. +%% +%% THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES +%% WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF +%% MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR +%% ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES +%% WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN +%% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF +%% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + +-module(dispatcher_prop). +-include_lib("proper/include/proper.hrl"). + +%% Generators. + +hostname_head_char() -> + oneof([choose($a, $z), choose($A, $Z), choose($0, $9)]). + +hostname_char() -> + oneof([choose($a, $z), choose($A, $Z), choose($0, $9), $-]). + +hostname_label() -> + ?SUCHTHAT(Label, [hostname_head_char()|list(hostname_char())], + length(Label) < 64). + +hostname() -> + ?SUCHTHAT(Hostname, + ?LET(Labels, list(hostname_label()), string:join(Labels, ".")), + length(Hostname) > 0 andalso length(Hostname) =< 255). + +port_number() -> + choose(1, 16#ffff). + +port_str() -> + oneof(["", ?LET(Port, port_number(), ":" ++ integer_to_list(Port))]). + +server() -> + ?LET({Hostname, PortStr}, {hostname(), port_str()}, + list_to_binary(Hostname ++ PortStr)). + +%% Properties. + +prop_split_host_symmetric() -> + ?FORALL(Server, server(), + begin case cowboy_dispatcher:split_host(Server) of + {Tokens, RawHost, undefined} -> + (Server == RawHost) and (Server == binary_join(Tokens, ".")); + {Tokens, RawHost, Port} -> + PortBin = (list_to_binary(":" ++ integer_to_list(Port))), + (Server == << RawHost/binary, PortBin/binary >>) + and (Server == << (binary_join(Tokens, "."))/binary, + PortBin/binary >>) + end end). + +%% Internal. + +%% Contributed by MononcQc on #erlounge. +binary_join(Flowers, Leaf) -> + case Flowers of + [] -> <<>>; + [Petal|Pot] -> iolist_to_binary( + [Petal | [[Leaf | Pollen] || Pollen <- Pot]]) + end. diff --git a/rabbitmq-server/plugins-src/cowboy-wrapper/cowboy-git/test/http_SUITE.erl b/rabbitmq-server/plugins-src/cowboy-wrapper/cowboy-git/test/http_SUITE.erl new file mode 100644 index 0000000..bad91a8 --- /dev/null +++ b/rabbitmq-server/plugins-src/cowboy-wrapper/cowboy-git/test/http_SUITE.erl @@ -0,0 +1,613 @@ +%% Copyright (c) 2011, Loïc Hoguin +%% Copyright (c) 2011, Anthony Ramine +%% +%% Permission to use, copy, modify, and/or distribute this software for any +%% purpose with or without fee is hereby granted, provided that the above +%% copyright notice and this permission notice appear in all copies. +%% +%% THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES +%% WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF +%% MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR +%% ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES +%% WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN +%% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF +%% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + +-module(http_SUITE). + +-include_lib("common_test/include/ct.hrl"). + +-export([all/0, groups/0, init_per_suite/1, end_per_suite/1, + init_per_group/2, end_per_group/2]). %% ct. +-export([chunked_response/1, headers_dupe/1, headers_huge/1, + keepalive_nl/1, max_keepalive/1, nc_rand/1, nc_zero/1, + pipeline/1, raw/1, set_resp_header/1, set_resp_overwrite/1, + set_resp_body/1, stream_body_set_resp/1, response_as_req/1, + static_mimetypes_function/1, static_attribute_etag/1, + static_function_etag/1, multipart/1]). %% http. +-export([http_200/1, http_404/1, handler_errors/1, + file_200/1, file_403/1, dir_403/1, file_404/1, + file_400/1]). %% http and https. +-export([http_10_hostless/1]). %% misc. +-export([rest_simple/1, rest_keepalive/1, rest_keepalive_post/1]). %% rest. + +%% ct. + +all() -> + [{group, http}, {group, https}, {group, misc}, {group, rest}]. + +groups() -> + BaseTests = [http_200, http_404, handler_errors, + file_200, file_403, dir_403, file_404, file_400], + [{http, [], [chunked_response, headers_dupe, headers_huge, + keepalive_nl, max_keepalive, nc_rand, nc_zero, pipeline, raw, + set_resp_header, set_resp_overwrite, + set_resp_body, response_as_req, stream_body_set_resp, + static_mimetypes_function, static_attribute_etag, + static_function_etag, multipart] ++ BaseTests}, + {https, [], BaseTests}, + {misc, [], [http_10_hostless]}, + {rest, [], [rest_simple, rest_keepalive, rest_keepalive_post]}]. + +init_per_suite(Config) -> + application:start(inets), + application:start(cowboy), + Config. + +end_per_suite(_Config) -> + application:stop(cowboy), + application:stop(inets), + ok. + +init_per_group(http, Config) -> + Port = 33080, + Config1 = init_static_dir(Config), + cowboy:start_listener(http, 100, + cowboy_tcp_transport, [{port, Port}], + cowboy_http_protocol, [{max_keepalive, 50}, + {dispatch, init_http_dispatch(Config1)}] + ), + [{scheme, "http"}, {port, Port}|Config1]; +init_per_group(https, Config) -> + Port = 33081, + Config1 = init_static_dir(Config), + application:start(crypto), + application:start(public_key), + application:start(ssl), + DataDir = ?config(data_dir, Config), + cowboy:start_listener(https, 100, + cowboy_ssl_transport, [ + {port, Port}, {certfile, DataDir ++ "cert.pem"}, + {keyfile, DataDir ++ "key.pem"}, {password, "cowboy"}], + cowboy_http_protocol, [{dispatch, init_https_dispatch(Config1)}] + ), + [{scheme, "https"}, {port, Port}|Config1]; +init_per_group(misc, Config) -> + Port = 33082, + cowboy:start_listener(misc, 100, + cowboy_tcp_transport, [{port, Port}], + cowboy_http_protocol, [{dispatch, [{'_', [ + {[], http_handler, []} + ]}]}]), + [{port, Port}|Config]; +init_per_group(rest, Config) -> + Port = 33083, + cowboy:start_listener(reset, 100, + cowboy_tcp_transport, [{port, Port}], + cowboy_http_protocol, [{dispatch, [{'_', [ + {[<<"simple">>], rest_simple_resource, []}, + {[<<"forbidden_post">>], rest_forbidden_resource, [true]}, + {[<<"simple_post">>], rest_forbidden_resource, [false]} + ]}]}]), + [{port, Port}|Config]. + +end_per_group(https, Config) -> + cowboy:stop_listener(https), + application:stop(ssl), + application:stop(public_key), + application:stop(crypto), + end_static_dir(Config), + ok; +end_per_group(http, Config) -> + cowboy:stop_listener(http), + end_static_dir(Config); +end_per_group(Listener, _Config) -> + cowboy:stop_listener(Listener), + ok. + +%% Dispatch configuration. + +init_http_dispatch(Config) -> + [ + {[<<"localhost">>], [ + {[<<"chunked_response">>], chunked_handler, []}, + {[<<"init_shutdown">>], http_handler_init_shutdown, []}, + {[<<"long_polling">>], http_handler_long_polling, []}, + {[<<"headers">>, <<"dupe">>], http_handler, + [{headers, [{<<"Connection">>, <<"close">>}]}]}, + {[<<"set_resp">>, <<"header">>], http_handler_set_resp, + [{headers, [{<<"Vary">>, <<"Accept">>}]}]}, + {[<<"set_resp">>, <<"overwrite">>], http_handler_set_resp, + [{headers, [{<<"Server">>, <<"DesireDrive/1.0">>}]}]}, + {[<<"set_resp">>, <<"body">>], http_handler_set_resp, + [{body, <<"A flameless dance does not equal a cycle">>}]}, + {[<<"stream_body">>, <<"set_resp">>], http_handler_stream_body, + [{reply, set_resp}, {body, <<"stream_body_set_resp">>}]}, + {[<<"static">>, '...'], cowboy_http_static, + [{directory, ?config(static_dir, Config)}, + {mimetypes, [{<<".css">>, [<<"text/css">>]}]}]}, + {[<<"static_mimetypes_function">>, '...'], cowboy_http_static, + [{directory, ?config(static_dir, Config)}, + {mimetypes, {fun(Path, data) when is_binary(Path) -> + [<<"text/html">>] end, data}}]}, + {[<<"handler_errors">>], http_handler_errors, []}, + {[<<"static_attribute_etag">>, '...'], cowboy_http_static, + [{directory, ?config(static_dir, Config)}, + {etag, {attributes, [filepath, filesize, inode, mtime]}}]}, + {[<<"static_function_etag">>, '...'], cowboy_http_static, + [{directory, ?config(static_dir, Config)}, + {etag, {fun static_function_etag/2, etag_data}}]}, + {[<<"multipart">>], http_handler_multipart, []}, + {[], http_handler, []} + ]} + ]. + +init_https_dispatch(Config) -> + init_http_dispatch(Config). + + +init_static_dir(Config) -> + Dir = filename:join(?config(priv_dir, Config), "static"), + Level1 = fun(Name) -> filename:join(Dir, Name) end, + ok = file:make_dir(Dir), + ok = file:write_file(Level1("test_file"), "test_file\n"), + ok = file:write_file(Level1("test_file.css"), "test_file.css\n"), + ok = file:write_file(Level1("test_noread"), "test_noread\n"), + ok = file:change_mode(Level1("test_noread"), 8#0333), + ok = file:write_file(Level1("test.html"), "test.html\n"), + ok = file:make_dir(Level1("test_dir")), + [{static_dir, Dir}|Config]. + +end_static_dir(Config) -> + Dir = ?config(static_dir, Config), + Level1 = fun(Name) -> filename:join(Dir, Name) end, + ok = file:delete(Level1("test_file")), + ok = file:delete(Level1("test_file.css")), + ok = file:delete(Level1("test_noread")), + ok = file:delete(Level1("test.html")), + ok = file:del_dir(Level1("test_dir")), + ok = file:del_dir(Dir), + Config. + +%% http. + +chunked_response(Config) -> + {ok, {{"HTTP/1.1", 200, "OK"}, _Headers, "chunked_handler\r\nworks fine!"}} = + httpc:request(build_url("/chunked_response", Config)). + +headers_dupe(Config) -> + {port, Port} = lists:keyfind(port, 1, Config), + {ok, Socket} = gen_tcp:connect("localhost", Port, + [binary, {active, false}, {packet, raw}]), + ok = gen_tcp:send(Socket, "GET /headers/dupe HTTP/1.1\r\n" + "Host: localhost\r\nConnection: keep-alive\r\n\r\n"), + {ok, Data} = gen_tcp:recv(Socket, 0, 6000), + {_Start, _Length} = binary:match(Data, <<"Connection: close">>), + nomatch = binary:match(Data, <<"Connection: keep-alive">>), + {error, closed} = gen_tcp:recv(Socket, 0, 1000). + +headers_huge(Config) -> + Cookie = lists:flatten(["whatever_man_biiiiiiiiiiiig_cookie_me_want_77=" + "Wed Apr 06 2011 10:38:52 GMT-0500 (CDT)" || _N <- lists:seq(1, 40)]), + {_Packet, 200} = raw_req(["GET / HTTP/1.0\r\nHost: localhost\r\n" + "Set-Cookie: ", Cookie, "\r\n\r\n"], Config). + +keepalive_nl(Config) -> + {port, Port} = lists:keyfind(port, 1, Config), + {ok, Socket} = gen_tcp:connect("localhost", Port, + [binary, {active, false}, {packet, raw}]), + ok = keepalive_nl_loop(Socket, 10), + ok = gen_tcp:close(Socket). + +keepalive_nl_loop(_Socket, 0) -> + ok; +keepalive_nl_loop(Socket, N) -> + ok = gen_tcp:send(Socket, "GET / HTTP/1.1\r\n" + "Host: localhost\r\nConnection: keep-alive\r\n\r\n"), + {ok, Data} = gen_tcp:recv(Socket, 0, 6000), + {0, 12} = binary:match(Data, <<"HTTP/1.1 200">>), + nomatch = binary:match(Data, <<"Connection: close">>), + ok = gen_tcp:send(Socket, "\r\n"), %% extra nl + keepalive_nl_loop(Socket, N - 1). + +max_keepalive(Config) -> + {port, Port} = lists:keyfind(port, 1, Config), + {ok, Socket} = gen_tcp:connect("localhost", Port, + [binary, {active, false}, {packet, raw}]), + ok = max_keepalive_loop(Socket, 50), + {error, closed} = gen_tcp:recv(Socket, 0, 1000). + +max_keepalive_loop(_Socket, 0) -> + ok; +max_keepalive_loop(Socket, N) -> + ok = gen_tcp:send(Socket, "GET / HTTP/1.1\r\n" + "Host: localhost\r\nConnection: keep-alive\r\n\r\n"), + {ok, Data} = gen_tcp:recv(Socket, 0, 6000), + {0, 12} = binary:match(Data, <<"HTTP/1.1 200">>), + case N of + 1 -> {_, _} = binary:match(Data, <<"Connection: close">>); + N -> nomatch = binary:match(Data, <<"Connection: close">>) + end, + keepalive_nl_loop(Socket, N - 1). + +multipart(Config) -> + Url = build_url("/multipart", Config), + Body = << + "This is a preamble." + "\r\n--OHai\r\nX-Name:answer\r\n\r\n42" + "\r\n--OHai\r\nServer:Cowboy\r\n\r\nIt rocks!\r\n" + "\r\n--OHai--" + "This is an epiloque." + >>, + Request = {Url, [], "multipart/x-makes-no-sense; boundary=OHai", Body}, + {ok, {{"HTTP/1.1", 200, "OK"}, _Headers, Response}} = + httpc:request(post, Request, [], [{body_format, binary}]), + Parts = binary_to_term(Response), + Parts = [ + {[{<<"X-Name">>, <<"answer">>}], <<"42">>}, + {[{'Server', <<"Cowboy">>}], <<"It rocks!\r\n">>} + ]. + +nc_rand(Config) -> + nc_reqs(Config, "/dev/urandom"). + +nc_zero(Config) -> + nc_reqs(Config, "/dev/zero"). + +nc_reqs(Config, Input) -> + Cat = os:find_executable("cat"), + Nc = os:find_executable("nc"), + case {Cat, Nc} of + {false, _} -> + {skip, {notfound, cat}}; + {_, false} -> + {skip, {notfound, nc}}; + _Good -> + %% Throw garbage at the server then check if it's still up. + {port, Port} = lists:keyfind(port, 1, Config), + [nc_run_req(Port, Input) || _N <- lists:seq(1, 100)], + Packet = "GET / HTTP/1.0\r\nHost: localhost\r\n\r\n", + {Packet, 200} = raw_req(Packet, Config) + end. + +nc_run_req(Port, Input) -> + os:cmd("cat " ++ Input ++ " | nc localhost " ++ integer_to_list(Port)). + +pipeline(Config) -> + {port, Port} = lists:keyfind(port, 1, Config), + {ok, Socket} = gen_tcp:connect("localhost", Port, + [binary, {active, false}, {packet, raw}]), + ok = gen_tcp:send(Socket, + "GET / HTTP/1.1\r\nHost: localhost\r\nConnection: keep-alive\r\n\r\n" + "GET / HTTP/1.1\r\nHost: localhost\r\nConnection: keep-alive\r\n\r\n" + "GET / HTTP/1.1\r\nHost: localhost\r\nConnection: keep-alive\r\n\r\n" + "GET / HTTP/1.1\r\nHost: localhost\r\nConnection: keep-alive\r\n\r\n" + "GET / HTTP/1.1\r\nHost: localhost\r\nConnection: close\r\n\r\n"), + Data = pipeline_recv(Socket, <<>>), + Reqs = binary:split(Data, << "\r\n\r\nhttp_handler" >>, [global, trim]), + 5 = length(Reqs), + pipeline_check(Reqs). + +pipeline_check([]) -> + ok; +pipeline_check([Req|Tail]) -> + << "HTTP/1.1 200", _Rest/bits >> = Req, + pipeline_check(Tail). + +pipeline_recv(Socket, SoFar) -> + case gen_tcp:recv(Socket, 0, 6000) of + {ok, Data} -> + pipeline_recv(Socket, << SoFar/binary, Data/binary >>); + {error, closed} -> + ok = gen_tcp:close(Socket), + SoFar + end. + +raw_req(Packet, Config) -> + {port, Port} = lists:keyfind(port, 1, Config), + {ok, Socket} = gen_tcp:connect("localhost", Port, + [binary, {active, false}, {packet, raw}]), + ok = gen_tcp:send(Socket, Packet), + Res = case gen_tcp:recv(Socket, 0, 6000) of + {ok, << "HTTP/1.1 ", Str:24/bits, _Rest/bits >>} -> + list_to_integer(binary_to_list(Str)); + {error, Reason} -> + Reason + end, + gen_tcp:close(Socket), + {Packet, Res}. + +%% Send a raw request. Return the response code and the full response. +raw_resp(Request, Config) -> + {port, Port} = lists:keyfind(port, 1, Config), + Transport = case ?config(scheme, Config) of + "http" -> gen_tcp; + "https" -> ssl + end, + {ok, Socket} = Transport:connect("localhost", Port, + [binary, {active, false}, {packet, raw}]), + ok = Transport:send(Socket, Request), + {StatusCode, Response} = case recv_loop(Transport, Socket, <<>>) of + {ok, << "HTTP/1.1 ", Str:24/bits, _Rest/bits >> = Bin} -> + {list_to_integer(binary_to_list(Str)), Bin}; + {ok, Bin} -> + {badresp, Bin}; + {error, Reason} -> + {Reason, <<>>} + end, + Transport:close(Socket), + {Response, StatusCode}. + +recv_loop(Transport, Socket, Acc) -> + case Transport:recv(Socket, 0, 6000) of + {ok, Data} -> + recv_loop(Transport, Socket, <>); + {error, closed} -> + ok = Transport:close(Socket), + {ok, Acc}; + {error, Reason} -> + {error, Reason} + end. + + + +raw(Config) -> + Huge = [$0 || _N <- lists:seq(1, 5000)], + Tests = [ + {"\r\n\r\n\r\n\r\n\r\nGET / HTTP/1.1\r\nHost: localhost\r\n\r\n", 200}, + {"\n", 400}, + {"Garbage\r\n\r\n", 400}, + {"\r\n\r\n\r\n\r\n\r\n\r\n", 400}, + {"GET / HTTP/1.1\r\nHost: dev-extend.eu\r\n\r\n", 400}, + {"", closed}, + {"\r\n", closed}, + {"\r\n\r\n", closed}, + {"GET / HTTP/1.1", closed}, + {"GET / HTTP/1.1\r\n", 408}, + {"GET / HTTP/1.1\r\nHost: localhost", 408}, + {"GET / HTTP/1.1\r\nHost: localhost\r\n", 408}, + {"GET / HTTP/1.1\r\nHost: localhost\r\n\r", 408}, + {"GET http://localhost/ HTTP/1.1\r\n\r\n", 501}, + {"GET / HTTP/1.2\r\nHost: localhost\r\n\r\n", 505}, + {"GET /init_shutdown HTTP/1.1\r\nHost: localhost\r\n\r\n", 666}, + {"GET /long_polling HTTP/1.1\r\nHost: localhost\r\n\r\n", 102}, + {Huge, 413}, + {"GET / HTTP/1.1\r\n" ++ Huge, 413} + ], + [{Packet, StatusCode} = raw_req(Packet, Config) + || {Packet, StatusCode} <- Tests]. + +set_resp_header(Config) -> + {port, Port} = lists:keyfind(port, 1, Config), + {ok, Socket} = gen_tcp:connect("localhost", Port, + [binary, {active, false}, {packet, raw}]), + ok = gen_tcp:send(Socket, "GET /set_resp/header HTTP/1.1\r\n" + "Host: localhost\r\nConnection: close\r\n\r\n"), + {ok, Data} = gen_tcp:recv(Socket, 0, 6000), + {_, _} = binary:match(Data, <<"Vary: Accept">>), + {_, _} = binary:match(Data, <<"Set-Cookie: ">>). + +set_resp_overwrite(Config) -> + {port, Port} = lists:keyfind(port, 1, Config), + {ok, Socket} = gen_tcp:connect("localhost", Port, + [binary, {active, false}, {packet, raw}]), + ok = gen_tcp:send(Socket, "GET /set_resp/overwrite HTTP/1.1\r\n" + "Host: localhost\r\nConnection: close\r\n\r\n"), + {ok, Data} = gen_tcp:recv(Socket, 0, 6000), + {_Start, _Length} = binary:match(Data, <<"Server: DesireDrive/1.0">>). + +set_resp_body(Config) -> + {port, Port} = lists:keyfind(port, 1, Config), + {ok, Socket} = gen_tcp:connect("localhost", Port, + [binary, {active, false}, {packet, raw}]), + ok = gen_tcp:send(Socket, "GET /set_resp/body HTTP/1.1\r\n" + "Host: localhost\r\nConnection: close\r\n\r\n"), + {ok, Data} = gen_tcp:recv(Socket, 0, 6000), + {_Start, _Length} = binary:match(Data, <<"\r\n\r\n" + "A flameless dance does not equal a cycle">>). + +response_as_req(Config) -> + Packet = +"HTTP/1.0 302 Found +Location: http://www.google.co.il/ +Cache-Control: private +Content-Type: text/html; charset=UTF-8 +Set-Cookie: PREF=ID=568f67013d4a7afa:FF=0:TM=1323014101:LM=1323014101:S=XqctDWC65MzKT0zC; expires=Tue, 03-Dec-2013 15:55:01 GMT; path=/; domain=.google.com +Date: Sun, 04 Dec 2011 15:55:01 GMT +Server: gws +Content-Length: 221 +X-XSS-Protection: 1; mode=block +X-Frame-Options: SAMEORIGIN + + +302 Moved +

302 Moved

+The document has moved +here. +", + {Packet, 400} = raw_req(Packet, Config). + +stream_body_set_resp(Config) -> + {Packet, 200} = raw_resp( + "GET /stream_body/set_resp HTTP/1.1\r\n" + "Host: localhost\r\nConnection: close\r\n\r\n", Config), + {_Start, _Length} = binary:match(Packet, <<"stream_body_set_resp">>). + +static_mimetypes_function(Config) -> + TestURL = build_url("/static_mimetypes_function/test.html", Config), + {ok, {{"HTTP/1.1", 200, "OK"}, Headers1, "test.html\n"}} = + httpc:request(TestURL), + "text/html" = ?config("content-type", Headers1). + +handler_errors(Config) -> + Request = fun(Case) -> + raw_resp(["GET /handler_errors?case=", Case, " HTTP/1.1\r\n", + "Host: localhost\r\n\r\n"], Config) end, + + {_Packet1, 500} = Request("init_before_reply"), + + {Packet2, 200} = Request("init_after_reply"), + nomatch = binary:match(Packet2, <<"HTTP/1.1 500">>), + + {Packet3, 200} = Request("init_reply_handle_error"), + nomatch = binary:match(Packet3, <<"HTTP/1.1 500">>), + + {_Packet4, 500} = Request("handle_before_reply"), + + {Packet5, 200} = Request("handle_after_reply"), + nomatch = binary:match(Packet5, <<"HTTP/1.1 500">>), + + {Packet6, 200} = raw_resp([ + "GET / HTTP/1.1\r\n", + "Host: localhost\r\n", + "Connection: keep-alive\r\n\r\n", + "GET /handler_errors?case=handle_after_reply\r\n", + "Host: localhost\r\n\r\n"], Config), + nomatch = binary:match(Packet6, <<"HTTP/1.1 500">>), + + {Packet7, 200} = raw_resp([ + "GET / HTTP/1.1\r\n", + "Host: localhost\r\n", + "Connection: keep-alive\r\n\r\n", + "GET /handler_errors?case=handle_before_reply HTTP/1.1\r\n", + "Host: localhost\r\n\r\n"], Config), + {{_, _}, _} = {binary:match(Packet7, <<"HTTP/1.1 500">>), Packet7}, + + done. + +static_attribute_etag(Config) -> + TestURL = build_url("/static_attribute_etag/test.html", Config), + {ok, {{"HTTP/1.1", 200, "OK"}, Headers1, "test.html\n"}} = + httpc:request(TestURL), + false = ?config("etag", Headers1) =:= undefined, + {ok, {{"HTTP/1.1", 200, "OK"}, Headers2, "test.html\n"}} = + httpc:request(TestURL), + true = ?config("etag", Headers1) =:= ?config("etag", Headers2). + +static_function_etag(Config) -> + TestURL = build_url("/static_function_etag/test.html", Config), + {ok, {{"HTTP/1.1", 200, "OK"}, Headers1, "test.html\n"}} = + httpc:request(TestURL), + false = ?config("etag", Headers1) =:= undefined, + {ok, {{"HTTP/1.1", 200, "OK"}, Headers2, "test.html\n"}} = + httpc:request(TestURL), + true = ?config("etag", Headers1) =:= ?config("etag", Headers2). + +static_function_etag(Arguments, etag_data) -> + {_, Filepath} = lists:keyfind(filepath, 1, Arguments), + {_, _Filesize} = lists:keyfind(filesize, 1, Arguments), + {_, _INode} = lists:keyfind(inode, 1, Arguments), + {_, _Modified} = lists:keyfind(mtime, 1, Arguments), + ChecksumCommand = lists:flatten(io_lib:format("sha1sum ~s", [Filepath])), + [Checksum|_] = string:tokens(os:cmd(ChecksumCommand), " "), + iolist_to_binary(Checksum). + +%% http and https. + +build_url(Path, Config) -> + {scheme, Scheme} = lists:keyfind(scheme, 1, Config), + {port, Port} = lists:keyfind(port, 1, Config), + Scheme ++ "://localhost:" ++ integer_to_list(Port) ++ Path. + +http_200(Config) -> + {ok, {{"HTTP/1.1", 200, "OK"}, _Headers, "http_handler"}} = + httpc:request(build_url("/", Config)). + +http_404(Config) -> + {ok, {{"HTTP/1.1", 404, "Not Found"}, _Headers, _Body}} = + httpc:request(build_url("/not/found", Config)). + +file_200(Config) -> + {ok, {{"HTTP/1.1", 200, "OK"}, Headers, "test_file\n"}} = + httpc:request(build_url("/static/test_file", Config)), + "application/octet-stream" = ?config("content-type", Headers), + + {ok, {{"HTTP/1.1", 200, "OK"}, Headers1, "test_file.css\n"}} = + httpc:request(build_url("/static/test_file.css", Config)), + "text/css" = ?config("content-type", Headers1). + +file_403(Config) -> + {ok, {{"HTTP/1.1", 403, "Forbidden"}, _Headers, _Body}} = + httpc:request(build_url("/static/test_noread", Config)). + +dir_403(Config) -> + {ok, {{"HTTP/1.1", 403, "Forbidden"}, _Headers, _Body}} = + httpc:request(build_url("/static/test_dir", Config)), + {ok, {{"HTTP/1.1", 403, "Forbidden"}, _Headers, _Body}} = + httpc:request(build_url("/static/test_dir/", Config)). + +file_404(Config) -> + {ok, {{"HTTP/1.1", 404, "Not Found"}, _Headers, _Body}} = + httpc:request(build_url("/static/not_found", Config)). + +file_400(Config) -> + {ok, {{"HTTP/1.1", 400, "Bad Request"}, _Headers, _Body}} = + httpc:request(build_url("/static/%2f", Config)), + {ok, {{"HTTP/1.1", 400, "Bad Request"}, _Headers1, _Body1}} = + httpc:request(build_url("/static/%2e", Config)), + {ok, {{"HTTP/1.1", 400, "Bad Request"}, _Headers2, _Body2}} = + httpc:request(build_url("/static/%2e%2e", Config)). +%% misc. + +http_10_hostless(Config) -> + Packet = "GET / HTTP/1.0\r\n\r\n", + {Packet, 200} = raw_req(Packet, Config). + +%% rest. + +rest_simple(Config) -> + Packet = "GET /simple HTTP/1.1\r\nHost: localhost\r\n\r\n", + {Packet, 200} = raw_req(Packet, Config). + +rest_keepalive(Config) -> + {port, Port} = lists:keyfind(port, 1, Config), + {ok, Socket} = gen_tcp:connect("localhost", Port, + [binary, {active, false}, {packet, raw}]), + ok = rest_keepalive_loop(Socket, 100), + ok = gen_tcp:close(Socket). + +rest_keepalive_loop(_Socket, 0) -> + ok; +rest_keepalive_loop(Socket, N) -> + ok = gen_tcp:send(Socket, "GET /simple HTTP/1.1\r\n" + "Host: localhost\r\nConnection: keep-alive\r\n\r\n"), + {ok, Data} = gen_tcp:recv(Socket, 0, 6000), + {0, 12} = binary:match(Data, <<"HTTP/1.1 200">>), + nomatch = binary:match(Data, <<"Connection: close">>), + rest_keepalive_loop(Socket, N - 1). + +rest_keepalive_post(Config) -> + {port, Port} = lists:keyfind(port, 1, Config), + {ok, Socket} = gen_tcp:connect("localhost", Port, + [binary, {active, false}, {packet, raw}]), + ok = rest_keepalive_post_loop(Socket, 10, forbidden_post), + ok = gen_tcp:close(Socket). + +rest_keepalive_post_loop(_Socket, 0, _) -> + ok; +rest_keepalive_post_loop(Socket, N, simple_post) -> + ok = gen_tcp:send(Socket, "POST /simple_post HTTP/1.1\r\n" + "Host: localhost\r\nConnection: keep-alive\r\n" + "Content-Length: 5\r\nContent-Type: text/plain\r\n\r\n12345"), + {ok, Data} = gen_tcp:recv(Socket, 0, 6000), + {0, 12} = binary:match(Data, <<"HTTP/1.1 303">>), + nomatch = binary:match(Data, <<"Connection: close">>), + rest_keepalive_post_loop(Socket, N - 1, forbidden_post); +rest_keepalive_post_loop(Socket, N, forbidden_post) -> + ok = gen_tcp:send(Socket, "POST /forbidden_post HTTP/1.1\r\n" + "Host: localhost\r\nConnection: keep-alive\r\n" + "Content-Length: 5\r\nContent-Type: text/plain\r\n\r\n12345"), + {ok, Data} = gen_tcp:recv(Socket, 0, 6000), + {0, 12} = binary:match(Data, <<"HTTP/1.1 403">>), + nomatch = binary:match(Data, <<"Connection: close">>), + rest_keepalive_post_loop(Socket, N - 1, simple_post). diff --git a/rabbitmq-server/plugins-src/cowboy-wrapper/cowboy-git/test/http_SUITE_data/cert.pem b/rabbitmq-server/plugins-src/cowboy-wrapper/cowboy-git/test/http_SUITE_data/cert.pem new file mode 100644 index 0000000..a772007 --- /dev/null +++ b/rabbitmq-server/plugins-src/cowboy-wrapper/cowboy-git/test/http_SUITE_data/cert.pem @@ -0,0 +1,14 @@ +-----BEGIN CERTIFICATE----- +MIICKTCCAZICCQCl9gdHk5NqUjANBgkqhkiG9w0BAQUFADBZMQswCQYDVQQGEwJB +VTETMBEGA1UECAwKU29tZS1TdGF0ZTEhMB8GA1UECgwYSW50ZXJuZXQgV2lkZ2l0 +cyBQdHkgTHRkMRIwEAYDVQQDDAlsb2NhbGhvc3QwHhcNMTEwNDA4MTMxNTE3WhcN +MTEwNTA4MTMxNTE3WjBZMQswCQYDVQQGEwJBVTETMBEGA1UECAwKU29tZS1TdGF0 +ZTEhMB8GA1UECgwYSW50ZXJuZXQgV2lkZ2l0cyBQdHkgTHRkMRIwEAYDVQQDDAls +b2NhbGhvc3QwgZ8wDQYJKoZIhvcNAQEBBQADgY0AMIGJAoGBAOjgFPS0dP4d8F1e +bNJPB+kAjM2FyTZGmkFCLUYONTPrdGOUIHL/UOGtU22BQzlskE+a6/j2Kg72tm8x +4X7yf+6s7CdRe086idNx9+GymZ64ZTnly33rD3AJffbBeWHwT2e9fuBeFk9WGC8v +kqECFZyqf7+znS0o48oBNcx3ePB5AgMBAAEwDQYJKoZIhvcNAQEFBQADgYEASTkv +oHuZyO8DgT8bIE6W3yM2fvlNshkhh7Thgpf32qQoVOxRU9EF0KpuJCCAHQHQNQlI +nf9Zc4UzOrLhxZBGocNhkkn4WLw2ysto/7+/+9xHah0M0l4auHLQagVLCoOsHUn2 +JX+A2NrbvuX5wnUrZGOdgY70tvMBeU/xLtp3af8= +-----END CERTIFICATE----- diff --git a/rabbitmq-server/plugins-src/cowboy-wrapper/cowboy-git/test/http_SUITE_data/key.pem b/rabbitmq-server/plugins-src/cowboy-wrapper/cowboy-git/test/http_SUITE_data/key.pem new file mode 100644 index 0000000..0b699cc --- /dev/null +++ b/rabbitmq-server/plugins-src/cowboy-wrapper/cowboy-git/test/http_SUITE_data/key.pem @@ -0,0 +1,18 @@ +-----BEGIN RSA PRIVATE KEY----- +Proc-Type: 4,ENCRYPTED +DEK-Info: DES-EDE3-CBC,F11262DB77BB804C + +jOJ+ft/dihIxz7CTuuK47fCTGdX7xMLANmA7mRg8y9OYhNZQiCz5GjcWLqe0NNl5 +qXPW0uvT/9B5O9o21Y2i/CKU1BqRLuXHXDsjHg7RGaSH6wIavWt+lR+I1sjieFbX +VByK1KHXjEU704DEILKJIA9gVzoYAgMzo+FTw2e/2jusXntxk8HXyF5zKTzjHBtI +NQGweJqTmfZjX3SgPP4Co/ShrA6fUG0uTp1HwbByJnwtAeT3xWJrAD4QSn7+qrlv +3qmEIqVXsvLrfZRY1WZ4uIsbLK8wkvxboSIoIK55VV9R2zRbwQULon6QJwKYujAr +J2WUYkHHQOMpaAzUmalaT+8GUt8/A1oSK4BdiSZywsMMm46/hDadXBzFg+dPL5g2 +Td+7/L0S6tUVWq4+YBp5EalZH6VQ4cqPYDJZUZ9xt6+yY7V5748lSdA7cHCROnbG +bKbSW9WbF7MPDHCjvCAfq+s1dafHJgyIOlMg2bm7V8eHWAA0xKQ/o7i5EyEyaKYR +UXGeAf+KfXcclEZ77v2RCXZvd6ceWkifm59qWv/3TCYaHiS2Aa3lVToMKTwYzzXQ +p5X5os6wv3IAi2nGyAIOoSDisdHmFteZNXNQsw0n3XCAYfsNMk+r5/r5YqDffURH +c8SMOCP4BIPoZ/abi/gnEntGqsx1YALg0aosHwHGDJ/l+QJC6u6PZk310YzRw4GL +K9+wscFgEub2OO+R83Vkfesj4tYzgOjab7+92a/soHdW0zhGejlvehODOgNZ6NUG +MPQlT+qpF9Jh5IThYXupXXFzJzQe3O/qVXy89m69JGa+AWRvbu+M/A== +-----END RSA PRIVATE KEY----- diff --git a/rabbitmq-server/plugins-src/cowboy-wrapper/cowboy-git/test/http_handler.erl b/rabbitmq-server/plugins-src/cowboy-wrapper/cowboy-git/test/http_handler.erl new file mode 100644 index 0000000..76a85d4 --- /dev/null +++ b/rabbitmq-server/plugins-src/cowboy-wrapper/cowboy-git/test/http_handler.erl @@ -0,0 +1,19 @@ +%% Feel free to use, reuse and abuse the code in this file. + +-module(http_handler). +-behaviour(cowboy_http_handler). +-export([init/3, handle/2, terminate/2]). + +-record(state, {headers, body}). + +init({_Transport, http}, Req, Opts) -> + Headers = proplists:get_value(headers, Opts, []), + Body = proplists:get_value(body, Opts, "http_handler"), + {ok, Req, #state{headers=Headers, body=Body}}. + +handle(Req, State=#state{headers=Headers, body=Body}) -> + {ok, Req2} = cowboy_http_req:reply(200, Headers, Body, Req), + {ok, Req2, State}. + +terminate(_Req, _State) -> + ok. diff --git a/rabbitmq-server/plugins-src/cowboy-wrapper/cowboy-git/test/http_handler_errors.erl b/rabbitmq-server/plugins-src/cowboy-wrapper/cowboy-git/test/http_handler_errors.erl new file mode 100644 index 0000000..1c23207 --- /dev/null +++ b/rabbitmq-server/plugins-src/cowboy-wrapper/cowboy-git/test/http_handler_errors.erl @@ -0,0 +1,40 @@ +%% Feel free to use, reuse and abuse the code in this file. + +-module(http_handler_errors). +-behaviour(cowboy_http_handler). +-export([init/3, handle/2, terminate/2]). + +init({_Transport, http}, Req, _Opts) -> + {Case, Req1} = cowboy_http_req:qs_val(<<"case">>, Req), + case_init(Case, Req1). + +case_init(<<"init_before_reply">> = Case, _Req) -> + erlang:error(Case); + +case_init(<<"init_after_reply">> = Case, Req) -> + {ok, _Req1} = cowboy_http_req:reply(200, [], "http_handler_crashes", Req), + erlang:error(Case); + +case_init(<<"init_reply_handle_error">> = Case, Req) -> + {ok, Req1} = cowboy_http_req:reply(200, [], "http_handler_crashes", Req), + {ok, Req1, Case}; + +case_init(<<"handle_before_reply">> = Case, Req) -> + {ok, Req, Case}; + +case_init(<<"handle_after_reply">> = Case, Req) -> + {ok, Req, Case}. + + +handle(_Req, <<"init_reply_handle_error">> = Case) -> + erlang:error(Case); + +handle(_Req, <<"handle_before_reply">> = Case) -> + erlang:error(Case); + +handle(Req, <<"handle_after_reply">> = Case) -> + {ok, _Req1} = cowboy_http_req:reply(200, [], "http_handler_crashes", Req), + erlang:error(Case). + +terminate(_Req, _State) -> + ok. diff --git a/rabbitmq-server/plugins-src/cowboy-wrapper/cowboy-git/test/http_handler_init_shutdown.erl b/rabbitmq-server/plugins-src/cowboy-wrapper/cowboy-git/test/http_handler_init_shutdown.erl new file mode 100644 index 0000000..ac63b44 --- /dev/null +++ b/rabbitmq-server/plugins-src/cowboy-wrapper/cowboy-git/test/http_handler_init_shutdown.erl @@ -0,0 +1,17 @@ +%% Feel free to use, reuse and abuse the code in this file. + +-module(http_handler_init_shutdown). +-behaviour(cowboy_http_handler). +-export([init/3, handle/2, terminate/2]). + +init({_Transport, http}, Req, _Opts) -> + {ok, Req2} = cowboy_http_req:reply(<<"666 Init Shutdown Testing">>, + [{'Connection', <<"close">>}], Req), + {shutdown, Req2, undefined}. + +handle(Req, State) -> + {ok, Req2} = cowboy_http_req:reply(200, [], "Hello world!", Req), + {ok, Req2, State}. + +terminate(_Req, _State) -> + ok. diff --git a/rabbitmq-server/plugins-src/cowboy-wrapper/cowboy-git/test/http_handler_long_polling.erl b/rabbitmq-server/plugins-src/cowboy-wrapper/cowboy-git/test/http_handler_long_polling.erl new file mode 100644 index 0000000..e838619 --- /dev/null +++ b/rabbitmq-server/plugins-src/cowboy-wrapper/cowboy-git/test/http_handler_long_polling.erl @@ -0,0 +1,22 @@ +%% Feel free to use, reuse and abuse the code in this file. + +-module(http_handler_long_polling). +-behaviour(cowboy_http_handler). +-export([init/3, handle/2, info/3, terminate/2]). + +init({_Transport, http}, Req, _Opts) -> + erlang:send_after(500, self(), timeout), + {loop, Req, 9, 5000, hibernate}. + +handle(_Req, _State) -> + exit(badarg). + +info(timeout, Req, 0) -> + {ok, Req2} = cowboy_http_req:reply(102, Req), + {ok, Req2, 0}; +info(timeout, Req, State) -> + erlang:send_after(500, self(), timeout), + {loop, Req, State - 1, hibernate}. + +terminate(_Req, _State) -> + ok. diff --git a/rabbitmq-server/plugins-src/cowboy-wrapper/cowboy-git/test/http_handler_multipart.erl b/rabbitmq-server/plugins-src/cowboy-wrapper/cowboy-git/test/http_handler_multipart.erl new file mode 100644 index 0000000..f5f7919 --- /dev/null +++ b/rabbitmq-server/plugins-src/cowboy-wrapper/cowboy-git/test/http_handler_multipart.erl @@ -0,0 +1,29 @@ +%% Feel free to use, reuse and abuse the code in this file. + +-module(http_handler_multipart). +-behaviour(cowboy_http_handler). +-export([init/3, handle/2, terminate/2]). + +init({_Transport, http}, Req, []) -> + {ok, Req, {}}. + +handle(Req, State) -> + {Result, Req2} = acc_multipart(Req, []), + {ok, Req3} = cowboy_http_req:reply(200, [], term_to_binary(Result), Req2), + {ok, Req3, State}. + +terminate(_Req, _State) -> + ok. + +acc_multipart(Req, Acc) -> + {Result, Req2} = cowboy_http_req:multipart_data(Req), + acc_multipart(Req2, Acc, Result). + +acc_multipart(Req, Acc, {headers, Headers}) -> + acc_multipart(Req, [{Headers, []}|Acc]); +acc_multipart(Req, [{Headers, BodyAcc}|Acc], {body, Data}) -> + acc_multipart(Req, [{Headers, [Data|BodyAcc]}|Acc]); +acc_multipart(Req, [{Headers, BodyAcc}|Acc], end_of_part) -> + acc_multipart(Req, [{Headers, list_to_binary(lists:reverse(BodyAcc))}|Acc]); +acc_multipart(Req, Acc, eof) -> + {lists:reverse(Acc), Req}. diff --git a/rabbitmq-server/plugins-src/cowboy-wrapper/cowboy-git/test/http_handler_set_resp.erl b/rabbitmq-server/plugins-src/cowboy-wrapper/cowboy-git/test/http_handler_set_resp.erl new file mode 100644 index 0000000..83d48c0 --- /dev/null +++ b/rabbitmq-server/plugins-src/cowboy-wrapper/cowboy-git/test/http_handler_set_resp.erl @@ -0,0 +1,33 @@ +%% Feel free to use, reuse and abuse the code in this file. + +-module(http_handler_set_resp). +-behaviour(cowboy_http_handler). +-export([init/3, handle/2, terminate/2]). + +init({_Transport, http}, Req, Opts) -> + Headers = proplists:get_value(headers, Opts, []), + Body = proplists:get_value(body, Opts, <<"http_handler_set_resp">>), + {ok, Req2} = lists:foldl(fun({Name, Value}, {ok, R}) -> + cowboy_http_req:set_resp_header(Name, Value, R) + end, {ok, Req}, Headers), + {ok, Req3} = cowboy_http_req:set_resp_body(Body, Req2), + {ok, Req4} = cowboy_http_req:set_resp_header( + <<"X-Cowboy-Test">>, <<"ok">>, Req3), + {ok, Req5} = cowboy_http_req:set_resp_cookie( + <<"cake">>, <<"lie">>, [], Req4), + {ok, Req5, undefined}. + +handle(Req, State) -> + case cowboy_http_req:has_resp_header(<<"X-Cowboy-Test">>, Req) of + false -> {ok, Req, State}; + true -> + case cowboy_http_req:has_resp_body(Req) of + false -> {ok, Req, State}; + true -> + {ok, Req2} = cowboy_http_req:reply(200, Req), + {ok, Req2, State} + end + end. + +terminate(_Req, _State) -> + ok. diff --git a/rabbitmq-server/plugins-src/cowboy-wrapper/cowboy-git/test/http_handler_stream_body.erl b/rabbitmq-server/plugins-src/cowboy-wrapper/cowboy-git/test/http_handler_stream_body.erl new file mode 100644 index 0000000..c90f746 --- /dev/null +++ b/rabbitmq-server/plugins-src/cowboy-wrapper/cowboy-git/test/http_handler_stream_body.erl @@ -0,0 +1,24 @@ +%% Feel free to use, reuse and abuse the code in this file. + +-module(http_handler_stream_body). +-behaviour(cowboy_http_handler). +-export([init/3, handle/2, terminate/2]). + +-record(state, {headers, body, reply}). + +init({_Transport, http}, Req, Opts) -> + Headers = proplists:get_value(headers, Opts, []), + Body = proplists:get_value(body, Opts, "http_handler_stream_body"), + Reply = proplists:get_value(reply, Opts), + {ok, Req, #state{headers=Headers, body=Body, reply=Reply}}. + +handle(Req, State=#state{headers=_Headers, body=Body, reply=set_resp}) -> + {ok, Transport, Socket} = cowboy_http_req:transport(Req), + SFun = fun() -> Transport:send(Socket, Body), sent end, + SLen = iolist_size(Body), + {ok, Req2} = cowboy_http_req:set_resp_body_fun(SLen, SFun, Req), + {ok, Req3} = cowboy_http_req:reply(200, Req2), + {ok, Req3, State}. + +terminate(_Req, _State) -> + ok. diff --git a/rabbitmq-server/plugins-src/cowboy-wrapper/cowboy-git/test/proper_SUITE.erl b/rabbitmq-server/plugins-src/cowboy-wrapper/cowboy-git/test/proper_SUITE.erl new file mode 100644 index 0000000..440aa5f --- /dev/null +++ b/rabbitmq-server/plugins-src/cowboy-wrapper/cowboy-git/test/proper_SUITE.erl @@ -0,0 +1,37 @@ +%% Copyright (c) 2011, Loïc Hoguin +%% +%% Permission to use, copy, modify, and/or distribute this software for any +%% purpose with or without fee is hereby granted, provided that the above +%% copyright notice and this permission notice appear in all copies. +%% +%% THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES +%% WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF +%% MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR +%% ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES +%% WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN +%% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF +%% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + +-module(proper_SUITE). + +-include_lib("common_test/include/ct.hrl"). + +-export([all/0, groups/0]). %% ct. +-export([dispatcher_split_host/1]). %% cowboy_dispatcher. + +%% ct. + +all() -> + [{group, dispatcher}]. + +groups() -> + [{dispatcher, [], [dispatcher_split_host]}]. + +%% cowboy_dispatcher. + +dispatcher_split_host(_Config) -> + true = proper:quickcheck(dispatcher_prop:prop_split_host_symmetric(), + [{on_output, fun(Format, Data) -> + io:format(user, Format, Data), %% Console. + io:format(Format, Data) %% Logs. + end}]). diff --git a/rabbitmq-server/plugins-src/cowboy-wrapper/cowboy-git/test/rest_forbidden_resource.erl b/rabbitmq-server/plugins-src/cowboy-wrapper/cowboy-git/test/rest_forbidden_resource.erl new file mode 100644 index 0000000..90dee84 --- /dev/null +++ b/rabbitmq-server/plugins-src/cowboy-wrapper/cowboy-git/test/rest_forbidden_resource.erl @@ -0,0 +1,40 @@ +-module(rest_forbidden_resource). +-export([init/3, rest_init/2, allowed_methods/2, forbidden/2, + content_types_provided/2, content_types_accepted/2, + post_is_create/2, create_path/2, to_text/2, from_text/2]). + +init(_Transport, _Req, _Opts) -> + {upgrade, protocol, cowboy_http_rest}. + +rest_init(Req, [Forbidden]) -> + {ok, Req, Forbidden}. + +allowed_methods(Req, State) -> + {['GET', 'HEAD', 'POST'], Req, State}. + +forbidden(Req, State=true) -> + {true, Req, State}; +forbidden(Req, State=false) -> + {false, Req, State}. + +content_types_provided(Req, State) -> + {[{{<<"text">>, <<"plain">>, []}, to_text}], Req, State}. + +content_types_accepted(Req, State) -> + {[{{<<"text">>, <<"plain">>, []}, from_text}], Req, State}. + +post_is_create(Req, State) -> + {true, Req, State}. + +create_path(Req, State) -> + {Path, Req2} = cowboy_http_req:raw_path(Req), + {Path, Req2, State}. + +to_text(Req, State) -> + {<<"This is REST!">>, Req, State}. + +from_text(Req, State) -> + {true, Req, State}. + + + diff --git a/rabbitmq-server/plugins-src/cowboy-wrapper/cowboy-git/test/rest_simple_resource.erl b/rabbitmq-server/plugins-src/cowboy-wrapper/cowboy-git/test/rest_simple_resource.erl new file mode 100644 index 0000000..e2c573c --- /dev/null +++ b/rabbitmq-server/plugins-src/cowboy-wrapper/cowboy-git/test/rest_simple_resource.erl @@ -0,0 +1,12 @@ +-module(rest_simple_resource). +-export([init/3, content_types_provided/2, get_text_plain/2]). + +init(_Transport, _Req, _Opts) -> + {upgrade, protocol, cowboy_http_rest}. + +content_types_provided(Req, State) -> + {[{{<<"text">>, <<"plain">>, []}, get_text_plain}], Req, State}. + +get_text_plain(Req, State) -> + {<<"This is REST!">>, Req, State}. + diff --git a/rabbitmq-server/plugins-src/cowboy-wrapper/cowboy-git/test/websocket_handler.erl b/rabbitmq-server/plugins-src/cowboy-wrapper/cowboy-git/test/websocket_handler.erl new file mode 100644 index 0000000..abb4967 --- /dev/null +++ b/rabbitmq-server/plugins-src/cowboy-wrapper/cowboy-git/test/websocket_handler.erl @@ -0,0 +1,38 @@ +%% Feel free to use, reuse and abuse the code in this file. + +-module(websocket_handler). +-behaviour(cowboy_http_handler). +-behaviour(cowboy_http_websocket_handler). +-export([init/3, handle/2, terminate/2]). +-export([websocket_init/3, websocket_handle/3, + websocket_info/3, websocket_terminate/3]). + +init(_Any, _Req, _Opts) -> + {upgrade, protocol, cowboy_http_websocket}. + +handle(_Req, _State) -> + exit(badarg). + +terminate(_Req, _State) -> + exit(badarg). + +websocket_init(_TransportName, Req, _Opts) -> + erlang:start_timer(1000, self(), <<"websocket_init">>), + Req2 = cowboy_http_req:compact(Req), + {ok, Req2, undefined}. + +websocket_handle({text, Data}, Req, State) -> + {reply, {text, Data}, Req, State}; +websocket_handle({binary, Data}, Req, State) -> + {reply, {binary, Data}, Req, State}; +websocket_handle(_Frame, Req, State) -> + {ok, Req, State}. + +websocket_info({timeout, _Ref, Msg}, Req, State) -> + erlang:start_timer(1000, self(), <<"websocket_handle">>), + {reply, {text, Msg}, Req, State}; +websocket_info(_Info, Req, State) -> + {ok, Req, State}. + +websocket_terminate(_Reason, _Req, _State) -> + ok. diff --git a/rabbitmq-server/plugins-src/cowboy-wrapper/cowboy-git/test/websocket_handler_init_shutdown.erl b/rabbitmq-server/plugins-src/cowboy-wrapper/cowboy-git/test/websocket_handler_init_shutdown.erl new file mode 100644 index 0000000..aa9e056 --- /dev/null +++ b/rabbitmq-server/plugins-src/cowboy-wrapper/cowboy-git/test/websocket_handler_init_shutdown.erl @@ -0,0 +1,30 @@ +%% Feel free to use, reuse and abuse the code in this file. + +-module(websocket_handler_init_shutdown). +-behaviour(cowboy_http_handler). +-behaviour(cowboy_http_websocket_handler). +-export([init/3, handle/2, terminate/2]). +-export([websocket_init/3, websocket_handle/3, + websocket_info/3, websocket_terminate/3]). + +init(_Any, _Req, _Opts) -> + {upgrade, protocol, cowboy_http_websocket}. + +handle(_Req, _State) -> + exit(badarg). + +terminate(_Req, _State) -> + exit(badarg). + +websocket_init(_TransportName, Req, _Opts) -> + {ok, Req2} = cowboy_http_req:reply(403, Req), + {shutdown, Req2}. + +websocket_handle(_Frame, _Req, _State) -> + exit(badarg). + +websocket_info(_Info, _Req, _State) -> + exit(badarg). + +websocket_terminate(_Reason, _Req, _State) -> + exit(badarg). diff --git a/rabbitmq-server/plugins-src/cowboy-wrapper/cowboy-git/test/ws_SUITE.erl b/rabbitmq-server/plugins-src/cowboy-wrapper/cowboy-git/test/ws_SUITE.erl new file mode 100644 index 0000000..136833f --- /dev/null +++ b/rabbitmq-server/plugins-src/cowboy-wrapper/cowboy-git/test/ws_SUITE.erl @@ -0,0 +1,318 @@ +%% Copyright (c) 2011, Loïc Hoguin +%% +%% Permission to use, copy, modify, and/or distribute this software for any +%% purpose with or without fee is hereby granted, provided that the above +%% copyright notice and this permission notice appear in all copies. +%% +%% THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES +%% WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF +%% MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR +%% ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES +%% WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN +%% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF +%% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + +-module(ws_SUITE). + +-include_lib("common_test/include/ct.hrl"). + +-export([all/0, groups/0, init_per_suite/1, end_per_suite/1, + init_per_group/2, end_per_group/2]). %% ct. +-export([ws0/1, ws8/1, ws8_single_bytes/1, ws8_init_shutdown/1, + ws13/1, ws_timeout_hibernate/1]). %% ws. + +%% ct. + +all() -> + [{group, ws}]. + +groups() -> + BaseTests = [ws0, ws8, ws8_single_bytes, ws8_init_shutdown, ws13, + ws_timeout_hibernate], + [{ws, [], BaseTests}]. + +init_per_suite(Config) -> + application:start(inets), + application:start(cowboy), + Config. + +end_per_suite(_Config) -> + application:stop(cowboy), + application:stop(inets), + ok. + +init_per_group(ws, Config) -> + Port = 33080, + cowboy:start_listener(ws, 100, + cowboy_tcp_transport, [{port, Port}], + cowboy_http_protocol, [{dispatch, init_dispatch()}] + ), + [{port, Port}|Config]. + +end_per_group(Listener, _Config) -> + cowboy:stop_listener(Listener), + ok. + +%% Dispatch configuration. + +init_dispatch() -> + [ + {[<<"localhost">>], [ + {[<<"websocket">>], websocket_handler, []}, + {[<<"ws_timeout_hibernate">>], ws_timeout_hibernate_handler, []}, + {[<<"ws_init_shutdown">>], websocket_handler_init_shutdown, []} + ]} + ]. + +%% ws and wss. + +%% This test makes sure the code works even if we wait for a reply +%% before sending the third challenge key in the GET body. +%% +%% This ensures that Cowboy will work fine with proxies on hixie. +ws0(Config) -> + {port, Port} = lists:keyfind(port, 1, Config), + {ok, Socket} = gen_tcp:connect("localhost", Port, + [binary, {active, false}, {packet, raw}]), + ok = gen_tcp:send(Socket, + "GET /websocket HTTP/1.1\r\n" + "Host: localhost\r\n" + "Connection: Upgrade\r\n" + "Upgrade: WebSocket\r\n" + "Origin: http://localhost\r\n" + "Sec-Websocket-Key1: Y\" 4 1Lj!957b8@0H756!i\r\n" + "Sec-Websocket-Key2: 1711 M;4\\74 80<6\r\n" + "\r\n"), + {ok, Handshake} = gen_tcp:recv(Socket, 0, 6000), + {ok, {http_response, {1, 1}, 101, "WebSocket Protocol Handshake"}, Rest} + = erlang:decode_packet(http, Handshake, []), + [Headers, <<>>] = websocket_headers( + erlang:decode_packet(httph, Rest, []), []), + {'Connection', "Upgrade"} = lists:keyfind('Connection', 1, Headers), + {'Upgrade', "WebSocket"} = lists:keyfind('Upgrade', 1, Headers), + {"sec-websocket-location", "ws://localhost/websocket"} + = lists:keyfind("sec-websocket-location", 1, Headers), + {"sec-websocket-origin", "http://localhost"} + = lists:keyfind("sec-websocket-origin", 1, Headers), + ok = gen_tcp:send(Socket, <<15,245,8,18,2,204,133,33>>), + {ok, Body} = gen_tcp:recv(Socket, 0, 6000), + <<169,244,191,103,146,33,149,59,74,104,67,5,99,118,171,236>> = Body, + ok = gen_tcp:send(Socket, << 0, "client_msg", 255 >>), + {ok, << 0, "client_msg", 255 >>} = gen_tcp:recv(Socket, 0, 6000), + {ok, << 0, "websocket_init", 255 >>} = gen_tcp:recv(Socket, 0, 6000), + {ok, << 0, "websocket_handle", 255 >>} = gen_tcp:recv(Socket, 0, 6000), + {ok, << 0, "websocket_handle", 255 >>} = gen_tcp:recv(Socket, 0, 6000), + {ok, << 0, "websocket_handle", 255 >>} = gen_tcp:recv(Socket, 0, 6000), + %% We try to send another HTTP request to make sure + %% the server closed the request. + ok = gen_tcp:send(Socket, [ + << 255, 0 >>, %% Close websocket command. + "GET / HTTP/1.1\r\nHost: localhost\r\n\r\n" %% Server should ignore it. + ]), + {ok, << 255, 0 >>} = gen_tcp:recv(Socket, 0, 6000), + {error, closed} = gen_tcp:recv(Socket, 0, 6000), + ok. + +ws8(Config) -> + {port, Port} = lists:keyfind(port, 1, Config), + {ok, Socket} = gen_tcp:connect("localhost", Port, + [binary, {active, false}, {packet, raw}]), + ok = gen_tcp:send(Socket, [ + "GET /websocket HTTP/1.1\r\n" + "Host: localhost\r\n" + "Connection: Upgrade\r\n" + "Upgrade: websocket\r\n" + "Sec-WebSocket-Origin: http://localhost\r\n" + "Sec-WebSocket-Version: 8\r\n" + "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n" + "\r\n"]), + {ok, Handshake} = gen_tcp:recv(Socket, 0, 6000), + {ok, {http_response, {1, 1}, 101, "Switching Protocols"}, Rest} + = erlang:decode_packet(http, Handshake, []), + [Headers, <<>>] = websocket_headers( + erlang:decode_packet(httph, Rest, []), []), + {'Connection', "Upgrade"} = lists:keyfind('Connection', 1, Headers), + {'Upgrade', "websocket"} = lists:keyfind('Upgrade', 1, Headers), + {"sec-websocket-accept", "s3pPLMBiTxaQ9kYGzzhZRbK+xOo="} + = lists:keyfind("sec-websocket-accept", 1, Headers), + ok = gen_tcp:send(Socket, << 16#81, 16#85, 16#37, 16#fa, 16#21, 16#3d, + 16#7f, 16#9f, 16#4d, 16#51, 16#58 >>), + {ok, << 1:1, 0:3, 1:4, 0:1, 5:7, "Hello" >>} + = gen_tcp:recv(Socket, 0, 6000), + {ok, << 1:1, 0:3, 1:4, 0:1, 14:7, "websocket_init" >>} + = gen_tcp:recv(Socket, 0, 6000), + {ok, << 1:1, 0:3, 1:4, 0:1, 16:7, "websocket_handle" >>} + = gen_tcp:recv(Socket, 0, 6000), + {ok, << 1:1, 0:3, 1:4, 0:1, 16:7, "websocket_handle" >>} + = gen_tcp:recv(Socket, 0, 6000), + {ok, << 1:1, 0:3, 1:4, 0:1, 16:7, "websocket_handle" >>} + = gen_tcp:recv(Socket, 0, 6000), + ok = gen_tcp:send(Socket, << 1:1, 0:3, 9:4, 0:8 >>), %% ping + {ok, << 1:1, 0:3, 10:4, 0:8 >>} = gen_tcp:recv(Socket, 0, 6000), %% pong + ok = gen_tcp:send(Socket, << 1:1, 0:3, 8:4, 0:8 >>), %% close + {ok, << 1:1, 0:3, 8:4, 0:8 >>} = gen_tcp:recv(Socket, 0, 6000), + {error, closed} = gen_tcp:recv(Socket, 0, 6000), + ok. + +ws8_single_bytes(Config) -> + {port, Port} = lists:keyfind(port, 1, Config), + {ok, Socket} = gen_tcp:connect("localhost", Port, + [binary, {active, false}, {packet, raw}]), + ok = gen_tcp:send(Socket, [ + "GET /websocket HTTP/1.1\r\n" + "Host: localhost\r\n" + "Connection: Upgrade\r\n" + "Upgrade: websocket\r\n" + "Sec-WebSocket-Origin: http://localhost\r\n" + "Sec-WebSocket-Version: 8\r\n" + "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n" + "\r\n"]), + {ok, Handshake} = gen_tcp:recv(Socket, 0, 6000), + {ok, {http_response, {1, 1}, 101, "Switching Protocols"}, Rest} + = erlang:decode_packet(http, Handshake, []), + [Headers, <<>>] = websocket_headers( + erlang:decode_packet(httph, Rest, []), []), + {'Connection', "Upgrade"} = lists:keyfind('Connection', 1, Headers), + {'Upgrade', "websocket"} = lists:keyfind('Upgrade', 1, Headers), + {"sec-websocket-accept", "s3pPLMBiTxaQ9kYGzzhZRbK+xOo="} + = lists:keyfind("sec-websocket-accept", 1, Headers), + ok = gen_tcp:send(Socket, << 16#81 >>), %% send one byte + ok = timer:sleep(100), %% sleep for a period + ok = gen_tcp:send(Socket, << 16#85 >>), %% send another and so on + ok = timer:sleep(100), + ok = gen_tcp:send(Socket, << 16#37 >>), + ok = timer:sleep(100), + ok = gen_tcp:send(Socket, << 16#fa >>), + ok = timer:sleep(100), + ok = gen_tcp:send(Socket, << 16#21 >>), + ok = timer:sleep(100), + ok = gen_tcp:send(Socket, << 16#3d >>), + ok = timer:sleep(100), + ok = gen_tcp:send(Socket, << 16#7f >>), + ok = timer:sleep(100), + ok = gen_tcp:send(Socket, << 16#9f >>), + ok = timer:sleep(100), + ok = gen_tcp:send(Socket, << 16#4d >>), + ok = timer:sleep(100), + ok = gen_tcp:send(Socket, << 16#51 >>), + ok = timer:sleep(100), + ok = gen_tcp:send(Socket, << 16#58 >>), + {ok, << 1:1, 0:3, 1:4, 0:1, 14:7, "websocket_init" >>} + = gen_tcp:recv(Socket, 0, 6000), + {ok, << 1:1, 0:3, 1:4, 0:1, 5:7, "Hello" >>} + = gen_tcp:recv(Socket, 0, 6000), + {ok, << 1:1, 0:3, 1:4, 0:1, 16:7, "websocket_handle" >>} + = gen_tcp:recv(Socket, 0, 6000), + {ok, << 1:1, 0:3, 1:4, 0:1, 16:7, "websocket_handle" >>} + = gen_tcp:recv(Socket, 0, 6000), + {ok, << 1:1, 0:3, 1:4, 0:1, 16:7, "websocket_handle" >>} + = gen_tcp:recv(Socket, 0, 6000), + ok = gen_tcp:send(Socket, << 1:1, 0:3, 9:4, 0:8 >>), %% ping + {ok, << 1:1, 0:3, 10:4, 0:8 >>} = gen_tcp:recv(Socket, 0, 6000), %% pong + ok = gen_tcp:send(Socket, << 1:1, 0:3, 8:4, 0:8 >>), %% close + {ok, << 1:1, 0:3, 8:4, 0:8 >>} = gen_tcp:recv(Socket, 0, 6000), + {error, closed} = gen_tcp:recv(Socket, 0, 6000), + ok. + +ws_timeout_hibernate(Config) -> + {port, Port} = lists:keyfind(port, 1, Config), + {ok, Socket} = gen_tcp:connect("localhost", Port, + [binary, {active, false}, {packet, raw}]), + ok = gen_tcp:send(Socket, [ + "GET /ws_timeout_hibernate HTTP/1.1\r\n" + "Host: localhost\r\n" + "Connection: Upgrade\r\n" + "Upgrade: websocket\r\n" + "Sec-WebSocket-Origin: http://localhost\r\n" + "Sec-WebSocket-Version: 8\r\n" + "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n" + "\r\n"]), + {ok, Handshake} = gen_tcp:recv(Socket, 0, 6000), + {ok, {http_response, {1, 1}, 101, "Switching Protocols"}, Rest} + = erlang:decode_packet(http, Handshake, []), + [Headers, <<>>] = websocket_headers( + erlang:decode_packet(httph, Rest, []), []), + {'Connection', "Upgrade"} = lists:keyfind('Connection', 1, Headers), + {'Upgrade', "websocket"} = lists:keyfind('Upgrade', 1, Headers), + {"sec-websocket-accept", "s3pPLMBiTxaQ9kYGzzhZRbK+xOo="} + = lists:keyfind("sec-websocket-accept", 1, Headers), + {ok, << 1:1, 0:3, 8:4, 0:8 >>} = gen_tcp:recv(Socket, 0, 6000), + {error, closed} = gen_tcp:recv(Socket, 0, 6000), + ok. + +ws8_init_shutdown(Config) -> + {port, Port} = lists:keyfind(port, 1, Config), + {ok, Socket} = gen_tcp:connect("localhost", Port, + [binary, {active, false}, {packet, raw}]), + ok = gen_tcp:send(Socket, [ + "GET /ws_init_shutdown HTTP/1.1\r\n" + "Host: localhost\r\n" + "Connection: Upgrade\r\n" + "Upgrade: websocket\r\n" + "Sec-WebSocket-Origin: http://localhost\r\n" + "Sec-WebSocket-Version: 8\r\n" + "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n" + "\r\n"]), + {ok, Handshake} = gen_tcp:recv(Socket, 0, 6000), + {ok, {http_response, {1, 1}, 403, "Forbidden"}, _Rest} + = erlang:decode_packet(http, Handshake, []), + {error, closed} = gen_tcp:recv(Socket, 0, 6000), + ok. + +ws13(Config) -> + {port, Port} = lists:keyfind(port, 1, Config), + {ok, Socket} = gen_tcp:connect("localhost", Port, + [binary, {active, false}, {packet, raw}]), + ok = gen_tcp:send(Socket, [ + "GET /websocket HTTP/1.1\r\n" + "Host: localhost\r\n" + "Connection: Upgrade\r\n" + "Origin: http://localhost\r\n" + "Sec-WebSocket-Version: 13\r\n" + "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n" + "Upgrade: websocket\r\n" + "\r\n"]), + {ok, Handshake} = gen_tcp:recv(Socket, 0, 6000), + {ok, {http_response, {1, 1}, 101, "Switching Protocols"}, Rest} + = erlang:decode_packet(http, Handshake, []), + [Headers, <<>>] = websocket_headers( + erlang:decode_packet(httph, Rest, []), []), + {'Connection', "Upgrade"} = lists:keyfind('Connection', 1, Headers), + {'Upgrade', "websocket"} = lists:keyfind('Upgrade', 1, Headers), + {"sec-websocket-accept", "s3pPLMBiTxaQ9kYGzzhZRbK+xOo="} + = lists:keyfind("sec-websocket-accept", 1, Headers), + %% text + ok = gen_tcp:send(Socket, << 16#81, 16#85, 16#37, 16#fa, 16#21, 16#3d, + 16#7f, 16#9f, 16#4d, 16#51, 16#58 >>), + {ok, << 1:1, 0:3, 1:4, 0:1, 5:7, "Hello" >>} + = gen_tcp:recv(Socket, 0, 6000), + %% binary (empty) + ok = gen_tcp:send(Socket, << 1:1, 0:3, 2:4, 0:8 >>), + {ok, << 1:1, 0:3, 2:4, 0:8 >>} = gen_tcp:recv(Socket, 0, 6000), + %% binary + ok = gen_tcp:send(Socket, << 16#82, 16#85, 16#37, 16#fa, 16#21, 16#3d, + 16#7f, 16#9f, 16#4d, 16#51, 16#58 >>), + {ok, << 1:1, 0:3, 2:4, 0:1, 5:7, "Hello" >>} + = gen_tcp:recv(Socket, 0, 6000), + %% Receives. + {ok, << 1:1, 0:3, 1:4, 0:1, 14:7, "websocket_init" >>} + = gen_tcp:recv(Socket, 0, 6000), + {ok, << 1:1, 0:3, 1:4, 0:1, 16:7, "websocket_handle" >>} + = gen_tcp:recv(Socket, 0, 6000), + {ok, << 1:1, 0:3, 1:4, 0:1, 16:7, "websocket_handle" >>} + = gen_tcp:recv(Socket, 0, 6000), + {ok, << 1:1, 0:3, 1:4, 0:1, 16:7, "websocket_handle" >>} + = gen_tcp:recv(Socket, 0, 6000), + ok = gen_tcp:send(Socket, << 1:1, 0:3, 9:4, 0:8 >>), %% ping + {ok, << 1:1, 0:3, 10:4, 0:8 >>} = gen_tcp:recv(Socket, 0, 6000), %% pong + ok = gen_tcp:send(Socket, << 1:1, 0:3, 8:4, 0:8 >>), %% close + {ok, << 1:1, 0:3, 8:4, 0:8 >>} = gen_tcp:recv(Socket, 0, 6000), + {error, closed} = gen_tcp:recv(Socket, 0, 6000), + ok. + +websocket_headers({ok, http_eoh, Rest}, Acc) -> + [Acc, Rest]; +websocket_headers({ok, {http_header, _I, Key, _R, Value}, Rest}, Acc) -> + F = fun(S) when is_atom(S) -> S; (S) -> string:to_lower(S) end, + websocket_headers(erlang:decode_packet(httph, Rest, []), + [{F(Key), Value}|Acc]). diff --git a/rabbitmq-server/plugins-src/cowboy-wrapper/cowboy-git/test/ws_timeout_hibernate_handler.erl b/rabbitmq-server/plugins-src/cowboy-wrapper/cowboy-git/test/ws_timeout_hibernate_handler.erl new file mode 100644 index 0000000..777948a --- /dev/null +++ b/rabbitmq-server/plugins-src/cowboy-wrapper/cowboy-git/test/ws_timeout_hibernate_handler.erl @@ -0,0 +1,29 @@ +%% Feel free to use, reuse and abuse the code in this file. + +-module(ws_timeout_hibernate_handler). +-behaviour(cowboy_http_handler). +-behaviour(cowboy_http_websocket_handler). +-export([init/3, handle/2, terminate/2]). +-export([websocket_init/3, websocket_handle/3, + websocket_info/3, websocket_terminate/3]). + +init(_Any, _Req, _Opts) -> + {upgrade, protocol, cowboy_http_websocket}. + +handle(_Req, _State) -> + exit(badarg). + +terminate(_Req, _State) -> + exit(badarg). + +websocket_init(_TransportName, Req, _Opts) -> + {ok, Req, undefined, 1000, hibernate}. + +websocket_handle(_Frame, Req, State) -> + {ok, Req, State, hibernate}. + +websocket_info(_Info, Req, State) -> + {ok, Req, State, hibernate}. + +websocket_terminate(_Reason, _Req, _State) -> + ok. diff --git a/rabbitmq-server/plugins-src/cowboy-wrapper/hash.mk b/rabbitmq-server/plugins-src/cowboy-wrapper/hash.mk new file mode 100644 index 0000000..5071907 --- /dev/null +++ b/rabbitmq-server/plugins-src/cowboy-wrapper/hash.mk @@ -0,0 +1 @@ +UPSTREAM_SHORT_HASH:=4b93c2d diff --git a/rabbitmq-server/plugins-src/cowboy-wrapper/package.mk b/rabbitmq-server/plugins-src/cowboy-wrapper/package.mk new file mode 100644 index 0000000..fd29da9 --- /dev/null +++ b/rabbitmq-server/plugins-src/cowboy-wrapper/package.mk @@ -0,0 +1,24 @@ +APP_NAME:=cowboy + +UPSTREAM_GIT:=https://github.com/rabbitmq/cowboy.git +UPSTREAM_REVISION:=4b93c2d19a10e5d9cee +RETAIN_ORIGINAL_VERSION:=true +WRAPPER_PATCHES:=\ + 0001-R12-fake-iodata-type.patch \ + 0002-R12-drop-all-references-to-boolean-type.patch \ + 0003-R12-drop-all-references-to-reference-type.patch \ + 0004-R12-drop-references-to-iodata-type.patch \ + 0005-R12-drop-references-to-Default-any-type.patch \ + 0006-Use-erlang-integer_to_list-and-lists-max-instead-of-.patch \ + 0007-R12-type-definitions-must-be-ordered.patch \ + 0008-sec-websocket-protocol.patch + +# Path include/http.hrl is needed during compilation +INCLUDE_DIRS+=$(CLONE_DIR) + +ORIGINAL_APP_FILE:=$(CLONE_DIR)/src/$(APP_NAME).app.src +DO_NOT_GENERATE_APP_FILE=true + +define construct_app_commands + cp $(CLONE_DIR)/LICENSE $(APP_DIR)/LICENSE-ISC-Cowboy +endef diff --git a/rabbitmq-server/plugins-src/do-package.mk b/rabbitmq-server/plugins-src/do-package.mk new file mode 100644 index 0000000..b76c9a5 --- /dev/null +++ b/rabbitmq-server/plugins-src/do-package.mk @@ -0,0 +1,578 @@ +# This file produces the makefile fragment associated with a package. +# It includes the package's package.mk, interprets all of the +# variables that package.mk might have set, and then visits any +# dependencies of the package that have not already been visited. +# +# PACKAGE_DIR should be set to the canonical path of the package. + +# Mark that this package has been visited, so we can avoid doing it again +DONE_$(PACKAGE_DIR):=true + +# Declare the standard per-package targets +.PHONY: $(PACKAGE_DIR)+dist $(PACKAGE_DIR)+clean $(PACKAGE_DIR)+clean-recursive + +$(PACKAGE_DIR)+dist:: $(PACKAGE_DIR)/dist/.done + +$(PACKAGE_DIR)+srcdist:: $(PACKAGE_DIR)/srcdist/.done + +$(PACKAGE_DIR)+clean:: + +$(PACKAGE_DIR)+clean-with-deps:: $(PACKAGE_DIR)+clean + +# Hook into the "all package" targets used by the main public-umbrella +# makefile +all-packages:: $(PACKAGE_DIR)/dist/.done +clean-all-packages:: $(PACKAGE_DIR)+clean + +ifndef NON_INTEGRATED_$(PACKAGE_DIR) + +PACKAGE_NAME=$(notdir $(abspath $(PACKAGE_DIR))) + +# Set all the per-package vars to their default values + +# The packages upon which this package depends +DEPS:= + +# The name of the erlang application produced by the package +APP_NAME=$(call package_to_app_name,$(PACKAGE_NAME)) + +# The location of the .app file which is used as the basis for the +# .app file which goes into the .ez +ORIGINAL_APP_FILE=$(EBIN_DIR)/$(APP_NAME).app + +# The location of the source for that file (before the modules list is +# generated). Ignored if DO_NOT_GENERATE_APP_FILE is set. +ORIGINAL_APP_SOURCE=$(PACKAGE_DIR)/src/$(APP_NAME).app.src + +# Set to prevent generation of the app file. +DO_NOT_GENERATE_APP_FILE:= + +# Should the .ez files for this package, its dependencies, and its +# source distribution be included in RabbitMQ releases, and should we test +# this plugin when invoking "make test" in the umbrella? +RELEASABLE:= + +# The options to pass to erlc when compiling .erl files in this +# package +PACKAGE_ERLC_OPTS=$(ERLC_OPTS) + +# The directories containing Erlang source files +SOURCE_DIRS:=$(PACKAGE_DIR)/src + +# The Erlang source files to compile and include in the package .ez file +SOURCE_ERLS=$(strip $(foreach D,$(SOURCE_DIRS),$(wildcard $(D)/*.erl))) + +# The directories containing Erlang *.hrl files to include in the +# package .ez file. +INCLUDE_DIRS:=$(PACKAGE_DIR)/include + +# The Erlang .hrl files to include in the package .ez file. +INCLUDE_HRLS=$(strip $(foreach D,$(INCLUDE_DIRS),$(wildcard $(D)/*.hrl))) + +# The location of the directory containing the .app file. This is +# also where the .beam files produced by compiling SOURCE_ERLS will +# go. +EBIN_DIR:=$(PACKAGE_DIR)/ebin + +# The .beam files for the application. +EBIN_BEAMS=$(patsubst %,$(EBIN_DIR)/%.beam,$(notdir $(basename $(SOURCE_ERLS)))) + +# Erlang expressions which will be invoked during testing (not in the +# broker). +STANDALONE_TEST_COMMANDS:= + +# Erlang expressions which will be invoked within the broker during +# testing. +WITH_BROKER_TEST_COMMANDS:= + +# Config file to give to the test broker. +WITH_BROKER_TEST_CONFIG:= + +# Test scripts which should be invokedduring testing +STANDALONE_TEST_SCRIPTS:= + +# Test scripts which should be invoked alongside a running broker +# during testing +WITH_BROKER_TEST_SCRIPTS:= + +# Test scripts which should be invoked to configure the broker before testing +WITH_BROKER_SETUP_SCRIPTS:= + +# When cleaning, should we also remove the cloned directory for +# wrappers? +PRESERVE_CLONE_DIR?= + +# The directory within the package that contains tests +TEST_DIR=$(PACKAGE_DIR)/test + +# The directories containing .erl files for tests +TEST_SOURCE_DIRS=$(TEST_DIR)/src + +# The .erl files for tests +TEST_SOURCE_ERLS=$(strip $(foreach D,$(TEST_SOURCE_DIRS),$(wildcard $(D)/*.erl))) + +# Where to put .beam files produced by compiling TEST_SOURCE_ERLS +TEST_EBIN_DIR=$(TEST_DIR)/ebin + +# The .beam files produced by compiling TEST_SOURCE_ERLS +TEST_EBIN_BEAMS=$(patsubst %,$(TEST_EBIN_DIR)/%.beam,$(notdir $(basename $(TEST_SOURCE_ERLS)))) + +# Wrapper package variables + +# The git URL to clone from. Setting this variable marks the package +# as a wrapper package. +UPSTREAM_GIT:= + +# The Mercurial URL to clone from. Setting this variable marks the +# package as a wrapper package. +UPSTREAM_HG:= + +UPSTREAM_TYPE=$(if $(UPSTREAM_GIT),git)$(if $(UPSTREAM_HG),hg) + +# The upstream revision to clone. Leave empty for default or master +UPSTREAM_REVISION:= + +# Where to clone the upstream repository to +CLONE_DIR=$(PACKAGE_DIR)/$(patsubst %-wrapper,%,$(PACKAGE_NAME))-$(UPSTREAM_TYPE) + +# The source directories contained in the cloned repositories. These +# are appended to SOURCE_DIRS. +UPSTREAM_SOURCE_DIRS=$(CLONE_DIR)/src + +# The include directories contained in the cloned repositories. These +# are appended to INCLUDE_DIRS. +UPSTREAM_INCLUDE_DIRS=$(CLONE_DIR)/include + +# Patches to apply to the upstream codebase after cloning, if any +WRAPPER_PATCHES:= + +# The version number to assign to the build artifacts +PACKAGE_VERSION=$(VERSION) + +# Should the app version incorporate the version from the original +# .app file? +RETAIN_ORIGINAL_VERSION:= + +# The original version that should be incorporated into the package +# version if RETAIN_ORIGINAL_VERSION is set. If empty, the original +# version will be extracted from ORIGINAL_APP_FILE. +ORIGINAL_VERSION:= + +# For customising construction of the build application directory. +CONSTRUCT_APP_PREREQS:= +construct_app_commands= + +package_rules= + +# Now let the package makefile fragment do its stuff +include $(PACKAGE_DIR)/package.mk + +# package_rules provides a convenient way to force prompt expansion +# of variables, including expansion in commands that would otherwise +# be deferred. +# +# If package_rules is defined by the package makefile, we expand it +# and eval it. The point here is to get around the fact that make +# defers expansion of commands. But if we use package variables in +# targets, as we naturally want to do, deferred expansion doesn't +# work: They might have been trampled on by a later package. Because +# we expand package_rules here, references to package varialbes will +# get expanded with the values we expect. +# +# The downside is that any variable references for which expansion +# really should be deferred need to be protected by doulbing up the +# dollar. E.g., inside package_rules, you should write $$@, not $@. +# +# We use the same trick again below. +ifdef package_rules +$(eval $(package_rules)) +endif + +# Some variables used for brevity below. Packages can't set these. +APP_FILE=$(PACKAGE_DIR)/build/$(APP_NAME).app.$(PACKAGE_VERSION) +APP_DONE=$(PACKAGE_DIR)/build/app/.done.$(PACKAGE_VERSION) +APP_DIR=$(PACKAGE_DIR)/build/app/$(APP_NAME)-$(PACKAGE_VERSION) +EZ_FILE=$(PACKAGE_DIR)/dist/$(APP_NAME)-$(PACKAGE_VERSION).ez +DEPS_FILE=$(PACKAGE_DIR)/build/deps.mk + + +# Convert the DEPS package names to canonical paths +DEP_PATHS:=$(foreach DEP,$(DEPS),$(call package_to_path,$(DEP))) + +# Handle RETAIN_ORIGINAL_VERSION / ORIGINAL_VERSION +ifdef RETAIN_ORIGINAL_VERSION + +# Automatically acquire ORIGINAL_VERSION from ORIGINAL_APP_FILE +ifndef ORIGINAL_VERSION + +# The generated ORIGINAL_VERSION setting goes in build/version.mk +$(eval $(call safe_include,$(PACKAGE_DIR)/build/version.mk)) + +$(PACKAGE_DIR)/build/version.mk: $(ORIGINAL_APP_FILE) + sed -n -e 's|^.*{vsn, *"\([^"]*\)".*$$|ORIGINAL_VERSION:=\1|p' <$< >$@ + +$(APP_FILE): $(PACKAGE_DIR)/build/version.mk + +endif # ifndef ORIGINAL_VERSION + +PACKAGE_VERSION:=$(ORIGINAL_VERSION)-rmq$(VERSION) + +endif # ifdef RETAIN_ORIGINAL_VERSION + +# Handle wrapper packages +ifneq ($(UPSTREAM_TYPE),) + +SOURCE_DIRS+=$(UPSTREAM_SOURCE_DIRS) +INCLUDE_DIRS+=$(UPSTREAM_INCLUDE_DIRS) + +define package_rules + +# We use --no-backup-if-mismatch to prevent .orig files ending up in +# source builds and causing warnings on Debian if the patches have +# fuzz. +ifdef UPSTREAM_GIT +$(CLONE_DIR)/.done: + rm -rf $(CLONE_DIR) + git clone $(UPSTREAM_GIT) $(CLONE_DIR) + # Work around weird github breakage (bug 25264) + cd $(CLONE_DIR) && git pull + $(if $(UPSTREAM_REVISION),cd $(CLONE_DIR) && git checkout $(UPSTREAM_REVISION)) + $(if $(WRAPPER_PATCHES),$(foreach F,$(WRAPPER_PATCHES),patch -E -z .umbrella-orig -d $(CLONE_DIR) -p1 <$(PACKAGE_DIR)/$(F) &&) :) + find $(CLONE_DIR) -name "*.umbrella-orig" -delete + touch $$@ +endif # UPSTREAM_GIT + +ifdef UPSTREAM_HG +$(CLONE_DIR)/.done: + rm -rf $(CLONE_DIR) + hg clone -r $(or $(UPSTREAM_REVISION),default) $(UPSTREAM_HG) $(CLONE_DIR) + $(if $(WRAPPER_PATCHES),$(foreach F,$(WRAPPER_PATCHES),patch -E -z .umbrella-orig -d $(CLONE_DIR) -p1 <$(PACKAGE_DIR)/$(F) &&) :) + find $(CLONE_DIR) -name "*.umbrella-orig" -delete + touch $$@ +endif # UPSTREAM_HG + +# When we clone, we need to remake anything derived from the app file +# (e.g. build/version.mk). +$(ORIGINAL_APP_FILE): $(CLONE_DIR)/.done + +# We include the commit hash into the package version, via hash.mk +# (not in build/ because we want it to survive +# make PRESERVE_CLONE_DIR=true clean +# for obvious reasons) +$(eval $(call safe_include,$(PACKAGE_DIR)/hash.mk)) + +$(PACKAGE_DIR)/hash.mk: $(CLONE_DIR)/.done + @mkdir -p $$(@D) +ifdef UPSTREAM_GIT + echo UPSTREAM_SHORT_HASH:=`git --git-dir=$(CLONE_DIR)/.git log -n 1 HEAD | grep commit | cut -b 8-14` >$$@ +endif +ifdef UPSTREAM_HG + echo UPSTREAM_SHORT_HASH:=`hg id -R $(CLONE_DIR) -i | cut -c -7` >$$@ +endif + +$(APP_FILE): $(PACKAGE_DIR)/hash.mk + +PACKAGE_VERSION:=$(PACKAGE_VERSION)-$(UPSTREAM_TYPE)$(UPSTREAM_SHORT_HASH) + +$(PACKAGE_DIR)+clean:: + [ "x" != "x$(PRESERVE_CLONE_DIR)" ] || rm -rf $(CLONE_DIR) hash.mk +endef # package_rules +$(eval $(package_rules)) + +endif # UPSTREAM_TYPE + +# Generate a rule to compile .erl files from the directory $(1) into +# directory $(2), taking extra erlc options from $(3) +define package_source_dir_targets +$(2)/%.beam: $(1)/%.erl $(PACKAGE_DIR)/build/dep-apps/.done | $(DEPS_FILE) + @mkdir -p $$(@D) + ERL_LIBS=$(PACKAGE_DIR)/build/dep-apps $(ERLC) $(PACKAGE_ERLC_OPTS) $(foreach D,$(INCLUDE_DIRS),-I $(D)) -pa $$(@D) -o $$(@D) $(3) $$< + +endef + +$(eval $(foreach D,$(SOURCE_DIRS),$(call package_source_dir_targets,$(D),$(EBIN_DIR),))) +$(eval $(foreach D,$(TEST_SOURCE_DIRS),$(call package_source_dir_targets,$(D),$(TEST_EBIN_DIR),-pa $(EBIN_DIR)))) + +# Commands to run the broker for tests +# +# $(1): The value for RABBITMQ_SERVER_START_ARGS +# $(2): Extra env var settings when invoking the rabbitmq-server script +# $(3): Extra .ezs to copy into the plugins dir +define run_broker + rm -rf $(TEST_TMPDIR) + mkdir -p $(foreach D,log plugins $(NODENAME),$(TEST_TMPDIR)/$(D)) + cp -p $(PACKAGE_DIR)/dist/*.ez $(TEST_TMPDIR)/plugins + $(call copy,$(3),$(TEST_TMPDIR)/plugins) + rm -f $(TEST_TMPDIR)/plugins/rabbit_common*.ez + RABBITMQ_PLUGINS_DIR=$(TEST_TMPDIR)/plugins \ + RABBITMQ_ENABLED_PLUGINS_FILE=$(TEST_TMPDIR)/enabled_plugins \ + $(UMBRELLA_BASE_DIR)/rabbitmq-server/scripts/rabbitmq-plugins \ + set --offline $$$$(RABBITMQ_PLUGINS_DIR=$(TEST_TMPDIR)/plugins \ + RABBITMQ_ENABLED_PLUGINS_FILE=$(TEST_TMPDIR)/enabled_plugins \ + $(UMBRELLA_BASE_DIR)/rabbitmq-server/scripts/rabbitmq-plugins list -m | tr '\n' ' ') + MAKE="$(MAKE)" \ + RABBITMQ_PLUGINS_DIR=$(TEST_TMPDIR)/plugins \ + RABBITMQ_ENABLED_PLUGINS_FILE=$(TEST_TMPDIR)/enabled_plugins \ + RABBITMQ_LOG_BASE=$(TEST_TMPDIR)/log \ + RABBITMQ_MNESIA_BASE=$(TEST_TMPDIR)/$(NODENAME) \ + RABBITMQ_PID_FILE=$(TEST_TMPDIR)/$(NODENAME).pid \ + RABBITMQ_NODENAME=$(NODENAME) \ + RABBITMQ_SERVER_START_ARGS=$(1) \ + $(2) $(UMBRELLA_BASE_DIR)/rabbitmq-server/scripts/rabbitmq-server +endef + +# Commands to run the package's test suite +# +# $(1): Extra .ezs to copy into the plugins dir +define run_with_broker_tests +$(if $(WITH_BROKER_TEST_COMMANDS)$(WITH_BROKER_TEST_SCRIPTS),$(call run_with_broker_tests_aux,$1)) +endef + +define run_with_broker_tests_aux + $(call run_broker,'-pa $(TEST_EBIN_DIR) -coverage directories ["$(EBIN_DIR)"$(COMMA)"$(TEST_EBIN_DIR)"]',RABBITMQ_CONFIG_FILE=$(WITH_BROKER_TEST_CONFIG),$(1)) & + $(UMBRELLA_BASE_DIR)/rabbitmq-server/scripts/rabbitmqctl -n $(NODENAME) wait $(TEST_TMPDIR)/$(NODENAME).pid + echo > $(TEST_TMPDIR)/rabbit-test-output && \ + if $(foreach SCRIPT,$(WITH_BROKER_SETUP_SCRIPTS),$(SCRIPT) &&) \ + $(foreach CMD,$(WITH_BROKER_TEST_COMMANDS), \ + echo >> $(TEST_TMPDIR)/rabbit-test-output && \ + echo "$(CMD)." \ + | tee -a $(TEST_TMPDIR)/rabbit-test-output \ + | $(ERL_CALL) $(ERL_CALL_OPTS) \ + | tee -a $(TEST_TMPDIR)/rabbit-test-output \ + | egrep "{ok, (ok|passed)}" >/dev/null &&) \ + MAKE="$(MAKE)" RABBITMQ_NODENAME="$(NODENAME)" \ + $(foreach SCRIPT,$(WITH_BROKER_TEST_SCRIPTS),$(SCRIPT) &&) : ; \ + then \ + touch $(TEST_TMPDIR)/.passed ; \ + printf "\nPASSED\n" ; \ + else \ + cat $(TEST_TMPDIR)/rabbit-test-output ; \ + printf "\n\nFAILED\n" ; \ + fi + sleep 1 + echo "rabbit_misc:report_cover(), init:stop()." | $(ERL_CALL) $(ERL_CALL_OPTS) + sleep 1 + test -f $(TEST_TMPDIR)/.passed +endef + +# The targets common to all integrated packages +define package_rules + +# Put all relevant ezs into the dist dir for this package, including +# the main ez file produced by this package +# +# When the package version changes, our .ez filename will change, and +# we need to regenerate the dist directory. So the dependency needs +# to go via a stamp file that incorporates the version in its name. +# But we need a target with a fixed name for other packages to depend +# on. And it can't be a phony, as a phony will always get rebuilt. +# Hence the need for two stamp files here. +$(PACKAGE_DIR)/dist/.done: $(PACKAGE_DIR)/dist/.done.$(PACKAGE_VERSION) + touch $$@ + +$(PACKAGE_DIR)/dist/.done.$(PACKAGE_VERSION): $(PACKAGE_DIR)/build/dep-ezs/.done $(APP_DONE) + rm -rf $$(@D) + mkdir -p $$(@D) + cd $(dir $(APP_DIR)) && zip -q -r $$(abspath $(EZ_FILE)) $(notdir $(APP_DIR)) + $$(call copy,$$(wildcard $$($$@ + +ifndef DO_NOT_GENERATE_APP_FILE + +# Generate the .app file. Note that this is a separate step from above +# so that the plugin still works correctly when symlinked as a directory +$(ORIGINAL_APP_FILE): $(ORIGINAL_APP_SOURCE) $(SOURCE_ERLS) $(UMBRELLA_BASE_DIR)/generate_app + @mkdir -p $$(@D) + escript $(UMBRELLA_BASE_DIR)/generate_app $$< $$@ $(SOURCE_DIRS) + +$(PACKAGE_DIR)+clean:: + rm -f $(ORIGINAL_APP_FILE) + +endif + +# Unpack the ezs from dependency packages, so that their contents are +# accessible to erlc +$(PACKAGE_DIR)/build/dep-apps/.done: $(PACKAGE_DIR)/build/dep-ezs/.done + rm -rf $$(@D) + mkdir -p $$(@D) + @echo [elided] unzip ezs + @cd $$(@D) && $$(foreach EZ,$$(wildcard $(PACKAGE_DIR)/build/dep-ezs/*.ez),unzip -q $$(abspath $$(EZ)) &&) : + touch $$@ + +# Dependency autogeneration. This is complicated slightly by the need +# to generate a dependency file which is path-independent. +$(DEPS_FILE): $(SOURCE_ERLS) $(INCLUDE_HRLS) $(TEST_SOURCE_ERLS) + @mkdir -p $$(@D) + @echo [elided] generate deps + @$$(if $$^,echo $$(subst : ,:,$$(foreach F,$$^,$$(abspath $$(F)):)) | escript $(abspath $(UMBRELLA_BASE_DIR)/generate_deps) $$@ '$$$$(EBIN_DIR)',echo >$$@) + @echo [elided] fix test deps + @$$(foreach F,$(TEST_EBIN_BEAMS),sed -e 's|^$$$$(EBIN_DIR)/$$(notdir $$(F)):|$$$$(TEST_EBIN_DIR)/$$(notdir $$(F)):|' $$@ > $$@.tmp && mv $$@.tmp $$@ && ) : + sed -e 's|$$@|$$$$(DEPS_FILE)|' $$@ > $$@.tmp && mv $$@.tmp $$@ + +$(eval $(call safe_include,$(DEPS_FILE))) + +$(PACKAGE_DIR)/srcdist/.done: $(PACKAGE_DIR)/srcdist/.done.$(PACKAGE_VERSION) + touch $$@ + +$(PACKAGE_DIR)/srcdist/.done.$(PACKAGE_VERSION): + mkdir -p $(PACKAGE_DIR)/build/srcdist/ + rsync -a --exclude '.hg*' --exclude '.git*' --exclude 'build' $(PACKAGE_DIR) $(PACKAGE_DIR)/build/srcdist/$(APP_NAME)-$(PACKAGE_VERSION) + mkdir -p $(PACKAGE_DIR)/srcdist/ + tar cjf $(PACKAGE_DIR)/srcdist/$(APP_NAME)-$(PACKAGE_VERSION)-src.tar.bz2 -C $(PACKAGE_DIR)/build/srcdist/ $(APP_NAME)-$(PACKAGE_VERSION) + touch $$@ + +$(PACKAGE_DIR)+clean:: + rm -rf $(EBIN_DIR)/*.beam $(TEST_EBIN_DIR)/*.beam $(PACKAGE_DIR)/dist $(PACKAGE_DIR)/srcdist $(PACKAGE_DIR)/build $(PACKAGE_DIR)/erl_crash.dump + +$(PACKAGE_DIR)+clean-with-deps:: $(foreach P,$(DEP_PATHS),$(P)+clean-with-deps) + +ifdef RELEASABLE +all-releasable:: $(PACKAGE_DIR)/dist/.done + +copy-releasable:: $(PACKAGE_DIR)/dist/.done + cp $(PACKAGE_DIR)/dist/*.ez $(PLUGINS_DIST_DIR) + +copy-srcdist:: $(PLUGINS_SRC_DIST_DIR)/$(PACKAGE_DIR)/.srcdist_done + +endif + +$(PLUGINS_SRC_DIST_DIR)/$(PACKAGE_DIR)/.srcdist_done:: $(ORIGINAL_APP_FILE) $(foreach P,$(DEP_PATHS),$(PLUGINS_SRC_DIST_DIR)/$(P)/.srcdist_done) + rsync -a --exclude '.hg*' --exclude '.git*' $(PACKAGE_DIR) $(PLUGINS_SRC_DIST_DIR)/ + [ -f $(PACKAGE_DIR)/license_info ] && cp $(PACKAGE_DIR)/license_info $(PLUGINS_SRC_DIST_DIR)/licensing/license_info_$(PACKAGE_NAME) || true + find $(PACKAGE_DIR) -maxdepth 1 -name 'LICENSE-*' -exec cp '{}' $(PLUGINS_SRC_DIST_DIR)/licensing/ \; + touch $(PLUGINS_SRC_DIST_DIR)/$(PACKAGE_DIR)/.srcdist_done + +# A hook to allow packages to verify that prerequisites are satisfied +# before running. +.PHONY: $(PACKAGE_DIR)+pre-run +$(PACKAGE_DIR)+pre-run:: + +# Run erlang with the package, its tests, and all its dependencies +# available. +.PHONY: $(PACKAGE_DIR)+run +$(PACKAGE_DIR)+run: $(PACKAGE_DIR)/dist/.done $(TEST_EBIN_BEAMS) $(PACKAGE_DIR)+pre-run + ERL_LIBS=$(PACKAGE_DIR)/dist $(ERL) $(ERL_OPTS) -pa $(TEST_EBIN_DIR) + +# Run the broker with the package, its tests, and all its dependencies +# available. +.PHONY: $(PACKAGE_DIR)+run-in-broker +$(PACKAGE_DIR)+run-in-broker: $(PACKAGE_DIR)/dist/.done $(RABBITMQ_SERVER_PATH)/dist/.done $(TEST_EBIN_BEAMS) + $(call run_broker,'-pa $(TEST_EBIN_DIR)',RABBITMQ_ALLOW_INPUT=true) + +# A hook to allow packages to verify that prerequisites are satisfied +# before running tests. +.PHONY: $(PACKAGE_DIR)+pre-test +$(PACKAGE_DIR)+pre-test:: + +# Runs the package's tests that operate within (or in conjuction with) +# a running broker. +.PHONY: $(PACKAGE_DIR)+in-broker-test +$(PACKAGE_DIR)+in-broker-test: $(PACKAGE_DIR)/dist/.done $(RABBITMQ_SERVER_PATH)/dist/.done $(TEST_EBIN_BEAMS) $(PACKAGE_DIR)+pre-test $(PACKAGE_DIR)+standalone-test $(if $(RELEASABLE),$(call chain_test,$(PACKAGE_DIR)+in-broker-test)) + $(call run_with_broker_tests) + +# Running the coverage tests requires Erlang/OTP R14. Note that +# coverage only covers the in-broker tests. +.PHONY: $(PACKAGE_DIR)+coverage +$(PACKAGE_DIR)+coverage: $(PACKAGE_DIR)/dist/.done $(COVERAGE_PATH)/dist/.done $(TEST_EBIN_BEAMS) $(PACKAGE_DIR)+pre-test + $(call run_with_broker_tests,$(COVERAGE_PATH)/dist/*.ez) + +# Runs the package's tests that don't need a running broker +.PHONY: $(PACKAGE_DIR)+standalone-test +$(PACKAGE_DIR)+standalone-test: $(PACKAGE_DIR)/dist/.done $(TEST_EBIN_BEAMS) $(PACKAGE_DIR)+pre-test $(if $(RELEASABLE),$(call chain_test,$(PACKAGE_DIR)+standalone-test)) + $$(if $(STANDALONE_TEST_COMMANDS),\ + $$(foreach CMD,$(STANDALONE_TEST_COMMANDS),\ + ERL_LIBS=$(PACKAGE_DIR)/dist $(ERL) -noinput $(ERL_OPTS) -pa $(TEST_EBIN_DIR) -sname standalone_test -eval "init:stop(case $$(CMD) of ok -> 0; passed -> 0; _Else -> 1 end)" &&\ + )\ + :) + $$(if $(STANDALONE_TEST_SCRIPTS),$$(foreach SCRIPT,$(STANDALONE_TEST_SCRIPTS),$$(SCRIPT) &&) :) + +# Run all the package's tests +.PHONY: $(PACKAGE_DIR)+test +$(PACKAGE_DIR)+test:: $(PACKAGE_DIR)+standalone-test $(PACKAGE_DIR)+in-broker-test + +.PHONY: $(PACKAGE_DIR)+check-xref +$(PACKAGE_DIR)+check-xref: $(PACKAGE_DIR)/dist/.done + UNPACKDIR=$$$$(mktemp -d $(TMPDIR)/tmp.XXXXXXXXXX) && \ + for ez in $$$$(find $(PACKAGE_DIR)/dist -type f -name "*.ez"); do \ + unzip -q $$$${ez} -d $$$${UNPACKDIR}; \ + done && \ + rm -rf $$$${UNPACKDIR}/rabbit_common-* && \ + ln -sf $$$$(pwd)/$(RABBITMQ_SERVER_PATH)/ebin $$$${UNPACKDIR} && \ + OK=true && \ + { $(UMBRELLA_BASE_DIR)/check_xref $(PACKAGE_DIR) $$$${UNPACKDIR} || OK=false; } && \ + rm -rf $$$${UNPACKDIR} && \ + $$$${OK} + +check-xref-packages:: $(PACKAGE_DIR)+check-xref + +endef +$(eval $(package_rules)) + +# Recursing into dependency packages has to be the last thing we do +# because it will trample all over the per-package variables. + +# Recurse into dependency packages +$(foreach DEP_PATH,$(DEP_PATHS),$(eval $(call do_package,$(DEP_PATH)))) + +else # NON_INTEGRATED_$(PACKAGE_DIR) + +define package_rules + +# When the package version changes, our .ez filename will change, and +# we need to regenerate the dist directory. So the dependency needs +# to go via a stamp file that incorporates the version in its name. +# But we need a target with a fixed name for other packages to depend +# on. And it can't be a phony, as a phony will always get rebuilt. +# Hence the need for two stamp files here. +$(PACKAGE_DIR)/dist/.done: $(PACKAGE_DIR)/dist/.done.$(VERSION) + touch $$@ + +# Non-integrated packages (rabbitmq-server and rabbitmq-erlang-client) +# present a dilemma. We could re-make the package every time we need +# it. But that will cause a huge amount of unnecessary rebuilding. +# Or we could not worry about rebuilding non-integrated packages. +# That's good for those developing plugins, but not for those who want +# to work on the broker and erlang client in the context of the +# plugins. So instead, we use a conservative approximation to the +# dependency structure within the package, to tell when to re-run the +# makefile. +$(PACKAGE_DIR)/dist/.done.$(VERSION): $(PACKAGE_DIR)/Makefile $(wildcard $(PACKAGE_DIR)/*.mk) $(wildcard $(PACKAGE_DIR)/src/*.erl) $(wildcard $(PACKAGE_DIR)/include/*.hrl) $(wildcard $(PACKAGE_DIR)/*.py) $(foreach DEP,$(NON_INTEGRATED_DEPS_$(PACKAGE_DIR)),$(call package_to_path,$(DEP))/dist/.done) + rm -rf $$(@D) + $$(MAKE) -C $(PACKAGE_DIR) + mkdir -p $$(@D) + touch $$@ + +# When building plugins-src we want to "make clean", but some +# non-integrated packages will not be there. Don't fall over in that case. +$(PACKAGE_DIR)+clean:: + if [ -d $(PACKAGE_DIR) ] ; then $$(MAKE) -C $(PACKAGE_DIR) clean ; fi + rm -rf $(PACKAGE_DIR)/dist + +endef +$(eval $(package_rules)) + +endif # NON_INTEGRATED_$(PACKAGE_DIR) diff --git a/rabbitmq-server/plugins-src/eldap-wrapper/.srcdist_done b/rabbitmq-server/plugins-src/eldap-wrapper/.srcdist_done new file mode 100644 index 0000000..e69de29 diff --git a/rabbitmq-server/plugins-src/eldap-wrapper/CONTRIBUTING.md b/rabbitmq-server/plugins-src/eldap-wrapper/CONTRIBUTING.md new file mode 100644 index 0000000..69a4b4a --- /dev/null +++ b/rabbitmq-server/plugins-src/eldap-wrapper/CONTRIBUTING.md @@ -0,0 +1,51 @@ +## Overview + +RabbitMQ projects use pull requests to discuss, collaborate on and accept code contributions. +Pull requests is the primary place of discussing code changes. + +## How to Contribute + +The process is fairly standard: + + * Fork the repository or repositories you plan on contributing to + * Clone [RabbitMQ umbrella repository](https://github.com/rabbitmq/rabbitmq-public-umbrella) + * `cd umbrella`, `make co` + * Create a branch with a descriptive name in the relevant repositories + * Make your changes, run tests, commit with a [descriptive message](http://tbaggery.com/2008/04/19/a-note-about-git-commit-messages.html), push to your fork + * Submit pull requests with an explanation what has been changed and **why** + * Submit a filled out and signed [Contributor Agreement](https://github.com/rabbitmq/ca#how-to-submit) if needed (see below) + * Be patient. We will get to your pull request eventually + +If what you are going to work on is a substantial change, please first ask the core team +of their opinion on [RabbitMQ mailing list](https://groups.google.com/forum/#!forum/rabbitmq-users). + + +## (Brief) Code of Conduct + +In one line: don't be a dick. + +Be respectful to the maintainers and other contributors. Open source +contributors put long hours into developing projects and doing user +support. Those projects and user support are available for free. We +believe this deserves some respect. + +Be respectful to people of all races, genders, religious beliefs and +political views. Regardless of how brilliant a pull request is +technically, we will not tolerate disrespectful or aggressive +behaviour. + +Contributors who violate this straightforward Code of Conduct will see +their pull requests closed and locked. + + +## Contributor Agreement + +If you want to contribute a non-trivial change, please submit a signed copy of our +[Contributor Agreement](https://github.com/rabbitmq/ca#how-to-submit) around the time +you submit your pull request. This will make it much easier (in some cases, possible) +for the RabbitMQ team at Pivotal to merge your contribution. + + +## Where to Ask Questions + +If something isn't clear, feel free to ask on our [mailing list](https://groups.google.com/forum/#!forum/rabbitmq-users). diff --git a/rabbitmq-server/plugins-src/eldap-wrapper/LICENSE-MIT-eldap b/rabbitmq-server/plugins-src/eldap-wrapper/LICENSE-MIT-eldap new file mode 100644 index 0000000..1f62009 --- /dev/null +++ b/rabbitmq-server/plugins-src/eldap-wrapper/LICENSE-MIT-eldap @@ -0,0 +1,21 @@ + +Copyright (c) 2010, Torbjorn Tornkvist + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. + diff --git a/rabbitmq-server/plugins-src/eldap-wrapper/Makefile b/rabbitmq-server/plugins-src/eldap-wrapper/Makefile new file mode 100644 index 0000000..482105a --- /dev/null +++ b/rabbitmq-server/plugins-src/eldap-wrapper/Makefile @@ -0,0 +1 @@ +include ../umbrella.mk diff --git a/rabbitmq-server/plugins-src/eldap-wrapper/eldap-appify.patch b/rabbitmq-server/plugins-src/eldap-wrapper/eldap-appify.patch new file mode 100644 index 0000000..90ad3d2 --- /dev/null +++ b/rabbitmq-server/plugins-src/eldap-wrapper/eldap-appify.patch @@ -0,0 +1,14 @@ +diff -Naur eldap.orig/ebin/eldap.app eldap/ebin/eldap.app +--- eldap.orig/ebin/eldap.app 1970-01-01 01:00:00.000000000 +0100 ++++ eldap/ebin/eldap.app 2011-01-20 12:47:04.377399296 +0000 +@@ -0,0 +1,10 @@ ++{application, eldap, ++ [{description, "LDAP Client Library"}, ++ {vsn, "0.01"}, ++ {modules, [ ++ eldap, ++ 'ELDAPv3' ++ ]}, ++ {registered, []}, ++ {applications, [kernel, stdlib]} ++ ]}. diff --git a/rabbitmq-server/plugins-src/eldap-wrapper/eldap-git/.done b/rabbitmq-server/plugins-src/eldap-wrapper/eldap-git/.done new file mode 100644 index 0000000..e69de29 diff --git a/rabbitmq-server/plugins-src/eldap-wrapper/eldap-git/LICENSE b/rabbitmq-server/plugins-src/eldap-wrapper/eldap-git/LICENSE new file mode 100644 index 0000000..1f62009 --- /dev/null +++ b/rabbitmq-server/plugins-src/eldap-wrapper/eldap-git/LICENSE @@ -0,0 +1,21 @@ + +Copyright (c) 2010, Torbjorn Tornkvist + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. + diff --git a/rabbitmq-server/plugins-src/eldap-wrapper/eldap-git/Makefile b/rabbitmq-server/plugins-src/eldap-wrapper/eldap-git/Makefile new file mode 100644 index 0000000..f5ecba4 --- /dev/null +++ b/rabbitmq-server/plugins-src/eldap-wrapper/eldap-git/Makefile @@ -0,0 +1,7 @@ + +all: + (cd src;$(MAKE)) + +clean: + (cd src;$(MAKE) clean) + diff --git a/rabbitmq-server/plugins-src/eldap-wrapper/eldap-git/README b/rabbitmq-server/plugins-src/eldap-wrapper/eldap-git/README new file mode 100644 index 0000000..e1bde9d --- /dev/null +++ b/rabbitmq-server/plugins-src/eldap-wrapper/eldap-git/README @@ -0,0 +1,33 @@ +Hi, + +This is 'eldap', the Erlang LDAP library. + +It exports an API that can do all possible operations +you may want to do against an LDAP server. The code has +been tested to work at some point, but only the bind +and search operations are running daily in our products, +so there may be bugs lurking in some parts of the code. + +To just use eldap for doing authentication, do like in: + + {ok,X} = eldap:open(["ldap.mycorp.com"], []). + eldap:simple_bind(X, "uid=tobbe,ou=People,dc=mycorp,dc=com", "passwd"). + +In the doc/README.example you'll find a trace from a +Erlang shell session as an example on how to setup a +connection, authenticate (bind) and perform a search. +Note that by using the option {ssl, true}, you should +be able to setup an SSL tunnel (LDAPS) if your Erlang +system has been configured with SSL. + +In the test directory there are some hints and examples +on how to test the code and how to setup and populate +an OpenLDAP server. The 'eldap' code has been tested +agains OpenLDAP, IPlanet and ActiveDirectory servers. + +If you plan to incorporate this code into your system +I suggest that you build a server/supervisor harnesk +that uses 'eldap' (as we have done in our products). + +Good luck ! +/Tobbe diff --git a/rabbitmq-server/plugins-src/eldap-wrapper/eldap-git/doc/README.example b/rabbitmq-server/plugins-src/eldap-wrapper/eldap-git/doc/README.example new file mode 100644 index 0000000..b96d5ef --- /dev/null +++ b/rabbitmq-server/plugins-src/eldap-wrapper/eldap-git/doc/README.example @@ -0,0 +1,44 @@ +1> {_,S} = eldap:open(["192.168.128.47"], []). +{ok,<0.30.0>} +2> eldap:simple_bind(S,"cn=Torbjorn Tornkvist,cn=Users,dc=bluetail,dc=com","qwe123"). +ok +3> Base = {base, "dc=bluetail,dc=com"}. +{base,"dc=bluetail,dc=com"} +4> Scope = {scope, eldap:wholeSubtree()}. +{scope,wholeSubtree} +5> Filter = {filter, eldap:equalityMatch("sAMAccountName", "tobbe")}. +{filter,{equalityMatch,{'AttributeValueAssertion',"sAMAccountName","tobbe"}}} +6> Search = [Base, Scope, Filter]. +[{base,"dc=bluetail,dc=com"}, + {scope,wholeSubtree}, + {filter,{equalityMatch,{'AttributeValueAssertion',"sAMAccountName","tobbe"}}}] +7> eldap:search(S, Search). +{ok,{eldap_search_result,[{eldap_entry, + "CN=Torbjorn Tornkvist,CN=Users,DC=bluetail,DC=com", + [{"manager", + ["CN=Tord Larsson,CN=Users,DC=bluetail,DC=com"]}, + {"memberOf", + ["CN=TestGroup2,CN=Users,DC=bluetail,DC=com", + "CN=TestGroup,CN=Users,DC=bluetail,DC=com", + "CN=Pre-Windows 2000 Compatible Access,CN=Builtin,DC=bluetail,DC=com", + "CN=Server Operators,CN=Builtin,DC=bluetail,DC=com"]}, + {"accountExpires",["0"]}, + {"adminCount",["1"]}, + {"badPasswordTime",["127119104851642448"]}, + {"badPwdCount",["0"]}, + {"codePage",["0"]}, + {"cn",["Torbjorn Tornkvist"]}, + {"company",["Alteon Web Systems"]}, + {"countryCode",["0"]}, + {"department",["Bluetail"]}, + {"displayName",["Torbjorn Tornkvist"]}, + {"mail",["tobbe@bluetail.com"]}, + {"givenName",["Torbjorn"]}, + {"instanceType",["4"]}, + {"lastLogoff",["0"]}, + {"lastLogon",["127119109376267104"]}, + {"logonCount",[...]}, + {"msNPAllowDialin"|...}, + {...}|...]}], + [["ldap://bluetail.com/CN=Configuration,DC=bluetail,DC=com"]]}} +8> diff --git a/rabbitmq-server/plugins-src/eldap-wrapper/eldap-git/doc/short-desc b/rabbitmq-server/plugins-src/eldap-wrapper/eldap-git/doc/short-desc new file mode 100644 index 0000000..e236da3 --- /dev/null +++ b/rabbitmq-server/plugins-src/eldap-wrapper/eldap-git/doc/short-desc @@ -0,0 +1 @@ +This is 'eldap', the Erlang LDAP library. diff --git a/rabbitmq-server/plugins-src/eldap-wrapper/eldap-git/ebin/eldap.app b/rabbitmq-server/plugins-src/eldap-wrapper/eldap-git/ebin/eldap.app new file mode 100644 index 0000000..3c4e87e --- /dev/null +++ b/rabbitmq-server/plugins-src/eldap-wrapper/eldap-git/ebin/eldap.app @@ -0,0 +1,10 @@ +{application, eldap, + [{description, "LDAP Client Library"}, + {vsn, "0.01"}, + {modules, [ + eldap, + 'ELDAPv3' + ]}, + {registered, []}, + {applications, [kernel, stdlib]} + ]}. diff --git a/rabbitmq-server/plugins-src/eldap-wrapper/eldap-git/include/eldap.hrl b/rabbitmq-server/plugins-src/eldap-wrapper/eldap-git/include/eldap.hrl new file mode 100644 index 0000000..ee5ad2f --- /dev/null +++ b/rabbitmq-server/plugins-src/eldap-wrapper/eldap-git/include/eldap.hrl @@ -0,0 +1,32 @@ +-ifndef( _ELDAP_HRL ). +-define( _ELDAP_HRL , 1 ). + +%%% +%%% Search input parameters +%%% +-record(eldap_search, { + base = [], % Baseobject + filter = [], % Search conditions + scope, % Search scope + attributes = [], % Attributes to be returned + types_only = false, % Return types+values or types + timeout = 0 % Timelimit for search + }). + +%%% +%%% Returned search result +%%% +-record(eldap_search_result, { + entries = [], % List of #eldap_entry{} records + referrals = [] % List of referrals + }). + +%%% +%%% LDAP entry +%%% +-record(eldap_entry, { + object_name = "", % The DN for the entry + attributes = [] % List of {Attribute, Value} pairs + }). + +-endif. diff --git a/rabbitmq-server/plugins-src/eldap-wrapper/eldap-git/src/ELDAPv3.asn b/rabbitmq-server/plugins-src/eldap-wrapper/eldap-git/src/ELDAPv3.asn new file mode 100644 index 0000000..0cfac48 --- /dev/null +++ b/rabbitmq-server/plugins-src/eldap-wrapper/eldap-git/src/ELDAPv3.asn @@ -0,0 +1,291 @@ +-- LDAPv3 ASN.1 specification, taken from RFC 2251 + +-- Lightweight-Directory-Access-Protocol-V3 DEFINITIONS +ELDAPv3 DEFINITIONS +IMPLICIT TAGS ::= + +BEGIN + +LDAPMessage ::= SEQUENCE { + messageID MessageID, + protocolOp CHOICE { + bindRequest BindRequest, + bindResponse BindResponse, + unbindRequest UnbindRequest, + searchRequest SearchRequest, + searchResEntry SearchResultEntry, + searchResDone SearchResultDone, + searchResRef SearchResultReference, + modifyRequest ModifyRequest, + modifyResponse ModifyResponse, + addRequest AddRequest, + addResponse AddResponse, + delRequest DelRequest, + delResponse DelResponse, + modDNRequest ModifyDNRequest, + modDNResponse ModifyDNResponse, + compareRequest CompareRequest, + compareResponse CompareResponse, + abandonRequest AbandonRequest, + extendedReq ExtendedRequest, + extendedResp ExtendedResponse }, + controls [0] Controls OPTIONAL } + +MessageID ::= INTEGER (0 .. maxInt) + +maxInt INTEGER ::= 2147483647 -- (2^^31 - 1) -- + +LDAPString ::= OCTET STRING + +LDAPOID ::= OCTET STRING + +LDAPDN ::= LDAPString + +RelativeLDAPDN ::= LDAPString + +AttributeType ::= LDAPString + +AttributeDescription ::= LDAPString + + + + +-- Wahl, et. al. Standards Track [Page 44] +-- +-- RFC 2251 LDAPv3 December 1997 + + +AttributeDescriptionList ::= SEQUENCE OF + AttributeDescription + +AttributeValue ::= OCTET STRING + +AttributeValueAssertion ::= SEQUENCE { + attributeDesc AttributeDescription, + assertionValue AssertionValue } + +AssertionValue ::= OCTET STRING + +Attribute ::= SEQUENCE { + type AttributeDescription, + vals SET OF AttributeValue } + +MatchingRuleId ::= LDAPString + +LDAPResult ::= SEQUENCE { + resultCode ENUMERATED { + success (0), + operationsError (1), + protocolError (2), + timeLimitExceeded (3), + sizeLimitExceeded (4), + compareFalse (5), + compareTrue (6), + authMethodNotSupported (7), + strongAuthRequired (8), + -- 9 reserved -- + referral (10), -- new + adminLimitExceeded (11), -- new + unavailableCriticalExtension (12), -- new + confidentialityRequired (13), -- new + saslBindInProgress (14), -- new + noSuchAttribute (16), + undefinedAttributeType (17), + inappropriateMatching (18), + constraintViolation (19), + attributeOrValueExists (20), + invalidAttributeSyntax (21), + -- 22-31 unused -- + noSuchObject (32), + aliasProblem (33), + invalidDNSyntax (34), + -- 35 reserved for undefined isLeaf -- + aliasDereferencingProblem (36), + -- 37-47 unused -- + inappropriateAuthentication (48), + +-- Wahl, et. al. Standards Track [Page 45] +-- +-- RFC 2251 LDAPv3 December 1997 + + + invalidCredentials (49), + insufficientAccessRights (50), + busy (51), + unavailable (52), + unwillingToPerform (53), + loopDetect (54), + -- 55-63 unused -- + namingViolation (64), + objectClassViolation (65), + notAllowedOnNonLeaf (66), + notAllowedOnRDN (67), + entryAlreadyExists (68), + objectClassModsProhibited (69), + -- 70 reserved for CLDAP -- + affectsMultipleDSAs (71), -- new + -- 72-79 unused -- + other (80) }, + -- 81-90 reserved for APIs -- + matchedDN LDAPDN, + errorMessage LDAPString, + referral [3] Referral OPTIONAL } + +Referral ::= SEQUENCE OF LDAPURL + +LDAPURL ::= LDAPString -- limited to characters permitted in URLs + +Controls ::= SEQUENCE OF Control + +Control ::= SEQUENCE { + controlType LDAPOID, + criticality BOOLEAN DEFAULT FALSE, + controlValue OCTET STRING OPTIONAL } + +BindRequest ::= [APPLICATION 0] SEQUENCE { + version INTEGER (1 .. 127), + name LDAPDN, + authentication AuthenticationChoice } + +AuthenticationChoice ::= CHOICE { + simple [0] OCTET STRING, + -- 1 and 2 reserved + sasl [3] SaslCredentials } + +SaslCredentials ::= SEQUENCE { + mechanism LDAPString, + credentials OCTET STRING OPTIONAL } + +BindResponse ::= [APPLICATION 1] SEQUENCE { + +-- Wahl, et. al. Standards Track [Page 46] +-- +-- RFC 2251 LDAPv3 December 1997 + + + COMPONENTS OF LDAPResult, + serverSaslCreds [7] OCTET STRING OPTIONAL } + +UnbindRequest ::= [APPLICATION 2] NULL + +SearchRequest ::= [APPLICATION 3] SEQUENCE { + baseObject LDAPDN, + scope ENUMERATED { + baseObject (0), + singleLevel (1), + wholeSubtree (2) }, + derefAliases ENUMERATED { + neverDerefAliases (0), + derefInSearching (1), + derefFindingBaseObj (2), + derefAlways (3) }, + sizeLimit INTEGER (0 .. maxInt), + timeLimit INTEGER (0 .. maxInt), + typesOnly BOOLEAN, + filter Filter, + attributes AttributeDescriptionList } + +Filter ::= CHOICE { + and [0] SET OF Filter, + or [1] SET OF Filter, + not [2] Filter, + equalityMatch [3] AttributeValueAssertion, + substrings [4] SubstringFilter, + greaterOrEqual [5] AttributeValueAssertion, + lessOrEqual [6] AttributeValueAssertion, + present [7] AttributeDescription, + approxMatch [8] AttributeValueAssertion, + extensibleMatch [9] MatchingRuleAssertion } + +SubstringFilter ::= SEQUENCE { + type AttributeDescription, + -- at least one must be present + substrings SEQUENCE OF CHOICE { + initial [0] LDAPString, + any [1] LDAPString, + final [2] LDAPString } } + +MatchingRuleAssertion ::= SEQUENCE { + matchingRule [1] MatchingRuleId OPTIONAL, + type [2] AttributeDescription OPTIONAL, + matchValue [3] AssertionValue, + dnAttributes [4] BOOLEAN DEFAULT FALSE } + +-- Wahl, et. al. Standards Track [Page 47] +-- +-- RFC 2251 LDAPv3 December 1997 + +SearchResultEntry ::= [APPLICATION 4] SEQUENCE { + objectName LDAPDN, + attributes PartialAttributeList } + +PartialAttributeList ::= SEQUENCE OF SEQUENCE { + type AttributeDescription, + vals SET OF AttributeValue } + +SearchResultReference ::= [APPLICATION 19] SEQUENCE OF LDAPURL + +SearchResultDone ::= [APPLICATION 5] LDAPResult + +ModifyRequest ::= [APPLICATION 6] SEQUENCE { + object LDAPDN, + modification SEQUENCE OF SEQUENCE { + operation ENUMERATED { + add (0), + delete (1), + replace (2) }, + modification AttributeTypeAndValues } } + +AttributeTypeAndValues ::= SEQUENCE { + type AttributeDescription, + vals SET OF AttributeValue } + +ModifyResponse ::= [APPLICATION 7] LDAPResult + +AddRequest ::= [APPLICATION 8] SEQUENCE { + entry LDAPDN, + attributes AttributeList } + +AttributeList ::= SEQUENCE OF SEQUENCE { + type AttributeDescription, + vals SET OF AttributeValue } + +AddResponse ::= [APPLICATION 9] LDAPResult + +DelRequest ::= [APPLICATION 10] LDAPDN + +DelResponse ::= [APPLICATION 11] LDAPResult + +ModifyDNRequest ::= [APPLICATION 12] SEQUENCE { + entry LDAPDN, + newrdn RelativeLDAPDN, + deleteoldrdn BOOLEAN, + newSuperior [0] LDAPDN OPTIONAL } + +ModifyDNResponse ::= [APPLICATION 13] LDAPResult + +-- Wahl, et. al. Standards Track [Page 48] +-- +-- RFC 2251 LDAPv3 December 1997 + + +CompareRequest ::= [APPLICATION 14] SEQUENCE { + entry LDAPDN, + ava AttributeValueAssertion } + +CompareResponse ::= [APPLICATION 15] LDAPResult + +AbandonRequest ::= [APPLICATION 16] MessageID + +ExtendedRequest ::= [APPLICATION 23] SEQUENCE { + requestName [0] LDAPOID, + requestValue [1] OCTET STRING OPTIONAL } + +ExtendedResponse ::= [APPLICATION 24] SEQUENCE { + COMPONENTS OF LDAPResult, + responseName [10] LDAPOID OPTIONAL, + response [11] OCTET STRING OPTIONAL } + +END + + diff --git a/rabbitmq-server/plugins-src/eldap-wrapper/eldap-git/src/Makefile b/rabbitmq-server/plugins-src/eldap-wrapper/eldap-git/src/Makefile new file mode 100644 index 0000000..dc15604 --- /dev/null +++ b/rabbitmq-server/plugins-src/eldap-wrapper/eldap-git/src/Makefile @@ -0,0 +1,26 @@ + +ERLC = erlc +EBIN_DIR = ../ebin +ERLC_FLAGS += -I ./src -I ../include +debug_info +ERL_OBJECTS := ${EBIN_DIR}/eldap.beam ${EBIN_DIR}/ELDAPv3.beam ${EBIN_DIR}/eldap_fsm.beam + +.SUFFIXES: .asn .erl .beam + +$(EBIN_DIR)/%.beam: %.erl + $(ERLC) $(ERLC_FLAGS) -o $(EBIN_DIR) $< + +.PHONY: all +all: asn $(ERL_OBJECTS) + +.PHONY: asn +asn: ELDAPv3.erl ../ebin/ELDAPv3.beam + +ELDAPv3.erl: ELDAPv3.asn + ${ERLC} ELDAPv3.asn + mv ELDAPv3.beam ${EBIN_DIR} + +.PHONY: clean +clean: + -rm $(ERL_OBJECTS) ELDAPv3.erl ELDAPv3.asn1db ELDAPv3.hrl + + diff --git a/rabbitmq-server/plugins-src/eldap-wrapper/eldap-git/src/eldap.erl b/rabbitmq-server/plugins-src/eldap-wrapper/eldap-git/src/eldap.erl new file mode 100644 index 0000000..b8422f2 --- /dev/null +++ b/rabbitmq-server/plugins-src/eldap-wrapper/eldap-git/src/eldap.erl @@ -0,0 +1,1078 @@ +-module(eldap). +%%% -------------------------------------------------------------------- +%%% Created: 12 Oct 2000 by Tobbe +%%% Function: Erlang client LDAP implementation according RFC 2251,2253 +%%% and 2255. The interface is based on RFC 1823, and +%%% draft-ietf-asid-ldap-c-api-00.txt +%%% +%%% Copyright (c) 2010 Torbjorn Tornkvist +%%% See MIT-LICENSE at the top dir for licensing information. +%%% -------------------------------------------------------------------- +-vc('$Id$ '). +-export([open/1,open/2,simple_bind/3,controlling_process/2, + baseObject/0,singleLevel/0,wholeSubtree/0,close/1, + equalityMatch/2,greaterOrEqual/2,lessOrEqual/2, + approxMatch/2,search/2,substrings/2,present/1, + 'and'/1,'or'/1,'not'/1,modify/3, mod_add/2, mod_delete/2, + mod_replace/2, add/3, delete/2, modify_dn/5,parse_dn/1, + parse_ldap_url/1]). + +-import(lists,[concat/1]). + +-include("ELDAPv3.hrl"). +-include("eldap.hrl"). + +-define(LDAP_VERSION, 3). +-define(LDAP_PORT, 389). +-define(LDAPS_PORT, 636). + +-record(eldap, {version = ?LDAP_VERSION, + host, % Host running LDAP server + port = ?LDAP_PORT, % The LDAP server port + fd, % Socket filedescriptor. + binddn = "", % Name of the entry to bind as + passwd, % Password for (above) entry + id = 0, % LDAP Request ID + log, % User provided log function + timeout = infinity, % Request timeout + anon_auth = false, % Allow anonymous authentication + use_tls = false % LDAP/LDAPS + }). + +%%% For debug purposes +%%-define(PRINT(S, A), io:fwrite("~w(~w): " ++ S, [?MODULE,?LINE|A])). +-define(PRINT(S, A), true). + +-define(elog(S, A), error_logger:info_msg("~w(~w): "++S,[?MODULE,?LINE|A])). + +%%% ==================================================================== +%%% Exported interface +%%% ==================================================================== + +%%% -------------------------------------------------------------------- +%%% open(Hosts [,Opts] ) +%%% -------------------- +%%% Setup a connection to on of the Hosts in the argument +%%% list. Stop at the first successful connection attempt. +%%% Valid Opts are: Where: +%%% +%%% {port, Port} - Port is the port number +%%% {log, F} - F(LogLevel, FormatString, ListOfArgs) +%%% {timeout, milliSec} - request timeout +%%% +%%% -------------------------------------------------------------------- +open(Hosts) -> + open(Hosts, []). + +open(Hosts, Opts) when list(Hosts), list(Opts) -> + Self = self(), + Pid = spawn_link(fun() -> init(Hosts, Opts, Self) end), + recv(Pid). + +%%% -------------------------------------------------------------------- +%%% Shutdown connection (and process) asynchronous. +%%% -------------------------------------------------------------------- + +close(Handle) when pid(Handle) -> + send(Handle, close). + +%%% -------------------------------------------------------------------- +%%% Set who we should link ourselves to +%%% -------------------------------------------------------------------- + +controlling_process(Handle, Pid) when pid(Handle),pid(Pid) -> + link(Pid), + send(Handle, {cnt_proc, Pid}), + recv(Handle). + +%%% -------------------------------------------------------------------- +%%% Authenticate ourselves to the Directory +%%% using simple authentication. +%%% +%%% Dn - The name of the entry to bind as +%%% Passwd - The password to be used +%%% +%%% Returns: ok | {error, Error} +%%% -------------------------------------------------------------------- +simple_bind(Handle, Dn, Passwd) when pid(Handle) -> + send(Handle, {simple_bind, Dn, Passwd}), + recv(Handle). + +%%% -------------------------------------------------------------------- +%%% Add an entry. The entry field MUST NOT exist for the AddRequest +%%% to succeed. The parent of the entry MUST exist. +%%% Example: +%%% +%%% add(Handle, +%%% "cn=Bill Valentine, ou=people, o=Bluetail AB, dc=bluetail, dc=com", +%%% [{"objectclass", ["person"]}, +%%% {"cn", ["Bill Valentine"]}, +%%% {"sn", ["Valentine"]}, +%%% {"telephoneNumber", ["545 555 00"]}] +%%% ) +%%% -------------------------------------------------------------------- +add(Handle, Entry, Attributes) when pid(Handle),list(Entry),list(Attributes) -> + send(Handle, {add, Entry, add_attrs(Attributes)}), + recv(Handle). + +%%% Do sanity check ! +add_attrs(Attrs) -> + F = fun({Type,Vals}) when list(Type),list(Vals) -> + %% Confused ? Me too... :-/ + {'AddRequest_attributes',Type, Vals} + end, + case catch lists:map(F, Attrs) of + {'EXIT', _} -> throw({error, attribute_values}); + Else -> Else + end. + +%%% -------------------------------------------------------------------- +%%% Delete an entry. The entry consists of the DN of +%%% the entry to be deleted. +%%% Example: +%%% +%%% delete(Handle, +%%% "cn=Bill Valentine, ou=people, o=Bluetail AB, dc=bluetail, dc=com" +%%% ) +%%% -------------------------------------------------------------------- +delete(Handle, Entry) when pid(Handle), list(Entry) -> + send(Handle, {delete, Entry}), + recv(Handle). + +%%% -------------------------------------------------------------------- +%%% Modify an entry. Given an entry a number of modification +%%% operations can be performed as one atomic operation. +%%% Example: +%%% +%%% modify(Handle, +%%% "cn=Torbjorn Tornkvist, ou=people, o=Bluetail AB, dc=bluetail, dc=com", +%%% [replace("telephoneNumber", ["555 555 00"]), +%%% add("description", ["LDAP hacker"])] +%%% ) +%%% -------------------------------------------------------------------- +modify(Handle, Object, Mods) when pid(Handle), list(Object), list(Mods) -> + send(Handle, {modify, Object, Mods}), + recv(Handle). + +%%% +%%% Modification operations. +%%% Example: +%%% replace("telephoneNumber", ["555 555 00"]) +%%% +mod_add(Type, Values) when list(Type), list(Values) -> m(add, Type, Values). +mod_delete(Type, Values) when list(Type), list(Values) -> m(delete, Type, Values). +mod_replace(Type, Values) when list(Type), list(Values) -> m(replace, Type, Values). + +m(Operation, Type, Values) -> + #'ModifyRequest_modification_SEQOF'{ + operation = Operation, + modification = #'AttributeTypeAndValues'{ + type = Type, + vals = Values}}. + +%%% -------------------------------------------------------------------- +%%% Modify an entry. Given an entry a number of modification +%%% operations can be performed as one atomic operation. +%%% Example: +%%% +%%% modify_dn(Handle, +%%% "cn=Bill Valentine, ou=people, o=Bluetail AB, dc=bluetail, dc=com", +%%% "cn=Ben Emerson", +%%% true, +%%% "" +%%% ) +%%% -------------------------------------------------------------------- +modify_dn(Handle, Entry, NewRDN, DelOldRDN, NewSup) + when pid(Handle),list(Entry),list(NewRDN),atom(DelOldRDN),list(NewSup) -> + send(Handle, {modify_dn, Entry, NewRDN, + bool_p(DelOldRDN), optional(NewSup)}), + recv(Handle). + +%%% Sanity checks ! + +bool_p(Bool) when Bool==true;Bool==false -> Bool. + +optional([]) -> asn1_NOVALUE; +optional(Value) -> Value. + +%%% -------------------------------------------------------------------- +%%% Synchronous search of the Directory returning a +%%% requested set of attributes. +%%% +%%% Example: +%%% +%%% Filter = eldap:substrings("sn", [{any,"o"}]), +%%% eldap:search(S, [{base, "dc=bluetail, dc=com"}, +%%% {filter, Filter}, +%%% {attributes,["cn"]}])), +%%% +%%% Returned result: {ok, #eldap_search_result{}} +%%% +%%% Example: +%%% +%%% {ok,{eldap_search_result, +%%% [{eldap_entry, +%%% "cn=Magnus Froberg, dc=bluetail, dc=com", +%%% [{"cn",["Magnus Froberg"]}]}, +%%% {eldap_entry, +%%% "cn=Torbjorn Tornkvist, dc=bluetail, dc=com", +%%% [{"cn",["Torbjorn Tornkvist"]}]}], +%%% []}} +%%% +%%% -------------------------------------------------------------------- +search(Handle, A) when pid(Handle), record(A, eldap_search) -> + call_search(Handle, A); +search(Handle, L) when pid(Handle), list(L) -> + case catch parse_search_args(L) of + {error, Emsg} -> {error, Emsg}; + A when record(A, eldap_search) -> call_search(Handle, A) + end. + +call_search(Handle, A) -> + send(Handle, {search, A}), + recv(Handle). + +parse_search_args(Args) -> + parse_search_args(Args, #eldap_search{scope = wholeSubtree}). + +parse_search_args([{base, Base}|T],A) -> + parse_search_args(T,A#eldap_search{base = Base}); +parse_search_args([{filter, Filter}|T],A) -> + parse_search_args(T,A#eldap_search{filter = Filter}); +parse_search_args([{scope, Scope}|T],A) -> + parse_search_args(T,A#eldap_search{scope = Scope}); +parse_search_args([{attributes, Attrs}|T],A) -> + parse_search_args(T,A#eldap_search{attributes = Attrs}); +parse_search_args([{types_only, TypesOnly}|T],A) -> + parse_search_args(T,A#eldap_search{types_only = TypesOnly}); +parse_search_args([{timeout, Timeout}|T],A) when integer(Timeout) -> + parse_search_args(T,A#eldap_search{timeout = Timeout}); +parse_search_args([H|_],_) -> + throw({error,{unknown_arg, H}}); +parse_search_args([],A) -> + A. + +%%% +%%% The Scope parameter +%%% +baseObject() -> baseObject. +singleLevel() -> singleLevel. +wholeSubtree() -> wholeSubtree. + +%%% +%%% Boolean filter operations +%%% +'and'(ListOfFilters) when list(ListOfFilters) -> {'and',ListOfFilters}. +'or'(ListOfFilters) when list(ListOfFilters) -> {'or', ListOfFilters}. +'not'(Filter) when tuple(Filter) -> {'not',Filter}. + +%%% +%%% The following Filter parameters consist of an attribute +%%% and an attribute value. Example: F("uid","tobbe") +%%% +equalityMatch(Desc, Value) -> {equalityMatch, av_assert(Desc, Value)}. +greaterOrEqual(Desc, Value) -> {greaterOrEqual, av_assert(Desc, Value)}. +lessOrEqual(Desc, Value) -> {lessOrEqual, av_assert(Desc, Value)}. +approxMatch(Desc, Value) -> {approxMatch, av_assert(Desc, Value)}. + +av_assert(Desc, Value) -> + #'AttributeValueAssertion'{attributeDesc = Desc, + assertionValue = Value}. + +%%% +%%% Filter to check for the presence of an attribute +%%% +present(Attribute) when list(Attribute) -> + {present, Attribute}. + + +%%% +%%% A substring filter seem to be based on a pattern: +%%% +%%% InitValue*AnyValue*FinalValue +%%% +%%% where all three parts seem to be optional (at least when +%%% talking with an OpenLDAP server). Thus, the arguments +%%% to substrings/2 looks like this: +%%% +%%% Type ::= string( ) +%%% SubStr ::= listof( {initial,Value} | {any,Value}, {final,Value}) +%%% +%%% Example: substrings("sn",[{initial,"To"},{any,"kv"},{final,"st"}]) +%%% will match entries containing: 'sn: Tornkvist' +%%% +substrings(Type, SubStr) when list(Type), list(SubStr) -> + Ss = {'SubstringFilter_substrings',v_substr(SubStr)}, + {substrings,#'SubstringFilter'{type = Type, + substrings = Ss}}. + +%%% -------------------------------------------------------------------- +%%% Worker process. We keep track of a controlling process to +%%% be able to terminate together with it. +%%% -------------------------------------------------------------------- + +init(Hosts, Opts, Cpid) -> + Data = parse_args(Opts, Cpid, #eldap{}), + case try_connect(Hosts, Data) of + {ok,Data2} -> + send(Cpid, {ok,self()}), + put(req_timeout, Data#eldap.timeout), % kludge... + loop(Cpid, Data2); + Else -> + send(Cpid, Else), + unlink(Cpid), + exit(Else) + end. + +parse_args([{port, Port}|T], Cpid, Data) when integer(Port) -> + parse_args(T, Cpid, Data#eldap{port = Port}); +parse_args([{timeout, Timeout}|T], Cpid, Data) when integer(Timeout),Timeout>0 -> + parse_args(T, Cpid, Data#eldap{timeout = Timeout}); +parse_args([{anon_auth, true}|T], Cpid, Data) -> + parse_args(T, Cpid, Data#eldap{anon_auth = false}); +parse_args([{anon_auth, _}|T], Cpid, Data) -> + parse_args(T, Cpid, Data); +parse_args([{ssl, true}|T], Cpid, Data) -> + parse_args(T, Cpid, Data#eldap{use_tls = true}); +parse_args([{ssl, _}|T], Cpid, Data) -> + parse_args(T, Cpid, Data); +parse_args([{log, F}|T], Cpid, Data) when function(F) -> + parse_args(T, Cpid, Data#eldap{log = F}); +parse_args([{log, _}|T], Cpid, Data) -> + parse_args(T, Cpid, Data); +parse_args([H|_], Cpid, _) -> + send(Cpid, {error,{wrong_option,H}}), + exit(wrong_option); +parse_args([], _, Data) -> + Data. + +%%% Try to connect to the hosts in the listed order, +%%% and stop with the first one to which a successful +%%% connection is made. + +try_connect([Host|Hosts], Data) -> + TcpOpts = [{packet, asn1}, {active,false}], + case do_connect(Host, Data, TcpOpts) of + {ok,Fd} -> {ok,Data#eldap{host = Host, fd = Fd}}; + _ -> try_connect(Hosts, Data) + end; +try_connect([],_) -> + {error,"connect failed"}. + +do_connect(Host, Data, Opts) when Data#eldap.use_tls == false -> + gen_tcp:connect(Host, Data#eldap.port, Opts, Data#eldap.timeout); +do_connect(Host, Data, Opts) when Data#eldap.use_tls == true -> + ssl:connect(Host, Data#eldap.port, [{verify,0}|Opts]). + + +loop(Cpid, Data) -> + receive + + {From, {search, A}} -> + {Res,NewData} = do_search(Data, A), + send(From,Res), + loop(Cpid, NewData); + + {From, {modify, Obj, Mod}} -> + {Res,NewData} = do_modify(Data, Obj, Mod), + send(From,Res), + loop(Cpid, NewData); + + {From, {modify_dn, Obj, NewRDN, DelOldRDN, NewSup}} -> + {Res,NewData} = do_modify_dn(Data, Obj, NewRDN, DelOldRDN, NewSup), + send(From,Res), + loop(Cpid, NewData); + + {From, {add, Entry, Attrs}} -> + {Res,NewData} = do_add(Data, Entry, Attrs), + send(From,Res), + loop(Cpid, NewData); + + {From, {delete, Entry}} -> + {Res,NewData} = do_delete(Data, Entry), + send(From,Res), + loop(Cpid, NewData); + + {From, {simple_bind, Dn, Passwd}} -> + {Res,NewData} = do_simple_bind(Data, Dn, Passwd), + send(From,Res), + loop(Cpid, NewData); + + {From, {cnt_proc, NewCpid}} -> + unlink(Cpid), + send(From,ok), + ?PRINT("New Cpid is: ~p~n",[NewCpid]), + loop(NewCpid, Data); + + {From, close} -> + unlink(Cpid), + exit(closed); + + {Cpid, 'EXIT', Reason} -> + ?PRINT("Got EXIT from Cpid, reason=~p~n",[Reason]), + exit(Reason); + + _XX -> + ?PRINT("loop got: ~p~n",[_XX]), + loop(Cpid, Data) + + end. + +%%% -------------------------------------------------------------------- +%%% bindRequest +%%% -------------------------------------------------------------------- + +%%% Authenticate ourselves to the directory using +%%% simple authentication. + +do_simple_bind(Data, anon, anon) -> %% For testing + do_the_simple_bind(Data, "", ""); +do_simple_bind(Data, Dn, _Passwd) when Dn=="",Data#eldap.anon_auth==false -> + {{error,anonymous_auth},Data}; +do_simple_bind(Data, _Dn, Passwd) when Passwd=="",Data#eldap.anon_auth==false -> + {{error,anonymous_auth},Data}; +do_simple_bind(Data, Dn, Passwd) -> + do_the_simple_bind(Data, Dn, Passwd). + +do_the_simple_bind(Data, Dn, Passwd) -> + case catch exec_simple_bind(Data#eldap{binddn = Dn, + passwd = Passwd, + id = bump_id(Data)}) of + {ok,NewData} -> {ok,NewData}; + {error,Emsg} -> {{error,Emsg},Data}; + Else -> {{error,Else},Data} + end. + +exec_simple_bind(Data) -> + Req = #'BindRequest'{version = Data#eldap.version, + name = Data#eldap.binddn, + authentication = {simple, Data#eldap.passwd}}, + log2(Data, "bind request = ~p~n", [Req]), + Reply = request(Data#eldap.fd, Data, Data#eldap.id, {bindRequest, Req}), + log2(Data, "bind reply = ~p~n", [Reply]), + exec_simple_bind_reply(Data, Reply). + +exec_simple_bind_reply(Data, {ok,Msg}) when + Msg#'LDAPMessage'.messageID == Data#eldap.id -> + case Msg#'LDAPMessage'.protocolOp of + {bindResponse, Result} -> + case Result#'BindResponse'.resultCode of + success -> {ok,Data}; + Error -> {error, Error} + end; + Other -> {error, Other} + end; +exec_simple_bind_reply(_, Error) -> + {error, Error}. + + +%%% -------------------------------------------------------------------- +%%% searchRequest +%%% -------------------------------------------------------------------- + +do_search(Data, A) -> + case catch do_search_0(Data, A) of + {error,Emsg} -> {ldap_closed_p(Data, Emsg),Data}; + {'EXIT',Error} -> {ldap_closed_p(Data, Error),Data}; + {ok,Res,Ref,NewData} -> {{ok,polish(Res, Ref)},NewData}; + Else -> {ldap_closed_p(Data, Else),Data} + end. + +%%% +%%% Polish the returned search result +%%% + +polish(Res, Ref) -> + R = polish_result(Res), + %%% No special treatment of referrals at the moment. + #eldap_search_result{entries = R, + referrals = Ref}. + +polish_result([H|T]) when record(H, 'SearchResultEntry') -> + ObjectName = H#'SearchResultEntry'.objectName, + F = fun({_,A,V}) -> {A,V} end, + Attrs = lists:map(F, H#'SearchResultEntry'.attributes), + [#eldap_entry{object_name = ObjectName, + attributes = Attrs}| + polish_result(T)]; +polish_result([]) -> + []. + +do_search_0(Data, A) -> + Req = #'SearchRequest'{baseObject = A#eldap_search.base, + scope = v_scope(A#eldap_search.scope), + derefAliases = neverDerefAliases, + sizeLimit = 0, % no size limit + timeLimit = v_timeout(A#eldap_search.timeout), + typesOnly = v_bool(A#eldap_search.types_only), + filter = v_filter(A#eldap_search.filter), + attributes = v_attributes(A#eldap_search.attributes) + }, + Id = bump_id(Data), + collect_search_responses(Data#eldap{id=Id}, Req, Id). + +%%% The returned answers cames in one packet per entry +%%% mixed with possible referals + +collect_search_responses(Data, Req, ID) -> + S = Data#eldap.fd, + log2(Data, "search request = ~p~n", [Req]), + send_request(S, Data, ID, {searchRequest, Req}), + Resp = recv_response(S, Data), + log2(Data, "search reply = ~p~n", [Resp]), + collect_search_responses(Data, S, ID, Resp, [], []). + +collect_search_responses(Data, S, ID, {ok,Msg}, Acc, Ref) + when record(Msg,'LDAPMessage') -> + case Msg#'LDAPMessage'.protocolOp of + {'searchResDone',R} when R#'LDAPResult'.resultCode == success -> + log2(Data, "search reply = searchResDone ~n", []), + {ok,Acc,Ref,Data}; + {'searchResEntry',R} when record(R,'SearchResultEntry') -> + Resp = recv_response(S, Data), + log2(Data, "search reply = ~p~n", [Resp]), + collect_search_responses(Data, S, ID, Resp, [R|Acc], Ref); + {'searchResRef',R} -> + %% At the moment we don't do anyting sensible here since + %% I haven't been able to trigger the server to generate + %% a response like this. + Resp = recv_response(S, Data), + log2(Data, "search reply = ~p~n", [Resp]), + collect_search_responses(Data, S, ID, Resp, Acc, [R|Ref]); + Else -> + throw({error,Else}) + end; +collect_search_responses(_, _, _, Else, _, _) -> + throw({error,Else}). + +%%% -------------------------------------------------------------------- +%%% addRequest +%%% -------------------------------------------------------------------- + +do_add(Data, Entry, Attrs) -> + case catch do_add_0(Data, Entry, Attrs) of + {error,Emsg} -> {ldap_closed_p(Data, Emsg),Data}; + {'EXIT',Error} -> {ldap_closed_p(Data, Error),Data}; + {ok,NewData} -> {ok,NewData}; + Else -> {ldap_closed_p(Data, Else),Data} + end. + +do_add_0(Data, Entry, Attrs) -> + Req = #'AddRequest'{entry = Entry, + attributes = Attrs}, + S = Data#eldap.fd, + Id = bump_id(Data), + log2(Data, "add request = ~p~n", [Req]), + Resp = request(S, Data, Id, {addRequest, Req}), + log2(Data, "add reply = ~p~n", [Resp]), + check_reply(Data#eldap{id = Id}, Resp, addResponse). + + +%%% -------------------------------------------------------------------- +%%% deleteRequest +%%% -------------------------------------------------------------------- + +do_delete(Data, Entry) -> + case catch do_delete_0(Data, Entry) of + {error,Emsg} -> {ldap_closed_p(Data, Emsg),Data}; + {'EXIT',Error} -> {ldap_closed_p(Data, Error),Data}; + {ok,NewData} -> {ok,NewData}; + Else -> {ldap_closed_p(Data, Else),Data} + end. + +do_delete_0(Data, Entry) -> + S = Data#eldap.fd, + Id = bump_id(Data), + log2(Data, "del request = ~p~n", [Entry]), + Resp = request(S, Data, Id, {delRequest, Entry}), + log2(Data, "del reply = ~p~n", [Resp]), + check_reply(Data#eldap{id = Id}, Resp, delResponse). + + +%%% -------------------------------------------------------------------- +%%% modifyRequest +%%% -------------------------------------------------------------------- + +do_modify(Data, Obj, Mod) -> + case catch do_modify_0(Data, Obj, Mod) of + {error,Emsg} -> {ldap_closed_p(Data, Emsg),Data}; + {'EXIT',Error} -> {ldap_closed_p(Data, Error),Data}; + {ok,NewData} -> {ok,NewData}; + Else -> {ldap_closed_p(Data, Else),Data} + end. + +do_modify_0(Data, Obj, Mod) -> + v_modifications(Mod), + Req = #'ModifyRequest'{object = Obj, + modification = Mod}, + S = Data#eldap.fd, + Id = bump_id(Data), + log2(Data, "modify request = ~p~n", [Req]), + Resp = request(S, Data, Id, {modifyRequest, Req}), + log2(Data, "modify reply = ~p~n", [Resp]), + check_reply(Data#eldap{id = Id}, Resp, modifyResponse). + +%%% -------------------------------------------------------------------- +%%% modifyDNRequest +%%% -------------------------------------------------------------------- + +do_modify_dn(Data, Entry, NewRDN, DelOldRDN, NewSup) -> + case catch do_modify_dn_0(Data, Entry, NewRDN, DelOldRDN, NewSup) of + {error,Emsg} -> {ldap_closed_p(Data, Emsg),Data}; + {'EXIT',Error} -> {ldap_closed_p(Data, Error),Data}; + {ok,NewData} -> {ok,NewData}; + Else -> {ldap_closed_p(Data, Else),Data} + end. + +do_modify_dn_0(Data, Entry, NewRDN, DelOldRDN, NewSup) -> + Req = #'ModifyDNRequest'{entry = Entry, + newrdn = NewRDN, + deleteoldrdn = DelOldRDN, + newSuperior = NewSup}, + S = Data#eldap.fd, + Id = bump_id(Data), + log2(Data, "modify DN request = ~p~n", [Req]), + Resp = request(S, Data, Id, {modDNRequest, Req}), + log2(Data, "modify DN reply = ~p~n", [Resp]), + check_reply(Data#eldap{id = Id}, Resp, modDNResponse). + +%%% -------------------------------------------------------------------- +%%% Send an LDAP request and receive the answer +%%% -------------------------------------------------------------------- + +request(S, Data, ID, Request) -> + send_request(S, Data, ID, Request), + recv_response(S, Data). + +send_request(S, Data, ID, Request) -> + Message = #'LDAPMessage'{messageID = ID, + protocolOp = Request}, + {ok,Bytes} = asn1rt:encode('ELDAPv3', 'LDAPMessage', Message), + case do_send(S, Data, Bytes) of + {error,Reason} -> throw({gen_tcp_error,Reason}); + Else -> Else + end. + +do_send(S, Data, Bytes) when Data#eldap.use_tls == false -> + gen_tcp:send(S, Bytes); +do_send(S, Data, Bytes) when Data#eldap.use_tls == true -> + ssl:send(S, Bytes). + +do_recv(S, Data, Len, Timeout) when Data#eldap.use_tls == false -> + gen_tcp:recv(S, Len, Timeout); +do_recv(S, Data, Len, Timeout) when Data#eldap.use_tls == true -> + ssl:recv(S, Len, Timeout). + +recv_response(S, Data) -> + Timeout = get(req_timeout), % kludge... + case do_recv(S, Data, 0, Timeout) of + {ok, Packet} -> + check_tag(Packet), + case asn1rt:decode('ELDAPv3', 'LDAPMessage', Packet) of + {ok,Resp} -> {ok,Resp}; + Error -> throw(Error) + end; + {error,Reason} -> + throw({gen_tcp_error, Reason}); + Error -> + throw(Error) + end. + +%%% Sanity check of received packet +check_tag(Data) -> + case asn1rt_ber_bin:decode_tag(b2l(Data)) of + {_Tag, Data1, _Rb} -> + case asn1rt_ber_bin:decode_length(b2l(Data1)) of + {{_Len, _Data2}, _Rb2} -> ok; + _ -> throw({error,decoded_tag_length}) + end; + _ -> throw({error,decoded_tag}) + end. + +%%% Check for expected kind of reply +check_reply(Data, {ok,Msg}, Op) when + Msg#'LDAPMessage'.messageID == Data#eldap.id -> + case Msg#'LDAPMessage'.protocolOp of + {Op, Result} -> + case Result#'LDAPResult'.resultCode of + success -> {ok,Data}; + Error -> {error, Error} + end; + Other -> {error, Other} + end; +check_reply(_, Error, _) -> + {error, Error}. + + +%%% -------------------------------------------------------------------- +%%% Verify the input data +%%% -------------------------------------------------------------------- + +v_filter({'and',L}) -> {'and',L}; +v_filter({'or', L}) -> {'or',L}; +v_filter({'not',L}) -> {'not',L}; +v_filter({equalityMatch,AV}) -> {equalityMatch,AV}; +v_filter({greaterOrEqual,AV}) -> {greaterOrEqual,AV}; +v_filter({lessOrEqual,AV}) -> {lessOrEqual,AV}; +v_filter({approxMatch,AV}) -> {approxMatch,AV}; +v_filter({present,A}) -> {present,A}; +v_filter({substrings,S}) when record(S,'SubstringFilter') -> {substrings,S}; +v_filter(_Filter) -> throw({error,concat(["unknown filter: ",_Filter])}). + +v_modifications(Mods) -> + F = fun({_,Op,_}) -> + case lists:member(Op,[add,delete,replace]) of + true -> true; + _ -> throw({error,{mod_operation,Op}}) + end + end, + lists:foreach(F, Mods). + +v_substr([{Key,Str}|T]) when list(Str),Key==initial;Key==any;Key==final -> + [{Key,Str}|v_substr(T)]; +v_substr([H|_]) -> + throw({error,{substring_arg,H}}); +v_substr([]) -> + []. +v_scope(baseObject) -> baseObject; +v_scope(singleLevel) -> singleLevel; +v_scope(wholeSubtree) -> wholeSubtree; +v_scope(_Scope) -> throw({error,concat(["unknown scope: ",_Scope])}). + +v_bool(true) -> true; +v_bool(false) -> false; +v_bool(_Bool) -> throw({error,concat(["not Boolean: ",_Bool])}). + +v_timeout(I) when integer(I), I>=0 -> I; +v_timeout(_I) -> throw({error,concat(["timeout not positive integer: ",_I])}). + +v_attributes(Attrs) -> + F = fun(A) when list(A) -> A; + (A) -> throw({error,concat(["attribute not String: ",A])}) + end, + lists:map(F,Attrs). + + +%%% -------------------------------------------------------------------- +%%% Log routines. Call a user provided log routine F. +%%% -------------------------------------------------------------------- + +log1(Data, Str, Args) -> log(Data, Str, Args, 1). +log2(Data, Str, Args) -> log(Data, Str, Args, 2). + +log(Data, Str, Args, Level) when function(Data#eldap.log) -> + catch (Data#eldap.log)(Level, Str, Args); +log(_, _, _, _) -> + ok. + + +%%% -------------------------------------------------------------------- +%%% Misc. routines +%%% -------------------------------------------------------------------- + +send(To,Msg) -> To ! {self(),Msg}. +recv(From) -> receive {From,Msg} -> Msg end. + +ldap_closed_p(Data, Emsg) when Data#eldap.use_tls == true -> + %% Check if the SSL socket seems to be alive or not + case catch ssl:sockname(Data#eldap.fd) of + {error, _} -> + ssl:close(Data#eldap.fd), + {error, ldap_closed}; + {ok, _} -> + {error, Emsg}; + _ -> + %% sockname crashes if the socket pid is not alive + {error, ldap_closed} + end; +ldap_closed_p(Data, Emsg) -> + %% non-SSL socket + case inet:port(Data#eldap.fd) of + {error,_} -> {error, ldap_closed}; + _ -> {error,Emsg} + end. + +bump_id(Data) -> Data#eldap.id + 1. + + +%%% -------------------------------------------------------------------- +%%% parse_dn/1 - Implementation of RFC 2253: +%%% +%%% "UTF-8 String Representation of Distinguished Names" +%%% +%%% Test cases: +%%% +%%% The simplest case: +%%% +%%% 1> eldap:parse_dn("CN=Steve Kille,O=Isode Limited,C=GB"). +%%% {ok,[[{attribute_type_and_value,"CN","Steve Kille"}], +%%% [{attribute_type_and_value,"O","Isode Limited"}], +%%% [{attribute_type_and_value,"C","GB"}]]} +%%% +%%% The first RDN is multi-valued: +%%% +%%% 2> eldap:parse_dn("OU=Sales+CN=J. Smith,O=Widget Inc.,C=US"). +%%% {ok,[[{attribute_type_and_value,"OU","Sales"}, +%%% {attribute_type_and_value,"CN","J. Smith"}], +%%% [{attribute_type_and_value,"O","Widget Inc."}], +%%% [{attribute_type_and_value,"C","US"}]]} +%%% +%%% Quoting a comma: +%%% +%%% 3> eldap:parse_dn("CN=L. Eagle,O=Sue\\, Grabbit and Runn,C=GB"). +%%% {ok,[[{attribute_type_and_value,"CN","L. Eagle"}], +%%% [{attribute_type_and_value,"O","Sue\\, Grabbit and Runn"}], +%%% [{attribute_type_and_value,"C","GB"}]]} +%%% +%%% A value contains a carriage return: +%%% +%%% 4> eldap:parse_dn("CN=Before +%%% 4> After,O=Test,C=GB"). +%%% {ok,[[{attribute_type_and_value,"CN","Before\nAfter"}], +%%% [{attribute_type_and_value,"O","Test"}], +%%% [{attribute_type_and_value,"C","GB"}]]} +%%% +%%% 5> eldap:parse_dn("CN=Before\\0DAfter,O=Test,C=GB"). +%%% {ok,[[{attribute_type_and_value,"CN","Before\\0DAfter"}], +%%% [{attribute_type_and_value,"O","Test"}], +%%% [{attribute_type_and_value,"C","GB"}]]} +%%% +%%% An RDN in OID form: +%%% +%%% 6> eldap:parse_dn("1.3.6.1.4.1.1466.0=#04024869,O=Test,C=GB"). +%%% {ok,[[{attribute_type_and_value,"1.3.6.1.4.1.1466.0","#04024869"}], +%%% [{attribute_type_and_value,"O","Test"}], +%%% [{attribute_type_and_value,"C","GB"}]]} +%%% +%%% +%%% -------------------------------------------------------------------- + +parse_dn("") -> % empty DN string + {ok,[]}; +parse_dn([H|_] = Str) when H=/=$, -> % 1:st name-component ! + case catch parse_name(Str,[]) of + {'EXIT',Reason} -> {parse_error,internal_error,Reason}; + Else -> Else + end. + +parse_name("",Acc) -> + {ok,lists:reverse(Acc)}; +parse_name([$,|T],Acc) -> % N:th name-component ! + parse_name(T,Acc); +parse_name(Str,Acc) -> + {Rest,NameComponent} = parse_name_component(Str), + parse_name(Rest,[NameComponent|Acc]). + +parse_name_component(Str) -> + parse_name_component(Str,[]). + +parse_name_component(Str,Acc) -> + case parse_attribute_type_and_value(Str) of + {[$+|Rest], ATV} -> + parse_name_component(Rest,[ATV|Acc]); + {Rest,ATV} -> + {Rest,lists:reverse([ATV|Acc])} + end. + +parse_attribute_type_and_value(Str) -> + case parse_attribute_type(Str) of + {Rest,[]} -> + error(expecting_attribute_type,Str); + {Rest,Type} -> + Rest2 = parse_equal_sign(Rest), + {Rest3,Value} = parse_attribute_value(Rest2), + {Rest3,{attribute_type_and_value,Type,Value}} + end. + +-define(IS_ALPHA(X) , X>=$a,X=<$z;X>=$A,X=<$Z ). +-define(IS_DIGIT(X) , X>=$0,X=<$9 ). +-define(IS_SPECIAL(X) , X==$,;X==$=;X==$+;X==$<;X==$>;X==$#;X==$; ). +-define(IS_QUOTECHAR(X) , X=/=$\\,X=/=$" ). +-define(IS_STRINGCHAR(X) , + X=/=$,,X=/=$=,X=/=$+,X=/=$<,X=/=$>,X=/=$#,X=/=$;,?IS_QUOTECHAR(X) ). +-define(IS_HEXCHAR(X) , ?IS_DIGIT(X);X>=$a,X=<$f;X>=$A,X=<$F ). + +parse_attribute_type([H|T]) when ?IS_ALPHA(H) -> + %% NB: It must be an error in the RFC in the definition + %% of 'attributeType', should be: (ALPHA *keychar) + {Rest,KeyChars} = parse_keychars(T), + {Rest,[H|KeyChars]}; +parse_attribute_type([H|_] = Str) when ?IS_DIGIT(H) -> + parse_oid(Str); +parse_attribute_type(Str) -> + error(invalid_attribute_type,Str). + + + +%%% Is a hexstring ! +parse_attribute_value([$#,X,Y|T]) when ?IS_HEXCHAR(X),?IS_HEXCHAR(Y) -> + {Rest,HexString} = parse_hexstring(T), + {Rest,[$#,X,Y|HexString]}; +%%% Is a "quotation-sequence" ! +parse_attribute_value([$"|T]) -> + {Rest,Quotation} = parse_quotation(T), + {Rest,[$"|Quotation]}; +%%% Is a stringchar , pair or Empty ! +parse_attribute_value(Str) -> + parse_string(Str). + +parse_hexstring(Str) -> + parse_hexstring(Str,[]). + +parse_hexstring([X,Y|T],Acc) when ?IS_HEXCHAR(X),?IS_HEXCHAR(Y) -> + parse_hexstring(T,[Y,X|Acc]); +parse_hexstring(T,Acc) -> + {T,lists:reverse(Acc)}. + +parse_quotation([$"|T]) -> % an empty: "" is ok ! + {T,[$"]}; +parse_quotation(Str) -> + parse_quotation(Str,[]). + +%%% Parse to end of quotation +parse_quotation([$"|T],Acc) -> + {T,lists:reverse([$"|Acc])}; +parse_quotation([X|T],Acc) when ?IS_QUOTECHAR(X) -> + parse_quotation(T,[X|Acc]); +parse_quotation([$\\,X|T],Acc) when ?IS_SPECIAL(X) -> + parse_quotation(T,[X,$\\|Acc]); +parse_quotation([$\\,$\\|T],Acc) -> + parse_quotation(T,[$\\,$\\|Acc]); +parse_quotation([$\\,$"|T],Acc) -> + parse_quotation(T,[$",$\\|Acc]); +parse_quotation([$\\,X,Y|T],Acc) when ?IS_HEXCHAR(X),?IS_HEXCHAR(Y) -> + parse_quotation(T,[Y,X,$\\|Acc]); +parse_quotation(T,_) -> + error(expecting_double_quote_mark,T). + +parse_string(Str) -> + parse_string(Str,[]). + +parse_string("",Acc) -> + {"",lists:reverse(Acc)}; +parse_string([H|T],Acc) when ?IS_STRINGCHAR(H) -> + parse_string(T,[H|Acc]); +parse_string([$\\,X|T],Acc) when ?IS_SPECIAL(X) -> % is a pair ! + parse_string(T,[X,$\\|Acc]); +parse_string([$\\,$\\|T],Acc) -> % is a pair ! + parse_string(T,[$\\,$\\|Acc]); +parse_string([$\\,$" |T],Acc) -> % is a pair ! + parse_string(T,[$" ,$\\|Acc]); +parse_string([$\\,X,Y|T],Acc) when ?IS_HEXCHAR(X),?IS_HEXCHAR(Y) -> % is a pair! + parse_string(T,[Y,X,$\\|Acc]); +parse_string(T,Acc) -> + {T,lists:reverse(Acc)}. + +parse_equal_sign([$=|T]) -> T; +parse_equal_sign(T) -> error(expecting_equal_sign,T). + +parse_keychars(Str) -> parse_keychars(Str,[]). + +parse_keychars([H|T],Acc) when ?IS_ALPHA(H) -> parse_keychars(T,[H|Acc]); +parse_keychars([H|T],Acc) when ?IS_DIGIT(H) -> parse_keychars(T,[H|Acc]); +parse_keychars([$-|T],Acc) -> parse_keychars(T,[$-|Acc]); +parse_keychars(T,Acc) -> {T,lists:reverse(Acc)}. + +parse_oid(Str) -> parse_oid(Str,[]). + +parse_oid([H,$.|T], Acc) when ?IS_DIGIT(H) -> + parse_oid(T,[$.,H|Acc]); +parse_oid([H|T], Acc) when ?IS_DIGIT(H) -> + parse_oid(T,[H|Acc]); +parse_oid(T, Acc) -> + {T,lists:reverse(Acc)}. + +error(Emsg,Rest) -> + throw({parse_error,Emsg,Rest}). + + +%%% -------------------------------------------------------------------- +%%% Parse LDAP url according to RFC 2255 +%%% +%%% Test case: +%%% +%%% 2> eldap:parse_ldap_url("ldap://10.42.126.33:389/cn=Administrative%20CA,o=Post%20Danmark,c=DK?certificateRevokationList;binary"). +%%% {ok,{{10,42,126,33},389}, +%%% [[{attribute_type_and_value,"cn","Administrative%20CA"}], +%%% [{attribute_type_and_value,"o","Post%20Danmark"}], +%%% [{attribute_type_and_value,"c","DK"}]], +%%% {attributes,["certificateRevokationList;binary"]}} +%%% +%%% -------------------------------------------------------------------- + +parse_ldap_url("ldap://" ++ Rest1 = Str) -> + {Rest2,HostPort} = parse_hostport(Rest1), + %% Split the string into DN and Attributes+etc + {Sdn,Rest3} = split_string(rm_leading_slash(Rest2),$?), + case parse_dn(Sdn) of + {parse_error,internal_error,_Reason} -> + {parse_error,internal_error,{Str,[]}}; + {parse_error,Emsg,Tail} -> + Head = get_head(Str,Tail), + {parse_error,Emsg,{Head,Tail}}; + {ok,DN} -> + %% We stop parsing here for now and leave + %% 'scope', 'filter' and 'extensions' to + %% be implemented later if needed. + {_Rest4,Attributes} = parse_attributes(Rest3), + {ok,HostPort,DN,Attributes} + end. + +rm_leading_slash([$/|Tail]) -> Tail; +rm_leading_slash(Tail) -> Tail. + +parse_attributes([$?|Tail]) -> + case split_string(Tail,$?) of + {[],Attributes} -> + {[],{attributes,string:tokens(Attributes,",")}}; + {Attributes,Rest} -> + {Rest,{attributes,string:tokens(Attributes,",")}} + end. + +parse_hostport(Str) -> + {HostPort,Rest} = split_string(Str,$/), + case split_string(HostPort,$:) of + {Shost,[]} -> + {Rest,{parse_host(Rest,Shost),?LDAP_PORT}}; + {Shost,[$:|Sport]} -> + {Rest,{parse_host(Rest,Shost), + parse_port(Rest,Sport)}} + end. + +parse_port(Rest,Sport) -> + case list_to_integer(Sport) of + Port when integer(Port) -> Port; + _ -> error(parsing_port,Rest) + end. + +parse_host(Rest,Shost) -> + case catch validate_host(Shost) of + {parse_error,Emsg,_} -> error(Emsg,Rest); + Host -> Host + end. + +validate_host(Shost) -> + case inet_parse:address(Shost) of + {ok,Host} -> Host; + _ -> + case inet_parse:domain(Shost) of + true -> Shost; + _ -> error(parsing_host,Shost) + end + end. + + +split_string(Str,Key) -> + Pred = fun(X) when X==Key -> false; (_) -> true end, + lists:splitwith(Pred, Str). + +get_head(Str,Tail) -> + get_head(Str,Tail,[]). + +%%% Should always succeed ! +get_head([H|Tail],Tail,Rhead) -> lists:reverse([H|Rhead]); +get_head([H|Rest],Tail,Rhead) -> get_head(Rest,Tail,[H|Rhead]). + +b2l(B) when binary(B) -> B; +b2l(L) when list(L) -> list_to_binary(L). + diff --git a/rabbitmq-server/plugins-src/eldap-wrapper/eldap-git/test/README.test b/rabbitmq-server/plugins-src/eldap-wrapper/eldap-git/test/README.test new file mode 100644 index 0000000..9816216 --- /dev/null +++ b/rabbitmq-server/plugins-src/eldap-wrapper/eldap-git/test/README.test @@ -0,0 +1,96 @@ +%%% $Id$ + +%%% -------------------------------------------------------------------- +%%% Init setup +%%% -------------------------------------------------------------------- + +I set up the OpenLDAP (2.0.6) server using the following +/usr/local/etc/openldap/slapd.conf file: + + include /usr/local/etc/openldap/schema/core.schema + pidfile /var/run/slapd.pid + argsfile /var/run/slapd.args + database ldbm + suffix "dc=bluetail, dc=com" + rootdn "dc=bluetail, dc=com" + rootpw hejsan + directory /usr/local/var/openldap-ldbm + index objectClass eq + + +%%% I started it on the console with some debug output: + + /usr/local/libexec/slapd -d 255 -f /usr/local/etc/openldap/slapd.conf + +%%% Then I defined the following data in: bluetail.ldif + + dn: dc=bluetail, dc=com + objectclass: organization + objectclass: dcObject + dc: bluetail + o: Bluetail AB + +%%% and in: tobbe.ldif + + dn: cn=Torbjorn Tornkvist, dc=bluetail, dc=com + objectclass: person + cn: Torbjorn Tornkvist + sn: Tornkvist + +%%% I load the data with: + + ldapadd -D "dc=bluetail, dc=com" -w hejsan < bluetail.ldif + ldapadd -D "dc=bluetail, dc=com" -w hejsan < people.ldif + +%%%% To search from a Unix shell: + + ldapsearch -L -b "dc=bluetail, dc=com" -w hejsan "(objectclass=*)" + ldapsearch -L -b "dc=bluetail, dc=com" -w hejsan "cn=Torbjorn Tornkvist" + ldapsearch -L -b "dc=bluetail, dc=com" -w hejsan "cn=Torb*kvist" + +%%% -------------------------------------------------------------------- +%%% Example with certificateRevocationList +%%% -------------------------------------------------------------------- + +%%% Using two ldif files: + +%%% post_danmark.ldif + +dn: o=Post Danmark, c=DK +objectclass: country +objectclass: organization +c: DK +o: Post Danmark + +%%% crl.ldif + +dn: cn=Administrative CA, o=Post Danmark, c=DK +objectclass: cRLDistributionPoint +cn: Administrative CA +certificateRevocationList;binary:< file:/home/tobbe/erlang/eldap/server1.crl + +%%% Note the definition of the CRL file !! + +%%% To add the difinitions + +ldapadd -D "o=Post Danmark, c=DK" -w hejsan < post_danmark.ldif +ldapadd -D "o=Post Danmark, c=DK" -w hejsan < crl.ldif + +%%% And to retreive the CRL + +ldapsearch -L -b "o=Post Danmark, c=DK" -w hejsan "(objectclass=*)" +ldapsearch -L -b "o=Post Danmark, c=DK" -w hejsan "(cn=Administrative CA)" \ + certificateRevocationList + +### Put the retrieved binary in a file (tmp) with +### the following header and footer + +-----BEGIN X509 CRL----- + <...binary....> +-----END X509 CRL----- + +### To verify it with openssl + + openssl crl -inform PEM -in tmp -text + +ldapsearch -L -D "cn=Torbjorn Tornkvist,o=Post Danmark,c=DK" -b "o=Post Danmark, c=DK" -w qwe123 "(cn=Torbjorn Tornkvist)" cn diff --git a/rabbitmq-server/plugins-src/eldap-wrapper/eldap-git/test/bill.ldif b/rabbitmq-server/plugins-src/eldap-wrapper/eldap-git/test/bill.ldif new file mode 100644 index 0000000..59022ad --- /dev/null +++ b/rabbitmq-server/plugins-src/eldap-wrapper/eldap-git/test/bill.ldif @@ -0,0 +1,13 @@ +dn: mail=bill@bluetail.com, dc=bluetail, dc=com +objectclass: posixAccount +mail: bill@bluetail.com +cn: Bill Valentine +sn: Valentine +uid: bill +uidNumber: 400 +gidNumber: 400 +homeDirectory: /home/accounts/bill +mailDirectory: /home/accounts/bill/INBOX +userPassword: baltazar +birMailAccept: accept +birCluster: bc1 diff --git a/rabbitmq-server/plugins-src/eldap-wrapper/eldap-git/test/bluetail.ldif b/rabbitmq-server/plugins-src/eldap-wrapper/eldap-git/test/bluetail.ldif new file mode 100644 index 0000000..914532e --- /dev/null +++ b/rabbitmq-server/plugins-src/eldap-wrapper/eldap-git/test/bluetail.ldif @@ -0,0 +1,18 @@ +dn: dc=bluetail, dc=com +objectclass: dcObject +dc: bluetail + +dn: o=Bluetail AB, dc=bluetail, dc=com +objectclass: organization +o: Bluetail AB +street: St.Eriksgatan 44 +postalCode: 112 34 + +dn: ou=people, o=Bluetail AB, dc=bluetail, dc=com +objectclass: organizationalUnit +ou: people +description: People working at Bluetail + + + + diff --git a/rabbitmq-server/plugins-src/eldap-wrapper/eldap-git/test/crl.ldif b/rabbitmq-server/plugins-src/eldap-wrapper/eldap-git/test/crl.ldif new file mode 100644 index 0000000..2e52873 --- /dev/null +++ b/rabbitmq-server/plugins-src/eldap-wrapper/eldap-git/test/crl.ldif @@ -0,0 +1,5 @@ +dn: cn=Administrative CA,o=Post Danmark,c=DK +objectclass: cRLDistributionPoint +cn: Administrative CA +certificateRevocationList;binary:< file:/home/tobbe/erlang/eldap/server1.crl + diff --git a/rabbitmq-server/plugins-src/eldap-wrapper/eldap-git/test/eldap_test.erl b/rabbitmq-server/plugins-src/eldap-wrapper/eldap-git/test/eldap_test.erl new file mode 100644 index 0000000..db64615 --- /dev/null +++ b/rabbitmq-server/plugins-src/eldap-wrapper/eldap-git/test/eldap_test.erl @@ -0,0 +1,537 @@ +-module(eldap_test). +%%% -------------------------------------------------------------------- +%%% Created: 12 Oct 2000 by Tobbe +%%% Function: Test code for the eldap module +%%% +%%% Copyright (C) 2000 Torbjörn Törnkvist +%%% Copyright (c) 2010 Torbjorn Tornkvist +%%% See MIT-LICENSE at the top dir for licensing information. +%%% +%%% -------------------------------------------------------------------- +-vc('$Id$ '). +-export([topen_bind/1,topen_bind/2,all/0,t10/0,t20/0,t21/0,t22/0, + t23/0,t24/0,t25/0,t26/0,t27/0,debug/1,t30/0,t31/0, + t40/0,t41/0,t50/0,t51/0]). +-export([crl1/0]). +-export([switch/1]). +-export([junk/0]). + +-include("ELDAPv3.hrl"). +-include("eldap.hrl"). + +junk() -> + DN = "cn=Torbjorn Tornkvist, ou=people, o=Bluetail AB, dc=bluetail, dc=com", + Msg = #'LDAPMessage'{messageID = 1, + protocolOp = {delRequest,DN}}, + asn1rt:encode('ELDAPv3', 'LDAPMessage', Msg). + +%%% -------------------------------------------------------------------- +%%% TEST STUFF +%%% ---------- +%%% When adding a new test case it can be useful to +%%% switch on debugging, i.e debug(t) in the call to +%%% topen_bind/2. +%%% -------------------------------------------------------------------- + +all() -> + Check = "=== Check the result of the previous test case !~n", + t10(), + t20(),t21(),t22(),t23(),t24(),t25(),t26(),t27(), + t30(),t26(Check),t31(),t26(Check), + t40(),t26(Check),t41(),t26(Check), + t50(),t26(Check),t51(),t26(Check), + ok. + +%%% +%%% Setup a connection and bind using simple authentication +%%% +t10() -> + F = fun() -> + sleep(), + line(), + io:format("=== TEST 10 (connection setup + simple auth)~n"), + line(), + X = topen_bind("localhost", debug(f)), + io:format("~p~n",[X]), + X + end, + go(F). + +%%% +%%% Do an equality match: sn = Tornkvist +%%% +t20() -> + F = fun() -> + sleep(), + line(), + io:format("=== TEST 20 (equality match)~n"), + line(), + {ok,S} = topen_bind("localhost", debug(f)), + Filter = eldap:equalityMatch("sn","Tornkvist"), + X=(catch eldap:search(S, [{base, "dc=bluetail, dc=com"}, + {filter, Filter}])), + + io:format("~p~n",[X]), + X + end, + go(F). + +%%% +%%% Do a substring match: sn = To*kv*st +%%% +t21() -> + F = fun() -> + sleep(), + line(), + io:format("=== TEST 21 (substring match)~n"), + line(), + {ok,S} = topen_bind("localhost", debug(f)), + Filter = eldap:substrings("sn", [{initial,"To"}, + {any,"kv"}, + {final,"st"}]), + X=(catch eldap:search(S, [{base, "dc=bluetail, dc=com"}, + {filter, Filter}])), + io:format("~p~n",[X]), + X + end, + go(F). + +%%% +%%% Do a substring match: sn = *o* +%%% and do only retrieve the cn attribute +%%% +t22() -> + F = fun() -> + sleep(), + line(), + io:format("=== TEST 22 (substring match + return 'cn' only)~n"), + line(), + {ok,S} = topen_bind("localhost", debug(f)), + Filter = eldap:substrings("sn", [{any,"o"}]), + X=(catch eldap:search(S, [{base, "dc=bluetail, dc=com"}, + {filter, Filter}, + {attributes,["cn"]}])), + io:format("~p~n",[X]), + X + end, + go(F). + + +%%% +%%% Do a present search for the attribute 'objectclass' +%%% on the base level. +%%% +t23() -> + F = fun() -> + sleep(), + line(), + io:format("=== TEST 23 (objectclass=* , base level)~n"), + line(), + {ok,S} = topen_bind("localhost", debug(f)), + X=(catch eldap:search(S, [{base, "dc=bluetail, dc=com"}, + {filter, eldap:present("objectclass")}, + {scope,eldap:baseObject()}])), + io:format("~p~n",[X]), + X + end, + go(F). + +%%% +%%% Do a present search for the attribute 'objectclass' +%%% on a single level. +%%% +t24() -> + F = fun() -> + sleep(), + line(), + io:format("=== TEST 24 (objectclass=* , single level)~n"), + line(), + {ok,S} = topen_bind("localhost", debug(f)), + X=(catch eldap:search(S, [{base, "dc=bluetail, dc=com"}, + {filter, eldap:present("objectclass")}, + {scope,eldap:singleLevel()}])), + io:format("~p~n",[X]), + X + end, + go(F). + +%%% +%%% Do a present search for the attribute 'objectclass' +%%% on the whole subtree. +%%% +t25() -> + F = fun() -> + sleep(), + line(), + io:format("=== TEST 25 (objectclass=* , whole subtree)~n"), + line(), + {ok,S} = topen_bind("localhost", debug(f)), + X=(catch eldap:search(S, [{base, "dc=bluetail, dc=com"}, + {filter, eldap:present("objectclass")}, + {scope,eldap:wholeSubtree()}])), + io:format("~p~n",[X]), + X + end, + go(F). + +%%% +%%% Do a present search for the attributes +%%% 'objectclass' and 'sn' on the whole subtree. +%%% +t26() -> t26([]). +t26(Heading) -> + F = fun() -> + sleep(), + line(), + heading(Heading, + "=== TEST 26 (objectclass=* and sn=*)~n"), + line(), + {ok,S} = topen_bind("localhost", debug(f)), + Filter = eldap:'and'([eldap:present("objectclass"), + eldap:present("sn")]), + X=(catch eldap:search(S, [{base, "dc=bluetail, dc=com"}, + {filter, Filter}, + {scope,eldap:wholeSubtree()}])), + io:format("~p~n",[X]), + X + end, + go(F). + +%%% +%%% Do a present search for the attributes +%%% 'objectclass' and (not 'sn') on the whole subtree. +%%% +t27() -> + F = fun() -> + sleep(), + line(), + io:format("=== TEST 27 (objectclass=* and (not sn))~n"), + line(), + {ok,S} = topen_bind("localhost", debug(f)), + Filter = eldap:'and'([eldap:present("objectclass"), + eldap:'not'(eldap:present("sn"))]), + X=(catch eldap:search(S, [{base, "dc=bluetail, dc=com"}, + {filter, Filter}, + {scope,eldap:wholeSubtree()}])), + io:format("~p~n",[X]), + X + end, + go(F). + +%%% +%%% Replace the 'telephoneNumber' attribute and +%%% add a new attribute 'description' +%%% +t30() -> t30([]). +t30(Heading) -> + F = fun() -> + sleep(), + {_,_,Tno} = erlang:now(), + Stno = integer_to_list(Tno), + Desc = "LDAP hacker " ++ Stno, + line(), + heading(Heading, + "=== TEST 30 (replace telephoneNumber/" + ++ Stno ++ " add description/" ++ Desc + ++ ")~n"), + line(), + {ok,S} = topen_bind("localhost", debug(f)), + Obj = "cn=Torbjorn Tornkvist, ou=people, o=Bluetail AB, dc=bluetail, dc=com", + Mod = [eldap:mod_replace("telephoneNumber", [Stno]), + eldap:mod_add("description", [Desc])], + X=(catch eldap:modify(S, Obj, Mod)), + io:format("~p~n",[X]), + X + end, + go(F). + +%%% +%%% Delete attribute 'description' +%%% +t31() -> t31([]). +t31(Heading) -> + F = fun() -> + sleep(), + {_,_,Tno} = erlang:now(), + line(), + heading(Heading, + "=== TEST 31 (delete 'description' attribute)~n"), + line(), + {ok,S} = topen_bind("localhost", debug(f)), + Obj = "cn=Torbjorn Tornkvist, ou=people, o=Bluetail AB, dc=bluetail, dc=com", + Mod = [eldap:mod_delete("description", [])], + X=(catch eldap:modify(S, Obj, Mod)), + io:format("~p~n",[X]), + X + end, + go(F). + +%%% +%%% Add an entry +%%% +t40() -> t40([]). +t40(Heading) -> + F = fun() -> + sleep(), + {_,_,Tno} = erlang:now(), + line(), + heading(Heading, + "=== TEST 40 (add entry 'Bill Valentine')~n"), + line(), + {ok,S} = topen_bind("localhost", debug(f)), + Entry = "cn=Bill Valentine, ou=people, o=Bluetail AB, dc=bluetail, dc=com", + X=(catch eldap:add(S, Entry, + [{"objectclass", ["person"]}, + {"cn", ["Bill Valentine"]}, + {"sn", ["Valentine"]}, + {"telephoneNumber", ["545 555 00"]}])), + io:format("~p~n",[X]), + X + end, + go(F). + +%%% +%%% Delete an entry +%%% +t41() -> t41([]). +t41(Heading) -> + F = fun() -> + sleep(), + {_,_,Tno} = erlang:now(), + line(), + heading(Heading, + "=== TEST 41 (delete entry 'Bill Valentine')~n"), + line(), + {ok,S} = topen_bind("localhost", debug(f)), + Entry = "cn=Bill Valentine, ou=people, o=Bluetail AB, dc=bluetail, dc=com", + X=(catch eldap:delete(S, Entry)), + io:format("~p~n",[X]), + X + end, + go(F). + +%%% +%%% Modify the DN of an entry +%%% +t50() -> t50([]). +t50(Heading) -> + F = fun() -> + sleep(), + {_,_,Tno} = erlang:now(), + line(), + heading(Heading, + "=== TEST 50 (modify DN to: 'Torbjorn M.Tornkvist')~n"), + line(), + {ok,S} = topen_bind("localhost", debug(f)), + Entry = "cn=Torbjorn Tornkvist, ou=people, o=Bluetail AB, dc=bluetail, dc=com", + X=(catch eldap:modify_dn(S, Entry, + "cn=Torbjorn M.Tornkvist", + false, + [])), + io:format("~p~n",[X]), + X + end, + go(F). + +%%% +%%% Modify the DN of an entry and remove the RDN attribute. +%%% NB: Must be run after: 't50' ! +%%% +t51() -> t51([]). +t51(Heading) -> + F = fun() -> + sleep(), + {_,_,Tno} = erlang:now(), + line(), + heading(Heading, + "=== TEST 51 (modify DN, remove the RDN attribute)~n"), + line(), + {ok,S} = topen_bind("localhost", debug(f)), + Entry = "cn=Torbjorn M.Tornkvist, ou=people, o=Bluetail AB, dc=bluetail, dc=com", + X=(catch eldap:modify_dn(S, Entry, + "cn=Torbjorn Tornkvist", + true, + [])), + io:format("~p~n",[X]), + X + end, + go(F). + +%%% -------------------------------------------------------------------- +%%% Test cases for certificate revocation lists +%%% -------------------------------------------------------------------- + +crl1() -> + F = fun() -> + sleep(), + line(), + io:format("=== CRL-TEST 1 ~n"), + line(), + {ok,S} = crl_open_bind("localhost", debug(f)), + Filter = eldap:equalityMatch("cn","Administrative CA"), + X=(catch eldap:search(S, [{base, "o=Post Danmark, c=DK"}, + {filter, Filter}, + {attributes,["certificateRevocationList"]}])), + dump_to_file("test-crl1.result",X), + ok + end, + go(F). + + +dump_to_file(Fname,{ok,Res}) -> + case Res#eldap_search_result.entries of + [Entry|_] -> + case Entry#eldap_entry.attributes of + [{Attribute,Value}|_] -> + file:write_file(Fname,list_to_binary(Value)), + io:format("Value of '~s' dumped to file: ~s~n", + [Attribute,Fname]); + Else -> + io:format("ERROR(dump_to_file): no attributes found~n",[]) + end; + Else -> + io:format("ERROR(dump_to_file): no entries found~n",[]) + end. + +switch(1) -> + %% + %% SEARCH + %% + F = fun() -> + sleep(), + line(), + io:format("=== SWITCH-TEST 1 (short-search)~n"), + line(), + {ok,S} = sw_open_bind("korp", debug(t)), + Filter = eldap:equalityMatch("cn","Administrative CA"), + X=(catch eldap:search(S, [{base, "o=Post Danmark, c=DK"}, + {filter, Filter}, + {attributes,["cn"]}])), + io:format("RESULT: ~p~n", [X]), + %%dump_to_file("test-switch-1.result",X), + eldap:close(S), + ok + end, + go(F); +switch(2) -> + %% + %% ADD AN ENTRY + %% + F = fun() -> + sleep(), + line(), + io:format("=== SWITCH-TEST 2 (add-entry)~n"), + line(), + {ok,S} = sw_open_bind("korp", debug(t)), + Entry = "cn=Bill Valentine, o=Post Danmark, c=DK", + X=(catch eldap:add(S, Entry, + [{"objectclass", ["person"]}, + {"cn", ["Bill Valentine"]}, + {"sn", ["Valentine"]} + ])), + io:format("~p~n",[X]), + eldap:close(S), + X + end, + go(F); +switch(3) -> + %% + %% SEARCH FOR THE NEWLEY ADDED ENTRY + %% + F = fun() -> + sleep(), + line(), + io:format("=== SWITCH-TEST 3 (search-added)~n"), + line(), + {ok,S} = sw_open_bind("korp", debug(t)), + Filter = eldap:equalityMatch("cn","Bill Valentine"), + X=(catch eldap:search(S, [{base, "o=Post Danmark, c=DK"}, + {filter, Filter}, + {attributes,["cn"]}])), + io:format("RESULT: ~p~n", [X]), + %%dump_to_file("test-switch-1.result",X), + eldap:close(S), + ok + end, + go(F); +switch(4) -> + %% + %% DELETE THE NEWLEY ADDED ENTRY + %% + F = fun() -> + sleep(), + line(), + io:format("=== SWITCH-TEST 4 (delete-added)~n"), + line(), + {ok,S} = sw_open_bind("korp", debug(t)), + Entry = "cn=Bill Valentine, o=Post Danmark, c=DK", + X=(catch eldap:delete(S, Entry)), + io:format("RESULT: ~p~n", [X]), + %%dump_to_file("test-switch-1.result",X), + eldap:close(S), + ok + end, + go(F). + + + +%%% --------------- +%%% Misc. functions +%%% --------------- + +sw_open_bind(Host) -> + sw_open_bind(Host, debug(t)). + +sw_open_bind(Host, Dbg) -> + sw_open_bind(Host, Dbg, "cn=Torbjorn Tornkvist,o=Post Danmark,c=DK", "qwe123"). + +sw_open_bind(Host, LogFun, RootDN, Passwd) -> + Opts = [{log,LogFun},{port,9779}], + {ok,Handle} = eldap:open([Host], Opts), + {eldap:simple_bind(Handle, RootDN, Passwd), + Handle}. + +crl_open_bind(Host) -> + crl_open_bind(Host, debug(t)). + +crl_open_bind(Host, Dbg) -> + do_open_bind(Host, Dbg, "o=Post Danmark, c=DK", "hejsan"). + +topen_bind(Host) -> + topen_bind(Host, debug(t)). + +topen_bind(Host, Dbg) -> + do_open_bind(Host, Dbg, "dc=bluetail, dc=com", "hejsan"). + +do_open_bind(Host, LogFun, RootDN, Passwd) -> + Opts = [{log,LogFun}], + {ok,Handle} = eldap:open([Host], Opts), + {eldap:simple_bind(Handle, RootDN, Passwd), + Handle}. + +debug(t) -> fun(L,S,A) -> io:format("--- " ++ S, A) end; +debug(1) -> fun(L,S,A) when L =< 1 -> io:format("--- " ++ S, A) end; +debug(2) -> fun(L,S,A) when L =< 2 -> io:format("--- " ++ S, A) end; +debug(f) -> false. + +sleep() -> msleep(400). +%sleep(Sec) -> msleep(Sec*1000). +msleep(T) -> receive after T -> true end. + +line() -> + S = "==============================================================\n", + io:format(S). + +heading([], Heading) -> io:format(Heading); +heading(Heading, _ ) -> io:format(Heading). + +%%% +%%% Process to run the test case +%%% +go(F) -> + Self = self(), + Pid = spawn(fun() -> run(F,Self) end), + receive {Pid, X} -> ok end. + +run(F, Pid) -> + Pid ! {self(),catch F()}. diff --git a/rabbitmq-server/plugins-src/eldap-wrapper/eldap-git/test/ldap.rc b/rabbitmq-server/plugins-src/eldap-wrapper/eldap-git/test/ldap.rc new file mode 100644 index 0000000..6cbdfea --- /dev/null +++ b/rabbitmq-server/plugins-src/eldap-wrapper/eldap-git/test/ldap.rc @@ -0,0 +1,103 @@ +#!/bin/sh +# +# ldap This shell script takes care of starting and stopping +# ldap servers (slapd and slurpd). +# +# chkconfig: - 39 61 +# description: LDAP stands for Lightweight Directory Access Protocol, used \ +# for implementing the industry standard directory services. +# processname: slapd +# config: /etc/openldap/slapd.conf +# pidfile: /var/run/slapd.pid + +# Source function library. +. /etc/init.d/functions + +# Source networking configuration and check that networking is up. +if [ -r /etc/sysconfig/network ] ; then + . /etc/sysconfig/network + [ ${NETWORKING} = "no" ] && exit 0 +fi + + +slapd=/usr/sbin/slapd +slurpd=/usr/sbin/slurpd +[ -x ${slapd} ] || exit 0 +[ -x ${slurpd} ] || exit 0 + +RETVAL=0 + +function start() { + # Start daemons. + echo -n "Starting slapd:" + daemon ${slapd} + RETVAL=$? + echo + if [ $RETVAL -eq 0 ]; then + if grep -q "^replogfile" /etc/openldap/slapd.conf; then + echo -n "Starting slurpd:" + daemon ${slurpd} + RETVAL=$? + echo + fi + fi + [ $RETVAL -eq 0 ] && touch /var/lock/subsys/ldap + return $RETVAL +} + +function stop() { + # Stop daemons. + echo -n "Shutting down ldap: " + killproc ${slapd} + RETVAL=$? + if [ $RETVAL -eq 0 ]; then + if grep -q "^replogfile" /etc/openldap/slapd.conf; then + killproc ${slurpd} + RETVAL=$? + fi + fi + echo + [ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/ldap /var/run/slapd.args + return $RETVAL +} + +# See how we were called. +case "$1" in + start) + start + ;; + stop) + stop + ;; + status) + status ${slapd} + if grep -q "^replogfile" /etc/openldap/slapd.conf ; then + status ${slurpd} + fi + ;; + restart) + stop + start + ;; + reload) + killall -HUP ${slapd} + RETVAL=$? + if [ $RETVAL -eq 0 ]; then + if grep -q "^replogfile" /etc/openldap/slapd.conf; then + killall -HUP ${slurpd} + RETVAL=$? + fi + fi + ;; + condrestart) + if [ -f /var/lock/subsys/ldap ] ; then + stop + start + fi + ;; + *) + echo "Usage: $0 start|stop|restart|status|condrestart}" + RETVAL=1 +esac + +exit $RETVAL diff --git a/rabbitmq-server/plugins-src/eldap-wrapper/eldap-git/test/people.ldif b/rabbitmq-server/plugins-src/eldap-wrapper/eldap-git/test/people.ldif new file mode 100644 index 0000000..20af5a0 --- /dev/null +++ b/rabbitmq-server/plugins-src/eldap-wrapper/eldap-git/test/people.ldif @@ -0,0 +1,11 @@ +dn: cn=Torbjorn Tornkvist, ou=people, o=Bluetail AB, dc=bluetail, dc=com +objectclass: person +cn: Torbjorn Tornkvist +sn: Tornkvist +telephoneNumber: 545 550 23 + +dn: cn=Magnus Froberg, ou=people, o=Bluetail AB, dc=bluetail, dc=com +objectclass: person +cn: Magnus Froberg +sn: Froberg +telephoneNumber: 545 550 26 diff --git a/rabbitmq-server/plugins-src/eldap-wrapper/eldap-git/test/post_danmark.ldif b/rabbitmq-server/plugins-src/eldap-wrapper/eldap-git/test/post_danmark.ldif new file mode 100644 index 0000000..24fbb3f --- /dev/null +++ b/rabbitmq-server/plugins-src/eldap-wrapper/eldap-git/test/post_danmark.ldif @@ -0,0 +1,5 @@ +dn: o=Post Danmark,c=DK +objectclass: country +objectclass: organization +c: DK +o: Post Danmark diff --git a/rabbitmq-server/plugins-src/eldap-wrapper/eldap-git/test/server1.crl b/rabbitmq-server/plugins-src/eldap-wrapper/eldap-git/test/server1.crl new file mode 100644 index 0000000..6be714a Binary files /dev/null and b/rabbitmq-server/plugins-src/eldap-wrapper/eldap-git/test/server1.crl differ diff --git a/rabbitmq-server/plugins-src/eldap-wrapper/eldap-git/test/slapd.conf b/rabbitmq-server/plugins-src/eldap-wrapper/eldap-git/test/slapd.conf new file mode 100644 index 0000000..6bf4fee --- /dev/null +++ b/rabbitmq-server/plugins-src/eldap-wrapper/eldap-git/test/slapd.conf @@ -0,0 +1,41 @@ +# $OpenLDAP: pkg/ldap/servers/slapd/slapd.conf,v 1.8.8.4 2000/08/26 17:06:18 kurt Exp $ +# +# See slapd.conf(5) for details on configuration options. +# This file should NOT be world readable. +# +include /usr/etc/openldap/schema/core.schema + +# Define global ACLs to disable default read access. + +# Do not enable referrals until AFTER you have a working directory +# service AND an understanding of referrals. +#referral ldap://root.openldap.org + +pidfile /var/run/slapd.pid +argsfile /var/run/slapd.args + +# Load dynamic backend modules: +# modulepath /usr/libexec/openldap +# moduleload back_ldap.la +# moduleload back_ldbm.la +# moduleload back_passwd.la +# moduleload back_shell.la + +####################################################################### +# ldbm database definitions +####################################################################### + +database ldbm +suffix "dc=bluetail, dc=com" +#suffix "o=My Organization Name, c=US" +rootdn "dc=bluetail, dc=com" +#rootdn "cn=Manager, o=My Organization Name, c=US" +# Cleartext passwords, especially for the rootdn, should +# be avoid. See slappasswd(8) and slapd.conf(5) for details. +# Use of strong authentication encouraged. +rootpw hejsan +# The database directory MUST exist prior to running slapd AND +# should only be accessable by the slapd/tools. Mode 700 recommended. +directory /usr/var/openldap-ldbm +# Indices to maintain +index objectClass eq diff --git a/rabbitmq-server/plugins-src/eldap-wrapper/eldap-git/test/tobbe.ldif b/rabbitmq-server/plugins-src/eldap-wrapper/eldap-git/test/tobbe.ldif new file mode 100644 index 0000000..1a13f2a --- /dev/null +++ b/rabbitmq-server/plugins-src/eldap-wrapper/eldap-git/test/tobbe.ldif @@ -0,0 +1,6 @@ +dn: cn=Torbjorn Tornkvist,o=Post Danmark,c=DK +objectclass: person +cn: Torbjorn Tornkvist +sn: Tornkvist +userPassword: qwe123 + diff --git a/rabbitmq-server/plugins-src/eldap-wrapper/eldap-no-ssl-seed.patch b/rabbitmq-server/plugins-src/eldap-wrapper/eldap-no-ssl-seed.patch new file mode 100644 index 0000000..1b5f956 --- /dev/null +++ b/rabbitmq-server/plugins-src/eldap-wrapper/eldap-no-ssl-seed.patch @@ -0,0 +1,17 @@ +diff --git a/src/eldap.erl b/src/eldap.erl +index 9a78270..b0cdb2e 100644 +--- a/src/eldap.erl ++++ b/src/eldap.erl +@@ -363,12 +363,5 @@ do_connect(Host, Data, Opts) when Data#eldap.use_tls == false -> + gen_tcp:connect(Host, Data#eldap.port, Opts, Data#eldap.timeout); + do_connect(Host, Data, Opts) when Data#eldap.use_tls == true -> +- Vsn = erlang:system_info(version), +- if Vsn >= "5.3" -> +- %% In R9C, but not in R9B +- {_,_,X} = erlang:now(), +- ssl:seed("bkrlnateqqo" ++ integer_to_list(X)); +- true -> true +- end, + ssl:connect(Host, Data#eldap.port, [{verify,0}|Opts]). + + diff --git a/rabbitmq-server/plugins-src/eldap-wrapper/hash.mk b/rabbitmq-server/plugins-src/eldap-wrapper/hash.mk new file mode 100644 index 0000000..262b7cc --- /dev/null +++ b/rabbitmq-server/plugins-src/eldap-wrapper/hash.mk @@ -0,0 +1 @@ +UPSTREAM_SHORT_HASH:=e309de4 diff --git a/rabbitmq-server/plugins-src/eldap-wrapper/license_info b/rabbitmq-server/plugins-src/eldap-wrapper/license_info new file mode 100644 index 0000000..0a0e13c --- /dev/null +++ b/rabbitmq-server/plugins-src/eldap-wrapper/license_info @@ -0,0 +1,3 @@ +Eldap is "Copyright (c) 2010, Torbjorn Tornkvist" and is covered by +the MIT license. It was downloaded from https://github.com/etnt/eldap + diff --git a/rabbitmq-server/plugins-src/eldap-wrapper/package.mk b/rabbitmq-server/plugins-src/eldap-wrapper/package.mk new file mode 100644 index 0000000..02c8b4e --- /dev/null +++ b/rabbitmq-server/plugins-src/eldap-wrapper/package.mk @@ -0,0 +1,30 @@ +APP_NAME:=eldap + +UPSTREAM_GIT:=https://github.com/rabbitmq/eldap.git +UPSTREAM_REVISION:=e309de4db4b78d67d623 +WRAPPER_PATCHES:=eldap-appify.patch remove-eldap-fsm.patch eldap-no-ssl-seed.patch remove-ietf-doc.patch + +ORIGINAL_APP_FILE:=$(CLONE_DIR)/ebin/$(APP_NAME).app +DO_NOT_GENERATE_APP_FILE=true + +GENERATED_DIR:=$(CLONE_DIR)/generated +PACKAGE_ERLC_OPTS+=-I $(GENERATED_DIR) +INCLUDE_HRLS+=$(GENERATED_DIR)/ELDAPv3.hrl +EBIN_BEAMS+=$(GENERATED_DIR)/ELDAPv3.beam + +define package_rules + +$(CLONE_DIR)/src/ELDAPv3.asn: $(CLONE_DIR)/.done + +$(GENERATED_DIR)/ELDAPv3.hrl $(GENERATED_DIR)/ELDAPv3.beam: $(CLONE_DIR)/src/ELDAPv3.asn + @mkdir -p $(GENERATED_DIR) + $(ERLC) $(PACKAGE_ERLC_OPTS) -o $(GENERATED_DIR) $$< + +$(PACKAGE_DIR)+clean:: + rm -rf $(GENERATED_DIR) $(EBIN_DIR) + +# This rule is run *before* the one in do_package.mk +$(PLUGINS_SRC_DIST_DIR)/$(PACKAGE_DIR)/.srcdist_done:: + cp $(CLONE_DIR)/LICENSE $(PACKAGE_DIR)/LICENSE-MIT-eldap + +endef diff --git a/rabbitmq-server/plugins-src/eldap-wrapper/remove-eldap-fsm.patch b/rabbitmq-server/plugins-src/eldap-wrapper/remove-eldap-fsm.patch new file mode 100644 index 0000000..f6b05f6 --- /dev/null +++ b/rabbitmq-server/plugins-src/eldap-wrapper/remove-eldap-fsm.patch @@ -0,0 +1,952 @@ +diff --git a/src/eldap_fsm.erl b/src/eldap_fsm.erl +deleted file mode 100644 +index 381ce69..0000000 +--- a/src/eldap_fsm.erl ++++ /dev/null +@@ -1,946 +0,0 @@ +--module(eldap_fsm). +-%%% -------------------------------------------------------------------- +-%%% Created: 12 Oct 2000 by Tobbe +-%%% Function: Erlang client LDAP implementation according RFC 2251. +-%%% The interface is based on RFC 1823, and +-%%% draft-ietf-asid-ldap-c-api-00.txt +-%%% +-%%% Copyright (C) 2000 Torbjörn Törnkvist +-%%% Copyright (c) 2010 Torbjorn Tornkvist +-%%% See MIT-LICENSE at the top dir for licensing information. +-%%% +-%%% Modified by Sean Hinde 7th Dec 2000 +-%%% Turned into gen_fsm, made non-blocking, added timers etc to support this. +-%%% Now has the concept of a name (string() or atom()) per instance which allows +-%%% multiple users to call by name if so desired. +-%%% +-%%% Can be configured with start_link parameters or use a config file to get +-%%% host to connect to, dn, password, log function etc. +-%%% -------------------------------------------------------------------- +- +- +-%%%---------------------------------------------------------------------- +-%%% LDAP Client state machine. +-%%% Possible states are: +-%%% connecting - actually disconnected, but retrying periodically +-%%% wait_bind_response - connected and sent bind request +-%%% active - bound to LDAP Server and ready to handle commands +-%%%---------------------------------------------------------------------- +- +-%%-compile(export_all). +-%%-export([Function/Arity, ...]). +- +--behaviour(gen_fsm). +- +-%% External exports +--export([start_link/1, start_link/5, start_link/6]). +- +--export([baseObject/0,singleLevel/0,wholeSubtree/0,close/1, +- equalityMatch/2,greaterOrEqual/2,lessOrEqual/2, +- approxMatch/2,search/2,substrings/2,present/1, +- 'and'/1,'or'/1,'not'/1,modify/3, mod_add/2, mod_delete/2, +- mod_replace/2, add/3, delete/2, modify_dn/5]). +--export([debug_level/2, get_status/1]). +- +-%% gen_fsm callbacks +--export([init/1, connecting/2, +- connecting/3, wait_bind_response/3, active/3, handle_event/3, +- handle_sync_event/4, handle_info/3, terminate/3, code_change/4]). +- +- +--import(lists,[concat/1]). +- +--include("ELDAPv3.hrl"). +--include("eldap.hrl"). +- +--define(LDAP_VERSION, 3). +--define(RETRY_TIMEOUT, 5000). +--define(BIND_TIMEOUT, 10000). +--define(CMD_TIMEOUT, 5000). +--define(MAX_TRANSACTION_ID, 65535). +--define(MIN_TRANSACTION_ID, 0). +- +--record(eldap, {version = ?LDAP_VERSION, +- hosts, % Possible hosts running LDAP servers +- host = null, % Connected Host LDAP server +- port = 389 , % The LDAP server port +- fd = null, % Socket filedescriptor. +- rootdn = "", % Name of the entry to bind as +- passwd, % Password for (above) entry +- id = 0, % LDAP Request ID +- log, % User provided log function +- bind_timer, % Ref to bind timeout +- dict, % dict holding operation params and results +- debug_level % Integer debug/logging level +- }). +- +-%%%---------------------------------------------------------------------- +-%%% API +-%%%---------------------------------------------------------------------- +-start_link(Name) -> +- Reg_name = list_to_atom("eldap_" ++ Name), +- gen_fsm:start_link({local, Reg_name}, ?MODULE, [], []). +- +-start_link(Name, Hosts, Port, Rootdn, Passwd) -> +- Log = fun(N, Fmt, Args) -> io:format("---- " ++ Fmt, [Args]) end, +- Reg_name = list_to_atom("eldap_" ++ Name), +- gen_fsm:start_link({local, Reg_name}, ?MODULE, {Hosts, Port, Rootdn, Passwd, Log}, []). +- +-start_link(Name, Hosts, Port, Rootdn, Passwd, Log) -> +- Reg_name = list_to_atom("eldap_" ++ Name), +- gen_fsm:start_link({local, Reg_name}, ?MODULE, {Hosts, Port, Rootdn, Passwd, Log}, []). +- +-%%% -------------------------------------------------------------------- +-%%% Set Debug Level. 0 - none, 1 - errors, 2 - ldap events +-%%% -------------------------------------------------------------------- +-debug_level(Handle, N) when integer(N) -> +- Handle1 = get_handle(Handle), +- gen_fsm:sync_send_all_state_event(Handle1, {debug_level,N}). +- +-%%% -------------------------------------------------------------------- +-%%% Get status of connection. +-%%% -------------------------------------------------------------------- +-get_status(Handle) -> +- Handle1 = get_handle(Handle), +- gen_fsm:sync_send_all_state_event(Handle1, get_status). +- +-%%% -------------------------------------------------------------------- +-%%% Shutdown connection (and process) asynchronous. +-%%% -------------------------------------------------------------------- +-close(Handle) -> +- Handle1 = get_handle(Handle), +- gen_fsm:send_all_state_event(Handle1, close). +- +-%%% -------------------------------------------------------------------- +-%%% Add an entry. The entry field MUST NOT exist for the AddRequest +-%%% to succeed. The parent of the entry MUST exist. +-%%% Example: +-%%% +-%%% add(Handle, +-%%% "cn=Bill Valentine, ou=people, o=Bluetail AB, dc=bluetail, dc=com", +-%%% [{"objectclass", ["person"]}, +-%%% {"cn", ["Bill Valentine"]}, +-%%% {"sn", ["Valentine"]}, +-%%% {"telephoneNumber", ["545 555 00"]}] +-%%% ) +-%%% -------------------------------------------------------------------- +-add(Handle, Entry, Attributes) when list(Entry),list(Attributes) -> +- Handle1 = get_handle(Handle), +- gen_fsm:sync_send_event(Handle1, {add, Entry, add_attrs(Attributes)}). +- +-%%% Do sanity check ! +-add_attrs(Attrs) -> +- F = fun({Type,Vals}) when list(Type),list(Vals) -> +- %% Confused ? Me too... :-/ +- {'AddRequest_attributes',Type, Vals} +- end, +- case catch lists:map(F, Attrs) of +- {'EXIT', _} -> throw({error, attribute_values}); +- Else -> Else +- end. +- +- +-%%% -------------------------------------------------------------------- +-%%% Delete an entry. The entry consists of the DN of +-%%% the entry to be deleted. +-%%% Example: +-%%% +-%%% delete(Handle, +-%%% "cn=Bill Valentine, ou=people, o=Bluetail AB, dc=bluetail, dc=com" +-%%% ) +-%%% -------------------------------------------------------------------- +-delete(Handle, Entry) when list(Entry) -> +- Handle1 = get_handle(Handle), +- gen_fsm:sync_send_event(Handle1, {delete, Entry}). +- +-%%% -------------------------------------------------------------------- +-%%% Modify an entry. Given an entry a number of modification +-%%% operations can be performed as one atomic operation. +-%%% Example: +-%%% +-%%% modify(Handle, +-%%% "cn=Torbjorn Tornkvist, ou=people, o=Bluetail AB, dc=bluetail, dc=com", +-%%% [replace("telephoneNumber", ["555 555 00"]), +-%%% add("description", ["LDAP hacker"])] +-%%% ) +-%%% -------------------------------------------------------------------- +-modify(Handle, Object, Mods) when list(Object), list(Mods) -> +- Handle1 = get_handle(Handle), +- gen_fsm:sync_send_event(Handle1, {modify, Object, Mods}). +- +-%%% +-%%% Modification operations. +-%%% Example: +-%%% replace("telephoneNumber", ["555 555 00"]) +-%%% +-mod_add(Type, Values) when list(Type), list(Values) -> m(add, Type, Values). +-mod_delete(Type, Values) when list(Type), list(Values) -> m(delete, Type, Values). +-mod_replace(Type, Values) when list(Type), list(Values) -> m(replace, Type, Values). +- +-m(Operation, Type, Values) -> +- #'ModifyRequest_modification_SEQOF'{ +- operation = Operation, +- modification = #'AttributeTypeAndValues'{ +- type = Type, +- vals = Values}}. +- +-%%% -------------------------------------------------------------------- +-%%% Modify an entry. Given an entry a number of modification +-%%% operations can be performed as one atomic operation. +-%%% Example: +-%%% +-%%% modify_dn(Handle, +-%%% "cn=Bill Valentine, ou=people, o=Bluetail AB, dc=bluetail, dc=com", +-%%% "cn=Ben Emerson", +-%%% true, +-%%% "" +-%%% ) +-%%% -------------------------------------------------------------------- +-modify_dn(Handle, Entry, NewRDN, DelOldRDN, NewSup) +- when list(Entry),list(NewRDN),atom(DelOldRDN),list(NewSup) -> +- Handle1 = get_handle(Handle), +- gen_fsm:sync_send_event(Handle1, {modify_dn, Entry, NewRDN, bool_p(DelOldRDN), optional(NewSup)}). +- +-%%% Sanity checks ! +- +-bool_p(Bool) when Bool==true;Bool==false -> Bool. +- +-optional([]) -> asn1_NOVALUE; +-optional(Value) -> Value. +- +-%%% -------------------------------------------------------------------- +-%%% Synchronous search of the Directory returning a +-%%% requested set of attributes. +-%%% +-%%% Example: +-%%% +-%%% Filter = eldap:substrings("sn", [{any,"o"}]), +-%%% eldap:search(S, [{base, "dc=bluetail, dc=com"}, +-%%% {filter, Filter}, +-%%% {attributes,["cn"]}])), +-%%% +-%%% Returned result: {ok, #eldap_search_result{}} +-%%% +-%%% Example: +-%%% +-%%% {ok,{eldap_search_result, +-%%% [{eldap_entry, +-%%% "cn=Magnus Froberg, dc=bluetail, dc=com", +-%%% [{"cn",["Magnus Froberg"]}]}, +-%%% {eldap_entry, +-%%% "cn=Torbjorn Tornkvist, dc=bluetail, dc=com", +-%%% [{"cn",["Torbjorn Tornkvist"]}]}], +-%%% []}} +-%%% +-%%% -------------------------------------------------------------------- +-search(Handle, A) when record(A, eldap_search) -> +- call_search(Handle, A); +-search(Handle, L) when list(Handle), list(L) -> +- case catch parse_search_args(L) of +- {error, Emsg} -> {error, Emsg}; +- {'EXIT', Emsg} -> {error, Emsg}; +- A when record(A, eldap_search) -> call_search(Handle, A) +- end. +- +-call_search(Handle, A) -> +- Handle1 = get_handle(Handle), +- gen_fsm:sync_send_event(Handle1, {search, A}). +- +-parse_search_args(Args) -> +- parse_search_args(Args, #eldap_search{scope = wholeSubtree}). +- +-parse_search_args([{base, Base}|T],A) -> +- parse_search_args(T,A#eldap_search{base = Base}); +-parse_search_args([{filter, Filter}|T],A) -> +- parse_search_args(T,A#eldap_search{filter = Filter}); +-parse_search_args([{scope, Scope}|T],A) -> +- parse_search_args(T,A#eldap_search{scope = Scope}); +-parse_search_args([{attributes, Attrs}|T],A) -> +- parse_search_args(T,A#eldap_search{attributes = Attrs}); +-parse_search_args([{types_only, TypesOnly}|T],A) -> +- parse_search_args(T,A#eldap_search{types_only = TypesOnly}); +-parse_search_args([{timeout, Timeout}|T],A) when integer(Timeout) -> +- parse_search_args(T,A#eldap_search{timeout = Timeout}); +-parse_search_args([H|T],A) -> +- throw({error,{unknown_arg, H}}); +-parse_search_args([],A) -> +- A. +- +-%%% +-%%% The Scope parameter +-%%% +-baseObject() -> baseObject. +-singleLevel() -> singleLevel. +-wholeSubtree() -> wholeSubtree. +- +-%%% +-%%% Boolean filter operations +-%%% +-'and'(ListOfFilters) when list(ListOfFilters) -> {'and',ListOfFilters}. +-'or'(ListOfFilters) when list(ListOfFilters) -> {'or', ListOfFilters}. +-'not'(Filter) when tuple(Filter) -> {'not',Filter}. +- +-%%% +-%%% The following Filter parameters consist of an attribute +-%%% and an attribute value. Example: F("uid","tobbe") +-%%% +-equalityMatch(Desc, Value) -> {equalityMatch, av_assert(Desc, Value)}. +-greaterOrEqual(Desc, Value) -> {greaterOrEqual, av_assert(Desc, Value)}. +-lessOrEqual(Desc, Value) -> {lessOrEqual, av_assert(Desc, Value)}. +-approxMatch(Desc, Value) -> {approxMatch, av_assert(Desc, Value)}. +- +-av_assert(Desc, Value) -> +- #'AttributeValueAssertion'{attributeDesc = Desc, +- assertionValue = Value}. +- +-%%% +-%%% Filter to check for the presence of an attribute +-%%% +-present(Attribute) when list(Attribute) -> +- {present, Attribute}. +- +- +-%%% +-%%% A substring filter seem to be based on a pattern: +-%%% +-%%% InitValue*AnyValue*FinalValue +-%%% +-%%% where all three parts seem to be optional (at least when +-%%% talking with an OpenLDAP server). Thus, the arguments +-%%% to substrings/2 looks like this: +-%%% +-%%% Type ::= string( ) +-%%% SubStr ::= listof( {initial,Value} | {any,Value}, {final,Value}) +-%%% +-%%% Example: substrings("sn",[{initial,"To"},{any,"kv"},{final,"st"}]) +-%%% will match entries containing: 'sn: Tornkvist' +-%%% +-substrings(Type, SubStr) when list(Type), list(SubStr) -> +- Ss = {'SubstringFilter_substrings',v_substr(SubStr)}, +- {substrings,#'SubstringFilter'{type = Type, +- substrings = Ss}}. +- +- +-get_handle(Pid) when pid(Pid) -> Pid; +-get_handle(Atom) when atom(Atom) -> Atom; +-get_handle(Name) when list(Name) -> list_to_atom("eldap_" ++ Name). +-%%%---------------------------------------------------------------------- +-%%% Callback functions from gen_fsm +-%%%---------------------------------------------------------------------- +- +-%%---------------------------------------------------------------------- +-%% Func: init/1 +-%% Returns: {ok, StateName, StateData} | +-%% {ok, StateName, StateData, Timeout} | +-%% ignore | +-%% {stop, StopReason} +-%% I use the trick of setting a timeout of 0 to pass control into the +-%% process. +-%%---------------------------------------------------------------------- +-init([]) -> +- case get_config() of +- {ok, Hosts, Rootdn, Passwd, Log} -> +- init({Hosts, Rootdn, Passwd, Log}); +- {error, Reason} -> +- {stop, Reason} +- end; +-init({Hosts, Port, Rootdn, Passwd, Log}) -> +- {ok, connecting, #eldap{hosts = Hosts, +- port = Port, +- rootdn = Rootdn, +- passwd = Passwd, +- id = 0, +- log = Log, +- dict = dict:new(), +- debug_level = 0}, 0}. +- +-%%---------------------------------------------------------------------- +-%% Func: StateName/2 +-%% Called when gen_fsm:send_event/2,3 is invoked (async) +-%% Returns: {next_state, NextStateName, NextStateData} | +-%% {next_state, NextStateName, NextStateData, Timeout} | +-%% {stop, Reason, NewStateData} +-%%---------------------------------------------------------------------- +-connecting(timeout, S) -> +- {ok, NextState, NewS} = connect_bind(S), +- {next_state, NextState, NewS}. +- +-%%---------------------------------------------------------------------- +-%% Func: StateName/3 +-%% Called when gen_fsm:sync_send_event/2,3 is invoked. +-%% Returns: {next_state, NextStateName, NextStateData} | +-%% {next_state, NextStateName, NextStateData, Timeout} | +-%% {reply, Reply, NextStateName, NextStateData} | +-%% {reply, Reply, NextStateName, NextStateData, Timeout} | +-%% {stop, Reason, NewStateData} | +-%% {stop, Reason, Reply, NewStateData} +-%%---------------------------------------------------------------------- +-connecting(Event, From, S) -> +- Reply = {error, connecting}, +- {reply, Reply, connecting, S}. +- +-wait_bind_response(Event, From, S) -> +- Reply = {error, wait_bind_response}, +- {reply, Reply, wait_bind_response, S}. +- +-active(Event, From, S) -> +- case catch send_command(Event, From, S) of +- {ok, NewS} -> +- {next_state, active, NewS}; +- {error, Reason} -> +- {reply, {error, Reason}, active, S}; +- {'EXIT', Reason} -> +- {reply, {error, Reason}, active, S} +- end. +- +-%%---------------------------------------------------------------------- +-%% Func: handle_event/3 +-%% Called when gen_fsm:send_all_state_event/2 is invoked. +-%% Returns: {next_state, NextStateName, NextStateData} | +-%% {next_state, NextStateName, NextStateData, Timeout} | +-%% {stop, Reason, NewStateData} +-%%---------------------------------------------------------------------- +-handle_event(close, StateName, S) -> +- gen_tcp:close(S#eldap.fd), +- {stop, closed, S}; +- +-handle_event(Event, StateName, S) -> +- {next_state, StateName, S}. +- +-%%---------------------------------------------------------------------- +-%% Func: handle_sync_event/4 +-%% Called when gen_fsm:sync_send_all_state_event/2,3 is invoked +-%% Returns: {next_state, NextStateName, NextStateData} | +-%% {next_state, NextStateName, NextStateData, Timeout} | +-%% {reply, Reply, NextStateName, NextStateData} | +-%% {reply, Reply, NextStateName, NextStateData, Timeout} | +-%% {stop, Reason, NewStateData} | +-%% {stop, Reason, Reply, NewStateData} +-%%---------------------------------------------------------------------- +-handle_sync_event({debug_level, N}, From, StateName, S) -> +- {reply, ok, StateName, S#eldap{debug_level = N}}; +- +-handle_sync_event(Event, From, StateName, S) -> +- {reply, {StateName, S}, StateName, S}; +- +-handle_sync_event(Event, From, StateName, S) -> +- Reply = ok, +- {reply, Reply, StateName, S}. +- +-%%---------------------------------------------------------------------- +-%% Func: handle_info/3 +-%% Returns: {next_state, NextStateName, NextStateData} | +-%% {next_state, NextStateName, NextStateData, Timeout} | +-%% {stop, Reason, NewStateData} +-%%---------------------------------------------------------------------- +- +-%% +-%% Packets arriving in various states +-%% +-handle_info({tcp, Socket, Data}, connecting, S) -> +- log1("eldap. tcp packet received when disconnected!~n~p~n", [Data], S), +- {next_state, connecting, S}; +- +-handle_info({tcp, Socket, Data}, wait_bind_response, S) -> +- cancel_timer(S#eldap.bind_timer), +- case catch recvd_wait_bind_response(Data, S) of +- bound -> {next_state, active, S}; +- {fail_bind, Reason} -> close_and_retry(S), +- {next_state, connecting, S#eldap{fd = null}}; +- {'EXIT', Reason} -> close_and_retry(S), +- {next_state, connecting, S#eldap{fd = null}}; +- {error, Reason} -> close_and_retry(S), +- {next_state, connecting, S#eldap{fd = null}} +- end; +- +-handle_info({tcp, Socket, Data}, active, S) -> +- case catch recvd_packet(Data, S) of +- {reply, Reply, To, NewS} -> gen_fsm:reply(To, Reply), +- {next_state, active, NewS}; +- {ok, NewS} -> {next_state, active, NewS}; +- {'EXIT', Reason} -> {next_state, active, S}; +- {error, Reason} -> {next_state, active, S} +- end; +- +-handle_info({tcp_closed, Socket}, All_fsm_states, S) -> +- F = fun(Id, [{Timer, From, Name}|Res]) -> +- gen_fsm:reply(From, {error, tcp_closed}), +- cancel_timer(Timer) +- end, +- dict:map(F, S#eldap.dict), +- retry_connect(), +- {next_state, connecting, S#eldap{fd = null, +- dict = dict:new()}}; +- +-handle_info({tcp_error, Socket, Reason}, Fsm_state, S) -> +- log1("eldap received tcp_error: ~p~nIn State: ~p~n", [Reason, Fsm_state], S), +- {next_state, Fsm_state, S}; +-%% +-%% Timers +-%% +-handle_info({timeout, Timer, {cmd_timeout, Id}}, active, S) -> +- case cmd_timeout(Timer, Id, S) of +- {reply, To, Reason, NewS} -> gen_fsm:reply(To, Reason), +- {next_state, active, NewS}; +- {error, Reason} -> {next_state, active, S} +- end; +- +-handle_info({timeout, retry_connect}, connecting, S) -> +- {ok, NextState, NewS} = connect_bind(S), +- {next_state, NextState, NewS}; +- +-handle_info({timeout, Timer, bind_timeout}, wait_bind_response, S) -> +- close_and_retry(S), +- {next_state, connecting, S#eldap{fd = null}}; +- +-%% +-%% Make sure we don't fill the message queue with rubbish +-%% +-handle_info(Info, StateName, S) -> +- log1("eldap. Unexpected Info: ~p~nIn state: ~p~n when StateData is: ~p~n", +- [Info, StateName, S], S), +- {next_state, StateName, S}. +- +-%%---------------------------------------------------------------------- +-%% Func: terminate/3 +-%% Purpose: Shutdown the fsm +-%% Returns: any +-%%---------------------------------------------------------------------- +-terminate(Reason, StateName, StatData) -> +- ok. +- +-%%---------------------------------------------------------------------- +-%% Func: code_change/4 +-%% Purpose: Convert process state when code is changed +-%% Returns: {ok, NewState, NewStateData} +-%%---------------------------------------------------------------------- +-code_change(OldVsn, StateName, S, Extra) -> +- {ok, StateName, S}. +- +-%%%---------------------------------------------------------------------- +-%%% Internal functions +-%%%---------------------------------------------------------------------- +-send_command(Command, From, S) -> +- Id = bump_id(S), +- {Name, Request} = gen_req(Command), +- Message = #'LDAPMessage'{messageID = Id, +- protocolOp = {Name, Request}}, +- log2("~p~n",[{Name, Request}], S), +- {ok, Bytes} = asn1rt:encode('ELDAPv3', 'LDAPMessage', Message), +- ok = gen_tcp:send(S#eldap.fd, Bytes), +- Timer = erlang:start_timer(?CMD_TIMEOUT, self(), {cmd_timeout, Id}), +- New_dict = dict:store(Id, [{Timer, From, Name}], S#eldap.dict), +- {ok, S#eldap{id = Id, +- dict = New_dict}}. +- +-gen_req({search, A}) -> +- {searchRequest, +- #'SearchRequest'{baseObject = A#eldap_search.base, +- scope = v_scope(A#eldap_search.scope), +- derefAliases = neverDerefAliases, +- sizeLimit = 0, % no size limit +- timeLimit = v_timeout(A#eldap_search.timeout), +- typesOnly = v_bool(A#eldap_search.types_only), +- filter = v_filter(A#eldap_search.filter), +- attributes = v_attributes(A#eldap_search.attributes) +- }}; +-gen_req({add, Entry, Attrs}) -> +- {addRequest, +- #'AddRequest'{entry = Entry, +- attributes = Attrs}}; +-gen_req({delete, Entry}) -> +- {delRequest, Entry}; +-gen_req({modify, Obj, Mod}) -> +- v_modifications(Mod), +- {modifyRequest, +- #'ModifyRequest'{object = Obj, +- modification = Mod}}; +-gen_req({modify_dn, Entry, NewRDN, DelOldRDN, NewSup}) -> +- {modDNRequest, +- #'ModifyDNRequest'{entry = Entry, +- newrdn = NewRDN, +- deleteoldrdn = DelOldRDN, +- newSuperior = NewSup}}. +- +-%%----------------------------------------------------------------------- +-%% recvd_packet +-%% Deals with incoming packets in the active state +-%% Will return one of: +-%% {ok, NewS} - Don't reply to client yet as this is part of a search +-%% result and we haven't got all the answers yet. +-%% {reply, Result, From, NewS} - Reply with result to client From +-%% {error, Reason} +-%% {'EXIT', Reason} - Broke +-%%----------------------------------------------------------------------- +-recvd_packet(Pkt, S) -> +- check_tag(Pkt), +- case asn1rt:decode('ELDAPv3', 'LDAPMessage', Pkt) of +- {ok,Msg} -> +- Op = Msg#'LDAPMessage'.protocolOp, +- log2("~p~n",[Op], S), +- Dict = S#eldap.dict, +- Id = Msg#'LDAPMessage'.messageID, +- {Timer, From, Name, Result_so_far} = get_op_rec(Id, Dict), +- case {Name, Op} of +- {searchRequest, {searchResEntry, R}} when +- record(R,'SearchResultEntry') -> +- New_dict = dict:append(Id, R, Dict), +- {ok, S#eldap{dict = New_dict}}; +- {searchRequest, {searchResDone, Result}} -> +- case Result#'LDAPResult'.resultCode of +- success -> +- {Res, Ref} = polish(Result_so_far), +- New_dict = dict:erase(Id, Dict), +- cancel_timer(Timer), +- {reply, #eldap_search_result{entries = Res, +- referrals = Ref}, From, +- S#eldap{dict = New_dict}}; +- Reason -> +- New_dict = dict:erase(Id, Dict), +- cancel_timer(Timer), +- {reply, {error, Reason}, From, S#eldap{dict = New_dict}} +- end; +- {searchRequest, {searchResRef, R}} -> +- New_dict = dict:append(Id, R, Dict), +- {ok, S#eldap{dict = New_dict}}; +- {addRequest, {addResponse, Result}} -> +- New_dict = dict:erase(Id, Dict), +- cancel_timer(Timer), +- Reply = check_reply(Result, From), +- {reply, Reply, From, S#eldap{dict = New_dict}}; +- {delRequest, {delResponse, Result}} -> +- New_dict = dict:erase(Id, Dict), +- cancel_timer(Timer), +- Reply = check_reply(Result, From), +- {reply, Reply, From, S#eldap{dict = New_dict}}; +- {modifyRequest, {modifyResponse, Result}} -> +- New_dict = dict:erase(Id, Dict), +- cancel_timer(Timer), +- Reply = check_reply(Result, From), +- {reply, Reply, From, S#eldap{dict = New_dict}}; +- {modDNRequest, {modDNResponse, Result}} -> +- New_dict = dict:erase(Id, Dict), +- cancel_timer(Timer), +- Reply = check_reply(Result, From), +- {reply, Reply, From, S#eldap{dict = New_dict}}; +- {OtherName, OtherResult} -> +- New_dict = dict:erase(Id, Dict), +- cancel_timer(Timer), +- {reply, {error, {invalid_result, OtherName, OtherResult}}, +- From, S#eldap{dict = New_dict}} +- end; +- Error -> Error +- end. +- +-check_reply(#'LDAPResult'{resultCode = success}, From) -> +- ok; +-check_reply(#'LDAPResult'{resultCode = Reason}, From) -> +- {error, Reason}; +-check_reply(Other, From) -> +- {error, Other}. +- +-get_op_rec(Id, Dict) -> +- case dict:find(Id, Dict) of +- {ok, [{Timer, From, Name}|Res]} -> +- {Timer, From, Name, Res}; +- error -> +- throw({error, unkown_id}) +- end. +- +-%%----------------------------------------------------------------------- +-%% recvd_wait_bind_response packet +-%% Deals with incoming packets in the wait_bind_response state +-%% Will return one of: +-%% bound - Success - move to active state +-%% {fail_bind, Reason} - Failed +-%% {error, Reason} +-%% {'EXIT', Reason} - Broken packet +-%%----------------------------------------------------------------------- +-recvd_wait_bind_response(Pkt, S) -> +- check_tag(Pkt), +- case asn1rt:decode('ELDAPv3', 'LDAPMessage', Pkt) of +- {ok,Msg} -> +- log2("~p", [Msg], S), +- check_id(S#eldap.id, Msg#'LDAPMessage'.messageID), +- case Msg#'LDAPMessage'.protocolOp of +- {bindResponse, Result} -> +- case Result#'LDAPResult'.resultCode of +- success -> bound; +- Error -> {fail_bind, Error} +- end +- end; +- Else -> +- {fail_bind, Else} +- end. +- +-check_id(Id, Id) -> ok; +-check_id(_, _) -> throw({error, wrong_bind_id}). +- +-%%----------------------------------------------------------------------- +-%% General Helpers +-%%----------------------------------------------------------------------- +- +-cancel_timer(Timer) -> +- erlang:cancel_timer(Timer), +- receive +- {timeout, Timer, _} -> +- ok +- after 0 -> +- ok +- end. +- +- +-%%% Sanity check of received packet +-check_tag(Data) -> +- case asn1rt_ber:decode_tag(Data) of +- {Tag, Data1, Rb} -> +- case asn1rt_ber:decode_length(Data1) of +- {{Len,Data2}, Rb2} -> ok; +- _ -> throw({error,decoded_tag_length}) +- end; +- _ -> throw({error,decoded_tag}) +- end. +- +-close_and_retry(S) -> +- gen_tcp:close(S#eldap.fd), +- retry_connect(). +- +-retry_connect() -> +- erlang:send_after(?RETRY_TIMEOUT, self(), +- {timeout, retry_connect}). +- +- +-%%----------------------------------------------------------------------- +-%% Sort out timed out commands +-%%----------------------------------------------------------------------- +-cmd_timeout(Timer, Id, S) -> +- Dict = S#eldap.dict, +- case dict:find(Id, Dict) of +- {ok, [{Id, Timer, From, Name}|Res]} -> +- case Name of +- searchRequest -> +- {Res1, Ref1} = polish(Res), +- New_dict = dict:erase(Id, Dict), +- {reply, From, {timeout, +- #eldap_search_result{entries = Res1, +- referrals = Ref1}}, +- S#eldap{dict = New_dict}}; +- Others -> +- New_dict = dict:erase(Id, Dict), +- {reply, From, {error, timeout}, S#eldap{dict = New_dict}} +- end; +- error -> +- {error, timed_out_cmd_not_in_dict} +- end. +- +-%%----------------------------------------------------------------------- +-%% Common stuff for results +-%%----------------------------------------------------------------------- +-%%% +-%%% Polish the returned search result +-%%% +- +-polish(Entries) -> +- polish(Entries, [], []). +- +-polish([H|T], Res, Ref) when record(H, 'SearchResultEntry') -> +- ObjectName = H#'SearchResultEntry'.objectName, +- F = fun({_,A,V}) -> {A,V} end, +- Attrs = lists:map(F, H#'SearchResultEntry'.attributes), +- polish(T, [#eldap_entry{object_name = ObjectName, +- attributes = Attrs}|Res], Ref); +-polish([H|T], Res, Ref) -> % No special treatment of referrals at the moment. +- polish(T, Res, [H|Ref]); +-polish([], Res, Ref) -> +- {Res, Ref}. +- +-%%----------------------------------------------------------------------- +-%% Connect to next server in list and attempt to bind to it. +-%%----------------------------------------------------------------------- +-connect_bind(S) -> +- Host = next_host(S#eldap.host, S#eldap.hosts), +- TcpOpts = [{packet, asn1}, {active, true}], +- case gen_tcp:connect(Host, S#eldap.port, TcpOpts) of +- {ok, Socket} -> +- case bind_request(Socket, S) of +- {ok, NewS} -> +- Timer = erlang:start_timer(?BIND_TIMEOUT, self(), +- {timeout, bind_timeout}), +- {ok, wait_bind_response, NewS#eldap{fd = Socket, +- host = Host, +- bind_timer = Timer}}; +- {error, Reason} -> +- gen_tcp:close(Socket), +- erlang:send_after(?RETRY_TIMEOUT, self(), +- {timeout, retry_connect}), +- {ok, connecting, S#eldap{host = Host}} +- end; +- {error, Reason} -> +- erlang:send_after(?RETRY_TIMEOUT, self(), +- {timeout, retry_connect}), +- {ok, connecting, S#eldap{host = Host}} +- end. +- +-bind_request(Socket, S) -> +- Id = bump_id(S), +- Req = #'BindRequest'{version = S#eldap.version, +- name = S#eldap.rootdn, +- authentication = {simple, S#eldap.passwd}}, +- Message = #'LDAPMessage'{messageID = Id, +- protocolOp = {bindRequest, Req}}, +- log2("Message:~p~n",[Message], S), +- {ok, Bytes} = asn1rt:encode('ELDAPv3', 'LDAPMessage', Message), +- ok = gen_tcp:send(Socket, Bytes), +- {ok, S#eldap{id = Id}}. +- +-%% Given last tried Server, find next one to try +-next_host(null, [H|_]) -> H; % First time, take first +-next_host(Host, Hosts) -> % Find next in turn +- next_host(Host, Hosts, Hosts). +- +-next_host(Host, [Host], Hosts) -> hd(Hosts); % Wrap back to first +-next_host(Host, [Host|Tail], Hosts) -> hd(Tail); % Take next +-next_host(Host, [], Hosts) -> hd(Hosts); % Never connected before? (shouldn't happen) +-next_host(Host, [H|T], Hosts) -> next_host(Host, T, Hosts). +- +- +-%%% -------------------------------------------------------------------- +-%%% Verify the input data +-%%% -------------------------------------------------------------------- +- +-v_filter({'and',L}) -> {'and',L}; +-v_filter({'or', L}) -> {'or',L}; +-v_filter({'not',L}) -> {'not',L}; +-v_filter({equalityMatch,AV}) -> {equalityMatch,AV}; +-v_filter({greaterOrEqual,AV}) -> {greaterOrEqual,AV}; +-v_filter({lessOrEqual,AV}) -> {lessOrEqual,AV}; +-v_filter({approxMatch,AV}) -> {approxMatch,AV}; +-v_filter({present,A}) -> {present,A}; +-v_filter({substrings,S}) when record(S,'SubstringFilter') -> {substrings,S}; +-v_filter(_Filter) -> throw({error,concat(["unknown filter: ",_Filter])}). +- +-v_modifications(Mods) -> +- F = fun({_,Op,_}) -> +- case lists:member(Op,[add,delete,replace]) of +- true -> true; +- _ -> throw({error,{mod_operation,Op}}) +- end +- end, +- lists:foreach(F, Mods). +- +-v_substr([{Key,Str}|T]) when list(Str),Key==initial;Key==any;Key==final -> +- [{Key,Str}|v_substr(T)]; +-v_substr([H|T]) -> +- throw({error,{substring_arg,H}}); +-v_substr([]) -> +- []. +-v_scope(baseObject) -> baseObject; +-v_scope(singleLevel) -> singleLevel; +-v_scope(wholeSubtree) -> wholeSubtree; +-v_scope(_Scope) -> throw({error,concat(["unknown scope: ",_Scope])}). +- +-v_bool(true) -> true; +-v_bool(false) -> false; +-v_bool(_Bool) -> throw({error,concat(["not Boolean: ",_Bool])}). +- +-v_timeout(I) when integer(I), I>=0 -> I; +-v_timeout(_I) -> throw({error,concat(["timeout not positive integer: ",_I])}). +- +-v_attributes(Attrs) -> +- F = fun(A) when list(A) -> A; +- (A) -> throw({error,concat(["attribute not String: ",A])}) +- end, +- lists:map(F,Attrs). +- +- +-%%% -------------------------------------------------------------------- +-%%% Get and Validate the initial configuration +-%%% -------------------------------------------------------------------- +-get_config() -> +- Priv_dir = code:priv_dir(eldap), +- File = filename:join(Priv_dir, "eldap.conf"), +- case file:consult(File) of +- {ok, Entries} -> +- case catch parse(Entries) of +- {ok, Hosts, Port, Rootdn, Passwd, Log} -> +- {ok, Hosts, Port, Rootdn, Passwd, Log}; +- {error, Reason} -> +- {error, Reason}; +- {'EXIT', Reason} -> +- {error, Reason} +- end; +- {error, Reason} -> +- {error, Reason} +- end. +- +-parse(Entries) -> +- {ok, +- get_hosts(host, Entries), +- get_integer(port, Entries), +- get_list(rootdn, Entries), +- get_list(passwd, Entries), +- get_log(log, Entries)}. +- +-get_integer(Key, List) -> +- case lists:keysearch(Key, 1, List) of +- {value, {Key, Value}} when integer(Value) -> +- Value; +- {value, {Key, Value}} -> +- throw({error, "Bad Value in Config for " ++ atom_to_list(Key)}); +- false -> +- throw({error, "No Entry in Config for " ++ atom_to_list(Key)}) +- end. +- +-get_list(Key, List) -> +- case lists:keysearch(Key, 1, List) of +- {value, {Key, Value}} when list(Value) -> +- Value; +- {value, {Key, Value}} -> +- throw({error, "Bad Value in Config for " ++ atom_to_list(Key)}); +- false -> +- throw({error, "No Entry in Config for " ++ atom_to_list(Key)}) +- end. +- +-get_log(Key, List) -> +- case lists:keysearch(Key, 1, List) of +- {value, {Key, Value}} when function(Value) -> +- Value; +- {value, {Key, Else}} -> +- false; +- false -> +- fun(Level, Format, Args) -> io:format("--- " ++ Format, Args) end +- end. +- +-get_hosts(Key, List) -> +- lists:map(fun({Key1, {A,B,C,D}}) when integer(A), +- integer(B), +- integer(C), +- integer(D), +- Key == Key1-> +- {A,B,C,D}; +- ({Key1, Value}) when list(Value), +- Key == Key1-> +- Value; +- ({Else, Value}) -> +- throw({error, "Bad Hostname in config"}) +- end, List). +- +-%%% -------------------------------------------------------------------- +-%%% Other Stuff +-%%% -------------------------------------------------------------------- +-bump_id(#eldap{id = Id}) when Id > ?MAX_TRANSACTION_ID -> +- ?MIN_TRANSACTION_ID; +-bump_id(#eldap{id = Id}) -> +- Id + 1. +- +-%%% -------------------------------------------------------------------- +-%%% Log routines. Call a user provided log routine Fun. +-%%% -------------------------------------------------------------------- +- +-log1(Str, Args, #eldap{log = Fun, debug_level = N}) -> log(Fun, Str, Args, 1, N). +-log2(Str, Args, #eldap{log = Fun, debug_level = N}) -> log(Fun, Str, Args, 2, N). +- +-log(Fun, Str, Args, This_level, Status) when function(Fun), This_level =< Status -> +- catch Fun(This_level, Str, Args); +-log(_, _, _, _, _) -> +- ok. diff --git a/rabbitmq-server/plugins-src/eldap-wrapper/remove-ietf-doc.patch b/rabbitmq-server/plugins-src/eldap-wrapper/remove-ietf-doc.patch new file mode 100644 index 0000000..e1f55d9 --- /dev/null +++ b/rabbitmq-server/plugins-src/eldap-wrapper/remove-ietf-doc.patch @@ -0,0 +1,3036 @@ +diff --git a/doc/draft-ietf-asid-ldap-c-api-00.txt b/doc/draft-ietf-asid-ldap-c-api-00.txt +deleted file mode 100755 +index 5f2e856..0000000 +--- a/doc/draft-ietf-asid-ldap-c-api-00.txt ++++ /dev/null +@@ -1,3030 +0,0 @@ +- +- +- +- +- +- +-Network Working Group T. Howes +-INTERNET-DRAFT Netscape Communications Corp. +-Intended Category: Standards Track M. Smith +-Obsoletes: RFC 1823 Netscape Communications Corp. +-Expires: January 1998 A. Herron +- Microsoft Corp. +- C. Weider +- Microsoft Corp. +- M. Wahl +- Critical Angle, Inc. +- +- 29 July 1997 +- +- +- The C LDAP Application Program Interface +- +- +- +- +-1. Status of this Memo +- +-This draft document will be submitted to the RFC Editor as a Standards +-Track document. Distribution of this memo is unlimited. Please send com- +-ments to the authors. +- +-This document is an Internet-Draft. Internet-Drafts are working docu- +-ments of the Internet Engineering Task Force (IETF), its areas, and its +-working groups. Note that other groups may also distribute working +-documents as Internet-Drafts. +- +-Internet-Drafts are draft documents valid for a maximum of six months +-and may be updated, replaced, or obsoleted by other documents at any +-time. It is inappropriate to use Internet-Drafts as reference material +-or to cite them other than as ``work in progress.'' +- +-To learn the current status of any Internet-Draft, please check the +-``1id-abstracts.txt'' listing contained in the Internet-Drafts Shadow +-Directories on ds.internic.net (US East Coast), nic.nordu.net (Europe), +-ftp.isi.edu (US West Coast), or munnari.oz.au (Pacific Rim). +- +-2. Introduction +- +-This document defines a C language application program interface to the +-lightweight directory access protocol (LDAP). This document replaces the +-previous definition of this API, defined in RFC 1823, updating it to +-include support for features found in version 3 of the LDAP protocol. +-New extended operation functions were added to support LDAPv3 features +-such as controls. In addition, other LDAP API changes were made to +- +- +- +-Expires: January 1998 [Page 1] +- +-C LDAP API The C LDAP Application Program Interface 29 July 1997 +- +- +-support information hiding and thread safety. +- +-The C LDAP API is designed to be powerful, yet simple to use. It defines +-compatible synchronous and asynchronous interfaces to LDAP to suit a +-wide variety of applications. This document gives a brief overview of +-the LDAP model, then an overview of how the API is used by an applica- +-tion program to obtain LDAP information. The API calls are described in +-detail, followed by an appendix that provides some example code demon- +-strating the use of the API. This document provides information to the +-Internet community. It does not specify any standard. +- +-3. Overview of the LDAP Model +- +-LDAP is the lightweight directory access protocol, described in [2] and +-[6]. It can provide a lightweight frontend to the X.500 directory [1], +-or a stand-alone service. In either mode, LDAP is based on a client- +-server model in which a client makes a TCP connection to an LDAP server, +-over which it sends requests and receives responses. +- +-The LDAP information model is based on the entry, which contains infor- +-mation about some object (e.g., a person). Entries are composed of +-attributes, which have a type and one or more values. Each attribute has +-a syntax that determines what kinds of values are allowed in the attri- +-bute (e.g., ASCII characters, a jpeg photograph, etc.) and how those +-values behave during directory operations (e.g., is case significant +-during comparisons). +- +-Entries may be organized in a tree structure, usually based on politi- +-cal, geographical, and organizational boundaries. Each entry is uniquely +-named relative to its sibling entries by its relative distinguished name +-(RDN) consisting of one or more distinguished attribute values from the +-entry. At most one value from each attribute may be used in the RDN. +-For example, the entry for the person Babs Jensen might be named with +-the "Barbara Jensen" value from the commonName attribute. +- +-A globally unique name for an entry, called a distinguished name or DN, +-is constructed by concatenating the sequence of RDNs from the entry up +-to the root of the tree. For example, if Babs worked for the University +-of Michigan, the DN of her U-M entry might be "cn=Barbara Jensen, +-o=University of Michigan, c=US". The DN format used by LDAP is defined +-in [4]. +- +-Operations are provided to authenticate, search for and retrieve infor- +-mation, modify information, and add and delete entries from the tree. +-The next sections give an overview of how the API is used and detailed +-descriptions of the LDAP API calls that implement all of these func- +-tions. +- +- +- +- +-Expires: January 1998 [Page 2] +- +-C LDAP API The C LDAP Application Program Interface 29 July 1997 +- +- +-4. Overview of LDAP API Use +- +-An application generally uses the C LDAP API in four simple steps. +- +-- Initialize an LDAP session with a default LDAP server. The +- ldap_init() function returns a handle to the session, allowing mul- +- tiple connections to be open at once. +- +-- Authenticate to the LDAP server. The ldap_bind() function and +- friends support a variety of authentication methods. +- +-- Perform some LDAP operations and obtain some results. ldap_search() +- and friends return results which can be parsed by +- ldap_result2error(), ldap_first_entry(), ldap_next_entry(), etc. +- +-- Close the session. The ldap_unbind() function closes the connec- +- tion. +- +-Operations can be performed either synchronously or asynchronously. The +-names of the synchronous functions end in _s. For example, a synchronous +-search can be completed by calling ldap_search_s(). An asynchronous +-search can be initiated by calling ldap_search(). All synchronous rou- +-tines return an indication of the outcome of the operation (e.g, the +-constant LDAP_SUCCESS or some other error code). The asynchronous rou- +-tines return the message id of the operation initiated. This id can be +-used in subsequent calls to ldap_result() to obtain the result(s) of the +-operation. An asynchronous operation can be abandoned by calling +-ldap_abandon(). +- +-Results and errors are returned in an opaque structure called LDAPMes- +-sage. Routines are provided to parse this structure, step through +-entries and attributes returned, etc. Routines are also provided to +-interpret errors. Later sections of this document describe these rou- +-tines in more detail. +- +-LDAP version 3 servers may return referrals to other servers. By +-default, implementations of this API will attempt to follow referrals +-automatically for the application. This behavior can be disabled glo- +-bally (using the ldap_set_option() call) or on a per-request basis +-through the use of a client control. +- +-As in the LDAPv3 protocol itself, all DNs and string values that are +-passed into or produced by the C LDAP API are represented as UTF-8[10] +-characters. +- +-For compatibility with existing applications, implementations of this +-API will by default use version 2 of the LDAP protocol. Applications +-that intend to take advantage of LDAP version 3 features will need to +- +- +- +-Expires: January 1998 [Page 3] +- +-C LDAP API The C LDAP Application Program Interface 29 July 1997 +- +- +-use the ldap_set_option() call with a LDAP_OPT_PROTOCOL_VERSION to +-switch to version 3. +- +- +-5. Common Data Structures +- +-Some data structures that are common to several LDAP API functions are +-defined here: +- +- typedef struct ldap LDAP; +- +- typedef struct ldapmsg LDAPMessage; +- +- struct berval { +- unsigned long bv_len; +- char *bv_val; +- }; +- +- struct timeval { +- long tv_sec; +- long tv_usec; +- }; +- +-The LDAP structure is an opaque data type that represents an LDAP ses- +-sion Typically this corresponds to a connection to a single server, but +-it may encompass several server connections in the face of LDAPv3 refer- +-rals. +- +-The LDAPMessage structure is an opaque data type that is used to return +-results and error information. +- +-The berval structure is used to represent arbitrary binary data and its +-fields have the following meanings: +- +-bv_len Length of data in bytes. +- +-bv_val A pointer to the data itself. +- +- +-The timeval structure is used to represent an interval of time and its +-fields have the following meanings: +- +-tv_sec Seconds component of time interval. +- +-tv_usec Microseconds component of time interval. +- +- +- +- +- +- +-Expires: January 1998 [Page 4] +- +-C LDAP API The C LDAP Application Program Interface 29 July 1997 +- +- +-6. LDAP Error Codes +- +-Many of the LDAP API routines return LDAP error codes, some of which +-indicate local errors and some of which may be returned by servers. +-Supported error codes are (hexadecimal values are given in parentheses +-after the constant): +- +- LDAP_SUCCESS (0x00) +- LDAP_OPERATIONS_ERROR( 0x01) +- LDAP_PROTOCOL_ERROR (0x02) +- LDAP_TIMELIMIT_EXCEEDED (0x03) +- LDAP_SIZELIMIT_EXCEEDED (0x04) +- LDAP_COMPARE_FALSE (0x05) +- LDAP_COMPARE_TRUE (0x06) +- LDAP_STRONG_AUTH_NOT_SUPPORTED (0x07) +- LDAP_STRONG_AUTH_REQUIRED (0x08) +- LDAP_REFERRAL (0x0a) -- new in LDAPv3 +- LDAP_ADMINLIMIT_EXCEEDED (0x0b) -- new in LDAPv3 +- LDAP_UNAVAILABLE_CRITICAL_EXTENSION (0x0c) -- new in LDAPv3 +- LDAP_CONFIDENTIALITY_REQUIRED (0x0d) -- new in LDAPv3 +- LDAP_NO_SUCH_ATTRIBUTE (0x10) +- LDAP_UNDEFINED_TYPE (0x11) +- LDAP_INAPPROPRIATE_MATCHING (0x12) +- LDAP_CONSTRAINT_VIOLATION (0x13) +- LDAP_TYPE_OR_VALUE_EXISTS (0x14) +- LDAP_INVALID_SYNTAX (0x15) +- LDAP_NO_SUCH_OBJECT (0x20) +- LDAP_ALIAS_PROBLEM (0x21) +- LDAP_INVALID_DN_SYNTAX (0x22) +- LDAP_IS_LEAF (0x23) -- not used in LDAPv3 +- LDAP_ALIAS_DEREF_PROBLEM (0x24) +- LDAP_INAPPROPRIATE_AUTH (0x30) +- LDAP_INVALID_CREDENTIALS (0x31) +- LDAP_INSUFFICIENT_ACCESS (0x32) +- LDAP_BUSY (0x33) +- LDAP_UNAVAILABLE (0x34) +- LDAP_UNWILLING_TO_PERFORM (0x35) +- LDAP_LOOP_DETECT (0x36) +- LDAP_NAMING_VIOLATION (0x40) +- LDAP_OBJECT_CLASS_VIOLATION (0x41) +- LDAP_NOT_ALLOWED_ON_NONLEAF (0x42) +- LDAP_NOT_ALLOWED_ON_RDN (0x43) +- LDAP_ALREADY_EXISTS (0x44) +- LDAP_NO_OBJECT_CLASS_MODS (0x45) +- LDAP_RESULTS_TOO_LARGE (0x46) +- LDAP_AFFECTS_MULTIPLE_DSAS (0x47) -- new in LDAPv3 +- LDAP_OTHER (0x50) +- LDAP_SERVER_DOWN (0x51) +- +- +- +-Expires: January 1998 [Page 5] +- +-C LDAP API The C LDAP Application Program Interface 29 July 1997 +- +- +- LDAP_LOCAL_ERROR (0x52) +- LDAP_ENCODING_ERROR (0x53) +- LDAP_DECODING_ERROR (0x54) +- LDAP_TIMEOUT (0x55) +- LDAP_AUTH_UNKNOWN (0x56) +- LDAP_FILTER_ERROR (0x57) +- LDAP_USER_CANCELLED (0x58) +- LDAP_PARAM_ERROR (0x59) +- LDAP_NO_MEMORY (0x5a) +- LDAP_CONNECT_ERROR (0x5b) +- LDAP_NOT_SUPPORTED (0x5c) +- LDAP_CONTROL_NOT_FOUND (0x5d) +- LDAP_NO_RESULTS_RETURNED (0x5e) +- LDAP_MORE_RESULTS_TO_RETURN (0x5f) +- LDAP_CLIENT_LOOP (0x60) +- LDAP_REFERRAL_LIMIT_EXCEEDED (0x61) +- +- +-7. Performing LDAP Operations +- +-This section describes each LDAP operation API call in detail. All func- +-tions take a "session handle," a pointer to an LDAP structure containing +-per-connection information. Many routines return results in an LDAPMes- +-sage structure. These structures and others are described as needed +-below. +- +- +-7.1. Initializing an LDAP Session +- +-ldap_init() initializes a session with an LDAP server. The server is not +-actually contacted until an operation is performed that requires it, +-allowing various options to be set after initialization. +- +- LDAP *ldap_init( +- char *hostname, +- int portno +- ); +- +-Use of the following routine is deprecated. +- +- LDAP *ldap_open( +- char *hostname, +- int portno +- ); +- +-Parameters are: +- +-hostname Contains a space-separated list of hostnames or dotted strings +- +- +- +-Expires: January 1998 [Page 6] +- +-C LDAP API The C LDAP Application Program Interface 29 July 1997 +- +- +- representing the IP address of hosts running an LDAP server to +- connect to. Each hostname in the list can include an optional +- port number which is separated from the host itself with a +- colon (:) character. The hosts are tried in the order listed, +- stopping with the first one to which a successful connection is +- made. Note that only ldap_open() attempts to make the connec- +- tion before returning to the caller. ldap_init() does not con- +- nect to the LDAP server. +- +-portno Contains the TCP port number to connect to. The default LDAP +- port of 389 can be obtained by supplying the constant +- LDAP_PORT. If a host includes a port number then this parame- +- ter is ignored. +- +-ldap_init() and ldap_open() both return a "session handle," a pointer to +-an opaque structure that should be passed to subsequent calls pertaining +-to the session. These routines return NULL if the session cannot be ini- +-tialized in which case the operating system error reporting mechanism +-can be checked to see why the call failed. +- +-Note that if you connect to an LDAPv2 server, one of the ldap_bind() +-calls described below must be completed before other operations can be +-performed on the session. LDAPv3 does not require that a bind operation +-be completed before other operations can be performed. +- +-The calling program can set various attributes of the session by calling +-the routines described in the next section. +- +- +-7.2. LDAP Session Handle Options +- +-The LDAP session handle returned by ldap_init() is a pointer to an +-opaque data type representing an LDAP session. Formerly, this data type +-was a structure exposed to the caller, and various fields in the struc- +-ture could be set to control aspects of the session, such as size and +-time limits on searches. +- +-In the interest of insulating callers from inevitable changes to this +-structure, these aspects of the session are now accessed through a pair +-of accessor functions, described below. +- +-ldap_get_option() is used to access the current value of various +-session-wide parameters. ldap_set_option() is used to set the value of +-these parameters. +- +- int ldap_get_option( +- LDAP *ld, +- int option, +- +- +- +-Expires: January 1998 [Page 7] +- +-C LDAP API The C LDAP Application Program Interface 29 July 1997 +- +- +- void *outvalue +- ); +- +- int ldap_set_option( +- LDAP *ld, +- int option, +- void *invalue +- ); +- +-Parameters are: +- +-ld The session handle. +- +-option The name of the option being accessed or set. This parameter +- should be one of the following constants, which have the indi- +- cated meanings. After the constant the actual value of the con- +- stant is listed in hexadecimal in parentheses followed by the +- type of the corresponding outvalue or invalue parameter. +- +- LDAP_OPT_DESC (0x01) int * +- The underlying socket descriptor corresponding to the default +- LDAP connection. +- +- LDAP_OPT_DEREF (0x02) int * +- Controls how aliases are handled during search. It can have +- one of the following values: LDAP_DEREF_NEVER (0x00), +- LDAP_DEREF_SEARCHING (0x01), LDAP_DEREF_FINDING (0x02), or +- LDAP_DEREF_ALWAYS (0x03). The LDAP_DEREF_SEARCHING value +- means aliases should be dereferenced during the search but not +- when locating the base object of the search. The +- LDAP_DEREF_FINDING value means aliases should be dereferenced +- when locating the base object but not during the search. +- +- LDAP_OPT_SIZELIMIT (0x03) int * +- A limit on the number of entries to return from a search. A +- value of zero means no limit. +- +- LDAP_OPT_TIMELIMIT (0x04) int * +- A limit on the number of seconds to spend on a search. A value +- of zero means no limit +- +- LDAP_OPT_REBIND_FN (0x06) function pointer +- See the discussion of ldap_bind() and friends below. +- +- LDAP_OPT_REBIND_ARG (0x07) void * +- See the discussion of ldap_bind() and friends below. +- +- LDAP_OPT_REFERRALS (0x08) void * +- +- +- +-Expires: January 1998 [Page 8] +- +-C LDAP API The C LDAP Application Program Interface 29 July 1997 +- +- +- This option controls whether the LDAP library automatically +- follows referrals returned by LDAP servers or not. It can be +- set to one of the constants LDAP_OPT_ON or LDAP_OPT_OFF. +- +- LDAP_OPT_RESTART (0x09) void * +- This option controls whether LDAP I/O operations should +- automatically be restarted if they abort prematurely. It +- should be set to one of the constants LDAP_OPT_ON or +- LDAP_OPT_OFF. This option is useful if an LDAP I/O operation +- may be interrupted prematurely, for example by a timer going +- off, or other interrrupt. +- +- LDAP_OPT_PROTOCOL_VERSION (0x11) int * +- This option indicates the version of the default LDAP server. +- It can be one of the constants LDAP_VERSION2 or LDAP_VERSION3. +- If no version is set the default is LDAP_VERSION2. +- +- LDAP_OPT_SERVER_CONTROLS (0x12) LDAPControl ** +- A default list of LDAP server controls to be sent with each +- request. See the Using Controls section below. +- +- LDAP_OPT_CLIENT_CONTROLS (0x13) LDAPControl ** +- A default list of client controls that affect the LDAP ses- +- sion. See the Using Controls section below. +- +- LDAP_OPT_HOST_NAME (0x30) char ** +- The host name of the default LDAP server. +- +- LDAP_OPT_ERROR_NUMBER (0x31) int * +- The code of the most recent LDAP error that occurred for this +- session. +- +- LDAP_OPT_ERROR_STRING (0x32) char ** +- The message returned with the most recent LDAP error that +- occurred for this session. +- +- +-outvalue The address of a place to put the value of the option. The +- actual type of this parameter depends on the setting of the +- option parameter. +- +-invalue A pointer to the value the option is to be given. The actual +- type of this parameter depends on the setting of the option +- parameter. The constants LDAP_OPT_ON and LDAP_OPT_OFF can be +- given for options that have on or off settings. +- +- +- +- +- +- +-Expires: January 1998 [Page 9] +- +-C LDAP API The C LDAP Application Program Interface 29 July 1997 +- +- +-7.3. Working with controls +- +-LDAPv3 operations can be extended through the use of controls. Controls +-may be sent to a server or returned to the client with any LDAP message. +-These controls are referred to as server controls. +- +-The LDAP API also supports a client-side extension mechanism through the +-use of client controls. These controls affect the behavior of the LDAP +-API only and are never sent to a server. A common data structure is +-used to represent both types of controls: +- +- typedef struct ldapcontrol { +- char *ldctl_oid; +- struct berval ldctl_value; +- char ldctl_iscritical; +- } LDAPControl, *PLDAPControl; +- +-The fields in the ldapcontrol structure have the following meanings: +- +-ldctl_oid The control type, represented as a string. +- +-ldctl_value The data associated with the control (if any). +- +-ldctl_iscritical Indicates whether the control is critical of not. If +- this field is non-zero, the operation will only be car- +- ried out if the control is recognized by the server +- and/or client. +- +-Some LDAP API calls allocate an ldapcontrol structure or a NULL- +-terminated array of ldapcontrol structures. The following routines can +-be used to dispose of a single control or an array of controls: +- +- void ldap_control_free( LDAPControl *ctrl ); +- void ldap_controls_free( LDAPControl **ctrls ); +- +-A set of controls that affect the entire session can be set using the +-ldap_set_option() function (see above). A list of controls can also be +-passed directly to some LDAP API calls such as ldap_search_ext(), in +-which case any controls set for the session through the use of +-ldap_set_option() are ignored. Control lists are represented as a NULL- +-terminated array of pointers to ldapcontrol structures. +- +-Server controls are defined by LDAPv3 protocol extension documents; for +-example, a control has been proposed to support server-side sorting of +-search results [7]. +- +-No client controls are defined by this document but they may be defined +-in future revisions or in any document that extends this API. +- +- +- +-Expires: January 1998 [Page 10] +- +-C LDAP API The C LDAP Application Program Interface 29 July 1997 +- +- +-7.4. Authenticating to the directory +- +-The following functions are used to authenticate an LDAP client to an +-LDAP directory server. +- +-The ldap_sasl_bind() and ldap_sasl_bind_s() functions can be used to do +-general and extensible authentication over LDAP through the use of the +-Simple Authentication Security Layer [8]. The routines both take the dn +-to bind as, the method to use, as a dotted-string representation of an +-OID identifying the method, and a struct berval holding the credentials. +-The special constant value LDAP_SASL_SIMPLE ("") can be passed to +-request simple authentication, or the simplified routines +-ldap_simple_bind() or ldap_simple_bind_s() can be used. +- +- int ldap_sasl_bind( +- LDAP *ld, +- char *dn, +- char *mechanism, +- struct berval *cred, +- LDAPControl **serverctrls, +- LDAPControl **clientctrls, +- int *msgidp +- ); +- +- int ldap_sasl_bind_s( +- LDAP *ld, +- char *dn, +- char *mechanism, +- struct berval *cred, +- LDAPControl **serverctrls, +- LDAPControl **clientctrls, +- struct berval **servercredp +- ); +- +- int ldap_simple_bind( +- LDAP *ld, +- char *dn, +- char *passwd +- ); +- +- int ldap_simple_bind_s( +- LDAP *ld, +- char *dn, +- char *passwd +- ); +- +- The use of the following routines is deprecated: +- +- +- +- +-Expires: January 1998 [Page 11] +- +-C LDAP API The C LDAP Application Program Interface 29 July 1997 +- +- +- int ldap_bind( LDAP *ld, char *dn, char *cred, int method ); +- +- int ldap_bind_s( LDAP *ld, char *dn, char *cred, int method ); +- +- int ldap_kerberos_bind( LDAP *ld, char *dn ); +- +- int ldap_kerberos_bind_s( LDAP *ld, char *dn ); +- +-Parameters are: +- +-ld The session handle. +- +-dn The name of the entry to bind as. +- +-mechanism Either LDAP_AUTH_SIMPLE_OID to get simple authentication, +- or a dotted text string representing an OID identifying the +- SASL method. +- +-cred The credentials with which to authenticate. Arbitrary +- credentials can be passed using this parameter. The format +- and content of the credentials depends on the setting of +- the mechanism parameter. +- +-passwd For ldap_simple_bind(), the password to compare to the +- entry's userPassword attribute. +- +-serverctrls List of LDAP server controls. +- +-clientctrls List of client controls. +- +-msgidp This result parameter will be set to the message id of the +- request if the ldap_sasl_bind() call succeeds. +- +-servercredp This result parameter will be set to the credentials +- returned by the server. This should be freed by calling +- ldap_If no credentials are returned it will be set to NULL. +- +-Additional parameters for the deprecated routines are not described. +-Interested readers are referred to RFC 1823. +- +-The ldap_sasl_bind() function initiates an asynchronous bind operation +-and returns the constant LDAP_SUCCESS if the request was successfully +-sent, or another LDAP error code if not. See the section below on error +-handling for more information about possible errors and how to interpret +-them. If successful, ldap_sasl_bind() places the message id of the +-request in *msgidp. A subsequent call to ldap_result(), described below, +-can be used to obtain the result of the bind. +- +- +- +- +-Expires: January 1998 [Page 12] +- +-C LDAP API The C LDAP Application Program Interface 29 July 1997 +- +- +-The ldap_simple_bind() function initiates a simple asynchronous bind +-operation and returns the message id of the operation initiated. A sub- +-sequent call to ldap_result(), described below, can be used to obtain +-the result of the bind. In case of error, ldap_simple_bind() will return +--1, setting the session error parameters in the LDAP structure appropri- +-ately. +- +-The synchronous ldap_sasl_bind_s() and ldap_simple_bind_s() functions +-both return the result of the operation, either the constant +-LDAP_SUCCESS if the operation was successful, or another LDAP error code +-if it was not. See the section below on error handling for more informa- +-tion about possible errors and how to interpret them. +- +-Note that if an LDAPv2 server is contacted, no other operations over the +-connection should be attempted before a bind call has successfully com- +-pleted. +- +-Subsequent bind calls can be used to re-authenticate over the same con- +-nection, and multistep SASL sequences can be accomplished through a +-sequence of calls to ldap_sasl_bind() or ldap_sasl_bind_s(). +- +- +-7.5. Closing the session +- +-The following functions are used to unbind from the directory, close the +-connection, and dispose of the session handle. +- +- int ldap_unbind( LDAP *ld ); +- +- int ldap_unbind_s( LDAP *ld ); +- +-Parameters are: +- +-ld The session handle. +- +-ldap_unbind() and ldap_unbind_s() both work synchronously, unbinding +-from the directory, closing the connection, and freeing up the ld struc- +-ture before returning. There is no server response to an unbind opera- +-tion. ldap_unbind() returns LDAP_SUCCESS (or another LDAP error code if +-the request cannot be sent to the LDAP server). After a call to +-ldap_unbind() or ldap_unbind_s(), the session handle ld is invalid. +- +- +-7.6. Searching +- +-The following functions are used to search the LDAP directory, returning +-a requested set of attributes for each entry matched. There are five +-variations. +- +- +- +-Expires: January 1998 [Page 13] +- +-C LDAP API The C LDAP Application Program Interface 29 July 1997 +- +- +- int ldap_search_ext( +- LDAP *ld, +- char *base, +- int scope, +- char *filter, +- char **attrs, +- int attrsonly, +- LDAPControl **serverctrls, +- LDAPControl **clientctrls, +- struct timeval *timeoutp, +- int sizelimit, +- int *msgidp +- ); +- +- int ldap_search_ext_s( +- LDAP *ld, +- char *base, +- int scope, +- char *filter, +- char **attrs, +- int attrsonly, +- LDAPControl **serverctrls, +- LDAPControl **clientctrls, +- struct timeval *timeoutp, +- int sizelimit, +- LDAPMessage **res +- ); +- +- int ldap_search( +- LDAP *ld, +- char *base, +- int scope, +- char *filter, +- char **attrs, +- int attrsonly +- ); +- +- int ldap_search_s( +- LDAP *ld, +- char *base, +- int scope, +- char *filter, +- char **attrs, +- int attrsonly, +- LDAPMessage **res +- ); +- +- int ldap_search_st( +- +- +- +-Expires: January 1998 [Page 14] +- +-C LDAP API The C LDAP Application Program Interface 29 July 1997 +- +- +- LDAP *ld, +- char *base, +- int scope, +- char *filter, +- char **attrs, +- int attrsonly, +- struct timeval *timeout, +- LDAPMessage **res +- ); +- +-Parameters are: +- +-ld The session handle. +- +-base The dn of the entry at which to start the search. +- +-scope One of LDAP_SCOPE_BASE (0x00), LDAP_SCOPE_ONELEVEL (0x01), +- or LDAP_SCOPE_SUBTREE (0x02), indicating the scope of the +- search. +- +-filter A character string as described in [3], representing the +- search filter. +- +-attrs A NULL-terminated array of strings indicating which attri- +- butes to return for each matching entry. Passing NULL for +- this parameter causes all available attributes to be +- retrieved. +- +-attrsonly A boolean value that should be zero if both attribute types +- and values are to be returned, non-zero if only types are +- wanted. +- +-timeout For the ldap_search_st() function, this specifies the local +- search timeout value. For the ldap_search_ext() and +- ldap_search_ext_s() functions, this specifies both the +- local search timeout value and the operation time limit +- that is sent to the server within the search request. +- +-res For the synchronous calls, this is a result parameter which +- will contain the results of the search upon completion of +- the call. +- +-serverctrls List of LDAP server controls. +- +-clientctrls List of client controls. +- +-msgidp This result parameter will be set to the message id of the +- request if the ldap_search_ext() call succeeds. +- +- +- +-Expires: January 1998 [Page 15] +- +-C LDAP API The C LDAP Application Program Interface 29 July 1997 +- +- +-There are three options in the session handle ld which potentially +-affect how the search is performed. They are: +- +-LDAP_OPT_SIZELIMIT +- A limit on the number of entries to return from the search. +- A value of zero means no limit. Note that the value from +- the session handle is ignored when using the +- ldap_search_ext() or ldap_search_ext_s() functions. +- +-LDAP_OPT_TIMELIMIT +- A limit on the number of seconds to spend on the search. A +- value of zero means no limit. Note that the value from the +- session handle is ignored when using the ldap_search_ext() +- or ldap_search_ext_s() functions. +- +-LDAP_OPT_DEREF +- One of LDAP_DEREF_NEVER (0x00), LDAP_DEREF_SEARCHING +- (0x01), LDAP_DEREF_FINDING (0x02), or LDAP_DEREF_ALWAYS +- (0x03), specifying how aliases should be handled during the +- search. The LDAP_DEREF_SEARCHING value means aliases should +- be dereferenced during the search but not when locating the +- base object of the search. The LDAP_DEREF_FINDING value +- means aliases should be dereferenced when locating the base +- object but not during the search. +- +-The ldap_search_ext() function initiates an asynchronous search opera- +-tion and returns the constant LDAP_SUCCESS if the request was success- +-fully sent, or another LDAP error code if not. See the section below on +-error handling for more information about possible errors and how to +-interpret them. If successful, ldap_search_ext() places the message id +-of the request in *msgidp. A subsequent call to ldap_result(), described +-below, can be used to obtain the results from the search. These results +-can be parsed using the result parsing routines described in detail +-later. +- +-Similar to ldap_search_ext(), the ldap_search() function initiates an +-asynchronous search operation and returns the message id of the opera- +-tion initiated. As for ldap_search_ext(), a subsequent call to +-ldap_result(), described below, can be used to obtain the result of the +-bind. In case of error, ldap_search() will return -1, setting the ses- +-sion error parameters in the LDAP structure appropriately. +- +-The synchronous ldap_search_ext_s(), ldap_search_s(), and +-ldap_search_st() functions all return the result of the operation, +-either the constant LDAP_SUCCESS if the operation was successful, or +-another LDAP error code if it was not. See the section below on error +-handling for more information about possible errors and how to interpret +-them. Entries returned from the search (if any) are contained in the +- +- +- +-Expires: January 1998 [Page 16] +- +-C LDAP API The C LDAP Application Program Interface 29 July 1997 +- +- +-res parameter. This parameter is opaque to the caller. Entries, attri- +-butes, values, etc., should be extracted by calling the parsing routines +-described below. The results contained in res should be freed when no +-longer in use by calling ldap_msgfree(), described later. +- +-The ldap_search_ext() and ldap_search_ext_s() functions support LDAPv3 +-server controls, client controls, and allow varying size and time limits +-to be easily specified for each search operation. The ldap_search_st() +-function is identical to ldap_search_s() except that it takes an addi- +-tional parameter specifying a local timeout for the search. +- +-7.7. Reading an Entry +- +-LDAP does not support a read operation directly. Instead, this operation +-is emulated by a search with base set to the DN of the entry to read, +-scope set to LDAP_SCOPE_BASE, and filter set to "(objectclass=*)". attrs +-contains the list of attributes to return. +- +- +-7.8. Listing the Children of an Entry +- +-LDAP does not support a list operation directly. Instead, this operation +-is emulated by a search with base set to the DN of the entry to list, +-scope set to LDAP_SCOPE_ONELEVEL, and filter set to "(objectclass=*)". +-attrs contains the list of attributes to return for each child entry. +- +-7.9. Comparing a Value Against an Entry +- +-The following routines are used to compare a given attribute value +-assertion against an LDAP entry. There are four variations: +- +- int ldap_compare_ext( +- LDAP *ld, +- char *dn, +- char *attr, +- struct berval *bvalue +- LDAPControl **serverctrls, +- LDAPControl **clientctrls, +- int *msgidp +- ); +- +- int ldap_compare_ext_s( +- LDAP *ld, +- char *dn, +- char *attr, +- struct berval *bvalue, +- LDAPControl **serverctrls, +- LDAPControl **clientctrls +- +- +- +-Expires: January 1998 [Page 17] +- +-C LDAP API The C LDAP Application Program Interface 29 July 1997 +- +- +- ); +- +- int ldap_compare( +- LDAP *ld, +- char *dn, +- char *attr, +- char *value +- ); +- +- int ldap_compare_s( +- LDAP *ld, +- char *dn, +- char *attr, +- char *value +- ); +- +-Parameters are: +- +-ld The session handle. +- +-dn The name of the entry to compare against. +- +-attr The attribute to compare against. +- +-bvalue The attribute value to compare against those found in the +- given entry. This parameter is used in the extended rou- +- tines and is a pointer to a struct berval so it is possible +- to compare binary values. +- +-value A string attribute value to compare against, used by the +- ldap_compare() and ldap_compare_s() functions. Use +- ldap_compare_ext() or ldap_compare_ext_s() if you need to +- compare binary values. +- +-serverctrls List of LDAP server controls. +- +-clientctrls List of client controls. +- +-msgidp This result parameter will be set to the message id of the +- request if the ldap_compare_ext() call succeeds. +- +-The ldap_compare_ext() function initiates an asynchronous compare opera- +-tion and returns the constant LDAP_SUCCESS if the request was success- +-fully sent, or another LDAP error code if not. See the section below on +-error handling for more information about possible errors and how to +-interpret them. If successful, ldap_compare_ext() places the message id +-of the request in *msgidp. A subsequent call to ldap_result(), described +-below, can be used to obtain the result of the compare. +- +- +- +-Expires: January 1998 [Page 18] +- +-C LDAP API The C LDAP Application Program Interface 29 July 1997 +- +- +-Similar to ldap_compare_ext(), the ldap_compare() function initiates an +-asynchronous compare operation and returns the message id of the opera- +-tion initiated. As for ldap_compare_ext(), a subsequent call to +-ldap_result(), described below, can be used to obtain the result of the +-bind. In case of error, ldap_compare() will return -1, setting the ses- +-sion error parameters in the LDAP structure appropriately. +- +-The synchronous ldap_compare_ext_s() and ldap_compare_s() functions both +-return the result of the operation, either the constant LDAP_SUCCESS if +-the operation was successful, or another LDAP error code if it was not. +-See the section below on error handling for more information about pos- +-sible errors and how to interpret them. +- +-The ldap_compare_ext() and ldap_compare_ext_s() functions support LDAPv3 +-server controls and client controls. +- +- +-7.10. Modifying an entry +- +-The following routines are used to modify an existing LDAP entry. There +-are four variations: +- +- typedef struct ldapmod { +- int mod_op; +- char *mod_type; +- union { +- char **modv_strvals; +- struct berval **modv_bvals; +- } mod_vals; +- } LDAPMod; +- #define mod_values mod_vals.modv_strvals +- #define mod_bvalues mod_vals.modv_bvals +- +- int ldap_modify_ext( +- LDAP *ld, +- char *dn, +- LDAPMod **mods, +- LDAPControl **serverctrls, +- LDAPControl **clientctrls, +- int *msgidp +- ); +- +- int ldap_modify_ext_s( +- LDAP *ld, +- char *dn, +- LDAPMod **mods, +- LDAPControl **serverctrls, +- LDAPControl **clientctrls +- +- +- +-Expires: January 1998 [Page 19] +- +-C LDAP API The C LDAP Application Program Interface 29 July 1997 +- +- +- ); +- +- int ldap_modify( +- LDAP *ld, +- char *dn, +- LDAPMod **mods +- ); +- +- int ldap_modify_s( +- LDAP *ld, +- char *dn, +- LDAPMod **mods +- ); +- +-Parameters are: +- +-ld The session handle. +- +-dn The name of the entry to modify. +- +-mods A NULL-terminated array of modifications to make to the +- entry. +- +-serverctrls List of LDAP server controls. +- +-clientctrls List of client controls. +- +-msgidp This result parameter will be set to the message id of the +- request if the ldap_modify_ext() call succeeds. +- +-The fields in the LDAPMod structure have the following meanings: +- +-mod_op The modification operation to perform. It should be one of +- LDAP_MOD_ADD (0x00), LDAP_MOD_DELETE (0x01), or +- LDAP_MOD_REPLACE (0x02). This field also indicates the +- type of values included in the mod_vals union. It is logi- +- cally ORed with LDAP_MOD_BVALUES (0x80) to select the +- mod_bvalues form. Otherwise, the mod_values form is used. +- +-mod_type The type of the attribute to modify. +- +-mod_vals The values (if any) to add, delete, or replace. Only one of +- the mod_values or mod_bvalues variants should be used, +- selected by ORing the mod_op field with the constant +- LDAP_MOD_BVALUES. mod_values is a NULL-terminated array of +- zero-terminated strings and mod_bvalues is a NULL- +- terminated array of berval structures that can be used to +- pass binary values such as images. +- +- +- +-Expires: January 1998 [Page 20] +- +-C LDAP API The C LDAP Application Program Interface 29 July 1997 +- +- +-For LDAP_MOD_ADD modifications, the given values are added to the +-entry, creating the attribute if necessary. +- +-For LDAP_MOD_DELETE modifications, the given values are deleted from the +-entry, removing the attribute if no values remain. If the entire attri- +-bute is to be deleted, the mod_vals field should be set to NULL. +- +-For LDAP_MOD_REPLACE modifications, the attribute will have the listed +-values after the modification, having been created if necessary, or +-removed if the mod_vals field is NULL. All modifications are performed +-in the order in which they are listed. +- +-The ldap_modify_ext() function initiates an asynchronous modify opera- +-tion and returns the constant LDAP_SUCCESS if the request was success- +-fully sent, or another LDAP error code if not. See the section below on +-error handling for more information about possible errors and how to +-interpret them. If successful, ldap_modify_ext() places the message id +-of the request in *msgidp. A subsequent call to ldap_result(), described +-below, can be used to obtain the result of the modify. +- +-Similar to ldap_modify_ext(), the ldap_modify() function initiates an +-asynchronous modify operation and returns the message id of the opera- +-tion initiated. As for ldap_modify_ext(), a subsequent call to +-ldap_result(), described below, can be used to obtain the result of the +-modify. In case of error, ldap_modify() will return -1, setting the ses- +-sion error parameters in the LDAP structure appropriately. +- +-The synchronous ldap_modify_ext_s() and ldap_modify_s() functions both +-return the result of the operation, either the constant LDAP_SUCCESS if +-the operation was successful, or another LDAP error code if it was not. +-See the section below on error handling for more information about pos- +-sible errors and how to interpret them. +- +-The ldap_modify_ext() and ldap_modify_ext_s() functions support LDAPv3 +-server controls and client controls. +- +- +-7.11. Modifying the Name of an Entry +- +-In LDAPv2, the ldap_modrdn() and ldap_modrdn_s() routines were used to +-change the name of an LDAP entry. They could only be used to change the +-least significant component of a name (the RDN or relative distinguished +-name). LDAPv3 provides the Modify DN protocol operation that allows more +-general name change access. The ldap_rename() and ldap_rename_s() rou- +-tines are used to change the name of an entry, and the use of the +-ldap_modrdn() and ldap_modrdn_s() routines is deprecated. +- +- int ldap_rename( +- +- +- +-Expires: January 1998 [Page 21] +- +-C LDAP API The C LDAP Application Program Interface 29 July 1997 +- +- +- LDAP *ld, +- char *dn, +- char *newrdn, +- char *newparent, +- int deleteoldrdn, +- LDAPControl **serverctrls, +- LDAPControl **clientctrls, +- int *msgidp +- +- ); +- int ldap_rename_s( +- LDAP *ld, +- char *dn, +- char *newrdn, +- char *newparent, +- int deleteoldrdn, +- LDAPControl **serverctrls, +- LDAPControl **clientctrls +- ); +- +- Use of the following routines is deprecated. +- +- int ldap_modrdn( +- LDAP *ld, +- char *dn, +- char *newrdn, +- int deleteoldrdn +- ); +- int ldap_modrdn_s( +- LDAP *ld, +- char *dn, +- char *newrdn, +- int deleteoldrdn +- ); +- +-Parameters are: +- +-ld The session handle. +- +-dn The name of the entry whose DN is to be changed. +- +-newrdn The new RDN to give the entry. +- +-newparent The new parent, or superior entry. If this parameter is +- NULL, only the RDN of the entry is changed. The root DN +- may be specified by passing a zero length string, "". The +- newparent parameter should always be NULL when using ver- +- sion 2 of the LDAP protocol; otherwise the server's +- +- +- +-Expires: January 1998 [Page 22] +- +-C LDAP API The C LDAP Application Program Interface 29 July 1997 +- +- +- behavior is undefined. +- +-deleteoldrdn This parameter only has meaning on the rename routines if +- newrdn is different than the old RDN. It is a boolean +- value, if non-zero indicating that the old RDN value(s) +- should be removed, if zero indicating that the old RDN +- value(s) should be retained as non-distinguished values of +- the entry. +- +-serverctrls List of LDAP server controls. +- +-clientctrls List of client controls. +- +-msgidp This result parameter will be set to the message id of the +- request if the ldap_rename() call succeeds. +- +-The ldap_rename() function initiates an asynchronous modify DN operation +-and returns the constant LDAP_SUCCESS if the request was successfully +-sent, or another LDAP error code if not. See the section below on error +-handling for more information about possible errors and how to interpret +-them. If successful, ldap_rename() places the DN message id of the +-request in *msgidp. A subsequent call to ldap_result(), described below, +-can be used to obtain the result of the rename. +- +-The synchronous ldap_rename_s() returns the result of the operation, +-either the constant LDAP_SUCCESS if the operation was successful, or +-another LDAP error code if it was not. See the section below on error +-handling for more information about possible errors and how to interpret +-them. +- +-The ldap_rename() and ldap_rename_s() functions both support LDAPv3 +-server controls and client controls. +- +- +-7.12. Adding an entry +- +-The following functions are used to add entries to the LDAP directory. +-There are four variations: +- +- int ldap_add_ext( +- LDAP *ld, +- char *dn, +- LDAPMod **attrs, +- LDAPControl **serverctrls, +- LDAPControl **clientctrls, +- int *msgidp +- ); +- +- +- +- +-Expires: January 1998 [Page 23] +- +-C LDAP API The C LDAP Application Program Interface 29 July 1997 +- +- +- int ldap_add_ext_s( +- LDAP *ld, +- char *dn, +- LDAPMod **attrs, +- LDAPControl **serverctrls, +- LDAPControl **clientctrls +- ); +- +- int ldap_add( +- LDAP *ld, +- char *dn, +- LDAPMod **attrs +- ); +- +- int ldap_add_s( +- LDAP *ld, +- char *dn, +- LDAPMod **attrs +- ); +- +-Parameters are: +- +-ld The session handle. +- +-dn The name of the entry to add. +- +-attrs The entry's attributes, specified using the LDAPMod struc- +- ture defined for ldap_modify(). The mod_type and mod_vals +- fields should be filled in. The mod_op field is ignored +- unless ORed with the constant LDAP_MOD_BVALUES, used to +- select the mod_bvalues case of the mod_vals union. +- +-serverctrls List of LDAP server controls. +- +-clientctrls List of client controls. +- +-msgidp This result parameter will be set to the message id of the +- request if the ldap_add_ext() call succeeds. +- +-Note that the parent of the entry being added must already exist or the +-parent must be empty (i.e., equal to the root DN) for an add to succeed. +- +-The ldap_add_ext() function initiates an asynchronous add operation and +-returns the constant LDAP_SUCCESS if the request was successfully sent, +-or another LDAP error code if not. See the section below on error han- +-dling for more information about possible errors and how to interpret +-them. If successful, ldap_add_ext() places the message id of the +-request in *msgidp. A subsequent call to ldap_result(), described below, +- +- +- +-Expires: January 1998 [Page 24] +- +-C LDAP API The C LDAP Application Program Interface 29 July 1997 +- +- +-can be used to obtain the result of the add. +- +-Similar to ldap_add_ext(), the ldap_add() function initiates an asyn- +-chronous add operation and returns the message id of the operation ini- +-tiated. As for ldap_add_ext(), a subsequent call to ldap_result(), +-described below, can be used to obtain the result of the add. In case of +-error, ldap_add() will return -1, setting the session error parameters +-in the LDAP structure appropriately. +- +-The synchronous ldap_add_ext_s() and ldap_add_s() functions both return +-the result of the operation, either the constant LDAP_SUCCESS if the +-operation was successful, or another LDAP error code if it was not. See +-the section below on error handling for more information about possible +-errors and how to interpret them. +- +-The ldap_add_ext() and ldap_add_ext_s() functions support LDAPv3 server +-controls and client controls. +- +- +- +-7.13. Deleting an entry +- +-The following functions are used to delete a leaf entry from the LDAP +-directory. There are four variations: +- +- int ldap_delete_ext( +- LDAP *ld, +- char *dn, +- LDAPControl **serverctrls, +- LDAPControl **clientctrls, +- int *msgidp +- ); +- +- int ldap_delete_ext_s( +- LDAP *ld, +- char *dn, +- LDAPControl **serverctrls, +- LDAPControl **clientctrls +- ); +- +- int ldap_delete( +- LDAP *ld, +- char *dn +- ); +- +- int ldap_delete_s( +- LDAP *ld, +- char *dn +- +- +- +-Expires: January 1998 [Page 25] +- +-C LDAP API The C LDAP Application Program Interface 29 July 1997 +- +- +- ); +- +-Parameters are: +- +-ld The session handle. +- +-dn The name of the entry to delete. +- +-serverctrls List of LDAP server controls. +- +-clientctrls List of client controls. +- +-msgidp This result parameter will be set to the message id of the +- request if the ldap_delete_ext() call succeeds. +- +-Note that the entry to delete must be a leaf entry (i.e., it must have +-no children). Deletion of entire subtrees in a single operation is not +-supported by LDAP. +- +-The ldap_delete_ext() function initiates an asynchronous delete opera- +-tion and returns the constant LDAP_SUCCESS if the request was success- +-fully sent, or another LDAP error code if not. See the section below on +-error handling for more information about possible errors and how to +-interpret them. If successful, ldap_delete_ext() places the message id +-of the request in *msgidp. A subsequent call to ldap_result(), described +-below, can be used to obtain the result of the delete. +- +-Similar to ldap_delete_ext(), the ldap_delete() function initiates an +-asynchronous delete operation and returns the message id of the opera- +-tion initiated. As for ldap_delete_ext(), a subsequent call to +-ldap_result(), described below, can be used to obtain the result of the +-delete. In case of error, ldap_delete() will return -1, setting the ses- +-sion error parameters in the LDAP structure appropriately. +- +-The synchronous ldap_delete_ext_s() and ldap_delete_s() functions both +-return the result of the operation, either the constant LDAP_SUCCESS if +-the operation was successful, or another LDAP error code if it was not. +-See the section below on error handling for more information about pos- +-sible errors and how to interpret them. +- +-The ldap_delete_ext() and ldap_delete_ext_s() functions support LDAPv3 +-server controls and client controls. +- +- +-7.14. Extended Operations +- +-The ldap_extended_operation() and ldap_extended_operation_s() routines +-allow extended LDAP operations to be passed to the server, providing a +- +- +- +-Expires: January 1998 [Page 26] +- +-C LDAP API The C LDAP Application Program Interface 29 July 1997 +- +- +-general protocol extensibility mechanism. +- +- int ldap_extended_operation( +- LDAP *ld, +- char *exoid, +- struct berval *exdata, +- LDAPControl **serverctrls, +- LDAPControl **clientctrls, +- int *msgidp +- ); +- +- int ldap_extended_operation_s( +- LDAP *ld, +- char *exoid, +- struct berval *exdata, +- LDAPControl **serverctrls, +- LDAPControl **clientctrls, +- char **retoidp, +- struct berval **retdatap +- ); +- +-Parameters are: +- +-ld The session handle. +- +-requestoid The dotted-OID text string naming the request. +- +-requestdata The arbitrary data required by the operation (if NULL, no +- data is sent to the server). +- +-serverctrls List of LDAP server controls. +- +-clientctrls List of client controls. +- +-msgidp This result parameter will be set to the message id of the +- request if the ldap_extended_operation() call succeeds. +- +-retoidp Pointer to a character string that will be set to an allo- +- cated, dotted-OID text string returned by the server. This +- string should be disposed of using the ldap_memfree() func- +- tion. If no OID was returned, *retoidp is set to NULL. +- +-retdatap Pointer to a berval structure pointer that will be set an +- allocated copy of the data returned by the server. This +- struct berval should be disposed of using ber_bvfree(). If +- no data is returned, *retdatap is set to NULL. +- +-The ldap_extended_operation() function initiates an asynchronous +- +- +- +-Expires: January 1998 [Page 27] +- +-C LDAP API The C LDAP Application Program Interface 29 July 1997 +- +- +-extended operation and returns the constant LDAP_SUCCESS if the request +-was successfully sent, or another LDAP error code if not. See the sec- +-tion below on error handling for more information about possible errors +-and how to interpret them. If successful, ldap_extended_operation() +-places the message id of the request in *msgidp. A subsequent call to +-ldap_result(), described below, can be used to obtain the result of the +-extended operation which can be passed to ldap_parse_extended_result() +-to obtain the OID and data contained in the response. +- +-The synchronous ldap_extended_operation_s() function returns the result +-of the operation, either the constant LDAP_SUCCESS if the operation was +-successful, or another LDAP error code if it was not. See the section +-below on error handling for more information about possible errors and +-how to interpret them. The retoid and retdata parameters are filled in +-with the OID and data from the response. If no OID or data was +-returned, these parameters are set to NULL. +- +-The ldap_extended_operation() and ldap_extended_operation_s() functions +-both support LDAPv3 server controls and client controls. +- +- +-8. Abandoning An Operation +- +-The following calls are used to abandon an operation in progress: +- +- int ldap_abandon_ext( +- LDAP *ld, +- int msgid, +- LDAPControl **serverctrls, +- LDAPControl **clientctrls +- ); +- +- int ldap_abandon( +- LDAP *ld, +- int msgid +- ); +- +- +-ld The session handle. +- +-msgid The message id of the request to be abandoned. +- +-serverctrls List of LDAP server controls. +- +-clientctrls List of client controls. +- +-ldap_abandon_ext() abandons the operation with message id msgid and +-returns the constant LDAP_SUCCESS if the abandon was successful or +- +- +- +-Expires: January 1998 [Page 28] +- +-C LDAP API The C LDAP Application Program Interface 29 July 1997 +- +- +-another LDAP error code if not. See the section below on error handling +-for more information about possible errors and how to interpret them. +- +-ldap_abandon() is identical to ldap_abandon_ext() except that it returns +-zero if the abandon was successful, -1 otherwise and does not support +-LDAPv3 server controls or client controls. +- +-After a successful call to ldap_abandon() or ldap_abandon_ext(), results +-with the given message id are never returned from a subsequent call to +-ldap_result(). There is no server response to LDAP abandon operations. +- +- +-9. Obtaining Results and Peeking Inside LDAP Messages +- +-ldap_result() is used to obtain the result of a previous asynchronously +-initiated operation. Note that depending on how it is called, +-ldap_result() may actually return a list or "chain" of messages. +- +-ldap_msgfree() frees the results obtained from a previous call to +-ldap_result(), or a synchronous search routine. +- +-ldap_msgtype() returns the type of an LDAP message. ldap_msgid() +-returns the message ID of an LDAP message. +- +- int ldap_result( +- LDAP *ld, +- int msgid, +- int all, +- struct timeval *timeout, +- LDAPMessage **res +- ); +- +- int ldap_msgfree( LDAPMessage *res ); +- +- int ldap_msgtype( LDAPMessage *res ); +- +- int ldap_msgid( LDAPMessage *res ); +- +-Parameters are: +- +-ld The session handle. +- +-msgid The message id of the operation whose results are to be +- returned, or the constant LDAP_RES_ANY (-1) if any result is +- desired. +- +-all Specifies how many messages will be retrieved in a single call +- to ldap_result(). This parameter only has meaning for search +- +- +- +-Expires: January 1998 [Page 29] +- +-C LDAP API The C LDAP Application Program Interface 29 July 1997 +- +- +- results. Pass the constant LDAP_MSG_ONE (0x00) to retrieve one +- message at a time. Pass LDAP_MSG_ALL (0x01) to request that +- all results of a search be received before returning all +- results in a single chain. Pass LDAP_MSG_RECEIVED (0x02) to +- indicate that all results retrieved so far should be returned +- in the result chain. +- +-timeout A timeout specifying how long to wait for results to be +- returned. A NULL value causes ldap_result() to block until +- results are available. A timeout value of zero seconds speci- +- fies a polling behavior. +- +-res For ldap_result(), a result parameter that will contain the +- result(s) of the operation. For ldap_msgfree(), the result +- chain to be freed, obtained from a previous call to +- ldap_result(), ldap_search_s(), or ldap_search_st(). +- +-Upon successful completion, ldap_result() returns the type of the first +-result returned in the res parameter. This will be one of the following +-constants. +- +- LDAP_RES_BIND (0x61) +- LDAP_RES_SEARCH_ENTRY (0x64) +- LDAP_RES_SEARCH_REFERENCE (0x73) -- new in LDAPv3 +- LDAP_RES_SEARCH_RESULT (0x65) +- LDAP_RES_MODIFY (0x67) +- LDAP_RES_ADD (0x69) +- LDAP_RES_DELETE (0x6B) +- LDAP_RES_MODDN (0x6D) +- LDAP_RES_COMPARE (0x6F) +- LDAP_RES_EXTENDED (0x78) -- new in LDAPv3 +- +-ldap_result() returns 0 if the timeout expired and -1 if an error +-occurs, in which case the error parameters of the LDAP session handle +-will be set accordingly. +- +-ldap_msgfree() frees the result structure pointed to by res and returns +-the type of the message it freed. +- +-ldap_msgtype() returns the type of the LDAP message it is passed as a +-parameter. The type will be one of the types listed above, or -1 on +-error. +- +-ldap_msgid() returns the message ID associated with the LDAP message +-passed as a parameter. +- +- +- +- +- +- +-Expires: January 1998 [Page 30] +- +-C LDAP API The C LDAP Application Program Interface 29 July 1997 +- +- +-10. Handling Errors and Parsing Results +- +-The following calls are used to extract information from results and +-handle errors returned by other LDAP API routines. +- +- int ldap_parse_result( +- LDAP *ld, +- LDAPMessage *res, +- int *errcodep, +- char **matcheddnp, +- char **errmsgp, +- char ***referralsp, +- LDAPControl ***serverctrlsp, +- int freeit +- ); +- +- int ldap_parse_sasl_bind_result( +- LDAP *ld, +- LDAPMessage *res, +- struct berval **servercredp, +- int freeit +- ); +- +- int ldap_parse_extended_result( +- LDAP *ld, +- LDAPMessage *res, +- char **resultoidp, +- struct berval **resultdata, +- int freeit +- ); +- +- char *ldap_err2string( int err ); +- +- The use of the following routines is deprecated. +- +- int ldap_result2error( +- LDAP *ld, +- LDAPMessage *res, +- int freeit +- ); +- +- void ldap_perror( LDAP *ld, char *msg ); +- +-Parameters are: +- +-ld The session handle. +- +-res The result of an LDAP operation as returned by +- +- +- +-Expires: January 1998 [Page 31] +- +-C LDAP API The C LDAP Application Program Interface 29 July 1997 +- +- +- ldap_result() or one of the synchronous API operation +- calls. +- +-errcodep This result parameter will be filled in with the LDAP error +- code field from the LDAPResult message. This is the indi- +- cation from the server of the outcome of the operation. +- NULL may be passed to ignore this field. +- +-matcheddnp In the case of a return of LDAP_NO_SUCH_OBJECT, this result +- parameter will be filled in with a DN indicating how much +- of the name in the request was recognized. NULL may be +- passed to ignore this field. The matched DN string should +- be freed by calling ldap_memfree() which is described later +- in this document. +- +-errmsgp This result parameter will be filled in with the contents +- of the error message field from the LDAPResult message. +- The error message string should be freed by calling +- ldap_memfree() which is described later in this document. +- NULL may be passed to ignore this field. +- +-referralsp This result parameter will be filled in with the contents +- of the referrals field from the LDAPResult message, indi- +- cating zero or more alternate LDAP servers where the +- request should be retried. The referrals array should be +- freed by calling ldap_value_free() which is described later +- in this document. NULL may be passed to ignore this field. +- +-serverctrlsp This result parameter will be filled in with an allocated +- array of controls copied out of the LDAPResult message. +- The control array should be freed by calling +- ldap_controls_free() which was described earlier. +- +-freeit A boolean that determines whether the res parameter is +- disposed of or not. Pass any non-zero value to have these +- routines free res after extracting the requested informa- +- tion. This is provided as a convenience; you can also use +- ldap_msgfree() to free the result later. +- +-servercredp For SASL bind results, this result parameter will be filled +- in with the credentials passed back by the server for +- mutual authentication, if given. An allocated berval struc- +- ture is returned that should be disposed of by calling +- ldap_ber_free(). NULL may be passed to ignore this field. +- +-resultoidp For extended results, this result parameter will be filled +- in with the dotted-OID text representation of the name of +- the extended operation response. This string should be +- +- +- +-Expires: January 1998 [Page 32] +- +-C LDAP API The C LDAP Application Program Interface 29 July 1997 +- +- +- disposed of by calling ldap_memfree(). NULL may be passed +- to ignore this field. +- +-resultdatap For extended results, this result parameter will be filled +- in with a pointer to a struct berval containing the data in +- the extended operation response. It should be disposed of +- by calling ldap_ber_free(). NULL may be passed to ignore +- this field. +- +-err For ldap_err2string(), an LDAP error code, as returned by +- ldap_result2error() or another LDAP API call. +- +-Additional parameters for the deprecated routines are not described. +-Interested readers are referred to RFC 1823. +- +-All of the ldap_parse_*_result() routines skip over messages of type +-LDAP_RES_SEARCH_ENTRY and LDAP_RES_SEARCH_REFERENCE when looking for a +-result message to parse. They return the constant LDAP_SUCCESS if the +-result was successfully parsed and another LDAP error code if not. Note +-that the LDAP error code that indicates the outcome of the operation +-performed by the server is placed in the errcodep ldap_parse_result() +-parameter. +- +-ldap_err2string() is used to convert a numeric LDAP error code, as +-returned by one of the ldap_parse_*_result() routines, or one of the +-synchronous API operation calls, into an informative NULL-terminated +-character string message describing the error. It returns a pointer to +-static data. +- +- +-11. Stepping Through a List of Results +- +-The ldap_first_message() and ldap_next_message() routines are used to +-step through the list of messages in a result chain returned by +-ldap_result(). For search operations, the result chain may actually +-include referral messages, entry messages, and result messages. +-ldap_count_messages() is used to count the number of messages returned. +-The ldap_msgtype() function, described above, can be used to distinguish +-between the different message types. +- +- LDAPMessage *ldap_first_message( LDAP *ld, LDAPMessage *res ); +- +- LDAPMessage *ldap_next_message( LDAP *ld, LDAPMessage *msg ); +- +- int ldap_count_messages( LDAP *ld, LDAPMessage *res ); +- +-Parameters are: +- +- +- +- +-Expires: January 1998 [Page 33] +- +-C LDAP API The C LDAP Application Program Interface 29 July 1997 +- +- +-ld The session handle. +- +-res The result chain, as obtained by a call to one of the synchronous +- search routines or ldap_result(). +- +-msg The message returned by a previous call to ldap_first_message() +- or ldap_next_message(). +- +-ldap_first_message() and ldap_next_message() will return NULL when no +-more messages exist in the result set to be returned. NULL is also +-returned if an error occurs while stepping through the entries, in which +-case the error parameters in the session handle ld will be set to indi- +-cate the error. +- +-ldap_count_messages() returns the number of messages contained in a +-chain of results. It can also be used to count the number of messages +-that remain in a chain if called with a message, entry, or reference +-returned by ldap_first_message(), ldap_next_message(), +-ldap_first_entry(), ldap_next_entry(), ldap_first_reference(), +-ldap_next_reference(). +- +- +-12. Parsing Search Results +- +-The following calls are used to parse the entries and references +-returned by ldap_search() and friends. These results are returned in an +-opaque structure that should only be accessed by calling the routines +-described below. Routines are provided to step through the entries and +-references returned, step through the attributes of an entry, retrieve +-the name of an entry, and retrieve the values associated with a given +-attribute in an entry. +- +- +-12.1. Stepping Through a List of Entries +- +-The ldap_first_entry() and ldap_next_entry() routines are used to step +-through and retrieve the list of entries from a search result chain. +-The ldap_first_reference() and ldap_next_reference() routines are used +-to step through and retrieve the list of continuation references from a +-search result chain. ldap_count_entries() is used to count the number +-of entries returned. ldap_count_references() is used to count the number +-of references returned. +- +- LDAPMessage *ldap_first_entry( LDAP *ld, LDAPMessage *res ); +- +- LDAPMessage *ldap_next_entry( LDAP *ld, LDAPMessage *entry ); +- +- LDAPMessage *ldap_first_reference( LDAP *ld, LDAPMessage *res ); +- +- +- +-Expires: January 1998 [Page 34] +- +-C LDAP API The C LDAP Application Program Interface 29 July 1997 +- +- +- LDAPMessage *ldap_next_reference( LDAP *ld, LDAPMessage *ref ); +- +- int ldap_count_entries( LDAP *ld, LDAPMessage *res ); +- +- int ldap_count_references( LDAP *ld, LDAPMessage *res ); +- +-Parameters are: +- +-ld The session handle. +- +-res The search result, as obtained by a call to one of the synchro- +- nous search routines or ldap_result(). +- +-entry The entry returned by a previous call to ldap_first_entry() or +- ldap_next_entry(). +- +-ldap_first_entry() and ldap_next_entry() will return NULL when no more +-entries or references exist in the result set to be returned. NULL is +-also returned if an error occurs while stepping through the entries, in +-which case the error parameters in the session handle ld will be set to +-indicate the error. +- +-ldap_count_entries() returns the number of entries contained in a chain +-of entries. It can also be used to count the number of entries that +-remain in a chain if called with a message, entry or reference returned +-by ldap_first_message(), ldap_next_message(), ldap_first_entry(), +-ldap_next_entry(), ldap_first_reference(), ldap_next_reference(). +- +-ldap_count_references() returns the number of references contained in a +-chain of search results. It can also be used to count the number of +-references that remain in a chain. +- +- +-12.2. Stepping Through the Attributes of an Entry +- +-The ldap_first_attribute() and ldap_next_attribute() calls are used to +-step through the list of attribute types returned with an entry. +- +- char *ldap_first_attribute( +- LDAP *ld, +- LDAPMessage *entry, +- BerElement **ptr +- ); +- +- char *ldap_next_attribute( +- LDAP *ld, +- LDAPMessage *entry, +- BerElement *ptr +- +- +- +-Expires: January 1998 [Page 35] +- +-C LDAP API The C LDAP Application Program Interface 29 July 1997 +- +- +- ); +- +- void ldap_memfree( char *mem ); +- +-Parameters are: +- +-ld The session handle. +- +-entry The entry whose attributes are to be stepped through, as returned +- by ldap_first_entry() or ldap_next_entry(). +- +-ptr In ldap_first_attribute(), the address of a pointer used inter- +- nally to keep track of the current position in the entry. In +- ldap_next_attribute(), the pointer returned by a previous call to +- ldap_first_attribute(). +- +-mem A pointer to memory allocated by the LDAP library, such as the +- attribute names returned by ldap_first_attribute() and +- ldap_next_attribute, or the DN returned by ldap_get_dn(). +- +-ldap_first_attribute() and ldap_next_attribute() will return NULL when +-the end of the attributes is reached, or if there is an error, in which +-case the error parameters in the session handle ld will be set to indi- +-cate the error. +- +-Both routines return a pointer to an allocated buffer containing the +-current attribute name. This should be freed when no longer in use by +-calling ldap_memfree(). +- +-ldap_first_attribute() will allocate and return in ptr a pointer to a +-BerElement used to keep track of the current position. This pointer +-should be passed in subsequent calls to ldap_next_attribute() to step +-through the entry's attributes. After a set of calls to +-ldap_first_attribute() and ldap_next_attibute(), if ptr is non-NULL, it +-should be freed by calling ldap_ber_free( ptr, 0 ). Note that it is very +-important to pass the second parameter as 0 (zero) in this call. +- +-The attribute names returned are suitable for passing in a call to +-ldap_get_values() and friends to retrieve the associated values. +- +- +-12.3. Retrieving the Values of an Attribute +- +-ldap_get_values() and ldap_get_values_len() are used to retrieve the +-values of a given attribute from an entry. ldap_count_values() and +-ldap_count_values_len() are used to count the returned values. +-ldap_value_free() and ldap_value_free_len() are used to free the values. +- +- +- +- +-Expires: January 1998 [Page 36] +- +-C LDAP API The C LDAP Application Program Interface 29 July 1997 +- +- +- char **ldap_get_values( +- LDAP *ld, +- LDAPMessage *entry, +- char *attr +- ); +- +- struct berval **ldap_get_values_len( +- LDAP *ld, +- LDAPMessage *entry, +- char *attr +- ); +- +- int ldap_count_values( char **vals ); +- +- int ldap_count_values_len( struct berval **vals ); +- +- int ldap_value_free( char **vals ); +- +- int ldap_value_free_len( struct berval **vals ); +- +-Parameters are: +- +-ld The session handle. +- +-entry The entry from which to retrieve values, as returned by +- ldap_first_entry() or ldap_next_entry(). +- +-attr The attribute whose values are to be retrieved, as returned by +- ldap_first_attribute() or ldap_next_attribute(), or a caller- +- supplied string (e.g., "mail"). +- +-vals The values returned by a previous call to ldap_get_values() or +- ldap_get_values_len(). +- +-Two forms of the various calls are provided. The first form is only +-suitable for use with non-binary character string data. The second _len +-form is used with any kind of data. +- +-Note that the values returned are dynamically allocated and should be +-freed by calling either ldap_value_free() or ldap_value_free_len() when +-no longer in use. +- +- +-12.4. Retrieving the name of an entry +- +-ldap_get_dn() is used to retrieve the name of an entry. +-ldap_explode_dn() and ldap_explode_rdn() are used to break up a name +-into its component parts. ldap_dn2ufn() is used to convert the name into +- +- +- +-Expires: January 1998 [Page 37] +- +-C LDAP API The C LDAP Application Program Interface 29 July 1997 +- +- +-a more "user friendly" format. +- +- char *ldap_get_dn( LDAP *ld, LDAPMessage *entry ); +- +- char **ldap_explode_dn( char *dn, int notypes ); +- +- char **ldap_explode_rdn( char *rdn, int notypes ); +- +- char *ldap_dn2ufn( char *dn ); +- +-Parameters are: +- +-ld The session handle. +- +-entry The entry whose name is to be retrieved, as returned by +- ldap_first_entry() or ldap_next_entry(). +- +-dn The dn to explode, such as returned by ldap_get_dn(). +- +-rdn The rdn to explode, such as returned in the components of the +- array returned by ldap_explode_dn(). +- +-notypes A boolean parameter, if non-zero indicating that the dn or rdn +- components should have their type information stripped off +- (i.e., "cn=Babs" would become "Babs"). +- +-ldap_get_dn() will return NULL if there is some error parsing the dn, +-setting error parameters in the session handle ld to indicate the error. +-It returns a pointer to malloc'ed space that the caller should free by +-calling ldap_memfree() when it is no longer in use. Note the format of +-the DNs returned is given by [4]. +- +-ldap_explode_dn() returns a NULL-terminated char * array containing the +-RDN components of the DN supplied, with or without types as indicated by +-the notypes parameter. The array returned should be freed when it is no +-longer in use by calling ldap_value_free(). +- +-ldap_explode_rdn() returns a NULL-terminated char * array containing the +-components of the RDN supplied, with or without types as indicated by +-the notypes parameter. The array returned should be freed when it is no +-longer in use by calling ldap_value_free(). +- +-ldap_dn2ufn() converts the DN into the user friendly format described in +-[5]. The UFN returned is malloc'ed space that should be freed by a call +-to ldap_memfree() when no longer in use. +- +- +- +- +- +- +-Expires: January 1998 [Page 38] +- +-C LDAP API The C LDAP Application Program Interface 29 July 1997 +- +- +-13. Encoded ASN.1 Value Manipulation +- +-This section describes routines which may be used to encode and decode +-BER-encoded ASN.1 values, which are often used inside of control and +-extension values. +- +-With the exceptions of two new functions ber_flatten() and ber_init(), +-these functions are compatible with the University of Michigan LDAP 3.3 +-implementation of BER. +- +- +-13.1. General +- +- struct berval { +- unsigned long bv_len; +- char *bv_val; +- }; +- +-A struct berval contains a sequence of bytes and an indication of its +-length. The bv_val is not null terminated. bv_len must always be a +-nonnegative number. Applications may allocate their own berval struc- +-tures. +- +- typedef struct berelement { +- /* opaque */ +- } BerElement; +- +-The BerElement structure contains not only a copy of the encoded value, +-but also state information used in encoding or decoding. Applications +-cannot allocate their own BerElement structures. The internal state is +-neither thread-specific nor locked, so two threads should not manipulate +-the same BerElement value simultaneously. +- +-A single BerElement value cannot be used for both encoding and decoding. +- +- void ber_bvfree ( struct berval *bv); +- +-ber_bvfree() frees a berval returned from this API. Both the bv->bv_val +-string and the berval itself are freed. Applications should not use +-ber_bvfree() with bervals which the application has allocated. +- +- void ber_bvecfree ( struct berval **bv ); +- +-ber_bvecfree() frees an array of bervals returned from this API. Each +-of the bervals in the array are freed using ber_bvfree(), then the array +-itself is freed. +- +- struct berval *ber_bvdup (struct berval *bv ); +- +- +- +-Expires: January 1998 [Page 39] +- +-C LDAP API The C LDAP Application Program Interface 29 July 1997 +- +- +-ber_bvdup() returns a copy of a berval. The bv_val field in the +-returned berval points to a different area of memory as the bv_val field +-in the argument berval. The null pointer is returned on error (e.g. out +-of memory). +- +- void ber_free ( BerElement *ber, int fbuf ); +- +-ber_free() frees a BerElement which is returned from the API calls +-ber_alloc_t() or ber_init(). Each BerElement must be freed by the +-caller. The second argument fbuf should always be set to 1. +- +- +-13.2. Encoding +- +- BerElement *ber_alloc_t(int options); +- +-ber_alloc_t() constructs and returns BerElement. The null pointer is +-returned on error. The options field contains a bitwise-or of options +-which are to be used when generating the encoding of this BerElement. +-One option is defined and must always be supplied: +- +- #define LBER_USE_DER 0x01 +- +-When this option is present, lengths will always be encoded in the +-minimum number of octets. Note that this option does not cause values +-of sets and sequences to be rearranged in tag and byte order, so these +-functions are not suitable for generating DER output as defined in X.509 +-and X.680. +- +-Unrecognized option bits are ignored. +- +-The BerElement returned by ber_alloc_t() is initially empty. Calls to +-ber_printf() will append bytes to the end of the ber_alloc_t(). +- +- int ber_printf(BerElement *ber, char *fmt, ... ) +- +-The ber_printf() routine is used to encode a BER element in much the +-same way that sprintf() works. One important difference, though, is +-that state information is kept in the ber argument so that multiple +-calls can be made to ber_printf() to append to the end of the BER ele- +-ment. ber must be a pointer to a BerElement returned by ber_alloc_t(). +-ber_printf() interprets and formats its arguments according to the for- +-mat string fmt. ber_printf() returns -1 if there is an error during +-encoding. As with sprintf(), each character in fmt refers to an argu- +-ment to ber_printf(). +- +-The format string can contain the following format characters: +- +- +- +- +-Expires: January 1998 [Page 40] +- +-C LDAP API The C LDAP Application Program Interface 29 July 1997 +- +- +-'t' Tag. The next argument is an int specifying the tag to override +- the next element to be written to the ber. This works across +- calls. The int value must contain the tag class, constructed +- bit, and tag value. The tag value must fit in a single octet +- (tag value is less than 32). For example, a tag of "[3]" for a +- constructed type is 0xA3. +- +-'b' Boolean. The next argument is an int, containing either 0 for +- FALSE or 0xff for TRUE. A boolean element is output. If this +- format character is not preceded by the 't' format modifier, the +- tag 0x01 is used for the element. +- +-'i' Integer. The next argument is an int, containing the integer in +- the host's byte order. An integer element is output. If this +- format character is not preceded by the 't' format modifier, the +- tag 0x02 is used for the element. +- +-'X' Bitstring. The next two arguments are a char * pointer to the +- start of the bitstring, followed by an int containing the number +- of bits in the bitstring. A bitstring element is output, in +- primitive form. If this format character is not preceded by the +- 't' format modifier, the tag 0x03 is used for the element. +- +-'n' Null. No argument is required. An ASN.1 NULL element is out- +- put. If this format character is not preceded by the 't' format +- modifier, the tag 0x05 is used for the element. +- +-'o' Octet string. The next two arguments are a char *, followed by +- an int with the length of the string. The string may contain +- null bytes and need not by null-terminated. An octet string +- element is output, in primitive form. If this format character +- is not preceded by the 't' format modifier, the tag 0x04 is used +- for the element. +- +-'s' Octet string. The next argument is a char * pointing to a +- null-terminated string. An octet string element in primitive +- form is output, which does not include the trailing ' ' byte. If +- this format character is not preceded by the 't' format modif- +- ier, the tag 0x04 is used for the element. +- +-'v' Several octet strings. The next argument is a char **, an array +- of char * pointers to null-terminated strings. The last element +- in the array must be a null pointer. The octet strings do not +- include the trailing SEQUENCE OF octet strings. The 't' format +- modifier cannot be used with this format character. +- +-'V' Several octet strings. A null-terminated array of berval *'s is +- supplied. Note that a construct like '{V}' is required to get an +- +- +- +-Expires: January 1998 [Page 41] +- +-C LDAP API The C LDAP Application Program Interface 29 July 1997 +- +- +- actual SEQUENCE OF octet strings. The 't' format modifier cannot +- be used with this format character. +- +-'{' Begin sequence. No argument is required. If this format char- +- acter is not preceded by the 't' format modifier, the tag 0x30 +- is used. +- +-'}' End sequence. No argument is required. The 't' format modifier +- cannot be used with this format character. +- +-'[' Begin set. No argument is required. If this format character +- is not preceded by the 't' format modifier, the tag 0x31 is +- used. +- +-']' End set. No argument is required. The 't' format modifier can- +- not be used with this format character. +- +-Each use of a '{' format character must be matched by a '}' character, +-either later in the format string, or in the format string of a subse- +-quent call to ber_printf() for that BerElement. The same applies to the +-'[' and +- +-Sequences and sets nest, and implementations of this API must maintain +-internal state to be able to properly calculate the lengths. +- +- int ber_flatten (BerElement *ber, struct berval **bvPtr); +- +-The ber_flatten routine allocates a struct berval whose contents are a +-BER encoding taken from the ber argument. The bvPtr pointer points to +-the returned berval, which must be freed using ber_bvfree(). This rou- +-tine returns 0 on success and -1 on error. +- +-The ber_flatten API call is not present in U-M LDAP 3.3. +- +-The use of ber_flatten on a BerElement in which all '{' and '}' format +-modifiers have not been properly matched can result in a berval whose +-contents are not a valid BER encoding. +- +- +-13.3. Encoding Example +- +-The following is an example of encoding the following ASN.1 data type: +- +- Example1Request ::= SEQUENCE { +- s OCTET STRING, -- must be printable +- val1 INTEGER, +- val2 [0] INTEGER DEFAULT 0 +- } +- +- +- +-Expires: January 1998 [Page 42] +- +-C LDAP API The C LDAP Application Program Interface 29 July 1997 +- +- +- int encode_example1(char *s,int val1,int val2,struct berval **bvPtr) +- { +- BerElement *ber; +- int rc; +- +- ber = ber_alloc_t(LBER_USE_DER); +- +- if (ber == NULL) return -1; +- +- if (ber_printf(ber,"{si",s,val1) == -1) { +- ber_free(ber,1); +- return -1; +- } +- +- if (val2 != 0) { +- if (ber_printf(ber,"ti",0x80,val2) == -1) { +- ber_free(ber,1); +- return -1; +- } +- } +- +- if (ber_printf(ber,"}") == -1) { +- ber_free(ber,1); +- return -1; +- } +- +- rc = ber_flatten(ber,bvPtr); +- ber_free(ber,1); +- return -1; +- } +- +- +-13.4. Decoding +- +-The following two symbols are available to applications. +- +- #define LBER_ERROR 0xffffffffL +- #define LBER_DEFAULT 0xffffffffL +- +- BerElement *ber_init (struct berval *bv); +- +-The ber_init functions construct BerElement and returns a new BerElement +-containing a copy of the data in the bv argument. ber_init returns the +-null pointer on error. +- +- unsigned long ber_scanf (BerElement *ber, char *fmt, ... ); +- +-The ber_scanf() routine is used to decode a BER element in much the same +- +- +- +-Expires: January 1998 [Page 43] +- +-C LDAP API The C LDAP Application Program Interface 29 July 1997 +- +- +-way that sscanf() works. One important difference, though, is that some +-state information is kept with the ber argument so that multiple calls +-can be made to ber_scanf() to sequentially read from the BER element. +-The ber argument must be a pointer to a BerElement returned by +-ber_init(). ber_scanf interprets the bytes according to the format +-string fmt, and stores the results in its additional arguments. +-ber_scanf() returns LBER_ERROR on error, and a nonnegative number on +-success. +- +-The format string contains conversion specifications which are used to +-direct the interpretation of the BER element. The format string can +-contain the following characters: +- +-'a' Octet string. A char ** argument should be supplied. Memory is +- allocated, filled with the contents of the octet string, null- +- terminated, and the pointer to the string is stored in the argu- +- ment. The returned value must be freed using ldap_memfree. The +- tag of the element must indicate the primitive form (constructed +- strings are not supported) but is otherwise ignored and dis- +- carded during the decoding. This format cannot be used with +- octet strings which could contain null bytes. +- +-'O' Octet string. A struct berval ** argument should be supplied, +- which upon return points to a allocated struct berval containing +- the octet string and its length. ber_bvfree() must be called to +- free the allocated memory. The tag of the element must indicate +- the primitive form (constructed strings are not supported) but +- is otherwise ignored during the decoding. +- +-'b' Boolean. A pointer to an int should be supplied. The int value +- stored will be 0 for FALSE or nonzero for TRUE. The tag of the +- element must indicate the primitive form but is otherwise +- ignored during the decoding. +- +-'i' Integer. A pointer to an int should be supplied. The int value +- stored will be in host byte order. The tag of the element must +- indicate the primitive form but is otherwise ignored during the +- decoding. ber_scanf() will return an error if the integer can- +- not be stored in an int. +- +-'B' Bitstring. A char ** argument should be supplied which will +- point to the allocated bits, followed by an unsigned long * +- argument, which will point to the length (in bits) of the bit- +- string returned. ldap_memfree must be called to free the bit- +- string. The tag of the element must indicate the primitive form +- (constructed bitstrings are not supported) but is otherwise +- ignored during the decoding. +- +- +- +- +-Expires: January 1998 [Page 44] +- +-C LDAP API The C LDAP Application Program Interface 29 July 1997 +- +- +-'n' Null. No argument is required. The element is simply skipped +- if it is recognized as a zero-length element. The tag is +- ignored. +- +-'v' Several octet strings. A char *** argument should be supplied, +- which upon return points to a allocated null-terminated array of +- char *'s containing the octet strings. NULL is stored if the +- sequence is empty. ldap_memfree must be called to free each +- element of the array and the array itself. The tag of the +- sequence and of the octet strings are ignored. +- +-'V' Several octet strings (which could contain null bytes). A +- struct berval *** should be supplied, which upon return points +- to a allocated null-terminated array of struct berval *'s con- +- taining the octet strings and their lengths. NULL is stored if +- the sequence is empty. ber_bvecfree() can be called to free the +- allocated memory. The tag of the sequence and of the octet +- strings are ignored. +- +-'x' Skip element. The next element is skipped. No argument is +- required. +- +-'{' Begin sequence. No argument is required. The initial sequence +- tag and length are skipped. +- +-'}' End sequence. No argument is required. +- +-'[' Begin set. No argument is required. The initial set tag and +- length are skipped. +- +-']' End set. No argument is required. +- +- unsigned long ber_peek_tag (BerElement *ber, unsigned long *lenPtr); +- +-ber_peek_tag() returns the tag of the next element to be parsed in the +-BerElement argument. The length of this element is stored in the +-*lenPtr argument. LBER_DEFAULT is returned if there is no further data +-to be read. The ber argument is not modified. +- +- unsigned long ber_skip_tag (BerElement *ber, unsigned long *lenPtr); +- +-ber_skip_tag() is similar to ber_peek_tag(), except that the state +-pointer in the BerElement argument is advanced past the first tag and +-length, and is pointed to the value part of the next element. This rou- +-tine should only be used with constructed types and situations when a +-BER encoding is used as the value of an OCTET STRING. The length of the +-value is stored in *lenPtr. +- +- +- +- +-Expires: January 1998 [Page 45] +- +-C LDAP API The C LDAP Application Program Interface 29 July 1997 +- +- +- unsigned long ber_first_element(BerElement *ber, +- unsigned long *lenPtr, char **opaquePtr); +- +- unsigned long ber_next_element (BerElement *ber, +- unsigned long *lenPtr, char *opaque); +- +-ber_first_element() and ber_next_element() are used to traverse a SET, +-SET OF, SEQUENCE or SEQUENCE OF data value. ber_first_element() calls +-ber_skip_tag(), stores internal information in *lenPtr and *opaquePtr, +-and calls ber_peek_tag() for the first element inside the constructed +-value. LBER_DEFAULT is returned if the constructed value is empty. +-ber_next_element() positions the state at the start of the next element +-in the constructed type. LBER_DEFAULT is returned if there are no +-further values. +- +-The len and opaque values should not be used by applications other than +-as arguments to ber_next_element(), as shown in the example below. +- +- +-13.5. Decoding Example +- +-The following is an example of decoding an ASN.1 data type: +- +- Example2Request ::= SEQUENCE { +- dn OCTET STRING, -- must be printable +- scope ENUMERATED { b (0), s (1), w (2) }, +- ali ENUMERATED { n (0), s (1), f (2), a (3) }, +- size INTEGER, +- time INTEGER, +- tonly BOOLEAN, +- attrs SEQUENCE OF OCTET STRING, -- must be printable +- [0] SEQUENCE OF SEQUENCE { +- type OCTET STRING -- must be printable, +- crit BOOLEAN DEFAULT FALSE, +- value OCTET STRING +- } OPTIONAL } +- +- #define LDAP_TAG_CONTROL_LIST 0xA0L /* context specific cons 0 */ +- +- int decode_example2(struct berval *bv) +- { +- BerElement *ber; +- unsigned long len; +- int scope, ali, size, time, tonly; +- char *dn = NULL, **attrs = NULL; +- int res,i,rc = 0; +- +- ber = ber_init(bv); +- +- +- +-Expires: January 1998 [Page 46] +- +-C LDAP API The C LDAP Application Program Interface 29 July 1997 +- +- +- if (ber == NULL) { +- printf("ERROR ber_init failed0); +- return -1; +- } +- +- res = ber_scanf(ber,"{aiiiiib{v}",&dn,&scope,&ali, +- &size,&time,&tonly,&attrs); +- +- if (res == -1) { +- printf("ERROR ber_scanf failed0); +- ber_free(ber,1); +- return -1; +- } +- +- /* *** use dn */ +- ldap_memfree(dn); +- +- for (i = 0; attrs != NULL && attrs[i] != NULL; i++) { +- /* *** use attrs[i] */ +- ldap_memfree(attrs[i]); +- } +- ldap_memfree(attrs); +- +- if (ber_peek_tag(ber,&len) == LDAP_TAG_CONTROL_LIST) { +- char *opaque; +- unsigned long tag; +- +- for (tag = ber_first_element(ber,&len,&opaque); +- tag != LBER_DEFAULT; +- tag = ber_next_element (ber,&len,opaque)) { +- +- unsigned long ttag, tlen; +- char *type; +- int crit; +- struct berval *value; +- +- if (ber_scanf(ber,"{a",&type) == LBER_ERROR) { +- printf("ERROR cannot parse type0); +- break; +- } +- /* *** use type */ +- ldap_memfree(type); +- +- ttag = ber_peek_tag(ber,&tlen); +- if (ttag == 0x01) { /* boolean */ +- if (ber_scanf(ber,"b", +- &crit) == LBER_ERROR) { +- printf("ERROR cannot parse crit0); +- +- +- +-Expires: January 1998 [Page 47] +- +-C LDAP API The C LDAP Application Program Interface 29 July 1997 +- +- +- rc = -1; +- break; +- } +- } else if (ttag == 0x04) { /* octet string */ +- crit = 0; +- } else { +- printf("ERROR extra field in controls0); +- break; +- } +- +- if (ber_scanf(ber,"O}",&value) == LBER_ERROR) { +- printf("ERROR cannot parse value0); +- rc = -1; +- break; +- } +- /* *** use value */ +- ldap_bvfree(value); +- } +- } +- +- ber_scanf(ber,"}"); +- +- ber_free(ber,1); +- +- return rc; +- } +- +- +- +-14. Security Considerations +- +-LDAPv2 supports security through protocol-level authentication using +-clear-text passwords. LDAPv3 adds support for SASL [8] (Simple Authen- +-tication Security Layer) methods. LDAPv3 also supports operation over a +-secure transport layer using Transport Layer Security TLS [8]. Readers +-are referred to the protocol documents for discussion of related secu- +-rity considerations. +- +-Implementations of this API should be cautious when handling authentica- +-tion credentials. In particular, keeping long-lived copies of creden- +-tials without the application's knowledge is discouraged. +- +- +-15. Acknowledgements +- +-Many members of the IETF ASID working group as well as members of the +-Internet at large have provided useful comments and suggestions that +-have been incorporated into this revision. +- +- +- +-Expires: January 1998 [Page 48] +- +-C LDAP API The C LDAP Application Program Interface 29 July 1997 +- +- +-This original material upon which this revision is based was based upon +-work supported by the National Science Foundation under Grant No. NCR- +-9416667. +- +- +-16. Bibliography +- +-[1] The Directory: Selected Attribute Syntaxes. CCITT, Recommendation +- X.520. +- +-[2] M. Wahl, A. Coulbeck, T. Howes, S. Kille, W. Yeong, C. Robbins, +- "Lightweight Directory Access Protocol Attribute Syntax Defini- +- tions", INTERNET-DRAFT , +- 11 July 1997. +- +-[3] T. Howes, "A String Representation of LDAP Search Filters," +- INTERNET-DRAFT , May 1997. +- +-[4] S. Kille, M. Wahl, "A UTF-8 String Representation of Distinguished +- Names", INTERNET-DRAFT , 29 April +- 1997. +- +-[5] S. Kille, "Using the OSI Directory to Achieve User Friendly Nam- +- ing," RFC 1781, March 1995. +- +-[6] M. Wahl, T. Howes, S. Kille, "Lightweight Directory Access Protocol +- (v3)", INTERNET-DRAFT , 11 +- July 1997. +- +-[7] A. Herron, T. Howes, M. Wahl, "LDAP Control Extension for Server +- Side Sorting of Search Result," INTERNET-DRAFT , 16 April 1997. +- +-[8] J. Meyers, "Simple Authentication and Security Layer", INTERNET- +- DRAFT , April 1997. +- +-[9] "Lightweight Directory Access Protocol (v3) Extension for Transport +- Layer Security", INTERNET-DRAFT , June 1997. +- +-[10] "UTF-8, a transformation format of Unicode and ISO 10646", RFC +- 2044, October 1996. +- +-[11] "IP Version 6 Addressing Architecture,", RFC 1884, December 1995. +- +- +- +- +- +- +- +-Expires: January 1998 [Page 49] +- +-C LDAP API The C LDAP Application Program Interface 29 July 1997 +- +- +-17. Author's Addresses +- +- Tim Howes +- Netscape Communications Corp. +- 501 E. Middlefield Rd., Mailstop MV068 +- Mountain View, CA 94043 +- USA +- +1 415 937-3419 +- howes@netscape.com +- +- +- Mark Smith +- Netscape Communications Corp. +- 501 E. Middlefield Rd., Mailstop MV068 +- Mountain View, CA 94043 +- USA +- +1 415 937-3477 +- mcs@netscape.com +- +- Andy Herron +- Microsoft Corp. +- 1 Microsoft Way +- Redmond, WA 98052 +- USA +- +1 425 882-8080 +- andyhe@microsoft.com +- +- Chris Weider +- Microsoft Corp. +- 1 Microsoft Way +- Redmond, WA 98052 +- USA +- +1 425 882-8080 +- cweider@microsoft.com +- +- Mark Wahl +- Critical Angle Inc. +- 4815 W Braker Lane #502-385 +- Austin, TX 78759 +- USA +- M.Wahl@critical-angle.com +- +- +-18. Appendix A - Sample LDAP API Code +- +- #include +- +- main() +- +- +- +-Expires: January 1998 [Page 50] +- +-C LDAP API The C LDAP Application Program Interface 29 July 1997 +- +- +- { +- LDAP *ld; +- LDAPMessage *res, *e; +- int i; +- char *a, *dn; +- BerElement *ptr; +- char **vals; +- +- /* open an LDAP session */ +- if ( (ld = ldap_init( "dotted.host.name", LDAP_PORT )) == NULL ) +- exit( 1 ); +- +- /* authenticate as nobody */ +- if ( ldap_simple_bind_s( ld, NULL, NULL ) != LDAP_SUCCESS ) { +- ldap_perror( ld, "ldap_simple_bind_s" ); +- exit( 1 ); +- } +- +- /* search for entries with cn of "Babs Jensen", return all attrs */ +- if ( ldap_search_s( ld, "o=University of Michigan, c=US", +- LDAP_SCOPE_SUBTREE, "(cn=Babs Jensen)", NULL, 0, &res ) +- != LDAP_SUCCESS ) { +- ldap_perror( ld, "ldap_search_s" ); +- exit( 1 ); +- } +- +- /* step through each entry returned */ +- for ( e = ldap_first_entry( ld, res ); e != NULL; +- e = ldap_next_entry( ld, e ) ) { +- /* print its name */ +- dn = ldap_get_dn( ld, e ); +- printf( "dn: %s\n", dn ); +- ldap_memfree( dn ); +- +- /* print each attribute */ +- for ( a = ldap_first_attribute( ld, e, &ptr ); a != NULL; +- a = ldap_next_attribute( ld, e, ptr ) ) { +- printf( "attribute: %s\n", a ); +- +- /* print each value */ +- vals = ldap_get_values( ld, e, a ); +- for ( i = 0; vals[i] != NULL; i++ ) { +- printf( "value: %s\n", vals[i] ); +- } +- ldap_value_free( vals ); +- } +- if ( ptr != NULL ) { +- ldap_ber_free( ptr, 0 ); +- +- +- +-Expires: January 1998 [Page 51] +- +-C LDAP API The C LDAP Application Program Interface 29 July 1997 +- +- +- } +- } +- /* free the search results */ +- ldap_msgfree( res ); +- +- /* close and free connection resources */ +- ldap_unbind( ld ); +- } +- +- +- +-19. Appendix B - Outstanding Issues +- +- +-19.1. Support for multithreaded applications +- +-In order to support multithreaded applications in a platform-independent +-way, some additions to the LDAP API are needed. Different implementors +-have taken different paths to solve this problem in the past. A common +-set of thread-related API calls must be defined so that application +-developers are not unduly burdened. These will be added to a future +-revision of this specification. +- +- +-19.2. Using Transport Layer Security (TLS) +- +-The API calls used to support TLS must be specified. They will be added +-to a future revision of this specification. +- +- +-19.3. Client control for chasing referrals +- +-A client control has been defined that can be used to specify on a per- +-operation basis whether references and external referrals are automati- +-cally chased by the client library. This will be added to a future +-revision of this specification. +- +- +-19.4. Potential confusion between hostname:port and IPv6 addresses +- +-String representations of IPv6 network addresses [11] can contain colon +-characters. The ldap_init() call is specified to take strings of the +-form "hostname:port" or "ipaddress:port". If IPv6 addresses are used, +-the latter could be ambiguous. A future revision of this specification +-will resolve this issue. +- +- +- +- +- +- +-Expires: January 1998 [Page 52] +- +-C LDAP API The C LDAP Application Program Interface 29 July 1997 +- +- +-19.5. Need to track SASL API standardization efforts +- +-If a standard Simple Authentication and Security Layer API is defined, +-it may be necessary to modify the LDAP API to accommodate it. +- +- +-19.6. Support for character sets other than UTF-8? +- +-Some application developers would prefer to pass string data using a +-character set other than UTF-8. This could be accommodated by adding a +-new option to ldap_set_option() that supports choosing a character set. +-If this feature is added, the number of different character sets sup- +-ported should definitely be minimized. +- +- +-19.7. Use of UTF-8 with LDAPv2 servers +- +-Strings are always passed as UTF-8 in this API but LDAP version 2 +-servers do not support the full range of UTF-8 characters. The expected +-behavior of this API when using LDAP version 2 with unsupported charac- +-ters should be specified. +- +- +- +- +- +- +- +- +- +- +- +- +- +- +- +- +- +- +- +- +- +- +- +- +- +- +- +- +- +- +-Expires: January 1998 [Page 53] +- +- +- +-1. Status of this Memo............................................1 +-2. Introduction...................................................1 +-3. Overview of the LDAP Model.....................................2 +-4. Overview of LDAP API Use.......................................3 +-5. Common Data Structures.........................................4 +-6. LDAP Error Codes...............................................5 +-7. Performing LDAP Operations.....................................6 +-7.1. Initializing an LDAP Session................................6 +-7.2. LDAP Session Handle Options.................................7 +-7.3. Working with controls.......................................10 +-7.4. Authenticating to the directory.............................11 +-7.5. Closing the session.........................................13 +-7.6. Searching...................................................13 +-7.7. Reading an Entry............................................17 +-7.8. Listing the Children of an Entry............................17 +-7.9. Comparing a Value Against an Entry..........................17 +-7.10. Modifying an entry..........................................19 +-7.11. Modifying the Name of an Entry..............................21 +-7.12. Adding an entry.............................................23 +-7.13. Deleting an entry...........................................25 +-7.14. Extended Operations.........................................26 +-8. Abandoning An Operation........................................28 +-9. Obtaining Results and Peeking Inside LDAP Messages.............29 +-10. Handling Errors and Parsing Results............................31 +-11. Stepping Through a List of Results.............................33 +-12. Parsing Search Results.........................................34 +-12.1. Stepping Through a List of Entries..........................34 +-12.2. Stepping Through the Attributes of an Entry.................35 +-12.3. Retrieving the Values of an Attribute.......................36 +-12.4. Retrieving the name of an entry.............................37 +-13. Encoded ASN.1 Value Manipulation...............................39 +-13.1. General.....................................................39 +-13.2. Encoding....................................................40 +-13.3. Encoding Example............................................42 +-13.4. Decoding....................................................43 +-13.5. Decoding Example............................................46 +-14. Security Considerations........................................48 +-15. Acknowledgements...............................................48 +-16. Bibliography...................................................49 +-17. Author's Addresses.............................................50 +-18. Appendix A - Sample LDAP API Code..............................50 +-19. Appendix B - Outstanding Issues................................52 +-19.1. Support for multithreaded applications......................52 +-19.2. Using Transport Layer Security (TLS)........................52 +-19.3. Client control for chasing referrals........................52 +-19.4. Potential confusion between hostname:port and IPv6 addresses52 +-19.5. Need to track SASL API standardization efforts..............53 +-19.6. Support for character sets other than UTF-8?................53 +-19.7. Use of UTF-8 with LDAPv2 servers............................53 +- +- +- +- +- +- +- +- diff --git a/rabbitmq-server/plugins-src/generate_app b/rabbitmq-server/plugins-src/generate_app new file mode 100644 index 0000000..fb0eb1e --- /dev/null +++ b/rabbitmq-server/plugins-src/generate_app @@ -0,0 +1,16 @@ +#!/usr/bin/env escript +%% -*- erlang -*- + +main([InFile, OutFile | SrcDirs]) -> + Modules = [list_to_atom(filename:basename(F, ".erl")) || + SrcDir <- SrcDirs, + F <- filelib:wildcard("*.erl", SrcDir)], + {ok, [{application, Application, Properties}]} = file:consult(InFile), + NewProperties = + case proplists:get_value(modules, Properties) of + [] -> lists:keyreplace(modules, 1, Properties, {modules, Modules}); + _ -> Properties + end, + file:write_file( + OutFile, + io_lib:format("~p.~n", [{application, Application, NewProperties}])). diff --git a/rabbitmq-server/plugins-src/generate_deps b/rabbitmq-server/plugins-src/generate_deps new file mode 100644 index 0000000..9f8485b --- /dev/null +++ b/rabbitmq-server/plugins-src/generate_deps @@ -0,0 +1,61 @@ +#!/usr/bin/env escript +%% -*- erlang -*- +-mode(compile). + +%% We expect the list of Erlang source and header files to arrive on +%% stdin, with the entries colon-separated. +main([TargetFile, EbinDir]) -> + ErlsAndHrls = [ string:strip(S,left) || + S <- string:tokens(io:get_line(""), ":\n")], + ErlFiles = [F || F <- ErlsAndHrls, lists:suffix(".erl", F)], + Modules = sets:from_list( + [list_to_atom(filename:basename(FileName, ".erl")) || + FileName <- ErlFiles]), + HrlFiles = [F || F <- ErlsAndHrls, lists:suffix(".hrl", F)], + IncludeDirs = lists:usort([filename:dirname(Path) || Path <- HrlFiles]), + Headers = sets:from_list(HrlFiles), + Deps = lists:foldl( + fun (Path, Deps1) -> + dict:store(Path, detect_deps(IncludeDirs, EbinDir, + Modules, Headers, Path), + Deps1) + end, dict:new(), ErlFiles), + {ok, Hdl} = file:open(TargetFile, [write, delayed_write]), + dict:fold( + fun (_Path, [], ok) -> + ok; + (Path, Dep, ok) -> + Module = filename:basename(Path, ".erl"), + ok = file:write(Hdl, [EbinDir, "/", Module, ".beam: ", + Path]), + ok = sets:fold(fun (E, ok) -> file:write(Hdl, [" ", E]) end, + ok, Dep), + file:write(Hdl, ["\n"]) + end, ok, Deps), + ok = file:write(Hdl, [TargetFile, ": ", escript:script_name(), "\n"]), + ok = file:sync(Hdl), + ok = file:close(Hdl). + +detect_deps(IncludeDirs, EbinDir, Modules, Headers, Path) -> + {ok, Forms} = epp:parse_file(Path, IncludeDirs, [{use_specs, true}]), + lists:foldl( + fun ({attribute, _Line, Attribute, Behaviour}, Deps) + when Attribute =:= behaviour orelse Attribute =:= behavior -> + maybe_add_to_deps(EbinDir, Modules, Behaviour, Deps); + ({attribute, _Line, compile, {parse_transform, Transform}}, Deps) -> + maybe_add_to_deps(EbinDir, Modules, Transform, Deps); + ({attribute, _Line, file, {FileName, _LineNumber1}}, Deps) -> + case sets:is_element(FileName, Headers) of + true -> sets:add_element(FileName, Deps); + false -> Deps + end; + (_Form, Deps) -> + Deps + end, sets:new(), Forms). + +maybe_add_to_deps(EbinDir, Modules, Module, Deps) -> + case sets:is_element(Module, Modules) of + true -> sets:add_element( + [EbinDir, "/", atom_to_list(Module), ".beam"], Deps); + false -> Deps + end. diff --git a/rabbitmq-server/plugins-src/licensing/LICENSE-APACHE2-ExplorerCanvas b/rabbitmq-server/plugins-src/licensing/LICENSE-APACHE2-ExplorerCanvas new file mode 100644 index 0000000..d645695 --- /dev/null +++ b/rabbitmq-server/plugins-src/licensing/LICENSE-APACHE2-ExplorerCanvas @@ -0,0 +1,202 @@ + + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright [yyyy] [name of copyright owner] + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. diff --git a/rabbitmq-server/plugins-src/licensing/LICENSE-APL2-Stomp-Websocket b/rabbitmq-server/plugins-src/licensing/LICENSE-APL2-Stomp-Websocket new file mode 100644 index 0000000..ef51da2 --- /dev/null +++ b/rabbitmq-server/plugins-src/licensing/LICENSE-APL2-Stomp-Websocket @@ -0,0 +1,202 @@ + + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + +TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + +1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + +2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + +3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + +4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + +5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + +6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + +7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + +8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + +9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + +END OF TERMS AND CONDITIONS + +APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + +Copyright [yyyy] [name of copyright owner] + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. diff --git a/rabbitmq-server/plugins-src/licensing/LICENSE-Apache-Basho b/rabbitmq-server/plugins-src/licensing/LICENSE-Apache-Basho new file mode 100644 index 0000000..e454a52 --- /dev/null +++ b/rabbitmq-server/plugins-src/licensing/LICENSE-Apache-Basho @@ -0,0 +1,178 @@ + + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + diff --git a/rabbitmq-server/plugins-src/licensing/LICENSE-BSD-base64js b/rabbitmq-server/plugins-src/licensing/LICENSE-BSD-base64js new file mode 100644 index 0000000..7073704 --- /dev/null +++ b/rabbitmq-server/plugins-src/licensing/LICENSE-BSD-base64js @@ -0,0 +1,25 @@ +/* + * Copyright (c) 2010 Nick Galbreath + * http://code.google.com/p/stringencoders/source/browse/#svn/trunk/javascript + * + * Permission is hereby granted, free of charge, to any person + * obtaining a copy of this software and associated documentation + * files (the "Software"), to deal in the Software without + * restriction, including without limitation the rights to use, + * copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following + * conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES + * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT + * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, + * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR + * OTHER DEALINGS IN THE SOFTWARE. +*/ diff --git a/rabbitmq-server/plugins-src/licensing/LICENSE-BSD-glMatrix b/rabbitmq-server/plugins-src/licensing/LICENSE-BSD-glMatrix new file mode 100644 index 0000000..660571e --- /dev/null +++ b/rabbitmq-server/plugins-src/licensing/LICENSE-BSD-glMatrix @@ -0,0 +1,26 @@ +Copyright (c) 2011, Brandon Jones +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are +met: + +1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + +2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the + distribution. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/rabbitmq-server/plugins-src/licensing/LICENSE-MIT-EJS10 b/rabbitmq-server/plugins-src/licensing/LICENSE-MIT-EJS10 new file mode 100644 index 0000000..f3bdcd8 --- /dev/null +++ b/rabbitmq-server/plugins-src/licensing/LICENSE-MIT-EJS10 @@ -0,0 +1,23 @@ +EJS - Embedded JavaScript + +Copyright (c) 2007 Edward Benson + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. + + diff --git a/rabbitmq-server/plugins-src/licensing/LICENSE-MIT-Flot b/rabbitmq-server/plugins-src/licensing/LICENSE-MIT-Flot new file mode 100644 index 0000000..67f4625 --- /dev/null +++ b/rabbitmq-server/plugins-src/licensing/LICENSE-MIT-Flot @@ -0,0 +1,22 @@ +Copyright (c) 2007-2013 IOLA and Ole Laursen + +Permission is hereby granted, free of charge, to any person +obtaining a copy of this software and associated documentation +files (the "Software"), to deal in the Software without +restriction, including without limitation the rights to use, +copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the +Software is furnished to do so, subject to the following +conditions: + +The above copyright notice and this permission notice shall be +included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES +OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT +HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, +WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +OTHER DEALINGS IN THE SOFTWARE. diff --git a/rabbitmq-server/plugins-src/licensing/LICENSE-MIT-Mochi b/rabbitmq-server/plugins-src/licensing/LICENSE-MIT-Mochi new file mode 100644 index 0000000..c85b65a --- /dev/null +++ b/rabbitmq-server/plugins-src/licensing/LICENSE-MIT-Mochi @@ -0,0 +1,9 @@ +This is the MIT license. + +Copyright (c) 2007 Mochi Media, Inc. + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/rabbitmq-server/plugins-src/licensing/LICENSE-MIT-Sammy060 b/rabbitmq-server/plugins-src/licensing/LICENSE-MIT-Sammy060 new file mode 100644 index 0000000..3debf5a --- /dev/null +++ b/rabbitmq-server/plugins-src/licensing/LICENSE-MIT-Sammy060 @@ -0,0 +1,25 @@ +Copyright (c) 2008 Aaron Quint, Quirkey NYC, LLC + +Permission is hereby granted, free of charge, to any person +obtaining a copy of this software and associated documentation +files (the "Software"), to deal in the Software without +restriction, including without limitation the rights to use, +copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the +Software is furnished to do so, subject to the following +conditions: + +The above copyright notice and this permission notice shall be +included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES +OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT +HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, +WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +OTHER DEALINGS IN THE SOFTWARE. + + + diff --git a/rabbitmq-server/plugins-src/licensing/LICENSE-MIT-eldap b/rabbitmq-server/plugins-src/licensing/LICENSE-MIT-eldap new file mode 100644 index 0000000..1f62009 --- /dev/null +++ b/rabbitmq-server/plugins-src/licensing/LICENSE-MIT-eldap @@ -0,0 +1,21 @@ + +Copyright (c) 2010, Torbjorn Tornkvist + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. + diff --git a/rabbitmq-server/plugins-src/licensing/LICENSE-MIT-jQuery164 b/rabbitmq-server/plugins-src/licensing/LICENSE-MIT-jQuery164 new file mode 100644 index 0000000..5a87162 --- /dev/null +++ b/rabbitmq-server/plugins-src/licensing/LICENSE-MIT-jQuery164 @@ -0,0 +1,21 @@ +Copyright (c) 2011 John Resig, http://jquery.com/ + +Permission is hereby granted, free of charge, to any person obtaining +a copy of this software and associated documentation files (the +"Software"), to deal in the Software without restriction, including +without limitation the rights to use, copy, modify, merge, publish, +distribute, sublicense, and/or sell copies of the Software, and to +permit persons to whom the Software is furnished to do so, subject to +the following conditions: + +The above copyright notice and this permission notice shall be +included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + diff --git a/rabbitmq-server/plugins-src/licensing/LICENSE-MPL-RabbitMQ b/rabbitmq-server/plugins-src/licensing/LICENSE-MPL-RabbitMQ new file mode 100644 index 0000000..c87c1a3 --- /dev/null +++ b/rabbitmq-server/plugins-src/licensing/LICENSE-MPL-RabbitMQ @@ -0,0 +1,455 @@ + MOZILLA PUBLIC LICENSE + Version 1.1 + + --------------- + +1. Definitions. + + 1.0.1. "Commercial Use" means distribution or otherwise making the + Covered Code available to a third party. + + 1.1. "Contributor" means each entity that creates or contributes to + the creation of Modifications. + + 1.2. "Contributor Version" means the combination of the Original + Code, prior Modifications used by a Contributor, and the Modifications + made by that particular Contributor. + + 1.3. "Covered Code" means the Original Code or Modifications or the + combination of the Original Code and Modifications, in each case + including portions thereof. + + 1.4. "Electronic Distribution Mechanism" means a mechanism generally + accepted in the software development community for the electronic + transfer of data. + + 1.5. "Executable" means Covered Code in any form other than Source + Code. + + 1.6. "Initial Developer" means the individual or entity identified + as the Initial Developer in the Source Code notice required by Exhibit + A. + + 1.7. "Larger Work" means a work which combines Covered Code or + portions thereof with code not governed by the terms of this License. + + 1.8. "License" means this document. + + 1.8.1. "Licensable" means having the right to grant, to the maximum + extent possible, whether at the time of the initial grant or + subsequently acquired, any and all of the rights conveyed herein. + + 1.9. "Modifications" means any addition to or deletion from the + substance or structure of either the Original Code or any previous + Modifications. When Covered Code is released as a series of files, a + Modification is: + A. Any addition to or deletion from the contents of a file + containing Original Code or previous Modifications. + + B. Any new file that contains any part of the Original Code or + previous Modifications. + + 1.10. "Original Code" means Source Code of computer software code + which is described in the Source Code notice required by Exhibit A as + Original Code, and which, at the time of its release under this + License is not already Covered Code governed by this License. + + 1.10.1. "Patent Claims" means any patent claim(s), now owned or + hereafter acquired, including without limitation, method, process, + and apparatus claims, in any patent Licensable by grantor. + + 1.11. "Source Code" means the preferred form of the Covered Code for + making modifications to it, including all modules it contains, plus + any associated interface definition files, scripts used to control + compilation and installation of an Executable, or source code + differential comparisons against either the Original Code or another + well known, available Covered Code of the Contributor's choice. The + Source Code can be in a compressed or archival form, provided the + appropriate decompression or de-archiving software is widely available + for no charge. + + 1.12. "You" (or "Your") means an individual or a legal entity + exercising rights under, and complying with all of the terms of, this + License or a future version of this License issued under Section 6.1. + For legal entities, "You" includes any entity which controls, is + controlled by, or is under common control with You. For purposes of + this definition, "control" means (a) the power, direct or indirect, + to cause the direction or management of such entity, whether by + contract or otherwise, or (b) ownership of more than fifty percent + (50%) of the outstanding shares or beneficial ownership of such + entity. + +2. Source Code License. + + 2.1. The Initial Developer Grant. + The Initial Developer hereby grants You a world-wide, royalty-free, + non-exclusive license, subject to third party intellectual property + claims: + (a) under intellectual property rights (other than patent or + trademark) Licensable by Initial Developer to use, reproduce, + modify, display, perform, sublicense and distribute the Original + Code (or portions thereof) with or without Modifications, and/or + as part of a Larger Work; and + + (b) under Patents Claims infringed by the making, using or + selling of Original Code, to make, have made, use, practice, + sell, and offer for sale, and/or otherwise dispose of the + Original Code (or portions thereof). + + (c) the licenses granted in this Section 2.1(a) and (b) are + effective on the date Initial Developer first distributes + Original Code under the terms of this License. + + (d) Notwithstanding Section 2.1(b) above, no patent license is + granted: 1) for code that You delete from the Original Code; 2) + separate from the Original Code; or 3) for infringements caused + by: i) the modification of the Original Code or ii) the + combination of the Original Code with other software or devices. + + 2.2. Contributor Grant. + Subject to third party intellectual property claims, each Contributor + hereby grants You a world-wide, royalty-free, non-exclusive license + + (a) under intellectual property rights (other than patent or + trademark) Licensable by Contributor, to use, reproduce, modify, + display, perform, sublicense and distribute the Modifications + created by such Contributor (or portions thereof) either on an + unmodified basis, with other Modifications, as Covered Code + and/or as part of a Larger Work; and + + (b) under Patent Claims infringed by the making, using, or + selling of Modifications made by that Contributor either alone + and/or in combination with its Contributor Version (or portions + of such combination), to make, use, sell, offer for sale, have + made, and/or otherwise dispose of: 1) Modifications made by that + Contributor (or portions thereof); and 2) the combination of + Modifications made by that Contributor with its Contributor + Version (or portions of such combination). + + (c) the licenses granted in Sections 2.2(a) and 2.2(b) are + effective on the date Contributor first makes Commercial Use of + the Covered Code. + + (d) Notwithstanding Section 2.2(b) above, no patent license is + granted: 1) for any code that Contributor has deleted from the + Contributor Version; 2) separate from the Contributor Version; + 3) for infringements caused by: i) third party modifications of + Contributor Version or ii) the combination of Modifications made + by that Contributor with other software (except as part of the + Contributor Version) or other devices; or 4) under Patent Claims + infringed by Covered Code in the absence of Modifications made by + that Contributor. + +3. Distribution Obligations. + + 3.1. Application of License. + The Modifications which You create or to which You contribute are + governed by the terms of this License, including without limitation + Section 2.2. The Source Code version of Covered Code may be + distributed only under the terms of this License or a future version + of this License released under Section 6.1, and You must include a + copy of this License with every copy of the Source Code You + distribute. You may not offer or impose any terms on any Source Code + version that alters or restricts the applicable version of this + License or the recipients' rights hereunder. However, You may include + an additional document offering the additional rights described in + Section 3.5. + + 3.2. Availability of Source Code. + Any Modification which You create or to which You contribute must be + made available in Source Code form under the terms of this License + either on the same media as an Executable version or via an accepted + Electronic Distribution Mechanism to anyone to whom you made an + Executable version available; and if made available via Electronic + Distribution Mechanism, must remain available for at least twelve (12) + months after the date it initially became available, or at least six + (6) months after a subsequent version of that particular Modification + has been made available to such recipients. You are responsible for + ensuring that the Source Code version remains available even if the + Electronic Distribution Mechanism is maintained by a third party. + + 3.3. Description of Modifications. + You must cause all Covered Code to which You contribute to contain a + file documenting the changes You made to create that Covered Code and + the date of any change. You must include a prominent statement that + the Modification is derived, directly or indirectly, from Original + Code provided by the Initial Developer and including the name of the + Initial Developer in (a) the Source Code, and (b) in any notice in an + Executable version or related documentation in which You describe the + origin or ownership of the Covered Code. + + 3.4. Intellectual Property Matters + (a) Third Party Claims. + If Contributor has knowledge that a license under a third party's + intellectual property rights is required to exercise the rights + granted by such Contributor under Sections 2.1 or 2.2, + Contributor must include a text file with the Source Code + distribution titled "LEGAL" which describes the claim and the + party making the claim in sufficient detail that a recipient will + know whom to contact. If Contributor obtains such knowledge after + the Modification is made available as described in Section 3.2, + Contributor shall promptly modify the LEGAL file in all copies + Contributor makes available thereafter and shall take other steps + (such as notifying appropriate mailing lists or newsgroups) + reasonably calculated to inform those who received the Covered + Code that new knowledge has been obtained. + + (b) Contributor APIs. + If Contributor's Modifications include an application programming + interface and Contributor has knowledge of patent licenses which + are reasonably necessary to implement that API, Contributor must + also include this information in the LEGAL file. + + (c) Representations. + Contributor represents that, except as disclosed pursuant to + Section 3.4(a) above, Contributor believes that Contributor's + Modifications are Contributor's original creation(s) and/or + Contributor has sufficient rights to grant the rights conveyed by + this License. + + 3.5. Required Notices. + You must duplicate the notice in Exhibit A in each file of the Source + Code. If it is not possible to put such notice in a particular Source + Code file due to its structure, then You must include such notice in a + location (such as a relevant directory) where a user would be likely + to look for such a notice. If You created one or more Modification(s) + You may add your name as a Contributor to the notice described in + Exhibit A. You must also duplicate this License in any documentation + for the Source Code where You describe recipients' rights or ownership + rights relating to Covered Code. You may choose to offer, and to + charge a fee for, warranty, support, indemnity or liability + obligations to one or more recipients of Covered Code. However, You + may do so only on Your own behalf, and not on behalf of the Initial + Developer or any Contributor. You must make it absolutely clear than + any such warranty, support, indemnity or liability obligation is + offered by You alone, and You hereby agree to indemnify the Initial + Developer and every Contributor for any liability incurred by the + Initial Developer or such Contributor as a result of warranty, + support, indemnity or liability terms You offer. + + 3.6. Distribution of Executable Versions. + You may distribute Covered Code in Executable form only if the + requirements of Section 3.1-3.5 have been met for that Covered Code, + and if You include a notice stating that the Source Code version of + the Covered Code is available under the terms of this License, + including a description of how and where You have fulfilled the + obligations of Section 3.2. The notice must be conspicuously included + in any notice in an Executable version, related documentation or + collateral in which You describe recipients' rights relating to the + Covered Code. You may distribute the Executable version of Covered + Code or ownership rights under a license of Your choice, which may + contain terms different from this License, provided that You are in + compliance with the terms of this License and that the license for the + Executable version does not attempt to limit or alter the recipient's + rights in the Source Code version from the rights set forth in this + License. If You distribute the Executable version under a different + license You must make it absolutely clear that any terms which differ + from this License are offered by You alone, not by the Initial + Developer or any Contributor. You hereby agree to indemnify the + Initial Developer and every Contributor for any liability incurred by + the Initial Developer or such Contributor as a result of any such + terms You offer. + + 3.7. Larger Works. + You may create a Larger Work by combining Covered Code with other code + not governed by the terms of this License and distribute the Larger + Work as a single product. In such a case, You must make sure the + requirements of this License are fulfilled for the Covered Code. + +4. Inability to Comply Due to Statute or Regulation. + + If it is impossible for You to comply with any of the terms of this + License with respect to some or all of the Covered Code due to + statute, judicial order, or regulation then You must: (a) comply with + the terms of this License to the maximum extent possible; and (b) + describe the limitations and the code they affect. Such description + must be included in the LEGAL file described in Section 3.4 and must + be included with all distributions of the Source Code. Except to the + extent prohibited by statute or regulation, such description must be + sufficiently detailed for a recipient of ordinary skill to be able to + understand it. + +5. Application of this License. + + This License applies to code to which the Initial Developer has + attached the notice in Exhibit A and to related Covered Code. + +6. Versions of the License. + + 6.1. New Versions. + Netscape Communications Corporation ("Netscape") may publish revised + and/or new versions of the License from time to time. Each version + will be given a distinguishing version number. + + 6.2. Effect of New Versions. + Once Covered Code has been published under a particular version of the + License, You may always continue to use it under the terms of that + version. You may also choose to use such Covered Code under the terms + of any subsequent version of the License published by Netscape. No one + other than Netscape has the right to modify the terms applicable to + Covered Code created under this License. + + 6.3. Derivative Works. + If You create or use a modified version of this License (which you may + only do in order to apply it to code which is not already Covered Code + governed by this License), You must (a) rename Your license so that + the phrases "Mozilla", "MOZILLAPL", "MOZPL", "Netscape", + "MPL", "NPL" or any confusingly similar phrase do not appear in your + license (except to note that your license differs from this License) + and (b) otherwise make it clear that Your version of the license + contains terms which differ from the Mozilla Public License and + Netscape Public License. (Filling in the name of the Initial + Developer, Original Code or Contributor in the notice described in + Exhibit A shall not of themselves be deemed to be modifications of + this License.) + +7. DISCLAIMER OF WARRANTY. + + COVERED CODE IS PROVIDED UNDER THIS LICENSE ON AN "AS IS" BASIS, + WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, + WITHOUT LIMITATION, WARRANTIES THAT THE COVERED CODE IS FREE OF + DEFECTS, MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE OR NON-INFRINGING. + THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE COVERED CODE + IS WITH YOU. SHOULD ANY COVERED CODE PROVE DEFECTIVE IN ANY RESPECT, + YOU (NOT THE INITIAL DEVELOPER OR ANY OTHER CONTRIBUTOR) ASSUME THE + COST OF ANY NECESSARY SERVICING, REPAIR OR CORRECTION. THIS DISCLAIMER + OF WARRANTY CONSTITUTES AN ESSENTIAL PART OF THIS LICENSE. NO USE OF + ANY COVERED CODE IS AUTHORIZED HEREUNDER EXCEPT UNDER THIS DISCLAIMER. + +8. TERMINATION. + + 8.1. This License and the rights granted hereunder will terminate + automatically if You fail to comply with terms herein and fail to cure + such breach within 30 days of becoming aware of the breach. All + sublicenses to the Covered Code which are properly granted shall + survive any termination of this License. Provisions which, by their + nature, must remain in effect beyond the termination of this License + shall survive. + + 8.2. If You initiate litigation by asserting a patent infringement + claim (excluding declatory judgment actions) against Initial Developer + or a Contributor (the Initial Developer or Contributor against whom + You file such action is referred to as "Participant") alleging that: + + (a) such Participant's Contributor Version directly or indirectly + infringes any patent, then any and all rights granted by such + Participant to You under Sections 2.1 and/or 2.2 of this License + shall, upon 60 days notice from Participant terminate prospectively, + unless if within 60 days after receipt of notice You either: (i) + agree in writing to pay Participant a mutually agreeable reasonable + royalty for Your past and future use of Modifications made by such + Participant, or (ii) withdraw Your litigation claim with respect to + the Contributor Version against such Participant. If within 60 days + of notice, a reasonable royalty and payment arrangement are not + mutually agreed upon in writing by the parties or the litigation claim + is not withdrawn, the rights granted by Participant to You under + Sections 2.1 and/or 2.2 automatically terminate at the expiration of + the 60 day notice period specified above. + + (b) any software, hardware, or device, other than such Participant's + Contributor Version, directly or indirectly infringes any patent, then + any rights granted to You by such Participant under Sections 2.1(b) + and 2.2(b) are revoked effective as of the date You first made, used, + sold, distributed, or had made, Modifications made by that + Participant. + + 8.3. If You assert a patent infringement claim against Participant + alleging that such Participant's Contributor Version directly or + indirectly infringes any patent where such claim is resolved (such as + by license or settlement) prior to the initiation of patent + infringement litigation, then the reasonable value of the licenses + granted by such Participant under Sections 2.1 or 2.2 shall be taken + into account in determining the amount or value of any payment or + license. + + 8.4. In the event of termination under Sections 8.1 or 8.2 above, + all end user license agreements (excluding distributors and resellers) + which have been validly granted by You or any distributor hereunder + prior to termination shall survive termination. + +9. LIMITATION OF LIABILITY. + + UNDER NO CIRCUMSTANCES AND UNDER NO LEGAL THEORY, WHETHER TORT + (INCLUDING NEGLIGENCE), CONTRACT, OR OTHERWISE, SHALL YOU, THE INITIAL + DEVELOPER, ANY OTHER CONTRIBUTOR, OR ANY DISTRIBUTOR OF COVERED CODE, + OR ANY SUPPLIER OF ANY OF SUCH PARTIES, BE LIABLE TO ANY PERSON FOR + ANY INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES OF ANY + CHARACTER INCLUDING, WITHOUT LIMITATION, DAMAGES FOR LOSS OF GOODWILL, + WORK STOPPAGE, COMPUTER FAILURE OR MALFUNCTION, OR ANY AND ALL OTHER + COMMERCIAL DAMAGES OR LOSSES, EVEN IF SUCH PARTY SHALL HAVE BEEN + INFORMED OF THE POSSIBILITY OF SUCH DAMAGES. THIS LIMITATION OF + LIABILITY SHALL NOT APPLY TO LIABILITY FOR DEATH OR PERSONAL INJURY + RESULTING FROM SUCH PARTY'S NEGLIGENCE TO THE EXTENT APPLICABLE LAW + PROHIBITS SUCH LIMITATION. SOME JURISDICTIONS DO NOT ALLOW THE + EXCLUSION OR LIMITATION OF INCIDENTAL OR CONSEQUENTIAL DAMAGES, SO + THIS EXCLUSION AND LIMITATION MAY NOT APPLY TO YOU. + +10. U.S. GOVERNMENT END USERS. + + The Covered Code is a "commercial item," as that term is defined in + 48 C.F.R. 2.101 (Oct. 1995), consisting of "commercial computer + software" and "commercial computer software documentation," as such + terms are used in 48 C.F.R. 12.212 (Sept. 1995). Consistent with 48 + C.F.R. 12.212 and 48 C.F.R. 227.7202-1 through 227.7202-4 (June 1995), + all U.S. Government End Users acquire Covered Code with only those + rights set forth herein. + +11. MISCELLANEOUS. + + This License represents the complete agreement concerning subject + matter hereof. If any provision of this License is held to be + unenforceable, such provision shall be reformed only to the extent + necessary to make it enforceable. This License shall be governed by + California law provisions (except to the extent applicable law, if + any, provides otherwise), excluding its conflict-of-law provisions. + With respect to disputes in which at least one party is a citizen of, + or an entity chartered or registered to do business in the United + States of America, any litigation relating to this License shall be + subject to the jurisdiction of the Federal Courts of the Northern + District of California, with venue lying in Santa Clara County, + California, with the losing party responsible for costs, including + without limitation, court costs and reasonable attorneys' fees and + expenses. The application of the United Nations Convention on + Contracts for the International Sale of Goods is expressly excluded. + Any law or regulation which provides that the language of a contract + shall be construed against the drafter shall not apply to this + License. + +12. RESPONSIBILITY FOR CLAIMS. + + As between Initial Developer and the Contributors, each party is + responsible for claims and damages arising, directly or indirectly, + out of its utilization of rights under this License and You agree to + work with Initial Developer and Contributors to distribute such + responsibility on an equitable basis. Nothing herein is intended or + shall be deemed to constitute any admission of liability. + +13. MULTIPLE-LICENSED CODE. + + Initial Developer may designate portions of the Covered Code as + "Multiple-Licensed". "Multiple-Licensed" means that the Initial + Developer permits you to utilize portions of the Covered Code under + Your choice of the NPL or the alternative licenses, if any, specified + by the Initial Developer in the file described in Exhibit A. + +EXHIBIT A -Mozilla Public License. + + ``The contents of this file are subject to the Mozilla Public License + Version 1.1 (the "License"); you may not use this file except in + compliance with the License. You may obtain a copy of the License at + http://www.mozilla.org/MPL/ + + Software distributed under the License is distributed on an "AS IS" + basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the + License for the specific language governing rights and limitations + under the License. + + The Original Code is RabbitMQ. + + The Initial Developer of the Original Code is GoPivotal, Inc. + Copyright (c) 2007-2014 GoPivotal, Inc. All rights reserved.'' + + [NOTE: The text of this Exhibit A may differ slightly from the text of + the notices in the Source Code files of the Original Code. You should + use the text of this Exhibit A rather than the text found in the + Original Code Source Code for Your Modifications.] diff --git a/rabbitmq-server/plugins-src/licensing/license_info_eldap-wrapper b/rabbitmq-server/plugins-src/licensing/license_info_eldap-wrapper new file mode 100644 index 0000000..0a0e13c --- /dev/null +++ b/rabbitmq-server/plugins-src/licensing/license_info_eldap-wrapper @@ -0,0 +1,3 @@ +Eldap is "Copyright (c) 2010, Torbjorn Tornkvist" and is covered by +the MIT license. It was downloaded from https://github.com/etnt/eldap + diff --git a/rabbitmq-server/plugins-src/licensing/license_info_mochiweb-wrapper b/rabbitmq-server/plugins-src/licensing/license_info_mochiweb-wrapper new file mode 100644 index 0000000..c72a6af --- /dev/null +++ b/rabbitmq-server/plugins-src/licensing/license_info_mochiweb-wrapper @@ -0,0 +1,4 @@ +Mochiweb is "Copyright (c) 2007 Mochi Media, Inc." and is covered by +the MIT license. It was downloaded from +http://github.com/mochi/mochiweb/ + diff --git a/rabbitmq-server/plugins-src/licensing/license_info_rabbitmq-management b/rabbitmq-server/plugins-src/licensing/license_info_rabbitmq-management new file mode 100644 index 0000000..eabe119 --- /dev/null +++ b/rabbitmq-server/plugins-src/licensing/license_info_rabbitmq-management @@ -0,0 +1,17 @@ +jQuery is "Copyright (c) 2010 John Resig" and is covered by the MIT +license. It was downloaded from http://jquery.com/ + +EJS is "Copyright (c) 2007 Edward Benson" and is covered by the MIT +license. It was downloaded from http://embeddedjs.com/ + +Sammy is "Copyright (c) 2008 Aaron Quint, Quirkey NYC, LLC" and is +covered by the MIT license. It was downloaded from +http://code.quirkey.com/sammy/ + +ExplorerCanvas is "Copyright 2006 Google Inc" and is covered by the +Apache License version 2.0. It was downloaded from +http://code.google.com/p/explorercanvas/ + +Flot is "Copyright (c) 2007-2013 IOLA and Ole Laursen" and is covered +by the MIT license. It was downloaded from +http://www.flotcharts.org/ diff --git a/rabbitmq-server/plugins-src/licensing/license_info_rabbitmq-management-visualiser b/rabbitmq-server/plugins-src/licensing/license_info_rabbitmq-management-visualiser new file mode 100644 index 0000000..9a8a906 --- /dev/null +++ b/rabbitmq-server/plugins-src/licensing/license_info_rabbitmq-management-visualiser @@ -0,0 +1,4 @@ +glMatrix is "Copyright (c) 2011, Brandon Jones" and is covered by the +BSD 2-Clause license. It was downloaded from +http://code.google.com/p/glmatrix/ + diff --git a/rabbitmq-server/plugins-src/licensing/license_info_webmachine-wrapper b/rabbitmq-server/plugins-src/licensing/license_info_webmachine-wrapper new file mode 100644 index 0000000..c00fb92 --- /dev/null +++ b/rabbitmq-server/plugins-src/licensing/license_info_webmachine-wrapper @@ -0,0 +1,3 @@ +Webmachine is Copyright (c) Basho Technologies and is covered by the +Apache License 2.0. It was downloaded from http://webmachine.basho.com/ + diff --git a/rabbitmq-server/plugins-src/mochiweb-wrapper/.srcdist_done b/rabbitmq-server/plugins-src/mochiweb-wrapper/.srcdist_done new file mode 100644 index 0000000..e69de29 diff --git a/rabbitmq-server/plugins-src/mochiweb-wrapper/10-build-on-R12B-5.patch b/rabbitmq-server/plugins-src/mochiweb-wrapper/10-build-on-R12B-5.patch new file mode 100644 index 0000000..af582a7 --- /dev/null +++ b/rabbitmq-server/plugins-src/mochiweb-wrapper/10-build-on-R12B-5.patch @@ -0,0 +1,303 @@ +diff --git a/src/mochiglobal.erl b/src/mochiglobal.erl +index ea645b0..6b20e41 100644 +--- a/src/mochiglobal.erl ++++ b/src/mochiglobal.erl +@@ -6,12 +6,12 @@ + -author("Bob Ippolito "). + -export([get/1, get/2, put/2, delete/1]). + +--spec get(atom()) -> any() | undefined. ++%% -spec get(atom()) -> any() | undefined. + %% @equiv get(K, undefined) + get(K) -> + get(K, undefined). + +--spec get(atom(), T) -> any() | T. ++%% -spec get(atom(), T) -> any() | T. + %% @doc Get the term for K or return Default. + get(K, Default) -> + get(K, Default, key_to_module(K)). +@@ -22,7 +22,7 @@ get(_K, Default, Mod) -> + Default + end. + +--spec put(atom(), any()) -> ok. ++%% -spec put(atom(), any()) -> ok. + %% @doc Store term V at K, replaces an existing term if present. + put(K, V) -> + put(K, V, key_to_module(K)). +@@ -33,7 +33,7 @@ put(_K, V, Mod) -> + {module, Mod} = code:load_binary(Mod, atom_to_list(Mod) ++ ".erl", Bin), + ok. + +--spec delete(atom()) -> boolean(). ++%% -spec delete(atom()) -> boolean(). + %% @doc Delete term stored at K, no-op if non-existent. + delete(K) -> + delete(K, key_to_module(K)). +@@ -42,21 +42,21 @@ delete(_K, Mod) -> + code:purge(Mod), + code:delete(Mod). + +--spec key_to_module(atom()) -> atom(). ++%% -spec key_to_module(atom()) -> atom(). + key_to_module(K) -> + list_to_atom("mochiglobal:" ++ atom_to_list(K)). + +--spec compile(atom(), any()) -> binary(). ++%% -spec compile(atom(), any()) -> binary(). + compile(Module, T) -> + {ok, Module, Bin} = compile:forms(forms(Module, T), + [verbose, report_errors]), + Bin. + +--spec forms(atom(), any()) -> [erl_syntax:syntaxTree()]. ++%% -spec forms(atom(), any()) -> [erl_syntax:syntaxTree()]. + forms(Module, T) -> + [erl_syntax:revert(X) || X <- term_to_abstract(Module, term, T)]. + +--spec term_to_abstract(atom(), atom(), any()) -> [erl_syntax:syntaxTree()]. ++%% -spec term_to_abstract(atom(), atom(), any()) -> [erl_syntax:syntaxTree()]. + term_to_abstract(Module, Getter, T) -> + [%% -module(Module). + erl_syntax:attribute( +diff --git a/src/mochiutf8.erl b/src/mochiutf8.erl +index 28f28c1..c9d2751 100644 +--- a/src/mochiutf8.erl ++++ b/src/mochiutf8.erl +@@ -11,11 +11,11 @@ + + %% External API + +--type unichar_low() :: 0..16#d7ff. +--type unichar_high() :: 16#e000..16#10ffff. +--type unichar() :: unichar_low() | unichar_high(). ++%% -type unichar_low() :: 0..16#d7ff. ++%% -type unichar_high() :: 16#e000..16#10ffff. ++%% -type unichar() :: unichar_low() | unichar_high(). + +--spec codepoint_to_bytes(unichar()) -> binary(). ++%% -spec codepoint_to_bytes(unichar()) -> binary(). + %% @doc Convert a unicode codepoint to UTF-8 bytes. + codepoint_to_bytes(C) when (C >= 16#00 andalso C =< 16#7f) -> + %% U+0000 - U+007F - 7 bits +@@ -40,12 +40,12 @@ codepoint_to_bytes(C) when (C >= 16#010000 andalso C =< 16#10FFFF) -> + 2#10:2, B1:6, + 2#10:2, B0:6>>. + +--spec codepoints_to_bytes([unichar()]) -> binary(). ++%% -spec codepoints_to_bytes([unichar()]) -> binary(). + %% @doc Convert a list of codepoints to a UTF-8 binary. + codepoints_to_bytes(L) -> + <<<<(codepoint_to_bytes(C))/binary>> || C <- L>>. + +--spec read_codepoint(binary()) -> {unichar(), binary(), binary()}. ++%% -spec read_codepoint(binary()) -> {unichar(), binary(), binary()}. + read_codepoint(Bin = <<2#0:1, C:7, Rest/binary>>) -> + %% U+0000 - U+007F - 7 bits + <> = Bin, +@@ -82,32 +82,32 @@ read_codepoint(Bin = <<2#11110:5, B3:3, + {C, B, Rest} + end. + +--spec codepoint_foldl(fun((unichar(), _) -> _), _, binary()) -> _. ++%% -spec codepoint_foldl(fun((unichar(), _) -> _), _, binary()) -> _. + codepoint_foldl(F, Acc, <<>>) when is_function(F, 2) -> + Acc; + codepoint_foldl(F, Acc, Bin) -> + {C, _, Rest} = read_codepoint(Bin), + codepoint_foldl(F, F(C, Acc), Rest). + +--spec bytes_foldl(fun((binary(), _) -> _), _, binary()) -> _. ++%% -spec bytes_foldl(fun((binary(), _) -> _), _, binary()) -> _. + bytes_foldl(F, Acc, <<>>) when is_function(F, 2) -> + Acc; + bytes_foldl(F, Acc, Bin) -> + {_, B, Rest} = read_codepoint(Bin), + bytes_foldl(F, F(B, Acc), Rest). + +--spec bytes_to_codepoints(binary()) -> [unichar()]. ++%% -spec bytes_to_codepoints(binary()) -> [unichar()]. + bytes_to_codepoints(B) -> + lists:reverse(codepoint_foldl(fun (C, Acc) -> [C | Acc] end, [], B)). + +--spec len(binary()) -> non_neg_integer(). ++%% -spec len(binary()) -> non_neg_integer(). + len(<<>>) -> + 0; + len(B) -> + {_, _, Rest} = read_codepoint(B), + 1 + len(Rest). + +--spec valid_utf8_bytes(B::binary()) -> binary(). ++%% -spec valid_utf8_bytes(B::binary()) -> binary(). + %% @doc Return only the bytes in B that represent valid UTF-8. Uses + %% the following recursive algorithm: skip one byte if B does not + %% follow UTF-8 syntax (a 1-4 byte encoding of some number), +@@ -118,7 +118,7 @@ valid_utf8_bytes(B) when is_binary(B) -> + + %% Internal API + +--spec binary_skip_bytes(binary(), [non_neg_integer()]) -> binary(). ++%% -spec binary_skip_bytes(binary(), [non_neg_integer()]) -> binary(). + %% @doc Return B, but skipping the 0-based indexes in L. + binary_skip_bytes(B, []) -> + B; +@@ -126,7 +126,7 @@ binary_skip_bytes(B, L) -> + binary_skip_bytes(B, L, 0, []). + + %% @private +--spec binary_skip_bytes(binary(), [non_neg_integer()], non_neg_integer(), iolist()) -> binary(). ++%% -spec binary_skip_bytes(binary(), [non_neg_integer()], non_neg_integer(), iolist()) -> binary(). + binary_skip_bytes(B, [], _N, Acc) -> + iolist_to_binary(lists:reverse([B | Acc])); + binary_skip_bytes(<<_, RestB/binary>>, [N | RestL], N, Acc) -> +@@ -134,13 +134,13 @@ binary_skip_bytes(<<_, RestB/binary>>, [N | RestL], N, Acc) -> + binary_skip_bytes(<>, L, N, Acc) -> + binary_skip_bytes(RestB, L, 1 + N, [C | Acc]). + +--spec invalid_utf8_indexes(binary()) -> [non_neg_integer()]. ++%% -spec invalid_utf8_indexes(binary()) -> [non_neg_integer()]. + %% @doc Return the 0-based indexes in B that are not valid UTF-8. + invalid_utf8_indexes(B) -> + invalid_utf8_indexes(B, 0, []). + + %% @private. +--spec invalid_utf8_indexes(binary(), non_neg_integer(), [non_neg_integer()]) -> [non_neg_integer()]. ++%% -spec invalid_utf8_indexes(binary(), non_neg_integer(), [non_neg_integer()]) -> [non_neg_integer()]. + invalid_utf8_indexes(<>, N, Acc) when C < 16#80 -> + %% U+0000 - U+007F - 7 bits + invalid_utf8_indexes(Rest, 1 + N, Acc); +diff --git a/src/mochiweb_charref.erl b/src/mochiweb_charref.erl +index 193c7c7..665d0f9 100644 +--- a/src/mochiweb_charref.erl ++++ b/src/mochiweb_charref.erl +@@ -11,7 +11,7 @@ + %% codepoint, or return undefined on failure. + %% The input should not include an ampersand or semicolon. + %% charref("#38") = 38, charref("#x26") = 38, charref("amp") = 38. +--spec charref(binary() | string()) -> integer() | [integer()] | undefined. ++%% -spec charref(binary() | string()) -> integer() | [integer()] | undefined. + charref(B) when is_binary(B) -> + charref(binary_to_list(B)); + charref([$#, C | L]) when C =:= $x orelse C =:= $X -> +diff --git a/src/mochiweb_http.erl b/src/mochiweb_http.erl +index 931ecd0..ae6410f 100644 +--- a/src/mochiweb_http.erl ++++ b/src/mochiweb_http.erl +@@ -121,12 +121,12 @@ call_body({M, F}, Req) -> + call_body(Body, Req) -> + Body(Req). + +--spec handle_invalid_request(term()) -> no_return(). ++%% -spec handle_invalid_request(term()) -> no_return(). + handle_invalid_request(Socket) -> + handle_invalid_request(Socket, {'GET', {abs_path, "/"}, {0,9}}, []), + exit(normal). + +--spec handle_invalid_request(term(), term(), term()) -> no_return(). ++%% -spec handle_invalid_request(term(), term(), term()) -> no_return(). + handle_invalid_request(Socket, Request, RevHeaders) -> + Req = new_request(Socket, Request, RevHeaders), + Req:respond({400, [], []}), +diff --git a/src/mochiweb_session.erl b/src/mochiweb_session.erl +index ac5d66b..ddf7c46 100644 +--- a/src/mochiweb_session.erl ++++ b/src/mochiweb_session.erl +@@ -21,11 +21,11 @@ + + %% @doc Generates a secure encrypted binary convining all the parameters. The + %% expiration time must be a 32-bit integer. +--spec generate_session_data( +- ExpirationTime :: expiration_time(), +- Data :: iolist(), +- FSessionKey :: key_fun(), +- ServerKey :: iolist()) -> binary(). ++%% -spec generate_session_data( ++%% ExpirationTime :: expiration_time(), ++%% Data :: iolist(), ++%% FSessionKey :: key_fun(), ++%% ServerKey :: iolist()) -> binary(). + generate_session_data(ExpirationTime, Data, FSessionKey, ServerKey) + when is_integer(ExpirationTime), is_function(FSessionKey)-> + BData = ensure_binary(Data), +@@ -39,11 +39,11 @@ generate_session_data(ExpirationTime, Data, FSessionKey, ServerKey) + %% @doc Convenience wrapper for generate_session_data that returns a + %% mochiweb cookie with "id" as the key, a max_age of 20000 seconds, + %% and the current local time as local time. +--spec generate_session_cookie( +- ExpirationTime :: expiration_time(), +- Data :: iolist(), +- FSessionKey :: key_fun(), +- ServerKey :: iolist()) -> header(). ++%% -spec generate_session_cookie( ++%% ExpirationTime :: expiration_time(), ++%% Data :: iolist(), ++%% FSessionKey :: key_fun(), ++%% ServerKey :: iolist()) -> header(). + generate_session_cookie(ExpirationTime, Data, FSessionKey, ServerKey) + when is_integer(ExpirationTime), is_function(FSessionKey)-> + CookieData = generate_session_data(ExpirationTime, Data, +@@ -55,13 +55,13 @@ generate_session_cookie(ExpirationTime, Data, FSessionKey, ServerKey) + calendar:universal_time())}]). + + %% TODO: This return type is messy to express in the type system. +--spec check_session_cookie( +- ECookie :: binary(), +- ExpirationTime :: string(), +- FSessionKey :: key_fun(), +- ServerKey :: iolist()) -> +- {Success :: boolean(), +- ExpTimeAndData :: [integer() | binary()]}. ++%% -spec check_session_cookie( ++ %% ECookie :: binary(), ++ %% ExpirationTime :: string(), ++ %% FSessionKey :: key_fun(), ++ %% ServerKey :: iolist()) -> ++ %% {Success :: boolean(), ++ %% ExpTimeAndData :: [integer() | binary()]}. + check_session_cookie(ECookie, ExpirationTime, FSessionKey, ServerKey) + when is_binary(ECookie), is_integer(ExpirationTime), + is_function(FSessionKey) -> +@@ -83,7 +83,7 @@ check_session_cookie(_ECookie, _ExpirationTime, _FSessionKey, _ServerKey) -> + {false, []}. + + %% 'Constant' time =:= operator for binary, to mitigate timing attacks. +--spec eq(binary(), binary()) -> boolean(). ++%% -spec eq(binary(), binary()) -> boolean(). + eq(A, B) when is_binary(A) andalso is_binary(B) -> + eq(A, B, 0). + +@@ -94,27 +94,27 @@ eq(<<>>, <<>>, 0) -> + eq(_As, _Bs, _Acc) -> + false. + +--spec ensure_binary(iolist()) -> binary(). ++%% -spec ensure_binary(iolist()) -> binary(). + ensure_binary(B) when is_binary(B) -> + B; + ensure_binary(L) when is_list(L) -> + iolist_to_binary(L). + +--spec encrypt_data(binary(), binary()) -> binary(). ++%% -spec encrypt_data(binary(), binary()) -> binary(). + encrypt_data(Data, Key) -> + IV = crypto:rand_bytes(16), + Crypt = crypto:aes_cfb_128_encrypt(Key, IV, Data), + <>. + +--spec decrypt_data(binary(), binary()) -> binary(). ++%% -spec decrypt_data(binary(), binary()) -> binary(). + decrypt_data(<>, Key) -> + crypto:aes_cfb_128_decrypt(Key, IV, Crypt). + +--spec gen_key(iolist(), iolist()) -> binary(). ++%% -spec gen_key(iolist(), iolist()) -> binary(). + gen_key(ExpirationTime, ServerKey)-> + crypto:md5_mac(ServerKey, [ExpirationTime]). + +--spec gen_hmac(iolist(), binary(), iolist(), binary()) -> binary(). ++%% -spec gen_hmac(iolist(), binary(), iolist(), binary()) -> binary(). + gen_hmac(ExpirationTime, Data, SessionKey, Key) -> + crypto:sha_mac(Key, [ExpirationTime, Data, SessionKey]). + diff --git a/rabbitmq-server/plugins-src/mochiweb-wrapper/20-MAX_RECV_BODY.patch b/rabbitmq-server/plugins-src/mochiweb-wrapper/20-MAX_RECV_BODY.patch new file mode 100644 index 0000000..2656fa2 --- /dev/null +++ b/rabbitmq-server/plugins-src/mochiweb-wrapper/20-MAX_RECV_BODY.patch @@ -0,0 +1,13 @@ +diff --git a/src/mochiweb_request.erl b/src/mochiweb_request.erl +index 5d89662..6765ab0 100644 +--- a/src/mochiweb_request.erl ++++ b/src/mochiweb_request.erl +@@ -42,7 +42,7 @@ + -define(IDLE_TIMEOUT, 300000). + + % Maximum recv_body() length of 1MB +--define(MAX_RECV_BODY, (1024*1024)). ++-define(MAX_RECV_BODY, 104857600). + + %% @spec get_header_value(K) -> undefined | Value + %% @doc Get the value of a given request header. diff --git a/rabbitmq-server/plugins-src/mochiweb-wrapper/30-remove-crypto-ssl-dependencies.patch b/rabbitmq-server/plugins-src/mochiweb-wrapper/30-remove-crypto-ssl-dependencies.patch new file mode 100644 index 0000000..0d5c85a --- /dev/null +++ b/rabbitmq-server/plugins-src/mochiweb-wrapper/30-remove-crypto-ssl-dependencies.patch @@ -0,0 +1,104 @@ +diff --git a/src/mochitemp.erl b/src/mochitemp.erl +index dda7863..f64876d 100644 +--- a/src/mochitemp.erl ++++ b/src/mochitemp.erl +@@ -1,7 +1,7 @@ + %% @author Bob Ippolito + %% @copyright 2010 Mochi Media, Inc. + +-%% @doc Create temporary files and directories. Requires crypto to be started. ++%% @doc Create temporary files and directories. + + -module(mochitemp). + -export([gettempdir/0]). +@@ -87,7 +87,7 @@ rngchars(N) -> + [rngchar() | rngchars(N - 1)]. + + rngchar() -> +- rngchar(crypto:rand_uniform(0, tuple_size(?SAFE_CHARS))). ++ rngchar(mochiweb_util:rand_uniform(0, tuple_size(?SAFE_CHARS))). + + rngchar(C) -> + element(1 + C, ?SAFE_CHARS). +@@ -177,7 +177,6 @@ gettempdir_cwd_test() -> + ok. + + rngchars_test() -> +- crypto:start(), + ?assertEqual( + "", + rngchars(0)), +@@ -199,7 +198,6 @@ rngchar_test() -> + ok. + + mkdtemp_n_failonce_test() -> +- crypto:start(), + D = mkdtemp(), + Path = filename:join([D, "testdir"]), + %% Toggle the existence of a dir so that it fails +@@ -246,7 +244,6 @@ make_dir_fail_test() -> + ok. + + mkdtemp_test() -> +- crypto:start(), + D = mkdtemp(), + ?assertEqual( + true, +@@ -257,7 +254,6 @@ mkdtemp_test() -> + ok. + + rmtempdir_test() -> +- crypto:start(), + D1 = mkdtemp(), + ?assertEqual( + true, +diff --git a/src/mochiweb.app.src b/src/mochiweb.app.src +index 8d75a3a..c98d8a0 100644 +--- a/src/mochiweb.app.src ++++ b/src/mochiweb.app.src +@@ -5,5 +5,5 @@ + {modules, []}, + {registered, []}, + {env, []}, +- {applications, [kernel, stdlib, crypto, inets, ssl, xmerl, ++ {applications, [kernel, stdlib, inets, xmerl, + compiler, syntax_tools]}]}. +diff --git a/src/mochiweb_multipart.erl b/src/mochiweb_multipart.erl +index a83a88c..a4857d6 100644 +--- a/src/mochiweb_multipart.erl ++++ b/src/mochiweb_multipart.erl +@@ -38,7 +38,7 @@ parts_to_body([{Start, End, Body}], ContentType, Size) -> + {HeaderList, Body}; + parts_to_body(BodyList, ContentType, Size) when is_list(BodyList) -> + parts_to_multipart_body(BodyList, ContentType, Size, +- mochihex:to_hex(crypto:rand_bytes(8))). ++ mochihex:to_hex(mochiweb_util:rand_bytes(8))). + + %% @spec parts_to_multipart_body([bodypart()], ContentType::string(), + %% Size::integer(), Boundary::string()) -> +diff --git a/src/mochiweb_util.erl b/src/mochiweb_util.erl +index 4d39990..a0bc2bc 100644 +--- a/src/mochiweb_util.erl ++++ b/src/mochiweb_util.erl +@@ -13,7 +13,7 @@ + -export([record_to_proplist/2, record_to_proplist/3]). + -export([safe_relative_path/1, partition/2]). + -export([parse_qvalues/1, pick_accepted_encodings/3]). +--export([make_io/1]). ++-export([make_io/1, rand_bytes/1, rand_uniform/2]). + + -define(PERCENT, 37). % $\% + -define(FULLSTOP, 46). % $\. +@@ -581,6 +581,12 @@ make_io(Integer) when is_integer(Integer) -> + make_io(Io) when is_list(Io); is_binary(Io) -> + Io. + ++rand_bytes(Count) -> ++ list_to_binary([rand_uniform(0, 16#FF + 1) || _ <- lists:seq(1, Count)]). ++ ++rand_uniform(Lo, Hi) -> ++ random:uniform(Hi - Lo) + Lo - 1. ++ + %% + %% Tests + %% diff --git a/rabbitmq-server/plugins-src/mochiweb-wrapper/40-remove-compiler-syntax_tools-dependencies.patch b/rabbitmq-server/plugins-src/mochiweb-wrapper/40-remove-compiler-syntax_tools-dependencies.patch new file mode 100644 index 0000000..c9938e5 --- /dev/null +++ b/rabbitmq-server/plugins-src/mochiweb-wrapper/40-remove-compiler-syntax_tools-dependencies.patch @@ -0,0 +1,124 @@ +diff --git a/src/mochiglobal.erl b/src/mochiglobal.erl +deleted file mode 100644 +index 6b20e41..0000000 +--- a/src/mochiglobal.erl ++++ /dev/null +@@ -1,107 +0,0 @@ +-%% @author Bob Ippolito +-%% @copyright 2010 Mochi Media, Inc. +-%% @doc Abuse module constant pools as a "read-only shared heap" (since erts 5.6) +-%% [1]. +--module(mochiglobal). +--author("Bob Ippolito "). +--export([get/1, get/2, put/2, delete/1]). +- +-%% -spec get(atom()) -> any() | undefined. +-%% @equiv get(K, undefined) +-get(K) -> +- get(K, undefined). +- +-%% -spec get(atom(), T) -> any() | T. +-%% @doc Get the term for K or return Default. +-get(K, Default) -> +- get(K, Default, key_to_module(K)). +- +-get(_K, Default, Mod) -> +- try Mod:term() +- catch error:undef -> +- Default +- end. +- +-%% -spec put(atom(), any()) -> ok. +-%% @doc Store term V at K, replaces an existing term if present. +-put(K, V) -> +- put(K, V, key_to_module(K)). +- +-put(_K, V, Mod) -> +- Bin = compile(Mod, V), +- code:purge(Mod), +- {module, Mod} = code:load_binary(Mod, atom_to_list(Mod) ++ ".erl", Bin), +- ok. +- +-%% -spec delete(atom()) -> boolean(). +-%% @doc Delete term stored at K, no-op if non-existent. +-delete(K) -> +- delete(K, key_to_module(K)). +- +-delete(_K, Mod) -> +- code:purge(Mod), +- code:delete(Mod). +- +-%% -spec key_to_module(atom()) -> atom(). +-key_to_module(K) -> +- list_to_atom("mochiglobal:" ++ atom_to_list(K)). +- +-%% -spec compile(atom(), any()) -> binary(). +-compile(Module, T) -> +- {ok, Module, Bin} = compile:forms(forms(Module, T), +- [verbose, report_errors]), +- Bin. +- +-%% -spec forms(atom(), any()) -> [erl_syntax:syntaxTree()]. +-forms(Module, T) -> +- [erl_syntax:revert(X) || X <- term_to_abstract(Module, term, T)]. +- +-%% -spec term_to_abstract(atom(), atom(), any()) -> [erl_syntax:syntaxTree()]. +-term_to_abstract(Module, Getter, T) -> +- [%% -module(Module). +- erl_syntax:attribute( +- erl_syntax:atom(module), +- [erl_syntax:atom(Module)]), +- %% -export([Getter/0]). +- erl_syntax:attribute( +- erl_syntax:atom(export), +- [erl_syntax:list( +- [erl_syntax:arity_qualifier( +- erl_syntax:atom(Getter), +- erl_syntax:integer(0))])]), +- %% Getter() -> T. +- erl_syntax:function( +- erl_syntax:atom(Getter), +- [erl_syntax:clause([], none, [erl_syntax:abstract(T)])])]. +- +-%% +-%% Tests +-%% +--ifdef(TEST). +--include_lib("eunit/include/eunit.hrl"). +-get_put_delete_test() -> +- K = '$$test$$mochiglobal', +- delete(K), +- ?assertEqual( +- bar, +- get(K, bar)), +- try +- ?MODULE:put(K, baz), +- ?assertEqual( +- baz, +- get(K, bar)), +- ?MODULE:put(K, wibble), +- ?assertEqual( +- wibble, +- ?MODULE:get(K)) +- after +- delete(K) +- end, +- ?assertEqual( +- bar, +- get(K, bar)), +- ?assertEqual( +- undefined, +- ?MODULE:get(K)), +- ok. +--endif. +diff --git a/src/mochiweb.app.src b/src/mochiweb.app.src +index c98d8a0..4a6808e 100644 +--- a/src/mochiweb.app.src ++++ b/src/mochiweb.app.src +@@ -5,5 +5,4 @@ + {modules, []}, + {registered, []}, + {env, []}, +- {applications, [kernel, stdlib, inets, xmerl, +- compiler, syntax_tools]}]}. ++ {applications, [kernel, stdlib, inets, xmerl]}]}. diff --git a/rabbitmq-server/plugins-src/mochiweb-wrapper/50-remove-json.patch b/rabbitmq-server/plugins-src/mochiweb-wrapper/50-remove-json.patch new file mode 100644 index 0000000..8c7597f --- /dev/null +++ b/rabbitmq-server/plugins-src/mochiweb-wrapper/50-remove-json.patch @@ -0,0 +1,1255 @@ +diff --git a/src/mochijson2.erl b/src/mochijson2.erl +deleted file mode 100644 +index 2b8d16e..0000000 +--- a/src/mochijson2.erl ++++ /dev/null +@@ -1,889 +0,0 @@ +-%% @author Bob Ippolito +-%% @copyright 2007 Mochi Media, Inc. +- +-%% @doc Yet another JSON (RFC 4627) library for Erlang. mochijson2 works +-%% with binaries as strings, arrays as lists (without an {array, _}) +-%% wrapper and it only knows how to decode UTF-8 (and ASCII). +-%% +-%% JSON terms are decoded as follows (javascript -> erlang): +-%%
    +-%%
  • {"key": "value"} -> +-%% {struct, [{<<"key">>, <<"value">>}]}
  • +-%%
  • ["array", 123, 12.34, true, false, null] -> +-%% [<<"array">>, 123, 12.34, true, false, null] +-%%
  • +-%%
+-%%
    +-%%
  • Strings in JSON decode to UTF-8 binaries in Erlang
  • +-%%
  • Objects decode to {struct, PropList}
  • +-%%
  • Numbers decode to integer or float
  • +-%%
  • true, false, null decode to their respective terms.
  • +-%%
+-%% The encoder will accept the same format that the decoder will produce, +-%% but will also allow additional cases for leniency: +-%%
    +-%%
  • atoms other than true, false, null will be considered UTF-8 +-%% strings (even as a proplist key) +-%%
  • +-%%
  • {json, IoList} will insert IoList directly into the output +-%% with no validation +-%%
  • +-%%
  • {array, Array} will be encoded as Array +-%% (legacy mochijson style) +-%%
  • +-%%
  • A non-empty raw proplist will be encoded as an object as long +-%% as the first pair does not have an atom key of json, struct, +-%% or array +-%%
  • +-%%
+- +--module(mochijson2). +--author('bob@mochimedia.com'). +--export([encoder/1, encode/1]). +--export([decoder/1, decode/1, decode/2]). +- +-%% This is a macro to placate syntax highlighters.. +--define(Q, $\"). +--define(ADV_COL(S, N), S#decoder{offset=N+S#decoder.offset, +- column=N+S#decoder.column}). +--define(INC_COL(S), S#decoder{offset=1+S#decoder.offset, +- column=1+S#decoder.column}). +--define(INC_LINE(S), S#decoder{offset=1+S#decoder.offset, +- column=1, +- line=1+S#decoder.line}). +--define(INC_CHAR(S, C), +- case C of +- $\n -> +- S#decoder{column=1, +- line=1+S#decoder.line, +- offset=1+S#decoder.offset}; +- _ -> +- S#decoder{column=1+S#decoder.column, +- offset=1+S#decoder.offset} +- end). +--define(IS_WHITESPACE(C), +- (C =:= $\s orelse C =:= $\t orelse C =:= $\r orelse C =:= $\n)). +- +-%% @type json_string() = atom | binary() +-%% @type json_number() = integer() | float() +-%% @type json_array() = [json_term()] +-%% @type json_object() = {struct, [{json_string(), json_term()}]} +-%% @type json_eep18_object() = {[{json_string(), json_term()}]} +-%% @type json_iolist() = {json, iolist()} +-%% @type json_term() = json_string() | json_number() | json_array() | +-%% json_object() | json_eep18_object() | json_iolist() +- +--record(encoder, {handler=null, +- utf8=false}). +- +--record(decoder, {object_hook=null, +- offset=0, +- line=1, +- column=1, +- state=null}). +- +-%% @spec encoder([encoder_option()]) -> function() +-%% @doc Create an encoder/1 with the given options. +-%% @type encoder_option() = handler_option() | utf8_option() +-%% @type utf8_option() = boolean(). Emit unicode as utf8 (default - false) +-encoder(Options) -> +- State = parse_encoder_options(Options, #encoder{}), +- fun (O) -> json_encode(O, State) end. +- +-%% @spec encode(json_term()) -> iolist() +-%% @doc Encode the given as JSON to an iolist. +-encode(Any) -> +- json_encode(Any, #encoder{}). +- +-%% @spec decoder([decoder_option()]) -> function() +-%% @doc Create a decoder/1 with the given options. +-decoder(Options) -> +- State = parse_decoder_options(Options, #decoder{}), +- fun (O) -> json_decode(O, State) end. +- +-%% @spec decode(iolist(), [{format, proplist | eep18 | struct}]) -> json_term() +-%% @doc Decode the given iolist to Erlang terms using the given object format +-%% for decoding, where proplist returns JSON objects as [{binary(), json_term()}] +-%% proplists, eep18 returns JSON objects as {[binary(), json_term()]}, and struct +-%% returns them as-is. +-decode(S, Options) -> +- json_decode(S, parse_decoder_options(Options, #decoder{})). +- +-%% @spec decode(iolist()) -> json_term() +-%% @doc Decode the given iolist to Erlang terms. +-decode(S) -> +- json_decode(S, #decoder{}). +- +-%% Internal API +- +-parse_encoder_options([], State) -> +- State; +-parse_encoder_options([{handler, Handler} | Rest], State) -> +- parse_encoder_options(Rest, State#encoder{handler=Handler}); +-parse_encoder_options([{utf8, Switch} | Rest], State) -> +- parse_encoder_options(Rest, State#encoder{utf8=Switch}). +- +-parse_decoder_options([], State) -> +- State; +-parse_decoder_options([{object_hook, Hook} | Rest], State) -> +- parse_decoder_options(Rest, State#decoder{object_hook=Hook}); +-parse_decoder_options([{format, Format} | Rest], State) +- when Format =:= struct orelse Format =:= eep18 orelse Format =:= proplist -> +- parse_decoder_options(Rest, State#decoder{object_hook=Format}). +- +-json_encode(true, _State) -> +- <<"true">>; +-json_encode(false, _State) -> +- <<"false">>; +-json_encode(null, _State) -> +- <<"null">>; +-json_encode(I, _State) when is_integer(I) -> +- integer_to_list(I); +-json_encode(F, _State) when is_float(F) -> +- mochinum:digits(F); +-json_encode(S, State) when is_binary(S); is_atom(S) -> +- json_encode_string(S, State); +-json_encode([{K, _}|_] = Props, State) when (K =/= struct andalso +- K =/= array andalso +- K =/= json) -> +- json_encode_proplist(Props, State); +-json_encode({struct, Props}, State) when is_list(Props) -> +- json_encode_proplist(Props, State); +-json_encode({Props}, State) when is_list(Props) -> +- json_encode_proplist(Props, State); +-json_encode({}, State) -> +- json_encode_proplist([], State); +-json_encode(Array, State) when is_list(Array) -> +- json_encode_array(Array, State); +-json_encode({array, Array}, State) when is_list(Array) -> +- json_encode_array(Array, State); +-json_encode({json, IoList}, _State) -> +- IoList; +-json_encode(Bad, #encoder{handler=null}) -> +- exit({json_encode, {bad_term, Bad}}); +-json_encode(Bad, State=#encoder{handler=Handler}) -> +- json_encode(Handler(Bad), State). +- +-json_encode_array([], _State) -> +- <<"[]">>; +-json_encode_array(L, State) -> +- F = fun (O, Acc) -> +- [$,, json_encode(O, State) | Acc] +- end, +- [$, | Acc1] = lists:foldl(F, "[", L), +- lists:reverse([$\] | Acc1]). +- +-json_encode_proplist([], _State) -> +- <<"{}">>; +-json_encode_proplist(Props, State) -> +- F = fun ({K, V}, Acc) -> +- KS = json_encode_string(K, State), +- VS = json_encode(V, State), +- [$,, VS, $:, KS | Acc] +- end, +- [$, | Acc1] = lists:foldl(F, "{", Props), +- lists:reverse([$\} | Acc1]). +- +-json_encode_string(A, State) when is_atom(A) -> +- L = atom_to_list(A), +- case json_string_is_safe(L) of +- true -> +- [?Q, L, ?Q]; +- false -> +- json_encode_string_unicode(xmerl_ucs:from_utf8(L), State, [?Q]) +- end; +-json_encode_string(B, State) when is_binary(B) -> +- case json_bin_is_safe(B) of +- true -> +- [?Q, B, ?Q]; +- false -> +- json_encode_string_unicode(xmerl_ucs:from_utf8(B), State, [?Q]) +- end; +-json_encode_string(I, _State) when is_integer(I) -> +- [?Q, integer_to_list(I), ?Q]; +-json_encode_string(L, State) when is_list(L) -> +- case json_string_is_safe(L) of +- true -> +- [?Q, L, ?Q]; +- false -> +- json_encode_string_unicode(L, State, [?Q]) +- end. +- +-json_string_is_safe([]) -> +- true; +-json_string_is_safe([C | Rest]) -> +- case C of +- ?Q -> +- false; +- $\\ -> +- false; +- $\b -> +- false; +- $\f -> +- false; +- $\n -> +- false; +- $\r -> +- false; +- $\t -> +- false; +- C when C >= 0, C < $\s; C >= 16#7f, C =< 16#10FFFF -> +- false; +- C when C < 16#7f -> +- json_string_is_safe(Rest); +- _ -> +- false +- end. +- +-json_bin_is_safe(<<>>) -> +- true; +-json_bin_is_safe(<>) -> +- case C of +- ?Q -> +- false; +- $\\ -> +- false; +- $\b -> +- false; +- $\f -> +- false; +- $\n -> +- false; +- $\r -> +- false; +- $\t -> +- false; +- C when C >= 0, C < $\s; C >= 16#7f -> +- false; +- C when C < 16#7f -> +- json_bin_is_safe(Rest) +- end. +- +-json_encode_string_unicode([], _State, Acc) -> +- lists:reverse([$\" | Acc]); +-json_encode_string_unicode([C | Cs], State, Acc) -> +- Acc1 = case C of +- ?Q -> +- [?Q, $\\ | Acc]; +- %% Escaping solidus is only useful when trying to protect +- %% against "" injection attacks which are only +- %% possible when JSON is inserted into a HTML document +- %% in-line. mochijson2 does not protect you from this, so +- %% if you do insert directly into HTML then you need to +- %% uncomment the following case or escape the output of encode. +- %% +- %% $/ -> +- %% [$/, $\\ | Acc]; +- %% +- $\\ -> +- [$\\, $\\ | Acc]; +- $\b -> +- [$b, $\\ | Acc]; +- $\f -> +- [$f, $\\ | Acc]; +- $\n -> +- [$n, $\\ | Acc]; +- $\r -> +- [$r, $\\ | Acc]; +- $\t -> +- [$t, $\\ | Acc]; +- C when C >= 0, C < $\s -> +- [unihex(C) | Acc]; +- C when C >= 16#7f, C =< 16#10FFFF, State#encoder.utf8 -> +- [xmerl_ucs:to_utf8(C) | Acc]; +- C when C >= 16#7f, C =< 16#10FFFF, not State#encoder.utf8 -> +- [unihex(C) | Acc]; +- C when C < 16#7f -> +- [C | Acc]; +- _ -> +- exit({json_encode, {bad_char, C}}) +- end, +- json_encode_string_unicode(Cs, State, Acc1). +- +-hexdigit(C) when C >= 0, C =< 9 -> +- C + $0; +-hexdigit(C) when C =< 15 -> +- C + $a - 10. +- +-unihex(C) when C < 16#10000 -> +- <> = <>, +- Digits = [hexdigit(D) || D <- [D3, D2, D1, D0]], +- [$\\, $u | Digits]; +-unihex(C) when C =< 16#10FFFF -> +- N = C - 16#10000, +- S1 = 16#d800 bor ((N bsr 10) band 16#3ff), +- S2 = 16#dc00 bor (N band 16#3ff), +- [unihex(S1), unihex(S2)]. +- +-json_decode(L, S) when is_list(L) -> +- json_decode(iolist_to_binary(L), S); +-json_decode(B, S) -> +- {Res, S1} = decode1(B, S), +- {eof, _} = tokenize(B, S1#decoder{state=trim}), +- Res. +- +-decode1(B, S=#decoder{state=null}) -> +- case tokenize(B, S#decoder{state=any}) of +- {{const, C}, S1} -> +- {C, S1}; +- {start_array, S1} -> +- decode_array(B, S1); +- {start_object, S1} -> +- decode_object(B, S1) +- end. +- +-make_object(V, #decoder{object_hook=N}) when N =:= null orelse N =:= struct -> +- V; +-make_object({struct, P}, #decoder{object_hook=eep18}) -> +- {P}; +-make_object({struct, P}, #decoder{object_hook=proplist}) -> +- P; +-make_object(V, #decoder{object_hook=Hook}) -> +- Hook(V). +- +-decode_object(B, S) -> +- decode_object(B, S#decoder{state=key}, []). +- +-decode_object(B, S=#decoder{state=key}, Acc) -> +- case tokenize(B, S) of +- {end_object, S1} -> +- V = make_object({struct, lists:reverse(Acc)}, S1), +- {V, S1#decoder{state=null}}; +- {{const, K}, S1} -> +- {colon, S2} = tokenize(B, S1), +- {V, S3} = decode1(B, S2#decoder{state=null}), +- decode_object(B, S3#decoder{state=comma}, [{K, V} | Acc]) +- end; +-decode_object(B, S=#decoder{state=comma}, Acc) -> +- case tokenize(B, S) of +- {end_object, S1} -> +- V = make_object({struct, lists:reverse(Acc)}, S1), +- {V, S1#decoder{state=null}}; +- {comma, S1} -> +- decode_object(B, S1#decoder{state=key}, Acc) +- end. +- +-decode_array(B, S) -> +- decode_array(B, S#decoder{state=any}, []). +- +-decode_array(B, S=#decoder{state=any}, Acc) -> +- case tokenize(B, S) of +- {end_array, S1} -> +- {lists:reverse(Acc), S1#decoder{state=null}}; +- {start_array, S1} -> +- {Array, S2} = decode_array(B, S1), +- decode_array(B, S2#decoder{state=comma}, [Array | Acc]); +- {start_object, S1} -> +- {Array, S2} = decode_object(B, S1), +- decode_array(B, S2#decoder{state=comma}, [Array | Acc]); +- {{const, Const}, S1} -> +- decode_array(B, S1#decoder{state=comma}, [Const | Acc]) +- end; +-decode_array(B, S=#decoder{state=comma}, Acc) -> +- case tokenize(B, S) of +- {end_array, S1} -> +- {lists:reverse(Acc), S1#decoder{state=null}}; +- {comma, S1} -> +- decode_array(B, S1#decoder{state=any}, Acc) +- end. +- +-tokenize_string(B, S=#decoder{offset=O}) -> +- case tokenize_string_fast(B, O) of +- {escape, O1} -> +- Length = O1 - O, +- S1 = ?ADV_COL(S, Length), +- <<_:O/binary, Head:Length/binary, _/binary>> = B, +- tokenize_string(B, S1, lists:reverse(binary_to_list(Head))); +- O1 -> +- Length = O1 - O, +- <<_:O/binary, String:Length/binary, ?Q, _/binary>> = B, +- {{const, String}, ?ADV_COL(S, Length + 1)} +- end. +- +-tokenize_string_fast(B, O) -> +- case B of +- <<_:O/binary, ?Q, _/binary>> -> +- O; +- <<_:O/binary, $\\, _/binary>> -> +- {escape, O}; +- <<_:O/binary, C1, _/binary>> when C1 < 128 -> +- tokenize_string_fast(B, 1 + O); +- <<_:O/binary, C1, C2, _/binary>> when C1 >= 194, C1 =< 223, +- C2 >= 128, C2 =< 191 -> +- tokenize_string_fast(B, 2 + O); +- <<_:O/binary, C1, C2, C3, _/binary>> when C1 >= 224, C1 =< 239, +- C2 >= 128, C2 =< 191, +- C3 >= 128, C3 =< 191 -> +- tokenize_string_fast(B, 3 + O); +- <<_:O/binary, C1, C2, C3, C4, _/binary>> when C1 >= 240, C1 =< 244, +- C2 >= 128, C2 =< 191, +- C3 >= 128, C3 =< 191, +- C4 >= 128, C4 =< 191 -> +- tokenize_string_fast(B, 4 + O); +- _ -> +- throw(invalid_utf8) +- end. +- +-tokenize_string(B, S=#decoder{offset=O}, Acc) -> +- case B of +- <<_:O/binary, ?Q, _/binary>> -> +- {{const, iolist_to_binary(lists:reverse(Acc))}, ?INC_COL(S)}; +- <<_:O/binary, "\\\"", _/binary>> -> +- tokenize_string(B, ?ADV_COL(S, 2), [$\" | Acc]); +- <<_:O/binary, "\\\\", _/binary>> -> +- tokenize_string(B, ?ADV_COL(S, 2), [$\\ | Acc]); +- <<_:O/binary, "\\/", _/binary>> -> +- tokenize_string(B, ?ADV_COL(S, 2), [$/ | Acc]); +- <<_:O/binary, "\\b", _/binary>> -> +- tokenize_string(B, ?ADV_COL(S, 2), [$\b | Acc]); +- <<_:O/binary, "\\f", _/binary>> -> +- tokenize_string(B, ?ADV_COL(S, 2), [$\f | Acc]); +- <<_:O/binary, "\\n", _/binary>> -> +- tokenize_string(B, ?ADV_COL(S, 2), [$\n | Acc]); +- <<_:O/binary, "\\r", _/binary>> -> +- tokenize_string(B, ?ADV_COL(S, 2), [$\r | Acc]); +- <<_:O/binary, "\\t", _/binary>> -> +- tokenize_string(B, ?ADV_COL(S, 2), [$\t | Acc]); +- <<_:O/binary, "\\u", C3, C2, C1, C0, Rest/binary>> -> +- C = erlang:list_to_integer([C3, C2, C1, C0], 16), +- if C > 16#D7FF, C < 16#DC00 -> +- %% coalesce UTF-16 surrogate pair +- <<"\\u", D3, D2, D1, D0, _/binary>> = Rest, +- D = erlang:list_to_integer([D3,D2,D1,D0], 16), +- [CodePoint] = xmerl_ucs:from_utf16be(<>), +- Acc1 = lists:reverse(xmerl_ucs:to_utf8(CodePoint), Acc), +- tokenize_string(B, ?ADV_COL(S, 12), Acc1); +- true -> +- Acc1 = lists:reverse(xmerl_ucs:to_utf8(C), Acc), +- tokenize_string(B, ?ADV_COL(S, 6), Acc1) +- end; +- <<_:O/binary, C1, _/binary>> when C1 < 128 -> +- tokenize_string(B, ?INC_CHAR(S, C1), [C1 | Acc]); +- <<_:O/binary, C1, C2, _/binary>> when C1 >= 194, C1 =< 223, +- C2 >= 128, C2 =< 191 -> +- tokenize_string(B, ?ADV_COL(S, 2), [C2, C1 | Acc]); +- <<_:O/binary, C1, C2, C3, _/binary>> when C1 >= 224, C1 =< 239, +- C2 >= 128, C2 =< 191, +- C3 >= 128, C3 =< 191 -> +- tokenize_string(B, ?ADV_COL(S, 3), [C3, C2, C1 | Acc]); +- <<_:O/binary, C1, C2, C3, C4, _/binary>> when C1 >= 240, C1 =< 244, +- C2 >= 128, C2 =< 191, +- C3 >= 128, C3 =< 191, +- C4 >= 128, C4 =< 191 -> +- tokenize_string(B, ?ADV_COL(S, 4), [C4, C3, C2, C1 | Acc]); +- _ -> +- throw(invalid_utf8) +- end. +- +-tokenize_number(B, S) -> +- case tokenize_number(B, sign, S, []) of +- {{int, Int}, S1} -> +- {{const, list_to_integer(Int)}, S1}; +- {{float, Float}, S1} -> +- {{const, list_to_float(Float)}, S1} +- end. +- +-tokenize_number(B, sign, S=#decoder{offset=O}, []) -> +- case B of +- <<_:O/binary, $-, _/binary>> -> +- tokenize_number(B, int, ?INC_COL(S), [$-]); +- _ -> +- tokenize_number(B, int, S, []) +- end; +-tokenize_number(B, int, S=#decoder{offset=O}, Acc) -> +- case B of +- <<_:O/binary, $0, _/binary>> -> +- tokenize_number(B, frac, ?INC_COL(S), [$0 | Acc]); +- <<_:O/binary, C, _/binary>> when C >= $1 andalso C =< $9 -> +- tokenize_number(B, int1, ?INC_COL(S), [C | Acc]) +- end; +-tokenize_number(B, int1, S=#decoder{offset=O}, Acc) -> +- case B of +- <<_:O/binary, C, _/binary>> when C >= $0 andalso C =< $9 -> +- tokenize_number(B, int1, ?INC_COL(S), [C | Acc]); +- _ -> +- tokenize_number(B, frac, S, Acc) +- end; +-tokenize_number(B, frac, S=#decoder{offset=O}, Acc) -> +- case B of +- <<_:O/binary, $., C, _/binary>> when C >= $0, C =< $9 -> +- tokenize_number(B, frac1, ?ADV_COL(S, 2), [C, $. | Acc]); +- <<_:O/binary, E, _/binary>> when E =:= $e orelse E =:= $E -> +- tokenize_number(B, esign, ?INC_COL(S), [$e, $0, $. | Acc]); +- _ -> +- {{int, lists:reverse(Acc)}, S} +- end; +-tokenize_number(B, frac1, S=#decoder{offset=O}, Acc) -> +- case B of +- <<_:O/binary, C, _/binary>> when C >= $0 andalso C =< $9 -> +- tokenize_number(B, frac1, ?INC_COL(S), [C | Acc]); +- <<_:O/binary, E, _/binary>> when E =:= $e orelse E =:= $E -> +- tokenize_number(B, esign, ?INC_COL(S), [$e | Acc]); +- _ -> +- {{float, lists:reverse(Acc)}, S} +- end; +-tokenize_number(B, esign, S=#decoder{offset=O}, Acc) -> +- case B of +- <<_:O/binary, C, _/binary>> when C =:= $- orelse C=:= $+ -> +- tokenize_number(B, eint, ?INC_COL(S), [C | Acc]); +- _ -> +- tokenize_number(B, eint, S, Acc) +- end; +-tokenize_number(B, eint, S=#decoder{offset=O}, Acc) -> +- case B of +- <<_:O/binary, C, _/binary>> when C >= $0 andalso C =< $9 -> +- tokenize_number(B, eint1, ?INC_COL(S), [C | Acc]) +- end; +-tokenize_number(B, eint1, S=#decoder{offset=O}, Acc) -> +- case B of +- <<_:O/binary, C, _/binary>> when C >= $0 andalso C =< $9 -> +- tokenize_number(B, eint1, ?INC_COL(S), [C | Acc]); +- _ -> +- {{float, lists:reverse(Acc)}, S} +- end. +- +-tokenize(B, S=#decoder{offset=O}) -> +- case B of +- <<_:O/binary, C, _/binary>> when ?IS_WHITESPACE(C) -> +- tokenize(B, ?INC_CHAR(S, C)); +- <<_:O/binary, "{", _/binary>> -> +- {start_object, ?INC_COL(S)}; +- <<_:O/binary, "}", _/binary>> -> +- {end_object, ?INC_COL(S)}; +- <<_:O/binary, "[", _/binary>> -> +- {start_array, ?INC_COL(S)}; +- <<_:O/binary, "]", _/binary>> -> +- {end_array, ?INC_COL(S)}; +- <<_:O/binary, ",", _/binary>> -> +- {comma, ?INC_COL(S)}; +- <<_:O/binary, ":", _/binary>> -> +- {colon, ?INC_COL(S)}; +- <<_:O/binary, "null", _/binary>> -> +- {{const, null}, ?ADV_COL(S, 4)}; +- <<_:O/binary, "true", _/binary>> -> +- {{const, true}, ?ADV_COL(S, 4)}; +- <<_:O/binary, "false", _/binary>> -> +- {{const, false}, ?ADV_COL(S, 5)}; +- <<_:O/binary, "\"", _/binary>> -> +- tokenize_string(B, ?INC_COL(S)); +- <<_:O/binary, C, _/binary>> when (C >= $0 andalso C =< $9) +- orelse C =:= $- -> +- tokenize_number(B, S); +- <<_:O/binary>> -> +- trim = S#decoder.state, +- {eof, S} +- end. +-%% +-%% Tests +-%% +--ifdef(TEST). +--include_lib("eunit/include/eunit.hrl"). +- +- +-%% testing constructs borrowed from the Yaws JSON implementation. +- +-%% Create an object from a list of Key/Value pairs. +- +-obj_new() -> +- {struct, []}. +- +-is_obj({struct, Props}) -> +- F = fun ({K, _}) when is_binary(K) -> true end, +- lists:all(F, Props). +- +-obj_from_list(Props) -> +- Obj = {struct, Props}, +- ?assert(is_obj(Obj)), +- Obj. +- +-%% Test for equivalence of Erlang terms. +-%% Due to arbitrary order of construction, equivalent objects might +-%% compare unequal as erlang terms, so we need to carefully recurse +-%% through aggregates (tuples and objects). +- +-equiv({struct, Props1}, {struct, Props2}) -> +- equiv_object(Props1, Props2); +-equiv(L1, L2) when is_list(L1), is_list(L2) -> +- equiv_list(L1, L2); +-equiv(N1, N2) when is_number(N1), is_number(N2) -> N1 == N2; +-equiv(B1, B2) when is_binary(B1), is_binary(B2) -> B1 == B2; +-equiv(A, A) when A =:= true orelse A =:= false orelse A =:= null -> true. +- +-%% Object representation and traversal order is unknown. +-%% Use the sledgehammer and sort property lists. +- +-equiv_object(Props1, Props2) -> +- L1 = lists:keysort(1, Props1), +- L2 = lists:keysort(1, Props2), +- Pairs = lists:zip(L1, L2), +- true = lists:all(fun({{K1, V1}, {K2, V2}}) -> +- equiv(K1, K2) and equiv(V1, V2) +- end, Pairs). +- +-%% Recursively compare tuple elements for equivalence. +- +-equiv_list([], []) -> +- true; +-equiv_list([V1 | L1], [V2 | L2]) -> +- equiv(V1, V2) andalso equiv_list(L1, L2). +- +-decode_test() -> +- [1199344435545.0, 1] = decode(<<"[1199344435545.0,1]">>), +- <<16#F0,16#9D,16#9C,16#95>> = decode([34,"\\ud835","\\udf15",34]). +- +-e2j_vec_test() -> +- test_one(e2j_test_vec(utf8), 1). +- +-test_one([], _N) -> +- %% io:format("~p tests passed~n", [N-1]), +- ok; +-test_one([{E, J} | Rest], N) -> +- %% io:format("[~p] ~p ~p~n", [N, E, J]), +- true = equiv(E, decode(J)), +- true = equiv(E, decode(encode(E))), +- test_one(Rest, 1+N). +- +-e2j_test_vec(utf8) -> +- [ +- {1, "1"}, +- {3.1416, "3.14160"}, %% text representation may truncate, trail zeroes +- {-1, "-1"}, +- {-3.1416, "-3.14160"}, +- {12.0e10, "1.20000e+11"}, +- {1.234E+10, "1.23400e+10"}, +- {-1.234E-10, "-1.23400e-10"}, +- {10.0, "1.0e+01"}, +- {123.456, "1.23456E+2"}, +- {10.0, "1e1"}, +- {<<"foo">>, "\"foo\""}, +- {<<"foo", 5, "bar">>, "\"foo\\u0005bar\""}, +- {<<"">>, "\"\""}, +- {<<"\n\n\n">>, "\"\\n\\n\\n\""}, +- {<<"\" \b\f\r\n\t\"">>, "\"\\\" \\b\\f\\r\\n\\t\\\"\""}, +- {obj_new(), "{}"}, +- {obj_from_list([{<<"foo">>, <<"bar">>}]), "{\"foo\":\"bar\"}"}, +- {obj_from_list([{<<"foo">>, <<"bar">>}, {<<"baz">>, 123}]), +- "{\"foo\":\"bar\",\"baz\":123}"}, +- {[], "[]"}, +- {[[]], "[[]]"}, +- {[1, <<"foo">>], "[1,\"foo\"]"}, +- +- %% json array in a json object +- {obj_from_list([{<<"foo">>, [123]}]), +- "{\"foo\":[123]}"}, +- +- %% json object in a json object +- {obj_from_list([{<<"foo">>, obj_from_list([{<<"bar">>, true}])}]), +- "{\"foo\":{\"bar\":true}}"}, +- +- %% fold evaluation order +- {obj_from_list([{<<"foo">>, []}, +- {<<"bar">>, obj_from_list([{<<"baz">>, true}])}, +- {<<"alice">>, <<"bob">>}]), +- "{\"foo\":[],\"bar\":{\"baz\":true},\"alice\":\"bob\"}"}, +- +- %% json object in a json array +- {[-123, <<"foo">>, obj_from_list([{<<"bar">>, []}]), null], +- "[-123,\"foo\",{\"bar\":[]},null]"} +- ]. +- +-%% test utf8 encoding +-encoder_utf8_test() -> +- %% safe conversion case (default) +- [34,"\\u0001","\\u0442","\\u0435","\\u0441","\\u0442",34] = +- encode(<<1,"\321\202\320\265\321\201\321\202">>), +- +- %% raw utf8 output (optional) +- Enc = mochijson2:encoder([{utf8, true}]), +- [34,"\\u0001",[209,130],[208,181],[209,129],[209,130],34] = +- Enc(<<1,"\321\202\320\265\321\201\321\202">>). +- +-input_validation_test() -> +- Good = [ +- {16#00A3, <>}, %% pound +- {16#20AC, <>}, %% euro +- {16#10196, <>} %% denarius +- ], +- lists:foreach(fun({CodePoint, UTF8}) -> +- Expect = list_to_binary(xmerl_ucs:to_utf8(CodePoint)), +- Expect = decode(UTF8) +- end, Good), +- +- Bad = [ +- %% 2nd, 3rd, or 4th byte of a multi-byte sequence w/o leading byte +- <>, +- %% missing continuations, last byte in each should be 80-BF +- <>, +- <>, +- <>, +- %% we don't support code points > 10FFFF per RFC 3629 +- <>, +- %% escape characters trigger a different code path +- <> +- ], +- lists:foreach( +- fun(X) -> +- ok = try decode(X) catch invalid_utf8 -> ok end, +- %% could be {ucs,{bad_utf8_character_code}} or +- %% {json_encode,{bad_char,_}} +- {'EXIT', _} = (catch encode(X)) +- end, Bad). +- +-inline_json_test() -> +- ?assertEqual(<<"\"iodata iodata\"">>, +- iolist_to_binary( +- encode({json, [<<"\"iodata">>, " iodata\""]}))), +- ?assertEqual({struct, [{<<"key">>, <<"iodata iodata">>}]}, +- decode( +- encode({struct, +- [{key, {json, [<<"\"iodata">>, " iodata\""]}}]}))), +- ok. +- +-big_unicode_test() -> +- UTF8Seq = list_to_binary(xmerl_ucs:to_utf8(16#0001d120)), +- ?assertEqual( +- <<"\"\\ud834\\udd20\"">>, +- iolist_to_binary(encode(UTF8Seq))), +- ?assertEqual( +- UTF8Seq, +- decode(iolist_to_binary(encode(UTF8Seq)))), +- ok. +- +-custom_decoder_test() -> +- ?assertEqual( +- {struct, [{<<"key">>, <<"value">>}]}, +- (decoder([]))("{\"key\": \"value\"}")), +- F = fun ({struct, [{<<"key">>, <<"value">>}]}) -> win end, +- ?assertEqual( +- win, +- (decoder([{object_hook, F}]))("{\"key\": \"value\"}")), +- ok. +- +-atom_test() -> +- %% JSON native atoms +- [begin +- ?assertEqual(A, decode(atom_to_list(A))), +- ?assertEqual(iolist_to_binary(atom_to_list(A)), +- iolist_to_binary(encode(A))) +- end || A <- [true, false, null]], +- %% Atom to string +- ?assertEqual( +- <<"\"foo\"">>, +- iolist_to_binary(encode(foo))), +- ?assertEqual( +- <<"\"\\ud834\\udd20\"">>, +- iolist_to_binary(encode(list_to_atom(xmerl_ucs:to_utf8(16#0001d120))))), +- ok. +- +-key_encode_test() -> +- %% Some forms are accepted as keys that would not be strings in other +- %% cases +- ?assertEqual( +- <<"{\"foo\":1}">>, +- iolist_to_binary(encode({struct, [{foo, 1}]}))), +- ?assertEqual( +- <<"{\"foo\":1}">>, +- iolist_to_binary(encode({struct, [{<<"foo">>, 1}]}))), +- ?assertEqual( +- <<"{\"foo\":1}">>, +- iolist_to_binary(encode({struct, [{"foo", 1}]}))), +- ?assertEqual( +- <<"{\"foo\":1}">>, +- iolist_to_binary(encode([{foo, 1}]))), +- ?assertEqual( +- <<"{\"foo\":1}">>, +- iolist_to_binary(encode([{<<"foo">>, 1}]))), +- ?assertEqual( +- <<"{\"foo\":1}">>, +- iolist_to_binary(encode([{"foo", 1}]))), +- ?assertEqual( +- <<"{\"\\ud834\\udd20\":1}">>, +- iolist_to_binary( +- encode({struct, [{[16#0001d120], 1}]}))), +- ?assertEqual( +- <<"{\"1\":1}">>, +- iolist_to_binary(encode({struct, [{1, 1}]}))), +- ok. +- +-unsafe_chars_test() -> +- Chars = "\"\\\b\f\n\r\t", +- [begin +- ?assertEqual(false, json_string_is_safe([C])), +- ?assertEqual(false, json_bin_is_safe(<>)), +- ?assertEqual(<>, decode(encode(<>))) +- end || C <- Chars], +- ?assertEqual( +- false, +- json_string_is_safe([16#0001d120])), +- ?assertEqual( +- false, +- json_bin_is_safe(list_to_binary(xmerl_ucs:to_utf8(16#0001d120)))), +- ?assertEqual( +- [16#0001d120], +- xmerl_ucs:from_utf8( +- binary_to_list( +- decode(encode(list_to_atom(xmerl_ucs:to_utf8(16#0001d120))))))), +- ?assertEqual( +- false, +- json_string_is_safe([16#110000])), +- ?assertEqual( +- false, +- json_bin_is_safe(list_to_binary(xmerl_ucs:to_utf8([16#110000])))), +- %% solidus can be escaped but isn't unsafe by default +- ?assertEqual( +- <<"/">>, +- decode(<<"\"\\/\"">>)), +- ok. +- +-int_test() -> +- ?assertEqual(0, decode("0")), +- ?assertEqual(1, decode("1")), +- ?assertEqual(11, decode("11")), +- ok. +- +-large_int_test() -> +- ?assertEqual(<<"-2147483649214748364921474836492147483649">>, +- iolist_to_binary(encode(-2147483649214748364921474836492147483649))), +- ?assertEqual(<<"2147483649214748364921474836492147483649">>, +- iolist_to_binary(encode(2147483649214748364921474836492147483649))), +- ok. +- +-float_test() -> +- ?assertEqual(<<"-2147483649.0">>, iolist_to_binary(encode(-2147483649.0))), +- ?assertEqual(<<"2147483648.0">>, iolist_to_binary(encode(2147483648.0))), +- ok. +- +-handler_test() -> +- ?assertEqual( +- {'EXIT',{json_encode,{bad_term,{x,y}}}}, +- catch encode({x,y})), +- F = fun ({x,y}) -> [] end, +- ?assertEqual( +- <<"[]">>, +- iolist_to_binary((encoder([{handler, F}]))({x, y}))), +- ok. +- +-encode_empty_test_() -> +- [{A, ?_assertEqual(<<"{}">>, iolist_to_binary(encode(B)))} +- || {A, B} <- [{"eep18 {}", {}}, +- {"eep18 {[]}", {[]}}, +- {"{struct, []}", {struct, []}}]]. +- +-encode_test_() -> +- P = [{<<"k">>, <<"v">>}], +- JSON = iolist_to_binary(encode({struct, P})), +- [{atom_to_list(F), +- ?_assertEqual(JSON, iolist_to_binary(encode(decode(JSON, [{format, F}]))))} +- || F <- [struct, eep18, proplist]]. +- +-format_test_() -> +- P = [{<<"k">>, <<"v">>}], +- JSON = iolist_to_binary(encode({struct, P})), +- [{atom_to_list(F), +- ?_assertEqual(A, decode(JSON, [{format, F}]))} +- || {F, A} <- [{struct, {struct, P}}, +- {eep18, {P}}, +- {proplist, P}]]. +- +--endif. +diff --git a/src/mochinum.erl b/src/mochinum.erl +deleted file mode 100644 +index c52b15c..0000000 +--- a/src/mochinum.erl ++++ /dev/null +@@ -1,354 +0,0 @@ +-%% @copyright 2007 Mochi Media, Inc. +-%% @author Bob Ippolito +- +-%% @doc Useful numeric algorithms for floats that cover some deficiencies +-%% in the math module. More interesting is digits/1, which implements +-%% the algorithm from: +-%% http://www.cs.indiana.edu/~burger/fp/index.html +-%% See also "Printing Floating-Point Numbers Quickly and Accurately" +-%% in Proceedings of the SIGPLAN '96 Conference on Programming Language +-%% Design and Implementation. +- +--module(mochinum). +--author("Bob Ippolito "). +--export([digits/1, frexp/1, int_pow/2, int_ceil/1]). +- +-%% IEEE 754 Float exponent bias +--define(FLOAT_BIAS, 1022). +--define(MIN_EXP, -1074). +--define(BIG_POW, 4503599627370496). +- +-%% External API +- +-%% @spec digits(number()) -> string() +-%% @doc Returns a string that accurately represents the given integer or float +-%% using a conservative amount of digits. Great for generating +-%% human-readable output, or compact ASCII serializations for floats. +-digits(N) when is_integer(N) -> +- integer_to_list(N); +-digits(0.0) -> +- "0.0"; +-digits(Float) -> +- {Frac1, Exp1} = frexp_int(Float), +- [Place0 | Digits0] = digits1(Float, Exp1, Frac1), +- {Place, Digits} = transform_digits(Place0, Digits0), +- R = insert_decimal(Place, Digits), +- case Float < 0 of +- true -> +- [$- | R]; +- _ -> +- R +- end. +- +-%% @spec frexp(F::float()) -> {Frac::float(), Exp::float()} +-%% @doc Return the fractional and exponent part of an IEEE 754 double, +-%% equivalent to the libc function of the same name. +-%% F = Frac * pow(2, Exp). +-frexp(F) -> +- frexp1(unpack(F)). +- +-%% @spec int_pow(X::integer(), N::integer()) -> Y::integer() +-%% @doc Moderately efficient way to exponentiate integers. +-%% int_pow(10, 2) = 100. +-int_pow(_X, 0) -> +- 1; +-int_pow(X, N) when N > 0 -> +- int_pow(X, N, 1). +- +-%% @spec int_ceil(F::float()) -> integer() +-%% @doc Return the ceiling of F as an integer. The ceiling is defined as +-%% F when F == trunc(F); +-%% trunc(F) when F < 0; +-%% trunc(F) + 1 when F > 0. +-int_ceil(X) -> +- T = trunc(X), +- case (X - T) of +- Pos when Pos > 0 -> T + 1; +- _ -> T +- end. +- +- +-%% Internal API +- +-int_pow(X, N, R) when N < 2 -> +- R * X; +-int_pow(X, N, R) -> +- int_pow(X * X, N bsr 1, case N band 1 of 1 -> R * X; 0 -> R end). +- +-insert_decimal(0, S) -> +- "0." ++ S; +-insert_decimal(Place, S) when Place > 0 -> +- L = length(S), +- case Place - L of +- 0 -> +- S ++ ".0"; +- N when N < 0 -> +- {S0, S1} = lists:split(L + N, S), +- S0 ++ "." ++ S1; +- N when N < 6 -> +- %% More places than digits +- S ++ lists:duplicate(N, $0) ++ ".0"; +- _ -> +- insert_decimal_exp(Place, S) +- end; +-insert_decimal(Place, S) when Place > -6 -> +- "0." ++ lists:duplicate(abs(Place), $0) ++ S; +-insert_decimal(Place, S) -> +- insert_decimal_exp(Place, S). +- +-insert_decimal_exp(Place, S) -> +- [C | S0] = S, +- S1 = case S0 of +- [] -> +- "0"; +- _ -> +- S0 +- end, +- Exp = case Place < 0 of +- true -> +- "e-"; +- false -> +- "e+" +- end, +- [C] ++ "." ++ S1 ++ Exp ++ integer_to_list(abs(Place - 1)). +- +- +-digits1(Float, Exp, Frac) -> +- Round = ((Frac band 1) =:= 0), +- case Exp >= 0 of +- true -> +- BExp = 1 bsl Exp, +- case (Frac =/= ?BIG_POW) of +- true -> +- scale((Frac * BExp * 2), 2, BExp, BExp, +- Round, Round, Float); +- false -> +- scale((Frac * BExp * 4), 4, (BExp * 2), BExp, +- Round, Round, Float) +- end; +- false -> +- case (Exp =:= ?MIN_EXP) orelse (Frac =/= ?BIG_POW) of +- true -> +- scale((Frac * 2), 1 bsl (1 - Exp), 1, 1, +- Round, Round, Float); +- false -> +- scale((Frac * 4), 1 bsl (2 - Exp), 2, 1, +- Round, Round, Float) +- end +- end. +- +-scale(R, S, MPlus, MMinus, LowOk, HighOk, Float) -> +- Est = int_ceil(math:log10(abs(Float)) - 1.0e-10), +- %% Note that the scheme implementation uses a 326 element look-up table +- %% for int_pow(10, N) where we do not. +- case Est >= 0 of +- true -> +- fixup(R, S * int_pow(10, Est), MPlus, MMinus, Est, +- LowOk, HighOk); +- false -> +- Scale = int_pow(10, -Est), +- fixup(R * Scale, S, MPlus * Scale, MMinus * Scale, Est, +- LowOk, HighOk) +- end. +- +-fixup(R, S, MPlus, MMinus, K, LowOk, HighOk) -> +- TooLow = case HighOk of +- true -> +- (R + MPlus) >= S; +- false -> +- (R + MPlus) > S +- end, +- case TooLow of +- true -> +- [(K + 1) | generate(R, S, MPlus, MMinus, LowOk, HighOk)]; +- false -> +- [K | generate(R * 10, S, MPlus * 10, MMinus * 10, LowOk, HighOk)] +- end. +- +-generate(R0, S, MPlus, MMinus, LowOk, HighOk) -> +- D = R0 div S, +- R = R0 rem S, +- TC1 = case LowOk of +- true -> +- R =< MMinus; +- false -> +- R < MMinus +- end, +- TC2 = case HighOk of +- true -> +- (R + MPlus) >= S; +- false -> +- (R + MPlus) > S +- end, +- case TC1 of +- false -> +- case TC2 of +- false -> +- [D | generate(R * 10, S, MPlus * 10, MMinus * 10, +- LowOk, HighOk)]; +- true -> +- [D + 1] +- end; +- true -> +- case TC2 of +- false -> +- [D]; +- true -> +- case R * 2 < S of +- true -> +- [D]; +- false -> +- [D + 1] +- end +- end +- end. +- +-unpack(Float) -> +- <> = <>, +- {Sign, Exp, Frac}. +- +-frexp1({_Sign, 0, 0}) -> +- {0.0, 0}; +-frexp1({Sign, 0, Frac}) -> +- Exp = log2floor(Frac), +- <> = <>, +- {Frac1, -(?FLOAT_BIAS) - 52 + Exp}; +-frexp1({Sign, Exp, Frac}) -> +- <> = <>, +- {Frac1, Exp - ?FLOAT_BIAS}. +- +-log2floor(Int) -> +- log2floor(Int, 0). +- +-log2floor(0, N) -> +- N; +-log2floor(Int, N) -> +- log2floor(Int bsr 1, 1 + N). +- +- +-transform_digits(Place, [0 | Rest]) -> +- transform_digits(Place, Rest); +-transform_digits(Place, Digits) -> +- {Place, [$0 + D || D <- Digits]}. +- +- +-frexp_int(F) -> +- case unpack(F) of +- {_Sign, 0, Frac} -> +- {Frac, ?MIN_EXP}; +- {_Sign, Exp, Frac} -> +- {Frac + (1 bsl 52), Exp - 53 - ?FLOAT_BIAS} +- end. +- +-%% +-%% Tests +-%% +--ifdef(TEST). +--include_lib("eunit/include/eunit.hrl"). +- +-int_ceil_test() -> +- ?assertEqual(1, int_ceil(0.0001)), +- ?assertEqual(0, int_ceil(0.0)), +- ?assertEqual(1, int_ceil(0.99)), +- ?assertEqual(1, int_ceil(1.0)), +- ?assertEqual(-1, int_ceil(-1.5)), +- ?assertEqual(-2, int_ceil(-2.0)), +- ok. +- +-int_pow_test() -> +- ?assertEqual(1, int_pow(1, 1)), +- ?assertEqual(1, int_pow(1, 0)), +- ?assertEqual(1, int_pow(10, 0)), +- ?assertEqual(10, int_pow(10, 1)), +- ?assertEqual(100, int_pow(10, 2)), +- ?assertEqual(1000, int_pow(10, 3)), +- ok. +- +-digits_test() -> +- ?assertEqual("0", +- digits(0)), +- ?assertEqual("0.0", +- digits(0.0)), +- ?assertEqual("1.0", +- digits(1.0)), +- ?assertEqual("-1.0", +- digits(-1.0)), +- ?assertEqual("0.1", +- digits(0.1)), +- ?assertEqual("0.01", +- digits(0.01)), +- ?assertEqual("0.001", +- digits(0.001)), +- ?assertEqual("1.0e+6", +- digits(1000000.0)), +- ?assertEqual("0.5", +- digits(0.5)), +- ?assertEqual("4503599627370496.0", +- digits(4503599627370496.0)), +- %% small denormalized number +- %% 4.94065645841246544177e-324 =:= 5.0e-324 +- <> = <<0,0,0,0,0,0,0,1>>, +- ?assertEqual("5.0e-324", +- digits(SmallDenorm)), +- ?assertEqual(SmallDenorm, +- list_to_float(digits(SmallDenorm))), +- %% large denormalized number +- %% 2.22507385850720088902e-308 +- <> = <<0,15,255,255,255,255,255,255>>, +- ?assertEqual("2.225073858507201e-308", +- digits(BigDenorm)), +- ?assertEqual(BigDenorm, +- list_to_float(digits(BigDenorm))), +- %% small normalized number +- %% 2.22507385850720138309e-308 +- <> = <<0,16,0,0,0,0,0,0>>, +- ?assertEqual("2.2250738585072014e-308", +- digits(SmallNorm)), +- ?assertEqual(SmallNorm, +- list_to_float(digits(SmallNorm))), +- %% large normalized number +- %% 1.79769313486231570815e+308 +- <> = <<127,239,255,255,255,255,255,255>>, +- ?assertEqual("1.7976931348623157e+308", +- digits(LargeNorm)), +- ?assertEqual(LargeNorm, +- list_to_float(digits(LargeNorm))), +- %% issue #10 - mochinum:frexp(math:pow(2, -1074)). +- ?assertEqual("5.0e-324", +- digits(math:pow(2, -1074))), +- ok. +- +-frexp_test() -> +- %% zero +- ?assertEqual({0.0, 0}, frexp(0.0)), +- %% one +- ?assertEqual({0.5, 1}, frexp(1.0)), +- %% negative one +- ?assertEqual({-0.5, 1}, frexp(-1.0)), +- %% small denormalized number +- %% 4.94065645841246544177e-324 +- <> = <<0,0,0,0,0,0,0,1>>, +- ?assertEqual({0.5, -1073}, frexp(SmallDenorm)), +- %% large denormalized number +- %% 2.22507385850720088902e-308 +- <> = <<0,15,255,255,255,255,255,255>>, +- ?assertEqual( +- {0.99999999999999978, -1022}, +- frexp(BigDenorm)), +- %% small normalized number +- %% 2.22507385850720138309e-308 +- <> = <<0,16,0,0,0,0,0,0>>, +- ?assertEqual({0.5, -1021}, frexp(SmallNorm)), +- %% large normalized number +- %% 1.79769313486231570815e+308 +- <> = <<127,239,255,255,255,255,255,255>>, +- ?assertEqual( +- {0.99999999999999989, 1024}, +- frexp(LargeNorm)), +- %% issue #10 - mochinum:frexp(math:pow(2, -1074)). +- ?assertEqual( +- {0.5, -1073}, +- frexp(math:pow(2, -1074))), +- ok. +- +--endif. diff --git a/rabbitmq-server/plugins-src/mochiweb-wrapper/CONTRIBUTING.md b/rabbitmq-server/plugins-src/mochiweb-wrapper/CONTRIBUTING.md new file mode 100644 index 0000000..69a4b4a --- /dev/null +++ b/rabbitmq-server/plugins-src/mochiweb-wrapper/CONTRIBUTING.md @@ -0,0 +1,51 @@ +## Overview + +RabbitMQ projects use pull requests to discuss, collaborate on and accept code contributions. +Pull requests is the primary place of discussing code changes. + +## How to Contribute + +The process is fairly standard: + + * Fork the repository or repositories you plan on contributing to + * Clone [RabbitMQ umbrella repository](https://github.com/rabbitmq/rabbitmq-public-umbrella) + * `cd umbrella`, `make co` + * Create a branch with a descriptive name in the relevant repositories + * Make your changes, run tests, commit with a [descriptive message](http://tbaggery.com/2008/04/19/a-note-about-git-commit-messages.html), push to your fork + * Submit pull requests with an explanation what has been changed and **why** + * Submit a filled out and signed [Contributor Agreement](https://github.com/rabbitmq/ca#how-to-submit) if needed (see below) + * Be patient. We will get to your pull request eventually + +If what you are going to work on is a substantial change, please first ask the core team +of their opinion on [RabbitMQ mailing list](https://groups.google.com/forum/#!forum/rabbitmq-users). + + +## (Brief) Code of Conduct + +In one line: don't be a dick. + +Be respectful to the maintainers and other contributors. Open source +contributors put long hours into developing projects and doing user +support. Those projects and user support are available for free. We +believe this deserves some respect. + +Be respectful to people of all races, genders, religious beliefs and +political views. Regardless of how brilliant a pull request is +technically, we will not tolerate disrespectful or aggressive +behaviour. + +Contributors who violate this straightforward Code of Conduct will see +their pull requests closed and locked. + + +## Contributor Agreement + +If you want to contribute a non-trivial change, please submit a signed copy of our +[Contributor Agreement](https://github.com/rabbitmq/ca#how-to-submit) around the time +you submit your pull request. This will make it much easier (in some cases, possible) +for the RabbitMQ team at Pivotal to merge your contribution. + + +## Where to Ask Questions + +If something isn't clear, feel free to ask on our [mailing list](https://groups.google.com/forum/#!forum/rabbitmq-users). diff --git a/rabbitmq-server/plugins-src/mochiweb-wrapper/LICENSE-MIT-Mochi b/rabbitmq-server/plugins-src/mochiweb-wrapper/LICENSE-MIT-Mochi new file mode 100644 index 0000000..c85b65a --- /dev/null +++ b/rabbitmq-server/plugins-src/mochiweb-wrapper/LICENSE-MIT-Mochi @@ -0,0 +1,9 @@ +This is the MIT license. + +Copyright (c) 2007 Mochi Media, Inc. + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/rabbitmq-server/plugins-src/mochiweb-wrapper/Makefile b/rabbitmq-server/plugins-src/mochiweb-wrapper/Makefile new file mode 100644 index 0000000..482105a --- /dev/null +++ b/rabbitmq-server/plugins-src/mochiweb-wrapper/Makefile @@ -0,0 +1 @@ +include ../umbrella.mk diff --git a/rabbitmq-server/plugins-src/mochiweb-wrapper/hash.mk b/rabbitmq-server/plugins-src/mochiweb-wrapper/hash.mk new file mode 100644 index 0000000..d1cebfa --- /dev/null +++ b/rabbitmq-server/plugins-src/mochiweb-wrapper/hash.mk @@ -0,0 +1 @@ +UPSTREAM_SHORT_HASH:=680dba8 diff --git a/rabbitmq-server/plugins-src/mochiweb-wrapper/license_info b/rabbitmq-server/plugins-src/mochiweb-wrapper/license_info new file mode 100644 index 0000000..c72a6af --- /dev/null +++ b/rabbitmq-server/plugins-src/mochiweb-wrapper/license_info @@ -0,0 +1,4 @@ +Mochiweb is "Copyright (c) 2007 Mochi Media, Inc." and is covered by +the MIT license. It was downloaded from +http://github.com/mochi/mochiweb/ + diff --git a/rabbitmq-server/plugins-src/mochiweb-wrapper/mochiweb-git/.done b/rabbitmq-server/plugins-src/mochiweb-wrapper/mochiweb-git/.done new file mode 100644 index 0000000..e69de29 diff --git a/rabbitmq-server/plugins-src/mochiweb-wrapper/mochiweb-git/.travis.yml b/rabbitmq-server/plugins-src/mochiweb-wrapper/mochiweb-git/.travis.yml new file mode 100644 index 0000000..43dad1a --- /dev/null +++ b/rabbitmq-server/plugins-src/mochiweb-wrapper/mochiweb-git/.travis.yml @@ -0,0 +1,7 @@ +language: erlang +notifications: + email: false +otp_release: + - R15B02 + - R15B03 + - R16B diff --git a/rabbitmq-server/plugins-src/mochiweb-wrapper/mochiweb-git/CHANGES.md b/rabbitmq-server/plugins-src/mochiweb-wrapper/mochiweb-git/CHANGES.md new file mode 100644 index 0000000..06a8b5f --- /dev/null +++ b/rabbitmq-server/plugins-src/mochiweb-wrapper/mochiweb-git/CHANGES.md @@ -0,0 +1,91 @@ +Version 2.7.0 released XXXX-XX-XX + +* `mochiweb_socket_server:stop/1` is now a synchronous + call instead of an asynchronous cast +* `mochiweb_html:parse_tokens/1` (and `parse/1`) will now create a + html element to wrap documents that have a HTML5 doctype + (``) but no html element + https://github.com/mochi/mochiweb/issues/110 + +Version 2.6.0 released 2013-04-15 + +* Enable R15B gen_tcp workaround only on R15B + https://github.com/mochi/mochiweb/pull/107 + +Version 2.5.0 released 2013-03-04 + +* Replace now() with os:timestamp() in acceptor (optimization) + https://github.com/mochi/mochiweb/pull/102 +* New mochiweb_session module for managing session cookies. + NOTE: this module is only supported on R15B02 and later! + https://github.com/mochi/mochiweb/pull/94 +* New mochiweb_base64url module for base64url encoding + (URL and Filename safe alphabet, see RFC 4648). +* Fix rebar.config in mochiwebapp_skel to use {branch, "master"} + https://github.com/mochi/mochiweb/issues/105 + +Version 2.4.2 released 2013-02-05 + +* Fixed issue in mochiweb_response introduced in v2.4.0 + https://github.com/mochi/mochiweb/pull/100 + +Version 2.4.1 released 2013-01-30 + +* Fixed issue in mochiweb_request introduced in v2.4.0 + https://github.com/mochi/mochiweb/issues/97 +* Fixed issue in mochifmt_records introduced in v2.4.0 + https://github.com/mochi/mochiweb/issues/96 + +Version 2.4.0 released 2013-01-23 + +* Switch from parameterized modules to explicit tuple module calls for + R16 compatibility (#95) +* Fix for mochiweb_acceptor crash with extra-long HTTP headers under + R15B02 (#91) +* Fix case in handling range headers (#85) +* Handle combined Content-Length header (#88) +* Windows security fix for `safe_relative_path`, any path with a + backslash on any platform is now considered unsafe (#92) + +Version 2.3.2 released 2012-07-27 + +* Case insensitive match for "Connection: close" (#81) + +Version 2.3.1 released 2012-03-31 + +* Fix edoc warnings (#63) +* Fix mochiweb_html handling of invalid charref sequences (unescaped &) (#69). +* Add a manual garbage collection between requests to avoid worst case behavior + on keep-alive sockets. +* Fix dst cookie bug (#73) +* Removed unnecessary template_dir option, see + https://github.com/basho/rebar/issues/203 + +Version 2.3.0 released 2011-10-14 + +* Handle ssl_closed message in mochiweb_http (#59) +* Added support for new MIME types (otf, eot, m4v, svg, svgz, ttc, ttf, + vcf, webm, webp, woff) (#61) +* Updated mochiweb_charref to support all HTML5 entities. Note that + if you are using this module directly, the spec has changed to return + `[integer()]` for some entities. (#64) + +Version 2.2.1 released 2011-08-31 + +* Removed `mochiweb_skel` module from the pre-rebar era + +Version 2.2.0 released 2011-08-29 + +* Added new `mochiweb_http:start_link/1` and + `mochiweb_socket_server:start_link/1` APIs to explicitly start linked + servers. Also added `{link, false}` option to the `start/1` variants + to explicitly start unlinked. This is in expectation that we will + eventually change the default behavior of `start/1` to be unlinked as you + would expect it to. See https://github.com/mochi/mochiweb/issues/58 for + discussion. + +Version 2.1.0 released 2011-08-29 + +* Added new `mochijson2:decode/2` with `{format, struct | proplist | eep18}` + options for easy decoding to various proplist formats. Also added encoding + support for eep18 style objects. diff --git a/rabbitmq-server/plugins-src/mochiweb-wrapper/mochiweb-git/LICENSE b/rabbitmq-server/plugins-src/mochiweb-wrapper/mochiweb-git/LICENSE new file mode 100644 index 0000000..c85b65a --- /dev/null +++ b/rabbitmq-server/plugins-src/mochiweb-wrapper/mochiweb-git/LICENSE @@ -0,0 +1,9 @@ +This is the MIT license. + +Copyright (c) 2007 Mochi Media, Inc. + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/rabbitmq-server/plugins-src/mochiweb-wrapper/mochiweb-git/Makefile b/rabbitmq-server/plugins-src/mochiweb-wrapper/mochiweb-git/Makefile new file mode 100644 index 0000000..9de1944 --- /dev/null +++ b/rabbitmq-server/plugins-src/mochiweb-wrapper/mochiweb-git/Makefile @@ -0,0 +1,29 @@ + +PREFIX:=../ +DEST:=$(PREFIX)$(PROJECT) + +REBAR=./rebar + +all: + @$(REBAR) get-deps compile + +edoc: + @$(REBAR) doc + +test: + @rm -rf .eunit + @mkdir -p .eunit + @$(REBAR) skip_deps=true eunit + +clean: + @$(REBAR) clean + +build_plt: + @$(REBAR) build-plt + +dialyzer: + @$(REBAR) dialyze + +app: + @$(REBAR) create template=mochiwebapp dest=$(DEST) appid=$(PROJECT) + diff --git a/rabbitmq-server/plugins-src/mochiweb-wrapper/mochiweb-git/README b/rabbitmq-server/plugins-src/mochiweb-wrapper/mochiweb-git/README new file mode 100644 index 0000000..80ee6e4 --- /dev/null +++ b/rabbitmq-server/plugins-src/mochiweb-wrapper/mochiweb-git/README @@ -0,0 +1,17 @@ +MochiWeb is an Erlang library for building lightweight HTTP servers. + +The latest version of MochiWeb is available at http://github.com/mochi/mochiweb + +The mailing list for MochiWeb is at http://groups.google.com/group/mochiweb/ + +R12B compatibility: +The master of MochiWeb is tested with R14A and later. A branch compatible +with R12B is maintained separately at http://github.com/lemenkov/mochiweb +The R12B branch of that repository is mirrored in the official repository +occasionally for convenience. + +To create a new mochiweb using project: + make app PROJECT=project_name + +To create a new mochiweb using project in a specific directory: + make app PROJECT=project_name PREFIX=$HOME/projects/ diff --git a/rabbitmq-server/plugins-src/mochiweb-wrapper/mochiweb-git/examples/hmac_api/README b/rabbitmq-server/plugins-src/mochiweb-wrapper/mochiweb-git/examples/hmac_api/README new file mode 100644 index 0000000..3771323 --- /dev/null +++ b/rabbitmq-server/plugins-src/mochiweb-wrapper/mochiweb-git/examples/hmac_api/README @@ -0,0 +1,206 @@ +Introduction +------------ + +This example shows how to make an Amazon-style HMAC authentication system for an API with mochiweb. + +Purpose +------- + +The purpose of this example is to: +* make it easy to implement an API in mochiweb + - using a proven approach so that 'amateurs' don't have to reinvent crypto +* make it easy to generate client libraries for that API so that client-side implementers can: + - reuse closely related code examples + - build compatibility unit tests instead of fiddling around debugging their library against live implementations of the system + +Scope +----- + +The scope of this document is: +* a description of the client-server exchange +* a reference implementation of + - the server-side implementation of the exchange + - the client-side implementation of the exchange +* developing a custom implementation of an API +* deploying that implementation to new client-side users to build their client libraries + +Contents +-------- + +Subsequent sections of this document are: +* the client-server exchange +* the reference implementation in this example +* building a custom implementation +* deploying a custom implementation + +The Client-Server Exchange +-------------------------- + +OVERVIEW + +This section describes the client-server exchange for an Amazon-style API authentication schema. It has the following characteristics: +* based on a public key/private key +* used to authenticate non-SSL api requests +* not a full once-use schema and is vulnerable to replay attacks within a short time window + +TYPE OF API + +The api described in this document is: +* suitable for machine-machine communication + +The api described in this document is NOT: +* an implementation of 2-legged OAUTH + - see https://github.com/tim/erlang-oauth +* an implementation of 3-legged OAUTH + +It is not suitable for use in applications where an end user has to log into a service and piggy-back on top of a keypair security system. + +THE CLIENT LIBRARY HERE IS **NOT** AN AMAZON CLIENT LIBRARY. AMAZON DOES FUNKY STUFF WITH HOSTNAMES AND PUSHES THEM ONTO THE URL IN CANONICALIZATION! THE CLIENT LIBRARY IS AMAZON-A-LIKE ENOUGH TO USE THE AMAZON DOCOS TO BUILD A TEST SUITE. + +STEP 1 + +The client is issued with a pair of keys, one public, one private, for example: +* public: "bcaa49f2a4f7d4f92ac36c8bf66d5bb6" +* private: "92bc93d6b8aaec1cde772f903e06daf5" + +In the Amazon docs these are referred to as: +* AWSAccessKeyId (public) +* AWSSecretAccessKey (private) + +These can be generated by the function: +hmac_api_lib:get_api_keypair/0 + +This function returns cryptographically strong random numbers using the openSSL crypto library under the covers. + +The public key is used as a declaration of identity, "I am bcaa49..." + +The private key is never passed over the wire and is used to construct the same hash on both the client- and the server-side. + +STEP 2 + +The client prepares their request: +* url +* time of request +* action (GET, POST, etc) +* type of request (application/json, etc) +* contents of request +* etc, etc + +These components are then turned into a string called the canonical form. + +The HTTP protocol is permissive; it treats different requests as if they were the same. For instance it doesn't care about the order in which headers are sent, and allows the same header to contain multiple values as a list or be specified multiple times as a key-value pair. + +Intermediate machines between the client and server MAY pack and repack the HTTP request as long as they don't alter its meaning in a narrow sense. This means that the format of the HTTP request is not guaranteed to be maintained. + +The canonical form simply ensures that all the valid ways of making the same request are represented by the same string - irrespective of how this is done. + +The canonical form handles POST bodies and query parameters and silently discards anchors in URL's. + +A hash of this string is made with the private key. + +STEP 3 + +The client makes the request to the server: +* the signature is included in the request in the standard HTTPAuthorization header. (As the Amazon documentation points out this is infelicitous as it is being used for Authentication not Authorization, but hey!). + +The Authorization header constructed has the form: + + +An Amazon one looks like: +Authorization: AWS 0PN5J17HBGZHT7JJ3X82:frJIUN8DYpKDtOLCwo//yllqDzg= + --- -------------------- ---------------------------- + sch public key signature + +The HTTP request is made. + +STEP 4 + +The request is processed: +* the server receives the request +* the server constructs the canonical form from the attributes of the request: + - url + - date header + - action (GET, POST, etc) + - content type of request (application/json, etc) + - some custom headers + - etc, etc +* the server takes the client's public key from the HTTPAuthorization header and looks up the client's private key +* the server signs the canonical form with the private key +* the server compares: + - the signature in the request to the signature it has just generated + - the time encoded in the request with the server time +* the request is accepted or denied + +The time comparison is 'fuzzy'. Different server's clocks will be out of sync to a degree, the request may have acquired a time from an intermediate machine along the way, etc, etc. Normally a 'clock skew' time is allowed - in Amazon's case this is 15 minutes. + +NOTA BENE: THIS CLOCK SKEW TIME ALLOWS FOR REPLAY ATTACKS WHERE A BAD GUY SIMPLY CAPTURES AND REPLAYS TRAFFIC. + +EXTENSION + +It is possible to extend this schema to prevent replay attacks. The server issues a nonce token (a random string) which is included in the signature. When the server authorizes the request it stores the token and prevents any request with that token (ie a replay) being authorized again. + +The client receives its next nonce token in the response to a successful request. + +The Reference Implementation In This Example +-------------------------------------------- + +The reference implementation used in this example is that described in the Amazon documentation here: +http://docs.amazonwebservices.com/AmazonS3/latest/dev/index.html?RESTAuthentication.html + +The try out the reference implementation: +* create a new mochiweb project as per the mochiweb README + - make app PROJECT=project_name +* copy hmac_api_lib.erl and hmac_api_client.erl into project_name/src +* copy hmac_api.hrl into project_name/include +* edit project_name_web.erl and add a call to hmac_api_lib:authorize_request/1 + +authorize/request/1 should be called in the loop of project_name_web.erl as per: + + loop(Req, DocRoot) -> + Auth = hmac_api_lib:authorize_request(Req), + io:format("Auth is ~p~n", [Auth]), + "/" ++ Path = Req:get(path), + ... + +When this is done you are ready to test the api: +* run 'make' in project_name/ to build the Erlang +* start the web server with 'start-dev.sh' in project_name/ (this will also open an Erlang shell to the Erlang VM) + +To test the api run this command in the Erlang shell: +* hmac_api_client:fire(). + +The reference implementation uses 5 constants defined in hmac_api.hrl. +* schema +* headerprefix +* dateheader +* publickey +* privatekey + +Building A Custom Implementation +-------------------------------- + +The simplest custom implementation is to simply take the existing code and change the values of the following constants: +* schema +* headerprefix +* dateheader + +If the API is to be used 'as is', please use the values which are commented out in hmac_api.hrl. This will make easier for software developers to work out which version of which client-side libraries they can use. + +Client libraries written in other languages than Erlang can reemployment the test suite in hmac_api_lib.erl. + +More sophisticated changes will involve changes to the canonicalization functions. + +Use of a generic schema should make reuse of client libraries easier across different platforms. + +If you develop an ‘as-is’ client-side library in another language please consider submitting its code to this example. + +Deploying A Custom Implementation +--------------------------------- + +When deploying a custom implementation, the server-side code should be released with unit tests so the client-side developer can easily build a robust client. + +In addition to that you will need to specify: +* description of how the API works: + - ie the acceptable methods and urls + - custom headers and their usage (if appropriate) + diff --git a/rabbitmq-server/plugins-src/mochiweb-wrapper/mochiweb-git/examples/hmac_api/hmac_api.hrl b/rabbitmq-server/plugins-src/mochiweb-wrapper/mochiweb-git/examples/hmac_api/hmac_api.hrl new file mode 100644 index 0000000..ddce280 --- /dev/null +++ b/rabbitmq-server/plugins-src/mochiweb-wrapper/mochiweb-git/examples/hmac_api/hmac_api.hrl @@ -0,0 +1,43 @@ +-author("Hypernumbers Ltd "). + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%%% %%% +%%% Reference values for testing against Amazon documents %%% +%%% %%% +%%% These need to be changed in production! %%% +%%% %%% +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +-define(schema, "AWS"). +%% defines the prefix for headers to be included in the signature +-define(headerprefix, "x-amz-"). +%% defines the date header +-define(dateheader, "x-amz-date"). + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%%% %%% +%%% Default values for defining a generic API %%% +%%% %%% +%%% Only change these if you alter the canonicalisation %%% +%%% %%% +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%%-define(schema, "MOCHIAPI"). +%%-define(headerprefix, "x-mochiapi-"). +%%-define(dateheader, "x-mochiapi-date"). + +%% a couple of keys for testing +%% these are taken from the document +%% % http://docs.amazonwebservices.com/AmazonS3/latest/dev/index.html?RESTAuthentication.html +%% they are not valid keys! +-define(publickey, "0PN5J17HBGZHT7JJ3X82"). +-define(privatekey, "uV3F3YluFJax1cknvbcGwgjvx4QpvB+leU8dUj2o"). + + +-record(hmac_signature, + { + method, + contentmd5, + contenttype, + date, + headers, + resource + }). diff --git a/rabbitmq-server/plugins-src/mochiweb-wrapper/mochiweb-git/examples/hmac_api/hmac_api_client.erl b/rabbitmq-server/plugins-src/mochiweb-wrapper/mochiweb-git/examples/hmac_api/hmac_api_client.erl new file mode 100644 index 0000000..a38fa96 --- /dev/null +++ b/rabbitmq-server/plugins-src/mochiweb-wrapper/mochiweb-git/examples/hmac_api/hmac_api_client.erl @@ -0,0 +1,34 @@ +-module(hmac_api_client). + +-export([ + fire/0 + ]). + +-include("hmac_api.hrl"). +-author("Hypernumbers Ltd "). + +fire() -> + URL = "http://127.0.0.1:8080/some/page/yeah/", + %% Dates SHOULD conform to Section 3.3 of RFC2616 + %% the examples from the RFC are: + %% Sun, 06 Nov 1994 08:49:37 GMT ; RFC 822, updated by RFC 1123 + %% Sunday, 06-Nov-94 08:49:37 GMT ; RFC 850, obsoleted by RFC 1036 + %% Sun Nov 6 08:49:37 1994 ; ANSI C's asctime() format + + %% Dates can be conveniently generated using dh_date.erl + %% https://github.com/daleharvey/dh_date + %% which is largely compatible with + %% http://uk.php.net/date + + %% You MIGHT find it convenient to insist on times in UTC only + %% as it reduces the errors caused by summer time and other + %% conversion issues + Method = post, + Headers = [{"content-type", "application/json"}, + {"date", "Sun, 10 Jul 2011 05:07:19"}], + ContentType = "application/json", + Body = "blah", + HTTPAuthHeader = hmac_api_lib:sign(?privatekey, Method, URL, + Headers, ContentType), + httpc:request(Method, {URL, [HTTPAuthHeader | Headers], + ContentType, Body}, [], []). diff --git a/rabbitmq-server/plugins-src/mochiweb-wrapper/mochiweb-git/examples/hmac_api/hmac_api_lib.erl b/rabbitmq-server/plugins-src/mochiweb-wrapper/mochiweb-git/examples/hmac_api/hmac_api_lib.erl new file mode 100644 index 0000000..6d04954 --- /dev/null +++ b/rabbitmq-server/plugins-src/mochiweb-wrapper/mochiweb-git/examples/hmac_api/hmac_api_lib.erl @@ -0,0 +1,435 @@ +-module(hmac_api_lib). + +-include("hmac_api.hrl"). +-include_lib("eunit/include/eunit.hrl"). + +-author("Hypernumbers Ltd "). + +%%% this library supports the hmac_sha api on both the client-side +%%% AND the server-side +%%% +%%% sign/5 is used client-side to sign a request +%%% - it returns an HTTPAuthorization header +%%% +%%% authorize_request/1 takes a mochiweb Request as an arguement +%%% and checks that the request matches the signature +%%% +%%% get_api_keypair/0 creates a pair of public/private keys +%%% +%%% THIS LIB DOESN'T IMPLEMENT THE AMAZON API IT ONLY IMPLEMENTS +%%% ENOUGH OF IT TO GENERATE A TEST SUITE. +%%% +%%% THE AMAZON API MUNGES HOSTNAME AND PATHS IN A CUSTOM WAY +%%% THIS IMPLEMENTATION DOESN'T +-export([ + authorize_request/1, + sign/5, + get_api_keypair/0 + ]). + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%%% %%% +%%% API %%% +%%% %%% +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + +authorize_request(Req) -> + Method = Req:get(method), + Path = Req:get(path), + Headers = normalise(mochiweb_headers:to_list(Req:get(headers))), + ContentMD5 = get_header(Headers, "content-md5"), + ContentType = get_header(Headers, "content-type"), + Date = get_header(Headers, "date"), + IncAuth = get_header(Headers, "authorization"), + {_Schema, _PublicKey, _Sig} = breakout(IncAuth), + %% normally you would use the public key to look up the private key + PrivateKey = ?privatekey, + Signature = #hmac_signature{method = Method, + contentmd5 = ContentMD5, + contenttype = ContentType, + date = Date, + headers = Headers, + resource = Path}, + Signed = sign_data(PrivateKey, Signature), + {_, AuthHeader} = make_HTTPAuth_header(Signed), + case AuthHeader of + IncAuth -> "match"; + _ -> "no_match" + end. + +sign(PrivateKey, Method, URL, Headers, ContentType) -> + Headers2 = normalise(Headers), + ContentMD5 = get_header(Headers2, "content-md5"), + Date = get_header(Headers2, "date"), + Signature = #hmac_signature{method = Method, + contentmd5 = ContentMD5, + contenttype = ContentType, + date = Date, + headers = Headers, + resource = URL}, + SignedSig = sign_data(PrivateKey, Signature), + make_HTTPAuth_header(SignedSig). + + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%%% %%% +%%% Internal Functions %%% +%%% %%% +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + +breakout(Header) -> + [Schema, Tail] = string:tokens(Header, " "), + [PublicKey, Signature] = string:tokens(Tail, ":"), + {Schema, PublicKey, Signature}. + +get_api_keypair() -> + Public = mochihex:to_hex(binary_to_list(crypto:strong_rand_bytes(16))), + Private = mochihex:to_hex(binary_to_list(crypto:strong_rand_bytes(16))), + {Public, Private}. + +make_HTTPAuth_header(Signature) -> + {"Authorization", ?schema ++ " " + ++ ?publickey ++ ":" ++ Signature}. + +make_signature_string(#hmac_signature{} = S) -> + Date = get_date(S#hmac_signature.headers, S#hmac_signature.date), + string:to_upper(atom_to_list(S#hmac_signature.method)) ++ "\n" + ++ S#hmac_signature.contentmd5 ++ "\n" + ++ S#hmac_signature.contenttype ++ "\n" + ++ Date ++ "\n" + ++ canonicalise_headers(S#hmac_signature.headers) + ++ canonicalise_resource(S#hmac_signature.resource). + +sign_data(PrivateKey, #hmac_signature{} = Signature) -> + Str = make_signature_string(Signature), + sign2(PrivateKey, Str). + +%% this fn is the entry point for a unit test which is why it is broken out... +%% if yer encryption and utf8 and base45 doo-dahs don't work then +%% yer Donald is well and truly Ducked so ye may as weel test it... +sign2(PrivateKey, Str) -> + Sign = xmerl_ucs:to_utf8(Str), + binary_to_list(base64:encode(crypto:sha_mac(PrivateKey, Sign))). + +canonicalise_headers([]) -> "\n"; +canonicalise_headers(List) when is_list(List) -> + List2 = [{string:to_lower(K), V} || {K, V} <- lists:sort(List)], + c_headers2(consolidate(List2, []), []). + +c_headers2([], Acc) -> string:join(Acc, "\n") ++ "\n"; +c_headers2([{?headerprefix ++ Rest, Key} | T], Acc) -> + Hd = string:strip(?headerprefix ++ Rest) ++ ":" ++ string:strip(Key), + c_headers2(T, [Hd | Acc]); +c_headers2([_H | T], Acc) -> c_headers2(T, Acc). + +consolidate([H | []], Acc) -> [H | Acc]; +consolidate([{H, K1}, {H, K2} | Rest], Acc) -> + consolidate([{H, join(K1, K2)} | Rest], Acc); +consolidate([{H1, K1}, {H2, K2} | Rest], Acc) -> + consolidate([{rectify(H2), rectify(K2)} | Rest], [{H1, K1} | Acc]). + +join(A, B) -> string:strip(A) ++ ";" ++ string:strip(B). + +%% removes line spacing as per RFC 2616 Section 4.2 +rectify(String) -> + Re = "[\x20* | \t*]+", + re:replace(String, Re, " ", [{return, list}, global]). + +canonicalise_resource("http://" ++ Rest) -> c_res2(Rest); +canonicalise_resource("https://" ++ Rest) -> c_res2(Rest); +canonicalise_resource(X) -> c_res3(X). + +c_res2(Rest) -> + N = string:str(Rest, "/"), + {_, Tail} = lists:split(N, Rest), + c_res3("/" ++ Tail). + +c_res3(Tail) -> + URL = case string:str(Tail, "#") of + 0 -> Tail; + N -> {U, _Anchor} = lists:split(N, Tail), + U + end, + U3 = case string:str(URL, "?") of + 0 -> URL; + N2 -> {U2, Q} = lists:split(N2, URL), + U2 ++ canonicalise_query(Q) + end, + string:to_lower(U3). + +canonicalise_query(List) -> + List1 = string:to_lower(List), + List2 = string:tokens(List1, "&"), + string:join(lists:sort(List2), "&"). + +%% if there's a header date take it and ditch the date +get_date([], Date) -> Date; +get_date([{K, _V} | T], Date) -> case string:to_lower(K) of + ?dateheader -> []; + _ -> get_date(T, Date) + end. + +normalise(List) -> norm2(List, []). + +norm2([], Acc) -> Acc; +norm2([{K, V} | T], Acc) when is_atom(K) -> + norm2(T, [{string:to_lower(atom_to_list(K)), V} | Acc]); +norm2([H | T], Acc) -> norm2(T, [H | Acc]). + +get_header(Headers, Type) -> + case lists:keyfind(Type, 1, Headers) of + false -> []; + {_K, V} -> V + end. + + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%%% %%% +%%% Unit Tests %%% +%%% %%% +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + + % taken from Amazon docs +%% http://docs.amazonwebservices.com/AmazonS3/latest/dev/index.html?RESTAuthentication.html +hash_test1(_) -> + Sig = "DELETE\n\n\n\nx-amz-date:Tue, 27 Mar 2007 21:20:26 +0000\n/johnsmith/photos/puppy.jpg", + Key = ?privatekey, + Hash = sign2(Key, Sig), + Expected = "k3nL7gH3+PadhTEVn5Ip83xlYzk=", + ?assertEqual(Expected, Hash). + +%% taken from Amazon docs +%% http://docs.amazonwebservices.com/AmazonS3/latest/dev/index.html?RESTAuthentication.html +hash_test2(_) -> + Sig = "GET\n\n\nTue, 27 Mar 2007 19:44:46 +0000\n/johnsmith/?acl", + Key = "uV3F3YluFJax1cknvbcGwgjvx4QpvB+leU8dUj2o", + Hash = sign2(Key, Sig), + Expected = "thdUi9VAkzhkniLj96JIrOPGi0g=", + ?assertEqual(Expected, Hash). + +%% taken from Amazon docs +%% http://docs.amazonwebservices.com/AmazonS3/latest/dev/index.html?RESTAuthentication.html +hash_test3(_) -> + Sig = "GET\n\n\nWed, 28 Mar 2007 01:49:49 +0000\n/dictionary/" + ++ "fran%C3%A7ais/pr%c3%a9f%c3%a8re", + Key = "uV3F3YluFJax1cknvbcGwgjvx4QpvB+leU8dUj2o", + Hash = sign2(Key, Sig), + Expected = "dxhSBHoI6eVSPcXJqEghlUzZMnY=", + ?assertEqual(Expected, Hash). + +signature_test1(_) -> + URL = "http://example.com:90/tongs/ya/bas", + Method = post, + ContentMD5 = "", + ContentType = "", + Date = "Sun, 10 Jul 2011 05:07:19 UTC", + Headers = [], + Signature = #hmac_signature{method = Method, + contentmd5 = ContentMD5, + contenttype = ContentType, + date = Date, + headers = Headers, + resource = URL}, + Sig = make_signature_string(Signature), + Expected = "POST\n\n\nSun, 10 Jul 2011 05:07:19 UTC\n\n/tongs/ya/bas", + ?assertEqual(Expected, Sig). + +signature_test2(_) -> + URL = "http://example.com:90/tongs/ya/bas", + Method = get, + ContentMD5 = "", + ContentType = "", + Date = "Sun, 10 Jul 2011 05:07:19 UTC", + Headers = [{"x-amz-acl", "public-read"}], + Signature = #hmac_signature{method = Method, + contentmd5 = ContentMD5, + contenttype = ContentType, + date = Date, + headers = Headers, + resource = URL}, + Sig = make_signature_string(Signature), + Expected = "GET\n\n\nSun, 10 Jul 2011 05:07:19 UTC\nx-amz-acl:public-read\n/tongs/ya/bas", + ?assertEqual(Expected, Sig). + +signature_test3(_) -> + URL = "http://example.com:90/tongs/ya/bas", + Method = get, + ContentMD5 = "", + ContentType = "", + Date = "Sun, 10 Jul 2011 05:07:19 UTC", + Headers = [{"x-amz-acl", "public-read"}, + {"yantze", "blast-off"}, + {"x-amz-doobie", "bongwater"}, + {"x-amz-acl", "public-write"}], + Signature = #hmac_signature{method = Method, + contentmd5 = ContentMD5, + contenttype = ContentType, + date = Date, + headers = Headers, + resource = URL}, + Sig = make_signature_string(Signature), + Expected = "GET\n\n\nSun, 10 Jul 2011 05:07:19 UTC\nx-amz-acl:public-read;public-write\nx-amz-doobie:bongwater\n/tongs/ya/bas", + ?assertEqual(Expected, Sig). + +signature_test4(_) -> + URL = "http://example.com:90/tongs/ya/bas", + Method = get, + ContentMD5 = "", + ContentType = "", + Date = "Sun, 10 Jul 2011 05:07:19 UTC", + Headers = [{"x-amz-acl", "public-read"}, + {"yantze", "blast-off"}, + {"x-amz-doobie oobie \t boobie ", "bongwater"}, + {"x-amz-acl", "public-write"}], + Signature = #hmac_signature{method = Method, + contentmd5 = ContentMD5, + contenttype = ContentType, + date = Date, + headers = Headers, + resource = URL}, + Sig = make_signature_string(Signature), + Expected = "GET\n\n\nSun, 10 Jul 2011 05:07:19 UTC\nx-amz-acl:public-read;public-write\nx-amz-doobie oobie boobie:bongwater\n/tongs/ya/bas", + ?assertEqual(Expected, Sig). + +signature_test5(_) -> + URL = "http://example.com:90/tongs/ya/bas", + Method = get, + ContentMD5 = "", + ContentType = "", + Date = "Sun, 10 Jul 2011 05:07:19 UTC", + Headers = [{"x-amz-acl", "public-Read"}, + {"yantze", "Blast-Off"}, + {"x-amz-doobie Oobie \t boobie ", "bongwater"}, + {"x-amz-acl", "public-write"}], + Signature = #hmac_signature{method = Method, + contentmd5 = ContentMD5, + contenttype = ContentType, + date = Date, + headers = Headers, + resource = URL}, + Sig = make_signature_string(Signature), + Expected = "GET\n\n\nSun, 10 Jul 2011 05:07:19 UTC\nx-amz-acl:public-Read;public-write\nx-amz-doobie oobie boobie:bongwater\n/tongs/ya/bas", + ?assertEqual(Expected, Sig). + +signature_test6(_) -> + URL = "http://example.com:90/tongs/ya/bas/?andy&zbish=bash&bosh=burp", + Method = get, + ContentMD5 = "", + ContentType = "", + Date = "Sun, 10 Jul 2011 05:07:19 UTC", + Headers = [], + Signature = #hmac_signature{method = Method, + contentmd5 = ContentMD5, + contenttype = ContentType, + date = Date, + headers = Headers, + resource = URL}, + Sig = make_signature_string(Signature), + Expected = "GET\n\n\nSun, 10 Jul 2011 05:07:19 UTC\n\n" + ++ "/tongs/ya/bas/?andy&bosh=burp&zbish=bash", + ?assertEqual(Expected, Sig). + +signature_test7(_) -> + URL = "http://exAMPLE.Com:90/tONgs/ya/bas/?ANdy&ZBish=Bash&bOsh=burp", + Method = get, + ContentMD5 = "", + ContentType = "", + Date = "Sun, 10 Jul 2011 05:07:19 UTC", + Headers = [], + Signature = #hmac_signature{method = Method, + contentmd5 = ContentMD5, + contenttype = ContentType, + date = Date, + headers = Headers, + resource = URL}, + Sig = make_signature_string(Signature), + Expected = "GET\n\n\nSun, 10 Jul 2011 05:07:19 UTC\n\n" + ++"/tongs/ya/bas/?andy&bosh=burp&zbish=bash", + ?assertEqual(Expected, Sig). + +signature_test8(_) -> + URL = "http://exAMPLE.Com:90/tONgs/ya/bas/?ANdy&ZBish=Bash&bOsh=burp", + Method = get, + ContentMD5 = "", + ContentType = "", + Date = "", + Headers = [{"x-aMz-daTe", "Tue, 27 Mar 2007 21:20:26 +0000"}], + Signature = #hmac_signature{method = Method, + contentmd5 = ContentMD5, + contenttype = ContentType, + date = Date, + headers = Headers, + resource = URL}, + Sig = make_signature_string(Signature), + Expected = "GET\n\n\n\n" + ++"x-amz-date:Tue, 27 Mar 2007 21:20:26 +0000\n" + ++"/tongs/ya/bas/?andy&bosh=burp&zbish=bash", + ?assertEqual(Expected, Sig). + +signature_test9(_) -> + URL = "http://exAMPLE.Com:90/tONgs/ya/bas/?ANdy&ZBish=Bash&bOsh=burp", + Method = get, + ContentMD5 = "", + ContentType = "", + Date = "Sun, 10 Jul 2011 05:07:19 UTC", + Headers = [{"x-amz-date", "Tue, 27 Mar 2007 21:20:26 +0000"}], + Signature = #hmac_signature{method = Method, + contentmd5 = ContentMD5, + contenttype = ContentType, + date = Date, + headers = Headers, + resource = URL}, + Sig = make_signature_string(Signature), + Expected = "GET\n\n\n\n" + ++"x-amz-date:Tue, 27 Mar 2007 21:20:26 +0000\n" + ++"/tongs/ya/bas/?andy&bosh=burp&zbish=bash", + ?assertEqual(Expected, Sig). + +amazon_test1(_) -> + URL = "http://exAMPLE.Com:90/johnsmith/photos/puppy.jpg", + Method = delete, + ContentMD5 = "", + ContentType = "", + Date = "", + Headers = [{"x-amz-date", "Tue, 27 Mar 2007 21:20:26 +0000"}], + Signature = #hmac_signature{method = Method, + contentmd5 = ContentMD5, + contenttype = ContentType, + date = Date, + headers = Headers, + resource = URL}, + Sig = sign_data(?privatekey, Signature), + Expected = "k3nL7gH3+PadhTEVn5Ip83xlYzk=", + ?assertEqual(Expected, Sig). + +unit_test_() -> + Setup = fun() -> ok end, + Cleanup = fun(_) -> ok end, + + Series1 = [ + fun hash_test1/1, + fun hash_test2/1, + fun hash_test3/1 + ], + + Series2 = [ + fun signature_test1/1, + fun signature_test2/1, + fun signature_test3/1, + fun signature_test4/1, + fun signature_test5/1, + fun signature_test6/1, + fun signature_test7/1, + fun signature_test8/1, + fun signature_test9/1 + ], + + Series3 = [ + fun amazon_test1/1 + ], + + {setup, Setup, Cleanup, [ + {with, [], Series1}, + {with, [], Series2}, + {with, [], Series3} + ]}. diff --git a/rabbitmq-server/plugins-src/mochiweb-wrapper/mochiweb-git/examples/https/https_store.erl b/rabbitmq-server/plugins-src/mochiweb-wrapper/mochiweb-git/examples/https/https_store.erl new file mode 100644 index 0000000..959cc00 --- /dev/null +++ b/rabbitmq-server/plugins-src/mochiweb-wrapper/mochiweb-git/examples/https/https_store.erl @@ -0,0 +1,146 @@ + +%% Trivial web storage app. It's available over both HTTP (port 8442) +%% and HTTPS (port 8443). You use a PUT to store items, a GET to +%% retrieve them and DELETE to delete them. The HTTP POST method is +%% invalid for this application. Example (using HTTPS transport): +%% +%% $ curl -k --verbose https://localhost:8443/flintstones +%% ... +%% 404 Not Found +%% ... +%% $ echo -e "Fred\nWilma\nBarney" | +%% curl -k --verbose https://localhost:8443/flintstones \ +%% -X PUT -H "Content-Type: text/plain" --data-binary @- +%% ... +%% 201 Created +%% ... +%% $ curl -k --verbose https://localhost:8443/flintstones +%% ... +%% Fred +%% Wilma +%% Barney +%% ... +%% $ curl -k --verbose https://localhost:8443/flintstones -X DELETE +%% ... +%% 200 OK +%% ... +%% $ curl -k --verbose https://localhost:8443/flintstones +%% ... +%% 404 Not Found +%% ... +%% +%% All submitted data is stored in memory (in an ets table). Could be +%% useful for ad-hoc testing. + +-module(https_store). + +-export([start/0, + stop/0, + dispatch/1, + loop/1 + ]). + +-define(HTTP_OPTS, [ + {loop, {?MODULE, dispatch}}, + {port, 8442}, + {name, http_8442} + ]). + +-define(HTTPS_OPTS, [ + {loop, {?MODULE, dispatch}}, + {port, 8443}, + {name, https_8443}, + {ssl, true}, + {ssl_opts, [ + {certfile, "server_cert.pem"}, + {keyfile, "server_key.pem"}]} + ]). + +-record(sd, {http, https}). +-record(resource, {type, data}). + +start() -> + {ok, Http} = mochiweb_http:start(?HTTP_OPTS), + {ok, Https} = mochiweb_http:start(?HTTPS_OPTS), + SD = #sd{http=Http, https=Https}, + Pid = spawn_link(fun() -> + ets:new(?MODULE, [named_table]), + loop(SD) + end), + register(http_store, Pid), + ok. + +stop() -> + http_store ! stop, + ok. + +dispatch(Req) -> + case Req:get(method) of + 'GET' -> + get_resource(Req); + 'PUT' -> + put_resource(Req); + 'DELETE' -> + delete_resource(Req); + _ -> + Headers = [{"Allow", "GET,PUT,DELETE"}], + Req:respond({405, Headers, "405 Method Not Allowed\r\n"}) + end. + +get_resource(Req) -> + Path = Req:get(path), + case ets:lookup(?MODULE, Path) of + [{Path, #resource{type=Type, data=Data}}] -> + Req:ok({Type, Data}); + [] -> + Req:respond({404, [], "404 Not Found\r\n"}) + end. + +put_resource(Req) -> + ContentType = case Req:get_header_value("Content-Type") of + undefined -> + "application/octet-stream"; + S -> + S + end, + Resource = #resource{type=ContentType, data=Req:recv_body()}, + http_store ! {self(), {put, Req:get(path), Resource}}, + Pid = whereis(http_store), + receive + {Pid, created} -> + Req:respond({201, [], "201 Created\r\n"}); + {Pid, updated} -> + Req:respond({200, [], "200 OK\r\n"}) + end. + +delete_resource(Req) -> + http_store ! {self(), {delete, Req:get(path)}}, + Pid = whereis(http_store), + receive + {Pid, ok} -> + Req:respond({200, [], "200 OK\r\n"}) + end. + +loop(#sd{http=Http, https=Https} = SD) -> + receive + stop -> + ok = mochiweb_http:stop(Http), + ok = mochiweb_http:stop(Https), + exit(normal); + {From, {put, Key, Val}} -> + Exists = ets:member(?MODULE, Key), + ets:insert(?MODULE, {Key, Val}), + case Exists of + true -> + From ! {self(), updated}; + false -> + From ! {self(), created} + end; + {From, {delete, Key}} -> + ets:delete(?MODULE, Key), + From ! {self(), ok}; + _ -> + ignore + end, + ?MODULE:loop(SD). + diff --git a/rabbitmq-server/plugins-src/mochiweb-wrapper/mochiweb-git/examples/https/server_cert.pem b/rabbitmq-server/plugins-src/mochiweb-wrapper/mochiweb-git/examples/https/server_cert.pem new file mode 100644 index 0000000..f84ccca --- /dev/null +++ b/rabbitmq-server/plugins-src/mochiweb-wrapper/mochiweb-git/examples/https/server_cert.pem @@ -0,0 +1,19 @@ +-----BEGIN CERTIFICATE----- +MIIDIDCCAgigAwIBAgIJAJLkNZzERPIUMA0GCSqGSIb3DQEBBQUAMBQxEjAQBgNV +BAMTCWxvY2FsaG9zdDAeFw0xMDAzMTgxOTM5MThaFw0yMDAzMTUxOTM5MThaMBQx +EjAQBgNVBAMTCWxvY2FsaG9zdDCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoC +ggEBAJeUCOZxbmtngF4S5lXckjSDLc+8C+XjMBYBPyy5eKdJY20AQ1s9/hhp3ulI +8pAvl+xVo4wQ+iBSvOzcy248Q+Xi6+zjceF7UNRgoYPgtJjKhdwcHV3mvFFrS/fp +9ggoAChaJQWDO1OCfUgTWXImhkw+vcDR11OVMAJ/h73dqzJPI9mfq44PTTHfYtgr +v4LAQAOlhXIAa2B+a6PlF6sqDqJaW5jLTcERjsBwnRhUGi7JevQzkejujX/vdA+N +jRBjKH/KLU5h3Q7wUchvIez0PXWVTCnZjpA9aR4m7YV05nKQfxtGd71czYDYk+j8 +hd005jetT4ir7JkAWValBybJVksCAwEAAaN1MHMwHQYDVR0OBBYEFJl9s51SnjJt +V/wgKWqV5Q6jnv1ZMEQGA1UdIwQ9MDuAFJl9s51SnjJtV/wgKWqV5Q6jnv1ZoRik +FjAUMRIwEAYDVQQDEwlsb2NhbGhvc3SCCQCS5DWcxETyFDAMBgNVHRMEBTADAQH/ +MA0GCSqGSIb3DQEBBQUAA4IBAQB2ldLeLCc+lxK5i0EZquLamMBJwDIjGpT0JMP9 +b4XQOK2JABIu54BQIZhwcjk3FDJz/uOW5vm8k1kYni8FCjNZAaRZzCUfiUYTbTKL +Rq9LuIAODyP2dnTqyKaQOOJHvrx9MRZ3XVecXPS0Tib4aO57vCaAbIkmhtYpTWmw +e3t8CAIDVtgvjR6Se0a1JA4LktR7hBu22tDImvCSJn1nVAaHpani6iPBPPdMuMsP +TBoeQfj8VpqBUjCStqJGa8ytjDFX73YaxV2mgrtGwPNme1x3YNRR11yTu7tksyMO +GrmgxNriqYRchBhNEf72AKF0LR1ByKwfbDB9rIsV00HtCgOp +-----END CERTIFICATE----- diff --git a/rabbitmq-server/plugins-src/mochiweb-wrapper/mochiweb-git/examples/https/server_key.pem b/rabbitmq-server/plugins-src/mochiweb-wrapper/mochiweb-git/examples/https/server_key.pem new file mode 100644 index 0000000..69bbf82 --- /dev/null +++ b/rabbitmq-server/plugins-src/mochiweb-wrapper/mochiweb-git/examples/https/server_key.pem @@ -0,0 +1,27 @@ +-----BEGIN RSA PRIVATE KEY----- +MIIEpAIBAAKCAQEAl5QI5nFua2eAXhLmVdySNIMtz7wL5eMwFgE/LLl4p0ljbQBD +Wz3+GGne6UjykC+X7FWjjBD6IFK87NzLbjxD5eLr7ONx4XtQ1GChg+C0mMqF3Bwd +Xea8UWtL9+n2CCgAKFolBYM7U4J9SBNZciaGTD69wNHXU5UwAn+Hvd2rMk8j2Z+r +jg9NMd9i2Cu/gsBAA6WFcgBrYH5ro+UXqyoOolpbmMtNwRGOwHCdGFQaLsl69DOR +6O6Nf+90D42NEGMof8otTmHdDvBRyG8h7PQ9dZVMKdmOkD1pHibthXTmcpB/G0Z3 +vVzNgNiT6PyF3TTmN61PiKvsmQBZVqUHJslWSwIDAQABAoIBACI8Ky5xHDFh9RpK +Rn/KC7OUlTpADKflgizWJ0Cgu2F9L9mkn5HyFHvLHa+u7CootbWJOiEejH/UcBtH +WyMQtX0snYCpdkUpJv5wvMoebGu+AjHOn8tfm9T/2O6rhwgckLyMb6QpGbMo28b1 +p9QiY17BJPZx7qJQJcHKsAvwDwSThlb7MFmWf42LYWlzybpeYQvwpd+UY4I0WXLu +/dqJIS9Npq+5Y5vbo2kAEAssb2hSCvhCfHmwFdKmBzlvgOn4qxgZ1iHQgfKI6Z3Y +J0573ZgOVTuacn+lewtdg5AaHFcl/zIYEr9SNqRoPNGbPliuv6k6N2EYcufWL5lR +sCmmmHECgYEAxm+7OpepGr++K3+O1e1MUhD7vSPkKJrCzNtUxbOi2NWj3FFUSPRU +adWhuxvUnZgTcgM1+KuQ0fB2VmxXe9IDcrSFS7PKFGtd2kMs/5mBw4UgDZkOQh+q +kDiBEV3HYYJWRq0w3NQ/9Iy1jxxdENHtGmG9aqamHxNtuO608wGW2S8CgYEAw4yG +ZyAic0Q/U9V2OHI0MLxLCzuQz17C2wRT1+hBywNZuil5YeTuIt2I46jro6mJmWI2 +fH4S/geSZzg2RNOIZ28+aK79ab2jWBmMnvFCvaru+odAuser4N9pfAlHZvY0pT+S +1zYX3f44ygiio+oosabLC5nWI0zB2gG8pwaJlaUCgYEAgr7poRB+ZlaCCY0RYtjo +mYYBKD02vp5BzdKSB3V1zeLuBWM84pjB6b3Nw0fyDig+X7fH3uHEGN+USRs3hSj6 +BqD01s1OT6fyfbYXNw5A1r+nP+5h26Wbr0zblcKxdQj4qbbBZC8hOJNhqTqqA0Qe +MmzF7jiBaiZV/Cyj4x1f9BcCgYEAhjL6SeuTuOctTqs/5pz5lDikh6DpUGcH8qaV +o6aRAHHcMhYkZzpk8yh1uUdD7516APmVyvn6rrsjjhLVq4ZAJjwB6HWvE9JBN0TR +bILF+sREHUqU8Zn2Ku0nxyfXCKIOnxlx/J/y4TaGYqBqfXNFWiXNUrjQbIlQv/xR +K48g/MECgYBZdQlYbMSDmfPCC5cxkdjrkmAl0EgV051PWAi4wR+hLxIMRjHBvAk7 +IweobkFvT4TICulgroLkYcSa5eOZGxB/DHqcQCbWj3reFV0VpzmTDoFKG54sqBRl +vVntGt0pfA40fF17VoS7riAdHF53ippTtsovHEsg5tq5NrBl5uKm2g== +-----END RSA PRIVATE KEY----- diff --git a/rabbitmq-server/plugins-src/mochiweb-wrapper/mochiweb-git/examples/keepalive/keepalive.erl b/rabbitmq-server/plugins-src/mochiweb-wrapper/mochiweb-git/examples/keepalive/keepalive.erl new file mode 100644 index 0000000..965a17e --- /dev/null +++ b/rabbitmq-server/plugins-src/mochiweb-wrapper/mochiweb-git/examples/keepalive/keepalive.erl @@ -0,0 +1,81 @@ +-module(keepalive). + +%% your web app can push data to clients using a technique called comet long +%% polling. browsers make a request and your server waits to send a +%% response until data is available. see wikipedia for a better explanation: +%% http://en.wikipedia.org/wiki/Comet_(programming)#Ajax_with_long_polling +%% +%% since the majority of your http handlers will be idle at any given moment, +%% you might consider making them hibernate while they wait for more data from +%% another process. however, since the execution stack is discarded when a +%% process hibernates, the handler would usually terminate after your response +%% code runs. this means http keep alives wouldn't work; the handler process +%% would terminate after each response and close its socket rather than +%% returning to the big @mochiweb_http@ loop and processing another request. +%% +%% however, if mochiweb exposes a continuation that encapsulates the return to +%% the top of the big loop in @mochiweb_http@, we can call that after the +%% response. if you do that then control flow returns to the proper place, +%% and keep alives work like they would if you hadn't hibernated. + +-export([ start/1, loop/1 + ]). + +%% internal export (so hibernate can reach it) +-export([ resume/3 + ]). + +-define(LOOP, {?MODULE, loop}). + +start(Options = [{port, _Port}]) -> + mochiweb_http:start([{name, ?MODULE}, {loop, ?LOOP} | Options]). + +loop(Req) -> + Path = Req:get(path), + case string:tokens(Path, "/") of + ["longpoll" | RestOfPath] -> + %% the "reentry" is a continuation -- what @mochiweb_http@ + %% needs to do to start its loop back at the top + Reentry = mochiweb_http:reentry(?LOOP), + + %% here we could send a message to some other process and hope + %% to get an interesting message back after a while. for + %% simplicity let's just send ourselves a message after a few + %% seconds + erlang:send_after(2000, self(), "honk honk"), + + %% since we expect to wait for a long time before getting a + %% reply, let's hibernate. memory usage will be minimized, so + %% we won't be wasting memory just sitting in a @receive@ + proc_lib:hibernate(?MODULE, resume, [Req, RestOfPath, Reentry]), + + %% we'll never reach this point, and this function @loop/1@ + %% won't ever return control to @mochiweb_http@. luckily + %% @resume/3@ will take care of that. + io:format("not gonna happen~n", []); + + _ -> + ok(Req, io_lib:format("some other page: ~p", [Path])) + end, + + io:format("restarting loop normally in ~p~n", [Path]), + ok. + +%% this is the function that's called when a message arrives. +resume(Req, RestOfPath, Reentry) -> + receive + Msg -> + Text = io_lib:format("wake up message: ~p~nrest of path: ~p", [Msg, RestOfPath]), + ok(Req, Text) + end, + + %% if we didn't call @Reentry@ here then the function would finish and the + %% process would exit. calling @Reentry@ takes care of returning control + %% to @mochiweb_http@ + io:format("reentering loop via continuation in ~p~n", [Req:get(path)]), + Reentry(Req). + +ok(Req, Response) -> + Req:ok({_ContentType = "text/plain", + _Headers = [], + Response}). diff --git a/rabbitmq-server/plugins-src/mochiweb-wrapper/mochiweb-git/include/internal.hrl b/rabbitmq-server/plugins-src/mochiweb-wrapper/mochiweb-git/include/internal.hrl new file mode 100644 index 0000000..6db899a --- /dev/null +++ b/rabbitmq-server/plugins-src/mochiweb-wrapper/mochiweb-git/include/internal.hrl @@ -0,0 +1,3 @@ + +-define(RECBUF_SIZE, 8192). + diff --git a/rabbitmq-server/plugins-src/mochiweb-wrapper/mochiweb-git/rebar b/rabbitmq-server/plugins-src/mochiweb-wrapper/mochiweb-git/rebar new file mode 100755 index 0000000..8082f14 Binary files /dev/null and b/rabbitmq-server/plugins-src/mochiweb-wrapper/mochiweb-git/rebar differ diff --git a/rabbitmq-server/plugins-src/mochiweb-wrapper/mochiweb-git/rebar.config b/rabbitmq-server/plugins-src/mochiweb-wrapper/mochiweb-git/rebar.config new file mode 100644 index 0000000..101930a --- /dev/null +++ b/rabbitmq-server/plugins-src/mochiweb-wrapper/mochiweb-git/rebar.config @@ -0,0 +1,16 @@ +% -*- mode: erlang -*- +{erl_opts, [debug_info, + {platform_define, "R15", 'gen_tcp_r15b_workaround'}]}. +{cover_enabled, true}. +{eunit_opts, [verbose, {report,{eunit_surefire,[{dir,"."}]}}]}. +{dialyzer_opts, [{warnings, [no_return, + no_unused, + no_improper_lists, + no_fun_app, + no_match, + no_opaque, + no_fail_call, + error_handling, + race_conditions, + behaviours, + unmatched_returns]}]}. diff --git a/rabbitmq-server/plugins-src/mochiweb-wrapper/mochiweb-git/scripts/entities.erl b/rabbitmq-server/plugins-src/mochiweb-wrapper/mochiweb-git/scripts/entities.erl new file mode 100755 index 0000000..e329ec7 --- /dev/null +++ b/rabbitmq-server/plugins-src/mochiweb-wrapper/mochiweb-git/scripts/entities.erl @@ -0,0 +1,45 @@ +#!/usr/bin/env escript +%% -*- mode: erlang -*- +-export([main/1]). + +%% @doc Script used to generate mochiweb_charref.erl table. + +main(_) -> + application:start(inets), + code:add_patha("ebin"), + {ok, {_, _, HTML}} = httpc:request("http://www.w3.org/TR/html5/named-character-references.html"), + print(lists:sort(search(mochiweb_html:parse(HTML)))). + +print([F | T]) -> + io:put_chars([clause(F), ";\n"]), + print(T); +print([]) -> + io:put_chars(["entity(_) -> undefined.\n"]), + ok. + +clause({Title, [Codepoint]}) -> + ["entity(\"", Title, "\") -> 16#", Codepoint]; +clause({Title, [First | Rest]}) -> + ["entity(\"", Title, "\") -> [16#", First, + [[", 16#", Codepoint] || Codepoint <- Rest], + "]"]. + + +search(Elem) -> + search(Elem, []). + +search({<<"tr">>, [{<<"id">>, <<"entity-", _/binary>>} | _], Children}, Acc) -> + %% HTML5 charrefs can have more than one code point(!) + [{<<"td">>, _, [{<<"code">>, _, [TitleSemi]}]}, + {<<"td">>, [], [RawCPs]} | _] = Children, + L = byte_size(TitleSemi) - 1, + <> = TitleSemi, + {match, Matches} = re:run(RawCPs, "(?:\\s*U\\+)([a-fA-F0-9]+)", + [{capture, all, binary}, global]), + [{Title, [CP || [_, CP] <- Matches]} | Acc]; +search({Tag, Attrs, [H | T]}, Acc) -> + search({Tag, Attrs, T}, search(H, Acc)); +search({_Tag, _Attrs, []}, Acc) -> + Acc; +search(<<_/binary>>, Acc) -> + Acc. diff --git a/rabbitmq-server/plugins-src/mochiweb-wrapper/mochiweb-git/scripts/new_mochiweb.erl b/rabbitmq-server/plugins-src/mochiweb-wrapper/mochiweb-git/scripts/new_mochiweb.erl new file mode 100755 index 0000000..f49ed39 --- /dev/null +++ b/rabbitmq-server/plugins-src/mochiweb-wrapper/mochiweb-git/scripts/new_mochiweb.erl @@ -0,0 +1,23 @@ +#!/usr/bin/env escript +%% -*- mode: erlang -*- +-export([main/1]). + +%% External API + +main(_) -> + usage(). + +%% Internal API + +usage() -> + io:format( + "new_mochiweb.erl has been replaced by a rebar template!\n" + "\n" + "To create a new mochiweb using project:\n" + " make app PROJECT=project_name\n" + "\n" + "To create a new mochiweb using project in a specific directory:\n" + " make app PROJECT=project_name PREFIX=$HOME/projects/\n" + "\n" + ), + halt(1). diff --git a/rabbitmq-server/plugins-src/mochiweb-wrapper/mochiweb-git/src/mochifmt.erl b/rabbitmq-server/plugins-src/mochiweb-wrapper/mochiweb-git/src/mochifmt.erl new file mode 100644 index 0000000..fc95e4f --- /dev/null +++ b/rabbitmq-server/plugins-src/mochiweb-wrapper/mochiweb-git/src/mochifmt.erl @@ -0,0 +1,425 @@ +%% @author Bob Ippolito +%% @copyright 2008 Mochi Media, Inc. + +%% @doc String Formatting for Erlang, inspired by Python 2.6 +%% (PEP 3101). +%% +-module(mochifmt). +-author('bob@mochimedia.com'). +-export([format/2, format_field/2, convert_field/2, get_value/2, get_field/2]). +-export([tokenize/1, format/3, get_field/3, format_field/3]). +-export([bformat/2, bformat/3]). +-export([f/2, f/3]). + +-record(conversion, {length, precision, ctype, align, fill_char, sign}). + +%% @spec tokenize(S::string()) -> tokens() +%% @doc Tokenize a format string into mochifmt's internal format. +tokenize(S) -> + {?MODULE, tokenize(S, "", [])}. + +%% @spec convert_field(Arg, Conversion::conversion()) -> term() +%% @doc Process Arg according to the given explicit conversion specifier. +convert_field(Arg, "") -> + Arg; +convert_field(Arg, "r") -> + repr(Arg); +convert_field(Arg, "s") -> + str(Arg). + +%% @spec get_value(Key::string(), Args::args()) -> term() +%% @doc Get the Key from Args. If Args is a tuple then convert Key to +%% an integer and get element(1 + Key, Args). If Args is a list and Key +%% can be parsed as an integer then use lists:nth(1 + Key, Args), +%% otherwise try and look for Key in Args as a proplist, converting +%% Key to an atom or binary if necessary. +get_value(Key, Args) when is_tuple(Args) -> + element(1 + list_to_integer(Key), Args); +get_value(Key, Args) when is_list(Args) -> + try lists:nth(1 + list_to_integer(Key), Args) + catch error:_ -> + {_K, V} = proplist_lookup(Key, Args), + V + end. + +%% @spec get_field(Key::string(), Args) -> term() +%% @doc Consecutively call get_value/2 on parts of Key delimited by ".", +%% replacing Args with the result of the previous get_value. This +%% is used to implement formats such as {0.0}. +get_field(Key, Args) -> + get_field(Key, Args, ?MODULE). + +%% @spec get_field(Key::string(), Args, Module) -> term() +%% @doc Consecutively call Module:get_value/2 on parts of Key delimited by ".", +%% replacing Args with the result of the previous get_value. This +%% is used to implement formats such as {0.0}. +get_field(Key, Args, Module) -> + {Name, Next} = lists:splitwith(fun (C) -> C =/= $. end, Key), + Res = try Module:get_value(Name, Args) + catch error:undef -> get_value(Name, Args) end, + case Next of + "" -> + Res; + "." ++ S1 -> + get_field(S1, Res, Module) + end. + +%% @spec format(Format::string(), Args) -> iolist() +%% @doc Format Args with Format. +format(Format, Args) -> + format(Format, Args, ?MODULE). + +%% @spec format(Format::string(), Args, Module) -> iolist() +%% @doc Format Args with Format using Module. +format({?MODULE, Parts}, Args, Module) -> + format2(Parts, Args, Module, []); +format(S, Args, Module) -> + format(tokenize(S), Args, Module). + +%% @spec format_field(Arg, Format) -> iolist() +%% @doc Format Arg with Format. +format_field(Arg, Format) -> + format_field(Arg, Format, ?MODULE). + +%% @spec format_field(Arg, Format, _Module) -> iolist() +%% @doc Format Arg with Format. +format_field(Arg, Format, _Module) -> + F = default_ctype(Arg, parse_std_conversion(Format)), + fix_padding(fix_sign(convert2(Arg, F), F), F). + +%% @spec f(Format::string(), Args) -> string() +%% @doc Format Args with Format and return a string(). +f(Format, Args) -> + f(Format, Args, ?MODULE). + +%% @spec f(Format::string(), Args, Module) -> string() +%% @doc Format Args with Format using Module and return a string(). +f(Format, Args, Module) -> + case lists:member(${, Format) of + true -> + binary_to_list(bformat(Format, Args, Module)); + false -> + Format + end. + +%% @spec bformat(Format::string(), Args) -> binary() +%% @doc Format Args with Format and return a binary(). +bformat(Format, Args) -> + iolist_to_binary(format(Format, Args)). + +%% @spec bformat(Format::string(), Args, Module) -> binary() +%% @doc Format Args with Format using Module and return a binary(). +bformat(Format, Args, Module) -> + iolist_to_binary(format(Format, Args, Module)). + +%% Internal API + +add_raw("", Acc) -> + Acc; +add_raw(S, Acc) -> + [{raw, lists:reverse(S)} | Acc]. + +tokenize([], S, Acc) -> + lists:reverse(add_raw(S, Acc)); +tokenize("{{" ++ Rest, S, Acc) -> + tokenize(Rest, "{" ++ S, Acc); +tokenize("{" ++ Rest, S, Acc) -> + {Format, Rest1} = tokenize_format(Rest), + tokenize(Rest1, "", [{format, make_format(Format)} | add_raw(S, Acc)]); +tokenize("}}" ++ Rest, S, Acc) -> + tokenize(Rest, "}" ++ S, Acc); +tokenize([C | Rest], S, Acc) -> + tokenize(Rest, [C | S], Acc). + +tokenize_format(S) -> + tokenize_format(S, 1, []). + +tokenize_format("}" ++ Rest, 1, Acc) -> + {lists:reverse(Acc), Rest}; +tokenize_format("}" ++ Rest, N, Acc) -> + tokenize_format(Rest, N - 1, "}" ++ Acc); +tokenize_format("{" ++ Rest, N, Acc) -> + tokenize_format(Rest, 1 + N, "{" ++ Acc); +tokenize_format([C | Rest], N, Acc) -> + tokenize_format(Rest, N, [C | Acc]). + +make_format(S) -> + {Name0, Spec} = case lists:splitwith(fun (C) -> C =/= $: end, S) of + {_, ""} -> + {S, ""}; + {SN, ":" ++ SS} -> + {SN, SS} + end, + {Name, Transform} = case lists:splitwith(fun (C) -> C =/= $! end, Name0) of + {_, ""} -> + {Name0, ""}; + {TN, "!" ++ TT} -> + {TN, TT} + end, + {Name, Transform, Spec}. + +proplist_lookup(S, P) -> + A = try list_to_existing_atom(S) + catch error:_ -> make_ref() end, + B = try list_to_binary(S) + catch error:_ -> make_ref() end, + proplist_lookup2({S, A, B}, P). + +proplist_lookup2({KS, KA, KB}, [{K, V} | _]) + when KS =:= K orelse KA =:= K orelse KB =:= K -> + {K, V}; +proplist_lookup2(Keys, [_ | Rest]) -> + proplist_lookup2(Keys, Rest). + +format2([], _Args, _Module, Acc) -> + lists:reverse(Acc); +format2([{raw, S} | Rest], Args, Module, Acc) -> + format2(Rest, Args, Module, [S | Acc]); +format2([{format, {Key, Convert, Format0}} | Rest], Args, Module, Acc) -> + Format = f(Format0, Args, Module), + V = case Module of + ?MODULE -> + V0 = get_field(Key, Args), + V1 = convert_field(V0, Convert), + format_field(V1, Format); + _ -> + V0 = try Module:get_field(Key, Args) + catch error:undef -> get_field(Key, Args, Module) end, + V1 = try Module:convert_field(V0, Convert) + catch error:undef -> convert_field(V0, Convert) end, + try Module:format_field(V1, Format) + catch error:undef -> format_field(V1, Format, Module) end + end, + format2(Rest, Args, Module, [V | Acc]). + +default_ctype(_Arg, C=#conversion{ctype=N}) when N =/= undefined -> + C; +default_ctype(Arg, C) when is_integer(Arg) -> + C#conversion{ctype=decimal}; +default_ctype(Arg, C) when is_float(Arg) -> + C#conversion{ctype=general}; +default_ctype(_Arg, C) -> + C#conversion{ctype=string}. + +fix_padding(Arg, #conversion{length=undefined}) -> + Arg; +fix_padding(Arg, F=#conversion{length=Length, fill_char=Fill0, align=Align0, + ctype=Type}) -> + Padding = Length - iolist_size(Arg), + Fill = case Fill0 of + undefined -> + $\s; + _ -> + Fill0 + end, + Align = case Align0 of + undefined -> + case Type of + string -> + left; + _ -> + right + end; + _ -> + Align0 + end, + case Padding > 0 of + true -> + do_padding(Arg, Padding, Fill, Align, F); + false -> + Arg + end. + +do_padding(Arg, Padding, Fill, right, _F) -> + [lists:duplicate(Padding, Fill), Arg]; +do_padding(Arg, Padding, Fill, center, _F) -> + LPadding = lists:duplicate(Padding div 2, Fill), + RPadding = case Padding band 1 of + 1 -> + [Fill | LPadding]; + _ -> + LPadding + end, + [LPadding, Arg, RPadding]; +do_padding([$- | Arg], Padding, Fill, sign_right, _F) -> + [[$- | lists:duplicate(Padding, Fill)], Arg]; +do_padding(Arg, Padding, Fill, sign_right, #conversion{sign=$-}) -> + [lists:duplicate(Padding, Fill), Arg]; +do_padding([S | Arg], Padding, Fill, sign_right, #conversion{sign=S}) -> + [[S | lists:duplicate(Padding, Fill)], Arg]; +do_padding(Arg, Padding, Fill, sign_right, #conversion{sign=undefined}) -> + [lists:duplicate(Padding, Fill), Arg]; +do_padding(Arg, Padding, Fill, left, _F) -> + [Arg | lists:duplicate(Padding, Fill)]. + +fix_sign(Arg, #conversion{sign=$+}) when Arg >= 0 -> + [$+, Arg]; +fix_sign(Arg, #conversion{sign=$\s}) when Arg >= 0 -> + [$\s, Arg]; +fix_sign(Arg, _F) -> + Arg. + +ctype($\%) -> percent; +ctype($s) -> string; +ctype($b) -> bin; +ctype($o) -> oct; +ctype($X) -> upper_hex; +ctype($x) -> hex; +ctype($c) -> char; +ctype($d) -> decimal; +ctype($g) -> general; +ctype($f) -> fixed; +ctype($e) -> exp. + +align($<) -> left; +align($>) -> right; +align($^) -> center; +align($=) -> sign_right. + +convert2(Arg, F=#conversion{ctype=percent}) -> + [convert2(100.0 * Arg, F#conversion{ctype=fixed}), $\%]; +convert2(Arg, #conversion{ctype=string}) -> + str(Arg); +convert2(Arg, #conversion{ctype=bin}) -> + erlang:integer_to_list(Arg, 2); +convert2(Arg, #conversion{ctype=oct}) -> + erlang:integer_to_list(Arg, 8); +convert2(Arg, #conversion{ctype=upper_hex}) -> + erlang:integer_to_list(Arg, 16); +convert2(Arg, #conversion{ctype=hex}) -> + string:to_lower(erlang:integer_to_list(Arg, 16)); +convert2(Arg, #conversion{ctype=char}) when Arg < 16#80 -> + [Arg]; +convert2(Arg, #conversion{ctype=char}) -> + xmerl_ucs:to_utf8(Arg); +convert2(Arg, #conversion{ctype=decimal}) -> + integer_to_list(Arg); +convert2(Arg, #conversion{ctype=general, precision=undefined}) -> + try mochinum:digits(Arg) + catch error:undef -> io_lib:format("~g", [Arg]) end; +convert2(Arg, #conversion{ctype=fixed, precision=undefined}) -> + io_lib:format("~f", [Arg]); +convert2(Arg, #conversion{ctype=exp, precision=undefined}) -> + io_lib:format("~e", [Arg]); +convert2(Arg, #conversion{ctype=general, precision=P}) -> + io_lib:format("~." ++ integer_to_list(P) ++ "g", [Arg]); +convert2(Arg, #conversion{ctype=fixed, precision=P}) -> + io_lib:format("~." ++ integer_to_list(P) ++ "f", [Arg]); +convert2(Arg, #conversion{ctype=exp, precision=P}) -> + io_lib:format("~." ++ integer_to_list(P) ++ "e", [Arg]). + +str(A) when is_atom(A) -> + atom_to_list(A); +str(I) when is_integer(I) -> + integer_to_list(I); +str(F) when is_float(F) -> + try mochinum:digits(F) + catch error:undef -> io_lib:format("~g", [F]) end; +str(L) when is_list(L) -> + L; +str(B) when is_binary(B) -> + B; +str(P) -> + repr(P). + +repr(P) when is_float(P) -> + try mochinum:digits(P) + catch error:undef -> float_to_list(P) end; +repr(P) -> + io_lib:format("~p", [P]). + +parse_std_conversion(S) -> + parse_std_conversion(S, #conversion{}). + +parse_std_conversion("", Acc) -> + Acc; +parse_std_conversion([Fill, Align | Spec], Acc) + when Align =:= $< orelse Align =:= $> orelse Align =:= $= orelse Align =:= $^ -> + parse_std_conversion(Spec, Acc#conversion{fill_char=Fill, + align=align(Align)}); +parse_std_conversion([Align | Spec], Acc) + when Align =:= $< orelse Align =:= $> orelse Align =:= $= orelse Align =:= $^ -> + parse_std_conversion(Spec, Acc#conversion{align=align(Align)}); +parse_std_conversion([Sign | Spec], Acc) + when Sign =:= $+ orelse Sign =:= $- orelse Sign =:= $\s -> + parse_std_conversion(Spec, Acc#conversion{sign=Sign}); +parse_std_conversion("0" ++ Spec, Acc) -> + Align = case Acc#conversion.align of + undefined -> + sign_right; + A -> + A + end, + parse_std_conversion(Spec, Acc#conversion{fill_char=$0, align=Align}); +parse_std_conversion(Spec=[D|_], Acc) when D >= $0 andalso D =< $9 -> + {W, Spec1} = lists:splitwith(fun (C) -> C >= $0 andalso C =< $9 end, Spec), + parse_std_conversion(Spec1, Acc#conversion{length=list_to_integer(W)}); +parse_std_conversion([$. | Spec], Acc) -> + case lists:splitwith(fun (C) -> C >= $0 andalso C =< $9 end, Spec) of + {"", Spec1} -> + parse_std_conversion(Spec1, Acc); + {P, Spec1} -> + parse_std_conversion(Spec1, + Acc#conversion{precision=list_to_integer(P)}) + end; +parse_std_conversion([Type], Acc) -> + parse_std_conversion("", Acc#conversion{ctype=ctype(Type)}). + + +%% +%% Tests +%% +-ifdef(TEST). +-include_lib("eunit/include/eunit.hrl"). + +tokenize_test() -> + {?MODULE, [{raw, "ABC"}]} = tokenize("ABC"), + {?MODULE, [{format, {"0", "", ""}}]} = tokenize("{0}"), + {?MODULE, [{raw, "ABC"}, {format, {"1", "", ""}}, {raw, "DEF"}]} = + tokenize("ABC{1}DEF"), + ok. + +format_test() -> + <<" -4">> = bformat("{0:4}", [-4]), + <<" 4">> = bformat("{0:4}", [4]), + <<" 4">> = bformat("{0:{0}}", [4]), + <<"4 ">> = bformat("{0:4}", ["4"]), + <<"4 ">> = bformat("{0:{0}}", ["4"]), + <<"1.2yoDEF">> = bformat("{2}{0}{1}{3}", {yo, "DE", 1.2, <<"F">>}), + <<"cafebabe">> = bformat("{0:x}", {16#cafebabe}), + <<"CAFEBABE">> = bformat("{0:X}", {16#cafebabe}), + <<"CAFEBABE">> = bformat("{0:X}", {16#cafebabe}), + <<"755">> = bformat("{0:o}", {8#755}), + <<"a">> = bformat("{0:c}", {97}), + %% Horizontal ellipsis + <<226, 128, 166>> = bformat("{0:c}", {16#2026}), + <<"11">> = bformat("{0:b}", {3}), + <<"11">> = bformat("{0:b}", [3]), + <<"11">> = bformat("{three:b}", [{three, 3}]), + <<"11">> = bformat("{three:b}", [{"three", 3}]), + <<"11">> = bformat("{three:b}", [{<<"three">>, 3}]), + <<"\"foo\"">> = bformat("{0!r}", {"foo"}), + <<"2008-5-4">> = bformat("{0.0}-{0.1}-{0.2}", {{2008,5,4}}), + <<"2008-05-04">> = bformat("{0.0:04}-{0.1:02}-{0.2:02}", {{2008,5,4}}), + <<"foo6bar-6">> = bformat("foo{1}{0}-{1}", {bar, 6}), + <<"-'atom test'-">> = bformat("-{arg!r}-", [{arg, 'atom test'}]), + <<"2008-05-04">> = bformat("{0.0:0{1.0}}-{0.1:0{1.1}}-{0.2:0{1.2}}", + {{2008,5,4}, {4, 2, 2}}), + ok. + +std_test() -> + M = mochifmt_std:new(), + <<"01">> = bformat("{0}{1}", [0, 1], M), + ok. + +records_test() -> + M = mochifmt_records:new([{conversion, record_info(fields, conversion)}]), + R = #conversion{length=long, precision=hard, sign=peace}, + long = M:get_value("length", R), + hard = M:get_value("precision", R), + peace = M:get_value("sign", R), + <<"long hard">> = bformat("{length} {precision}", R, M), + <<"long hard">> = bformat("{0.length} {0.precision}", [R], M), + ok. + +-endif. diff --git a/rabbitmq-server/plugins-src/mochiweb-wrapper/mochiweb-git/src/mochifmt_records.erl b/rabbitmq-server/plugins-src/mochiweb-wrapper/mochiweb-git/src/mochifmt_records.erl new file mode 100644 index 0000000..7d166ff --- /dev/null +++ b/rabbitmq-server/plugins-src/mochiweb-wrapper/mochiweb-git/src/mochifmt_records.erl @@ -0,0 +1,42 @@ +%% @author Bob Ippolito +%% @copyright 2008 Mochi Media, Inc. + +%% @doc Formatter that understands records. +%% +%% Usage: +%% +%% 1> M = mochifmt_records:new([{rec, record_info(fields, rec)}]), +%% M:format("{0.bar}", [#rec{bar=foo}]). +%% foo + +-module(mochifmt_records). +-author('bob@mochimedia.com'). +-export([new/1, get_value/3]). + +new([{_Rec, RecFields}]=Recs) when is_list(RecFields) -> + {?MODULE, Recs}. + +get_value(Key, Rec, {?MODULE, Recs}) + when is_tuple(Rec) and is_atom(element(1, Rec)) -> + try begin + Atom = list_to_existing_atom(Key), + {_, Fields} = proplists:lookup(element(1, Rec), Recs), + element(get_rec_index(Atom, Fields, 2), Rec) + end + catch error:_ -> mochifmt:get_value(Key, Rec) + end; +get_value(Key, Args, {?MODULE, _Recs}) -> + mochifmt:get_value(Key, Args). + +get_rec_index(Atom, [Atom | _], Index) -> + Index; +get_rec_index(Atom, [_ | Rest], Index) -> + get_rec_index(Atom, Rest, 1 + Index). + + +%% +%% Tests +%% +-ifdef(TEST). +-include_lib("eunit/include/eunit.hrl"). +-endif. diff --git a/rabbitmq-server/plugins-src/mochiweb-wrapper/mochiweb-git/src/mochifmt_std.erl b/rabbitmq-server/plugins-src/mochiweb-wrapper/mochiweb-git/src/mochifmt_std.erl new file mode 100644 index 0000000..ea68c4a --- /dev/null +++ b/rabbitmq-server/plugins-src/mochiweb-wrapper/mochiweb-git/src/mochifmt_std.erl @@ -0,0 +1,33 @@ +%% @author Bob Ippolito +%% @copyright 2008 Mochi Media, Inc. + +%% @doc Template module for a mochifmt formatter. + +-module(mochifmt_std). +-author('bob@mochimedia.com'). +-export([new/0, format/3, get_value/3, format_field/3, get_field/3, convert_field/3]). + +new() -> + {?MODULE}. + +format(Format, Args, {?MODULE}=THIS) -> + mochifmt:format(Format, Args, THIS). + +get_field(Key, Args, {?MODULE}=THIS) -> + mochifmt:get_field(Key, Args, THIS). + +convert_field(Key, Args, {?MODULE}) -> + mochifmt:convert_field(Key, Args). + +get_value(Key, Args, {?MODULE}) -> + mochifmt:get_value(Key, Args). + +format_field(Arg, Format, {?MODULE}=THIS) -> + mochifmt:format_field(Arg, Format, THIS). + +%% +%% Tests +%% +-ifdef(TEST). +-include_lib("eunit/include/eunit.hrl"). +-endif. diff --git a/rabbitmq-server/plugins-src/mochiweb-wrapper/mochiweb-git/src/mochihex.erl b/rabbitmq-server/plugins-src/mochiweb-wrapper/mochiweb-git/src/mochihex.erl new file mode 100644 index 0000000..796f3ad --- /dev/null +++ b/rabbitmq-server/plugins-src/mochiweb-wrapper/mochiweb-git/src/mochihex.erl @@ -0,0 +1,88 @@ +%% @author Bob Ippolito +%% @copyright 2006 Mochi Media, Inc. + +%% @doc Utilities for working with hexadecimal strings. + +-module(mochihex). +-author('bob@mochimedia.com'). + +-export([to_hex/1, to_bin/1, to_int/1, dehex/1, hexdigit/1]). + +%% @spec to_hex(integer | iolist()) -> string() +%% @doc Convert an iolist to a hexadecimal string. +to_hex(0) -> + "0"; +to_hex(I) when is_integer(I), I > 0 -> + to_hex_int(I, []); +to_hex(B) -> + to_hex(iolist_to_binary(B), []). + +%% @spec to_bin(string()) -> binary() +%% @doc Convert a hexadecimal string to a binary. +to_bin(L) -> + to_bin(L, []). + +%% @spec to_int(string()) -> integer() +%% @doc Convert a hexadecimal string to an integer. +to_int(L) -> + erlang:list_to_integer(L, 16). + +%% @spec dehex(char()) -> integer() +%% @doc Convert a hex digit to its integer value. +dehex(C) when C >= $0, C =< $9 -> + C - $0; +dehex(C) when C >= $a, C =< $f -> + C - $a + 10; +dehex(C) when C >= $A, C =< $F -> + C - $A + 10. + +%% @spec hexdigit(integer()) -> char() +%% @doc Convert an integer less than 16 to a hex digit. +hexdigit(C) when C >= 0, C =< 9 -> + C + $0; +hexdigit(C) when C =< 15 -> + C + $a - 10. + +%% Internal API + +to_hex(<<>>, Acc) -> + lists:reverse(Acc); +to_hex(<>, Acc) -> + to_hex(Rest, [hexdigit(C2), hexdigit(C1) | Acc]). + +to_hex_int(0, Acc) -> + Acc; +to_hex_int(I, Acc) -> + to_hex_int(I bsr 4, [hexdigit(I band 15) | Acc]). + +to_bin([], Acc) -> + iolist_to_binary(lists:reverse(Acc)); +to_bin([C1, C2 | Rest], Acc) -> + to_bin(Rest, [(dehex(C1) bsl 4) bor dehex(C2) | Acc]). + + + +%% +%% Tests +%% +-ifdef(TEST). +-include_lib("eunit/include/eunit.hrl"). + +to_hex_test() -> + "ff000ff1" = to_hex([255, 0, 15, 241]), + "ff000ff1" = to_hex(16#ff000ff1), + "0" = to_hex(16#0), + ok. + +to_bin_test() -> + <<255, 0, 15, 241>> = to_bin("ff000ff1"), + <<255, 0, 10, 161>> = to_bin("Ff000aA1"), + ok. + +to_int_test() -> + 16#ff000ff1 = to_int("ff000ff1"), + 16#ff000aa1 = to_int("FF000Aa1"), + 16#0 = to_int("0"), + ok. + +-endif. diff --git a/rabbitmq-server/plugins-src/mochiweb-wrapper/mochiweb-git/src/mochijson.erl b/rabbitmq-server/plugins-src/mochiweb-wrapper/mochiweb-git/src/mochijson.erl new file mode 100644 index 0000000..d283189 --- /dev/null +++ b/rabbitmq-server/plugins-src/mochiweb-wrapper/mochiweb-git/src/mochijson.erl @@ -0,0 +1,529 @@ +%% @author Bob Ippolito +%% @copyright 2006 Mochi Media, Inc. + +%% @doc Yet another JSON (RFC 4627) library for Erlang. +-module(mochijson). +-author('bob@mochimedia.com'). +-export([encoder/1, encode/1]). +-export([decoder/1, decode/1]). +-export([binary_encoder/1, binary_encode/1]). +-export([binary_decoder/1, binary_decode/1]). + +% This is a macro to placate syntax highlighters.. +-define(Q, $\"). +-define(ADV_COL(S, N), S#decoder{column=N+S#decoder.column}). +-define(INC_COL(S), S#decoder{column=1+S#decoder.column}). +-define(INC_LINE(S), S#decoder{column=1, line=1+S#decoder.line}). + +%% @type json_string() = atom | string() | binary() +%% @type json_number() = integer() | float() +%% @type json_array() = {array, [json_term()]} +%% @type json_object() = {struct, [{json_string(), json_term()}]} +%% @type json_term() = json_string() | json_number() | json_array() | +%% json_object() +%% @type encoding() = utf8 | unicode +%% @type encoder_option() = {input_encoding, encoding()} | +%% {handler, function()} +%% @type decoder_option() = {input_encoding, encoding()} | +%% {object_hook, function()} +%% @type bjson_string() = binary() +%% @type bjson_number() = integer() | float() +%% @type bjson_array() = [bjson_term()] +%% @type bjson_object() = {struct, [{bjson_string(), bjson_term()}]} +%% @type bjson_term() = bjson_string() | bjson_number() | bjson_array() | +%% bjson_object() +%% @type binary_encoder_option() = {handler, function()} +%% @type binary_decoder_option() = {object_hook, function()} + +-record(encoder, {input_encoding=unicode, + handler=null}). + +-record(decoder, {input_encoding=utf8, + object_hook=null, + line=1, + column=1, + state=null}). + +%% @spec encoder([encoder_option()]) -> function() +%% @doc Create an encoder/1 with the given options. +encoder(Options) -> + State = parse_encoder_options(Options, #encoder{}), + fun (O) -> json_encode(O, State) end. + +%% @spec encode(json_term()) -> iolist() +%% @doc Encode the given as JSON to an iolist. +encode(Any) -> + json_encode(Any, #encoder{}). + +%% @spec decoder([decoder_option()]) -> function() +%% @doc Create a decoder/1 with the given options. +decoder(Options) -> + State = parse_decoder_options(Options, #decoder{}), + fun (O) -> json_decode(O, State) end. + +%% @spec decode(iolist()) -> json_term() +%% @doc Decode the given iolist to Erlang terms. +decode(S) -> + json_decode(S, #decoder{}). + +%% @spec binary_decoder([binary_decoder_option()]) -> function() +%% @doc Create a binary_decoder/1 with the given options. +binary_decoder(Options) -> + mochijson2:decoder(Options). + +%% @spec binary_encoder([binary_encoder_option()]) -> function() +%% @doc Create a binary_encoder/1 with the given options. +binary_encoder(Options) -> + mochijson2:encoder(Options). + +%% @spec binary_encode(bjson_term()) -> iolist() +%% @doc Encode the given as JSON to an iolist, using lists for arrays and +%% binaries for strings. +binary_encode(Any) -> + mochijson2:encode(Any). + +%% @spec binary_decode(iolist()) -> bjson_term() +%% @doc Decode the given iolist to Erlang terms, using lists for arrays and +%% binaries for strings. +binary_decode(S) -> + mochijson2:decode(S). + +%% Internal API + +parse_encoder_options([], State) -> + State; +parse_encoder_options([{input_encoding, Encoding} | Rest], State) -> + parse_encoder_options(Rest, State#encoder{input_encoding=Encoding}); +parse_encoder_options([{handler, Handler} | Rest], State) -> + parse_encoder_options(Rest, State#encoder{handler=Handler}). + +parse_decoder_options([], State) -> + State; +parse_decoder_options([{input_encoding, Encoding} | Rest], State) -> + parse_decoder_options(Rest, State#decoder{input_encoding=Encoding}); +parse_decoder_options([{object_hook, Hook} | Rest], State) -> + parse_decoder_options(Rest, State#decoder{object_hook=Hook}). + +json_encode(true, _State) -> + "true"; +json_encode(false, _State) -> + "false"; +json_encode(null, _State) -> + "null"; +json_encode(I, _State) when is_integer(I) -> + integer_to_list(I); +json_encode(F, _State) when is_float(F) -> + mochinum:digits(F); +json_encode(L, State) when is_list(L); is_binary(L); is_atom(L) -> + json_encode_string(L, State); +json_encode({array, Props}, State) when is_list(Props) -> + json_encode_array(Props, State); +json_encode({struct, Props}, State) when is_list(Props) -> + json_encode_proplist(Props, State); +json_encode(Bad, #encoder{handler=null}) -> + exit({json_encode, {bad_term, Bad}}); +json_encode(Bad, State=#encoder{handler=Handler}) -> + json_encode(Handler(Bad), State). + +json_encode_array([], _State) -> + "[]"; +json_encode_array(L, State) -> + F = fun (O, Acc) -> + [$,, json_encode(O, State) | Acc] + end, + [$, | Acc1] = lists:foldl(F, "[", L), + lists:reverse([$\] | Acc1]). + +json_encode_proplist([], _State) -> + "{}"; +json_encode_proplist(Props, State) -> + F = fun ({K, V}, Acc) -> + KS = case K of + K when is_atom(K) -> + json_encode_string_utf8(atom_to_list(K)); + K when is_integer(K) -> + json_encode_string(integer_to_list(K), State); + K when is_list(K); is_binary(K) -> + json_encode_string(K, State) + end, + VS = json_encode(V, State), + [$,, VS, $:, KS | Acc] + end, + [$, | Acc1] = lists:foldl(F, "{", Props), + lists:reverse([$\} | Acc1]). + +json_encode_string(A, _State) when is_atom(A) -> + json_encode_string_unicode(xmerl_ucs:from_utf8(atom_to_list(A))); +json_encode_string(B, _State) when is_binary(B) -> + json_encode_string_unicode(xmerl_ucs:from_utf8(B)); +json_encode_string(S, #encoder{input_encoding=utf8}) -> + json_encode_string_utf8(S); +json_encode_string(S, #encoder{input_encoding=unicode}) -> + json_encode_string_unicode(S). + +json_encode_string_utf8(S) -> + [?Q | json_encode_string_utf8_1(S)]. + +json_encode_string_utf8_1([C | Cs]) when C >= 0, C =< 16#7f -> + NewC = case C of + $\\ -> "\\\\"; + ?Q -> "\\\""; + _ when C >= $\s, C < 16#7f -> C; + $\t -> "\\t"; + $\n -> "\\n"; + $\r -> "\\r"; + $\f -> "\\f"; + $\b -> "\\b"; + _ when C >= 0, C =< 16#7f -> unihex(C); + _ -> exit({json_encode, {bad_char, C}}) + end, + [NewC | json_encode_string_utf8_1(Cs)]; +json_encode_string_utf8_1(All=[C | _]) when C >= 16#80, C =< 16#10FFFF -> + [?Q | Rest] = json_encode_string_unicode(xmerl_ucs:from_utf8(All)), + Rest; +json_encode_string_utf8_1([]) -> + "\"". + +json_encode_string_unicode(S) -> + [?Q | json_encode_string_unicode_1(S)]. + +json_encode_string_unicode_1([C | Cs]) -> + NewC = case C of + $\\ -> "\\\\"; + ?Q -> "\\\""; + _ when C >= $\s, C < 16#7f -> C; + $\t -> "\\t"; + $\n -> "\\n"; + $\r -> "\\r"; + $\f -> "\\f"; + $\b -> "\\b"; + _ when C >= 0, C =< 16#10FFFF -> unihex(C); + _ -> exit({json_encode, {bad_char, C}}) + end, + [NewC | json_encode_string_unicode_1(Cs)]; +json_encode_string_unicode_1([]) -> + "\"". + +dehex(C) when C >= $0, C =< $9 -> + C - $0; +dehex(C) when C >= $a, C =< $f -> + C - $a + 10; +dehex(C) when C >= $A, C =< $F -> + C - $A + 10. + +hexdigit(C) when C >= 0, C =< 9 -> + C + $0; +hexdigit(C) when C =< 15 -> + C + $a - 10. + +unihex(C) when C < 16#10000 -> + <> = <>, + Digits = [hexdigit(D) || D <- [D3, D2, D1, D0]], + [$\\, $u | Digits]; +unihex(C) when C =< 16#10FFFF -> + N = C - 16#10000, + S1 = 16#d800 bor ((N bsr 10) band 16#3ff), + S2 = 16#dc00 bor (N band 16#3ff), + [unihex(S1), unihex(S2)]. + +json_decode(B, S) when is_binary(B) -> + json_decode(binary_to_list(B), S); +json_decode(L, S) -> + {Res, L1, S1} = decode1(L, S), + {eof, [], _} = tokenize(L1, S1#decoder{state=trim}), + Res. + +decode1(L, S=#decoder{state=null}) -> + case tokenize(L, S#decoder{state=any}) of + {{const, C}, L1, S1} -> + {C, L1, S1}; + {start_array, L1, S1} -> + decode_array(L1, S1#decoder{state=any}, []); + {start_object, L1, S1} -> + decode_object(L1, S1#decoder{state=key}, []) + end. + +make_object(V, #decoder{object_hook=null}) -> + V; +make_object(V, #decoder{object_hook=Hook}) -> + Hook(V). + +decode_object(L, S=#decoder{state=key}, Acc) -> + case tokenize(L, S) of + {end_object, Rest, S1} -> + V = make_object({struct, lists:reverse(Acc)}, S1), + {V, Rest, S1#decoder{state=null}}; + {{const, K}, Rest, S1} when is_list(K) -> + {colon, L2, S2} = tokenize(Rest, S1), + {V, L3, S3} = decode1(L2, S2#decoder{state=null}), + decode_object(L3, S3#decoder{state=comma}, [{K, V} | Acc]) + end; +decode_object(L, S=#decoder{state=comma}, Acc) -> + case tokenize(L, S) of + {end_object, Rest, S1} -> + V = make_object({struct, lists:reverse(Acc)}, S1), + {V, Rest, S1#decoder{state=null}}; + {comma, Rest, S1} -> + decode_object(Rest, S1#decoder{state=key}, Acc) + end. + +decode_array(L, S=#decoder{state=any}, Acc) -> + case tokenize(L, S) of + {end_array, Rest, S1} -> + {{array, lists:reverse(Acc)}, Rest, S1#decoder{state=null}}; + {start_array, Rest, S1} -> + {Array, Rest1, S2} = decode_array(Rest, S1#decoder{state=any}, []), + decode_array(Rest1, S2#decoder{state=comma}, [Array | Acc]); + {start_object, Rest, S1} -> + {Array, Rest1, S2} = decode_object(Rest, S1#decoder{state=key}, []), + decode_array(Rest1, S2#decoder{state=comma}, [Array | Acc]); + {{const, Const}, Rest, S1} -> + decode_array(Rest, S1#decoder{state=comma}, [Const | Acc]) + end; +decode_array(L, S=#decoder{state=comma}, Acc) -> + case tokenize(L, S) of + {end_array, Rest, S1} -> + {{array, lists:reverse(Acc)}, Rest, S1#decoder{state=null}}; + {comma, Rest, S1} -> + decode_array(Rest, S1#decoder{state=any}, Acc) + end. + +tokenize_string(IoList=[C | _], S=#decoder{input_encoding=utf8}, Acc) + when is_list(C); is_binary(C); C >= 16#7f -> + List = xmerl_ucs:from_utf8(iolist_to_binary(IoList)), + tokenize_string(List, S#decoder{input_encoding=unicode}, Acc); +tokenize_string("\"" ++ Rest, S, Acc) -> + {lists:reverse(Acc), Rest, ?INC_COL(S)}; +tokenize_string("\\\"" ++ Rest, S, Acc) -> + tokenize_string(Rest, ?ADV_COL(S, 2), [$\" | Acc]); +tokenize_string("\\\\" ++ Rest, S, Acc) -> + tokenize_string(Rest, ?ADV_COL(S, 2), [$\\ | Acc]); +tokenize_string("\\/" ++ Rest, S, Acc) -> + tokenize_string(Rest, ?ADV_COL(S, 2), [$/ | Acc]); +tokenize_string("\\b" ++ Rest, S, Acc) -> + tokenize_string(Rest, ?ADV_COL(S, 2), [$\b | Acc]); +tokenize_string("\\f" ++ Rest, S, Acc) -> + tokenize_string(Rest, ?ADV_COL(S, 2), [$\f | Acc]); +tokenize_string("\\n" ++ Rest, S, Acc) -> + tokenize_string(Rest, ?ADV_COL(S, 2), [$\n | Acc]); +tokenize_string("\\r" ++ Rest, S, Acc) -> + tokenize_string(Rest, ?ADV_COL(S, 2), [$\r | Acc]); +tokenize_string("\\t" ++ Rest, S, Acc) -> + tokenize_string(Rest, ?ADV_COL(S, 2), [$\t | Acc]); +tokenize_string([$\\, $u, C3, C2, C1, C0 | Rest], S, Acc) -> + % coalesce UTF-16 surrogate pair? + C = dehex(C0) bor + (dehex(C1) bsl 4) bor + (dehex(C2) bsl 8) bor + (dehex(C3) bsl 12), + tokenize_string(Rest, ?ADV_COL(S, 6), [C | Acc]); +tokenize_string([C | Rest], S, Acc) when C >= $\s; C < 16#10FFFF -> + tokenize_string(Rest, ?ADV_COL(S, 1), [C | Acc]). + +tokenize_number(IoList=[C | _], Mode, S=#decoder{input_encoding=utf8}, Acc) + when is_list(C); is_binary(C); C >= 16#7f -> + List = xmerl_ucs:from_utf8(iolist_to_binary(IoList)), + tokenize_number(List, Mode, S#decoder{input_encoding=unicode}, Acc); +tokenize_number([$- | Rest], sign, S, []) -> + tokenize_number(Rest, int, ?INC_COL(S), [$-]); +tokenize_number(Rest, sign, S, []) -> + tokenize_number(Rest, int, S, []); +tokenize_number([$0 | Rest], int, S, Acc) -> + tokenize_number(Rest, frac, ?INC_COL(S), [$0 | Acc]); +tokenize_number([C | Rest], int, S, Acc) when C >= $1, C =< $9 -> + tokenize_number(Rest, int1, ?INC_COL(S), [C | Acc]); +tokenize_number([C | Rest], int1, S, Acc) when C >= $0, C =< $9 -> + tokenize_number(Rest, int1, ?INC_COL(S), [C | Acc]); +tokenize_number(Rest, int1, S, Acc) -> + tokenize_number(Rest, frac, S, Acc); +tokenize_number([$., C | Rest], frac, S, Acc) when C >= $0, C =< $9 -> + tokenize_number(Rest, frac1, ?ADV_COL(S, 2), [C, $. | Acc]); +tokenize_number([E | Rest], frac, S, Acc) when E == $e; E == $E -> + tokenize_number(Rest, esign, ?INC_COL(S), [$e, $0, $. | Acc]); +tokenize_number(Rest, frac, S, Acc) -> + {{int, lists:reverse(Acc)}, Rest, S}; +tokenize_number([C | Rest], frac1, S, Acc) when C >= $0, C =< $9 -> + tokenize_number(Rest, frac1, ?INC_COL(S), [C | Acc]); +tokenize_number([E | Rest], frac1, S, Acc) when E == $e; E == $E -> + tokenize_number(Rest, esign, ?INC_COL(S), [$e | Acc]); +tokenize_number(Rest, frac1, S, Acc) -> + {{float, lists:reverse(Acc)}, Rest, S}; +tokenize_number([C | Rest], esign, S, Acc) when C == $-; C == $+ -> + tokenize_number(Rest, eint, ?INC_COL(S), [C | Acc]); +tokenize_number(Rest, esign, S, Acc) -> + tokenize_number(Rest, eint, S, Acc); +tokenize_number([C | Rest], eint, S, Acc) when C >= $0, C =< $9 -> + tokenize_number(Rest, eint1, ?INC_COL(S), [C | Acc]); +tokenize_number([C | Rest], eint1, S, Acc) when C >= $0, C =< $9 -> + tokenize_number(Rest, eint1, ?INC_COL(S), [C | Acc]); +tokenize_number(Rest, eint1, S, Acc) -> + {{float, lists:reverse(Acc)}, Rest, S}. + +tokenize([], S=#decoder{state=trim}) -> + {eof, [], S}; +tokenize([L | Rest], S) when is_list(L) -> + tokenize(L ++ Rest, S); +tokenize([B | Rest], S) when is_binary(B) -> + tokenize(xmerl_ucs:from_utf8(B) ++ Rest, S); +tokenize("\r\n" ++ Rest, S) -> + tokenize(Rest, ?INC_LINE(S)); +tokenize("\n" ++ Rest, S) -> + tokenize(Rest, ?INC_LINE(S)); +tokenize([C | Rest], S) when C == $\s; C == $\t -> + tokenize(Rest, ?INC_COL(S)); +tokenize("{" ++ Rest, S) -> + {start_object, Rest, ?INC_COL(S)}; +tokenize("}" ++ Rest, S) -> + {end_object, Rest, ?INC_COL(S)}; +tokenize("[" ++ Rest, S) -> + {start_array, Rest, ?INC_COL(S)}; +tokenize("]" ++ Rest, S) -> + {end_array, Rest, ?INC_COL(S)}; +tokenize("," ++ Rest, S) -> + {comma, Rest, ?INC_COL(S)}; +tokenize(":" ++ Rest, S) -> + {colon, Rest, ?INC_COL(S)}; +tokenize("null" ++ Rest, S) -> + {{const, null}, Rest, ?ADV_COL(S, 4)}; +tokenize("true" ++ Rest, S) -> + {{const, true}, Rest, ?ADV_COL(S, 4)}; +tokenize("false" ++ Rest, S) -> + {{const, false}, Rest, ?ADV_COL(S, 5)}; +tokenize("\"" ++ Rest, S) -> + {String, Rest1, S1} = tokenize_string(Rest, ?INC_COL(S), []), + {{const, String}, Rest1, S1}; +tokenize(L=[C | _], S) when C >= $0, C =< $9; C == $- -> + case tokenize_number(L, sign, S, []) of + {{int, Int}, Rest, S1} -> + {{const, list_to_integer(Int)}, Rest, S1}; + {{float, Float}, Rest, S1} -> + {{const, list_to_float(Float)}, Rest, S1} + end. + + +%% +%% Tests +%% +-ifdef(TEST). +-include_lib("eunit/include/eunit.hrl"). + +%% testing constructs borrowed from the Yaws JSON implementation. + +%% Create an object from a list of Key/Value pairs. + +obj_new() -> + {struct, []}. + +is_obj({struct, Props}) -> + F = fun ({K, _}) when is_list(K) -> + true; + (_) -> + false + end, + lists:all(F, Props). + +obj_from_list(Props) -> + Obj = {struct, Props}, + case is_obj(Obj) of + true -> Obj; + false -> exit(json_bad_object) + end. + +%% Test for equivalence of Erlang terms. +%% Due to arbitrary order of construction, equivalent objects might +%% compare unequal as erlang terms, so we need to carefully recurse +%% through aggregates (tuples and objects). + +equiv({struct, Props1}, {struct, Props2}) -> + equiv_object(Props1, Props2); +equiv({array, L1}, {array, L2}) -> + equiv_list(L1, L2); +equiv(N1, N2) when is_number(N1), is_number(N2) -> N1 == N2; +equiv(S1, S2) when is_list(S1), is_list(S2) -> S1 == S2; +equiv(true, true) -> true; +equiv(false, false) -> true; +equiv(null, null) -> true. + +%% Object representation and traversal order is unknown. +%% Use the sledgehammer and sort property lists. + +equiv_object(Props1, Props2) -> + L1 = lists:keysort(1, Props1), + L2 = lists:keysort(1, Props2), + Pairs = lists:zip(L1, L2), + true = lists:all(fun({{K1, V1}, {K2, V2}}) -> + equiv(K1, K2) and equiv(V1, V2) + end, Pairs). + +%% Recursively compare tuple elements for equivalence. + +equiv_list([], []) -> + true; +equiv_list([V1 | L1], [V2 | L2]) -> + equiv(V1, V2) andalso equiv_list(L1, L2). + +e2j_vec_test() -> + test_one(e2j_test_vec(utf8), 1). + +issue33_test() -> + %% http://code.google.com/p/mochiweb/issues/detail?id=33 + Js = {struct, [{"key", [194, 163]}]}, + Encoder = encoder([{input_encoding, utf8}]), + "{\"key\":\"\\u00a3\"}" = lists:flatten(Encoder(Js)). + +test_one([], _N) -> + %% io:format("~p tests passed~n", [N-1]), + ok; +test_one([{E, J} | Rest], N) -> + %% io:format("[~p] ~p ~p~n", [N, E, J]), + true = equiv(E, decode(J)), + true = equiv(E, decode(encode(E))), + test_one(Rest, 1+N). + +e2j_test_vec(utf8) -> + [ + {1, "1"}, + {3.1416, "3.14160"}, % text representation may truncate, trail zeroes + {-1, "-1"}, + {-3.1416, "-3.14160"}, + {12.0e10, "1.20000e+11"}, + {1.234E+10, "1.23400e+10"}, + {-1.234E-10, "-1.23400e-10"}, + {10.0, "1.0e+01"}, + {123.456, "1.23456E+2"}, + {10.0, "1e1"}, + {"foo", "\"foo\""}, + {"foo" ++ [5] ++ "bar", "\"foo\\u0005bar\""}, + {"", "\"\""}, + {"\"", "\"\\\"\""}, + {"\n\n\n", "\"\\n\\n\\n\""}, + {"\\", "\"\\\\\""}, + {"\" \b\f\r\n\t\"", "\"\\\" \\b\\f\\r\\n\\t\\\"\""}, + {obj_new(), "{}"}, + {obj_from_list([{"foo", "bar"}]), "{\"foo\":\"bar\"}"}, + {obj_from_list([{"foo", "bar"}, {"baz", 123}]), + "{\"foo\":\"bar\",\"baz\":123}"}, + {{array, []}, "[]"}, + {{array, [{array, []}]}, "[[]]"}, + {{array, [1, "foo"]}, "[1,\"foo\"]"}, + + % json array in a json object + {obj_from_list([{"foo", {array, [123]}}]), + "{\"foo\":[123]}"}, + + % json object in a json object + {obj_from_list([{"foo", obj_from_list([{"bar", true}])}]), + "{\"foo\":{\"bar\":true}}"}, + + % fold evaluation order + {obj_from_list([{"foo", {array, []}}, + {"bar", obj_from_list([{"baz", true}])}, + {"alice", "bob"}]), + "{\"foo\":[],\"bar\":{\"baz\":true},\"alice\":\"bob\"}"}, + + % json object in a json array + {{array, [-123, "foo", obj_from_list([{"bar", {array, []}}]), null]}, + "[-123,\"foo\",{\"bar\":[]},null]"} + ]. + +-endif. diff --git a/rabbitmq-server/plugins-src/mochiweb-wrapper/mochiweb-git/src/mochilists.erl b/rabbitmq-server/plugins-src/mochiweb-wrapper/mochiweb-git/src/mochilists.erl new file mode 100644 index 0000000..d93b241 --- /dev/null +++ b/rabbitmq-server/plugins-src/mochiweb-wrapper/mochiweb-git/src/mochilists.erl @@ -0,0 +1,104 @@ +%% @copyright Copyright (c) 2010 Mochi Media, Inc. +%% @author David Reid + +%% @doc Utility functions for dealing with proplists. + +-module(mochilists). +-author("David Reid "). +-export([get_value/2, get_value/3, is_defined/2, set_default/2, set_defaults/2]). + +%% @spec set_default({Key::term(), Value::term()}, Proplist::list()) -> list() +%% +%% @doc Return new Proplist with {Key, Value} set if not is_defined(Key, Proplist). +set_default({Key, Value}, Proplist) -> + case is_defined(Key, Proplist) of + true -> + Proplist; + false -> + [{Key, Value} | Proplist] + end. + +%% @spec set_defaults([{Key::term(), Value::term()}], Proplist::list()) -> list() +%% +%% @doc Return new Proplist with {Key, Value} set if not is_defined(Key, Proplist). +set_defaults(DefaultProps, Proplist) -> + lists:foldl(fun set_default/2, Proplist, DefaultProps). + + +%% @spec is_defined(Key::term(), Proplist::list()) -> bool() +%% +%% @doc Returns true if Propist contains at least one entry associated +%% with Key, otherwise false is returned. +is_defined(Key, Proplist) -> + lists:keyfind(Key, 1, Proplist) =/= false. + + +%% @spec get_value(Key::term(), Proplist::list()) -> term() | undefined +%% +%% @doc Return the value of Key or undefined +get_value(Key, Proplist) -> + get_value(Key, Proplist, undefined). + +%% @spec get_value(Key::term(), Proplist::list(), Default::term()) -> term() +%% +%% @doc Return the value of Key or Default +get_value(_Key, [], Default) -> + Default; +get_value(Key, Proplist, Default) -> + case lists:keyfind(Key, 1, Proplist) of + false -> + Default; + {Key, Value} -> + Value + end. + +%% +%% Tests +%% +-ifdef(TEST). +-include_lib("eunit/include/eunit.hrl"). + +set_defaults_test() -> + ?assertEqual( + [{k, v}], + set_defaults([{k, v}], [])), + ?assertEqual( + [{k, v}], + set_defaults([{k, vee}], [{k, v}])), + ?assertEqual( + lists:sort([{kay, vee}, {k, v}]), + lists:sort(set_defaults([{k, vee}, {kay, vee}], [{k, v}]))), + ok. + +set_default_test() -> + ?assertEqual( + [{k, v}], + set_default({k, v}, [])), + ?assertEqual( + [{k, v}], + set_default({k, vee}, [{k, v}])), + ok. + +get_value_test() -> + ?assertEqual( + undefined, + get_value(foo, [])), + ?assertEqual( + undefined, + get_value(foo, [{bar, baz}])), + ?assertEqual( + bar, + get_value(foo, [{foo, bar}])), + ?assertEqual( + default, + get_value(foo, [], default)), + ?assertEqual( + default, + get_value(foo, [{bar, baz}], default)), + ?assertEqual( + bar, + get_value(foo, [{foo, bar}], default)), + ok. + +-endif. + diff --git a/rabbitmq-server/plugins-src/mochiweb-wrapper/mochiweb-git/src/mochilogfile2.erl b/rabbitmq-server/plugins-src/mochiweb-wrapper/mochiweb-git/src/mochilogfile2.erl new file mode 100644 index 0000000..b4a7e3c --- /dev/null +++ b/rabbitmq-server/plugins-src/mochiweb-wrapper/mochiweb-git/src/mochilogfile2.erl @@ -0,0 +1,140 @@ +%% @author Bob Ippolito +%% @copyright 2010 Mochi Media, Inc. + +%% @doc Write newline delimited log files, ensuring that if a truncated +%% entry is found on log open then it is fixed before writing. Uses +%% delayed writes and raw files for performance. +-module(mochilogfile2). +-author('bob@mochimedia.com'). + +-export([open/1, write/2, close/1, name/1]). + +%% @spec open(Name) -> Handle +%% @doc Open the log file Name, creating or appending as necessary. All data +%% at the end of the file will be truncated until a newline is found, to +%% ensure that all records are complete. +open(Name) -> + {ok, FD} = file:open(Name, [raw, read, write, delayed_write, binary]), + fix_log(FD), + {?MODULE, Name, FD}. + +%% @spec name(Handle) -> string() +%% @doc Return the path of the log file. +name({?MODULE, Name, _FD}) -> + Name. + +%% @spec write(Handle, IoData) -> ok +%% @doc Write IoData to the log file referenced by Handle. +write({?MODULE, _Name, FD}, IoData) -> + ok = file:write(FD, [IoData, $\n]), + ok. + +%% @spec close(Handle) -> ok +%% @doc Close the log file referenced by Handle. +close({?MODULE, _Name, FD}) -> + ok = file:sync(FD), + ok = file:close(FD), + ok. + +fix_log(FD) -> + {ok, Location} = file:position(FD, eof), + Seek = find_last_newline(FD, Location), + {ok, Seek} = file:position(FD, Seek), + ok = file:truncate(FD), + ok. + +%% Seek backwards to the last valid log entry +find_last_newline(_FD, N) when N =< 1 -> + 0; +find_last_newline(FD, Location) -> + case file:pread(FD, Location - 1, 1) of + {ok, <<$\n>>} -> + Location; + {ok, _} -> + find_last_newline(FD, Location - 1) + end. + +%% +%% Tests +%% +-ifdef(TEST). +-include_lib("eunit/include/eunit.hrl"). +name_test() -> + D = mochitemp:mkdtemp(), + FileName = filename:join(D, "open_close_test.log"), + H = open(FileName), + ?assertEqual( + FileName, + name(H)), + close(H), + file:delete(FileName), + file:del_dir(D), + ok. + +open_close_test() -> + D = mochitemp:mkdtemp(), + FileName = filename:join(D, "open_close_test.log"), + OpenClose = fun () -> + H = open(FileName), + ?assertEqual( + true, + filelib:is_file(FileName)), + ok = close(H), + ?assertEqual( + {ok, <<>>}, + file:read_file(FileName)), + ok + end, + OpenClose(), + OpenClose(), + file:delete(FileName), + file:del_dir(D), + ok. + +write_test() -> + D = mochitemp:mkdtemp(), + FileName = filename:join(D, "write_test.log"), + F = fun () -> + H = open(FileName), + write(H, "test line"), + close(H), + ok + end, + F(), + ?assertEqual( + {ok, <<"test line\n">>}, + file:read_file(FileName)), + F(), + ?assertEqual( + {ok, <<"test line\ntest line\n">>}, + file:read_file(FileName)), + file:delete(FileName), + file:del_dir(D), + ok. + +fix_log_test() -> + D = mochitemp:mkdtemp(), + FileName = filename:join(D, "write_test.log"), + file:write_file(FileName, <<"first line good\nsecond line bad">>), + F = fun () -> + H = open(FileName), + write(H, "test line"), + close(H), + ok + end, + F(), + ?assertEqual( + {ok, <<"first line good\ntest line\n">>}, + file:read_file(FileName)), + file:write_file(FileName, <<"first line bad">>), + F(), + ?assertEqual( + {ok, <<"test line\n">>}, + file:read_file(FileName)), + F(), + ?assertEqual( + {ok, <<"test line\ntest line\n">>}, + file:read_file(FileName)), + ok. + +-endif. diff --git a/rabbitmq-server/plugins-src/mochiweb-wrapper/mochiweb-git/src/mochitemp.erl b/rabbitmq-server/plugins-src/mochiweb-wrapper/mochiweb-git/src/mochitemp.erl new file mode 100644 index 0000000..f64876d --- /dev/null +++ b/rabbitmq-server/plugins-src/mochiweb-wrapper/mochiweb-git/src/mochitemp.erl @@ -0,0 +1,307 @@ +%% @author Bob Ippolito +%% @copyright 2010 Mochi Media, Inc. + +%% @doc Create temporary files and directories. + +-module(mochitemp). +-export([gettempdir/0]). +-export([mkdtemp/0, mkdtemp/3]). +-export([rmtempdir/1]). +%% -export([mkstemp/4]). +-define(SAFE_CHARS, {$a, $b, $c, $d, $e, $f, $g, $h, $i, $j, $k, $l, $m, + $n, $o, $p, $q, $r, $s, $t, $u, $v, $w, $x, $y, $z, + $A, $B, $C, $D, $E, $F, $G, $H, $I, $J, $K, $L, $M, + $N, $O, $P, $Q, $R, $S, $T, $U, $V, $W, $X, $Y, $Z, + $0, $1, $2, $3, $4, $5, $6, $7, $8, $9, $_}). +-define(TMP_MAX, 10000). + +-include_lib("kernel/include/file.hrl"). + +%% TODO: An ugly wrapper over the mktemp tool with open_port and sadness? +%% We can't implement this race-free in Erlang without the ability +%% to issue O_CREAT|O_EXCL. I suppose we could hack something with +%% mkdtemp, del_dir, open. +%% mkstemp(Suffix, Prefix, Dir, Options) -> +%% ok. + +rmtempdir(Dir) -> + case file:del_dir(Dir) of + {error, eexist} -> + ok = rmtempdirfiles(Dir), + ok = file:del_dir(Dir); + ok -> + ok + end. + +rmtempdirfiles(Dir) -> + {ok, Files} = file:list_dir(Dir), + ok = rmtempdirfiles(Dir, Files). + +rmtempdirfiles(_Dir, []) -> + ok; +rmtempdirfiles(Dir, [Basename | Rest]) -> + Path = filename:join([Dir, Basename]), + case filelib:is_dir(Path) of + true -> + ok = rmtempdir(Path); + false -> + ok = file:delete(Path) + end, + rmtempdirfiles(Dir, Rest). + +mkdtemp() -> + mkdtemp("", "tmp", gettempdir()). + +mkdtemp(Suffix, Prefix, Dir) -> + mkdtemp_n(rngpath_fun(Suffix, Prefix, Dir), ?TMP_MAX). + + + +mkdtemp_n(RngPath, 1) -> + make_dir(RngPath()); +mkdtemp_n(RngPath, N) -> + try make_dir(RngPath()) + catch throw:{error, eexist} -> + mkdtemp_n(RngPath, N - 1) + end. + +make_dir(Path) -> + case file:make_dir(Path) of + ok -> + ok; + E={error, eexist} -> + throw(E) + end, + %% Small window for a race condition here because dir is created 777 + ok = file:write_file_info(Path, #file_info{mode=8#0700}), + Path. + +rngpath_fun(Prefix, Suffix, Dir) -> + fun () -> + filename:join([Dir, Prefix ++ rngchars(6) ++ Suffix]) + end. + +rngchars(0) -> + ""; +rngchars(N) -> + [rngchar() | rngchars(N - 1)]. + +rngchar() -> + rngchar(mochiweb_util:rand_uniform(0, tuple_size(?SAFE_CHARS))). + +rngchar(C) -> + element(1 + C, ?SAFE_CHARS). + +%% @spec gettempdir() -> string() +%% @doc Get a usable temporary directory using the first of these that is a directory: +%% $TMPDIR, $TMP, $TEMP, "/tmp", "/var/tmp", "/usr/tmp", ".". +gettempdir() -> + gettempdir(gettempdir_checks(), fun normalize_dir/1). + +gettempdir_checks() -> + [{fun os:getenv/1, ["TMPDIR", "TMP", "TEMP"]}, + {fun gettempdir_identity/1, ["/tmp", "/var/tmp", "/usr/tmp"]}, + {fun gettempdir_cwd/1, [cwd]}]. + +gettempdir_identity(L) -> + L. + +gettempdir_cwd(cwd) -> + {ok, L} = file:get_cwd(), + L. + +gettempdir([{_F, []} | RestF], Normalize) -> + gettempdir(RestF, Normalize); +gettempdir([{F, [L | RestL]} | RestF], Normalize) -> + case Normalize(F(L)) of + false -> + gettempdir([{F, RestL} | RestF], Normalize); + Dir -> + Dir + end. + +normalize_dir(False) when False =:= false orelse False =:= "" -> + %% Erlang doesn't have an unsetenv, wtf. + false; +normalize_dir(L) -> + Dir = filename:absname(L), + case filelib:is_dir(Dir) of + false -> + false; + true -> + Dir + end. + +%% +%% Tests +%% +-ifdef(TEST). +-include_lib("eunit/include/eunit.hrl"). + +pushenv(L) -> + [{K, os:getenv(K)} || K <- L]. +popenv(L) -> + F = fun ({K, false}) -> + %% Erlang doesn't have an unsetenv, wtf. + os:putenv(K, ""); + ({K, V}) -> + os:putenv(K, V) + end, + lists:foreach(F, L). + +gettempdir_fallback_test() -> + ?assertEqual( + "/", + gettempdir([{fun gettempdir_identity/1, ["/--not-here--/"]}, + {fun gettempdir_identity/1, ["/"]}], + fun normalize_dir/1)), + ?assertEqual( + "/", + %% simulate a true os:getenv unset env + gettempdir([{fun gettempdir_identity/1, [false]}, + {fun gettempdir_identity/1, ["/"]}], + fun normalize_dir/1)), + ok. + +gettempdir_identity_test() -> + ?assertEqual( + "/", + gettempdir([{fun gettempdir_identity/1, ["/"]}], fun normalize_dir/1)), + ok. + +gettempdir_cwd_test() -> + {ok, Cwd} = file:get_cwd(), + ?assertEqual( + normalize_dir(Cwd), + gettempdir([{fun gettempdir_cwd/1, [cwd]}], fun normalize_dir/1)), + ok. + +rngchars_test() -> + ?assertEqual( + "", + rngchars(0)), + ?assertEqual( + 10, + length(rngchars(10))), + ok. + +rngchar_test() -> + ?assertEqual( + $a, + rngchar(0)), + ?assertEqual( + $A, + rngchar(26)), + ?assertEqual( + $_, + rngchar(62)), + ok. + +mkdtemp_n_failonce_test() -> + D = mkdtemp(), + Path = filename:join([D, "testdir"]), + %% Toggle the existence of a dir so that it fails + %% the first time and succeeds the second. + F = fun () -> + case filelib:is_dir(Path) of + true -> + file:del_dir(Path); + false -> + file:make_dir(Path) + end, + Path + end, + try + %% Fails the first time + ?assertThrow( + {error, eexist}, + mkdtemp_n(F, 1)), + %% Reset state + file:del_dir(Path), + %% Succeeds the second time + ?assertEqual( + Path, + mkdtemp_n(F, 2)) + after rmtempdir(D) + end, + ok. + +mkdtemp_n_fail_test() -> + {ok, Cwd} = file:get_cwd(), + ?assertThrow( + {error, eexist}, + mkdtemp_n(fun () -> Cwd end, 1)), + ?assertThrow( + {error, eexist}, + mkdtemp_n(fun () -> Cwd end, 2)), + ok. + +make_dir_fail_test() -> + {ok, Cwd} = file:get_cwd(), + ?assertThrow( + {error, eexist}, + make_dir(Cwd)), + ok. + +mkdtemp_test() -> + D = mkdtemp(), + ?assertEqual( + true, + filelib:is_dir(D)), + ?assertEqual( + ok, + file:del_dir(D)), + ok. + +rmtempdir_test() -> + D1 = mkdtemp(), + ?assertEqual( + true, + filelib:is_dir(D1)), + ?assertEqual( + ok, + rmtempdir(D1)), + D2 = mkdtemp(), + ?assertEqual( + true, + filelib:is_dir(D2)), + ok = file:write_file(filename:join([D2, "foo"]), <<"bytes">>), + D3 = mkdtemp("suffix", "prefix", D2), + ?assertEqual( + true, + filelib:is_dir(D3)), + ok = file:write_file(filename:join([D3, "foo"]), <<"bytes">>), + ?assertEqual( + ok, + rmtempdir(D2)), + ?assertEqual( + {error, enoent}, + file:consult(D3)), + ?assertEqual( + {error, enoent}, + file:consult(D2)), + ok. + +gettempdir_env_test() -> + Env = pushenv(["TMPDIR", "TEMP", "TMP"]), + FalseEnv = [{"TMPDIR", false}, {"TEMP", false}, {"TMP", false}], + try + popenv(FalseEnv), + popenv([{"TMPDIR", "/"}]), + ?assertEqual( + "/", + os:getenv("TMPDIR")), + ?assertEqual( + "/", + gettempdir()), + {ok, Cwd} = file:get_cwd(), + popenv(FalseEnv), + popenv([{"TMP", Cwd}]), + ?assertEqual( + normalize_dir(Cwd), + gettempdir()) + after popenv(Env) + end, + ok. + +-endif. diff --git a/rabbitmq-server/plugins-src/mochiweb-wrapper/mochiweb-git/src/mochiutf8.erl b/rabbitmq-server/plugins-src/mochiweb-wrapper/mochiweb-git/src/mochiutf8.erl new file mode 100644 index 0000000..c9d2751 --- /dev/null +++ b/rabbitmq-server/plugins-src/mochiweb-wrapper/mochiweb-git/src/mochiutf8.erl @@ -0,0 +1,317 @@ +%% @copyright 2010 Mochi Media, Inc. +%% @author Bob Ippolito + +%% @doc Algorithm to convert any binary to a valid UTF-8 sequence by ignoring +%% invalid bytes. + +-module(mochiutf8). +-export([valid_utf8_bytes/1, codepoint_to_bytes/1, codepoints_to_bytes/1]). +-export([bytes_to_codepoints/1, bytes_foldl/3, codepoint_foldl/3]). +-export([read_codepoint/1, len/1]). + +%% External API + +%% -type unichar_low() :: 0..16#d7ff. +%% -type unichar_high() :: 16#e000..16#10ffff. +%% -type unichar() :: unichar_low() | unichar_high(). + +%% -spec codepoint_to_bytes(unichar()) -> binary(). +%% @doc Convert a unicode codepoint to UTF-8 bytes. +codepoint_to_bytes(C) when (C >= 16#00 andalso C =< 16#7f) -> + %% U+0000 - U+007F - 7 bits + <>; +codepoint_to_bytes(C) when (C >= 16#080 andalso C =< 16#07FF) -> + %% U+0080 - U+07FF - 11 bits + <<0:5, B1:5, B0:6>> = <>, + <<2#110:3, B1:5, + 2#10:2, B0:6>>; +codepoint_to_bytes(C) when (C >= 16#0800 andalso C =< 16#FFFF) andalso + (C < 16#D800 orelse C > 16#DFFF) -> + %% U+0800 - U+FFFF - 16 bits (excluding UTC-16 surrogate code points) + <> = <>, + <<2#1110:4, B2:4, + 2#10:2, B1:6, + 2#10:2, B0:6>>; +codepoint_to_bytes(C) when (C >= 16#010000 andalso C =< 16#10FFFF) -> + %% U+10000 - U+10FFFF - 21 bits + <<0:3, B3:3, B2:6, B1:6, B0:6>> = <>, + <<2#11110:5, B3:3, + 2#10:2, B2:6, + 2#10:2, B1:6, + 2#10:2, B0:6>>. + +%% -spec codepoints_to_bytes([unichar()]) -> binary(). +%% @doc Convert a list of codepoints to a UTF-8 binary. +codepoints_to_bytes(L) -> + <<<<(codepoint_to_bytes(C))/binary>> || C <- L>>. + +%% -spec read_codepoint(binary()) -> {unichar(), binary(), binary()}. +read_codepoint(Bin = <<2#0:1, C:7, Rest/binary>>) -> + %% U+0000 - U+007F - 7 bits + <> = Bin, + {C, B, Rest}; +read_codepoint(Bin = <<2#110:3, B1:5, + 2#10:2, B0:6, + Rest/binary>>) -> + %% U+0080 - U+07FF - 11 bits + case <> of + <> when C >= 16#80 -> + <> = Bin, + {C, B, Rest} + end; +read_codepoint(Bin = <<2#1110:4, B2:4, + 2#10:2, B1:6, + 2#10:2, B0:6, + Rest/binary>>) -> + %% U+0800 - U+FFFF - 16 bits (excluding UTC-16 surrogate code points) + case <> of + <> when (C >= 16#0800 andalso C =< 16#FFFF) andalso + (C < 16#D800 orelse C > 16#DFFF) -> + <> = Bin, + {C, B, Rest} + end; +read_codepoint(Bin = <<2#11110:5, B3:3, + 2#10:2, B2:6, + 2#10:2, B1:6, + 2#10:2, B0:6, + Rest/binary>>) -> + %% U+10000 - U+10FFFF - 21 bits + case <> of + <> when (C >= 16#010000 andalso C =< 16#10FFFF) -> + <> = Bin, + {C, B, Rest} + end. + +%% -spec codepoint_foldl(fun((unichar(), _) -> _), _, binary()) -> _. +codepoint_foldl(F, Acc, <<>>) when is_function(F, 2) -> + Acc; +codepoint_foldl(F, Acc, Bin) -> + {C, _, Rest} = read_codepoint(Bin), + codepoint_foldl(F, F(C, Acc), Rest). + +%% -spec bytes_foldl(fun((binary(), _) -> _), _, binary()) -> _. +bytes_foldl(F, Acc, <<>>) when is_function(F, 2) -> + Acc; +bytes_foldl(F, Acc, Bin) -> + {_, B, Rest} = read_codepoint(Bin), + bytes_foldl(F, F(B, Acc), Rest). + +%% -spec bytes_to_codepoints(binary()) -> [unichar()]. +bytes_to_codepoints(B) -> + lists:reverse(codepoint_foldl(fun (C, Acc) -> [C | Acc] end, [], B)). + +%% -spec len(binary()) -> non_neg_integer(). +len(<<>>) -> + 0; +len(B) -> + {_, _, Rest} = read_codepoint(B), + 1 + len(Rest). + +%% -spec valid_utf8_bytes(B::binary()) -> binary(). +%% @doc Return only the bytes in B that represent valid UTF-8. Uses +%% the following recursive algorithm: skip one byte if B does not +%% follow UTF-8 syntax (a 1-4 byte encoding of some number), +%% skip sequence of 2-4 bytes if it represents an overlong encoding +%% or bad code point (surrogate U+D800 - U+DFFF or > U+10FFFF). +valid_utf8_bytes(B) when is_binary(B) -> + binary_skip_bytes(B, invalid_utf8_indexes(B)). + +%% Internal API + +%% -spec binary_skip_bytes(binary(), [non_neg_integer()]) -> binary(). +%% @doc Return B, but skipping the 0-based indexes in L. +binary_skip_bytes(B, []) -> + B; +binary_skip_bytes(B, L) -> + binary_skip_bytes(B, L, 0, []). + +%% @private +%% -spec binary_skip_bytes(binary(), [non_neg_integer()], non_neg_integer(), iolist()) -> binary(). +binary_skip_bytes(B, [], _N, Acc) -> + iolist_to_binary(lists:reverse([B | Acc])); +binary_skip_bytes(<<_, RestB/binary>>, [N | RestL], N, Acc) -> + binary_skip_bytes(RestB, RestL, 1 + N, Acc); +binary_skip_bytes(<>, L, N, Acc) -> + binary_skip_bytes(RestB, L, 1 + N, [C | Acc]). + +%% -spec invalid_utf8_indexes(binary()) -> [non_neg_integer()]. +%% @doc Return the 0-based indexes in B that are not valid UTF-8. +invalid_utf8_indexes(B) -> + invalid_utf8_indexes(B, 0, []). + +%% @private. +%% -spec invalid_utf8_indexes(binary(), non_neg_integer(), [non_neg_integer()]) -> [non_neg_integer()]. +invalid_utf8_indexes(<>, N, Acc) when C < 16#80 -> + %% U+0000 - U+007F - 7 bits + invalid_utf8_indexes(Rest, 1 + N, Acc); +invalid_utf8_indexes(<>, N, Acc) + when C1 band 16#E0 =:= 16#C0, + C2 band 16#C0 =:= 16#80 -> + %% U+0080 - U+07FF - 11 bits + case ((C1 band 16#1F) bsl 6) bor (C2 band 16#3F) of + C when C < 16#80 -> + %% Overlong encoding. + invalid_utf8_indexes(Rest, 2 + N, [1 + N, N | Acc]); + _ -> + %% Upper bound U+07FF does not need to be checked + invalid_utf8_indexes(Rest, 2 + N, Acc) + end; +invalid_utf8_indexes(<>, N, Acc) + when C1 band 16#F0 =:= 16#E0, + C2 band 16#C0 =:= 16#80, + C3 band 16#C0 =:= 16#80 -> + %% U+0800 - U+FFFF - 16 bits + case ((((C1 band 16#0F) bsl 6) bor (C2 band 16#3F)) bsl 6) bor + (C3 band 16#3F) of + C when (C < 16#800) orelse (C >= 16#D800 andalso C =< 16#DFFF) -> + %% Overlong encoding or surrogate. + invalid_utf8_indexes(Rest, 3 + N, [2 + N, 1 + N, N | Acc]); + _ -> + %% Upper bound U+FFFF does not need to be checked + invalid_utf8_indexes(Rest, 3 + N, Acc) + end; +invalid_utf8_indexes(<>, N, Acc) + when C1 band 16#F8 =:= 16#F0, + C2 band 16#C0 =:= 16#80, + C3 band 16#C0 =:= 16#80, + C4 band 16#C0 =:= 16#80 -> + %% U+10000 - U+10FFFF - 21 bits + case ((((((C1 band 16#0F) bsl 6) bor (C2 band 16#3F)) bsl 6) bor + (C3 band 16#3F)) bsl 6) bor (C4 band 16#3F) of + C when (C < 16#10000) orelse (C > 16#10FFFF) -> + %% Overlong encoding or invalid code point. + invalid_utf8_indexes(Rest, 4 + N, [3 + N, 2 + N, 1 + N, N | Acc]); + _ -> + invalid_utf8_indexes(Rest, 4 + N, Acc) + end; +invalid_utf8_indexes(<<_, Rest/binary>>, N, Acc) -> + %% Invalid char + invalid_utf8_indexes(Rest, 1 + N, [N | Acc]); +invalid_utf8_indexes(<<>>, _N, Acc) -> + lists:reverse(Acc). + +%% +%% Tests +%% +-ifdef(TEST). +-include_lib("eunit/include/eunit.hrl"). + +binary_skip_bytes_test() -> + ?assertEqual(<<"foo">>, + binary_skip_bytes(<<"foo">>, [])), + ?assertEqual(<<"foobar">>, + binary_skip_bytes(<<"foo bar">>, [3])), + ?assertEqual(<<"foo">>, + binary_skip_bytes(<<"foo bar">>, [3, 4, 5, 6])), + ?assertEqual(<<"oo bar">>, + binary_skip_bytes(<<"foo bar">>, [0])), + ok. + +invalid_utf8_indexes_test() -> + ?assertEqual( + [], + invalid_utf8_indexes(<<"unicode snowman for you: ", 226, 152, 131>>)), + ?assertEqual( + [0], + invalid_utf8_indexes(<<128>>)), + ?assertEqual( + [57,59,60,64,66,67], + invalid_utf8_indexes(<<"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; (", + 167, 65, 170, 186, 73, 83, 80, 166, 87, 186, 217, 41, 41>>)), + ok. + +codepoint_to_bytes_test() -> + %% U+0000 - U+007F - 7 bits + %% U+0080 - U+07FF - 11 bits + %% U+0800 - U+FFFF - 16 bits (excluding UTC-16 surrogate code points) + %% U+10000 - U+10FFFF - 21 bits + ?assertEqual( + <<"a">>, + codepoint_to_bytes($a)), + ?assertEqual( + <<16#c2, 16#80>>, + codepoint_to_bytes(16#80)), + ?assertEqual( + <<16#df, 16#bf>>, + codepoint_to_bytes(16#07ff)), + ?assertEqual( + <<16#ef, 16#bf, 16#bf>>, + codepoint_to_bytes(16#ffff)), + ?assertEqual( + <<16#f4, 16#8f, 16#bf, 16#bf>>, + codepoint_to_bytes(16#10ffff)), + ok. + +bytes_foldl_test() -> + ?assertEqual( + <<"abc">>, + bytes_foldl(fun (B, Acc) -> <> end, <<>>, <<"abc">>)), + ?assertEqual( + <<"abc", 226, 152, 131, 228, 184, 173, 194, 133, 244,143,191,191>>, + bytes_foldl(fun (B, Acc) -> <> end, <<>>, + <<"abc", 226, 152, 131, 228, 184, 173, 194, 133, 244,143,191,191>>)), + ok. + +bytes_to_codepoints_test() -> + ?assertEqual( + "abc" ++ [16#2603, 16#4e2d, 16#85, 16#10ffff], + bytes_to_codepoints(<<"abc", 226, 152, 131, 228, 184, 173, 194, 133, 244,143,191,191>>)), + ok. + +codepoint_foldl_test() -> + ?assertEqual( + "cba", + codepoint_foldl(fun (C, Acc) -> [C | Acc] end, [], <<"abc">>)), + ?assertEqual( + [16#10ffff, 16#85, 16#4e2d, 16#2603 | "cba"], + codepoint_foldl(fun (C, Acc) -> [C | Acc] end, [], + <<"abc", 226, 152, 131, 228, 184, 173, 194, 133, 244,143,191,191>>)), + ok. + +len_test() -> + ?assertEqual( + 29, + len(<<"unicode snowman for you: ", 226, 152, 131, 228, 184, 173, 194, 133, 244, 143, 191, 191>>)), + ok. + +codepoints_to_bytes_test() -> + ?assertEqual( + iolist_to_binary(lists:map(fun codepoint_to_bytes/1, lists:seq(1, 1000))), + codepoints_to_bytes(lists:seq(1, 1000))), + ok. + +valid_utf8_bytes_test() -> + ?assertEqual( + <<"invalid U+11ffff: ">>, + valid_utf8_bytes(<<"invalid U+11ffff: ", 244, 159, 191, 191>>)), + ?assertEqual( + <<"U+10ffff: ", 244, 143, 191, 191>>, + valid_utf8_bytes(<<"U+10ffff: ", 244, 143, 191, 191>>)), + ?assertEqual( + <<"overlong 2-byte encoding (a): ">>, + valid_utf8_bytes(<<"overlong 2-byte encoding (a): ", 2#11000001, 2#10100001>>)), + ?assertEqual( + <<"overlong 2-byte encoding (!): ">>, + valid_utf8_bytes(<<"overlong 2-byte encoding (!): ", 2#11000000, 2#10100001>>)), + ?assertEqual( + <<"mu: ", 194, 181>>, + valid_utf8_bytes(<<"mu: ", 194, 181>>)), + ?assertEqual( + <<"bad coding bytes: ">>, + valid_utf8_bytes(<<"bad coding bytes: ", 2#10011111, 2#10111111, 2#11111111>>)), + ?assertEqual( + <<"low surrogate (unpaired): ">>, + valid_utf8_bytes(<<"low surrogate (unpaired): ", 237, 176, 128>>)), + ?assertEqual( + <<"high surrogate (unpaired): ">>, + valid_utf8_bytes(<<"high surrogate (unpaired): ", 237, 191, 191>>)), + ?assertEqual( + <<"unicode snowman for you: ", 226, 152, 131>>, + valid_utf8_bytes(<<"unicode snowman for you: ", 226, 152, 131>>)), + ?assertEqual( + <<"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; (AISPW))">>, + valid_utf8_bytes(<<"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; (", + 167, 65, 170, 186, 73, 83, 80, 166, 87, 186, 217, 41, 41>>)), + ok. + +-endif. diff --git a/rabbitmq-server/plugins-src/mochiweb-wrapper/mochiweb-git/src/mochiweb.app.src b/rabbitmq-server/plugins-src/mochiweb-wrapper/mochiweb-git/src/mochiweb.app.src new file mode 100644 index 0000000..4a6808e --- /dev/null +++ b/rabbitmq-server/plugins-src/mochiweb-wrapper/mochiweb-git/src/mochiweb.app.src @@ -0,0 +1,8 @@ +%% This is generated from src/mochiweb.app.src +{application, mochiweb, + [{description, "MochiMedia Web Server"}, + {vsn, "2.7.0"}, + {modules, []}, + {registered, []}, + {env, []}, + {applications, [kernel, stdlib, inets, xmerl]}]}. diff --git a/rabbitmq-server/plugins-src/mochiweb-wrapper/mochiweb-git/src/mochiweb.erl b/rabbitmq-server/plugins-src/mochiweb-wrapper/mochiweb-git/src/mochiweb.erl new file mode 100644 index 0000000..f597c73 --- /dev/null +++ b/rabbitmq-server/plugins-src/mochiweb-wrapper/mochiweb-git/src/mochiweb.erl @@ -0,0 +1,76 @@ +%% @author Bob Ippolito +%% @copyright 2007 Mochi Media, Inc. + +%% @doc Start and stop the MochiWeb server. + +-module(mochiweb). +-author('bob@mochimedia.com'). + +-export([new_request/1, new_response/1]). +-export([all_loaded/0, all_loaded/1, reload/0]). +-export([ensure_started/1]). + +reload() -> + [c:l(Module) || Module <- all_loaded()]. + +all_loaded() -> + all_loaded(filename:dirname(code:which(?MODULE))). + +all_loaded(Base) when is_atom(Base) -> + []; +all_loaded(Base) -> + FullBase = Base ++ "/", + F = fun ({_Module, Loaded}, Acc) when is_atom(Loaded) -> + Acc; + ({Module, Loaded}, Acc) -> + case lists:prefix(FullBase, Loaded) of + true -> + [Module | Acc]; + false -> + Acc + end + end, + lists:foldl(F, [], code:all_loaded()). + + +%% @spec new_request({Socket, Request, Headers}) -> MochiWebRequest +%% @doc Return a mochiweb_request data structure. +new_request({Socket, {Method, {abs_path, Uri}, Version}, Headers}) -> + mochiweb_request:new(Socket, + Method, + Uri, + Version, + mochiweb_headers:make(Headers)); +% this case probably doesn't "exist". +new_request({Socket, {Method, {absoluteURI, _Protocol, _Host, _Port, Uri}, + Version}, Headers}) -> + mochiweb_request:new(Socket, + Method, + Uri, + Version, + mochiweb_headers:make(Headers)); +%% Request-URI is "*" +%% From http://www.w3.org/Protocols/rfc2616/rfc2616-sec5.html#sec5.1.2 +new_request({Socket, {Method, '*'=Uri, Version}, Headers}) -> + mochiweb_request:new(Socket, + Method, + Uri, + Version, + mochiweb_headers:make(Headers)). + +%% @spec new_response({Request, integer(), Headers}) -> MochiWebResponse +%% @doc Return a mochiweb_response data structure. +new_response({Request, Code, Headers}) -> + mochiweb_response:new(Request, + Code, + mochiweb_headers:make(Headers)). + +%% @spec ensure_started(App::atom()) -> ok +%% @doc Start the given App if it has not been started already. +ensure_started(App) -> + case application:start(App) of + ok -> + ok; + {error, {already_started, App}} -> + ok + end. diff --git a/rabbitmq-server/plugins-src/mochiweb-wrapper/mochiweb-git/src/mochiweb_acceptor.erl b/rabbitmq-server/plugins-src/mochiweb-wrapper/mochiweb-git/src/mochiweb_acceptor.erl new file mode 100644 index 0000000..ebbaf45 --- /dev/null +++ b/rabbitmq-server/plugins-src/mochiweb-wrapper/mochiweb-git/src/mochiweb_acceptor.erl @@ -0,0 +1,50 @@ +%% @author Bob Ippolito +%% @copyright 2010 Mochi Media, Inc. + +%% @doc MochiWeb acceptor. + +-module(mochiweb_acceptor). +-author('bob@mochimedia.com'). + +-include("internal.hrl"). + +-export([start_link/3, init/3]). + +start_link(Server, Listen, Loop) -> + proc_lib:spawn_link(?MODULE, init, [Server, Listen, Loop]). + +init(Server, Listen, Loop) -> + T1 = os:timestamp(), + case catch mochiweb_socket:accept(Listen) of + {ok, Socket} -> + gen_server:cast(Server, {accepted, self(), timer:now_diff(os:timestamp(), T1)}), + call_loop(Loop, Socket); + {error, closed} -> + exit(normal); + {error, timeout} -> + init(Server, Listen, Loop); + {error, esslaccept} -> + exit(normal); + Other -> + error_logger:error_report( + [{application, mochiweb}, + "Accept failed error", + lists:flatten(io_lib:format("~p", [Other]))]), + exit({error, accept_failed}) + end. + +call_loop({M, F}, Socket) -> + M:F(Socket); +call_loop({M, F, [A1]}, Socket) -> + M:F(Socket, A1); +call_loop({M, F, A}, Socket) -> + erlang:apply(M, F, [Socket | A]); +call_loop(Loop, Socket) -> + Loop(Socket). + +%% +%% Tests +%% +-ifdef(TEST). +-include_lib("eunit/include/eunit.hrl"). +-endif. diff --git a/rabbitmq-server/plugins-src/mochiweb-wrapper/mochiweb-git/src/mochiweb_base64url.erl b/rabbitmq-server/plugins-src/mochiweb-wrapper/mochiweb-git/src/mochiweb_base64url.erl new file mode 100644 index 0000000..ab5aaec --- /dev/null +++ b/rabbitmq-server/plugins-src/mochiweb-wrapper/mochiweb-git/src/mochiweb_base64url.erl @@ -0,0 +1,83 @@ +-module(mochiweb_base64url). +-export([encode/1, decode/1]). +%% @doc URL and filename safe base64 variant with no padding, +%% also known as "base64url" per RFC 4648. +%% +%% This differs from base64 in the following ways: +%% '-' is used in place of '+' (62), +%% '_' is used in place of '/' (63), +%% padding is implicit rather than explicit ('='). + +-spec encode(iolist()) -> binary(). +encode(B) when is_binary(B) -> + encode_binary(B); +encode(L) when is_list(L) -> + encode_binary(iolist_to_binary(L)). + +-spec decode(iolist()) -> binary(). +decode(B) when is_binary(B) -> + decode_binary(B); +decode(L) when is_list(L) -> + decode_binary(iolist_to_binary(L)). + +%% Implementation, derived from stdlib base64.erl + +%% One-based decode map. +-define(DECODE_MAP, + {bad,bad,bad,bad,bad,bad,bad,bad,ws,ws,bad,bad,ws,bad,bad, %1-15 + bad,bad,bad,bad,bad,bad,bad,bad,bad,bad,bad,bad,bad,bad,bad,bad, %16-31 + ws,bad,bad,bad,bad,bad,bad,bad,bad,bad,bad,bad,bad,62,bad,bad, %32-47 + 52,53,54,55,56,57,58,59,60,61,bad,bad,bad,bad,bad,bad, %48-63 + bad,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14, %64-79 + 15,16,17,18,19,20,21,22,23,24,25,bad,bad,bad,bad,63, %80-95 + bad,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40, %96-111 + 41,42,43,44,45,46,47,48,49,50,51,bad,bad,bad,bad,bad, %112-127 + bad,bad,bad,bad,bad,bad,bad,bad,bad,bad,bad,bad,bad,bad,bad,bad, + bad,bad,bad,bad,bad,bad,bad,bad,bad,bad,bad,bad,bad,bad,bad,bad, + bad,bad,bad,bad,bad,bad,bad,bad,bad,bad,bad,bad,bad,bad,bad,bad, + bad,bad,bad,bad,bad,bad,bad,bad,bad,bad,bad,bad,bad,bad,bad,bad, + bad,bad,bad,bad,bad,bad,bad,bad,bad,bad,bad,bad,bad,bad,bad,bad, + bad,bad,bad,bad,bad,bad,bad,bad,bad,bad,bad,bad,bad,bad,bad,bad, + bad,bad,bad,bad,bad,bad,bad,bad,bad,bad,bad,bad,bad,bad,bad,bad, + bad,bad,bad,bad,bad,bad,bad,bad,bad,bad,bad,bad,bad,bad,bad,bad}). + +encode_binary(Bin) -> + Split = 3*(byte_size(Bin) div 3), + <> = Bin, + Main = << <<(b64e(C)):8>> || <> <= Main0 >>, + case Rest of + <> -> + <
>; + <> -> + <
>; + <<>> -> + Main + end. + +decode_binary(Bin) -> + Main = << <<(b64d(C)):6>> || <> <= Bin, + (C =/= $\t andalso C =/= $\s andalso + C =/= $\r andalso C =/= $\n) >>, + case bit_size(Main) rem 8 of + 0 -> + Main; + N -> + Split = byte_size(Main) - 1, + <> = Main, + Result + end. + +%% accessors + +b64e(X) -> + element(X+1, + {$A, $B, $C, $D, $E, $F, $G, $H, $I, $J, $K, $L, $M, $N, + $O, $P, $Q, $R, $S, $T, $U, $V, $W, $X, $Y, $Z, + $a, $b, $c, $d, $e, $f, $g, $h, $i, $j, $k, $l, $m, $n, + $o, $p, $q, $r, $s, $t, $u, $v, $w, $x, $y, $z, + $0, $1, $2, $3, $4, $5, $6, $7, $8, $9, $-, $_}). + +b64d(X) -> + b64d_ok(element(X, ?DECODE_MAP)). + +b64d_ok(I) when is_integer(I) -> I. diff --git a/rabbitmq-server/plugins-src/mochiweb-wrapper/mochiweb-git/src/mochiweb_charref.erl b/rabbitmq-server/plugins-src/mochiweb-wrapper/mochiweb-git/src/mochiweb_charref.erl new file mode 100644 index 0000000..665d0f9 --- /dev/null +++ b/rabbitmq-server/plugins-src/mochiweb-wrapper/mochiweb-git/src/mochiweb_charref.erl @@ -0,0 +1,2183 @@ +%% @author Bob Ippolito +%% @copyright 2007 Mochi Media, Inc. + +%% @doc Converts HTML 5 charrefs and entities to codepoints (or lists of code points). +-module(mochiweb_charref). +-export([charref/1]). + +%% External API. + +%% @doc Convert a decimal charref, hex charref, or html entity to a unicode +%% codepoint, or return undefined on failure. +%% The input should not include an ampersand or semicolon. +%% charref("#38") = 38, charref("#x26") = 38, charref("amp") = 38. +%% -spec charref(binary() | string()) -> integer() | [integer()] | undefined. +charref(B) when is_binary(B) -> + charref(binary_to_list(B)); +charref([$#, C | L]) when C =:= $x orelse C =:= $X -> + try erlang:list_to_integer(L, 16) + catch + error:badarg -> undefined + end; +charref([$# | L]) -> + try list_to_integer(L) + catch + error:badarg -> undefined + end; +charref(L) -> + entity(L). + +%% Internal API. + +%% [2011-10-14] Generated from: +%% http://www.w3.org/TR/html5/named-character-references.html + +entity("AElig") -> 16#000C6; +entity("AMP") -> 16#00026; +entity("Aacute") -> 16#000C1; +entity("Abreve") -> 16#00102; +entity("Acirc") -> 16#000C2; +entity("Acy") -> 16#00410; +entity("Afr") -> 16#1D504; +entity("Agrave") -> 16#000C0; +entity("Alpha") -> 16#00391; +entity("Amacr") -> 16#00100; +entity("And") -> 16#02A53; +entity("Aogon") -> 16#00104; +entity("Aopf") -> 16#1D538; +entity("ApplyFunction") -> 16#02061; +entity("Aring") -> 16#000C5; +entity("Ascr") -> 16#1D49C; +entity("Assign") -> 16#02254; +entity("Atilde") -> 16#000C3; +entity("Auml") -> 16#000C4; +entity("Backslash") -> 16#02216; +entity("Barv") -> 16#02AE7; +entity("Barwed") -> 16#02306; +entity("Bcy") -> 16#00411; +entity("Because") -> 16#02235; +entity("Bernoullis") -> 16#0212C; +entity("Beta") -> 16#00392; +entity("Bfr") -> 16#1D505; +entity("Bopf") -> 16#1D539; +entity("Breve") -> 16#002D8; +entity("Bscr") -> 16#0212C; +entity("Bumpeq") -> 16#0224E; +entity("CHcy") -> 16#00427; +entity("COPY") -> 16#000A9; +entity("Cacute") -> 16#00106; +entity("Cap") -> 16#022D2; +entity("CapitalDifferentialD") -> 16#02145; +entity("Cayleys") -> 16#0212D; +entity("Ccaron") -> 16#0010C; +entity("Ccedil") -> 16#000C7; +entity("Ccirc") -> 16#00108; +entity("Cconint") -> 16#02230; +entity("Cdot") -> 16#0010A; +entity("Cedilla") -> 16#000B8; +entity("CenterDot") -> 16#000B7; +entity("Cfr") -> 16#0212D; +entity("Chi") -> 16#003A7; +entity("CircleDot") -> 16#02299; +entity("CircleMinus") -> 16#02296; +entity("CirclePlus") -> 16#02295; +entity("CircleTimes") -> 16#02297; +entity("ClockwiseContourIntegral") -> 16#02232; +entity("CloseCurlyDoubleQuote") -> 16#0201D; +entity("CloseCurlyQuote") -> 16#02019; +entity("Colon") -> 16#02237; +entity("Colone") -> 16#02A74; +entity("Congruent") -> 16#02261; +entity("Conint") -> 16#0222F; +entity("ContourIntegral") -> 16#0222E; +entity("Copf") -> 16#02102; +entity("Coproduct") -> 16#02210; +entity("CounterClockwiseContourIntegral") -> 16#02233; +entity("Cross") -> 16#02A2F; +entity("Cscr") -> 16#1D49E; +entity("Cup") -> 16#022D3; +entity("CupCap") -> 16#0224D; +entity("DD") -> 16#02145; +entity("DDotrahd") -> 16#02911; +entity("DJcy") -> 16#00402; +entity("DScy") -> 16#00405; +entity("DZcy") -> 16#0040F; +entity("Dagger") -> 16#02021; +entity("Darr") -> 16#021A1; +entity("Dashv") -> 16#02AE4; +entity("Dcaron") -> 16#0010E; +entity("Dcy") -> 16#00414; +entity("Del") -> 16#02207; +entity("Delta") -> 16#00394; +entity("Dfr") -> 16#1D507; +entity("DiacriticalAcute") -> 16#000B4; +entity("DiacriticalDot") -> 16#002D9; +entity("DiacriticalDoubleAcute") -> 16#002DD; +entity("DiacriticalGrave") -> 16#00060; +entity("DiacriticalTilde") -> 16#002DC; +entity("Diamond") -> 16#022C4; +entity("DifferentialD") -> 16#02146; +entity("Dopf") -> 16#1D53B; +entity("Dot") -> 16#000A8; +entity("DotDot") -> 16#020DC; +entity("DotEqual") -> 16#02250; +entity("DoubleContourIntegral") -> 16#0222F; +entity("DoubleDot") -> 16#000A8; +entity("DoubleDownArrow") -> 16#021D3; +entity("DoubleLeftArrow") -> 16#021D0; +entity("DoubleLeftRightArrow") -> 16#021D4; +entity("DoubleLeftTee") -> 16#02AE4; +entity("DoubleLongLeftArrow") -> 16#027F8; +entity("DoubleLongLeftRightArrow") -> 16#027FA; +entity("DoubleLongRightArrow") -> 16#027F9; +entity("DoubleRightArrow") -> 16#021D2; +entity("DoubleRightTee") -> 16#022A8; +entity("DoubleUpArrow") -> 16#021D1; +entity("DoubleUpDownArrow") -> 16#021D5; +entity("DoubleVerticalBar") -> 16#02225; +entity("DownArrow") -> 16#02193; +entity("DownArrowBar") -> 16#02913; +entity("DownArrowUpArrow") -> 16#021F5; +entity("DownBreve") -> 16#00311; +entity("DownLeftRightVector") -> 16#02950; +entity("DownLeftTeeVector") -> 16#0295E; +entity("DownLeftVector") -> 16#021BD; +entity("DownLeftVectorBar") -> 16#02956; +entity("DownRightTeeVector") -> 16#0295F; +entity("DownRightVector") -> 16#021C1; +entity("DownRightVectorBar") -> 16#02957; +entity("DownTee") -> 16#022A4; +entity("DownTeeArrow") -> 16#021A7; +entity("Downarrow") -> 16#021D3; +entity("Dscr") -> 16#1D49F; +entity("Dstrok") -> 16#00110; +entity("ENG") -> 16#0014A; +entity("ETH") -> 16#000D0; +entity("Eacute") -> 16#000C9; +entity("Ecaron") -> 16#0011A; +entity("Ecirc") -> 16#000CA; +entity("Ecy") -> 16#0042D; +entity("Edot") -> 16#00116; +entity("Efr") -> 16#1D508; +entity("Egrave") -> 16#000C8; +entity("Element") -> 16#02208; +entity("Emacr") -> 16#00112; +entity("EmptySmallSquare") -> 16#025FB; +entity("EmptyVerySmallSquare") -> 16#025AB; +entity("Eogon") -> 16#00118; +entity("Eopf") -> 16#1D53C; +entity("Epsilon") -> 16#00395; +entity("Equal") -> 16#02A75; +entity("EqualTilde") -> 16#02242; +entity("Equilibrium") -> 16#021CC; +entity("Escr") -> 16#02130; +entity("Esim") -> 16#02A73; +entity("Eta") -> 16#00397; +entity("Euml") -> 16#000CB; +entity("Exists") -> 16#02203; +entity("ExponentialE") -> 16#02147; +entity("Fcy") -> 16#00424; +entity("Ffr") -> 16#1D509; +entity("FilledSmallSquare") -> 16#025FC; +entity("FilledVerySmallSquare") -> 16#025AA; +entity("Fopf") -> 16#1D53D; +entity("ForAll") -> 16#02200; +entity("Fouriertrf") -> 16#02131; +entity("Fscr") -> 16#02131; +entity("GJcy") -> 16#00403; +entity("GT") -> 16#0003E; +entity("Gamma") -> 16#00393; +entity("Gammad") -> 16#003DC; +entity("Gbreve") -> 16#0011E; +entity("Gcedil") -> 16#00122; +entity("Gcirc") -> 16#0011C; +entity("Gcy") -> 16#00413; +entity("Gdot") -> 16#00120; +entity("Gfr") -> 16#1D50A; +entity("Gg") -> 16#022D9; +entity("Gopf") -> 16#1D53E; +entity("GreaterEqual") -> 16#02265; +entity("GreaterEqualLess") -> 16#022DB; +entity("GreaterFullEqual") -> 16#02267; +entity("GreaterGreater") -> 16#02AA2; +entity("GreaterLess") -> 16#02277; +entity("GreaterSlantEqual") -> 16#02A7E; +entity("GreaterTilde") -> 16#02273; +entity("Gscr") -> 16#1D4A2; +entity("Gt") -> 16#0226B; +entity("HARDcy") -> 16#0042A; +entity("Hacek") -> 16#002C7; +entity("Hat") -> 16#0005E; +entity("Hcirc") -> 16#00124; +entity("Hfr") -> 16#0210C; +entity("HilbertSpace") -> 16#0210B; +entity("Hopf") -> 16#0210D; +entity("HorizontalLine") -> 16#02500; +entity("Hscr") -> 16#0210B; +entity("Hstrok") -> 16#00126; +entity("HumpDownHump") -> 16#0224E; +entity("HumpEqual") -> 16#0224F; +entity("IEcy") -> 16#00415; +entity("IJlig") -> 16#00132; +entity("IOcy") -> 16#00401; +entity("Iacute") -> 16#000CD; +entity("Icirc") -> 16#000CE; +entity("Icy") -> 16#00418; +entity("Idot") -> 16#00130; +entity("Ifr") -> 16#02111; +entity("Igrave") -> 16#000CC; +entity("Im") -> 16#02111; +entity("Imacr") -> 16#0012A; +entity("ImaginaryI") -> 16#02148; +entity("Implies") -> 16#021D2; +entity("Int") -> 16#0222C; +entity("Integral") -> 16#0222B; +entity("Intersection") -> 16#022C2; +entity("InvisibleComma") -> 16#02063; +entity("InvisibleTimes") -> 16#02062; +entity("Iogon") -> 16#0012E; +entity("Iopf") -> 16#1D540; +entity("Iota") -> 16#00399; +entity("Iscr") -> 16#02110; +entity("Itilde") -> 16#00128; +entity("Iukcy") -> 16#00406; +entity("Iuml") -> 16#000CF; +entity("Jcirc") -> 16#00134; +entity("Jcy") -> 16#00419; +entity("Jfr") -> 16#1D50D; +entity("Jopf") -> 16#1D541; +entity("Jscr") -> 16#1D4A5; +entity("Jsercy") -> 16#00408; +entity("Jukcy") -> 16#00404; +entity("KHcy") -> 16#00425; +entity("KJcy") -> 16#0040C; +entity("Kappa") -> 16#0039A; +entity("Kcedil") -> 16#00136; +entity("Kcy") -> 16#0041A; +entity("Kfr") -> 16#1D50E; +entity("Kopf") -> 16#1D542; +entity("Kscr") -> 16#1D4A6; +entity("LJcy") -> 16#00409; +entity("LT") -> 16#0003C; +entity("Lacute") -> 16#00139; +entity("Lambda") -> 16#0039B; +entity("Lang") -> 16#027EA; +entity("Laplacetrf") -> 16#02112; +entity("Larr") -> 16#0219E; +entity("Lcaron") -> 16#0013D; +entity("Lcedil") -> 16#0013B; +entity("Lcy") -> 16#0041B; +entity("LeftAngleBracket") -> 16#027E8; +entity("LeftArrow") -> 16#02190; +entity("LeftArrowBar") -> 16#021E4; +entity("LeftArrowRightArrow") -> 16#021C6; +entity("LeftCeiling") -> 16#02308; +entity("LeftDoubleBracket") -> 16#027E6; +entity("LeftDownTeeVector") -> 16#02961; +entity("LeftDownVector") -> 16#021C3; +entity("LeftDownVectorBar") -> 16#02959; +entity("LeftFloor") -> 16#0230A; +entity("LeftRightArrow") -> 16#02194; +entity("LeftRightVector") -> 16#0294E; +entity("LeftTee") -> 16#022A3; +entity("LeftTeeArrow") -> 16#021A4; +entity("LeftTeeVector") -> 16#0295A; +entity("LeftTriangle") -> 16#022B2; +entity("LeftTriangleBar") -> 16#029CF; +entity("LeftTriangleEqual") -> 16#022B4; +entity("LeftUpDownVector") -> 16#02951; +entity("LeftUpTeeVector") -> 16#02960; +entity("LeftUpVector") -> 16#021BF; +entity("LeftUpVectorBar") -> 16#02958; +entity("LeftVector") -> 16#021BC; +entity("LeftVectorBar") -> 16#02952; +entity("Leftarrow") -> 16#021D0; +entity("Leftrightarrow") -> 16#021D4; +entity("LessEqualGreater") -> 16#022DA; +entity("LessFullEqual") -> 16#02266; +entity("LessGreater") -> 16#02276; +entity("LessLess") -> 16#02AA1; +entity("LessSlantEqual") -> 16#02A7D; +entity("LessTilde") -> 16#02272; +entity("Lfr") -> 16#1D50F; +entity("Ll") -> 16#022D8; +entity("Lleftarrow") -> 16#021DA; +entity("Lmidot") -> 16#0013F; +entity("LongLeftArrow") -> 16#027F5; +entity("LongLeftRightArrow") -> 16#027F7; +entity("LongRightArrow") -> 16#027F6; +entity("Longleftarrow") -> 16#027F8; +entity("Longleftrightarrow") -> 16#027FA; +entity("Longrightarrow") -> 16#027F9; +entity("Lopf") -> 16#1D543; +entity("LowerLeftArrow") -> 16#02199; +entity("LowerRightArrow") -> 16#02198; +entity("Lscr") -> 16#02112; +entity("Lsh") -> 16#021B0; +entity("Lstrok") -> 16#00141; +entity("Lt") -> 16#0226A; +entity("Map") -> 16#02905; +entity("Mcy") -> 16#0041C; +entity("MediumSpace") -> 16#0205F; +entity("Mellintrf") -> 16#02133; +entity("Mfr") -> 16#1D510; +entity("MinusPlus") -> 16#02213; +entity("Mopf") -> 16#1D544; +entity("Mscr") -> 16#02133; +entity("Mu") -> 16#0039C; +entity("NJcy") -> 16#0040A; +entity("Nacute") -> 16#00143; +entity("Ncaron") -> 16#00147; +entity("Ncedil") -> 16#00145; +entity("Ncy") -> 16#0041D; +entity("NegativeMediumSpace") -> 16#0200B; +entity("NegativeThickSpace") -> 16#0200B; +entity("NegativeThinSpace") -> 16#0200B; +entity("NegativeVeryThinSpace") -> 16#0200B; +entity("NestedGreaterGreater") -> 16#0226B; +entity("NestedLessLess") -> 16#0226A; +entity("NewLine") -> 16#0000A; +entity("Nfr") -> 16#1D511; +entity("NoBreak") -> 16#02060; +entity("NonBreakingSpace") -> 16#000A0; +entity("Nopf") -> 16#02115; +entity("Not") -> 16#02AEC; +entity("NotCongruent") -> 16#02262; +entity("NotCupCap") -> 16#0226D; +entity("NotDoubleVerticalBar") -> 16#02226; +entity("NotElement") -> 16#02209; +entity("NotEqual") -> 16#02260; +entity("NotEqualTilde") -> [16#02242, 16#00338]; +entity("NotExists") -> 16#02204; +entity("NotGreater") -> 16#0226F; +entity("NotGreaterEqual") -> 16#02271; +entity("NotGreaterFullEqual") -> [16#02267, 16#00338]; +entity("NotGreaterGreater") -> [16#0226B, 16#00338]; +entity("NotGreaterLess") -> 16#02279; +entity("NotGreaterSlantEqual") -> [16#02A7E, 16#00338]; +entity("NotGreaterTilde") -> 16#02275; +entity("NotHumpDownHump") -> [16#0224E, 16#00338]; +entity("NotHumpEqual") -> [16#0224F, 16#00338]; +entity("NotLeftTriangle") -> 16#022EA; +entity("NotLeftTriangleBar") -> [16#029CF, 16#00338]; +entity("NotLeftTriangleEqual") -> 16#022EC; +entity("NotLess") -> 16#0226E; +entity("NotLessEqual") -> 16#02270; +entity("NotLessGreater") -> 16#02278; +entity("NotLessLess") -> [16#0226A, 16#00338]; +entity("NotLessSlantEqual") -> [16#02A7D, 16#00338]; +entity("NotLessTilde") -> 16#02274; +entity("NotNestedGreaterGreater") -> [16#02AA2, 16#00338]; +entity("NotNestedLessLess") -> [16#02AA1, 16#00338]; +entity("NotPrecedes") -> 16#02280; +entity("NotPrecedesEqual") -> [16#02AAF, 16#00338]; +entity("NotPrecedesSlantEqual") -> 16#022E0; +entity("NotReverseElement") -> 16#0220C; +entity("NotRightTriangle") -> 16#022EB; +entity("NotRightTriangleBar") -> [16#029D0, 16#00338]; +entity("NotRightTriangleEqual") -> 16#022ED; +entity("NotSquareSubset") -> [16#0228F, 16#00338]; +entity("NotSquareSubsetEqual") -> 16#022E2; +entity("NotSquareSuperset") -> [16#02290, 16#00338]; +entity("NotSquareSupersetEqual") -> 16#022E3; +entity("NotSubset") -> [16#02282, 16#020D2]; +entity("NotSubsetEqual") -> 16#02288; +entity("NotSucceeds") -> 16#02281; +entity("NotSucceedsEqual") -> [16#02AB0, 16#00338]; +entity("NotSucceedsSlantEqual") -> 16#022E1; +entity("NotSucceedsTilde") -> [16#0227F, 16#00338]; +entity("NotSuperset") -> [16#02283, 16#020D2]; +entity("NotSupersetEqual") -> 16#02289; +entity("NotTilde") -> 16#02241; +entity("NotTildeEqual") -> 16#02244; +entity("NotTildeFullEqual") -> 16#02247; +entity("NotTildeTilde") -> 16#02249; +entity("NotVerticalBar") -> 16#02224; +entity("Nscr") -> 16#1D4A9; +entity("Ntilde") -> 16#000D1; +entity("Nu") -> 16#0039D; +entity("OElig") -> 16#00152; +entity("Oacute") -> 16#000D3; +entity("Ocirc") -> 16#000D4; +entity("Ocy") -> 16#0041E; +entity("Odblac") -> 16#00150; +entity("Ofr") -> 16#1D512; +entity("Ograve") -> 16#000D2; +entity("Omacr") -> 16#0014C; +entity("Omega") -> 16#003A9; +entity("Omicron") -> 16#0039F; +entity("Oopf") -> 16#1D546; +entity("OpenCurlyDoubleQuote") -> 16#0201C; +entity("OpenCurlyQuote") -> 16#02018; +entity("Or") -> 16#02A54; +entity("Oscr") -> 16#1D4AA; +entity("Oslash") -> 16#000D8; +entity("Otilde") -> 16#000D5; +entity("Otimes") -> 16#02A37; +entity("Ouml") -> 16#000D6; +entity("OverBar") -> 16#0203E; +entity("OverBrace") -> 16#023DE; +entity("OverBracket") -> 16#023B4; +entity("OverParenthesis") -> 16#023DC; +entity("PartialD") -> 16#02202; +entity("Pcy") -> 16#0041F; +entity("Pfr") -> 16#1D513; +entity("Phi") -> 16#003A6; +entity("Pi") -> 16#003A0; +entity("PlusMinus") -> 16#000B1; +entity("Poincareplane") -> 16#0210C; +entity("Popf") -> 16#02119; +entity("Pr") -> 16#02ABB; +entity("Precedes") -> 16#0227A; +entity("PrecedesEqual") -> 16#02AAF; +entity("PrecedesSlantEqual") -> 16#0227C; +entity("PrecedesTilde") -> 16#0227E; +entity("Prime") -> 16#02033; +entity("Product") -> 16#0220F; +entity("Proportion") -> 16#02237; +entity("Proportional") -> 16#0221D; +entity("Pscr") -> 16#1D4AB; +entity("Psi") -> 16#003A8; +entity("QUOT") -> 16#00022; +entity("Qfr") -> 16#1D514; +entity("Qopf") -> 16#0211A; +entity("Qscr") -> 16#1D4AC; +entity("RBarr") -> 16#02910; +entity("REG") -> 16#000AE; +entity("Racute") -> 16#00154; +entity("Rang") -> 16#027EB; +entity("Rarr") -> 16#021A0; +entity("Rarrtl") -> 16#02916; +entity("Rcaron") -> 16#00158; +entity("Rcedil") -> 16#00156; +entity("Rcy") -> 16#00420; +entity("Re") -> 16#0211C; +entity("ReverseElement") -> 16#0220B; +entity("ReverseEquilibrium") -> 16#021CB; +entity("ReverseUpEquilibrium") -> 16#0296F; +entity("Rfr") -> 16#0211C; +entity("Rho") -> 16#003A1; +entity("RightAngleBracket") -> 16#027E9; +entity("RightArrow") -> 16#02192; +entity("RightArrowBar") -> 16#021E5; +entity("RightArrowLeftArrow") -> 16#021C4; +entity("RightCeiling") -> 16#02309; +entity("RightDoubleBracket") -> 16#027E7; +entity("RightDownTeeVector") -> 16#0295D; +entity("RightDownVector") -> 16#021C2; +entity("RightDownVectorBar") -> 16#02955; +entity("RightFloor") -> 16#0230B; +entity("RightTee") -> 16#022A2; +entity("RightTeeArrow") -> 16#021A6; +entity("RightTeeVector") -> 16#0295B; +entity("RightTriangle") -> 16#022B3; +entity("RightTriangleBar") -> 16#029D0; +entity("RightTriangleEqual") -> 16#022B5; +entity("RightUpDownVector") -> 16#0294F; +entity("RightUpTeeVector") -> 16#0295C; +entity("RightUpVector") -> 16#021BE; +entity("RightUpVectorBar") -> 16#02954; +entity("RightVector") -> 16#021C0; +entity("RightVectorBar") -> 16#02953; +entity("Rightarrow") -> 16#021D2; +entity("Ropf") -> 16#0211D; +entity("RoundImplies") -> 16#02970; +entity("Rrightarrow") -> 16#021DB; +entity("Rscr") -> 16#0211B; +entity("Rsh") -> 16#021B1; +entity("RuleDelayed") -> 16#029F4; +entity("SHCHcy") -> 16#00429; +entity("SHcy") -> 16#00428; +entity("SOFTcy") -> 16#0042C; +entity("Sacute") -> 16#0015A; +entity("Sc") -> 16#02ABC; +entity("Scaron") -> 16#00160; +entity("Scedil") -> 16#0015E; +entity("Scirc") -> 16#0015C; +entity("Scy") -> 16#00421; +entity("Sfr") -> 16#1D516; +entity("ShortDownArrow") -> 16#02193; +entity("ShortLeftArrow") -> 16#02190; +entity("ShortRightArrow") -> 16#02192; +entity("ShortUpArrow") -> 16#02191; +entity("Sigma") -> 16#003A3; +entity("SmallCircle") -> 16#02218; +entity("Sopf") -> 16#1D54A; +entity("Sqrt") -> 16#0221A; +entity("Square") -> 16#025A1; +entity("SquareIntersection") -> 16#02293; +entity("SquareSubset") -> 16#0228F; +entity("SquareSubsetEqual") -> 16#02291; +entity("SquareSuperset") -> 16#02290; +entity("SquareSupersetEqual") -> 16#02292; +entity("SquareUnion") -> 16#02294; +entity("Sscr") -> 16#1D4AE; +entity("Star") -> 16#022C6; +entity("Sub") -> 16#022D0; +entity("Subset") -> 16#022D0; +entity("SubsetEqual") -> 16#02286; +entity("Succeeds") -> 16#0227B; +entity("SucceedsEqual") -> 16#02AB0; +entity("SucceedsSlantEqual") -> 16#0227D; +entity("SucceedsTilde") -> 16#0227F; +entity("SuchThat") -> 16#0220B; +entity("Sum") -> 16#02211; +entity("Sup") -> 16#022D1; +entity("Superset") -> 16#02283; +entity("SupersetEqual") -> 16#02287; +entity("Supset") -> 16#022D1; +entity("THORN") -> 16#000DE; +entity("TRADE") -> 16#02122; +entity("TSHcy") -> 16#0040B; +entity("TScy") -> 16#00426; +entity("Tab") -> 16#00009; +entity("Tau") -> 16#003A4; +entity("Tcaron") -> 16#00164; +entity("Tcedil") -> 16#00162; +entity("Tcy") -> 16#00422; +entity("Tfr") -> 16#1D517; +entity("Therefore") -> 16#02234; +entity("Theta") -> 16#00398; +entity("ThickSpace") -> [16#0205F, 16#0200A]; +entity("ThinSpace") -> 16#02009; +entity("Tilde") -> 16#0223C; +entity("TildeEqual") -> 16#02243; +entity("TildeFullEqual") -> 16#02245; +entity("TildeTilde") -> 16#02248; +entity("Topf") -> 16#1D54B; +entity("TripleDot") -> 16#020DB; +entity("Tscr") -> 16#1D4AF; +entity("Tstrok") -> 16#00166; +entity("Uacute") -> 16#000DA; +entity("Uarr") -> 16#0219F; +entity("Uarrocir") -> 16#02949; +entity("Ubrcy") -> 16#0040E; +entity("Ubreve") -> 16#0016C; +entity("Ucirc") -> 16#000DB; +entity("Ucy") -> 16#00423; +entity("Udblac") -> 16#00170; +entity("Ufr") -> 16#1D518; +entity("Ugrave") -> 16#000D9; +entity("Umacr") -> 16#0016A; +entity("UnderBar") -> 16#0005F; +entity("UnderBrace") -> 16#023DF; +entity("UnderBracket") -> 16#023B5; +entity("UnderParenthesis") -> 16#023DD; +entity("Union") -> 16#022C3; +entity("UnionPlus") -> 16#0228E; +entity("Uogon") -> 16#00172; +entity("Uopf") -> 16#1D54C; +entity("UpArrow") -> 16#02191; +entity("UpArrowBar") -> 16#02912; +entity("UpArrowDownArrow") -> 16#021C5; +entity("UpDownArrow") -> 16#02195; +entity("UpEquilibrium") -> 16#0296E; +entity("UpTee") -> 16#022A5; +entity("UpTeeArrow") -> 16#021A5; +entity("Uparrow") -> 16#021D1; +entity("Updownarrow") -> 16#021D5; +entity("UpperLeftArrow") -> 16#02196; +entity("UpperRightArrow") -> 16#02197; +entity("Upsi") -> 16#003D2; +entity("Upsilon") -> 16#003A5; +entity("Uring") -> 16#0016E; +entity("Uscr") -> 16#1D4B0; +entity("Utilde") -> 16#00168; +entity("Uuml") -> 16#000DC; +entity("VDash") -> 16#022AB; +entity("Vbar") -> 16#02AEB; +entity("Vcy") -> 16#00412; +entity("Vdash") -> 16#022A9; +entity("Vdashl") -> 16#02AE6; +entity("Vee") -> 16#022C1; +entity("Verbar") -> 16#02016; +entity("Vert") -> 16#02016; +entity("VerticalBar") -> 16#02223; +entity("VerticalLine") -> 16#0007C; +entity("VerticalSeparator") -> 16#02758; +entity("VerticalTilde") -> 16#02240; +entity("VeryThinSpace") -> 16#0200A; +entity("Vfr") -> 16#1D519; +entity("Vopf") -> 16#1D54D; +entity("Vscr") -> 16#1D4B1; +entity("Vvdash") -> 16#022AA; +entity("Wcirc") -> 16#00174; +entity("Wedge") -> 16#022C0; +entity("Wfr") -> 16#1D51A; +entity("Wopf") -> 16#1D54E; +entity("Wscr") -> 16#1D4B2; +entity("Xfr") -> 16#1D51B; +entity("Xi") -> 16#0039E; +entity("Xopf") -> 16#1D54F; +entity("Xscr") -> 16#1D4B3; +entity("YAcy") -> 16#0042F; +entity("YIcy") -> 16#00407; +entity("YUcy") -> 16#0042E; +entity("Yacute") -> 16#000DD; +entity("Ycirc") -> 16#00176; +entity("Ycy") -> 16#0042B; +entity("Yfr") -> 16#1D51C; +entity("Yopf") -> 16#1D550; +entity("Yscr") -> 16#1D4B4; +entity("Yuml") -> 16#00178; +entity("ZHcy") -> 16#00416; +entity("Zacute") -> 16#00179; +entity("Zcaron") -> 16#0017D; +entity("Zcy") -> 16#00417; +entity("Zdot") -> 16#0017B; +entity("ZeroWidthSpace") -> 16#0200B; +entity("Zeta") -> 16#00396; +entity("Zfr") -> 16#02128; +entity("Zopf") -> 16#02124; +entity("Zscr") -> 16#1D4B5; +entity("aacute") -> 16#000E1; +entity("abreve") -> 16#00103; +entity("ac") -> 16#0223E; +entity("acE") -> [16#0223E, 16#00333]; +entity("acd") -> 16#0223F; +entity("acirc") -> 16#000E2; +entity("acute") -> 16#000B4; +entity("acy") -> 16#00430; +entity("aelig") -> 16#000E6; +entity("af") -> 16#02061; +entity("afr") -> 16#1D51E; +entity("agrave") -> 16#000E0; +entity("alefsym") -> 16#02135; +entity("aleph") -> 16#02135; +entity("alpha") -> 16#003B1; +entity("amacr") -> 16#00101; +entity("amalg") -> 16#02A3F; +entity("amp") -> 16#00026; +entity("and") -> 16#02227; +entity("andand") -> 16#02A55; +entity("andd") -> 16#02A5C; +entity("andslope") -> 16#02A58; +entity("andv") -> 16#02A5A; +entity("ang") -> 16#02220; +entity("ange") -> 16#029A4; +entity("angle") -> 16#02220; +entity("angmsd") -> 16#02221; +entity("angmsdaa") -> 16#029A8; +entity("angmsdab") -> 16#029A9; +entity("angmsdac") -> 16#029AA; +entity("angmsdad") -> 16#029AB; +entity("angmsdae") -> 16#029AC; +entity("angmsdaf") -> 16#029AD; +entity("angmsdag") -> 16#029AE; +entity("angmsdah") -> 16#029AF; +entity("angrt") -> 16#0221F; +entity("angrtvb") -> 16#022BE; +entity("angrtvbd") -> 16#0299D; +entity("angsph") -> 16#02222; +entity("angst") -> 16#000C5; +entity("angzarr") -> 16#0237C; +entity("aogon") -> 16#00105; +entity("aopf") -> 16#1D552; +entity("ap") -> 16#02248; +entity("apE") -> 16#02A70; +entity("apacir") -> 16#02A6F; +entity("ape") -> 16#0224A; +entity("apid") -> 16#0224B; +entity("apos") -> 16#00027; +entity("approx") -> 16#02248; +entity("approxeq") -> 16#0224A; +entity("aring") -> 16#000E5; +entity("ascr") -> 16#1D4B6; +entity("ast") -> 16#0002A; +entity("asymp") -> 16#02248; +entity("asympeq") -> 16#0224D; +entity("atilde") -> 16#000E3; +entity("auml") -> 16#000E4; +entity("awconint") -> 16#02233; +entity("awint") -> 16#02A11; +entity("bNot") -> 16#02AED; +entity("backcong") -> 16#0224C; +entity("backepsilon") -> 16#003F6; +entity("backprime") -> 16#02035; +entity("backsim") -> 16#0223D; +entity("backsimeq") -> 16#022CD; +entity("barvee") -> 16#022BD; +entity("barwed") -> 16#02305; +entity("barwedge") -> 16#02305; +entity("bbrk") -> 16#023B5; +entity("bbrktbrk") -> 16#023B6; +entity("bcong") -> 16#0224C; +entity("bcy") -> 16#00431; +entity("bdquo") -> 16#0201E; +entity("becaus") -> 16#02235; +entity("because") -> 16#02235; +entity("bemptyv") -> 16#029B0; +entity("bepsi") -> 16#003F6; +entity("bernou") -> 16#0212C; +entity("beta") -> 16#003B2; +entity("beth") -> 16#02136; +entity("between") -> 16#0226C; +entity("bfr") -> 16#1D51F; +entity("bigcap") -> 16#022C2; +entity("bigcirc") -> 16#025EF; +entity("bigcup") -> 16#022C3; +entity("bigodot") -> 16#02A00; +entity("bigoplus") -> 16#02A01; +entity("bigotimes") -> 16#02A02; +entity("bigsqcup") -> 16#02A06; +entity("bigstar") -> 16#02605; +entity("bigtriangledown") -> 16#025BD; +entity("bigtriangleup") -> 16#025B3; +entity("biguplus") -> 16#02A04; +entity("bigvee") -> 16#022C1; +entity("bigwedge") -> 16#022C0; +entity("bkarow") -> 16#0290D; +entity("blacklozenge") -> 16#029EB; +entity("blacksquare") -> 16#025AA; +entity("blacktriangle") -> 16#025B4; +entity("blacktriangledown") -> 16#025BE; +entity("blacktriangleleft") -> 16#025C2; +entity("blacktriangleright") -> 16#025B8; +entity("blank") -> 16#02423; +entity("blk12") -> 16#02592; +entity("blk14") -> 16#02591; +entity("blk34") -> 16#02593; +entity("block") -> 16#02588; +entity("bne") -> [16#0003D, 16#020E5]; +entity("bnequiv") -> [16#02261, 16#020E5]; +entity("bnot") -> 16#02310; +entity("bopf") -> 16#1D553; +entity("bot") -> 16#022A5; +entity("bottom") -> 16#022A5; +entity("bowtie") -> 16#022C8; +entity("boxDL") -> 16#02557; +entity("boxDR") -> 16#02554; +entity("boxDl") -> 16#02556; +entity("boxDr") -> 16#02553; +entity("boxH") -> 16#02550; +entity("boxHD") -> 16#02566; +entity("boxHU") -> 16#02569; +entity("boxHd") -> 16#02564; +entity("boxHu") -> 16#02567; +entity("boxUL") -> 16#0255D; +entity("boxUR") -> 16#0255A; +entity("boxUl") -> 16#0255C; +entity("boxUr") -> 16#02559; +entity("boxV") -> 16#02551; +entity("boxVH") -> 16#0256C; +entity("boxVL") -> 16#02563; +entity("boxVR") -> 16#02560; +entity("boxVh") -> 16#0256B; +entity("boxVl") -> 16#02562; +entity("boxVr") -> 16#0255F; +entity("boxbox") -> 16#029C9; +entity("boxdL") -> 16#02555; +entity("boxdR") -> 16#02552; +entity("boxdl") -> 16#02510; +entity("boxdr") -> 16#0250C; +entity("boxh") -> 16#02500; +entity("boxhD") -> 16#02565; +entity("boxhU") -> 16#02568; +entity("boxhd") -> 16#0252C; +entity("boxhu") -> 16#02534; +entity("boxminus") -> 16#0229F; +entity("boxplus") -> 16#0229E; +entity("boxtimes") -> 16#022A0; +entity("boxuL") -> 16#0255B; +entity("boxuR") -> 16#02558; +entity("boxul") -> 16#02518; +entity("boxur") -> 16#02514; +entity("boxv") -> 16#02502; +entity("boxvH") -> 16#0256A; +entity("boxvL") -> 16#02561; +entity("boxvR") -> 16#0255E; +entity("boxvh") -> 16#0253C; +entity("boxvl") -> 16#02524; +entity("boxvr") -> 16#0251C; +entity("bprime") -> 16#02035; +entity("breve") -> 16#002D8; +entity("brvbar") -> 16#000A6; +entity("bscr") -> 16#1D4B7; +entity("bsemi") -> 16#0204F; +entity("bsim") -> 16#0223D; +entity("bsime") -> 16#022CD; +entity("bsol") -> 16#0005C; +entity("bsolb") -> 16#029C5; +entity("bsolhsub") -> 16#027C8; +entity("bull") -> 16#02022; +entity("bullet") -> 16#02022; +entity("bump") -> 16#0224E; +entity("bumpE") -> 16#02AAE; +entity("bumpe") -> 16#0224F; +entity("bumpeq") -> 16#0224F; +entity("cacute") -> 16#00107; +entity("cap") -> 16#02229; +entity("capand") -> 16#02A44; +entity("capbrcup") -> 16#02A49; +entity("capcap") -> 16#02A4B; +entity("capcup") -> 16#02A47; +entity("capdot") -> 16#02A40; +entity("caps") -> [16#02229, 16#0FE00]; +entity("caret") -> 16#02041; +entity("caron") -> 16#002C7; +entity("ccaps") -> 16#02A4D; +entity("ccaron") -> 16#0010D; +entity("ccedil") -> 16#000E7; +entity("ccirc") -> 16#00109; +entity("ccups") -> 16#02A4C; +entity("ccupssm") -> 16#02A50; +entity("cdot") -> 16#0010B; +entity("cedil") -> 16#000B8; +entity("cemptyv") -> 16#029B2; +entity("cent") -> 16#000A2; +entity("centerdot") -> 16#000B7; +entity("cfr") -> 16#1D520; +entity("chcy") -> 16#00447; +entity("check") -> 16#02713; +entity("checkmark") -> 16#02713; +entity("chi") -> 16#003C7; +entity("cir") -> 16#025CB; +entity("cirE") -> 16#029C3; +entity("circ") -> 16#002C6; +entity("circeq") -> 16#02257; +entity("circlearrowleft") -> 16#021BA; +entity("circlearrowright") -> 16#021BB; +entity("circledR") -> 16#000AE; +entity("circledS") -> 16#024C8; +entity("circledast") -> 16#0229B; +entity("circledcirc") -> 16#0229A; +entity("circleddash") -> 16#0229D; +entity("cire") -> 16#02257; +entity("cirfnint") -> 16#02A10; +entity("cirmid") -> 16#02AEF; +entity("cirscir") -> 16#029C2; +entity("clubs") -> 16#02663; +entity("clubsuit") -> 16#02663; +entity("colon") -> 16#0003A; +entity("colone") -> 16#02254; +entity("coloneq") -> 16#02254; +entity("comma") -> 16#0002C; +entity("commat") -> 16#00040; +entity("comp") -> 16#02201; +entity("compfn") -> 16#02218; +entity("complement") -> 16#02201; +entity("complexes") -> 16#02102; +entity("cong") -> 16#02245; +entity("congdot") -> 16#02A6D; +entity("conint") -> 16#0222E; +entity("copf") -> 16#1D554; +entity("coprod") -> 16#02210; +entity("copy") -> 16#000A9; +entity("copysr") -> 16#02117; +entity("crarr") -> 16#021B5; +entity("cross") -> 16#02717; +entity("cscr") -> 16#1D4B8; +entity("csub") -> 16#02ACF; +entity("csube") -> 16#02AD1; +entity("csup") -> 16#02AD0; +entity("csupe") -> 16#02AD2; +entity("ctdot") -> 16#022EF; +entity("cudarrl") -> 16#02938; +entity("cudarrr") -> 16#02935; +entity("cuepr") -> 16#022DE; +entity("cuesc") -> 16#022DF; +entity("cularr") -> 16#021B6; +entity("cularrp") -> 16#0293D; +entity("cup") -> 16#0222A; +entity("cupbrcap") -> 16#02A48; +entity("cupcap") -> 16#02A46; +entity("cupcup") -> 16#02A4A; +entity("cupdot") -> 16#0228D; +entity("cupor") -> 16#02A45; +entity("cups") -> [16#0222A, 16#0FE00]; +entity("curarr") -> 16#021B7; +entity("curarrm") -> 16#0293C; +entity("curlyeqprec") -> 16#022DE; +entity("curlyeqsucc") -> 16#022DF; +entity("curlyvee") -> 16#022CE; +entity("curlywedge") -> 16#022CF; +entity("curren") -> 16#000A4; +entity("curvearrowleft") -> 16#021B6; +entity("curvearrowright") -> 16#021B7; +entity("cuvee") -> 16#022CE; +entity("cuwed") -> 16#022CF; +entity("cwconint") -> 16#02232; +entity("cwint") -> 16#02231; +entity("cylcty") -> 16#0232D; +entity("dArr") -> 16#021D3; +entity("dHar") -> 16#02965; +entity("dagger") -> 16#02020; +entity("daleth") -> 16#02138; +entity("darr") -> 16#02193; +entity("dash") -> 16#02010; +entity("dashv") -> 16#022A3; +entity("dbkarow") -> 16#0290F; +entity("dblac") -> 16#002DD; +entity("dcaron") -> 16#0010F; +entity("dcy") -> 16#00434; +entity("dd") -> 16#02146; +entity("ddagger") -> 16#02021; +entity("ddarr") -> 16#021CA; +entity("ddotseq") -> 16#02A77; +entity("deg") -> 16#000B0; +entity("delta") -> 16#003B4; +entity("demptyv") -> 16#029B1; +entity("dfisht") -> 16#0297F; +entity("dfr") -> 16#1D521; +entity("dharl") -> 16#021C3; +entity("dharr") -> 16#021C2; +entity("diam") -> 16#022C4; +entity("diamond") -> 16#022C4; +entity("diamondsuit") -> 16#02666; +entity("diams") -> 16#02666; +entity("die") -> 16#000A8; +entity("digamma") -> 16#003DD; +entity("disin") -> 16#022F2; +entity("div") -> 16#000F7; +entity("divide") -> 16#000F7; +entity("divideontimes") -> 16#022C7; +entity("divonx") -> 16#022C7; +entity("djcy") -> 16#00452; +entity("dlcorn") -> 16#0231E; +entity("dlcrop") -> 16#0230D; +entity("dollar") -> 16#00024; +entity("dopf") -> 16#1D555; +entity("dot") -> 16#002D9; +entity("doteq") -> 16#02250; +entity("doteqdot") -> 16#02251; +entity("dotminus") -> 16#02238; +entity("dotplus") -> 16#02214; +entity("dotsquare") -> 16#022A1; +entity("doublebarwedge") -> 16#02306; +entity("downarrow") -> 16#02193; +entity("downdownarrows") -> 16#021CA; +entity("downharpoonleft") -> 16#021C3; +entity("downharpoonright") -> 16#021C2; +entity("drbkarow") -> 16#02910; +entity("drcorn") -> 16#0231F; +entity("drcrop") -> 16#0230C; +entity("dscr") -> 16#1D4B9; +entity("dscy") -> 16#00455; +entity("dsol") -> 16#029F6; +entity("dstrok") -> 16#00111; +entity("dtdot") -> 16#022F1; +entity("dtri") -> 16#025BF; +entity("dtrif") -> 16#025BE; +entity("duarr") -> 16#021F5; +entity("duhar") -> 16#0296F; +entity("dwangle") -> 16#029A6; +entity("dzcy") -> 16#0045F; +entity("dzigrarr") -> 16#027FF; +entity("eDDot") -> 16#02A77; +entity("eDot") -> 16#02251; +entity("eacute") -> 16#000E9; +entity("easter") -> 16#02A6E; +entity("ecaron") -> 16#0011B; +entity("ecir") -> 16#02256; +entity("ecirc") -> 16#000EA; +entity("ecolon") -> 16#02255; +entity("ecy") -> 16#0044D; +entity("edot") -> 16#00117; +entity("ee") -> 16#02147; +entity("efDot") -> 16#02252; +entity("efr") -> 16#1D522; +entity("eg") -> 16#02A9A; +entity("egrave") -> 16#000E8; +entity("egs") -> 16#02A96; +entity("egsdot") -> 16#02A98; +entity("el") -> 16#02A99; +entity("elinters") -> 16#023E7; +entity("ell") -> 16#02113; +entity("els") -> 16#02A95; +entity("elsdot") -> 16#02A97; +entity("emacr") -> 16#00113; +entity("empty") -> 16#02205; +entity("emptyset") -> 16#02205; +entity("emptyv") -> 16#02205; +entity("emsp") -> 16#02003; +entity("emsp13") -> 16#02004; +entity("emsp14") -> 16#02005; +entity("eng") -> 16#0014B; +entity("ensp") -> 16#02002; +entity("eogon") -> 16#00119; +entity("eopf") -> 16#1D556; +entity("epar") -> 16#022D5; +entity("eparsl") -> 16#029E3; +entity("eplus") -> 16#02A71; +entity("epsi") -> 16#003B5; +entity("epsilon") -> 16#003B5; +entity("epsiv") -> 16#003F5; +entity("eqcirc") -> 16#02256; +entity("eqcolon") -> 16#02255; +entity("eqsim") -> 16#02242; +entity("eqslantgtr") -> 16#02A96; +entity("eqslantless") -> 16#02A95; +entity("equals") -> 16#0003D; +entity("equest") -> 16#0225F; +entity("equiv") -> 16#02261; +entity("equivDD") -> 16#02A78; +entity("eqvparsl") -> 16#029E5; +entity("erDot") -> 16#02253; +entity("erarr") -> 16#02971; +entity("escr") -> 16#0212F; +entity("esdot") -> 16#02250; +entity("esim") -> 16#02242; +entity("eta") -> 16#003B7; +entity("eth") -> 16#000F0; +entity("euml") -> 16#000EB; +entity("euro") -> 16#020AC; +entity("excl") -> 16#00021; +entity("exist") -> 16#02203; +entity("expectation") -> 16#02130; +entity("exponentiale") -> 16#02147; +entity("fallingdotseq") -> 16#02252; +entity("fcy") -> 16#00444; +entity("female") -> 16#02640; +entity("ffilig") -> 16#0FB03; +entity("fflig") -> 16#0FB00; +entity("ffllig") -> 16#0FB04; +entity("ffr") -> 16#1D523; +entity("filig") -> 16#0FB01; +entity("fjlig") -> [16#00066, 16#0006A]; +entity("flat") -> 16#0266D; +entity("fllig") -> 16#0FB02; +entity("fltns") -> 16#025B1; +entity("fnof") -> 16#00192; +entity("fopf") -> 16#1D557; +entity("forall") -> 16#02200; +entity("fork") -> 16#022D4; +entity("forkv") -> 16#02AD9; +entity("fpartint") -> 16#02A0D; +entity("frac12") -> 16#000BD; +entity("frac13") -> 16#02153; +entity("frac14") -> 16#000BC; +entity("frac15") -> 16#02155; +entity("frac16") -> 16#02159; +entity("frac18") -> 16#0215B; +entity("frac23") -> 16#02154; +entity("frac25") -> 16#02156; +entity("frac34") -> 16#000BE; +entity("frac35") -> 16#02157; +entity("frac38") -> 16#0215C; +entity("frac45") -> 16#02158; +entity("frac56") -> 16#0215A; +entity("frac58") -> 16#0215D; +entity("frac78") -> 16#0215E; +entity("frasl") -> 16#02044; +entity("frown") -> 16#02322; +entity("fscr") -> 16#1D4BB; +entity("gE") -> 16#02267; +entity("gEl") -> 16#02A8C; +entity("gacute") -> 16#001F5; +entity("gamma") -> 16#003B3; +entity("gammad") -> 16#003DD; +entity("gap") -> 16#02A86; +entity("gbreve") -> 16#0011F; +entity("gcirc") -> 16#0011D; +entity("gcy") -> 16#00433; +entity("gdot") -> 16#00121; +entity("ge") -> 16#02265; +entity("gel") -> 16#022DB; +entity("geq") -> 16#02265; +entity("geqq") -> 16#02267; +entity("geqslant") -> 16#02A7E; +entity("ges") -> 16#02A7E; +entity("gescc") -> 16#02AA9; +entity("gesdot") -> 16#02A80; +entity("gesdoto") -> 16#02A82; +entity("gesdotol") -> 16#02A84; +entity("gesl") -> [16#022DB, 16#0FE00]; +entity("gesles") -> 16#02A94; +entity("gfr") -> 16#1D524; +entity("gg") -> 16#0226B; +entity("ggg") -> 16#022D9; +entity("gimel") -> 16#02137; +entity("gjcy") -> 16#00453; +entity("gl") -> 16#02277; +entity("glE") -> 16#02A92; +entity("gla") -> 16#02AA5; +entity("glj") -> 16#02AA4; +entity("gnE") -> 16#02269; +entity("gnap") -> 16#02A8A; +entity("gnapprox") -> 16#02A8A; +entity("gne") -> 16#02A88; +entity("gneq") -> 16#02A88; +entity("gneqq") -> 16#02269; +entity("gnsim") -> 16#022E7; +entity("gopf") -> 16#1D558; +entity("grave") -> 16#00060; +entity("gscr") -> 16#0210A; +entity("gsim") -> 16#02273; +entity("gsime") -> 16#02A8E; +entity("gsiml") -> 16#02A90; +entity("gt") -> 16#0003E; +entity("gtcc") -> 16#02AA7; +entity("gtcir") -> 16#02A7A; +entity("gtdot") -> 16#022D7; +entity("gtlPar") -> 16#02995; +entity("gtquest") -> 16#02A7C; +entity("gtrapprox") -> 16#02A86; +entity("gtrarr") -> 16#02978; +entity("gtrdot") -> 16#022D7; +entity("gtreqless") -> 16#022DB; +entity("gtreqqless") -> 16#02A8C; +entity("gtrless") -> 16#02277; +entity("gtrsim") -> 16#02273; +entity("gvertneqq") -> [16#02269, 16#0FE00]; +entity("gvnE") -> [16#02269, 16#0FE00]; +entity("hArr") -> 16#021D4; +entity("hairsp") -> 16#0200A; +entity("half") -> 16#000BD; +entity("hamilt") -> 16#0210B; +entity("hardcy") -> 16#0044A; +entity("harr") -> 16#02194; +entity("harrcir") -> 16#02948; +entity("harrw") -> 16#021AD; +entity("hbar") -> 16#0210F; +entity("hcirc") -> 16#00125; +entity("hearts") -> 16#02665; +entity("heartsuit") -> 16#02665; +entity("hellip") -> 16#02026; +entity("hercon") -> 16#022B9; +entity("hfr") -> 16#1D525; +entity("hksearow") -> 16#02925; +entity("hkswarow") -> 16#02926; +entity("hoarr") -> 16#021FF; +entity("homtht") -> 16#0223B; +entity("hookleftarrow") -> 16#021A9; +entity("hookrightarrow") -> 16#021AA; +entity("hopf") -> 16#1D559; +entity("horbar") -> 16#02015; +entity("hscr") -> 16#1D4BD; +entity("hslash") -> 16#0210F; +entity("hstrok") -> 16#00127; +entity("hybull") -> 16#02043; +entity("hyphen") -> 16#02010; +entity("iacute") -> 16#000ED; +entity("ic") -> 16#02063; +entity("icirc") -> 16#000EE; +entity("icy") -> 16#00438; +entity("iecy") -> 16#00435; +entity("iexcl") -> 16#000A1; +entity("iff") -> 16#021D4; +entity("ifr") -> 16#1D526; +entity("igrave") -> 16#000EC; +entity("ii") -> 16#02148; +entity("iiiint") -> 16#02A0C; +entity("iiint") -> 16#0222D; +entity("iinfin") -> 16#029DC; +entity("iiota") -> 16#02129; +entity("ijlig") -> 16#00133; +entity("imacr") -> 16#0012B; +entity("image") -> 16#02111; +entity("imagline") -> 16#02110; +entity("imagpart") -> 16#02111; +entity("imath") -> 16#00131; +entity("imof") -> 16#022B7; +entity("imped") -> 16#001B5; +entity("in") -> 16#02208; +entity("incare") -> 16#02105; +entity("infin") -> 16#0221E; +entity("infintie") -> 16#029DD; +entity("inodot") -> 16#00131; +entity("int") -> 16#0222B; +entity("intcal") -> 16#022BA; +entity("integers") -> 16#02124; +entity("intercal") -> 16#022BA; +entity("intlarhk") -> 16#02A17; +entity("intprod") -> 16#02A3C; +entity("iocy") -> 16#00451; +entity("iogon") -> 16#0012F; +entity("iopf") -> 16#1D55A; +entity("iota") -> 16#003B9; +entity("iprod") -> 16#02A3C; +entity("iquest") -> 16#000BF; +entity("iscr") -> 16#1D4BE; +entity("isin") -> 16#02208; +entity("isinE") -> 16#022F9; +entity("isindot") -> 16#022F5; +entity("isins") -> 16#022F4; +entity("isinsv") -> 16#022F3; +entity("isinv") -> 16#02208; +entity("it") -> 16#02062; +entity("itilde") -> 16#00129; +entity("iukcy") -> 16#00456; +entity("iuml") -> 16#000EF; +entity("jcirc") -> 16#00135; +entity("jcy") -> 16#00439; +entity("jfr") -> 16#1D527; +entity("jmath") -> 16#00237; +entity("jopf") -> 16#1D55B; +entity("jscr") -> 16#1D4BF; +entity("jsercy") -> 16#00458; +entity("jukcy") -> 16#00454; +entity("kappa") -> 16#003BA; +entity("kappav") -> 16#003F0; +entity("kcedil") -> 16#00137; +entity("kcy") -> 16#0043A; +entity("kfr") -> 16#1D528; +entity("kgreen") -> 16#00138; +entity("khcy") -> 16#00445; +entity("kjcy") -> 16#0045C; +entity("kopf") -> 16#1D55C; +entity("kscr") -> 16#1D4C0; +entity("lAarr") -> 16#021DA; +entity("lArr") -> 16#021D0; +entity("lAtail") -> 16#0291B; +entity("lBarr") -> 16#0290E; +entity("lE") -> 16#02266; +entity("lEg") -> 16#02A8B; +entity("lHar") -> 16#02962; +entity("lacute") -> 16#0013A; +entity("laemptyv") -> 16#029B4; +entity("lagran") -> 16#02112; +entity("lambda") -> 16#003BB; +entity("lang") -> 16#027E8; +entity("langd") -> 16#02991; +entity("langle") -> 16#027E8; +entity("lap") -> 16#02A85; +entity("laquo") -> 16#000AB; +entity("larr") -> 16#02190; +entity("larrb") -> 16#021E4; +entity("larrbfs") -> 16#0291F; +entity("larrfs") -> 16#0291D; +entity("larrhk") -> 16#021A9; +entity("larrlp") -> 16#021AB; +entity("larrpl") -> 16#02939; +entity("larrsim") -> 16#02973; +entity("larrtl") -> 16#021A2; +entity("lat") -> 16#02AAB; +entity("latail") -> 16#02919; +entity("late") -> 16#02AAD; +entity("lates") -> [16#02AAD, 16#0FE00]; +entity("lbarr") -> 16#0290C; +entity("lbbrk") -> 16#02772; +entity("lbrace") -> 16#0007B; +entity("lbrack") -> 16#0005B; +entity("lbrke") -> 16#0298B; +entity("lbrksld") -> 16#0298F; +entity("lbrkslu") -> 16#0298D; +entity("lcaron") -> 16#0013E; +entity("lcedil") -> 16#0013C; +entity("lceil") -> 16#02308; +entity("lcub") -> 16#0007B; +entity("lcy") -> 16#0043B; +entity("ldca") -> 16#02936; +entity("ldquo") -> 16#0201C; +entity("ldquor") -> 16#0201E; +entity("ldrdhar") -> 16#02967; +entity("ldrushar") -> 16#0294B; +entity("ldsh") -> 16#021B2; +entity("le") -> 16#02264; +entity("leftarrow") -> 16#02190; +entity("leftarrowtail") -> 16#021A2; +entity("leftharpoondown") -> 16#021BD; +entity("leftharpoonup") -> 16#021BC; +entity("leftleftarrows") -> 16#021C7; +entity("leftrightarrow") -> 16#02194; +entity("leftrightarrows") -> 16#021C6; +entity("leftrightharpoons") -> 16#021CB; +entity("leftrightsquigarrow") -> 16#021AD; +entity("leftthreetimes") -> 16#022CB; +entity("leg") -> 16#022DA; +entity("leq") -> 16#02264; +entity("leqq") -> 16#02266; +entity("leqslant") -> 16#02A7D; +entity("les") -> 16#02A7D; +entity("lescc") -> 16#02AA8; +entity("lesdot") -> 16#02A7F; +entity("lesdoto") -> 16#02A81; +entity("lesdotor") -> 16#02A83; +entity("lesg") -> [16#022DA, 16#0FE00]; +entity("lesges") -> 16#02A93; +entity("lessapprox") -> 16#02A85; +entity("lessdot") -> 16#022D6; +entity("lesseqgtr") -> 16#022DA; +entity("lesseqqgtr") -> 16#02A8B; +entity("lessgtr") -> 16#02276; +entity("lesssim") -> 16#02272; +entity("lfisht") -> 16#0297C; +entity("lfloor") -> 16#0230A; +entity("lfr") -> 16#1D529; +entity("lg") -> 16#02276; +entity("lgE") -> 16#02A91; +entity("lhard") -> 16#021BD; +entity("lharu") -> 16#021BC; +entity("lharul") -> 16#0296A; +entity("lhblk") -> 16#02584; +entity("ljcy") -> 16#00459; +entity("ll") -> 16#0226A; +entity("llarr") -> 16#021C7; +entity("llcorner") -> 16#0231E; +entity("llhard") -> 16#0296B; +entity("lltri") -> 16#025FA; +entity("lmidot") -> 16#00140; +entity("lmoust") -> 16#023B0; +entity("lmoustache") -> 16#023B0; +entity("lnE") -> 16#02268; +entity("lnap") -> 16#02A89; +entity("lnapprox") -> 16#02A89; +entity("lne") -> 16#02A87; +entity("lneq") -> 16#02A87; +entity("lneqq") -> 16#02268; +entity("lnsim") -> 16#022E6; +entity("loang") -> 16#027EC; +entity("loarr") -> 16#021FD; +entity("lobrk") -> 16#027E6; +entity("longleftarrow") -> 16#027F5; +entity("longleftrightarrow") -> 16#027F7; +entity("longmapsto") -> 16#027FC; +entity("longrightarrow") -> 16#027F6; +entity("looparrowleft") -> 16#021AB; +entity("looparrowright") -> 16#021AC; +entity("lopar") -> 16#02985; +entity("lopf") -> 16#1D55D; +entity("loplus") -> 16#02A2D; +entity("lotimes") -> 16#02A34; +entity("lowast") -> 16#02217; +entity("lowbar") -> 16#0005F; +entity("loz") -> 16#025CA; +entity("lozenge") -> 16#025CA; +entity("lozf") -> 16#029EB; +entity("lpar") -> 16#00028; +entity("lparlt") -> 16#02993; +entity("lrarr") -> 16#021C6; +entity("lrcorner") -> 16#0231F; +entity("lrhar") -> 16#021CB; +entity("lrhard") -> 16#0296D; +entity("lrm") -> 16#0200E; +entity("lrtri") -> 16#022BF; +entity("lsaquo") -> 16#02039; +entity("lscr") -> 16#1D4C1; +entity("lsh") -> 16#021B0; +entity("lsim") -> 16#02272; +entity("lsime") -> 16#02A8D; +entity("lsimg") -> 16#02A8F; +entity("lsqb") -> 16#0005B; +entity("lsquo") -> 16#02018; +entity("lsquor") -> 16#0201A; +entity("lstrok") -> 16#00142; +entity("lt") -> 16#0003C; +entity("ltcc") -> 16#02AA6; +entity("ltcir") -> 16#02A79; +entity("ltdot") -> 16#022D6; +entity("lthree") -> 16#022CB; +entity("ltimes") -> 16#022C9; +entity("ltlarr") -> 16#02976; +entity("ltquest") -> 16#02A7B; +entity("ltrPar") -> 16#02996; +entity("ltri") -> 16#025C3; +entity("ltrie") -> 16#022B4; +entity("ltrif") -> 16#025C2; +entity("lurdshar") -> 16#0294A; +entity("luruhar") -> 16#02966; +entity("lvertneqq") -> [16#02268, 16#0FE00]; +entity("lvnE") -> [16#02268, 16#0FE00]; +entity("mDDot") -> 16#0223A; +entity("macr") -> 16#000AF; +entity("male") -> 16#02642; +entity("malt") -> 16#02720; +entity("maltese") -> 16#02720; +entity("map") -> 16#021A6; +entity("mapsto") -> 16#021A6; +entity("mapstodown") -> 16#021A7; +entity("mapstoleft") -> 16#021A4; +entity("mapstoup") -> 16#021A5; +entity("marker") -> 16#025AE; +entity("mcomma") -> 16#02A29; +entity("mcy") -> 16#0043C; +entity("mdash") -> 16#02014; +entity("measuredangle") -> 16#02221; +entity("mfr") -> 16#1D52A; +entity("mho") -> 16#02127; +entity("micro") -> 16#000B5; +entity("mid") -> 16#02223; +entity("midast") -> 16#0002A; +entity("midcir") -> 16#02AF0; +entity("middot") -> 16#000B7; +entity("minus") -> 16#02212; +entity("minusb") -> 16#0229F; +entity("minusd") -> 16#02238; +entity("minusdu") -> 16#02A2A; +entity("mlcp") -> 16#02ADB; +entity("mldr") -> 16#02026; +entity("mnplus") -> 16#02213; +entity("models") -> 16#022A7; +entity("mopf") -> 16#1D55E; +entity("mp") -> 16#02213; +entity("mscr") -> 16#1D4C2; +entity("mstpos") -> 16#0223E; +entity("mu") -> 16#003BC; +entity("multimap") -> 16#022B8; +entity("mumap") -> 16#022B8; +entity("nGg") -> [16#022D9, 16#00338]; +entity("nGt") -> [16#0226B, 16#020D2]; +entity("nGtv") -> [16#0226B, 16#00338]; +entity("nLeftarrow") -> 16#021CD; +entity("nLeftrightarrow") -> 16#021CE; +entity("nLl") -> [16#022D8, 16#00338]; +entity("nLt") -> [16#0226A, 16#020D2]; +entity("nLtv") -> [16#0226A, 16#00338]; +entity("nRightarrow") -> 16#021CF; +entity("nVDash") -> 16#022AF; +entity("nVdash") -> 16#022AE; +entity("nabla") -> 16#02207; +entity("nacute") -> 16#00144; +entity("nang") -> [16#02220, 16#020D2]; +entity("nap") -> 16#02249; +entity("napE") -> [16#02A70, 16#00338]; +entity("napid") -> [16#0224B, 16#00338]; +entity("napos") -> 16#00149; +entity("napprox") -> 16#02249; +entity("natur") -> 16#0266E; +entity("natural") -> 16#0266E; +entity("naturals") -> 16#02115; +entity("nbsp") -> 16#000A0; +entity("nbump") -> [16#0224E, 16#00338]; +entity("nbumpe") -> [16#0224F, 16#00338]; +entity("ncap") -> 16#02A43; +entity("ncaron") -> 16#00148; +entity("ncedil") -> 16#00146; +entity("ncong") -> 16#02247; +entity("ncongdot") -> [16#02A6D, 16#00338]; +entity("ncup") -> 16#02A42; +entity("ncy") -> 16#0043D; +entity("ndash") -> 16#02013; +entity("ne") -> 16#02260; +entity("neArr") -> 16#021D7; +entity("nearhk") -> 16#02924; +entity("nearr") -> 16#02197; +entity("nearrow") -> 16#02197; +entity("nedot") -> [16#02250, 16#00338]; +entity("nequiv") -> 16#02262; +entity("nesear") -> 16#02928; +entity("nesim") -> [16#02242, 16#00338]; +entity("nexist") -> 16#02204; +entity("nexists") -> 16#02204; +entity("nfr") -> 16#1D52B; +entity("ngE") -> [16#02267, 16#00338]; +entity("nge") -> 16#02271; +entity("ngeq") -> 16#02271; +entity("ngeqq") -> [16#02267, 16#00338]; +entity("ngeqslant") -> [16#02A7E, 16#00338]; +entity("nges") -> [16#02A7E, 16#00338]; +entity("ngsim") -> 16#02275; +entity("ngt") -> 16#0226F; +entity("ngtr") -> 16#0226F; +entity("nhArr") -> 16#021CE; +entity("nharr") -> 16#021AE; +entity("nhpar") -> 16#02AF2; +entity("ni") -> 16#0220B; +entity("nis") -> 16#022FC; +entity("nisd") -> 16#022FA; +entity("niv") -> 16#0220B; +entity("njcy") -> 16#0045A; +entity("nlArr") -> 16#021CD; +entity("nlE") -> [16#02266, 16#00338]; +entity("nlarr") -> 16#0219A; +entity("nldr") -> 16#02025; +entity("nle") -> 16#02270; +entity("nleftarrow") -> 16#0219A; +entity("nleftrightarrow") -> 16#021AE; +entity("nleq") -> 16#02270; +entity("nleqq") -> [16#02266, 16#00338]; +entity("nleqslant") -> [16#02A7D, 16#00338]; +entity("nles") -> [16#02A7D, 16#00338]; +entity("nless") -> 16#0226E; +entity("nlsim") -> 16#02274; +entity("nlt") -> 16#0226E; +entity("nltri") -> 16#022EA; +entity("nltrie") -> 16#022EC; +entity("nmid") -> 16#02224; +entity("nopf") -> 16#1D55F; +entity("not") -> 16#000AC; +entity("notin") -> 16#02209; +entity("notinE") -> [16#022F9, 16#00338]; +entity("notindot") -> [16#022F5, 16#00338]; +entity("notinva") -> 16#02209; +entity("notinvb") -> 16#022F7; +entity("notinvc") -> 16#022F6; +entity("notni") -> 16#0220C; +entity("notniva") -> 16#0220C; +entity("notnivb") -> 16#022FE; +entity("notnivc") -> 16#022FD; +entity("npar") -> 16#02226; +entity("nparallel") -> 16#02226; +entity("nparsl") -> [16#02AFD, 16#020E5]; +entity("npart") -> [16#02202, 16#00338]; +entity("npolint") -> 16#02A14; +entity("npr") -> 16#02280; +entity("nprcue") -> 16#022E0; +entity("npre") -> [16#02AAF, 16#00338]; +entity("nprec") -> 16#02280; +entity("npreceq") -> [16#02AAF, 16#00338]; +entity("nrArr") -> 16#021CF; +entity("nrarr") -> 16#0219B; +entity("nrarrc") -> [16#02933, 16#00338]; +entity("nrarrw") -> [16#0219D, 16#00338]; +entity("nrightarrow") -> 16#0219B; +entity("nrtri") -> 16#022EB; +entity("nrtrie") -> 16#022ED; +entity("nsc") -> 16#02281; +entity("nsccue") -> 16#022E1; +entity("nsce") -> [16#02AB0, 16#00338]; +entity("nscr") -> 16#1D4C3; +entity("nshortmid") -> 16#02224; +entity("nshortparallel") -> 16#02226; +entity("nsim") -> 16#02241; +entity("nsime") -> 16#02244; +entity("nsimeq") -> 16#02244; +entity("nsmid") -> 16#02224; +entity("nspar") -> 16#02226; +entity("nsqsube") -> 16#022E2; +entity("nsqsupe") -> 16#022E3; +entity("nsub") -> 16#02284; +entity("nsubE") -> [16#02AC5, 16#00338]; +entity("nsube") -> 16#02288; +entity("nsubset") -> [16#02282, 16#020D2]; +entity("nsubseteq") -> 16#02288; +entity("nsubseteqq") -> [16#02AC5, 16#00338]; +entity("nsucc") -> 16#02281; +entity("nsucceq") -> [16#02AB0, 16#00338]; +entity("nsup") -> 16#02285; +entity("nsupE") -> [16#02AC6, 16#00338]; +entity("nsupe") -> 16#02289; +entity("nsupset") -> [16#02283, 16#020D2]; +entity("nsupseteq") -> 16#02289; +entity("nsupseteqq") -> [16#02AC6, 16#00338]; +entity("ntgl") -> 16#02279; +entity("ntilde") -> 16#000F1; +entity("ntlg") -> 16#02278; +entity("ntriangleleft") -> 16#022EA; +entity("ntrianglelefteq") -> 16#022EC; +entity("ntriangleright") -> 16#022EB; +entity("ntrianglerighteq") -> 16#022ED; +entity("nu") -> 16#003BD; +entity("num") -> 16#00023; +entity("numero") -> 16#02116; +entity("numsp") -> 16#02007; +entity("nvDash") -> 16#022AD; +entity("nvHarr") -> 16#02904; +entity("nvap") -> [16#0224D, 16#020D2]; +entity("nvdash") -> 16#022AC; +entity("nvge") -> [16#02265, 16#020D2]; +entity("nvgt") -> [16#0003E, 16#020D2]; +entity("nvinfin") -> 16#029DE; +entity("nvlArr") -> 16#02902; +entity("nvle") -> [16#02264, 16#020D2]; +entity("nvlt") -> [16#0003C, 16#020D2]; +entity("nvltrie") -> [16#022B4, 16#020D2]; +entity("nvrArr") -> 16#02903; +entity("nvrtrie") -> [16#022B5, 16#020D2]; +entity("nvsim") -> [16#0223C, 16#020D2]; +entity("nwArr") -> 16#021D6; +entity("nwarhk") -> 16#02923; +entity("nwarr") -> 16#02196; +entity("nwarrow") -> 16#02196; +entity("nwnear") -> 16#02927; +entity("oS") -> 16#024C8; +entity("oacute") -> 16#000F3; +entity("oast") -> 16#0229B; +entity("ocir") -> 16#0229A; +entity("ocirc") -> 16#000F4; +entity("ocy") -> 16#0043E; +entity("odash") -> 16#0229D; +entity("odblac") -> 16#00151; +entity("odiv") -> 16#02A38; +entity("odot") -> 16#02299; +entity("odsold") -> 16#029BC; +entity("oelig") -> 16#00153; +entity("ofcir") -> 16#029BF; +entity("ofr") -> 16#1D52C; +entity("ogon") -> 16#002DB; +entity("ograve") -> 16#000F2; +entity("ogt") -> 16#029C1; +entity("ohbar") -> 16#029B5; +entity("ohm") -> 16#003A9; +entity("oint") -> 16#0222E; +entity("olarr") -> 16#021BA; +entity("olcir") -> 16#029BE; +entity("olcross") -> 16#029BB; +entity("oline") -> 16#0203E; +entity("olt") -> 16#029C0; +entity("omacr") -> 16#0014D; +entity("omega") -> 16#003C9; +entity("omicron") -> 16#003BF; +entity("omid") -> 16#029B6; +entity("ominus") -> 16#02296; +entity("oopf") -> 16#1D560; +entity("opar") -> 16#029B7; +entity("operp") -> 16#029B9; +entity("oplus") -> 16#02295; +entity("or") -> 16#02228; +entity("orarr") -> 16#021BB; +entity("ord") -> 16#02A5D; +entity("order") -> 16#02134; +entity("orderof") -> 16#02134; +entity("ordf") -> 16#000AA; +entity("ordm") -> 16#000BA; +entity("origof") -> 16#022B6; +entity("oror") -> 16#02A56; +entity("orslope") -> 16#02A57; +entity("orv") -> 16#02A5B; +entity("oscr") -> 16#02134; +entity("oslash") -> 16#000F8; +entity("osol") -> 16#02298; +entity("otilde") -> 16#000F5; +entity("otimes") -> 16#02297; +entity("otimesas") -> 16#02A36; +entity("ouml") -> 16#000F6; +entity("ovbar") -> 16#0233D; +entity("par") -> 16#02225; +entity("para") -> 16#000B6; +entity("parallel") -> 16#02225; +entity("parsim") -> 16#02AF3; +entity("parsl") -> 16#02AFD; +entity("part") -> 16#02202; +entity("pcy") -> 16#0043F; +entity("percnt") -> 16#00025; +entity("period") -> 16#0002E; +entity("permil") -> 16#02030; +entity("perp") -> 16#022A5; +entity("pertenk") -> 16#02031; +entity("pfr") -> 16#1D52D; +entity("phi") -> 16#003C6; +entity("phiv") -> 16#003D5; +entity("phmmat") -> 16#02133; +entity("phone") -> 16#0260E; +entity("pi") -> 16#003C0; +entity("pitchfork") -> 16#022D4; +entity("piv") -> 16#003D6; +entity("planck") -> 16#0210F; +entity("planckh") -> 16#0210E; +entity("plankv") -> 16#0210F; +entity("plus") -> 16#0002B; +entity("plusacir") -> 16#02A23; +entity("plusb") -> 16#0229E; +entity("pluscir") -> 16#02A22; +entity("plusdo") -> 16#02214; +entity("plusdu") -> 16#02A25; +entity("pluse") -> 16#02A72; +entity("plusmn") -> 16#000B1; +entity("plussim") -> 16#02A26; +entity("plustwo") -> 16#02A27; +entity("pm") -> 16#000B1; +entity("pointint") -> 16#02A15; +entity("popf") -> 16#1D561; +entity("pound") -> 16#000A3; +entity("pr") -> 16#0227A; +entity("prE") -> 16#02AB3; +entity("prap") -> 16#02AB7; +entity("prcue") -> 16#0227C; +entity("pre") -> 16#02AAF; +entity("prec") -> 16#0227A; +entity("precapprox") -> 16#02AB7; +entity("preccurlyeq") -> 16#0227C; +entity("preceq") -> 16#02AAF; +entity("precnapprox") -> 16#02AB9; +entity("precneqq") -> 16#02AB5; +entity("precnsim") -> 16#022E8; +entity("precsim") -> 16#0227E; +entity("prime") -> 16#02032; +entity("primes") -> 16#02119; +entity("prnE") -> 16#02AB5; +entity("prnap") -> 16#02AB9; +entity("prnsim") -> 16#022E8; +entity("prod") -> 16#0220F; +entity("profalar") -> 16#0232E; +entity("profline") -> 16#02312; +entity("profsurf") -> 16#02313; +entity("prop") -> 16#0221D; +entity("propto") -> 16#0221D; +entity("prsim") -> 16#0227E; +entity("prurel") -> 16#022B0; +entity("pscr") -> 16#1D4C5; +entity("psi") -> 16#003C8; +entity("puncsp") -> 16#02008; +entity("qfr") -> 16#1D52E; +entity("qint") -> 16#02A0C; +entity("qopf") -> 16#1D562; +entity("qprime") -> 16#02057; +entity("qscr") -> 16#1D4C6; +entity("quaternions") -> 16#0210D; +entity("quatint") -> 16#02A16; +entity("quest") -> 16#0003F; +entity("questeq") -> 16#0225F; +entity("quot") -> 16#00022; +entity("rAarr") -> 16#021DB; +entity("rArr") -> 16#021D2; +entity("rAtail") -> 16#0291C; +entity("rBarr") -> 16#0290F; +entity("rHar") -> 16#02964; +entity("race") -> [16#0223D, 16#00331]; +entity("racute") -> 16#00155; +entity("radic") -> 16#0221A; +entity("raemptyv") -> 16#029B3; +entity("rang") -> 16#027E9; +entity("rangd") -> 16#02992; +entity("range") -> 16#029A5; +entity("rangle") -> 16#027E9; +entity("raquo") -> 16#000BB; +entity("rarr") -> 16#02192; +entity("rarrap") -> 16#02975; +entity("rarrb") -> 16#021E5; +entity("rarrbfs") -> 16#02920; +entity("rarrc") -> 16#02933; +entity("rarrfs") -> 16#0291E; +entity("rarrhk") -> 16#021AA; +entity("rarrlp") -> 16#021AC; +entity("rarrpl") -> 16#02945; +entity("rarrsim") -> 16#02974; +entity("rarrtl") -> 16#021A3; +entity("rarrw") -> 16#0219D; +entity("ratail") -> 16#0291A; +entity("ratio") -> 16#02236; +entity("rationals") -> 16#0211A; +entity("rbarr") -> 16#0290D; +entity("rbbrk") -> 16#02773; +entity("rbrace") -> 16#0007D; +entity("rbrack") -> 16#0005D; +entity("rbrke") -> 16#0298C; +entity("rbrksld") -> 16#0298E; +entity("rbrkslu") -> 16#02990; +entity("rcaron") -> 16#00159; +entity("rcedil") -> 16#00157; +entity("rceil") -> 16#02309; +entity("rcub") -> 16#0007D; +entity("rcy") -> 16#00440; +entity("rdca") -> 16#02937; +entity("rdldhar") -> 16#02969; +entity("rdquo") -> 16#0201D; +entity("rdquor") -> 16#0201D; +entity("rdsh") -> 16#021B3; +entity("real") -> 16#0211C; +entity("realine") -> 16#0211B; +entity("realpart") -> 16#0211C; +entity("reals") -> 16#0211D; +entity("rect") -> 16#025AD; +entity("reg") -> 16#000AE; +entity("rfisht") -> 16#0297D; +entity("rfloor") -> 16#0230B; +entity("rfr") -> 16#1D52F; +entity("rhard") -> 16#021C1; +entity("rharu") -> 16#021C0; +entity("rharul") -> 16#0296C; +entity("rho") -> 16#003C1; +entity("rhov") -> 16#003F1; +entity("rightarrow") -> 16#02192; +entity("rightarrowtail") -> 16#021A3; +entity("rightharpoondown") -> 16#021C1; +entity("rightharpoonup") -> 16#021C0; +entity("rightleftarrows") -> 16#021C4; +entity("rightleftharpoons") -> 16#021CC; +entity("rightrightarrows") -> 16#021C9; +entity("rightsquigarrow") -> 16#0219D; +entity("rightthreetimes") -> 16#022CC; +entity("ring") -> 16#002DA; +entity("risingdotseq") -> 16#02253; +entity("rlarr") -> 16#021C4; +entity("rlhar") -> 16#021CC; +entity("rlm") -> 16#0200F; +entity("rmoust") -> 16#023B1; +entity("rmoustache") -> 16#023B1; +entity("rnmid") -> 16#02AEE; +entity("roang") -> 16#027ED; +entity("roarr") -> 16#021FE; +entity("robrk") -> 16#027E7; +entity("ropar") -> 16#02986; +entity("ropf") -> 16#1D563; +entity("roplus") -> 16#02A2E; +entity("rotimes") -> 16#02A35; +entity("rpar") -> 16#00029; +entity("rpargt") -> 16#02994; +entity("rppolint") -> 16#02A12; +entity("rrarr") -> 16#021C9; +entity("rsaquo") -> 16#0203A; +entity("rscr") -> 16#1D4C7; +entity("rsh") -> 16#021B1; +entity("rsqb") -> 16#0005D; +entity("rsquo") -> 16#02019; +entity("rsquor") -> 16#02019; +entity("rthree") -> 16#022CC; +entity("rtimes") -> 16#022CA; +entity("rtri") -> 16#025B9; +entity("rtrie") -> 16#022B5; +entity("rtrif") -> 16#025B8; +entity("rtriltri") -> 16#029CE; +entity("ruluhar") -> 16#02968; +entity("rx") -> 16#0211E; +entity("sacute") -> 16#0015B; +entity("sbquo") -> 16#0201A; +entity("sc") -> 16#0227B; +entity("scE") -> 16#02AB4; +entity("scap") -> 16#02AB8; +entity("scaron") -> 16#00161; +entity("sccue") -> 16#0227D; +entity("sce") -> 16#02AB0; +entity("scedil") -> 16#0015F; +entity("scirc") -> 16#0015D; +entity("scnE") -> 16#02AB6; +entity("scnap") -> 16#02ABA; +entity("scnsim") -> 16#022E9; +entity("scpolint") -> 16#02A13; +entity("scsim") -> 16#0227F; +entity("scy") -> 16#00441; +entity("sdot") -> 16#022C5; +entity("sdotb") -> 16#022A1; +entity("sdote") -> 16#02A66; +entity("seArr") -> 16#021D8; +entity("searhk") -> 16#02925; +entity("searr") -> 16#02198; +entity("searrow") -> 16#02198; +entity("sect") -> 16#000A7; +entity("semi") -> 16#0003B; +entity("seswar") -> 16#02929; +entity("setminus") -> 16#02216; +entity("setmn") -> 16#02216; +entity("sext") -> 16#02736; +entity("sfr") -> 16#1D530; +entity("sfrown") -> 16#02322; +entity("sharp") -> 16#0266F; +entity("shchcy") -> 16#00449; +entity("shcy") -> 16#00448; +entity("shortmid") -> 16#02223; +entity("shortparallel") -> 16#02225; +entity("shy") -> 16#000AD; +entity("sigma") -> 16#003C3; +entity("sigmaf") -> 16#003C2; +entity("sigmav") -> 16#003C2; +entity("sim") -> 16#0223C; +entity("simdot") -> 16#02A6A; +entity("sime") -> 16#02243; +entity("simeq") -> 16#02243; +entity("simg") -> 16#02A9E; +entity("simgE") -> 16#02AA0; +entity("siml") -> 16#02A9D; +entity("simlE") -> 16#02A9F; +entity("simne") -> 16#02246; +entity("simplus") -> 16#02A24; +entity("simrarr") -> 16#02972; +entity("slarr") -> 16#02190; +entity("smallsetminus") -> 16#02216; +entity("smashp") -> 16#02A33; +entity("smeparsl") -> 16#029E4; +entity("smid") -> 16#02223; +entity("smile") -> 16#02323; +entity("smt") -> 16#02AAA; +entity("smte") -> 16#02AAC; +entity("smtes") -> [16#02AAC, 16#0FE00]; +entity("softcy") -> 16#0044C; +entity("sol") -> 16#0002F; +entity("solb") -> 16#029C4; +entity("solbar") -> 16#0233F; +entity("sopf") -> 16#1D564; +entity("spades") -> 16#02660; +entity("spadesuit") -> 16#02660; +entity("spar") -> 16#02225; +entity("sqcap") -> 16#02293; +entity("sqcaps") -> [16#02293, 16#0FE00]; +entity("sqcup") -> 16#02294; +entity("sqcups") -> [16#02294, 16#0FE00]; +entity("sqsub") -> 16#0228F; +entity("sqsube") -> 16#02291; +entity("sqsubset") -> 16#0228F; +entity("sqsubseteq") -> 16#02291; +entity("sqsup") -> 16#02290; +entity("sqsupe") -> 16#02292; +entity("sqsupset") -> 16#02290; +entity("sqsupseteq") -> 16#02292; +entity("squ") -> 16#025A1; +entity("square") -> 16#025A1; +entity("squarf") -> 16#025AA; +entity("squf") -> 16#025AA; +entity("srarr") -> 16#02192; +entity("sscr") -> 16#1D4C8; +entity("ssetmn") -> 16#02216; +entity("ssmile") -> 16#02323; +entity("sstarf") -> 16#022C6; +entity("star") -> 16#02606; +entity("starf") -> 16#02605; +entity("straightepsilon") -> 16#003F5; +entity("straightphi") -> 16#003D5; +entity("strns") -> 16#000AF; +entity("sub") -> 16#02282; +entity("subE") -> 16#02AC5; +entity("subdot") -> 16#02ABD; +entity("sube") -> 16#02286; +entity("subedot") -> 16#02AC3; +entity("submult") -> 16#02AC1; +entity("subnE") -> 16#02ACB; +entity("subne") -> 16#0228A; +entity("subplus") -> 16#02ABF; +entity("subrarr") -> 16#02979; +entity("subset") -> 16#02282; +entity("subseteq") -> 16#02286; +entity("subseteqq") -> 16#02AC5; +entity("subsetneq") -> 16#0228A; +entity("subsetneqq") -> 16#02ACB; +entity("subsim") -> 16#02AC7; +entity("subsub") -> 16#02AD5; +entity("subsup") -> 16#02AD3; +entity("succ") -> 16#0227B; +entity("succapprox") -> 16#02AB8; +entity("succcurlyeq") -> 16#0227D; +entity("succeq") -> 16#02AB0; +entity("succnapprox") -> 16#02ABA; +entity("succneqq") -> 16#02AB6; +entity("succnsim") -> 16#022E9; +entity("succsim") -> 16#0227F; +entity("sum") -> 16#02211; +entity("sung") -> 16#0266A; +entity("sup") -> 16#02283; +entity("sup1") -> 16#000B9; +entity("sup2") -> 16#000B2; +entity("sup3") -> 16#000B3; +entity("supE") -> 16#02AC6; +entity("supdot") -> 16#02ABE; +entity("supdsub") -> 16#02AD8; +entity("supe") -> 16#02287; +entity("supedot") -> 16#02AC4; +entity("suphsol") -> 16#027C9; +entity("suphsub") -> 16#02AD7; +entity("suplarr") -> 16#0297B; +entity("supmult") -> 16#02AC2; +entity("supnE") -> 16#02ACC; +entity("supne") -> 16#0228B; +entity("supplus") -> 16#02AC0; +entity("supset") -> 16#02283; +entity("supseteq") -> 16#02287; +entity("supseteqq") -> 16#02AC6; +entity("supsetneq") -> 16#0228B; +entity("supsetneqq") -> 16#02ACC; +entity("supsim") -> 16#02AC8; +entity("supsub") -> 16#02AD4; +entity("supsup") -> 16#02AD6; +entity("swArr") -> 16#021D9; +entity("swarhk") -> 16#02926; +entity("swarr") -> 16#02199; +entity("swarrow") -> 16#02199; +entity("swnwar") -> 16#0292A; +entity("szlig") -> 16#000DF; +entity("target") -> 16#02316; +entity("tau") -> 16#003C4; +entity("tbrk") -> 16#023B4; +entity("tcaron") -> 16#00165; +entity("tcedil") -> 16#00163; +entity("tcy") -> 16#00442; +entity("tdot") -> 16#020DB; +entity("telrec") -> 16#02315; +entity("tfr") -> 16#1D531; +entity("there4") -> 16#02234; +entity("therefore") -> 16#02234; +entity("theta") -> 16#003B8; +entity("thetasym") -> 16#003D1; +entity("thetav") -> 16#003D1; +entity("thickapprox") -> 16#02248; +entity("thicksim") -> 16#0223C; +entity("thinsp") -> 16#02009; +entity("thkap") -> 16#02248; +entity("thksim") -> 16#0223C; +entity("thorn") -> 16#000FE; +entity("tilde") -> 16#002DC; +entity("times") -> 16#000D7; +entity("timesb") -> 16#022A0; +entity("timesbar") -> 16#02A31; +entity("timesd") -> 16#02A30; +entity("tint") -> 16#0222D; +entity("toea") -> 16#02928; +entity("top") -> 16#022A4; +entity("topbot") -> 16#02336; +entity("topcir") -> 16#02AF1; +entity("topf") -> 16#1D565; +entity("topfork") -> 16#02ADA; +entity("tosa") -> 16#02929; +entity("tprime") -> 16#02034; +entity("trade") -> 16#02122; +entity("triangle") -> 16#025B5; +entity("triangledown") -> 16#025BF; +entity("triangleleft") -> 16#025C3; +entity("trianglelefteq") -> 16#022B4; +entity("triangleq") -> 16#0225C; +entity("triangleright") -> 16#025B9; +entity("trianglerighteq") -> 16#022B5; +entity("tridot") -> 16#025EC; +entity("trie") -> 16#0225C; +entity("triminus") -> 16#02A3A; +entity("triplus") -> 16#02A39; +entity("trisb") -> 16#029CD; +entity("tritime") -> 16#02A3B; +entity("trpezium") -> 16#023E2; +entity("tscr") -> 16#1D4C9; +entity("tscy") -> 16#00446; +entity("tshcy") -> 16#0045B; +entity("tstrok") -> 16#00167; +entity("twixt") -> 16#0226C; +entity("twoheadleftarrow") -> 16#0219E; +entity("twoheadrightarrow") -> 16#021A0; +entity("uArr") -> 16#021D1; +entity("uHar") -> 16#02963; +entity("uacute") -> 16#000FA; +entity("uarr") -> 16#02191; +entity("ubrcy") -> 16#0045E; +entity("ubreve") -> 16#0016D; +entity("ucirc") -> 16#000FB; +entity("ucy") -> 16#00443; +entity("udarr") -> 16#021C5; +entity("udblac") -> 16#00171; +entity("udhar") -> 16#0296E; +entity("ufisht") -> 16#0297E; +entity("ufr") -> 16#1D532; +entity("ugrave") -> 16#000F9; +entity("uharl") -> 16#021BF; +entity("uharr") -> 16#021BE; +entity("uhblk") -> 16#02580; +entity("ulcorn") -> 16#0231C; +entity("ulcorner") -> 16#0231C; +entity("ulcrop") -> 16#0230F; +entity("ultri") -> 16#025F8; +entity("umacr") -> 16#0016B; +entity("uml") -> 16#000A8; +entity("uogon") -> 16#00173; +entity("uopf") -> 16#1D566; +entity("uparrow") -> 16#02191; +entity("updownarrow") -> 16#02195; +entity("upharpoonleft") -> 16#021BF; +entity("upharpoonright") -> 16#021BE; +entity("uplus") -> 16#0228E; +entity("upsi") -> 16#003C5; +entity("upsih") -> 16#003D2; +entity("upsilon") -> 16#003C5; +entity("upuparrows") -> 16#021C8; +entity("urcorn") -> 16#0231D; +entity("urcorner") -> 16#0231D; +entity("urcrop") -> 16#0230E; +entity("uring") -> 16#0016F; +entity("urtri") -> 16#025F9; +entity("uscr") -> 16#1D4CA; +entity("utdot") -> 16#022F0; +entity("utilde") -> 16#00169; +entity("utri") -> 16#025B5; +entity("utrif") -> 16#025B4; +entity("uuarr") -> 16#021C8; +entity("uuml") -> 16#000FC; +entity("uwangle") -> 16#029A7; +entity("vArr") -> 16#021D5; +entity("vBar") -> 16#02AE8; +entity("vBarv") -> 16#02AE9; +entity("vDash") -> 16#022A8; +entity("vangrt") -> 16#0299C; +entity("varepsilon") -> 16#003F5; +entity("varkappa") -> 16#003F0; +entity("varnothing") -> 16#02205; +entity("varphi") -> 16#003D5; +entity("varpi") -> 16#003D6; +entity("varpropto") -> 16#0221D; +entity("varr") -> 16#02195; +entity("varrho") -> 16#003F1; +entity("varsigma") -> 16#003C2; +entity("varsubsetneq") -> [16#0228A, 16#0FE00]; +entity("varsubsetneqq") -> [16#02ACB, 16#0FE00]; +entity("varsupsetneq") -> [16#0228B, 16#0FE00]; +entity("varsupsetneqq") -> [16#02ACC, 16#0FE00]; +entity("vartheta") -> 16#003D1; +entity("vartriangleleft") -> 16#022B2; +entity("vartriangleright") -> 16#022B3; +entity("vcy") -> 16#00432; +entity("vdash") -> 16#022A2; +entity("vee") -> 16#02228; +entity("veebar") -> 16#022BB; +entity("veeeq") -> 16#0225A; +entity("vellip") -> 16#022EE; +entity("verbar") -> 16#0007C; +entity("vert") -> 16#0007C; +entity("vfr") -> 16#1D533; +entity("vltri") -> 16#022B2; +entity("vnsub") -> [16#02282, 16#020D2]; +entity("vnsup") -> [16#02283, 16#020D2]; +entity("vopf") -> 16#1D567; +entity("vprop") -> 16#0221D; +entity("vrtri") -> 16#022B3; +entity("vscr") -> 16#1D4CB; +entity("vsubnE") -> [16#02ACB, 16#0FE00]; +entity("vsubne") -> [16#0228A, 16#0FE00]; +entity("vsupnE") -> [16#02ACC, 16#0FE00]; +entity("vsupne") -> [16#0228B, 16#0FE00]; +entity("vzigzag") -> 16#0299A; +entity("wcirc") -> 16#00175; +entity("wedbar") -> 16#02A5F; +entity("wedge") -> 16#02227; +entity("wedgeq") -> 16#02259; +entity("weierp") -> 16#02118; +entity("wfr") -> 16#1D534; +entity("wopf") -> 16#1D568; +entity("wp") -> 16#02118; +entity("wr") -> 16#02240; +entity("wreath") -> 16#02240; +entity("wscr") -> 16#1D4CC; +entity("xcap") -> 16#022C2; +entity("xcirc") -> 16#025EF; +entity("xcup") -> 16#022C3; +entity("xdtri") -> 16#025BD; +entity("xfr") -> 16#1D535; +entity("xhArr") -> 16#027FA; +entity("xharr") -> 16#027F7; +entity("xi") -> 16#003BE; +entity("xlArr") -> 16#027F8; +entity("xlarr") -> 16#027F5; +entity("xmap") -> 16#027FC; +entity("xnis") -> 16#022FB; +entity("xodot") -> 16#02A00; +entity("xopf") -> 16#1D569; +entity("xoplus") -> 16#02A01; +entity("xotime") -> 16#02A02; +entity("xrArr") -> 16#027F9; +entity("xrarr") -> 16#027F6; +entity("xscr") -> 16#1D4CD; +entity("xsqcup") -> 16#02A06; +entity("xuplus") -> 16#02A04; +entity("xutri") -> 16#025B3; +entity("xvee") -> 16#022C1; +entity("xwedge") -> 16#022C0; +entity("yacute") -> 16#000FD; +entity("yacy") -> 16#0044F; +entity("ycirc") -> 16#00177; +entity("ycy") -> 16#0044B; +entity("yen") -> 16#000A5; +entity("yfr") -> 16#1D536; +entity("yicy") -> 16#00457; +entity("yopf") -> 16#1D56A; +entity("yscr") -> 16#1D4CE; +entity("yucy") -> 16#0044E; +entity("yuml") -> 16#000FF; +entity("zacute") -> 16#0017A; +entity("zcaron") -> 16#0017E; +entity("zcy") -> 16#00437; +entity("zdot") -> 16#0017C; +entity("zeetrf") -> 16#02128; +entity("zeta") -> 16#003B6; +entity("zfr") -> 16#1D537; +entity("zhcy") -> 16#00436; +entity("zigrarr") -> 16#021DD; +entity("zopf") -> 16#1D56B; +entity("zscr") -> 16#1D4CF; +entity("zwj") -> 16#0200D; +entity("zwnj") -> 16#0200C; +entity(_) -> undefined. + +%% +%% Tests +%% +-ifdef(TEST). +-include_lib("eunit/include/eunit.hrl"). + +exhaustive_entity_test() -> + T = mochiweb_cover:clause_lookup_table(?MODULE, entity), + [?assertEqual(V, entity(K)) || {K, V} <- T]. + +charref_test() -> + 1234 = charref("#1234"), + 255 = charref("#xfF"), + 255 = charref(<<"#XFf">>), + 38 = charref("amp"), + 38 = charref(<<"amp">>), + undefined = charref("not_an_entity"), + undefined = charref("#not_an_entity"), + undefined = charref("#xnot_an_entity"), + ok. + +-endif. diff --git a/rabbitmq-server/plugins-src/mochiweb-wrapper/mochiweb-git/src/mochiweb_cookies.erl b/rabbitmq-server/plugins-src/mochiweb-wrapper/mochiweb-git/src/mochiweb_cookies.erl new file mode 100644 index 0000000..1cc4e91 --- /dev/null +++ b/rabbitmq-server/plugins-src/mochiweb-wrapper/mochiweb-git/src/mochiweb_cookies.erl @@ -0,0 +1,331 @@ +%% @author Emad El-Haraty +%% @copyright 2007 Mochi Media, Inc. + +%% @doc HTTP Cookie parsing and generating (RFC 2109, RFC 2965). + +-module(mochiweb_cookies). +-export([parse_cookie/1, cookie/3, cookie/2]). + +-define(QUOTE, $\"). + +-define(IS_WHITESPACE(C), + (C =:= $\s orelse C =:= $\t orelse C =:= $\r orelse C =:= $\n)). + +%% RFC 2616 separators (called tspecials in RFC 2068) +-define(IS_SEPARATOR(C), + (C < 32 orelse + C =:= $\s orelse C =:= $\t orelse + C =:= $( orelse C =:= $) orelse C =:= $< orelse C =:= $> orelse + C =:= $@ orelse C =:= $, orelse C =:= $; orelse C =:= $: orelse + C =:= $\\ orelse C =:= $\" orelse C =:= $/ orelse + C =:= $[ orelse C =:= $] orelse C =:= $? orelse C =:= $= orelse + C =:= ${ orelse C =:= $})). + +%% @type proplist() = [{Key::string(), Value::string()}]. +%% @type header() = {Name::string(), Value::string()}. +%% @type int_seconds() = integer(). + +%% @spec cookie(Key::string(), Value::string()) -> header() +%% @doc Short-hand for cookie(Key, Value, []). +cookie(Key, Value) -> + cookie(Key, Value, []). + +%% @spec cookie(Key::string(), Value::string(), Options::[Option]) -> header() +%% where Option = {max_age, int_seconds()} | {local_time, {date(), time()}} +%% | {domain, string()} | {path, string()} +%% | {secure, true | false} | {http_only, true | false} +%% +%% @doc Generate a Set-Cookie header field tuple. +cookie(Key, Value, Options) -> + Cookie = [any_to_list(Key), "=", quote(Value), "; Version=1"], + %% Set-Cookie: + %% Comment, Domain, Max-Age, Path, Secure, Version + %% Set-Cookie2: + %% Comment, CommentURL, Discard, Domain, Max-Age, Path, Port, Secure, + %% Version + ExpiresPart = + case proplists:get_value(max_age, Options) of + undefined -> + ""; + RawAge -> + When = case proplists:get_value(local_time, Options) of + undefined -> + calendar:local_time(); + LocalTime -> + LocalTime + end, + Age = case RawAge < 0 of + true -> + 0; + false -> + RawAge + end, + ["; Expires=", age_to_cookie_date(Age, When), + "; Max-Age=", quote(Age)] + end, + SecurePart = + case proplists:get_value(secure, Options) of + true -> + "; Secure"; + _ -> + "" + end, + DomainPart = + case proplists:get_value(domain, Options) of + undefined -> + ""; + Domain -> + ["; Domain=", quote(Domain)] + end, + PathPart = + case proplists:get_value(path, Options) of + undefined -> + ""; + Path -> + ["; Path=", quote(Path)] + end, + HttpOnlyPart = + case proplists:get_value(http_only, Options) of + true -> + "; HttpOnly"; + _ -> + "" + end, + CookieParts = [Cookie, ExpiresPart, SecurePart, DomainPart, PathPart, HttpOnlyPart], + {"Set-Cookie", lists:flatten(CookieParts)}. + + +%% Every major browser incorrectly handles quoted strings in a +%% different and (worse) incompatible manner. Instead of wasting time +%% writing redundant code for each browser, we restrict cookies to +%% only contain characters that browsers handle compatibly. +%% +%% By replacing the definition of quote with this, we generate +%% RFC-compliant cookies: +%% +%% quote(V) -> +%% Fun = fun(?QUOTE, Acc) -> [$\\, ?QUOTE | Acc]; +%% (Ch, Acc) -> [Ch | Acc] +%% end, +%% [?QUOTE | lists:foldr(Fun, [?QUOTE], V)]. + +%% Convert to a string and raise an error if quoting is required. +quote(V0) -> + V = any_to_list(V0), + lists:all(fun(Ch) -> Ch =:= $/ orelse not ?IS_SEPARATOR(Ch) end, V) + orelse erlang:error({cookie_quoting_required, V}), + V. + + +%% Return a date in the form of: Wdy, DD-Mon-YYYY HH:MM:SS GMT +%% See also: rfc2109: 10.1.2 +rfc2109_cookie_expires_date(LocalTime) -> + {{YYYY,MM,DD},{Hour,Min,Sec}} = + case calendar:local_time_to_universal_time_dst(LocalTime) of + [] -> + {Date, {Hour1, Min1, Sec1}} = LocalTime, + LocalTime2 = {Date, {Hour1 + 1, Min1, Sec1}}, + case calendar:local_time_to_universal_time_dst(LocalTime2) of + [Gmt] -> Gmt; + [_,Gmt] -> Gmt + end; + [Gmt] -> Gmt; + [_,Gmt] -> Gmt + end, + DayNumber = calendar:day_of_the_week({YYYY,MM,DD}), + lists:flatten( + io_lib:format("~s, ~2.2.0w-~3.s-~4.4.0w ~2.2.0w:~2.2.0w:~2.2.0w GMT", + [httpd_util:day(DayNumber),DD,httpd_util:month(MM),YYYY,Hour,Min,Sec])). + +add_seconds(Secs, LocalTime) -> + Greg = calendar:datetime_to_gregorian_seconds(LocalTime), + calendar:gregorian_seconds_to_datetime(Greg + Secs). + +age_to_cookie_date(Age, LocalTime) -> + rfc2109_cookie_expires_date(add_seconds(Age, LocalTime)). + +%% @spec parse_cookie(string()) -> [{K::string(), V::string()}] +%% @doc Parse the contents of a Cookie header field, ignoring cookie +%% attributes, and return a simple property list. +parse_cookie("") -> + []; +parse_cookie(Cookie) -> + parse_cookie(Cookie, []). + +%% Internal API + +parse_cookie([], Acc) -> + lists:reverse(Acc); +parse_cookie(String, Acc) -> + {{Token, Value}, Rest} = read_pair(String), + Acc1 = case Token of + "" -> + Acc; + "$" ++ _ -> + Acc; + _ -> + [{Token, Value} | Acc] + end, + parse_cookie(Rest, Acc1). + +read_pair(String) -> + {Token, Rest} = read_token(skip_whitespace(String)), + {Value, Rest1} = read_value(skip_whitespace(Rest)), + {{Token, Value}, skip_past_separator(Rest1)}. + +read_value([$= | Value]) -> + Value1 = skip_whitespace(Value), + case Value1 of + [?QUOTE | _] -> + read_quoted(Value1); + _ -> + read_token(Value1) + end; +read_value(String) -> + {"", String}. + +read_quoted([?QUOTE | String]) -> + read_quoted(String, []). + +read_quoted([], Acc) -> + {lists:reverse(Acc), []}; +read_quoted([?QUOTE | Rest], Acc) -> + {lists:reverse(Acc), Rest}; +read_quoted([$\\, Any | Rest], Acc) -> + read_quoted(Rest, [Any | Acc]); +read_quoted([C | Rest], Acc) -> + read_quoted(Rest, [C | Acc]). + +skip_whitespace(String) -> + F = fun (C) -> ?IS_WHITESPACE(C) end, + lists:dropwhile(F, String). + +read_token(String) -> + F = fun (C) -> not ?IS_SEPARATOR(C) end, + lists:splitwith(F, String). + +skip_past_separator([]) -> + []; +skip_past_separator([$; | Rest]) -> + Rest; +skip_past_separator([$, | Rest]) -> + Rest; +skip_past_separator([_ | Rest]) -> + skip_past_separator(Rest). + +any_to_list(V) when is_list(V) -> + V; +any_to_list(V) when is_atom(V) -> + atom_to_list(V); +any_to_list(V) when is_binary(V) -> + binary_to_list(V); +any_to_list(V) when is_integer(V) -> + integer_to_list(V). + +%% +%% Tests +%% +-ifdef(TEST). +-include_lib("eunit/include/eunit.hrl"). + +quote_test() -> + %% ?assertError eunit macro is not compatible with coverage module + try quote(":wq") + catch error:{cookie_quoting_required, ":wq"} -> ok + end, + ?assertEqual( + "foo", + quote(foo)), + ok. + +parse_cookie_test() -> + %% RFC example + C1 = "$Version=\"1\"; Customer=\"WILE_E_COYOTE\"; $Path=\"/acme\"; + Part_Number=\"Rocket_Launcher_0001\"; $Path=\"/acme\"; + Shipping=\"FedEx\"; $Path=\"/acme\"", + ?assertEqual( + [{"Customer","WILE_E_COYOTE"}, + {"Part_Number","Rocket_Launcher_0001"}, + {"Shipping","FedEx"}], + parse_cookie(C1)), + %% Potential edge cases + ?assertEqual( + [{"foo", "x"}], + parse_cookie("foo=\"\\x\"")), + ?assertEqual( + [], + parse_cookie("=")), + ?assertEqual( + [{"foo", ""}, {"bar", ""}], + parse_cookie(" foo ; bar ")), + ?assertEqual( + [{"foo", ""}, {"bar", ""}], + parse_cookie("foo=;bar=")), + ?assertEqual( + [{"foo", "\";"}, {"bar", ""}], + parse_cookie("foo = \"\\\";\";bar ")), + ?assertEqual( + [{"foo", "\";bar"}], + parse_cookie("foo=\"\\\";bar")), + ?assertEqual( + [], + parse_cookie([])), + ?assertEqual( + [{"foo", "bar"}, {"baz", "wibble"}], + parse_cookie("foo=bar , baz=wibble ")), + ok. + +domain_test() -> + ?assertEqual( + {"Set-Cookie", + "Customer=WILE_E_COYOTE; " + "Version=1; " + "Domain=acme.com; " + "HttpOnly"}, + cookie("Customer", "WILE_E_COYOTE", + [{http_only, true}, {domain, "acme.com"}])), + ok. + +local_time_test() -> + {"Set-Cookie", S} = cookie("Customer", "WILE_E_COYOTE", + [{max_age, 111}, {secure, true}]), + ?assertMatch( + ["Customer=WILE_E_COYOTE", + " Version=1", + " Expires=" ++ _, + " Max-Age=111", + " Secure"], + string:tokens(S, ";")), + ok. + +cookie_test() -> + C1 = {"Set-Cookie", + "Customer=WILE_E_COYOTE; " + "Version=1; " + "Path=/acme"}, + C1 = cookie("Customer", "WILE_E_COYOTE", [{path, "/acme"}]), + C1 = cookie("Customer", "WILE_E_COYOTE", + [{path, "/acme"}, {badoption, "negatory"}]), + C1 = cookie('Customer', 'WILE_E_COYOTE', [{path, '/acme'}]), + C1 = cookie(<<"Customer">>, <<"WILE_E_COYOTE">>, [{path, <<"/acme">>}]), + + {"Set-Cookie","=NoKey; Version=1"} = cookie("", "NoKey", []), + {"Set-Cookie","=NoKey; Version=1"} = cookie("", "NoKey"), + LocalTime = calendar:universal_time_to_local_time({{2007, 5, 15}, {13, 45, 33}}), + C2 = {"Set-Cookie", + "Customer=WILE_E_COYOTE; " + "Version=1; " + "Expires=Tue, 15-May-2007 13:45:33 GMT; " + "Max-Age=0"}, + C2 = cookie("Customer", "WILE_E_COYOTE", + [{max_age, -111}, {local_time, LocalTime}]), + C3 = {"Set-Cookie", + "Customer=WILE_E_COYOTE; " + "Version=1; " + "Expires=Wed, 16-May-2007 13:45:50 GMT; " + "Max-Age=86417"}, + C3 = cookie("Customer", "WILE_E_COYOTE", + [{max_age, 86417}, {local_time, LocalTime}]), + ok. + +-endif. diff --git a/rabbitmq-server/plugins-src/mochiweb-wrapper/mochiweb-git/src/mochiweb_cover.erl b/rabbitmq-server/plugins-src/mochiweb-wrapper/mochiweb-git/src/mochiweb_cover.erl new file mode 100644 index 0000000..aa075d5 --- /dev/null +++ b/rabbitmq-server/plugins-src/mochiweb-wrapper/mochiweb-git/src/mochiweb_cover.erl @@ -0,0 +1,75 @@ +%% @author Bob Ippolito +%% @copyright 2010 Mochi Media, Inc. + +%% @doc Workarounds for various cover deficiencies. +-module(mochiweb_cover). +-export([get_beam/1, get_abstract_code/1, + get_clauses/2, clause_lookup_table/1]). +-export([clause_lookup_table/2]). + +%% Internal + +get_beam(Module) -> + {Module, Beam, _Path} = code:get_object_code(Module), + Beam. + +get_abstract_code(Beam) -> + {ok, {_Module, + [{abstract_code, + {raw_abstract_v1, L}}]}} = beam_lib:chunks(Beam, [abstract_code]), + L. + +get_clauses(Function, Code) -> + [L] = [Clauses || {function, _, FName, _, Clauses} + <- Code, FName =:= Function], + L. + +clause_lookup_table(Module, Function) -> + clause_lookup_table( + get_clauses(Function, + get_abstract_code(get_beam(Module)))). + +clause_lookup_table(Clauses) -> + lists:foldr(fun clause_fold/2, [], Clauses). + +clause_fold({clause, _, + [InTerm], + _Guards=[], + [OutTerm]}, + Acc) -> + try [{erl_parse:normalise(InTerm), erl_parse:normalise(OutTerm)} | Acc] + catch error:_ -> Acc + end; +clause_fold(_, Acc) -> + Acc. + +%% +%% Tests +%% +-ifdef(TEST). +-include_lib("eunit/include/eunit.hrl"). +foo_table(a) -> b; +foo_table("a") -> <<"b">>; +foo_table(123) -> {4, 3, 2}; +foo_table([list]) -> []; +foo_table([list1, list2]) -> [list1, list2, list3]; +foo_table(ignored) -> some, code, ignored; +foo_table(Var) -> Var. + +foo_table_test() -> + T = clause_lookup_table(?MODULE, foo_table), + [?assertEqual(V, foo_table(K)) || {K, V} <- T]. + +clause_lookup_table_test() -> + ?assertEqual(b, foo_table(a)), + ?assertEqual(ignored, foo_table(ignored)), + ?assertEqual('Var', foo_table('Var')), + ?assertEqual( + [{a, b}, + {"a", <<"b">>}, + {123, {4, 3, 2}}, + {[list], []}, + {[list1, list2], [list1, list2, list3]}], + clause_lookup_table(?MODULE, foo_table)). + +-endif. diff --git a/rabbitmq-server/plugins-src/mochiweb-wrapper/mochiweb-git/src/mochiweb_echo.erl b/rabbitmq-server/plugins-src/mochiweb-wrapper/mochiweb-git/src/mochiweb_echo.erl new file mode 100644 index 0000000..e145840 --- /dev/null +++ b/rabbitmq-server/plugins-src/mochiweb-wrapper/mochiweb-git/src/mochiweb_echo.erl @@ -0,0 +1,41 @@ +%% @author Bob Ippolito +%% @copyright 2007 Mochi Media, Inc. + +%% @doc Simple and stupid echo server to demo mochiweb_socket_server. + +-module(mochiweb_echo). +-author('bob@mochimedia.com'). +-export([start/0, stop/0, loop/1]). + +stop() -> + mochiweb_socket_server:stop(?MODULE). + +start() -> + mochiweb_socket_server:start([{link, false} | options()]). + +options() -> + [{name, ?MODULE}, + {port, 6789}, + {ip, "127.0.0.1"}, + {max, 1}, + {loop, {?MODULE, loop}}]. + +loop(Socket) -> + case mochiweb_socket:recv(Socket, 0, 30000) of + {ok, Data} -> + case mochiweb_socket:send(Socket, Data) of + ok -> + loop(Socket); + _ -> + exit(normal) + end; + _Other -> + exit(normal) + end. + +%% +%% Tests +%% +-ifdef(TEST). +-include_lib("eunit/include/eunit.hrl"). +-endif. diff --git a/rabbitmq-server/plugins-src/mochiweb-wrapper/mochiweb-git/src/mochiweb_headers.erl b/rabbitmq-server/plugins-src/mochiweb-wrapper/mochiweb-git/src/mochiweb_headers.erl new file mode 100644 index 0000000..b49cf9e --- /dev/null +++ b/rabbitmq-server/plugins-src/mochiweb-wrapper/mochiweb-git/src/mochiweb_headers.erl @@ -0,0 +1,420 @@ +%% @author Bob Ippolito +%% @copyright 2007 Mochi Media, Inc. + +%% @doc Case preserving (but case insensitive) HTTP Header dictionary. + +-module(mochiweb_headers). +-author('bob@mochimedia.com'). +-export([empty/0, from_list/1, insert/3, enter/3, get_value/2, lookup/2]). +-export([delete_any/2, get_primary_value/2, get_combined_value/2]). +-export([default/3, enter_from_list/2, default_from_list/2]). +-export([to_list/1, make/1]). +-export([from_binary/1]). + +%% @type headers(). +%% @type key() = atom() | binary() | string(). +%% @type value() = atom() | binary() | string() | integer(). + +%% @spec empty() -> headers() +%% @doc Create an empty headers structure. +empty() -> + gb_trees:empty(). + +%% @spec make(headers() | [{key(), value()}]) -> headers() +%% @doc Construct a headers() from the given list. +make(L) when is_list(L) -> + from_list(L); +%% assume a non-list is already mochiweb_headers. +make(T) -> + T. + +%% @spec from_binary(iolist()) -> headers() +%% @doc Transforms a raw HTTP header into a mochiweb headers structure. +%% +%% The given raw HTTP header can be one of the following: +%% +%% 1) A string or a binary representing a full HTTP header ending with +%% double CRLF. +%% Examples: +%% ``` +%% "Content-Length: 47\r\nContent-Type: text/plain\r\n\r\n" +%% <<"Content-Length: 47\r\nContent-Type: text/plain\r\n\r\n">>''' +%% +%% 2) A list of binaries or strings where each element represents a raw +%% HTTP header line ending with a single CRLF. +%% Examples: +%% ``` +%% [<<"Content-Length: 47\r\n">>, <<"Content-Type: text/plain\r\n">>] +%% ["Content-Length: 47\r\n", "Content-Type: text/plain\r\n"] +%% ["Content-Length: 47\r\n", <<"Content-Type: text/plain\r\n">>]''' +%% +from_binary(RawHttpHeader) when is_binary(RawHttpHeader) -> + from_binary(RawHttpHeader, []); +from_binary(RawHttpHeaderList) -> + from_binary(list_to_binary([RawHttpHeaderList, "\r\n"])). + +from_binary(RawHttpHeader, Acc) -> + case erlang:decode_packet(httph, RawHttpHeader, []) of + {ok, {http_header, _, H, _, V}, Rest} -> + from_binary(Rest, [{H, V} | Acc]); + _ -> + make(Acc) + end. + +%% @spec from_list([{key(), value()}]) -> headers() +%% @doc Construct a headers() from the given list. +from_list(List) -> + lists:foldl(fun ({K, V}, T) -> insert(K, V, T) end, empty(), List). + +%% @spec enter_from_list([{key(), value()}], headers()) -> headers() +%% @doc Insert pairs into the headers, replace any values for existing keys. +enter_from_list(List, T) -> + lists:foldl(fun ({K, V}, T1) -> enter(K, V, T1) end, T, List). + +%% @spec default_from_list([{key(), value()}], headers()) -> headers() +%% @doc Insert pairs into the headers for keys that do not already exist. +default_from_list(List, T) -> + lists:foldl(fun ({K, V}, T1) -> default(K, V, T1) end, T, List). + +%% @spec to_list(headers()) -> [{key(), string()}] +%% @doc Return the contents of the headers. The keys will be the exact key +%% that was first inserted (e.g. may be an atom or binary, case is +%% preserved). +to_list(T) -> + F = fun ({K, {array, L}}, Acc) -> + L1 = lists:reverse(L), + lists:foldl(fun (V, Acc1) -> [{K, V} | Acc1] end, Acc, L1); + (Pair, Acc) -> + [Pair | Acc] + end, + lists:reverse(lists:foldl(F, [], gb_trees:values(T))). + +%% @spec get_value(key(), headers()) -> string() | undefined +%% @doc Return the value of the given header using a case insensitive search. +%% undefined will be returned for keys that are not present. +get_value(K, T) -> + case lookup(K, T) of + {value, {_, V}} -> + expand(V); + none -> + undefined + end. + +%% @spec get_primary_value(key(), headers()) -> string() | undefined +%% @doc Return the value of the given header up to the first semicolon using +%% a case insensitive search. undefined will be returned for keys +%% that are not present. +get_primary_value(K, T) -> + case get_value(K, T) of + undefined -> + undefined; + V -> + lists:takewhile(fun (C) -> C =/= $; end, V) + end. + +%% @spec get_combined_value(key(), headers()) -> string() | undefined +%% @doc Return the value from the given header using a case insensitive search. +%% If the value of the header is a comma-separated list where holds values +%% are all identical, the identical value will be returned. +%% undefined will be returned for keys that are not present or the +%% values in the list are not the same. +%% +%% NOTE: The process isn't designed for a general purpose. If you need +%% to access all values in the combined header, please refer to +%% '''tokenize_header_value/1'''. +%% +%% Section 4.2 of the RFC 2616 (HTTP 1.1) describes multiple message-header +%% fields with the same field-name may be present in a message if and only +%% if the entire field-value for that header field is defined as a +%% comma-separated list [i.e., #(values)]. +get_combined_value(K, T) -> + case get_value(K, T) of + undefined -> + undefined; + V -> + case sets:to_list(sets:from_list(tokenize_header_value(V))) of + [Val] -> + Val; + _ -> + undefined + end + end. + +%% @spec lookup(key(), headers()) -> {value, {key(), string()}} | none +%% @doc Return the case preserved key and value for the given header using +%% a case insensitive search. none will be returned for keys that are +%% not present. +lookup(K, T) -> + case gb_trees:lookup(normalize(K), T) of + {value, {K0, V}} -> + {value, {K0, expand(V)}}; + none -> + none + end. + +%% @spec default(key(), value(), headers()) -> headers() +%% @doc Insert the pair into the headers if it does not already exist. +default(K, V, T) -> + K1 = normalize(K), + V1 = any_to_list(V), + try gb_trees:insert(K1, {K, V1}, T) + catch + error:{key_exists, _} -> + T + end. + +%% @spec enter(key(), value(), headers()) -> headers() +%% @doc Insert the pair into the headers, replacing any pre-existing key. +enter(K, V, T) -> + K1 = normalize(K), + V1 = any_to_list(V), + gb_trees:enter(K1, {K, V1}, T). + +%% @spec insert(key(), value(), headers()) -> headers() +%% @doc Insert the pair into the headers, merging with any pre-existing key. +%% A merge is done with Value = V0 ++ ", " ++ V1. +insert(K, V, T) -> + K1 = normalize(K), + V1 = any_to_list(V), + try gb_trees:insert(K1, {K, V1}, T) + catch + error:{key_exists, _} -> + {K0, V0} = gb_trees:get(K1, T), + V2 = merge(K1, V1, V0), + gb_trees:update(K1, {K0, V2}, T) + end. + +%% @spec delete_any(key(), headers()) -> headers() +%% @doc Delete the header corresponding to key if it is present. +delete_any(K, T) -> + K1 = normalize(K), + gb_trees:delete_any(K1, T). + +%% Internal API + +tokenize_header_value(undefined) -> + undefined; +tokenize_header_value(V) -> + reversed_tokens(trim_and_reverse(V, false), [], []). + +trim_and_reverse([S | Rest], Reversed) when S=:=$ ; S=:=$\n; S=:=$\t -> + trim_and_reverse(Rest, Reversed); +trim_and_reverse(V, false) -> + trim_and_reverse(lists:reverse(V), true); +trim_and_reverse(V, true) -> + V. + +reversed_tokens([], [], Acc) -> + Acc; +reversed_tokens([], Token, Acc) -> + [Token | Acc]; +reversed_tokens("\"" ++ Rest, [], Acc) -> + case extract_quoted_string(Rest, []) of + {String, NewRest} -> + reversed_tokens(NewRest, [], [String | Acc]); + undefined -> + undefined + end; +reversed_tokens("\"" ++ _Rest, _Token, _Acc) -> + undefined; +reversed_tokens([C | Rest], [], Acc) when C=:=$ ;C=:=$\n;C=:=$\t;C=:=$, -> + reversed_tokens(Rest, [], Acc); +reversed_tokens([C | Rest], Token, Acc) when C=:=$ ;C=:=$\n;C=:=$\t;C=:=$, -> + reversed_tokens(Rest, [], [Token | Acc]); +reversed_tokens([C | Rest], Token, Acc) -> + reversed_tokens(Rest, [C | Token], Acc); +reversed_tokens(_, _, _) -> + undefeined. + +extract_quoted_string([], _Acc) -> + undefined; +extract_quoted_string("\"\\" ++ Rest, Acc) -> + extract_quoted_string(Rest, "\"" ++ Acc); +extract_quoted_string("\"" ++ Rest, Acc) -> + {Acc, Rest}; +extract_quoted_string([C | Rest], Acc) -> + extract_quoted_string(Rest, [C | Acc]). + +expand({array, L}) -> + mochiweb_util:join(lists:reverse(L), ", "); +expand(V) -> + V. + +merge("set-cookie", V1, {array, L}) -> + {array, [V1 | L]}; +merge("set-cookie", V1, V0) -> + {array, [V1, V0]}; +merge(_, V1, V0) -> + V0 ++ ", " ++ V1. + +normalize(K) when is_list(K) -> + string:to_lower(K); +normalize(K) when is_atom(K) -> + normalize(atom_to_list(K)); +normalize(K) when is_binary(K) -> + normalize(binary_to_list(K)). + +any_to_list(V) when is_list(V) -> + V; +any_to_list(V) when is_atom(V) -> + atom_to_list(V); +any_to_list(V) when is_binary(V) -> + binary_to_list(V); +any_to_list(V) when is_integer(V) -> + integer_to_list(V). + +%% +%% Tests. +%% +-ifdef(TEST). +-include_lib("eunit/include/eunit.hrl"). + +make_test() -> + Identity = make([{hdr, foo}]), + ?assertEqual( + Identity, + make(Identity)). + +enter_from_list_test() -> + H = make([{hdr, foo}]), + ?assertEqual( + [{baz, "wibble"}, {hdr, "foo"}], + to_list(enter_from_list([{baz, wibble}], H))), + ?assertEqual( + [{hdr, "bar"}], + to_list(enter_from_list([{hdr, bar}], H))), + ok. + +default_from_list_test() -> + H = make([{hdr, foo}]), + ?assertEqual( + [{baz, "wibble"}, {hdr, "foo"}], + to_list(default_from_list([{baz, wibble}], H))), + ?assertEqual( + [{hdr, "foo"}], + to_list(default_from_list([{hdr, bar}], H))), + ok. + +get_primary_value_test() -> + H = make([{hdr, foo}, {baz, <<"wibble;taco">>}]), + ?assertEqual( + "foo", + get_primary_value(hdr, H)), + ?assertEqual( + undefined, + get_primary_value(bar, H)), + ?assertEqual( + "wibble", + get_primary_value(<<"baz">>, H)), + ok. + +get_combined_value_test() -> + H = make([{hdr, foo}, {baz, <<"wibble,taco">>}, {content_length, "123, 123"}, + {test, " 123, 123, 123 , 123,123 "}, + {test2, "456, 123, 123 , 123"}, + {test3, "123"}, {test4, " 123, "}]), + ?assertEqual( + "foo", + get_combined_value(hdr, H)), + ?assertEqual( + undefined, + get_combined_value(bar, H)), + ?assertEqual( + undefined, + get_combined_value(<<"baz">>, H)), + ?assertEqual( + "123", + get_combined_value(<<"content_length">>, H)), + ?assertEqual( + "123", + get_combined_value(<<"test">>, H)), + ?assertEqual( + undefined, + get_combined_value(<<"test2">>, H)), + ?assertEqual( + "123", + get_combined_value(<<"test3">>, H)), + ?assertEqual( + "123", + get_combined_value(<<"test4">>, H)), + ok. + +set_cookie_test() -> + H = make([{"set-cookie", foo}, {"set-cookie", bar}, {"set-cookie", baz}]), + ?assertEqual( + [{"set-cookie", "foo"}, {"set-cookie", "bar"}, {"set-cookie", "baz"}], + to_list(H)), + ok. + +headers_test() -> + H = ?MODULE:make([{hdr, foo}, {"Hdr", "bar"}, {'Hdr', 2}]), + [{hdr, "foo, bar, 2"}] = ?MODULE:to_list(H), + H1 = ?MODULE:insert(taco, grande, H), + [{hdr, "foo, bar, 2"}, {taco, "grande"}] = ?MODULE:to_list(H1), + H2 = ?MODULE:make([{"Set-Cookie", "foo"}]), + [{"Set-Cookie", "foo"}] = ?MODULE:to_list(H2), + H3 = ?MODULE:insert("Set-Cookie", "bar", H2), + [{"Set-Cookie", "foo"}, {"Set-Cookie", "bar"}] = ?MODULE:to_list(H3), + "foo, bar" = ?MODULE:get_value("set-cookie", H3), + {value, {"Set-Cookie", "foo, bar"}} = ?MODULE:lookup("set-cookie", H3), + undefined = ?MODULE:get_value("shibby", H3), + none = ?MODULE:lookup("shibby", H3), + H4 = ?MODULE:insert("content-type", + "application/x-www-form-urlencoded; charset=utf8", + H3), + "application/x-www-form-urlencoded" = ?MODULE:get_primary_value( + "content-type", H4), + H4 = ?MODULE:delete_any("nonexistent-header", H4), + H3 = ?MODULE:delete_any("content-type", H4), + HB = <<"Content-Length: 47\r\nContent-Type: text/plain\r\n\r\n">>, + H_HB = ?MODULE:from_binary(HB), + H_HB = ?MODULE:from_binary(binary_to_list(HB)), + "47" = ?MODULE:get_value("Content-Length", H_HB), + "text/plain" = ?MODULE:get_value("Content-Type", H_HB), + L_H_HB = ?MODULE:to_list(H_HB), + 2 = length(L_H_HB), + true = lists:member({'Content-Length', "47"}, L_H_HB), + true = lists:member({'Content-Type', "text/plain"}, L_H_HB), + HL = [ <<"Content-Length: 47\r\n">>, <<"Content-Type: text/plain\r\n">> ], + HL2 = [ "Content-Length: 47\r\n", <<"Content-Type: text/plain\r\n">> ], + HL3 = [ <<"Content-Length: 47\r\n">>, "Content-Type: text/plain\r\n" ], + H_HL = ?MODULE:from_binary(HL), + H_HL = ?MODULE:from_binary(HL2), + H_HL = ?MODULE:from_binary(HL3), + "47" = ?MODULE:get_value("Content-Length", H_HL), + "text/plain" = ?MODULE:get_value("Content-Type", H_HL), + L_H_HL = ?MODULE:to_list(H_HL), + 2 = length(L_H_HL), + true = lists:member({'Content-Length', "47"}, L_H_HL), + true = lists:member({'Content-Type', "text/plain"}, L_H_HL), + [] = ?MODULE:to_list(?MODULE:from_binary(<<>>)), + [] = ?MODULE:to_list(?MODULE:from_binary(<<"">>)), + [] = ?MODULE:to_list(?MODULE:from_binary(<<"\r\n">>)), + [] = ?MODULE:to_list(?MODULE:from_binary(<<"\r\n\r\n">>)), + [] = ?MODULE:to_list(?MODULE:from_binary("")), + [] = ?MODULE:to_list(?MODULE:from_binary([<<>>])), + [] = ?MODULE:to_list(?MODULE:from_binary([<<"">>])), + [] = ?MODULE:to_list(?MODULE:from_binary([<<"\r\n">>])), + [] = ?MODULE:to_list(?MODULE:from_binary([<<"\r\n\r\n">>])), + ok. + +tokenize_header_value_test() -> + ?assertEqual(["a quote in a \"quote\"."], + tokenize_header_value("\"a quote in a \\\"quote\\\".\"")), + ?assertEqual(["abc"], tokenize_header_value("abc")), + ?assertEqual(["abc", "def"], tokenize_header_value("abc def")), + ?assertEqual(["abc", "def"], tokenize_header_value("abc , def")), + ?assertEqual(["abc", "def"], tokenize_header_value(",abc ,, def,,")), + ?assertEqual(["abc def"], tokenize_header_value("\"abc def\" ")), + ?assertEqual(["abc, def"], tokenize_header_value("\"abc, def\"")), + ?assertEqual(["\\a\\$"], tokenize_header_value("\"\\a\\$\"")), + ?assertEqual(["abc def", "foo, bar", "12345", ""], + tokenize_header_value("\"abc def\" \"foo, bar\" , 12345, \"\"")), + ?assertEqual(undefined, + tokenize_header_value(undefined)), + ?assertEqual(undefined, + tokenize_header_value("umatched quote\"")), + ?assertEqual(undefined, + tokenize_header_value("\"unmatched quote")). + +-endif. diff --git a/rabbitmq-server/plugins-src/mochiweb-wrapper/mochiweb-git/src/mochiweb_html.erl b/rabbitmq-server/plugins-src/mochiweb-wrapper/mochiweb-git/src/mochiweb_html.erl new file mode 100644 index 0000000..3732924 --- /dev/null +++ b/rabbitmq-server/plugins-src/mochiweb-wrapper/mochiweb-git/src/mochiweb_html.erl @@ -0,0 +1,774 @@ +%% @author Bob Ippolito +%% @copyright 2007 Mochi Media, Inc. + +%% @doc Loosely tokenizes and generates parse trees for HTML 4. +-module(mochiweb_html). +-export([tokens/1, parse/1, parse_tokens/1, to_tokens/1, escape/1, + escape_attr/1, to_html/1]). +-compile([export_all]). +-ifdef(TEST). +-export([destack/1, destack/2, is_singleton/1]). +-endif. + +%% This is a macro to placate syntax highlighters.. +-define(QUOTE, $\"). %% $\" +-define(SQUOTE, $\'). %% $\' +-define(ADV_COL(S, N), + S#decoder{column=N+S#decoder.column, + offset=N+S#decoder.offset}). +-define(INC_COL(S), + S#decoder{column=1+S#decoder.column, + offset=1+S#decoder.offset}). +-define(INC_LINE(S), + S#decoder{column=1, + line=1+S#decoder.line, + offset=1+S#decoder.offset}). +-define(INC_CHAR(S, C), + case C of + $\n -> + S#decoder{column=1, + line=1+S#decoder.line, + offset=1+S#decoder.offset}; + _ -> + S#decoder{column=1+S#decoder.column, + offset=1+S#decoder.offset} + end). + +-define(IS_WHITESPACE(C), + (C =:= $\s orelse C =:= $\t orelse C =:= $\r orelse C =:= $\n)). +-define(IS_LITERAL_SAFE(C), + ((C >= $A andalso C =< $Z) orelse (C >= $a andalso C =< $z) + orelse (C >= $0 andalso C =< $9))). +-define(PROBABLE_CLOSE(C), + (C =:= $> orelse ?IS_WHITESPACE(C))). + +-record(decoder, {line=1, + column=1, + offset=0}). + +%% @type html_node() = {string(), [html_attr()], [html_node() | string()]} +%% @type html_attr() = {string(), string()} +%% @type html_token() = html_data() | start_tag() | end_tag() | inline_html() | html_comment() | html_doctype() +%% @type html_data() = {data, string(), Whitespace::boolean()} +%% @type start_tag() = {start_tag, Name, [html_attr()], Singleton::boolean()} +%% @type end_tag() = {end_tag, Name} +%% @type html_comment() = {comment, Comment} +%% @type html_doctype() = {doctype, [Doctype]} +%% @type inline_html() = {'=', iolist()} + +%% External API. + +%% @spec parse(string() | binary()) -> html_node() +%% @doc tokenize and then transform the token stream into a HTML tree. +parse(Input) -> + parse_tokens(tokens(Input)). + +%% @spec parse_tokens([html_token()]) -> html_node() +%% @doc Transform the output of tokens(Doc) into a HTML tree. +parse_tokens(Tokens) when is_list(Tokens) -> + %% Skip over doctype, processing instructions + [{start_tag, Tag, Attrs, false} | Rest] = find_document(Tokens, normal), + {Tree, _} = tree(Rest, [norm({Tag, Attrs})]), + Tree. + +find_document(Tokens=[{start_tag, _Tag, _Attrs, false} | _Rest], Mode) -> + maybe_add_html_tag(Tokens, Mode); +find_document([{doctype, [<<"html">>]} | Rest], _Mode) -> + find_document(Rest, html5); +find_document([_T | Rest], Mode) -> + find_document(Rest, Mode); +find_document([], _Mode) -> + []. + +maybe_add_html_tag(Tokens=[{start_tag, Tag, _Attrs, false} | _], html5) + when Tag =/= <<"html">> -> + [{start_tag, <<"html">>, [], false} | Tokens]; +maybe_add_html_tag(Tokens, _Mode) -> + Tokens. + +%% @spec tokens(StringOrBinary) -> [html_token()] +%% @doc Transform the input UTF-8 HTML into a token stream. +tokens(Input) -> + tokens(iolist_to_binary(Input), #decoder{}, []). + +%% @spec to_tokens(html_node()) -> [html_token()] +%% @doc Convert a html_node() tree to a list of tokens. +to_tokens({Tag0}) -> + to_tokens({Tag0, [], []}); +to_tokens(T={'=', _}) -> + [T]; +to_tokens(T={doctype, _}) -> + [T]; +to_tokens(T={comment, _}) -> + [T]; +to_tokens({Tag0, Acc}) -> + %% This is only allowed in sub-tags: {p, [{"class", "foo"}]} + to_tokens({Tag0, [], Acc}); +to_tokens({Tag0, Attrs, Acc}) -> + Tag = to_tag(Tag0), + case is_singleton(Tag) of + true -> + to_tokens([], [{start_tag, Tag, Attrs, true}]); + false -> + to_tokens([{Tag, Acc}], [{start_tag, Tag, Attrs, false}]) + end. + +%% @spec to_html([html_token()] | html_node()) -> iolist() +%% @doc Convert a list of html_token() to a HTML document. +to_html(Node) when is_tuple(Node) -> + to_html(to_tokens(Node)); +to_html(Tokens) when is_list(Tokens) -> + to_html(Tokens, []). + +%% @spec escape(string() | atom() | binary()) -> binary() +%% @doc Escape a string such that it's safe for HTML (amp; lt; gt;). +escape(B) when is_binary(B) -> + escape(binary_to_list(B), []); +escape(A) when is_atom(A) -> + escape(atom_to_list(A), []); +escape(S) when is_list(S) -> + escape(S, []). + +%% @spec escape_attr(string() | binary() | atom() | integer() | float()) -> binary() +%% @doc Escape a string such that it's safe for HTML attrs +%% (amp; lt; gt; quot;). +escape_attr(B) when is_binary(B) -> + escape_attr(binary_to_list(B), []); +escape_attr(A) when is_atom(A) -> + escape_attr(atom_to_list(A), []); +escape_attr(S) when is_list(S) -> + escape_attr(S, []); +escape_attr(I) when is_integer(I) -> + escape_attr(integer_to_list(I), []); +escape_attr(F) when is_float(F) -> + escape_attr(mochinum:digits(F), []). + +to_html([], Acc) -> + lists:reverse(Acc); +to_html([{'=', Content} | Rest], Acc) -> + to_html(Rest, [Content | Acc]); +to_html([{pi, Bin} | Rest], Acc) -> + Open = [<<">, + Bin, + <<"?>">>], + to_html(Rest, [Open | Acc]); +to_html([{pi, Tag, Attrs} | Rest], Acc) -> + Open = [<<">, + Tag, + attrs_to_html(Attrs, []), + <<"?>">>], + to_html(Rest, [Open | Acc]); +to_html([{comment, Comment} | Rest], Acc) -> + to_html(Rest, [[<<"">>] | Acc]); +to_html([{doctype, Parts} | Rest], Acc) -> + Inside = doctype_to_html(Parts, Acc), + to_html(Rest, [[<<">, Inside, <<">">>] | Acc]); +to_html([{data, Data, _Whitespace} | Rest], Acc) -> + to_html(Rest, [escape(Data) | Acc]); +to_html([{start_tag, Tag, Attrs, Singleton} | Rest], Acc) -> + Open = [<<"<">>, + Tag, + attrs_to_html(Attrs, []), + case Singleton of + true -> <<" />">>; + false -> <<">">> + end], + to_html(Rest, [Open | Acc]); +to_html([{end_tag, Tag} | Rest], Acc) -> + to_html(Rest, [[<<">, Tag, <<">">>] | Acc]). + +doctype_to_html([], Acc) -> + lists:reverse(Acc); +doctype_to_html([Word | Rest], Acc) -> + case lists:all(fun (C) -> ?IS_LITERAL_SAFE(C) end, + binary_to_list(iolist_to_binary(Word))) of + true -> + doctype_to_html(Rest, [[<<" ">>, Word] | Acc]); + false -> + doctype_to_html(Rest, [[<<" \"">>, escape_attr(Word), ?QUOTE] | Acc]) + end. + +attrs_to_html([], Acc) -> + lists:reverse(Acc); +attrs_to_html([{K, V} | Rest], Acc) -> + attrs_to_html(Rest, + [[<<" ">>, escape(K), <<"=\"">>, + escape_attr(V), <<"\"">>] | Acc]). + +escape([], Acc) -> + list_to_binary(lists:reverse(Acc)); +escape("<" ++ Rest, Acc) -> + escape(Rest, lists:reverse("<", Acc)); +escape(">" ++ Rest, Acc) -> + escape(Rest, lists:reverse(">", Acc)); +escape("&" ++ Rest, Acc) -> + escape(Rest, lists:reverse("&", Acc)); +escape([C | Rest], Acc) -> + escape(Rest, [C | Acc]). + +escape_attr([], Acc) -> + list_to_binary(lists:reverse(Acc)); +escape_attr("<" ++ Rest, Acc) -> + escape_attr(Rest, lists:reverse("<", Acc)); +escape_attr(">" ++ Rest, Acc) -> + escape_attr(Rest, lists:reverse(">", Acc)); +escape_attr("&" ++ Rest, Acc) -> + escape_attr(Rest, lists:reverse("&", Acc)); +escape_attr([?QUOTE | Rest], Acc) -> + escape_attr(Rest, lists:reverse(""", Acc)); +escape_attr([C | Rest], Acc) -> + escape_attr(Rest, [C | Acc]). + +to_tag(A) when is_atom(A) -> + norm(atom_to_list(A)); +to_tag(L) -> + norm(L). + +to_tokens([], Acc) -> + lists:reverse(Acc); +to_tokens([{Tag, []} | Rest], Acc) -> + to_tokens(Rest, [{end_tag, to_tag(Tag)} | Acc]); +to_tokens([{Tag0, [{T0} | R1]} | Rest], Acc) -> + %% Allow {br} + to_tokens([{Tag0, [{T0, [], []} | R1]} | Rest], Acc); +to_tokens([{Tag0, [T0={'=', _C0} | R1]} | Rest], Acc) -> + %% Allow {'=', iolist()} + to_tokens([{Tag0, R1} | Rest], [T0 | Acc]); +to_tokens([{Tag0, [T0={comment, _C0} | R1]} | Rest], Acc) -> + %% Allow {comment, iolist()} + to_tokens([{Tag0, R1} | Rest], [T0 | Acc]); +to_tokens([{Tag0, [T0={pi, _S0} | R1]} | Rest], Acc) -> + %% Allow {pi, binary()} + to_tokens([{Tag0, R1} | Rest], [T0 | Acc]); +to_tokens([{Tag0, [T0={pi, _S0, _A0} | R1]} | Rest], Acc) -> + %% Allow {pi, binary(), list()} + to_tokens([{Tag0, R1} | Rest], [T0 | Acc]); +to_tokens([{Tag0, [{T0, A0=[{_, _} | _]} | R1]} | Rest], Acc) -> + %% Allow {p, [{"class", "foo"}]} + to_tokens([{Tag0, [{T0, A0, []} | R1]} | Rest], Acc); +to_tokens([{Tag0, [{T0, C0} | R1]} | Rest], Acc) -> + %% Allow {p, "content"} and {p, <<"content">>} + to_tokens([{Tag0, [{T0, [], C0} | R1]} | Rest], Acc); +to_tokens([{Tag0, [{T0, A1, C0} | R1]} | Rest], Acc) when is_binary(C0) -> + %% Allow {"p", [{"class", "foo"}], <<"content">>} + to_tokens([{Tag0, [{T0, A1, binary_to_list(C0)} | R1]} | Rest], Acc); +to_tokens([{Tag0, [{T0, A1, C0=[C | _]} | R1]} | Rest], Acc) + when is_integer(C) -> + %% Allow {"p", [{"class", "foo"}], "content"} + to_tokens([{Tag0, [{T0, A1, [C0]} | R1]} | Rest], Acc); +to_tokens([{Tag0, [{T0, A1, C1} | R1]} | Rest], Acc) -> + %% Native {"p", [{"class", "foo"}], ["content"]} + Tag = to_tag(Tag0), + T1 = to_tag(T0), + case is_singleton(norm(T1)) of + true -> + to_tokens([{Tag, R1} | Rest], [{start_tag, T1, A1, true} | Acc]); + false -> + to_tokens([{T1, C1}, {Tag, R1} | Rest], + [{start_tag, T1, A1, false} | Acc]) + end; +to_tokens([{Tag0, [L | R1]} | Rest], Acc) when is_list(L) -> + %% List text + Tag = to_tag(Tag0), + to_tokens([{Tag, R1} | Rest], [{data, iolist_to_binary(L), false} | Acc]); +to_tokens([{Tag0, [B | R1]} | Rest], Acc) when is_binary(B) -> + %% Binary text + Tag = to_tag(Tag0), + to_tokens([{Tag, R1} | Rest], [{data, B, false} | Acc]). + +tokens(B, S=#decoder{offset=O}, Acc) -> + case B of + <<_:O/binary>> -> + lists:reverse(Acc); + _ -> + {Tag, S1} = tokenize(B, S), + case parse_flag(Tag) of + script -> + {Tag2, S2} = tokenize_script(B, S1), + tokens(B, S2, [Tag2, Tag | Acc]); + textarea -> + {Tag2, S2} = tokenize_textarea(B, S1), + tokens(B, S2, [Tag2, Tag | Acc]); + none -> + tokens(B, S1, [Tag | Acc]) + end + end. + +parse_flag({start_tag, B, _, false}) -> + case string:to_lower(binary_to_list(B)) of + "script" -> + script; + "textarea" -> + textarea; + _ -> + none + end; +parse_flag(_) -> + none. + +tokenize(B, S=#decoder{offset=O}) -> + case B of + <<_:O/binary, "", _/binary>> -> + Len = O - Start, + <<_:Start/binary, Raw:Len/binary, _/binary>> = Bin, + {{comment, Raw}, ?ADV_COL(S, 3)}; + <<_:O/binary, C, _/binary>> -> + tokenize_comment(Bin, ?INC_CHAR(S, C), Start); + <<_:Start/binary, Raw/binary>> -> + {{comment, Raw}, S} + end. + +tokenize_script(Bin, S=#decoder{offset=O}) -> + tokenize_script(Bin, S, O). + +tokenize_script(Bin, S=#decoder{offset=O}, Start) -> + case Bin of + %% Just a look-ahead, we want the end_tag separately + <<_:O/binary, $<, $/, SS, CC, RR, II, PP, TT, ZZ, _/binary>> + when (SS =:= $s orelse SS =:= $S) andalso + (CC =:= $c orelse CC =:= $C) andalso + (RR =:= $r orelse RR =:= $R) andalso + (II =:= $i orelse II =:= $I) andalso + (PP =:= $p orelse PP =:= $P) andalso + (TT=:= $t orelse TT =:= $T) andalso + ?PROBABLE_CLOSE(ZZ) -> + Len = O - Start, + <<_:Start/binary, Raw:Len/binary, _/binary>> = Bin, + {{data, Raw, false}, S}; + <<_:O/binary, C, _/binary>> -> + tokenize_script(Bin, ?INC_CHAR(S, C), Start); + <<_:Start/binary, Raw/binary>> -> + {{data, Raw, false}, S} + end. + +tokenize_textarea(Bin, S=#decoder{offset=O}) -> + tokenize_textarea(Bin, S, O). + +tokenize_textarea(Bin, S=#decoder{offset=O}, Start) -> + case Bin of + %% Just a look-ahead, we want the end_tag separately + <<_:O/binary, $<, $/, TT, EE, XX, TT2, AA, RR, EE2, AA2, ZZ, _/binary>> + when (TT =:= $t orelse TT =:= $T) andalso + (EE =:= $e orelse EE =:= $E) andalso + (XX =:= $x orelse XX =:= $X) andalso + (TT2 =:= $t orelse TT2 =:= $T) andalso + (AA =:= $a orelse AA =:= $A) andalso + (RR =:= $r orelse RR =:= $R) andalso + (EE2 =:= $e orelse EE2 =:= $E) andalso + (AA2 =:= $a orelse AA2 =:= $A) andalso + ?PROBABLE_CLOSE(ZZ) -> + Len = O - Start, + <<_:Start/binary, Raw:Len/binary, _/binary>> = Bin, + {{data, Raw, false}, S}; + <<_:O/binary, C, _/binary>> -> + tokenize_textarea(Bin, ?INC_CHAR(S, C), Start); + <<_:Start/binary, Raw/binary>> -> + {{data, Raw, false}, S} + end. diff --git a/rabbitmq-server/plugins-src/mochiweb-wrapper/mochiweb-git/src/mochiweb_http.erl b/rabbitmq-server/plugins-src/mochiweb-wrapper/mochiweb-git/src/mochiweb_http.erl new file mode 100644 index 0000000..ae6410f --- /dev/null +++ b/rabbitmq-server/plugins-src/mochiweb-wrapper/mochiweb-git/src/mochiweb_http.erl @@ -0,0 +1,268 @@ +%% @author Bob Ippolito +%% @copyright 2007 Mochi Media, Inc. + +%% @doc HTTP server. + +-module(mochiweb_http). +-author('bob@mochimedia.com'). +-export([start/1, start_link/1, stop/0, stop/1]). +-export([loop/2]). +-export([after_response/2, reentry/1]). +-export([parse_range_request/1, range_skip_length/2]). + +-define(REQUEST_RECV_TIMEOUT, 300000). %% timeout waiting for request line +-define(HEADERS_RECV_TIMEOUT, 30000). %% timeout waiting for headers + +-define(MAX_HEADERS, 1000). +-define(DEFAULTS, [{name, ?MODULE}, + {port, 8888}]). + +parse_options(Options) -> + {loop, HttpLoop} = proplists:lookup(loop, Options), + Loop = {?MODULE, loop, [HttpLoop]}, + Options1 = [{loop, Loop} | proplists:delete(loop, Options)], + mochilists:set_defaults(?DEFAULTS, Options1). + +stop() -> + mochiweb_socket_server:stop(?MODULE). + +stop(Name) -> + mochiweb_socket_server:stop(Name). + +%% @spec start(Options) -> ServerRet +%% Options = [option()] +%% Option = {name, atom()} | {ip, string() | tuple()} | {backlog, integer()} +%% | {nodelay, boolean()} | {acceptor_pool_size, integer()} +%% | {ssl, boolean()} | {profile_fun, undefined | (Props) -> ok} +%% | {link, false} +%% @doc Start a mochiweb server. +%% profile_fun is used to profile accept timing. +%% After each accept, if defined, profile_fun is called with a proplist of a subset of the mochiweb_socket_server state and timing information. +%% The proplist is as follows: [{name, Name}, {port, Port}, {active_sockets, ActiveSockets}, {timing, Timing}]. +%% @end +start(Options) -> + mochiweb_socket_server:start(parse_options(Options)). + +start_link(Options) -> + mochiweb_socket_server:start_link(parse_options(Options)). + +loop(Socket, Body) -> + ok = mochiweb_socket:setopts(Socket, [{packet, http}]), + request(Socket, Body). + +-ifdef(gen_tcp_r15b_workaround). +-define(R15B_GEN_TCP_FIX, {tcp_error,_,emsgsize} -> + % R15B02 returns this then closes the socket, so close and exit + mochiweb_socket:close(Socket), + exit(normal); + ). +-else. +-define(R15B_GEN_TCP_FIX,). +-endif. + +request(Socket, Body) -> + ok = mochiweb_socket:setopts(Socket, [{active, once}]), + receive + {Protocol, _, {http_request, Method, Path, Version}} when Protocol == http orelse Protocol == ssl -> + ok = mochiweb_socket:setopts(Socket, [{packet, httph}]), + headers(Socket, {Method, Path, Version}, [], Body, 0); + {Protocol, _, {http_error, "\r\n"}} when Protocol == http orelse Protocol == ssl -> + request(Socket, Body); + {Protocol, _, {http_error, "\n"}} when Protocol == http orelse Protocol == ssl -> + request(Socket, Body); + {tcp_closed, _} -> + mochiweb_socket:close(Socket), + exit(normal); + {ssl_closed, _} -> + mochiweb_socket:close(Socket), + exit(normal); + ?R15B_GEN_TCP_FIX + _Other -> + handle_invalid_request(Socket) + after ?REQUEST_RECV_TIMEOUT -> + mochiweb_socket:close(Socket), + exit(normal) + end. + +reentry(Body) -> + fun (Req) -> + ?MODULE:after_response(Body, Req) + end. + +headers(Socket, Request, Headers, _Body, ?MAX_HEADERS) -> + %% Too many headers sent, bad request. + ok = mochiweb_socket:setopts(Socket, [{packet, raw}]), + handle_invalid_request(Socket, Request, Headers); +headers(Socket, Request, Headers, Body, HeaderCount) -> + ok = mochiweb_socket:setopts(Socket, [{active, once}]), + receive + {Protocol, _, http_eoh} when Protocol == http orelse Protocol == ssl -> + Req = new_request(Socket, Request, Headers), + call_body(Body, Req), + ?MODULE:after_response(Body, Req); + {Protocol, _, {http_header, _, Name, _, Value}} when Protocol == http orelse Protocol == ssl -> + headers(Socket, Request, [{Name, Value} | Headers], Body, + 1 + HeaderCount); + {tcp_closed, _} -> + mochiweb_socket:close(Socket), + exit(normal); + ?R15B_GEN_TCP_FIX + _Other -> + handle_invalid_request(Socket, Request, Headers) + after ?HEADERS_RECV_TIMEOUT -> + mochiweb_socket:close(Socket), + exit(normal) + end. + +call_body({M, F, A}, Req) -> + erlang:apply(M, F, [Req | A]); +call_body({M, F}, Req) -> + M:F(Req); +call_body(Body, Req) -> + Body(Req). + +%% -spec handle_invalid_request(term()) -> no_return(). +handle_invalid_request(Socket) -> + handle_invalid_request(Socket, {'GET', {abs_path, "/"}, {0,9}}, []), + exit(normal). + +%% -spec handle_invalid_request(term(), term(), term()) -> no_return(). +handle_invalid_request(Socket, Request, RevHeaders) -> + Req = new_request(Socket, Request, RevHeaders), + Req:respond({400, [], []}), + mochiweb_socket:close(Socket), + exit(normal). + +new_request(Socket, Request, RevHeaders) -> + ok = mochiweb_socket:setopts(Socket, [{packet, raw}]), + mochiweb:new_request({Socket, Request, lists:reverse(RevHeaders)}). + +after_response(Body, Req) -> + Socket = Req:get(socket), + case Req:should_close() of + true -> + mochiweb_socket:close(Socket), + exit(normal); + false -> + Req:cleanup(), + erlang:garbage_collect(), + ?MODULE:loop(Socket, Body) + end. + +parse_range_request("bytes=0-") -> + undefined; +parse_range_request(RawRange) when is_list(RawRange) -> + try + "bytes=" ++ RangeString = RawRange, + Ranges = string:tokens(RangeString, ","), + lists:map(fun ("-" ++ V) -> + {none, list_to_integer(V)}; + (R) -> + case string:tokens(R, "-") of + [S1, S2] -> + {list_to_integer(S1), list_to_integer(S2)}; + [S] -> + {list_to_integer(S), none} + end + end, + Ranges) + catch + _:_ -> + fail + end. + +range_skip_length(Spec, Size) -> + case Spec of + {none, R} when R =< Size, R >= 0 -> + {Size - R, R}; + {none, _OutOfRange} -> + {0, Size}; + {R, none} when R >= 0, R < Size -> + {R, Size - R}; + {_OutOfRange, none} -> + invalid_range; + {Start, End} when 0 =< Start, Start =< End, End < Size -> + {Start, End - Start + 1}; + {Start, End} when 0 =< Start, Start =< End, End >= Size -> + {Start, Size - Start}; + {_OutOfRange, _End} -> + invalid_range + end. + +%% +%% Tests +%% +-ifdef(TEST). +-include_lib("eunit/include/eunit.hrl"). + +range_test() -> + %% valid, single ranges + ?assertEqual([{20, 30}], parse_range_request("bytes=20-30")), + ?assertEqual([{20, none}], parse_range_request("bytes=20-")), + ?assertEqual([{none, 20}], parse_range_request("bytes=-20")), + + %% trivial single range + ?assertEqual(undefined, parse_range_request("bytes=0-")), + + %% invalid, single ranges + ?assertEqual(fail, parse_range_request("")), + ?assertEqual(fail, parse_range_request("garbage")), + ?assertEqual(fail, parse_range_request("bytes=-20-30")), + + %% valid, multiple range + ?assertEqual( + [{20, 30}, {50, 100}, {110, 200}], + parse_range_request("bytes=20-30,50-100,110-200")), + ?assertEqual( + [{20, none}, {50, 100}, {none, 200}], + parse_range_request("bytes=20-,50-100,-200")), + + %% no ranges + ?assertEqual([], parse_range_request("bytes=")), + ok. + +range_skip_length_test() -> + Body = <<"012345678901234567890123456789012345678901234567890123456789">>, + BodySize = byte_size(Body), %% 60 + BodySize = 60, + + %% these values assume BodySize =:= 60 + ?assertEqual({1,9}, range_skip_length({1,9}, BodySize)), %% 1-9 + ?assertEqual({10,10}, range_skip_length({10,19}, BodySize)), %% 10-19 + ?assertEqual({40, 20}, range_skip_length({none, 20}, BodySize)), %% -20 + ?assertEqual({30, 30}, range_skip_length({30, none}, BodySize)), %% 30- + + %% valid edge cases for range_skip_length + ?assertEqual({BodySize, 0}, range_skip_length({none, 0}, BodySize)), + ?assertEqual({0, BodySize}, range_skip_length({none, BodySize}, BodySize)), + ?assertEqual({0, BodySize}, range_skip_length({0, none}, BodySize)), + BodySizeLess1 = BodySize - 1, + ?assertEqual({BodySizeLess1, 1}, + range_skip_length({BodySize - 1, none}, BodySize)), + ?assertEqual({BodySizeLess1, 1}, + range_skip_length({BodySize - 1, BodySize+5}, BodySize)), + ?assertEqual({BodySizeLess1, 1}, + range_skip_length({BodySize - 1, BodySize}, BodySize)), + + %% out of range, return whole thing + ?assertEqual({0, BodySize}, + range_skip_length({none, BodySize + 1}, BodySize)), + ?assertEqual({0, BodySize}, + range_skip_length({none, -1}, BodySize)), + ?assertEqual({0, BodySize}, + range_skip_length({0, BodySize + 1}, BodySize)), + + %% invalid ranges + ?assertEqual(invalid_range, + range_skip_length({-1, 30}, BodySize)), + ?assertEqual(invalid_range, + range_skip_length({-1, BodySize + 1}, BodySize)), + ?assertEqual(invalid_range, + range_skip_length({BodySize, 40}, BodySize)), + ?assertEqual(invalid_range, + range_skip_length({-1, none}, BodySize)), + ?assertEqual(invalid_range, + range_skip_length({BodySize, none}, BodySize)), + ok. + +-endif. diff --git a/rabbitmq-server/plugins-src/mochiweb-wrapper/mochiweb-git/src/mochiweb_io.erl b/rabbitmq-server/plugins-src/mochiweb-wrapper/mochiweb-git/src/mochiweb_io.erl new file mode 100644 index 0000000..8454b43 --- /dev/null +++ b/rabbitmq-server/plugins-src/mochiweb-wrapper/mochiweb-git/src/mochiweb_io.erl @@ -0,0 +1,43 @@ +%% @author Bob Ippolito +%% @copyright 2007 Mochi Media, Inc. + +%% @doc Utilities for dealing with IO devices (open files). + +-module(mochiweb_io). +-author('bob@mochimedia.com'). + +-export([iodevice_stream/3, iodevice_stream/2]). +-export([iodevice_foldl/4, iodevice_foldl/3]). +-export([iodevice_size/1]). +-define(READ_SIZE, 8192). + +iodevice_foldl(F, Acc, IoDevice) -> + iodevice_foldl(F, Acc, IoDevice, ?READ_SIZE). + +iodevice_foldl(F, Acc, IoDevice, BufferSize) -> + case file:read(IoDevice, BufferSize) of + eof -> + Acc; + {ok, Data} -> + iodevice_foldl(F, F(Data, Acc), IoDevice, BufferSize) + end. + +iodevice_stream(Callback, IoDevice) -> + iodevice_stream(Callback, IoDevice, ?READ_SIZE). + +iodevice_stream(Callback, IoDevice, BufferSize) -> + F = fun (Data, ok) -> Callback(Data) end, + ok = iodevice_foldl(F, ok, IoDevice, BufferSize). + +iodevice_size(IoDevice) -> + {ok, Size} = file:position(IoDevice, eof), + {ok, 0} = file:position(IoDevice, bof), + Size. + + +%% +%% Tests +%% +-ifdef(TEST). +-include_lib("eunit/include/eunit.hrl"). +-endif. diff --git a/rabbitmq-server/plugins-src/mochiweb-wrapper/mochiweb-git/src/mochiweb_mime.erl b/rabbitmq-server/plugins-src/mochiweb-wrapper/mochiweb-git/src/mochiweb_mime.erl new file mode 100644 index 0000000..7d9f249 --- /dev/null +++ b/rabbitmq-server/plugins-src/mochiweb-wrapper/mochiweb-git/src/mochiweb_mime.erl @@ -0,0 +1,415 @@ +%% @author Bob Ippolito +%% @copyright 2007 Mochi Media, Inc. + +%% @doc Gives a good MIME type guess based on file extension. + +-module(mochiweb_mime). +-author('bob@mochimedia.com'). +-export([from_extension/1]). + +%% @spec from_extension(S::string()) -> string() | undefined +%% @doc Given a filename extension (e.g. ".html") return a guess for the MIME +%% type such as "text/html". Will return the atom undefined if no good +%% guess is available. + +from_extension(".stl") -> + "application/SLA"; +from_extension(".stp") -> + "application/STEP"; +from_extension(".step") -> + "application/STEP"; +from_extension(".dwg") -> + "application/acad"; +from_extension(".ez") -> + "application/andrew-inset"; +from_extension(".ccad") -> + "application/clariscad"; +from_extension(".drw") -> + "application/drafting"; +from_extension(".tsp") -> + "application/dsptype"; +from_extension(".dxf") -> + "application/dxf"; +from_extension(".xls") -> + "application/excel"; +from_extension(".unv") -> + "application/i-deas"; +from_extension(".jar") -> + "application/java-archive"; +from_extension(".hqx") -> + "application/mac-binhex40"; +from_extension(".cpt") -> + "application/mac-compactpro"; +from_extension(".pot") -> + "application/vnd.ms-powerpoint"; +from_extension(".ppt") -> + "application/vnd.ms-powerpoint"; +from_extension(".dms") -> + "application/octet-stream"; +from_extension(".lha") -> + "application/octet-stream"; +from_extension(".lzh") -> + "application/octet-stream"; +from_extension(".oda") -> + "application/oda"; +from_extension(".ogg") -> + "application/ogg"; +from_extension(".ogm") -> + "application/ogg"; +from_extension(".pdf") -> + "application/pdf"; +from_extension(".pgp") -> + "application/pgp"; +from_extension(".ai") -> + "application/postscript"; +from_extension(".eps") -> + "application/postscript"; +from_extension(".ps") -> + "application/postscript"; +from_extension(".prt") -> + "application/pro_eng"; +from_extension(".rtf") -> + "application/rtf"; +from_extension(".smi") -> + "application/smil"; +from_extension(".smil") -> + "application/smil"; +from_extension(".sol") -> + "application/solids"; +from_extension(".vda") -> + "application/vda"; +from_extension(".xlm") -> + "application/vnd.ms-excel"; +from_extension(".cod") -> + "application/vnd.rim.cod"; +from_extension(".pgn") -> + "application/x-chess-pgn"; +from_extension(".cpio") -> + "application/x-cpio"; +from_extension(".csh") -> + "application/x-csh"; +from_extension(".deb") -> + "application/x-debian-package"; +from_extension(".dcr") -> + "application/x-director"; +from_extension(".dir") -> + "application/x-director"; +from_extension(".dxr") -> + "application/x-director"; +from_extension(".gz") -> + "application/x-gzip"; +from_extension(".hdf") -> + "application/x-hdf"; +from_extension(".ipx") -> + "application/x-ipix"; +from_extension(".ips") -> + "application/x-ipscript"; +from_extension(".js") -> + "application/x-javascript"; +from_extension(".skd") -> + "application/x-koan"; +from_extension(".skm") -> + "application/x-koan"; +from_extension(".skp") -> + "application/x-koan"; +from_extension(".skt") -> + "application/x-koan"; +from_extension(".latex") -> + "application/x-latex"; +from_extension(".lsp") -> + "application/x-lisp"; +from_extension(".scm") -> + "application/x-lotusscreencam"; +from_extension(".mif") -> + "application/x-mif"; +from_extension(".com") -> + "application/x-msdos-program"; +from_extension(".exe") -> + "application/octet-stream"; +from_extension(".cdf") -> + "application/x-netcdf"; +from_extension(".nc") -> + "application/x-netcdf"; +from_extension(".pl") -> + "application/x-perl"; +from_extension(".pm") -> + "application/x-perl"; +from_extension(".rar") -> + "application/x-rar-compressed"; +from_extension(".sh") -> + "application/x-sh"; +from_extension(".shar") -> + "application/x-shar"; +from_extension(".swf") -> + "application/x-shockwave-flash"; +from_extension(".sit") -> + "application/x-stuffit"; +from_extension(".sv4cpio") -> + "application/x-sv4cpio"; +from_extension(".sv4crc") -> + "application/x-sv4crc"; +from_extension(".tar.gz") -> + "application/x-tar-gz"; +from_extension(".tgz") -> + "application/x-tar-gz"; +from_extension(".tar") -> + "application/x-tar"; +from_extension(".tcl") -> + "application/x-tcl"; +from_extension(".texi") -> + "application/x-texinfo"; +from_extension(".texinfo") -> + "application/x-texinfo"; +from_extension(".man") -> + "application/x-troff-man"; +from_extension(".me") -> + "application/x-troff-me"; +from_extension(".ms") -> + "application/x-troff-ms"; +from_extension(".roff") -> + "application/x-troff"; +from_extension(".t") -> + "application/x-troff"; +from_extension(".tr") -> + "application/x-troff"; +from_extension(".ustar") -> + "application/x-ustar"; +from_extension(".src") -> + "application/x-wais-source"; +from_extension(".zip") -> + "application/zip"; +from_extension(".tsi") -> + "audio/TSP-audio"; +from_extension(".au") -> + "audio/basic"; +from_extension(".snd") -> + "audio/basic"; +from_extension(".kar") -> + "audio/midi"; +from_extension(".mid") -> + "audio/midi"; +from_extension(".midi") -> + "audio/midi"; +from_extension(".mp2") -> + "audio/mpeg"; +from_extension(".mp3") -> + "audio/mpeg"; +from_extension(".mpga") -> + "audio/mpeg"; +from_extension(".aif") -> + "audio/x-aiff"; +from_extension(".aifc") -> + "audio/x-aiff"; +from_extension(".aiff") -> + "audio/x-aiff"; +from_extension(".m3u") -> + "audio/x-mpegurl"; +from_extension(".wax") -> + "audio/x-ms-wax"; +from_extension(".wma") -> + "audio/x-ms-wma"; +from_extension(".rpm") -> + "audio/x-pn-realaudio-plugin"; +from_extension(".ram") -> + "audio/x-pn-realaudio"; +from_extension(".rm") -> + "audio/x-pn-realaudio"; +from_extension(".ra") -> + "audio/x-realaudio"; +from_extension(".wav") -> + "audio/x-wav"; +from_extension(".pdb") -> + "chemical/x-pdb"; +from_extension(".ras") -> + "image/cmu-raster"; +from_extension(".gif") -> + "image/gif"; +from_extension(".ief") -> + "image/ief"; +from_extension(".jpe") -> + "image/jpeg"; +from_extension(".jpeg") -> + "image/jpeg"; +from_extension(".jpg") -> + "image/jpeg"; +from_extension(".jp2") -> + "image/jp2"; +from_extension(".png") -> + "image/png"; +from_extension(".tif") -> + "image/tiff"; +from_extension(".tiff") -> + "image/tiff"; +from_extension(".pnm") -> + "image/x-portable-anymap"; +from_extension(".pbm") -> + "image/x-portable-bitmap"; +from_extension(".pgm") -> + "image/x-portable-graymap"; +from_extension(".ppm") -> + "image/x-portable-pixmap"; +from_extension(".rgb") -> + "image/x-rgb"; +from_extension(".xbm") -> + "image/x-xbitmap"; +from_extension(".xwd") -> + "image/x-xwindowdump"; +from_extension(".iges") -> + "model/iges"; +from_extension(".igs") -> + "model/iges"; +from_extension(".mesh") -> + "model/mesh"; +from_extension(".") -> + ""; +from_extension(".msh") -> + "model/mesh"; +from_extension(".silo") -> + "model/mesh"; +from_extension(".vrml") -> + "model/vrml"; +from_extension(".wrl") -> + "model/vrml"; +from_extension(".css") -> + "text/css"; +from_extension(".htm") -> + "text/html"; +from_extension(".html") -> + "text/html"; +from_extension(".asc") -> + "text/plain"; +from_extension(".c") -> + "text/plain"; +from_extension(".cc") -> + "text/plain"; +from_extension(".f90") -> + "text/plain"; +from_extension(".f") -> + "text/plain"; +from_extension(".hh") -> + "text/plain"; +from_extension(".m") -> + "text/plain"; +from_extension(".txt") -> + "text/plain"; +from_extension(".rtx") -> + "text/richtext"; +from_extension(".sgm") -> + "text/sgml"; +from_extension(".sgml") -> + "text/sgml"; +from_extension(".tsv") -> + "text/tab-separated-values"; +from_extension(".jad") -> + "text/vnd.sun.j2me.app-descriptor"; +from_extension(".etx") -> + "text/x-setext"; +from_extension(".xml") -> + "application/xml"; +from_extension(".dl") -> + "video/dl"; +from_extension(".fli") -> + "video/fli"; +from_extension(".flv") -> + "video/x-flv"; +from_extension(".gl") -> + "video/gl"; +from_extension(".mp4") -> + "video/mp4"; +from_extension(".mpe") -> + "video/mpeg"; +from_extension(".mpeg") -> + "video/mpeg"; +from_extension(".mpg") -> + "video/mpeg"; +from_extension(".mov") -> + "video/quicktime"; +from_extension(".qt") -> + "video/quicktime"; +from_extension(".viv") -> + "video/vnd.vivo"; +from_extension(".vivo") -> + "video/vnd.vivo"; +from_extension(".asf") -> + "video/x-ms-asf"; +from_extension(".asx") -> + "video/x-ms-asx"; +from_extension(".wmv") -> + "video/x-ms-wmv"; +from_extension(".wmx") -> + "video/x-ms-wmx"; +from_extension(".wvx") -> + "video/x-ms-wvx"; +from_extension(".avi") -> + "video/x-msvideo"; +from_extension(".movie") -> + "video/x-sgi-movie"; +from_extension(".mime") -> + "www/mime"; +from_extension(".ice") -> + "x-conference/x-cooltalk"; +from_extension(".vrm") -> + "x-world/x-vrml"; +from_extension(".spx") -> + "audio/ogg"; +from_extension(".xhtml") -> + "application/xhtml+xml"; +from_extension(".bz2") -> + "application/x-bzip2"; +from_extension(".doc") -> + "application/msword"; +from_extension(".z") -> + "application/x-compress"; +from_extension(".ico") -> + "image/x-icon"; +from_extension(".bmp") -> + "image/bmp"; +from_extension(".m4a") -> + "audio/mpeg"; +from_extension(".csv") -> + "text/csv"; +from_extension(".eot") -> + "application/vnd.ms-fontobject"; +from_extension(".m4v") -> + "video/mp4"; +from_extension(".svg") -> + "image/svg+xml"; +from_extension(".svgz") -> + "image/svg+xml"; +from_extension(".ttc") -> + "application/x-font-ttf"; +from_extension(".ttf") -> + "application/x-font-ttf"; +from_extension(".vcf") -> + "text/x-vcard"; +from_extension(".webm") -> + "video/web"; +from_extension(".webp") -> + "image/web"; +from_extension(".woff") -> + "application/x-font-woff"; +from_extension(".otf") -> + "font/opentype"; +from_extension(_) -> + undefined. + +%% +%% Tests +%% +-ifdef(TEST). +-include_lib("eunit/include/eunit.hrl"). + +exhaustive_from_extension_test() -> + T = mochiweb_cover:clause_lookup_table(?MODULE, from_extension), + [?assertEqual(V, from_extension(K)) || {K, V} <- T]. + +from_extension_test() -> + ?assertEqual("text/html", + from_extension(".html")), + ?assertEqual(undefined, + from_extension("")), + ?assertEqual(undefined, + from_extension(".wtf")), + ok. + +-endif. diff --git a/rabbitmq-server/plugins-src/mochiweb-wrapper/mochiweb-git/src/mochiweb_multipart.erl b/rabbitmq-server/plugins-src/mochiweb-wrapper/mochiweb-git/src/mochiweb_multipart.erl new file mode 100644 index 0000000..a4857d6 --- /dev/null +++ b/rabbitmq-server/plugins-src/mochiweb-wrapper/mochiweb-git/src/mochiweb_multipart.erl @@ -0,0 +1,872 @@ +%% @author Bob Ippolito +%% @copyright 2007 Mochi Media, Inc. + +%% @doc Utilities for parsing multipart/form-data. + +-module(mochiweb_multipart). +-author('bob@mochimedia.com'). + +-export([parse_form/1, parse_form/2]). +-export([parse_multipart_request/2]). +-export([parts_to_body/3, parts_to_multipart_body/4]). +-export([default_file_handler/2]). + +-define(CHUNKSIZE, 4096). + +-record(mp, {state, boundary, length, buffer, callback, req}). + +%% TODO: DOCUMENT THIS MODULE. +%% @type key() = atom() | string() | binary(). +%% @type value() = atom() | iolist() | integer(). +%% @type header() = {key(), value()}. +%% @type bodypart() = {Start::integer(), End::integer(), Body::iolist()}. +%% @type formfile() = {Name::string(), ContentType::string(), Content::binary()}. +%% @type request(). +%% @type file_handler() = (Filename::string(), ContentType::string()) -> file_handler_callback(). +%% @type file_handler_callback() = (binary() | eof) -> file_handler_callback() | term(). + +%% @spec parts_to_body([bodypart()], ContentType::string(), +%% Size::integer()) -> {[header()], iolist()} +%% @doc Return {[header()], iolist()} representing the body for the given +%% parts, may be a single part or multipart. +parts_to_body([{Start, End, Body}], ContentType, Size) -> + HeaderList = [{"Content-Type", ContentType}, + {"Content-Range", + ["bytes ", + mochiweb_util:make_io(Start), "-", mochiweb_util:make_io(End), + "/", mochiweb_util:make_io(Size)]}], + {HeaderList, Body}; +parts_to_body(BodyList, ContentType, Size) when is_list(BodyList) -> + parts_to_multipart_body(BodyList, ContentType, Size, + mochihex:to_hex(mochiweb_util:rand_bytes(8))). + +%% @spec parts_to_multipart_body([bodypart()], ContentType::string(), +%% Size::integer(), Boundary::string()) -> +%% {[header()], iolist()} +%% @doc Return {[header()], iolist()} representing the body for the given +%% parts, always a multipart response. +parts_to_multipart_body(BodyList, ContentType, Size, Boundary) -> + HeaderList = [{"Content-Type", + ["multipart/byteranges; ", + "boundary=", Boundary]}], + MultiPartBody = multipart_body(BodyList, ContentType, Boundary, Size), + + {HeaderList, MultiPartBody}. + +%% @spec multipart_body([bodypart()], ContentType::string(), +%% Boundary::string(), Size::integer()) -> iolist() +%% @doc Return the representation of a multipart body for the given [bodypart()]. +multipart_body([], _ContentType, Boundary, _Size) -> + ["--", Boundary, "--\r\n"]; +multipart_body([{Start, End, Body} | BodyList], ContentType, Boundary, Size) -> + ["--", Boundary, "\r\n", + "Content-Type: ", ContentType, "\r\n", + "Content-Range: ", + "bytes ", mochiweb_util:make_io(Start), "-", mochiweb_util:make_io(End), + "/", mochiweb_util:make_io(Size), "\r\n\r\n", + Body, "\r\n" + | multipart_body(BodyList, ContentType, Boundary, Size)]. + +%% @spec parse_form(request()) -> [{string(), string() | formfile()}] +%% @doc Parse a multipart form from the given request using the in-memory +%% default_file_handler/2. +parse_form(Req) -> + parse_form(Req, fun default_file_handler/2). + +%% @spec parse_form(request(), F::file_handler()) -> [{string(), string() | term()}] +%% @doc Parse a multipart form from the given request using the given file_handler(). +parse_form(Req, FileHandler) -> + Callback = fun (Next) -> parse_form_outer(Next, FileHandler, []) end, + {_, _, Res} = parse_multipart_request(Req, Callback), + Res. + +parse_form_outer(eof, _, Acc) -> + lists:reverse(Acc); +parse_form_outer({headers, H}, FileHandler, State) -> + {"form-data", H1} = proplists:get_value("content-disposition", H), + Name = proplists:get_value("name", H1), + Filename = proplists:get_value("filename", H1), + case Filename of + undefined -> + fun (Next) -> + parse_form_value(Next, {Name, []}, FileHandler, State) + end; + _ -> + ContentType = proplists:get_value("content-type", H), + Handler = FileHandler(Filename, ContentType), + fun (Next) -> + parse_form_file(Next, {Name, Handler}, FileHandler, State) + end + end. + +parse_form_value(body_end, {Name, Acc}, FileHandler, State) -> + Value = binary_to_list(iolist_to_binary(lists:reverse(Acc))), + State1 = [{Name, Value} | State], + fun (Next) -> parse_form_outer(Next, FileHandler, State1) end; +parse_form_value({body, Data}, {Name, Acc}, FileHandler, State) -> + Acc1 = [Data | Acc], + fun (Next) -> parse_form_value(Next, {Name, Acc1}, FileHandler, State) end. + +parse_form_file(body_end, {Name, Handler}, FileHandler, State) -> + Value = Handler(eof), + State1 = [{Name, Value} | State], + fun (Next) -> parse_form_outer(Next, FileHandler, State1) end; +parse_form_file({body, Data}, {Name, Handler}, FileHandler, State) -> + H1 = Handler(Data), + fun (Next) -> parse_form_file(Next, {Name, H1}, FileHandler, State) end. + +default_file_handler(Filename, ContentType) -> + default_file_handler_1(Filename, ContentType, []). + +default_file_handler_1(Filename, ContentType, Acc) -> + fun(eof) -> + Value = iolist_to_binary(lists:reverse(Acc)), + {Filename, ContentType, Value}; + (Next) -> + default_file_handler_1(Filename, ContentType, [Next | Acc]) + end. + +parse_multipart_request(Req, Callback) -> + %% TODO: Support chunked? + Length = list_to_integer(Req:get_combined_header_value("content-length")), + Boundary = iolist_to_binary( + get_boundary(Req:get_header_value("content-type"))), + Prefix = <<"\r\n--", Boundary/binary>>, + BS = byte_size(Boundary), + Chunk = read_chunk(Req, Length), + Length1 = Length - byte_size(Chunk), + <<"--", Boundary:BS/binary, "\r\n", Rest/binary>> = Chunk, + feed_mp(headers, flash_multipart_hack(#mp{boundary=Prefix, + length=Length1, + buffer=Rest, + callback=Callback, + req=Req})). + +parse_headers(<<>>) -> + []; +parse_headers(Binary) -> + parse_headers(Binary, []). + +parse_headers(Binary, Acc) -> + case find_in_binary(<<"\r\n">>, Binary) of + {exact, N} -> + <> = Binary, + parse_headers(Rest, [split_header(Line) | Acc]); + not_found -> + lists:reverse([split_header(Binary) | Acc]) + end. + +split_header(Line) -> + {Name, [$: | Value]} = lists:splitwith(fun (C) -> C =/= $: end, + binary_to_list(Line)), + {string:to_lower(string:strip(Name)), + mochiweb_util:parse_header(Value)}. + +read_chunk(Req, Length) when Length > 0 -> + case Length of + Length when Length < ?CHUNKSIZE -> + Req:recv(Length); + _ -> + Req:recv(?CHUNKSIZE) + end. + +read_more(State=#mp{length=Length, buffer=Buffer, req=Req}) -> + Data = read_chunk(Req, Length), + Buffer1 = <>, + flash_multipart_hack(State#mp{length=Length - byte_size(Data), + buffer=Buffer1}). + +flash_multipart_hack(State=#mp{length=0, buffer=Buffer, boundary=Prefix}) -> + %% http://code.google.com/p/mochiweb/issues/detail?id=22 + %% Flash doesn't terminate multipart with \r\n properly so we fix it up here + PrefixSize = size(Prefix), + case size(Buffer) - (2 + PrefixSize) of + Seek when Seek >= 0 -> + case Buffer of + <<_:Seek/binary, Prefix:PrefixSize/binary, "--">> -> + Buffer1 = <>, + State#mp{buffer=Buffer1}; + _ -> + State + end; + _ -> + State + end; +flash_multipart_hack(State) -> + State. + +feed_mp(headers, State=#mp{buffer=Buffer, callback=Callback}) -> + {State1, P} = case find_in_binary(<<"\r\n\r\n">>, Buffer) of + {exact, N} -> + {State, N}; + _ -> + S1 = read_more(State), + %% Assume headers must be less than ?CHUNKSIZE + {exact, N} = find_in_binary(<<"\r\n\r\n">>, + S1#mp.buffer), + {S1, N} + end, + <> = State1#mp.buffer, + NextCallback = Callback({headers, parse_headers(Headers)}), + feed_mp(body, State1#mp{buffer=Rest, + callback=NextCallback}); +feed_mp(body, State=#mp{boundary=Prefix, buffer=Buffer, callback=Callback}) -> + Boundary = find_boundary(Prefix, Buffer), + case Boundary of + {end_boundary, Start, Skip} -> + <> = Buffer, + C1 = Callback({body, Data}), + C2 = C1(body_end), + {State#mp.length, Rest, C2(eof)}; + {next_boundary, Start, Skip} -> + <> = Buffer, + C1 = Callback({body, Data}), + feed_mp(headers, State#mp{callback=C1(body_end), + buffer=Rest}); + {maybe, Start} -> + <> = Buffer, + feed_mp(body, read_more(State#mp{callback=Callback({body, Data}), + buffer=Rest})); + not_found -> + {Data, Rest} = {Buffer, <<>>}, + feed_mp(body, read_more(State#mp{callback=Callback({body, Data}), + buffer=Rest})) + end. + +get_boundary(ContentType) -> + {"multipart/form-data", Opts} = mochiweb_util:parse_header(ContentType), + case proplists:get_value("boundary", Opts) of + S when is_list(S) -> + S + end. + +%% @spec find_in_binary(Pattern::binary(), Data::binary()) -> +%% {exact, N} | {partial, N, K} | not_found +%% @doc Searches for the given pattern in the given binary. +find_in_binary(P, Data) when size(P) > 0 -> + PS = size(P), + DS = size(Data), + case DS - PS of + Last when Last < 0 -> + partial_find(P, Data, 0, DS); + Last -> + case binary:match(Data, P) of + {Pos, _} -> {exact, Pos}; + nomatch -> partial_find(P, Data, Last+1, PS-1) + end + end. + +partial_find(_B, _D, _N, 0) -> + not_found; +partial_find(B, D, N, K) -> + <> = B, + case D of + <<_Skip:N/binary, B1:K/binary>> -> + {partial, N, K}; + _ -> + partial_find(B, D, 1 + N, K - 1) + end. + +find_boundary(Prefix, Data) -> + case find_in_binary(Prefix, Data) of + {exact, Skip} -> + PrefixSkip = Skip + size(Prefix), + case Data of + <<_:PrefixSkip/binary, "\r\n", _/binary>> -> + {next_boundary, Skip, size(Prefix) + 2}; + <<_:PrefixSkip/binary, "--\r\n", _/binary>> -> + {end_boundary, Skip, size(Prefix) + 4}; + _ when size(Data) < PrefixSkip + 4 -> + %% Underflow + {maybe, Skip}; + _ -> + %% False positive + not_found + end; + {partial, Skip, Length} when (Skip + Length) =:= size(Data) -> + %% Underflow + {maybe, Skip}; + _ -> + not_found + end. + +%% +%% Tests +%% +-ifdef(TEST). +-include_lib("eunit/include/eunit.hrl"). + +ssl_cert_opts() -> + EbinDir = filename:dirname(code:which(?MODULE)), + CertDir = filename:join([EbinDir, "..", "support", "test-materials"]), + CertFile = filename:join(CertDir, "test_ssl_cert.pem"), + KeyFile = filename:join(CertDir, "test_ssl_key.pem"), + [{certfile, CertFile}, {keyfile, KeyFile}]. + +with_socket_server(Transport, ServerFun, ClientFun) -> + ServerOpts0 = [{ip, "127.0.0.1"}, {port, 0}, {loop, ServerFun}], + ServerOpts = case Transport of + plain -> + ServerOpts0; + ssl -> + ServerOpts0 ++ [{ssl, true}, {ssl_opts, ssl_cert_opts()}] + end, + {ok, Server} = mochiweb_socket_server:start_link(ServerOpts), + Port = mochiweb_socket_server:get(Server, port), + ClientOpts = [binary, {active, false}], + {ok, Client} = case Transport of + plain -> + gen_tcp:connect("127.0.0.1", Port, ClientOpts); + ssl -> + ClientOpts1 = [{ssl_imp, new} | ClientOpts], + {ok, SslSocket} = ssl:connect("127.0.0.1", Port, ClientOpts1), + {ok, {ssl, SslSocket}} + end, + Res = (catch ClientFun(Client)), + mochiweb_socket_server:stop(Server), + Res. + +fake_request(Socket, ContentType, Length) -> + mochiweb_request:new(Socket, + 'POST', + "/multipart", + {1,1}, + mochiweb_headers:make( + [{"content-type", ContentType}, + {"content-length", Length}])). + +test_callback({body, <<>>}, Rest=[body_end | _]) -> + %% When expecting the body_end we might get an empty binary + fun (Next) -> test_callback(Next, Rest) end; +test_callback({body, Got}, [{body, Expect} | Rest]) when Got =/= Expect -> + %% Partial response + GotSize = size(Got), + <> = Expect, + fun (Next) -> test_callback(Next, [{body, Expect1} | Rest]) end; +test_callback(Got, [Expect | Rest]) -> + ?assertEqual(Got, Expect), + case Rest of + [] -> + ok; + _ -> + fun (Next) -> test_callback(Next, Rest) end + end. + +parse3_http_test() -> + parse3(plain). + +parse3_https_test() -> + parse3(ssl). + +parse3(Transport) -> + ContentType = "multipart/form-data; boundary=---------------------------7386909285754635891697677882", + BinContent = <<"-----------------------------7386909285754635891697677882\r\nContent-Disposition: form-data; name=\"hidden\"\r\n\r\nmultipart message\r\n-----------------------------7386909285754635891697677882\r\nContent-Disposition: form-data; name=\"file\"; filename=\"test_file.txt\"\r\nContent-Type: text/plain\r\n\r\nWoo multiline text file\n\nLa la la\r\n-----------------------------7386909285754635891697677882--\r\n">>, + Expect = [{headers, + [{"content-disposition", + {"form-data", [{"name", "hidden"}]}}]}, + {body, <<"multipart message">>}, + body_end, + {headers, + [{"content-disposition", + {"form-data", [{"name", "file"}, {"filename", "test_file.txt"}]}}, + {"content-type", {"text/plain", []}}]}, + {body, <<"Woo multiline text file\n\nLa la la">>}, + body_end, + eof], + TestCallback = fun (Next) -> test_callback(Next, Expect) end, + ServerFun = fun (Socket) -> + ok = mochiweb_socket:send(Socket, BinContent), + exit(normal) + end, + ClientFun = fun (Socket) -> + Req = fake_request(Socket, ContentType, + byte_size(BinContent)), + Res = parse_multipart_request(Req, TestCallback), + {0, <<>>, ok} = Res, + ok + end, + ok = with_socket_server(Transport, ServerFun, ClientFun), + ok. + +parse2_http_test() -> + parse2(plain). + +parse2_https_test() -> + parse2(ssl). + +parse2(Transport) -> + ContentType = "multipart/form-data; boundary=---------------------------6072231407570234361599764024", + BinContent = <<"-----------------------------6072231407570234361599764024\r\nContent-Disposition: form-data; name=\"hidden\"\r\n\r\nmultipart message\r\n-----------------------------6072231407570234361599764024\r\nContent-Disposition: form-data; name=\"file\"; filename=\"\"\r\nContent-Type: application/octet-stream\r\n\r\n\r\n-----------------------------6072231407570234361599764024--\r\n">>, + Expect = [{headers, + [{"content-disposition", + {"form-data", [{"name", "hidden"}]}}]}, + {body, <<"multipart message">>}, + body_end, + {headers, + [{"content-disposition", + {"form-data", [{"name", "file"}, {"filename", ""}]}}, + {"content-type", {"application/octet-stream", []}}]}, + {body, <<>>}, + body_end, + eof], + TestCallback = fun (Next) -> test_callback(Next, Expect) end, + ServerFun = fun (Socket) -> + ok = mochiweb_socket:send(Socket, BinContent), + exit(normal) + end, + ClientFun = fun (Socket) -> + Req = fake_request(Socket, ContentType, + byte_size(BinContent)), + Res = parse_multipart_request(Req, TestCallback), + {0, <<>>, ok} = Res, + ok + end, + ok = with_socket_server(Transport, ServerFun, ClientFun), + ok. + +parse_form_http_test() -> + do_parse_form(plain). + +parse_form_https_test() -> + do_parse_form(ssl). + +do_parse_form(Transport) -> + ContentType = "multipart/form-data; boundary=AaB03x", + "AaB03x" = get_boundary(ContentType), + Content = mochiweb_util:join( + ["--AaB03x", + "Content-Disposition: form-data; name=\"submit-name\"", + "", + "Larry", + "--AaB03x", + "Content-Disposition: form-data; name=\"files\";" + ++ "filename=\"file1.txt\"", + "Content-Type: text/plain", + "", + "... contents of file1.txt ...", + "--AaB03x--", + ""], "\r\n"), + BinContent = iolist_to_binary(Content), + ServerFun = fun (Socket) -> + ok = mochiweb_socket:send(Socket, BinContent), + exit(normal) + end, + ClientFun = fun (Socket) -> + Req = fake_request(Socket, ContentType, + byte_size(BinContent)), + Res = parse_form(Req), + [{"submit-name", "Larry"}, + {"files", {"file1.txt", {"text/plain",[]}, + <<"... contents of file1.txt ...">>} + }] = Res, + ok + end, + ok = with_socket_server(Transport, ServerFun, ClientFun), + ok. + +parse_http_test() -> + do_parse(plain). + +parse_https_test() -> + do_parse(ssl). + +do_parse(Transport) -> + ContentType = "multipart/form-data; boundary=AaB03x", + "AaB03x" = get_boundary(ContentType), + Content = mochiweb_util:join( + ["--AaB03x", + "Content-Disposition: form-data; name=\"submit-name\"", + "", + "Larry", + "--AaB03x", + "Content-Disposition: form-data; name=\"files\";" + ++ "filename=\"file1.txt\"", + "Content-Type: text/plain", + "", + "... contents of file1.txt ...", + "--AaB03x--", + ""], "\r\n"), + BinContent = iolist_to_binary(Content), + Expect = [{headers, + [{"content-disposition", + {"form-data", [{"name", "submit-name"}]}}]}, + {body, <<"Larry">>}, + body_end, + {headers, + [{"content-disposition", + {"form-data", [{"name", "files"}, {"filename", "file1.txt"}]}}, + {"content-type", {"text/plain", []}}]}, + {body, <<"... contents of file1.txt ...">>}, + body_end, + eof], + TestCallback = fun (Next) -> test_callback(Next, Expect) end, + ServerFun = fun (Socket) -> + ok = mochiweb_socket:send(Socket, BinContent), + exit(normal) + end, + ClientFun = fun (Socket) -> + Req = fake_request(Socket, ContentType, + byte_size(BinContent)), + Res = parse_multipart_request(Req, TestCallback), + {0, <<>>, ok} = Res, + ok + end, + ok = with_socket_server(Transport, ServerFun, ClientFun), + ok. + +parse_partial_body_boundary_http_test() -> + parse_partial_body_boundary(plain). + +parse_partial_body_boundary_https_test() -> + parse_partial_body_boundary(ssl). + +parse_partial_body_boundary(Transport) -> + Boundary = string:copies("$", 2048), + ContentType = "multipart/form-data; boundary=" ++ Boundary, + ?assertEqual(Boundary, get_boundary(ContentType)), + Content = mochiweb_util:join( + ["--" ++ Boundary, + "Content-Disposition: form-data; name=\"submit-name\"", + "", + "Larry", + "--" ++ Boundary, + "Content-Disposition: form-data; name=\"files\";" + ++ "filename=\"file1.txt\"", + "Content-Type: text/plain", + "", + "... contents of file1.txt ...", + "--" ++ Boundary ++ "--", + ""], "\r\n"), + BinContent = iolist_to_binary(Content), + Expect = [{headers, + [{"content-disposition", + {"form-data", [{"name", "submit-name"}]}}]}, + {body, <<"Larry">>}, + body_end, + {headers, + [{"content-disposition", + {"form-data", [{"name", "files"}, {"filename", "file1.txt"}]}}, + {"content-type", {"text/plain", []}} + ]}, + {body, <<"... contents of file1.txt ...">>}, + body_end, + eof], + TestCallback = fun (Next) -> test_callback(Next, Expect) end, + ServerFun = fun (Socket) -> + ok = mochiweb_socket:send(Socket, BinContent), + exit(normal) + end, + ClientFun = fun (Socket) -> + Req = fake_request(Socket, ContentType, + byte_size(BinContent)), + Res = parse_multipart_request(Req, TestCallback), + {0, <<>>, ok} = Res, + ok + end, + ok = with_socket_server(Transport, ServerFun, ClientFun), + ok. + +parse_large_header_http_test() -> + parse_large_header(plain). + +parse_large_header_https_test() -> + parse_large_header(ssl). + +parse_large_header(Transport) -> + ContentType = "multipart/form-data; boundary=AaB03x", + "AaB03x" = get_boundary(ContentType), + Content = mochiweb_util:join( + ["--AaB03x", + "Content-Disposition: form-data; name=\"submit-name\"", + "", + "Larry", + "--AaB03x", + "Content-Disposition: form-data; name=\"files\";" + ++ "filename=\"file1.txt\"", + "Content-Type: text/plain", + "x-large-header: " ++ string:copies("%", 4096), + "", + "... contents of file1.txt ...", + "--AaB03x--", + ""], "\r\n"), + BinContent = iolist_to_binary(Content), + Expect = [{headers, + [{"content-disposition", + {"form-data", [{"name", "submit-name"}]}}]}, + {body, <<"Larry">>}, + body_end, + {headers, + [{"content-disposition", + {"form-data", [{"name", "files"}, {"filename", "file1.txt"}]}}, + {"content-type", {"text/plain", []}}, + {"x-large-header", {string:copies("%", 4096), []}} + ]}, + {body, <<"... contents of file1.txt ...">>}, + body_end, + eof], + TestCallback = fun (Next) -> test_callback(Next, Expect) end, + ServerFun = fun (Socket) -> + ok = mochiweb_socket:send(Socket, BinContent), + exit(normal) + end, + ClientFun = fun (Socket) -> + Req = fake_request(Socket, ContentType, + byte_size(BinContent)), + Res = parse_multipart_request(Req, TestCallback), + {0, <<>>, ok} = Res, + ok + end, + ok = with_socket_server(Transport, ServerFun, ClientFun), + ok. + +find_boundary_test() -> + B = <<"\r\n--X">>, + {next_boundary, 0, 7} = find_boundary(B, <<"\r\n--X\r\nRest">>), + {next_boundary, 1, 7} = find_boundary(B, <<"!\r\n--X\r\nRest">>), + {end_boundary, 0, 9} = find_boundary(B, <<"\r\n--X--\r\nRest">>), + {end_boundary, 1, 9} = find_boundary(B, <<"!\r\n--X--\r\nRest">>), + not_found = find_boundary(B, <<"--X\r\nRest">>), + {maybe, 0} = find_boundary(B, <<"\r\n--X\r">>), + {maybe, 1} = find_boundary(B, <<"!\r\n--X\r">>), + P = <<"\r\n-----------------------------16037454351082272548568224146">>, + B0 = <<55,212,131,77,206,23,216,198,35,87,252,118,252,8,25,211,132,229, + 182,42,29,188,62,175,247,243,4,4,0,59, 13,10,45,45,45,45,45,45,45, + 45,45,45,45,45,45,45,45,45,45,45,45,45,45,45,45,45,45,45,45,45,45, + 49,54,48,51,55,52,53,52,51,53,49>>, + {maybe, 30} = find_boundary(P, B0), + not_found = find_boundary(B, <<"\r\n--XJOPKE">>), + ok. + +find_in_binary_test() -> + {exact, 0} = find_in_binary(<<"foo">>, <<"foobarbaz">>), + {exact, 1} = find_in_binary(<<"oo">>, <<"foobarbaz">>), + {exact, 8} = find_in_binary(<<"z">>, <<"foobarbaz">>), + not_found = find_in_binary(<<"q">>, <<"foobarbaz">>), + {partial, 7, 2} = find_in_binary(<<"azul">>, <<"foobarbaz">>), + {exact, 0} = find_in_binary(<<"foobarbaz">>, <<"foobarbaz">>), + {partial, 0, 3} = find_in_binary(<<"foobar">>, <<"foo">>), + {partial, 1, 3} = find_in_binary(<<"foobar">>, <<"afoo">>), + ok. + +flash_parse_http_test() -> + flash_parse(plain). + +flash_parse_https_test() -> + flash_parse(ssl). + +flash_parse(Transport) -> + ContentType = "multipart/form-data; boundary=----------ei4GI3GI3Ij5Ef1ae0KM7Ij5ei4Ij5", + "----------ei4GI3GI3Ij5Ef1ae0KM7Ij5ei4Ij5" = get_boundary(ContentType), + BinContent = <<"------------ei4GI3GI3Ij5Ef1ae0KM7Ij5ei4Ij5\r\nContent-Disposition: form-data; name=\"Filename\"\r\n\r\nhello.txt\r\n------------ei4GI3GI3Ij5Ef1ae0KM7Ij5ei4Ij5\r\nContent-Disposition: form-data; name=\"success_action_status\"\r\n\r\n201\r\n------------ei4GI3GI3Ij5Ef1ae0KM7Ij5ei4Ij5\r\nContent-Disposition: form-data; name=\"file\"; filename=\"hello.txt\"\r\nContent-Type: application/octet-stream\r\n\r\nhello\n\r\n------------ei4GI3GI3Ij5Ef1ae0KM7Ij5ei4Ij5\r\nContent-Disposition: form-data; name=\"Upload\"\r\n\r\nSubmit Query\r\n------------ei4GI3GI3Ij5Ef1ae0KM7Ij5ei4Ij5--">>, + Expect = [{headers, + [{"content-disposition", + {"form-data", [{"name", "Filename"}]}}]}, + {body, <<"hello.txt">>}, + body_end, + {headers, + [{"content-disposition", + {"form-data", [{"name", "success_action_status"}]}}]}, + {body, <<"201">>}, + body_end, + {headers, + [{"content-disposition", + {"form-data", [{"name", "file"}, {"filename", "hello.txt"}]}}, + {"content-type", {"application/octet-stream", []}}]}, + {body, <<"hello\n">>}, + body_end, + {headers, + [{"content-disposition", + {"form-data", [{"name", "Upload"}]}}]}, + {body, <<"Submit Query">>}, + body_end, + eof], + TestCallback = fun (Next) -> test_callback(Next, Expect) end, + ServerFun = fun (Socket) -> + ok = mochiweb_socket:send(Socket, BinContent), + exit(normal) + end, + ClientFun = fun (Socket) -> + Req = fake_request(Socket, ContentType, + byte_size(BinContent)), + Res = parse_multipart_request(Req, TestCallback), + {0, <<>>, ok} = Res, + ok + end, + ok = with_socket_server(Transport, ServerFun, ClientFun), + ok. + +flash_parse2_http_test() -> + flash_parse2(plain). + +flash_parse2_https_test() -> + flash_parse2(ssl). + +flash_parse2(Transport) -> + ContentType = "multipart/form-data; boundary=----------ei4GI3GI3Ij5Ef1ae0KM7Ij5ei4Ij5", + "----------ei4GI3GI3Ij5Ef1ae0KM7Ij5ei4Ij5" = get_boundary(ContentType), + Chunk = iolist_to_binary(string:copies("%", 4096)), + BinContent = <<"------------ei4GI3GI3Ij5Ef1ae0KM7Ij5ei4Ij5\r\nContent-Disposition: form-data; name=\"Filename\"\r\n\r\nhello.txt\r\n------------ei4GI3GI3Ij5Ef1ae0KM7Ij5ei4Ij5\r\nContent-Disposition: form-data; name=\"success_action_status\"\r\n\r\n201\r\n------------ei4GI3GI3Ij5Ef1ae0KM7Ij5ei4Ij5\r\nContent-Disposition: form-data; name=\"file\"; filename=\"hello.txt\"\r\nContent-Type: application/octet-stream\r\n\r\n", Chunk/binary, "\r\n------------ei4GI3GI3Ij5Ef1ae0KM7Ij5ei4Ij5\r\nContent-Disposition: form-data; name=\"Upload\"\r\n\r\nSubmit Query\r\n------------ei4GI3GI3Ij5Ef1ae0KM7Ij5ei4Ij5--">>, + Expect = [{headers, + [{"content-disposition", + {"form-data", [{"name", "Filename"}]}}]}, + {body, <<"hello.txt">>}, + body_end, + {headers, + [{"content-disposition", + {"form-data", [{"name", "success_action_status"}]}}]}, + {body, <<"201">>}, + body_end, + {headers, + [{"content-disposition", + {"form-data", [{"name", "file"}, {"filename", "hello.txt"}]}}, + {"content-type", {"application/octet-stream", []}}]}, + {body, Chunk}, + body_end, + {headers, + [{"content-disposition", + {"form-data", [{"name", "Upload"}]}}]}, + {body, <<"Submit Query">>}, + body_end, + eof], + TestCallback = fun (Next) -> test_callback(Next, Expect) end, + ServerFun = fun (Socket) -> + ok = mochiweb_socket:send(Socket, BinContent), + exit(normal) + end, + ClientFun = fun (Socket) -> + Req = fake_request(Socket, ContentType, + byte_size(BinContent)), + Res = parse_multipart_request(Req, TestCallback), + {0, <<>>, ok} = Res, + ok + end, + ok = with_socket_server(Transport, ServerFun, ClientFun), + ok. + +parse_headers_test() -> + ?assertEqual([], parse_headers(<<>>)). + +flash_multipart_hack_test() -> + Buffer = <<"prefix-">>, + Prefix = <<"prefix">>, + State = #mp{length=0, buffer=Buffer, boundary=Prefix}, + ?assertEqual(State, + flash_multipart_hack(State)). + +parts_to_body_single_test() -> + {HL, B} = parts_to_body([{0, 5, <<"01234">>}], + "text/plain", + 10), + [{"Content-Range", Range}, + {"Content-Type", Type}] = lists:sort(HL), + ?assertEqual( + <<"bytes 0-5/10">>, + iolist_to_binary(Range)), + ?assertEqual( + <<"text/plain">>, + iolist_to_binary(Type)), + ?assertEqual( + <<"01234">>, + iolist_to_binary(B)), + ok. + +parts_to_body_multi_test() -> + {[{"Content-Type", Type}], + _B} = parts_to_body([{0, 5, <<"01234">>}, {5, 10, <<"56789">>}], + "text/plain", + 10), + ?assertMatch( + <<"multipart/byteranges; boundary=", _/binary>>, + iolist_to_binary(Type)), + ok. + +parts_to_multipart_body_test() -> + {[{"Content-Type", V}], B} = parts_to_multipart_body( + [{0, 5, <<"01234">>}, {5, 10, <<"56789">>}], + "text/plain", + 10, + "BOUNDARY"), + MB = multipart_body( + [{0, 5, <<"01234">>}, {5, 10, <<"56789">>}], + "text/plain", + "BOUNDARY", + 10), + ?assertEqual( + <<"multipart/byteranges; boundary=BOUNDARY">>, + iolist_to_binary(V)), + ?assertEqual( + iolist_to_binary(MB), + iolist_to_binary(B)), + ok. + +multipart_body_test() -> + ?assertEqual( + <<"--BOUNDARY--\r\n">>, + iolist_to_binary(multipart_body([], "text/plain", "BOUNDARY", 0))), + ?assertEqual( + <<"--BOUNDARY\r\n" + "Content-Type: text/plain\r\n" + "Content-Range: bytes 0-5/10\r\n\r\n" + "01234\r\n" + "--BOUNDARY\r\n" + "Content-Type: text/plain\r\n" + "Content-Range: bytes 5-10/10\r\n\r\n" + "56789\r\n" + "--BOUNDARY--\r\n">>, + iolist_to_binary(multipart_body([{0, 5, <<"01234">>}, {5, 10, <<"56789">>}], + "text/plain", + "BOUNDARY", + 10))), + ok. + +%% @todo Move somewhere more appropriate than in the test suite + +multipart_parsing_benchmark_test() -> + run_multipart_parsing_benchmark(1). + +run_multipart_parsing_benchmark(0) -> ok; +run_multipart_parsing_benchmark(N) -> + multipart_parsing_benchmark(), + run_multipart_parsing_benchmark(N-1). + +multipart_parsing_benchmark() -> + ContentType = "multipart/form-data; boundary=----------ei4GI3GI3Ij5Ef1ae0KM7Ij5ei4Ij5", + Chunk = binary:copy(<<"This Is_%Some=Quite0Long4String2Used9For7BenchmarKing.5">>, 102400), + BinContent = <<"------------ei4GI3GI3Ij5Ef1ae0KM7Ij5ei4Ij5\r\nContent-Disposition: form-data; name=\"Filename\"\r\n\r\nhello.txt\r\n------------ei4GI3GI3Ij5Ef1ae0KM7Ij5ei4Ij5\r\nContent-Disposition: form-data; name=\"success_action_status\"\r\n\r\n201\r\n------------ei4GI3GI3Ij5Ef1ae0KM7Ij5ei4Ij5\r\nContent-Disposition: form-data; name=\"file\"; filename=\"hello.txt\"\r\nContent-Type: application/octet-stream\r\n\r\n", Chunk/binary, "\r\n------------ei4GI3GI3Ij5Ef1ae0KM7Ij5ei4Ij5\r\nContent-Disposition: form-data; name=\"Upload\"\r\n\r\nSubmit Query\r\n------------ei4GI3GI3Ij5Ef1ae0KM7Ij5ei4Ij5--">>, + Expect = [{headers, + [{"content-disposition", + {"form-data", [{"name", "Filename"}]}}]}, + {body, <<"hello.txt">>}, + body_end, + {headers, + [{"content-disposition", + {"form-data", [{"name", "success_action_status"}]}}]}, + {body, <<"201">>}, + body_end, + {headers, + [{"content-disposition", + {"form-data", [{"name", "file"}, {"filename", "hello.txt"}]}}, + {"content-type", {"application/octet-stream", []}}]}, + {body, Chunk}, + body_end, + {headers, + [{"content-disposition", + {"form-data", [{"name", "Upload"}]}}]}, + {body, <<"Submit Query">>}, + body_end, + eof], + TestCallback = fun (Next) -> test_callback(Next, Expect) end, + ServerFun = fun (Socket) -> + ok = mochiweb_socket:send(Socket, BinContent), + exit(normal) + end, + ClientFun = fun (Socket) -> + Req = fake_request(Socket, ContentType, + byte_size(BinContent)), + Res = parse_multipart_request(Req, TestCallback), + {0, <<>>, ok} = Res, + ok + end, + ok = with_socket_server(plain, ServerFun, ClientFun), + ok. +-endif. diff --git a/rabbitmq-server/plugins-src/mochiweb-wrapper/mochiweb-git/src/mochiweb_request.erl b/rabbitmq-server/plugins-src/mochiweb-wrapper/mochiweb-git/src/mochiweb_request.erl new file mode 100644 index 0000000..d967bdb --- /dev/null +++ b/rabbitmq-server/plugins-src/mochiweb-wrapper/mochiweb-git/src/mochiweb_request.erl @@ -0,0 +1,857 @@ +%% @author Bob Ippolito +%% @copyright 2007 Mochi Media, Inc. + +%% @doc MochiWeb HTTP Request abstraction. + +-module(mochiweb_request). +-author('bob@mochimedia.com'). + +-include_lib("kernel/include/file.hrl"). +-include("internal.hrl"). + +-define(QUIP, "Any of you quaids got a smint?"). + +-export([new/5]). +-export([get_header_value/2, get_primary_header_value/2, get_combined_header_value/2, get/2, dump/1]). +-export([send/2, recv/2, recv/3, recv_body/1, recv_body/2, stream_body/4]). +-export([start_response/2, start_response_length/2, start_raw_response/2]). +-export([respond/2, ok/2]). +-export([not_found/1, not_found/2]). +-export([parse_post/1, parse_qs/1]). +-export([should_close/1, cleanup/1]). +-export([parse_cookie/1, get_cookie_value/2]). +-export([serve_file/3, serve_file/4]). +-export([accepted_encodings/2]). +-export([accepts_content_type/2, accepted_content_types/2]). + +-define(SAVE_QS, mochiweb_request_qs). +-define(SAVE_PATH, mochiweb_request_path). +-define(SAVE_RECV, mochiweb_request_recv). +-define(SAVE_BODY, mochiweb_request_body). +-define(SAVE_BODY_LENGTH, mochiweb_request_body_length). +-define(SAVE_POST, mochiweb_request_post). +-define(SAVE_COOKIE, mochiweb_request_cookie). +-define(SAVE_FORCE_CLOSE, mochiweb_request_force_close). + +%% @type key() = atom() | string() | binary() +%% @type value() = atom() | string() | binary() | integer() +%% @type headers(). A mochiweb_headers structure. +%% @type request(). A mochiweb_request parameterized module instance. +%% @type response(). A mochiweb_response parameterized module instance. +%% @type ioheaders() = headers() | [{key(), value()}]. + +% 5 minute default idle timeout +-define(IDLE_TIMEOUT, 300000). + +% Maximum recv_body() length of 1MB +-define(MAX_RECV_BODY, 104857600). + +%% @spec new(Socket, Method, RawPath, Version, headers()) -> request() +%% @doc Create a new request instance. +new(Socket, Method, RawPath, Version, Headers) -> + {?MODULE, [Socket, Method, RawPath, Version, Headers]}. + +%% @spec get_header_value(K, request()) -> undefined | Value +%% @doc Get the value of a given request header. +get_header_value(K, {?MODULE, [_Socket, _Method, _RawPath, _Version, Headers]}) -> + mochiweb_headers:get_value(K, Headers). + +get_primary_header_value(K, {?MODULE, [_Socket, _Method, _RawPath, _Version, Headers]}) -> + mochiweb_headers:get_primary_value(K, Headers). + +get_combined_header_value(K, {?MODULE, [_Socket, _Method, _RawPath, _Version, Headers]}) -> + mochiweb_headers:get_combined_value(K, Headers). + +%% @type field() = socket | scheme | method | raw_path | version | headers | peer | path | body_length | range + +%% @spec get(field(), request()) -> term() +%% @doc Return the internal representation of the given field. If +%% socket is requested on a HTTPS connection, then +%% an ssl socket will be returned as {ssl, SslSocket}. +%% You can use SslSocket with the ssl +%% application, eg: ssl:peercert(SslSocket). +get(socket, {?MODULE, [Socket, _Method, _RawPath, _Version, _Headers]}) -> + Socket; +get(scheme, {?MODULE, [Socket, _Method, _RawPath, _Version, _Headers]}) -> + case mochiweb_socket:type(Socket) of + plain -> + http; + ssl -> + https + end; +get(method, {?MODULE, [_Socket, Method, _RawPath, _Version, _Headers]}) -> + Method; +get(raw_path, {?MODULE, [_Socket, _Method, RawPath, _Version, _Headers]}) -> + RawPath; +get(version, {?MODULE, [_Socket, _Method, _RawPath, Version, _Headers]}) -> + Version; +get(headers, {?MODULE, [_Socket, _Method, _RawPath, _Version, Headers]}) -> + Headers; +get(peer, {?MODULE, [Socket, _Method, _RawPath, _Version, _Headers]}=THIS) -> + case mochiweb_socket:peername(Socket) of + {ok, {Addr={10, _, _, _}, _Port}} -> + case get_header_value("x-forwarded-for", THIS) of + undefined -> + inet_parse:ntoa(Addr); + Hosts -> + string:strip(lists:last(string:tokens(Hosts, ","))) + end; + {ok, {{127, 0, 0, 1}, _Port}} -> + case get_header_value("x-forwarded-for", THIS) of + undefined -> + "127.0.0.1"; + Hosts -> + string:strip(lists:last(string:tokens(Hosts, ","))) + end; + {ok, {Addr, _Port}} -> + inet_parse:ntoa(Addr); + {error, enotconn} -> + exit(normal) + end; +get(path, {?MODULE, [_Socket, _Method, RawPath, _Version, _Headers]}) -> + case erlang:get(?SAVE_PATH) of + undefined -> + {Path0, _, _} = mochiweb_util:urlsplit_path(RawPath), + Path = mochiweb_util:unquote(Path0), + put(?SAVE_PATH, Path), + Path; + Cached -> + Cached + end; +get(body_length, {?MODULE, [_Socket, _Method, _RawPath, _Version, _Headers]}=THIS) -> + case erlang:get(?SAVE_BODY_LENGTH) of + undefined -> + BodyLength = body_length(THIS), + put(?SAVE_BODY_LENGTH, {cached, BodyLength}), + BodyLength; + {cached, Cached} -> + Cached + end; +get(range, {?MODULE, [_Socket, _Method, _RawPath, _Version, _Headers]}=THIS) -> + case get_header_value(range, THIS) of + undefined -> + undefined; + RawRange -> + mochiweb_http:parse_range_request(RawRange) + end. + +%% @spec dump(request()) -> {mochiweb_request, [{atom(), term()}]} +%% @doc Dump the internal representation to a "human readable" set of terms +%% for debugging/inspection purposes. +dump({?MODULE, [_Socket, Method, RawPath, Version, Headers]}) -> + {?MODULE, [{method, Method}, + {version, Version}, + {raw_path, RawPath}, + {headers, mochiweb_headers:to_list(Headers)}]}. + +%% @spec send(iodata(), request()) -> ok +%% @doc Send data over the socket. +send(Data, {?MODULE, [Socket, _Method, _RawPath, _Version, _Headers]}) -> + case mochiweb_socket:send(Socket, Data) of + ok -> + ok; + _ -> + exit(normal) + end. + +%% @spec recv(integer(), request()) -> binary() +%% @doc Receive Length bytes from the client as a binary, with the default +%% idle timeout. +recv(Length, {?MODULE, [_Socket, _Method, _RawPath, _Version, _Headers]}=THIS) -> + recv(Length, ?IDLE_TIMEOUT, THIS). + +%% @spec recv(integer(), integer(), request()) -> binary() +%% @doc Receive Length bytes from the client as a binary, with the given +%% Timeout in msec. +recv(Length, Timeout, {?MODULE, [Socket, _Method, _RawPath, _Version, _Headers]}) -> + case mochiweb_socket:recv(Socket, Length, Timeout) of + {ok, Data} -> + put(?SAVE_RECV, true), + Data; + _ -> + exit(normal) + end. + +%% @spec body_length(request()) -> undefined | chunked | unknown_transfer_encoding | integer() +%% @doc Infer body length from transfer-encoding and content-length headers. +body_length({?MODULE, [_Socket, _Method, _RawPath, _Version, _Headers]}=THIS) -> + case get_header_value("transfer-encoding", THIS) of + undefined -> + case get_combined_header_value("content-length", THIS) of + undefined -> + undefined; + Length -> + list_to_integer(Length) + end; + "chunked" -> + chunked; + Unknown -> + {unknown_transfer_encoding, Unknown} + end. + + +%% @spec recv_body(request()) -> binary() +%% @doc Receive the body of the HTTP request (defined by Content-Length). +%% Will only receive up to the default max-body length of 1MB. +recv_body({?MODULE, [_Socket, _Method, _RawPath, _Version, _Headers]}=THIS) -> + recv_body(?MAX_RECV_BODY, THIS). + +%% @spec recv_body(integer(), request()) -> binary() +%% @doc Receive the body of the HTTP request (defined by Content-Length). +%% Will receive up to MaxBody bytes. +recv_body(MaxBody, {?MODULE, [_Socket, _Method, _RawPath, _Version, _Headers]}=THIS) -> + case erlang:get(?SAVE_BODY) of + undefined -> + % we could use a sane constant for max chunk size + Body = stream_body(?MAX_RECV_BODY, fun + ({0, _ChunkedFooter}, {_LengthAcc, BinAcc}) -> + iolist_to_binary(lists:reverse(BinAcc)); + ({Length, Bin}, {LengthAcc, BinAcc}) -> + NewLength = Length + LengthAcc, + if NewLength > MaxBody -> + exit({body_too_large, chunked}); + true -> + {NewLength, [Bin | BinAcc]} + end + end, {0, []}, MaxBody, THIS), + put(?SAVE_BODY, Body), + Body; + Cached -> Cached + end. + +stream_body(MaxChunkSize, ChunkFun, FunState, {?MODULE,[_Socket,_Method,_RawPath,_Version,_Headers]}=THIS) -> + stream_body(MaxChunkSize, ChunkFun, FunState, undefined, THIS). + +stream_body(MaxChunkSize, ChunkFun, FunState, MaxBodyLength, + {?MODULE, [_Socket, _Method, _RawPath, _Version, _Headers]}=THIS) -> + Expect = case get_header_value("expect", THIS) of + undefined -> + undefined; + Value when is_list(Value) -> + string:to_lower(Value) + end, + case Expect of + "100-continue" -> + _ = start_raw_response({100, gb_trees:empty()}, THIS), + ok; + _Else -> + ok + end, + case body_length(THIS) of + undefined -> + undefined; + {unknown_transfer_encoding, Unknown} -> + exit({unknown_transfer_encoding, Unknown}); + chunked -> + % In this case the MaxBody is actually used to + % determine the maximum allowed size of a single + % chunk. + stream_chunked_body(MaxChunkSize, ChunkFun, FunState, THIS); + 0 -> + <<>>; + Length when is_integer(Length) -> + case MaxBodyLength of + MaxBodyLength when is_integer(MaxBodyLength), MaxBodyLength < Length -> + exit({body_too_large, content_length}); + _ -> + stream_unchunked_body(Length, ChunkFun, FunState, THIS) + end + end. + + +%% @spec start_response({integer(), ioheaders()}, request()) -> response() +%% @doc Start the HTTP response by sending the Code HTTP response and +%% ResponseHeaders. The server will set header defaults such as Server +%% and Date if not present in ResponseHeaders. +start_response({Code, ResponseHeaders}, {?MODULE, [_Socket, _Method, _RawPath, _Version, _Headers]}=THIS) -> + HResponse = mochiweb_headers:make(ResponseHeaders), + HResponse1 = mochiweb_headers:default_from_list(server_headers(), + HResponse), + start_raw_response({Code, HResponse1}, THIS). + +%% @spec start_raw_response({integer(), headers()}, request()) -> response() +%% @doc Start the HTTP response by sending the Code HTTP response and +%% ResponseHeaders. +start_raw_response({Code, ResponseHeaders}, {?MODULE, [_Socket, _Method, _RawPath, Version, _Headers]}=THIS) -> + F = fun ({K, V}, Acc) -> + [mochiweb_util:make_io(K), <<": ">>, V, <<"\r\n">> | Acc] + end, + End = lists:foldl(F, [<<"\r\n">>], + mochiweb_headers:to_list(ResponseHeaders)), + send([make_version(Version), make_code(Code), <<"\r\n">> | End], THIS), + mochiweb:new_response({THIS, Code, ResponseHeaders}). + + +%% @spec start_response_length({integer(), ioheaders(), integer()}, request()) -> response() +%% @doc Start the HTTP response by sending the Code HTTP response and +%% ResponseHeaders including a Content-Length of Length. The server +%% will set header defaults such as Server +%% and Date if not present in ResponseHeaders. +start_response_length({Code, ResponseHeaders, Length}, + {?MODULE, [_Socket, _Method, _RawPath, _Version, _Headers]}=THIS) -> + HResponse = mochiweb_headers:make(ResponseHeaders), + HResponse1 = mochiweb_headers:enter("Content-Length", Length, HResponse), + start_response({Code, HResponse1}, THIS). + +%% @spec respond({integer(), ioheaders(), iodata() | chunked | {file, IoDevice}}, request()) -> response() +%% @doc Start the HTTP response with start_response, and send Body to the +%% client (if the get(method) /= 'HEAD'). The Content-Length header +%% will be set by the Body length, and the server will insert header +%% defaults. +respond({Code, ResponseHeaders, {file, IoDevice}}, + {?MODULE, [_Socket, Method, _RawPath, _Version, _Headers]}=THIS) -> + Length = mochiweb_io:iodevice_size(IoDevice), + Response = start_response_length({Code, ResponseHeaders, Length}, THIS), + case Method of + 'HEAD' -> + ok; + _ -> + mochiweb_io:iodevice_stream( + fun (Body) -> send(Body, THIS) end, + IoDevice) + end, + Response; +respond({Code, ResponseHeaders, chunked}, {?MODULE, [_Socket, Method, _RawPath, Version, _Headers]}=THIS) -> + HResponse = mochiweb_headers:make(ResponseHeaders), + HResponse1 = case Method of + 'HEAD' -> + %% This is what Google does, http://www.google.com/ + %% is chunked but HEAD gets Content-Length: 0. + %% The RFC is ambiguous so emulating Google is smart. + mochiweb_headers:enter("Content-Length", "0", + HResponse); + _ when Version >= {1, 1} -> + %% Only use chunked encoding for HTTP/1.1 + mochiweb_headers:enter("Transfer-Encoding", "chunked", + HResponse); + _ -> + %% For pre-1.1 clients we send the data as-is + %% without a Content-Length header and without + %% chunk delimiters. Since the end of the document + %% is now ambiguous we must force a close. + put(?SAVE_FORCE_CLOSE, true), + HResponse + end, + start_response({Code, HResponse1}, THIS); +respond({Code, ResponseHeaders, Body}, {?MODULE, [_Socket, Method, _RawPath, _Version, _Headers]}=THIS) -> + Response = start_response_length({Code, ResponseHeaders, iolist_size(Body)}, THIS), + case Method of + 'HEAD' -> + ok; + _ -> + send(Body, THIS) + end, + Response. + +%% @spec not_found(request()) -> response() +%% @doc Alias for not_found([]). +not_found({?MODULE, [_Socket, _Method, _RawPath, _Version, _Headers]}=THIS) -> + not_found([], THIS). + +%% @spec not_found(ExtraHeaders, request()) -> response() +%% @doc Alias for respond({404, [{"Content-Type", "text/plain"} +%% | ExtraHeaders], <<"Not found.">>}). +not_found(ExtraHeaders, {?MODULE, [_Socket, _Method, _RawPath, _Version, _Headers]}=THIS) -> + respond({404, [{"Content-Type", "text/plain"} | ExtraHeaders], + <<"Not found.">>}, THIS). + +%% @spec ok({value(), iodata()} | {value(), ioheaders(), iodata() | {file, IoDevice}}, request()) -> +%% response() +%% @doc respond({200, [{"Content-Type", ContentType} | Headers], Body}). +ok({ContentType, Body}, {?MODULE, [_Socket, _Method, _RawPath, _Version, _Headers]}=THIS) -> + ok({ContentType, [], Body}, THIS); +ok({ContentType, ResponseHeaders, Body}, {?MODULE, [_Socket, _Method, _RawPath, _Version, _Headers]}=THIS) -> + HResponse = mochiweb_headers:make(ResponseHeaders), + case THIS:get(range) of + X when (X =:= undefined orelse X =:= fail) orelse Body =:= chunked -> + %% http://code.google.com/p/mochiweb/issues/detail?id=54 + %% Range header not supported when chunked, return 200 and provide + %% full response. + HResponse1 = mochiweb_headers:enter("Content-Type", ContentType, + HResponse), + respond({200, HResponse1, Body}, THIS); + Ranges -> + {PartList, Size} = range_parts(Body, Ranges), + case PartList of + [] -> %% no valid ranges + HResponse1 = mochiweb_headers:enter("Content-Type", + ContentType, + HResponse), + %% could be 416, for now we'll just return 200 + respond({200, HResponse1, Body}, THIS); + PartList -> + {RangeHeaders, RangeBody} = + mochiweb_multipart:parts_to_body(PartList, ContentType, Size), + HResponse1 = mochiweb_headers:enter_from_list( + [{"Accept-Ranges", "bytes"} | + RangeHeaders], + HResponse), + respond({206, HResponse1, RangeBody}, THIS) + end + end. + +%% @spec should_close(request()) -> bool() +%% @doc Return true if the connection must be closed. If false, using +%% Keep-Alive should be safe. +should_close({?MODULE, [_Socket, _Method, _RawPath, Version, _Headers]}=THIS) -> + ForceClose = erlang:get(?SAVE_FORCE_CLOSE) =/= undefined, + DidNotRecv = erlang:get(?SAVE_RECV) =:= undefined, + ForceClose orelse Version < {1, 0} + %% Connection: close + orelse is_close(get_header_value("connection", THIS)) + %% HTTP 1.0 requires Connection: Keep-Alive + orelse (Version =:= {1, 0} + andalso get_header_value("connection", THIS) =/= "Keep-Alive") + %% unread data left on the socket, can't safely continue + orelse (DidNotRecv + andalso get_combined_header_value("content-length", THIS) =/= undefined + andalso list_to_integer(get_combined_header_value("content-length", THIS)) > 0) + orelse (DidNotRecv + andalso get_header_value("transfer-encoding", THIS) =:= "chunked"). + +is_close("close") -> + true; +is_close(S=[_C, _L, _O, _S, _E]) -> + string:to_lower(S) =:= "close"; +is_close(_) -> + false. + +%% @spec cleanup(request()) -> ok +%% @doc Clean up any junk in the process dictionary, required before continuing +%% a Keep-Alive request. +cleanup({?MODULE, [_Socket, _Method, _RawPath, _Version, _Headers]}) -> + L = [?SAVE_QS, ?SAVE_PATH, ?SAVE_RECV, ?SAVE_BODY, ?SAVE_BODY_LENGTH, + ?SAVE_POST, ?SAVE_COOKIE, ?SAVE_FORCE_CLOSE], + lists:foreach(fun(K) -> + erase(K) + end, L), + ok. + +%% @spec parse_qs(request()) -> [{Key::string(), Value::string()}] +%% @doc Parse the query string of the URL. +parse_qs({?MODULE, [_Socket, _Method, RawPath, _Version, _Headers]}) -> + case erlang:get(?SAVE_QS) of + undefined -> + {_, QueryString, _} = mochiweb_util:urlsplit_path(RawPath), + Parsed = mochiweb_util:parse_qs(QueryString), + put(?SAVE_QS, Parsed), + Parsed; + Cached -> + Cached + end. + +%% @spec get_cookie_value(Key::string, request()) -> string() | undefined +%% @doc Get the value of the given cookie. +get_cookie_value(Key, {?MODULE, [_Socket, _Method, _RawPath, _Version, _Headers]}=THIS) -> + proplists:get_value(Key, parse_cookie(THIS)). + +%% @spec parse_cookie(request()) -> [{Key::string(), Value::string()}] +%% @doc Parse the cookie header. +parse_cookie({?MODULE, [_Socket, _Method, _RawPath, _Version, _Headers]}=THIS) -> + case erlang:get(?SAVE_COOKIE) of + undefined -> + Cookies = case get_header_value("cookie", THIS) of + undefined -> + []; + Value -> + mochiweb_cookies:parse_cookie(Value) + end, + put(?SAVE_COOKIE, Cookies), + Cookies; + Cached -> + Cached + end. + +%% @spec parse_post(request()) -> [{Key::string(), Value::string()}] +%% @doc Parse an application/x-www-form-urlencoded form POST. This +%% has the side-effect of calling recv_body(). +parse_post({?MODULE, [_Socket, _Method, _RawPath, _Version, _Headers]}=THIS) -> + case erlang:get(?SAVE_POST) of + undefined -> + Parsed = case recv_body(THIS) of + undefined -> + []; + Binary -> + case get_primary_header_value("content-type",THIS) of + "application/x-www-form-urlencoded" ++ _ -> + mochiweb_util:parse_qs(Binary); + _ -> + [] + end + end, + put(?SAVE_POST, Parsed), + Parsed; + Cached -> + Cached + end. + +%% @spec stream_chunked_body(integer(), fun(), term(), request()) -> term() +%% @doc The function is called for each chunk. +%% Used internally by read_chunked_body. +stream_chunked_body(MaxChunkSize, Fun, FunState, + {?MODULE, [_Socket, _Method, _RawPath, _Version, _Headers]}=THIS) -> + case read_chunk_length(THIS) of + 0 -> + Fun({0, read_chunk(0, THIS)}, FunState); + Length when Length > MaxChunkSize -> + NewState = read_sub_chunks(Length, MaxChunkSize, Fun, FunState, THIS), + stream_chunked_body(MaxChunkSize, Fun, NewState, THIS); + Length -> + NewState = Fun({Length, read_chunk(Length, THIS)}, FunState), + stream_chunked_body(MaxChunkSize, Fun, NewState, THIS) + end. + +stream_unchunked_body(0, Fun, FunState, {?MODULE, [_Socket, _Method, _RawPath, _Version, _Headers]}) -> + Fun({0, <<>>}, FunState); +stream_unchunked_body(Length, Fun, FunState, + {?MODULE, [_Socket, _Method, _RawPath, _Version, _Headers]}=THIS) when Length > 0 -> + PktSize = case Length > ?RECBUF_SIZE of + true -> + ?RECBUF_SIZE; + false -> + Length + end, + Bin = recv(PktSize, THIS), + NewState = Fun({PktSize, Bin}, FunState), + stream_unchunked_body(Length - PktSize, Fun, NewState, THIS). + +%% @spec read_chunk_length(request()) -> integer() +%% @doc Read the length of the next HTTP chunk. +read_chunk_length({?MODULE, [Socket, _Method, _RawPath, _Version, _Headers]}) -> + ok = mochiweb_socket:setopts(Socket, [{packet, line}]), + case mochiweb_socket:recv(Socket, 0, ?IDLE_TIMEOUT) of + {ok, Header} -> + ok = mochiweb_socket:setopts(Socket, [{packet, raw}]), + Splitter = fun (C) -> + C =/= $\r andalso C =/= $\n andalso C =/= $ + end, + {Hex, _Rest} = lists:splitwith(Splitter, binary_to_list(Header)), + mochihex:to_int(Hex); + _ -> + exit(normal) + end. + +%% @spec read_chunk(integer(), request()) -> Chunk::binary() | [Footer::binary()] +%% @doc Read in a HTTP chunk of the given length. If Length is 0, then read the +%% HTTP footers (as a list of binaries, since they're nominal). +read_chunk(0, {?MODULE, [Socket, _Method, _RawPath, _Version, _Headers]}) -> + ok = mochiweb_socket:setopts(Socket, [{packet, line}]), + F = fun (F1, Acc) -> + case mochiweb_socket:recv(Socket, 0, ?IDLE_TIMEOUT) of + {ok, <<"\r\n">>} -> + Acc; + {ok, Footer} -> + F1(F1, [Footer | Acc]); + _ -> + exit(normal) + end + end, + Footers = F(F, []), + ok = mochiweb_socket:setopts(Socket, [{packet, raw}]), + put(?SAVE_RECV, true), + Footers; +read_chunk(Length, {?MODULE, [Socket, _Method, _RawPath, _Version, _Headers]}) -> + case mochiweb_socket:recv(Socket, 2 + Length, ?IDLE_TIMEOUT) of + {ok, <>} -> + Chunk; + _ -> + exit(normal) + end. + +read_sub_chunks(Length, MaxChunkSize, Fun, FunState, + {?MODULE, [_Socket, _Method, _RawPath, _Version, _Headers]}=THIS) when Length > MaxChunkSize -> + Bin = recv(MaxChunkSize, THIS), + NewState = Fun({size(Bin), Bin}, FunState), + read_sub_chunks(Length - MaxChunkSize, MaxChunkSize, Fun, NewState, THIS); + +read_sub_chunks(Length, _MaxChunkSize, Fun, FunState, + {?MODULE, [_Socket, _Method, _RawPath, _Version, _Headers]}=THIS) -> + Fun({Length, read_chunk(Length, THIS)}, FunState). + +%% @spec serve_file(Path, DocRoot, request()) -> Response +%% @doc Serve a file relative to DocRoot. +serve_file(Path, DocRoot, {?MODULE, [_Socket, _Method, _RawPath, _Version, _Headers]}=THIS) -> + serve_file(Path, DocRoot, [], THIS). + +%% @spec serve_file(Path, DocRoot, ExtraHeaders, request()) -> Response +%% @doc Serve a file relative to DocRoot. +serve_file(Path, DocRoot, ExtraHeaders, {?MODULE, [_Socket, _Method, _RawPath, _Version, _Headers]}=THIS) -> + case mochiweb_util:safe_relative_path(Path) of + undefined -> + not_found(ExtraHeaders, THIS); + RelPath -> + FullPath = filename:join([DocRoot, RelPath]), + case filelib:is_dir(FullPath) of + true -> + maybe_redirect(RelPath, FullPath, ExtraHeaders, THIS); + false -> + maybe_serve_file(FullPath, ExtraHeaders, THIS) + end + end. + +%% Internal API + +%% This has the same effect as the DirectoryIndex directive in httpd +directory_index(FullPath) -> + filename:join([FullPath, "index.html"]). + +maybe_redirect([], FullPath, ExtraHeaders, {?MODULE, [_Socket, _Method, _RawPath, _Version, _Headers]}=THIS) -> + maybe_serve_file(directory_index(FullPath), ExtraHeaders, THIS); + +maybe_redirect(RelPath, FullPath, ExtraHeaders, + {?MODULE, [_Socket, _Method, _RawPath, _Version, Headers]}=THIS) -> + case string:right(RelPath, 1) of + "/" -> + maybe_serve_file(directory_index(FullPath), ExtraHeaders, THIS); + _ -> + Host = mochiweb_headers:get_value("host", Headers), + Location = "http://" ++ Host ++ "/" ++ RelPath ++ "/", + LocationBin = list_to_binary(Location), + MoreHeaders = [{"Location", Location}, + {"Content-Type", "text/html"} | ExtraHeaders], + Top = <<"" + "" + "301 Moved Permanently" + "" + "

Moved Permanently

" + "

The document has moved >, + Bottom = <<">here.

\n">>, + Body = <>, + respond({301, MoreHeaders, Body}, THIS) + end. + +maybe_serve_file(File, ExtraHeaders, {?MODULE, [_Socket, _Method, _RawPath, _Version, _Headers]}=THIS) -> + case file:read_file_info(File) of + {ok, FileInfo} -> + LastModified = httpd_util:rfc1123_date(FileInfo#file_info.mtime), + case get_header_value("if-modified-since", THIS) of + LastModified -> + respond({304, ExtraHeaders, ""}, THIS); + _ -> + case file:open(File, [raw, binary]) of + {ok, IoDevice} -> + ContentType = mochiweb_util:guess_mime(File), + Res = ok({ContentType, + [{"last-modified", LastModified} + | ExtraHeaders], + {file, IoDevice}}, THIS), + ok = file:close(IoDevice), + Res; + _ -> + not_found(ExtraHeaders, THIS) + end + end; + {error, _} -> + not_found(ExtraHeaders, THIS) + end. + +server_headers() -> + [{"Server", "MochiWeb/1.0 (" ++ ?QUIP ++ ")"}, + {"Date", httpd_util:rfc1123_date()}]. + +make_code(X) when is_integer(X) -> + [integer_to_list(X), [" " | httpd_util:reason_phrase(X)]]; +make_code(Io) when is_list(Io); is_binary(Io) -> + Io. + +make_version({1, 0}) -> + <<"HTTP/1.0 ">>; +make_version(_) -> + <<"HTTP/1.1 ">>. + +range_parts({file, IoDevice}, Ranges) -> + Size = mochiweb_io:iodevice_size(IoDevice), + F = fun (Spec, Acc) -> + case mochiweb_http:range_skip_length(Spec, Size) of + invalid_range -> + Acc; + V -> + [V | Acc] + end + end, + LocNums = lists:foldr(F, [], Ranges), + {ok, Data} = file:pread(IoDevice, LocNums), + Bodies = lists:zipwith(fun ({Skip, Length}, PartialBody) -> + {Skip, Skip + Length - 1, PartialBody} + end, + LocNums, Data), + {Bodies, Size}; +range_parts(Body0, Ranges) -> + Body = iolist_to_binary(Body0), + Size = size(Body), + F = fun(Spec, Acc) -> + case mochiweb_http:range_skip_length(Spec, Size) of + invalid_range -> + Acc; + {Skip, Length} -> + <<_:Skip/binary, PartialBody:Length/binary, _/binary>> = Body, + [{Skip, Skip + Length - 1, PartialBody} | Acc] + end + end, + {lists:foldr(F, [], Ranges), Size}. + +%% @spec accepted_encodings([encoding()], request()) -> [encoding()] | bad_accept_encoding_value +%% @type encoding() = string(). +%% +%% @doc Returns a list of encodings accepted by a request. Encodings that are +%% not supported by the server will not be included in the return list. +%% This list is computed from the "Accept-Encoding" header and +%% its elements are ordered, descendingly, according to their Q values. +%% +%% Section 14.3 of the RFC 2616 (HTTP 1.1) describes the "Accept-Encoding" +%% header and the process of determining which server supported encodings +%% can be used for encoding the body for the request's response. +%% +%% Examples +%% +%% 1) For a missing "Accept-Encoding" header: +%% accepted_encodings(["gzip", "identity"]) -> ["identity"] +%% +%% 2) For an "Accept-Encoding" header with value "gzip, deflate": +%% accepted_encodings(["gzip", "identity"]) -> ["gzip", "identity"] +%% +%% 3) For an "Accept-Encoding" header with value "gzip;q=0.5, deflate": +%% accepted_encodings(["gzip", "deflate", "identity"]) -> +%% ["deflate", "gzip", "identity"] +%% +accepted_encodings(SupportedEncodings, {?MODULE, [_Socket, _Method, _RawPath, _Version, _Headers]}=THIS) -> + AcceptEncodingHeader = case get_header_value("Accept-Encoding", THIS) of + undefined -> + ""; + Value -> + Value + end, + case mochiweb_util:parse_qvalues(AcceptEncodingHeader) of + invalid_qvalue_string -> + bad_accept_encoding_value; + QList -> + mochiweb_util:pick_accepted_encodings( + QList, SupportedEncodings, "identity" + ) + end. + +%% @spec accepts_content_type(string() | binary(), request()) -> boolean() | bad_accept_header +%% +%% @doc Determines whether a request accepts a given media type by analyzing its +%% "Accept" header. +%% +%% Examples +%% +%% 1) For a missing "Accept" header: +%% accepts_content_type("application/json") -> true +%% +%% 2) For an "Accept" header with value "text/plain, application/*": +%% accepts_content_type("application/json") -> true +%% +%% 3) For an "Accept" header with value "text/plain, */*; q=0.0": +%% accepts_content_type("application/json") -> false +%% +%% 4) For an "Accept" header with value "text/plain; q=0.5, */*; q=0.1": +%% accepts_content_type("application/json") -> true +%% +%% 5) For an "Accept" header with value "text/*; q=0.0, */*": +%% accepts_content_type("text/plain") -> false +%% +accepts_content_type(ContentType1, {?MODULE, [_Socket, _Method, _RawPath, _Version, _Headers]}=THIS) -> + ContentType = re:replace(ContentType1, "\\s", "", [global, {return, list}]), + AcceptHeader = accept_header(THIS), + case mochiweb_util:parse_qvalues(AcceptHeader) of + invalid_qvalue_string -> + bad_accept_header; + QList -> + [MainType, _SubType] = string:tokens(ContentType, "/"), + SuperType = MainType ++ "/*", + lists:any( + fun({"*/*", Q}) when Q > 0.0 -> + true; + ({Type, Q}) when Q > 0.0 -> + Type =:= ContentType orelse Type =:= SuperType; + (_) -> + false + end, + QList + ) andalso + (not lists:member({ContentType, 0.0}, QList)) andalso + (not lists:member({SuperType, 0.0}, QList)) + end. + +%% @spec accepted_content_types([string() | binary()], request()) -> [string()] | bad_accept_header +%% +%% @doc Filters which of the given media types this request accepts. This filtering +%% is performed by analyzing the "Accept" header. The returned list is sorted +%% according to the preferences specified in the "Accept" header (higher Q values +%% first). If two or more types have the same preference (Q value), they're order +%% in the returned list is the same as they're order in the input list. +%% +%% Examples +%% +%% 1) For a missing "Accept" header: +%% accepted_content_types(["text/html", "application/json"]) -> +%% ["text/html", "application/json"] +%% +%% 2) For an "Accept" header with value "text/html, application/*": +%% accepted_content_types(["application/json", "text/html"]) -> +%% ["application/json", "text/html"] +%% +%% 3) For an "Accept" header with value "text/html, */*; q=0.0": +%% accepted_content_types(["text/html", "application/json"]) -> +%% ["text/html"] +%% +%% 4) For an "Accept" header with value "text/html; q=0.5, */*; q=0.1": +%% accepts_content_types(["application/json", "text/html"]) -> +%% ["text/html", "application/json"] +%% +accepted_content_types(Types1, {?MODULE, [_Socket, _Method, _RawPath, _Version, _Headers]}=THIS) -> + Types = lists:map( + fun(T) -> re:replace(T, "\\s", "", [global, {return, list}]) end, + Types1), + AcceptHeader = accept_header(THIS), + case mochiweb_util:parse_qvalues(AcceptHeader) of + invalid_qvalue_string -> + bad_accept_header; + QList -> + TypesQ = lists:foldr( + fun(T, Acc) -> + case proplists:get_value(T, QList) of + undefined -> + [MainType, _SubType] = string:tokens(T, "/"), + case proplists:get_value(MainType ++ "/*", QList) of + undefined -> + case proplists:get_value("*/*", QList) of + Q when is_float(Q), Q > 0.0 -> + [{Q, T} | Acc]; + _ -> + Acc + end; + Q when Q > 0.0 -> + [{Q, T} | Acc]; + _ -> + Acc + end; + Q when Q > 0.0 -> + [{Q, T} | Acc]; + _ -> + Acc + end + end, + [], Types), + % Note: Stable sort. If 2 types have the same Q value we leave them in the + % same order as in the input list. + SortFun = fun({Q1, _}, {Q2, _}) -> Q1 >= Q2 end, + [Type || {_Q, Type} <- lists:sort(SortFun, TypesQ)] + end. + +accept_header({?MODULE, [_Socket, _Method, _RawPath, _Version, _Headers]}=THIS) -> + case get_header_value("Accept", THIS) of + undefined -> + "*/*"; + Value -> + Value + end. + +%% +%% Tests +%% +-ifdef(TEST). +-include_lib("eunit/include/eunit.hrl"). +-endif. diff --git a/rabbitmq-server/plugins-src/mochiweb-wrapper/mochiweb-git/src/mochiweb_request_tests.erl b/rabbitmq-server/plugins-src/mochiweb-wrapper/mochiweb-git/src/mochiweb_request_tests.erl new file mode 100644 index 0000000..b40c867 --- /dev/null +++ b/rabbitmq-server/plugins-src/mochiweb-wrapper/mochiweb-git/src/mochiweb_request_tests.erl @@ -0,0 +1,182 @@ +-module(mochiweb_request_tests). + +-ifdef(TEST). +-include_lib("eunit/include/eunit.hrl"). + +accepts_content_type_test() -> + Req1 = mochiweb_request:new(nil, 'GET', "/foo", {1, 1}, + mochiweb_headers:make([{"Accept", "multipart/related"}])), + ?assertEqual(true, Req1:accepts_content_type("multipart/related")), + ?assertEqual(true, Req1:accepts_content_type(<<"multipart/related">>)), + + Req2 = mochiweb_request:new(nil, 'GET', "/foo", {1, 1}, + mochiweb_headers:make([{"Accept", "text/html"}])), + ?assertEqual(false, Req2:accepts_content_type("multipart/related")), + + Req3 = mochiweb_request:new(nil, 'GET', "/foo", {1, 1}, + mochiweb_headers:make([{"Accept", "text/html, multipart/*"}])), + ?assertEqual(true, Req3:accepts_content_type("multipart/related")), + + Req4 = mochiweb_request:new(nil, 'GET', "/foo", {1, 1}, + mochiweb_headers:make([{"Accept", "text/html, multipart/*; q=0.0"}])), + ?assertEqual(false, Req4:accepts_content_type("multipart/related")), + + Req5 = mochiweb_request:new(nil, 'GET', "/foo", {1, 1}, + mochiweb_headers:make([{"Accept", "text/html, multipart/*; q=0"}])), + ?assertEqual(false, Req5:accepts_content_type("multipart/related")), + + Req6 = mochiweb_request:new(nil, 'GET', "/foo", {1, 1}, + mochiweb_headers:make([{"Accept", "text/html, */*; q=0.0"}])), + ?assertEqual(false, Req6:accepts_content_type("multipart/related")), + + Req7 = mochiweb_request:new(nil, 'GET', "/foo", {1, 1}, + mochiweb_headers:make([{"Accept", "multipart/*; q=0.0, */*"}])), + ?assertEqual(false, Req7:accepts_content_type("multipart/related")), + + Req8 = mochiweb_request:new(nil, 'GET', "/foo", {1, 1}, + mochiweb_headers:make([{"Accept", "*/*; q=0.0, multipart/*"}])), + ?assertEqual(true, Req8:accepts_content_type("multipart/related")), + + Req9 = mochiweb_request:new(nil, 'GET', "/foo", {1, 1}, + mochiweb_headers:make([{"Accept", "*/*; q=0.0, multipart/related"}])), + ?assertEqual(true, Req9:accepts_content_type("multipart/related")), + + Req10 = mochiweb_request:new(nil, 'GET', "/foo", {1, 1}, + mochiweb_headers:make([{"Accept", "text/html; level=1"}])), + ?assertEqual(true, Req10:accepts_content_type("text/html;level=1")), + + Req11 = mochiweb_request:new(nil, 'GET', "/foo", {1, 1}, + mochiweb_headers:make([{"Accept", "text/html; level=1, text/html"}])), + ?assertEqual(true, Req11:accepts_content_type("text/html")), + + Req12 = mochiweb_request:new(nil, 'GET', "/foo", {1, 1}, + mochiweb_headers:make([{"Accept", "text/html; level=1; q=0.0, text/html"}])), + ?assertEqual(false, Req12:accepts_content_type("text/html;level=1")), + + Req13 = mochiweb_request:new(nil, 'GET', "/foo", {1, 1}, + mochiweb_headers:make([{"Accept", "text/html; level=1; q=0.0, text/html"}])), + ?assertEqual(false, Req13:accepts_content_type("text/html; level=1")), + + Req14 = mochiweb_request:new(nil, 'GET', "/foo", {1, 1}, + mochiweb_headers:make([{"Accept", "text/html;level=1;q=0.1, text/html"}])), + ?assertEqual(true, Req14:accepts_content_type("text/html; level=1")). + +accepted_encodings_test() -> + Req1 = mochiweb_request:new(nil, 'GET', "/foo", {1, 1}, + mochiweb_headers:make([])), + ?assertEqual(["identity"], + Req1:accepted_encodings(["gzip", "identity"])), + + Req2 = mochiweb_request:new(nil, 'GET', "/foo", {1, 1}, + mochiweb_headers:make([{"Accept-Encoding", "gzip, deflate"}])), + ?assertEqual(["gzip", "identity"], + Req2:accepted_encodings(["gzip", "identity"])), + + Req3 = mochiweb_request:new(nil, 'GET', "/foo", {1, 1}, + mochiweb_headers:make([{"Accept-Encoding", "gzip;q=0.5, deflate"}])), + ?assertEqual(["deflate", "gzip", "identity"], + Req3:accepted_encodings(["gzip", "deflate", "identity"])), + + Req4 = mochiweb_request:new(nil, 'GET', "/foo", {1, 1}, + mochiweb_headers:make([{"Accept-Encoding", "identity, *;q=0"}])), + ?assertEqual(["identity"], + Req4:accepted_encodings(["gzip", "deflate", "identity"])), + + Req5 = mochiweb_request:new(nil, 'GET', "/foo", {1, 1}, + mochiweb_headers:make([{"Accept-Encoding", "gzip; q=0.1, *;q=0"}])), + ?assertEqual(["gzip"], + Req5:accepted_encodings(["gzip", "deflate", "identity"])), + + Req6 = mochiweb_request:new(nil, 'GET', "/foo", {1, 1}, + mochiweb_headers:make([{"Accept-Encoding", "gzip; q=, *;q=0"}])), + ?assertEqual(bad_accept_encoding_value, + Req6:accepted_encodings(["gzip", "deflate", "identity"])), + + Req7 = mochiweb_request:new(nil, 'GET', "/foo", {1, 1}, + mochiweb_headers:make([{"Accept-Encoding", "gzip;q=2.0, *;q=0"}])), + ?assertEqual(bad_accept_encoding_value, + Req7:accepted_encodings(["gzip", "identity"])), + + Req8 = mochiweb_request:new(nil, 'GET', "/foo", {1, 1}, + mochiweb_headers:make([{"Accept-Encoding", "deflate, *;q=0.0"}])), + ?assertEqual([], + Req8:accepted_encodings(["gzip", "identity"])). + +accepted_content_types_test() -> + Req1 = mochiweb_request:new(nil, 'GET', "/foo", {1, 1}, + mochiweb_headers:make([{"Accept", "text/html"}])), + ?assertEqual(["text/html"], + Req1:accepted_content_types(["text/html", "application/json"])), + + Req2 = mochiweb_request:new(nil, 'GET', "/foo", {1, 1}, + mochiweb_headers:make([{"Accept", "text/html, */*;q=0"}])), + ?assertEqual(["text/html"], + Req2:accepted_content_types(["text/html", "application/json"])), + + Req3 = mochiweb_request:new(nil, 'GET', "/foo", {1, 1}, + mochiweb_headers:make([{"Accept", "text/*, */*;q=0"}])), + ?assertEqual(["text/html"], + Req3:accepted_content_types(["text/html", "application/json"])), + + Req4 = mochiweb_request:new(nil, 'GET', "/foo", {1, 1}, + mochiweb_headers:make([{"Accept", "text/*;q=0.8, */*;q=0.5"}])), + ?assertEqual(["text/html", "application/json"], + Req4:accepted_content_types(["application/json", "text/html"])), + + Req5 = mochiweb_request:new(nil, 'GET', "/foo", {1, 1}, + mochiweb_headers:make([{"Accept", "text/*;q=0.8, */*;q=0.5"}])), + ?assertEqual(["text/html", "application/json"], + Req5:accepted_content_types(["text/html", "application/json"])), + + Req6 = mochiweb_request:new(nil, 'GET', "/foo", {1, 1}, + mochiweb_headers:make([{"Accept", "text/*;q=0.5, */*;q=0.5"}])), + ?assertEqual(["application/json", "text/html"], + Req6:accepted_content_types(["application/json", "text/html"])), + + Req7 = mochiweb_request:new(nil, 'GET', "/foo", {1, 1}, + mochiweb_headers:make( + [{"Accept", "text/html;q=0.5, application/json;q=0.5"}])), + ?assertEqual(["application/json", "text/html"], + Req7:accepted_content_types(["application/json", "text/html"])), + + Req8 = mochiweb_request:new(nil, 'GET', "/foo", {1, 1}, + mochiweb_headers:make([{"Accept", "text/html"}])), + ?assertEqual([], + Req8:accepted_content_types(["application/json"])), + + Req9 = mochiweb_request:new(nil, 'GET', "/foo", {1, 1}, + mochiweb_headers:make([{"Accept", "text/*;q=0.9, text/html;q=0.5, */*;q=0.7"}])), + ?assertEqual(["application/json", "text/html"], + Req9:accepted_content_types(["text/html", "application/json"])). + +should_close_test() -> + F = fun (V, H) -> + (mochiweb_request:new( + nil, 'GET', "/", V, + mochiweb_headers:make(H) + )):should_close() + end, + ?assertEqual( + true, + F({1, 1}, [{"Connection", "close"}])), + ?assertEqual( + true, + F({1, 0}, [{"Connection", "close"}])), + ?assertEqual( + true, + F({1, 1}, [{"Connection", "ClOSe"}])), + ?assertEqual( + false, + F({1, 1}, [{"Connection", "closer"}])), + ?assertEqual( + false, + F({1, 1}, [])), + ?assertEqual( + true, + F({1, 0}, [])), + ?assertEqual( + false, + F({1, 0}, [{"Connection", "Keep-Alive"}])), + ok. + +-endif. diff --git a/rabbitmq-server/plugins-src/mochiweb-wrapper/mochiweb-git/src/mochiweb_response.erl b/rabbitmq-server/plugins-src/mochiweb-wrapper/mochiweb-git/src/mochiweb_response.erl new file mode 100644 index 0000000..308a26b --- /dev/null +++ b/rabbitmq-server/plugins-src/mochiweb-wrapper/mochiweb-git/src/mochiweb_response.erl @@ -0,0 +1,72 @@ +%% @author Bob Ippolito +%% @copyright 2007 Mochi Media, Inc. + +%% @doc Response abstraction. + +-module(mochiweb_response). +-author('bob@mochimedia.com'). + +-define(QUIP, "Any of you quaids got a smint?"). + +-export([new/3, get_header_value/2, get/2, dump/1]). +-export([send/2, write_chunk/2]). + +%% @type response(). A mochiweb_response parameterized module instance. + +%% @spec new(Request, Code, Headers) -> response() +%% @doc Create a new mochiweb_response instance. +new(Request, Code, Headers) -> + {?MODULE, [Request, Code, Headers]}. + +%% @spec get_header_value(string() | atom() | binary(), response()) -> +%% string() | undefined +%% @doc Get the value of the given response header. +get_header_value(K, {?MODULE, [_Request, _Code, Headers]}) -> + mochiweb_headers:get_value(K, Headers). + +%% @spec get(request | code | headers, response()) -> term() +%% @doc Return the internal representation of the given field. +get(request, {?MODULE, [Request, _Code, _Headers]}) -> + Request; +get(code, {?MODULE, [_Request, Code, _Headers]}) -> + Code; +get(headers, {?MODULE, [_Request, _Code, Headers]}) -> + Headers. + +%% @spec dump(response()) -> {mochiweb_request, [{atom(), term()}]} +%% @doc Dump the internal representation to a "human readable" set of terms +%% for debugging/inspection purposes. +dump({?MODULE, [Request, Code, Headers]}) -> + [{request, Request:dump()}, + {code, Code}, + {headers, mochiweb_headers:to_list(Headers)}]. + +%% @spec send(iodata(), response()) -> ok +%% @doc Send data over the socket if the method is not HEAD. +send(Data, {?MODULE, [Request, _Code, _Headers]}) -> + case Request:get(method) of + 'HEAD' -> + ok; + _ -> + Request:send(Data) + end. + +%% @spec write_chunk(iodata(), response()) -> ok +%% @doc Write a chunk of a HTTP chunked response. If Data is zero length, +%% then the chunked response will be finished. +write_chunk(Data, {?MODULE, [Request, _Code, _Headers]}=THIS) -> + case Request:get(version) of + Version when Version >= {1, 1} -> + Length = iolist_size(Data), + send([io_lib:format("~.16b\r\n", [Length]), Data, <<"\r\n">>], THIS); + _ -> + send(Data, THIS) + end. + + +%% +%% Tests +%% +-ifdef(TEST). +-include_lib("eunit/include/eunit.hrl"). +-endif. diff --git a/rabbitmq-server/plugins-src/mochiweb-wrapper/mochiweb-git/src/mochiweb_session.erl b/rabbitmq-server/plugins-src/mochiweb-wrapper/mochiweb-git/src/mochiweb_session.erl new file mode 100644 index 0000000..ddf7c46 --- /dev/null +++ b/rabbitmq-server/plugins-src/mochiweb-wrapper/mochiweb-git/src/mochiweb_session.erl @@ -0,0 +1,189 @@ +%% @author Asier Azkuenaga Batiz + +%% @doc HTTP Cookie session. Note that the expiration time travels unencrypted +%% as far as this module is concerned. In order to achieve more security, +%% it is advised to use https. +%% Based on the paper +%% +%% "A Secure Cookie Protocol". +%% This module is only supported on R15B02 and later, the AES CFB mode is not +%% available in earlier releases of crypto. +-module(mochiweb_session). +-export([generate_session_data/4, generate_session_cookie/4, + check_session_cookie/4]). + +-export_types([expiration_time/0]). +-type expiration_time() :: integer(). +-type key_fun() :: fun((string()) -> iolist()). + +%% TODO: Import this from elsewhere after attribute types refactor. +-type header() :: {string(), string()}. + +%% @doc Generates a secure encrypted binary convining all the parameters. The +%% expiration time must be a 32-bit integer. +%% -spec generate_session_data( +%% ExpirationTime :: expiration_time(), +%% Data :: iolist(), +%% FSessionKey :: key_fun(), +%% ServerKey :: iolist()) -> binary(). +generate_session_data(ExpirationTime, Data, FSessionKey, ServerKey) + when is_integer(ExpirationTime), is_function(FSessionKey)-> + BData = ensure_binary(Data), + ExpTime = integer_to_list(ExpirationTime), + Key = gen_key(ExpTime, ServerKey), + Hmac = gen_hmac(ExpTime, BData, FSessionKey(ExpTime), Key), + EData = encrypt_data(BData, Key), + mochiweb_base64url:encode( + <>). + +%% @doc Convenience wrapper for generate_session_data that returns a +%% mochiweb cookie with "id" as the key, a max_age of 20000 seconds, +%% and the current local time as local time. +%% -spec generate_session_cookie( +%% ExpirationTime :: expiration_time(), +%% Data :: iolist(), +%% FSessionKey :: key_fun(), +%% ServerKey :: iolist()) -> header(). +generate_session_cookie(ExpirationTime, Data, FSessionKey, ServerKey) + when is_integer(ExpirationTime), is_function(FSessionKey)-> + CookieData = generate_session_data(ExpirationTime, Data, + FSessionKey, ServerKey), + mochiweb_cookies:cookie("id", CookieData, + [{max_age, 20000}, + {local_time, + calendar:universal_time_to_local_time( + calendar:universal_time())}]). + +%% TODO: This return type is messy to express in the type system. +%% -spec check_session_cookie( + %% ECookie :: binary(), + %% ExpirationTime :: string(), + %% FSessionKey :: key_fun(), + %% ServerKey :: iolist()) -> + %% {Success :: boolean(), + %% ExpTimeAndData :: [integer() | binary()]}. +check_session_cookie(ECookie, ExpirationTime, FSessionKey, ServerKey) + when is_binary(ECookie), is_integer(ExpirationTime), + is_function(FSessionKey) -> + case mochiweb_base64url:decode(ECookie) of + <> -> + ETString = integer_to_list(ExpirationTime1), + Key = gen_key(ETString, ServerKey), + Data = decrypt_data(EData, Key), + Hmac2 = gen_hmac(ETString, + Data, + FSessionKey(ETString), + Key), + {ExpirationTime1 >= ExpirationTime andalso eq(Hmac2, BHmac), + [ExpirationTime1, binary_to_list(Data)]}; + _ -> + {false, []} + end; +check_session_cookie(_ECookie, _ExpirationTime, _FSessionKey, _ServerKey) -> + {false, []}. + +%% 'Constant' time =:= operator for binary, to mitigate timing attacks. +%% -spec eq(binary(), binary()) -> boolean(). +eq(A, B) when is_binary(A) andalso is_binary(B) -> + eq(A, B, 0). + +eq(<>, <>, Acc) -> + eq(As, Bs, Acc bor (A bxor B)); +eq(<<>>, <<>>, 0) -> + true; +eq(_As, _Bs, _Acc) -> + false. + +%% -spec ensure_binary(iolist()) -> binary(). +ensure_binary(B) when is_binary(B) -> + B; +ensure_binary(L) when is_list(L) -> + iolist_to_binary(L). + +%% -spec encrypt_data(binary(), binary()) -> binary(). +encrypt_data(Data, Key) -> + IV = crypto:rand_bytes(16), + Crypt = crypto:aes_cfb_128_encrypt(Key, IV, Data), + <>. + +%% -spec decrypt_data(binary(), binary()) -> binary(). +decrypt_data(<>, Key) -> + crypto:aes_cfb_128_decrypt(Key, IV, Crypt). + +%% -spec gen_key(iolist(), iolist()) -> binary(). +gen_key(ExpirationTime, ServerKey)-> + crypto:md5_mac(ServerKey, [ExpirationTime]). + +%% -spec gen_hmac(iolist(), binary(), iolist(), binary()) -> binary(). +gen_hmac(ExpirationTime, Data, SessionKey, Key) -> + crypto:sha_mac(Key, [ExpirationTime, Data, SessionKey]). + + +-ifdef(TEST). +-include_lib("eunit/include/eunit.hrl"). + +generate_check_session_cookie_test_() -> + {setup, + fun setup_server_key/0, + fun generate_check_session_cookie/1}. + +setup_server_key() -> + crypto:start(), + ["adfasdfasfs",30000]. + +generate_check_session_cookie([ServerKey, TS]) -> + Id = fun (A) -> A end, + TSFuture = TS + 1000, + TSPast = TS - 1, + [?_assertEqual( + {true, [TSFuture, "alice"]}, + check_session_cookie( + generate_session_data(TSFuture, "alice", Id, ServerKey), + TS, Id, ServerKey)), + ?_assertEqual( + {true, [TSFuture, "alice and"]}, + check_session_cookie( + generate_session_data(TSFuture, "alice and", Id, ServerKey), + TS, Id, ServerKey)), + ?_assertEqual( + {true, [TSFuture, "alice and"]}, + check_session_cookie( + generate_session_data(TSFuture, "alice and", Id, ServerKey), + TS, Id,ServerKey)), + ?_assertEqual( + {true, [TSFuture, "alice and bob"]}, + check_session_cookie( + generate_session_data(TSFuture, "alice and bob", + Id, ServerKey), + TS, Id, ServerKey)), + ?_assertEqual( + {true, [TSFuture, "alice jlkjfkjsdfg sdkfjgldsjgl"]}, + check_session_cookie( + generate_session_data(TSFuture, "alice jlkjfkjsdfg sdkfjgldsjgl", + Id, ServerKey), + TS, Id, ServerKey)), + ?_assertEqual( + {true, [TSFuture, "alice .'¡'ç+-$%/(&\""]}, + check_session_cookie( + generate_session_data(TSFuture, "alice .'¡'ç+-$%/(&\"" + ,Id, ServerKey), + TS, Id, ServerKey)), + ?_assertEqual( + {true,[TSFuture,"alice456689875"]}, + check_session_cookie( + generate_session_data(TSFuture, ["alice","456689875"], + Id, ServerKey), + TS, Id, ServerKey)), + ?_assertError( + function_clause, + check_session_cookie( + generate_session_data(TSFuture, {tuple,one}, + Id, ServerKey), + TS, Id,ServerKey)), + ?_assertEqual( + {false, [TSPast, "bob"]}, + check_session_cookie( + generate_session_data(TSPast, "bob", Id,ServerKey), + TS, Id, ServerKey)) + ]. +-endif. diff --git a/rabbitmq-server/plugins-src/mochiweb-wrapper/mochiweb-git/src/mochiweb_socket.erl b/rabbitmq-server/plugins-src/mochiweb-wrapper/mochiweb-git/src/mochiweb_socket.erl new file mode 100644 index 0000000..76b018c --- /dev/null +++ b/rabbitmq-server/plugins-src/mochiweb-wrapper/mochiweb-git/src/mochiweb_socket.erl @@ -0,0 +1,84 @@ +%% @copyright 2010 Mochi Media, Inc. + +%% @doc MochiWeb socket - wrapper for plain and ssl sockets. + +-module(mochiweb_socket). + +-export([listen/4, accept/1, recv/3, send/2, close/1, port/1, peername/1, + setopts/2, type/1]). + +-define(ACCEPT_TIMEOUT, 2000). + +listen(Ssl, Port, Opts, SslOpts) -> + case Ssl of + true -> + case ssl:listen(Port, Opts ++ SslOpts) of + {ok, ListenSocket} -> + {ok, {ssl, ListenSocket}}; + {error, _} = Err -> + Err + end; + false -> + gen_tcp:listen(Port, Opts) + end. + +accept({ssl, ListenSocket}) -> + % There's a bug in ssl:transport_accept/2 at the moment, which is the + % reason for the try...catch block. Should be fixed in OTP R14. + try ssl:transport_accept(ListenSocket) of + {ok, Socket} -> + case ssl:ssl_accept(Socket) of + ok -> + {ok, {ssl, Socket}}; + {error, _} = Err -> + Err + end; + {error, _} = Err -> + Err + catch + error:{badmatch, {error, Reason}} -> + {error, Reason} + end; +accept(ListenSocket) -> + gen_tcp:accept(ListenSocket, ?ACCEPT_TIMEOUT). + +recv({ssl, Socket}, Length, Timeout) -> + ssl:recv(Socket, Length, Timeout); +recv(Socket, Length, Timeout) -> + gen_tcp:recv(Socket, Length, Timeout). + +send({ssl, Socket}, Data) -> + ssl:send(Socket, Data); +send(Socket, Data) -> + gen_tcp:send(Socket, Data). + +close({ssl, Socket}) -> + ssl:close(Socket); +close(Socket) -> + gen_tcp:close(Socket). + +port({ssl, Socket}) -> + case ssl:sockname(Socket) of + {ok, {_, Port}} -> + {ok, Port}; + {error, _} = Err -> + Err + end; +port(Socket) -> + inet:port(Socket). + +peername({ssl, Socket}) -> + ssl:peername(Socket); +peername(Socket) -> + inet:peername(Socket). + +setopts({ssl, Socket}, Opts) -> + ssl:setopts(Socket, Opts); +setopts(Socket, Opts) -> + inet:setopts(Socket, Opts). + +type({ssl, _}) -> + ssl; +type(_) -> + plain. + diff --git a/rabbitmq-server/plugins-src/mochiweb-wrapper/mochiweb-git/src/mochiweb_socket_server.erl b/rabbitmq-server/plugins-src/mochiweb-wrapper/mochiweb-git/src/mochiweb_socket_server.erl new file mode 100644 index 0000000..a3d4da3 --- /dev/null +++ b/rabbitmq-server/plugins-src/mochiweb-wrapper/mochiweb-git/src/mochiweb_socket_server.erl @@ -0,0 +1,348 @@ +%% @author Bob Ippolito +%% @copyright 2007 Mochi Media, Inc. + +%% @doc MochiWeb socket server. + +-module(mochiweb_socket_server). +-author('bob@mochimedia.com'). +-behaviour(gen_server). + +-include("internal.hrl"). + +-export([start/1, start_link/1, stop/1]). +-export([init/1, handle_call/3, handle_cast/2, terminate/2, code_change/3, + handle_info/2]). +-export([get/2, set/3]). + +-record(mochiweb_socket_server, + {port, + loop, + name=undefined, + %% NOTE: This is currently ignored. + max=2048, + ip=any, + listen=null, + nodelay=false, + backlog=128, + active_sockets=0, + acceptor_pool_size=16, + ssl=false, + ssl_opts=[{ssl_imp, new}], + acceptor_pool=sets:new(), + profile_fun=undefined}). + +-define(is_old_state(State), not is_record(State, mochiweb_socket_server)). + +start_link(Options) -> + start_server(start_link, parse_options(Options)). + +start(Options) -> + case lists:keytake(link, 1, Options) of + {value, {_Key, false}, Options1} -> + start_server(start, parse_options(Options1)); + _ -> + %% TODO: https://github.com/mochi/mochiweb/issues/58 + %% [X] Phase 1: Add new APIs (Sep 2011) + %% [_] Phase 2: Add deprecation warning + %% [_] Phase 3: Change default to {link, false} and ignore link + %% [_] Phase 4: Add deprecation warning for {link, _} option + %% [_] Phase 5: Remove support for {link, _} option + start_link(Options) + end. + +get(Name, Property) -> + gen_server:call(Name, {get, Property}). + +set(Name, profile_fun, Fun) -> + gen_server:cast(Name, {set, profile_fun, Fun}); +set(Name, Property, _Value) -> + error_logger:info_msg("?MODULE:set for ~p with ~p not implemented~n", + [Name, Property]). + +stop(Name) when is_atom(Name) orelse is_pid(Name) -> + gen_server:call(Name, stop); +stop({Scope, Name}) when Scope =:= local orelse Scope =:= global -> + stop(Name); +stop(Options) -> + State = parse_options(Options), + stop(State#mochiweb_socket_server.name). + +%% Internal API + +parse_options(State=#mochiweb_socket_server{}) -> + State; +parse_options(Options) -> + parse_options(Options, #mochiweb_socket_server{}). + +parse_options([], State) -> + State; +parse_options([{name, L} | Rest], State) when is_list(L) -> + Name = {local, list_to_atom(L)}, + parse_options(Rest, State#mochiweb_socket_server{name=Name}); +parse_options([{name, A} | Rest], State) when A =:= undefined -> + parse_options(Rest, State#mochiweb_socket_server{name=A}); +parse_options([{name, A} | Rest], State) when is_atom(A) -> + Name = {local, A}, + parse_options(Rest, State#mochiweb_socket_server{name=Name}); +parse_options([{name, Name} | Rest], State) -> + parse_options(Rest, State#mochiweb_socket_server{name=Name}); +parse_options([{port, L} | Rest], State) when is_list(L) -> + Port = list_to_integer(L), + parse_options(Rest, State#mochiweb_socket_server{port=Port}); +parse_options([{port, Port} | Rest], State) -> + parse_options(Rest, State#mochiweb_socket_server{port=Port}); +parse_options([{ip, Ip} | Rest], State) -> + ParsedIp = case Ip of + any -> + any; + Ip when is_tuple(Ip) -> + Ip; + Ip when is_list(Ip) -> + {ok, IpTuple} = inet_parse:address(Ip), + IpTuple + end, + parse_options(Rest, State#mochiweb_socket_server{ip=ParsedIp}); +parse_options([{loop, Loop} | Rest], State) -> + parse_options(Rest, State#mochiweb_socket_server{loop=Loop}); +parse_options([{backlog, Backlog} | Rest], State) -> + parse_options(Rest, State#mochiweb_socket_server{backlog=Backlog}); +parse_options([{nodelay, NoDelay} | Rest], State) -> + parse_options(Rest, State#mochiweb_socket_server{nodelay=NoDelay}); +parse_options([{acceptor_pool_size, Max} | Rest], State) -> + MaxInt = ensure_int(Max), + parse_options(Rest, + State#mochiweb_socket_server{acceptor_pool_size=MaxInt}); +parse_options([{max, Max} | Rest], State) -> + error_logger:info_report([{warning, "TODO: max is currently unsupported"}, + {max, Max}]), + MaxInt = ensure_int(Max), + parse_options(Rest, State#mochiweb_socket_server{max=MaxInt}); +parse_options([{ssl, Ssl} | Rest], State) when is_boolean(Ssl) -> + parse_options(Rest, State#mochiweb_socket_server{ssl=Ssl}); +parse_options([{ssl_opts, SslOpts} | Rest], State) when is_list(SslOpts) -> + SslOpts1 = [{ssl_imp, new} | proplists:delete(ssl_imp, SslOpts)], + parse_options(Rest, State#mochiweb_socket_server{ssl_opts=SslOpts1}); +parse_options([{profile_fun, ProfileFun} | Rest], State) when is_function(ProfileFun) -> + parse_options(Rest, State#mochiweb_socket_server{profile_fun=ProfileFun}). + + +start_server(F, State=#mochiweb_socket_server{ssl=Ssl, name=Name}) -> + ok = prep_ssl(Ssl), + case Name of + undefined -> + gen_server:F(?MODULE, State, []); + _ -> + gen_server:F(Name, ?MODULE, State, []) + end. + +prep_ssl(true) -> + ok = mochiweb:ensure_started(crypto), + ok = mochiweb:ensure_started(asn1), + ok = mochiweb:ensure_started(public_key), + ok = mochiweb:ensure_started(ssl); +prep_ssl(false) -> + ok. + +ensure_int(N) when is_integer(N) -> + N; +ensure_int(S) when is_list(S) -> + list_to_integer(S). + +ipv6_supported() -> + case (catch inet:getaddr("localhost", inet6)) of + {ok, _Addr} -> + true; + {error, _} -> + false + end. + +init(State=#mochiweb_socket_server{ip=Ip, port=Port, backlog=Backlog, nodelay=NoDelay}) -> + process_flag(trap_exit, true), + BaseOpts = [binary, + {reuseaddr, true}, + {packet, 0}, + {backlog, Backlog}, + {recbuf, ?RECBUF_SIZE}, + {exit_on_close, false}, + {active, false}, + {nodelay, NoDelay}], + Opts = case Ip of + any -> + case ipv6_supported() of % IPv4, and IPv6 if supported + true -> [inet, inet6 | BaseOpts]; + _ -> BaseOpts + end; + {_, _, _, _} -> % IPv4 + [inet, {ip, Ip} | BaseOpts]; + {_, _, _, _, _, _, _, _} -> % IPv6 + [inet6, {ip, Ip} | BaseOpts] + end, + listen(Port, Opts, State). + +new_acceptor_pool(Listen, + State=#mochiweb_socket_server{acceptor_pool=Pool, + acceptor_pool_size=Size, + loop=Loop}) -> + F = fun (_, S) -> + Pid = mochiweb_acceptor:start_link(self(), Listen, Loop), + sets:add_element(Pid, S) + end, + Pool1 = lists:foldl(F, Pool, lists:seq(1, Size)), + State#mochiweb_socket_server{acceptor_pool=Pool1}. + +listen(Port, Opts, State=#mochiweb_socket_server{ssl=Ssl, ssl_opts=SslOpts}) -> + case mochiweb_socket:listen(Ssl, Port, Opts, SslOpts) of + {ok, Listen} -> + {ok, ListenPort} = mochiweb_socket:port(Listen), + {ok, new_acceptor_pool( + Listen, + State#mochiweb_socket_server{listen=Listen, + port=ListenPort})}; + {error, Reason} -> + {stop, Reason} + end. + +do_get(port, #mochiweb_socket_server{port=Port}) -> + Port; +do_get(active_sockets, #mochiweb_socket_server{active_sockets=ActiveSockets}) -> + ActiveSockets. + + +state_to_proplist(#mochiweb_socket_server{name=Name, + port=Port, + active_sockets=ActiveSockets}) -> + [{name, Name}, {port, Port}, {active_sockets, ActiveSockets}]. + +upgrade_state(State = #mochiweb_socket_server{}) -> + State; +upgrade_state({mochiweb_socket_server, Port, Loop, Name, + Max, IP, Listen, NoDelay, Backlog, ActiveSockets, + AcceptorPoolSize, SSL, SSL_opts, + AcceptorPool}) -> + #mochiweb_socket_server{port=Port, loop=Loop, name=Name, max=Max, ip=IP, + listen=Listen, nodelay=NoDelay, backlog=Backlog, + active_sockets=ActiveSockets, + acceptor_pool_size=AcceptorPoolSize, + ssl=SSL, + ssl_opts=SSL_opts, + acceptor_pool=AcceptorPool}. + +handle_call(Req, From, State) when ?is_old_state(State) -> + handle_call(Req, From, upgrade_state(State)); +handle_call({get, Property}, _From, State) -> + Res = do_get(Property, State), + {reply, Res, State}; +handle_call(stop, _From, State) -> + {stop, normal, ok, State}; +handle_call(_Message, _From, State) -> + Res = error, + {reply, Res, State}. + + +handle_cast(Req, State) when ?is_old_state(State) -> + handle_cast(Req, upgrade_state(State)); +handle_cast({accepted, Pid, Timing}, + State=#mochiweb_socket_server{active_sockets=ActiveSockets}) -> + State1 = State#mochiweb_socket_server{active_sockets=1 + ActiveSockets}, + case State#mochiweb_socket_server.profile_fun of + undefined -> + undefined; + F when is_function(F) -> + catch F([{timing, Timing} | state_to_proplist(State1)]) + end, + {noreply, recycle_acceptor(Pid, State1)}; +handle_cast({set, profile_fun, ProfileFun}, State) -> + State1 = case ProfileFun of + ProfileFun when is_function(ProfileFun); ProfileFun =:= undefined -> + State#mochiweb_socket_server{profile_fun=ProfileFun}; + _ -> + State + end, + {noreply, State1}. + + +terminate(Reason, State) when ?is_old_state(State) -> + terminate(Reason, upgrade_state(State)); +terminate(_Reason, #mochiweb_socket_server{listen=Listen}) -> + mochiweb_socket:close(Listen). + +code_change(_OldVsn, State, _Extra) -> + State. + +recycle_acceptor(Pid, State=#mochiweb_socket_server{ + acceptor_pool=Pool, + listen=Listen, + loop=Loop, + active_sockets=ActiveSockets}) -> + case sets:is_element(Pid, Pool) of + true -> + Acceptor = mochiweb_acceptor:start_link(self(), Listen, Loop), + Pool1 = sets:add_element(Acceptor, sets:del_element(Pid, Pool)), + State#mochiweb_socket_server{acceptor_pool=Pool1}; + false -> + State#mochiweb_socket_server{active_sockets=ActiveSockets - 1} + end. + +handle_info(Msg, State) when ?is_old_state(State) -> + handle_info(Msg, upgrade_state(State)); +handle_info({'EXIT', Pid, normal}, State) -> + {noreply, recycle_acceptor(Pid, State)}; +handle_info({'EXIT', Pid, Reason}, + State=#mochiweb_socket_server{acceptor_pool=Pool}) -> + case sets:is_element(Pid, Pool) of + true -> + %% If there was an unexpected error accepting, log and sleep. + error_logger:error_report({?MODULE, ?LINE, + {acceptor_error, Reason}}), + timer:sleep(100); + false -> + ok + end, + {noreply, recycle_acceptor(Pid, State)}; + +% this is what release_handler needs to get a list of modules, +% since our supervisor modules list is set to 'dynamic' +% see sasl-2.1.9.2/src/release_handler_1.erl get_dynamic_mods +handle_info({From, Tag, get_modules}, State = #mochiweb_socket_server{name={local,Mod}}) -> + From ! {element(2,Tag), [Mod]}, + {noreply, State}; + +% If for some reason we can't get the module name, send empty list to avoid release_handler timeout: +handle_info({From, Tag, get_modules}, State) -> + error_logger:info_msg("mochiweb_socket_server replying to dynamic modules request as '[]'~n",[]), + From ! {element(2,Tag), []}, + {noreply, State}; + +handle_info(Info, State) -> + error_logger:info_report([{'INFO', Info}, {'State', State}]), + {noreply, State}. + + + +%% +%% Tests +%% +-ifdef(TEST). +-include_lib("eunit/include/eunit.hrl"). + +upgrade_state_test() -> + OldState = {mochiweb_socket_server, + port, loop, name, + max, ip, listen, + nodelay, backlog, + active_sockets, + acceptor_pool_size, + ssl, ssl_opts, acceptor_pool}, + State = upgrade_state(OldState), + CmpState = #mochiweb_socket_server{port=port, loop=loop, + name=name, max=max, ip=ip, + listen=listen, nodelay=nodelay, + backlog=backlog, + active_sockets=active_sockets, + acceptor_pool_size=acceptor_pool_size, + ssl=ssl, ssl_opts=ssl_opts, + acceptor_pool=acceptor_pool, + profile_fun=undefined}, + ?assertEqual(CmpState, State). + +-endif. diff --git a/rabbitmq-server/plugins-src/mochiweb-wrapper/mochiweb-git/src/mochiweb_util.erl b/rabbitmq-server/plugins-src/mochiweb-wrapper/mochiweb-git/src/mochiweb_util.erl new file mode 100644 index 0000000..a0bc2bc --- /dev/null +++ b/rabbitmq-server/plugins-src/mochiweb-wrapper/mochiweb-git/src/mochiweb_util.erl @@ -0,0 +1,992 @@ +%% @author Bob Ippolito +%% @copyright 2007 Mochi Media, Inc. + +%% @doc Utilities for parsing and quoting. + +-module(mochiweb_util). +-author('bob@mochimedia.com'). +-export([join/2, quote_plus/1, urlencode/1, parse_qs/1, unquote/1]). +-export([path_split/1]). +-export([urlsplit/1, urlsplit_path/1, urlunsplit/1, urlunsplit_path/1]). +-export([guess_mime/1, parse_header/1]). +-export([shell_quote/1, cmd/1, cmd_string/1, cmd_port/2, cmd_status/1, cmd_status/2]). +-export([record_to_proplist/2, record_to_proplist/3]). +-export([safe_relative_path/1, partition/2]). +-export([parse_qvalues/1, pick_accepted_encodings/3]). +-export([make_io/1, rand_bytes/1, rand_uniform/2]). + +-define(PERCENT, 37). % $\% +-define(FULLSTOP, 46). % $\. +-define(IS_HEX(C), ((C >= $0 andalso C =< $9) orelse + (C >= $a andalso C =< $f) orelse + (C >= $A andalso C =< $F))). +-define(QS_SAFE(C), ((C >= $a andalso C =< $z) orelse + (C >= $A andalso C =< $Z) orelse + (C >= $0 andalso C =< $9) orelse + (C =:= ?FULLSTOP orelse C =:= $- orelse C =:= $~ orelse + C =:= $_))). + +hexdigit(C) when C < 10 -> $0 + C; +hexdigit(C) when C < 16 -> $A + (C - 10). + +unhexdigit(C) when C >= $0, C =< $9 -> C - $0; +unhexdigit(C) when C >= $a, C =< $f -> C - $a + 10; +unhexdigit(C) when C >= $A, C =< $F -> C - $A + 10. + +%% @spec partition(String, Sep) -> {String, [], []} | {Prefix, Sep, Postfix} +%% @doc Inspired by Python 2.5's str.partition: +%% partition("foo/bar", "/") = {"foo", "/", "bar"}, +%% partition("foo", "/") = {"foo", "", ""}. +partition(String, Sep) -> + case partition(String, Sep, []) of + undefined -> + {String, "", ""}; + Result -> + Result + end. + +partition("", _Sep, _Acc) -> + undefined; +partition(S, Sep, Acc) -> + case partition2(S, Sep) of + undefined -> + [C | Rest] = S, + partition(Rest, Sep, [C | Acc]); + Rest -> + {lists:reverse(Acc), Sep, Rest} + end. + +partition2(Rest, "") -> + Rest; +partition2([C | R1], [C | R2]) -> + partition2(R1, R2); +partition2(_S, _Sep) -> + undefined. + + + +%% @spec safe_relative_path(string()) -> string() | undefined +%% @doc Return the reduced version of a relative path or undefined if it +%% is not safe. safe relative paths can be joined with an absolute path +%% and will result in a subdirectory of the absolute path. Safe paths +%% never contain a backslash character. +safe_relative_path("/" ++ _) -> + undefined; +safe_relative_path(P) -> + case string:chr(P, $\\) of + 0 -> + safe_relative_path(P, []); + _ -> + undefined + end. + +safe_relative_path("", Acc) -> + case Acc of + [] -> + ""; + _ -> + string:join(lists:reverse(Acc), "/") + end; +safe_relative_path(P, Acc) -> + case partition(P, "/") of + {"", "/", _} -> + %% /foo or foo//bar + undefined; + {"..", _, _} when Acc =:= [] -> + undefined; + {"..", _, Rest} -> + safe_relative_path(Rest, tl(Acc)); + {Part, "/", ""} -> + safe_relative_path("", ["", Part | Acc]); + {Part, _, Rest} -> + safe_relative_path(Rest, [Part | Acc]) + end. + +%% @spec shell_quote(string()) -> string() +%% @doc Quote a string according to UNIX shell quoting rules, returns a string +%% surrounded by double quotes. +shell_quote(L) -> + shell_quote(L, [$\"]). + +%% @spec cmd_port([string()], Options) -> port() +%% @doc open_port({spawn, mochiweb_util:cmd_string(Argv)}, Options). +cmd_port(Argv, Options) -> + open_port({spawn, cmd_string(Argv)}, Options). + +%% @spec cmd([string()]) -> string() +%% @doc os:cmd(cmd_string(Argv)). +cmd(Argv) -> + os:cmd(cmd_string(Argv)). + +%% @spec cmd_string([string()]) -> string() +%% @doc Create a shell quoted command string from a list of arguments. +cmd_string(Argv) -> + string:join([shell_quote(X) || X <- Argv], " "). + +%% @spec cmd_status([string()]) -> {ExitStatus::integer(), Stdout::binary()} +%% @doc Accumulate the output and exit status from the given application, +%% will be spawned with cmd_port/2. +cmd_status(Argv) -> + cmd_status(Argv, []). + +%% @spec cmd_status([string()], [atom()]) -> {ExitStatus::integer(), Stdout::binary()} +%% @doc Accumulate the output and exit status from the given application, +%% will be spawned with cmd_port/2. +cmd_status(Argv, Options) -> + Port = cmd_port(Argv, [exit_status, stderr_to_stdout, + use_stdio, binary | Options]), + try cmd_loop(Port, []) + after catch port_close(Port) + end. + +%% @spec cmd_loop(port(), list()) -> {ExitStatus::integer(), Stdout::binary()} +%% @doc Accumulate the output and exit status from a port. +cmd_loop(Port, Acc) -> + receive + {Port, {exit_status, Status}} -> + {Status, iolist_to_binary(lists:reverse(Acc))}; + {Port, {data, Data}} -> + cmd_loop(Port, [Data | Acc]) + end. + +%% @spec join([iolist()], iolist()) -> iolist() +%% @doc Join a list of strings or binaries together with the given separator +%% string or char or binary. The output is flattened, but may be an +%% iolist() instead of a string() if any of the inputs are binary(). +join([], _Separator) -> + []; +join([S], _Separator) -> + lists:flatten(S); +join(Strings, Separator) -> + lists:flatten(revjoin(lists:reverse(Strings), Separator, [])). + +revjoin([], _Separator, Acc) -> + Acc; +revjoin([S | Rest], Separator, []) -> + revjoin(Rest, Separator, [S]); +revjoin([S | Rest], Separator, Acc) -> + revjoin(Rest, Separator, [S, Separator | Acc]). + +%% @spec quote_plus(atom() | integer() | float() | string() | binary()) -> string() +%% @doc URL safe encoding of the given term. +quote_plus(Atom) when is_atom(Atom) -> + quote_plus(atom_to_list(Atom)); +quote_plus(Int) when is_integer(Int) -> + quote_plus(integer_to_list(Int)); +quote_plus(Binary) when is_binary(Binary) -> + quote_plus(binary_to_list(Binary)); +quote_plus(Float) when is_float(Float) -> + quote_plus(mochinum:digits(Float)); +quote_plus(String) -> + quote_plus(String, []). + +quote_plus([], Acc) -> + lists:reverse(Acc); +quote_plus([C | Rest], Acc) when ?QS_SAFE(C) -> + quote_plus(Rest, [C | Acc]); +quote_plus([$\s | Rest], Acc) -> + quote_plus(Rest, [$+ | Acc]); +quote_plus([C | Rest], Acc) -> + <> = <>, + quote_plus(Rest, [hexdigit(Lo), hexdigit(Hi), ?PERCENT | Acc]). + +%% @spec urlencode([{Key, Value}]) -> string() +%% @doc URL encode the property list. +urlencode(Props) -> + Pairs = lists:foldr( + fun ({K, V}, Acc) -> + [quote_plus(K) ++ "=" ++ quote_plus(V) | Acc] + end, [], Props), + string:join(Pairs, "&"). + +%% @spec parse_qs(string() | binary()) -> [{Key, Value}] +%% @doc Parse a query string or application/x-www-form-urlencoded. +parse_qs(Binary) when is_binary(Binary) -> + parse_qs(binary_to_list(Binary)); +parse_qs(String) -> + parse_qs(String, []). + +parse_qs([], Acc) -> + lists:reverse(Acc); +parse_qs(String, Acc) -> + {Key, Rest} = parse_qs_key(String), + {Value, Rest1} = parse_qs_value(Rest), + parse_qs(Rest1, [{Key, Value} | Acc]). + +parse_qs_key(String) -> + parse_qs_key(String, []). + +parse_qs_key([], Acc) -> + {qs_revdecode(Acc), ""}; +parse_qs_key([$= | Rest], Acc) -> + {qs_revdecode(Acc), Rest}; +parse_qs_key(Rest=[$; | _], Acc) -> + {qs_revdecode(Acc), Rest}; +parse_qs_key(Rest=[$& | _], Acc) -> + {qs_revdecode(Acc), Rest}; +parse_qs_key([C | Rest], Acc) -> + parse_qs_key(Rest, [C | Acc]). + +parse_qs_value(String) -> + parse_qs_value(String, []). + +parse_qs_value([], Acc) -> + {qs_revdecode(Acc), ""}; +parse_qs_value([$; | Rest], Acc) -> + {qs_revdecode(Acc), Rest}; +parse_qs_value([$& | Rest], Acc) -> + {qs_revdecode(Acc), Rest}; +parse_qs_value([C | Rest], Acc) -> + parse_qs_value(Rest, [C | Acc]). + +%% @spec unquote(string() | binary()) -> string() +%% @doc Unquote a URL encoded string. +unquote(Binary) when is_binary(Binary) -> + unquote(binary_to_list(Binary)); +unquote(String) -> + qs_revdecode(lists:reverse(String)). + +qs_revdecode(S) -> + qs_revdecode(S, []). + +qs_revdecode([], Acc) -> + Acc; +qs_revdecode([$+ | Rest], Acc) -> + qs_revdecode(Rest, [$\s | Acc]); +qs_revdecode([Lo, Hi, ?PERCENT | Rest], Acc) when ?IS_HEX(Lo), ?IS_HEX(Hi) -> + qs_revdecode(Rest, [(unhexdigit(Lo) bor (unhexdigit(Hi) bsl 4)) | Acc]); +qs_revdecode([C | Rest], Acc) -> + qs_revdecode(Rest, [C | Acc]). + +%% @spec urlsplit(Url) -> {Scheme, Netloc, Path, Query, Fragment} +%% @doc Return a 5-tuple, does not expand % escapes. Only supports HTTP style +%% URLs. +urlsplit(Url) -> + {Scheme, Url1} = urlsplit_scheme(Url), + {Netloc, Url2} = urlsplit_netloc(Url1), + {Path, Query, Fragment} = urlsplit_path(Url2), + {Scheme, Netloc, Path, Query, Fragment}. + +urlsplit_scheme(Url) -> + case urlsplit_scheme(Url, []) of + no_scheme -> + {"", Url}; + Res -> + Res + end. + +urlsplit_scheme([C | Rest], Acc) when ((C >= $a andalso C =< $z) orelse + (C >= $A andalso C =< $Z) orelse + (C >= $0 andalso C =< $9) orelse + C =:= $+ orelse C =:= $- orelse + C =:= $.) -> + urlsplit_scheme(Rest, [C | Acc]); +urlsplit_scheme([$: | Rest], Acc=[_ | _]) -> + {string:to_lower(lists:reverse(Acc)), Rest}; +urlsplit_scheme(_Rest, _Acc) -> + no_scheme. + +urlsplit_netloc("//" ++ Rest) -> + urlsplit_netloc(Rest, []); +urlsplit_netloc(Path) -> + {"", Path}. + +urlsplit_netloc("", Acc) -> + {lists:reverse(Acc), ""}; +urlsplit_netloc(Rest=[C | _], Acc) when C =:= $/; C =:= $?; C =:= $# -> + {lists:reverse(Acc), Rest}; +urlsplit_netloc([C | Rest], Acc) -> + urlsplit_netloc(Rest, [C | Acc]). + + +%% @spec path_split(string()) -> {Part, Rest} +%% @doc Split a path starting from the left, as in URL traversal. +%% path_split("foo/bar") = {"foo", "bar"}, +%% path_split("/foo/bar") = {"", "foo/bar"}. +path_split(S) -> + path_split(S, []). + +path_split("", Acc) -> + {lists:reverse(Acc), ""}; +path_split("/" ++ Rest, Acc) -> + {lists:reverse(Acc), Rest}; +path_split([C | Rest], Acc) -> + path_split(Rest, [C | Acc]). + + +%% @spec urlunsplit({Scheme, Netloc, Path, Query, Fragment}) -> string() +%% @doc Assemble a URL from the 5-tuple. Path must be absolute. +urlunsplit({Scheme, Netloc, Path, Query, Fragment}) -> + lists:flatten([case Scheme of "" -> ""; _ -> [Scheme, "://"] end, + Netloc, + urlunsplit_path({Path, Query, Fragment})]). + +%% @spec urlunsplit_path({Path, Query, Fragment}) -> string() +%% @doc Assemble a URL path from the 3-tuple. +urlunsplit_path({Path, Query, Fragment}) -> + lists:flatten([Path, + case Query of "" -> ""; _ -> [$? | Query] end, + case Fragment of "" -> ""; _ -> [$# | Fragment] end]). + +%% @spec urlsplit_path(Url) -> {Path, Query, Fragment} +%% @doc Return a 3-tuple, does not expand % escapes. Only supports HTTP style +%% paths. +urlsplit_path(Path) -> + urlsplit_path(Path, []). + +urlsplit_path("", Acc) -> + {lists:reverse(Acc), "", ""}; +urlsplit_path("?" ++ Rest, Acc) -> + {Query, Fragment} = urlsplit_query(Rest), + {lists:reverse(Acc), Query, Fragment}; +urlsplit_path("#" ++ Rest, Acc) -> + {lists:reverse(Acc), "", Rest}; +urlsplit_path([C | Rest], Acc) -> + urlsplit_path(Rest, [C | Acc]). + +urlsplit_query(Query) -> + urlsplit_query(Query, []). + +urlsplit_query("", Acc) -> + {lists:reverse(Acc), ""}; +urlsplit_query("#" ++ Rest, Acc) -> + {lists:reverse(Acc), Rest}; +urlsplit_query([C | Rest], Acc) -> + urlsplit_query(Rest, [C | Acc]). + +%% @spec guess_mime(string()) -> string() +%% @doc Guess the mime type of a file by the extension of its filename. +guess_mime(File) -> + case mochiweb_mime:from_extension(filename:extension(File)) of + undefined -> + "text/plain"; + Mime -> + Mime + end. + +%% @spec parse_header(string()) -> {Type, [{K, V}]} +%% @doc Parse a Content-Type like header, return the main Content-Type +%% and a property list of options. +parse_header(String) -> + %% TODO: This is exactly as broken as Python's cgi module. + %% Should parse properly like mochiweb_cookies. + [Type | Parts] = [string:strip(S) || S <- string:tokens(String, ";")], + F = fun (S, Acc) -> + case lists:splitwith(fun (C) -> C =/= $= end, S) of + {"", _} -> + %% Skip anything with no name + Acc; + {_, ""} -> + %% Skip anything with no value + Acc; + {Name, [$\= | Value]} -> + [{string:to_lower(string:strip(Name)), + unquote_header(string:strip(Value))} | Acc] + end + end, + {string:to_lower(Type), + lists:foldr(F, [], Parts)}. + +unquote_header("\"" ++ Rest) -> + unquote_header(Rest, []); +unquote_header(S) -> + S. + +unquote_header("", Acc) -> + lists:reverse(Acc); +unquote_header("\"", Acc) -> + lists:reverse(Acc); +unquote_header([$\\, C | Rest], Acc) -> + unquote_header(Rest, [C | Acc]); +unquote_header([C | Rest], Acc) -> + unquote_header(Rest, [C | Acc]). + +%% @spec record_to_proplist(Record, Fields) -> proplist() +%% @doc calls record_to_proplist/3 with a default TypeKey of '__record' +record_to_proplist(Record, Fields) -> + record_to_proplist(Record, Fields, '__record'). + +%% @spec record_to_proplist(Record, Fields, TypeKey) -> proplist() +%% @doc Return a proplist of the given Record with each field in the +%% Fields list set as a key with the corresponding value in the Record. +%% TypeKey is the key that is used to store the record type +%% Fields should be obtained by calling record_info(fields, record_type) +%% where record_type is the record type of Record +record_to_proplist(Record, Fields, TypeKey) + when tuple_size(Record) - 1 =:= length(Fields) -> + lists:zip([TypeKey | Fields], tuple_to_list(Record)). + + +shell_quote([], Acc) -> + lists:reverse([$\" | Acc]); +shell_quote([C | Rest], Acc) when C =:= $\" orelse C =:= $\` orelse + C =:= $\\ orelse C =:= $\$ -> + shell_quote(Rest, [C, $\\ | Acc]); +shell_quote([C | Rest], Acc) -> + shell_quote(Rest, [C | Acc]). + +%% @spec parse_qvalues(string()) -> [qvalue()] | invalid_qvalue_string +%% @type qvalue() = {media_type() | encoding() , float()}. +%% @type media_type() = string(). +%% @type encoding() = string(). +%% +%% @doc Parses a list (given as a string) of elements with Q values associated +%% to them. Elements are separated by commas and each element is separated +%% from its Q value by a semicolon. Q values are optional but when missing +%% the value of an element is considered as 1.0. A Q value is always in the +%% range [0.0, 1.0]. A Q value list is used for example as the value of the +%% HTTP "Accept" and "Accept-Encoding" headers. +%% +%% Q values are described in section 2.9 of the RFC 2616 (HTTP 1.1). +%% +%% Example: +%% +%% parse_qvalues("gzip; q=0.5, deflate, identity;q=0.0") -> +%% [{"gzip", 0.5}, {"deflate", 1.0}, {"identity", 0.0}] +%% +parse_qvalues(QValuesStr) -> + try + lists:map( + fun(Pair) -> + [Type | Params] = string:tokens(Pair, ";"), + NormParams = normalize_media_params(Params), + {Q, NonQParams} = extract_q(NormParams), + {string:join([string:strip(Type) | NonQParams], ";"), Q} + end, + string:tokens(string:to_lower(QValuesStr), ",") + ) + catch + _Type:_Error -> + invalid_qvalue_string + end. + +normalize_media_params(Params) -> + {ok, Re} = re:compile("\\s"), + normalize_media_params(Re, Params, []). + +normalize_media_params(_Re, [], Acc) -> + lists:reverse(Acc); +normalize_media_params(Re, [Param | Rest], Acc) -> + NormParam = re:replace(Param, Re, "", [global, {return, list}]), + normalize_media_params(Re, Rest, [NormParam | Acc]). + +extract_q(NormParams) -> + {ok, KVRe} = re:compile("^([^=]+)=([^=]+)$"), + {ok, QRe} = re:compile("^((?:0|1)(?:\\.\\d{1,3})?)$"), + extract_q(KVRe, QRe, NormParams, []). + +extract_q(_KVRe, _QRe, [], Acc) -> + {1.0, lists:reverse(Acc)}; +extract_q(KVRe, QRe, [Param | Rest], Acc) -> + case re:run(Param, KVRe, [{capture, [1, 2], list}]) of + {match, [Name, Value]} -> + case Name of + "q" -> + {match, [Q]} = re:run(Value, QRe, [{capture, [1], list}]), + QVal = case Q of + "0" -> + 0.0; + "1" -> + 1.0; + Else -> + list_to_float(Else) + end, + case QVal < 0.0 orelse QVal > 1.0 of + false -> + {QVal, lists:reverse(Acc) ++ Rest} + end; + _ -> + extract_q(KVRe, QRe, Rest, [Param | Acc]) + end + end. + +%% @spec pick_accepted_encodings([qvalue()], [encoding()], encoding()) -> +%% [encoding()] +%% +%% @doc Determines which encodings specified in the given Q values list are +%% valid according to a list of supported encodings and a default encoding. +%% +%% The returned list of encodings is sorted, descendingly, according to the +%% Q values of the given list. The last element of this list is the given +%% default encoding unless this encoding is explicitily or implicitily +%% marked with a Q value of 0.0 in the given Q values list. +%% Note: encodings with the same Q value are kept in the same order as +%% found in the input Q values list. +%% +%% This encoding picking process is described in section 14.3 of the +%% RFC 2616 (HTTP 1.1). +%% +%% Example: +%% +%% pick_accepted_encodings( +%% [{"gzip", 0.5}, {"deflate", 1.0}], +%% ["gzip", "identity"], +%% "identity" +%% ) -> +%% ["gzip", "identity"] +%% +pick_accepted_encodings(AcceptedEncs, SupportedEncs, DefaultEnc) -> + SortedQList = lists:reverse( + lists:sort(fun({_, Q1}, {_, Q2}) -> Q1 < Q2 end, AcceptedEncs) + ), + {Accepted, Refused} = lists:foldr( + fun({E, Q}, {A, R}) -> + case Q > 0.0 of + true -> + {[E | A], R}; + false -> + {A, [E | R]} + end + end, + {[], []}, + SortedQList + ), + Refused1 = lists:foldr( + fun(Enc, Acc) -> + case Enc of + "*" -> + lists:subtract(SupportedEncs, Accepted) ++ Acc; + _ -> + [Enc | Acc] + end + end, + [], + Refused + ), + Accepted1 = lists:foldr( + fun(Enc, Acc) -> + case Enc of + "*" -> + lists:subtract(SupportedEncs, Accepted ++ Refused1) ++ Acc; + _ -> + [Enc | Acc] + end + end, + [], + Accepted + ), + Accepted2 = case lists:member(DefaultEnc, Accepted1) of + true -> + Accepted1; + false -> + Accepted1 ++ [DefaultEnc] + end, + [E || E <- Accepted2, lists:member(E, SupportedEncs), + not lists:member(E, Refused1)]. + +make_io(Atom) when is_atom(Atom) -> + atom_to_list(Atom); +make_io(Integer) when is_integer(Integer) -> + integer_to_list(Integer); +make_io(Io) when is_list(Io); is_binary(Io) -> + Io. + +rand_bytes(Count) -> + list_to_binary([rand_uniform(0, 16#FF + 1) || _ <- lists:seq(1, Count)]). + +rand_uniform(Lo, Hi) -> + random:uniform(Hi - Lo) + Lo - 1. + +%% +%% Tests +%% +-ifdef(TEST). +-include_lib("eunit/include/eunit.hrl"). + +make_io_test() -> + ?assertEqual( + <<"atom">>, + iolist_to_binary(make_io(atom))), + ?assertEqual( + <<"20">>, + iolist_to_binary(make_io(20))), + ?assertEqual( + <<"list">>, + iolist_to_binary(make_io("list"))), + ?assertEqual( + <<"binary">>, + iolist_to_binary(make_io(<<"binary">>))), + ok. + +-record(test_record, {field1=f1, field2=f2}). +record_to_proplist_test() -> + ?assertEqual( + [{'__record', test_record}, + {field1, f1}, + {field2, f2}], + record_to_proplist(#test_record{}, record_info(fields, test_record))), + ?assertEqual( + [{'typekey', test_record}, + {field1, f1}, + {field2, f2}], + record_to_proplist(#test_record{}, + record_info(fields, test_record), + typekey)), + ok. + +shell_quote_test() -> + ?assertEqual( + "\"foo \\$bar\\\"\\`' baz\"", + shell_quote("foo $bar\"`' baz")), + ok. + +cmd_port_test_spool(Port, Acc) -> + receive + {Port, eof} -> + Acc; + {Port, {data, {eol, Data}}} -> + cmd_port_test_spool(Port, ["\n", Data | Acc]); + {Port, Unknown} -> + throw({unknown, Unknown}) + after 1000 -> + throw(timeout) + end. + +cmd_port_test() -> + Port = cmd_port(["echo", "$bling$ `word`!"], + [eof, stream, {line, 4096}]), + Res = try lists:append(lists:reverse(cmd_port_test_spool(Port, []))) + after catch port_close(Port) + end, + self() ! {Port, wtf}, + try cmd_port_test_spool(Port, []) + catch throw:{unknown, wtf} -> ok + end, + try cmd_port_test_spool(Port, []) + catch throw:timeout -> ok + end, + ?assertEqual( + "$bling$ `word`!\n", + Res). + +cmd_test() -> + ?assertEqual( + "$bling$ `word`!\n", + cmd(["echo", "$bling$ `word`!"])), + ok. + +cmd_string_test() -> + ?assertEqual( + "\"echo\" \"\\$bling\\$ \\`word\\`!\"", + cmd_string(["echo", "$bling$ `word`!"])), + ok. + +cmd_status_test() -> + ?assertEqual( + {0, <<"$bling$ `word`!\n">>}, + cmd_status(["echo", "$bling$ `word`!"])), + ok. + + +parse_header_test() -> + ?assertEqual( + {"multipart/form-data", [{"boundary", "AaB03x"}]}, + parse_header("multipart/form-data; boundary=AaB03x")), + %% This tests (currently) intentionally broken behavior + ?assertEqual( + {"multipart/form-data", + [{"b", ""}, + {"cgi", "is"}, + {"broken", "true\"e"}]}, + parse_header("multipart/form-data;b=;cgi=\"i\\s;broken=true\"e;=z;z")), + ok. + +guess_mime_test() -> + "text/plain" = guess_mime(""), + "text/plain" = guess_mime(".text"), + "application/zip" = guess_mime(".zip"), + "application/zip" = guess_mime("x.zip"), + "text/html" = guess_mime("x.html"), + "application/xhtml+xml" = guess_mime("x.xhtml"), + ok. + +path_split_test() -> + {"", "foo/bar"} = path_split("/foo/bar"), + {"foo", "bar"} = path_split("foo/bar"), + {"bar", ""} = path_split("bar"), + ok. + +urlsplit_test() -> + {"", "", "/foo", "", "bar?baz"} = urlsplit("/foo#bar?baz"), + {"http", "host:port", "/foo", "", "bar?baz"} = + urlsplit("http://host:port/foo#bar?baz"), + {"http", "host", "", "", ""} = urlsplit("http://host"), + {"", "", "/wiki/Category:Fruit", "", ""} = + urlsplit("/wiki/Category:Fruit"), + ok. + +urlsplit_path_test() -> + {"/foo/bar", "", ""} = urlsplit_path("/foo/bar"), + {"/foo", "baz", ""} = urlsplit_path("/foo?baz"), + {"/foo", "", "bar?baz"} = urlsplit_path("/foo#bar?baz"), + {"/foo", "", "bar?baz#wibble"} = urlsplit_path("/foo#bar?baz#wibble"), + {"/foo", "bar", "baz"} = urlsplit_path("/foo?bar#baz"), + {"/foo", "bar?baz", "baz"} = urlsplit_path("/foo?bar?baz#baz"), + ok. + +urlunsplit_test() -> + "/foo#bar?baz" = urlunsplit({"", "", "/foo", "", "bar?baz"}), + "http://host:port/foo#bar?baz" = + urlunsplit({"http", "host:port", "/foo", "", "bar?baz"}), + ok. + +urlunsplit_path_test() -> + "/foo/bar" = urlunsplit_path({"/foo/bar", "", ""}), + "/foo?baz" = urlunsplit_path({"/foo", "baz", ""}), + "/foo#bar?baz" = urlunsplit_path({"/foo", "", "bar?baz"}), + "/foo#bar?baz#wibble" = urlunsplit_path({"/foo", "", "bar?baz#wibble"}), + "/foo?bar#baz" = urlunsplit_path({"/foo", "bar", "baz"}), + "/foo?bar?baz#baz" = urlunsplit_path({"/foo", "bar?baz", "baz"}), + ok. + +join_test() -> + ?assertEqual("foo,bar,baz", + join(["foo", "bar", "baz"], $,)), + ?assertEqual("foo,bar,baz", + join(["foo", "bar", "baz"], ",")), + ?assertEqual("foo bar", + join([["foo", " bar"]], ",")), + ?assertEqual("foo bar,baz", + join([["foo", " bar"], "baz"], ",")), + ?assertEqual("foo", + join(["foo"], ",")), + ?assertEqual("foobarbaz", + join(["foo", "bar", "baz"], "")), + ?assertEqual("foo" ++ [<<>>] ++ "bar" ++ [<<>>] ++ "baz", + join(["foo", "bar", "baz"], <<>>)), + ?assertEqual("foobar" ++ [<<"baz">>], + join(["foo", "bar", <<"baz">>], "")), + ?assertEqual("", + join([], "any")), + ok. + +quote_plus_test() -> + "foo" = quote_plus(foo), + "1" = quote_plus(1), + "1.1" = quote_plus(1.1), + "foo" = quote_plus("foo"), + "foo+bar" = quote_plus("foo bar"), + "foo%0A" = quote_plus("foo\n"), + "foo%0A" = quote_plus("foo\n"), + "foo%3B%26%3D" = quote_plus("foo;&="), + "foo%3B%26%3D" = quote_plus(<<"foo;&=">>), + ok. + +unquote_test() -> + ?assertEqual("foo bar", + unquote("foo+bar")), + ?assertEqual("foo bar", + unquote("foo%20bar")), + ?assertEqual("foo\r\n", + unquote("foo%0D%0A")), + ?assertEqual("foo\r\n", + unquote(<<"foo%0D%0A">>)), + ok. + +urlencode_test() -> + "foo=bar&baz=wibble+%0D%0A&z=1" = urlencode([{foo, "bar"}, + {"baz", "wibble \r\n"}, + {z, 1}]), + ok. + +parse_qs_test() -> + ?assertEqual( + [{"foo", "bar"}, {"baz", "wibble \r\n"}, {"z", "1"}], + parse_qs("foo=bar&baz=wibble+%0D%0a&z=1")), + ?assertEqual( + [{"", "bar"}, {"baz", "wibble \r\n"}, {"z", ""}], + parse_qs("=bar&baz=wibble+%0D%0a&z=")), + ?assertEqual( + [{"foo", "bar"}, {"baz", "wibble \r\n"}, {"z", "1"}], + parse_qs(<<"foo=bar&baz=wibble+%0D%0a&z=1">>)), + ?assertEqual( + [], + parse_qs("")), + ?assertEqual( + [{"foo", ""}, {"bar", ""}, {"baz", ""}], + parse_qs("foo;bar&baz")), + ok. + +partition_test() -> + {"foo", "", ""} = partition("foo", "/"), + {"foo", "/", "bar"} = partition("foo/bar", "/"), + {"foo", "/", ""} = partition("foo/", "/"), + {"", "/", "bar"} = partition("/bar", "/"), + {"f", "oo/ba", "r"} = partition("foo/bar", "oo/ba"), + ok. + +safe_relative_path_test() -> + "foo" = safe_relative_path("foo"), + "foo/" = safe_relative_path("foo/"), + "foo" = safe_relative_path("foo/bar/.."), + "bar" = safe_relative_path("foo/../bar"), + "bar/" = safe_relative_path("foo/../bar/"), + "" = safe_relative_path("foo/.."), + "" = safe_relative_path("foo/../"), + undefined = safe_relative_path("/foo"), + undefined = safe_relative_path("../foo"), + undefined = safe_relative_path("foo/../.."), + undefined = safe_relative_path("foo//"), + undefined = safe_relative_path("foo\\bar"), + ok. + +parse_qvalues_test() -> + [] = parse_qvalues(""), + [{"identity", 0.0}] = parse_qvalues("identity;q=0"), + [{"identity", 0.0}] = parse_qvalues("identity ;q=0"), + [{"identity", 0.0}] = parse_qvalues(" identity; q =0 "), + [{"identity", 0.0}] = parse_qvalues("identity ; q = 0"), + [{"identity", 0.0}] = parse_qvalues("identity ; q= 0.0"), + [{"gzip", 1.0}, {"deflate", 1.0}, {"identity", 0.0}] = parse_qvalues( + "gzip,deflate,identity;q=0.0" + ), + [{"deflate", 1.0}, {"gzip", 1.0}, {"identity", 0.0}] = parse_qvalues( + "deflate,gzip,identity;q=0.0" + ), + [{"gzip", 1.0}, {"deflate", 1.0}, {"gzip", 1.0}, {"identity", 0.0}] = + parse_qvalues("gzip,deflate,gzip,identity;q=0"), + [{"gzip", 1.0}, {"deflate", 1.0}, {"identity", 0.0}] = parse_qvalues( + "gzip, deflate , identity; q=0.0" + ), + [{"gzip", 1.0}, {"deflate", 1.0}, {"identity", 0.0}] = parse_qvalues( + "gzip; q=1, deflate;q=1.0, identity;q=0.0" + ), + [{"gzip", 0.5}, {"deflate", 1.0}, {"identity", 0.0}] = parse_qvalues( + "gzip; q=0.5, deflate;q=1.0, identity;q=0" + ), + [{"gzip", 0.5}, {"deflate", 1.0}, {"identity", 0.0}] = parse_qvalues( + "gzip; q=0.5, deflate , identity;q=0.0" + ), + [{"gzip", 0.5}, {"deflate", 0.8}, {"identity", 0.0}] = parse_qvalues( + "gzip; q=0.5, deflate;q=0.8, identity;q=0.0" + ), + [{"gzip", 0.5}, {"deflate", 1.0}, {"identity", 1.0}] = parse_qvalues( + "gzip; q=0.5,deflate,identity" + ), + [{"gzip", 0.5}, {"deflate", 1.0}, {"identity", 1.0}, {"identity", 1.0}] = + parse_qvalues("gzip; q=0.5,deflate,identity, identity "), + [{"text/html;level=1", 1.0}, {"text/plain", 0.5}] = + parse_qvalues("text/html;level=1, text/plain;q=0.5"), + [{"text/html;level=1", 0.3}, {"text/plain", 1.0}] = + parse_qvalues("text/html;level=1;q=0.3, text/plain"), + [{"text/html;level=1", 0.3}, {"text/plain", 1.0}] = + parse_qvalues("text/html; level = 1; q = 0.3, text/plain"), + [{"text/html;level=1", 0.3}, {"text/plain", 1.0}] = + parse_qvalues("text/html;q=0.3;level=1, text/plain"), + invalid_qvalue_string = parse_qvalues("gzip; q=1.1, deflate"), + invalid_qvalue_string = parse_qvalues("gzip; q=0.5, deflate;q=2"), + invalid_qvalue_string = parse_qvalues("gzip, deflate;q=AB"), + invalid_qvalue_string = parse_qvalues("gzip; q=2.1, deflate"), + invalid_qvalue_string = parse_qvalues("gzip; q=0.1234, deflate"), + invalid_qvalue_string = parse_qvalues("text/html;level=1;q=0.3, text/html;level"), + ok. + +pick_accepted_encodings_test() -> + ["identity"] = pick_accepted_encodings( + [], + ["gzip", "identity"], + "identity" + ), + ["gzip", "identity"] = pick_accepted_encodings( + [{"gzip", 1.0}], + ["gzip", "identity"], + "identity" + ), + ["identity"] = pick_accepted_encodings( + [{"gzip", 0.0}], + ["gzip", "identity"], + "identity" + ), + ["gzip", "identity"] = pick_accepted_encodings( + [{"gzip", 1.0}, {"deflate", 1.0}], + ["gzip", "identity"], + "identity" + ), + ["gzip", "identity"] = pick_accepted_encodings( + [{"gzip", 0.5}, {"deflate", 1.0}], + ["gzip", "identity"], + "identity" + ), + ["identity"] = pick_accepted_encodings( + [{"gzip", 0.0}, {"deflate", 0.0}], + ["gzip", "identity"], + "identity" + ), + ["gzip"] = pick_accepted_encodings( + [{"gzip", 1.0}, {"deflate", 1.0}, {"identity", 0.0}], + ["gzip", "identity"], + "identity" + ), + ["gzip", "deflate", "identity"] = pick_accepted_encodings( + [{"gzip", 1.0}, {"deflate", 1.0}], + ["gzip", "deflate", "identity"], + "identity" + ), + ["gzip", "deflate"] = pick_accepted_encodings( + [{"gzip", 1.0}, {"deflate", 1.0}, {"identity", 0.0}], + ["gzip", "deflate", "identity"], + "identity" + ), + ["deflate", "gzip", "identity"] = pick_accepted_encodings( + [{"gzip", 0.2}, {"deflate", 1.0}], + ["gzip", "deflate", "identity"], + "identity" + ), + ["deflate", "deflate", "gzip", "identity"] = pick_accepted_encodings( + [{"gzip", 0.2}, {"deflate", 1.0}, {"deflate", 1.0}], + ["gzip", "deflate", "identity"], + "identity" + ), + ["deflate", "gzip", "gzip", "identity"] = pick_accepted_encodings( + [{"gzip", 0.2}, {"deflate", 1.0}, {"gzip", 1.0}], + ["gzip", "deflate", "identity"], + "identity" + ), + ["gzip", "deflate", "gzip", "identity"] = pick_accepted_encodings( + [{"gzip", 0.2}, {"deflate", 0.9}, {"gzip", 1.0}], + ["gzip", "deflate", "identity"], + "identity" + ), + [] = pick_accepted_encodings( + [{"*", 0.0}], + ["gzip", "deflate", "identity"], + "identity" + ), + ["gzip", "deflate", "identity"] = pick_accepted_encodings( + [{"*", 1.0}], + ["gzip", "deflate", "identity"], + "identity" + ), + ["gzip", "deflate", "identity"] = pick_accepted_encodings( + [{"*", 0.6}], + ["gzip", "deflate", "identity"], + "identity" + ), + ["gzip"] = pick_accepted_encodings( + [{"gzip", 1.0}, {"*", 0.0}], + ["gzip", "deflate", "identity"], + "identity" + ), + ["gzip", "deflate"] = pick_accepted_encodings( + [{"gzip", 1.0}, {"deflate", 0.6}, {"*", 0.0}], + ["gzip", "deflate", "identity"], + "identity" + ), + ["deflate", "gzip"] = pick_accepted_encodings( + [{"gzip", 0.5}, {"deflate", 1.0}, {"*", 0.0}], + ["gzip", "deflate", "identity"], + "identity" + ), + ["gzip", "identity"] = pick_accepted_encodings( + [{"deflate", 0.0}, {"*", 1.0}], + ["gzip", "deflate", "identity"], + "identity" + ), + ["gzip", "identity"] = pick_accepted_encodings( + [{"*", 1.0}, {"deflate", 0.0}], + ["gzip", "deflate", "identity"], + "identity" + ), + ok. + +-endif. diff --git a/rabbitmq-server/plugins-src/mochiweb-wrapper/mochiweb-git/src/reloader.erl b/rabbitmq-server/plugins-src/mochiweb-wrapper/mochiweb-git/src/reloader.erl new file mode 100644 index 0000000..8266b33 --- /dev/null +++ b/rabbitmq-server/plugins-src/mochiweb-wrapper/mochiweb-git/src/reloader.erl @@ -0,0 +1,161 @@ +%% @copyright 2007 Mochi Media, Inc. +%% @author Matthew Dempsky +%% +%% @doc Erlang module for automatically reloading modified modules +%% during development. + +-module(reloader). +-author("Matthew Dempsky "). + +-include_lib("kernel/include/file.hrl"). + +-behaviour(gen_server). +-export([start/0, start_link/0]). +-export([stop/0]). +-export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]). +-export([all_changed/0]). +-export([is_changed/1]). +-export([reload_modules/1]). +-record(state, {last, tref}). + +%% External API + +%% @spec start() -> ServerRet +%% @doc Start the reloader. +start() -> + gen_server:start({local, ?MODULE}, ?MODULE, [], []). + +%% @spec start_link() -> ServerRet +%% @doc Start the reloader. +start_link() -> + gen_server:start_link({local, ?MODULE}, ?MODULE, [], []). + +%% @spec stop() -> ok +%% @doc Stop the reloader. +stop() -> + gen_server:call(?MODULE, stop). + +%% gen_server callbacks + +%% @spec init([]) -> {ok, State} +%% @doc gen_server init, opens the server in an initial state. +init([]) -> + {ok, TRef} = timer:send_interval(timer:seconds(1), doit), + {ok, #state{last = stamp(), tref = TRef}}. + +%% @spec handle_call(Args, From, State) -> tuple() +%% @doc gen_server callback. +handle_call(stop, _From, State) -> + {stop, shutdown, stopped, State}; +handle_call(_Req, _From, State) -> + {reply, {error, badrequest}, State}. + +%% @spec handle_cast(Cast, State) -> tuple() +%% @doc gen_server callback. +handle_cast(_Req, State) -> + {noreply, State}. + +%% @spec handle_info(Info, State) -> tuple() +%% @doc gen_server callback. +handle_info(doit, State) -> + Now = stamp(), + _ = doit(State#state.last, Now), + {noreply, State#state{last = Now}}; +handle_info(_Info, State) -> + {noreply, State}. + +%% @spec terminate(Reason, State) -> ok +%% @doc gen_server termination callback. +terminate(_Reason, State) -> + {ok, cancel} = timer:cancel(State#state.tref), + ok. + + +%% @spec code_change(_OldVsn, State, _Extra) -> State +%% @doc gen_server code_change callback (trivial). +code_change(_Vsn, State, _Extra) -> + {ok, State}. + +%% @spec reload_modules([atom()]) -> [{module, atom()} | {error, term()}] +%% @doc code:purge/1 and code:load_file/1 the given list of modules in order, +%% return the results of code:load_file/1. +reload_modules(Modules) -> + [begin code:purge(M), code:load_file(M) end || M <- Modules]. + +%% @spec all_changed() -> [atom()] +%% @doc Return a list of beam modules that have changed. +all_changed() -> + [M || {M, Fn} <- code:all_loaded(), is_list(Fn), is_changed(M)]. + +%% @spec is_changed(atom()) -> boolean() +%% @doc true if the loaded module is a beam with a vsn attribute +%% and does not match the on-disk beam file, returns false otherwise. +is_changed(M) -> + try + module_vsn(M:module_info()) =/= module_vsn(code:get_object_code(M)) + catch _:_ -> + false + end. + +%% Internal API + +module_vsn({M, Beam, _Fn}) -> + {ok, {M, Vsn}} = beam_lib:version(Beam), + Vsn; +module_vsn(L) when is_list(L) -> + {_, Attrs} = lists:keyfind(attributes, 1, L), + {_, Vsn} = lists:keyfind(vsn, 1, Attrs), + Vsn. + +doit(From, To) -> + [case file:read_file_info(Filename) of + {ok, #file_info{mtime = Mtime}} when Mtime >= From, Mtime < To -> + reload(Module); + {ok, _} -> + unmodified; + {error, enoent} -> + %% The Erlang compiler deletes existing .beam files if + %% recompiling fails. Maybe it's worth spitting out a + %% warning here, but I'd want to limit it to just once. + gone; + {error, Reason} -> + io:format("Error reading ~s's file info: ~p~n", + [Filename, Reason]), + error + end || {Module, Filename} <- code:all_loaded(), is_list(Filename)]. + +reload(Module) -> + io:format("Reloading ~p ...", [Module]), + code:purge(Module), + case code:load_file(Module) of + {module, Module} -> + io:format(" ok.~n"), + case erlang:function_exported(Module, test, 0) of + true -> + io:format(" - Calling ~p:test() ...", [Module]), + case catch Module:test() of + ok -> + io:format(" ok.~n"), + reload; + Reason -> + io:format(" fail: ~p.~n", [Reason]), + reload_but_test_failed + end; + false -> + reload + end; + {error, Reason} -> + io:format(" fail: ~p.~n", [Reason]), + error + end. + + +stamp() -> + erlang:localtime(). + +%% +%% Tests +%% +-ifdef(TEST). +-include_lib("eunit/include/eunit.hrl"). +-endif. diff --git a/rabbitmq-server/plugins-src/mochiweb-wrapper/mochiweb-git/support/templates/mochiwebapp.template b/rabbitmq-server/plugins-src/mochiweb-wrapper/mochiweb-git/support/templates/mochiwebapp.template new file mode 100644 index 0000000..4942609 --- /dev/null +++ b/rabbitmq-server/plugins-src/mochiweb-wrapper/mochiweb-git/support/templates/mochiwebapp.template @@ -0,0 +1,22 @@ +%% -*- erlang -*- +{variables, [{appid, "mochiwebapp"}, + {author, "Mochi Media "}, + {year, "2010"}, + {version, "0.1"}, + {port, 8080}, + {dest, "{{appid}}"}]}. +{dir, "{{dest}}"}. +{template, "mochiwebapp_skel/src/mochiapp.app.src", "{{dest}}/src/{{appid}}.app.src"}. +{template, "mochiwebapp_skel/src/mochiapp.erl", "{{dest}}/src/{{appid}}.erl"}. +{template, "mochiwebapp_skel/src/mochiapp_app.erl", "{{dest}}/src/{{appid}}_app.erl"}. +{template, "mochiwebapp_skel/src/mochiapp_deps.erl", "{{dest}}/src/{{appid}}_deps.erl"}. +{template, "mochiwebapp_skel/src/mochiapp_sup.erl", "{{dest}}/src/{{appid}}_sup.erl"}. +{template, "mochiwebapp_skel/src/mochiapp_web.erl", "{{dest}}/src/{{appid}}_web.erl"}. +{template, "mochiwebapp_skel/start-dev.sh", "{{dest}}/start-dev.sh"}. +{template, "mochiwebapp_skel/priv/www/index.html", "{{dest}}/priv/www/index.html"}. +{file, "../../.gitignore", "{{dest}}/.gitignore"}. +{file, "../../Makefile", "{{dest}}/Makefile"}. +{file, "mochiwebapp_skel/rebar.config", "{{dest}}/rebar.config"}. +{file, "../../rebar", "{{dest}}/rebar"}. +{chmod, 8#755, "{{dest}}/rebar"}. +{chmod, 8#755, "{{dest}}/start-dev.sh"}. diff --git a/rabbitmq-server/plugins-src/mochiweb-wrapper/mochiweb-git/support/templates/mochiwebapp_skel/priv/www/index.html b/rabbitmq-server/plugins-src/mochiweb-wrapper/mochiweb-git/support/templates/mochiwebapp_skel/priv/www/index.html new file mode 100644 index 0000000..40ac0c8 --- /dev/null +++ b/rabbitmq-server/plugins-src/mochiweb-wrapper/mochiweb-git/support/templates/mochiwebapp_skel/priv/www/index.html @@ -0,0 +1,8 @@ + + +It Worked + + +{{appid}} running. + + diff --git a/rabbitmq-server/plugins-src/mochiweb-wrapper/mochiweb-git/support/templates/mochiwebapp_skel/rebar.config b/rabbitmq-server/plugins-src/mochiweb-wrapper/mochiweb-git/support/templates/mochiwebapp_skel/rebar.config new file mode 100644 index 0000000..da4939c --- /dev/null +++ b/rabbitmq-server/plugins-src/mochiweb-wrapper/mochiweb-git/support/templates/mochiwebapp_skel/rebar.config @@ -0,0 +1,7 @@ +%% -*- erlang -*- +{erl_opts, [debug_info]}. +{deps, [ + {mochiweb, ".*", + {git, "git://github.com/mochi/mochiweb.git", {branch, "master"}}}]}. +{cover_enabled, true}. +{eunit_opts, [verbose, {report,{eunit_surefire,[{dir,"."}]}}]}. diff --git a/rabbitmq-server/plugins-src/mochiweb-wrapper/mochiweb-git/support/templates/mochiwebapp_skel/src/mochiapp.app.src b/rabbitmq-server/plugins-src/mochiweb-wrapper/mochiweb-git/support/templates/mochiwebapp_skel/src/mochiapp.app.src new file mode 100644 index 0000000..c0bb11f --- /dev/null +++ b/rabbitmq-server/plugins-src/mochiweb-wrapper/mochiweb-git/support/templates/mochiwebapp_skel/src/mochiapp.app.src @@ -0,0 +1,9 @@ +%% -*- erlang -*- +{application, {{appid}}, + [{description, "{{appid}}"}, + {vsn, "{{version}}"}, + {modules, []}, + {registered, []}, + {mod, {'{{appid}}_app', []}}, + {env, []}, + {applications, [kernel, stdlib, crypto]}]}. diff --git a/rabbitmq-server/plugins-src/mochiweb-wrapper/mochiweb-git/support/templates/mochiwebapp_skel/src/mochiapp.erl b/rabbitmq-server/plugins-src/mochiweb-wrapper/mochiweb-git/support/templates/mochiwebapp_skel/src/mochiapp.erl new file mode 100644 index 0000000..9770f2c --- /dev/null +++ b/rabbitmq-server/plugins-src/mochiweb-wrapper/mochiweb-git/support/templates/mochiwebapp_skel/src/mochiapp.erl @@ -0,0 +1,30 @@ +%% @author {{author}} +%% @copyright {{year}} {{author}} + +%% @doc {{appid}}. + +-module({{appid}}). +-author("{{author}}"). +-export([start/0, stop/0]). + +ensure_started(App) -> + case application:start(App) of + ok -> + ok; + {error, {already_started, App}} -> + ok + end. + + +%% @spec start() -> ok +%% @doc Start the {{appid}} server. +start() -> + {{appid}}_deps:ensure(), + ensure_started(crypto), + application:start({{appid}}). + + +%% @spec stop() -> ok +%% @doc Stop the {{appid}} server. +stop() -> + application:stop({{appid}}). diff --git a/rabbitmq-server/plugins-src/mochiweb-wrapper/mochiweb-git/support/templates/mochiwebapp_skel/src/mochiapp_app.erl b/rabbitmq-server/plugins-src/mochiweb-wrapper/mochiweb-git/support/templates/mochiwebapp_skel/src/mochiapp_app.erl new file mode 100644 index 0000000..6bbb255 --- /dev/null +++ b/rabbitmq-server/plugins-src/mochiweb-wrapper/mochiweb-git/support/templates/mochiwebapp_skel/src/mochiapp_app.erl @@ -0,0 +1,22 @@ +%% @author {{author}} +%% @copyright {{appid}} {{author}} + +%% @doc Callbacks for the {{appid}} application. + +-module({{appid}}_app). +-author("{{author}}"). + +-behaviour(application). +-export([start/2,stop/1]). + + +%% @spec start(_Type, _StartArgs) -> ServerRet +%% @doc application start callback for {{appid}}. +start(_Type, _StartArgs) -> + {{appid}}_deps:ensure(), + {{appid}}_sup:start_link(). + +%% @spec stop(_State) -> ServerRet +%% @doc application stop callback for {{appid}}. +stop(_State) -> + ok. diff --git a/rabbitmq-server/plugins-src/mochiweb-wrapper/mochiweb-git/support/templates/mochiwebapp_skel/src/mochiapp_deps.erl b/rabbitmq-server/plugins-src/mochiweb-wrapper/mochiweb-git/support/templates/mochiwebapp_skel/src/mochiapp_deps.erl new file mode 100644 index 0000000..ad151bc --- /dev/null +++ b/rabbitmq-server/plugins-src/mochiweb-wrapper/mochiweb-git/support/templates/mochiwebapp_skel/src/mochiapp_deps.erl @@ -0,0 +1,84 @@ +%% @author {{author}} +%% @copyright {{year}} {{author}} + +%% @doc Ensure that the relatively-installed dependencies are on the code +%% loading path, and locate resources relative +%% to this application's path. + +-module({{appid}}_deps). +-author("{{author}}"). + +-export([ensure/0, ensure/1]). +-export([get_base_dir/0, get_base_dir/1]). +-export([local_path/1, local_path/2]). +-export([deps_on_path/0, new_siblings/1]). + +%% @spec deps_on_path() -> [ProjNameAndVers] +%% @doc List of project dependencies on the path. +deps_on_path() -> + F = fun (X, Acc) -> + ProjDir = filename:dirname(X), + case {filename:basename(X), + filename:basename(filename:dirname(ProjDir))} of + {"ebin", "deps"} -> + [filename:basename(ProjDir) | Acc]; + _ -> + Acc + end + end, + ordsets:from_list(lists:foldl(F, [], code:get_path())). + +%% @spec new_siblings(Module) -> [Dir] +%% @doc Find new siblings paths relative to Module that aren't already on the +%% code path. +new_siblings(Module) -> + Existing = deps_on_path(), + SiblingEbin = filelib:wildcard(local_path(["deps", "*", "ebin"], Module)), + Siblings = [filename:dirname(X) || X <- SiblingEbin, + ordsets:is_element( + filename:basename(filename:dirname(X)), + Existing) =:= false], + lists:filter(fun filelib:is_dir/1, + lists:append([[filename:join([X, "ebin"]), + filename:join([X, "include"])] || + X <- Siblings])). + + +%% @spec ensure(Module) -> ok +%% @doc Ensure that all ebin and include paths for dependencies +%% of the application for Module are on the code path. +ensure(Module) -> + code:add_paths(new_siblings(Module)), + code:clash(), + ok. + +%% @spec ensure() -> ok +%% @doc Ensure that the ebin and include paths for dependencies of +%% this application are on the code path. Equivalent to +%% ensure(?Module). +ensure() -> + ensure(?MODULE). + +%% @spec get_base_dir(Module) -> string() +%% @doc Return the application directory for Module. It assumes Module is in +%% a standard OTP layout application in the ebin or src directory. +get_base_dir(Module) -> + {file, Here} = code:is_loaded(Module), + filename:dirname(filename:dirname(Here)). + +%% @spec get_base_dir() -> string() +%% @doc Return the application directory for this application. Equivalent to +%% get_base_dir(?MODULE). +get_base_dir() -> + get_base_dir(?MODULE). + +%% @spec local_path([string()], Module) -> string() +%% @doc Return an application-relative directory from Module's application. +local_path(Components, Module) -> + filename:join([get_base_dir(Module) | Components]). + +%% @spec local_path(Components) -> string() +%% @doc Return an application-relative directory for this application. +%% Equivalent to local_path(Components, ?MODULE). +local_path(Components) -> + local_path(Components, ?MODULE). diff --git a/rabbitmq-server/plugins-src/mochiweb-wrapper/mochiweb-git/support/templates/mochiwebapp_skel/src/mochiapp_sup.erl b/rabbitmq-server/plugins-src/mochiweb-wrapper/mochiweb-git/support/templates/mochiwebapp_skel/src/mochiapp_sup.erl new file mode 100644 index 0000000..39db395 --- /dev/null +++ b/rabbitmq-server/plugins-src/mochiweb-wrapper/mochiweb-git/support/templates/mochiwebapp_skel/src/mochiapp_sup.erl @@ -0,0 +1,56 @@ +%% @author {{author}} +%% @copyright {{year}} {{author}} + +%% @doc Supervisor for the {{appid}} application. + +-module({{appid}}_sup). +-author("{{author}}"). + +-behaviour(supervisor). + +%% External exports +-export([start_link/0, upgrade/0]). + +%% supervisor callbacks +-export([init/1]). + +%% @spec start_link() -> ServerRet +%% @doc API for starting the supervisor. +start_link() -> + supervisor:start_link({local, ?MODULE}, ?MODULE, []). + +%% @spec upgrade() -> ok +%% @doc Add processes if necessary. +upgrade() -> + {ok, {_, Specs}} = init([]), + + Old = sets:from_list( + [Name || {Name, _, _, _} <- supervisor:which_children(?MODULE)]), + New = sets:from_list([Name || {Name, _, _, _, _, _} <- Specs]), + Kill = sets:subtract(Old, New), + + sets:fold(fun (Id, ok) -> + supervisor:terminate_child(?MODULE, Id), + supervisor:delete_child(?MODULE, Id), + ok + end, ok, Kill), + + [supervisor:start_child(?MODULE, Spec) || Spec <- Specs], + ok. + +%% @spec init([]) -> SupervisorTree +%% @doc supervisor callback. +init([]) -> + Web = web_specs({{appid}}_web, {{port}}), + Processes = [Web], + Strategy = {one_for_one, 10, 10}, + {ok, + {Strategy, lists:flatten(Processes)}}. + +web_specs(Mod, Port) -> + WebConfig = [{ip, {0,0,0,0}}, + {port, Port}, + {docroot, {{appid}}_deps:local_path(["priv", "www"])}], + {Mod, + {Mod, start, [WebConfig]}, + permanent, 5000, worker, dynamic}. diff --git a/rabbitmq-server/plugins-src/mochiweb-wrapper/mochiweb-git/support/templates/mochiwebapp_skel/src/mochiapp_web.erl b/rabbitmq-server/plugins-src/mochiweb-wrapper/mochiweb-git/support/templates/mochiwebapp_skel/src/mochiapp_web.erl new file mode 100644 index 0000000..8976265 --- /dev/null +++ b/rabbitmq-server/plugins-src/mochiweb-wrapper/mochiweb-git/support/templates/mochiwebapp_skel/src/mochiapp_web.erl @@ -0,0 +1,69 @@ +%% @author {{author}} +%% @copyright {{year}} {{author}} + +%% @doc Web server for {{appid}}. + +-module({{appid}}_web). +-author("{{author}}"). + +-export([start/1, stop/0, loop/2]). + +%% External API + +start(Options) -> + {DocRoot, Options1} = get_option(docroot, Options), + Loop = fun (Req) -> + ?MODULE:loop(Req, DocRoot) + end, + mochiweb_http:start([{name, ?MODULE}, {loop, Loop} | Options1]). + +stop() -> + mochiweb_http:stop(?MODULE). + +loop(Req, DocRoot) -> + "/" ++ Path = Req:get(path), + try + case Req:get(method) of + Method when Method =:= 'GET'; Method =:= 'HEAD' -> + case Path of + _ -> + Req:serve_file(Path, DocRoot) + end; + 'POST' -> + case Path of + _ -> + Req:not_found() + end; + _ -> + Req:respond({501, [], []}) + end + catch + Type:What -> + Report = ["web request failed", + {path, Path}, + {type, Type}, {what, What}, + {trace, erlang:get_stacktrace()}], + error_logger:error_report(Report), + %% NOTE: mustache templates need \\ because they are not awesome. + Req:respond({500, [{"Content-Type", "text/plain"}], + "request failed, sorry\\n"}) + end. + +%% Internal API + +get_option(Option, Options) -> + {proplists:get_value(Option, Options), proplists:delete(Option, Options)}. + +%% +%% Tests +%% +-ifdef(TEST). +-include_lib("eunit/include/eunit.hrl"). + +you_should_write_a_test() -> + ?assertEqual( + "No, but I will!", + "Have you written any tests?"), + ok. + +-endif. diff --git a/rabbitmq-server/plugins-src/mochiweb-wrapper/mochiweb-git/support/templates/mochiwebapp_skel/start-dev.sh b/rabbitmq-server/plugins-src/mochiweb-wrapper/mochiweb-git/support/templates/mochiwebapp_skel/start-dev.sh new file mode 100755 index 0000000..fb7c45e --- /dev/null +++ b/rabbitmq-server/plugins-src/mochiweb-wrapper/mochiweb-git/support/templates/mochiwebapp_skel/start-dev.sh @@ -0,0 +1,6 @@ +#!/bin/sh +# NOTE: mustache templates need \\ because they are not awesome. +exec erl -pa ebin edit deps/*/ebin -boot start_sasl \\ + -sname {{appid}}_dev \\ + -s {{appid}} \\ + -s reloader diff --git a/rabbitmq-server/plugins-src/mochiweb-wrapper/mochiweb-git/support/test-materials/test_ssl_cert.pem b/rabbitmq-server/plugins-src/mochiweb-wrapper/mochiweb-git/support/test-materials/test_ssl_cert.pem new file mode 100644 index 0000000..f84ccca --- /dev/null +++ b/rabbitmq-server/plugins-src/mochiweb-wrapper/mochiweb-git/support/test-materials/test_ssl_cert.pem @@ -0,0 +1,19 @@ +-----BEGIN CERTIFICATE----- +MIIDIDCCAgigAwIBAgIJAJLkNZzERPIUMA0GCSqGSIb3DQEBBQUAMBQxEjAQBgNV +BAMTCWxvY2FsaG9zdDAeFw0xMDAzMTgxOTM5MThaFw0yMDAzMTUxOTM5MThaMBQx +EjAQBgNVBAMTCWxvY2FsaG9zdDCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoC +ggEBAJeUCOZxbmtngF4S5lXckjSDLc+8C+XjMBYBPyy5eKdJY20AQ1s9/hhp3ulI +8pAvl+xVo4wQ+iBSvOzcy248Q+Xi6+zjceF7UNRgoYPgtJjKhdwcHV3mvFFrS/fp +9ggoAChaJQWDO1OCfUgTWXImhkw+vcDR11OVMAJ/h73dqzJPI9mfq44PTTHfYtgr +v4LAQAOlhXIAa2B+a6PlF6sqDqJaW5jLTcERjsBwnRhUGi7JevQzkejujX/vdA+N +jRBjKH/KLU5h3Q7wUchvIez0PXWVTCnZjpA9aR4m7YV05nKQfxtGd71czYDYk+j8 +hd005jetT4ir7JkAWValBybJVksCAwEAAaN1MHMwHQYDVR0OBBYEFJl9s51SnjJt +V/wgKWqV5Q6jnv1ZMEQGA1UdIwQ9MDuAFJl9s51SnjJtV/wgKWqV5Q6jnv1ZoRik +FjAUMRIwEAYDVQQDEwlsb2NhbGhvc3SCCQCS5DWcxETyFDAMBgNVHRMEBTADAQH/ +MA0GCSqGSIb3DQEBBQUAA4IBAQB2ldLeLCc+lxK5i0EZquLamMBJwDIjGpT0JMP9 +b4XQOK2JABIu54BQIZhwcjk3FDJz/uOW5vm8k1kYni8FCjNZAaRZzCUfiUYTbTKL +Rq9LuIAODyP2dnTqyKaQOOJHvrx9MRZ3XVecXPS0Tib4aO57vCaAbIkmhtYpTWmw +e3t8CAIDVtgvjR6Se0a1JA4LktR7hBu22tDImvCSJn1nVAaHpani6iPBPPdMuMsP +TBoeQfj8VpqBUjCStqJGa8ytjDFX73YaxV2mgrtGwPNme1x3YNRR11yTu7tksyMO +GrmgxNriqYRchBhNEf72AKF0LR1ByKwfbDB9rIsV00HtCgOp +-----END CERTIFICATE----- diff --git a/rabbitmq-server/plugins-src/mochiweb-wrapper/mochiweb-git/support/test-materials/test_ssl_key.pem b/rabbitmq-server/plugins-src/mochiweb-wrapper/mochiweb-git/support/test-materials/test_ssl_key.pem new file mode 100644 index 0000000..69bbf82 --- /dev/null +++ b/rabbitmq-server/plugins-src/mochiweb-wrapper/mochiweb-git/support/test-materials/test_ssl_key.pem @@ -0,0 +1,27 @@ +-----BEGIN RSA PRIVATE KEY----- +MIIEpAIBAAKCAQEAl5QI5nFua2eAXhLmVdySNIMtz7wL5eMwFgE/LLl4p0ljbQBD +Wz3+GGne6UjykC+X7FWjjBD6IFK87NzLbjxD5eLr7ONx4XtQ1GChg+C0mMqF3Bwd +Xea8UWtL9+n2CCgAKFolBYM7U4J9SBNZciaGTD69wNHXU5UwAn+Hvd2rMk8j2Z+r +jg9NMd9i2Cu/gsBAA6WFcgBrYH5ro+UXqyoOolpbmMtNwRGOwHCdGFQaLsl69DOR +6O6Nf+90D42NEGMof8otTmHdDvBRyG8h7PQ9dZVMKdmOkD1pHibthXTmcpB/G0Z3 +vVzNgNiT6PyF3TTmN61PiKvsmQBZVqUHJslWSwIDAQABAoIBACI8Ky5xHDFh9RpK +Rn/KC7OUlTpADKflgizWJ0Cgu2F9L9mkn5HyFHvLHa+u7CootbWJOiEejH/UcBtH +WyMQtX0snYCpdkUpJv5wvMoebGu+AjHOn8tfm9T/2O6rhwgckLyMb6QpGbMo28b1 +p9QiY17BJPZx7qJQJcHKsAvwDwSThlb7MFmWf42LYWlzybpeYQvwpd+UY4I0WXLu +/dqJIS9Npq+5Y5vbo2kAEAssb2hSCvhCfHmwFdKmBzlvgOn4qxgZ1iHQgfKI6Z3Y +J0573ZgOVTuacn+lewtdg5AaHFcl/zIYEr9SNqRoPNGbPliuv6k6N2EYcufWL5lR +sCmmmHECgYEAxm+7OpepGr++K3+O1e1MUhD7vSPkKJrCzNtUxbOi2NWj3FFUSPRU +adWhuxvUnZgTcgM1+KuQ0fB2VmxXe9IDcrSFS7PKFGtd2kMs/5mBw4UgDZkOQh+q +kDiBEV3HYYJWRq0w3NQ/9Iy1jxxdENHtGmG9aqamHxNtuO608wGW2S8CgYEAw4yG +ZyAic0Q/U9V2OHI0MLxLCzuQz17C2wRT1+hBywNZuil5YeTuIt2I46jro6mJmWI2 +fH4S/geSZzg2RNOIZ28+aK79ab2jWBmMnvFCvaru+odAuser4N9pfAlHZvY0pT+S +1zYX3f44ygiio+oosabLC5nWI0zB2gG8pwaJlaUCgYEAgr7poRB+ZlaCCY0RYtjo +mYYBKD02vp5BzdKSB3V1zeLuBWM84pjB6b3Nw0fyDig+X7fH3uHEGN+USRs3hSj6 +BqD01s1OT6fyfbYXNw5A1r+nP+5h26Wbr0zblcKxdQj4qbbBZC8hOJNhqTqqA0Qe +MmzF7jiBaiZV/Cyj4x1f9BcCgYEAhjL6SeuTuOctTqs/5pz5lDikh6DpUGcH8qaV +o6aRAHHcMhYkZzpk8yh1uUdD7516APmVyvn6rrsjjhLVq4ZAJjwB6HWvE9JBN0TR +bILF+sREHUqU8Zn2Ku0nxyfXCKIOnxlx/J/y4TaGYqBqfXNFWiXNUrjQbIlQv/xR +K48g/MECgYBZdQlYbMSDmfPCC5cxkdjrkmAl0EgV051PWAi4wR+hLxIMRjHBvAk7 +IweobkFvT4TICulgroLkYcSa5eOZGxB/DHqcQCbWj3reFV0VpzmTDoFKG54sqBRl +vVntGt0pfA40fF17VoS7riAdHF53ippTtsovHEsg5tq5NrBl5uKm2g== +-----END RSA PRIVATE KEY----- diff --git a/rabbitmq-server/plugins-src/mochiweb-wrapper/mochiweb-git/test/mochiweb_base64url_tests.erl b/rabbitmq-server/plugins-src/mochiweb-wrapper/mochiweb-git/test/mochiweb_base64url_tests.erl new file mode 100644 index 0000000..69f276a --- /dev/null +++ b/rabbitmq-server/plugins-src/mochiweb-wrapper/mochiweb-git/test/mochiweb_base64url_tests.erl @@ -0,0 +1,27 @@ +-module(mochiweb_base64url_tests). +-include_lib("eunit/include/eunit.hrl"). + +id(X) -> + ?assertEqual( + X, + mochiweb_base64url:decode(mochiweb_base64url:encode(X))), + ?assertEqual( + X, + mochiweb_base64url:decode( + binary_to_list(mochiweb_base64url:encode(binary_to_list(X))))). + +random_binary(Short,Long) -> + << <<(random:uniform(256) - 1)>> + || _ <- lists:seq(1, Short + random:uniform(1 + Long - Short) - 1) >>. + +empty_test() -> + id(<<>>). + +onechar_test() -> + [id(<>) || C <- lists:seq(0,255)], + ok. + +nchar_test() -> + %% 1000 tests of 2-6 char strings + [id(B) || _ <- lists:seq(1,1000), B <- [random_binary(2, 6)]], + ok. diff --git a/rabbitmq-server/plugins-src/mochiweb-wrapper/mochiweb-git/test/mochiweb_html_tests.erl b/rabbitmq-server/plugins-src/mochiweb-wrapper/mochiweb-git/test/mochiweb_html_tests.erl new file mode 100644 index 0000000..3d35400 --- /dev/null +++ b/rabbitmq-server/plugins-src/mochiweb-wrapper/mochiweb-git/test/mochiweb_html_tests.erl @@ -0,0 +1,589 @@ +-module(mochiweb_html_tests). +-include_lib("eunit/include/eunit.hrl"). + +to_html_test() -> + ?assertEqual( + <<"hey!

what's up

sucka
RAW!">>, + iolist_to_binary( + mochiweb_html:to_html({html, [], + [{<<"head">>, [], + [{title, <<"hey!">>}]}, + {body, [], + [{p, [{class, foo}], [<<"what's">>, <<" up">>, {br}]}, + {'div', <<"sucka">>}, + {'=', <<"RAW!">>}, + {comment, <<" comment! ">>}]}]}))), + ?assertEqual( + <<"">>, + iolist_to_binary( + mochiweb_html:to_html({doctype, + [<<"html">>, <<"PUBLIC">>, + <<"-//W3C//DTD XHTML 1.0 Transitional//EN">>, + <<"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">>]}))), + ?assertEqual( + <<"">>, + iolist_to_binary( + mochiweb_html:to_html({<<"html">>,[], + [{pi, <<"xml:namespace">>, + [{<<"prefix">>,<<"o">>}, + {<<"ns">>,<<"urn:schemas-microsoft-com:office:office">>}]}]}))), + ok. + +escape_test() -> + ?assertEqual( + <<"&quot;\"word ><<up!&quot;">>, + mochiweb_html:escape(<<""\"word ><>)), + ?assertEqual( + <<"&quot;\"word ><<up!&quot;">>, + mochiweb_html:escape(""\"word ><>, + mochiweb_html:escape('"\"word >< + ?assertEqual( + <<"&quot;"word ><<up!&quot;">>, + mochiweb_html:escape_attr(<<""\"word ><>)), + ?assertEqual( + <<"&quot;"word ><<up!&quot;">>, + mochiweb_html:escape_attr(""\"word ><>, + mochiweb_html:escape_attr('"\"word ><>, + mochiweb_html:escape_attr(12345)), + ?assertEqual( + <<"1.5">>, + mochiweb_html:escape_attr(1.5)), + ok. + +tokens_test() -> + ?assertEqual( + [{start_tag, <<"foo">>, [{<<"bar">>, <<"baz">>}, + {<<"wibble">>, <<"wibble">>}, + {<<"alice">>, <<"bob">>}], true}], + mochiweb_html:tokens(<<"">>)), + ?assertEqual( + [{start_tag, <<"foo">>, [{<<"bar">>, <<"baz">>}, + {<<"wibble">>, <<"wibble">>}, + {<<"alice">>, <<"bob">>}], true}], + mochiweb_html:tokens(<<"">>)), + ?assertEqual( + [{comment, <<"[if lt IE 7]>\n\n>}], + mochiweb_html:tokens(<<"">>)), + ?assertEqual( + [{start_tag, <<"script">>, [{<<"type">>, <<"text/javascript">>}], false}, + {data, <<" A= B <= C ">>, false}, + {end_tag, <<"script">>}], + mochiweb_html:tokens(<<"">>)), + ?assertEqual( + [{start_tag, <<"script">>, [{<<"type">>, <<"text/javascript">>}], false}, + {data, <<" A= B <= C ">>, false}, + {end_tag, <<"script">>}], + mochiweb_html:tokens(<<"">>)), + ?assertEqual( + [{start_tag, <<"script">>, [{<<"type">>, <<"text/javascript">>}], false}, + {data, <<" A= B <= C ">>, false}, + {end_tag, <<"script">>}], + mochiweb_html:tokens(<<"">>)), + ?assertEqual( + [{start_tag, <<"script">>, [{<<"type">>, <<"text/javascript">>}], false}, + {data, <<" A= B <= C ">>, false}, + {end_tag, <<"script">>}], + mochiweb_html:tokens(<<"">>)), + ?assertEqual( + [{start_tag, <<"textarea">>, [], false}, + {data, <<"">>, false}, + {end_tag, <<"textarea">>}], + mochiweb_html:tokens(<<"">>)), + ?assertEqual( + [{start_tag, <<"textarea">>, [], false}, + {data, <<"">>, false}], + mochiweb_html:tokens(<<" + + + + + + diff --git a/rabbitmq-server/plugins-src/rabbitmq-management/priv/www/js/tmpl/queue.ejs b/rabbitmq-server/plugins-src/rabbitmq-management/priv/www/js/tmpl/queue.ejs new file mode 100644 index 0000000..46fcd44 --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-management/priv/www/js/tmpl/queue.ejs @@ -0,0 +1,308 @@ +

Queue <%= fmt_string(queue.name) %><%= fmt_maybe_vhost(queue.vhost) %>

+ +
+

Overview

+
+ <%= queue_lengths('lengths-q', queue) %> +<% if (rates_mode != 'none') { %> + <%= message_rates('msg-rates-q', queue.message_stats) %> +<% } %> + +

Details

+ + + + + + + + + + <% if (queue.owner_pid_details != undefined) { %> + + + + +<% } %> +<% if (nodes_interesting) { %> + + + + + <% if (queue.owner_pid_details == undefined) { %> + + + + + <% } %> +<% } %> +
Features<%= fmt_features(queue) %>
Policy<%= fmt_string(queue.policy, '') %>
Exclusive owner<%= link_conn(queue.owner_pid_details.name) %>
Node<%= fmt_node(queue.node) %>
Slaves + <% + var has_unsynced_node = false; + for (var i in queue.slave_nodes) { + var node = queue.slave_nodes[i]; + %> + <% + if (jQuery.inArray(node, queue.synchronised_slave_nodes) == -1) { + has_unsynced_node = true; + %> + <%= fmt_node(node) %> (unsynchronised) + <% } else { %> + <%= fmt_node(node) %> + <% } %> +
+ <% } %> + <% if (queue.state == 'syncing') { %> + + + + + +
+ <%= fmt_sync_state(queue) %> + +
+ + + + +
+
+ <% } else if (has_unsynced_node) { %> +
+ + + + +
+ <% } %> +
+ + + + + + + + + + + + + + +
State<%= fmt_object_state(queue) %>
Consumers<%= fmt_string(queue.consumers) %>
Consumer utilisation <%= fmt_percent(queue.consumer_utilisation) %>
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
TotalReadyUnackedIn memoryPersistent
+ Messages + + + <%= fmt_num_thousands(queue.messages) %> + + <%= fmt_num_thousands(queue.messages_ready) %> + + <%= fmt_num_thousands(queue.messages_unacknowledged) %> + + <%= fmt_num_thousands(queue.messages_ram) %> + + <%= fmt_num_thousands(queue.messages_persistent) %> +
+ Message body bytes + + + <%= fmt_bytes(queue.message_bytes) %> + + <%= fmt_bytes(queue.message_bytes_ready) %> + + <%= fmt_bytes(queue.message_bytes_unacknowledged) %> + + <%= fmt_bytes(queue.message_bytes_ram) %> + + <%= fmt_bytes(queue.message_bytes_persistent) %> +
+ Process memory + + <%= fmt_bytes(queue.memory) %>
+
+
+ +<% if (rates_mode == 'detailed') { %> +
+

Message rates breakdown

+
+ + + + + +
+ <%= format('msg-detail-publishes', + {'mode': 'queue', + 'object': queue.incoming, + 'label': 'Incoming'}) %> + + + <%= format('msg-detail-deliveries', + {'mode': 'queue', + 'object': queue.deliveries}) %> +
+
+
+<% } %> + +
+

Consumers

+
+<%= format('consumers', {'mode': 'queue', 'consumers': queue.consumer_details}) %> +
+
+ +
+

Bindings

+
+
+ <%= format('bindings', {'mode': 'queue', 'bindings': bindings}) %> +

+

This queue

+ + <%= format('add-binding', {'mode': 'queue', 'parent': queue}) %> +
+
+
+ +<%= format('publish', {'mode': 'queue', 'queue': queue}) %> + +
+

Get messages

+
+

+ Warning: getting messages from a queue is a destructive action. + +

+
+ + + + + + + + + + + + + + + + +
+ +
+ + +
+ +
+
+
+
+ +<% if (user_policymaker) { %> +
+

Move messages

+
+ <% if (NAVIGATION['Admin'][0]['Shovel Management'] == undefined) { %> +

To move messages, the shovel plugin must be enabled, try:

+
$ rabbitmq-plugins enable rabbitmq_shovel rabbitmq_shovel_management
+ <% } else { %> +

+ The shovel plugin can be used to move messages from this queue + to another one. The form below will create a temporary shovel to + move messages to another queue on the same virtual host, with + default settings. +

+

+ For more options see the shovel + interface. +

+
+ + + + + + + + + + + + + + + + + + +
Destination queue:
+ +
+ <% } %> +
+
+<% } %> + +
+

Delete / purge

+
+
+ + + + +
+ +
+ + + + +
+
+
diff --git a/rabbitmq-server/plugins-src/rabbitmq-management/priv/www/js/tmpl/queues.ejs b/rabbitmq-server/plugins-src/rabbitmq-management/priv/www/js/tmpl/queues.ejs new file mode 100644 index 0000000..0c3fed8 --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-management/priv/www/js/tmpl/queues.ejs @@ -0,0 +1,266 @@ +

Queues

+
+

All queues

+
+<%= filter_ui(queues) %> +
+<% if (queues.length > 0) { %> + + + + <%= group_heading('queues', 'Overview', [vhosts_interesting, nodes_interesting, true]) %> + <%= group_heading('queues', 'Messages', []) %> + <%= group_heading('queues', 'Message bytes', []) %> +<% if (rates_mode != 'none') { %> + <%= group_heading('queues', 'Message rates', []) %> +<% } %> + + + +<% if (vhosts_interesting) { %> + +<% } %> + +<% if (nodes_interesting) { %> + +<% } %> +<% if (show_column('queues', 'features')) { %> + +<% } %> +<% if (show_column('queues', 'features_no_policy')) { %> + +<% } %> +<% if (show_column('queues', 'policy')) { %> + +<% } %> +<% if (show_column('queues', 'consumers')) { %> + +<% } %> +<% if (show_column('queues', 'consumer_utilisation')) { %> + +<% } %> +<% if (show_column('queues', 'state')) { %> + +<% } %> +<% if (show_column('queues', 'msgs-ready')) { %> + +<% } %> +<% if (show_column('queues', 'msgs-unacked')) { %> + +<% } %> +<% if (show_column('queues', 'msgs-ram')) { %> + +<% } %> +<% if (show_column('queues', 'msgs-persistent')) { %> + +<% } %> +<% if (show_column('queues', 'msgs-total')) { %> + +<% } %> +<% if (show_column('queues', 'msg-bytes-ready')) { %> + +<% } %> +<% if (show_column('queues', 'msg-bytes-unacked')) { %> + +<% } %> +<% if (show_column('queues', 'msg-bytes-ram')) { %> + +<% } %> +<% if (show_column('queues', 'msg-bytes-persistent')) { %> + +<% } %> +<% if (show_column('queues', 'msg-bytes-total')) { %> + +<% } %> +<% if (rates_mode != 'none') { %> + <% if (show_column('queues', 'rate-incoming')) { %> + + <% } %> + <% if (show_column('queues', 'rate-deliver')) { %> + + <% } %> + <% if (show_column('queues', 'rate-redeliver')) { %> + + <% } %> + <% if (show_column('queues', 'rate-ack')) { %> + + <% } %> +<% } %> + + + +<% + for (var i = 0; i < queues.length; i++) { + var queue = queues[i]; +%> + > +<% if (vhosts_interesting) { %> + +<% } %> + +<% if (nodes_interesting) { %> + +<% } %> +<% if (show_column('queues', 'features')) { %> + +<% } %> +<% if (show_column('queues', 'features_no_policy')) { %> + +<% } %> +<% if (show_column('queues', 'policy')) { %> + +<% } %> +<% if (show_column('queues', 'consumers')) { %> + +<% } %> +<% if (show_column('queues', 'consumer_utilisation')) { %> + +<% } %> +<% if (show_column('queues', 'state')) { %> + +<% } %> +<% if (show_column('queues', 'msgs-ready')) { %> + +<% } %> +<% if (show_column('queues', 'msgs-unacked')) { %> + +<% } %> +<% if (show_column('queues', 'msgs-ram')) { %> + +<% } %> +<% if (show_column('queues', 'msgs-persistent')) { %> + +<% } %> +<% if (show_column('queues', 'msgs-total')) { %> + +<% } %> +<% if (show_column('queues', 'msg-bytes-ready')) { %> + +<% } %> +<% if (show_column('queues', 'msg-bytes-unacked')) { %> + +<% } %> +<% if (show_column('queues', 'msg-bytes-ram')) { %> + +<% } %> +<% if (show_column('queues', 'msg-bytes-persistent')) { %> + +<% } %> +<% if (show_column('queues', 'msg-bytes-total')) { %> + +<% } %> +<% if (rates_mode != 'none') { %> + <% if (show_column('queues', 'rate-incoming')) { %> + + <% } %> + <% if (show_column('queues', 'rate-deliver')) { %> + + <% } %> + <% if (show_column('queues', 'rate-redeliver')) { %> + + <% } %> + <% if (show_column('queues', 'rate-ack')) { %> + + <% } %> +<% } %> + + <% } %> + +
+/-
<%= fmt_sort('Virtual host', 'vhost') %><%= fmt_sort('Name', 'name') %><%= fmt_sort('Node', 'node') %>FeaturesFeatures<%= fmt_sort('Policy','policy') %><%= fmt_sort('Consumers', 'consumers') %><%= fmt_sort('Consumer utilisation', 'consumer_utilisation') %><%= fmt_sort('State', 'state') %><%= fmt_sort('Ready', 'messages_ready') %><%= fmt_sort('Unacked', 'messages_unacknowledged') %><%= fmt_sort('In Memory', 'messages_ram') %><%= fmt_sort('Persistent', 'messages_persistent') %><%= fmt_sort('Total', 'messages') %><%= fmt_sort('Ready', 'message_bytes_ready') %><%= fmt_sort('Unacked', 'message_bytes_unacknowledged') %><%= fmt_sort('In Memory', 'message_bytes_ram') %><%= fmt_sort('Persistent', 'message_bytes_persistent') %><%= fmt_sort('Total', 'message_bytes') %><%= fmt_sort('incoming', 'message_stats.publish_details.rate') %><%= fmt_sort('deliver / get', 'message_stats.deliver_get_details.rate') %><%= fmt_sort('redelivered', 'message_stats.redeliver_details.rate') %><%= fmt_sort('ack', 'message_stats.ack_details.rate') %>
<%= fmt_string(queue.vhost) %><%= link_queue(queue.vhost, queue.name, queue.arguments) %> + <%= fmt_node(queue.node) %> + <%= fmt_mirrors(queue) %> + <% if (queue.state == 'syncing') { %> + <%= fmt_sync_state(queue) %> + <% } %> + + <%= fmt_features_short(queue) %> + <%= fmt_policy_short(queue) %> + <%= fmt_features_short(queue) %><%= fmt_string(queue.policy) %><%= fmt_string(queue.consumers) %><%= fmt_percent(queue.consumer_utilisation) %><%= fmt_object_state(queue) %><%= fmt_num_thousands(queue.messages_ready) %><%= fmt_num_thousands(queue.messages_unacknowledged) %><%= fmt_num_thousands(queue.messages_ram) %><%= fmt_num_thousands(queue.messages_persistent) %><%= fmt_num_thousands(queue.messages) %><%= fmt_bytes(queue.message_bytes_ready) %><%= fmt_bytes(queue.message_bytes_unacknowledged) %><%= fmt_bytes(queue.message_bytes_ram) %><%= fmt_bytes(queue.message_bytes_persistent) %><%= fmt_bytes(queue.message_bytes) %><%= fmt_detail_rate(queue.message_stats, 'publish') %><%= fmt_detail_rate(queue.message_stats, 'deliver_get') %><%= fmt_detail_rate(queue.message_stats, 'redeliver') %><%= fmt_detail_rate(queue.message_stats, 'ack') %>
+<% } else { %> +

... no queues ...

+<% } %> +
+
+
+ +
+

Add a new queue

+
+
+ +<% if (vhosts_interesting) { %> + + + + +<% } else { %> + +<% } %> + + + + + + + + +<% + if (nodes_interesting) { + var nodes = JSON.parse(sync_get('/nodes')); +%> + + + + +<% } %> + + + + + + + + +
+ +
*
+ +
+ +
+ +
+
+ + + + + + +
+ +
+
+
diff --git a/rabbitmq-server/plugins-src/rabbitmq-management/priv/www/js/tmpl/rate-options.ejs b/rabbitmq-server/plugins-src/rabbitmq-management/priv/www/js/tmpl/rate-options.ejs new file mode 100644 index 0000000..da15b0e --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-management/priv/www/js/tmpl/rate-options.ejs @@ -0,0 +1,52 @@ +<% + var id = span.attr('for'); + var mode = get_pref('rate-mode-' + id); + var size = get_pref('chart-size-' + id); + var range = get_pref('chart-range'); +%> + +
+ + + + + + + + + + + + + + + + + + + + +
+

This time series

+
+ <%= fmt_radio('mode', 'Chart', 'chart', mode) %> + <%= fmt_radio('mode', 'Current value', 'curr', mode) %> + <% if (id != 'node-stats') { %> + <%= fmt_radio('mode', 'Moving average', 'avg', mode) %> + <% } %> +
+ <%= fmt_radio('size', 'Small', 'small', size) %> + <%= fmt_radio('size', 'Medium', 'medium', size) %> + <%= fmt_radio('size', 'Large', 'large', size) %> +
+

All time series

+
+<% + for (p in CHART_PERIODS) { +%> + <%= fmt_radio('range', CHART_PERIODS[p], p, range) %> +<% + } +%> +
+
diff --git a/rabbitmq-server/plugins-src/rabbitmq-management/priv/www/js/tmpl/registry.ejs b/rabbitmq-server/plugins-src/rabbitmq-management/priv/www/js/tmpl/registry.ejs new file mode 100644 index 0000000..38d98e1 --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-management/priv/www/js/tmpl/registry.ejs @@ -0,0 +1,25 @@ +<% if (node.running) { %> + + + + +<% if (show_enabled) { %> + +<% } %> + + <% + for (var i = 0; i < list.length; i++) { + var item = list[i]; + %> + > + + +<% if (show_enabled) { %> + +<% } %> + + <% } %> +
NameDescriptionEnabled
<%= fmt_string(item.name) %><%= fmt_string(item.description) %><%= fmt_boolean(item.enabled) %>
+<% } else {%> +

...node not running...

+<% } %> diff --git a/rabbitmq-server/plugins-src/rabbitmq-management/priv/www/js/tmpl/status.ejs b/rabbitmq-server/plugins-src/rabbitmq-management/priv/www/js/tmpl/status.ejs new file mode 100644 index 0000000..74c2a95 --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-management/priv/www/js/tmpl/status.ejs @@ -0,0 +1 @@ +

<%= text %>

diff --git a/rabbitmq-server/plugins-src/rabbitmq-management/priv/www/js/tmpl/user.ejs b/rabbitmq-server/plugins-src/rabbitmq-management/priv/www/js/tmpl/user.ejs new file mode 100644 index 0000000..0201744 --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-management/priv/www/js/tmpl/user.ejs @@ -0,0 +1,85 @@ +

User: <%= fmt_string(user.name) %>

+ +<% if (permissions.length == 0) { %> +

+ This user does not have permission to access any virtual hosts.
+ Use "Set Permission" below to grant permission to access virtual hosts. +

+<% } %> + +
+

Overview

+
+ + + + + + + + + +
Tags<%= fmt_string(user.tags) %>
Can log in with password<%= fmt_boolean(user.password_hash.length > 0) %>
+
+
+ +<%= format('permissions', {'mode': 'user', 'permissions': permissions, 'vhosts': vhosts, 'parent': user}) %> + +
+

Update this user

+
+
+ + + + + + + + + + +
+ + +
+ + *
+ + * + (confirm) +
+ +
+ + + + [Admin] + [Monitoring] + [Policymaker] + [Management] + [None] + +
+ +
+
+
+ + +
+

Delete this user

+
+
+ + +
+
+
diff --git a/rabbitmq-server/plugins-src/rabbitmq-management/priv/www/js/tmpl/users.ejs b/rabbitmq-server/plugins-src/rabbitmq-management/priv/www/js/tmpl/users.ejs new file mode 100644 index 0000000..f0dfc4d --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-management/priv/www/js/tmpl/users.ejs @@ -0,0 +1,97 @@ +

Users

+
+

All users

+
+<%= filter_ui(users) %> +
+<% if (users.length > 0) { %> + + + + + + + + + + + <% + for (var i = 0; i < users.length; i++) { + var user = users[i]; + %> + > + + + + + + <% } %> + +
<%= fmt_sort('Name', 'name') %><%= fmt_sort('Tags', 'tags') %>Can access virtual hostsHas password
<%= link_user(user.name) %><%= fmt_string(user.tags) %><%= fmt_permissions(user, permissions, 'user', 'vhost', + '

No access

') %>
<%= fmt_boolean(user.password_hash.length > 0) %>
+<% } else { %> +

... no vhosts ...

+<% } %> +

+
+
+
+ +
+

Add a user

+
+
+ + + + + + + + + + + + + +
+ + * +
+ + +
+ + *
+ + * + (confirm) +
+ +
+ + + + + + + + +
+ +
+
+
diff --git a/rabbitmq-server/plugins-src/rabbitmq-management/priv/www/js/tmpl/vhost.ejs b/rabbitmq-server/plugins-src/rabbitmq-management/priv/www/js/tmpl/vhost.ejs new file mode 100644 index 0000000..fe45177 --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-management/priv/www/js/tmpl/vhost.ejs @@ -0,0 +1,38 @@ +

Virtual Host: <%= fmt_string(vhost.name) %>

+ +<% if (permissions.length == 0) { %> +

+ No users have permission to access this virtual host.
+ Use "Set Permission" below to grant users permission to access this virtual host. +

+<% } %> + +
+

Overview

+
+ <%= queue_lengths('lengths-vhost', vhost) %> +<% if (rates_mode != 'none') { %> + <%= message_rates('msg-rates-vhost', vhost.message_stats) %> +<% } %> + <%= data_rates('data-rates-vhost', vhost, 'Data rates') %> +

Details

+ + + + + +
Tracing enabled:<%= fmt_boolean(vhost.tracing) %>
+
+
+ +<%= format('permissions', {'mode': 'vhost', 'permissions': permissions, 'users': users, 'parent': vhost}) %> + +
+

Delete this vhost

+
+
+ + +
+
+
diff --git a/rabbitmq-server/plugins-src/rabbitmq-management/priv/www/js/tmpl/vhosts.ejs b/rabbitmq-server/plugins-src/rabbitmq-management/priv/www/js/tmpl/vhosts.ejs new file mode 100644 index 0000000..a6cbe9f --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-management/priv/www/js/tmpl/vhosts.ejs @@ -0,0 +1,104 @@ +

Virtual Hosts

+ +
+

All virtual hosts

+
+<%= filter_ui(vhosts) %> +
+<% if (vhosts.length > 0) { %> + + + + + <%= group_heading('vhosts', 'Messages', []) %> + <%= group_heading('vhosts', 'Network', []) %> +<% if (rates_mode != 'none') { %> + <%= group_heading('vhosts', 'Message rates', []) %> +<% } %> + + + + + +<% if (show_column('vhosts', 'msgs-ready')) { %> + +<% } %> +<% if (show_column('vhosts', 'msgs-unacked')) { %> + +<% } %> +<% if (show_column('vhosts', 'msgs-total')) { %> + +<% } %> +<% if (show_column('vhosts', 'from_client')) { %> + +<% } %> +<% if (show_column('vhosts', 'to_client')) { %> + +<% } %> +<% if (rates_mode != 'none') { %> + <% if (show_column('vhosts', 'rate-publish')) { %> + + <% } %> + <% if (show_column('vhosts', 'rate-deliver')) { %> + + <% } %> +<% } %> + + + + <% + for (var i = 0; i < vhosts.length; i++) { + var vhost = vhosts[i]; + %> + > + + +<% if (show_column('vhosts', 'msgs-ready')) { %> + +<% } %> +<% if (show_column('vhosts', 'msgs-unacked')) { %> + +<% } %> +<% if (show_column('vhosts', 'msgs-total')) { %> + +<% } %> +<% if (show_column('vhosts', 'from_client')) { %> + +<% } %> +<% if (show_column('vhosts', 'to_client')) { %> + +<% } %> +<% if (rates_mode != 'none') { %> + <% if (show_column('vhosts', 'rate-publish')) { %> + + <% } %> + <% if (show_column('vhosts', 'rate-deliver')) { %> + + <% } %> +<% } %> + + <% } %> + +
Overview+/-
<%= fmt_sort('Name', 'name') %>Users <%= fmt_sort('Ready', 'messages_ready') %><%= fmt_sort('Unacked', 'messages_unacknowledged') %><%= fmt_sort('Total', 'messages') %><%= fmt_sort('From client', 'recv_oct_details.rate') %><%= fmt_sort('To client', 'send_oct_details.rate') %><%= fmt_sort('publish', 'message_stats.publish_details.rate') %><%= fmt_sort('deliver / get','message_stats.deliver_get_details.rate') %>
<%= link_vhost(vhost.name) %><%= fmt_permissions(vhost, permissions, 'vhost', 'user', + '

No users

') %>
<%= fmt_num_thousands(vhost.messages_ready) %><%= fmt_num_thousands(vhost.messages_unacknowledged) %><%= fmt_num_thousands(vhost.messages) %><%= fmt_detail_rate_bytes(vhost, 'recv_oct') %><%= fmt_detail_rate_bytes(vhost, 'send_oct') %><%= fmt_detail_rate(vhost.message_stats, 'publish') %><%= fmt_detail_rate(vhost.message_stats, 'deliver_get') %>
+<% } else { %> +

... no vhosts ...

+<% } %> +
+
+
+ +
+

Add a new virtual host

+
+
+ + + + + +
*
+ +
+
+
diff --git a/rabbitmq-server/plugins-src/rabbitmq-management/src/rabbit_mgmt_app.erl b/rabbitmq-server/plugins-src/rabbitmq-management/src/rabbit_mgmt_app.erl new file mode 100644 index 0000000..b5f4b6b --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-management/src/rabbit_mgmt_app.erl @@ -0,0 +1,114 @@ +%% The contents of this file are subject to the Mozilla Public License +%% Version 1.1 (the "License"); you may not use this file except in +%% compliance with the License. You may obtain a copy of the License at +%% http://www.mozilla.org/MPL/ +%% +%% Software distributed under the License is distributed on an "AS IS" +%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the +%% License for the specific language governing rights and limitations +%% under the License. +%% +%% The Original Code is RabbitMQ Management Plugin. +%% +%% The Initial Developer of the Original Code is GoPivotal, Inc. +%% Copyright (c) 2010-2015 Pivotal Software, Inc. All rights reserved. +%% + +-module(rabbit_mgmt_app). + +-behaviour(application). +-export([start/2, stop/1, reset_dispatcher/1]). + +-include("rabbit_mgmt.hrl"). +-include_lib("amqp_client/include/amqp_client.hrl"). + +-define(CONTEXT, rabbit_mgmt). +-define(STATIC_PATH, "priv/www"). + +start(_Type, _StartArgs) -> + {ok, Listener} = application:get_env(rabbitmq_management, listener), + setup_wm_logging(), + register_context(Listener, []), + log_startup(Listener), + rabbit_mgmt_sup_sup:start_link(). + +stop(_State) -> + unregister_context(), + ok. + +%% At the point at which this is invoked we have both newly enabled +%% apps and about-to-disable apps running (so that +%% rabbit_mgmt_reset_handler can look at all of them to find +%% extensions). Therefore we have to explicitly exclude +%% about-to-disable apps from our new dispatcher. +reset_dispatcher(IgnoreApps) -> + unregister_context(), + {ok, Listener} = application:get_env(rabbitmq_management, listener), + register_context(Listener, IgnoreApps). + +register_context(Listener, IgnoreApps) -> + rabbit_web_dispatch:register_context_handler( + ?CONTEXT, Listener, "", make_loop(IgnoreApps), "RabbitMQ Management"). + +unregister_context() -> + rabbit_web_dispatch:unregister_context(?CONTEXT). + +make_loop(IgnoreApps) -> + Dispatch = rabbit_mgmt_dispatcher:build_dispatcher(IgnoreApps), + WMLoop = rabbit_webmachine:makeloop(Dispatch), + LocalPaths = [filename:join(module_path(M), ?STATIC_PATH) || + M <- rabbit_mgmt_dispatcher:modules(IgnoreApps)], + fun(Req) -> respond(Req, LocalPaths, WMLoop) end. + +module_path(Module) -> + {file, Here} = code:is_loaded(Module), + filename:dirname(filename:dirname(Here)). + +respond(Req, LocalPaths, WMLoop) -> + Path = Req:get(path), + Redirect = fun(L) -> {301, [{"Location", L}], ""} end, + case Path of + "/api/" ++ Rest when length(Rest) > 0 -> + WMLoop(Req); + "" -> + Req:respond(Redirect("/")); + "/mgmt/" -> + Req:respond(Redirect("/")); + "/mgmt" -> + Req:respond(Redirect("/")); + "/" ++ Stripped -> + serve_file(Req, Stripped, LocalPaths, Redirect) + end. + +serve_file(Req, Path, [LocalPath], _Redirect) -> + Req:serve_file(Path, LocalPath); +serve_file(Req, Path, [LocalPath | Others], Redirect) -> + Path1 = filename:join([LocalPath, Path]), + case filelib:is_regular(Path1) of + true -> Req:serve_file(Path, LocalPath); + false -> case filelib:is_regular(Path1 ++ "/index.html") of + true -> index(Req, Path, LocalPath, Redirect); + false -> serve_file(Req, Path, Others, Redirect) + end + end. + +index(Req, Path, LocalPath, Redirect) -> + case lists:reverse(Path) of + "" -> Req:serve_file("index.html", LocalPath); + "/" ++ _ -> Req:serve_file(Path ++ "index.html", LocalPath); + _ -> Req:respond(Redirect(Path ++ "/")) + end. + +setup_wm_logging() -> + rabbit_webmachine:setup(), + {ok, LogDir} = application:get_env(rabbitmq_management, http_log_dir), + case LogDir of + none -> ok; + _ -> webmachine_log:add_handler(webmachine_log_handler, [LogDir]) + end. + +log_startup(Listener) -> + rabbit_log:info("Management plugin started. Port: ~w~n", [port(Listener)]). + +port(Listener) -> + proplists:get_value(port, Listener). diff --git a/rabbitmq-server/plugins-src/rabbitmq-management/src/rabbit_mgmt_db.erl b/rabbitmq-server/plugins-src/rabbitmq-management/src/rabbit_mgmt_db.erl new file mode 100644 index 0000000..e7cb753 --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-management/src/rabbit_mgmt_db.erl @@ -0,0 +1,1213 @@ +%% The contents of this file are subject to the Mozilla Public License +%% Version 1.1 (the "License"); you may not use this file except in +%% compliance with the License. You may obtain a copy of the License at +%% http://www.mozilla.org/MPL/ +%% +%% Software distributed under the License is distributed on an "AS IS" +%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the +%% License for the specific language governing rights and limitations +%% under the License. +%% +%% The Original Code is RabbitMQ Management Plugin. +%% +%% The Initial Developer of the Original Code is GoPivotal, Inc. +%% Copyright (c) 2010-2015 Pivotal Software, Inc. All rights reserved. +%% + +-module(rabbit_mgmt_db). + +-include("rabbit_mgmt.hrl"). +-include_lib("rabbit_common/include/rabbit.hrl"). + +-behaviour(gen_server2). + +-export([start_link/0]). + +-export([augment_exchanges/3, augment_queues/3, + augment_nodes/2, augment_vhosts/2, + get_channel/2, get_connection/2, + get_all_channels/1, get_all_connections/1, + get_all_consumers/0, get_all_consumers/1, + get_overview/2, get_overview/1]). + +-export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, + code_change/3, handle_pre_hibernate/1, + prioritise_cast/3, prioritise_call/4, format_message_queue/2]). + +%% For testing +-export([override_lookups/1, reset_lookups/0]). + +-import(rabbit_misc, [pget/3, pset/3]). + +%% The management database listens to events broadcast via the +%% rabbit_event mechanism, and responds to queries from the various +%% rabbit_mgmt_wm_* modules. It handles several kinds of events, and +%% slices and dices them in various ways. +%% +%% There are three types of events coming in: created (when an object +%% is created, containing immutable facts about it), stats (emitted on +%% a timer, with mutable facts about the object), and deleted (just +%% containing the object's ID). In this context "objects" means +%% connections, channels, exchanges, queues, consumers, vhosts and +%% nodes. Note that we do not care about users, permissions, bindings, +%% parameters or policies. +%% +%% Connections and channels are identified by pids. Queues and +%% exchanges are identified by names (which are #resource{}s). VHosts +%% and nodes are identified by names which are binaries. And consumers +%% are identified by {ChPid, QName, CTag}. +%% +%% The management database records the "created" events for +%% connections, channels and consumers, and can thus be authoritative +%% about those objects. For queues, exchanges and nodes we go to +%% Mnesia to find out the immutable details of the objects. +%% +%% For everything other than consumers, the database can then augment +%% these immutable details with stats, as the object changes. (We +%% never emit anything very interesting about consumers). +%% +%% Stats on the inbound side are refered to as coarse- and +%% fine-grained. Fine grained statistics are the message rates +%% maintained by channels and associated with tuples: {publishing +%% channel, exchange}, {publishing channel, exchange, queue} and +%% {queue, consuming channel}. Coarse grained stats are everything +%% else and are associated with only one object, not a tuple. +%% +%% Within the management database though we rearrange things a bit: we +%% refer to basic stats, simple stats and detail stats. +%% +%% Basic stats are those coarse grained stats for which we do not +%% retain a history and do not perform any calculations - +%% e.g. connection.state or channel.prefetch_count. +%% +%% Simple stats are those for which we do history / calculations which +%% are associated with one object *after aggregation* - so these might +%% originate with coarse grained stats - e.g. connection.send_oct or +%% queue.messages_ready. But they might also originate from fine +%% grained stats which have been aggregated - e.g. the message rates +%% for a vhost or queue. +%% +%% Finally, detailed stats are those for which we do history / +%% calculations which are associated with two objects. These +%% have to have originated as fine grained stats, but can still have +%% been aggregated. +%% +%% Created events and basic stats are stored in ETS tables by object, +%% looked up in an orddict in #state.tables. Simple and detailed stats +%% (which only differ depending on how they're keyed) are stored in +%% #state.aggregated_stats. +%% +%% For detailed stats we also store an index for each object referencing +%% all the other objects that form a detailed stats key with it. This is +%% so that we can always avoid table scanning while deleting stats and +%% thus make sure that handling deleted events is O(n)-ish. +%% +%% For each key for simple and detailed stats we maintain a #stats{} +%% record, essentially a base counter for everything that happened +%% before the samples we have kept, and a gb_tree of {timestamp, +%% sample} values. +%% +%% We also have #state.old_stats to let us calculate instantaneous +%% rates, in order to apportion simple / detailed stats into time +%% slices as they come in. These instantaneous rates are not returned +%% in response to any query, the rates shown in the API are calculated +%% at query time. old_stats contains both coarse and fine +%% entries. Coarse entries are pruned when the corresponding object is +%% deleted, and fine entries are pruned when the emitting channel is +%% closed, and whenever we receive new fine stats from a channel. So +%% it's quite close to being a cache of "the previous stats we +%% received". +%% +%% We also keep a timer going, in order to prune old samples from +%% #state.aggregated_stats. +%% +%% Overall the object is to do all the aggregation when events come +%% in, and make queries be simple lookups as much as possible. One +%% area where this does not happen is the global overview - which is +%% aggregated from vhost stats at query time since we do not want to +%% reveal anything about other vhosts to unprivileged users. + +-record(state, { + %% "stats" for which no calculations are required + tables, + %% database of aggregated samples + aggregated_stats, + %% index for detailed aggregated_stats that have 2-tuple keys + aggregated_stats_index, + %% What the previous info item was for any given + %% {queue/channel/connection} + old_stats, + gc_timer, + gc_next_key, + lookups, + interval, + event_refresh_ref, + rates_mode}). + +-define(FINE_STATS_TYPES, [channel_queue_stats, channel_exchange_stats, + channel_queue_exchange_stats]). +-define(TABLES, [queue_stats, connection_stats, channel_stats, + consumers_by_queue, consumers_by_channel, + node_stats, node_node_stats]). + +-define(DELIVER_GET, [deliver, deliver_no_ack, get, get_no_ack]). +-define(FINE_STATS, [publish, publish_in, publish_out, + ack, deliver_get, confirm, return_unroutable, redeliver] ++ + ?DELIVER_GET). + +%% Most come from channels as fine stats, but queues emit these directly. +-define(QUEUE_MSG_RATES, [disk_reads, disk_writes]). + +-define(MSG_RATES, ?FINE_STATS ++ ?QUEUE_MSG_RATES). + +-define(QUEUE_MSG_COUNTS, [messages, messages_ready, messages_unacknowledged]). + +-define(COARSE_NODE_STATS, + [mem_used, fd_used, sockets_used, proc_used, disk_free, + io_read_count, io_read_bytes, io_read_avg_time, + io_write_count, io_write_bytes, io_write_avg_time, + io_sync_count, io_sync_avg_time, + io_seek_count, io_seek_avg_time, + io_reopen_count, mnesia_ram_tx_count, mnesia_disk_tx_count, + msg_store_read_count, msg_store_write_count, + queue_index_journal_write_count, + queue_index_write_count, queue_index_read_count]). + +-define(COARSE_NODE_NODE_STATS, [send_bytes, recv_bytes]). + +%% Normally 0 and no history means "has never happened, don't +%% report". But for these things we do want to report even at 0 with +%% no history. +-define(ALWAYS_REPORT_STATS, + [io_read_avg_time, io_write_avg_time, + io_sync_avg_time | ?QUEUE_MSG_COUNTS]). + +-define(COARSE_CONN_STATS, [recv_oct, send_oct]). + +-define(GC_INTERVAL, 5000). +-define(GC_MIN_ROWS, 100). +-define(GC_MIN_RATIO, 0.01). + +-define(DROP_LENGTH, 1000). + +prioritise_cast({event, #event{type = Type, + props = Props}}, Len, _State) + when (Type =:= channel_stats orelse + Type =:= queue_stats) andalso Len > ?DROP_LENGTH -> + case pget(idle_since, Props) of + unknown -> drop; + _ -> 0 + end; +prioritise_cast(_Msg, _Len, _State) -> + 0. + +%% We want timely replies to queries even when overloaded, so return 5 +%% as priority. Also we only have access to the queue length here, not +%% in handle_call/3, so stash it in the dictionary. This is a bit ugly +%% but better than fiddling with gen_server2 even more. +prioritise_call(_Msg, _From, Len, _State) -> + put(last_queue_length, Len), + 5. + +%%---------------------------------------------------------------------------- +%% API +%%---------------------------------------------------------------------------- + +start_link() -> + Ref = make_ref(), + case gen_server2:start_link({global, ?MODULE}, ?MODULE, [Ref], []) of + {ok, Pid} -> register(?MODULE, Pid), %% [1] + rabbit:force_event_refresh(Ref), + {ok, Pid}; + Else -> Else + end. +%% [1] For debugging it's helpful to locally register the name too +%% since that shows up in places global names don't. + +%% R = Ranges, M = Mode +augment_exchanges(Xs, R, M) -> safe_call({augment_exchanges, Xs, R, M}, Xs). +augment_queues(Qs, R, M) -> safe_call({augment_queues, Qs, R, M}, Qs). +augment_vhosts(VHosts, R) -> safe_call({augment_vhosts, VHosts, R}, VHosts). +augment_nodes(Nodes, R) -> safe_call({augment_nodes, Nodes, R}, Nodes). + +get_channel(Name, R) -> safe_call({get_channel, Name, R}, not_found). +get_connection(Name, R) -> safe_call({get_connection, Name, R}, not_found). + +get_all_channels(R) -> safe_call({get_all_channels, R}). +get_all_connections(R) -> safe_call({get_all_connections, R}). + +get_all_consumers() -> safe_call({get_all_consumers, all}). +get_all_consumers(V) -> safe_call({get_all_consumers, V}). + +get_overview(User, R) -> safe_call({get_overview, User, R}). +get_overview(R) -> safe_call({get_overview, all, R}). + +override_lookups(Lookups) -> safe_call({override_lookups, Lookups}). +reset_lookups() -> safe_call(reset_lookups). + +safe_call(Term) -> safe_call(Term, []). +safe_call(Term, Default) -> safe_call(Term, Default, 1). + +%% See rabbit_mgmt_sup_sup for a discussion of the retry logic. +safe_call(Term, Default, Retries) -> + rabbit_misc:with_exit_handler( + fun () -> + case Retries of + 0 -> Default; + _ -> rabbit_mgmt_sup_sup:start_child(), + safe_call(Term, Default, Retries - 1) + end + end, + fun () -> gen_server2:call({global, ?MODULE}, Term, infinity) end). + +%%---------------------------------------------------------------------------- +%% Internal, gen_server2 callbacks +%%---------------------------------------------------------------------------- + +init([Ref]) -> + %% When Rabbit is overloaded, it's usually especially important + %% that the management plugin work. + process_flag(priority, high), + {ok, Interval} = application:get_env(rabbit, collect_statistics_interval), + {ok, RatesMode} = application:get_env(rabbitmq_management, rates_mode), + rabbit_node_monitor:subscribe(self()), + rabbit_log:info("Statistics database started.~n"), + Table = fun () -> ets:new(rabbit_mgmt_db, [ordered_set]) end, + Tables = orddict:from_list([{Key, Table()} || Key <- ?TABLES]), + {ok, set_gc_timer( + reset_lookups( + #state{interval = Interval, + tables = Tables, + old_stats = Table(), + aggregated_stats = Table(), + aggregated_stats_index = Table(), + event_refresh_ref = Ref, + rates_mode = RatesMode})), hibernate, + {backoff, ?HIBERNATE_AFTER_MIN, ?HIBERNATE_AFTER_MIN, ?DESIRED_HIBERNATE}}. + +handle_call({augment_exchanges, Xs, Ranges, basic}, _From, State) -> + reply(list_exchange_stats(Ranges, Xs, State), State); + +handle_call({augment_exchanges, Xs, Ranges, full}, _From, State) -> + reply(detail_exchange_stats(Ranges, Xs, State), State); + +handle_call({augment_queues, Qs, Ranges, basic}, _From, State) -> + reply(list_queue_stats(Ranges, Qs, State), State); + +handle_call({augment_queues, Qs, Ranges, full}, _From, State) -> + reply(detail_queue_stats(Ranges, Qs, State), State); + +handle_call({augment_vhosts, VHosts, Ranges}, _From, State) -> + reply(vhost_stats(Ranges, VHosts, State), State); + +handle_call({augment_nodes, Nodes, Ranges}, _From, State) -> + {reply, node_stats(Ranges, Nodes, State), State}; + +handle_call({get_channel, Name, Ranges}, _From, + State = #state{tables = Tables}) -> + case created_event(Name, channel_stats, Tables) of + not_found -> reply(not_found, State); + Ch -> [Result] = detail_channel_stats(Ranges, [Ch], State), + reply(Result, State) + end; + +handle_call({get_connection, Name, Ranges}, _From, + State = #state{tables = Tables}) -> + case created_event(Name, connection_stats, Tables) of + not_found -> reply(not_found, State); + Conn -> [Result] = connection_stats(Ranges, [Conn], State), + reply(Result, State) + end; + +handle_call({get_all_channels, Ranges}, _From, + State = #state{tables = Tables}) -> + Chans = created_events(channel_stats, Tables), + reply(list_channel_stats(Ranges, Chans, State), State); + +handle_call({get_all_connections, Ranges}, _From, + State = #state{tables = Tables}) -> + Conns = created_events(connection_stats, Tables), + reply(connection_stats(Ranges, Conns, State), State); + +handle_call({get_all_consumers, VHost}, + _From, State = #state{tables = Tables}) -> + All = ets:tab2list(orddict:fetch(consumers_by_queue, Tables)), + {reply, [augment_msg_stats( + augment_consumer(Obj), State) || + {{#resource{virtual_host = VHostC}, _Ch, _CTag}, Obj} <- All, + VHost =:= all orelse VHost =:= VHostC], State}; + +handle_call({get_overview, User, Ranges}, _From, + State = #state{tables = Tables}) -> + VHosts = case User of + all -> rabbit_vhost:list(); + _ -> rabbit_mgmt_util:list_visible_vhosts(User) + end, + %% TODO: there's no reason we can't do an overview of send_oct and + %% recv_oct now! + VStats = [read_simple_stats(vhost_stats, VHost, State) || + VHost <- VHosts], + MessageStats = [overview_sum(Type, VStats) || Type <- ?MSG_RATES], + QueueStats = [overview_sum(Type, VStats) || Type <- ?QUEUE_MSG_COUNTS], + F = case User of + all -> fun (L) -> length(L) end; + _ -> fun (L) -> length(rabbit_mgmt_util:filter_user(L, User)) end + end, + %% Filtering out the user's consumers would be rather expensive so let's + %% just not show it + Consumers = case User of + all -> Table = orddict:fetch(consumers_by_queue, Tables), + [{consumers, ets:info(Table, size)}]; + _ -> [] + end, + ObjectTotals = Consumers ++ + [{queues, length([Q || V <- VHosts, + Q <- rabbit_amqqueue:list(V)])}, + {exchanges, length([X || V <- VHosts, + X <- rabbit_exchange:list(V)])}, + {connections, F(created_events(connection_stats, Tables))}, + {channels, F(created_events(channel_stats, Tables))}], + reply([{message_stats, format_samples(Ranges, MessageStats, State)}, + {queue_totals, format_samples(Ranges, QueueStats, State)}, + {object_totals, ObjectTotals}, + {statistics_db_event_queue, get(last_queue_length)}], State); + +handle_call({override_lookups, Lookups}, _From, State) -> + reply(ok, State#state{lookups = Lookups}); + +handle_call(reset_lookups, _From, State) -> + reply(ok, reset_lookups(State)); + +%% Used in rabbit_mgmt_test_db where we need guarantees events have +%% been handled before querying +handle_call({event, Event = #event{reference = none}}, _From, State) -> + handle_event(Event, State), + reply(ok, State); + +handle_call(_Request, _From, State) -> + reply(not_understood, State). + +%% Only handle events that are real, or pertain to a force-refresh +%% that we instigated. +handle_cast({event, Event = #event{reference = none}}, State) -> + handle_event(Event, State), + noreply(State); + +handle_cast({event, Event = #event{reference = Ref}}, + State = #state{event_refresh_ref = Ref}) -> + handle_event(Event, State), + noreply(State); + +handle_cast(_Request, State) -> + noreply(State). + +handle_info(gc, State) -> + noreply(set_gc_timer(gc_batch(State))); + +handle_info({node_down, Node}, State = #state{tables = Tables}) -> + Conns = created_events(connection_stats, Tables), + Chs = created_events(channel_stats, Tables), + delete_all_from_node(connection_closed, Node, Conns, State), + delete_all_from_node(channel_closed, Node, Chs, State), + noreply(State); + +handle_info(_Info, State) -> + noreply(State). + +terminate(_Arg, _State) -> + ok. + +code_change(_OldVsn, State, _Extra) -> + {ok, State}. + +reply(Reply, NewState) -> {reply, Reply, NewState, hibernate}. +noreply(NewState) -> {noreply, NewState, hibernate}. + +set_gc_timer(State) -> + TRef = erlang:send_after(?GC_INTERVAL, self(), gc), + State#state{gc_timer = TRef}. + +reset_lookups(State) -> + State#state{lookups = [{exchange, fun rabbit_exchange:lookup/1}, + {queue, fun rabbit_amqqueue:lookup/1}]}. + +handle_pre_hibernate(State) -> + %% rabbit_event can end up holding on to some memory after a busy + %% workout, but it's not a gen_server so we can't make it + %% hibernate. The best we can do is forcibly GC it here (if + %% rabbit_mgmt_db is hibernating the odds are rabbit_event is + %% quiescing in some way too). + rpc:multicall( + rabbit_mnesia:cluster_nodes(running), rabbit_mgmt_db_handler, gc, []), + {hibernate, State}. + +format_message_queue(Opt, MQ) -> rabbit_misc:format_message_queue(Opt, MQ). + +delete_all_from_node(Type, Node, Items, State) -> + [case node(Pid) of + Node -> handle_event(#event{type = Type, props = [{pid, Pid}]}, State); + _ -> ok + end || Item <- Items, Pid <- [pget(pid, Item)]]. + +%%---------------------------------------------------------------------------- +%% Internal, utilities +%%---------------------------------------------------------------------------- + +pget(Key, List) -> pget(Key, List, unknown). + +%% id_name() and id() are for use when handling events, id_lookup() +%% for when augmenting. The difference is that when handling events a +%% queue name will be a resource, but when augmenting we will be +%% passed a queue proplist that will already have been formatted - +%% i.e. it will have name and vhost keys. +id_name(node_stats) -> name; +id_name(node_node_stats) -> route; +id_name(vhost_stats) -> name; +id_name(queue_stats) -> name; +id_name(exchange_stats) -> name; +id_name(channel_stats) -> pid; +id_name(connection_stats) -> pid. + +id(Type, List) -> pget(id_name(Type), List). + +id_lookup(queue_stats, List) -> + rabbit_misc:r(pget(vhost, List), queue, pget(name, List)); +id_lookup(exchange_stats, List) -> + rabbit_misc:r(pget(vhost, List), exchange, pget(name, List)); +id_lookup(Type, List) -> + id(Type, List). + +lookup_element(Table, Key) -> lookup_element(Table, Key, 2). + +lookup_element(Table, Key, Pos) -> + try ets:lookup_element(Table, Key, Pos) + catch error:badarg -> [] + end. + +fine_stats_id(ChPid, {Q, X}) -> {ChPid, Q, X}; +fine_stats_id(ChPid, QorX) -> {ChPid, QorX}. + +floor(TS, #state{interval = Interval}) -> + rabbit_mgmt_util:floor(rabbit_mgmt_format:now_to_ms(TS), Interval). +ceil(TS, #state{interval = Interval}) -> + rabbit_mgmt_util:ceil (rabbit_mgmt_format:now_to_ms(TS), Interval). + +details_key(Key) -> list_to_atom(atom_to_list(Key) ++ "_details"). + +%%---------------------------------------------------------------------------- +%% Internal, event-receiving side +%%---------------------------------------------------------------------------- + +handle_event(#event{type = queue_stats, props = Stats, timestamp = Timestamp}, + State) -> + handle_stats(queue_stats, Stats, Timestamp, + [{fun rabbit_mgmt_format:properties/1,[backing_queue_status]}, + {fun rabbit_mgmt_format:now_to_str/1, [idle_since]}, + {fun rabbit_mgmt_format:queue_state/1, [state]}], + ?QUEUE_MSG_COUNTS, ?QUEUE_MSG_RATES, State); + +handle_event(Event = #event{type = queue_deleted, + props = [{name, Name}], + timestamp = Timestamp}, + State = #state{old_stats = OldTable}) -> + delete_consumers(Name, consumers_by_queue, consumers_by_channel, State), + %% This is fiddly. Unlike for connections and channels, we need to + %% decrease any amalgamated coarse stats for [messages, + %% messages_ready, messages_unacknowledged] for this queue - since + %% the queue's deletion means we have really got rid of messages! + Id = {coarse, {queue_stats, Name}}, + %% This ceil must correspond to the ceil in append_samples/5 + TS = ceil(Timestamp, State), + OldStats = lookup_element(OldTable, Id), + [record_sample(Id, {Key, -pget(Key, OldStats, 0), TS, State}, true, State) + || Key <- ?QUEUE_MSG_COUNTS], + delete_samples(channel_queue_stats, {'_', Name}, State), + delete_samples(queue_exchange_stats, {Name, '_'}, State), + delete_samples(queue_stats, Name, State), + handle_deleted(queue_stats, Event, State); + +handle_event(Event = #event{type = exchange_deleted, + props = [{name, Name}]}, State) -> + delete_samples(channel_exchange_stats, {'_', Name}, State), + delete_samples(queue_exchange_stats, {'_', Name}, State), + delete_samples(exchange_stats, Name, State), + handle_deleted(exchange_stats, Event, State); + +handle_event(#event{type = vhost_deleted, + props = [{name, Name}]}, State) -> + delete_samples(vhost_stats, Name, State); + +handle_event(#event{type = connection_created, props = Stats}, State) -> + handle_created( + connection_stats, Stats, + [{fun rabbit_mgmt_format:addr/1, [host, peer_host]}, + {fun rabbit_mgmt_format:port/1, [port, peer_port]}, + {fun rabbit_mgmt_format:protocol/1, [protocol]}, + {fun rabbit_mgmt_format:amqp_table/1, [client_properties]}], State); + +handle_event(#event{type = connection_stats, props = Stats, + timestamp = Timestamp}, + State) -> + handle_stats(connection_stats, Stats, Timestamp, [], ?COARSE_CONN_STATS, + State); + +handle_event(Event = #event{type = connection_closed, + props = [{pid, Pid}]}, State) -> + delete_samples(connection_stats, Pid, State), + handle_deleted(connection_stats, Event, State); + +handle_event(#event{type = channel_created, props = Stats}, State) -> + handle_created(channel_stats, Stats, [], State); + +handle_event(#event{type = channel_stats, props = Stats, timestamp = Timestamp}, + State = #state{old_stats = OldTable}) -> + handle_stats(channel_stats, Stats, Timestamp, + [{fun rabbit_mgmt_format:now_to_str/1, [idle_since]}], + [], State), + ChPid = id(channel_stats, Stats), + AllStats = [old_fine_stats(Type, Stats, State) + || Type <- ?FINE_STATS_TYPES], + ets:match_delete(OldTable, {{fine, {ChPid, '_'}}, '_'}), + ets:match_delete(OldTable, {{fine, {ChPid, '_', '_'}}, '_'}), + [handle_fine_stats(Timestamp, AllStatsElem, State) + || AllStatsElem <- AllStats]; + +handle_event(Event = #event{type = channel_closed, + props = [{pid, Pid}]}, + State = #state{old_stats = Old}) -> + delete_consumers(Pid, consumers_by_channel, consumers_by_queue, State), + delete_samples(channel_queue_stats, {Pid, '_'}, State), + delete_samples(channel_exchange_stats, {Pid, '_'}, State), + delete_samples(channel_stats, Pid, State), + handle_deleted(channel_stats, Event, State), + ets:match_delete(Old, {{fine, {Pid, '_'}}, '_'}), + ets:match_delete(Old, {{fine, {Pid, '_', '_'}}, '_'}); + +handle_event(#event{type = consumer_created, props = Props}, State) -> + Fmt = [{fun rabbit_mgmt_format:amqp_table/1, [arguments]}], + handle_consumer(fun(Table, Id, P0) -> + P = rabbit_mgmt_format:format(P0, Fmt), + ets:insert(Table, {Id, P}) + end, + Props, State); + +handle_event(#event{type = consumer_deleted, props = Props}, State) -> + handle_consumer(fun(Table, Id, _P) -> ets:delete(Table, Id) end, + Props, State); + +%% TODO: we don't clear up after dead nodes here - this is a very tiny +%% leak every time a node is permanently removed from the cluster. Do +%% we care? +handle_event(#event{type = node_stats, props = Stats0, timestamp = Timestamp}, + State) -> + Stats = proplists:delete(persister_stats, Stats0) ++ + pget(persister_stats, Stats0), + handle_stats(node_stats, Stats, Timestamp, [], ?COARSE_NODE_STATS, State); + +handle_event(#event{type = node_node_stats, props = Stats, + timestamp = Timestamp}, State) -> + handle_stats(node_node_stats, Stats, Timestamp, [], ?COARSE_NODE_NODE_STATS, + State); + +handle_event(Event = #event{type = node_node_deleted, + props = [{route, Route}]}, State) -> + delete_samples(node_node_stats, Route, State), + handle_deleted(node_node_stats, Event, State); + +handle_event(_Event, _State) -> + ok. + +handle_created(TName, Stats, Funs, State = #state{tables = Tables}) -> + Formatted = rabbit_mgmt_format:format(Stats, Funs), + ets:insert(orddict:fetch(TName, Tables), {{id(TName, Stats), create}, + Formatted, + pget(name, Stats)}), + {ok, State}. + +handle_stats(TName, Stats, Timestamp, Funs, RatesKeys, State) -> + handle_stats(TName, Stats, Timestamp, Funs, RatesKeys, [], State). + +handle_stats(TName, Stats, Timestamp, Funs, RatesKeys, NoAggRatesKeys, + State = #state{tables = Tables, old_stats = OldTable}) -> + Id = id(TName, Stats), + IdSamples = {coarse, {TName, Id}}, + OldStats = lookup_element(OldTable, IdSamples), + append_samples( + Stats, Timestamp, OldStats, IdSamples, RatesKeys, true, State), + append_samples( + Stats, Timestamp, OldStats, IdSamples, NoAggRatesKeys, false, State), + StripKeys = [id_name(TName)] ++ RatesKeys ++ ?FINE_STATS_TYPES, + Stats1 = [{K, V} || {K, V} <- Stats, not lists:member(K, StripKeys)], + Stats2 = rabbit_mgmt_format:format(Stats1, Funs), + ets:insert(orddict:fetch(TName, Tables), {{Id, stats}, Stats2, Timestamp}), + {ok, State}. + +handle_deleted(TName, #event{props = Props}, State = #state{tables = Tables, + old_stats = Old}) -> + Id = id(TName, Props), + case orddict:find(TName, Tables) of + {ok, Table} -> ets:delete(Table, {Id, create}), + ets:delete(Table, {Id, stats}); + error -> ok + end, + ets:delete(Old, {coarse, {TName, Id}}), + {ok, State}. + +handle_consumer(Fun, Props, State = #state{tables = Tables}) -> + P = rabbit_mgmt_format:format(Props, []), + CTag = pget(consumer_tag, P), + Q = pget(queue, P), + Ch = pget(channel, P), + QTable = orddict:fetch(consumers_by_queue, Tables), + ChTable = orddict:fetch(consumers_by_channel, Tables), + Fun(QTable, {Q, Ch, CTag}, P), + Fun(ChTable, {Ch, Q, CTag}, P), + {ok, State}. + +%% The consumer_deleted event is emitted by queues themselves - +%% therefore in the event that a queue dies suddenly we may not get +%% it. The best way to handle this is to make sure we also clean up +%% consumers when we hear about any queue going down. +delete_consumers(PrimId, PrimTableName, SecTableName, + #state{tables = Tables}) -> + Table1 = orddict:fetch(PrimTableName, Tables), + Table2 = orddict:fetch(SecTableName, Tables), + SecIdCTags = ets:match(Table1, {{PrimId, '$1', '$2'}, '_'}), + ets:match_delete(Table1, {{PrimId, '_', '_'}, '_'}), + [ets:delete(Table2, {SecId, PrimId, CTag}) || [SecId, CTag] <- SecIdCTags]. + +old_fine_stats(Type, Props, #state{old_stats = Old}) -> + case pget(Type, Props) of + unknown -> ignore; + AllFineStats0 -> ChPid = id(channel_stats, Props), + [begin + Id = fine_stats_id(ChPid, Ids), + {Id, Stats, lookup_element(Old, {fine, Id})} + end || {Ids, Stats} <- AllFineStats0] + end. + +handle_fine_stats(_Timestamp, ignore, _State) -> + ok; + +handle_fine_stats(Timestamp, AllStats, State) -> + [handle_fine_stat(Id, Stats, Timestamp, OldStats, State) || + {Id, Stats, OldStats} <- AllStats]. + +handle_fine_stat(Id, Stats, Timestamp, OldStats, State) -> + Total = lists:sum([V || {K, V} <- Stats, lists:member(K, ?DELIVER_GET)]), + Stats1 = case Total of + 0 -> Stats; + _ -> [{deliver_get, Total}|Stats] + end, + append_samples(Stats1, Timestamp, OldStats, {fine, Id}, all, true, State). + +delete_samples(Type, {Id, '_'}, State) -> + delete_samples_with_index(Type, Id, fun forward/2, State); +delete_samples(Type, {'_', Id}, State) -> + delete_samples_with_index(Type, Id, fun reverse/2, State); +delete_samples(Type, Id, #state{aggregated_stats = ETS}) -> + ets:match_delete(ETS, delete_match(Type, Id)). + +delete_samples_with_index(Type, Id, Order, + #state{aggregated_stats = ETS, + aggregated_stats_index = ETSi}) -> + Ids2 = lists:append(ets:match(ETSi, {{Type, Id, '$1'}})), + ets:match_delete(ETSi, {{Type, Id, '_'}}), + [begin + ets:match_delete(ETS, delete_match(Type, Order(Id, Id2))), + ets:match_delete(ETSi, {{Type, Id2, Id}}) + end || Id2 <- Ids2]. + +forward(A, B) -> {A, B}. +reverse(A, B) -> {B, A}. + +delete_match(Type, Id) -> {{{Type, Id}, '_'}, '_'}. + +append_samples(Stats, TS, OldStats, Id, Keys, Agg, + State = #state{old_stats = OldTable}) -> + case ignore_coarse_sample(Id, State) of + false -> + %% This ceil must correspond to the ceil in handle_event + %% queue_deleted + NewMS = ceil(TS, State), + case Keys of + all -> [append_sample(K, V, NewMS, OldStats, Id, Agg, State) + || {K, V} <- Stats]; + _ -> [append_sample(K, V, NewMS, OldStats, Id, Agg, State) + || K <- Keys, + V <- [pget(K, Stats)], + V =/= 0 orelse lists:member(K, ?ALWAYS_REPORT_STATS)] + end, + ets:insert(OldTable, {Id, Stats}); + true -> + ok + end. + +append_sample(Key, Val, NewMS, OldStats, Id, Agg, State) when is_number(Val) -> + OldVal = case pget(Key, OldStats, 0) of + N when is_number(N) -> N; + _ -> 0 + end, + record_sample(Id, {Key, Val - OldVal, NewMS, State}, Agg, State), + ok; +append_sample(_Key, _Value, _NewMS, _OldStats, _Id, _Agg, _State) -> + ok. + +ignore_coarse_sample({coarse, {queue_stats, Q}}, State) -> + not object_exists(Q, State); +ignore_coarse_sample(_, _) -> + false. + +%% Node stats do not have a vhost of course +record_sample({coarse, {node_stats, _Node} = Id}, Args, true, _State) -> + record_sample0(Id, Args); + +record_sample({coarse, {node_node_stats, _Names} = Id}, Args, true, _State) -> + record_sample0(Id, Args); + +record_sample({coarse, Id}, Args, false, _State) -> + record_sample0(Id, Args); + +record_sample({coarse, Id}, Args, true, State) -> + record_sample0(Id, Args), + record_sample0({vhost_stats, vhost(Id, State)}, Args); + +%% Deliveries / acks (Q -> Ch) +record_sample({fine, {Ch, Q = #resource{kind = queue}}}, Args, true, State) -> + case object_exists(Q, State) of + true -> record_sample0({channel_queue_stats, {Ch, Q}}, Args), + record_sample0({queue_stats, Q}, Args); + false -> ok + end, + record_sample0({channel_stats, Ch}, Args), + record_sample0({vhost_stats, vhost(Q)}, Args); + +%% Publishes / confirms (Ch -> X) +record_sample({fine, {Ch, X = #resource{kind = exchange}}}, Args, true,State) -> + case object_exists(X, State) of + true -> record_sample0({channel_exchange_stats, {Ch, X}}, Args), + record_sampleX(publish_in, X, Args); + false -> ok + end, + record_sample0({channel_stats, Ch}, Args), + record_sample0({vhost_stats, vhost(X)}, Args); + +%% Publishes (but not confirms) (Ch -> X -> Q) +record_sample({fine, {_Ch, + Q = #resource{kind = queue}, + X = #resource{kind = exchange}}}, Args, true, State) -> + %% TODO This one logically feels like it should be here. It would + %% correspond to "publishing channel message rates to queue" - + %% which would be nice to handle - except we don't. And just + %% uncommenting this means it gets merged in with "consuming + %% channel delivery from queue" - which is not very helpful. + %% record_sample0({channel_queue_stats, {Ch, Q}}, Args), + QExists = object_exists(Q, State), + XExists = object_exists(X, State), + case QExists of + true -> record_sample0({queue_stats, Q}, Args); + false -> ok + end, + case QExists andalso XExists of + true -> record_sample0({queue_exchange_stats, {Q, X}}, Args); + false -> ok + end, + case XExists of + true -> record_sampleX(publish_out, X, Args); + false -> ok + end. + +%% We have to check the queue and exchange objects still exist since +%% their deleted event could be overtaken by a channel stats event +%% which contains fine stats referencing them. That's also why we +%% don't need to check the channels exist - their deleted event can't +%% be overtaken by their own last stats event. +%% +%% Also, sometimes the queue_deleted event is not emitted by the queue +%% (in the nodedown case) - so it can overtake the final queue_stats +%% event (which is not *guaranteed* to be lost). So we make a similar +%% check for coarse queue stats. +%% +%% We can be sure that mnesia will be up to date by the time we receive +%% the event (even though we dirty read) since the deletions are +%% synchronous and we do not emit the deleted event until after the +%% deletion has occurred. +object_exists(Name = #resource{kind = Kind}, #state{lookups = Lookups}) -> + case (pget(Kind, Lookups))(Name) of + {ok, _} -> true; + _ -> false + end. + +vhost(#resource{virtual_host = VHost}) -> VHost. + +vhost({queue_stats, #resource{virtual_host = VHost}}, _State) -> + VHost; +vhost({TName, Pid}, #state{tables = Tables}) -> + Table = orddict:fetch(TName, Tables), + pget(vhost, lookup_element(Table, {Pid, create})). + +%% exchanges have two sets of "publish" stats, so rearrange things a touch +record_sampleX(RenamePublishTo, X, {publish, Diff, TS, State}) -> + record_sample0({exchange_stats, X}, {RenamePublishTo, Diff, TS, State}); +record_sampleX(_RenamePublishTo, X, {Type, Diff, TS, State}) -> + record_sample0({exchange_stats, X}, {Type, Diff, TS, State}). + +%% Ignore case where ID1 and ID2 are in a tuple, i.e. detailed stats, +%% when in basic mode +record_sample0({Type, {_ID1, _ID2}}, {_, _, _, #state{rates_mode = basic}}) + when Type =/= node_node_stats -> + ok; +record_sample0(Id0, {Key, Diff, TS, #state{aggregated_stats = ETS, + aggregated_stats_index = ETSi}}) -> + Id = {Id0, Key}, + Old = case lookup_element(ETS, Id) of + [] -> case Id0 of + {Type, {Id1, Id2}} -> + ets:insert(ETSi, {{Type, Id2, Id1}}), + ets:insert(ETSi, {{Type, Id1, Id2}}); + _ -> + ok + end, + rabbit_mgmt_stats:blank(); + E -> E + end, + ets:insert(ETS, {Id, rabbit_mgmt_stats:record(TS, Diff, Old)}). + +%%---------------------------------------------------------------------------- +%% Internal, querying side +%%---------------------------------------------------------------------------- + +-define(QUEUE_DETAILS, + {queue_stats, [{incoming, queue_exchange_stats, fun first/1}, + {deliveries, channel_queue_stats, fun second/1}]}). + +-define(EXCHANGE_DETAILS, + {exchange_stats, [{incoming, channel_exchange_stats, fun second/1}, + {outgoing, queue_exchange_stats, fun second/1}]}). + +-define(CHANNEL_DETAILS, + {channel_stats, [{publishes, channel_exchange_stats, fun first/1}, + {deliveries, channel_queue_stats, fun first/1}]}). + +-define(NODE_DETAILS, + {node_stats, [{cluster_links, node_node_stats, fun first/1}]}). + +first(Id) -> {Id, '$1'}. +second(Id) -> {'$1', Id}. + +list_queue_stats(Ranges, Objs, State) -> + adjust_hibernated_memory_use( + merge_stats(Objs, queue_funs(Ranges, State))). + +detail_queue_stats(Ranges, Objs, State) -> + adjust_hibernated_memory_use( + merge_stats(Objs, [consumer_details_fun( + fun (Props) -> id_lookup(queue_stats, Props) end, + consumers_by_queue, State), + detail_stats_fun(Ranges, ?QUEUE_DETAILS, State) + | queue_funs(Ranges, State)])). + +queue_funs(Ranges, State) -> + [basic_stats_fun(queue_stats, State), + simple_stats_fun(Ranges, queue_stats, State), + augment_msg_stats_fun(State)]. + +list_exchange_stats(Ranges, Objs, State) -> + merge_stats(Objs, [simple_stats_fun(Ranges, exchange_stats, State), + augment_msg_stats_fun(State)]). + +detail_exchange_stats(Ranges, Objs, State) -> + merge_stats(Objs, [simple_stats_fun(Ranges, exchange_stats, State), + detail_stats_fun(Ranges, ?EXCHANGE_DETAILS, State), + augment_msg_stats_fun(State)]). + +connection_stats(Ranges, Objs, State) -> + merge_stats(Objs, [basic_stats_fun(connection_stats, State), + simple_stats_fun(Ranges, connection_stats, State), + augment_msg_stats_fun(State)]). + +list_channel_stats(Ranges, Objs, State) -> + merge_stats(Objs, [basic_stats_fun(channel_stats, State), + simple_stats_fun(Ranges, channel_stats, State), + augment_msg_stats_fun(State)]). + +detail_channel_stats(Ranges, Objs, State) -> + merge_stats(Objs, [basic_stats_fun(channel_stats, State), + simple_stats_fun(Ranges, channel_stats, State), + consumer_details_fun( + fun (Props) -> pget(pid, Props) end, + consumers_by_channel, State), + detail_stats_fun(Ranges, ?CHANNEL_DETAILS, State), + augment_msg_stats_fun(State)]). + +vhost_stats(Ranges, Objs, State) -> + merge_stats(Objs, [simple_stats_fun(Ranges, vhost_stats, State)]). + +node_stats(Ranges, Objs, State) -> + merge_stats(Objs, [basic_stats_fun(node_stats, State), + simple_stats_fun(Ranges, node_stats, State), + detail_and_basic_stats_fun( + node_node_stats, Ranges, ?NODE_DETAILS, State)]). + +merge_stats(Objs, Funs) -> + [lists:foldl(fun (Fun, Props) -> combine(Fun(Props), Props) end, Obj, Funs) + || Obj <- Objs]. + +combine(New, Old) -> + case pget(state, Old) of + unknown -> New ++ Old; + live -> New ++ proplists:delete(state, Old); + _ -> proplists:delete(state, New) ++ Old + end. + +%% i.e. the non-calculated stats +basic_stats_fun(Type, #state{tables = Tables}) -> + Table = orddict:fetch(Type, Tables), + fun (Props) -> + Id = id_lookup(Type, Props), + lookup_element(Table, {Id, stats}) + end. + +%% i.e. coarse stats, and fine stats aggregated up to a single number per thing +simple_stats_fun(Ranges, Type, State) -> + fun (Props) -> + Id = id_lookup(Type, Props), + extract_msg_stats( + format_samples(Ranges, read_simple_stats(Type, Id, State), State)) + end. + +%% i.e. fine stats that are broken out per sub-thing +detail_stats_fun(Ranges, {IdType, FineSpecs}, State) -> + fun (Props) -> + Id = id_lookup(IdType, Props), + [detail_stats(Ranges, Name, AggregatedStatsType, IdFun(Id), State) + || {Name, AggregatedStatsType, IdFun} <- FineSpecs] + end. + +%% This does not quite do the same as detail_stats_fun + +%% basic_stats_fun; the basic part here assumes compound keys (like +%% detail stats) but non-calculated (like basic stats). Currently the +%% only user of that is node-node stats. +%% +%% We also assume that FineSpecs is single length here (at [1]). +detail_and_basic_stats_fun(Type, Ranges, {IdType, FineSpecs}, + State = #state{tables = Tables}) -> + Table = orddict:fetch(Type, Tables), + F = detail_stats_fun(Ranges, {IdType, FineSpecs}, State), + fun (Props) -> + Id = id_lookup(IdType, Props), + BasicStatsRaw = ets:match(Table, {{{Id, '$1'}, stats}, '$2', '_'}), + BasicStatsDict = dict:from_list([{K, V} || [K,V] <- BasicStatsRaw]), + [{K, Items}] = F(Props), %% [1] + Items2 = [case dict:find(id_lookup(IdType, Item), BasicStatsDict) of + {ok, BasicStats} -> BasicStats ++ Item; + error -> Item + end || Item <- Items], + [{K, Items2}] + end. + +read_simple_stats(Type, Id, #state{aggregated_stats = ETS}) -> + FromETS = ets:match(ETS, {{{Type, Id}, '$1'}, '$2'}), + [{K, V} || [K, V] <- FromETS]. + +read_detail_stats(Type, Id, #state{aggregated_stats = ETS}) -> + %% Id must contain '$1' + FromETS = ets:match(ETS, {{{Type, Id}, '$2'}, '$3'}), + %% [[G, K, V]] -> [{G, [{K, V}]}] where G is Q/X/Ch, K is from + %% ?FINE_STATS and V is a stats tree + %% TODO does this need to be optimised? + lists:foldl( + fun ([G, K, V], L) -> + case lists:keyfind(G, 1, L) of + false -> [{G, [{K, V}]} | L]; + {G, KVs} -> lists:keyreplace(G, 1, L, {G, [{K, V} | KVs]}) + end + end, [], FromETS). + +extract_msg_stats(Stats) -> + FineStats = lists:append([[K, details_key(K)] || K <- ?MSG_RATES]), + {MsgStats, Other} = + lists:partition(fun({K, _}) -> lists:member(K, FineStats) end, Stats), + case MsgStats of + [] -> Other; + _ -> [{message_stats, MsgStats} | Other] + end. + +detail_stats(Ranges, Name, AggregatedStatsType, Id, State) -> + {Name, + [[{stats, format_samples(Ranges, KVs, State)} | format_detail_id(G, State)] + || {G, KVs} <- read_detail_stats(AggregatedStatsType, Id, State)]}. + +format_detail_id(ChPid, State) when is_pid(ChPid) -> + augment_msg_stats([{channel, ChPid}], State); +format_detail_id(#resource{name = Name, virtual_host = Vhost, kind = Kind}, + _State) -> + [{Kind, [{name, Name}, {vhost, Vhost}]}]; +format_detail_id(Node, _State) when is_atom(Node) -> + [{name, Node}]. + +format_samples(Ranges, ManyStats, #state{interval = Interval}) -> + lists:append( + [case rabbit_mgmt_stats:is_blank(Stats) andalso + not lists:member(K, ?ALWAYS_REPORT_STATS) of + true -> []; + false -> {Details, Counter} = rabbit_mgmt_stats:format( + pick_range(K, Ranges), + Stats, Interval), + [{K, Counter}, + {details_key(K), Details}] + end || {K, Stats} <- ManyStats]). + +pick_range(K, {RangeL, RangeM, RangeD, RangeN}) -> + case {lists:member(K, ?QUEUE_MSG_COUNTS), + lists:member(K, ?MSG_RATES), + lists:member(K, ?COARSE_CONN_STATS), + lists:member(K, ?COARSE_NODE_STATS) + orelse lists:member(K, ?COARSE_NODE_NODE_STATS)} of + {true, false, false, false} -> RangeL; + {false, true, false, false} -> RangeM; + {false, false, true, false} -> RangeD; + {false, false, false, true} -> RangeN + end. + +%% We do this when retrieving the queue record rather than when +%% storing it since the memory use will drop *after* we find out about +%% hibernation, so to do it when we receive a queue stats event would +%% be fiddly and racy. This should be quite cheap though. +adjust_hibernated_memory_use(Qs) -> + Pids = [pget(pid, Q) || + Q <- Qs, pget(idle_since, Q, not_idle) =/= not_idle], + %% We use delegate here not for ordering reasons but because we + %% want to get the right amount of parallelism and minimise + %% cross-cluster communication. + {Mem, _BadNodes} = delegate:invoke(Pids, {erlang, process_info, [memory]}), + MemDict = dict:from_list([{P, M} || {P, M = {memory, _}} <- Mem]), + [case dict:find(pget(pid, Q), MemDict) of + error -> Q; + {ok, Memory} -> [Memory|proplists:delete(memory, Q)] + end || Q <- Qs]. + +created_event(Name, Type, Tables) -> + Table = orddict:fetch(Type, Tables), + case ets:match(Table, {{'$1', create}, '_', Name}) of + [] -> not_found; + [[Id]] -> lookup_element(Table, {Id, create}) + end. + +created_events(Type, Tables) -> + [Facts || {{_, create}, Facts, _Name} + <- ets:tab2list(orddict:fetch(Type, Tables))]. + +consumer_details_fun(KeyFun, TableName, State = #state{tables = Tables}) -> + Table = orddict:fetch(TableName, Tables), + fun ([]) -> []; + (Props) -> Pattern = {KeyFun(Props), '_', '_'}, + [{consumer_details, + [augment_msg_stats(augment_consumer(Obj), State) + || Obj <- lists:append( + ets:match(Table, {Pattern, '$1'}))]}] + end. + +augment_consumer(Obj) -> + [{queue, rabbit_mgmt_format:resource(pget(queue, Obj))} | + proplists:delete(queue, Obj)]. + +%%---------------------------------------------------------------------------- +%% Internal, query-time summing for overview +%%---------------------------------------------------------------------------- + +overview_sum(Type, VHostStats) -> + Stats = [pget(Type, VHost, rabbit_mgmt_stats:blank()) + || VHost <- VHostStats], + {Type, rabbit_mgmt_stats:sum(Stats)}. + +%%---------------------------------------------------------------------------- +%% Internal, query-time augmentation +%%---------------------------------------------------------------------------- + +augment_msg_stats(Props, State) -> + rabbit_mgmt_format:strip_pids( + (augment_msg_stats_fun(State))(Props) ++ Props). + +augment_msg_stats_fun(State) -> + Funs = [{connection, fun augment_connection_pid/2}, + {channel, fun augment_channel_pid/2}, + {owner_pid, fun augment_connection_pid/2}], + fun (Props) -> augment(Props, Funs, State) end. + +augment(Items, Funs, State) -> + Augmented = [augment(K, Items, Fun, State) || {K, Fun} <- Funs], + [{K, V} || {K, V} <- Augmented, V =/= unknown]. + +augment(K, Items, Fun, State) -> + Key = details_key(K), + case pget(K, Items) of + none -> {Key, unknown}; + unknown -> {Key, unknown}; + Id -> {Key, Fun(Id, State)} + end. + +augment_channel_pid(Pid, #state{tables = Tables}) -> + Ch = lookup_element(orddict:fetch(channel_stats, Tables), + {Pid, create}), + Conn = lookup_element(orddict:fetch(connection_stats, Tables), + {pget(connection, Ch), create}), + [{name, pget(name, Ch)}, + {number, pget(number, Ch)}, + {user, pget(user, Ch)}, + {connection_name, pget(name, Conn)}, + {peer_port, pget(peer_port, Conn)}, + {peer_host, pget(peer_host, Conn)}]. + +augment_connection_pid(Pid, #state{tables = Tables}) -> + Conn = lookup_element(orddict:fetch(connection_stats, Tables), + {Pid, create}), + [{name, pget(name, Conn)}, + {peer_port, pget(peer_port, Conn)}, + {peer_host, pget(peer_host, Conn)}]. + +%%---------------------------------------------------------------------------- +%% Internal, event-GCing +%%---------------------------------------------------------------------------- + +gc_batch(State = #state{aggregated_stats = ETS}) -> + {ok, Policies} = application:get_env( + rabbitmq_management, sample_retention_policies), + Rows = erlang:max(?GC_MIN_ROWS, + round(?GC_MIN_RATIO * ets:info(ETS, size))), + gc_batch(Rows, Policies, State). + +gc_batch(0, _Policies, State) -> + State; +gc_batch(Rows, Policies, State = #state{aggregated_stats = ETS, + gc_next_key = Key0}) -> + Key = case Key0 of + undefined -> ets:first(ETS); + _ -> ets:next(ETS, Key0) + end, + Key1 = case Key of + '$end_of_table' -> undefined; + _ -> Now = floor(os:timestamp(), State), + Stats = ets:lookup_element(ETS, Key, 2), + gc(Key, Stats, Policies, Now, ETS), + Key + end, + gc_batch(Rows - 1, Policies, State#state{gc_next_key = Key1}). + +gc({{Type, Id}, Key}, Stats, Policies, Now, ETS) -> + Policy = pget(retention_policy(Type), Policies), + case rabbit_mgmt_stats:gc({Policy, Now}, Stats) of + Stats -> ok; + Stats2 -> ets:insert(ETS, {{{Type, Id}, Key}, Stats2}) + end. + +retention_policy(node_stats) -> global; +retention_policy(node_node_stats) -> global; +retention_policy(vhost_stats) -> global; +retention_policy(queue_stats) -> basic; +retention_policy(exchange_stats) -> basic; +retention_policy(connection_stats) -> basic; +retention_policy(channel_stats) -> basic; +retention_policy(queue_exchange_stats) -> detailed; +retention_policy(channel_exchange_stats) -> detailed; +retention_policy(channel_queue_stats) -> detailed. diff --git a/rabbitmq-server/plugins-src/rabbitmq-management/src/rabbit_mgmt_dispatcher.erl b/rabbitmq-server/plugins-src/rabbitmq-management/src/rabbit_mgmt_dispatcher.erl new file mode 100644 index 0000000..e42635e --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-management/src/rabbit_mgmt_dispatcher.erl @@ -0,0 +1,88 @@ +%% The contents of this file are subject to the Mozilla Public License +%% Version 1.1 (the "License"); you may not use this file except in +%% compliance with the License. You may obtain a copy of the License at +%% http://www.mozilla.org/MPL/ +%% +%% Software distributed under the License is distributed on an "AS IS" +%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the +%% License for the specific language governing rights and limitations +%% under the License. +%% +%% The Original Code is RabbitMQ Management Plugin. +%% +%% The Initial Developer of the Original Code is GoPivotal, Inc. +%% Copyright (c) 2010-2015 Pivotal Software, Inc. All rights reserved. +%% + +-module(rabbit_mgmt_dispatcher). + +-export([modules/1, build_dispatcher/1]). + +-behaviour(rabbit_mgmt_extension). +-export([dispatcher/0, web_ui/0]). + +build_dispatcher(Ignore) -> + [{["api" | Path], Mod, Args} || + {Path, Mod, Args} <- + lists:append([Module:dispatcher() || Module <- modules(Ignore)])]. + +modules(IgnoreApps) -> + [Module || {App, Module, Behaviours} <- + rabbit_misc:all_module_attributes(behaviour), + not lists:member(App, IgnoreApps), + lists:member(rabbit_mgmt_extension, Behaviours)]. + +%%---------------------------------------------------------------------------- + +web_ui() -> [{javascript, <<"dispatcher.js">>}]. + +dispatcher() -> + [{["overview"], rabbit_mgmt_wm_overview, []}, + {["cluster-name"], rabbit_mgmt_wm_cluster_name, []}, + {["nodes"], rabbit_mgmt_wm_nodes, []}, + {["nodes", node], rabbit_mgmt_wm_node, []}, + {["extensions"], rabbit_mgmt_wm_extensions, []}, + {["all-configuration"], rabbit_mgmt_wm_definitions, []}, %% This was the old name, let's not break things gratuitously. + {["definitions"], rabbit_mgmt_wm_definitions, []}, + {["parameters"], rabbit_mgmt_wm_parameters, []}, + {["parameters", component], rabbit_mgmt_wm_parameters, []}, + {["parameters", component, vhost], rabbit_mgmt_wm_parameters, []}, + {["parameters", component, vhost, name], rabbit_mgmt_wm_parameter, []}, + {["policies"], rabbit_mgmt_wm_policies, []}, + {["policies", vhost], rabbit_mgmt_wm_policies, []}, + {["policies", vhost, name], rabbit_mgmt_wm_policy, []}, + {["connections"], rabbit_mgmt_wm_connections, []}, + {["connections", connection], rabbit_mgmt_wm_connection, []}, + {["connections", connection, "channels"], rabbit_mgmt_wm_connection_channels, []}, + {["channels"], rabbit_mgmt_wm_channels, []}, + {["channels", channel], rabbit_mgmt_wm_channel, []}, + {["consumers"], rabbit_mgmt_wm_consumers, []}, + {["consumers", vhost], rabbit_mgmt_wm_consumers, []}, + {["exchanges"], rabbit_mgmt_wm_exchanges, []}, + {["exchanges", vhost], rabbit_mgmt_wm_exchanges, []}, + {["exchanges", vhost, exchange], rabbit_mgmt_wm_exchange, []}, + {["exchanges", vhost, exchange, "publish"], rabbit_mgmt_wm_exchange_publish, []}, + {["exchanges", vhost, exchange, "bindings", "source"], rabbit_mgmt_wm_bindings, [exchange_source]}, + {["exchanges", vhost, exchange, "bindings", "destination"], rabbit_mgmt_wm_bindings, [exchange_destination]}, + {["queues"], rabbit_mgmt_wm_queues, []}, + {["queues", vhost], rabbit_mgmt_wm_queues, []}, + {["queues", vhost, queue], rabbit_mgmt_wm_queue, []}, + {["queues", vhost, destination, "bindings"], rabbit_mgmt_wm_bindings, [queue]}, + {["queues", vhost, queue, "contents"], rabbit_mgmt_wm_queue_purge, []}, + {["queues", vhost, queue, "get"], rabbit_mgmt_wm_queue_get, []}, + {["queues", vhost, queue, "actions"], rabbit_mgmt_wm_queue_actions, []}, + {["bindings"], rabbit_mgmt_wm_bindings, [all]}, + {["bindings", vhost], rabbit_mgmt_wm_bindings, [all]}, + {["bindings", vhost, "e", source, dtype, destination], rabbit_mgmt_wm_bindings, [source_destination]}, + {["bindings", vhost, "e", source, dtype, destination, props], rabbit_mgmt_wm_binding, []}, + {["vhosts"], rabbit_mgmt_wm_vhosts, []}, + {["vhosts", vhost], rabbit_mgmt_wm_vhost, []}, + {["vhosts", vhost, "permissions"], rabbit_mgmt_wm_permissions_vhost, []}, + {["users"], rabbit_mgmt_wm_users, []}, + {["users", user], rabbit_mgmt_wm_user, []}, + {["users", user, "permissions"], rabbit_mgmt_wm_permissions_user, []}, + {["whoami"], rabbit_mgmt_wm_whoami, []}, + {["permissions"], rabbit_mgmt_wm_permissions, []}, + {["permissions", vhost, user], rabbit_mgmt_wm_permission, []}, + {["aliveness-test", vhost], rabbit_mgmt_wm_aliveness_test, []} + ]. diff --git a/rabbitmq-server/plugins-src/rabbitmq-management/src/rabbit_mgmt_extension.erl b/rabbitmq-server/plugins-src/rabbitmq-management/src/rabbit_mgmt_extension.erl new file mode 100644 index 0000000..b558c40 --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-management/src/rabbit_mgmt_extension.erl @@ -0,0 +1,32 @@ +%% The contents of this file are subject to the Mozilla Public License +%% Version 1.1 (the "License"); you may not use this file except in +%% compliance with the License. You may obtain a copy of the License at +%% http://www.mozilla.org/MPL/ +%% +%% Software distributed under the License is distributed on an "AS IS" +%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the +%% License for the specific language governing rights and limitations +%% under the License. +%% +%% The Original Code is RabbitMQ Management Plugin. +%% +%% The Initial Developer of the Original Code is GoPivotal, Inc. +%% Copyright (c) 2011-2015 Pivotal Software, Inc. All rights reserved. +%% + +-module(rabbit_mgmt_extension). + +-export([behaviour_info/1]). + +behaviour_info(callbacks) -> + [ + %% Return a webmachine dispatcher table to integrate + {dispatcher, 0}, + + %% Return a proplist of information for the web UI to integrate + %% this extension. Currently the proplist should have one key, + %% 'javascript', the name of a javascript file to load and run. + {web_ui, 0} + ]; +behaviour_info(_Other) -> + undefined. diff --git a/rabbitmq-server/plugins-src/rabbitmq-management/src/rabbit_mgmt_format.erl b/rabbitmq-server/plugins-src/rabbitmq-management/src/rabbit_mgmt_format.erl new file mode 100644 index 0000000..d2a0641 --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-management/src/rabbit_mgmt_format.erl @@ -0,0 +1,327 @@ +%% The contents of this file are subject to the Mozilla Public License +%% Version 1.1 (the "License"); you may not use this file except in +%% compliance with the License. You may obtain a copy of the License at +%% http://www.mozilla.org/MPL/ +%% +%% Software distributed under the License is distributed on an "AS IS" +%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the +%% License for the specific language governing rights and limitations +%% under the License. +%% +%% The Original Code is RabbitMQ Management Plugin. +%% +%% The Initial Developer of the Original Code is GoPivotal, Inc. +%% Copyright (c) 2010-2015 Pivotal Software, Inc. All rights reserved. +%% + +-module(rabbit_mgmt_format). + +-export([format/2, print/2, remove/1, ip/1, ipb/1, amqp_table/1, tuple/1]). +-export([parameter/1, now_to_str/1, now_to_str_ms/1, now_to_ms/1, strip_pids/1]). +-export([node_from_pid/1, protocol/1, resource/1, queue/1, queue_state/1]). +-export([exchange/1, user/1, internal_user/1, binding/1, url/2]). +-export([pack_binding_props/2, tokenise/1]). +-export([to_amqp_table/1, listener/1, properties/1, basic_properties/1]). +-export([record/2, to_basic_properties/1]). +-export([addr/1, port/1]). + +-import(rabbit_misc, [pget/2, pset/3]). + +-include_lib("rabbit_common/include/rabbit.hrl"). +-include_lib("rabbit_common/include/rabbit_framing.hrl"). + +-define(PIDS_TO_STRIP, [connection, owner_pid, channel, + exclusive_consumer_pid]). + +%%-------------------------------------------------------------------- + +format(Stats, Fs) -> + lists:concat([format_item(Stat, Fs) || {_Name, Value} = Stat <- Stats, + Value =/= unknown]). + +format_item(Stat, []) -> + [Stat]; +format_item({Name, Value}, [{Fun, Names} | Fs]) -> + case lists:member(Name, Names) of + true -> case Fun(Value) of + List when is_list(List) -> List; + Formatted -> [{Name, Formatted}] + end; + false -> format_item({Name, Value}, Fs) + end. + +print(Fmt, Val) when is_list(Val) -> + list_to_binary(lists:flatten(io_lib:format(Fmt, Val))); +print(Fmt, Val) -> + print(Fmt, [Val]). + +%% TODO - can we remove all these "unknown" cases? Coverage never hits them. + +remove(_) -> []. + +node_from_pid(Pid) when is_pid(Pid) -> [{node, node(Pid)}]; +node_from_pid('') -> []; +node_from_pid(unknown) -> []; +node_from_pid(none) -> []. + +nodes_from_pids(Name) -> + fun('') -> []; + (Pids) -> [{Name, [node(Pid) || Pid <- Pids]}] + end. + +ip(unknown) -> unknown; +ip(IP) -> list_to_binary(rabbit_misc:ntoa(IP)). + +ipb(unknown) -> unknown; +ipb(IP) -> list_to_binary(rabbit_misc:ntoab(IP)). + +addr(S) when is_list(S); is_atom(S); is_binary(S) -> print("~s", S); +addr(Addr) when is_tuple(Addr) -> ip(Addr). + +port(Port) when is_number(Port) -> Port; +port(Port) -> print("~w", Port). + +properties(unknown) -> unknown; +properties(Table) -> {struct, [{Name, tuple(Value)} || + {Name, Value} <- Table]}. + +amqp_table(unknown) -> unknown; +amqp_table(undefined) -> amqp_table([]); +amqp_table(Table) -> {struct, [{Name, amqp_value(Type, Value)} || + {Name, Type, Value} <- Table]}. + +amqp_value(array, Vs) -> [amqp_value(T, V) || {T, V} <- Vs]; +amqp_value(table, V) -> amqp_table(V); +amqp_value(_Type, V) when is_binary(V) -> utf8_safe(V); +amqp_value(_Type, V) -> V. + +utf8_safe(V) -> + try + xmerl_ucs:from_utf8(V), + V + catch exit:{ucs, _} -> + Enc = split_lines(base64:encode(V)), + <<"Not UTF-8, base64 is: ", Enc/binary>> + end. + +% MIME enforces a limit on line length of base 64-encoded data to 76 characters. +split_lines(<>) -> + <>; +split_lines(Text) -> + Text. + +parameter(P) -> pset(value, rabbit_misc:term_to_json(pget(value, P)), P). + +tuple(unknown) -> unknown; +tuple(Tuple) when is_tuple(Tuple) -> [tuple(E) || E <- tuple_to_list(Tuple)]; +tuple(Term) -> Term. + +protocol(unknown) -> + unknown; +protocol(Version = {_Major, _Minor, _Revision}) -> + protocol({'AMQP', Version}); +protocol({Family, Version}) -> + print("~s ~s", [Family, protocol_version(Version)]). + +protocol_version(Arbitrary) + when is_list(Arbitrary) -> Arbitrary; +protocol_version({Major, Minor}) -> io_lib:format("~B-~B", [Major, Minor]); +protocol_version({Major, Minor, 0}) -> protocol_version({Major, Minor}); +protocol_version({Major, Minor, Revision}) -> io_lib:format("~B-~B-~B", + [Major, Minor, Revision]). + +now_to_ms(unknown) -> + unknown; +now_to_ms(Now) -> + timer:now_diff(Now, {0,0,0}) div 1000. + +now_to_str(unknown) -> + unknown; +now_to_str(Now) -> + {{Y, M, D}, {H, Min, S}} = calendar:now_to_local_time(Now), + print("~w-~2.2.0w-~2.2.0w ~w:~2.2.0w:~2.2.0w", [Y, M, D, H, Min, S]). + +now_to_str_ms(unknown) -> + unknown; +now_to_str_ms(Now = {_, _, Micro}) -> + print("~s:~3.3.0w", [now_to_str(Now), Micro div 1000]). + +resource(unknown) -> unknown; +resource(Res) -> resource(name, Res). + +resource(_, unknown) -> + unknown; +resource(NameAs, #resource{name = Name, virtual_host = VHost}) -> + [{NameAs, Name}, {vhost, VHost}]. + +policy('') -> []; +policy(Policy) -> [{policy, Policy}]. + +internal_user(User) -> + [{name, User#internal_user.username}, + {password_hash, base64:encode(User#internal_user.password_hash)}, + {tags, tags(User#internal_user.tags)}]. + +user(User) -> + [{name, User#user.username}, + {tags, tags(User#user.tags)}]. + +tags(Tags) -> + list_to_binary(string:join([atom_to_list(T) || T <- Tags], ",")). + +listener(#listener{node = Node, protocol = Protocol, + ip_address = IPAddress, port = Port}) -> + [{node, Node}, + {protocol, Protocol}, + {ip_address, ip(IPAddress)}, + {port, Port}]. + +pack_binding_props(<<"">>, []) -> + <<"~">>; +pack_binding_props(Key, []) -> + list_to_binary(quote_binding(Key)); +pack_binding_props(Key, Args) -> + ArgsEnc = rabbit_mgmt_wm_binding:args_hash(Args), + list_to_binary(quote_binding(Key) ++ "~" ++ quote_binding(ArgsEnc)). + +quote_binding(Name) -> + re:replace(mochiweb_util:quote_plus(Name), "~", "%7E", [global]). + +%% Unfortunately string:tokens("foo~~bar", "~"). -> ["foo","bar"], we lose +%% the fact that there's a double ~. +tokenise("") -> + []; +tokenise(Str) -> + Count = string:cspan(Str, "~"), + case length(Str) of + Count -> [Str]; + _ -> [string:sub_string(Str, 1, Count) | + tokenise(string:sub_string(Str, Count + 2))] + end. + +to_amqp_table({struct, T}) -> + to_amqp_table(T); +to_amqp_table(T) -> + [to_amqp_table_row(K, V) || {K, V} <- T]. + +to_amqp_table_row(K, V) -> + {T, V2} = type_val(V), + {K, T, V2}. + +to_amqp_array(L) -> + [type_val(I) || I <- L]. + +type_val({struct, M}) -> {table, to_amqp_table(M)}; +type_val(L) when is_list(L) -> {array, to_amqp_array(L)}; +type_val(X) when is_binary(X) -> {longstr, X}; +type_val(X) when is_integer(X) -> {long, X}; +type_val(X) when is_number(X) -> {double, X}; +type_val(true) -> {bool, true}; +type_val(false) -> {bool, false}; +type_val(null) -> throw({error, null_not_allowed}); +type_val(X) -> throw({error, {unhandled_type, X}}). + +url(Fmt, Vals) -> + print(Fmt, [mochiweb_util:quote_plus(V) || V <- Vals]). + +exchange(X) -> + format(X, [{fun resource/1, [name]}, + {fun amqp_table/1, [arguments]}, + {fun policy/1, [policy]}]). + +%% We get queues using rabbit_amqqueue:list/1 rather than :info_all/1 since +%% the latter wakes up each queue. Therefore we have a record rather than a +%% proplist to deal with. +queue(#amqqueue{name = Name, + durable = Durable, + auto_delete = AutoDelete, + exclusive_owner = ExclusiveOwner, + arguments = Arguments, + pid = Pid, + state = State}) -> + format( + [{name, Name}, + {durable, Durable}, + {auto_delete, AutoDelete}, + {owner_pid, ExclusiveOwner}, + {arguments, Arguments}, + {pid, Pid}, + {state, State}], + [{fun resource/1, [name]}, + {fun amqp_table/1, [arguments]}, + {fun policy/1, [policy]}]). + +queue_state({syncing, Msgs}) -> [{state, syncing}, + {sync_messages, Msgs}]; +queue_state(Status) -> [{state, Status}]. + +%% We get bindings using rabbit_binding:list_*/1 rather than :info_all/1 since +%% there are no per-exchange / queue / etc variants for the latter. Therefore +%% we have a record rather than a proplist to deal with. +binding(#binding{source = S, + key = Key, + destination = D, + args = Args}) -> + format( + [{source, S}, + {destination, D#resource.name}, + {destination_type, D#resource.kind}, + {routing_key, Key}, + {arguments, Args}, + {properties_key, pack_binding_props(Key, Args)}], + [{fun (Res) -> resource(source, Res) end, [source]}, + {fun amqp_table/1, [arguments]}]). + +basic_properties(Props = #'P_basic'{}) -> + Res = record(Props, record_info(fields, 'P_basic')), + format(Res, [{fun amqp_table/1, [headers]}]). + +record(Record, Fields) -> + {Res, _Ix} = lists:foldl(fun (K, {L, Ix}) -> + {case element(Ix, Record) of + undefined -> L; + V -> [{K, V}|L] + end, Ix + 1} + end, {[], 2}, Fields), + Res. + +to_basic_properties({struct, P}) -> + to_basic_properties(P); + +to_basic_properties(Props) -> + E = fun (A, B) -> throw({error, {A, B}}) end, + Fmt = fun (headers, H) -> to_amqp_table(H); + (delivery_mode, V) when is_integer(V) -> V; + (delivery_mode, _V) -> E(not_int,delivery_mode); + (priority, V) when is_integer(V) -> V; + (priority, _V) -> E(not_int, priority); + (timestamp, V) when is_integer(V) -> V; + (timestamp, _V) -> E(not_int, timestamp); + (_, V) when is_binary(V) -> V; + (K, _V) -> E(not_string, K) + end, + {Res, _Ix} = lists:foldl( + fun (K, {P, Ix}) -> + {case proplists:get_value(a2b(K), Props) of + undefined -> P; + V -> setelement(Ix, P, Fmt(K, V)) + end, Ix + 1} + end, {#'P_basic'{}, 2}, + record_info(fields, 'P_basic')), + Res. + +a2b(A) -> + list_to_binary(atom_to_list(A)). + +%% Items can be connections, channels, consumers or queues, hence remove takes +%% various items. +strip_pids(Item = [T | _]) when is_tuple(T) -> + format(Item, + [{fun node_from_pid/1, [pid]}, + {fun remove/1, ?PIDS_TO_STRIP}, + {nodes_from_pids(slave_nodes), [slave_pids]}, + {nodes_from_pids(synchronised_slave_nodes), + [synchronised_slave_pids]} + ]); + +strip_pids(Items) -> [strip_pids(I) || I <- Items]. diff --git a/rabbitmq-server/plugins-src/rabbitmq-management/src/rabbit_mgmt_load_definitions.erl b/rabbitmq-server/plugins-src/rabbitmq-management/src/rabbit_mgmt_load_definitions.erl new file mode 100644 index 0000000..d12f545 --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-management/src/rabbit_mgmt_load_definitions.erl @@ -0,0 +1,48 @@ +%% The contents of this file are subject to the Mozilla Public License +%% Version 1.1 (the "License"); you may not use this file except in +%% compliance with the License. You may obtain a copy of the License at +%% http://www.mozilla.org/MPL/ +%% +%% Software distributed under the License is distributed on an "AS IS" +%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the +%% License for the specific language governing rights and limitations +%% under the License. +%% +%% The Original Code is RabbitMQ Management Plugin. +%% +%% The Initial Developer of the Original Code is GoPivotal, Inc. +%% Copyright (c) 2010-2015 Pivotal Software, Inc. All rights reserved. +%% + +-module(rabbit_mgmt_load_definitions). + +-export([maybe_load_definitions/0]). + +%% We want to A) make sure we apply defintions before being open for +%% business (hence why we don't do this in the mgmt app startup) and +%% B) in fact do it before empty_db_check (so the defaults will not +%% get created if we don't need 'em). + +-rabbit_boot_step({load_definitions, + [{description, "configured definitions"}, + {mfa, {rabbit_mgmt_load_definitions, + maybe_load_definitions, + []}}, + {requires, recovery}, + {enables, empty_db_check}]}). + +maybe_load_definitions() -> + {ok, File} = application:get_env(rabbitmq_management, load_definitions), + case File of + none -> ok; + _ -> case file:read_file(File) of + {ok, Body} -> rabbit_log:info( + "Applying definitions from: ~s~n", [File]), + load_definitions(Body); + {error, E} -> {error, {could_not_read_defs, {File, E}}} + end + end. + +load_definitions(Body) -> + rabbit_mgmt_wm_definitions:apply_defs( + Body, fun () -> ok end, fun (E) -> {error, E} end). diff --git a/rabbitmq-server/plugins-src/rabbitmq-management/src/rabbit_mgmt_reset_handler.erl b/rabbitmq-server/plugins-src/rabbitmq-management/src/rabbit_mgmt_reset_handler.erl new file mode 100644 index 0000000..e16351f --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-management/src/rabbit_mgmt_reset_handler.erl @@ -0,0 +1,86 @@ +%% The contents of this file are subject to the Mozilla Public License +%% Version 1.1 (the "License"); you may not use this file except in +%% compliance with the License. You may obtain a copy of the License at +%% http://www.mozilla.org/MPL/ +%% +%% Software distributed under the License is distributed on an "AS IS" +%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the +%% License for the specific language governing rights and limitations +%% under the License. +%% +%% The Original Code is RabbitMQ Management Console. +%% +%% The Initial Developer of the Original Code is GoPivotal, Inc. +%% Copyright (c) 2010-2015 Pivotal Software, Inc. All rights reserved. +%% + +%% When management extensions are enabled and/or disabled at runtime, the +%% management web dispatch mechanism needs to be reset. This event handler +%% deals with responding to 'plugins_changed' events for management +%% extensions, forcing a reset when necessary. + +-module(rabbit_mgmt_reset_handler). + +-include_lib("rabbit_common/include/rabbit.hrl"). + +-behaviour(gen_event). + +-export([init/1, handle_call/2, handle_event/2, handle_info/2, + terminate/2, code_change/3]). + +-rabbit_boot_step({?MODULE, + [{description, "management extension handling"}, + {mfa, {gen_event, add_handler, + [rabbit_event, ?MODULE, []]}}, + {cleanup, {gen_event, delete_handler, + [rabbit_event, ?MODULE, []]}}, + {requires, rabbit_event}, + {enables, recovery}]}). + +-import(rabbit_misc, [pget/2, pget/3]). + +%%---------------------------------------------------------------------------- + +init([]) -> + {ok, []}. + +handle_call(_Request, State) -> + {ok, not_understood, State}. + +handle_event(#event{type = plugins_changed, props = Details}, State) -> + Enabled = pget(enabled, Details), + Disabled = pget(disabled, Details), + case extensions_changed(Enabled ++ Disabled) of + true -> rabbit_mgmt_app:reset_dispatcher(Disabled); + false -> ok + end, + {ok, State}; + +handle_event(_Event, State) -> + {ok, State}. + +handle_info(_Info, State) -> + {ok, State}. + +terminate(_Arg, _State) -> + ok. + +code_change(_OldVsn, State, _Extra) -> + {ok, State}. + +%%---------------------------------------------------------------------------- + +%% We explicitly ignore the case where management has been +%% started/stopped since the dispatcher is either freshly created or +%% about to vanish. +extensions_changed(Apps) -> + not lists:member(rabbitmq_management, Apps) andalso + lists:any(fun is_extension/1, [Mod || App <- Apps, Mod <- mods(App)]). + +is_extension(Mod) -> + lists:member(rabbit_mgmt_extension, + pget(behaviour, Mod:module_info(attributes), [])). + +mods(App) -> + {ok, Modules} = application:get_key(App, modules), + Modules. diff --git a/rabbitmq-server/plugins-src/rabbitmq-management/src/rabbit_mgmt_stats.erl b/rabbitmq-server/plugins-src/rabbitmq-management/src/rabbit_mgmt_stats.erl new file mode 100644 index 0000000..3e0c8a2 --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-management/src/rabbit_mgmt_stats.erl @@ -0,0 +1,201 @@ +%% The contents of this file are subject to the Mozilla Public License +%% Version 1.1 (the "License"); you may not use this file except in +%% compliance with the License. You may obtain a copy of the License at +%% http://www.mozilla.org/MPL/ +%% +%% Software distributed under the License is distributed on an "AS IS" +%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the +%% License for the specific language governing rights and limitations +%% under the License. +%% +%% The Original Code is RabbitMQ Management Plugin. +%% +%% The Initial Developer of the Original Code is GoPivotal, Inc. +%% Copyright (c) 2010-2012 GoPivotal, Inc. All rights reserved. +%% + +-module(rabbit_mgmt_stats). + +-include("rabbit_mgmt.hrl"). + +-export([blank/0, is_blank/1, record/3, format/3, sum/1, gc/2]). + +-import(rabbit_misc, [pget/2]). + +%%---------------------------------------------------------------------------- + +blank() -> #stats{diffs = gb_trees:empty(), base = 0}. + +is_blank(#stats{diffs = Diffs, base = 0}) -> gb_trees:is_empty(Diffs); +is_blank(#stats{}) -> false. + +%%---------------------------------------------------------------------------- +%% Event-time +%%---------------------------------------------------------------------------- + +record(TS, Diff, Stats = #stats{diffs = Diffs}) -> + Diffs2 = case gb_trees:lookup(TS, Diffs) of + {value, Total} -> gb_trees:update(TS, Diff + Total, Diffs); + none -> gb_trees:insert(TS, Diff, Diffs) + end, + Stats#stats{diffs = Diffs2}. + +%%---------------------------------------------------------------------------- +%% Query-time +%%---------------------------------------------------------------------------- + +format(no_range, #stats{diffs = Diffs, base = Base}, Interval) -> + Now = rabbit_mgmt_format:now_to_ms(os:timestamp()), + RangePoint = ((Now div Interval) * Interval) - Interval, + Count = sum_entire_tree(gb_trees:iterator(Diffs), Base), + {[{rate, format_rate( + Diffs, RangePoint, Interval, Interval)}], Count}; + +format(Range, #stats{diffs = Diffs, base = Base}, Interval) -> + RangePoint = Range#range.last - Interval, + {Samples, Count} = extract_samples( + Range, Base, gb_trees:iterator(Diffs), []), + Part1 = [{rate, format_rate( + Diffs, RangePoint, Range#range.incr, Interval)}, + {samples, Samples}], + Length = length(Samples), + Part2 = case Length > 1 of + true -> [{sample, S2}, {timestamp, T2}] = hd(Samples), + [{sample, S1}, {timestamp, T1}] = lists:last(Samples), + Total = lists:sum([pget(sample, I) || I <- Samples]), + [{avg_rate, (S2 - S1) * 1000 / (T2 - T1)}, + {avg, Total / Length}]; + false -> [] + end, + {Part1 ++ Part2, Count}. + +format_rate(Diffs, RangePoint, Incr, Interval) -> + case nth_largest(Diffs, 2) of + false -> 0.0; + {TS, S} -> case TS - RangePoint of %% [0] + D when D =< Incr andalso D >= 0 -> S * 1000 / Interval; + _ -> 0.0 + end + end. + +%% [0] Only display the rate if it's live - i.e. ((the end of the +%% range) - interval) corresponds to the second to last data point we +%% have. If the end of the range is earlier we have gone silent, if +%% it's later we have been asked for a range back in time (in which +%% case showing the correct instantaneous rate would be quite a faff, +%% and probably unwanted). Why the second to last? Because data is +%% still arriving for the last... +nth_largest(Tree, N) -> + case gb_trees:is_empty(Tree) of + true -> false; + false when N == 1 -> gb_trees:largest(Tree); + false -> {_, _, Tree2} = gb_trees:take_largest(Tree), + nth_largest(Tree2, N - 1) + end. + +sum_entire_tree(Iter, Acc) -> + case gb_trees:next(Iter) of + none -> Acc; + {_TS, S, Iter2} -> sum_entire_tree(Iter2, Acc + S) + end. + +%% What we want to do here is: given the #range{}, provide a set of +%% samples such that we definitely provide a set of samples which +%% covers the exact range requested, despite the fact that we might +%% not have it. We need to spin up over the entire range of the +%% samples we *do* have since they are diff-based (and we convert to +%% absolute values here). +extract_samples(Range = #range{first = Next}, Base, It, Samples) -> + case gb_trees:next(It) of + {TS, S, It2} -> extract_samples1(Range, Base, TS, S, It2, Samples); + none -> extract_samples1(Range, Base, Next, 0, It, Samples) + end. + +extract_samples1(Range = #range{first = Next, last = Last, incr = Incr}, + Base, TS, S, It, Samples) -> + if + %% We've gone over the range. Terminate. + Next > Last -> + {Samples, Base}; + %% We've hit bang on a sample. Record it and move to the next. + Next =:= TS -> + extract_samples(Range#range{first = Next + Incr}, Base + S, It, + append(Base + S, Next, Samples)); + %% We haven't yet hit the beginning of our range. + Next > TS -> + extract_samples(Range, Base + S, It, Samples); + %% We have a valid sample, but we haven't used it up + %% yet. Append it and loop around. + Next < TS -> + extract_samples1(Range#range{first = Next + Incr}, Base, TS, S, It, + append(Base, Next, Samples)) + end. + +append(S, TS, Samples) -> [[{sample, S}, {timestamp, TS}] | Samples]. + +sum([]) -> blank(); + +sum([Stats | StatsN]) -> + lists:foldl( + fun (#stats{diffs = D1, base = B1}, #stats{diffs = D2, base = B2}) -> + #stats{diffs = add_trees(D1, gb_trees:iterator(D2)), + base = B1 + B2} + end, Stats, StatsN). + +add_trees(Tree, It) -> + case gb_trees:next(It) of + none -> Tree; + {K, V, It2} -> add_trees( + case gb_trees:lookup(K, Tree) of + {value, V2} -> gb_trees:update(K, V + V2, Tree); + none -> gb_trees:insert(K, V, Tree) + end, It2) + end. + +%%---------------------------------------------------------------------------- +%% Event-GCing +%%---------------------------------------------------------------------------- + +gc(Cutoff, #stats{diffs = Diffs, base = Base}) -> + List = lists:reverse(gb_trees:to_list(Diffs)), + gc(Cutoff, List, [], Base). + +%% Go through the list, amalgamating all too-old samples with the next +%% newest keepable one [0] (we move samples forward in time since the +%% semantics of a sample is "we had this many x by this time"). If the +%% sample is too old, but would not be too old if moved to a rounder +%% timestamp which does not exist then invent one and move it there +%% [1]. But if it's just outright too old, move it to the base [2]. +gc(_Cutoff, [], Keep, Base) -> + #stats{diffs = gb_trees:from_orddict(Keep), base = Base}; +gc(Cutoff, [H = {TS, S} | T], Keep, Base) -> + {NewKeep, NewBase} = + case keep(Cutoff, TS) of + keep -> {[H | Keep], Base}; + drop -> {Keep, S + Base}; %% [2] + {move, D} when Keep =:= [] -> {[{TS + D, S}], Base}; %% [1] + {move, _} -> [{KTS, KS} | KT] = Keep, + {[{KTS, KS + S} | KT], Base} %% [0] + end, + gc(Cutoff, T, NewKeep, NewBase). + +keep({Policy, Now}, TS) -> + lists:foldl(fun ({AgeSec, DivisorSec}, Action) -> + prefer_action( + Action, + case (Now - TS) =< (AgeSec * 1000) of + true -> DivisorMillis = DivisorSec * 1000, + case TS rem DivisorMillis of + 0 -> keep; + Rem -> {move, DivisorMillis - Rem} + end; + false -> drop + end) + end, drop, Policy). + +prefer_action(keep, _) -> keep; +prefer_action(_, keep) -> keep; +prefer_action({move, A}, {move, B}) -> {move, lists:min([A, B])}; +prefer_action({move, A}, drop) -> {move, A}; +prefer_action(drop, {move, A}) -> {move, A}; +prefer_action(drop, drop) -> drop. diff --git a/rabbitmq-server/plugins-src/rabbitmq-management/src/rabbit_mgmt_sup.erl b/rabbitmq-server/plugins-src/rabbitmq-management/src/rabbit_mgmt_sup.erl new file mode 100644 index 0000000..992ff72 --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-management/src/rabbit_mgmt_sup.erl @@ -0,0 +1,34 @@ +%% The contents of this file are subject to the Mozilla Public License +%% Version 1.1 (the "License"); you may not use this file except in +%% compliance with the License. You may obtain a copy of the License at +%% http://www.mozilla.org/MPL/ +%% +%% Software distributed under the License is distributed on an "AS IS" +%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the +%% License for the specific language governing rights and limitations +%% under the License. +%% +%% The Original Code is RabbitMQ Management Console. +%% +%% The Initial Developer of the Original Code is GoPivotal, Inc. +%% Copyright (c) 2011-2015 Pivotal Software, Inc. All rights reserved. +%% + +-module(rabbit_mgmt_sup). + +-behaviour(mirrored_supervisor). + +-export([init/1]). +-export([start_link/0]). + +-include_lib("rabbit_common/include/rabbit.hrl"). + +init([]) -> + DB = {rabbit_mgmt_db, {rabbit_mgmt_db, start_link, []}, + permanent, ?MAX_WAIT, worker, [rabbit_mgmt_db]}, + {ok, {{one_for_one, 10, 10}, [DB]}}. + +start_link() -> + mirrored_supervisor:start_link( + {local, ?MODULE}, ?MODULE, fun rabbit_misc:execute_mnesia_transaction/1, + ?MODULE, []). diff --git a/rabbitmq-server/plugins-src/rabbitmq-management/src/rabbit_mgmt_sup_sup.erl b/rabbitmq-server/plugins-src/rabbitmq-management/src/rabbit_mgmt_sup_sup.erl new file mode 100644 index 0000000..6758a5d --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-management/src/rabbit_mgmt_sup_sup.erl @@ -0,0 +1,64 @@ +%% The contents of this file are subject to the Mozilla Public License +%% Version 1.1 (the "License"); you may not use this file except in +%% compliance with the License. You may obtain a copy of the License at +%% http://www.mozilla.org/MPL/ +%% +%% Software distributed under the License is distributed on an "AS IS" +%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the +%% License for the specific language governing rights and limitations +%% under the License. +%% +%% The Original Code is RabbitMQ Management Console. +%% +%% The Initial Developer of the Original Code is GoPivotal, Inc. +%% Copyright (c) 2011-2015 Pivotal Software, Inc. All rights reserved. +%% + +-module(rabbit_mgmt_sup_sup). + +%% We want there to be one management database in the cluster, with a +%% globally registered name. So we use mirrored_supervisor for +%% failover (in rabbit_mgmt_sup) and register a global name for the +%% database. +%% +%% Unfortunately it's more complicated than using these things +%% naively. The first problem is that on failover the mirrored +%% supervisor might move the DB to a new node before the global name +%% database notices and removes the old record. In that case starting +%% the new database will fail. +%% +%% The second problem is that after a network partition things get +%% worse. Since mirrored_supervisor uses Mnesia for global shared +%% state, we have effectively two (or more) mirrored_supervisors. But +%% the global name database does not do this, so at least one of them +%% cannot start the management database; so the mirrored supervisor +%% has to die. But what if the admin restarts the partition which +%% contains the management DB? In that case we need to start a new +%% management DB in the winning partition. +%% +%% Rather than try to get mirrored_supervisor to handle this +%% post-partition state we go for a simpler approach: allow the whole +%% mirrored_supervisor to die in the two edge cases above, and +%% whenever we want to call into the mgmt DB we will start it up if it +%% appears not to be there. See rabbit_mgmt_db:safe_call/3 for the +%% code which restarts the DB if necessary. + +-behaviour(supervisor2). + +-export([start_link/0, start_child/0]). +-export([init/1]). + +-include_lib("rabbit_common/include/rabbit.hrl"). + +start_link() -> supervisor2:start_link({local, ?MODULE}, ?MODULE, []). + +start_child() -> supervisor2:start_child( ?MODULE, sup()). + +%%---------------------------------------------------------------------------- + +init([]) -> + {ok, {{one_for_one, 0, 1}, [sup()]}}. + +sup() -> + {rabbit_mgmt_sup, {rabbit_mgmt_sup, start_link, []}, + temporary, ?MAX_WAIT, supervisor, [rabbit_mgmt_sup]}. diff --git a/rabbitmq-server/plugins-src/rabbitmq-management/src/rabbit_mgmt_util.erl b/rabbitmq-server/plugins-src/rabbitmq-management/src/rabbit_mgmt_util.erl new file mode 100644 index 0000000..2a15101 --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-management/src/rabbit_mgmt_util.erl @@ -0,0 +1,618 @@ +%% The contents of this file are subject to the Mozilla Public License +%% Version 1.1 (the "License"); you may not use this file except in +%% compliance with the License. You may obtain a copy of the License at +%% http://www.mozilla.org/MPL/ +%% +%% Software distributed under the License is distributed on an "AS IS" +%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the +%% License for the specific language governing rights and limitations +%% under the License. +%% +%% The Original Code is RabbitMQ Management Plugin. +%% +%% The Initial Developer of the Original Code is GoPivotal, Inc. +%% Copyright (c) 2010-2015 Pivotal Software, Inc. All rights reserved. +%% + +-module(rabbit_mgmt_util). + +%% TODO sort all this out; maybe there's scope for rabbit_mgmt_request? + +-export([is_authorized/2, is_authorized_admin/2, is_authorized_admin/4, + vhost/1]). +-export([is_authorized_vhost/2, is_authorized_user/3, + is_authorized_monitor/2, is_authorized_policies/2]). +-export([bad_request/3, bad_request_exception/4, id/2, parse_bool/1, + parse_int/1]). +-export([with_decode/4, not_found/3, amqp_request/4]). +-export([with_channel/4, with_channel/5]). +-export([props_to_method/2, props_to_method/4]). +-export([all_or_one_vhost/2, http_to_amqp/5, reply/3, filter_vhost/3]). +-export([filter_conn_ch_list/3, filter_user/2, list_login_vhosts/2]). +-export([with_decode/5, decode/1, decode/2, redirect/2, set_resp_header/3, + args/1]). +-export([reply_list/3, reply_list/4, sort_list/2, destination_type/1]). +-export([post_respond/1, columns/1, is_monitor/1]). +-export([list_visible_vhosts/1, b64decode_or_throw/1, no_range/0, range/1, + range_ceil/1, floor/2, ceil/2]). + +-import(rabbit_misc, [pget/2, pget/3]). + +-include("rabbit_mgmt.hrl"). +-include_lib("amqp_client/include/amqp_client.hrl"). + +-include_lib("webmachine/include/wm_reqdata.hrl"). +-include_lib("webmachine/include/wm_reqstate.hrl"). + +-define(FRAMING, rabbit_framing_amqp_0_9_1). + +%%-------------------------------------------------------------------- + +is_authorized(ReqData, Context) -> + is_authorized(ReqData, Context, '', fun(_) -> true end). + +is_authorized_admin(ReqData, Context) -> + is_authorized(ReqData, Context, + <<"Not administrator user">>, + fun(#user{tags = Tags}) -> is_admin(Tags) end). + +is_authorized_admin(ReqData, Context, Username, Password) -> + is_authorized(ReqData, Context, Username, Password, + <<"Not administrator user">>, + fun(#user{tags = Tags}) -> is_admin(Tags) end). + +is_authorized_monitor(ReqData, Context) -> + is_authorized(ReqData, Context, + <<"Not monitor user">>, + fun(#user{tags = Tags}) -> is_monitor(Tags) end). + +is_authorized_vhost(ReqData, Context) -> + is_authorized(ReqData, Context, + <<"User not authorised to access virtual host">>, + fun(User) -> + user_matches_vhost(ReqData, User) + end). + +user_matches_vhost(ReqData, User) -> + case vhost(ReqData) of + not_found -> true; + none -> true; + V -> lists:member(V, list_login_vhosts(User, peersock(ReqData))) + end. + +%% Used for connections / channels. A normal user can only see / delete +%% their own stuff. Monitors can see other users' and delete their +%% own. Admins can do it all. +is_authorized_user(ReqData, Context, Item) -> + is_authorized(ReqData, Context, + <<"User not authorised to access object">>, + fun(#user{username = Username, tags = Tags}) -> + case wrq:method(ReqData) of + 'DELETE' -> is_admin(Tags); + _ -> is_monitor(Tags) + end orelse Username == pget(user, Item) + end). + +%% For policies / parameters. Like is_authorized_vhost but you have to +%% be a policymaker. +is_authorized_policies(ReqData, Context) -> + is_authorized(ReqData, Context, + <<"User not authorised to access object">>, + fun(User = #user{tags = Tags}) -> + is_policymaker(Tags) andalso + user_matches_vhost(ReqData, User) + end). + +is_authorized(ReqData, Context, ErrorMsg, Fun) -> + case rabbit_web_dispatch_util:parse_auth_header( + wrq:get_req_header("authorization", ReqData)) of + [Username, Password] -> + is_authorized(ReqData, Context, Username, Password, ErrorMsg, Fun); + _ -> + {?AUTH_REALM, ReqData, Context} + end. + +is_authorized(ReqData, Context, Username, Password, ErrorMsg, Fun) -> + ErrFun = fun (Msg) -> + rabbit_log:warning("HTTP access denied: user '~s' - ~s~n", + [Username, Msg]), + not_authorised(Msg, ReqData, Context) + end, + case rabbit_access_control:check_user_pass_login(Username, Password) of + {ok, User = #user{tags = Tags}} -> + IP = peer(ReqData), + case rabbit_access_control:check_user_loopback(Username, IP) of + ok -> + case is_mgmt_user(Tags) of + true -> + case Fun(User) of + true -> {true, ReqData, + Context#context{user = User, + password = Password}}; + false -> ErrFun(ErrorMsg) + end; + false -> + ErrFun(<<"Not management user">>) + end; + not_allowed -> + ErrFun(<<"User can only log in via localhost">>) + end; + {refused, _Username, Msg, Args} -> + rabbit_log:warning("HTTP access denied: ~s~n", + [rabbit_misc:format(Msg, Args)]), + not_authorised(<<"Login failed">>, ReqData, Context) + end. + +peer(ReqData) -> + {ok, {IP,_Port}} = peername(peersock(ReqData)), + IP. + +%% We can't use wrq:peer/1 because that trusts X-Forwarded-For. +peersock(ReqData) -> + WMState = ReqData#wm_reqdata.wm_state, + WMState#wm_reqstate.socket. + +%% Like the one in rabbit_net, but we and webmachine have a different +%% way of wrapping +peername(Sock) when is_port(Sock) -> inet:peername(Sock); +peername({ssl, SSL}) -> ssl:peername(SSL). + +vhost(ReqData) -> + case id(vhost, ReqData) of + none -> none; + VHost -> case rabbit_vhost:exists(VHost) of + true -> VHost; + false -> not_found + end + end. + +destination_type(ReqData) -> + case id(dtype, ReqData) of + <<"e">> -> exchange; + <<"q">> -> queue + end. + +reply(Facts, ReqData, Context) -> + reply0(extract_columns(Facts, ReqData), ReqData, Context). + +reply0(Facts, ReqData, Context) -> + ReqData1 = set_resp_header("Cache-Control", "no-cache", ReqData), + try + {mochijson2:encode(Facts), ReqData1, Context} + catch exit:{json_encode, E} -> + Error = iolist_to_binary( + io_lib:format("JSON encode error: ~p", [E])), + Reason = iolist_to_binary( + io_lib:format("While encoding:~n~p", [Facts])), + internal_server_error(Error, Reason, ReqData1, Context) + end. + +reply_list(Facts, ReqData, Context) -> + reply_list(Facts, ["vhost", "name"], ReqData, Context). + +reply_list(Facts, DefaultSorts, ReqData, Context) -> + reply(sort_list( + extract_columns_list(Facts, ReqData), + DefaultSorts, + wrq:get_qs_value("sort", ReqData), + wrq:get_qs_value("sort_reverse", ReqData)), + ReqData, Context). + +sort_list(Facts, Sorts) -> sort_list(Facts, Sorts, undefined, false). + +sort_list(Facts, DefaultSorts, Sort, Reverse) -> + SortList = case Sort of + undefined -> DefaultSorts; + Extra -> [Extra | DefaultSorts] + end, + %% lists:sort/2 is much more expensive than lists:sort/1 + Sorted = [V || {_K, V} <- lists:sort( + [{sort_key(F, SortList), F} || F <- Facts])], + case Reverse of + "true" -> lists:reverse(Sorted); + _ -> Sorted + end. + +sort_key(_Item, []) -> + []; +sort_key(Item, [Sort | Sorts]) -> + [get_dotted_value(Sort, Item) | sort_key(Item, Sorts)]. + +get_dotted_value(Key, Item) -> + Keys = string:tokens(Key, "."), + get_dotted_value0(Keys, Item). + +get_dotted_value0([Key], Item) -> + %% Put "nothing" before everything else, in number terms it usually + %% means 0. + pget_bin(list_to_binary(Key), Item, 0); +get_dotted_value0([Key | Keys], Item) -> + get_dotted_value0(Keys, pget_bin(list_to_binary(Key), Item, [])). + +pget_bin(Key, List, Default) -> + case lists:partition(fun ({K, _V}) -> a2b(K) =:= Key end, List) of + {[{_K, V}], _} -> V; + {[], _} -> Default + end. + +extract_columns(Item, ReqData) -> + extract_column_items(Item, columns(ReqData)). + +extract_columns_list(Items, ReqData) -> + Cols = columns(ReqData), + [extract_column_items(Item, Cols) || Item <- Items]. + +columns(ReqData) -> + case wrq:get_qs_value("columns", ReqData) of + undefined -> all; + Str -> [[list_to_binary(T) || T <- string:tokens(C, ".")] + || C <- string:tokens(Str, ",")] + end. + +extract_column_items(Item, all) -> + Item; +extract_column_items({struct, L}, Cols) -> + extract_column_items(L, Cols); +extract_column_items(Item = [T | _], Cols) when is_tuple(T) -> + [{K, extract_column_items(V, descend_columns(a2b(K), Cols))} || + {K, V} <- Item, want_column(a2b(K), Cols)]; +extract_column_items(L, Cols) when is_list(L) -> + [extract_column_items(I, Cols) || I <- L]; +extract_column_items(O, _Cols) -> + O. + +want_column(_Col, all) -> true; +want_column(Col, Cols) -> lists:any(fun([C|_]) -> C == Col end, Cols). + +descend_columns(_K, []) -> []; +descend_columns( K, [[K] | _Rest]) -> all; +descend_columns( K, [[K | K2] | Rest]) -> [K2 | descend_columns(K, Rest)]; +descend_columns( K, [[_K2 | _ ] | Rest]) -> descend_columns(K, Rest). + +a2b(A) when is_atom(A) -> list_to_binary(atom_to_list(A)); +a2b(B) -> B. + +bad_request(Reason, ReqData, Context) -> + halt_response(400, bad_request, Reason, ReqData, Context). + +not_authorised(Reason, ReqData, Context) -> + halt_response(401, not_authorised, Reason, ReqData, Context). + +not_found(Reason, ReqData, Context) -> + halt_response(404, not_found, Reason, ReqData, Context). + +internal_server_error(Error, Reason, ReqData, Context) -> + rabbit_log:error("~s~n~s~n", [Error, Reason]), + halt_response(500, Error, Reason, ReqData, Context). + +halt_response(Code, Type, Reason, ReqData, Context) -> + Json = {struct, [{error, Type}, + {reason, rabbit_mgmt_format:tuple(Reason)}]}, + ReqData1 = wrq:append_to_response_body(mochijson2:encode(Json), ReqData), + {{halt, Code}, ReqData1, Context}. + +id(Key, ReqData) when Key =:= exchange; + Key =:= source; + Key =:= destination -> + case id0(Key, ReqData) of + <<"amq.default">> -> <<"">>; + Name -> Name + end; +id(Key, ReqData) -> + id0(Key, ReqData). + +id0(Key, ReqData) -> + case orddict:find(Key, wrq:path_info(ReqData)) of + {ok, Id} -> list_to_binary(mochiweb_util:unquote(Id)); + error -> none + end. + +with_decode(Keys, ReqData, Context, Fun) -> + with_decode(Keys, wrq:req_body(ReqData), ReqData, Context, Fun). + +with_decode(Keys, Body, ReqData, Context, Fun) -> + case decode(Keys, Body) of + {error, Reason} -> bad_request(Reason, ReqData, Context); + {ok, Values, JSON} -> try + Fun(Values, JSON) + catch {error, Error} -> + bad_request(Error, ReqData, Context) + end + end. + +decode(Keys, Body) -> + case decode(Body) of + {ok, J0} -> J = [{list_to_atom(binary_to_list(K)), V} || {K, V} <- J0], + Results = [get_or_missing(K, J) || K <- Keys], + case [E || E = {key_missing, _} <- Results] of + [] -> {ok, Results, J}; + Errors -> {error, Errors} + end; + Else -> Else + end. + +decode(<<"">>) -> + {ok, []}; + +decode(Body) -> + try + {struct, J} = mochijson2:decode(Body), + {ok, J} + catch error:_ -> {error, not_json} + end. + +get_or_missing(K, L) -> + case pget(K, L) of + undefined -> {key_missing, K}; + V -> V + end. + +http_to_amqp(MethodName, ReqData, Context, Transformers, Extra) -> + case vhost(ReqData) of + not_found -> + not_found(vhost_not_found, ReqData, Context); + VHost -> + case decode(wrq:req_body(ReqData)) of + {ok, Props} -> + try + Node = case pget(<<"node">>, Props) of + undefined -> node(); + N -> rabbit_nodes:make( + binary_to_list(N)) + end, + amqp_request(VHost, ReqData, Context, Node, + props_to_method( + MethodName, Props, Transformers, Extra)) + catch {error, Error} -> + bad_request(Error, ReqData, Context) + end; + {error, Reason} -> + bad_request(Reason, ReqData, Context) + end + end. + +props_to_method(MethodName, Props, Transformers, Extra) -> + Props1 = [{list_to_atom(binary_to_list(K)), V} || {K, V} <- Props], + props_to_method( + MethodName, rabbit_mgmt_format:format(Props1 ++ Extra, Transformers)). + +props_to_method(MethodName, Props) -> + Props1 = rabbit_mgmt_format:format( + Props, + [{fun (Args) -> [{arguments, args(Args)}] end, [arguments]}]), + FieldNames = ?FRAMING:method_fieldnames(MethodName), + {Res, _Idx} = lists:foldl( + fun (K, {R, Idx}) -> + NewR = case pget(K, Props1) of + undefined -> R; + V -> setelement(Idx, R, V) + end, + {NewR, Idx + 1} + end, {?FRAMING:method_record(MethodName), 2}, + FieldNames), + Res. + +parse_bool(<<"true">>) -> true; +parse_bool(<<"false">>) -> false; +parse_bool(true) -> true; +parse_bool(false) -> false; +parse_bool(undefined) -> undefined; +parse_bool(V) -> throw({error, {not_boolean, V}}). + +parse_int(I) when is_integer(I) -> I; +parse_int(F) when is_number(F) -> trunc(F); +parse_int(S) -> try + list_to_integer(binary_to_list(S)) + catch error:badarg -> + throw({error, {not_integer, S}}) + end. + +amqp_request(VHost, ReqData, Context, Method) -> + amqp_request(VHost, ReqData, Context, node(), Method). + +amqp_request(VHost, ReqData, Context, Node, Method) -> + with_channel(VHost, ReqData, Context, Node, + fun (Ch) -> + amqp_channel:call(Ch, Method), + {true, ReqData, Context} + end). + +with_channel(VHost, ReqData, Context, Fun) -> + with_channel(VHost, ReqData, Context, node(), Fun). + +with_channel(VHost, ReqData, + Context = #context{user = #user {username = Username}, + password = Password}, + Node, Fun) -> + Params = #amqp_params_direct{username = Username, + password = Password, + node = Node, + virtual_host = VHost}, + case amqp_connection:start(Params) of + {ok, Conn} -> + {ok, Ch} = amqp_connection:open_channel(Conn), + try + Fun(Ch) + catch + exit:{{shutdown, + {server_initiated_close, ?NOT_FOUND, Reason}}, _} -> + not_found(Reason, ReqData, Context); + exit:{{shutdown, + {server_initiated_close, ?ACCESS_REFUSED, Reason}}, _} -> + not_authorised(Reason, ReqData, Context); + exit:{{shutdown, {ServerClose, Code, Reason}}, _} + when ServerClose =:= server_initiated_close; + ServerClose =:= server_initiated_hard_close -> + bad_request_exception(Code, Reason, ReqData, Context); + exit:{{shutdown, {connection_closing, + {ServerClose, Code, Reason}}}, _} + when ServerClose =:= server_initiated_close; + ServerClose =:= server_initiated_hard_close -> + bad_request_exception(Code, Reason, ReqData, Context) + after + catch amqp_channel:close(Ch), + catch amqp_connection:close(Conn) + end; + {error, {auth_failure, Msg}} -> + not_authorised(Msg, ReqData, Context); + {error, access_refused} -> + not_authorised(<<"Access refused.">>, ReqData, Context); + {error, {nodedown, N}} -> + bad_request( + list_to_binary( + io_lib:format("Node ~s could not be contacted", [N])), + ReqData, Context) + end. + +bad_request_exception(Code, Reason, ReqData, Context) -> + bad_request(list_to_binary(io_lib:format("~p ~s", [Code, Reason])), + ReqData, Context). + +all_or_one_vhost(ReqData, Fun) -> + case rabbit_mgmt_util:vhost(ReqData) of + none -> lists:append([Fun(V) || V <- rabbit_vhost:list()]); + not_found -> vhost_not_found; + VHost -> Fun(VHost) + end. + +filter_vhost(List, ReqData, Context) -> + VHosts = list_login_vhosts(Context#context.user, peersock(ReqData)), + [I || I <- List, lists:member(pget(vhost, I), VHosts)]. + +filter_user(List, _ReqData, #context{user = User}) -> + filter_user(List, User). + +filter_user(List, #user{username = Username, tags = Tags}) -> + case is_monitor(Tags) of + true -> List; + false -> [I || I <- List, pget(user, I) == Username] + end. + +filter_conn_ch_list(List, ReqData, Context) -> + rabbit_mgmt_format:strip_pids( + filter_user( + case vhost(ReqData) of + none -> List; + VHost -> [I || I <- List, pget(vhost, I) =:= VHost] + end, ReqData, Context)). + +redirect(Location, ReqData) -> + wrq:do_redirect(true, + set_resp_header("Location", + binary_to_list(Location), ReqData)). + +set_resp_header(K, V, ReqData) -> + wrq:set_resp_header(K, strip_crlf(V), ReqData). + +strip_crlf(Str) -> lists:append(string:tokens(Str, "\r\n")). + +args({struct, L}) -> args(L); +args(L) -> rabbit_mgmt_format:to_amqp_table(L). + +%% Make replying to a post look like anything else... +post_respond({true, ReqData, Context}) -> + {true, ReqData, Context}; +post_respond({{halt, Code}, ReqData, Context}) -> + {{halt, Code}, ReqData, Context}; +post_respond({JSON, ReqData, Context}) -> + {true, set_resp_header( + "Content-Type", "application/json", + wrq:append_to_response_body(JSON, ReqData)), Context}. + +is_admin(T) -> intersects(T, [administrator]). +is_policymaker(T) -> intersects(T, [administrator, policymaker]). +is_monitor(T) -> intersects(T, [administrator, monitoring]). +is_mgmt_user(T) -> intersects(T, [administrator, monitoring, policymaker, + management]). + +intersects(A, B) -> lists:any(fun(I) -> lists:member(I, B) end, A). + +%% The distinction between list_visible_vhosts and list_login_vhosts +%% is there to ensure that admins / monitors can always learn of the +%% existence of all vhosts, and can always see their contribution to +%% global stats. However, if an admin / monitor does not have any +%% permissions for a vhost, it's probably less confusing to make that +%% prevent them from seeing "into" it, than letting them see stuff +%% that they then can't touch. + +list_visible_vhosts(User = #user{tags = Tags}) -> + case is_monitor(Tags) of + true -> rabbit_vhost:list(); + false -> list_login_vhosts(User, undefined) + end. + +list_login_vhosts(User, Sock) -> + [V || V <- rabbit_vhost:list(), + case catch rabbit_access_control:check_vhost_access(User, V, Sock) of + ok -> true; + _ -> false + end]. + +%% Wow, base64:decode throws lots of weird errors. Catch and convert to one +%% that will cause a bad_request. +b64decode_or_throw(B64) -> + try + base64:decode(B64) + catch error:_ -> + throw({error, {not_base64, B64}}) + end. + +no_range() -> {no_range, no_range, no_range, no_range}. + +%% Take floor on queries so we make sure we only return samples +%% for which we've finished receiving events. Fixes the "drop at +%% the end" problem. +range(ReqData) -> {range("lengths", fun floor/2, ReqData), + range("msg_rates", fun floor/2, ReqData), + range("data_rates", fun floor/2, ReqData), + range("node_stats", fun floor/2, ReqData)}. + +%% ...but if we know only one event could have contributed towards +%% what we are interested in, then let's take the ceiling instead and +%% get slightly fresher data that will match up with any +%% non-historical data we have (e.g. queue length vs queue messages in +%% RAM, they should both come from the same snapshot or we might +%% report more messages in RAM than total). +%% +%% However, we only do this for queue lengths since a) it's the only +%% thing where this ends up being really glaring and b) for other +%% numbers we care more about the rate than the absolute value, and if +%% we use ceil() we stand a 50:50 chance of looking up the last sample +%% in the range before we get it, and thus deriving an instantaneous +%% rate of 0.0. +range_ceil(ReqData) -> {range("lengths", fun ceil/2, ReqData), + range("msg_rates", fun floor/2, ReqData), + range("data_rates", fun floor/2, ReqData), + range("node_stats", fun floor/2, ReqData)}. + +range(Prefix, Round, ReqData) -> + Age0 = int(Prefix ++ "_age", ReqData), + Incr0 = int(Prefix ++ "_incr", ReqData), + if + is_integer(Age0) andalso is_integer(Incr0) -> + Age = Age0 * 1000, + Incr = Incr0 * 1000, + Now = rabbit_mgmt_format:now_to_ms(os:timestamp()), + Last = Round(Now, Incr), + #range{first = (Last - Age), + last = Last, + incr = Incr}; + true -> + no_range + end. + +floor(TS, Interval) -> (TS div Interval) * Interval. + +ceil(TS, Interval) -> case floor(TS, Interval) of + TS -> TS; + Floor -> Floor + Interval + end. + +int(Name, ReqData) -> + case wrq:get_qs_value(Name, ReqData) of + undefined -> undefined; + Str -> case catch list_to_integer(Str) of + {'EXIT', _} -> undefined; + Integer -> Integer + end + end. diff --git a/rabbitmq-server/plugins-src/rabbitmq-management/src/rabbit_mgmt_wm_aliveness_test.erl b/rabbitmq-server/plugins-src/rabbitmq-management/src/rabbit_mgmt_wm_aliveness_test.erl new file mode 100644 index 0000000..fec6196 --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-management/src/rabbit_mgmt_wm_aliveness_test.erl @@ -0,0 +1,59 @@ +%% The contents of this file are subject to the Mozilla Public License +%% Version 1.1 (the "License"); you may not use this file except in +%% compliance with the License. You may obtain a copy of the License at +%% http://www.mozilla.org/MPL/ +%% +%% Software distributed under the License is distributed on an "AS IS" +%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the +%% License for the specific language governing rights and limitations +%% under the License. +%% +%% The Original Code is RabbitMQ Management Plugin. +%% +%% The Initial Developer of the Original Code is GoPivotal, Inc. +%% Copyright (c) 2010-2015 Pivotal Software, Inc. All rights reserved. +%% + +-module(rabbit_mgmt_wm_aliveness_test). + +-export([init/1, to_json/2, content_types_provided/2, is_authorized/2]). +-export([resource_exists/2]). + +-include("rabbit_mgmt.hrl"). +-include_lib("webmachine/include/webmachine.hrl"). +-include_lib("amqp_client/include/amqp_client.hrl"). + +-define(QUEUE, <<"aliveness-test">>). + +%%-------------------------------------------------------------------- + +init(_Config) -> {ok, #context{}}. + +content_types_provided(ReqData, Context) -> + {[{"application/json", to_json}], ReqData, Context}. + +resource_exists(ReqData, Context) -> + {case rabbit_mgmt_util:vhost(ReqData) of + not_found -> false; + _ -> true + end, ReqData, Context}. + +to_json(ReqData, Context) -> + rabbit_mgmt_util:with_channel( + rabbit_mgmt_util:vhost(ReqData), ReqData, Context, + fun(Ch) -> + amqp_channel:call(Ch, #'queue.declare'{queue = ?QUEUE}), + amqp_channel:call(Ch, #'basic.publish'{routing_key = ?QUEUE}, + #amqp_msg{payload = <<"test_message">>}), + {#'basic.get_ok'{}, _} = + amqp_channel:call(Ch, #'basic.get'{queue = ?QUEUE, + no_ack = true}), + %% Don't delete the queue. If this is pinged every few + %% seconds we don't want to create a mnesia transaction + %% each time. + rabbit_mgmt_util:reply([{status, ok}], ReqData, Context) + end). + +is_authorized(ReqData, Context) -> + rabbit_mgmt_util:is_authorized_vhost(ReqData, Context). + diff --git a/rabbitmq-server/plugins-src/rabbitmq-management/src/rabbit_mgmt_wm_binding.erl b/rabbitmq-server/plugins-src/rabbitmq-management/src/rabbit_mgmt_wm_binding.erl new file mode 100644 index 0000000..16e42ae --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-management/src/rabbit_mgmt_wm_binding.erl @@ -0,0 +1,137 @@ +%% The contents of this file are subject to the Mozilla Public License +%% Version 1.1 (the "License"); you may not use this file except in +%% compliance with the License. You may obtain a copy of the License at +%% http://www.mozilla.org/MPL/ +%% +%% Software distributed under the License is distributed on an "AS IS" +%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the +%% License for the specific language governing rights and limitations +%% under the License. +%% +%% The Original Code is RabbitMQ Management Plugin. +%% +%% The Initial Developer of the Original Code is GoPivotal, Inc. +%% Copyright (c) 2010-2015 Pivotal Software, Inc. All rights reserved. +%% + +-module(rabbit_mgmt_wm_binding). + +-export([init/1, resource_exists/2, to_json/2, + content_types_provided/2, content_types_accepted/2, + is_authorized/2, allowed_methods/2, delete_resource/2, + args_hash/1]). + +-include("rabbit_mgmt.hrl"). +-include_lib("webmachine/include/webmachine.hrl"). +-include_lib("amqp_client/include/amqp_client.hrl"). + +%%-------------------------------------------------------------------- +init(_Config) -> {ok, #context{}}. + +content_types_provided(ReqData, Context) -> + {[{"application/json", to_json}], ReqData, Context}. + +content_types_accepted(ReqData, Context) -> + {[{"application/json", accept_content}], ReqData, Context}. + +allowed_methods(ReqData, Context) -> + {['HEAD', 'GET', 'DELETE'], ReqData, Context}. + +resource_exists(ReqData, Context) -> + Binding = binding(ReqData), + {case Binding of + not_found -> false; + {bad_request, _} -> false; + _ -> case rabbit_binding:exists(Binding) of + true -> true; + _ -> false + end + end, ReqData, Context}. + +to_json(ReqData, Context) -> + with_binding(ReqData, Context, + fun(Binding) -> + rabbit_mgmt_util:reply( + rabbit_mgmt_format:binding(Binding), + ReqData, Context) + end). + +delete_resource(ReqData, Context) -> + MethodName = case rabbit_mgmt_util:destination_type(ReqData) of + exchange -> 'exchange.unbind'; + queue -> 'queue.unbind' + end, + sync_resource(MethodName, ReqData, Context). + +is_authorized(ReqData, Context) -> + rabbit_mgmt_util:is_authorized_vhost(ReqData, Context). + +%%-------------------------------------------------------------------- + +binding(ReqData) -> + case rabbit_mgmt_util:vhost(ReqData) of + not_found -> not_found; + VHost -> Source = rabbit_mgmt_util:id(source, ReqData), + Dest = rabbit_mgmt_util:id(destination, ReqData), + DestType = rabbit_mgmt_util:destination_type(ReqData), + Props = rabbit_mgmt_util:id(props, ReqData), + SName = rabbit_misc:r(VHost, exchange, Source), + DName = rabbit_misc:r(VHost, DestType, Dest), + case unpack(SName, DName, Props) of + {bad_request, Str} -> + {bad_request, Str}; + {Key, Args} -> + #binding{ source = SName, + destination = DName, + key = Key, + args = Args } + end + end. + +unpack(Src, Dst, Props) -> + case rabbit_mgmt_format:tokenise(binary_to_list(Props)) of + ["~"] -> {<<>>, []}; + [Key] -> {unquote(Key), []}; + ["~", ArgsEnc] -> lookup(<<>>, ArgsEnc, Src, Dst); + [Key, ArgsEnc] -> lookup(unquote(Key), ArgsEnc, Src, Dst); + _ -> {bad_request, {too_many_tokens, Props}} + end. + +lookup(RoutingKey, ArgsEnc, Src, Dst) -> + lookup(RoutingKey, unquote(ArgsEnc), + rabbit_binding:list_for_source_and_destination(Src, Dst)). + +lookup(_RoutingKey, _Hash, []) -> + {bad_request, "binding not found"}; +lookup(RoutingKey, Hash, [#binding{args = Args} | Rest]) -> + case args_hash(Args) =:= Hash of + true -> {RoutingKey, Args}; + false -> lookup(RoutingKey, Hash, Rest) + end. + +args_hash(Args) -> + list_to_binary(rabbit_misc:base64url(erlang:md5(term_to_binary(Args)))). + +unquote(Name) -> + list_to_binary(mochiweb_util:unquote(Name)). + +with_binding(ReqData, Context, Fun) -> + case binding(ReqData) of + {bad_request, Reason} -> + rabbit_mgmt_util:bad_request(Reason, ReqData, Context); + Binding -> + Fun(Binding) + end. + +sync_resource(MethodName, ReqData, Context) -> + with_binding( + ReqData, Context, + fun(Binding) -> + Props0 = rabbit_mgmt_format:binding(Binding), + Props = Props0 ++ + [{exchange, proplists:get_value(source, Props0)}, + {queue, proplists:get_value(destination, Props0)}], + rabbit_mgmt_util:amqp_request( + rabbit_mgmt_util:vhost(ReqData), ReqData, Context, + rabbit_mgmt_util:props_to_method(MethodName, Props)) + end). diff --git a/rabbitmq-server/plugins-src/rabbitmq-management/src/rabbit_mgmt_wm_bindings.erl b/rabbitmq-server/plugins-src/rabbitmq-management/src/rabbit_mgmt_wm_bindings.erl new file mode 100644 index 0000000..4f80778 --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-management/src/rabbit_mgmt_wm_bindings.erl @@ -0,0 +1,136 @@ +%% The contents of this file are subject to the Mozilla Public License +%% Version 1.1 (the "License"); you may not use this file except in +%% compliance with the License. You may obtain a copy of the License at +%% http://www.mozilla.org/MPL/ +%% +%% Software distributed under the License is distributed on an "AS IS" +%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the +%% License for the specific language governing rights and limitations +%% under the License. +%% +%% The Original Code is RabbitMQ Management Plugin. +%% +%% The Initial Developer of the Original Code is GoPivotal, Inc. +%% Copyright (c) 2010-2015 Pivotal Software, Inc. All rights reserved. +%% + +-module(rabbit_mgmt_wm_bindings). + +-export([init/1, to_json/2, content_types_provided/2, is_authorized/2]). +-export([allowed_methods/2, post_is_create/2, create_path/2]). +-export([content_types_accepted/2, accept_content/2, resource_exists/2]). +-export([basic/1, augmented/2]). + +-include("rabbit_mgmt.hrl"). +-include_lib("webmachine/include/webmachine.hrl"). +-include_lib("amqp_client/include/amqp_client.hrl"). + +%%-------------------------------------------------------------------- + +init([Mode]) -> + {ok, {Mode, #context{}}}. + +content_types_provided(ReqData, Context) -> + {[{"application/json", to_json}], ReqData, Context}. + +resource_exists(ReqData, {Mode, Context}) -> + {case list_bindings(Mode, ReqData) of + vhost_not_found -> false; + _ -> true + end, ReqData, {Mode, Context}}. + +content_types_accepted(ReqData, Context) -> + {[{"application/json", accept_content}], ReqData, Context}. + +allowed_methods(ReqData, {Mode, Context}) -> + {case Mode of + source_destination -> ['HEAD', 'GET', 'POST']; + _ -> ['HEAD', 'GET'] + end, ReqData, {Mode, Context}}. + +post_is_create(ReqData, Context) -> + {true, ReqData, Context}. + +to_json(ReqData, {Mode, Context}) -> + Bs = [rabbit_mgmt_format:binding(B) || B <- list_bindings(Mode, ReqData)], + rabbit_mgmt_util:reply_list( + rabbit_mgmt_util:filter_vhost(Bs, ReqData, Context), + ["vhost", "source", "type", "destination", + "routing_key", "properties_key"], + ReqData, {Mode, Context}). + +create_path(ReqData, Context) -> + {"dummy", ReqData, Context}. + +accept_content(ReqData, {_Mode, Context}) -> + Source = rabbit_mgmt_util:id(source, ReqData), + Dest = rabbit_mgmt_util:id(destination, ReqData), + DestType = rabbit_mgmt_util:id(dtype, ReqData), + VHost = rabbit_mgmt_util:vhost(ReqData), + {ok, Props} = rabbit_mgmt_util:decode(wrq:req_body(ReqData)), + {Method, Key, Args} = method_key_args(DestType, Source, Dest, Props), + Response = rabbit_mgmt_util:amqp_request(VHost, ReqData, Context, Method), + case Response of + {{halt, _}, _, _} = Res -> + Res; + {true, ReqData, Context2} -> + Loc = rabbit_web_dispatch_util:relativise( + wrq:path(ReqData), + binary_to_list( + rabbit_mgmt_format:url( + "/api/bindings/~s/e/~s/~s/~s/~s", + [VHost, Source, DestType, Dest, + rabbit_mgmt_format:pack_binding_props(Key, Args)]))), + {true, rabbit_mgmt_util:set_resp_header("Location", Loc, ReqData), + Context2} + end. + +is_authorized(ReqData, {Mode, Context}) -> + {Res, RD2, C2} = rabbit_mgmt_util:is_authorized_vhost(ReqData, Context), + {Res, RD2, {Mode, C2}}. + +%%-------------------------------------------------------------------- + +basic(ReqData) -> + [rabbit_mgmt_format:binding(B) || + B <- list_bindings(all, ReqData)]. + +augmented(ReqData, Context) -> + rabbit_mgmt_util:filter_vhost(basic(ReqData), ReqData, Context). + +method_key_args(<<"q">>, Source, Dest, Props) -> + M = #'queue.bind'{routing_key = K, arguments = A} = + rabbit_mgmt_util:props_to_method( + 'queue.bind', Props, + [], [{exchange, Source}, {queue, Dest}]), + {M, K, A}; + +method_key_args(<<"e">>, Source, Dest, Props) -> + M = #'exchange.bind'{routing_key = K, arguments = A} = + rabbit_mgmt_util:props_to_method( + 'exchange.bind', Props, + [], [{source, Source}, {destination, Dest}]), + {M, K, A}. + +%%-------------------------------------------------------------------- + +list_bindings(all, ReqData) -> + rabbit_mgmt_util:all_or_one_vhost(ReqData, + fun (VHost) -> + rabbit_binding:list(VHost) + end); +list_bindings(exchange_source, ReqData) -> + rabbit_binding:list_for_source(r(exchange, exchange, ReqData)); +list_bindings(exchange_destination, ReqData) -> + rabbit_binding:list_for_destination(r(exchange, exchange, ReqData)); +list_bindings(queue, ReqData) -> + rabbit_binding:list_for_destination(r(queue, destination, ReqData)); +list_bindings(source_destination, ReqData) -> + DestType = rabbit_mgmt_util:destination_type(ReqData), + rabbit_binding:list_for_source_and_destination( + r(exchange, source, ReqData), + r(DestType, destination, ReqData)). + +r(Type, Name, ReqData) -> + rabbit_misc:r(rabbit_mgmt_util:vhost(ReqData), Type, + rabbit_mgmt_util:id(Name, ReqData)). diff --git a/rabbitmq-server/plugins-src/rabbitmq-management/src/rabbit_mgmt_wm_channel.erl b/rabbitmq-server/plugins-src/rabbitmq-management/src/rabbit_mgmt_wm_channel.erl new file mode 100644 index 0000000..6410433 --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-management/src/rabbit_mgmt_wm_channel.erl @@ -0,0 +1,51 @@ +%% The contents of this file are subject to the Mozilla Public License +%% Version 1.1 (the "License"); you may not use this file except in +%% compliance with the License. You may obtain a copy of the License at +%% http://www.mozilla.org/MPL/ +%% +%% Software distributed under the License is distributed on an "AS IS" +%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the +%% License for the specific language governing rights and limitations +%% under the License. +%% +%% The Original Code is RabbitMQ Management Plugin. +%% +%% The Initial Developer of the Original Code is GoPivotal, Inc. +%% Copyright (c) 2010-2015 Pivotal Software, Inc. All rights reserved. +%% + +-module(rabbit_mgmt_wm_channel). + +-export([init/1, to_json/2, content_types_provided/2, is_authorized/2]). +-export([resource_exists/2]). + +-include("rabbit_mgmt.hrl"). +-include_lib("webmachine/include/webmachine.hrl"). +-include_lib("rabbit_common/include/rabbit.hrl"). + +%%-------------------------------------------------------------------- + +init(_Config) -> {ok, #context{}}. + +content_types_provided(ReqData, Context) -> + {[{"application/json", to_json}], ReqData, Context}. + +resource_exists(ReqData, Context) -> + case channel(ReqData) of + not_found -> {false, ReqData, Context}; + _Conn -> {true, ReqData, Context} + end. + +to_json(ReqData, Context) -> + rabbit_mgmt_util:reply( + {struct, rabbit_mgmt_format:strip_pids(channel(ReqData))}, + ReqData, Context). + +is_authorized(ReqData, Context) -> + rabbit_mgmt_util:is_authorized_user(ReqData, Context, channel(ReqData)). + +%%-------------------------------------------------------------------- + +channel(ReqData) -> + rabbit_mgmt_db:get_channel(rabbit_mgmt_util:id(channel, ReqData), + rabbit_mgmt_util:range(ReqData)). diff --git a/rabbitmq-server/plugins-src/rabbitmq-management/src/rabbit_mgmt_wm_channels.erl b/rabbitmq-server/plugins-src/rabbitmq-management/src/rabbit_mgmt_wm_channels.erl new file mode 100644 index 0000000..6675fa2 --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-management/src/rabbit_mgmt_wm_channels.erl @@ -0,0 +1,44 @@ +%% The contents of this file are subject to the Mozilla Public License +%% Version 1.1 (the "License"); you may not use this file except in +%% compliance with the License. You may obtain a copy of the License at +%% http://www.mozilla.org/MPL/ +%% +%% Software distributed under the License is distributed on an "AS IS" +%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the +%% License for the specific language governing rights and limitations +%% under the License. +%% +%% The Original Code is RabbitMQ Management Plugin. +%% +%% The Initial Developer of the Original Code is GoPivotal, Inc. +%% Copyright (c) 2010-2015 Pivotal Software, Inc. All rights reserved. +%% + +-module(rabbit_mgmt_wm_channels). + +-export([init/1, to_json/2, content_types_provided/2, is_authorized/2, + augmented/2]). + +-import(rabbit_misc, [pget/2]). + +-include("rabbit_mgmt.hrl"). +-include_lib("webmachine/include/webmachine.hrl"). +-include_lib("rabbit_common/include/rabbit.hrl"). + +%%-------------------------------------------------------------------- + +init(_Config) -> {ok, #context{}}. + +content_types_provided(ReqData, Context) -> + {[{"application/json", to_json}], ReqData, Context}. + +to_json(ReqData, Context) -> + rabbit_mgmt_util:reply_list(augmented(ReqData, Context), ReqData, Context). + +is_authorized(ReqData, Context) -> + rabbit_mgmt_util:is_authorized(ReqData, Context). + +augmented(ReqData, Context) -> + rabbit_mgmt_util:filter_conn_ch_list( + rabbit_mgmt_db:get_all_channels( + rabbit_mgmt_util:range(ReqData)), ReqData, Context). diff --git a/rabbitmq-server/plugins-src/rabbitmq-management/src/rabbit_mgmt_wm_cluster_name.erl b/rabbitmq-server/plugins-src/rabbitmq-management/src/rabbit_mgmt_wm_cluster_name.erl new file mode 100644 index 0000000..f741821 --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-management/src/rabbit_mgmt_wm_cluster_name.erl @@ -0,0 +1,57 @@ +%% The contents of this file are subject to the Mozilla Public License +%% Version 1.1 (the "License"); you may not use this file except in +%% compliance with the License. You may obtain a copy of the License at +%% http://www.mozilla.org/MPL/ +%% +%% Software distributed under the License is distributed on an "AS IS" +%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the +%% License for the specific language governing rights and limitations +%% under the License. +%% +%% The Original Code is RabbitMQ Management Plugin. +%% +%% The Initial Developer of the Original Code is GoPivotal, Inc. +%% Copyright (c) 2010-2015 Pivotal Software, Inc. All rights reserved. +%% + +-module(rabbit_mgmt_wm_cluster_name). + +-export([init/1, resource_exists/2, to_json/2, + content_types_provided/2, content_types_accepted/2, + is_authorized/2, allowed_methods/2, accept_content/2]). + +-include("rabbit_mgmt.hrl"). +-include_lib("webmachine/include/webmachine.hrl"). +-include_lib("amqp_client/include/amqp_client.hrl"). + +%%-------------------------------------------------------------------- +init(_Config) -> {ok, #context{}}. + +content_types_provided(ReqData, Context) -> + {[{"application/json", to_json}], ReqData, Context}. + +content_types_accepted(ReqData, Context) -> + {[{"application/json", accept_content}], ReqData, Context}. + +allowed_methods(ReqData, Context) -> + {['HEAD', 'GET', 'PUT'], ReqData, Context}. + +resource_exists(ReqData, Context) -> + {true, ReqData, Context}. + +to_json(ReqData, Context) -> + rabbit_mgmt_util:reply( + [{name, rabbit_nodes:cluster_name()}], ReqData, Context). + +accept_content(ReqData, Context) -> + rabbit_mgmt_util:with_decode( + [name], ReqData, Context, fun([Name], _) -> + rabbit_nodes:set_cluster_name(Name), + {true, ReqData, Context} + end). + +is_authorized(ReqData, Context) -> + case wrq:method(ReqData) of + 'PUT' -> rabbit_mgmt_util:is_authorized_admin(ReqData, Context); + _ -> rabbit_mgmt_util:is_authorized(ReqData, Context) + end. diff --git a/rabbitmq-server/plugins-src/rabbitmq-management/src/rabbit_mgmt_wm_connection.erl b/rabbitmq-server/plugins-src/rabbitmq-management/src/rabbit_mgmt_wm_connection.erl new file mode 100644 index 0000000..523bec2 --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-management/src/rabbit_mgmt_wm_connection.erl @@ -0,0 +1,66 @@ +%% The contents of this file are subject to the Mozilla Public License +%% Version 1.1 (the "License"); you may not use this file except in +%% compliance with the License. You may obtain a copy of the License at +%% http://www.mozilla.org/MPL/ +%% +%% Software distributed under the License is distributed on an "AS IS" +%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the +%% License for the specific language governing rights and limitations +%% under the License. +%% +%% The Original Code is RabbitMQ Management Plugin. +%% +%% The Initial Developer of the Original Code is GoPivotal, Inc. +%% Copyright (c) 2010-2015 Pivotal Software, Inc. All rights reserved. +%% + +-module(rabbit_mgmt_wm_connection). + +-export([init/1, resource_exists/2, to_json/2, content_types_provided/2, + is_authorized/2, allowed_methods/2, delete_resource/2, conn/1]). + +-include("rabbit_mgmt.hrl"). +-include_lib("webmachine/include/webmachine.hrl"). +-include_lib("rabbit_common/include/rabbit.hrl"). + +%%-------------------------------------------------------------------- + +init(_Config) -> {ok, #context{}}. + +content_types_provided(ReqData, Context) -> + {[{"application/json", to_json}], ReqData, Context}. + +allowed_methods(ReqData, Context) -> + {['HEAD', 'GET', 'DELETE'], ReqData, Context}. + +resource_exists(ReqData, Context) -> + case conn(ReqData) of + not_found -> {false, ReqData, Context}; + _Conn -> {true, ReqData, Context} + end. + +to_json(ReqData, Context) -> + rabbit_mgmt_util:reply( + {struct, rabbit_mgmt_format:strip_pids(conn(ReqData))}, ReqData, Context). + +delete_resource(ReqData, Context) -> + Conn = conn(ReqData), + Pid = proplists:get_value(pid, Conn), + Reason = case wrq:get_req_header(<<"X-Reason">>, ReqData) of + undefined -> "Closed via management plugin"; + V -> V + end, + case proplists:get_value(type, Conn) of + direct -> amqp_direct_connection:server_close(Pid, 320, Reason); + network -> rabbit_networking:close_connection(Pid, Reason) + end, + {true, ReqData, Context}. + +is_authorized(ReqData, Context) -> + rabbit_mgmt_util:is_authorized_user(ReqData, Context, conn(ReqData)). + +%%-------------------------------------------------------------------- + +conn(ReqData) -> + rabbit_mgmt_db:get_connection(rabbit_mgmt_util:id(connection, ReqData), + rabbit_mgmt_util:range_ceil(ReqData)). diff --git a/rabbitmq-server/plugins-src/rabbitmq-management/src/rabbit_mgmt_wm_connection_channels.erl b/rabbitmq-server/plugins-src/rabbitmq-management/src/rabbit_mgmt_wm_connection_channels.erl new file mode 100644 index 0000000..9becc91 --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-management/src/rabbit_mgmt_wm_connection_channels.erl @@ -0,0 +1,54 @@ +%% The contents of this file are subject to the Mozilla Public License +%% Version 1.1 (the "License"); you may not use this file except in +%% compliance with the License. You may obtain a copy of the License at +%% http://www.mozilla.org/MPL/ +%% +%% Software distributed under the License is distributed on an "AS IS" +%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the +%% License for the specific language governing rights and limitations +%% under the License. +%% +%% The Original Code is RabbitMQ Management Plugin. +%% +%% The Initial Developer of the Original Code is GoPivotal, Inc. +%% Copyright (c) 2010-2015 Pivotal Software, Inc. All rights reserved. +%% + +-module(rabbit_mgmt_wm_connection_channels). + +-export([init/1, to_json/2, content_types_provided/2, is_authorized/2]). +-export([resource_exists/2]). + +-include("rabbit_mgmt.hrl"). +-include_lib("webmachine/include/webmachine.hrl"). +-include_lib("rabbit_common/include/rabbit.hrl"). + +%%-------------------------------------------------------------------- + +init(_Config) -> {ok, #context{}}. + +content_types_provided(ReqData, Context) -> + {[{"application/json", to_json}], ReqData, Context}. + +resource_exists(ReqData, Context) -> + case rabbit_mgmt_wm_connection:conn(ReqData) of + error -> {false, ReqData, Context}; + _Conn -> {true, ReqData, Context} + end. + +to_json(ReqData, Context) -> + Name = proplists:get_value(name, rabbit_mgmt_wm_connection:conn(ReqData)), + Chs = rabbit_mgmt_db:get_all_channels(rabbit_mgmt_util:range(ReqData)), + rabbit_mgmt_util:reply_list( + [Ch || Ch <- rabbit_mgmt_util:filter_conn_ch_list(Chs, ReqData, Context), + conn_name(Ch) =:= Name], + ReqData, Context). + +is_authorized(ReqData, Context) -> + rabbit_mgmt_util:is_authorized_user( + ReqData, Context, rabbit_mgmt_wm_connection:conn(ReqData)). + +%%-------------------------------------------------------------------- + +conn_name(Ch) -> + proplists:get_value(name, proplists:get_value(connection_details, Ch)). diff --git a/rabbitmq-server/plugins-src/rabbitmq-management/src/rabbit_mgmt_wm_connections.erl b/rabbitmq-server/plugins-src/rabbitmq-management/src/rabbit_mgmt_wm_connections.erl new file mode 100644 index 0000000..1263d91 --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-management/src/rabbit_mgmt_wm_connections.erl @@ -0,0 +1,44 @@ +%% The contents of this file are subject to the Mozilla Public License +%% Version 1.1 (the "License"); you may not use this file except in +%% compliance with the License. You may obtain a copy of the License at +%% http://www.mozilla.org/MPL/ +%% +%% Software distributed under the License is distributed on an "AS IS" +%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the +%% License for the specific language governing rights and limitations +%% under the License. +%% +%% The Original Code is RabbitMQ Management Plugin. +%% +%% The Initial Developer of the Original Code is GoPivotal, Inc. +%% Copyright (c) 2010-2015 Pivotal Software, Inc. All rights reserved. +%% + +-module(rabbit_mgmt_wm_connections). + +-export([init/1, to_json/2, content_types_provided/2, is_authorized/2, + augmented/2]). + +-import(rabbit_misc, [pget/2]). + +-include("rabbit_mgmt.hrl"). +-include_lib("webmachine/include/webmachine.hrl"). +-include_lib("rabbit_common/include/rabbit.hrl"). + +%%-------------------------------------------------------------------- + +init(_Config) -> {ok, #context{}}. + +content_types_provided(ReqData, Context) -> + {[{"application/json", to_json}], ReqData, Context}. + +to_json(ReqData, Context) -> + rabbit_mgmt_util:reply_list(augmented(ReqData, Context), ReqData, Context). + +is_authorized(ReqData, Context) -> + rabbit_mgmt_util:is_authorized(ReqData, Context). + +augmented(ReqData, Context) -> + rabbit_mgmt_util:filter_conn_ch_list( + rabbit_mgmt_db:get_all_connections( + rabbit_mgmt_util:range_ceil(ReqData)), ReqData, Context). diff --git a/rabbitmq-server/plugins-src/rabbitmq-management/src/rabbit_mgmt_wm_consumers.erl b/rabbitmq-server/plugins-src/rabbitmq-management/src/rabbit_mgmt_wm_consumers.erl new file mode 100644 index 0000000..3d791d0 --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-management/src/rabbit_mgmt_wm_consumers.erl @@ -0,0 +1,56 @@ +%% The contents of this file are subject to the Mozilla Public License +%% Version 1.1 (the "License"); you may not use this file except in +%% compliance with the License. You may obtain a copy of the License at +%% http://www.mozilla.org/MPL/ +%% +%% Software distributed under the License is distributed on an "AS IS" +%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the +%% License for the specific language governing rights and limitations +%% under the License. +%% +%% The Original Code is RabbitMQ Management Plugin. +%% +%% The Initial Developer of the Original Code is GoPivotal, Inc. +%% Copyright (c) 2010-2015 Pivotal Software, Inc. All rights reserved. + +-module(rabbit_mgmt_wm_consumers). + +-export([init/1, to_json/2, content_types_provided/2, resource_exists/2, + is_authorized/2]). + +-import(rabbit_misc, [pget/2]). + +-include("rabbit_mgmt.hrl"). +-include_lib("webmachine/include/webmachine.hrl"). +-include_lib("rabbit_common/include/rabbit.hrl"). + +%%-------------------------------------------------------------------- + +init(_Config) -> {ok, #context{}}. + +content_types_provided(ReqData, Context) -> + {[{"application/json", to_json}], ReqData, Context}. + +resource_exists(ReqData, Context) -> + {case rabbit_mgmt_util:vhost(ReqData) of + vhost_not_found -> false; + _ -> true + end, ReqData, Context}. + +to_json(ReqData, Context = #context{user = User}) -> + Consumers = case rabbit_mgmt_util:vhost(ReqData) of + none -> rabbit_mgmt_db:get_all_consumers(); + VHost -> rabbit_mgmt_db:get_all_consumers(VHost) + end, + rabbit_mgmt_util:reply_list( + filter_user(Consumers, User), ReqData, Context). + +is_authorized(ReqData, Context) -> + rabbit_mgmt_util:is_authorized(ReqData, Context). + +filter_user(List, #user{username = Username, tags = Tags}) -> + case rabbit_mgmt_util:is_monitor(Tags) of + true -> List; + false -> [I || I <- List, + pget(user, pget(channel_details, I)) == Username] + end. diff --git a/rabbitmq-server/plugins-src/rabbitmq-management/src/rabbit_mgmt_wm_definitions.erl b/rabbitmq-server/plugins-src/rabbitmq-management/src/rabbit_mgmt_wm_definitions.erl new file mode 100644 index 0000000..a33d6a1 --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-management/src/rabbit_mgmt_wm_definitions.erl @@ -0,0 +1,281 @@ +%% The contents of this file are subject to the Mozilla Public License +%% Version 1.1 (the "License"); you may not use this file except in +%% compliance with the License. You may obtain a copy of the License at +%% http://www.mozilla.org/MPL/ +%% +%% Software distributed under the License is distributed on an "AS IS" +%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the +%% License for the specific language governing rights and limitations +%% under the License. +%% +%% The Original Code is RabbitMQ Management Plugin. +%% +%% The Initial Developer of the Original Code is GoPivotal, Inc. +%% Copyright (c) 2010-2015 Pivotal Software, Inc. All rights reserved. +%% + +-module(rabbit_mgmt_wm_definitions). + +-export([init/1, to_json/2, content_types_provided/2, is_authorized/2]). +-export([content_types_accepted/2, allowed_methods/2, accept_json/2]). +-export([post_is_create/2, create_path/2, accept_multipart/2]). + +-export([apply_defs/3]). + +-import(rabbit_misc, [pget/2, pget/3]). + +-include("rabbit_mgmt.hrl"). +-include_lib("webmachine/include/webmachine.hrl"). +-include_lib("amqp_client/include/amqp_client.hrl"). + +%%-------------------------------------------------------------------- +init(_Config) -> {ok, #context{}}. + +content_types_provided(ReqData, Context) -> + {[{"application/json", to_json}], ReqData, Context}. + +content_types_accepted(ReqData, Context) -> + {[{"application/json", accept_json}, + {"multipart/form-data", accept_multipart}], ReqData, Context}. + +allowed_methods(ReqData, Context) -> + {['HEAD', 'GET', 'POST'], ReqData, Context}. + +post_is_create(ReqData, Context) -> + {true, ReqData, Context}. + +create_path(ReqData, Context) -> + {"dummy", ReqData, Context}. + +to_json(ReqData, Context) -> + Xs = [X || X <- rabbit_mgmt_wm_exchanges:basic(ReqData), + export_exchange(X)], + Qs = [Q || Q <- rabbit_mgmt_wm_queues:basic(ReqData), + export_queue(Q)], + QNames = [{pget(name, Q), pget(vhost, Q)} || Q <- Qs], + Bs = [B || B <- rabbit_mgmt_wm_bindings:basic(ReqData), + export_binding(B, QNames)], + {ok, Vsn} = application:get_key(rabbit, vsn), + rabbit_mgmt_util:reply( + [{rabbit_version, list_to_binary(Vsn)}] ++ + filter( + [{users, rabbit_mgmt_wm_users:users()}, + {vhosts, rabbit_mgmt_wm_vhosts:basic()}, + {permissions, rabbit_mgmt_wm_permissions:permissions()}, + {parameters, rabbit_mgmt_wm_parameters:basic(ReqData)}, + {policies, rabbit_mgmt_wm_policies:basic(ReqData)}, + {queues, Qs}, + {exchanges, Xs}, + {bindings, Bs}]), + case wrq:get_qs_value("download", ReqData) of + undefined -> ReqData; + Filename -> rabbit_mgmt_util:set_resp_header( + "Content-Disposition", + "attachment; filename=" ++ + mochiweb_util:unquote(Filename), ReqData) + end, + Context). + +accept_json(ReqData, Context) -> + accept(wrq:req_body(ReqData), ReqData, Context). + +accept_multipart(ReqData, Context) -> + Parts = webmachine_multipart:get_all_parts( + wrq:req_body(ReqData), + webmachine_multipart:find_boundary(ReqData)), + Redirect = get_part("redirect", Parts), + Json = get_part("file", Parts), + Resp = {Res, _, _} = accept(Json, ReqData, Context), + case Res of + true -> + ReqData1 = + case Redirect of + unknown -> ReqData; + _ -> rabbit_mgmt_util:redirect(Redirect, ReqData) + end, + {true, ReqData1, Context}; + _ -> + Resp + end. + +is_authorized(ReqData, Context) -> + case wrq:get_qs_value("auth", ReqData) of + undefined -> rabbit_mgmt_util:is_authorized_admin(ReqData, Context); + Auth -> is_authorized_qs(ReqData, Context, Auth) + end. + +%% Support for the web UI - it can't add a normal "authorization" +%% header for a file download. +is_authorized_qs(ReqData, Context, Auth) -> + case rabbit_web_dispatch_util:parse_auth_header("Basic " ++ Auth) of + [Username, Password] -> rabbit_mgmt_util:is_authorized_admin( + ReqData, Context, Username, Password); + _ -> {?AUTH_REALM, ReqData, Context} + end. + +%%-------------------------------------------------------------------- + +accept(Body, ReqData, Context) -> + apply_defs(Body, fun() -> {true, ReqData, Context} end, + fun(E) -> rabbit_mgmt_util:bad_request(E, ReqData, Context) end). + +apply_defs(Body, SuccessFun, ErrorFun) -> + case rabbit_mgmt_util:decode([], Body) of + {error, E} -> + ErrorFun(E); + {ok, _, All} -> + try + for_all(users, All, fun add_user/1), + for_all(vhosts, All, fun add_vhost/1), + for_all(permissions, All, fun add_permission/1), + for_all(parameters, All, fun add_parameter/1), + for_all(policies, All, fun add_policy/1), + for_all(queues, All, fun add_queue/1), + for_all(exchanges, All, fun add_exchange/1), + for_all(bindings, All, fun add_binding/1), + SuccessFun() + catch {error, E} -> ErrorFun(format(E)); + exit:E -> ErrorFun(format(E)) + end + end. + +format(#amqp_error{name = Name, explanation = Explanation}) -> + list_to_binary(rabbit_misc:format("~s: ~s", [Name, Explanation])); +format(E) -> + list_to_binary(rabbit_misc:format("~p", [E])). + +get_part(Name, Parts) -> + %% TODO any reason not to use lists:keyfind instead? + Filtered = [Value || {N, _Meta, Value} <- Parts, N == Name], + case Filtered of + [] -> unknown; + [F] -> F + end. + +export_queue(Queue) -> + pget(owner_pid, Queue) == none. + +export_binding(Binding, Qs) -> + Src = pget(source, Binding), + Dest = pget(destination, Binding), + DestType = pget(destination_type, Binding), + VHost = pget(vhost, Binding), + Src =/= <<"">> + andalso + ( (DestType =:= queue andalso lists:member({Dest, VHost}, Qs)) + orelse (DestType =:= exchange andalso Dest =/= <<"">>) ). + +export_exchange(Exchange) -> + export_name(pget(name, Exchange)). + +export_name(<<>>) -> false; +export_name(<<"amq.", _/binary>>) -> false; +export_name(_Name) -> true. + +%%-------------------------------------------------------------------- + +rw_state() -> + [{users, [name, password_hash, tags]}, + {vhosts, [name]}, + {permissions, [user, vhost, configure, write, read]}, + {parameters, [vhost, component, name, value]}, + {policies, [vhost, name, pattern, definition, priority, 'apply-to']}, + {queues, [name, vhost, durable, auto_delete, arguments]}, + {exchanges, [name, vhost, type, durable, auto_delete, internal, + arguments]}, + {bindings, [source, vhost, destination, destination_type, routing_key, + arguments]}]. + +filter(Items) -> + [filter_items(N, V, proplists:get_value(N, rw_state())) || {N, V} <- Items]. + +filter_items(Name, List, Allowed) -> + {Name, [filter_item(I, Allowed) || I <- List]}. + +filter_item(Item, Allowed) -> + [{K, Fact} || {K, Fact} <- Item, lists:member(K, Allowed)]. + +%%-------------------------------------------------------------------- + +for_all(Name, All, Fun) -> + case pget(Name, All) of + undefined -> ok; + List -> [Fun([{atomise_name(K), V} || {K, V} <- I]) || + {struct, I} <- List] + end. + +atomise_name(N) -> list_to_atom(binary_to_list(N)). + +%%-------------------------------------------------------------------- + +add_parameter(Param) -> + VHost = pget(vhost, Param), + Comp = pget(component, Param), + Key = pget(name, Param), + Term = rabbit_misc:json_to_term(pget(value, Param)), + case rabbit_runtime_parameters:set(VHost, Comp, Key, Term, none) of + ok -> ok; + {error_string, E} -> S = rabbit_misc:format(" (~s/~s/~s)", + [VHost, Comp, Key]), + exit(list_to_binary(E ++ S)) + end. + +add_policy(Param) -> + VHost = pget(vhost, Param), + Key = pget(name, Param), + case rabbit_policy:set( + VHost, Key, pget(pattern, Param), + rabbit_misc:json_to_term(pget(definition, Param)), + pget(priority, Param), + pget('apply-to', Param, <<"all">>)) of + ok -> ok; + {error_string, E} -> S = rabbit_misc:format(" (~s/~s)", [VHost, Key]), + exit(list_to_binary(E ++ S)) + end. + +add_user(User) -> + rabbit_mgmt_wm_user:put_user(User). + +add_vhost(VHost) -> + VHostName = pget(name, VHost), + VHostTrace = pget(tracing, VHost), + rabbit_mgmt_wm_vhost:put_vhost(VHostName, VHostTrace). + +add_permission(Permission) -> + rabbit_auth_backend_internal:set_permissions(pget(user, Permission), + pget(vhost, Permission), + pget(configure, Permission), + pget(write, Permission), + pget(read, Permission)). + +add_queue(Queue) -> + rabbit_amqqueue:declare(r(queue, Queue), + pget(durable, Queue), + pget(auto_delete, Queue), + rabbit_mgmt_util:args(pget(arguments, Queue)), + none). + +add_exchange(Exchange) -> + Internal = case pget(internal, Exchange) of + undefined -> false; %% =< 2.2.0 + I -> I + end, + rabbit_exchange:declare(r(exchange, Exchange), + rabbit_exchange:check_type(pget(type, Exchange)), + pget(durable, Exchange), + pget(auto_delete, Exchange), + Internal, + rabbit_mgmt_util:args(pget(arguments, Exchange))). + +add_binding(Binding) -> + DestType = list_to_atom(binary_to_list(pget(destination_type, Binding))), + rabbit_binding:add( + #binding{source = r(exchange, source, Binding), + destination = r(DestType, destination, Binding), + key = pget(routing_key, Binding), + args = rabbit_mgmt_util:args(pget(arguments, Binding))}). + +r(Type, Props) -> r(Type, name, Props). + +r(Type, Name, Props) -> + rabbit_misc:r(pget(vhost, Props), Type, pget(Name, Props)). diff --git a/rabbitmq-server/plugins-src/rabbitmq-management/src/rabbit_mgmt_wm_exchange.erl b/rabbitmq-server/plugins-src/rabbitmq-management/src/rabbit_mgmt_wm_exchange.erl new file mode 100644 index 0000000..374280a --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-management/src/rabbit_mgmt_wm_exchange.erl @@ -0,0 +1,84 @@ +%% The contents of this file are subject to the Mozilla Public License +%% Version 1.1 (the "License"); you may not use this file except in +%% compliance with the License. You may obtain a copy of the License at +%% http://www.mozilla.org/MPL/ +%% +%% Software distributed under the License is distributed on an "AS IS" +%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the +%% License for the specific language governing rights and limitations +%% under the License. +%% +%% The Original Code is RabbitMQ Management Plugin. +%% +%% The Initial Developer of the Original Code is GoPivotal, Inc. +%% Copyright (c) 2010-2015 Pivotal Software, Inc. All rights reserved. +%% + +-module(rabbit_mgmt_wm_exchange). + +-export([init/1, resource_exists/2, to_json/2, + content_types_provided/2, content_types_accepted/2, + is_authorized/2, allowed_methods/2, accept_content/2, + delete_resource/2, exchange/1, exchange/2]). + +-include("rabbit_mgmt.hrl"). +-include_lib("webmachine/include/webmachine.hrl"). +-include_lib("amqp_client/include/amqp_client.hrl"). + +%%-------------------------------------------------------------------- +init(_Config) -> {ok, #context{}}. + +content_types_provided(ReqData, Context) -> + {[{"application/json", to_json}], ReqData, Context}. + +content_types_accepted(ReqData, Context) -> + {[{"application/json", accept_content}], ReqData, Context}. + +allowed_methods(ReqData, Context) -> + {['HEAD', 'GET', 'PUT', 'DELETE'], ReqData, Context}. + +resource_exists(ReqData, Context) -> + {case exchange(ReqData) of + not_found -> false; + _ -> true + end, ReqData, Context}. + +to_json(ReqData, Context) -> + [X] = rabbit_mgmt_db:augment_exchanges( + [exchange(ReqData)], rabbit_mgmt_util:range(ReqData), full), + rabbit_mgmt_util:reply(X, ReqData, Context). + +accept_content(ReqData, Context) -> + rabbit_mgmt_util:http_to_amqp( + 'exchange.declare', ReqData, Context, + [{fun rabbit_mgmt_util:parse_bool/1, [durable, auto_delete, internal]}], + [{exchange, rabbit_mgmt_util:id(exchange, ReqData)}]). + +delete_resource(ReqData, Context) -> + IfUnused = "true" =:= wrq:get_qs_value("if-unused", ReqData), + rabbit_mgmt_util:amqp_request( + rabbit_mgmt_util:vhost(ReqData), ReqData, Context, + #'exchange.delete'{exchange = id(ReqData), + if_unused = IfUnused}). + +is_authorized(ReqData, Context) -> + rabbit_mgmt_util:is_authorized_vhost(ReqData, Context). + +%%-------------------------------------------------------------------- + +exchange(ReqData) -> + case rabbit_mgmt_util:vhost(ReqData) of + not_found -> not_found; + VHost -> exchange(VHost, id(ReqData)) + end. + +exchange(VHost, XName) -> + Name = rabbit_misc:r(VHost, exchange, XName), + case rabbit_exchange:lookup(Name) of + {ok, X} -> rabbit_mgmt_format:exchange( + rabbit_exchange:info(X)); + {error, not_found} -> not_found + end. + +id(ReqData) -> + rabbit_mgmt_util:id(exchange, ReqData). diff --git a/rabbitmq-server/plugins-src/rabbitmq-management/src/rabbit_mgmt_wm_exchange_publish.erl b/rabbitmq-server/plugins-src/rabbitmq-management/src/rabbit_mgmt_wm_exchange_publish.erl new file mode 100644 index 0000000..467c055 --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-management/src/rabbit_mgmt_wm_exchange_publish.erl @@ -0,0 +1,102 @@ +%% The contents of this file are subject to the Mozilla Public License +%% Version 1.1 (the "License"); you may not use this file except in +%% compliance with the License. You may obtain a copy of the License at +%% http://www.mozilla.org/MPL/ +%% +%% Software distributed under the License is distributed on an "AS IS" +%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the +%% License for the specific language governing rights and limitations +%% under the License. +%% +%% The Original Code is RabbitMQ Management Plugin. +%% +%% The Initial Developer of the Original Code is GoPivotal, Inc. +%% Copyright (c) 2011-2015 Pivotal Software, Inc. All rights reserved. +%% + +-module(rabbit_mgmt_wm_exchange_publish). + +-export([init/1, resource_exists/2, post_is_create/2, is_authorized/2, + allowed_methods/2, content_types_provided/2, process_post/2]). + +-include("rabbit_mgmt.hrl"). +-include_lib("webmachine/include/webmachine.hrl"). +-include_lib("amqp_client/include/amqp_client.hrl"). + +%%-------------------------------------------------------------------- +init(_Config) -> {ok, #context{}}. + +allowed_methods(ReqData, Context) -> + {['POST'], ReqData, Context}. + +content_types_provided(ReqData, Context) -> + {[{"application/json", to_json}], ReqData, Context}. + +resource_exists(ReqData, Context) -> + {case rabbit_mgmt_wm_exchange:exchange(ReqData) of + not_found -> false; + _ -> true + end, ReqData, Context}. + +post_is_create(ReqData, Context) -> + {false, ReqData, Context}. + +process_post(ReqData, Context) -> + rabbit_mgmt_util:post_respond(do_it(ReqData, Context)). + +do_it(ReqData, Context) -> + VHost = rabbit_mgmt_util:vhost(ReqData), + X = rabbit_mgmt_util:id(exchange, ReqData), + rabbit_mgmt_util:with_decode( + [routing_key, properties, payload, payload_encoding], ReqData, Context, + fun ([RoutingKey, Props0, Payload0, Enc], _) when is_binary(Payload0) -> + rabbit_mgmt_util:with_channel( + VHost, ReqData, Context, + fun (Ch) -> + MRef = erlang:monitor(process, Ch), + amqp_channel:register_confirm_handler(Ch, self()), + amqp_channel:register_return_handler(Ch, self()), + amqp_channel:call(Ch, #'confirm.select'{}), + Props = rabbit_mgmt_format:to_basic_properties(Props0), + Payload = decode(Payload0, Enc), + amqp_channel:cast(Ch, #'basic.publish'{ + exchange = X, + routing_key = RoutingKey, + mandatory = true}, + #amqp_msg{props = Props, + payload = Payload}), + receive + {#'basic.return'{}, _} -> + receive + #'basic.ack'{} -> ok + end, + good(MRef, false, ReqData, Context); + #'basic.ack'{} -> + good(MRef, true, ReqData, Context); + {'DOWN', _, _, _, Err} -> + bad(Err, ReqData, Context) + end + end); + ([_RoutingKey, _Props, _Payload, _Enc], _) -> + throw({error, payload_not_string}) + end). + +good(MRef, Routed, ReqData, Context) -> + erlang:demonitor(MRef), + rabbit_mgmt_util:reply([{routed, Routed}], ReqData, Context). + +bad({shutdown, {connection_closing, + {server_initiated_close, Code, Reason}}}, ReqData, Context) -> + rabbit_mgmt_util:bad_request_exception(Code, Reason, ReqData, Context); + +bad({shutdown, {server_initiated_close, Code, Reason}}, ReqData, Context) -> + rabbit_mgmt_util:bad_request_exception(Code, Reason, ReqData, Context). + +is_authorized(ReqData, Context) -> + rabbit_mgmt_util:is_authorized_vhost(ReqData, Context). + +%%-------------------------------------------------------------------- + +decode(Payload, <<"string">>) -> Payload; +decode(Payload, <<"base64">>) -> rabbit_mgmt_util:b64decode_or_throw(Payload); +decode(_Payload, Enc) -> throw({error, {unsupported_encoding, Enc}}). diff --git a/rabbitmq-server/plugins-src/rabbitmq-management/src/rabbit_mgmt_wm_exchanges.erl b/rabbitmq-server/plugins-src/rabbitmq-management/src/rabbit_mgmt_wm_exchanges.erl new file mode 100644 index 0000000..f1df46a --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-management/src/rabbit_mgmt_wm_exchanges.erl @@ -0,0 +1,56 @@ +%% The contents of this file are subject to the Mozilla Public License +%% Version 1.1 (the "License"); you may not use this file except in +%% compliance with the License. You may obtain a copy of the License at +%% http://www.mozilla.org/MPL/ +%% +%% Software distributed under the License is distributed on an "AS IS" +%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the +%% License for the specific language governing rights and limitations +%% under the License. +%% +%% The Original Code is RabbitMQ Management Plugin. +%% +%% The Initial Developer of the Original Code is GoPivotal, Inc. +%% Copyright (c) 2010-2015 Pivotal Software, Inc. All rights reserved. +%% + +-module(rabbit_mgmt_wm_exchanges). + +-export([init/1, to_json/2, content_types_provided/2, is_authorized/2, + resource_exists/2, basic/1, augmented/2]). + +-include("rabbit_mgmt.hrl"). +-include_lib("webmachine/include/webmachine.hrl"). +-include_lib("rabbit_common/include/rabbit.hrl"). + +%%-------------------------------------------------------------------- + +init(_Config) -> {ok, #context{}}. + +content_types_provided(ReqData, Context) -> + {[{"application/json", to_json}], ReqData, Context}. + +resource_exists(ReqData, Context) -> + {case exchanges0(ReqData) of + vhost_not_found -> false; + _ -> true + end, ReqData, Context}. + +to_json(ReqData, Context) -> + rabbit_mgmt_util:reply_list(augmented(ReqData, Context), ReqData, Context). + +is_authorized(ReqData, Context) -> + rabbit_mgmt_util:is_authorized_vhost(ReqData, Context). + +%%-------------------------------------------------------------------- + +augmented(ReqData, Context) -> + rabbit_mgmt_db:augment_exchanges( + rabbit_mgmt_util:filter_vhost(basic(ReqData), ReqData, Context), + rabbit_mgmt_util:range(ReqData), basic). + +basic(ReqData) -> + [rabbit_mgmt_format:exchange(X) || X <- exchanges0(ReqData)]. + +exchanges0(ReqData) -> + rabbit_mgmt_util:all_or_one_vhost(ReqData, fun rabbit_exchange:info_all/1). diff --git a/rabbitmq-server/plugins-src/rabbitmq-management/src/rabbit_mgmt_wm_extensions.erl b/rabbitmq-server/plugins-src/rabbitmq-management/src/rabbit_mgmt_wm_extensions.erl new file mode 100644 index 0000000..b38bf1d --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-management/src/rabbit_mgmt_wm_extensions.erl @@ -0,0 +1,37 @@ +%% The contents of this file are subject to the Mozilla Public License +%% Version 1.1 (the "License"); you may not use this file except in +%% compliance with the License. You may obtain a copy of the License at +%% http://www.mozilla.org/MPL/ +%% +%% Software distributed under the License is distributed on an "AS IS" +%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the +%% License for the specific language governing rights and limitations +%% under the License. +%% +%% The Original Code is RabbitMQ Management Plugin. +%% +%% The Initial Developer of the Original Code is GoPivotal, Inc. +%% Copyright (c) 2011-2015 Pivotal Software, Inc. All rights reserved. +%% + +-module(rabbit_mgmt_wm_extensions). + +-export([init/1, to_json/2, content_types_provided/2, is_authorized/2]). + +-include("rabbit_mgmt.hrl"). +-include_lib("webmachine/include/webmachine.hrl"). +-include_lib("rabbit_common/include/rabbit.hrl"). + +%%-------------------------------------------------------------------- +init(_Config) -> {ok, #context{}}. + +content_types_provided(ReqData, Context) -> + {[{"application/json", to_json}], ReqData, Context}. + +to_json(ReqData, Context) -> + Modules = rabbit_mgmt_dispatcher:modules([]), + rabbit_mgmt_util:reply( + [Module:web_ui() || Module <- Modules], ReqData, Context). + +is_authorized(ReqData, Context) -> + rabbit_mgmt_util:is_authorized(ReqData, Context). diff --git a/rabbitmq-server/plugins-src/rabbitmq-management/src/rabbit_mgmt_wm_node.erl b/rabbitmq-server/plugins-src/rabbitmq-management/src/rabbit_mgmt_wm_node.erl new file mode 100644 index 0000000..646d183 --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-management/src/rabbit_mgmt_wm_node.erl @@ -0,0 +1,67 @@ +%% The contents of this file are subject to the Mozilla Public License +%% Version 1.1 (the "License"); you may not use this file except in +%% compliance with the License. You may obtain a copy of the License at +%% http://www.mozilla.org/MPL/ +%% +%% Software distributed under the License is distributed on an "AS IS" +%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the +%% License for the specific language governing rights and limitations +%% under the License. +%% +%% The Original Code is RabbitMQ Management Console. +%% +%% The Initial Developer of the Original Code is GoPivotal, Inc. +%% Copyright (c) 2010-2015 Pivotal Software, Inc. All rights reserved. +%% + +-module(rabbit_mgmt_wm_node). + +-export([init/1, to_json/2, content_types_provided/2, is_authorized/2]). +-export([resource_exists/2]). + +-include("rabbit_mgmt.hrl"). +-include_lib("webmachine/include/webmachine.hrl"). +-include_lib("rabbit_common/include/rabbit.hrl"). + +%%-------------------------------------------------------------------- + +init(_Config) -> {ok, #context{}}. + +content_types_provided(ReqData, Context) -> + {[{"application/json", to_json}], ReqData, Context}. + +resource_exists(ReqData, Context) -> + {case node0(ReqData) of + not_found -> false; + _ -> true + end, ReqData, Context}. + +to_json(ReqData, Context) -> + rabbit_mgmt_util:reply(node0(ReqData), ReqData, Context). + +is_authorized(ReqData, Context) -> + rabbit_mgmt_util:is_authorized_monitor(ReqData, Context). + +%%-------------------------------------------------------------------- + +node0(ReqData) -> + Node = list_to_atom(binary_to_list(rabbit_mgmt_util:id(node, ReqData))), + case [N || N <- rabbit_mgmt_wm_nodes:all_nodes(ReqData), + proplists:get_value(name, N) == Node] of + [] -> not_found; + [Data] -> augment(ReqData, Node, Data) + end. + +augment(ReqData, Node, Data) -> + lists:foldl(fun (Key, DataN) -> augment(Key, ReqData, Node, DataN) end, + Data, [memory, binary]). + +augment(Key, ReqData, Node, Data) -> + case wrq:get_qs_value(atom_to_list(Key), ReqData) of + "true" -> Res = case rpc:call(Node, rabbit_vm, Key, [], infinity) of + {badrpc, _} -> not_available; + Result -> Result + end, + [{Key, Res} | Data]; + _ -> Data + end. diff --git a/rabbitmq-server/plugins-src/rabbitmq-management/src/rabbit_mgmt_wm_nodes.erl b/rabbitmq-server/plugins-src/rabbitmq-management/src/rabbit_mgmt_wm_nodes.erl new file mode 100644 index 0000000..fb86e1e --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-management/src/rabbit_mgmt_wm_nodes.erl @@ -0,0 +1,51 @@ +%% The contents of this file are subject to the Mozilla Public License +%% Version 1.1 (the "License"); you may not use this file except in +%% compliance with the License. You may obtain a copy of the License at +%% http://www.mozilla.org/MPL/ +%% +%% Software distributed under the License is distributed on an "AS IS" +%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the +%% License for the specific language governing rights and limitations +%% under the License. +%% +%% The Original Code is RabbitMQ Management Console. +%% +%% The Initial Developer of the Original Code is GoPivotal, Inc. +%% Copyright (c) 2010-2015 Pivotal Software, Inc. All rights reserved. +%% + +-module(rabbit_mgmt_wm_nodes). + +-export([init/1, to_json/2, content_types_provided/2, is_authorized/2]). +-export([all_nodes/1, all_nodes_raw/0]). + +-include("rabbit_mgmt.hrl"). +-include_lib("webmachine/include/webmachine.hrl"). +-include_lib("rabbit_common/include/rabbit.hrl"). + +%%-------------------------------------------------------------------- + +init(_Config) -> {ok, #context{}}. + +content_types_provided(ReqData, Context) -> + {[{"application/json", to_json}], ReqData, Context}. + +to_json(ReqData, Context) -> + rabbit_mgmt_util:reply_list(all_nodes(ReqData), ReqData, Context). + +is_authorized(ReqData, Context) -> + rabbit_mgmt_util:is_authorized_monitor(ReqData, Context). + +%%-------------------------------------------------------------------- + +all_nodes(ReqData) -> + rabbit_mgmt_db:augment_nodes( + all_nodes_raw(), rabbit_mgmt_util:range_ceil(ReqData)). + +all_nodes_raw() -> + S = rabbit_mnesia:status(), + Nodes = proplists:get_value(nodes, S), + Types = proplists:get_keys(Nodes), + Running = proplists:get_value(running_nodes, S), + [[{name, Node}, {type, Type}, {running, lists:member(Node, Running)}] || + Type <- Types, Node <- proplists:get_value(Type, Nodes)]. diff --git a/rabbitmq-server/plugins-src/rabbitmq-management/src/rabbit_mgmt_wm_overview.erl b/rabbitmq-server/plugins-src/rabbitmq-management/src/rabbit_mgmt_wm_overview.erl new file mode 100644 index 0000000..486016c --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-management/src/rabbit_mgmt_wm_overview.erl @@ -0,0 +1,102 @@ +%% The contents of this file are subject to the Mozilla Public License +%% Version 1.1 (the "License"); you may not use this file except in +%% compliance with the License. You may obtain a copy of the License at +%% http://www.mozilla.org/MPL/ +%% +%% Software distributed under the License is distributed on an "AS IS" +%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the +%% License for the specific language governing rights and limitations +%% under the License. +%% +%% The Original Code is RabbitMQ Management Plugin. +%% +%% The Initial Developer of the Original Code is GoPivotal, Inc. +%% Copyright (c) 2010-2015 Pivotal Software, Inc. All rights reserved. +%% + +-module(rabbit_mgmt_wm_overview). + +-export([init/1, to_json/2, content_types_provided/2, is_authorized/2]). + +-import(rabbit_misc, [pget/2, pget/3]). + +-include("rabbit_mgmt.hrl"). +-include_lib("webmachine/include/webmachine.hrl"). +-include_lib("rabbit_common/include/rabbit.hrl"). + +%%-------------------------------------------------------------------- + +init(_Config) -> {ok, #context{}}. + +content_types_provided(ReqData, Context) -> + {[{"application/json", to_json}], ReqData, Context}. + +to_json(ReqData, Context = #context{user = User = #user{tags = Tags}}) -> + {ok, RatesMode} = application:get_env(rabbitmq_management, rates_mode), + %% NB: this duplicates what's in /nodes but we want a global idea + %% of this. And /nodes is not accessible to non-monitor users. + ExchangeTypes = rabbit_mgmt_external_stats:list_registry_plugins(exchange), + Overview0 = [{management_version, version(rabbitmq_management)}, + {rates_mode, RatesMode}, + {exchange_types, ExchangeTypes}, + {rabbitmq_version, version(rabbit)}, + {cluster_name, rabbit_nodes:cluster_name()}, + {erlang_version, erlang_version()}, + {erlang_full_version, erlang_full_version()}], + Range = rabbit_mgmt_util:range(ReqData), + Overview = + case rabbit_mgmt_util:is_monitor(Tags) of + true -> + Overview0 ++ + [{K, maybe_struct(V)} || + {K,V} <- rabbit_mgmt_db:get_overview(Range)] ++ + [{node, node()}, + {statistics_db_node, stats_db_node()}, + {listeners, listeners()}, + {contexts, web_contexts(ReqData)}]; + _ -> + Overview0 ++ + [{K, maybe_struct(V)} || + {K, V} <- rabbit_mgmt_db:get_overview(User, Range)] + end, + rabbit_mgmt_util:reply(Overview, ReqData, Context). + +is_authorized(ReqData, Context) -> + rabbit_mgmt_util:is_authorized(ReqData, Context). + +%%-------------------------------------------------------------------- + +stats_db_node() -> + case global:whereis_name(rabbit_mgmt_db) of + undefined -> not_running; + Pid -> node(Pid) + end. + +version(App) -> + {ok, V} = application:get_key(App, vsn), + list_to_binary(V). + +listeners() -> + rabbit_mgmt_util:sort_list( + [rabbit_mgmt_format:listener(L) + || L <- rabbit_networking:active_listeners()], + ["protocol", "port", "node"] ). + +maybe_struct(L) when is_list(L) -> {struct, L}; +maybe_struct(V) -> V. + +%%-------------------------------------------------------------------- + +web_contexts(ReqData) -> + rabbit_mgmt_util:sort_list( + lists:append( + [fmt_contexts(N) || N <- rabbit_mgmt_wm_nodes:all_nodes(ReqData)]), + ["description", "port", "node"]). + +fmt_contexts(N) -> + [[{node, pget(name, N)} | C] || C <- pget(contexts, N, [])]. + +erlang_version() -> list_to_binary(rabbit_misc:otp_release()). + +erlang_full_version() -> + list_to_binary(string:strip(erlang:system_info(system_version), both, $\n)). diff --git a/rabbitmq-server/plugins-src/rabbitmq-management/src/rabbit_mgmt_wm_parameter.erl b/rabbitmq-server/plugins-src/rabbitmq-management/src/rabbit_mgmt_wm_parameter.erl new file mode 100644 index 0000000..5af5d38 --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-management/src/rabbit_mgmt_wm_parameter.erl @@ -0,0 +1,88 @@ +%% The contents of this file are subject to the Mozilla Public License +%% Version 1.1 (the "License"); you may not use this file except in +%% compliance with the License. You may obtain a copy of the License at +%% http://www.mozilla.org/MPL/ +%% +%% Software distributed under the License is distributed on an "AS IS" +%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the +%% License for the specific language governing rights and limitations +%% under the License. +%% +%% The Original Code is RabbitMQ Management Plugin. +%% +%% The Initial Developer of the Original Code is GoPivotal, Inc. +%% Copyright (c) 2010-2015 Pivotal Software, Inc. All rights reserved. +%% + +-module(rabbit_mgmt_wm_parameter). + +-export([init/1, resource_exists/2, to_json/2, + content_types_provided/2, content_types_accepted/2, + is_authorized/2, allowed_methods/2, accept_content/2, + delete_resource/2]). + +-import(rabbit_misc, [pget/2]). + +-include("rabbit_mgmt.hrl"). +-include_lib("webmachine/include/webmachine.hrl"). +-include_lib("rabbit_common/include/rabbit.hrl"). + +%%-------------------------------------------------------------------- + +init(_Config) -> {ok, #context{}}. + +content_types_provided(ReqData, Context) -> + {[{"application/json", to_json}], ReqData, Context}. + +content_types_accepted(ReqData, Context) -> + {[{"application/json", accept_content}], ReqData, Context}. + +allowed_methods(ReqData, Context) -> + {['HEAD', 'GET', 'PUT', 'DELETE'], ReqData, Context}. + +resource_exists(ReqData, Context) -> + {case parameter(ReqData) of + not_found -> false; + _ -> true + end, ReqData, Context}. + +to_json(ReqData, Context) -> + rabbit_mgmt_util:reply(rabbit_mgmt_format:parameter(parameter(ReqData)), + ReqData, Context). + +accept_content(ReqData, Context = #context{user = User}) -> + case rabbit_mgmt_util:vhost(ReqData) of + not_found -> + rabbit_mgmt_util:not_found(vhost_not_found, ReqData, Context); + VHost -> + rabbit_mgmt_util:with_decode( + [value], ReqData, Context, + fun([Value], _) -> + case rabbit_runtime_parameters:set( + VHost, component(ReqData), name(ReqData), + rabbit_misc:json_to_term(Value), User) of + ok -> + {true, ReqData, Context}; + {error_string, Reason} -> + rabbit_mgmt_util:bad_request( + list_to_binary(Reason), ReqData, Context) + end + end) + end. + +delete_resource(ReqData, Context) -> + ok = rabbit_runtime_parameters:clear( + rabbit_mgmt_util:vhost(ReqData), component(ReqData), name(ReqData)), + {true, ReqData, Context}. + +is_authorized(ReqData, Context) -> + rabbit_mgmt_util:is_authorized_policies(ReqData, Context). + +%%-------------------------------------------------------------------- + +parameter(ReqData) -> + rabbit_runtime_parameters:lookup( + rabbit_mgmt_util:vhost(ReqData), component(ReqData), name(ReqData)). + +component(ReqData) -> rabbit_mgmt_util:id(component, ReqData). +name(ReqData) -> rabbit_mgmt_util:id(name, ReqData). diff --git a/rabbitmq-server/plugins-src/rabbitmq-management/src/rabbit_mgmt_wm_parameters.erl b/rabbitmq-server/plugins-src/rabbitmq-management/src/rabbit_mgmt_wm_parameters.erl new file mode 100644 index 0000000..0664baf --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-management/src/rabbit_mgmt_wm_parameters.erl @@ -0,0 +1,63 @@ +%% The contents of this file are subject to the Mozilla Public License +%% Version 1.1 (the "License"); you may not use this file except in +%% compliance with the License. You may obtain a copy of the License at +%% http://www.mozilla.org/MPL/ +%% +%% Software distributed under the License is distributed on an "AS IS" +%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the +%% License for the specific language governing rights and limitations +%% under the License. +%% +%% The Original Code is RabbitMQ Management Plugin. +%% +%% The Initial Developer of the Original Code is GoPivotal, Inc. +%% Copyright (c) 2010-2015 Pivotal Software, Inc. All rights reserved. +%% + +-module(rabbit_mgmt_wm_parameters). + +-export([init/1, to_json/2, content_types_provided/2, is_authorized/2, + resource_exists/2, basic/1]). + +-include("rabbit_mgmt.hrl"). +-include_lib("webmachine/include/webmachine.hrl"). +-include_lib("rabbit_common/include/rabbit.hrl"). + +%%-------------------------------------------------------------------- + +init(_Config) -> {ok, #context{}}. + +content_types_provided(ReqData, Context) -> + {[{"application/json", to_json}], ReqData, Context}. + +resource_exists(ReqData, Context) -> + {case basic(ReqData) of + not_found -> false; + _ -> true + end, ReqData, Context}. + +to_json(ReqData, Context) -> + rabbit_mgmt_util:reply_list( + rabbit_mgmt_util:filter_vhost(basic(ReqData), ReqData, Context), + ReqData, Context). + +is_authorized(ReqData, Context) -> + rabbit_mgmt_util:is_authorized_policies(ReqData, Context). + +%%-------------------------------------------------------------------- + +basic(ReqData) -> + Raw = case rabbit_mgmt_util:id(component, ReqData) of + none -> rabbit_runtime_parameters:list(); + Name -> case rabbit_mgmt_util:vhost(ReqData) of + none -> rabbit_runtime_parameters:list_component( + Name); + not_found -> not_found; + VHost -> rabbit_runtime_parameters:list( + VHost, Name) + end + end, + case Raw of + not_found -> not_found; + _ -> [rabbit_mgmt_format:parameter(P) || P <- Raw] + end. diff --git a/rabbitmq-server/plugins-src/rabbitmq-management/src/rabbit_mgmt_wm_permission.erl b/rabbitmq-server/plugins-src/rabbitmq-management/src/rabbit_mgmt_wm_permission.erl new file mode 100644 index 0000000..ec1046b --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-management/src/rabbit_mgmt_wm_permission.erl @@ -0,0 +1,97 @@ +%% The contents of this file are subject to the Mozilla Public License +%% Version 1.1 (the "License"); you may not use this file except in +%% compliance with the License. You may obtain a copy of the License at +%% http://www.mozilla.org/MPL/ +%% +%% Software distributed under the License is distributed on an "AS IS" +%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the +%% License for the specific language governing rights and limitations +%% under the License. +%% +%% The Original Code is RabbitMQ Management Plugin. +%% +%% The Initial Developer of the Original Code is GoPivotal, Inc. +%% Copyright (c) 2010-2015 Pivotal Software, Inc. All rights reserved. +%% + +-module(rabbit_mgmt_wm_permission). + +-export([init/1, resource_exists/2, to_json/2, + content_types_provided/2, content_types_accepted/2, + is_authorized/2, allowed_methods/2, accept_content/2, + delete_resource/2]). + +-include("rabbit_mgmt.hrl"). +-include_lib("webmachine/include/webmachine.hrl"). +-include_lib("rabbit_common/include/rabbit.hrl"). + +%%-------------------------------------------------------------------- +init(_Config) -> {ok, #context{}}. + +content_types_provided(ReqData, Context) -> + {[{"application/json", to_json}], ReqData, Context}. + +content_types_accepted(ReqData, Context) -> + {[{"application/json", accept_content}], ReqData, Context}. + +allowed_methods(ReqData, Context) -> + {['HEAD', 'GET', 'PUT', 'DELETE'], ReqData, Context}. + +resource_exists(ReqData, Context) -> + {case perms(ReqData) of + none -> false; + not_found -> false; + _ -> true + end, ReqData, Context}. + +to_json(ReqData, Context) -> + rabbit_mgmt_util:reply(perms(ReqData), ReqData, Context). + +accept_content(ReqData, Context) -> + case perms(ReqData) of + not_found -> + rabbit_mgmt_util:bad_request(vhost_or_user_not_found, + ReqData, Context); + _ -> + User = rabbit_mgmt_util:id(user, ReqData), + VHost = rabbit_mgmt_util:id(vhost, ReqData), + rabbit_mgmt_util:with_decode( + [configure, write, read], ReqData, Context, + fun([Conf, Write, Read], _) -> + rabbit_auth_backend_internal:set_permissions( + User, VHost, Conf, Write, Read), + {true, ReqData, Context} + end) + end. + +delete_resource(ReqData, Context) -> + User = rabbit_mgmt_util:id(user, ReqData), + VHost = rabbit_mgmt_util:id(vhost, ReqData), + rabbit_auth_backend_internal:clear_permissions(User, VHost), + {true, ReqData, Context}. + +is_authorized(ReqData, Context) -> + rabbit_mgmt_util:is_authorized_admin(ReqData, Context). + +%%-------------------------------------------------------------------- + +perms(ReqData) -> + User = rabbit_mgmt_util:id(user, ReqData), + case rabbit_auth_backend_internal:lookup_user(User) of + {ok, _} -> + case rabbit_mgmt_util:vhost(ReqData) of + not_found -> + not_found; + VHost -> + Perms = + rabbit_auth_backend_internal:list_user_vhost_permissions( + User, VHost), + case Perms of + [Rest] -> [{user, User}, + {vhost, VHost} | Rest]; + [] -> none + end + end; + {error, _} -> + not_found + end. diff --git a/rabbitmq-server/plugins-src/rabbitmq-management/src/rabbit_mgmt_wm_permissions.erl b/rabbitmq-server/plugins-src/rabbitmq-management/src/rabbit_mgmt_wm_permissions.erl new file mode 100644 index 0000000..52f4771 --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-management/src/rabbit_mgmt_wm_permissions.erl @@ -0,0 +1,43 @@ +%% The contents of this file are subject to the Mozilla Public License +%% Version 1.1 (the "License"); you may not use this file except in +%% compliance with the License. You may obtain a copy of the License at +%% http://www.mozilla.org/MPL/ +%% +%% Software distributed under the License is distributed on an "AS IS" +%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the +%% License for the specific language governing rights and limitations +%% under the License. +%% +%% The Original Code is RabbitMQ Management Plugin. +%% +%% The Initial Developer of the Original Code is GoPivotal, Inc. +%% Copyright (c) 2010-2015 Pivotal Software, Inc. All rights reserved. +%% + +-module(rabbit_mgmt_wm_permissions). + +-export([init/1, to_json/2, content_types_provided/2, is_authorized/2]). +-export([permissions/0]). + +-include("rabbit_mgmt.hrl"). +-include_lib("webmachine/include/webmachine.hrl"). +-include_lib("rabbit_common/include/rabbit.hrl"). + +%%-------------------------------------------------------------------- + +init(_Config) -> {ok, #context{}}. + +content_types_provided(ReqData, Context) -> + {[{"application/json", to_json}], ReqData, Context}. + +to_json(ReqData, Context) -> + rabbit_mgmt_util:reply_list(permissions(), ["vhost", "user"], + ReqData, Context). + +is_authorized(ReqData, Context) -> + rabbit_mgmt_util:is_authorized_admin(ReqData, Context). + +%%-------------------------------------------------------------------- + +permissions() -> + rabbit_auth_backend_internal:list_permissions(). diff --git a/rabbitmq-server/plugins-src/rabbitmq-management/src/rabbit_mgmt_wm_permissions_user.erl b/rabbitmq-server/plugins-src/rabbitmq-management/src/rabbit_mgmt_wm_permissions_user.erl new file mode 100644 index 0000000..df19045 --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-management/src/rabbit_mgmt_wm_permissions_user.erl @@ -0,0 +1,46 @@ +%% The contents of this file are subject to the Mozilla Public License +%% Version 1.1 (the "License"); you may not use this file except in +%% compliance with the License. You may obtain a copy of the License at +%% http://www.mozilla.org/MPL/ +%% +%% Software distributed under the License is distributed on an "AS IS" +%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the +%% License for the specific language governing rights and limitations +%% under the License. +%% +%% The Original Code is RabbitMQ Management Plugin. +%% +%% The Initial Developer of the Original Code is GoPivotal, Inc. +%% Copyright (c) 2010-2015 Pivotal Software, Inc. All rights reserved. +%% + +-module(rabbit_mgmt_wm_permissions_user). + +-export([init/1, to_json/2, content_types_provided/2, resource_exists/2, + is_authorized/2]). + +-include("rabbit_mgmt.hrl"). +-include_lib("webmachine/include/webmachine.hrl"). +-include_lib("rabbit_common/include/rabbit.hrl"). + +%%-------------------------------------------------------------------- + +init(_Config) -> {ok, #context{}}. + +content_types_provided(ReqData, Context) -> + {[{"application/json", to_json}], ReqData, Context}. + +resource_exists(ReqData, Context) -> + {case rabbit_mgmt_wm_user:user(ReqData) of + {ok, _} -> true; + {error, _} -> false + end, ReqData, Context}. + +to_json(ReqData, Context) -> + User = rabbit_mgmt_util:id(user, ReqData), + Perms = rabbit_auth_backend_internal:list_user_permissions(User), + rabbit_mgmt_util:reply_list([[{user, User} | Rest] || Rest <- Perms], + ["vhost", "user"], ReqData, Context). + +is_authorized(ReqData, Context) -> + rabbit_mgmt_util:is_authorized_admin(ReqData, Context). diff --git a/rabbitmq-server/plugins-src/rabbitmq-management/src/rabbit_mgmt_wm_permissions_vhost.erl b/rabbitmq-server/plugins-src/rabbitmq-management/src/rabbit_mgmt_wm_permissions_vhost.erl new file mode 100644 index 0000000..062a902 --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-management/src/rabbit_mgmt_wm_permissions_vhost.erl @@ -0,0 +1,43 @@ +%% The contents of this file are subject to the Mozilla Public License +%% Version 1.1 (the "License"); you may not use this file except in +%% compliance with the License. You may obtain a copy of the License at +%% http://www.mozilla.org/MPL/ +%% +%% Software distributed under the License is distributed on an "AS IS" +%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the +%% License for the specific language governing rights and limitations +%% under the License. +%% +%% The Original Code is RabbitMQ Management Plugin. +%% +%% The Initial Developer of the Original Code is GoPivotal, Inc. +%% Copyright (c) 2010-2015 Pivotal Software, Inc. All rights reserved. +%% + +-module(rabbit_mgmt_wm_permissions_vhost). + +-export([init/1, to_json/2, content_types_provided/2, resource_exists/2, + is_authorized/2]). + +-include("rabbit_mgmt.hrl"). +-include_lib("webmachine/include/webmachine.hrl"). +-include_lib("rabbit_common/include/rabbit.hrl"). + +%%-------------------------------------------------------------------- + +init(_Config) -> {ok, #context{}}. + +content_types_provided(ReqData, Context) -> + {[{"application/json", to_json}], ReqData, Context}. + +resource_exists(ReqData, Context) -> + {rabbit_vhost:exists(rabbit_mgmt_wm_vhost:id(ReqData)), ReqData, Context}. + +to_json(ReqData, Context) -> + VHost = rabbit_mgmt_util:id(vhost, ReqData), + Perms = rabbit_auth_backend_internal:list_vhost_permissions(VHost), + rabbit_mgmt_util:reply_list([[{vhost, VHost} | Rest] || Rest <- Perms], + ["vhost", "user"], ReqData, Context). + +is_authorized(ReqData, Context) -> + rabbit_mgmt_util:is_authorized_admin(ReqData, Context). diff --git a/rabbitmq-server/plugins-src/rabbitmq-management/src/rabbit_mgmt_wm_policies.erl b/rabbitmq-server/plugins-src/rabbitmq-management/src/rabbit_mgmt_wm_policies.erl new file mode 100644 index 0000000..1236bcd --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-management/src/rabbit_mgmt_wm_policies.erl @@ -0,0 +1,54 @@ +%% The contents of this file are subject to the Mozilla Public License +%% Version 1.1 (the "License"); you may not use this file except in +%% compliance with the License. You may obtain a copy of the License at +%% http://www.mozilla.org/MPL/ +%% +%% Software distributed under the License is distributed on an "AS IS" +%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the +%% License for the specific language governing rights and limitations +%% under the License. +%% +%% The Original Code is RabbitMQ Management Plugin. +%% +%% The Initial Developer of the Original Code is GoPivotal, Inc. +%% Copyright (c) 2010-2015 Pivotal Software, Inc. All rights reserved. +%% + +-module(rabbit_mgmt_wm_policies). + +-export([init/1, to_json/2, content_types_provided/2, is_authorized/2, + resource_exists/2, basic/1]). + +-include("rabbit_mgmt.hrl"). +-include_lib("webmachine/include/webmachine.hrl"). +-include_lib("rabbit_common/include/rabbit.hrl"). + +%%-------------------------------------------------------------------- + +init(_Config) -> {ok, #context{}}. + +content_types_provided(ReqData, Context) -> + {[{"application/json", to_json}], ReqData, Context}. + +resource_exists(ReqData, Context) -> + {case basic(ReqData) of + not_found -> false; + _ -> true + end, ReqData, Context}. + +to_json(ReqData, Context) -> + rabbit_mgmt_util:reply_list( + rabbit_mgmt_util:filter_vhost(basic(ReqData), ReqData, Context), + ["priority"], ReqData, Context). + +is_authorized(ReqData, Context) -> + rabbit_mgmt_util:is_authorized_policies(ReqData, Context). + +%%-------------------------------------------------------------------- + +basic(ReqData) -> + case rabbit_mgmt_util:vhost(ReqData) of + not_found -> not_found; + none -> rabbit_policy:list(); + VHost -> rabbit_policy:list(VHost) + end. diff --git a/rabbitmq-server/plugins-src/rabbitmq-management/src/rabbit_mgmt_wm_policy.erl b/rabbitmq-server/plugins-src/rabbitmq-management/src/rabbit_mgmt_wm_policy.erl new file mode 100644 index 0000000..fa9e1aa --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-management/src/rabbit_mgmt_wm_policy.erl @@ -0,0 +1,88 @@ +%% The contents of this file are subject to the Mozilla Public License +%% Version 1.1 (the "License"); you may not use this file except in +%% compliance with the License. You may obtain a copy of the License at +%% http://www.mozilla.org/MPL/ +%% +%% Software distributed under the License is distributed on an "AS IS" +%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the +%% License for the specific language governing rights and limitations +%% under the License. +%% +%% The Original Code is RabbitMQ Management Plugin. +%% +%% The Initial Developer of the Original Code is GoPivotal, Inc. +%% Copyright (c) 2010-2015 Pivotal Software, Inc. All rights reserved. +%% + +-module(rabbit_mgmt_wm_policy). + +-export([init/1, resource_exists/2, to_json/2, + content_types_provided/2, content_types_accepted/2, + is_authorized/2, allowed_methods/2, accept_content/2, + delete_resource/2]). + +-import(rabbit_misc, [pget/2]). + +-include("rabbit_mgmt.hrl"). +-include_lib("webmachine/include/webmachine.hrl"). +-include_lib("rabbit_common/include/rabbit.hrl"). + +%%-------------------------------------------------------------------- + +init(_Config) -> {ok, #context{}}. + +content_types_provided(ReqData, Context) -> + {[{"application/json", to_json}], ReqData, Context}. + +content_types_accepted(ReqData, Context) -> + {[{"application/json", accept_content}], ReqData, Context}. + +allowed_methods(ReqData, Context) -> + {['HEAD', 'GET', 'PUT', 'DELETE'], ReqData, Context}. + +resource_exists(ReqData, Context) -> + {case policy(ReqData) of + not_found -> false; + _ -> true + end, ReqData, Context}. + +to_json(ReqData, Context) -> + rabbit_mgmt_util:reply(policy(ReqData), ReqData, Context). + +accept_content(ReqData, Context) -> + case rabbit_mgmt_util:vhost(ReqData) of + not_found -> + rabbit_mgmt_util:not_found(vhost_not_found, ReqData, Context); + VHost -> + rabbit_mgmt_util:with_decode( + [pattern, definition], ReqData, Context, + fun([Pattern, Definition], Body) -> + case rabbit_policy:set( + VHost, name(ReqData), Pattern, + rabbit_misc:json_to_term(Definition), + proplists:get_value(priority, Body), + proplists:get_value('apply-to', Body)) of + ok -> + {true, ReqData, Context}; + {error_string, Reason} -> + rabbit_mgmt_util:bad_request( + list_to_binary(Reason), ReqData, Context) + end + end) + end. + +delete_resource(ReqData, Context) -> + ok = rabbit_policy:delete( + rabbit_mgmt_util:vhost(ReqData), name(ReqData)), + {true, ReqData, Context}. + +is_authorized(ReqData, Context) -> + rabbit_mgmt_util:is_authorized_policies(ReqData, Context). + +%%-------------------------------------------------------------------- + +policy(ReqData) -> + rabbit_policy:lookup( + rabbit_mgmt_util:vhost(ReqData), name(ReqData)). + +name(ReqData) -> rabbit_mgmt_util:id(name, ReqData). diff --git a/rabbitmq-server/plugins-src/rabbitmq-management/src/rabbit_mgmt_wm_queue.erl b/rabbitmq-server/plugins-src/rabbitmq-management/src/rabbit_mgmt_wm_queue.erl new file mode 100644 index 0000000..fb7e8ab --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-management/src/rabbit_mgmt_wm_queue.erl @@ -0,0 +1,84 @@ +%% The contents of this file are subject to the Mozilla Public License +%% Version 1.1 (the "License"); you may not use this file except in +%% compliance with the License. You may obtain a copy of the License at +%% http://www.mozilla.org/MPL/ +%% +%% Software distributed under the License is distributed on an "AS IS" +%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the +%% License for the specific language governing rights and limitations +%% under the License. +%% +%% The Original Code is RabbitMQ Management Plugin. +%% +%% The Initial Developer of the Original Code is GoPivotal, Inc. +%% Copyright (c) 2010-2015 Pivotal Software, Inc. All rights reserved. +%% + +-module(rabbit_mgmt_wm_queue). + +-export([init/1, resource_exists/2, to_json/2, + content_types_provided/2, content_types_accepted/2, + is_authorized/2, allowed_methods/2, accept_content/2, + delete_resource/2, queue/1, queue/2]). + +-include("rabbit_mgmt.hrl"). +-include_lib("webmachine/include/webmachine.hrl"). +-include_lib("amqp_client/include/amqp_client.hrl"). + +%%-------------------------------------------------------------------- +init(_Config) -> {ok, #context{}}. + +content_types_provided(ReqData, Context) -> + {[{"application/json", to_json}], ReqData, Context}. + +content_types_accepted(ReqData, Context) -> + {[{"application/json", accept_content}], ReqData, Context}. + +allowed_methods(ReqData, Context) -> + {['HEAD', 'GET', 'PUT', 'DELETE'], ReqData, Context}. + +resource_exists(ReqData, Context) -> + {case queue(ReqData) of + not_found -> false; + _ -> true + end, ReqData, Context}. + +to_json(ReqData, Context) -> + [Q] = rabbit_mgmt_db:augment_queues( + [queue(ReqData)], rabbit_mgmt_util:range_ceil(ReqData), full), + rabbit_mgmt_util:reply(rabbit_mgmt_format:strip_pids(Q), ReqData, Context). + +accept_content(ReqData, Context) -> + rabbit_mgmt_util:http_to_amqp( + 'queue.declare', ReqData, Context, + [{fun rabbit_mgmt_util:parse_bool/1, [durable, auto_delete]}], + [{queue, rabbit_mgmt_util:id(queue, ReqData)}]). + +delete_resource(ReqData, Context) -> + rabbit_mgmt_util:amqp_request( + rabbit_mgmt_util:vhost(ReqData), + ReqData, Context, + #'queue.delete'{ queue = rabbit_mgmt_util:id(queue, ReqData), + if_empty = qs_true("if-empty", ReqData), + if_unused = qs_true("if-unused", ReqData) }). + +is_authorized(ReqData, Context) -> + rabbit_mgmt_util:is_authorized_vhost(ReqData, Context). + +%%-------------------------------------------------------------------- + +queue(ReqData) -> + case rabbit_mgmt_util:vhost(ReqData) of + not_found -> not_found; + VHost -> queue(VHost, rabbit_mgmt_util:id(queue, ReqData)) + end. + + +queue(VHost, QName) -> + Name = rabbit_misc:r(VHost, queue, QName), + case rabbit_amqqueue:lookup(Name) of + {ok, Q} -> rabbit_mgmt_format:queue(Q); + {error, not_found} -> not_found + end. + +qs_true(Key, ReqData) -> "true" =:= wrq:get_qs_value(Key, ReqData). diff --git a/rabbitmq-server/plugins-src/rabbitmq-management/src/rabbit_mgmt_wm_queue_actions.erl b/rabbitmq-server/plugins-src/rabbitmq-management/src/rabbit_mgmt_wm_queue_actions.erl new file mode 100644 index 0000000..a601764 --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-management/src/rabbit_mgmt_wm_queue_actions.erl @@ -0,0 +1,70 @@ +%% The contents of this file are subject to the Mozilla Public License +%% Version 1.1 (the "License"); you may not use this file except in +%% compliance with the License. You may obtain a copy of the License at +%% http://www.mozilla.org/MPL/ +%% +%% Software distributed under the License is distributed on an "AS IS" +%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the +%% License for the specific language governing rights and limitations +%% under the License. +%% +%% The Original Code is RabbitMQ Management Plugin. +%% +%% The Initial Developer of the Original Code is GoPivotal, Inc. +%% Copyright (c) 2011-2012 GoPivotal, Inc. All rights reserved. +%% + +-module(rabbit_mgmt_wm_queue_actions). + +-export([init/1, resource_exists/2, post_is_create/2, is_authorized/2, + allowed_methods/2, process_post/2]). + +-include("rabbit_mgmt.hrl"). +-include_lib("webmachine/include/webmachine.hrl"). +-include_lib("amqp_client/include/amqp_client.hrl"). + +%%-------------------------------------------------------------------- + +init(_Config) -> {ok, #context{}}. + +allowed_methods(ReqData, Context) -> + {['POST'], ReqData, Context}. + +resource_exists(ReqData, Context) -> + {case rabbit_mgmt_wm_queue:queue(ReqData) of + not_found -> false; + _ -> true + end, ReqData, Context}. + +post_is_create(ReqData, Context) -> + {false, ReqData, Context}. + +process_post(ReqData, Context) -> + rabbit_mgmt_util:post_respond(do_it(ReqData, Context)). + +do_it(ReqData, Context) -> + VHost = rabbit_mgmt_util:vhost(ReqData), + QName = rabbit_mgmt_util:id(queue, ReqData), + rabbit_mgmt_util:with_decode( + [action], ReqData, Context, + fun([Action], _Body) -> + rabbit_amqqueue:with( + rabbit_misc:r(VHost, queue, QName), + fun(Q) -> action(Action, Q, ReqData, Context) end) + end). + +is_authorized(ReqData, Context) -> + rabbit_mgmt_util:is_authorized_admin(ReqData, Context). + +%%-------------------------------------------------------------------- + +action(<<"sync">>, #amqqueue{pid = QPid}, ReqData, Context) -> + spawn(fun() -> rabbit_amqqueue:sync_mirrors(QPid) end), + {true, ReqData, Context}; + +action(<<"cancel_sync">>, #amqqueue{pid = QPid}, ReqData, Context) -> + rabbit_amqqueue:cancel_sync_mirrors(QPid), + {true, ReqData, Context}; + +action(Else, _Q, ReqData, Context) -> + rabbit_mgmt_util:bad_request({unknown, Else}, ReqData, Context). diff --git a/rabbitmq-server/plugins-src/rabbitmq-management/src/rabbit_mgmt_wm_queue_get.erl b/rabbitmq-server/plugins-src/rabbitmq-management/src/rabbit_mgmt_wm_queue_get.erl new file mode 100644 index 0000000..8998bfc --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-management/src/rabbit_mgmt_wm_queue_get.erl @@ -0,0 +1,124 @@ +%% The contents of this file are subject to the Mozilla Public License +%% Version 1.1 (the "License"); you may not use this file except in +%% compliance with the License. You may obtain a copy of the License at +%% http://www.mozilla.org/MPL/ +%% +%% Software distributed under the License is distributed on an "AS IS" +%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the +%% License for the specific language governing rights and limitations +%% under the License. +%% +%% The Original Code is RabbitMQ Management Plugin. +%% +%% The Initial Developer of the Original Code is GoPivotal, Inc. +%% Copyright (c) 2011-2015 Pivotal Software, Inc. All rights reserved. +%% + +-module(rabbit_mgmt_wm_queue_get). + +-export([init/1, resource_exists/2, post_is_create/2, is_authorized/2, + allowed_methods/2, process_post/2]). + +-include("rabbit_mgmt.hrl"). +-include_lib("webmachine/include/webmachine.hrl"). +-include_lib("amqp_client/include/amqp_client.hrl"). + +%%-------------------------------------------------------------------- + +init(_Config) -> {ok, #context{}}. + +allowed_methods(ReqData, Context) -> + {['POST'], ReqData, Context}. + +resource_exists(ReqData, Context) -> + {case rabbit_mgmt_wm_queue:queue(ReqData) of + not_found -> false; + _ -> true + end, ReqData, Context}. + +post_is_create(ReqData, Context) -> + {false, ReqData, Context}. + +process_post(ReqData, Context) -> + rabbit_mgmt_util:post_respond(do_it(ReqData, Context)). + +do_it(ReqData, Context) -> + VHost = rabbit_mgmt_util:vhost(ReqData), + Q = rabbit_mgmt_util:id(queue, ReqData), + rabbit_mgmt_util:with_decode( + [requeue, count, encoding], ReqData, Context, + fun([RequeueBin, CountBin, EncBin], Body) -> + rabbit_mgmt_util:with_channel( + VHost, ReqData, Context, + fun (Ch) -> + NoAck = not rabbit_mgmt_util:parse_bool(RequeueBin), + Count = rabbit_mgmt_util:parse_int(CountBin), + Enc = case EncBin of + <<"auto">> -> auto; + <<"base64">> -> base64; + _ -> throw({error, + {bad_encoding, + EncBin}}) + end, + Trunc = case proplists:get_value(truncate, Body) of + undefined -> none; + TruncBin -> rabbit_mgmt_util:parse_int( + TruncBin) + end, + rabbit_mgmt_util:reply( + basic_gets(Count, Ch, Q, NoAck, Enc, Trunc), + ReqData, Context) + end) + end). + +basic_gets(0, _, _, _, _, _) -> + []; + +basic_gets(Count, Ch, Q, NoAck, Enc, Trunc) -> + case basic_get(Ch, Q, NoAck, Enc, Trunc) of + none -> []; + M -> [M | basic_gets(Count - 1, Ch, Q, NoAck, Enc, Trunc)] + end. + +basic_get(Ch, Q, NoAck, Enc, Trunc) -> + case amqp_channel:call(Ch, #'basic.get'{queue = Q, + no_ack = NoAck}) of + {#'basic.get_ok'{redelivered = Redelivered, + exchange = Exchange, + routing_key = RoutingKey, + message_count = MessageCount}, + #amqp_msg{props = Props, payload = Payload}} -> + [{payload_bytes, size(Payload)}, + {redelivered, Redelivered}, + {exchange, Exchange}, + {routing_key, RoutingKey}, + {message_count, MessageCount}, + {properties, rabbit_mgmt_format:basic_properties(Props)}] ++ + payload_part(maybe_truncate(Payload, Trunc), Enc); + #'basic.get_empty'{} -> + none + end. + +is_authorized(ReqData, Context) -> + rabbit_mgmt_util:is_authorized_vhost(ReqData, Context). + +%%-------------------------------------------------------------------- + +maybe_truncate(Payload, none) -> Payload; +maybe_truncate(Payload, Len) when size(Payload) < Len -> Payload; +maybe_truncate(Payload, Len) -> + <> = Payload, + Start. + +payload_part(Payload, Enc) -> + {PL, E} = case Enc of + auto -> try + %% TODO mochijson does this but is it safe? + xmerl_ucs:from_utf8(Payload), + {Payload, string} + catch exit:{ucs, _} -> + {base64:encode(Payload), base64} + end; + _ -> {base64:encode(Payload), base64} + end, + [{payload, PL}, {payload_encoding, E}]. diff --git a/rabbitmq-server/plugins-src/rabbitmq-management/src/rabbit_mgmt_wm_queue_purge.erl b/rabbitmq-server/plugins-src/rabbitmq-management/src/rabbit_mgmt_wm_queue_purge.erl new file mode 100644 index 0000000..42aeb95 --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-management/src/rabbit_mgmt_wm_queue_purge.erl @@ -0,0 +1,45 @@ +%% The contents of this file are subject to the Mozilla Public License +%% Version 1.1 (the "License"); you may not use this file except in +%% compliance with the License. You may obtain a copy of the License at +%% http://www.mozilla.org/MPL/ +%% +%% Software distributed under the License is distributed on an "AS IS" +%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the +%% License for the specific language governing rights and limitations +%% under the License. +%% +%% The Original Code is RabbitMQ Management Plugin. +%% +%% The Initial Developer of the Original Code is GoPivotal, Inc. +%% Copyright (c) 2010-2015 Pivotal Software, Inc. All rights reserved. +%% + +-module(rabbit_mgmt_wm_queue_purge). + +-export([init/1, resource_exists/2, is_authorized/2, allowed_methods/2, + delete_resource/2]). + +-include("rabbit_mgmt.hrl"). +-include_lib("webmachine/include/webmachine.hrl"). +-include_lib("amqp_client/include/amqp_client.hrl"). + +%%-------------------------------------------------------------------- +init(_Config) -> {ok, #context{}}. + +allowed_methods(ReqData, Context) -> + {['DELETE'], ReqData, Context}. + +resource_exists(ReqData, Context) -> + {case rabbit_mgmt_wm_queue:queue(ReqData) of + not_found -> false; + _ -> true + end, ReqData, Context}. + +delete_resource(ReqData, Context) -> + rabbit_mgmt_util:amqp_request( + rabbit_mgmt_util:vhost(ReqData), + ReqData, Context, + #'queue.purge'{ queue = rabbit_mgmt_util:id(queue, ReqData) }). + +is_authorized(ReqData, Context) -> + rabbit_mgmt_util:is_authorized_vhost(ReqData, Context). diff --git a/rabbitmq-server/plugins-src/rabbitmq-management/src/rabbit_mgmt_wm_queues.erl b/rabbitmq-server/plugins-src/rabbitmq-management/src/rabbit_mgmt_wm_queues.erl new file mode 100644 index 0000000..51265c7 --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-management/src/rabbit_mgmt_wm_queues.erl @@ -0,0 +1,62 @@ +%% The contents of this file are subject to the Mozilla Public License +%% Version 1.1 (the "License"); you may not use this file except in +%% compliance with the License. You may obtain a copy of the License at +%% http://www.mozilla.org/MPL/ +%% +%% Software distributed under the License is distributed on an "AS IS" +%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the +%% License for the specific language governing rights and limitations +%% under the License. +%% +%% The Original Code is RabbitMQ Management Plugin. +%% +%% The Initial Developer of the Original Code is GoPivotal, Inc. +%% Copyright (c) 2010-2015 Pivotal Software, Inc. All rights reserved. +%% + +-module(rabbit_mgmt_wm_queues). + +-export([init/1, to_json/2, content_types_provided/2, is_authorized/2, + resource_exists/2, basic/1, augmented/2]). + +-include("rabbit_mgmt.hrl"). +-include_lib("webmachine/include/webmachine.hrl"). +-include_lib("rabbit_common/include/rabbit.hrl"). + +%%-------------------------------------------------------------------- + +init(_Config) -> {ok, #context{}}. + +content_types_provided(ReqData, Context) -> + {[{"application/json", to_json}], ReqData, Context}. + +resource_exists(ReqData, Context) -> + {case queues0(ReqData) of + vhost_not_found -> false; + _ -> true + end, ReqData, Context}. + +to_json(ReqData, Context) -> + rabbit_mgmt_util:reply_list(augmented(ReqData, Context), ReqData, Context). + +is_authorized(ReqData, Context) -> + rabbit_mgmt_util:is_authorized_vhost(ReqData, Context). + +%%-------------------------------------------------------------------- + +augmented(ReqData, Context) -> + rabbit_mgmt_format:strip_pids( + rabbit_mgmt_db:augment_queues( + rabbit_mgmt_util:filter_vhost(basic(ReqData), ReqData, Context), + rabbit_mgmt_util:range_ceil(ReqData), basic)). + +basic(ReqData) -> + [rabbit_mgmt_format:queue(Q) || Q <- queues0(ReqData)] ++ + [rabbit_mgmt_format:queue(Q#amqqueue{state = down}) || + Q <- down_queues(ReqData)]. + +queues0(ReqData) -> + rabbit_mgmt_util:all_or_one_vhost(ReqData, fun rabbit_amqqueue:list/1). + +down_queues(ReqData) -> + rabbit_mgmt_util:all_or_one_vhost(ReqData, fun rabbit_amqqueue:list_down/1). diff --git a/rabbitmq-server/plugins-src/rabbitmq-management/src/rabbit_mgmt_wm_user.erl b/rabbitmq-server/plugins-src/rabbitmq-management/src/rabbit_mgmt_wm_user.erl new file mode 100644 index 0000000..b8b6529 --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-management/src/rabbit_mgmt_wm_user.erl @@ -0,0 +1,109 @@ +%% The contents of this file are subject to the Mozilla Public License +%% Version 1.1 (the "License"); you may not use this file except in +%% compliance with the License. You may obtain a copy of the License at +%% http://www.mozilla.org/MPL/ +%% +%% Software distributed under the License is distributed on an "AS IS" +%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the +%% License for the specific language governing rights and limitations +%% under the License. +%% +%% The Original Code is RabbitMQ Management Plugin. +%% +%% The Initial Developer of the Original Code is GoPivotal, Inc. +%% Copyright (c) 2010-2015 Pivotal Software, Inc. All rights reserved. +%% + +-module(rabbit_mgmt_wm_user). + +-export([init/1, resource_exists/2, to_json/2, + content_types_provided/2, content_types_accepted/2, + is_authorized/2, allowed_methods/2, accept_content/2, + delete_resource/2, user/1, put_user/1]). + +-import(rabbit_misc, [pget/2]). + +-include("rabbit_mgmt.hrl"). +-include_lib("webmachine/include/webmachine.hrl"). +-include_lib("rabbit_common/include/rabbit.hrl"). + +%%-------------------------------------------------------------------- +init(_Config) -> {ok, #context{}}. + +content_types_provided(ReqData, Context) -> + {[{"application/json", to_json}], ReqData, Context}. + +content_types_accepted(ReqData, Context) -> + {[{"application/json", accept_content}], ReqData, Context}. + +allowed_methods(ReqData, Context) -> + {['HEAD', 'GET', 'PUT', 'DELETE'], ReqData, Context}. + +resource_exists(ReqData, Context) -> + {case user(ReqData) of + {ok, _} -> true; + {error, _} -> false + end, ReqData, Context}. + +to_json(ReqData, Context) -> + {ok, User} = user(ReqData), + rabbit_mgmt_util:reply(rabbit_mgmt_format:internal_user(User), + ReqData, Context). + +accept_content(ReqData, Context) -> + Username = rabbit_mgmt_util:id(user, ReqData), + rabbit_mgmt_util:with_decode( + [], ReqData, Context, + fun(_, User) -> + put_user([{name, Username} | User]), + {true, ReqData, Context} + end). + +delete_resource(ReqData, Context) -> + User = rabbit_mgmt_util:id(user, ReqData), + rabbit_auth_backend_internal:delete_user(User), + {true, ReqData, Context}. + +is_authorized(ReqData, Context) -> + rabbit_mgmt_util:is_authorized_admin(ReqData, Context). + +%%-------------------------------------------------------------------- + +user(ReqData) -> + rabbit_auth_backend_internal:lookup_user(rabbit_mgmt_util:id(user, ReqData)). + +put_user(User) -> + CP = fun rabbit_auth_backend_internal:change_password/2, + CPH = fun rabbit_auth_backend_internal:change_password_hash/2, + case {proplists:is_defined(password, User), + proplists:is_defined(password_hash, User)} of + {true, _} -> put_user(User, pget(password, User), CP); + {_, true} -> Hash = rabbit_mgmt_util:b64decode_or_throw( + pget(password_hash, User)), + put_user(User, Hash, CPH); + _ -> put_user(User, <<>>, CPH) + end. + +put_user(User, PWArg, PWFun) -> + Username = pget(name, User), + Tags = case {pget(tags, User), pget(administrator, User)} of + {undefined, undefined} -> + throw({error, tags_not_present}); + {undefined, AdminS} -> + case rabbit_mgmt_util:parse_bool(AdminS) of + true -> [administrator]; + false -> [] + end; + {TagsS, _} -> + [list_to_atom(string:strip(T)) || + T <- string:tokens(binary_to_list(TagsS), ",")] + end, + case rabbit_auth_backend_internal:lookup_user(Username) of + {error, not_found} -> + rabbit_auth_backend_internal:add_user( + Username, rabbit_guid:binary(rabbit_guid:gen_secure(), "tmp")); + _ -> + ok + end, + PWFun(Username, PWArg), + ok = rabbit_auth_backend_internal:set_tags(Username, Tags). diff --git a/rabbitmq-server/plugins-src/rabbitmq-management/src/rabbit_mgmt_wm_users.erl b/rabbitmq-server/plugins-src/rabbitmq-management/src/rabbit_mgmt_wm_users.erl new file mode 100644 index 0000000..7bf07bf --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-management/src/rabbit_mgmt_wm_users.erl @@ -0,0 +1,47 @@ +%% The contents of this file are subject to the Mozilla Public License +%% Version 1.1 (the "License"); you may not use this file except in +%% compliance with the License. You may obtain a copy of the License at +%% http://www.mozilla.org/MPL/ +%% +%% Software distributed under the License is distributed on an "AS IS" +%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the +%% License for the specific language governing rights and limitations +%% under the License. +%% +%% The Original Code is RabbitMQ Management Plugin. +%% +%% The Initial Developer of the Original Code is GoPivotal, Inc. +%% Copyright (c) 2010-2015 Pivotal Software, Inc. All rights reserved. +%% + +-module(rabbit_mgmt_wm_users). + +-export([init/1, to_json/2, content_types_provided/2, is_authorized/2]). +-export([users/0]). + +-import(rabbit_misc, [pget/2]). + +-include("rabbit_mgmt.hrl"). +-include_lib("webmachine/include/webmachine.hrl"). +-include_lib("rabbit_common/include/rabbit.hrl"). + +%%-------------------------------------------------------------------- + +init(_Config) -> {ok, #context{}}. + +content_types_provided(ReqData, Context) -> + {[{"application/json", to_json}], ReqData, Context}. + +to_json(ReqData, Context) -> + rabbit_mgmt_util:reply_list(users(), ReqData, Context). + +is_authorized(ReqData, Context) -> + rabbit_mgmt_util:is_authorized_admin(ReqData, Context). + +%%-------------------------------------------------------------------- + +users() -> + [begin + {ok, User} = rabbit_auth_backend_internal:lookup_user(pget(user, U)), + rabbit_mgmt_format:internal_user(User) + end || U <- rabbit_auth_backend_internal:list_users()]. diff --git a/rabbitmq-server/plugins-src/rabbitmq-management/src/rabbit_mgmt_wm_vhost.erl b/rabbitmq-server/plugins-src/rabbitmq-management/src/rabbit_mgmt_wm_vhost.erl new file mode 100644 index 0000000..0908d90 --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-management/src/rabbit_mgmt_wm_vhost.erl @@ -0,0 +1,83 @@ +%% The contents of this file are subject to the Mozilla Public License +%% Version 1.1 (the "License"); you may not use this file except in +%% compliance with the License. You may obtain a copy of the License at +%% http://www.mozilla.org/MPL/ +%% +%% Software distributed under the License is distributed on an "AS IS" +%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the +%% License for the specific language governing rights and limitations +%% under the License. +%% +%% The Original Code is RabbitMQ Management Plugin. +%% +%% The Initial Developer of the Original Code is GoPivotal, Inc. +%% Copyright (c) 2010-2015 Pivotal Software, Inc. All rights reserved. +%% + +-module(rabbit_mgmt_wm_vhost). + +-export([init/1, resource_exists/2, to_json/2, + content_types_provided/2, content_types_accepted/2, + is_authorized/2, allowed_methods/2, accept_content/2, + delete_resource/2, id/1, put_vhost/2]). + +-import(rabbit_misc, [pget/2]). + +-include("rabbit_mgmt.hrl"). +-include_lib("webmachine/include/webmachine.hrl"). +-include_lib("rabbit_common/include/rabbit.hrl"). + +%%-------------------------------------------------------------------- +init(_Config) -> {ok, #context{}}. + +content_types_provided(ReqData, Context) -> + {[{"application/json", to_json}], ReqData, Context}. + +content_types_accepted(ReqData, Context) -> + {[{"application/json", accept_content}], ReqData, Context}. + +allowed_methods(ReqData, Context) -> + {['HEAD', 'GET', 'PUT', 'DELETE'], ReqData, Context}. + +resource_exists(ReqData, Context) -> + {rabbit_vhost:exists(id(ReqData)), ReqData, Context}. + +to_json(ReqData, Context) -> + rabbit_mgmt_util:reply( + hd(rabbit_mgmt_db:augment_vhosts( + [rabbit_vhost:info(id(ReqData))], rabbit_mgmt_util:range(ReqData))), + ReqData, Context). + +accept_content(ReqData, Context) -> + Name = id(ReqData), + rabbit_mgmt_util:with_decode( + [], ReqData, Context, + fun(_, VHost) -> + put_vhost(Name, rabbit_mgmt_util:parse_bool( + pget(tracing, VHost))), + {true, ReqData, Context} + end). + +delete_resource(ReqData, Context) -> + VHost = id(ReqData), + rabbit_vhost:delete(VHost), + {true, ReqData, Context}. + +is_authorized(ReqData, Context) -> + rabbit_mgmt_util:is_authorized_admin(ReqData, Context). + +%%-------------------------------------------------------------------- + +id(ReqData) -> + rabbit_mgmt_util:id(vhost, ReqData). + +put_vhost(Name, Trace) -> + case rabbit_vhost:exists(Name) of + true -> ok; + false -> rabbit_vhost:add(Name) + end, + case Trace of + true -> rabbit_trace:start(Name); + false -> rabbit_trace:stop(Name); + undefined -> ok + end. diff --git a/rabbitmq-server/plugins-src/rabbitmq-management/src/rabbit_mgmt_wm_vhosts.erl b/rabbitmq-server/plugins-src/rabbitmq-management/src/rabbit_mgmt_wm_vhosts.erl new file mode 100644 index 0000000..961b6f7 --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-management/src/rabbit_mgmt_wm_vhosts.erl @@ -0,0 +1,47 @@ +%% The contents of this file are subject to the Mozilla Public License +%% Version 1.1 (the "License"); you may not use this file except in +%% compliance with the License. You may obtain a copy of the License at +%% http://www.mozilla.org/MPL/ +%% +%% Software distributed under the License is distributed on an "AS IS" +%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the +%% License for the specific language governing rights and limitations +%% under the License. +%% +%% The Original Code is RabbitMQ Management Plugin. +%% +%% The Initial Developer of the Original Code is GoPivotal, Inc. +%% Copyright (c) 2010-2015 Pivotal Software, Inc. All rights reserved. +%% + +-module(rabbit_mgmt_wm_vhosts). + +-export([init/1, to_json/2, content_types_provided/2, is_authorized/2]). +-export([basic/0, augmented/2]). + +-include("rabbit_mgmt.hrl"). +-include_lib("webmachine/include/webmachine.hrl"). +-include_lib("rabbit_common/include/rabbit.hrl"). + +%%-------------------------------------------------------------------- + +init(_Config) -> {ok, #context{}}. + +content_types_provided(ReqData, Context) -> + {[{"application/json", to_json}], ReqData, Context}. + +to_json(ReqData, Context) -> + rabbit_mgmt_util:reply_list(augmented(ReqData, Context), ReqData, Context). + +is_authorized(ReqData, Context) -> + rabbit_mgmt_util:is_authorized(ReqData, Context). + +%%-------------------------------------------------------------------- + +augmented(ReqData, #context{user = User}) -> + rabbit_mgmt_db:augment_vhosts( + [rabbit_vhost:info(V) || V <- rabbit_mgmt_util:list_visible_vhosts(User)], + rabbit_mgmt_util:range(ReqData)). + +basic() -> + rabbit_vhost:info_all([name]). diff --git a/rabbitmq-server/plugins-src/rabbitmq-management/src/rabbit_mgmt_wm_whoami.erl b/rabbitmq-server/plugins-src/rabbitmq-management/src/rabbit_mgmt_wm_whoami.erl new file mode 100644 index 0000000..564b394 --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-management/src/rabbit_mgmt_wm_whoami.erl @@ -0,0 +1,35 @@ +%% The contents of this file are subject to the Mozilla Public License +%% Version 1.1 (the "License"); you may not use this file except in +%% compliance with the License. You may obtain a copy of the License at +%% http://www.mozilla.org/MPL/ +%% +%% Software distributed under the License is distributed on an "AS IS" +%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the +%% License for the specific language governing rights and limitations +%% under the License. +%% +%% The Original Code is RabbitMQ Management Plugin. +%% +%% The Initial Developer of the Original Code is GoPivotal, Inc. +%% Copyright (c) 2010-2015 Pivotal Software, Inc. All rights reserved. +%% + +-module(rabbit_mgmt_wm_whoami). + +-export([init/1, to_json/2, content_types_provided/2, is_authorized/2]). + +-include("rabbit_mgmt.hrl"). +-include_lib("webmachine/include/webmachine.hrl"). +-include_lib("rabbit_common/include/rabbit.hrl"). + +%%-------------------------------------------------------------------- +init(_Config) -> {ok, #context{}}. + +content_types_provided(ReqData, Context) -> + {[{"application/json", to_json}], ReqData, Context}. + +to_json(ReqData, Context = #context{user = User}) -> + rabbit_mgmt_util:reply(rabbit_mgmt_format:user(User), ReqData, Context). + +is_authorized(ReqData, Context) -> + rabbit_mgmt_util:is_authorized(ReqData, Context). diff --git a/rabbitmq-server/plugins-src/rabbitmq-management/src/rabbitmq_management.app.src b/rabbitmq-server/plugins-src/rabbitmq-management/src/rabbitmq_management.app.src new file mode 100644 index 0000000..bf54277 --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-management/src/rabbitmq_management.app.src @@ -0,0 +1,18 @@ +{application, rabbitmq_management, + [{description, "RabbitMQ Management Console"}, + {vsn, "%%VSN%%"}, + {modules, []}, + {registered, []}, + {mod, {rabbit_mgmt_app, []}}, + {env, [{listener, [{port, 15672}]}, + {http_log_dir, none}, + {load_definitions, none}, + {rates_mode, basic}, + {sample_retention_policies, + %% List of {MaxAgeInSeconds, SampleEveryNSeconds} + [{global, [{605, 5}, {3660, 60}, {29400, 600}, {86400, 1800}]}, + {basic, [{605, 5}, {3600, 60}]}, + {detailed, [{10, 5}]}]} + ]}, + {applications, [kernel, stdlib, rabbit, xmerl, rabbitmq_web_dispatch, + amqp_client, rabbitmq_management_agent]}]}. diff --git a/rabbitmq-server/plugins-src/rabbitmq-management/test/src/default-config b/rabbitmq-server/plugins-src/rabbitmq-management/test/src/default-config new file mode 100644 index 0000000..b76eba8 --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-management/test/src/default-config @@ -0,0 +1,15 @@ +# rabbitmqadmin.conf.example START + +[non_default] +hostname = localhost +port = 25672 +username = guest +password = guest +declare_vhost = / # Used as default for declare / delete only +vhost = / # Used as default for declare / delete / list + +[bad_host] +hostname = rabbit.acme.com +port = 15672 +username = guest +password = guest diff --git a/rabbitmq-server/plugins-src/rabbitmq-management/test/src/rabbit_mgmt_test_clustering.erl b/rabbitmq-server/plugins-src/rabbitmq-management/test/src/rabbit_mgmt_test_clustering.erl new file mode 100644 index 0000000..642b427 --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-management/test/src/rabbit_mgmt_test_clustering.erl @@ -0,0 +1,98 @@ +%% The contents of this file are subject to the Mozilla Public License +%% Version 1.1 (the "License"); you may not use this file except in +%% compliance with the License. You may obtain a copy of the License at +%% http://www.mozilla.org/MPL/ +%% +%% Software distributed under the License is distributed on an "AS IS" +%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the +%% License for the specific language governing rights and limitations +%% under the License. +%% +%% The Original Code is RabbitMQ Management Console. +%% +%% The Initial Developers of the Original Code are Rabbit Technologies Ltd. +%% +%% Copyright (C) 2010 Rabbit Technologies Ltd. +%% +%% All Rights Reserved. +%% +%% Contributor(s): ______________________________________. +%% +-module(rabbit_mgmt_test_clustering). + +-compile(export_all). +-include("rabbit_mgmt_test.hrl"). + +-import(rabbit_mgmt_test_http, [http_get/1, http_put/3, http_delete/2]). +-import(rabbit_misc, [pget/2]). + +%%---------------------------------------------------------------------------- + +cluster_nodes_with() -> cluster_ab. +cluster_nodes([_A, _B]) -> + ?assertEqual(2, length(http_get("/nodes"))), + ok. + +ha_with() -> cluster_ab. +ha([RabbitCfg, HareCfg]) -> + Rabbit = pget(nodename, RabbitCfg), + Hare = pget(nodename, HareCfg), + Policy = [{pattern, <<".*">>}, + {definition, [{'ha-mode', <<"all">>}]}], + http_put("/policies/%2f/HA", Policy, ?NO_CONTENT), + QArgs = [{node, list_to_binary(atom_to_list(Hare))}], + http_put("/queues/%2f/ha-queue", QArgs, ?NO_CONTENT), + Q = wait_for("/queues/%2f/ha-queue"), + assert_node(Hare, pget(node, Q)), + assert_single_node(Rabbit, pget(slave_nodes, Q)), + assert_single_node(Rabbit, pget(synchronised_slave_nodes, Q)), + _HareCfg2 = rabbit_test_configs:restart_node(HareCfg), + + Q2 = wait_for("/queues/%2f/ha-queue"), + assert_node(Rabbit, pget(node, Q2)), + assert_single_node(Hare, pget(slave_nodes, Q2)), + assert_single_node(Hare, pget(synchronised_slave_nodes, Q2)), + http_delete("/queues/%2f/ha-queue", ?NO_CONTENT), + http_delete("/policies/%2f/HA", ?NO_CONTENT), + ok. + +%%---------------------------------------------------------------------------- + +wait_for(Path) -> + wait_for(Path, [slave_nodes, synchronised_slave_nodes]). + +wait_for(Path, Keys) -> + wait_for(Path, Keys, 1000). + +wait_for(Path, Keys, 0) -> + exit({timeout, {Path, Keys}}); + +wait_for(Path, Keys, Count) -> + Res = http_get(Path), + case present(Keys, Res) of + false -> timer:sleep(10), + wait_for(Path, Keys, Count - 1); + true -> Res + end. + +present(Keys, Res) -> + lists:all(fun (Key) -> + X = pget(Key, Res), + X =/= [] andalso X =/= undefined + end, Keys). + +assert_single_node(Exp, Act) -> + ?assertEqual(1, length(Act)), + assert_node(Exp, hd(Act)). + +assert_nodes(Exp, Act0) -> + Act = [read_node(A) || A <- Act0], + ?debugVal({Exp, Act}), + ?assertEqual(length(Exp), length(Act)), + [?assert(lists:member(E, Act)) || E <- Exp]. + +assert_node(Exp, Act) -> + ?assertEqual(Exp, read_node(Act)). + +read_node(N) -> + list_to_atom(hd(string:tokens(binary_to_list(N), "@"))). diff --git a/rabbitmq-server/plugins-src/rabbitmq-management/test/src/rabbit_mgmt_test_db.erl b/rabbitmq-server/plugins-src/rabbitmq-management/test/src/rabbit_mgmt_test_db.erl new file mode 100644 index 0000000..528ec33 --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-management/test/src/rabbit_mgmt_test_db.erl @@ -0,0 +1,296 @@ +%% The contents of this file are subject to the Mozilla Public License +%% Version 1.1 (the "License"); you may not use this file except in +%% compliance with the License. You may obtain a copy of the License at +%% http://www.mozilla.org/MPL/ +%% +%% Software distributed under the License is distributed on an "AS IS" +%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the +%% License for the specific language governing rights and limitations +%% under the License. +%% +%% The Original Code is RabbitMQ Management Console. +%% +%% The Initial Developer of the Original Code is GoPivotal, Inc. +%% Copyright (c) 2010-2015 Pivotal Software, Inc. All rights reserved. +%% + +-module(rabbit_mgmt_test_db). + +-include("rabbit_mgmt.hrl"). +-include_lib("eunit/include/eunit.hrl"). +-include_lib("rabbit_common/include/rabbit.hrl"). + +-import(rabbit_misc, [pget/2]). +-import(rabbit_mgmt_test_util, [assert_list/2, assert_item/2, test_item/2]). + +-define(debugVal2(E), + ((fun (__V) -> + ?debugFmt(<<"~s = ~p">>, [(??E), __V]), + __V + end)(E))). + +%%---------------------------------------------------------------------------- +%% Tests +%%---------------------------------------------------------------------------- + +queue_coarse_test() -> + rabbit_mgmt_db:override_lookups([{exchange, fun dummy_lookup/1}, + {queue, fun dummy_lookup/1}]), + create_q(test, 0), + create_q(test2, 0), + stats_q(test, 0, 10), + stats_q(test2, 0, 1), + R = range(0, 1, 1), + Exp = fun(N) -> simple_details(messages, N, R) end, + assert_item(Exp(10), get_q(test, R)), + assert_item(Exp(11), get_vhost(R)), + assert_item(Exp(11), get_overview_q(R)), + delete_q(test, 0), + assert_item(Exp(1), get_vhost(R)), + assert_item(Exp(1), get_overview_q(R)), + delete_q(test2, 0), + assert_item(Exp(0), get_vhost(R)), + assert_item(Exp(0), get_overview_q(R)), + rabbit_mgmt_db:reset_lookups(), + ok. + +connection_coarse_test() -> + create_conn(test, 0), + create_conn(test2, 0), + stats_conn(test, 0, 10), + stats_conn(test2, 0, 1), + R = range(0, 1, 1), + Exp = fun(N) -> simple_details(recv_oct, N, R) end, + assert_item(Exp(10), get_conn(test, R)), + assert_item(Exp(1), get_conn(test2, R)), + delete_conn(test, 1), + delete_conn(test2, 1), + assert_list([], rabbit_mgmt_db:get_all_connections(R)), + ok. + +fine_stats_aggregation_test() -> + rabbit_mgmt_db:override_lookups([{exchange, fun dummy_lookup/1}, + {queue, fun dummy_lookup/1}]), + create_ch(ch1, 0), + create_ch(ch2, 0), + stats_ch(ch1, 0, [{x, 100}], [{q1, x, 100}, + {q2, x, 10}], [{q1, 2}, + {q2, 1}]), + stats_ch(ch2, 0, [{x, 10}], [{q1, x, 50}, + {q2, x, 5}], []), + fine_stats_aggregation_test0(true), + delete_q(q2, 0), + fine_stats_aggregation_test0(false), + delete_ch(ch1, 1), + delete_ch(ch2, 1), + rabbit_mgmt_db:reset_lookups(), + ok. + +fine_stats_aggregation_test0(Q2Exists) -> + R = range(0, 1, 1), + Ch1 = get_ch(ch1, R), + Ch2 = get_ch(ch2, R), + X = get_x(x, R), + Q1 = get_q(q1, R), + V = get_vhost(R), + O = get_overview(R), + assert_fine_stats(m, publish, 100, Ch1, R), + assert_fine_stats(m, publish, 10, Ch2, R), + assert_fine_stats(m, publish_in, 110, X, R), + assert_fine_stats(m, publish_out, 165, X, R), + assert_fine_stats(m, publish, 150, Q1, R), + assert_fine_stats(m, deliver_get, 2, Q1, R), + assert_fine_stats(m, deliver_get, 3, Ch1, R), + assert_fine_stats(m, publish, 110, V, R), + assert_fine_stats(m, deliver_get, 3, V, R), + assert_fine_stats(m, publish, 110, O, R), + assert_fine_stats(m, deliver_get, 3, O, R), + assert_fine_stats({pub, x}, publish, 100, Ch1, R), + assert_fine_stats({pub, x}, publish, 10, Ch2, R), + assert_fine_stats({in, ch1}, publish, 100, X, R), + assert_fine_stats({in, ch2}, publish, 10, X, R), + assert_fine_stats({out, q1}, publish, 150, X, R), + assert_fine_stats({in, x}, publish, 150, Q1, R), + assert_fine_stats({del, ch1}, deliver_get, 2, Q1, R), + assert_fine_stats({del, q1}, deliver_get, 2, Ch1, R), + case Q2Exists of + true -> Q2 = get_q(q2, R), + assert_fine_stats(m, publish, 15, Q2, R), + assert_fine_stats(m, deliver_get, 1, Q2, R), + assert_fine_stats({out, q2}, publish, 15, X, R), + assert_fine_stats({in, x}, publish, 15, Q2, R), + assert_fine_stats({del, ch1}, deliver_get, 1, Q2, R), + assert_fine_stats({del, q2}, deliver_get, 1, Ch1, R); + false -> assert_fine_stats_neg({out, q2}, X), + assert_fine_stats_neg({del, q2}, Ch1) + end, + ok. + +fine_stats_aggregation_time_test() -> + rabbit_mgmt_db:override_lookups([{exchange, fun dummy_lookup/1}, + {queue, fun dummy_lookup/1}]), + create_ch(ch, 0), + stats_ch(ch, 0, [{x, 100}], [{q, x, 50}], [{q, 20}]), + stats_ch(ch, 5, [{x, 110}], [{q, x, 55}], [{q, 22}]), + + R1 = range(0, 1, 1), + assert_fine_stats(m, publish, 100, get_ch(ch, R1), R1), + assert_fine_stats(m, publish, 50, get_q(q, R1), R1), + assert_fine_stats(m, deliver_get, 20, get_q(q, R1), R1), + + R2 = range(5, 6, 1), + assert_fine_stats(m, publish, 110, get_ch(ch, R2), R2), + assert_fine_stats(m, publish, 55, get_q(q, R2), R2), + assert_fine_stats(m, deliver_get, 22, get_q(q, R2), R2), + + delete_q(q, 0), + delete_ch(ch, 1), + rabbit_mgmt_db:reset_lookups(), + ok. + +assert_fine_stats(m, Type, N, Obj, R) -> + Act = pget(message_stats, Obj), + assert_item(simple_details(Type, N, R), Act); +assert_fine_stats({T2, Name}, Type, N, Obj, R) -> + Act = find_detailed_stats(Name, pget(expand(T2), Obj)), + assert_item(simple_details(Type, N, R), Act). + +assert_fine_stats_neg({T2, Name}, Obj) -> + detailed_stats_absent(Name, pget(expand(T2), Obj)). + +%%---------------------------------------------------------------------------- +%% Events in +%%---------------------------------------------------------------------------- + +create_q(Name, Timestamp) -> + %% Technically we do not need this, the DB ignores it, but let's + %% be symmetrical... + event(queue_created, [{name, q(Name)}], Timestamp). + +create_conn(Name, Timestamp) -> + event(connection_created, [{pid, pid(Name)}, + {name, a2b(Name)}], Timestamp). + +create_ch(Name, Timestamp) -> + event(channel_created, [{pid, pid(Name)}, + {name, a2b(Name)}], Timestamp). + +stats_q(Name, Timestamp, Msgs) -> + event(queue_stats, [{name, q(Name)}, + {messages, Msgs}], Timestamp). + +stats_conn(Name, Timestamp, Oct) -> + event(connection_stats, [{pid , pid(Name)}, + {recv_oct, Oct}], Timestamp). + +stats_ch(Name, Timestamp, XStats, QXStats, QStats) -> + XStats1 = [{x(XName), [{publish, N}]} || {XName, N} <- XStats], + QXStats1 = [{{q(QName), x(XName)}, [{publish, N}]} + || {QName, XName, N} <- QXStats], + QStats1 = [{q(QName), [{deliver_no_ack, N}]} || {QName, N} <- QStats], + event(channel_stats, + [{pid, pid(Name)}, + {channel_exchange_stats, XStats1}, + {channel_queue_exchange_stats, QXStats1}, + {channel_queue_stats, QStats1}], Timestamp). + +delete_q(Name, Timestamp) -> + event(queue_deleted, [{name, q(Name)}], Timestamp). + +delete_conn(Name, Timestamp) -> + event(connection_closed, [{pid, pid_del(Name)}], Timestamp). + +delete_ch(Name, Timestamp) -> + event(channel_closed, [{pid, pid_del(Name)}], Timestamp). + +event(Type, Stats, Timestamp) -> + ok = gen_server:call(rabbit_mgmt_db, + {event, #event{type = Type, + props = Stats, + reference = none, + timestamp = sec_to_triple(Timestamp)}}). + +sec_to_triple(Sec) -> {Sec div 1000000, Sec rem 1000000, 0}. + +%%---------------------------------------------------------------------------- +%% Events out +%%---------------------------------------------------------------------------- + +range(F, L, I) -> + R = #range{first = F * 1000, last = L * 1000, incr = I * 1000}, + {R, R, R, R}. + +get_x(Name, Range) -> + [X] = rabbit_mgmt_db:augment_exchanges([x2(Name)], Range, full), + X. + +get_q(Name, Range) -> + [Q] = rabbit_mgmt_db:augment_queues([q2(Name)], Range, full), + Q. + +get_vhost(Range) -> + [VHost] = rabbit_mgmt_db:augment_vhosts([[{name, <<"/">>}]], Range), + VHost. + +get_conn(Name, Range) -> rabbit_mgmt_db:get_connection(a2b(Name), Range). +get_ch(Name, Range) -> rabbit_mgmt_db:get_channel(a2b(Name), Range). + +get_overview(Range) -> rabbit_mgmt_db:get_overview(Range). +get_overview_q(Range) -> pget(queue_totals, get_overview(Range)). + +details0(R, AR, A, L) -> + [{rate, R}, + {samples, [[{sample, S}, {timestamp, T}] || {T, S} <- L]}, + {avg_rate, AR}, + {avg, A}]. + +simple_details(Thing, N, {#range{first = First, last = Last}, _, _, _}) -> + [{Thing, N}, + {atom_suffix(Thing, "_details"), + details0(0.0, 0.0, N * 1.0, [{Last, N}, {First, N}])}]. + +atom_suffix(Atom, Suffix) -> + list_to_atom(atom_to_list(Atom) ++ Suffix). + +find_detailed_stats(Name, List) -> + [S] = filter_detailed_stats(Name, List), + S. + +detailed_stats_absent(Name, List) -> + [] = filter_detailed_stats(Name, List). + +filter_detailed_stats(Name, List) -> + [Stats || [{stats, Stats}, {_, Details}] <- List, + pget(name, Details) =:= a2b(Name)]. + +expand(in) -> incoming; +expand(out) -> outgoing; +expand(del) -> deliveries; +expand(pub) -> publishes. + +%%---------------------------------------------------------------------------- +%% Util +%%---------------------------------------------------------------------------- + +x(Name) -> rabbit_misc:r(<<"/">>, exchange, a2b(Name)). +x2(Name) -> q2(Name). +q(Name) -> rabbit_misc:r(<<"/">>, queue, a2b(Name)). +q2(Name) -> [{name, a2b(Name)}, + {vhost, <<"/">>}]. + +pid(Name) -> + case get({pid, Name}) of + undefined -> P = spawn(fun() -> ok end), + put({pid, Name}, P), + P; + Pid -> Pid + end. + +pid_del(Name) -> + Pid = pid(Name), + erase({pid, Name}), + Pid. + +a2b(A) -> list_to_binary(atom_to_list(A)). + +dummy_lookup(_Thing) -> {ok, ignore_this}. diff --git a/rabbitmq-server/plugins-src/rabbitmq-management/test/src/rabbit_mgmt_test_db_unit.erl b/rabbitmq-server/plugins-src/rabbitmq-management/test/src/rabbit_mgmt_test_db_unit.erl new file mode 100644 index 0000000..80af615 --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-management/test/src/rabbit_mgmt_test_db_unit.erl @@ -0,0 +1,135 @@ +%% The contents of this file are subject to the Mozilla Public License +%% Version 1.1 (the "License"); you may not use this file except in +%% compliance with the License. You may obtain a copy of the License at +%% http://www.mozilla.org/MPL/ +%% +%% Software distributed under the License is distributed on an "AS IS" +%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the +%% License for the specific language governing rights and limitations +%% under the License. +%% +%% The Original Code is RabbitMQ Management Console. +%% +%% The Initial Developer of the Original Code is GoPivotal, Inc. +%% Copyright (c) 2010-2012 GoPivotal, Inc. All rights reserved. +%% + +-module(rabbit_mgmt_test_db_unit). + +-include("rabbit_mgmt.hrl"). +-include_lib("eunit/include/eunit.hrl"). + +gc_test() -> + T = fun (Before, After) -> + ?assertEqual(After, unstats( + rabbit_mgmt_stats:gc( + cutoff(), stats(Before)))) + end, + %% Cut off old sample, move to base + T({[{8999, 123}, {9000, 456}], 0}, + {[{9000, 456}], 123}), + %% Amalgamate old samples to rounder one + T({[{9001, 100}, {9010, 020}, {10000, 003}], 0}, + {[{10000, 123}], 0}), + %% The same, but a bit less + T({[{9000, 100}, {9901, 020}, {9910, 003}], 0}, + {[{9000, 100}, {9910, 023}], 0}), + %% Nothing needs to be done + T({[{9000, 100}, {9990, 020}, {9991, 003}], 0}, + {[{9000, 100}, {9990, 020}, {9991, 003}], 0}), + %% Invent a newer sample that's acceptable + T({[{9001, 10}, {9010, 02}], 0}, + {[{9100, 12}], 0}), + %% ...but don't if it's too old + T({[{8001, 10}, {8010, 02}], 0}, + {[], 12}), + ok. + +format_test() -> + Interval = 10, + T = fun ({First, Last, Incr}, Stats, Results) -> + ?assertEqual(format(Results), + rabbit_mgmt_stats:format( + #range{first = First * 1000, + last = Last * 1000, + incr = Incr * 1000}, + stats(Stats), + Interval * 1000)) + end, + + %% Just three samples, all of which we format. Note the + %% instantaneous rate is taken from the penultimate sample. + T({10, 30, 10}, {[{10, 10}, {20, 20}, {30, 30}], 1}, + {[{30, 61}, {20, 31}, {10, 11}], 2.0, 2.5, 103/3, 61}), + + %% Skip over the second (and ditto). + T({10, 30, 20}, {[{10, 10}, {20, 20}, {30, 30}], 1}, + {[{30, 61}, {10, 11}], 2.0, 2.5, 36.0, 61}), + + %% Skip over some and invent some. Note that the instantaneous + %% rate drops to 0 since the last event is now in the past. + T({0, 40, 20}, {[{10, 10}, {20, 20}, {30, 30}], 1}, + {[{40, 61}, {20, 31}, {0, 1}], 0.0, 1.5, 31.0, 61}), + + %% And a case where the range starts after the samples + T({20, 40, 10}, {[{10, 10}, {20, 20}, {30, 30}], 1}, + {[{40, 61}, {30, 61}, {20, 31}], 0.0, 1.5, 51.0, 61}), + + %% A single sample - which should lead to some bits not getting generated + T({10, 10, 10}, {[{10, 10}, {20, 20}, {30, 30}], 1}, + {[{10, 11}], 0.0, 11}), + + %% No samples - which should also lead to some bits not getting generated + T({10, 0, 10}, {[{10, 10}, {20, 20}, {30, 30}], 1}, + {[], 0.0, 1}), + + %% TODO more? + ok. + +format_no_range_test() -> + Interval = 10, + T = fun (Stats, Results) -> + ?assertEqual(format(Results), + rabbit_mgmt_stats:format( + no_range, stats(Stats), Interval * 1000)) + end, + + %% Just three samples + T({[{10, 10}, {20, 20}, {30, 30}], 1}, + {0.0, 61}), + ok. + + +%%-------------------------------------------------------------------- + +cutoff() -> + {[{10, 1}, {100, 10}, {1000, 100}], %% Sec + 10000000}. %% Millis + +stats({Diffs, Base}) -> + #stats{diffs = gb_trees:from_orddict(secs_to_millis(Diffs)), base = Base}. + +unstats(#stats{diffs = Diffs, base = Base}) -> + {millis_to_secs(gb_trees:to_list(Diffs)), Base}. + +secs_to_millis(L) -> [{TS * 1000, S} || {TS, S} <- L]. +millis_to_secs(L) -> [{TS div 1000, S} || {TS, S} <- L]. + +format({Rate, Count}) -> + {[{rate, Rate}], + Count}; + +format({Samples, Rate, Count}) -> + {[{rate, Rate}, + {samples, format_samples(Samples)}], + Count}; + +format({Samples, Rate, AvgRate, Avg, Count}) -> + {[{rate, Rate}, + {samples, format_samples(Samples)}, + {avg_rate, AvgRate}, + {avg, Avg}], + Count}. + +format_samples(Samples) -> + [[{sample, S}, {timestamp, TS * 1000}] || {TS, S} <- Samples]. diff --git a/rabbitmq-server/plugins-src/rabbitmq-management/test/src/rabbit_mgmt_test_http.erl b/rabbitmq-server/plugins-src/rabbitmq-management/test/src/rabbit_mgmt_test_http.erl new file mode 100644 index 0000000..f56a330 --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-management/test/src/rabbit_mgmt_test_http.erl @@ -0,0 +1,1291 @@ +%% The contents of this file are subject to the Mozilla Public License +%% Version 1.1 (the "License"); you may not use this file except in +%% compliance with the License. You may obtain a copy of the License at +%% http://www.mozilla.org/MPL/ +%% +%% Software distributed under the License is distributed on an "AS IS" +%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the +%% License for the specific language governing rights and limitations +%% under the License. +%% +%% The Original Code is RabbitMQ Management Console. +%% +%% The Initial Developer of the Original Code is GoPivotal, Inc. +%% Copyright (c) 2010-2015 Pivotal Software, Inc. All rights reserved. +%% + +-module(rabbit_mgmt_test_http). + +-include("rabbit_mgmt_test.hrl"). + +-export([http_get/1, http_put/3, http_delete/2]). + +-import(rabbit_mgmt_test_util, [assert_list/2, assert_item/2, test_item/2]). +-import(rabbit_misc, [pget/2]). + +overview_test() -> + %% Rather crude, but this req doesn't say much and at least this means it + %% didn't blow up. + true = 0 < length(pget(listeners, http_get("/overview"))), + http_put("/users/myuser", [{password, <<"myuser">>}, + {tags, <<"management">>}], ?NO_CONTENT), + http_get("/overview", "myuser", "myuser", ?OK), + http_delete("/users/myuser", ?NO_CONTENT), + %% TODO uncomment when priv works in test + %%http_get(""), + ok. + +cluster_name_test() -> + http_put("/users/myuser", [{password, <<"myuser">>}, + {tags, <<"management">>}], ?NO_CONTENT), + http_put("/cluster-name", [{name, "foo"}], "myuser", "myuser", ?NOT_AUTHORISED), + http_put("/cluster-name", [{name, "foo"}], ?NO_CONTENT), + [{name, "foo"}] = http_get("/cluster-name", "myuser", "myuser", ?OK), + http_delete("/users/myuser", ?NO_CONTENT), + ok. + +nodes_test() -> + http_put("/users/user", [{password, <<"user">>}, + {tags, <<"management">>}], ?NO_CONTENT), + http_put("/users/monitor", [{password, <<"monitor">>}, + {tags, <<"monitoring">>}], ?NO_CONTENT), + DiscNode = [{type, <<"disc">>}, {running, true}], + assert_list([DiscNode], http_get("/nodes")), + assert_list([DiscNode], http_get("/nodes", "monitor", "monitor", ?OK)), + http_get("/nodes", "user", "user", ?NOT_AUTHORISED), + [Node] = http_get("/nodes"), + Path = "/nodes/" ++ binary_to_list(pget(name, Node)), + assert_item(DiscNode, http_get(Path, ?OK)), + assert_item(DiscNode, http_get(Path, "monitor", "monitor", ?OK)), + http_get(Path, "user", "user", ?NOT_AUTHORISED), + http_delete("/users/user", ?NO_CONTENT), + http_delete("/users/monitor", ?NO_CONTENT), + ok. + +auth_test() -> + http_put("/users/user", [{password, <<"user">>}, + {tags, <<"">>}], ?NO_CONTENT), + test_auth(?NOT_AUTHORISED, []), + test_auth(?NOT_AUTHORISED, [auth_header("user", "user")]), + test_auth(?NOT_AUTHORISED, [auth_header("guest", "gust")]), + test_auth(?OK, [auth_header("guest", "guest")]), + http_delete("/users/user", ?NO_CONTENT), + ok. + +%% This test is rather over-verbose as we're trying to test understanding of +%% Webmachine +vhosts_test() -> + assert_list([[{name, <<"/">>}]], http_get("/vhosts")), + %% Create a new one + http_put("/vhosts/myvhost", none, ?NO_CONTENT), + %% PUT should be idempotent + http_put("/vhosts/myvhost", none, ?NO_CONTENT), + %% Check it's there + assert_list([[{name, <<"/">>}], [{name, <<"myvhost">>}]], + http_get("/vhosts")), + %% Check individually + assert_item([{name, <<"/">>}], http_get("/vhosts/%2f", ?OK)), + assert_item([{name, <<"myvhost">>}],http_get("/vhosts/myvhost")), + %% Delete it + http_delete("/vhosts/myvhost", ?NO_CONTENT), + %% It's not there + http_get("/vhosts/myvhost", ?NOT_FOUND), + http_delete("/vhosts/myvhost", ?NOT_FOUND). + +vhosts_trace_test() -> + http_put("/vhosts/myvhost", none, ?NO_CONTENT), + Disabled = [{name, <<"myvhost">>}, {tracing, false}], + Enabled = [{name, <<"myvhost">>}, {tracing, true}], + Disabled = http_get("/vhosts/myvhost"), + http_put("/vhosts/myvhost", [{tracing, true}], ?NO_CONTENT), + Enabled = http_get("/vhosts/myvhost"), + http_put("/vhosts/myvhost", [{tracing, true}], ?NO_CONTENT), + Enabled = http_get("/vhosts/myvhost"), + http_put("/vhosts/myvhost", [{tracing, false}], ?NO_CONTENT), + Disabled = http_get("/vhosts/myvhost"), + http_delete("/vhosts/myvhost", ?NO_CONTENT). + +users_test() -> + assert_item([{name, <<"guest">>}, {tags, <<"administrator">>}], + http_get("/whoami")), + http_get("/users/myuser", ?NOT_FOUND), + http_put_raw("/users/myuser", "Something not JSON", ?BAD_REQUEST), + http_put("/users/myuser", [{flim, <<"flam">>}], ?BAD_REQUEST), + http_put("/users/myuser", [{tags, <<"management">>}], ?NO_CONTENT), + http_put("/users/myuser", [{password_hash, <<"not_hash">>}], ?BAD_REQUEST), + http_put("/users/myuser", [{password_hash, + <<"IECV6PZI/Invh0DL187KFpkO5Jc=">>}, + {tags, <<"management">>}], ?NO_CONTENT), + http_put("/users/myuser", [{password, <<"password">>}, + {tags, <<"administrator, foo">>}], ?NO_CONTENT), + assert_item([{name, <<"myuser">>}, {tags, <<"administrator,foo">>}], + http_get("/users/myuser")), + assert_list([[{name, <<"myuser">>}, {tags, <<"administrator,foo">>}], + [{name, <<"guest">>}, {tags, <<"administrator">>}]], + http_get("/users")), + test_auth(?OK, [auth_header("myuser", "password")]), + http_delete("/users/myuser", ?NO_CONTENT), + test_auth(?NOT_AUTHORISED, [auth_header("myuser", "password")]), + http_get("/users/myuser", ?NOT_FOUND), + ok. + +users_legacy_administrator_test() -> + http_put("/users/myuser1", [{administrator, <<"true">>}], ?NO_CONTENT), + http_put("/users/myuser2", [{administrator, <<"false">>}], ?NO_CONTENT), + assert_item([{name, <<"myuser1">>}, {tags, <<"administrator">>}], + http_get("/users/myuser1")), + assert_item([{name, <<"myuser2">>}, {tags, <<"">>}], + http_get("/users/myuser2")), + http_delete("/users/myuser1", ?NO_CONTENT), + http_delete("/users/myuser2", ?NO_CONTENT), + ok. + +permissions_validation_test() -> + Good = [{configure, <<".*">>}, {write, <<".*">>}, {read, <<".*">>}], + http_put("/permissions/wrong/guest", Good, ?BAD_REQUEST), + http_put("/permissions/%2f/wrong", Good, ?BAD_REQUEST), + http_put("/permissions/%2f/guest", + [{configure, <<"[">>}, {write, <<".*">>}, {read, <<".*">>}], + ?BAD_REQUEST), + http_put("/permissions/%2f/guest", Good, ?NO_CONTENT), + ok. + +permissions_list_test() -> + [[{user,<<"guest">>}, + {vhost,<<"/">>}, + {configure,<<".*">>}, + {write,<<".*">>}, + {read,<<".*">>}]] = + http_get("/permissions"), + + http_put("/users/myuser1", [{password, <<"">>}, {tags, <<"administrator">>}], + ?NO_CONTENT), + http_put("/users/myuser2", [{password, <<"">>}, {tags, <<"administrator">>}], + ?NO_CONTENT), + http_put("/vhosts/myvhost1", none, ?NO_CONTENT), + http_put("/vhosts/myvhost2", none, ?NO_CONTENT), + + Perms = [{configure, <<"foo">>}, {write, <<"foo">>}, {read, <<"foo">>}], + http_put("/permissions/myvhost1/myuser1", Perms, ?NO_CONTENT), + http_put("/permissions/myvhost2/myuser1", Perms, ?NO_CONTENT), + http_put("/permissions/myvhost1/myuser2", Perms, ?NO_CONTENT), + + 4 = length(http_get("/permissions")), + 2 = length(http_get("/users/myuser1/permissions")), + 1 = length(http_get("/users/myuser2/permissions")), + + http_get("/users/notmyuser/permissions", ?NOT_FOUND), + http_get("/vhosts/notmyvhost/permissions", ?NOT_FOUND), + + http_delete("/users/myuser1", ?NO_CONTENT), + http_delete("/users/myuser2", ?NO_CONTENT), + http_delete("/vhosts/myvhost1", ?NO_CONTENT), + http_delete("/vhosts/myvhost2", ?NO_CONTENT), + ok. + +permissions_test() -> + http_put("/users/myuser", [{password, <<"myuser">>}, {tags, <<"administrator">>}], + ?NO_CONTENT), + http_put("/vhosts/myvhost", none, ?NO_CONTENT), + + http_put("/permissions/myvhost/myuser", + [{configure, <<"foo">>}, {write, <<"foo">>}, {read, <<"foo">>}], + ?NO_CONTENT), + + Permission = [{user,<<"myuser">>}, + {vhost,<<"myvhost">>}, + {configure,<<"foo">>}, + {write,<<"foo">>}, + {read,<<"foo">>}], + Default = [{user,<<"guest">>}, + {vhost,<<"/">>}, + {configure,<<".*">>}, + {write,<<".*">>}, + {read,<<".*">>}], + Permission = http_get("/permissions/myvhost/myuser"), + assert_list([Permission, Default], http_get("/permissions")), + assert_list([Permission], http_get("/users/myuser/permissions")), + http_delete("/permissions/myvhost/myuser", ?NO_CONTENT), + http_get("/permissions/myvhost/myuser", ?NOT_FOUND), + + http_delete("/users/myuser", ?NO_CONTENT), + http_delete("/vhosts/myvhost", ?NO_CONTENT), + ok. + +connections_test() -> + {ok, Conn} = amqp_connection:start(#amqp_params_network{}), + LocalPort = local_port(Conn), + Path = binary_to_list( + rabbit_mgmt_format:print( + "/connections/127.0.0.1%3A~w%20->%20127.0.0.1%3A5672", + [LocalPort])), + http_get(Path, ?OK), + http_delete(Path, ?NO_CONTENT), + %% TODO rabbit_reader:shutdown/2 returns before the connection is + %% closed. It may not be worth fixing. + timer:sleep(200), + http_get(Path, ?NOT_FOUND). + +test_auth(Code, Headers) -> + {ok, {{_, Code, _}, _, _}} = req(get, "/overview", Headers). + +exchanges_test() -> + %% Can pass booleans or strings + Good = [{type, <<"direct">>}, {durable, <<"true">>}], + http_put("/vhosts/myvhost", none, ?NO_CONTENT), + http_get("/exchanges/myvhost/foo", ?NOT_AUTHORISED), + http_put("/exchanges/myvhost/foo", Good, ?NOT_AUTHORISED), + http_put("/permissions/myvhost/guest", + [{configure, <<".*">>}, {write, <<".*">>}, {read, <<".*">>}], + ?NO_CONTENT), + http_get("/exchanges/myvhost/foo", ?NOT_FOUND), + http_put("/exchanges/myvhost/foo", Good, ?NO_CONTENT), + http_put("/exchanges/myvhost/foo", Good, ?NO_CONTENT), + http_get("/exchanges/%2f/foo", ?NOT_FOUND), + assert_item([{name,<<"foo">>}, + {vhost,<<"myvhost">>}, + {type,<<"direct">>}, + {durable,true}, + {auto_delete,false}, + {internal,false}, + {arguments,[]}], + http_get("/exchanges/myvhost/foo")), + + http_put("/exchanges/badvhost/bar", Good, ?NOT_FOUND), + http_put("/exchanges/myvhost/bar", [{type, <<"bad_exchange_type">>}], + ?BAD_REQUEST), + http_put("/exchanges/myvhost/bar", [{type, <<"direct">>}, + {durable, <<"troo">>}], + ?BAD_REQUEST), + http_put("/exchanges/myvhost/foo", [{type, <<"direct">>}], + ?BAD_REQUEST), + + http_delete("/exchanges/myvhost/foo", ?NO_CONTENT), + http_delete("/exchanges/myvhost/foo", ?NOT_FOUND), + + http_delete("/vhosts/myvhost", ?NO_CONTENT), + http_get("/exchanges/badvhost", ?NOT_FOUND), + ok. + +queues_test() -> + Good = [{durable, true}], + http_get("/queues/%2f/foo", ?NOT_FOUND), + http_put("/queues/%2f/foo", Good, ?NO_CONTENT), + http_put("/queues/%2f/foo", Good, ?NO_CONTENT), + http_get("/queues/%2f/foo", ?OK), + + http_put("/queues/badvhost/bar", Good, ?NOT_FOUND), + http_put("/queues/%2f/bar", + [{durable, <<"troo">>}], + ?BAD_REQUEST), + http_put("/queues/%2f/foo", + [{durable, false}], + ?BAD_REQUEST), + + http_put("/queues/%2f/baz", Good, ?NO_CONTENT), + + Queues = http_get("/queues/%2f"), + Queue = http_get("/queues/%2f/foo"), + assert_list([[{name, <<"foo">>}, + {vhost, <<"/">>}, + {durable, true}, + {auto_delete, false}, + {arguments, []}], + [{name, <<"baz">>}, + {vhost, <<"/">>}, + {durable, true}, + {auto_delete, false}, + {arguments, []}]], Queues), + assert_item([{name, <<"foo">>}, + {vhost, <<"/">>}, + {durable, true}, + {auto_delete, false}, + {arguments, []}], Queue), + + http_delete("/queues/%2f/foo", ?NO_CONTENT), + http_delete("/queues/%2f/baz", ?NO_CONTENT), + http_delete("/queues/%2f/foo", ?NOT_FOUND), + http_get("/queues/badvhost", ?NOT_FOUND), + ok. + +bindings_test() -> + XArgs = [{type, <<"direct">>}], + QArgs = [], + http_put("/exchanges/%2f/myexchange", XArgs, ?NO_CONTENT), + http_put("/queues/%2f/myqueue", QArgs, ?NO_CONTENT), + BArgs = [{routing_key, <<"routing">>}, {arguments, []}], + http_post("/bindings/%2f/e/myexchange/q/myqueue", BArgs, ?CREATED), + http_get("/bindings/%2f/e/myexchange/q/myqueue/routing", ?OK), + http_get("/bindings/%2f/e/myexchange/q/myqueue/rooting", ?NOT_FOUND), + Binding = + [{source,<<"myexchange">>}, + {vhost,<<"/">>}, + {destination,<<"myqueue">>}, + {destination_type,<<"queue">>}, + {routing_key,<<"routing">>}, + {arguments,[]}, + {properties_key,<<"routing">>}], + DBinding = + [{source,<<"">>}, + {vhost,<<"/">>}, + {destination,<<"myqueue">>}, + {destination_type,<<"queue">>}, + {routing_key,<<"myqueue">>}, + {arguments,[]}, + {properties_key,<<"myqueue">>}], + Binding = http_get("/bindings/%2f/e/myexchange/q/myqueue/routing"), + assert_list([Binding], + http_get("/bindings/%2f/e/myexchange/q/myqueue")), + assert_list([Binding, DBinding], + http_get("/queues/%2f/myqueue/bindings")), + assert_list([Binding], + http_get("/exchanges/%2f/myexchange/bindings/source")), + http_delete("/bindings/%2f/e/myexchange/q/myqueue/routing", ?NO_CONTENT), + http_delete("/bindings/%2f/e/myexchange/q/myqueue/routing", ?NOT_FOUND), + http_delete("/exchanges/%2f/myexchange", ?NO_CONTENT), + http_delete("/queues/%2f/myqueue", ?NO_CONTENT), + http_get("/bindings/badvhost", ?NOT_FOUND), + http_get("/bindings/badvhost/myqueue/myexchange/routing", ?NOT_FOUND), + http_get("/bindings/%2f/e/myexchange/q/myqueue/routing", ?NOT_FOUND), + ok. + +bindings_post_test() -> + XArgs = [{type, <<"direct">>}], + QArgs = [], + BArgs = [{routing_key, <<"routing">>}, {arguments, [{foo, <<"bar">>}]}], + http_put("/exchanges/%2f/myexchange", XArgs, ?NO_CONTENT), + http_put("/queues/%2f/myqueue", QArgs, ?NO_CONTENT), + http_post("/bindings/%2f/e/myexchange/q/badqueue", BArgs, ?NOT_FOUND), + http_post("/bindings/%2f/e/badexchange/q/myqueue", BArgs, ?NOT_FOUND), + Headers1 = http_post("/bindings/%2f/e/myexchange/q/myqueue", [], ?CREATED), + "../../../../%2F/e/myexchange/q/myqueue/~" = pget("location", Headers1), + Headers2 = http_post("/bindings/%2f/e/myexchange/q/myqueue", BArgs, ?CREATED), + PropertiesKey = "routing~V4mGFgnPNrdtRmluZIxTDA", + PropertiesKeyBin = list_to_binary(PropertiesKey), + "../../../../%2F/e/myexchange/q/myqueue/" ++ PropertiesKey = + pget("location", Headers2), + URI = "/bindings/%2F/e/myexchange/q/myqueue/" ++ PropertiesKey, + [{source,<<"myexchange">>}, + {vhost,<<"/">>}, + {destination,<<"myqueue">>}, + {destination_type,<<"queue">>}, + {routing_key,<<"routing">>}, + {arguments,[{foo,<<"bar">>}]}, + {properties_key,PropertiesKeyBin}] = http_get(URI, ?OK), + http_get(URI ++ "x", ?NOT_FOUND), + http_delete(URI, ?NO_CONTENT), + http_delete("/exchanges/%2f/myexchange", ?NO_CONTENT), + http_delete("/queues/%2f/myqueue", ?NO_CONTENT), + ok. + +bindings_e2e_test() -> + BArgs = [{routing_key, <<"routing">>}, {arguments, []}], + http_post("/bindings/%2f/e/amq.direct/e/badexchange", BArgs, ?NOT_FOUND), + http_post("/bindings/%2f/e/badexchange/e/amq.fanout", BArgs, ?NOT_FOUND), + Headers = http_post("/bindings/%2f/e/amq.direct/e/amq.fanout", BArgs, ?CREATED), + "../../../../%2F/e/amq.direct/e/amq.fanout/routing" = + pget("location", Headers), + [{source,<<"amq.direct">>}, + {vhost,<<"/">>}, + {destination,<<"amq.fanout">>}, + {destination_type,<<"exchange">>}, + {routing_key,<<"routing">>}, + {arguments,[]}, + {properties_key,<<"routing">>}] = + http_get("/bindings/%2f/e/amq.direct/e/amq.fanout/routing", ?OK), + http_delete("/bindings/%2f/e/amq.direct/e/amq.fanout/routing", ?NO_CONTENT), + http_post("/bindings/%2f/e/amq.direct/e/amq.headers", BArgs, ?CREATED), + Binding = + [{source,<<"amq.direct">>}, + {vhost,<<"/">>}, + {destination,<<"amq.headers">>}, + {destination_type,<<"exchange">>}, + {routing_key,<<"routing">>}, + {arguments,[]}, + {properties_key,<<"routing">>}], + Binding = http_get("/bindings/%2f/e/amq.direct/e/amq.headers/routing"), + assert_list([Binding], + http_get("/bindings/%2f/e/amq.direct/e/amq.headers")), + assert_list([Binding], + http_get("/exchanges/%2f/amq.direct/bindings/source")), + assert_list([Binding], + http_get("/exchanges/%2f/amq.headers/bindings/destination")), + http_delete("/bindings/%2f/e/amq.direct/e/amq.headers/routing", ?NO_CONTENT), + http_get("/bindings/%2f/e/amq.direct/e/amq.headers/rooting", ?NOT_FOUND), + ok. + +permissions_administrator_test() -> + http_put("/users/isadmin", [{password, <<"isadmin">>}, + {tags, <<"administrator">>}], ?NO_CONTENT), + http_put("/users/notadmin", [{password, <<"notadmin">>}, + {tags, <<"administrator">>}], ?NO_CONTENT), + http_put("/users/notadmin", [{password, <<"notadmin">>}, + {tags, <<"management">>}], ?NO_CONTENT), + Test = + fun(Path) -> + http_get(Path, "notadmin", "notadmin", ?NOT_AUTHORISED), + http_get(Path, "isadmin", "isadmin", ?OK), + http_get(Path, "guest", "guest", ?OK) + end, + %% All users can get a list of vhosts. It may be filtered. + %%Test("/vhosts"), + Test("/vhosts/%2f"), + Test("/vhosts/%2f/permissions"), + Test("/users"), + Test("/users/guest"), + Test("/users/guest/permissions"), + Test("/permissions"), + Test("/permissions/%2f/guest"), + http_delete("/users/notadmin", ?NO_CONTENT), + http_delete("/users/isadmin", ?NO_CONTENT), + ok. + +permissions_vhost_test() -> + QArgs = [], + PermArgs = [{configure, <<".*">>}, {write, <<".*">>}, {read, <<".*">>}], + http_put("/users/myuser", [{password, <<"myuser">>}, + {tags, <<"management">>}], ?NO_CONTENT), + http_put("/vhosts/myvhost1", none, ?NO_CONTENT), + http_put("/vhosts/myvhost2", none, ?NO_CONTENT), + http_put("/permissions/myvhost1/myuser", PermArgs, ?NO_CONTENT), + http_put("/permissions/myvhost1/guest", PermArgs, ?NO_CONTENT), + http_put("/permissions/myvhost2/guest", PermArgs, ?NO_CONTENT), + assert_list([[{name, <<"/">>}], + [{name, <<"myvhost1">>}], + [{name, <<"myvhost2">>}]], http_get("/vhosts", ?OK)), + assert_list([[{name, <<"myvhost1">>}]], + http_get("/vhosts", "myuser", "myuser", ?OK)), + http_put("/queues/myvhost1/myqueue", QArgs, ?NO_CONTENT), + http_put("/queues/myvhost2/myqueue", QArgs, ?NO_CONTENT), + Test1 = + fun(Path) -> + Results = http_get(Path, "myuser", "myuser", ?OK), + [case pget(vhost, Result) of + <<"myvhost2">> -> + throw({got_result_from_vhost2_in, Path, Result}); + _ -> + ok + end || Result <- Results] + end, + Test2 = + fun(Path1, Path2) -> + http_get(Path1 ++ "/myvhost1/" ++ Path2, "myuser", "myuser", + ?OK), + http_get(Path1 ++ "/myvhost2/" ++ Path2, "myuser", "myuser", + ?NOT_AUTHORISED) + end, + Test1("/exchanges"), + Test2("/exchanges", ""), + Test2("/exchanges", "amq.direct"), + Test1("/queues"), + Test2("/queues", ""), + Test2("/queues", "myqueue"), + Test1("/bindings"), + Test2("/bindings", ""), + Test2("/queues", "myqueue/bindings"), + Test2("/exchanges", "amq.default/bindings/source"), + Test2("/exchanges", "amq.default/bindings/destination"), + Test2("/bindings", "e/amq.default/q/myqueue"), + Test2("/bindings", "e/amq.default/q/myqueue/myqueue"), + http_delete("/vhosts/myvhost1", ?NO_CONTENT), + http_delete("/vhosts/myvhost2", ?NO_CONTENT), + http_delete("/users/myuser", ?NO_CONTENT), + ok. + +permissions_amqp_test() -> + %% Just test that it works at all, not that it works in all possible cases. + QArgs = [], + PermArgs = [{configure, <<"foo.*">>}, {write, <<"foo.*">>}, + {read, <<"foo.*">>}], + http_put("/users/myuser", [{password, <<"myuser">>}, + {tags, <<"management">>}], ?NO_CONTENT), + http_put("/permissions/%2f/myuser", PermArgs, ?NO_CONTENT), + http_put("/queues/%2f/bar-queue", QArgs, "myuser", "myuser", + ?NOT_AUTHORISED), + http_put("/queues/%2f/bar-queue", QArgs, "nonexistent", "nonexistent", + ?NOT_AUTHORISED), + http_delete("/users/myuser", ?NO_CONTENT), + ok. + +get_conn(Username, Password) -> + {ok, Conn} = amqp_connection:start(#amqp_params_network{ + username = list_to_binary(Username), + password = list_to_binary(Password)}), + LocalPort = local_port(Conn), + ConnPath = rabbit_misc:format( + "/connections/127.0.0.1%3A~w%20->%20127.0.0.1%3A5672", + [LocalPort]), + ChPath = rabbit_misc:format( + "/channels/127.0.0.1%3A~w%20->%20127.0.0.1%3A5672%20(1)", + [LocalPort]), + ConnChPath = rabbit_misc:format( + "/connections/127.0.0.1%3A~w%20->%20127.0.0.1%3A5672/channels", + [LocalPort]), + {Conn, ConnPath, ChPath, ConnChPath}. + +permissions_connection_channel_consumer_test() -> + PermArgs = [{configure, <<".*">>}, {write, <<".*">>}, {read, <<".*">>}], + http_put("/users/user", [{password, <<"user">>}, + {tags, <<"management">>}], ?NO_CONTENT), + http_put("/permissions/%2f/user", PermArgs, ?NO_CONTENT), + http_put("/users/monitor", [{password, <<"monitor">>}, + {tags, <<"monitoring">>}], ?NO_CONTENT), + http_put("/permissions/%2f/monitor", PermArgs, ?NO_CONTENT), + http_put("/queues/%2f/test", [], ?NO_CONTENT), + + {Conn1, UserConn, UserCh, UserConnCh} = get_conn("user", "user"), + {Conn2, MonConn, MonCh, MonConnCh} = get_conn("monitor", "monitor"), + {Conn3, AdmConn, AdmCh, AdmConnCh} = get_conn("guest", "guest"), + {ok, Ch1} = amqp_connection:open_channel(Conn1), + {ok, Ch2} = amqp_connection:open_channel(Conn2), + {ok, Ch3} = amqp_connection:open_channel(Conn3), + [amqp_channel:subscribe( + Ch, #'basic.consume'{queue = <<"test">>}, self()) || + Ch <- [Ch1, Ch2, Ch3]], + AssertLength = fun (Path, User, Len) -> + ?assertEqual(Len, + length(http_get(Path, User, User, ?OK))) + end, + [begin + AssertLength(P, "user", 1), + AssertLength(P, "monitor", 3), + AssertLength(P, "guest", 3) + end || P <- ["/connections", "/channels", "/consumers", "/consumers/%2f"]], + + AssertRead = fun(Path, UserStatus) -> + http_get(Path, "user", "user", UserStatus), + http_get(Path, "monitor", "monitor", ?OK), + http_get(Path, ?OK) + end, + AssertRead(UserConn, ?OK), + AssertRead(MonConn, ?NOT_AUTHORISED), + AssertRead(AdmConn, ?NOT_AUTHORISED), + AssertRead(UserCh, ?OK), + AssertRead(MonCh, ?NOT_AUTHORISED), + AssertRead(AdmCh, ?NOT_AUTHORISED), + AssertRead(UserConnCh, ?OK), + AssertRead(MonConnCh, ?NOT_AUTHORISED), + AssertRead(AdmConnCh, ?NOT_AUTHORISED), + + AssertClose = fun(Path, User, Status) -> + http_delete(Path, User, User, Status) + end, + AssertClose(UserConn, "monitor", ?NOT_AUTHORISED), + AssertClose(MonConn, "user", ?NOT_AUTHORISED), + AssertClose(AdmConn, "guest", ?NO_CONTENT), + AssertClose(MonConn, "guest", ?NO_CONTENT), + AssertClose(UserConn, "user", ?NO_CONTENT), + + http_delete("/users/user", ?NO_CONTENT), + http_delete("/users/monitor", ?NO_CONTENT), + http_get("/connections/foo", ?NOT_FOUND), + http_get("/channels/foo", ?NOT_FOUND), + http_delete("/queues/%2f/test", ?NO_CONTENT), + ok. + +consumers_test() -> + http_put("/queues/%2f/test", [], ?NO_CONTENT), + {Conn, _ConnPath, _ChPath, _ConnChPath} = get_conn("guest", "guest"), + {ok, Ch} = amqp_connection:open_channel(Conn), + amqp_channel:subscribe( + Ch, #'basic.consume'{queue = <<"test">>, + no_ack = false, + consumer_tag = <<"my-ctag">> }, self()), + assert_list([[{exclusive, false}, + {ack_required, true}, + {consumer_tag, <<"my-ctag">>}]], http_get("/consumers")), + amqp_connection:close(Conn), + http_delete("/queues/%2f/test", ?NO_CONTENT), + ok. + +defs(Key, URI, CreateMethod, Args) -> + defs(Key, URI, CreateMethod, Args, + fun(URI2) -> http_delete(URI2, ?NO_CONTENT) end). + +defs_v(Key, URI, CreateMethod, Args) -> + Rep1 = fun (S, S2) -> re:replace(S, "", S2, [{return, list}]) end, + Rep2 = fun (L, V2) -> lists:keymap(fun (vhost) -> V2; + (V) -> V end, 2, L) end, + %% Test against default vhost + defs(Key, Rep1(URI, "%2f"), CreateMethod, Rep2(Args, <<"/">>)), + + %% Test against new vhost + http_put("/vhosts/test", none, ?NO_CONTENT), + PermArgs = [{configure, <<".*">>}, {write, <<".*">>}, {read, <<".*">>}], + http_put("/permissions/test/guest", PermArgs, ?NO_CONTENT), + defs(Key, Rep1(URI, "test"), CreateMethod, Rep2(Args, <<"test">>), + fun(URI2) -> http_delete(URI2, ?NO_CONTENT), + http_delete("/vhosts/test", ?NO_CONTENT) end). + +defs(Key, URI, CreateMethod, Args, DeleteFun) -> + %% Create the item + URI2 = case CreateMethod of + put -> http_put(URI, Args, ?NO_CONTENT), + URI; + post -> Headers = http_post(URI, Args, ?CREATED), + rabbit_web_dispatch_util:unrelativise( + URI, pget("location", Headers)) + end, + %% Make sure it ends up in definitions + Definitions = http_get("/definitions", ?OK), + true = lists:any(fun(I) -> test_item(Args, I) end, pget(Key, Definitions)), + + %% Delete it + DeleteFun(URI2), + + %% Post the definitions back, it should get recreated in correct form + http_post("/definitions", Definitions, ?CREATED), + assert_item(Args, http_get(URI2, ?OK)), + + %% And delete it again + DeleteFun(URI2), + + ok. + +definitions_test() -> + rabbit_runtime_parameters_test:register(), + rabbit_runtime_parameters_test:register_policy_validator(), + + defs_v(queues, "/queues//my-queue", put, + [{name, <<"my-queue">>}, + {durable, true}]), + defs_v(exchanges, "/exchanges//my-exchange", put, + [{name, <<"my-exchange">>}, + {type, <<"direct">>}]), + defs_v(bindings, "/bindings//e/amq.direct/e/amq.fanout", post, + [{routing_key, <<"routing">>}, {arguments, []}]), + defs_v(policies, "/policies//my-policy", put, + [{vhost, vhost}, + {name, <<"my-policy">>}, + {pattern, <<".*">>}, + {definition, [{testpos, [1, 2, 3]}]}, + {priority, 1}]), + defs_v(parameters, "/parameters/test//good", put, + [{vhost, vhost}, + {component, <<"test">>}, + {name, <<"good">>}, + {value, <<"ignore">>}]), + defs(users, "/users/myuser", put, + [{name, <<"myuser">>}, + {password_hash, <<"WAbU0ZIcvjTpxM3Q3SbJhEAM2tQ=">>}, + {tags, <<"management">>}]), + defs(vhosts, "/vhosts/myvhost", put, + [{name, <<"myvhost">>}]), + defs(permissions, "/permissions/%2f/guest", put, + [{user, <<"guest">>}, + {vhost, <<"/">>}, + {configure, <<"c">>}, + {write, <<"w">>}, + {read, <<"r">>}]), + + %% We just messed with guest's permissions + http_put("/permissions/%2f/guest", + [{configure, <<".*">>}, + {write, <<".*">>}, + {read, <<".*">>}], ?NO_CONTENT), + + BrokenConfig = + [{users, []}, + {vhosts, []}, + {permissions, []}, + {queues, []}, + {exchanges, [[{name, <<"amq.direct">>}, + {vhost, <<"/">>}, + {type, <<"definitely not direct">>}, + {durable, true}, + {auto_delete, false}, + {arguments, []} + ]]}, + {bindings, []}], + http_post("/definitions", BrokenConfig, ?BAD_REQUEST), + + rabbit_runtime_parameters_test:unregister_policy_validator(), + rabbit_runtime_parameters_test:unregister(), + ok. + +definitions_remove_things_test() -> + {ok, Conn} = amqp_connection:start(#amqp_params_network{}), + {ok, Ch} = amqp_connection:open_channel(Conn), + amqp_channel:call(Ch, #'queue.declare'{ queue = <<"my-exclusive">>, + exclusive = true }), + http_get("/queues/%2f/my-exclusive", ?OK), + Definitions = http_get("/definitions", ?OK), + [] = pget(queues, Definitions), + [] = pget(exchanges, Definitions), + [] = pget(bindings, Definitions), + amqp_channel:close(Ch), + amqp_connection:close(Conn), + ok. + +definitions_server_named_queue_test() -> + {ok, Conn} = amqp_connection:start(#amqp_params_network{}), + {ok, Ch} = amqp_connection:open_channel(Conn), + #'queue.declare_ok'{ queue = QName } = + amqp_channel:call(Ch, #'queue.declare'{}), + amqp_channel:close(Ch), + amqp_connection:close(Conn), + Path = "/queues/%2f/" ++ mochiweb_util:quote_plus(QName), + http_get(Path, ?OK), + Definitions = http_get("/definitions", ?OK), + http_delete(Path, ?NO_CONTENT), + http_get(Path, ?NOT_FOUND), + http_post("/definitions", Definitions, ?CREATED), + http_get(Path, ?OK), + http_delete(Path, ?NO_CONTENT), + ok. + +aliveness_test() -> + [{status, <<"ok">>}] = http_get("/aliveness-test/%2f", ?OK), + http_get("/aliveness-test/foo", ?NOT_FOUND), + http_delete("/queues/%2f/aliveness-test", ?NO_CONTENT), + ok. + +arguments_test() -> + XArgs = [{type, <<"headers">>}, + {arguments, [{'alternate-exchange', <<"amq.direct">>}]}], + QArgs = [{arguments, [{'x-expires', 1800000}]}], + BArgs = [{routing_key, <<"">>}, + {arguments, [{'x-match', <<"all">>}, + {foo, <<"bar">>}]}], + http_put("/exchanges/%2f/myexchange", XArgs, ?NO_CONTENT), + http_put("/queues/%2f/myqueue", QArgs, ?NO_CONTENT), + http_post("/bindings/%2f/e/myexchange/q/myqueue", BArgs, ?CREATED), + Definitions = http_get("/definitions", ?OK), + http_delete("/exchanges/%2f/myexchange", ?NO_CONTENT), + http_delete("/queues/%2f/myqueue", ?NO_CONTENT), + http_post("/definitions", Definitions, ?CREATED), + [{'alternate-exchange', <<"amq.direct">>}] = + pget(arguments, http_get("/exchanges/%2f/myexchange", ?OK)), + [{'x-expires', 1800000}] = + pget(arguments, http_get("/queues/%2f/myqueue", ?OK)), + true = lists:sort([{'x-match', <<"all">>}, {foo, <<"bar">>}]) =:= + lists:sort(pget(arguments, + http_get("/bindings/%2f/e/myexchange/q/myqueue/" ++ + "~nXOkVwqZzUOdS9_HcBWheg", ?OK))), + http_delete("/exchanges/%2f/myexchange", ?NO_CONTENT), + http_delete("/queues/%2f/myqueue", ?NO_CONTENT), + ok. + +arguments_table_test() -> + Args = [{'upstreams', [<<"amqp://localhost/%2f/upstream1">>, + <<"amqp://localhost/%2f/upstream2">>]}], + XArgs = [{type, <<"headers">>}, + {arguments, Args}], + http_put("/exchanges/%2f/myexchange", XArgs, ?NO_CONTENT), + Definitions = http_get("/definitions", ?OK), + http_delete("/exchanges/%2f/myexchange", ?NO_CONTENT), + http_post("/definitions", Definitions, ?CREATED), + Args = pget(arguments, http_get("/exchanges/%2f/myexchange", ?OK)), + http_delete("/exchanges/%2f/myexchange", ?NO_CONTENT), + ok. + +queue_purge_test() -> + QArgs = [], + http_put("/queues/%2f/myqueue", QArgs, ?NO_CONTENT), + {ok, Conn} = amqp_connection:start(#amqp_params_network{}), + {ok, Ch} = amqp_connection:open_channel(Conn), + Publish = fun() -> + amqp_channel:call( + Ch, #'basic.publish'{exchange = <<"">>, + routing_key = <<"myqueue">>}, + #amqp_msg{payload = <<"message">>}) + end, + Publish(), + Publish(), + amqp_channel:call( + Ch, #'queue.declare'{queue = <<"exclusive">>, exclusive = true}), + {#'basic.get_ok'{}, _} = + amqp_channel:call(Ch, #'basic.get'{queue = <<"myqueue">>}), + http_delete("/queues/%2f/myqueue/contents", ?NO_CONTENT), + http_delete("/queues/%2f/badqueue/contents", ?NOT_FOUND), + http_delete("/queues/%2f/exclusive/contents", ?BAD_REQUEST), + http_delete("/queues/%2f/exclusive", ?BAD_REQUEST), + #'basic.get_empty'{} = + amqp_channel:call(Ch, #'basic.get'{queue = <<"myqueue">>}), + amqp_channel:close(Ch), + amqp_connection:close(Conn), + http_delete("/queues/%2f/myqueue", ?NO_CONTENT), + ok. + +queue_actions_test() -> + http_put("/queues/%2f/q", [], ?NO_CONTENT), + http_post("/queues/%2f/q/actions", [{action, sync}], ?NO_CONTENT), + http_post("/queues/%2f/q/actions", [{action, cancel_sync}], ?NO_CONTENT), + http_post("/queues/%2f/q/actions", [{action, change_colour}], ?BAD_REQUEST), + http_delete("/queues/%2f/q", ?NO_CONTENT), + ok. + +exclusive_consumer_test() -> + {ok, Conn} = amqp_connection:start(#amqp_params_network{}), + {ok, Ch} = amqp_connection:open_channel(Conn), + #'queue.declare_ok'{ queue = QName } = + amqp_channel:call(Ch, #'queue.declare'{exclusive = true}), + amqp_channel:subscribe(Ch, #'basic.consume'{queue = QName, + exclusive = true}, self()), + timer:sleep(1000), %% Sadly we need to sleep to let the stats update + http_get("/queues/%2f/"), %% Just check we don't blow up + amqp_channel:close(Ch), + amqp_connection:close(Conn), + ok. + +sorting_test() -> + QArgs = [], + PermArgs = [{configure, <<".*">>}, {write, <<".*">>}, {read, <<".*">>}], + http_put("/vhosts/vh1", none, ?NO_CONTENT), + http_put("/permissions/vh1/guest", PermArgs, ?NO_CONTENT), + http_put("/queues/%2f/test0", QArgs, ?NO_CONTENT), + http_put("/queues/vh1/test1", QArgs, ?NO_CONTENT), + http_put("/queues/%2f/test2", QArgs, ?NO_CONTENT), + http_put("/queues/vh1/test3", QArgs, ?NO_CONTENT), + assert_list([[{name, <<"test0">>}], + [{name, <<"test2">>}], + [{name, <<"test1">>}], + [{name, <<"test3">>}]], http_get("/queues", ?OK)), + assert_list([[{name, <<"test0">>}], + [{name, <<"test1">>}], + [{name, <<"test2">>}], + [{name, <<"test3">>}]], http_get("/queues?sort=name", ?OK)), + assert_list([[{name, <<"test0">>}], + [{name, <<"test2">>}], + [{name, <<"test1">>}], + [{name, <<"test3">>}]], http_get("/queues?sort=vhost", ?OK)), + assert_list([[{name, <<"test3">>}], + [{name, <<"test1">>}], + [{name, <<"test2">>}], + [{name, <<"test0">>}]], http_get("/queues?sort_reverse=true", ?OK)), + assert_list([[{name, <<"test3">>}], + [{name, <<"test2">>}], + [{name, <<"test1">>}], + [{name, <<"test0">>}]], http_get("/queues?sort=name&sort_reverse=true", ?OK)), + assert_list([[{name, <<"test3">>}], + [{name, <<"test1">>}], + [{name, <<"test2">>}], + [{name, <<"test0">>}]], http_get("/queues?sort=vhost&sort_reverse=true", ?OK)), + %% Rather poor but at least test it doesn't blow up with dots + http_get("/queues?sort=owner_pid_details.name", ?OK), + http_delete("/queues/%2f/test0", ?NO_CONTENT), + http_delete("/queues/vh1/test1", ?NO_CONTENT), + http_delete("/queues/%2f/test2", ?NO_CONTENT), + http_delete("/queues/vh1/test3", ?NO_CONTENT), + http_delete("/vhosts/vh1", ?NO_CONTENT), + ok. + +columns_test() -> + http_put("/queues/%2f/test", [{arguments, [{<<"foo">>, <<"bar">>}]}], + ?NO_CONTENT), + [[{name, <<"test">>}, {arguments, [{foo, <<"bar">>}]}]] = + http_get("/queues?columns=arguments.foo,name", ?OK), + [{name, <<"test">>}, {arguments, [{foo, <<"bar">>}]}] = + http_get("/queues/%2f/test?columns=arguments.foo,name", ?OK), + http_delete("/queues/%2f/test", ?NO_CONTENT), + ok. + +get_test() -> + %% Real world example... + Headers = [{<<"x-forwarding">>, array, + [{table, + [{<<"uri">>, longstr, + <<"amqp://localhost/%2f/upstream">>}]}]}], + http_put("/queues/%2f/myqueue", [], ?NO_CONTENT), + {ok, Conn} = amqp_connection:start(#amqp_params_network{}), + {ok, Ch} = amqp_connection:open_channel(Conn), + Publish = fun (Payload) -> + amqp_channel:cast( + Ch, #'basic.publish'{exchange = <<>>, + routing_key = <<"myqueue">>}, + #amqp_msg{props = #'P_basic'{headers = Headers}, + payload = Payload}) + end, + Publish(<<"1aaa">>), + Publish(<<"2aaa">>), + Publish(<<"3aaa">>), + amqp_connection:close(Conn), + [Msg] = http_post("/queues/%2f/myqueue/get", [{requeue, false}, + {count, 1}, + {encoding, auto}, + {truncate, 1}], ?OK), + false = pget(redelivered, Msg), + <<>> = pget(exchange, Msg), + <<"myqueue">> = pget(routing_key, Msg), + <<"1">> = pget(payload, Msg), + [{'x-forwarding', + [[{uri,<<"amqp://localhost/%2f/upstream">>}]]}] = + pget(headers, pget(properties, Msg)), + + [M2, M3] = http_post("/queues/%2f/myqueue/get", [{requeue, true}, + {count, 5}, + {encoding, auto}], ?OK), + <<"2aaa">> = pget(payload, M2), + <<"3aaa">> = pget(payload, M3), + 2 = length(http_post("/queues/%2f/myqueue/get", [{requeue, false}, + {count, 5}, + {encoding, auto}], ?OK)), + [] = http_post("/queues/%2f/myqueue/get", [{requeue, false}, + {count, 5}, + {encoding, auto}], ?OK), + http_delete("/queues/%2f/myqueue", ?NO_CONTENT), + ok. + +get_fail_test() -> + http_put("/users/myuser", [{password, <<"password">>}, + {tags, <<"management">>}], ?NO_CONTENT), + http_put("/queues/%2f/myqueue", [], ?NO_CONTENT), + http_post("/queues/%2f/myqueue/get", + [{requeue, false}, + {count, 1}, + {encoding, auto}], "myuser", "password", ?NOT_AUTHORISED), + http_delete("/queues/%2f/myqueue", ?NO_CONTENT), + http_delete("/users/myuser", ?NO_CONTENT), + ok. + +publish_test() -> + Headers = [{'x-forwarding', [[{uri,<<"amqp://localhost/%2f/upstream">>}]]}], + Msg = msg(<<"myqueue">>, Headers, <<"Hello world">>), + http_put("/queues/%2f/myqueue", [], ?NO_CONTENT), + ?assertEqual([{routed, true}], + http_post("/exchanges/%2f/amq.default/publish", Msg, ?OK)), + [Msg2] = http_post("/queues/%2f/myqueue/get", [{requeue, false}, + {count, 1}, + {encoding, auto}], ?OK), + assert_item(Msg, Msg2), + http_post("/exchanges/%2f/amq.default/publish", Msg2, ?OK), + [Msg3] = http_post("/queues/%2f/myqueue/get", [{requeue, false}, + {count, 1}, + {encoding, auto}], ?OK), + assert_item(Msg, Msg3), + http_delete("/queues/%2f/myqueue", ?NO_CONTENT), + ok. + +publish_fail_test() -> + Msg = msg(<<"myqueue">>, [], <<"Hello world">>), + http_put("/queues/%2f/myqueue", [], ?NO_CONTENT), + http_put("/users/myuser", [{password, <<"password">>}, + {tags, <<"management">>}], ?NO_CONTENT), + http_post("/exchanges/%2f/amq.default/publish", Msg, "myuser", "password", + ?NOT_AUTHORISED), + Msg2 = [{exchange, <<"">>}, + {routing_key, <<"myqueue">>}, + {properties, [{user_id, <<"foo">>}]}, + {payload, <<"Hello world">>}, + {payload_encoding, <<"string">>}], + http_post("/exchanges/%2f/amq.default/publish", Msg2, ?BAD_REQUEST), + Msg3 = [{exchange, <<"">>}, + {routing_key, <<"myqueue">>}, + {properties, []}, + {payload, [<<"not a string">>]}, + {payload_encoding, <<"string">>}], + http_post("/exchanges/%2f/amq.default/publish", Msg3, ?BAD_REQUEST), + MsgTemplate = [{exchange, <<"">>}, + {routing_key, <<"myqueue">>}, + {payload, <<"Hello world">>}, + {payload_encoding, <<"string">>}], + [http_post("/exchanges/%2f/amq.default/publish", + [{properties, [BadProp]} | MsgTemplate], ?BAD_REQUEST) + || BadProp <- [{priority, <<"really high">>}, + {timestamp, <<"recently">>}, + {expiration, 1234}]], + http_delete("/users/myuser", ?NO_CONTENT), + ok. + +publish_base64_test() -> + Msg = msg(<<"myqueue">>, [], <<"YWJjZA==">>, <<"base64">>), + BadMsg1 = msg(<<"myqueue">>, [], <<"flibble">>, <<"base64">>), + BadMsg2 = msg(<<"myqueue">>, [], <<"YWJjZA==">>, <<"base99">>), + http_put("/queues/%2f/myqueue", [], ?NO_CONTENT), + http_post("/exchanges/%2f/amq.default/publish", Msg, ?OK), + http_post("/exchanges/%2f/amq.default/publish", BadMsg1, ?BAD_REQUEST), + http_post("/exchanges/%2f/amq.default/publish", BadMsg2, ?BAD_REQUEST), + [Msg2] = http_post("/queues/%2f/myqueue/get", [{requeue, false}, + {count, 1}, + {encoding, auto}], ?OK), + ?assertEqual(<<"abcd">>, pget(payload, Msg2)), + http_delete("/queues/%2f/myqueue", ?NO_CONTENT), + ok. + +publish_unrouted_test() -> + Msg = msg(<<"hmmm">>, [], <<"Hello world">>), + ?assertEqual([{routed, false}], + http_post("/exchanges/%2f/amq.default/publish", Msg, ?OK)). + +if_empty_unused_test() -> + http_put("/exchanges/%2f/test", [], ?NO_CONTENT), + http_put("/queues/%2f/test", [], ?NO_CONTENT), + http_post("/bindings/%2f/e/test/q/test", [], ?CREATED), + http_post("/exchanges/%2f/amq.default/publish", + msg(<<"test">>, [], <<"Hello world">>), ?OK), + http_delete("/queues/%2f/test?if-empty=true", ?BAD_REQUEST), + http_delete("/exchanges/%2f/test?if-unused=true", ?BAD_REQUEST), + http_delete("/queues/%2f/test/contents", ?NO_CONTENT), + + {Conn, _ConnPath, _ChPath, _ConnChPath} = get_conn("guest", "guest"), + {ok, Ch} = amqp_connection:open_channel(Conn), + amqp_channel:subscribe(Ch, #'basic.consume'{queue = <<"test">> }, self()), + http_delete("/queues/%2f/test?if-unused=true", ?BAD_REQUEST), + amqp_connection:close(Conn), + + http_delete("/queues/%2f/test?if-empty=true", ?NO_CONTENT), + http_delete("/exchanges/%2f/test?if-unused=true", ?NO_CONTENT), + passed. + +parameters_test() -> + rabbit_runtime_parameters_test:register(), + + http_put("/parameters/test/%2f/good", [{value, <<"ignore">>}], ?NO_CONTENT), + http_put("/parameters/test/%2f/maybe", [{value, <<"good">>}], ?NO_CONTENT), + http_put("/parameters/test/%2f/maybe", [{value, <<"bad">>}], ?BAD_REQUEST), + http_put("/parameters/test/%2f/bad", [{value, <<"good">>}], ?BAD_REQUEST), + http_put("/parameters/test/um/good", [{value, <<"ignore">>}], ?NOT_FOUND), + + Good = [{vhost, <<"/">>}, + {component, <<"test">>}, + {name, <<"good">>}, + {value, <<"ignore">>}], + Maybe = [{vhost, <<"/">>}, + {component, <<"test">>}, + {name, <<"maybe">>}, + {value, <<"good">>}], + List = [Good, Maybe], + + assert_list(List, http_get("/parameters")), + assert_list(List, http_get("/parameters/test")), + assert_list(List, http_get("/parameters/test/%2f")), + assert_list([], http_get("/parameters/oops")), + http_get("/parameters/test/oops", ?NOT_FOUND), + + assert_item(Good, http_get("/parameters/test/%2f/good", ?OK)), + assert_item(Maybe, http_get("/parameters/test/%2f/maybe", ?OK)), + + http_delete("/parameters/test/%2f/good", ?NO_CONTENT), + http_delete("/parameters/test/%2f/maybe", ?NO_CONTENT), + http_delete("/parameters/test/%2f/bad", ?NOT_FOUND), + + 0 = length(http_get("/parameters")), + 0 = length(http_get("/parameters/test")), + 0 = length(http_get("/parameters/test/%2f")), + rabbit_runtime_parameters_test:unregister(), + ok. + +policy_test() -> + rabbit_runtime_parameters_test:register_policy_validator(), + PolicyPos = [{vhost, <<"/">>}, + {name, <<"policy_pos">>}, + {pattern, <<".*">>}, + {definition, [{testpos,[1,2,3]}]}, + {priority, 10}], + PolicyEven = [{vhost, <<"/">>}, + {name, <<"policy_even">>}, + {pattern, <<".*">>}, + {definition, [{testeven,[1,2,3,4]}]}, + {priority, 10}], + http_put( + "/policies/%2f/policy_pos", + lists:keydelete(key, 1, PolicyPos), + ?NO_CONTENT), + http_put( + "/policies/%2f/policy_even", + lists:keydelete(key, 1, PolicyEven), + ?NO_CONTENT), + assert_item(PolicyPos, http_get("/policies/%2f/policy_pos", ?OK)), + assert_item(PolicyEven, http_get("/policies/%2f/policy_even", ?OK)), + List = [PolicyPos, PolicyEven], + assert_list(List, http_get("/policies", ?OK)), + assert_list(List, http_get("/policies/%2f", ?OK)), + + http_delete("/policies/%2f/policy_pos", ?NO_CONTENT), + http_delete("/policies/%2f/policy_even", ?NO_CONTENT), + 0 = length(http_get("/policies")), + 0 = length(http_get("/policies/%2f")), + rabbit_runtime_parameters_test:unregister_policy_validator(), + ok. + +policy_permissions_test() -> + rabbit_runtime_parameters_test:register(), + + http_put("/users/admin", [{password, <<"admin">>}, + {tags, <<"administrator">>}], ?NO_CONTENT), + http_put("/users/mon", [{password, <<"monitor">>}, + {tags, <<"monitoring">>}], ?NO_CONTENT), + http_put("/users/policy", [{password, <<"policy">>}, + {tags, <<"policymaker">>}], ?NO_CONTENT), + http_put("/users/mgmt", [{password, <<"mgmt">>}, + {tags, <<"management">>}], ?NO_CONTENT), + Perms = [{configure, <<".*">>}, + {write, <<".*">>}, + {read, <<".*">>}], + http_put("/vhosts/v", none, ?NO_CONTENT), + http_put("/permissions/v/admin", Perms, ?NO_CONTENT), + http_put("/permissions/v/mon", Perms, ?NO_CONTENT), + http_put("/permissions/v/policy", Perms, ?NO_CONTENT), + http_put("/permissions/v/mgmt", Perms, ?NO_CONTENT), + + Policy = [{pattern, <<".*">>}, + {definition, [{<<"ha-mode">>, <<"all">>}]}], + Param = [{value, <<"">>}], + + http_put("/policies/%2f/HA", Policy, ?NO_CONTENT), + http_put("/parameters/test/%2f/good", Param, ?NO_CONTENT), + + Pos = fun (U) -> + http_put("/policies/v/HA", Policy, U, U, ?NO_CONTENT), + http_put( + "/parameters/test/v/good", Param, U, U, ?NO_CONTENT), + 1 = length(http_get("/policies", U, U, ?OK)), + 1 = length(http_get("/parameters/test", U, U, ?OK)), + 1 = length(http_get("/parameters", U, U, ?OK)), + 1 = length(http_get("/policies/v", U, U, ?OK)), + 1 = length(http_get("/parameters/test/v", U, U, ?OK)), + http_get("/policies/v/HA", U, U, ?OK), + http_get("/parameters/test/v/good", U, U, ?OK) + end, + Neg = fun (U) -> + http_put("/policies/v/HA", Policy, U, U, ?NOT_AUTHORISED), + http_put( + "/parameters/test/v/good", Param, U, U, ?NOT_AUTHORISED), + http_put( + "/parameters/test/v/admin", Param, U, U, ?NOT_AUTHORISED), + http_get("/policies", U, U, ?NOT_AUTHORISED), + http_get("/policies/v", U, U, ?NOT_AUTHORISED), + http_get("/parameters", U, U, ?NOT_AUTHORISED), + http_get("/parameters/test", U, U, ?NOT_AUTHORISED), + http_get("/parameters/test/v", U, U, ?NOT_AUTHORISED), + http_get("/policies/v/HA", U, U, ?NOT_AUTHORISED), + http_get("/parameters/test/v/good", U, U, ?NOT_AUTHORISED) + end, + AlwaysNeg = + fun (U) -> + http_put("/policies/%2f/HA", Policy, U, U, ?NOT_AUTHORISED), + http_put( + "/parameters/test/%2f/good", Param, U, U, ?NOT_AUTHORISED), + http_get("/policies/%2f/HA", U, U, ?NOT_AUTHORISED), + http_get("/parameters/test/%2f/good", U, U, ?NOT_AUTHORISED) + end, + + [Neg(U) || U <- ["mon", "mgmt"]], + [Pos(U) || U <- ["admin", "policy"]], + [AlwaysNeg(U) || U <- ["mon", "mgmt", "admin", "policy"]], + + %% This one is deliberately different between admin and policymaker. + http_put("/parameters/test/v/admin", Param, "admin", "admin", ?NO_CONTENT), + http_put("/parameters/test/v/admin", Param, "policy", "policy", + ?BAD_REQUEST), + + http_delete("/vhosts/v", ?NO_CONTENT), + http_delete("/users/admin", ?NO_CONTENT), + http_delete("/users/mon", ?NO_CONTENT), + http_delete("/users/policy", ?NO_CONTENT), + http_delete("/users/mgmt", ?NO_CONTENT), + http_delete("/policies/%2f/HA", ?NO_CONTENT), + + rabbit_runtime_parameters_test:unregister(), + ok. + + +extensions_test() -> + [[{javascript,<<"dispatcher.js">>}]] = http_get("/extensions", ?OK), + ok. + +%%--------------------------------------------------------------------------- + +msg(Key, Headers, Body) -> + msg(Key, Headers, Body, <<"string">>). + +msg(Key, Headers, Body, Enc) -> + [{exchange, <<"">>}, + {routing_key, Key}, + {properties, [{delivery_mode, 2}, + {headers, Headers}]}, + {payload, Body}, + {payload_encoding, Enc}]. + +local_port(Conn) -> + [{sock, Sock}] = amqp_connection:info(Conn, [sock]), + {ok, Port} = inet:port(Sock), + Port. + +%%--------------------------------------------------------------------------- +http_get(Path) -> + http_get(Path, ?OK). + +http_get(Path, CodeExp) -> + http_get(Path, "guest", "guest", CodeExp). + +http_get(Path, User, Pass, CodeExp) -> + {ok, {{_HTTP, CodeAct, _}, Headers, ResBody}} = + req(get, Path, [auth_header(User, Pass)]), + assert_code(CodeExp, CodeAct, "GET", Path, ResBody), + decode(CodeExp, Headers, ResBody). + +http_put(Path, List, CodeExp) -> + http_put_raw(Path, format_for_upload(List), CodeExp). + +http_put(Path, List, User, Pass, CodeExp) -> + http_put_raw(Path, format_for_upload(List), User, Pass, CodeExp). + +http_post(Path, List, CodeExp) -> + http_post_raw(Path, format_for_upload(List), CodeExp). + +http_post(Path, List, User, Pass, CodeExp) -> + http_post_raw(Path, format_for_upload(List), User, Pass, CodeExp). + +format_for_upload(none) -> + <<"">>; +format_for_upload(List) -> + iolist_to_binary(mochijson2:encode({struct, List})). + +http_put_raw(Path, Body, CodeExp) -> + http_upload_raw(put, Path, Body, "guest", "guest", CodeExp). + +http_put_raw(Path, Body, User, Pass, CodeExp) -> + http_upload_raw(put, Path, Body, User, Pass, CodeExp). + +http_post_raw(Path, Body, CodeExp) -> + http_upload_raw(post, Path, Body, "guest", "guest", CodeExp). + +http_post_raw(Path, Body, User, Pass, CodeExp) -> + http_upload_raw(post, Path, Body, User, Pass, CodeExp). + +http_upload_raw(Type, Path, Body, User, Pass, CodeExp) -> + {ok, {{_HTTP, CodeAct, _}, Headers, ResBody}} = + req(Type, Path, [auth_header(User, Pass)], Body), + assert_code(CodeExp, CodeAct, Type, Path, ResBody), + decode(CodeExp, Headers, ResBody). + +http_delete(Path, CodeExp) -> + http_delete(Path, "guest", "guest", CodeExp). + +http_delete(Path, User, Pass, CodeExp) -> + {ok, {{_HTTP, CodeAct, _}, Headers, ResBody}} = + req(delete, Path, [auth_header(User, Pass)]), + assert_code(CodeExp, CodeAct, "DELETE", Path, ResBody), + decode(CodeExp, Headers, ResBody). + +assert_code(CodeExp, CodeAct, Type, Path, Body) -> + case CodeExp of + CodeAct -> ok; + _ -> throw({expected, CodeExp, got, CodeAct, type, Type, + path, Path, body, Body}) + end. + +req(Type, Path, Headers) -> + httpc:request(Type, {?PREFIX ++ Path, Headers}, ?HTTPC_OPTS, []). + +req(Type, Path, Headers, Body) -> + httpc:request(Type, {?PREFIX ++ Path, Headers, "application/json", Body}, + ?HTTPC_OPTS, []). + +decode(?OK, _Headers, ResBody) -> cleanup(mochijson2:decode(ResBody)); +decode(_, Headers, _ResBody) -> Headers. + +cleanup(L) when is_list(L) -> + [cleanup(I) || I <- L]; +cleanup({struct, I}) -> + cleanup(I); +cleanup({K, V}) when is_binary(K) -> + {list_to_atom(binary_to_list(K)), cleanup(V)}; +cleanup(I) -> + I. + +auth_header(Username, Password) -> + {"Authorization", + "Basic " ++ binary_to_list(base64:encode(Username ++ ":" ++ Password))}. + diff --git a/rabbitmq-server/plugins-src/rabbitmq-management/test/src/rabbit_mgmt_test_unit.erl b/rabbitmq-server/plugins-src/rabbitmq-management/test/src/rabbit_mgmt_test_unit.erl new file mode 100644 index 0000000..de71872 --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-management/test/src/rabbit_mgmt_test_unit.erl @@ -0,0 +1,63 @@ +%% The contents of this file are subject to the Mozilla Public License +%% Version 1.1 (the "License"); you may not use this file except in +%% compliance with the License. You may obtain a copy of the License at +%% http://www.mozilla.org/MPL/ +%% +%% Software distributed under the License is distributed on an "AS IS" +%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the +%% License for the specific language governing rights and limitations +%% under the License. +%% +%% The Original Code is RabbitMQ Management Console. +%% +%% The Initial Developer of the Original Code is GoPivotal, Inc. +%% Copyright (c) 2010-2015 Pivotal Software, Inc. All rights reserved. +%% + +-module(rabbit_mgmt_test_unit). + +-include_lib("eunit/include/eunit.hrl"). + +tokenise_test() -> + [] = rabbit_mgmt_format:tokenise(""), + ["foo"] = rabbit_mgmt_format:tokenise("foo"), + ["foo", "bar"] = rabbit_mgmt_format:tokenise("foo~bar"), + ["foo", "", "bar"] = rabbit_mgmt_format:tokenise("foo~~bar"), + ok. + +pack_binding_test() -> + assert_binding(<<"~">>, + <<"">>, []), + assert_binding(<<"foo">>, + <<"foo">>, []), + assert_binding(<<"foo%7Ebar%2Fbash">>, + <<"foo~bar/bash">>, []), + assert_binding(<<"foo%7Ebar%7Ebash">>, + <<"foo~bar~bash">>, []), + ok. + +amqp_table_test() -> + assert_table({struct, []}, []), + assert_table({struct, [{<<"x-expires">>, 1000}]}, + [{<<"x-expires">>, long, 1000}]), + assert_table({struct, + [{<<"x-forwarding">>, + [{struct, + [{<<"uri">>, <<"amqp://localhost/%2f/upstream">>}]}]}]}, + [{<<"x-forwarding">>, array, + [{table, [{<<"uri">>, longstr, + <<"amqp://localhost/%2f/upstream">>}]}]}]). + +assert_table(JSON, AMQP) -> + ?assertEqual(JSON, rabbit_mgmt_format:amqp_table(AMQP)), + ?assertEqual(AMQP, rabbit_mgmt_format:to_amqp_table(JSON)). + +%%-------------------------------------------------------------------- + +assert_binding(Packed, Routing, Args) -> + case rabbit_mgmt_format:pack_binding_props(Routing, Args) of + Packed -> + ok; + Act -> + throw({pack, Routing, Args, expected, Packed, got, Act}) + end. diff --git a/rabbitmq-server/plugins-src/rabbitmq-management/test/src/rabbit_mgmt_test_util.erl b/rabbitmq-server/plugins-src/rabbitmq-management/test/src/rabbit_mgmt_test_util.erl new file mode 100644 index 0000000..1e53d89 --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-management/test/src/rabbit_mgmt_test_util.erl @@ -0,0 +1,45 @@ +%% The contents of this file are subject to the Mozilla Public License +%% Version 1.1 (the "License"); you may not use this file except in +%% compliance with the License. You may obtain a copy of the License at +%% http://www.mozilla.org/MPL/ +%% +%% Software distributed under the License is distributed on an "AS IS" +%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the +%% License for the specific language governing rights and limitations +%% under the License. +%% +%% The Original Code is RabbitMQ Management Console. +%% +%% The Initial Developer of the Original Code is GoPivotal, Inc. +%% Copyright (c) 2010-2012 GoPivotal, Inc. All rights reserved. +%% + +-module(rabbit_mgmt_test_util). + +-export([assert_list/2, assert_item/2, test_item/2]). + +assert_list(Exp, Act) -> + case length(Exp) == length(Act) of + true -> ok; + false -> throw({expected, Exp, actual, Act}) + end, + [case length(lists:filter(fun(ActI) -> test_item(ExpI, ActI) end, Act)) of + 1 -> ok; + N -> throw({found, N, ExpI, in, Act}) + end || ExpI <- Exp]. + +assert_item(Exp, Act) -> + case test_item0(Exp, Act) of + [] -> ok; + Or -> throw(Or) + end. + +test_item(Exp, Act) -> + case test_item0(Exp, Act) of + [] -> true; + _ -> false + end. + +test_item0(Exp, Act) -> + [{did_not_find, ExpI, in, Act} || ExpI <- Exp, + not lists:member(ExpI, Act)]. diff --git a/rabbitmq-server/plugins-src/rabbitmq-management/test/src/rabbitmqadmin-test-wrapper.sh b/rabbitmq-server/plugins-src/rabbitmq-management/test/src/rabbitmqadmin-test-wrapper.sh new file mode 100755 index 0000000..d684ec9 --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-management/test/src/rabbitmqadmin-test-wrapper.sh @@ -0,0 +1,27 @@ +#!/bin/sh -e +TWO=$(python2 -c 'import sys;print(sys.version_info[0])') +THREE=$(python3 -c 'import sys;print(sys.version_info[0])') + +if [ $TWO != 2 ] ; then + echo Python 2 not found! + exit 1 +fi + +if [ $THREE != 3 ] ; then + echo Python 3 not found! + exit 1 +fi + +echo +echo ---------------------- +echo Testing under Python 2 +echo ---------------------- + +python2 $(dirname $0)/rabbitmqadmin-test.py + +echo +echo ---------------------- +echo Testing under Python 3 +echo ---------------------- + +python3 $(dirname $0)/rabbitmqadmin-test.py diff --git a/rabbitmq-server/plugins-src/rabbitmq-management/test/src/rabbitmqadmin-test.py b/rabbitmq-server/plugins-src/rabbitmq-management/test/src/rabbitmqadmin-test.py new file mode 100755 index 0000000..47af73b --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-management/test/src/rabbitmqadmin-test.py @@ -0,0 +1,256 @@ +#!/usr/bin/env python + +import unittest +import os +import os.path +import socket +import subprocess +import sys +import shutil + +# TODO test: SSL, depth, config file, encodings(?), completion(???) + +class TestRabbitMQAdmin(unittest.TestCase): + def test_no_args(self): + self.run_fail([]) + + def test_help(self): + self.run_success(['--help']) + self.run_success(['help', 'subcommands']) + self.run_success(['help', 'config']) + self.run_fail(['help', 'astronomy']) + + def test_host(self): + self.run_success(['show', 'overview']) + self.run_success(['--host', 'localhost', 'show', 'overview']) + self.run_fail(['--host', 'some-host-that-does-not-exist', 'show', 'overview']) + + def test_port(self): + # Test port selection + self.run_success(['--port', '15672', 'show', 'overview']) + # Test port not open + self.run_fail(['--port', '15673', 'show', 'overview']) + # Test port open but not talking HTTP + self.run_fail(['--port', '5672', 'show', 'overview']) + + def test_config(self): + original_home = os.getenv('HOME') + tmpdir = os.getenv("TMPDIR") or os.getenv("TEMP") or "/tmp" + shutil.copyfile(os.path.dirname(__file__) + os.sep + "default-config", + tmpdir + os.sep + ".rabbitmqadmin.conf") + os.environ['HOME'] = tmpdir + + self.run_fail(['--config', '/tmp/no-such-config-file', 'show', 'overview']) + + cf = os.path.dirname(__file__) + os.sep + "test-config" + self.run_success(['--config', cf, '--node', 'host_normal', 'show', 'overview']) + + # test 'default node in the config file' where "default" uses an invalid host + self.run_fail(['--config', cf, 'show', 'overview']) + self.run_success(["show", "overview"]) + self.run_fail(['--node', 'non_default', "show", "overview"]) + os.environ['HOME'] = original_home + + def test_user(self): + self.run_success(['--user', 'guest', '--password', 'guest', 'show', 'overview']) + self.run_fail(['--user', 'no', '--password', 'guest', 'show', 'overview']) + self.run_fail(['--user', 'guest', '--password', 'no', 'show', 'overview']) + + def test_fmt_long(self): + self.assert_output(""" +-------------------------------------------------------------------------------- + + name: / +tracing: False + +-------------------------------------------------------------------------------- + +""", ['--format', 'long', 'list', 'vhosts', 'name', 'tracing']) + + def test_fmt_kvp(self): + self.assert_output("""name="/" tracing="False" +""", ['--format', 'kvp', 'list', 'vhosts', 'name', 'tracing']) + + def test_fmt_tsv(self): + self.assert_output("""name tracing +/ False +""", ['--format', 'tsv', 'list', 'vhosts', 'name', 'tracing']) + + def test_fmt_table(self): + out = """+------+---------+ +| name | tracing | ++------+---------+ +| / | False | ++------+---------+ +""" + self.assert_output(out, ['list', 'vhosts', 'name', 'tracing']) + self.assert_output(out, ['--format', 'table', 'list', 'vhosts', 'name', 'tracing']) + + def test_fmt_bash(self): + self.assert_output("""/ +""", ['--format', 'bash', 'list', 'vhosts', 'name', 'tracing']) + + def test_vhosts(self): + self.assert_list(['/'], l('vhosts')) + self.run_success(['declare', 'vhost', 'name=foo']) + self.assert_list(['/', 'foo'], l('vhosts')) + self.run_success(['delete', 'vhost', 'name=foo']) + self.assert_list(['/'], l('vhosts')) + + def test_users(self): + self.assert_list(['guest'], l('users')) + self.run_fail(['declare', 'user', 'name=foo']) + self.run_success(['declare', 'user', 'name=foo', 'password=pass', 'tags=']) + self.assert_list(['foo', 'guest'], l('users')) + self.run_success(['delete', 'user', 'name=foo']) + self.assert_list(['guest'], l('users')) + + def test_permissions(self): + self.run_success(['declare', 'vhost', 'name=foo']) + self.run_success(['declare', 'user', 'name=bar', 'password=pass', 'tags=']) + self.assert_table([['guest', '/']], ['list', 'permissions', 'user', 'vhost']) + self.run_success(['declare', 'permission', 'user=bar', 'vhost=foo', 'configure=.*', 'write=.*', 'read=.*']) + self.assert_table([['guest', '/'], ['bar', 'foo']], ['list', 'permissions', 'user', 'vhost']) + self.run_success(['delete', 'user', 'name=bar']) + self.run_success(['delete', 'vhost', 'name=foo']) + + def test_alt_vhost(self): + self.run_success(['declare', 'vhost', 'name=foo']) + self.run_success(['declare', 'permission', 'user=guest', 'vhost=foo', 'configure=.*', 'write=.*', 'read=.*']) + self.run_success(['declare', 'queue', 'name=in_/']) + self.run_success(['--vhost', 'foo', 'declare', 'queue', 'name=in_foo']) + self.assert_table([['/', 'in_/'], ['foo', 'in_foo']], ['list', 'queues', 'vhost', 'name']) + self.run_success(['--vhost', 'foo', 'delete', 'queue', 'name=in_foo']) + self.run_success(['delete', 'queue', 'name=in_/']) + self.run_success(['delete', 'vhost', 'name=foo']) + + def test_exchanges(self): + self.run_success(['declare', 'exchange', 'name=foo', 'type=direct']) + self.assert_list(['', 'amq.direct', 'amq.fanout', 'amq.headers', 'amq.match', 'amq.rabbitmq.log', 'amq.rabbitmq.trace', 'amq.topic', 'foo'], l('exchanges')) + self.run_success(['delete', 'exchange', 'name=foo']) + + def test_queues(self): + self.run_success(['declare', 'queue', 'name=foo']) + self.assert_list(['foo'], l('queues')) + self.run_success(['delete', 'queue', 'name=foo']) + + def test_bindings(self): + self.run_success(['declare', 'queue', 'name=foo']) + self.run_success(['declare', 'binding', 'source=amq.direct', 'destination=foo', 'destination_type=queue', 'routing_key=test']) + self.assert_table([['', 'foo', 'queue', 'foo'], ['amq.direct', 'foo', 'queue', 'test']], ['list', 'bindings', 'source', 'destination', 'destination_type', 'routing_key']) + self.run_success(['delete', 'queue', 'name=foo']) + + def test_policies(self): + self.run_success(['declare', 'policy', 'name=ha', 'pattern=.*', 'definition={"ha-mode":"all"}']) + self.assert_table([['ha', '/', '.*', '{"ha-mode": "all"}']], ['list', 'policies', 'name', 'vhost', 'pattern', 'definition']) + self.run_success(['delete', 'policy', 'name=ha']) + + def test_parameters(self): + self.ctl(['eval', 'rabbit_runtime_parameters_test:register().']) + self.run_success(['declare', 'parameter', 'component=test', 'name=good', 'value=123']) + self.assert_table([['test', 'good', '/', '123']], ['list', 'parameters', 'component', 'name', 'vhost', 'value']) + self.run_success(['delete', 'parameter', 'component=test', 'name=good']) + self.ctl(['eval', 'rabbit_runtime_parameters_test:unregister().']) + + def test_publish(self): + self.run_success(['declare', 'queue', 'name=test']) + self.run_success(['publish', 'routing_key=test', 'payload=test_1']) + self.run_success(['publish', 'routing_key=test', 'payload=test_2']) + self.run_success(['publish', 'routing_key=test'], stdin=b'test_3') + self.assert_table([exp_msg('test', 2, False, 'test_1')], ['get', 'queue=test', 'requeue=false']) + self.assert_table([exp_msg('test', 1, False, 'test_2')], ['get', 'queue=test', 'requeue=true']) + self.assert_table([exp_msg('test', 1, True, 'test_2')], ['get', 'queue=test', 'requeue=false']) + self.assert_table([exp_msg('test', 0, False, 'test_3')], ['get', 'queue=test', 'requeue=false']) + self.run_success(['publish', 'routing_key=test'], stdin=b'test_4') + filename = '/tmp/rabbitmq-test/get.txt' + ensure_dir(filename) + self.run_success(['get', 'queue=test', 'requeue=false', 'payload_file=' + filename]) + with open(filename) as f: + self.assertEqual('test_4', f.read()) + os.remove(filename) + self.run_success(['delete', 'queue', 'name=test']) + + def test_ignore_vhost(self): + self.run_success(['--vhost', '/', 'show', 'overview']) + self.run_success(['--vhost', '/', 'list', 'users']) + self.run_success(['--vhost', '/', 'list', 'vhosts']) + self.run_success(['--vhost', '/', 'list', 'nodes']) + self.run_success(['--vhost', '/', 'list', 'permissions']) + self.run_success(['--vhost', '/', 'declare', 'user', 'name=foo', 'password=pass', 'tags=']) + self.run_success(['delete', 'user', 'name=foo']) + + def test_sort(self): + self.run_success(['declare', 'queue', 'name=foo']) + self.run_success(['declare', 'binding', 'source=amq.direct', 'destination=foo', 'destination_type=queue', 'routing_key=bbb']) + self.run_success(['declare', 'binding', 'source=amq.topic', 'destination=foo', 'destination_type=queue', 'routing_key=aaa']) + self.assert_table([['', 'foo'], ['amq.direct', 'bbb'], ['amq.topic', 'aaa']], ['--sort', 'source', 'list', 'bindings', 'source', 'routing_key']) + self.assert_table([['amq.topic', 'aaa'], ['amq.direct', 'bbb'], ['', 'foo']], ['--sort', 'routing_key', 'list', 'bindings', 'source', 'routing_key']) + self.assert_table([['amq.topic', 'aaa'], ['amq.direct', 'bbb'], ['', 'foo']], ['--sort', 'source', '--sort-reverse', 'list', 'bindings', 'source', 'routing_key']) + self.run_success(['delete', 'queue', 'name=foo']) + + # --------------------------------------------------------------------------- + + def run_success(self, args, **kwargs): + (stdout, ret) = self.admin(args, **kwargs) + if ret != 0: + self.fail(stdout) + + def run_fail(self, args): + (stdout, ret) = self.admin(args) + if ret == 0: + self.fail(stdout) + + def assert_output(self, expected, args): + self.assertEqual(expected, self.admin(args)[0]) + + def assert_list(self, expected, args0): + args = ['-f', 'tsv', '-q'] + args.extend(args0) + self.assertEqual(expected, self.admin(args)[0].splitlines()) + + def assert_table(self, expected, args0): + args = ['-f', 'tsv', '-q'] + args.extend(args0) + self.assertEqual(expected, [l.split('\t') for l in self.admin(args)[0].splitlines()]) + + def admin(self, args0, stdin=None): + args = ['python{0}'.format(sys.version_info[0]), + norm('../../../bin/rabbitmqadmin')] + args.extend(args0) + return run(args, stdin) + + def ctl(self, args0, stdin=None): + args = [norm('../../../../rabbitmq-server/scripts/rabbitmqctl'), '-n', 'rabbit-test'] + args.extend(args0) + (stdout, ret) = run(args, stdin) + if ret != 0: + self.fail(stdout) + +def norm(cmd): + return os.path.normpath(os.path.join(os.getcwd(), sys.argv[0], cmd)) + +def run(args, stdin): + proc = subprocess.Popen(args, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE) + (stdout, stderr) = proc.communicate(stdin) + returncode = proc.returncode + res = stdout.decode('utf-8') + stderr.decode('utf-8') + return (res, returncode) + +def l(thing): + return ['list', thing, 'name'] + +def exp_msg(key, count, redelivered, payload): + # routing_key, exchange, message_count, payload, payload_bytes, payload_encoding, properties, redelivered + return [key, '', str(count), payload, str(len(payload)), 'string', '', str(redelivered)] + +def ensure_dir(f): + d = os.path.dirname(f) + if not os.path.exists(d): + os.makedirs(d) + +if __name__ == '__main__': + print("\nrabbitmqadmin tests\n===================\n") + suite = unittest.TestLoader().loadTestsFromTestCase(TestRabbitMQAdmin) + results = unittest.TextTestRunner(verbosity=2).run(suite) + if not results.wasSuccessful(): + sys.exit(1) diff --git a/rabbitmq-server/plugins-src/rabbitmq-management/test/src/test-config b/rabbitmq-server/plugins-src/rabbitmq-management/test/src/test-config new file mode 100644 index 0000000..93322e7 --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-management/test/src/test-config @@ -0,0 +1,15 @@ +# rabbitmqadmin.conf.example START + +[host_normal] +hostname = localhost +port = 15672 +username = guest +password = guest +declare_vhost = / # Used as default for declare / delete only +vhost = / # Used as default for declare / delete / list + +[default] +hostname = localhost +port = 99999 +username = guest +password = guest diff --git a/rabbitmq-server/plugins-src/rabbitmq-mqtt/.srcdist_done b/rabbitmq-server/plugins-src/rabbitmq-mqtt/.srcdist_done new file mode 100644 index 0000000..e69de29 diff --git a/rabbitmq-server/plugins-src/rabbitmq-mqtt/CONTRIBUTING.md b/rabbitmq-server/plugins-src/rabbitmq-mqtt/CONTRIBUTING.md new file mode 100644 index 0000000..69a4b4a --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-mqtt/CONTRIBUTING.md @@ -0,0 +1,51 @@ +## Overview + +RabbitMQ projects use pull requests to discuss, collaborate on and accept code contributions. +Pull requests is the primary place of discussing code changes. + +## How to Contribute + +The process is fairly standard: + + * Fork the repository or repositories you plan on contributing to + * Clone [RabbitMQ umbrella repository](https://github.com/rabbitmq/rabbitmq-public-umbrella) + * `cd umbrella`, `make co` + * Create a branch with a descriptive name in the relevant repositories + * Make your changes, run tests, commit with a [descriptive message](http://tbaggery.com/2008/04/19/a-note-about-git-commit-messages.html), push to your fork + * Submit pull requests with an explanation what has been changed and **why** + * Submit a filled out and signed [Contributor Agreement](https://github.com/rabbitmq/ca#how-to-submit) if needed (see below) + * Be patient. We will get to your pull request eventually + +If what you are going to work on is a substantial change, please first ask the core team +of their opinion on [RabbitMQ mailing list](https://groups.google.com/forum/#!forum/rabbitmq-users). + + +## (Brief) Code of Conduct + +In one line: don't be a dick. + +Be respectful to the maintainers and other contributors. Open source +contributors put long hours into developing projects and doing user +support. Those projects and user support are available for free. We +believe this deserves some respect. + +Be respectful to people of all races, genders, religious beliefs and +political views. Regardless of how brilliant a pull request is +technically, we will not tolerate disrespectful or aggressive +behaviour. + +Contributors who violate this straightforward Code of Conduct will see +their pull requests closed and locked. + + +## Contributor Agreement + +If you want to contribute a non-trivial change, please submit a signed copy of our +[Contributor Agreement](https://github.com/rabbitmq/ca#how-to-submit) around the time +you submit your pull request. This will make it much easier (in some cases, possible) +for the RabbitMQ team at Pivotal to merge your contribution. + + +## Where to Ask Questions + +If something isn't clear, feel free to ask on our [mailing list](https://groups.google.com/forum/#!forum/rabbitmq-users). diff --git a/rabbitmq-server/plugins-src/rabbitmq-mqtt/Makefile b/rabbitmq-server/plugins-src/rabbitmq-mqtt/Makefile new file mode 100644 index 0000000..482105a --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-mqtt/Makefile @@ -0,0 +1 @@ +include ../umbrella.mk diff --git a/rabbitmq-server/plugins-src/rabbitmq-mqtt/README.md b/rabbitmq-server/plugins-src/rabbitmq-mqtt/README.md new file mode 100644 index 0000000..72ba9ea --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-mqtt/README.md @@ -0,0 +1,9 @@ +# RabbitMQ MQTT adapter + +The MQTT adapter is included in the RabbitMQ distribution. To enable +it, use rabbitmq-plugins: + + rabbitmq-plugins enable rabbitmq_mqtt + +Full usage instructions can be found at +. diff --git a/rabbitmq-server/plugins-src/rabbitmq-mqtt/include/rabbit_mqtt.hrl b/rabbitmq-server/plugins-src/rabbitmq-mqtt/include/rabbit_mqtt.hrl new file mode 100644 index 0000000..b620a31 --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-mqtt/include/rabbit_mqtt.hrl @@ -0,0 +1,44 @@ +%% The contents of this file are subject to the Mozilla Public License +%% Version 1.1 (the "License"); you may not use this file except in +%% compliance with the License. You may obtain a copy of the License +%% at http://www.mozilla.org/MPL/ +%% +%% Software distributed under the License is distributed on an "AS IS" +%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See +%% the License for the specific language governing rights and +%% limitations under the License. +%% +%% The Original Code is RabbitMQ. +%% +%% The Initial Developer of the Original Code is GoPivotal, Inc. +%% Copyright (c) 2007-2015 Pivotal Software, Inc. All rights reserved. +%% + +-define(CLIENT_ID_MAXLEN, 23). + +%% reader state +-record(state, { socket, + conn_name, + await_recv, + connection_state, + keepalive, + keepalive_sup, + conserve, + parse_state, + proc_state }). + +%% processor state +-record(proc_state, { socket, + subscriptions, + consumer_tags, + unacked_pubs, + awaiting_ack, + awaiting_seqno, + message_id, + client_id, + clean_sess, + will_msg, + channels, + connection, + exchange, + ssl_login_name }). diff --git a/rabbitmq-server/plugins-src/rabbitmq-mqtt/include/rabbit_mqtt_frame.hrl b/rabbitmq-server/plugins-src/rabbitmq-mqtt/include/rabbit_mqtt_frame.hrl new file mode 100644 index 0000000..968f986 --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-mqtt/include/rabbit_mqtt_frame.hrl @@ -0,0 +1,93 @@ +%% The contents of this file are subject to the Mozilla Public License +%% Version 1.1 (the "License"); you may not use this file except in +%% compliance with the License. You may obtain a copy of the License +%% at http://www.mozilla.org/MPL/ +%% +%% Software distributed under the License is distributed on an "AS IS" +%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See +%% the License for the specific language governing rights and +%% limitations under the License. +%% +%% The Original Code is RabbitMQ. +%% +%% The Initial Developer of the Original Code is GoPivotal, Inc. +%% Copyright (c) 2007-2015 Pivotal Software, Inc. All rights reserved. +%% + +-define(PROTOCOL_NAMES, [{3, "MQIsdp"}, {4, "MQTT"}]). + +%% frame types + +-define(CONNECT, 1). +-define(CONNACK, 2). +-define(PUBLISH, 3). +-define(PUBACK, 4). +-define(PUBREC, 5). +-define(PUBREL, 6). +-define(PUBCOMP, 7). +-define(SUBSCRIBE, 8). +-define(SUBACK, 9). +-define(UNSUBSCRIBE, 10). +-define(UNSUBACK, 11). +-define(PINGREQ, 12). +-define(PINGRESP, 13). +-define(DISCONNECT, 14). + +%% connect return codes + +-define(CONNACK_ACCEPT, 0). +-define(CONNACK_PROTO_VER, 1). %% unacceptable protocol version +-define(CONNACK_INVALID_ID, 2). %% identifier rejected +-define(CONNACK_SERVER, 3). %% server unavailable +-define(CONNACK_CREDENTIALS, 4). %% bad user name or password +-define(CONNACK_AUTH, 5). %% not authorized + +%% qos levels + +-define(QOS_0, 0). +-define(QOS_1, 1). +-define(QOS_2, 2). + +-record(mqtt_frame, {fixed, + variable, + payload}). + +-record(mqtt_frame_fixed, {type = 0, + dup = 0, + qos = 0, + retain = 0}). + +-record(mqtt_frame_connect, {proto_ver, + will_retain, + will_qos, + will_flag, + clean_sess, + keep_alive, + client_id, + will_topic, + will_msg, + username, + password}). + +-record(mqtt_frame_connack, {return_code}). + +-record(mqtt_frame_publish, {topic_name, + message_id}). + +-record(mqtt_frame_subscribe,{message_id, + topic_table}). + +-record(mqtt_frame_suback, {message_id, + qos_table = []}). + +-record(mqtt_topic, {name, + qos}). + +-record(mqtt_frame_other, {other}). + +-record(mqtt_msg, {retain, + qos, + topic, + dup, + message_id, + payload}). diff --git a/rabbitmq-server/plugins-src/rabbitmq-mqtt/package.mk b/rabbitmq-server/plugins-src/rabbitmq-mqtt/package.mk new file mode 100644 index 0000000..9db0895 --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-mqtt/package.mk @@ -0,0 +1,21 @@ +RELEASABLE:=true +DEPS:=rabbitmq-server rabbitmq-erlang-client rabbitmq-test +STANDALONE_TEST_COMMANDS:=eunit:test(rabbit_mqtt_util) +WITH_BROKER_TEST_SCRIPTS:=$(PACKAGE_DIR)/test/test.sh +WITH_BROKER_TEST_CONFIG:=$(PACKAGE_DIR)/test/ebin/test +WITH_BROKER_SETUP_SCRIPTS:=$(PACKAGE_DIR)/test/setup-rabbit-test.sh + +define package_rules + +$(PACKAGE_DIR)+pre-test:: + rm -rf $(PACKAGE_DIR)/test/certs + mkdir $(PACKAGE_DIR)/test/certs + mkdir -p $(PACKAGE_DIR)/test/ebin + sed -E -e "s|%%CERTS_DIR%%|$(abspath $(PACKAGE_DIR))/test/certs|g" < $(PACKAGE_DIR)/test/src/test.config > $(PACKAGE_DIR)/test/ebin/test.config + $(MAKE) -C $(PACKAGE_DIR)/../rabbitmq-test/certs all PASSWORD=bunnychow DIR=$(abspath $(PACKAGE_DIR))/test/certs + cp $(PACKAGE_DIR)/test/src/rabbitmq_mqtt_standalone.app.src $(PACKAGE_DIR)/test/ebin/rabbitmq_mqtt.app + +$(PACKAGE_DIR)+clean:: + rm -rf $(PACKAGE_DIR)/test/certs + +endef diff --git a/rabbitmq-server/plugins-src/rabbitmq-mqtt/src/rabbit_mqtt.erl b/rabbitmq-server/plugins-src/rabbitmq-mqtt/src/rabbit_mqtt.erl new file mode 100644 index 0000000..92c2916 --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-mqtt/src/rabbit_mqtt.erl @@ -0,0 +1,28 @@ +%% The contents of this file are subject to the Mozilla Public License +%% Version 1.1 (the "License"); you may not use this file except in +%% compliance with the License. You may obtain a copy of the License +%% at http://www.mozilla.org/MPL/ +%% +%% Software distributed under the License is distributed on an "AS IS" +%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See +%% the License for the specific language governing rights and +%% limitations under the License. +%% +%% The Original Code is RabbitMQ. +%% +%% The Initial Developer of the Original Code is GoPivotal, Inc. +%% Copyright (c) 2007-2015 Pivotal Software, Inc. All rights reserved. +%% + +-module(rabbit_mqtt). + +-behaviour(application). +-export([start/2, stop/1]). + +start(normal, []) -> + {ok, Listeners} = application:get_env(tcp_listeners), + {ok, SslListeners} = application:get_env(ssl_listeners), + rabbit_mqtt_sup:start_link({Listeners, SslListeners}, []). + +stop(_State) -> + ok. diff --git a/rabbitmq-server/plugins-src/rabbitmq-mqtt/src/rabbit_mqtt_collector.erl b/rabbitmq-server/plugins-src/rabbitmq-mqtt/src/rabbit_mqtt_collector.erl new file mode 100644 index 0000000..52a3da9 --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-mqtt/src/rabbit_mqtt_collector.erl @@ -0,0 +1,94 @@ +%% The contents of this file are subject to the Mozilla Public License +%% Version 1.1 (the "License"); you may not use this file except in +%% compliance with the License. You may obtain a copy of the License +%% at http://www.mozilla.org/MPL/ +%% +%% Software distributed under the License is distributed on an "AS IS" +%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See +%% the License for the specific language governing rights and +%% limitations under the License. +%% +%% The Original Code is RabbitMQ. +%% +%% The Initial Developer of the Original Code is GoPivotal, Inc. +%% Copyright (c) 2007-2015 Pivotal Software, Inc. All rights reserved. +%% + +-module(rabbit_mqtt_collector). + +-behaviour(gen_server). + +-export([start_link/0, register/2, unregister/2]). + +-export([init/1, handle_call/3, handle_cast/2, handle_info/2, + terminate/2, code_change/3]). + +-record(state, {client_ids}). + +-define(SERVER, ?MODULE). + +%%---------------------------------------------------------------------------- + +start_link() -> + gen_server:start_link({local, ?SERVER}, ?MODULE, [], []). + +register(ClientId, Pid) -> + gen_server:call(rabbit_mqtt_collector, {register, ClientId, Pid}, infinity). + +unregister(ClientId, Pid) -> + gen_server:call(rabbit_mqtt_collector, {unregister, ClientId, Pid}, infinity). + +%%---------------------------------------------------------------------------- + +init([]) -> + {ok, #state{client_ids = dict:new()}}. % clientid -> {pid, monitor} + +%%-------------------------------------------------------------------------- + +handle_call({register, ClientId, Pid}, _From, + State = #state{client_ids = Ids}) -> + Ids1 = case dict:find(ClientId, Ids) of + {ok, {OldPid, MRef}} when Pid =/= OldPid -> + catch gen_server2:cast(OldPid, duplicate_id), + erlang:demonitor(MRef), + dict:erase(ClientId, Ids); + error -> + Ids + end, + Ids2 = dict:store(ClientId, {Pid, erlang:monitor(process, Pid)}, Ids1), + {reply, ok, State#state{client_ids = Ids2}}; + +handle_call({unregister, ClientId, Pid}, _From, State = #state{client_ids = Ids}) -> + {Reply, Ids1} = case dict:find(ClientId, Ids) of + {ok, {Pid, MRef}} -> erlang:demonitor(MRef), + {ok, dict:erase(ClientId, Ids)}; + _ -> {ok, Ids} + end, + {reply, Reply, State#state{ client_ids = Ids1 }}; + +handle_call(Msg, _From, State) -> + {stop, {unhandled_call, Msg}, State}. + +handle_cast(Msg, State) -> + {stop, {unhandled_cast, Msg}, State}. + +handle_info({'EXIT', _, {shutdown, closed}}, State) -> + {stop, {shutdown, closed}, State}; + +handle_info({'DOWN', MRef, process, DownPid, _Reason}, + State = #state{client_ids = Ids}) -> + Ids1 = dict:filter(fun (ClientId, {Pid, M}) + when Pid =:= DownPid, MRef =:= M -> + rabbit_log:warning("MQTT disconnect from ~p~n", + [ClientId]), + false; + (_, _) -> + true + end, Ids), + {noreply, State #state{ client_ids = Ids1 }}. + +terminate(_Reason, _State) -> + ok. + +code_change(_OldVsn, State, _Extra) -> + {ok, State}. diff --git a/rabbitmq-server/plugins-src/rabbitmq-mqtt/src/rabbit_mqtt_connection_sup.erl b/rabbitmq-server/plugins-src/rabbitmq-mqtt/src/rabbit_mqtt_connection_sup.erl new file mode 100644 index 0000000..fd083a4 --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-mqtt/src/rabbit_mqtt_connection_sup.erl @@ -0,0 +1,51 @@ +%% The contents of this file are subject to the Mozilla Public License +%% Version 1.1 (the "License"); you may not use this file except in +%% compliance with the License. You may obtain a copy of the License +%% at http://www.mozilla.org/MPL/ +%% +%% Software distributed under the License is distributed on an "AS IS" +%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See +%% the License for the specific language governing rights and +%% limitations under the License. +%% +%% The Original Code is RabbitMQ. +%% +%% The Initial Developer of the Original Code is GoPivotal, Inc. +%% Copyright (c) 2007-2015 Pivotal Software, Inc. All rights reserved. +%% + +-module(rabbit_mqtt_connection_sup). + +-behaviour(supervisor2). + +-define(MAX_WAIT, 16#ffffffff). + +-export([start_link/0, start_keepalive_link/0]). + +-export([init/1]). + +%%---------------------------------------------------------------------------- + +start_link() -> + {ok, SupPid} = supervisor2:start_link(?MODULE, []), + {ok, ReaderPid} = supervisor2:start_child( + SupPid, + {rabbit_mqtt_reader, + {rabbit_mqtt_reader, start_link, []}, + intrinsic, ?MAX_WAIT, worker, [rabbit_mqtt_reader]}), + {ok, KeepaliveSup} = supervisor2:start_child( + SupPid, + {rabbit_keepalive_sup, + {rabbit_mqtt_connection_sup, start_keepalive_link, []}, + intrinsic, infinity, supervisor, [rabbit_keepalive_sup]}), + {ok, SupPid, {KeepaliveSup, ReaderPid}}. + +start_keepalive_link() -> + supervisor2:start_link(?MODULE, []). + +%%---------------------------------------------------------------------------- + +init([]) -> + {ok, {{one_for_all, 0, 1}, []}}. + + diff --git a/rabbitmq-server/plugins-src/rabbitmq-mqtt/src/rabbit_mqtt_frame.erl b/rabbitmq-server/plugins-src/rabbitmq-mqtt/src/rabbit_mqtt_frame.erl new file mode 100644 index 0000000..6282411 --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-mqtt/src/rabbit_mqtt_frame.erl @@ -0,0 +1,232 @@ +%% The contents of this file are subject to the Mozilla Public License +%% Version 1.1 (the "License"); you may not use this file except in +%% compliance with the License. You may obtain a copy of the License +%% at http://www.mozilla.org/MPL/ +%% +%% Software distributed under the License is distributed on an "AS IS" +%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See +%% the License for the specific language governing rights and +%% limitations under the License. +%% +%% The Original Code is RabbitMQ. +%% +%% The Initial Developer of the Original Code is GoPivotal, Inc. +%% Copyright (c) 2007-2015 Pivotal Software, Inc. All rights reserved. +%% + +-module(rabbit_mqtt_frame). + +-export([parse/2, initial_state/0]). +-export([serialise/1]). + +-include("rabbit_mqtt_frame.hrl"). + +-define(RESERVED, 0). +-define(MAX_LEN, 16#fffffff). +-define(HIGHBIT, 2#10000000). +-define(LOWBITS, 2#01111111). + +initial_state() -> none. + +parse(<<>>, none) -> + {more, fun(Bin) -> parse(Bin, none) end}; +parse(<>, none) -> + parse_remaining_len(Rest, #mqtt_frame_fixed{ type = MessageType, + dup = bool(Dup), + qos = QoS, + retain = bool(Retain) }); +parse(Bin, Cont) -> Cont(Bin). + +parse_remaining_len(<<>>, Fixed) -> + {more, fun(Bin) -> parse_remaining_len(Bin, Fixed) end}; +parse_remaining_len(Rest, Fixed) -> + parse_remaining_len(Rest, Fixed, 1, 0). + +parse_remaining_len(_Bin, _Fixed, _Multiplier, Length) + when Length > ?MAX_LEN -> + {error, invalid_mqtt_frame_len}; +parse_remaining_len(<<>>, Fixed, Multiplier, Length) -> + {more, fun(Bin) -> parse_remaining_len(Bin, Fixed, Multiplier, Length) end}; +parse_remaining_len(<<1:1, Len:7, Rest/binary>>, Fixed, Multiplier, Value) -> + parse_remaining_len(Rest, Fixed, Multiplier * ?HIGHBIT, Value + Len * Multiplier); +parse_remaining_len(<<0:1, Len:7, Rest/binary>>, Fixed, Multiplier, Value) -> + parse_frame(Rest, Fixed, Value + Len * Multiplier). + +parse_frame(Bin, #mqtt_frame_fixed{ type = Type, + qos = Qos } = Fixed, Length) -> + case {Type, Bin} of + {?CONNECT, <>} -> + {ProtoName, Rest1} = parse_utf(FrameBin), + <> = Rest1, + <> = Rest2, + {ClientId, Rest4} = parse_utf(Rest3), + {WillTopic, Rest5} = parse_utf(Rest4, WillFlag), + {WillMsg, Rest6} = parse_msg(Rest5, WillFlag), + {UserName, Rest7} = parse_utf(Rest6, UsernameFlag), + {PasssWord, <<>>} = parse_utf(Rest7, PasswordFlag), + case protocol_name_approved(ProtoVersion, ProtoName) of + true -> + wrap(Fixed, + #mqtt_frame_connect{ + proto_ver = ProtoVersion, + will_retain = bool(WillRetain), + will_qos = WillQos, + will_flag = bool(WillFlag), + clean_sess = bool(CleanSession), + keep_alive = KeepAlive, + client_id = ClientId, + will_topic = WillTopic, + will_msg = WillMsg, + username = UserName, + password = PasssWord}, Rest); + false -> + {error, protocol_header_corrupt} + end; + {?PUBLISH, <>} -> + {TopicName, Rest1} = parse_utf(FrameBin), + {MessageId, Payload} = case Qos of + 0 -> {undefined, Rest1}; + _ -> <> = Rest1, + {M, R} + end, + wrap(Fixed, #mqtt_frame_publish { topic_name = TopicName, + message_id = MessageId }, + Payload, Rest); + {?PUBACK, <>} -> + <> = FrameBin, + wrap(Fixed, #mqtt_frame_publish { message_id = MessageId }, Rest); + {Subs, <>} + when Subs =:= ?SUBSCRIBE orelse Subs =:= ?UNSUBSCRIBE -> + 1 = Qos, + <> = FrameBin, + Topics = parse_topics(Subs, Rest1, []), + wrap(Fixed, #mqtt_frame_subscribe { message_id = MessageId, + topic_table = Topics }, Rest); + {Minimal, Rest} + when Minimal =:= ?DISCONNECT orelse Minimal =:= ?PINGREQ -> + Length = 0, + wrap(Fixed, Rest); + {_, TooShortBin} -> + {more, fun(BinMore) -> + parse_frame(<>, + Fixed, Length) + end} + end. + +parse_topics(_, <<>>, Topics) -> + Topics; +parse_topics(?SUBSCRIBE = Sub, Bin, Topics) -> + {Name, <<_:6, QoS:2, Rest/binary>>} = parse_utf(Bin), + parse_topics(Sub, Rest, [#mqtt_topic { name = Name, qos = QoS } | Topics]); +parse_topics(?UNSUBSCRIBE = Sub, Bin, Topics) -> + {Name, <>} = parse_utf(Bin), + parse_topics(Sub, Rest, [#mqtt_topic { name = Name } | Topics]). + +wrap(Fixed, Variable, Payload, Rest) -> + {ok, #mqtt_frame { variable = Variable, fixed = Fixed, payload = Payload }, Rest}. +wrap(Fixed, Variable, Rest) -> + {ok, #mqtt_frame { variable = Variable, fixed = Fixed }, Rest}. +wrap(Fixed, Rest) -> + {ok, #mqtt_frame { fixed = Fixed }, Rest}. + +parse_utf(Bin, 0) -> + {undefined, Bin}; +parse_utf(Bin, _) -> + parse_utf(Bin). + +parse_utf(<>) -> + {binary_to_list(Str), Rest}. + +parse_msg(Bin, 0) -> + {undefined, Bin}; +parse_msg(<>, _) -> + {Msg, Rest}. + +bool(0) -> false; +bool(1) -> true. + +%% serialisation + +serialise(#mqtt_frame{ fixed = Fixed, + variable = Variable, + payload = Payload }) -> + serialise_variable(Fixed, Variable, serialise_payload(Payload)). + +serialise_payload(undefined) -> <<>>; +serialise_payload(B) when is_binary(B) -> B. + +serialise_variable(#mqtt_frame_fixed { type = ?CONNACK } = Fixed, + #mqtt_frame_connack { return_code = ReturnCode }, + <<>> = PayloadBin) -> + VariableBin = <>, + serialise_fixed(Fixed, VariableBin, PayloadBin); + +serialise_variable(#mqtt_frame_fixed { type = SubAck } = Fixed, + #mqtt_frame_suback { message_id = MessageId, + qos_table = Qos }, + <<>> = _PayloadBin) + when SubAck =:= ?SUBACK orelse SubAck =:= ?UNSUBACK -> + VariableBin = <>, + QosBin = << <> || Q <- Qos >>, + serialise_fixed(Fixed, VariableBin, QosBin); + +serialise_variable(#mqtt_frame_fixed { type = ?PUBLISH, + qos = Qos } = Fixed, + #mqtt_frame_publish { topic_name = TopicName, + message_id = MessageId }, + PayloadBin) -> + TopicBin = serialise_utf(TopicName), + MessageIdBin = case Qos of + 0 -> <<>>; + 1 -> <> + end, + serialise_fixed(Fixed, <>, PayloadBin); + +serialise_variable(#mqtt_frame_fixed { type = ?PUBACK } = Fixed, + #mqtt_frame_publish { message_id = MessageId }, + PayloadBin) -> + MessageIdBin = <>, + serialise_fixed(Fixed, MessageIdBin, PayloadBin); + +serialise_variable(#mqtt_frame_fixed {} = Fixed, + undefined, + <<>> = _PayloadBin) -> + serialise_fixed(Fixed, <<>>, <<>>). + +serialise_fixed(#mqtt_frame_fixed{ type = Type, + dup = Dup, + qos = Qos, + retain = Retain }, VariableBin, PayloadBin) + when is_integer(Type) andalso ?CONNECT =< Type andalso Type =< ?DISCONNECT -> + Len = size(VariableBin) + size(PayloadBin), + true = (Len =< ?MAX_LEN), + LenBin = serialise_len(Len), + <>. + +serialise_utf(String) -> + StringBin = unicode:characters_to_binary(String), + Len = size(StringBin), + true = (Len =< 16#ffff), + <>. + +serialise_len(N) when N =< ?LOWBITS -> + <<0:1, N:7>>; +serialise_len(N) -> + <<1:1, (N rem ?HIGHBIT):7, (serialise_len(N div ?HIGHBIT))/binary>>. + +opt(undefined) -> ?RESERVED; +opt(false) -> 0; +opt(true) -> 1; +opt(X) when is_integer(X) -> X. + +protocol_name_approved(Ver, Name) -> + lists:member({Ver, Name}, ?PROTOCOL_NAMES). diff --git a/rabbitmq-server/plugins-src/rabbitmq-mqtt/src/rabbit_mqtt_processor.erl b/rabbitmq-server/plugins-src/rabbitmq-mqtt/src/rabbit_mqtt_processor.erl new file mode 100644 index 0000000..5c51a8b --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-mqtt/src/rabbit_mqtt_processor.erl @@ -0,0 +1,523 @@ +%% The contents of this file are subject to the Mozilla Public License +%% Version 1.1 (the "License"); you may not use this file except in +%% compliance with the License. You may obtain a copy of the License +%% at http://www.mozilla.org/MPL/ +%% +%% Software distributed under the License is distributed on an "AS IS" +%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See +%% the License for the specific language governing rights and +%% limitations under the License. +%% +%% The Original Code is RabbitMQ. +%% +%% The Initial Developer of the Original Code is GoPivotal, Inc. +%% Copyright (c) 2007-2015 Pivotal Software, Inc. All rights reserved. +%% + +-module(rabbit_mqtt_processor). + +-export([info/2, initial_state/2, + process_frame/2, amqp_pub/2, amqp_callback/2, send_will/1, + close_connection/1]). + +-include_lib("amqp_client/include/amqp_client.hrl"). +-include("rabbit_mqtt_frame.hrl"). +-include("rabbit_mqtt.hrl"). + +-define(APP, rabbitmq_mqtt). +-define(FRAME_TYPE(Frame, Type), + Frame = #mqtt_frame{ fixed = #mqtt_frame_fixed{ type = Type }}). + +initial_state(Socket,SSLLoginName) -> + #proc_state{ unacked_pubs = gb_trees:empty(), + awaiting_ack = gb_trees:empty(), + message_id = 1, + subscriptions = dict:new(), + consumer_tags = {undefined, undefined}, + channels = {undefined, undefined}, + exchange = rabbit_mqtt_util:env(exchange), + socket = Socket, + ssl_login_name = SSLLoginName }. + +info(client_id, #proc_state{ client_id = ClientId }) -> ClientId. + +process_frame(#mqtt_frame{ fixed = #mqtt_frame_fixed{ type = Type }}, + PState = #proc_state{ connection = undefined } ) + when Type =/= ?CONNECT -> + {error, connect_expected, PState}; +process_frame(Frame = #mqtt_frame{ fixed = #mqtt_frame_fixed{ type = Type }}, + PState) -> + process_request(Type, Frame, PState). + +process_request(?CONNECT, + #mqtt_frame{ variable = #mqtt_frame_connect{ + username = Username, + password = Password, + proto_ver = ProtoVersion, + clean_sess = CleanSess, + client_id = ClientId0, + keep_alive = Keepalive} = Var}, + PState = #proc_state{ ssl_login_name = SSLLoginName }) -> + ClientId = case ClientId0 of + [] -> rabbit_mqtt_util:gen_client_id(); + [_|_] -> ClientId0 + end, + {ReturnCode, PState1} = + case {lists:member(ProtoVersion, proplists:get_keys(?PROTOCOL_NAMES)), + ClientId0 =:= [] andalso CleanSess =:= false} of + {false, _} -> + {?CONNACK_PROTO_VER, PState}; + {_, true} -> + {?CONNACK_INVALID_ID, PState}; + _ -> + case creds(Username, Password, SSLLoginName) of + nocreds -> + rabbit_log:error("MQTT login failed - no credentials~n"), + {?CONNACK_CREDENTIALS, PState}; + {UserBin, PassBin} -> + case process_login(UserBin, PassBin, ProtoVersion, PState) of + {?CONNACK_ACCEPT, Conn} -> + link(Conn), + {ok, Ch} = amqp_connection:open_channel(Conn), + link(Ch), + amqp_channel:enable_delivery_flow_control(Ch), + ok = rabbit_mqtt_collector:register( + ClientId, self()), + Prefetch = rabbit_mqtt_util:env(prefetch), + #'basic.qos_ok'{} = amqp_channel:call( + Ch, #'basic.qos'{prefetch_count = Prefetch}), + rabbit_mqtt_reader:start_keepalive(self(), Keepalive), + {?CONNACK_ACCEPT, + maybe_clean_sess( + PState #proc_state{ will_msg = make_will_msg(Var), + clean_sess = CleanSess, + channels = {Ch, undefined}, + connection = Conn, + client_id = ClientId })}; + ConnAck -> + {ConnAck, PState} + end + end + end, + send_client(#mqtt_frame{ fixed = #mqtt_frame_fixed{ type = ?CONNACK}, + variable = #mqtt_frame_connack{ + return_code = ReturnCode }}, PState1), + {ok, PState1}; + +process_request(?PUBACK, + #mqtt_frame{ + variable = #mqtt_frame_publish{ message_id = MessageId }}, + #proc_state{ channels = {Channel, _}, + awaiting_ack = Awaiting } = PState) -> + Tag = gb_trees:get(MessageId, Awaiting), + amqp_channel:cast( + Channel, #'basic.ack'{ delivery_tag = Tag }), + {ok, PState #proc_state{ awaiting_ack = gb_trees:delete( MessageId, Awaiting)}}; + +process_request(?PUBLISH, + #mqtt_frame{ + fixed = #mqtt_frame_fixed{ qos = ?QOS_2 }}, PState) -> + {error, qos2_not_supported, PState}; +process_request(?PUBLISH, + #mqtt_frame{ + fixed = #mqtt_frame_fixed{ qos = Qos, + retain = Retain, + dup = Dup }, + variable = #mqtt_frame_publish{ topic_name = Topic, + message_id = MessageId }, + payload = Payload }, PState) -> + {ok, amqp_pub(#mqtt_msg{ retain = Retain, + qos = Qos, + topic = Topic, + dup = Dup, + message_id = MessageId, + payload = Payload }, PState)}; + +process_request(?SUBSCRIBE, + #mqtt_frame{ + variable = #mqtt_frame_subscribe{ message_id = MessageId, + topic_table = Topics }, + payload = undefined }, + #proc_state{ channels = {Channel, _}, + exchange = Exchange} = PState0) -> + {QosResponse, PState1} = + lists:foldl(fun (#mqtt_topic{ name = TopicName, + qos = Qos }, {QosList, PState}) -> + SupportedQos = supported_subs_qos(Qos), + {Queue, #proc_state{ subscriptions = Subs } = PState1} = + ensure_queue(SupportedQos, PState), + Binding = #'queue.bind'{ + queue = Queue, + exchange = Exchange, + routing_key = rabbit_mqtt_util:mqtt2amqp( + TopicName)}, + #'queue.bind_ok'{} = amqp_channel:call(Channel, Binding), + {[SupportedQos | QosList], + PState1 #proc_state{ subscriptions = + dict:append(TopicName, SupportedQos, Subs) }} + end, {[], PState0}, Topics), + send_client(#mqtt_frame{ fixed = #mqtt_frame_fixed{ type = ?SUBACK }, + variable = #mqtt_frame_suback{ + message_id = MessageId, + qos_table = QosResponse }}, PState1), + + {ok, PState1}; + +process_request(?UNSUBSCRIBE, + #mqtt_frame{ + variable = #mqtt_frame_subscribe{ message_id = MessageId, + topic_table = Topics }, + payload = undefined }, #proc_state{ channels = {Channel, _}, + exchange = Exchange, + client_id = ClientId, + subscriptions = Subs0} = PState) -> + Queues = rabbit_mqtt_util:subcription_queue_name(ClientId), + Subs1 = + lists:foldl( + fun (#mqtt_topic{ name = TopicName }, Subs) -> + QosSubs = case dict:find(TopicName, Subs) of + {ok, Val} when is_list(Val) -> lists:usort(Val); + error -> [] + end, + lists:foreach( + fun (QosSub) -> + Queue = element(QosSub + 1, Queues), + Binding = #'queue.unbind'{ + queue = Queue, + exchange = Exchange, + routing_key = + rabbit_mqtt_util:mqtt2amqp(TopicName)}, + #'queue.unbind_ok'{} = amqp_channel:call(Channel, Binding) + end, QosSubs), + dict:erase(TopicName, Subs) + end, Subs0, Topics), + send_client(#mqtt_frame{ fixed = #mqtt_frame_fixed { type = ?UNSUBACK }, + variable = #mqtt_frame_suback{ message_id = MessageId }}, + PState), + {ok, PState #proc_state{ subscriptions = Subs1 }}; + +process_request(?PINGREQ, #mqtt_frame{}, PState) -> + send_client(#mqtt_frame{ fixed = #mqtt_frame_fixed{ type = ?PINGRESP }}, + PState), + {ok, PState}; + +process_request(?DISCONNECT, #mqtt_frame{}, PState) -> + {stop, PState}. + +%%---------------------------------------------------------------------------- + +amqp_callback({#'basic.deliver'{ consumer_tag = ConsumerTag, + delivery_tag = DeliveryTag, + routing_key = RoutingKey }, + #amqp_msg{ props = #'P_basic'{ headers = Headers }, + payload = Payload }, + DeliveryCtx} = Delivery, + #proc_state{ channels = {Channel, _}, + awaiting_ack = Awaiting, + message_id = MsgId } = PState) -> + amqp_channel:notify_received(DeliveryCtx), + case {delivery_dup(Delivery), delivery_qos(ConsumerTag, Headers, PState)} of + {true, {?QOS_0, ?QOS_1}} -> + amqp_channel:cast( + Channel, #'basic.ack'{ delivery_tag = DeliveryTag }), + {ok, PState}; + {true, {?QOS_0, ?QOS_0}} -> + {ok, PState}; + {Dup, {DeliveryQos, _SubQos} = Qos} -> + send_client( + #mqtt_frame{ fixed = #mqtt_frame_fixed{ + type = ?PUBLISH, + qos = DeliveryQos, + dup = Dup }, + variable = #mqtt_frame_publish{ + message_id = + case DeliveryQos of + ?QOS_0 -> undefined; + ?QOS_1 -> MsgId + end, + topic_name = + rabbit_mqtt_util:amqp2mqtt( + RoutingKey) }, + payload = Payload}, PState), + case Qos of + {?QOS_0, ?QOS_0} -> + {ok, PState}; + {?QOS_1, ?QOS_1} -> + {ok, + next_msg_id( + PState #proc_state{ + awaiting_ack = + gb_trees:insert(MsgId, DeliveryTag, Awaiting)})}; + {?QOS_0, ?QOS_1} -> + amqp_channel:cast( + Channel, #'basic.ack'{ delivery_tag = DeliveryTag }), + {ok, PState} + end + end; + +amqp_callback(#'basic.ack'{ multiple = true, delivery_tag = Tag } = Ack, + PState = #proc_state{ unacked_pubs = UnackedPubs }) -> + case gb_trees:size(UnackedPubs) > 0 andalso + gb_trees:take_smallest(UnackedPubs) of + {TagSmall, MsgId, UnackedPubs1} when TagSmall =< Tag -> + send_client( + #mqtt_frame{ fixed = #mqtt_frame_fixed{ type = ?PUBACK }, + variable = #mqtt_frame_publish{ message_id = MsgId }}, + PState), + amqp_callback(Ack, PState #proc_state{ unacked_pubs = UnackedPubs1 }); + _ -> + {ok, PState} + end; + +amqp_callback(#'basic.ack'{ multiple = false, delivery_tag = Tag }, + PState = #proc_state{ unacked_pubs = UnackedPubs }) -> + send_client( + #mqtt_frame{ fixed = #mqtt_frame_fixed{ type = ?PUBACK }, + variable = #mqtt_frame_publish{ + message_id = gb_trees:get( + Tag, UnackedPubs) }}, PState), + {ok, PState #proc_state{ unacked_pubs = gb_trees:delete(Tag, UnackedPubs) }}. + +delivery_dup({#'basic.deliver'{ redelivered = Redelivered }, + #amqp_msg{ props = #'P_basic'{ headers = Headers }}, + _DeliveryCtx}) -> + case rabbit_mqtt_util:table_lookup(Headers, <<"x-mqtt-dup">>) of + undefined -> Redelivered; + {bool, Dup} -> Redelivered orelse Dup + end. + +next_msg_id(PState = #proc_state{ message_id = 16#ffff }) -> + PState #proc_state{ message_id = 1 }; +next_msg_id(PState = #proc_state{ message_id = MsgId }) -> + PState #proc_state{ message_id = MsgId + 1 }. + +%% decide at which qos level to deliver based on subscription +%% and the message publish qos level. non-MQTT publishes are +%% assumed to be qos 1, regardless of delivery_mode. +delivery_qos(Tag, _Headers, #proc_state{ consumer_tags = {Tag, _} }) -> + {?QOS_0, ?QOS_0}; +delivery_qos(Tag, Headers, #proc_state{ consumer_tags = {_, Tag} }) -> + case rabbit_mqtt_util:table_lookup(Headers, <<"x-mqtt-publish-qos">>) of + {byte, Qos} -> {lists:min([Qos, ?QOS_1]), ?QOS_1}; + undefined -> {?QOS_1, ?QOS_1} + end. + +maybe_clean_sess(PState = #proc_state { clean_sess = false }) -> + {_Queue, PState1} = ensure_queue(?QOS_1, PState), + PState1; +maybe_clean_sess(PState = #proc_state { clean_sess = true, + connection = Conn, + client_id = ClientId }) -> + {_, Queue} = rabbit_mqtt_util:subcription_queue_name(ClientId), + {ok, Channel} = amqp_connection:open_channel(Conn), + try amqp_channel:call(Channel, #'queue.delete'{ queue = Queue }) of + #'queue.delete_ok'{} -> ok = amqp_channel:close(Channel) + catch + exit:_Error -> ok + end, + PState. + +%%---------------------------------------------------------------------------- + +make_will_msg(#mqtt_frame_connect{ will_flag = false }) -> + undefined; +make_will_msg(#mqtt_frame_connect{ will_retain = Retain, + will_qos = Qos, + will_topic = Topic, + will_msg = Msg }) -> + #mqtt_msg{ retain = Retain, + qos = Qos, + topic = Topic, + dup = false, + payload = Msg }. + +process_login(UserBin, PassBin, ProtoVersion, + #proc_state{ channels = {undefined, undefined}, + socket = Sock }) -> + {VHost, UsernameBin} = get_vhost_username(UserBin), + case amqp_connection:start(#amqp_params_direct{ + username = UsernameBin, + password = PassBin, + virtual_host = VHost, + adapter_info = adapter_info(Sock, ProtoVersion)}) of + {ok, Connection} -> + case rabbit_access_control:check_user_loopback(UsernameBin, Sock) of + ok -> {?CONNACK_ACCEPT, Connection}; + not_allowed -> amqp_connection:close(Connection), + rabbit_log:warning( + "MQTT login failed for ~p access_refused " + "(access must be from localhost)~n", + [binary_to_list(UsernameBin)]), + ?CONNACK_AUTH + end; + {error, {auth_failure, Explanation}} -> + rabbit_log:error("MQTT login failed for ~p auth_failure: ~s~n", + [binary_to_list(UserBin), Explanation]), + ?CONNACK_CREDENTIALS; + {error, access_refused} -> + rabbit_log:warning("MQTT login failed for ~p access_refused " + "(vhost access not allowed)~n", + [binary_to_list(UserBin)]), + ?CONNACK_AUTH + end. + +get_vhost_username(UserBin) -> + %% split at the last colon, disallowing colons in username + case re:split(UserBin, ":(?!.*?:)") of + [Vhost, UserName] -> {Vhost, UserName}; + [UserBin] -> {rabbit_mqtt_util:env(vhost), UserBin} + end. + +creds(User, Pass, SSLLoginName) -> + DefaultUser = rabbit_mqtt_util:env(default_user), + DefaultPass = rabbit_mqtt_util:env(default_pass), + {ok, Anon} = application:get_env(?APP, allow_anonymous), + {ok, TLSAuth} = application:get_env(?APP, ssl_cert_login), + U = case {User =/= undefined, is_binary(DefaultUser), + Anon =:= true, (TLSAuth andalso SSLLoginName =/= none)} of + {true, _, _, _} -> list_to_binary(User); + {false, _, _, true} -> SSLLoginName; + {false, true, true, false} -> DefaultUser; + _ -> nocreds + end, + case U of + nocreds -> + nocreds; + _ -> + case {Pass =/= undefined, is_binary(DefaultPass), Anon =:= true, SSLLoginName == U} of + {true, _, _, _} -> {U, list_to_binary(Pass)}; + {false, _, _, _} -> {U, none}; + {false, true, true, _} -> {U, DefaultPass}; + _ -> {U, none} + end + end. + +supported_subs_qos(?QOS_0) -> ?QOS_0; +supported_subs_qos(?QOS_1) -> ?QOS_1; +supported_subs_qos(?QOS_2) -> ?QOS_1. + +delivery_mode(?QOS_0) -> 1; +delivery_mode(?QOS_1) -> 2. + +%% different qos subscriptions are received in different queues +%% with appropriate durability and timeout arguments +%% this will lead to duplicate messages for overlapping subscriptions +%% with different qos values - todo: prevent duplicates +ensure_queue(Qos, #proc_state{ channels = {Channel, _}, + client_id = ClientId, + clean_sess = CleanSess, + consumer_tags = {TagQ0, TagQ1} = Tags} = PState) -> + {QueueQ0, QueueQ1} = rabbit_mqtt_util:subcription_queue_name(ClientId), + Qos1Args = case {rabbit_mqtt_util:env(subscription_ttl), CleanSess} of + {undefined, _} -> + []; + {Ms, false} when is_integer(Ms) -> + [{<<"x-expires">>, long, Ms}]; + _ -> + [] + end, + QueueSetup = + case {TagQ0, TagQ1, Qos} of + {undefined, _, ?QOS_0} -> + {QueueQ0, + #'queue.declare'{ queue = QueueQ0, + durable = false, + auto_delete = true }, + #'basic.consume'{ queue = QueueQ0, + no_ack = true }}; + {_, undefined, ?QOS_1} -> + {QueueQ1, + #'queue.declare'{ queue = QueueQ1, + durable = true, + auto_delete = false, + arguments = Qos1Args }, + #'basic.consume'{ queue = QueueQ1, + no_ack = false }}; + {_, _, ?QOS_0} -> + {exists, QueueQ0}; + {_, _, ?QOS_1} -> + {exists, QueueQ1} + end, + case QueueSetup of + {Queue, Declare, Consume} -> + #'queue.declare_ok'{} = amqp_channel:call(Channel, Declare), + #'basic.consume_ok'{ consumer_tag = Tag } = + amqp_channel:call(Channel, Consume), + {Queue, PState #proc_state{ consumer_tags = setelement(Qos+1, Tags, Tag) }}; + {exists, Q} -> + {Q, PState} + end. + +send_will(PState = #proc_state{ will_msg = WillMsg }) -> + amqp_pub(WillMsg, PState). + +amqp_pub(undefined, PState) -> + PState; + +%% set up a qos1 publishing channel if necessary +%% this channel will only be used for publishing, not consuming +amqp_pub(Msg = #mqtt_msg{ qos = ?QOS_1 }, + PState = #proc_state{ channels = {ChQos0, undefined}, + awaiting_seqno = undefined, + connection = Conn }) -> + {ok, Channel} = amqp_connection:open_channel(Conn), + #'confirm.select_ok'{} = amqp_channel:call(Channel, #'confirm.select'{}), + amqp_channel:register_confirm_handler(Channel, self()), + amqp_pub(Msg, PState #proc_state{ channels = {ChQos0, Channel}, + awaiting_seqno = 1 }); + +amqp_pub(#mqtt_msg{ qos = Qos, + topic = Topic, + dup = Dup, + message_id = MessageId, + payload = Payload }, + PState = #proc_state{ channels = {ChQos0, ChQos1}, + exchange = Exchange, + unacked_pubs = UnackedPubs, + awaiting_seqno = SeqNo }) -> + Method = #'basic.publish'{ exchange = Exchange, + routing_key = + rabbit_mqtt_util:mqtt2amqp(Topic)}, + Headers = [{<<"x-mqtt-publish-qos">>, byte, Qos}, + {<<"x-mqtt-dup">>, bool, Dup}], + Msg = #amqp_msg{ props = #'P_basic'{ headers = Headers, + delivery_mode = delivery_mode(Qos)}, + payload = Payload }, + {UnackedPubs1, Ch, SeqNo1} = + case Qos =:= ?QOS_1 andalso MessageId =/= undefined of + true -> {gb_trees:enter(SeqNo, MessageId, UnackedPubs), ChQos1, + SeqNo + 1}; + false -> {UnackedPubs, ChQos0, SeqNo} + end, + amqp_channel:cast_flow(Ch, Method, Msg), + PState #proc_state{ unacked_pubs = UnackedPubs1, + awaiting_seqno = SeqNo1 }. + +adapter_info(Sock, ProtoVer) -> + amqp_connection:socket_adapter_info( + Sock, {'MQTT', human_readable_mqtt_version(ProtoVer)}). + +human_readable_mqtt_version(3) -> + "3.1.0"; +human_readable_mqtt_version(4) -> + "3.1.1"; +human_readable_mqtt_version(_) -> + "N/A". + +send_client(Frame, #proc_state{ socket = Sock }) -> + %rabbit_log:info("MQTT sending frame ~p ~n", [Frame]), + rabbit_net:port_command(Sock, rabbit_mqtt_frame:serialise(Frame)). + +close_connection(PState = #proc_state{ connection = undefined }) -> + PState; +close_connection(PState = #proc_state{ connection = Connection, + client_id = ClientId }) -> + % todo: maybe clean session + case ClientId of + undefined -> ok; + _ -> ok = rabbit_mqtt_collector:unregister(ClientId, self()) + end, + %% ignore noproc or other exceptions to avoid debris + catch amqp_connection:close(Connection), + PState #proc_state{ channels = {undefined, undefined}, + connection = undefined }. diff --git a/rabbitmq-server/plugins-src/rabbitmq-mqtt/src/rabbit_mqtt_reader.erl b/rabbitmq-server/plugins-src/rabbitmq-mqtt/src/rabbit_mqtt_reader.erl new file mode 100644 index 0000000..30cf032 --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-mqtt/src/rabbit_mqtt_reader.erl @@ -0,0 +1,299 @@ +%% The contents of this file are subject to the Mozilla Public License +%% Version 1.1 (the "License"); you may not use this file except in +%% compliance with the License. You may obtain a copy of the License +%% at http://www.mozilla.org/MPL/ +%% +%% Software distributed under the License is distributed on an "AS IS" +%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See +%% the License for the specific language governing rights and +%% limitations under the License. +%% +%% The Original Code is RabbitMQ. +%% +%% The Initial Developer of the Original Code is GoPivotal, Inc. +%% Copyright (c) 2007-2015 Pivotal Software, Inc. All rights reserved. +%% + +-module(rabbit_mqtt_reader). +-behaviour(gen_server2). + +-export([start_link/0]). +-export([init/1, handle_call/3, handle_cast/2, handle_info/2, + code_change/3, terminate/2]). + +-export([conserve_resources/3, start_keepalive/2]). + +-include_lib("amqp_client/include/amqp_client.hrl"). +-include("rabbit_mqtt.hrl"). + +%%---------------------------------------------------------------------------- + +start_link() -> + gen_server2:start_link(?MODULE, [], []). + +conserve_resources(Pid, _, Conserve) -> + Pid ! {conserve_resources, Conserve}, + ok. + +%%---------------------------------------------------------------------------- + +init([]) -> + {ok, undefined, hibernate, {backoff, 1000, 1000, 10000}}. + +handle_call(Msg, From, State) -> + {stop, {mqtt_unexpected_call, Msg, From}, State}. + +handle_cast({go, Sock0, SockTransform, KeepaliveSup}, undefined) -> + process_flag(trap_exit, true), + case rabbit_net:connection_string(Sock0, inbound) of + {ok, ConnStr} -> + log(info, "accepting MQTT connection ~p (~s)~n", [self(), ConnStr]), + case SockTransform(Sock0) of + {ok, Sock} -> + rabbit_alarm:register( + self(), {?MODULE, conserve_resources, []}), + ProcessorState = rabbit_mqtt_processor:initial_state(Sock,ssl_login_name(Sock)), + {noreply, + control_throttle( + #state{socket = Sock, + conn_name = ConnStr, + await_recv = false, + connection_state = running, + keepalive = {none, none}, + keepalive_sup = KeepaliveSup, + conserve = false, + parse_state = rabbit_mqtt_frame:initial_state(), + proc_state = ProcessorState }), + hibernate}; + {error, Reason} -> + rabbit_net:fast_close(Sock0), + {stop, {network_error, Reason, ConnStr}, undefined} + end; + {network_error, Reason} -> + rabbit_net:fast_close(Sock0), + {stop, {shutdown, Reason}, undefined}; + {error, enotconn} -> + rabbit_net:fast_close(Sock0), + {stop, shutdown, undefined}; + {error, Reason} -> + rabbit_net:fast_close(Sock0), + {stop, {network_error, Reason}, undefined} + end; + +handle_cast(duplicate_id, + State = #state{ proc_state = PState, + conn_name = ConnName }) -> + log(warning, "MQTT disconnecting duplicate client id ~p (~p)~n", + [rabbit_mqtt_processor:info(client_id, PState), ConnName]), + {stop, {shutdown, duplicate_id}, State}; + +handle_cast(Msg, State) -> + {stop, {mqtt_unexpected_cast, Msg}, State}. + +handle_info({#'basic.deliver'{}, #amqp_msg{}, _DeliveryCtx} = Delivery, + State = #state{ proc_state = ProcState }) -> + callback_reply(State, rabbit_mqtt_processor:amqp_callback(Delivery, + ProcState)); + +handle_info(#'basic.ack'{} = Ack, State = #state{ proc_state = ProcState }) -> + callback_reply(State, rabbit_mqtt_processor:amqp_callback(Ack, ProcState)); + +handle_info(#'basic.consume_ok'{}, State) -> + {noreply, State, hibernate}; + +handle_info(#'basic.cancel'{}, State) -> + {stop, {shutdown, subscription_cancelled}, State}; + +handle_info({'EXIT', _Conn, Reason}, State) -> + {stop, {connection_died, Reason}, State}; + +handle_info({inet_reply, _Ref, ok}, State) -> + {noreply, State, hibernate}; + +handle_info({inet_async, Sock, _Ref, {ok, Data}}, + State = #state{ socket = Sock }) -> + process_received_bytes( + Data, control_throttle(State #state{ await_recv = false })); + +handle_info({inet_async, _Sock, _Ref, {error, Reason}}, State = #state {}) -> + network_error(Reason, State); + +handle_info({inet_reply, _Sock, {error, Reason}}, State = #state {}) -> + network_error(Reason, State); + +handle_info({conserve_resources, Conserve}, State) -> + {noreply, control_throttle(State #state{ conserve = Conserve }), hibernate}; + +handle_info({bump_credit, Msg}, State) -> + credit_flow:handle_bump_msg(Msg), + {noreply, control_throttle(State), hibernate}; + +handle_info({start_keepalives, Keepalive}, + State = #state { keepalive_sup = KeepaliveSup, socket = Sock }) -> + %% Only the client has the responsibility for sending keepalives + SendFun = fun() -> ok end, + Parent = self(), + ReceiveFun = fun() -> Parent ! keepalive_timeout end, + Heartbeater = rabbit_heartbeat:start( + KeepaliveSup, Sock, 0, SendFun, Keepalive, ReceiveFun), + {noreply, State #state { keepalive = Heartbeater }}; + +handle_info(keepalive_timeout, State = #state {conn_name = ConnStr, + proc_state = PState}) -> + log(error, "closing MQTT connection ~p (keepalive timeout)~n", [ConnStr]), + send_will_and_terminate(PState, {shutdown, keepalive_timeout}, State); + +handle_info(Msg, State) -> + {stop, {mqtt_unexpected_msg, Msg}, State}. + +terminate({network_error, {ssl_upgrade_error, closed}, ConnStr}, _State) -> + log(error, "MQTT detected TLS upgrade error on ~s: connection closed~n", + [ConnStr]); + +terminate({network_error, + {ssl_upgrade_error, + {tls_alert, "handshake failure"}}, ConnStr}, _State) -> + log(error, "MQTT detected TLS upgrade error on ~s: handshake failure~n", + [ConnStr]); + +terminate({network_error, + {ssl_upgrade_error, + {tls_alert, "unknown ca"}}, ConnStr}, _State) -> + log(error, "MQTT detected TLS certificate verification error on ~s: alert 'unknown CA'~n", + [ConnStr]); + +terminate({network_error, + {ssl_upgrade_error, + {tls_alert, Alert}}, ConnStr}, _State) -> + log(error, "MQTT detected TLS upgrade error on ~s: alert ~s~n", + [ConnStr, Alert]); + +terminate({network_error, {ssl_upgrade_error, Reason}, ConnStr}, _State) -> + log(error, "MQTT detected TLS upgrade error on ~s: ~p~n", + [ConnStr, Reason]); + +terminate({network_error, Reason, ConnStr}, _State) -> + log(error, "MQTT detected network error on ~s: ~p~n", + [ConnStr, Reason]); + +terminate({network_error, Reason}, _State) -> + log(error, "MQTT detected network error: ~p~n", [Reason]); + +terminate(normal, #state{proc_state = ProcState, + conn_name = ConnName}) -> + rabbit_mqtt_processor:close_connection(ProcState), + log(info, "closing MQTT connection ~p (~s)~n", [self(), ConnName]), + ok; + +terminate(_Reason, #state{proc_state = ProcState}) -> + rabbit_mqtt_processor:close_connection(ProcState), + ok. + +code_change(_OldVsn, State, _Extra) -> + {ok, State}. + +ssl_login_name(Sock) -> + case rabbit_net:peercert(Sock) of + {ok, C} -> case rabbit_ssl:peer_cert_auth_name(C) of + unsafe -> none; + not_found -> none; + Name -> Name + end; + {error, no_peercert} -> none; + nossl -> none + end. + +%%---------------------------------------------------------------------------- + +process_received_bytes(<<>>, State) -> + {noreply, State, hibernate}; +process_received_bytes(Bytes, + State = #state{ parse_state = ParseState, + proc_state = ProcState, + conn_name = ConnStr }) -> + case rabbit_mqtt_frame:parse(Bytes, ParseState) of + {more, ParseState1} -> + {noreply, + control_throttle( State #state{ parse_state = ParseState1 }), + hibernate}; + {ok, Frame, Rest} -> + case rabbit_mqtt_processor:process_frame(Frame, ProcState) of + {ok, ProcState1} -> + PS = rabbit_mqtt_frame:initial_state(), + process_received_bytes( + Rest, + State #state{ parse_state = PS, + proc_state = ProcState1 }); + {error, Reason, ProcState1} -> + log(info, "MQTT protocol error ~p for connection ~p~n", + [Reason, ConnStr]), + {stop, {shutdown, Reason}, pstate(State, ProcState1)}; + {error, Error} -> + log(error, "MQTT detected framing error '~p' for connection ~p~n", + [Error, ConnStr]), + {stop, {shutdown, Error}, State}; + {stop, ProcState1} -> + {stop, normal, pstate(State, ProcState1)} + end; + {error, Error} -> + log(error, "MQTT detected framing error '~p' for connection ~p~n", + [ConnStr, Error]), + {stop, {shutdown, Error}, State} + end. + +callback_reply(State, {ok, ProcState}) -> + {noreply, pstate(State, ProcState), hibernate}; +callback_reply(State, {error, Reason, ProcState}) -> + {stop, Reason, pstate(State, ProcState)}. + +start_keepalive(_, 0 ) -> ok; +start_keepalive(Pid, Keepalive) -> Pid ! {start_keepalives, Keepalive}. + +pstate(State = #state {}, PState = #proc_state{}) -> + State #state{ proc_state = PState }. + +%%---------------------------------------------------------------------------- + +log(Level, Fmt, Args) -> rabbit_log:log(connection, Level, Fmt, Args). + +send_will_and_terminate(PState, State) -> + send_will_and_terminate(PState, {shutdown, conn_closed}, State). + +send_will_and_terminate(PState, Reason, State) -> + rabbit_mqtt_processor:send_will(PState), + % todo: flush channel after publish + {stop, Reason, State}. + +network_error(closed, + State = #state{ conn_name = ConnStr, + proc_state = PState }) -> + log(info, "MQTT detected network error for ~p: peer closed TCP connection~n", + [ConnStr]), + send_will_and_terminate(PState, State); + +network_error(Reason, + State = #state{ conn_name = ConnStr, + proc_state = PState }) -> + log(info, "MQTT detected network error for ~p: ~p~n", [ConnStr, Reason]), + send_will_and_terminate(PState, State). + +run_socket(State = #state{ connection_state = blocked }) -> + State; +run_socket(State = #state{ await_recv = true }) -> + State; +run_socket(State = #state{ socket = Sock }) -> + rabbit_net:async_recv(Sock, 0, infinity), + State#state{ await_recv = true }. + +control_throttle(State = #state{ connection_state = Flow, + conserve = Conserve }) -> + case {Flow, Conserve orelse credit_flow:blocked()} of + {running, true} -> ok = rabbit_heartbeat:pause_monitor( + State#state.keepalive), + State #state{ connection_state = blocked }; + {blocked, false} -> ok = rabbit_heartbeat:resume_monitor( + State#state.keepalive), + run_socket(State #state{ + connection_state = running }); + {_, _} -> run_socket(State) + end. diff --git a/rabbitmq-server/plugins-src/rabbitmq-mqtt/src/rabbit_mqtt_sup.erl b/rabbitmq-server/plugins-src/rabbitmq-mqtt/src/rabbit_mqtt_sup.erl new file mode 100644 index 0000000..38edcf9 --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-mqtt/src/rabbit_mqtt_sup.erl @@ -0,0 +1,86 @@ +%% The contents of this file are subject to the Mozilla Public License +%% Version 1.1 (the "License"); you may not use this file except in +%% compliance with the License. You may obtain a copy of the License +%% at http://www.mozilla.org/MPL/ +%% +%% Software distributed under the License is distributed on an "AS IS" +%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See +%% the License for the specific language governing rights and +%% limitations under the License. +%% +%% The Original Code is RabbitMQ. +%% +%% The Initial Developer of the Original Code is GoPivotal, Inc. +%% Copyright (c) 2007-2015 Pivotal Software, Inc. All rights reserved. +%% + +-module(rabbit_mqtt_sup). +-behaviour(supervisor2). + +-define(MAX_WAIT, 16#ffffffff). + +-export([start_link/2, init/1]). + +-export([start_client/1, start_ssl_client/2]). + +start_link(Listeners, []) -> + supervisor2:start_link({local, ?MODULE}, ?MODULE, [Listeners]). + +init([{Listeners, SslListeners0}]) -> + {ok, SocketOpts} = application:get_env(rabbitmq_mqtt, tcp_listen_options), + {SslOpts, SslListeners} + = case SslListeners0 of + [] -> {none, []}; + _ -> {rabbit_networking:ensure_ssl(), + case rabbit_networking:poodle_check('MQTT') of + ok -> SslListeners0; + danger -> [] + end} + end, + {ok, {{one_for_all, 10, 10}, + [{collector, + {rabbit_mqtt_collector, start_link, []}, + transient, ?MAX_WAIT, worker, [rabbit_mqtt_collector]}, + {rabbit_mqtt_client_sup, + {rabbit_client_sup, start_link, [{local, rabbit_mqtt_client_sup}, + {rabbit_mqtt_connection_sup, start_link, []}]}, + transient, infinity, supervisor, [rabbit_client_sup]} | + listener_specs(fun tcp_listener_spec/1, + [SocketOpts], Listeners) ++ + listener_specs(fun ssl_listener_spec/1, + [SocketOpts, SslOpts], SslListeners)]}}. + +listener_specs(Fun, Args, Listeners) -> + [Fun([Address | Args]) || + Listener <- Listeners, + Address <- rabbit_networking:tcp_listener_addresses(Listener)]. + +tcp_listener_spec([Address, SocketOpts]) -> + rabbit_networking:tcp_listener_spec( + rabbit_mqtt_listener_sup, Address, SocketOpts, + mqtt, "MQTT TCP Listener", + {?MODULE, start_client, []}). + +ssl_listener_spec([Address, SocketOpts, SslOpts]) -> + rabbit_networking:tcp_listener_spec( + rabbit_mqtt_listener_sup, Address, SocketOpts, + 'mqtt/ssl', "MQTT SSL Listener", + {?MODULE, start_ssl_client, [SslOpts]}). + +start_client(Sock, SockTransform) -> + {ok, _, {KeepaliveSup, Reader}} = + supervisor2:start_child(rabbit_mqtt_client_sup, []), + ok = rabbit_net:controlling_process(Sock, Reader), + ok = gen_server2:cast(Reader, {go, Sock, SockTransform, KeepaliveSup}), + + %% see comment in rabbit_networking:start_client/2 + gen_event:which_handlers(error_logger), + Reader. + +start_client(Sock) -> + start_client(Sock, fun (S) -> {ok, S} end). + +start_ssl_client(SslOpts, Sock) -> + Transform = rabbit_networking:ssl_transform_fun(SslOpts), + start_client(Sock, Transform). + diff --git a/rabbitmq-server/plugins-src/rabbitmq-mqtt/src/rabbit_mqtt_util.erl b/rabbitmq-server/plugins-src/rabbitmq-mqtt/src/rabbit_mqtt_util.erl new file mode 100644 index 0000000..336e2a4 --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-mqtt/src/rabbit_mqtt_util.erl @@ -0,0 +1,62 @@ +%% The contents of this file are subject to the Mozilla Public License +%% Version 1.1 (the "License"); you may not use this file except in +%% compliance with the License. You may obtain a copy of the License +%% at http://www.mozilla.org/MPL/ +%% +%% Software distributed under the License is distributed on an "AS IS" +%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See +%% the License for the specific language governing rights and +%% limitations under the License. +%% +%% The Original Code is RabbitMQ. +%% +%% The Initial Developer of the Original Code is GoPivotal, Inc. +%% Copyright (c) 2007-2015 Pivotal Software, Inc. All rights reserved. +%% + +-module(rabbit_mqtt_util). + +-include("rabbit_mqtt.hrl"). + +-compile(export_all). + +subcription_queue_name(ClientId) -> + Base = "mqtt-subscription-" ++ ClientId ++ "qos", + {list_to_binary(Base ++ "0"), list_to_binary(Base ++ "1")}. + +%% amqp mqtt descr +%% * + match one topic level +%% # # match multiple topic levels +%% . / topic level separator +mqtt2amqp(Topic) -> + erlang:iolist_to_binary( + re:replace(re:replace(Topic, "/", ".", [global]), + "[\+]", "*", [global])). + +amqp2mqtt(Topic) -> + erlang:iolist_to_binary( + re:replace(re:replace(Topic, "[\*]", "+", [global]), + "[\.]", "/", [global])). + +gen_client_id() -> + lists:nthtail(1, rabbit_guid:string(rabbit_guid:gen_secure(), [])). + +env(Key) -> + case application:get_env(rabbitmq_mqtt, Key) of + {ok, Val} -> coerce_env_value(Key, Val); + undefined -> undefined + end. + +coerce_env_value(default_pass, Val) -> to_binary(Val); +coerce_env_value(default_user, Val) -> to_binary(Val); +coerce_env_value(exchange, Val) -> to_binary(Val); +coerce_env_value(vhost, Val) -> to_binary(Val); +coerce_env_value(_, Val) -> Val. + +to_binary(Val) when is_list(Val) -> list_to_binary(Val); +to_binary(Val) -> Val. + +table_lookup(undefined, _Key) -> + undefined; +table_lookup(Table, Key) -> + rabbit_misc:table_lookup(Table, Key). diff --git a/rabbitmq-server/plugins-src/rabbitmq-mqtt/src/rabbitmq_mqtt.app.src b/rabbitmq-server/plugins-src/rabbitmq-mqtt/src/rabbitmq_mqtt.app.src new file mode 100644 index 0000000..6b60610 --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-mqtt/src/rabbitmq_mqtt.app.src @@ -0,0 +1,22 @@ +{application, rabbitmq_mqtt, + [{description, "RabbitMQ MQTT Adapter"}, + {vsn, "%%VSN%%"}, + {modules, []}, + {registered, []}, + {mod, {rabbit_mqtt, []}}, + {env, [{default_user, <<"guest">>}, + {default_pass, <<"guest">>}, + {ssl_cert_login,false}, + {allow_anonymous, true}, + {vhost, <<"/">>}, + {exchange, <<"amq.topic">>}, + {subscription_ttl, 1800000}, % 30 min + {prefetch, 10}, + {ssl_listeners, []}, + {tcp_listeners, [1883]}, + {tcp_listen_options, [binary, + {packet, raw}, + {reuseaddr, true}, + {backlog, 128}, + {nodelay, true}]}]}, + {applications, [kernel, stdlib, rabbit, amqp_client]}]}. diff --git a/rabbitmq-server/plugins-src/rabbitmq-mqtt/test/Makefile b/rabbitmq-server/plugins-src/rabbitmq-mqtt/test/Makefile new file mode 100644 index 0000000..190e740 --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-mqtt/test/Makefile @@ -0,0 +1,42 @@ +UPSTREAM_GIT=https://git.eclipse.org/gitroot/paho/org.eclipse.paho.mqtt.java.git +REVISION=00b5b2f99ae8410b7d96d106e080a092c5f92546 + +JC=javac + +TEST_SRC=src +CHECKOUT_DIR=test_client +PAHO_JAR_NAME=org.eclipse.paho.client.mqttv3.jar +PAHO_JAR=$(CHECKOUT_DIR)/org.eclipse.paho.client.mqttv3/$(PAHO_JAR_NAME) +JUNIT_JAR=../lib/junit.jar +JAVA_AMQP_DIR=../../rabbitmq-java-client/ +JAVA_AMQP_CLASSES=$(JAVA_AMQP_DIR)build/classes/ + +ALL_CLASSES:=$(foreach f,$(shell find src -name '*.class'),'$(f)') +CP:=$(PAHO_JAR):$(JUNIT_JAR):$(TEST_SRC):$(JAVA_AMQP_CLASSES) + +HOSTNAME:=$(shell hostname) + +define class_from_path +$(subst .class,,$(subst src.,,$(subst /,.,$(1)))) +endef + +.PHONY: test +test: build_java_amqp + ant test -Dhostname=$(HOSTNAME) + +clean: + ant clean + rm -rf test_client + + +distclean: clean + rm -rf $(CHECKOUT_DIR) + +$(CHECKOUT_DIR): + git clone $(UPSTREAM_GIT) $@ + (cd $@ && git checkout $(REVISION)) || rm -rf $@ + + +.PHONY: build_java_amqp +build_java_amqp: $(CHECKOUT_DIR) + $(MAKE) -C $(JAVA_AMQP_DIR) jar diff --git a/rabbitmq-server/plugins-src/rabbitmq-mqtt/test/build.properties b/rabbitmq-server/plugins-src/rabbitmq-mqtt/test/build.properties new file mode 100644 index 0000000..25da28c --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-mqtt/test/build.properties @@ -0,0 +1,17 @@ +build.out=build +test.resources=${build.out}/test/resources +javac.debug=true +test.javac.out=${build.out}/test/classes +test.resources=${build.out}/test/resources +test.src.home=src +certs.dir=certs +certs.password=test +server.keystore=${test.resources}/server.jks +server.cert=${certs.dir}/server/cert.pem +ca.cert=${certs.dir}/testca/cacert.pem +server.keystore.phrase=bunnyhop + +client.keystore=${test.resources}/client.jks +client.keystore.phrase=bunnychow +client.srckeystore=${certs.dir}/client/keycert.p12 +client.srckeystore.password=bunnychow diff --git a/rabbitmq-server/plugins-src/rabbitmq-mqtt/test/build.xml b/rabbitmq-server/plugins-src/rabbitmq-mqtt/test/build.xml new file mode 100644 index 0000000..1f80b16 --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-mqtt/test/build.xml @@ -0,0 +1,150 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/rabbitmq-server/plugins-src/rabbitmq-mqtt/test/lib/junit.jar b/rabbitmq-server/plugins-src/rabbitmq-mqtt/test/lib/junit.jar new file mode 100644 index 0000000..674d71e Binary files /dev/null and b/rabbitmq-server/plugins-src/rabbitmq-mqtt/test/lib/junit.jar differ diff --git a/rabbitmq-server/plugins-src/rabbitmq-mqtt/test/rabbit-test.sh b/rabbitmq-server/plugins-src/rabbitmq-mqtt/test/rabbit-test.sh new file mode 100755 index 0000000..b0c6585 --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-mqtt/test/rabbit-test.sh @@ -0,0 +1,6 @@ +#!/bin/sh +CTL=$1 +USER="O=client,CN=$(hostname)" + +$CTL add_user "$USER" '' +$CTL set_permissions -p / "$USER" ".*" ".*" ".*" diff --git a/rabbitmq-server/plugins-src/rabbitmq-mqtt/test/setup-rabbit-test.sh b/rabbitmq-server/plugins-src/rabbitmq-mqtt/test/setup-rabbit-test.sh new file mode 100755 index 0000000..9b2708a --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-mqtt/test/setup-rabbit-test.sh @@ -0,0 +1,2 @@ +#!/bin/sh -e +sh -e `dirname $0`/rabbit-test.sh "`dirname $0`/../../rabbitmq-server/scripts/rabbitmqctl -n rabbit-test" diff --git a/rabbitmq-server/plugins-src/rabbitmq-mqtt/test/src/com/rabbitmq/mqtt/test/MqttTest.java b/rabbitmq-server/plugins-src/rabbitmq-mqtt/test/src/com/rabbitmq/mqtt/test/MqttTest.java new file mode 100644 index 0000000..ca31b5c --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-mqtt/test/src/com/rabbitmq/mqtt/test/MqttTest.java @@ -0,0 +1,431 @@ +// The contents of this file are subject to the Mozilla Public License +// Version 1.1 (the "License"); you may not use this file except in +// compliance with the License. You may obtain a copy of the License +// at http://www.mozilla.org/MPL/ +// +// Software distributed under the License is distributed on an "AS IS" +// basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See +// the License for the specific language governing rights and +// limitations under the License. +// +// The Original Code is RabbitMQ. +// +// The Initial Developer of the Original Code is GoPivotal, Inc. +// Copyright (c) 2007-2015 Pivotal Software, Inc. All rights reserved. +// + +package com.rabbitmq.mqtt.test; + +import com.rabbitmq.client.*; +import junit.framework.Assert; +import junit.framework.TestCase; +import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken; +import org.eclipse.paho.client.mqttv3.MqttCallback; +import org.eclipse.paho.client.mqttv3.MqttClient; +import org.eclipse.paho.client.mqttv3.MqttConnectOptions; +import org.eclipse.paho.client.mqttv3.MqttDeliveryToken; +import org.eclipse.paho.client.mqttv3.MqttException; +import org.eclipse.paho.client.mqttv3.MqttMessage; +import org.eclipse.paho.client.mqttv3.MqttTopic; +import org.eclipse.paho.client.mqttv3.internal.NetworkModule; +import org.eclipse.paho.client.mqttv3.internal.TCPNetworkModule; +import org.eclipse.paho.client.mqttv3.internal.wire.MqttInputStream; +import org.eclipse.paho.client.mqttv3.internal.wire.MqttOutputStream; +import org.eclipse.paho.client.mqttv3.internal.wire.MqttPingReq; + +import javax.net.SocketFactory; +import java.io.IOException; +import java.net.InetAddress; +import java.net.Socket; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.concurrent.TimeoutException; + +/*** + * MQTT v3.1 tests + * TODO: synchronise access to variables + */ + +public class MqttTest extends TestCase implements MqttCallback { + + private final String host = "localhost"; + private final int port = 1883; + private final String brokerUrl = "tcp://" + host + ":" + port; + private String clientId; + private String clientId2; + private MqttClient client; + private MqttClient client2; + private MqttConnectOptions conOpt; + private ArrayList receivedMessages; + + private final byte[] payload = "payload".getBytes(); + private final String topic = "test-topic"; + private int testDelay = 2000; + private long lastReceipt; + private boolean expectConnectionFailure; + + private ConnectionFactory connectionFactory; + private Connection conn; + private Channel ch; + + // override 10s limit + private class MyConnOpts extends MqttConnectOptions { + private int keepAliveInterval = 60; + @Override + public void setKeepAliveInterval(int keepAliveInterval) { + this.keepAliveInterval = keepAliveInterval; + } + @Override + public int getKeepAliveInterval() { + return keepAliveInterval; + } + } + + @Override + public void setUp() throws MqttException { + clientId = getClass().getSimpleName() + ((int) (10000*Math.random())); + clientId2 = clientId + "-2"; + client = new MqttClient(brokerUrl, clientId, null); + client2 = new MqttClient(brokerUrl, clientId2, null); + conOpt = new MyConnOpts(); + setConOpts(conOpt); + receivedMessages = new ArrayList(); + expectConnectionFailure = false; + } + + @Override + public void tearDown() throws MqttException { + // clean any sticky sessions + setConOpts(conOpt); + client = new MqttClient(brokerUrl, clientId, null); + try { + client.connect(conOpt); + client.disconnect(); + } catch (Exception ignored) {} + + client2 = new MqttClient(brokerUrl, clientId2, null); + try { + client2.connect(conOpt); + client2.disconnect(); + } catch (Exception ignored) {} + } + + private void setUpAmqp() throws IOException, TimeoutException { + connectionFactory = new ConnectionFactory(); + connectionFactory.setHost(host); + conn = connectionFactory.newConnection(); + ch = conn.createChannel(); + } + + private void tearDownAmqp() throws IOException { + conn.close(); + } + + private void setConOpts(MqttConnectOptions conOpts) { + // provide authentication if the broker needs it + // conOpts.setUserName("guest"); + // conOpts.setPassword("guest".toCharArray()); + conOpts.setCleanSession(true); + conOpts.setKeepAliveInterval(60); + } + + public void testConnectFirst() throws MqttException, IOException, InterruptedException { + NetworkModule networkModule = new TCPNetworkModule(SocketFactory.getDefault(), host, port, ""); + networkModule.start(); + MqttInputStream mqttIn = new MqttInputStream (networkModule.getInputStream()); + MqttOutputStream mqttOut = new MqttOutputStream(networkModule.getOutputStream()); + try { + mqttOut.write(new MqttPingReq()); + mqttOut.flush(); + mqttIn.readMqttWireMessage(); + fail("Error expected if CONNECT is not first packet"); + } catch (IOException ignored) {} + } + + public void testInvalidUser() throws MqttException { + conOpt.setUserName("invalid-user"); + try { + client.connect(conOpt); + fail("Authentication failure expected"); + } catch (MqttException ex) { + Assert.assertEquals(MqttException.REASON_CODE_FAILED_AUTHENTICATION, ex.getReasonCode()); + } + } + + public void testInvalidPassword() throws MqttException { + conOpt.setUserName("invalid-user"); + conOpt.setPassword("invalid-password".toCharArray()); + try { + client.connect(conOpt); + fail("Authentication failure expected"); + } catch (MqttException ex) { + Assert.assertEquals(MqttException.REASON_CODE_FAILED_AUTHENTICATION, ex.getReasonCode()); + } + } + + + public void testSubscribeQos0() throws MqttException, InterruptedException { + client.connect(conOpt); + client.setCallback(this); + client.subscribe(topic, 0); + + publish(client, topic, 0, payload); + Thread.sleep(testDelay); + Assert.assertEquals(1, receivedMessages.size()); + Assert.assertEquals(true, Arrays.equals(receivedMessages.get(0).getPayload(), payload)); + Assert.assertEquals(0, receivedMessages.get(0).getQos()); + client.disconnect(); + } + + public void testSubscribeUnsubscribe() throws MqttException, InterruptedException { + client.connect(conOpt); + client.setCallback(this); + client.subscribe(topic, 0); + + publish(client, topic, 1, payload); + Thread.sleep(testDelay); + Assert.assertEquals(1, receivedMessages.size()); + Assert.assertEquals(true, Arrays.equals(receivedMessages.get(0).getPayload(), payload)); + Assert.assertEquals(0, receivedMessages.get(0).getQos()); + + client.unsubscribe(topic); + publish(client, topic, 0, payload); + Thread.sleep(testDelay); + Assert.assertEquals(1, receivedMessages.size()); + client.disconnect(); + } + + public void testSubscribeQos1() throws MqttException, InterruptedException { + client.connect(conOpt); + client.setCallback(this); + client.subscribe(topic, 1); + + publish(client, topic, 0, payload); + publish(client, topic, 1, payload); + Thread.sleep(testDelay); + + Assert.assertEquals(2, receivedMessages.size()); + MqttMessage msg1 = receivedMessages.get(0); + MqttMessage msg2 = receivedMessages.get(1); + + Assert.assertEquals(true, Arrays.equals(msg1.getPayload(), payload)); + Assert.assertEquals(0, msg1.getQos()); + + Assert.assertEquals(true, Arrays.equals(msg2.getPayload(), payload)); + Assert.assertEquals(1, msg2.getQos()); + + client.disconnect(); + } + + public void testTopics() throws MqttException, InterruptedException { + client.connect(conOpt); + client.setCallback(this); + client.subscribe("/+/mid/#"); + String cases[] = {"/pre/mid2", "/mid", "/a/mid/b/c/d", "/frob/mid"}; + List expected = Arrays.asList("/a/mid/b/c/d", "/frob/mid"); + for(String example : cases){ + publish(client, example, 0, example.getBytes()); + } + Thread.sleep(testDelay); + Assert.assertEquals(expected.size(), receivedMessages.size()); + for (MqttMessage m : receivedMessages){ + expected.contains(new String(m.getPayload())); + } + client.disconnect(); + } + + public void testNonCleanSession() throws MqttException, InterruptedException { + conOpt.setCleanSession(false); + client.connect(conOpt); + client.subscribe(topic, 1); + client.disconnect(); + + client2.connect(conOpt); + publish(client2, topic, 1, payload); + client2.disconnect(); + + client.setCallback(this); + client.connect(conOpt); + + Thread.sleep(testDelay); + Assert.assertEquals(1, receivedMessages.size()); + Assert.assertEquals(true, Arrays.equals(receivedMessages.get(0).getPayload(), payload)); + client.disconnect(); + } + + public void testCleanSession() throws MqttException, InterruptedException { + conOpt.setCleanSession(false); + client.connect(conOpt); + client.subscribe(topic, 1); + client.disconnect(); + + client2.connect(conOpt); + publish(client2, topic, 1, payload); + client2.disconnect(); + + conOpt.setCleanSession(true); + client.connect(conOpt); + client.setCallback(this); + client.subscribe(topic, 1); + + Thread.sleep(testDelay); + Assert.assertEquals(0, receivedMessages.size()); + client.unsubscribe(topic); + client.disconnect(); + } + + public void testMultipleClientIds() throws MqttException, InterruptedException { + client.connect(conOpt); + client2 = new MqttClient(brokerUrl, clientId, null); + client2.connect(conOpt); + Thread.sleep(testDelay); + Assert.assertFalse(client.isConnected()); + client2.disconnect(); + } + + public void testPing() throws MqttException, InterruptedException { + conOpt.setKeepAliveInterval(1); + client.connect(conOpt); + Thread.sleep(3000); + Assert.assertEquals(true, client.isConnected()); + client.disconnect(); + } + + public void testWill() throws MqttException, InterruptedException, IOException { + client2.connect(conOpt); + client2.subscribe(topic); + client2.setCallback(this); + + final SocketFactory factory = SocketFactory.getDefault(); + final ArrayList sockets = new ArrayList(); + SocketFactory testFactory = new SocketFactory() { + public Socket createSocket(String s, int i) throws IOException { + Socket sock = factory.createSocket(s, i); + sockets.add(sock); + return sock; + } + public Socket createSocket(String s, int i, InetAddress a, int i1) throws IOException { + return null; + } + public Socket createSocket(InetAddress a, int i) throws IOException { + return null; + } + public Socket createSocket(InetAddress a, int i, InetAddress a1, int i1) throws IOException { + return null; + } + @Override + public Socket createSocket() throws IOException { + Socket sock = new Socket(); + sockets.add(sock); + return sock; + } + }; + conOpt.setSocketFactory(testFactory); + MqttTopic willTopic = client.getTopic(topic); + conOpt.setWill(willTopic, payload, 0, false); + conOpt.setCleanSession(false); + client.connect(conOpt); + + Assert.assertEquals(1, sockets.size()); + expectConnectionFailure = true; + sockets.get(0).close(); + Thread.sleep(testDelay); + + Assert.assertEquals(1, receivedMessages.size()); + Assert.assertEquals(true, Arrays.equals(receivedMessages.get(0).getPayload(), payload)); + client2.disconnect(); + } + + public void testSubscribeMultiple() throws MqttException { + client.connect(conOpt); + publish(client, "/topic/1", 1, "msq1-qos1".getBytes()); + + client2.connect(conOpt); + client2.setCallback(this); + client2.subscribe("/topic/#"); + client2.subscribe("/topic/#"); + + publish(client, "/topic/2", 0, "msq2-qos0".getBytes()); + publish(client, "/topic/3", 1, "msq3-qos1".getBytes()); + publish(client, topic, 0, "msq4-qos0".getBytes()); + publish(client, topic, 1, "msq4-qos1".getBytes()); + + Assert.assertEquals(2, receivedMessages.size()); + client.disconnect(); + client2.disconnect(); + } + + public void testPublishMultiple() throws MqttException, InterruptedException { + int pubCount = 50; + for (int subQos=0; subQos < 2; subQos++){ + for (int pubQos=0; pubQos < 2; pubQos++){ + client.connect(conOpt); + client.subscribe(topic, subQos); + client.setCallback(this); + long start = System.currentTimeMillis(); + for (int i=0; i receivedMessages; + + private long lastReceipt; + private boolean expectConnectionFailure; + + + private static final String getHost() { + Object host = System.getProperty("hostname"); + assertNotNull(host); + return host.toString(); + } + + // override 10s limit + private class MyConnOpts extends MqttConnectOptions { + private int keepAliveInterval = 60; + + @Override + public void setKeepAliveInterval(int keepAliveInterval) { + this.keepAliveInterval = keepAliveInterval; + } + + @Override + public int getKeepAliveInterval() { + return keepAliveInterval; + } + } + + + @Override + public void setUp() throws MqttException, IOException { + clientId = getClass().getSimpleName() + ((int) (10000 * Math.random())); + clientId2 = clientId + "-2"; + client = new MqttClient(brokerUrl, clientId, null); + client2 = new MqttClient(brokerUrl, clientId2, null); + conOpt = new MyConnOpts(); + conOpt.setSocketFactory(MutualAuth.getSSLContextWithoutCert().getSocketFactory()); + setConOpts(conOpt); + receivedMessages = new ArrayList(); + expectConnectionFailure = false; + } + + @Override + public void tearDown() throws MqttException { + // clean any sticky sessions + setConOpts(conOpt); + client = new MqttClient(brokerUrl, clientId, null); + try { + client.connect(conOpt); + client.disconnect(); + } catch (Exception ignored) { + } + + client2 = new MqttClient(brokerUrl, clientId2, null); + try { + client2.connect(conOpt); + client2.disconnect(); + } catch (Exception ignored) { + } + } + + + private void setConOpts(MqttConnectOptions conOpts) { + // provide authentication if the broker needs it + // conOpts.setUserName("guest"); + // conOpts.setPassword("guest".toCharArray()); + conOpts.setCleanSession(true); + conOpts.setKeepAliveInterval(60); + } + + public void testCertLogin() throws MqttException { + try { + conOpt.setSocketFactory(MutualAuth.getSSLContextWithClientCert().getSocketFactory()); + client.connect(conOpt); + } catch (Exception e) { + e.printStackTrace(); + fail("Exception: " + e.getMessage()); + } + } + + + public void testInvalidUser() throws MqttException { + conOpt.setUserName("invalid-user"); + try { + client.connect(conOpt); + fail("Authentication failure expected"); + } catch (MqttException ex) { + Assert.assertEquals(MqttException.REASON_CODE_FAILED_AUTHENTICATION, ex.getReasonCode()); + } catch (Exception e) { + e.printStackTrace(); + fail("Exception: " + e.getMessage()); + } + } + + public void testInvalidPassword() throws MqttException { + conOpt.setUserName("invalid-user"); + conOpt.setPassword("invalid-password".toCharArray()); + try { + client.connect(conOpt); + fail("Authentication failure expected"); + } catch (MqttException ex) { + Assert.assertEquals(MqttException.REASON_CODE_FAILED_AUTHENTICATION, ex.getReasonCode()); + } catch (Exception e) { + e.printStackTrace(); + fail("Exception: " + e.getMessage()); + } + } + + + public void connectionLost(Throwable cause) { + if (!expectConnectionFailure) + fail("Connection unexpectedly lost"); + } + + public void messageArrived(String topic, MqttMessage message) throws Exception { + lastReceipt = System.currentTimeMillis(); + receivedMessages.add(message); + } + + public void deliveryComplete(IMqttDeliveryToken token) { + } +} diff --git a/rabbitmq-server/plugins-src/rabbitmq-mqtt/test/src/com/rabbitmq/mqtt/test/tls/MutualAuth.java b/rabbitmq-server/plugins-src/rabbitmq-mqtt/test/src/com/rabbitmq/mqtt/test/tls/MutualAuth.java new file mode 100644 index 0000000..a2d5d25 --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-mqtt/test/src/com/rabbitmq/mqtt/test/tls/MutualAuth.java @@ -0,0 +1,84 @@ +package com.rabbitmq.mqtt.test.tls; + +import javax.net.ssl.KeyManagerFactory; +import javax.net.ssl.SSLContext; +import javax.net.ssl.TrustManagerFactory; +import java.io.IOException; +import java.security.KeyStore; +import java.security.KeyStoreException; +import java.security.NoSuchAlgorithmException; +import java.security.cert.CertificateException; +import java.util.Arrays; +import java.util.List; + +public class MutualAuth { + + private MutualAuth() { + + } + + private static String getStringProperty(String propertyName) throws IllegalArgumentException { + Object value = System.getProperty(propertyName); + if (value == null) throw new IllegalArgumentException("Property: " + propertyName + " not found"); + return value.toString(); + } + + private static TrustManagerFactory getServerTrustManagerFactory() throws NoSuchAlgorithmException, CertificateException, IOException, KeyStoreException { + char[] trustPhrase = getStringProperty("server.keystore.passwd").toCharArray(); + MutualAuth dummy = new MutualAuth(); + + // Server TrustStore + KeyStore tks = KeyStore.getInstance("JKS"); + tks.load(dummy.getClass().getResourceAsStream("/server.jks"), trustPhrase); + + TrustManagerFactory tmf = TrustManagerFactory.getInstance("X509"); + tmf.init(tks); + + return tmf; + } + + public static SSLContext getSSLContextWithClientCert() throws IOException { + + char[] clientPhrase = getStringProperty("client.keystore.passwd").toCharArray(); + + MutualAuth dummy = new MutualAuth(); + try { + SSLContext sslContext = getVanillaSSLContext(); + // Client Keystore + KeyStore ks = KeyStore.getInstance("JKS"); + ks.load(dummy.getClass().getResourceAsStream("/client.jks"), clientPhrase); + KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509"); + kmf.init(ks, clientPhrase); + + sslContext.init(kmf.getKeyManagers(), getServerTrustManagerFactory().getTrustManagers(), null); + return sslContext; + } catch (Exception e) { + throw new IOException(e); + } + + } + + private static SSLContext getVanillaSSLContext() throws NoSuchAlgorithmException { + SSLContext result = null; + List xs = Arrays.asList("TLSv1.2", "TLSv1.1", "TLSv1"); + for(String x : xs) { + try { + return SSLContext.getInstance(x); + } catch (NoSuchAlgorithmException nae) { + // keep trying + } + } + throw new NoSuchAlgorithmException("Could not obtain an SSLContext for TLS 1.0-1.2"); + } + + public static SSLContext getSSLContextWithoutCert() throws IOException { + try { + SSLContext sslContext = getVanillaSSLContext(); + sslContext.init(null, getServerTrustManagerFactory().getTrustManagers(), null); + return sslContext; + } catch (Exception e) { + throw new IOException(e); + } + } + +} diff --git a/rabbitmq-server/plugins-src/rabbitmq-mqtt/test/src/rabbit_mqtt_util_tests.erl b/rabbitmq-server/plugins-src/rabbitmq-mqtt/test/src/rabbit_mqtt_util_tests.erl new file mode 100644 index 0000000..be307bf --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-mqtt/test/src/rabbit_mqtt_util_tests.erl @@ -0,0 +1,42 @@ +%% The contents of this file are subject to the Mozilla Public License +%% Version 1.1 (the "License"); you may not use this file except in +%% compliance with the License. You may obtain a copy of the License +%% at http://www.mozilla.org/MPL/ +%% +%% Software distributed under the License is distributed on an "AS IS" +%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See +%% the License for the specific language governing rights and +%% limitations under the License. +%% +%% The Original Code is RabbitMQ. +%% +%% The Initial Developer of the Original Code is GoPivotal, Inc. +%% Copyright (c) 2007-2015 Pivotal Software, Inc. All rights reserved. +%% + +-module(rabbit_mqtt_util_tests). + +-include_lib("eunit/include/eunit.hrl"). + +all_test_() -> + {setup, + fun setup/0, + [fun coerce_exchange/0, + fun coerce_vhost/0, + fun coerce_default_user/0, + fun coerce_default_pass/0]}. + +setup() -> + application:load(rabbitmq_mqtt). + +coerce_exchange() -> + ?assertEqual(<<"amq.topic">>, rabbit_mqtt_util:env(exchange)). + +coerce_vhost() -> + ?assertEqual(<<"/">>, rabbit_mqtt_util:env(vhost)). + +coerce_default_user() -> + ?assertEqual(<<"guest_user">>, rabbit_mqtt_util:env(default_user)). + +coerce_default_pass() -> + ?assertEqual(<<"guest_pass">>, rabbit_mqtt_util:env(default_pass)). diff --git a/rabbitmq-server/plugins-src/rabbitmq-mqtt/test/src/rabbitmq_mqtt_standalone.app.src b/rabbitmq-server/plugins-src/rabbitmq-mqtt/test/src/rabbitmq_mqtt_standalone.app.src new file mode 100644 index 0000000..1c9fcb4 --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-mqtt/test/src/rabbitmq_mqtt_standalone.app.src @@ -0,0 +1,22 @@ +{application, rabbitmq_mqtt, + [{description, "RabbitMQ MQTT Adapter"}, + {vsn, "%%VSN%%"}, + {modules, []}, + {registered, []}, + {mod, {rabbit_mqtt, []}}, + {env, [{default_user, "guest_user"}, + {default_pass, "guest_pass"}, + {ssl_cert_login,false}, + {allow_anonymous, true}, + {vhost, "/"}, + {exchange, "amq.topic"}, + {subscription_ttl, 1800000}, % 30 min + {prefetch, 10}, + {ssl_listeners, []}, + {tcp_listeners, [1883]}, + {tcp_listen_options, [binary, + {packet, raw}, + {reuseaddr, true}, + {backlog, 128}, + {nodelay, true}]}]}, + {applications, [kernel, stdlib, rabbit, amqp_client]}]}. diff --git a/rabbitmq-server/plugins-src/rabbitmq-mqtt/test/src/test.config b/rabbitmq-server/plugins-src/rabbitmq-mqtt/test/src/test.config new file mode 100644 index 0000000..3d6baff --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-mqtt/test/src/test.config @@ -0,0 +1,14 @@ +[{rabbitmq_mqtt, [ + {ssl_cert_login, true}, + {allow_anonymous, true}, + {tcp_listeners, [1883]}, + {ssl_listeners, [8883]} + ]}, + {rabbit, [{ssl_options, [{cacertfile,"%%CERTS_DIR%%/testca/cacert.pem"}, + {certfile,"%%CERTS_DIR%%/server/cert.pem"}, + {keyfile,"%%CERTS_DIR%%/server/key.pem"}, + {verify,verify_peer}, + {fail_if_no_peer_cert,false} + ]} + ]} +]. diff --git a/rabbitmq-server/plugins-src/rabbitmq-mqtt/test/test.sh b/rabbitmq-server/plugins-src/rabbitmq-mqtt/test/test.sh new file mode 100755 index 0000000..ae60a49 --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-mqtt/test/test.sh @@ -0,0 +1,3 @@ +#!/bin/sh +$MAKE -C `dirname $0` build_java_amqp +$MAKE -C `dirname $0` test diff --git a/rabbitmq-server/plugins-src/rabbitmq-shovel-management/.srcdist_done b/rabbitmq-server/plugins-src/rabbitmq-shovel-management/.srcdist_done new file mode 100644 index 0000000..e69de29 diff --git a/rabbitmq-server/plugins-src/rabbitmq-shovel-management/CONTRIBUTING.md b/rabbitmq-server/plugins-src/rabbitmq-shovel-management/CONTRIBUTING.md new file mode 100644 index 0000000..69a4b4a --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-shovel-management/CONTRIBUTING.md @@ -0,0 +1,51 @@ +## Overview + +RabbitMQ projects use pull requests to discuss, collaborate on and accept code contributions. +Pull requests is the primary place of discussing code changes. + +## How to Contribute + +The process is fairly standard: + + * Fork the repository or repositories you plan on contributing to + * Clone [RabbitMQ umbrella repository](https://github.com/rabbitmq/rabbitmq-public-umbrella) + * `cd umbrella`, `make co` + * Create a branch with a descriptive name in the relevant repositories + * Make your changes, run tests, commit with a [descriptive message](http://tbaggery.com/2008/04/19/a-note-about-git-commit-messages.html), push to your fork + * Submit pull requests with an explanation what has been changed and **why** + * Submit a filled out and signed [Contributor Agreement](https://github.com/rabbitmq/ca#how-to-submit) if needed (see below) + * Be patient. We will get to your pull request eventually + +If what you are going to work on is a substantial change, please first ask the core team +of their opinion on [RabbitMQ mailing list](https://groups.google.com/forum/#!forum/rabbitmq-users). + + +## (Brief) Code of Conduct + +In one line: don't be a dick. + +Be respectful to the maintainers and other contributors. Open source +contributors put long hours into developing projects and doing user +support. Those projects and user support are available for free. We +believe this deserves some respect. + +Be respectful to people of all races, genders, religious beliefs and +political views. Regardless of how brilliant a pull request is +technically, we will not tolerate disrespectful or aggressive +behaviour. + +Contributors who violate this straightforward Code of Conduct will see +their pull requests closed and locked. + + +## Contributor Agreement + +If you want to contribute a non-trivial change, please submit a signed copy of our +[Contributor Agreement](https://github.com/rabbitmq/ca#how-to-submit) around the time +you submit your pull request. This will make it much easier (in some cases, possible) +for the RabbitMQ team at Pivotal to merge your contribution. + + +## Where to Ask Questions + +If something isn't clear, feel free to ask on our [mailing list](https://groups.google.com/forum/#!forum/rabbitmq-users). diff --git a/rabbitmq-server/plugins-src/rabbitmq-shovel-management/Makefile b/rabbitmq-server/plugins-src/rabbitmq-shovel-management/Makefile new file mode 100644 index 0000000..482105a --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-shovel-management/Makefile @@ -0,0 +1 @@ +include ../umbrella.mk diff --git a/rabbitmq-server/plugins-src/rabbitmq-shovel-management/README b/rabbitmq-server/plugins-src/rabbitmq-shovel-management/README new file mode 100644 index 0000000..8d6d3cc --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-shovel-management/README @@ -0,0 +1,11 @@ +Adds information on shovel status to the management plugin. Build it +like any other plugin. + +If you have a heterogenous cluster (where the nodes have different +plugins installed), this should be installed on the same nodes as the +management plugin. + +Strictly speaking the shovel does not need to be installed, but then +it won't tell you much. + +The HTTP API is very simple: GET /api/shovels. diff --git a/rabbitmq-server/plugins-src/rabbitmq-shovel-management/etc/rabbit-test.config b/rabbitmq-server/plugins-src/rabbitmq-shovel-management/etc/rabbit-test.config new file mode 100644 index 0000000..a0546a2 --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-shovel-management/etc/rabbit-test.config @@ -0,0 +1,19 @@ +%% We test sample retention separately in rabbit_mgmt_test_db_unit, +%% but for rabbit_mgmt_test_db we want to make sure samples never +%% expire. +[ + {rabbitmq_shovel, + [{shovels, + [{'my-static', + [{sources, [{broker, "amqp://"}, + {declarations, [{'queue.declare', [{queue, <<"static">>}]}]} + ]}, + {destinations, [{broker, "amqp://"}]}, + {queue, <<"static">>}, + {publish_fields, [ {exchange, <<"">>}, + {routing_key, <<"static2">>} + ]} + ]} + ]} + ]} +]. diff --git a/rabbitmq-server/plugins-src/rabbitmq-shovel-management/package.mk b/rabbitmq-server/plugins-src/rabbitmq-shovel-management/package.mk new file mode 100644 index 0000000..6c3bac2 --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-shovel-management/package.mk @@ -0,0 +1,9 @@ +RELEASABLE:=true +DEPS:=rabbitmq-management rabbitmq-shovel +WITH_BROKER_TEST_COMMANDS:=rabbit_shovel_mgmt_test_all:all_tests() +WITH_BROKER_TEST_CONFIG:=$(PACKAGE_DIR)/etc/rabbit-test + +CONSTRUCT_APP_PREREQS:=$(shell find $(PACKAGE_DIR)/priv -type f) +define construct_app_commands + cp -r $(PACKAGE_DIR)/priv $(APP_DIR) +endef diff --git a/rabbitmq-server/plugins-src/rabbitmq-shovel-management/priv/www/js/shovel.js b/rabbitmq-server/plugins-src/rabbitmq-shovel-management/priv/www/js/shovel.js new file mode 100644 index 0000000..13827d6 --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-shovel-management/priv/www/js/shovel.js @@ -0,0 +1,95 @@ +dispatcher_add(function(sammy) { + sammy.get('#/shovels', function() { + render({'shovels': {path: '/shovels', + options: {vhost:true}}}, + 'shovels', '#/shovels'); + }); + sammy.get('#/dynamic-shovels', function() { + render({'shovels': {path: '/parameters/shovel', + options:{vhost:true}}, + 'vhosts': '/vhosts'}, + 'dynamic-shovels', '#/dynamic-shovels'); + }); + sammy.get('#/dynamic-shovels/:vhost/:id', function() { + render({'shovel': '/parameters/shovel/' + esc(this.params['vhost']) + '/' + esc(this.params['id'])}, + 'dynamic-shovel', '#/dynamic-shovels'); + }); + sammy.put('#/shovel-parameters', function() { + var num_keys = ['prefetch-count', 'reconnect-delay']; + var bool_keys = ['add-forward-headers']; + var arrayable_keys = ['src-uri', 'dest-uri']; + var redirect = this.params['redirect']; + if (redirect != undefined) { + delete this.params['redirect']; + } + put_parameter(this, [], num_keys, bool_keys, arrayable_keys); + if (redirect != undefined) { + go_to(redirect); + } + return false; + }); + sammy.del('#/shovel-parameters', function() { + if (sync_delete(this, '/parameters/:component/:vhost/:name')) + go_to('#/dynamic-shovels'); + return false; + }); +}); + + +NAVIGATION['Admin'][0]['Shovel Status'] = ['#/shovels', "monitoring"]; +NAVIGATION['Admin'][0]['Shovel Management'] = ['#/dynamic-shovels', "policymaker"]; + +HELP['shovel-uri'] = + 'Both source and destination can be either a local or remote broker. See the "URI examples" pane for examples of how to construct URIs. If connecting to a cluster, you can enter several URIs here separated by spaces.'; + +HELP['shovel-queue-exchange'] = + 'You can set both source and destination as either a queue or an exchange. If you choose "queue", it will be declared beforehand; if you choose "exchange" it will not, but an appropriate binding and queue will be created when the source is an exchange.'; + +HELP['shovel-prefetch'] = + 'Maximum number of unacknowledged messages that may be in flight over a shovel at one time. Defaults to 1000 if not set.'; + +HELP['shovel-reconnect'] = + 'Time in seconds to wait after a shovel goes down before attempting reconnection. Defaults to 1 if not set.'; + +HELP['shovel-forward-headers'] = + 'Whether to add headers to the shovelled messages indicating where they have been shovelled from and to. Defaults to false if not set.'; + +HELP['shovel-ack-mode'] = + '
\ +
on-confirm
\ +
Messages are acknowledged at the source after they have been confirmed at the destination. Handles network errors and broker failures without losing messages. The slowest option, and the default.
\ +
on-publish
\ +
Messages are acknowledged at the source after they have been published at the destination. Handles network errors without losing messages, but may lose messages in the event of broker failures.
\ +
no-ack
\ +
Message acknowledgements are not used. The fastest option, but may lose messages in the event of network or broker failures.
\ +
'; + +HELP['shovel-delete-after'] = + '
\ +
Never
\ +
The shovel never deletes itself; it will persist until it is explicitly removed.
\ +
After initial length transferred
\ +
The shovel will check the length of the queue when it starts up. It will transfer that many messages, and then delete itself.
\ +
'; + +function link_shovel(vhost, name) { + return _link_to(name, '#/dynamic-shovels/' + esc(vhost) + '/' + esc(name)); +} + +function fmt_shovel_endpoint(prefix, shovel) { + var txt = ''; + if (shovel[prefix + '-queue']) { + txt += fmt_string(shovel[prefix + '-queue']) + 'queue'; + } else { + if (shovel[prefix + '-exchange']) { + txt += fmt_string(shovel[prefix + '-exchange']); + } else { + txt += 'as published'; + } + if (shovel[prefix + '-exchange-key']) { + txt += ' : ' + fmt_string(shovel[prefix + '-exchange-key']); + } + txt += 'exchange'; + } + return txt; +} diff --git a/rabbitmq-server/plugins-src/rabbitmq-shovel-management/priv/www/js/tmpl/dynamic-shovel.ejs b/rabbitmq-server/plugins-src/rabbitmq-shovel-management/priv/www/js/tmpl/dynamic-shovel.ejs new file mode 100644 index 0000000..9f7759b --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-shovel-management/priv/www/js/tmpl/dynamic-shovel.ejs @@ -0,0 +1,57 @@ +

Dynamic Shovel: <%= fmt_string(shovel.name) %><%= fmt_maybe_vhost(shovel.vhost) %>

+ +
+

Overview

+
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Source<%= fmt_string(shovel.value['src-uri']) %>
<%= fmt_shovel_endpoint('src', shovel.value) %>
Destination<%= fmt_string(shovel.value['dest-uri']) %>
<%= fmt_shovel_endpoint('dest', shovel.value) %>
Prefetch count<%= fmt_string(shovel.value['prefetch-count']) %>
Reconnect delay<%= fmt_time(shovel.value['reconnect-delay'], 's') %>
Add headers<%= fmt_boolean(shovel.value['add-forward-headers']) %>
Ack mode<%= fmt_string(shovel.value['ack-mode']) %>
Auto-delete<%= fmt_string(shovel.value['delete-after']) %>
+
+
+ +
+

Delete this shovel

+
+
+ + + + +
+
+
diff --git a/rabbitmq-server/plugins-src/rabbitmq-shovel-management/priv/www/js/tmpl/dynamic-shovels.ejs b/rabbitmq-server/plugins-src/rabbitmq-shovel-management/priv/www/js/tmpl/dynamic-shovels.ejs new file mode 100644 index 0000000..cb82d8b --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-shovel-management/priv/www/js/tmpl/dynamic-shovels.ejs @@ -0,0 +1,237 @@ +

Dynamic Shovels

+
+

Shovels

+
+<% if (shovels.length > 0) { %> + + + +<% if (vhosts_interesting) { %> + +<% } %> + + + + + + + + + + + +<% + for (var i = 0; i < shovels.length; i++) { + var shovel = shovels[i]; +%> + > +<% if (vhosts_interesting) { %> + +<% } %> + + + + + + + + + + + +<% } %> + +
Virtual HostNameSourceDestinationPrefetch CountReconnect DelayAdd headersAck modeAuto-delete
<%= fmt_string(shovel.vhost) %><%= link_shovel(shovel.vhost, shovel.name) %><%= fmt_shortened_uri(shovel.value['src-uri']) %><%= fmt_shovel_endpoint('src', shovel.value) %><%= fmt_shortened_uri(shovel.value['dest-uri']) %><%= fmt_shovel_endpoint('dest', shovel.value) %><%= shovel.value['prefetch-count'] %><%= fmt_time(shovel.value['reconnect-delay'], 's') %><%= fmt_boolean(shovel.value['add-forward-headers']) %><%= fmt_string(shovel.value['ack-mode']) %><%= fmt_string(shovel.value['delete-after']) %>
+<% } else { %> +

... no shovels ...

+<% } %> +
+
+ +
+

Add a new shovel

+
+
+ + +<% if (vhosts_interesting) { %> + + + + +<% } else { %> + +<% } %> + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ +
*
Source: + + + + + + + + + +
+ URI + + + + +
* +
+ +
+ +
+
Destination: + + + + + + + + + +
+ URI + + + + +
* +
+ +
+ +
+
+ +
+ + s
+ + + +
+ + + +
+ + + +
+ +
+
+
+
+

URI examples

+
+
    +
  • + amqp://
    + connect to local server as default user +
  • +
  • + amqp://user@/my-vhost
    + connect to local server with alternate user and virtual host + (passwords are not required for local connections) +
  • +
  • + amqp://server-name
    + connect to server-name, without SSL and default credentials +
  • +
  • + amqp://user:password@server-name/my-vhost
    + connect to server-name, with credentials and overridden + virtual host +
  • +
  • + amqps://user:password@server-name?cacertfile=/path/to/cacert.pem&certfile=/path/to/cert.pem&keyfile=/path/to/key.pem&verify=verify_peer
    + connect to server-name, with credentials and SSL +
  • +
  • + amqps://server-name?cacertfile=/path/to/cacert.pem&certfile=/path/to/cert.pem&keyfile=/path/to/key.pem&verify=verify_peer&fail_if_no_peer_cert=true&auth_mechanism=external
    + connect to server-name, with SSL and EXTERNAL authentication +
  • +
+
+
diff --git a/rabbitmq-server/plugins-src/rabbitmq-shovel-management/priv/www/js/tmpl/shovels.ejs b/rabbitmq-server/plugins-src/rabbitmq-shovel-management/priv/www/js/tmpl/shovels.ejs new file mode 100644 index 0000000..728bf44 --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-shovel-management/priv/www/js/tmpl/shovels.ejs @@ -0,0 +1,70 @@ +

Shovel Status

+<% + var extra_width = 0; + if (vhosts_interesting) extra_width++; + if (nodes_interesting) extra_width++; +%> +
+<% if (shovels.length > 0) { %> + + + + +<% if (nodes_interesting) { %> + +<% } %> +<% if (vhosts_interesting) { %> + +<% } %> + + + + + + + +<% + for (var i = 0; i < shovels.length; i++) { + var shovel = shovels[i]; +%> + > + +<% if (nodes_interesting) { %> + +<% } %> +<% if (vhosts_interesting) { %> + +<% } %> +<% if (shovel.state == 'terminated') { %> + + + + + + +<% } else { %> + + <% if (shovel.definition == undefined) { %> + + + <% } else { %> + + + + + <% } %> + + +<% } %> + <% } %> + +
NameNodeVirtual HostStateSourceDestinationLast changed
+ <%= fmt_string(shovel.name) %> + <%= fmt_string(shovel.type) %> + <%= fmt_node(shovel.node) %><%= fmt_string(shovel.vhost, '') %><%= fmt_state('red', shovel.state) %><%= shovel.timestamp %>
+
<%= fmt_string(shovel.reason) %>
+
<%= fmt_state('green', shovel.state) %><%= fmt_string(shovel.src_uri) %><%= fmt_string(shovel.dest_uri) %><%= fmt_string(shovel.src_uri) %><%= fmt_shovel_endpoint('src', shovel.definition) %><%= fmt_string(shovel.dest_uri) %><%= fmt_shovel_endpoint('dest', shovel.definition) %><%= shovel.timestamp %>
+<% } else { %> +

... no shovels ...

+<% } %> +
diff --git a/rabbitmq-server/plugins-src/rabbitmq-shovel-management/src/rabbit_shovel_mgmt.erl b/rabbitmq-server/plugins-src/rabbitmq-shovel-management/src/rabbit_shovel_mgmt.erl new file mode 100644 index 0000000..3f5c846 --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-shovel-management/src/rabbit_shovel_mgmt.erl @@ -0,0 +1,130 @@ +%% The contents of this file are subject to the Mozilla Public License +%% Version 1.1 (the "License"); you may not use this file except in +%% compliance with the License. You may obtain a copy of the License +%% at http://www.mozilla.org/MPL/ +%% +%% Software distributed under the License is distributed on an "AS IS" +%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See +%% the License for the specific language governing rights and +%% limitations under the License. +%% +%% The Original Code is RabbitMQ. +%% +%% The Initial Developer of the Original Code is GoPivotal, Inc. +%% Copyright (c) 2007-2014 GoPivotal, Inc. All rights reserved. +%% + +-module(rabbit_shovel_mgmt). + +-behaviour(rabbit_mgmt_extension). + +-export([dispatcher/0, web_ui/0]). +-export([init/1, to_json/2, resource_exists/2, content_types_provided/2, + is_authorized/2]). + +-import(rabbit_misc, [pget/2]). + +-include_lib("rabbitmq_management/include/rabbit_mgmt.hrl"). +-include_lib("amqp_client/include/amqp_client.hrl"). +-include_lib("webmachine/include/webmachine.hrl"). + +dispatcher() -> [{["shovels"], ?MODULE, []}, + {["shovels", vhost], ?MODULE, []}]. +web_ui() -> [{javascript, <<"shovel.js">>}]. + +%%-------------------------------------------------------------------- + +init(_Config) -> {ok, #context{}}. + +content_types_provided(ReqData, Context) -> + {[{"application/json", to_json}], ReqData, Context}. + +resource_exists(ReqData, Context) -> + {case rabbit_mgmt_util:vhost(ReqData) of + not_found -> false; + _ -> true + end, ReqData, Context}. + +to_json(ReqData, Context) -> + rabbit_mgmt_util:reply_list( + filter_vhost_req(status(ReqData, Context), ReqData), ReqData, Context). + +is_authorized(ReqData, Context) -> + rabbit_mgmt_util:is_authorized_monitor(ReqData, Context). + +%%-------------------------------------------------------------------- + +filter_vhost_req(List, ReqData) -> + case rabbit_mgmt_util:vhost(ReqData) of + none -> List; + VHost -> [I || I <- List, + pget(vhost, I) =:= VHost] + end. + +%% Allow users to see things in the vhosts they are authorised. But +%% static shovels do not have a vhost, so only allow admins (not +%% monitors) to see them. +filter_vhost_user(List, _ReqData, #context{user = User = #user{tags = Tags}}) -> + VHosts = rabbit_mgmt_util:list_login_vhosts(User, undefined), + [I || I <- List, case pget(vhost, I) of + undefined -> lists:member(administrator, Tags); + VHost -> lists:member(VHost, VHosts) + end]. + +status(ReqData, Context) -> + filter_vhost_user( + lists:append([status(Node) || Node <- [node() | nodes()]]), + ReqData, Context). + +status(Node) -> + case rpc:call(Node, rabbit_shovel_status, status, [], infinity) of + {badrpc, {'EXIT', _}} -> + []; + Status -> + [format(Node, I) || I <- Status] + end. + +format(Node, {Name, Type, Info, TS}) -> + [{node, Node}, {timestamp, format_ts(TS)}] ++ + format_name(Type, Name) ++ + format_info(Info, Type, Name). + +format_name(static, Name) -> [{name, Name}, + {type, static}]; +format_name(dynamic, {VHost, Name}) -> [{name, Name}, + {vhost, VHost}, + {type, dynamic}]. + +format_info(starting, _Type, _Name) -> + [{state, starting}]; + +format_info({running, Props}, Type, Name) -> + [{state, running}] ++ lookup_src_dest(Type, Name) ++ Props; + +format_info({terminated, Reason}, _Type, _Name) -> + [{state, terminated}, + {reason, print("~p", [Reason])}]. + +format_ts({{Y, M, D}, {H, Min, S}}) -> + print("~w-~2.2.0w-~2.2.0w ~w:~2.2.0w:~2.2.0w", [Y, M, D, H, Min, S]). + +print(Fmt, Val) -> + list_to_binary(io_lib:format(Fmt, Val)). + +lookup_src_dest(static, _Name) -> + %% This is too messy to do, the config may be on another node and anyway + %% does not necessarily tell us the source and destination very clearly. + []; + +lookup_src_dest(dynamic, {VHost, Name}) -> + case rabbit_runtime_parameters:lookup(VHost, <<"shovel">>, Name) of + %% We might not find anything if the shovel has been deleted + %% before we got here + not_found -> + []; + Props -> + Def = pget(value, Props), + Ks = [<<"src-queue">>, <<"src-exchange">>, <<"src-exchange-key">>, + <<"dest-queue">>,<<"dest-exchange">>,<<"dest-exchange-key">>], + [{definition, [{K, V} || {K, V} <- Def, lists:member(K, Ks)]}] + end. diff --git a/rabbitmq-server/plugins-src/rabbitmq-shovel-management/src/rabbitmq_shovel_management.app.src b/rabbitmq-server/plugins-src/rabbitmq-shovel-management/src/rabbitmq_shovel_management.app.src new file mode 100644 index 0000000..61e4ca0 --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-shovel-management/src/rabbitmq_shovel_management.app.src @@ -0,0 +1,6 @@ +{application, rabbitmq_shovel_management, + [{description, "Shovel Status"}, + {vsn, "%%VSN%%"}, + {modules, []}, + {registered, []}, + {applications, [kernel, stdlib, rabbit, rabbitmq_management]}]}. diff --git a/rabbitmq-server/plugins-src/rabbitmq-shovel-management/test/src/rabbit_shovel_mgmt_test_all.erl b/rabbitmq-server/plugins-src/rabbitmq-shovel-management/test/src/rabbit_shovel_mgmt_test_all.erl new file mode 100644 index 0000000..b82c4e1 --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-shovel-management/test/src/rabbit_shovel_mgmt_test_all.erl @@ -0,0 +1,28 @@ +%% The contents of this file are subject to the Mozilla Public License +%% Version 1.1 (the "License"); you may not use this file except in +%% compliance with the License. You may obtain a copy of the License at +%% http://www.mozilla.org/MPL/ +%% +%% Software distributed under the License is distributed on an "AS IS" +%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the +%% License for the specific language governing rights and limitations +%% under the License. +%% +%% The Original Code is RabbitMQ Management Console. +%% +%% The Initial Developer of the Original Code is GoPivotal, Inc. +%% Copyright (c) 2010-2014 GoPivotal, Inc. All rights reserved. +%% + +-module(rabbit_shovel_mgmt_test_all). + +-export([all_tests/0]). + +all_tests() -> + ok = eunit:test(tests(rabbit_shovel_mgmt_test_http, 60), [verbose]). + +tests(Module, Timeout) -> + {foreach, fun() -> ok end, + [{timeout, Timeout, fun () -> Module:F() end} || + {F, _Arity} <- proplists:get_value(exports, Module:module_info()), + string:right(atom_to_list(F), 5) =:= "_test"]}. diff --git a/rabbitmq-server/plugins-src/rabbitmq-shovel-management/test/src/rabbit_shovel_mgmt_test_http.erl b/rabbitmq-server/plugins-src/rabbitmq-shovel-management/test/src/rabbit_shovel_mgmt_test_http.erl new file mode 100644 index 0000000..b3407ce --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-shovel-management/test/src/rabbit_shovel_mgmt_test_http.erl @@ -0,0 +1,217 @@ +%% The contents of this file are subject to the Mozilla Public License +%% Version 1.1 (the "License"); you may not use this file except in +%% compliance with the License. You may obtain a copy of the License at +%% http://www.mozilla.org/MPL/ +%% +%% Software distributed under the License is distributed on an "AS IS" +%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the +%% License for the specific language governing rights and limitations +%% under the License. +%% +%% The Original Code is RabbitMQ Management Console. +%% +%% The Initial Developer of the Original Code is GoPivotal, Inc. +%% Copyright (c) 2010-2014 GoPivotal, Inc. All rights reserved. +%% + +-module(rabbit_shovel_mgmt_test_http). + +-include_lib("rabbitmq_management/include/rabbit_mgmt_test.hrl"). + +-import(rabbit_misc, [pget/2]). + +shovels_test() -> + http_put("/users/admin", [{password, <<"admin">>}, + {tags, <<"administrator">>}], ?NO_CONTENT), + http_put("/users/mon", [{password, <<"mon">>}, + {tags, <<"monitoring">>}], ?NO_CONTENT), + http_put("/vhosts/v", none, ?NO_CONTENT), + Perms = [{configure, <<".*">>}, + {write, <<".*">>}, + {read, <<".*">>}], + http_put("/permissions/v/guest", Perms, ?NO_CONTENT), + http_put("/permissions/v/admin", Perms, ?NO_CONTENT), + http_put("/permissions/v/mon", Perms, ?NO_CONTENT), + + [http_put("/parameters/shovel/" ++ V ++ "/my-dynamic", + [{value, [{'src-uri', <<"amqp://">>}, + {'dest-uri', <<"amqp://">>}, + {'src-queue', <<"test">>}, + {'dest-queue', <<"test2">>}]}], ?NO_CONTENT) + || V <- ["%2f", "v"]], + Static = [{name, <<"my-static">>}, + {type, <<"static">>}], + Dynamic1 = [{name, <<"my-dynamic">>}, + {vhost, <<"/">>}, + {type, <<"dynamic">>}], + Dynamic2 = [{name, <<"my-dynamic">>}, + {vhost, <<"v">>}, + {type, <<"dynamic">>}], + Assert = fun (Req, User, Res) -> + assert_list(Res, http_get(Req, User, User, ?OK)) + end, + Assert("/shovels", "guest", [Static, Dynamic1, Dynamic2]), + Assert("/shovels/%2f", "guest", [Dynamic1]), + Assert("/shovels/v", "guest", [Dynamic2]), + Assert("/shovels", "admin", [Static, Dynamic2]), + Assert("/shovels/%2f", "admin", []), + Assert("/shovels/v", "admin", [Dynamic2]), + Assert("/shovels", "mon", [Dynamic2]), + Assert("/shovels/%2f", "mon", []), + Assert("/shovels/v", "mon", [Dynamic2]), + + http_delete("/vhosts/v", ?NO_CONTENT), + http_delete("/users/admin", ?NO_CONTENT), + http_delete("/users/mon", ?NO_CONTENT), + ok. + +%% It's a bit arbitrary to be testing this here, but we want to be +%% able to test that mgmt extensions can be started and stopped +%% *somewhere*, and here is as good a place as any. +dynamic_plugin_enable_disable_test() -> + http_get("/shovels", ?OK), + disable_plugin("rabbitmq_shovel_management"), + http_get("/shovels", ?NOT_FOUND), + http_get("/overview", ?OK), + disable_plugin("rabbitmq_management"), + http_fail("/shovels"), + http_fail("/overview"), + enable_plugin("rabbitmq_management"), + http_get("/shovels", ?NOT_FOUND), + http_get("/overview", ?OK), + enable_plugin("rabbitmq_shovel_management"), + http_get("/shovels", ?OK), + http_get("/overview", ?OK), + passed. + +%%--------------------------------------------------------------------------- +%% TODO this is mostly copypasta from the mgmt tests + +http_get(Path) -> + http_get(Path, ?OK). + +http_get(Path, CodeExp) -> + http_get(Path, "guest", "guest", CodeExp). + +http_get(Path, User, Pass, CodeExp) -> + {ok, {{_HTTP, CodeAct, _}, Headers, ResBody}} = + req(get, Path, [auth_header(User, Pass)]), + assert_code(CodeExp, CodeAct, "GET", Path, ResBody), + decode(CodeExp, Headers, ResBody). + +http_fail(Path) -> + {error, {failed_connect, _}} = req(get, Path, []). + +http_put(Path, List, CodeExp) -> + http_put_raw(Path, format_for_upload(List), CodeExp). + +http_put(Path, List, User, Pass, CodeExp) -> + http_put_raw(Path, format_for_upload(List), User, Pass, CodeExp). + +http_post(Path, List, CodeExp) -> + http_post_raw(Path, format_for_upload(List), CodeExp). + +http_post(Path, List, User, Pass, CodeExp) -> + http_post_raw(Path, format_for_upload(List), User, Pass, CodeExp). + +format_for_upload(none) -> + <<"">>; +format_for_upload(List) -> + iolist_to_binary(mochijson2:encode({struct, List})). + +http_put_raw(Path, Body, CodeExp) -> + http_upload_raw(put, Path, Body, "guest", "guest", CodeExp). + +http_put_raw(Path, Body, User, Pass, CodeExp) -> + http_upload_raw(put, Path, Body, User, Pass, CodeExp). + +http_post_raw(Path, Body, CodeExp) -> + http_upload_raw(post, Path, Body, "guest", "guest", CodeExp). + +http_post_raw(Path, Body, User, Pass, CodeExp) -> + http_upload_raw(post, Path, Body, User, Pass, CodeExp). + +http_upload_raw(Type, Path, Body, User, Pass, CodeExp) -> + {ok, {{_HTTP, CodeAct, _}, Headers, ResBody}} = + req(Type, Path, [auth_header(User, Pass)], Body), + assert_code(CodeExp, CodeAct, Type, Path, ResBody), + decode(CodeExp, Headers, ResBody). + +http_delete(Path, CodeExp) -> + http_delete(Path, "guest", "guest", CodeExp). + +http_delete(Path, User, Pass, CodeExp) -> + {ok, {{_HTTP, CodeAct, _}, Headers, ResBody}} = + req(delete, Path, [auth_header(User, Pass)]), + assert_code(CodeExp, CodeAct, "DELETE", Path, ResBody), + decode(CodeExp, Headers, ResBody). + +assert_code(CodeExp, CodeAct, Type, Path, Body) -> + case CodeExp of + CodeAct -> ok; + _ -> throw({expected, CodeExp, got, CodeAct, type, Type, + path, Path, body, Body}) + end. + +req(Type, Path, Headers) -> + httpc:request(Type, {?PREFIX ++ Path, Headers}, ?HTTPC_OPTS, []). + +req(Type, Path, Headers, Body) -> + httpc:request(Type, {?PREFIX ++ Path, Headers, "application/json", Body}, + ?HTTPC_OPTS, []). + +decode(?OK, _Headers, ResBody) -> cleanup(mochijson2:decode(ResBody)); +decode(_, Headers, _ResBody) -> Headers. + +cleanup(L) when is_list(L) -> + [cleanup(I) || I <- L]; +cleanup({struct, I}) -> + cleanup(I); +cleanup({K, V}) when is_binary(K) -> + {list_to_atom(binary_to_list(K)), cleanup(V)}; +cleanup(I) -> + I. + +auth_header(Username, Password) -> + {"Authorization", + "Basic " ++ binary_to_list(base64:encode(Username ++ ":" ++ Password))}. + +assert_list(Exp, Act) -> + case length(Exp) == length(Act) of + true -> ok; + false -> throw({expected, Exp, actual, Act}) + end, + [case length(lists:filter(fun(ActI) -> test_item(ExpI, ActI) end, Act)) of + 1 -> ok; + N -> throw({found, N, ExpI, in, Act}) + end || ExpI <- Exp]. + +assert_item(Exp, Act) -> + case test_item0(Exp, Act) of + [] -> ok; + Or -> throw(Or) + end. + +test_item(Exp, Act) -> + case test_item0(Exp, Act) of + [] -> true; + _ -> false + end. + +test_item0(Exp, Act) -> + [{did_not_find, ExpI, in, Act} || ExpI <- Exp, + not lists:member(ExpI, Act)]. +%%--------------------------------------------------------------------------- + +enable_plugin(Plugin) -> + plugins_action(enable, [Plugin], []). + +disable_plugin(Plugin) -> + plugins_action(disable, [Plugin], []). + +plugins_action(Command, Args, Opts) -> + PluginsFile = os:getenv("RABBITMQ_ENABLED_PLUGINS_FILE"), + PluginsDir = os:getenv("RABBITMQ_PLUGINS_DIR"), + Node = node(), + rpc:call(Node, rabbit_plugins_main, action, + [Command, Node, Args, Opts, PluginsFile, PluginsDir]). diff --git a/rabbitmq-server/plugins-src/rabbitmq-shovel/.srcdist_done b/rabbitmq-server/plugins-src/rabbitmq-shovel/.srcdist_done new file mode 100644 index 0000000..e69de29 diff --git a/rabbitmq-server/plugins-src/rabbitmq-shovel/CONTRIBUTING.md b/rabbitmq-server/plugins-src/rabbitmq-shovel/CONTRIBUTING.md new file mode 100644 index 0000000..69a4b4a --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-shovel/CONTRIBUTING.md @@ -0,0 +1,51 @@ +## Overview + +RabbitMQ projects use pull requests to discuss, collaborate on and accept code contributions. +Pull requests is the primary place of discussing code changes. + +## How to Contribute + +The process is fairly standard: + + * Fork the repository or repositories you plan on contributing to + * Clone [RabbitMQ umbrella repository](https://github.com/rabbitmq/rabbitmq-public-umbrella) + * `cd umbrella`, `make co` + * Create a branch with a descriptive name in the relevant repositories + * Make your changes, run tests, commit with a [descriptive message](http://tbaggery.com/2008/04/19/a-note-about-git-commit-messages.html), push to your fork + * Submit pull requests with an explanation what has been changed and **why** + * Submit a filled out and signed [Contributor Agreement](https://github.com/rabbitmq/ca#how-to-submit) if needed (see below) + * Be patient. We will get to your pull request eventually + +If what you are going to work on is a substantial change, please first ask the core team +of their opinion on [RabbitMQ mailing list](https://groups.google.com/forum/#!forum/rabbitmq-users). + + +## (Brief) Code of Conduct + +In one line: don't be a dick. + +Be respectful to the maintainers and other contributors. Open source +contributors put long hours into developing projects and doing user +support. Those projects and user support are available for free. We +believe this deserves some respect. + +Be respectful to people of all races, genders, religious beliefs and +political views. Regardless of how brilliant a pull request is +technically, we will not tolerate disrespectful or aggressive +behaviour. + +Contributors who violate this straightforward Code of Conduct will see +their pull requests closed and locked. + + +## Contributor Agreement + +If you want to contribute a non-trivial change, please submit a signed copy of our +[Contributor Agreement](https://github.com/rabbitmq/ca#how-to-submit) around the time +you submit your pull request. This will make it much easier (in some cases, possible) +for the RabbitMQ team at Pivotal to merge your contribution. + + +## Where to Ask Questions + +If something isn't clear, feel free to ask on our [mailing list](https://groups.google.com/forum/#!forum/rabbitmq-users). diff --git a/rabbitmq-server/plugins-src/rabbitmq-shovel/Makefile b/rabbitmq-server/plugins-src/rabbitmq-shovel/Makefile new file mode 100644 index 0000000..482105a --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-shovel/Makefile @@ -0,0 +1 @@ +include ../umbrella.mk diff --git a/rabbitmq-server/plugins-src/rabbitmq-shovel/README b/rabbitmq-server/plugins-src/rabbitmq-shovel/README new file mode 100644 index 0000000..1d7d1b0 --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-shovel/README @@ -0,0 +1,4 @@ +Generic build instructions are at: + http://www.rabbitmq.com/plugin-development.html + +See the http://www.rabbitmq.com/shovel.html page for full instructions. diff --git a/rabbitmq-server/plugins-src/rabbitmq-shovel/generate_deps b/rabbitmq-server/plugins-src/rabbitmq-shovel/generate_deps new file mode 100644 index 0000000..29587b5 --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-shovel/generate_deps @@ -0,0 +1,54 @@ +#!/usr/bin/env escript +%% -*- erlang -*- +-mode(compile). + +main([IncludeDir, ErlDir, EbinDir, TargetFile]) -> + ErlDirContents = filelib:wildcard("*.erl", ErlDir), + ErlFiles = [filename:join(ErlDir, FileName) || FileName <- ErlDirContents], + Modules = sets:from_list( + [list_to_atom(filename:basename(FileName, ".erl")) || + FileName <- ErlDirContents]), + Headers = sets:from_list( + [filename:join(IncludeDir, FileName) || + FileName <- filelib:wildcard("*.hrl", IncludeDir)]), + Deps = lists:foldl( + fun (Path, Deps1) -> + dict:store(Path, detect_deps(IncludeDir, EbinDir, + Modules, Headers, Path), + Deps1) + end, dict:new(), ErlFiles), + {ok, Hdl} = file:open(TargetFile, [write, delayed_write]), + dict:fold( + fun (_Path, [], ok) -> + ok; + (Path, Dep, ok) -> + Module = filename:basename(Path, ".erl"), + ok = file:write(Hdl, [EbinDir, "/", Module, ".beam: ", + Path]), + ok = sets:fold(fun (E, ok) -> file:write(Hdl, [" ", E]) end, + ok, Dep), + file:write(Hdl, ["\n"]) + end, ok, Deps), + ok = file:write(Hdl, [TargetFile, ": ", escript:script_name(), "\n"]), + ok = file:sync(Hdl), + ok = file:close(Hdl). + +detect_deps(IncludeDir, EbinDir, Modules, Headers, Path) -> + {ok, Forms} = epp:parse_file(Path, [IncludeDir], [{use_specs, true}]), + lists:foldl( + fun ({attribute, _LineNumber, Attribute, Behaviour}, Deps) + when Attribute =:= behaviour orelse Attribute =:= behavior -> + case sets:is_element(Behaviour, Modules) of + true -> sets:add_element( + [EbinDir, "/", atom_to_list(Behaviour), ".beam"], + Deps); + false -> Deps + end; + ({attribute, _LineNumber, file, {FileName, _LineNumber1}}, Deps) -> + case sets:is_element(FileName, Headers) of + true -> sets:add_element(FileName, Deps); + false -> Deps + end; + (_Form, Deps) -> + Deps + end, sets:new(), Forms). diff --git a/rabbitmq-server/plugins-src/rabbitmq-shovel/include/rabbit_shovel.hrl b/rabbitmq-server/plugins-src/rabbitmq-shovel/include/rabbit_shovel.hrl new file mode 100644 index 0000000..5168c8f --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-shovel/include/rabbit_shovel.hrl @@ -0,0 +1,32 @@ +%% The contents of this file are subject to the Mozilla Public License +%% Version 1.1 (the "License"); you may not use this file except in +%% compliance with the License. You may obtain a copy of the License +%% at http://www.mozilla.org/MPL/ +%% +%% Software distributed under the License is distributed on an "AS IS" +%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See +%% the License for the specific language governing rights and +%% limitations under the License. +%% +%% The Original Code is RabbitMQ. +%% +%% The Initial Developer of the Original Code is GoPivotal, Inc. +%% Copyright (c) 2007-2015 Pivotal Software, Inc. All rights reserved. +%% + +-record(endpoint, + {uris, + resource_declaration + }). + +-record(shovel, + {sources, + destinations, + prefetch_count, + ack_mode, + publish_fields, + publish_properties, + queue, + reconnect_delay, + delete_after = never + }). diff --git a/rabbitmq-server/plugins-src/rabbitmq-shovel/package.mk b/rabbitmq-server/plugins-src/rabbitmq-shovel/package.mk new file mode 100644 index 0000000..6cf8254 --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-shovel/package.mk @@ -0,0 +1,3 @@ +RELEASABLE:=true +DEPS:=rabbitmq-erlang-client +WITH_BROKER_TEST_COMMANDS:=rabbit_shovel_test_all:all_tests() diff --git a/rabbitmq-server/plugins-src/rabbitmq-shovel/src/rabbit_shovel.erl b/rabbitmq-server/plugins-src/rabbitmq-shovel/src/rabbit_shovel.erl new file mode 100644 index 0000000..c945321 --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-shovel/src/rabbit_shovel.erl @@ -0,0 +1,27 @@ +%% The contents of this file are subject to the Mozilla Public License +%% Version 1.1 (the "License"); you may not use this file except in +%% compliance with the License. You may obtain a copy of the License +%% at http://www.mozilla.org/MPL/ +%% +%% Software distributed under the License is distributed on an "AS IS" +%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See +%% the License for the specific language governing rights and +%% limitations under the License. +%% +%% The Original Code is RabbitMQ. +%% +%% The Initial Developer of the Original Code is GoPivotal, Inc. +%% Copyright (c) 2007-2015 Pivotal Software, Inc. All rights reserved. +%% + +-module(rabbit_shovel). + +-export([start/0, stop/0, start/2, stop/1]). + +start() -> rabbit_shovel_sup:start_link(), ok. + +stop() -> ok. + +start(normal, []) -> rabbit_shovel_sup:start_link(). + +stop(_State) -> ok. diff --git a/rabbitmq-server/plugins-src/rabbitmq-shovel/src/rabbit_shovel_config.erl b/rabbitmq-server/plugins-src/rabbitmq-shovel/src/rabbit_shovel_config.erl new file mode 100644 index 0000000..a20b73b --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-shovel/src/rabbit_shovel_config.erl @@ -0,0 +1,257 @@ +%% The contents of this file are subject to the Mozilla Public License +%% Version 1.1 (the "License"); you may not use this file except in +%% compliance with the License. You may obtain a copy of the License +%% at http://www.mozilla.org/MPL/ +%% +%% Software distributed under the License is distributed on an "AS IS" +%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See +%% the License for the specific language governing rights and +%% limitations under the License. +%% +%% The Original Code is RabbitMQ. +%% +%% The Initial Developer of the Original Code is GoPivotal, Inc. +%% Copyright (c) 2007-2015 Pivotal Software, Inc. All rights reserved. +%% + +-module(rabbit_shovel_config). + +-export([parse/2, + ensure_defaults/2]). + +-include_lib("amqp_client/include/amqp_client.hrl"). +-include("rabbit_shovel.hrl"). + +-define(IGNORE_FIELDS, [delete_after]). +-define(EXTRA_KEYS, [add_forward_headers]). + +parse(ShovelName, Config) -> + {ok, Defaults} = application:get_env(defaults), + try + {ok, parse_shovel_config_dict( + ShovelName, parse_shovel_config_proplist( + enrich_shovel_config(Config, Defaults)))} + catch throw:{error, Reason} -> + {error, {invalid_shovel_configuration, ShovelName, Reason}} + end. + +%% ensures that any defaults that have been applied to a parsed +%% shovel, are written back to the original proplist +ensure_defaults(ShovelConfig, ParsedShovel) -> + lists:keystore(reconnect_delay, 1, + ShovelConfig, + {reconnect_delay, + ParsedShovel#shovel.reconnect_delay}). + +enrich_shovel_config(Config, Defaults) -> + Config1 = proplists:unfold(Config), + case [E || E <- Config1, not (is_tuple(E) andalso tuple_size(E) == 2)] of + [] -> case duplicate_keys(Config1) of + [] -> return(lists:ukeysort(1, Config1 ++ Defaults)); + Dups -> fail({duplicate_parameters, Dups}) + end; + Invalid -> fail({invalid_parameters, Invalid}) + end. + +parse_shovel_config_proplist(Config) -> + Dict = dict:from_list(Config), + Fields = record_info(fields, shovel) -- ?IGNORE_FIELDS, + Keys = dict:fetch_keys(Dict) -- ?EXTRA_KEYS, + case {Keys -- Fields, Fields -- Keys} of + {[], []} -> {_Pos, Dict1} = + lists:foldl( + fun (FieldName, {Pos, Acc}) -> + {Pos + 1, + dict:update(FieldName, + fun (V) -> {V, Pos} end, + Acc)} + end, {2, Dict}, Fields), + return(Dict1); + {[], Missing} -> fail({missing_parameters, Missing}); + {Unknown, _} -> fail({unrecognised_parameters, Unknown}) + end. + +parse_shovel_config_dict(Name, Dict) -> + Cfg = run_state_monad( + [fun (Shovel) -> + {ok, Value} = dict:find(Key, Dict), + try {ParsedValue, Pos} = Fun(Value), + return(setelement(Pos, Shovel, ParsedValue)) + catch throw:{error, Reason} -> + fail({invalid_parameter_value, Key, Reason}) + end + end || {Fun, Key} <- + [{fun parse_endpoint/1, sources}, + {fun parse_endpoint/1, destinations}, + {fun parse_non_negative_integer/1, prefetch_count}, + {fun parse_ack_mode/1, ack_mode}, + {fun parse_binary/1, queue}, + make_parse_publish(publish_fields), + make_parse_publish(publish_properties), + {fun parse_non_negative_number/1, reconnect_delay}]], + #shovel{}), + case dict:find(add_forward_headers, Dict) of + {ok, true} -> add_forward_headers_fun(Name, Cfg); + _ -> Cfg + end. + +%% --=: Plain state monad implementation start :=-- +run_state_monad(FunList, State) -> + lists:foldl(fun (Fun, StateN) -> Fun(StateN) end, State, FunList). + +return(V) -> V. + +fail(Reason) -> throw({error, Reason}). +%% --=: end :=-- + +parse_endpoint({Endpoint, Pos}) when is_list(Endpoint) -> + Brokers = case proplists:get_value(brokers, Endpoint) of + undefined -> + case proplists:get_value(broker, Endpoint) of + undefined -> fail({missing_endpoint_parameter, + broker_or_brokers}); + B -> [B] + end; + Bs when is_list(Bs) -> + Bs; + B -> + fail({expected_list, brokers, B}) + end, + {[], Brokers1} = run_state_monad( + lists:duplicate(length(Brokers), + fun check_uri/1), + {Brokers, []}), + + ResourceDecls = + case proplists:get_value(declarations, Endpoint, []) of + Decls when is_list(Decls) -> + Decls; + Decls -> + fail({expected_list, declarations, Decls}) + end, + {[], ResourceDecls1} = + run_state_monad( + lists:duplicate(length(ResourceDecls), fun parse_declaration/1), + {ResourceDecls, []}), + + DeclareFun = + fun (_Conn, Ch) -> + [amqp_channel:call(Ch, M) || M <- lists:reverse(ResourceDecls1)] + end, + return({#endpoint{uris = Brokers1, + resource_declaration = DeclareFun}, + Pos}); +parse_endpoint({Endpoint, _Pos}) -> + fail({require_list, Endpoint}). + +check_uri({[Uri | Uris], Acc}) -> + case amqp_uri:parse(Uri) of + {ok, _Params} -> + return({Uris, [Uri | Acc]}); + {error, _} = Err -> + throw(Err) + end. + +parse_declaration({[{Method, Props} | Rest], Acc}) when is_list(Props) -> + FieldNames = try rabbit_framing_amqp_0_9_1:method_fieldnames(Method) + catch exit:Reason -> fail(Reason) + end, + case proplists:get_keys(Props) -- FieldNames of + [] -> ok; + UnknownFields -> fail({unknown_fields, Method, UnknownFields}) + end, + {Res, _Idx} = lists:foldl( + fun (K, {R, Idx}) -> + NewR = case proplists:get_value(K, Props) of + undefined -> R; + V -> setelement(Idx, R, V) + end, + {NewR, Idx + 1} + end, {rabbit_framing_amqp_0_9_1:method_record(Method), 2}, + FieldNames), + return({Rest, [Res | Acc]}); +parse_declaration({[{Method, Props} | _Rest], _Acc}) -> + fail({expected_method_field_list, Method, Props}); +parse_declaration({[Method | Rest], Acc}) -> + parse_declaration({[{Method, []} | Rest], Acc}). + +parse_non_negative_integer({N, Pos}) when is_integer(N) andalso N >= 0 -> + return({N, Pos}); +parse_non_negative_integer({N, _Pos}) -> + fail({require_non_negative_integer, N}). + +parse_non_negative_number({N, Pos}) when is_number(N) andalso N >= 0 -> + return({N, Pos}); +parse_non_negative_number({N, _Pos}) -> + fail({require_non_negative_number, N}). + +parse_binary({Binary, Pos}) when is_binary(Binary) -> + return({Binary, Pos}); +parse_binary({NotABinary, _Pos}) -> + fail({require_binary, NotABinary}). + +parse_ack_mode({Val, Pos}) when Val =:= no_ack orelse + Val =:= on_publish orelse + Val =:= on_confirm -> + return({Val, Pos}); +parse_ack_mode({WrongVal, _Pos}) -> + fail({ack_mode_value_requires_one_of, {no_ack, on_publish, on_confirm}, + WrongVal}). + +make_parse_publish(publish_fields) -> + {make_parse_publish1(record_info(fields, 'basic.publish')), publish_fields}; +make_parse_publish(publish_properties) -> + {make_parse_publish1(record_info(fields, 'P_basic')), publish_properties}. + +make_parse_publish1(ValidFields) -> + fun ({Fields, Pos}) when is_list(Fields) -> + make_publish_fun(Fields, Pos, ValidFields); + ({Fields, _Pos}) -> + fail({require_list, Fields}) + end. + +make_publish_fun(Fields, Pos, ValidFields) -> + SuppliedFields = proplists:get_keys(Fields), + case SuppliedFields -- ValidFields of + [] -> + FieldIndices = make_field_indices(ValidFields, Fields), + Fun = fun (_SrcUri, _DestUri, Publish) -> + lists:foldl(fun ({Pos1, Value}, Pub) -> + setelement(Pos1, Pub, Value) + end, Publish, FieldIndices) + end, + return({Fun, Pos}); + Unexpected -> + fail({unexpected_fields, Unexpected, ValidFields}) + end. + +make_field_indices(Valid, Fields) -> + make_field_indices(Fields, field_map(Valid, 2), []). + +make_field_indices([], _Idxs , Acc) -> + lists:reverse(Acc); +make_field_indices([{Key, Value} | Rest], Idxs, Acc) -> + make_field_indices(Rest, Idxs, [{dict:fetch(Key, Idxs), Value} | Acc]). + +field_map(Fields, Idx0) -> + {Dict, _IdxMax} = + lists:foldl(fun (Field, {Dict1, Idx1}) -> + {dict:store(Field, Idx1, Dict1), Idx1 + 1} + end, {dict:new(), Idx0}, Fields), + Dict. + +duplicate_keys(PropList) -> + proplists:get_keys( + lists:foldl(fun (K, L) -> lists:keydelete(K, 1, L) end, PropList, + proplists:get_keys(PropList))). + +add_forward_headers_fun(Name, #shovel{publish_properties = PubProps} = Cfg) -> + PubProps2 = + fun(SrcUri, DestUri, Props) -> + rabbit_shovel_util:update_headers( + [{<<"shovelled-by">>, rabbit_nodes:cluster_name()}, + {<<"shovel-type">>, <<"static">>}, + {<<"shovel-name">>, list_to_binary(atom_to_list(Name))}], + [], SrcUri, DestUri, PubProps(SrcUri, DestUri, Props)) + end, + Cfg#shovel{publish_properties = PubProps2}. diff --git a/rabbitmq-server/plugins-src/rabbitmq-shovel/src/rabbit_shovel_dyn_worker_sup.erl b/rabbitmq-server/plugins-src/rabbitmq-shovel/src/rabbit_shovel_dyn_worker_sup.erl new file mode 100644 index 0000000..0e5991d --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-shovel/src/rabbit_shovel_dyn_worker_sup.erl @@ -0,0 +1,40 @@ +%% The contents of this file are subject to the Mozilla Public License +%% Version 1.1 (the "License"); you may not use this file except in +%% compliance with the License. You may obtain a copy of the License +%% at http://www.mozilla.org/MPL/ +%% +%% Software distributed under the License is distributed on an "AS IS" +%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See +%% the License for the specific language governing rights and +%% limitations under the License. +%% +%% The Original Code is RabbitMQ. +%% +%% The Initial Developer of the Original Code is GoPivotal, Inc. +%% Copyright (c) 2007-2015 Pivotal Software, Inc. All rights reserved. +%% + +-module(rabbit_shovel_dyn_worker_sup). +-behaviour(supervisor2). + +-export([start_link/2, init/1]). + +-import(rabbit_misc, [pget/3]). + +-include_lib("rabbit_common/include/rabbit.hrl"). +-define(SUPERVISOR, ?MODULE). + +start_link(Name, Config) -> + supervisor2:start_link(?MODULE, [Name, Config]). + +%%---------------------------------------------------------------------------- + +init([Name, Config]) -> + {ok, {{one_for_one, 1, ?MAX_WAIT}, + [{Name, + {rabbit_shovel_worker, start_link, [dynamic, Name, Config]}, + case pget(<<"reconnect-delay">>, Config, 1) of + N when is_integer(N) andalso N > 0 -> {transient, N}; + _ -> temporary + end, + 16#ffffffff, worker, [rabbit_shovel_worker]}]}}. diff --git a/rabbitmq-server/plugins-src/rabbitmq-shovel/src/rabbit_shovel_dyn_worker_sup_sup.erl b/rabbitmq-server/plugins-src/rabbitmq-shovel/src/rabbit_shovel_dyn_worker_sup_sup.erl new file mode 100644 index 0000000..38bbd50 --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-shovel/src/rabbit_shovel_dyn_worker_sup_sup.erl @@ -0,0 +1,75 @@ +%% The contents of this file are subject to the Mozilla Public License +%% Version 1.1 (the "License"); you may not use this file except in +%% compliance with the License. You may obtain a copy of the License +%% at http://www.mozilla.org/MPL/ +%% +%% Software distributed under the License is distributed on an "AS IS" +%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See +%% the License for the specific language governing rights and +%% limitations under the License. +%% +%% The Original Code is RabbitMQ. +%% +%% The Initial Developer of the Original Code is GoPivotal, Inc. +%% Copyright (c) 2007-2015 Pivotal Software, Inc. All rights reserved. +%% + +-module(rabbit_shovel_dyn_worker_sup_sup). +-behaviour(mirrored_supervisor). + +-export([start_link/0, init/1, adjust/2, stop_child/1]). + +-import(rabbit_misc, [pget/2]). + +-include_lib("rabbit_common/include/rabbit.hrl"). +-define(SUPERVISOR, ?MODULE). + +start_link() -> + {ok, Pid} = mirrored_supervisor:start_link( + {local, ?SUPERVISOR}, ?SUPERVISOR, + fun rabbit_misc:execute_mnesia_transaction/1, ?MODULE, []), + Shovels = rabbit_runtime_parameters:list_component(<<"shovel">>), + [start_child({pget(vhost, Shovel), pget(name, Shovel)}, + pget(value, Shovel)) || Shovel <- Shovels], + {ok, Pid}. + +adjust(Name, Def) -> + case child_exists(Name) of + true -> stop_child(Name); + false -> ok + end, + start_child(Name, Def). + +start_child(Name, Def) -> + case mirrored_supervisor:start_child( + ?SUPERVISOR, + {Name, {rabbit_shovel_dyn_worker_sup, start_link, [Name, Def]}, + transient, ?MAX_WAIT, worker, [rabbit_shovel_dyn_worker_sup]}) of + {ok, _Pid} -> ok; + {error, {already_started, _Pid}} -> ok + end. + +child_exists(Name) -> + lists:any(fun ({N, _, _, _}) -> N =:= Name end, + mirrored_supervisor:which_children(?SUPERVISOR)). + +stop_child(Name) -> + case get(shovel_worker_autodelete) of + true -> ok; %% [1] + _ -> ok = mirrored_supervisor:terminate_child(?SUPERVISOR, Name), + ok = mirrored_supervisor:delete_child(?SUPERVISOR, Name), + rabbit_shovel_status:remove(Name) + end. + +%% [1] An autodeleting worker removes its own parameter, and thus ends +%% up here via the parameter callback. It is a transient worker that +%% is just about to terminate normally - so we don't need to tell the +%% supervisor to stop us - and as usual if we call into our own +%% supervisor we risk deadlock. +%% +%% See rabbit_shovel_worker:maybe_autodelete/1 + +%%---------------------------------------------------------------------------- + +init([]) -> + {ok, {{one_for_one, 3, 10}, []}}. diff --git a/rabbitmq-server/plugins-src/rabbitmq-shovel/src/rabbit_shovel_parameters.erl b/rabbitmq-server/plugins-src/rabbitmq-shovel/src/rabbit_shovel_parameters.erl new file mode 100644 index 0000000..49bbcac --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-shovel/src/rabbit_shovel_parameters.erl @@ -0,0 +1,278 @@ +%% The contents of this file are subject to the Mozilla Public License +%% Version 1.1 (the "License"); you may not use this file except in +%% compliance with the License. You may obtain a copy of the License +%% at http://www.mozilla.org/MPL/ +%% +%% Software distributed under the License is distributed on an "AS IS" +%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See +%% the License for the specific language governing rights and +%% limitations under the License. +%% +%% The Original Code is RabbitMQ. +%% +%% The Initial Developer of the Original Code is GoPivotal, Inc. +%% Copyright (c) 2007-2015 Pivotal Software, Inc. All rights reserved. +%% + +-module(rabbit_shovel_parameters). +-behaviour(rabbit_runtime_parameter). + +-include_lib("amqp_client/include/amqp_client.hrl"). +-include("rabbit_shovel.hrl"). + +-export([validate/5, notify/4, notify_clear/3]). +-export([register/0, unregister/0, parse/2]). + +-import(rabbit_misc, [pget/2, pget/3]). + +-rabbit_boot_step({?MODULE, + [{description, "shovel parameters"}, + {mfa, {rabbit_shovel_parameters, register, []}}, + {cleanup, {?MODULE, unregister, []}}, + {requires, rabbit_registry}, + {enables, recovery}]}). + +register() -> + rabbit_registry:register(runtime_parameter, <<"shovel">>, ?MODULE). + +unregister() -> + rabbit_registry:unregister(runtime_parameter, <<"shovel">>). + +validate(_VHost, <<"shovel">>, Name, Def, User) -> + [case pget2(<<"src-exchange">>, <<"src-queue">>, Def) of + zero -> {error, "Must specify 'src-exchange' or 'src-queue'", []}; + one -> ok; + both -> {error, "Cannot specify 'src-exchange' and 'src-queue'", []} + end, + case pget2(<<"dest-exchange">>, <<"dest-queue">>, Def) of + zero -> ok; + one -> ok; + both -> {error, "Cannot specify 'dest-exchange' and 'dest-queue'", []} + end, + case {pget(<<"delete-after">>, Def), pget(<<"ack-mode">>, Def)} of + {N, <<"no-ack">>} when is_integer(N) -> + {error, "Cannot specify 'no-ack' and numerical 'delete-after'", []}; + _ -> + ok + end | rabbit_parameter_validation:proplist(Name, validation(User), Def)]; + +validate(_VHost, _Component, Name, _Term, _User) -> + {error, "name not recognised: ~p", [Name]}. + +pget2(K1, K2, Defs) -> case {pget(K1, Defs), pget(K2, Defs)} of + {undefined, undefined} -> zero; + {undefined, _} -> one; + {_, undefined} -> one; + {_, _} -> both + end. + +notify(VHost, <<"shovel">>, Name, Definition) -> + rabbit_shovel_dyn_worker_sup_sup:adjust({VHost, Name}, Definition). + +notify_clear(VHost, <<"shovel">>, Name) -> + rabbit_shovel_dyn_worker_sup_sup:stop_child({VHost, Name}). + +%%---------------------------------------------------------------------------- + +validation(User) -> + [{<<"src-uri">>, validate_uri_fun(User), mandatory}, + {<<"dest-uri">>, validate_uri_fun(User), mandatory}, + {<<"src-exchange">>, fun rabbit_parameter_validation:binary/2,optional}, + {<<"src-exchange-key">>,fun rabbit_parameter_validation:binary/2,optional}, + {<<"src-queue">>, fun rabbit_parameter_validation:binary/2,optional}, + {<<"dest-exchange">>, fun rabbit_parameter_validation:binary/2,optional}, + {<<"dest-exchange-key">>,fun rabbit_parameter_validation:binary/2,optional}, + {<<"dest-queue">>, fun rabbit_parameter_validation:binary/2,optional}, + {<<"prefetch-count">>, fun rabbit_parameter_validation:number/2,optional}, + {<<"reconnect-delay">>, fun rabbit_parameter_validation:number/2,optional}, + {<<"add-forward-headers">>, fun rabbit_parameter_validation:boolean/2,optional}, + {<<"publish-properties">>, fun validate_properties/2, optional}, + {<<"ack-mode">>, rabbit_parameter_validation:enum( + ['no-ack', 'on-publish', 'on-confirm']), optional}, + {<<"delete-after">>, fun validate_delete_after/2, optional} + ]. + +validate_uri_fun(User) -> + fun (Name, Term) -> validate_uri(Name, Term, User) end. + +validate_uri(Name, Term, User) when is_binary(Term) -> + case rabbit_parameter_validation:binary(Name, Term) of + ok -> case amqp_uri:parse(binary_to_list(Term)) of + {ok, P} -> validate_params_user(P, User); + {error, E} -> {error, "\"~s\" not a valid URI: ~p", [Term, E]} + end; + E -> E + end; +validate_uri(Name, Term, User) -> + case rabbit_parameter_validation:list(Name, Term) of + ok -> case [V || URI <- Term, + V <- [validate_uri(Name, URI, User)], + element(1, V) =:= error] of + [] -> ok; + [E | _] -> E + end; + E -> E + end. + +validate_params_user(#amqp_params_direct{}, none) -> + ok; +validate_params_user(#amqp_params_direct{virtual_host = VHost}, + User = #user{username = Username}) -> + case rabbit_vhost:exists(VHost) andalso + (catch rabbit_access_control:check_vhost_access( + User, VHost, undefined)) of + ok -> ok; + _ -> {error, "user \"~s\" may not connect to vhost \"~s\"", + [Username, VHost]} + end; +validate_params_user(#amqp_params_network{}, _User) -> + ok. + +validate_delete_after(_Name, <<"never">>) -> ok; +validate_delete_after(_Name, <<"queue-length">>) -> ok; +validate_delete_after(_Name, N) when is_integer(N) -> ok; +validate_delete_after(Name, Term) -> + {error, "~s should be number, \"never\" or \"queue-length\", actually was " + "~p", [Name, Term]}. + +%% TODO headers? +validate_properties(Name, Term) -> + Str = fun rabbit_parameter_validation:binary/2, + Num = fun rabbit_parameter_validation:number/2, + rabbit_parameter_validation:proplist( + Name, [{<<"content_type">>, Str, optional}, + {<<"content_encoding">>, Str, optional}, + {<<"delivery_mode">>, Num, optional}, + {<<"priority">>, Num, optional}, + {<<"correlation_id">>, Str, optional}, + {<<"reply_to">>, Str, optional}, + {<<"expiration">>, Str, optional}, + {<<"message_id">>, Str, optional}, + {<<"timestamp">>, Num, optional}, + {<<"type">>, Str, optional}, + {<<"user_id">>, Str, optional}, + {<<"app_id">>, Str, optional}, + {<<"cluster_id">>, Str, optional}], Term). + +%%---------------------------------------------------------------------------- + +parse({VHost, Name}, Def) -> + SrcURIs = get_uris(<<"src-uri">>, Def), + DestURIs = get_uris(<<"dest-uri">>, Def), + SrcX = pget(<<"src-exchange">>, Def, none), + SrcXKey = pget(<<"src-exchange-key">>, Def, <<>>), %% [1] + SrcQ = pget(<<"src-queue">>, Def, none), + DestX = pget(<<"dest-exchange">>, Def, none), + DestXKey = pget(<<"dest-exchange-key">>, Def, none), + DestQ = pget(<<"dest-queue">>, Def, none), + %% [1] src-exchange-key is never ignored if src-exchange is set + {SrcFun, Queue, Table1} = + case SrcQ of + none -> {fun (_Conn, Ch) -> + Ms = [#'queue.declare'{exclusive = true}, + #'queue.bind'{routing_key = SrcXKey, + exchange = SrcX}], + [amqp_channel:call(Ch, M) || M <- Ms] + end, <<>>, [{<<"src-exchange">>, SrcX}, + {<<"src-exchange-key">>, SrcXKey}]}; + _ -> {fun (Conn, _Ch) -> + ensure_queue(Conn, SrcQ) + end, SrcQ, [{<<"src-queue">>, SrcQ}]} + end, + DestFun = fun (Conn, _Ch) -> + case DestQ of + none -> ok; + _ -> ensure_queue(Conn, DestQ) + end + end, + {X, Key} = case DestQ of + none -> {DestX, DestXKey}; + _ -> {<<>>, DestQ} + end, + Table2 = [{K, V} || {K, V} <- [{<<"dest-exchange">>, DestX}, + {<<"dest-exchange-key">>, DestXKey}, + {<<"dest-queue">>, DestQ}], + V =/= none], + PubFun = fun (_SrcURI, _DestURI, P0) -> + P1 = case X of + none -> P0; + _ -> P0#'basic.publish'{exchange = X} + end, + case Key of + none -> P1; + _ -> P1#'basic.publish'{routing_key = Key} + end + end, + AddHeaders = pget(<<"add-forward-headers">>, Def, false), + Table0 = [{<<"shovelled-by">>, rabbit_nodes:cluster_name()}, + {<<"shovel-type">>, <<"dynamic">>}, + {<<"shovel-name">>, Name}, + {<<"shovel-vhost">>, VHost}], + SetProps = lookup_indices(pget(<<"publish-properties">>, Def, []), + record_info(fields, 'P_basic')), + PubPropsFun = fun (SrcURI, DestURI, P0) -> + P = set_properties(P0, SetProps), + case AddHeaders of + true -> rabbit_shovel_util:update_headers( + Table0, Table1 ++ Table2, + SrcURI, DestURI, P); + false -> P + end + end, + {ok, #shovel{ + sources = #endpoint{uris = SrcURIs, + resource_declaration = SrcFun}, + destinations = #endpoint{uris = DestURIs, + resource_declaration = DestFun}, + prefetch_count = pget(<<"prefetch-count">>, Def, 1000), + ack_mode = translate_ack_mode( + pget(<<"ack-mode">>, Def, <<"on-confirm">>)), + publish_fields = PubFun, + publish_properties = PubPropsFun, + queue = Queue, + reconnect_delay = pget(<<"reconnect-delay">>, Def, 1), + delete_after = opt_b2a(pget(<<"delete-after">>, Def, <<"never">>)) + }}. + +get_uris(Key, Def) -> + URIs = case pget(Key, Def) of + B when is_binary(B) -> [B]; + L when is_list(L) -> L + end, + [binary_to_list(URI) || URI <- URIs]. + +translate_ack_mode(<<"on-confirm">>) -> on_confirm; +translate_ack_mode(<<"on-publish">>) -> on_publish; +translate_ack_mode(<<"no-ack">>) -> no_ack. + +ensure_queue(Conn, Queue) -> + {ok, Ch} = amqp_connection:open_channel(Conn), + try + amqp_channel:call(Ch, #'queue.declare'{queue = Queue, + passive = true}) + catch exit:{{shutdown, {server_initiated_close, ?NOT_FOUND, _Text}}, _} -> + {ok, Ch2} = amqp_connection:open_channel(Conn), + amqp_channel:call(Ch2, #'queue.declare'{queue = Queue, + durable = true}), + catch amqp_channel:close(Ch2) + + after + catch amqp_channel:close(Ch) + end. + +opt_b2a(B) when is_binary(B) -> list_to_atom(binary_to_list(B)); +opt_b2a(N) -> N. + +set_properties(Props, []) -> + Props; +set_properties(Props, [{Ix, V} | Rest]) -> + set_properties(setelement(Ix, Props, V), Rest). + +lookup_indices(KVs, L) -> + [{1 + list_find(list_to_atom(binary_to_list(K)), L), V} || {K, V} <- KVs]. + +list_find(K, L) -> list_find(K, L, 1). + +list_find(K, [K|_], N) -> N; +list_find(K, [], _N) -> exit({not_found, K}); +list_find(K, [_|L], N) -> list_find(K, L, N + 1). diff --git a/rabbitmq-server/plugins-src/rabbitmq-shovel/src/rabbit_shovel_status.erl b/rabbitmq-server/plugins-src/rabbitmq-shovel/src/rabbit_shovel_status.erl new file mode 100644 index 0000000..37d738e --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-shovel/src/rabbit_shovel_status.erl @@ -0,0 +1,84 @@ +%% The contents of this file are subject to the Mozilla Public License +%% Version 1.1 (the "License"); you may not use this file except in +%% compliance with the License. You may obtain a copy of the License +%% at http://www.mozilla.org/MPL/ +%% +%% Software distributed under the License is distributed on an "AS IS" +%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See +%% the License for the specific language governing rights and +%% limitations under the License. +%% +%% The Original Code is RabbitMQ. +%% +%% The Initial Developer of the Original Code is GoPivotal, Inc. +%% Copyright (c) 2007-2015 Pivotal Software, Inc. All rights reserved. +%% + +-module(rabbit_shovel_status). +-behaviour(gen_server). + +-export([start_link/0]). + +-export([report/3, remove/1, status/0]). + +-export([init/1, handle_call/3, handle_cast/2, handle_info/2, + terminate/2, code_change/3]). + +-define(SERVER, ?MODULE). +-define(ETS_NAME, ?MODULE). + +-record(state, {}). +-record(entry, {name, type, info, timestamp}). + +start_link() -> + gen_server:start_link({local, ?SERVER}, ?MODULE, [], []). + +report(Name, Type, Info) -> + gen_server:cast(?SERVER, {report, Name, Type, Info, calendar:local_time()}). + +remove(Name) -> + gen_server:cast(?SERVER, {remove, Name}). + +status() -> + gen_server:call(?SERVER, status, infinity). + +init([]) -> + ?ETS_NAME = ets:new(?ETS_NAME, + [named_table, {keypos, #entry.name}, private]), + {ok, #state{}}. + +handle_call(status, _From, State) -> + Entries = ets:tab2list(?ETS_NAME), + {reply, [{Entry#entry.name, Entry#entry.type, Entry#entry.info, + Entry#entry.timestamp} + || Entry <- Entries], State}. + +handle_cast({report, Name, Type, Info, Timestamp}, State) -> + true = ets:insert(?ETS_NAME, #entry{name = Name, type = Type, info = Info, + timestamp = Timestamp}), + rabbit_event:notify(shovel_worker_status, + split_name(Name) ++ split_status(Info)), + {noreply, State}; + +handle_cast({remove, Name}, State) -> + true = ets:delete(?ETS_NAME, Name), + rabbit_event:notify(shovel_worker_removed, split_name(Name)), + {noreply, State}. + +handle_info(_Info, State) -> + {noreply, State}. + +terminate(_Reason, _State) -> + ok. + +code_change(_OldVsn, State, _Extra) -> + {ok, State}. + +split_status({running, MoreInfo}) -> [{status, running} | MoreInfo]; +split_status({terminated, Reason}) -> [{status, terminated}, + {reason, Reason}]; +split_status(Status) when is_atom(Status) -> [{status, Status}]. + +split_name({VHost, Name}) -> [{name, Name}, + {vhost, VHost}]; +split_name(Name) when is_atom(Name) -> [{name, Name}]. diff --git a/rabbitmq-server/plugins-src/rabbitmq-shovel/src/rabbit_shovel_sup.erl b/rabbitmq-server/plugins-src/rabbitmq-shovel/src/rabbit_shovel_sup.erl new file mode 100644 index 0000000..b0bce9d --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-shovel/src/rabbit_shovel_sup.erl @@ -0,0 +1,87 @@ +%% The contents of this file are subject to the Mozilla Public License +%% Version 1.1 (the "License"); you may not use this file except in +%% compliance with the License. You may obtain a copy of the License +%% at http://www.mozilla.org/MPL/ +%% +%% Software distributed under the License is distributed on an "AS IS" +%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See +%% the License for the specific language governing rights and +%% limitations under the License. +%% +%% The Original Code is RabbitMQ. +%% +%% The Initial Developer of the Original Code is GoPivotal, Inc. +%% Copyright (c) 2007-2015 Pivotal Software, Inc. All rights reserved. +%% + +-module(rabbit_shovel_sup). +-behaviour(supervisor2). + +-export([start_link/0, init/1]). + +-import(rabbit_shovel_config, [ensure_defaults/2]). + +-include("rabbit_shovel.hrl"). + +start_link() -> + case parse_configuration(application:get_env(shovels)) of + {ok, Configurations} -> + supervisor2:start_link({local, ?MODULE}, ?MODULE, [Configurations]); + {error, Reason} -> + {error, Reason} + end. + +init([Configurations]) -> + Len = dict:size(Configurations), + ChildSpecs = [{rabbit_shovel_status, + {rabbit_shovel_status, start_link, []}, + transient, 16#ffffffff, worker, + [rabbit_shovel_status]}, + {rabbit_shovel_dyn_worker_sup_sup, + {rabbit_shovel_dyn_worker_sup_sup, start_link, []}, + transient, 16#ffffffff, supervisor, + [rabbit_shovel_dyn_worker_sup_sup]} | + make_child_specs(Configurations)], + {ok, {{one_for_one, 2*Len, 2}, ChildSpecs}}. + +make_child_specs(Configurations) -> + dict:fold( + fun (ShovelName, ShovelConfig, Acc) -> + [{ShovelName, + {rabbit_shovel_worker_sup, start_link, + [ShovelName, ShovelConfig]}, + permanent, + 16#ffffffff, + supervisor, + [rabbit_shovel_worker_sup]} | Acc] + end, [], Configurations). + +parse_configuration(undefined) -> + {ok, dict:new()}; +parse_configuration({ok, Env}) -> + {ok, Defaults} = application:get_env(defaults), + parse_configuration(Defaults, Env, dict:new()). + +parse_configuration(_Defaults, [], Acc) -> + {ok, Acc}; +parse_configuration(Defaults, [{ShovelName, ShovelConfig} | Env], Acc) + when is_atom(ShovelName) andalso is_list(ShovelConfig) -> + case dict:is_key(ShovelName, Acc) of + true -> {error, {duplicate_shovel_definition, ShovelName}}; + false -> case validate_shovel_config(ShovelName, ShovelConfig) of + {ok, Shovel} -> + %% make sure the config we accumulate has any + %% relevant default values (discovered during + %% validation), applied back to it + UpdatedConfig = ensure_defaults(ShovelConfig, Shovel), + Acc2 = dict:store(ShovelName, UpdatedConfig, Acc), + parse_configuration(Defaults, Env, Acc2); + Error -> + Error + end + end; +parse_configuration(_Defaults, _, _Acc) -> + {error, require_list_of_shovel_configurations}. + +validate_shovel_config(ShovelName, ShovelConfig) -> + rabbit_shovel_config:parse(ShovelName, ShovelConfig). diff --git a/rabbitmq-server/plugins-src/rabbitmq-shovel/src/rabbit_shovel_util.erl b/rabbitmq-server/plugins-src/rabbitmq-shovel/src/rabbit_shovel_util.erl new file mode 100644 index 0000000..a3b0f9c --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-shovel/src/rabbit_shovel_util.erl @@ -0,0 +1,32 @@ +%% The contents of this file are subject to the Mozilla Public License +%% Version 1.1 (the "License"); you may not use this file except in +%% compliance with the License. You may obtain a copy of the License +%% at http://www.mozilla.org/MPL/ +%% +%% Software distributed under the License is distributed on an "AS IS" +%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See +%% the License for the specific language governing rights and +%% limitations under the License. +%% +%% The Original Code is RabbitMQ. +%% +%% The Initial Developer of the Original Code is GoPivotal, Inc. +%% Copyright (c) 2007-2015 Pivotal Software, Inc. All rights reserved. +%% + +-module(rabbit_shovel_util). + +-export([update_headers/5]). + +-include_lib("rabbit_common/include/rabbit_framing.hrl"). + +-define(ROUTING_HEADER, <<"x-shovelled">>). + +update_headers(Prefix, Suffix, SrcURI, DestURI, + Props = #'P_basic'{headers = Headers}) -> + Table = Prefix ++ [{<<"src-uri">>, SrcURI}, + {<<"dest-uri">>, DestURI}] ++ Suffix, + Headers2 = rabbit_basic:prepend_table_header( + ?ROUTING_HEADER, [{K, longstr, V} || {K, V} <- Table], + Headers), + Props#'P_basic'{headers = Headers2}. diff --git a/rabbitmq-server/plugins-src/rabbitmq-shovel/src/rabbit_shovel_worker.erl b/rabbitmq-server/plugins-src/rabbitmq-shovel/src/rabbit_shovel_worker.erl new file mode 100644 index 0000000..e5a8f63 --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-shovel/src/rabbit_shovel_worker.erl @@ -0,0 +1,268 @@ +%% The contents of this file are subject to the Mozilla Public License +%% Version 1.1 (the "License"); you may not use this file except in +%% compliance with the License. You may obtain a copy of the License +%% at http://www.mozilla.org/MPL/ +%% +%% Software distributed under the License is distributed on an "AS IS" +%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See +%% the License for the specific language governing rights and +%% limitations under the License. +%% +%% The Original Code is RabbitMQ. +%% +%% The Initial Developer of the Original Code is GoPivotal, Inc. +%% Copyright (c) 2007-2015 Pivotal Software, Inc. All rights reserved. +%% + +-module(rabbit_shovel_worker). +-behaviour(gen_server2). + +-export([start_link/3]). +-export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, + code_change/3]). + +-include_lib("amqp_client/include/amqp_client.hrl"). +-include("rabbit_shovel.hrl"). + +-define(MAX_CONNECTION_CLOSE_TIMEOUT, 10000). + +-record(state, {inbound_conn, inbound_ch, outbound_conn, outbound_ch, + name, type, config, inbound_uri, outbound_uri, unacked, + remaining, %% [1] + remaining_unacked}). %% [2] + +%% [1] Counts down until we shut down in all modes +%% [2] Counts down until we stop publishing in on-confirm mode + +start_link(Type, Name, Config) -> + ok = rabbit_shovel_status:report(Name, Type, starting), + gen_server2:start_link(?MODULE, [Type, Name, Config], []). + +%%--------------------------- +%% Gen Server Implementation +%%--------------------------- + +init([Type, Name, Config]) -> + gen_server2:cast(self(), init), + {ok, Shovel} = parse(Type, Name, Config), + {ok, #state{name = Name, type = Type, config = Shovel}}. + +parse(static, Name, Config) -> rabbit_shovel_config:parse(Name, Config); +parse(dynamic, Name, Config) -> rabbit_shovel_parameters:parse(Name, Config). + +handle_call(_Msg, _From, State) -> + {noreply, State}. + +handle_cast(init, State = #state{config = Config}) -> + random:seed(now()), + #shovel{sources = Sources, destinations = Destinations} = Config, + {InboundConn, InboundChan, InboundURI} = + make_conn_and_chan(Sources#endpoint.uris), + {OutboundConn, OutboundChan, OutboundURI} = + make_conn_and_chan(Destinations#endpoint.uris), + + %% Don't trap exits until we have established connections so that + %% if we try to shut down while waiting for a connection to be + %% established then we don't block + process_flag(trap_exit, true), + + (Sources#endpoint.resource_declaration)(InboundConn, InboundChan), + (Destinations#endpoint.resource_declaration)(OutboundConn, OutboundChan), + + NoAck = Config#shovel.ack_mode =:= no_ack, + case NoAck of + false -> Prefetch = Config#shovel.prefetch_count, + #'basic.qos_ok'{} = + amqp_channel:call( + InboundChan, #'basic.qos'{prefetch_count = Prefetch}); + true -> ok + end, + + case Config#shovel.ack_mode of + on_confirm -> + #'confirm.select_ok'{} = + amqp_channel:call(OutboundChan, #'confirm.select'{}), + ok = amqp_channel:register_confirm_handler(OutboundChan, self()); + _ -> + ok + end, + + Remaining = remaining(InboundChan, Config), + case Remaining of + 0 -> exit({shutdown, autodelete}); + _ -> ok + end, + + #'basic.consume_ok'{} = + amqp_channel:subscribe( + InboundChan, #'basic.consume'{queue = Config#shovel.queue, + no_ack = NoAck}, + self()), + + State1 = + State#state{inbound_conn = InboundConn, inbound_ch = InboundChan, + outbound_conn = OutboundConn, outbound_ch = OutboundChan, + inbound_uri = InboundURI, + outbound_uri = OutboundURI, + remaining = Remaining, + remaining_unacked = Remaining, + unacked = gb_trees:empty()}, + ok = report_running(State1), + {noreply, State1}. + +handle_info(#'basic.consume_ok'{}, State) -> + {noreply, State}; + +handle_info({#'basic.deliver'{delivery_tag = Tag, + exchange = Exchange, routing_key = RoutingKey}, + Msg = #amqp_msg{props = Props = #'P_basic'{}}}, + State = #state{inbound_uri = InboundURI, + outbound_uri = OutboundURI, + config = #shovel{publish_properties = PropsFun, + publish_fields = FieldsFun}}) -> + Method = #'basic.publish'{exchange = Exchange, routing_key = RoutingKey}, + Method1 = FieldsFun(InboundURI, OutboundURI, Method), + Msg1 = Msg#amqp_msg{props = PropsFun(InboundURI, OutboundURI, Props)}, + {noreply, publish(Tag, Method1, Msg1, State)}; + +handle_info(#'basic.ack'{delivery_tag = Seq, multiple = Multiple}, + State = #state{config = #shovel{ack_mode = on_confirm}}) -> + {noreply, confirm_to_inbound( + fun (DTag, Multi) -> + #'basic.ack'{delivery_tag = DTag, multiple = Multi} + end, Seq, Multiple, State)}; + +handle_info(#'basic.nack'{delivery_tag = Seq, multiple = Multiple}, + State = #state{config = #shovel{ack_mode = on_confirm}}) -> + {noreply, confirm_to_inbound( + fun (DTag, Multi) -> + #'basic.nack'{delivery_tag = DTag, multiple = Multi} + end, Seq, Multiple, State)}; + +handle_info(#'basic.cancel'{}, State = #state{name = Name}) -> + rabbit_log:warning("Shovel ~p received 'basic.cancel' from the broker~n", + [Name]), + {stop, {shutdown, restart}, State}; + +handle_info({'EXIT', InboundConn, Reason}, + State = #state{inbound_conn = InboundConn}) -> + {stop, {inbound_conn_died, Reason}, State}; + +handle_info({'EXIT', OutboundConn, Reason}, + State = #state{outbound_conn = OutboundConn}) -> + {stop, {outbound_conn_died, Reason}, State}. + +terminate(Reason, #state{inbound_conn = undefined, inbound_ch = undefined, + outbound_conn = undefined, outbound_ch = undefined, + name = Name, type = Type}) -> + rabbit_shovel_status:report(Name, Type, {terminated, Reason}), + ok; +terminate({shutdown, autodelete}, State = #state{name = {VHost, Name}, + type = dynamic}) -> + close_connections(State), + %% See rabbit_shovel_dyn_worker_sup_sup:stop_child/1 + put(shovel_worker_autodelete, true), + rabbit_runtime_parameters:clear(VHost, <<"shovel">>, Name), + rabbit_shovel_status:remove({VHost, Name}), + ok; +terminate(Reason, State) -> + close_connections(State), + rabbit_shovel_status:report(State#state.name, State#state.type, + {terminated, Reason}), + ok. + +code_change(_OldVsn, State, _Extra) -> + {ok, State}. + +%%--------------------------- +%% Helpers +%%--------------------------- + +confirm_to_inbound(MsgCtr, Seq, Multiple, State = + #state{inbound_ch = InboundChan, unacked = Unacked}) -> + ok = amqp_channel:cast( + InboundChan, MsgCtr(gb_trees:get(Seq, Unacked), Multiple)), + {Unacked1, Removed} = remove_delivery_tags(Seq, Multiple, Unacked, 0), + decr_remaining(Removed, State#state{unacked = Unacked1}). + +remove_delivery_tags(Seq, false, Unacked, 0) -> + {gb_trees:delete(Seq, Unacked), 1}; +remove_delivery_tags(Seq, true, Unacked, Count) -> + case gb_trees:is_empty(Unacked) of + true -> {Unacked, Count}; + false -> {Smallest, _Val, Unacked1} = gb_trees:take_smallest(Unacked), + case Smallest > Seq of + true -> {Unacked, Count}; + false -> remove_delivery_tags(Seq, true, Unacked1, Count+1) + end + end. + +report_running(State) -> + rabbit_shovel_status:report( + State#state.name, State#state.type, + {running, [{src_uri, State#state.inbound_uri}, + {dest_uri, State#state.outbound_uri}]}). + +publish(_Tag, _Method, _Msg, State = #state{remaining_unacked = 0}) -> + %% We are in on-confirm mode, and are autodelete. We have + %% published all the messages we need to; we just wait for acks to + %% come back. So drop subsequent messages on the floor to be + %% requeued later. + State; + +publish(Tag, Method, Msg, + State = #state{inbound_ch = InboundChan, outbound_ch = OutboundChan, + config = Config, unacked = Unacked}) -> + Seq = case Config#shovel.ack_mode of + on_confirm -> amqp_channel:next_publish_seqno(OutboundChan); + _ -> undefined + end, + ok = amqp_channel:call(OutboundChan, Method, Msg), + decr_remaining_unacked( + case Config#shovel.ack_mode of + no_ack -> decr_remaining(1, State); + on_confirm -> State#state{unacked = gb_trees:insert( + Seq, Tag, Unacked)}; + on_publish -> ok = amqp_channel:cast( + InboundChan, #'basic.ack'{delivery_tag = Tag}), + decr_remaining(1, State) + end). + +make_conn_and_chan(URIs) -> + URI = lists:nth(random:uniform(length(URIs)), URIs), + {ok, AmqpParam} = amqp_uri:parse(URI), + {ok, Conn} = amqp_connection:start(AmqpParam), + link(Conn), + {ok, Chan} = amqp_connection:open_channel(Conn), + {Conn, Chan, list_to_binary(amqp_uri:remove_credentials(URI))}. + +remaining(_Ch, #shovel{delete_after = never}) -> + unlimited; +remaining(Ch, #shovel{delete_after = 'queue-length', queue = Queue}) -> + #'queue.declare_ok'{message_count = N} = + amqp_channel:call(Ch, #'queue.declare'{queue = Queue, + passive = true}), + N; +remaining(_Ch, #shovel{delete_after = Count}) -> + Count. + +decr_remaining(_N, State = #state{remaining = unlimited}) -> + State; +decr_remaining(N, State = #state{remaining = M}) -> + case M > N of + true -> State#state{remaining = M - N}; + false -> exit({shutdown, autodelete}) + end. + +decr_remaining_unacked(State = #state{remaining_unacked = unlimited}) -> + State; +decr_remaining_unacked(State = #state{remaining_unacked = 0}) -> + State; +decr_remaining_unacked(State = #state{remaining_unacked = N}) -> + State#state{remaining_unacked = N - 1}. + +close_connections(State) -> + catch amqp_connection:close(State#state.inbound_conn, + ?MAX_CONNECTION_CLOSE_TIMEOUT), + catch amqp_connection:close(State#state.outbound_conn, + ?MAX_CONNECTION_CLOSE_TIMEOUT). diff --git a/rabbitmq-server/plugins-src/rabbitmq-shovel/src/rabbit_shovel_worker_sup.erl b/rabbitmq-server/plugins-src/rabbitmq-shovel/src/rabbit_shovel_worker_sup.erl new file mode 100644 index 0000000..1705d5f --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-shovel/src/rabbit_shovel_worker_sup.erl @@ -0,0 +1,40 @@ +%% The contents of this file are subject to the Mozilla Public License +%% Version 1.1 (the "License"); you may not use this file except in +%% compliance with the License. You may obtain a copy of the License +%% at http://www.mozilla.org/MPL/ +%% +%% Software distributed under the License is distributed on an "AS IS" +%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See +%% the License for the specific language governing rights and +%% limitations under the License. +%% +%% The Original Code is RabbitMQ. +%% +%% The Initial Developer of the Original Code is GoPivotal, Inc. +%% Copyright (c) 2007-2015 Pivotal Software, Inc. All rights reserved. +%% + +-module(rabbit_shovel_worker_sup). +-behaviour(mirrored_supervisor). + +-export([start_link/2, init/1]). + +-include("rabbit_shovel.hrl"). +-include_lib("rabbit_common/include/rabbit.hrl"). + +start_link(ShovelName, ShovelConfig) -> + mirrored_supervisor:start_link({local, ShovelName}, ShovelName, + fun rabbit_misc:execute_mnesia_transaction/1, + ?MODULE, [ShovelName, ShovelConfig]). + +init([Name, Config]) -> + ChildSpecs = [{Name, + {rabbit_shovel_worker, start_link, [static, Name, Config]}, + case proplists:get_value(reconnect_delay, Config, none) of + N when is_integer(N) andalso N > 0 -> {permanent, N}; + _ -> temporary + end, + 16#ffffffff, + worker, + [rabbit_shovel_worker]}], + {ok, {{one_for_one, 1, ?MAX_WAIT}, ChildSpecs}}. diff --git a/rabbitmq-server/plugins-src/rabbitmq-shovel/src/rabbitmq_shovel.app.src b/rabbitmq-server/plugins-src/rabbitmq-shovel/src/rabbitmq_shovel.app.src new file mode 100644 index 0000000..895aa75 --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-shovel/src/rabbitmq_shovel.app.src @@ -0,0 +1,13 @@ +{application, rabbitmq_shovel, + [{description, "Data Shovel for RabbitMQ"}, + {vsn, "%%VSN%%"}, + {modules, []}, + {registered, []}, + {env, [{defaults, [{prefetch_count, 1000}, + {ack_mode, on_confirm}, + {publish_fields, []}, + {publish_properties, []}, + {reconnect_delay, 5}] + }]}, + {mod, {rabbit_shovel, []}}, + {applications, [kernel, stdlib, rabbit, amqp_client]}]}. diff --git a/rabbitmq-server/plugins-src/rabbitmq-shovel/test/src/rabbit_shovel_test.erl b/rabbitmq-server/plugins-src/rabbitmq-shovel/test/src/rabbit_shovel_test.erl new file mode 100644 index 0000000..6619112 --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-shovel/test/src/rabbit_shovel_test.erl @@ -0,0 +1,250 @@ +%% The contents of this file are subject to the Mozilla Public License +%% Version 1.1 (the "License"); you may not use this file except in +%% compliance with the License. You may obtain a copy of the License +%% at http://www.mozilla.org/MPL/ +%% +%% Software distributed under the License is distributed on an "AS IS" +%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See +%% the License for the specific language governing rights and +%% limitations under the License. +%% +%% The Original Code is RabbitMQ. +%% +%% The Initial Developer of the Original Code is GoPivotal, Inc. +%% Copyright (c) 2007-2015 Pivotal Software, Inc. All rights reserved. +%% + +-module(rabbit_shovel_test). +-export([test/0]). +-include_lib("amqp_client/include/amqp_client.hrl"). +-include_lib("eunit/include/eunit.hrl"). + +-define(EXCHANGE, <<"test_exchange">>). +-define(TO_SHOVEL, <<"to_the_shovel">>). +-define(FROM_SHOVEL, <<"from_the_shovel">>). +-define(UNSHOVELLED, <<"unshovelled">>). +-define(SHOVELLED, <<"shovelled">>). +-define(TIMEOUT, 1000). + +main_test() -> + %% it may already be running. Stop if possible + application:stop(rabbitmq_shovel), + + %% shovel can be started with zero shovels configured + ok = application:start(rabbitmq_shovel), + ok = application:stop(rabbitmq_shovel), + + %% various ways of breaking the config + require_list_of_shovel_configurations = + test_broken_shovel_configs(invalid_config), + + require_list_of_shovel_configurations = + test_broken_shovel_configs([{test_shovel, invalid_shovel_config}]), + + Config = [{sources, [{broker, "amqp://"}]}, + {destinations, [{broker, "amqp://"}]}, + {queue, <<"">>}], + + {duplicate_shovel_definition, test_shovel} = + test_broken_shovel_configs( + [{test_shovel, Config}, {test_shovel, Config}]), + + {invalid_parameters, [{invalid, invalid, invalid}]} = + test_broken_shovel_config([{invalid, invalid, invalid} | Config]), + + {duplicate_parameters, [queue]} = + test_broken_shovel_config([{queue, <<"">>} | Config]), + + {missing_parameters, Missing} = + test_broken_shovel_config([]), + [destinations, queue, sources] = lists:sort(Missing), + + {unrecognised_parameters, [invalid]} = + test_broken_shovel_config([{invalid, invalid} | Config]), + + {require_list, invalid} = + test_broken_shovel_sources(invalid), + + {missing_endpoint_parameter, broker_or_brokers} = + test_broken_shovel_sources([]), + + {expected_list, brokers, invalid} = + test_broken_shovel_sources([{brokers, invalid}]), + + {expected_string_uri, 42} = + test_broken_shovel_sources([{brokers, [42]}]), + + {{unexpected_uri_scheme, "invalid"}, "invalid://"} = + test_broken_shovel_sources([{broker, "invalid://"}]), + + {{unable_to_parse_uri, no_scheme}, "invalid"} = + test_broken_shovel_sources([{broker, "invalid"}]), + + {expected_list,declarations, invalid} = + test_broken_shovel_sources([{broker, "amqp://"}, + {declarations, invalid}]), + {unknown_method_name, 42} = + test_broken_shovel_sources([{broker, "amqp://"}, + {declarations, [42]}]), + + {expected_method_field_list, 'queue.declare', 42} = + test_broken_shovel_sources([{broker, "amqp://"}, + {declarations, [{'queue.declare', 42}]}]), + + {unknown_fields, 'queue.declare', [invalid]} = + test_broken_shovel_sources( + [{broker, "amqp://"}, + {declarations, [{'queue.declare', [invalid]}]}]), + + {{invalid_amqp_params_parameter, heartbeat, "text", + [{"heartbeat", "text"}], {not_an_integer, "text"}}, _} = + test_broken_shovel_sources( + [{broker, "amqp://localhost/?heartbeat=text"}]), + + {{invalid_amqp_params_parameter, username, "text", + [{"username", "text"}], + {parameter_unconfigurable_in_query, username, "text"}}, _} = + test_broken_shovel_sources([{broker, "amqp://?username=text"}]), + + {invalid_parameter_value, prefetch_count, + {require_non_negative_integer, invalid}} = + test_broken_shovel_config([{prefetch_count, invalid} | Config]), + + {invalid_parameter_value, ack_mode, + {ack_mode_value_requires_one_of, + {no_ack, on_publish, on_confirm}, invalid}} = + test_broken_shovel_config([{ack_mode, invalid} | Config]), + + {invalid_parameter_value, queue, + {require_binary, invalid}} = + test_broken_shovel_config([{sources, [{broker, "amqp://"}]}, + {destinations, [{broker, "amqp://"}]}, + {queue, invalid}]), + + {invalid_parameter_value, publish_properties, + {require_list, invalid}} = + test_broken_shovel_config([{publish_properties, invalid} | Config]), + + {invalid_parameter_value, publish_properties, + {unexpected_fields, [invalid], _}} = + test_broken_shovel_config([{publish_properties, [invalid]} | Config]), + + {{invalid_ssl_parameter, fail_if_no_peer_cert, "42", _, + {require_boolean, '42'}}, _} = + test_broken_shovel_sources([{broker, "amqps://username:password@host:5673/vhost?cacertfile=/path/to/cacert.pem&certfile=/path/to/certfile.pem&keyfile=/path/to/keyfile.pem&verify=verify_peer&fail_if_no_peer_cert=42"}]), + + %% a working config + application:set_env( + rabbitmq_shovel, + shovels, + [{test_shovel, + [{sources, + [{broker, "amqp:///%2f?heartbeat=5"}, + {declarations, + [{'queue.declare', [exclusive, auto_delete]}, + {'exchange.declare', [{exchange, ?EXCHANGE}, auto_delete]}, + {'queue.bind', [{queue, <<>>}, {exchange, ?EXCHANGE}, + {routing_key, ?TO_SHOVEL}]} + ]}]}, + {destinations, + [{broker, "amqp:///%2f"}]}, + {queue, <<>>}, + {ack_mode, on_confirm}, + {publish_fields, [{exchange, ?EXCHANGE}, {routing_key, ?FROM_SHOVEL}]}, + {publish_properties, [{delivery_mode, 2}, + {cluster_id, <<"my-cluster">>}, + {content_type, ?SHOVELLED}]}, + {add_forward_headers, true} + ]}], + infinity), + + ok = application:start(rabbitmq_shovel), + + await_running_shovel(test_shovel), + + {ok, Conn} = amqp_connection:start(#amqp_params_network{}), + {ok, Chan} = amqp_connection:open_channel(Conn), + + #'queue.declare_ok'{ queue = Q } = + amqp_channel:call(Chan, #'queue.declare' { exclusive = true }), + #'queue.bind_ok'{} = + amqp_channel:call(Chan, #'queue.bind' { queue = Q, exchange = ?EXCHANGE, + routing_key = ?FROM_SHOVEL }), + #'queue.bind_ok'{} = + amqp_channel:call(Chan, #'queue.bind' { queue = Q, exchange = ?EXCHANGE, + routing_key = ?TO_SHOVEL }), + + #'basic.consume_ok'{ consumer_tag = CTag } = + amqp_channel:subscribe(Chan, + #'basic.consume' { queue = Q, exclusive = true }, + self()), + receive + #'basic.consume_ok'{ consumer_tag = CTag } -> ok + after ?TIMEOUT -> throw(timeout_waiting_for_consume_ok) + end, + + ok = amqp_channel:call(Chan, + #'basic.publish' { exchange = ?EXCHANGE, + routing_key = ?TO_SHOVEL }, + #amqp_msg { payload = <<42>>, + props = #'P_basic' { + delivery_mode = 2, + content_type = ?UNSHOVELLED } + }), + + receive + {#'basic.deliver' { consumer_tag = CTag, delivery_tag = AckTag, + routing_key = ?FROM_SHOVEL }, + #amqp_msg { payload = <<42>>, + props = #'P_basic' { delivery_mode = 2, + content_type = ?SHOVELLED, + headers = [{<<"x-shovelled">>, + _, _}]} + }} -> + ok = amqp_channel:call(Chan, #'basic.ack'{ delivery_tag = AckTag }) + after ?TIMEOUT -> throw(timeout_waiting_for_deliver1) + end, + + [{test_shovel, static, {running, _Info}, _Time}] = + rabbit_shovel_status:status(), + + receive + {#'basic.deliver' { consumer_tag = CTag, delivery_tag = AckTag1, + routing_key = ?TO_SHOVEL }, + #amqp_msg { payload = <<42>>, + props = #'P_basic' { delivery_mode = 2, + content_type = ?UNSHOVELLED } + }} -> + ok = amqp_channel:call(Chan, #'basic.ack'{ delivery_tag = AckTag1 }) + after ?TIMEOUT -> throw(timeout_waiting_for_deliver2) + end, + + amqp_channel:close(Chan), + amqp_connection:close(Conn), + + ok. + +test_broken_shovel_configs(Configs) -> + application:set_env(rabbitmq_shovel, shovels, Configs), + {error, {Error, _}} = application:start(rabbitmq_shovel), + Error. + +test_broken_shovel_config(Config) -> + {invalid_shovel_configuration, test_shovel, Error} = + test_broken_shovel_configs([{test_shovel, Config}]), + Error. + +test_broken_shovel_sources(Sources) -> + {invalid_parameter_value, sources, Error} = + test_broken_shovel_config([{sources, Sources}, + {destinations, [{broker, "amqp://"}]}, + {queue, <<"">>}]), + Error. + +await_running_shovel(Name) -> + case [Name || {Name, _, {running, _}, _} + <- rabbit_shovel_status:status()] of + [_] -> ok; + _ -> timer:sleep(100), + await_running_shovel(Name) + end. diff --git a/rabbitmq-server/plugins-src/rabbitmq-shovel/test/src/rabbit_shovel_test_all.erl b/rabbitmq-server/plugins-src/rabbitmq-shovel/test/src/rabbit_shovel_test_all.erl new file mode 100644 index 0000000..2269ea8 --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-shovel/test/src/rabbit_shovel_test_all.erl @@ -0,0 +1,33 @@ +%% The contents of this file are subject to the Mozilla Public License +%% Version 1.1 (the "License"); you may not use this file except in +%% compliance with the License. You may obtain a copy of the License at +%% http://www.mozilla.org/MPL/ +%% +%% Software distributed under the License is distributed on an "AS IS" +%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the +%% License for the specific language governing rights and limitations +%% under the License. +%% +%% The Original Code is RabbitMQ +%% +%% The Initial Developer of the Original Code is GoPivotal, Inc. +%% Copyright (c) 2010-2015 Pivotal Software, Inc. All rights reserved. +%% + +-module(rabbit_shovel_test_all). + +-export([all_tests/0]). + +all_tests() -> + ok = eunit:test(tests(rabbit_shovel_test, 60), [verbose]), + ok = eunit:test(tests(rabbit_shovel_test_dyn, 60), [verbose]). + +tests(Module, Timeout) -> + {foreach, fun() -> ok end, + [{timeout, Timeout, fun () -> Module:F() end} || F <- funs(Module, "_test")] ++ + [{timeout, Timeout, Fun} || Gen <- funs(Module, "_test_"), + Fun <- Module:Gen()]}. + +funs(Module, Suffix) -> + [F || {F, _Arity} <- proplists:get_value(exports, Module:module_info()), + string:right(atom_to_list(F), length(Suffix)) =:= Suffix]. diff --git a/rabbitmq-server/plugins-src/rabbitmq-shovel/test/src/rabbit_shovel_test_dyn.erl b/rabbitmq-server/plugins-src/rabbitmq-shovel/test/src/rabbit_shovel_test_dyn.erl new file mode 100644 index 0000000..b3c74d8 --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-shovel/test/src/rabbit_shovel_test_dyn.erl @@ -0,0 +1,293 @@ +%% The contents of this file are subject to the Mozilla Public License +%% Version 1.1 (the "License"); you may not use this file except in +%% compliance with the License. You may obtain a copy of the License +%% at http://www.mozilla.org/MPL/ +%% +%% Software distributed under the License is distributed on an "AS IS" +%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See +%% the License for the specific language governing rights and +%% limitations under the License. +%% +%% The Original Code is RabbitMQ Federation. +%% +%% The Initial Developer of the Original Code is GoPivotal, Inc. +%% Copyright (c) 2007-2015 Pivotal Software, Inc. All rights reserved. +%% + +-module(rabbit_shovel_test_dyn). + +-include_lib("eunit/include/eunit.hrl"). +-include_lib("amqp_client/include/amqp_client.hrl"). + +-import(rabbit_misc, [pget/2]). + +simple_test() -> + with_ch( + fun (Ch) -> + set_param(<<"test">>, [{<<"src-queue">>, <<"src">>}, + {<<"dest-queue">>, <<"dest">>}]), + publish_expect(Ch, <<>>, <<"src">>, <<"dest">>, <<"hello">>) + end). + +set_properties_test() -> + with_ch( + fun (Ch) -> + Ps = [{<<"src-queue">>, <<"src">>}, + {<<"dest-queue">>, <<"dest">>}, + {<<"publish-properties">>, [{<<"cluster_id">>, <<"x">>}]}], + set_param(<<"test">>, Ps), + #amqp_msg{props = #'P_basic'{cluster_id = Cluster}} = + publish_expect(Ch, <<>>, <<"src">>, <<"dest">>, <<"hi">>), + ?assertEqual(<<"x">>, Cluster) + end). + +exchange_test() -> + with_ch( + fun (Ch) -> + amqp_channel:call(Ch, #'queue.declare'{queue = <<"queue">>, + durable = true}), + amqp_channel:call( + Ch, #'queue.bind'{queue = <<"queue">>, + exchange = <<"amq.topic">>, + routing_key = <<"test-key">>}), + set_param(<<"test">>, [{<<"src-exchange">>, <<"amq.direct">>}, + {<<"src-exchange-key">>,<<"test-key">>}, + {<<"dest-exchange">>, <<"amq.topic">>}]), + publish_expect(Ch, <<"amq.direct">>, <<"test-key">>, + <<"queue">>, <<"hello">>), + set_param(<<"test">>, [{<<"src-exchange">>, <<"amq.direct">>}, + {<<"src-exchange-key">>, <<"test-key">>}, + {<<"dest-exchange">>, <<"amq.topic">>}, + {<<"dest-exchange-key">>,<<"new-key">>}]), + publish(Ch, <<"amq.direct">>, <<"test-key">>, <<"hello">>), + expect_empty(Ch, <<"queue">>), + amqp_channel:call( + Ch, #'queue.bind'{queue = <<"queue">>, + exchange = <<"amq.topic">>, + routing_key = <<"new-key">>}), + publish_expect(Ch, <<"amq.direct">>, <<"test-key">>, + <<"queue">>, <<"hello">>) + end). + +restart_test() -> + with_ch( + fun (Ch) -> + set_param(<<"test">>, [{<<"src-queue">>, <<"src">>}, + {<<"dest-queue">>, <<"dest">>}]), + %% The catch is because connections link to the shovel, + %% so one connection will die, kill the shovel, kill + %% the other connection, then we can't close it + [catch amqp_connection:close(C) || C <- rabbit_direct:list()], + publish_expect(Ch, <<>>, <<"src">>, <<"dest">>, <<"hello">>) + end). + +change_definition_test() -> + with_ch( + fun (Ch) -> + set_param(<<"test">>, [{<<"src-queue">>, <<"src">>}, + {<<"dest-queue">>, <<"dest">>}]), + publish_expect(Ch, <<>>, <<"src">>, <<"dest">>, <<"hello">>), + set_param(<<"test">>, [{<<"src-queue">>, <<"src">>}, + {<<"dest-queue">>, <<"dest2">>}]), + publish_expect(Ch, <<>>, <<"src">>, <<"dest2">>, <<"hello">>), + expect_empty(Ch, <<"dest">>), + clear_param(<<"test">>), + publish_expect(Ch, <<>>, <<"src">>, <<"src">>, <<"hello">>), + expect_empty(Ch, <<"dest">>), + expect_empty(Ch, <<"dest2">>) + end). + +autodelete_test_() -> + [autodelete_case({<<"on-confirm">>, <<"queue-length">>, 0, 100}), + autodelete_case({<<"on-confirm">>, 50, 50, 50}), + autodelete_case({<<"on-publish">>, <<"queue-length">>, 0, 100}), + autodelete_case({<<"on-publish">>, 50, 50, 50}), + %% no-ack is not compatible with explicit count + autodelete_case({<<"no-ack">>, <<"queue-length">>, 0, 100})]. + +autodelete_case(Args) -> + fun () -> with_ch(autodelete_do(Args)) end. + +autodelete_do({AckMode, After, ExpSrc, ExpDest}) -> + fun (Ch) -> + amqp_channel:call(Ch, #'confirm.select'{}), + amqp_channel:call(Ch, #'queue.declare'{queue = <<"src">>}), + publish_count(Ch, <<>>, <<"src">>, <<"hello">>, 100), + amqp_channel:wait_for_confirms(Ch), + set_param_nowait(<<"test">>, [{<<"src-queue">>, <<"src">>}, + {<<"dest-queue">>, <<"dest">>}, + {<<"ack-mode">>, AckMode}, + {<<"delete-after">>, After}]), + await_autodelete(<<"test">>), + expect_count(Ch, <<"src">>, <<"hello">>, ExpSrc), + expect_count(Ch, <<"dest">>, <<"hello">>, ExpDest) + end. + +validation_test() -> + URIs = [{<<"src-uri">>, <<"amqp://">>}, + {<<"dest-uri">>, <<"amqp://">>}], + + %% Need valid src and dest URIs + invalid_param([]), + invalid_param([{<<"src-queue">>, <<"test">>}, + {<<"src-uri">>, <<"derp">>}, + {<<"dest-uri">>, <<"amqp://">>}]), + invalid_param([{<<"src-queue">>, <<"test">>}, + {<<"src-uri">>, [<<"derp">>]}, + {<<"dest-uri">>, <<"amqp://">>}]), + invalid_param([{<<"src-queue">>, <<"test">>}, + {<<"dest-uri">>, <<"amqp://">>}]), + + %% Also need src exchange or queue + invalid_param(URIs), + valid_param([{<<"src-exchange">>, <<"test">>} | URIs]), + QURIs = [{<<"src-queue">>, <<"test">>} | URIs], + valid_param(QURIs), + + %% But not both + invalid_param([{<<"src-exchange">>, <<"test">>} | QURIs]), + + %% Check these are of right type + invalid_param([{<<"prefetch-count">>, <<"three">>} | QURIs]), + invalid_param([{<<"reconnect-delay">>, <<"three">>} | QURIs]), + invalid_param([{<<"ack-mode">>, <<"whenever">>} | QURIs]), + invalid_param([{<<"delete-after">>, <<"whenever">>} | QURIs]), + + %% Check properties have to look property-ish + invalid_param([{<<"publish-properties">>, [{<<"nonexistent">>, <<>>}]}]), + invalid_param([{<<"publish-properties">>, [{<<"cluster_id">>, 2}]}]), + invalid_param([{<<"publish-properties">>, <<"something">>}]), + + %% Can't use explicit message count and no-ack together + invalid_param([{<<"delete-after">>, 1}, + {<<"ack-mode">>, <<"no-ack">>} | QURIs]), + ok. + +security_validation_test() -> + [begin + rabbit_vhost:add(U), + rabbit_auth_backend_internal:add_user(U, <<>>), + rabbit_auth_backend_internal:set_permissions( + U, U, <<".*">>, <<".*">>, <<".*">>) + end || U <- [<<"a">>, <<"b">>]], + + Qs = [{<<"src-queue">>, <<"test">>}, + {<<"dest-queue">>, <<"test2">>}], + + A = lookup_user(<<"a">>), + valid_param([{<<"src-uri">>, <<"amqp:///a">>}, + {<<"dest-uri">>, <<"amqp:///a">>} | Qs], A), + invalid_param([{<<"src-uri">>, <<"amqp:///a">>}, + {<<"dest-uri">>, <<"amqp:///b">>} | Qs], A), + invalid_param([{<<"src-uri">>, <<"amqp:///b">>}, + {<<"dest-uri">>, <<"amqp:///a">>} | Qs], A), + [begin + rabbit_vhost:delete(U), + rabbit_auth_backend_internal:delete_user(U) + end || U <- [<<"a">>, <<"b">>]], + ok. + +%%---------------------------------------------------------------------------- + +with_ch(Fun) -> + {ok, Conn} = amqp_connection:start(#amqp_params_network{}), + {ok, Ch} = amqp_connection:open_channel(Conn), + Fun(Ch), + amqp_connection:close(Conn), + cleanup(), + ok. + +publish(Ch, X, Key, Payload) when is_binary(Payload) -> + publish(Ch, X, Key, #amqp_msg{payload = Payload}); + +publish(Ch, X, Key, Msg = #amqp_msg{}) -> + amqp_channel:cast(Ch, #'basic.publish'{exchange = X, + routing_key = Key}, Msg). + +publish_expect(Ch, X, Key, Q, Payload) -> + publish(Ch, X, Key, Payload), + expect(Ch, Q, Payload). + +expect(Ch, Q, Payload) -> + amqp_channel:subscribe(Ch, #'basic.consume'{queue = Q, + no_ack = true}, self()), + receive + #'basic.consume_ok'{consumer_tag = CTag} -> ok + end, + Msg = receive + {#'basic.deliver'{}, #amqp_msg{payload = Payload} = M} -> + M + after 1000 -> + exit({not_received, Payload}) + end, + amqp_channel:call(Ch, #'basic.cancel'{consumer_tag = CTag}), + Msg. + +expect_empty(Ch, Q) -> + ?assertMatch(#'basic.get_empty'{}, + amqp_channel:call(Ch, #'basic.get'{ queue = Q })). + +publish_count(Ch, X, Key, M, Count) -> + [publish(Ch, X, Key, M) || _ <- lists:seq(1, Count)]. + +expect_count(Ch, Q, M, Count) -> + [expect(Ch, Q, M) || _ <- lists:seq(1, Count)], + expect_empty(Ch, Q). + +set_param(Name, Value) -> + set_param_nowait(Name, Value), + await_shovel(Name). + +set_param_nowait(Name, Value) -> + ok = rabbit_runtime_parameters:set( + <<"/">>, <<"shovel">>, Name, [{<<"src-uri">>, <<"amqp://">>}, + {<<"dest-uri">>, [<<"amqp://">>]} | + Value], none). + +invalid_param(Value, User) -> + {error_string, _} = rabbit_runtime_parameters:set( + <<"/">>, <<"shovel">>, <<"invalid">>, Value, User). + +valid_param(Value, User) -> + ok = rabbit_runtime_parameters:set( + <<"/">>, <<"shovel">>, <<"a">>, Value, User), + ok = rabbit_runtime_parameters:clear(<<"/">>, <<"shovel">>, <<"a">>). + +invalid_param(Value) -> invalid_param(Value, none). +valid_param(Value) -> valid_param(Value, none). + +lookup_user(Name) -> + {ok, User} = rabbit_access_control:check_user_login(Name, []), + User. + +clear_param(Name) -> + rabbit_runtime_parameters:clear(<<"/">>, <<"shovel">>, Name). + +cleanup() -> + [rabbit_runtime_parameters:clear(pget(vhost, P), + pget(component, P), + pget(name, P)) || + P <- rabbit_runtime_parameters:list()], + [rabbit_amqqueue:delete(Q, false, false) || Q <- rabbit_amqqueue:list()]. + +await_shovel(Name) -> + await(fun () -> lists:member(Name, shovels_from_status()) end). + +await_autodelete(Name) -> + await(fun () -> not lists:member(Name, shovels_from_parameters()) end), + await(fun () -> not lists:member(Name, shovels_from_status()) end). + +await(Pred) -> + case Pred() of + true -> ok; + false -> timer:sleep(100), + await(Pred) + end. + +shovels_from_status() -> + S = rabbit_shovel_status:status(), + [N || {{<<"/">>, N}, dynamic, {running, _}, _} <- S]. + +shovels_from_parameters() -> + L = rabbit_runtime_parameters:list(<<"/">>, <<"shovel">>), + [pget(name, Shovel) || Shovel <- L]. diff --git a/rabbitmq-server/plugins-src/rabbitmq-stomp/.srcdist_done b/rabbitmq-server/plugins-src/rabbitmq-stomp/.srcdist_done new file mode 100644 index 0000000..e69de29 diff --git a/rabbitmq-server/plugins-src/rabbitmq-stomp/.travis.yml b/rabbitmq-server/plugins-src/rabbitmq-stomp/.travis.yml new file mode 100644 index 0000000..467bda0 --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-stomp/.travis.yml @@ -0,0 +1,32 @@ +sudo: false +language: erlang +notifications: + email: + - alerts@rabbitmq.com +addons: + apt: + packages: + - xsltproc +otp_release: + - R16B03-1 + - "17.5" + - "18.0" +install: + - if [ ! -d "$HOME/rabbitmq-public-umbrella/.git" ]; then git clone https://github.com/rabbitmq/rabbitmq-public-umbrella.git $HOME/rabbitmq-public-umbrella; fi + - cd $HOME/rabbitmq-public-umbrella + - make co + - make up +before_script: + - IFS="/" read -a PARTS <<< "$TRAVIS_REPO_SLUG" + - export TEST_DIR=$HOME/rabbitmq-public-umbrella/${PARTS[1]} + - rm -rf ${TEST_DIR} + - cp -r ${TRAVIS_BUILD_DIR} ${TEST_DIR} + - cd ${TEST_DIR} +script: make test +before_cache: + - rm -rf ${TEST_DIR} + - cd $HOME +cache: + apt: true + directories: + - $HOME/rabbitmq-public-umbrella diff --git a/rabbitmq-server/plugins-src/rabbitmq-stomp/CONTRIBUTING.md b/rabbitmq-server/plugins-src/rabbitmq-stomp/CONTRIBUTING.md new file mode 100644 index 0000000..69a4b4a --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-stomp/CONTRIBUTING.md @@ -0,0 +1,51 @@ +## Overview + +RabbitMQ projects use pull requests to discuss, collaborate on and accept code contributions. +Pull requests is the primary place of discussing code changes. + +## How to Contribute + +The process is fairly standard: + + * Fork the repository or repositories you plan on contributing to + * Clone [RabbitMQ umbrella repository](https://github.com/rabbitmq/rabbitmq-public-umbrella) + * `cd umbrella`, `make co` + * Create a branch with a descriptive name in the relevant repositories + * Make your changes, run tests, commit with a [descriptive message](http://tbaggery.com/2008/04/19/a-note-about-git-commit-messages.html), push to your fork + * Submit pull requests with an explanation what has been changed and **why** + * Submit a filled out and signed [Contributor Agreement](https://github.com/rabbitmq/ca#how-to-submit) if needed (see below) + * Be patient. We will get to your pull request eventually + +If what you are going to work on is a substantial change, please first ask the core team +of their opinion on [RabbitMQ mailing list](https://groups.google.com/forum/#!forum/rabbitmq-users). + + +## (Brief) Code of Conduct + +In one line: don't be a dick. + +Be respectful to the maintainers and other contributors. Open source +contributors put long hours into developing projects and doing user +support. Those projects and user support are available for free. We +believe this deserves some respect. + +Be respectful to people of all races, genders, religious beliefs and +political views. Regardless of how brilliant a pull request is +technically, we will not tolerate disrespectful or aggressive +behaviour. + +Contributors who violate this straightforward Code of Conduct will see +their pull requests closed and locked. + + +## Contributor Agreement + +If you want to contribute a non-trivial change, please submit a signed copy of our +[Contributor Agreement](https://github.com/rabbitmq/ca#how-to-submit) around the time +you submit your pull request. This will make it much easier (in some cases, possible) +for the RabbitMQ team at Pivotal to merge your contribution. + + +## Where to Ask Questions + +If something isn't clear, feel free to ask on our [mailing list](https://groups.google.com/forum/#!forum/rabbitmq-users). diff --git a/rabbitmq-server/plugins-src/rabbitmq-stomp/Makefile b/rabbitmq-server/plugins-src/rabbitmq-stomp/Makefile new file mode 100644 index 0000000..482105a --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-stomp/Makefile @@ -0,0 +1 @@ +include ../umbrella.mk diff --git a/rabbitmq-server/plugins-src/rabbitmq-stomp/NOTES b/rabbitmq-server/plugins-src/rabbitmq-stomp/NOTES new file mode 100644 index 0000000..b4a9f02 --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-stomp/NOTES @@ -0,0 +1,71 @@ +Comments from Sean Treadway, 2 June 2008, on the rabbitmq-discuss list: + + - On naming, extensibility, and headers: + + "STOMP looked like it was MQ agnostic and extensible while keeping + the core headers well defined (ack=client, message_id, etc...), + but my application was not MQ agnostic. Plus I saw some of the + ActiveMQ headers weren't available or necessary in RabbitMQ. + + "Keeping the AMQP naming is the best way to piggy back on the AMQP + documentation. For those that need simple, transient queues, the + existing STOMP documentation would be sufficient." + + ... + + "I only have experience with RabbitMQ, so I'm fine with exposing + AMQP rather than try to come to some agreement over the extension + names of standard STOMP headers." + + - On queue deletion over STOMP: + + "Here, I would stick with the verbs defined in STOMP and extend the + verbs with headers. One possibility is to use UNSUBSCRIBE + messages to change the queue properties before sending the + 'basic.cancel' method. Another possibility is to change queue + properties on a SUBSCRIBE message. Neither seem nice to me. Third + option is to do nothing, and delete the queues outside of the + STOMP protocol" + +Comments from Darien Kindlund, 11 February 2009, on the rabbitmq-discuss list: + + - On testing of connection establishment: + + "[O]nce I switched each perl process over to re-using their + existing STOMP connection, things worked much, much better. As + such, I'm continuing development. In your unit testing, you may + want to include rapid connect/disconnect behavior or otherwise + explicitly warn developers to avoid this scenario." + +Comments from Novak Joe, 11 September 2008, on the rabbitmq-discuss list: + + - On broadcast send: + + "That said, I think it would also be useful to add to the STOMP + wiki page an additional note on broadcast SEND. In particular I + found that in order to send a message to a broadcast exchange it + needs look something like: + + --------------------------------- + SEND + destination:x.mytopic + exchange:amq.topic + + my message + \x00 + -------------------------------- + + "However my initial newb intuition was that it should look more like: + + --------------------------------- + SEND + destination: + exchange:amq.topic + routing_key:x.mytopic + + my message + \x00 + -------------------------------- + + "The ruby examples cleared this up but not before I experienced a + bit of confusion on the subject." diff --git a/rabbitmq-server/plugins-src/rabbitmq-stomp/README.md b/rabbitmq-server/plugins-src/rabbitmq-stomp/README.md new file mode 100644 index 0000000..782d563 --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-stomp/README.md @@ -0,0 +1,14 @@ +# RabbitMQ STOMP adapter + +The STOMP adapter is included in the RabbitMQ distribution. To enable +it, use rabbitmq-plugins: + + rabbitmq-plugins enable rabbitmq_stomp + +## Supported STOMP Versions + +1.0 through 1.2. + +## Documentation + +[RabbitMQ STOMP plugin documentation](http://www.rabbitmq.com/stomp.html). diff --git a/rabbitmq-server/plugins-src/rabbitmq-stomp/deps/pika/Makefile b/rabbitmq-server/plugins-src/rabbitmq-stomp/deps/pika/Makefile new file mode 100644 index 0000000..b082bb5 --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-stomp/deps/pika/Makefile @@ -0,0 +1,27 @@ +UPSTREAM_GIT=https://github.com/pika/pika.git +REVISION=0.9.14 + +LIB_DIR=pika +CHECKOUT_DIR=pika-git + +TARGETS=$(LIB_DIR) + +all: $(TARGETS) + +clean: + rm -rf $(LIB_DIR) + +distclean: clean + rm -rf $(CHECKOUT_DIR) + +$(LIB_DIR) : $(CHECKOUT_DIR) + rm -rf $@ + cp -R $< $@ + +$(CHECKOUT_DIR): + git clone $(UPSTREAM_GIT) $@ + (cd $@ && git checkout $(REVISION)) || rm -rf $@ + +echo-revision: + @echo $(REVISION) + diff --git a/rabbitmq-server/plugins-src/rabbitmq-stomp/deps/stomppy/Makefile b/rabbitmq-server/plugins-src/rabbitmq-stomp/deps/stomppy/Makefile new file mode 100644 index 0000000..40f5bd1 --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-stomp/deps/stomppy/Makefile @@ -0,0 +1,27 @@ +UPSTREAM_GIT=https://github.com/jasonrbriggs/stomp.py.git +REVISION=v4.0.16 + +LIB_DIR=stomppy +CHECKOUT_DIR=stomppy-git + +TARGETS=$(LIB_DIR) + +all: $(TARGETS) + +clean: + rm -rf $(LIB_DIR) + +distclean: clean + rm -rf $(CHECKOUT_DIR) + +$(LIB_DIR) : $(CHECKOUT_DIR) + rm -rf $@ + cp -R $< $@ + +$(CHECKOUT_DIR): + git clone $(UPSTREAM_GIT) $@ + (cd $@ && git checkout $(REVISION)) || rm -rf $@ + +echo-revision: + @echo $(REVISION) + diff --git a/rabbitmq-server/plugins-src/rabbitmq-stomp/examples/perl/rabbitmq_stomp_recv.pl b/rabbitmq-server/plugins-src/rabbitmq-stomp/examples/perl/rabbitmq_stomp_recv.pl new file mode 100755 index 0000000..7b8b9cc --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-stomp/examples/perl/rabbitmq_stomp_recv.pl @@ -0,0 +1,13 @@ +#!/usr/bin/perl -w +# subscribe to messages from the queue 'foo' +use Net::Stomp; +my $stomp = Net::Stomp->new({hostname=>'localhost', port=>'61613'}); +$stomp->connect({login=>'guest', passcode=>'guest'}); +$stomp->subscribe({'destination'=>'/queue/foo', 'ack'=>'client'}); +while (1) { + my $frame = $stomp->receive_frame; + print $frame->body . "\n"; + $stomp->ack({frame=>$frame}); + last if $frame->body eq 'QUIT'; +} +$stomp->disconnect; diff --git a/rabbitmq-server/plugins-src/rabbitmq-stomp/examples/perl/rabbitmq_stomp_rpc_client.pl b/rabbitmq-server/plugins-src/rabbitmq-stomp/examples/perl/rabbitmq_stomp_rpc_client.pl new file mode 100755 index 0000000..3a98e16 --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-stomp/examples/perl/rabbitmq_stomp_rpc_client.pl @@ -0,0 +1,15 @@ +#!/usr/bin/perl -w + +use Net::Stomp; +my $stomp = Net::Stomp->new({hostname=>'localhost', port=>'61613'}); +$stomp->connect({login=>'guest', passcode=>'guest'}); + +my $private_q_name = "/queue/c-" . time() . "-" . rand(); + +$stomp->subscribe({destination => $private_q_name}); +$stomp->send({destination => '/queue/rabbitmq_stomp_rpc_service', + 'reply-to' => $private_q_name, + body => "request from $private_q_name"}); +print "Reply: " . $stomp->receive_frame->body; + +$stomp->disconnect; diff --git a/rabbitmq-server/plugins-src/rabbitmq-stomp/examples/perl/rabbitmq_stomp_rpc_service.pl b/rabbitmq-server/plugins-src/rabbitmq-stomp/examples/perl/rabbitmq_stomp_rpc_service.pl new file mode 100755 index 0000000..31e79ae --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-stomp/examples/perl/rabbitmq_stomp_rpc_service.pl @@ -0,0 +1,21 @@ +#!/usr/bin/perl -w + +use Net::Stomp; + +my $stomp = Net::Stomp->new({hostname=>'localhost', port=>'61613'}); +$stomp->connect({login=>'guest', passcode=>'guest'}); + +$stomp->subscribe({'destination'=>'/queue/rabbitmq_stomp_rpc_service', 'ack'=>'client'}); +while (1) { + print "Waiting for request...\n"; + my $frame = $stomp->receive_frame; + print "Received message, reply_to = " . $frame->headers->{"reply-to"} . "\n"; + print $frame->body . "\n"; + + $stomp->send({destination => $frame->headers->{"reply-to"}, bytes_message => 1, + body => "Got body: " . $frame->body}); + $stomp->ack({frame=>$frame}); + last if $frame->body eq 'QUIT'; +} + +$stomp->disconnect; diff --git a/rabbitmq-server/plugins-src/rabbitmq-stomp/examples/perl/rabbitmq_stomp_send.pl b/rabbitmq-server/plugins-src/rabbitmq-stomp/examples/perl/rabbitmq_stomp_send.pl new file mode 100755 index 0000000..4d26b78 --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-stomp/examples/perl/rabbitmq_stomp_send.pl @@ -0,0 +1,9 @@ +#!/usr/bin/perl -w +# send a message to the queue 'foo' +use Net::Stomp; +my $stomp = Net::Stomp->new({hostname=>'localhost', port=>'61613'}); +$stomp->connect({login=>'guest', passcode=>'guest'}); +$stomp->send({destination=>'/exchange/amq.fanout', + bytes_message=>1, + body=>($ARGV[0] or "test\0message")}); +$stomp->disconnect; diff --git a/rabbitmq-server/plugins-src/rabbitmq-stomp/examples/perl/rabbitmq_stomp_send_many.pl b/rabbitmq-server/plugins-src/rabbitmq-stomp/examples/perl/rabbitmq_stomp_send_many.pl new file mode 100755 index 0000000..f6ff54e --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-stomp/examples/perl/rabbitmq_stomp_send_many.pl @@ -0,0 +1,11 @@ +#!/usr/bin/perl -w +# send a message to the queue 'foo' +use Net::Stomp; +my $stomp = Net::Stomp->new({hostname=>'localhost', port=>'61613'}); +$stomp->connect({login=>'guest', passcode=>'guest'}); +for (my $i = 0; $i < 10000; $i++) { + $stomp->send({destination=>'/queue/foo', + bytes_message=>1, + body=>($ARGV[0] or "message $i")}); +} +$stomp->disconnect; diff --git a/rabbitmq-server/plugins-src/rabbitmq-stomp/examples/perl/rabbitmq_stomp_slow_recv.pl b/rabbitmq-server/plugins-src/rabbitmq-stomp/examples/perl/rabbitmq_stomp_slow_recv.pl new file mode 100755 index 0000000..043568f --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-stomp/examples/perl/rabbitmq_stomp_slow_recv.pl @@ -0,0 +1,14 @@ +#!/usr/bin/perl -w +# subscribe to messages from the queue 'foo' +use Net::Stomp; +my $stomp = Net::Stomp->new({hostname=>'localhost', port=>'61613'}); +$stomp->connect({login=>'guest', passcode=>'guest', prefetch=>1}); +$stomp->subscribe({'destination'=>'/queue/foo', 'ack'=>'client'}); +while (1) { + my $frame = $stomp->receive_frame; + print $frame->body . "\n"; + sleep 1; + $stomp->ack({frame=>$frame}); + last if $frame->body eq 'QUIT'; +} +$stomp->disconnect; diff --git a/rabbitmq-server/plugins-src/rabbitmq-stomp/examples/ruby/cb-receiver.rb b/rabbitmq-server/plugins-src/rabbitmq-stomp/examples/ruby/cb-receiver.rb new file mode 100644 index 0000000..4e6e261 --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-stomp/examples/ruby/cb-receiver.rb @@ -0,0 +1,8 @@ +require 'rubygems' +require 'stomp' + +conn = Stomp::Connection.open('guest', 'guest', 'localhost') +conn.subscribe('/queue/carl') +while mesg = conn.receive + puts mesg.body +end diff --git a/rabbitmq-server/plugins-src/rabbitmq-stomp/examples/ruby/cb-sender.rb b/rabbitmq-server/plugins-src/rabbitmq-stomp/examples/ruby/cb-sender.rb new file mode 100644 index 0000000..3d75946 --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-stomp/examples/ruby/cb-sender.rb @@ -0,0 +1,6 @@ +require 'rubygems' +require 'stomp' + +client = Stomp::Client.new("guest", "guest", "localhost", 61613) +10000.times { |i| client.publish '/queue/carl', "Test Message number #{i}"} +client.publish '/queue/carl', "All Done!" diff --git a/rabbitmq-server/plugins-src/rabbitmq-stomp/examples/ruby/cb-slow-receiver.rb b/rabbitmq-server/plugins-src/rabbitmq-stomp/examples/ruby/cb-slow-receiver.rb new file mode 100644 index 0000000..d98e5f8 --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-stomp/examples/ruby/cb-slow-receiver.rb @@ -0,0 +1,13 @@ +require 'rubygems' +require 'stomp' + +# Note: requires support for connect_headers hash in the STOMP gem's connection.rb +conn = Stomp::Connection.open('guest', 'guest', 'localhost', 61613, false, 5, {:prefetch => 1}) +conn.subscribe('/queue/carl', {:ack => 'client'}) +while mesg = conn.receive + puts mesg.body + puts 'Sleeping...' + sleep 0.2 + puts 'Awake again. Acking.' + conn.ack mesg.headers['message-id'] +end diff --git a/rabbitmq-server/plugins-src/rabbitmq-stomp/examples/ruby/persistent-receiver.rb b/rabbitmq-server/plugins-src/rabbitmq-stomp/examples/ruby/persistent-receiver.rb new file mode 100644 index 0000000..5a83df6 --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-stomp/examples/ruby/persistent-receiver.rb @@ -0,0 +1,11 @@ +require 'rubygems' +require 'stomp' + +conn = Stomp::Connection.open('guest', 'guest', 'localhost') +conn.subscribe('/queue/durable', :'auto-delete' => false, :durable => true) + +puts "Waiting for messages..." + +while mesg = conn.receive + puts mesg.body +end diff --git a/rabbitmq-server/plugins-src/rabbitmq-stomp/examples/ruby/persistent-sender.rb b/rabbitmq-server/plugins-src/rabbitmq-stomp/examples/ruby/persistent-sender.rb new file mode 100644 index 0000000..1be32d6 --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-stomp/examples/ruby/persistent-sender.rb @@ -0,0 +1,13 @@ +require 'rubygems' +require 'stomp' + +# Use this case to test durable queues +# +# Start the sender - 11 messages will be sent to /queue/durable and the sender exits +# Stop the server - 11 messages will be written to disk +# Start the server +# Start the receiver - 11 messages should be received and the receiver - interrupt the receive loop + +client = Stomp::Client.new("guest", "guest", "localhost", 61613) +10.times { |i| client.publish '/queue/durable', "Test Message number #{i} sent at #{Time.now}", 'delivery-mode' => '2'} +client.publish '/queue/durable', "All Done!" diff --git a/rabbitmq-server/plugins-src/rabbitmq-stomp/examples/ruby/topic-broadcast-receiver.rb b/rabbitmq-server/plugins-src/rabbitmq-stomp/examples/ruby/topic-broadcast-receiver.rb new file mode 100644 index 0000000..b338e53 --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-stomp/examples/ruby/topic-broadcast-receiver.rb @@ -0,0 +1,11 @@ +require 'rubygems' +require 'stomp' + +topic = ARGV[0] || 'x' +puts "Binding to /topic/#{topic}" + +conn = Stomp::Connection.open('guest', 'guest', 'localhost') +conn.subscribe("/topic/#{topic}") +while mesg = conn.receive + puts mesg.body +end diff --git a/rabbitmq-server/plugins-src/rabbitmq-stomp/examples/ruby/topic-broadcast-with-unsubscribe.rb b/rabbitmq-server/plugins-src/rabbitmq-stomp/examples/ruby/topic-broadcast-with-unsubscribe.rb new file mode 100644 index 0000000..19f05ee --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-stomp/examples/ruby/topic-broadcast-with-unsubscribe.rb @@ -0,0 +1,13 @@ +require 'rubygems' +require 'stomp' # this is a gem + +conn = Stomp::Connection.open('guest', 'guest', 'localhost') +puts "Subscribing to /topic/x" +conn.subscribe('/topic/x') +puts 'Receiving...' +mesg = conn.receive +puts mesg.body +puts "Unsubscribing from /topic/x" +conn.unsubscribe('/topic/x') +puts 'Sleeping 5 seconds...' +sleep 5 diff --git a/rabbitmq-server/plugins-src/rabbitmq-stomp/examples/ruby/topic-sender.rb b/rabbitmq-server/plugins-src/rabbitmq-stomp/examples/ruby/topic-sender.rb new file mode 100644 index 0000000..b0861f9 --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-stomp/examples/ruby/topic-sender.rb @@ -0,0 +1,7 @@ +require 'rubygems' +require 'stomp' + +client = Stomp::Client.new("guest", "guest", "localhost", 61613) +client.publish '/topic/x.y', 'first message' +client.publish '/topic/x.z', 'second message' +client.publish '/topic/x', 'third message' diff --git a/rabbitmq-server/plugins-src/rabbitmq-stomp/include/rabbit_stomp.hrl b/rabbitmq-server/plugins-src/rabbitmq-stomp/include/rabbit_stomp.hrl new file mode 100644 index 0000000..d1497f4 --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-stomp/include/rabbit_stomp.hrl @@ -0,0 +1,22 @@ +%% The contents of this file are subject to the Mozilla Public License +%% Version 1.1 (the "License"); you may not use this file except in +%% compliance with the License. You may obtain a copy of the License +%% at http://www.mozilla.org/MPL/ +%% +%% Software distributed under the License is distributed on an "AS IS" +%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See +%% the License for the specific language governing rights and +%% limitations under the License. +%% +%% The Original Code is RabbitMQ. +%% +%% The Initial Developer of the Original Code is GoPivotal, Inc. +%% Copyright (c) 2007-2015 Pivotal Software, Inc. All rights reserved. +%% + +-record(stomp_configuration, {default_login, + default_passcode, + implicit_connect, + ssl_cert_login}). + +-define(SUPPORTED_VERSIONS, ["1.0", "1.1", "1.2"]). diff --git a/rabbitmq-server/plugins-src/rabbitmq-stomp/include/rabbit_stomp_frame.hrl b/rabbitmq-server/plugins-src/rabbitmq-stomp/include/rabbit_stomp_frame.hrl new file mode 100644 index 0000000..77d5810 --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-stomp/include/rabbit_stomp_frame.hrl @@ -0,0 +1,17 @@ +%% The contents of this file are subject to the Mozilla Public License +%% Version 1.1 (the "License"); you may not use this file except in +%% compliance with the License. You may obtain a copy of the License +%% at http://www.mozilla.org/MPL/ +%% +%% Software distributed under the License is distributed on an "AS IS" +%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See +%% the License for the specific language governing rights and +%% limitations under the License. +%% +%% The Original Code is RabbitMQ. +%% +%% The Initial Developer of the Original Code is GoPivotal, Inc. +%% Copyright (c) 2007-2015 Pivotal Software, Inc. All rights reserved. +%% + +-record(stomp_frame, {command, headers, body_iolist}). diff --git a/rabbitmq-server/plugins-src/rabbitmq-stomp/include/rabbit_stomp_headers.hrl b/rabbitmq-server/plugins-src/rabbitmq-stomp/include/rabbit_stomp_headers.hrl new file mode 100644 index 0000000..3aff8b5 --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-stomp/include/rabbit_stomp_headers.hrl @@ -0,0 +1,69 @@ +%% The contents of this file are subject to the Mozilla Public License +%% Version 1.1 (the "License"); you may not use this file except in +%% compliance with the License. You may obtain a copy of the License +%% at http://www.mozilla.org/MPL/ +%% +%% Software distributed under the License is distributed on an "AS IS" +%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See +%% the License for the specific language governing rights and +%% limitations under the License. +%% +%% The Original Code is RabbitMQ. +%% +%% The Initial Developer of the Original Code is GoPivotal, Inc. +%% Copyright (c) 2011-2015 Pivotal Software, Inc. All rights reserved. +%% + +-define(HEADER_ACCEPT_VERSION, "accept-version"). +-define(HEADER_ACK, "ack"). +-define(HEADER_AMQP_MESSAGE_ID, "amqp-message-id"). +-define(HEADER_APP_ID, "app-id"). +-define(HEADER_CONTENT_ENCODING, "content-encoding"). +-define(HEADER_CONTENT_LENGTH, "content-length"). +-define(HEADER_CONTENT_TYPE, "content-type"). +-define(HEADER_CORRELATION_ID, "correlation-id"). +-define(HEADER_DESTINATION, "destination"). +-define(HEADER_EXPIRATION, "expiration"). +-define(HEADER_HEART_BEAT, "heart-beat"). +-define(HEADER_HOST, "host"). +-define(HEADER_ID, "id"). +-define(HEADER_LOGIN, "login"). +-define(HEADER_MESSAGE_ID, "message-id"). +-define(HEADER_PASSCODE, "passcode"). +-define(HEADER_PERSISTENT, "persistent"). +-define(HEADER_PREFETCH_COUNT, "prefetch-count"). +-define(HEADER_PRIORITY, "priority"). +-define(HEADER_RECEIPT, "receipt"). +-define(HEADER_REDELIVERED, "redelivered"). +-define(HEADER_REPLY_TO, "reply-to"). +-define(HEADER_SERVER, "server"). +-define(HEADER_SESSION, "session"). +-define(HEADER_SUBSCRIPTION, "subscription"). +-define(HEADER_TIMESTAMP, "timestamp"). +-define(HEADER_TRANSACTION, "transaction"). +-define(HEADER_TYPE, "type"). +-define(HEADER_USER_ID, "user-id"). +-define(HEADER_VERSION, "version"). +-define(HEADER_X_DEAD_LETTER_EXCHANGE, "x-dead-letter-exchange"). +-define(HEADER_X_DEAD_LETTER_ROUTING_KEY, "x-dead-letter-routing-key"). +-define(HEADER_X_EXPIRES, "x-expires"). +-define(HEADER_X_MAX_LENGTH, "x-max-length"). +-define(HEADER_X_MAX_LENGTH_BYTES, "x-max-length-bytes"). +-define(HEADER_X_MAX_PRIORITY, "x-max-priority"). +-define(HEADER_X_MESSAGE_TTL, "x-message-ttl"). + +-define(MESSAGE_ID_SEPARATOR, "@@"). + +-define(HEADERS_NOT_ON_SEND, [?HEADER_MESSAGE_ID]). + +-define(TEMP_QUEUE_ID_PREFIX, "/temp-queue/"). + +-define(HEADER_ARGUMENTS, [ + ?HEADER_X_DEAD_LETTER_EXCHANGE, + ?HEADER_X_DEAD_LETTER_ROUTING_KEY, + ?HEADER_X_EXPIRES, + ?HEADER_X_MAX_LENGTH, + ?HEADER_X_MAX_LENGTH_BYTES, + ?HEADER_X_MAX_PRIORITY, + ?HEADER_X_MESSAGE_TTL + ]). diff --git a/rabbitmq-server/plugins-src/rabbitmq-stomp/package.mk b/rabbitmq-server/plugins-src/rabbitmq-stomp/package.mk new file mode 100644 index 0000000..daacc68 --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-stomp/package.mk @@ -0,0 +1,26 @@ +RELEASABLE:=true +DEPS:=rabbitmq-server rabbitmq-erlang-client rabbitmq-test +STANDALONE_TEST_COMMANDS:=eunit:test([rabbit_stomp_test_util,rabbit_stomp_test_frame],[verbose]) +WITH_BROKER_TEST_SCRIPTS:=$(PACKAGE_DIR)/test/src/test.py $(PACKAGE_DIR)/test/src/test_connect_options.py $(PACKAGE_DIR)/test/src/test_ssl.py +WITH_BROKER_TEST_COMMANDS:=rabbit_stomp_test:all_tests() rabbit_stomp_amqqueue_test:all_tests() +WITH_BROKER_TEST_CONFIG:=$(PACKAGE_DIR)/test/ebin/test + +define package_rules + +$(PACKAGE_DIR)+pre-test:: + rm -rf $(PACKAGE_DIR)/test/certs + mkdir $(PACKAGE_DIR)/test/certs + mkdir -p $(PACKAGE_DIR)/test/ebin + sed -e "s|%%CERTS_DIR%%|$(abspath $(PACKAGE_DIR))/test/certs|g" < $(PACKAGE_DIR)/test/src/test.config > $(PACKAGE_DIR)/test/ebin/test.config + $(MAKE) -C $(PACKAGE_DIR)/../rabbitmq-test/certs all PASSWORD=test DIR=$(abspath $(PACKAGE_DIR))/test/certs + $(MAKE) -C $(PACKAGE_DIR)/deps/stomppy + $(MAKE) -C $(PACKAGE_DIR)/deps/pika + +$(PACKAGE_DIR)+clean:: + rm -rf $(PACKAGE_DIR)/test/certs + +$(PACKAGE_DIR)+clean-with-deps:: + $(MAKE) -C $(PACKAGE_DIR)/deps/stomppy distclean + $(MAKE) -C $(PACKAGE_DIR)/deps/pika distclean + +endef diff --git a/rabbitmq-server/plugins-src/rabbitmq-stomp/src/rabbit_stomp.erl b/rabbitmq-server/plugins-src/rabbitmq-stomp/src/rabbit_stomp.erl new file mode 100644 index 0000000..bd867aa --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-stomp/src/rabbit_stomp.erl @@ -0,0 +1,87 @@ +%% The contents of this file are subject to the Mozilla Public License +%% Version 1.1 (the "License"); you may not use this file except in +%% compliance with the License. You may obtain a copy of the License +%% at http://www.mozilla.org/MPL/ +%% +%% Software distributed under the License is distributed on an "AS IS" +%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See +%% the License for the specific language governing rights and +%% limitations under the License. +%% +%% The Original Code is RabbitMQ. +%% +%% The Initial Developer of the Original Code is GoPivotal, Inc. +%% Copyright (c) 2007-2015 Pivotal Software, Inc. All rights reserved. +%% + +-module(rabbit_stomp). + +-include("rabbit_stomp.hrl"). + +-behaviour(application). +-export([start/2, stop/1]). + +-define(DEFAULT_CONFIGURATION, + #stomp_configuration{ + default_login = undefined, + default_passcode = undefined, + implicit_connect = false, + ssl_cert_login = false}). + +start(normal, []) -> + Config = parse_configuration(), + Listeners = parse_listener_configuration(), + rabbit_stomp_sup:start_link(Listeners, Config). + +stop(_State) -> + ok. + +parse_listener_configuration() -> + {ok, Listeners} = application:get_env(tcp_listeners), + {ok, SslListeners} = application:get_env(ssl_listeners), + {Listeners, SslListeners}. + +parse_configuration() -> + {ok, UserConfig} = application:get_env(default_user), + Conf0 = parse_default_user(UserConfig, ?DEFAULT_CONFIGURATION), + {ok, SSLLogin} = application:get_env(ssl_cert_login), + {ok, ImplicitConnect} = application:get_env(implicit_connect), + Conf = Conf0#stomp_configuration{ssl_cert_login = SSLLogin, + implicit_connect = ImplicitConnect}, + report_configuration(Conf), + Conf. + +parse_default_user([], Configuration) -> + Configuration; +parse_default_user([{login, Login} | Rest], Configuration) -> + parse_default_user(Rest, Configuration#stomp_configuration{ + default_login = Login}); +parse_default_user([{passcode, Passcode} | Rest], Configuration) -> + parse_default_user(Rest, Configuration#stomp_configuration{ + default_passcode = Passcode}); +parse_default_user([Unknown | Rest], Configuration) -> + rabbit_log:warning("rabbit_stomp: ignoring invalid default_user " + "configuration option: ~p~n", [Unknown]), + parse_default_user(Rest, Configuration). + +report_configuration(#stomp_configuration{ + default_login = Login, + implicit_connect = ImplicitConnect, + ssl_cert_login = SSLCertLogin}) -> + case Login of + undefined -> ok; + _ -> rabbit_log:info("rabbit_stomp: default user '~s' " + "enabled~n", [Login]) + end, + + case ImplicitConnect of + true -> rabbit_log:info("rabbit_stomp: implicit connect enabled~n"); + false -> ok + end, + + case SSLCertLogin of + true -> rabbit_log:info("rabbit_stomp: ssl_cert_login enabled~n"); + false -> ok + end, + + ok. diff --git a/rabbitmq-server/plugins-src/rabbitmq-stomp/src/rabbit_stomp_client_sup.erl b/rabbitmq-server/plugins-src/rabbitmq-stomp/src/rabbit_stomp_client_sup.erl new file mode 100644 index 0000000..4f293ed --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-stomp/src/rabbit_stomp_client_sup.erl @@ -0,0 +1,55 @@ +%% The contents of this file are subject to the Mozilla Public License +%% Version 1.1 (the "License"); you may not use this file except in +%% compliance with the License. You may obtain a copy of the License +%% at http://www.mozilla.org/MPL/ +%% +%% Software distributed under the License is distributed on an "AS IS" +%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See +%% the License for the specific language governing rights and +%% limitations under the License. +%% +%% The Original Code is RabbitMQ. +%% +%% The Initial Developer of the Original Code is GoPivotal, Inc. +%% Copyright (c) 2007-2015 Pivotal Software, Inc. All rights reserved. +%% + +-module(rabbit_stomp_client_sup). +-behaviour(supervisor2). + +-define(MAX_WAIT, 16#ffffffff). +-export([start_link/1, init/1]). + +start_link(Configuration) -> + {ok, SupPid} = supervisor2:start_link(?MODULE, []), + {ok, HelperPid} = + supervisor2:start_child(SupPid, + {rabbit_stomp_heartbeat_sup, + {rabbit_connection_helper_sup, start_link, []}, + intrinsic, infinity, supervisor, + [rabbit_connection_helper_sup]}), + %% The processor is intrinsic. When it exits, the supervisor goes too. + {ok, ProcessorPid} = + supervisor2:start_child(SupPid, + {rabbit_stomp_processor, + {rabbit_stomp_processor, start_link, + [Configuration]}, + intrinsic, ?MAX_WAIT, worker, + [rabbit_stomp_processor]}), + %% We want the reader to be transient since when it exits normally + %% the processor may have some work still to do (and the reader + %% tells the processor to exit). However, if the reader terminates + %% abnormally then we want to take everything down. + {ok, ReaderPid} = supervisor2:start_child( + SupPid, + {rabbit_stomp_reader, + {rabbit_stomp_reader, + start_link, [HelperPid, ProcessorPid, Configuration]}, + transient, ?MAX_WAIT, worker, + [rabbit_stomp_reader]}), + + {ok, SupPid, ReaderPid}. + +init([]) -> + {ok, {{one_for_all, 0, 1}, []}}. + diff --git a/rabbitmq-server/plugins-src/rabbitmq-stomp/src/rabbit_stomp_frame.erl b/rabbitmq-server/plugins-src/rabbitmq-stomp/src/rabbit_stomp_frame.erl new file mode 100644 index 0000000..e6cedcf --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-stomp/src/rabbit_stomp_frame.erl @@ -0,0 +1,266 @@ +%% The contents of this file are subject to the Mozilla Public License +%% Version 1.1 (the "License"); you may not use this file except in +%% compliance with the License. You may obtain a copy of the License +%% at http://www.mozilla.org/MPL/ +%% +%% Software distributed under the License is distributed on an "AS IS" +%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See +%% the License for the specific language governing rights and +%% limitations under the License. +%% +%% The Original Code is RabbitMQ. +%% +%% The Initial Developer of the Original Code is GoPivotal, Inc. +%% Copyright (c) 2007-2015 Pivotal Software, Inc. All rights reserved. +%% + +%% stomp_frame implements the STOMP framing protocol "version 1.0", as +%% per http://stomp.codehaus.org/Protocol + +-module(rabbit_stomp_frame). + +-include("rabbit_stomp_frame.hrl"). +-include("rabbit_stomp_headers.hrl"). + +-export([parse/2, initial_state/0]). +-export([header/2, header/3, + boolean_header/2, boolean_header/3, + integer_header/2, integer_header/3, + binary_header/2, binary_header/3]). +-export([serialize/1, serialize/2]). + +initial_state() -> none. + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%% STOMP 1.1 frames basic syntax +%% Rabbit modifications: +%% o CR LF is equivalent to LF in all element terminators (eol). +%% o Escape codes for header names and values include \r for CR +%% and CR is not allowed. +%% o Header names and values are not limited to UTF-8 strings. +%% o Header values may contain unescaped colons +%% +%% frame_seq ::= *(noise frame) +%% noise ::= *(NUL | eol) +%% eol ::= LF | CR LF +%% frame ::= cmd hdrs body NUL +%% body ::= *OCTET +%% cmd ::= 1*NOTEOL eol +%% hdrs ::= *hdr eol +%% hdr ::= hdrname COLON hdrvalue eol +%% hdrname ::= 1*esc_char +%% hdrvalue ::= *esc_char +%% esc_char ::= HDROCT | BACKSLASH ESCCODE +%% +%% Terms in CAPS all represent sets (alternatives) of single octets. +%% They are defined here using a small extension of BNF, minus (-): +%% +%% term1 - term2 denotes any of the possibilities in term1 +%% excluding those in term2. +%% In this grammar minus is only used for sets of single octets. +%% +%% OCTET ::= '00'x..'FF'x % any octet +%% NUL ::= '00'x % the zero octet +%% LF ::= '\n' % '0a'x newline or linefeed +%% CR ::= '\r' % '0d'x carriage return +%% NOTEOL ::= OCTET - (CR | LF) % any octet except CR or LF +%% BACKSLASH ::= '\\' % '5c'x +%% ESCCODE ::= 'c' | 'n' | 'r' | BACKSLASH +%% COLON ::= ':' +%% HDROCT ::= NOTEOL - (COLON | BACKSLASH) +%% % octets allowed in a header +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + +%% explicit frame characters +-define(NUL, 0). +-define(CR, $\r). +-define(LF, $\n). +-define(BSL, $\\). +-define(COLON, $:). + +%% header escape codes +-define(LF_ESC, $n). +-define(BSL_ESC, $\\). +-define(COLON_ESC, $c). +-define(CR_ESC, $r). + +%% parser state +-record(state, {acc, cmd, hdrs, hdrname}). + +parse(Content, {resume, Continuation}) -> Continuation(Content); +parse(Content, none ) -> parser(Content, noframe, #state{}). + +more(Continuation) -> {more, {resume, Continuation}}. + +%% Single-function parser: Term :: noframe | command | headers | hdrname | hdrvalue +%% general more and line-end detection +parser(<<>>, Term , State) -> more(fun(Rest) -> parser(Rest, Term, State) end); +parser(<>, Term , State) -> more(fun(Rest) -> parser(<>, Term, State) end); +parser(<>, Term , State) -> parser(<>, Term, State); +parser(<>, Term , _State) -> {error, {unexpected_chars(Term), [?CR, Ch]}}; +%% escape processing (only in hdrname and hdrvalue terms) +parser(<>, Term , State) -> more(fun(Rest) -> parser(<>, Term, State) end); +parser(<>, Term , State) + when Term == hdrname; + Term == hdrvalue -> unescape(Ch, fun(Ech) -> parser(Rest, Term, accum(Ech, State)) end); +%% inter-frame noise +parser(<>, noframe , State) -> parser(Rest, noframe, State); +parser(<>, noframe , State) -> parser(Rest, noframe, State); +%% detect transitions +parser( Rest, noframe , State) -> goto(noframe, command, Rest, State); +parser(<>, command , State) -> goto(command, headers, Rest, State); +parser(<>, headers , State) -> goto(headers, body, Rest, State); +parser( Rest, headers , State) -> goto(headers, hdrname, Rest, State); +parser(<>, hdrname , State) -> goto(hdrname, hdrvalue, Rest, State); +parser(<>, hdrname , State) -> goto(hdrname, headers, Rest, State); +parser(<>, hdrvalue, State) -> goto(hdrvalue, headers, Rest, State); +%% accumulate +parser(<>, Term , State) -> parser(Rest, Term, accum(Ch, State)). + +%% state transitions +goto(noframe, command, Rest, State ) -> parser(Rest, command, State#state{acc = []}); +goto(command, headers, Rest, State = #state{acc = Acc} ) -> parser(Rest, headers, State#state{cmd = lists:reverse(Acc), hdrs = []}); +goto(headers, body, Rest, #state{cmd = Cmd, hdrs = Hdrs}) -> parse_body(Rest, #stomp_frame{command = Cmd, headers = Hdrs}); +goto(headers, hdrname, Rest, State ) -> parser(Rest, hdrname, State#state{acc = []}); +goto(hdrname, hdrvalue, Rest, State = #state{acc = Acc} ) -> parser(Rest, hdrvalue, State#state{acc = [], hdrname = lists:reverse(Acc)}); +goto(hdrname, headers, _Rest, #state{acc = Acc} ) -> {error, {header_no_value, lists:reverse(Acc)}}; % badly formed header -- fatal error +goto(hdrvalue, headers, Rest, State = #state{acc = Acc, hdrs = Headers, hdrname = HdrName}) -> + parser(Rest, headers, State#state{hdrs = insert_header(Headers, HdrName, lists:reverse(Acc))}). + +%% error atom +unexpected_chars(noframe) -> unexpected_chars_between_frames; +unexpected_chars(command) -> unexpected_chars_in_command; +unexpected_chars(hdrname) -> unexpected_chars_in_header; +unexpected_chars(hdrvalue) -> unexpected_chars_in_header; +unexpected_chars(_Term) -> unexpected_chars. + +%% general accumulation +accum(Ch, State = #state{acc = Acc}) -> State#state{acc = [Ch | Acc]}. + +%% resolve escapes (with error processing) +unescape(?LF_ESC, Fun) -> Fun(?LF); +unescape(?BSL_ESC, Fun) -> Fun(?BSL); +unescape(?COLON_ESC, Fun) -> Fun(?COLON); +unescape(?CR_ESC, Fun) -> Fun(?CR); +unescape(Ch, _Fun) -> {error, {bad_escape, [?BSL, Ch]}}. + +%% insert header unless aleady seen +insert_header(Headers, Name, Value) -> + case lists:keymember(Name, 1, Headers) of + true -> Headers; % first header only + false -> [{Name, Value} | Headers] + end. + +parse_body(Content, Frame = #stomp_frame{command = Command}) -> + case Command of + "SEND" -> parse_body(Content, Frame, [], integer_header(Frame, ?HEADER_CONTENT_LENGTH, unknown)); + _ -> parse_body(Content, Frame, [], unknown) + end. + +parse_body(Content, Frame, Chunks, unknown) -> + parse_body2(Content, Frame, Chunks, case firstnull(Content) of + -1 -> {more, unknown}; + Pos -> {done, Pos} + end); +parse_body(Content, Frame, Chunks, Remaining) -> + Size = byte_size(Content), + parse_body2(Content, Frame, Chunks, case Remaining >= Size of + true -> {more, Remaining - Size}; + false -> {done, Remaining} + end). + +parse_body2(Content, Frame, Chunks, {more, Left}) -> + Chunks1 = finalize_chunk(Content, Chunks), + more(fun(Rest) -> parse_body(Rest, Frame, Chunks1, Left) end); +parse_body2(Content, Frame, Chunks, {done, Pos}) -> + <> = Content, + Body = lists:reverse(finalize_chunk(Chunk, Chunks)), + {ok, Frame#stomp_frame{body_iolist = Body}, Rest}. + +finalize_chunk(<<>>, Chunks) -> Chunks; +finalize_chunk(Chunk, Chunks) -> [Chunk | Chunks]. + +default_value({ok, Value}, _DefaultValue) -> Value; +default_value(not_found, DefaultValue) -> DefaultValue. + +header(#stomp_frame{headers = Headers}, Key) -> + case lists:keysearch(Key, 1, Headers) of + {value, {_, Str}} -> {ok, Str}; + _ -> not_found + end. + +header(F, K, D) -> default_value(header(F, K), D). + +boolean_header(#stomp_frame{headers = Headers}, Key) -> + case lists:keysearch(Key, 1, Headers) of + {value, {_, "true"}} -> {ok, true}; + {value, {_, "false"}} -> {ok, false}; + %% some Python clients serialize True/False as "True"/"False" + {value, {_, "True"}} -> {ok, true}; + {value, {_, "False"}} -> {ok, false}; + _ -> not_found + end. + +boolean_header(F, K, D) -> default_value(boolean_header(F, K), D). + +internal_integer_header(Headers, Key) -> + case lists:keysearch(Key, 1, Headers) of + {value, {_, Str}} -> {ok, list_to_integer(string:strip(Str))}; + _ -> not_found + end. + +integer_header(#stomp_frame{headers = Headers}, Key) -> + internal_integer_header(Headers, Key). + +integer_header(F, K, D) -> default_value(integer_header(F, K), D). + +binary_header(F, K) -> + case header(F, K) of + {ok, Str} -> {ok, list_to_binary(Str)}; + not_found -> not_found + end. + +binary_header(F, K, D) -> default_value(binary_header(F, K), D). + +serialize(Frame) -> + serialize(Frame, true). + +%% second argument controls whether a trailing linefeed +%% character should be added, see rabbitmq/rabbitmq-stomp#39. +serialize(Frame, true) -> + serialize(Frame, false) ++ [?LF]; +serialize(#stomp_frame{command = Command, + headers = Headers, + body_iolist = BodyFragments}, false) -> + Len = iolist_size(BodyFragments), + [Command, ?LF, + lists:map(fun serialize_header/1, + lists:keydelete(?HEADER_CONTENT_LENGTH, 1, Headers)), + if + Len > 0 -> [?HEADER_CONTENT_LENGTH ++ ":", integer_to_list(Len), ?LF]; + true -> [] + end, + ?LF, BodyFragments, 0]. + +serialize_header({K, V}) when is_integer(V) -> hdr(escape(K), integer_to_list(V)); +serialize_header({K, V}) when is_boolean(V) -> hdr(escape(K), boolean_to_list(V)); +serialize_header({K, V}) when is_list(V) -> hdr(escape(K), escape(V)). + +boolean_to_list(true) -> "true"; +boolean_to_list(_) -> "false". + +hdr(K, V) -> [K, ?COLON, V, ?LF]. + +escape(Str) -> [escape1(Ch) || Ch <- Str]. + +escape1(?COLON) -> [?BSL, ?COLON_ESC]; +escape1(?BSL) -> [?BSL, ?BSL_ESC]; +escape1(?LF) -> [?BSL, ?LF_ESC]; +escape1(?CR) -> [?BSL, ?CR_ESC]; +escape1(Ch) -> Ch. + +firstnull(Content) -> firstnull(Content, 0). + +firstnull(<<>>, _N) -> -1; +firstnull(<<0, _Rest/binary>>, N) -> N; +firstnull(<<_Ch, Rest/binary>>, N) -> firstnull(Rest, N+1). diff --git a/rabbitmq-server/plugins-src/rabbitmq-stomp/src/rabbit_stomp_processor.erl b/rabbitmq-server/plugins-src/rabbitmq-stomp/src/rabbit_stomp_processor.erl new file mode 100644 index 0000000..80edb66 --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-stomp/src/rabbit_stomp_processor.erl @@ -0,0 +1,1098 @@ +%% The contents of this file are subject to the Mozilla Public License +%% Version 1.1 (the "License"); you may not use this file except in +%% compliance with the License. You may obtain a copy of the License +%% at http://www.mozilla.org/MPL/ +%% +%% Software distributed under the License is distributed on an "AS IS" +%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See +%% the License for the specific language governing rights and +%% limitations under the License. +%% +%% The Original Code is RabbitMQ. +%% +%% The Initial Developer of the Original Code is GoPivotal, Inc. +%% Copyright (c) 2007-2015 Pivotal Software, Inc. All rights reserved. +%% + +-module(rabbit_stomp_processor). +-behaviour(gen_server2). + +-export([start_link/1, init_arg/2, process_frame/2, flush_and_die/1]). +-export([init/1, handle_call/3, handle_cast/2, handle_info/2, + code_change/3, terminate/2]). + +-include_lib("amqp_client/include/amqp_client.hrl"). +-include_lib("amqp_client/include/rabbit_routing_prefixes.hrl"). +-include("rabbit_stomp_frame.hrl"). +-include("rabbit_stomp.hrl"). +-include("rabbit_stomp_headers.hrl"). + +-record(state, {session_id, channel, connection, subscriptions, + version, start_heartbeat_fun, pending_receipts, + config, route_state, reply_queues, frame_transformer, + adapter_info, send_fun, ssl_login_name, peer_addr, + %% see rabbitmq/rabbitmq-stomp#39 + trailing_lf}). + +-record(subscription, {dest_hdr, ack_mode, multi_ack, description}). + +-define(FLUSH_TIMEOUT, 60000). + +%%---------------------------------------------------------------------------- +%% Public API +%%---------------------------------------------------------------------------- +start_link(Args) -> + gen_server2:start_link(?MODULE, Args, []). + +init_arg(ProcessorPid, InitArgs) -> + gen_server2:cast(ProcessorPid, {init, InitArgs}). + +process_frame(Pid, Frame = #stomp_frame{command = "SEND"}) -> + credit_flow:send(Pid), + gen_server2:cast(Pid, {"SEND", Frame, self()}); +process_frame(Pid, Frame = #stomp_frame{command = Command}) -> + gen_server2:cast(Pid, {Command, Frame, noflow}). + +flush_and_die(Pid) -> + gen_server2:cast(Pid, flush_and_die). + +%%---------------------------------------------------------------------------- +%% Basic gen_server2 callbacks +%%---------------------------------------------------------------------------- + +init(Configuration) -> + process_flag(trap_exit, true), + {ok, + #state { + session_id = none, + channel = none, + connection = none, + subscriptions = dict:new(), + version = none, + pending_receipts = undefined, + config = Configuration, + route_state = rabbit_routing_util:init_state(), + reply_queues = dict:new(), + frame_transformer = undefined, + trailing_lf = rabbit_misc:get_env(rabbitmq_stomp, trailing_lf, true)}, + hibernate, + {backoff, 1000, 1000, 10000} + }. + +terminate(_Reason, State) -> + close_connection(State). + +handle_cast({init, [SendFun, AdapterInfo, StartHeartbeatFun, SSLLoginName, + PeerAddr]}, + State) -> + {noreply, State #state { send_fun = SendFun, + adapter_info = AdapterInfo, + start_heartbeat_fun = StartHeartbeatFun, + ssl_login_name = SSLLoginName, + peer_addr = PeerAddr}}; + +handle_cast(flush_and_die, State) -> + {stop, normal, close_connection(State)}; + +handle_cast({"STOMP", Frame, noflow}, State) -> + process_connect(no_implicit, Frame, State); + +handle_cast({"CONNECT", Frame, noflow}, State) -> + process_connect(no_implicit, Frame, State); + +handle_cast(Request, State = #state{channel = none, + config = #stomp_configuration{ + implicit_connect = true}}) -> + {noreply, State1 = #state{channel = Ch}, _} = + process_connect(implicit, #stomp_frame{headers = []}, State), + case Ch of + none -> {stop, normal, State1}; + _ -> handle_cast(Request, State1) + end; + +handle_cast(_Request, State = #state{channel = none, + config = #stomp_configuration{ + implicit_connect = false}}) -> + {noreply, + send_error("Illegal command", + "You must log in using CONNECT first", + State), + hibernate}; + +handle_cast({Command, Frame, FlowPid}, + State = #state{frame_transformer = FT}) -> + case FlowPid of + noflow -> ok; + _ -> credit_flow:ack(FlowPid) + end, + Frame1 = FT(Frame), + process_request( + fun(StateN) -> + case validate_frame(Command, Frame1, StateN) of + R = {error, _, _, _} -> R; + _ -> handle_frame(Command, Frame1, StateN) + end + end, + fun(StateM) -> ensure_receipt(Frame1, StateM) end, + State); + +handle_cast(client_timeout, + State = #state{adapter_info = #amqp_adapter_info{name = S}}) -> + rabbit_log:warning("STOMP detected missed client heartbeat(s) " + "on connection ~s, closing it~n", [S]), + {stop, {shutdown, client_heartbeat_timeout}, close_connection(State)}. + +handle_info(#'basic.consume_ok'{}, State) -> + {noreply, State, hibernate}; +handle_info(#'basic.cancel_ok'{}, State) -> + {noreply, State, hibernate}; +handle_info(#'basic.ack'{delivery_tag = Tag, multiple = IsMulti}, State) -> + {noreply, flush_pending_receipts(Tag, IsMulti, State), hibernate}; +handle_info({Delivery = #'basic.deliver'{}, + #amqp_msg{props = Props, payload = Payload}, + DeliveryCtx}, State) -> + State1 = send_delivery(Delivery, Props, Payload, State), + amqp_channel:notify_received(DeliveryCtx), + {noreply, State1, hibernate}; +handle_info(#'basic.cancel'{consumer_tag = Ctag}, State) -> + process_request( + fun(StateN) -> server_cancel_consumer(Ctag, StateN) end, State); +handle_info({'EXIT', Conn, + {shutdown, {server_initiated_close, Code, Explanation}}}, + State = #state{connection = Conn}) -> + amqp_death(Code, Explanation, State); +handle_info({'EXIT', Conn, + {shutdown, {connection_closing, + {server_initiated_close, Code, Explanation}}}}, + State = #state{connection = Conn}) -> + amqp_death(Code, Explanation, State); +handle_info({'EXIT', Conn, Reason}, State = #state{connection = Conn}) -> + send_error("AMQP connection died", "Reason: ~p", [Reason], State), + {stop, {conn_died, Reason}, State}; + +handle_info({'EXIT', Ch, Reason}, State = #state{channel = Ch}) -> + send_error("AMQP channel died", "Reason: ~p", [Reason], State), + {stop, {channel_died, Reason}, State}; +handle_info({'EXIT', Ch, + {shutdown, {server_initiated_close, Code, Explanation}}}, + State = #state{channel = Ch}) -> + amqp_death(Code, Explanation, State); + + +handle_info({inet_reply, _, ok}, State) -> + {noreply, State, hibernate}; +handle_info({bump_credit, Msg}, State) -> + credit_flow:handle_bump_msg(Msg), + {noreply, State, hibernate}; +handle_info({inet_reply, _, Status}, State) -> + {stop, Status, State}. + +process_request(ProcessFun, State) -> + process_request(ProcessFun, fun (StateM) -> StateM end, State). + +process_request(ProcessFun, SuccessFun, State) -> + Res = case catch ProcessFun(State) of + {'EXIT', + {{shutdown, + {server_initiated_close, ReplyCode, Explanation}}, _}} -> + amqp_death(ReplyCode, Explanation, State); + {'EXIT', Reason} -> + priv_error("Processing error", "Processing error", + Reason, State); + Result -> + Result + end, + case Res of + {ok, Frame, NewState} -> + case Frame of + none -> ok; + _ -> send_frame(Frame, NewState) + end, + {noreply, SuccessFun(NewState), hibernate}; + {error, Message, Detail, NewState} -> + {noreply, send_error(Message, Detail, NewState), hibernate}; + {stop, normal, NewState} -> + {stop, normal, SuccessFun(NewState)}; + {stop, R, NewState} -> + {stop, R, NewState} + end. + +process_connect(Implicit, Frame, + State = #state{channel = none, + config = Config, + ssl_login_name = SSLLoginName, + adapter_info = AdapterInfo}) -> + process_request( + fun(StateN) -> + case negotiate_version(Frame) of + {ok, Version} -> + FT = frame_transformer(Version), + Frame1 = FT(Frame), + {Username, Passwd} = creds(Frame1, SSLLoginName, Config), + {ok, DefaultVHost} = application:get_env( + rabbitmq_stomp, default_vhost), + {ProtoName, _} = AdapterInfo#amqp_adapter_info.protocol, + Res = do_login( + Username, Passwd, + login_header(Frame1, ?HEADER_HOST, DefaultVHost), + login_header(Frame1, ?HEADER_HEART_BEAT, "0,0"), + AdapterInfo#amqp_adapter_info{ + protocol = {ProtoName, Version}}, Version, + StateN#state{frame_transformer = FT}), + case {Res, Implicit} of + {{ok, _, StateN1}, implicit} -> ok(StateN1); + _ -> Res + end; + {error, no_common_version} -> + error("Version mismatch", + "Supported versions are ~s~n", + [string:join(?SUPPORTED_VERSIONS, ",")], + StateN) + end + end, + State). + +creds(Frame, SSLLoginName, + #stomp_configuration{default_login = DefLogin, + default_passcode = DefPasscode}) -> + PasswordCreds = {login_header(Frame, ?HEADER_LOGIN, DefLogin), + login_header(Frame, ?HEADER_PASSCODE, DefPasscode)}, + case {rabbit_stomp_frame:header(Frame, ?HEADER_LOGIN), SSLLoginName} of + {not_found, none} -> PasswordCreds; + {not_found, SSLName} -> {SSLName, none}; + _ -> PasswordCreds + end. + +login_header(Frame, Key, Default) when is_binary(Default) -> + login_header(Frame, Key, binary_to_list(Default)); +login_header(Frame, Key, Default) -> + case rabbit_stomp_frame:header(Frame, Key, Default) of + undefined -> undefined; + Hdr -> list_to_binary(Hdr) + end. + +%%---------------------------------------------------------------------------- +%% Frame Transformation +%%---------------------------------------------------------------------------- +frame_transformer("1.0") -> fun rabbit_stomp_util:trim_headers/1; +frame_transformer(_) -> fun(Frame) -> Frame end. + +%%---------------------------------------------------------------------------- +%% Frame Validation +%%---------------------------------------------------------------------------- + +validate_frame(Command, Frame, State) + when Command =:= "SUBSCRIBE" orelse Command =:= "UNSUBSCRIBE" -> + Hdr = fun(Name) -> rabbit_stomp_frame:header(Frame, Name) end, + case {Hdr(?HEADER_PERSISTENT), Hdr(?HEADER_ID)} of + {{ok, "true"}, not_found} -> + error("Missing Header", + "Header 'id' is required for durable subscriptions", State); + _ -> + ok(State) + end; +validate_frame(_Command, _Frame, State) -> + ok(State). + +%%---------------------------------------------------------------------------- +%% Frame handlers +%%---------------------------------------------------------------------------- + +handle_frame("DISCONNECT", _Frame, State) -> + {stop, normal, close_connection(State)}; + +handle_frame("SUBSCRIBE", Frame, State) -> + with_destination("SUBSCRIBE", Frame, State, fun do_subscribe/4); + +handle_frame("UNSUBSCRIBE", Frame, State) -> + ConsumerTag = rabbit_stomp_util:consumer_tag(Frame), + cancel_subscription(ConsumerTag, Frame, State); + +handle_frame("SEND", Frame, State) -> + without_headers(?HEADERS_NOT_ON_SEND, "SEND", Frame, State, + fun (_Command, Frame1, State1) -> + with_destination("SEND", Frame1, State1, fun do_send/4) + end); + +handle_frame("ACK", Frame, State) -> + ack_action("ACK", Frame, State, fun create_ack_method/3); + +handle_frame("NACK", Frame, State) -> + ack_action("NACK", Frame, State, fun create_nack_method/3); + +handle_frame("BEGIN", Frame, State) -> + transactional_action(Frame, "BEGIN", fun begin_transaction/2, State); + +handle_frame("COMMIT", Frame, State) -> + transactional_action(Frame, "COMMIT", fun commit_transaction/2, State); + +handle_frame("ABORT", Frame, State) -> + transactional_action(Frame, "ABORT", fun abort_transaction/2, State); + +handle_frame(Command, _Frame, State) -> + error("Bad command", + "Could not interpret command ~p~n", + [Command], + State). + +%%---------------------------------------------------------------------------- +%% Internal helpers for processing frames callbacks +%%---------------------------------------------------------------------------- + +ack_action(Command, Frame, + State = #state{subscriptions = Subs, + channel = Channel, + version = Version}, MethodFun) -> + AckHeader = rabbit_stomp_util:ack_header_name(Version), + case rabbit_stomp_frame:header(Frame, AckHeader) of + {ok, AckValue} -> + case rabbit_stomp_util:parse_message_id(AckValue) of + {ok, {ConsumerTag, _SessionId, DeliveryTag}} -> + case dict:find(ConsumerTag, Subs) of + {ok, Sub} -> + Requeue = rabbit_stomp_frame:boolean_header(Frame, "requeue", true), + Method = MethodFun(DeliveryTag, Sub, Requeue), + case transactional(Frame) of + {yes, Transaction} -> + extend_transaction( + Transaction, {Method}, State); + no -> + amqp_channel:call(Channel, Method), + ok(State) + end; + error -> + error("Subscription not found", + "Message with id ~p has no subscription", + [AckValue], + State) + end; + _ -> + error("Invalid header", + "~p must include a valid ~p header~n", + [Command, AckHeader], + State) + end; + not_found -> + error("Missing header", + "~p must include the ~p header~n", + [Command, AckHeader], + State) + end. + +%%---------------------------------------------------------------------------- +%% Internal helpers for processing frames callbacks +%%---------------------------------------------------------------------------- +server_cancel_consumer(ConsumerTag, State = #state{subscriptions = Subs}) -> + case dict:find(ConsumerTag, Subs) of + error -> + error("Server cancelled unknown subscription", + "Consumer tag ~p is not associated with a subscription.~n", + [ConsumerTag], + State); + {ok, Subscription = #subscription{description = Description}} -> + Id = case rabbit_stomp_util:tag_to_id(ConsumerTag) of + {ok, {_, Id1}} -> Id1; + {error, {_, Id1}} -> "Unknown[" ++ Id1 ++ "]" + end, + send_error_frame("Server cancelled subscription", + [{?HEADER_SUBSCRIPTION, Id}], + "The server has canceled a subscription.~n" + "No more messages will be delivered for ~p.~n", + [Description], + State), + tidy_canceled_subscription(ConsumerTag, Subscription, + #stomp_frame{}, State) + end. + +cancel_subscription({error, invalid_prefix}, _Frame, State) -> + error("Invalid id", + "UNSUBSCRIBE 'id' may not start with ~s~n", + [?TEMP_QUEUE_ID_PREFIX], + State); + +cancel_subscription({error, _}, _Frame, State) -> + error("Missing destination or id", + "UNSUBSCRIBE must include a 'destination' or 'id' header", + State); + +cancel_subscription({ok, ConsumerTag, Description}, Frame, + State = #state{subscriptions = Subs, + channel = Channel}) -> + case dict:find(ConsumerTag, Subs) of + error -> + error("No subscription found", + "UNSUBSCRIBE must refer to an existing subscription.~n" + "Subscription to ~p not found.~n", + [Description], + State); + {ok, Subscription = #subscription{description = Descr}} -> + case amqp_channel:call(Channel, + #'basic.cancel'{ + consumer_tag = ConsumerTag}) of + #'basic.cancel_ok'{consumer_tag = ConsumerTag} -> + tidy_canceled_subscription(ConsumerTag, Subscription, + Frame, State); + _ -> + error("Failed to cancel subscription", + "UNSUBSCRIBE to ~p failed.~n", + [Descr], + State) + end + end. + +tidy_canceled_subscription(ConsumerTag, #subscription{dest_hdr = DestHdr}, + Frame, State = #state{subscriptions = Subs}) -> + Subs1 = dict:erase(ConsumerTag, Subs), + {ok, Dest} = rabbit_routing_util:parse_endpoint(DestHdr), + maybe_delete_durable_sub(Dest, Frame, State#state{subscriptions = Subs1}). + +maybe_delete_durable_sub({topic, Name}, Frame, + State = #state{channel = Channel}) -> + case rabbit_stomp_frame:boolean_header(Frame, + ?HEADER_PERSISTENT, false) of + true -> + {ok, Id} = rabbit_stomp_frame:header(Frame, ?HEADER_ID), + QName = rabbit_stomp_util:subscription_queue_name(Name, Id), + amqp_channel:call(Channel, + #'queue.delete'{queue = list_to_binary(QName), + nowait = false}), + ok(State); + false -> + ok(State) + end; +maybe_delete_durable_sub(_Destination, _Frame, State) -> + ok(State). + +with_destination(Command, Frame, State, Fun) -> + case rabbit_stomp_frame:header(Frame, ?HEADER_DESTINATION) of + {ok, DestHdr} -> + case rabbit_routing_util:parse_endpoint(DestHdr) of + {ok, Destination} -> + case Fun(Destination, DestHdr, Frame, State) of + {error, invalid_endpoint} -> + error("Invalid destination", + "'~s' is not a valid destination for '~s'~n", + [DestHdr, Command], + State); + {error, {invalid_destination, Msg}} -> + error("Invalid destination", + "~s", + [Msg], + State); + {error, Reason} -> + throw(Reason); + Result -> + Result + end; + {error, {invalid_destination, Type, Content}} -> + error("Invalid destination", + "'~s' is not a valid ~p destination~n", + [Content, Type], + State); + {error, {unknown_destination, Content}} -> + error("Unknown destination", + "'~s' is not a valid destination.~n" + "Valid destination types are: ~s.~n", + [Content, + string:join(rabbit_routing_util:all_dest_prefixes(), + ", ")], State) + end; + not_found -> + error("Missing destination", + "~p must include a 'destination' header~n", + [Command], + State) + end. + +without_headers([Hdr | Hdrs], Command, Frame, State, Fun) -> + case rabbit_stomp_frame:header(Frame, Hdr) of + {ok, _} -> + error("Invalid header", + "'~s' is not allowed on '~s'.~n", + [Hdr, Command], + State); + not_found -> + without_headers(Hdrs, Command, Frame, State, Fun) + end; +without_headers([], Command, Frame, State, Fun) -> + Fun(Command, Frame, State). + +do_login(undefined, _, _, _, _, _, State) -> + error("Bad CONNECT", "Missing login or passcode header(s)", State); +do_login(Username, Passwd, VirtualHost, Heartbeat, AdapterInfo, Version, + State = #state{peer_addr = Addr}) -> + case start_connection( + #amqp_params_direct{username = Username, + password = Passwd, + virtual_host = VirtualHost, + adapter_info = AdapterInfo}, Username, Addr) of + {ok, Connection} -> + link(Connection), + {ok, Channel} = amqp_connection:open_channel(Connection), + link(Channel), + amqp_channel:enable_delivery_flow_control(Channel), + SessionId = rabbit_guid:string(rabbit_guid:gen_secure(), "session"), + {{SendTimeout, ReceiveTimeout}, State1} = + ensure_heartbeats(Heartbeat, State), + ok("CONNECTED", + [{?HEADER_SESSION, SessionId}, + {?HEADER_HEART_BEAT, + io_lib:format("~B,~B", [SendTimeout, ReceiveTimeout])}, + {?HEADER_SERVER, server_header()}, + {?HEADER_VERSION, Version}], + "", + State1#state{session_id = SessionId, + channel = Channel, + connection = Connection, + version = Version}); + {error, {auth_failure, _}} -> + rabbit_log:warning("STOMP login failed for user ~p~n", + [binary_to_list(Username)]), + error("Bad CONNECT", "Access refused for user '" ++ + binary_to_list(Username) ++ "'~n", [], State); + {error, access_refused} -> + rabbit_log:warning("STOMP login failed - access_refused " + "(vhost access not allowed)~n"), + error("Bad CONNECT", "Virtual host '" ++ + binary_to_list(VirtualHost) ++ + "' access denied", State); + {error, not_loopback} -> + rabbit_log:warning("STOMP login failed - access_refused " + "(user must access over loopback)~n"), + error("Bad CONNECT", "non-loopback access denied", State) + end. + +start_connection(Params, Username, Addr) -> + case amqp_connection:start(Params) of + {ok, Conn} -> case rabbit_access_control:check_user_loopback( + Username, Addr) of + ok -> {ok, Conn}; + not_allowed -> amqp_connection:close(Conn), + {error, not_loopback} + end; + {error, E} -> {error, E} + end. + +server_header() -> + {ok, Product} = application:get_key(rabbit, id), + {ok, Version} = application:get_key(rabbit, vsn), + rabbit_misc:format("~s/~s", [Product, Version]). + +do_subscribe(Destination, DestHdr, Frame, + State = #state{subscriptions = Subs, + route_state = RouteState, + channel = Channel}) -> + Prefetch = + rabbit_stomp_frame:integer_header(Frame, ?HEADER_PREFETCH_COUNT, + undefined), + {AckMode, IsMulti} = rabbit_stomp_util:ack_mode(Frame), + case ensure_endpoint(source, Destination, Frame, Channel, RouteState) of + {ok, Queue, RouteState1} -> + {ok, ConsumerTag, Description} = + rabbit_stomp_util:consumer_tag(Frame), + case Prefetch of + undefined -> ok; + _ -> amqp_channel:call( + Channel, #'basic.qos'{prefetch_count = Prefetch}) + end, + case dict:find(ConsumerTag, Subs) of + {ok, _} -> + Message = "Duplicated subscription identifier", + Detail = "A subscription identified by '~s' alredy exists.", + error(Message, Detail, [ConsumerTag], State), + send_error(Message, Detail, [ConsumerTag], State), + {stop, normal, close_connection(State)}; + error -> + ExchangeAndKey = + rabbit_routing_util:parse_routing(Destination), + try + amqp_channel:subscribe(Channel, + #'basic.consume'{ + queue = Queue, + consumer_tag = ConsumerTag, + no_local = false, + no_ack = (AckMode == auto), + exclusive = false, + arguments = []}, + self()), + ok = rabbit_routing_util:ensure_binding( + Queue, ExchangeAndKey, Channel) + catch exit:Err -> + %% it's safe to delete this queue, it + %% was server-named and declared by us + case Destination of + {exchange, _} -> + ok = maybe_clean_up_queue(Queue, State); + {topic, _} -> + ok = maybe_clean_up_queue(Queue, State); + _ -> + ok + end, + exit(Err) + end, + ok(State#state{subscriptions = + dict:store( + ConsumerTag, + #subscription{dest_hdr = DestHdr, + ack_mode = AckMode, + multi_ack = IsMulti, + description = Description}, + Subs), + route_state = RouteState1}) + end; + {error, _} = Err -> + Err + end. + +maybe_clean_up_queue(Queue, #state{connection = Connection}) -> + {ok, Channel} = amqp_connection:open_channel(Connection), + catch amqp_channel:call(Channel, #'queue.delete'{queue = Queue}), + catch amqp_channel:close(Channel), + ok. + +do_send(Destination, _DestHdr, + Frame = #stomp_frame{body_iolist = BodyFragments}, + State = #state{channel = Channel, route_state = RouteState}) -> + case ensure_endpoint(dest, Destination, Frame, Channel, RouteState) of + + {ok, _Q, RouteState1} -> + + {Frame1, State1} = + ensure_reply_to(Frame, State#state{route_state = RouteState1}), + + Props = rabbit_stomp_util:message_properties(Frame1), + + {Exchange, RoutingKey} = + rabbit_routing_util:parse_routing(Destination), + + Method = #'basic.publish'{ + exchange = list_to_binary(Exchange), + routing_key = list_to_binary(RoutingKey), + mandatory = false, + immediate = false}, + + case transactional(Frame1) of + {yes, Transaction} -> + extend_transaction( + Transaction, + fun(StateN) -> + maybe_record_receipt(Frame1, StateN) + end, + {Method, Props, BodyFragments}, + State1); + no -> + ok(send_method(Method, Props, BodyFragments, + maybe_record_receipt(Frame1, State1))) + end; + + {error, _} = Err -> + + Err + end. + +create_ack_method(DeliveryTag, #subscription{multi_ack = IsMulti}, _) -> + #'basic.ack'{delivery_tag = DeliveryTag, + multiple = IsMulti}. + +create_nack_method(DeliveryTag, #subscription{multi_ack = IsMulti}, Requeue) -> + #'basic.nack'{delivery_tag = DeliveryTag, + multiple = IsMulti, + requeue = Requeue}. + +negotiate_version(Frame) -> + ClientVers = re:split(rabbit_stomp_frame:header( + Frame, ?HEADER_ACCEPT_VERSION, "1.0"), + ",", [{return, list}]), + rabbit_stomp_util:negotiate_version(ClientVers, ?SUPPORTED_VERSIONS). + + +send_delivery(Delivery = #'basic.deliver'{consumer_tag = ConsumerTag}, + Properties, Body, + State = #state{session_id = SessionId, + subscriptions = Subs, + version = Version}) -> + case dict:find(ConsumerTag, Subs) of + {ok, #subscription{ack_mode = AckMode}} -> + send_frame( + "MESSAGE", + rabbit_stomp_util:headers(SessionId, Delivery, Properties, + AckMode, Version), + Body, + State); + error -> + send_error("Subscription not found", + "There is no current subscription with tag '~s'.", + [ConsumerTag], + State) + end. + + +send_method(Method, Channel, State) -> + amqp_channel:call(Channel, Method), + State. + +send_method(Method, State = #state{channel = Channel}) -> + send_method(Method, Channel, State). + +send_method(Method, Properties, BodyFragments, + State = #state{channel = Channel}) -> + send_method(Method, Channel, Properties, BodyFragments, State). + +send_method(Method = #'basic.publish'{}, Channel, Properties, BodyFragments, + State) -> + amqp_channel:cast_flow( + Channel, Method, + #amqp_msg{props = Properties, + payload = list_to_binary(BodyFragments)}), + State. + +close_connection(State = #state{connection = none}) -> + State; +%% Closing the connection will close the channel and subchannels +close_connection(State = #state{connection = Connection}) -> + %% ignore noproc or other exceptions to avoid debris + catch amqp_connection:close(Connection), + State#state{channel = none, connection = none, subscriptions = none}. + +%%---------------------------------------------------------------------------- +%% Reply-To +%%---------------------------------------------------------------------------- +ensure_reply_to(Frame = #stomp_frame{headers = Headers}, State) -> + case rabbit_stomp_frame:header(Frame, ?HEADER_REPLY_TO) of + not_found -> + {Frame, State}; + {ok, ReplyTo} -> + {ok, Destination} = rabbit_routing_util:parse_endpoint(ReplyTo), + case rabbit_routing_util:dest_temp_queue(Destination) of + none -> + {Frame, State}; + TempQueueId -> + {ReplyQueue, State1} = + ensure_reply_queue(TempQueueId, State), + {Frame#stomp_frame{ + headers = lists:keyreplace( + ?HEADER_REPLY_TO, 1, Headers, + {?HEADER_REPLY_TO, ReplyQueue})}, + State1} + end + end. + +ensure_reply_queue(TempQueueId, State = #state{channel = Channel, + reply_queues = RQS, + subscriptions = Subs}) -> + case dict:find(TempQueueId, RQS) of + {ok, RQ} -> + {binary_to_list(RQ), State}; + error -> + #'queue.declare_ok'{queue = Queue} = + amqp_channel:call(Channel, + #'queue.declare'{auto_delete = true, + exclusive = true}), + + ConsumerTag = rabbit_stomp_util:consumer_tag_reply_to(TempQueueId), + #'basic.consume_ok'{} = + amqp_channel:subscribe(Channel, + #'basic.consume'{ + queue = Queue, + consumer_tag = ConsumerTag, + no_ack = true, + nowait = false}, + self()), + + Destination = binary_to_list(Queue), + + %% synthesise a subscription to the reply queue destination + Subs1 = dict:store(ConsumerTag, + #subscription{dest_hdr = Destination, + multi_ack = false}, + Subs), + + {Destination, State#state{ + reply_queues = dict:store(TempQueueId, Queue, RQS), + subscriptions = Subs1}} + end. + +%%---------------------------------------------------------------------------- +%% Receipt Handling +%%---------------------------------------------------------------------------- + +ensure_receipt(Frame = #stomp_frame{command = Command}, State) -> + case rabbit_stomp_frame:header(Frame, ?HEADER_RECEIPT) of + {ok, Id} -> do_receipt(Command, Id, State); + not_found -> State + end. + +do_receipt("SEND", _, State) -> + %% SEND frame receipts are handled when messages are confirmed + State; +do_receipt(_Frame, ReceiptId, State) -> + send_frame("RECEIPT", [{"receipt-id", ReceiptId}], "", State). + +maybe_record_receipt(Frame, State = #state{channel = Channel, + pending_receipts = PR}) -> + case rabbit_stomp_frame:header(Frame, ?HEADER_RECEIPT) of + {ok, Id} -> + PR1 = case PR of + undefined -> + amqp_channel:register_confirm_handler( + Channel, self()), + #'confirm.select_ok'{} = + amqp_channel:call(Channel, #'confirm.select'{}), + gb_trees:empty(); + _ -> + PR + end, + SeqNo = amqp_channel:next_publish_seqno(Channel), + State#state{pending_receipts = gb_trees:insert(SeqNo, Id, PR1)}; + not_found -> + State + end. + +flush_pending_receipts(DeliveryTag, IsMulti, + State = #state{pending_receipts = PR}) -> + {Receipts, PR1} = accumulate_receipts(DeliveryTag, IsMulti, PR), + State1 = lists:foldl(fun(ReceiptId, StateN) -> + do_receipt(none, ReceiptId, StateN) + end, State, Receipts), + State1#state{pending_receipts = PR1}. + +accumulate_receipts(DeliveryTag, false, PR) -> + case gb_trees:lookup(DeliveryTag, PR) of + {value, ReceiptId} -> {[ReceiptId], gb_trees:delete(DeliveryTag, PR)}; + none -> {[], PR} + end; + +accumulate_receipts(DeliveryTag, true, PR) -> + case gb_trees:is_empty(PR) of + true -> {[], PR}; + false -> accumulate_receipts1(DeliveryTag, + gb_trees:take_smallest(PR), []) + end. + +accumulate_receipts1(DeliveryTag, {Key, Value, PR}, Acc) + when Key > DeliveryTag -> + {lists:reverse(Acc), gb_trees:insert(Key, Value, PR)}; +accumulate_receipts1(DeliveryTag, {_Key, Value, PR}, Acc) -> + Acc1 = [Value | Acc], + case gb_trees:is_empty(PR) of + true -> {lists:reverse(Acc1), PR}; + false -> accumulate_receipts1(DeliveryTag, + gb_trees:take_smallest(PR), Acc1) + end. + + +%%---------------------------------------------------------------------------- +%% Transaction Support +%%---------------------------------------------------------------------------- + +transactional(Frame) -> + case rabbit_stomp_frame:header(Frame, ?HEADER_TRANSACTION) of + {ok, Transaction} -> {yes, Transaction}; + not_found -> no + end. + +transactional_action(Frame, Name, Fun, State) -> + case transactional(Frame) of + {yes, Transaction} -> + Fun(Transaction, State); + no -> + error("Missing transaction", + "~p must include a 'transaction' header~n", + [Name], + State) + end. + +with_transaction(Transaction, State, Fun) -> + case get({transaction, Transaction}) of + undefined -> + error("Bad transaction", + "Invalid transaction identifier: ~p~n", + [Transaction], + State); + Actions -> + Fun(Actions, State) + end. + +begin_transaction(Transaction, State) -> + put({transaction, Transaction}, []), + ok(State). + +extend_transaction(Transaction, Callback, Action, State) -> + extend_transaction(Transaction, {callback, Callback, Action}, State). + +extend_transaction(Transaction, Action, State0) -> + with_transaction( + Transaction, State0, + fun (Actions, State) -> + put({transaction, Transaction}, [Action | Actions]), + ok(State) + end). + +commit_transaction(Transaction, State0) -> + with_transaction( + Transaction, State0, + fun (Actions, State) -> + FinalState = lists:foldr(fun perform_transaction_action/2, + State, + Actions), + erase({transaction, Transaction}), + ok(FinalState) + end). + +abort_transaction(Transaction, State0) -> + with_transaction( + Transaction, State0, + fun (_Actions, State) -> + erase({transaction, Transaction}), + ok(State) + end). + +perform_transaction_action({callback, Callback, Action}, State) -> + perform_transaction_action(Action, Callback(State)); +perform_transaction_action({Method}, State) -> + send_method(Method, State); +perform_transaction_action({Method, Props, BodyFragments}, State) -> + send_method(Method, Props, BodyFragments, State). + +%%-------------------------------------------------------------------- +%% Heartbeat Management +%%-------------------------------------------------------------------- + +ensure_heartbeats(Heartbeats, + State = #state{start_heartbeat_fun = SHF, + send_fun = RawSendFun}) -> + [CX, CY] = [list_to_integer(X) || + X <- re:split(Heartbeats, ",", [{return, list}])], + + SendFun = fun() -> RawSendFun(sync, <<$\n>>) end, + Pid = self(), + ReceiveFun = fun() -> gen_server2:cast(Pid, client_timeout) end, + + {SendTimeout, ReceiveTimeout} = + {millis_to_seconds(CY), millis_to_seconds(CX)}, + + SHF(SendTimeout, SendFun, ReceiveTimeout, ReceiveFun), + + {{SendTimeout * 1000 , ReceiveTimeout * 1000}, State}. + +millis_to_seconds(M) when M =< 0 -> 0; +millis_to_seconds(M) when M < 1000 -> 1; +millis_to_seconds(M) -> M div 1000. + +%%---------------------------------------------------------------------------- +%% Queue Setup +%%---------------------------------------------------------------------------- + +ensure_endpoint(_Direction, {queue, []}, _Frame, _Channel, _State) -> + {error, {invalid_destination, "Destination cannot be blank"}}; + +ensure_endpoint(source, EndPoint, {_, _, Headers, _} = Frame, Channel, State) -> + Params = + case rabbit_stomp_frame:boolean_header( + Frame, ?HEADER_PERSISTENT, false) of + true -> + [{subscription_queue_name_gen, + fun () -> + {ok, Id} = rabbit_stomp_frame:header(Frame, ?HEADER_ID), + {_, Name} = rabbit_routing_util:parse_routing(EndPoint), + list_to_binary( + rabbit_stomp_util:subscription_queue_name(Name, + Id)) + end}, + {durable, true}]; + false -> + [{subscription_queue_name_gen, + fun () -> + Id = rabbit_guid:gen_secure(), + {_, Name} = rabbit_routing_util:parse_routing(EndPoint), + list_to_binary( + rabbit_stomp_util:subscription_queue_name(Name, + Id)) + end}, + {durable, false}] + end, + Arguments = rabbit_stomp_util:build_arguments(Headers), + rabbit_routing_util:ensure_endpoint(source, Channel, EndPoint, [Arguments | Params], State); + +ensure_endpoint(Direction, Endpoint, {_, _, Headers, _}, Channel, State) -> + Arguments = rabbit_stomp_util:build_arguments(Headers), + rabbit_routing_util:ensure_endpoint(Direction, Channel, Endpoint, [Arguments], State). + +%%---------------------------------------------------------------------------- +%% Success/error handling +%%---------------------------------------------------------------------------- + +ok(State) -> + {ok, none, State}. + +ok(Command, Headers, BodyFragments, State) -> + {ok, #stomp_frame{command = Command, + headers = Headers, + body_iolist = BodyFragments}, State}. + +amqp_death(ReplyCode, Explanation, State) -> + ErrorName = amqp_connection:error_atom(ReplyCode), + ErrorDesc = rabbit_misc:format("~s~n", [Explanation]), + log_error(ErrorName, ErrorDesc, none), + {stop, normal, close_connection(send_error(atom_to_list(ErrorName), ErrorDesc, State))}. + +error(Message, Detail, State) -> + priv_error(Message, Detail, none, State). + +error(Message, Format, Args, State) -> + priv_error(Message, Format, Args, none, State). + +priv_error(Message, Detail, ServerPrivateDetail, State) -> + log_error(Message, Detail, ServerPrivateDetail), + {error, Message, Detail, State}. + +priv_error(Message, Format, Args, ServerPrivateDetail, State) -> + priv_error(Message, rabbit_misc:format(Format, Args), ServerPrivateDetail, + State). + +log_error(Message, Detail, ServerPrivateDetail) -> + rabbit_log:error("STOMP error frame sent:~n" + "Message: ~p~n" + "Detail: ~p~n" + "Server private detail: ~p~n", + [Message, Detail, ServerPrivateDetail]). + +%%---------------------------------------------------------------------------- +%% Frame sending utilities +%%---------------------------------------------------------------------------- +send_frame(Command, Headers, BodyFragments, State) -> + send_frame(#stomp_frame{command = Command, + headers = Headers, + body_iolist = BodyFragments}, + State). + +send_frame(Frame, State = #state{send_fun = SendFun, + trailing_lf = TrailingLF}) -> + SendFun(async, rabbit_stomp_frame:serialize(Frame, TrailingLF)), + State. + +send_error_frame(Message, ExtraHeaders, Format, Args, State) -> + send_error_frame(Message, ExtraHeaders, rabbit_misc:format(Format, Args), + State). + +send_error_frame(Message, ExtraHeaders, Detail, State) -> + send_frame("ERROR", [{"message", Message}, + {"content-type", "text/plain"}, + {"version", string:join(?SUPPORTED_VERSIONS, ",")}] ++ + ExtraHeaders, + Detail, State). + +send_error(Message, Detail, State) -> + send_error_frame(Message, [], Detail, State). + +send_error(Message, Format, Args, State) -> + send_error(Message, rabbit_misc:format(Format, Args), State). + +%%---------------------------------------------------------------------------- +%% Skeleton gen_server2 callbacks +%%---------------------------------------------------------------------------- +handle_call(_Msg, _From, State) -> + {noreply, State}. + +code_change(_OldVsn, State, _Extra) -> + {ok, State}. diff --git a/rabbitmq-server/plugins-src/rabbitmq-stomp/src/rabbit_stomp_reader.erl b/rabbitmq-server/plugins-src/rabbitmq-stomp/src/rabbit_stomp_reader.erl new file mode 100644 index 0000000..673afee --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-stomp/src/rabbit_stomp_reader.erl @@ -0,0 +1,243 @@ +%% The contents of this file are subject to the Mozilla Public License +%% Version 1.1 (the "License"); you may not use this file except in +%% compliance with the License. You may obtain a copy of the License +%% at http://www.mozilla.org/MPL/ +%% +%% Software distributed under the License is distributed on an "AS IS" +%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See +%% the License for the specific language governing rights and +%% limitations under the License. +%% +%% The Original Code is RabbitMQ. +%% +%% The Initial Developer of the Original Code is GoPivotal, Inc. +%% Copyright (c) 2007-2015 Pivotal Software, Inc. All rights reserved. +%% + +-module(rabbit_stomp_reader). + +-export([start_link/3]). +-export([init/3, mainloop/2]). +-export([system_continue/3, system_terminate/4, system_code_change/4]). +-export([conserve_resources/3]). + +-include("rabbit_stomp.hrl"). +-include("rabbit_stomp_frame.hrl"). +-include_lib("amqp_client/include/amqp_client.hrl"). + +-record(reader_state, {socket, parse_state, processor, state, + conserve_resources, recv_outstanding, + parent}). + +%%---------------------------------------------------------------------------- + +start_link(SupHelperPid, ProcessorPid, Configuration) -> + {ok, proc_lib:spawn_link(?MODULE, init, + [SupHelperPid, ProcessorPid, Configuration])}. + +log(Level, Fmt, Args) -> rabbit_log:log(connection, Level, Fmt, Args). + +init(SupHelperPid, ProcessorPid, Configuration) -> + Reply = go(SupHelperPid, ProcessorPid, Configuration), + rabbit_stomp_processor:flush_and_die(ProcessorPid), + Reply. + +go(SupHelperPid, ProcessorPid, Configuration) -> + process_flag(trap_exit, true), + receive + {go, Sock0, SockTransform} -> + case rabbit_net:connection_string(Sock0, inbound) of + {ok, ConnStr} -> + case SockTransform(Sock0) of + {ok, Sock} -> + DebugOpts = sys:debug_options([]), + ProcInitArgs = processor_args(SupHelperPid, + Configuration, + Sock), + rabbit_stomp_processor:init_arg(ProcessorPid, + ProcInitArgs), + log(info, "accepting STOMP connection ~p (~s)~n", + [self(), ConnStr]), + + ParseState = rabbit_stomp_frame:initial_state(), + try + mainloop(DebugOpts, + register_resource_alarm( + #reader_state{socket = Sock, + parse_state = ParseState, + processor = ProcessorPid, + state = running, + conserve_resources = false, + recv_outstanding = false})), + log(info, "closing STOMP connection ~p (~s)~n", + [self(), ConnStr]) + catch _:Ex -> + log_network_error(ConnStr, Ex), + rabbit_net:fast_close(Sock), + rabbit_stomp_processor:flush_and_die(ProcessorPid), + exit(normal) + end, + done; + {error, enotconn} -> + rabbit_net:fast_close(Sock0), + rabbit_stomp_processor:flush_and_die(ProcessorPid), + exit(normal); + {error, Reason} -> + log_network_error(ConnStr, Reason), + rabbit_net:fast_close(Sock0), + rabbit_stomp_processor:flush_and_die(ProcessorPid), + exit(normal) + end + end + end. + +mainloop(DebugOpts, State0 = #reader_state{socket = Sock}) -> + State = run_socket(control_throttle(State0)), + receive + {inet_async, Sock, _Ref, {ok, Data}} -> + mainloop(DebugOpts, process_received_bytes( + Data, State#reader_state{recv_outstanding = false})); + {inet_async, _Sock, _Ref, {error, closed}} -> + ok; + {inet_async, _Sock, _Ref, {error, Reason}} -> + throw({inet_error, Reason}); + {inet_reply, _Sock, {error, closed}} -> + ok; + {conserve_resources, Conserve} -> + mainloop(DebugOpts, State#reader_state{conserve_resources = Conserve}); + {bump_credit, Msg} -> + credit_flow:handle_bump_msg(Msg), + mainloop(DebugOpts, State); + {system, From, Request} -> + sys:handle_system_msg(Request, From, State#reader_state.parent, + ?MODULE, DebugOpts, State); + {'EXIT', _From, shutdown} -> + ok; + Other -> + log(warning, "STOMP connection ~p received " + "an unexpected message ~p~n", [Other]), + ok + end. + +process_received_bytes([], State) -> + State; +process_received_bytes(Bytes, + State = #reader_state{ + processor = Processor, + parse_state = ParseState, + state = S}) -> + case rabbit_stomp_frame:parse(Bytes, ParseState) of + {more, ParseState1} -> + State#reader_state{parse_state = ParseState1}; + {ok, Frame, Rest} -> + rabbit_stomp_processor:process_frame(Processor, Frame), + PS = rabbit_stomp_frame:initial_state(), + process_received_bytes(Rest, State#reader_state{ + parse_state = PS, + state = next_state(S, Frame)}) + end. + +conserve_resources(Pid, _Source, Conserve) -> + Pid ! {conserve_resources, Conserve}, + ok. + +register_resource_alarm(State) -> + rabbit_alarm:register(self(), {?MODULE, conserve_resources, []}), State. + +control_throttle(State = #reader_state{state = CS, + conserve_resources = Mem}) -> + case {CS, Mem orelse credit_flow:blocked()} of + {running, true} -> State#reader_state{state = blocking}; + {blocking, false} -> State#reader_state{state = running}; + {blocked, false} -> State#reader_state{state = running}; + {_, _} -> State + end. + +next_state(blocking, #stomp_frame{command = "SEND"}) -> + blocked; +next_state(S, _) -> + S. + +run_socket(State = #reader_state{state = blocked}) -> + State; +run_socket(State = #reader_state{recv_outstanding = true}) -> + State; +run_socket(State = #reader_state{socket = Sock}) -> + rabbit_net:async_recv(Sock, 0, infinity), + State#reader_state{recv_outstanding = true}. + +%%---------------------------------------------------------------------------- + +system_continue(Parent, DebugOpts, State) -> + mainloop(DebugOpts, State#reader_state{parent = Parent}). + +system_terminate(Reason, _Parent, _OldVsn, _Extra) -> + exit(Reason). + +system_code_change(Misc, _Module, _OldSvn, _Extra) -> + {ok, Misc}. + +%%---------------------------------------------------------------------------- + +processor_args(SupPid, Configuration, Sock) -> + SendFun = fun (sync, IoData) -> + %% no messages emitted + catch rabbit_net:send(Sock, IoData); + (async, IoData) -> + %% {inet_reply, _, _} will appear soon + %% We ignore certain errors here, as we will be + %% receiving an asynchronous notification of the + %% same (or a related) fault shortly anyway. See + %% bug 21365. + catch rabbit_net:port_command(Sock, IoData) + end, + + StartHeartbeatFun = + fun (SendTimeout, SendFin, ReceiveTimeout, ReceiveFun) -> + rabbit_heartbeat:start(SupPid, Sock, SendTimeout, + SendFin, ReceiveTimeout, ReceiveFun) + end, + {ok, {PeerAddr, _PeerPort}} = rabbit_net:sockname(Sock), + [SendFun, adapter_info(Sock), StartHeartbeatFun, + ssl_login_name(Sock, Configuration), PeerAddr]. + +adapter_info(Sock) -> + amqp_connection:socket_adapter_info(Sock, {'STOMP', 0}). + +ssl_login_name(_Sock, #stomp_configuration{ssl_cert_login = false}) -> + none; +ssl_login_name(Sock, #stomp_configuration{ssl_cert_login = true}) -> + case rabbit_net:peercert(Sock) of + {ok, C} -> case rabbit_ssl:peer_cert_auth_name(C) of + unsafe -> none; + not_found -> none; + Name -> Name + end; + {error, no_peercert} -> none; + nossl -> none + end. + +%%---------------------------------------------------------------------------- + +log_network_error(ConnStr, {ssl_upgrade_error, + {tls_alert, "handshake failure"}}) -> + log(error, "STOMP detected TLS upgrade error on " + "~p (~s): handshake failure~n", [self(), ConnStr]); + +log_network_error(ConnStr, {ssl_upgrade_error, + {tls_alert, "unknown ca"}}) -> + log(error, "STOMP detected TLS certificate " + "verification error on " + "~p (~s): alert 'unknown CA'~n", [self(), ConnStr]); + +log_network_error(ConnStr, {ssl_upgrade_error, {tls_alert, Alert}}) -> + log(error, "STOMP detected TLS upgrade error on " + "~p (~s): alert ~s~n", [self(), ConnStr, Alert]); + +log_network_error(ConnStr, {ssl_upgrade_error, closed}) -> + log(error, "STOMP detected TLS upgrade error on " + "~p (~s): connection closed~n", [self(), ConnStr]); + +log_network_error(ConnStr, Ex) -> + log(error, "STOMP detected network error on " + "~p (~s):~n~p~n", [self(), ConnStr, Ex]). diff --git a/rabbitmq-server/plugins-src/rabbitmq-stomp/src/rabbit_stomp_sup.erl b/rabbitmq-server/plugins-src/rabbitmq-stomp/src/rabbit_stomp_sup.erl new file mode 100644 index 0000000..d376e5e --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-stomp/src/rabbit_stomp_sup.erl @@ -0,0 +1,83 @@ +%% The contents of this file are subject to the Mozilla Public License +%% Version 1.1 (the "License"); you may not use this file except in +%% compliance with the License. You may obtain a copy of the License +%% at http://www.mozilla.org/MPL/ +%% +%% Software distributed under the License is distributed on an "AS IS" +%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See +%% the License for the specific language governing rights and +%% limitations under the License. +%% +%% The Original Code is RabbitMQ. +%% +%% The Initial Developer of the Original Code is GoPivotal, Inc. +%% Copyright (c) 2007-2015 Pivotal Software, Inc. All rights reserved. +%% + +-module(rabbit_stomp_sup). +-behaviour(supervisor). + +-export([start_link/2, init/1]). + +-export([start_client/2, start_ssl_client/3]). + +start_link(Listeners, Configuration) -> + supervisor:start_link({local, ?MODULE}, ?MODULE, + [Listeners, Configuration]). + +init([{Listeners, SslListeners0}, Configuration]) -> + {ok, SocketOpts} = application:get_env(rabbitmq_stomp, tcp_listen_options), + {SslOpts, SslListeners} + = case SslListeners0 of + [] -> {none, []}; + _ -> {rabbit_networking:ensure_ssl(), + case rabbit_networking:poodle_check('STOMP') of + ok -> SslListeners0; + danger -> [] + end} + end, + {ok, {{one_for_all, 10, 10}, + [{rabbit_stomp_client_sup_sup, + {rabbit_client_sup, start_link, + [{local, rabbit_stomp_client_sup_sup}, + {rabbit_stomp_client_sup, start_link,[]}]}, + transient, infinity, supervisor, [rabbit_client_sup]} | + listener_specs(fun tcp_listener_spec/1, + [SocketOpts, Configuration], Listeners) ++ + listener_specs(fun ssl_listener_spec/1, + [SocketOpts, SslOpts, Configuration], SslListeners)]}}. + +listener_specs(Fun, Args, Listeners) -> + [Fun([Address | Args]) || + Listener <- Listeners, + Address <- rabbit_networking:tcp_listener_addresses(Listener)]. + +tcp_listener_spec([Address, SocketOpts, Configuration]) -> + rabbit_networking:tcp_listener_spec( + rabbit_stomp_listener_sup, Address, SocketOpts, + stomp, "STOMP TCP Listener", + {?MODULE, start_client, [Configuration]}). + +ssl_listener_spec([Address, SocketOpts, SslOpts, Configuration]) -> + rabbit_networking:tcp_listener_spec( + rabbit_stomp_listener_sup, Address, SocketOpts, + 'stomp/ssl', "STOMP SSL Listener", + {?MODULE, start_ssl_client, [Configuration, SslOpts]}). + +start_client(Configuration, Sock, SockTransform) -> + {ok, _Child, Reader} = supervisor:start_child(rabbit_stomp_client_sup_sup, + [Configuration]), + ok = rabbit_net:controlling_process(Sock, Reader), + Reader ! {go, Sock, SockTransform}, + + %% see comment in rabbit_networking:start_client/2 + gen_event:which_handlers(error_logger), + + Reader. + +start_client(Configuration, Sock) -> + start_client(Configuration, Sock, fun (S) -> {ok, S} end). + +start_ssl_client(Configuration, SslOpts, Sock) -> + Transform = rabbit_networking:ssl_transform_fun(SslOpts), + start_client(Configuration, Sock, Transform). diff --git a/rabbitmq-server/plugins-src/rabbitmq-stomp/src/rabbit_stomp_util.erl b/rabbitmq-server/plugins-src/rabbitmq-stomp/src/rabbit_stomp_util.erl new file mode 100644 index 0000000..9034a2b --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-stomp/src/rabbit_stomp_util.erl @@ -0,0 +1,357 @@ +%% The contents of this file are subject to the Mozilla Public License +%% Version 1.1 (the "License"); you may not use this file except in +%% compliance with the License. You may obtain a copy of the License +%% at http://www.mozilla.org/MPL/ +%% +%% Software distributed under the License is distributed on an "AS IS" +%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See +%% the License for the specific language governing rights and +%% limitations under the License. +%% +%% The Original Code is RabbitMQ. +%% +%% The Initial Developer of the Original Code is GoPivotal, Inc. +%% Copyright (c) 2007-2015 Pivotal Software, Inc. All rights reserved. +%% + +-module(rabbit_stomp_util). + +-export([parse_message_id/1, subscription_queue_name/2]). +-export([longstr_field/2]). +-export([ack_mode/1, consumer_tag_reply_to/1, consumer_tag/1, message_headers/1, + headers_post_process/1, headers/5, message_properties/1, tag_to_id/1, + msg_header_name/1, ack_header_name/1, build_arguments/1]). +-export([negotiate_version/2]). +-export([trim_headers/1]). + +-include_lib("amqp_client/include/amqp_client.hrl"). +-include_lib("amqp_client/include/rabbit_routing_prefixes.hrl"). +-include("rabbit_stomp_frame.hrl"). +-include("rabbit_stomp_headers.hrl"). + +-define(INTERNAL_TAG_PREFIX, "T_"). +-define(QUEUE_TAG_PREFIX, "Q_"). + +%%-------------------------------------------------------------------- +%% Frame and Header Parsing +%%-------------------------------------------------------------------- + +consumer_tag_reply_to(QueueId) -> + internal_tag(?TEMP_QUEUE_ID_PREFIX ++ QueueId). + +consumer_tag(Frame) -> + case rabbit_stomp_frame:header(Frame, ?HEADER_ID) of + {ok, Id} -> + case lists:prefix(?TEMP_QUEUE_ID_PREFIX, Id) of + false -> {ok, internal_tag(Id), "id='" ++ Id ++ "'"}; + true -> {error, invalid_prefix} + end; + not_found -> + case rabbit_stomp_frame:header(Frame, ?HEADER_DESTINATION) of + {ok, DestHdr} -> + {ok, queue_tag(DestHdr), + "destination='" ++ DestHdr ++ "'"}; + not_found -> + {error, missing_destination_header} + end + end. + +ack_mode(Frame) -> + case rabbit_stomp_frame:header(Frame, ?HEADER_ACK, "auto") of + "auto" -> {auto, false}; + "client" -> {client, true}; + "client-individual" -> {client, false} + end. + +message_properties(Frame = #stomp_frame{headers = Headers}) -> + BinH = fun(K) -> rabbit_stomp_frame:binary_header(Frame, K, undefined) end, + IntH = fun(K) -> rabbit_stomp_frame:integer_header(Frame, K, undefined) end, + + DeliveryMode = case rabbit_stomp_frame:boolean_header( + Frame, ?HEADER_PERSISTENT, false) of + true -> 2; + false -> undefined + end, + + #'P_basic'{ content_type = BinH(?HEADER_CONTENT_TYPE), + content_encoding = BinH(?HEADER_CONTENT_ENCODING), + headers = [longstr_field(K, V) || + {K, V} <- Headers, user_header(K)], + delivery_mode = DeliveryMode, + priority = IntH(?HEADER_PRIORITY), + correlation_id = BinH(?HEADER_CORRELATION_ID), + reply_to = BinH(?HEADER_REPLY_TO), + expiration = BinH(?HEADER_EXPIRATION), + message_id = BinH(?HEADER_AMQP_MESSAGE_ID), + timestamp = IntH(?HEADER_TIMESTAMP), + type = BinH(?HEADER_TYPE), + user_id = BinH(?HEADER_USER_ID), + app_id = BinH(?HEADER_APP_ID) }. + +message_headers(Props = #'P_basic'{headers = Headers}) -> + adhoc_convert_headers( + Headers, + lists:foldl(fun({Header, Index}, Acc) -> + maybe_header(Header, element(Index, Props), Acc) + end, [], + [{?HEADER_CONTENT_TYPE, #'P_basic'.content_type}, + {?HEADER_CONTENT_ENCODING, #'P_basic'.content_encoding}, + {?HEADER_PERSISTENT, #'P_basic'.delivery_mode}, + {?HEADER_PRIORITY, #'P_basic'.priority}, + {?HEADER_CORRELATION_ID, #'P_basic'.correlation_id}, + {?HEADER_REPLY_TO, #'P_basic'.reply_to}, + {?HEADER_EXPIRATION, #'P_basic'.expiration}, + {?HEADER_AMQP_MESSAGE_ID, #'P_basic'.message_id}, + {?HEADER_TIMESTAMP, #'P_basic'.timestamp}, + {?HEADER_TYPE, #'P_basic'.type}, + {?HEADER_USER_ID, #'P_basic'.user_id}, + {?HEADER_APP_ID, #'P_basic'.app_id}])). + +adhoc_convert_headers(undefined, Existing) -> + Existing; +adhoc_convert_headers(Headers, Existing) -> + lists:foldr(fun ({K, longstr, V}, Acc) -> + [{binary_to_list(K), binary_to_list(V)} | Acc]; + ({K, signedint, V}, Acc) -> + [{binary_to_list(K), integer_to_list(V)} | Acc]; + (_, Acc) -> + Acc + end, Existing, Headers). + +headers_extra(SessionId, AckMode, Version, + #'basic.deliver'{consumer_tag = ConsumerTag, + delivery_tag = DeliveryTag, + exchange = ExchangeBin, + routing_key = RoutingKeyBin, + redelivered = Redelivered}) -> + case tag_to_id(ConsumerTag) of + {ok, {internal, Id}} -> [{?HEADER_SUBSCRIPTION, Id}]; + _ -> [] + end ++ + [{?HEADER_DESTINATION, + format_destination(binary_to_list(ExchangeBin), + binary_to_list(RoutingKeyBin))}, + {?HEADER_MESSAGE_ID, + create_message_id(ConsumerTag, SessionId, DeliveryTag)}, + {?HEADER_REDELIVERED, Redelivered}] ++ + case AckMode == client andalso Version == "1.2" of + true -> [{?HEADER_ACK, + create_message_id(ConsumerTag, SessionId, DeliveryTag)}]; + false -> [] + end. + +headers_post_process(Headers) -> + Prefixes = rabbit_routing_util:dest_prefixes(), + [case Header of + {?HEADER_REPLY_TO, V} -> + case lists:any(fun (P) -> lists:prefix(P, V) end, Prefixes) of + true -> {?HEADER_REPLY_TO, V}; + false -> {?HEADER_REPLY_TO, ?REPLY_QUEUE_PREFIX ++ V} + end; + {_, _} -> + Header + end || Header <- Headers]. + +headers(SessionId, Delivery, Properties, AckMode, Version) -> + headers_extra(SessionId, AckMode, Version, Delivery) ++ + headers_post_process(message_headers(Properties)). + +tag_to_id(<>) -> + {ok, {internal, binary_to_list(Id)}}; +tag_to_id(<>) -> + {ok, {queue, binary_to_list(Id)}}; +tag_to_id(Other) when is_binary(Other) -> + {error, {unknown, binary_to_list(Other)}}. + +user_header(Hdr) + when Hdr =:= ?HEADER_CONTENT_TYPE orelse + Hdr =:= ?HEADER_CONTENT_ENCODING orelse + Hdr =:= ?HEADER_PERSISTENT orelse + Hdr =:= ?HEADER_PRIORITY orelse + Hdr =:= ?HEADER_CORRELATION_ID orelse + Hdr =:= ?HEADER_REPLY_TO orelse + Hdr =:= ?HEADER_EXPIRATION orelse + Hdr =:= ?HEADER_AMQP_MESSAGE_ID orelse + Hdr =:= ?HEADER_TIMESTAMP orelse + Hdr =:= ?HEADER_TYPE orelse + Hdr =:= ?HEADER_USER_ID orelse + Hdr =:= ?HEADER_APP_ID orelse + Hdr =:= ?HEADER_DESTINATION -> + false; +user_header(_) -> + true. + +parse_message_id(MessageId) -> + case split(MessageId, ?MESSAGE_ID_SEPARATOR) of + [ConsumerTag, SessionId, DeliveryTag] -> + {ok, {list_to_binary(ConsumerTag), + SessionId, + list_to_integer(DeliveryTag)}}; + _ -> + {error, invalid_message_id} + end. + +negotiate_version(ClientVers, ServerVers) -> + Common = lists:filter(fun(Ver) -> + lists:member(Ver, ServerVers) + end, ClientVers), + case Common of + [] -> + {error, no_common_version}; + [H|T] -> + {ok, lists:foldl(fun(Ver, AccN) -> + max_version(Ver, AccN) + end, H, T)} + end. + +max_version(V, V) -> + V; +max_version(V1, V2) -> + Split = fun(X) -> re:split(X, "\\.", [{return, list}]) end, + find_max_version({V1, Split(V1)}, {V2, Split(V2)}). + +find_max_version({V1, [X|T1]}, {V2, [X|T2]}) -> + find_max_version({V1, T1}, {V2, T2}); +find_max_version({V1, [X]}, {V2, [Y]}) -> + case list_to_integer(X) >= list_to_integer(Y) of + true -> V1; + false -> V2 + end; +find_max_version({_V1, []}, {V2, Y}) when length(Y) > 0 -> + V2; +find_max_version({V1, X}, {_V2, []}) when length(X) > 0 -> + V1. + +%% ---- Header processing helpers ---- + +longstr_field(K, V) -> + {list_to_binary(K), longstr, list_to_binary(V)}. + +maybe_header(_Key, undefined, Acc) -> + Acc; +maybe_header(?HEADER_PERSISTENT, 2, Acc) -> + [{?HEADER_PERSISTENT, "true"} | Acc]; +maybe_header(Key, Value, Acc) when is_binary(Value) -> + [{Key, binary_to_list(Value)} | Acc]; +maybe_header(Key, Value, Acc) when is_integer(Value) -> + [{Key, integer_to_list(Value)}| Acc]; +maybe_header(_Key, _Value, Acc) -> + Acc. + +create_message_id(ConsumerTag, SessionId, DeliveryTag) -> + [ConsumerTag, + ?MESSAGE_ID_SEPARATOR, + SessionId, + ?MESSAGE_ID_SEPARATOR, + integer_to_list(DeliveryTag)]. + +trim_headers(Frame = #stomp_frame{headers = Hdrs}) -> + Frame#stomp_frame{headers = [{K, string:strip(V, left)} || {K, V} <- Hdrs]}. + +internal_tag(Base) -> + list_to_binary(?INTERNAL_TAG_PREFIX ++ Base). + +queue_tag(Base) -> + list_to_binary(?QUEUE_TAG_PREFIX ++ Base). + +ack_header_name("1.2") -> ?HEADER_ID; +ack_header_name("1.1") -> ?HEADER_MESSAGE_ID; +ack_header_name("1.0") -> ?HEADER_MESSAGE_ID. + +msg_header_name("1.2") -> ?HEADER_ACK; +msg_header_name("1.1") -> ?HEADER_MESSAGE_ID; +msg_header_name("1.0") -> ?HEADER_MESSAGE_ID. + +build_arguments(Headers) -> + Arguments = + lists:foldl(fun({K, V}, Acc) -> + case lists:member(K, ?HEADER_ARGUMENTS) of + true -> [build_argument(K, V) | Acc]; + false -> Acc + end + end, + [], + Headers), + {arguments, Arguments}. + +%% build the actual value thru pattern matching +build_argument(?HEADER_X_DEAD_LETTER_EXCHANGE, Val) -> + {list_to_binary(?HEADER_X_DEAD_LETTER_EXCHANGE), longstr, + list_to_binary(string:strip(Val))}; +build_argument(?HEADER_X_DEAD_LETTER_ROUTING_KEY, Val) -> + {list_to_binary(?HEADER_X_DEAD_LETTER_ROUTING_KEY), longstr, + list_to_binary(string:strip(Val))}; +build_argument(?HEADER_X_EXPIRES, Val) -> + {list_to_binary(?HEADER_X_EXPIRES), long, + list_to_integer(string:strip(Val))}; +build_argument(?HEADER_X_MAX_LENGTH, Val) -> + {list_to_binary(?HEADER_X_MAX_LENGTH), long, + list_to_integer(string:strip(Val))}; +build_argument(?HEADER_X_MAX_LENGTH_BYTES, Val) -> + {list_to_binary(?HEADER_X_MAX_LENGTH_BYTES), long, + list_to_integer(string:strip(Val))}; +build_argument(?HEADER_X_MAX_PRIORITY, Val) -> + {list_to_binary(?HEADER_X_MAX_PRIORITY), long, + list_to_integer(string:strip(Val))}; +build_argument(?HEADER_X_MESSAGE_TTL, Val) -> + {list_to_binary(?HEADER_X_MESSAGE_TTL), long, + list_to_integer(string:strip(Val))}. + +%%-------------------------------------------------------------------- +%% Destination Formatting +%%-------------------------------------------------------------------- + +format_destination("", RoutingKey) -> + ?QUEUE_PREFIX ++ "/" ++ escape(RoutingKey); +format_destination("amq.topic", RoutingKey) -> + ?TOPIC_PREFIX ++ "/" ++ escape(RoutingKey); +format_destination(Exchange, "") -> + ?EXCHANGE_PREFIX ++ "/" ++ escape(Exchange); +format_destination(Exchange, RoutingKey) -> + ?EXCHANGE_PREFIX ++ "/" ++ escape(Exchange) ++ "/" ++ escape(RoutingKey). + +%%-------------------------------------------------------------------- +%% Destination Parsing +%%-------------------------------------------------------------------- + +subscription_queue_name(Destination, SubscriptionId) -> + %% We need a queue name that a) can be derived from the + %% Destination and SubscriptionId, and b) meets the constraints on + %% AMQP queue names. It doesn't need to be secure; we use md5 here + %% simply as a convenient means to bound the length. + rabbit_guid:string( + erlang:md5(term_to_binary({Destination, SubscriptionId})), + "stomp-subscription"). + +%% ---- Helpers ---- + +split([], _Splitter) -> []; +split(Content, []) -> Content; +split(Content, Splitter) -> split(Content, [], [], Splitter). + +split([], RPart, RParts, _Splitter) -> + lists:reverse([lists:reverse(RPart) | RParts]); +split(Content = [Elem | Rest1], RPart, RParts, Splitter) -> + case take_prefix(Splitter, Content) of + {ok, Rest2} -> + split(Rest2, [], [lists:reverse(RPart) | RParts], Splitter); + not_found -> + split(Rest1, [Elem | RPart], RParts, Splitter) + end. + +take_prefix([Char | Prefix], [Char | List]) -> take_prefix(Prefix, List); +take_prefix([], List) -> {ok, List}; +take_prefix(_Prefix, _List) -> not_found. + +escape(Str) -> escape(Str, []). + +escape([$/ | Str], Acc) -> escape(Str, "F2%" ++ Acc); %% $/ == '2F'x +escape([$% | Str], Acc) -> escape(Str, "52%" ++ Acc); %% $% == '25'x +escape([X | Str], Acc) when X < 32 orelse X > 127 -> + escape(Str, revhex(X) ++ "%" ++ Acc); +escape([C | Str], Acc) -> escape(Str, [C | Acc]); +escape([], Acc) -> lists:reverse(Acc). + +revhex(I) -> hexdig(I) ++ hexdig(I bsr 4). + +hexdig(I) -> erlang:integer_to_list(I band 15, 16). diff --git a/rabbitmq-server/plugins-src/rabbitmq-stomp/src/rabbitmq_stomp.app.src b/rabbitmq-server/plugins-src/rabbitmq-stomp/src/rabbitmq_stomp.app.src new file mode 100644 index 0000000..6757e25 --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-stomp/src/rabbitmq_stomp.app.src @@ -0,0 +1,22 @@ +{application, rabbitmq_stomp, + [{description, "Embedded Rabbit Stomp Adapter"}, + {vsn, "%%VSN%%"}, + {modules, []}, + {registered, []}, + {mod, {rabbit_stomp, []}}, + {env, [{default_user, + [{login, "guest"}, + {passcode, "guest"}]}, + {default_vhost, <<"/">>}, + {ssl_cert_login, false}, + {implicit_connect, false}, + {tcp_listeners, [61613]}, + {ssl_listeners, []}, + {tcp_listen_options, [binary, + {packet, raw}, + {reuseaddr, true}, + {backlog, 128}, + {nodelay, true}]}, + %% see rabbitmq/rabbitmq-stomp#39 + {trailing_lf, true}]}, + {applications, [kernel, stdlib, rabbit, amqp_client]}]}. diff --git a/rabbitmq-server/plugins-src/rabbitmq-stomp/test/src/ack.py b/rabbitmq-server/plugins-src/rabbitmq-stomp/test/src/ack.py new file mode 100644 index 0000000..e87bca3 --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-stomp/test/src/ack.py @@ -0,0 +1,242 @@ +import unittest +import stomp +import base +import time + +class TestAck(base.BaseTest): + + def test_ack_client(self): + destination = "/queue/ack-test" + + # subscribe and send message + self.listener.reset(2) ## expecting 2 messages + self.subscribe_dest(self.conn, destination, None, + ack='client', + headers={'prefetch-count': '10'}) + self.conn.send(destination, "test1") + self.conn.send(destination, "test2") + self.assertTrue(self.listener.await(4), "initial message not received") + self.assertEquals(2, len(self.listener.messages)) + + # disconnect with no ack + self.conn.disconnect() + + # now reconnect + conn2 = self.create_connection() + try: + listener2 = base.WaitableListener() + listener2.reset(2) + conn2.set_listener('', listener2) + self.subscribe_dest(conn2, destination, None, + ack='client', + headers={'prefetch-count': '10'}) + self.assertTrue(listener2.await(), "message not received again") + self.assertEquals(2, len(listener2.messages)) + + # now ack only the last message - expecting cumulative behaviour + mid = listener2.messages[1]['headers'][self.ack_id_source_header] + self.ack_message(conn2, mid, None) + finally: + conn2.disconnect() + + # now reconnect again, shouldn't see the message + conn3 = self.create_connection() + try: + listener3 = base.WaitableListener() + conn3.set_listener('', listener3) + self.subscribe_dest(conn3, destination, None) + self.assertFalse(listener3.await(3), + "unexpected message. ACK not working?") + finally: + conn3.disconnect() + + def test_ack_client_individual(self): + destination = "/queue/ack-test-individual" + + # subscribe and send message + self.listener.reset(2) ## expecting 2 messages + self.subscribe_dest(self.conn, destination, None, + ack='client-individual', + headers={'prefetch-count': '10'}) + self.conn.send(destination, "test1") + self.conn.send(destination, "test2") + self.assertTrue(self.listener.await(4), "Both initial messages not received") + self.assertEquals(2, len(self.listener.messages)) + + # disconnect without acks + self.conn.disconnect() + + # now reconnect + conn2 = self.create_connection() + try: + listener2 = base.WaitableListener() + listener2.reset(2) ## expect 2 messages + conn2.set_listener('', listener2) + self.subscribe_dest(conn2, destination, None, + ack='client-individual', + headers={'prefetch-count': '10'}) + self.assertTrue(listener2.await(2.5), "Did not receive 2 messages") + self.assertEquals(2, len(listener2.messages), "Not exactly 2 messages received") + + # now ack only the 'test2' message - expecting individual behaviour + nummsgs = len(listener2.messages) + mid = None + for ind in range(nummsgs): + if listener2.messages[ind]['message']=="test2": + mid = listener2.messages[ind]['headers'][self.ack_id_source_header] + self.assertEquals(1, ind, 'Expecting test2 to be second message') + break + self.assertTrue(mid, "Did not find test2 message id.") + self.ack_message(conn2, mid, None) + finally: + conn2.disconnect() + + # now reconnect again, shouldn't see the message + conn3 = self.create_connection() + try: + listener3 = base.WaitableListener() + listener3.reset(2) ## expecting a single message, but wait for two + conn3.set_listener('', listener3) + self.subscribe_dest(conn3, destination, None) + self.assertFalse(listener3.await(2.5), + "Expected to see only one message. ACK not working?") + self.assertEquals(1, len(listener3.messages), "Expecting exactly one message") + self.assertEquals("test1", listener3.messages[0]['message'], "Unexpected message remains") + finally: + conn3.disconnect() + + def test_ack_client_tx(self): + destination = "/queue/ack-test-tx" + + # subscribe and send message + self.listener.reset() + self.subscribe_dest(self.conn, destination, None, ack='client') + self.conn.send(destination, "test") + self.assertTrue(self.listener.await(3), "initial message not received") + self.assertEquals(1, len(self.listener.messages)) + + # disconnect with no ack + self.conn.disconnect() + + # now reconnect + conn2 = self.create_connection() + try: + tx = "abc" + listener2 = base.WaitableListener() + conn2.set_listener('', listener2) + conn2.begin(transaction=tx) + self.subscribe_dest(conn2, destination, None, ack='client') + self.assertTrue(listener2.await(), "message not received again") + self.assertEquals(1, len(listener2.messages)) + + # now ack + mid = listener2.messages[0]['headers'][self.ack_id_source_header] + self.ack_message(conn2, mid, None, transaction=tx) + + #now commit + conn2.commit(transaction=tx) + finally: + conn2.disconnect() + + # now reconnect again, shouldn't see the message + conn3 = self.create_connection() + try: + listener3 = base.WaitableListener() + conn3.set_listener('', listener3) + self.subscribe_dest(conn3, destination, None) + self.assertFalse(listener3.await(3), + "unexpected message. TX ACK not working?") + finally: + conn3.disconnect() + + def test_topic_prefetch(self): + destination = "/topic/prefetch-test" + + # subscribe and send message + self.listener.reset(6) ## expect 6 messages + self.subscribe_dest(self.conn, destination, None, + ack='client', + headers={'prefetch-count': '5'}) + + for x in range(10): + self.conn.send(destination, "test" + str(x)) + + self.assertFalse(self.listener.await(3), + "Should not have been able to see 6 messages") + self.assertEquals(5, len(self.listener.messages)) + + def test_nack(self): + destination = "/queue/nack-test" + + #subscribe and send + self.subscribe_dest(self.conn, destination, None, + ack='client-individual') + self.conn.send(destination, "nack-test") + + self.assertTrue(self.listener.await(), "Not received message") + message_id = self.listener.messages[0]['headers'][self.ack_id_source_header] + self.listener.reset() + + self.nack_message(self.conn, message_id, None) + self.assertTrue(self.listener.await(), "Not received message after NACK") + message_id = self.listener.messages[0]['headers'][self.ack_id_source_header] + self.ack_message(self.conn, message_id, None) + + def test_nack_multi(self): + destination = "/queue/nack-multi" + + self.listener.reset(2) + + #subscribe and send + self.subscribe_dest(self.conn, destination, None, + ack='client', + headers = {'prefetch-count' : '10'}) + self.conn.send(destination, "nack-test1") + self.conn.send(destination, "nack-test2") + + self.assertTrue(self.listener.await(), "Not received messages") + message_id = self.listener.messages[1]['headers'][self.ack_id_source_header] + self.listener.reset(2) + + self.nack_message(self.conn, message_id, None) + self.assertTrue(self.listener.await(), "Not received message again") + message_id = self.listener.messages[1]['headers'][self.ack_id_source_header] + self.ack_message(self.conn, message_id, None) + + def test_nack_without_requeueing(self): + destination = "/queue/nack-test-no-requeue" + + self.subscribe_dest(self.conn, destination, None, + ack='client-individual') + self.conn.send(destination, "nack-test") + + self.assertTrue(self.listener.await(), "Not received message") + message_id = self.listener.messages[0]['headers'][self.ack_id_source_header] + self.listener.reset() + + self.conn.send_frame("NACK", {self.ack_id_header: message_id, "requeue": False}) + self.assertFalse(self.listener.await(4), "Received message after NACK with requeue = False") + +class TestAck11(TestAck): + + def create_connection_obj(self, version='1.1', vhost='/', heartbeats=(0, 0)): + conn = stomp.StompConnection11(vhost=vhost, + heartbeats=heartbeats) + self.ack_id_source_header = 'message-id' + self.ack_id_header = 'message-id' + return conn + + def test_version(self): + self.assertEquals('1.1', self.conn.version) + +class TestAck12(TestAck): + + def create_connection_obj(self, version='1.2', vhost='/', heartbeats=(0, 0)): + conn = stomp.StompConnection12(vhost=vhost, + heartbeats=heartbeats) + self.ack_id_source_header = 'ack' + self.ack_id_header = 'id' + return conn + + def test_version(self): + self.assertEquals('1.2', self.conn.version) diff --git a/rabbitmq-server/plugins-src/rabbitmq-stomp/test/src/base.py b/rabbitmq-server/plugins-src/rabbitmq-stomp/test/src/base.py new file mode 100644 index 0000000..e3d5819 --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-stomp/test/src/base.py @@ -0,0 +1,242 @@ +import unittest +import stomp +import sys +import threading + + +class BaseTest(unittest.TestCase): + + def create_connection_obj(self, version='1.0', vhost='/', heartbeats=(0, 0)): + if version == '1.0': + conn = stomp.StompConnection10() + self.ack_id_source_header = 'message-id' + self.ack_id_header = 'message-id' + elif version == '1.1': + conn = stomp.StompConnection11(vhost=vhost, + heartbeats=heartbeats) + self.ack_id_source_header = 'message-id' + self.ack_id_header = 'message-id' + elif version == '1.2': + conn = stomp.StompConnection12(vhost=vhost, + heartbeats=heartbeats) + self.ack_id_source_header = 'ack' + self.ack_id_header = 'id' + else: + conn = stomp.StompConnection12(vhost=vhost, + heartbeats=heartbeats) + conn.version = version + return conn + + def create_connection(self, user='guest', passcode='guest', wait=True, **kwargs): + conn = self.create_connection_obj(**kwargs) + conn.start() + conn.connect(user, passcode, wait=wait) + return conn + + def subscribe_dest(self, conn, destination, sub_id, **kwargs): + if type(conn) is stomp.StompConnection10: + # 'id' is optional in STOMP 1.0. + if sub_id != None: + kwargs['id'] = sub_id + conn.subscribe(destination, **kwargs) + else: + # 'id' is required in STOMP 1.1+. + if sub_id == None: + sub_id = 'ctag' + conn.subscribe(destination, sub_id, **kwargs) + + def unsubscribe_dest(self, conn, destination, sub_id, **kwargs): + if type(conn) is stomp.StompConnection10: + # 'id' is optional in STOMP 1.0. + if sub_id != None: + conn.unsubscribe(id=sub_id, **kwargs) + else: + conn.unsubscribe(destination=destination, **kwargs) + else: + # 'id' is required in STOMP 1.1+. + if sub_id == None: + sub_id = 'ctag' + conn.unsubscribe(sub_id, **kwargs) + + def ack_message(self, conn, msg_id, sub_id, **kwargs): + if type(conn) is stomp.StompConnection10: + conn.ack(msg_id, **kwargs) + elif type(conn) is stomp.StompConnection11: + if sub_id == None: + sub_id = 'ctag' + conn.ack(msg_id, sub_id, **kwargs) + elif type(conn) is stomp.StompConnection12: + conn.ack(msg_id, **kwargs) + + def nack_message(self, conn, msg_id, sub_id, **kwargs): + if type(conn) is stomp.StompConnection10: + # Normally unsupported by STOMP 1.0. + conn.send_frame("NACK", {"message-id": msg_id}) + elif type(conn) is stomp.StompConnection11: + if sub_id == None: + sub_id = 'ctag' + conn.nack(msg_id, sub_id, **kwargs) + elif type(conn) is stomp.StompConnection12: + conn.nack(msg_id, **kwargs) + + def create_subscriber_connection(self, dest): + conn = self.create_connection() + listener = WaitableListener() + conn.set_listener('', listener) + self.subscribe_dest(conn, dest, None, receipt="sub.receipt") + listener.await() + self.assertEquals(1, len(listener.receipts)) + listener.reset() + return conn, listener + + def setUp(self): + self.conn = self.create_connection() + self.listener = WaitableListener() + self.conn.set_listener('', self.listener) + + def tearDown(self): + if self.conn.is_connected(): + self.conn.disconnect() + self.conn.stop() + + def simple_test_send_rec(self, dest, route = None): + self.listener.reset() + + self.subscribe_dest(self.conn, dest, None) + self.conn.send(dest, "foo") + + self.assertTrue(self.listener.await(), "Timeout, no message received") + + # assert no errors + if len(self.listener.errors) > 0: + self.fail(self.listener.errors[0]['message']) + + # check header content + msg = self.listener.messages[0] + self.assertEquals("foo", msg['message']) + self.assertEquals(dest, msg['headers']['destination']) + + def assertListener(self, errMsg, numMsgs=0, numErrs=0, numRcts=0, timeout=10): + if numMsgs + numErrs + numRcts > 0: + self._assertTrue(self.listener.await(timeout), errMsg + " (#awaiting)") + else: + self._assertFalse(self.listener.await(timeout), errMsg + " (#awaiting)") + self._assertEquals(numMsgs, len(self.listener.messages), errMsg + " (#messages)") + self._assertEquals(numErrs, len(self.listener.errors), errMsg + " (#errors)") + self._assertEquals(numRcts, len(self.listener.receipts), errMsg + " (#receipts)") + + def _assertTrue(self, bool, msg): + if not bool: + self.listener.print_state(msg, True) + self.assertTrue(bool, msg) + + def _assertFalse(self, bool, msg): + if bool: + self.listener.print_state(msg, True) + self.assertFalse(bool, msg) + + def _assertEquals(self, expected, actual, msg): + if expected != actual: + self.listener.print_state(msg, True) + self.assertEquals(expected, actual, msg) + + def assertListenerAfter(self, verb, errMsg="", numMsgs=0, numErrs=0, numRcts=0, timeout=5): + num = numMsgs + numErrs + numRcts + self.listener.reset(num if num>0 else 1) + verb() + self.assertListener(errMsg=errMsg, numMsgs=numMsgs, numErrs=numErrs, numRcts=numRcts, timeout=timeout) + +class WaitableListener(object): + + def __init__(self): + self.debug = False + if self.debug: + print '(listener) init' + self.messages = [] + self.errors = [] + self.receipts = [] + self.latch = Latch(1) + self.msg_no = 0 + + def _next_msg_no(self): + self.msg_no += 1 + return self.msg_no + + def _append(self, array, msg, hdrs): + mno = self._next_msg_no() + array.append({'message' : msg, 'headers' : hdrs, 'msg_no' : mno}) + self.latch.countdown() + + def on_receipt(self, headers, message): + if self.debug: + print '(on_receipt) message:', message, 'headers:', headers + self._append(self.receipts, message, headers) + + def on_error(self, headers, message): + if self.debug: + print '(on_error) message:', message, 'headers:', headers + self._append(self.errors, message, headers) + + def on_message(self, headers, message): + if self.debug: + print '(on_message) message:', message, 'headers:', headers + self._append(self.messages, message, headers) + + def reset(self, count=1): + if self.debug: + self.print_state('(reset listener--old state)') + self.messages = [] + self.errors = [] + self.receipts = [] + self.latch = Latch(count) + self.msg_no = 0 + if self.debug: + self.print_state('(reset listener--new state)') + + def await(self, timeout=10): + return self.latch.await(timeout) + + def print_state(self, hdr="", full=False): + print hdr, + print '#messages:', len(self.messages), + print '#errors:', len(self.errors), + print '#receipts:', len(self.receipts), + print 'Remaining count:', self.latch.get_count() + if full: + if len(self.messages) != 0: print 'Messages:', self.messages + if len(self.errors) != 0: print 'Messages:', self.errors + if len(self.receipts) != 0: print 'Messages:', self.receipts + +class Latch(object): + + def __init__(self, count=1): + self.cond = threading.Condition() + self.cond.acquire() + self.count = count + self.cond.release() + + def countdown(self): + self.cond.acquire() + if self.count > 0: + self.count -= 1 + if self.count == 0: + self.cond.notify_all() + self.cond.release() + + def await(self, timeout=None): + try: + self.cond.acquire() + if self.count == 0: + return True + else: + self.cond.wait(timeout) + return self.count == 0 + finally: + self.cond.release() + + def get_count(self): + try: + self.cond.acquire() + return self.count + finally: + self.cond.release() diff --git a/rabbitmq-server/plugins-src/rabbitmq-stomp/test/src/connect_options.py b/rabbitmq-server/plugins-src/rabbitmq-stomp/test/src/connect_options.py new file mode 100644 index 0000000..d802bc6 --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-stomp/test/src/connect_options.py @@ -0,0 +1,43 @@ +import unittest +import stomp +import base +import test_util + +class TestConnectOptions(base.BaseTest): + + def test_implicit_connect(self): + ''' Implicit connect with receipt on first command ''' + self.conn.disconnect() + test_util.enable_implicit_connect() + listener = base.WaitableListener() + new_conn = stomp.Connection() + new_conn.set_listener('', listener) + + new_conn.start() # not going to issue connect + self.subscribe_dest(new_conn, "/topic/implicit", 'sub_implicit', + receipt='implicit') + + try: + self.assertTrue(listener.await(5)) + self.assertEquals(1, len(listener.receipts), + 'Missing receipt. Likely not connected') + self.assertEquals('implicit', listener.receipts[0]['headers']['receipt-id']) + finally: + new_conn.disconnect() + test_util.disable_implicit_connect() + + def test_default_user(self): + ''' Default user connection ''' + self.conn.disconnect() + test_util.enable_default_user() + listener = base.WaitableListener() + new_conn = stomp.Connection() + new_conn.set_listener('', listener) + new_conn.start() + new_conn.connect() + try: + self.assertFalse(listener.await(3)) # no error back + self.assertTrue(new_conn.is_connected()) + finally: + new_conn.disconnect() + test_util.disable_default_user() diff --git a/rabbitmq-server/plugins-src/rabbitmq-stomp/test/src/destinations.py b/rabbitmq-server/plugins-src/rabbitmq-stomp/test/src/destinations.py new file mode 100644 index 0000000..760bb9f --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-stomp/test/src/destinations.py @@ -0,0 +1,516 @@ +import unittest +import stomp +import base +import time + +class TestExchange(base.BaseTest): + + + def test_amq_direct(self): + ''' Test basic send/receive for /exchange/amq.direct ''' + self.__test_exchange_send_rec("amq.direct", "route") + + def test_amq_topic(self): + ''' Test basic send/receive for /exchange/amq.topic ''' + self.__test_exchange_send_rec("amq.topic", "route") + + def test_amq_fanout(self): + ''' Test basic send/receive for /exchange/amq.fanout ''' + self.__test_exchange_send_rec("amq.fanout", "route") + + def test_amq_fanout_no_route(self): + ''' Test basic send/receive, /exchange/amq.direct, no routing key''' + self.__test_exchange_send_rec("amq.fanout") + + def test_invalid_exchange(self): + ''' Test invalid exchange error ''' + self.listener.reset(1) + self.subscribe_dest(self.conn, "/exchange/does.not.exist", None, + ack="auto") + self.assertListener("Expecting an error", numErrs=1) + err = self.listener.errors[0] + self.assertEquals("not_found", err['headers']['message']) + self.assertEquals( + "NOT_FOUND - no exchange 'does.not.exist' in vhost '/'\n", + err['message']) + time.sleep(1) + self.assertFalse(self.conn.is_connected()) + + def __test_exchange_send_rec(self, exchange, route = None): + if exchange != "amq.topic": + dest = "/exchange/" + exchange + else: + dest = "/topic" + if route != None: + dest += "/" + route + + self.simple_test_send_rec(dest) + +class TestQueue(base.BaseTest): + + def test_send_receive(self): + ''' Test basic send/receive for /queue ''' + destination = '/queue/test' + self.simple_test_send_rec(destination) + + def test_send_receive_in_other_conn(self): + ''' Test send in one connection, receive in another ''' + destination = '/queue/test2' + + # send + self.conn.send(destination, "hello") + + # now receive + conn2 = self.create_connection() + try: + listener2 = base.WaitableListener() + conn2.set_listener('', listener2) + + self.subscribe_dest(conn2, destination, None, ack="auto") + self.assertTrue(listener2.await(10), "no receive") + finally: + conn2.disconnect() + + def test_send_receive_in_other_conn_with_disconnect(self): + ''' Test send, disconnect, receive ''' + destination = '/queue/test3' + + # send + self.conn.send(destination, "hello thar", receipt="foo") + self.listener.await(3) + self.conn.disconnect() + + # now receive + conn2 = self.create_connection() + try: + listener2 = base.WaitableListener() + conn2.set_listener('', listener2) + + self.subscribe_dest(conn2, destination, None, ack="auto") + self.assertTrue(listener2.await(10), "no receive") + finally: + conn2.disconnect() + + + def test_multi_subscribers(self): + ''' Test multiple subscribers against a single /queue destination ''' + destination = '/queue/test-multi' + + ## set up two subscribers + conn1, listener1 = self.create_subscriber_connection(destination) + conn2, listener2 = self.create_subscriber_connection(destination) + + try: + ## now send + self.conn.send(destination, "test1") + self.conn.send(destination, "test2") + + ## expect both consumers to get a message? + self.assertTrue(listener1.await(2)) + self.assertEquals(1, len(listener1.messages), + "unexpected message count") + self.assertTrue(listener2.await(2)) + self.assertEquals(1, len(listener2.messages), + "unexpected message count") + finally: + conn1.disconnect() + conn2.disconnect() + + def test_send_with_receipt(self): + destination = '/queue/test-receipt' + def noop(): pass + self.__test_send_receipt(destination, noop, noop) + + def test_send_with_receipt_tx(self): + destination = '/queue/test-receipt-tx' + tx = 'receipt.tx' + + def before(): + self.conn.begin(transaction=tx) + + def after(): + self.assertFalse(self.listener.await(1)) + self.conn.commit(transaction=tx) + + self.__test_send_receipt(destination, before, after, {'transaction': tx}) + + def test_interleaved_receipt_no_receipt(self): + ''' Test i-leaved receipt/no receipt, no-r bracketed by rs ''' + + destination = '/queue/ir' + + self.listener.reset(5) + + self.subscribe_dest(self.conn, destination, None, ack="auto") + self.conn.send(destination, 'first', receipt='a') + self.conn.send(destination, 'second') + self.conn.send(destination, 'third', receipt='b') + + self.assertListener("Missing messages/receipts", numMsgs=3, numRcts=2, timeout=3) + + self.assertEquals(set(['a','b']), self.__gather_receipts()) + + def test_interleaved_receipt_no_receipt_tx(self): + ''' Test i-leaved receipt/no receipt, no-r bracketed by r+xactions ''' + + destination = '/queue/ir' + tx = 'tx.ir' + + # three messages and two receipts + self.listener.reset(5) + + self.subscribe_dest(self.conn, destination, None, ack="auto") + self.conn.begin(transaction=tx) + + self.conn.send(destination, 'first', receipt='a', transaction=tx) + self.conn.send(destination, 'second', transaction=tx) + self.conn.send(destination, 'third', receipt='b', transaction=tx) + self.conn.commit(transaction=tx) + + self.assertListener("Missing messages/receipts", numMsgs=3, numRcts=2, timeout=40) + + expected = set(['a', 'b']) + missing = expected.difference(self.__gather_receipts()) + + self.assertEquals(set(), missing, "Missing receipts: " + str(missing)) + + def test_interleaved_receipt_no_receipt_inverse(self): + ''' Test i-leaved receipt/no receipt, r bracketed by no-rs ''' + + destination = '/queue/ir' + + self.listener.reset(4) + + self.subscribe_dest(self.conn, destination, None, ack="auto") + self.conn.send(destination, 'first') + self.conn.send(destination, 'second', receipt='a') + self.conn.send(destination, 'third') + + self.assertListener("Missing messages/receipt", numMsgs=3, numRcts=1, timeout=3) + + self.assertEquals(set(['a']), self.__gather_receipts()) + + def __test_send_receipt(self, destination, before, after, headers = {}): + count = 50 + self.listener.reset(count) + + before() + expected_receipts = set() + + for x in range(0, count): + receipt = "test" + str(x) + expected_receipts.add(receipt) + self.conn.send(destination, "test receipt", + receipt=receipt, headers=headers) + after() + + self.assertTrue(self.listener.await(5)) + + missing_receipts = expected_receipts.difference( + self.__gather_receipts()) + + self.assertEquals(set(), missing_receipts, + "missing receipts: " + str(missing_receipts)) + + def __gather_receipts(self): + result = set() + for r in self.listener.receipts: + result.add(r['headers']['receipt-id']) + return result + +class TestTopic(base.BaseTest): + + def test_send_receive(self): + ''' Test basic send/receive for /topic ''' + destination = '/topic/test' + self.simple_test_send_rec(destination) + + def test_send_multiple(self): + ''' Test /topic with multiple consumers ''' + destination = '/topic/multiple' + + ## set up two subscribers + conn1, listener1 = self.create_subscriber_connection(destination) + conn2, listener2 = self.create_subscriber_connection(destination) + + try: + ## listeners are expecting 2 messages + listener1.reset(2) + listener2.reset(2) + + ## now send + self.conn.send(destination, "test1") + self.conn.send(destination, "test2") + + ## expect both consumers to get both messages + self.assertTrue(listener1.await(5)) + self.assertEquals(2, len(listener1.messages), + "unexpected message count") + self.assertTrue(listener2.await(5)) + self.assertEquals(2, len(listener2.messages), + "unexpected message count") + finally: + conn1.disconnect() + conn2.disconnect() + + def test_send_multiple_with_a_large_message(self): + ''' Test /topic with multiple consumers ''' + destination = '/topic/16mb' + # payload size + s = 1024 * 1024 * 16 + message = 'x' * s + + conn1, listener1 = self.create_subscriber_connection(destination) + conn2, listener2 = self.create_subscriber_connection(destination) + + try: + listener1.reset(2) + listener2.reset(2) + + self.conn.send(destination, message) + self.conn.send(destination, message) + + self.assertTrue(listener1.await(10)) + self.assertEquals(2, len(listener1.messages), + "unexpected message count") + self.assertTrue(len(listener2.messages[0]['message']) == s, + "unexpected message size") + + self.assertTrue(listener2.await(10)) + self.assertEquals(2, len(listener2.messages), + "unexpected message count") + finally: + conn1.disconnect() + conn2.disconnect() + +class TestReplyQueue(base.BaseTest): + + def test_reply_queue(self): + ''' Test with two separate clients. Client 1 sends + message to a known destination with a defined reply + queue. Client 2 receives on known destination and replies + on the reply destination. Client 1 gets the reply message''' + + known = '/queue/known' + reply = '/temp-queue/0' + + ## Client 1 uses pre-supplied connection and listener + ## Set up client 2 + conn2, listener2 = self.create_subscriber_connection(known) + + try: + self.conn.send(known, "test", + headers = {"reply-to": reply}) + + self.assertTrue(listener2.await(5)) + self.assertEquals(1, len(listener2.messages)) + + reply_to = listener2.messages[0]['headers']['reply-to'] + self.assertTrue(reply_to.startswith('/reply-queue/')) + + conn2.send(reply_to, "reply") + self.assertTrue(self.listener.await(5)) + self.assertEquals("reply", self.listener.messages[0]['message']) + finally: + conn2.disconnect() + + def test_reuse_reply_queue(self): + ''' Test re-use of reply-to queue ''' + + known2 = '/queue/known2' + known3 = '/queue/known3' + reply = '/temp-queue/foo' + + def respond(cntn, listna): + self.assertTrue(listna.await(5)) + self.assertEquals(1, len(listna.messages)) + reply_to = listna.messages[0]['headers']['reply-to'] + self.assertTrue(reply_to.startswith('/reply-queue/')) + cntn.send(reply_to, "reply") + + ## Client 1 uses pre-supplied connection and listener + ## Set up clients 2 and 3 + conn2, listener2 = self.create_subscriber_connection(known2) + conn3, listener3 = self.create_subscriber_connection(known3) + try: + self.listener.reset(2) + self.conn.send(known2, "test2", + headers = {"reply-to": reply}) + self.conn.send(known3, "test3", + headers = {"reply-to": reply}) + respond(conn2, listener2) + respond(conn3, listener3) + + self.assertTrue(self.listener.await(5)) + self.assertEquals(2, len(self.listener.messages)) + self.assertEquals("reply", self.listener.messages[0]['message']) + self.assertEquals("reply", self.listener.messages[1]['message']) + finally: + conn2.disconnect() + conn3.disconnect() + + def test_perm_reply_queue(self): + '''As test_reply_queue, but with a non-temp reply queue''' + + known = '/queue/known' + reply = '/queue/reply' + + ## Client 1 uses pre-supplied connection and listener + ## Set up client 2 + conn1, listener1 = self.create_subscriber_connection(reply) + conn2, listener2 = self.create_subscriber_connection(known) + + try: + conn1.send(known, "test", + headers = {"reply-to": reply}) + + self.assertTrue(listener2.await(5)) + self.assertEquals(1, len(listener2.messages)) + + reply_to = listener2.messages[0]['headers']['reply-to'] + self.assertTrue(reply_to == reply) + + conn2.send(reply_to, "reply") + self.assertTrue(listener1.await(5)) + self.assertEquals("reply", listener1.messages[0]['message']) + finally: + conn1.disconnect() + conn2.disconnect() + +class TestDurableSubscription(base.BaseTest): + + ID = 'test.subscription' + + def __subscribe(self, dest, conn=None, id=None): + if not conn: + conn = self.conn + if not id: + id = TestDurableSubscription.ID + + self.subscribe_dest(conn, dest, id, ack="auto", + headers = {'persistent': 'true', + 'receipt': 1}) + + def __assert_receipt(self, listener=None, pos=None): + if not listener: + listener = self.listener + + self.assertTrue(listener.await(5)) + self.assertEquals(1, len(self.listener.receipts)) + if pos is not None: + self.assertEquals(pos, self.listener.receipts[0]['msg_no']) + + def __assert_message(self, msg, listener=None, pos=None): + if not listener: + listener = self.listener + + self.assertTrue(listener.await(5)) + self.assertEquals(1, len(listener.messages)) + self.assertEquals(msg, listener.messages[0]['message']) + if pos is not None: + self.assertEquals(pos, self.listener.messages[0]['msg_no']) + + def test_durable_subscription(self): + destination = '/topic/durable' + + self.__subscribe(destination) + self.__assert_receipt() + + # send first message without unsubscribing + self.listener.reset(1) + self.conn.send(destination, "first") + self.__assert_message("first") + + # now unsubscribe (disconnect only) + self.unsubscribe_dest(self.conn, destination, TestDurableSubscription.ID) + + # send again + self.listener.reset(2) + self.conn.send(destination, "second") + + # resubscribe and expect receipt + self.__subscribe(destination) + self.__assert_receipt(pos=1) + # and message + self.__assert_message("second", pos=2) + + # now unsubscribe (cancel) + self.unsubscribe_dest(self.conn, destination, TestDurableSubscription.ID, + headers={'persistent': 'true'}) + + # send again + self.listener.reset(1) + self.conn.send(destination, "third") + + # resubscribe and expect no message + self.__subscribe(destination) + self.assertTrue(self.listener.await(3)) + self.assertEquals(0, len(self.listener.messages)) + self.assertEquals(1, len(self.listener.receipts)) + + def test_share_subscription(self): + destination = '/topic/durable-shared' + + conn2 = self.create_connection() + conn2.set_listener('', self.listener) + + try: + self.__subscribe(destination) + self.__assert_receipt() + self.listener.reset(1) + self.__subscribe(destination, conn2) + self.__assert_receipt() + + self.listener.reset(100) + + # send 100 messages + for x in xrange(0, 100): + self.conn.send(destination, "msg" + str(x)) + + self.assertTrue(self.listener.await(5)) + self.assertEquals(100, len(self.listener.messages)) + finally: + conn2.disconnect() + + def test_separate_ids(self): + destination = '/topic/durable-separate' + + conn2 = self.create_connection() + listener2 = base.WaitableListener() + conn2.set_listener('', listener2) + + try: + # ensure durable subscription exists for each ID + self.__subscribe(destination) + self.__assert_receipt() + self.__subscribe(destination, conn2, "other.id") + self.__assert_receipt(listener2) + self.unsubscribe_dest(self.conn, destination, TestDurableSubscription.ID) + self.unsubscribe_dest(conn2, destination, "other.id") + + self.listener.reset(101) + listener2.reset(101) ## 100 messages and 1 receipt + + # send 100 messages + for x in xrange(0, 100): + self.conn.send(destination, "msg" + str(x)) + + self.__subscribe(destination) + self.__subscribe(destination, conn2, "other.id") + + for l in [self.listener, listener2]: + self.assertTrue(l.await(15)) + self.assertTrue(len(l.messages) >= 90) + self.assertTrue(len(l.messages) <= 100) + + finally: + conn2.disconnect() + + def test_durable_subscribe_no_id(self): + destination = '/topic/durable-invalid' + + self.conn.send_frame('SUBSCRIBE', + {'destination': destination, 'ack': 'auto', 'persistent': 'true'}) + self.listener.await(3) + self.assertEquals(1, len(self.listener.errors)) + self.assertEquals("Missing Header", self.listener.errors[0]['headers']['message']) diff --git a/rabbitmq-server/plugins-src/rabbitmq-stomp/test/src/errors.py b/rabbitmq-server/plugins-src/rabbitmq-stomp/test/src/errors.py new file mode 100644 index 0000000..d3fa60a --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-stomp/test/src/errors.py @@ -0,0 +1,94 @@ +import unittest +import stomp +import base +import time + +class TestErrorsAndCloseConnection(base.BaseTest): + def __test_duplicate_consumer_tag_with_headers(self, destination, headers): + self.subscribe_dest(self.conn, destination, None, + headers = headers) + + self.subscribe_dest(self.conn, destination, None, + headers = headers) + + self.assertTrue(self.listener.await()) + + self.assertEquals(1, len(self.listener.errors)) + errorReceived = self.listener.errors[0] + self.assertEquals("Duplicated subscription identifier", errorReceived['headers']['message']) + self.assertEquals("A subscription identified by 'T_1' alredy exists.", errorReceived['message']) + time.sleep(2) + self.assertFalse(self.conn.is_connected()) + + + def test_duplicate_consumer_tag_with_transient_destination(self): + destination = "/exchange/amq.direct/duplicate-consumer-tag-test1" + self.__test_duplicate_consumer_tag_with_headers(destination, {'id': 1}) + + def test_duplicate_consumer_tag_with_durable_destination(self): + destination = "/queue/duplicate-consumer-tag-test2" + self.__test_duplicate_consumer_tag_with_headers(destination, {'id': 1, + 'persistent': True}) + + +class TestErrors(base.BaseTest): + + def test_invalid_queue_destination(self): + self.__test_invalid_destination("queue", "/bah/baz") + + def test_invalid_empty_queue_destination(self): + self.__test_invalid_destination("queue", "") + + def test_invalid_topic_destination(self): + self.__test_invalid_destination("topic", "/bah/baz") + + def test_invalid_empty_topic_destination(self): + self.__test_invalid_destination("topic", "") + + def test_invalid_exchange_destination(self): + self.__test_invalid_destination("exchange", "/bah/baz/boo") + + def test_invalid_empty_exchange_destination(self): + self.__test_invalid_destination("exchange", "") + + def test_invalid_default_exchange_destination(self): + self.__test_invalid_destination("exchange", "//foo") + + def test_unknown_destination(self): + self.listener.reset() + self.conn.send("/something/interesting", 'test_unknown_destination') + + self.assertTrue(self.listener.await()) + self.assertEquals(1, len(self.listener.errors)) + + err = self.listener.errors[0] + self.assertEquals("Unknown destination", err['headers']['message']) + + def test_send_missing_destination(self): + self.__test_missing_destination("SEND") + + def test_send_missing_destination(self): + self.__test_missing_destination("SUBSCRIBE") + + def __test_missing_destination(self, command): + self.listener.reset() + self.conn.send_frame(command) + + self.assertTrue(self.listener.await()) + self.assertEquals(1, len(self.listener.errors)) + + err = self.listener.errors[0] + self.assertEquals("Missing destination", err['headers']['message']) + + def __test_invalid_destination(self, dtype, content): + self.listener.reset() + self.conn.send("/" + dtype + content, '__test_invalid_destination:' + dtype + content) + + self.assertTrue(self.listener.await()) + self.assertEquals(1, len(self.listener.errors)) + + err = self.listener.errors[0] + self.assertEquals("Invalid destination", err['headers']['message']) + self.assertEquals("'" + content + "' is not a valid " + + dtype + " destination\n", + err['message']) diff --git a/rabbitmq-server/plugins-src/rabbitmq-stomp/test/src/lifecycle.py b/rabbitmq-server/plugins-src/rabbitmq-stomp/test/src/lifecycle.py new file mode 100644 index 0000000..ff9b119 --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-stomp/test/src/lifecycle.py @@ -0,0 +1,168 @@ +import unittest +import stomp +import base +import time + +class TestLifecycle(base.BaseTest): + + def test_unsubscribe_exchange_destination(self): + ''' Test UNSUBSCRIBE command with exchange''' + d = "/exchange/amq.fanout" + self.unsub_test(d, self.sub_and_send(d)) + + def test_unsubscribe_exchange_destination_with_receipt(self): + ''' Test receipted UNSUBSCRIBE command with exchange''' + d = "/exchange/amq.fanout" + self.unsub_test(d, self.sub_and_send(d, receipt="unsub.rct"), numRcts=1) + + def test_unsubscribe_queue_destination(self): + ''' Test UNSUBSCRIBE command with queue''' + d = "/queue/unsub01" + self.unsub_test(d, self.sub_and_send(d)) + + def test_unsubscribe_queue_destination_with_receipt(self): + ''' Test receipted UNSUBSCRIBE command with queue''' + d = "/queue/unsub02" + self.unsub_test(d, self.sub_and_send(d, receipt="unsub.rct"), numRcts=1) + + def test_unsubscribe_exchange_id(self): + ''' Test UNSUBSCRIBE command with exchange by id''' + d = "/exchange/amq.fanout" + self.unsub_test(d, self.sub_and_send(d, subid="exchid")) + + def test_unsubscribe_exchange_id_with_receipt(self): + ''' Test receipted UNSUBSCRIBE command with exchange by id''' + d = "/exchange/amq.fanout" + self.unsub_test(d, self.sub_and_send(d, subid="exchid", receipt="unsub.rct"), numRcts=1) + + def test_unsubscribe_queue_id(self): + ''' Test UNSUBSCRIBE command with queue by id''' + d = "/queue/unsub03" + self.unsub_test(d, self.sub_and_send(d, subid="queid")) + + def test_unsubscribe_queue_id_with_receipt(self): + ''' Test receipted UNSUBSCRIBE command with queue by id''' + d = "/queue/unsub04" + self.unsub_test(d, self.sub_and_send(d, subid="queid", receipt="unsub.rct"), numRcts=1) + + def test_connect_version_1_0(self): + ''' Test CONNECT with version 1.0''' + self.conn.disconnect() + new_conn = self.create_connection(version="1.0") + try: + self.assertTrue(new_conn.is_connected()) + finally: + new_conn.disconnect() + self.assertFalse(new_conn.is_connected()) + + def test_connect_version_1_1(self): + ''' Test CONNECT with version 1.1''' + self.conn.disconnect() + new_conn = self.create_connection(version="1.1") + try: + self.assertTrue(new_conn.is_connected()) + finally: + new_conn.disconnect() + self.assertFalse(new_conn.is_connected()) + + def test_connect_version_1_2(self): + ''' Test CONNECT with version 1.2''' + self.conn.disconnect() + new_conn = self.create_connection(version="1.2") + try: + self.assertTrue(new_conn.is_connected()) + finally: + new_conn.disconnect() + self.assertFalse(new_conn.is_connected()) + + def test_heartbeat_disconnects_client(self): + ''' Test heart-beat disconnection''' + self.conn.disconnect() + new_conn = self.create_connection(version='1.1', heartbeats=(1500, 0)) + try: + self.assertTrue(new_conn.is_connected()) + time.sleep(1) + self.assertTrue(new_conn.is_connected()) + time.sleep(3) + self.assertFalse(new_conn.is_connected()) + finally: + if new_conn.is_connected(): + new_conn.disconnect() + + def test_unsupported_version(self): + ''' Test unsupported version on CONNECT command''' + self.bad_connect("Supported versions are 1.0,1.1,1.2\n", version='100.1') + + def test_bad_username(self): + ''' Test bad username''' + self.bad_connect("Access refused for user 'gust'\n", user='gust') + + def test_bad_password(self): + ''' Test bad password''' + self.bad_connect("Access refused for user 'guest'\n", passcode='gust') + + def test_bad_vhost(self): + ''' Test bad virtual host''' + self.bad_connect("Virtual host '//' access denied", version='1.1', vhost='//') + + def bad_connect(self, expected, user='guest', passcode='guest', **kwargs): + self.conn.disconnect() + new_conn = self.create_connection_obj(**kwargs) + listener = base.WaitableListener() + new_conn.set_listener('', listener) + try: + new_conn.start() + new_conn.connect(user, passcode) + self.assertTrue(listener.await()) + self.assertEquals(expected, listener.errors[0]['message']) + finally: + if new_conn.is_connected(): + new_conn.disconnect() + + def test_bad_header_on_send(self): + ''' Test disallowed header on SEND ''' + self.listener.reset(1) + self.conn.send_frame("SEND", {"destination":"a", "message-id":"1"}) + self.assertTrue(self.listener.await()) + self.assertEquals(1, len(self.listener.errors)) + errorReceived = self.listener.errors[0] + self.assertEquals("Invalid header", errorReceived['headers']['message']) + self.assertEquals("'message-id' is not allowed on 'SEND'.\n", errorReceived['message']) + + def test_disconnect(self): + ''' Test DISCONNECT command''' + self.conn.disconnect() + self.assertFalse(self.conn.is_connected()) + + def test_disconnect_with_receipt(self): + ''' Test the DISCONNECT command with receipts ''' + time.sleep(3) + self.listener.reset(1) + self.conn.send_frame("DISCONNECT", {"receipt": "test"}) + self.assertTrue(self.listener.await()) + self.assertEquals(1, len(self.listener.receipts)) + receiptReceived = self.listener.receipts[0]['headers']['receipt-id'] + self.assertEquals("test", receiptReceived + , "Wrong receipt received: '" + receiptReceived + "'") + + def unsub_test(self, dest, verbs, numRcts=0): + def afterfun(): + self.conn.send(dest, "after-test") + subverb, unsubverb = verbs + self.assertListenerAfter(subverb, numMsgs=1, + errMsg="FAILED to subscribe and send") + self.assertListenerAfter(unsubverb, numRcts=numRcts, + errMsg="Incorrect responses from UNSUBSCRIBE") + self.assertListenerAfter(afterfun, + errMsg="Still receiving messages") + + def sub_and_send(self, dest, subid=None, receipt=None): + def subfun(): + self.subscribe_dest(self.conn, dest, subid) + self.conn.send(dest, "test") + def unsubfun(): + headers = {} + if receipt != None: + headers['receipt'] = receipt + self.unsubscribe_dest(self.conn, dest, subid, **headers) + return subfun, unsubfun diff --git a/rabbitmq-server/plugins-src/rabbitmq-stomp/test/src/parsing.py b/rabbitmq-server/plugins-src/rabbitmq-stomp/test/src/parsing.py new file mode 100644 index 0000000..27326b6 --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-stomp/test/src/parsing.py @@ -0,0 +1,317 @@ +''' +Few tests for a rabbitmq-stomp adaptor. They intend to increase code coverage +of the erlang stomp code. +''' +import unittest +import re +import socket +import functools +import time +import sys + +def connect(cnames): + ''' Decorator that creates stomp connections and issues CONNECT ''' + cmd=('CONNECT\n' + 'login:guest\n' + 'passcode:guest\n' + '\n' + '\n\0') + resp = ('CONNECTED\n' + 'session:(.*)\n' + 'heart-beat:0,0\n' + 'server:RabbitMQ/(.*)\n' + 'version:1.0\n' + '\n\x00') + def w(m): + @functools.wraps(m) + def wrapper(self, *args, **kwargs): + for cname in cnames: + sd = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + sd.settimeout(30000) + sd.connect((self.host, self.port)) + sd.sendall(cmd) + self.match(resp, sd.recv(4096)) + setattr(self, cname, sd) + try: + r = m(self, *args, **kwargs) + finally: + for cname in cnames: + try: + getattr(self, cname).close() + except IOError: + pass + return r + return wrapper + return w + + +class TestParsing(unittest.TestCase): + host='127.0.0.1' + port=61613 + + + def match(self, pattern, data): + ''' helper: try to match a regexp with a string. + Fail test if they do not match. + ''' + matched = re.match(pattern, data) + if matched: + return matched.groups() + self.assertTrue(False, 'No match:\n%r\n%r' % (pattern, data) ) + + def recv_atleast(self, bufsize): + recvhead = [] + rl = bufsize + while rl > 0: + buf = self.cd.recv(rl) + bl = len(buf) + if bl==0: break + recvhead.append( buf ) + rl -= bl + return ''.join(recvhead) + + + @connect(['cd']) + def test_newline_after_nul(self): + self.cd.sendall('\n' + 'SUBSCRIBE\n' + 'destination:/exchange/amq.fanout\n' + '\n\x00\n' + 'SEND\n' + 'content-type:text/plain\n' + 'destination:/exchange/amq.fanout\n\n' + 'hello\n\x00\n') + resp = ('MESSAGE\n' + 'destination:/exchange/amq.fanout\n' + 'message-id:Q_/exchange/amq.fanout@@session-(.*)\n' + 'redelivered:false\n' + 'content-type:text/plain\n' + 'content-length:6\n' + '\n' + 'hello\n\0') + self.match(resp, self.cd.recv(4096)) + + @connect(['cd']) + def test_send_without_content_type(self): + self.cd.sendall('\n' + 'SUBSCRIBE\n' + 'destination:/exchange/amq.fanout\n' + '\n\x00\n' + 'SEND\n' + 'destination:/exchange/amq.fanout\n\n' + 'hello\n\x00') + resp = ('MESSAGE\n' + 'destination:/exchange/amq.fanout\n' + 'message-id:Q_/exchange/amq.fanout@@session-(.*)\n' + 'redelivered:false\n' + 'content-length:6\n' + '\n' + 'hello\n\0') + self.match(resp, self.cd.recv(4096)) + + @connect(['cd']) + def test_send_without_content_type_binary(self): + msg = u'\u0ca0\ufffd\x00\n\x01hello\x00'.encode('utf-8') + self.cd.sendall('\n' + 'SUBSCRIBE\n' + 'destination:/exchange/amq.fanout\n' + '\n\x00\n' + 'SEND\n' + 'destination:/exchange/amq.fanout\n' + 'content-length:'+str(len(msg))+'\n\n' + + msg + '\x00') + resp = ('MESSAGE\n' + 'destination:/exchange/amq.fanout\n' + 'message-id:Q_/exchange/amq.fanout@@session-(.*)\n' + 'redelivered:false\n' + 'content-length:'+str(len(msg))+'\n' + '\n' + + msg + '\0') + self.match(resp, self.cd.recv(4096)) + + @connect(['cd']) + def test_newline_after_nul_and_leading_nul(self): + self.cd.sendall('\n' + '\x00SUBSCRIBE\n' + 'destination:/exchange/amq.fanout\n' + '\n\x00\n' + '\x00SEND\n' + 'destination:/exchange/amq.fanout\n' + 'content-type:text/plain\n' + '\nhello\n\x00\n') + resp = ('MESSAGE\n' + 'destination:/exchange/amq.fanout\n' + 'message-id:Q_/exchange/amq.fanout@@session-(.*)\n' + 'redelivered:false\n' + 'content-type:text/plain\n' + 'content-length:6\n' + '\n' + 'hello\n\0') + self.match(resp, self.cd.recv(4096)) + + @connect(['cd']) + def test_bad_command(self): + ''' Trigger an error message. ''' + self.cd.sendall('WRONGCOMMAND\n' + 'destination:a\n' + 'exchange:amq.fanout\n' + '\n\0') + resp = ('ERROR\n' + 'message:Bad command\n' + 'content-type:text/plain\n' + 'version:1.0,1.1,1.2\n' + 'content-length:43\n' + '\n' + 'Could not interpret command "WRONGCOMMAND"\n' + '\0') + self.match(resp, self.cd.recv(4096)) + + @connect(['sd', 'cd1', 'cd2']) + def test_broadcast(self): + ''' Single message should be delivered to two consumers: + amq.topic --routing_key--> first_queue --> first_connection + \--routing_key--> second_queue--> second_connection + ''' + subscribe=( 'SUBSCRIBE\n' + 'id: XsKNhAf\n' + 'destination:/exchange/amq.topic/da9d4779\n' + '\n\0') + for cd in [self.cd1, self.cd2]: + cd.sendall(subscribe) + + time.sleep(0.1) + + self.sd.sendall('SEND\n' + 'content-type:text/plain\n' + 'destination:/exchange/amq.topic/da9d4779\n' + '\n' + 'message' + '\n\0') + + resp=('MESSAGE\n' + 'subscription:(.*)\n' + 'destination:/topic/da9d4779\n' + 'message-id:(.*)\n' + 'redelivered:false\n' + 'content-type:text/plain\n' + 'content-length:8\n' + '\n' + 'message' + '\n\x00') + for cd in [self.cd1, self.cd2]: + self.match(resp, cd.recv(4096)) + + @connect(['cd']) + def test_message_with_embedded_nulls(self): + ''' Test sending/receiving message with embedded nulls. ''' + dest='destination:/exchange/amq.topic/test_embed_nulls_message\n' + resp_dest='destination:/topic/test_embed_nulls_message\n' + subscribe=( 'SUBSCRIBE\n' + 'id:xxx\n' + +dest+ + '\n\0') + self.cd.sendall(subscribe) + + boilerplate = '0123456789'*1024 # large enough boilerplate + message = '01' + oldi = 2 + for i in [5, 90, 256-1, 384-1, 512, 1024, 1024+256+64+32]: + message = message + '\0' + boilerplate[oldi+1:i] + oldi = i + msg_len = len(message) + + self.cd.sendall('SEND\n' + +dest+ + 'content-type:text/plain\n' + 'content-length:%i\n' + '\n' + '%s' + '\0' % (len(message), message) ) + + headresp=('MESSAGE\n' # 8 + 'subscription:(.*)\n' # 14 + subscription + +resp_dest+ # 44 + 'message-id:(.*)\n' # 12 + message-id + 'redelivered:false\n' # 18 + 'content-type:text/plain\n' # 24 + 'content-length:%i\n' # 16 + 4==len('1024') + '\n' # 1 + '(.*)$' # prefix of body+null (potentially) + % len(message) ) + headlen = 8 + 24 + 14 + (3) + 44 + 12 + 18 + (48) + 16 + (4) + 1 + (1) + + headbuf = self.recv_atleast(headlen) + self.assertFalse(len(headbuf) == 0) + + (sub, msg_id, bodyprefix) = self.match(headresp, headbuf) + bodyresp=( '%s\0' % message ) + bodylen = len(bodyresp); + + bodybuf = ''.join([bodyprefix, + self.recv_atleast(bodylen - len(bodyprefix))]) + + self.assertEqual(len(bodybuf), msg_len+1, + "body received not the same length as message sent") + self.assertEqual(bodybuf, bodyresp, + " body (...'%s')\nincorrectly returned as (...'%s')" + % (bodyresp[-10:], bodybuf[-10:])) + + @connect(['cd']) + def test_message_in_packets(self): + ''' Test sending/receiving message in packets. ''' + base_dest='topic/test_embed_nulls_message\n' + dest='destination:/exchange/amq.' + base_dest + resp_dest='destination:/'+ base_dest + subscribe=( 'SUBSCRIBE\n' + 'id:xxx\n' + +dest+ + '\n\0') + self.cd.sendall(subscribe) + + boilerplate = '0123456789'*1024 # large enough boilerplate + + message = boilerplate[:1024 + 512 + 256 + 32] + msg_len = len(message) + + msg_to_send = ('SEND\n' + +dest+ + 'content-type:text/plain\n' + '\n' + '%s' + '\0' % (message) ) + packet_size = 191 + part_index = 0 + msg_to_send_len = len(msg_to_send) + while part_index < msg_to_send_len: + part = msg_to_send[part_index:part_index+packet_size] + time.sleep(0.1) + self.cd.sendall(part) + part_index += packet_size + + headresp=('MESSAGE\n' # 8 + 'subscription:(.*)\n' # 14 + subscription + +resp_dest+ # 44 + 'message-id:(.*)\n' # 12 + message-id + 'redelivered:false\n' # 18 + 'content-type:text/plain\n' # 24 + 'content-length:%i\n' # 16 + 4==len('1024') + '\n' # 1 + '(.*)$' # prefix of body+null (potentially) + % len(message) ) + headlen = 8 + 24 + 14 + (3) + 44 + 12 + 18 + (48) + 16 + (4) + 1 + (1) + + headbuf = self.recv_atleast(headlen) + self.assertFalse(len(headbuf) == 0) + + (sub, msg_id, bodyprefix) = self.match(headresp, headbuf) + bodyresp=( '%s\0' % message ) + bodylen = len(bodyresp); + + bodybuf = ''.join([bodyprefix, + self.recv_atleast(bodylen - len(bodyprefix))]) + + self.assertEqual(len(bodybuf), msg_len+1, + "body received not the same length as message sent") + self.assertEqual(bodybuf, bodyresp, + " body ('%s')\nincorrectly returned as ('%s')" + % (bodyresp, bodybuf)) diff --git a/rabbitmq-server/plugins-src/rabbitmq-stomp/test/src/queue_properties.py b/rabbitmq-server/plugins-src/rabbitmq-stomp/test/src/queue_properties.py new file mode 100644 index 0000000..cc85487 --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-stomp/test/src/queue_properties.py @@ -0,0 +1,79 @@ +import unittest +import stomp +import pika +import base +import time + +class TestQueueProperties(base.BaseTest): + + def test_subscribe(self): + destination = "/queue/queue-properties-subscribe-test" + + # subscribe + self.subscribe_dest(self.conn, destination, None, + headers={ + 'x-message-ttl': 60000, + 'x-expires': 70000, + 'x-max-length': 10, + 'x-max-length-bytes': 20000, + 'x-dead-letter-exchange': 'dead-letter-exchange', + 'x-dead-letter-routing-key': 'dead-letter-routing-key', + 'x-max-priority': 6, + }) + + # now try to declare the queue using pika + # if the properties are the same we should + # not get any error + connection = pika.BlockingConnection(pika.ConnectionParameters( + host='localhost')) + channel = connection.channel() + channel.queue_declare(queue='queue-properties-subscribe-test', + durable=True, + arguments={ + 'x-message-ttl': 60000, + 'x-expires': 70000, + 'x-max-length': 10, + 'x-max-length-bytes': 20000, + 'x-dead-letter-exchange': 'dead-letter-exchange', + 'x-dead-letter-routing-key': 'dead-letter-routing-key', + 'x-max-priority': 6, + }) + + self.conn.disconnect() + connection.close() + + def test_send(self): + destination = "/queue/queue-properties-send-test" + + # send + self.conn.send(destination, "test1", + headers={ + 'x-message-ttl': 60000, + 'x-expires': 70000, + 'x-max-length': 10, + 'x-max-length-bytes': 20000, + 'x-dead-letter-exchange': 'dead-letter-exchange', + 'x-dead-letter-routing-key': 'dead-letter-routing-key', + 'x-max-priority': 6, + }) + + # now try to declare the queue using pika + # if the properties are the same we should + # not get any error + connection = pika.BlockingConnection(pika.ConnectionParameters( + host='localhost')) + channel = connection.channel() + channel.queue_declare(queue='queue-properties-send-test', + durable=True, + arguments={ + 'x-message-ttl': 60000, + 'x-expires': 70000, + 'x-max-length': 10, + 'x-max-length-bytes': 20000, + 'x-dead-letter-exchange': 'dead-letter-exchange', + 'x-dead-letter-routing-key': 'dead-letter-routing-key', + 'x-max-priority': 6, + }) + + self.conn.disconnect() + connection.close() diff --git a/rabbitmq-server/plugins-src/rabbitmq-stomp/test/src/rabbit_stomp_amqqueue_test.erl b/rabbitmq-server/plugins-src/rabbitmq-stomp/test/src/rabbit_stomp_amqqueue_test.erl new file mode 100644 index 0000000..42c18ed --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-stomp/test/src/rabbit_stomp_amqqueue_test.erl @@ -0,0 +1,225 @@ +%% The contents of this file are subject to the Mozilla Public License +%% Version 1.1 (the "License"); you may not use this file except in +%% compliance with the License. You may obtain a copy of the License +%% at http://www.mozilla.org/MPL/ +%% +%% Software distributed under the License is distributed on an "AS IS" +%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See +%% the License for the specific language governing rights and +%% limitations under the License. +%% +%% The Original Code is RabbitMQ. +%% +%% The Initial Developer of the Original Code is GoPivotal, Inc. +%% Copyright (c) 2007-2015 Pivotal Software, Inc. All rights reserved. +%% + +-module(rabbit_stomp_amqqueue_test). +-export([all_tests/0]). +-compile(export_all). + +-include_lib("eunit/include/eunit.hrl"). +-include_lib("amqp_client/include/amqp_client.hrl"). +-include("rabbit_stomp.hrl"). +-include("rabbit_stomp_frame.hrl"). +-include("rabbit_stomp_headers.hrl"). + +-define(QUEUE, <<"TestQueue">>). +-define(DESTINATION, "/amq/queue/TestQueue"). + +all_tests() -> + [[ok = run_test(TestFun, Version) + || TestFun <- [fun test_subscribe_error/3, + fun test_subscribe/3, + fun test_unsubscribe_ack/3, + fun test_subscribe_ack/3, + fun test_send/3, + fun test_delete_queue_subscribe/3, + fun test_temp_destination_queue/3, + fun test_temp_destination_in_send/3, + fun test_blank_destination_in_send/3]] + || Version <- ?SUPPORTED_VERSIONS], + ok. + +run_test(TestFun, Version) -> + {ok, Connection} = amqp_connection:start(#amqp_params_direct{}), + {ok, Channel} = amqp_connection:open_channel(Connection), + {ok, Client} = rabbit_stomp_client:connect(Version), + + Result = (catch TestFun(Channel, Client, Version)), + + rabbit_stomp_client:disconnect(Client), + amqp_channel:close(Channel), + amqp_connection:close(Connection), + Result. + +test_subscribe_error(_Channel, Client, _Version) -> + %% SUBSCRIBE to missing queue + rabbit_stomp_client:send( + Client, "SUBSCRIBE", [{"destination", ?DESTINATION}]), + {ok, _Client1, Hdrs, _} = stomp_receive(Client, "ERROR"), + "not_found" = proplists:get_value("message", Hdrs), + ok. + +test_subscribe(Channel, Client, _Version) -> + #'queue.declare_ok'{} = + amqp_channel:call(Channel, #'queue.declare'{queue = ?QUEUE, + auto_delete = true}), + + %% subscribe and wait for receipt + rabbit_stomp_client:send( + Client, "SUBSCRIBE", [{"destination", ?DESTINATION}, {"receipt", "foo"}]), + {ok, Client1, _, _} = stomp_receive(Client, "RECEIPT"), + + %% send from amqp + Method = #'basic.publish'{exchange = <<"">>, routing_key = ?QUEUE}, + + amqp_channel:call(Channel, Method, #amqp_msg{props = #'P_basic'{}, + payload = <<"hello">>}), + + {ok, _Client2, _, [<<"hello">>]} = stomp_receive(Client1, "MESSAGE"), + ok. + +test_unsubscribe_ack(Channel, Client, Version) -> + #'queue.declare_ok'{} = + amqp_channel:call(Channel, #'queue.declare'{queue = ?QUEUE, + auto_delete = true}), + %% subscribe and wait for receipt + rabbit_stomp_client:send( + Client, "SUBSCRIBE", [{"destination", ?DESTINATION}, + {"receipt", "rcpt1"}, + {"ack", "client"}, + {"id", "subscription-id"}]), + {ok, Client1, _, _} = stomp_receive(Client, "RECEIPT"), + + %% send from amqp + Method = #'basic.publish'{exchange = <<"">>, routing_key = ?QUEUE}, + + amqp_channel:call(Channel, Method, #amqp_msg{props = #'P_basic'{}, + payload = <<"hello">>}), + + {ok, Client2, Hdrs1, [<<"hello">>]} = stomp_receive(Client1, "MESSAGE"), + + rabbit_stomp_client:send( + Client2, "UNSUBSCRIBE", [{"destination", ?DESTINATION}, + {"id", "subscription-id"}]), + + rabbit_stomp_client:send( + Client2, "ACK", [{rabbit_stomp_util:ack_header_name(Version), + proplists:get_value( + rabbit_stomp_util:msg_header_name(Version), Hdrs1)}, + {"receipt", "rcpt2"}]), + + {ok, _Client3, Hdrs2, _Body2} = stomp_receive(Client2, "ERROR"), + ?assertEqual("Subscription not found", + proplists:get_value("message", Hdrs2)), + ok. + +test_subscribe_ack(Channel, Client, Version) -> + #'queue.declare_ok'{} = + amqp_channel:call(Channel, #'queue.declare'{queue = ?QUEUE, + auto_delete = true}), + + %% subscribe and wait for receipt + rabbit_stomp_client:send( + Client, "SUBSCRIBE", [{"destination", ?DESTINATION}, + {"receipt", "foo"}, + {"ack", "client"}]), + {ok, Client1, _, _} = stomp_receive(Client, "RECEIPT"), + + %% send from amqp + Method = #'basic.publish'{exchange = <<"">>, routing_key = ?QUEUE}, + + amqp_channel:call(Channel, Method, #amqp_msg{props = #'P_basic'{}, + payload = <<"hello">>}), + + {ok, _Client2, Headers, [<<"hello">>]} = stomp_receive(Client1, "MESSAGE"), + false = (Version == "1.2") xor proplists:is_defined(?HEADER_ACK, Headers), + + MsgHeader = rabbit_stomp_util:msg_header_name(Version), + AckValue = proplists:get_value(MsgHeader, Headers), + AckHeader = rabbit_stomp_util:ack_header_name(Version), + + rabbit_stomp_client:send(Client, "ACK", [{AckHeader, AckValue}]), + #'basic.get_empty'{} = + amqp_channel:call(Channel, #'basic.get'{queue = ?QUEUE}), + ok. + +test_send(Channel, Client, _Version) -> + #'queue.declare_ok'{} = + amqp_channel:call(Channel, #'queue.declare'{queue = ?QUEUE, + auto_delete = true}), + + %% subscribe and wait for receipt + rabbit_stomp_client:send( + Client, "SUBSCRIBE", [{"destination", ?DESTINATION}, {"receipt", "foo"}]), + {ok, Client1, _, _} = stomp_receive(Client, "RECEIPT"), + + %% send from stomp + rabbit_stomp_client:send( + Client1, "SEND", [{"destination", ?DESTINATION}], ["hello"]), + + {ok, _Client2, _, [<<"hello">>]} = stomp_receive(Client1, "MESSAGE"), + ok. + +test_delete_queue_subscribe(Channel, Client, _Version) -> + #'queue.declare_ok'{} = + amqp_channel:call(Channel, #'queue.declare'{queue = ?QUEUE, + auto_delete = true}), + + %% subscribe and wait for receipt + rabbit_stomp_client:send( + Client, "SUBSCRIBE", [{"destination", ?DESTINATION}, {"receipt", "bah"}]), + {ok, Client1, _, _} = stomp_receive(Client, "RECEIPT"), + + %% delete queue while subscribed + #'queue.delete_ok'{} = + amqp_channel:call(Channel, #'queue.delete'{queue = ?QUEUE}), + + {ok, _Client2, Headers, _} = stomp_receive(Client1, "ERROR"), + + ?DESTINATION = proplists:get_value("subscription", Headers), + + % server closes connection + ok. + +test_temp_destination_queue(Channel, Client, _Version) -> + #'queue.declare_ok'{} = + amqp_channel:call(Channel, #'queue.declare'{queue = ?QUEUE, + auto_delete = true}), + rabbit_stomp_client:send( Client, "SEND", [{"destination", ?DESTINATION}, + {"reply-to", "/temp-queue/foo"}], + ["ping"]), + amqp_channel:call(Channel,#'basic.consume'{queue = ?QUEUE, no_ack = true}), + receive #'basic.consume_ok'{consumer_tag = _Tag} -> ok end, + receive {#'basic.deliver'{delivery_tag = _DTag}, + #'amqp_msg'{payload = <<"ping">>, + props = #'P_basic'{reply_to = ReplyTo}}} -> ok + end, + ok = amqp_channel:call(Channel, + #'basic.publish'{routing_key = ReplyTo}, + #amqp_msg{payload = <<"pong">>}), + {ok, _Client1, _, [<<"pong">>]} = stomp_receive(Client, "MESSAGE"), + ok. + +test_temp_destination_in_send(_Channel, Client, _Version) -> + rabbit_stomp_client:send( Client, "SEND", [{"destination", "/temp-queue/foo"}], + ["poing"]), + {ok, _Client1, Hdrs, _} = stomp_receive(Client, "ERROR"), + "Invalid destination" = proplists:get_value("message", Hdrs), + ok. + +test_blank_destination_in_send(_Channel, Client, _Version) -> + rabbit_stomp_client:send( Client, "SEND", [{"destination", ""}], + ["poing"]), + {ok, _Client1, Hdrs, _} = stomp_receive(Client, "ERROR"), + "Invalid destination" = proplists:get_value("message", Hdrs), + ok. + +stomp_receive(Client, Command) -> + {#stomp_frame{command = Command, + headers = Hdrs, + body_iolist = Body}, Client1} = + rabbit_stomp_client:recv(Client), + {ok, Client1, Hdrs, Body}. + diff --git a/rabbitmq-server/plugins-src/rabbitmq-stomp/test/src/rabbit_stomp_client.erl b/rabbitmq-server/plugins-src/rabbitmq-stomp/test/src/rabbit_stomp_client.erl new file mode 100644 index 0000000..2ae0699 --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-stomp/test/src/rabbit_stomp_client.erl @@ -0,0 +1,83 @@ +%% The contents of this file are subject to the Mozilla Public License +%% Version 1.1 (the "License"); you may not use this file except in +%% compliance with the License. You may obtain a copy of the License at +%% http://www.mozilla.org/MPL/ +%% +%% Software distributed under the License is distributed on an "AS IS" +%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the +%% License for the specific language governing rights and limitations +%% under the License. +%% +%% The Original Code is RabbitMQ Management Console. +%% +%% The Initial Developers of the Original Code are Rabbit Technologies Ltd. +%% +%% Copyright (C) 2011 Rabbit Technologies Ltd. +%% +%% All Rights Reserved. +%% +%% Contributor(s): ______________________________________. +%% + +%% The stupidest client imaginable, just for testing. + +-module(rabbit_stomp_client). + +-export([connect/0, connect/1, disconnect/1, send/2, send/3, send/4, recv/1]). + +-include("rabbit_stomp_frame.hrl"). + +-define(TIMEOUT, 1000). % milliseconds + +connect() -> connect0([]). +connect(V) -> connect0([{"accept-version", V}]). + +connect0(Version) -> + {ok, Sock} = gen_tcp:connect(localhost, 61613, [{active, false}, binary]), + Client0 = recv_state(Sock), + send(Client0, "CONNECT", [{"login", "guest"}, + {"passcode", "guest"} | Version]), + {#stomp_frame{command = "CONNECTED"}, Client1} = recv(Client0), + {ok, Client1}. + +disconnect(Client = {Sock, _}) -> + send(Client, "DISCONNECT"), + gen_tcp:close(Sock). + +send(Client, Command) -> + send(Client, Command, []). + +send(Client, Command, Headers) -> + send(Client, Command, Headers, []). + +send({Sock, _}, Command, Headers, Body) -> + Frame = rabbit_stomp_frame:serialize( + #stomp_frame{command = list_to_binary(Command), + headers = Headers, + body_iolist = Body}), + gen_tcp:send(Sock, Frame). + +recv_state(Sock) -> + {Sock, []}. + +recv({_Sock, []} = Client) -> + recv(Client, rabbit_stomp_frame:initial_state(), 0); +recv({Sock, [Frame | Frames]}) -> + {Frame, {Sock, Frames}}. + +recv(Client = {Sock, _}, FrameState, Length) -> + {ok, Payload} = gen_tcp:recv(Sock, Length, ?TIMEOUT), + parse(Payload, Client, FrameState, Length). + +parse(Payload, Client = {Sock, FramesRev}, FrameState, Length) -> + case rabbit_stomp_frame:parse(Payload, FrameState) of + {ok, Frame, <<>>} -> + recv({Sock, lists:reverse([Frame | FramesRev])}); + {ok, Frame, <<"\n">>} -> + recv({Sock, lists:reverse([Frame | FramesRev])}); + {ok, Frame, Rest} -> + parse(Rest, {Sock, [Frame | FramesRev]}, + rabbit_stomp_frame:initial_state(), Length); + {more, NewState} -> + recv(Client, NewState, 0) + end. diff --git a/rabbitmq-server/plugins-src/rabbitmq-stomp/test/src/rabbit_stomp_publish_test.erl b/rabbitmq-server/plugins-src/rabbitmq-stomp/test/src/rabbit_stomp_publish_test.erl new file mode 100644 index 0000000..a67e9aa --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-stomp/test/src/rabbit_stomp_publish_test.erl @@ -0,0 +1,88 @@ +%% The contents of this file are subject to the Mozilla Public License +%% Version 1.1 (the "License"); you may not use this file except in +%% compliance with the License. You may obtain a copy of the License at +%% http://www.mozilla.org/MPL/ +%% +%% Software distributed under the License is distributed on an "AS IS" +%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the +%% License for the specific language governing rights and limitations +%% under the License. +%% +%% The Original Code is RabbitMQ Management Console. +%% +%% The Initial Developers of the Original Code are Rabbit Technologies Ltd. +%% +%% Copyright (C) 2011 Rabbit Technologies Ltd. +%% +%% All Rights Reserved. +%% +%% Contributor(s): ______________________________________. +%% +-module(rabbit_stomp_publish_test). + +-export([run/0]). + +-include("rabbit_stomp_frame.hrl"). + +-define(DESTINATION, "/queue/test"). + +-define(MICROS_PER_UPDATE, 5000000). +-define(MICROS_PER_UPDATE_MSG, 100000). +-define(MICROS_PER_SECOND, 1000000). + +%% A very simple publish-and-consume-as-fast-as-you-can test. + +run() -> + [put(K, 0) || K <- [sent, recd, last_sent, last_recd]], + put(last_ts, os:timestamp()), + {ok, Pub} = rabbit_stomp_client:connect(), + {ok, Recv} = rabbit_stomp_client:connect(), + Self = self(), + spawn(fun() -> publish(Self, Pub, 0, os:timestamp()) end), + rabbit_stomp_client:send( + Recv, "SUBSCRIBE", [{"destination", ?DESTINATION}]), + spawn(fun() -> recv(Self, Recv, 0, os:timestamp()) end), + report(). + +report() -> + receive + {sent, C} -> put(sent, C); + {recd, C} -> put(recd, C) + end, + Diff = timer:now_diff(os:timestamp(), get(last_ts)), + case Diff > ?MICROS_PER_UPDATE of + true -> S = get(sent) - get(last_sent), + R = get(recd) - get(last_recd), + put(last_sent, get(sent)), + put(last_recd, get(recd)), + put(last_ts, os:timestamp()), + io:format("Send ~p msg/s | Recv ~p msg/s~n", + [trunc(S * ?MICROS_PER_SECOND / Diff), + trunc(R * ?MICROS_PER_SECOND / Diff)]); + false -> ok + end, + report(). + +publish(Owner, Client, Count, TS) -> + rabbit_stomp_client:send( + Client, "SEND", [{"destination", ?DESTINATION}], + [integer_to_list(Count)]), + Diff = timer:now_diff(os:timestamp(), TS), + case Diff > ?MICROS_PER_UPDATE_MSG of + true -> Owner ! {sent, Count + 1}, + publish(Owner, Client, Count + 1, os:timestamp()); + false -> publish(Owner, Client, Count + 1, TS) + end. + +recv(Owner, Client0, Count, TS) -> + {#stomp_frame{body_iolist = Body}, Client1} = + rabbit_stomp_client:recv(Client0), + BodyInt = list_to_integer(binary_to_list(iolist_to_binary(Body))), + Count = BodyInt, + Diff = timer:now_diff(os:timestamp(), TS), + case Diff > ?MICROS_PER_UPDATE_MSG of + true -> Owner ! {recd, Count + 1}, + recv(Owner, Client1, Count + 1, os:timestamp()); + false -> recv(Owner, Client1, Count + 1, TS) + end. + diff --git a/rabbitmq-server/plugins-src/rabbitmq-stomp/test/src/rabbit_stomp_test.erl b/rabbitmq-server/plugins-src/rabbitmq-stomp/test/src/rabbit_stomp_test.erl new file mode 100644 index 0000000..2f5b580 --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-stomp/test/src/rabbit_stomp_test.erl @@ -0,0 +1,65 @@ +%% The contents of this file are subject to the Mozilla Public License +%% Version 1.1 (the "License"); you may not use this file except in +%% compliance with the License. You may obtain a copy of the License +%% at http://www.mozilla.org/MPL/ +%% +%% Software distributed under the License is distributed on an "AS IS" +%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See +%% the License for the specific language governing rights and +%% limitations under the License. +%% +%% The Original Code is RabbitMQ. +%% +%% The Initial Developer of the Original Code is GoPivotal, Inc. +%% Copyright (c) 2007-2015 Pivotal Software, Inc. All rights reserved. +%% + +-module(rabbit_stomp_test). +-export([all_tests/0]). +-import(rabbit_misc, [pget/2]). + +-include_lib("amqp_client/include/amqp_client.hrl"). +-include("rabbit_stomp_frame.hrl"). +-define(DESTINATION, "/queue/bulk-test"). + +all_tests() -> + test_messages_not_dropped_on_disconnect(), + test_direct_client_connections_are_not_leaked(), + ok. + +-define(GARBAGE, <<"bdaf63dda9d78b075c748b740e7c3510ad203b07\nbdaf63dd">>). + +count_connections() -> + length(supervisor2:which_children(rabbit_stomp_client_sup_sup)). + +test_direct_client_connections_are_not_leaked() -> + N = count_connections(), + lists:foreach(fun (_) -> + {ok, Client = {Socket, _}} = rabbit_stomp_client:connect(), + %% send garbage which trips up the parser + gen_tcp:send(Socket, ?GARBAGE), + rabbit_stomp_client:send( + Client, "LOL", [{"", ""}]) + end, + lists:seq(1, 100)), + timer:sleep(5000), + N = count_connections(), + ok. + +test_messages_not_dropped_on_disconnect() -> + N = count_connections(), + {ok, Client} = rabbit_stomp_client:connect(), + N1 = N + 1, + N1 = count_connections(), + [rabbit_stomp_client:send( + Client, "SEND", [{"destination", ?DESTINATION}], + [integer_to_list(Count)]) || Count <- lists:seq(1, 1000)], + rabbit_stomp_client:disconnect(Client), + QName = rabbit_misc:r(<<"/">>, queue, <<"bulk-test">>), + timer:sleep(3000), + N = count_connections(), + rabbit_amqqueue:with( + QName, fun(Q) -> + 1000 = pget(messages, rabbit_amqqueue:info(Q, [messages])) + end), + ok. diff --git a/rabbitmq-server/plugins-src/rabbitmq-stomp/test/src/rabbit_stomp_test_frame.erl b/rabbitmq-server/plugins-src/rabbitmq-stomp/test/src/rabbit_stomp_test_frame.erl new file mode 100644 index 0000000..c53fff7 --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-stomp/test/src/rabbit_stomp_test_frame.erl @@ -0,0 +1,171 @@ +%% The contents of this file are subject to the Mozilla Public License +%% Version 1.1 (the "License"); you may not use this file except in +%% compliance with the License. You may obtain a copy of the License +%% at http://www.mozilla.org/MPL/ +%% +%% Software distributed under the License is distributed on an "AS IS" +%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See +%% the License for the specific language governing rights and +%% limitations under the License. +%% +%% The Original Code is RabbitMQ. +%% +%% The Initial Developer of the Original Code is GoPivotal, Inc. +%% Copyright (c) 2007-2015 Pivotal Software, Inc. All rights reserved. +%% + +-module(rabbit_stomp_test_frame). + +-include_lib("eunit/include/eunit.hrl"). +-include_lib("amqp_client/include/amqp_client.hrl"). +-include("rabbit_stomp_frame.hrl"). +-include("rabbit_stomp_headers.hrl"). + +parse_simple_frame_test() -> + parse_simple_frame_gen("\n"). + +parse_simple_frame_crlf_test() -> + parse_simple_frame_gen("\r\n"). + +parse_simple_frame_gen(Term) -> + Headers = [{"header1", "value1"}, {"header2", "value2"}], + Content = frame_string("COMMAND", + Headers, + "Body Content", + Term), + {"COMMAND", Frame, _State} = parse_complete(Content), + [?assertEqual({ok, Value}, + rabbit_stomp_frame:header(Frame, Key)) || + {Key, Value} <- Headers], + #stomp_frame{body_iolist = Body} = Frame, + ?assertEqual(<<"Body Content">>, iolist_to_binary(Body)). + +parse_command_only_test() -> + {ok, #stomp_frame{command = "COMMAND"}, _Rest} = parse("COMMAND\n\n\0"). + +parse_ignore_empty_frames_test() -> + {ok, #stomp_frame{command = "COMMAND"}, _Rest} = parse("\0\0COMMAND\n\n\0"). + +parse_heartbeat_interframe_test() -> + {ok, #stomp_frame{command = "COMMAND"}, _Rest} = parse("\nCOMMAND\n\n\0"). + +parse_crlf_interframe_test() -> + {ok, #stomp_frame{command = "COMMAND"}, _Rest} = parse("\r\nCOMMAND\n\n\0"). + +parse_carriage_return_not_ignored_interframe_test() -> + {error, {unexpected_chars_between_frames, "\rC"}} = parse("\rCOMMAND\n\n\0"). + +parse_carriage_return_mid_command_test() -> + {error, {unexpected_chars_in_command, "\rA"}} = parse("COMM\rAND\n\n\0"). + +parse_carriage_return_end_command_test() -> + {error, {unexpected_chars_in_command, "\r\r"}} = parse("COMMAND\r\r\n\n\0"). + +parse_resume_mid_command_test() -> + First = "COMM", + Second = "AND\n\n\0", + {more, Resume} = parse(First), + {ok, #stomp_frame{command = "COMMAND"}, _Rest} = parse(Second, Resume). + +parse_resume_mid_header_key_test() -> + First = "COMMAND\nheade", + Second = "r1:value1\n\n\0", + {more, Resume} = parse(First), + {ok, Frame = #stomp_frame{command = "COMMAND"}, _Rest} = + parse(Second, Resume), + ?assertEqual({ok, "value1"}, + rabbit_stomp_frame:header(Frame, "header1")). + +parse_resume_mid_header_val_test() -> + First = "COMMAND\nheader1:val", + Second = "ue1\n\n\0", + {more, Resume} = parse(First), + {ok, Frame = #stomp_frame{command = "COMMAND"}, _Rest} = + parse(Second, Resume), + ?assertEqual({ok, "value1"}, + rabbit_stomp_frame:header(Frame, "header1")). + +parse_resume_mid_body_test() -> + First = "COMMAND\n\nABC", + Second = "DEF\0", + {more, Resume} = parse(First), + {ok, #stomp_frame{command = "COMMAND", body_iolist = Body}, _Rest} = + parse(Second, Resume), + ?assertEqual([<<"ABC">>, <<"DEF">>], Body). + +parse_no_header_stripping_test() -> + Content = "COMMAND\nheader: foo \n\n\0", + {ok, Frame, _} = parse(Content), + {ok, Val} = rabbit_stomp_frame:header(Frame, "header"), + ?assertEqual(" foo ", Val). + +parse_multiple_headers_test() -> + Content = "COMMAND\nheader:correct\nheader:incorrect\n\n\0", + {ok, Frame, _} = parse(Content), + {ok, Val} = rabbit_stomp_frame:header(Frame, "header"), + ?assertEqual("correct", Val). + +header_no_colon_test() -> + Content = "COMMAND\n" + "hdr1:val1\n" + "hdrerror\n" + "hdr2:val2\n" + "\n\0", + ?assertEqual(parse(Content), {error, {header_no_value, "hdrerror"}}). + +no_nested_escapes_test() -> + Content = "COM\\\\rAND\n" % no escapes + "hdr\\\\rname:" % one escape + "hdr\\\\rval\n\n\0", % one escape + {ok, Frame, _} = parse(Content), + ?assertEqual(Frame, + #stomp_frame{command = "COM\\\\rAND", + headers = [{"hdr\\rname", "hdr\\rval"}], + body_iolist = []}). + +header_name_with_cr_test() -> + Content = "COMMAND\nhead\rer:val\n\n\0", + {error, {unexpected_chars_in_header, "\re"}} = parse(Content). + +header_value_with_cr_test() -> + Content = "COMMAND\nheader:val\rue\n\n\0", + {error, {unexpected_chars_in_header, "\ru"}} = parse(Content). + +header_value_with_colon_test() -> + Content = "COMMAND\nheader:val:ue\n\n\0", + {ok, Frame, _} = parse(Content), + ?assertEqual(Frame, + #stomp_frame{ command = "COMMAND", + headers = [{"header", "val:ue"}], + body_iolist = []}). + +test_frame_serialization(Expected, TrailingLF) -> + {ok, Frame, _} = parse(Expected), + {ok, Val} = rabbit_stomp_frame:header(Frame, "head\r:\ner"), + ?assertEqual(":\n\r\\", Val), + Serialized = lists:flatten(rabbit_stomp_frame:serialize(Frame, TrailingLF)), + ?assertEqual(Expected, rabbit_misc:format("~s", [Serialized])). + +headers_escaping_roundtrip_test() -> + test_frame_serialization("COMMAND\nhead\\r\\c\\ner:\\c\\n\\r\\\\\n\n\0\n", true). + +headers_escaping_roundtrip_without_trailing_lf_test() -> + test_frame_serialization("COMMAND\nhead\\r\\c\\ner:\\c\\n\\r\\\\\n\n\0", false). + +parse(Content) -> + parse(Content, rabbit_stomp_frame:initial_state()). +parse(Content, State) -> + rabbit_stomp_frame:parse(list_to_binary(Content), State). + +parse_complete(Content) -> + {ok, Frame = #stomp_frame{command = Command}, State} = parse(Content), + {Command, Frame, State}. + +frame_string(Command, Headers, BodyContent) -> + frame_string(Command, Headers, BodyContent, "\n"). + +frame_string(Command, Headers, BodyContent, Term) -> + HeaderString = + lists:flatten([Key ++ ":" ++ Value ++ Term || {Key, Value} <- Headers]), + Command ++ Term ++ HeaderString ++ Term ++ BodyContent ++ "\0" ++ "\n". + diff --git a/rabbitmq-server/plugins-src/rabbitmq-stomp/test/src/rabbit_stomp_test_util.erl b/rabbitmq-server/plugins-src/rabbitmq-stomp/test/src/rabbit_stomp_test_util.erl new file mode 100644 index 0000000..a25e306 --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-stomp/test/src/rabbit_stomp_test_util.erl @@ -0,0 +1,226 @@ +%% The contents of this file are subject to the Mozilla Public License +%% Version 1.1 (the "License"); you may not use this file except in +%% compliance with the License. You may obtain a copy of the License +%% at http://www.mozilla.org/MPL/ +%% +%% Software distributed under the License is distributed on an "AS IS" +%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See +%% the License for the specific language governing rights and +%% limitations under the License. +%% +%% The Original Code is RabbitMQ. +%% +%% The Initial Developer of the Original Code is GoPivotal, Inc. +%% Copyright (c) 2007-2015 Pivotal Software, Inc. All rights reserved. +%% + +-module(rabbit_stomp_test_util). + +-include_lib("eunit/include/eunit.hrl"). +-include_lib("amqp_client/include/amqp_client.hrl"). +-include_lib("amqp_client/include/rabbit_routing_prefixes.hrl"). +-include("rabbit_stomp_frame.hrl"). + +%%-------------------------------------------------------------------- +%% Header Processing Tests +%%-------------------------------------------------------------------- + +longstr_field_test() -> + {<<"ABC">>, longstr, <<"DEF">>} = + rabbit_stomp_util:longstr_field("ABC", "DEF"). + +message_properties_test() -> + Headers = [ + {"content-type", "text/plain"}, + {"content-encoding", "UTF-8"}, + {"persistent", "true"}, + {"priority", "1"}, + {"correlation-id", "123"}, + {"reply-to", "something"}, + {"expiration", "my-expiration"}, + {"amqp-message-id", "M123"}, + {"timestamp", "123456"}, + {"type", "freshly-squeezed"}, + {"user-id", "joe"}, + {"app-id", "joe's app"}, + {"str", "foo"}, + {"int", "123"} + ], + + #'P_basic'{ + content_type = <<"text/plain">>, + content_encoding = <<"UTF-8">>, + delivery_mode = 2, + priority = 1, + correlation_id = <<"123">>, + reply_to = <<"something">>, + expiration = <<"my-expiration">>, + message_id = <<"M123">>, + timestamp = 123456, + type = <<"freshly-squeezed">>, + user_id = <<"joe">>, + app_id = <<"joe's app">>, + headers = [{<<"str">>, longstr, <<"foo">>}, + {<<"int">>, longstr, <<"123">>}] + } = + rabbit_stomp_util:message_properties(#stomp_frame{headers = Headers}). + +message_headers_test() -> + Properties = #'P_basic'{ + headers = [{<<"str">>, longstr, <<"foo">>}, + {<<"int">>, signedint, 123}], + content_type = <<"text/plain">>, + content_encoding = <<"UTF-8">>, + delivery_mode = 2, + priority = 1, + correlation_id = 123, + reply_to = <<"something">>, + message_id = <<"M123">>, + timestamp = 123456, + type = <<"freshly-squeezed">>, + user_id = <<"joe">>, + app_id = <<"joe's app">>}, + + Headers = rabbit_stomp_util:message_headers(Properties), + + Expected = [ + {"content-type", "text/plain"}, + {"content-encoding", "UTF-8"}, + {"persistent", "true"}, + {"priority", "1"}, + {"correlation-id", "123"}, + {"reply-to", "something"}, + {"expiration", "my-expiration"}, + {"amqp-message-id", "M123"}, + {"timestamp", "123456"}, + {"type", "freshly-squeezed"}, + {"user-id", "joe"}, + {"app-id", "joe's app"}, + {"str", "foo"}, + {"int", "123"} + ], + + [] = lists:subtract(Headers, Expected). + +minimal_message_headers_with_no_custom_test() -> + Delivery = #'basic.deliver'{ + consumer_tag = <<"Q_123">>, + delivery_tag = 123, + exchange = <<"">>, + routing_key = <<"foo">>}, + + Properties = #'P_basic'{}, + + Headers = rabbit_stomp_util:message_headers(Properties), + Expected = [ + {"content-type", "text/plain"}, + {"content-encoding", "UTF-8"}, + {"amqp-message-id", "M123"} + ], + + [] = lists:subtract(Headers, Expected). + +headers_post_process_test() -> + Headers = [{"header1", "1"}, + {"header2", "12"}, + {"reply-to", "something"}], + Expected = [{"header1", "1"}, + {"header2", "12"}, + {"reply-to", "/reply-queue/something"}], + [] = lists:subtract( + rabbit_stomp_util:headers_post_process(Headers), Expected). + +headers_post_process_noop_replyto_test() -> + [begin + Headers = [{"reply-to", Prefix ++ "/something"}], + Headers = rabbit_stomp_util:headers_post_process(Headers) + end || Prefix <- rabbit_routing_util:dest_prefixes()]. + +headers_post_process_noop2_test() -> + Headers = [{"header1", "1"}, + {"header2", "12"}], + Expected = [{"header1", "1"}, + {"header2", "12"}], + [] = lists:subtract( + rabbit_stomp_util:headers_post_process(Headers), Expected). + +negotiate_version_both_empty_test() -> + {error, no_common_version} = rabbit_stomp_util:negotiate_version([],[]). + +negotiate_version_no_common_test() -> + {error, no_common_version} = + rabbit_stomp_util:negotiate_version(["1.2"],["1.3"]). + +negotiate_version_simple_common_test() -> + {ok, "1.2"} = + rabbit_stomp_util:negotiate_version(["1.2"],["1.2"]). + +negotiate_version_two_choice_common_test() -> + {ok, "1.3"} = + rabbit_stomp_util:negotiate_version(["1.2", "1.3"],["1.2", "1.3"]). + +negotiate_version_two_choice_common_out_of_order_test() -> + {ok, "1.3"} = + rabbit_stomp_util:negotiate_version(["1.3", "1.2"],["1.2", "1.3"]). + +negotiate_version_two_choice_big_common_test() -> + {ok, "1.20.23"} = + rabbit_stomp_util:negotiate_version(["1.20.23", "1.30.456"], + ["1.20.23", "1.30.457"]). +negotiate_version_choice_mismatched_length_test() -> + {ok, "1.2.3"} = + rabbit_stomp_util:negotiate_version(["1.2", "1.2.3"], + ["1.2.3", "1.2"]). +negotiate_version_choice_duplicates_test() -> + {ok, "1.2"} = + rabbit_stomp_util:negotiate_version(["1.2", "1.2"], + ["1.2", "1.2"]). +trim_headers_test() -> + #stomp_frame{headers = [{"one", "foo"}, {"two", "baz "}]} = + rabbit_stomp_util:trim_headers( + #stomp_frame{headers = [{"one", " foo"}, {"two", " baz "}]}). + +%%-------------------------------------------------------------------- +%% Frame Parsing Tests +%%-------------------------------------------------------------------- + +ack_mode_auto_test() -> + Frame = #stomp_frame{headers = [{"ack", "auto"}]}, + {auto, _} = rabbit_stomp_util:ack_mode(Frame). + +ack_mode_auto_default_test() -> + Frame = #stomp_frame{headers = []}, + {auto, _} = rabbit_stomp_util:ack_mode(Frame). + +ack_mode_client_test() -> + Frame = #stomp_frame{headers = [{"ack", "client"}]}, + {client, true} = rabbit_stomp_util:ack_mode(Frame). + +ack_mode_client_individual_test() -> + Frame = #stomp_frame{headers = [{"ack", "client-individual"}]}, + {client, false} = rabbit_stomp_util:ack_mode(Frame). + +consumer_tag_id_test() -> + Frame = #stomp_frame{headers = [{"id", "foo"}]}, + {ok, <<"T_foo">>, _} = rabbit_stomp_util:consumer_tag(Frame). + +consumer_tag_destination_test() -> + Frame = #stomp_frame{headers = [{"destination", "foo"}]}, + {ok, <<"Q_foo">>, _} = rabbit_stomp_util:consumer_tag(Frame). + +consumer_tag_invalid_test() -> + Frame = #stomp_frame{headers = []}, + {error, missing_destination_header} = rabbit_stomp_util:consumer_tag(Frame). + +%%-------------------------------------------------------------------- +%% Message ID Parsing Tests +%%-------------------------------------------------------------------- + +parse_valid_message_id_test() -> + {ok, {<<"bar">>, "abc", 123}} = + rabbit_stomp_util:parse_message_id("bar@@abc@@123"). + +parse_invalid_message_id_test() -> + {error, invalid_message_id} = + rabbit_stomp_util:parse_message_id("blah"). + diff --git a/rabbitmq-server/plugins-src/rabbitmq-stomp/test/src/redelivered.py b/rabbitmq-server/plugins-src/rabbitmq-stomp/test/src/redelivered.py new file mode 100644 index 0000000..bbabd3f --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-stomp/test/src/redelivered.py @@ -0,0 +1,32 @@ +import unittest +import stomp +import base +import time + +class TestRedelivered(base.BaseTest): + + def test_redelivered(self): + destination = "/queue/redelivered-test" + + # subscribe and send message + self.subscribe_dest(self.conn, destination, None, ack='client') + self.conn.send(destination, "test1") + self.assertTrue(self.listener.await(4), "initial message not received") + self.assertEquals(1, len(self.listener.messages)) + self.assertEquals('false', self.listener.messages[0]['headers']['redelivered']) + + # disconnect with no ack + self.conn.disconnect() + + # now reconnect + conn2 = self.create_connection() + try: + listener2 = base.WaitableListener() + listener2.reset(1) + conn2.set_listener('', listener2) + self.subscribe_dest(conn2, destination, None, ack='client') + self.assertTrue(listener2.await(), "message not received again") + self.assertEquals(1, len(listener2.messages)) + self.assertEquals('true', listener2.messages[0]['headers']['redelivered']) + finally: + conn2.disconnect() diff --git a/rabbitmq-server/plugins-src/rabbitmq-stomp/test/src/reliability.py b/rabbitmq-server/plugins-src/rabbitmq-stomp/test/src/reliability.py new file mode 100644 index 0000000..b8bb150 --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-stomp/test/src/reliability.py @@ -0,0 +1,34 @@ +import base +import stomp +import unittest +import time + +class TestReliability(base.BaseTest): + + def test_send_and_disconnect(self): + ''' Test close socket after send does not lose messages ''' + destination = "/queue/reliability" + pub_conn = self.create_connection() + try: + msg = "0" * (128) + + count = 10000 + + listener = base.WaitableListener() + listener.reset(count) + self.conn.set_listener('', listener) + self.subscribe_dest(self.conn, destination, None) + + for x in range(0, count): + pub_conn.send(destination, msg + str(x)) + time.sleep(2.0) + pub_conn.disconnect() + + if listener.await(30): + self.assertEquals(count, len(listener.messages)) + else: + listener.print_state("Final state of listener:") + self.fail("Did not receive %s messages in time" % count) + finally: + if pub_conn.is_connected(): + pub_conn.disconnect() diff --git a/rabbitmq-server/plugins-src/rabbitmq-stomp/test/src/ssl_lifecycle.py b/rabbitmq-server/plugins-src/rabbitmq-stomp/test/src/ssl_lifecycle.py new file mode 100644 index 0000000..53636df --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-stomp/test/src/ssl_lifecycle.py @@ -0,0 +1,75 @@ +import unittest +import os +import os.path +import sys + +import stomp +import base +import ssl + + +base_path = os.path.dirname(sys.argv[0]) + +ssl_key_file = os.path.abspath(base_path + "/../certs/client/key.pem") +ssl_cert_file = os.path.abspath(base_path + "/../certs/client/cert.pem") +ssl_ca_certs = os.path.abspath(base_path + "/../certs/testca/cacert.pem") + +class TestSslClient(unittest.TestCase): + + def __ssl_connect(self): + conn = stomp.Connection(host_and_ports = [ ('localhost', 61614) ], + use_ssl = True, ssl_key_file = ssl_key_file, + ssl_cert_file = ssl_cert_file, + ssl_ca_certs = ssl_ca_certs) + print "FILE: ", ssl_cert_file + conn.start() + conn.connect("guest", "guest") + return conn + + def __ssl_auth_connect(self): + conn = stomp.Connection(host_and_ports = [ ('localhost', 61614) ], + use_ssl = True, ssl_key_file = ssl_key_file, + ssl_cert_file = ssl_cert_file, + ssl_ca_certs = ssl_ca_certs) + conn.start() + conn.connect() + return conn + + def test_ssl_connect(self): + conn = self.__ssl_connect() + conn.disconnect() + + def test_ssl_auth_connect(self): + conn = self.__ssl_auth_connect() + conn.disconnect() + + def test_ssl_send_receive(self): + conn = self.__ssl_connect() + self.__test_conn(conn) + + def test_ssl_auth_send_receive(self): + conn = self.__ssl_auth_connect() + self.__test_conn(conn) + + def __test_conn(self, conn): + try: + listener = base.WaitableListener() + + conn.set_listener('', listener) + + d = "/topic/ssl.test" + conn.subscribe(destination=d, ack="auto", id="ctag", receipt="sub") + + self.assertTrue(listener.await(1)) + + self.assertEquals("sub", + listener.receipts[0]['headers']['receipt-id']) + + listener.reset(1) + conn.send(body="Hello SSL!", destination=d) + + self.assertTrue(listener.await()) + + self.assertEquals("Hello SSL!", listener.messages[0]['message']) + finally: + conn.disconnect() diff --git a/rabbitmq-server/plugins-src/rabbitmq-stomp/test/src/test.config b/rabbitmq-server/plugins-src/rabbitmq-stomp/test/src/test.config new file mode 100644 index 0000000..4fd77fb --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-stomp/test/src/test.config @@ -0,0 +1,12 @@ +[{rabbitmq_stomp, [{default_user, []}, + {ssl_cert_login, true}, + {ssl_listeners, [61614]} + ]}, + {rabbit, [{ssl_options, [{cacertfile,"%%CERTS_DIR%%/testca/cacert.pem"}, + {certfile,"%%CERTS_DIR%%/server/cert.pem"}, + {keyfile,"%%CERTS_DIR%%/server/key.pem"}, + {verify,verify_peer}, + {fail_if_no_peer_cert,true} + ]} + ]} +]. diff --git a/rabbitmq-server/plugins-src/rabbitmq-stomp/test/src/test.py b/rabbitmq-server/plugins-src/rabbitmq-stomp/test/src/test.py new file mode 100755 index 0000000..381c5b4 --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-stomp/test/src/test.py @@ -0,0 +1,18 @@ +#!/usr/bin/env python + +import test_runner + +if __name__ == '__main__': + modules = [ + 'ack', + 'destinations', + 'errors', + 'lifecycle', + 'parsing', + 'queue_properties', + 'redelivered', + 'reliability', + 'transactions', + ] + test_runner.run_unittests(modules) + diff --git a/rabbitmq-server/plugins-src/rabbitmq-stomp/test/src/test_connect_options.py b/rabbitmq-server/plugins-src/rabbitmq-stomp/test/src/test_connect_options.py new file mode 100755 index 0000000..6822f97 --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-stomp/test/src/test_connect_options.py @@ -0,0 +1,8 @@ +#!/usr/bin/env python + +import test_runner + +if __name__ == '__main__': + modules = ['connect_options'] + test_runner.run_unittests(modules) + diff --git a/rabbitmq-server/plugins-src/rabbitmq-stomp/test/src/test_runner.py b/rabbitmq-server/plugins-src/rabbitmq-stomp/test/src/test_runner.py new file mode 100644 index 0000000..90a5456 --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-stomp/test/src/test_runner.py @@ -0,0 +1,26 @@ +#!/usr/bin/env python + +import unittest +import sys +import os + +def add_deps_to_path(): + deps_dir = os.path.realpath(os.path.join(__file__, "..", "..", "..", "deps")) + sys.path.append(os.path.join(deps_dir, "stomppy", "stomppy")) + sys.path.append(os.path.join(deps_dir, "pika", "pika")) + +def run_unittests(modules): + add_deps_to_path() + + suite = unittest.TestSuite() + for m in modules: + mod = __import__(m) + for name in dir(mod): + obj = getattr(mod, name) + if name.startswith("Test") and issubclass(obj, unittest.TestCase): + suite.addTest(unittest.TestLoader().loadTestsFromTestCase(obj)) + + ts = unittest.TextTestRunner().run(unittest.TestSuite(suite)) + if ts.errors or ts.failures: + sys.exit(1) + diff --git a/rabbitmq-server/plugins-src/rabbitmq-stomp/test/src/test_ssl.py b/rabbitmq-server/plugins-src/rabbitmq-stomp/test/src/test_ssl.py new file mode 100755 index 0000000..e96be6a --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-stomp/test/src/test_ssl.py @@ -0,0 +1,10 @@ +#!/usr/bin/env python + +import test_runner +import test_util + +if __name__ == '__main__': + modules = ['ssl_lifecycle'] + test_util.ensure_ssl_auth_user() + test_runner.run_unittests(modules) + diff --git a/rabbitmq-server/plugins-src/rabbitmq-stomp/test/src/test_util.py b/rabbitmq-server/plugins-src/rabbitmq-stomp/test/src/test_util.py new file mode 100644 index 0000000..f22fd66 --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-stomp/test/src/test_util.py @@ -0,0 +1,42 @@ +import subprocess +import socket +import sys +import os +import os.path + +def ensure_ssl_auth_user(): + user = 'O=client,CN=%s' % socket.gethostname() + rabbitmqctl(['stop_app']) + rabbitmqctl(['reset']) + rabbitmqctl(['start_app']) + rabbitmqctl(['add_user', user, 'foo']) + rabbitmqctl(['clear_password', user]) + rabbitmqctl(['set_permissions', user, '.*', '.*', '.*']) + +def enable_implicit_connect(): + switch_config(implicit_connect='true', default_user='[{login, "guest"}, {passcode, "guest"}]') + +def disable_implicit_connect(): + switch_config(implicit_connect='false', default_user='[]') + +def enable_default_user(): + switch_config(default_user='[{login, "guest"}, {passcode, "guest"}]') + +def disable_default_user(): + switch_config(default_user='[]') + +def switch_config(implicit_connect='', default_user=''): + cmd = 'application:stop(rabbitmq_stomp),' + if implicit_connect: + cmd += 'application:set_env(rabbitmq_stomp,implicit_connect,' + implicit_connect + '),' + if default_user: + cmd += 'application:set_env(rabbitmq_stomp,default_user,' + default_user + '),' + cmd += 'application:start(rabbitmq_stomp).' + rabbitmqctl(['eval', cmd]) + +def rabbitmqctl(args): + ctl = os.path.normpath(os.path.join(os.getcwd(), sys.argv[0], '../../../../rabbitmq-server/scripts/rabbitmqctl')) + cmdline = [ctl, '-n', 'rabbit-test'] + cmdline.extend(args) + subprocess.check_call(cmdline) + diff --git a/rabbitmq-server/plugins-src/rabbitmq-stomp/test/src/transactions.py b/rabbitmq-server/plugins-src/rabbitmq-stomp/test/src/transactions.py new file mode 100644 index 0000000..d4f166b --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-stomp/test/src/transactions.py @@ -0,0 +1,54 @@ +import unittest +import stomp +import base +import time + +class TestTransactions(base.BaseTest): + + def test_tx_commit(self): + ''' Test TX with a COMMIT and ensure messages are delivered ''' + destination = "/exchange/amq.fanout" + tx = "test.tx" + + self.listener.reset() + self.subscribe_dest(self.conn, destination, None) + self.conn.begin(transaction=tx) + self.conn.send(destination, "hello!", transaction=tx) + self.conn.send(destination, "again!") + + ## should see the second message + self.assertTrue(self.listener.await(3)) + self.assertEquals(1, len(self.listener.messages)) + self.assertEquals("again!", self.listener.messages[0]['message']) + + ## now look for the first message + self.listener.reset() + self.conn.commit(transaction=tx) + self.assertTrue(self.listener.await(3)) + self.assertEquals(1, len(self.listener.messages), + "Missing committed message") + self.assertEquals("hello!", self.listener.messages[0]['message']) + + def test_tx_abort(self): + ''' Test TX with an ABORT and ensure messages are discarded ''' + destination = "/exchange/amq.fanout" + tx = "test.tx" + + self.listener.reset() + self.subscribe_dest(self.conn, destination, None) + self.conn.begin(transaction=tx) + self.conn.send(destination, "hello!", transaction=tx) + self.conn.send(destination, "again!") + + ## should see the second message + self.assertTrue(self.listener.await(3)) + self.assertEquals(1, len(self.listener.messages)) + self.assertEquals("again!", self.listener.messages[0]['message']) + + ## now look for the first message to be discarded + self.listener.reset() + self.conn.abort(transaction=tx) + self.assertFalse(self.listener.await(3)) + self.assertEquals(0, len(self.listener.messages), + "Unexpected committed message") + diff --git a/rabbitmq-server/plugins-src/rabbitmq-test/.srcdist_done b/rabbitmq-server/plugins-src/rabbitmq-test/.srcdist_done new file mode 100644 index 0000000..e69de29 diff --git a/rabbitmq-server/plugins-src/rabbitmq-test/CONTRIBUTING.md b/rabbitmq-server/plugins-src/rabbitmq-test/CONTRIBUTING.md new file mode 100644 index 0000000..69a4b4a --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-test/CONTRIBUTING.md @@ -0,0 +1,51 @@ +## Overview + +RabbitMQ projects use pull requests to discuss, collaborate on and accept code contributions. +Pull requests is the primary place of discussing code changes. + +## How to Contribute + +The process is fairly standard: + + * Fork the repository or repositories you plan on contributing to + * Clone [RabbitMQ umbrella repository](https://github.com/rabbitmq/rabbitmq-public-umbrella) + * `cd umbrella`, `make co` + * Create a branch with a descriptive name in the relevant repositories + * Make your changes, run tests, commit with a [descriptive message](http://tbaggery.com/2008/04/19/a-note-about-git-commit-messages.html), push to your fork + * Submit pull requests with an explanation what has been changed and **why** + * Submit a filled out and signed [Contributor Agreement](https://github.com/rabbitmq/ca#how-to-submit) if needed (see below) + * Be patient. We will get to your pull request eventually + +If what you are going to work on is a substantial change, please first ask the core team +of their opinion on [RabbitMQ mailing list](https://groups.google.com/forum/#!forum/rabbitmq-users). + + +## (Brief) Code of Conduct + +In one line: don't be a dick. + +Be respectful to the maintainers and other contributors. Open source +contributors put long hours into developing projects and doing user +support. Those projects and user support are available for free. We +believe this deserves some respect. + +Be respectful to people of all races, genders, religious beliefs and +political views. Regardless of how brilliant a pull request is +technically, we will not tolerate disrespectful or aggressive +behaviour. + +Contributors who violate this straightforward Code of Conduct will see +their pull requests closed and locked. + + +## Contributor Agreement + +If you want to contribute a non-trivial change, please submit a signed copy of our +[Contributor Agreement](https://github.com/rabbitmq/ca#how-to-submit) around the time +you submit your pull request. This will make it much easier (in some cases, possible) +for the RabbitMQ team at Pivotal to merge your contribution. + + +## Where to Ask Questions + +If something isn't clear, feel free to ask on our [mailing list](https://groups.google.com/forum/#!forum/rabbitmq-users). diff --git a/rabbitmq-server/plugins-src/rabbitmq-test/Makefile b/rabbitmq-server/plugins-src/rabbitmq-test/Makefile new file mode 100644 index 0000000..3f11414 --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-test/Makefile @@ -0,0 +1,210 @@ +.PHONY: all full lite conformance16 update-qpid-testsuite run-qpid-testsuite \ + prepare restart-app stop-app start-app \ + start-secondary-app stop-secondary-app \ + restart-secondary-node cleanup force-snapshot \ + enable-ha disable-ha + +include ../umbrella.mk + +BROKER_DIR=../rabbitmq-server +TEST_DIR=../rabbitmq-java-client + +TEST_RABBIT_PORT=5672 +TEST_HARE_PORT=5673 +TEST_RABBIT_SSL_PORT=5671 +TEST_HARE_SSL_PORT=5670 + +COVER=true + +ifeq ($(COVER), true) +COVER_START=start-cover +COVER_STOP=stop-cover +else +COVER_START= +COVER_STOP= +endif + +# we actually want to test for ssl above 3.9 (eg >= 3.10), but this +# comparison is buggy because it doesn't believe 10 > 9, so it doesn't +# believe 3.10 > 3.9. As a result, we cheat, and use the erts version +# instead. SSL 3.10 came out with R13B, which included erts 5.7.1, so +# we require > 5.7.0. +SSL_VERIFY=$(shell if [ $$(erl -noshell -eval 'io:format(erlang:system_info(version)), halt().') \> "5.7.0" ]; then echo "true"; else echo "false"; fi) +ifeq (true,$(SSL_VERIFY)) +SSL_VERIFY_OPTION :={verify,verify_peer},{fail_if_no_peer_cert,false} +else +SSL_VERIFY_OPTION :={verify_code,1} +endif +export SSL_CERTS_DIR := $(realpath certs) +export PASSWORD := test +RABBIT_BROKER_OPTIONS := "-rabbit ssl_listeners [{\\\"0.0.0.0\\\",$(TEST_RABBIT_SSL_PORT)}] -rabbit ssl_options [{cacertfile,\\\"$(SSL_CERTS_DIR)/testca/cacert.pem\\\"},{certfile,\\\"$(SSL_CERTS_DIR)/server/cert.pem\\\"},{keyfile,\\\"$(SSL_CERTS_DIR)/server/key.pem\\\"},$(SSL_VERIFY_OPTION)] -rabbit auth_mechanisms ['PLAIN','AMQPLAIN','EXTERNAL','RABBIT-CR-DEMO']" +HARE_BROKER_OPTIONS := "-rabbit ssl_listeners [{\\\"0.0.0.0\\\",$(TEST_HARE_SSL_PORT)}] -rabbit ssl_options [{cacertfile,\\\"$(SSL_CERTS_DIR)/testca/cacert.pem\\\"},{certfile,\\\"$(SSL_CERTS_DIR)/server/cert.pem\\\"},{keyfile,\\\"$(SSL_CERTS_DIR)/server/key.pem\\\"},$(SSL_VERIFY_OPTION)] -rabbit auth_mechanisms ['PLAIN','AMQPLAIN','EXTERNAL','RABBIT-CR-DEMO']" + +TESTS_FAILED := echo '\n============'\ + '\nTESTS FAILED'\ + '\n============\n' + +all: full test + +full: + OK=true && \ + $(MAKE) prepare && \ + { $(MAKE) -C $(BROKER_DIR) run-tests || { OK=false; $(TESTS_FAILED); } } && \ + { $(MAKE) run-qpid-testsuite || { OK=false; $(TESTS_FAILED); } } && \ + { ( cd $(TEST_DIR) && MAKE=$(MAKE) ant test-suite ) || { OK=false; $(TESTS_FAILED); } } && \ + $(MAKE) cleanup && { $$OK || $(TESTS_FAILED); } && $$OK + +unit: + OK=true && \ + $(MAKE) prepare && \ + { $(MAKE) -C $(BROKER_DIR) run-tests || OK=false; } && \ + $(MAKE) cleanup && $$OK + +lite: + OK=true && \ + $(MAKE) prepare && \ + { $(MAKE) -C $(BROKER_DIR) run-tests || OK=false; } && \ + { ( cd $(TEST_DIR) && MAKE=$(MAKE) ant test-suite ) || OK=false; } && \ + $(MAKE) cleanup && $$OK + +conformance16: + OK=true && \ + $(MAKE) prepare && \ + { $(MAKE) -C $(BROKER_DIR) run-tests || OK=false; } && \ + { ( cd $(TEST_DIR) && MAKE=$(MAKE) ant test-suite ) || OK=false; } && \ + $(MAKE) cleanup && $$OK + +qpid_testsuite: + $(MAKE) update-qpid-testsuite + +update-qpid-testsuite: + svn co -r 906960 http://svn.apache.org/repos/asf/qpid/trunk/qpid/python qpid_testsuite + # hg clone http://rabbit-hg.eng.vmware.com/mirrors/qpid_testsuite + - patch -N -r - -p0 -d qpid_testsuite/ < qpid_patch + +prepare-qpid-patch: + cd qpid_testsuite && svn diff > ../qpid_patch && cd .. + +run-qpid-testsuite: qpid_testsuite + AMQP_SPEC=../rabbitmq-docs/specs/amqp0-8.xml qpid_testsuite/qpid-python-test -m tests_0-8 -I rabbit_failing.txt + AMQP_SPEC=../rabbitmq-docs/specs/amqp0-9-1.xml qpid_testsuite/qpid-python-test -m tests_0-9 -I rabbit_failing.txt + +clean: + rm -rf qpid_testsuite + +prepare: create_ssl_certs + $(MAKE) -C $(BROKER_DIR) \ + RABBITMQ_NODENAME=hare \ + RABBITMQ_NODE_IP_ADDRESS=0.0.0.0 \ + RABBITMQ_NODE_PORT=${TEST_HARE_PORT} \ + RABBITMQ_SERVER_START_ARGS=$(HARE_BROKER_OPTIONS) \ + RABBITMQ_CONFIG_FILE=/does-not-exist \ + RABBITMQ_ENABLED_PLUGINS_FILE=/does-not-exist \ + stop-node cleandb start-background-node + $(MAKE) -C $(BROKER_DIR) \ + RABBITMQ_NODE_IP_ADDRESS=0.0.0.0 \ + RABBITMQ_NODE_PORT=${TEST_RABBIT_PORT} \ + RABBITMQ_SERVER_START_ARGS=$(RABBIT_BROKER_OPTIONS) \ + RABBITMQ_CONFIG_FILE=/does-not-exist \ + RABBITMQ_ENABLED_PLUGINS_FILE=/does-not-exist \ + stop-node cleandb start-background-node ${COVER_START} start-rabbit-on-node + $(MAKE) -C $(BROKER_DIR) RABBITMQ_NODENAME=hare start-rabbit-on-node + +start-app: + $(MAKE) -C $(BROKER_DIR) \ + RABBITMQ_NODE_IP_ADDRESS=0.0.0.0 \ + RABBITMQ_NODE_PORT=${TEST_RABBIT_PORT} \ + RABBITMQ_SERVER_START_ARGS=$(RABBIT_BROKER_OPTIONS) \ + RABBITMQ_CONFIG_FILE=/does-not-exist \ + RABBITMQ_ENABLED_PLUGINS_FILE=/does-not-exist \ + start-rabbit-on-node + +stop-app: + $(MAKE) -C $(BROKER_DIR) stop-rabbit-on-node + +restart-app: stop-app start-app + +start-secondary-app: + $(MAKE) -C $(BROKER_DIR) RABBITMQ_NODENAME=hare start-rabbit-on-node + +stop-secondary-app: + $(MAKE) -C $(BROKER_DIR) RABBITMQ_NODENAME=hare stop-rabbit-on-node + +restart-secondary-node: + $(MAKE) -C $(BROKER_DIR) \ + RABBITMQ_NODENAME=hare \ + RABBITMQ_NODE_IP_ADDRESS=0.0.0.0 \ + RABBITMQ_NODE_PORT=${TEST_HARE_PORT} \ + RABBITMQ_SERVER_START_ARGS=$(HARE_BROKER_OPTIONS) \ + RABBITMQ_CONFIG_FILE=/does-not-exist \ + RABBITMQ_ENABLED_PLUGINS_FILE=/does-not-exist \ + stop-node start-background-node + $(MAKE) -C $(BROKER_DIR) RABBITMQ_NODENAME=hare start-rabbit-on-node + +force-snapshot: + $(MAKE) -C $(BROKER_DIR) force-snapshot + +set-resource-alarm: + $(MAKE) -C $(BROKER_DIR) set-resource-alarm SOURCE=$(SOURCE) + +clear-resource-alarm: + $(MAKE) -C $(BROKER_DIR) clear-resource-alarm SOURCE=$(SOURCE) + +enable-ha: + $(BROKER_DIR)/scripts/rabbitmqctl set_policy HA \ + ".*" '{"ha-mode": "all"}' + +disable-ha: + $(BROKER_DIR)/scripts/rabbitmqctl clear_policy HA + +cleanup: + -$(MAKE) -C $(BROKER_DIR) \ + RABBITMQ_NODENAME=hare \ + RABBITMQ_NODE_IP_ADDRESS=0.0.0.0 \ + RABBITMQ_NODE_PORT=${TEST_HARE_PORT} \ + RABBITMQ_SERVER_START_ARGS=$(HARE_BROKER_OPTIONS) \ + RABBITMQ_CONFIG_FILE=/does-not-exist \ + RABBITMQ_ENABLED_PLUGINS_FILE=/does-not-exist \ + stop-rabbit-on-node stop-node + -$(MAKE) -C $(BROKER_DIR) \ + RABBITMQ_NODE_IP_ADDRESS=0.0.0.0 \ + RABBITMQ_NODE_PORT=${TEST_RABBIT_PORT} \ + RABBITMQ_SERVER_START_ARGS=$(RABBIT_BROKER_OPTIONS) \ + RABBITMQ_CONFIG_FILE=/does-not-exist \ + RABBITMQ_ENABLED_PLUGINS_FILE=/does-not-exist \ + stop-rabbit-on-node ${COVER_STOP} stop-node + +define compare_version +$(shell awk 'BEGIN { + split("$(1)", v1, "\."); + version1 = v1[1] * 1000000 + v1[2] * 10000 + v1[3] * 100 + v1[4]; + + split("$(2)", v2, "\."); + version2 = v2[1] * 1000000 + v2[2] * 10000 + v2[3] * 100 + v2[4]; + + if (version1 $(3) version2) { + print "true"; + } else { + print "false"; + } +}') +endef + +ERLANG_SSL_VER = $(shell erl -noshell -eval '\ + ok = application:load(ssl), \ + {ok, VSN} = application:get_key(ssl, vsn), \ + io:format("~s~n", [VSN]), \ + halt(0).') +MINIMUM_ERLANG_SSL_VER = 5.3 + +ifeq ($(call compare_version,$(ERLANG_SSL_VER),$(MINIMUM_ERLANG_SSL_VER),>=),true) +create_ssl_certs: + $(MAKE) -C certs DIR=$(SSL_CERTS_DIR) clean all +else +create_ssl_certs: + @# Skip SSL certs if Erlang is older than R16B01 (ssl 5.3). + $(MAKE) -C certs DIR=$(SSL_CERTS_DIR) clean + @echo "WARNING: Skip SSL certs creation; Erlang's SSL application is too" \ + "old ($(ERLANG_SSL_VER) < $(MINIMUM_ERLANG_SSL_VER)) and SSL support" \ + "is disabled in RabbitMQ" +endif diff --git a/rabbitmq-server/plugins-src/rabbitmq-test/README b/rabbitmq-server/plugins-src/rabbitmq-test/README new file mode 100644 index 0000000..9b19505 --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-test/README @@ -0,0 +1,19 @@ +Useful targets: + +$ make unit # runs the Erlang unit tests +$ make lite # runs the Erlang unit tests and the Java client / functional tests +$ make full # runs both the above plus the QPid test suite +$ make test # runs the Erlang multi-node integration tests +$ make all # runs all of the above + +The multi-node tests take a long time, so you might want to run a subset: + +$ make test FILTER=dynamic_ha # <- run just one suite +$ make test FILTER=dynamic_ha:change_policy # <- run just one test + +The multi-node tests also default to coverage off, to turn it on: + +$ make test COVER=true + +This repository is not related to plugin tests; run "make test" in a +plugin directory to test that plugin. diff --git a/rabbitmq-server/plugins-src/rabbitmq-test/certs/Makefile b/rabbitmq-server/plugins-src/rabbitmq-test/certs/Makefile new file mode 100644 index 0000000..32db63f --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-test/certs/Makefile @@ -0,0 +1,58 @@ +OPENSSL=openssl + +ifndef DIR +DIR := . +endif + +ifdef PASSWORD +P12PASS := true +else +P12PASS := @echo No PASSWORD defined. && false +endif + +.PRECIOUS: %/testca +.PHONY: %/clean target all p12pass + +all: client server + +client: p12pass + echo $(DIR) + $(MAKE) target DIR=$(DIR) TARGET=client EXTENSIONS=client_ca_extensions + +server: p12pass + $(MAKE) target DIR=$(DIR) TARGET=server EXTENSIONS=server_ca_extensions + +p12pass: + $(P12PASS) + +target: $(DIR)/testca + mkdir $(DIR)/$(TARGET) + { ( cd $(DIR)/$(TARGET) && \ + openssl genrsa -out key.pem 2048 &&\ + openssl req -new -key key.pem -out req.pem -outform PEM\ + -subj /CN=$$(hostname)/O=$(TARGET)/L=$$$$/ -nodes &&\ + cd ../testca && \ + openssl ca -config openssl.cnf -in ../$(TARGET)/req.pem -out \ + ../$(TARGET)/cert.pem -notext -batch -extensions \ + $(EXTENSIONS) && \ + cd ../$(TARGET) && \ + openssl pkcs12 -export -out keycert.p12 -in cert.pem -inkey key.pem \ + -passout pass:$(PASSWORD) ) || (rm -rf $(DIR)/$(TARGET) && false); } + +$(DIR)/testca: + mkdir $(DIR)/testca + cp openssl.cnf $(DIR)/testca/openssl.cnf + { ( cd $(DIR)/testca && \ + mkdir certs private && \ + chmod 700 private && \ + echo 01 > serial && \ + touch index.txt && \ + openssl req -x509 -config openssl.cnf -newkey rsa:2048 -days 365 \ + -out cacert.pem -outform PEM -subj /CN=MyTestCA/L=$$$$/ -nodes && \ + openssl x509 -in cacert.pem -out cacert.cer -outform DER ) \ + || (rm -rf $@ && false); } + +clean: + rm -rf $(DIR)/testca + rm -rf $(DIR)/server + rm -rf $(DIR)/client diff --git a/rabbitmq-server/plugins-src/rabbitmq-test/certs/openssl.cnf b/rabbitmq-server/plugins-src/rabbitmq-test/certs/openssl.cnf new file mode 100644 index 0000000..93ffb2f --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-test/certs/openssl.cnf @@ -0,0 +1,54 @@ +[ ca ] +default_ca = testca + +[ testca ] +dir = . +certificate = $dir/cacert.pem +database = $dir/index.txt +new_certs_dir = $dir/certs +private_key = $dir/private/cakey.pem +serial = $dir/serial + +default_crl_days = 7 +default_days = 365 +default_md = sha1 + +policy = testca_policy +x509_extensions = certificate_extensions + +[ testca_policy ] +commonName = supplied +stateOrProvinceName = optional +countryName = optional +emailAddress = optional +organizationName = optional +organizationalUnitName = optional +domainComponent = optional + +[ certificate_extensions ] +basicConstraints = CA:false + +[ req ] +default_bits = 2048 +default_keyfile = ./private/cakey.pem +default_md = sha1 +prompt = yes +distinguished_name = root_ca_distinguished_name +x509_extensions = root_ca_extensions + +[ root_ca_distinguished_name ] +commonName = hostname + +[ root_ca_extensions ] +basicConstraints = CA:true +keyUsage = keyCertSign, cRLSign + +[ client_ca_extensions ] +basicConstraints = CA:false +keyUsage = digitalSignature +extendedKeyUsage = 1.3.6.1.5.5.7.3.2 + +[ server_ca_extensions ] +basicConstraints = CA:false +keyUsage = keyEncipherment +extendedKeyUsage = 1.3.6.1.5.5.7.3.1 diff --git a/rabbitmq-server/plugins-src/rabbitmq-test/package.mk b/rabbitmq-server/plugins-src/rabbitmq-test/package.mk new file mode 100644 index 0000000..161d016 --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-test/package.mk @@ -0,0 +1,11 @@ +DEPS:=rabbitmq-erlang-client +FILTER:=all +COVER:=false +WITH_BROKER_TEST_COMMANDS:=rabbit_test_runner:run_in_broker(\"$(PACKAGE_DIR)/test/ebin\",\"$(FILTER)\") + +## Require R15B to compile inet_proxy_dist since it requires includes +## introduced there. +ifeq ($(shell erl -noshell -eval 'io:format([list_to_integer(X) || X <- string:tokens(erlang:system_info(version), ".")] >= [5,9]), halt().'),true) +STANDALONE_TEST_COMMANDS:=rabbit_test_runner:run_multi(\"$(UMBRELLA_BASE_DIR)/rabbitmq-server\",\"$(PACKAGE_DIR)/test/ebin\",\"$(FILTER)\",$(COVER),none) +PACKAGE_ERLC_OPTS+=-Derlang_r15b_or_later +endif diff --git a/rabbitmq-server/plugins-src/rabbitmq-test/qpid_config.py b/rabbitmq-server/plugins-src/rabbitmq-test/qpid_config.py new file mode 100644 index 0000000..16388a6 --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-test/qpid_config.py @@ -0,0 +1,26 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +# + +import os + +AMQP_SPEC_DIR=os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), "../rabbitmq-docs/specs") +amqp_spec = os.path.join(AMQP_SPEC_DIR, "amqp.0-10-qpid-errata.xml") +amqp_spec_0_8 = os.path.join(AMQP_SPEC_DIR, "amqp0-8.xml") +amqp_spec_0_9 = os.path.join(AMQP_SPEC_DIR, "amqp0-9.xml") +amqp_spec = 'file://'+os.path.join(AMQP_SPEC_DIR, 'amqp.0-10.xml') diff --git a/rabbitmq-server/plugins-src/rabbitmq-test/qpid_patch b/rabbitmq-server/plugins-src/rabbitmq-test/qpid_patch new file mode 100644 index 0000000..2c4b590 --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-test/qpid_patch @@ -0,0 +1,142 @@ +Index: tests_0-8/basic.py +=================================================================== +--- tests_0-8/basic.py (revision 906960) ++++ tests_0-8/basic.py (working copy) +@@ -98,7 +98,7 @@ + channel.basic_consume(queue="") + self.fail("Expected failure when consuming from unspecified queue") + except Closed, e: +- self.assertConnectionException(530, e.args[0]) ++ self.assertChannelException(404, e.args[0]) + + def test_consume_unique_consumers(self): + """ +Index: tests_0-8/exchange.py +=================================================================== +--- tests_0-8/exchange.py (revision 906960) ++++ tests_0-8/exchange.py (working copy) +@@ -138,8 +138,6 @@ + # Test automatic binding by queue name. + self.queue_declare(queue="d") + self.assertPublishConsume(queue="d", routing_key="d") +- # Test explicit bind to default queue +- self.verifyDirectExchange("") + + + # TODO aconway 2006-09-27: Fill in empty tests: +@@ -318,7 +316,7 @@ + self.channel.exchange_declare(exchange="test_different_declared_type_exchange", type="topic") + self.fail("Expected 530 for redeclaration of exchange with different type.") + except Closed, e: +- self.assertConnectionException(530, e.args[0]) ++ self.assertChannelException(406, e.args[0]) + #cleanup + other = self.connect() + c2 = other.channel(1) +Index: tests_0-8/queue.py +=================================================================== +--- tests_0-8/queue.py (revision 906960) ++++ tests_0-8/queue.py (working copy) +@@ -37,14 +37,10 @@ + channel.basic_publish(exchange="test-exchange", routing_key="key", content=Content("two")) + channel.basic_publish(exchange="test-exchange", routing_key="key", content=Content("three")) + +- #check that the queue now reports 3 messages: +- reply = channel.queue_declare(queue="test-queue") +- self.assertEqual(3, reply.message_count) +- + #now do the purge, then test that three messages are purged and the count drops to 0 + reply = channel.queue_purge(queue="test-queue"); + self.assertEqual(3, reply.message_count) +- reply = channel.queue_declare(queue="test-queue") ++ reply = channel.queue_declare(queue="test-queue", exclusive=True) + self.assertEqual(0, reply.message_count) + + #send a further message and consume it, ensuring that the other messages are really gone +@@ -71,7 +67,7 @@ + channel.queue_purge() + self.fail("Expected failure when purging unspecified queue") + except Closed, e: +- self.assertConnectionException(530, e.args[0]) ++ self.assertChannelException(404, e.args[0]) + + #cleanup + other = self.connect() +@@ -174,11 +170,7 @@ + #check attempted deletion of non-existant queue is handled correctly: + channel = self.client.channel(2) + channel.channel_open() +- try: +- channel.queue_delete(queue="i-dont-exist", if_empty="True") +- self.fail("Expected delete of non-existant queue to fail") +- except Closed, e: +- self.assertChannelException(404, e.args[0]) ++ channel.queue_delete(queue="i-dont-exist", if_empty="True") + + + +Index: qpid/codec.py +=================================================================== +--- qpid/codec.py (revision 906960) ++++ qpid/codec.py (working copy) +@@ -76,6 +76,7 @@ + if not self.types: + self.typecode(ord('S'), "longstr") + self.typecode(ord('I'), "long") ++ self.typecode(ord('t'), "bool") + + def typecode(self, code, type): + self.types[code] = type +@@ -206,6 +207,22 @@ + """ + return self.unpack("!B") + ++ def encode_bool(self, b): ++ """ ++ encodes bool (8 bits) data 't' in network byte order ++ """ ++ ++ if ((b is not True) and (b is not False)): ++ raise ValueError('Valid range of bool is True or False') ++ ++ self.pack("!B", int(b)) ++ ++ def decode_bool(self): ++ """ ++ decodes a bool (8 bits) encoded in network byte order ++ """ ++ return bool(self.unpack("!B")) ++ + def encode_short(self, o): + """ + encodes short (16 bits) data 'o' in network byte order +Index: qpid/testlib.py +=================================================================== +--- qpid/testlib.py (revision 906960) ++++ qpid/testlib.py (working copy) +@@ -67,8 +67,7 @@ + + if not self.client.closed: + self.client.channel(0).connection_close(reply_code=200) +- else: +- self.client.close() ++ self.client.close() + + def connect(self, host=None, port=None, user=None, password=None, tune_params=None): + """Create a new connction, return the Client object""" +Index: qpid_config.py +=================================================================== +--- qpid_config.py (revision 906960) ++++ qpid_config.py (working copy) +@@ -19,7 +19,8 @@ + + import os + +-AMQP_SPEC_DIR=os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), "specs") ++AMQP_SPEC_DIR=os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), "../rabbitmq-docs/specs") + amqp_spec = os.path.join(AMQP_SPEC_DIR, "amqp.0-10-qpid-errata.xml") +-amqp_spec_0_8 = os.path.join(AMQP_SPEC_DIR, "amqp.0-8.xml") +-amqp_spec_0_9 = os.path.join(AMQP_SPEC_DIR, "amqp.0-9.xml") ++amqp_spec_0_8 = os.path.join(AMQP_SPEC_DIR, "amqp0-8.xml") ++amqp_spec_0_9 = os.path.join(AMQP_SPEC_DIR, "amqp0-9.xml") ++amqp_spec = 'file://'+os.path.join(AMQP_SPEC_DIR, 'amqp.0-10.xml') diff --git a/rabbitmq-server/plugins-src/rabbitmq-test/rabbit_failing.txt b/rabbitmq-server/plugins-src/rabbitmq-test/rabbit_failing.txt new file mode 100644 index 0000000..be4eccf --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-test/rabbit_failing.txt @@ -0,0 +1,9 @@ +tests_0-8.basic.BasicTests.test_ack +tests_0-8.basic.BasicTests.test_consume_no_local +tests_0-8.basic.BasicTests.test_qos_prefetch_count +tests_0-8.basic.BasicTests.test_qos_prefetch_size +tests_0-8.broker.BrokerTests.test_basic_delivery_immediate +tests_0-8.broker.BrokerTests.test_channel_flow +tests_0-8.tx.TxTests.test_auto_rollback +tests_0-8.tx.TxTests.test_rollback +tests_0-9.query.* diff --git a/rabbitmq-server/plugins-src/rabbitmq-test/src/inet_proxy_dist.erl b/rabbitmq-server/plugins-src/rabbitmq-test/src/inet_proxy_dist.erl new file mode 100644 index 0000000..847ef2e --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-test/src/inet_proxy_dist.erl @@ -0,0 +1,199 @@ +%% The contents of this file are subject to the Mozilla Public License +%% Version 1.1 (the "License"); you may not use this file except in +%% compliance with the License. You may obtain a copy of the License +%% at http://www.mozilla.org/MPL/ +%% +%% Software distributed under the License is distributed on an "AS IS" +%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See +%% the License for the specific language governing rights and +%% limitations under the License. +%% +%% The Original Code is RabbitMQ. +%% +%% The Initial Developer of the Original Code is GoPivotal, Inc. +%% Copyright (c) 2007-2014 GoPivotal, Inc. All rights reserved. +%% +-module(inet_proxy_dist). + +%% A distribution plugin that uses the usual inet_tcp_dist but allows +%% insertion of a proxy at the receiving end. + +%% inet_*_dist "behaviour" +-export([listen/1, accept/1, accept_connection/5, + setup/5, close/1, select/1, is_node_name/1]). + +%% For copypasta from inet_tcp_dist +-export([do_setup/6]). +-import(error_logger,[error_msg/2]). + +-define(REAL, inet_tcp_dist). + +%%---------------------------------------------------------------------------- + +listen(Name) -> ?REAL:listen(Name). +select(Node) -> ?REAL:select(Node). +accept(Listen) -> ?REAL:accept(Listen). +close(Socket) -> ?REAL:close(Socket). +is_node_name(Node) -> ?REAL:is_node_name(Node). + +accept_connection(AcceptPid, Socket, MyNode, Allowed, SetupTime) -> + ?REAL:accept_connection(AcceptPid, Socket, MyNode, Allowed, SetupTime). + +%% This is copied from inet_tcp_dist, in order to change the +%% output of erl_epmd:port_please/2. + +-ifdef(erlang_r15b_or_later). + +-include_lib("kernel/include/net_address.hrl"). +-include_lib("kernel/include/dist_util.hrl"). + +setup(Node, Type, MyNode, LongOrShortNames,SetupTime) -> + spawn_opt(?MODULE, do_setup, + [self(), Node, Type, MyNode, LongOrShortNames, SetupTime], + [link, {priority, max}]). + +do_setup(Kernel, Node, Type, MyNode, LongOrShortNames,SetupTime) -> + ?trace("~p~n",[{inet_tcp_dist,self(),setup,Node}]), + [Name, Address] = splitnode(Node, LongOrShortNames), + case inet:getaddr(Address, inet) of + {ok, Ip} -> + Timer = dist_util:start_timer(SetupTime), + case erl_epmd:port_please(Name, Ip) of + {port, TcpPort, Version} -> + ?trace("port_please(~p) -> version ~p~n", + [Node,Version]), + dist_util:reset_timer(Timer), + %% Modification START + ProxyPort = case TcpPort >= 25672 andalso TcpPort < 25700 + andalso inet_tcp_proxy:is_enabled() of + true -> TcpPort + 5000; + false -> TcpPort + end, + case inet_tcp:connect(Ip, ProxyPort, + [{active, false}, + {packet,2}]) of + {ok, Socket} -> + {ok, {_, SrcPort}} = inet:sockname(Socket), + ok = inet_tcp_proxy_manager:register( + node(), Node, SrcPort, TcpPort, ProxyPort), + %% Modification END + HSData = #hs_data{ + kernel_pid = Kernel, + other_node = Node, + this_node = MyNode, + socket = Socket, + timer = Timer, + this_flags = 0, + other_version = Version, + f_send = fun inet_tcp:send/2, + f_recv = fun inet_tcp:recv/3, + f_setopts_pre_nodeup = + fun(S) -> + inet:setopts + (S, + [{active, false}, + {packet, 4}, + nodelay()]) + end, + f_setopts_post_nodeup = + fun(S) -> + inet:setopts + (S, + [{active, true}, + {deliver, port}, + {packet, 4}, + nodelay()]) + end, + f_getll = fun inet:getll/1, + f_address = + fun(_,_) -> + #net_address{ + address = {Ip,TcpPort}, + host = Address, + protocol = tcp, + family = inet} + end, + mf_tick = fun inet_tcp_dist:tick/1, + mf_getstat = fun inet_tcp_dist:getstat/1, + request_type = Type + }, + dist_util:handshake_we_started(HSData); + R -> + io:format("~p failed! ~p~n", [node(), R]), + %% Other Node may have closed since + %% port_please ! + ?trace("other node (~p) " + "closed since port_please.~n", + [Node]), + ?shutdown(Node) + end; + _ -> + ?trace("port_please (~p) " + "failed.~n", [Node]), + ?shutdown(Node) + end; + _Other -> + ?trace("inet_getaddr(~p) " + "failed (~p).~n", [Node,_Other]), + ?shutdown(Node) + end. + +%% If Node is illegal terminate the connection setup!! +splitnode(Node, LongOrShortNames) -> + case split_node(atom_to_list(Node), $@, []) of + [Name|Tail] when Tail =/= [] -> + Host = lists:append(Tail), + case split_node(Host, $., []) of + [_] when LongOrShortNames =:= longnames -> + error_msg("** System running to use " + "fully qualified " + "hostnames **~n" + "** Hostname ~s is illegal **~n", + [Host]), + ?shutdown(Node); + L when length(L) > 1, LongOrShortNames =:= shortnames -> + error_msg("** System NOT running to use fully qualified " + "hostnames **~n" + "** Hostname ~s is illegal **~n", + [Host]), + ?shutdown(Node); + _ -> + [Name, Host] + end; + [_] -> + error_msg("** Nodename ~p illegal, no '@' character **~n", + [Node]), + ?shutdown(Node); + _ -> + error_msg("** Nodename ~p illegal **~n", [Node]), + ?shutdown(Node) + end. + +split_node([Chr|T], Chr, Ack) -> [lists:reverse(Ack)|split_node(T, Chr, [])]; +split_node([H|T], Chr, Ack) -> split_node(T, Chr, [H|Ack]); +split_node([], _, Ack) -> [lists:reverse(Ack)]. + +%% we may not always want the nodelay behaviour +%% for performance reasons + +nodelay() -> + case application:get_env(kernel, dist_nodelay) of + undefined -> + {nodelay, true}; + {ok, true} -> + {nodelay, true}; + {ok, false} -> + {nodelay, false}; + _ -> + {nodelay, true} + end. + +-else. + +setup(_Node, _Type, _MyNode, _LongOrShortNames, _SetupTime) -> + exit(erlang_r15b_required). + +do_setup(_Kernel, _Node, _Type, _MyNode, _LongOrShortNames, _SetupTime) -> + exit(erlang_r15b_required). + +-endif. diff --git a/rabbitmq-server/plugins-src/rabbitmq-test/src/inet_tcp_proxy.erl b/rabbitmq-server/plugins-src/rabbitmq-test/src/inet_tcp_proxy.erl new file mode 100644 index 0000000..28d58e0 --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-test/src/inet_tcp_proxy.erl @@ -0,0 +1,106 @@ +%% The contents of this file are subject to the Mozilla Public License +%% Version 1.1 (the "License"); you may not use this file except in +%% compliance with the License. You may obtain a copy of the License +%% at http://www.mozilla.org/MPL/ +%% +%% Software distributed under the License is distributed on an "AS IS" +%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See +%% the License for the specific language governing rights and +%% limitations under the License. +%% +%% The Original Code is RabbitMQ. +%% +%% The Initial Developer of the Original Code is GoPivotal, Inc. +%% Copyright (c) 2007-2014 GoPivotal, Inc. All rights reserved. +%% +-module(inet_tcp_proxy). + +%% A TCP proxy for insertion into the Erlang distribution mechanism, +%% which allows us to simulate network partitions. + +-export([start/0, reconnect/1, is_enabled/0, allow/1, block/1]). + +-define(TABLE, ?MODULE). + +%% This can't start_link because there's no supervision hierarchy we +%% can easily fit it into (we need to survive all application +%% restarts). So we have to do some horrible error handling. + +start() -> + spawn(error_handler(fun go/0)), + ok. + +reconnect(Nodes) -> + [erlang:disconnect_node(N) || N <- Nodes, N =/= node()], + ok. + +is_enabled() -> + lists:member(?TABLE, ets:all()). + +allow(Node) -> ets:delete(?TABLE, Node). +block(Node) -> ets:insert(?TABLE, {Node, block}). + +%%---------------------------------------------------------------------------- + +error_handler(Thunk) -> + fun () -> + try + Thunk() + catch _:{{nodedown, _}, _} -> + %% The only other node we ever talk to is the test + %% runner; if that's down then the test is nearly + %% over; die quietly. + ok; + _:X -> + io:format(user, "TCP proxy died with ~p~n At ~p~n", + [X, erlang:get_stacktrace()]), + erlang:halt(1) + end + end. + +go() -> + ets:new(?TABLE, [public, named_table]), + {ok, Port} = application:get_env(kernel, inet_dist_listen_min), + ProxyPort = Port + 5000, + {ok, Sock} = gen_tcp:listen(ProxyPort, [inet, + {reuseaddr, true}]), + accept_loop(Sock, Port). + +accept_loop(ListenSock, Port) -> + {ok, Sock} = gen_tcp:accept(ListenSock), + Proxy = spawn(error_handler(fun() -> run_it(Sock, Port) end)), + ok = gen_tcp:controlling_process(Sock, Proxy), + accept_loop(ListenSock, Port). + +run_it(SockIn, Port) -> + case {inet:peername(SockIn), inet:sockname(SockIn)} of + {{ok, {_Addr, SrcPort}}, {ok, {Addr, _OtherPort}}} -> + {ok, Remote, This} = inet_tcp_proxy_manager:lookup(SrcPort), + case node() of + This -> ok; + _ -> exit({not_me, node(), This}) + end, + {ok, SockOut} = gen_tcp:connect(Addr, Port, [inet]), + run_loop({SockIn, SockOut}, Remote, []); + _ -> + ok + end. + +run_loop(Sockets, RemoteNode, Buf0) -> + Block = [{RemoteNode, block}] =:= ets:lookup(?TABLE, RemoteNode), + receive + {tcp, Sock, Data} -> + Buf = [Data | Buf0], + case Block of + false -> gen_tcp:send(other(Sock, Sockets), lists:reverse(Buf)), + run_loop(Sockets, RemoteNode, []); + true -> run_loop(Sockets, RemoteNode, Buf) + end; + {tcp_closed, Sock} -> + gen_tcp:close(other(Sock, Sockets)); + X -> + exit({weirdness, X}) + end. + +other(A, {A, B}) -> B; +other(B, {A, B}) -> A. diff --git a/rabbitmq-server/plugins-src/rabbitmq-test/src/inet_tcp_proxy_manager.erl b/rabbitmq-server/plugins-src/rabbitmq-test/src/inet_tcp_proxy_manager.erl new file mode 100644 index 0000000..a79ea9f --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-test/src/inet_tcp_proxy_manager.erl @@ -0,0 +1,107 @@ +%% The contents of this file are subject to the Mozilla Public License +%% Version 1.1 (the "License"); you may not use this file except in +%% compliance with the License. You may obtain a copy of the License +%% at http://www.mozilla.org/MPL/ +%% +%% Software distributed under the License is distributed on an "AS IS" +%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See +%% the License for the specific language governing rights and +%% limitations under the License. +%% +%% The Original Code is RabbitMQ. +%% +%% The Initial Developer of the Original Code is GoPivotal, Inc. +%% Copyright (c) 2007-2014 GoPivotal, Inc. All rights reserved. +%% +-module(inet_tcp_proxy_manager). + +%% The TCP proxies need to decide whether to block based on the node +%% they're running on, and the node connecting to them. The trouble +%% is, they don't have an easy way to determine the latter. Therefore +%% when A connects to B we register the source port used by A here, so +%% that B can later look it up and find out who A is without having to +%% sniff the distribution protocol. +%% +%% That does unfortunately mean that we need a central control +%% thing. We assume here it's running on the node called +%% 'standalone_test' since that's where tests are orchestrated from. +%% +%% Yes, this leaks. For its intended lifecycle, that's fine. + +-behaviour(gen_server). + +-export([start_link/0, register/5, lookup/1]). + +-export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, + code_change/3]). + +-define(NODE, standalone_test). + +-record(state, {ports, pending}). + +start_link() -> + Node = node(), + Node = controller_node(), + gen_server:start_link({local, ?MODULE}, ?MODULE, [], []). + +register(_From, _To, _SrcPort, Port, Port) -> + %% No proxy, don't register + ok; +register(From, To, SrcPort, _Port, _ProxyPort) -> + gen_server:call(name(), {register, From, To, SrcPort}, infinity). + +lookup(SrcPort) -> + gen_server:call(name(), {lookup, SrcPort}, infinity). + +controller_node() -> + rabbit_nodes:make(atom_to_list(?NODE)). + +name() -> + {?MODULE, controller_node()}. + +%%---------------------------------------------------------------------------- + +init([]) -> + net_kernel:monitor_nodes(true), + {ok, #state{ports = dict:new(), + pending = []}}. + +handle_call({register, FromNode, ToNode, SrcPort}, _From, + State = #state{ports = Ports, + pending = Pending}) -> + {Notify, Pending2} = + lists:partition(fun ({P, _}) -> P =:= SrcPort end, Pending), + [gen_server:reply(From, {ok, FromNode, ToNode}) || {_, From} <- Notify], + {reply, ok, + State#state{ports = dict:store(SrcPort, {FromNode, ToNode}, Ports), + pending = Pending2}}; + +handle_call({lookup, SrcPort}, From, + State = #state{ports = Ports, pending = Pending}) -> + case dict:find(SrcPort, Ports) of + {ok, {FromNode, ToNode}} -> + {reply, {ok, FromNode, ToNode}, State}; + error -> + {noreply, State#state{pending = [{SrcPort, From} | Pending]}} + end; + +handle_call(_Req, _From, State) -> + {reply, unknown_request, State}. + +handle_cast(_C, State) -> + {noreply, State}. + +handle_info({nodedown, Node}, State = #state{ports = Ports}) -> + Ports1 = dict:filter( + fun (_, {From, To}) -> + Node =/= From andalso Node =/= To + end, Ports), + {noreply, State#state{ports = Ports1}}; + +handle_info(_I, State) -> + {noreply, State}. + +terminate(_Reason, _State) -> + ok. + +code_change(_, State, _) -> {ok, State}. diff --git a/rabbitmq-server/plugins-src/rabbitmq-test/src/rabbit_ha_test_consumer.erl b/rabbitmq-server/plugins-src/rabbitmq-test/src/rabbit_ha_test_consumer.erl new file mode 100644 index 0000000..f11d8d4 --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-test/src/rabbit_ha_test_consumer.erl @@ -0,0 +1,114 @@ +%% The contents of this file are subject to the Mozilla Public License +%% Version 1.1 (the "License"); you may not use this file except in +%% compliance with the License. You may obtain a copy of the License +%% at http://www.mozilla.org/MPL/ +%% +%% Software distributed under the License is distributed on an "AS IS" +%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See +%% the License for the specific language governing rights and +%% limitations under the License. +%% +%% The Original Code is RabbitMQ. +%% +%% The Initial Developer of the Original Code is GoPivotal, Inc. +%% Copyright (c) 2007-2014 GoPivotal, Inc. All rights reserved. +%% +-module(rabbit_ha_test_consumer). + +-include_lib("amqp_client/include/amqp_client.hrl"). + +-export([await_response/1, create/5, start/6]). + +await_response(ConsumerPid) -> + case receive {ConsumerPid, Response} -> Response end of + {error, Reason} -> erlang:error(Reason); + ok -> ok + end. + +create(Channel, Queue, TestPid, CancelOnFailover, ExpectingMsgs) -> + ConsumerPid = spawn_link(?MODULE, start, + [TestPid, Channel, Queue, CancelOnFailover, + ExpectingMsgs + 1, ExpectingMsgs]), + amqp_channel:subscribe( + Channel, consume_method(Queue, CancelOnFailover), ConsumerPid), + ConsumerPid. + +start(TestPid, Channel, Queue, CancelOnFailover, LowestSeen, MsgsToConsume) -> + error_logger:info_msg("consumer ~p on ~p awaiting ~w messages " + "(lowest seen = ~w, cancel-on-failover = ~w)~n", + [self(), Channel, MsgsToConsume, LowestSeen, + CancelOnFailover]), + run(TestPid, Channel, Queue, CancelOnFailover, LowestSeen, MsgsToConsume). + +run(TestPid, _Channel, _Queue, _CancelOnFailover, _LowestSeen, 0) -> + consumer_reply(TestPid, ok); +run(TestPid, Channel, Queue, CancelOnFailover, LowestSeen, MsgsToConsume) -> + receive + #'basic.consume_ok'{} -> + run(TestPid, Channel, Queue, + CancelOnFailover, LowestSeen, MsgsToConsume); + {Delivery = #'basic.deliver'{ redelivered = Redelivered }, + #amqp_msg{payload = Payload}} -> + MsgNum = list_to_integer(binary_to_list(Payload)), + + ack(Delivery, Channel), + + %% we can receive any message we've already seen and, + %% because of the possibility of multiple requeuings, we + %% might see these messages in any order. If we are seeing + %% a message again, we don't decrement the MsgsToConsume + %% counter. + if + MsgNum + 1 == LowestSeen -> + run(TestPid, Channel, Queue, + CancelOnFailover, MsgNum, MsgsToConsume - 1); + MsgNum >= LowestSeen -> + error_logger:info_msg( + "consumer ~p on ~p ignoring redeliverd msg ~p~n", + [self(), Channel, MsgNum]), + true = Redelivered, %% ASSERTION + run(TestPid, Channel, Queue, + CancelOnFailover, LowestSeen, MsgsToConsume); + true -> + %% We received a message we haven't seen before, + %% but it is not the next message in the expected + %% sequence. + consumer_reply(TestPid, + {error, {unexpected_message, MsgNum}}) + end; + #'basic.cancel'{} when CancelOnFailover -> + error_logger:info_msg("consumer ~p on ~p received basic.cancel: " + "resubscribing to ~p on ~p~n", + [self(), Channel, Queue, Channel]), + resubscribe(TestPid, Channel, Queue, CancelOnFailover, + LowestSeen, MsgsToConsume); + #'basic.cancel'{} -> + exit(cancel_received_without_cancel_on_failover) + end. + +%% +%% Private API +%% + +resubscribe(TestPid, Channel, Queue, CancelOnFailover, LowestSeen, + MsgsToConsume) -> + amqp_channel:subscribe( + Channel, consume_method(Queue, CancelOnFailover), self()), + ok = receive #'basic.consume_ok'{} -> ok + end, + error_logger:info_msg("re-subscripting consumer ~p on ~p complete " + "(received basic.consume_ok)", + [self(), Channel]), + start(TestPid, Channel, Queue, CancelOnFailover, LowestSeen, MsgsToConsume). + +consume_method(Queue, CancelOnFailover) -> + Args = [{<<"x-cancel-on-ha-failover">>, bool, CancelOnFailover}], + #'basic.consume'{queue = Queue, + arguments = Args}. + +ack(#'basic.deliver'{delivery_tag = DeliveryTag}, Channel) -> + amqp_channel:call(Channel, #'basic.ack'{delivery_tag = DeliveryTag}), + ok. + +consumer_reply(TestPid, Reply) -> + TestPid ! {self(), Reply}. diff --git a/rabbitmq-server/plugins-src/rabbitmq-test/src/rabbit_ha_test_producer.erl b/rabbitmq-server/plugins-src/rabbitmq-test/src/rabbit_ha_test_producer.erl new file mode 100644 index 0000000..f3070fe --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-test/src/rabbit_ha_test_producer.erl @@ -0,0 +1,119 @@ +%% The contents of this file are subject to the Mozilla Public License +%% Version 1.1 (the "License"); you may not use this file except in +%% compliance with the License. You may obtain a copy of the License +%% at http://www.mozilla.org/MPL/ +%% +%% Software distributed under the License is distributed on an "AS IS" +%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See +%% the License for the specific language governing rights and +%% limitations under the License. +%% +%% The Original Code is RabbitMQ. +%% +%% The Initial Developer of the Original Code is GoPivotal, Inc. +%% Copyright (c) 2007-2014 GoPivotal, Inc. All rights reserved. +%% +-module(rabbit_ha_test_producer). + +-export([await_response/1, start/5, create/5]). + +-include_lib("amqp_client/include/amqp_client.hrl"). + +await_response(ProducerPid) -> + error_logger:info_msg("waiting for producer pid ~p~n", [ProducerPid]), + case receive {ProducerPid, Response} -> Response end of + ok -> ok; + {error, _} = Else -> exit(Else); + Else -> exit({weird_response, Else}) + end. + +create(Channel, Queue, TestPid, Confirm, MsgsToSend) -> + ProducerPid = spawn_link(?MODULE, start, [Channel, Queue, TestPid, + Confirm, MsgsToSend]), + receive + {ProducerPid, started} -> ProducerPid + end. + +start(Channel, Queue, TestPid, Confirm, MsgsToSend) -> + ConfirmState = + case Confirm of + true -> amqp_channel:register_confirm_handler(Channel, self()), + #'confirm.select_ok'{} = + amqp_channel:call(Channel, #'confirm.select'{}), + gb_trees:empty(); + false -> none + end, + TestPid ! {self(), started}, + error_logger:info_msg("publishing ~w msgs on ~p~n", [MsgsToSend, Channel]), + producer(Channel, Queue, TestPid, ConfirmState, MsgsToSend). + +%% +%% Private API +%% + +producer(_Channel, _Queue, TestPid, none, 0) -> + TestPid ! {self(), ok}; +producer(Channel, _Queue, TestPid, ConfirmState, 0) -> + error_logger:info_msg("awaiting confirms on channel ~p~n", [Channel]), + Msg = case drain_confirms(no_nacks, ConfirmState) of + no_nacks -> ok; + nacks -> {error, received_nacks}; + {Nacks, CS} -> {error, {missing_confirms, Nacks, + lists:sort(gb_trees:keys(CS))}} + end, + TestPid ! {self(), Msg}; + +producer(Channel, Queue, TestPid, ConfirmState, MsgsToSend) -> + Method = #'basic.publish'{exchange = <<"">>, + routing_key = Queue, + mandatory = false, + immediate = false}, + + ConfirmState1 = maybe_record_confirm(ConfirmState, Channel, MsgsToSend), + + amqp_channel:call(Channel, Method, + #amqp_msg{props = #'P_basic'{delivery_mode = 2}, + payload = list_to_binary( + integer_to_list(MsgsToSend))}), + + producer(Channel, Queue, TestPid, ConfirmState1, MsgsToSend - 1). + +maybe_record_confirm(none, _, _) -> + none; +maybe_record_confirm(ConfirmState, Channel, MsgsToSend) -> + SeqNo = amqp_channel:next_publish_seqno(Channel), + gb_trees:insert(SeqNo, MsgsToSend, ConfirmState). + +drain_confirms(Nacks, ConfirmState) -> + case gb_trees:is_empty(ConfirmState) of + true -> Nacks; + false -> receive + #'basic.ack'{delivery_tag = DeliveryTag, + multiple = IsMulti} -> + drain_confirms(Nacks, + delete_confirms(DeliveryTag, IsMulti, + ConfirmState)); + #'basic.nack'{delivery_tag = DeliveryTag, + multiple = IsMulti} -> + drain_confirms(nacks, + delete_confirms(DeliveryTag, IsMulti, + ConfirmState)) + after + 60000 -> {Nacks, ConfirmState} + end + end. + +delete_confirms(DeliveryTag, false, ConfirmState) -> + gb_trees:delete(DeliveryTag, ConfirmState); +delete_confirms(DeliveryTag, true, ConfirmState) -> + multi_confirm(DeliveryTag, ConfirmState). + +multi_confirm(DeliveryTag, ConfirmState) -> + case gb_trees:is_empty(ConfirmState) of + true -> ConfirmState; + false -> {Key, _, ConfirmState1} = gb_trees:take_smallest(ConfirmState), + case Key =< DeliveryTag of + true -> multi_confirm(DeliveryTag, ConfirmState1); + false -> ConfirmState + end + end. diff --git a/rabbitmq-server/plugins-src/rabbitmq-test/src/rabbit_test_configs.erl b/rabbitmq-server/plugins-src/rabbitmq-test/src/rabbit_test_configs.erl new file mode 100644 index 0000000..f286733 --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-test/src/rabbit_test_configs.erl @@ -0,0 +1,279 @@ +%% The contents of this file are subject to the Mozilla Public License +%% Version 1.1 (the "License"); you may not use this file except in +%% compliance with the License. You may obtain a copy of the License +%% at http://www.mozilla.org/MPL/ +%% +%% Software distributed under the License is distributed on an "AS IS" +%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See +%% the License for the specific language governing rights and +%% limitations under the License. +%% +%% The Original Code is RabbitMQ. +%% +%% The Initial Developer of the Original Code is GoPivotal, Inc. +%% Copyright (c) 2007-2014 GoPivotal, Inc. All rights reserved. +%% +-module(rabbit_test_configs). + +-include_lib("amqp_client/include/amqp_client.hrl"). + +-export([enable_plugins/1]). +-export([cluster/2, cluster_ab/1, cluster_abc/1, start_ab/1, start_abc/1]). +-export([start_connections/1, build_cluster/1]). +-export([ha_policy_all/1, ha_policy_two_pos/1]). +-export([start_nodes/2, start_nodes/3, add_to_cluster/2, + rabbitmqctl/2, rabbitmqctl_fail/2]). +-export([stop_nodes/1, start_node/1, stop_node/1, kill_node/1, restart_node/1, + start_node_fail/1, execute/1]). +-export([cover_work_factor/2]). + +-import(rabbit_test_util, [set_ha_policy/3, set_ha_policy/4, a2b/1]). +-import(rabbit_misc, [pget/2, pget/3]). + +-define(INITIAL_KEYS, [cover, base, server, plugins]). +-define(NON_RUNNING_KEYS, ?INITIAL_KEYS ++ [nodename, port, mnesia_dir]). + +cluster_ab(InitialCfg) -> cluster(InitialCfg, [a, b]). +cluster_abc(InitialCfg) -> cluster(InitialCfg, [a, b, c]). +start_ab(InitialCfg) -> start_nodes(InitialCfg, [a, b]). +start_abc(InitialCfg) -> start_nodes(InitialCfg, [a, b, c]). + +cluster(InitialCfg, NodeNames) -> + start_connections(build_cluster(start_nodes(InitialCfg, NodeNames))). + +start_nodes(InitialCfg, NodeNames) -> + start_nodes(InitialCfg, NodeNames, 5672). + +start_nodes(InitialCfg0, NodeNames, FirstPort) -> + {ok, Already0} = net_adm:names(), + Already = [list_to_atom(N) || {N, _P} <- Already0], + [check_node_not_running(Node, Already) || Node <- NodeNames], + Ports = lists:seq(FirstPort, length(NodeNames) + FirstPort - 1), + InitialCfgs = case InitialCfg0 of + [{_, _}|_] -> [InitialCfg0 || _ <- NodeNames]; + _ -> InitialCfg0 + end, + Nodes = [[{nodename, N}, {port, P}, + {mnesia_dir, rabbit_misc:format("rabbitmq-~s-mnesia", [N])} | + strip_non_initial(Cfg)] + || {N, P, Cfg} <- lists:zip3(NodeNames, Ports, InitialCfgs)], + [start_node(Node) || Node <- Nodes]. + +check_node_not_running(Node, Already) -> + case lists:member(Node, Already) of + true -> exit({node_already_running, Node}); + false -> ok + end. + +strip_non_initial(Cfg) -> + [{K, V} || {K, V} <- Cfg, lists:member(K, ?INITIAL_KEYS)]. + +strip_running(Cfg) -> + [{K, V} || {K, V} <- Cfg, lists:member(K, ?NON_RUNNING_KEYS)]. + +enable_plugins(Cfg) -> + enable_plugins(pget(plugins, Cfg), pget(server, Cfg), Cfg). + +enable_plugins(none, _Server, _Cfg) -> ok; +enable_plugins(_Dir, Server, Cfg) -> + R = execute(Cfg, Server ++ "/scripts/rabbitmq-plugins list -m"), + Plugins = string:join(string:tokens(R, "\n"), " "), + execute(Cfg, {Server ++ "/scripts/rabbitmq-plugins set --offline ~s", + [Plugins]}), + ok. + +start_node(Cfg0) -> + Node = rabbit_nodes:make(pget(nodename, Cfg0)), + Cfg = [{node, Node} | Cfg0], + Server = pget(server, Cfg), + Linked = execute_bg(Cfg, Server ++ "/scripts/rabbitmq-server"), + rabbitmqctl(Cfg, {"wait ~s", [pid_file(Cfg)]}), + OSPid = rpc:call(Node, os, getpid, []), + %% The cover system thinks all nodes with the same name are the + %% same node and will automaticaly re-establish cover as soon as + %% we see them, so we only want to start cover once per node name + %% for the entire test run. + case {pget(cover, Cfg), lists:member(Node, cover:which_nodes())} of + {true, false} -> cover:start([Node]); + _ -> ok + end, + [{os_pid, OSPid}, + {linked_pid, Linked} | Cfg]. + +start_node_fail(Cfg0) -> + Node = rabbit_nodes:make(pget(nodename, Cfg0)), + Cfg = [{node, Node}, {acceptable_exit_codes, lists:seq(1, 255)} | Cfg0], + Server = pget(server, Cfg), + execute(Cfg, Server ++ "/scripts/rabbitmq-server"), + ok. + +build_cluster([First | Rest]) -> + add_to_cluster([First], Rest). + +add_to_cluster([First | _] = Existing, New) -> + [cluster_with(First, Node) || Node <- New], + Existing ++ New. + +cluster_with(Cfg, NewCfg) -> + Node = pget(node, Cfg), + rabbitmqctl(NewCfg, stop_app), + rabbitmqctl(NewCfg, {"join_cluster ~s", [Node]}), + rabbitmqctl(NewCfg, start_app). + +rabbitmqctl(Cfg, Str) -> + Node = pget(node, Cfg), + Server = pget(server, Cfg), + Cmd = case Node of + undefined -> {"~s", [fmt(Str)]}; + _ -> {"-n ~s ~s", [Node, fmt(Str)]} + end, + execute(Cfg, {Server ++ "/scripts/rabbitmqctl ~s", [fmt(Cmd)]}). + +rabbitmqctl_fail(Cfg, Str) -> + rabbitmqctl([{acceptable_exit_codes, lists:seq(1, 255)} | Cfg], Str). + +ha_policy_all([Cfg | _] = Cfgs) -> + set_ha_policy(Cfg, <<".*">>, <<"all">>), + Cfgs. + +ha_policy_two_pos([Cfg | _] = Cfgs) -> + Members = [a2b(pget(node, C)) || C <- Cfgs], + TwoNodes = [M || M <- lists:sublist(Members, 2)], + set_ha_policy(Cfg, <<"^ha.two.">>, {<<"nodes">>, TwoNodes}, + [{<<"ha-promote-on-shutdown">>, <<"always">>}]), + set_ha_policy(Cfg, <<"^ha.auto.">>, {<<"nodes">>, TwoNodes}, + [{<<"ha-sync-mode">>, <<"automatic">>}, + {<<"ha-promote-on-shutdown">>, <<"always">>}]), + Cfgs. + +start_connections(Nodes) -> [start_connection(Node) || Node <- Nodes]. + +start_connection(Cfg) -> + Port = pget(port, Cfg), + {ok, Conn} = amqp_connection:start(#amqp_params_network{port = Port}), + {ok, Ch} = amqp_connection:open_channel(Conn), + [{connection, Conn}, {channel, Ch} | Cfg]. + +stop_nodes(Nodes) -> [stop_node(Node) || Node <- Nodes]. + +stop_node(Cfg) -> + maybe_flush_cover(Cfg), + catch rabbitmqctl(Cfg, {"stop ~s", [pid_file(Cfg)]}), + strip_running(Cfg). + +kill_node(Cfg) -> + maybe_flush_cover(Cfg), + OSPid = pget(os_pid, Cfg), + catch execute(Cfg, {"kill -9 ~s", [OSPid]}), + await_os_pid_death(OSPid), + strip_running(Cfg). + +await_os_pid_death(OSPid) -> + case rabbit_misc:is_os_process_alive(OSPid) of + true -> timer:sleep(100), + await_os_pid_death(OSPid); + false -> ok + end. + +restart_node(Cfg) -> + start_node(stop_node(Cfg)). + +maybe_flush_cover(Cfg) -> + case pget(cover, Cfg) of + true -> cover:flush(pget(node, Cfg)); + false -> ok + end. + +%% Cover slows things down enough that if we are sending messages in +%% bulk, we want to send fewer or we'll be here all day... +cover_work_factor(Without, Cfg) -> + case pget(cover, Cfg) of + true -> trunc(Without * 0.1); + false -> Without + end. + +%%---------------------------------------------------------------------------- + +execute(Cmd) -> + execute([], Cmd, [0]). + +execute(Cfg, Cmd) -> + %% code 137 -> killed with SIGKILL which we do in some tests + execute(environment(Cfg), Cmd, pget(acceptable_exit_codes, Cfg, [0, 137])). + +execute(Env0, Cmd0, AcceptableExitCodes) -> + Env = [{"RABBITMQ_" ++ K, fmt(V)} || {K, V} <- Env0], + Cmd = fmt(Cmd0), + error_logger:info_msg("Invoking '~s'~n", [Cmd]), + Port = erlang:open_port( + {spawn, "/usr/bin/env sh -c \"" ++ Cmd ++ "\""}, + [{env, Env}, exit_status, + stderr_to_stdout, use_stdio]), + port_receive_loop(Port, "", AcceptableExitCodes). + +environment(Cfg) -> + Nodename = pget(nodename, Cfg), + Plugins = pget(plugins, Cfg), + case Nodename of + undefined -> + plugins_env(Plugins); + _ -> + Port = pget(port, Cfg), + Base = pget(base, Cfg), + Server = pget(server, Cfg), + [{"MNESIA_DIR", {"~s/~s", [Base, pget(mnesia_dir, Cfg)]}}, + {"PLUGINS_EXPAND_DIR", {"~s/~s-plugins-expand", [Base, Nodename]}}, + {"LOG_BASE", {"~s", [Base]}}, + {"NODENAME", {"~s", [Nodename]}}, + {"NODE_PORT", {"~B", [Port]}}, + {"PID_FILE", pid_file(Cfg)}, + {"CONFIG_FILE", "/some/path/which/does/not/exist"}, + {"ALLOW_INPUT", "1"}, %% Needed to make it close on exit + %% Bit of a hack - only needed for mgmt tests. + {"SERVER_START_ARGS", + {"-rabbitmq_management listener [{port,1~B}]", [Port]}}, + {"SERVER_ERL_ARGS", + %% Next two lines are defaults + {"+K true +A30 +P 1048576 " + "-kernel inet_default_connect_options [{nodelay,true}] " + %% Some tests need to be able to make distribution unhappy + "-pa ~s/../rabbitmq-test/ebin " + "-proto_dist inet_proxy", [Server]}} + | plugins_env(Plugins)] + end. + +plugins_env(none) -> + [{"ENABLED_PLUGINS_FILE", "/does-not-exist"}]; +plugins_env(Dir) -> + [{"PLUGINS_DIR", {"~s/plugins", [Dir]}}, + {"PLUGINS_EXPAND_DIR", {"~s/expand", [Dir]}}, + {"ENABLED_PLUGINS_FILE", {"~s/enabled_plugins", [Dir]}}]. + +pid_file(Cfg) -> + rabbit_misc:format("~s/~s.pid", [pget(base, Cfg), pget(nodename, Cfg)]). + +port_receive_loop(Port, Stdout, AcceptableExitCodes) -> + receive + {Port, {exit_status, X}} -> + Fmt = "Command exited with code ~p~nStdout: ~s~n", + Args = [X, Stdout], + case lists:member(X, AcceptableExitCodes) of + true -> error_logger:info_msg(Fmt, Args), + Stdout; + false -> error_logger:error_msg(Fmt, Args), + exit({exit_status, X, AcceptableExitCodes, Stdout}) + end; + {Port, {data, Out}} -> + port_receive_loop(Port, Stdout ++ Out, AcceptableExitCodes) + end. + +execute_bg(Cfg, Cmd) -> + spawn_link(fun () -> + execute(Cfg, Cmd), + {links, Links} = process_info(self(), links), + [unlink(L) || L <- Links] + end). + +fmt({Fmt, Args}) -> rabbit_misc:format(Fmt, Args); +fmt(Str) -> Str. + diff --git a/rabbitmq-server/plugins-src/rabbitmq-test/src/rabbit_test_runner.erl b/rabbitmq-server/plugins-src/rabbitmq-test/src/rabbit_test_runner.erl new file mode 100644 index 0000000..d0df292 --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-test/src/rabbit_test_runner.erl @@ -0,0 +1,230 @@ +%% The contents of this file are subject to the Mozilla Public License +%% Version 1.1 (the "License"); you may not use this file except in +%% compliance with the License. You may obtain a copy of the License at +%% http://www.mozilla.org/MPL/ +%% +%% Software distributed under the License is distributed on an "AS IS" +%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the +%% License for the specific language governing rights and limitations +%% under the License. +%% +%% The Original Code is RabbitMQ +%% +%% The Initial Developer of the Original Code is GoPivotal, Inc. +%% Copyright (c) 2010-2014 GoPivotal, Inc. All rights reserved. +%% + +-module(rabbit_test_runner). + +-include_lib("kernel/include/file.hrl"). + +-define(TIMEOUT, 600). + +-import(rabbit_misc, [pget/2]). + +-export([run_in_broker/2, run_multi/5]). + +run_in_broker(Dir, Filter) -> + add_server_test_ebin_dir(), + io:format("~nIn-broker tests~n================~n~n", []), + eunit:test(make_tests_single(Dir, Filter, ?TIMEOUT), []). + +run_multi(ServerDir, Dir, Filter, Cover, PluginsDir) -> + io:format("~nMulti-node tests~n================~n~n", []), + %% Umbrella does not give us -sname + net_kernel:start([?MODULE, shortnames]), + inets:start(), %% Used by HTTP tests + error_logger:tty(false), + case Cover of + true -> io:format("Cover compiling..."), + cover:start(), + ok = rabbit_misc:enable_cover(["../rabbitmq-server/"]), + io:format(" done.~n~n"); + false -> ok + end, + R = eunit:test(make_tests_multi( + ServerDir, Dir, Filter, Cover, PluginsDir, ?TIMEOUT), []), + case Cover of + true -> io:format("~nCover reporting..."), + ok = rabbit_misc:report_cover(), + io:format(" done.~n~n"); + false -> ok + end, + R. + +make_tests_single(Dir, Filter, Timeout) -> + {Filtered, AllCount, Width} = find_tests(Dir, Filter, "_test"), + io:format("Running ~B of ~B tests; FILTER=~s~n~n", + [length(Filtered), AllCount, Filter]), + [make_test_single(M, FWith, F, ShowHeading, Timeout, Width) + || {M, FWith, F, ShowHeading} <- annotate_show_heading(Filtered)]. + +make_tests_multi(ServerDir, Dir, Filter, Cover, PluginsDir, Timeout) -> + {Filtered, AllCount, Width} = find_tests(Dir, Filter, "_with"), + io:format("Running ~B of ~B tests; FILTER=~s; COVER=~s~n~n", + [length(Filtered), AllCount, Filter, Cover]), + Cfg = [{cover, Cover}, + {base, basedir() ++ "/nodes"}, + {server, ServerDir}, + {plugins, PluginsDir}], + rabbit_test_configs:enable_plugins(Cfg), + [make_test_multi(M, FWith, F, ShowHeading, Timeout, Width, Cfg) + || {M, FWith, F, ShowHeading} <- annotate_show_heading(Filtered)]. + +find_tests(Dir, Filter, Suffix) -> + All = [{M, FWith, F} || + M <- modules(Dir), + {FWith, _Arity} <- proplists:get_value(exports, M:module_info()), + string:right(atom_to_list(FWith), length(Suffix)) =:= Suffix, + F <- [truncate_function_name(FWith, length(Suffix))]], + Filtered = [Test || {M, _FWith, F} = Test <- All, + should_run(M, F, Filter)], + Width = case Filtered of + [] -> 0; + _ -> lists:max([atom_length(F) || {_, _, F} <- Filtered]) + end, + {Filtered, length(All), Width}. + +make_test_single(M, FWith, F, ShowHeading, Timeout, Width) -> + {timeout, + Timeout, + fun () -> + maybe_print_heading(M, ShowHeading), + io:format(user, "~s [running]", [name(F, Width)]), + M:FWith(), + io:format(user, " [PASSED].~n", []) + end}. + +make_test_multi(M, FWith, F, ShowHeading, Timeout, Width, InitialCfg) -> + {setup, + fun () -> + maybe_print_heading(M, ShowHeading), + io:format(user, "~s [setup]", [name(F, Width)]), + setup_error_logger(M, F, basedir()), + recursive_delete(pget(base, InitialCfg)), + try + apply_config(M:FWith(), InitialCfg) + catch + error:{Type, Error, Cfg, Stack} -> + case Cfg of + InitialCfg -> ok; %% [0] + _ -> rabbit_test_configs:stop_nodes(Cfg) + end, + exit({Type, Error, Stack}) + end + end, + fun (Nodes) -> + rabbit_test_configs:stop_nodes(Nodes), + %% Partition tests change this, let's revert + net_kernel:set_net_ticktime(60, 1), + io:format(user, ".~n", []) + end, + fun (Nodes) -> + [{timeout, + Timeout, + fun () -> + [link(pget(linked_pid, N)) || N <- Nodes], + io:format(user, " [running]", []), + %%try + M:F(Nodes), + io:format(user, " [PASSED]", []) + %% catch + %% Type:Reason -> + %% io:format(user, "YYY stop~n", []), + %% rabbit_test_configs:stop_nodes(Nodes), + %% exit({Type, Reason, erlang:get_stacktrace()}) + %% end + end}] + end}. +%% [0] If we didn't get as far as starting any nodes then we only have +%% one proplist for initial config, not several per node. So avoid +%% trying to "stop" it - it won't work (and there's nothing to do +%% anyway). + +maybe_print_heading(M, true) -> + io:format(user, "~n~s~n~s~n", [M, string:chars($-, atom_length(M))]); +maybe_print_heading(_M, false) -> + ok. + +apply_config(Things, Cfg) when is_list(Things) -> + lists:foldl(fun apply_config/2, Cfg, Things); +apply_config(F, Cfg) when is_atom(F) -> + apply_config(fun (C) -> rabbit_test_configs:F(C) end, Cfg); +apply_config(F, Cfg) when is_function(F) -> + try + F(Cfg) + catch + Type:Error -> erlang:error({Type, Error, Cfg, erlang:get_stacktrace()}) + end. + +annotate_show_heading(List) -> + annotate_show_heading(List, undefined). + +annotate_show_heading([], _) -> + []; +annotate_show_heading([{M, FWith, F} | Rest], Current) -> + [{M, FWith, F, M =/= Current} | annotate_show_heading(Rest, M)]. + +setup_error_logger(M, F, Base) -> + case error_logger_logfile_filename() of + {error, no_log_file} -> ok; + _ -> ok = error_logger:logfile(close) + end, + FN = rabbit_misc:format("~s/~s:~s.log", [basedir(), M, F]), + ensure_dir(Base), + ok = error_logger:logfile({open, FN}). + +truncate_function_name(FWith, Length) -> + FName = atom_to_list(FWith), + list_to_atom(string:substr(FName, 1, length(FName) - Length)). + +should_run(_M, _F, "all") -> true; +should_run(M, F, Filter) -> MF = rabbit_misc:format("~s:~s", [M, F]), + case re:run(MF, Filter) of + {match, _} -> true; + nomatch -> false + end. + +ensure_dir(Path) -> + case file:read_file_info(Path) of + {ok, #file_info{type=regular}} -> exit({exists_as_file, Path}); + {ok, #file_info{type=directory}} -> ok; + _ -> file:make_dir(Path) + end. + +modules(RelDir) -> + {ok, Files} = file:list_dir(RelDir), + [M || F <- Files, + M <- case string:tokens(F, ".") of + [MStr, "beam"] -> [list_to_atom(MStr)]; + _ -> [] + end]. + +recursive_delete(Dir) -> + rabbit_test_configs:execute({"rm -rf ~s", [Dir]}). + +name(F, Width) -> + R = atom_to_list(F), + R ++ ":" ++ string:chars($ , Width - length(R)). + +atom_length(A) -> length(atom_to_list(A)). + +basedir() -> "/tmp/rabbitmq-multi-node". + +%% reimplement error_logger:logfile(filename) only using +%% gen_event:call/4 instead of gen_event:call/3 with our old friend +%% the 5 second timeout. Grr. +error_logger_logfile_filename() -> + case gen_event:call( + error_logger, error_logger_file_h, filename, infinity) of + {error,_} -> {error, no_log_file}; + Val -> Val + end. + +add_server_test_ebin_dir() -> + %% Some tests need modules from this dir, but it's not on the path + %% by default. + {file, Path} = code:is_loaded(rabbit), + Ebin = filename:dirname(Path), + TestEbin = filename:join([Ebin, "..", "test", "ebin"]), + code:add_path(TestEbin). diff --git a/rabbitmq-server/plugins-src/rabbitmq-test/src/rabbit_test_util.erl b/rabbitmq-server/plugins-src/rabbitmq-test/src/rabbit_test_util.erl new file mode 100644 index 0000000..973e1b0 --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-test/src/rabbit_test_util.erl @@ -0,0 +1,147 @@ +%% The contents of this file are subject to the Mozilla Public License +%% Version 1.1 (the "License"); you may not use this file except in +%% compliance with the License. You may obtain a copy of the License +%% at http://www.mozilla.org/MPL/ +%% +%% Software distributed under the License is distributed on an "AS IS" +%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See +%% the License for the specific language governing rights and +%% limitations under the License. +%% +%% The Original Code is RabbitMQ. +%% +%% The Initial Developer of the Original Code is GoPivotal, Inc. +%% Copyright (c) 2007-2014 GoPivotal, Inc. All rights reserved. +%% +-module(rabbit_test_util). + +-include_lib("amqp_client/include/amqp_client.hrl"). +-import(rabbit_misc, [pget/2]). + +-compile(export_all). + +set_ha_policy(Cfg, Pattern, Policy) -> + set_ha_policy(Cfg, Pattern, Policy, []). + +set_ha_policy(Cfg, Pattern, Policy, Extra) -> + set_policy(Cfg, Pattern, Pattern, <<"queues">>, ha_policy(Policy) ++ Extra). + +ha_policy(<<"all">>) -> [{<<"ha-mode">>, <<"all">>}]; +ha_policy({Mode, Params}) -> [{<<"ha-mode">>, Mode}, + {<<"ha-params">>, Params}]. + +set_policy(Cfg, Name, Pattern, ApplyTo, Definition) -> + ok = rpc:call(pget(node, Cfg), rabbit_policy, set, + [<<"/">>, Name, Pattern, Definition, 0, ApplyTo]). + +clear_policy(Cfg, Name) -> + ok = rpc:call(pget(node, Cfg), rabbit_policy, delete, [<<"/">>, Name]). + +set_param(Cfg, Component, Name, Value) -> + ok = rpc:call(pget(node, Cfg), rabbit_runtime_parameters, set, + [<<"/">>, Component, Name, Value, none]). + +clear_param(Cfg, Component, Name) -> + ok = rpc:call(pget(node, Cfg), rabbit_runtime_parameters, clear, + [<<"/">>, Component, Name]). + +enable_plugin(Cfg, Plugin) -> + plugins_action(enable, Cfg, [Plugin], []). + +disable_plugin(Cfg, Plugin) -> + plugins_action(disable, Cfg, [Plugin], []). + +control_action(Command, Cfg) -> + control_action(Command, Cfg, [], []). + +control_action(Command, Cfg, Args) -> + control_action(Command, Cfg, Args, []). + +control_action(Command, Cfg, Args, Opts) -> + Node = pget(node, Cfg), + rpc:call(Node, rabbit_control_main, action, + [Command, Node, Args, Opts, + fun (F, A) -> + error_logger:info_msg(F ++ "~n", A) + end]). + +plugins_action(Command, Cfg, Args, Opts) -> + PluginsFile = os:getenv("RABBITMQ_ENABLED_PLUGINS_FILE"), + PluginsDir = os:getenv("RABBITMQ_PLUGINS_DIR"), + Node = pget(node, Cfg), + rpc:call(Node, rabbit_plugins_main, action, + [Command, Node, Args, Opts, PluginsFile, PluginsDir]). + +restart_app(Cfg) -> + stop_app(Cfg), + start_app(Cfg). + +stop_app(Cfg) -> + control_action(stop_app, Cfg). + +start_app(Cfg) -> + control_action(start_app, Cfg). + +connect(Cfg) -> + Port = pget(port, Cfg), + {ok, Conn} = amqp_connection:start(#amqp_params_network{port = Port}), + {ok, Ch} = amqp_connection:open_channel(Conn), + {Conn, Ch}. + +%%---------------------------------------------------------------------------- + +kill_after(Time, Cfg, Method) -> + timer:sleep(Time), + kill(Cfg, Method). + +kill(Cfg, Method) -> + kill0(Cfg, Method), + wait_down(pget(node, Cfg)). + +kill0(Cfg, stop) -> rabbit_test_configs:stop_node(Cfg); +kill0(Cfg, sigkill) -> rabbit_test_configs:kill_node(Cfg). + +wait_down(Node) -> + case net_adm:ping(Node) of + pong -> timer:sleep(25), + wait_down(Node); + pang -> ok + end. + +a2b(A) -> list_to_binary(atom_to_list(A)). + +%%---------------------------------------------------------------------------- + +publish(Ch, QName, Count) -> + amqp_channel:call(Ch, #'confirm.select'{}), + [amqp_channel:call(Ch, + #'basic.publish'{routing_key = QName}, + #amqp_msg{props = #'P_basic'{delivery_mode = 2}, + payload = list_to_binary(integer_to_list(I))}) + || I <- lists:seq(1, Count)], + amqp_channel:wait_for_confirms(Ch). + +consume(Ch, QName, Count) -> + amqp_channel:subscribe(Ch, #'basic.consume'{queue = QName, no_ack = true}, + self()), + CTag = receive #'basic.consume_ok'{consumer_tag = C} -> C end, + [begin + Exp = list_to_binary(integer_to_list(I)), + receive {#'basic.deliver'{consumer_tag = CTag}, + #amqp_msg{payload = Exp}} -> + ok + after 500 -> + exit(timeout) + end + end|| I <- lists:seq(1, Count)], + #'queue.declare_ok'{message_count = 0} + = amqp_channel:call(Ch, #'queue.declare'{queue = QName, + durable = true}), + amqp_channel:call(Ch, #'basic.cancel'{consumer_tag = CTag}), + ok. + +fetch(Ch, QName, Count) -> + [{#'basic.get_ok'{}, _} = + amqp_channel:call(Ch, #'basic.get'{queue = QName}) || + _ <- lists:seq(1, Count)], + ok. diff --git a/rabbitmq-server/plugins-src/rabbitmq-test/src/rabbitmq_test.app.src b/rabbitmq-server/plugins-src/rabbitmq-test/src/rabbitmq_test.app.src new file mode 100644 index 0000000..108f874 --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-test/src/rabbitmq_test.app.src @@ -0,0 +1,11 @@ +{application, rabbitmq_test, + [ + {description, ""}, + {vsn, "1"}, + {registered, []}, + {applications, [ + kernel, + stdlib + ]}, + {env, []} + ]}. diff --git a/rabbitmq-server/plugins-src/rabbitmq-test/test/src/cluster_rename.erl b/rabbitmq-server/plugins-src/rabbitmq-test/test/src/cluster_rename.erl new file mode 100644 index 0000000..258c0dc --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-test/test/src/cluster_rename.erl @@ -0,0 +1,194 @@ +%% The contents of this file are subject to the Mozilla Public License +%% Version 1.1 (the "License"); you may not use this file except in +%% compliance with the License. You may obtain a copy of the License +%% at http://www.mozilla.org/MPL/ +%% +%% Software distributed under the License is distributed on an "AS IS" +%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See +%% the License for the specific language governing rights and +%% limitations under the License. +%% +%% The Original Code is RabbitMQ. +%% +%% The Initial Developer of the Original Code is GoPivotal, Inc. +%% Copyright (c) 2007-2014 GoPivotal, Inc. All rights reserved. +%% +-module(cluster_rename). + +-compile(export_all). +-include_lib("eunit/include/eunit.hrl"). +-include_lib("amqp_client/include/amqp_client.hrl"). + +-import(rabbit_misc, [pget/2]). + +-define(CLUSTER2, + fun(C) -> rabbit_test_configs:cluster(C, [bugs, bigwig]) end). + +-define(CLUSTER3, + fun(C) -> rabbit_test_configs:cluster(C, [bugs, bigwig, peter]) end). + +%% Rolling rename of a cluster, each node should do a secondary rename. +rename_cluster_one_by_one_with() -> ?CLUSTER3. +rename_cluster_one_by_one([Bugs, Bigwig, Peter]) -> + publish_all([{Bugs, <<"1">>}, {Bigwig, <<"2">>}, {Peter, <<"3">>}]), + + Jessica = stop_rename_start(Bugs, jessica, [bugs, jessica]), + Hazel = stop_rename_start(Bigwig, hazel, [bigwig, hazel]), + Flopsy = stop_rename_start(Peter, flopsy, [peter, flopsy]), + + consume_all([{Jessica, <<"1">>}, {Hazel, <<"2">>}, {Flopsy, <<"3">>}]), + stop_all([Jessica, Hazel, Flopsy]), + ok. + +%% Big bang rename of a cluster, bugs should do a primary rename. +rename_cluster_big_bang_with() -> ?CLUSTER3. +rename_cluster_big_bang([Bugs, Bigwig, Peter]) -> + publish_all([{Bugs, <<"1">>}, {Bigwig, <<"2">>}, {Peter, <<"3">>}]), + + Peter1 = rabbit_test_configs:stop_node(Peter), + Bigwig1 = rabbit_test_configs:stop_node(Bigwig), + Bugs1 = rabbit_test_configs:stop_node(Bugs), + + Map = [bugs, jessica, bigwig, hazel, peter, flopsy], + Jessica0 = rename_node(Bugs1, jessica, Map), + Hazel0 = rename_node(Bigwig1, hazel, Map), + Flopsy0 = rename_node(Peter1, flopsy, Map), + + Jessica = rabbit_test_configs:start_node(Jessica0), + Hazel = rabbit_test_configs:start_node(Hazel0), + Flopsy = rabbit_test_configs:start_node(Flopsy0), + + consume_all([{Jessica, <<"1">>}, {Hazel, <<"2">>}, {Flopsy, <<"3">>}]), + stop_all([Jessica, Hazel, Flopsy]), + ok. + +%% Here we test that bugs copes with things being renamed around it. +partial_one_by_one_with() -> ?CLUSTER3. +partial_one_by_one([Bugs, Bigwig, Peter]) -> + publish_all([{Bugs, <<"1">>}, {Bigwig, <<"2">>}, {Peter, <<"3">>}]), + + Jessica = stop_rename_start(Bugs, jessica, [bugs, jessica]), + Hazel = stop_rename_start(Bigwig, hazel, [bigwig, hazel]), + + consume_all([{Jessica, <<"1">>}, {Hazel, <<"2">>}, {Peter, <<"3">>}]), + stop_all([Jessica, Hazel, Peter]), + ok. + +%% Here we test that bugs copes with things being renamed around it. +partial_big_bang_with() -> ?CLUSTER3. +partial_big_bang([Bugs, Bigwig, Peter]) -> + publish_all([{Bugs, <<"1">>}, {Bigwig, <<"2">>}, {Peter, <<"3">>}]), + + Peter1 = rabbit_test_configs:stop_node(Peter), + Bigwig1 = rabbit_test_configs:stop_node(Bigwig), + Bugs1 = rabbit_test_configs:stop_node(Bugs), + + Map = [bigwig, hazel, peter, flopsy], + Hazel0 = rename_node(Bigwig1, hazel, Map), + Flopsy0 = rename_node(Peter1, flopsy, Map), + + Bugs2 = rabbit_test_configs:start_node(Bugs1), + Hazel = rabbit_test_configs:start_node(Hazel0), + Flopsy = rabbit_test_configs:start_node(Flopsy0), + + consume_all([{Bugs2, <<"1">>}, {Hazel, <<"2">>}, {Flopsy, <<"3">>}]), + stop_all([Bugs2, Hazel, Flopsy]), + ok. + +%% We should be able to specify the -n parameter on ctl with either +%% the before or after name for the local node (since in real cases +%% one might want to invoke the command before or after the hostname +%% has changed) - usually we test before so here we test after. +post_change_nodename_with() -> ?CLUSTER2. +post_change_nodename([Bugs, _Bigwig]) -> + publish(Bugs, <<"bugs">>), + + Bugs1 = rabbit_test_configs:stop_node(Bugs), + Bugs2 = [{nodename, jessica} | proplists:delete(nodename, Bugs1)], + Jessica0 = rename_node(Bugs2, jessica, [bugs, jessica]), + Jessica = rabbit_test_configs:start_node(Jessica0), + + consume(Jessica, <<"bugs">>), + stop_all([Jessica]), + ok. + +%% If we invoke rename but the node name does not actually change, we +%% should roll back. +abortive_rename_with() -> ?CLUSTER2. +abortive_rename([Bugs, _Bigwig]) -> + publish(Bugs, <<"bugs">>), + + Bugs1 = rabbit_test_configs:stop_node(Bugs), + _Jessica = rename_node(Bugs1, jessica, [bugs, jessica]), + Bugs2 = rabbit_test_configs:start_node(Bugs1), + + consume(Bugs2, <<"bugs">>), + ok. + +%% And test some ways the command can fail. +rename_fail_with() -> ?CLUSTER2. +rename_fail([Bugs, _Bigwig]) -> + Bugs1 = rabbit_test_configs:stop_node(Bugs), + %% Rename from a node that does not exist + rename_node_fail(Bugs1, [bugzilla, jessica]), + %% Rename to a node which does + rename_node_fail(Bugs1, [bugs, bigwig]), + %% Rename two nodes to the same thing + rename_node_fail(Bugs1, [bugs, jessica, bigwig, jessica]), + %% Rename while impersonating a node not in the cluster + rename_node_fail(set_node(rabbit, Bugs1), [bugs, jessica]), + ok. + +rename_twice_fail_with() -> ?CLUSTER2. +rename_twice_fail([Bugs, _Bigwig]) -> + Bugs1 = rabbit_test_configs:stop_node(Bugs), + Indecisive = rename_node(Bugs1, indecisive, [bugs, indecisive]), + rename_node_fail(Indecisive, [indecisive, jessica]), + ok. + +%% ---------------------------------------------------------------------------- + +%% Normal post-test stop does not work since names have changed... +stop_all(Cfgs) -> + [rabbit_test_configs:stop_node(Cfg) || Cfg <- Cfgs]. + +stop_rename_start(Cfg, Nodename, Map) -> + rabbit_test_configs:start_node( + rename_node(rabbit_test_configs:stop_node(Cfg), Nodename, Map)). + +rename_node(Cfg, Nodename, Map) -> + rename_node(Cfg, Nodename, Map, fun rabbit_test_configs:rabbitmqctl/2). + +rename_node_fail(Cfg, Map) -> + rename_node(Cfg, ignored, Map, fun rabbit_test_configs:rabbitmqctl_fail/2). + +rename_node(Cfg, Nodename, Map, Ctl) -> + MapS = string:join( + [atom_to_list(rabbit_nodes:make(N)) || N <- Map], " "), + Ctl(Cfg, {"rename_cluster_node ~s", [MapS]}), + set_node(Nodename, Cfg). + +publish(Cfg, Q) -> + Ch = pget(channel, Cfg), + amqp_channel:call(Ch, #'confirm.select'{}), + amqp_channel:call(Ch, #'queue.declare'{queue = Q, durable = true}), + amqp_channel:cast(Ch, #'basic.publish'{routing_key = Q}, + #amqp_msg{props = #'P_basic'{delivery_mode = 2}, + payload = Q}), + amqp_channel:wait_for_confirms(Ch). + +consume(Cfg, Q) -> + {_Conn, Ch} = rabbit_test_util:connect(Cfg), + amqp_channel:call(Ch, #'queue.declare'{queue = Q, durable = true}), + {#'basic.get_ok'{}, #amqp_msg{payload = Q}} = + amqp_channel:call(Ch, #'basic.get'{queue = Q}). + + +publish_all(CfgsKeys) -> + [publish(Cfg, Key) || {Cfg, Key} <- CfgsKeys]. + +consume_all(CfgsKeys) -> + [consume(Cfg, Key) || {Cfg, Key} <- CfgsKeys]. + +set_node(Nodename, Cfg) -> + [{nodename, Nodename} | proplists:delete(nodename, Cfg)]. diff --git a/rabbitmq-server/plugins-src/rabbitmq-test/test/src/clustering_management.erl b/rabbitmq-server/plugins-src/rabbitmq-test/test/src/clustering_management.erl new file mode 100644 index 0000000..b114aab --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-test/test/src/clustering_management.erl @@ -0,0 +1,608 @@ +%% The contents of this file are subject to the Mozilla Public License +%% Version 1.1 (the "License"); you may not use this file except in +%% compliance with the License. You may obtain a copy of the License +%% at http://www.mozilla.org/MPL/ +%% +%% Software distributed under the License is distributed on an "AS IS" +%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See +%% the License for the specific language governing rights and +%% limitations under the License. +%% +%% The Original Code is RabbitMQ. +%% +%% The Initial Developer of the Original Code is GoPivotal, Inc. +%% Copyright (c) 2007-2014 GoPivotal, Inc. All rights reserved. +%% +-module(clustering_management). + +-compile(export_all). +-include_lib("eunit/include/eunit.hrl"). +-include_lib("amqp_client/include/amqp_client.hrl"). + +-import(rabbit_misc, [pget/2]). + +-define(LOOP_RECURSION_DELAY, 100). + +join_and_part_cluster_with() -> start_abc. +join_and_part_cluster(Config) -> + [Rabbit, Hare, Bunny] = cluster_members(Config), + assert_not_clustered(Rabbit), + assert_not_clustered(Hare), + assert_not_clustered(Bunny), + + stop_join_start(Rabbit, Bunny), + assert_clustered([Rabbit, Bunny]), + + stop_join_start(Hare, Bunny, true), + assert_cluster_status( + {[Bunny, Hare, Rabbit], [Bunny, Rabbit], [Bunny, Hare, Rabbit]}, + [Rabbit, Hare, Bunny]), + + %% Allow clustering with already clustered node + ok = stop_app(Rabbit), + {ok, already_member} = join_cluster(Rabbit, Hare), + ok = start_app(Rabbit), + + stop_reset_start(Rabbit), + assert_not_clustered(Rabbit), + assert_cluster_status({[Bunny, Hare], [Bunny], [Bunny, Hare]}, + [Hare, Bunny]), + + stop_reset_start(Hare), + assert_not_clustered(Hare), + assert_not_clustered(Bunny). + +join_cluster_bad_operations_with() -> start_abc. +join_cluster_bad_operations(Config) -> + [Rabbit, Hare, Bunny] = cluster_members(Config), + + %% Non-existant node + ok = stop_app(Rabbit), + assert_failure(fun () -> join_cluster(Rabbit, non@existant) end), + ok = start_app(Rabbit), + assert_not_clustered(Rabbit), + + %% Trying to cluster with mnesia running + assert_failure(fun () -> join_cluster(Rabbit, Bunny) end), + assert_not_clustered(Rabbit), + + %% Trying to cluster the node with itself + ok = stop_app(Rabbit), + assert_failure(fun () -> join_cluster(Rabbit, Rabbit) end), + ok = start_app(Rabbit), + assert_not_clustered(Rabbit), + + %% Do not let the node leave the cluster or reset if it's the only + %% ram node + stop_join_start(Hare, Rabbit, true), + assert_cluster_status({[Rabbit, Hare], [Rabbit], [Rabbit, Hare]}, + [Rabbit, Hare]), + ok = stop_app(Hare), + assert_failure(fun () -> join_cluster(Rabbit, Bunny) end), + assert_failure(fun () -> reset(Rabbit) end), + ok = start_app(Hare), + assert_cluster_status({[Rabbit, Hare], [Rabbit], [Rabbit, Hare]}, + [Rabbit, Hare]), + + %% Cannot start RAM-only node first + ok = stop_app(Rabbit), + ok = stop_app(Hare), + assert_failure(fun () -> start_app(Hare) end), + ok = start_app(Rabbit), + ok = start_app(Hare), + ok. + +%% This tests that the nodes in the cluster are notified immediately of a node +%% join, and not just after the app is started. +join_to_start_interval_with() -> start_abc. +join_to_start_interval(Config) -> + [Rabbit, Hare, _Bunny] = cluster_members(Config), + + ok = stop_app(Rabbit), + ok = join_cluster(Rabbit, Hare), + assert_cluster_status({[Rabbit, Hare], [Rabbit, Hare], [Hare]}, + [Rabbit, Hare]), + ok = start_app(Rabbit), + assert_clustered([Rabbit, Hare]). + +forget_cluster_node_with() -> start_abc. +forget_cluster_node([_, HareCfg, _] = Config) -> + [Rabbit, Hare, Bunny] = cluster_members(Config), + + %% Trying to remove a node not in the cluster should fail + assert_failure(fun () -> forget_cluster_node(Hare, Rabbit) end), + + stop_join_start(Rabbit, Hare), + assert_clustered([Rabbit, Hare]), + + %% Trying to remove an online node should fail + assert_failure(fun () -> forget_cluster_node(Hare, Rabbit) end), + + ok = stop_app(Rabbit), + %% We're passing the --offline flag, but Hare is online + assert_failure(fun () -> forget_cluster_node(Hare, Rabbit, true) end), + %% Removing some non-existant node will fail + assert_failure(fun () -> forget_cluster_node(Hare, non@existant) end), + ok = forget_cluster_node(Hare, Rabbit), + assert_not_clustered(Hare), + assert_cluster_status({[Rabbit, Hare], [Rabbit, Hare], [Hare]}, + [Rabbit]), + + %% Now we can't start Rabbit since it thinks that it's still in the cluster + %% with Hare, while Hare disagrees. + assert_failure(fun () -> start_app(Rabbit) end), + + ok = reset(Rabbit), + ok = start_app(Rabbit), + assert_not_clustered(Rabbit), + + %% Now we remove Rabbit from an offline node. + stop_join_start(Bunny, Hare), + stop_join_start(Rabbit, Hare), + assert_clustered([Rabbit, Hare, Bunny]), + ok = stop_app(Hare), + ok = stop_app(Rabbit), + ok = stop_app(Bunny), + %% This is fine but we need the flag + assert_failure(fun () -> forget_cluster_node(Hare, Bunny) end), + %% Also fails because hare node is still running + assert_failure(fun () -> forget_cluster_node(Hare, Bunny, true) end), + %% But this works + HareCfg2 = rabbit_test_configs:stop_node(HareCfg), + rabbit_test_configs:rabbitmqctl( + HareCfg2, {"forget_cluster_node --offline ~s", [Bunny]}), + _HareCfg3 = rabbit_test_configs:start_node(HareCfg2), + ok = start_app(Rabbit), + %% Bunny still thinks its clustered with Rabbit and Hare + assert_failure(fun () -> start_app(Bunny) end), + ok = reset(Bunny), + ok = start_app(Bunny), + assert_not_clustered(Bunny), + assert_clustered([Rabbit, Hare]). + +forget_removes_things_with() -> cluster_ab. +forget_removes_things(Cfg) -> + test_removes_things(Cfg, fun (R, H) -> ok = forget_cluster_node(H, R) end). + +reset_removes_things_with() -> cluster_ab. +reset_removes_things(Cfg) -> + test_removes_things(Cfg, fun (R, _H) -> ok = reset(R) end). + +test_removes_things([RabbitCfg, HareCfg] = Config, LoseRabbit) -> + Unmirrored = <<"unmirrored-queue">>, + [Rabbit, Hare] = cluster_members(Config), + RCh = pget(channel, RabbitCfg), + declare(RCh, Unmirrored), + ok = stop_app(Rabbit), + + {_HConn, HCh} = rabbit_test_util:connect(HareCfg), + {'EXIT',{{shutdown,{server_initiated_close,404,_}}, _}} = + (catch declare(HCh, Unmirrored)), + + ok = LoseRabbit(Rabbit, Hare), + {_HConn2, HCh2} = rabbit_test_util:connect(HareCfg), + declare(HCh2, Unmirrored), + ok. + +forget_offline_removes_things_with() -> cluster_ab. +forget_offline_removes_things([Rabbit, Hare]) -> + Unmirrored = <<"unmirrored-queue">>, + X = <<"X">>, + RCh = pget(channel, Rabbit), + declare(RCh, Unmirrored), + + amqp_channel:call(RCh, #'exchange.declare'{durable = true, + exchange = X, + auto_delete = true}), + amqp_channel:call(RCh, #'queue.bind'{queue = Unmirrored, + exchange = X}), + ok = stop_app(pget(node, Rabbit)), + + {_HConn, HCh} = rabbit_test_util:connect(Hare), + {'EXIT',{{shutdown,{server_initiated_close,404,_}}, _}} = + (catch declare(HCh, Unmirrored)), + + Hare2 = rabbit_test_configs:stop_node(Hare), + _Rabbit2 = rabbit_test_configs:stop_node(Rabbit), + rabbit_test_configs:rabbitmqctl( + Hare2, {"forget_cluster_node --offline ~s", [pget(node, Rabbit)]}), + Hare3 = rabbit_test_configs:start_node(Hare2), + + {_HConn2, HCh2} = rabbit_test_util:connect(Hare3), + declare(HCh2, Unmirrored), + {'EXIT',{{shutdown,{server_initiated_close,404,_}}, _}} = + (catch amqp_channel:call(HCh2,#'exchange.declare'{durable = true, + exchange = X, + auto_delete = true, + passive = true})), + ok. + +forget_promotes_offline_slave_with() -> + fun (Cfgs) -> + rabbit_test_configs:cluster(Cfgs, [a, b, c, d]) + end. + +forget_promotes_offline_slave([A, B, C, D]) -> + ACh = pget(channel, A), + ANode = pget(node, A), + Q = <<"mirrored-queue">>, + declare(ACh, Q), + set_ha_policy(Q, A, [B, C]), + set_ha_policy(Q, A, [C, D]), %% Test add and remove from recoverable_slaves + + %% Publish and confirm + amqp_channel:call(ACh, #'confirm.select'{}), + amqp_channel:cast(ACh, #'basic.publish'{routing_key = Q}, + #amqp_msg{props = #'P_basic'{delivery_mode = 2}}), + amqp_channel:wait_for_confirms(ACh), + + %% We kill nodes rather than stop them in order to make sure + %% that we aren't dependent on anything that happens as they shut + %% down (see bug 26467). + D2 = rabbit_test_configs:kill_node(D), + C2 = rabbit_test_configs:kill_node(C), + _B2 = rabbit_test_configs:kill_node(B), + _A2 = rabbit_test_configs:kill_node(A), + + rabbit_test_configs:rabbitmqctl(C2, "force_boot"), + + C3 = rabbit_test_configs:start_node(C2), + + %% We should now have the following dramatis personae: + %% A - down, master + %% B - down, used to be slave, no longer is, never had the message + %% C - running, should be slave, but has wiped the message on restart + %% D - down, recoverable slave, contains message + %% + %% So forgetting A should offline-promote the queue to D, keeping + %% the message. + + rabbit_test_configs:rabbitmqctl(C3, {"forget_cluster_node ~s", [ANode]}), + + D3 = rabbit_test_configs:start_node(D2), + {_DConn2, DCh2} = rabbit_test_util:connect(D3), + #'queue.declare_ok'{message_count = 1} = declare(DCh2, Q), + ok. + +set_ha_policy(Q, MasterCfg, SlaveCfgs) -> + Nodes = [list_to_binary(atom_to_list(pget(node, N))) || + N <- [MasterCfg | SlaveCfgs]], + rabbit_test_util:set_ha_policy(MasterCfg, Q, {<<"nodes">>, Nodes}), + await_slaves(Q, pget(node, MasterCfg), [pget(node, C) || C <- SlaveCfgs]). + +await_slaves(Q, MNode, SNodes) -> + {ok, #amqqueue{pid = MPid, + slave_pids = SPids}} = + rpc:call(MNode, rabbit_amqqueue, lookup, + [rabbit_misc:r(<<"/">>, queue, Q)]), + ActMNode = node(MPid), + ActSNodes = lists:usort([node(P) || P <- SPids]), + case {MNode, lists:usort(SNodes)} of + {ActMNode, ActSNodes} -> ok; + _ -> timer:sleep(100), + await_slaves(Q, MNode, SNodes) + end. + +force_boot_with() -> cluster_ab. +force_boot([Rabbit, Hare]) -> + rabbit_test_configs:rabbitmqctl_fail(Rabbit, force_boot), + Rabbit2 = rabbit_test_configs:stop_node(Rabbit), + _Hare2 = rabbit_test_configs:stop_node(Hare), + rabbit_test_configs:start_node_fail(Rabbit2), + rabbit_test_configs:rabbitmqctl(Rabbit2, force_boot), + _Rabbit3 = rabbit_test_configs:start_node(Rabbit2), + ok. + +change_cluster_node_type_with() -> start_abc. +change_cluster_node_type(Config) -> + [Rabbit, Hare, _Bunny] = cluster_members(Config), + + %% Trying to change the ram node when not clustered should always fail + ok = stop_app(Rabbit), + assert_failure(fun () -> change_cluster_node_type(Rabbit, ram) end), + assert_failure(fun () -> change_cluster_node_type(Rabbit, disc) end), + ok = start_app(Rabbit), + + ok = stop_app(Rabbit), + join_cluster(Rabbit, Hare), + assert_cluster_status({[Rabbit, Hare], [Rabbit, Hare], [Hare]}, + [Rabbit, Hare]), + change_cluster_node_type(Rabbit, ram), + assert_cluster_status({[Rabbit, Hare], [Hare], [Hare]}, + [Rabbit, Hare]), + change_cluster_node_type(Rabbit, disc), + assert_cluster_status({[Rabbit, Hare], [Rabbit, Hare], [Hare]}, + [Rabbit, Hare]), + change_cluster_node_type(Rabbit, ram), + ok = start_app(Rabbit), + assert_cluster_status({[Rabbit, Hare], [Hare], [Hare, Rabbit]}, + [Rabbit, Hare]), + + %% Changing to ram when you're the only ram node should fail + ok = stop_app(Hare), + assert_failure(fun () -> change_cluster_node_type(Hare, ram) end), + ok = start_app(Hare). + +change_cluster_when_node_offline_with() -> start_abc. +change_cluster_when_node_offline(Config) -> + [Rabbit, Hare, Bunny] = cluster_members(Config), + + %% Cluster the three notes + stop_join_start(Rabbit, Hare), + assert_clustered([Rabbit, Hare]), + + stop_join_start(Bunny, Hare), + assert_clustered([Rabbit, Hare, Bunny]), + + %% Bring down Rabbit, and remove Bunny from the cluster while + %% Rabbit is offline + ok = stop_app(Rabbit), + ok = stop_app(Bunny), + ok = reset(Bunny), + assert_cluster_status({[Bunny], [Bunny], []}, [Bunny]), + assert_cluster_status({[Rabbit, Hare], [Rabbit, Hare], [Hare]}, [Hare]), + assert_cluster_status( + {[Rabbit, Hare, Bunny], [Rabbit, Hare, Bunny], [Hare, Bunny]}, [Rabbit]), + + %% Bring Rabbit back up + ok = start_app(Rabbit), + assert_clustered([Rabbit, Hare]), + ok = start_app(Bunny), + assert_not_clustered(Bunny), + + %% Now the same, but Rabbit is a RAM node, and we bring up Bunny + %% before + ok = stop_app(Rabbit), + ok = change_cluster_node_type(Rabbit, ram), + ok = start_app(Rabbit), + stop_join_start(Bunny, Hare), + assert_cluster_status( + {[Rabbit, Hare, Bunny], [Hare, Bunny], [Rabbit, Hare, Bunny]}, + [Rabbit, Hare, Bunny]), + ok = stop_app(Rabbit), + ok = stop_app(Bunny), + ok = reset(Bunny), + ok = start_app(Bunny), + assert_not_clustered(Bunny), + assert_cluster_status({[Rabbit, Hare], [Hare], [Hare]}, [Hare]), + assert_cluster_status( + {[Rabbit, Hare, Bunny], [Hare, Bunny], [Hare, Bunny]}, + [Rabbit]), + ok = start_app(Rabbit), + assert_cluster_status({[Rabbit, Hare], [Hare], [Rabbit, Hare]}, + [Rabbit, Hare]), + assert_not_clustered(Bunny). + +update_cluster_nodes_with() -> start_abc. +update_cluster_nodes(Config) -> + [Rabbit, Hare, Bunny] = cluster_members(Config), + + %% Mnesia is running... + assert_failure(fun () -> update_cluster_nodes(Rabbit, Hare) end), + + ok = stop_app(Rabbit), + ok = join_cluster(Rabbit, Hare), + ok = stop_app(Bunny), + ok = join_cluster(Bunny, Hare), + ok = start_app(Bunny), + stop_reset_start(Hare), + assert_failure(fun () -> start_app(Rabbit) end), + %% Bogus node + assert_failure(fun () -> update_cluster_nodes(Rabbit, non@existant) end), + %% Inconsisent node + assert_failure(fun () -> update_cluster_nodes(Rabbit, Hare) end), + ok = update_cluster_nodes(Rabbit, Bunny), + ok = start_app(Rabbit), + assert_not_clustered(Hare), + assert_clustered([Rabbit, Bunny]). + +erlang_config_with() -> start_ab. +erlang_config(Config) -> + [Rabbit, Hare] = cluster_members(Config), + + ok = stop_app(Hare), + ok = reset(Hare), + ok = rpc:call(Hare, application, set_env, + [rabbit, cluster_nodes, {[Rabbit], disc}]), + ok = start_app(Hare), + assert_clustered([Rabbit, Hare]), + + ok = stop_app(Hare), + ok = reset(Hare), + ok = rpc:call(Hare, application, set_env, + [rabbit, cluster_nodes, {[Rabbit], ram}]), + ok = start_app(Hare), + assert_cluster_status({[Rabbit, Hare], [Rabbit], [Rabbit, Hare]}, + [Rabbit, Hare]), + + %% Check having a stop_app'ed node around doesn't break completely. + ok = stop_app(Hare), + ok = reset(Hare), + ok = stop_app(Rabbit), + ok = rpc:call(Hare, application, set_env, + [rabbit, cluster_nodes, {[Rabbit], disc}]), + ok = start_app(Hare), + ok = start_app(Rabbit), + assert_not_clustered(Hare), + assert_not_clustered(Rabbit), + + %% We get a warning but we start anyway + ok = stop_app(Hare), + ok = reset(Hare), + ok = rpc:call(Hare, application, set_env, + [rabbit, cluster_nodes, {[non@existent], disc}]), + ok = start_app(Hare), + assert_not_clustered(Hare), + assert_not_clustered(Rabbit), + + %% If we use a legacy config file, the node fails to start. + ok = stop_app(Hare), + ok = reset(Hare), + ok = rpc:call(Hare, application, set_env, + [rabbit, cluster_nodes, [Rabbit]]), + assert_failure(fun () -> start_app(Hare) end), + assert_not_clustered(Rabbit), + + %% If we use an invalid node name, the node fails to start. + ok = stop_app(Hare), + ok = reset(Hare), + ok = rpc:call(Hare, application, set_env, + [rabbit, cluster_nodes, {["Mike's computer"], disc}]), + assert_failure(fun () -> start_app(Hare) end), + assert_not_clustered(Rabbit), + + %% If we use an invalid node type, the node fails to start. + ok = stop_app(Hare), + ok = reset(Hare), + ok = rpc:call(Hare, application, set_env, + [rabbit, cluster_nodes, {[Rabbit], blue}]), + assert_failure(fun () -> start_app(Hare) end), + assert_not_clustered(Rabbit), + + %% If we use an invalid cluster_nodes conf, the node fails to start. + ok = stop_app(Hare), + ok = reset(Hare), + ok = rpc:call(Hare, application, set_env, + [rabbit, cluster_nodes, true]), + assert_failure(fun () -> start_app(Hare) end), + assert_not_clustered(Rabbit), + + ok = stop_app(Hare), + ok = reset(Hare), + ok = rpc:call(Hare, application, set_env, + [rabbit, cluster_nodes, "Yes, please"]), + assert_failure(fun () -> start_app(Hare) end), + assert_not_clustered(Rabbit). + +force_reset_node_with() -> start_abc. +force_reset_node(Config) -> + [Rabbit, Hare, _Bunny] = cluster_members(Config), + + stop_join_start(Rabbit, Hare), + stop_app(Rabbit), + force_reset(Rabbit), + %% Hare thinks that Rabbit is still clustered + assert_cluster_status({[Rabbit, Hare], [Rabbit, Hare], [Hare]}, + [Hare]), + %% %% ...but it isn't + assert_cluster_status({[Rabbit], [Rabbit], []}, [Rabbit]), + %% We can rejoin Rabbit and Hare + update_cluster_nodes(Rabbit, Hare), + start_app(Rabbit), + assert_clustered([Rabbit, Hare]). + +%% ---------------------------------------------------------------------------- +%% Internal utils + +cluster_members(Nodes) -> [pget(node,Cfg) || Cfg <- Nodes]. + +assert_cluster_status(Status0, Nodes) -> + Status = {AllNodes, _, _} = sort_cluster_status(Status0), + wait_for_cluster_status(Status, AllNodes, Nodes). + +wait_for_cluster_status(Status, AllNodes, Nodes) -> + Max = 10000 / ?LOOP_RECURSION_DELAY, + wait_for_cluster_status(0, Max, Status, AllNodes, Nodes). + +wait_for_cluster_status(N, Max, Status, _AllNodes, Nodes) when N >= Max -> + erlang:error({cluster_status_max_tries_failed, + [{nodes, Nodes}, + {expected_status, Status}, + {max_tried, Max}]}); +wait_for_cluster_status(N, Max, Status, AllNodes, Nodes) -> + case lists:all(fun (Node) -> + verify_status_equal(Node, Status, AllNodes) + end, Nodes) of + true -> ok; + false -> timer:sleep(?LOOP_RECURSION_DELAY), + wait_for_cluster_status(N + 1, Max, Status, AllNodes, Nodes) + end. + +verify_status_equal(Node, Status, AllNodes) -> + NodeStatus = sort_cluster_status(cluster_status(Node)), + (AllNodes =/= [Node]) =:= rpc:call(Node, rabbit_mnesia, is_clustered, []) + andalso NodeStatus =:= Status. + +cluster_status(Node) -> + {rpc:call(Node, rabbit_mnesia, cluster_nodes, [all]), + rpc:call(Node, rabbit_mnesia, cluster_nodes, [disc]), + rpc:call(Node, rabbit_mnesia, cluster_nodes, [running])}. + +sort_cluster_status({All, Disc, Running}) -> + {lists:sort(All), lists:sort(Disc), lists:sort(Running)}. + +assert_clustered(Nodes) -> + assert_cluster_status({Nodes, Nodes, Nodes}, Nodes). + +assert_not_clustered(Node) -> + assert_cluster_status({[Node], [Node], [Node]}, [Node]). + +assert_failure(Fun) -> + case catch Fun() of + {error, Reason} -> Reason; + {badrpc, {'EXIT', Reason}} -> Reason; + {badrpc_multi, Reason, _Nodes} -> Reason; + Other -> exit({expected_failure, Other}) + end. + +stop_app(Node) -> + control_action(stop_app, Node). + +start_app(Node) -> + control_action(start_app, Node). + +join_cluster(Node, To) -> + join_cluster(Node, To, false). + +join_cluster(Node, To, Ram) -> + control_action(join_cluster, Node, [atom_to_list(To)], [{"--ram", Ram}]). + +reset(Node) -> + control_action(reset, Node). + +force_reset(Node) -> + control_action(force_reset, Node). + +forget_cluster_node(Node, Removee, RemoveWhenOffline) -> + control_action(forget_cluster_node, Node, [atom_to_list(Removee)], + [{"--offline", RemoveWhenOffline}]). + +forget_cluster_node(Node, Removee) -> + forget_cluster_node(Node, Removee, false). + +change_cluster_node_type(Node, Type) -> + control_action(change_cluster_node_type, Node, [atom_to_list(Type)]). + +update_cluster_nodes(Node, DiscoveryNode) -> + control_action(update_cluster_nodes, Node, [atom_to_list(DiscoveryNode)]). + +stop_join_start(Node, ClusterTo, Ram) -> + ok = stop_app(Node), + ok = join_cluster(Node, ClusterTo, Ram), + ok = start_app(Node). + +stop_join_start(Node, ClusterTo) -> + stop_join_start(Node, ClusterTo, false). + +stop_reset_start(Node) -> + ok = stop_app(Node), + ok = reset(Node), + ok = start_app(Node). + +control_action(Command, Node) -> + control_action(Command, Node, [], []). + +control_action(Command, Node, Args) -> + control_action(Command, Node, Args, []). + +control_action(Command, Node, Args, Opts) -> + rpc:call(Node, rabbit_control_main, action, + [Command, Node, Args, Opts, + fun io:format/2]). + +declare(Ch, Name) -> + Res = amqp_channel:call(Ch, #'queue.declare'{durable = true, + queue = Name}), + amqp_channel:call(Ch, #'queue.bind'{queue = Name, + exchange = <<"amq.fanout">>}), + Res. diff --git a/rabbitmq-server/plugins-src/rabbitmq-test/test/src/crashing_queues.erl b/rabbitmq-server/plugins-src/rabbitmq-test/test/src/crashing_queues.erl new file mode 100644 index 0000000..e34fd04 --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-test/test/src/crashing_queues.erl @@ -0,0 +1,213 @@ +%% The contents of this file are subject to the Mozilla Public License +%% Version 1.1 (the "License"); you may not use this file except in +%% compliance with the License. You may obtain a copy of the License +%% at http://www.mozilla.org/MPL/ +%% +%% Software distributed under the License is distributed on an "AS IS" +%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See +%% the License for the specific language governing rights and +%% limitations under the License. +%% +%% The Original Code is RabbitMQ. +%% +%% The Initial Developer of the Original Code is GoPivotal, Inc. +%% Copyright (c) 2007-2014 GoPivotal, Inc. All rights reserved. +%% +-module(crashing_queues). + +-compile(export_all). +-include_lib("eunit/include/eunit.hrl"). +-include_lib("amqp_client/include/amqp_client.hrl"). + +-import(rabbit_test_util, [set_ha_policy/3, a2b/1]). +-import(rabbit_misc, [pget/2]). + +crashing_unmirrored_with() -> [cluster_ab]. +crashing_unmirrored([CfgA, CfgB]) -> + A = pget(node, CfgA), + ChA = pget(channel, CfgA), + ConnB = pget(connection, CfgB), + amqp_channel:call(ChA, #'confirm.select'{}), + test_queue_failure(A, ChA, ConnB, 1, 0, + #'queue.declare'{queue = <<"test">>, durable = true}), + test_queue_failure(A, ChA, ConnB, 0, 0, + #'queue.declare'{queue = <<"test">>, durable = false}), + ok. + +crashing_mirrored_with() -> [cluster_ab, ha_policy_all]. +crashing_mirrored([CfgA, CfgB]) -> + A = pget(node, CfgA), + ChA = pget(channel, CfgA), + ConnB = pget(connection, CfgB), + amqp_channel:call(ChA, #'confirm.select'{}), + test_queue_failure(A, ChA, ConnB, 2, 1, + #'queue.declare'{queue = <<"test">>, durable = true}), + test_queue_failure(A, ChA, ConnB, 2, 1, + #'queue.declare'{queue = <<"test">>, durable = false}), + ok. + +test_queue_failure(Node, Ch, RaceConn, MsgCount, SlaveCount, Decl) -> + #'queue.declare_ok'{queue = QName} = amqp_channel:call(Ch, Decl), + publish(Ch, QName, transient), + publish(Ch, QName, durable), + Racer = spawn_declare_racer(RaceConn, Decl), + kill_queue(Node, QName), + assert_message_count(MsgCount, Ch, QName), + assert_slave_count(SlaveCount, Node, QName), + stop_declare_racer(Racer), + amqp_channel:call(Ch, #'queue.delete'{queue = QName}). + +give_up_after_repeated_crashes_with() -> [cluster_ab]. +give_up_after_repeated_crashes([CfgA, CfgB]) -> + A = pget(node, CfgA), + ChA = pget(channel, CfgA), + ChB = pget(channel, CfgB), + QName = <<"test">>, + amqp_channel:call(ChA, #'confirm.select'{}), + amqp_channel:call(ChA, #'queue.declare'{queue = QName, + durable = true}), + await_state(A, QName, running), + publish(ChA, QName, durable), + kill_queue_hard(A, QName), + {'EXIT', _} = (catch amqp_channel:call( + ChA, #'queue.declare'{queue = QName, + durable = true})), + await_state(A, QName, crashed), + amqp_channel:call(ChB, #'queue.delete'{queue = QName}), + amqp_channel:call(ChB, #'queue.declare'{queue = QName, + durable = true}), + await_state(A, QName, running), + + %% Since it's convenient, also test absent queue status here. + rabbit_test_configs:stop_node(CfgB), + await_state(A, QName, down), + ok. + + +publish(Ch, QName, DelMode) -> + Publish = #'basic.publish'{exchange = <<>>, routing_key = QName}, + Msg = #amqp_msg{props = #'P_basic'{delivery_mode = del_mode(DelMode)}}, + amqp_channel:cast(Ch, Publish, Msg), + amqp_channel:wait_for_confirms(Ch). + +del_mode(transient) -> 1; +del_mode(durable) -> 2. + +spawn_declare_racer(Conn, Decl) -> + Self = self(), + spawn_link(fun() -> declare_racer_loop(Self, Conn, Decl) end). + +stop_declare_racer(Pid) -> + Pid ! stop, + MRef = erlang:monitor(process, Pid), + receive + {'DOWN', MRef, process, Pid, _} -> ok + end. + +declare_racer_loop(Parent, Conn, Decl) -> + receive + stop -> unlink(Parent) + after 0 -> + %% Catch here because we might happen to catch the queue + %% while it is in the middle of recovering and thus + %% explode with NOT_FOUND because crashed. Doesn't matter, + %% we are only in this loop to try to fool the recovery + %% code anyway. + try + case amqp_connection:open_channel(Conn) of + {ok, Ch} -> amqp_channel:call(Ch, Decl); + closing -> ok + end + catch + exit:_ -> + ok + end, + declare_racer_loop(Parent, Conn, Decl) + end. + +await_state(Node, QName, State) -> + await_state(Node, QName, State, 30000). + +await_state(Node, QName, State, Time) -> + case state(Node, QName) of + State -> + ok; + Other -> + case Time of + 0 -> exit({timeout_awaiting_state, State, Other}); + _ -> timer:sleep(100), + await_state(Node, QName, State, Time - 100) + end + end. + +state(Node, QName) -> + V = <<"/">>, + Res = rabbit_misc:r(V, queue, QName), + [[{name, Res}, + {state, State}]] = + rpc:call(Node, rabbit_amqqueue, info_all, [V, [name, state]]), + State. + +kill_queue_hard(Node, QName) -> + case kill_queue(Node, QName) of + crashed -> ok; + _NewPid -> timer:sleep(100), + kill_queue_hard(Node, QName) + end. + +kill_queue(Node, QName) -> + Pid1 = queue_pid(Node, QName), + exit(Pid1, boom), + await_new_pid(Node, QName, Pid1). + +queue_pid(Node, QName) -> + #amqqueue{pid = QPid, + state = State} = lookup(Node, QName), + case State of + crashed -> case sup_child(Node, rabbit_amqqueue_sup_sup) of + {ok, _} -> QPid; %% restarting + {error, no_child} -> crashed %% given up + end; + _ -> QPid + end. + +sup_child(Node, Sup) -> + case rpc:call(Node, supervisor2, which_children, [Sup]) of + [{_, Child, _, _}] -> {ok, Child}; + [] -> {error, no_child}; + {badrpc, {'EXIT', {noproc, _}}} -> {error, no_sup} + end. + +lookup(Node, QName) -> + {ok, Q} = rpc:call(Node, rabbit_amqqueue, lookup, + [rabbit_misc:r(<<"/">>, queue, QName)]), + Q. + +await_new_pid(Node, QName, OldPid) -> + case queue_pid(Node, QName) of + OldPid -> timer:sleep(10), + await_new_pid(Node, QName, OldPid); + New -> New + end. + +assert_message_count(Count, Ch, QName) -> + #'queue.declare_ok'{message_count = Count} = + amqp_channel:call(Ch, #'queue.declare'{queue = QName, + passive = true}). + +assert_slave_count(Count, Node, QName) -> + Q = lookup(Node, QName), + [{_, Pids}] = rpc:call(Node, rabbit_amqqueue, info, [Q, [slave_pids]]), + RealCount = case Pids of + '' -> 0; + _ -> length(Pids) + end, + case RealCount of + Count -> + ok; + _ when RealCount < Count -> + timer:sleep(10), + assert_slave_count(Count, Node, QName); + _ -> + exit({too_many_slaves, Count, RealCount}) + end. diff --git a/rabbitmq-server/plugins-src/rabbitmq-test/test/src/dynamic_ha.erl b/rabbitmq-server/plugins-src/rabbitmq-test/test/src/dynamic_ha.erl new file mode 100644 index 0000000..e9acb52 --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-test/test/src/dynamic_ha.erl @@ -0,0 +1,254 @@ +%% The contents of this file are subject to the Mozilla Public License +%% Version 1.1 (the "License"); you may not use this file except in +%% compliance with the License. You may obtain a copy of the License +%% at http://www.mozilla.org/MPL/ +%% +%% Software distributed under the License is distributed on an "AS IS" +%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See +%% the License for the specific language governing rights and +%% limitations under the License. +%% +%% The Original Code is RabbitMQ. +%% +%% The Initial Developer of the Original Code is GoPivotal, Inc. +%% Copyright (c) 2007-2014 GoPivotal, Inc. All rights reserved. +%% +-module(dynamic_ha). + +%% rabbit_tests:test_dynamic_mirroring() is a unit test which should +%% test the logic of what all the policies decide to do, so we don't +%% need to exhaustively test that here. What we need to test is that: +%% +%% * Going from non-mirrored to mirrored works and vice versa +%% * Changing policy can add / remove mirrors and change the master +%% * Adding a node will create a new mirror when there are not enough nodes +%% for the policy +%% * Removing a node will not create a new mirror even if the policy +%% logic wants it (since this gives us a good way to lose messages +%% on cluster shutdown, by repeated failover to new nodes) +%% +%% The first two are change_policy, the last two are change_cluster + +-compile(export_all). +-include_lib("eunit/include/eunit.hrl"). +-include_lib("amqp_client/include/amqp_client.hrl"). + +-define(QNAME, <<"ha.test">>). +-define(POLICY, <<"^ha.test$">>). %% " emacs +-define(VHOST, <<"/">>). + +-import(rabbit_test_util, [set_ha_policy/3, set_ha_policy/4, + clear_policy/2, a2b/1, publish/3, consume/3]). +-import(rabbit_misc, [pget/2]). + +change_policy_with() -> cluster_abc. +change_policy([CfgA, _CfgB, _CfgC] = Cfgs) -> + ACh = pget(channel, CfgA), + [A, B, C] = [pget(node, Cfg) || Cfg <- Cfgs], + + %% When we first declare a queue with no policy, it's not HA. + amqp_channel:call(ACh, #'queue.declare'{queue = ?QNAME}), + assert_slaves(A, ?QNAME, {A, ''}), + + %% Give it policy "all", it becomes HA and gets all mirrors + set_ha_policy(CfgA, ?POLICY, <<"all">>), + assert_slaves(A, ?QNAME, {A, [B, C]}), + + %% Give it policy "nodes", it gets specific mirrors + set_ha_policy(CfgA, ?POLICY, {<<"nodes">>, [a2b(A), a2b(B)]}), + assert_slaves(A, ?QNAME, {A, [B]}), + + %% Now explicitly change the mirrors + set_ha_policy(CfgA, ?POLICY, {<<"nodes">>, [a2b(A), a2b(C)]}), + assert_slaves(A, ?QNAME, {A, [C]}, [{A, [B, C]}]), + + %% Clear the policy, and we go back to non-mirrored + clear_policy(CfgA, ?POLICY), + assert_slaves(A, ?QNAME, {A, ''}), + + %% Test switching "away" from an unmirrored node + set_ha_policy(CfgA, ?POLICY, {<<"nodes">>, [a2b(B), a2b(C)]}), + assert_slaves(A, ?QNAME, {A, [B, C]}, [{A, [B]}, {A, [C]}]), + + ok. + +change_cluster_with() -> cluster_abc. +change_cluster([CfgA, _CfgB, _CfgC] = CfgsABC) -> + ACh = pget(channel, CfgA), + [A, B, C] = [pget(node, Cfg) || Cfg <- CfgsABC], + + amqp_channel:call(ACh, #'queue.declare'{queue = ?QNAME}), + assert_slaves(A, ?QNAME, {A, ''}), + + %% Give it policy exactly 4, it should mirror to all 3 nodes + set_ha_policy(CfgA, ?POLICY, {<<"exactly">>, 4}), + assert_slaves(A, ?QNAME, {A, [B, C]}), + + %% Add D and E, D joins in + [CfgD, CfgE] = CfgsDE = rabbit_test_configs:start_nodes(CfgA, [d, e], 5675), + D = pget(node, CfgD), + E = pget(node, CfgE), + rabbit_test_configs:add_to_cluster(CfgsABC, CfgsDE), + assert_slaves(A, ?QNAME, {A, [B, C, D]}), + + %% Remove D, E joins in + rabbit_test_configs:stop_node(CfgD), + assert_slaves(A, ?QNAME, {A, [B, C, E]}), + + %% Clean up since we started this by hand + rabbit_test_configs:stop_node(CfgE), + ok. + +rapid_change_with() -> cluster_abc. +rapid_change([CfgA, _CfgB, _CfgC]) -> + ACh = pget(channel, CfgA), + {_Pid, MRef} = spawn_monitor( + fun() -> + [rapid_amqp_ops(ACh, I) || I <- lists:seq(1, 100)] + end), + rapid_loop(CfgA, MRef), + ok. + +rapid_amqp_ops(Ch, I) -> + Payload = list_to_binary(integer_to_list(I)), + amqp_channel:call(Ch, #'queue.declare'{queue = ?QNAME}), + amqp_channel:cast(Ch, #'basic.publish'{exchange = <<"">>, + routing_key = ?QNAME}, + #amqp_msg{payload = Payload}), + amqp_channel:subscribe(Ch, #'basic.consume'{queue = ?QNAME, + no_ack = true}, self()), + receive #'basic.consume_ok'{} -> ok + end, + receive {#'basic.deliver'{}, #amqp_msg{payload = Payload}} -> + ok + end, + amqp_channel:call(Ch, #'queue.delete'{queue = ?QNAME}). + +rapid_loop(Cfg, MRef) -> + receive + {'DOWN', MRef, process, _Pid, normal} -> + ok; + {'DOWN', MRef, process, _Pid, Reason} -> + exit({amqp_ops_died, Reason}) + after 0 -> + set_ha_policy(Cfg, ?POLICY, <<"all">>), + clear_policy(Cfg, ?POLICY), + rapid_loop(Cfg, MRef) + end. + +%% Vhost deletion needs to successfully tear down policies and queues +%% with policies. At least smoke-test that it doesn't blow up. +vhost_deletion_with() -> [cluster_ab, ha_policy_all]. +vhost_deletion([CfgA, _CfgB]) -> + ACh = pget(channel, CfgA), + Node = pget(node, CfgA), + amqp_channel:call(ACh, #'queue.declare'{queue = <<"test">>}), + ok = rpc:call(Node, rabbit_vhost, delete, [<<"/">>]), + ok. + +promote_on_shutdown_with() -> cluster_ab. +promote_on_shutdown([CfgA, CfgB]) -> + set_ha_policy(CfgA, <<"^ha.promote">>, <<"all">>, + [{<<"ha-promote-on-shutdown">>, <<"always">>}]), + set_ha_policy(CfgA, <<"^ha.nopromote">>, <<"all">>), + + ACh = pget(channel, CfgA), + [begin + amqp_channel:call(ACh, #'queue.declare'{queue = Q, + durable = true}), + publish(ACh, Q, 10) + end || Q <- [<<"ha.promote.test">>, <<"ha.nopromote.test">>]], + rabbit_test_configs:restart_node(CfgB), + CfgA1 = rabbit_test_configs:stop_node(CfgA), + {_, BCh} = rabbit_test_util:connect(CfgB), + #'queue.declare_ok'{message_count = 0} = + amqp_channel:call( + BCh, #'queue.declare'{queue = <<"ha.promote.test">>, + durable = true}), + ?assertExit( + {{shutdown, {server_initiated_close, 404, _}}, _}, + amqp_channel:call( + BCh, #'queue.declare'{queue = <<"ha.nopromote.test">>, + durable = true})), + CfgA2 = rabbit_test_configs:start_node(CfgA1), + {_, ACh2} = rabbit_test_util:connect(CfgA2), + #'queue.declare_ok'{message_count = 10} = + amqp_channel:call( + ACh2, #'queue.declare'{queue = <<"ha.nopromote.test">>, + durable = true}), + ok. + +%%---------------------------------------------------------------------------- + +assert_slaves(RPCNode, QName, Exp) -> + assert_slaves(RPCNode, QName, Exp, []). + +assert_slaves(RPCNode, QName, Exp, PermittedIntermediate) -> + assert_slaves0(RPCNode, QName, Exp, + [{get(previous_exp_m_node), get(previous_exp_s_nodes)} | + PermittedIntermediate]). + +assert_slaves0(RPCNode, QName, {ExpMNode, ExpSNodes}, PermittedIntermediate) -> + Q = find_queue(QName, RPCNode), + Pid = proplists:get_value(pid, Q), + SPids = proplists:get_value(slave_pids, Q), + ActMNode = node(Pid), + ActSNodes = case SPids of + '' -> ''; + _ -> [node(SPid) || SPid <- SPids] + end, + case ExpMNode =:= ActMNode andalso equal_list(ExpSNodes, ActSNodes) of + false -> + %% It's an async change, so if nothing has changed let's + %% just wait - of course this means if something does not + %% change when expected then we time out the test which is + %% a bit tedious + case [found || {PermMNode, PermSNodes} <- PermittedIntermediate, + PermMNode =:= ActMNode, + equal_list(PermSNodes, ActSNodes)] of + [] -> ct:fail("Expected ~p / ~p, got ~p / ~p~nat ~p~n", + [ExpMNode, ExpSNodes, ActMNode, ActSNodes, + get_stacktrace()]); + _ -> timer:sleep(100), + assert_slaves0(RPCNode, QName, {ExpMNode, ExpSNodes}, + PermittedIntermediate) + end; + true -> + put(previous_exp_m_node, ExpMNode), + put(previous_exp_s_nodes, ExpSNodes), + ok + end. + +equal_list('', '') -> true; +equal_list('', _Act) -> false; +equal_list(_Exp, '') -> false; +equal_list([], []) -> true; +equal_list(_Exp, []) -> false; +equal_list([], _Act) -> false; +equal_list([H|T], Act) -> case lists:member(H, Act) of + true -> equal_list(T, Act -- [H]); + false -> false + end. + +find_queue(QName, RPCNode) -> + Qs = rpc:call(RPCNode, rabbit_amqqueue, info_all, [?VHOST], infinity), + case find_queue0(QName, Qs) of + did_not_find_queue -> timer:sleep(100), + find_queue(QName, RPCNode); + Q -> Q + end. + +find_queue0(QName, Qs) -> + case [Q || Q <- Qs, proplists:get_value(name, Q) =:= + rabbit_misc:r(?VHOST, queue, QName)] of + [R] -> R; + [] -> did_not_find_queue + end. + +get_stacktrace() -> + try + throw(e) + catch + _:e -> + erlang:get_stacktrace() + end. diff --git a/rabbitmq-server/plugins-src/rabbitmq-test/test/src/eager_sync.erl b/rabbitmq-server/plugins-src/rabbitmq-test/test/src/eager_sync.erl new file mode 100644 index 0000000..9c2d935 --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-test/test/src/eager_sync.erl @@ -0,0 +1,205 @@ +%% The contents of this file are subject to the Mozilla Public License +%% Version 1.1 (the "License"); you may not use this file except in +%% compliance with the License. You may obtain a copy of the License +%% at http://www.mozilla.org/MPL/ +%% +%% Software distributed under the License is distributed on an "AS IS" +%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See +%% the License for the specific language governing rights and +%% limitations under the License. +%% +%% The Original Code is RabbitMQ. +%% +%% The Initial Developer of the Original Code is GoPivotal, Inc. +%% Copyright (c) 2007-2014 GoPivotal, Inc. All rights reserved. +%% +-module(eager_sync). + +-compile(export_all). +-include_lib("eunit/include/eunit.hrl"). +-include_lib("amqp_client/include/amqp_client.hrl"). + +-define(QNAME, <<"ha.two.test">>). +-define(QNAME_AUTO, <<"ha.auto.test">>). +-define(MESSAGE_COUNT, 2000). + +-import(rabbit_test_util, [a2b/1, publish/3, consume/3, fetch/3]). +-import(rabbit_misc, [pget/2]). + +-define(CONFIG, [cluster_abc, ha_policy_two_pos]). + +eager_sync_with() -> ?CONFIG. +eager_sync([A, B, C]) -> + %% Queue is on AB but not C. + ACh = pget(channel, A), + Ch = pget(channel, C), + amqp_channel:call(ACh, #'queue.declare'{queue = ?QNAME, + durable = true}), + + %% Don't sync, lose messages + publish(Ch, ?QNAME, ?MESSAGE_COUNT), + restart(A), + restart(B), + consume(Ch, ?QNAME, 0), + + %% Sync, keep messages + publish(Ch, ?QNAME, ?MESSAGE_COUNT), + restart(A), + ok = sync(C, ?QNAME), + restart(B), + consume(Ch, ?QNAME, ?MESSAGE_COUNT), + + %% Check the no-need-to-sync path + publish(Ch, ?QNAME, ?MESSAGE_COUNT), + ok = sync(C, ?QNAME), + consume(Ch, ?QNAME, ?MESSAGE_COUNT), + + %% keep unacknowledged messages + publish(Ch, ?QNAME, ?MESSAGE_COUNT), + fetch(Ch, ?QNAME, 2), + restart(A), + fetch(Ch, ?QNAME, 3), + sync(C, ?QNAME), + restart(B), + consume(Ch, ?QNAME, ?MESSAGE_COUNT), + + ok. + +eager_sync_cancel_with() -> ?CONFIG. +eager_sync_cancel([A, B, C]) -> + %% Queue is on AB but not C. + ACh = pget(channel, A), + Ch = pget(channel, C), + + amqp_channel:call(ACh, #'queue.declare'{queue = ?QNAME, + durable = true}), + {ok, not_syncing} = sync_cancel(C, ?QNAME), %% Idempotence + eager_sync_cancel_test2(A, B, C, Ch). + +eager_sync_cancel_test2(A, B, C, Ch) -> + %% Sync then cancel + publish(Ch, ?QNAME, ?MESSAGE_COUNT), + restart(A), + spawn_link(fun() -> ok = sync_nowait(C, ?QNAME) end), + case wait_for_syncing(C, ?QNAME, 1) of + ok -> + case sync_cancel(C, ?QNAME) of + ok -> + wait_for_running(C, ?QNAME), + restart(B), + consume(Ch, ?QNAME, 0), + + {ok, not_syncing} = sync_cancel(C, ?QNAME), %% Idempotence + ok; + {ok, not_syncing} -> + %% Damn. Syncing finished between wait_for_syncing/3 and + %% sync_cancel/2 above. Start again. + amqp_channel:call(Ch, #'queue.purge'{queue = ?QNAME}), + eager_sync_cancel_test2(A, B, C, Ch) + end; + synced_already -> + %% Damn. Syncing finished before wait_for_syncing/3. Start again. + amqp_channel:call(Ch, #'queue.purge'{queue = ?QNAME}), + eager_sync_cancel_test2(A, B, C, Ch) + end. + +eager_sync_auto_with() -> ?CONFIG. +eager_sync_auto([A, B, C]) -> + ACh = pget(channel, A), + Ch = pget(channel, C), + amqp_channel:call(ACh, #'queue.declare'{queue = ?QNAME_AUTO, + durable = true}), + + %% Sync automatically, don't lose messages + publish(Ch, ?QNAME_AUTO, ?MESSAGE_COUNT), + restart(A), + wait_for_sync(C, ?QNAME_AUTO), + restart(B), + wait_for_sync(C, ?QNAME_AUTO), + consume(Ch, ?QNAME_AUTO, ?MESSAGE_COUNT), + + ok. + +eager_sync_auto_on_policy_change_with() -> ?CONFIG. +eager_sync_auto_on_policy_change([A, B, C]) -> + ACh = pget(channel, A), + Ch = pget(channel, C), + amqp_channel:call(ACh, #'queue.declare'{queue = ?QNAME, + durable = true}), + + %% Sync automatically once the policy is changed to tell us to. + publish(Ch, ?QNAME, ?MESSAGE_COUNT), + restart(A), + Params = [a2b(pget(node, Cfg)) || Cfg <- [A, B]], + rabbit_test_util:set_ha_policy( + A, <<"^ha.two.">>, {<<"nodes">>, Params}, + [{<<"ha-sync-mode">>, <<"automatic">>}]), + wait_for_sync(C, ?QNAME), + + ok. + +eager_sync_requeue_with() -> ?CONFIG. +eager_sync_requeue([A, B, C]) -> + %% Queue is on AB but not C. + ACh = pget(channel, A), + Ch = pget(channel, C), + amqp_channel:call(ACh, #'queue.declare'{queue = ?QNAME, + durable = true}), + + publish(Ch, ?QNAME, 2), + {#'basic.get_ok'{delivery_tag = TagA}, _} = + amqp_channel:call(Ch, #'basic.get'{queue = ?QNAME}), + {#'basic.get_ok'{delivery_tag = TagB}, _} = + amqp_channel:call(Ch, #'basic.get'{queue = ?QNAME}), + amqp_channel:cast(Ch, #'basic.reject'{delivery_tag = TagA, requeue = true}), + restart(B), + ok = sync(C, ?QNAME), + amqp_channel:cast(Ch, #'basic.reject'{delivery_tag = TagB, requeue = true}), + consume(Ch, ?QNAME, 2), + + ok. + +restart(Cfg) -> rabbit_test_util:restart_app(Cfg). + +sync(Cfg, QName) -> + case sync_nowait(Cfg, QName) of + ok -> wait_for_sync(Cfg, QName), + ok; + R -> R + end. + +sync_nowait(Cfg, QName) -> action(Cfg, sync_queue, QName). +sync_cancel(Cfg, QName) -> action(Cfg, cancel_sync_queue, QName). + +wait_for_sync(Cfg, QName) -> + sync_detection:wait_for_sync_status(true, Cfg, QName). + +action(Cfg, Action, QName) -> + rabbit_test_util:control_action( + Action, Cfg, [binary_to_list(QName)], [{"-p", "/"}]). + +queue(Cfg, QName) -> + QNameRes = rabbit_misc:r(<<"/">>, queue, QName), + {ok, Q} = rpc:call(pget(node, Cfg), rabbit_amqqueue, lookup, [QNameRes]), + Q. + +wait_for_syncing(Cfg, QName, Target) -> + case state(Cfg, QName) of + {{syncing, _}, _} -> ok; + {running, Target} -> synced_already; + _ -> timer:sleep(100), + wait_for_syncing(Cfg, QName, Target) + end. + +wait_for_running(Cfg, QName) -> + case state(Cfg, QName) of + {running, _} -> ok; + _ -> timer:sleep(100), + wait_for_running(Cfg, QName) + end. + +state(Cfg, QName) -> + [{state, State}, {synchronised_slave_pids, Pids}] = + rpc:call(pget(node, Cfg), rabbit_amqqueue, info, + [queue(Cfg, QName), [state, synchronised_slave_pids]]), + {State, length(Pids)}. diff --git a/rabbitmq-server/plugins-src/rabbitmq-test/test/src/many_node_ha.erl b/rabbitmq-server/plugins-src/rabbitmq-test/test/src/many_node_ha.erl new file mode 100644 index 0000000..9104d4c --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-test/test/src/many_node_ha.erl @@ -0,0 +1,64 @@ +%% The contents of this file are subject to the Mozilla Public License +%% Version 1.1 (the "License"); you may not use this file except in +%% compliance with the License. You may obtain a copy of the License +%% at http://www.mozilla.org/MPL/ +%% +%% Software distributed under the License is distributed on an "AS IS" +%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See +%% the License for the specific language governing rights and +%% limitations under the License. +%% +%% The Original Code is RabbitMQ. +%% +%% The Initial Developer of the Original Code is GoPivotal, Inc. +%% Copyright (c) 2007-2014 GoPivotal, Inc. All rights reserved. +%% +-module(many_node_ha). + +-compile(export_all). +-include_lib("eunit/include/eunit.hrl"). +-include_lib("amqp_client/include/amqp_client.hrl"). + +-import(rabbit_test_util, [a2b/1]). +-import(rabbit_misc, [pget/2]). + +kill_intermediate_with() -> + fun (Cfg) -> rabbit_test_configs:ha_policy_all( + rabbit_test_configs:cluster(Cfg, [a,b,c,d,e,f])) + end. +kill_intermediate([CfgA, CfgB, CfgC, CfgD, CfgE, CfgF]) -> + Msgs = rabbit_test_configs:cover_work_factor(20000, CfgA), + MasterChannel = pget(channel, CfgA), + ConsumerChannel = pget(channel, CfgE), + ProducerChannel = pget(channel, CfgF), + Queue = <<"test">>, + amqp_channel:call(MasterChannel, #'queue.declare'{queue = Queue, + auto_delete = false}), + + %% TODO: this seems *highly* timing dependant - the assumption being + %% that the kill will work quickly enough that there will still be + %% some messages in-flight that we *must* receive despite the intervening + %% node deaths. It would be nice if we could find a means to do this + %% in a way that is not actually timing dependent. + + %% Worse still, it assumes that killing the master will cause a + %% failover to Slave1, and so on. Nope. + + ConsumerPid = rabbit_ha_test_consumer:create(ConsumerChannel, + Queue, self(), false, Msgs), + + ProducerPid = rabbit_ha_test_producer:create(ProducerChannel, + Queue, self(), false, Msgs), + + %% create a killer for the master and the first 3 slaves + [rabbit_test_util:kill_after(Time, Cfg, sigkill) || + {Cfg, Time} <- [{CfgA, 50}, + {CfgB, 50}, + {CfgC, 100}, + {CfgD, 100}]], + + %% verify that the consumer got all msgs, or die, or time out + rabbit_ha_test_producer:await_response(ProducerPid), + rabbit_ha_test_consumer:await_response(ConsumerPid), + ok. + diff --git a/rabbitmq-server/plugins-src/rabbitmq-test/test/src/partitions.erl b/rabbitmq-server/plugins-src/rabbitmq-test/test/src/partitions.erl new file mode 100644 index 0000000..56b99ca --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-test/test/src/partitions.erl @@ -0,0 +1,370 @@ +%% The contents of this file are subject to the Mozilla Public License +%% Version 1.1 (the "License"); you may not use this file except in +%% compliance with the License. You may obtain a copy of the License +%% at http://www.mozilla.org/MPL/ +%% +%% Software distributed under the License is distributed on an "AS IS" +%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See +%% the License for the specific language governing rights and +%% limitations under the License. +%% +%% The Original Code is RabbitMQ. +%% +%% The Initial Developer of the Original Code is GoPivotal, Inc. +%% Copyright (c) 2007-2014 GoPivotal, Inc. All rights reserved. +%% +-module(partitions). + +-compile(export_all). +-include_lib("eunit/include/eunit.hrl"). +-include_lib("amqp_client/include/amqp_client.hrl"). + +-import(rabbit_misc, [pget/2]). + +-define(CONFIG, [start_abc, fun enable_dist_proxy/1, + build_cluster, short_ticktime(1), start_connections]). +%% We set ticktime to 1s and setuptime is 7s so to make sure it +%% passes... +-define(DELAY, 8000). + +ignore_with() -> ?CONFIG. +ignore(Cfgs) -> + [A, B, C] = [pget(node, Cfg) || Cfg <- Cfgs], + block_unblock([{A, B}, {A, C}]), + timer:sleep(?DELAY), + [B, C] = partitions(A), + [A] = partitions(B), + [A] = partitions(C), + ok. + +pause_minority_on_down_with() -> ?CONFIG. +pause_minority_on_down([CfgA, CfgB, CfgC] = Cfgs) -> + A = pget(node, CfgA), + set_mode(Cfgs, pause_minority), + true = is_running(A), + + rabbit_test_util:kill(CfgB, sigkill), + timer:sleep(?DELAY), + true = is_running(A), + + rabbit_test_util:kill(CfgC, sigkill), + await_running(A, false), + ok. + +pause_minority_on_blocked_with() -> ?CONFIG. +pause_minority_on_blocked(Cfgs) -> + [A, B, C] = [pget(node, Cfg) || Cfg <- Cfgs], + set_mode(Cfgs, pause_minority), + pause_on_blocked(A, B, C). + +pause_if_all_down_on_down_with() -> ?CONFIG. +pause_if_all_down_on_down([_, CfgB, CfgC] = Cfgs) -> + [A, B, C] = [pget(node, Cfg) || Cfg <- Cfgs], + set_mode(Cfgs, {pause_if_all_down, [C], ignore}), + [(true = is_running(N)) || N <- [A, B, C]], + + rabbit_test_util:kill(CfgB, sigkill), + timer:sleep(?DELAY), + [(true = is_running(N)) || N <- [A, C]], + + rabbit_test_util:kill(CfgC, sigkill), + timer:sleep(?DELAY), + await_running(A, false), + ok. + +pause_if_all_down_on_blocked_with() -> ?CONFIG. +pause_if_all_down_on_blocked(Cfgs) -> + [A, B, C] = [pget(node, Cfg) || Cfg <- Cfgs], + set_mode(Cfgs, {pause_if_all_down, [C], ignore}), + pause_on_blocked(A, B, C). + +pause_on_blocked(A, B, C) -> + [(true = is_running(N)) || N <- [A, B, C]], + block([{A, B}, {A, C}]), + await_running(A, false), + [await_running(N, true) || N <- [B, C]], + unblock([{A, B}, {A, C}]), + [await_running(N, true) || N <- [A, B, C]], + Status = rpc:call(B, rabbit_mnesia, status, []), + [] = pget(partitions, Status), + ok. + +%% Make sure we do not confirm any messages after a partition has +%% happened but before we pause, since any such confirmations would be +%% lies. +%% +%% This test has to use an AB cluster (not ABC) since GM ends up +%% taking longer to detect down slaves when there are more nodes and +%% we close the window by mistake. +%% +%% In general there are quite a few ways to accidentally cause this +%% test to pass since there are a lot of things in the broker that can +%% suddenly take several seconds to time out when TCP connections +%% won't establish. +pause_minority_false_promises_mirrored_with() -> + [start_ab, fun enable_dist_proxy/1, + build_cluster, short_ticktime(10), start_connections, ha_policy_all]. + +pause_minority_false_promises_mirrored(Cfgs) -> + pause_false_promises(Cfgs, pause_minority). + +pause_minority_false_promises_unmirrored_with() -> + [start_ab, fun enable_dist_proxy/1, + build_cluster, short_ticktime(10), start_connections]. + +pause_minority_false_promises_unmirrored(Cfgs) -> + pause_false_promises(Cfgs, pause_minority). + +pause_if_all_down_false_promises_mirrored_with() -> + [start_ab, fun enable_dist_proxy/1, + build_cluster, short_ticktime(10), start_connections, ha_policy_all]. + +pause_if_all_down_false_promises_mirrored([_, CfgB | _] = Cfgs) -> + B = pget(node, CfgB), + pause_false_promises(Cfgs, {pause_if_all_down, [B], ignore}). + +pause_if_all_down_false_promises_unmirrored_with() -> + [start_ab, fun enable_dist_proxy/1, + build_cluster, short_ticktime(10), start_connections]. + +pause_if_all_down_false_promises_unmirrored([_, CfgB | _] = Cfgs) -> + B = pget(node, CfgB), + pause_false_promises(Cfgs, {pause_if_all_down, [B], ignore}). + +pause_false_promises([CfgA, CfgB | _] = Cfgs, ClusterPartitionHandling) -> + [A, B] = [pget(node, Cfg) || Cfg <- Cfgs], + set_mode([CfgA], ClusterPartitionHandling), + ChA = pget(channel, CfgA), + ChB = pget(channel, CfgB), + amqp_channel:call(ChB, #'queue.declare'{queue = <<"test">>, + durable = true}), + amqp_channel:call(ChA, #'confirm.select'{}), + amqp_channel:register_confirm_handler(ChA, self()), + + %% Cause a partition after 1s + Self = self(), + spawn_link(fun () -> + timer:sleep(1000), + %%io:format(user, "~p BLOCK~n", [calendar:local_time()]), + block([{A, B}]), + unlink(Self) + end), + + %% Publish large no of messages, see how many we get confirmed + [amqp_channel:cast(ChA, #'basic.publish'{routing_key = <<"test">>}, + #amqp_msg{props = #'P_basic'{delivery_mode = 1}}) || + _ <- lists:seq(1, 100000)], + %%io:format(user, "~p finish publish~n", [calendar:local_time()]), + + %% Time for the partition to be detected. We don't put this sleep + %% in receive_acks since otherwise we'd have another similar sleep + %% at the end. + timer:sleep(30000), + Confirmed = receive_acks(0), + %%io:format(user, "~p got acks~n", [calendar:local_time()]), + await_running(A, false), + %%io:format(user, "~p A stopped~n", [calendar:local_time()]), + + unblock([{A, B}]), + await_running(A, true), + + %% But how many made it onto the rest of the cluster? + #'queue.declare_ok'{message_count = Survived} = + amqp_channel:call(ChB, #'queue.declare'{queue = <<"test">>, + durable = true}), + %%io:format(user, "~p queue declared~n", [calendar:local_time()]), + case Confirmed > Survived of + true -> ?debugVal({Confirmed, Survived}); + false -> ok + end, + ?assert(Confirmed =< Survived), + ok. + +receive_acks(Max) -> + receive + #'basic.ack'{delivery_tag = DTag} -> + receive_acks(DTag) + after ?DELAY -> + Max + end. + +prompt_disconnect_detection_with() -> + [start_ab, fun enable_dist_proxy/1, + build_cluster, short_ticktime(1), start_connections]. + +prompt_disconnect_detection([CfgA, CfgB]) -> + A = pget(node, CfgA), + B = pget(node, CfgB), + ChB = pget(channel, CfgB), + [amqp_channel:call(ChB, #'queue.declare'{}) || _ <- lists:seq(1, 100)], + block([{A, B}]), + timer:sleep(?DELAY), + %% We want to make sure we do not end up waiting for setuptime * + %% no of queues. Unfortunately that means we need a timeout... + [] = rpc(CfgA, rabbit_amqqueue, info_all, [<<"/">>], ?DELAY), + ok. + +ctl_ticktime_sync_with() -> [start_ab, short_ticktime(1)]. +ctl_ticktime_sync([CfgA | _]) -> + %% Server has 1s net_ticktime, make sure ctl doesn't get disconnected + "ok\n" = rabbit_test_configs:rabbitmqctl(CfgA, "eval 'timer:sleep(5000).'"), + ok. + +%% NB: we test full and partial partitions here. +autoheal_with() -> ?CONFIG. +autoheal(Cfgs) -> + set_mode(Cfgs, autoheal), + do_autoheal(Cfgs). + +autoheal_after_pause_if_all_down_with() -> ?CONFIG. +autoheal_after_pause_if_all_down([_, CfgB, CfgC | _] = Cfgs) -> + B = pget(node, CfgB), + C = pget(node, CfgC), + set_mode(Cfgs, {pause_if_all_down, [B, C], autoheal}), + do_autoheal(Cfgs). + +do_autoheal(Cfgs) -> + [A, B, C] = [pget(node, Cfg) || Cfg <- Cfgs], + Test = fun (Pairs) -> + block_unblock(Pairs), + %% Sleep to make sure all the partitions are noticed + %% ?DELAY for the net_tick timeout + timer:sleep(?DELAY), + [await_listening(N, true) || N <- [A, B, C]], + [await_partitions(N, []) || N <- [A, B, C]] + end, + Test([{B, C}]), + Test([{A, C}, {B, C}]), + Test([{A, B}, {A, C}, {B, C}]), + ok. + +partial_false_positive_with() -> ?CONFIG. +partial_false_positive(Cfgs) -> + [A, B, C] = [pget(node, Cfg) || Cfg <- Cfgs], + block([{A, B}]), + timer:sleep(1000), + block([{A, C}]), + timer:sleep(?DELAY), + unblock([{A, B}, {A, C}]), + timer:sleep(?DELAY), + %% When B times out A's connection, it will check with C. C will + %% not have timed out A yet, but already it can't talk to it. We + %% need to not consider this a partial partition; B and C should + %% still talk to each other. + [B, C] = partitions(A), + [A] = partitions(B), + [A] = partitions(C), + ok. + +partial_to_full_with() -> ?CONFIG. +partial_to_full(Cfgs) -> + [A, B, C] = [pget(node, Cfg) || Cfg <- Cfgs], + block_unblock([{A, B}]), + timer:sleep(?DELAY), + %% There are several valid ways this could go, depending on how + %% the DOWN messages race: either A gets disconnected first and BC + %% stay together, or B gets disconnected first and AC stay + %% together, or both make it through and all three get + %% disconnected. + case {partitions(A), partitions(B), partitions(C)} of + {[B, C], [A], [A]} -> ok; + {[B], [A, C], [B]} -> ok; + {[B, C], [A, C], [A, B]} -> ok; + Partitions -> exit({partitions, Partitions}) + end. + +partial_pause_minority_with() -> ?CONFIG. +partial_pause_minority(Cfgs) -> + [A, B, C] = [pget(node, Cfg) || Cfg <- Cfgs], + set_mode(Cfgs, pause_minority), + block([{A, B}]), + [await_running(N, false) || N <- [A, B]], + await_running(C, true), + unblock([{A, B}]), + [await_listening(N, true) || N <- [A, B, C]], + [await_partitions(N, []) || N <- [A, B, C]], + ok. + +partial_pause_if_all_down_with() -> ?CONFIG. +partial_pause_if_all_down(Cfgs) -> + [A, B, C] = [pget(node, Cfg) || Cfg <- Cfgs], + set_mode(Cfgs, {pause_if_all_down, [B], ignore}), + block([{A, B}]), + await_running(A, false), + [await_running(N, true) || N <- [B, C]], + unblock([{A, B}]), + [await_listening(N, true) || N <- [A, B, C]], + [await_partitions(N, []) || N <- [A, B, C]], + ok. + +set_mode(Cfgs, Mode) -> + [set_env(Cfg, rabbit, cluster_partition_handling, Mode) || Cfg <- Cfgs]. + +set_env(Cfg, App, K, V) -> + rpc(Cfg, application, set_env, [App, K, V]). + +block_unblock(Pairs) -> + block(Pairs), + timer:sleep(?DELAY), + unblock(Pairs). + +block(Pairs) -> [block(X, Y) || {X, Y} <- Pairs]. +unblock(Pairs) -> [allow(X, Y) || {X, Y} <- Pairs]. + +partitions(Node) -> + case rpc:call(Node, rabbit_node_monitor, partitions, []) of + {badrpc, {'EXIT', E}} = R -> case rabbit_misc:is_abnormal_exit(E) of + true -> R; + false -> timer:sleep(1000), + partitions(Node) + end; + Partitions -> Partitions + end. + +block(X, Y) -> + rpc:call(X, inet_tcp_proxy, block, [Y]), + rpc:call(Y, inet_tcp_proxy, block, [X]). + +allow(X, Y) -> + rpc:call(X, inet_tcp_proxy, allow, [Y]), + rpc:call(Y, inet_tcp_proxy, allow, [X]). + +await_running (Node, Bool) -> await(Node, Bool, fun is_running/1). +await_listening (Node, Bool) -> await(Node, Bool, fun is_listening/1). +await_partitions(Node, Parts) -> await(Node, Parts, fun partitions/1). + +await(Node, Res, Fun) -> + case Fun(Node) of + Res -> ok; + _ -> timer:sleep(100), + await(Node, Res, Fun) + end. + +is_running(Node) -> rpc:call(Node, rabbit, is_running, []). + +is_listening(Node) -> + case rpc:call(Node, rabbit_networking, node_listeners, [Node]) of + [] -> false; + [_|_] -> true; + _ -> false + end. + +enable_dist_proxy(Cfgs) -> + inet_tcp_proxy_manager:start_link(), + Nodes = [pget(node, Cfg) || Cfg <- Cfgs], + [ok = rpc:call(Node, inet_tcp_proxy, start, []) || Node <- Nodes], + [ok = rpc:call(Node, inet_tcp_proxy, reconnect, [Nodes]) || Node <- Nodes], + Cfgs. + +short_ticktime(Time) -> + fun (Cfgs) -> + [rpc(Cfg, net_kernel, set_net_ticktime, [Time, 0]) || Cfg <- Cfgs], + net_kernel:set_net_ticktime(Time, 0), + Cfgs + end. + +rpc(Cfg, M, F, A) -> + rpc:call(pget(node, Cfg), M, F, A). + +rpc(Cfg, M, F, A, T) -> + rpc:call(pget(node, Cfg), M, F, A, T). diff --git a/rabbitmq-server/plugins-src/rabbitmq-test/test/src/rabbit_priority_queue_test.erl b/rabbitmq-server/plugins-src/rabbitmq-test/test/src/rabbit_priority_queue_test.erl new file mode 100644 index 0000000..44228ff --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-test/test/src/rabbit_priority_queue_test.erl @@ -0,0 +1,335 @@ +%% The contents of this file are subject to the Mozilla Public License +%% Version 1.1 (the "License"); you may not use this file except in +%% compliance with the License. You may obtain a copy of the License +%% at http://www.mozilla.org/MPL/ +%% +%% Software distributed under the License is distributed on an "AS IS" +%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See +%% the License for the specific language governing rights and +%% limitations under the License. +%% +%% The Original Code is RabbitMQ. +%% +%% The Initial Developer of the Original Code is GoPivotal, Inc. +%% Copyright (c) 2007-2014 GoPivotal, Inc. All rights reserved. +%% + +-module(rabbit_priority_queue_test). + +-compile(export_all). +-include_lib("eunit/include/eunit.hrl"). +-include_lib("amqp_client/include/amqp_client.hrl"). + +-import(rabbit_misc, [pget/2]). + +%% The BQ API is used in all sorts of places in all sorts of +%% ways. Therefore we have to jump through a few different hoops +%% in order to integration-test it. +%% +%% * start/1, stop/0, init/3, terminate/2, delete_and_terminate/2 +%% - starting and stopping rabbit. durable queues / persistent msgs needed +%% to test recovery +%% +%% * publish/5, drain_confirmed/1, fetch/2, ack/2, is_duplicate/2, msg_rates/1, +%% needs_timeout/1, timeout/1, invoke/3, resume/1 [0] +%% - regular publishing and consuming, with confirms and acks and durability +%% +%% * publish_delivered/4 - publish with acks straight through +%% * discard/3 - publish without acks straight through +%% * dropwhile/2 - expire messages without DLX +%% * fetchwhile/4 - expire messages with DLX +%% * ackfold/4 - reject messages with DLX +%% * requeue/2 - reject messages without DLX +%% * drop/2 - maxlen messages without DLX +%% * purge/1 - issue AMQP queue.purge +%% * purge_acks/1 - mirror queue explicit sync with unacked msgs +%% * fold/3 - mirror queue explicit sync +%% * depth/1 - mirror queue implicit sync detection +%% * len/1, is_empty/1 - info items +%% * handle_pre_hibernate/1 - hibernation +%% +%% * set_ram_duration_target/2, ram_duration/1, status/1 +%% - maybe need unit testing? +%% +%% [0] publish enough to get credit flow from msg store + +recovery_test() -> + {Conn, Ch} = open(), + Q = <<"test">>, + declare(Ch, Q, 3), + publish(Ch, Q, [1, 2, 3, 1, 2, 3, 1, 2, 3]), + amqp_connection:close(Conn), + + %% TODO these break coverage + rabbit:stop(), + rabbit:start(), + + {Conn2, Ch2} = open(), + get_all(Ch2, Q, do_ack, [3, 3, 3, 2, 2, 2, 1, 1, 1]), + delete(Ch2, Q), + amqp_connection:close(Conn2), + passed. + +simple_order_test() -> + {Conn, Ch} = open(), + Q = <<"test">>, + declare(Ch, Q, 3), + publish(Ch, Q, [1, 2, 3, 1, 2, 3, 1, 2, 3]), + get_all(Ch, Q, do_ack, [3, 3, 3, 2, 2, 2, 1, 1, 1]), + publish(Ch, Q, [2, 3, 1, 2, 3, 1, 2, 3, 1]), + get_all(Ch, Q, no_ack, [3, 3, 3, 2, 2, 2, 1, 1, 1]), + publish(Ch, Q, [3, 1, 2, 3, 1, 2, 3, 1, 2]), + get_all(Ch, Q, do_ack, [3, 3, 3, 2, 2, 2, 1, 1, 1]), + delete(Ch, Q), + amqp_connection:close(Conn), + passed. + +matching_test() -> + {Conn, Ch} = open(), + Q = <<"test">>, + declare(Ch, Q, 5), + %% We round priority down, and 0 is the default + publish(Ch, Q, [undefined, 0, 5, 10, undefined]), + get_all(Ch, Q, do_ack, [5, 10, undefined, 0, undefined]), + delete(Ch, Q), + amqp_connection:close(Conn), + passed. + +resume_test() -> + {Conn, Ch} = open(), + Q = <<"test">>, + declare(Ch, Q, 5), + amqp_channel:call(Ch, #'confirm.select'{}), + publish_many(Ch, Q, 10000), + amqp_channel:wait_for_confirms(Ch), + amqp_channel:call(Ch, #'queue.purge'{queue = Q}), %% Assert it exists + delete(Ch, Q), + amqp_connection:close(Conn), + passed. + +straight_through_test() -> + {Conn, Ch} = open(), + Q = <<"test">>, + declare(Ch, Q, 3), + [begin + consume(Ch, Q, Ack), + [begin + publish1(Ch, Q, P), + assert_delivered(Ch, Ack, P) + end || P <- [1, 2, 3]], + cancel(Ch) + end || Ack <- [do_ack, no_ack]], + get_empty(Ch, Q), + delete(Ch, Q), + amqp_connection:close(Conn), + passed. + +dropwhile_fetchwhile_test() -> + {Conn, Ch} = open(), + Q = <<"test">>, + [begin + declare(Ch, Q, Args ++ arguments(3)), + publish(Ch, Q, [1, 2, 3, 1, 2, 3, 1, 2, 3]), + timer:sleep(10), + get_empty(Ch, Q), + delete(Ch, Q) + end || + Args <- [[{<<"x-message-ttl">>, long, 1}], + [{<<"x-message-ttl">>, long, 1}, + {<<"x-dead-letter-exchange">>, longstr, <<"amq.fanout">>}] + ]], + amqp_connection:close(Conn), + passed. + +ackfold_test() -> + {Conn, Ch} = open(), + Q = <<"test">>, + Q2 = <<"test2">>, + declare(Ch, Q, + [{<<"x-dead-letter-exchange">>, longstr, <<>>}, + {<<"x-dead-letter-routing-key">>, longstr, Q2} + | arguments(3)]), + declare(Ch, Q2, none), + publish(Ch, Q, [1, 2, 3]), + [_, _, DTag] = get_all(Ch, Q, manual_ack, [3, 2, 1]), + amqp_channel:cast(Ch, #'basic.nack'{delivery_tag = DTag, + multiple = true, + requeue = false}), + timer:sleep(100), + get_all(Ch, Q2, do_ack, [3, 2, 1]), + delete(Ch, Q), + delete(Ch, Q2), + amqp_connection:close(Conn), + passed. + +requeue_test() -> + {Conn, Ch} = open(), + Q = <<"test">>, + declare(Ch, Q, 3), + publish(Ch, Q, [1, 2, 3]), + [_, _, DTag] = get_all(Ch, Q, manual_ack, [3, 2, 1]), + amqp_channel:cast(Ch, #'basic.nack'{delivery_tag = DTag, + multiple = true, + requeue = true}), + get_all(Ch, Q, do_ack, [3, 2, 1]), + delete(Ch, Q), + amqp_connection:close(Conn), + passed. + +drop_test() -> + {Conn, Ch} = open(), + Q = <<"test">>, + declare(Ch, Q, [{<<"x-max-length">>, long, 4} | arguments(3)]), + publish(Ch, Q, [1, 2, 3, 1, 2, 3, 1, 2, 3]), + %% We drop from the head, so this is according to the "spec" even + %% if not likely to be what the user wants. + get_all(Ch, Q, do_ack, [2, 1, 1, 1]), + delete(Ch, Q), + amqp_connection:close(Conn), + passed. + +purge_test() -> + {Conn, Ch} = open(), + Q = <<"test">>, + declare(Ch, Q, 3), + publish(Ch, Q, [1, 2, 3]), + amqp_channel:call(Ch, #'queue.purge'{queue = Q}), + get_empty(Ch, Q), + delete(Ch, Q), + amqp_connection:close(Conn), + passed. + +ram_duration_test() -> + QName = rabbit_misc:r(<<"/">>, queue, <<"pseudo">>), + Q0 = rabbit_amqqueue:pseudo_queue(QName, self()), + Q = Q0#amqqueue{arguments = [{<<"x-max-priority">>, long, 5}]}, + PQ = rabbit_priority_queue, + BQS1 = PQ:init(Q, new, fun(_, _) -> ok end), + {_Duration1, BQS2} = PQ:ram_duration(BQS1), + BQS3 = PQ:set_ram_duration_target(infinity, BQS2), + BQS4 = PQ:set_ram_duration_target(1, BQS3), + {_Duration2, BQS5} = PQ:ram_duration(BQS4), + PQ:delete_and_terminate(a_whim, BQS5), + passed. + +mirror_queue_sync_with() -> cluster_ab. +mirror_queue_sync([CfgA, _CfgB]) -> + Ch = pget(channel, CfgA), + Q = <<"test">>, + declare(Ch, Q, 3), + publish(Ch, Q, [1, 2, 3]), + ok = rabbit_test_util:set_ha_policy(CfgA, <<".*">>, <<"all">>), + publish(Ch, Q, [1, 2, 3, 1, 2, 3]), + %% master now has 9, slave 6. + get_partial(Ch, Q, manual_ack, [3, 3, 3, 2, 2, 2]), + %% So some but not all are unacked at the slave + rabbit_test_util:control_action(sync_queue, CfgA, [binary_to_list(Q)], + [{"-p", "/"}]), + wait_for_sync(CfgA, rabbit_misc:r(<<"/">>, queue, Q)), + passed. + +%%---------------------------------------------------------------------------- + +open() -> + {ok, Conn} = amqp_connection:start(#amqp_params_network{}), + {ok, Ch} = amqp_connection:open_channel(Conn), + {Conn, Ch}. + +declare(Ch, Q, Args) when is_list(Args) -> + amqp_channel:call(Ch, #'queue.declare'{queue = Q, + durable = true, + arguments = Args}); +declare(Ch, Q, Max) -> + declare(Ch, Q, arguments(Max)). + +delete(Ch, Q) -> + amqp_channel:call(Ch, #'queue.delete'{queue = Q}). + +publish(Ch, Q, Ps) -> + amqp_channel:call(Ch, #'confirm.select'{}), + [publish1(Ch, Q, P) || P <- Ps], + amqp_channel:wait_for_confirms(Ch). + +publish_many(_Ch, _Q, 0) -> ok; +publish_many( Ch, Q, N) -> publish1(Ch, Q, random:uniform(5)), + publish_many(Ch, Q, N - 1). + +publish1(Ch, Q, P) -> + amqp_channel:cast(Ch, #'basic.publish'{routing_key = Q}, + #amqp_msg{props = props(P), + payload = priority2bin(P)}). + +props(undefined) -> #'P_basic'{delivery_mode = 2}; +props(P) -> #'P_basic'{priority = P, + delivery_mode = 2}. + +consume(Ch, Q, Ack) -> + amqp_channel:subscribe(Ch, #'basic.consume'{queue = Q, + no_ack = Ack =:= no_ack, + consumer_tag = <<"ctag">>}, + self()), + receive + #'basic.consume_ok'{consumer_tag = <<"ctag">>} -> + ok + end. + +cancel(Ch) -> + amqp_channel:call(Ch, #'basic.cancel'{consumer_tag = <<"ctag">>}). + +assert_delivered(Ch, Ack, P) -> + PBin = priority2bin(P), + receive + {#'basic.deliver'{delivery_tag = DTag}, #amqp_msg{payload = PBin2}} -> + ?assertEqual(PBin, PBin2), + maybe_ack(Ch, Ack, DTag) + end. + +get_all(Ch, Q, Ack, Ps) -> + DTags = get_partial(Ch, Q, Ack, Ps), + get_empty(Ch, Q), + DTags. + +get_partial(Ch, Q, Ack, Ps) -> + [get_ok(Ch, Q, Ack, P) || P <- Ps]. + +get_empty(Ch, Q) -> + #'basic.get_empty'{} = amqp_channel:call(Ch, #'basic.get'{queue = Q}). + +get_ok(Ch, Q, Ack, P) -> + PBin = priority2bin(P), + {#'basic.get_ok'{delivery_tag = DTag}, #amqp_msg{payload = PBin2}} = + amqp_channel:call(Ch, #'basic.get'{queue = Q, + no_ack = Ack =:= no_ack}), + ?assertEqual(PBin, PBin2), + maybe_ack(Ch, Ack, DTag). + +maybe_ack(Ch, do_ack, DTag) -> + amqp_channel:cast(Ch, #'basic.ack'{delivery_tag = DTag}), + DTag; +maybe_ack(_Ch, _, DTag) -> + DTag. + +arguments(none) -> []; +arguments(Max) -> [{<<"x-max-priority">>, byte, Max}]. + +priority2bin(undefined) -> <<"undefined">>; +priority2bin(Int) -> list_to_binary(integer_to_list(Int)). + +%%---------------------------------------------------------------------------- + +wait_for_sync(Cfg, Q) -> + case synced(Cfg, Q) of + true -> ok; + false -> timer:sleep(100), + wait_for_sync(Cfg, Q) + end. + +synced(Cfg, Q) -> + Info = rpc:call(pget(node, Cfg), + rabbit_amqqueue, info_all, + [<<"/">>, [name, synchronised_slave_pids]]), + [SSPids] = [Pids || [{name, Q1}, {synchronised_slave_pids, Pids}] <- Info, + Q =:= Q1], + length(SSPids) =:= 1. + +%%---------------------------------------------------------------------------- diff --git a/rabbitmq-server/plugins-src/rabbitmq-test/test/src/simple_ha.erl b/rabbitmq-server/plugins-src/rabbitmq-test/test/src/simple_ha.erl new file mode 100644 index 0000000..389ff23 --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-test/test/src/simple_ha.erl @@ -0,0 +1,143 @@ +%% The contents of this file are subject to the Mozilla Public License +%% Version 1.1 (the "License"); you may not use this file except in +%% compliance with the License. You may obtain a copy of the License +%% at http://www.mozilla.org/MPL/ +%% +%% Software distributed under the License is distributed on an "AS IS" +%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See +%% the License for the specific language governing rights and +%% limitations under the License. +%% +%% The Original Code is RabbitMQ. +%% +%% The Initial Developer of the Original Code is GoPivotal, Inc. +%% Copyright (c) 2007-2014 GoPivotal, Inc. All rights reserved. +%% +-module(simple_ha). + +-compile(export_all). +-include_lib("eunit/include/eunit.hrl"). +-include_lib("amqp_client/include/amqp_client.hrl"). + +-import(rabbit_test_util, [set_ha_policy/3, a2b/1]). +-import(rabbit_misc, [pget/2]). + +-define(CONFIG, [cluster_abc, ha_policy_all]). + +rapid_redeclare_with() -> [cluster_ab, ha_policy_all]. +rapid_redeclare([CfgA | _]) -> + Ch = pget(channel, CfgA), + Queue = <<"test">>, + [begin + amqp_channel:call(Ch, #'queue.declare'{queue = Queue, + durable = true}), + amqp_channel:call(Ch, #'queue.delete'{queue = Queue}) + end || _I <- lists:seq(1, 20)], + ok. + +%% Check that by the time we get a declare-ok back, the slaves are up +%% and in Mnesia. +declare_synchrony_with() -> [cluster_ab, ha_policy_all]. +declare_synchrony([Rabbit, Hare]) -> + RabbitCh = pget(channel, Rabbit), + HareCh = pget(channel, Hare), + Q = <<"mirrored-queue">>, + declare(RabbitCh, Q), + amqp_channel:call(RabbitCh, #'confirm.select'{}), + amqp_channel:cast(RabbitCh, #'basic.publish'{routing_key = Q}, + #amqp_msg{props = #'P_basic'{delivery_mode = 2}}), + amqp_channel:wait_for_confirms(RabbitCh), + _Rabbit2 = rabbit_test_configs:kill_node(Rabbit), + + #'queue.declare_ok'{message_count = 1} = declare(HareCh, Q), + ok. + +declare(Ch, Name) -> + amqp_channel:call(Ch, #'queue.declare'{durable = true, queue = Name}). + +consume_survives_stop_with() -> ?CONFIG. +consume_survives_sigkill_with() -> ?CONFIG. +consume_survives_policy_with() -> ?CONFIG. +auto_resume_with() -> ?CONFIG. +auto_resume_no_ccn_client_with() -> ?CONFIG. + +consume_survives_stop(Cf) -> consume_survives(Cf, fun stop/2, true). +consume_survives_sigkill(Cf) -> consume_survives(Cf, fun sigkill/2, true). +consume_survives_policy(Cf) -> consume_survives(Cf, fun policy/2, true). +auto_resume(Cf) -> consume_survives(Cf, fun sigkill/2, false). +auto_resume_no_ccn_client(Cf) -> consume_survives(Cf, fun sigkill/2, false, + false). + +confirms_survive_stop_with() -> ?CONFIG. +confirms_survive_sigkill_with() -> ?CONFIG. +confirms_survive_policy_with() -> ?CONFIG. + +confirms_survive_stop(Cf) -> confirms_survive(Cf, fun stop/2). +confirms_survive_sigkill(Cf) -> confirms_survive(Cf, fun sigkill/2). +confirms_survive_policy(Cf) -> confirms_survive(Cf, fun policy/2). + +%%---------------------------------------------------------------------------- + +consume_survives(Nodes, DeathFun, CancelOnFailover) -> + consume_survives(Nodes, DeathFun, CancelOnFailover, true). + +consume_survives([CfgA, CfgB, CfgC] = Nodes, + DeathFun, CancelOnFailover, CCNSupported) -> + Msgs = rabbit_test_configs:cover_work_factor(20000, CfgA), + Channel1 = pget(channel, CfgA), + Channel2 = pget(channel, CfgB), + Channel3 = pget(channel, CfgC), + + %% declare the queue on the master, mirrored to the two slaves + Queue = <<"test">>, + amqp_channel:call(Channel1, #'queue.declare'{queue = Queue, + auto_delete = false}), + + %% start up a consumer + ConsCh = case CCNSupported of + true -> Channel2; + false -> open_incapable_channel(pget(port, CfgB)) + end, + ConsumerPid = rabbit_ha_test_consumer:create( + ConsCh, Queue, self(), CancelOnFailover, Msgs), + + %% send a bunch of messages from the producer + ProducerPid = rabbit_ha_test_producer:create(Channel3, Queue, + self(), false, Msgs), + DeathFun(CfgA, Nodes), + %% verify that the consumer got all msgs, or die - the await_response + %% calls throw an exception if anything goes wrong.... + rabbit_ha_test_consumer:await_response(ConsumerPid), + rabbit_ha_test_producer:await_response(ProducerPid), + ok. + +confirms_survive([CfgA, CfgB, _CfgC] = Nodes, DeathFun) -> + Msgs = rabbit_test_configs:cover_work_factor(20000, CfgA), + Node1Channel = pget(channel, CfgA), + Node2Channel = pget(channel, CfgB), + + %% declare the queue on the master, mirrored to the two slaves + Queue = <<"test">>, + amqp_channel:call(Node1Channel,#'queue.declare'{queue = Queue, + auto_delete = false, + durable = true}), + + %% send a bunch of messages from the producer + ProducerPid = rabbit_ha_test_producer:create(Node2Channel, Queue, + self(), true, Msgs), + DeathFun(CfgA, Nodes), + rabbit_ha_test_producer:await_response(ProducerPid), + ok. + +stop(Cfg, _Cfgs) -> rabbit_test_util:kill_after(50, Cfg, stop). +sigkill(Cfg, _Cfgs) -> rabbit_test_util:kill_after(50, Cfg, sigkill). +policy(Cfg, [_|T]) -> Nodes = [a2b(pget(node, C)) || C <- T], + set_ha_policy(Cfg, <<".*">>, {<<"nodes">>, Nodes}). + +open_incapable_channel(NodePort) -> + Props = [{<<"capabilities">>, table, []}], + {ok, ConsConn} = + amqp_connection:start(#amqp_params_network{port = NodePort, + client_properties = Props}), + {ok, Ch} = amqp_connection:open_channel(ConsConn), + Ch. diff --git a/rabbitmq-server/plugins-src/rabbitmq-test/test/src/sync_detection.erl b/rabbitmq-server/plugins-src/rabbitmq-test/test/src/sync_detection.erl new file mode 100644 index 0000000..18f6f5d --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-test/test/src/sync_detection.erl @@ -0,0 +1,189 @@ +%% The contents of this file are subject to the Mozilla Public License +%% Version 1.1 (the "License"); you may not use this file except in +%% compliance with the License. You may obtain a copy of the License +%% at http://www.mozilla.org/MPL/ +%% +%% Software distributed under the License is distributed on an "AS IS" +%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See +%% the License for the specific language governing rights and +%% limitations under the License. +%% +%% The Original Code is RabbitMQ. +%% +%% The Initial Developer of the Original Code is GoPivotal, Inc. +%% Copyright (c) 2007-2014 GoPivotal, Inc. All rights reserved. +%% +-module(sync_detection). + +-compile(export_all). +-include_lib("eunit/include/eunit.hrl"). +-include_lib("amqp_client/include/amqp_client.hrl"). + +-import(rabbit_test_util, [stop_app/1, start_app/1]). +-import(rabbit_misc, [pget/2]). + +-define(LOOP_RECURSION_DELAY, 100). + +slave_synchronization_with() -> [cluster_ab, ha_policy_two_pos]. +slave_synchronization([Master, Slave]) -> + Channel = pget(channel, Master), + Queue = <<"ha.two.test">>, + #'queue.declare_ok'{} = + amqp_channel:call(Channel, #'queue.declare'{queue = Queue, + auto_delete = false}), + + %% The comments on the right are the queue length and the pending acks on + %% the master. + stop_app(Slave), + + %% We get and ack one message when the slave is down, and check that when we + %% start the slave it's not marked as synced until ack the message. We also + %% publish another message when the slave is up. + send_dummy_message(Channel, Queue), % 1 - 0 + {#'basic.get_ok'{delivery_tag = Tag1}, _} = + amqp_channel:call(Channel, #'basic.get'{queue = Queue}), % 0 - 1 + + start_app(Slave), + + slave_unsynced(Master, Queue), + send_dummy_message(Channel, Queue), % 1 - 1 + slave_unsynced(Master, Queue), + + amqp_channel:cast(Channel, #'basic.ack'{delivery_tag = Tag1}), % 1 - 0 + + slave_synced(Master, Queue), + + %% We restart the slave and we send a message, so that the slave will only + %% have one of the messages. + stop_app(Slave), + start_app(Slave), + + send_dummy_message(Channel, Queue), % 2 - 0 + + slave_unsynced(Master, Queue), + + %% We reject the message that the slave doesn't have, and verify that it's + %% still unsynced + {#'basic.get_ok'{delivery_tag = Tag2}, _} = + amqp_channel:call(Channel, #'basic.get'{queue = Queue}), % 1 - 1 + slave_unsynced(Master, Queue), + amqp_channel:cast(Channel, #'basic.reject'{ delivery_tag = Tag2, + requeue = true }), % 2 - 0 + slave_unsynced(Master, Queue), + {#'basic.get_ok'{delivery_tag = Tag3}, _} = + amqp_channel:call(Channel, #'basic.get'{queue = Queue}), % 1 - 1 + amqp_channel:cast(Channel, #'basic.ack'{delivery_tag = Tag3}), % 1 - 0 + slave_synced(Master, Queue), + {#'basic.get_ok'{delivery_tag = Tag4}, _} = + amqp_channel:call(Channel, #'basic.get'{queue = Queue}), % 0 - 1 + amqp_channel:cast(Channel, #'basic.ack'{delivery_tag = Tag4}), % 0 - 0 + slave_synced(Master, Queue). + +slave_synchronization_ttl_with() -> [cluster_abc, ha_policy_two_pos]. +slave_synchronization_ttl([Master, Slave, DLX]) -> + Channel = pget(channel, Master), + DLXChannel = pget(channel, DLX), + + %% We declare a DLX queue to wait for messages to be TTL'ed + DLXQueue = <<"dlx-queue">>, + #'queue.declare_ok'{} = + amqp_channel:call(Channel, #'queue.declare'{queue = DLXQueue, + auto_delete = false}), + + TestMsgTTL = 5000, + Queue = <<"ha.two.test">>, + %% Sadly we need fairly high numbers for the TTL because starting/stopping + %% nodes takes a fair amount of time. + Args = [{<<"x-message-ttl">>, long, TestMsgTTL}, + {<<"x-dead-letter-exchange">>, longstr, <<>>}, + {<<"x-dead-letter-routing-key">>, longstr, DLXQueue}], + #'queue.declare_ok'{} = + amqp_channel:call(Channel, #'queue.declare'{queue = Queue, + auto_delete = false, + arguments = Args}), + + slave_synced(Master, Queue), + + %% All unknown + stop_app(Slave), + send_dummy_message(Channel, Queue), + send_dummy_message(Channel, Queue), + start_app(Slave), + slave_unsynced(Master, Queue), + wait_for_messages(DLXQueue, DLXChannel, 2), + slave_synced(Master, Queue), + + %% 1 unknown, 1 known + stop_app(Slave), + send_dummy_message(Channel, Queue), + start_app(Slave), + slave_unsynced(Master, Queue), + send_dummy_message(Channel, Queue), + slave_unsynced(Master, Queue), + wait_for_messages(DLXQueue, DLXChannel, 2), + slave_synced(Master, Queue), + + %% %% both known + send_dummy_message(Channel, Queue), + send_dummy_message(Channel, Queue), + slave_synced(Master, Queue), + wait_for_messages(DLXQueue, DLXChannel, 2), + slave_synced(Master, Queue), + + ok. + +send_dummy_message(Channel, Queue) -> + Payload = <<"foo">>, + Publish = #'basic.publish'{exchange = <<>>, routing_key = Queue}, + amqp_channel:cast(Channel, Publish, #amqp_msg{payload = Payload}). + +slave_pids(Node, Queue) -> + {ok, Q} = rpc:call(Node, rabbit_amqqueue, lookup, + [rabbit_misc:r(<<"/">>, queue, Queue)]), + SSP = synchronised_slave_pids, + [{SSP, Pids}] = rpc:call(Node, rabbit_amqqueue, info, [Q, [SSP]]), + case Pids of + '' -> []; + _ -> Pids + end. + +%% The mnesia syncronization takes a while, but we don't want to wait for the +%% test to fail, since the timetrap is quite high. +wait_for_sync_status(Status, Cfg, Queue) -> + Max = 10000 / ?LOOP_RECURSION_DELAY, + wait_for_sync_status(0, Max, Status, pget(node, Cfg), Queue). + +wait_for_sync_status(N, Max, Status, Node, Queue) when N >= Max -> + erlang:error({sync_status_max_tries_failed, + [{queue, Queue}, + {node, Node}, + {expected_status, Status}, + {max_tried, Max}]}); +wait_for_sync_status(N, Max, Status, Node, Queue) -> + Synced = length(slave_pids(Node, Queue)) =:= 1, + case Synced =:= Status of + true -> ok; + false -> timer:sleep(?LOOP_RECURSION_DELAY), + wait_for_sync_status(N + 1, Max, Status, Node, Queue) + end. + +slave_synced(Cfg, Queue) -> + wait_for_sync_status(true, Cfg, Queue). + +slave_unsynced(Cfg, Queue) -> + wait_for_sync_status(false, Cfg, Queue). + +wait_for_messages(Queue, Channel, N) -> + Sub = #'basic.consume'{queue = Queue}, + #'basic.consume_ok'{consumer_tag = CTag} = amqp_channel:call(Channel, Sub), + receive + #'basic.consume_ok'{} -> ok + end, + lists:foreach( + fun (_) -> receive + {#'basic.deliver'{delivery_tag = Tag}, _Content} -> + amqp_channel:cast(Channel, + #'basic.ack'{delivery_tag = Tag}) + end + end, lists:seq(1, N)), + amqp_channel:call(Channel, #'basic.cancel'{consumer_tag = CTag}). diff --git a/rabbitmq-server/plugins-src/rabbitmq-tracing/.srcdist_done b/rabbitmq-server/plugins-src/rabbitmq-tracing/.srcdist_done new file mode 100644 index 0000000..e69de29 diff --git a/rabbitmq-server/plugins-src/rabbitmq-tracing/CONTRIBUTING.md b/rabbitmq-server/plugins-src/rabbitmq-tracing/CONTRIBUTING.md new file mode 100644 index 0000000..69a4b4a --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-tracing/CONTRIBUTING.md @@ -0,0 +1,51 @@ +## Overview + +RabbitMQ projects use pull requests to discuss, collaborate on and accept code contributions. +Pull requests is the primary place of discussing code changes. + +## How to Contribute + +The process is fairly standard: + + * Fork the repository or repositories you plan on contributing to + * Clone [RabbitMQ umbrella repository](https://github.com/rabbitmq/rabbitmq-public-umbrella) + * `cd umbrella`, `make co` + * Create a branch with a descriptive name in the relevant repositories + * Make your changes, run tests, commit with a [descriptive message](http://tbaggery.com/2008/04/19/a-note-about-git-commit-messages.html), push to your fork + * Submit pull requests with an explanation what has been changed and **why** + * Submit a filled out and signed [Contributor Agreement](https://github.com/rabbitmq/ca#how-to-submit) if needed (see below) + * Be patient. We will get to your pull request eventually + +If what you are going to work on is a substantial change, please first ask the core team +of their opinion on [RabbitMQ mailing list](https://groups.google.com/forum/#!forum/rabbitmq-users). + + +## (Brief) Code of Conduct + +In one line: don't be a dick. + +Be respectful to the maintainers and other contributors. Open source +contributors put long hours into developing projects and doing user +support. Those projects and user support are available for free. We +believe this deserves some respect. + +Be respectful to people of all races, genders, religious beliefs and +political views. Regardless of how brilliant a pull request is +technically, we will not tolerate disrespectful or aggressive +behaviour. + +Contributors who violate this straightforward Code of Conduct will see +their pull requests closed and locked. + + +## Contributor Agreement + +If you want to contribute a non-trivial change, please submit a signed copy of our +[Contributor Agreement](https://github.com/rabbitmq/ca#how-to-submit) around the time +you submit your pull request. This will make it much easier (in some cases, possible) +for the RabbitMQ team at Pivotal to merge your contribution. + + +## Where to Ask Questions + +If something isn't clear, feel free to ask on our [mailing list](https://groups.google.com/forum/#!forum/rabbitmq-users). diff --git a/rabbitmq-server/plugins-src/rabbitmq-tracing/Makefile b/rabbitmq-server/plugins-src/rabbitmq-tracing/Makefile new file mode 100644 index 0000000..482105a --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-tracing/Makefile @@ -0,0 +1 @@ +include ../umbrella.mk diff --git a/rabbitmq-server/plugins-src/rabbitmq-tracing/README b/rabbitmq-server/plugins-src/rabbitmq-tracing/README new file mode 100644 index 0000000..61b5ae4 --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-tracing/README @@ -0,0 +1,42 @@ +An opinionated tracing plugin for RabbitMQ management. Build it like +any other plugin. After installation you should see a "Tracing" tab in +the management UI. Hopefully use is obvious. + +Configuration +============= + +There is one configuration option: + +directory: This controls where the log files go. It defaults to +"/var/tmp/rabbitmq-tracing". + +Performance +=========== + +On my workstation, rabbitmq-tracing can write about 2000 msg/s to a +log file. You should be careful using rabbitmq-tracing if you think +you're going to capture more messages than this. Of course, any +messages that can't be logged are queued. + +The code to serve up the log files over HTTP is pretty dumb, it loads +the whole log into memory. If you have large log files you may wish +to transfer them off the server in some other way. + +HTTP API +======== + +GET /api/traces +GET /api/traces/ +GET PUT DELETE /api/traces// +GET /api/trace-files +GET DELETE /api/trace-files/ (GET returns the file as text/plain, + not JSON describing it.) + +Example for how to create a trace: + +$ curl -i -u guest:guest -H "content-type:application/json" -XPUT \ + http://localhost:55672/api/traces/%2f/my-trace \ + -d'{"format":"text","pattern":"#", "max_payload_bytes":1000}' + +max_payload_bytes is optional (omit it to prevent payload truncation), +format and pattern are mandatory. \ No newline at end of file diff --git a/rabbitmq-server/plugins-src/rabbitmq-tracing/package.mk b/rabbitmq-server/plugins-src/rabbitmq-tracing/package.mk new file mode 100644 index 0000000..58341bb --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-tracing/package.mk @@ -0,0 +1,8 @@ +RELEASABLE:=true +DEPS:=rabbitmq-management +WITH_BROKER_TEST_COMMANDS:=eunit:test(rabbit_tracing_test,[verbose]) + +CONSTRUCT_APP_PREREQS:=$(shell find $(PACKAGE_DIR)/priv -type f) +define construct_app_commands + cp -r $(PACKAGE_DIR)/priv $(APP_DIR) +endef diff --git a/rabbitmq-server/plugins-src/rabbitmq-tracing/priv/www/js/tmpl/traces.ejs b/rabbitmq-server/plugins-src/rabbitmq-tracing/priv/www/js/tmpl/traces.ejs new file mode 100644 index 0000000..470527d --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-tracing/priv/www/js/tmpl/traces.ejs @@ -0,0 +1,153 @@ +

Traces

+
+

All traces

+
+ + + + + +
+

Currently running traces

+ <% if (traces.length > 0) { %> + + + + <% if (vhosts_interesting) { %> + + <% } %> + + + + + + + + + + + <% + for (var i = 0; i < traces.length; i++) { + var trace = traces[i]; + %> + > + <% if (vhosts_interesting) { %> + + <% } %> + + + + + <% if (trace.queue) { %> + + + <% } else { %> + + <% } %> + + + <% } %> + +
Virtual hostNamePatternFormatPayload limitRateQueued
<%= fmt_string(trace.vhost) %><%= fmt_string(trace.name) %><%= fmt_string(trace.pattern) %><%= fmt_string(trace.format) %><%= fmt_string(trace.max_payload_bytes, 'Unlimited') %> + <%= fmt_detail_rate(trace.queue.message_stats, 'deliver_no_ack') %> + + <%= trace.queue.messages %> + <%= link_trace_queue(trace) %> + +
FAILED
+
+
+ + + +
+
+ <% } else { %> +

... no traces running ...

+ <% } %> +
+

Trace log files

+ <% if (files.length > 0) { %> + + + + + + + + + + <% + for (var i = 0; i < files.length; i++) { + var file = files[i]; + %> + > + + + + + <% } %> + +
NameSize
<%= link_trace(file.name) %><%= fmt_bytes(file.size) %> +
+ + +
+
+ <% } else { %> +

... no files ...

+ <% } %> +
+
+
+ +
+

Add a new trace

+
+
+ +<% if (vhosts_interesting) { %> + + + + +<% } else { %> + +<% } %> + + + + + + + + + + + + + + + + +
+ +
*
+ +
+ +
+ + Examples: #, publish.#, deliver.# #.amq.direct, #.myqueue +
+ +
+
+
diff --git a/rabbitmq-server/plugins-src/rabbitmq-tracing/priv/www/js/tracing.js b/rabbitmq-server/plugins-src/rabbitmq-tracing/priv/www/js/tracing.js new file mode 100644 index 0000000..73c8b09 --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-tracing/priv/www/js/tracing.js @@ -0,0 +1,48 @@ +dispatcher_add(function(sammy) { + sammy.get('#/traces', function() { + render({'traces': '/traces', + 'vhosts': '/vhosts', + 'files': '/trace-files'}, + 'traces', '#/traces'); + }); + sammy.get('#/traces/:vhost/:name', function() { + var path = '/traces/' + esc(this.params['vhost']) + '/' + esc(this.params['name']); + render({'trace': path}, + 'trace', '#/traces'); + }); + sammy.put('#/traces', function() { + if (this.params['max_payload_bytes'] === '') { + delete this.params['max_payload_bytes']; + } + else { + this.params['max_payload_bytes'] = + parseInt(this.params['max_payload_bytes']); + } + if (sync_put(this, '/traces/:vhost/:name')) + update(); + return false; + }); + sammy.del('#/traces', function() { + if (sync_delete(this, '/traces/:vhost/:name')) + partial_update(); + return false; + }); + sammy.del('#/trace-files', function() { + if (sync_delete(this, '/trace-files/:name')) + partial_update(); + return false; + }); +}); + +NAVIGATION['Admin'][0]['Tracing'] = ['#/traces', 'administrator']; + +HELP['tracing-max-payload'] = + 'Maximum size of payload to log, in bytes. Payloads larger than this limit will be truncated. Leave blank to prevent truncation. Set to 0 to prevent logging of payload altogether.'; + +function link_trace(name) { + return _link_to(name, 'api/trace-files/' + esc(name)); +} + +function link_trace_queue(trace) { + return _link_to('(queue)', '#/queues/' + esc(trace.vhost) + '/' + esc(trace.queue.name)); +} diff --git a/rabbitmq-server/plugins-src/rabbitmq-tracing/src/rabbit_tracing_app.erl b/rabbitmq-server/plugins-src/rabbitmq-tracing/src/rabbit_tracing_app.erl new file mode 100644 index 0000000..815855b --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-tracing/src/rabbit_tracing_app.erl @@ -0,0 +1,26 @@ +%% The contents of this file are subject to the Mozilla Public License +%% Version 1.1 (the "License"); you may not use this file except in +%% compliance with the License. You may obtain a copy of the License +%% at http://www.mozilla.org/MPL/ +%% +%% Software distributed under the License is distributed on an "AS IS" +%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See +%% the License for the specific language governing rights and +%% limitations under the License. +%% +%% The Original Code is RabbitMQ. +%% +%% The Initial Developer of the Original Code is GoPivotal, Inc. +%% Copyright (c) 2007-2015 Pivotal Software, Inc. All rights reserved. +%% + +-module(rabbit_tracing_app). + +-behaviour(application). +-export([start/2, stop/1]). + +start(_Type, _StartArgs) -> + rabbit_tracing_sup:start_link(). + +stop(_State) -> + ok. diff --git a/rabbitmq-server/plugins-src/rabbitmq-tracing/src/rabbit_tracing_consumer.erl b/rabbitmq-server/plugins-src/rabbitmq-tracing/src/rabbit_tracing_consumer.erl new file mode 100644 index 0000000..ca2273b --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-tracing/src/rabbit_tracing_consumer.erl @@ -0,0 +1,231 @@ +%% The contents of this file are subject to the Mozilla Public License +%% Version 1.1 (the "License"); you may not use this file except in +%% compliance with the License. You may obtain a copy of the License +%% at http://www.mozilla.org/MPL/ +%% +%% Software distributed under the License is distributed on an "AS IS" +%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See +%% the License for the specific language governing rights and +%% limitations under the License. +%% +%% The Original Code is RabbitMQ Federation. +%% +%% The Initial Developer of the Original Code is GoPivotal, Inc. +%% Copyright (c) 2007-2015 Pivotal Software, Inc. All rights reserved. +%% + +-module(rabbit_tracing_consumer). + +-behaviour(gen_server). + +-include_lib("amqp_client/include/amqp_client.hrl"). + +-import(rabbit_misc, [pget/2, pget/3, table_lookup/2]). + +-record(state, {conn, ch, vhost, queue, file, filename, format, buf, buf_cnt, + max_payload}). +-record(log_record, {timestamp, type, exchange, queue, node, connection, + vhost, username, channel, routing_keys, routed_queues, + properties, payload}). + +-define(X, <<"amq.rabbitmq.trace">>). +-define(MAX_BUF, 100). + +-export([start_link/1, info_all/1]). +-export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, + code_change/3]). + +start_link(Args) -> + gen_server:start_link(?MODULE, Args, []). + +info_all(Pid) -> + gen_server:call(Pid, info_all, infinity). + +%%---------------------------------------------------------------------------- + +init(Args) -> + process_flag(trap_exit, true), + Name = pget(name, Args), + VHost = pget(vhost, Args), + MaxPayload = pget(max_payload_bytes, Args, unlimited), + {ok, Conn} = amqp_connection:start( + #amqp_params_direct{virtual_host = VHost}), + link(Conn), + {ok, Ch} = amqp_connection:open_channel(Conn), + link(Ch), + #'queue.declare_ok'{queue = Q} = + amqp_channel:call(Ch, #'queue.declare'{durable = false, + exclusive = true}), + #'queue.bind_ok'{} = + amqp_channel:call( + Ch, #'queue.bind'{exchange = ?X, queue = Q, + routing_key = pget(pattern, Args)}), + amqp_channel:enable_delivery_flow_control(Ch), + #'basic.consume_ok'{} = + amqp_channel:subscribe(Ch, #'basic.consume'{queue = Q, + no_ack = true}, self()), + {ok, Dir} = application:get_env(directory), + Filename = Dir ++ "/" ++ binary_to_list(Name) ++ ".log", + case filelib:ensure_dir(Filename) of + ok -> + case prim_file:open(Filename, [append]) of + {ok, F} -> + rabbit_tracing_traces:announce(VHost, Name, self()), + Format = list_to_atom(binary_to_list(pget(format, Args))), + rabbit_log:info("Tracer opened log file ~p with " + "format ~p~n", [Filename, Format]), + {ok, #state{conn = Conn, ch = Ch, vhost = VHost, queue = Q, + file = F, filename = Filename, + format = Format, buf = [], buf_cnt = 0, + max_payload = MaxPayload}}; + {error, E} -> + {stop, {could_not_open, Filename, E}} + end; + {error, E} -> + {stop, {could_not_create_dir, Dir, E}} + end. + +handle_call(info_all, _From, State = #state{vhost = V, queue = Q}) -> + [QInfo] = rabbit_mgmt_db:augment_queues( + [rabbit_mgmt_wm_queue:queue(V, Q)], + rabbit_mgmt_util:no_range(), basic), + {reply, [{queue, rabbit_mgmt_format:strip_pids(QInfo)}], State}; + +handle_call(_Req, _From, State) -> + {reply, unknown_request, State}. + +handle_cast(_C, State) -> + {noreply, State}. + +handle_info({BasicDeliver, Msg, DeliveryCtx}, + State = #state{format = Format}) -> + amqp_channel:notify_received(DeliveryCtx), + {noreply, log(Format, delivery_to_log_record({BasicDeliver, Msg}, State), + State), + 0}; + +handle_info(timeout, State) -> + {noreply, flush(State)}; + +handle_info(_I, State) -> + {noreply, State}. + +terminate(shutdown, State = #state{conn = Conn, ch = Ch, + file = F, filename = Filename}) -> + flush(State), + catch amqp_channel:close(Ch), + catch amqp_connection:close(Conn), + catch prim_file:close(F), + rabbit_log:info("Tracer closed log file ~p~n", [Filename]), + ok; + +terminate(_Reason, _State) -> + ok. + +code_change(_, State, _) -> {ok, State}. + +%%---------------------------------------------------------------------------- + +delivery_to_log_record({#'basic.deliver'{routing_key = Key}, + #amqp_msg{props = #'P_basic'{headers = H}, + payload = Payload}}, State) -> + {Type, Q, RQs} = case Key of + <<"publish.", _Rest/binary>> -> + {array, Qs} = table_lookup(H, <<"routed_queues">>), + {published, none, [Q || {_, Q} <- Qs]}; + <<"deliver.", Rest/binary>> -> + {received, Rest, none} + end, + {longstr, Node} = table_lookup(H, <<"node">>), + {longstr, X} = table_lookup(H, <<"exchange_name">>), + {array, Keys} = table_lookup(H, <<"routing_keys">>), + {table, Props} = table_lookup(H, <<"properties">>), + {longstr, Conn} = table_lookup(H, <<"connection">>), + {longstr, VHost} = table_lookup(H, <<"vhost">>), + {longstr, User} = table_lookup(H, <<"user">>), + {signedint, Chan} = table_lookup(H, <<"channel">>), + #log_record{timestamp = rabbit_mgmt_format:now_to_str_ms(os:timestamp()), + type = Type, + exchange = X, + queue = Q, + node = Node, + connection = Conn, + vhost = VHost, + username = User, + channel = Chan, + routing_keys = [K || {_, K} <- Keys], + routed_queues= RQs, + properties = Props, + payload = truncate(Payload, State)}. + +log(text, Record, State) -> + Fmt = "~n========================================" + "========================================~n~s: Message ~s~n~n" + "Node: ~s~nConnection: ~s~n" + "Virtual host: ~s~nUser: ~s~n" + "Channel: ~p~nExchange: ~s~n" + "Routing keys: ~p~n" ++ + case Record#log_record.queue of + none -> ""; + _ -> "Queue: ~s~n" + end ++ + case Record#log_record.routed_queues of + none -> ""; + _ -> "Routed queues: ~p~n" + end ++ + "Properties: ~p~nPayload: ~n~s~n", + Args = + [Record#log_record.timestamp, + Record#log_record.type, + Record#log_record.node, Record#log_record.connection, + Record#log_record.vhost, Record#log_record.username, + Record#log_record.channel, Record#log_record.exchange, + Record#log_record.routing_keys] ++ + case Record#log_record.queue of + none -> []; + Q -> [Q] + end ++ + case Record#log_record.routed_queues of + none -> []; + RQs -> [RQs] + end ++ + [Record#log_record.properties, Record#log_record.payload], + print_log(io_lib:format(Fmt, Args), State); + +log(json, Record, State) -> + print_log(mochijson2:encode( + [{timestamp, Record#log_record.timestamp}, + {type, Record#log_record.type}, + {node, Record#log_record.node}, + {connection, Record#log_record.connection}, + {vhost, Record#log_record.vhost}, + {user, Record#log_record.username}, + {channel, Record#log_record.channel}, + {exchange, Record#log_record.exchange}, + {queue, Record#log_record.queue}, + {routed_queues, Record#log_record.routed_queues}, + {routing_keys, Record#log_record.routing_keys}, + {properties, rabbit_mgmt_format:amqp_table( + Record#log_record.properties)}, + {payload, base64:encode(Record#log_record.payload)}]) + ++ "\n", + State). + +print_log(LogMsg, State = #state{buf = Buf, buf_cnt = BufCnt}) -> + maybe_flush(State#state{buf = [LogMsg | Buf], buf_cnt = BufCnt + 1}). + +maybe_flush(State = #state{buf_cnt = ?MAX_BUF}) -> + flush(State); +maybe_flush(State) -> + State. + +flush(State = #state{file = F, buf = Buf}) -> + prim_file:write(F, lists:reverse(Buf)), + State#state{buf = [], buf_cnt = 0}. + +truncate(Payload, #state{max_payload = Max}) -> + case Max =:= unlimited orelse size(Payload) =< Max of + true -> Payload; + false -> <> = Payload, + Trunc + end. diff --git a/rabbitmq-server/plugins-src/rabbitmq-tracing/src/rabbit_tracing_consumer_sup.erl b/rabbitmq-server/plugins-src/rabbitmq-tracing/src/rabbit_tracing_consumer_sup.erl new file mode 100644 index 0000000..8dbc22f --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-tracing/src/rabbit_tracing_consumer_sup.erl @@ -0,0 +1,34 @@ +%% The contents of this file are subject to the Mozilla Public License +%% Version 1.1 (the "License"); you may not use this file except in +%% compliance with the License. You may obtain a copy of the License +%% at http://www.mozilla.org/MPL/ +%% +%% Software distributed under the License is distributed on an "AS IS" +%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See +%% the License for the specific language governing rights and +%% limitations under the License. +%% +%% The Original Code is RabbitMQ Federation. +%% +%% The Initial Developer of the Original Code is GoPivotal, Inc. +%% Copyright (c) 2007-2015 Pivotal Software, Inc. All rights reserved. +%% + +-module(rabbit_tracing_consumer_sup). + +-behaviour(supervisor). + +-include_lib("rabbit_common/include/rabbit.hrl"). + +-export([start_link/1]). +-export([init/1]). + +start_link(Args) -> supervisor2:start_link(?MODULE, Args). + +%%---------------------------------------------------------------------------- + +init(Args) -> + {ok, {{one_for_one, 3, 10}, + [{consumer, {rabbit_tracing_consumer, start_link, [Args]}, + transient, ?MAX_WAIT, worker, + [rabbit_tracing_consumer]}]}}. diff --git a/rabbitmq-server/plugins-src/rabbitmq-tracing/src/rabbit_tracing_files.erl b/rabbitmq-server/plugins-src/rabbitmq-tracing/src/rabbit_tracing_files.erl new file mode 100644 index 0000000..c5520e7 --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-tracing/src/rabbit_tracing_files.erl @@ -0,0 +1,51 @@ +%% The contents of this file are subject to the Mozilla Public License +%% Version 1.1 (the "License"); you may not use this file except in +%% compliance with the License. You may obtain a copy of the License +%% at http://www.mozilla.org/MPL/ +%% +%% Software distributed under the License is distributed on an "AS IS" +%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See +%% the License for the specific language governing rights and +%% limitations under the License. +%% +%% The Original Code is RabbitMQ. +%% +%% The Initial Developer of the Original Code is GoPivotal, Inc. +%% Copyright (c) 2007-2015 Pivotal Software, Inc. All rights reserved. +%% + +-module(rabbit_tracing_files). + +-include_lib("kernel/include/file.hrl"). + +-export([list/0, exists/1, delete/1, full_path/1]). + +%%-------------------------------------------------------------------- + +list() -> + {ok, Dir} = application:get_env(rabbitmq_tracing, directory), + ok = filelib:ensure_dir(Dir ++ "/a"), + {ok, Names} = file:list_dir(Dir), + [file_info(Name) || Name <- Names]. + +exists(Name) -> + filelib:is_regular(full_path(Name)). + +delete(Name) -> + ok = file:delete(full_path(Name)). + +full_path(Name0) when is_binary(Name0) -> + full_path(binary_to_list(Name0)); +full_path(Name0) -> + {ok, Dir} = application:get_env(rabbitmq_tracing, directory), + case mochiweb_util:safe_relative_path(Name0) of + undefined -> exit(how_rude); + Name -> Dir ++ "/" ++ Name + end. + +%%-------------------------------------------------------------------- + +file_info(Name) -> + {ok, Info} = file:read_file_info(full_path(Name)), + [{name, list_to_binary(Name)}, + {size, Info#file_info.size}]. diff --git a/rabbitmq-server/plugins-src/rabbitmq-tracing/src/rabbit_tracing_mgmt.erl b/rabbitmq-server/plugins-src/rabbitmq-tracing/src/rabbit_tracing_mgmt.erl new file mode 100644 index 0000000..3d2d44d --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-tracing/src/rabbit_tracing_mgmt.erl @@ -0,0 +1,29 @@ +%% The contents of this file are subject to the Mozilla Public License +%% Version 1.1 (the "License"); you may not use this file except in +%% compliance with the License. You may obtain a copy of the License +%% at http://www.mozilla.org/MPL/ +%% +%% Software distributed under the License is distributed on an "AS IS" +%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See +%% the License for the specific language governing rights and +%% limitations under the License. +%% +%% The Original Code is RabbitMQ. +%% +%% The Initial Developer of the Original Code is GoPivotal, Inc. +%% Copyright (c) 2007-2015 Pivotal Software, Inc. All rights reserved. +%% + +-module(rabbit_tracing_mgmt). + +-behaviour(rabbit_mgmt_extension). + +-export([dispatcher/0, web_ui/0]). + +dispatcher() -> [{["traces"], rabbit_tracing_wm_traces, []}, + {["traces", vhost], rabbit_tracing_wm_traces, []}, + {["traces", vhost, name], rabbit_tracing_wm_trace, []}, + {["trace-files"], rabbit_tracing_wm_files, []}, + {["trace-files", name], rabbit_tracing_wm_file, []}]. + +web_ui() -> [{javascript, <<"tracing.js">>}]. diff --git a/rabbitmq-server/plugins-src/rabbitmq-tracing/src/rabbit_tracing_sup.erl b/rabbitmq-server/plugins-src/rabbitmq-tracing/src/rabbit_tracing_sup.erl new file mode 100644 index 0000000..502ef7e --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-tracing/src/rabbit_tracing_sup.erl @@ -0,0 +1,50 @@ +%% The contents of this file are subject to the Mozilla Public License +%% Version 1.1 (the "License"); you may not use this file except in +%% compliance with the License. You may obtain a copy of the License +%% at http://www.mozilla.org/MPL/ +%% +%% Software distributed under the License is distributed on an "AS IS" +%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See +%% the License for the specific language governing rights and +%% limitations under the License. +%% +%% The Original Code is RabbitMQ. +%% +%% The Initial Developer of the Original Code is GoPivotal, Inc. +%% Copyright (c) 2007-2015 Pivotal Software, Inc. All rights reserved. +%% + +-module(rabbit_tracing_sup). + +-behaviour(supervisor). + +-include_lib("rabbit_common/include/rabbit.hrl"). + +-define(SUPERVISOR, ?MODULE). + +-export([start_link/0, start_child/2, stop_child/1]). +-export([init/1]). + +%%---------------------------------------------------------------------------- + +start_link() -> + supervisor:start_link({local, ?SUPERVISOR}, ?MODULE, []). + +start_child(Id, Args) -> + supervisor:start_child( + ?SUPERVISOR, + {Id, {rabbit_tracing_consumer_sup, start_link, [Args]}, + temporary, ?MAX_WAIT, supervisor, + [rabbit_tracing_consumer_sup]}). + +stop_child(Id) -> + supervisor:terminate_child(?SUPERVISOR, Id), + supervisor:delete_child(?SUPERVISOR, Id), + ok. + +%%---------------------------------------------------------------------------- + +init([]) -> {ok, {{one_for_one, 3, 10}, + [{traces, {rabbit_tracing_traces, start_link, []}, + transient, ?MAX_WAIT, worker, + [rabbit_tracing_traces]}]}}. diff --git a/rabbitmq-server/plugins-src/rabbitmq-tracing/src/rabbit_tracing_traces.erl b/rabbitmq-server/plugins-src/rabbitmq-tracing/src/rabbit_tracing_traces.erl new file mode 100644 index 0000000..53336d7 --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-tracing/src/rabbit_tracing_traces.erl @@ -0,0 +1,123 @@ +%% The contents of this file are subject to the Mozilla Public License +%% Version 1.1 (the "License"); you may not use this file except in +%% compliance with the License. You may obtain a copy of the License +%% at http://www.mozilla.org/MPL/ +%% +%% Software distributed under the License is distributed on an "AS IS" +%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See +%% the License for the specific language governing rights and +%% limitations under the License. +%% +%% The Original Code is RabbitMQ. +%% +%% The Initial Developer of the Original Code is GoPivotal, Inc. +%% Copyright (c) 2007-2015 Pivotal Software, Inc. All rights reserved. +%% + +-module(rabbit_tracing_traces). + +-behaviour(gen_server). + +-import(rabbit_misc, [pget/2]). + +-export([list/0, lookup/2, create/3, stop/2, announce/3]). + +-export([start_link/0]). +-export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, + code_change/3]). + +-define(SERVER, ?MODULE). + +-record(state, { table }). + +%%-------------------------------------------------------------------- + +start_link() -> + gen_server:start_link({local, ?MODULE}, ?MODULE, [], []). + +list() -> + gen_server:call(?MODULE, list, infinity). + +lookup(VHost, Name) -> + gen_server:call(?MODULE, {lookup, VHost, Name}, infinity). + +create(VHost, Name, Trace) -> + gen_server:call(?MODULE, {create, VHost, Name, Trace}, infinity). + +stop(VHost, Name) -> + gen_server:call(?MODULE, {stop, VHost, Name}, infinity). + +announce(VHost, Name, Pid) -> + gen_server:cast(?MODULE, {announce, {VHost, Name}, Pid}). + +%%-------------------------------------------------------------------- + +init([]) -> + {ok, #state{table = ets:new(anon, [private])}}. + +handle_call(list, _From, State = #state{table = Table}) -> + {reply, [augment(Trace) || {_K, Trace} <- ets:tab2list(Table)], State}; + +handle_call({lookup, VHost, Name}, _From, State = #state{table = Table}) -> + {reply, case ets:lookup(Table, {VHost, Name}) of + [] -> not_found; + [{_K, Trace}] -> augment(Trace) + end, State}; + +handle_call({create, VHost, Name, Trace0}, _From, + State = #state{table = Table}) -> + Already = vhost_tracing(VHost, Table), + Trace = pset(vhost, VHost, pset(name, Name, Trace0)), + true = ets:insert(Table, {{VHost, Name}, Trace}), + case Already of + true -> ok; + false -> rabbit_trace:start(VHost) + end, + {reply, rabbit_tracing_sup:start_child({VHost, Name}, Trace), State}; + +handle_call({stop, VHost, Name}, _From, State = #state{table = Table}) -> + true = ets:delete(Table, {VHost, Name}), + case vhost_tracing(VHost, Table) of + true -> ok; + false -> rabbit_trace:stop(VHost) + end, + rabbit_tracing_sup:stop_child({VHost, Name}), + {reply, ok, State}; + +handle_call(_Req, _From, State) -> + {reply, unknown_request, State}. + +handle_cast({announce, Key, Pid}, State = #state{table = Table}) -> + case ets:lookup(Table, Key) of + [] -> ok; + [{_, Trace}] -> ets:insert(Table, {Key, pset(pid, Pid, Trace)}) + end, + {noreply, State}; + +handle_cast(_C, State) -> + {noreply, State}. + +handle_info(_I, State) -> + {noreply, State}. + +terminate(_, _) -> ok. + +code_change(_, State, _) -> {ok, State}. + +%%-------------------------------------------------------------------- + +pset(Key, Value, List) -> [{Key, Value} | proplists:delete(Key, List)]. + +vhost_tracing(VHost, Table) -> + case [true || {{V, _}, _} <- ets:tab2list(Table), V =:= VHost] of + [] -> false; + _ -> true + end. + +augment(Trace) -> + Pid = pget(pid, Trace), + Trace1 = lists:keydelete(pid, 1, Trace), + case Pid of + undefined -> Trace1; + _ -> rabbit_tracing_consumer:info_all(Pid) ++ Trace1 + end. diff --git a/rabbitmq-server/plugins-src/rabbitmq-tracing/src/rabbit_tracing_wm_file.erl b/rabbitmq-server/plugins-src/rabbitmq-tracing/src/rabbit_tracing_wm_file.erl new file mode 100644 index 0000000..30a134b --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-tracing/src/rabbit_tracing_wm_file.erl @@ -0,0 +1,49 @@ +%% The contents of this file are subject to the Mozilla Public License +%% Version 1.1 (the "License"); you may not use this file except in +%% compliance with the License. You may obtain a copy of the License at +%% http://www.mozilla.org/MPL/ +%% +%% Software distributed under the License is distributed on an "AS IS" +%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the +%% License for the specific language governing rights and limitations +%% under the License. +%% +%% The Original Code is RabbitMQ. +%% +%% The Initial Developer of the Original Code is GoPivotal, Inc. +%% Copyright (c) 2007-2015 Pivotal Software, Inc. All rights reserved. + +-module(rabbit_tracing_wm_file). + +-export([init/1, resource_exists/2, serve/2, content_types_provided/2, + is_authorized/2, allowed_methods/2, delete_resource/2]). + +-include_lib("rabbitmq_management/include/rabbit_mgmt.hrl"). +-include_lib("webmachine/include/webmachine.hrl"). + +%%-------------------------------------------------------------------- +init(_Config) -> {ok, #context{}}. + +content_types_provided(ReqData, Context) -> + {[{"text/plain", serve}], ReqData, Context}. + +allowed_methods(ReqData, Context) -> + {['HEAD', 'GET', 'DELETE'], ReqData, Context}. + +resource_exists(ReqData, Context) -> + Name = rabbit_mgmt_util:id(name, ReqData), + {rabbit_tracing_files:exists(Name), ReqData, Context}. + +serve(ReqData, Context) -> + Name = rabbit_mgmt_util:id(name, ReqData), + {ok, Content} = file:read_file(rabbit_tracing_files:full_path(Name)), + {Content, ReqData, Context}. + +delete_resource(ReqData, Context) -> + Name = rabbit_mgmt_util:id(name, ReqData), + ok = rabbit_tracing_files:delete(Name), + {true, ReqData, Context}. + +is_authorized(ReqData, Context) -> + rabbit_mgmt_util:is_authorized_admin(ReqData, Context). + diff --git a/rabbitmq-server/plugins-src/rabbitmq-tracing/src/rabbit_tracing_wm_files.erl b/rabbitmq-server/plugins-src/rabbitmq-tracing/src/rabbit_tracing_wm_files.erl new file mode 100644 index 0000000..d3a8004 --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-tracing/src/rabbit_tracing_wm_files.erl @@ -0,0 +1,37 @@ +%% The contents of this file are subject to the Mozilla Public License +%% Version 1.1 (the "License"); you may not use this file except in +%% compliance with the License. You may obtain a copy of the License +%% at http://www.mozilla.org/MPL/ +%% +%% Software distributed under the License is distributed on an "AS IS" +%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See +%% the License for the specific language governing rights and +%% limitations under the License. +%% +%% The Original Code is RabbitMQ. +%% +%% The Initial Developer of the Original Code is GoPivotal, Inc. +%% Copyright (c) 2007-2015 Pivotal Software, Inc. All rights reserved. +%% + +-module(rabbit_tracing_wm_files). + +-export([init/1, to_json/2, content_types_provided/2, is_authorized/2]). + +-include_lib("rabbitmq_management/include/rabbit_mgmt.hrl"). +-include_lib("webmachine/include/webmachine.hrl"). + +%%-------------------------------------------------------------------- + +init(_Config) -> {ok, #context{}}. + +content_types_provided(ReqData, Context) -> + {[{"application/json", to_json}], ReqData, Context}. + +to_json(ReqData, Context) -> + rabbit_mgmt_util:reply(rabbit_tracing_files:list(), ReqData, Context). + +is_authorized(ReqData, Context) -> + rabbit_mgmt_util:is_authorized_admin(ReqData, Context). + +%%-------------------------------------------------------------------- diff --git a/rabbitmq-server/plugins-src/rabbitmq-tracing/src/rabbit_tracing_wm_trace.erl b/rabbitmq-server/plugins-src/rabbitmq-tracing/src/rabbit_tracing_wm_trace.erl new file mode 100644 index 0000000..a9cdbac --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-tracing/src/rabbit_tracing_wm_trace.erl @@ -0,0 +1,106 @@ +%% The contents of this file are subject to the Mozilla Public License +%% Version 1.1 (the "License"); you may not use this file except in +%% compliance with the License. You may obtain a copy of the License at +%% http://www.mozilla.org/MPL/ +%% +%% Software distributed under the License is distributed on an "AS IS" +%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the +%% License for the specific language governing rights and limitations +%% under the License. +%% +%% The Original Code is RabbitMQ. +%% +%% The Initial Developer of the Original Code is GoPivotal, Inc. +%% Copyright (c) 2007-2015 Pivotal Software, Inc. All rights reserved. + +-module(rabbit_tracing_wm_trace). + +-export([init/1, resource_exists/2, to_json/2, + content_types_provided/2, content_types_accepted/2, + is_authorized/2, allowed_methods/2, accept_content/2, + delete_resource/2]). + +-define(ERR, <<"Something went wrong trying to start the trace - check the " + "logs.">>). + +-import(rabbit_misc, [pget/2, pget/3]). + +-include_lib("rabbitmq_management/include/rabbit_mgmt.hrl"). +-include_lib("webmachine/include/webmachine.hrl"). + +%%-------------------------------------------------------------------- +init(_Config) -> {ok, #context{}}. + +content_types_provided(ReqData, Context) -> + {[{"application/json", to_json}], ReqData, Context}. + +content_types_accepted(ReqData, Context) -> + {[{"application/json", accept_content}], ReqData, Context}. + +allowed_methods(ReqData, Context) -> + {['HEAD', 'GET', 'PUT', 'DELETE'], ReqData, Context}. + +resource_exists(ReqData, Context) -> + {case trace(ReqData) of + not_found -> false; + _ -> true + end, ReqData, Context}. + +to_json(ReqData, Context) -> + rabbit_mgmt_util:reply(trace(ReqData), ReqData, Context). + +accept_content(RD, Ctx) -> + case rabbit_mgmt_util:vhost(RD) of + not_found -> + not_found; + VHost -> + Name = rabbit_mgmt_util:id(name, RD), + rabbit_mgmt_util:with_decode( + [format, pattern], RD, Ctx, + fun([_, _], Trace) -> + Fs = [fun val_payload_bytes/3, fun val_format/3, + fun val_create/3], + case lists:foldl(fun (F, ok) -> F(VHost, Name, Trace); + (_F, Err) -> Err + end, ok, Fs) of + ok -> {true, RD, Ctx}; + Err -> rabbit_mgmt_util:bad_request(Err, RD, Ctx) + end + end) + end. + +delete_resource(ReqData, Context) -> + VHost = rabbit_mgmt_util:vhost(ReqData), + Name = rabbit_mgmt_util:id(name, ReqData), + ok = rabbit_tracing_traces:stop(VHost, Name), + {true, ReqData, Context}. + +is_authorized(ReqData, Context) -> + rabbit_mgmt_util:is_authorized_admin(ReqData, Context). + +%%-------------------------------------------------------------------- + +trace(ReqData) -> + case rabbit_mgmt_util:vhost(ReqData) of + not_found -> not_found; + VHost -> rabbit_tracing_traces:lookup( + VHost, rabbit_mgmt_util:id(name, ReqData)) + end. + +val_payload_bytes(_VHost, _Name, Trace) -> + case is_integer(pget(max_payload_bytes, Trace, 0)) of + false -> <<"max_payload_bytes not integer">>; + true -> ok + end. + +val_format(_VHost, _Name, Trace) -> + case lists:member(pget(format, Trace), [<<"json">>, <<"text">>]) of + false -> <<"format not json or text">>; + true -> ok + end. + +val_create(VHost, Name, Trace) -> + case rabbit_tracing_traces:create(VHost, Name, Trace) of + {ok, _} -> ok; + _ -> ?ERR + end. diff --git a/rabbitmq-server/plugins-src/rabbitmq-tracing/src/rabbit_tracing_wm_traces.erl b/rabbitmq-server/plugins-src/rabbitmq-tracing/src/rabbit_tracing_wm_traces.erl new file mode 100644 index 0000000..ef0fe50 --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-tracing/src/rabbit_tracing_wm_traces.erl @@ -0,0 +1,37 @@ +%% The contents of this file are subject to the Mozilla Public License +%% Version 1.1 (the "License"); you may not use this file except in +%% compliance with the License. You may obtain a copy of the License +%% at http://www.mozilla.org/MPL/ +%% +%% Software distributed under the License is distributed on an "AS IS" +%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See +%% the License for the specific language governing rights and +%% limitations under the License. +%% +%% The Original Code is RabbitMQ. +%% +%% The Initial Developer of the Original Code is GoPivotal, Inc. +%% Copyright (c) 2007-2015 Pivotal Software, Inc. All rights reserved. +%% + +-module(rabbit_tracing_wm_traces). + +-export([init/1, to_json/2, content_types_provided/2, is_authorized/2]). + +-include_lib("rabbitmq_management/include/rabbit_mgmt.hrl"). +-include_lib("webmachine/include/webmachine.hrl"). + +%%-------------------------------------------------------------------- + +init(_Config) -> {ok, #context{}}. + +content_types_provided(ReqData, Context) -> + {[{"application/json", to_json}], ReqData, Context}. + +to_json(ReqData, Context) -> + rabbit_mgmt_util:reply(rabbit_tracing_traces:list(), ReqData, Context). + +is_authorized(ReqData, Context) -> + rabbit_mgmt_util:is_authorized_admin(ReqData, Context). + +%%-------------------------------------------------------------------- diff --git a/rabbitmq-server/plugins-src/rabbitmq-tracing/src/rabbitmq_tracing.app.src b/rabbitmq-server/plugins-src/rabbitmq-tracing/src/rabbitmq_tracing.app.src new file mode 100644 index 0000000..df66878 --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-tracing/src/rabbitmq_tracing.app.src @@ -0,0 +1,8 @@ +{application, rabbitmq_tracing, + [{description, "RabbitMQ message logging / tracing"}, + {vsn, "%%VSN%%"}, + {modules, []}, + {registered, []}, + {mod, {rabbit_tracing_app, []}}, + {env, [{directory, "/var/tmp/rabbitmq-tracing"}]}, + {applications, [kernel, stdlib, rabbit, rabbitmq_management]}]}. diff --git a/rabbitmq-server/plugins-src/rabbitmq-tracing/test/src/rabbit_tracing_test.erl b/rabbitmq-server/plugins-src/rabbitmq-tracing/test/src/rabbit_tracing_test.erl new file mode 100644 index 0000000..df184eb --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-tracing/test/src/rabbit_tracing_test.erl @@ -0,0 +1,201 @@ +%% The contents of this file are subject to the Mozilla Public License +%% Version 1.1 (the "License"); you may not use this file except in +%% compliance with the License. You may obtain a copy of the License +%% at http://www.mozilla.org/MPL/ +%% +%% Software distributed under the License is distributed on an "AS IS" +%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See +%% the License for the specific language governing rights and +%% limitations under the License. +%% +%% The Original Code is RabbitMQ. +%% +%% The Initial Developer of the Original Code is GoPivotal, Inc. +%% Copyright (c) 2007-2015 Pivotal Software, Inc. All rights reserved. +%% + +-module(rabbit_tracing_test). + +-define(LOG_DIR, "/var/tmp/rabbitmq-tracing/"). + +-include_lib("eunit/include/eunit.hrl"). +-include_lib("amqp_client/include/amqp_client.hrl"). +-include_lib("rabbitmq_management/include/rabbit_mgmt_test.hrl"). + +-import(rabbit_misc, [pget/2]). + +tracing_test() -> + case filelib:is_dir(?LOG_DIR) of + true -> {ok, Files} = file:list_dir(?LOG_DIR), + [ok = file:delete(?LOG_DIR ++ F) || F <- Files]; + _ -> ok + end, + + [] = http_get("/traces/%2f/"), + [] = http_get("/trace-files/"), + + Args = [{format, <<"json">>}, + {pattern, <<"#">>}], + http_put("/traces/%2f/test", Args, ?NO_CONTENT), + assert_list([[{name, <<"test">>}, + {format, <<"json">>}, + {pattern, <<"#">>}]], http_get("/traces/%2f/")), + assert_item([{name, <<"test">>}, + {format, <<"json">>}, + {pattern, <<"#">>}], http_get("/traces/%2f/test")), + + {ok, Conn} = amqp_connection:start(#amqp_params_network{}), + {ok, Ch} = amqp_connection:open_channel(Conn), + amqp_channel:cast(Ch, #'basic.publish'{ exchange = <<"amq.topic">>, + routing_key = <<"key">> }, + #amqp_msg{props = #'P_basic'{}, + payload = <<"Hello world">>}), + + amqp_channel:close(Ch), + amqp_connection:close(Conn), + + timer:sleep(100), + + http_delete("/traces/%2f/test", ?NO_CONTENT), + [] = http_get("/traces/%2f/"), + assert_list([[{name, <<"test.log">>}]], http_get("/trace-files/")), + %% This is a bit cheeky as the log is actually one JSON doc per + %% line and we assume here it's only one line + assert_item([{type, <<"published">>}, + {exchange, <<"amq.topic">>}, + {routing_keys, [<<"key">>]}, + {payload, base64:encode(<<"Hello world">>)}], + http_get("/trace-files/test.log")), + http_delete("/trace-files/test.log", ?NO_CONTENT), + ok. + +tracing_validation_test() -> + Path = "/traces/%2f/test", + http_put(Path, [{pattern, <<"#">>}], ?BAD_REQUEST), + http_put(Path, [{format, <<"json">>}], ?BAD_REQUEST), + http_put(Path, [{format, <<"ebcdic">>}, + {pattern, <<"#">>}], ?BAD_REQUEST), + http_put(Path, [{format, <<"text">>}, + {pattern, <<"#">>}, + {max_payload_bytes, <<"abc">>}], ?BAD_REQUEST), + http_put(Path, [{format, <<"json">>}, + {pattern, <<"#">>}, + {max_payload_bytes, 1000}], ?NO_CONTENT), + http_delete(Path, ?NO_CONTENT), + ok. + +%%--------------------------------------------------------------------------- +%% Below is copypasta from rabbit_mgmt_test_http, it's not obvious how +%% to share that given the build system. + +http_get(Path) -> + http_get(Path, ?OK). + +http_get(Path, CodeExp) -> + http_get(Path, "guest", "guest", CodeExp). + +http_get(Path, User, Pass, CodeExp) -> + {ok, {{_HTTP, CodeAct, _}, Headers, ResBody}} = + req(get, Path, [auth_header(User, Pass)]), + assert_code(CodeExp, CodeAct, "GET", Path, ResBody), + decode(CodeExp, Headers, ResBody). + +http_put(Path, List, CodeExp) -> + http_put_raw(Path, format_for_upload(List), CodeExp). + +http_put(Path, List, User, Pass, CodeExp) -> + http_put_raw(Path, format_for_upload(List), User, Pass, CodeExp). + +http_post(Path, List, CodeExp) -> + http_post_raw(Path, format_for_upload(List), CodeExp). + +http_post(Path, List, User, Pass, CodeExp) -> + http_post_raw(Path, format_for_upload(List), User, Pass, CodeExp). + +format_for_upload(List) -> + iolist_to_binary(mochijson2:encode({struct, List})). + +http_put_raw(Path, Body, CodeExp) -> + http_upload_raw(put, Path, Body, "guest", "guest", CodeExp). + +http_put_raw(Path, Body, User, Pass, CodeExp) -> + http_upload_raw(put, Path, Body, User, Pass, CodeExp). + +http_post_raw(Path, Body, CodeExp) -> + http_upload_raw(post, Path, Body, "guest", "guest", CodeExp). + +http_post_raw(Path, Body, User, Pass, CodeExp) -> + http_upload_raw(post, Path, Body, User, Pass, CodeExp). + +http_upload_raw(Type, Path, Body, User, Pass, CodeExp) -> + {ok, {{_HTTP, CodeAct, _}, Headers, ResBody}} = + req(Type, Path, [auth_header(User, Pass)], Body), + assert_code(CodeExp, CodeAct, Type, Path, ResBody), + decode(CodeExp, Headers, ResBody). + +http_delete(Path, CodeExp) -> + http_delete(Path, "guest", "guest", CodeExp). + +http_delete(Path, User, Pass, CodeExp) -> + {ok, {{_HTTP, CodeAct, _}, Headers, ResBody}} = + req(delete, Path, [auth_header(User, Pass)]), + assert_code(CodeExp, CodeAct, "DELETE", Path, ResBody), + decode(CodeExp, Headers, ResBody). + +assert_code(CodeExp, CodeAct, Type, Path, Body) -> + case CodeExp of + CodeAct -> ok; + _ -> throw({expected, CodeExp, got, CodeAct, type, Type, + path, Path, body, Body}) + end. + +req(Type, Path, Headers) -> + httpc:request(Type, {?PREFIX ++ Path, Headers}, ?HTTPC_OPTS, []). + +req(Type, Path, Headers, Body) -> + httpc:request(Type, {?PREFIX ++ Path, Headers, "application/json", Body}, + ?HTTPC_OPTS, []). + +decode(?OK, _Headers, ResBody) -> cleanup(mochijson2:decode(ResBody)); +decode(_, Headers, _ResBody) -> Headers. + +cleanup(L) when is_list(L) -> + [cleanup(I) || I <- L]; +cleanup({struct, I}) -> + cleanup(I); +cleanup({K, V}) when is_binary(K) -> + {list_to_atom(binary_to_list(K)), cleanup(V)}; +cleanup(I) -> + I. + +auth_header(Username, Password) -> + {"Authorization", + "Basic " ++ binary_to_list(base64:encode(Username ++ ":" ++ Password))}. + +%%--------------------------------------------------------------------------- + +assert_list(Exp, Act) -> + case length(Exp) == length(Act) of + true -> ok; + false -> throw({expected, Exp, actual, Act}) + end, + [case length(lists:filter(fun(ActI) -> test_item(ExpI, ActI) end, Act)) of + 1 -> ok; + N -> throw({found, N, ExpI, in, Act}) + end || ExpI <- Exp]. + +assert_item(Exp, Act) -> + case test_item0(Exp, Act) of + [] -> ok; + Or -> throw(Or) + end. + +test_item(Exp, Act) -> + case test_item0(Exp, Act) of + [] -> true; + _ -> false + end. + +test_item0(Exp, Act) -> + [{did_not_find, ExpI, in, Act} || ExpI <- Exp, + not lists:member(ExpI, Act)]. diff --git a/rabbitmq-server/plugins-src/rabbitmq-web-dispatch/.srcdist_done b/rabbitmq-server/plugins-src/rabbitmq-web-dispatch/.srcdist_done new file mode 100644 index 0000000..e69de29 diff --git a/rabbitmq-server/plugins-src/rabbitmq-web-dispatch/CONTRIBUTING.md b/rabbitmq-server/plugins-src/rabbitmq-web-dispatch/CONTRIBUTING.md new file mode 100644 index 0000000..69a4b4a --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-web-dispatch/CONTRIBUTING.md @@ -0,0 +1,51 @@ +## Overview + +RabbitMQ projects use pull requests to discuss, collaborate on and accept code contributions. +Pull requests is the primary place of discussing code changes. + +## How to Contribute + +The process is fairly standard: + + * Fork the repository or repositories you plan on contributing to + * Clone [RabbitMQ umbrella repository](https://github.com/rabbitmq/rabbitmq-public-umbrella) + * `cd umbrella`, `make co` + * Create a branch with a descriptive name in the relevant repositories + * Make your changes, run tests, commit with a [descriptive message](http://tbaggery.com/2008/04/19/a-note-about-git-commit-messages.html), push to your fork + * Submit pull requests with an explanation what has been changed and **why** + * Submit a filled out and signed [Contributor Agreement](https://github.com/rabbitmq/ca#how-to-submit) if needed (see below) + * Be patient. We will get to your pull request eventually + +If what you are going to work on is a substantial change, please first ask the core team +of their opinion on [RabbitMQ mailing list](https://groups.google.com/forum/#!forum/rabbitmq-users). + + +## (Brief) Code of Conduct + +In one line: don't be a dick. + +Be respectful to the maintainers and other contributors. Open source +contributors put long hours into developing projects and doing user +support. Those projects and user support are available for free. We +believe this deserves some respect. + +Be respectful to people of all races, genders, religious beliefs and +political views. Regardless of how brilliant a pull request is +technically, we will not tolerate disrespectful or aggressive +behaviour. + +Contributors who violate this straightforward Code of Conduct will see +their pull requests closed and locked. + + +## Contributor Agreement + +If you want to contribute a non-trivial change, please submit a signed copy of our +[Contributor Agreement](https://github.com/rabbitmq/ca#how-to-submit) around the time +you submit your pull request. This will make it much easier (in some cases, possible) +for the RabbitMQ team at Pivotal to merge your contribution. + + +## Where to Ask Questions + +If something isn't clear, feel free to ask on our [mailing list](https://groups.google.com/forum/#!forum/rabbitmq-users). diff --git a/rabbitmq-server/plugins-src/rabbitmq-web-dispatch/LICENSE b/rabbitmq-server/plugins-src/rabbitmq-web-dispatch/LICENSE new file mode 100644 index 0000000..7714141 --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-web-dispatch/LICENSE @@ -0,0 +1,470 @@ + MOZILLA PUBLIC LICENSE + Version 1.1 + + --------------- + +1. Definitions. + + 1.0.1. "Commercial Use" means distribution or otherwise making the + Covered Code available to a third party. + + 1.1. "Contributor" means each entity that creates or contributes to + the creation of Modifications. + + 1.2. "Contributor Version" means the combination of the Original + Code, prior Modifications used by a Contributor, and the Modifications + made by that particular Contributor. + + 1.3. "Covered Code" means the Original Code or Modifications or the + combination of the Original Code and Modifications, in each case + including portions thereof. + + 1.4. "Electronic Distribution Mechanism" means a mechanism generally + accepted in the software development community for the electronic + transfer of data. + + 1.5. "Executable" means Covered Code in any form other than Source + Code. + + 1.6. "Initial Developer" means the individual or entity identified + as the Initial Developer in the Source Code notice required by Exhibit + A. + + 1.7. "Larger Work" means a work which combines Covered Code or + portions thereof with code not governed by the terms of this License. + + 1.8. "License" means this document. + + 1.8.1. "Licensable" means having the right to grant, to the maximum + extent possible, whether at the time of the initial grant or + subsequently acquired, any and all of the rights conveyed herein. + + 1.9. "Modifications" means any addition to or deletion from the + substance or structure of either the Original Code or any previous + Modifications. When Covered Code is released as a series of files, a + Modification is: + A. Any addition to or deletion from the contents of a file + containing Original Code or previous Modifications. + + B. Any new file that contains any part of the Original Code or + previous Modifications. + + 1.10. "Original Code" means Source Code of computer software code + which is described in the Source Code notice required by Exhibit A as + Original Code, and which, at the time of its release under this + License is not already Covered Code governed by this License. + + 1.10.1. "Patent Claims" means any patent claim(s), now owned or + hereafter acquired, including without limitation, method, process, + and apparatus claims, in any patent Licensable by grantor. + + 1.11. "Source Code" means the preferred form of the Covered Code for + making modifications to it, including all modules it contains, plus + any associated interface definition files, scripts used to control + compilation and installation of an Executable, or source code + differential comparisons against either the Original Code or another + well known, available Covered Code of the Contributor's choice. The + Source Code can be in a compressed or archival form, provided the + appropriate decompression or de-archiving software is widely available + for no charge. + + 1.12. "You" (or "Your") means an individual or a legal entity + exercising rights under, and complying with all of the terms of, this + License or a future version of this License issued under Section 6.1. + For legal entities, "You" includes any entity which controls, is + controlled by, or is under common control with You. For purposes of + this definition, "control" means (a) the power, direct or indirect, + to cause the direction or management of such entity, whether by + contract or otherwise, or (b) ownership of more than fifty percent + (50%) of the outstanding shares or beneficial ownership of such + entity. + +2. Source Code License. + + 2.1. The Initial Developer Grant. + The Initial Developer hereby grants You a world-wide, royalty-free, + non-exclusive license, subject to third party intellectual property + claims: + (a) under intellectual property rights (other than patent or + trademark) Licensable by Initial Developer to use, reproduce, + modify, display, perform, sublicense and distribute the Original + Code (or portions thereof) with or without Modifications, and/or + as part of a Larger Work; and + + (b) under Patents Claims infringed by the making, using or + selling of Original Code, to make, have made, use, practice, + sell, and offer for sale, and/or otherwise dispose of the + Original Code (or portions thereof). + + (c) the licenses granted in this Section 2.1(a) and (b) are + effective on the date Initial Developer first distributes + Original Code under the terms of this License. + + (d) Notwithstanding Section 2.1(b) above, no patent license is + granted: 1) for code that You delete from the Original Code; 2) + separate from the Original Code; or 3) for infringements caused + by: i) the modification of the Original Code or ii) the + combination of the Original Code with other software or devices. + + 2.2. Contributor Grant. + Subject to third party intellectual property claims, each Contributor + hereby grants You a world-wide, royalty-free, non-exclusive license + + (a) under intellectual property rights (other than patent or + trademark) Licensable by Contributor, to use, reproduce, modify, + display, perform, sublicense and distribute the Modifications + created by such Contributor (or portions thereof) either on an + unmodified basis, with other Modifications, as Covered Code + and/or as part of a Larger Work; and + + (b) under Patent Claims infringed by the making, using, or + selling of Modifications made by that Contributor either alone + and/or in combination with its Contributor Version (or portions + of such combination), to make, use, sell, offer for sale, have + made, and/or otherwise dispose of: 1) Modifications made by that + Contributor (or portions thereof); and 2) the combination of + Modifications made by that Contributor with its Contributor + Version (or portions of such combination). + + (c) the licenses granted in Sections 2.2(a) and 2.2(b) are + effective on the date Contributor first makes Commercial Use of + the Covered Code. + + (d) Notwithstanding Section 2.2(b) above, no patent license is + granted: 1) for any code that Contributor has deleted from the + Contributor Version; 2) separate from the Contributor Version; + 3) for infringements caused by: i) third party modifications of + Contributor Version or ii) the combination of Modifications made + by that Contributor with other software (except as part of the + Contributor Version) or other devices; or 4) under Patent Claims + infringed by Covered Code in the absence of Modifications made by + that Contributor. + +3. Distribution Obligations. + + 3.1. Application of License. + The Modifications which You create or to which You contribute are + governed by the terms of this License, including without limitation + Section 2.2. The Source Code version of Covered Code may be + distributed only under the terms of this License or a future version + of this License released under Section 6.1, and You must include a + copy of this License with every copy of the Source Code You + distribute. You may not offer or impose any terms on any Source Code + version that alters or restricts the applicable version of this + License or the recipients' rights hereunder. However, You may include + an additional document offering the additional rights described in + Section 3.5. + + 3.2. Availability of Source Code. + Any Modification which You create or to which You contribute must be + made available in Source Code form under the terms of this License + either on the same media as an Executable version or via an accepted + Electronic Distribution Mechanism to anyone to whom you made an + Executable version available; and if made available via Electronic + Distribution Mechanism, must remain available for at least twelve (12) + months after the date it initially became available, or at least six + (6) months after a subsequent version of that particular Modification + has been made available to such recipients. You are responsible for + ensuring that the Source Code version remains available even if the + Electronic Distribution Mechanism is maintained by a third party. + + 3.3. Description of Modifications. + You must cause all Covered Code to which You contribute to contain a + file documenting the changes You made to create that Covered Code and + the date of any change. You must include a prominent statement that + the Modification is derived, directly or indirectly, from Original + Code provided by the Initial Developer and including the name of the + Initial Developer in (a) the Source Code, and (b) in any notice in an + Executable version or related documentation in which You describe the + origin or ownership of the Covered Code. + + 3.4. Intellectual Property Matters + (a) Third Party Claims. + If Contributor has knowledge that a license under a third party's + intellectual property rights is required to exercise the rights + granted by such Contributor under Sections 2.1 or 2.2, + Contributor must include a text file with the Source Code + distribution titled "LEGAL" which describes the claim and the + party making the claim in sufficient detail that a recipient will + know whom to contact. If Contributor obtains such knowledge after + the Modification is made available as described in Section 3.2, + Contributor shall promptly modify the LEGAL file in all copies + Contributor makes available thereafter and shall take other steps + (such as notifying appropriate mailing lists or newsgroups) + reasonably calculated to inform those who received the Covered + Code that new knowledge has been obtained. + + (b) Contributor APIs. + If Contributor's Modifications include an application programming + interface and Contributor has knowledge of patent licenses which + are reasonably necessary to implement that API, Contributor must + also include this information in the LEGAL file. + + (c) Representations. + Contributor represents that, except as disclosed pursuant to + Section 3.4(a) above, Contributor believes that Contributor's + Modifications are Contributor's original creation(s) and/or + Contributor has sufficient rights to grant the rights conveyed by + this License. + + 3.5. Required Notices. + You must duplicate the notice in Exhibit A in each file of the Source + Code. If it is not possible to put such notice in a particular Source + Code file due to its structure, then You must include such notice in a + location (such as a relevant directory) where a user would be likely + to look for such a notice. If You created one or more Modification(s) + You may add your name as a Contributor to the notice described in + Exhibit A. You must also duplicate this License in any documentation + for the Source Code where You describe recipients' rights or ownership + rights relating to Covered Code. You may choose to offer, and to + charge a fee for, warranty, support, indemnity or liability + obligations to one or more recipients of Covered Code. However, You + may do so only on Your own behalf, and not on behalf of the Initial + Developer or any Contributor. You must make it absolutely clear than + any such warranty, support, indemnity or liability obligation is + offered by You alone, and You hereby agree to indemnify the Initial + Developer and every Contributor for any liability incurred by the + Initial Developer or such Contributor as a result of warranty, + support, indemnity or liability terms You offer. + + 3.6. Distribution of Executable Versions. + You may distribute Covered Code in Executable form only if the + requirements of Section 3.1-3.5 have been met for that Covered Code, + and if You include a notice stating that the Source Code version of + the Covered Code is available under the terms of this License, + including a description of how and where You have fulfilled the + obligations of Section 3.2. The notice must be conspicuously included + in any notice in an Executable version, related documentation or + collateral in which You describe recipients' rights relating to the + Covered Code. You may distribute the Executable version of Covered + Code or ownership rights under a license of Your choice, which may + contain terms different from this License, provided that You are in + compliance with the terms of this License and that the license for the + Executable version does not attempt to limit or alter the recipient's + rights in the Source Code version from the rights set forth in this + License. If You distribute the Executable version under a different + license You must make it absolutely clear that any terms which differ + from this License are offered by You alone, not by the Initial + Developer or any Contributor. You hereby agree to indemnify the + Initial Developer and every Contributor for any liability incurred by + the Initial Developer or such Contributor as a result of any such + terms You offer. + + 3.7. Larger Works. + You may create a Larger Work by combining Covered Code with other code + not governed by the terms of this License and distribute the Larger + Work as a single product. In such a case, You must make sure the + requirements of this License are fulfilled for the Covered Code. + +4. Inability to Comply Due to Statute or Regulation. + + If it is impossible for You to comply with any of the terms of this + License with respect to some or all of the Covered Code due to + statute, judicial order, or regulation then You must: (a) comply with + the terms of this License to the maximum extent possible; and (b) + describe the limitations and the code they affect. Such description + must be included in the LEGAL file described in Section 3.4 and must + be included with all distributions of the Source Code. Except to the + extent prohibited by statute or regulation, such description must be + sufficiently detailed for a recipient of ordinary skill to be able to + understand it. + +5. Application of this License. + + This License applies to code to which the Initial Developer has + attached the notice in Exhibit A and to related Covered Code. + +6. Versions of the License. + + 6.1. New Versions. + Netscape Communications Corporation ("Netscape") may publish revised + and/or new versions of the License from time to time. Each version + will be given a distinguishing version number. + + 6.2. Effect of New Versions. + Once Covered Code has been published under a particular version of the + License, You may always continue to use it under the terms of that + version. You may also choose to use such Covered Code under the terms + of any subsequent version of the License published by Netscape. No one + other than Netscape has the right to modify the terms applicable to + Covered Code created under this License. + + 6.3. Derivative Works. + If You create or use a modified version of this License (which you may + only do in order to apply it to code which is not already Covered Code + governed by this License), You must (a) rename Your license so that + the phrases "Mozilla", "MOZILLAPL", "MOZPL", "Netscape", + "MPL", "NPL" or any confusingly similar phrase do not appear in your + license (except to note that your license differs from this License) + and (b) otherwise make it clear that Your version of the license + contains terms which differ from the Mozilla Public License and + Netscape Public License. (Filling in the name of the Initial + Developer, Original Code or Contributor in the notice described in + Exhibit A shall not of themselves be deemed to be modifications of + this License.) + +7. DISCLAIMER OF WARRANTY. + + COVERED CODE IS PROVIDED UNDER THIS LICENSE ON AN "AS IS" BASIS, + WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, + WITHOUT LIMITATION, WARRANTIES THAT THE COVERED CODE IS FREE OF + DEFECTS, MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE OR NON-INFRINGING. + THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE COVERED CODE + IS WITH YOU. SHOULD ANY COVERED CODE PROVE DEFECTIVE IN ANY RESPECT, + YOU (NOT THE INITIAL DEVELOPER OR ANY OTHER CONTRIBUTOR) ASSUME THE + COST OF ANY NECESSARY SERVICING, REPAIR OR CORRECTION. THIS DISCLAIMER + OF WARRANTY CONSTITUTES AN ESSENTIAL PART OF THIS LICENSE. NO USE OF + ANY COVERED CODE IS AUTHORIZED HEREUNDER EXCEPT UNDER THIS DISCLAIMER. + +8. TERMINATION. + + 8.1. This License and the rights granted hereunder will terminate + automatically if You fail to comply with terms herein and fail to cure + such breach within 30 days of becoming aware of the breach. All + sublicenses to the Covered Code which are properly granted shall + survive any termination of this License. Provisions which, by their + nature, must remain in effect beyond the termination of this License + shall survive. + + 8.2. If You initiate litigation by asserting a patent infringement + claim (excluding declatory judgment actions) against Initial Developer + or a Contributor (the Initial Developer or Contributor against whom + You file such action is referred to as "Participant") alleging that: + + (a) such Participant's Contributor Version directly or indirectly + infringes any patent, then any and all rights granted by such + Participant to You under Sections 2.1 and/or 2.2 of this License + shall, upon 60 days notice from Participant terminate prospectively, + unless if within 60 days after receipt of notice You either: (i) + agree in writing to pay Participant a mutually agreeable reasonable + royalty for Your past and future use of Modifications made by such + Participant, or (ii) withdraw Your litigation claim with respect to + the Contributor Version against such Participant. If within 60 days + of notice, a reasonable royalty and payment arrangement are not + mutually agreed upon in writing by the parties or the litigation claim + is not withdrawn, the rights granted by Participant to You under + Sections 2.1 and/or 2.2 automatically terminate at the expiration of + the 60 day notice period specified above. + + (b) any software, hardware, or device, other than such Participant's + Contributor Version, directly or indirectly infringes any patent, then + any rights granted to You by such Participant under Sections 2.1(b) + and 2.2(b) are revoked effective as of the date You first made, used, + sold, distributed, or had made, Modifications made by that + Participant. + + 8.3. If You assert a patent infringement claim against Participant + alleging that such Participant's Contributor Version directly or + indirectly infringes any patent where such claim is resolved (such as + by license or settlement) prior to the initiation of patent + infringement litigation, then the reasonable value of the licenses + granted by such Participant under Sections 2.1 or 2.2 shall be taken + into account in determining the amount or value of any payment or + license. + + 8.4. In the event of termination under Sections 8.1 or 8.2 above, + all end user license agreements (excluding distributors and resellers) + which have been validly granted by You or any distributor hereunder + prior to termination shall survive termination. + +9. LIMITATION OF LIABILITY. + + UNDER NO CIRCUMSTANCES AND UNDER NO LEGAL THEORY, WHETHER TORT + (INCLUDING NEGLIGENCE), CONTRACT, OR OTHERWISE, SHALL YOU, THE INITIAL + DEVELOPER, ANY OTHER CONTRIBUTOR, OR ANY DISTRIBUTOR OF COVERED CODE, + OR ANY SUPPLIER OF ANY OF SUCH PARTIES, BE LIABLE TO ANY PERSON FOR + ANY INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES OF ANY + CHARACTER INCLUDING, WITHOUT LIMITATION, DAMAGES FOR LOSS OF GOODWILL, + WORK STOPPAGE, COMPUTER FAILURE OR MALFUNCTION, OR ANY AND ALL OTHER + COMMERCIAL DAMAGES OR LOSSES, EVEN IF SUCH PARTY SHALL HAVE BEEN + INFORMED OF THE POSSIBILITY OF SUCH DAMAGES. THIS LIMITATION OF + LIABILITY SHALL NOT APPLY TO LIABILITY FOR DEATH OR PERSONAL INJURY + RESULTING FROM SUCH PARTY'S NEGLIGENCE TO THE EXTENT APPLICABLE LAW + PROHIBITS SUCH LIMITATION. SOME JURISDICTIONS DO NOT ALLOW THE + EXCLUSION OR LIMITATION OF INCIDENTAL OR CONSEQUENTIAL DAMAGES, SO + THIS EXCLUSION AND LIMITATION MAY NOT APPLY TO YOU. + +10. U.S. GOVERNMENT END USERS. + + The Covered Code is a "commercial item," as that term is defined in + 48 C.F.R. 2.101 (Oct. 1995), consisting of "commercial computer + software" and "commercial computer software documentation," as such + terms are used in 48 C.F.R. 12.212 (Sept. 1995). Consistent with 48 + C.F.R. 12.212 and 48 C.F.R. 227.7202-1 through 227.7202-4 (June 1995), + all U.S. Government End Users acquire Covered Code with only those + rights set forth herein. + +11. MISCELLANEOUS. + + This License represents the complete agreement concerning subject + matter hereof. If any provision of this License is held to be + unenforceable, such provision shall be reformed only to the extent + necessary to make it enforceable. This License shall be governed by + California law provisions (except to the extent applicable law, if + any, provides otherwise), excluding its conflict-of-law provisions. + With respect to disputes in which at least one party is a citizen of, + or an entity chartered or registered to do business in the United + States of America, any litigation relating to this License shall be + subject to the jurisdiction of the Federal Courts of the Northern + District of California, with venue lying in Santa Clara County, + California, with the losing party responsible for costs, including + without limitation, court costs and reasonable attorneys' fees and + expenses. The application of the United Nations Convention on + Contracts for the International Sale of Goods is expressly excluded. + Any law or regulation which provides that the language of a contract + shall be construed against the drafter shall not apply to this + License. + +12. RESPONSIBILITY FOR CLAIMS. + + As between Initial Developer and the Contributors, each party is + responsible for claims and damages arising, directly or indirectly, + out of its utilization of rights under this License and You agree to + work with Initial Developer and Contributors to distribute such + responsibility on an equitable basis. Nothing herein is intended or + shall be deemed to constitute any admission of liability. + +13. MULTIPLE-LICENSED CODE. + + Initial Developer may designate portions of the Covered Code as + "Multiple-Licensed". "Multiple-Licensed" means that the Initial + Developer permits you to utilize portions of the Covered Code under + Your choice of the NPL or the alternative licenses, if any, specified + by the Initial Developer in the file described in Exhibit A. + +EXHIBIT A -Mozilla Public License. + + ``The contents of this file are subject to the Mozilla Public License + Version 1.1 (the "License"); you may not use this file except in + compliance with the License. You may obtain a copy of the License at + http://www.mozilla.org/MPL/ + + Software distributed under the License is distributed on an "AS IS" + basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the + License for the specific language governing rights and limitations + under the License. + + The Original Code is ______________________________________. + + The Initial Developer of the Original Code is ________________________. + Portions created by ______________________ are Copyright (C) ______ + _______________________. All Rights Reserved. + + Contributor(s): ______________________________________. + + Alternatively, the contents of this file may be used under the terms + of the _____ license (the "[___] License"), in which case the + provisions of [______] License are applicable instead of those + above. If you wish to allow use of your version of this file only + under the terms of the [____] License and not to allow others to use + your version of this file under the MPL, indicate your decision by + deleting the provisions above and replace them with the notice and + other provisions required by the [___] License. If you do not delete + the provisions above, a recipient may use your version of this file + under either the MPL or the [___] License." + + [NOTE: The text of this Exhibit A may differ slightly from the text of + the notices in the Source Code files of the Original Code. You should + use the text of this Exhibit A rather than the text found in the + Original Code Source Code for Your Modifications.] + diff --git a/rabbitmq-server/plugins-src/rabbitmq-web-dispatch/Makefile b/rabbitmq-server/plugins-src/rabbitmq-web-dispatch/Makefile new file mode 100644 index 0000000..482105a --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-web-dispatch/Makefile @@ -0,0 +1 @@ +include ../umbrella.mk diff --git a/rabbitmq-server/plugins-src/rabbitmq-web-dispatch/README.md b/rabbitmq-server/plugins-src/rabbitmq-web-dispatch/README.md new file mode 100644 index 0000000..425ef6d --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-web-dispatch/README.md @@ -0,0 +1,25 @@ +rabbitmq-web-dispatch +--------------------- + +rabbitmq-web-dispatch is a thin veneer around mochiweb that provides the +ability for multiple applications to co-exist on mochiweb +listeners. Applications can register static docroots or dynamic +handlers to be executed, dispatched by URL path prefix. + +See http://www.rabbitmq.com/mochiweb.html for information on +configuring web plugins. + +The most general registration procedure is +`rabbit_web_dispatch:register_context_handler/5`. This takes a callback +procedure of the form + + loop(Request) -> + ... + +The module `rabbit_webmachine` provides a means of running more than +one webmachine in a VM, and understands rabbitmq-web-dispatch contexts. To +use it, supply a dispatch table term of the kind usually given to +webmachine in the file `priv/dispatch.conf`. + +`setup/{1,2}` in the same module allows some global configuration of +webmachine logging and error handling. diff --git a/rabbitmq-server/plugins-src/rabbitmq-web-dispatch/package.mk b/rabbitmq-server/plugins-src/rabbitmq-web-dispatch/package.mk new file mode 100644 index 0000000..d5913ca --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-web-dispatch/package.mk @@ -0,0 +1,3 @@ +DEPS:=mochiweb-wrapper webmachine-wrapper +WITH_BROKER_TEST_COMMANDS:=rabbit_web_dispatch_test:test() +STANDALONE_TEST_COMMANDS:=rabbit_web_dispatch_test_unit:test() diff --git a/rabbitmq-server/plugins-src/rabbitmq-web-dispatch/src/rabbit_web_dispatch.erl b/rabbitmq-server/plugins-src/rabbitmq-web-dispatch/src/rabbit_web_dispatch.erl new file mode 100644 index 0000000..c328c1f --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-web-dispatch/src/rabbit_web_dispatch.erl @@ -0,0 +1,114 @@ +%% The contents of this file are subject to the Mozilla Public License +%% Version 1.1 (the "License"); you may not use this file except in +%% compliance with the License. You may obtain a copy of the License +%% at http://www.mozilla.org/MPL/ +%% +%% Software distributed under the License is distributed on an "AS IS" +%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See +%% the License for the specific language governing rights and +%% limitations under the License. +%% +%% The Original Code is RabbitMQ. +%% +%% The Initial Developer of the Original Code is GoPivotal, Inc. +%% Copyright (c) 2010-2014 GoPivotal, Inc. All rights reserved. +%% + +-module(rabbit_web_dispatch). + +-export([register_context_handler/5, register_static_context/6]). +-export([register_port_redirect/4]). +-export([unregister_context/1]). + +%% Handler Registration + +%% Registers a dynamic selector and handler combination, with a link +%% to display in lists. +register_handler(Name, Listener, Selector, Handler, Link) -> + rabbit_web_dispatch_registry:add(Name, Listener, Selector, Handler, Link). + +%% Methods for standard use cases + +%% Registers a dynamic handler under a fixed context path, with link +%% to display in the global context. +register_context_handler(Name, Listener, Prefix, Handler, LinkText) -> + register_handler( + Name, Listener, context_selector(Prefix), Handler, {Prefix, LinkText}), + {ok, Prefix}. + +%% Convenience function registering a fully static context to serve +%% content from a module-relative directory, with link to display in +%% the global context. +register_static_context(Name, Listener, Prefix, Module, FSPath, LinkText) -> + register_handler(Name, Listener, + context_selector(Prefix), + static_context_handler(Prefix, Module, FSPath), + {Prefix, LinkText}), + {ok, Prefix}. + +%% A context which just redirects the request to a different port. +register_port_redirect(Name, Listener, Prefix, RedirectPort) -> + register_context_handler( + Name, Listener, Prefix, + fun (Req) -> + Host = case Req:get_header_value("host") of + undefined -> {ok, {IP, _Port}} = rabbit_net:sockname( + Req:get(socket)), + rabbit_misc:ntoa(IP); + Header -> hd(string:tokens(Header, ":")) + end, + URL = rabbit_misc:format( + "~s://~s:~B~s", + [Req:get(scheme), Host, RedirectPort, Req:get(raw_path)]), + Req:respond({301, [{"Location", URL}], ""}) + end, + rabbit_misc:format("Redirect to port ~B", [RedirectPort])). + +context_selector("") -> + fun(_Req) -> true end; +context_selector(Prefix) -> + Prefix1 = "/" ++ Prefix, + fun(Req) -> + Path = Req:get(raw_path), + (Path == Prefix1) orelse (string:str(Path, Prefix1 ++ "/") == 1) + end. + +%% Produces a handler for use with register_handler that serves up +%% static content from a directory specified relative to the directory +%% containing the ebin directory containing the named module's beam +%% file. +static_context_handler(Prefix, Module, FSPath) -> + {file, Here} = code:is_loaded(Module), + ModuleRoot = filename:dirname(filename:dirname(Here)), + LocalPath = filename:join(ModuleRoot, FSPath), + static_context_handler(Prefix, LocalPath). + +%% Produces a handler for use with register_handler that serves up +%% static content from a specified directory. +static_context_handler("", LocalPath) -> + fun(Req) -> + "/" ++ Path = Req:get(raw_path), + serve_file(Req, Path, LocalPath) + end; +static_context_handler(Prefix, LocalPath) -> + fun(Req) -> + "/" ++ Path = Req:get(raw_path), + case string:substr(Path, length(Prefix) + 1) of + "" -> Req:respond({301, [{"Location", "/" ++ Prefix ++ "/"}], ""}); + "/" ++ P -> serve_file(Req, P, LocalPath) + end + end. + +serve_file(Req, Path, LocalPath) -> + case Req:get(method) of + Method when Method =:= 'GET'; Method =:= 'HEAD' -> + Req:serve_file(Path, LocalPath); + _ -> + Req:respond({405, [{"Allow", "GET, HEAD"}], + "Only GET or HEAD supported for static content"}) + end. + +%% The opposite of all those register_* functions. +unregister_context(Name) -> + rabbit_web_dispatch_registry:remove(Name). + diff --git a/rabbitmq-server/plugins-src/rabbitmq-web-dispatch/src/rabbit_web_dispatch_app.erl b/rabbitmq-server/plugins-src/rabbitmq-web-dispatch/src/rabbit_web_dispatch_app.erl new file mode 100644 index 0000000..478b3fd --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-web-dispatch/src/rabbit_web_dispatch_app.erl @@ -0,0 +1,30 @@ +%% The contents of this file are subject to the Mozilla Public License +%% Version 1.1 (the "License"); you may not use this file except in +%% compliance with the License. You may obtain a copy of the License +%% at http://www.mozilla.org/MPL/ +%% +%% Software distributed under the License is distributed on an "AS IS" +%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See +%% the License for the specific language governing rights and +%% limitations under the License. +%% +%% The Original Code is RabbitMQ. +%% +%% The Initial Developer of the Original Code is GoPivotal, Inc. +%% Copyright (c) 2010-2014 GoPivotal, Inc. All rights reserved. +%% + +-module(rabbit_web_dispatch_app). + +-behaviour(application). +-export([start/2,stop/1]). + +%% @spec start(_Type, _StartArgs) -> ServerRet +%% @doc application start callback for rabbit_web_dispatch. +start(_Type, _StartArgs) -> + rabbit_web_dispatch_sup:start_link(). + +%% @spec stop(_State) -> ServerRet +%% @doc application stop callback for rabbit_web_dispatch. +stop(_State) -> + ok. diff --git a/rabbitmq-server/plugins-src/rabbitmq-web-dispatch/src/rabbit_web_dispatch_registry.erl b/rabbitmq-server/plugins-src/rabbitmq-web-dispatch/src/rabbit_web_dispatch_registry.erl new file mode 100644 index 0000000..e15530a --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-web-dispatch/src/rabbit_web_dispatch_registry.erl @@ -0,0 +1,199 @@ +%% The contents of this file are subject to the Mozilla Public License +%% Version 1.1 (the "License"); you may not use this file except in +%% compliance with the License. You may obtain a copy of the License +%% at http://www.mozilla.org/MPL/ +%% +%% Software distributed under the License is distributed on an "AS IS" +%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See +%% the License for the specific language governing rights and +%% limitations under the License. +%% +%% The Original Code is RabbitMQ. +%% +%% The Initial Developer of the Original Code is GoPivotal, Inc. +%% Copyright (c) 2010-2014 GoPivotal, Inc. All rights reserved. +%% + +-module(rabbit_web_dispatch_registry). + +-behaviour(gen_server). + +-export([start_link/0]). +-export([add/5, remove/1, set_fallback/2, lookup/2, list_all/0]). +-export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, + code_change/3]). + +-define(ETS, rabbitmq_web_dispatch). + +%% This gen_server is merely to serialise modifications to the dispatch +%% table for listeners. + +start_link() -> + gen_server:start_link({local, ?MODULE}, ?MODULE, [], []). + +add(Name, Listener, Selector, Handler, Link) -> + gen_server:call(?MODULE, {add, Name, Listener, Selector, Handler, Link}, + infinity). + +remove(Name) -> + gen_server:call(?MODULE, {remove, Name}, infinity). + +set_fallback(Listener, FallbackHandler) -> + gen_server:call(?MODULE, {set_fallback, Listener, FallbackHandler}, + infinity). + +lookup(Listener, Req) -> + case lookup_dispatch(Listener) of + {ok, {Selectors, Fallback}} -> + case catch match_request(Selectors, Req) of + {'EXIT', Reason} -> {lookup_failure, Reason}; + no_handler -> {handler, Fallback}; + Handler -> {handler, Handler} + end; + Err -> + Err + end. + +%% This is called in a somewhat obfuscated manner in +%% rabbit_mgmt_external_stats:rabbit_web_dispatch_registry_list_all() +list_all() -> + gen_server:call(?MODULE, list_all, infinity). + +%% Callback Methods + +init([]) -> + ?ETS = ets:new(?ETS, [named_table, public]), + {ok, undefined}. + +handle_call({add, Name, Listener, Selector, Handler, Link = {_, Desc}}, _From, + undefined) -> + Continue = case rabbit_web_dispatch_sup:ensure_listener(Listener) of + new -> set_dispatch( + Listener, [], + listing_fallback_handler(Listener)), + true; + existing -> true; + ignore -> false + end, + case Continue of + true -> case lookup_dispatch(Listener) of + {ok, {Selectors, Fallback}} -> + Selector2 = lists:keystore( + Name, 1, Selectors, + {Name, Selector, Handler, Link}), + set_dispatch(Listener, Selector2, Fallback); + {error, {different, Desc2, Listener2}} -> + exit({incompatible_listeners, + {Desc, Listener}, {Desc2, Listener2}}) + end; + false -> ok + end, + {reply, ok, undefined}; + +handle_call({remove, Name}, _From, + undefined) -> + Listener = listener_by_name(Name), + {ok, {Selectors, Fallback}} = lookup_dispatch(Listener), + Selectors1 = lists:keydelete(Name, 1, Selectors), + set_dispatch(Listener, Selectors1, Fallback), + case Selectors1 of + [] -> rabbit_web_dispatch_sup:stop_listener(Listener); + _ -> ok + end, + {reply, ok, undefined}; + +handle_call({set_fallback, Listener, FallbackHandler}, _From, + undefined) -> + {ok, {Selectors, _OldFallback}} = lookup_dispatch(Listener), + set_dispatch(Listener, Selectors, FallbackHandler), + {reply, ok, undefined}; + +handle_call(list_all, _From, undefined) -> + {reply, list(), undefined}; + +handle_call(Req, _From, State) -> + rabbit_log:error("Unexpected call to ~p: ~p~n", [?MODULE, Req]), + {stop, unknown_request, State}. + +handle_cast(_, State) -> + {noreply, State}. + +handle_info(_, State) -> + {noreply, State}. + +terminate(_, _) -> + true = ets:delete(?ETS), + ok. + +code_change(_, State, _) -> + {ok, State}. + +%%--------------------------------------------------------------------------- + +%% Internal Methods + +port(Listener) -> proplists:get_value(port, Listener). + +lookup_dispatch(Lsnr) -> + case ets:lookup(?ETS, port(Lsnr)) of + [{_, Lsnr, S, F}] -> {ok, {S, F}}; + [{_, Lsnr2, S, _F}] -> {error, {different, first_desc(S), Lsnr2}}; + [] -> {error, {no_record_for_listener, Lsnr}} + end. + +first_desc([{_N, _S, _H, {_, Desc}} | _]) -> Desc. + +set_dispatch(Listener, Selectors, Fallback) -> + ets:insert(?ETS, {port(Listener), Listener, Selectors, Fallback}). + +match_request([], _) -> + no_handler; +match_request([{_Name, Selector, Handler, _Link}|Rest], Req) -> + case Selector(Req) of + true -> Handler; + false -> match_request(Rest, Req) + end. + +list() -> + [{Path, Desc, Listener} || + {_P, Listener, Selectors, _F} <- ets:tab2list(?ETS), + {_N, _S, _H, {Path, Desc}} <- Selectors]. + +listener_by_name(Name) -> + case [L || {_P, L, S, _F} <- ets:tab2list(?ETS), contains_name(Name, S)] of + [Listener] -> Listener; + [] -> exit({not_found, Name}) + end. + +contains_name(Name, Selectors) -> + lists:member(Name, [N || {N, _S, _H, _L} <- Selectors]). + +list(Listener) -> + {ok, {Selectors, _Fallback}} = lookup_dispatch(Listener), + [{Path, Desc} || {_N, _S, _H, {Path, Desc}} <- Selectors]. + +%%--------------------------------------------------------------------------- + +listing_fallback_handler(Listener) -> + fun(Req) -> + HTMLPrefix = + "" + "RabbitMQ Web Server" + "

RabbitMQ Web Server

Contexts available:

    ", + HTMLSuffix = "
", + {ReqPath, _, _} = mochiweb_util:urlsplit_path(Req:get(raw_path)), + List = + case list(Listener) of + [] -> + "
  • No contexts installed
  • "; + Contexts -> + [handler_listing(Path, ReqPath, Desc) + || {Path, Desc} <- Contexts] + end, + Req:respond({200, [], HTMLPrefix ++ List ++ HTMLSuffix}) + end. + +handler_listing(Path, ReqPath, Desc) -> + io_lib:format( + "
  • ~s
  • ", + [rabbit_web_dispatch_util:relativise(ReqPath, "/" ++ Path), Desc]). diff --git a/rabbitmq-server/plugins-src/rabbitmq-web-dispatch/src/rabbit_web_dispatch_sup.erl b/rabbitmq-server/plugins-src/rabbitmq-web-dispatch/src/rabbit_web_dispatch_sup.erl new file mode 100644 index 0000000..5582d47 --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-web-dispatch/src/rabbit_web_dispatch_sup.erl @@ -0,0 +1,116 @@ +%% The contents of this file are subject to the Mozilla Public License +%% Version 1.1 (the "License"); you may not use this file except in +%% compliance with the License. You may obtain a copy of the License +%% at http://www.mozilla.org/MPL/ +%% +%% Software distributed under the License is distributed on an "AS IS" +%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See +%% the License for the specific language governing rights and +%% limitations under the License. +%% +%% The Original Code is RabbitMQ. +%% +%% The Initial Developer of the Original Code is GoPivotal, Inc. +%% Copyright (c) 2010-2014 GoPivotal, Inc. All rights reserved. +%% + +-module(rabbit_web_dispatch_sup). + +-behaviour(supervisor). + +-define(SUP, ?MODULE). + +%% External exports +-export([start_link/0, ensure_listener/1, stop_listener/1]). + +%% supervisor callbacks +-export([init/1]). + +%% @spec start_link() -> ServerRet +%% @doc API for starting the supervisor. +start_link() -> + supervisor:start_link({local, ?SUP}, ?MODULE, []). + +ensure_listener(Listener) -> + case proplists:get_value(port, Listener) of + undefined -> + {error, {no_port_given, Listener}}; + _ -> + Child = {{rabbit_web_dispatch_web, name(Listener)}, + {mochiweb_http, start, [mochi_options(Listener)]}, + transient, 5000, worker, dynamic}, + case supervisor:start_child(?SUP, Child) of + {ok, _} -> new; + {error, {already_started, _}} -> existing; + {error, {E, _}} -> check_error(Listener, E) + end + end. + +stop_listener(Listener) -> + Name = name(Listener), + ok = supervisor:terminate_child(?SUP, {rabbit_web_dispatch_web, Name}), + ok = supervisor:delete_child(?SUP, {rabbit_web_dispatch_web, Name}). + +%% @spec init([[instance()]]) -> SupervisorTree +%% @doc supervisor callback. +init([]) -> + Registry = {rabbit_web_dispatch_registry, + {rabbit_web_dispatch_registry, start_link, []}, + transient, 5000, worker, dynamic}, + {ok, {{one_for_one, 10, 10}, [Registry]}}. + +%% ---------------------------------------------------------------------- + +mochi_options(Listener) -> + [{name, name(Listener)}, + {loop, loopfun(Listener)} | + ssl_config(proplists:delete( + name, proplists:delete(ignore_in_use, Listener)))]. + +loopfun(Listener) -> + fun (Req) -> + case rabbit_web_dispatch_registry:lookup(Listener, Req) of + no_handler -> + Req:not_found(); + {error, Reason} -> + Req:respond({500, [], "Registry Error: " ++ Reason}); + {handler, Handler} -> + Handler(Req) + end + end. + +name(Listener) -> + Port = proplists:get_value(port, Listener), + list_to_atom(atom_to_list(?MODULE) ++ "_" ++ integer_to_list(Port)). + +ssl_config(Options) -> + case proplists:get_value(ssl, Options) of + true -> rabbit_networking:ensure_ssl(), + case rabbit_networking:poodle_check('HTTP') of + ok -> case proplists:get_value(ssl_opts, Options) of + undefined -> auto_ssl(Options); + _ -> fix_ssl(Options) + end; + danger -> proplists:delete(ssl, Options) + end; + _ -> Options + end. + +auto_ssl(Options) -> + {ok, ServerOpts} = application:get_env(rabbit, ssl_options), + Remove = [verify, fail_if_no_peer_cert], + SSLOpts = [{K, V} || {K, V} <- ServerOpts, + not lists:member(K, Remove)], + fix_ssl([{ssl_opts, SSLOpts} | Options]). + +fix_ssl(Options) -> + SSLOpts = proplists:get_value(ssl_opts, Options), + rabbit_misc:pset(ssl_opts, + rabbit_networking:fix_ssl_options(SSLOpts), Options). + +check_error(Listener, Error) -> + Ignore = proplists:get_value(ignore_in_use, Listener, false), + case {Error, Ignore} of + {eaddrinuse, true} -> ignore; + _ -> exit({could_not_start_listener, Listener, Error}) + end. diff --git a/rabbitmq-server/plugins-src/rabbitmq-web-dispatch/src/rabbit_web_dispatch_util.erl b/rabbitmq-server/plugins-src/rabbitmq-web-dispatch/src/rabbit_web_dispatch_util.erl new file mode 100644 index 0000000..c031138 --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-web-dispatch/src/rabbit_web_dispatch_util.erl @@ -0,0 +1,63 @@ +%% The contents of this file are subject to the Mozilla Public License +%% Version 1.1 (the "License"); you may not use this file except in +%% compliance with the License. You may obtain a copy of the License +%% at http://www.mozilla.org/MPL/ +%% +%% Software distributed under the License is distributed on an "AS IS" +%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See +%% the License for the specific language governing rights and +%% limitations under the License. +%% +%% The Original Code is RabbitMQ. +%% +%% The Initial Developer of the Original Code is GoPivotal, Inc. +%% Copyright (c) 2010-2014 GoPivotal, Inc. All rights reserved. +%% + +-module(rabbit_web_dispatch_util). + +-export([parse_auth_header/1]). +-export([relativise/2, unrelativise/2]). + +parse_auth_header(Header) -> + case Header of + "Basic " ++ Base64 -> + Str = base64:mime_decode_to_string(Base64), + case string:chr(Str, $:) of + 0 -> invalid; + N -> [list_to_binary(string:sub_string(Str, 1, N - 1)), + list_to_binary(string:sub_string(Str, N + 1))] + end; + _ -> + invalid + end. + +relativise("/" ++ F, "/" ++ T) -> + From = string:tokens(F, "/"), + To = string:tokens(T, "/"), + string:join(relativise0(From, To), "/"). + +relativise0([H], [H|_] = To) -> + To; +relativise0([H|From], [H|To]) -> + relativise0(From, To); +relativise0(From, []) -> + lists:duplicate(length(From), ".."); +relativise0([_|From], To) -> + lists:duplicate(length(From), "..") ++ To; +relativise0([], To) -> + To. + +unrelativise(F, "/" ++ T) -> "/" ++ T; +unrelativise(F, "./" ++ T) -> unrelativise(F, T); +unrelativise(F, "../" ++ T) -> unrelativise(strip_tail(F), T); +unrelativise(F, T) -> case string:str(F, "/") of + 0 -> T; + _ -> strip_tail(F) ++ "/" ++ T + end. + +strip_tail("") -> exit(not_enough_to_strip); +strip_tail(S) -> case string:rstr(S, "/") of + 0 -> ""; + I -> string:left(S, I - 1) + end. diff --git a/rabbitmq-server/plugins-src/rabbitmq-web-dispatch/src/rabbit_webmachine.erl b/rabbitmq-server/plugins-src/rabbitmq-web-dispatch/src/rabbit_webmachine.erl new file mode 100644 index 0000000..a199a31 --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-web-dispatch/src/rabbit_webmachine.erl @@ -0,0 +1,68 @@ +%% This file contains an adapted version of webmachine_mochiweb:loop/1 +%% from webmachine (revision 0c4b60ac68b4). + +%% All modifications are (C) 2011-2013 GoPivotal, Inc. + +-module(rabbit_webmachine). + +%% An alternative to webmachine_mochiweb, which places the dispatch +%% table (among other things) into the application env, and thereby +%% makes it impossible to run more than one instance of +%% webmachine. Since rabbit_web_dispatch is all about multi-tenanting +%% webapps, clearly this won't do for us. + +%% Instead of using webmachine_mochiweb:start/1 or +%% webmachine_mochiweb:loop/1, construct a loop procedure using +%% makeloop/1 and supply it as the argument to +%% rabbit_web_dispatch:register_context_handler or to mochiweb_http:start. + +%% We hardwire the "error handler" and use a "logging module" if +%% supplied. + +-export([makeloop/1, setup/0]). + +setup() -> + application:set_env( + webmachine, error_handler, rabbit_webmachine_error_handler). + +makeloop(Dispatch) -> + fun (MochiReq) -> + Req = webmachine:new_request(mochiweb, MochiReq), + {Path, _} = Req:path(), + {ReqData, _} = Req:get_reqdata(), + %% webmachine_mochiweb:loop/1 uses dispatch/4 here; + %% however, we don't need to dispatch by the host name. + case webmachine_dispatcher:dispatch(Path, Dispatch, ReqData) of + {no_dispatch_match, _Host, _PathElements} -> + {ErrorBody, ReqState1} = + rabbit_webmachine_error_handler:render_error( + 404, Req, {none, none, []}), + Req1 = {webmachine_request, ReqState1}, + {ok, ReqState2} = Req1:append_to_response_body(ErrorBody), + Req2 = {webmachine_request, ReqState2}, + {ok, ReqState3} = Req2:send_response(404), + maybe_log_access(ReqState3); + {Mod, ModOpts, HostTokens, Port, PathTokens, Bindings, + AppRoot, StringPath} -> + BootstrapResource = webmachine_resource:new(x,x,x,x), + {ok, Resource} = BootstrapResource:wrap(Mod, ModOpts), + {ok, RS1} = Req:load_dispatch_data(Bindings, HostTokens, Port, + PathTokens, + AppRoot, StringPath), + XReq1 = {webmachine_request, RS1}, + {ok, RS2} = XReq1:set_metadata('resource_module', Mod), + try + webmachine_decision_core:handle_request(Resource, RS2) + catch + error:_ -> + FailReq = {webmachine_request, RS2}, + {ok, RS3} = FailReq:send_response(500), + maybe_log_access(RS3) + end + end + end. + +maybe_log_access(ReqState) -> + Req = {webmachine_request, ReqState}, + {LogData, _ReqState1} = Req:log_data(), + webmachine_log:log_access(LogData). diff --git a/rabbitmq-server/plugins-src/rabbitmq-web-dispatch/src/rabbit_webmachine_error_handler.erl b/rabbitmq-server/plugins-src/rabbitmq-web-dispatch/src/rabbit_webmachine_error_handler.erl new file mode 100644 index 0000000..849e5b9 --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-web-dispatch/src/rabbit_webmachine_error_handler.erl @@ -0,0 +1,57 @@ +%% The contents of this file are subject to the Mozilla Public License +%% Version 1.1 (the "License"); you may not use this file except in +%% compliance with the License. You may obtain a copy of the License +%% at http://www.mozilla.org/MPL/ +%% +%% Software distributed under the License is distributed on an "AS IS" +%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See +%% the License for the specific language governing rights and +%% limitations under the License. +%% +%% The Original Code is RabbitMQ. +%% +%% The Initial Developer of the Original Code is GoPivotal, Inc. +%% Copyright (c) 2010-2014 GoPivotal, Inc. All rights reserved. +%% + +%% We need to ensure all responses are application/json; anything +%% coming back as text/html could constitute an XSS vector. Also I'm +%% sure it's easier on our clients if they can always expect JSON +%% responses. +%% +%% Based on webmachine_error_handler, but I'm not sure enough remains +%% to be copyrightable. + +-module(rabbit_webmachine_error_handler). + +-export([render_error/3]). + +render_error(Code, Req, Reason) -> + case Req:has_response_body() of + {true, _} -> maybe_log(Req, Reason), + Req:response_body(); + {false, _} -> render_error_body(Code, Req:trim_state(), Reason) + end. + +render_error_body(404, Req, Reason) -> error_body(404, Req, "Not Found"); +render_error_body(Code, Req, Reason) -> error_body(Code, Req, Reason). + +error_body(Code, Req, Reason) -> + {ok, ReqState} = Req:add_response_header("Content-Type","application/json"), + case Code of + 500 -> maybe_log(Req, Reason); + _ -> ok + end, + Json = {struct, + [{error, list_to_binary(httpd_util:reason_phrase(Code))}, + {reason, list_to_binary(rabbit_misc:format("~p~n", [Reason]))}]}, + {mochijson2:encode(Json), ReqState}. + +maybe_log(_Req, {error, {exit, normal, _Stack}}) -> + %% webmachine_request did an exit(normal), so suppress this + %% message. This usually happens when a chunked upload is + %% interrupted by network failure. + ok; +maybe_log(Req, Reason) -> + {Path, _} = Req:path(), + error_logger:error_msg("webmachine error: path=~p~n~p~n", [Path, Reason]). diff --git a/rabbitmq-server/plugins-src/rabbitmq-web-dispatch/src/rabbitmq_web_dispatch.app.src b/rabbitmq-server/plugins-src/rabbitmq-web-dispatch/src/rabbitmq_web_dispatch.app.src new file mode 100644 index 0000000..5e7dd4d --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-web-dispatch/src/rabbitmq_web_dispatch.app.src @@ -0,0 +1,8 @@ +{application, rabbitmq_web_dispatch, + [{description, "RabbitMQ Web Dispatcher"}, + {vsn, "%%VSN%%"}, + {modules, []}, + {registered, []}, + {mod, {rabbit_web_dispatch_app, []}}, + {env, []}, + {applications, [kernel, stdlib, mochiweb, webmachine]}]}. diff --git a/rabbitmq-server/plugins-src/rabbitmq-web-dispatch/test/priv/www/index.html b/rabbitmq-server/plugins-src/rabbitmq-web-dispatch/test/priv/www/index.html new file mode 100644 index 0000000..b9f7cd4 --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-web-dispatch/test/priv/www/index.html @@ -0,0 +1,7 @@ + + + RabbitMQ HTTP Server Test Page + + + + diff --git a/rabbitmq-server/plugins-src/rabbitmq-web-dispatch/test/src/rabbit_web_dispatch_test.erl b/rabbitmq-server/plugins-src/rabbitmq-web-dispatch/test/src/rabbit_web_dispatch_test.erl new file mode 100644 index 0000000..139dccc --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-web-dispatch/test/src/rabbit_web_dispatch_test.erl @@ -0,0 +1,38 @@ +%% The contents of this file are subject to the Mozilla Public License +%% Version 1.1 (the "License"); you may not use this file except in +%% compliance with the License. You may obtain a copy of the License +%% at http://www.mozilla.org/MPL/ +%% +%% Software distributed under the License is distributed on an "AS IS" +%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See +%% the License for the specific language governing rights and +%% limitations under the License. +%% +%% The Original Code is RabbitMQ. +%% +%% The Initial Developer of the Original Code is GoPivotal, Inc. +%% Copyright (c) 2010-2014 GoPivotal, Inc. All rights reserved. +%% + +-module(rabbit_web_dispatch_test). + +-include_lib("eunit/include/eunit.hrl"). + +query_static_resource_test() -> + %% TODO this is a fairly rubbish test, but not as bad as it was + rabbit_web_dispatch:register_static_context(test, [{port, 12345}], + "rabbit_web_dispatch_test", + ?MODULE, "priv/www", "Test"), + {ok, {_Status, _Headers, Body}} = + httpc:request("http://localhost:12345/rabbit_web_dispatch_test/index.html"), + ?assert(string:str(Body, "RabbitMQ HTTP Server Test Page") /= 0). + +add_idempotence_test() -> + F = fun(_Req) -> ok end, + L = {"/foo", "Foo"}, + rabbit_web_dispatch_registry:add(foo, [{port, 12345}], F, F, L), + rabbit_web_dispatch_registry:add(foo, [{port, 12345}], F, F, L), + ?assertEqual( + 1, length([ok || {"/foo", _, _} <- + rabbit_web_dispatch_registry:list_all()])), + passed. diff --git a/rabbitmq-server/plugins-src/rabbitmq-web-dispatch/test/src/rabbit_web_dispatch_test_unit.erl b/rabbitmq-server/plugins-src/rabbitmq-web-dispatch/test/src/rabbit_web_dispatch_test_unit.erl new file mode 100644 index 0000000..b90ed40 --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-web-dispatch/test/src/rabbit_web_dispatch_test_unit.erl @@ -0,0 +1,36 @@ +%% The contents of this file are subject to the Mozilla Public License +%% Version 1.1 (the "License"); you may not use this file except in +%% compliance with the License. You may obtain a copy of the License +%% at http://www.mozilla.org/MPL/ +%% +%% Software distributed under the License is distributed on an "AS IS" +%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See +%% the License for the specific language governing rights and +%% limitations under the License. +%% +%% The Original Code is RabbitMQ. +%% +%% The Initial Developer of the Original Code is GoPivotal, Inc. +%% Copyright (c) 2010-2014 GoPivotal, Inc. All rights reserved. +%% + +-module(rabbit_web_dispatch_test_unit). + +-include_lib("eunit/include/eunit.hrl"). + +relativise_test() -> + Rel = fun rabbit_web_dispatch_util:relativise/2, + ?assertEqual("baz", Rel("/foo/bar/bash", "/foo/bar/baz")), + ?assertEqual("../bax/baz", Rel("/foo/bar/bash", "/foo/bax/baz")), + ?assertEqual("../bax/baz", Rel("/bar/bash", "/bax/baz")), + ?assertEqual("..", Rel("/foo/bar/bash", "/foo/bar")), + ?assertEqual("../..", Rel("/foo/bar/bash", "/foo")), + ?assertEqual("bar/baz", Rel("/foo/bar", "/foo/bar/baz")), + ?assertEqual("foo", Rel("/", "/foo")). + +unrelativise_test() -> + Un = fun rabbit_web_dispatch_util:unrelativise/2, + ?assertEqual("/foo/bar", Un("/foo/foo", "bar")), + ?assertEqual("/foo/bar", Un("/foo/foo", "./bar")), + ?assertEqual("bar", Un("foo", "bar")), + ?assertEqual("/baz/bar", Un("/foo/foo", "../baz/bar")). diff --git a/rabbitmq-server/plugins-src/rabbitmq-web-stomp-examples/.srcdist_done b/rabbitmq-server/plugins-src/rabbitmq-web-stomp-examples/.srcdist_done new file mode 100644 index 0000000..e69de29 diff --git a/rabbitmq-server/plugins-src/rabbitmq-web-stomp-examples/CONTRIBUTING.md b/rabbitmq-server/plugins-src/rabbitmq-web-stomp-examples/CONTRIBUTING.md new file mode 100644 index 0000000..69a4b4a --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-web-stomp-examples/CONTRIBUTING.md @@ -0,0 +1,51 @@ +## Overview + +RabbitMQ projects use pull requests to discuss, collaborate on and accept code contributions. +Pull requests is the primary place of discussing code changes. + +## How to Contribute + +The process is fairly standard: + + * Fork the repository or repositories you plan on contributing to + * Clone [RabbitMQ umbrella repository](https://github.com/rabbitmq/rabbitmq-public-umbrella) + * `cd umbrella`, `make co` + * Create a branch with a descriptive name in the relevant repositories + * Make your changes, run tests, commit with a [descriptive message](http://tbaggery.com/2008/04/19/a-note-about-git-commit-messages.html), push to your fork + * Submit pull requests with an explanation what has been changed and **why** + * Submit a filled out and signed [Contributor Agreement](https://github.com/rabbitmq/ca#how-to-submit) if needed (see below) + * Be patient. We will get to your pull request eventually + +If what you are going to work on is a substantial change, please first ask the core team +of their opinion on [RabbitMQ mailing list](https://groups.google.com/forum/#!forum/rabbitmq-users). + + +## (Brief) Code of Conduct + +In one line: don't be a dick. + +Be respectful to the maintainers and other contributors. Open source +contributors put long hours into developing projects and doing user +support. Those projects and user support are available for free. We +believe this deserves some respect. + +Be respectful to people of all races, genders, religious beliefs and +political views. Regardless of how brilliant a pull request is +technically, we will not tolerate disrespectful or aggressive +behaviour. + +Contributors who violate this straightforward Code of Conduct will see +their pull requests closed and locked. + + +## Contributor Agreement + +If you want to contribute a non-trivial change, please submit a signed copy of our +[Contributor Agreement](https://github.com/rabbitmq/ca#how-to-submit) around the time +you submit your pull request. This will make it much easier (in some cases, possible) +for the RabbitMQ team at Pivotal to merge your contribution. + + +## Where to Ask Questions + +If something isn't clear, feel free to ask on our [mailing list](https://groups.google.com/forum/#!forum/rabbitmq-users). diff --git a/rabbitmq-server/plugins-src/rabbitmq-web-stomp-examples/LICENSE b/rabbitmq-server/plugins-src/rabbitmq-web-stomp-examples/LICENSE new file mode 100644 index 0000000..012eade --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-web-stomp-examples/LICENSE @@ -0,0 +1,9 @@ +This package, the rabbitmq-web-stomp-examples, is licensed under the +MPL. For the MPL, please see LICENSE-MPL-RabbitMQ. + +priv/stomp.js is a part of stomp-websocket project +(https://github.com/jmesnil/stomp-websocket) and is released under +APL2. For the license see LICENSE-APL2-Stomp-Websocket. + +If you have any questions regarding licensing, please contact us at +info@rabbitmq.com. diff --git a/rabbitmq-server/plugins-src/rabbitmq-web-stomp-examples/LICENSE-APL2-Stomp-Websocket b/rabbitmq-server/plugins-src/rabbitmq-web-stomp-examples/LICENSE-APL2-Stomp-Websocket new file mode 100644 index 0000000..ef51da2 --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-web-stomp-examples/LICENSE-APL2-Stomp-Websocket @@ -0,0 +1,202 @@ + + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + +TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + +1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + +2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + +3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + +4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + +5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + +6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + +7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + +8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + +9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + +END OF TERMS AND CONDITIONS + +APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + +Copyright [yyyy] [name of copyright owner] + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. diff --git a/rabbitmq-server/plugins-src/rabbitmq-web-stomp-examples/LICENSE-MPL-RabbitMQ b/rabbitmq-server/plugins-src/rabbitmq-web-stomp-examples/LICENSE-MPL-RabbitMQ new file mode 100644 index 0000000..c87c1a3 --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-web-stomp-examples/LICENSE-MPL-RabbitMQ @@ -0,0 +1,455 @@ + MOZILLA PUBLIC LICENSE + Version 1.1 + + --------------- + +1. Definitions. + + 1.0.1. "Commercial Use" means distribution or otherwise making the + Covered Code available to a third party. + + 1.1. "Contributor" means each entity that creates or contributes to + the creation of Modifications. + + 1.2. "Contributor Version" means the combination of the Original + Code, prior Modifications used by a Contributor, and the Modifications + made by that particular Contributor. + + 1.3. "Covered Code" means the Original Code or Modifications or the + combination of the Original Code and Modifications, in each case + including portions thereof. + + 1.4. "Electronic Distribution Mechanism" means a mechanism generally + accepted in the software development community for the electronic + transfer of data. + + 1.5. "Executable" means Covered Code in any form other than Source + Code. + + 1.6. "Initial Developer" means the individual or entity identified + as the Initial Developer in the Source Code notice required by Exhibit + A. + + 1.7. "Larger Work" means a work which combines Covered Code or + portions thereof with code not governed by the terms of this License. + + 1.8. "License" means this document. + + 1.8.1. "Licensable" means having the right to grant, to the maximum + extent possible, whether at the time of the initial grant or + subsequently acquired, any and all of the rights conveyed herein. + + 1.9. "Modifications" means any addition to or deletion from the + substance or structure of either the Original Code or any previous + Modifications. When Covered Code is released as a series of files, a + Modification is: + A. Any addition to or deletion from the contents of a file + containing Original Code or previous Modifications. + + B. Any new file that contains any part of the Original Code or + previous Modifications. + + 1.10. "Original Code" means Source Code of computer software code + which is described in the Source Code notice required by Exhibit A as + Original Code, and which, at the time of its release under this + License is not already Covered Code governed by this License. + + 1.10.1. "Patent Claims" means any patent claim(s), now owned or + hereafter acquired, including without limitation, method, process, + and apparatus claims, in any patent Licensable by grantor. + + 1.11. "Source Code" means the preferred form of the Covered Code for + making modifications to it, including all modules it contains, plus + any associated interface definition files, scripts used to control + compilation and installation of an Executable, or source code + differential comparisons against either the Original Code or another + well known, available Covered Code of the Contributor's choice. The + Source Code can be in a compressed or archival form, provided the + appropriate decompression or de-archiving software is widely available + for no charge. + + 1.12. "You" (or "Your") means an individual or a legal entity + exercising rights under, and complying with all of the terms of, this + License or a future version of this License issued under Section 6.1. + For legal entities, "You" includes any entity which controls, is + controlled by, or is under common control with You. For purposes of + this definition, "control" means (a) the power, direct or indirect, + to cause the direction or management of such entity, whether by + contract or otherwise, or (b) ownership of more than fifty percent + (50%) of the outstanding shares or beneficial ownership of such + entity. + +2. Source Code License. + + 2.1. The Initial Developer Grant. + The Initial Developer hereby grants You a world-wide, royalty-free, + non-exclusive license, subject to third party intellectual property + claims: + (a) under intellectual property rights (other than patent or + trademark) Licensable by Initial Developer to use, reproduce, + modify, display, perform, sublicense and distribute the Original + Code (or portions thereof) with or without Modifications, and/or + as part of a Larger Work; and + + (b) under Patents Claims infringed by the making, using or + selling of Original Code, to make, have made, use, practice, + sell, and offer for sale, and/or otherwise dispose of the + Original Code (or portions thereof). + + (c) the licenses granted in this Section 2.1(a) and (b) are + effective on the date Initial Developer first distributes + Original Code under the terms of this License. + + (d) Notwithstanding Section 2.1(b) above, no patent license is + granted: 1) for code that You delete from the Original Code; 2) + separate from the Original Code; or 3) for infringements caused + by: i) the modification of the Original Code or ii) the + combination of the Original Code with other software or devices. + + 2.2. Contributor Grant. + Subject to third party intellectual property claims, each Contributor + hereby grants You a world-wide, royalty-free, non-exclusive license + + (a) under intellectual property rights (other than patent or + trademark) Licensable by Contributor, to use, reproduce, modify, + display, perform, sublicense and distribute the Modifications + created by such Contributor (or portions thereof) either on an + unmodified basis, with other Modifications, as Covered Code + and/or as part of a Larger Work; and + + (b) under Patent Claims infringed by the making, using, or + selling of Modifications made by that Contributor either alone + and/or in combination with its Contributor Version (or portions + of such combination), to make, use, sell, offer for sale, have + made, and/or otherwise dispose of: 1) Modifications made by that + Contributor (or portions thereof); and 2) the combination of + Modifications made by that Contributor with its Contributor + Version (or portions of such combination). + + (c) the licenses granted in Sections 2.2(a) and 2.2(b) are + effective on the date Contributor first makes Commercial Use of + the Covered Code. + + (d) Notwithstanding Section 2.2(b) above, no patent license is + granted: 1) for any code that Contributor has deleted from the + Contributor Version; 2) separate from the Contributor Version; + 3) for infringements caused by: i) third party modifications of + Contributor Version or ii) the combination of Modifications made + by that Contributor with other software (except as part of the + Contributor Version) or other devices; or 4) under Patent Claims + infringed by Covered Code in the absence of Modifications made by + that Contributor. + +3. Distribution Obligations. + + 3.1. Application of License. + The Modifications which You create or to which You contribute are + governed by the terms of this License, including without limitation + Section 2.2. The Source Code version of Covered Code may be + distributed only under the terms of this License or a future version + of this License released under Section 6.1, and You must include a + copy of this License with every copy of the Source Code You + distribute. You may not offer or impose any terms on any Source Code + version that alters or restricts the applicable version of this + License or the recipients' rights hereunder. However, You may include + an additional document offering the additional rights described in + Section 3.5. + + 3.2. Availability of Source Code. + Any Modification which You create or to which You contribute must be + made available in Source Code form under the terms of this License + either on the same media as an Executable version or via an accepted + Electronic Distribution Mechanism to anyone to whom you made an + Executable version available; and if made available via Electronic + Distribution Mechanism, must remain available for at least twelve (12) + months after the date it initially became available, or at least six + (6) months after a subsequent version of that particular Modification + has been made available to such recipients. You are responsible for + ensuring that the Source Code version remains available even if the + Electronic Distribution Mechanism is maintained by a third party. + + 3.3. Description of Modifications. + You must cause all Covered Code to which You contribute to contain a + file documenting the changes You made to create that Covered Code and + the date of any change. You must include a prominent statement that + the Modification is derived, directly or indirectly, from Original + Code provided by the Initial Developer and including the name of the + Initial Developer in (a) the Source Code, and (b) in any notice in an + Executable version or related documentation in which You describe the + origin or ownership of the Covered Code. + + 3.4. Intellectual Property Matters + (a) Third Party Claims. + If Contributor has knowledge that a license under a third party's + intellectual property rights is required to exercise the rights + granted by such Contributor under Sections 2.1 or 2.2, + Contributor must include a text file with the Source Code + distribution titled "LEGAL" which describes the claim and the + party making the claim in sufficient detail that a recipient will + know whom to contact. If Contributor obtains such knowledge after + the Modification is made available as described in Section 3.2, + Contributor shall promptly modify the LEGAL file in all copies + Contributor makes available thereafter and shall take other steps + (such as notifying appropriate mailing lists or newsgroups) + reasonably calculated to inform those who received the Covered + Code that new knowledge has been obtained. + + (b) Contributor APIs. + If Contributor's Modifications include an application programming + interface and Contributor has knowledge of patent licenses which + are reasonably necessary to implement that API, Contributor must + also include this information in the LEGAL file. + + (c) Representations. + Contributor represents that, except as disclosed pursuant to + Section 3.4(a) above, Contributor believes that Contributor's + Modifications are Contributor's original creation(s) and/or + Contributor has sufficient rights to grant the rights conveyed by + this License. + + 3.5. Required Notices. + You must duplicate the notice in Exhibit A in each file of the Source + Code. If it is not possible to put such notice in a particular Source + Code file due to its structure, then You must include such notice in a + location (such as a relevant directory) where a user would be likely + to look for such a notice. If You created one or more Modification(s) + You may add your name as a Contributor to the notice described in + Exhibit A. You must also duplicate this License in any documentation + for the Source Code where You describe recipients' rights or ownership + rights relating to Covered Code. You may choose to offer, and to + charge a fee for, warranty, support, indemnity or liability + obligations to one or more recipients of Covered Code. However, You + may do so only on Your own behalf, and not on behalf of the Initial + Developer or any Contributor. You must make it absolutely clear than + any such warranty, support, indemnity or liability obligation is + offered by You alone, and You hereby agree to indemnify the Initial + Developer and every Contributor for any liability incurred by the + Initial Developer or such Contributor as a result of warranty, + support, indemnity or liability terms You offer. + + 3.6. Distribution of Executable Versions. + You may distribute Covered Code in Executable form only if the + requirements of Section 3.1-3.5 have been met for that Covered Code, + and if You include a notice stating that the Source Code version of + the Covered Code is available under the terms of this License, + including a description of how and where You have fulfilled the + obligations of Section 3.2. The notice must be conspicuously included + in any notice in an Executable version, related documentation or + collateral in which You describe recipients' rights relating to the + Covered Code. You may distribute the Executable version of Covered + Code or ownership rights under a license of Your choice, which may + contain terms different from this License, provided that You are in + compliance with the terms of this License and that the license for the + Executable version does not attempt to limit or alter the recipient's + rights in the Source Code version from the rights set forth in this + License. If You distribute the Executable version under a different + license You must make it absolutely clear that any terms which differ + from this License are offered by You alone, not by the Initial + Developer or any Contributor. You hereby agree to indemnify the + Initial Developer and every Contributor for any liability incurred by + the Initial Developer or such Contributor as a result of any such + terms You offer. + + 3.7. Larger Works. + You may create a Larger Work by combining Covered Code with other code + not governed by the terms of this License and distribute the Larger + Work as a single product. In such a case, You must make sure the + requirements of this License are fulfilled for the Covered Code. + +4. Inability to Comply Due to Statute or Regulation. + + If it is impossible for You to comply with any of the terms of this + License with respect to some or all of the Covered Code due to + statute, judicial order, or regulation then You must: (a) comply with + the terms of this License to the maximum extent possible; and (b) + describe the limitations and the code they affect. Such description + must be included in the LEGAL file described in Section 3.4 and must + be included with all distributions of the Source Code. Except to the + extent prohibited by statute or regulation, such description must be + sufficiently detailed for a recipient of ordinary skill to be able to + understand it. + +5. Application of this License. + + This License applies to code to which the Initial Developer has + attached the notice in Exhibit A and to related Covered Code. + +6. Versions of the License. + + 6.1. New Versions. + Netscape Communications Corporation ("Netscape") may publish revised + and/or new versions of the License from time to time. Each version + will be given a distinguishing version number. + + 6.2. Effect of New Versions. + Once Covered Code has been published under a particular version of the + License, You may always continue to use it under the terms of that + version. You may also choose to use such Covered Code under the terms + of any subsequent version of the License published by Netscape. No one + other than Netscape has the right to modify the terms applicable to + Covered Code created under this License. + + 6.3. Derivative Works. + If You create or use a modified version of this License (which you may + only do in order to apply it to code which is not already Covered Code + governed by this License), You must (a) rename Your license so that + the phrases "Mozilla", "MOZILLAPL", "MOZPL", "Netscape", + "MPL", "NPL" or any confusingly similar phrase do not appear in your + license (except to note that your license differs from this License) + and (b) otherwise make it clear that Your version of the license + contains terms which differ from the Mozilla Public License and + Netscape Public License. (Filling in the name of the Initial + Developer, Original Code or Contributor in the notice described in + Exhibit A shall not of themselves be deemed to be modifications of + this License.) + +7. DISCLAIMER OF WARRANTY. + + COVERED CODE IS PROVIDED UNDER THIS LICENSE ON AN "AS IS" BASIS, + WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, + WITHOUT LIMITATION, WARRANTIES THAT THE COVERED CODE IS FREE OF + DEFECTS, MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE OR NON-INFRINGING. + THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE COVERED CODE + IS WITH YOU. SHOULD ANY COVERED CODE PROVE DEFECTIVE IN ANY RESPECT, + YOU (NOT THE INITIAL DEVELOPER OR ANY OTHER CONTRIBUTOR) ASSUME THE + COST OF ANY NECESSARY SERVICING, REPAIR OR CORRECTION. THIS DISCLAIMER + OF WARRANTY CONSTITUTES AN ESSENTIAL PART OF THIS LICENSE. NO USE OF + ANY COVERED CODE IS AUTHORIZED HEREUNDER EXCEPT UNDER THIS DISCLAIMER. + +8. TERMINATION. + + 8.1. This License and the rights granted hereunder will terminate + automatically if You fail to comply with terms herein and fail to cure + such breach within 30 days of becoming aware of the breach. All + sublicenses to the Covered Code which are properly granted shall + survive any termination of this License. Provisions which, by their + nature, must remain in effect beyond the termination of this License + shall survive. + + 8.2. If You initiate litigation by asserting a patent infringement + claim (excluding declatory judgment actions) against Initial Developer + or a Contributor (the Initial Developer or Contributor against whom + You file such action is referred to as "Participant") alleging that: + + (a) such Participant's Contributor Version directly or indirectly + infringes any patent, then any and all rights granted by such + Participant to You under Sections 2.1 and/or 2.2 of this License + shall, upon 60 days notice from Participant terminate prospectively, + unless if within 60 days after receipt of notice You either: (i) + agree in writing to pay Participant a mutually agreeable reasonable + royalty for Your past and future use of Modifications made by such + Participant, or (ii) withdraw Your litigation claim with respect to + the Contributor Version against such Participant. If within 60 days + of notice, a reasonable royalty and payment arrangement are not + mutually agreed upon in writing by the parties or the litigation claim + is not withdrawn, the rights granted by Participant to You under + Sections 2.1 and/or 2.2 automatically terminate at the expiration of + the 60 day notice period specified above. + + (b) any software, hardware, or device, other than such Participant's + Contributor Version, directly or indirectly infringes any patent, then + any rights granted to You by such Participant under Sections 2.1(b) + and 2.2(b) are revoked effective as of the date You first made, used, + sold, distributed, or had made, Modifications made by that + Participant. + + 8.3. If You assert a patent infringement claim against Participant + alleging that such Participant's Contributor Version directly or + indirectly infringes any patent where such claim is resolved (such as + by license or settlement) prior to the initiation of patent + infringement litigation, then the reasonable value of the licenses + granted by such Participant under Sections 2.1 or 2.2 shall be taken + into account in determining the amount or value of any payment or + license. + + 8.4. In the event of termination under Sections 8.1 or 8.2 above, + all end user license agreements (excluding distributors and resellers) + which have been validly granted by You or any distributor hereunder + prior to termination shall survive termination. + +9. LIMITATION OF LIABILITY. + + UNDER NO CIRCUMSTANCES AND UNDER NO LEGAL THEORY, WHETHER TORT + (INCLUDING NEGLIGENCE), CONTRACT, OR OTHERWISE, SHALL YOU, THE INITIAL + DEVELOPER, ANY OTHER CONTRIBUTOR, OR ANY DISTRIBUTOR OF COVERED CODE, + OR ANY SUPPLIER OF ANY OF SUCH PARTIES, BE LIABLE TO ANY PERSON FOR + ANY INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES OF ANY + CHARACTER INCLUDING, WITHOUT LIMITATION, DAMAGES FOR LOSS OF GOODWILL, + WORK STOPPAGE, COMPUTER FAILURE OR MALFUNCTION, OR ANY AND ALL OTHER + COMMERCIAL DAMAGES OR LOSSES, EVEN IF SUCH PARTY SHALL HAVE BEEN + INFORMED OF THE POSSIBILITY OF SUCH DAMAGES. THIS LIMITATION OF + LIABILITY SHALL NOT APPLY TO LIABILITY FOR DEATH OR PERSONAL INJURY + RESULTING FROM SUCH PARTY'S NEGLIGENCE TO THE EXTENT APPLICABLE LAW + PROHIBITS SUCH LIMITATION. SOME JURISDICTIONS DO NOT ALLOW THE + EXCLUSION OR LIMITATION OF INCIDENTAL OR CONSEQUENTIAL DAMAGES, SO + THIS EXCLUSION AND LIMITATION MAY NOT APPLY TO YOU. + +10. U.S. GOVERNMENT END USERS. + + The Covered Code is a "commercial item," as that term is defined in + 48 C.F.R. 2.101 (Oct. 1995), consisting of "commercial computer + software" and "commercial computer software documentation," as such + terms are used in 48 C.F.R. 12.212 (Sept. 1995). Consistent with 48 + C.F.R. 12.212 and 48 C.F.R. 227.7202-1 through 227.7202-4 (June 1995), + all U.S. Government End Users acquire Covered Code with only those + rights set forth herein. + +11. MISCELLANEOUS. + + This License represents the complete agreement concerning subject + matter hereof. If any provision of this License is held to be + unenforceable, such provision shall be reformed only to the extent + necessary to make it enforceable. This License shall be governed by + California law provisions (except to the extent applicable law, if + any, provides otherwise), excluding its conflict-of-law provisions. + With respect to disputes in which at least one party is a citizen of, + or an entity chartered or registered to do business in the United + States of America, any litigation relating to this License shall be + subject to the jurisdiction of the Federal Courts of the Northern + District of California, with venue lying in Santa Clara County, + California, with the losing party responsible for costs, including + without limitation, court costs and reasonable attorneys' fees and + expenses. The application of the United Nations Convention on + Contracts for the International Sale of Goods is expressly excluded. + Any law or regulation which provides that the language of a contract + shall be construed against the drafter shall not apply to this + License. + +12. RESPONSIBILITY FOR CLAIMS. + + As between Initial Developer and the Contributors, each party is + responsible for claims and damages arising, directly or indirectly, + out of its utilization of rights under this License and You agree to + work with Initial Developer and Contributors to distribute such + responsibility on an equitable basis. Nothing herein is intended or + shall be deemed to constitute any admission of liability. + +13. MULTIPLE-LICENSED CODE. + + Initial Developer may designate portions of the Covered Code as + "Multiple-Licensed". "Multiple-Licensed" means that the Initial + Developer permits you to utilize portions of the Covered Code under + Your choice of the NPL or the alternative licenses, if any, specified + by the Initial Developer in the file described in Exhibit A. + +EXHIBIT A -Mozilla Public License. + + ``The contents of this file are subject to the Mozilla Public License + Version 1.1 (the "License"); you may not use this file except in + compliance with the License. You may obtain a copy of the License at + http://www.mozilla.org/MPL/ + + Software distributed under the License is distributed on an "AS IS" + basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the + License for the specific language governing rights and limitations + under the License. + + The Original Code is RabbitMQ. + + The Initial Developer of the Original Code is GoPivotal, Inc. + Copyright (c) 2007-2014 GoPivotal, Inc. All rights reserved.'' + + [NOTE: The text of this Exhibit A may differ slightly from the text of + the notices in the Source Code files of the Original Code. You should + use the text of this Exhibit A rather than the text found in the + Original Code Source Code for Your Modifications.] diff --git a/rabbitmq-server/plugins-src/rabbitmq-web-stomp-examples/Makefile b/rabbitmq-server/plugins-src/rabbitmq-web-stomp-examples/Makefile new file mode 100644 index 0000000..482105a --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-web-stomp-examples/Makefile @@ -0,0 +1 @@ +include ../umbrella.mk diff --git a/rabbitmq-server/plugins-src/rabbitmq-web-stomp-examples/README.md b/rabbitmq-server/plugins-src/rabbitmq-web-stomp-examples/README.md new file mode 100644 index 0000000..0008d57 --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-web-stomp-examples/README.md @@ -0,0 +1,21 @@ +RabbitMQ-Web-Stomp-Examples plugin +================================== + +This project contains few basic examples of RabbitMq-Web-Stomp plugin +usage. + +Once installed the server will bind to port 15670 and serve few static +html files from there: + + * http://127.0.0.1:15670/ + +Installation +------------ + +Generic build instructions are at: + + * http://www.rabbitmq.com/plugin-development.html + +Instructions on how to install a plugin into RabbitMQ broker: + + * http://www.rabbitmq.com/plugins.html#installing-plugins diff --git a/rabbitmq-server/plugins-src/rabbitmq-web-stomp-examples/package.mk b/rabbitmq-server/plugins-src/rabbitmq-web-stomp-examples/package.mk new file mode 100644 index 0000000..01e3b9d --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-web-stomp-examples/package.mk @@ -0,0 +1,6 @@ +RELEASABLE:=true +DEPS:=rabbitmq-web-dispatch rabbitmq-web-stomp rabbitmq-server + +define construct_app_commands + cp -r $(PACKAGE_DIR)/priv $(APP_DIR) +endef diff --git a/rabbitmq-server/plugins-src/rabbitmq-web-stomp-examples/priv/bunny.html b/rabbitmq-server/plugins-src/rabbitmq-web-stomp-examples/priv/bunny.html new file mode 100644 index 0000000..15f686d --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-web-stomp-examples/priv/bunny.html @@ -0,0 +1,141 @@ + + + + + + + + RabbitMQ Web STOMP Examples: Bunny Drawing + + +

    RabbitMQ Web STOMP Examples > Bunny Drawing

    + + + diff --git a/rabbitmq-server/plugins-src/rabbitmq-web-stomp-examples/priv/bunny.png b/rabbitmq-server/plugins-src/rabbitmq-web-stomp-examples/priv/bunny.png new file mode 100644 index 0000000..6c2284b Binary files /dev/null and b/rabbitmq-server/plugins-src/rabbitmq-web-stomp-examples/priv/bunny.png differ diff --git a/rabbitmq-server/plugins-src/rabbitmq-web-stomp-examples/priv/echo.html b/rabbitmq-server/plugins-src/rabbitmq-web-stomp-examples/priv/echo.html new file mode 100644 index 0000000..2119dfd --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-web-stomp-examples/priv/echo.html @@ -0,0 +1,111 @@ + + + + + + + RabbitMQ Web STOMP Examples : Echo Server + + +

    RabbitMQ Web STOMP Examples > Echo Server

    + +
    +

    Received

    +
    +
    +
    + +
    +

    Logs

    +
    +
    + + + diff --git a/rabbitmq-server/plugins-src/rabbitmq-web-stomp-examples/priv/index.html b/rabbitmq-server/plugins-src/rabbitmq-web-stomp-examples/priv/index.html new file mode 100644 index 0000000..f722add --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-web-stomp-examples/priv/index.html @@ -0,0 +1,16 @@ + + + + + RabbitMQ Web STOMP Examples + + + +

    RabbitMQ Web STOMP Examples

    + + + diff --git a/rabbitmq-server/plugins-src/rabbitmq-web-stomp-examples/priv/main.css b/rabbitmq-server/plugins-src/rabbitmq-web-stomp-examples/priv/main.css new file mode 100644 index 0000000..a5cdcda --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-web-stomp-examples/priv/main.css @@ -0,0 +1,38 @@ +body { + font-family: "Arial"; + color: #444; +} + +h1, h2 { + color: #f60; + font-weight: normal; +} + +h1 { + font-size: 1.5em; +} + +h2 { + font-size: 1.2em; + margin: 0; +} + +a { + color: #f60; + border: 1px solid #fda; + background: #fff0e0; + border-radius: 3px; -moz-border-radius: 3px; + padding: 2px; + text-decoration: none; + /* font-weight: bold; */ +} + +ul.menu { + list-style-type: none; + padding: 0; + margin: 0; +} + +ul.menu li { + padding: 5px 0; +} diff --git a/rabbitmq-server/plugins-src/rabbitmq-web-stomp-examples/priv/pencil.cur b/rabbitmq-server/plugins-src/rabbitmq-web-stomp-examples/priv/pencil.cur new file mode 100755 index 0000000..a3e3598 Binary files /dev/null and b/rabbitmq-server/plugins-src/rabbitmq-web-stomp-examples/priv/pencil.cur differ diff --git a/rabbitmq-server/plugins-src/rabbitmq-web-stomp-examples/priv/sockjs-0.3.js b/rabbitmq-server/plugins-src/rabbitmq-web-stomp-examples/priv/sockjs-0.3.js new file mode 100644 index 0000000..585215c --- /dev/null +++ b/rabbitmq-server/plugins-src/rabbitmq-web-stomp-examples/priv/sockjs-0.3.js @@ -0,0 +1,2379 @@ +/* SockJS client, version 0.3.4, http://sockjs.org, MIT License + +Copyright (c) 2011-2012 VMware, Inc. + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. +*/ + +// JSON2 by Douglas Crockford (minified). +var JSON;JSON||(JSON={}),function(){function str(a,b){var c,d,e,f,g=gap,h,i=b[a];i&&typeof i=="object"&&typeof i.toJSON=="function"&&(i=i.toJSON(a)),typeof rep=="function"&&(i=rep.call(b,a,i));switch(typeof i){case"string":return quote(i);case"number":return isFinite(i)?String(i):"null";case"boolean":case"null":return String(i);case"object":if(!i)return"null";gap+=indent,h=[];if(Object.prototype.toString.apply(i)==="[object Array]"){f=i.length;for(c=0;c 1) { + this._listeners[eventType] = arr.slice(0, idx).concat( arr.slice(idx+1) ); + } else { + delete this._listeners[eventType]; + } + return; + } + return; +}; + +REventTarget.prototype.dispatchEvent = function (event) { + var t = event.type; + var args = Array.prototype.slice.call(arguments, 0); + if (this['on'+t]) { + this['on'+t].apply(this, args); + } + if (this._listeners && t in this._listeners) { + for(var i=0; i < this._listeners[t].length; i++) { + this._listeners[t][i].apply(this, args); + } + } +}; +// [*] End of lib/reventtarget.js + + +// [*] Including lib/simpleevent.js +/* + * ***** BEGIN LICENSE BLOCK ***** + * Copyright (c) 2011-2012 VMware, Inc. + * + * For the license see COPYING. + * ***** END LICENSE BLOCK ***** + */ + +var SimpleEvent = function(type, obj) { + this.type = type; + if (typeof obj !== 'undefined') { + for(var k in obj) { + if (!obj.hasOwnProperty(k)) continue; + this[k] = obj[k]; + } + } +}; + +SimpleEvent.prototype.toString = function() { + var r = []; + for(var k in this) { + if (!this.hasOwnProperty(k)) continue; + var v = this[k]; + if (typeof v === 'function') v = '[function]'; + r.push(k + '=' + v); + } + return 'SimpleEvent(' + r.join(', ') + ')'; +}; +// [*] End of lib/simpleevent.js + + +// [*] Including lib/eventemitter.js +/* + * ***** BEGIN LICENSE BLOCK ***** + * Copyright (c) 2011-2012 VMware, Inc. + * + * For the license see COPYING. + * ***** END LICENSE BLOCK ***** + */ + +var EventEmitter = function(events) { + var that = this; + that._events = events || []; + that._listeners = {}; +}; +EventEmitter.prototype.emit = function(type) { + var that = this; + that._verifyType(type); + if (that._nuked) return; + + var args = Array.prototype.slice.call(arguments, 1); + if (that['on'+type]) { + that['on'+type].apply(that, args); + } + if (type in that._listeners) { + for(var i = 0; i < that._listeners[type].length; i++) { + that._listeners[type][i].apply(that, args); + } + } +}; + +EventEmitter.prototype.on = function(type, callback) { + var that = this; + that._verifyType(type); + if (that._nuked) return; + + if (!(type in that._listeners)) { + that._listeners[type] = []; + } + that._listeners[type].push(callback); +}; + +EventEmitter.prototype._verifyType = function(type) { + var that = this; + if (utils.arrIndexOf(that._events, type) === -1) { + utils.log('Event ' + JSON.stringify(type) + + ' not listed ' + JSON.stringify(that._events) + + ' in ' + that); + } +}; + +EventEmitter.prototype.nuke = function() { + var that = this; + that._nuked = true; + for(var i=0; i= 3000 && code <= 4999); +}; + +// See: http://www.erg.abdn.ac.uk/~gerrit/dccp/notes/ccid2/rto_estimator/ +// and RFC 2988. +utils.countRTO = function (rtt) { + var rto; + if (rtt > 100) { + rto = 3 * rtt; // rto > 300msec + } else { + rto = rtt + 200; // 200msec < rto <= 300msec + } + return rto; +} + +utils.log = function() { + if (_window.console && console.log && console.log.apply) { + console.log.apply(console, arguments); + } +}; + +utils.bind = function(fun, that) { + if (fun.bind) { + return fun.bind(that); + } else { + return function() { + return fun.apply(that, arguments); + }; + } +}; + +utils.flatUrl = function(url) { + return url.indexOf('?') === -1 && url.indexOf('#') === -1; +}; + +utils.amendUrl = function(url) { + var dl = _document.location; + if (!url) { + throw new Error('Wrong url for SockJS'); + } + if (!utils.flatUrl(url)) { + throw new Error('Only basic urls are supported in SockJS'); + } + + // '//abc' --> 'http://abc' + if (url.indexOf('//') === 0) { + url = dl.protocol + url; + } + // '/abc' --> 'http://localhost:80/abc' + if (url.indexOf('/') === 0) { + url = dl.protocol + '//' + dl.host + url; + } + // strip trailing slashes + url = url.replace(/[/]+$/,''); + return url; +}; + +// IE doesn't support [].indexOf. +utils.arrIndexOf = function(arr, obj){ + for(var i=0; i < arr.length; i++){ + if(arr[i] === obj){ + return i; + } + } + return -1; +}; + +utils.arrSkip = function(arr, obj) { + var idx = utils.arrIndexOf(arr, obj); + if (idx === -1) { + return arr.slice(); + } else { + var dst = arr.slice(0, idx); + return dst.concat(arr.slice(idx+1)); + } +}; + +// Via: https://gist.github.com/1133122/2121c601c5549155483f50be3da5305e83b8c5df +utils.isArray = Array.isArray || function(value) { + return {}.toString.call(value).indexOf('Array') >= 0 +}; + +utils.delay = function(t, fun) { + if(typeof t === 'function') { + fun = t; + t = 0; + } + return setTimeout(fun, t); +}; + + +// Chars worth escaping, as defined by Douglas Crockford: +// https://github.com/douglascrockford/JSON-js/blob/47a9882cddeb1e8529e07af9736218075372b8ac/json2.js#L196 +var json_escapable = /[\\\"\x00-\x1f\x7f-\x9f\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g, + json_lookup = { +"\u0000":"\\u0000","\u0001":"\\u0001","\u0002":"\\u0002","\u0003":"\\u0003", +"\u0004":"\\u0004","\u0005":"\\u0005","\u0006":"\\u0006","\u0007":"\\u0007", +"\b":"\\b","\t":"\\t","\n":"\\n","\u000b":"\\u000b","\f":"\\f","\r":"\\r", +"\u000e":"\\u000e","\u000f":"\\u000f","\u0010":"\\u0010","\u0011":"\\u0011", +"\u0012":"\\u0012","\u0013":"\\u0013","\u0014":"\\u0014","\u0015":"\\u0015", +"\u0016":"\\u0016","\u0017":"\\u0017","\u0018":"\\u0018","\u0019":"\\u0019", +"\u001a":"\\u001a","\u001b":"\\u001b","\u001c":"\\u001c","\u001d":"\\u001d", +"\u001e":"\\u001e","\u001f":"\\u001f","\"":"\\\"","\\":"\\\\", +"\u007f":"\\u007f","\u0080":"\\u0080","\u0081":"\\u0081","\u0082":"\\u0082", +"\u0083":"\\u0083","\u0084":"\\u0084","\u0085":"\\u0085","\u0086":"\\u0086", +"\u0087":"\\u0087","\u0088":"\\u0088","\u0089":"\\u0089","\u008a":"\\u008a", +"\u008b":"\\u008b","\u008c":"\\u008c","\u008d":"\\u008d","\u008e":"\\u008e", +"\u008f":"\\u008f","\u0090":"\\u0090","\u0091":"\\u0091","\u0092":"\\u0092", +"\u0093":"\\u0093","\u0094":"\\u0094","\u0095":"\\u0095","\u0096":"\\u0096", +"\u0097":"\\u0097","\u0098":"\\u0098","\u0099":"\\u0099","\u009a":"\\u009a", +"\u009b":"\\u009b","\u009c":"\\u009c","\u009d":"\\u009d","\u009e":"\\u009e", +"\u009f":"\\u009f","\u00ad":"\\u00ad","\u0600":"\\u0600","\u0601":"\\u0601", +"\u0602":"\\u0602","\u0603":"\\u0603","\u0604":"\\u0604","\u070f":"\\u070f", +"\u17b4":"\\u17b4","\u17b5":"\\u17b5","\u200c":"\\u200c","\u200d":"\\u200d", +"\u200e":"\\u200e","\u200f":"\\u200f","\u2028":"\\u2028","\u2029":"\\u2029", +"\u202a":"\\u202a","\u202b":"\\u202b","\u202c":"\\u202c","\u202d":"\\u202d", +"\u202e":"\\u202e","\u202f":"\\u202f","\u2060":"\\u2060","\u2061":"\\u2061", +"\u2062":"\\u2062","\u2063":"\\u2063","\u2064":"\\u2064","\u2065":"\\u2065", +"\u2066":"\\u2066","\u2067":"\\u2067","\u2068":"\\u2068","\u2069":"\\u2069", +"\u206a":"\\u206a","\u206b":"\\u206b","\u206c":"\\u206c","\u206d":"\\u206d", +"\u206e":"\\u206e","\u206f":"\\u206f","\ufeff":"\\ufeff","\ufff0":"\\ufff0", +"\ufff1":"\\ufff1","\ufff2":"\\ufff2","\ufff3":"\\ufff3","\ufff4":"\\ufff4", +"\ufff5":"\\ufff5","\ufff6":"\\ufff6","\ufff7":"\\ufff7","\ufff8":"\\ufff8", +"\ufff9":"\\ufff9","\ufffa":"\\ufffa","\ufffb":"\\ufffb","\ufffc":"\\ufffc", +"\ufffd":"\\ufffd","\ufffe":"\\ufffe","\uffff":"\\uffff"}; + +// Some extra characters that Chrome gets wrong, and substitutes with +// something else on the wire. +var extra_escapable = /[\x00-\x1f\ud800-\udfff\ufffe\uffff\u0300-\u0333\u033d-\u0346\u034a-\u034c\u0350-\u0352\u0357-\u0358\u035c-\u0362\u0374\u037e\u0387\u0591-\u05af\u05c4\u0610-\u0617\u0653-\u0654\u0657-\u065b\u065d-\u065e\u06df-\u06e2\u06eb-\u06ec\u0730\u0732-\u0733\u0735-\u0736\u073a\u073d\u073f-\u0741\u0743\u0745\u0747\u07eb-\u07f1\u0951\u0958-\u095f\u09dc-\u09dd\u09df\u0a33\u0a36\u0a59-\u0a5b\u0a5e\u0b5c-\u0b5d\u0e38-\u0e39\u0f43\u0f4d\u0f52\u0f57\u0f5c\u0f69\u0f72-\u0f76\u0f78\u0f80-\u0f83\u0f93\u0f9d\u0fa2\u0fa7\u0fac\u0fb9\u1939-\u193a\u1a17\u1b6b\u1cda-\u1cdb\u1dc0-\u1dcf\u1dfc\u1dfe\u1f71\u1f73\u1f75\u1f77\u1f79\u1f7b\u1f7d\u1fbb\u1fbe\u1fc9\u1fcb\u1fd3\u1fdb\u1fe3\u1feb\u1fee-\u1fef\u1ff9\u1ffb\u1ffd\u2000-\u2001\u20d0-\u20d1\u20d4-\u20d7\u20e7-\u20e9\u2126\u212a-\u212b\u2329-\u232a\u2adc\u302b-\u302c\uaab2-\uaab3\uf900-\ufa0d\ufa10\ufa12\ufa15-\ufa1e\ufa20\ufa22\ufa25-\ufa26\ufa2a-\ufa2d\ufa30-\ufa6d\ufa70-\ufad9\ufb1d\ufb1f\ufb2a-\ufb36\ufb38-\ufb3c\ufb3e\ufb40-\ufb41\ufb43-\ufb44\ufb46-\ufb4e\ufff0-\uffff]/g, + extra_lookup; + +// JSON Quote string. Use native implementation when possible. +var JSONQuote = (JSON && JSON.stringify) || function(string) { + json_escapable.lastIndex = 0; + if (json_escapable.test(string)) { + string = string.replace(json_escapable, function(a) { + return json_lookup[a]; + }); + } + return '"' + string + '"'; +}; + +// This may be quite slow, so let's delay until user actually uses bad +// characters. +var unroll_lookup = function(escapable) { + var i; + var unrolled = {} + var c = [] + for(i=0; i<65536; i++) { + c.push( String.fromCharCode(i) ); + } + escapable.lastIndex = 0; + c.join('').replace(escapable, function (a) { + unrolled[ a ] = '\\u' + ('0000' + a.charCodeAt(0).toString(16)).slice(-4); + return ''; + }); + escapable.lastIndex = 0; + return unrolled; +}; + +// Quote string, also taking care of unicode characters that browsers +// often break. Especially, take care of unicode surrogates: +// http://en.wikipedia.org/wiki/Mapping_of_Unicode_characters#Surrogates +utils.quote = function(string) { + var quoted = JSONQuote(string); + + // In most cases this should be very fast and good enough. + extra_escapable.lastIndex = 0; + if(!extra_escapable.test(quoted)) { + return quoted; + } + + if(!extra_lookup) extra_lookup = unroll_lookup(extra_escapable); + + return quoted.replace(extra_escapable, function(a) { + return extra_lookup[a]; + }); +} + +var _all_protocols = ['websocket', + 'xdr-streaming', + 'xhr-streaming', + 'iframe-eventsource', + 'iframe-htmlfile', + 'xdr-polling', + 'xhr-polling', + 'iframe-xhr-polling', + 'jsonp-polling']; + +utils.probeProtocols = function() { + var probed = {}; + for(var i=0; i<_all_protocols.length; i++) { + var protocol = _all_protocols[i]; + // User can have a typo in protocol name. + probed[protocol] = SockJS[protocol] && + SockJS[protocol].enabled(); + } + return probed; +}; + +utils.detectProtocols = function(probed, protocols_whitelist, info) { + var pe = {}, + protocols = []; + if (!protocols_whitelist) protocols_whitelist = _all_protocols; + for(var i=0; i 0) { + maybe_push(protos); + } + } + } + + // 1. Websocket + if (info.websocket !== false) { + maybe_push(['websocket']); + } + + // 2. Streaming + if (pe['xhr-streaming'] && !info.null_origin) { + protocols.push('xhr-streaming'); + } else { + if (pe['xdr-streaming'] && !info.cookie_needed && !info.null_origin) { + protocols.push('xdr-streaming'); + } else { + maybe_push(['iframe-eventsource', + 'iframe-htmlfile']); + } + } + + // 3. Polling + if (pe['xhr-polling'] && !info.null_origin) { + protocols.push('xhr-polling'); + } else { + if (pe['xdr-polling'] && !info.cookie_needed && !info.null_origin) { + protocols.push('xdr-polling'); + } else { + maybe_push(['iframe-xhr-polling', + 'jsonp-polling']); + } + } + return protocols; +} +// [*] End of lib/utils.js + + +// [*] Including lib/dom.js +/* + * ***** BEGIN LICENSE BLOCK ***** + * Copyright (c) 2011-2012 VMware, Inc. + * + * For the license see COPYING. + * ***** END LICENSE BLOCK ***** + */ + +// May be used by htmlfile jsonp and transports. +var MPrefix = '_sockjs_global'; +utils.createHook = function() { + var window_id = 'a' + utils.random_string(8); + if (!(MPrefix in _window)) { + var map = {}; + _window[MPrefix] = function(window_id) { + if (!(window_id in map)) { + map[window_id] = { + id: window_id, + del: function() {delete map[window_id];} + }; + } + return map[window_id]; + } + } + return _window[MPrefix](window_id); +}; + + + +utils.attachMessage = function(listener) { + utils.attachEvent('message', listener); +}; +utils.attachEvent = function(event, listener) { + if (typeof _window.addEventListener !== 'undefined') { + _window.addEventListener(event, listener, false); + } else { + // IE quirks. + // According to: http://stevesouders.com/misc/test-postmessage.php + // the message gets delivered only to 'document', not 'window'. + _document.attachEvent("on" + event, listener); + // I get 'window' for ie8. + _window.attachEvent("on" + event, listener); + } +}; + +utils.detachMessage = function(listener) { + utils.detachEvent('message', listener); +}; +utils.detachEvent = function(event, listener) { + if (typeof _window.addEventListener !== 'undefined') { + _window.removeEventListener(event, listener, false); + } else { + _document.detachEvent("on" + event, listener); + _window.detachEvent("on" + event, listener); + } +}; + + +var on_unload = {}; +// Things registered after beforeunload are to be called immediately. +var after_unload = false; + +var trigger_unload_callbacks = function() { + for(var ref in on_unload) { + on_unload[ref](); + delete on_unload[ref]; + }; +}; + +var unload_triggered = function() { + if(after_unload) return; + after_unload = true; + trigger_unload_callbacks(); +}; + +// 'unload' alone is not reliable in opera within an iframe, but we +// can't use `beforeunload` as IE fires it on javascript: links. +utils.attachEvent('unload', unload_triggered); + +utils.unload_add = function(listener) { + var ref = utils.random_string(8); + on_unload[ref] = listener; + if (after_unload) { + utils.delay(trigger_unload_callbacks); + } + return ref; +}; +utils.unload_del = function(ref) { + if (ref in on_unload) + delete on_unload[ref]; +}; + + +utils.createIframe = function (iframe_url, error_callback) { + var iframe = _document.createElement('iframe'); + var tref, unload_ref; + var unattach = function() { + clearTimeout(tref); + // Explorer had problems with that. + try {iframe.onload = null;} catch (x) {} + iframe.onerror = null; + }; + var cleanup = function() { + if (iframe) { + unattach(); + // This timeout makes chrome fire onbeforeunload event + // within iframe. Without the timeout it goes straight to + // onunload. + setTimeout(function() { + if(iframe) { + iframe.parentNode.removeChild(iframe); + } + iframe = null; + }, 0); + utils.unload_del(unload_ref); + } + }; + var onerror = function(r) { + if (iframe) { + cleanup(); + error_callback(r); + } + }; + var post = function(msg, origin) { + try { + // When the iframe is not loaded, IE raises an exception + // on 'contentWindow'. + if (iframe && iframe.contentWindow) { + iframe.contentWindow.postMessage(msg, origin); + } + } catch (x) {}; + }; + + iframe.src = iframe_url; + iframe.style.display = 'none'; + iframe.style.position = 'absolute'; + iframe.onerror = function(){onerror('onerror');}; + iframe.onload = function() { + // `onload` is triggered before scripts on the iframe are + // executed. Give it few seconds to actually load stuff. + clearTimeout(tref); + tref = setTimeout(function(){onerror('onload timeout');}, 2000); + }; + _document.body.appendChild(iframe); + tref = setTimeout(function(){onerror('timeout');}, 15000); + unload_ref = utils.unload_add(cleanup); + return { + post: post, + cleanup: cleanup, + loaded: unattach + }; +}; + +utils.createHtmlfile = function (iframe_url, error_callback) { + var doc = new ActiveXObject('htmlfile'); + var tref, unload_ref; + var iframe; + var unattach = function() { + clearTimeout(tref); + }; + var cleanup = function() { + if (doc) { + unattach(); + utils.unload_del(unload_ref); + iframe.parentNode.removeChild(iframe); + iframe = doc = null; + CollectGarbage(); + } + }; + var onerror = function(r) { + if (doc) { + cleanup(); + error_callback(r); + } + }; + var post = function(msg, origin) { + try { + // When the iframe is not loaded, IE raises an exception + // on 'contentWindow'. + if (iframe && iframe.contentWindow) { + iframe.contentWindow.postMessage(msg, origin); + } + } catch (x) {}; + }; + + doc.open(); + doc.write('' + + 'document.domain="' + document.domain + '";' + + ''); + doc.close(); + doc.parentWindow[WPrefix] = _window[WPrefix]; + var c = doc.createElement('div'); + doc.body.appendChild(c); + iframe = doc.createElement('iframe'); + c.appendChild(iframe); + iframe.src = iframe_url; + tref = setTimeout(function(){onerror('timeout');}, 15000); + unload_ref = utils.unload_add(cleanup); + return { + post: post, + cleanup: cleanup, + loaded: unattach + }; +}; +// [*] End of lib/dom.js + + +// [*] Including lib/dom2.js +/* + * ***** BEGIN LICENSE BLOCK ***** + * Copyright (c) 2011-2012 VMware, Inc. + * + * For the license see COPYING. + * ***** END LICENSE BLOCK ***** + */ + +var AbstractXHRObject = function(){}; +AbstractXHRObject.prototype = new EventEmitter(['chunk', 'finish']); + +AbstractXHRObject.prototype._start = function(method, url, payload, opts) { + var that = this; + + try { + that.xhr = new XMLHttpRequest(); + } catch(x) {}; + + if (!that.xhr) { + try { + that.xhr = new _window.ActiveXObject('Microsoft.XMLHTTP'); + } catch(x) {}; + } + if (_window.ActiveXObject || _window.XDomainRequest) { + // IE8 caches even POSTs + url += ((url.indexOf('?') === -1) ? '?' : '&') + 't='+(+new Date); + } + + // Explorer tends to keep connection open, even after the + // tab gets closed: http://bugs.jquery.com/ticket/5280 + that.unload_ref = utils.unload_add(function(){that._cleanup(true);}); + try { + that.xhr.open(method, url, true); + } catch(e) { + // IE raises an exception on wrong port. + that.emit('finish', 0, ''); + that._cleanup(); + return; + }; + + if (!opts || !opts.no_credentials) { + // Mozilla docs says https://developer.mozilla.org/en/XMLHttpRequest : + // "This never affects same-site requests." + that.xhr.withCredentials = 'true'; + } + if (opts && opts.headers) { + for(var key in opts.headers) { + that.xhr.setRequestHeader(key, opts.headers[key]); + } + } + + that.xhr.onreadystatechange = function() { + if (that.xhr) { + var x = that.xhr; + switch (x.readyState) { + case 3: + // IE doesn't like peeking into responseText or status + // on Microsoft.XMLHTTP and readystate=3 + try { + var status = x.status; + var text = x.responseText; + } catch (x) {}; + // IE returns 1223 for 204: http://bugs.jquery.com/ticket/1450 + if (status === 1223) status = 204; + + // IE does return readystate == 3 for 404 answers. + if (text && text.length > 0) { + that.emit('chunk', status, text); + } + break; + case 4: + var status = x.status; + // IE returns 1223 for 204: http://bugs.jquery.com/ticket/1450 + if (status === 1223) status = 204; + + that.emit('finish', status, x.responseText); + that._cleanup(false); + break; + } + } + }; + that.xhr.send(payload); +}; + +AbstractXHRObject.prototype._cleanup = function(abort) { + var that = this; + if (!that.xhr) return; + utils.unload_del(that.unload_ref); + + // IE needs this field to be a function + that.xhr.onreadystatechange = function(){}; + + if (abort) { + try { + that.xhr.abort(); + } catch(x) {}; + } + that.unload_ref = that.xhr = null; +}; + +AbstractXHRObject.prototype.close = function() { + var that = this; + that.nuke(); + that._cleanup(true); +}; + +var XHRCorsObject = utils.XHRCorsObject = function() { + var that = this, args = arguments; + utils.delay(function(){that._start.apply(that, args);}); +}; +XHRCorsObject.prototype = new AbstractXHRObject(); + +var XHRLocalObject = utils.XHRLocalObject = function(method, url, payload) { + var that = this; + utils.delay(function(){ + that._start(method, url, payload, { + no_credentials: true + }); + }); +}; +XHRLocalObject.prototype = new AbstractXHRObject(); + + + +// References: +// http://ajaxian.com/archives/100-line-ajax-wrapper +// http://msdn.microsoft.com/en-us/library/cc288060(v=VS.85).aspx +var XDRObject = utils.XDRObject = function(method, url, payload) { + var that = this; + utils.delay(function(){that._start(method, url, payload);}); +}; +XDRObject.prototype = new EventEmitter(['chunk', 'finish']); +XDRObject.prototype._start = function(method, url, payload) { + var that = this; + var xdr = new XDomainRequest(); + // IE caches even POSTs + url += ((url.indexOf('?') === -1) ? '?' : '&') + 't='+(+new Date); + + var onerror = xdr.ontimeout = xdr.onerror = function() { + that.emit('finish', 0, ''); + that._cleanup(false); + }; + xdr.onprogress = function() { + that.emit('chunk', 200, xdr.responseText); + }; + xdr.onload = function() { + that.emit('finish', 200, xdr.responseText); + that._cleanup(false); + }; + that.xdr = xdr; + that.unload_ref = utils.unload_add(function(){that._cleanup(true);}); + try { + // Fails with AccessDenied if port number is bogus + that.xdr.open(method, url); + that.xdr.send(payload); + } catch(x) { + onerror(); + } +}; + +XDRObject.prototype._cleanup = function(abort) { + var that = this; + if (!that.xdr) return; + utils.unload_del(that.unload_ref); + + that.xdr.ontimeout = that.xdr.onerror = that.xdr.onprogress = + that.xdr.onload = null; + if (abort) { + try { + that.xdr.abort(); + } catch(x) {}; + } + that.unload_ref = that.xdr = null; +}; + +XDRObject.prototype.close = function() { + var that = this; + that.nuke(); + that._cleanup(true); +}; + +// 1. Is natively via XHR +// 2. Is natively via XDR +// 3. Nope, but postMessage is there so it should work via the Iframe. +// 4. Nope, sorry. +utils.isXHRCorsCapable = function() { + if (_window.XMLHttpRequest && 'withCredentials' in new XMLHttpRequest()) { + return 1; + } + // XDomainRequest doesn't work if page is served from file:// + if (_window.XDomainRequest && _document.domain) { + return 2; + } + if (IframeTransport.enabled()) { + return 3; + } + return 4; +}; +// [*] End of lib/dom2.js + + +// [*] Including lib/sockjs.js +/* + * ***** BEGIN LICENSE BLOCK ***** + * Copyright (c) 2011-2012 VMware, Inc. + * + * For the license see COPYING. + * ***** END LICENSE BLOCK ***** + */ + +var SockJS = function(url, dep_protocols_whitelist, options) { + if (this === _window) { + // makes `new` optional + return new SockJS(url, dep_protocols_whitelist, options); + } + + var that = this, protocols_whitelist; + that._options = {devel: false, debug: false, protocols_whitelist: [], + info: undefined, rtt: undefined}; + if (options) { + utils.objectExtend(that._options, options); + } + that._base_url = utils.amendUrl(url); + that._server = that._options.server || utils.random_number_string(1000); + if (that._options.protocols_whitelist && + that._options.protocols_whitelist.length) { + protocols_whitelist = that._options.protocols_whitelist; + } else { + // Deprecated API + if (typeof dep_protocols_whitelist === 'string' && + dep_protocols_whitelist.length > 0) { + protocols_whitelist = [dep_protocols_whitelist]; + } else if (utils.isArray(dep_protocols_whitelist)) { + protocols_whitelist = dep_protocols_whitelist + } else { + protocols_whitelist = null; + } + if (protocols_whitelist) { + that._debug('Deprecated API: Use "protocols_whitelist" option ' + + 'instead of supplying protocol list as a second ' + + 'parameter to SockJS constructor.'); + } + } + that._protocols = []; + that.protocol = null; + that.readyState = SockJS.CONNECTING; + that._ir = createInfoReceiver(that._base_url); + that._ir.onfinish = function(info, rtt) { + that._ir = null; + if (info) { + if (that._options.info) { + // Override if user supplies the option + info = utils.objectExtend(info, that._options.info); + } + if (that._options.rtt) { + rtt = that._options.rtt; + } + that._applyInfo(info, rtt, protocols_whitelist); + that._didClose(); + } else { + that._didClose(1002, 'Can\'t connect to server', true); + } + }; +}; +// Inheritance +SockJS.prototype = new REventTarget(); + +SockJS.version = "0.3.4"; + +SockJS.CONNECTING = 0; +SockJS.OPEN = 1; +SockJS.CLOSING = 2; +SockJS.CLOSED = 3; + +SockJS.prototype._debug = function() { + if (this._options.debug) + utils.log.apply(utils, arguments); +}; + +SockJS.prototype._dispatchOpen = function() { + var that = this; + if (that.readyState === SockJS.CONNECTING) { + if (that._transport_tref) { + clearTimeout(that._transport_tref); + that._transport_tref = null; + } + that.readyState = SockJS.OPEN; + that.dispatchEvent(new SimpleEvent("open")); + } else { + // The server might have been restarted, and lost track of our + // connection. + that._didClose(1006, "Server lost session"); + } +}; + +SockJS.prototype._dispatchMessage = function(data) { + var that = this; + if (that.readyState !== SockJS.OPEN) + return; + that.dispatchEvent(new SimpleEvent("message", {data: data})); +}; + +SockJS.prototype._dispatchHeartbeat = function(data) { + var that = this; + if (that.readyState !== SockJS.OPEN) + return; + that.dispatchEvent(new SimpleEvent('heartbeat', {})); +}; + +SockJS.prototype._didClose = function(code, reason, force) { + var that = this; + if (that.readyState !== SockJS.CONNECTING && + that.readyState !== SockJS.OPEN && + that.readyState !== SockJS.CLOSING) + throw new Error('INVALID_STATE_ERR'); + if (that._ir) { + that._ir.nuke(); + that._ir = null; + } + + if (that._transport) { + that._transport.doCleanup(); + that._transport = null; + } + + var close_event = new SimpleEvent("close", { + code: code, + reason: reason, + wasClean: utils.userSetCode(code)}); + + if (!utils.userSetCode(code) && + that.readyState === SockJS.CONNECTING && !force) { + if (that._try_next_protocol(close_event)) { + return; + } + close_event = new SimpleEvent("close", {code: 2000, + reason: "All transports failed", + wasClean: false, + last_event: close_event}); + } + that.readyState = SockJS.CLOSED; + + utils.delay(function() { + that.dispatchEvent(close_event); + }); +}; + +SockJS.prototype._didMessage = function(data) { + var that = this; + var type = data.slice(0, 1); + switch(type) { + case 'o': + that._dispatchOpen(); + break; + case 'a': + var payload = JSON.parse(data.slice(1) || '[]'); + for(var i=0; i < payload.length; i++){ + that._dispatchMessage(payload[i]); + } + break; + case 'm': + var payload = JSON.parse(data.slice(1) || 'null'); + that._dispatchMessage(payload); + break; + case 'c': + var payload = JSON.parse(data.slice(1) || '[]'); + that._didClose(payload[0], payload[1]); + break; + case 'h': + that._dispatchHeartbeat(); + break; + } +}; + +SockJS.prototype._try_next_protocol = function(close_event) { + var that = this; + if (that.protocol) { + that._debug('Closed transport:', that.protocol, ''+close_event); + that.protocol = null; + } + if (that._transport_tref) { + clearTimeout(that._transport_tref); + that._transport_tref = null; + } + + while(1) { + var protocol = that.protocol = that._protocols.shift(); + if (!protocol) { + return false; + } + // Some protocols require access to `body`, what if were in + // the `head`? + if (SockJS[protocol] && + SockJS[protocol].need_body === true && + (!_document.body || + (typeof _document.readyState !== 'undefined' + && _document.readyState !== 'complete'))) { + that._protocols.unshift(protocol); + that.protocol = 'waiting-for-load'; + utils.attachEvent('load', function(){ + that._try_next_protocol(); + }); + return true; + } + + if (!SockJS[protocol] || + !SockJS[protocol].enabled(that._options)) { + that._debug('Skipping transport:', protocol); + } else { + var roundTrips = SockJS[protocol].roundTrips || 1; + var to = ((that._options.rto || 0) * roundTrips) || 5000; + that._transport_tref = utils.delay(to, function() { + if (that.readyState === SockJS.CONNECTING) { + // I can't understand how it is possible to run + // this timer, when the state is CLOSED, but + // apparently in IE everythin is possible. + that._didClose(2007, "Transport timeouted"); + } + }); + + var connid = utils.random_string(8); + var trans_url = that._base_url + '/' + that._server + '/' + connid; + that._debug('Opening transport:', protocol, ' url:'+trans_url, + ' RTO:'+that._options.rto); + that._transport = new SockJS[protocol](that, trans_url, + that._base_url); + return true; + } + } +}; + +SockJS.prototype.close = function(code, reason) { + var that = this; + if (code && !utils.userSetCode(code)) + throw new Error("INVALID_ACCESS_ERR"); + if(that.readyState !== SockJS.CONNECTING && + that.readyState !== SockJS.OPEN) { + return false; + } + that.readyState = SockJS.CLOSING; + that._didClose(code || 1000, reason || "Normal closure"); + return true; +}; + +SockJS.prototype.send = function(data) { + var that = this; + if (that.readyState === SockJS.CONNECTING) + throw new Error('INVALID_STATE_ERR'); + if (that.readyState === SockJS.OPEN) { + that._transport.doSend(utils.quote('' + data)); + } + return true; +}; + +SockJS.prototype._applyInfo = function(info, rtt, protocols_whitelist) { + var that = this; + that._options.info = info; + that._options.rtt = rtt; + that._options.rto = utils.countRTO(rtt); + that._options.info.null_origin = !_document.domain; + var probed = utils.probeProtocols(); + that._protocols = utils.detectProtocols(probed, protocols_whitelist, info); +}; +// [*] End of lib/sockjs.js + + +// [*] Including lib/trans-websocket.js +/* + * ***** BEGIN LICENSE BLOCK ***** + * Copyright (c) 2011-2012 VMware, Inc. + * + * For the license see COPYING. + * ***** END LICENSE BLOCK ***** + */ + +var WebSocketTransport = SockJS.websocket = function(ri, trans_url) { + var that = this; + var url = trans_url + '/websocket'; + if (url.slice(0, 5) === 'https') { + url = 'wss' + url.slice(5); + } else { + url = 'ws' + url.slice(4); + } + that.ri = ri; + that.url = url; + var Constructor = _window.WebSocket || _window.MozWebSocket; + + that.ws = new Constructor(that.url); + that.ws.onmessage = function(e) { + that.ri._didMessage(e.data); + }; + // Firefox has an interesting bug. If a websocket connection is + // created after onunload, it stays alive even when user + // navigates away from the page. In such situation let's lie - + // let's not open the ws connection at all. See: + // https://github.com/sockjs/sockjs-client/issues/28 + // https://bugzilla.mozilla.org/show_bug.cgi?id=696085 + that.unload_ref = utils.unload_add(function(){that.ws.close()}); + that.ws.onclose = function() { + that.ri._didMessage(utils.closeFrame(1006, "WebSocket connection broken")); + }; +}; + +WebSocketTransport.prototype.doSend = function(data) { + this.ws.send('[' + data + ']'); +}; + +WebSocketTransport.prototype.doCleanup = function() { + var that = this; + var ws = that.ws; + if (ws) { + ws.onmessage = ws.onclose = null; + ws.close(); + utils.unload_del(that.unload_ref); + that.unload_ref = that.ri = that.ws = null; + } +}; + +WebSocketTransport.enabled = function() { + return !!(_window.WebSocket || _window.MozWebSocket); +}; + +// In theory, ws should require 1 round trip. But in chrome, this is +// not very stable over SSL. Most likely a ws connection requires a +// separate SSL connection, in which case 2 round trips are an +// absolute minumum. +WebSocketTransport.roundTrips = 2; +// [*] End of lib/trans-websocket.js + + +// [*] Including lib/trans-sender.js +/* + * ***** BEGIN LICENSE BLOCK ***** + * Copyright (c) 2011-2012 VMware, Inc. + * + * For the license see COPYING. + * ***** END LICENSE BLOCK ***** + */ + +var BufferedSender = function() {}; +BufferedSender.prototype.send_constructor = function(sender) { + var that = this; + that.send_buffer = []; + that.sender = sender; +}; +BufferedSender.prototype.doSend = function(message) { + var that = this; + that.send_buffer.push(message); + if (!that.send_stop) { + that.send_schedule(); + } +}; + +// For polling transports in a situation when in the message callback, +// new message is being send. If the sending connection was started +// before receiving one, it is possible to saturate the network and +// timeout due to the lack of receiving socket. To avoid that we delay +// sending messages by some small time, in order to let receiving +// connection be started beforehand. This is only a halfmeasure and +// does not fix the big problem, but it does make the tests go more +// stable on slow networks. +BufferedSender.prototype.send_schedule_wait = function() { + var that = this; + var tref; + that.send_stop = function() { + that.send_stop = null; + clearTimeout(tref); + }; + tref = utils.delay(25, function() { + that.send_stop = null; + that.send_schedule(); + }); +}; + +BufferedSender.prototype.send_schedule = function() { + var that = this; + if (that.send_buffer.length > 0) { + var payload = '[' + that.send_buffer.join(',') + ']'; + that.send_stop = that.sender(that.trans_url, payload, function(success, abort_reason) { + that.send_stop = null; + if (success === false) { + that.ri._didClose(1006, 'Sending error ' + abort_reason); + } else { + that.send_schedule_wait(); + } + }); + that.send_buffer = []; + } +}; + +BufferedSender.prototype.send_destructor = function() { + var that = this; + if (that._send_stop) { + that._send_stop(); + } + that._send_stop = null; +}; + +var jsonPGenericSender = function(url, payload, callback) { + var that = this; + + if (!('_send_form' in that)) { + var form = that._send_form = _document.createElement('form'); + var area = that._send_area = _document.createElement('textarea'); + area.name = 'd'; + form.style.display = 'none'; + form.style.position = 'absolute'; + form.method = 'POST'; + form.enctype = 'application/x-www-form-urlencoded'; + form.acceptCharset = "UTF-8"; + form.appendChild(area); + _document.body.appendChild(form); + } + var form = that._send_form; + var area = that._send_area; + var id = 'a' + utils.random_string(8); + form.target = id; + form.action = url + '/jsonp_send?i=' + id; + + var iframe; + try { + // ie6 dynamic iframes with target="" support (thanks Chris Lambacher) + iframe = _document.createElement('