This commit is contained in:
Dale Mellor 2020-03-23 15:33:18 +00:00
commit a739007e23
25 changed files with 3881 additions and 0 deletions

33
.gitignore vendored Normal file
View file

@ -0,0 +1,33 @@
*.o
*.lo
.deps
.libs
stamp-h1
*.la
*~
autom4te.cache
aclocal.m4
config.guess
config.log
config.status
config.sub
configure
auto-config.h
auto-config.h.in
depcomp
dmbcs-micro-server.html
dmbcs-micro-server.pc
install-sh
libtool
ltmain.sh
m4
makefile
makefile.in
missing
micro-server.pc
micro-server.info
texinfo.tex
mdate-sh
stamp-vti
version.texi
compile

0
AUTHORS Normal file
View file

674
COPYING Normal file
View file

@ -0,0 +1,674 @@
GNU GENERAL PUBLIC LICENSE
Version 3, 29 June 2007
Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
Everyone is permitted to copy and distribute verbatim copies
of this license document, but changing it is not allowed.
Preamble
The GNU General Public License is a free, copyleft license for
software and other kinds of works.
The licenses for most software and other practical works are designed
to take away your freedom to share and change the works. By contrast,
the GNU General Public License is intended to guarantee your freedom to
share and change all versions of a program--to make sure it remains free
software for all its users. We, the Free Software Foundation, use the
GNU General Public License for most of our software; it applies also to
any other work released this way by its authors. You can apply it to
your programs, too.
When we speak of free software, we are referring to freedom, not
price. Our General Public Licenses are designed to make sure that you
have the freedom to distribute copies of free software (and charge for
them if you wish), that you receive source code or can get it if you
want it, that you can change the software or use pieces of it in new
free programs, and that you know you can do these things.
To protect your rights, we need to prevent others from denying you
these rights or asking you to surrender the rights. Therefore, you have
certain responsibilities if you distribute copies of the software, or if
you modify it: responsibilities to respect the freedom of others.
For example, if you distribute copies of such a program, whether
gratis or for a fee, you must pass on to the recipients the same
freedoms that you received. You must make sure that they, too, receive
or can get the source code. And you must show them these terms so they
know their rights.
Developers that use the GNU GPL protect your rights with two steps:
(1) assert copyright on the software, and (2) offer you this License
giving you legal permission to copy, distribute and/or modify it.
For the developers' and authors' protection, the GPL clearly explains
that there is no warranty for this free software. For both users' and
authors' sake, the GPL requires that modified versions be marked as
changed, so that their problems will not be attributed erroneously to
authors of previous versions.
Some devices are designed to deny users access to install or run
modified versions of the software inside them, although the manufacturer
can do so. This is fundamentally incompatible with the aim of
protecting users' freedom to change the software. The systematic
pattern of such abuse occurs in the area of products for individuals to
use, which is precisely where it is most unacceptable. Therefore, we
have designed this version of the GPL to prohibit the practice for those
products. If such problems arise substantially in other domains, we
stand ready to extend this provision to those domains in future versions
of the GPL, as needed to protect the freedom of users.
Finally, every program is threatened constantly by software patents.
States should not allow patents to restrict development and use of
software on general-purpose computers, but in those that do, we wish to
avoid the special danger that patents applied to a free program could
make it effectively proprietary. To prevent this, the GPL assures that
patents cannot be used to render the program non-free.
The precise terms and conditions for copying, distribution and
modification follow.
TERMS AND CONDITIONS
0. Definitions.
"This License" refers to version 3 of the GNU General Public License.
"Copyright" also means copyright-like laws that apply to other kinds of
works, such as semiconductor masks.
"The Program" refers to any copyrightable work licensed under this
License. Each licensee is addressed as "you". "Licensees" and
"recipients" may be individuals or organizations.
To "modify" a work means to copy from or adapt all or part of the work
in a fashion requiring copyright permission, other than the making of an
exact copy. The resulting work is called a "modified version" of the
earlier work or a work "based on" the earlier work.
A "covered work" means either the unmodified Program or a work based
on the Program.
To "propagate" a work means to do anything with it that, without
permission, would make you directly or secondarily liable for
infringement under applicable copyright law, except executing it on a
computer or modifying a private copy. Propagation includes copying,
distribution (with or without modification), making available to the
public, and in some countries other activities as well.
To "convey" a work means any kind of propagation that enables other
parties to make or receive copies. Mere interaction with a user through
a computer network, with no transfer of a copy, is not conveying.
An interactive user interface displays "Appropriate Legal Notices"
to the extent that it includes a convenient and prominently visible
feature that (1) displays an appropriate copyright notice, and (2)
tells the user that there is no warranty for the work (except to the
extent that warranties are provided), that licensees may convey the
work under this License, and how to view a copy of this License. If
the interface presents a list of user commands or options, such as a
menu, a prominent item in the list meets this criterion.
1. Source Code.
The "source code" for a work means the preferred form of the work
for making modifications to it. "Object code" means any non-source
form of a work.
A "Standard Interface" means an interface that either is an official
standard defined by a recognized standards body, or, in the case of
interfaces specified for a particular programming language, one that
is widely used among developers working in that language.
The "System Libraries" of an executable work include anything, other
than the work as a whole, that (a) is included in the normal form of
packaging a Major Component, but which is not part of that Major
Component, and (b) serves only to enable use of the work with that
Major Component, or to implement a Standard Interface for which an
implementation is available to the public in source code form. A
"Major Component", in this context, means a major essential component
(kernel, window system, and so on) of the specific operating system
(if any) on which the executable work runs, or a compiler used to
produce the work, or an object code interpreter used to run it.
The "Corresponding Source" for a work in object code form means all
the source code needed to generate, install, and (for an executable
work) run the object code and to modify the work, including scripts to
control those activities. However, it does not include the work's
System Libraries, or general-purpose tools or generally available free
programs which are used unmodified in performing those activities but
which are not part of the work. For example, Corresponding Source
includes interface definition files associated with source files for
the work, and the source code for shared libraries and dynamically
linked subprograms that the work is specifically designed to require,
such as by intimate data communication or control flow between those
subprograms and other parts of the work.
The Corresponding Source need not include anything that users
can regenerate automatically from other parts of the Corresponding
Source.
The Corresponding Source for a work in source code form is that
same work.
2. Basic Permissions.
All rights granted under this License are granted for the term of
copyright on the Program, and are irrevocable provided the stated
conditions are met. This License explicitly affirms your unlimited
permission to run the unmodified Program. The output from running a
covered work is covered by this License only if the output, given its
content, constitutes a covered work. This License acknowledges your
rights of fair use or other equivalent, as provided by copyright law.
You may make, run and propagate covered works that you do not
convey, without conditions so long as your license otherwise remains
in force. You may convey covered works to others for the sole purpose
of having them make modifications exclusively for you, or provide you
with facilities for running those works, provided that you comply with
the terms of this License in conveying all material for which you do
not control copyright. Those thus making or running the covered works
for you must do so exclusively on your behalf, under your direction
and control, on terms that prohibit them from making any copies of
your copyrighted material outside their relationship with you.
Conveying under any other circumstances is permitted solely under
the conditions stated below. Sublicensing is not allowed; section 10
makes it unnecessary.
3. Protecting Users' Legal Rights From Anti-Circumvention Law.
No covered work shall be deemed part of an effective technological
measure under any applicable law fulfilling obligations under article
11 of the WIPO copyright treaty adopted on 20 December 1996, or
similar laws prohibiting or restricting circumvention of such
measures.
When you convey a covered work, you waive any legal power to forbid
circumvention of technological measures to the extent such circumvention
is effected by exercising rights under this License with respect to
the covered work, and you disclaim any intention to limit operation or
modification of the work as a means of enforcing, against the work's
users, your or third parties' legal rights to forbid circumvention of
technological measures.
4. Conveying Verbatim Copies.
You may convey verbatim copies of the Program's source code as you
receive it, in any medium, provided that you conspicuously and
appropriately publish on each copy an appropriate copyright notice;
keep intact all notices stating that this License and any
non-permissive terms added in accord with section 7 apply to the code;
keep intact all notices of the absence of any warranty; and give all
recipients a copy of this License along with the Program.
You may charge any price or no price for each copy that you convey,
and you may offer support or warranty protection for a fee.
5. Conveying Modified Source Versions.
You may convey a work based on the Program, or the modifications to
produce it from the Program, in the form of source code under the
terms of section 4, provided that you also meet all of these conditions:
a) The work must carry prominent notices stating that you modified
it, and giving a relevant date.
b) The work must carry prominent notices stating that it is
released under this License and any conditions added under section
7. This requirement modifies the requirement in section 4 to
"keep intact all notices".
c) You must license the entire work, as a whole, under this
License to anyone who comes into possession of a copy. This
License will therefore apply, along with any applicable section 7
additional terms, to the whole of the work, and all its parts,
regardless of how they are packaged. This License gives no
permission to license the work in any other way, but it does not
invalidate such permission if you have separately received it.
d) If the work has interactive user interfaces, each must display
Appropriate Legal Notices; however, if the Program has interactive
interfaces that do not display Appropriate Legal Notices, your
work need not make them do so.
A compilation of a covered work with other separate and independent
works, which are not by their nature extensions of the covered work,
and which are not combined with it such as to form a larger program,
in or on a volume of a storage or distribution medium, is called an
"aggregate" if the compilation and its resulting copyright are not
used to limit the access or legal rights of the compilation's users
beyond what the individual works permit. Inclusion of a covered work
in an aggregate does not cause this License to apply to the other
parts of the aggregate.
6. Conveying Non-Source Forms.
You may convey a covered work in object code form under the terms
of sections 4 and 5, provided that you also convey the
machine-readable Corresponding Source under the terms of this License,
in one of these ways:
a) Convey the object code in, or embodied in, a physical product
(including a physical distribution medium), accompanied by the
Corresponding Source fixed on a durable physical medium
customarily used for software interchange.
b) Convey the object code in, or embodied in, a physical product
(including a physical distribution medium), accompanied by a
written offer, valid for at least three years and valid for as
long as you offer spare parts or customer support for that product
model, to give anyone who possesses the object code either (1) a
copy of the Corresponding Source for all the software in the
product that is covered by this License, on a durable physical
medium customarily used for software interchange, for a price no
more than your reasonable cost of physically performing this
conveying of source, or (2) access to copy the
Corresponding Source from a network server at no charge.
c) Convey individual copies of the object code with a copy of the
written offer to provide the Corresponding Source. This
alternative is allowed only occasionally and noncommercially, and
only if you received the object code with such an offer, in accord
with subsection 6b.
d) Convey the object code by offering access from a designated
place (gratis or for a charge), and offer equivalent access to the
Corresponding Source in the same way through the same place at no
further charge. You need not require recipients to copy the
Corresponding Source along with the object code. If the place to
copy the object code is a network server, the Corresponding Source
may be on a different server (operated by you or a third party)
that supports equivalent copying facilities, provided you maintain
clear directions next to the object code saying where to find the
Corresponding Source. Regardless of what server hosts the
Corresponding Source, you remain obligated to ensure that it is
available for as long as needed to satisfy these requirements.
e) Convey the object code using peer-to-peer transmission, provided
you inform other peers where the object code and Corresponding
Source of the work are being offered to the general public at no
charge under subsection 6d.
A separable portion of the object code, whose source code is excluded
from the Corresponding Source as a System Library, need not be
included in conveying the object code work.
A "User Product" is either (1) a "consumer product", which means any
tangible personal property which is normally used for personal, family,
or household purposes, or (2) anything designed or sold for incorporation
into a dwelling. In determining whether a product is a consumer product,
doubtful cases shall be resolved in favor of coverage. For a particular
product received by a particular user, "normally used" refers to a
typical or common use of that class of product, regardless of the status
of the particular user or of the way in which the particular user
actually uses, or expects or is expected to use, the product. A product
is a consumer product regardless of whether the product has substantial
commercial, industrial or non-consumer uses, unless such uses represent
the only significant mode of use of the product.
"Installation Information" for a User Product means any methods,
procedures, authorization keys, or other information required to install
and execute modified versions of a covered work in that User Product from
a modified version of its Corresponding Source. The information must
suffice to ensure that the continued functioning of the modified object
code is in no case prevented or interfered with solely because
modification has been made.
If you convey an object code work under this section in, or with, or
specifically for use in, a User Product, and the conveying occurs as
part of a transaction in which the right of possession and use of the
User Product is transferred to the recipient in perpetuity or for a
fixed term (regardless of how the transaction is characterized), the
Corresponding Source conveyed under this section must be accompanied
by the Installation Information. But this requirement does not apply
if neither you nor any third party retains the ability to install
modified object code on the User Product (for example, the work has
been installed in ROM).
The requirement to provide Installation Information does not include a
requirement to continue to provide support service, warranty, or updates
for a work that has been modified or installed by the recipient, or for
the User Product in which it has been modified or installed. Access to a
network may be denied when the modification itself materially and
adversely affects the operation of the network or violates the rules and
protocols for communication across the network.
Corresponding Source conveyed, and Installation Information provided,
in accord with this section must be in a format that is publicly
documented (and with an implementation available to the public in
source code form), and must require no special password or key for
unpacking, reading or copying.
7. Additional Terms.
"Additional permissions" are terms that supplement the terms of this
License by making exceptions from one or more of its conditions.
Additional permissions that are applicable to the entire Program shall
be treated as though they were included in this License, to the extent
that they are valid under applicable law. If additional permissions
apply only to part of the Program, that part may be used separately
under those permissions, but the entire Program remains governed by
this License without regard to the additional permissions.
When you convey a copy of a covered work, you may at your option
remove any additional permissions from that copy, or from any part of
it. (Additional permissions may be written to require their own
removal in certain cases when you modify the work.) You may place
additional permissions on material, added by you to a covered work,
for which you have or can give appropriate copyright permission.
Notwithstanding any other provision of this License, for material you
add to a covered work, you may (if authorized by the copyright holders of
that material) supplement the terms of this License with terms:
a) Disclaiming warranty or limiting liability differently from the
terms of sections 15 and 16 of this License; or
b) Requiring preservation of specified reasonable legal notices or
author attributions in that material or in the Appropriate Legal
Notices displayed by works containing it; or
c) Prohibiting misrepresentation of the origin of that material, or
requiring that modified versions of such material be marked in
reasonable ways as different from the original version; or
d) Limiting the use for publicity purposes of names of licensors or
authors of the material; or
e) Declining to grant rights under trademark law for use of some
trade names, trademarks, or service marks; or
f) Requiring indemnification of licensors and authors of that
material by anyone who conveys the material (or modified versions of
it) with contractual assumptions of liability to the recipient, for
any liability that these contractual assumptions directly impose on
those licensors and authors.
All other non-permissive additional terms are considered "further
restrictions" within the meaning of section 10. If the Program as you
received it, or any part of it, contains a notice stating that it is
governed by this License along with a term that is a further
restriction, you may remove that term. If a license document contains
a further restriction but permits relicensing or conveying under this
License, you may add to a covered work material governed by the terms
of that license document, provided that the further restriction does
not survive such relicensing or conveying.
If you add terms to a covered work in accord with this section, you
must place, in the relevant source files, a statement of the
additional terms that apply to those files, or a notice indicating
where to find the applicable terms.
Additional terms, permissive or non-permissive, may be stated in the
form of a separately written license, or stated as exceptions;
the above requirements apply either way.
8. Termination.
You may not propagate or modify a covered work except as expressly
provided under this License. Any attempt otherwise to propagate or
modify it is void, and will automatically terminate your rights under
this License (including any patent licenses granted under the third
paragraph of section 11).
However, if you cease all violation of this License, then your
license from a particular copyright holder is reinstated (a)
provisionally, unless and until the copyright holder explicitly and
finally terminates your license, and (b) permanently, if the copyright
holder fails to notify you of the violation by some reasonable means
prior to 60 days after the cessation.
Moreover, your license from a particular copyright holder is
reinstated permanently if the copyright holder notifies you of the
violation by some reasonable means, this is the first time you have
received notice of violation of this License (for any work) from that
copyright holder, and you cure the violation prior to 30 days after
your receipt of the notice.
Termination of your rights under this section does not terminate the
licenses of parties who have received copies or rights from you under
this License. If your rights have been terminated and not permanently
reinstated, you do not qualify to receive new licenses for the same
material under section 10.
9. Acceptance Not Required for Having Copies.
You are not required to accept this License in order to receive or
run a copy of the Program. Ancillary propagation of a covered work
occurring solely as a consequence of using peer-to-peer transmission
to receive a copy likewise does not require acceptance. However,
nothing other than this License grants you permission to propagate or
modify any covered work. These actions infringe copyright if you do
not accept this License. Therefore, by modifying or propagating a
covered work, you indicate your acceptance of this License to do so.
10. Automatic Licensing of Downstream Recipients.
Each time you convey a covered work, the recipient automatically
receives a license from the original licensors, to run, modify and
propagate that work, subject to this License. You are not responsible
for enforcing compliance by third parties with this License.
An "entity transaction" is a transaction transferring control of an
organization, or substantially all assets of one, or subdividing an
organization, or merging organizations. If propagation of a covered
work results from an entity transaction, each party to that
transaction who receives a copy of the work also receives whatever
licenses to the work the party's predecessor in interest had or could
give under the previous paragraph, plus a right to possession of the
Corresponding Source of the work from the predecessor in interest, if
the predecessor has it or can get it with reasonable efforts.
You may not impose any further restrictions on the exercise of the
rights granted or affirmed under this License. For example, you may
not impose a license fee, royalty, or other charge for exercise of
rights granted under this License, and you may not initiate litigation
(including a cross-claim or counterclaim in a lawsuit) alleging that
any patent claim is infringed by making, using, selling, offering for
sale, or importing the Program or any portion of it.
11. Patents.
A "contributor" is a copyright holder who authorizes use under this
License of the Program or a work on which the Program is based. The
work thus licensed is called the contributor's "contributor version".
A contributor's "essential patent claims" are all patent claims
owned or controlled by the contributor, whether already acquired or
hereafter acquired, that would be infringed by some manner, permitted
by this License, of making, using, or selling its contributor version,
but do not include claims that would be infringed only as a
consequence of further modification of the contributor version. For
purposes of this definition, "control" includes the right to grant
patent sublicenses in a manner consistent with the requirements of
this License.
Each contributor grants you a non-exclusive, worldwide, royalty-free
patent license under the contributor's essential patent claims, to
make, use, sell, offer for sale, import and otherwise run, modify and
propagate the contents of its contributor version.
In the following three paragraphs, a "patent license" is any express
agreement or commitment, however denominated, not to enforce a patent
(such as an express permission to practice a patent or covenant not to
sue for patent infringement). To "grant" such a patent license to a
party means to make such an agreement or commitment not to enforce a
patent against the party.
If you convey a covered work, knowingly relying on a patent license,
and the Corresponding Source of the work is not available for anyone
to copy, free of charge and under the terms of this License, through a
publicly available network server or other readily accessible means,
then you must either (1) cause the Corresponding Source to be so
available, or (2) arrange to deprive yourself of the benefit of the
patent license for this particular work, or (3) arrange, in a manner
consistent with the requirements of this License, to extend the patent
license to downstream recipients. "Knowingly relying" means you have
actual knowledge that, but for the patent license, your conveying the
covered work in a country, or your recipient's use of the covered work
in a country, would infringe one or more identifiable patents in that
country that you have reason to believe are valid.
If, pursuant to or in connection with a single transaction or
arrangement, you convey, or propagate by procuring conveyance of, a
covered work, and grant a patent license to some of the parties
receiving the covered work authorizing them to use, propagate, modify
or convey a specific copy of the covered work, then the patent license
you grant is automatically extended to all recipients of the covered
work and works based on it.
A patent license is "discriminatory" if it does not include within
the scope of its coverage, prohibits the exercise of, or is
conditioned on the non-exercise of one or more of the rights that are
specifically granted under this License. You may not convey a covered
work if you are a party to an arrangement with a third party that is
in the business of distributing software, under which you make payment
to the third party based on the extent of your activity of conveying
the work, and under which the third party grants, to any of the
parties who would receive the covered work from you, a discriminatory
patent license (a) in connection with copies of the covered work
conveyed by you (or copies made from those copies), or (b) primarily
for and in connection with specific products or compilations that
contain the covered work, unless you entered into that arrangement,
or that patent license was granted, prior to 28 March 2007.
Nothing in this License shall be construed as excluding or limiting
any implied license or other defenses to infringement that may
otherwise be available to you under applicable patent law.
12. No Surrender of Others' Freedom.
If conditions are imposed on you (whether by court order, agreement or
otherwise) that contradict the conditions of this License, they do not
excuse you from the conditions of this License. If you cannot convey a
covered work so as to satisfy simultaneously your obligations under this
License and any other pertinent obligations, then as a consequence you may
not convey it at all. For example, if you agree to terms that obligate you
to collect a royalty for further conveying from those to whom you convey
the Program, the only way you could satisfy both those terms and this
License would be to refrain entirely from conveying the Program.
13. Use with the GNU Affero General Public License.
Notwithstanding any other provision of this License, you have
permission to link or combine any covered work with a work licensed
under version 3 of the GNU Affero General Public License into a single
combined work, and to convey the resulting work. The terms of this
License will continue to apply to the part which is the covered work,
but the special requirements of the GNU Affero General Public License,
section 13, concerning interaction through a network will apply to the
combination as such.
14. Revised Versions of this License.
The Free Software Foundation may publish revised and/or new versions of
the GNU General Public License from time to time. Such new versions will
be similar in spirit to the present version, but may differ in detail to
address new problems or concerns.
Each version is given a distinguishing version number. If the
Program specifies that a certain numbered version of the GNU General
Public License "or any later version" applies to it, you have the
option of following the terms and conditions either of that numbered
version or of any later version published by the Free Software
Foundation. If the Program does not specify a version number of the
GNU General Public License, you may choose any version ever published
by the Free Software Foundation.
If the Program specifies that a proxy can decide which future
versions of the GNU General Public License can be used, that proxy's
public statement of acceptance of a version permanently authorizes you
to choose that version for the Program.
Later license versions may give you additional or different
permissions. However, no additional obligations are imposed on any
author or copyright holder as a result of your choosing to follow a
later version.
15. Disclaimer of Warranty.
THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY
APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT
HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY
OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO,
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM
IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF
ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
16. Limitation of Liability.
IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS
THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY
GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF
DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD
PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS),
EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
SUCH DAMAGES.
17. Interpretation of Sections 15 and 16.
If the disclaimer of warranty and limitation of liability provided
above cannot be given local legal effect according to their terms,
reviewing courts shall apply local law that most closely approximates
an absolute waiver of all civil liability in connection with the
Program, unless a warranty or assumption of liability accompanies a
copy of the Program in return for a fee.
END OF TERMS AND CONDITIONS
How to Apply These Terms to Your New Programs
If you develop a new program, and you want it to be of the greatest
possible use to the public, the best way to achieve this is to make it
free software which everyone can redistribute and change under these terms.
To do so, attach the following notices to the program. It is safest
to attach them to the start of each source file to most effectively
state the exclusion of warranty; and each file should have at least
the "copyright" line and a pointer to where the full notice is found.
<one line to give the program's name and a brief idea of what it does.>
Copyright (C) <year> <name of author>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
Also add information on how to contact you by electronic and paper mail.
If the program does terminal interaction, make it output a short
notice like this when it starts in an interactive mode:
<program> Copyright (C) <year> <name of author>
This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
This is free software, and you are welcome to redistribute it
under certain conditions; type `show c' for details.
The hypothetical commands `show w' and `show c' should show the appropriate
parts of the General Public License. Of course, your program's commands
might be different; for a GUI interface, you would use an "about box".
You should also get your employer (if you work as a programmer) or school,
if any, to sign a "copyright disclaimer" for the program, if necessary.
For more information on this, and how to apply and follow the GNU GPL, see
<http://www.gnu.org/licenses/>.
The GNU General Public License does not permit incorporating your program
into proprietary programs. If your program is a subroutine library, you
may consider it more useful to permit linking proprietary applications with
the library. If this is what you want to do, use the GNU Lesser General
Public License instead of this License. But first, please read
<http://www.gnu.org/philosophy/why-not-lgpl.html>.

0
ChangeLog Normal file
View file

View file

@ -0,0 +1,106 @@
/*
* dmbcs-micro-server A C++ library providing CGI or built-in
* web server functions
*
* Copyright (C) 2018 DM Bespoke Computer Solutions Ltd
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or (at
* your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef DMBCS__MICRO_SERVER__HTTP_SERVER__H
#error The http-server--impl.h header should not be included in application code.
#endif
#include <regex>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <sstream>
namespace DMBCS { namespace Micro_Server {
/* This is the detailed implementation of the \c return_* methods
* below, which form the public interface to be used by
* applications. */
void return_payload (const int client_socket,
const string_view http_type,
const string_view origin_url,
const char *const payload,
const size_t length);
inline void Http_Server::return_html (int const socket, string const &html)
{
return_payload (socket, "text/html", {}, html.c_str (), html.length ());
}
inline void Http_Server::return_text (int const socket, string const &text)
{
return_payload (socket, "text/plain", {}, text.c_str (), text.length ());
}
inline void Http_Server::return_css (int const socket, string const &text)
{
return_payload (socket, "text/css", {}, text.c_str (), text.length ());
}
inline void Http_Server::return_pdf (int const socket, string const &pdf)
{
return_payload (socket, "application/pdf", {}, pdf.c_str (), pdf.length ());
}
inline void Http_Server::return_json (int const socket,
string const &json,
const string_view origin_url)
{
return_payload (socket, "application/json", origin_url,
json.c_str (), json.length ());
}
inline void Http_Server::return_bad (int const socket)
{ return_payload (socket, "text/plain", {}, nullptr, 0); }
inline void Http_Server::return_png (int const socket,
vector<uint8_t> const &png)
{
return_payload (socket, "image/png", {},
(char const *) png.data (), png.size ());
}
inline void Http_Server::return_svg (int const socket, string const &svg)
{
return_payload (socket, "image/svg+xml", {}, svg.c_str (), svg.length ());
}
} } /* End of namespace DMBCS::Micro_Server. */

View file

@ -0,0 +1,184 @@
/*
* dmbcs-micro-server A C++ library providing CGI or built-in
* web server functions
*
* Copyright (C) 2018 DM Bespoke Computer Solutions Ltd
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or (at
* your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <DMBCS/micro-server/http-server.h>
namespace DMBCS { namespace Micro_Server {
string Http_Server::add_html_headers (string const &&body)
{
auto hold = ostringstream {};
hold << "Content-type: text/html; charset=utf-8\r\n"
<< "Content-length: " << body.length () << "\r\n\r\n";
return hold.str () + move (body);
}
void return_payload (const int socket,
const string_view type,
const string_view origin_url,
const char *const payload,
const size_t length)
{
ostringstream out {};
out << "HTTP/1.1 200 OK\r\n";
if (origin_url.size ())
out << "Access-Control-Allow-Origin: " << origin_url << "\r\n";
out << "Content-type: " << type << "; charset=utf-8\r\n"
<< "Content-length: " << length
<< "\r\n\r\n"
<< string {payload, length};
write (socket, out.str ().c_str (), out.str ().length ());
}
static string extract_header (const vector<char>& request,
const string& tag)
{
static const regex re {"[\r\n]" + tag + ":[[:space:]]*([^\r\n]*)"};
match_results<vector<char>::const_iterator> match;
if (regex_search (begin (request), end (request), match, re))
return match.str (1);
return {};
}
void Http_Server::process_message (vector<char> &&request,
int const socket)
{
static const regex protocol_re
{"^(GET|POST|OPTIONS)[[:space:]]+([^[:space:]?]+)([?]([^[:space:]]*))?"};
match_results<vector<char>::iterator> match {};
if (! regex_search (begin (request), end (request), match, protocol_re))
{
Http_Server::return_bad (socket);
return;
}
auto callback = service_methods.find (match.str(2) [0] == '/'
? match.str(2).substr (1)
: match.str(2));
if (callback == end (service_methods))
{
Http_Server::return_bad (socket);
return;
}
Query_String QS;
/* We need to extract this in advance as we will destroy the request
* buffer when we manufacture the Query_String object. */
const string access_url { extract_header (request, "Origin") };
if (match.str (1) == "OPTIONS")
{
std::ostringstream O;
O << "HTTP/1.1 200 OK\r\n"
<< "Access-Control-Allow-Origin: " << access_url << "\r\n"
<< "Access-Control-Allow-Methods: POST, GET, OPTIONS\r\n"
<< "Access-Control-Max-Age: 86400\r\n\r\n";
write (socket, O.str ().data (), O.str ().size ());
return;
}
/* We need to extract this in advance as we will destroy the request
* buffer when we manufacture the Query_String object. */
const string cookie { extract_header (request, "Cookie") };
if (match.str (1) == "POST")
{
static auto const length_re
= regex {"[\r\n]Content-Length:[[:space:]]*([^\r\n]*)"};
auto match = match_results<vector<char>::iterator> {};
if (! regex_search
(begin (request), end (request), match, length_re))
{
Http_Server::return_bad (socket);
return;
}
auto content_start = int {0};
{
static auto const re = regex {"\r\n\r\n"};
auto match = match_results<vector<char>::iterator> {};
regex_search (begin (request), end (request), match, re);
content_start = match.position ();
}
auto const needed_len
= (size_t) (atoi (match.str (1).c_str ()) + content_start + 4);
auto len = request.size ();
request.resize (needed_len);
while (len < needed_len)
{
auto const L
= read (socket, request.data () + len, needed_len - len);
if (L <= 0)
{
Http_Server::return_bad (socket);
return;
}
len += L;
}
scan_multipart (QS,
{begin (request) + content_start + 4, end (request)});
QS.post_method = 1;
}
else /* GET */
{
QS = query_string (match.str (4));
}
if (cookie.length () > 0) QS.add ("__cookie__", cookie);
QS.access_url = access_url;
(callback->second) (move (QS), socket);
}
} } /* End of namespace DMBCS::Micro_Server. */

View file

@ -0,0 +1,174 @@
/*
* dmbcs-micro-server A C++ library providing CGI or built-in
* web server functions
*
* Copyright (C) 2018 DM Bespoke Computer Solutions Ltd
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or (at
* your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef DMBCS__MICRO_SERVER__HTTP_SERVER__H
#define DMBCS__MICRO_SERVER__HTTP_SERVER__H
#include <DMBCS/micro-server/tcp-server.h>
#include <DMBCS/micro-server/query-string.h>
namespace DMBCS { namespace Micro_Server {
/** We understand a HTTP server as a purely functional object: a
* method call with arguments, returning a value which is a function
* only of those arguments and having no other side-effects. The
* prototype URL is
* {host}:{port}/{method}?{par1}={value1}&{par2}={value2}... where
* {host} is obviously us and {port} is supplied to the server
* constructor. The constructor method (\c create) takes one other
* argument: a mapping of {method} names onto function objects; these
* are called with a set of parameters as distilled from the URL, and
* a socket to which the function is expected to send the
* corresponding reply. Convenience methods are provided by this
* class to help with the composition of those replies, and all
* functions should conclude their work by calling one of these
* methods.
*
* Thus there are two functions which the application is immediately
* concerned with: the \c create function which returns a
* fully-constructed \c Http_Server object given a map of method names
* to functions and a local port number on which to operate, and the \c
* tick function which causes the server to operate for a single
* interaction with some client. The application should call this \c
* tick method frequently so that the server can function as required.
*
* The rest of the interesting functions are the \c return_* functions,
* which will send to a given \c client_socket a fully-formed HTTP
* response carrying a payload of various type: PNG image, JSON data,
* or HTML web pages. */
struct Http_Server : Tcp_Server
{
/** The type of function-thing which implements the brawn behind a
* particular service call. */
using Service_Method = function<void (Query_String const &,
int const client_socket)>;
/** The type of object which holds the correspondence between server
* method names and the C++ function objects which implement the
* methods. */
using Service_Methods = map<string, Service_Method>;
Service_Methods service_methods;
/** The sole object constructor. Creates a ready-to-use server object
* which operates the HTTP protocol on the local \a port, and will
* call out to functions in the \a methods object. The only action
* which can be performed on this object is to pass it to the \c tick
* function below, allowing the server to perform a small unit of
* work. */
Http_Server (uint16_t const port, Service_Methods &&methods)
: Tcp_Server {port}, service_methods {move (methods)}
{}
void process_message (vector<char> &&message,
int const socket) override;
/* !!!! We need to question whether we want this or not. */
static string add_html_headers (string const &&);
/** Convenience function allowing a server method to send a \a html
* page back to the client on the given \a client_socket. */
static void return_html (int const client_socket, string const &html);
/** Convenience function allowing a server method to send a \a text
* page back to the client on the given \a client_socket. */
static void return_text (int const client_socket, string const &text);
/** Convenience function allowing a server method to send a \a CSS
* specification back to the client on the given \a client_socket. */
static void return_css (int const client_socket, string const &text);
/** Convenience function allowing a server method to send a \a pdf
* document back to the client on the given \a client_socket. */
static void return_pdf (int const client_socket, string const &pdf);
/** Convenience function allowing a server method to send a \a json
* data structure back to the client on the given \a
* client_socket. */
static void return_json (int const client_socket, string const &json,
const string_view origin_url);
/** Convenience function allowing a server method to send a response
* to a bad request to the client. Note that we actually send a good
* response of an empty payload in this case (we have got this far;
* as far as the HTTP protocol is concerned any well-formatted
* response from us is a good one, even if it signals failure of the
* server to produce meaningful results). */
static void return_bad (int const client_socket);
/** Convenience function allowing a server method to send a \a png
* image back to the client on the given \a client_socket. The \a
* png should be provided by the \c Png class available with this
* library. */
static void return_png (int const client_socket,
vector<uint8_t> const &png);
/** Convenience function allowing a server method to send a \a svg
* image back to the client on the given \a client_socket. The \a
* png should be provided by the \c Png class available with this
* library. */
static void return_svg (int const client_socket,
string const &svg);
} ; /* End of class Http_Server. */
} } /* End of namespace DMBCS::Micro_Server. */
inline void add (DMBCS::Micro_Server::Http_Server &S,
std::string &&method_name,
DMBCS::Micro_Server::Http_Server::Service_Method &&M)
{ S.service_methods.emplace (method_name, M); }
inline bool tick (DMBCS::Micro_Server::Http_Server &S,
const std::chrono::system_clock::duration& wait)
{ return S.tick (wait); }
#include <DMBCS/micro-server/http-server--impl.h>
#endif /* Undefined DMBCS__MICRO_SERVER__HTTP_SERVER__H. */

View file

@ -0,0 +1,171 @@
/*
* dmbcs-micro-server A C++ library providing CGI or built-in
* web server functions
*
* Copyright (C) 2018 DM Bespoke Computer Solutions Ltd
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or (at
* your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <DMBCS/micro-server/hyper-tags.h>
#include <cstdarg>
#include <sstream>
#include <iomanip>
#include <vector>
#include <regex>
namespace DMBCS { namespace Micro_Server {
/* Note that we need to take a copy of the replacement texts, since it
* is understood that the original _reg_ is to be left intact. */
Hyper_Tags &Hyper_Tags::add (Hyper_Tags const &reg, int const harden)
{
for (auto i : reg.tags)
{
add (i.name, i.replacement);
if (harden) harden_last ();
}
return *this;
}
Hyper_Tags &Hyper_Tags::add (string const &tag,
double const &value,
int const precision)
{
auto hold = ostringstream {};
hold << setprecision (precision) << value;
return add (tag, hold.str ());
}
void Hyper_Tags::add_date (string const &tag,
time_t const &time,
string const &format)
{
array<char, 200> buffer;
strftime (& buffer [0], buffer.size (), format.c_str (), localtime (&time));
add (tag, & buffer [0]);
}
Hyper_Tags & Hyper_Tags::add_query (Query_String const &Q)
{
for (auto const &q : Q) add (q.first, q.second);
return *this;
}
void Hyper_Tags::add_passthrough_tag (Query_String const &query_string,
...)
{
va_list arguments; va_start (arguments, query_string);
auto acc = string {};
const char *query_name;
while ((query_name = va_arg (arguments, const char*)))
{
auto const query_value = query_string.get (query_name, string {});
if (query_value.length ())
{
acc += string {"<input type=\"hidden\" name=\""}
+ query_name
+ "\" value=\""
+ query_value
+ "\"/>";
}
}
add ("pass-through", move (acc));
va_end (arguments);
}
string Hyper_Tags::substitute_ (string &&input, int repeat)
{
while (repeat-- > 0)
{
for (auto const &i : sections)
input = remove_tags (i.name, move (input), i.keep == LOSE_SECTION);
for (auto const &i : tags)
{
auto hold = ostringstream {};
hold << '[' << i.name << "/]";
input = transform (move (input), hold.str (), i.replacement);
}
}
return input;
}
string Hyper_Tags::remove_tags (string const &tag_name,
string &&input,
bool const &whole_section)
{
auto const open_tag = "[" + tag_name + "]";
auto const close_tag = "[/" + tag_name + "]";
for (auto start_end = input.find (close_tag);
start_end != input.npos;
start_end = input.find (close_tag))
{
auto const start_open = input.rfind (open_tag, start_end);
if (start_open == input.npos) return input;
if (whole_section)
input.erase (start_open,
start_end + close_tag.length () - start_open);
else
{
input.erase (start_end, close_tag.length ());
input.erase (start_open, open_tag.length ());
}
}
return input;
}
string Hyper_Tags::strip_loose_tags (string&& in)
{
static const regex re {"\\[[-!_[:alnum:]]*/\\]"};
cmatch match {};
for (int i = 0;
regex_search (in.c_str () + i, match, re);
i += match.position ())
in.erase (i + match.position (), match.length ());
return in;
}
} } /* End of namespace DMBCS::Micro_Server. */

View file

@ -0,0 +1,200 @@
/*
* dmbcs-micro-server A C++ library providing CGI or built-in
* web server functions
*
* Copyright (C) 2018 DM Bespoke Computer Solutions Ltd
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or (at
* your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef DMBCS__MICRO_SERVER__HYPER_TAGS__H
#define DMBCS__MICRO_SERVER__HYPER_TAGS__H
#include <DMBCS/micro-server/query-string.h>
#include <algorithm>
#include <list>
#include <string>
namespace DMBCS { namespace Micro_Server {
using namespace std;
struct Hyper_Tags
{
/* A tag is an entry on a HTML template of the form [xxx/], which is
* to be replaced with some dynamic content. */
struct Tag
{
string name;
string replacement;
} ;
/* A section is part of a HTML template surrounded by strings of the
* form [xxx] and [/xxx]; either the whole section will be eliminated
* (LOSE_SECTION) or just the surrounding tags will be stripped
* leaving the content of the section in place (KEEP_SECTION). */
enum Section_Keep { KEEP_SECTION = 1, LOSE_SECTION = 0 } ;
struct Section
{
string name;
Section_Keep keep;
} ;
/* New tags are inserted at the front of the list, so normally they
* override older tag definitions. This can be overridden by making a
* new tag `hard', when the last_tag pointer will be placed after it,
* and all future additions to the registry will go in *after* the
* hard tags, and thus will *not* override the earlier meaning. */
list<Tag> tags;
list<Tag>::iterator last_hard; /* Actually, the first `soft'. */
vector<Section> sections;
Hyper_Tags () : last_hard {begin (tags)} {}
Hyper_Tags (Hyper_Tags const &) = delete;
Hyper_Tags (Hyper_Tags &&) = default;
Hyper_Tags &operator= (Hyper_Tags const &) = delete;
Hyper_Tags &operator= (Hyper_Tags &&) = default;
static string remove_tags (string const &tag_name,
string &&input,
bool const &whole_section);
Hyper_Tags &section (string const &name, Section_Keep const &keep)
{
sections.push_back (Section {name, keep});
sections.push_back (Section {"!" + name,
keep ? LOSE_SECTION : KEEP_SECTION});
return *this;
}
void harden_last () { if (last_hard != end (tags)) ++last_hard; }
bool has (string const &a) const
{
return find_if (begin (tags), end (tags), [&a] (Tag const &T)
{ return T.name == a; })
!= end (tags);
}
Hyper_Tags &add (Hyper_Tags const &registry, int const harden = 0);
Hyper_Tags &operator+= (const Hyper_Tags &tags) { return add (tags); }
Hyper_Tags &add (const string &tag,
string &&substitution_text)
{ last_hard = tags.insert (last_hard,
Tag {tag, move (substitution_text)});
return *this; }
template <typename T>
Hyper_Tags &add (string const &tag, T const &value)
{
ostringstream hold;
hold << value;
return add (tag, hold.str ());
}
Hyper_Tags &add
(string const &tag, double const &value, int const precision = 4);
template <typename T>
Hyper_Tags &add (const string &tag, Query_String &qs, const T &fallback)
{ add (tag, qs.get (tag, fallback)); return *this; }
Hyper_Tags &add_query (Query_String const &);
/* The tag is replaced with a string consisting of a comma-separated
* (no spaces) list of integers. */
template <typename Iterator>
void add_int_list (const string &tag, Iterator begin, Iterator end);
void add_date (const string &tag,
const time_t &time,
const string &format);
void add_file (const string &tag, const string &file_name)
{ add (tag, slurp_file (file_name)); }
/* The trailing arguments are a list of const char *const, terminated
* with nullptr. */
void add_passthrough_tag (const Query_String& query_string, ...)
__attribute__ ((sentinel));
Hyper_Tags& keep_section (const string& name, const bool keep = 1)
{ return section (name, keep ? KEEP_SECTION : LOSE_SECTION); }
Hyper_Tags& lose_section (const string& name, const bool lose = 1)
{ return section (name, lose ? LOSE_SECTION : KEEP_SECTION); }
string substitute_ (string&& html_template, int repeat);
static string strip_loose_tags (string&&);
} ; /* End of class Hyper_Tags. */
template <typename Iterator>
inline void Hyper_Tags::add_int_list (const string &tag_name,
Iterator begin,
Iterator end)
{
auto acc = ostringstream {};
acc << *begin;
for (++begin; begin != end; ++begin) acc << ',' << *begin;
add (tag_name, acc.str ());
}
} } /* End of namespace DMBCS::Micro_Server. */
inline std::string substitute (DMBCS::Micro_Server::Hyper_Tags &tags,
std::string &&html_template,
int repeat = 1)
{ return tags.substitute_ (std::move (html_template), repeat); }
inline std::string substitute (std::string &&html_template,
DMBCS::Micro_Server::Hyper_Tags &tags,
int repeat = 1)
{ return tags.substitute_ (std::move (html_template), repeat); }
#endif /* Undefined DMBCS__MICRO_SERVER__HYPER_TAGS__H. */

View file

@ -0,0 +1,45 @@
# dmbcs-micro-server A C++ library providing CGI or built-in
# web server functions
#
# Copyright (C) 2018 DM Bespoke Computer Solutions Ltd
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or (at
# your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
incdir = ${prefix}/include/DMBCS/micro-server
inc2dir = ${prefix}/include/DMBCS
AM_CXXFLAGS = -Wall -Wextra -I${top_srcdir} \
-DMAC='"@MAC@"' \
${CURLPP_CFLAGS} -std=c++2a
AM_LIBS = ${CURLPP_LIBS}
lib_LTLIBRARIES = libdmbcs-micro-server.la
dmbcs_classes = http-server \
hyper-tags \
query-string \
tcp-server \
udp-server \
utils
libdmbcs_micro_server_la_SOURCES = ${dmbcs_classes:=.cc}
inc_HEADERS = ${dmbcs_classes:=.h} micro-server.h http-server--impl.h
inc2_HEADERS = micro-server.h
MAINTAINERCLEANFILES = makefile.in

View file

@ -0,0 +1,31 @@
/*
* dmbcs-micro-server A C++ library providing CGI or built-in
* web server functions
*
* Copyright (C) 2018 DM Bespoke Computer Solutions Ltd
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or (at
* your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef DMBCS__MICRO_SERVER__MICRO_SERVER__H
#define DMBCS__MICRO_SERVER__MICRO_SERVER__H
#include <DMBCS/micro-server/http-server.h>
#include <DMBCS/micro-server/hyper-tags.h>
#include <DMBCS/micro-server/tcp-server.h>
#include <DMBCS/micro-server/udp-server.h>
#include <DMBCS/micro-server/utils.h>
#endif /* Undefined DMBCS__MICRO_SERVER__MICRO_SERVER__H. */

View file

@ -0,0 +1,238 @@
/*
* dmbcs-micro-server A C++ library providing CGI or built-in
* web server functions
*
* Copyright (C) 2018 DM Bespoke Computer Solutions Ltd
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or (at
* your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <DMBCS/micro-server/query-string.h>
#include <cstdlib>
#include <cstdio>
#include <iostream>
#include <regex>
#include <sstream>
namespace DMBCS { namespace Micro_Server {
static void scan_string (Query_String &Q, string const &a)
{
for (string::size_type name_start = 0; name_start < a.length (); )
{
auto const name_end = a.find_first_of ("&=", name_start);
auto const name
= name_end == a.npos ? a.substr (name_start)
: a.substr (name_start, name_end - name_start);
string::size_type value_end;
if (name_end == a.npos || a [name_end] == '&')
{
Q [name] = "1";
value_end = name_end;
}
else
{
auto const value_start = name_end + 1;
value_end = a.find ('&', value_start);
auto const real_end = value_end == a.npos ? a.length ()
: value_end;
Q [name] = decode_url (a.substr (value_start,
real_end - value_start));
}
name_start = value_end
+ ((value_end != a.npos && a [value_end] == '&') ? 1 : 0);
}
}
void scan_multipart (Query_String &Q, string const &a)
{
auto const boundary_end = a.find_first_of ("\r\n");
if (boundary_end == a.npos)
return;
auto const boundary = "\r\n" + a.substr (0, boundary_end);
auto last_boundary = size_t {0};
for (;;)
{
auto const next_boundary = a.find (boundary, last_boundary + 1);
if (next_boundary == a.npos)
return;
auto const name_start = a.find ("name=\"", last_boundary);
if (name_start == a.npos || name_start > next_boundary)
return;
auto const name_end = a.find ("\"", name_start + 6);
if (name_end == a.npos)
return;
auto const name = a.substr (name_start + 6,
name_end - name_start - 6);
auto const data_start = a.find ("\r\n\r\n", name_end);
if (data_start == a.npos)
return;
if (name == "upload-file")
copy (a.begin () + data_start + 4,
a.begin () + next_boundary,
back_inserter (Q.upload_data));
else
Q [name] = a.substr (data_start + 4,
next_boundary - (data_start + 4));
last_boundary = next_boundary;
}
}
Query_String get_query_string ()
{
auto ret = Query_String {};
auto const method = getenv ("REQUEST_METHOD");
if (method.empty () || method == "GET")
{
scan_string (ret, getenv ("QUERY_STRING"));
}
else
{
ret.post_method = 1;
auto const input_length = atoi (getenv ("CONTENT_LENGTH").c_str ());
auto line = string {};
for (int i = 0; i < input_length; ++i)
line += getchar ();
if (line [0] == '-' && line [1] == '-')
scan_multipart (ret, line);
else
scan_string (ret, line);
}
return ret;
}
Query_String query_string (string const &input)
{
Query_String ret;
scan_string (ret, input);
return ret;
}
string query_string (Query_String const &Q)
{
if (Q.empty ()) return string {};
auto separator = string {};
auto ret = string {};
for (auto const &i : Q)
{
ret += separator + i.first + '=' + i.second;
separator = "&";
}
return ret;
}
vector <int> Query_String::get_list (string const &name) const
{
auto ret = vector <int> {};
char comma;
auto s = istringstream {get (name, string ())};
for (;;)
{
int k; s >> k;
if (s)
{
ret.push_back (k);
s >> comma;
}
else
return ret;
}
}
void Query_String::make_list (string const &name,
vector <int> const &list)
{
if (list.empty ()) return;
auto hold = ostringstream {};
hold << list.front ();
for (auto i = list.begin () + 1; i != list.end (); ++i)
hold << "," << *i;
(*this) [name] = hold.str ();
}
vector <int> Query_String::get_checked_array (string const &name_prefix)
const
{
auto ret = vector <int> {};
for (auto i : *this)
if (name_prefix == i.first.substr (0, name_prefix.length ()))
ret.push_back (atol (i.first.substr (name_prefix.length ()).c_str ()));
return ret;
}
} } /* End of namespace DMBCS::Micro_Server. */

View file

@ -0,0 +1,194 @@
/*
* dmbcs-micro-server A C++ library providing CGI or built-in
* web server functions
*
* Copyright (C) 2018 DM Bespoke Computer Solutions Ltd
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or (at
* your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef DMBCS__MICRO_SERVER__QUERY_STRING__H
#define DMBCS__MICRO_SERVER__QUERY_STRING__H
#include <DMBCS/micro-server/utils.h>
#include <vector>
#include <string>
#include <map>
#include <sstream>
#include <stdexcept>
namespace DMBCS { namespace Micro_Server {
/* An object designed to hold the (name, value) pairs passed to a GCI
* program through the query part of the URL, and carries along with it
* the information of whether query string came from a HTTP POST or GET
* method, and if a message authentication code (MAC) was verified on
* the URL. There is also the possibility that the object holds the
* contents of an uploaded file, if such was sent from a POSTed HTML
* form.
*
* The chief methods are the /has/ and /get/ methods, to determine if a
* query string has a given value, and to get a value (which may be
* interpreted as any numeric or string type) returning some default if
* it is not there.
*
* The main functions are the /query_string/s which produces a
* Query_String object from a CGI programʼs environment (as
* appropriate), and conversely produce a URI fragment with the
* contents of a given Query_String object. */
struct Query_String : map <string, string>
{
/* The secret mixed into a URL before computing a cryptographic
* fingerprint. The value assigned here comes from the top-level
* configure script via the local makefile. */
static constexpr char const * MAC_KEY = MAC;
/* Either the HTTP method was POST, or else we assume GET. */
bool post_method {0};
/* Either the query contained a verified MAC, or it didnʼt. */
bool mac_verified_ {0};
/* Will be empty unless this was a multipart POST with an uploaded
* file in it. */
vector <uint8_t> upload_data;
/* If we have been called from a browser exercising cross-site access
* control, this is the URL of the other site from which we will allow
* access. */
string access_url;
/* Allow all bulk operations except copy, which might get a tad
* expensive if done in the wrong place. */
Query_String () = default;
Query_String (Query_String &) = delete;
Query_String (Query_String &&) = default;
Query_String &operator= (Query_String const &) = delete;
Query_String &operator= (Query_String &&) = default;
/* Add a new parameter to this query set, with the given name and
* value. */
template <typename T>
void add (string const &name, T const &value)
{ ostringstream hold; hold << value; add (name, hold.str ()); }
/* Add a new parameter to this query set, with the given name and a
* value pulled from a parameter with that same name in the input
* query string, or else use the fallback as the value. */
template <typename T>
void add (string const &name,
Query_String const &input,
T const &fallback)
{ add (name, input.get (name, fallback)); }
/* Return TRUE if the HTTP request indicated a POST method, else
* return FALSE. */
bool posted () const { return post_method; }
/* Return TRUE only if this query had a MAC which we have verified. */
bool mac_verified () const { return mac_verified_; }
/* Return TRUE only if this query has a parameter with the given
* name. */
bool has (string const &name) const { return find (name) != end (); }
/* Get the value of the parameter with the given name in this query;
* use the fallback value if this procedure fails (the fallback is
* also useful to establish the desired return type, which may be any
* numerical or string type). */
template <typename T>
T get (string const &name, T const &fallback = {}) const;
/* If the value of a parameter is a comma-separated list of integers,
* the list is returned as a vector. (This is the only list case we
* currently implement; others may appear in future versions. */
vector<int> get_list (string const &name) const;
/* Add a comma-separated list of integers to /this/ object. */
void make_list (string const &name, vector <int> const &list);
/* If the parameters which start with the given name are present, the
* remainder of the name should be a numerical sequence and the
* numerical values of all these parameters are returned in the
* unsorted vector of ints. */
vector<int> get_checked_array (string const &name) const;
} ;
/* Manufacture a Query_String according to environment variables and/or
* standard input, as directed by the CGI specification. */
Query_String get_query_string ();
/* Manufacture a Query_String object to reflect the URI parameter list
* found in the input. */
Query_String query_string (string const & input);
/* Create a URI parameter list with the data held in the Query_String,
* and add a guarding MAC to the end. */
string query_string_with_mac (Query_String const &);
/* Create a URI parameter list with the data held in the Query_String
* (no MAC will be present). */
string query_string (Query_String const &);
void scan_multipart (Query_String &Q, string const &a);
/********************* IMPLEMENTATION ***********************/
template <>
inline string Query_String::get (string const &name,
string const &fallback) const
try { return at (name); }
catch (out_of_range&) { return fallback; }
template <typename T>
inline T Query_String::get (string const &name, T const &fallback) const
try { if (at (name).length () == 0) return fallback;
T ret; istringstream {at (name)} >> ret; return ret; }
catch (out_of_range&) { return fallback; }
template <>
inline void Query_String::add (string const &name, string const &value)
{ (*this) [name] = value; }
template<> inline void
Query_String::add (string const &name, Query_String const &input_query)
{ add (name, input_query.get (name, string {})); }
} } /* End of namespace DMBCS::Micro_Server. */
#endif /* Undefined DMBCS__MICRO_SERVER__QUERY_STRING__H. */

View file

@ -0,0 +1,202 @@
/*
* dmbcs-micro-server A C++ library providing CGI or built-in
* web server functions
*
* Copyright (C) 2018 DM Bespoke Computer Solutions Ltd
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or (at
* your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <DMBCS/micro-server/tcp-server.h>
#include <sys/socket.h>
#include <iostream>
#include <netinet/in.h>
#include <string.h>
#include <sstream>
#include <vector>
#include <unistd.h>
namespace DMBCS { namespace Micro_Server {
/* Used also by Udp_Server. */
int Tcp_Server::bind_socket (uint16_t const port, int const socket_type)
{
const int listen_socket = socket (AF_INET, socket_type, 0);
int option = 1;
setsockopt (listen_socket, SOL_SOCKET, SO_REUSEADDR, (char*)&option,
sizeof (option));
sockaddr_in addr;
addr.sin_family = AF_INET;
addr.sin_port = htons (port);
addr.sin_addr.s_addr = INADDR_ANY;
if (::bind (listen_socket, (sockaddr const *) &addr, sizeof (addr)))
throw runtime_error {"cannot bind to port " + std::to_string (port)};
return listen_socket;
}
Tcp_Server::Tcp_Server (uint16_t const port)
{
listen_socket = bind_socket (port, SOCK_STREAM);
listen (listen_socket, 5);
}
bool Tcp_Server::tick (const chrono::system_clock::duration& wait,
fd_set& read_socket_set,
std::function<void(const fd_set&)> fallout)
{
append_to_fdset (*this, &read_socket_set);
timeval t {chrono::duration_cast<chrono::seconds> (wait).count (),
chrono::duration_cast<chrono::microseconds> (wait).count ()
% 1'000'000};
auto const test
= select (FD_SETSIZE, &read_socket_set, nullptr, nullptr, &t);
if (-1 == test)
{
if (EINTR == errno)
throw runtime_error {"program interrupted"};
else
throw runtime_error {"TCP server socket problem"};
}
if (0 == test)
return 0;
if (FD_ISSET (listen_socket, &read_socket_set))
{
sockaddr_in addr;
socklen_t size = sizeof (addr);
int sock = accept (listen_socket, (sockaddr*) &addr, &size);
client_sockets [sock] = addr;
return 1;
}
for (auto const c : client_sockets)
if (FD_ISSET (c.first, &read_socket_set))
{
/* !! Hardwire, hard limit. */
auto const len = read (c.first,
message_buffer.data (),
message_buffer.size ());
if (len < 0)
{
std::cerr << strerror (errno);
close (c.first);
client_departed (c.first);
client_sockets.erase (c.first);
}
else if (! len)
{
close (c.first);
client_departed (c.first);
client_sockets.erase (c.first);
}
else
process_message ({begin (message_buffer),
begin (message_buffer) + len},
c.first);
return 1;
}
fallout (read_socket_set);
return 1;
}
bool Aggregate_Ticker::operator()
(const chrono::system_clock::duration& wait)
{
fd_set fds;
FD_ZERO (&fds);
int top {0};
for (auto &T : servers)
top = std::max (top, append_to_fdset (*T, &fds));
timeval t { chrono::duration_cast<chrono::seconds> (wait).count (),
chrono::duration_cast<chrono::microseconds> (wait).count ()
% 1'000'000};
auto const test = select (FD_SETSIZE, &fds, nullptr, nullptr, &t);
if (-1 == test)
{
if (EINTR == errno) throw runtime_error {"program interrupted"};
else
throw runtime_error {"server socket problem, errno="
+ std::to_string (errno)};
}
if (0 == test) return 0;
for (auto &T : servers)
if (T->tick (chrono::microseconds {0}))
return 1;
return 0;
}
int append_to_fdset (Tcp_Server const &tcp,
fd_set *const set)
{
FD_SET (tcp.listen_socket, set);
for (auto const &S : tcp.client_sockets) FD_SET (S.first, set);
return std::max (tcp.listen_socket, begin (tcp.client_sockets)->first);
}
fd_set provide_fdset (Tcp_Server const &tcp)
{
fd_set ret;
FD_ZERO (&ret);
append_to_fdset (tcp, &ret);
return ret;
}
uint16_t listener_port (const Tcp_Server& T)
{
sockaddr_in addr;
socklen_t len = sizeof (addr);
if (0 != getsockname (T.listen_socket, (sockaddr*) &addr, &len))
throw std::runtime_error
{"cannot get port of Tcp_Serverʼs listener socket"};
return ntohs (addr.sin_port);
}
} } /* End of namespace DMBCS::Micro_Server. */

View file

@ -0,0 +1,170 @@
/*
* dmbcs-micro-server A C++ library providing CGI or built-in
* web server functions
*
* Copyright (C) 2018 DM Bespoke Computer Solutions Ltd
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or (at
* your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef DMBCS__MICRO_SERVER__TCP_SERVER__H
#define DMBCS__MICRO_SERVER__TCP_SERVER__H
#include <arpa/inet.h>
#include <chrono>
#include <functional>
#include <map>
#include <set>
#include <unistd.h>
#include <vector>
namespace DMBCS { namespace Micro_Server {
using namespace std;
/* A Tcp_Server is an object which listens to a TCP port, accepts
* connections from multiple clients (and disconnections), and takes any
* messages these clients may send to us and hand them off to a
* specialized message despatcher. Real servers will inherit this class
* and provide their own specialized message processor. */
struct Tcp_Server
{
/* The socket on which we take new connections. */
int listen_socket;
/* All of the connected sockets, kept in _descending_ order so that
* the highest file descriptor of the sockets is immediately to
* hand. */
map<int, sockaddr_in, greater<int>> client_sockets;
vector<char> message_buffer {vector<char> (10000)};
static int bind_socket (const uint16_t port, const int socket_type);
/** The sole object constructor. Creates a ready-to-use server object
* which operates the TCP protocol on the local \a port, and will
* call out to the process_message method when any messages come in.
* The only action which can be performed on this object is to call
* the \c tick method, allowing the server to perform a small unit of
* work.
*
* Will throw a runtime_error if it is not possible to establish the
* listening socket. */
explicit Tcp_Server (const uint16_t port);
/* Boilerplate requirement of destructor of virtual class. */
virtual ~Tcp_Server () noexcept { close (listen_socket); }
/* A function which must take the message which has come off of the
* wire, do whatever work is required to service the message, and then
* send an appropriate response back to the client, via the given
* socket. */
virtual void process_message (vector<char>&& message,
const int socket) = 0;
/* Will be called when a client disconnects. */
virtual void client_departed (const int /* socket */) {}
/** Cause the server to perform some small unit of work, or wait up to
* /wait/ time for some work requirement to appear.
*
* The return value indicates if any work was done. Will throw a
* runtime_error if the program receives a signal during
* operations. */
bool tick (const chrono::system_clock::duration& wait,
fd_set& read_socket_set,
std::function<void(const fd_set&)> fallout);
bool tick (const chrono::system_clock::duration& wait)
{
fd_set s {}; FD_ZERO (&s);
return tick (wait, s, nullptr);
}
/** Surrender our interest in this socket. The socket itself will
* continue to remain active, but we will no longer be polling it for
* data and returning messages from it. */
int disown (const int socket)
{ client_sockets.erase (socket); return socket; }
} ; /* End of class Tcp_Server. */
/* Find out the TCP port number of the listener socket, to which clients
* can connect. This is necessary if the class was set up with a zero
* port, in which case the operating system would have quietly assigned
* a random port.
*
* Will throw a runtime_error if the operation fails. */
uint16_t listener_port (const Tcp_Server&);
/* Append all listening and connected sockets in the Tcp_Server to the
* fd_set. */
int append_to_fdset (const Tcp_Server &, fd_set *const);
inline int append_to_fdset (fd_set *const f, const Tcp_Server& T)
{ return append_to_fdset (T, f); }
/* Manufacture an fdset containing the listening socket and all of the
* connected client sockets in the Tcp_Server. */
fd_set provide_fdset (const Tcp_Server& tcp);
/* This class proxies a set of separate Tcp_Server's, presenting them to
* the application as if they were one. In particular, it allows each
* of the servers equal opportunity to service an incoming message.
*
* NOTE that it is left to the application to destroy the individual
* servers, once an Aggregate_Ticker is destroyed. */
struct Aggregate_Ticker
{
vector <Tcp_Server*> servers;
Aggregate_Ticker (vector <Tcp_Server*>&& s_) : servers {move (s_)}
{}
bool operator() (const chrono::system_clock::duration& wait);
};
/* Manufacture an Aggregate_Ticker. */
inline Aggregate_Ticker aggregate_ticker (vector <Tcp_Server*>&& S)
{ return Aggregate_Ticker {move (S)}; }
} } /* End of namespace DMBCS::Micro_Server. */
#endif /* Undefined DMBCS__MICRO_SERVER__TCP_SERVER__H. */

View file

@ -0,0 +1,96 @@
/*
* dmbcs-micro-server A C++ library providing CGI or built-in
* web server functions
*
* Copyright (C) 2018 DM Bespoke Computer Solutions Ltd
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or (at
* your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <DMBCS/micro-server/udp-server.h>
#include <DMBCS/micro-server/tcp-server.h> /* For bind_socket. */
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
#include <sstream>
#include <vector>
#include <unistd.h>
namespace DMBCS { namespace Micro_Server {
Udp_Server::Udp_Server (uint16_t const port)
{
listen_socket = Tcp_Server::bind_socket (port, SOCK_DGRAM);
}
bool Udp_Server::tick (std::chrono::system_clock::duration linger)
{
namespace C = std::chrono;
auto s = provide_fdset (*this);
timeval t { C::duration_cast<C::seconds> (linger).count (),
C::duration_cast<C::microseconds> (linger).count ()
% 1'000'000};
auto const test = select (listen_socket + 1, &s, nullptr, nullptr, &t);
if (-1 == test)
{
if (EINTR == errno)
throw runtime_error {"program interrupted"};
}
else if (0 == test)
return 0;
else if (FD_ISSET (listen_socket, &s))
{
static char buffer [1500];
auto const size
= recv (listen_socket, buffer, sizeof (buffer), 0);
process_message (std::vector<uint8_t>
((uint8_t*) buffer, (uint8_t*) (buffer + size)));
}
return 1;
}
int append_to_fdset (Udp_Server const &udp,
fd_set *const set)
{
FD_SET (udp.listen_socket, set);
return udp.listen_socket;
}
fd_set provide_fdset (Udp_Server const &udp)
{
fd_set ret;
FD_ZERO (&ret);
append_to_fdset (udp, &ret);
return ret;
}
} } /* End of namespace DMBCS::Micro_Server. */

View file

@ -0,0 +1,100 @@
/*
* dmbcs-micro-server A C++ library providing CGI or built-in
* web server functions
*
* Copyright (C) 2018 DM Bespoke Computer Solutions Ltd
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or (at
* your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef DMBCS__MICRO_SERVER__UDP_SERVER__H
#define DMBCS__MICRO_SERVER__UDP_SERVER__H
#include <chrono>
#include <functional>
#include <map>
#include <set>
#include <vector>
namespace DMBCS { namespace Micro_Server {
using namespace std;
/* A Tcp_Server is an object which listens to a TCP port, accepts
* connections from multiple clients (and disconnections), and takes any
* messages these clients may send to us and hand them off to a
* specialized message despatcher. Real servers will inherit this class
* and provide their own specialized message processor. */
struct Udp_Server
{
/* The socket on which we take new connections. */
int listen_socket;
string buffer;
/** The sole object constructor. Creates a ready-to-use server object
* which operates the TCP protocol on the local \a port, and will
* call out to the process_message method when any messages come in.
* The only action which can be performed on this object is to call
* the \c tick method, allowing the server to perform a small unit of
* work. */
explicit Udp_Server (uint16_t const port);
/* Boilerplate requirement of destructor of virtual class. */
virtual ~Udp_Server () = default;
/* A function which must take the message which has come off of the
* wire, do whatever work is required to service the message, and then
* send an appropriate response back to the client, via the given
* socket. */
virtual void process_message (std::vector<uint8_t> &&) = 0;
/** Cause the server to perform some small unit of work, or wait up to
* /wait/ time for some work requirement to appear.
*
* The return value indicates if any work was done. */
bool tick (std::chrono::system_clock::duration);
} ; /* End of class Tcp_Server. */
/* Append all listening and connected sockets in the Tcp_Server to the
* fd_set. */
int append_to_fdset (Udp_Server const &, fd_set *const);
inline int append_to_fdset (fd_set *const f, Udp_Server const &U)
{ return append_to_fdset (U, f); }
/* Manufacture an fdset containing the listening socket and all of the
* connected client sockets in the Tcp_Server. */
fd_set provide_fdset (Udp_Server const &);
} } /* End of namespace DMBCS::Micro_Server. */
#endif /* Undefined DMBCS__MICRO_SERVER__UDP_SERVER__H. */

181
DMBCS/micro-server/utils.cc Normal file
View file

@ -0,0 +1,181 @@
/*
* dmbcs-micro-server A C++ library providing CGI or built-in
* web server functions
*
* Copyright (C) 2018 DM Bespoke Computer Solutions Ltd
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or (at
* your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <DMBCS/micro-server/utils.h>
#include <fstream>
#include <iostream>
#include <sstream>
#include <unistd.h>
namespace DMBCS { namespace Micro_Server {
string slurp_file (ifstream& input)
{
input.seekg (0, ios::end);
string line (input.tellg (), char {});
input.seekg (0, ios::beg);
input.read (line.data (), line.length ());
return line;
}
string slurp_file_with_error (const string& file_name)
{
ifstream input {file_name};
if (! input.good ()) throw Slurp_Error {file_name};
return slurp_file (input);
}
string slurp_file (string const & file_name)
try { return slurp_file_with_error (file_name); }
catch (Slurp_Error&)
{
cerr << "SLURP FILE NOT FOUND: " << file_name << "" << endl;
return {};
}
string transform (string && in,
string const &find,
string const &replace)
{
for (auto i = string::size_type {0};
(i = in.find (find, i)) != in.npos;
i += replace.length ())
in.replace (i, find.length (), replace);
return in;
}
string htmlize_quotes (string && input)
{
return transform (move (input), "\"", "&quot;");
}
string escape_quotes (string && a)
{
return transform (transform (transform (move (a), "\\", "\\\\"),
"'", "\\\'"),
"\"", "\\\"");
}
string decode_url (string && url)
{
url = transform (move (url), "+", " ");
for (string::size_type i = 0; (i = url.find ('%', i)) != url.npos; ++i)
{
if (i < url.length () - 1 && url [i + 1] == '%')
{
url.erase (i);
}
else if (i < url.length () - 2 && isxdigit (url [i+1])
&& isxdigit (url [i+2]))
{
url [i] = (char) strtol (url.substr (i+1, 2).c_str (), 0, 16);
url.erase (i + 1, 2);
}
}
return url;
}
string strip_leading_space (string && in)
{
auto start_space = string::size_type {0};
for (;;)
{
/* Scan for first newline which is immediately followed by more white
space (this is the candidate for removal). */
while (start_space < in.length () - 1
&& (in [start_space] != '\n'
|| !isspace (in [start_space + 1])))
++ start_space;
if (start_space >= in.length () - 1)
return in;
/* Want to leave one space behind. */
auto start_text = string::size_type {start_space += 2};
while (start_text < in.length () && isspace (in [start_text]))
++ start_text;
in.erase (start_space, start_text - start_space);
}
return in;
}
string htmlize (string && entry)
{
static auto const A = vector <pair <char, char const *>>
{ {'&', "&amp;"}, {'"', "&quot;"},
/*{'%', "&#37;"},*/
{'<', "&lt;"}, {'>', "&gt;"} } ;
for (auto &i : A)
entry = transform (move (entry), string {i.first}, i.second);
return entry;
}
string hexalize (unsigned char const *const buffer,
size_t const buffer_size)
{
auto ret = string (2 * buffer_size, ' ');
for (size_t i = 0; i < buffer_size; ++i)
sprintf (ret.data () + 2 * i, "%2.2x", (unsigned int) buffer [i]);
return ret;
}
string getenv (string const & env)
{
auto const a = ::getenv (env.c_str ());
return a ? string {a} : string {};
}
} } /* End of namespace DMBCS::Micro_Server. */

View file

@ -0,0 +1,90 @@
/*
* dmbcs-micro-server A C++ library providing CGI or built-in
* web server functions
*
* Copyright (C) 2018 DM Bespoke Computer Solutions Ltd
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or (at
* your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef DMBCS__MICRO_SERVER__UTILS__H
#define DMBCS__MICRO_SERVER__UTILS__H
#include <fstream>
#include <string>
#include <vector>
namespace DMBCS { namespace Micro_Server {
using namespace std;
/* Get a string containing the entire contents of the file at file_name.
* If there is a problem reading this file then the Slurp_Error will be
* thrown. */
struct Slurp_Error : runtime_error
{ Slurp_Error () : runtime_error {"file slurp error"} {}
explicit Slurp_Error (const string& file_name)
: runtime_error {"file slurp error: " + file_name + ""} {} } ;
string slurp_file_with_error (string const &file_name);
/* Read the entire contents of the file at file_name, returning an
* empty string object if this cannot be done. */
string slurp_file (string const &file_name);
/* Read the entire contents of the file attached to the input stream,
* returning an empty string object if this cannot be done. */
string slurp_file (ifstream &file_name);
/* Replace _all_ occurences of find with replace. */
string transform (string &&, string const &find, string const &replace);
/* Replace _all_ occurences of dog-ears with the corresponding HTML
* entity. */
string htmlize_quotes (string &&);
/* Make sure the given string displays verbatim in HTML pages. */
string htmlize (string &&);
/* To remove the network wastage of sending indented HTML, this function
* replaces any spaces at the start of a line with a single space (just
* in case this is significant to the parsing of the document). */
string strip_leading_space (string &&);
/* Replace apostrophes, dog-ears and the escape back-slash with
* escaped versions. */
string escape_quotes (string &&);
/* Expand out all space-plusses and unicode markers to their correct
* character representatives. */
string decode_url (string &&);
/* Return a hex-string representation of the data values in the buffer.
* The return string will be exactly twice buffer_size long. */
string hexalize (unsigned char const *const buffer,
size_t const buffer_size);
/* Return the value of the environment variable env if possible, or else
* just return an empty string. */
string getenv (string const & env);
} } /* End of namespace DMBCS::Micro_Server. */
#endif /* Undefined DMBCS_DMBCS__MICRO_SERVER__UTILS__H. */

0
NEWS Normal file
View file

0
README Normal file
View file

69
configure.ac Normal file
View file

@ -0,0 +1,69 @@
# dmbcs-micro-server A C++ library providing CGI or built-in
# web server functions
#
# Copyright (C) 2018 DM Bespoke Computer Solutions Ltd
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or (at
# your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# Process this file with autoconf to produce a configure script.
AC_PREREQ(2.69)
AC_INIT([dmbcs-micro-server], [0.2], [http://rdmp.org/dmbcs/contact])
AM_INIT_AUTOMAKE
AM_SILENT_RULES([yes])
AC_CONFIG_SRCDIR([DMBCS/micro-server/query-string.cc])
AC_CONFIG_MACRO_DIR([m4])
# Checks for programs.
AC_PROG_LIBTOOL
AC_PROG_CXX
AC_PROG_AWK
AC_PROG_CC
AC_PROG_CPP
AC_PROG_INSTALL
AC_PROG_LN_S
AC_PROG_MAKE_SET
# Checks for libraries.
PKG_CHECK_MODULES([CURLPP], [curlpp])
AC_SEARCH_LIBS([gcry_md_open], [gcrypt])
# Configure-time program parameters.
AC_ARG_WITH([mac],
AS_HELP_STRING([--with-mac],
[The secret to create the URL MACs]),
MAC=$withval,
AC_MSG_WARN([using default MAC (use --with-mac to change)])
MAC="theuas.")
AC_SUBST([MAC])
# Checks for header files.
AC_CHECK_HEADERS([netinet/in.h sys/socket.h unistd.h])
# Checks for typedefs, structures, and compiler characteristics.
AC_CHECK_HEADER_STDBOOL
AC_C_INLINE
AC_TYPE_SIZE_T
AC_TYPE_UINT16_T
AC_TYPE_UINT8_T
# Checks for library functions.
AC_CHECK_FUNCS([select socket strtol])
AC_CONFIG_FILES([DMBCS/micro-server/makefile
makefile
dmbcs-micro-server.pc])
AC_OUTPUT

11
dmbcs-micro-server.pc.in Normal file
View file

@ -0,0 +1,11 @@
prefix=@prefix@
exec_prefix=${prefix}
libdir=${exec_prefix}/lib
includedir=${prefix}/include
Name: dmbcs-micro-server
Description: CGI C++ programming library
Version: @VERSION@
Requires: curlpp
Libs: -L${libdir} -ldmbcs-micro-server @CURLPP_LIBS@ -lgcrypt
Cflags: -I${includedir}

876
dmbcs-micro-server.texi Normal file
View file

@ -0,0 +1,876 @@
\input texinfo
@setfilename dmbcs-micro-server.info
@include version.texi
@settitle The dmbcs-micro-server Library Reference Manual
@syncodeindex tp fn
@syncodeindex pg fn
@copying
This is the reference manual for the dmbcs-micro-server C++ library
(version @value{VERSION}, @value{UPDATED}).
Copyright @copyright{} 2018 Dale Mellor
@quotation
Permission is granted to copy, distribute and/or modify this document
under the terms of the GNU General Public License as published by the
Free Software Foundation; either version 2 of the License, or (at your
option) any later version. The text of the license can be found in the
section entitled ``Copying''.
@end quotation
@end copying
@dircategory Libraries
@direntry
* dmbcs-micro-server: (dmbcs-micro-server). C++ CGI utility library.
@end direntry
@setchapternewpage odd
@titlepage
@title The DMBCS micro-server Library Reference Manual
@subtitle Version @value{VERSION}
@subtitle @value{UPDATED}
@author Dale Mellor
@page
@vskip 0pt plus 1filll
@insertcopying
@end titlepage
@node Top
@summarycontents
@contents
@page
@ifnottex
@top The micro-server Library
@insertcopying
@end ifnottex
@menu
* Introduction::
* Using the library::
* Reference::
* Examples of Use::
* Hints on how the library might be used in a real application::
@detailmenu
--- The Detailed Node Listing ---
Introduction
* HTTP Web Services::
* Our Choice of Name::
* Your Choice of Library::
* Outline of Library:: An overview of the library's implementation
Using the library
Reference
* The utility functions::
* The Query_String class::
* The Hyper_Tags class::
* The Http_Server class::
The Query_String class
* MACs::
* Constructors::
* Methods::
* Functions::
The Hyper_Tags class
* Constructors_::
* Methods_::
* Functions_::
The Http_Server class
* Constructor__::
* Functions__::
Examples of Use
* Simple example -- Online multiplier:: A simple application of the library
Hints on how the library might be used in a real application
* Web-site skins:: Web sites which provide a user-selected `look-and-feel'
* Other inheritence:: Other ideas
@end detailmenu
@end menu
@node Introduction, Using the library, Top, Top
@chapter Introduction
This document introduces, defines and exemplifies use of the
dmbcs-micro-server C++ library, providing services to applications for
processing and responding to requests from a web browser, including the
possibility of making the application a stand-alone web server in its
own right (a `micro-server').
@menu
* HTTP Web Services:: Introduction to CGI and web servers
* Our Choice of Name:: Use of `DMBCS'
* Your Choice of Library:: Some gentle advice
* Outline of Library:: An overview of the library's implementation
@end menu
@node HTTP Web Services, Our Choice of Name, Introduction, Introduction
@section HTTP Web Services
A CGI program is a program, written in any language, which conforms to
the Common Gateway Interface (CGI). It is a program executed by a web
server (on the server machine), and is passed information about the
request from the user through the UNIX environment. It is left to the
program to interpret the user's requirements and to produce on its
standard output channel a HTML document which the web server will
relay back to the user's browser for display on his console.
CGI programs are thus very flexible and amenable to lots of jobs.
Some of them are very small, amounting to only a couple of lines of an
interpreted language to slurp a file from the local storage and send
it straight back to the browser (which is essentially what a web
server would normally do but this allows the programmer to do so in a
more customized way). On the other hand, some CGI programs are
extremely large consisting of hundreds of thousands of lines of code
which may interact with a database and other third-party systems (such
as payment gateways) and do some real work on behalf of the user,
before sending the results of the transactions along for the user's
perusal.
This project came from a CGI program of the latter description. Because
of its size, complexity and the anticipated load (number of users), it
was decided very early on that it would be written in a proper
programming language which could be compiled optimized to run on the
bare metal of the available computer system, whatever that is. Thus it
would have to be C or C++. While the original implementation had been
in C for political reasons, it has since been ported to the more
convenient C++. As a side-effect of this development, the parts of the
system which deal specifically with the business of being a CGI program
have been abstracted and encapsulated, and then they were syphoned off
into their own separate library; this was further extended to provide
the functions of a self-contained web server. This is now being offered
for general use.
The result is a library which can be used to inject a self-contained web
server into an arbitrary C++ code base, opening up the interface to such
a legacy program to the modern world, without needing to also build the
infrastructure to serve such a program to the public (although in
practice infrastructure needs to be built for other reasons---like
security and virtualization---but the self-contained scheme allows for
greater flexibility and better functional isolation).
@node Our Choice of Name, Your Choice of Library, HTTP Web Services, Introduction
@section Our Choice of Name
The micro-server library was written from scratch since 2004 as part of
a large internet project. The requirement for a C++ framework for
undertaking this task is very common, and thus it is no surprise that
other efforts already exist, notably the GNU cgicc project.
Some consideration was given to the name of this project. At first it
was going to be called cgicc2, but that carries the implication that
it somehow carries on from the GNU cgicc project, and that it is both
a development and enhancement of it. This is definitely not the case,
but a parallel development, completely different but solving the same
problems. Unfortunately appending any number or even letter carries
some implication of sequencing, and if the two projects continue (as
they should) to be developed then some imbalance would occur in the
rivalry. Such is definitely not the intention, but for the two
projects to be seen as separate. Choice of one or other framework for
any particular project should be based on what the framework offers,
not what letters happen to come in its name.
Thus it is decided to mark this particular library with the string
'DMBCS': all code appears inside the namespace 'DMBCS::Micro_Server' and
the include file is called 'dmbcs-micro-server.h'; the project is
formally known as the 'dmbcs-micro-server' C++ library.
@node Your Choice of Library, Outline of Library, Our Choice of Name, Introduction
@section Your Choice of Library
We offer only this advice: look at the API provided by all the available
libraries, and consider the nature of the working framework (paradigm)
that that implies; choose the library that seems most appropriate for
your project: the one that you will feel most comfortable working with.
@node Outline of Library, , Your Choice of Library, Introduction
@section Outline of Library
The library consists of a high-level wrapper and two workhorse classes.
@code{Http_Server} interfaces with multiple clients and delegates
processing of their requests to registered callback functions, which
make direct use of the following workhorses. @code{Query_String} looks
after the part of the CGI interface which passes from the browser to the
CGI program and provides methods specifically for interpreting the
arguments from the browser's query string, and @code{Hyper_Tags} looks
after the part of the CGI interface which passes back from the CGI
program to the browser, and displays the results of the user's request;
this deals with updating template HTML files with some dynamic content
before passing the result to the web server for eventual transmission to
the browser.
@node Using the library
@chapter Using the library
To re-iterate: all code in this library comes in the namespace
'DMBCS::Micro_Server' and the header which introduces all of the
functionality is 'dmbcs-micro-server.h'.
A project would generally consist of a template HTML file with markers
where dynamically-generated content needs to be placed, a .css file
which should be injected into the file or provided by the server as an
inclusion target in the HTML, a .js file containing code that should run
client-side to implement part of the functionality of the web site, and
a (micro-) web server which runs continually, listens for client
connections, and provides generated HTML files and JSON data strings on
demand of the client.
@menu
@end menu
@node Reference, Examples of Use, Using the library, Top
@chapter Reference
@menu
* The utility functions:: Useful functions needed by web applications
* The Query_String class:: For processing the user's arguments
* The Hyper_Tags class:: For substituting dynamic content into HTML
* The Http_Server class:: To implement a micro-server in your own app
@end menu
@node The utility functions, The Query_String class, Reference, Reference
@section The utility functions
Introductory text here.
@deftypefun string slurp_file_with_error (const std::string &@var{file_name}) throw (Slurp_Error)
This function will attempt to open the file named file_name (which may
be a complete path to the file), and read the whole lot into a string.
If any errors occur in the process, a Slurp_Error object will be thrown
(these exception objects do not convey any information back to the
application).
@end deftypefun
@deftypefun string slurp_file (const std::string &@var{file_name})
This function will attempt to open file @var{file_name} (which may be a
full path), and will slurp the entire contents (which are presumably a
hyper-tag'd template file) into a string. If any errors occur, an empty
string will be produced, and a message printed on the standard error
channel to indicate the name of the file which has failed.
In the majority of applications of this function, in the majority of
calls, the error condition thrown from the function above will not
happen; it would be regarded as an infrastructure build problem rather
than a run-time error if a known file were not accessible. Thus it is
wasteful to worry about and deal with the error at global level--outside
the library--, rather let the program carry on and provide a message to
the user's console about the missing file (the fact that the problem
occurred will be obvious!) Once it is known which file has failed, the
remedy will be equally obvious.
@end deftypefun
@deftypefun string htmlize_quotes (string && @var{input})
Any occurrence of `` in @var{input} will be replaced by `@code{&quot};'
@end deftypefun
@deftypefun string htmlize (string && @var{input})
All occurrences of HTML-special characters (<>&") will be replaced with
entity representations.
The main purpose of this method is to alleviate the possibility of
injection attacks into a web-site; if any part of a page is generated
from user-supplied input there is the danger that the user might be
savvy enough to get JavaScript to execute there! But the function is
quite generally useful when it is known that a string needs to appear
verbatim in a web page, regardless of its contents, and thus ought to be
used whenever a string is added to a set of @code{Hyper_Tag}s.
@end deftypefun
@deftypefun string strip_leading_space (string && @var{input})
Replace all sequences of white space at the start of all lines with a
single space character.
HTML recognizes all sequences of white space outside of quotes as a
single space separator. Thus sequences of white space at the
beginning of lines (which are used by web programmers to give a nice
aesthetic indentation) are a waste of bandwidth. This function will
remove all such occurrences from @var{input}.
@end deftypefun
@deftypefun string escape_quotes (string @var{input})
Replace all occurrences of quotation marks and escapes with escaped
versions, where the escape character is the back-slash.
This function will put an escape ('\') in front of ''', '''', or '\'
which occur in @var{input}, and is useful (essential when dealing with
user-supplied input) to ensure that strings do not mess up SQL
statements.
@end deftypefun
@deftypefun string decode_url (string @var{url})
This function will replace '+' with ' ', and any %-escaped character
codes in the @var{url} with the ASCII equivalent.
This will produce a plain string, decoded from strings passed from forms
or EcmaScript on web pages passed through parameter lists in URI's, as
provided within the context of this library by the @code{Query_String}
object.
@end deftypefun
@deftypefun string hexalize (unsigned char const *const @var{buffer}, size_t const @var{buffer_size})
Return an ASCII string containing the hexadecimal representation of the
octets in the given @var{buffer}. The @var{buffer_size} is the number of
octets to decode.
@end deftypefun
@deftypefun string getenv (string const & @var{env})
Return the value of the environment variable called @var{env}, or an
empty string if the variable does not exist in the environment.
This is a convenience wrapper around the system @code{getenv} call,
adapting it to the use of strings.
@end deftypefun
@node The Query_String class
@section The Query_String class
A Query_String object is a std::map<std::string, std::string> object
in which the keys are the variables passed in the URL query string
part and the values are the values from that query string. Methods
are defined which interrogate the class in details more specific to
the CGI problem, and allow for values to be extracted in either string
or a numeric form, as expected by the CGI program.
@menu
* MACs::
* Constructors::
* Methods::
* Functions::
@end menu
@node MACs, Constructors, The Query_String class, The Query_String class
@subsection MACs
@node Constructors
@subsection Constructors
@deftp {Constructor} Query_String ()
The null constructor, only useful if you are intending to build up a
new query string for eventual inclusion into some part of a HTML page.
@end deftp
@deftp {Constructor} Query_String (const char *const *const @var{env})
The real workhorse constructor. The argument @var{env} should be the
environment variable passed to the CGI's main routine as the third
argument. This will create an object which represents the information
passed to the CGI program via the query string, whether it were sent
via the GET or the PUT protocols. The object returned can
subsequently be used to interrogate the user's request string.
@end deftp
@node Methods
@subsection Methods
@deftp {Method} void Query_String::add (const std::string &@var{name}, const T &@var{value})
This method will add an entry into the Query_String with key
@var{name} and value a string representation of @var{value}. The type
T of @var{value} may be anything that can be passed into a
std::ostream object via the << operator.
@end deftp
@deftp {Method} void Query_String::add (const std::string &@var{name}, const Query_String &@var{input}, const T &@var{fallback})
This method will add an entry into the current Query_String which is a
copy of the entry in the @var{input} Query_String, unless such entry
does not exist in which case a key @var{name} with value a std::string
representation of @var{fallback} will be added to the current
Query_String. The type T of @var{fallback} may be anything that can
be passed to the << operator of a std::ostream object.
@end deftp
@deftp {Method} bool Query_String::posted () const
Returns TRUE if the query string had been sent to the CGI program via
the POST protocol, FALSE otherwise (which case would presumably have
been caused by use of the GET protocol).
@end deftp
@deftp {Method} T Query_String::get (const std::string &name, const T &fallback) const
This gets the value associated with the key @var{name} from the query
string, unless no such key was present in which case @var{fallback} is
returned. Note that the type of value returned is the same as that of
@var{fallback}, thus @var{fallback} serves the dual role of defining
the fallback value and determining the return type of the function.
@end deftp
@deftp {Method} std::vector <int> Query_String::get_list (const std::string &name) const
In the case that a query string value corresponds to a multiple choice
selection on the HTML form, it will be a comma-separated string of
selected values. This function will return a vector comprised of
these values, interpreted as integers. If there is no key @var{name}
in the query string, then the returned vector will have no elements
(zero length).
@end deftp
@deftp {Method} std::vector <int> Query_String::get_checked_array (const std::string &NAME) const
@end deftp
@deftp {Method} bool Query_String::has (const std::string &NAME) const
This method returns TRUE if the @var{name} was a variable present in
the query string, FALSE otherwise.
@end deftp
@deftp {Method} bool Query_String::mac_verified () const
This method will return TRUE only if there was a MAC present in the
query string, and the MAC was valid. Otherwise FALSE will be
returned.
@end deftp
@node Functions
@subsection Functions
@deftypefun static string Query_String::add_mac (string @var{input})
This static class member will add a cryptographic MAC onto any string,
but this is only meaningful for composed query strings.
@end deftypefun
@node The Hyper_Tags class
@section The Hyper_Tags class
Whereas a HTML document contains markup tags expressed as labels between
angle brackets (possibly in pairs), so a micro-server template file
contains dynamic content holders expressed as labels between square
brackets. The Hyper_Tags class contains methods for replacing single
tags with some dynamically-generated content, and for selectively
keeping or removing sections of the template bracketed between pairs of
hyper tags.
A Hyper_Tags object should be considered a container of tag
definitions. The CGI program will typically build up a list of such
definitions in the container, until all the work is done when the
template file is read in from local storage and the Hyper_Tags object
is instructed to make all the changes in one go before sending the
result to the server and hence to the user's browser.
@menu
* Constructors_::
* Methods_::
* Functions_::
@end menu
@node Constructors_, , The Hyper_Tags class, The Hyper_Tags class
@subsection Constructors_
@deftp {Constructor} Hyper_Tags ()
The class only has the one, default, constructor, which returns an
empty object waiting to be filled with tag definitions.
@end deftp
@node Methods_, , Constructors_, The Hyper_Tags class
@subsection Methods_
@deftp {Method} void Hyper_Tags::harden_last ()
Normally, a later tag definition would overrule any earlier ones when
substitutions are finally made into a template file. However, if it
is desired that the last tag added should be the final tag definition
on substitution, then it can be `hardened' by calling this method.
@end deftp
@deftp {Method} bool Hyper_Tags::has (const std::string &@var{name}) const
This method returns TRUE if a tag with the given @var{name} has been
registered with the Hyper_Tags object. In all other cases it returns FALSE.
@end deftp
@deftp {Method} Hyper_Tags &Hyper_Tags::add (const Hyper_Tags &@var{registry}, const int @var{harden} = 0)
This method copies all the tag definitions contained in @var{registry}
into the current object, and returns a reference to the current
object. If the optional @var{harden} argument is passed a non-zero
value, then all the incumbent tag definitions will be hardened, as per
the discussion of the harden_last () method above.
@end deftp
@deftp {Method} Hyper_Tags &Hyper_Tags::add (const std::string &@var{name}, string @var{substitution_text})
One of the main methods of the class. Declares that, in the final
substitutions, any parts of the template file which look like
``[@var{name}/]'' should be replaced with @var{substitution_text}.
The return is a reference to the current object.
@end deftp
@deftp {Method} Hyper_Tags &Hyper_Tags::add (const std::string &@var{name}, const int value)
Declares that tags called @var{name} in the template file be replaced
with a string representing the integer @var{value}. The return is a
reference to the current object.
@end deftp
@deftp {Method} Hyper_Tags &Hyper_Tags::add (const std::string &@var{name}, const double @var{value}, const int @var{precision} = 4)
Declares that tags called @var{name} be replaced with a string
representing the floating-point @var{value}, with at least
@var{precision} significant figures. The return is a reference to the
current object.
@end deftp
@deftp {Method} Hyper_Tags &add (const std::string &@var{name}, Query_String &@var{q_s}, const T &@var{fallback})
Declares that tags called @var{name} be replaced with a value from
@var{q_s} with the same name if this exists, or with @var{fallback}
otherwise. The type T of fallback should be one of std::string, int,
or double. The return is a reference to the current object.
@end deftp
@deftp {Method} void add_int_list (const std::string &@var{name}, I @var{begin}, I @var{end})
Here, I is an iterator type over an STL container of int's. This
method declares that tags named @var{name} be substituted with a
comma-separated list of integer values.
@end deftp
@deftp {Method} void add_date (const std::string &@var{name}, const time_t &@var{time}, const std::string &@var{format})
Specifies that tags named @var{name} be replaced with a string
representing the date indicated by the UNIX @var{time} object. The
conversion takes place according to @var{format}, which is described
in the strftime () libc manual.
@end deftp
@deftp {Method} void add_file (const std::string &@var{name}, const std::string &@var{file_name})
Specifies that tags named @var{name} in the template HTML file be
replaced with the entire contents of the file called @var{file_name}
in the file system.
@end deftp
@deftp{Method} void add_passthrough_tag (const Query_String &@var{query_string}, ...)
The trailing arguments are a list of const char *const, terminated
with a NULL pointer. This method specifies that contents of the HTML
template file which look like ``[pass-through/]'' be replaced with a
set of lines which look like ``<input type=``hidden'' name=``name''
value=``value''/>'' where the @var{names} are taken from the list of
arguments to the function, and the @var{__values__} are taken from the
@var{query_string} variables with the same names. Thus, if the tag in
the template file is contained within ``<form>'' markup elements, the
current values in the query string will be propagated into any query
string that the new form produces.
@end deftp
@deftp{Type} enum Hyper_Tags::Section_Keep @{ KEEP_SECTION, LOSE_SECTION @}
@end deftp
@deftp{Method} Hyper_Tags &section (const std::string &@var{name}, const Section_Keep &@var{keep})
This method declares the action to be taken when a pair of tags like
``[@var{name}]'' and ``[/@var{name}]'' are seen in a template file.
If @var{keep} is KEEP_SECTION, then the tags are simply removed from
the file leaving the contents between the tags intact. If @var{keep}
is LOSE_SECTION then the tags and all the text between them is removed
from the file.
@end deftp
@deftp{Method} string Hyper_Tags::substitute (string @var{html_template}, int @var{repeat} = 1)
One of the main methods of the class. This will go through
@var{html_template} and replace any hyper tags which have been
registered with the substitution text that has been declared as the
replacement. If a tag is seen in the @var{html_template} which has
not been registered with the current object, then this tag will remain
intact in the template.
Additionally, any pairs of tags seen in @var{html_template} which have
been registered as a section will be removed, possibly removing also
all the text between the tags, according to the registered behaviour.
@end deftp
@node Functions_, , Methods_, The Hyper_Tags class
@subsection Functions_
@deftypefun static string Hyper_Tags::strip_loose_tags (string @var{input})
It may happen that, after processing, a HTML template string still
contains some hyper tags which have not been substituted. This static
function will injudiciously remove any such tags, thus eliminating any
ugliness from the page sent back to the user.
@end deftypefun
@node The Http_Server class, , The Hyper_Tags class, Reference
@section The Http_Server class
@menu
* Constructor__::
* Functions__::
@end menu
@node Constructor__, Functions__, The Http_Server class, The Http_Server class
@subsection Constructor__
@deftypefun Http_Server::Http_Server (const int @var{socket}, const
std::map<std::string, std::function<void (Query_String, int)> @var{methods})
Create a HTTP server to listen on the @var{socket}. Processing for
requests will be delegated to the @var{methods}, referenced by the
HTTP-call path name via the @code{map}. When called, the @var{methods}
will be passed a @code{Query_String} object and a socket on which a
response must be sent; methods are provided in the Http_Server namespace
to facilitate with this.
Please see the example application to see how this is used in practice.
@end deftypefun
@node Functions__, , Constructor__, The Http_Server class
@subsection Functions__
@deftypefun bool tick (Http_Server &@var{server}, const long @var{wait_us})
Cause the @var{server} to act on a single instruction from a web client,
waiting up to @var{wait_us} micro-seconds for such a request to appear.
The return is usually @code{TRUE}, but may be @code{FALSE} if the
listening socket goes away (this is a mechanism that could be used by a
request processing function to terminate the server frow within).
In practice this method is called repeatedly, @emph{ad infinitum}.
@end deftypefun
@deftypefun void Http_Server::return_html (const int @var{socket},
const std::string &@var{html})
This method sends the @var{html} to the socket, dressed up as a valid
response to a HTTP request for data.
@end deftypefun
@deftypefun void Http_Server::return_text (const int @var{socket},
const std::string &@var{text})
This method sends the @var{text} to the socket, dressed up as a valid
response to a HTTP request for data.
@end deftypefun
@deftypefun void Http_Server::return_pdf (const int @var{socket},
const std::string &@var{pdf})
This method sends the @var{pdf} to the socket, dressed up as a valid
response to a HTTP request for data. The `string' is actually
understood as a buffer of arbitrary octects here.
@end deftypefun
@deftypefun void Http_Server::return_json (const int @var{socket},
const std::string &@var{json})
This method sends the @var{json} to the socket, dressed up as a valid
response to a HTTP request for data.
@end deftypefun
@deftypefun void Http_Server::return_bad (const int @var{socket})
This method sends an empty payload to the socket, dressed up as a
@emph{valid} response to a HTTP request for data (it would generally be
taken by an Ecmascript application to be a `bad' response to a request
for data).
@end deftypefun
@deftypefun void Http_Server::return_png (const int @var{socket},
const std::string &@var{png})
This method sends the @var{png} to the socket, dressed up as a valid
response to a HTTP request for data. The `string' is actually
understood as a buffer of arbitrary octects here.
@end deftypefun
@deftypefun void Http_Server::return_svg (const int @var{socket},
const std::string &@var{svg})
This method sends the @var{svg} to the socket, dressed up as a valid
response to a HTTP request for data. The `string' is actually
understood as a buffer of arbitrary octects here.
@end deftypefun
@node Examples of Use, Hints on how the library might be used in a real application, Reference, Top
@chapter Examples of Use
We don't start with the tradition hello, world program as this would
not demonstrate in any way the dynamic nature of CGI programming! The
simplest example we can think of allows the user to input two numbers
and returns the product of them, and the full code for this is
provided in the next section.
@menu
* Simple example -- Online multiplier:: A simple application of the library
@end menu
@node Simple example -- Online multiplier
@section Simple example -- Online multiplier
The following is provided as the simplest code to demonstrate use of the
dmbcs-micro-server library, not to inform of any coding style or quality
system approach. We assume a standard GNU system with recent
@code{make}, @code{bash}, @code{gcc}, etc.
Start with the HTML file @code{calc.html}
@verbatim
<html>
<head><title>Multiplier</title></head>
<body><h1>Multiplier</h1>
<form action="compute" method="GET" id="calc">
<input type="text" id="arg_1"/> x <input type="text" id="arg_2"/>
= <input type="text" id="result">[result/]</input>
<br>
<input type="submit">CALCULATE</input>
</form>
</body>
</html>
@end verbatim
And the C++ source file @code{calc.cc}
@verbatim
#include <dmbcs-micro-server.h>
using namespace DMBCS::Micro_Server;
/* This function both serves up the basic HTML page, and
* performs the multiplication and injects the result into the
* HTML. */
void home_page (Query_String const &query, int const socket)
{
auto tags = Hyper_Tags {};
add (tags,
"result",
query.get ("arg_1", 0) * query.get ("arg_2", 0));
Http_Server::return_html (substitute (tags,
slurp_file ("calc.html")));
}
int main ()
{
auto server = Http_Server {2022,
{ {"", home_page},
{"compute", home_page} }};
for (;;) tick (server, 1000000);
return 0;
}
@end verbatim
then the @code{makefile}
@verbatim
CXXFLAGS = `pkg-config --cflags dmbcs-micro-server`
LDFLAGS = `pkg-config --libs dmbcs-micro-server`
@end verbatim
Then at the command line type
@verbatim
make calc
./calc
@end verbatim
then point a browser at @code{http://localhost:2022/} and use the simple
calculator (don't try to do anything funny: the code has been kept
deliberately simple and doesn't do any error checking). Note that an
answer can be obtained directly with a URL like
@code{http://localhost:2022/compute?arg_1=3&arg_2=4}.
@node Hints on how the library might be used in a real application, , Examples of Use, Top
@chapter Hints on how the library might be used in a real application
While the basic library as it is serves perfectly well for small CGI
programs, the library was derived from a larger project. From this
experience, it is seen that many large projects will want to produce
specializations of the classes and utility functions provided by the
library for the particular project. The following are some specific
details about how the library was enhanced in the original project.
@menu
* Web-site skins:: Web sites which provide a user-selected `look-and-feel'
* Other inheritence:: Other ideas
@end menu
@node Web-site skins, Other inheritence, Hints on how the library might be used in a real application, Hints on how the library might be used in a real application
@section Web-site skins
The underlying paradigm promoted by the library is one where the
programmer supplies template HTML files which the CGI program
populates with extra dynamic content. This opens the possibility of
supplying the same dynamic content into different templates, for
example where a web site allows the user to choose a favorite `skin',
or `look-and-feel', then each template file would exist in different
forms to provide all the different skins available. In this case, the
library can be extended by ensuring that whenever a template is
slurped from local storage for use, it is the template appropriate for
the user's choice of skin that is pulled up.
In the original project, this was achieved by placing all templates
for a skin into a single directory to which the CGI program has access
(it was actually a sub-directory underneath the cgi-bin directory, and
the web server was configured to *not* allow direct access from a
browser to these files).
The main modification was to add a new utility function in the
project's namespace
@code{string slurp_file (const std::string &skin,
const std::string &filename)}
which is called by the program in lieu of slurp_file, and
ensures that the file from the correct skin directory is chosen,
calling through to the library function to get the eventual work
done. To complete the effect, it is also necessary to derive a new
class from Hyper_Tags with the following additional methods
@code{blah, blah, blah}
(The Query_String class works completely as is; it may be
convenient to introduce this into the projects own namespace with a
@code{typedef Query_String Query_String;}
line in a project header file.)
@node Other inheritence, , Web-site skins, Hints on how the library might be used in a real application
@section Other inheritence
@bye

36
makefile.am Normal file
View file

@ -0,0 +1,36 @@
# dmbcs-micro-server A C++ library providing CGI or built-in
# web server functions
#
# Copyright (C) 2018 DM Bespoke Computer Solutions Ltd
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or (at
# your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
SUBDIRS = DMBCS/micro-server
ACLOCAL_AMFLAGS = -I m4
pkgconfigdir = ${prefix}/lib/pkgconfig
pkgconfig_DATA = dmbcs-micro-server.pc
AM_MAKEINFOHTMLFLAGS = --no-split --css-ref=dmbcs-info.css
info_TEXINFOS = dmbcs-micro-server.texi
MAINTAINERCLEANFILES = aclocal.m4 compile config.guess config.sub configure \
dmbcs-micro-server.html \
depcomp install-sh ltmain.sh makefile.in \
missing texinfo.tex mdate-sh