项目作者: neel

项目描述 :
C++ HTTP server library based on boost beast
高级语言: C++
项目地址: git://github.com/neel/udho.git
创建时间: 2019-09-30T15:53:19Z
项目社区:https://github.com/neel/udho

开源协议:BSD 2-Clause "Simplified" License

下载


Udho (উধো)

udho is a tiny http library based on Boost.Beast.

Boost.Beast is based on Boost.Asio which provides low level asynchronous I/O. udho was originally created to add HTTP functionality to an existing application that is using Boost.Asio.

License
pipeline status develop
pipeline status master
Codacy Badge
Total alerts
Language grade: C/C++

  1. std::string world(udho::contexts::stateless ctx){
  2. return "{'planet': 'Earth'}";
  3. }
  4. std::string planet(udho::contexts::stateless ctx, std::string name){
  5. return "Hello "+name;
  6. }
  7. int main(){
  8. boost::asio::io_service io;
  9. udho::servers::ostreamed::stateless server(io, std::cout);
  10. server[udho::configs::server::document_root] = "/path/to/static/files";
  11. auto urls = udho::router() | "/world" >> udho::get(&world).json()
  12. | "/planet/(\\w+)" >> udho::get(&planet).plain();
  13. server.serve(urls, 9198);
  14. io.run();
  15. return 0;
  16. }

Dependencies:

boost depend on boost-beast library. As boost-beast is only available on boost >= 1.66, udho requires a boost version at least 1.66. udho may optionally use ICU library for unicode regex functionality. In that case ICU library may be required.

  • boost > 1.66
  • pugixml
  • icu [optional]

Features:

  • regular expression based url routing to callables (functions / function objects)
  • compile time binding of routing rule with callables
  • compile time travarsable url router
  • response mime type specification in routing rule
  • any default constructible can be used as callable argument type that can be parsed from std::string
  • any ostreamable can be used as return type of callables
  • automatic type coersion for url based method calling
  • throwable http error messages
  • serving static content from disk document root if no rule matched
  • urlencoded/multipart form parsing
  • on memory session for stateful web applications (no globals) and no session for stateless applications
  • strictly typed on memory session storage
  • compile time pluggable logging
  • customizable logging
  • typesafe configuration

Example

  1. std::string login(udho::contexts::stateful<user> ctx){ /// < strictly typed stateful context
  2. const static username = "derp";
  3. const static password = "derp123";
  4. if(ctx.session().exists<user>()){
  5. user data;
  6. ctx.session() >> data; /// < extract session data
  7. return "already logged in";
  8. }else{
  9. if(ctx.form().has("user") && ctx.form().has("pass")){
  10. std::string usr = ctx.form().field<std::string>("user"); /// < form field value from post request
  11. std::string psw = ctx.form().field<std::string>("pass"); /// < form field value from post request
  12. if(usr == username && psw == password){
  13. ctx.session() << user(usr); /// < put data in session
  14. return "successful";
  15. }
  16. }
  17. }
  18. return "failed";
  19. }
  20. std::string echo(udho::contexts::stateful<user> ctx, int num){
  21. if(ctx.session().exists<user>()){
  22. user data;
  23. ctx.session() >> data;
  24. return boost::format("{'name': '%1%', 'num': %2%}") % data.name % num;
  25. }
  26. return "{}";
  27. }
  28. int main(){
  29. boost::asio::io_service io;
  30. udho::servers::ostreamed::stateful<user> server(io, std::cout);
  31. server[udho::configs::server::document_root] = "/path/to/static/files";
  32. auto router = udho::router()
  33. | (udho::post(&login).plain() = "^/login$")
  34. | (udho::get(&echo).json() = "^/echo/(\\d+)$");
  35. server.serve(router, 9198);
  36. io.run();
  37. return 0;
  38. }