目录
一、环境
1.vs2019
2.cmake
3.OpenSSL
二、安装
1.安装vs2019
选择C++模块,修改下安装位置,其他默认安装即可
2.安装cmake
cmake在安装vs2019时会自带安装
配置环境变量:path路径下添加cmake的bin路径
C:\Tools\Microsoft Visual Studio\2019\Community\Common7\IDE\CommonExtensions\Microsoft\CMake\CMake\bin
验证:使用命令行输入 cmake --version
3.安装OpenSSL
下载地址:Win32/Win64 OpenSSL Installer for Windows - Shining Light Productions (slproweb.com)
注意一定要选择不带light的版本,推荐选择msi后缀
下载后进行安装,选择安装路径后默认安装即可
环境变量配置:
path变量下添加路径OpenSSL的bin路径:
如:C:\Program Files\OpenSSL-Win64\bin
验证:使用命令行输入 OpenSSL
三、编译
1.克隆代码
首先clone workflow代码 git clone https://github.com/sogou/workflow.git
克隆后切换分支到Windows版本
2.编译
进入workflow的根目录,然创建一个目录如:build
打开cmd,键入命令进行编译(参考编译文档:workflow/README_cn.md at windows · sogou/workflow · GitHub)
如果配置了OpenSSL环境变量则使用cmake -B build -S .
如果未配置则使用cmake -B build -S . -DOPENSSL_ROOT_DIR=[openssl directory]
随后会在build目录中生成一堆文件,双击.sln打开工程
双击打开后直接点击生成
workflow文件夹下会产生_include和_lib两个文件夹,此时编译完成
四、创建项目并引入workflow
1.创建项目
注:一定选择x64
2.配置
①右键项目名=》属性=》VC++目录=》包含目录
添加OpenSSL的include目录和之前生成workflow的_include目录
②右键项目名=》属性=》VC++目录=》库目录
添加OpenSSL的lib目录下的VC目录和之前生成workflow的_lib目录下的Debug目录
③右键项目名=》属性=》链接器=》输入=》附加依赖项
workflow.lib
libssl64MDd.lib
libcrypto64MDd.lib
ws2_32.lib
wsock32.lib
④右键项目名=》属性=》C/C++=》代码生成=》运行库=》多线程调试(/MTd)
到此配置完成
3.测试
使用tutorial-01-wget.cc进行测试
/*
Copyright (c) 2019 Sogou, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Author: Xie Han (xiehan@sogou-inc.com;63350856@qq.com)
*/
#include <signal.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <string>
#include "workflow/HttpMessage.h"
#include "workflow/HttpUtil.h"
#include "workflow/WFTaskFactory.h"
#ifndef _WIN32
#include <unistd.h>
#endif
#ifdef _WIN32
#define strcasecmp _stricmp
#define strncasecmp _strnicmp
#endif
#define REDIRECT_MAX 5
#define RETRY_MAX 2
void wget_callback(WFHttpTask* task)
{
protocol::HttpRequest* req = task->get_req();
protocol::HttpResponse* resp = task->get_resp();
int state = task->get_state();
int error = task->get_error();
switch (state)
{
case WFT_STATE_SYS_ERROR:
fprintf(stderr, "system error: %s\n", strerror(error));
break;
case WFT_STATE_DNS_ERROR:
fprintf(stderr, "DNS error: %s\n", gai_strerror(error));
break;
case WFT_STATE_SSL_ERROR:
fprintf(stderr, "SSL error: %d\n", error);
break;
case WFT_STATE_TASK_ERROR:
fprintf(stderr, "Task error: %d\n", error);
break;
case WFT_STATE_SUCCESS:
break;
}
if (state != WFT_STATE_SUCCESS)
{
fprintf(stderr, "Failed. Press Ctrl-C to exit.\n");
return;
}
std::string name;
std::string value;
/* Print request. */
fprintf(stderr, "%s %s %s\r\n", req->get_method(),
req->get_http_version(),
req->get_request_uri());
protocol::HttpHeaderCursor req_cursor(req);
while (req_cursor.next(name, value))
fprintf(stderr, "%s: %s\r\n", name.c_str(), value.c_str());
fprintf(stderr, "\r\n");
/* Print response header. */
fprintf(stderr, "%s %s %s\r\n", resp->get_http_version(),
resp->get_status_code(),
resp->get_reason_phrase());
protocol::HttpHeaderCursor resp_cursor(resp);
while (resp_cursor.next(name, value))
fprintf(stderr, "%s: %s\r\n", name.c_str(), value.c_str());
fprintf(stderr, "\r\n");
/* Print response body. */
const void* body;
size_t body_len;
resp->get_parsed_body(&body, &body_len);
fwrite(body, 1, body_len, stdout);
fflush(stdout);
fprintf(stderr, "\nSuccess. Press Ctrl-C to exit.\n");
}
void sig_handler(int signo) { }
int main(int argc, char* argv[])
{
WFHttpTask* task;
if (argc != 2)
{
fprintf(stderr, "USAGE: %s <http URL>\n", argv[0]);
exit(1);
}
signal(SIGINT, sig_handler);
std::string url = argv[1];
if (strncasecmp(argv[1], "http://", 7) != 0 &&
strncasecmp(argv[1], "https://", 8) != 0)
{
url = "http://" + url;
}
task = WFTaskFactory::create_http_task(url, REDIRECT_MAX, RETRY_MAX,
wget_callback);
protocol::HttpRequest* req = task->get_req();
req->add_header_pair("Accept", "*/*");
req->add_header_pair("User-Agent", "Wget/1.14 (linux-gnu)");
req->add_header_pair("Connection", "close");
task->start();
#ifndef _WIN32
pause();
#else
getchar();
#endif
return 0;
}
此时会报一个'strerror': This function or variable may be unsafe. Consider using strerror_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details. 按照提示在预处理器中添加宏
右键项目名=》属性=》C/C++=》预处理器=》预处理器宏定义=》_CRT_SECURE_NO_WARNINGS
此时运行程序控制台未能正常输出,因为只有一个参数
最后在调试中添加调试的第二个参数:
再次运行,正常输出