libevevnt-http客户端和服务器端常用函数

本文档详细介绍了libevent-http库中用于客户端和服务器端的接口,包括设置连接关闭回调、URI编码解码、请求超时管理、HTTP请求处理及响应发送等关键操作。这些接口对于构建高性能的HTTP应用至关重要。
摘要由CSDN通过智能技术生成

官网文档

libevevnt-http客户端

evbuffer

请求超时的正确打开姿势 -美图大佬

1.客户端相关接口

index函数原型函数的作用
1
2

void evhttp_connection_set_closecb(       struct evhttp_connection* evcon,void(*)(struct evhttp_connection*, void* ),void*

);

 设置一个连接关闭的回调

3char* evhttp_decode_uri( const char* uri );解码URI,返回值必须自己释放掉
4char* evhttp_encode_uri( const char* uri)编码URI,返回值需要释放
5char* evhttp_htmlescape( const char* html )忽略html的字符
6void evhttp_parse_query(   const char* uri,struct evkeyvalq*)解析请求里的参数
7struct evhttp_request_free( struct evhttp_request* req )释放http请求
8void evhttp_set_time_out ( struct evhttp*,int timeout_in_sec);设置HTTP请求超时时间
9


void evhttp_request_set_header_cb (struct evhttp_request *, int(*cb)(struct evhttp_request *, void *))

设置回调函数,在包头读取完成后回调
10void evhttp_request_set_chunked_cb (struct evhttp_request *, void(*cb)(struct evhttp_request *, void *))设置回调函数,在body有数据返回后回调
11struct evhttp_request* evhttp_request_new(    void(*)(struct evhttp_request*, void*)cb,void* arg ); 参数中的回调函数在完成一个请求的时候或者发生错误时调用
12

evhttp_request_get_output_headers

获取请求的header
13int evhttp_add_header(struct evkeyvalq *headers, const char *key, const char *value);有选择的向evhttp_request添加包头字段

2.服务器端相关接口

index
1int evhttp_accept_socket(struct evhttp* http, int    fd);

使HTTP服务器在指定端口上接受连接。

可以多次调用使server监听多个不同的socket

int evhttp_bind_socket( struct evhttp* http,const char*  address,u_short      port);给服务器绑定一个指定的地址和端口
void evhttp_free( struct evhttp* hrttp );释放创建的HTTP server
struct evhttp* evhttp_start( const char* address, u_short    port);在指定的地址和端口上启动一个HTTP服务器
void evhttp_set_cb(     struct evhttp*,const char*,void(*)(struct evhttp_request* , void*),void* ) 给指定的URI设置的回调
void evhttp_set_gencb( struct evhttp*,const char*,void(*)(struct evhttp_request* , void*),void* )       给所有的请求(不包含上面的特定回调的)设置的回调函数
9void evhttp_send_error ( struct evhttp_request* req,int error,const char* reason);

 发送一个html错误消息给客户端

error:  http错误码

reason:错误的简单描述

10void evhttp_send_reply( struct evhttp_request* req,int code,const char* reason,struct evbuffer* databuf)

     发送一个html回复给客户端

       code       : HTTP返回码

       reason    : 返回码的消息

       databuf  : 返回内容的body部分

1struct evhttp* evhttp_new(struct event_base* base)创建一个新的HTTP server


 


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值