1--- 2title: 'Simple-Web-Server: a fast and flexible HTTP/1.1 C++ client and server library' 3tags: 4 - C++ 5 - web 6 - http 7 - client 8 - server 9 - library 10 - asio 11authors: 12 - name: Ole Christian Eidheim 13 orcid: 0000-0001-5355-6326 14 affiliation: 1 15affiliations: 16 - name: Department of Computer Science, Norwegian University of Science and Technology 17 index: 1 18date: 18 July 2019 19bibliography: paper.bib 20--- 21 22# Summary 23 24The programming language C++ is commonly used for resource intensive tasks. 25Simple-Web-Server is a library that can be utilized in C++ applications to 26implement web-resources or perform HTTP or HTTPS requests in a simple manner 27across OS platforms compared to using a networking library directly. Thus, 28Simple-Web-Server can be helpful for any research software written in C++ that 29needs to communicate with remote endpoints through HTTP or HTTPS. 30 31The main features, apart from speed and ease of use, are flexibility and safety. 32The asynchronous I/O library Asio C++ Library [@asio] is used to implement 33networking and asynchronous event handling. The sending of outgoing messages has 34been made thread safe, and event handling in one or several threads is 35supported. The default event handling strategy is using one thread, commonly 36called event-loop, which makes accessing shared resources safe without using 37resource locking through for instance mutexes. Although, accessing shared 38resources in a multithreaded event-handling strategy can be made safer by 39utilizing the annotation offered in Clang Thread Safety Analysis 40[@clang_thread_safety]. In some cases, however, processing requests 41sequentially, in an event-loop scheme, can be faster than processing the 42requests in several threads where shared resources must be protected from 43simultaneous use. 44 45An additional safety feature is stopping of asynchronous handlers when the 46associated client or server object has been destroyed. An atomic instruction 47based class, ScopeRunner, was implemented to achieve this since reader-writer 48locks proved more resource intensive for this specific task. In detail, a 49ScopeRunner object has an internal atomic counter that is increased when an 50asynchronous handler is run. At the end of the handler, the counter is 51decreased. When the destructor of a client or server object is called, the 52ScopeRunner object delays the destructor until its internal counter is 0, then 53sets the counter to a negative value. Finally, when the internal counter is 54negative, the handlers are returned from instead of potentially calling methods 55or using member variables of a destroyed client or server object. 56 57Compared to using a low-level network library, specialized for a specific task, 58a slight performance overhead is expected when using the more generalized 59Simple-Web-Server library. The various utility and safety features, and code 60abstractions contribute to this overhead, but a good balance between safety, 61usability and speed is continuously sought during development of this library. 62Regular expressions can for instance be used to define which functions to be 63called for specific request paths. This can be convenient for the library user, 64but a more specific algorithm can be more efficient than using regular 65expressions. 66 67The Asio C++ Library [@asio] is currently proposed to the C++ standard library 68[@wakely]. If accepted in one of the future revisions of the C++ programming 69language, C++ applications can make use of a standardized event handling system. 70Until then, efforts are made to support old and new versions of the Asio C++ 71Library, as well as both the standalone and Boost variants of the library. 72 73Simple-Web-Server is used in teaching at the Norwegian University of Science and 74Technology, and used in many external projects, for instance in the 75multi-purpose emulation framework MAME [@mame]. The library was also used in the 76senior thesis by Chung and Callin [@chung]. Furthermore, one of the motivations 77for the Simple-Web-Server project was to create a HTTP/1.1 library that was 78relatively easy to modify and extend to suit a specific need, which could also 79be positive with regards to source code contributions to the project. 80 81There are several alternatives to Simple-Web-Server. Most notably Boost.Beast 82[@beast], but this library is made for library authors and is thus harder to 83utilize in a C++ application. Additionally, Boost.Beast does not support 84standalone Asio. Another alternative is H2O [@h20] that supports several event 85handling systems, however, Asio is not yet supported. Both Boost.Beast, and to a 86lesser degree H2O, supports the WebSocket protocol [@websocket_protocol]. In the 87case of Simple-Web-Server, WebSocket is supported through a related external 88project named Simple-WebSocket-Server [@simple_websocket_server]. 89 90Based on Simple-Web-Server, a new C++ library supporting HTTP/2 is under 91development. HTTP/2 is very different from HTTP/1.1, but the experiences from 92developing Simple-Web-Server, and some its implementations, such as the 93ScopeRunner class, can be helpful when writing an HTTP/2 library. 94 95# Acknowledgments 96 97I would like to thank all those who have contributed to the Simple-Web-Server 98project. 99 100# References 101