libevent高并发网络编程 - 05_libevent实现http客户端


链接: C/C++Linux服务器开发/后台架构师【零声教育】-学习视频教程-腾讯课堂

1 http客户端相关的API

evhttp_uri_parse() 用于解析 URI 字符串并创建一个 evhttp_uri 结构体表示该 URI

evhttp_uri_parse()

struct evhttp_uri *
			evhttp_uri_parse(const char *source_uri)
    
返回一个指向 evhttp_uri 结构体的指针,该结构体包含了 URI 的各个组成部分(包括协议、主机名、端口号、路径、查询参数和片段标识符等)
    
    
struct evhttp_uri {
	unsigned flags;
	char *scheme; 	/* scheme; e.g http, ftp etc */
	char *userinfo; /* userinfo (typically username:pass), or NULL */
	char *host; 	/* hostname, IP address, or NULL */
	int port; 		/* port, or zero */
#ifndef _WIN32
	char *unixsocket; /* unix domain socket or NULL */
#endif
	char *path; 	/* path, or "". */
	char *query; 	/* query, or NULL */
	char *fragment; /* fragment or NULL */
};

evhttp_uri_get_scheme()

evhttp_uri_get_scheme() 用于获取 URI 的协议部分(即 :// 前面的部分)

const char *
evhttp_uri_get_scheme(const struct evhttp_uri *uri)
{
	return uri->scheme;
}

函数返回一个字符串指针,指向 URI 的协议部分。如果 URI 中没有明确指定协议,则返回空指针。
    
http://ffmpeg.club/index.html?id=1 =》 http

evhttp_uri_get_port()

evhttp_uri_get_port() 用于获取 URI 的端口号

int
evhttp_uri_get_port(const struct evhttp_uri *uri)
{
	return uri->port;
}

函数返回一个整数,表示 URI 的端口号。如果 URI 中没有明确指定端口号,则返回默认值(HTTP 协议默认端口为 80,HTTPS 协议默认端口为 443)
    
http://ffmpeg.club/index.html?id=1 =》 80

evhttp_uri_get_host()

evhttp_uri_get_host() 用于获取 URI 的主机名部分。

const char *
evhttp_uri_get_host(const struct evhttp_uri *uri)
{
	return uri->host;
}

函数返回一个字符串指针,指向 URI 的主机名部分。如果 URI 中没有明确指定主机名,则返回空指针。
    
http://ffmpeg.club/index.html?id=1 =》 ffmpeg.club

evhttp_uri_get_path()

evhttp_uri_get_path() 用于获取 URI 的路径部分。

const char *
evhttp_uri_get_path(const struct evhttp_uri *uri)
{
	return uri->path;
}

函数返回一个字符串指针,指向 URI 的路径部分。如果 URI 中没有明确指定路径,则返回空字符串。
    
http://ffmpeg.club/index.html?id=1 =》 /index.html

evhttp_uri_get_query()

evhttp_uri_get_query() 用于获取 URI 的查询部分,获取路径后面的参数。

const char *
evhttp_uri_get_query(const struct evhttp_uri *uri)
{
	return uri->query;
}

http://ffmpeg.club/index.html?id=1 =》 id=1

evhttp_connection_base_bufferevent_new()

evhttp_connection_base_bufferevent_new() 用于创建一个 evhttp_connection 对象,表示基于网络连接的 HTTP 客户端

该函数使用一个 bufferevent 对象来处理底层的网络 I/O,并使用指定的 DNS 解析器解析主机名。如果 dnsbase 参数为 NULL,则表示使用默认的全局 DNS 解析器。

struct evhttp_connection *
	evhttp_connection_base_bufferevent_new(struct event_base *base, 
                                           struct evdns_base *dnsbase, 
                                           struct bufferevent* bev,
                                           const char *address, 
                                           unsigned short port)
参数:
    base:事件处理器对象,用于安排事件处理和超时。
    dnsbase:指定的 DNS 解析器解析主机名。如果dnsbase 参数为 NULL,则表示使用默认的全局 DNS 解析器。
    bev: bufferevent对象
    address:服务器的 IP 地址或主机名。
    port:服务器的端口号。

evhttp_request_new()

evhttp_request_new() 用于创建一个 evhttp_request 对象,表示 HTTP 请求

struct evhttp_request *
			evhttp_request_new(void (*cb)(struct evhttp_request *, void *), 
                               void *arg)
该函数需要以下参数:
    cb:回调函数,当请求处理完成后调用。可以为 NULL,表示不需要回调函数。
    arg:传递给回调函数的额外参数。
    
    

需要注意的是,在使用完 evhttp_request 对象后,必须手动释放其所占用的内存空间:

evhttp_request_free(req);

evhttp_make_request()

evhttp_make_request() 用于向指定的 evhttp_connection 对象发起 HTTP 请求。

int
evhttp_make_request(struct evhttp_connection *evcon,
    				struct evhttp_request *req,
    				enum evhttp_cmd_type type, 
                    const char *uri)
    
该函数需要以下参数:
    evcon:表示要使用的 evhttp_connection 对象。
    req:表示要发送的 HTTP 请求。
    type:表示 HTTP 方法类型,可以是 GET、POST、DELETE 等等。
    uri:表示请求的 URI。

evhttp_request_get_response_code()

获取一个HTTP请求的响应状态码

int
evhttp_request_get_response_code(const struct evhttp_request *req)
{
	return req->response_code;
}

evhttp_request_get_response_code_line()

返回参数req所指向的结构体中的response_code_line成员变量的值,即HTTP响应状态码行,例如“HTTP/1.1 200 OK”。因此,调用该函数可以获取一个HTTP请求的完整响应状态行

const char *
evhttp_request_get_response_code_line(const struct evhttp_request *req)
{
	return req->response_code_line;
}

evbuffer_add_printf()

int
	evbuffer_add_printf(struct evbuffer *buf, 
                        const char *fmt, ...)

2 编写http客户端的流程

对url解析端口、主机名部分、路径部分等

发送http请求前,需要对目标的URL进行解析。

例如:http://ffmpeg.club/index.html?id=1
协议部分:http
端口号:80
主机名部分:ffmpeg.club
路径部分:/index.html
参数:id=1
string http_url = "http://ffmpeg.club/index.html?id=1";
//http_url = "http://ffmpeg.club/101.jpg";

// 分析url地址
// 解析 URI 字符串并创建一个 evhttp_uri 结构体表示该 URI
evhttp_uri *uri = evhttp_uri_parse(http_url.c_str());

// http https 获取 URI 的协议部分(即 :// 前面的部分)
const char *scheme = evhttp_uri_get_scheme(uri);
if (!scheme)
{
    cerr << "scheme is null" << endl;
    return -1;
}
cout << "scheme is " << scheme << endl;

//获取 URI 的端口号
int port = evhttp_uri_get_port(uri);
if (port < 0)
{
    if (strcmp(scheme, "http") == 0)
        port = 80;
}
cout << "port is " << port << endl;

//获取 URI 的主机名部分 host ffmpeg.club 
const char *host = evhttp_uri_get_host(uri);
if (!host)
{
    cerr << "host is null" << endl;
    return -1;
}
cout << "host is " << host << endl;

//获取 URI 的路径部分
const char *path = evhttp_uri_get_path(uri);
if (!path || strlen(path) == 0)
{
    path = "/";
}
if (path)
    cout << "path is " << path << endl;

//获取 URI 的查询部分,获取路径后面的参数
//?id=1  后面的内容 id=1
const char *query = evhttp_uri_get_query(uri);
if (query)
    cout << "query is " << query << endl;
else
    cout << "query is NULL" << endl;
输出结果:
    event_base_new success!
    scheme is http
    port is 80
    host is ffmpeg.club
    path is /index.html
    query is id=1

完成http客户端的请求

需要先创建一个 evhttp_connection 对象,evhttp_request_new并指定响应回调函数,配置请求头部和请求数据,最后通过evhttp_make_request指定请求类型和请求uri发送http请求。

// bufferevent  连接http服务器
bufferevent *bev = bufferevent_socket_new(base, -1, BEV_OPT_CLOSE_ON_FREE);

//创建一个 evhttp_connection 对象,表示基于网络连接的 HTTP 客户端
evhttp_connection *evcon = evhttp_connection_base_bufferevent_new(base,
                                                                  NULL, bev, host, port);

//http client  请求 回调函数设置
evhttp_request *req = evhttp_request_new(http_client_cb, base);

// 设置请求的head 消息报头 信息
evkeyvalq *output_headers = evhttp_request_get_output_headers(req);
evhttp_add_header(output_headers, "Host", host);

//向指定的 evhttp_connection 对象发起 HTTP 请求
evhttp_make_request(evcon, req, EVHTTP_REQ_GET, path);

响应回调函数

服务器响应请求后,客户端会调用evhttp_request_new的回调函数,在请求回调函数中处理返回的数据。

cout << "http_client_cb" << endl;
event_base *base = (event_base *)ctx;
//服务端响应错误
if (req == NULL)
{
    int errcode = EVUTIL_SOCKET_ERROR();
    cout << "socket error:" << evutil_socket_error_to_string(errcode) << endl;
    return;
}

//获取path 
const char *path = evhttp_request_get_uri(req);
cout << "request path is " << path << endl;
string filepath = ".";
filepath += path;
cout << "filepath is " << filepath << endl;

//如果路径中有目录,需要分析出目录,并创建
FILE *fp = fopen(filepath.c_str(), "wb");
if (!fp)
{
    cout << "open file " << filepath<<" failed!" << endl;
}

//获取一个HTTP请求的响应状态码 200 404
cout << "Response :" << evhttp_request_get_response_code(req); 
//HTTP响应状态码行,例如“HTTP/1.1 200 OK”
cout <<" "<< evhttp_request_get_response_code_line(req) << endl;

char buf[1024] = {0};
evbuffer *input = evhttp_request_get_input_buffer(req);
for (;;)
{
    int len = evbuffer_remove(input,buf,sizeof(buf)-1);
    if (len <= 0)break;
    buf[len] = 0;
    if (!fp)
        continue;
    fwrite(buf, 1, len, fp);
    //cout << buf << flush;
}
if (fp)
    fclose(fp);

//退出循环
event_base_loopbreak(base);
输出结果:
    http_client_cb
    request path is /index.html
    filepath is ./index.html
    Response :200 

3 完成的http客户端程序

实现http客户端的GET、POST请求编写,请求服务器的文件并保存。

#include <event2/event.h>
#include <event2/listener.h>
#include <event2/http.h>
#include <event2/bufferevent.h>
#include <event2/buffer.h>
#include <string.h>
#include <string>
#ifndef _WIN32
#include <signal.h>
#endif
#include <iostream>
using namespace std;

/*响应回调函数*/
void http_client_cb(struct evhttp_request *req, void *ctx)
{
	cout << "http_client_cb" << endl;
	event_base *base = (event_base *)ctx;
	//服务端响应错误
	if (req == NULL)
	{
		int errcode = EVUTIL_SOCKET_ERROR();
		cout << "socket error:" << evutil_socket_error_to_string(errcode) << endl;
		return;
	}

	//获取path 
	const char *path = evhttp_request_get_uri(req);
	cout << "request path is " << path << endl;
	string filepath = ".";
	filepath += path;
	cout << "filepath is " << filepath << endl;
	
	//如果路径中有目录,需要分析出目录,并创建
	FILE *fp = fopen(filepath.c_str(), "wb");
	if (!fp)
	{
		cout << "open file " << filepath<<" failed!" << endl;
	}

	//获取一个HTTP请求的响应状态码 200 404
	cout << "Response :" << evhttp_request_get_response_code(req); 
	//HTTP响应状态码行,例如“HTTP/1.1 200 OK”
	cout <<" "<< evhttp_request_get_response_code_line(req) << endl;

	char buf[1024] = {0};
	evbuffer *input = evhttp_request_get_input_buffer(req);
	for (;;)
	{
		int len = evbuffer_remove(input,buf,sizeof(buf)-1);
		if (len <= 0)break;
		buf[len] = 0;
		if (!fp)
			continue;
		fwrite(buf, 1, len, fp);
		//cout << buf << flush;
	}
	if (fp)
		fclose(fp);

	//退出循环
	event_base_loopbreak(base);
}

/*发送GET请求*/
int TestGetHttp()
{
	//创建libevent的上下文
	event_base * base = event_base_new();
	if (base)
	{
		cout << "event_base_new success!" << endl;
	}

	//   生成请求信息 GET
	string http_url = "http://ffmpeg.club/index.html?id=1";
	//http_url = "http://ffmpeg.club/101.jpg";
	http_url = "http://127.0.0.1:8080/index.html";

	// 分析url地址
	// 解析 URI 字符串并创建一个 evhttp_uri 结构体表示该 URI
	evhttp_uri *uri = evhttp_uri_parse(http_url.c_str());

	// http https 获取 URI 的协议部分(即 :// 前面的部分)
	const char *scheme = evhttp_uri_get_scheme(uri);
	if (!scheme)
	{
		cerr << "scheme is null" << endl;
		return -1;
	}
	cout << "scheme is " << scheme << endl;

	//获取 URI 的端口号
	int port = evhttp_uri_get_port(uri);
	if (port < 0)
	{
		if (strcmp(scheme, "http") == 0)
			port = 80;
	}
	cout << "port is " << port << endl;

	//获取 URI 的主机名部分 host ffmpeg.club 
	const char *host = evhttp_uri_get_host(uri);
	if (!host)
	{
		cerr << "host is null" << endl;
		return -1;
	}
	cout << "host is " << host << endl;

	//获取 URI 的路径部分
	const char *path = evhttp_uri_get_path(uri);
	if (!path || strlen(path) == 0)
	{
		path = "/";
	}
	if (path)
		cout << "path is " << path << endl;

	//获取 URI 的查询部分,获取路径后面的参数
	//?id=1  后面的内容 id=1
	const char *query = evhttp_uri_get_query(uri);
	if (query)
		cout << "query is " << query << endl;
	else
		cout << "query is NULL" << endl;

	// bufferevent  连接http服务器
	bufferevent *bev = bufferevent_socket_new(base, -1, BEV_OPT_CLOSE_ON_FREE);

	//创建一个 `evhttp_connection` 对象,表示基于网络连接的 HTTP 客户端
	evhttp_connection *evcon = evhttp_connection_base_bufferevent_new(base,
		NULL, bev, host, port);

	//http client  请求 回调函数设置
	evhttp_request *req = evhttp_request_new(http_client_cb, base);

	// 设置请求的head 消息报头 信息
	evkeyvalq *output_headers = evhttp_request_get_output_headers(req);
	evhttp_add_header(output_headers, "Host", host);

	//向指定的 evhttp_connection 对象发起 HTTP 请求
	evhttp_make_request(evcon, req, EVHTTP_REQ_GET, path);
	

	//事件分发处理
	if (base)
		event_base_dispatch(base);
	if (uri)evhttp_uri_free(uri);
	if (evcon)evhttp_connection_free(evcon);
	if (base)
		event_base_free(base);
}

int TestPostHttp()
{
	//创建libevent的上下文
	event_base * base = event_base_new();
	if (base)
	{
		cout << "event_base_new success!" << endl;
	}

	//   生成请求信息 GET
	string http_url = "http://127.0.0.1:8080/index.html";

	// 分析url地址
	// 解析 URI 字符串并创建一个 evhttp_uri 结构体表示该 URI
	evhttp_uri *uri = evhttp_uri_parse(http_url.c_str());

	// http https 获取 URI 的协议部分(即 :// 前面的部分)
	const char *scheme = evhttp_uri_get_scheme(uri);
	if (!scheme)
	{
		cerr << "scheme is null" << endl;
		return -1;
	}
	cout << "scheme is " << scheme << endl;

	//获取 URI 的端口号
	int port = evhttp_uri_get_port(uri);
	if (port < 0)
	{
		if (strcmp(scheme, "http") == 0)
			port = 80;
	}
	cout << "port is " << port << endl;

	//获取 URI 的主机名部分 host ffmpeg.club 
	const char *host = evhttp_uri_get_host(uri);
	if (!host)
	{
		cerr << "host is null" << endl;
		return -1;
	}
	cout << "host is " << host << endl;

	//获取 URI 的路径部分
	const char *path = evhttp_uri_get_path(uri);
	if (!path || strlen(path) == 0)
	{
		path = "/";
	}
	if (path)
		cout << "path is " << path << endl;

	//获取 URI 的查询部分,获取路径后面的参数
	//?id=1  后面的内容 id=1
	const char *query = evhttp_uri_get_query(uri);
	if (query)
		cout << "query is " << query << endl;
	else
		cout << "query is NULL" << endl;

	// bufferevent  连接http服务器
	bufferevent *bev = bufferevent_socket_new(base, -1, BEV_OPT_CLOSE_ON_FREE);

	//创建一个 `evhttp_connection` 对象,表示基于网络连接的 HTTP 客户端
	evhttp_connection *evcon = evhttp_connection_base_bufferevent_new(base,
		NULL, bev, host, port);

	//http client  请求 回调函数设置
	evhttp_request *req = evhttp_request_new(http_client_cb, base);

	// 设置请求的head 消息报头 信息
	evkeyvalq *output_headers = evhttp_request_get_output_headers(req);
	evhttp_add_header(output_headers, "Host", host);

	//发送post数据
	evbuffer *output = evhttp_request_get_output_buffer(req);
	evbuffer_add_printf(output, "xcj=%d&b=%d", 1, 2);

	//向指定的 evhttp_connection 对象发起 HTTP 请求
	evhttp_make_request(evcon, req, EVHTTP_REQ_POST, path);
	

	//事件分发处理
	if (base)
		event_base_dispatch(base);
	if (uri)evhttp_uri_free(uri);
	if (evcon)evhttp_connection_free(evcon);
	if (base)
		event_base_free(base);
}

int main()
{
#ifdef _WIN32 
	//初始化socket库
	WSADATA wsa;
	WSAStartup(MAKEWORD(2,2),&wsa);
#else
	//忽略管道信号,发送数据给已关闭的socket
	if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
		return 1;
#endif
	std::cout << "test http client!\n";
	TestGetHttp();	//发送GET请求
	TestPostHttp();	//发送POST请求
    
	
#ifdef _WIN32
	WSACleanup();
#endif
	return 0;
}

在这里插入图片描述

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
除了多进程、I/O多路复用和libevent开发,还有其他一些技术可以实现高并发服务器,包括以下几种常见的方法: 1. 多线程模型:使用多线程可以实现并发处理客户端请求。每个线程独立处理一个客户端连接,通过线程池来管理和复用线程,避免频繁创建和销毁线程的开销。多线程模型相对于多进程模型来说,线程间的切换开销较小,但需要注意线程安全性和共享资源的同步问题。 2. 单线程+异步非阻塞模型:在单线程模型中使用异步非阻塞的方式处理客户端请求。通过设置套接字为非阻塞模式,并使用事件驱动机制(如epoll、kqueue等)来监听和处理事件。当有事件发生时,通过异步方式处理事件,不会阻塞主线程。这种模型适用于I/O密集型的任务,但需要注意编程复杂性和回调函数的处理。 3. 协程模型:协程是一种轻量级的线程,可以在一个线程内实现多个协程的切换和调度。通过使用协程库(如Goroutine、Coroutine等),可以实现高并发服务器。协程模型可以减少线程切换的开销,并且编程模型更简单,但需要注意协程的调度和同步问题。 4. 多进程+异步非阻塞模型:结合多进程和异步非阻塞模型,可以实现高并发服务器。每个子进程使用异步非阻塞的方式处理客户端请求,通过进程间通信机制(如消息队列、共享内存等)进行数据传递和同步。这种模型可以充分利用多核CPU的优势,但需要注意进程间通信的复杂性和资源消耗。 5. 分布式服务器:使用分布式技术可以实现高并发服务器。将服务器拆分成多个独立的节点,每个节点负责处理部分客户端请求。通过负载均衡器来分发请求,实现请求的均衡分配。分布式服务器可以提高系统的可扩展性和容错性,但需要考虑节点间的通信和数据一致性等问题。 以上是几种常见的能够实现高并发服务器的技术。根据具体的应用需求和场景,选择合适的技术方案进行开发。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值