Title:cpp-netlib笔记三-Cookie支持测试

Author:Kagula

Date:2017-06-02

Environment:

[1]Boost 1.64

本机安装目录 D:\SDK\boost_1_64_0

[2]Windows10、VS2017 Update2

[3]OpenCL 1.0.2k本机安装目录

D:\SDK\OpenSSL_1_0_2k_vc2007

[4]CMake 3.8.1

[5]cpp-netlib-0.12.0-final

本机安装目录 D:\SDK\cpp-netlib-0.12.0-final

[6]Python 3.6.1

[7]ActivePerl 5.16

[8]IE11、Chrome 58.0.3029.110 、Postman

Introduction: 

测试cpp-netlib对Cookie的支持,直接贴代码。

源码清单

#include <thread>
#include <boost/network/protocol/http/server.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>#include <iostream>
#include <map>
#include <string>
#include <algorithm>
#include <sstream>
#include <iomanip>
using namespace std;namespace http = boost::network::http;
namespace utils = boost::network::utils;struct counter_server;
typedef http::server<counter_server> server;//用来存放每个seesion对应的计数值。
//由于这里的代码目的是做技术可行性分析,所以没做线程同步控制.
map<string, int> g_session;//在Cookie中标记我们session的key
string g_sessionKey("CSessionID");//用于为每个session分配不同的编号
unsigned g_sessionIndex = 0;/*
《http状态码一览表以及HTTP响应的返回头信息(支持跳转》
http://blog.sina.com.cn/s/blog_415bd7070100en9i.html
*///
struct counter_server {explicit counter_server() {}void operator()(server::request const &request,server::connection_ptr connection) {//找Cookie.string cookie;for (const auto& header : request.headers) {std::string lowerCaseName;lowerCaseName.resize(header.name.size());std::transform(header.name.begin(), header.name.end(), lowerCaseName.begin(), ::tolower);if (lowerCaseName == "cookie"){cookie = header.value;break;}//if}//for//如果有Cookie,把session id分离出来。std::string sessionID;if (cookie.empty() == false){std::vector<std::string> vecRec;boost::split(vecRec, cookie, boost::is_any_of(";"));for (unsigned i = 0; i < vecRec.size(); i++){std::vector<std::string> vecSub;boost::split(vecSub, vecRec[i], boost::is_any_of("="));if (vecSub.size() == 2){boost::trim_if(vecSub[0], boost::is_any_of("\" \n\r\t'"));boost::trim_if(vecSub[1], boost::is_any_of("\" \n\r\t'"));if (vecSub[0] == g_sessionKey){sessionID = vecSub[1];break;}}//end if}//end for}//end if//如果没有session id,分配一个session idif (sessionID.empty()||g_session.find(sessionID)==g_session.end()){g_sessionIndex++;sessionID = std::to_string(g_sessionIndex);g_session[sessionID] = 0;}g_session[sessionID]++;string content = [](const std::string &sessionID)->string{stringstream ss;ss << "sessionID=" << sessionID << "," << "counter=" << g_session[sessionID];return ss.str();}(sessionID);static server::response_header headers[] = {{ "Server", "cpp-netlib-0.12.0-final" },{ "Connection", "close" },{ "Content-Type", "text/plain" },//Content-Type: text/html; charset=UTF-8{ "Set-Cookie", "" },{ "Content-Length", "0" },{ "Access-Control-Allow-Origin", "*" }, //没有这个设定chrome每次会让服务端这个方法进入两次。{ "Cache-Control", "no-cache" },{ "Date", "Fri, 02 Jun 2017 01:24:55 GMT" }};stringstream ss;ss << g_sessionKey << "=" << sessionID << "; path=/";headers[3].value = ss.str();//Set-Cookie: lasthandler=1247645572; expires=Wed, 15-Jul-2009 09:12:52 GMT; path=/; domain=yoka.comheaders[4].value = std::to_string(content.size());headers[7].value = []() ->string{using namespace boost::gregorian;date today(day_clock::universal_day());//获取GMT日期const boost::posix_time::ptime now(today,boost::posix_time::second_clock::universal_time().time_of_day());const boost::posix_time::time_duration td = now.time_of_day();stringstream ss;ss << today.day_of_week() << ", " << today.day() << " " << today.month() << " " << today.year() << " ";ss << setw(2) << setfill('0') << td.hours() << ":";ss << setw(2) << setfill('0') << td.minutes() << ":";ss << setw(2) << setfill('0') << td.seconds() << " GMT";return ss.str();}();connection->set_status(server::connection::ok);//200connection->set_headers(boost::make_iterator_range(headers, headers + 8));connection->write(content);}
};#ifdef _WIN32
#pragma comment(lib, "cppnetlib-server-parsers.lib")
#endifint main(int argc, char** argv)
{cout << "技术可行性测试!" << endl;cout << "通过一个计数器Demo,来测试Cookie的支持!" << endl;try {counter_server handler;server::options options(handler);server instance(options.thread_pool(std::make_shared<utils::thread_pool>(4)).address("0.0.0.0").port("8080"));instance.run();}catch (std::exception &e) {std::cerr << e.what() << std::endl;}return 0;
}

总结 

通过这个示例,证实cpp-netlib支持session是没有问题的。

cpp-netlib笔记三-Cookie支持测试相关推荐

  1. 爬虫笔记三 cookie 和 session和 正则 和 数据类型 和 代理

    cookie 和 session 产生的原因:http协议无状态,访问之间不能通讯 cookie 指某些网站为了辨别用户身份.进行会话跟踪而存储在用户本地终端上的数据. 做爬虫时,使用账号登录,封装c ...

  2. ESP32 开发笔记(三)源码示例 24_XPT2046_Touch_Test 电阻触摸XPT2046驱动测试触摸校正

    开发板购买链接 https://item.taobao.com/item.htm?spm=a2oq0.12575281.0.0.50111deb2Ij1As&ft=t&id=62636 ...

  3. b 计数器位选信号 verilog_Verilog HDL菜鸟学习笔记———三、Verilog常用语法之一...

    1.一个完整版实例 上一次Verilog学习笔记中,我通过几个小例子,较为直观的对Verilog编程有了一些了解.这次笔记,我开始着重的系统学习Verilog编程语法基础.在我系统学习语法之前,我先用 ...

  4. 构建高性能WEB站点笔记三

    构建高性能WEB站点笔记三 第10章 分布式缓存 10.1数据库的前端缓存区 文件系统内核缓冲区,位于物理内存的内核地址空间,除了使用O_DIRECT标记打开的文件以外,所有对磁盘文件的读写操作都要经 ...

  5. 《MFC游戏开发》笔记三 游戏贴图与透明特效的实现

    本系列文章由七十一雾央编写,转载请注明出处. http://blog.csdn.net/u011371356/article/details/9313239 作者:七十一雾央 新浪微博:http:// ...

  6. 【Visual C++】游戏开发笔记三十三 浅墨DirectX提高班之二 化腐朽为神奇:DirectX初始化四步曲

    这篇文章里,我们将迈出精通DirectX的第一步,先了解典型Direct3D程序的书写流程,然后学习COM接口的对象的一些思想,然后按照"四步曲"的思路,系统地学习DirectX的 ...

  7. K8S 学习笔记三 核心技术 Helm nfs prometheus grafana 高可用集群部署 容器部署流程

    K8S 学习笔记三 核心技术 2.13 Helm 2.13.1 Helm 引入 2.13.2 使用 Helm 可以解决哪些问题 2.13.3 Helm 概述 2.13.4 Helm 的 3 个重要概念 ...

  8. Spring框架学习笔记(三)(AOP,事务管理)

    Spring框架学习笔记(三) 九.AOP 9.1 AOP的注解配置 (1) 新建计算器核心功能(模拟:不能在改动核心代码) (2) 建立一个普通的Java类写增强代码(面向切面编程),使用Sprin ...

  9. SurfaceFlinger学习笔记(三)之SurfaceFlinger进程

    概述 本系列是基于android Q 即android10 SurfaceFlinger学习笔记(一)应用启动流程 SurfaceFlinger学习笔记(二)之Surface SurfaceFling ...

最新文章

  1. 《评人工智能如何走向新阶段》后记(再续3)
  2. 第四课.Python函数(一)
  3. 把用户体验设计放到底层逻辑架构设计的前面去做
  4. 产业结盟 跨界共赢 | 新华三成为“中国联通物联网产业联盟” 首批成员
  5. 塞尔达传说gba_回顾 | 猹鱼主题速写:塞尔达传说 英国绅士
  6. Delphi指针大全
  7. 一个关于c++ list迭代器的问题
  8. CodeForces - 137D Palindromes(dp+路径输出)
  9. 编程与算法中的端点问题(linspace(a, b, n),endpoint)
  10. commons-beanutils实现java深度克隆
  11. 7代cpu能装虚拟xp系统吗_【精选】减少DCS系统故障的实例与措施
  12. STM32 避障小车 —— HC-SR04
  13. excel两个表格数据合并
  14. python cnn 回归模型_【深度学习系列】CNN模型的可视化
  15. 苹果在线商店开售官方翻新Retina MacBook Pro
  16. curl: (51) Unable to communicate securely with peer: requested domain name does not match the server
  17. 字蛛font-spider报错,web font not found,碰到的最新问题及解决方法
  18. 【读书笔记】好好思考-成甲
  19. 最美中国字|“粘”字书写技巧这样写,保证工整又好看!
  20. 爬虫:爬取糗事百科数据

热门文章

  1. 简历上的 10 项最佳技能(附示例和常见问题解答)
  2. lammps计算团簇的方法
  3. laysns模板系统仿善恶模版 完美自适应
  4. Android 反编译资料
  5. clahe(限制对比度自适应直方图均衡化)
  6. 2013-9-7中文幽默演讲比赛-我的演讲之路
  7. 防爆等级介绍 - IP65防爆等级和dIIBT4防爆等级的有什么区别?
  8. C语言——switch语句
  9. 提高网站打开速度的7大秘诀
  10. Facebook营销常犯的错误,看看你中招了吗?