cinatra发布新版本

栏目: Html5 · 发布时间: 5年前

内容简介:这次的版本具体有这些更新:1.简化文件上传接口的使用,使用示例:

cinatra 本次更新主要侧重于简化接口使用,增加一些功能,修复一下bug。这仍然是一个预发布版本,但强烈推荐大家试用。

这次的版本具体有这些更新:

1.简化文件上传接口的使用,使用示例:

    //http upload(multipart)
    server.set_http_handler<GET, POST>("/upload_multipart", [](request& req, response& res) {
        assert(req.get_content_type() == content_type::multipart);
        auto text = req.get_query_value("text");
        std::cout<<text<<std::endl;
        auto& files = req.get_upload_files();
        for (auto& file : files) {
            std::cout << file.get_file_path() << " " << file.get_file_size() << std::endl;
        }
 
        res.set_status_and_content(status_type::ok, "multipart finished");
    });
 
    //http upload(octet-stream)
    server.set_http_handler<GET, POST>("/upload_octet_stream", [](request& req, response& res) {
        assert(req.get_content_type() == content_type::octet_stream);
        auto& files = req.get_upload_files();
        for (auto& file : files) {
            std::cout << file.get_file_path() << " " << file.get_file_size() << std::endl;
        }
 
        res.set_status_and_content(status_type::ok, "octet-stream finished");
    });

非常简洁,用户直接获取上传的文件即可,框架负责处理文件上传的细节。

2.简化文件下载功能

框架内置chunked文件下载功能,只要输入文件路径就可以实现下载:

http://127.0.0.1:8080/assets/show.jpg

更棒的是文件下载支持断点续传,这对大文件下载很方便。

3.简化websocket接口的使用

    server.set_http_handler<GET, POST>("/ws", [](request& req, response& res) {
        assert(req.get_content_type() == content_type::websocket);
 
        req.on(ws_open, [](request& req){
            std::cout << "websocket start" << std::endl;
        });
 
        req.on(ws_message, [](request& req) {
            auto part_data = req.get_part_data();
            //echo
            std::string str = std::string(part_data.data(), part_data.length());
            req.get_conn()->send_ws_string(std::move(str));
            std::cout << part_data.data() << std::endl;
        });
 
        req.on(ws_close, [](request& req) {
            std::cout << "websocket close" << std::endl;
        });
 
        req.on(ws_error, [](request& req) {
            std::cout << "websocket error" << std::endl;
        });
    });

用户只需要在各自的事件响应函数里写逻辑即可,非常方便。

4.提供了一些更便利的render html接口

    server.set_http_handler<GET, POST>("/string", [](request& req, response& res) {
        res.render_string("OK");
    },enable_cache{false});
 
    server.set_http_handler<GET, POST>("/404", [](request& req, response& res) {
        res.render_404();
    },enable_cache{false});
 
    server.set_http_handler<GET, POST>("/404_custom", [](request& req, response& res) {
        res.render_404("./404.html");
    },enable_cache{false});

5.修复了一些bug

  1. 修复了和nginx结合的时候cookie的一个bug
  2. 修复了multipart同时有文件和键值对时忽略了键值对的bug;
  3. 修复了multipart传多个文件时丢失header的bug;
  4. 修复了url请求不支持中文的bug;

欢迎大家试用并提出宝贵的建议和意见,让cinatra越来越完善.

Hope you can enjoy cinatra!

Post Views: 47


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

The Smashing Book

The Smashing Book

Jacob Gube、Dmitry Fadeev、Chris Spooner、Darius A Monsef IV、Alessandro Cattaneo、Steven Snell、David Leggett、Andrew Maier、Kayla Knight、Yves Peters、René Schmidt、Smashing Magazine editorial team、Vitaly Friedman、Sven Lennartz / 2009 / $ 29.90 / € 23.90

The Smashing Book is a printed book about best practices in modern Web design. The book shares technical tips and best practices on coding, usability and optimization and explores how to create succes......一起来看看 《The Smashing Book》 这本书的介绍吧!

RGB CMYK 转换工具
RGB CMYK 转换工具

RGB CMYK 互转工具

HSV CMYK 转换工具
HSV CMYK 转换工具

HSV CMYK互换工具